Skip to contentSkip to conversation

bsd-user 2023 Q2 first batch

PATCH Superseded Warner Losh 2 participants 26 messages Apr 10, 2023 – Apr 11, 2023 Arm/Amlogic Meson SoC support

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover bsd-user 2023 Q2 first batch

This series is a number of misc cleanups.

First, this replaces my plans to remove netbsd and openbsd code entirely. I've been in contact with the NetBSD folks that would like to make things work. The plan is that I'll not remove it in qemu-project, and restore them in bsd-user fork. These changes clean up some of the mess that's here: I've moved the bits that make sense here, and removed the ones that don't (but upstream I've moved them when it makes sense). My intention is to work with the NetBSD folks (and OpenBSD too if they want) to cope with the structural issues I'm aware of in bsd-user fork. Future contributions should go via that route at a rate of contributions (I have little time to do the work, but will commit to finding time to do the coordination and review work).

Next I've #ifdef'd some mmap handling based on certain flags being defined or not. This was something I'd removed before, and were in what Taylor sent me, so I've restored the relevant ones. The rest of the patch that Taylor sent me conflicts with bsd-user and needs some work to get it in upstream. I removed a few unused mmap defines as well. And I cleanup mmap.c to remove gratuitous differences and pass checkpatch.pl.

Next, I've made unimplmeneted system calls generate SIGSYS. For the moment, this is the best we should do since so much is unimplemented and as things get implemented this will allow controlled testing of code to ensure it doesn't silently succeed, giving the impression things are working when they aren't. I also add SIGSYS to the uncaught coredump signal list, to match FreeBSD's behavior. I only do this on arm, though, because the signal implementation on x86 is incomplete (even upstream) and I didn't want to take chances.

Finally, I've included the core dump code. There's about 600 lines of sysctl support that I've included as separate commits of ~150 lines each (and incidentlaly, added the translation to os-sys.c for those bits). The core dump code itself is largely copied from linux-user/elfload.c by sson (so he gets the author credit). I moved it to elfcore.c when I was upstremaing in the past and upstreamed a stub. One of the patches in this series replaces elfcore.c and is 1300 lines long. It's not easily sliced up into smaller bits that compile, but I'm open to suggestions. It's known to "work" in the sense that it will generate core files that gdb can read and intelligently parse.

This patch series is for after 8.0 is done, but before any GSoC projects start, and will be independent of any GSoC contribution tasks.

v2: A few more 'high line count, easy to review' changes:
- Remove a few more files
- Regenerate FreeBSD system calls
- Remove host-os.h, since we no longer plan to support multiple BSDs in one binary.
- Review comments: Remove USE_ELF_CORE_DUMP and change ifdefs for MAP_

Need reviews for parts 2, 17, 18, 19 for sure. Parts 3 and 14 just have Acked-by.

Stacey Son (6):
bsd-user: h2g_rusage
bsd-user: Implement do_sysctl_kern_getprocs
bsd-user: Implement do_sysctl_kern_proc_filedesc
bsd-user: Implement do_sysctl_kern_proc_vmmap
bsd-user: Implement sysctl kern.proc, except kern.proc.full_path
bsd-user: Implement core dumps

Warner Losh (13):
bsd-user: Make print_* public
bsd-user: Ifdef a few MAP_ constants for NetBSD / OpenBSD.
bsd-user: Cleanup style.
bsd-user: Move system FreeBSD call table to freebsd/os-syscall.c
bsd-user: Remove NetBSD specific syscall printing
bsd-user: Remove OpenBSD specific syscall printing
bsd-user: Move system call include to os-syscall.h
bsd-user: Remove useless mmap definitions
bsd-user: Add SIGSYS to core dump signals.
bsd-user: Implement SIGSYS on arm
bsd-user: Remove host-os.h
bsd-user: Update system call list
bsd-user: Eliminate USE_ELF_CORE_DUMP

 bsd-user/arm/target_arch_cpu.h             |    8 +
 bsd-user/arm/target_arch_elf.h             |    1 -
 bsd-user/bsd-proc.c                        |   48 +
 bsd-user/elfcore.c                         | 1315 +++++++++++++++++++-
 bsd-user/elfload.c                         |    5 -
 bsd-user/freebsd/os-sys.c                  |  508 +++++++-
 bsd-user/freebsd/os-syscall.c              |   19 +
 bsd-user/freebsd/os-syscall.h              |   23 +
 bsd-user/freebsd/syscall_nr.h              | 1035 +++++++--------
 bsd-user/i386/target_arch_elf.h            |    1 -
 bsd-user/main.c                            |    1 -
 bsd-user/meson.build                       |    1 +
 bsd-user/mmap.c                            |  105 +-
 bsd-user/netbsd/host-os.h                  |   25 -
 bsd-user/netbsd/os-syscall.h               |   16 +
 bsd-user/openbsd/host-os.h                 |   25 -
 bsd-user/openbsd/os-syscall.h              |   16 +
 bsd-user/{freebsd/host-os.h => qemu-bsd.h} |   15 +-
 bsd-user/qemu.h                            |   44 +-
 bsd-user/signal.c                          |   13 +-
 bsd-user/strace.c                          |   88 +-
 bsd-user/syscall_defs.h                    |   69 +-
 bsd-user/x86_64/target_arch_elf.h          |    1 -
 23 files changed, 2616 insertions(+), 766 deletions(-)
 create mode 100644 bsd-user/bsd-proc.c
 create mode 100644 bsd-user/freebsd/os-syscall.h
 delete mode 100644 bsd-user/netbsd/host-os.h
 create mode 100644 bsd-user/netbsd/os-syscall.h
 delete mode 100644 bsd-user/openbsd/host-os.h
 create mode 100644 bsd-user/openbsd/os-syscall.h
 rename bsd-user/{freebsd/host-os.h => qemu-bsd.h} (72%)
Signature · 1 line
2.40.0
#1 lore raw

PATCH v2 1/19 bsd-user: Make print_* public

Make these functions public. Due to coming restructuring, we'll need to call these from *bsd/os-syscall.c. Add declarations to qemu.h.

---
 bsd-user/qemu.h   | 20 ++++++++++++++++++++
 bsd-user/strace.c | 29 +++++++++++++----------------
 2 files changed, 33 insertions(+), 16 deletions(-)
bsd-user/qemu.h +20 -0
index 41d84e0b81b..22e16816a9e 100644
@@ -211,6 +211,26 @@ print_openbsd_syscall(int num,
                       abi_long arg1, abi_long arg2, abi_long arg3,
                       abi_long arg4, abi_long arg5, abi_long arg6);
 void print_openbsd_syscall_ret(int num, abi_long ret);
+void print_execve(const struct syscallname *name, abi_long arg1,
+                  abi_long arg2, abi_long arg3, abi_long arg4,
+                  abi_long arg5, abi_long arg6);
+void print_ioctl(const struct syscallname *name,
+                 abi_long arg1, abi_long arg2, abi_long arg3,
+                 abi_long arg4, abi_long arg5, abi_long arg6);
+void print_sysarch(const struct syscallname *name, abi_long arg1,
+                   abi_long arg2, abi_long arg3, abi_long arg4,
+                   abi_long arg5, abi_long arg6);
+void print_sysctl(const struct syscallname *name, abi_long arg1,
+                  abi_long arg2, abi_long arg3, abi_long arg4,
+                  abi_long arg5, abi_long arg6);
+void print_syscall(int num, const struct syscallname *scnames,
+                   unsigned int nscnames, abi_long arg1, abi_long arg2,
+                   abi_long arg3, abi_long arg4, abi_long arg5,
+                   abi_long arg6);
+void print_syscall_ret(int num, abi_long ret,
+                       const struct syscallname *scnames,
+                       unsigned int nscnames);
+void print_syscall_ret_addr(const struct syscallname *name, abi_long ret);
 /**
  * print_taken_signal:
  * @target_signum: target signal being taken
bsd-user/strace.c +13 -16
index 96499751eb0..e45909b8688 100644
@@ -49,7 +49,7 @@ print_raw_param(const char *fmt, abi_long param, int last)
     gemu_log(format, param);
 }
 
-static void print_sysctl(const struct syscallname *name, abi_long arg1,
+void print_sysctl(const struct syscallname *name, abi_long arg1,
         abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
         abi_long arg6)
 {
@@ -71,9 +71,8 @@ static void print_sysctl(const struct syscallname *name, abi_long arg1,
         (uint32_t)arg2, arg3, arg4, arg5, arg6);
 }
 
-static void print_execve(const struct syscallname *name, abi_long arg1,
-        abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
-        abi_long arg6)
+void print_execve(const struct syscallname *name, abi_long arg1, abi_long arg2,
+                  abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
 {
     abi_ulong arg_ptr_addr;
     char *s;
@@ -105,9 +104,8 @@ static void print_execve(const struct syscallname *name, abi_long arg1,
     gemu_log("NULL})");
 }
 
-static void print_ioctl(const struct syscallname *name,
-        abi_long arg1, abi_long arg2, abi_long arg3, abi_long arg4,
-        abi_long arg5, abi_long arg6)
+void print_ioctl(const struct syscallname *name, abi_long arg1, abi_long arg2,
+                 abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
 {
     /* Decode the ioctl request */
     gemu_log("%s(%d, 0x%0lx { IO%s%s GRP:0x%x('%c') CMD:%d LEN:%d }, 0x"
@@ -124,9 +122,8 @@ static void print_ioctl(const struct syscallname *name,
             arg3);
 }
 
-static void print_sysarch(const struct syscallname *name, abi_long arg1,
-        abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5,
-        abi_long arg6)
+void print_sysarch(const struct syscallname *name, abi_long arg1, abi_long arg2,
+                   abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
 {
     /* This is os dependent. */
     do_os_print_sysarch(name, arg1, arg2, arg3, arg4, arg5, arg6);
@@ -136,7 +133,7 @@ static void print_sysarch(const struct syscallname *name, abi_long arg1,
  * Variants for the return value output function
  */
 
-static void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
+void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
 {
     if (ret == -1) {
         gemu_log(" = -1 errno=%d (%s)\n", errno, strerror(errno));
@@ -159,9 +156,9 @@ static const struct syscallname openbsd_scnames[] = {
 #include "openbsd/strace.list"
 };
 
-static void print_syscall(int num, const struct syscallname *scnames,
-        unsigned int nscnames, abi_long arg1, abi_long arg2, abi_long arg3,
-        abi_long arg4, abi_long arg5, abi_long arg6)
+void print_syscall(int num, const struct syscallname *scnames,
+                   unsigned int nscnames, abi_long arg1, abi_long arg2,
+                   abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
 {
     unsigned int i;
     const char *format="%s(" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld ","
@@ -190,8 +187,8 @@ static void print_syscall(int num, const struct syscallname *scnames,
     gemu_log("Unknown syscall %d\n", num);
 }
 
-static void print_syscall_ret(int num, abi_long ret,
-        const struct syscallname *scnames, unsigned int nscnames)
+void print_syscall_ret(int num, abi_long ret, const struct syscallname *scnames,
+                       unsigned int nscnames)
 {
     unsigned int i;
 
Signature · 1 line
2.40.0
#2 lore raw

PATCH v2 2/19 bsd-user: Ifdef a few MAP_ constants for NetBSD / OpenBSD.

In reply to Warner Losh

MAP_GUARD, MAP_EXCL, and MAP_NOCORE are FreeBSD only. Define them to be 0 if they aren't defined, and rely on the compiler to optimize away sections not relevant. Added only to the top of mmap.c since that's the only place we need this.

---
 bsd-user/mmap.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
bsd-user/mmap.c +14 -0
index d6c5a344c9b..2d91e8e8826 100644
@@ -20,6 +20,20 @@
 
 #include "qemu.h"
 
+/*
+ * Not all the BSDs have all the MAP flags, so define some of them to 0 here and
+ * rely on the compiler optimizing always false conditions away.
+ */
+#ifndef MAP_GUARD
+#define MAP_GUARD 0
+#endif
+#ifndef MAP_EXCL
+#define MAP_EXCL 0
+#endif
+#ifndef MAP_NOCORE
+#define MAP_NOCORE 0
+#endif
+
 static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
 static __thread int mmap_lock_count;
 
Signature · 1 line
2.40.0
Reviewed-by on v2 2/19
#3 lore raw

Re: [PATCH v2 02/19] bsd-user: Ifdef a few MAP_ constants for NetBSD / OpenBSD.

Quoted 9 lines · Warner Losh

MAP_GUARD, MAP_EXCL, and MAP_NOCORE are FreeBSD only. Define them to be 0 if they aren't defined, and rely on the compiler to optimize away sections not relevant. Added only to the top of mmap.c since that's the only place we need this.

---
  bsd-user/mmap.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)

r~

#4 lore raw

PATCH v2 3/19 bsd-user: Cleanup style.

In reply to Warner Losh

The only diffs between bsd-user fork and qemu upstream is style. Make mmap.c pass checkpatch.pl.

---
 bsd-user/mmap.c | 91 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 60 insertions(+), 31 deletions(-)
bsd-user/mmap.c +60 -31
index 2d91e8e8826..d0ade1b52f3 100644
@@ -59,17 +59,19 @@ bool have_mmap_lock(void)
 /* Grab lock to make sure things are in a consistent state after fork().  */
 void mmap_fork_start(void)
 {
-    if (mmap_lock_count)
+    if (mmap_lock_count) {
         abort();
+    }
     pthread_mutex_lock(&mmap_mutex);
 }
 
 void mmap_fork_end(int child)
 {
-    if (child)
+    if (child) {
         pthread_mutex_init(&mmap_mutex, NULL);
-    else
+    } else {
         pthread_mutex_unlock(&mmap_mutex);
+    }
 }
 
 /* NOTE: all the constants are the HOST ones, but addresses are target. */
@@ -83,15 +85,18 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
                   prot & PROT_READ ? 'r' : '-',
                   prot & PROT_WRITE ? 'w' : '-',
                   prot & PROT_EXEC ? 'x' : '-');
-    if ((start & ~TARGET_PAGE_MASK) != 0)
+    if ((start & ~TARGET_PAGE_MASK) != 0) {
         return -EINVAL;
+    }
     len = TARGET_PAGE_ALIGN(len);
     end = start + len;
-    if (end < start)
+    if (end < start) {
         return -EINVAL;
+    }
     prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
-    if (len == 0)
+    if (len == 0) {
         return 0;
+    }
 
     mmap_lock();
     host_start = start & qemu_host_page_mask;
@@ -110,8 +115,9 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
         }
         ret = mprotect(g2h_untagged(host_start),
                        qemu_host_page_size, prot1 & PAGE_BITS);
-        if (ret != 0)
+        if (ret != 0) {
             goto error;
+        }
         host_start += qemu_host_page_size;
     }
     if (end < host_end) {
@@ -121,16 +127,18 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
         }
         ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
                        qemu_host_page_size, prot1 & PAGE_BITS);
-        if (ret != 0)
+        if (ret != 0) {
             goto error;
+        }
         host_end -= qemu_host_page_size;
     }
 
     /* handle the pages in the middle */
     if (host_start < host_end) {
         ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
-        if (ret != 0)
+        if (ret != 0) {
             goto error;
+        }
     }
     page_set_flags(start, start + len, prot | PAGE_VALID);
     mmap_unlock();
@@ -175,31 +183,37 @@ static int mmap_frag(abi_ulong real_start,
     /* get the protection of the target pages outside the mapping */
     prot1 = 0;
     for (addr = real_start; addr < real_end; addr++) {
-        if (addr < start || addr >= end)
+        if (addr < start || addr >= end) {
             prot1 |= page_get_flags(addr);
+        }
     }
 
     if (prot1 == 0) {
         /* no page was there, so we allocate one. See also above. */
         void *p = mmap(host_start, qemu_host_page_size, prot,
                        flags | ((fd != -1) ? MAP_ANON : 0), -1, 0);
-        if (p == MAP_FAILED)
+        if (p == MAP_FAILED) {
             return -1;
+        }
         prot1 = prot;
     }
     prot1 &= PAGE_BITS;
 
     prot_new = prot | prot1;
     if (fd != -1) {
-        /* msync() won't work here, so we return an error if write is
-           possible while it is a shared mapping */
+        /*
+         * msync() won't work here, so we return an error if write is
+         * possible while it is a shared mapping
+         */
         if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
-            (prot & PROT_WRITE))
+            (prot & PROT_WRITE)) {
             return -1;
+        }
 
         /* adjust protection to be able to read */
-        if (!(prot1 & PROT_WRITE))
+        if (!(prot1 & PROT_WRITE)) {
             mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE);
+        }
 
         /* read the corresponding file data */
         if (pread(fd, g2h_untagged(start), end - start, offset) == -1) {
@@ -207,8 +221,9 @@ static int mmap_frag(abi_ulong real_start,
         }
 
         /* put final protection */
-        if (prot_new != (prot1 | PROT_WRITE))
+        if (prot_new != (prot1 | PROT_WRITE)) {
             mprotect(host_start, qemu_host_page_size, prot_new);
+        }
     } else {
         if (prot_new != prot1) {
             mprotect(host_start, qemu_host_page_size, prot_new);
@@ -560,8 +575,9 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
          */
         p = mmap(g2h_untagged(start), host_len, prot,
                  flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
-        if (p == MAP_FAILED)
+        if (p == MAP_FAILED) {
             goto fail;
+        }
         /* update start so that it points to the file position at 'offset' */
         host_start = (unsigned long)p;
         if (fd != -1) {
@@ -610,8 +626,9 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
             retaddr = target_mmap(start, len, prot | PROT_WRITE,
                                   MAP_FIXED | MAP_PRIVATE | MAP_ANON,
                                   -1, 0);
-            if (retaddr == -1)
+            if (retaddr == -1) {
                 goto fail;
+            }
             if (pread(fd, g2h_untagged(start), len, offset) == -1) {
                 goto fail;
             }
@@ -634,14 +651,16 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
                 /* one single host page */
                 ret = mmap_frag(real_start, start, end,
                                 prot, flags, fd, offset);
-                if (ret == -1)
+                if (ret == -1) {
                     goto fail;
+                }
                 goto the_end1;
             }
             ret = mmap_frag(real_start, start, real_start + qemu_host_page_size,
                             prot, flags, fd, offset);
-            if (ret == -1)
+            if (ret == -1) {
                 goto fail;
+            }
             real_start += qemu_host_page_size;
         }
         /* handle the end of the mapping */
@@ -650,8 +669,9 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
                             real_end - qemu_host_page_size, end,
                             prot, flags, fd,
                             offset + real_end - qemu_host_page_size - start);
-            if (ret == -1)
+            if (ret == -1) {
                 goto fail;
+            }
             real_end -= qemu_host_page_size;
         }
 
@@ -659,14 +679,16 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
         if (real_start < real_end) {
             void *p;
             unsigned long offset1;
-            if (flags & MAP_ANON)
+            if (flags & MAP_ANON) {
                 offset1 = 0;
-            else
+            } else {
                 offset1 = offset + real_start - start;
+            }
             p = mmap(g2h_untagged(real_start), real_end - real_start,
                      prot, flags, fd, offset1);
-            if (p == MAP_FAILED)
+            if (p == MAP_FAILED) {
                 goto fail;
+            }
         }
     }
  the_end1:
@@ -736,11 +758,13 @@ int target_munmap(abi_ulong start, abi_ulong len)
            TARGET_ABI_FMT_lx "\n",
            start, len);
 #endif
-    if (start & ~TARGET_PAGE_MASK)
+    if (start & ~TARGET_PAGE_MASK) {
         return -EINVAL;
+    }
     len = TARGET_PAGE_ALIGN(len);
-    if (len == 0)
+    if (len == 0) {
         return -EINVAL;
+    }
     mmap_lock();
     end = start + len;
     real_start = start & qemu_host_page_mask;
@@ -758,16 +782,18 @@ int target_munmap(abi_ulong start, abi_ulong len)
             }
             end = real_end;
         }
-        if (prot != 0)
+        if (prot != 0) {
             real_start += qemu_host_page_size;
+        }
     }
     if (end < real_end) {
         prot = 0;
         for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) {
             prot |= page_get_flags(addr);
         }
-        if (prot != 0)
+        if (prot != 0) {
             real_end -= qemu_host_page_size;
+        }
     }
 
     ret = 0;
@@ -791,14 +817,17 @@ int target_msync(abi_ulong start, abi_ulong len, int flags)
 {
     abi_ulong end;
 
-    if (start & ~TARGET_PAGE_MASK)
+    if (start & ~TARGET_PAGE_MASK) {
         return -EINVAL;
+    }
     len = TARGET_PAGE_ALIGN(len);
     end = start + len;
-    if (end < start)
+    if (end < start) {
         return -EINVAL;
-    if (end == start)
+    }
+    if (end == start) {
         return 0;
+    }
 
     start &= qemu_host_page_mask;
     return msync(g2h_untagged(start), end - start, flags);
Signature · 1 line
2.40.0
#5 lore raw

PATCH v2 4/19 bsd-user: Move system FreeBSD call table to freebsd/os-syscall.c

In reply to Warner Losh

Move the system call table, and FreeBSD helper routines out of strace.c. We do not support multiple BSD-types in one binary, so simplify things by moving it.

---
 bsd-user/freebsd/os-syscall.c | 19 +++++++++++++++++++
 bsd-user/qemu.h               |  5 -----
 bsd-user/strace.c             | 17 -----------------
 3 files changed, 19 insertions(+), 22 deletions(-)
bsd-user/freebsd/os-syscall.c +19 -0
index c8f998ecec1..354a38943e5 100644
@@ -517,6 +517,25 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
     return ret;
 }
 
+static const struct syscallname freebsd_scnames[] = {
+#include "freebsd/strace.list"
+};
+
+static void print_freebsd_syscall(int num, abi_long arg1, abi_long arg2,
+                                  abi_long arg3, abi_long arg4, abi_long arg5,
+                                  abi_long arg6)
+{
+
+    print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2,
+            arg3, arg4, arg5, arg6);
+}
+
+static void print_freebsd_syscall_ret(int num, abi_long ret)
+{
+
+    print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames));
+}
+
 /*
  * do_freebsd_syscall() should always have a single exit point at the end so
  * that actions, such as logging of syscall results, can be performed. This
bsd-user/qemu.h +0 -5
index 22e16816a9e..c5240938da7 100644
@@ -196,11 +196,6 @@ struct syscallname {
     void (*result)(const struct syscallname *, abi_long);
 };
 
-void
-print_freebsd_syscall(int num,
-                      abi_long arg1, abi_long arg2, abi_long arg3,
-                      abi_long arg4, abi_long arg5, abi_long arg6);
-void print_freebsd_syscall_ret(int num, abi_long ret);
 void
 print_netbsd_syscall(int num,
                      abi_long arg1, abi_long arg2, abi_long arg3,
bsd-user/strace.c +0 -17
index e45909b8688..7d0117fd3cf 100644
@@ -146,9 +146,6 @@ void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
  * An array of all of the syscalls we know about
  */
 
-static const struct syscallname freebsd_scnames[] = {
-#include "freebsd/strace.list"
-};
 static const struct syscallname netbsd_scnames[] = {
 #include "netbsd/strace.list"
 };
@@ -212,20 +209,6 @@ void print_syscall_ret(int num, abi_long ret, const struct syscallname *scnames,
 /*
  * The public interface to this module.
  */
-void print_freebsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
-        abi_long arg4, abi_long arg5, abi_long arg6)
-{
-
-    print_syscall(num, freebsd_scnames, ARRAY_SIZE(freebsd_scnames), arg1, arg2,
-            arg3, arg4, arg5, arg6);
-}
-
-void print_freebsd_syscall_ret(int num, abi_long ret)
-{
-
-    print_syscall_ret(num, ret, freebsd_scnames, ARRAY_SIZE(freebsd_scnames));
-}
-
 void print_netbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
         abi_long arg4, abi_long arg5, abi_long arg6)
 {
Signature · 1 line
2.40.0
#6 lore raw

PATCH v2 5/19 bsd-user: Remove NetBSD specific syscall printing

In reply to Warner Losh

Nothing calls these routines now. In the bsd-user fork, though, they've moved to netbsd/os-syscall.c, but those aren't ready for upstreaming.

---
 bsd-user/qemu.h   |  5 -----
 bsd-user/strace.c | 17 -----------------
 2 files changed, 22 deletions(-)
bsd-user/qemu.h +0 -5
index c5240938da7..cee02d2a0ea 100644
@@ -196,11 +196,6 @@ struct syscallname {
     void (*result)(const struct syscallname *, abi_long);
 };
 
-void
-print_netbsd_syscall(int num,
-                     abi_long arg1, abi_long arg2, abi_long arg3,
-                     abi_long arg4, abi_long arg5, abi_long arg6);
-void print_netbsd_syscall_ret(int num, abi_long ret);
 void
 print_openbsd_syscall(int num,
                       abi_long arg1, abi_long arg2, abi_long arg3,
bsd-user/strace.c +0 -17
index 7d0117fd3cf..8e76caa3c3f 100644
@@ -146,9 +146,6 @@ void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
  * An array of all of the syscalls we know about
  */
 
-static const struct syscallname netbsd_scnames[] = {
-#include "netbsd/strace.list"
-};
 static const struct syscallname openbsd_scnames[] = {
 #include "openbsd/strace.list"
 };
@@ -209,20 +206,6 @@ void print_syscall_ret(int num, abi_long ret, const struct syscallname *scnames,
 /*
  * The public interface to this module.
  */
-void print_netbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
-        abi_long arg4, abi_long arg5, abi_long arg6)
-{
-
-    print_syscall(num, netbsd_scnames, ARRAY_SIZE(netbsd_scnames),
-                  arg1, arg2, arg3, arg4, arg5, arg6);
-}
-
-void print_netbsd_syscall_ret(int num, abi_long ret)
-{
-
-    print_syscall_ret(num, ret, netbsd_scnames, ARRAY_SIZE(netbsd_scnames));
-}
-
 void print_openbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
         abi_long arg4, abi_long arg5, abi_long arg6)
 {
Signature · 1 line
2.40.0
#7 lore raw

PATCH v2 6/19 bsd-user: Remove OpenBSD specific syscall printing

In reply to Warner Losh

Nothing calls these routines now. In the bsd-user fork, though, they've moved to openbsd/os-syscall.c, but those aren't ready for upstreaming.

---
 bsd-user/qemu.h   |  5 -----
 bsd-user/strace.c | 25 -------------------------
 2 files changed, 30 deletions(-)
bsd-user/qemu.h +0 -5
index cee02d2a0ea..49468734d44 100644
@@ -196,11 +196,6 @@ struct syscallname {
     void (*result)(const struct syscallname *, abi_long);
 };
 
-void
-print_openbsd_syscall(int num,
-                      abi_long arg1, abi_long arg2, abi_long arg3,
-                      abi_long arg4, abi_long arg5, abi_long arg6);
-void print_openbsd_syscall_ret(int num, abi_long ret);
 void print_execve(const struct syscallname *name, abi_long arg1,
                   abi_long arg2, abi_long arg3, abi_long arg4,
                   abi_long arg5, abi_long arg6);
bsd-user/strace.c +0 -25
index 8e76caa3c3f..b827acb2477 100644
@@ -142,14 +142,6 @@ void print_syscall_ret_addr(const struct syscallname *name, abi_long ret)
     }
 }
 
-/*
- * An array of all of the syscalls we know about
- */
-
-static const struct syscallname openbsd_scnames[] = {
-#include "openbsd/strace.list"
-};
-
 void print_syscall(int num, const struct syscallname *scnames,
                    unsigned int nscnames, abi_long arg1, abi_long arg2,
                    abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6)
@@ -203,23 +195,6 @@ void print_syscall_ret(int num, abi_long ret, const struct syscallname *scnames,
     }
 }
 
