1*355f6c61Stsutsui /* $NetBSD: linux_ptrace.c,v 1.36 2022/09/05 14:14:42 tsutsui Exp $ */
20f4ddbecStron
30f4ddbecStron /*-
40f4ddbecStron * Copyright (c) 1999 The NetBSD Foundation, Inc.
50f4ddbecStron * All rights reserved.
60f4ddbecStron *
70f4ddbecStron * This code is derived from software contributed to The NetBSD Foundation
80f4ddbecStron * by Matthias Scheler.
90f4ddbecStron *
100f4ddbecStron * Redistribution and use in source and binary forms, with or without
110f4ddbecStron * modification, are permitted provided that the following conditions
120f4ddbecStron * are met:
130f4ddbecStron * 1. Redistributions of source code must retain the above copyright
140f4ddbecStron * notice, this list of conditions and the following disclaimer.
150f4ddbecStron * 2. Redistributions in binary form must reproduce the above copyright
160f4ddbecStron * notice, this list of conditions and the following disclaimer in the
170f4ddbecStron * documentation and/or other materials provided with the distribution.
180f4ddbecStron *
190f4ddbecStron * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
200f4ddbecStron * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
210f4ddbecStron * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220f4ddbecStron * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
230f4ddbecStron * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
240f4ddbecStron * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
250f4ddbecStron * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
260f4ddbecStron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
270f4ddbecStron * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
280f4ddbecStron * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
290f4ddbecStron * POSSIBILITY OF SUCH DAMAGE.
300f4ddbecStron */
310f4ddbecStron
32dab6ef8bSlukem #include <sys/cdefs.h>
33*355f6c61Stsutsui __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.36 2022/09/05 14:14:42 tsutsui Exp $");
34dab6ef8bSlukem
350f4ddbecStron #include <sys/param.h>
360f4ddbecStron #include <sys/mount.h>
370f4ddbecStron #include <sys/proc.h>
380f4ddbecStron #include <sys/ptrace.h>
390f4ddbecStron #include <sys/systm.h>
402e045593Smatt #include <sys/syscall.h>
410f4ddbecStron #include <sys/syscallargs.h>
42ad1c55c7Sjdolecek #include <uvm/uvm_extern.h>
430f4ddbecStron
4462397f55Stron #include <machine/reg.h>
4562397f55Stron
460f4ddbecStron #include <compat/linux/common/linux_types.h>
470f4ddbecStron #include <compat/linux/common/linux_ptrace.h>
480f4ddbecStron #include <compat/linux/common/linux_signal.h>
490f4ddbecStron
500f4ddbecStron #include <compat/linux/common/linux_util.h>
510f4ddbecStron #include <compat/linux/common/linux_machdep.h>
52ad1c55c7Sjdolecek #include <compat/linux/common/linux_emuldata.h>
53ad1c55c7Sjdolecek #include <compat/linux/common/linux_exec.h> /* for emul_linux */
540f4ddbecStron
550f4ddbecStron #include <compat/linux/linux_syscallargs.h>
560f4ddbecStron
57ad1c55c7Sjdolecek #include <lib/libkern/libkern.h> /* for offsetof() */
58ad1c55c7Sjdolecek
59f5dfa6e4Stron struct linux_reg {
60f5dfa6e4Stron long ebx;
61f5dfa6e4Stron long ecx;
62f5dfa6e4Stron long edx;
63f5dfa6e4Stron long esi;
64f5dfa6e4Stron long edi;
65f5dfa6e4Stron long ebp;
66f5dfa6e4Stron long eax;
6737aedd06Slukem long xds, xes; /* unsigned short ds, __ds, es, __es; */
6837aedd06Slukem long __fs, __gs; /* unsigned short fs, __fs, gs, __gs; */
69f5dfa6e4Stron long orig_eax;
70f5dfa6e4Stron long eip;
7137aedd06Slukem long xcs; /* unsigned short cs, __cs; */
72f5dfa6e4Stron long eflags;
73f5dfa6e4Stron long esp;
7437aedd06Slukem long xss; /* unsigned short ss, __ss; */
75f5dfa6e4Stron };
76f5dfa6e4Stron
77ad1c55c7Sjdolecek /* structure used for storing floating point context */
7881ed0870Sjunyoung struct linux_fpctx {
79ad1c55c7Sjdolecek long cwd;
80ad1c55c7Sjdolecek long swd;
81ad1c55c7Sjdolecek long twd;
82ad1c55c7Sjdolecek long fip;
83ad1c55c7Sjdolecek long fcs;
84ad1c55c7Sjdolecek long foo;
85ad1c55c7Sjdolecek long fos;
86ad1c55c7Sjdolecek long st_space[20];
87ad1c55c7Sjdolecek };
88ad1c55c7Sjdolecek
89ad1c55c7Sjdolecek /* user struct for linux process - this is used for Linux ptrace emulation */
90ad1c55c7Sjdolecek /* most of it is junk only used by gdb */
91ad1c55c7Sjdolecek struct linux_user {
92ad1c55c7Sjdolecek struct linux_reg regs; /* registers */
93ad1c55c7Sjdolecek int u_fpvalid; /* true if math co-processor being used. */
9481ed0870Sjunyoung struct linux_fpctx i387; /* Math Co-processor registers. */
95ad1c55c7Sjdolecek /* The rest of this junk is to help gdb figure out what goes where */
96ad1c55c7Sjdolecek #define lusr_startgdb u_tsize
97ad1c55c7Sjdolecek unsigned long int u_tsize; /* Text segment size (pages). */
98ad1c55c7Sjdolecek unsigned long int u_dsize; /* Data segment size (pages). */
99ad1c55c7Sjdolecek unsigned long int u_ssize; /* Stack segment size (pages). */
100ad1c55c7Sjdolecek unsigned long start_code; /* Starting virtual address of text. */
10181ed0870Sjunyoung unsigned long start_stack; /* Starting virtual address of stack
10281ed0870Sjunyoung area. This is actually the bottom of
10381ed0870Sjunyoung the stack, the top of the stack is
10481ed0870Sjunyoung always found in the esp register. */
105ad1c55c7Sjdolecek long int __signal; /* Signal that caused the core dump. */
106ad1c55c7Sjdolecek int __reserved; /* unused */
10781ed0870Sjunyoung void *u_ar0; /* Used by gdb to help find the values
10881ed0870Sjunyoung for the registers. */
10981ed0870Sjunyoung struct linux_fpctx *u_fpstate; /* Math Co-processor pointer. */
110ad1c55c7Sjdolecek unsigned long __magic; /* To uniquely identify a core file */
111ad1c55c7Sjdolecek char u_comm[32]; /* User command that was responsible */
112ad1c55c7Sjdolecek int u_debugreg[8];
113ad1c55c7Sjdolecek #define u_debugreg_end u_debugreg[7]
114ad1c55c7Sjdolecek };
115ad1c55c7Sjdolecek
116ad1c55c7Sjdolecek #define LUSR_OFF(member) offsetof(struct linux_user, member)
11762397f55Stron
118bfb6f314Sad int linux_ptrace_disabled = 1; /* bitrotted */
119bfb6f314Sad
1200f4ddbecStron int
linux_sys_ptrace_arch(struct lwp * l,const struct linux_sys_ptrace_args * uap,register_t * retval)1213c507045Srmind linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap,
1223c507045Srmind register_t *retval)
1230f4ddbecStron {
1247e2790cfSdsl /* {
1250f4ddbecStron syscallarg(int) request;
1260f4ddbecStron syscallarg(int) pid;
1270f4ddbecStron syscallarg(int) addr;
1280f4ddbecStron syscallarg(int) data;
1297e2790cfSdsl } */
1303c507045Srmind struct proc *p = l->l_proc, *t;
13146141a31Sthorpej struct lwp *lt;
132ad1c55c7Sjdolecek struct reg *regs = NULL;
133ad1c55c7Sjdolecek struct fpreg *fpregs = NULL;
134ad1c55c7Sjdolecek struct linux_reg *linux_regs = NULL;
13581ed0870Sjunyoung struct linux_fpctx *linux_fpregs = NULL;
13633fa5ccbSchs struct linux_emuldata *led;
1373c507045Srmind int request, error, addr;
13895739e63Sdsl size_t fp_size;
1390f4ddbecStron
140bfb6f314Sad if (linux_ptrace_disabled)
141bfb6f314Sad return ENOSYS;
142bfb6f314Sad
1433c507045Srmind error = 0;
14462397f55Stron request = SCARG(uap, request);
1450f4ddbecStron
1463c507045Srmind switch (request) {
1473c507045Srmind case LINUX_PTRACE_PEEKUSR:
1483c507045Srmind case LINUX_PTRACE_POKEUSR:
1493c507045Srmind break;
1503c507045Srmind case LINUX_PTRACE_GETREGS:
1513c507045Srmind case LINUX_PTRACE_SETREGS:
1523c507045Srmind regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
1533c507045Srmind linux_regs = kmem_alloc(sizeof(struct linux_reg), KM_SLEEP);
1543c507045Srmind if (request == LINUX_PTRACE_SETREGS) {
1553c507045Srmind error = copyin((void *)SCARG(uap, data), linux_regs,
1563c507045Srmind sizeof(struct linux_reg));
1573c507045Srmind if (error) {
1583c507045Srmind goto out;
1593c507045Srmind }
1603c507045Srmind }
1613c507045Srmind break;
1623c507045Srmind case LINUX_PTRACE_GETFPREGS:
1633c507045Srmind case LINUX_PTRACE_SETFPREGS:
1643c507045Srmind fpregs = kmem_alloc(sizeof(struct fpreg), KM_SLEEP);
1653c507045Srmind linux_fpregs = kmem_alloc(sizeof(struct linux_fpctx), KM_SLEEP);
1663c507045Srmind if (request == LINUX_PTRACE_SETFPREGS) {
1673c507045Srmind error = copyin((void *)SCARG(uap, data), linux_fpregs,
1683c507045Srmind sizeof(struct linux_fpctx));
1693c507045Srmind if (error) {
1703c507045Srmind goto out;
1713c507045Srmind }
1723c507045Srmind }
1733c507045Srmind break;
1743c507045Srmind default:
1753c507045Srmind error = EIO;
1763c507045Srmind goto out;
1773c507045Srmind }
17862397f55Stron
1793c507045Srmind /* Find the process we are supposed to be operating on. */
1800eaaa024Sad mutex_enter(&proc_lock);
1813c507045Srmind if ((t = proc_find(SCARG(uap, pid))) == NULL) {
1820eaaa024Sad mutex_exit(&proc_lock);
183b00a9731Schristos error = ESRCH;
184b00a9731Schristos goto out;
1853c507045Srmind }
1863c507045Srmind mutex_enter(t->p_lock);
18762397f55Stron
18862397f55Stron /*
1893c507045Srmind * You cannot do what you want to the process if:
1903c507045Srmind * 1. It is not being traced at all,
19162397f55Stron */
1923c507045Srmind if (!ISSET(t->p_slflag, PSL_TRACED)) {
1933c507045Srmind mutex_exit(t->p_lock);
1940eaaa024Sad mutex_exit(&proc_lock);
1953c507045Srmind error = EPERM;
1963c507045Srmind goto out;
1973c507045Srmind }
19862397f55Stron /*
199a69b333eSkamil * 2. It is not being traced by _you_, or
200a69b333eSkamil * 3. It is not currently stopped.
20162397f55Stron */
202a69b333eSkamil if (t->p_pptr != p || t->p_stat != SSTOP || !t->p_waited) {
2033c507045Srmind mutex_exit(t->p_lock);
2040eaaa024Sad mutex_exit(&proc_lock);
2053c507045Srmind error = EBUSY;
2063c507045Srmind goto out;
2073c507045Srmind }
2080eaaa024Sad mutex_exit(&proc_lock);
2093c507045Srmind /* XXX: ptrace needs revamp for multi-threading support. */
2103c507045Srmind if (t->p_nlwps > 1) {
2113c507045Srmind mutex_exit(t->p_lock);
2123c507045Srmind error = ENOSYS;
2133c507045Srmind goto out;
2143c507045Srmind }
21546141a31Sthorpej lt = LIST_FIRST(&t->p_lwps);
21662397f55Stron *retval = 0;
21762397f55Stron
21862397f55Stron switch (request) {
21962397f55Stron case LINUX_PTRACE_GETREGS:
22046141a31Sthorpej error = process_read_regs(lt, regs);
2213c507045Srmind mutex_exit(t->p_lock);
2223c507045Srmind if (error) {
2233c507045Srmind break;
2243c507045Srmind }
22541aa5859Sriastradh memset(linux_regs, 0, sizeof(*linux_regs));
226ad1c55c7Sjdolecek linux_regs->ebx = regs->r_ebx;
227ad1c55c7Sjdolecek linux_regs->ecx = regs->r_ecx;
228ad1c55c7Sjdolecek linux_regs->edx = regs->r_edx;
229ad1c55c7Sjdolecek linux_regs->esi = regs->r_esi;
230ad1c55c7Sjdolecek linux_regs->edi = regs->r_edi;
231ad1c55c7Sjdolecek linux_regs->ebp = regs->r_ebp;
232ad1c55c7Sjdolecek linux_regs->eax = regs->r_eax;
233ad1c55c7Sjdolecek linux_regs->xds = regs->r_ds;
234ad1c55c7Sjdolecek linux_regs->xes = regs->r_es;
235ad1c55c7Sjdolecek linux_regs->orig_eax = regs->r_eax; /* XXX is this correct? */
236ad1c55c7Sjdolecek linux_regs->eip = regs->r_cs + regs->r_eip;
237ad1c55c7Sjdolecek linux_regs->xcs = regs->r_cs;
238ad1c55c7Sjdolecek linux_regs->eflags = regs->r_eflags;
239ad1c55c7Sjdolecek linux_regs->esp = regs->r_esp;
240ad1c55c7Sjdolecek linux_regs->xss = regs->r_ss;
24162397f55Stron
24253524e44Schristos error = copyout(linux_regs, (void *)SCARG(uap, data),
24362397f55Stron sizeof(struct linux_reg));
2443c507045Srmind break;
245ad1c55c7Sjdolecek
24662397f55Stron case LINUX_PTRACE_SETREGS:
247ad1c55c7Sjdolecek regs->r_ebx = linux_regs->ebx;
248ad1c55c7Sjdolecek regs->r_ecx = linux_regs->ecx;
249ad1c55c7Sjdolecek regs->r_edx = linux_regs->edx;
250ad1c55c7Sjdolecek regs->r_esi = linux_regs->esi;
251ad1c55c7Sjdolecek regs->r_edi = linux_regs->edi;
252ad1c55c7Sjdolecek regs->r_ebp = linux_regs->ebp;
253ad1c55c7Sjdolecek regs->r_eax = linux_regs->eax;
254ad1c55c7Sjdolecek regs->r_ds = linux_regs->xds;
255ad1c55c7Sjdolecek regs->r_es = linux_regs->xes;
256ad1c55c7Sjdolecek regs->r_eip = linux_regs->eip - linux_regs->xcs;
257ad1c55c7Sjdolecek regs->r_cs = linux_regs->xcs;
258ad1c55c7Sjdolecek regs->r_eflags = linux_regs->eflags;
259ad1c55c7Sjdolecek regs->r_esp = linux_regs->esp;
260ad1c55c7Sjdolecek regs->r_ss = linux_regs->xss;
26162397f55Stron
26246141a31Sthorpej error = process_write_regs(lt, regs);
2633c507045Srmind mutex_exit(t->p_lock);
2643c507045Srmind break;
265ad1c55c7Sjdolecek
266ad1c55c7Sjdolecek case LINUX_PTRACE_GETFPREGS:
26795739e63Sdsl fp_size = sizeof fpregs;
26895739e63Sdsl error = process_read_fpregs(lt, fpregs, &fp_size);
2693c507045Srmind mutex_exit(t->p_lock);
2703c507045Srmind if (error) {
2713c507045Srmind break;
2723c507045Srmind }
2733c507045Srmind /* Zero the contents if NetBSD fpreg structure is smaller */
27495739e63Sdsl if (fp_size < sizeof(struct linux_fpctx)) {
27581ed0870Sjunyoung memset(linux_fpregs, '\0', sizeof(struct linux_fpctx));
2763c507045Srmind }
277ad1c55c7Sjdolecek memcpy(linux_fpregs, fpregs,
278d1579b2dSriastradh uimin(sizeof(struct linux_fpctx), fp_size));
27953524e44Schristos error = copyout(linux_fpregs, (void *)SCARG(uap, data),
28081ed0870Sjunyoung sizeof(struct linux_fpctx));
2813c507045Srmind break;
282ad1c55c7Sjdolecek
283ad1c55c7Sjdolecek case LINUX_PTRACE_SETFPREGS:
284ad1c55c7Sjdolecek memset(fpregs, '\0', sizeof(struct fpreg));
285ad1c55c7Sjdolecek memcpy(fpregs, linux_fpregs,
286d1579b2dSriastradh uimin(sizeof(struct linux_fpctx), sizeof(struct fpreg)));
28746141a31Sthorpej error = process_write_regs(lt, regs);
2883c507045Srmind mutex_exit(t->p_lock);
2893c507045Srmind break;
290ad1c55c7Sjdolecek
291ad1c55c7Sjdolecek case LINUX_PTRACE_PEEKUSR:
2923c507045Srmind /* XXX locking */
293ad1c55c7Sjdolecek addr = SCARG(uap, addr);
294ad1c55c7Sjdolecek
295ad1c55c7Sjdolecek error = 0;
296ad1c55c7Sjdolecek if (addr < LUSR_OFF(lusr_startgdb)) {
297ad1c55c7Sjdolecek /* XXX should provide appropriate register */
29833fa5ccbSchs error = ENOTSUP;
299ad1c55c7Sjdolecek } else if (addr == LUSR_OFF(u_tsize))
300ad1c55c7Sjdolecek *retval = p->p_vmspace->vm_tsize;
301ad1c55c7Sjdolecek else if (addr == LUSR_OFF(u_dsize))
302ad1c55c7Sjdolecek *retval = p->p_vmspace->vm_dsize;
303ad1c55c7Sjdolecek else if (addr == LUSR_OFF(u_ssize))
304ad1c55c7Sjdolecek *retval = p->p_vmspace->vm_ssize;
305ad1c55c7Sjdolecek else if (addr == LUSR_OFF(start_code))
306ad1c55c7Sjdolecek *retval = (register_t) p->p_vmspace->vm_taddr;
307ad1c55c7Sjdolecek else if (addr == LUSR_OFF(start_stack))
308ad1c55c7Sjdolecek *retval = (register_t) p->p_vmspace->vm_minsaddr;
309ad1c55c7Sjdolecek else if (addr == LUSR_OFF(u_ar0))
310ad1c55c7Sjdolecek *retval = LUSR_OFF(regs);
311ad1c55c7Sjdolecek else if (addr >= LUSR_OFF(u_debugreg)
312ad1c55c7Sjdolecek && addr <= LUSR_OFF(u_debugreg_end)) {
313ad1c55c7Sjdolecek int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
314ad1c55c7Sjdolecek
315ad1c55c7Sjdolecek /* only do this for Linux processes */
31633fa5ccbSchs if (t->p_emul != &emul_linux) {
31733fa5ccbSchs mutex_exit(t->p_lock);
31833fa5ccbSchs return EINVAL;
319ad1c55c7Sjdolecek }
32033fa5ccbSchs
32133fa5ccbSchs led = lt->l_emuldata;
32233fa5ccbSchs *retval = led->led_debugreg[off];
323ad1c55c7Sjdolecek } else if (addr == LUSR_OFF(__signal)) {
32433fa5ccbSchs error = ENOTSUP;
325ad1c55c7Sjdolecek } else if (addr == LUSR_OFF(u_fpstate)) {
32633fa5ccbSchs error = ENOTSUP;
327ad1c55c7Sjdolecek } else if (addr == LUSR_OFF(__magic)) {
32833fa5ccbSchs error = ENOTSUP;
329ad1c55c7Sjdolecek } else if (addr == LUSR_OFF(u_comm)) {
33033fa5ccbSchs error = ENOTSUP;
331ad1c55c7Sjdolecek } else {
332ad1c55c7Sjdolecek #ifdef DEBUG_LINUX
333ad1c55c7Sjdolecek printf("linux_ptrace: unsupported address: %d\n", addr);
334ad1c55c7Sjdolecek #endif
33533fa5ccbSchs error = ENOTSUP;
336ad1c55c7Sjdolecek }
3373c507045Srmind mutex_exit(t->p_lock);
3383c507045Srmind break;
339ad1c55c7Sjdolecek
340ad1c55c7Sjdolecek case LINUX_PTRACE_POKEUSR:
341ad1c55c7Sjdolecek /* we only support setting debugregs for now */
342ad1c55c7Sjdolecek addr = SCARG(uap, addr);
34333fa5ccbSchs if (addr >= LUSR_OFF(u_debugreg) &&
34433fa5ccbSchs addr <= LUSR_OFF(u_debugreg_end)) {
345ad1c55c7Sjdolecek int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
346ad1c55c7Sjdolecek int data = SCARG(uap, data);
347ad1c55c7Sjdolecek
348ad1c55c7Sjdolecek /* only do this for Linux processes */
3493c507045Srmind if (t->p_emul != &emul_linux) {
35033fa5ccbSchs mutex_exit(t->p_lock);
35133fa5ccbSchs return EINVAL;
352ad1c55c7Sjdolecek }
35333fa5ccbSchs led = lt->l_emuldata;
35433fa5ccbSchs led->led_debugreg[off] = data;
3553c507045Srmind }
35633fa5ccbSchs mutex_exit(t->p_lock);
35733fa5ccbSchs break;
35833fa5ccbSchs
359ad1c55c7Sjdolecek default:
3603c507045Srmind mutex_exit(t->p_lock);
361ad1c55c7Sjdolecek break;
3620f4ddbecStron }
363ad1c55c7Sjdolecek out:
364ad1c55c7Sjdolecek if (regs)
3653c507045Srmind kmem_free(regs, sizeof(*regs));
366ad1c55c7Sjdolecek if (linux_regs)
3673c507045Srmind kmem_free(linux_regs, sizeof(*linux_regs));
3683c507045Srmind if (fpregs)
3693c507045Srmind kmem_free(fpregs, sizeof(*fpregs));
370ad1c55c7Sjdolecek if (linux_fpregs)
3713c507045Srmind kmem_free(linux_fpregs, sizeof(*linux_fpregs));
3723c507045Srmind
3733c507045Srmind return error;
3740f4ddbecStron }
375