Skip to contentSkip to conversation

Implement the stat system calls for FreeBSD.

PATCH Superseded Karim Taha 3 participants 70 messages Aug 8, 2023 – Aug 9, 2023 Arm/Amlogic Meson SoC support

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Karim Taha

author
#0 lore raw

cover Implement the stat system calls for FreeBSD.

Upstream the implementation of the stat system calls from the qemu-bsd-user fork, the following system calls are implemented:
stat(2), lstat(2), fstat(2), fstatat(2), nstat, nfstat, nlstat getfh(2), lgetfh(2), fhopen(2), fhstat(2), fhstatfs(2), statfs(2) fstatfs(2), getfsstat(2), getdents(2), getdirentries(2), fcntl(2)

Karim Taha (1):
Add os-stat.c to the build

Kyle Evans (2):
Disable clang warnings arising from bsd-user/qemu.h Define safe_fcntl macro in bsd-user/syscall_defs.h

Michal Meloun (9):
Forward declare functions defined in os-stat.c Add struct target_stat to bsd-user/syscall_defs.h Add struct target_statfs to bsd-user/syscall_defs.h Implement h2t_freebsd_stat and h2t_freebsd_statfs functions Implement freebsd11 stat related syscalls Implement freebsd11 stat related syscalls Implement freebsd11 stat related syscalls Implement freebsd11 stat related syscalls Implement freebsd11 stat related syscalls

Mikaël Urankar (1):
Implement do_freebsd_realpathat syscall

Stacey Son (13):
Declarations of h2t and t2h conversion functions.
Add struct target_freebsd11_stat to bsd-user/syscall_defs Add structs target_freebsd11_nstat and target_freebsd11_statfs to
bsd-user/syscall_defs.h
Add struct target_freebsd_fhandle and fcntl flags to
bsd-user/syscall_defs.h
Implement host-target convertion functions
Implement host-target convertion functions
Implement host-target convertion functions
Implement host-target convertion functions
Implement stat related syscalls
Implement stat related syscalls
Implement stat related syscalls
Implement stat related syscalls
Implement stat related syscalls

Warner Losh (7):
Move _WANT_FREEBSD macros to include/qemu/osdep.h Update the definitions of __put_user and __get_user macros Rename target_freebsd_time_t to target_time_t
Add glue to call the following syscalls to the freebsd_syscall
function:
Add glue to call the following syscalls to the freebsd_syscall
function:
Add glue to call the following syscalls to the freebsd_syscall
function:
Add glue to call the following syscalls to the freebsd_syscall
function:

 bsd-user/freebsd/meson.build  |   1 +
 bsd-user/freebsd/os-stat.c    | 325 +++++++++++++++++
 bsd-user/freebsd/os-stat.h    | 663 ++++++++++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c | 121 ++++++-
 bsd-user/freebsd/qemu-os.h    |  50 +++
 bsd-user/qemu.h               | 113 +++---
 bsd-user/signal.c             |   5 +-
 bsd-user/syscall_defs.h       | 222 +++++++++++-
 include/qemu/osdep.h          |  13 +
 9 files changed, 1447 insertions(+), 66 deletions(-)
 create mode 100644 bsd-user/freebsd/os-stat.c
 create mode 100644 bsd-user/freebsd/os-stat.h
 create mode 100644 bsd-user/freebsd/qemu-os.h
Signature · 1 line
2.40.0
author
#1 lore raw

PATCH 1/33 Move _WANT_FREEBSD macros to include/qemu/osdep.h

From: Warner Losh <[email protected]>

move _WANT_FREEBSD macros from bsd-user/freebsd/os-syscall.c to include/qemu/osdep.h in order to pull some struct defintions needed later in the build.

---
 bsd-user/freebsd/os-syscall.c | 11 -----------
 include/qemu/osdep.h          | 13 +++++++++++++
 2 files changed, 13 insertions(+), 11 deletions(-)
bsd-user/freebsd/os-syscall.c +0 -11
index de36c4b71c..2224a280ea 100644
@@ -17,17 +17,6 @@
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
-
-/*
- * We need the FreeBSD "legacy" definitions. Rust needs the FreeBSD 11 system
- * calls since it doesn't use libc at all, so we have to emulate that despite
- * FreeBSD 11 being EOL'd.
- */
-#define _WANT_FREEBSD11_STAT
-#define _WANT_FREEBSD11_STATFS
-#define _WANT_FREEBSD11_DIRENT
-#define _WANT_KERNEL_ERRNO
-#define _WANT_SEMUN
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "qemu/path.h"
include/qemu/osdep.h +13 -0
index cc61b00ba9..1ca94ad3de 100644
@@ -88,6 +88,19 @@ QEMU_EXTERN_C int daemon(int, int);
 #define __USE_MINGW_ANSI_STDIO 1
 #endif
 
+/*
+ * We need the FreeBSD "legacy" definitions. Rust needs the FreeBSD 11 system
+ * calls since it doesn't use libc at all, so we have to emulate that despite
+ * FreeBSD 11 being EOL'd.
+ */
+#ifdef __FreeBSD__
+#define _WANT_FREEBSD11_STAT
+#define _WANT_FREEBSD11_STATFS
+#define _WANT_FREEBSD11_DIRENT
+#define _WANT_KERNEL_ERRNO
+#define _WANT_SEMUN
+#endif
+
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdbool.h>
Signature · 1 line
2.40.0
Acked-by on v1 1/33
#2 lore raw

Re: [PATCH 01/33] Move _WANT_FREEBSD macros to include/qemu/osdep.h

Quoted 6 lines · Karim Taha

From: Warner Losh<[email protected]>

move _WANT_FREEBSD macros from bsd-user/freebsd/os-syscall.c to include/qemu/osdep.h in order to pull some struct defintions needed later in the build.

Quoted 6 lines
---
  bsd-user/freebsd/os-syscall.c | 11 -----------
  include/qemu/osdep.h          | 13 +++++++++++++
  2 files changed, 13 insertions(+), 11 deletions(-)

r~

author
#3 lore raw

PATCH 2/33 Disable clang warnings arising from bsd-user/qemu.h

In reply to Karim Taha

From: Kyle Evans <[email protected]>

Implement PRAGMA_DISABLE_PACKED_WARNING and PRAGMA_REENABLE_PACKED_WARNING macros in bsd-user/qemu.h.

---
 bsd-user/qemu.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
bsd-user/qemu.h +31 -0
index edf9602f9b..dfdfa8dd67 100644
@@ -276,6 +276,37 @@ static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
  * 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 compile time error stemming from
