Skip to contentSkip to conversation

linux-user: A set of miscellaneous patches

PATCH Superseded Aleksandar Markovic 5 participants 13 messages Jun 7, 2019 – Jun 19, 2019

v10 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v10 cover linux-user: A set of miscellaneous patches

From: Aleksandar Markovic <[email protected]>

This is a collection of misc patches for Linux user that I recently accumulated from variuous sources. All of them originate from problems observed on mips target. However, these changes actually affect and fix problems on multiple targets.

v9->v10:

- improved commit messages for patches 2 and 3

v8->v9:

- fixed build error on some systems related to SOL_ALG

v7->v8:

- added a patch on setsockopt() option SOL_ALG

v6->v7:

- fixed a build error for older kernels related to the patch on setsockopt() options
- removed four patches that on the meantime got accepted into the main source tree

v5->v6:

- fixed a mistake in patch #4
- improved commit messages in patches #4 and #6

v4->v5:

- added the patch on statx() support
- improved the patch on IPV6_<ADD|DROP>_MEMBERSHIP to take into account the possibility of different names for a field
- minor corrections in commit messages

v3->v4:

- improved commit messages (fixed some typos, improved relevance)

v2->v3:

- updated and improved commit messages
- added IPV6_DROP_MEMBERSHIP support to the patch on setsockopt()'s option

v1->v2:

- added the patch on setsockopt()'s option IPV6_ADD_MEMBERSHIP
- improved the commit me

Aleksandar Rikalo (1):
linux-user: Add support for statx() syscall

Neng Chen (1):
linux-user: Add support for setsockopt() options
IPV6_<ADD|DROP>_MEMBERSHIP

Yunqiang Su (1):
linux-user: Add support for setsockopt() option SOL_ALG

 linux-user/syscall.c      | 193 +++++++++++++++++++++++++++++++++++++++++++++-
 linux-user/syscall_defs.h |  37 +++++++++
 2 files changed, 229 insertions(+), 1 deletion(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v10 1/3 linux-user: Add support for setsockopt() options IPV6_<ADD|DROP>_MEMBERSHIP

From: Neng Chen <[email protected]>

Add support for options IPV6_ADD_MEMBERSHIP and IPV6_DROP_MEMPEMBERSHIP of the syscall setsockopt(). These options control membership in multicast groups. Their argument is a pointer to a struct ipv6_mreq, which is in turn defined in IP v6 header netinet/in.h as:

 struct ipv6_mreq {
     /* IPv6 multicast address of group */
     struct  in6_addr  ipv6mr_multiaddr;
     /* local IPv6 address of interface */
     int     ipv6mr_interface;

};

...whereas its definition in kernel's include/uapi/linux/in6.h is:

#if __UAPI_DEF_IPV6_MREQ

 struct ipv6_mreq {
     /* IPv6 multicast address of group */
         struct  in6_addr ipv6mr_multiaddr;
     /* local IPv6 address of interface */
     int     ipv6mr_ifindex;

};
#endif

The first field of ipv6_mreq has the same name ("ipv6mr_multiaddr") and type ("in6_addr") in both cases. Moreover, the in6_addr structure consists of fields that are always big-endian (on host of any endian), therefore the ipv6_mreq's field ipv6mr_multiaddr doesn't need any endian conversion.

The second field of ipv6_mreq may, however, depending on the build environment, have different names. This is the reason why the lines "#if __UAPI_DEF_IPV6_MREQ" and "#if defined(__UAPI_DEF_IPV6_MREQ)" are used in this patch - to establish the right choice for the field name. Also, endian conversion is needed for this field, since it is of type "int".

---
 linux-user/syscall.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
linux-user/syscall.c +27 -0
index 5e29e67..dde6889 100644
@@ -1921,6 +1921,33 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
                                        &pki, sizeof(pki)));
             break;
         }
+        case IPV6_ADD_MEMBERSHIP:
+        case IPV6_DROP_MEMBERSHIP:
+        {
+            struct ipv6_mreq ipv6mreq;
+
+            if (optlen < sizeof(ipv6mreq)) {
+                return -TARGET_EINVAL;
+            }
+
+            if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
+                return -TARGET_EFAULT;
+            }
+
+#if defined(__UAPI_DEF_IPV6_MREQ)
+#if __UAPI_DEF_IPV6_MREQ
+            ipv6mreq.ipv6mr_ifindex = tswap32(ipv6mreq.ipv6mr_ifindex);
+#else
+            ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
+#endif /* __UAPI_DEF_IVP6_MREQ */
+#else
+            ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
+#endif /* defined (__UAPI_DEF_IPV6_MREQ) */
+
+            ret = get_errno(setsockopt(sockfd, level, optname,
+                                       &ipv6mreq, sizeof(ipv6mreq)));
+            break;
+        }
         default:
             goto unimplemented;
         }
Signature · 1 line
2.7.4
#2 lore raw

Re: [Qemu-devel] [PATCH v10 1/3] linux-user: Add support for setsockopt() options IPV6_<ADD|DROP>_MEMBERSHIP

Quoted 73 lines · Aleksandar Markovic

