Skip to contentSkip to conversation

linux-user improvements

PATCH In review Richard Henderson 2 participants 6 messages Oct 17, 2012 – Oct 19, 2012

lore
Share & tools
lore
#0 lore raw

cover linux-user improvements

The remaining alpha-linux-user patch, with updated dependencies. In addition, a new fix for guest system paths containing links.

r~

Richard Henderson (4):
cpu-all: Add unaligned load/store helper functions
linux-user: Rewrite __get_user/__put_user with __builtin_choose_expr
alpha-linux-user: Fix sigaction
user: Consider symbolic links as possible directories

 cpu-all.h                 | 38 +++++++++++++++++++++++++++
 linux-user/qemu.h         | 67 ++++++++++++++++++++++++++---------------------
 linux-user/signal.c       | 22 ++++++----------
 linux-user/syscall_defs.h |  2 +-
 path.c                    |  5 ++--
 5 files changed, 87 insertions(+), 47 deletions(-)
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/4 cpu-all: Add unaligned load/store helper functions

---
 cpu-all.h | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
cpu-all.h +38 -0
index 2b99682..2db4414 100644
@@ -113,6 +113,44 @@ static inline void tswap64s(uint64_t *s)
 #define bswaptls(s) bswap64s(s)
 #endif
 
+/* Unaligned loads and stores.  */
+
+static inline uint16_t unaligned_r16(const void *ptr)
+{
+    uint16_t ret;
+    memcpy(&ret, ptr, sizeof(ret));
+    return ret;
+}
+
+static inline uint32_t unaligned_r32(const void *ptr)
+{
+    uint32_t ret;
+    memcpy(&ret, ptr, sizeof(ret));
+    return ret;
+}
+
+static inline uint64_t unaligned_r64(const void *ptr)
+{
+    uint64_t ret;
+    memcpy(&ret, ptr, sizeof(ret));
+    return ret;
+}
+
+static inline void unaligned_w16(void *ptr, uint16_t v)
+{
+    memcpy(ptr, &v, sizeof(v));
+}
+
+static inline void unaligned_w32(void *ptr, uint32_t v)
+{
+    memcpy(ptr, &v, sizeof(v));
+}
+
+static inline void unaligned_w64(void *ptr, uint64_t v)
+{
+    memcpy(ptr, &v, sizeof(v));
+}
+
 /* CPU memory access without any memory or io remapping */
 
 /*
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] cpu-all: Add unaligned load/store helper functions

Quoted 10 lines · Richard Henderson
---
 cpu-all.h | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
cpu-all.h +0 -0
index 2b99682..2db4414 100644

cpu-all.h static inline void tswap64s(uint64_t *s) · line 118

Quoted 6 lines
cpu-all.h +3 -0
@@ -113,6 +113,44 @@ static inline void tswap64s(uint64_t *s)
 #define bswaptls(s) bswap64s(s)
 #endif
 
+/* Unaligned loads and stores.  */
+
+static inline uint16_t unaligned_r16(const void *ptr)

I think the names should match other loads and stores, so this should be something like lduw_unaligned_raw().

cpu-all.h static inline void tswap64s(uint64_t *s) · line 121

