mm: make wait_on_page_writeback() wait for multiple pending writebacks
[linux/fpc-iii.git] / arch / xtensa / kernel / ptrace.c
blobbb3f4797d212b0fa432b087fab00ed6959de7c04
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
6 * Copyright (C) 2001 - 2007 Tensilica Inc.
8 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
9 * Chris Zankel <chris@zankel.net>
10 * Scott Foehner<sfoehner@yahoo.com>,
11 * Kevin Chea
12 * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
15 #include <linux/audit.h>
16 #include <linux/errno.h>
17 #include <linux/hw_breakpoint.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/perf_event.h>
21 #include <linux/ptrace.h>
22 #include <linux/regset.h>
23 #include <linux/sched.h>
24 #include <linux/sched/task_stack.h>
25 #include <linux/seccomp.h>
26 #include <linux/security.h>
27 #include <linux/signal.h>
28 #include <linux/smp.h>
29 #include <linux/tracehook.h>
30 #include <linux/uaccess.h>
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/syscalls.h>
35 #include <asm/coprocessor.h>
36 #include <asm/elf.h>
37 #include <asm/page.h>
38 #include <asm/ptrace.h>
40 static int gpr_get(struct task_struct *target,
41 const struct user_regset *regset,
42 struct membuf to)
44 struct pt_regs *regs = task_pt_regs(target);
45 struct user_pt_regs newregs = {
46 .pc = regs->pc,
47 .ps = regs->ps & ~(1 << PS_EXCM_BIT),
48 .lbeg = regs->lbeg,
49 .lend = regs->lend,
50 .lcount = regs->lcount,
51 .sar = regs->sar,
52 .threadptr = regs->threadptr,
53 .windowbase = regs->windowbase,
54 .windowstart = regs->windowstart,
55 .syscall = regs->syscall,
58 memcpy(newregs.a,
59 regs->areg + XCHAL_NUM_AREGS - regs->windowbase * 4,
60 regs->windowbase * 16);
61 memcpy(newregs.a + regs->windowbase * 4,
62 regs->areg,
63 (WSBITS - regs->windowbase) * 16);
65 return membuf_write(&to, &newregs, sizeof(newregs));
68 static int gpr_set(struct task_struct *target,
69 const struct user_regset *regset,
70 unsigned int pos, unsigned int count,
71 const void *kbuf, const void __user *ubuf)
73 int ret;
74 struct user_pt_regs newregs = {0};
75 struct pt_regs *regs;
76 const u32 ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
78 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
79 if (ret)
80 return ret;
82 if (newregs.windowbase >= XCHAL_NUM_AREGS / 4)
83 return -EINVAL;
85 regs = task_pt_regs(target);
86 regs->pc = newregs.pc;
87 regs->ps = (regs->ps & ~ps_mask) | (newregs.ps & ps_mask);
88 regs->lbeg = newregs.lbeg;
89 regs->lend = newregs.lend;
90 regs->lcount = newregs.lcount;
91 regs->sar = newregs.sar;
92 regs->threadptr = newregs.threadptr;
94 if (newregs.syscall)
95 regs->syscall = newregs.syscall;
97 if (newregs.windowbase != regs->windowbase ||
98 newregs.windowstart != regs->windowstart) {
99 u32 rotws, wmask;
101 rotws = (((newregs.windowstart |
102 (newregs.windowstart << WSBITS)) >>
103 newregs.windowbase) &
104 ((1 << WSBITS) - 1)) & ~1;
105 wmask = ((rotws ? WSBITS + 1 - ffs(rotws) : 0) << 4) |
106 (rotws & 0xF) | 1;
107 regs->windowbase = newregs.windowbase;
108 regs->windowstart = newregs.windowstart;
109 regs->wmask = wmask;
112 memcpy(regs->areg + XCHAL_NUM_AREGS - newregs.windowbase * 4,
113 newregs.a, newregs.windowbase * 16);
114 memcpy(regs->areg, newregs.a + newregs.windowbase * 4,
115 (WSBITS - newregs.windowbase) * 16);
117 return 0;
120 static int tie_get(struct task_struct *target,
121 const struct user_regset *regset,
122 struct membuf to)
124 int ret;
125 struct pt_regs *regs = task_pt_regs(target);
126 struct thread_info *ti = task_thread_info(target);
127 elf_xtregs_t *newregs = kzalloc(sizeof(elf_xtregs_t), GFP_KERNEL);
129 if (!newregs)
130 return -ENOMEM;
132 newregs->opt = regs->xtregs_opt;
133 newregs->user = ti->xtregs_user;
135 #if XTENSA_HAVE_COPROCESSORS
136 /* Flush all coprocessor registers to memory. */
137 coprocessor_flush_all(ti);
138 newregs->cp0 = ti->xtregs_cp.cp0;
139 newregs->cp1 = ti->xtregs_cp.cp1;
140 newregs->cp2 = ti->xtregs_cp.cp2;
141 newregs->cp3 = ti->xtregs_cp.cp3;
142 newregs->cp4 = ti->xtregs_cp.cp4;
143 newregs->cp5 = ti->xtregs_cp.cp5;
144 newregs->cp6 = ti->xtregs_cp.cp6;
145 newregs->cp7 = ti->xtregs_cp.cp7;
146 #endif
147 ret = membuf_write(&to, newregs, sizeof(*newregs));
148 kfree(newregs);
149 return ret;
152 static int tie_set(struct task_struct *target,
153 const struct user_regset *regset,
154 unsigned int pos, unsigned int count,
155 const void *kbuf, const void __user *ubuf)
157 int ret;
158 struct pt_regs *regs = task_pt_regs(target);
159 struct thread_info *ti = task_thread_info(target);
160 elf_xtregs_t *newregs = kzalloc(sizeof(elf_xtregs_t), GFP_KERNEL);
162 if (!newregs)
163 return -ENOMEM;
165 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
166 newregs, 0, -1);
168 if (ret)
169 goto exit;
170 regs->xtregs_opt = newregs->opt;
171 ti->xtregs_user = newregs->user;
173 #if XTENSA_HAVE_COPROCESSORS
174 /* Flush all coprocessors before we overwrite them. */
175 coprocessor_flush_all(ti);
176 coprocessor_release_all(ti);
177 ti->xtregs_cp.cp0 = newregs->cp0;
178 ti->xtregs_cp.cp1 = newregs->cp1;
179 ti->xtregs_cp.cp2 = newregs->cp2;
180 ti->xtregs_cp.cp3 = newregs->cp3;
181 ti->xtregs_cp.cp4 = newregs->cp4;
182 ti->xtregs_cp.cp5 = newregs->cp5;
183 ti->xtregs_cp.cp6 = newregs->cp6;
184 ti->xtregs_cp.cp7 = newregs->cp7;
185 #endif
186 exit:
187 kfree(newregs);
188 return ret;
191 enum xtensa_regset {
192 REGSET_GPR,
193 REGSET_TIE,
196 static const struct user_regset xtensa_regsets[] = {
197 [REGSET_GPR] = {
198 .core_note_type = NT_PRSTATUS,
199 .n = sizeof(struct user_pt_regs) / sizeof(u32),
200 .size = sizeof(u32),
201 .align = sizeof(u32),
202 .regset_get = gpr_get,
203 .set = gpr_set,
205 [REGSET_TIE] = {
206 .core_note_type = NT_PRFPREG,
207 .n = sizeof(elf_xtregs_t) / sizeof(u32),
208 .size = sizeof(u32),
209 .align = sizeof(u32),
210 .regset_get = tie_get,
211 .set = tie_set,
215 static const struct user_regset_view user_xtensa_view = {
216 .name = "xtensa",
217 .e_machine = EM_XTENSA,
218 .regsets = xtensa_regsets,
219 .n = ARRAY_SIZE(xtensa_regsets)
222 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
224 return &user_xtensa_view;
227 void user_enable_single_step(struct task_struct *child)
229 child->ptrace |= PT_SINGLESTEP;
232 void user_disable_single_step(struct task_struct *child)
234 child->ptrace &= ~PT_SINGLESTEP;
238 * Called by kernel/ptrace.c when detaching to disable single stepping.
241 void ptrace_disable(struct task_struct *child)
243 /* Nothing to do.. */
246 static int ptrace_getregs(struct task_struct *child, void __user *uregs)
248 return copy_regset_to_user(child, &user_xtensa_view, REGSET_GPR,
249 0, sizeof(xtensa_gregset_t), uregs);
252 static int ptrace_setregs(struct task_struct *child, void __user *uregs)
254 return copy_regset_from_user(child, &user_xtensa_view, REGSET_GPR,
255 0, sizeof(xtensa_gregset_t), uregs);
258 static int ptrace_getxregs(struct task_struct *child, void __user *uregs)
260 return copy_regset_to_user(child, &user_xtensa_view, REGSET_TIE,
261 0, sizeof(elf_xtregs_t), uregs);
264 static int ptrace_setxregs(struct task_struct *child, void __user *uregs)
266 return copy_regset_from_user(child, &user_xtensa_view, REGSET_TIE,
267 0, sizeof(elf_xtregs_t), uregs);
270 static int ptrace_peekusr(struct task_struct *child, long regno,
271 long __user *ret)
273 struct pt_regs *regs;
274 unsigned long tmp;
276 regs = task_pt_regs(child);
277 tmp = 0; /* Default return value. */
279 switch(regno) {
280 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
281 tmp = regs->areg[regno - REG_AR_BASE];
282 break;
284 case REG_A_BASE ... REG_A_BASE + 15:
285 tmp = regs->areg[regno - REG_A_BASE];
286 break;
288 case REG_PC:
289 tmp = regs->pc;
290 break;
292 case REG_PS:
293 /* Note: PS.EXCM is not set while user task is running;
294 * its being set in regs is for exception handling
295 * convenience.
297 tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
298 break;
300 case REG_WB:
301 break; /* tmp = 0 */
303 case REG_WS:
305 unsigned long wb = regs->windowbase;
306 unsigned long ws = regs->windowstart;
307 tmp = ((ws >> wb) | (ws << (WSBITS - wb))) &
308 ((1 << WSBITS) - 1);
309 break;
311 case REG_LBEG:
312 tmp = regs->lbeg;
313 break;
315 case REG_LEND:
316 tmp = regs->lend;
317 break;
319 case REG_LCOUNT:
320 tmp = regs->lcount;
321 break;
323 case REG_SAR:
324 tmp = regs->sar;
325 break;
327 case SYSCALL_NR:
328 tmp = regs->syscall;
329 break;
331 default:
332 return -EIO;
334 return put_user(tmp, ret);
337 static int ptrace_pokeusr(struct task_struct *child, long regno, long val)
339 struct pt_regs *regs;
340 regs = task_pt_regs(child);
342 switch (regno) {
343 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
344 regs->areg[regno - REG_AR_BASE] = val;
345 break;
347 case REG_A_BASE ... REG_A_BASE + 15:
348 regs->areg[regno - REG_A_BASE] = val;
349 break;
351 case REG_PC:
352 regs->pc = val;
353 break;
355 case SYSCALL_NR:
356 regs->syscall = val;
357 break;
359 default:
360 return -EIO;
362 return 0;
365 #ifdef CONFIG_HAVE_HW_BREAKPOINT
366 static void ptrace_hbptriggered(struct perf_event *bp,
367 struct perf_sample_data *data,
368 struct pt_regs *regs)
370 int i;
371 struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
373 if (bp->attr.bp_type & HW_BREAKPOINT_X) {
374 for (i = 0; i < XCHAL_NUM_IBREAK; ++i)
375 if (current->thread.ptrace_bp[i] == bp)
376 break;
377 i <<= 1;
378 } else {
379 for (i = 0; i < XCHAL_NUM_DBREAK; ++i)
380 if (current->thread.ptrace_wp[i] == bp)
381 break;
382 i = (i << 1) | 1;
385 force_sig_ptrace_errno_trap(i, (void __user *)bkpt->address);
388 static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type)
390 struct perf_event_attr attr;
392 ptrace_breakpoint_init(&attr);
394 /* Initialise fields to sane defaults. */
395 attr.bp_addr = 0;
396 attr.bp_len = 1;
397 attr.bp_type = type;
398 attr.disabled = 1;
400 return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL,
401 tsk);
405 * Address bit 0 choose instruction (0) or data (1) break register, bits
406 * 31..1 are the register number.
407 * Both PTRACE_GETHBPREGS and PTRACE_SETHBPREGS transfer two 32-bit words:
408 * address (0) and control (1).
409 * Instruction breakpoint contorl word is 0 to clear breakpoint, 1 to set.
410 * Data breakpoint control word bit 31 is 'trigger on store', bit 30 is
411 * 'trigger on load, bits 29..0 are length. Length 0 is used to clear a
412 * breakpoint. To set a breakpoint length must be a power of 2 in the range
413 * 1..64 and the address must be length-aligned.
416 static long ptrace_gethbpregs(struct task_struct *child, long addr,
417 long __user *datap)
419 struct perf_event *bp;
420 u32 user_data[2] = {0};
421 bool dbreak = addr & 1;
422 unsigned idx = addr >> 1;
424 if ((!dbreak && idx >= XCHAL_NUM_IBREAK) ||
425 (dbreak && idx >= XCHAL_NUM_DBREAK))
426 return -EINVAL;
428 if (dbreak)
429 bp = child->thread.ptrace_wp[idx];
430 else
431 bp = child->thread.ptrace_bp[idx];
433 if (bp) {
434 user_data[0] = bp->attr.bp_addr;
435 user_data[1] = bp->attr.disabled ? 0 : bp->attr.bp_len;
436 if (dbreak) {
437 if (bp->attr.bp_type & HW_BREAKPOINT_R)
438 user_data[1] |= DBREAKC_LOAD_MASK;
439 if (bp->attr.bp_type & HW_BREAKPOINT_W)
440 user_data[1] |= DBREAKC_STOR_MASK;
444 if (copy_to_user(datap, user_data, sizeof(user_data)))
445 return -EFAULT;
447 return 0;
450 static long ptrace_sethbpregs(struct task_struct *child, long addr,
451 long __user *datap)
453 struct perf_event *bp;
454 struct perf_event_attr attr;
455 u32 user_data[2];
456 bool dbreak = addr & 1;
457 unsigned idx = addr >> 1;
458 int bp_type = 0;
460 if ((!dbreak && idx >= XCHAL_NUM_IBREAK) ||
461 (dbreak && idx >= XCHAL_NUM_DBREAK))
462 return -EINVAL;
464 if (copy_from_user(user_data, datap, sizeof(user_data)))
465 return -EFAULT;
467 if (dbreak) {
468 bp = child->thread.ptrace_wp[idx];
469 if (user_data[1] & DBREAKC_LOAD_MASK)
470 bp_type |= HW_BREAKPOINT_R;
471 if (user_data[1] & DBREAKC_STOR_MASK)
472 bp_type |= HW_BREAKPOINT_W;
473 } else {
474 bp = child->thread.ptrace_bp[idx];
475 bp_type = HW_BREAKPOINT_X;
478 if (!bp) {
479 bp = ptrace_hbp_create(child,
480 bp_type ? bp_type : HW_BREAKPOINT_RW);
481 if (IS_ERR(bp))
482 return PTR_ERR(bp);
483 if (dbreak)
484 child->thread.ptrace_wp[idx] = bp;
485 else
486 child->thread.ptrace_bp[idx] = bp;
489 attr = bp->attr;
490 attr.bp_addr = user_data[0];
491 attr.bp_len = user_data[1] & ~(DBREAKC_LOAD_MASK | DBREAKC_STOR_MASK);
492 attr.bp_type = bp_type;
493 attr.disabled = !attr.bp_len;
495 return modify_user_hw_breakpoint(bp, &attr);
497 #endif
499 long arch_ptrace(struct task_struct *child, long request,
500 unsigned long addr, unsigned long data)
502 int ret = -EPERM;
503 void __user *datap = (void __user *) data;
505 switch (request) {
506 case PTRACE_PEEKUSR: /* read register specified by addr. */
507 ret = ptrace_peekusr(child, addr, datap);
508 break;
510 case PTRACE_POKEUSR: /* write register specified by addr. */
511 ret = ptrace_pokeusr(child, addr, data);
512 break;
514 case PTRACE_GETREGS:
515 ret = ptrace_getregs(child, datap);
516 break;
518 case PTRACE_SETREGS:
519 ret = ptrace_setregs(child, datap);
520 break;
522 case PTRACE_GETXTREGS:
523 ret = ptrace_getxregs(child, datap);
524 break;
526 case PTRACE_SETXTREGS:
527 ret = ptrace_setxregs(child, datap);
528 break;
529 #ifdef CONFIG_HAVE_HW_BREAKPOINT
530 case PTRACE_GETHBPREGS:
531 ret = ptrace_gethbpregs(child, addr, datap);
532 break;
534 case PTRACE_SETHBPREGS:
535 ret = ptrace_sethbpregs(child, addr, datap);
536 break;
537 #endif
538 default:
539 ret = ptrace_request(child, request, addr, data);
540 break;
543 return ret;
546 void do_syscall_trace_leave(struct pt_regs *regs);
547 int do_syscall_trace_enter(struct pt_regs *regs)
549 if (regs->syscall == NO_SYSCALL)
550 regs->areg[2] = -ENOSYS;
552 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
553 tracehook_report_syscall_entry(regs)) {
554 regs->areg[2] = -ENOSYS;
555 regs->syscall = NO_SYSCALL;
556 return 0;
559 if (regs->syscall == NO_SYSCALL ||
560 secure_computing() == -1) {
561 do_syscall_trace_leave(regs);
562 return 0;
565 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
566 trace_sys_enter(regs, syscall_get_nr(current, regs));
568 audit_syscall_entry(regs->syscall, regs->areg[6],
569 regs->areg[3], regs->areg[4],
570 regs->areg[5]);
571 return 1;
574 void do_syscall_trace_leave(struct pt_regs *regs)
576 int step;
578 audit_syscall_exit(regs);
580 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
581 trace_sys_exit(regs, regs_return_value(regs));
583 step = test_thread_flag(TIF_SINGLESTEP);
585 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
586 tracehook_report_syscall_exit(regs, step);