From: Neng Chen <[email protected]>

Add support for options IPV6_ADD_MEMBERSHIP and IPV6_DROP_MEMPEMBERSHIP of the syscall setsockopt(). These options control membership in multicast groups. Their argument is a pointer to a struct ipv6_mreq, which is in turn defined in IP v6 header netinet/in.h as:

 struct ipv6_mreq {
     /* IPv6 multicast address of group */
     struct  in6_addr  ipv6mr_multiaddr;
     /* local IPv6 address of interface */
     int     ipv6mr_interface;

};

...whereas its definition in kernel's include/uapi/linux/in6.h is:

#if __UAPI_DEF_IPV6_MREQ

 struct ipv6_mreq {
     /* IPv6 multicast address of group */
         struct  in6_addr ipv6mr_multiaddr;
     /* local IPv6 address of interface */
     int     ipv6mr_ifindex;

};
#endif

The first field of ipv6_mreq has the same name ("ipv6mr_multiaddr") and type ("in6_addr") in both cases. Moreover, the in6_addr structure consists of fields that are always big-endian (on host of any endian), therefore the ipv6_mreq's field ipv6mr_multiaddr doesn't need any endian conversion.

The second field of ipv6_mreq may, however, depending on the build environment, have different names. This is the reason why the lines "#if __UAPI_DEF_IPV6_MREQ" and "#if defined(__UAPI_DEF_IPV6_MREQ)" are used in this patch - to establish the right choice for the field name. Also, endian conversion is needed for this field, since it is of type "int".

---
 linux-user/syscall.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
linux-user/syscall.c +20 -0
index 5e29e67..dde6889 100644
@@ -1921,6 +1921,33 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
                                        &pki, sizeof(pki)));
             break;
         }
+        case IPV6_ADD_MEMBERSHIP:
+        case IPV6_DROP_MEMBERSHIP:
+        {
+            struct ipv6_mreq ipv6mreq;
+
+            if (optlen < sizeof(ipv6mreq)) {
+                return -TARGET_EINVAL;
+            }
+
+            if (copy_from_user(&ipv6mreq, optval_addr, sizeof(ipv6mreq))) {
+                return -TARGET_EFAULT;
+            }
+
+#if defined(__UAPI_DEF_IPV6_MREQ)
+#if __UAPI_DEF_IPV6_MREQ
+            ipv6mreq.ipv6mr_ifindex = tswap32(ipv6mreq.ipv6mr_ifindex);
+#else
+            ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
+#endif /* __UAPI_DEF_IVP6_MREQ */
+#else

linux-user/syscall.c static abi_long do_setsockopt(int sockfd, int level, int optname, · line 1953

Quoted 10 lines
linux-user/syscall.c +7 -0
+            ipv6mreq.ipv6mr_interface = tswap32(ipv6mreq.ipv6mr_interface);
+#endif /* defined (__UAPI_DEF_IPV6_MREQ) */
+
+            ret = get_errno(setsockopt(sockfd, level, optname,
+                                       &ipv6mreq, sizeof(ipv6mreq)));
+            break;
+        }
         default:
             goto unimplemented;
         }

It becomes complicated...

I think the first version of your patch using only ipv6mr_interface is the correct one:

- POSIX defines ipv6mr_interface [1]

- __UAPI_DEF_IVP6_MREQ appears in kernel headers with v3.12

cfd280c91253 net: sync some IP headers with glibc

- without __UAPI_DEF_IVP6_MREQ kernel defines ipv6mr_ifindex

and in cfd280c91253 it is explained:

"If you include the kernel headers first you get those,
and if you include the glibc headers first you get those, and the following patch arranges a coordination and synchronization between the two."

So before 3.12, a program can't include both netinet/in.h and linux/in6.h.

In linux-user/syscall.c, we only include netinet/in.h (glibc) and not linux/in6.h (kernel-headers), so ipv6mr_interface is the one to use.

I found debian/wheezy (2013) is the most recent debian distro without the definition of __UAPI_DEF_IPV6_MREQ, but it doesn't have gcc-4.8 and thus QEMU can't be built.

Thanks,
Laurent

[1]
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/netinet/in.h.html

#3 lore raw

PATCH v10 2/3 linux-user: Add support for setsockopt() option SOL_ALG

In reply to Aleksandar Markovic

From: Yunqiang Su <[email protected]>

Add support for options SOL_ALG of the syscall setsockopt(). This option is used in relation to Linux kernel Crypto API, and allows a user to set additional information for the cipher operation via syscall setsockopt(). The field "optname" must be one of the following:

- ALG_SET_KEY – seting the key
- ALG_SET_AEAD_AUTHSIZE – set the authentication tag size

SOL_ALG is relatively newer setsockopt() option. Therefore, the code that handles SOL_ALG is enclosed in "ifdef" so that the build does not fail for older kernels that do not contain support for SOL_ALG. "ifdef" also contains check if ALG_SET_KEY and ALG_SET_AEAD_AUTHSIZE are defined.

---
 linux-user/syscall.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
