aboutsummaryrefslogtreecommitdiff
path: root/arch/frv/kernel/ptrace.c
blob: 60eeed3694c0764d2d0b9d7d3a0c2cffe14b9a89 (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
/* ptrace.c: FRV specific parts of process tracing
 *
 * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 * - Derived from arch/m68k/kernel/ptrace.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

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

#include <asm/uaccess.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <asm/system.h>
#include <asm/processor.h>
#include <asm/unistd.h>

/*
 * does not yet catch signals sent when the child dies.
 * in exit.c or in signal.c.
 */

/*
 * retrieve the contents of FRV userspace general registers
 */
static int genregs_get(struct task_struct *target,
		       const struct user_regset *regset,
		       unsigned int pos, unsigned int count,
		       void *kbuf, void __user *ubuf)
{
	const struct user_int_regs *iregs = &target->thread.user->i;
	int ret;

	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
				  iregs, 0, sizeof(*iregs));
	if (ret < 0)
		return ret;

	return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
					sizeof(*iregs), -1);
}

/*
 * update the contents of the FRV userspace general registers
 */
static int genregs_set(struct task_struct *target,
		       const struct user_regset *regset,
		       unsigned int pos, unsigned int count,
		       const void *kbuf, const void __user *ubuf)
{
	struct user_int_regs *iregs = &target->thread.user->i;
	unsigned int offs_gr0, offs_gr1;
	int ret;

	/* not allowed to set PSR or __status */
	if (pos < offsetof(struct user_int_regs, psr) + sizeof(long) &&
	    pos + count > offsetof(struct user_int_regs, psr))
		return -EIO;

	if (pos < offsetof(struct user_int_regs, __status) + sizeof(long) &&
	    pos + count > offsetof(struct user_int_regs, __status))
		return -EIO;

	/* set the control regs */
	offs_gr0 = offsetof(struct user_int_regs, gr[0]);
	offs_gr1 = offsetof(struct user_int_regs, gr[1]);
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 iregs, 0, offs_gr0);
	if (ret < 0)
		return ret;

	/* skip GR0/TBR */
	ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
					offs_gr0, offs_gr1);
	if (ret < 0)
		return ret;

	/* set the general regs */
	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 &iregs->gr[1], offs_gr1, sizeof(*iregs));
	if (ret < 0)
		return ret;

	return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
					sizeof(*iregs), -1);
}

/*
 * retrieve the contents of FRV userspace FP/Media registers
 */
static int fpmregs_get(struct task_struct *target,
		       const struct user_regset *regset,
		       unsigned int pos, unsigned int count,
		       void *kbuf, void __user *ubuf)
{
	const struct user_fpmedia_regs *fpregs = &target->thread.user->f;
	int ret;

	ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
				  fpregs, 0, sizeof(*fpregs));
	if (ret < 0)
		return ret;

	return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
					sizeof(*fpregs), -1);
}

/*
 * update the contents of the FRV userspace FP/Media registers
 */
static int fpmregs_set(struct task_struct *target,
		       const struct user_regset *regset,
		       unsigned int pos, unsigned int count,
		       const void *kbuf, const void __user *ubuf)
{
	struct user_fpmedia_regs *fpregs = &target->thread.user->f;
	int ret;

	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
				 fpregs, 0, sizeof(*fpregs));
	if (ret < 0)
		return ret;

	return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
					sizeof(*fpregs), -1);
}

/*
 * determine if the FP/Media registers have actually been used
 */
static int fpmregs_active(struct task_struct *target,
			  const struct user_regset *regset)
{
	return tsk_used_math(target) ? regset->n : 0;
}

/*
 * Define the register sets available on the FRV under Linux
 */
enum frv_regset {
	REGSET_GENERAL,
	REGSET_FPMEDIA,
};

static const struct user_regset frv_regsets[] = {
	/*
	 * General register format is:
	 *	PSR, ISR, CCR, CCCR, LR, LCR, PC, (STATUS), SYSCALLNO, ORIG_G8
	 *	GNER0-1, IACC0, TBR, GR1-63
	 */
	[REGSET_GENERAL] = {
		.core_note_type	= NT_PRSTATUS,
		.n		= ELF_NGREG,
		.size		= sizeof(long),
		.align		= sizeof(long),
		.get		= genregs_get,
		.set		= genregs_set,
	},
	/*
	 * FPU/Media register format is:
	 *	FR0-63, FNER0-1, MSR0-1, ACC0-7, ACCG0-8, FSR
	 */
	[REGSET_FPMEDIA] = {
		.core_note_type	= NT_PRFPREG,
		.n		= sizeof(struct user_fpmedia_regs) / sizeof(long),
		.size		= sizeof(long),
		.align		= sizeof(long),
		.get		= fpmregs_get,
		.set		= fpmregs_set,
		.active		= fpmregs_active,
	},
};

static const struct user_regset_view user_frv_native_view = {
	.name		= "frv",
	.e_machine	= EM_FRV,
	.regsets	= frv_regsets,
	.n		= ARRAY_SIZE(frv_regsets),
};

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

