diff mbox series

linux-user: syscall: ioctls: support DRM_IOCTL_VERSION

Message ID 20200226113834.26404-1-chengang@emindsoft.com.cn
State New
Headers show
Series linux-user: syscall: ioctls: support DRM_IOCTL_VERSION | expand

Commit Message

Chen Gang Feb. 26, 2020, 11:38 a.m. UTC
From: Chen Gang <chengang@emindsoft.com.cn>

The other DRM_IOCTL_* commands will be done later.

Signed-off-by: Chen Gang <chengang@emindsoft.com.cn>
---
 linux-user/ioctls.h        |   3 +
 linux-user/syscall.c       | 134 +++++++++++++++++++++++++++++++++++++
 linux-user/syscall_defs.h  |  16 +++++
 linux-user/syscall_types.h |  12 ++++
 4 files changed, 165 insertions(+)

Comments

Laurent Vivier March 10, 2020, 8:16 p.m. UTC | #1
Le 26/02/2020 à 12:38, chengang@emindsoft.com.cn a écrit :
> From: Chen Gang <chengang@emindsoft.com.cn>
> 
> The other DRM_IOCTL_* commands will be done later.
> 
> Signed-off-by: Chen Gang <chengang@emindsoft.com.cn>
> ---
>  linux-user/ioctls.h        |   3 +
>  linux-user/syscall.c       | 134 +++++++++++++++++++++++++++++++++++++
>  linux-user/syscall_defs.h  |  16 +++++
>  linux-user/syscall_types.h |  12 ++++
>  4 files changed, 165 insertions(+)
> 
> diff --git a/linux-user/ioctls.h b/linux-user/ioctls.h
> index 0defa1d8c1..c2294b48a0 100644
> --- a/linux-user/ioctls.h
> +++ b/linux-user/ioctls.h
> @@ -574,6 +574,9 @@
>    IOCTL_SPECIAL(SIOCDELRT, IOC_W, do_ioctl_rt,
>                  MK_PTR(MK_STRUCT(STRUCT_rtentry)))
>  
> +  IOCTL_SPECIAL(DRM_IOCTL_VERSION, IOC_RW, do_ioctl_drm,
> +                MK_PTR(MK_STRUCT(STRUCT_drm_version)))
> +

Rather than adding a specific function to process the structure, perhaps
we can add this in a generic way?

The problem with drm_version structure is the pointers to the strings.

Did you try to add a TYPE_STRING in
thunk_type_size()/thunk_type_align()/think_convert()/do_ioctl() to do that?

...

> diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h
> index 152ec637cb..5e455a32af 100644
> --- a/linux-user/syscall_defs.h
> +++ b/linux-user/syscall_defs.h
> @@ -1167,6 +1167,9 @@ struct target_rtc_pll_info {
>  #define TARGET_DM_TARGET_MSG          TARGET_IOWRU(0xfd, 0x0e)
>  #define TARGET_DM_DEV_SET_GEOMETRY    TARGET_IOWRU(0xfd, 0x0f)
>  
> +/* drm ioctls */
> +#define TARGET_DRM_IOCTL_VERSION      TARGET_IOWRU('d', 0x00)

Why do you use the TARGET_IOWRU variant?

Can't you use TARGET_IOWR('d', 0x00, struct target_drm_version)?

> +
>  /* from asm/termbits.h */
>  
>  #define TARGET_NCC 8
> @@ -2598,6 +2602,18 @@ struct target_mq_attr {
>      abi_long mq_curmsgs;
>  };
>  
> +struct target_drm_version {
> +    int version_major;
> +    int version_minor;
> +    int version_patchlevel;
> +    abi_ulong name_len;
> +    abi_ulong name;
> +    abi_ulong date_len;
> +    abi_ulong date;
> +    abi_ulong desc_len;
> +    abi_ulong desc;
> +};
> +
>  #include "socket.h"
>  
>  #include "errno_defs.h"
> diff --git a/linux-user/syscall_types.h b/linux-user/syscall_types.h
> index 4e12c1661e..52a031ad35 100644
> --- a/linux-user/syscall_types.h
> +++ b/linux-user/syscall_types.h
> @@ -292,6 +292,18 @@ STRUCT(dm_target_versions,
>  STRUCT(dm_target_msg,
>         TYPE_ULONGLONG) /* sector */
>  
> +/* TODO: use #ifdef 32 bit #else 64 bit, next */
> +STRUCT(drm_version,
> +       TYPE_INT, /* version_major */
> +       TYPE_INT, /* version_minor */
> +       TYPE_INT, /* version_patchlevel */
> +       TYPE_ULONG, /* name_len */
> +       TYPE_PTRVOID, /* name */
> +       TYPE_ULONG, /* date_len */
> +       TYPE_PTRVOID, /* date */
> +       TYPE_ULONG, /* desc_len */
> +       TYPE_PTRVOID) /* desc */
> +

After defining a TYPE_STRING, you could use MKPTR(TYPE_STRING) for name,
date and desc.

Thanks,
Laurent
Laurent Vivier March 11, 2020, 7:35 a.m. UTC | #2
Le 10/03/2020 à 21:16, Laurent Vivier a écrit :
> Le 26/02/2020 à 12:38, chengang@emindsoft.com.cn a écrit :
>> From: Chen Gang <chengang@emindsoft.com.cn>
>>
>> The other DRM_IOCTL_* commands will be done later.
>>
>> Signed-off-by: Chen Gang <chengang@emindsoft.com.cn>
>> ---
>>  linux-user/ioctls.h        |   3 +
>>  linux-user/syscall.c       | 134 +++++++++++++++++++++++++++++++++++++
>>  linux-user/syscall_defs.h  |  16 +++++
>>  linux-user/syscall_types.h |  12 ++++
>>  4 files changed, 165 insertions(+)
>>
>> diff --git a/linux-user/ioctls.h b/linux-user/ioctls.h
>> index 0defa1d8c1..c2294b48a0 100644
>> --- a/linux-user/ioctls.h
>> +++ b/linux-user/ioctls.h
>> @@ -574,6 +574,9 @@
>>    IOCTL_SPECIAL(SIOCDELRT, IOC_W, do_ioctl_rt,
>>                  MK_PTR(MK_STRUCT(STRUCT_rtentry)))
>>  
>> +  IOCTL_SPECIAL(DRM_IOCTL_VERSION, IOC_RW, do_ioctl_drm,
>> +                MK_PTR(MK_STRUCT(STRUCT_drm_version)))
>> +
> 
> Rather than adding a specific function to process the structure, perhaps
> we can add this in a generic way?
> 
> The problem with drm_version structure is the pointers to the strings.
> 
> Did you try to add a TYPE_STRING in
> thunk_type_size()/thunk_type_align()/think_convert()/do_ioctl() to do that?

In fact we can't do that because we need to know the size of the buffer
to allocate and it is provided by another field. It cannot be generic,
so I think what you do is the best we can do.

Thanks,
LAurent
Chen Gang March 12, 2020, 8:34 a.m. UTC | #3
Thank you very much for your reply. :)

DRM_IOCTL_VERSION has to be a special one, but my patch really need be
improved. The string pointers should be translated, but the new string
buffers need not be allocated.

I will try to send patch v2 within this week end.

DRM_IOCTL_* really have many commands, at present, I have implemented
about 20+ commands for i965 gpu using, although some of them are not
generic enough. I will try to send all of them when I have free time.

Thanks.

On 2020/3/11 下午3:35, Laurent Vivier wrote:
> Le 10/03/2020 à 21:16, Laurent Vivier a écrit :
>> Le 26/02/2020 à 12:38, chengang@emindsoft.com.cn a écrit :
>>> From: Chen Gang <chengang@emindsoft.com.cn>
>>>
>>> The other DRM_IOCTL_* commands will be done later.
>>>
>>> Signed-off-by: Chen Gang <chengang@emindsoft.com.cn>
>>> ---
>>>  linux-user/ioctls.h        |   3 +
>>>  linux-user/syscall.c       | 134 +++++++++++++++++++++++++++++++++++++
>>>  linux-user/syscall_defs.h  |  16 +++++
>>>  linux-user/syscall_types.h |  12 ++++
>>>  4 files changed, 165 insertions(+)
>>>
>>> diff --git a/linux-user/ioctls.h b/linux-user/ioctls.h
>>> index 0defa1d8c1..c2294b48a0 100644
>>> --- a/linux-user/ioctls.h
>>> +++ b/linux-user/ioctls.h
>>> @@ -574,6 +574,9 @@
>>>    IOCTL_SPECIAL(SIOCDELRT, IOC_W, do_ioctl_rt,
>>>                  MK_PTR(MK_STRUCT(STRUCT_rtentry)))
>>>  
>>> +  IOCTL_SPECIAL(DRM_IOCTL_VERSION, IOC_RW, do_ioctl_drm,
>>> +                MK_PTR(MK_STRUCT(STRUCT_drm_version)))
>>> +
>>
>> Rather than adding a specific function to process the structure, perhaps
>> we can add this in a generic way?
>>
>> The problem with drm_version structure is the pointers to the strings.
>>
>> Did you try to add a TYPE_STRING in
>> thunk_type_size()/thunk_type_align()/think_convert()/do_ioctl() to do that?
> 
> In fact we can't do that because we need to know the size of the buffer
> to allocate and it is provided by another field. It cannot be generic,
> so I think what you do is the best we can do.
> 
> Thanks,
> LAurent
> 
> 
> 
> 
>
diff mbox series

Patch

diff --git a/linux-user/ioctls.h b/linux-user/ioctls.h
index 0defa1d8c1..c2294b48a0 100644
--- a/linux-user/ioctls.h
+++ b/linux-user/ioctls.h
@@ -574,6 +574,9 @@ 
   IOCTL_SPECIAL(SIOCDELRT, IOC_W, do_ioctl_rt,
                 MK_PTR(MK_STRUCT(STRUCT_rtentry)))
 
+  IOCTL_SPECIAL(DRM_IOCTL_VERSION, IOC_RW, do_ioctl_drm,
+                MK_PTR(MK_STRUCT(STRUCT_drm_version)))
+
 #ifdef TARGET_TIOCSTART
   IOCTL_IGNORE(TIOCSTART)
   IOCTL_IGNORE(TIOCSTOP)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 8d27d10807..1f5d48a0b0 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -112,6 +112,7 @@ 
 #include <linux/if_alg.h>
 #include <linux/rtc.h>
 #include <sound/asound.h>
+#include <libdrm/drm.h>
 #include "linux_loop.h"
 #include "uname.h"
 
@@ -5196,6 +5197,139 @@  static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_temp,
 }
 #endif
 