linux-user/syscall.c +31 -0
index dde6889..82c08b6 100644
@@ -103,6 +103,7 @@
 #include <linux/blkpg.h>
 #include <netpacket/packet.h>
 #include <linux/netlink.h>
+#include <linux/if_alg.h>
 #include "linux_loop.h"
 #include "uname.h"
 
@@ -1998,6 +1999,36 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
             goto unimplemented;
         }
         break;
+#if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
+    case SOL_ALG:
+        switch (optname) {
+        case ALG_SET_KEY:
+        {
+            char *alg_key = g_malloc(optlen);
+
+            if (!alg_key) {
+                return -TARGET_ENOMEM;
+            }
+            if (copy_from_user(alg_key, optval_addr, optlen)) {
+                g_free(alg_key);
+                return -TARGET_EFAULT;
+            }
+            ret = get_errno(setsockopt(sockfd, level, optname,
+                                       alg_key, optlen));
+            g_free(alg_key);
+            break;
+        }
+        case ALG_SET_AEAD_AUTHSIZE:
+        {
+            ret = get_errno(setsockopt(sockfd, level, optname,
+                                       NULL, optlen));
+            break;
+        }
+        default:
+            goto unimplemented;
+        }
+        break;
+#endif
     case TARGET_SOL_SOCKET:
         switch (optname) {
         case TARGET_SO_RCVTIMEO:
Signature · 1 line
2.7.4
Reviewed-by on v10 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH v10 2/3] linux-user: Add support for setsockopt() option SOL_ALG

Le 07/06/2019 à 12:35, Aleksandar Markovic a écrit :

Quoted 63 lines

From: Yunqiang Su <[email protected]>

Add support for options SOL_ALG of the syscall setsockopt(). This option is used in relation to Linux kernel Crypto API, and allows a user to set additional information for the cipher operation via syscall setsockopt(). The field "optname" must be one of the following:

- ALG_SET_KEY – seting the key
- ALG_SET_AEAD_AUTHSIZE – set the authentication tag size

SOL_ALG is relatively newer setsockopt() option. Therefore, the code that handles SOL_ALG is enclosed in "ifdef" so that the build does not fail for older kernels that do not contain support for SOL_ALG. "ifdef" also contains check if ALG_SET_KEY and ALG_SET_AEAD_AUTHSIZE are defined.

---
 linux-user/syscall.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
linux-user/syscall.c +24 -0
index dde6889..82c08b6 100644
@@ -103,6 +103,7 @@
 #include <linux/blkpg.h>
 #include <netpacket/packet.h>
 #include <linux/netlink.h>
+#include <linux/if_alg.h>
 #include "linux_loop.h"
 #include "uname.h"
 
@@ -1998,6 +1999,36 @@ static abi_long do_setsockopt(int sockfd, int level, int optname,
             goto unimplemented;
         }
         break;