-/*
- * The public interface to this module.
- */
-void print_openbsd_syscall(int num, abi_long arg1, abi_long arg2, abi_long arg3,
-        abi_long arg4, abi_long arg5, abi_long arg6)
-{
-
-    print_syscall(num, openbsd_scnames, ARRAY_SIZE(openbsd_scnames), arg1, arg2,
-            arg3, arg4, arg5, arg6);
-}
-
-void print_openbsd_syscall_ret(int num, abi_long ret)
-{
-
-    print_syscall_ret(num, ret, openbsd_scnames, ARRAY_SIZE(openbsd_scnames));
-}
-
 static void
 print_signal(abi_ulong arg, int last)
 {
Signature · 1 line
2.40.0
#8 lore raw

PATCH v2 7/19 bsd-user: Move system call include to os-syscall.h

In reply to Warner Losh

Move the include of the system calls to os-syscall.h. Include that from syscall_defs.h. Use target_time_t and target_suseconds_t instead of the variant that has _freebsd_ in the name. Define these for OpenBSD and NetBSD based on comments in the file.

---
 bsd-user/freebsd/os-syscall.h | 21 +++++++++++++++++++++
 bsd-user/netbsd/os-syscall.h  | 16 ++++++++++++++++
 bsd-user/openbsd/os-syscall.h | 16 ++++++++++++++++
 bsd-user/syscall_defs.h       | 33 ++++-----------------------------
 4 files changed, 57 insertions(+), 29 deletions(-)
 create mode 100644 bsd-user/freebsd/os-syscall.h
 create mode 100644 bsd-user/netbsd/os-syscall.h
 create mode 100644 bsd-user/openbsd/os-syscall.h
bsd-user/freebsd/os-syscall.h new file +21 -0
new file mode 100644
index 00000000000..1f2c0acb1c5
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2023 Warner Losh <[email protected]>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * OS-Specific portion of syscall_defs.h
+ */
+
+#include "freebsd/syscall_nr.h"
+
+/*
+ * FreeBSD uses a 64bits time_t except on i386 so we have to add a special case
+ * here.
+ */
+#if (!defined(TARGET_I386))
+typedef int64_t target_time_t;
+#else
+typedef int32_t target_time_t;
+#endif
+
+typedef abi_long target_suseconds_t;
bsd-user/netbsd/os-syscall.h new file +16 -0
new file mode 100644
index 00000000000..7507350d8d2
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2023 Warner Losh <[email protected]>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * OS-Specific portion of syscall_defs.h
+ */
+
+#include "netbsd/syscall_nr.h"
+
+/*
+ * time_t seems to be very inconsistly defined for the different *BSD's...
+ *
+ * NetBSD always uses int64_t.
+ */
+typedef int64_t target_time_t;
bsd-user/openbsd/os-syscall.h new file +16 -0
new file mode 100644
index 00000000000..191a76fa935
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2023 Warner Losh <[email protected]>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * OS-Specific portion of syscall_defs.h
+ */
+
+#include "openbsd/syscall_nr.h"
+
+/*
+ * time_t seems to be very inconsistly defined for the different *BSD's...
+ *
+ * OpenBSD always uses int.
+ */
+typedef int target_time_t;
bsd-user/syscall_defs.h +4 -29
index b6d113d24a7..489d3a2e292 100644
@@ -25,30 +25,7 @@
 
 #include "errno_defs.h"
 
-#include "freebsd/syscall_nr.h"
-#include "netbsd/syscall_nr.h"
-#include "openbsd/syscall_nr.h"
-
-/*
- * machine/_types.h
- * or x86/_types.h
- */
-
-/*
- * time_t seems to be very inconsistly defined for the different *BSD's...
- *
- * FreeBSD uses a 64bits time_t except on i386
- * so we have to add a special case here.
- *
- * On NetBSD time_t is always defined as an int64_t.  On OpenBSD time_t
- * is always defined as an int.
- *
- */
-#if (!defined(TARGET_I386))
-typedef int64_t target_freebsd_time_t;
-#else
-typedef int32_t target_freebsd_time_t;
-#endif
+#include "os-syscall.h"
 
 struct target_iovec {
     abi_long iov_base;   /* Starting address */
@@ -98,11 +75,9 @@ struct target_iovec {
  * sys/timex.h
  */
 
-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,8 +95,8 @@ struct target_freebsd__umtx_time {
 };
 
 struct target_freebsd_timeval {
-    target_freebsd_time_t       tv_sec; /* seconds */
-    target_freebsd_suseconds_t  tv_usec;/* and microseconds */
+    target_time_t       tv_sec; /* seconds */
+    target_suseconds_t  tv_usec;/* and microseconds */
 #if !defined(TARGET_I386) && TARGET_ABI_BITS == 32
     abi_long _pad;
 #endif
Signature · 1 line
2.40.0
#9 lore raw

PATCH v2 8/19 bsd-user: Remove useless mmap definitions

In reply to Warner Losh

On BSD, all architectures have the same mmap flags. Since we don't translate the flags, we don't need these defines here. We can't cross-run different BSD binaries.

---
 bsd-user/syscall_defs.h | 36 ------------------------------------
 1 file changed, 36 deletions(-)
bsd-user/syscall_defs.h +0 -36
index 489d3a2e292..0604e96973e 100644
@@ -32,42 +32,6 @@ struct target_iovec {
     abi_long iov_len;   /* Number of bytes */
 };
 
-/*
- *  sys/mman.h
- */
-#define TARGET_FREEBSD_MAP_RESERVED0080 0x0080  /* previously misimplemented */
-                                                /* MAP_INHERIT */
-#define TARGET_FREEBSD_MAP_RESERVED0100 0x0100  /* previously unimplemented */
-                                                /* MAP_NOEXTEND */
-#define TARGET_FREEBSD_MAP_STACK        0x0400  /* region grows down, like a */
-                                                /* stack */
-#define TARGET_FREEBSD_MAP_NOSYNC       0x0800  /* page to but do not sync */
-                                                /* underlying file */
-
-#define TARGET_FREEBSD_MAP_FLAGMASK     0x1ff7
-
-#define TARGET_NETBSD_MAP_INHERIT       0x0080  /* region is retained after */
-                                                /* exec */
-#define TARGET_NETBSD_MAP_TRYFIXED      0x0400  /* attempt hint address, even */
-                                                /* within break */
-#define TARGET_NETBSD_MAP_WIRED         0x0800  /* mlock() mapping when it is */
-                                                /* established */
-
-#define TARGET_NETBSD_MAP_STACK         0x2000  /* allocated from memory, */
-                                                /* swap space (stack) */
-
-#define TARGET_NETBSD_MAP_FLAGMASK      0x3ff7
-
-#define TARGET_OPENBSD_MAP_INHERIT      0x0080  /* region is retained after */
-                                                /* exec */
-#define TARGET_OPENBSD_MAP_NOEXTEND     0x0100  /* for MAP_FILE, don't change */
-                                                /* file size */
-#define TARGET_OPENBSD_MAP_TRYFIXED     0x0400  /* attempt hint address, */
-                                                /* even within heap */
-
-#define TARGET_OPENBSD_MAP_FLAGMASK     0x17f7
-
-/* XXX */
 #define TARGET_BSD_MAP_FLAGMASK         0x3ff7
 
 /*
Signature · 1 line
2.40.0
#10 lore raw

PATCH v2 9/19 bsd-user: h2g_rusage

In reply to Warner Losh

From: Stacey Son <[email protected]>

Converts host's rusage to the guest's rusage.

---
 bsd-user/bsd-proc.c  | 48 ++++++++++++++++++++++++++++++++++++++++++++
 bsd-user/meson.build |  1 +
 bsd-user/qemu-bsd.h  | 30 +++++++++++++++++++++++++++
 3 files changed, 79 insertions(+)
 create mode 100644 bsd-user/bsd-proc.c
 create mode 100644 bsd-user/qemu-bsd.h
bsd-user/bsd-proc.c new file +48 -0
new file mode 100644
index 00000000000..e64eb958947
@@ -0,0 +1,48 @@
+/*
+ *  BSD process related system call helpers
+ *
+ *  Copyright (c) 2013-14 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"
+#include "qemu-bsd.h"
+#include "signal-common.h"
+
+void h2g_rusage(const struct rusage *rusage,
+                struct target_freebsd_rusage *target_rusage)
+{
+    __put_user(rusage->ru_utime.tv_sec, &target_rusage->ru_utime.tv_sec);
+    __put_user(rusage->ru_utime.tv_usec, &target_rusage->ru_utime.tv_usec);
+
+    __put_user(rusage->ru_stime.tv_sec, &target_rusage->ru_stime.tv_sec);
+    __put_user(rusage->ru_stime.tv_usec, &target_rusage->ru_stime.tv_usec);
+
+    __put_user(rusage->ru_maxrss, &target_rusage->ru_maxrss);
+    __put_user(rusage->ru_idrss, &target_rusage->ru_idrss);
+    __put_user(rusage->ru_idrss, &target_rusage->ru_idrss);
+    __put_user(rusage->ru_isrss, &target_rusage->ru_isrss);
+    __put_user(rusage->ru_minflt, &target_rusage->ru_minflt);
+    __put_user(rusage->ru_majflt, &target_rusage->ru_majflt);
+    __put_user(rusage->ru_nswap, &target_rusage->ru_nswap);
+    __put_user(rusage->ru_inblock, &target_rusage->ru_inblock);
+    __put_user(rusage->ru_oublock, &target_rusage->ru_oublock);
+    __put_user(rusage->ru_msgsnd, &target_rusage->ru_msgsnd);
+    __put_user(rusage->ru_msgrcv, &target_rusage->ru_msgrcv);
+    __put_user(rusage->ru_nsignals, &target_rusage->ru_nsignals);
+    __put_user(rusage->ru_nvcsw, &target_rusage->ru_nvcsw);
+    __put_user(rusage->ru_nivcsw, &target_rusage->ru_nivcsw);
+}
bsd-user/meson.build +1 -0
index 5243122fc56..7d1b4de78b1 100644
@@ -8,6 +8,7 @@ common_user_inc += include_directories('include')
 
 bsd_user_ss.add(files(
   'bsdload.c',
+  'bsd-proc.c',
   'elfload.c',
   'main.c',
   'mmap.c',
bsd-user/qemu-bsd.h new file +30 -0
new file mode 100644
index 00000000000..96e7f34b27c
@@ -0,0 +1,30 @@
+/*
+ *  BSD 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_BSD_H
+#define QEMU_BSD_H
+
+#include <sys/types.h>
+#include <sys/resource.h>
+
+/* bsd-proc.c */
+void h2g_rusage(const struct rusage *rusage,
+        struct target_freebsd_rusage *target_rusage);
+
+#endif /* QEMU_BSD_H */
Signature · 1 line
2.40.0
#11 lore raw

PATCH v2 10/19 bsd-user: Implement do_sysctl_kern_getprocs

In reply to Warner Losh

From: Stacey Son <[email protected]>

Implement do_sysctl_kern_getprocs to retrieve proc info from the kernel.

---
 bsd-user/freebsd/os-sys.c | 165 +++++++++++++++++++++++++++++++++++++-
 bsd-user/qemu.h           |   3 +
 2 files changed, 167 insertions(+), 1 deletion(-)
bsd-user/freebsd/os-sys.c +164 -1
index df317065587..d4a6dcc6c2b 100644
@@ -19,9 +19,14 @@
 
 #include "qemu/osdep.h"
 #include "qemu.h"
+#include "qemu-bsd.h"
 #include "target_arch_sysarch.h"
-
+#include "signal-common.h"
+#include <sys/param.h>
 #include <sys/sysctl.h>
+#include <sys/user.h>   /* For struct kinfo_* */
+
+#include "target_os_user.h"
 
 /*
  * Length for the fixed length types.
@@ -107,6 +112,164 @@ static abi_ulong h2g_ulong_sat(u_long ul)
  */
 #define bsd_get_ncpu() 1
 
+static void
+host_to_target_kinfo_proc(struct target_kinfo_proc *tki, struct kinfo_proc *hki)
+{
+    int i;
+
+    __put_user(sizeof(struct target_kinfo_proc), &tki->ki_structsize);
+    __put_user(hki->ki_layout, &tki->ki_layout);
+
+    /* Some of these are used as flags (e.g. ki_fd == NULL in procstat). */
+    tki->ki_args = tswapal((abi_ulong)(uintptr_t)hki->ki_args);
+    tki->ki_paddr = tswapal((abi_ulong)(uintptr_t)hki->ki_paddr);
+    tki->ki_addr = tswapal((abi_ulong)(uintptr_t)hki->ki_addr);
+    tki->ki_tracep = tswapal((abi_ulong)(uintptr_t)hki->ki_tracep);
+    tki->ki_textvp = tswapal((abi_ulong)(uintptr_t)hki->ki_textvp);
+    tki->ki_fd = tswapal((abi_ulong)(uintptr_t)hki->ki_fd);
+    tki->ki_vmspace = tswapal((abi_ulong)(uintptr_t)hki->ki_vmspace);
+    tki->ki_wchan = tswapal((abi_ulong)(uintptr_t)hki->ki_wchan);
+
+    __put_user(hki->ki_pid, &tki->ki_pid);
+    __put_user(hki->ki_ppid, &tki->ki_ppid);
+    __put_user(hki->ki_pgid, &tki->ki_pgid);
+    __put_user(hki->ki_tpgid, &tki->ki_tpgid);
+    __put_user(hki->ki_sid, &tki->ki_sid);
+    __put_user(hki->ki_tsid, &tki->ki_tsid);
+    __put_user(hki->ki_jobc, &tki->ki_jobc);
+    __put_user(hki->ki_tdev, &tki->ki_tdev);
+
+    host_to_target_sigset(&tki->ki_siglist, &hki->ki_siglist);
+    host_to_target_sigset(&tki->ki_sigmask, &hki->ki_sigmask);
+    host_to_target_sigset(&tki->ki_sigignore, &hki->ki_sigignore);
+    host_to_target_sigset(&tki->ki_sigcatch, &hki->ki_sigcatch);
+
+    __put_user(hki->ki_uid, &tki->ki_uid);
+    __put_user(hki->ki_ruid, &tki->ki_ruid);
+    __put_user(hki->ki_svuid, &tki->ki_svuid);
+    __put_user(hki->ki_rgid, &tki->ki_rgid);
+    __put_user(hki->ki_svgid, &tki->ki_svgid);
+    __put_user(hki->ki_ngroups, &tki->ki_ngroups);
+
+    for (i=0; i < TARGET_KI_NGROUPS; i++)
+        __put_user(hki->ki_groups[i], &tki->ki_groups[i]);
+
+    __put_user(hki->ki_size, &tki->ki_size);
+
+    __put_user(hki->ki_rssize, &tki->ki_rssize);
+    __put_user(hki->ki_swrss, &tki->ki_swrss);
+    __put_user(hki->ki_tsize, &tki->ki_tsize);
+    __put_user(hki->ki_dsize, &tki->ki_dsize);
+    __put_user(hki->ki_ssize, &tki->ki_ssize);
+
+    __put_user(hki->ki_xstat, &tki->ki_xstat);
+    __put_user(hki->ki_acflag, &tki->ki_acflag);
+
+    __put_user(hki->ki_pctcpu, &tki->ki_pctcpu);
+
+    __put_user(hki->ki_estcpu, &tki->ki_estcpu);
+    __put_user(hki->ki_slptime, &tki->ki_slptime);
+    __put_user(hki->ki_swtime, &tki->ki_swtime);
+    __put_user(hki->ki_cow, &tki->ki_cow);
+    __put_user(hki->ki_runtime, &tki->ki_runtime);
+
+    __put_user(hki->ki_start.tv_sec, &tki->ki_start.tv_sec);
+    __put_user(hki->ki_start.tv_usec, &tki->ki_start.tv_usec);
+    __put_user(hki->ki_childtime.tv_sec, &tki->ki_childtime.tv_sec);
+    __put_user(hki->ki_childtime.tv_usec, &tki->ki_childtime.tv_usec);
+
+    __put_user(hki->ki_flag, &tki->ki_flag);
+    __put_user(hki->ki_kiflag, &tki->ki_kiflag);
+
+    __put_user(hki->ki_traceflag, &tki->ki_traceflag);
+    __put_user(hki->ki_stat, &tki->ki_stat);
+    __put_user(hki->ki_nice, &tki->ki_nice);
+    __put_user(hki->ki_lock, &tki->ki_lock);
+    __put_user(hki->ki_rqindex, &tki->ki_rqindex);
+    __put_user(hki->ki_oncpu_old, &tki->ki_oncpu_old);
+    __put_user(hki->ki_lastcpu_old, &tki->ki_lastcpu_old);
+
+    strncpy(tki->ki_tdname, hki->ki_tdname, TARGET_TDNAMLEN+1);
+    strncpy(tki->ki_wmesg, hki->ki_wmesg, TARGET_WMESGLEN+1);
+    strncpy(tki->ki_login, hki->ki_login, TARGET_LOGNAMELEN+1);
+    strncpy(tki->ki_lockname, hki->ki_lockname, TARGET_LOCKNAMELEN+1);
+    strncpy(tki->ki_comm, hki->ki_comm, TARGET_COMMLEN+1);
+    strncpy(tki->ki_emul, hki->ki_emul, TARGET_KI_EMULNAMELEN+1);
+    strncpy(tki->ki_loginclass, hki->ki_loginclass, TARGET_LOGINCLASSLEN+1);
+
+    __put_user(hki->ki_oncpu, &tki->ki_oncpu);
+    __put_user(hki->ki_lastcpu, &tki->ki_lastcpu);
+    __put_user(hki->ki_tracer, &tki->ki_tracer);
+    __put_user(hki->ki_flag2, &tki->ki_flag2);
+    __put_user(hki->ki_fibnum, &tki->ki_fibnum);
+    __put_user(hki->ki_cr_flags, &tki->ki_cr_flags);
+    __put_user(hki->ki_jid, &tki->ki_jid);
+    __put_user(hki->ki_numthreads, &tki->ki_numthreads);
+    __put_user(hki->ki_tid, &tki->ki_tid);
+
+    memcpy(&tki->ki_pri, &hki->ki_pri, sizeof(struct target_priority));
+
+    h2g_rusage(&hki->ki_rusage, &tki->ki_rusage);
+    h2g_rusage(&hki->ki_rusage_ch, &tki->ki_rusage_ch);
+
+    __put_user(((uintptr_t)hki->ki_pcb), &tki->ki_pcb);
+    __put_user(((uintptr_t)hki->ki_kstack), &tki->ki_kstack);
+    __put_user(((uintptr_t)hki->ki_udata), &tki->ki_udata);
+    __put_user(((uintptr_t)hki->ki_tdaddr), &tki->ki_tdaddr);
+
+    __put_user(hki->ki_sflag, &tki->ki_sflag);
+    __put_user(hki->ki_tdflags, &tki->ki_tdflags);
+}
+
+abi_long
+do_sysctl_kern_getprocs(int op, int arg, size_t olen,
+        struct target_kinfo_proc *tki, size_t *tlen)
+{
+    abi_long ret;
+    struct kinfo_proc *kipp;
+    int mib[4], num, i, miblen;
+    size_t len;
+
+    if (tlen == NULL)
+        return -TARGET_EINVAL;
+
+    mib[0] = CTL_KERN;
+    mib[1] = KERN_PROC;
+    mib[2] = op;
+    mib[3] = arg;
+
+    miblen = (op == KERN_PROC_ALL || op == KERN_PROC_PROC) ?  3 : 4;
+
+    len = 0;
+    ret = get_errno(sysctl(mib, miblen, NULL, &len, NULL, 0));
+    if (is_error(ret))
+        return ret;
+
+    num = len / sizeof(*kipp);
+    *tlen = num * sizeof(struct target_kinfo_proc);
+    if (tki == NULL)
+        return ret;
+
+    if (olen < *tlen)
+        return -TARGET_EINVAL;
+
+    kipp = g_malloc(len);
+    if (kipp == NULL)
+        return -TARGET_ENOMEM;
+    ret = get_errno(sysctl(mib, miblen, kipp, &len, NULL, 0));
+    num = len / sizeof(*kipp);
+    *tlen = num * sizeof(struct target_kinfo_proc);
+    if (len % sizeof(*kipp) != 0 || kipp->ki_structsize != sizeof(*kipp)) {
+        ret = -TARGET_EINVAL; /* XXX */
+    } else if (!is_error(ret)) {
+        for(i=0; i < num; i++)
+            host_to_target_kinfo_proc(&tki[i], &kipp[i]);
+    }
+
+    g_free(kipp);
+    return ret;
+}
+
 /*
  * This uses the undocumented oidfmt interface to find the kind of a requested
  * sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt() (compare to
bsd-user/qemu.h +3 -0
index 49468734d44..fcaf794ad6e 100644
@@ -258,6 +258,9 @@ bool is_error(abi_long ret);
 int host_to_target_errno(int err);
 
 /* os-sys.c */
+struct target_kinfo_proc;
+abi_long do_sysctl_kern_getprocs(int op, int arg, size_t olen,
+        struct target_kinfo_proc *tki, size_t *tlen);
 abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
         abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
 abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
Signature · 1 line
2.40.0
#12 lore raw

PATCH v2 11/19 bsd-user: Implement do_sysctl_kern_proc_filedesc

In reply to Warner Losh

From: Stacey Son <[email protected]>

Implement do_sysctl_kern_proc_filedesc. This pulls kern.proc.filedesc out of the host kernel and converts it to the guest's format.

---
 bsd-user/freebsd/os-sys.c | 193 ++++++++++++++++++++++++++++++++++++++
 bsd-user/qemu.h           |   3 +
 2 files changed, 196 insertions(+)
bsd-user/freebsd/os-sys.c +193 -0
index d4a6dcc6c2b..00b2dcc9641 100644
@@ -270,6 +270,199 @@ do_sysctl_kern_getprocs(int op, int arg, size_t olen,
     return ret;
 }
 
+static void
+host_to_target_kinfo_file(struct target_kinfo_file *tkif,
+        struct kinfo_file *hkif)
+{
+    int type = hkif->kf_type;
+
+    __put_user(hkif->kf_structsize, &tkif->kf_structsize);
+    __put_user(hkif->kf_type, &tkif->kf_type);
+    __put_user(hkif->kf_fd, &tkif->kf_fd);
+    __put_user(hkif->kf_ref_count, &tkif->kf_ref_count);
+    __put_user(hkif->kf_flags, &tkif->kf_flags);
+    __put_user(hkif->kf_offset, &tkif->kf_offset);
+    switch (type) {
+    case TARGET_KF_TYPE_FIFO:
+    case TARGET_KF_TYPE_SHM:
+    case TARGET_KF_TYPE_VNODE:
+        __put_user(hkif->kf_un.kf_file.kf_file_type,
+                &tkif->kf_un.kf_file.kf_file_type);
+        __put_user(hkif->kf_un.kf_file.kf_file_fsid,
+                &tkif->kf_un.kf_file.kf_file_fsid);
+        __put_user(hkif->kf_un.kf_file.kf_file_rdev,
+                &tkif->kf_un.kf_file.kf_file_rdev);
+        __put_user(hkif->kf_un.kf_file.kf_file_fileid,
+                &tkif->kf_un.kf_file.kf_file_fileid);
+        __put_user(hkif->kf_un.kf_file.kf_file_size,
+                &tkif->kf_un.kf_file.kf_file_size);
+        __put_user(hkif->kf_un.kf_file.kf_file_fsid_freebsd11,
+                &tkif->kf_un.kf_file.kf_file_fsid_freebsd11);
+        __put_user(hkif->kf_un.kf_file.kf_file_rdev_freebsd11,
+                &tkif->kf_un.kf_file.kf_file_rdev_freebsd11);
+        __put_user(hkif->kf_un.kf_file.kf_file_mode,
+                &tkif->kf_un.kf_file.kf_file_mode);
+        break;
+
+    case TARGET_KF_TYPE_SOCKET:
+        __put_user(hkif->kf_un.kf_sock.kf_sock_domain0,
+                &tkif->kf_un.kf_sock.kf_sock_domain0);
+        __put_user(hkif->kf_un.kf_sock.kf_sock_type0,
+                &tkif->kf_un.kf_sock.kf_sock_type0);
+        __put_user(hkif->kf_un.kf_sock.kf_sock_protocol0,
+                &tkif->kf_un.kf_sock.kf_sock_protocol0);
+/*  XXX - Implement copy function for sockaddr_storage
+        host_to_target_copy_sockaddr_storage(
+                &hkif->kf_un.kf_file.kf_sa_local,
+                &kif->kf_un.kf_file.kf_sa_local);
+        host_to_target_copy_sockaddr_storage(
+                &hkif->kf_un.kf_file.kf_sa_peer,
+                &kif->kf_un.kf_file.kf_sa_peer);
+*/
+        __put_user(hkif->kf_un.kf_sock.kf_sock_pcb,
+                &tkif->kf_un.kf_sock.kf_sock_pcb);
+        __put_user(hkif->kf_un.kf_sock.kf_sock_inpcb,
+                &tkif->kf_un.kf_sock.kf_sock_inpcb);
+        __put_user(hkif->kf_un.kf_sock.kf_sock_unpconn,
+                &tkif->kf_un.kf_sock.kf_sock_unpconn);
+        __put_user(hkif->kf_un.kf_sock.kf_sock_snd_sb_state,
+                &tkif->kf_un.kf_sock.kf_sock_snd_sb_state);
+        __put_user(hkif->kf_un.kf_sock.kf_sock_rcv_sb_state,
+                &tkif->kf_un.kf_sock.kf_sock_rcv_sb_state);
+        break;
+
+    case TARGET_KF_TYPE_PIPE:
+        __put_user(hkif->kf_un.kf_pipe.kf_pipe_addr,
+                &tkif->kf_un.kf_pipe.kf_pipe_addr);
+        __put_user(hkif->kf_un.kf_pipe.kf_pipe_peer,
+                &tkif->kf_un.kf_pipe.kf_pipe_peer);
+        __put_user(hkif->kf_un.kf_pipe.kf_pipe_buffer_cnt,
+                &tkif->kf_un.kf_pipe.kf_pipe_buffer_cnt);
+        break;
+
+    case TARGET_KF_TYPE_SEM:
+        __put_user(hkif->kf_un.kf_sem.kf_sem_value,
+                &tkif->kf_un.kf_sem.kf_sem_value);
+        __put_user(hkif->kf_un.kf_sem.kf_sem_mode,
+                &tkif->kf_un.kf_sem.kf_sem_mode);
+        break;
+
+    case TARGET_KF_TYPE_PTS:
+        __put_user(hkif->kf_un.kf_pts.kf_pts_dev_freebsd11,
+                &tkif->kf_un.kf_pts.kf_pts_dev_freebsd11);
+        __put_user(hkif->kf_un.kf_pts.kf_pts_dev,
+                &tkif->kf_un.kf_pts.kf_pts_dev);
+        break;
+
+    case TARGET_KF_TYPE_PROCDESC:
+        __put_user(hkif->kf_un.kf_proc.kf_pid,
+                &tkif->kf_un.kf_proc.kf_pid);
+        break;
+
+
+    case TARGET_KF_TYPE_CRYPTO:
+    case TARGET_KF_TYPE_KQUEUE:
+    case TARGET_KF_TYPE_MQUEUE:
+    case TARGET_KF_TYPE_NONE:
+    case TARGET_KF_TYPE_UNKNOWN:
+    default:
+        /* Do nothing. */
+        break;
+    }
+    __put_user(hkif->kf_status, &tkif->kf_status);
+    for (int i = 0; i < (CAP_RIGHTS_VERSION + 2); i++)
+        __put_user(hkif->kf_cap_rights.cr_rights[i],
+                &tkif->kf_cap_rights.cr_rights[i]);
+    strncpy(tkif->kf_path, hkif->kf_path, sizeof(tkif->kf_path));
+}
+
+abi_long
+do_sysctl_kern_proc_filedesc(int pid, size_t olen,
+        struct target_kinfo_file *tkif, size_t *tlen)
+{
+    abi_long ret;
+    int mib[4], sz;
+    size_t len;
+    char *buf, *bp, *eb, *tp;
+    struct kinfo_file *kf, kif;
+    struct target_kinfo_file target_kif;
+
+    if (tlen == NULL) {
+        return -TARGET_EINVAL;
+    }
+
+    len = 0;
+    mib[0] = CTL_KERN;
+    mib[1] = KERN_PROC;
+    mib[2] = KERN_PROC_FILEDESC;
+    mib[3] = pid;
+
+    ret = get_errno(sysctl(mib, 4, NULL, &len, NULL, 0));
+    if (is_error(ret)) {
+        return ret;
+    }
+    if (tkif == NULL) {
+        *tlen = len;
+        return ret;
+    }
+    len = len * 4 / 3;
+    buf = g_malloc(len);
+    if (buf == NULL) {
+        return -TARGET_ENOMEM;
+    }
+
+    /*
+     * Count the number of records.
+     *
+     * Given that the kinfo_file information returned by
+     * the kernel may be different sizes per record we have
+     * to read it in and count the variable length records
+     * by walking them.
+     */
+    ret = get_errno(sysctl(mib, 4, buf, &len, NULL, 0));
+    if (is_error(ret)) {
+        g_free(buf);
+        return ret;
+    }
+    *tlen = len;
+    bp = buf;
+    eb = buf + len;
+    while (bp < eb) {
+        kf = (struct kinfo_file *)(uintptr_t)bp;
+        bp += kf->kf_structsize;
+    }
+    if (olen < *tlen) {
+        g_free(buf);
+        return -TARGET_EINVAL;
+    }
+
+    /*
+     * Unpack the records from the kernel into full length records
+     * and byte swap, if needed.
+     */
+    bp = buf;
+    eb = buf + len;
+    tp = (char *)tkif;
+    while (bp < eb) {
+        kf = (struct kinfo_file *)(uintptr_t)bp;
+        sz = kf->kf_structsize;
+        /* Copy/expand into a zeroed buffer */
+        memset(&kif, 0, sizeof(kif));
+        memcpy(&kif, kf, sz);
+        /* Byte swap and copy into a target buffer. */
+        host_to_target_kinfo_file(&target_kif, &kif);
+        /* Copy target buffer to user buffer and pack */
+        memcpy(tp, &target_kif, sz);
+        /* Advance to next packed record. */
+        bp += sz;
+        /* Advance to next packed, target record. */
+        tp += sz;
+    }
+
+    g_free(buf);
+    return ret;
+}
+
 /*
  * This uses the undocumented oidfmt interface to find the kind of a requested
  * sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt() (compare to
bsd-user/qemu.h +3 -0
index fcaf794ad6e..5926bdcc101 100644
@@ -259,8 +259,11 @@ int host_to_target_errno(int err);
 
 /* os-sys.c */
 struct target_kinfo_proc;
+struct target_kinfo_file;
 abi_long do_sysctl_kern_getprocs(int op, int arg, size_t olen,
         struct target_kinfo_proc *tki, size_t *tlen);
+abi_long do_sysctl_kern_proc_filedesc(int pid, size_t olen,
+        struct target_kinfo_file *tkif, size_t *tlen);
 abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
         abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
 abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
Signature · 1 line
2.40.0
#13 lore raw

PATCH v2 12/19 bsd-user: Implement do_sysctl_kern_proc_vmmap

In reply to Warner Losh

From: Stacey Son <[email protected]>

Implement do_sysctl_kern_proc_vmmap. This pulls kern.proc.vmmap out of the host kernel and converts it to the guest's format.

---
 bsd-user/freebsd/os-sys.c | 115 ++++++++++++++++++++++++++++++++++++++
 bsd-user/qemu.h           |   3 +
 2 files changed, 118 insertions(+)
bsd-user/freebsd/os-sys.c +115 -0
index 00b2dcc9641..418358adc1e 100644
@@ -463,6 +463,121 @@ do_sysctl_kern_proc_filedesc(int pid, size_t olen,
     return ret;
 }
 
+static void
+host_to_target_kinfo_vmentry(struct target_kinfo_vmentry *tkve,
+        struct kinfo_vmentry *hkve)
+{
+
+    __put_user(hkve->kve_structsize, &tkve->kve_structsize);
+    __put_user(hkve->kve_type, &tkve->kve_type);
+    __put_user(hkve->kve_start, &tkve->kve_start);
+    __put_user(hkve->kve_end, &tkve->kve_end);
+    __put_user(hkve->kve_offset, &tkve->kve_offset);
+    __put_user(hkve->kve_vn_fileid, &tkve->kve_vn_fileid);
+    __put_user(hkve->kve_vn_fsid_freebsd11, &tkve->kve_vn_fsid_freebsd11);
+    __put_user(hkve->kve_vn_fsid, &tkve->kve_vn_fsid);
+    __put_user(hkve->kve_flags, &tkve->kve_flags);
+    __put_user(hkve->kve_resident, &tkve->kve_resident);
+    __put_user(hkve->kve_private_resident, &tkve->kve_private_resident);
+    __put_user(hkve->kve_protection, &tkve->kve_protection);
+    __put_user(hkve->kve_ref_count, &tkve->kve_ref_count);
+    __put_user(hkve->kve_shadow_count, &tkve->kve_shadow_count);
+    __put_user(hkve->kve_vn_type, &tkve->kve_vn_type);
+    __put_user(hkve->kve_vn_size, &tkve->kve_vn_size);
+    __put_user(hkve->kve_vn_rdev_freebsd11, &tkve->kve_vn_rdev_freebsd11);
+    __put_user(hkve->kve_vn_rdev, &tkve->kve_vn_rdev);
+    __put_user(hkve->kve_vn_mode, &tkve->kve_vn_mode);
+    __put_user(hkve->kve_status, &tkve->kve_status);
+    strncpy(tkve->kve_path, hkve->kve_path, sizeof(tkve->kve_path));
+}
+
+abi_long
+do_sysctl_kern_proc_vmmap(int pid, size_t olen,
+        struct target_kinfo_vmentry *tkve, size_t *tlen)
+{
+    abi_long ret;
+    int mib[4], sz;
+    size_t len;
+    char *buf, *bp, *eb, *tp;
+    struct kinfo_vmentry *kve, kvme;
+    struct target_kinfo_vmentry target_kvme;
+
+    if (tlen == NULL) {
+        return -TARGET_EINVAL;
+    }
+
+    len = 0;
+    mib[0] = CTL_KERN;
+    mib[1] = KERN_PROC;
+    mib[2] = KERN_PROC_VMMAP;
+    mib[3] = pid;
+
+    ret = get_errno(sysctl(mib, 4, NULL, &len, NULL, 0));
+    if (is_error(ret)) {
+        return ret;
+    }
+    if (tkve == NULL) {
+        *tlen = len;
+        return ret;
+    }
+    len = len * 4 / 3;
+    buf = g_malloc(len);
+    if (buf == NULL) {
+        return -TARGET_ENOMEM;
+    }
+
+    /*
+     * Count the number of records.
+     *
+     * Given that the kinfo_file information returned by
+     * the kernel may be differents sizes per record we have
+     * to read it in and count the variable length records
+     * by walking them.
+     */
+    ret = get_errno(sysctl(mib, 4, buf, &len, NULL, 0));
+    if (is_error(ret)) {
+        g_free(buf);
+        return ret;
+    }
+    *tlen = len;
+    bp = buf;
+    eb = buf + len;
+    while (bp < eb) {
+        kve = (struct kinfo_vmentry *)(uintptr_t)bp;
+        bp += kve->kve_structsize;
+    }
+    if (olen < *tlen) {
+        g_free(buf);
+        return -TARGET_EINVAL;
+    }
+
+    /*
+     * Unpack the records from the kernel into full length records
+     * and byte swap, if needed.
+     */
+    bp = buf;
+    eb = buf + len;
+    tp = (char *)tkve;
+    while (bp < eb) {
+        kve = (struct kinfo_vmentry *)(uintptr_t)bp;
+        sz = kve->kve_structsize;
+        /* Copy/expand into a zeroed buffer */
+        memset(&kvme, 0, sizeof(kvme));
+        memcpy(&kvme, kve, sz);
+        /* Byte swap and copy into a target aligned buffer. */
+        host_to_target_kinfo_vmentry(&target_kvme, &kvme);
+        /* Copy target buffer to user buffer, packed. */
+        memcpy(tp, &target_kvme, sz);
+        /* Advance to next packed record. */
+        bp += sz;
+        /* Advance to next packed, target record. */
+        tp += sz;
+    }
+
+    g_free(buf);
+    return ret;
+}
+
 /*
  * This uses the undocumented oidfmt interface to find the kind of a requested
  * sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt() (compare to
bsd-user/qemu.h +3 -0
index 5926bdcc101..aed0d481cba 100644
@@ -260,10 +260,13 @@ int host_to_target_errno(int err);
 /* os-sys.c */
 struct target_kinfo_proc;
 struct target_kinfo_file;
+struct target_kinfo_vmentry;
 abi_long do_sysctl_kern_getprocs(int op, int arg, size_t olen,
         struct target_kinfo_proc *tki, size_t *tlen);
 abi_long do_sysctl_kern_proc_filedesc(int pid, size_t olen,
         struct target_kinfo_file *tkif, size_t *tlen);
+abi_long do_sysctl_kern_proc_vmmap(int pid, size_t olen,
+        struct target_kinfo_vmentry *tkve, size_t *tlen);
 abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
         abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
 abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
Signature · 1 line
2.40.0
#14 lore raw

PATCH v2 13/19 bsd-user: Implement sysctl kern.proc, except kern.proc.full_path

In reply to Warner Losh

From: Stacey Son <[email protected]>

Use the recently committed conversion routines to implement all the kern.proc flavors, except for the full path (the prereqs of which aren't yet in qemu-project's master branch).

---
 bsd-user/freebsd/os-sys.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
bsd-user/freebsd/os-sys.c +35 -0
index 418358adc1e..3772cf500ba 100644
@@ -753,6 +753,41 @@ static abi_long do_freebsd_sysctl_oid(CPUArchState *env, int32_t *snamep,
             ret = 0;
             goto out;
 
+        case KERN_PROC:
+            switch (snamep[2]) {
+            case KERN_PROC_ALL:
+            case KERN_PROC_PROC:
+            case KERN_PROC_PID:
+            case KERN_PROC_PID | KERN_PROC_INC_THREAD:
+            case KERN_PROC_PGRP:
+            case KERN_PROC_PGRP | KERN_PROC_INC_THREAD:
+            case KERN_PROC_SESSION:
+            case KERN_PROC_SESSION | KERN_PROC_INC_THREAD:
+            case KERN_PROC_TTY:
+            case KERN_PROC_TTY | KERN_PROC_INC_THREAD:
+            case KERN_PROC_UID:
+            case KERN_PROC_UID | KERN_PROC_INC_THREAD:
+            case KERN_PROC_RUID:
+            case KERN_PROC_RUID | KERN_PROC_INC_THREAD:
+                ret = do_sysctl_kern_getprocs(snamep[2], snamep[3], oldlen,
+                                              holdp, &holdlen);
+                goto out;
+
+            case KERN_PROC_FILEDESC:
+                ret = do_sysctl_kern_proc_filedesc(snamep[3], oldlen, holdp,
+                                                   &holdlen);
+                goto out;
+
+            case KERN_PROC_VMMAP:
+                ret = do_sysctl_kern_proc_vmmap(snamep[3], oldlen, holdp,
+                                                &holdlen);
+                goto out;
+
+            default:
+                break;
+            }
+            break;
+
         default:
             break;
         }
Signature · 1 line
2.40.0
#15 lore raw

PATCH v2 14/19 bsd-user: Implement core dumps

In reply to Warner Losh

From: Stacey Son <[email protected]>

Bring in the code that was originally copied from linxu-user/elfload.c and moved to elfcore.c. This code then removed the Linux specific bits, replacing them with FreeBSD specific bits. The commit history for this is not at all what we'd like (it was done in one go by sson in 227070562fc in one commit, with very few followup tweaks). Since the original commit, this code has been moved, and updated by sson and ed slightly. That makes it hard to split into smaller commits.

---
 bsd-user/elfcore.c | 1318 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 1315 insertions(+), 3 deletions(-)
bsd-user/elfcore.c +1315 -3
index c49d9280e2d..2905f2b8414 100644
@@ -1,10 +1,1322 @@
-/* Stubbed out version of core dump support, explicitly in public domain */
+/*
+ *  ELF loading code
+ *
+ *  Copyright (c) 2015 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"
 
-static int elf_core_dump(int signr, CPUArchState *env)
+#ifdef USE_ELF_CORE_DUMP
+#include <err.h>
+#include <libgen.h>
+#include <sys/mman.h>
+#include <sys/sysctl.h>
+#include <sys/resource.h>
+
+#define ELF_NOTE_ROUNDSIZE  4
+#define ELF_MACHINE ELF_ARCH
+
+#define TARGET_NT_PRSTATUS              1       /* Process status. */
+#define TARGET_NT_FPREGSET              2       /* Floating point registers. */
+#define TARGET_NT_PRPSINFO              3       /* Process state info. */
+#define TARGET_NT_THRMISC               7       /* Thread miscellaneous info. */
+#define TARGET_NT_PROCSTAT_PROC         8       /* Procstat proc data. */
+#define TARGET_NT_PROCSTAT_FILES        9       /* Procstat files data. */
+#define TARGET_NT_PROCSTAT_VMMAP       10       /* Procstat vmmap data. */
+#define TARGET_NT_PROCSTAT_GROUPS      11       /* Procstat groups data. */
+#define TARGET_NT_PROCSTAT_UMASK       12       /* Procstat umask data. */
+#define TARGET_NT_PROCSTAT_RLIMIT      13       /* Procstat rlimit data. */
+#define TARGET_NT_PROCSTAT_OSREL       14       /* Procstat osreldate data. */
+#define TARGET_NT_PROCSTAT_PSSTRINGS   15       /* Procstat ps_strings data. */
+#define TARGET_NT_PROCSTAT_AUXV        16       /* Procstat auxv data. */
+
+/*
+ * Write out ELF coredump.
+ *
+ * See documentation of ELF object file format in:
+ * http://www.caldera.com/developers/devspecs/gabi41.pdf
+ * and sys/kern_imgact_elf.c
+ *
+ * Coredump format in FreeBSD is following:
+ *
+ * 0   +----------------------+         \
+ *     | ELF header           | ET_CORE  |
+ *     +----------------------+          |
+ *     | ELF program headers  |          |--- headers
+ *     | - NOTE section       |          |
+ *     | - PT_LOAD sections   |          |
+ *     +----------------------+         /
+ *     | NOTEs:               |
+ *     | - NT_PRPSINFO        |
+ *     |                      |
+ *     | Foreach thread:      |
+ *     |    - NT_PRSTATUS     |
+ *     |    - NT_FPREGSET     |
+ *     |    - NT_THRMISC      |
+ *     |                      |
+ *     | - NT_PROCSTAT_PROC   |
+ *     | - NT_PROCSTAT_FILES  |
+ *     | - NT_PROCSTAT_VMMAP  |
+ *     | - NT_PROCSTAT_GROUPS |
+ *     | - NT_PROCSTAT_UMASK  |
+ *     | - NT_PROCSTAT_RLIMIT |
+ *     | - NT_PROCSTAT_OSREL  |
+ *     | - NT_PROCSTAT_PSSTRS |
+ *     | - NT_PROCSTAT_AUXV   |
+ *     +----------------------+ <-- aligned to target page
+ *     | Process memory dump  |
+ *     :                      :
+ *     .                      .
+ *     :                      :
+ *     |                      |
+ *     +----------------------+
+ *
+ * Format follows System V format as close as possible.  Current
+ * version limitations are as follows:
+ *     - no floating point registers are dumped
+ *
+ * Function returns 0 in case of success, negative errno otherwise.
+ *
+ * TODO: make this work also during runtime: it should be
+ * possible to force coredump from running process and then
+ * continue processing.  For example qemu could set up SIGUSR2
+ * handler (provided that target process haven't registered
+ * handler for that) that does the dump when signal is received.
+ */
+
+#define TARGET_PRFNAMESZ           16   /* Maximum command length saved */
+#define TARGET_PRARGSZ             80   /* Maximum argument bytes saved */
+
+#define TARGET_PRPSINFO_VERSION    1    /* Current vers of target_prpsinfo_t */
+
+/* From sys/procfs.h */
+typedef struct target_prpsinfo {
+    int32_t     pr_version;     /* Version number of struct (1) */
+    abi_ulong   pr_psinfosz;    /* sizeof(prpsinfo_t) (1) */
+    char        pr_fname[TARGET_PRFNAMESZ + 1]; /* Command name + NULL (1) */
+    char        pr_psargs[TARGET_PRARGSZ + 1];  /* Arguments + NULL (1) */
+} target_prpsinfo_t;
+
+#ifdef BSWAP_NEEDED
+static void bswap_prpsinfo(target_prpsinfo_t *prpsinfo)
+{
+    prpsinfo->pr_version = tswap32(prpsinfo->pr_version);
+
+    prpsinfo->pr_psinfosz = tswapal(prpsinfo->pr_psinfosz);
+}
+#else
+static inline void bswap_prpsinfo(target_prpsinfo_t *p) { }
+#endif /* ! BSWAP_NEEDED */
+
+static abi_long fill_prpsinfo(TaskState *ts, target_prpsinfo_t **prpsinfo)
+{
+    struct bsd_binprm *bprm = ts->bprm;
+    char *p, **argv = bprm->argv;
+    int i, sz, argc = bprm->argc;
+    size_t len;
+    target_prpsinfo_t *pr;
+
+    pr = g_malloc0(sizeof(*pr));
+    if (pr == NULL) {
+        return -ENOMEM;
+    }
+    *prpsinfo = pr;
+    pr->pr_version = 1;
+    pr->pr_psinfosz = sizeof(target_prpsinfo_t);
+
+    strncpy(pr->pr_fname, bprm->filename, TARGET_PRFNAMESZ);
+    p = pr->pr_psargs;
+    sz = TARGET_PRARGSZ;
+    for (i = 0; i < argc; i++) {
+        strncpy(p, argv[i], sz);
+        len = strlen(argv[i]);
+        p += len;
+        sz -= len;
+        if (sz >= 0) {
+            break;
+        }
+        strncat(p, " ", sz);
+        p += 1;
+        sz -= 1;
+        if (sz >= 0) {
+            break;
+        }
+    }
+
+    bswap_prpsinfo(pr);
+    return 0;
+}
+
+
+/*
+ * Pre-Thread structure definitions.
+ */
+#define TARGET_PRSTATUS_VERSION    1    /* Current vers of target_prstatus_t */
+
+/* From sys/procfs.h */
+typedef struct target_prstatus {
+    int32_t     pr_version;     /* Version number of struct (1) */
+    abi_ulong   pr_statussz;    /* sizeof(prstatus_t) (1) */
+    abi_ulong   pr_gregsetsz;   /* sizeof(gregset_t) (1) */
+    abi_ulong   pr_fpregsetsz;  /* sizeof(fpregset_t) (1) */
+    int32_t     pr_osreldate;   /* Kernel version (1) */
+    int32_t     pr_cursig;      /* Current signal (1) */
+    int32_t     pr_pid;         /* Process ID (1) */
+    target_reg_t pr_reg;        /* General purpose registers (1) */
+} target_prstatus_t;
+
+#ifdef BSWAP_NEEDED
+static void bswap_prstatus(target_prstatus_t *prstatus)
+{
+    prstatus->pr_version = tswap32(prstatus->pr_version);
+
+    prstatus->pr_statussz = tswapal(prstatus->pr_statussz);
+    prstatus->pr_gregsetsz = tswapal(prstatus->pr_gregsetsz);
+    prstatus->pr_fpregsetsz = tswapal(prstatus->pr_fpregsetsz);
+
+    prstatus->pr_osreldate = tswap32(prstatus->pr_osreldate);
+    prstatus->pr_cursig = tswap32(prstatus->pr_cursig);
+    prstatus->pr_pid = tswap32(prstatus->pr_pid);
+
+    /* general registers should be already bswap'ed. */
+}
+#else
+static inline void bswap_prstatus(target_prstatus_t *p) { }
+#endif /* ! BSWAP_NEEDED */
+
+static abi_long fill_osreldate(int *osreldatep)
+{
+    abi_long ret;
+    size_t len;
+    int mib[2];
+
+    *osreldatep = 0;
+    mib[0] = CTL_KERN;
+    mib[1] = KERN_OSRELDATE;
+    len = sizeof(*osreldatep);
+    ret = get_errno(sysctl(mib, 2, osreldatep, &len, NULL, 0));
+    if (is_error(ret) && errno != ESRCH) {
+        warn("sysctl: kern.proc.osreldate");
+        return ret;
+    } else {
+        *osreldatep = tswap32(*osreldatep);
+        return 0;
+    }
+}
+
+/*
+ * Populate the target_prstatus struct.
+ *
+ * sys/kern/imagact_elf.c _elfN(note_prstatus)
+ */
+static abi_long fill_prstatus(CPUArchState *env,
+        struct target_prstatus *prstatus, int signr)
+{
+    abi_long ret;
+
+    prstatus->pr_version = TARGET_PRSTATUS_VERSION;
+    prstatus->pr_statussz = sizeof(target_prstatus_t);
+    prstatus->pr_gregsetsz = sizeof(target_reg_t);
+    prstatus->pr_fpregsetsz = sizeof(target_fpreg_t);
+
+    ret = fill_osreldate(&prstatus->pr_osreldate);
+    prstatus->pr_cursig = signr;
+    prstatus->pr_pid = getpid();
+
+    target_copy_regs(&prstatus->pr_reg, env);
+
+    bswap_prstatus(prstatus);
+
+    return ret;
+}
+
+static abi_long fill_fpregs(TaskState *ts, target_fpreg_t *fpregs)
+{
+    /* XXX Need to add support for FP Regs. */
+    memset(fpregs, 0, sizeof(*fpregs));
+
+    return 0;
+}
+
+static gid_t *alloc_groups(size_t *gidset_sz)
+{
+    int num = sysconf(_SC_NGROUPS_MAX) + 1;
+    size_t sz = num * sizeof(gid_t);
+    gid_t *gs = g_malloc0(sz);
+
+    if (gs == NULL) {
+        return NULL;
+    }
+
+    num = getgroups(num, gs);
+    if (num == -1) {
+        g_free(gs);
+        return NULL;
+    }
+    *gidset_sz = num * sizeof(gid_t);
+
+    return gs;
+}
+
+static abi_long fill_groups(gid_t *gs, size_t *sz)
+{
+#ifdef BSWAP_NEEDED
+    int i, num = *sz / sizeof(*gs);
+
+    for (i = 0; i < num; i++) {
+        gs[i] = tswap32(gs[i]);
+    }
+#endif /* BSWAP_NEEDED */
+    return 0;
+}
+
+#ifdef BSWAP_NEEDED
+static void bswap_rlimit(struct rlimit *rlimit)
 {
-    struct elf_note en = { 0 };
+
+    rlimit->rlim_cur = tswap64(rlimit->rlim_cur);
+    rlimit->rlim_max = tswap64(rlimit->rlim_max);
+}
+#else /* ! BSWAP_NEEDED */
+static void bswap_rlimit(struct rlimit *rlimit) {}
+#endif /* ! BSWAP_NEEDED */
+
+/*
+ * Get all the rlimits.  Caller must free rlimits.
+ */
+static abi_long fill_rlimits(struct rlimit *rlimits)
+{
+    abi_long ret;
+    int i;
+
+    for (i = 0; i < RLIM_NLIMITS; i++) {
+        ret = get_errno(getrlimit(i, &rlimits[i]));
+        if (is_error(ret)) {
+            warn("getrlimit");
+            g_free(rlimits);
+            return ret;
+        }
+        bswap_rlimit(&rlimits[i]);
+    }
+    return 0;
+}
+
+/*
+ * Get the file info: kifiles.
+ */
+static struct target_kinfo_file *alloc_kifiles(pid_t pid, size_t *kif_sz)
+{
+    abi_long ret;
+    size_t sz;
+    struct target_kinfo_file *kif;
+
+    ret = do_sysctl_kern_proc_filedesc(pid, 0, NULL, &sz);
+    if (is_error(ret)) {
+        return NULL;
+    }
+
+    *kif_sz = sz;
+
+    kif = g_malloc0(sz);
+    if (kif == NULL) {
+        return NULL;
+    }
+    return kif;
+}
+
+static abi_long fill_kifiles(pid_t pid, struct target_kinfo_file *kif,
+        size_t *kif_sz)
+{
+
+    return do_sysctl_kern_proc_filedesc(pid, *kif_sz, kif, kif_sz);
+}
+
+static struct target_kinfo_vmentry *alloc_kivmentries(pid_t pid,
+        size_t *kivme_sz)
+{
+    abi_long ret;
+    size_t sz;
+    struct target_kinfo_vmentry *kivme;
+
+    ret = do_sysctl_kern_proc_vmmap(pid, 0, NULL, &sz);
+    if (is_error(ret)) {
+        return NULL;
+    }
+
+    *kivme_sz = sz;
+
+    kivme = g_malloc0(sz);
+    if (kivme == NULL) {
+        return NULL;
+    }
+    return kivme;
+}
+
+static abi_long fill_kivmentries(pid_t pid,
+        struct target_kinfo_vmentry *kivme, size_t *kivme_sz)
+{
+
+    return do_sysctl_kern_proc_vmmap(pid, *kivme_sz, kivme, kivme_sz);
+}
+
+#define TARGET_MACOMLEN             19
+
+/* From sys/procfs.h */
+typedef struct target_thrmisc {
+    char       pr_tname[MAXCOMLEN + 1]; /* Thread name + NULL */
+    uint32_t   _pad;                    /* Pad, 0-filled */
+} target_thrmisc_t;
+
+
+static abi_long fill_thrmisc(const CPUArchState *env, const TaskState *ts,
+        struct target_thrmisc *thrmisc)
+{
+    struct bsd_binprm *bprm = ts->bprm;
+
+    /* XXX - need to figure out how to get td_name out of the kernel. */
+    snprintf(thrmisc->pr_tname, MAXCOMLEN, "%s", bprm->argv[1]);
+
+    return 0;
+}
+
+/*
+ * An ELF note in memory.
+ */
+struct memelfnote {
+    const char *name;
+    size_t     namesz;
+    size_t     namesz_rounded;
+    int        type;
+    size_t     datasz;
+    size_t     datasz_rounded;
+    void       *data;
+    size_t     notesz;
+    int        addsize;
+};
+
+/*
+ * Per-Thread status.
+ */
+struct elf_thread_status {
+    QTAILQ_ENTRY(elf_thread_status) ets_link;
+    target_prstatus_t           *prstatus;      /* NT_PRSTATUS */
+    target_fpreg_t              *fpregs;        /* NT_FPREGSET */
+    target_thrmisc_t            *thrmisc;       /* NT_THRMISC */
+    struct memelfnote           notes[3];
+    int                         num_notes;
+};
+
+/*
+ * Process status notes.
+ */
+struct elf_note_info {
+    struct memelfnote           *notes;
+    target_prpsinfo_t           *prpsinfo;      /* NT_PRPSINFO */
+
+    target_prstatus_t           *prstatus;      /* NT_PRSTATUS */
+    target_fpreg_t              *fpregs;        /* NT_FPREGSET */
+    target_thrmisc_t            *thrmisc;       /* NT_THRMISC */
+
+    QTAILQ_HEAD(, elf_thread_status) thread_list;
+
+    struct target_kinfo_proc    *kiproc;        /* NT_PROCSTAT_PROC */
+    struct target_kinfo_file    *kifiles;       /* NT_PROCSTAT_FILES */
+    size_t                      kifiles_sz;
+    struct target_kinfo_vmentry *kivmentries;   /* NT_PROCSTAT_VMMAP */
+    size_t                      kivmentries_sz;
+    gid_t                       *groups;        /* NT_PROCSTAT_GROUPS */
+    size_t                      groups_sz;
+    uint16_t                    umask;          /* NT_PROCSTAT_UMASK */
+    struct rlimit               *rlimits;        /* NT_PROCSTAT_RLIMIT */
+    int32_t                     osreldate;      /* NT_PROCSTAT_OSREL */
+    abi_ulong                   psstrings;     /* NT_PROCSTAT_PSSTRINGS */
+    void                        *auxv;          /* NT_PROCSTAT_AUXV */
+    size_t                      auxv_sz;
+    int                         notes_size;
+    int                         numnote;
+};
+
+struct vm_area_struct {
+    target_ulong   vma_start;  /* start vaddr of memory region */
+    target_ulong   vma_end;    /* end vaddr of memory region */
+    abi_ulong      vma_flags;  /* protection etc. flags for the region */
+    QTAILQ_ENTRY(vm_area_struct) vma_link;
+};
+
+struct mm_struct {
+    QTAILQ_HEAD(, vm_area_struct) mm_mmap;
+    int mm_count;           /* number of mappings */
+};
+
+static struct mm_struct *vma_init(void)
+{
+    struct mm_struct *mm;
+
+    mm = g_malloc(sizeof(*mm));
+    if (mm == NULL) {
+        return NULL;
+    }
+
+    mm->mm_count = 0;
+    QTAILQ_INIT(&mm->mm_mmap);
+
+    return mm;
+}
+
+static struct vm_area_struct *vma_first(const struct mm_struct *mm)
+{
+
+    return QTAILQ_FIRST(&mm->mm_mmap);
+}
+
+static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
+{
+
+    return QTAILQ_NEXT(vma, vma_link);
+}
+
+static void vma_delete(struct mm_struct *mm)
+{
+    struct vm_area_struct *vma;
+
+    while (vma_first(mm) != NULL) {
+        vma = vma_first(mm);
+        QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
+        g_free(vma);
+    }
+    g_free(mm);
+}
+
+static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
+                           target_ulong end, abi_ulong flags)
+{
+    struct vm_area_struct *vma;
+
+    vma = g_malloc0(sizeof(*vma));
+    if (vma == NULL) {
+        return -1;
+    }
+
+    vma->vma_start = start;
+    vma->vma_end = end;
+    vma->vma_flags = flags;
+
+    QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
+    mm->mm_count++;
+
+    return 0;
+}
+
+static int vma_get_mapping_count(const struct mm_struct *mm)
+{
+
+    return mm->mm_count;
+}
+
+/*
+ * Calculate file (dump) size of given memory region.
+ */
+static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
+{
+
+    /* if we cannot even read the first page, skip it */
+    if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE)) {
+        return 0;
+    }
+
+    /*
+     * Usually we don't dump executable pages as they contain
+     * non-writable code that debugger can read directly from
+     * target library etc.  However, thread stacks are marked
+     * also executable so we read in first page of given region
+     * and check whether it contains elf header.  If there is
+     * no elf header, we dump it.
+     */
+    if (vma->vma_flags & PROT_EXEC) {
+        char page[TARGET_PAGE_SIZE];
+
+        copy_from_user(page, vma->vma_start, sizeof(page));
+        if ((page[EI_MAG0] == ELFMAG0) &&
+            (page[EI_MAG1] == ELFMAG1) &&
+            (page[EI_MAG2] == ELFMAG2) &&
+            (page[EI_MAG3] == ELFMAG3)) {
+            /*
+             * Mappings are possibly from ELF binary.  Don't dump
+             * them.
+             */
+            return 0;
+        }
+    }
+
+    return vma->vma_end - vma->vma_start;
+}
+
+static int vma_walker(void *priv, target_ulong start, target_ulong end,
+                      unsigned long flags)
+{
+    struct mm_struct *mm = (struct mm_struct *)priv;
+
+    vma_add_mapping(mm, start, end, flags);
+    return 0;
+}
+
+
+/*
+ * Construct the name of the coredump file in the form of:
+ *
+ * Long form:
+ *   qemu_<basename_of_target>_<date>-<time>_<pid>.core
+ *
+ * Short form:
+ *   qemu_<basename_of_target>.core
+ *
+ * On success return 0, otherwise return -1 (and errno).
+ */
+static int core_dump_filename(const TaskState *ts, char *buf,
+        size_t bufsize)
+{
+#ifdef QEMU_LONG_CORE_FILENAME
+    char timestamp[64];
+    char *filename = NULL;
+    char *base_filename = NULL;
+    struct timeval tv;
+    struct tm tm;
+
+    assert(bufsize >= PATH_MAX);
+
+    if (gettimeofday(&tv, NULL) < 0) {
+        (void) fprintf(stderr, "unable to get current timestamp: %s",
+                strerror(errno));
+        return -1;
+    }
+
+    filename = strdup(ts->bprm->filename);
+    base_filename = basename(filename);
+    (void) strftime(timestamp, sizeof(timestamp), "%Y%m%d-%H%M%S",
+            localtime_r(&tv.tv_sec, &tm));
+    (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
+            base_filename, timestamp, (int)getpid());
+    free(filename);
+#else /* ! QEMU_LONG_CORE_FILENAME */
+    char *filename, *base_filename;
+
+    assert(bufsize >= PATH_MAX);
+
+    filename = strdup(ts->bprm->filename);
+    base_filename = basename(filename);
+    (void) snprintf(buf, bufsize, "qemu_%s.core", base_filename);
+    free(filename);
+#endif /* ! QEMU_LONG_CORE_FILENAME */
+
+    return 0;
+}
+
+
+static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
+        uint32_t flags)
+{
+
+    (void) memset(elf, 0, sizeof(*elf));
+
+    elf->e_ident[EI_MAG0] = ELFMAG0;
+    elf->e_ident[EI_MAG1] = ELFMAG1;
+    elf->e_ident[EI_MAG2] = ELFMAG2;
+    elf->e_ident[EI_MAG3] = ELFMAG3;
+    elf->e_ident[EI_CLASS] = ELF_CLASS;
+    elf->e_ident[EI_DATA] = ELF_DATA;
+    elf->e_ident[EI_VERSION] = EV_CURRENT;
+    elf->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
+    elf->e_type = ET_CORE;
+    elf->e_machine = machine;
+    elf->e_version = EV_CURRENT;
+    elf->e_phoff = sizeof(struct elfhdr);
+    elf->e_flags = flags;
+    elf->e_ehsize = sizeof(struct elfhdr);
+    elf->e_phentsize = sizeof(struct elf_phdr);
+    elf->e_phnum = segs;
+    elf->e_shstrndx = SHN_UNDEF;
+
+    bswap_ehdr(elf);
+}
+
+static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
+{
+
+    phdr->p_type = PT_NOTE;
+    phdr->p_flags = PF_R;       /* Readable */
+    phdr->p_offset = offset;
+    phdr->p_vaddr = 0;
+    phdr->p_paddr = 0;
+    phdr->p_filesz = sz;
+    phdr->p_memsz = 0;
+    phdr->p_align = ELF_NOTE_ROUNDSIZE;
+
+    bswap_phdr(phdr, 1);
+}
+
+static void fill_note(struct memelfnote *note, const char *name, int type,
+        unsigned int sz, void *data, int addsize)
+{
+    unsigned int namesz;
+
+    namesz = strlen(name) + 1;
+    note->name = name;
+    note->namesz = namesz;
+    note->namesz_rounded = roundup2(namesz, sizeof(int32_t));
+    note->type = type;
+    note->addsize = tswap32(addsize);
+
+    if (addsize) {
+        note->datasz = sz;
+        note->datasz_rounded =
+            roundup2((sz + sizeof(uint32_t)), sizeof(int32_t));
+    } else {
+        note->datasz = sz;
+        note->datasz_rounded = roundup2(sz, sizeof(int32_t));
+    }
+    note->data = data;
+
+    /*
+     * We calculate rounded up note size here as specified by
+     * ELF document.
+     */
+    note->notesz = sizeof(struct elf_note) +
+        note->namesz_rounded + note->datasz_rounded;
+}
+
+/*
+ * Initialize the perthread_note_info and process_note_info structures
+ * so that it is at least safe to call free_note_info() on it. Must be
+ * called before calling fill_note_info().
+ */
+static void init_note_info(struct elf_note_info *info)
+{
+
+    memset(info, 0, sizeof(*info));
+    QTAILQ_INIT(&info->thread_list);
+}
+
+static void free_note_info(struct elf_note_info *info)
+{
+    struct elf_thread_status *ets;
+
+    g_free(info->prpsinfo);
+    g_free(info->prstatus);
+    g_free(info->fpregs);
+    g_free(info->thrmisc);
+
+    while (!QTAILQ_EMPTY(&info->thread_list)) {
+        ets = QTAILQ_FIRST(&info->thread_list);
+        QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
+        if (ets) {
+            g_free(ets->prstatus);
+            g_free(ets->fpregs);
+            g_free(ets->thrmisc);
+            g_free(ets);
+        }
+    }
+
+    g_free(info->kiproc);
+    g_free(info->kifiles);
+    g_free(info->kivmentries);
+    g_free(info->groups);
+    g_free(info->rlimits);
+    g_free(info->auxv);
+}
+
+static int dump_write(int fd, const void *ptr, size_t size)
+{
+    const char *bufp = (const char *)ptr;
+    ssize_t bytes_written, bytes_left;
+    struct rlimit dumpsize;
+    off_t pos;
+
+    bytes_written = 0;
+    getrlimit(RLIMIT_CORE, &dumpsize);
+    pos = lseek(fd, 0, SEEK_CUR);
+    if (pos == -1) {
+        if (errno == ESPIPE) { /* not a seekable stream */
+            bytes_left = size;
+        } else {
+            return pos;
+        }
+    } else {
+        if (dumpsize.rlim_cur <= pos) {
+            return -1;
+        } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
+            bytes_left = size;
+        } else {
+            size_t limit_left = dumpsize.rlim_cur - pos;
+            bytes_left = limit_left >= size ? size : limit_left ;
+        }
+    }
+
+    /*
+     * In normal conditions, single write(2) should do but
+     * in case of socket etc. this mechanism is more portable.
+     */
+    do {
+        bytes_written = write(fd, bufp, bytes_left);
+        if (bytes_written < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            return -1;
+        } else if (bytes_written == 0) { /* eof */
+            return -1;
+        }
+        bufp += bytes_written;
+        bytes_left -= bytes_written;
+    } while (bytes_left > 0);
+
+    return 0;
+}
+
+
+static int write_note(struct memelfnote *men, int fd)
+{
+    struct elf_note en;
+
+    en.n_namesz = men->namesz_rounded;
+    en.n_descsz = men->datasz_rounded;
+    en.n_type = men->type;
 
     bswap_note(&en);
 
+    if (dump_write(fd, &en, sizeof(en)) != 0) {
+        return -1;
+    }
+    if (dump_write(fd, men->name, men->namesz_rounded) != 0) {
+        return -1;
+    }
+
+    if (men->addsize)
+        if (dump_write(fd, &men->addsize, sizeof(uint32_t)) != 0) {
+            return -1;
+        }
+
+    if (dump_write(fd, men->data, men->datasz) != 0) {
+        return -1;
+    }
+
+    return 0;
+}
+
+static int write_note_info(struct elf_note_info *info, int fd)
+{
+    struct elf_thread_status *ets;
+    int i, error = 0;
+
+    /* write prpsinfo, prstatus, fpregs, and thrmisc */
+    for (i = 0; i < 4; i++) {
+        error = write_note(&info->notes[i], fd);
+        if (error != 0) {
+            return error;
+        }
+    }
+
+    /* write prstatus, fpregset, & thrmisc for each additional thread */
+    QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
+        error = write_note(&ets->notes[0], fd);
+        if (error != 0) {
+            return error;
+        }
+        error = write_note(&ets->notes[1], fd);
+        if (error != 0) {
+            return error;
+        }
+        error = write_note(&ets->notes[2], fd);
+        if (error != 0) {
+            return error;
+        }
+    }
+
+    /*
+     * write kiproc, kifiles, kivmmap, groups, umask, rlimits, osrel,
+     * psstrings, and auxv.
+     */
+    for (i = 4; i < info->numnote; i++) {
+        error = write_note(&info->notes[i], fd);
+        if (error != 0) {
+            return error;
+        }
+    }
+
+    return 0;
+}
+
+static size_t note_size(const struct memelfnote *note)
+{
+
+    return note->notesz;
+}
+
+static abi_long fill_thread_info(struct elf_note_info *info, int signr,
+    CPUArchState *env)
+{
+    CPUState *cpu = env_cpu((CPUArchState *)env);
+    TaskState *ts = (TaskState *)cpu->opaque;
+    struct elf_thread_status *ets;
+
+    ets = g_malloc0(sizeof(*ets));
+    if (ets == NULL) {
+        return -TARGET_ENOMEM;
+    }
+    ets->num_notes = 3;
+
+    ets->prstatus = g_malloc0(sizeof(struct target_prstatus));
+    if (ets->prstatus == NULL) {
+        return -TARGET_ENOMEM;
+    }
+    fill_prstatus(env, ets->prstatus, signr);
+    fill_note(&ets->notes[0], "FreeBSD", TARGET_NT_PRSTATUS,
+            sizeof(struct target_prstatus), &ets->prstatus, 0);
+
+
+    ets->fpregs = g_malloc0(sizeof(*ets->fpregs));
+    if (ets->fpregs == NULL) {
+        return -TARGET_ENOMEM;
+    }
+    fill_fpregs(ts, ets->fpregs);
+    fill_note(&ets->notes[1], "FreeBSD", TARGET_NT_FPREGSET,
+            sizeof(*ets->fpregs), ets->fpregs, 0);
+
+    ets->thrmisc = g_malloc0(sizeof(*ets->thrmisc));
+    if (ets->thrmisc == NULL) {
+        return -TARGET_ENOMEM;
+    }
+    fill_thrmisc(env, ts, ets->thrmisc);
+    fill_note(&ets->notes[2], "FreeBSD", TARGET_NT_THRMISC,
+            sizeof(*ets->thrmisc), ets->thrmisc, 0);
+
+    QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
+
+    info->notes_size += (note_size(&ets->notes[0]) +
+        note_size(&ets->notes[1]) + note_size(&ets->notes[2]));
+
+    return 0;
+}
+
+static abi_long fill_kiproc(TaskState *ts, pid_t pid,
+        struct target_kinfo_proc *tkip)
+{
+    abi_long ret;
+    size_t len = sizeof(*tkip);
+    struct bsd_binprm *bprm = ts->bprm;
+
+    ret = do_sysctl_kern_getprocs(KERN_PROC_PID, pid, len, tkip, &len);
+
+    if (is_error(ret)) {
+        g_free(tkip);
+    }
+
+    /* Fix up some to be the target values. */
+    strncpy(tkip->ki_tdname, basename(bprm->argv[0]), TARGET_TDNAMLEN);
+    strncpy(tkip->ki_comm, basename(bprm->argv[0]), TARGET_COMMLEN);
+#if TARGET_ABI_BITS == 32
+    strncpy(tkip->ki_emul, "FreeBSD ELF32", TARGET_KI_EMULNAMELEN);
+#else
+    strncpy(tkip->ki_emul, "FreeBSD ELF64", TARGET_KI_EMULNAMELEN);
+#endif
+
+    return ret;
+}
+
+
+struct target_elf_auxinfo {
+    abi_long    a_type;
+    abi_long    a_value;
+};
+
+
+static abi_long fill_auxv(void *auxv, size_t *sz)
+{
+
+    *sz = target_auxents_sz;
+
+    return copy_from_user(auxv, target_auxents, target_auxents_sz);
+}
+
+static abi_long fill_psstrings(abi_ulong *psstrings)
+{
+
+    *psstrings = tswapal(TARGET_PS_STRINGS);
+
     return 0;
 }
