ptrace.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. #include <asm/byteorder.h>
  35. #include <asm/cpu.h>
  36. #include <asm/cpu-info.h>
  37. #include <asm/dsp.h>
  38. #include <asm/fpu.h>
  39. #include <asm/mipsregs.h>
  40. #include <asm/mipsmtregs.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/page.h>
  43. #include <asm/syscall.h>
  44. #include <linux/uaccess.h>
  45. #include <asm/bootinfo.h>
  46. #include <asm/reg.h>
  47. #define CREATE_TRACE_POINTS
  48. #include <trace/events/syscalls.h>
  49. static void init_fp_ctx(struct task_struct *target)
  50. {
  51. /* If FP has been used then the target already has context */
  52. if (tsk_used_math(target))
  53. return;
  54. /* Begin with data registers set to all 1s... */
  55. memset(&target->thread.fpu.fpr, ~0, sizeof(target->thread.fpu.fpr));
  56. /* FCSR has been preset by `mips_set_personality_nan'. */
  57. /*
  58. * Record that the target has "used" math, such that the context
  59. * just initialised, and any modifications made by the caller,
  60. * aren't discarded.
  61. */
  62. set_stopped_child_used_math(target);
  63. }
  64. /*
  65. * Called by kernel/ptrace.c when detaching..
  66. *
  67. * Make sure single step bits etc are not set.
  68. */
  69. void ptrace_disable(struct task_struct *child)
  70. {
  71. /* Don't load the watchpoint registers for the ex-child. */
  72. clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
  73. }
  74. /*
  75. * Poke at FCSR according to its mask. Set the Cause bits even
  76. * if a corresponding Enable bit is set. This will be noticed at
  77. * the time the thread is switched to and SIGFPE thrown accordingly.
  78. */
  79. static void ptrace_setfcr31(struct task_struct *child, u32 value)
  80. {
  81. u32 fcr31;
  82. u32 mask;
  83. fcr31 = child->thread.fpu.fcr31;
  84. mask = boot_cpu_data.fpu_msk31;
  85. child->thread.fpu.fcr31 = (value & ~mask) | (fcr31 & mask);
  86. }
  87. /*
  88. * Read a general register set. We always use the 64-bit format, even
  89. * for 32-bit kernels and for 32-bit processes on a 64-bit kernel.
  90. * Registers are sign extended to fill the available space.
  91. */
  92. int ptrace_getregs(struct task_struct *child, struct user_pt_regs __user *data)
  93. {
  94. struct pt_regs *regs;
  95. int i;
  96. if (!access_ok(VERIFY_WRITE, data, 38 * 8))
  97. return -EIO;
  98. regs = task_pt_regs(child);
  99. for (i = 0; i < 32; i++)
  100. __put_user((long)regs->regs[i], (__s64 __user *)&data->regs[i]);
  101. __put_user((long)regs->lo, (__s64 __user *)&data->lo);
  102. __put_user((long)regs->hi, (__s64 __user *)&data->hi);
  103. __put_user((long)regs->cp0_epc, (__s64 __user *)&data->cp0_epc);
  104. __put_user((long)regs->cp0_badvaddr, (__s64 __user *)&data->cp0_badvaddr);
  105. __put_user((long)regs->cp0_status, (__s64 __user *)&data->cp0_status);
  106. __put_user((long)regs->cp0_cause, (__s64 __user *)&data->cp0_cause);
  107. return 0;
  108. }
  109. /*
  110. * Write a general register set. As for PTRACE_GETREGS, we always use
  111. * the 64-bit format. On a 32-bit kernel only the lower order half
  112. * (according to endianness) will be used.
  113. */
  114. int ptrace_setregs(struct task_struct *child, struct user_pt_regs __user *data)
  115. {
  116. struct pt_regs *regs;
  117. int i;
  118. if (!access_ok(VERIFY_READ, data, 38 * 8))
  119. return -EIO;
  120. regs = task_pt_regs(child);
  121. for (i = 0; i < 32; i++)
  122. __get_user(regs->regs[i], (__s64 __user *)&data->regs[i]);
  123. __get_user(regs->lo, (__s64 __user *)&data->lo);
  124. __get_user(regs->hi, (__s64 __user *)&data->hi);
  125. __get_user(regs->cp0_epc, (__s64 __user *)&data->cp0_epc);
  126. /* badvaddr, status, and cause may not be written. */
  127. /* System call number may have been changed */
  128. mips_syscall_update_nr(child, regs);
  129. return 0;
  130. }
  131. int ptrace_getfpregs(struct task_struct *child, __u32 __user *data)
  132. {
  133. int i;
  134. if (!access_ok(VERIFY_WRITE, data, 33 * 8))
  135. return -EIO;
  136. if (tsk_used_math(child)) {
  137. union fpureg *fregs = get_fpu_regs(child);
  138. for (i = 0; i < 32; i++)
  139. __put_user(get_fpr64(&fregs[i], 0),
  140. i + (__u64 __user *)data);
  141. } else {
  142. for (i = 0; i < 32; i++)
  143. __put_user((__u64) -1, i + (__u64 __user *) data);
  144. }
  145. __put_user(child->thread.fpu.fcr31, data + 64);
  146. __put_user(boot_cpu_data.fpu_id, data + 65);
  147. return 0;
  148. }
  149. int ptrace_setfpregs(struct task_struct *child, __u32 __user *data)
  150. {
  151. union fpureg *fregs;
  152. u64 fpr_val;
  153. u32 value;
  154. int i;
  155. if (!access_ok(VERIFY_READ, data, 33 * 8))
  156. return -EIO;
  157. init_fp_ctx(child);
  158. fregs = get_fpu_regs(child);
  159. for (i = 0; i < 32; i++) {
  160. __get_user(fpr_val, i + (__u64 __user *)data);
  161. set_fpr64(&fregs[i], 0, fpr_val);
  162. }
  163. __get_user(value, data + 64);
  164. ptrace_setfcr31(child, value);
  165. /* FIR may not be written. */
  166. return 0;
  167. }
  168. int ptrace_get_watch_regs(struct task_struct *child,
  169. struct pt_watch_regs __user *addr)
  170. {
  171. enum pt_watch_style style;
  172. int i;
  173. if (!cpu_has_watch || boot_cpu_data.watch_reg_use_cnt == 0)
  174. return -EIO;
  175. if (!access_ok(VERIFY_WRITE, addr, sizeof(struct pt_watch_regs)))
  176. return -EIO;
  177. #ifdef CONFIG_32BIT
  178. style = pt_watch_style_mips32;
  179. #define WATCH_STYLE mips32
  180. #else
  181. style = pt_watch_style_mips64;
  182. #define WATCH_STYLE mips64
  183. #endif
  184. __put_user(style, &addr->style);
  185. __put_user(boot_cpu_data.watch_reg_use_cnt,
  186. &addr->WATCH_STYLE.num_valid);
  187. for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
  188. __put_user(child->thread.watch.mips3264.watchlo[i],
  189. &addr->WATCH_STYLE.watchlo[i]);
  190. __put_user(child->thread.watch.mips3264.watchhi[i] &
  191. (MIPS_WATCHHI_MASK | MIPS_WATCHHI_IRW),
  192. &addr->WATCH_STYLE.watchhi[i]);
  193. __put_user(boot_cpu_data.watch_reg_masks[i],
  194. &addr->WATCH_STYLE.watch_masks[i]);
  195. }
  196. for (; i < 8; i++) {
  197. __put_user(0, &addr->WATCH_STYLE.watchlo[i]);
  198. __put_user(0, &addr->WATCH_STYLE.watchhi[i]);
  199. __put_user(0, &addr->WATCH_STYLE.watch_masks[i]);
  200. }
  201. return 0;
  202. }
  203. int ptrace_set_watch_regs(struct task_struct *child,
  204. struct pt_watch_regs __user *addr)
  205. {
  206. int i;
  207. int watch_active = 0;
  208. unsigned long lt[NUM_WATCH_REGS];
  209. u16 ht[NUM_WATCH_REGS];
  210. if (!cpu_has_watch || boot_cpu_data.watch_reg_use_cnt == 0)
  211. return -EIO;
  212. if (!access_ok(VERIFY_READ, addr, sizeof(struct pt_watch_regs)))
  213. return -EIO;
  214. /* Check the values. */
  215. for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
  216. __get_user(lt[i], &addr->WATCH_STYLE.watchlo[i]);
  217. #ifdef CONFIG_32BIT
  218. if (lt[i] & __UA_LIMIT)
  219. return -EINVAL;
  220. #else
  221. if (test_tsk_thread_flag(child, TIF_32BIT_ADDR)) {
  222. if (lt[i] & 0xffffffff80000000UL)
  223. return -EINVAL;
  224. } else {
  225. if (lt[i] & __UA_LIMIT)
  226. return -EINVAL;
  227. }
  228. #endif
  229. __get_user(ht[i], &addr->WATCH_STYLE.watchhi[i]);
  230. if (ht[i] & ~MIPS_WATCHHI_MASK)
  231. return -EINVAL;
  232. }
  233. /* Install them. */
  234. for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
  235. if (lt[i] & MIPS_WATCHLO_IRW)
  236. watch_active = 1;
  237. child->thread.watch.mips3264.watchlo[i] = lt[i];
  238. /* Set the G bit. */
  239. child->thread.watch.mips3264.watchhi[i] = ht[i];
  240. }
  241. if (watch_active)
  242. set_tsk_thread_flag(child, TIF_LOAD_WATCH);
  243. else
  244. clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
  245. return 0;
  246. }
  247. /* regset get/set implementations */
  248. #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
  249. static int gpr32_get(struct task_struct *target,
  250. const struct user_regset *regset,
  251. unsigned int pos, unsigned int count,
  252. void *kbuf, void __user *ubuf)
  253. {
  254. struct pt_regs *regs = task_pt_regs(target);
  255. u32 uregs[ELF_NGREG] = {};
  256. mips_dump_regs32(uregs, regs);
  257. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
  258. sizeof(uregs));
  259. }
  260. static int gpr32_set(struct task_struct *target,
  261. const struct user_regset *regset,
  262. unsigned int pos, unsigned int count,
  263. const void *kbuf, const void __user *ubuf)
  264. {
  265. struct pt_regs *regs = task_pt_regs(target);
  266. u32 uregs[ELF_NGREG];
  267. unsigned start, num_regs, i;
  268. int err;
  269. start = pos / sizeof(u32);
  270. num_regs = count / sizeof(u32);
  271. if (start + num_regs > ELF_NGREG)
  272. return -EIO;
  273. err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
  274. sizeof(uregs));
  275. if (err)
  276. return err;
  277. for (i = start; i < num_regs; i++) {
  278. /*
  279. * Cast all values to signed here so that if this is a 64-bit
  280. * kernel, the supplied 32-bit values will be sign extended.
  281. */
  282. switch (i) {
  283. case MIPS32_EF_R1 ... MIPS32_EF_R25:
  284. /* k0/k1 are ignored. */
  285. case MIPS32_EF_R28 ... MIPS32_EF_R31:
  286. regs->regs[i - MIPS32_EF_R0] = (s32)uregs[i];
  287. break;
  288. case MIPS32_EF_LO:
  289. regs->lo = (s32)uregs[i];
  290. break;
  291. case MIPS32_EF_HI:
  292. regs->hi = (s32)uregs[i];
  293. break;
  294. case MIPS32_EF_CP0_EPC:
  295. regs->cp0_epc = (s32)uregs[i];
  296. break;
  297. }
  298. }
  299. /* System call number may have been changed */
  300. mips_syscall_update_nr(target, regs);
  301. return 0;
  302. }
  303. #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
  304. #ifdef CONFIG_64BIT
  305. static int gpr64_get(struct task_struct *target,
  306. const struct user_regset *regset,
  307. unsigned int pos, unsigned int count,
  308. void *kbuf, void __user *ubuf)
  309. {
  310. struct pt_regs *regs = task_pt_regs(target);
  311. u64 uregs[ELF_NGREG] = {};
  312. mips_dump_regs64(uregs, regs);
  313. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
  314. sizeof(uregs));
  315. }
  316. static int gpr64_set(struct task_struct *target,
  317. const struct user_regset *regset,
  318. unsigned int pos, unsigned int count,
  319. const void *kbuf, const void __user *ubuf)
  320. {
  321. struct pt_regs *regs = task_pt_regs(target);
  322. u64 uregs[ELF_NGREG];
  323. unsigned start, num_regs, i;
  324. int err;
  325. start = pos / sizeof(u64);
  326. num_regs = count / sizeof(u64);
  327. if (start + num_regs > ELF_NGREG)
  328. return -EIO;
  329. err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
  330. sizeof(uregs));
  331. if (err)
  332. return err;
  333. for (i = start; i < num_regs; i++) {
  334. switch (i) {
  335. case MIPS64_EF_R1 ... MIPS64_EF_R25:
  336. /* k0/k1 are ignored. */
  337. case MIPS64_EF_R28 ... MIPS64_EF_R31:
  338. regs->regs[i - MIPS64_EF_R0] = uregs[i];
  339. break;
  340. case MIPS64_EF_LO:
  341. regs->lo = uregs[i];
  342. break;
  343. case MIPS64_EF_HI:
  344. regs->hi = uregs[i];
  345. break;
  346. case MIPS64_EF_CP0_EPC:
  347. regs->cp0_epc = uregs[i];
  348. break;
  349. }
  350. }
  351. /* System call number may have been changed */
  352. mips_syscall_update_nr(target, regs);
  353. return 0;
  354. }
  355. #endif /* CONFIG_64BIT */
  356. static int fpr_get(struct task_struct *target,
  357. const struct user_regset *regset,
  358. unsigned int pos, unsigned int count,
  359. void *kbuf, void __user *ubuf)
  360. {
  361. unsigned i;
  362. int err;
  363. u64 fpr_val;
  364. /* XXX fcr31 */
  365. if (sizeof(target->thread.fpu.fpr[i]) == sizeof(elf_fpreg_t))
  366. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  367. &target->thread.fpu,
  368. 0, sizeof(elf_fpregset_t));
  369. for (i = 0; i < NUM_FPU_REGS; i++) {
  370. fpr_val = get_fpr64(&target->thread.fpu.fpr[i], 0);
  371. err = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  372. &fpr_val, i * sizeof(elf_fpreg_t),
  373. (i + 1) * sizeof(elf_fpreg_t));
  374. if (err)
  375. return err;
  376. }
  377. return 0;
  378. }
  379. static int fpr_set(struct task_struct *target,
  380. const struct user_regset *regset,
  381. unsigned int pos, unsigned int count,
  382. const void *kbuf, const void __user *ubuf)
  383. {
  384. unsigned i;
  385. int err;
  386. u64 fpr_val;
  387. /* XXX fcr31 */
  388. init_fp_ctx(target);
  389. if (sizeof(target->thread.fpu.fpr[i]) == sizeof(elf_fpreg_t))
  390. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  391. &target->thread.fpu,
  392. 0, sizeof(elf_fpregset_t));
  393. BUILD_BUG_ON(sizeof(fpr_val) != sizeof(elf_fpreg_t));
  394. for (i = 0; i < NUM_FPU_REGS && count >= sizeof(elf_fpreg_t); i++) {
  395. err = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  396. &fpr_val, i * sizeof(elf_fpreg_t),
  397. (i + 1) * sizeof(elf_fpreg_t));
  398. if (err)
  399. return err;
  400. set_fpr64(&target->thread.fpu.fpr[i], 0, fpr_val);
  401. }
  402. return 0;
  403. }
  404. enum mips_regset {
  405. REGSET_GPR,
  406. REGSET_FPR,
  407. };
  408. struct pt_regs_offset {
  409. const char *name;
  410. int offset;
  411. };
  412. #define REG_OFFSET_NAME(reg, r) { \
  413. .name = #reg, \
  414. .offset = offsetof(struct pt_regs, r) \
  415. }
  416. #define REG_OFFSET_END { \
  417. .name = NULL, \
  418. .offset = 0 \
  419. }
  420. static const struct pt_regs_offset regoffset_table[] = {
  421. REG_OFFSET_NAME(r0, regs[0]),
  422. REG_OFFSET_NAME(r1, regs[1]),
  423. REG_OFFSET_NAME(r2, regs[2]),
  424. REG_OFFSET_NAME(r3, regs[3]),
  425. REG_OFFSET_NAME(r4, regs[4]),
  426. REG_OFFSET_NAME(r5, regs[5]),
  427. REG_OFFSET_NAME(r6, regs[6]),
  428. REG_OFFSET_NAME(r7, regs[7]),
  429. REG_OFFSET_NAME(r8, regs[8]),
  430. REG_OFFSET_NAME(r9, regs[9]),
  431. REG_OFFSET_NAME(r10, regs[10]),
  432. REG_OFFSET_NAME(r11, regs[11]),
  433. REG_OFFSET_NAME(r12, regs[12]),
  434. REG_OFFSET_NAME(r13, regs[13]),
  435. REG_OFFSET_NAME(r14, regs[14]),
  436. REG_OFFSET_NAME(r15, regs[15]),
  437. REG_OFFSET_NAME(r16, regs[16]),
  438. REG_OFFSET_NAME(r17, regs[17]),
  439. REG_OFFSET_NAME(r18, regs[18]),
  440. REG_OFFSET_NAME(r19, regs[19]),
  441. REG_OFFSET_NAME(r20, regs[20]),
  442. REG_OFFSET_NAME(r21, regs[21]),
  443. REG_OFFSET_NAME(r22, regs[22]),
  444. REG_OFFSET_NAME(r23, regs[23]),
  445. REG_OFFSET_NAME(r24, regs[24]),
  446. REG_OFFSET_NAME(r25, regs[25]),
  447. REG_OFFSET_NAME(r26, regs[26]),
  448. REG_OFFSET_NAME(r27, regs[27]),
  449. REG_OFFSET_NAME(r28, regs[28]),
  450. REG_OFFSET_NAME(r29, regs[29]),
  451. REG_OFFSET_NAME(r30, regs[30]),
  452. REG_OFFSET_NAME(r31, regs[31]),
  453. REG_OFFSET_NAME(c0_status, cp0_status),
  454. REG_OFFSET_NAME(hi, hi),
  455. REG_OFFSET_NAME(lo, lo),
  456. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  457. REG_OFFSET_NAME(acx, acx),
  458. #endif
  459. REG_OFFSET_NAME(c0_badvaddr, cp0_badvaddr),
  460. REG_OFFSET_NAME(c0_cause, cp0_cause),
  461. REG_OFFSET_NAME(c0_epc, cp0_epc),
  462. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  463. REG_OFFSET_NAME(mpl0, mpl[0]),
  464. REG_OFFSET_NAME(mpl1, mpl[1]),
  465. REG_OFFSET_NAME(mpl2, mpl[2]),
  466. REG_OFFSET_NAME(mtp0, mtp[0]),
  467. REG_OFFSET_NAME(mtp1, mtp[1]),
  468. REG_OFFSET_NAME(mtp2, mtp[2]),
  469. #endif
  470. REG_OFFSET_END,
  471. };
  472. /**
  473. * regs_query_register_offset() - query register offset from its name
  474. * @name: the name of a register
  475. *
  476. * regs_query_register_offset() returns the offset of a register in struct
  477. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  478. */
  479. int regs_query_register_offset(const char *name)
  480. {
  481. const struct pt_regs_offset *roff;
  482. for (roff = regoffset_table; roff->name != NULL; roff++)
  483. if (!strcmp(roff->name, name))
  484. return roff->offset;
  485. return -EINVAL;
  486. }
  487. #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
  488. static const struct user_regset mips_regsets[] = {
  489. [REGSET_GPR] = {
  490. .core_note_type = NT_PRSTATUS,
  491. .n = ELF_NGREG,
  492. .size = sizeof(unsigned int),
  493. .align = sizeof(unsigned int),
  494. .get = gpr32_get,
  495. .set = gpr32_set,
  496. },
  497. [REGSET_FPR] = {
  498. .core_note_type = NT_PRFPREG,
  499. .n = ELF_NFPREG,
  500. .size = sizeof(elf_fpreg_t),
  501. .align = sizeof(elf_fpreg_t),
  502. .get = fpr_get,
  503. .set = fpr_set,
  504. },
  505. };
  506. static const struct user_regset_view user_mips_view = {
  507. .name = "mips",
  508. .e_machine = ELF_ARCH,
  509. .ei_osabi = ELF_OSABI,
  510. .regsets = mips_regsets,
  511. .n = ARRAY_SIZE(mips_regsets),
  512. };
  513. #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
  514. #ifdef CONFIG_64BIT
  515. static const struct user_regset mips64_regsets[] = {
  516. [REGSET_GPR] = {
  517. .core_note_type = NT_PRSTATUS,
  518. .n = ELF_NGREG,
  519. .size = sizeof(unsigned long),
  520. .align = sizeof(unsigned long),
  521. .get = gpr64_get,
  522. .set = gpr64_set,
  523. },
  524. [REGSET_FPR] = {
  525. .core_note_type = NT_PRFPREG,
  526. .n = ELF_NFPREG,
  527. .size = sizeof(elf_fpreg_t),
  528. .align = sizeof(elf_fpreg_t),
  529. .get = fpr_get,
  530. .set = fpr_set,
  531. },
  532. };
  533. static const struct user_regset_view user_mips64_view = {
  534. .name = "mips64",
  535. .e_machine = ELF_ARCH,
  536. .ei_osabi = ELF_OSABI,
  537. .regsets = mips64_regsets,
  538. .n = ARRAY_SIZE(mips64_regsets),
  539. };
  540. #ifdef CONFIG_MIPS32_N32
  541. static const struct user_regset_view user_mipsn32_view = {
  542. .name = "mipsn32",
  543. .e_flags = EF_MIPS_ABI2,
  544. .e_machine = ELF_ARCH,
  545. .ei_osabi = ELF_OSABI,
  546. .regsets = mips64_regsets,
  547. .n = ARRAY_SIZE(mips64_regsets),
  548. };
  549. #endif /* CONFIG_MIPS32_N32 */
  550. #endif /* CONFIG_64BIT */
  551. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  552. {
  553. #ifdef CONFIG_32BIT
  554. return &user_mips_view;
  555. #else
  556. #ifdef CONFIG_MIPS32_O32
  557. if (test_tsk_thread_flag(task, TIF_32BIT_REGS))
  558. return &user_mips_view;
  559. #endif
  560. #ifdef CONFIG_MIPS32_N32
  561. if (test_tsk_thread_flag(task, TIF_32BIT_ADDR))
  562. return &user_mipsn32_view;
  563. #endif
  564. return &user_mips64_view;
  565. #endif
  566. }
  567. long arch_ptrace(struct task_struct *child, long request,
  568. unsigned long addr, unsigned long data)
  569. {
  570. int ret;
  571. void __user *addrp = (void __user *) addr;
  572. void __user *datavp = (void __user *) data;
  573. unsigned long __user *datalp = (void __user *) data;
  574. switch (request) {
  575. /* when I and D space are separate, these will need to be fixed. */
  576. case PTRACE_PEEKTEXT: /* read word at location addr. */
  577. case PTRACE_PEEKDATA:
  578. ret = generic_ptrace_peekdata(child, addr, data);
  579. break;
  580. /* Read the word at location addr in the USER area. */
  581. case PTRACE_PEEKUSR: {
  582. struct pt_regs *regs;
  583. union fpureg *fregs;
  584. unsigned long tmp = 0;
  585. regs = task_pt_regs(child);
  586. ret = 0; /* Default return value. */
  587. switch (addr) {
  588. case 0 ... 31:
  589. tmp = regs->regs[addr];
  590. break;
  591. case FPR_BASE ... FPR_BASE + 31:
  592. if (!tsk_used_math(child)) {
  593. /* FP not yet used */
  594. tmp = -1;
  595. break;
  596. }
  597. fregs = get_fpu_regs(child);
  598. #ifdef CONFIG_32BIT
  599. if (test_thread_flag(TIF_32BIT_FPREGS)) {
  600. /*
  601. * The odd registers are actually the high
  602. * order bits of the values stored in the even
  603. * registers - unless we're using r2k_switch.S.
  604. */
  605. tmp = get_fpr32(&fregs[(addr & ~1) - FPR_BASE],
  606. addr & 1);
  607. break;
  608. }
  609. #endif
  610. tmp = get_fpr32(&fregs[addr - FPR_BASE], 0);
  611. break;
  612. case PC:
  613. tmp = regs->cp0_epc;
  614. break;
  615. case CAUSE:
  616. tmp = regs->cp0_cause;
  617. break;
  618. case BADVADDR:
  619. tmp = regs->cp0_badvaddr;
  620. break;
  621. case MMHI:
  622. tmp = regs->hi;
  623. break;
  624. case MMLO:
  625. tmp = regs->lo;
  626. break;
  627. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  628. case ACX:
  629. tmp = regs->acx;
  630. break;
  631. #endif
  632. case FPC_CSR:
  633. tmp = child->thread.fpu.fcr31;
  634. break;
  635. case FPC_EIR:
  636. /* implementation / version register */
  637. tmp = boot_cpu_data.fpu_id;
  638. break;
  639. case DSP_BASE ... DSP_BASE + 5: {
  640. dspreg_t *dregs;
  641. if (!cpu_has_dsp) {
  642. tmp = 0;
  643. ret = -EIO;
  644. goto out;
  645. }
  646. dregs = __get_dsp_regs(child);
  647. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  648. break;
  649. }
  650. case DSP_CONTROL:
  651. if (!cpu_has_dsp) {
  652. tmp = 0;
  653. ret = -EIO;
  654. goto out;
  655. }
  656. tmp = child->thread.dsp.dspcontrol;
  657. break;
  658. default:
  659. tmp = 0;
  660. ret = -EIO;
  661. goto out;
  662. }
  663. ret = put_user(tmp, datalp);
  664. break;
  665. }
  666. /* when I and D space are separate, this will have to be fixed. */
  667. case PTRACE_POKETEXT: /* write the word at location addr. */
  668. case PTRACE_POKEDATA:
  669. ret = generic_ptrace_pokedata(child, addr, data);
  670. break;
  671. case PTRACE_POKEUSR: {
  672. struct pt_regs *regs;
  673. ret = 0;
  674. regs = task_pt_regs(child);
  675. switch (addr) {
  676. case 0 ... 31:
  677. regs->regs[addr] = data;
  678. /* System call number may have been changed */
  679. if (addr == 2)
  680. mips_syscall_update_nr(child, regs);
  681. else if (addr == 4 &&
  682. mips_syscall_is_indirect(child, regs))
  683. mips_syscall_update_nr(child, regs);
  684. break;
  685. case FPR_BASE ... FPR_BASE + 31: {
  686. union fpureg *fregs = get_fpu_regs(child);
  687. init_fp_ctx(child);
  688. #ifdef CONFIG_32BIT
  689. if (test_thread_flag(TIF_32BIT_FPREGS)) {
  690. /*
  691. * The odd registers are actually the high
  692. * order bits of the values stored in the even
  693. * registers - unless we're using r2k_switch.S.
  694. */
  695. set_fpr32(&fregs[(addr & ~1) - FPR_BASE],
  696. addr & 1, data);
  697. break;
  698. }
  699. #endif
  700. set_fpr64(&fregs[addr - FPR_BASE], 0, data);
  701. break;
  702. }
  703. case PC:
  704. regs->cp0_epc = data;
  705. break;
  706. case MMHI:
  707. regs->hi = data;
  708. break;
  709. case MMLO:
  710. regs->lo = data;
  711. break;
  712. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  713. case ACX:
  714. regs->acx = data;
  715. break;
  716. #endif
  717. case FPC_CSR:
  718. init_fp_ctx(child);
  719. ptrace_setfcr31(child, data);
  720. break;
  721. case DSP_BASE ... DSP_BASE + 5: {
  722. dspreg_t *dregs;
  723. if (!cpu_has_dsp) {
  724. ret = -EIO;
  725. break;
  726. }
  727. dregs = __get_dsp_regs(child);
  728. dregs[addr - DSP_BASE] = data;
  729. break;
  730. }
  731. case DSP_CONTROL:
  732. if (!cpu_has_dsp) {
  733. ret = -EIO;
  734. break;
  735. }
  736. child->thread.dsp.dspcontrol = data;
  737. break;
  738. default:
  739. /* The rest are not allowed. */
  740. ret = -EIO;
  741. break;
  742. }
  743. break;
  744. }
  745. case PTRACE_GETREGS:
  746. ret = ptrace_getregs(child, datavp);
  747. break;
  748. case PTRACE_SETREGS:
  749. ret = ptrace_setregs(child, datavp);
  750. break;
  751. case PTRACE_GETFPREGS:
  752. ret = ptrace_getfpregs(child, datavp);
  753. break;
  754. case PTRACE_SETFPREGS:
  755. ret = ptrace_setfpregs(child, datavp);
  756. break;
  757. case PTRACE_GET_THREAD_AREA:
  758. ret = put_user(task_thread_info(child)->tp_value, datalp);
  759. break;
  760. case PTRACE_GET_WATCH_REGS:
  761. ret = ptrace_get_watch_regs(child, addrp);
  762. break;
  763. case PTRACE_SET_WATCH_REGS:
  764. ret = ptrace_set_watch_regs(child, addrp);
  765. break;
  766. default:
  767. ret = ptrace_request(child, request, addr, data);
  768. break;
  769. }
  770. out:
  771. return ret;
  772. }
  773. /*
  774. * Notification of system call entry/exit
  775. * - triggered by current->work.syscall_trace
  776. */
  777. asmlinkage long syscall_trace_enter(struct pt_regs *regs, long syscall)
  778. {
  779. user_exit();
  780. current_thread_info()->syscall = syscall;
  781. if (test_thread_flag(TIF_SYSCALL_TRACE)) {
  782. if (tracehook_report_syscall_entry(regs))
  783. return -1;
  784. syscall = current_thread_info()->syscall;
  785. }
  786. #ifdef CONFIG_SECCOMP
  787. if (unlikely(test_thread_flag(TIF_SECCOMP))) {
  788. int ret, i;
  789. struct seccomp_data sd;
  790. unsigned long args[6];
  791. sd.nr = syscall;
  792. sd.arch = syscall_get_arch();
  793. syscall_get_arguments(current, regs, 0, 6, args);
  794. for (i = 0; i < 6; i++)
  795. sd.args[i] = args[i];
  796. sd.instruction_pointer = KSTK_EIP(current);
  797. ret = __secure_computing(&sd);
  798. if (ret == -1)
  799. return ret;
  800. syscall = current_thread_info()->syscall;
  801. }
  802. #endif
  803. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  804. trace_sys_enter(regs, regs->regs[2]);
  805. audit_syscall_entry(syscall, regs->regs[4], regs->regs[5],
  806. regs->regs[6], regs->regs[7]);
  807. /*
  808. * Negative syscall numbers are mistaken for rejected syscalls, but
  809. * won't have had the return value set appropriately, so we do so now.
  810. */
  811. if (syscall < 0)
  812. syscall_set_return_value(current, regs, -ENOSYS, 0);
  813. return syscall;
  814. }
  815. /*
  816. * Notification of system call entry/exit
  817. * - triggered by current->work.syscall_trace
  818. */
  819. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  820. {
  821. /*
  822. * We may come here right after calling schedule_user()
  823. * or do_notify_resume(), in which case we can be in RCU
  824. * user mode.
  825. */
  826. user_exit();
  827. audit_syscall_exit(regs);
  828. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  829. trace_sys_exit(regs, regs_return_value(regs));
  830. if (test_thread_flag(TIF_SYSCALL_TRACE))
  831. tracehook_report_syscall_exit(regs, 0);
  832. user_enter();
  833. }