+#if defined(SOL_ALG) && defined(ALG_SET_KEY) && defined(ALG_SET_AEAD_AUTHSIZE)
+    case SOL_ALG:
+        switch (optname) {
+        case ALG_SET_KEY:
+        {
+            char *alg_key = g_malloc(optlen);
+
+            if (!alg_key) {
+                return -TARGET_ENOMEM;
+            }
+            if (copy_from_user(alg_key, optval_addr, optlen)) {
+                g_free(alg_key);
+                return -TARGET_EFAULT;
+            }
+            ret = get_errno(setsockopt(sockfd, level, optname,
+                                       alg_key, optlen));
+            g_free(alg_key);
+            break;
+        }
+        case ALG_SET_AEAD_AUTHSIZE:
+        {
+            ret = get_errno(setsockopt(sockfd, level, optname,
+                                       NULL, optlen));

linux-user/syscall.c static abi_long do_setsockopt(int sockfd, int level, int optname, · line 2034

Quoted 10 lines
linux-user/syscall.c +7 -0
+            break;
+        }
+        default:
+            goto unimplemented;
+        }
+        break;
+#endif
     case TARGET_SOL_SOCKET:
         switch (optname) {
         case TARGET_SO_RCVTIMEO:
#5 lore raw

PATCH v10 3/3 linux-user: Add support for statx() syscall

In reply to Aleksandar Markovic

From: Aleksandar Rikalo <[email protected]>

Implement support for translation of system call statx().

The implementation is based on "best effort" approach: if host is capable of executing statx(), host statx() is used. If not, the implementation includes invoking other (more mature) system calls (from the same 'stat' family) on the host side to achieve as close as possible functionality.

Support for statx() in kernel and glibc was, however, introduced at different points of time (the difference is more than a year):

- kernel: Linux 4.11 (30 April 2017)
- glibc: glibc 2.28 (1 Aug 2018)

In this patch, the availability of statx() support is established via __NR_statx (if it is defined, statx() is considered available). This coincedes with statx() introduction in kernel.

However, the structure statx definition may not be available for hosts with glibc older than 2.28 (it is, by design, to be defined in one of glibc headers), even though the full statx() functionality may be supported in kernel, if the kernel is not older than 4.11. Hence, a structure "target_statx" is defined in this patch, to remove that dependency on glibc headers, and to use statx() functionality as soon as the host kernel is capable of supporting it. Such structure statx definition is used for both target and host structures statx (of course, this doesn't mean the endian arrangement is the same on target and host, and endian conversion is done in all necessary cases).

---
 linux-user/syscall.c      | 135 +++++++++++++++++++++++++++++++++++++++++++++-
 linux-user/syscall_defs.h |  37 +++++++++++++
 2 files changed, 171 insertions(+), 1 deletion(-)
linux-user/syscall.c +134 -1
index 82c08b6..b78ed45 100644
@@ -43,6 +43,7 @@
 #include <sys/times.h>
 #include <sys/shm.h>
 #include <sys/sem.h>
+#include <sys/stat.h>
 #include <sys/statfs.h>
 #include <utime.h>
 #include <sys/sysinfo.h>
@@ -6526,6 +6527,48 @@ static inline abi_long host_to_target_stat64(void *cpu_env,
 }
 #endif
 
+#if defined(TARGET_NR_statx) && defined(__NR_statx)
+static inline abi_long host_to_target_statx(struct target_statx *host_stx,
+                                            abi_ulong target_addr)
+{
+    struct target_statx *target_stx;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_stx, target_addr,  0)) {
+        return -TARGET_EFAULT;
+    }
+    memset(target_stx, 0, sizeof(*target_stx));
+
+    __put_user(host_stx->stx_mask, &target_stx->stx_mask);
+    __put_user(host_stx->stx_blksize, &target_stx->stx_blksize);
+    __put_user(host_stx->stx_attributes, &target_stx->stx_attributes);
+    __put_user(host_stx->stx_nlink, &target_stx->stx_nlink);
+    __put_user(host_stx->stx_uid, &target_stx->stx_uid);
+    __put_user(host_stx->stx_gid, &target_stx->stx_gid);
+    __put_user(host_stx->stx_mode, &target_stx->stx_mode);
+    __put_user(host_stx->stx_ino, &target_stx->stx_ino);
+    __put_user(host_stx->stx_size, &target_stx->stx_size);
+    __put_user(host_stx->stx_blocks, &target_stx->stx_blocks);
+    __put_user(host_stx->stx_attributes_mask, &target_stx->stx_attributes_mask);
+    __put_user(host_stx->stx_atime.tv_sec, &target_stx->stx_atime.tv_sec);
+    __put_user(host_stx->stx_atime.tv_nsec, &target_stx->stx_atime.tv_nsec);
+    __put_user(host_stx->stx_btime.tv_sec, &target_stx->stx_atime.tv_sec);
+    __put_user(host_stx->stx_btime.tv_nsec, &target_stx->stx_atime.tv_nsec);
+    __put_user(host_stx->stx_ctime.tv_sec, &target_stx->stx_atime.tv_sec);
+    __put_user(host_stx->stx_ctime.tv_nsec, &target_stx->stx_atime.tv_nsec);
+    __put_user(host_stx->stx_mtime.tv_sec, &target_stx->stx_atime.tv_sec);
+    __put_user(host_stx->stx_mtime.tv_nsec, &target_stx->stx_atime.tv_nsec);
+    __put_user(host_stx->stx_rdev_major, &target_stx->stx_rdev_major);
+    __put_user(host_stx->stx_rdev_minor, &target_stx->stx_rdev_minor);
+    __put_user(host_stx->stx_dev_major, &target_stx->stx_dev_major);
+    __put_user(host_stx->stx_dev_minor, &target_stx->stx_dev_minor);
+
+    unlock_user_struct(target_stx, target_addr, 1);
+
+    return 0;
+}
+#endif
+
+
 /* ??? Using host futex calls even when target atomic operations
    are not really atomic probably breaks things.  However implementing
    futexes locally would make futexes shared between multiple processes
@@ -7104,7 +7147,8 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
     abi_long ret;
 #if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) \
     || defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64) \
-    || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64)
+    || defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64) \
+    || defined(TARGET_NR_statx)
     struct stat st;
 #endif
 #if defined(TARGET_NR_statfs) || defined(TARGET_NR_statfs64) \
@@ -10182,6 +10226,95 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
             ret = host_to_target_stat64(cpu_env, arg3, &st);
         return ret;
 #endif
+#if defined(TARGET_NR_statx)
+    case TARGET_NR_statx:
+        {
+            struct target_statx *target_stx;
+            int dirfd = arg1;
+            int flags = arg3;
+
+            p = lock_user_string(arg2);
+            if (p == NULL) {
+                return -TARGET_EFAULT;
+            }
+#if defined(__NR_statx)
+            {
+                /*
+                 * It is assumed that struct statx is arhitecture independent
+                 */
+                struct target_statx host_stx;
+                int mask = arg4;
+
+                ret = get_errno(syscall(__NR_statx, dirfd, p, flags, mask,
+                                        &host_stx));
+                if (!is_error(ret)) {
+                    if (host_to_target_statx(&host_stx, arg5) != 0) {
+                        unlock_user(p, arg2, 0);
+                        return -TARGET_EFAULT;
+                    }
+                }
+
+                if (ret != TARGET_ENOSYS) {
+                    unlock_user(p, arg2, 0);
+                    return ret;
+                }
+            }
+#endif
+            if ((p == NULL) || (*((char *)p) == 0)) {
+                /*
+                 * By file descriptor
+                 */
+                if (flags & AT_EMPTY_PATH) {
+                    unlock_user(p, arg2, 0);
+                    return -TARGET_ENOENT;
+                }
+                ret = get_errno(fstat(dirfd, &st));
+            } else if (*((char *)p) == '/') {
+                /*
+                 * By absolute pathname
+                 */
+                ret = get_errno(stat(path(p), &st));
+            } else {
+                if (dirfd == AT_FDCWD) {
+                    /*
+                     * By pathname relative to the current working directory
+                     */
+                    ret = get_errno(stat(path(p), &st));
+                } else {
+                    /*
+                     * By pathname relative to the directory referred to by
+                     * the file descriptor 'dirfd'
+                     */
+                    ret = get_errno(fstatat(dirfd, path(p), &st, flags));
+                }
+            }
+            unlock_user(p, arg2, 0);
+
+            if (!is_error(ret)) {
+                if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
+                    return -TARGET_EFAULT;
+                }
+                memset(target_stx, 0, sizeof(*target_stx));
+                __put_user(major(st.st_dev), &target_stx->stx_dev_major);
+                __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
+                __put_user(st.st_ino, &target_stx->stx_ino);
+                __put_user(st.st_mode, &target_stx->stx_mode);
+                __put_user(st.st_uid, &target_stx->stx_uid);
+                __put_user(st.st_gid, &target_stx->stx_gid);
+                __put_user(st.st_nlink, &target_stx->stx_nlink);
+                __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
+                __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
+                __put_user(st.st_size, &target_stx->stx_size);
+                __put_user(st.st_blksize, &target_stx->stx_blksize);
+                __put_user(st.st_blocks, &target_stx->stx_blocks);
+                __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
+                __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
+                __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
+                unlock_user_struct(target_stx, arg5, 1);
+            }
+        }
+        return ret;
+#endif
 #ifdef TARGET_NR_lchown
     case TARGET_NR_lchown:
         if (!(p = lock_user_string(arg1)))