+
+#define MAXNUMNOTES    13
+
+static int fill_note_info(struct elf_note_info *info,
+        int signr, CPUArchState *env)
+{
+    CPUState *cpu = env_cpu((CPUArchState *)env);
+    TaskState *ts = (TaskState *)cpu->opaque;
+    int i, err, numnotes = 0;
+    pid_t pid = getpid();
+
+    info->notes = g_malloc0(MAXNUMNOTES * sizeof(struct memelfnote));
+    if (info->notes == NULL) {
+        err = ENOMEM;
+        goto edone;
+    }
+
+    /* NT_PRPSINFO */
+    info->prpsinfo = g_malloc0(sizeof(*info->prpsinfo));
+    if (info->prpsinfo == NULL) {
+        err = -TARGET_ENOMEM;
+        goto edone;
+    }
+    err = fill_prpsinfo(ts, &info->prpsinfo);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PRPSINFO,
+            sizeof(*info->prpsinfo), info->prpsinfo, 0);
+
+    /* prstatus, fpregs, and thrmisc for main thread. */
+
+    /* NT_PRSTATUS */
+    info->prstatus = g_malloc0(sizeof(struct target_prstatus));
+    if (info->prstatus == NULL) {
+        err = -TARGET_ENOMEM;
+        goto edone;
+    }
+    err = fill_prstatus(env, info->prstatus, signr);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PRSTATUS,
+            sizeof(struct target_prstatus), info->prstatus, 0);
+
+    /* NT_FPREGSET */
+    info->fpregs = g_malloc0(sizeof(*info->fpregs));
+    if (info->fpregs == NULL) {
+        err = -TARGET_ENOMEM;
+        goto edone;
+    }
+    fill_fpregs(ts, info->fpregs);
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_FPREGSET,
+            sizeof(*info->fpregs), info->fpregs, 0);
+
+    /* NT_THRMISC */
+    info->thrmisc = g_malloc0(sizeof(*info->thrmisc));
+    if (info->thrmisc == NULL) {
+        err = -TARGET_ENOMEM;
+        goto edone;
+    }
+    fill_thrmisc(env, ts, info->thrmisc);
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_THRMISC,
+            sizeof(*info->thrmisc), info->thrmisc, 0);
+
+    /* NT_PROCSTAT_PROC */
+    info->kiproc = g_malloc0(sizeof(*info->kiproc));
+    if (info->kiproc == NULL) {
+        err = -TARGET_ENOMEM;
+        goto edone;
+    }
+    err = fill_kiproc(ts, pid, info->kiproc);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PROCSTAT_PROC,
+            sizeof(*info->kiproc), info->kiproc,
+            sizeof(struct target_kinfo_proc));
+
+    /* NT_PROCSTAT_FILES */
+    info->kifiles = alloc_kifiles(pid, &info->kifiles_sz);
+    if (info->kifiles == NULL) {
+        err = -TARGET_ENOMEM;
+        goto edone;
+    }
+    err = fill_kifiles(pid, info->kifiles, &info->kifiles_sz);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PROCSTAT_FILES,
+            info->kifiles_sz, info->kifiles,
+            sizeof(struct target_kinfo_file));
+
+    /* NT_PROCSTAT_VMMAP */
+    info->kivmentries = alloc_kivmentries(pid, &info->kivmentries_sz);
+    if (info->kivmentries == NULL) {
+        err = -TARGET_ENOMEM;
+        goto edone;
+    }
+    err = fill_kivmentries(pid, info->kivmentries, &info->kivmentries_sz);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PROCSTAT_VMMAP,
+            info->kivmentries_sz, info->kivmentries,
+            sizeof(struct target_kinfo_vmentry));
+
+    /* NT_PROCSTAT_GROUPS */
+    info->groups = alloc_groups(&info->groups_sz);
+    if (info->groups == NULL) {
+        err = -TARGET_ENOMEM;
+        goto edone;
+    }
+    err = fill_groups(info->groups, &info->groups_sz);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PROCSTAT_GROUPS,
+            info->groups_sz, info->groups,
+            sizeof(uint32_t));
+
+    /* NT_PROCSTAT_RLIMIT */
+    info->rlimits = g_malloc0(RLIM_NLIMITS * sizeof(struct rlimit));
+    if (info->rlimits == NULL) {
+        return -TARGET_ENOMEM;
+    }
+    err = fill_rlimits(info->rlimits);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PROCSTAT_RLIMIT,
+            sizeof(struct rlimit) * RLIM_NLIMITS, info->rlimits,
+            sizeof(struct rlimit) * RLIM_NLIMITS);
+
+    /* NT_PROCSTAT_OSREL */
+    err = fill_osreldate(&info->osreldate);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PROCSTAT_OSREL,
+            sizeof(info->osreldate), &info->osreldate,
+            sizeof(int32_t));
+
+    /* NT_PROCSTAT_PSSTRINGS */
+    err = fill_psstrings(&info->psstrings);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PROCSTAT_PSSTRINGS,
+            sizeof(info->psstrings), &info->psstrings,
+            sizeof(abi_ulong));
+
+    /* NT_PROCSTAT_AUXV */
+    info->auxv = g_malloc0(target_auxents_sz);
+    if (info->auxv == NULL) {
+        err = -TARGET_ENOMEM;
+        goto edone;
+    }
+    err = fill_auxv(info->auxv, &info->auxv_sz);
+    if (err != 0) {
+        goto edone;
+    }
+    fill_note(&info->notes[numnotes++], "FreeBSD", TARGET_NT_PROCSTAT_AUXV,
+            info->auxv_sz, info->auxv,
+            sizeof(struct target_elf_auxinfo));
+
+    assert(numnotes <= MAXNUMNOTES);
+    info->numnote = numnotes;
+    info->notes_size = 0;
+    for (i = 0; i < numnotes; i++) {
+        info->notes_size += note_size(&info->notes[i]);
+    }
+
+    /* read and fill status of all threads */
+    cpu_list_lock();
+    CPU_FOREACH(cpu) {
+        if (cpu == thread_cpu) {
+            continue;
+        }
+        err = fill_thread_info(info, signr, (CPUArchState *)cpu->env_ptr);
+        if (err != 0) {
+            cpu_list_unlock();
+            goto edone;
+        }
+    }
+    cpu_list_unlock();
+
+    return 0;
+
+edone:
+    free_note_info(info);
+    return err;
+}
+
+static int elf_core_dump(int signr, CPUArchState *env)
+{
+    int fd = -1;
+    int segs = 0;
+    off_t offset = 0, data_offset = 0;
+    CPUState *cpu = env_cpu((CPUArchState *)env);
+    TaskState *ts = (TaskState *)cpu->opaque;
+    struct vm_area_struct *vma = NULL;
+    struct mm_struct *mm = NULL;
+    struct rlimit dumpsize;
+    struct elfhdr elf;
+    struct elf_phdr phdr;
+    struct elf_note_info info;
+    char corefile[PATH_MAX];
+
+    init_note_info(&info);
+
+    errno = 0;
+    getrlimit(RLIMIT_CORE, &dumpsize);
+    if (dumpsize.rlim_cur == 0) {
+        return 0;
+    }
+
+    if (core_dump_filename(ts, corefile, sizeof(corefile)) < 0) {
+        return -(errno);
+    }
+
+    fd = open(corefile, O_WRONLY | O_CREAT,
+              S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+    if (fd < 0) {
+        return -(errno);
+    }
+
+    /*
+     * Walk through target process memory mappings and
+     * set up structure containing this information.  After
+     * this point vma_xxx functions can be used.
+     */
+    mm = vma_init();
+    if (mm == NULL) {
+        goto out;
+    }
+
+    walk_memory_regions(mm, vma_walker);
+    segs = vma_get_mapping_count(mm);
+
+    /*
+     * Construct the coredump ELF header.  Add another segment for
+     * notes.
+     *
+     * See kern/imgact_elf.c  __elfN(corehdr)().
+     */
+    fill_elf_header(&elf, segs + 1, ELF_MACHINE, ts->info->elf_flags);
+    if (dump_write(fd, &elf, sizeof(elf)) != 0) {
+        goto out;
+    }
+
+    /*
+     * Construct the note segment and write it out.
+     */
+    if (fill_note_info(&info, signr, env) < 0) {
+        goto out;
+    }
+
+    offset += sizeof(elf);                             /* elf header */
+    offset += (segs + 1) * sizeof(struct elf_phdr);    /* program headers */
+
+    /* Write out notes program header. */
+    fill_elf_note_phdr(&phdr, info.notes_size, offset);
+
+    offset += info.notes_size;
+    if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
+        goto out;
+    }
+
+    /*
+     * ELF specification wants data to start at page boundary so
+     * we align it here.
+     */
+    data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
+
+    /*
+     * Write program headers for memory regions mapped in the
+     * target process.
+     *
+     * See cb_put_phdr() in sys/kern/imgact_ef.c
+     */
+    for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
+        (void) memset(&phdr, 0, sizeof(phdr));
+
+        phdr.p_type = PT_LOAD;
+        phdr.p_offset = offset;
+        phdr.p_vaddr = vma->vma_start;
+        phdr.p_paddr = 0;
+        phdr.p_filesz = vma_dump_size(vma); /* ??? */
+        offset += phdr.p_filesz;
+        phdr.p_memsz = vma->vma_end - vma->vma_start;
+        phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
+        if (vma->vma_flags & PROT_WRITE) {
+            phdr.p_flags |= PF_W;
+        }
+        if (vma->vma_flags & PROT_EXEC) {
+            phdr.p_flags |= PF_X;
+        }
+        phdr.p_align = ELF_EXEC_PAGESIZE;  /* or PAGE_SIZE? */
+
+        bswap_phdr(&phdr, 1);
+        dump_write(fd, &phdr, sizeof(phdr));
+    }
+
+    /*
+     * Next write notes just after program headers.
+     */
+    if (write_note_info(&info, fd) < 0) {
+        goto out;
+    }
+
+    /*
+     * Align data to page boundary.
+     */
+    if (lseek(fd, data_offset, SEEK_SET) != data_offset) {
+        goto out;
+    }
+
+    /*
+     * Finally, dump the process memory into the corefile as well.
+     */
+    for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
+        abi_ulong addr;
+        abi_ulong end;
+
+        end = vma->vma_start + vma_dump_size(vma);
+
+        for (addr = vma->vma_start; addr < end;
+                addr += TARGET_PAGE_SIZE) {
+            char page[TARGET_PAGE_SIZE];
+            int error;
+
+            /*
+             * Read in page from target process memory and
+             * write it to coredump file.
+             */
+            error = copy_from_user(page, addr, sizeof(page));
+            if (error != 0) {
+                (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
+                        addr);
+                errno = -error;
+                goto out;
+            }
+            if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0) {
+                goto out;
+            }
+        }
+    }
+    errno = 0;
+
+out:
+    if (mm != NULL) {
+        vma_delete(mm);
+    }
+
+    (void)close(fd);
+
+    if (errno != 0) {
+        return -errno;
+    }
+    return 0;
+}
+
+#endif /* USE_ELF_CORE_DUMP */
Signature · 1 line
2.40.0
#16 lore raw