+static inline void free_drmversion(struct drm_version *host)
+{
+    if (!host) {
+        return;
+    }
+    if (host->name) {
+        free(host->name);
+    }
+    if (host->date) {
+        free(host->date);
+    }
+    if (host->desc) {
+        free(host->desc);
+    }
+}
+
+static inline bool t2h_drmversion_str(unsigned long *hlen,
+                                      char **h, abi_ulong *glen)
+{
+    __get_user(*hlen, glen);
+    if (*hlen) {
+        *h = malloc(*hlen + 1);
+        if (!*h) {
+            return false;
+        }
+        (*h)[*hlen] = '\0';
+    } else {
+        *h = NULL;
+    }
+    return true;
+}
+
+static inline abi_long target_to_host_drmversion(struct drm_version *host_ver,
+                                                abi_long target_addr)
+{
+    struct target_drm_version *target_ver;
+
+    if (!lock_user_struct(VERIFY_READ, target_ver, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    __get_user(host_ver->name_len, &target_ver->name_len);
+    if (!t2h_drmversion_str(&host_ver->name_len,
+                            &host_ver->name, &target_ver->name_len)) {
+        goto err;
+    }
+    if (!t2h_drmversion_str(&host_ver->date_len,
+                            &host_ver->date, &target_ver->date_len)) {
+        goto err;
+    }
+    if (!t2h_drmversion_str(&host_ver->desc_len,
+                            &host_ver->desc, &target_ver->desc_len)) {
+        goto err;
+    }
+    unlock_user_struct(target_ver, target_addr, 0);
+    return 0;
+
+err:
+    free_drmversion(host_ver);
+    return -TARGET_ENOMEM;
+}
+
+static inline bool h2t_drmversion_str(int hlen, char *h,
+                                      abi_ulong *glen, abi_ulong g)
+{
+    char *target_str;
+
+    if ((hlen > 0) && h) {
+        target_str = lock_user(VERIFY_WRITE, g, hlen, 0);
+        if (!target_str) {
+            return false;
+        }
+        memcpy(target_str, h, hlen);
+        unlock_user(target_str, g, hlen);
+    }
+    __put_user(hlen, glen);
+    return true;
+}
+
+static inline abi_long host_to_target_drmversion(abi_ulong target_addr,
+                                                 struct drm_version *host_ver)
+{
+    struct target_drm_version *target_ver;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_ver, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    __put_user(host_ver->version_major, &target_ver->version_major);
+    __put_user(host_ver->version_minor, &target_ver->version_minor);
+    __put_user(host_ver->version_patchlevel, &target_ver->version_patchlevel);
+    if (!h2t_drmversion_str(host_ver->name_len, host_ver->name,
+                            &target_ver->name_len, target_ver->name)) {
+        return -TARGET_EFAULT;
+    }
+    if (!h2t_drmversion_str(host_ver->date_len, host_ver->date,
+                            &target_ver->date_len, target_ver->date)) {
+        return -TARGET_EFAULT;
+    }
+    if (!h2t_drmversion_str(host_ver->desc_len, host_ver->desc,
+                            &target_ver->desc_len, target_ver->desc)) {
+        return -TARGET_EFAULT;
+    }
+
+    unlock_user_struct(target_ver, target_addr, 0);
+    return 0;
+}
+
+static abi_long do_ioctl_drm(const IOCTLEntry *ie, uint8_t *buf_temp,
+                             int fd, int cmd, abi_long arg)
+{
+    struct drm_version *ver;
+    abi_long ret;
+
+    switch (ie->host_cmd) {
+    case DRM_IOCTL_VERSION:
+        ver = (struct drm_version *)buf_temp;
+        memset(ver, 0, sizeof(*ver));
+        ret = target_to_host_drmversion(ver, arg);
+        if (is_error(ret)) {
+            return ret;
+        }
+        ret = get_errno(safe_ioctl(fd, ie->host_cmd, ver));
+        if (is_error(ret)) {
+            free_drmversion(ver);
+            return ret;
+        }
+        ret = host_to_target_drmversion(arg, ver);
+        free_drmversion(ver);
+        return ret;
+        break;
+    }
+    return -TARGET_EFAULT;
+}
+
 static IOCTLEntry ioctl_entries[] = {
 #define IOCTL(cmd, access, ...) \
     { TARGET_ ## cmd, cmd, #cmd, access, 0, {  __VA_ARGS__ } },
diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h
index 152ec637cb..5e455a32af 100644
--- a/linux-user/syscall_defs.h
+++ b/linux-user/syscall_defs.h
@@ -1167,6 +1167,9 @@  struct target_rtc_pll_info {
 #define TARGET_DM_TARGET_MSG          TARGET_IOWRU(0xfd, 0x0e)
 #define TARGET_DM_DEV_SET_GEOMETRY    TARGET_IOWRU(0xfd, 0x0f)
 
+/* drm ioctls */
+#define TARGET_DRM_IOCTL_VERSION      TARGET_IOWRU('d', 0x00)
+
 /* from asm/termbits.h */
 
 #define TARGET_NCC 8
@@ -2598,6 +2602,18 @@  struct target_mq_attr {
     abi_long mq_curmsgs;
 };
 
+struct target_drm_version {
+    int version_major;
+    int version_minor;
+    int version_patchlevel;
+    abi_ulong name_len;
+    abi_ulong name;
+    abi_ulong date_len;
+    abi_ulong date;
+    abi_ulong desc_len;
+    abi_ulong desc;
+};
+
 #include "socket.h"
 
 #include "errno_defs.h"
diff --git a/linux-user/syscall_types.h b/linux-user/syscall_types.h
index 4e12c1661e..52a031ad35 100644
--- a/linux-user/syscall_types.h
+++ b/linux-user/syscall_types.h
@@ -292,6 +292,18 @@  STRUCT(dm_target_versions,
 STRUCT(dm_target_msg,
        TYPE_ULONGLONG) /* sector */
 
+/* TODO: use #ifdef 32 bit #else 64 bit, next */
+STRUCT(drm_version,
+       TYPE_INT, /* version_major */
+       TYPE_INT, /* version_minor */
+       TYPE_INT, /* version_patchlevel */
+       TYPE_ULONG, /* name_len */
+       TYPE_PTRVOID, /* name */
+       TYPE_ULONG, /* date_len */
+       TYPE_PTRVOID, /* date */
+       TYPE_ULONG, /* desc_len */
+       TYPE_PTRVOID) /* desc */
+
 STRUCT(file_clone_range,
        TYPE_LONGLONG, /* src_fd */
        TYPE_ULONGLONG, /* src_offset */