linux-user/syscall_defs.h +37 -0
index 7f141f6..170c4dd 100644
@@ -2536,4 +2536,41 @@ struct target_user_cap_data {
 /* Return size of the log buffer */
 #define TARGET_SYSLOG_ACTION_SIZE_BUFFER   10
 
+struct target_statx_timestamp {
+   int64_t tv_sec;
+   uint32_t tv_nsec;
+   int32_t __reserved;
+};
+
+struct target_statx {
+   /* 0x00 */
+   uint32_t stx_mask;       /* What results were written [uncond] */
+   uint32_t stx_blksize;    /* Preferred general I/O size [uncond] */
+   uint64_t stx_attributes; /* Flags conveying information about the file */
+   /* 0x10 */
+   uint32_t stx_nlink;      /* Number of hard links */
+   uint32_t stx_uid;        /* User ID of owner */
+   uint32_t stx_gid;        /* Group ID of owner */
+   uint16_t stx_mode;       /* File mode */
+   uint16_t __spare0[1];
+   /* 0x20 */
+   uint64_t stx_ino;        /* Inode number */
+   uint64_t stx_size;       /* File size */
+   uint64_t stx_blocks;     /* Number of 512-byte blocks allocated */
+   uint64_t stx_attributes_mask; /* Mask to show what is supported */
+   /* 0x40 */
+   struct target_statx_timestamp  stx_atime;  /* Last access time */
+   struct target_statx_timestamp  stx_btime;  /* File creation time */
+   struct target_statx_timestamp  stx_ctime;  /* Last attribute change time */
+   struct target_statx_timestamp  stx_mtime;  /* Last data modification time */
+   /* 0x80 */
+   uint32_t stx_rdev_major;   /* Device ID of special file [if bdev/cdev] */
+   uint32_t stx_rdev_minor;
+   uint32_t stx_dev_major; /* ID of device containing file [uncond] */
+   uint32_t stx_dev_minor;
+   /* 0x90 */
+   uint64_t __spare2[14];  /* Spare space for future expansion */
+   /* 0x100 */
+};
+
 #endif
Signature · 1 line
2.7.4
#6 lore raw

Re: [Qemu-devel] [PATCH v10 3/3] linux-user: Add support for statx() syscall

Le 07/06/2019 à 12:35, Aleksandar Markovic a écrit :

Quoted 43 lines

From: Aleksandar Rikalo <[email protected]>

Implement support for translation of system call statx().

The implementation is based on "best effort" approach: if host is capable of executing statx(), host statx() is used. If not, the implementation includes invoking other (more mature) system calls (from the same 'stat' family) on the host side to achieve as close as possible functionality.

Support for statx() in kernel and glibc was, however, introduced at different points of time (the difference is more than a year):

- kernel: Linux 4.11 (30 April 2017)
- glibc: glibc 2.28 (1 Aug 2018)

In this patch, the availability of statx() support is established via __NR_statx (if it is defined, statx() is considered available). This coincedes with statx() introduction in kernel.

However, the structure statx definition may not be available for hosts with glibc older than 2.28 (it is, by design, to be defined in one of glibc headers), even though the full statx() functionality may be supported in kernel, if the kernel is not older than 4.11. Hence, a structure "target_statx" is defined in this patch, to remove that dependency on glibc headers, and to use statx() functionality as soon as the host kernel is capable of supporting it. Such structure statx definition is used for both target and host structures statx (of course, this doesn't mean the endian arrangement is the same on target and host, and endian conversion is done in all necessary cases).

---
 linux-user/syscall.c      | 135 +++++++++++++++++++++++++++++++++++++++++++++-
 linux-user/syscall_defs.h |  37 +++++++++++++
 2 files changed, 171 insertions(+), 1 deletion(-)
linux-user/syscall.c +0 -0
index 82c08b6..b78ed45 100644

...

Quoted 9 lines
linux-user/syscall.c +5 -0
@@ -10182,6 +10226,95 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
             ret = host_to_target_stat64(cpu_env, arg3, &st);
         return ret;
 #endif
+#if defined(TARGET_NR_statx)
+    case TARGET_NR_statx:
+        {
+            struct target_statx *target_stx;
+            int dirfd = arg1;

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10243

Quoted 10 lines
linux-user/syscall.c +10 -0
+            int flags = arg3;
+
+            p = lock_user_string(arg2);
+            if (p == NULL) {
+                return -TARGET_EFAULT;
+            }
+#if defined(__NR_statx)
+            {
+                /*
+                 * It is assumed that struct statx is arhitecture independent

s/arhitecture/architecture/

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10249

Quoted 6 lines
linux-user/syscall.c +6 -0
+                 */
+                struct target_statx host_stx;
+                int mask = arg4;
+
+                ret = get_errno(syscall(__NR_statx, dirfd, p, flags, mask,
+                                        &host_stx));

You should define sys_statx() using _syscall5() macro and use it.

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10257

Quoted 8 lines
linux-user/syscall.c +8 -0
+                if (!is_error(ret)) {
+                    if (host_to_target_statx(&host_stx, arg5) != 0) {
+                        unlock_user(p, arg2, 0);
+                        return -TARGET_EFAULT;
+                    }
+                }
+
+                if (ret != TARGET_ENOSYS) {

ret != -TARGET_ENOSYS

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10263

Quoted 6 lines
linux-user/syscall.c +6 -0
+                    unlock_user(p, arg2, 0);
+                    return ret;
+                }
+            }
+#endif
+            if ((p == NULL) || (*((char *)p) == 0)) {

You already checked above p is not NULL and exited with -TARGET_EFAULT.

BTW, do we really need to emulate the syscall if it is not available?

I think the user-space application calling statx() should be ready to receive ENOSYS and define some kinds of fallback (like you do below). So it should not be done by QEMU.

Quoted 9 lines
linux-user/syscall.c +9 -0
+                /*
+                 * By file descriptor
+                 */
+                if (flags & AT_EMPTY_PATH) {
+                    unlock_user(p, arg2, 0);
+                    return -TARGET_ENOENT;
+                }
+                ret = get_errno(fstat(dirfd, &st));
+            } else if (*((char *)p) == '/') {

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10282

Quoted 10 lines
linux-user/syscall.c +10 -0
+                /*
+                 * By absolute pathname
+                 */
+                ret = get_errno(stat(path(p), &st));
+            } else {
+                if (dirfd == AT_FDCWD) {
+                    /*
+                     * By pathname relative to the current working directory
+                     */
+                    ret = get_errno(stat(path(p), &st));

Why do we divide the case in two parts, fstatat() should work here too.

Quoted 25 lines
linux-user/syscall.c +25 -0
+                } else {
+                    /*
+                     * By pathname relative to the directory referred to by
+                     * the file descriptor 'dirfd'
+                     */
+                    ret = get_errno(fstatat(dirfd, path(p), &st, flags));
+                }
+            }
+            unlock_user(p, arg2, 0);
+
+            if (!is_error(ret)) {
+                if (!lock_user_struct(VERIFY_WRITE, target_stx, arg5, 0)) {
+                    return -TARGET_EFAULT;
+                }
+                memset(target_stx, 0, sizeof(*target_stx));
+                __put_user(major(st.st_dev), &target_stx->stx_dev_major);
+                __put_user(minor(st.st_dev), &target_stx->stx_dev_minor);
+                __put_user(st.st_ino, &target_stx->stx_ino);
+                __put_user(st.st_mode, &target_stx->stx_mode);
+                __put_user(st.st_uid, &target_stx->stx_uid);
+                __put_user(st.st_gid, &target_stx->stx_gid);
+                __put_user(st.st_nlink, &target_stx->stx_nlink);
+                __put_user(major(st.st_rdev), &target_stx->stx_rdev_major);
+                __put_user(minor(st.st_rdev), &target_stx->stx_rdev_minor);
+                __put_user(st.st_size, &target_stx->stx_size);

linux-user/syscall.c static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, · line 10317

Quoted 10 lines
linux-user/syscall.c +10 -0
+                __put_user(st.st_blksize, &target_stx->stx_blksize);
+                __put_user(st.st_blocks, &target_stx->stx_blocks);
+                __put_user(st.st_atime, &target_stx->stx_atime.tv_sec);
+                __put_user(st.st_mtime, &target_stx->stx_mtime.tv_sec);
+                __put_user(st.st_ctime, &target_stx->stx_ctime.tv_sec);
+                unlock_user_struct(target_stx, arg5, 1);
+            }
+        }
+        return ret;
+#endif

Thanks,
Laurent

#7 lore raw

Re: [Qemu-devel] [PATCH v10 3/3] linux-user: Add support for statx() syscall

Hi Laurent,

Quoted 1 line

s/arhitecture/architecture/

Done.

Quoted 1 line

You should define sys_statx() using _syscall5() macro and use it.

Done.

Quoted 1 line

ret != -TARGET_ENOSYS

Done.

Quoted 1 line

You already checked above p is not NULL and exited with -TARGET_EFAULT.

Done.

Quoted 5 lines

BTW, do we really need to emulate the syscall if it is not available?

I think the user-space application calling statx() should be ready to receive ENOSYS and define some kinds of fallback (like you do below). So it should not be done by QEMU.

nanoMIPS linux port doesn't support any of "stats" but the statx, so there is no fallback in nanoMIPS user-space applications.

I think, we can expect similar situation for any new linux port.

Quoted 1 line

Why do we divide the case in two parts, fstatat() should work here too.

fstat() uses file descriptor, but here we have string which represents file name with absolute path.

All system calls from 'stat' group whose name starts with letter f require file descriptor as an argument. Whereas remaining system calls require file name / path as string. In that sense, statx() is a hybrid between the two, hence the solution I propose.

Aleksandar Rikalo

#8 lore raw

Re: [Qemu-devel] [PATCH v10 3/3] linux-user: Add support for statx() syscall

Le 19/06/2019 à 14:12, Aleksandar Rikalo a écrit :

Quoted 1 line

Hi Laurent,

...

Quoted 10 lines
Quoted 5 lines

BTW, do we really need to emulate the syscall if it is not available?

I think the user-space application calling statx() should be ready to receive ENOSYS and define some kinds of fallback (like you do below). So it should not be done by QEMU.

nanoMIPS linux port doesn't support any of "stats" but the statx, so there is no fallback in nanoMIPS user-space applications.

I think, we can expect similar situation for any new linux port.

OK, I understand, so I agree, we need the emulation part.

Quoted 9 lines
Quoted 1 line

Why do we divide the case in two parts, fstatat() should work here too.

fstat() uses file descriptor, but here we have string which represents file name with absolute path.

All system calls from 'stat' group whose name starts with letter f require file descriptor as an argument. Whereas remaining system calls require file name / path as string. In that sense, statx() is a hybrid between the two, hence the solution I propose.

but fstatat() works like statx(), it accepts file descriptor and path.

So what I proposed is to replace:

+ } else {
+ if (dirfd == AT_FDCWD) {
+ /*
+ * By pathname relative to the current working directory
+ */
+ ret = get_errno(stat(path(p), &st));
+ unlock_user(p, arg2, 0);
+ } else {
+ /*

+                     * By pathname relative to the directory referred to by
+                     * the file descriptor 'dirfd'
+                     */

+ ret = get_errno(fstatat(dirfd, path(p), &st, flags));
+ unlock_user(p, arg2, 0);
+ }
+ }

by something like;

+ } else {
+ ret = get_errno(fstatat(dirfd, path(p), &st, flags));
+ }