Quoted 3 lines
cpu-all.h +3 -0
+{
+    uint16_t ret;
+    memcpy(&ret, ptr, sizeof(ret));

This also assumes native host byte order which may not be so useful. Perhaps actually there should be three versions, _be, _le and without infix for target byte order?

Quoted 39 lines
cpu-all.h +32 -1
+    return ret;
+}
+
+static inline uint32_t unaligned_r32(const void *ptr)
+{
+    uint32_t ret;
+    memcpy(&ret, ptr, sizeof(ret));
+    return ret;
+}
+
+static inline uint64_t unaligned_r64(const void *ptr)
+{
+    uint64_t ret;
+    memcpy(&ret, ptr, sizeof(ret));
+    return ret;
+}
+
+static inline void unaligned_w16(void *ptr, uint16_t v)
+{
+    memcpy(ptr, &v, sizeof(v));
+}
+
+static inline void unaligned_w32(void *ptr, uint32_t v)
+{
+    memcpy(ptr, &v, sizeof(v));
+}
+
+static inline void unaligned_w64(void *ptr, uint64_t v)
+{
+    memcpy(ptr, &v, sizeof(v));
+}
+
 /* CPU memory access without any memory or io remapping */
 
 /*
--

1.7.11.7

#3 lore raw

PATCH 2/4 linux-user: Rewrite __get_user/__put_user with __builtin_choose_expr

In reply to Richard Henderson

The previous formuation with multiple assignments to __typeof(*hptr) falls down when hptr is qualified const. E.g. with const struct S *p, p->f is also qualified const.

With this formulation, there's no assignment to any local variable.

---
 linux-user/qemu.h | 67 ++++++++++++++++++++++++++++++-------------------------
 1 file changed, 37 insertions(+), 30 deletions(-)
linux-user/qemu.h +37 -30
index 5e53dca..bf0c911 100644
@@ -287,36 +287,43 @@ static inline int access_ok(int type, abi_ulong addr, abi_ulong size)
                             (type == VERIFY_READ) ? PAGE_READ : (PAGE_READ | PAGE_WRITE)) == 0;
 }
 
-/* NOTE __get_user and __put_user use host pointers and don't check access. */
-/* These are usually used to access struct data members once the
- * struct has been locked - usually with lock_user_struct().
- */
-#define __put_user(x, hptr)\
-({ __typeof(*hptr) pu_ = (x);\
-    switch(sizeof(*hptr)) {\
-    case 1: break;\
-    case 2: pu_ = tswap16(pu_); break; \
-    case 4: pu_ = tswap32(pu_); break; \
-    case 8: pu_ = tswap64(pu_); break; \
-    default: abort();\
-    }\
-    memcpy(hptr, &pu_, sizeof(pu_)); \
-    0;\
-})
-
-#define __get_user(x, hptr) \
-({ __typeof(*hptr) gu_; \
-    memcpy(&gu_, hptr, sizeof(gu_)); \
-    switch(sizeof(*hptr)) {\
-    case 1: break; \
-    case 2: gu_ = tswap16(gu_); break; \
-    case 4: gu_ = tswap32(gu_); break; \
-    case 8: gu_ = tswap64(gu_); break; \
-    default: abort();\
-    }\
-    (x) = gu_; \
-    0;\
-})
+/* NOTE __get_user and __put_user use host pointers and don't check access.
+   These are usually used to access struct data members once the struct has
+   been locked - usually with lock_user_struct.  */
+
+/* Tricky points:
+   - Use __builtin_choose_expr to avoid type promotion from ?:,
+   - Invalid sizes result in a "invalid use of void expression" error,
+     stemming from the fact that the return type of abort is void.
+   - While we eliminate byte stores with the "true" part of the first
+     choose, the "false" part of the first choose must remain valid
+     to avoid tripping over the "invalid use" error above.  Thus the
+     use of <= 2 to keep byte stores syntactically ok in the discarded
+     expression.  */
+
+#define __put_user(x, hptr)                                               \
+(__builtin_choose_expr(sizeof(*(hptr)) == 1, *(hptr) = (uint8_t)(x),      \
+   __builtin_choose_expr(sizeof(*(hptr)) <= 2, unaligned_w16,             \
+   __builtin_choose_expr(sizeof(*(hptr)) == 4, unaligned_w32,             \
+   __builtin_choose_expr(sizeof(*(hptr)) == 8, unaligned_w64, abort)))    \
+     ((hptr),                                                             \
+      __builtin_choose_expr(sizeof(*(hptr)) <= 2, tswap16((uint16_t)(x)), \
+      __builtin_choose_expr(sizeof(*(hptr)) == 4, tswap32((uint32_t)(x)), \
+      __builtin_choose_expr(sizeof(*(hptr)) == 8, tswap64((uint64_t)(x)), \
+                            abort()))))),                                 \
+ 0)
+
+#define __get_user(x, hptr)                                           \
+((x) =                                                                \
+ __builtin_choose_expr(sizeof(*(hptr)) == 1, *(hptr),                 \
+ __builtin_choose_expr(sizeof(*(hptr)) == 2,                          \
+                       (typeof(*(hptr)))tswap16(unaligned_r16(hptr)), \
+ __builtin_choose_expr(sizeof(*(hptr)) == 4,                          \
+                       (typeof(*(hptr)))tswap32(unaligned_r32(hptr)), \
+ __builtin_choose_expr(sizeof(*(hptr)) == 8,                          \
+                       (typeof(*(hptr)))tswap64(unaligned_r64(hptr)), \
+                       abort())))),                                   \
+ 0)
 
 /* put_user()/get_user() take a guest address and check access */
 /* These are usually used to access an atomic data type, such as an int,
Signature · 1 line
1.7.11.7
#4 lore raw

PATCH 3/4 alpha-linux-user: Fix sigaction

In reply to Richard Henderson

Unconditional bswap replaced by __get_user/__put_user.

---
 linux-user/signal.c       | 22 ++++++++--------------
 linux-user/syscall_defs.h |  2 +-
 2 files changed, 9 insertions(+), 15 deletions(-)
linux-user/signal.c +8 -14
index 95e2ffa..407619a 100644
@@ -607,28 +607,22 @@ int do_sigaction(int sig, const struct target_sigaction *act,
             sig, act, oact);
 #endif
     if (oact) {
-        oact->_sa_handler = tswapal(k->_sa_handler);
-#if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
-        oact->sa_flags = bswap32(k->sa_flags);
-#else
-        oact->sa_flags = tswapal(k->sa_flags);
-#endif
+        __put_user(k->_sa_handler, &oact->_sa_handler);
+        __put_user(k->sa_flags, &oact->sa_flags);
 #if !defined(TARGET_MIPS)
-        oact->sa_restorer = tswapal(k->sa_restorer);
+        __put_user(k->sa_restorer, &oact->sa_restorer);
 #endif
+        /* Not swapped.  */
         oact->sa_mask = k->sa_mask;
     }
     if (act) {
         /* FIXME: This is not threadsafe.  */
-        k->_sa_handler = tswapal(act->_sa_handler);
-#if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
-        k->sa_flags = bswap32(act->sa_flags);
-#else
-        k->sa_flags = tswapal(act->sa_flags);
-#endif
+        __get_user(k->_sa_handler, &act->_sa_handler);
+        __get_user(k->sa_flags, &act->sa_flags);
 #if !defined(TARGET_MIPS)
-        k->sa_restorer = tswapal(act->sa_restorer);
+        __get_user(k->sa_restorer, &act->sa_restorer);
 #endif
+        /* To be swapped in target_to_host_sigset.  */
         k->sa_mask = act->sa_mask;
 
         /* we update the host linux signal state */
linux-user/syscall_defs.h +1 -1
index a98cbf7..8ca70b9 100644
@@ -540,7 +540,7 @@ int do_sigaction(int sig, const struct target_sigaction *act,
 struct target_old_sigaction {
     abi_ulong _sa_handler;
     abi_ulong sa_mask;
-    abi_ulong sa_flags;
+    int32_t sa_flags;
 };
 
 struct target_rt_sigaction {
Signature · 1 line
1.7.11.7
#5 lore raw

PATCH 4/4 user: Consider symbolic links as possible directories

In reply to Richard Henderson

Commit 2296f194dfde4c0a54f249d3fdb8c8ca21dc611b reduced the number of syscalls performed during user emulation startup, but failed to consider the use of symbolic links in creating directory structures.

---
 path.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
path.c +3 -2
index ef3f277..4c5b0f6 100644
@@ -58,9 +58,10 @@ static struct pathelem *new_entry(const char *root,
 #define streq(a,b) (strcmp((a), (b)) == 0)
 
 /* Not all systems provide this feature */
-#if defined(DT_DIR) && defined(DT_UNKNOWN)
+#if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK)
 # define dirent_type(dirent) ((dirent)->d_type)
-# define is_dir_maybe(type)  ((type) == DT_DIR || (type) == DT_UNKNOWN)
+# define is_dir_maybe(type) \
+    ((type) == DT_DIR || (type) == DT_UNKNOWN || (type) == DT_LNK)
 #else
 # define dirent_type(dirent) (1)
 # define is_dir_maybe(type)  (type)
Signature · 1 line
1.7.11.7