summaryrefslogtreecommitdiffstats
path: root/arch/sparc/kernel/ptrace_32.c
blob: e7db48acb838b45e98c9a86f18d38b69b9c65947 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// SPDX-License-Identifier: GPL-2.0
/* ptrace.c: Sparc process tracing support.
 *
 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
 *
 * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
 * and David Mosberger.
 *
 * Added Linux support -miguel (weird, eh?, the original code was meant
 * to emulate SunOS).
 */

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/ptrace.h>
#include <linux/user.h>
#include <linux/smp.h>
#include <linux/security.h>
#include <linux/signal.h>
#include <linux/regset.h>
#include <linux/elf.h>

#include <linux/uaccess.h>
#include <asm/cacheflush.h>

#include "kernel.h"

/* #define ALLOW_INIT_TRACING */

/*
 * Called by kernel/ptrace.c when detaching..
 *
 * Make sure single step bits etc are not set.
 */
void ptrace_disable(struct task_struct *child)
{
	/* nothing to do */
}

enum sparc_regset {
	REGSET_GENERAL,
	REGSET_FP,
};

static int regwindow32_get(struct task_struct *target,
			   const struct pt_regs *regs,
			   u32 *uregs)
{
	unsigned long reg_window = regs->u_regs[UREG_I6];
	int size = 16 * sizeof(u32);

	if (target == current) {
		if (copy_from_user(uregs, (void __user *)reg_window, size))
			return -EFAULT;
	} else {
		if (access_process_vm(target, reg_window, uregs, size,
				      FOLL_FORCE) != size)
			return -EFAULT;
	}
	return 0;
}

static int regwindow32_set(struct task_struct *target,
			   const struct pt_regs *regs,
			   u32 *uregs)
{
	unsigned long reg_window = regs->u_regs[UREG_I6];
	int size = 16 * sizeof(u32);

	if (target == current) {
		if (copy_to_user((void __user *)reg_window, uregs, size))
			return -EFAULT;
	} else {
		if (access_process_vm(target, reg_window, uregs, size,
				      FOLL_FORCE | FOLL_WRITE) != size)
			return -EFAULT;
	}
	return 0;
}

static int genregs32_get(struct task_struct *target,
			 const struct user_regset *regset,
			 struct membuf to)
{
	const struct pt_regs *regs = target->thread.kregs;
	u32 uregs[16];

	if (target == current)
		flush_user_windows();

	membuf_write(&to, regs->u_regs, 16 * sizeof(u32));
	if (!to.left)
		return 0;
	if (regwindow32_get(target, regs, uregs))
		return -EFAULT;
	membuf_write(&to, uregs, 16 * sizeof(u32));
	membuf_store(&to, regs->psr);
	membuf_store(&to, regs->pc);
	membuf_store(&to, regs->npc);
	membuf_store(&to, regs->y);
	return membuf_zero(&to, 2 * sizeof(u32));
}

static int genregs32_set(struct task_struct *target,
			 const struct user_regset *regset,
			 unsigned int pos, unsigned int count,
			 const void *kbuf, const void __user *ubuf)
{
	struct pt_regs *regs = target->thread.kregs;
	u32 uregs[16];
	u32 psr;
	int ret;

	if (target == current)
		flush_user_windows();

	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 regs->u_regs,
				 0, 16 * sizeof(u32));
	if (ret || !count)
		return ret;

	if (regwindow32_get(target, regs, uregs))
		return -EFAULT;
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 uregs,
				 16 * sizeof(u32), 32 * sizeof(u32));
	if (ret)
		return ret;
	if (regwindow32_set(target, regs, uregs))
		return -EFAULT;
	if (!count)
		return 0;

	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 &psr,
				 32 * sizeof(u32), 33 * sizeof(u32));
	if (ret)
		return ret;
	regs->psr = (regs->psr & ~(PSR_ICC | PSR_SYSCALL)) |
		    (psr & (PSR_ICC | PSR_SYSCALL));
	if (!count)
		return 0;
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 &regs->pc,
				 33 * sizeof(u32), 34 * sizeof(u32));
	if (ret || !count)
		return ret;
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 &regs->npc,
				 34 * sizeof(u32), 35 * sizeof(u32));
	if (ret || !count)
		return ret;
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 &regs->y,
				 35 * sizeof(u32), 36 * sizeof(u32));
	if (ret || !count)
		return ret;
	return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
					 36 * sizeof(u32), 38 * sizeof(u32));
}

