blob: 646dd58169ecb077eb257b051afbbc131cc4a77f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* ptrace.c: Sparc process tracing support.
3 *
David S. Miller8e3fe802008-02-06 21:00:44 -08004 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
7 * and David Mosberger.
8 *
Joe Perches5b2afff2007-12-20 13:55:45 -08009 * Added Linux support -miguel (weird, eh?, the original code was meant
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * to emulate SunOS).
11 */
12
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/errno.h>
17#include <linux/ptrace.h>
18#include <linux/user.h>
19#include <linux/smp.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 S. Miller8e3fe802008-02-06 21:00:44 -080022#include <linux/regset.h>
23#include <linux/elf.h>
David S. Miller1c133b42008-07-27 03:13:13 -070024#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/pgtable.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
David Howellsd550bbd2012-03-28 18:30:03 +010028#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Sam Ravnborgc8c87822014-04-21 21:39:37 +020030#include "kernel.h"
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/* #define ALLOW_INIT_TRACING */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*
35 * Called by kernel/ptrace.c when detaching..
36 *
37 * Make sure single step bits etc are not set.
38 */
39void ptrace_disable(struct task_struct *child)
40{
41 /* nothing to do */
42}
43
David S. Miller8e3fe802008-02-06 21:00:44 -080044enum sparc_regset {
45 REGSET_GENERAL,
46 REGSET_FP,
47};
48
Al Virocf51e122020-05-17 12:20:40 -040049static int regwindow32_get(struct task_struct *target,
50 const struct pt_regs *regs,
51 u32 *uregs)
52{
53 unsigned long reg_window = regs->u_regs[UREG_I6];
54 int size = 16 * sizeof(u32);
55
56 if (target == current) {
57 if (copy_from_user(uregs, (void __user *)reg_window, size))
58 return -EFAULT;
59 } else {
60 if (access_process_vm(target, reg_window, uregs, size,
61 FOLL_FORCE) != size)
62 return -EFAULT;
63 }
64 return 0;
65}
66
67static int regwindow32_set(struct task_struct *target,
68 const struct pt_regs *regs,
69 u32 *uregs)
70{
71 unsigned long reg_window = regs->u_regs[UREG_I6];
72 int size = 16 * sizeof(u32);
73
74 if (target == current) {
75 if (copy_to_user((void __user *)reg_window, uregs, size))
76 return -EFAULT;
77 } else {
78 if (access_process_vm(target, reg_window, uregs, size,
79 FOLL_FORCE | FOLL_WRITE) != size)
80 return -EFAULT;
81 }
82 return 0;
83}
84
David S. Miller8e3fe802008-02-06 21:00:44 -080085static int genregs32_get(struct task_struct *target,
86 const struct user_regset *regset,
87 unsigned int pos, unsigned int count,
88 void *kbuf, void __user *ubuf)
89{
90 const struct pt_regs *regs = target->thread.kregs;
Al Virocf51e122020-05-17 12:20:40 -040091 u32 uregs[16];
92 int ret;
David S. Miller8e3fe802008-02-06 21:00:44 -080093
94 if (target == current)
95 flush_user_windows();
96
Al Virocf51e122020-05-17 12:20:40 -040097 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
98 regs->u_regs,
99 0, 16 * sizeof(u32));
100 if (ret || !count)
101 return ret;
David S. Miller8e3fe802008-02-06 21:00:44 -0800102
Al Virocf51e122020-05-17 12:20:40 -0400103 if (pos < 32 * sizeof(u32)) {
104 if (regwindow32_get(target, regs, uregs))
David S. Miller8e3fe802008-02-06 21:00:44 -0800105 return -EFAULT;
Al Virocf51e122020-05-17 12:20:40 -0400106 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
107 uregs,
108 16 * sizeof(u32), 32 * sizeof(u32));
109 if (ret || !count)
110 return ret;
David S. Miller8e3fe802008-02-06 21:00:44 -0800111 }
David S. Miller8e3fe802008-02-06 21:00:44 -0800112
Al Virocf51e122020-05-17 12:20:40 -0400113 uregs[0] = regs->psr;
114 uregs[1] = regs->pc;
115 uregs[2] = regs->npc;
116 uregs[3] = regs->y;
117 uregs[4] = 0; /* WIM */
118 uregs[5] = 0; /* TBR */
119 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
120 uregs,
121 32 * sizeof(u32), 38 * sizeof(u32));
David S. Miller8e3fe802008-02-06 21:00:44 -0800122}
123
124static int genregs32_set(struct task_struct *target,
125 const struct user_regset *regset,
126 unsigned int pos, unsigned int count,
127 const void *kbuf, const void __user *ubuf)
128{
129 struct pt_regs *regs = target->thread.kregs;
Al Virocf51e122020-05-17 12:20:40 -0400130 u32 uregs[16];
131 u32 psr;
132 int ret;
David S. Miller8e3fe802008-02-06 21:00:44 -0800133
134 if (target == current)
135 flush_user_windows();
136
Al Virocf51e122020-05-17 12:20:40 -0400137 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
138 regs->u_regs,
139 0, 16 * sizeof(u32));
140 if (ret || !count)
141 return ret;
David S. Miller8e3fe802008-02-06 21:00:44 -0800142
Al Virocf51e122020-05-17 12:20:40 -0400143 if (pos < 32 * sizeof(u32)) {
144 if (regwindow32_get(target, regs, uregs))
David S. Miller8e3fe802008-02-06 21:00:44 -0800145 return -EFAULT;
Al Virocf51e122020-05-17 12:20:40 -0400146 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
147 uregs,
148 16 * sizeof(u32), 32 * sizeof(u32));
149 if (ret)
150 return ret;
151 if (regwindow32_set(target, regs, uregs))
152 return -EFAULT;
153 if (!count)
154 return 0;
David S. Miller8e3fe802008-02-06 21:00:44 -0800155 }
Al Virocf51e122020-05-17 12:20:40 -0400156 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
157 &psr,
158 32 * sizeof(u32), 33 * sizeof(u32));
159 if (ret)
160 return ret;
161 regs->psr = (regs->psr & ~(PSR_ICC | PSR_SYSCALL)) |
162 (psr & (PSR_ICC | PSR_SYSCALL));
163 if (!count)
164 return 0;
165 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
166 &regs->pc,
167 33 * sizeof(u32), 34 * sizeof(u32));
168 if (ret || !count)
169 return ret;
170 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
Al Viro9d964e12020-06-06 23:44:24 -0400171 &regs->npc,
Al Virocf51e122020-05-17 12:20:40 -0400172 34 * sizeof(u32), 35 * sizeof(u32));
173 if (ret || !count)
174 return ret;
Al Viro9d964e12020-06-06 23:44:24 -0400175 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
176 &regs->y,
177 35 * sizeof(u32), 36 * sizeof(u32));
178 if (ret || !count)
179 return ret;
David S. Miller8e3fe802008-02-06 21:00:44 -0800180 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
Al Viro9d964e12020-06-06 23:44:24 -0400181 36 * sizeof(u32), 38 * sizeof(u32));
David S. Miller8e3fe802008-02-06 21:00:44 -0800182}
183
184static int fpregs32_get(struct task_struct *target,
185 const struct user_regset *regset,
186 unsigned int pos, unsigned int count,
187 void *kbuf, void __user *ubuf)
188{
189 const unsigned long *fpregs = target->thread.float_regs;
190 int ret = 0;
191
192#if 0
193 if (target == current)
194 save_and_clear_fpu();
195#endif
196
197 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
198 fpregs,
199 0, 32 * sizeof(u32));
200
201 if (!ret)
202 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
203 32 * sizeof(u32),
204 33 * sizeof(u32));
205 if (!ret)
206 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
207 &target->thread.fsr,
208 33 * sizeof(u32),
209 34 * sizeof(u32));
210
211 if (!ret) {
212 unsigned long val;
213
214 val = (1 << 8) | (8 << 16);
215 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
216 &val,
217 34 * sizeof(u32),
218 35 * sizeof(u32));
219 }
220
221 if (!ret)
222 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
223 35 * sizeof(u32), -1);
224
225 return ret;
226}
227
228static int fpregs32_set(struct task_struct *target,
229 const struct user_regset *regset,
230 unsigned int pos, unsigned int count,
231 const void *kbuf, const void __user *ubuf)
232{
233 unsigned long *fpregs = target->thread.float_regs;
234 int ret;
235
236#if 0
237 if (target == current)
238 save_and_clear_fpu();
239#endif
240 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
241 fpregs,
242 0, 32 * sizeof(u32));
243 if (!ret)
244 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
245 32 * sizeof(u32),
246 33 * sizeof(u32));
247 if (!ret && count > 0) {
248 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
249 &target->thread.fsr,
250 33 * sizeof(u32),
251 34 * sizeof(u32));
252 }
253
254 if (!ret)
255 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
256 34 * sizeof(u32), -1);
257 return ret;
258}
259
260static const struct user_regset sparc32_regsets[] = {
261 /* Format is:
262 * G0 --> G7
263 * O0 --> O7
264 * L0 --> L7
265 * I0 --> I7
266 * PSR, PC, nPC, Y, WIM, TBR
267 */
268 [REGSET_GENERAL] = {
269 .core_note_type = NT_PRSTATUS,
David S. Miller7d4ee282008-09-12 15:01:31 -0700270 .n = 38,
David S. Miller8e3fe802008-02-06 21:00:44 -0800271 .size = sizeof(u32), .align = sizeof(u32),
272 .get = genregs32_get, .set = genregs32_set
273 },
274 /* Format is:
275 * F0 --> F31
276 * empty 32-bit word
277 * FSR (32--bit word)
278 * FPU QUEUE COUNT (8-bit char)
279 * FPU QUEUE ENTRYSIZE (8-bit char)
280 * FPU ENABLED (8-bit char)
281 * empty 8-bit char
282 * FPU QUEUE (64 32-bit ints)
283 */
284 [REGSET_FP] = {
285 .core_note_type = NT_PRFPREG,
David S. Miller7d4ee282008-09-12 15:01:31 -0700286 .n = 99,
David S. Miller8e3fe802008-02-06 21:00:44 -0800287 .size = sizeof(u32), .align = sizeof(u32),
288 .get = fpregs32_get, .set = fpregs32_set
289 },
290};
291
292static const struct user_regset_view user_sparc32_view = {
293 .name = "sparc", .e_machine = EM_SPARC,
294 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
295};
296
297const struct user_regset_view *task_user_regset_view(struct task_struct *task)
298{
299 return &user_sparc32_view;
300}
301
Namhyung Kima9384e232010-10-27 15:34:03 -0700302struct fps {
303 unsigned long regs[32];
304 unsigned long fsr;
305 unsigned long flags;
306 unsigned long extra;
307 unsigned long fpqd;
308 struct fq {
309 unsigned long *insnaddr;
310 unsigned long insn;
311 } fpq[16];
312};
313
Namhyung Kim9b05a692010-10-27 15:33:47 -0700314long arch_ptrace(struct task_struct *child, long request,
315 unsigned long addr, unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
David S. Miller97753692008-02-07 03:00:17 -0800317 unsigned long addr2 = current->thread.kregs->u_regs[UREG_I4];
Namhyung Kima9384e232010-10-27 15:34:03 -0700318 void __user *addr2p;
David S. Millerd256eb82008-02-07 05:06:51 -0800319 const struct user_regset_view *view;
Namhyung Kima9384e232010-10-27 15:34:03 -0700320 struct pt_regs __user *pregs;
321 struct fps __user *fps;
David S. Millerd256eb82008-02-07 05:06:51 -0800322 int ret;
323
David S. Millerd786a4a2008-04-09 19:39:25 -0700324 view = task_user_regset_view(current);
Namhyung Kima9384e232010-10-27 15:34:03 -0700325 addr2p = (void __user *) addr2;
326 pregs = (struct pt_regs __user *) addr;
327 fps = (struct fps __user *) addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 switch(request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 case PTRACE_GETREGS: {
David S. Millerd256eb82008-02-07 05:06:51 -0800331 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
332 32 * sizeof(u32),
333 4 * sizeof(u32),
334 &pregs->psr);
335 if (!ret)
336 copy_regset_to_user(child, view, REGSET_GENERAL,
337 1 * sizeof(u32),
338 15 * sizeof(u32),
339 &pregs->u_regs[0]);
David S. Miller97753692008-02-07 03:00:17 -0800340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 case PTRACE_SETREGS: {
David S. Millerd256eb82008-02-07 05:06:51 -0800344 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
345 32 * sizeof(u32),
346 4 * sizeof(u32),
347 &pregs->psr);
348 if (!ret)
349 copy_regset_from_user(child, view, REGSET_GENERAL,
350 1 * sizeof(u32),
351 15 * sizeof(u32),
352 &pregs->u_regs[0]);
David S. Miller97753692008-02-07 03:00:17 -0800353 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 case PTRACE_GETFPREGS: {
David S. Millerd256eb82008-02-07 05:06:51 -0800357 ret = copy_regset_to_user(child, view, REGSET_FP,
358 0 * sizeof(u32),
359 32 * sizeof(u32),
360 &fps->regs[0]);
361 if (!ret)
362 ret = copy_regset_to_user(child, view, REGSET_FP,
363 33 * sizeof(u32),
364 1 * sizeof(u32),
365 &fps->fsr);
David S. Miller97753692008-02-07 03:00:17 -0800366
David S. Millerd256eb82008-02-07 05:06:51 -0800367 if (!ret) {
368 if (__put_user(0, &fps->fpqd) ||
369 __put_user(0, &fps->flags) ||
370 __put_user(0, &fps->extra) ||
371 clear_user(fps->fpq, sizeof(fps->fpq)))
372 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
David S. Miller97753692008-02-07 03:00:17 -0800374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
377 case PTRACE_SETFPREGS: {
David S. Millerd256eb82008-02-07 05:06:51 -0800378 ret = copy_regset_from_user(child, view, REGSET_FP,
379 0 * sizeof(u32),
380 32 * sizeof(u32),
381 &fps->regs[0]);
382 if (!ret)
383 ret = copy_regset_from_user(child, view, REGSET_FP,
384 33 * sizeof(u32),
385 1 * sizeof(u32),
386 &fps->fsr);
David S. Miller97753692008-02-07 03:00:17 -0800387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 case PTRACE_READTEXT:
David S. Miller97753692008-02-07 03:00:17 -0800391 case PTRACE_READDATA:
Namhyung Kima9384e232010-10-27 15:34:03 -0700392 ret = ptrace_readdata(child, addr, addr2p, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David S. Miller97753692008-02-07 03:00:17 -0800394 if (ret == data)
395 ret = 0;
396 else if (ret >= 0)
397 ret = -EIO;
398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 case PTRACE_WRITETEXT:
David S. Miller97753692008-02-07 03:00:17 -0800401 case PTRACE_WRITEDATA:
Namhyung Kima9384e232010-10-27 15:34:03 -0700402 ret = ptrace_writedata(child, addr2p, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
David S. Miller97753692008-02-07 03:00:17 -0800404 if (ret == data)
405 ret = 0;
406 else if (ret >= 0)
407 ret = -EIO;
408 break;
409
410 default:
David S. Miller986bef82008-05-10 21:11:23 -0700411 if (request == PTRACE_SPARC_DETACH)
412 request = PTRACE_DETACH;
David S. Miller97753692008-02-07 03:00:17 -0800413 ret = ptrace_request(child, request, addr, data);
414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
David S. Miller97753692008-02-07 03:00:17 -0800417 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
David S. Miller1c133b42008-07-27 03:13:13 -0700420asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
David S. Miller1c133b42008-07-27 03:13:13 -0700422 int ret = 0;
423
424 if (test_thread_flag(TIF_SYSCALL_TRACE)) {
425 if (syscall_exit_p)
426 tracehook_report_syscall_exit(regs, 0);
427 else
428 ret = tracehook_report_syscall_entry(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
David S. Miller1c133b42008-07-27 03:13:13 -0700430
431 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}