1/*	$NetBSD: linux_ptrace.c,v 1.23 2021/09/07 11:43:04 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Scheler.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32
33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.23 2021/09/07 11:43:04 riastradh Exp $");
35
36#include <sys/param.h>
37#include <sys/mount.h>
38#include <sys/proc.h>
39#include <sys/ptrace.h>
40#include <sys/systm.h>
41#include <sys/syscallargs.h>
42#include <uvm/uvm_extern.h>
43
44#include <machine/reg.h>
45#include <machine/pcb.h>
46
47#include <compat/linux/common/linux_types.h>
48#include <compat/linux/common/linux_ptrace.h>
49#include <compat/linux/common/linux_signal.h>
50
51#include <compat/linux/common/linux_util.h>
52#include <compat/linux/common/linux_machdep.h>
53#include <compat/linux/common/linux_emuldata.h>
54#include <compat/linux/common/linux_exec.h>	/* for emul_linux */
55
56#include <compat/linux/linux_syscallargs.h>
57
58#include <lib/libkern/libkern.h>	/* for offsetof() */
59
60/*
61 * On ARMv2, uregs contains R0--R15, orig_R0.
62 * On ARMv3 and later, it's R0--R15, CPSR, orig_R0.
63 * As far as I can see, Linux doesn't initialise orig_R0 on ARMv2, so we
64 * just produce the ARMv3 version.
65 */
66
67struct linux_reg {
68	long uregs[18];
69};
70
71#define LINUX_REG_R0	0
72#define LINUX_REG_R1	1
73#define LINUX_REG_R2	2
74#define LINUX_REG_R3	3
75#define LINUX_REG_R4	4
76#define LINUX_REG_R5	5
77#define LINUX_REG_R6	6
78#define LINUX_REG_R7	7
79#define LINUX_REG_R8	8
80#define LINUX_REG_R9	9
81#define LINUX_REG_R10	10
82#define LINUX_REG_FP	11
83#define LINUX_REG_IP	12
84#define LINUX_REG_SP	13
85#define LINUX_REG_LR	14
86#define LINUX_REG_PC	15
87#define LINUX_REG_CPSR	16
88#define LINUX_REG_ORIG_R0 17
89
90int linux_ptrace_disabled = 1;	/* bitrotted */
91
92int
93linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap,
94    register_t *retval)
95{
96	/* {
97		syscallarg(int) request;
98		syscallarg(int) pid;
99		syscallarg(int) addr;
100		syscallarg(int) data;
101	} */
102	struct proc *p = l->l_proc, *t;
103	struct lwp *lt;
104#ifdef _ARM_ARCH_6
105	struct pcb *pcb;
106	void *val;
107#endif
108	struct reg *regs = NULL;
109	struct linux_reg *linux_regs = NULL;
110	int request, error;
111
112	if (linux_ptrace_disabled)
113		return ENOSYS;
114
115	error = 0;
116	request = SCARG(uap, request);
117	regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
118	linux_regs = kmem_alloc(sizeof(struct linux_reg), KM_SLEEP);
119
120	switch (request) {
121	case LINUX_PTRACE_GETREGS:
122		break;
123	case LINUX_PTRACE_SETREGS:
124		error = copyin((void *)SCARG(uap, data), linux_regs,
125		    sizeof(struct linux_reg));
126		if (error) {
127			goto out;
128		}
129		break;
130	default:
131		error = EIO;
132		goto out;
133	}
134
135	/* Find the process we are supposed to be operating on. */
136	mutex_enter(&proc_lock);
137	if ((t = proc_find(SCARG(uap, pid))) == NULL) {
138		mutex_exit(&proc_lock);
139		error = ESRCH;
140		goto out;
141	}
142	mutex_enter(t->p_lock);
143
144	/*
145	 * You cannot do what you want to the process if:
146	 * 1. It is not being traced at all,
147	 */
148	if (!ISSET(t->p_slflag, PSL_TRACED)) {
149		mutex_exit(t->p_lock);
150		mutex_exit(&proc_lock);
151		error = EPERM;
152		goto out;
153	}
154	/*
155	 * 2. It is being traced by procfs (which has different signal
156	 *    delivery semantics),
157	 * 3. It is not being traced by _you_, or
158	 * 4. It is not currently stopped.
159	 */
160	if (t->p_pptr != p || t->p_stat != SSTOP || !t->p_waited) {
161		mutex_exit(t->p_lock);
162		mutex_exit(&proc_lock);
163		error = EBUSY;
164		goto out;
165	}
166	mutex_exit(&proc_lock);
167	/* XXX: ptrace needs revamp for multi-threading support. */
168	if (t->p_nlwps > 1) {
169		mutex_exit(t->p_lock);
170		error = ENOSYS;
171		goto out;
172	}
173	lt = LIST_FIRST(&t->p_lwps);
174	*retval = 0;
175
176	switch (request) {
177	case LINUX_PTRACE_GETREGS:
178		error = process_read_regs(lt, regs);
179		mutex_exit(t->p_lock);
180		if (error) {
181			break;
182		}
183		memset(linux_regs, 0, sizeof(*linux_regs));
184		memcpy(linux_regs->uregs, regs->r, 13 * sizeof(register_t));
185		linux_regs->uregs[LINUX_REG_SP] = regs->r_sp;
186		linux_regs->uregs[LINUX_REG_LR] = regs->r_lr;
187		linux_regs->uregs[LINUX_REG_PC] = regs->r_pc;
188		linux_regs->uregs[LINUX_REG_CPSR] = regs->r_cpsr;
189		linux_regs->uregs[LINUX_REG_ORIG_R0] = regs->r[0];
190
191		error = copyout(linux_regs, (void *)SCARG(uap, data),
192		    sizeof(struct linux_reg));
193		break;
194
195	case LINUX_PTRACE_SETREGS:
196		memcpy(regs->r, linux_regs->uregs, 13 * sizeof(register_t));
197		regs->r_sp = linux_regs->uregs[LINUX_REG_SP];
198		regs->r_lr = linux_regs->uregs[LINUX_REG_LR];
199		regs->r_pc = linux_regs->uregs[LINUX_REG_PC];
200		regs->r_cpsr = linux_regs->uregs[LINUX_REG_CPSR];
201		error = process_write_regs(lt, regs);
202		mutex_exit(t->p_lock);
203		break;
204
205#ifdef _ARM_ARCH_6
206#define LINUX_PTRACE_GET_THREAD_AREA	22
207	case LINUX_PTRACE_GET_THREAD_AREA:
208		mutex_exit(t->p_lock);
209		pcb = lwp_getpcb(l);
210		val = (void *)pcb->pcb_user_pid_ro;
211		error = copyout(&val, (void *)SCARG(uap, data), sizeof(val));
212		break;
213#endif
214
215	default:
216		mutex_exit(t->p_lock);
217	}
218out:
219	if (regs)
220		kmem_free(regs, sizeof(*regs));
221	if (linux_regs)
222		kmem_free(linux_regs, sizeof(*linux_regs));
223	return error;
224
225}
226