static int fpregs32_get(struct task_struct *target,
			const struct user_regset *regset,
			struct membuf to)
{
#if 0
	if (target == current)
		save_and_clear_fpu();
#endif

	membuf_write(&to, target->thread.float_regs, 32 * sizeof(u32));
	membuf_zero(&to, sizeof(u32));
	membuf_write(&to, &target->thread.fsr, sizeof(u32));
	membuf_store(&to, (u32)((1 << 8) | (8 << 16)));
	return membuf_zero(&to, 64 * sizeof(u32));
}

static int fpregs32_set(struct task_struct *target,
			const struct user_regset *regset,
			unsigned int pos, unsigned int count,
			const void *kbuf, const void __user *ubuf)
{
	unsigned long *fpregs = target->thread.float_regs;
	int ret;

#if 0
	if (target == current)
		save_and_clear_fpu();
#endif
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 fpregs,
				 0, 32 * sizeof(u32));
	if (!ret)
		user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
					  32 * sizeof(u32),
					  33 * sizeof(u32));
	if (!ret)
		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
					 &target->thread.fsr,
					 33 * sizeof(u32),
					 34 * sizeof(u32));
	if (!ret)
		ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
						34 * sizeof(u32), -1);
	return ret;
}

static const struct user_regset sparc32_regsets[] = {
	/* Format is:
	 * 	G0 --> G7
	 *	O0 --> O7
	 *	L0 --> L7
	 *	I0 --> I7
	 *	PSR, PC, nPC, Y, WIM, TBR
	 */
	[REGSET_GENERAL] = {
		.core_note_type = NT_PRSTATUS,
		.n = 38,
		.size = sizeof(u32), .align = sizeof(u32),
		.regset_get = genregs32_get, .set = genregs32_set
	},
	/* Format is:
	 *	F0 --> F31
	 *	empty 32-bit word
	 *	FSR (32--bit word)
	 *	FPU QUEUE COUNT (8-bit char)
	 *	FPU QUEUE ENTRYSIZE (8-bit char)
	 *	FPU ENABLED (8-bit char)
	 *	empty 8-bit char
	 *	FPU QUEUE (64 32-bit ints)
	 */
	[REGSET_FP] = {
		.core_note_type = NT_PRFPREG,
		.n = 99,
		.size = sizeof(u32), .align = sizeof(u32),
		.regset_get = fpregs32_get, .set = fpregs32_set
	},
};

static int getregs_get(struct task_struct *target,
			 const struct user_regset *regset,
			 struct membuf to)
{
	const struct pt_regs *regs = target->thread.kregs;

	if (target == current)
		flush_user_windows();

	membuf_store(&to, regs->psr);
	membuf_store(&to, regs->pc);
	membuf_store(&to, regs->npc);
	membuf_store(&to, regs->y);
	return membuf_write(&to, regs->u_regs + 1, 15 * sizeof(u32));
}

static int setregs_set(struct task_struct *target,
			 const struct user_regset *regset,
			 unsigned int pos, unsigned int count,
			 const void *kbuf, const void __user *ubuf)
{
	struct pt_regs *regs = target->thread.kregs;
	u32 v[4];
	int ret;

	if (target == current)
		flush_user_windows();

	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 v,
				 0, 4 * sizeof(u32));
	if (ret)
		return ret;
	regs->psr = (regs->psr & ~(PSR_ICC | PSR_SYSCALL)) |
		    (v[0] & (PSR_ICC | PSR_SYSCALL));
	regs->pc = v[1];
	regs->npc = v[2];
	regs->y = v[3];
	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 regs->u_regs + 1,
				 4 * sizeof(u32) , 19 * sizeof(u32));
}