/*
 * Get contents of register REGNO in task TASK.
 */
static inline long get_reg(struct task_struct *task, int regno)
{
	struct user_context *user = task->thread.user;

	if (regno < 0 || regno >= PT__END)
		return 0;

	return ((unsigned long *) user)[regno];
}

/*
 * Write contents of register REGNO in task TASK.
 */
static inline int put_reg(struct task_struct *task, int regno,
			  unsigned long data)
{
	struct user_context *user = task->thread.user;

	if (regno < 0 || regno >= PT__END)
		return -EIO;

	switch (regno) {
	case PT_GR(0):
		return 0;
	case PT_PSR:
	case PT__STATUS:
		return -EIO;
	default:
		((unsigned long *) user)[regno] = data;
		return 0;
	}
}

/*
 * Called by kernel/ptrace.c when detaching..
 *
 * Control h/w single stepping
 */
void user_enable_single_step(struct task_struct *child)
{
	child->thread.frame0->__status |= REG__STATUS_STEP;
}

void user_disable_single_step(struct task_struct *child)
{
	child->thread.frame0->__status &= ~REG__STATUS_STEP;
}

void ptrace_disable(struct task_struct *child)
{
	user_disable_single_step(child);
}

long arch_ptrace(struct task_struct *child, long request, long addr, long data)
{
	unsigned long tmp;
	int ret;

	switch (request) {
		/* read the word at location addr in the USER area. */
	case PTRACE_PEEKUSR: {
		tmp = 0;
		ret = -EIO;
		if ((addr & 3) || addr < 0)
			break;

		ret = 0;
		switch (addr >> 2) {
		case 0 ... PT__END - 1:
			tmp = get_reg(child, addr >> 2);
			break;

		case PT__END + 0:
			tmp = child->mm->end_code - child->mm->start_code;
			break;

		case PT__END + 1:
			tmp = child->mm->end_data - child->mm->start_data;
			break;

		case PT__END + 2:
			tmp = child->mm->start_stack - child->mm->start_brk;
			break;

		case PT__END + 3:
			tmp = child->mm->start_code;
			break;

		case PT__END + 4:
			tmp = child->mm->start_stack;
			break;

		default:
			ret = -EIO;
			break;
		}

		if (ret == 0)
			ret = put_user(tmp, (unsigned long *) data);
		break;
	}

	case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
		ret = -EIO;
		if ((addr & 3) || addr < 0)
			break;

		ret = 0;
		switch (addr >> 2) {
		case 0 ... PT__END - 1:
			ret = put_reg(child, addr >> 2, data);
			break;

		default:
			ret = -EIO;
			break;
		}
		break;

	case PTRACE_GETREGS:	/* Get all integer regs from the child. */
		return copy_regset_to_user(child, &user_frv_native_view,
					   REGSET_GENERAL,
					   0, sizeof(child->thread.user->i),
					   (void __user *)data);

	case PTRACE_SETREGS:	/* Set all integer regs in the child. */
		return copy_regset_from_user(child, &user_frv_native_view,
					     REGSET_GENERAL,
					     0, sizeof(child->thread.user->i),
					     (const void __user *)data);

	case PTRACE_GETFPREGS:	/* Get the child FP/Media state. */
		return copy_regset_to_user(child, &user_frv_native_view,
					   REGSET_FPMEDIA,
					   0, sizeof(child->thread.user->f),
					   (void __user *)data);

	case PTRACE_SETFPREGS:	/* Set the child FP/Media state. */
		return copy_regset_from_user(child, &user_frv_native_view,
					     REGSET_FPMEDIA,
					     0, sizeof(child->thread.user->f),
					     (const void __user *)data);

	case PTRACE_GETFDPIC:
		tmp = 0;
		switch (addr) {
		case PTRACE_GETFDPIC_EXEC:
			tmp = child->mm->context.exec_fdpic_loadmap;
			break;
		case PTRACE_GETFDPIC_INTERP:
			tmp = child->mm->context.interp_fdpic_loadmap;
			break;
		default:
			break;
		}

		ret = 0;
		if (put_user(tmp, (unsigned long *) data)) {
			ret = -EFAULT;
			break;
		}
		break;

	default:
		ret = ptrace_request(child, request, addr, data);
		break;
	}
	return ret;
}

/*
 * handle tracing of system call entry
 * - return the revised system call number or ULONG_MAX to cause ENOSYS
 */
asmlinkage unsigned long syscall_trace_entry(void)
{
	__frame->__status |= REG__STATUS_SYSC_ENTRY;
	if (tracehook_report_syscall_entry(__frame)) {
		/* tracing decided this syscall should not happen, so
		 * We'll return a bogus call number to get an ENOSYS
		 * error, but leave the original number in
		 * __frame->syscallno
		 */
		return ULONG_MAX;
	}

	return __frame->syscallno;
}

/*
 * handle tracing of system call exit
 */
asmlinkage void syscall_trace_exit(void)
{
	__frame->__status |= REG__STATUS_SYSC_EXIT;
	tracehook_report_syscall_exit(__frame, 0);
}