bsd-user 2023 Q2 first batch
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.
Stacey Son (6):
bsd-user: h2g_rusage
bsd-user: Implmenet 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: Implment core dumps
Warner Losh (10):
bsd-user: Make print_* public
bsd-user: Ifdef a few MAP_ constants for NetBSD
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/arm/target_arch_cpu.h | 8 + bsd-user/bsd-proc.c | 48 ++ bsd-user/elfcore.c | 1318 +++++++++++++++++++++++++++++++- bsd-user/freebsd/os-sys.c | 508 +++++++++++- bsd-user/freebsd/os-syscall.c | 19 + bsd-user/freebsd/os-syscall.h | 21 + bsd-user/meson.build | 1 + bsd-user/mmap.c | 101 ++- bsd-user/netbsd/os-syscall.h | 16 + bsd-user/openbsd/os-syscall.h | 16 + bsd-user/qemu-bsd.h | 30 + bsd-user/qemu.h | 44 +- bsd-user/signal.c | 13 +- bsd-user/strace.c | 88 +-- bsd-user/syscall_defs.h | 69 +- 15 files changed, 2106 insertions(+), 194 deletions(-) create mode 100644 bsd-user/bsd-proc.c 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 create mode 100644 bsd-user/qemu-bsd.h
Signature · 1 line
2.40.0
PATCH 1/16 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
@@ -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
@@ -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
Re: [PATCH 01/16] bsd-user: Make print_* public
Quoted 8 lines · Warner Losh
Make these functions public. Due to coming restructuring, we'll need to call these from
*bsd/os-syscall.c. Add declarations toqemu.h.Signed-off-by Warner Losh<[email protected]>--- bsd-user/qemu.h | 20 ++++++++++++++++++++ bsd-user/strace.c | 29 +++++++++++++---------------- 2 files changed, 33 insertions(+), 16 deletions(-)
r~
PATCH 2/16 bsd-user: Ifdef a few MAP_ constants for NetBSD
In reply to Warner LoshMAP_GUARD, MAP_EXCL, and MAP_NOCORE are FreeBSD only. Add back the ifdefs that I removed in 36d5d891559f (but only these ifdefs, the rest of the commit is not reverted).
--- bsd-user/mmap.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
bsd-user/mmap.c +10 -0
@@ -416,27 +416,33 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,qemu_log ("MAP_ALIGNED(%u) " , (flags &MAP_ALIGNMENT_MASK ) >>MAP_ALIGNMENT_SHIFT ); } +#ifdef MAP_GUARD if (flags &MAP_GUARD ) {qemu_log ("MAP_GUARD " ); } +#endif if (flags &MAP_FIXED ) {qemu_log ("MAP_FIXED " ); }if (flags &MAP_ANON ) {qemu_log ("MAP_ANON " ); } +#ifdef MAP_EXCL if (flags &MAP_EXCL ) {qemu_log ("MAP_EXCL " ); } +#endif if (flags &MAP_PRIVATE ) {qemu_log ("MAP_PRIVATE " ); }if (flags &MAP_SHARED ) {qemu_log ("MAP_SHARED " ); } +#ifdef MAP_NOCORE if (flags &MAP_NOCORE ) {qemu_log ("MAP_NOCORE " ); } +#endif if (flags &MAP_STACK ) {qemu_log ("MAP_STACK " ); }
@@ -454,6 +460,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, goto fail; } } +#ifdef MAP_GUARD if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 || offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |/* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
@@ -461,6 +468,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, errno =EINVAL ; goto fail; } +#endif if (offset & ~TARGET_PAGE_MASK ) { errno =EINVAL ;
@@ -608,11 +616,13 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, goto the_end; } +#ifdef MAP_EXCL /* Reject the mapping if any page within the range is mapped */ if ((flags &MAP_EXCL ) &&page_check_range (start, len,0 ) <0 ) { errno =EINVAL ; goto fail; } +#endif /* handle the start of the mapping */ if (start > real_start) {
Signature · 1 line
2.40.0
Re: [PATCH 02/16] bsd-user: Ifdef a few MAP_ constants for NetBSD
Quoted 14 lines · Warner Losh
MAP_GUARD,MAP_EXCL, andMAP_NOCOREare FreeBSD only. Add back the ifdefs that I removed in36d5d891559f(but only these ifdefs, the rest of the commit is not reverted).Signed-off-by Warner Losh<[email protected]>--- bsd-user/mmap.c | 10 ++++++++++ 1 file changed, 10 insertions(+)bsd-user/mmap.c +0
-0
bsd-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 423
Quoted 8 lines
bsd-user/mmap.c +2
-0@@ -416,27 +416,33 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,qemu_log ("MAP_ALIGNED(%u) " , (flags &MAP_ALIGNMENT_MASK ) >>MAP_ALIGNMENT_SHIFT ); } +#ifdef MAP_GUARD if (flags &MAP_GUARD ) {qemu_log ("MAP_GUARD " ); } +#endif
Maybe better as
#ifndef MAP_GUARD
#define MAP_GUARD 0
#endif
etc, somewhere common, and let the compiler eliminate the always false conditions.
r~
Re: [PATCH 02/16] bsd-user: Ifdef a few MAP_ constants for NetBSD
Quoted 28 lines · Richard Henderson
bsd-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong
Quoted 14 lines · Warner Losh
MAP_GUARD,MAP_EXCL, andMAP_NOCOREare FreeBSD only. Add back the ifdefs that I removed in36d5d891559f(but only these ifdefs, the rest of the commit is not reverted).Signed-off-by Warner Losh<[email protected]>--- bsd-user/mmap.c | 10 ++++++++++ 1 file changed, 10 insertions(+)bsd-user/mmap.c +0
-0@@ -416,27 +416,33 @@ abi_long target_mmap(abi_ulong start, abi_ulonglen, int prot,
bsd-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong · line 417
Quoted 2 lines
bsd-user/mmap.c +0
-0qemu_log("MAP_ALIGNED(%u) ", (flags & MAP_ALIGNMENT_MASK) >>
MAP_ALIGNMENT_SHIFT);bsd-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 423
Quoted 6 lines
bsd-user/mmap.c +2
-0} +#ifdef MAP_GUARD if (flags &MAP_GUARD ) {qemu_log ("MAP_GUARD " ); } +#endif Maybe better as
Quoted 7 lines
#ifndef
MAP_GUARD
#defineMAP_GUARD0
#endifetc, somewhere common, and let the compiler eliminate the always false conditions.
Interesting notion... I can do that in syscall_defs.h. If that's the pattern in qemu I'll
do it here.... but I've been burned in the past by warnings about always true or always
false conditions... It would be less invasive though...
Warner
PATCH 3/16 bsd-user: Cleanup style.
In reply to Warner LoshThe 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
@@ -45,17 +45,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. */
@@ -69,15 +71,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;
@@ -96,8 +101,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) {
@@ -107,16 +113,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 ();
@@ -161,31 +169,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 ) {
@@ -193,8 +207,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);
@@ -554,8 +569,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 ) {
@@ -604,8 +620,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; }
@@ -630,14 +647,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 */
@@ -646,8 +665,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; }
@@ -655,14 +675,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:
@@ -732,11 +754,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;
@@ -754,16 +778,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 ;
@@ -787,14 +813,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
Re: [PATCH 03/16] bsd-user: Cleanup style.
Quoted 7 lines · Warner Losh
The only diffs between bsd-user fork and qemu upstream is style. Make
mmap.cpass checkpatch.pl.Signed-off-by Warner Losh<[email protected]>--- bsd-user/mmap.c | 91 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 31 deletions(-)
r~
PATCH 4/16 bsd-user: Move system FreeBSD call table to freebsd/os-syscall.c
In reply to Warner LoshMove 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
@@ -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
@@ -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
@@ -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
Re: [PATCH 04/16] bsd-user: Move system FreeBSD call table to freebsd/os-syscall.c
Quoted 10 lines · 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.Signed-off-by Warner Losh<[email protected]>--- bsd-user/freebsd/os-syscall.c | 19 +++++++++++++++++++ bsd-user/qemu.h | 5 ----- bsd-user/strace.c | 17 ----------------- 3 files changed, 19 insertions(+), 22 deletions(-)
r~
PATCH 5/16 bsd-user: Remove NetBSD specific syscall printing
In reply to Warner LoshNothing 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
@@ -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
@@ -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
Re: [PATCH 05/16] bsd-user: Remove NetBSD specific syscall printing
Quoted 8 lines · 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.Signed-off-by Warner Losh<[email protected]>--- bsd-user/qemu.h | 5 ----- bsd-user/strace.c | 17 ----------------- 2 files changed, 22 deletions(-)
r~
PATCH 6/16 bsd-user: Remove OpenBSD specific syscall printing
In reply to Warner LoshNothing 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
@@ -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
@@ -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
Re: [PATCH 06/16] bsd-user: Remove OpenBSD specific syscall printing
Quoted 8 lines · 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.Signed-off-by Warner Losh<[email protected]>--- bsd-user/qemu.h | 5 ----- bsd-user/strace.c | 25 ------------------------- 2 files changed, 30 deletions(-)
r~
PATCH 7/16 bsd-user: Move system call include to os-syscall.h
In reply to Warner LoshMove 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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH 07/16] bsd-user: Move system call include to os-syscall.h
Quoted 14 lines · Warner Losh
bsd-user/syscall_defs.h +1
-3@@ -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;
bsd-user/syscall_defs.h struct target_freebsd__umtx_time { · line 99
Quoted 7 lines
bsd-user/syscall_defs.h +2
-2@@ -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 */
Did I miss where target_suseconds_t got defined?
With the context provided, you remove target_freebsd_suseconds_t but don't replace it.
r~
Re: [PATCH 07/16] bsd-user: Move system call include to os-syscall.h
Quoted 17 lines · Richard Henderson
Quoted 16 lines · Warner Losh
bsd-user/syscall_defs.h +1
-3@@ -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 { };
Quoted 10 lines
bsd-user/syscall_defs.h struct target_freebsd__umtx_time { · line 99
Quoted 5 lines
bsd-user/syscall_defs.h +2
-2struct 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 */ Did I miss where
target_suseconds_tgot defined?
With the context provided, you removetarget_freebsd_suseconds_tbut don't replace it.
At the very end of the first file bsd-user/freebsd/os-syscall.h I define it.
Warner
Richard Henderson
Reviewed-byRe: [PATCH 07/16] bsd-user: Move system call include to os-syscall.h
Quoted 31 lines · Warner Losh
On Sat, Apr 8, 2023 at 1:08 PM Richard Henderson <[email protected] <mailto:[email protected]>> wrote:
On 4/5/23 14:36, Warner Losh wrote: > @@ -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 */ Did I miss where target_suseconds_t got defined? With the context provided, you remove target_freebsd_suseconds_t but don't replace it.
Quoted 1 line
At the very end of the first file
bsd-user/freebsd/os-syscall.hI define it.
Ah, there it is, thanks.
r~
PATCH 8/16 bsd-user: Remove useless mmap definitions
In reply to Warner LoshOn 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
@@ -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
Re: [PATCH 08/16] bsd-user: Remove useless mmap definitions
Quoted 8 lines · 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.
Signed-off-by Warner Losh<[email protected]>--- bsd-user/syscall_defs.h | 36 ------------------------------------ 1 file changed, 36 deletions(-)
r~
PATCH 9/16 bsd-user: h2g_rusage
In reply to Warner LoshFrom: 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
@@ -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
@@ -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
@@ -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
Re: [PATCH 09/16] bsd-user: h2g_rusage
Quoted 6 lines · Warner Losh
From: Stacey Son<[email protected]>
Converts host's rusage to the guest's rusage.
Signed-off-by Stacey Son<[email protected]>Signed-off-by Warner Losh<[email protected]>
Quoted 7 lines
--- 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 100644bsd-user/qemu-bsd.h
r~
PATCH 10/16 bsd-user: Implmenet do_sysctl_kern_getprocs
In reply to Warner LoshFrom: 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
@@ -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
@@ -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
Re: [PATCH 10/16] bsd-user: Implmenet do_sysctl_kern_getprocs
Quoted 10 lines · Warner Losh
From: Stacey Son <[email protected]>
Implement
do_sysctl_kern_getprocsto retrieve proc info from the kernel.Signed-off-by Stacey Son <[email protected]>Signed-off-by Warner Losh <[email protected]>--- bsd-user/freebsd/os-sys.c | 165 +++++++++++++++++++++++++++++++++++++- bsd-user/qemu.h | 3 + 2 files changed, 167 insertions(+), 1 deletion(-)
Typo in subject.
r~
PATCH 11/16 bsd-user: Implement do_sysctl_kern_proc_filedesc
In reply to Warner LoshFrom: 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
@@ -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
@@ -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
Re: [PATCH 11/16] bsd-user: Implement do_sysctl_kern_proc_filedesc
Quoted 11 lines · 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.Signed-off-by Stacey Son<[email protected]>Signed-off-by Warner Losh<[email protected]>--- bsd-user/freebsd/os-sys.c | 193 ++++++++++++++++++++++++++++++++++++++ bsd-user/qemu.h | 3 + 2 files changed, 196 insertions(+)
r~
PATCH 12/16 bsd-user: Implement do_sysctl_kern_proc_vmmap
In reply to Warner LoshFrom: 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
@@ -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
@@ -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
Re: [PATCH 12/16] bsd-user: Implement do_sysctl_kern_proc_vmmap
Quoted 11 lines · 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.Signed-off-by Stacey Son<[email protected]>Signed-off-by Warner Losh<[email protected]>--- bsd-user/freebsd/os-sys.c | 115 ++++++++++++++++++++++++++++++++++++++ bsd-user/qemu.h | 3 + 2 files changed, 118 insertions(+)
r~
PATCH 13/16 bsd-user: Implement sysctl kern.proc, except kern.proc.full_path
In reply to Warner LoshFrom: 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
@@ -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
Re: [PATCH 13/16] bsd-user: Implement sysctl kern.proc, except kern.proc.full_path
Quoted 11 lines · 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).
Signed-off-by Stacey Son<[email protected]>Signed-off-by Warner Losh<[email protected]>--- bsd-user/freebsd/os-sys.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
r~
PATCH 14/16 bsd-user: Implment core dumps
In reply to Warner LoshFrom: 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
@@ -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
Re: [PATCH 14/16] bsd-user: Implment core dumps
Quoted 35 lines · Warner Losh
From: Stacey Son <[email protected]>
Bring in the code that was originally copied from
linxu-user/elfload.cand moved toelfcore.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 in227070562fcin 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.Signed-off-by Stacey Son <[email protected]>Signed-off-by Ed Schouten <[email protected]>Signed-off-by Warner Losh <[email protected]>--- bsd-user/elfcore.c | 1318 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 1315 insertions(+), 3 deletions(-)bsd-user/elfcore.c +12
-1@@ -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
bsd-user/elfcore.c line 21
Quoted 10 lines
bsd-user/elfcore.c +8
-1+* 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
Would this ever be unset?
Typo in subject.
I'm not going to review this one line by line, but
r~
Re: [PATCH 14/16] bsd-user: Implment core dumps
Quoted 46 lines · Richard Henderson
Quoted 22 lines · Warner Losh
From: Stacey Son <[email protected]>
Bring in the code that was originally copied from
linxu-user/elfload.cand moved toelfcore.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 in227070562fcin 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.Signed-off-by Stacey Son <[email protected]>Signed-off-by Ed Schouten <[email protected]>Signed-off-by Warner Losh <[email protected]>--- bsd-user/elfcore.c | 1318 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 1315 insertions(+), 3 deletions(-)bsd-user/elfcore.c +0
-0bsd-user/elfcore.c line 1
Quoted 1 line
bsd-user/elfcore.c +0
-1@@ -1,10 +1,1322 @@ -/* Stubbed out version of core dump support, explicitly in public domain */
Quoted 21 lines
bsd-user/elfcore.c +17
-0+/* +* 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"
Quoted 5 lines
bsd-user/elfcore.c line 21
Quoted 2 lines
bsd-user/elfcore.c +1
-1-static int elf_core_dump (int signr,CPUArchState *env) +#ifdef USE_ELF_CORE_DUMP Would this ever be unset?
I was sure that some architectures didn't use this. However, they all have them, so I'll
remove it.
Quoted 2 lines
Typo in subject.
Doh! Yes. Thanks.
Quoted 4 lines
I'm not going to review this one line by line, but
I've looked at this at a high level, and it seems legit... But it is a bit daunting... And for a 'debug' feature,
I'm OK with 'working and looks OK'... If it were more central, I'd worry more about it, but we still have
about 20k lines to upstream and I'd rather more of them be looked at than this if push comes to shove....
Warner
Quoted 2 lines
r~
Re: [PATCH 14/16] bsd-user: Implment core dumps
Quoted 59 lines · Warner Losh
Quoted 48 lines · Richard Henderson
Quoted 22 lines · Warner Losh
From: Stacey Son <[email protected]>
Bring in the code that was originally copied from
linxu-user/elfload.cand moved toelfcore.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 in227070562fcin 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.Signed-off-by Stacey Son <[email protected]>Signed-off-by Ed Schouten <[email protected]>Signed-off-by Warner Losh <[email protected]>--- bsd-user/elfcore.c | 1318 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 1315 insertions(+), 3 deletions(-)bsd-user/elfcore.c +0
-0bsd-user/elfcore.c line 1
Quoted 1 line
bsd-user/elfcore.c +0
-1@@ -1,10 +1,1322 @@ -/* Stubbed out version of core dump support, explicitly in public domain */
bsd-user/elfcore.c
Quoted 6 lines
bsd-user/elfcore.c +6
-0+/* +* ELF loading code +* +* Copyright (c) 2015 Stacey D. Son +* +* This program is free software; you can redistribute it and/or modify
bsd-user/elfcore.c line 7
Quoted 1 line
bsd-user/elfcore.c +1
-0+* it under the terms of the GNU General Public License as published by
Quoted 14 lines
bsd-user/elfcore.c +10
-0+* 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"Quoted 5 lines
bsd-user/elfcore.c line 21
Quoted 2 lines
bsd-user/elfcore.c +1
-1-static int elf_core_dump (int signr,CPUArchState *env) +#ifdef USE_ELF_CORE_DUMP Would this ever be unset?
Quoted 4 lines
I was sure that some architectures didn't use this. However, they all have them, so I'll
remove it.
I'll remove it as a separate commit since it affects other files as well that currently define it.
Warner
Quoted 24 lines
Typo in subject.
Quoted 1 line
Doh! Yes. Thanks.
Quoted 4 lines
I'm not going to review this one line by line, but
I've looked at this at a high level, and it seems legit... But it is a bit daunting... And for a 'debug' feature,
I'm OK with 'working and looks OK'... If it were more central, I'd worry more about it, but we still have
about 20k lines to upstream and I'd rather more of them be looked at than this if push comes to shove....Warner
Quoted 2 lines
r~
PATCH 15/16 bsd-user: Add SIGSYS to core dump signals.
In reply to Warner LoshSIGSYS 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
@@ -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
Re: [PATCH 15/16] bsd-user: Add SIGSYS to core dump signals.
Quoted 7 lines · Warner Losh
SIGSYS creates a core by default if uncaught. Follow that here. Sort with the same order as is in the kernel.
Signed-off-by Warner Losh<[email protected]>--- bsd-user/signal.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
r~
PATCH 16/16 bsd-user: Implement SIGSYS on arm
In reply to Warner LoshWhen 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
@@ -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
Re: [PATCH 16/16] bsd-user: Implement SIGSYS on arm
Quoted 7 lines · Warner Losh
When a system call returns ENOSYS, send a SIGSYS to the process (to generate a core dump).
Signed-off-by Warner Losh<[email protected]>--- bsd-user/arm/target_arch_cpu.h | 8 ++++++++ 1 file changed, 8 insertions(+)
r~