commit dae500e6f1d58ab262901de20e0015175431472e Author: Kamil Rytarowski Date: Sat Oct 17 23:57:34 2020 +0200 Fix s87_tw reconstruction to correctly indicate register states Cherry-pick and adapt (without moving the functions to a separate C file). commit d815196083bd4d7a6b2c28130461f92a6d28ba33 Author: mgorny Date: Thu Oct 15 17:43:08 2020 +0000 Fix s87_tw reconstruction to correctly indicate register states Fix the code reconstructing s87_tw (full tag word) from fx_sw (abridged tag word) to correctly represent all register states. The previous code only distinguished between empty/non-empty registers, and assigned 'regular value' to all non-empty registers. The new code explicitly distinguishes the two other tag word values: empty and special. --------------------------------------------------------------------- Module Name: src Committed By: mgorny Date: Thu Oct 15 17:43:09 UTC 2020 Modified Files: src/sys/arch/x86/x86: convert_xmm_s87.c src/tests/lib/libc/sys: t_ptrace_x86_wait.h Log Message: Fix s87_tw reconstruction to correctly indicate register states Fix the code reconstructing s87_tw (full tag word) from fx_sw (abridged tag word) to correctly represent all register states. The previous code only distinguished between empty/non-empty registers, and assigned 'regular value' to all non-empty registers. The new code explicitly distinguishes the two other tag word values: empty and special. To generate a diff of this commit: cvs rdiff -u -r1.5 -r1.6 src/sys/arch/x86/x86/convert_xmm_s87.c cvs rdiff -u -r1.27 -r1.28 src/tests/lib/libc/sys/t_ptrace_x86_wait.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. ------------------------------------------------------------------- diff --git a/sys/arch/x86/x86/fpu.c b/sys/arch/x86/x86/fpu.c index 7ba5e73ee2d8..fafed4169d89 100644 --- a/sys/arch/x86/x86/fpu.c +++ b/sys/arch/x86/x86/fpu.c @@ -710,7 +710,7 @@ fpu_save_area_fork(struct pcb *pcb2, const struct pcb *pcb1) static void process_xmm_to_s87(const struct fxsave *sxmm, struct save87 *s87) { - unsigned int tag, ab_tag; + unsigned int tag, ab_tag, st; const struct fpaccfx *fx_reg; struct fpacc87 *s87_reg; int i; @@ -765,12 +765,28 @@ process_xmm_to_s87(const struct fxsave *sxmm, struct save87 *s87) return; } + /* For ST(i), i = fpu_reg - top, we start with fpu_reg=7. */ + st = 7 - ((sxmm->fx_sw >> 11) & 7); tag = 0; - /* Separate bits of abridged tag word with zeros */ - for (i = 0x80; i != 0; tag <<= 1, i >>= 1) - tag |= ab_tag & i; - /* Replicate and invert so that 0 => 0b11 and 1 => 0b00 */ - s87->s87_tw = (tag | tag >> 1) ^ 0xffff; + for (i = 0x80; i != 0; i >>= 1) { + tag <<= 2; + if (ab_tag & i) { + unsigned int exp; + /* Non-empty - we need to check ST(i) */ + fx_reg = &sxmm->fx_87_ac[st]; + exp = fx_reg->r.f87_exp_sign & 0x7fff; + if (exp == 0) { + if (fx_reg->r.f87_mantissa == 0) + tag |= 1; /* Zero */ + else + tag |= 2; /* Denormal */ + } else if (exp == 0x7fff) + tag |= 2; /* Infinity or NaN */ + } else + tag |= 3; /* Empty */ + st = (st - 1) & 7; + } + s87->s87_tw = tag; } static void commit 8070bbb89c6613b8d8ce2d020e7da2746d00f7ac Author: Kamil Rytarowski Date: Sat Oct 17 20:22:55 2020 +0200 Cherry-pick an redo the patch for: commit 412bf76c8d1a4d0016678605d62fb8e63c7c6048 Author: mgorny Date: Thu Oct 15 17:37:35 2020 +0000 Fix the machine-dependent ptrace requests to respect LWP number Fix the machine-dependent ptrace register-related requests (e.g. PT_GETXMMREGS, PT_GETXSTATE on x86) to correctly respect the LWP number passed as the data argument. Before this change, these requests did not operate on the requested LWP of a multithreaded program. This change required moving ptrace_update_lwp() out of unit scope, and changing ptrace_machdep_dorequest() function to take a pointer to pointer as the second argument, consistently with ptrace_regs(). I am planning to extend the ATF ptrace() register tests in the future to check for regressions in multithreaded programs, as time permits. Reviewed by kamil. ------------------------------------------------------------------ CVS cversion: Module Name: src Committed By: mgorny Date: Thu Oct 15 17:37:36 UTC 2020 Modified Files: src/sys/arch/amd64/amd64: process_machdep.c src/sys/arch/i386/i386: process_machdep.c src/sys/arch/powerpc/include: ptrace.h src/sys/arch/powerpc/powerpc: process_machdep.c src/sys/arch/sh3/include: ptrace.h src/sys/arch/sh3/sh3: process_machdep.c src/sys/kern: sys_ptrace_common.c src/sys/sys: ptrace.h Log Message: Fix the machine-dependent ptrace requests to respect LWP number Fix the machine-dependent ptrace register-related requests (e.g. PT_GETXMMREGS, PT_GETXSTATE on x86) to correctly respect the LWP number passed as the data argument. Before this change, these requests did not operate on the requested LWP of a multithreaded program. This change required moving ptrace_update_lwp() out of unit scope, and changing ptrace_machdep_dorequest() function to take a pointer to pointer as the second argument, consistently with ptrace_regs(). I am planning to extend the ATF ptrace() register tests in the future to check for regressions in multithreaded programs, as time permits. Reviewed by kamil. To generate a diff of this commit: cvs rdiff -u -r1.47 -r1.48 src/sys/arch/amd64/amd64/process_machdep.c cvs rdiff -u -r1.94 -r1.95 src/sys/arch/i386/i386/process_machdep.c cvs rdiff -u -r1.17 -r1.18 src/sys/arch/powerpc/include/ptrace.h cvs rdiff -u -r1.39 -r1.40 src/sys/arch/powerpc/powerpc/process_machdep.c cvs rdiff -u -r1.18 -r1.19 src/sys/arch/sh3/include/ptrace.h cvs rdiff -u -r1.22 -r1.23 src/sys/arch/sh3/sh3/process_machdep.c cvs rdiff -u -r1.83 -r1.84 src/sys/kern/sys_ptrace_common.c cvs rdiff -u -r1.70 -r1.71 src/sys/sys/ptrace.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. ------------------------------------------------------------------ Build tested on sh3, ppc. Build and run tested on amd64 and i386. This version avoids breaking ABI. This patch contains follow-up fixes for: ------------------------------------------------------------------ Module Name: src Committed By: martin Date: Thu Oct 15 18:57:16 UTC 2020 Modified Files: src/sys/arch/powerpc/powerpc: process_machdep.c Log Message: Add missing 'error' declaration To generate a diff of this commit: cvs rdiff -u -r1.40 -r1.41 src/sys/arch/powerpc/powerpc/process_machdep.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. ---------------------------------------------------------------------- Module Name: src Committed By: martin Date: Thu Oct 15 18:58:59 UTC 2020 Modified Files: src/sys/arch/sh3/sh3: process_machdep.c Log Message: Add missing 'error' declaration To generate a diff of this commit: cvs rdiff -u -r1.23 -r1.24 src/sys/arch/sh3/sh3/process_machdep.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. ---------------------------------------------------------------------- Tested on x86 with: void* thread_func(uint64_t* data) { int i; printf("in thread_func, lwp = %d\n", _lwp_self()); sleep((*data & 0x1f) / 3); __asm__ __volatile__ ( "movq (%0), %%mm0\n\t" "int3\n\t" : : "r"(data) : "%mm0" ); printf("out thread_func\n"); return NULL; } uint64_t data1 = 0x0102030405060708; uint64_t data2 = 0x1112131415161718; int main() { int ret; int pid = fork(); assert(pid != -1); if (pid == 0) { int i; pthread_t t2, t3; ret = ptrace(PT_TRACE_ME, 0, NULL, 0); assert(ret != -1); printf("in main, lwp = %d\n", _lwp_self()); ret = pthread_create(&t2, NULL, thread_func, &data1); assert(ret == 0); printf("thread started\n"); ret = pthread_create(&t3, NULL, thread_func, &data2); assert(ret == 0); printf("thread started\n"); ret = pthread_join(t3, NULL); assert(ret == 0); printf("thread joined\n"); ret = pthread_join(t2, NULL); assert(ret == 0); printf("thread joined\n"); _exit(0); } pid_t waited = waitpid(pid, &ret, 0); assert(waited == pid); printf("wait: %d\n", ret); int i, j; for (i = 0; i < 2; i++) { struct fpreg fpr; struct xmmregs fpr; struct xstate xst; struct iovec iov = {&xst, sizeof(xst)}; struct ptrace_siginfo info; ret = ptrace(PT_GET_SIGINFO, pid, &info, sizeof(info)); assert(ret == 0); int j = info.psi_lwpid; ret = ptrace(PT_GETXSTATE, pid, &iov, j); if (ret == 0) printf("lwp %d, xst.mm0: %016llx\n", j, xst.xs_fxsave.fx_87_ac[0].r.f87_mantissa); else printf("lwp %d, xst failed\n", j); ret = ptrace(PT_GETFPREGS, pid, &fpr, j); ret = ptrace(PT_GETXMMREGS, pid, &fpr, j); if (ret == 0) printf("lwp %d, fpr.mm0: %016llx\n", j, fpr.fxstate.fx_87_ac[0].r.f87_mantissa); else printf("lwp %d, fpr failed\n", j); ret = ptrace(PT_CONTINUE, pid, (void*)1, 0); assert(ret == 0); waited = waitpid(pid, &ret, 0); assert(waited == pid); printf("wait: %d\n", ret); } return 0; } diff --git a/sys/arch/amd64/amd64/process_machdep.c b/sys/arch/amd64/amd64/process_machdep.c index 41b48339d9ff..1917a05dcd30 100644 --- a/sys/arch/amd64/amd64/process_machdep.c +++ b/sys/arch/amd64/amd64/process_machdep.c @@ -367,6 +367,100 @@ ptrace_machdep_dorequest( return 0; } +static int +ptrace_update_lwp2(struct proc *t, struct lwp **lt, lwpid_t lid) +{ + if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1) + return 0; + + mutex_enter(t->p_lock); + lwp_delref2(*lt); + + *lt = lwp_find(t, lid); + if (*lt == NULL) { + mutex_exit(t->p_lock); + return ESRCH; + } + + if ((*lt)->l_flag & LW_SYSTEM) { + mutex_exit(t->p_lock); + *lt = NULL; + return EINVAL; + } + + lwp_addref(*lt); + mutex_exit(t->p_lock); + + return 0; +} + +int +ptrace_machdep_dorequest2( + struct lwp *l, + struct lwp **lt, + int req, + void *addr, + int data +) +{ + struct uio uio; + struct iovec iov; + struct vmspace *vm; + int error; + int write = 0; + + switch (req) { + case PT_SETXSTATE: + write = 1; + + /* FALLTHROUGH */ + case PT_GETXSTATE: + /* write = 0 done above. */ + if ((error = ptrace_update_lwp2((*lt)->l_proc, lt, data)) != 0) + return error; + if (!process_machdep_validxstate((*lt)->l_proc)) + return EINVAL; + if (__predict_false(l->l_proc->p_flag & PK_32)) { + struct netbsd32_iovec user_iov; + if ((error = copyin(addr, &user_iov, sizeof(user_iov))) + != 0) + return error; + + iov.iov_base = NETBSD32PTR64(user_iov.iov_base); + iov.iov_len = user_iov.iov_len; + } else { + struct iovec user_iov; + if ((error = copyin(addr, &user_iov, sizeof(user_iov))) + != 0) + return error; + + iov.iov_base = user_iov.iov_base; + iov.iov_len = user_iov.iov_len; + } + + error = proc_vmspace_getref(l->l_proc, &vm); + if (error) + return error; + if (iov.iov_len > sizeof(struct xstate)) + iov.iov_len = sizeof(struct xstate); + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + uio.uio_offset = 0; + uio.uio_resid = iov.iov_len; + uio.uio_rw = write ? UIO_WRITE : UIO_READ; + uio.uio_vmspace = vm; + error = process_machdep_doxstate(l, *lt, &uio); + uvmspace_free(vm); + return error; + } + +#ifdef DIAGNOSTIC + panic("ptrace_machdep: impossible"); +#endif + + return 0; +} + /* * The following functions are used by both ptrace(2) and procfs. */ diff --git a/sys/arch/i386/i386/process_machdep.c b/sys/arch/i386/i386/process_machdep.c index fddee872d49f..97a05d40ad3a 100644 --- a/sys/arch/i386/i386/process_machdep.c +++ b/sys/arch/i386/i386/process_machdep.c @@ -347,6 +347,114 @@ ptrace_machdep_dorequest( return 0; } +static int +ptrace_update_lwp2(struct proc *t, struct lwp **lt, lwpid_t lid) +{ + if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1) + return 0; + + mutex_enter(t->p_lock); + lwp_delref2(*lt); + + *lt = lwp_find(t, lid); + if (*lt == NULL) { + mutex_exit(t->p_lock); + return ESRCH; + } + + if ((*lt)->l_flag & LW_SYSTEM) { + mutex_exit(t->p_lock); + *lt = NULL; + return EINVAL; + } + + lwp_addref(*lt); + mutex_exit(t->p_lock); + + return 0; +} + +int +ptrace_machdep_dorequest2( + struct lwp *l, + struct lwp **lt, + int req, + void *addr, + int data +) +{ + struct uio uio; + struct iovec iov; + struct iovec user_iov; + struct vmspace *vm; + int error; + int write = 0; + + switch (req) { + case PT_SETXMMREGS: + write = 1; + + /* FALLTHROUGH */ + case PT_GETXMMREGS: + /* write = 0 done above. */ + if ((error = ptrace_update_lwp2((*lt)->l_proc, lt, data)) != 0) + return error; + if (!process_machdep_validxmmregs((*lt)->l_proc)) + return (EINVAL); + error = proc_vmspace_getref(l->l_proc, &vm); + if (error) { + return error; + } + iov.iov_base = addr; + iov.iov_len = sizeof(struct xmmregs); + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + uio.uio_offset = 0; + uio.uio_resid = sizeof(struct xmmregs); + uio.uio_rw = write ? UIO_WRITE : UIO_READ; + uio.uio_vmspace = vm; + error = process_machdep_doxmmregs(l, *lt, &uio); + uvmspace_free(vm); + return error; + + case PT_SETXSTATE: + write = 1; + + /* FALLTHROUGH */ + case PT_GETXSTATE: + /* write = 0 done above. */ + if ((error = ptrace_update_lwp2((*lt)->l_proc, lt, data)) != 0) + return error; + if (!process_machdep_validxstate((*lt)->l_proc)) + return EINVAL; + if ((error = copyin(addr, &user_iov, sizeof(user_iov))) != 0) + return error; + error = proc_vmspace_getref(l->l_proc, &vm); + if (error) { + return error; + } + iov.iov_base = user_iov.iov_base; + iov.iov_len = user_iov.iov_len; + if (iov.iov_len > sizeof(struct xstate)) + iov.iov_len = sizeof(struct xstate); + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + uio.uio_offset = 0; + uio.uio_resid = iov.iov_len; + uio.uio_rw = write ? UIO_WRITE : UIO_READ; + uio.uio_vmspace = vm; + error = process_machdep_doxstate(l, *lt, &uio); + uvmspace_free(vm); + return error; + } + +#ifdef DIAGNOSTIC + panic("ptrace_machdep: impossible"); +#endif + + return 0; +} + /* * The following functions are used by both ptrace(2) and procfs. */ diff --git a/sys/arch/powerpc/powerpc/process_machdep.c b/sys/arch/powerpc/powerpc/process_machdep.c index be5f3342716f..7147a677d003 100644 --- a/sys/arch/powerpc/powerpc/process_machdep.c +++ b/sys/arch/powerpc/powerpc/process_machdep.c @@ -219,6 +219,69 @@ ptrace_machdep_dorequest(struct lwp *l, struct lwp *lt, return (0); } +static int +ptrace_update_lwp2(struct proc *t, struct lwp **lt, lwpid_t lid) +{ + if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1) + return 0; + + mutex_enter(t->p_lock); + lwp_delref2(*lt); + + *lt = lwp_find(t, lid); + if (*lt == NULL) { + mutex_exit(t->p_lock); + return ESRCH; + } + + if ((*lt)->l_flag & LW_SYSTEM) { + mutex_exit(t->p_lock); + *lt = NULL; + return EINVAL; + } + + lwp_addref(*lt); + mutex_exit(t->p_lock); + + return 0; +} + +int +ptrace_machdep_dorequest2(struct lwp *l, struct lwp **lt, + int req, void *addr, int data) +{ + struct uio uio; + struct iovec iov; + int write = 0, error; + + switch (req) { + case PT_SETVECREGS: + write = 1; + + case PT_GETVECREGS: + /* write = 0 done above. */ + if ((error = ptrace_update_lwp2((*lt)->l_proc, lt, data)) != 0) + return error; + if (!process_machdep_validvecregs((*lt)->l_proc)) + return (EINVAL); + iov.iov_base = addr; + iov.iov_len = sizeof(struct vreg); + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + uio.uio_offset = 0; + uio.uio_resid = sizeof(struct vreg); + uio.uio_rw = write ? UIO_WRITE : UIO_READ; + uio.uio_vmspace = l->l_proc->p_vmspace; + return process_machdep_dovecregs(l, *lt, &uio); + } + +#ifdef DIAGNOSTIC + panic("ptrace_machdep: impossible"); +#endif + + return (0); +} + /* * The following functions are used by both ptrace(2) and procfs. */ diff --git a/sys/arch/sh3/include/ptrace.h b/sys/arch/sh3/include/ptrace.h index e4bd522d4349..a46bcc1ea384 100644 --- a/sys/arch/sh3/include/ptrace.h +++ b/sys/arch/sh3/include/ptrace.h @@ -94,6 +94,7 @@ #ifdef __HAVE_PTRACE_MACHDEP int ptrace_machdep_dorequest(struct lwp *, struct lwp *, int, void *, int); +int ptrace_machdep_dorequest2(struct lwp *, struct lwp **, int, void *, int); #endif #endif /* _KERNEL */ diff --git a/sys/arch/sh3/sh3/process_machdep.c b/sys/arch/sh3/sh3/process_machdep.c index 82e359f95ea7..20142c7a76fa 100644 --- a/sys/arch/sh3/sh3/process_machdep.c +++ b/sys/arch/sh3/sh3/process_machdep.c @@ -221,6 +221,69 @@ ptrace_machdep_dorequest(struct lwp *l, struct lwp *lt, } } +#ifdef COMPAT_40 +static int +ptrace_update_lwp2(struct proc *t, struct lwp **lt, lwpid_t lid) +{ + if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1) + return 0; + + mutex_enter(t->p_lock); + lwp_delref2(*lt); + + *lt = lwp_find(t, lid); + if (*lt == NULL) { + mutex_exit(t->p_lock); + return ESRCH; + } + + if ((*lt)->l_flag & LW_SYSTEM) { + mutex_exit(t->p_lock); + *lt = NULL; + return EINVAL; + } + + lwp_addref(*lt); + mutex_exit(t->p_lock); + + return 0; +} +#endif + +int +ptrace_machdep_dorequest2(struct lwp *l, struct lwp **lt, + int req, void *addr, int data) +{ + struct uio uio; + struct iovec iov; + int write = 0, error; + + switch (req) { + default: + return EINVAL; + +#ifdef COMPAT_40 + case PT___SETREGS40: + write = 1; + /* FALLTHROUGH*/ + + case PT___GETREGS40: + if ((error = ptrace_update_lwp2((*lt)->l_proc, lt, data)) != 0) + return error; + if (!process_validregs(*lt)) + return EINVAL; + iov.iov_base = addr; + iov.iov_len = sizeof(struct __reg40); + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + uio.uio_offset = 0; + uio.uio_resid = sizeof(struct __reg40); + uio.uio_rw = write ? UIO_WRITE : UIO_READ; + uio.uio_vmspace = l->l_proc->p_vmspace; + return process_machdep_doregs40(l, *lt, &uio); +#endif /* COMPAT_40 */ + } +} #ifdef COMPAT_40 diff --git a/sys/kern/sys_ptrace_common.c b/sys/kern/sys_ptrace_common.c index 63575f488266..c758573d948c 100644 --- a/sys/kern/sys_ptrace_common.c +++ b/sys/kern/sys_ptrace_common.c @@ -1432,7 +1432,7 @@ do_ptrace(struct ptrace_methods *ptm, struct lwp *l, int req, pid_t pid, #ifdef __HAVE_PTRACE_MACHDEP PTRACE_MACHDEP_REQUEST_CASES - error = ptrace_machdep_dorequest(l, lt, req, addr, data); + error = ptrace_machdep_dorequest2(l, <, req, addr, data); break; #endif } diff --git a/sys/sys/ptrace.h b/sys/sys/ptrace.h index eeced2f8134e..57201a4c17ad 100644 --- a/sys/sys/ptrace.h +++ b/sys/sys/ptrace.h @@ -281,6 +281,9 @@ int process_write_regs(struct lwp *, const struct reg *); int ptrace_machdep_dorequest(struct lwp *, struct lwp *, int, void *, int); +int ptrace_machdep_dorequest2(struct lwp *, struct lwp **, int, + void *, int); + #ifndef FIX_SSTEP #define FIX_SSTEP(p) #endif