MIPS: Add DSP ASE regset support
[linux-2.6-block.git] / arch / mips / kernel / ptrace.c
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.
5  *
6  * Copyright (C) 1992 Ross Biro
7  * Copyright (C) Linus Torvalds
8  * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
9  * Copyright (C) 1996 David S. Miller
10  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
11  * Copyright (C) 1999 MIPS Technologies, Inc.
12  * Copyright (C) 2000 Ulf Carlsson
13  *
14  * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
15  * binaries.
16  */
17 #include <linux/compiler.h>
18 #include <linux/context_tracking.h>
19 #include <linux/elf.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/sched/task_stack.h>
23 #include <linux/mm.h>
24 #include <linux/errno.h>
25 #include <linux/ptrace.h>
26 #include <linux/regset.h>
27 #include <linux/smp.h>
28 #include <linux/security.h>
29 #include <linux/stddef.h>
30 #include <linux/tracehook.h>
31 #include <linux/audit.h>
32 #include <linux/seccomp.h>
33 #include <linux/ftrace.h>
34
35 #include <asm/byteorder.h>
36 #include <asm/cpu.h>
37 #include <asm/cpu-info.h>
38 #include <asm/dsp.h>
39 #include <asm/fpu.h>
40 #include <asm/mipsregs.h>
41 #include <asm/mipsmtregs.h>
42 #include <asm/pgtable.h>
43 #include <asm/page.h>
44 #include <asm/processor.h>
45 #include <asm/syscall.h>
46 #include <linux/uaccess.h>
47 #include <asm/bootinfo.h>
48 #include <asm/reg.h>
49
50 #define CREATE_TRACE_POINTS
51 #include <trace/events/syscalls.h>
52
53 static void init_fp_ctx(struct task_struct *target)
54 {
55         /* If FP has been used then the target already has context */
56         if (tsk_used_math(target))
57                 return;
58
59         /* Begin with data registers set to all 1s... */
60         memset(&target->thread.fpu.fpr, ~0, sizeof(target->thread.fpu.fpr));
61
62         /* FCSR has been preset by `mips_set_personality_nan'.  */
63
64         /*
65          * Record that the target has "used" math, such that the context
66          * just initialised, and any modifications made by the caller,
67          * aren't discarded.
68          */
69         set_stopped_child_used_math(target);
70 }
71
72 /*
73  * Called by kernel/ptrace.c when detaching..
74  *
75  * Make sure single step bits etc are not set.
76  */
77 void ptrace_disable(struct task_struct *child)
78 {
79         /* Don't load the watchpoint registers for the ex-child. */
80         clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
81 }
82
83 /*
84  * Poke at FCSR according to its mask.  Set the Cause bits even
85  * if a corresponding Enable bit is set.  This will be noticed at
86  * the time the thread is switched to and SIGFPE thrown accordingly.
87  */
88 static void ptrace_setfcr31(struct task_struct *child, u32 value)
89 {
90         u32 fcr31;
91         u32 mask;
92
93         fcr31 = child->thread.fpu.fcr31;
94         mask = boot_cpu_data.fpu_msk31;
95         child->thread.fpu.fcr31 = (value & ~mask) | (fcr31 & mask);
96 }
97
98 /*
99  * Read a general register set.  We always use the 64-bit format, even
100  * for 32-bit kernels and for 32-bit processes on a 64-bit kernel.
101  * Registers are sign extended to fill the available space.
102  */
103 int ptrace_getregs(struct task_struct *child, struct user_pt_regs __user *data)
104 {
105         struct pt_regs *regs;
106         int i;
107
108         if (!access_ok(VERIFY_WRITE, data, 38 * 8))
109                 return -EIO;
110
111         regs = task_pt_regs(child);
112
113         for (i = 0; i < 32; i++)
114                 __put_user((long)regs->regs[i], (__s64 __user *)&data->regs[i]);
115         __put_user((long)regs->lo, (__s64 __user *)&data->lo);
116         __put_user((long)regs->hi, (__s64 __user *)&data->hi);
117         __put_user((long)regs->cp0_epc, (__s64 __user *)&data->cp0_epc);
118         __put_user((long)regs->cp0_badvaddr, (__s64 __user *)&data->cp0_badvaddr);
119         __put_user((long)regs->cp0_status, (__s64 __user *)&data->cp0_status);
120         __put_user((long)regs->cp0_cause, (__s64 __user *)&data->cp0_cause);
121
122         return 0;
123 }
124
125 /*
126  * Write a general register set.  As for PTRACE_GETREGS, we always use
127  * the 64-bit format.  On a 32-bit kernel only the lower order half
128  * (according to endianness) will be used.
129  */
130 int ptrace_setregs(struct task_struct *child, struct user_pt_regs __user *data)
131 {
132         struct pt_regs *regs;
133         int i;
134
135         if (!access_ok(VERIFY_READ, data, 38 * 8))
136                 return -EIO;
137
138         regs = task_pt_regs(child);
139
140         for (i = 0; i < 32; i++)
141                 __get_user(regs->regs[i], (__s64 __user *)&data->regs[i]);
142         __get_user(regs->lo, (__s64 __user *)&data->lo);
143         __get_user(regs->hi, (__s64 __user *)&data->hi);
144         __get_user(regs->cp0_epc, (__s64 __user *)&data->cp0_epc);
145
146         /* badvaddr, status, and cause may not be written.  */
147
148         /* System call number may have been changed */
149         mips_syscall_update_nr(child, regs);
150
151         return 0;
152 }
153
154 int ptrace_getfpregs(struct task_struct *child, __u32 __user *data)
155 {
156         int i;
157
158         if (!access_ok(VERIFY_WRITE, data, 33 * 8))
159                 return -EIO;
160
161         if (tsk_used_math(child)) {
162                 union fpureg *fregs = get_fpu_regs(child);
163                 for (i = 0; i < 32; i++)
164                         __put_user(get_fpr64(&fregs[i], 0),
165                                    i + (__u64 __user *)data);
166         } else {
167                 for (i = 0; i < 32; i++)
168                         __put_user((__u64) -1, i + (__u64 __user *) data);
169         }
170
171         __put_user(child->thread.fpu.fcr31, data + 64);
172         __put_user(boot_cpu_data.fpu_id, data + 65);
173
174         return 0;
175 }
176
177 int ptrace_setfpregs(struct task_struct *child, __u32 __user *data)
178 {
179         union fpureg *fregs;
180         u64 fpr_val;
181         u32 value;
182         int i;
183
184         if (!access_ok(VERIFY_READ, data, 33 * 8))
185                 return -EIO;
186
187         init_fp_ctx(child);
188         fregs = get_fpu_regs(child);
189
190         for (i = 0; i < 32; i++) {
191                 __get_user(fpr_val, i + (__u64 __user *)data);
192                 set_fpr64(&fregs[i], 0, fpr_val);
193         }
194
195         __get_user(value, data + 64);
196         ptrace_setfcr31(child, value);
197
198         /* FIR may not be written.  */
199
200         return 0;
201 }
202
203 int ptrace_get_watch_regs(struct task_struct *child,
204                           struct pt_watch_regs __user *addr)
205 {
206         enum pt_watch_style style;
207         int i;
208
209         if (!cpu_has_watch || boot_cpu_data.watch_reg_use_cnt == 0)
210                 return -EIO;
211         if (!access_ok(VERIFY_WRITE, addr, sizeof(struct pt_watch_regs)))
212                 return -EIO;
213
214 #ifdef CONFIG_32BIT
215         style = pt_watch_style_mips32;
216 #define WATCH_STYLE mips32
217 #else
218         style = pt_watch_style_mips64;
219 #define WATCH_STYLE mips64
220 #endif
221
222         __put_user(style, &addr->style);
223         __put_user(boot_cpu_data.watch_reg_use_cnt,
224                    &addr->WATCH_STYLE.num_valid);
225         for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
226                 __put_user(child->thread.watch.mips3264.watchlo[i],
227                            &addr->WATCH_STYLE.watchlo[i]);
228                 __put_user(child->thread.watch.mips3264.watchhi[i] &
229                                 (MIPS_WATCHHI_MASK | MIPS_WATCHHI_IRW),
230                            &addr->WATCH_STYLE.watchhi[i]);
231                 __put_user(boot_cpu_data.watch_reg_masks[i],
232                            &addr->WATCH_STYLE.watch_masks[i]);
233         }
234         for (; i < 8; i++) {
235                 __put_user(0, &addr->WATCH_STYLE.watchlo[i]);
236                 __put_user(0, &addr->WATCH_STYLE.watchhi[i]);
237                 __put_user(0, &addr->WATCH_STYLE.watch_masks[i]);
238         }
239
240         return 0;
241 }
242
243 int ptrace_set_watch_regs(struct task_struct *child,
244                           struct pt_watch_regs __user *addr)
245 {
246         int i;
247         int watch_active = 0;
248         unsigned long lt[NUM_WATCH_REGS];
249         u16 ht[NUM_WATCH_REGS];
250
251         if (!cpu_has_watch || boot_cpu_data.watch_reg_use_cnt == 0)
252                 return -EIO;
253         if (!access_ok(VERIFY_READ, addr, sizeof(struct pt_watch_regs)))
254                 return -EIO;
255         /* Check the values. */
256         for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
257                 __get_user(lt[i], &addr->WATCH_STYLE.watchlo[i]);
258 #ifdef CONFIG_32BIT
259                 if (lt[i] & __UA_LIMIT)
260                         return -EINVAL;
261 #else
262                 if (test_tsk_thread_flag(child, TIF_32BIT_ADDR)) {
263                         if (lt[i] & 0xffffffff80000000UL)
264                                 return -EINVAL;
265                 } else {
266                         if (lt[i] & __UA_LIMIT)
267                                 return -EINVAL;
268                 }
269 #endif
270                 __get_user(ht[i], &addr->WATCH_STYLE.watchhi[i]);
271                 if (ht[i] & ~MIPS_WATCHHI_MASK)
272                         return -EINVAL;
273         }
274         /* Install them. */
275         for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
276                 if (lt[i] & MIPS_WATCHLO_IRW)
277                         watch_active = 1;
278                 child->thread.watch.mips3264.watchlo[i] = lt[i];
279                 /* Set the G bit. */
280                 child->thread.watch.mips3264.watchhi[i] = ht[i];
281         }
282
283         if (watch_active)
284                 set_tsk_thread_flag(child, TIF_LOAD_WATCH);
285         else
286                 clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
287
288         return 0;
289 }
290
291 /* regset get/set implementations */
292
293 #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
294
295 static int gpr32_get(struct task_struct *target,
296                      const struct user_regset *regset,
297                      unsigned int pos, unsigned int count,
298                      void *kbuf, void __user *ubuf)
299 {
300         struct pt_regs *regs = task_pt_regs(target);
301         u32 uregs[ELF_NGREG] = {};
302
303         mips_dump_regs32(uregs, regs);
304         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
305                                    sizeof(uregs));
306 }
307
308 static int gpr32_set(struct task_struct *target,
309                      const struct user_regset *regset,
310                      unsigned int pos, unsigned int count,
311                      const void *kbuf, const void __user *ubuf)
312 {
313         struct pt_regs *regs = task_pt_regs(target);
314         u32 uregs[ELF_NGREG];
315         unsigned start, num_regs, i;
316         int err;
317
318         start = pos / sizeof(u32);
319         num_regs = count / sizeof(u32);
320
321         if (start + num_regs > ELF_NGREG)
322                 return -EIO;
323
324         err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
325                                  sizeof(uregs));
326         if (err)
327                 return err;
328
329         for (i = start; i < num_regs; i++) {
330                 /*
331                  * Cast all values to signed here so that if this is a 64-bit
332                  * kernel, the supplied 32-bit values will be sign extended.
333                  */
334                 switch (i) {
335                 case MIPS32_EF_R1 ... MIPS32_EF_R25:
336                         /* k0/k1 are ignored. */
337                 case MIPS32_EF_R28 ... MIPS32_EF_R31:
338                         regs->regs[i - MIPS32_EF_R0] = (s32)uregs[i];
339                         break;
340                 case MIPS32_EF_LO:
341                         regs->lo = (s32)uregs[i];
342                         break;
343                 case MIPS32_EF_HI:
344                         regs->hi = (s32)uregs[i];
345                         break;
346                 case MIPS32_EF_CP0_EPC:
347                         regs->cp0_epc = (s32)uregs[i];
348                         break;
349                 }
350         }
351
352         /* System call number may have been changed */
353         mips_syscall_update_nr(target, regs);
354
355         return 0;
356 }
357
358 #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
359
360 #ifdef CONFIG_64BIT
361
362 static int gpr64_get(struct task_struct *target,
363                      const struct user_regset *regset,
364                      unsigned int pos, unsigned int count,
365                      void *kbuf, void __user *ubuf)
366 {
367         struct pt_regs *regs = task_pt_regs(target);
368         u64 uregs[ELF_NGREG] = {};
369
370         mips_dump_regs64(uregs, regs);
371         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
372                                    sizeof(uregs));
373 }
374
375 static int gpr64_set(struct task_struct *target,
376                      const struct user_regset *regset,
377                      unsigned int pos, unsigned int count,
378                      const void *kbuf, const void __user *ubuf)
379 {
380         struct pt_regs *regs = task_pt_regs(target);
381         u64 uregs[ELF_NGREG];
382         unsigned start, num_regs, i;
383         int err;
384
385         start = pos / sizeof(u64);
386         num_regs = count / sizeof(u64);
387
388         if (start + num_regs > ELF_NGREG)
389                 return -EIO;
390
391         err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
392                                  sizeof(uregs));
393         if (err)
394                 return err;
395
396         for (i = start; i < num_regs; i++) {
397                 switch (i) {
398                 case MIPS64_EF_R1 ... MIPS64_EF_R25:
399                         /* k0/k1 are ignored. */
400                 case MIPS64_EF_R28 ... MIPS64_EF_R31:
401                         regs->regs[i - MIPS64_EF_R0] = uregs[i];
402                         break;
403                 case MIPS64_EF_LO:
404                         regs->lo = uregs[i];
405                         break;
406                 case MIPS64_EF_HI:
407                         regs->hi = uregs[i];
408                         break;
409                 case MIPS64_EF_CP0_EPC:
410                         regs->cp0_epc = uregs[i];
411                         break;
412                 }
413         }
414
415         /* System call number may have been changed */
416         mips_syscall_update_nr(target, regs);
417
418         return 0;
419 }
420
421 #endif /* CONFIG_64BIT */
422
423 /*
424  * Copy the floating-point context to the supplied NT_PRFPREG buffer,
425  * !CONFIG_CPU_HAS_MSA variant.  FP context's general register slots
426  * correspond 1:1 to buffer slots.  Only general registers are copied.
427  */
428 static int fpr_get_fpa(struct task_struct *target,
429                        unsigned int *pos, unsigned int *count,
430                        void **kbuf, void __user **ubuf)
431 {
432         return user_regset_copyout(pos, count, kbuf, ubuf,
433                                    &target->thread.fpu,
434                                    0, NUM_FPU_REGS * sizeof(elf_fpreg_t));
435 }
436
437 /*
438  * Copy the floating-point context to the supplied NT_PRFPREG buffer,
439  * CONFIG_CPU_HAS_MSA variant.  Only lower 64 bits of FP context's
440  * general register slots are copied to buffer slots.  Only general
441  * registers are copied.
442  */
443 static int fpr_get_msa(struct task_struct *target,
444                        unsigned int *pos, unsigned int *count,
445                        void **kbuf, void __user **ubuf)
446 {
447         unsigned int i;
448         u64 fpr_val;
449         int err;
450
451         BUILD_BUG_ON(sizeof(fpr_val) != sizeof(elf_fpreg_t));
452         for (i = 0; i < NUM_FPU_REGS; i++) {
453                 fpr_val = get_fpr64(&target->thread.fpu.fpr[i], 0);
454                 err = user_regset_copyout(pos, count, kbuf, ubuf,
455                                           &fpr_val, i * sizeof(elf_fpreg_t),
456                                           (i + 1) * sizeof(elf_fpreg_t));
457                 if (err)
458                         return err;
459         }
460
461         return 0;
462 }
463
464 /*
465  * Copy the floating-point context to the supplied NT_PRFPREG buffer.
466  * Choose the appropriate helper for general registers, and then copy
467  * the FCSR and FIR registers separately.
468  */
469 static int fpr_get(struct task_struct *target,
470                    const struct user_regset *regset,
471                    unsigned int pos, unsigned int count,
472                    void *kbuf, void __user *ubuf)
473 {
474         const int fcr31_pos = NUM_FPU_REGS * sizeof(elf_fpreg_t);
475         const int fir_pos = fcr31_pos + sizeof(u32);
476         int err;
477
478         if (sizeof(target->thread.fpu.fpr[0]) == sizeof(elf_fpreg_t))
479                 err = fpr_get_fpa(target, &pos, &count, &kbuf, &ubuf);
480         else
481                 err = fpr_get_msa(target, &pos, &count, &kbuf, &ubuf);
482         if (err)
483                 return err;
484
485         err = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
486                                   &target->thread.fpu.fcr31,
487                                   fcr31_pos, fcr31_pos + sizeof(u32));
488         if (err)
489                 return err;
490
491         err = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
492                                   &boot_cpu_data.fpu_id,
493                                   fir_pos, fir_pos + sizeof(u32));
494
495         return err;
496 }
497
498 /*
499  * Copy the supplied NT_PRFPREG buffer to the floating-point context,
500  * !CONFIG_CPU_HAS_MSA variant.   Buffer slots correspond 1:1 to FP
501  * context's general register slots.  Only general registers are copied.
502  */
503 static int fpr_set_fpa(struct task_struct *target,
504                        unsigned int *pos, unsigned int *count,
505                        const void **kbuf, const void __user **ubuf)
506 {
507         return user_regset_copyin(pos, count, kbuf, ubuf,
508                                   &target->thread.fpu,
509                                   0, NUM_FPU_REGS * sizeof(elf_fpreg_t));
510 }
511
512 /*
513  * Copy the supplied NT_PRFPREG buffer to the floating-point context,
514  * CONFIG_CPU_HAS_MSA variant.  Buffer slots are copied to lower 64
515  * bits only of FP context's general register slots.  Only general
516  * registers are copied.
517  */
518 static int fpr_set_msa(struct task_struct *target,
519                        unsigned int *pos, unsigned int *count,
520                        const void **kbuf, const void __user **ubuf)
521 {
522         unsigned int i;
523         u64 fpr_val;
524         int err;
525
526         BUILD_BUG_ON(sizeof(fpr_val) != sizeof(elf_fpreg_t));
527         for (i = 0; i < NUM_FPU_REGS && *count > 0; i++) {
528                 err = user_regset_copyin(pos, count, kbuf, ubuf,
529                                          &fpr_val, i * sizeof(elf_fpreg_t),
530                                          (i + 1) * sizeof(elf_fpreg_t));
531                 if (err)
532                         return err;
533                 set_fpr64(&target->thread.fpu.fpr[i], 0, fpr_val);
534         }
535
536         return 0;
537 }
538
539 /*
540  * Copy the supplied NT_PRFPREG buffer to the floating-point context.
541  * Choose the appropriate helper for general registers, and then copy
542  * the FCSR register separately.  Ignore the incoming FIR register
543  * contents though, as the register is read-only.
544  *
545  * We optimize for the case where `count % sizeof(elf_fpreg_t) == 0',
546  * which is supposed to have been guaranteed by the kernel before
547  * calling us, e.g. in `ptrace_regset'.  We enforce that requirement,
548  * so that we can safely avoid preinitializing temporaries for
549  * partial register writes.
550  */
551 static int fpr_set(struct task_struct *target,
552                    const struct user_regset *regset,
553                    unsigned int pos, unsigned int count,
554                    const void *kbuf, const void __user *ubuf)
555 {
556         const int fcr31_pos = NUM_FPU_REGS * sizeof(elf_fpreg_t);
557         const int fir_pos = fcr31_pos + sizeof(u32);
558         u32 fcr31;
559         int err;
560
561         BUG_ON(count % sizeof(elf_fpreg_t));
562
563         if (pos + count > sizeof(elf_fpregset_t))
564                 return -EIO;
565
566         init_fp_ctx(target);
567
568         if (sizeof(target->thread.fpu.fpr[0]) == sizeof(elf_fpreg_t))
569                 err = fpr_set_fpa(target, &pos, &count, &kbuf, &ubuf);
570         else
571                 err = fpr_set_msa(target, &pos, &count, &kbuf, &ubuf);
572         if (err)
573                 return err;
574
575         if (count > 0) {
576                 err = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
577                                          &fcr31,
578                                          fcr31_pos, fcr31_pos + sizeof(u32));
579                 if (err)
580                         return err;
581
582                 ptrace_setfcr31(target, fcr31);
583         }
584
585         if (count > 0)
586                 err = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
587                                                 fir_pos,
588                                                 fir_pos + sizeof(u32));
589
590         return err;
591 }
592
593 #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
594
595 /*
596  * Copy the DSP context to the supplied 32-bit NT_MIPS_DSP buffer.
597  */
598 static int dsp32_get(struct task_struct *target,
599                      const struct user_regset *regset,
600                      unsigned int pos, unsigned int count,
601                      void *kbuf, void __user *ubuf)
602 {
603         unsigned int start, num_regs, i;
604         u32 dspregs[NUM_DSP_REGS + 1];
605
606         BUG_ON(count % sizeof(u32));
607
608         if (!cpu_has_dsp)
609                 return -EIO;
610
611         start = pos / sizeof(u32);
612         num_regs = count / sizeof(u32);
613
614         if (start + num_regs > NUM_DSP_REGS + 1)
615                 return -EIO;
616
617         for (i = start; i < num_regs; i++)
618                 switch (i) {
619                 case 0 ... NUM_DSP_REGS - 1:
620                         dspregs[i] = target->thread.dsp.dspr[i];
621                         break;
622                 case NUM_DSP_REGS:
623                         dspregs[i] = target->thread.dsp.dspcontrol;
624                         break;
625                 }
626         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, dspregs, 0,
627                                    sizeof(dspregs));
628 }
629
630 /*
631  * Copy the supplied 32-bit NT_MIPS_DSP buffer to the DSP context.
632  */
633 static int dsp32_set(struct task_struct *target,
634                      const struct user_regset *regset,
635                      unsigned int pos, unsigned int count,
636                      const void *kbuf, const void __user *ubuf)
637 {
638         unsigned int start, num_regs, i;
639         u32 dspregs[NUM_DSP_REGS + 1];
640         int err;
641
642         BUG_ON(count % sizeof(u32));
643
644         if (!cpu_has_dsp)
645                 return -EIO;
646
647         start = pos / sizeof(u32);
648         num_regs = count / sizeof(u32);
649
650         if (start + num_regs > NUM_DSP_REGS + 1)
651                 return -EIO;
652
653         err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, dspregs, 0,
654                                  sizeof(dspregs));
655         if (err)
656                 return err;
657
658         for (i = start; i < num_regs; i++)
659                 switch (i) {
660                 case 0 ... NUM_DSP_REGS - 1:
661                         target->thread.dsp.dspr[i] = (s32)dspregs[i];
662                         break;
663                 case NUM_DSP_REGS:
664                         target->thread.dsp.dspcontrol = (s32)dspregs[i];
665                         break;
666                 }
667
668         return 0;
669 }
670
671 #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
672
673 #ifdef CONFIG_64BIT
674
675 /*
676  * Copy the DSP context to the supplied 64-bit NT_MIPS_DSP buffer.
677  */
678 static int dsp64_get(struct task_struct *target,
679                      const struct user_regset *regset,
680                      unsigned int pos, unsigned int count,
681                      void *kbuf, void __user *ubuf)
682 {
683         unsigned int start, num_regs, i;
684         u64 dspregs[NUM_DSP_REGS + 1];
685
686         BUG_ON(count % sizeof(u64));
687
688         if (!cpu_has_dsp)
689                 return -EIO;
690
691         start = pos / sizeof(u64);
692         num_regs = count / sizeof(u64);
693
694         if (start + num_regs > NUM_DSP_REGS + 1)
695                 return -EIO;
696
697         for (i = start; i < num_regs; i++)
698                 switch (i) {
699                 case 0 ... NUM_DSP_REGS - 1:
700                         dspregs[i] = target->thread.dsp.dspr[i];
701                         break;
702                 case NUM_DSP_REGS:
703                         dspregs[i] = target->thread.dsp.dspcontrol;
704                         break;
705                 }
706         return user_regset_copyout(&pos, &count, &kbuf, &ubuf, dspregs, 0,
707                                    sizeof(dspregs));
708 }
709
710 /*
711  * Copy the supplied 64-bit NT_MIPS_DSP buffer to the DSP context.
712  */
713 static int dsp64_set(struct task_struct *target,
714                      const struct user_regset *regset,
715                      unsigned int pos, unsigned int count,
716                      const void *kbuf, const void __user *ubuf)
717 {
718         unsigned int start, num_regs, i;
719         u64 dspregs[NUM_DSP_REGS + 1];
720         int err;
721
722         BUG_ON(count % sizeof(u64));
723
724         if (!cpu_has_dsp)
725                 return -EIO;
726
727         start = pos / sizeof(u64);
728         num_regs = count / sizeof(u64);
729
730         if (start + num_regs > NUM_DSP_REGS + 1)
731                 return -EIO;
732
733         err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, dspregs, 0,
734                                  sizeof(dspregs));
735         if (err)
736                 return err;
737
738         for (i = start; i < num_regs; i++)
739                 switch (i) {
740                 case 0 ... NUM_DSP_REGS - 1:
741                         target->thread.dsp.dspr[i] = dspregs[i];
742                         break;
743                 case NUM_DSP_REGS:
744                         target->thread.dsp.dspcontrol = dspregs[i];
745                         break;
746                 }
747
748         return 0;
749 }
750
751 #endif /* CONFIG_64BIT */
752
753 /*
754  * Determine whether the DSP context is present.
755  */
756 static int dsp_active(struct task_struct *target,
757                       const struct user_regset *regset)
758 {
759         return cpu_has_dsp ? NUM_DSP_REGS + 1 : -ENODEV;
760 }
761
762 enum mips_regset {
763         REGSET_GPR,
764         REGSET_FPR,
765         REGSET_DSP,
766 };
767
768 struct pt_regs_offset {
769         const char *name;
770         int offset;
771 };
772
773 #define REG_OFFSET_NAME(reg, r) {                                       \
774         .name = #reg,                                                   \
775         .offset = offsetof(struct pt_regs, r)                           \
776 }
777
778 #define REG_OFFSET_END {                                                \
779         .name = NULL,                                                   \
780         .offset = 0                                                     \
781 }
782
783 static const struct pt_regs_offset regoffset_table[] = {
784         REG_OFFSET_NAME(r0, regs[0]),
785         REG_OFFSET_NAME(r1, regs[1]),
786         REG_OFFSET_NAME(r2, regs[2]),
787         REG_OFFSET_NAME(r3, regs[3]),
788         REG_OFFSET_NAME(r4, regs[4]),
789         REG_OFFSET_NAME(r5, regs[5]),
790         REG_OFFSET_NAME(r6, regs[6]),
791         REG_OFFSET_NAME(r7, regs[7]),
792         REG_OFFSET_NAME(r8, regs[8]),
793         REG_OFFSET_NAME(r9, regs[9]),
794         REG_OFFSET_NAME(r10, regs[10]),
795         REG_OFFSET_NAME(r11, regs[11]),
796         REG_OFFSET_NAME(r12, regs[12]),
797         REG_OFFSET_NAME(r13, regs[13]),
798         REG_OFFSET_NAME(r14, regs[14]),
799         REG_OFFSET_NAME(r15, regs[15]),
800         REG_OFFSET_NAME(r16, regs[16]),
801         REG_OFFSET_NAME(r17, regs[17]),
802         REG_OFFSET_NAME(r18, regs[18]),
803         REG_OFFSET_NAME(r19, regs[19]),
804         REG_OFFSET_NAME(r20, regs[20]),
805         REG_OFFSET_NAME(r21, regs[21]),
806         REG_OFFSET_NAME(r22, regs[22]),
807         REG_OFFSET_NAME(r23, regs[23]),
808         REG_OFFSET_NAME(r24, regs[24]),
809         REG_OFFSET_NAME(r25, regs[25]),
810         REG_OFFSET_NAME(r26, regs[26]),
811         REG_OFFSET_NAME(r27, regs[27]),
812         REG_OFFSET_NAME(r28, regs[28]),
813         REG_OFFSET_NAME(r29, regs[29]),
814         REG_OFFSET_NAME(r30, regs[30]),
815         REG_OFFSET_NAME(r31, regs[31]),
816         REG_OFFSET_NAME(c0_status, cp0_status),
817         REG_OFFSET_NAME(hi, hi),
818         REG_OFFSET_NAME(lo, lo),
819 #ifdef CONFIG_CPU_HAS_SMARTMIPS
820         REG_OFFSET_NAME(acx, acx),
821 #endif
822         REG_OFFSET_NAME(c0_badvaddr, cp0_badvaddr),
823         REG_OFFSET_NAME(c0_cause, cp0_cause),
824         REG_OFFSET_NAME(c0_epc, cp0_epc),
825 #ifdef CONFIG_CPU_CAVIUM_OCTEON
826         REG_OFFSET_NAME(mpl0, mpl[0]),
827         REG_OFFSET_NAME(mpl1, mpl[1]),
828         REG_OFFSET_NAME(mpl2, mpl[2]),
829         REG_OFFSET_NAME(mtp0, mtp[0]),
830         REG_OFFSET_NAME(mtp1, mtp[1]),
831         REG_OFFSET_NAME(mtp2, mtp[2]),
832 #endif
833         REG_OFFSET_END,
834 };
835
836 /**
837  * regs_query_register_offset() - query register offset from its name
838  * @name:       the name of a register
839  *
840  * regs_query_register_offset() returns the offset of a register in struct
841  * pt_regs from its name. If the name is invalid, this returns -EINVAL;
842  */
843 int regs_query_register_offset(const char *name)
844 {
845         const struct pt_regs_offset *roff;
846         for (roff = regoffset_table; roff->name != NULL; roff++)
847                 if (!strcmp(roff->name, name))
848                         return roff->offset;
849         return -EINVAL;
850 }
851
852 #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
853
854 static const struct user_regset mips_regsets[] = {
855         [REGSET_GPR] = {
856                 .core_note_type = NT_PRSTATUS,
857                 .n              = ELF_NGREG,
858                 .size           = sizeof(unsigned int),
859                 .align          = sizeof(unsigned int),
860                 .get            = gpr32_get,
861                 .set            = gpr32_set,
862         },
863         [REGSET_FPR] = {
864                 .core_note_type = NT_PRFPREG,
865                 .n              = ELF_NFPREG,
866                 .size           = sizeof(elf_fpreg_t),
867                 .align          = sizeof(elf_fpreg_t),
868                 .get            = fpr_get,
869                 .set            = fpr_set,
870         },
871         [REGSET_DSP] = {
872                 .core_note_type = NT_MIPS_DSP,
873                 .n              = NUM_DSP_REGS + 1,
874                 .size           = sizeof(u32),
875                 .align          = sizeof(u32),
876                 .get            = dsp32_get,
877                 .set            = dsp32_set,
878                 .active         = dsp_active,
879         },
880 };
881
882 static const struct user_regset_view user_mips_view = {
883         .name           = "mips",
884         .e_machine      = ELF_ARCH,
885         .ei_osabi       = ELF_OSABI,
886         .regsets        = mips_regsets,
887         .n              = ARRAY_SIZE(mips_regsets),
888 };
889
890 #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
891
892 #ifdef CONFIG_64BIT
893
894 static const struct user_regset mips64_regsets[] = {
895         [REGSET_GPR] = {
896                 .core_note_type = NT_PRSTATUS,
897                 .n              = ELF_NGREG,
898                 .size           = sizeof(unsigned long),
899                 .align          = sizeof(unsigned long),
900                 .get            = gpr64_get,
901                 .set            = gpr64_set,
902         },
903         [REGSET_FPR] = {
904                 .core_note_type = NT_PRFPREG,
905                 .n              = ELF_NFPREG,
906                 .size           = sizeof(elf_fpreg_t),
907                 .align          = sizeof(elf_fpreg_t),
908                 .get            = fpr_get,
909                 .set            = fpr_set,
910         },
911         [REGSET_DSP] = {
912                 .core_note_type = NT_MIPS_DSP,
913                 .n              = NUM_DSP_REGS + 1,
914                 .size           = sizeof(u64),
915                 .align          = sizeof(u64),
916                 .get            = dsp64_get,
917                 .set            = dsp64_set,
918                 .active         = dsp_active,
919         },
920 };
921
922 static const struct user_regset_view user_mips64_view = {
923         .name           = "mips64",
924         .e_machine      = ELF_ARCH,
925         .ei_osabi       = ELF_OSABI,
926         .regsets        = mips64_regsets,
927         .n              = ARRAY_SIZE(mips64_regsets),
928 };
929
930 #ifdef CONFIG_MIPS32_N32
931
932 static const struct user_regset_view user_mipsn32_view = {
933         .name           = "mipsn32",
934         .e_flags        = EF_MIPS_ABI2,
935         .e_machine      = ELF_ARCH,
936         .ei_osabi       = ELF_OSABI,
937         .regsets        = mips64_regsets,
938         .n              = ARRAY_SIZE(mips64_regsets),
939 };
940
941 #endif /* CONFIG_MIPS32_N32 */
942
943 #endif /* CONFIG_64BIT */
944
945 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
946 {
947 #ifdef CONFIG_32BIT
948         return &user_mips_view;
949 #else
950 #ifdef CONFIG_MIPS32_O32
951         if (test_tsk_thread_flag(task, TIF_32BIT_REGS))
952                 return &user_mips_view;
953 #endif
954 #ifdef CONFIG_MIPS32_N32
955         if (test_tsk_thread_flag(task, TIF_32BIT_ADDR))
956                 return &user_mipsn32_view;
957 #endif
958         return &user_mips64_view;
959 #endif
960 }
961
962 long arch_ptrace(struct task_struct *child, long request,
963                  unsigned long addr, unsigned long data)
964 {
965         int ret;
966         void __user *addrp = (void __user *) addr;
967         void __user *datavp = (void __user *) data;
968         unsigned long __user *datalp = (void __user *) data;
969
970         switch (request) {
971         /* when I and D space are separate, these will need to be fixed. */
972         case PTRACE_PEEKTEXT: /* read word at location addr. */
973         case PTRACE_PEEKDATA:
974                 ret = generic_ptrace_peekdata(child, addr, data);
975                 break;
976
977         /* Read the word at location addr in the USER area. */
978         case PTRACE_PEEKUSR: {
979                 struct pt_regs *regs;
980                 union fpureg *fregs;
981                 unsigned long tmp = 0;
982
983                 regs = task_pt_regs(child);
984                 ret = 0;  /* Default return value. */
985
986                 switch (addr) {
987                 case 0 ... 31:
988                         tmp = regs->regs[addr];
989                         break;
990                 case FPR_BASE ... FPR_BASE + 31:
991                         if (!tsk_used_math(child)) {
992                                 /* FP not yet used */
993                                 tmp = -1;
994                                 break;
995                         }
996                         fregs = get_fpu_regs(child);
997
998 #ifdef CONFIG_32BIT
999                         if (test_tsk_thread_flag(child, TIF_32BIT_FPREGS)) {
1000                                 /*
1001                                  * The odd registers are actually the high
1002                                  * order bits of the values stored in the even
1003                                  * registers.
1004                                  */
1005                                 tmp = get_fpr32(&fregs[(addr & ~1) - FPR_BASE],
1006                                                 addr & 1);
1007                                 break;
1008                         }
1009 #endif
1010                         tmp = get_fpr64(&fregs[addr - FPR_BASE], 0);
1011                         break;
1012                 case PC:
1013                         tmp = regs->cp0_epc;
1014                         break;
1015                 case CAUSE:
1016                         tmp = regs->cp0_cause;
1017                         break;
1018                 case BADVADDR:
1019                         tmp = regs->cp0_badvaddr;
1020                         break;
1021                 case MMHI:
1022                         tmp = regs->hi;
1023                         break;
1024                 case MMLO:
1025                         tmp = regs->lo;
1026                         break;
1027 #ifdef CONFIG_CPU_HAS_SMARTMIPS
1028                 case ACX:
1029                         tmp = regs->acx;
1030                         break;
1031 #endif
1032                 case FPC_CSR:
1033                         tmp = child->thread.fpu.fcr31;
1034                         break;
1035                 case FPC_EIR:
1036                         /* implementation / version register */
1037                         tmp = boot_cpu_data.fpu_id;
1038                         break;
1039                 case DSP_BASE ... DSP_BASE + 5: {
1040                         dspreg_t *dregs;
1041
1042                         if (!cpu_has_dsp) {
1043                                 tmp = 0;
1044                                 ret = -EIO;
1045                                 goto out;
1046                         }
1047                         dregs = __get_dsp_regs(child);
1048                         tmp = dregs[addr - DSP_BASE];
1049                         break;
1050                 }
1051                 case DSP_CONTROL:
1052                         if (!cpu_has_dsp) {
1053                                 tmp = 0;
1054                                 ret = -EIO;
1055                                 goto out;
1056                         }
1057                         tmp = child->thread.dsp.dspcontrol;
1058                         break;
1059                 default:
1060                         tmp = 0;
1061                         ret = -EIO;
1062                         goto out;
1063                 }
1064                 ret = put_user(tmp, datalp);
1065                 break;
1066         }
1067
1068         /* when I and D space are separate, this will have to be fixed. */
1069         case PTRACE_POKETEXT: /* write the word at location addr. */
1070         case PTRACE_POKEDATA:
1071                 ret = generic_ptrace_pokedata(child, addr, data);
1072                 break;
1073
1074         case PTRACE_POKEUSR: {
1075                 struct pt_regs *regs;
1076                 ret = 0;
1077                 regs = task_pt_regs(child);
1078
1079                 switch (addr) {
1080                 case 0 ... 31:
1081                         regs->regs[addr] = data;
1082                         /* System call number may have been changed */
1083                         if (addr == 2)
1084                                 mips_syscall_update_nr(child, regs);
1085                         else if (addr == 4 &&
1086                                  mips_syscall_is_indirect(child, regs))
1087                                 mips_syscall_update_nr(child, regs);
1088                         break;
1089                 case FPR_BASE ... FPR_BASE + 31: {
1090                         union fpureg *fregs = get_fpu_regs(child);
1091
1092                         init_fp_ctx(child);
1093 #ifdef CONFIG_32BIT
1094                         if (test_tsk_thread_flag(child, TIF_32BIT_FPREGS)) {
1095                                 /*
1096                                  * The odd registers are actually the high
1097                                  * order bits of the values stored in the even
1098                                  * registers.
1099                                  */
1100                                 set_fpr32(&fregs[(addr & ~1) - FPR_BASE],
1101                                           addr & 1, data);
1102                                 break;
1103                         }
1104 #endif
1105                         set_fpr64(&fregs[addr - FPR_BASE], 0, data);
1106                         break;
1107                 }
1108                 case PC:
1109                         regs->cp0_epc = data;
1110                         break;
1111                 case MMHI:
1112                         regs->hi = data;
1113                         break;
1114                 case MMLO:
1115                         regs->lo = data;
1116                         break;
1117 #ifdef CONFIG_CPU_HAS_SMARTMIPS
1118                 case ACX:
1119                         regs->acx = data;
1120                         break;
1121 #endif
1122                 case FPC_CSR:
1123                         init_fp_ctx(child);
1124                         ptrace_setfcr31(child, data);
1125                         break;
1126                 case DSP_BASE ... DSP_BASE + 5: {
1127                         dspreg_t *dregs;
1128
1129                         if (!cpu_has_dsp) {
1130                                 ret = -EIO;
1131                                 break;
1132                         }
1133
1134                         dregs = __get_dsp_regs(child);
1135                         dregs[addr - DSP_BASE] = data;
1136                         break;
1137                 }
1138                 case DSP_CONTROL:
1139                         if (!cpu_has_dsp) {
1140                                 ret = -EIO;
1141                                 break;
1142                         }
1143                         child->thread.dsp.dspcontrol = data;
1144                         break;
1145                 default:
1146                         /* The rest are not allowed. */
1147                         ret = -EIO;
1148                         break;
1149                 }
1150                 break;
1151                 }
1152
1153         case PTRACE_GETREGS:
1154                 ret = ptrace_getregs(child, datavp);
1155                 break;
1156
1157         case PTRACE_SETREGS:
1158                 ret = ptrace_setregs(child, datavp);
1159                 break;
1160
1161         case PTRACE_GETFPREGS:
1162                 ret = ptrace_getfpregs(child, datavp);
1163                 break;
1164
1165         case PTRACE_SETFPREGS:
1166                 ret = ptrace_setfpregs(child, datavp);
1167                 break;
1168
1169         case PTRACE_GET_THREAD_AREA:
1170                 ret = put_user(task_thread_info(child)->tp_value, datalp);
1171                 break;
1172
1173         case PTRACE_GET_WATCH_REGS:
1174                 ret = ptrace_get_watch_regs(child, addrp);
1175                 break;
1176
1177         case PTRACE_SET_WATCH_REGS:
1178                 ret = ptrace_set_watch_regs(child, addrp);
1179                 break;
1180
1181         default:
1182                 ret = ptrace_request(child, request, addr, data);
1183                 break;
1184         }
1185  out:
1186         return ret;
1187 }
1188
1189 /*
1190  * Notification of system call entry/exit
1191  * - triggered by current->work.syscall_trace
1192  */
1193 asmlinkage long syscall_trace_enter(struct pt_regs *regs, long syscall)
1194 {
1195         user_exit();
1196
1197         current_thread_info()->syscall = syscall;
1198
1199         if (test_thread_flag(TIF_SYSCALL_TRACE)) {
1200                 if (tracehook_report_syscall_entry(regs))
1201                         return -1;
1202                 syscall = current_thread_info()->syscall;
1203         }
1204
1205 #ifdef CONFIG_SECCOMP
1206         if (unlikely(test_thread_flag(TIF_SECCOMP))) {
1207                 int ret, i;
1208                 struct seccomp_data sd;
1209                 unsigned long args[6];
1210
1211                 sd.nr = syscall;
1212                 sd.arch = syscall_get_arch();
1213                 syscall_get_arguments(current, regs, 0, 6, args);
1214                 for (i = 0; i < 6; i++)
1215                         sd.args[i] = args[i];
1216                 sd.instruction_pointer = KSTK_EIP(current);
1217
1218                 ret = __secure_computing(&sd);
1219                 if (ret == -1)
1220                         return ret;
1221                 syscall = current_thread_info()->syscall;
1222         }
1223 #endif
1224
1225         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1226                 trace_sys_enter(regs, regs->regs[2]);
1227
1228         audit_syscall_entry(syscall, regs->regs[4], regs->regs[5],
1229                             regs->regs[6], regs->regs[7]);
1230
1231         /*
1232          * Negative syscall numbers are mistaken for rejected syscalls, but
1233          * won't have had the return value set appropriately, so we do so now.
1234          */
1235         if (syscall < 0)
1236                 syscall_set_return_value(current, regs, -ENOSYS, 0);
1237         return syscall;
1238 }
1239
1240 /*
1241  * Notification of system call entry/exit
1242  * - triggered by current->work.syscall_trace
1243  */
1244 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1245 {
1246         /*
1247          * We may come here right after calling schedule_user()
1248          * or do_notify_resume(), in which case we can be in RCU
1249          * user mode.
1250          */
1251         user_exit();
1252
1253         audit_syscall_exit(regs);
1254
1255         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1256                 trace_sys_exit(regs, regs_return_value(regs));
1257
1258         if (test_thread_flag(TIF_SYSCALL_TRACE))
1259                 tracehook_report_syscall_exit(regs, 0);
1260
1261         user_enter();
1262 }