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.
Modified files: Index: src/sys/arch/amd64/amd64/process_machdep.c diff -u src/sys/arch/amd64/amd64/process_machdep.c:1.47 src/sys/arch/amd64/amd64/process_machdep.c:1.48 --- src/sys/arch/amd64/amd64/process_machdep.c:1.47 Wed Nov 27 09:16:58 2019 +++ src/sys/arch/amd64/amd64/process_machdep.c Thu Oct 15 17:37:35 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: process_machdep.c,v 1.47 2019/11/27 09:16:58 rin Exp $ */ +/* $NetBSD: process_machdep.c,v 1.48 2020/10/15 17:37:35 mgorny Exp $ */ /* * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. @@ -74,7 +74,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.47 2019/11/27 09:16:58 rin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.48 2020/10/15 17:37:35 mgorny Exp $"); #include "opt_xen.h" #include <sys/param.h> @@ -307,7 +307,7 @@ process_machdep_write_xstate(struct lwp int ptrace_machdep_dorequest( struct lwp *l, - struct lwp *lt, + struct lwp **lt, int req, void *addr, int data @@ -326,7 +326,9 @@ ptrace_machdep_dorequest( /* FALLTHROUGH */ case PT_GETXSTATE: /* write = false done above. */ - if (!process_machdep_validfpu(lt->l_proc)) + if ((error = ptrace_update_lwp((*lt)->l_proc, lt, data)) != 0) + return error; + if (!process_machdep_validfpu((*lt)->l_proc)) return EINVAL; if (__predict_false(l->l_proc->p_flag & PK_32)) { struct netbsd32_iovec user_iov; @@ -357,7 +359,7 @@ ptrace_machdep_dorequest( 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); + error = process_machdep_doxstate(l, *lt, &uio); uvmspace_free(vm); return error; @@ -367,8 +369,10 @@ ptrace_machdep_dorequest( /* FALLTHROUGH */ case PT_GETXMMREGS: /* only for COMPAT_NETBSD32 */ /* write = false done above. */ + if ((error = ptrace_update_lwp((*lt)->l_proc, lt, data)) != 0) + return error; MODULE_HOOK_CALL(netbsd32_process_doxmmregs_hook, - (l, lt, addr, write), EINVAL, error); + (l, *lt, addr, write), EINVAL, error); return error; } Index: src/sys/arch/i386/i386/process_machdep.c diff -u src/sys/arch/i386/i386/process_machdep.c:1.94 src/sys/arch/i386/i386/process_machdep.c:1.95 --- src/sys/arch/i386/i386/process_machdep.c:1.94 Tue Aug 6 02:04:43 2019 +++ src/sys/arch/i386/i386/process_machdep.c Thu Oct 15 17:37:35 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: process_machdep.c,v 1.94 2019/08/06 02:04:43 kamil Exp $ */ +/* $NetBSD: process_machdep.c,v 1.95 2020/10/15 17:37:35 mgorny Exp $ */ /*- * Copyright (c) 1998, 2000, 2001, 2008 The NetBSD Foundation, Inc. @@ -75,7 +75,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.94 2019/08/06 02:04:43 kamil Exp $"); +__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.95 2020/10/15 17:37:35 mgorny Exp $"); #include "opt_ptrace.h" @@ -273,7 +273,7 @@ process_machdep_write_xmmregs(struct lwp int ptrace_machdep_dorequest( struct lwp *l, - struct lwp *lt, + struct lwp **lt, int req, void *addr, int data @@ -293,7 +293,9 @@ ptrace_machdep_dorequest( /* FALLTHROUGH */ case PT_GETXMMREGS: /* write = 0 done above. */ - if (!process_machdep_validxmmregs(lt->l_proc)) + if ((error = ptrace_update_lwp((*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) { @@ -307,7 +309,7 @@ ptrace_machdep_dorequest( 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); + error = process_machdep_doxmmregs(l, *lt, &uio); uvmspace_free(vm); return error; @@ -317,7 +319,9 @@ ptrace_machdep_dorequest( /* FALLTHROUGH */ case PT_GETXSTATE: /* write = 0 done above. */ - if (!process_machdep_validxstate(lt->l_proc)) + if ((error = ptrace_update_lwp((*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; @@ -335,7 +339,7 @@ ptrace_machdep_dorequest( 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); + error = process_machdep_doxstate(l, *lt, &uio); uvmspace_free(vm); return error; } Index: src/sys/arch/powerpc/include/ptrace.h diff -u src/sys/arch/powerpc/include/ptrace.h:1.17 src/sys/arch/powerpc/include/ptrace.h:1.18 --- src/sys/arch/powerpc/include/ptrace.h:1.17 Sun Jun 21 00:39:59 2020 +++ src/sys/arch/powerpc/include/ptrace.h Thu Oct 15 17:37:35 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: ptrace.h,v 1.17 2020/06/21 00:39:59 rin Exp $ */ +/* $NetBSD: ptrace.h,v 1.18 2020/10/15 17:37:35 mgorny Exp $ */ #ifndef _POWERPC_PTRACE_H #define _POWERPC_PTRACE_H @@ -41,7 +41,7 @@ case PT_GETVECREGS: \ case PT_SETVECREGS: -int ptrace_machdep_dorequest(struct lwp *, struct lwp *, int, void *, int); +int ptrace_machdep_dorequest(struct lwp *, struct lwp **, int, void *, int); int process_machdep_dovecregs(struct lwp *, struct lwp *, struct uio *); int process_machdep_validvecregs(struct proc *); Index: src/sys/arch/powerpc/powerpc/process_machdep.c diff -u src/sys/arch/powerpc/powerpc/process_machdep.c:1.39 src/sys/arch/powerpc/powerpc/process_machdep.c:1.40 --- src/sys/arch/powerpc/powerpc/process_machdep.c:1.39 Mon Jul 6 09:34:18 2020 +++ src/sys/arch/powerpc/powerpc/process_machdep.c Thu Oct 15 17:37:36 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: process_machdep.c,v 1.39 2020/07/06 09:34:18 rin Exp $ */ +/* $NetBSD: process_machdep.c,v 1.40 2020/10/15 17:37:36 mgorny Exp $ */ /* * Copyright (C) 1995, 1996 Wolfgang Solfrank. @@ -32,7 +32,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.39 2020/07/06 09:34:18 rin Exp $"); +__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.40 2020/10/15 17:37:36 mgorny Exp $"); #ifdef _KERNEL_OPT #include "opt_altivec.h" @@ -188,7 +188,7 @@ process_machdep_write_vecregs(struct lwp } int -ptrace_machdep_dorequest(struct lwp *l, struct lwp *lt, +ptrace_machdep_dorequest(struct lwp *l, struct lwp **lt, int req, void *addr, int data) { struct uio uio; @@ -201,7 +201,9 @@ ptrace_machdep_dorequest(struct lwp *l, case PT_GETVECREGS: /* write = 0 done above. */ - if (!process_machdep_validvecregs(lt->l_proc)) + if ((error = ptrace_update_lwp((*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); @@ -211,7 +213,7 @@ ptrace_machdep_dorequest(struct lwp *l, 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); + return process_machdep_dovecregs(l, *lt, &uio); } #ifdef DIAGNOSTIC Index: src/sys/arch/sh3/include/ptrace.h diff -u src/sys/arch/sh3/include/ptrace.h:1.18 src/sys/arch/sh3/include/ptrace.h:1.19 --- src/sys/arch/sh3/include/ptrace.h:1.18 Mon Sep 14 05:04:05 2020 +++ src/sys/arch/sh3/include/ptrace.h Thu Oct 15 17:37:36 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: ptrace.h,v 1.18 2020/09/14 05:04:05 mrg Exp $ */ +/* $NetBSD: ptrace.h,v 1.19 2020/10/15 17:37:36 mgorny Exp $ */ /* * Copyright (c) 1993 Christopher G. Demetriou @@ -95,7 +95,7 @@ #endif /* COMPAT_40 */ #ifdef __HAVE_PTRACE_MACHDEP -int ptrace_machdep_dorequest(struct lwp *, struct lwp *, int, void *, int); +int ptrace_machdep_dorequest(struct lwp *, struct lwp **, int, void *, int); #endif #define PTRACE_LWP_GETPRIVATE(l) (l)->l_md.md_regs->tf_gbr Index: src/sys/arch/sh3/sh3/process_machdep.c diff -u src/sys/arch/sh3/sh3/process_machdep.c:1.22 src/sys/arch/sh3/sh3/process_machdep.c:1.23 --- src/sys/arch/sh3/sh3/process_machdep.c:1.22 Wed Nov 20 19:37:52 2019 +++ src/sys/arch/sh3/sh3/process_machdep.c Thu Oct 15 17:37:36 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: process_machdep.c,v 1.22 2019/11/20 19:37:52 pgoyette Exp $ */ +/* $NetBSD: process_machdep.c,v 1.23 2020/10/15 17:37:36 mgorny Exp $ */ /* * Copyright (c) 1993 The Regents of the University of California. @@ -77,7 +77,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.22 2019/11/20 19:37:52 pgoyette Exp $"); +__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.23 2020/10/15 17:37:36 mgorny Exp $"); #include <sys/param.h> #include <sys/systm.h> @@ -183,7 +183,7 @@ process_write_regs(struct lwp *l, const #ifdef __HAVE_PTRACE_MACHDEP int -ptrace_machdep_dorequest(struct lwp *l, struct lwp *lt, +ptrace_machdep_dorequest(struct lwp *l, struct lwp **lt, int req, void *addr, int data) { struct uio uio; @@ -200,7 +200,9 @@ ptrace_machdep_dorequest(struct lwp *l, /* FALLTHROUGH*/ case PT___GETREGS40: - if (!process_validregs(lt)) + if ((error = ptrace_update_lwp((*lt)->l_proc, lt, data)) != 0) + return error; + if (!process_validregs(*lt)) return EINVAL; iov.iov_base = addr; iov.iov_len = sizeof(struct __reg40); @@ -210,7 +212,7 @@ ptrace_machdep_dorequest(struct lwp *l, 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); + return process_machdep_doregs40(l, *lt, &uio); #endif /* COMPAT_40 */ } } Index: src/sys/kern/sys_ptrace_common.c diff -u src/sys/kern/sys_ptrace_common.c:1.83 src/sys/kern/sys_ptrace_common.c:1.84 --- src/sys/kern/sys_ptrace_common.c:1.83 Sat May 30 08:41:22 2020 +++ src/sys/kern/sys_ptrace_common.c Thu Oct 15 17:37:36 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: sys_ptrace_common.c,v 1.83 2020/05/30 08:41:22 maxv Exp $ */ +/* $NetBSD: sys_ptrace_common.c,v 1.84 2020/10/15 17:37:36 mgorny Exp $ */ /*- * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. @@ -118,7 +118,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.83 2020/05/30 08:41:22 maxv Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sys_ptrace_common.c,v 1.84 2020/10/15 17:37:36 mgorny Exp $"); #ifdef _KERNEL_OPT #include "opt_ptrace.h" @@ -565,7 +565,7 @@ ptrace_needs_hold(int req) } } -static int +int ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid) { if (lid == 0 || lid == (*lt)->l_lid || t->p_nlwps == 1) @@ -1569,7 +1569,7 @@ do_ptrace(struct ptrace_methods *ptm, st #ifdef __HAVE_PTRACE_MACHDEP PTRACE_MACHDEP_REQUEST_CASES - error = ptrace_machdep_dorequest(l, lt, req, addr, data); + error = ptrace_machdep_dorequest(l, <, req, addr, data); break; #endif } Index: src/sys/sys/ptrace.h diff -u src/sys/sys/ptrace.h:1.70 src/sys/sys/ptrace.h:1.71 --- src/sys/sys/ptrace.h:1.70 Thu May 14 13:32:15 2020 +++ src/sys/sys/ptrace.h Thu Oct 15 17:37:36 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: ptrace.h,v 1.70 2020/05/14 13:32:15 kamil Exp $ */ +/* $NetBSD: ptrace.h,v 1.71 2020/10/15 17:37:36 mgorny Exp $ */ /*- * Copyright (c) 1984, 1993 @@ -243,6 +243,7 @@ struct ptrace_methods { int ptrace_init(void); int ptrace_fini(void); +int ptrace_update_lwp(struct proc *t, struct lwp **lt, lwpid_t lid); void ptrace_hooks(void); int process_doregs(struct lwp *, struct lwp *, struct uio *); @@ -337,7 +338,7 @@ int process_write_regs(struct lwp *, con #endif #endif -int ptrace_machdep_dorequest(struct lwp *, struct lwp *, int, +int ptrace_machdep_dorequest(struct lwp *, struct lwp **, int, void *, int); #ifndef FIX_SSTEP