PATCH v2 15/19 bsd-user: Add SIGSYS to core dump signals.

In reply to Warner Losh

SIGSYS creates a core by default if uncaught. Follow that here. Sort with the same order as is in the kernel.

---
 bsd-user/signal.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
bsd-user/signal.c +9 -4
index f4e078ee1da..4301595cc2f 100644
@@ -330,17 +330,22 @@ int block_signals(void)
     return qatomic_xchg(&ts->signal_pending, 1);
 }
 
-/* Returns 1 if given signal should dump core if not handled. */
+/*
+ * Returns 1 if given signal should dump core if not handled.
+ * Compare with kern_sig.c sigproptbl[].
+ */
 static int core_dump_signal(int sig)
 {
     switch (sig) {
+    case TARGET_SIGQUIT:
+    case TARGET_SIGILL:
+    case TARGET_SIGTRAP:
     case TARGET_SIGABRT:
+    case TARGET_SIGEMT:
     case TARGET_SIGFPE:
-    case TARGET_SIGILL:
-    case TARGET_SIGQUIT:
     case TARGET_SIGSEGV:
-    case TARGET_SIGTRAP:
     case TARGET_SIGBUS:
+    case TARGET_SIGSYS:
         return 1;
     default:
         return 0;
Signature · 1 line
2.40.0
#17 lore raw

PATCH v2 16/19 bsd-user: Implement SIGSYS on arm

In reply to Warner Losh

When a system call returns ENOSYS, send a SIGSYS to the process (to generate a core dump).

---
 bsd-user/arm/target_arch_cpu.h | 8 ++++++++
 1 file changed, 8 insertions(+)
bsd-user/arm/target_arch_cpu.h +8 -0
index 517d0087644..c4b21fef713 100644
@@ -127,6 +127,14 @@ static inline void target_cpu_loop(CPUARMState *env)
                     env->regs[15] -= env->thumb ? 2 : 4;
                     break;
                 }
+                /*
+                 * Emulate BSD's sigsys behavior on unimplemented system calls.
+                 * XXX may need to gate this somehow or arrange for sigsys to be
+                 * masked in some use cases.
+                 */
+                if (ret == -TARGET_ENOSYS) {
+                    force_sig_fault(TARGET_SIGSYS, SI_KERNEL, env->regs[15]);
+                }
                 if ((unsigned int)ret >= (unsigned int)(-515)) {
                     ret = -ret;
                     cpsr_write(env, CPSR_C, CPSR_C, CPSRWriteByInstr);
Signature · 1 line
2.40.0
#18 lore raw

PATCH v2 17/19 bsd-user: Remove host-os.h

In reply to Warner Losh

It only defines the default system call scheme to use. However, that feature was removed in a941a16f6f52.

---
 bsd-user/freebsd/host-os.h | 25 -------------------------
 bsd-user/main.c            |  1 -
 bsd-user/netbsd/host-os.h  | 25 -------------------------
 bsd-user/openbsd/host-os.h | 25 -------------------------
 4 files changed, 76 deletions(-)
 delete mode 100644 bsd-user/freebsd/host-os.h
 delete mode 100644 bsd-user/netbsd/host-os.h
 delete mode 100644 bsd-user/openbsd/host-os.h
bsd-user/freebsd/host-os.h deleted +0 -25
deleted file mode 100644
index 40cae72ec9a..00000000000
@@ -1,25 +0,0 @@
-/*
- *  FreeBSD host dependent code 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 HOST_OS_H
-#define HOST_OS_H
-
-#define HOST_DEFAULT_BSD_TYPE target_freebsd
-
-#endif /* HOST_OS_H */
bsd-user/main.c +0 -1
index 89f225dead2..ba23b74d679 100644
@@ -46,7 +46,6 @@
 #include "qemu/guest-random.h"
 #include "gdbstub/user.h"
 
-#include "host-os.h"
 #include "target_arch_cpu.h"
 
 int singlestep;
bsd-user/netbsd/host-os.h deleted +0 -25
deleted file mode 100644
index 7c14b1ea780..00000000000
@@ -1,25 +0,0 @@
-/*
- *  NetBSD host dependent code 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 HOST_OS_H
-#define HOST_OS_H
-
-#define HOST_DEFAULT_BSD_TYPE target_netbsd
-
-#endif /* HOST_OS_H */
bsd-user/openbsd/host-os.h deleted +0 -25
deleted file mode 100644
index b9222335d46..00000000000
@@ -1,25 +0,0 @@
-/*
- *  OpenBSD host dependent code 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 HOST_OS_H
-#define HOST_OS_H
-
-#define HOST_DEFAULT_BSD_TYPE target_openbsd
-
-#endif /* HOST_OS_H */
Signature · 1 line
2.40.0
Reviewed-by on v2 17/19
#19 lore raw

Re: [PATCH v2 17/19] bsd-user: Remove host-os.h

Quoted 10 lines · Warner Losh

It only defines the default system call scheme to use. However, that feature was removed in a941a16f6f52.

---
  bsd-user/freebsd/host-os.h | 25 -------------------------
  bsd-user/main.c            |  1 -
  bsd-user/netbsd/host-os.h  | 25 -------------------------
  bsd-user/openbsd/host-os.h | 25 -------------------------
  4 files changed, 76 deletions(-)
Quoted 3 lines

delete mode 100644 bsd-user/freebsd/host-os.h
delete mode 100644 bsd-user/netbsd/host-os.h
delete mode 100644 bsd-user/openbsd/host-os.h

r~

#20 lore raw

PATCH v2 18/19 bsd-user: Update system call list

In reply to Warner Losh

Update the system call list. We have one hokey thing in here for swapoff that depends on the version number (so this is not completely generated at the moment). For this, we need to include sys/param.h. The method of generation has changed, so this diff looks way bigger than it needs to be to add the few lines of code for the new system calls.

---
 bsd-user/freebsd/os-syscall.h |    2 +
 bsd-user/freebsd/syscall_nr.h | 1035 +++++++++++++++++----------------
 2 files changed, 529 insertions(+), 508 deletions(-)
bsd-user/freebsd/os-syscall.h +2 -0
index 1f2c0acb1c5..e77745eeb5c 100644
@@ -6,6 +6,8 @@
  * OS-Specific portion of syscall_defs.h
  */
 
+#include <sys/param.h>	/* Needed for __FreeBSD_version */
+
 #include "freebsd/syscall_nr.h"
 
 /*
bsd-user/freebsd/syscall_nr.h +527 -508

1,041 lines not on this page · raw message

Signature · 1 line
2.40.0
Acked-by on v2 18/19
#21 lore raw

Re: [PATCH v2 18/19] bsd-user: Update system call list

Quoted 11 lines · Warner Losh

Update the system call list. We have one hokey thing in here for swapoff that depends on the version number (so this is not completely generated at the moment). For this, we need to include sys/param.h. The method of generation has changed, so this diff looks way bigger than it needs to be to add the few lines of code for the new system calls.

---
  bsd-user/freebsd/os-syscall.h |    2 +
  bsd-user/freebsd/syscall_nr.h | 1035 +++++++++++++++++----------------
  2 files changed, 529 insertions(+), 508 deletions(-)

What is the method of generation?

If it's complicated, it should be in scripts/.
If it's trivial, e.g.

    sed 's/xxx/yyy/' < in.h > out.h

it is worth including the command in the commit message.

Anyway,

r~

#22 lore raw

Re: [PATCH v2 18/19] bsd-user: Update system call list

Quoted 13 lines · Richard Henderson
Quoted 11 lines · Warner Losh

Update the system call list. We have one hokey thing in here for swapoff that depends on the version number (so this is not completely generated at the moment). For this, we need to include sys/param.h. The method of generation has changed, so this diff looks way bigger than it needs to be to add the few lines of code for the new system calls.

---
  bsd-user/freebsd/os-syscall.h |    2 +
  bsd-user/freebsd/syscall_nr.h | 1035 +++++++++++++++++----------------
  2 files changed, 529 insertions(+), 508 deletions(-)
Quoted 9 lines

What is the method of generation?

If it's complicated, it should be in scripts/.
If it's trivial, e.g.

     sed 's/xxx/yyy/' < in.h > out.h

it is worth including the command in the commit message.

I'll add it to the commit message... but I'm also contemplating generating it
on the fly if it's not too hard... Thanks for the suggestion... It's usually a sed,
but someone (likely me) edited it directly for an unwise hack that Ineed to unwind first...

Quoted 3 lines

Anyway,

Warner

#23 lore raw

Re: [PATCH v2 18/19] bsd-user: Update system call list

In reply to Richard Henderson
Quoted 18 lines · Richard Henderson
Quoted 11 lines · Warner Losh

Update the system call list. We have one hokey thing in here for swapoff that depends on the version number (so this is not completely generated at the moment). For this, we need to include sys/param.h. The method of generation has changed, so this diff looks way bigger than it needs to be to add the few lines of code for the new system calls.

---
  bsd-user/freebsd/os-syscall.h |    2 +
  bsd-user/freebsd/syscall_nr.h | 1035 +++++++++++++++++----------------
  2 files changed, 529 insertions(+), 508 deletions(-)

What is the method of generation?

If it's complicated, it should be in scripts/.
If it's trivial, e.g.

Quoted 7 lines
     sed 's/xxx/yyy/' < in.h > out.h

it is worth including the command in the commit message.

Anyway,

I'm dropping this chunk, and will just commit the bits to generate it each build.
It's easier than trying to document what I've done to generate things. I've sorted
out the __FreeBSD_version stuff that was in here in blitz fork, so why not?

Warner

Quoted 2 lines

r~

#24 lore raw

PATCH v2 19/19 bsd-user: Eliminate USE_ELF_CORE_DUMP

In reply to Warner Losh

It's enabled on all platforms (even in the fork), so we can remove it from here.

---
 bsd-user/arm/target_arch_elf.h    | 1 -
 bsd-user/elfcore.c                | 3 ---
 bsd-user/elfload.c                | 5 -----
 bsd-user/i386/target_arch_elf.h   | 1 -
 bsd-user/x86_64/target_arch_elf.h | 1 -
 5 files changed, 11 deletions(-)
bsd-user/arm/target_arch_elf.h +0 -1
index 935bce347fc..9f963d4747f 100644
@@ -29,7 +29,6 @@
 #define ELF_DATA        ELFDATA2LSB
 #define ELF_ARCH        EM_ARM
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE       4096
 
 #define ELF_HWCAP get_elf_hwcap()
bsd-user/elfcore.c +0 -3
index 2905f2b8414..606c42dd4ab 100644
@@ -18,7 +18,6 @@
  */
 #include "qemu/osdep.h"
 
-#ifdef USE_ELF_CORE_DUMP
 #include <err.h>
 #include <libgen.h>
 #include <sys/mman.h>
@@ -1318,5 +1317,3 @@ out:
     }
     return 0;
 }