+ *   the fact that abort has no parameters.
+ * - It's easier to use the endian-specific unaligned load/store
+ *   functions than host-endian unaligned load/store plus tswapN.
+ * - The pragmas are necessary only to silence a clang false-positive
+ *   warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
+ * - We have to disable -Wpragmas warnings to avoid a complaint about
+ *   an unknown warning type from older compilers that don't know about
+ *   -Waddress-of-packed-member.
+ * - gcc has bugs in its _Pragma() support in some versions, eg
+ *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
+ *   include the warning-suppression pragmas for clang
+ */
+#ifdef __clang__
+#define PRAGMA_DISABLE_PACKED_WARNING                                   \
+    _Pragma("GCC diagnostic push");                                     \
+    _Pragma("GCC diagnostic ignored \"-Wpragmas\"");                    \
+    _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
+
+#define PRAGMA_REENABLE_PACKED_WARNING          \
+    _Pragma("GCC diagnostic pop")
+
+#else
+#define PRAGMA_DISABLE_PACKED_WARNING
+#define PRAGMA_REENABLE_PACKED_WARNING
+#endif
+
 #define __put_user(x, hptr)\
 ({\
     int size = sizeof(*hptr);\
Signature · 1 line
2.40.0
Reviewed-by on v1 2/33
#4 lore raw

Re: [PATCH 02/33] Disable clang warnings arising from bsd-user/qemu.h

Quoted 7 lines · Karim Taha

+/*
+ * Tricky points:
+ * - Use __builtin_choose_expr to avoid type promotion from ?:,
+ * - Invalid sizes result in a compile time error stemming from
+ * the fact that abort has no parameters.
+ * - It's easier to use the endian-specific unaligned load/store
+ * functions than host-endian unaligned load/store plus tswapN.

Quoted 8 lines

+ * - The pragmas are necessary only to silence a clang false-positive
+ * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
+ * - We have to disable -Wpragmas warnings to avoid a complaint about
+ * an unknown warning type from older compilers that don't know about
+ * -Waddress-of-packed-member.
+ * - gcc has bugs in its _Pragma() support in some versions, eg
+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
+ * include the warning-suppression pragmas for clang

Perhaps s/in some versions/prior to gcc-13/ ? At least that's what the bugzilla says, and it will help when auditing for compiler versions in a few years when gcc-12 is EOL.

Either way,

r~

#5 lore raw

Re: [PATCH 02/33] Disable clang warnings arising from bsd-user/qemu.h

Quoted 17 lines · Richard Henderson
Quoted 7 lines · Karim Taha

+/*
+ * Tricky points:
+ * - Use __builtin_choose_expr to avoid type promotion from ?:,
+ * - Invalid sizes result in a compile time error stemming from
+ * the fact that abort has no parameters.
+ * - It's easier to use the endian-specific unaligned load/store
+ * functions than host-endian unaligned load/store plus tswapN.

Quoted 8 lines

+ * - The pragmas are necessary only to silence a clang false-positive
+ * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
+ * - We have to disable -Wpragmas warnings to avoid a complaint about
+ * an unknown warning type from older compilers that don't know about
+ * -Waddress-of-packed-member.
+ * - gcc has bugs in its _Pragma() support in some versions, eg
+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
+ * include the warning-suppression pragmas for clang

Quoted 5 lines

Perhaps s/in some versions/prior to gcc-13/ ? At least that's what the bugzilla says, and it will help when auditing for compiler versions in a few years when gcc-12 is EOL.

Looking at this now, This bug is in clang 7, from 2018. For FreeBSD, we don't support
anything older than clang 12 or 13 However, the bug still exists in clang 16, the latest.
I believe this was also copied verbatim from linux-user, so let's leave it and then make
this one of the common things as a followup... ok?

Warner

Either way,

Quoted 4 lines

r~

#6 lore raw

Re: [PATCH 02/33] Disable clang warnings arising from bsd-user/qemu.h

Quoted 4 lines · Warner Losh

Looking at this now, This bug is in clang 7, from 2018. For FreeBSD, we don't support anything older than clang 12 or 13 However, the bug still exists in clang 16, the latest. I believe this was also copied verbatim from linux-user, so let's leave it and then make this one of the common things as a followup... ok?

Ah. In which case this should simply be moved to qemu/compiler.h.

r~

#7 lore raw

Re: [PATCH 02/33] Disable clang warnings arising from bsd-user/qemu.h

Quoted 11 lines · Richard Henderson
Quoted 1 line · Warner Losh

Looking at this now, This bug is in clang 7, from 2018. For FreeBSD, we

don't support

Quoted 1 line

anything older than clang 12 or 13 However, the bug still exists in

clang 16, the latest.

Quoted 1 line

I believe this was also copied verbatim from linux-user, so let's leave

it and then make

Quoted 1 line

this one of the common things as a followup... ok?

Ah. In which case this should simply be moved to qemu/compiler.h.

OK. That we can do. I'll look into it.

Warner

Quoted 3 lines

r~

author
#8 lore raw

PATCH 3/33 Update the definitions of __put_user and __get_user macros

In reply to Karim Taha

From: Warner Losh <[email protected]>

Use __builtin_choose_expr to avoid type promotion from ?:
in __put_user_e and __get_user_e macros.
Copied from linux-user/qemu.h, originally by Blue Swirl.

---
 bsd-user/qemu.h   | 81 ++++++++++++++++++++---------------------------
 bsd-user/signal.c |  5 +--
 2 files changed, 35 insertions(+), 51 deletions(-)
bsd-user/qemu.h +34 -47
index dfdfa8dd67..c41ebfe937 100644
@@ -307,50 +307,37 @@ static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
 #define PRAGMA_REENABLE_PACKED_WARNING
 #endif
 
-#define __put_user(x, hptr)\
-({\
-    int size = sizeof(*hptr);\
-    switch (size) {\
-    case 1:\
-        *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
-        break;\
-    case 2:\
-        *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
-        break;\
-    case 4:\
-        *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
-        break;\
-    case 8:\
-        *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
-        break;\
-    default:\
-        abort();\
-    } \
-    0;\
-})
+#define __put_user_e(x, hptr, e)                                            \
+    do {                                                                    \
+        PRAGMA_DISABLE_PACKED_WARNING;                                      \
+        (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,                 \
+        __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,            \
+        __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p,            \
+        __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort))))  \
+            ((hptr), (x)), (void)0);                                        \
+        PRAGMA_REENABLE_PACKED_WARNING;                                     \
+    } while (0)
+
+#define __get_user_e(x, hptr, e)                                            \
+    do {                                                                    \
+        PRAGMA_DISABLE_PACKED_WARNING;                                      \
+        ((x) = (typeof(*hptr))(                                             \
+        __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,                 \
+        __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p,           \
+        __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p,            \
+        __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort))))  \
+            (hptr)), (void)0);                                              \
+        PRAGMA_REENABLE_PACKED_WARNING;                                     \
+    } while (0)
 
-#define __get_user(x, hptr) \
-({\
-    int size = sizeof(*hptr);\
-    switch (size) {\
-    case 1:\
-        x = (typeof(*hptr))*(uint8_t *)(hptr);\
-        break;\
-    case 2:\
-        x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
-        break;\
-    case 4:\
-        x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
-        break;\
-    case 8:\
-        x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
-        break;\
-    default:\
-        x = 0;\
-        abort();\
-    } \
-    0;\
-})
+
+#if TARGET_BIG_ENDIAN
+# define __put_user(x, hptr)  __put_user_e(x, hptr, be)
+# define __get_user(x, hptr)  __get_user_e(x, hptr, be)
+#else
+# define __put_user(x, hptr)  __put_user_e(x, hptr, le)
+# define __get_user(x, hptr)  __get_user_e(x, hptr, le)
+#endif
 
 /*
  * put_user()/get_user() take a guest address and check access
@@ -363,10 +350,10 @@ static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
 ({                                                                      \
     abi_ulong __gaddr = (gaddr);                                        \
     target_type *__hptr;                                                \
-    abi_long __ret;                                                     \
+    abi_long __ret = 0;                                                 \
     __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0);  \
     if (__hptr) {                                                       \
-        __ret = __put_user((x), __hptr);                                \
+        __put_user((x), __hptr);                                        \
         unlock_user(__hptr, __gaddr, sizeof(target_type));              \
     } else                                                              \
         __ret = -TARGET_EFAULT;                                         \
@@ -377,10 +364,10 @@ static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
 ({                                                                      \
     abi_ulong __gaddr = (gaddr);                                        \
     target_type *__hptr;                                                \
-    abi_long __ret;                                                     \
+    abi_long __ret = 0;                                                 \
     __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1);   \
     if (__hptr) {                                                       \
-        __ret = __get_user((x), __hptr);                                \
+        __get_user((x), __hptr);                                        \
         unlock_user(__hptr, __gaddr, 0);                                \
     } else {                                                            \
         (x) = 0;                                                        \
bsd-user/signal.c +1 -4
index f4e078ee1d..4db85a3485 100644
@@ -787,10 +787,7 @@ static int reset_signal_mask(target_ucontext_t *ucontext)
     TaskState *ts = (TaskState *)thread_cpu->opaque;
 
     for (i = 0; i < TARGET_NSIG_WORDS; i++) {
-        if (__get_user(target_set.__bits[i],
-                    &ucontext->uc_sigmask.__bits[i])) {
-            return -TARGET_EFAULT;
-        }
+        __get_user(target_set.__bits[i], &ucontext->uc_sigmask.__bits[i]);
     }
     target_to_host_sigset_internal(&blocked, &target_set);
     ts->signal_mask = blocked;
Signature · 1 line
2.40.0
Reviewed-by on v1 3/33
#9 lore raw

Re: [PATCH 03/33] Update the definitions of __put_user and __get_user macros

Quoted 54 lines · Karim Taha

From: Warner Losh <[email protected]>

Use __builtin_choose_expr to avoid type promotion from ?:
in __put_user_e and __get_user_e macros.
Copied from linux-user/qemu.h, originally by Blue Swirl.

---
  bsd-user/qemu.h   | 81 ++++++++++++++++++++---------------------------
  bsd-user/signal.c |  5 +--
  2 files changed, 35 insertions(+), 51 deletions(-)
bsd-user/qemu.h +12 -21
index dfdfa8dd67..c41ebfe937 100644
@@ -307,50 +307,37 @@ static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
  #define PRAGMA_REENABLE_PACKED_WARNING
  #endif
 
-#define __put_user(x, hptr)\
-({\
-    int size = sizeof(*hptr);\
-    switch (size) {\
-    case 1:\
-        *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
-        break;\
-    case 2:\
-        *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
-        break;\
-    case 4:\
-        *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
-        break;\
-    case 8:\
-        *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
-        break;\
-    default:\
-        abort();\
-    } \
-    0;\
-})
+#define __put_user_e(x, hptr, e)                                            \
+    do {                                                                    \
+        PRAGMA_DISABLE_PACKED_WARNING;                                      \
+        (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,                 \
+        __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,            \
+        __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p,            \
+        __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort))))  \
+            ((hptr), (x)), (void)0);                                        \
+        PRAGMA_REENABLE_PACKED_WARNING;                                     \
+    } while (0)
+
+#define __get_user_e(x, hptr, e)                                            \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong addr, abi_ulong size) · line 331

Quoted 10 lines
bsd-user/qemu.h +10 -0
+    do {                                                                    \
+        PRAGMA_DISABLE_PACKED_WARNING;                                      \
+        ((x) = (typeof(*hptr))(                                             \
+        __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,                 \
+        __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p,           \
+        __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p,            \
+        __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort))))  \
+            (hptr)), (void)0);                                              \
+        PRAGMA_REENABLE_PACKED_WARNING;                                     \
+    } while (0)

Hmm. I guess this works. The typeof cast in __get_user_e being required when sizeof(x) > sizeof(*hptr) in order to get the correct extension.

Is it clearer with _Generic?

    (x) = _Generic(*(hptr),
                   int8_t: *(int8_t *)(hptr),
                   uint8_t: *(uint8_t *)(hptr),
                   int16_t: (int16_t)lduw_##e##_p(hptr),
                   uint16_t: lduw_##e##_p(hptr),
                   int32_t: (int32_t)ldl_##e##_p(hptr),
                   uint32_t: (uint32_t)ldl_##e##_p(hptr),
                   int64_t: (int64_t)ldq_##e##_p(hptr),
                   uint64_t: ldq_##e##_p(hptr));

In particular I believe the error message will be much prettier.

Either way,

r~

#10 lore raw

Re: [PATCH 03/33] Update the definitions of __put_user and __get_user macros

Quoted 81 lines · Richard Henderson

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong

Quoted 18 lines · Karim Taha

From: Warner Losh <[email protected]>

Use __builtin_choose_expr to avoid type promotion from ?:
in __put_user_e and __get_user_e macros.
Copied from linux-user/qemu.h, originally by Blue Swirl.

---
  bsd-user/qemu.h   | 81 ++++++++++++++++++++---------------------------
  bsd-user/signal.c |  5 +--
  2 files changed, 35 insertions(+), 51 deletions(-)
bsd-user/qemu.h +0 -0
index dfdfa8dd67..c41ebfe937 100644
@@ -307,50 +307,37 @@ static inline bool access_ok(int type, abi_ulong

addr, abi_ulong size)

Quoted 15 lines
bsd-user/qemu.h +0 -12
  #define PRAGMA_REENABLE_PACKED_WARNING
  #endif
 
-#define __put_user(x, hptr)\
-({\
-    int size = sizeof(*hptr);\
-    switch (size) {\
-    case 1:\
-        *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
-        break;\
-    case 2:\
-        *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
-        break;\
-    case 4:\
-        *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)

Quoted 10 lines
bsd-user/qemu.h +1 -9
-        break;\
-    case 8:\
-        *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
-        break;\
-    default:\
-        abort();\
-    } \
-    0;\
-})
+#define __put_user_e(x, hptr, e)
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 311

Quoted 1 line
bsd-user/qemu.h +1 -0
+    do {
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 312

Quoted 1 line
bsd-user/qemu.h +1 -0
+        PRAGMA_DISABLE_PACKED_WARNING;
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 313

Quoted 1 line
bsd-user/qemu.h +1 -0
+        (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,
     \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 314

Quoted 1 line
bsd-user/qemu.h +1 -0
+        __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 315

Quoted 1 line
bsd-user/qemu.h +1 -0
+        __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p,
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 316

Quoted 1 line
bsd-user/qemu.h +1 -0
+        __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p,

abort)))) \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 317

Quoted 1 line
bsd-user/qemu.h +1 -0
+            ((hptr), (x)), (void)0);
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 318

Quoted 1 line
bsd-user/qemu.h +1 -0
+        PRAGMA_REENABLE_PACKED_WARNING;
     \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)

Quoted 3 lines
bsd-user/qemu.h +3 -0
+    } while (0)
+
+#define __get_user_e(x, hptr, e)
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 322

Quoted 1 line
bsd-user/qemu.h +1 -0
+    do {
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 323

Quoted 1 line
bsd-user/qemu.h +1 -0
+        PRAGMA_DISABLE_PACKED_WARNING;
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 324

Quoted 1 line
bsd-user/qemu.h +1 -0
+        ((x) = (typeof(*hptr))(
     \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 325

Quoted 1 line
bsd-user/qemu.h +1 -0
+        __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,
     \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 326

Quoted 1 line
bsd-user/qemu.h +1 -0
+        __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p,
     \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 327

Quoted 1 line
bsd-user/qemu.h +1 -0
+        __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p,
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 328

Quoted 1 line
bsd-user/qemu.h +1 -0
+        __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p,

abort)))) \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 329

Quoted 1 line
bsd-user/qemu.h +1 -0
+            (hptr)), (void)0);
Quoted 9 lines
    \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 330

Quoted 1 line
bsd-user/qemu.h +1 -0
+        PRAGMA_REENABLE_PACKED_WARNING;
     \

bsd-user/qemu.h static inline bool access_ok(int type, abi_ulong · line 331

Quoted 1 line
bsd-user/qemu.h +1 -0
+    } while (0)

Hmm. I guess this works. The typeof cast in __get_user_e being required when sizeof(x) >
sizeof(*hptr) in order to get the correct extension.

This code was copied 100% from the current linux-user :) It was also copied before I needed
to do _Generic for a different project for something as well, so I didn't think to switch over
from the old-school, gcc-specific hack to the modern clean form.

Quoted 12 lines

Is it clearer with _Generic?

     (x) = _Generic(*(hptr),
                    int8_t: *(int8_t *)(hptr),
                    uint8_t: *(uint8_t *)(hptr),
                    int16_t: (int16_t)lduw_##e##_p(hptr),
                    uint16_t: lduw_##e##_p(hptr),
                    int32_t: (int32_t)ldl_##e##_p(hptr),
                    uint32_t: (uint32_t)ldl_##e##_p(hptr),
                    int64_t: (int64_t)ldq_##e##_p(hptr),
                    uint64_t: ldq_##e##_p(hptr));
Quoted 2 lines

In particular I believe the error message will be much prettier.

Indeed. That looks cleaner. I'll see if I can test that against the latest bsd-user upstream.

Warner

Quoted 6 lines

Either way,

r~

#11 lore raw

Re: [PATCH 03/33] Update the definitions of __put_user and __get_user macros

Quoted 28 lines · Warner Losh
     > +#define __put_user_e(x, hptr, e)                                            \
     > +    do {                                                                    \
     > +        PRAGMA_DISABLE_PACKED_WARNING;                                      \
     > +        (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,                 \
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p,            \
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p,            \
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort))))  \
     > +            ((hptr), (x)), (void)0);                                        \
     > +        PRAGMA_REENABLE_PACKED_WARNING;                                     \
     > +    } while (0)
     > +
     > +#define __get_user_e(x, hptr, e)                                            \
     > +    do {                                                                    \
     > +        PRAGMA_DISABLE_PACKED_WARNING;                                      \
     > +        ((x) = (typeof(*hptr))(                                             \
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,                 \
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p,           \
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p,            \
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort))))  \
     > +            (hptr)), (void)0);                                              \
     > +        PRAGMA_REENABLE_PACKED_WARNING;                                     \
     > +    } while (0)

    Hmm.  I guess this works.  The typeof cast in __get_user_e being required when
    sizeof(x) >
    sizeof(*hptr) in order to get the correct extension.
Quoted 1 line

This code was copied 100% from the current linux-user :)

Ha ha indeed! I should have known.

Quoted 15 lines
    Is it clearer with _Generic?

          (x) = _Generic(*(hptr),
                         int8_t: *(int8_t *)(hptr),
                         uint8_t: *(uint8_t *)(hptr),
                         int16_t: (int16_t)lduw_##e##_p(hptr),
                         uint16_t: lduw_##e##_p(hptr),
                         int32_t: (int32_t)ldl_##e##_p(hptr),
                         uint32_t: (uint32_t)ldl_##e##_p(hptr),
                         int64_t: (int64_t)ldq_##e##_p(hptr),
                         uint64_t: ldq_##e##_p(hptr));

    In particular I believe the error message will be much prettier.
Quoted 1 line

Indeed. That looks cleaner. I'll see if I can test that against the latest bsd-user upstream.

I'll see if we can share this code via common-user.

r~

#12 lore raw

Re: [PATCH 03/33] Update the definitions of __put_user and __get_user macros

Quoted 44 lines · Richard Henderson
Quoted 1 line · Warner Losh
     > +#define __put_user_e(x, hptr, e)
            \
Quoted 1 line
     > +    do {
            \
Quoted 1 line
     > +        PRAGMA_DISABLE_PACKED_WARNING;
            \
Quoted 1 line
     > +        (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p,
           \
Quoted 1 line
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 2,

stw_##e##_p, \

Quoted 1 line
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 4,

stl_##e##_p, \

Quoted 1 line
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p,

abort)))) \

Quoted 1 line
     > +            ((hptr), (x)), (void)0);
            \
Quoted 1 line
     > +        PRAGMA_REENABLE_PACKED_WARNING;
           \
Quoted 3 lines
     > +    } while (0)
     > +
     > +#define __get_user_e(x, hptr, e)
            \
Quoted 1 line
     > +    do {
            \
Quoted 1 line
     > +        PRAGMA_DISABLE_PACKED_WARNING;
            \
Quoted 1 line
     > +        ((x) = (typeof(*hptr))(
           \
Quoted 1 line
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p,
           \
Quoted 1 line
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 2,

lduw_##e##_p, \

Quoted 1 line
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 4,

ldl_##e##_p, \

Quoted 1 line
     > +        __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p,

abort)))) \

Quoted 1 line
     > +            (hptr)), (void)0);
            \
Quoted 1 line
     > +        PRAGMA_REENABLE_PACKED_WARNING;
           \
Quoted 3 lines

> + } while (0)

Hmm. I guess this works. The typeof cast in __get_user_e being

Quoted 9 lines

required when

Quoted 5 lines

sizeof(x) >
sizeof(*hptr) in order to get the correct extension.

This code was copied 100% from the current linux-user :)

Ha ha indeed! I should have known.

Yea... It's old-school crazy, and I've done a lot of that in my day, but not here :)

Quoted 12 lines
Quoted 12 lines
    Is it clearer with _Generic?

          (x) = _Generic(*(hptr),
                         int8_t: *(int8_t *)(hptr),
                         uint8_t: *(uint8_t *)(hptr),
                         int16_t: (int16_t)lduw_##e##_p(hptr),
                         uint16_t: lduw_##e##_p(hptr),
                         int32_t: (int32_t)ldl_##e##_p(hptr),
                         uint32_t: (uint32_t)ldl_##e##_p(hptr),
                         int64_t: (int64_t)ldq_##e##_p(hptr),
                         uint64_t: ldq_##e##_p(hptr));
Quoted 8 lines
Quoted 4 lines

In particular I believe the error message will be much prettier.

Indeed. That looks cleaner. I'll see if I can test that against the

latest bsd-user upstream.

I'll see if we can share this code via common-user.

It seems to work, though I still need the pragmas for clang 16 (see another patch for that).

I tried to implement both get and put in terms of this, but found that it broke the blitz
branch. So why don't we land this as is for bsd-user and then one of us can try to
put it into common-user so as to not create too many waves for our GSoC student Kariim.
There's already enough to fix in this series... Sound fair?

Warner

#13 lore raw

Re: [PATCH 03/33] Update the definitions of __put_user and __get_user macros

Quoted 4 lines · Warner Losh

I tried to implement both get and put in terms of this, but found that it broke the blitz branch. So why don't we land this as is for bsd-user and then one of us can try to put it into common-user so as to not create too many waves for our GSoC student Kariim. There's already enough to fix in this series... Sound fair?

Yes, that's fine.

r~

author
#14 lore raw

PATCH 4/33 Declarations of h2t and t2h conversion functions.

In reply to Karim Taha

From: Stacey Son <[email protected]>

Declarations of functions that convert between host and target structs.

---
 bsd-user/freebsd/qemu-os.h | 35 +++++++++++++++++++++++++++++++++++
 bsd-user/qemu.h            |  1 +
 2 files changed, 36 insertions(+)
 create mode 100644 bsd-user/freebsd/qemu-os.h
bsd-user/freebsd/qemu-os.h new file +35 -0
new file mode 100644
index 0000000000..7ef4c55350
@@ -0,0 +1,35 @@
+/*
+ *  FreeBSD conversion extern declarations
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OS_H
+#define QEMU_OS_H
+
+/* qemu/osdep.h pulls in the rest */
+
+#include <sys/acl.h>
+#include <sys/mount.h>
+#include <sys/timex.h>
+#include <sys/rtprio.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+struct freebsd11_stat;
+
+#endif /* QEMU_OS_H */
bsd-user/qemu.h +1 -0
index c41ebfe937..1344c3fce6 100644
@@ -38,6 +38,7 @@ extern char **environ;
 #include "exec/gdbstub.h"
 #include "qemu/clang-tsa.h"
 
+#include "qemu-os.h"
 /*
  * This struct is used to hold certain information about the image.  Basically,
  * it replicates in user space what would be certain task_struct fields in the
Signature · 1 line
2.40.0
#15 lore raw

Re: [PATCH 04/33] Declarations of h2t and t2h conversion functions.

Quoted 58 lines · Karim Taha

From: Stacey Son <[email protected]>

Declarations of functions that convert between host and target structs.

---
  bsd-user/freebsd/qemu-os.h | 35 +++++++++++++++++++++++++++++++++++
  bsd-user/qemu.h            |  1 +
  2 files changed, 36 insertions(+)

create mode 100644 bsd-user/freebsd/qemu-os.h

bsd-user/freebsd/qemu-os.h new file +35 -0
new file mode 100644
index 0000000000..7ef4c55350
@@ -0,0 +1,35 @@
+/*
+ *  FreeBSD conversion extern declarations
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OS_H
+#define QEMU_OS_H
+
+/* qemu/osdep.h pulls in the rest */
+
+#include <sys/acl.h>
+#include <sys/mount.h>
+#include <sys/timex.h>
+#include <sys/rtprio.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+struct freebsd11_stat;
+
+#endif /* QEMU_OS_H */
bsd-user/qemu.h +0 -0
index c41ebfe937..1344c3fce6 100644

bsd-user/qemu.h extern char **environ; · line 44

Quoted 7 lines
bsd-user/qemu.h +1 -0
@@ -38,6 +38,7 @@ extern char **environ;
  #include "exec/gdbstub.h"
  #include "qemu/clang-tsa.h"
 
+#include "qemu-os.h"
  /*
   * This struct is used to hold certain information about the image.  Basically,
   * it replicates in user space what would be certain task_struct fields in the

The subject and comment do not match the patch, or at least not obviously.
This appears to merely include some extra system headers, not declare any sort of coversion functions.

r~

author
#16 lore raw

PATCH 5/33 Forward declare functions defined in os-stat.c

In reply to Karim Taha

From: Michal Meloun <[email protected]>

Add to bsd-user/freebsd/qemu-os.h the forward declarations of conversion functions related to stat syscalls.

---
 bsd-user/freebsd/qemu-os.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
bsd-user/freebsd/qemu-os.h +15 -0
index 7ef4c55350..12adc50928 100644
@@ -32,4 +32,19 @@
 
 struct freebsd11_stat;
 
+/* os-stat.c */
+abi_long h2t_freebsd11_stat(abi_ulong target_addr,
+        struct freebsd11_stat *host_st);
+abi_long h2t_freebsd11_nstat(abi_ulong target_addr,
+        struct freebsd11_stat *host_st);
+abi_long t2h_freebsd_fhandle(fhandle_t *host_fh, abi_ulong target_addr);
+abi_long h2t_freebsd_fhandle(abi_ulong target_addr, fhandle_t *host_fh);
+abi_long h2t_freebsd11_statfs(abi_ulong target_addr,
+    struct freebsd11_statfs *host_statfs);
+abi_long target_to_host_fcntl_cmd(int cmd);
+abi_long h2t_freebsd_stat(abi_ulong target_addr,
+        struct stat *host_st);
+abi_long h2t_freebsd_statfs(abi_ulong target_addr,
+    struct statfs *host_statfs);
+
 #endif /* QEMU_OS_H */
Signature · 1 line
2.40.0
#17 lore raw

Re: [PATCH 05/33] Forward declare functions defined in os-stat.c

Quoted 24 lines · Karim Taha

From: Michal Meloun <[email protected]>

Add to bsd-user/freebsd/qemu-os.h the forward declarations of conversion functions related to stat syscalls.

---
  bsd-user/freebsd/qemu-os.h | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
bsd-user/freebsd/qemu-os.h +5 -0
index 7ef4c55350..12adc50928 100644
@@ -32,4 +32,19 @@
 
  struct freebsd11_stat;
 
+/* os-stat.c */
+abi_long h2t_freebsd11_stat(abi_ulong target_addr,
+        struct freebsd11_stat *host_st);
+abi_long h2t_freebsd11_nstat(abi_ulong target_addr,
+        struct freebsd11_stat *host_st);

bsd-user/freebsd/qemu-os.h line 49

Quoted 10 lines
bsd-user/freebsd/qemu-os.h +10 -0
+abi_long t2h_freebsd_fhandle(fhandle_t *host_fh, abi_ulong target_addr);
+abi_long h2t_freebsd_fhandle(abi_ulong target_addr, fhandle_t *host_fh);
+abi_long h2t_freebsd11_statfs(abi_ulong target_addr,
+    struct freebsd11_statfs *host_statfs);
+abi_long target_to_host_fcntl_cmd(int cmd);
+abi_long h2t_freebsd_stat(abi_ulong target_addr,
+        struct stat *host_st);
+abi_long h2t_freebsd_statfs(abi_ulong target_addr,
+    struct statfs *host_statfs);
+

This appears to be the patch that matches the subject and comment for patch 4.

r~

author
#18 lore raw

PATCH 6/33 Add struct target_freebsd11_stat to bsd-user/syscall_defs

In reply to Karim Taha

From: Stacey Son <[email protected]>

---
 bsd-user/syscall_defs.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
bsd-user/syscall_defs.h +33 -0
index b6d113d24a..5ef76ffa52 100644
@@ -179,6 +179,39 @@ struct target_freebsd__wrusage {
     struct target_freebsd_rusage wru_children;
 };
 
+/*
+ * sys/stat.h
+ */
+struct target_freebsd11_stat {
+    uint32_t  st_dev;       /* inode's device */
+    uint32_t  st_ino;       /* inode's number */
+    int16_t   st_mode;      /* inode protection mode */
+    int16_t   st_nlink;     /* number of hard links */
+    uint32_t  st_uid;       /* user ID of the file's owner */
+    uint32_t  st_gid;       /* group ID of the file's group */
+    uint32_t  st_rdev;      /* device type */
+    struct  target_freebsd_timespec st_atim; /* time last accessed */
+    struct  target_freebsd_timespec st_mtim; /* time last data modification */
+    struct  target_freebsd_timespec st_ctim; /* time last file status change */
+    int64_t    st_size;     /* file size, in bytes */
+    int64_t    st_blocks;   /* blocks allocated for file */
+    uint32_t   st_blksize;  /* optimal blocksize for I/O */
+    uint32_t   st_flags;    /* user defined flags for file */
+    __uint32_t st_gen;      /* file generation number */
+    __int32_t  st_lspare;
+    struct target_freebsd_timespec st_birthtim; /* time of file creation */
+    /*
+     * Explicitly pad st_birthtim to 16 bytes so that the size of
+     * struct stat is backwards compatible.  We use bitfields instead
+     * of an array of chars so that this doesn't require a C99 compiler
+     * to compile if the size of the padding is 0.  We use 2 bitfields
+     * to cover up to 64 bits on 32-bit machines.  We assume that
+     * CHAR_BIT is 8...
+     */
+    unsigned int:(8 / 2) * (16 - (int)sizeof(struct target_freebsd_timespec));
+    unsigned int:(8 / 2) * (16 - (int)sizeof(struct target_freebsd_timespec));
+} __packed;
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
Signature · 1 line
2.40.0
#20 lore raw

Re: [PATCH 06/33] Add struct target_freebsd11_stat to bsd-user/syscall_defs

In reply to Karim Taha

bsd-user/syscall_defs.h struct target_freebsd__wrusage { · line 201

Quoted 3 lines · Karim Taha
bsd-user/syscall_defs.h +3 -0
+    uint32_t   st_flags;    /* user defined flags for file */
+    __uint32_t st_gen;      /* file generation number */
+    __int32_t  st_lspare;

Oh, drop the __ types.

r~

#21 lore raw

Re: [PATCH 06/33] Add struct target_freebsd11_stat to bsd-user/syscall_defs

Quoted 7 lines · Richard Henderson

bsd-user/syscall_defs.h struct target_freebsd__wrusage { · line 201

Quoted 3 lines · Karim Taha
bsd-user/syscall_defs.h +3 -0
+    uint32_t   st_flags;    /* user defined flags for file */
+    __uint32_t st_gen;      /* file generation number */
+    __int32_t  st_lspare;

Oh, drop the __ types.

Agreed. The original from the blitz branch copied from FreeBSD's stat.h file which
used the __ types to avoid namespace pollution... a constraint we don't have here.

Warner

author
#22 lore raw

PATCH 7/33 Add struct target_stat to bsd-user/syscall_defs.h

In reply to Karim Taha

From: Michal Meloun <[email protected]>

---
 bsd-user/syscall_defs.h | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
bsd-user/syscall_defs.h +38 -0
index 5ef76ffa52..e796e7e13d 100644
@@ -212,6 +212,44 @@ struct target_freebsd11_stat {
     unsigned int:(8 / 2) * (16 - (int)sizeof(struct target_freebsd_timespec));
 } __packed;
 
+#if defined(__i386__)
+#define TARGET_HAS_STAT_TIME_T_EXT       1
+#endif
+
+struct target_stat {
+        uint64_t  st_dev;               /* inode's device */
+        uint64_t  st_ino;               /* inode's number */
+        uint64_t  st_nlink;             /* number of hard links */
+        int16_t   st_mode;              /* inode protection mode */
+        int16_t   st_padding0;
+        uint32_t  st_uid;               /* user ID of the file's owner */
+        uint32_t  st_gid;               /* group ID of the file's group */
+        int32_t   st_padding1;
+        uint64_t  st_rdev;              /* device type */
+#ifdef TARGET_HAS_STAT_TIME_T_EXT
+        int32_t   st_atim_ext;
+#endif
+        struct  target_freebsd_timespec st_atim; /* time of last access */
+#ifdef TARGET_HAS_STAT_TIME_T_EXT
+        int32_t   st_mtim_ext;
+#endif
+        struct  target_freebsd_timespec st_mtim; /* time of last data modification */
+#ifdef TARGET_HAS_STAT_TIME_T_EXT
+        __int32_t st_ctim_ext;
+#endif
+        struct  target_freebsd_timespec st_ctim;/* time of last file status change */
+#ifdef TARGET_HAS_STAT_TIME_T_EXT
+        __int32_t st_btim_ext;
+#endif
+        struct  target_freebsd_timespec st_birthtim;   /* time of file creation */
+        int64_t   st_size;              /* file size, in bytes */
+        int64_t   st_blocks;            /* blocks allocated for file */
+        uint32_t  st_blksize;           /* optimal blocksize for I/O */
+        uint32_t  st_flags;             /* user defined flags for file */
+        uint64_t  st_gen;               /* file generation number */
+        uint64_t  st_spare[10];
+};
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
Signature · 1 line
2.40.0
Acked-by on v1 7/33
#23 lore raw

Re: [PATCH 07/33] Add struct target_stat to bsd-user/syscall_defs.h

bsd-user/syscall_defs.h struct target_freebsd11_stat { · line 222

Quoted 4 lines · Karim Taha
bsd-user/syscall_defs.h +4 -0
+struct target_stat {
+        uint64_t  st_dev;               /* inode's device */
+        uint64_t  st_ino;               /* inode's number */
+        uint64_t  st_nlink;             /* number of hard links */

Indentation of 8 instead of 4.

Otherwise,

r~

author
#24 lore raw

PATCH 8/33 Add structs target_freebsd11_nstat and target_freebsd11_statfs to bsd-user/syscall_defs.h

In reply to Karim Taha

From: Stacey Son <[email protected]>

---
 bsd-user/syscall_defs.h | 65 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
bsd-user/syscall_defs.h +65 -0
index e796e7e13d..06be8244de 100644
@@ -250,6 +250,71 @@ struct target_stat {
         uint64_t  st_spare[10];
 };
 
+
+/* struct nstat is the same as stat above but without the st_lspare field */
+struct target_freebsd11_nstat {
+    uint32_t  st_dev;       /* inode's device */
+    uint32_t  st_ino;       /* inode's number */
+    int16_t   st_mode;      /* inode protection mode */
+    int16_t   st_nlink;     /* number of hard links */
+    uint32_t  st_uid;       /* user ID of the file's owner */
+    uint32_t  st_gid;       /* group ID of the file's group */
+    uint32_t  st_rdev;      /* device type */
+    struct  target_freebsd_timespec st_atim; /* time last accessed */
+    struct  target_freebsd_timespec st_mtim; /* time last data modification */
+    struct  target_freebsd_timespec st_ctim; /* time last file status change */
+    int64_t    st_size;     /* file size, in bytes */
+    int64_t    st_blocks;   /* blocks allocated for file */
+    uint32_t   st_blksize;  /* optimal blocksize for I/O */
+    uint32_t   st_flags;    /* user defined flags for file */
+    __uint32_t st_gen;      /* file generation number */
+    /* __int32_t  st_lspare; */
+    struct target_freebsd_timespec st_birthtim; /* time of file creation */
+    /*
+     * Explicitly pad st_birthtim to 16 bytes so that the size of
+     * struct stat is backwards compatible.  We use bitfields instead
+     * of an array of chars so that this doesn't require a C99 compiler
+     * to compile if the size of the padding is 0.  We use 2 bitfields
+     * to cover up to 64 bits on 32-bit machines.  We assume that
+     * CHAR_BIT is 8...
+     */
+    unsigned int:(8 / 2) * (16 - (int)sizeof(struct target_freebsd_timespec));
+    unsigned int:(8 / 2) * (16 - (int)sizeof(struct target_freebsd_timespec));
+} __packed;
+
+/*
+ * sys/mount.h
+ */
+
+/* filesystem id type */
+typedef struct target_freebsd_fsid { int32_t val[2]; } target_freebsd_fsid_t;
+
+/* filesystem statistics */
+struct target_freebsd11_statfs {
+    uint32_t f_version; /* structure version number */
+    uint32_t f_type;    /* type of filesystem */
+    uint64_t f_flags;   /* copy of mount exported flags */
+    uint64_t f_bsize;   /* filesystem fragment size */
+    uint64_t f_iosize;  /* optimal transfer block size */
+    uint64_t f_blocks;  /* total data blocks in filesystem */
+    uint64_t f_bfree;   /* free blocks in filesystem */
+    int64_t  f_bavail;  /* free blocks avail to non-superuser */
+    uint64_t f_files;   /* total file nodes in filesystem */
+    int64_t  f_ffree;   /* free nodes avail to non-superuser */
+    uint64_t f_syncwrites;  /* count of sync writes since mount */
+    uint64_t f_asyncwrites; /* count of async writes since mount */
+    uint64_t f_syncreads;   /* count of sync reads since mount */
+    uint64_t f_asyncreads;  /* count of async reads since mount */
+    uint64_t f_spare[10];   /* unused spare */
+    uint32_t f_namemax; /* maximum filename length */
+    uint32_t f_owner;   /* user that mounted the filesystem */
+    target_freebsd_fsid_t   f_fsid; /* filesystem id */
+    char     f_charspare[80];           /* spare string space */
+    char     f_fstypename[16];   /* filesys type name */
+    char     f_mntfromname[88];    /* mount filesystem */
+    char     f_mntonname[88];      /* dir on which mounted*/
+};
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
Signature · 1 line
2.40.0
#25 lore raw

Re: [PATCH 08/33] Add structs target_freebsd11_nstat and target_freebsd11_statfs to bsd-user/syscall_defs.h

bsd-user/syscall_defs.h struct target_stat { · line 270

Quoted 2 lines · Karim Taha
bsd-user/syscall_defs.h +2 -0
+    uint32_t   st_flags;    /* user defined flags for file */
+    __uint32_t st_gen;      /* file generation number */

Drop the __.

Quoted 1 line

+ /* __int32_t st_lspare; */

Why commented out?

Quoted 1 line

+ struct target_freebsd_timespec st_birthtim; /* time of file creation */

Does that not place st_birthtim at the wrong place?

r~

#26 lore raw

Re: [PATCH 08/33] Add structs target_freebsd11_nstat and target_freebsd11_statfs to bsd-user/syscall_defs.h

Quoted 10 lines · Richard Henderson

bsd-user/syscall_defs.h struct target_stat { · line 270

Quoted 2 lines · Karim Taha
bsd-user/syscall_defs.h +2 -0
+    uint32_t   st_flags;    /* user defined flags for file */
+    __uint32_t st_gen;      /* file generation number */

Drop the __.

Quoted 1 line

+ /* __int32_t st_lspare; */

Why commented out?

I believe that the element was a padding one ....

Quoted 5 lines
Quoted 1 line

+ struct target_freebsd_timespec st_birthtim; /* time of file

creation */

Does that not place st_birthtim at the wrong place?

So this winds up in the right place because there's a hole...

However, having said that, I don't think it should be commented out. It's not
in the bsd-user branch. And the state of the upstream code is such that we can't
run full tests easily on the system calls, so we're making sure they basically
work, but will run the full regression test once some other changes are made to allow shared libraries to work (many of the calls in this patch are needed
to make 'hello world' work).

Warner

Quoted 3 lines

r~

#27 lore raw

Re: [PATCH 08/33] Add structs target_freebsd11_nstat and target_freebsd11_statfs to bsd-user/syscall_defs.h

Quoted 12 lines · Warner Losh

> +    /* __int32_t  st_lspare; */

Why commented out?

I believe that the element was a padding one ....

> +    struct target_freebsd_timespec st_birthtim; /* time of file creation */

Does that not place st_birthtim at the wrong place?

Quoted 8 lines

So this winds up in the right place because there's a hole...

However, having said that, I don't think it should be commented out. It's not in the bsd-user branch. And the state of the upstream code is such that we can't run full tests easily on the system calls, so we're making sure they basically work, but will run the full regression test once some other changes are made to allow shared libraries to work (many of the calls in this patch are needed to make 'hello world' work).

I think there is not a hole, because the struct is __packed.

(Also, QEMU_PACKED vs __packed?)

r~

#28 lore raw

Re: [PATCH 08/33] Add structs target_freebsd11_nstat and target_freebsd11_statfs to bsd-user/syscall_defs.h

Quoted 23 lines · Richard Henderson
Quoted 8 lines · Warner Losh

> + /* __int32_t st_lspare; */

Why commented out?

I believe that the element was a padding one ....

> + struct target_freebsd_timespec st_birthtim; /* time of file

creation */

Quoted 7 lines

Does that not place st_birthtim at the wrong place?

So this winds up in the right place because there's a hole...

However, having said that, I don't think it should be commented out.

It's not

Quoted 1 line

in the bsd-user branch. And the state of the upstream code is such that

we can't

Quoted 1 line

run full tests easily on the system calls, so we're making sure they

basically

Quoted 1 line

work, but will run the full regression test once some other changes are

Quoted 8 lines

made

Quoted 1 line

to allow shared libraries to work (many of the calls in this patch are

needed

Quoted 1 line

to make 'hello world' work).

I think there is not a hole, because the struct is __packed.

(Also, QEMU_PACKED vs __packed?)

There's a nstat that's an older stat w/o this field. I'm not entirely sure __packed should
be on either one of these, honestly. nstat is quite old, and I'm not at all sure what's up
with it. I think it dates from a time when there was only i386 and then we expanded to
alpha and needed to 'fix' this interface... Not sure why it got the freebsd11_ prefix, so
I'll have to chase those details down to see if this is an extra cut and paste or what.
That may take a little bit to chase down in the logs and in people's memory. I think
that's the back story. Normally, nstat is only defined in the kernel, and this is a binary
interface from ages ago that we likely don't need to implement, but I need to confirm
that, and make sure rust or go don't have some weird, misguided mistake...

But nstat and stat are supposed to be the same, except nstat omits st_spare, so that's
why it's commented out. stat's supposed to be carefully laid out so packed or not
doesn't matter. But testing that would take a little bit as well.

Warner

Quoted 2 lines

~

#29 lore raw

Re: [PATCH 08/33] Add structs target_freebsd11_nstat and target_freebsd11_statfs to bsd-user/syscall_defs.h

Quoted 56 lines · Warner Losh
Quoted 23 lines · Richard Henderson
Quoted 8 lines · Warner Losh

> + /* __int32_t st_lspare; */

Why commented out?

I believe that the element was a padding one ....

> + struct target_freebsd_timespec st_birthtim; /* time of file

creation */

Quoted 7 lines

Does that not place st_birthtim at the wrong place?

So this winds up in the right place because there's a hole...

However, having said that, I don't think it should be commented out.

It's not

Quoted 1 line

in the bsd-user branch. And the state of the upstream code is such that

we can't

Quoted 1 line

run full tests easily on the system calls, so we're making sure they

basically

Quoted 1 line

work, but will run the full regression test once some other changes are

Quoted 8 lines

made

Quoted 1 line

to allow shared libraries to work (many of the calls in this patch are

needed

Quoted 1 line

to make 'hello world' work).

I think there is not a hole, because the struct is __packed.

(Also, QEMU_PACKED vs __packed?)

There's a nstat that's an older stat w/o this field. I'm not entirely sure __packed should
be on either one of these, honestly. nstat is quite old, and I'm not at all sure what's up
with it. I think it dates from a time when there was only i386 and then we expanded to
alpha and needed to 'fix' this interface... Not sure why it got the freebsd11_ prefix, so
I'll have to chase those details down to see if this is an extra cut and paste or what.
That may take a little bit to chase down in the logs and in people's memory. I think
that's the back story. Normally, nstat is only defined in the kernel, and this is a binary
interface from ages ago that we likely don't need to implement, but I need to confirm
that, and make sure rust or go don't have some weird, misguided mistake...

Quoted 6 lines

But nstat and stat are supposed to be the same, except nstat omits st_spare, so that's
why it's commented out. stat's supposed to be carefully laid out so packed or not
doesn't matter. But testing that would take a little bit as well.

OK. Back in 1998, John Dyson added nstat and friends:
commit 1f5621728039a2009fc163d345508d0ee9fae2e9

Author: John Dyson <[email protected]>
Date:   Mon May 11 03:55:28 1998 +0000

Fix the futimes/undelete/utrace conflict with other BSD's. Note that the only common usage of utrace (the possible problem with this commit) is with malloc, so this should be a real problem. Add the various NetBSD syscalls that allow full emulation of their development environment.

such emulation hasn't worked since the ELF cut over in FreeSBD 3.2 in 1999,if it even did before that (there's some code to implement these, but
it's not at all clear to me it ever worked)... This was all preserved when FreeBSD 11 converted to 64-bit inodes.

So I guess we should preserve it, but I still need to find out the layout of nstat vs stat on different platforms (though the only systems we ran on at the time were i386, so after grubbing around with git blame, I completely retract my 'it was related to alpha' comments: they were completely wrong). But even so, I guess it was only ever used in life fire with a.out to run old netbsd a.out files from 25 years ago and it's relevance is zero since nobody has used it since then and all maintenance since then in FreeBSD has been basically useless.

I may try to do a regression build of rust w/o it to see if it's used there or not...

So today I learned...

Warner

author
#30 lore raw

PATCH 9/33 Add struct target_statfs to bsd-user/syscall_defs.h

In reply to Karim Taha

From: Michal Meloun <[email protected]>

---
 bsd-user/syscall_defs.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
bsd-user/syscall_defs.h +25 -0
index 06be8244de..ab59d00a47 100644
@@ -315,6 +315,31 @@ struct target_freebsd11_statfs {
     char     f_mntonname[88];      /* dir on which mounted*/
 };
 
+struct target_statfs {
+        uint32_t f_version;             /* structure version number */
+        uint32_t f_type;                /* type of filesystem */
+        uint64_t f_flags;               /* copy of mount exported flags */
+        uint64_t f_bsize;               /* filesystem fragment size */
+        uint64_t f_iosize;              /* optimal transfer block size */
+        uint64_t f_blocks;              /* total data blocks in filesystem */
+        uint64_t f_bfree;               /* free blocks in filesystem */
+        int64_t  f_bavail;              /* free blocks avail to non-superuser */
+        uint64_t f_files;               /* total file nodes in filesystem */
+        int64_t  f_ffree;               /* free nodes avail to non-superuser */
+        uint64_t f_syncwrites;          /* count of sync writes since mount */
+        uint64_t f_asyncwrites;         /* count of async writes since mount */
+        uint64_t f_syncreads;           /* count of sync reads since mount */
+        uint64_t f_asyncreads;          /* count of async reads since mount */
+        uint64_t f_spare[10];           /* unused spare */
+        uint32_t f_namemax;             /* maximum filename length */
+        uint32_t f_owner;               /* user that mounted the filesystem */
+        target_freebsd_fsid_t f_fsid;   /* filesystem id */
+        char      f_charspare[80];      /* spare string space */
+        char      f_fstypename[16];     /* filesystem type name */
+        char      f_mntfromname[1024];  /* mounted filesystem */
+        char      f_mntonname[1024];    /* directory on which mounted */
+};
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
Signature · 1 line
2.40.0
Acked-by on v1 9/33
#31 lore raw

Re: [PATCH 09/33] Add struct target_statfs to bsd-user/syscall_defs.h

bsd-user/syscall_defs.h struct target_freebsd11_statfs { · line 319

Quoted 2 lines · Karim Taha
bsd-user/syscall_defs.h +2 -0
+struct target_statfs {
+        uint32_t f_version;             /* structure version number */

Indentation. Otherwise,

r~

author
#32 lore raw

PATCH 10/33 Add struct target_freebsd_fhandle and fcntl flags to bsd-user/syscall_defs.h

In reply to Karim Taha

From: Stacey Son <[email protected]>

---
 bsd-user/syscall_defs.h | 51 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
bsd-user/syscall_defs.h +51 -0
index ab59d00a47..608731f0ac 100644
@@ -340,6 +340,57 @@ struct target_statfs {
         char      f_mntonname[1024];    /* directory on which mounted */
 };
 
+/* File identifier. These are unique per filesystem on a single machine. */
+#define TARGET_MAXFIDSZ     16
+
+struct target_freebsd_fid {
+    u_short     fid_len;            /* len of data in bytes */
+    u_short     fid_data0;          /* force longword align */
+    char        fid_data[TARGET_MAXFIDSZ];  /* data (variable len) */
+};
+
+/* Generic file handle */
+struct target_freebsd_fhandle {
+    target_freebsd_fsid_t   fh_fsid;    /* Filesystem id of mount point */
+    struct target_freebsd_fid fh_fid;   /* Filesys specific id */
+};
+typedef struct target_freebsd_fhandle target_freebsd_fhandle_t;
+
+/*
+ * sys/fcntl.h
+ */
+#define TARGET_F_DUPFD              0
+#define TARGET_F_GETFD              1
+#define TARGET_F_SETFD              2
+#define TARGET_F_GETFL              3
+#define TARGET_F_SETFL              4
+#define TARGET_F_GETOWN             5
+#define TARGET_F_SETOWN             6
+#define TARGET_F_OGETLK             7
+#define TARGET_F_OSETLK             8
+#define TARGET_F_OSETLKW            9
+#define TARGET_F_DUP2FD             10
+#define TARGET_F_GETLK              11
+#define TARGET_F_SETLK              12
+#define TARGET_F_SETLKW             13
+#define TARGET_F_SETLK_REMOTE       14
+#define TARGET_F_READAHEAD          15
+#define TARGET_F_RDAHEAD            16
+#define TARGET_F_DUPFD_CLOEXEC     17
+#define TARGET_F_DUP2FD_CLOEXEC    18
+/* FreeBSD-specific */
+#define TARGET_F_ADD_SEALS          19
+#define TARGET_F_GET_SEALS          20
+
+struct target_freebsd_flock {
+    int64_t l_start;
+    int64_t l_len;
+    int32_t l_pid;
+    int16_t l_type;
+    int16_t l_whence;
+    int32_t l_sysid;
+} QEMU_PACKED;
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
Signature · 1 line
2.40.0
Acked-by on v1 10/33
#33 lore raw

Re: [PATCH 10/33] Add struct target_freebsd_fhandle and fcntl flags to bsd-user/syscall_defs.h

bsd-user/syscall_defs.h struct target_statfs { · line 349

Quoted 4 lines · Karim Taha
bsd-user/syscall_defs.h +4 -0
+struct target_freebsd_fid {
+    u_short     fid_len;            /* len of data in bytes */
+    u_short     fid_data0;          /* force longword align */
+    char        fid_data[TARGET_MAXFIDSZ];  /* data (variable len) */

uint16_t?

Otherwise,

r~

#34 lore raw

Re: [PATCH 10/33] Add struct target_freebsd_fhandle and fcntl flags to bsd-user/syscall_defs.h

Quoted 8 lines · Richard Henderson

bsd-user/syscall_defs.h struct target_statfs { · line 349

Quoted 4 lines · Karim Taha
bsd-user/syscall_defs.h +4 -0
+struct target_freebsd_fid {
+    u_short     fid_len;            /* len of data in bytes */
+    u_short     fid_data0;          /* force longword align */
+    char        fid_data[TARGET_MAXFIDSZ];  /* data (variable len) */

uint16_t?

These were copied from FreeBSD's sys/mount.h... Changing to uint16_t likely is a good idea, though. I'll handle the logistics of making changes like this in
bsd-user upstream with Kariim.

Warner

Quoted 3 lines

Otherwise,

author
#35 lore raw

PATCH 11/33 Define safe_fcntl macro in bsd-user/syscall_defs.h

In reply to Karim Taha

From: Kyle Evans <[email protected]>

---
 bsd-user/syscall_defs.h | 2 ++
 1 file changed, 2 insertions(+)
bsd-user/syscall_defs.h +2 -0
index 608731f0ac..52ebadbe2c 100644
@@ -438,6 +438,8 @@ type safe_##name(type1 arg1, type2 arg2, type3 arg3, type4 arg4, \
     return safe_syscall(SYS_##name, arg1, arg2, arg3, arg4, arg5, arg6); \
 }
 
+#define safe_fcntl(...) safe_syscall(SYS_fcntl, __VA_ARGS__)
+
 /* So far all target and host bitmasks are the same */
 #define target_to_host_bitmask(x, tbl) (x)
 #define host_to_target_bitmask(x, tbl) (x)
Signature · 1 line
2.40.0
author
#37 lore raw

PATCH 12/33 Rename target_freebsd_time_t to target_time_t

In reply to Karim Taha

From: Warner Losh <[email protected]>

This is necessary for future code using target_time_t, in bsd-user/syscall_defs.

---
 bsd-user/syscall_defs.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
bsd-user/syscall_defs.h +4 -4
index 52ebadbe2c..b1d753e9d8 100644
@@ -45,9 +45,9 @@
  *
  */
 #if (!defined(TARGET_I386))
-typedef int64_t target_freebsd_time_t;
+typedef int64_t target_time_t;
 #else
-typedef int32_t target_freebsd_time_t;
+typedef int32_t target_time_t;
 #endif
 
 struct target_iovec {
@@ -102,7 +102,7 @@ typedef abi_long target_freebsd_suseconds_t;
 
 /* compare to sys/timespec.h */
 struct target_freebsd_timespec {
-    target_freebsd_time_t   tv_sec;     /* seconds */
+    target_time_t   tv_sec;     /* seconds */
     abi_long                tv_nsec;    /* and nanoseconds */
 #if !defined(TARGET_I386) && TARGET_ABI_BITS == 32
     abi_long _pad;
@@ -120,7 +120,7 @@ struct target_freebsd__umtx_time {
 };
 
 struct target_freebsd_timeval {
-    target_freebsd_time_t       tv_sec; /* seconds */
+    target_time_t       tv_sec; /* seconds */
     target_freebsd_suseconds_t  tv_usec;/* and microseconds */
 #if !defined(TARGET_I386) && TARGET_ABI_BITS == 32
     abi_long _pad;
Signature · 1 line
2.40.0
Reviewed-by on v1 12/33
#38 lore raw

Re: [PATCH 12/33] Rename target_freebsd_time_t to target_time_t

Quoted 10 lines · Karim Taha

From: Warner Losh<[email protected]>

This is necessary for future code using target_time_t, in bsd-user/syscall_defs.

---
  bsd-user/syscall_defs.h | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)

r~

author
#39 lore raw

PATCH 13/33 Implement host-target convertion functions

In reply to Karim Taha

From: Stacey Son <[email protected]>

Implement the stat converstion functions:
h2t_freebsd11_stat
h2t_freebsd_nstat

---
 bsd-user/freebsd/os-stat.c | 94 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
 create mode 100644 bsd-user/freebsd/os-stat.c
bsd-user/freebsd/os-stat.c new file +94 -0
new file mode 100644
index 0000000000..8c73f7402c
@@ -0,0 +1,94 @@
+/*
+ *  FreeBSD stat related conversion routines
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+
+#include "qemu.h"
+
+/*
+ * stat conversion
+ */
+abi_long h2t_freebsd11_stat(abi_ulong target_addr,
+        struct freebsd11_stat *host_st)
+{
+    struct target_freebsd11_stat *target_st;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    memset(target_st, 0, sizeof(*target_st));
+    __put_user(host_st->st_dev, &target_st->st_dev);
+    __put_user(host_st->st_ino, &target_st->st_ino);
+    __put_user(host_st->st_mode, &target_st->st_mode);
+    __put_user(host_st->st_nlink, &target_st->st_nlink);
+    __put_user(host_st->st_uid, &target_st->st_uid);
+    __put_user(host_st->st_gid, &target_st->st_gid);
+    __put_user(host_st->st_rdev, &target_st->st_rdev);
+    __put_user(host_st->st_atim.tv_sec, &target_st->st_atim.tv_sec);
+    __put_user(host_st->st_atim.tv_nsec, &target_st->st_atim.tv_nsec);
+    __put_user(host_st->st_mtim.tv_sec, &target_st->st_mtim.tv_sec);
+    __put_user(host_st->st_mtim.tv_nsec, &target_st->st_mtim.tv_nsec);
+    __put_user(host_st->st_ctim.tv_sec, &target_st->st_ctim.tv_sec);
+    __put_user(host_st->st_ctim.tv_nsec, &target_st->st_ctim.tv_nsec);
+    __put_user(host_st->st_size, &target_st->st_size);
+    __put_user(host_st->st_blocks, &target_st->st_blocks);
+    __put_user(host_st->st_blksize, &target_st->st_blksize);
+    __put_user(host_st->st_flags, &target_st->st_flags);
+    __put_user(host_st->st_gen, &target_st->st_gen);
+    /* st_lspare not used */
+    __put_user(host_st->st_birthtim.tv_sec, &target_st->st_birthtim.tv_sec);
+    __put_user(host_st->st_birthtim.tv_nsec, &target_st->st_birthtim.tv_nsec);
+    unlock_user_struct(target_st, target_addr, 1);
+
+    return 0;
+}
+
+abi_long h2t_freebsd11_nstat(abi_ulong target_addr,
+        struct freebsd11_stat *host_st)
+{
+    struct target_freebsd11_nstat *target_st;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    memset(target_st, 0, sizeof(*target_st));
+    __put_user(host_st->st_dev, &target_st->st_dev);
+    __put_user(host_st->st_ino, &target_st->st_ino);
+    __put_user(host_st->st_mode, &target_st->st_mode);
+    __put_user(host_st->st_nlink, &target_st->st_nlink);
+    __put_user(host_st->st_uid, &target_st->st_uid);
+    __put_user(host_st->st_gid, &target_st->st_gid);
+    __put_user(host_st->st_rdev, &target_st->st_rdev);
+    __put_user(host_st->st_atim.tv_sec, &target_st->st_atim.tv_sec);
+    __put_user(host_st->st_atim.tv_nsec, &target_st->st_atim.tv_nsec);
+    __put_user(host_st->st_mtim.tv_sec, &target_st->st_mtim.tv_sec);
+    __put_user(host_st->st_mtim.tv_nsec, &target_st->st_mtim.tv_nsec);
+    __put_user(host_st->st_ctim.tv_sec, &target_st->st_ctim.tv_sec);
+    __put_user(host_st->st_ctim.tv_nsec, &target_st->st_ctim.tv_nsec);
+    __put_user(host_st->st_size, &target_st->st_size);
+    __put_user(host_st->st_blocks, &target_st->st_blocks);
+    __put_user(host_st->st_blksize, &target_st->st_blksize);
+    __put_user(host_st->st_flags, &target_st->st_flags);
+    __put_user(host_st->st_gen, &target_st->st_gen);
+    __put_user(host_st->st_birthtim.tv_sec, &target_st->st_birthtim.tv_sec);
+    __put_user(host_st->st_birthtim.tv_nsec, &target_st->st_birthtim.tv_nsec);
+    unlock_user_struct(target_st, target_addr, 1);
+
+    return 0;
+}
+
Signature · 1 line
2.40.0
Reviewed-by on v1 13/33
#40 lore raw

Re: [PATCH 13/33] Implement host-target convertion functions

Quoted 6 lines · Karim Taha

From: Stacey Son<[email protected]>

Implement the stat converstion functions:
h2t_freebsd11_stat
h2t_freebsd_nstat

Quoted 6 lines
---
  bsd-user/freebsd/os-stat.c | 94 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 94 insertions(+)

create mode 100644 bsd-user/freebsd/os-stat.c

r~

#41 lore raw

Re: [PATCH 13/33] Implement host-target convertion functions

In reply to Karim Taha
Quoted 5 lines · Karim Taha

From: Stacey Son <[email protected]>

Implement the stat converstion functions:
h2t_freebsd11_stat
h2t_freebsd_nstat

"conversion"

And copy the functions into the subject, so that we don't wind up with 4 commits with identical subject.

r~

author
#42 lore raw

PATCH 14/33 Implement host-target convertion functions

In reply to Karim Taha

From: Stacey Son <[email protected]>

Implement the stat converstion functions:
h2t_freebsd_fhandle
t2h_freebsd_fhandle

---
 bsd-user/freebsd/os-stat.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
bsd-user/freebsd/os-stat.c +37 -0
index 8c73f7402c..6716cee3e2 100644
@@ -92,3 +92,40 @@ abi_long h2t_freebsd11_nstat(abi_ulong target_addr,
     return 0;
 }
 
+/*
+ * file handle conversion
+ */
+abi_long t2h_freebsd_fhandle(fhandle_t *host_fh, abi_ulong target_addr)
+{
+    target_freebsd_fhandle_t *target_fh;
+
+    if (!lock_user_struct(VERIFY_READ, target_fh, target_addr, 1)) {
+        return -TARGET_EFAULT;
+    }
+    __get_user(host_fh->fh_fsid.val[0], &target_fh->fh_fsid.val[0]);
+    __get_user(host_fh->fh_fsid.val[1], &target_fh->fh_fsid.val[0]);
+    __get_user(host_fh->fh_fid.fid_len, &target_fh->fh_fid.fid_len);
+    /* u_short         fid_data0; */
+    memcpy(host_fh->fh_fid.fid_data, target_fh->fh_fid.fid_data,
+        TARGET_MAXFIDSZ);
+    unlock_user_struct(target_fh, target_addr, 0);
+    return 0;
+}
+
+abi_long h2t_freebsd_fhandle(abi_ulong target_addr, fhandle_t *host_fh)
+{
+    target_freebsd_fhandle_t *target_fh;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_fh, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    __put_user(host_fh->fh_fsid.val[0], &target_fh->fh_fsid.val[0]);
+    __put_user(host_fh->fh_fsid.val[1], &target_fh->fh_fsid.val[0]);
+    __put_user(host_fh->fh_fid.fid_len, &target_fh->fh_fid.fid_len);
+    /* u_short         fid_data0; */
+    memcpy(target_fh->fh_fid.fid_data, host_fh->fh_fid.fid_data,
+            TARGET_MAXFIDSZ);
+    unlock_user_struct(target_fh, target_addr, 1);
+    return 0;
+}
+
Signature · 1 line
2.40.0
Reviewed-by on v1 14/33
#43 lore raw

Re: [PATCH 14/33] Implement host-target convertion functions

Quoted 11 lines · Karim Taha

From: Stacey Son<[email protected]>

Implement the stat converstion functions:
h2t_freebsd_fhandle
t2h_freebsd_fhandle

---
  bsd-user/freebsd/os-stat.c | 37 +++++++++++++++++++++++++++++++++++++
  1 file changed, 37 insertions(+)

Same typo and changes to subject. Otherwise,

r~

author
#44 lore raw

PATCH 15/33 Implement host-target convertion functions

In reply to Karim Taha

From: Stacey Son <[email protected]>

Implement the stat converstion functions:
h2t_freebds11_statfs

---
 bsd-user/freebsd/os-stat.c | 41 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
bsd-user/freebsd/os-stat.c +41 -0
index 6716cee3e2..9eb01bf664 100644
@@ -129,3 +129,44 @@ abi_long h2t_freebsd_fhandle(abi_ulong target_addr, fhandle_t *host_fh)
     return 0;
 }
 
+/*
+ *  file system stat
+ */
+abi_long h2t_freebsd11_statfs(abi_ulong target_addr,
+        struct freebsd11_statfs *host_statfs)
+{
+    struct target_freebsd11_statfs *target_statfs;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_statfs, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    __put_user(host_statfs->f_version, &target_statfs->f_version);
+    __put_user(host_statfs->f_type, &target_statfs->f_type);
+    __put_user(host_statfs->f_flags, &target_statfs->f_flags);
+    __put_user(host_statfs->f_bsize, &target_statfs->f_bsize);
+    __put_user(host_statfs->f_iosize, &target_statfs->f_iosize);
+    __put_user(host_statfs->f_blocks, &target_statfs->f_blocks);
+    __put_user(host_statfs->f_bfree, &target_statfs->f_bfree);
+    __put_user(host_statfs->f_bavail, &target_statfs->f_bavail);
+    __put_user(host_statfs->f_files, &target_statfs->f_files);
+    __put_user(host_statfs->f_ffree, &target_statfs->f_ffree);
+    __put_user(host_statfs->f_syncwrites, &target_statfs->f_syncwrites);
+    __put_user(host_statfs->f_asyncwrites, &target_statfs->f_asyncwrites);
+    __put_user(host_statfs->f_syncreads, &target_statfs->f_syncreads);
+    __put_user(host_statfs->f_asyncreads, &target_statfs->f_asyncreads);
+    /* uint64_t f_spare[10]; */
+    __put_user(host_statfs->f_namemax, &target_statfs->f_namemax);
+    __put_user(host_statfs->f_owner, &target_statfs->f_owner);
+    __put_user(host_statfs->f_fsid.val[0], &target_statfs->f_fsid.val[0]);
+    __put_user(host_statfs->f_fsid.val[1], &target_statfs->f_fsid.val[1]);
+    /* char f_charspace[80]; */
+    strncpy(target_statfs->f_fstypename, host_statfs->f_fstypename,
+        sizeof(target_statfs->f_fstypename));
+    strncpy(target_statfs->f_mntfromname, host_statfs->f_mntfromname,
+        sizeof(target_statfs->f_mntfromname));
+    strncpy(target_statfs->f_mntonname, host_statfs->f_mntonname,
+        sizeof(target_statfs->f_mntonname));
+    unlock_user_struct(target_statfs, target_addr, 1);
+    return 0;
+}
+
Signature · 1 line
2.40.0
author
#46 lore raw

PATCH 16/33 Implement host-target convertion functions

In reply to Karim Taha

From: Stacey Son <[email protected]>

Implement the stat converstion functions:
target_to_host_fcntl_cmd

---
 bsd-user/freebsd/os-stat.c | 71 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)
bsd-user/freebsd/os-stat.c +71 -0
index 9eb01bf664..f5b4ded8bb 100644
@@ -170,3 +170,74 @@ abi_long h2t_freebsd11_statfs(abi_ulong target_addr,
     return 0;
 }
 
+/*
+ * fcntl cmd conversion
+ */
+abi_long target_to_host_fcntl_cmd(int cmd)
+{
+
+    switch (cmd) {
+    case TARGET_F_DUPFD:
+        return F_DUPFD;
+
+    case TARGET_F_DUP2FD:
+        return F_DUP2FD;
+
+    case TARGET_F_GETFD:
+        return F_GETFD;
+
+    case TARGET_F_SETFD:
+        return F_SETFD;
+
+    case TARGET_F_GETFL:
+        return F_GETFL;
+
+    case TARGET_F_SETFL:
+        return F_SETFL;
+
+    case TARGET_F_GETOWN:
+        return F_GETOWN;
+
+    case TARGET_F_SETOWN:
+        return F_SETOWN;
+
+    case TARGET_F_GETLK:
+        return F_GETLK;
+
+    case TARGET_F_SETLK:
+        return F_SETLK;
+
+    case TARGET_F_SETLKW:
+        return F_SETLKW;
+
+    case TARGET_F_READAHEAD:
+        return F_READAHEAD;
+
+    case TARGET_F_RDAHEAD:
+        return F_RDAHEAD;
+
+#ifdef F_DUPFD_CLOEXEC
+    case TARGET_F_DUPFD_CLOEXEC:
+        return F_DUPFD_CLOEXEC;
+#endif
+
+#ifdef F_DUP2FD_CLOEXEC
+    case TARGET_F_DUP2FD_CLOEXEC:
+        return F_DUP2FD_CLOEXEC;
+#endif
+
+#ifdef F_ADD_SEALS
+    case TARGET_F_ADD_SEALS:
+        return F_ADD_SEALS;
+#endif
+
+#ifdef F_GET_SEALS
+    case TARGET_F_GET_SEALS:
+        return F_GET_SEALS;
+#endif
+
+    default:
+        return -TARGET_EINVAL;
+    }
+}
+
Signature · 1 line
2.40.0
#47 lore raw

Re: [PATCH 16/33] Implement host-target convertion functions

Quoted 10 lines · Karim Taha

From: Stacey Son <[email protected]>

Implement the stat converstion functions:
target_to_host_fcntl_cmd

---
  bsd-user/freebsd/os-stat.c | 71 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 71 insertions(+)

Which host / guest pairs have varying fcntl constants? I thought freebsd had these standardized...

r~

#48 lore raw

Re: [PATCH 16/33] Implement host-target convertion functions

Quoted 6 lines · Richard Henderson
Quoted 5 lines · Karim Taha

From: Stacey Son <[email protected]>

Implement the stat converstion functions:
target_to_host_fcntl_cmd

Quoted 9 lines
Quoted 5 lines
---
  bsd-user/freebsd/os-stat.c | 71 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 71 insertions(+)

Which host / guest pairs have varying fcntl constants? I thought freebsd had these standardized...

Ah, indeed. This can be an identity wrapper, since the only #ifdefs for these
values are for visibility. So this whole function can be replaced by

abi_long target_to_host_fcntl_cmd(int cmd)

{
    return cmd;
}

Warner

author
#49 lore raw

PATCH 17/33 Implement h2t_freebsd_stat and h2t_freebsd_statfs functions

In reply to Karim Taha

From: Michal Meloun <[email protected]>

They are the 64-bit variants of h2t_freebsd11_stat and h2t_freebsd11_statfs, respectively

---
 bsd-user/freebsd/os-stat.c | 82 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
bsd-user/freebsd/os-stat.c +82 -0
index f5b4ded8bb..57b5368331 100644
@@ -58,6 +58,50 @@ abi_long h2t_freebsd11_stat(abi_ulong target_addr,
     return 0;
 }
 
+abi_long h2t_freebsd_stat(abi_ulong target_addr,
+        struct stat *host_st)
+{
+    struct target_stat *target_st;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_st, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    memset(target_st, 0, sizeof(*target_st));
+    __put_user(host_st->st_dev, &target_st->st_dev);
+    __put_user(host_st->st_ino, &target_st->st_ino);
+    __put_user(host_st->st_nlink, &target_st->st_nlink);
+    __put_user(host_st->st_mode, &target_st->st_mode);
+    __put_user(host_st->st_uid, &target_st->st_uid);
+    __put_user(host_st->st_gid, &target_st->st_gid);
+    __put_user(host_st->st_rdev, &target_st->st_rdev);
+    __put_user(host_st->st_atim.tv_sec, &target_st->st_atim.tv_sec);
+    __put_user(host_st->st_atim.tv_nsec, &target_st->st_atim.tv_nsec);
+#ifdef TARGET_HAS_STAT_TIME_T_EXT
+/*    __put_user(host_st->st_mtim_ext, &target_st->st_mtim_ext); XXX */
+#endif
+    __put_user(host_st->st_mtim.tv_sec, &target_st->st_mtim.tv_sec);
+    __put_user(host_st->st_mtim.tv_nsec, &target_st->st_mtim.tv_nsec);
+#ifdef TARGET_HAS_STAT_TIME_T_EXT
+/*    __put_user(host_st->st_ctim_ext, &target_st->st_ctim_ext); XXX */
+#endif
+    __put_user(host_st->st_ctim.tv_sec, &target_st->st_ctim.tv_sec);
+    __put_user(host_st->st_ctim.tv_nsec, &target_st->st_ctim.tv_nsec);
+#ifdef TARGET_HAS_STAT_TIME_T_EXT
+/*    __put_user(host_st->st_birthtim_ext, &target_st->st_birthtim_ext); XXX */
+#endif
+    __put_user(host_st->st_birthtim.tv_sec, &target_st->st_birthtim.tv_sec);
+    __put_user(host_st->st_birthtim.tv_nsec, &target_st->st_birthtim.tv_nsec);
+
+    __put_user(host_st->st_size, &target_st->st_size);
+    __put_user(host_st->st_blocks, &target_st->st_blocks);
+    __put_user(host_st->st_blksize, &target_st->st_blksize);
+    __put_user(host_st->st_flags, &target_st->st_flags);
+    __put_user(host_st->st_gen, &target_st->st_gen);
+    unlock_user_struct(target_st, target_addr, 1);
+
+    return 0;
+}
+
 abi_long h2t_freebsd11_nstat(abi_ulong target_addr,
         struct freebsd11_stat *host_st)
 {
@@ -170,6 +214,44 @@ abi_long h2t_freebsd11_statfs(abi_ulong target_addr,
     return 0;
 }
 
+abi_long h2t_freebsd_statfs(abi_ulong target_addr,
+        struct statfs *host_statfs)
+{
+    struct target_statfs *target_statfs;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_statfs, target_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    __put_user(host_statfs->f_version, &target_statfs->f_version);
+    __put_user(host_statfs->f_type, &target_statfs->f_type);
+    __put_user(host_statfs->f_flags, &target_statfs->f_flags);
+    __put_user(host_statfs->f_bsize, &target_statfs->f_bsize);
+    __put_user(host_statfs->f_iosize, &target_statfs->f_iosize);
+    __put_user(host_statfs->f_blocks, &target_statfs->f_blocks);
+    __put_user(host_statfs->f_bfree, &target_statfs->f_bfree);
+    __put_user(host_statfs->f_bavail, &target_statfs->f_bavail);
+    __put_user(host_statfs->f_files, &target_statfs->f_files);
+    __put_user(host_statfs->f_ffree, &target_statfs->f_ffree);
+    __put_user(host_statfs->f_syncwrites, &target_statfs->f_syncwrites);
+    __put_user(host_statfs->f_asyncwrites, &target_statfs->f_asyncwrites);
+    __put_user(host_statfs->f_syncreads, &target_statfs->f_syncreads);
+    __put_user(host_statfs->f_asyncreads, &target_statfs->f_asyncreads);
+    /* uint64_t f_spare[10]; */
+    __put_user(host_statfs->f_namemax, &target_statfs->f_namemax);
+    __put_user(host_statfs->f_owner, &target_statfs->f_owner);
+    __put_user(host_statfs->f_fsid.val[0], &target_statfs->f_fsid.val[0]);
+    __put_user(host_statfs->f_fsid.val[1], &target_statfs->f_fsid.val[1]);
+    /* char f_charspace[80]; */
+    strncpy(target_statfs->f_fstypename, host_statfs->f_fstypename,
+        sizeof(target_statfs->f_fstypename));
+    strncpy(target_statfs->f_mntfromname, host_statfs->f_mntfromname,
+        sizeof(target_statfs->f_mntfromname));
+    strncpy(target_statfs->f_mntonname, host_statfs->f_mntonname,
+        sizeof(target_statfs->f_mntonname));
+    unlock_user_struct(target_statfs, target_addr, 1);
+    return 0;
+}
+
 /*
  * fcntl cmd conversion
  */
Signature · 1 line
2.40.0
Reviewed-by on v1 17/33
#50 lore raw

Re: [PATCH 17/33] Implement h2t_freebsd_stat and h2t_freebsd_statfs functions

Quoted 10 lines · Karim Taha

From: Michal Meloun<[email protected]>

They are the 64-bit variants of h2t_freebsd11_stat and h2t_freebsd11_statfs, respectively

---
  bsd-user/freebsd/os-stat.c | 82 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 82 insertions(+)

r~

author
#51 lore raw

PATCH 18/33 Implement stat related syscalls

In reply to Karim Taha

From: Stacey Son <[email protected]>

Implement the following syscalls:
stat(2)
lstat(2)
fstat(2)
fstatat(2)
nstat
nfstat
nlstat

---
 bsd-user/freebsd/os-stat.h | 130 +++++++++++++++++++++++++++++++++++++
 1 file changed, 130 insertions(+)
 create mode 100644 bsd-user/freebsd/os-stat.h
bsd-user/freebsd/os-stat.h new file +130 -0
new file mode 100644
index 0000000000..f8f99b4a72
@@ -0,0 +1,130 @@
+/*
+ *  stat related system call shims and definitions
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef BSD_USER_FREEBSD_OS_STAT_H
+#define BSD_USER_FREEBSD_OS_STAT_H
+
+/* stat(2) */
+static inline abi_long do_freebsd11_stat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;
+    struct freebsd11_stat st;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(freebsd11_stat(path(p), &st));
+    UNLOCK_PATH(p, arg1);
+    if (!is_error(ret)) {
+        ret = h2t_freebsd11_stat(arg2, &st);
+    }
+    return ret;
+}
+
+/* lstat(2) */
+static inline abi_long do_freebsd11_lstat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;
+    struct freebsd11_stat st;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(freebsd11_lstat(path(p), &st));
+    UNLOCK_PATH(p, arg1);
+    if (!is_error(ret)) {
+        ret = h2t_freebsd11_stat(arg2, &st);
+    }
+    return ret;
+}
+
+/* fstat(2) */
+static inline abi_long do_freebsd_fstat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    struct stat st;
+
+    ret = get_errno(fstat(arg1, &st));
+    if (!is_error(ret))  {
+        ret = h2t_freebsd_stat(arg2, &st);
+    }
+    return ret;
+}
+
+/* fstatat(2) */
+static inline abi_long do_freebsd_fstatat(abi_long arg1, abi_long arg2,
+        abi_long arg3, abi_long arg4)
+{
+    abi_long ret;
+    void *p;
+    struct stat st;
+
+    LOCK_PATH(p, arg2);
+    ret = get_errno(fstatat(arg1, p, &st, arg4));
+    UNLOCK_PATH(p, arg2);
+    if (!is_error(ret) && arg3) {
+        ret = h2t_freebsd_stat(arg3, &st);
+    }
+    return ret;
+}
+
+/* undocummented nstat(char *path, struct nstat *ub) syscall */
+static abi_long do_freebsd11_nstat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;
+    struct freebsd11_stat st;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(freebsd11_nstat(path(p), &st));
+    UNLOCK_PATH(p, arg1);
+    if (!is_error(ret)) {
+        ret = h2t_freebsd11_nstat(arg2, &st);
+    }
+    return ret;
+}
+
+/* undocummented nfstat(int fd, struct nstat *sb) syscall */
+static abi_long do_freebsd11_nfstat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    struct freebsd11_stat st;
+
+    ret = get_errno(freebsd11_nfstat(arg1, &st));
+    if (!is_error(ret))  {
+        ret = h2t_freebsd11_nstat(arg2, &st);
+    }
+    return ret;
+}
+
+/* undocummented nlstat(char *path, struct nstat *ub) syscall */
+static abi_long do_freebsd11_nlstat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;
+    struct freebsd11_stat st;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(freebsd11_nlstat(path(p), &st));
+    UNLOCK_PATH(p, arg1);
+    if (!is_error(ret)) {
+        ret = h2t_freebsd11_nstat(arg2, &st);
+    }
+    return ret;
+}
+
+#endif /* BSD_USER_FREEBSD_OS_STAT_H */
Signature · 1 line
2.40.0
#52 lore raw

Re: [PATCH 18/33] Implement stat related syscalls

Quoted 11 lines · Karim Taha

From: Stacey Son <[email protected]>

Implement the following syscalls:
stat(2)
lstat(2)
fstat(2)
fstatat(2)
nstat
nfstat
nlstat

Quoted 2 lines

Why are all of these in os-stat.h instead of os-stat.c?
Is this attempting to avoid clang's warning for unused static inline function in a c file?

Quoted 5 lines
bsd-user/freebsd/os-stat.h new file +5 -0
+/* stat(2) */
+static inline abi_long do_freebsd11_stat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;

bsd-user/freebsd/os-stat.h line 37

Quoted 10 lines
bsd-user/freebsd/os-stat.h new file +10 -0
+    struct freebsd11_stat st;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(freebsd11_stat(path(p), &st));
+    UNLOCK_PATH(p, arg1);
+    if (!is_error(ret)) {
+        ret = h2t_freebsd11_stat(arg2, &st);
+    }
+    return ret;
+}

The patch ordering is poor, because freebsd11_stat is used here but not introduced until patch 23, and do_freebsd11_stat itself is not used until patch 30.

And yet you delay compilation of os-stat.c until patch 29. Patch 29 is either too early or too late, depending on the viewpoint.

If os-stat.c compilation was enabled earlier, it would require you to order all of the patches such that os-stat.c will always compile.

If os-stat.c compilation was enabled later (indeed last), you would not need to mark this function 'static inline' in order to avoid unused function warnings prior to their use in patches 30-33.

I prefer the ordering in which os-stat.c always compiles. This probably requires patches 23-27 be ordered first, and patches 30-33 be merged with patches 18-22. There is no need for *any* of these functions to be marked inline -- leave that choice to the compiler.

r~

#53 lore raw

Re: [PATCH 18/33] Implement stat related syscalls

Quoted 12 lines · Richard Henderson
Quoted 11 lines · Karim Taha

From: Stacey Son <[email protected]>

Implement the following syscalls:
stat(2)
lstat(2)
fstat(2)
fstatat(2)
nstat
nfstat
nlstat

Quoted 7 lines
Quoted 2 lines

Why are all of these in os-stat.h instead of os-stat.c? Is this attempting to avoid clang's warning for unused static inline function in a c file?

I asked Stacey about this ages ago (why some things are like this) and he'd indicated it was for better optimization (inlining) in do_freebsd_syscall(). Other
inlines are in the .h files so that we can define a common set, and include that
file and have netbsd/openbsd/freebsd pick and choose which ones to use without
the warnings. The whole thing is a big mess right now, to be honest, but I've tried
to avoid making structural changes in this area since I didn't want to introduce bugs
and we have been chasing some weird corruption bug with threads for ages (though
it might be related to the vm address space use bug that you fixed recently, I've not
run things to see if that fixed it or not).

So, that's kinda why. I do agree with you that that would be a better structure, but
can we use this structure for upstreaming and once we get the other issues worked
out, we can do a restructure... We've moved things around a bit, and I'm also waiting
for the NetBSD folks that contacted me to finish their efforts before I pull the rug out
from them (or they timeout, which isn't quite yet).

Warner

Quoted 44 lines
Quoted 5 lines
bsd-user/freebsd/os-stat.h new file +5 -0
+/* stat(2) */
+static inline abi_long do_freebsd11_stat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;

bsd-user/freebsd/os-stat.h line 37

Quoted 10 lines
bsd-user/freebsd/os-stat.h new file +10 -0
+    struct freebsd11_stat st;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(freebsd11_stat(path(p), &st));
+    UNLOCK_PATH(p, arg1);
+    if (!is_error(ret)) {
+        ret = h2t_freebsd11_stat(arg2, &st);
+    }
+    return ret;
+}

The patch ordering is poor, because freebsd11_stat is used here but not introduced until
patch 23, and do_freebsd11_stat itself is not used until patch 30.

And yet you delay compilation of os-stat.c until patch 29. Patch 29 is either too early
or too late, depending on the viewpoint.

If os-stat.c compilation was enabled earlier, it would require you to order all of the
patches such that os-stat.c will always compile.

If os-stat.c compilation was enabled later (indeed last), you would not need to mark this
function 'static inline' in order to avoid unused function warnings prior to their use in
patches 30-33.

I prefer the ordering in which os-stat.c always compiles. This probably requires patches
23-27 be ordered first, and patches 30-33 be merged with patches 18-22. There is no need
for *any* of these functions to be marked inline -- leave that choice to the compiler.

r~

Reviewed-by
#54 lore raw

Re: [PATCH 18/33] Implement stat related syscalls

Quoted 5 lines · Warner Losh

So, that's kinda why. I do agree with you that that would be a better structure, but can we use this structure for upstreaming and once we get the other issues worked out, we can do a restructure... We've moved things around a bit, and I'm also waiting for the NetBSD folks that contacted me to finish their efforts before I pull the rug out from them (or they timeout, which isn't quite yet).

Yes, that's fine. I understand the pain.

In which case have a

for patches 18-33. My only quibble with all of them related to this.

r~

author
#55 lore raw

PATCH 19/33 Implement stat related syscalls

In reply to Karim Taha

From: Stacey Son <[email protected]>

Implement the following syscalls:
getfh(2)
lgetfh(2)
fhopen(2)
fhstat(2)
fhstatfs(2)

---
 bsd-user/freebsd/os-stat.h | 83 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
bsd-user/freebsd/os-stat.h +83 -0
index f8f99b4a72..935663c071 100644
@@ -127,4 +127,87 @@ static abi_long do_freebsd11_nlstat(abi_long arg1, abi_long arg2)
     return ret;
 }
 
+/* getfh(2) */
+static abi_long do_freebsd_getfh(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;
+    fhandle_t host_fh;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(getfh(path(p), &host_fh));
+    UNLOCK_PATH(p, arg1);
+    if (is_error(ret)) {
+        return ret;
+    }
+    return h2t_freebsd_fhandle(arg2, &host_fh);
+}
+
+/* lgetfh(2) */
+static inline abi_long do_freebsd_lgetfh(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;
+    fhandle_t host_fh;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(lgetfh(path(p), &host_fh));
+    UNLOCK_PATH(p, arg1);
+    if (is_error(ret)) {
+        return ret;
+    }
+    return h2t_freebsd_fhandle(arg2, &host_fh);
+}
+
+/* fhopen(2) */
+static inline abi_long do_freebsd_fhopen(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    fhandle_t host_fh;
+
+    ret = t2h_freebsd_fhandle(&host_fh, arg1);
+    if (is_error(ret)) {
+        return ret;
+    }
+
+    return get_errno(fhopen(&host_fh, arg2));
+}
+
+/* fhstat(2) */
+static inline abi_long do_freebsd_fhstat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    fhandle_t host_fh;
+    struct stat host_sb;
+
+    ret = t2h_freebsd_fhandle(&host_fh, arg1);
+    if (is_error(ret)) {
+        return ret;
+    }
+    ret = get_errno(fhstat(&host_fh, &host_sb));
+    if (is_error(ret)) {
+        return ret;
+    }
+    return h2t_freebsd_stat(arg2, &host_sb);
+}
+
+/* fhstatfs(2) */
+static inline abi_long do_freebsd_fhstatfs(abi_ulong target_fhp_addr,
+        abi_ulong target_stfs_addr)
+{
+    abi_long ret;
+    fhandle_t host_fh;
+    struct statfs host_stfs;
+
+    ret = t2h_freebsd_fhandle(&host_fh, target_fhp_addr);
+    if (is_error(ret)) {
+        return ret;
+    }
+    ret = get_errno(fhstatfs(&host_fh, &host_stfs));
+    if (is_error(ret)) {
+        return ret;
+    }
+    return h2t_freebsd_statfs(target_stfs_addr, &host_stfs);
+}
+
 #endif /* BSD_USER_FREEBSD_OS_STAT_H */
Signature · 1 line
2.40.0
author
#56 lore raw

PATCH 20/33 Implement stat related syscalls

In reply to Karim Taha

From: Stacey Son <[email protected]>

Implement the following syscalls:
statfs(2)
fstatfs(2)
getfsstat(2)

---
 bsd-user/freebsd/os-stat.h | 69 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
bsd-user/freebsd/os-stat.h +69 -0
index 935663c071..9492c93c55 100644
@@ -210,4 +210,73 @@ static inline abi_long do_freebsd_fhstatfs(abi_ulong target_fhp_addr,
     return h2t_freebsd_statfs(target_stfs_addr, &host_stfs);
 }
 
+/* statfs(2) */
+static inline abi_long do_freebsd_statfs(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;
+    struct statfs host_stfs;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(statfs(path(p), &host_stfs));
+    UNLOCK_PATH(p, arg1);
+    if (is_error(ret)) {
+        return ret;
+    }
+
+    return h2t_freebsd_statfs(arg2, &host_stfs);
+}
+
+/* fstatfs(2) */
+static inline abi_long do_freebsd_fstatfs(abi_long fd, abi_ulong target_addr)
+{
+    abi_long ret;
+    struct statfs host_stfs;
+
+    ret = get_errno(fstatfs(fd, &host_stfs));
+    if (is_error(ret)) {
+        return ret;
+    }
+
+    return h2t_freebsd_statfs(target_addr, &host_stfs);
+}
+
+/* getfsstat(2) */
+static inline abi_long do_freebsd_getfsstat(abi_ulong target_addr,
+        abi_long bufsize, abi_long flags)
+{
+    abi_long ret;
+    struct statfs *host_stfs;
+    int count;
+    long host_bufsize;
+
+    count = bufsize / sizeof(struct target_statfs);
+
+    /* if user buffer is NULL then return number of mounted FS's */
+    if (target_addr == 0 || count == 0) {
+        return get_errno(freebsd11_getfsstat(NULL, 0, flags));
+    }
+
+    /* XXX check count to be reasonable */
+    host_bufsize = sizeof(struct statfs) * count;
+    host_stfs = alloca(host_bufsize);
+    if (!host_stfs) {
+        return -TARGET_EINVAL;
+    }
+
+    ret = count = get_errno(getfsstat(host_stfs, host_bufsize, flags));
+    if (is_error(ret)) {
+        return ret;
+    }
+
+    while (count--) {
+        if (h2t_freebsd_statfs((target_addr +
+                        (count * sizeof(struct target_statfs))),
+                    &host_stfs[count])) {
+            return -TARGET_EFAULT;
+        }
+    }
+    return ret;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_STAT_H */
Signature · 1 line
2.40.0
author
#57 lore raw

PATCH 21/33 Implement stat related syscalls

In reply to Karim Taha

From: Stacey Son <[email protected]>

Implement the following syscalls:
getdents(2)
getdirecentries(2)

---
 bsd-user/freebsd/os-stat.h | 72 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
bsd-user/freebsd/os-stat.h +72 -0
index 9492c93c55..7dc41cd0bf 100644
@@ -279,4 +279,76 @@ static inline abi_long do_freebsd_getfsstat(abi_ulong target_addr,
     return ret;
 }
 
+/* getdents(2) */
+static inline abi_long do_freebsd11_getdents(abi_long arg1,
+        abi_ulong arg2, abi_long nbytes)
+{
+    abi_long ret;
+    struct freebsd11_dirent *dirp;
+
+    dirp = lock_user(VERIFY_WRITE, arg2, nbytes, 0);
+    if (dirp == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(freebsd11_getdents(arg1, (char *)dirp, nbytes));
+    if (!is_error(ret)) {
+        struct freebsd11_dirent *de;
+        int len = ret;
+        int reclen;
+
+        de = dirp;
+        while (len > 0) {
+            reclen = de->d_reclen;
+            if (reclen > len) {
+                return -TARGET_EFAULT;
+            }
+            de->d_reclen = tswap16(reclen);
+            de->d_fileno = tswap32(de->d_fileno);
+            len -= reclen;
+        }
+    }
+    return ret;
+}
+
+/* getdirecentries(2) */
+static inline abi_long do_freebsd_getdirentries(abi_long arg1,
+        abi_ulong arg2, abi_long nbytes, abi_ulong arg4)
+{
+    abi_long ret;
+    struct dirent *dirp;
+    long basep;
+
+    dirp = lock_user(VERIFY_WRITE, arg2, nbytes, 0);
+    if (dirp == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(getdirentries(arg1, (char *)dirp, nbytes, &basep));
+    if (!is_error(ret)) {
+        struct dirent *de;
+        int len = ret;
+        int reclen;
+
+        de = dirp;
+        while (len > 0) {
+            reclen = de->d_reclen;
+            if (reclen > len) {
+                return -TARGET_EFAULT;
+            }
+            de->d_fileno = tswap64(de->d_fileno);
+            de->d_off = tswap64(de->d_off);
+            de->d_reclen = tswap16(de->d_reclen);
+            de->d_namlen = tswap16(de->d_namlen);
+            len -= reclen;
+            de = (struct dirent *)((void *)de + reclen);
+        }
+    }
+    unlock_user(dirp, arg2, ret);
+    if (arg4) {
+        if (put_user(basep, arg4, abi_ulong)) {
+            return -TARGET_EFAULT;
+        }
+    }
+    return ret;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_STAT_H */
Signature · 1 line
2.40.0
author
#58 lore raw

PATCH 22/33 Implement stat related syscalls

In reply to Karim Taha

From: Stacey Son <[email protected]>

Implement the following syscalls:
fcntl(2)

---
 bsd-user/freebsd/os-stat.h | 74 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
bsd-user/freebsd/os-stat.h +74 -0
index 7dc41cd0bf..5d9323c7d1 100644
@@ -351,4 +351,78 @@ static inline abi_long do_freebsd_getdirentries(abi_long arg1,
     return ret;
 }
 
+/* fcntl(2) */
+static inline abi_long do_freebsd_fcntl(abi_long arg1, abi_long arg2,
+        abi_ulong arg3)
+{
+    abi_long ret;
+    int host_cmd;
+    struct flock fl;
+    struct target_freebsd_flock *target_fl;
+
+    host_cmd = target_to_host_fcntl_cmd(arg2);
+    if (host_cmd < 0) {
+        return host_cmd;
+    }
+    switch (arg2) {
+    case TARGET_F_GETLK:
+        if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1)) {
+            return -TARGET_EFAULT;
+        }
+        __get_user(fl.l_type, &target_fl->l_type);
+        __get_user(fl.l_whence, &target_fl->l_whence);
+        __get_user(fl.l_start, &target_fl->l_start);
+        __get_user(fl.l_len, &target_fl->l_len);
+        __get_user(fl.l_pid, &target_fl->l_pid);
+        __get_user(fl.l_sysid, &target_fl->l_sysid);
+        unlock_user_struct(target_fl, arg3, 0);
+        ret = get_errno(safe_fcntl(arg1, host_cmd, &fl));
+        if (!is_error(ret)) {
+            if (!lock_user_struct(VERIFY_WRITE, target_fl, arg3, 0)) {
+                return -TARGET_EFAULT;
+            }
+            __put_user(fl.l_type, &target_fl->l_type);
+            __put_user(fl.l_whence, &target_fl->l_whence);
+            __put_user(fl.l_start, &target_fl->l_start);
+            __put_user(fl.l_len, &target_fl->l_len);
+            __put_user(fl.l_pid, &target_fl->l_pid);
+            __put_user(fl.l_sysid, &target_fl->l_sysid);
+            unlock_user_struct(target_fl, arg3, 1);
+        }
+        break;
+
+    case TARGET_F_SETLK:
+    case TARGET_F_SETLKW:
+        if (!lock_user_struct(VERIFY_READ, target_fl, arg3, 1)) {
+            return -TARGET_EFAULT;
+        }
+        __get_user(fl.l_type, &target_fl->l_type);
+        __get_user(fl.l_whence, &target_fl->l_whence);
+        __get_user(fl.l_start, &target_fl->l_start);
+        __get_user(fl.l_len, &target_fl->l_len);
+        __get_user(fl.l_pid, &target_fl->l_pid);
+        __get_user(fl.l_sysid, &target_fl->l_sysid);
+        unlock_user_struct(target_fl, arg3, 0);
+        ret = get_errno(safe_fcntl(arg1, host_cmd, &fl));
+        break;
+
+    case TARGET_F_DUPFD:
+    case TARGET_F_DUP2FD:
+    case TARGET_F_GETOWN:
+    case TARGET_F_SETOWN:
+    case TARGET_F_GETFD:
+    case TARGET_F_SETFD:
+    case TARGET_F_GETFL:
+    case TARGET_F_SETFL:
+    case TARGET_F_READAHEAD:
+    case TARGET_F_RDAHEAD:
+    case TARGET_F_ADD_SEALS:
+    case TARGET_F_GET_SEALS:
+    default:
+        ret = get_errno(safe_fcntl(arg1, host_cmd, arg3));
+        break;
+    }
+    return ret;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_STAT_H */
Signature · 1 line
2.40.0
author
#59 lore raw

PATCH 23/33 Implement freebsd11 stat related syscalls

In reply to Karim Taha

From: Michal Meloun <[email protected]>

Rename the following syscalls to the freebsd11 variant:
do_freebsd_lstat -> do_freebsd11_lstat do_freebsd_stat -> do_freebsd11_stat

---
 bsd-user/freebsd/os-stat.h | 5 +++++
 1 file changed, 5 insertions(+)
bsd-user/freebsd/os-stat.h +5 -0
index 5d9323c7d1..aef55c8bb5 100644
@@ -20,6 +20,11 @@
 #ifndef BSD_USER_FREEBSD_OS_STAT_H
 #define BSD_USER_FREEBSD_OS_STAT_H
 
+int freebsd11_stat(const char *path, struct freebsd11_stat *stat);
+__sym_compat(stat, freebsd11_stat, FBSD_1.0);
+int freebsd11_lstat(const char *path, struct freebsd11_stat *stat);
+__sym_compat(lstat, freebsd11_lstat, FBSD_1.0);
+
 /* stat(2) */
 static inline abi_long do_freebsd11_stat(abi_long arg1, abi_long arg2)
 {
Signature · 1 line
2.40.0
author
#60 lore raw

PATCH 24/33 Implement freebsd11 stat related syscalls

In reply to Karim Taha

From: Michal Meloun <[email protected]>

Implement the freebsd11 variant of the following syscalls:
fstat(2)
fstatat(2)
fhstat(2)
fhstatfs(2)

---
 bsd-user/freebsd/os-stat.h | 78 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)
bsd-user/freebsd/os-stat.h +78 -0
index aef55c8bb5..2e0c7245df 100644
@@ -24,6 +24,17 @@ int freebsd11_stat(const char *path, struct freebsd11_stat *stat);
 __sym_compat(stat, freebsd11_stat, FBSD_1.0);
 int freebsd11_lstat(const char *path, struct freebsd11_stat *stat);
 __sym_compat(lstat, freebsd11_lstat, FBSD_1.0);
+int freebsd11_fstat(int fd, struct freebsd11_stat *stat);
+__sym_compat(fstat, freebsd11_fstat, FBSD_1.0);
+int freebsd11_fstatat(int fd, const char *path, struct freebsd11_stat *stat,
+        int flag);
+__sym_compat(fstatat, freebsd11_fstatat, FBSD_1.1);
+
+int freebsd11_fhstat(const fhandle_t *fhandle, struct freebsd11_stat *stat);
+__sym_compat(fhstat, freebsd11_fhstat, FBSD_1.0);
+int freebsd11_fhstatfs(const fhandle_t *fhandle, struct freebsd11_statfs * buf);
+__sym_compat(fhstatfs, freebsd11_fhstatfs, FBSD_1.0);
+int freebsd11_statfs(const char *path, struct freebsd11_statfs *buf);
 
 /* stat(2) */
 static inline abi_long do_freebsd11_stat(abi_long arg1, abi_long arg2)
@@ -57,6 +68,19 @@ static inline abi_long do_freebsd11_lstat(abi_long arg1, abi_long arg2)
     return ret;
 }
 
+/* fstat(2) */
+static inline abi_long do_freebsd11_fstat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    struct freebsd11_stat st;
+
+    ret = get_errno(freebsd11_fstat(arg1, &st));
+    if (!is_error(ret))  {
+        ret = h2t_freebsd11_stat(arg2, &st);
+    }
+    return ret;
+}
+
 /* fstat(2) */
 static inline abi_long do_freebsd_fstat(abi_long arg1, abi_long arg2)
 {
@@ -70,6 +94,23 @@ static inline abi_long do_freebsd_fstat(abi_long arg1, abi_long arg2)
     return ret;
 }
 
+/* fstatat(2) */
+static inline abi_long do_freebsd11_fstatat(abi_long arg1, abi_long arg2,
+        abi_long arg3, abi_long arg4)
+{
+    abi_long ret;
+    void *p;
+    struct freebsd11_stat st;
+
+    LOCK_PATH(p, arg2);
+    ret = get_errno(freebsd11_fstatat(arg1, p, &st, arg4));
+    UNLOCK_PATH(p, arg2);
+    if (!is_error(ret) && arg3) {
+        ret = h2t_freebsd11_stat(arg3, &st);
+    }
+    return ret;
+}
+
 /* fstatat(2) */
 static inline abi_long do_freebsd_fstatat(abi_long arg1, abi_long arg2,
         abi_long arg3, abi_long arg4)
@@ -178,6 +219,24 @@ static inline abi_long do_freebsd_fhopen(abi_long arg1, abi_long arg2)
     return get_errno(fhopen(&host_fh, arg2));
 }
 
+/* fhstat(2) */
+static inline abi_long do_freebsd11_fhstat(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    fhandle_t host_fh;
+    struct freebsd11_stat host_sb;
+
+    ret = t2h_freebsd_fhandle(&host_fh, arg1);
+    if (is_error(ret)) {
+        return ret;
+    }
+    ret = get_errno(freebsd11_fhstat(&host_fh, &host_sb));
+    if (is_error(ret)) {
+        return ret;
+    }
+    return h2t_freebsd11_stat(arg2, &host_sb);
+}
+
 /* fhstat(2) */
 static inline abi_long do_freebsd_fhstat(abi_long arg1, abi_long arg2)
 {
@@ -196,6 +255,25 @@ static inline abi_long do_freebsd_fhstat(abi_long arg1, abi_long arg2)
     return h2t_freebsd_stat(arg2, &host_sb);
 }
 
+/* fhstatfs(2) */
+static inline abi_long do_freebsd11_fhstatfs(abi_ulong target_fhp_addr,
+        abi_ulong target_stfs_addr)
+{
+    abi_long ret;
+    fhandle_t host_fh;
+    struct freebsd11_statfs host_stfs;
+
+    ret = t2h_freebsd_fhandle(&host_fh, target_fhp_addr);
+    if (is_error(ret)) {
+        return ret;
+    }
+    ret = get_errno(freebsd11_fhstatfs(&host_fh, &host_stfs));
+    if (is_error(ret)) {
+        return ret;
+    }
+    return h2t_freebsd11_statfs(target_stfs_addr, &host_stfs);
+}
+
 /* fhstatfs(2) */
 static inline abi_long do_freebsd_fhstatfs(abi_ulong target_fhp_addr,
         abi_ulong target_stfs_addr)
Signature · 1 line
2.40.0
author
#61 lore raw

PATCH 25/33 Implement freebsd11 stat related syscalls

In reply to Karim Taha

From: Michal Meloun <[email protected]>

Implement the freebsd11 variant of the following syscalls:
statfs(2)
fstatfs(2)
getfsstat(2)

---
 bsd-user/freebsd/os-stat.h | 75 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
bsd-user/freebsd/os-stat.h +75 -0
index 2e0c7245df..04a61fabd1 100644
@@ -32,9 +32,15 @@ __sym_compat(fstatat, freebsd11_fstatat, FBSD_1.1);
 
 int freebsd11_fhstat(const fhandle_t *fhandle, struct freebsd11_stat *stat);
 __sym_compat(fhstat, freebsd11_fhstat, FBSD_1.0);
+int freebsd11_getfsstat(struct freebsd11_statfs *buf, long bufsize, int mode);
+__sym_compat(getfsstat, freebsd11_getfsstat, FBSD_1.0);
 int freebsd11_fhstatfs(const fhandle_t *fhandle, struct freebsd11_statfs * buf);
 __sym_compat(fhstatfs, freebsd11_fhstatfs, FBSD_1.0);
 int freebsd11_statfs(const char *path, struct freebsd11_statfs *buf);
+__sym_compat(statfs, freebsd11_statfs, FBSD_1.0);
+int freebsd11_fstatfs(int fd, struct freebsd11_statfs *buf);
+__sym_compat(fstatfs, freebsd11_fstatfs, FBSD_1.0);
+
 
 /* stat(2) */
 static inline abi_long do_freebsd11_stat(abi_long arg1, abi_long arg2)
@@ -293,6 +299,23 @@ static inline abi_long do_freebsd_fhstatfs(abi_ulong target_fhp_addr,
     return h2t_freebsd_statfs(target_stfs_addr, &host_stfs);
 }
 
+/* statfs(2) */
+static inline abi_long do_freebsd11_statfs(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    void *p;
+    struct freebsd11_statfs host_stfs;
+
+    LOCK_PATH(p, arg1);
+    ret = get_errno(freebsd11_statfs(path(p), &host_stfs));
+    UNLOCK_PATH(p, arg1);
+    if (is_error(ret)) {
+        return ret;
+    }
+
+    return h2t_freebsd11_statfs(arg2, &host_stfs);
+}
+
 /* statfs(2) */
 static inline abi_long do_freebsd_statfs(abi_long arg1, abi_long arg2)
 {
@@ -310,6 +333,20 @@ static inline abi_long do_freebsd_statfs(abi_long arg1, abi_long arg2)
     return h2t_freebsd_statfs(arg2, &host_stfs);
 }
 
+/* fstatfs(2) */
+static inline abi_long do_freebsd11_fstatfs(abi_long fd, abi_ulong target_addr)
+{
+    abi_long ret;
+    struct freebsd11_statfs host_stfs;
+
+    ret = get_errno(freebsd11_fstatfs(fd, &host_stfs));
+    if (is_error(ret)) {
+        return ret;
+    }
+
+    return h2t_freebsd11_statfs(target_addr, &host_stfs);
+}
+
 /* fstatfs(2) */
 static inline abi_long do_freebsd_fstatfs(abi_long fd, abi_ulong target_addr)
 {
@@ -324,6 +361,44 @@ static inline abi_long do_freebsd_fstatfs(abi_long fd, abi_ulong target_addr)
     return h2t_freebsd_statfs(target_addr, &host_stfs);
 }
 
+/* getfsstat(2) */
+static inline abi_long do_freebsd11_getfsstat(abi_ulong target_addr,
+        abi_long bufsize, abi_long flags)
+{
+    abi_long ret;
+    struct freebsd11_statfs *host_stfs;
+    int count;
+    long host_bufsize;
+
+    count = bufsize / sizeof(struct target_freebsd11_statfs);
+
+    /* if user buffer is NULL then return number of mounted FS's */
+    if (target_addr == 0 || count == 0) {
+        return get_errno(freebsd11_getfsstat(NULL, 0, flags));
+    }
+
+    /* XXX check count to be reasonable */
+    host_bufsize = sizeof(struct freebsd11_statfs) * count;
+    host_stfs = alloca(host_bufsize);
+    if (!host_stfs) {
+        return -TARGET_EINVAL;
+    }
+
+    ret = count = get_errno(freebsd11_getfsstat(host_stfs, host_bufsize, flags));
+    if (is_error(ret)) {
+        return ret;
+    }
+
+    while (count--) {
+        if (h2t_freebsd11_statfs((target_addr +
+                        (count * sizeof(struct target_freebsd11_statfs))),
+                    &host_stfs[count])) {
+            return -TARGET_EFAULT;
+        }
+    }
+    return ret;
+}
+
 /* getfsstat(2) */
 static inline abi_long do_freebsd_getfsstat(abi_ulong target_addr,
         abi_long bufsize, abi_long flags)
Signature · 1 line
2.40.0
author
#62 lore raw

PATCH 26/33 Implement freebsd11 stat related syscalls

In reply to Karim Taha

From: Michal Meloun <[email protected]>

Implement the freebsd11 variant of the following syscalls:
getdirecentries(2)

---
 bsd-user/freebsd/os-stat.h | 44 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
bsd-user/freebsd/os-stat.h +44 -0
index 04a61fabd1..26909af455 100644
@@ -41,6 +41,11 @@ __sym_compat(statfs, freebsd11_statfs, FBSD_1.0);
 int freebsd11_fstatfs(int fd, struct freebsd11_statfs *buf);
 __sym_compat(fstatfs, freebsd11_fstatfs, FBSD_1.0);
 
+ssize_t freebsd11_getdirentries(int fd, char *buf, size_t nbytes, off_t *basep);
+__sym_compat(getdirentries, freebsd11_getdirentries, FBSD_1.0);
+ssize_t freebsd11_getdents(int fd, char *buf, size_t nbytes);
+__sym_compat(getdents, freebsd11_getdents, FBSD_1.0);
+
 
 /* stat(2) */
 static inline abi_long do_freebsd11_stat(abi_long arg1, abi_long arg2)
@@ -468,6 +473,45 @@ static inline abi_long do_freebsd11_getdents(abi_long arg1,
     return ret;
 }
 
+/* getdirecentries(2) */
+static inline abi_long do_freebsd11_getdirentries(abi_long arg1,
+        abi_ulong arg2, abi_long nbytes, abi_ulong arg4)
+{
+    abi_long ret;
+    struct freebsd11_dirent *dirp;
+    long basep;
+
+    dirp = lock_user(VERIFY_WRITE, arg2, nbytes, 0);
+    if (dirp == NULL) {
+        return -TARGET_EFAULT;
+    }
+    ret = get_errno(freebsd11_getdirentries(arg1, (char *)dirp, nbytes, &basep));
+    if (!is_error(ret)) {
+        struct freebsd11_dirent *de;
+        int len = ret;
+        int reclen;
+
+        de = dirp;
+        while (len > 0) {
+            reclen = de->d_reclen;
+            if (reclen > len) {
+                return -TARGET_EFAULT;
+            }
+            de->d_reclen = tswap16(reclen);
+            de->d_fileno = tswap32(de->d_fileno);
+            len -= reclen;
+            de = (struct freebsd11_dirent *)((void *)de + reclen);
+        }
+    }
+    unlock_user(dirp, arg2, ret);
+    if (arg4) {
+        if (put_user(basep, arg4, abi_ulong)) {
+            return -TARGET_EFAULT;
+        }
+    }
+    return ret;
+}
+
 /* getdirecentries(2) */
 static inline abi_long do_freebsd_getdirentries(abi_long arg1,
         abi_ulong arg2, abi_long nbytes, abi_ulong arg4)
Signature · 1 line
2.40.0
author
#63 lore raw

PATCH 27/33 Implement freebsd11 stat related syscalls

In reply to Karim Taha

From: Michal Meloun <[email protected]>

Forward declaration of the nstat syscalls:
nstat
nlstat
nfstat

---
 bsd-user/freebsd/os-stat.h | 7 +++++++
 1 file changed, 7 insertions(+)
bsd-user/freebsd/os-stat.h +7 -0
index 26909af455..e31b2aab9e 100644
@@ -46,6 +46,13 @@ __sym_compat(getdirentries, freebsd11_getdirentries, FBSD_1.0);
 ssize_t freebsd11_getdents(int fd, char *buf, size_t nbytes);
 __sym_compat(getdents, freebsd11_getdents, FBSD_1.0);
 
+/* undocumented nstat system calls */
+int freebsd11_nstat(const char *path, struct freebsd11_stat *sb);
+__sym_compat(nstat, freebsd11_nstat, FBSD_1.0);
+int freebsd11_nlstat(const char *path, struct freebsd11_stat *sb);
+__sym_compat(nlstat, freebsd11_nlstat, FBSD_1.0);
+int freebsd11_nfstat(int fd, struct freebsd11_stat *sb);
+__sym_compat(nfstat, freebsd11_nfstat, FBSD_1.0);
 
 /* stat(2) */
 static inline abi_long do_freebsd11_stat(abi_long arg1, abi_long arg2)
Signature · 1 line
2.40.0
author
#64 lore raw

PATCH 28/33 Implement do_freebsd_realpathat syscall

In reply to Karim Taha

From: Mikaël Urankar <[email protected]>

---
 bsd-user/freebsd/os-stat.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
bsd-user/freebsd/os-stat.h +26 -0
index e31b2aab9e..cf56c778ae 100644
@@ -634,4 +634,30 @@ static inline abi_long do_freebsd_fcntl(abi_long arg1, abi_long arg2,
     return ret;
 }
 
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 1300080
+extern int __realpathat(int fd, const char *path, char *buf, size_t size,
+        int flags);
+// https://svnweb.freebsd.org/base?view=revision&revision=358172
+// no man page
+static inline abi_long do_freebsd_realpathat(abi_long arg1, abi_long arg2,
+        abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    abi_long ret;
+    void *p, *b;
+
+    LOCK_PATH(p, arg2);
+    b = lock_user(VERIFY_WRITE, arg3, arg4, 0);
+    if (b == NULL) {
+        UNLOCK_PATH(p, arg2);
+        return -TARGET_EFAULT;
+    }
+
+    ret = get_errno(__realpathat(arg1, p, b, arg4, arg5));
+    UNLOCK_PATH(p, arg2);
+    unlock_user(b, arg3, ret);
+
+    return ret;
+}
+#endif
+
 #endif /* BSD_USER_FREEBSD_OS_STAT_H */
Signature · 1 line
2.40.0
author
#65 lore raw

PATCH 29/33 Add os-stat.c to the build

In reply to Karim Taha
---
 bsd-user/freebsd/meson.build | 1 +
 1 file changed, 1 insertion(+)
bsd-user/freebsd/meson.build +1 -0
index f87c788e84..f2f047cca3 100644
@@ -1,4 +1,5 @@
 bsd_user_ss.add(files(
+  'os-stat.c',
   'os-sys.c',
   'os-syscall.c',
 ))
Signature · 1 line
2.40.0
author
#66 lore raw

PATCH 30/33 Add glue to call the following syscalls to the freebsd_syscall function:

In reply to Karim Taha

From: Warner Losh <[email protected]>

freebsd11_stat
freebsd11_lstat
freebsd11_fstat
freebsd11_fstatat
freebsd11_nstat, freebsd11_nfstat, freebsd11_nlstat fstatat
fstat

---
 bsd-user/freebsd/os-syscall.c | 42 +++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
bsd-user/freebsd/os-syscall.c +42 -0
index 2224a280ea..ade47a0d2f 100644
@@ -36,6 +36,9 @@
 #include "bsd-file.h"
 #include "bsd-proc.h"
 
+/* *BSD dependent syscall shims */
+#include "os-stat.h"
+
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
 safe_syscall4(int, openat, int, fd, const char *, path, int, flags, mode_t,
@@ -482,6 +485,45 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_undelete(arg1);
         break;
 
+        /*
+         * stat system calls
+         */
+    case TARGET_FREEBSD_NR_freebsd11_stat: /* stat(2) */
+        ret = do_freebsd11_stat(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_lstat: /* lstat(2) */
+        ret = do_freebsd11_lstat(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_fstat: /* fstat(2) */
+        ret = do_freebsd11_fstat(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_fstat: /* fstat(2) */
+        ret = do_freebsd_fstat(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_fstatat: /* fstatat(2) */
+        ret = do_freebsd11_fstatat(arg1, arg2, arg3, arg4);
+        break;
+
+    case TARGET_FREEBSD_NR_fstatat: /* fstatat(2) */
+        ret = do_freebsd_fstatat(arg1, arg2, arg3, arg4);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_nstat: /* undocumented */
+        ret = do_freebsd11_nstat(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_nfstat: /* undocumented */
+        ret = do_freebsd11_nfstat(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_nlstat: /* undocumented */
+        ret = do_freebsd11_nlstat(arg1, arg2);
+        break;
+
         /*
          * sys{ctl, arch, call}
          */
Signature · 1 line
2.40.0
author
#67 lore raw

PATCH 31/33 Add glue to call the following syscalls to the freebsd_syscall function:

In reply to Karim Taha

From: Warner Losh <[email protected]>

getfh
lgetfh
fhopen
freebsd11_fhstat
freebsd11_fhstatfs
fhstat
fhstatfs

---
 bsd-user/freebsd/os-syscall.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
bsd-user/freebsd/os-syscall.c +28 -0
index ade47a0d2f..73616a5be0 100644
@@ -524,6 +524,34 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_freebsd11_nlstat(arg1, arg2);
         break;
 
+    case TARGET_FREEBSD_NR_getfh: /* getfh(2) */
+        ret = do_freebsd_getfh(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_lgetfh: /* lgetfh(2) */
+        ret = do_freebsd_lgetfh(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_fhopen: /* fhopen(2) */
+        ret = do_freebsd_fhopen(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_fhstat: /* fhstat(2) */
+        ret = do_freebsd11_fhstat(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_fhstat: /* fhstat(2) */
+        ret = do_freebsd_fhstat(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_fhstatfs: /* fhstatfs(2) */
+        ret = do_freebsd11_fhstatfs(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_fhstatfs: /* fhstatfs(2) */
+        ret = do_freebsd_fhstatfs(arg1, arg2);
+        break;
+
         /*
          * sys{ctl, arch, call}
          */
Signature · 1 line
2.40.0
author
#68 lore raw

PATCH 32/33 Add glue to call the following syscalls to the freebsd_syscall function:

In reply to Karim Taha

From: Warner Losh <[email protected]>

freebsd11_statfs
statfs
freebsd11_fstatfs
fstatfs
freebsd11_getfsstat
getfsstat

---
 bsd-user/freebsd/os-syscall.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
bsd-user/freebsd/os-syscall.c +24 -0
index 73616a5be0..916a754bf8 100644
@@ -552,6 +552,30 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_freebsd_fhstatfs(arg1, arg2);
         break;
 
+    case TARGET_FREEBSD_NR_freebsd11_statfs: /* statfs(2) */
+        ret = do_freebsd11_statfs(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_statfs: /* statfs(2) */
+        ret = do_freebsd_statfs(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_fstatfs: /* fstatfs(2) */
+        ret = do_freebsd11_fstatfs(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_fstatfs: /* fstatfs(2) */
+        ret = do_freebsd_fstatfs(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_getfsstat: /* getfsstat(2) */
+        ret = do_freebsd11_getfsstat(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_getfsstat: /* getfsstat(2) */
+        ret = do_freebsd_getfsstat(arg1, arg2, arg3);
+        break;
+
         /*
          * sys{ctl, arch, call}
          */
Signature · 1 line
2.40.0
author
#69 lore raw

PATCH 33/33 Add glue to call the following syscalls to the freebsd_syscall function:

In reply to Karim Taha

From: Warner Losh <[email protected]>

freebsd11_getdents
getdirentries
freebsd11_getdirentries
fcntl

---
 bsd-user/freebsd/os-syscall.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
bsd-user/freebsd/os-syscall.c +16 -0
index 916a754bf8..e9b1b663af 100644
@@ -576,6 +576,22 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_freebsd_getfsstat(arg1, arg2, arg3);
         break;
 
+    case TARGET_FREEBSD_NR_freebsd11_getdents: /* getdents(2) */
+        ret = do_freebsd11_getdents(arg1, arg2, arg3);
+        break;
+
+    case TARGET_FREEBSD_NR_getdirentries: /* getdirentries(2) */
+        ret = do_freebsd_getdirentries(arg1, arg2, arg3, arg4);
+        break;
+
+    case TARGET_FREEBSD_NR_freebsd11_getdirentries: /* getdirentries(2) */
+        ret = do_freebsd11_getdirentries(arg1, arg2, arg3, arg4);
+        break;
+    case TARGET_FREEBSD_NR_fcntl: /* fcntl(2) */
+        ret = do_freebsd_fcntl(arg1, arg2, arg3);
+        break;
+
+
         /*
          * sys{ctl, arch, call}
          */
Signature · 1 line
2.40.0