as fstatat() also accepts AT_FDCWD.

Moreover in kernel vfs_fstatat() calls vfs_statx():

static inline int vfs_fstatat(int dfd, const char __user *filename,
struct kstat *stat, int flags)
{
return vfs_statx(dfd, filename, flags | AT_NO_AUTOMOUNT,
stat, STATX_BASIC_STATS);
}

so maybe all the cases can be emulated by fstatat()?

Or did I miss something?

Thanks,
Laurent

#9 lore raw

Re: [Qemu-devel] [PATCH v10 3/3] linux-user: Add support for statx() syscall

In reply to Aleksandar Markovic
Quoted 1 line · Aleksandar Markovic

Implement support for translation of system call statx().

I also need these patches for 32-bit RISC-V linux user mode support.

glibc ld.so calls statx if fstatat is not supported. Apparently new linux architecture ports aren't allowed to define __ARCH_WANT_NEW_STAT which enables fstatat because this is already obsolete. 64-bit RISC-V linux does have fstatat, but apparently this was a mistake which we can't fix now because the ABI is already frozen. The 32-bit RISC-V ABI is not frozen yet, so it won't have fstatat. Anyways, without statx, ld.so doesn't work, which makes user mode qemu pretty useless, so we do need this emulated in qemu to make the 32-bit RISC-V linux user mode support work properly.