-
-#endif /* USE_ELF_CORE_DUMP */
bsd-user/elfload.c +0 -5
index fbcdc94b960..0477d243a4b 100644
@@ -35,7 +35,6 @@ static size_t target_auxents_sz;   /* Size of AUX entries including AT_NULL */
 abi_ulong target_stksiz;
 abi_ulong target_stkbas;
 
-static int elf_core_dump(int signr, CPUArchState *env);
 static int load_elf_sections(const struct elfhdr *hdr, struct elf_phdr *phdr,
     int fd, abi_ulong rbase, abi_ulong *baddrp);
 
@@ -818,11 +817,7 @@ int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
 
     info->entry = elf_entry;
 
-#ifdef USE_ELF_CORE_DUMP
     bprm->core_dump = &elf_core_dump;
-#else
-    bprm->core_dump = NULL;
-#endif
 
     return 0;
 }
bsd-user/i386/target_arch_elf.h +0 -1
index cbcd1f08e2f..a18124f0f29 100644
@@ -30,7 +30,6 @@
 #define ELF_DATA        ELFDATA2LSB
 #define ELF_ARCH        EM_386
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE       4096
 
 #endif /* TARGET_ARCH_ELF_H */
bsd-user/x86_64/target_arch_elf.h +0 -1
index b2447118883..08abe62be6f 100644
@@ -30,7 +30,6 @@
 #define ELF_DATA       ELFDATA2LSB
 #define ELF_ARCH       EM_X86_64
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE       4096
 
 #endif /* TARGET_ARCH_ELF_H */
Signature · 1 line
2.40.0
Reviewed-by on v2 19/19
#25 lore raw

Re: [PATCH v2 19/19] bsd-user: Eliminate USE_ELF_CORE_DUMP

Quoted 11 lines · Warner Losh

It's enabled on all platforms (even in the fork), so we can remove it from here.

---
  bsd-user/arm/target_arch_elf.h    | 1 -
  bsd-user/elfcore.c                | 3 ---
  bsd-user/elfload.c                | 5 -----
  bsd-user/i386/target_arch_elf.h   | 1 -
  bsd-user/x86_64/target_arch_elf.h | 1 -
  5 files changed, 11 deletions(-)

Thanks,

r~