blob: 49768401ce0fe2c15c4b72cbb8a37f4668168bd1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ptrace.c: FRV specific parts of process tracing
2 *
3 * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from arch/m68k/kernel/ptrace.c
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ptrace.h>
19#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070021#include <linux/signal.h>
David Howells4a3b9892009-06-11 13:05:24 +010022#include <linux/regset.h>
23#include <linux/elf.h>
24#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/page.h>
28#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/processor.h>
30#include <asm/unistd.h>
31
32/*
33 * does not yet catch signals sent when the child dies.
34 * in exit.c or in signal.c.
35 */
36
37/*
David Howells4a3b9892009-06-11 13:05:24 +010038 * retrieve the contents of FRV userspace general registers
39 */
40static int genregs_get(struct task_struct *target,
41 const struct user_regset *regset,
42 unsigned int pos, unsigned int count,
43 void *kbuf, void __user *ubuf)
44{
45 const struct user_int_regs *iregs = &target->thread.user->i;
46 int ret;
47
48 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
49 iregs, 0, sizeof(*iregs));
50 if (ret < 0)
51 return ret;
52
53 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
54 sizeof(*iregs), -1);
55}
56
57/*
58 * update the contents of the FRV userspace general registers
59 */
60static int genregs_set(struct task_struct *target,
61 const struct user_regset *regset,
62 unsigned int pos, unsigned int count,
63 const void *kbuf, const void __user *ubuf)
64{
65 struct user_int_regs *iregs = &target->thread.user->i;
66 unsigned int offs_gr0, offs_gr1;
67 int ret;
68
69 /* not allowed to set PSR or __status */
70 if (pos < offsetof(struct user_int_regs, psr) + sizeof(long) &&
71 pos + count > offsetof(struct user_int_regs, psr))
72 return -EIO;
73
74 if (pos < offsetof(struct user_int_regs, __status) + sizeof(long) &&
75 pos + count > offsetof(struct user_int_regs, __status))
76 return -EIO;
77
78 /* set the control regs */
79 offs_gr0 = offsetof(struct user_int_regs, gr[0]);
80 offs_gr1 = offsetof(struct user_int_regs, gr[1]);
81 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
82 iregs, 0, offs_gr0);
83 if (ret < 0)
84 return ret;
85
86 /* skip GR0/TBR */
87 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
88 offs_gr0, offs_gr1);
89 if (ret < 0)
90 return ret;
91
92 /* set the general regs */
93 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
94 &iregs->gr[1], offs_gr1, sizeof(*iregs));
95 if (ret < 0)
96 return ret;
97
98 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
99 sizeof(*iregs), -1);
100}
101
102/*
103 * retrieve the contents of FRV userspace FP/Media registers
104 */
105static int fpmregs_get(struct task_struct *target,
106 const struct user_regset *regset,
107 unsigned int pos, unsigned int count,
108 void *kbuf, void __user *ubuf)
109{
110 const struct user_fpmedia_regs *fpregs = &target->thread.user->f;
111 int ret;
112
113 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
114 fpregs, 0, sizeof(*fpregs));
115 if (ret < 0)
116 return ret;
117
118 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
119 sizeof(*fpregs), -1);
120}
121
122/*
123 * update the contents of the FRV userspace FP/Media registers
124 */
125static int fpmregs_set(struct task_struct *target,
126 const struct user_regset *regset,
127 unsigned int pos, unsigned int count,
128 const void *kbuf, const void __user *ubuf)
129{
130 struct user_fpmedia_regs *fpregs = &target->thread.user->f;
131 int ret;
132
133 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
134 fpregs, 0, sizeof(*fpregs));
135 if (ret < 0)
136 return ret;
137
138 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
139 sizeof(*fpregs), -1);
140}
141
142/*
143 * determine if the FP/Media registers have actually been used
144 */
145static int fpmregs_active(struct task_struct *target,
146 const struct user_regset *regset)
147{
148 return tsk_used_math(target) ? regset->n : 0;
149}
150
151/*
152 * Define the register sets available on the FRV under Linux
153 */
154enum frv_regset {
155 REGSET_GENERAL,
156 REGSET_FPMEDIA,
157};
158
159static const struct user_regset frv_regsets[] = {
160 /*
161 * General register format is:
162 * PSR, ISR, CCR, CCCR, LR, LCR, PC, (STATUS), SYSCALLNO, ORIG_G8
163 * GNER0-1, IACC0, TBR, GR1-63
164 */
165 [REGSET_GENERAL] = {
166 .core_note_type = NT_PRSTATUS,
167 .n = ELF_NGREG,
168 .size = sizeof(long),
169 .align = sizeof(long),
170 .get = genregs_get,
171 .set = genregs_set,
172 },
173 /*
174 * FPU/Media register format is:
175 * FR0-63, FNER0-1, MSR0-1, ACC0-7, ACCG0-8, FSR
176 */
177 [REGSET_FPMEDIA] = {
178 .core_note_type = NT_PRFPREG,
179 .n = sizeof(struct user_fpmedia_regs) / sizeof(long),
180 .size = sizeof(long),
181 .align = sizeof(long),
182 .get = fpmregs_get,
183 .set = fpmregs_set,
184 .active = fpmregs_active,
185 },
186};
187
188static const struct user_regset_view user_frv_native_view = {
189 .name = "frv",
190 .e_machine = EM_FRV,
191 .regsets = frv_regsets,
192 .n = ARRAY_SIZE(frv_regsets),
193};
194
195const struct user_regset_view *task_user_regset_view(struct task_struct *task)
196{
197 return &user_frv_native_view;
198}
199
200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * Get contents of register REGNO in task TASK.
202 */
203static inline long get_reg(struct task_struct *task, int regno)
204{
205 struct user_context *user = task->thread.user;
206
207 if (regno < 0 || regno >= PT__END)
208 return 0;
209
210 return ((unsigned long *) user)[regno];
211}
212
213/*
214 * Write contents of register REGNO in task TASK.
215 */
216static inline int put_reg(struct task_struct *task, int regno,
217 unsigned long data)
218{
219 struct user_context *user = task->thread.user;
220
221 if (regno < 0 || regno >= PT__END)
222 return -EIO;
223
224 switch (regno) {
225 case PT_GR(0):
226 return 0;
227 case PT_PSR:
228 case PT__STATUS:
229 return -EIO;
230 default:
231 ((unsigned long *) user)[regno] = data;
232 return 0;
233 }
234}
235
236/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * Called by kernel/ptrace.c when detaching..
238 *
239 * Control h/w single stepping
240 */
David Howells4a3b9892009-06-11 13:05:24 +0100241void user_enable_single_step(struct task_struct *child)
242{
243 child->thread.frame0->__status |= REG__STATUS_STEP;
244}
245
246void user_disable_single_step(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 child->thread.frame0->__status &= ~REG__STATUS_STEP;
249}
250
David Howells4a3b9892009-06-11 13:05:24 +0100251void ptrace_disable(struct task_struct *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
David Howells4a3b9892009-06-11 13:05:24 +0100253 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Namhyung Kim9b05a692010-10-27 15:33:47 -0700256long arch_ptrace(struct task_struct *child, long request,
257 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long tmp;
260 int ret;
Namhyung Kim81d32852010-10-27 15:33:52 -0700261 int regno = addr >> 2;
262 unsigned long __user *datap = (unsigned long __user *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* read the word at location addr in the USER area. */
266 case PTRACE_PEEKUSR: {
267 tmp = 0;
268 ret = -EIO;
Namhyung Kim81d32852010-10-27 15:33:52 -0700269 if (addr & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
271
272 ret = 0;
Namhyung Kim81d32852010-10-27 15:33:52 -0700273 switch (regno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 case 0 ... PT__END - 1:
Namhyung Kim81d32852010-10-27 15:33:52 -0700275 tmp = get_reg(child, regno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277
278 case PT__END + 0:
279 tmp = child->mm->end_code - child->mm->start_code;
280 break;
281
282 case PT__END + 1:
283 tmp = child->mm->end_data - child->mm->start_data;
284 break;
285
286 case PT__END + 2:
287 tmp = child->mm->start_stack - child->mm->start_brk;
288 break;
289
290 case PT__END + 3:
291 tmp = child->mm->start_code;
292 break;
293
294 case PT__END + 4:
295 tmp = child->mm->start_stack;
296 break;
297
298 default:
299 ret = -EIO;
300 break;
301 }
302
303 if (ret == 0)
Namhyung Kim81d32852010-10-27 15:33:52 -0700304 ret = put_user(tmp, datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
306 }
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
309 ret = -EIO;
Namhyung Kim81d32852010-10-27 15:33:52 -0700310 if (addr & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312
Namhyung Kim81d32852010-10-27 15:33:52 -0700313 switch (regno) {
David Howells4a3b9892009-06-11 13:05:24 +0100314 case 0 ... PT__END - 1:
Namhyung Kim81d32852010-10-27 15:33:52 -0700315 ret = put_reg(child, regno, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317 }
318 break;
319
David Howells4a3b9892009-06-11 13:05:24 +0100320 case PTRACE_GETREGS: /* Get all integer regs from the child. */
321 return copy_regset_to_user(child, &user_frv_native_view,
322 REGSET_GENERAL,
323 0, sizeof(child->thread.user->i),
Namhyung Kim81d32852010-10-27 15:33:52 -0700324 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
David Howells4a3b9892009-06-11 13:05:24 +0100326 case PTRACE_SETREGS: /* Set all integer regs in the child. */
327 return copy_regset_from_user(child, &user_frv_native_view,
328 REGSET_GENERAL,
329 0, sizeof(child->thread.user->i),
Namhyung Kim81d32852010-10-27 15:33:52 -0700330 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David Howells4a3b9892009-06-11 13:05:24 +0100332 case PTRACE_GETFPREGS: /* Get the child FP/Media state. */
333 return copy_regset_to_user(child, &user_frv_native_view,
334 REGSET_FPMEDIA,
335 0, sizeof(child->thread.user->f),
Namhyung Kim81d32852010-10-27 15:33:52 -0700336 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
David Howells4a3b9892009-06-11 13:05:24 +0100338 case PTRACE_SETFPREGS: /* Set the child FP/Media state. */
339 return copy_regset_from_user(child, &user_frv_native_view,
340 REGSET_FPMEDIA,
341 0, sizeof(child->thread.user->f),
Namhyung Kim81d32852010-10-27 15:33:52 -0700342 datap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 default:
David Howells4a3b9892009-06-11 13:05:24 +0100345 ret = ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 break;
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return ret;
349}
350
David Howells4a3b9892009-06-11 13:05:24 +0100351/*
352 * handle tracing of system call entry
353 * - return the revised system call number or ULONG_MAX to cause ENOSYS
354 */
355asmlinkage unsigned long syscall_trace_entry(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
David Howells4a3b9892009-06-11 13:05:24 +0100357 __frame->__status |= REG__STATUS_SYSC_ENTRY;
358 if (tracehook_report_syscall_entry(__frame)) {
359 /* tracing decided this syscall should not happen, so
360 * We'll return a bogus call number to get an ENOSYS
361 * error, but leave the original number in
362 * __frame->syscallno
363 */
364 return ULONG_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
David Howells4a3b9892009-06-11 13:05:24 +0100366
367 return __frame->syscallno;
368}
369
370/*
371 * handle tracing of system call exit
372 */
373asmlinkage void syscall_trace_exit(void)
374{
375 __frame->__status |= REG__STATUS_SYSC_EXIT;
376 tracehook_report_syscall_exit(__frame, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}