summaryrefslogtreecommitdiff
path: root/sys/compat/linux/arch/arm/linux_ptrace.c
blob: dbc43e2c0b78086e09a0b24818958d92af382b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*	$NetBSD: linux_ptrace.c,v 1.21.8.1 2022/08/03 11:11:33 martin Exp $	*/

/*-
 * Copyright (c) 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Matthias Scheler.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.21.8.1 2022/08/03 11:11:33 martin Exp $");

#include <sys/param.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/ptrace.h>
#include <sys/systm.h>
#include <sys/syscallargs.h>
#include <uvm/uvm_extern.h>

#include <machine/reg.h>
#include <machine/pcb.h>

#include <compat/linux/common/linux_types.h>
#include <compat/linux/common/linux_ptrace.h>
#include <compat/linux/common/linux_signal.h>

#include <compat/linux/common/linux_util.h>
#include <compat/linux/common/linux_machdep.h>
#include <compat/linux/common/linux_emuldata.h>
#include <compat/linux/common/linux_exec.h>	/* for emul_linux */

#include <compat/linux/linux_syscallargs.h>

#include <lib/libkern/libkern.h>	/* for offsetof() */

/*
 * On ARMv2, uregs contains R0--R15, orig_R0.
 * On ARMv3 and later, it's R0--R15, CPSR, orig_R0.
 * As far as I can see, Linux doesn't initialise orig_R0 on ARMv2, so we
 * just produce the ARMv3 version.
 */

struct linux_reg {
	long uregs[18];
};

#define LINUX_REG_R0	0
#define LINUX_REG_R1	1
#define LINUX_REG_R2	2
#define LINUX_REG_R3	3
#define LINUX_REG_R4	4
#define LINUX_REG_R5	5
#define LINUX_REG_R6	6
#define LINUX_REG_R7	7
#define LINUX_REG_R8	8
#define LINUX_REG_R9	9
#define LINUX_REG_R10	10
#define LINUX_REG_FP	11
#define LINUX_REG_IP	12
#define LINUX_REG_SP	13
#define LINUX_REG_LR	14
#define LINUX_REG_PC	15
#define LINUX_REG_CPSR	16
#define LINUX_REG_ORIG_R0 17

int linux_ptrace_disabled = 1;	/* bitrotted */

int
linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap,
    register_t *retval)
{
	/* {
		syscallarg(int) request;
		syscallarg(int) pid;
		syscallarg(int) addr;
		syscallarg(int) data;
	} */
	struct proc *p = l->l_proc, *t;
	struct lwp *lt;
#ifdef _ARM_ARCH_6
	struct pcb *pcb;
	void *val;
#endif
	struct reg *regs = NULL;
	struct linux_reg *linux_regs = NULL;
	int request, error;

	if (linux_ptrace_disabled)
		return ENOSYS;

	error = 0;
	request = SCARG(uap, request);
	regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
	linux_regs = kmem_alloc(sizeof(struct linux_reg), KM_SLEEP);

	switch (request) {
	case LINUX_PTRACE_GETREGS:
		break;
	case LINUX_PTRACE_SETREGS:
		error = copyin((void *)SCARG(uap, data), linux_regs,
		    sizeof(struct linux_reg));
		if (error) {
			goto out;
		}
		break;
	default:
		error = EIO;
		goto out;
	}

	/* Find the process we are supposed to be operating on. */
	mutex_enter(proc_lock);
	if ((t = proc_find(SCARG(uap, pid))) == NULL) {
		mutex_exit(proc_lock);
		error = ESRCH;
		goto out;
	}
	mutex_enter(t->p_lock);

	/*
	 * You cannot do what you want to the process if:
	 * 1. It is not being traced at all,
	 */
	if (!ISSET(t->p_slflag, PSL_TRACED)) {
		mutex_exit(t->p_lock);
		mutex_exit(proc_lock);
		error = EPERM;
		goto out;
	}
	/*
	 * 2. It is being traced by procfs (which has different signal
	 *    delivery semantics),
	 * 3. It is not being traced by _you_, or
	 * 4. It is not currently stopped.
	 */
	if (t->p_pptr != p || t->p_stat != SSTOP || !t->p_waited) {
		mutex_exit(t->p_lock);
		mutex_exit(proc_lock);
		error = EBUSY;
		goto out;
	}
	mutex_exit(proc_lock);
	/* XXX: ptrace needs revamp for multi-threading support. */
	if (t->p_nlwps > 1) {
		mutex_exit(t->p_lock);
		error = ENOSYS;
		goto out;
	}
	lt = LIST_FIRST(&t->p_lwps);
	*retval = 0;

	switch (request) {
	case LINUX_PTRACE_GETREGS:
		error = process_read_regs(lt, regs);
		mutex_exit(t->p_lock);
		if (error) {
			break;
		}
		memset(linux_regs, 0, sizeof(*linux_regs));
		memcpy(linux_regs->uregs, regs->r, 13 * sizeof(register_t));
		linux_regs->uregs[LINUX_REG_SP] = regs->r_sp;
		linux_regs->uregs[LINUX_REG_LR] = regs->r_lr;
		linux_regs->uregs[LINUX_REG_PC] = regs->r_pc;
		linux_regs->uregs[LINUX_REG_CPSR] = regs->r_cpsr;
		linux_regs->uregs[LINUX_REG_ORIG_R0] = regs->r[0];

		error = copyout(linux_regs, (void *)SCARG(uap, data),
		    sizeof(struct linux_reg));
		break;

	case LINUX_PTRACE_SETREGS:
		memcpy(regs->r, linux_regs->uregs, 13 * sizeof(register_t));
		regs->r_sp = linux_regs->uregs[LINUX_REG_SP];
		regs->r_lr = linux_regs->uregs[LINUX_REG_LR];
		regs->r_pc = linux_regs->uregs[LINUX_REG_PC];
		regs->r_cpsr = linux_regs->uregs[LINUX_REG_CPSR];
		error = process_write_regs(lt, regs);
		mutex_exit(t->p_lock);
		break;

#ifdef _ARM_ARCH_6
#define LINUX_PTRACE_GET_THREAD_AREA	22
	case LINUX_PTRACE_GET_THREAD_AREA:
		mutex_exit(t->p_lock);
		pcb = lwp_getpcb(l);
		val = (void *)pcb->pcb_user_pid_ro;
		error = copyout(&val, (void *)SCARG(uap, data), sizeof(val));
		break;
#endif

	default:
		mutex_exit(t->p_lock);
	}
out:
	if (regs)
		kmem_free(regs, sizeof(*regs));
	if (linux_regs)
		kmem_free(linux_regs, sizeof(*linux_regs));
	return error;

}