I started with the August 2018 version of the patch a few weeks ago, and just noticed that it has been resubmitted. I had to modify the patch slightly to apply to current sources, and had to fix one bug to make it work. The line
+ if (ret != TARGET_ENOSYS) {
needs to instead be
+ if (ret != -TARGET_ENOSYS) {
I see that Laurent has already pointed that out.

Incidentally, I also have strace patches for statx that work on top of this patch, since I didn't see that in the nanomips patch set I started with. That helped me debug the 32-bit RISC-V user mode support.

I've tested this on Ubuntu 16.04 (no host statx) and Ubuntu 19.10 (with host statx) and it worked well for me running the gcc testsuite for a riscv32-linux target. I haven't tried testing the latest version of the patch yet. I can do that if this is helpful.

Jim

#10 lore raw

Re: [Qemu-devel] [PATCH v10 3/3] linux-user: Add support for statx() syscall

Quoted 7 lines · Jim Wilson
Quoted 2 lines · Aleksandar Markovic

Implement support for translation of system call statx().

I also need these patches for 32-bit RISC-V linux user mode support.

Quoted 10 lines

glibc ld.so calls statx if fstatat is not supported. Apparently new linux architecture ports aren't allowed to define __ARCH_WANT_NEW_STAT which enables fstatat because this is already obsolete. 64-bit RISC-V linux does have fstatat, but apparently this was a mistake which we can't fix now because the ABI is already frozen. The 32-bit RISC-V ABI is not frozen yet, so it won't have fstatat. Anyways, without statx, ld.so doesn't work, which makes user mode qemu pretty useless, so we do need this emulated in qemu to make the 32-bit RISC-V linux user mode support work properly.

Jim, Aleksandar Rikalo, the author of the patch was about to send Laurent explanation why this aproach is needed in its current organization, on a very similar line of reasoning as yours.

I am waiting on him to send a new version of the series. Meanwhile you can send strace patch to the list, and I can even incude it in my series after and together with Aleksandar's patch, if you don't object.

Yours,

Aleksandar M.

I started with the August 2018 version of the patch a few weeks ago, and

Quoted 20 lines

just noticed that it has been resubmitted. I had to modify the patch slightly to apply to current sources, and had to fix one bug to make it work. The line
+ if (ret != TARGET_ENOSYS) {
needs to instead be
+ if (ret != -TARGET_ENOSYS) {
I see that Laurent has already pointed that out.

Incidentally, I also have strace patches for statx that work on top of this patch, since I didn't see that in the nanomips patch set I started with. That helped me debug the 32-bit RISC-V user mode support.

I've tested this on Ubuntu 16.04 (no host statx) and Ubuntu 19.10 (with host statx) and it worked well for me running the gcc testsuite for a riscv32-linux target. I haven't tried testing the latest version of the patch yet. I can do that if this is helpful.

Jim

#11 lore raw

Re: [Qemu-devel] [PATCH v10 3/3] linux-user: Add support for statx() syscall

Quoted 1 line · Aleksandar Markovic

I am waiting on him to send a new version of the series. Meanwhile you can send strace patch to the list, and I can even incude it in my series after and together with Aleksandar's patch, if you don't object.

I submitted it the usual way, so it is on the mailing list now. If you want to include it with your patch series that is fine.
https://lists.nongnu.org/archive/html/qemu-devel/2019-06/msg04087.html

Jim

#12 lore raw
In reply to Aleksandar Markovic

Ping

Quoted 73 lines · Aleksandar Markovic

From: Aleksandar Markovic <[email protected]>

This is a collection of misc patches for Linux user that I recently accumulated from variuous sources. All of them originate from problems observed on mips target. However, these changes actually affect and fix problems on multiple targets.

v9->v10:

- improved commit messages for patches 2 and 3

v8->v9:

- fixed build error on some systems related to SOL_ALG

v7->v8:

- added a patch on setsockopt() option SOL_ALG

v6->v7:

- fixed a build error for older kernels related to the patch on setsockopt() options
- removed four patches that on the meantime got accepted into the main source tree

v5->v6:

- fixed a mistake in patch #4
- improved commit messages in patches #4 and #6

v4->v5:

- added the patch on statx() support
- improved the patch on IPV6_<ADD|DROP>_MEMBERSHIP to take into account the possibility of different names for a field
- minor corrections in commit messages

v3->v4:

- improved commit messages (fixed some typos, improved relevance)

v2->v3:

- updated and improved commit messages
- added IPV6_DROP_MEMBERSHIP support to the patch on setsockopt()'s option

v1->v2:

- added the patch on setsockopt()'s option IPV6_ADD_MEMBERSHIP
- improved the commit me

Aleksandar Rikalo (1):
linux-user: Add support for statx() syscall

Neng Chen (1):
linux-user: Add support for setsockopt() options
IPV6_<ADD|DROP>_MEMBERSHIP

Yunqiang Su (1):
linux-user: Add support for setsockopt() option SOL_ALG

 linux-user/syscall.c      | 193 ++++++++++++++++++++++++++++++
+++++++++++++++-
 linux-user/syscall_defs.h |  37 +++++++++
 2 files changed, 229 insertions(+), 1 deletion(-)

--
2.7.4