static int getfpregs_get(struct task_struct *target,
			const struct user_regset *regset,
			struct membuf to)
{
#if 0
	if (target == current)
		save_and_clear_fpu();
#endif
	membuf_write(&to, &target->thread.float_regs, 32 * sizeof(u32));
	membuf_write(&to, &target->thread.fsr, sizeof(u32));
	return membuf_zero(&to, 35 * sizeof(u32));
}

static int setfpregs_set(struct task_struct *target,
			const struct user_regset *regset,
			unsigned int pos, unsigned int count,
			const void *kbuf, const void __user *ubuf)
{
	unsigned long *fpregs = target->thread.float_regs;
	int ret;

#if 0
	if (target == current)
		save_and_clear_fpu();
#endif
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 fpregs,
				 0, 32 * sizeof(u32));
	if (ret)
		return ret;
	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 &target->thread.fsr,
				 32 * sizeof(u32),
				 33 * sizeof(u32));
}

static const struct user_regset ptrace32_regsets[] = {
	[REGSET_GENERAL] = {
		.n = 19, .size = sizeof(u32),
		.regset_get = getregs_get, .set = setregs_set,
	},
	[REGSET_FP] = {
		.n = 68, .size = sizeof(u32),
		.regset_get = getfpregs_get, .set = setfpregs_set,
	},
};

static const struct user_regset_view ptrace32_view = {
	.regsets = ptrace32_regsets, .n = ARRAY_SIZE(ptrace32_regsets)
};

static const struct user_regset_view user_sparc32_view = {
	.name = "sparc", .e_machine = EM_SPARC,
	.regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
};

const struct user_regset_view *task_user_regset_view(struct task_struct *task)
{
	return &user_sparc32_view;
}

struct fps {
	unsigned long regs[32];
	unsigned long fsr;
	unsigned long flags;
	unsigned long extra;
	unsigned long fpqd;
	struct fq {
		unsigned long *insnaddr;
		unsigned long insn;
	} fpq[16];
};

long arch_ptrace(struct task_struct *child, long request,
		 unsigned long addr, unsigned long data)
{
	unsigned long addr2 = current->thread.kregs->u_regs[UREG_I4];
	void __user *addr2p;
	struct pt_regs __user *pregs;
	struct fps __user *fps;
	int ret;

	addr2p = (void __user *) addr2;
	pregs = (struct pt_regs __user *) addr;
	fps = (struct fps __user *) addr;

	switch(request) {
	case PTRACE_GETREGS: {
		ret = copy_regset_to_user(child, &ptrace32_view,
					  REGSET_GENERAL, 0,
					  19 * sizeof(u32),
					  pregs);
		break;
	}

	case PTRACE_SETREGS: {
		ret = copy_regset_from_user(child, &ptrace32_view,
					    REGSET_GENERAL, 0,
					    19 * sizeof(u32),
					    pregs);
		break;
	}

	case PTRACE_GETFPREGS: {
		ret = copy_regset_to_user(child, &ptrace32_view,
					  REGSET_FP, 0,
					  68 * sizeof(u32),
					  fps);
		break;
	}

	case PTRACE_SETFPREGS: {
		ret = copy_regset_from_user(child, &ptrace32_view,
					  REGSET_FP, 0,
					  33 * sizeof(u32),
					  fps);
		break;
	}

	case PTRACE_READTEXT:
	case PTRACE_READDATA:
		ret = ptrace_readdata(child, addr, addr2p, data);

		if (ret == data)
			ret = 0;
		else if (ret >= 0)
			ret = -EIO;
		break;

	case PTRACE_WRITETEXT:
	case PTRACE_WRITEDATA:
		ret = ptrace_writedata(child, addr2p, addr, data);

		if (ret == data)
			ret = 0;
		else if (ret >= 0)
			ret = -EIO;
		break;

	default:
		if (request == PTRACE_SPARC_DETACH)
			request = PTRACE_DETACH;
		ret = ptrace_request(child, request, addr, data);
		break;
	}

	return ret;
}

asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
{
	int ret = 0;

	if (test_thread_flag(TIF_SYSCALL_TRACE)) {
		if (syscall_exit_p)
			ptrace_report_syscall_exit(regs, 0);
		else
			ret = ptrace_report_syscall_entry(regs);
	}

	return ret;
}