ptrace.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. /*
  357. * Copy the floating-point context to the supplied NT_PRFPREG buffer,
  358. * !CONFIG_CPU_HAS_MSA variant. FP context's general register slots
  359. * correspond 1:1 to buffer slots. Only general registers are copied.
  360. */
  361. static int fpr_get_fpa(struct task_struct *target,
  362. unsigned int *pos, unsigned int *count,
  363. void **kbuf, void __user **ubuf)
  364. {
  365. return user_regset_copyout(pos, count, kbuf, ubuf,
  366. &target->thread.fpu,
  367. 0, NUM_FPU_REGS * sizeof(elf_fpreg_t));
  368. }
  369. /*
  370. * Copy the floating-point context to the supplied NT_PRFPREG buffer,
  371. * CONFIG_CPU_HAS_MSA variant. Only lower 64 bits of FP context's
  372. * general register slots are copied to buffer slots. Only general
  373. * registers are copied.
  374. */
  375. static int fpr_get_msa(struct task_struct *target,
  376. unsigned int *pos, unsigned int *count,
  377. void **kbuf, void __user **ubuf)
  378. {
  379. unsigned int i;
  380. u64 fpr_val;
  381. int err;
  382. BUILD_BUG_ON(sizeof(fpr_val) != sizeof(elf_fpreg_t));
  383. for (i = 0; i < NUM_FPU_REGS; i++) {
  384. fpr_val = get_fpr64(&target->thread.fpu.fpr[i], 0);
  385. err = user_regset_copyout(pos, count, kbuf, ubuf,
  386. &fpr_val, i * sizeof(elf_fpreg_t),
  387. (i + 1) * sizeof(elf_fpreg_t));
  388. if (err)
  389. return err;
  390. }
  391. return 0;
  392. }
  393. /*
  394. * Copy the floating-point context to the supplied NT_PRFPREG buffer.
  395. * Choose the appropriate helper for general registers, and then copy
  396. * the FCSR register separately.
  397. */
  398. static int fpr_get(struct task_struct *target,
  399. const struct user_regset *regset,
  400. unsigned int pos, unsigned int count,
  401. void *kbuf, void __user *ubuf)
  402. {
  403. const int fcr31_pos = NUM_FPU_REGS * sizeof(elf_fpreg_t);
  404. int err;
  405. if (sizeof(target->thread.fpu.fpr[0]) == sizeof(elf_fpreg_t))
  406. err = fpr_get_fpa(target, &pos, &count, &kbuf, &ubuf);
  407. else
  408. err = fpr_get_msa(target, &pos, &count, &kbuf, &ubuf);
  409. if (err)
  410. return err;
  411. err = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  412. &target->thread.fpu.fcr31,
  413. fcr31_pos, fcr31_pos + sizeof(u32));
  414. return err;
  415. }
  416. /*
  417. * Copy the supplied NT_PRFPREG buffer to the floating-point context,
  418. * !CONFIG_CPU_HAS_MSA variant. Buffer slots correspond 1:1 to FP
  419. * context's general register slots. Only general registers are copied.
  420. */
  421. static int fpr_set_fpa(struct task_struct *target,
  422. unsigned int *pos, unsigned int *count,
  423. const void **kbuf, const void __user **ubuf)
  424. {
  425. return user_regset_copyin(pos, count, kbuf, ubuf,
  426. &target->thread.fpu,
  427. 0, NUM_FPU_REGS * sizeof(elf_fpreg_t));
  428. }
  429. /*
  430. * Copy the supplied NT_PRFPREG buffer to the floating-point context,
  431. * CONFIG_CPU_HAS_MSA variant. Buffer slots are copied to lower 64
  432. * bits only of FP context's general register slots. Only general
  433. * registers are copied.
  434. */
  435. static int fpr_set_msa(struct task_struct *target,
  436. unsigned int *pos, unsigned int *count,
  437. const void **kbuf, const void __user **ubuf)
  438. {
  439. unsigned int i;
  440. u64 fpr_val;
  441. int err;
  442. BUILD_BUG_ON(sizeof(fpr_val) != sizeof(elf_fpreg_t));
  443. for (i = 0; i < NUM_FPU_REGS && *count > 0; i++) {
  444. err = user_regset_copyin(pos, count, kbuf, ubuf,
  445. &fpr_val, i * sizeof(elf_fpreg_t),
  446. (i + 1) * sizeof(elf_fpreg_t));
  447. if (err)
  448. return err;
  449. set_fpr64(&target->thread.fpu.fpr[i], 0, fpr_val);
  450. }
  451. return 0;
  452. }
  453. /*
  454. * Copy the supplied NT_PRFPREG buffer to the floating-point context.
  455. * Choose the appropriate helper for general registers, and then copy
  456. * the FCSR register separately.
  457. *
  458. * We optimize for the case where `count % sizeof(elf_fpreg_t) == 0',
  459. * which is supposed to have been guaranteed by the kernel before
  460. * calling us, e.g. in `ptrace_regset'. We enforce that requirement,
  461. * so that we can safely avoid preinitializing temporaries for
  462. * partial register writes.
  463. */
  464. static int fpr_set(struct task_struct *target,
  465. const struct user_regset *regset,
  466. unsigned int pos, unsigned int count,
  467. const void *kbuf, const void __user *ubuf)
  468. {
  469. const int fcr31_pos = NUM_FPU_REGS * sizeof(elf_fpreg_t);
  470. u32 fcr31;
  471. int err;
  472. BUG_ON(count % sizeof(elf_fpreg_t));
  473. if (pos + count > sizeof(elf_fpregset_t))
  474. return -EIO;
  475. init_fp_ctx(target);
  476. if (sizeof(target->thread.fpu.fpr[0]) == sizeof(elf_fpreg_t))
  477. err = fpr_set_fpa(target, &pos, &count, &kbuf, &ubuf);
  478. else
  479. err = fpr_set_msa(target, &pos, &count, &kbuf, &ubuf);
  480. if (err)
  481. return err;
  482. if (count > 0) {
  483. err = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  484. &fcr31,
  485. fcr31_pos, fcr31_pos + sizeof(u32));
  486. if (err)
  487. return err;
  488. ptrace_setfcr31(target, fcr31);
  489. }
  490. return err;
  491. }
  492. enum mips_regset {
  493. REGSET_GPR,
  494. REGSET_FPR,
  495. };
  496. struct pt_regs_offset {
  497. const char *name;
  498. int offset;
  499. };
  500. #define REG_OFFSET_NAME(reg, r) { \
  501. .name = #reg, \
  502. .offset = offsetof(struct pt_regs, r) \
  503. }
  504. #define REG_OFFSET_END { \
  505. .name = NULL, \
  506. .offset = 0 \
  507. }
  508. static const struct pt_regs_offset regoffset_table[] = {
  509. REG_OFFSET_NAME(r0, regs[0]),
  510. REG_OFFSET_NAME(r1, regs[1]),
  511. REG_OFFSET_NAME(r2, regs[2]),
  512. REG_OFFSET_NAME(r3, regs[3]),
  513. REG_OFFSET_NAME(r4, regs[4]),
  514. REG_OFFSET_NAME(r5, regs[5]),
  515. REG_OFFSET_NAME(r6, regs[6]),
  516. REG_OFFSET_NAME(r7, regs[7]),
  517. REG_OFFSET_NAME(r8, regs[8]),
  518. REG_OFFSET_NAME(r9, regs[9]),
  519. REG_OFFSET_NAME(r10, regs[10]),
  520. REG_OFFSET_NAME(r11, regs[11]),
  521. REG_OFFSET_NAME(r12, regs[12]),
  522. REG_OFFSET_NAME(r13, regs[13]),
  523. REG_OFFSET_NAME(r14, regs[14]),
  524. REG_OFFSET_NAME(r15, regs[15]),
  525. REG_OFFSET_NAME(r16, regs[16]),
  526. REG_OFFSET_NAME(r17, regs[17]),
  527. REG_OFFSET_NAME(r18, regs[18]),
  528. REG_OFFSET_NAME(r19, regs[19]),
  529. REG_OFFSET_NAME(r20, regs[20]),
  530. REG_OFFSET_NAME(r21, regs[21]),
  531. REG_OFFSET_NAME(r22, regs[22]),
  532. REG_OFFSET_NAME(r23, regs[23]),
  533. REG_OFFSET_NAME(r24, regs[24]),
  534. REG_OFFSET_NAME(r25, regs[25]),
  535. REG_OFFSET_NAME(r26, regs[26]),
  536. REG_OFFSET_NAME(r27, regs[27]),
  537. REG_OFFSET_NAME(r28, regs[28]),
  538. REG_OFFSET_NAME(r29, regs[29]),
  539. REG_OFFSET_NAME(r30, regs[30]),
  540. REG_OFFSET_NAME(r31, regs[31]),
  541. REG_OFFSET_NAME(c0_status, cp0_status),
  542. REG_OFFSET_NAME(hi, hi),
  543. REG_OFFSET_NAME(lo, lo),
  544. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  545. REG_OFFSET_NAME(acx, acx),
  546. #endif
  547. REG_OFFSET_NAME(c0_badvaddr, cp0_badvaddr),
  548. REG_OFFSET_NAME(c0_cause, cp0_cause),
  549. REG_OFFSET_NAME(c0_epc, cp0_epc),
  550. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  551. REG_OFFSET_NAME(mpl0, mpl[0]),
  552. REG_OFFSET_NAME(mpl1, mpl[1]),
  553. REG_OFFSET_NAME(mpl2, mpl[2]),
  554. REG_OFFSET_NAME(mtp0, mtp[0]),
  555. REG_OFFSET_NAME(mtp1, mtp[1]),
  556. REG_OFFSET_NAME(mtp2, mtp[2]),
  557. #endif
  558. REG_OFFSET_END,
  559. };
  560. /**
  561. * regs_query_register_offset() - query register offset from its name
  562. * @name: the name of a register
  563. *
  564. * regs_query_register_offset() returns the offset of a register in struct
  565. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  566. */
  567. int regs_query_register_offset(const char *name)
  568. {
  569. const struct pt_regs_offset *roff;
  570. for (roff = regoffset_table; roff->name != NULL; roff++)
  571. if (!strcmp(roff->name, name))
  572. return roff->offset;
  573. return -EINVAL;
  574. }
  575. #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
  576. static const struct user_regset mips_regsets[] = {
  577. [REGSET_GPR] = {
  578. .core_note_type = NT_PRSTATUS,
  579. .n = ELF_NGREG,
  580. .size = sizeof(unsigned int),
  581. .align = sizeof(unsigned int),
  582. .get = gpr32_get,
  583. .set = gpr32_set,
  584. },
  585. [REGSET_FPR] = {
  586. .core_note_type = NT_PRFPREG,
  587. .n = ELF_NFPREG,
  588. .size = sizeof(elf_fpreg_t),
  589. .align = sizeof(elf_fpreg_t),
  590. .get = fpr_get,
  591. .set = fpr_set,
  592. },
  593. };
  594. static const struct user_regset_view user_mips_view = {
  595. .name = "mips",
  596. .e_machine = ELF_ARCH,
  597. .ei_osabi = ELF_OSABI,
  598. .regsets = mips_regsets,
  599. .n = ARRAY_SIZE(mips_regsets),
  600. };
  601. #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
  602. #ifdef CONFIG_64BIT
  603. static const struct user_regset mips64_regsets[] = {
  604. [REGSET_GPR] = {
  605. .core_note_type = NT_PRSTATUS,
  606. .n = ELF_NGREG,
  607. .size = sizeof(unsigned long),
  608. .align = sizeof(unsigned long),
  609. .get = gpr64_get,
  610. .set = gpr64_set,
  611. },
  612. [REGSET_FPR] = {
  613. .core_note_type = NT_PRFPREG,
  614. .n = ELF_NFPREG,
  615. .size = sizeof(elf_fpreg_t),
  616. .align = sizeof(elf_fpreg_t),
  617. .get = fpr_get,
  618. .set = fpr_set,
  619. },
  620. };
  621. static const struct user_regset_view user_mips64_view = {
  622. .name = "mips64",
  623. .e_machine = ELF_ARCH,
  624. .ei_osabi = ELF_OSABI,
  625. .regsets = mips64_regsets,
  626. .n = ARRAY_SIZE(mips64_regsets),
  627. };
  628. #ifdef CONFIG_MIPS32_N32
  629. static const struct user_regset_view user_mipsn32_view = {
  630. .name = "mipsn32",
  631. .e_flags = EF_MIPS_ABI2,
  632. .e_machine = ELF_ARCH,
  633. .ei_osabi = ELF_OSABI,
  634. .regsets = mips64_regsets,
  635. .n = ARRAY_SIZE(mips64_regsets),
  636. };
  637. #endif /* CONFIG_MIPS32_N32 */
  638. #endif /* CONFIG_64BIT */
  639. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  640. {
  641. #ifdef CONFIG_32BIT
  642. return &user_mips_view;
  643. #else
  644. #ifdef CONFIG_MIPS32_O32
  645. if (test_tsk_thread_flag(task, TIF_32BIT_REGS))
  646. return &user_mips_view;
  647. #endif
  648. #ifdef CONFIG_MIPS32_N32
  649. if (test_tsk_thread_flag(task, TIF_32BIT_ADDR))
  650. return &user_mipsn32_view;
  651. #endif
  652. return &user_mips64_view;
  653. #endif
  654. }
  655. long arch_ptrace(struct task_struct *child, long request,
  656. unsigned long addr, unsigned long data)
  657. {
  658. int ret;
  659. void __user *addrp = (void __user *) addr;
  660. void __user *datavp = (void __user *) data;
  661. unsigned long __user *datalp = (void __user *) data;
  662. switch (request) {
  663. /* when I and D space are separate, these will need to be fixed. */
  664. case PTRACE_PEEKTEXT: /* read word at location addr. */
  665. case PTRACE_PEEKDATA:
  666. ret = generic_ptrace_peekdata(child, addr, data);
  667. break;
  668. /* Read the word at location addr in the USER area. */
  669. case PTRACE_PEEKUSR: {
  670. struct pt_regs *regs;
  671. union fpureg *fregs;
  672. unsigned long tmp = 0;
  673. regs = task_pt_regs(child);
  674. ret = 0; /* Default return value. */
  675. switch (addr) {
  676. case 0 ... 31:
  677. tmp = regs->regs[addr];
  678. break;
  679. case FPR_BASE ... FPR_BASE + 31:
  680. if (!tsk_used_math(child)) {
  681. /* FP not yet used */
  682. tmp = -1;
  683. break;
  684. }
  685. fregs = get_fpu_regs(child);
  686. #ifdef CONFIG_32BIT
  687. if (test_thread_flag(TIF_32BIT_FPREGS)) {
  688. /*
  689. * The odd registers are actually the high
  690. * order bits of the values stored in the even
  691. * registers - unless we're using r2k_switch.S.
  692. */
  693. tmp = get_fpr32(&fregs[(addr & ~1) - FPR_BASE],
  694. addr & 1);
  695. break;
  696. }
  697. #endif
  698. tmp = get_fpr32(&fregs[addr - FPR_BASE], 0);
  699. break;
  700. case PC:
  701. tmp = regs->cp0_epc;
  702. break;
  703. case CAUSE:
  704. tmp = regs->cp0_cause;
  705. break;
  706. case BADVADDR:
  707. tmp = regs->cp0_badvaddr;
  708. break;
  709. case MMHI:
  710. tmp = regs->hi;
  711. break;
  712. case MMLO:
  713. tmp = regs->lo;
  714. break;
  715. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  716. case ACX:
  717. tmp = regs->acx;
  718. break;
  719. #endif
  720. case FPC_CSR:
  721. tmp = child->thread.fpu.fcr31;
  722. break;
  723. case FPC_EIR:
  724. /* implementation / version register */
  725. tmp = boot_cpu_data.fpu_id;
  726. break;
  727. case DSP_BASE ... DSP_BASE + 5: {
  728. dspreg_t *dregs;
  729. if (!cpu_has_dsp) {
  730. tmp = 0;
  731. ret = -EIO;
  732. goto out;
  733. }
  734. dregs = __get_dsp_regs(child);
  735. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  736. break;
  737. }
  738. case DSP_CONTROL:
  739. if (!cpu_has_dsp) {
  740. tmp = 0;
  741. ret = -EIO;
  742. goto out;
  743. }
  744. tmp = child->thread.dsp.dspcontrol;
  745. break;
  746. default:
  747. tmp = 0;
  748. ret = -EIO;
  749. goto out;
  750. }
  751. ret = put_user(tmp, datalp);
  752. break;
  753. }
  754. /* when I and D space are separate, this will have to be fixed. */
  755. case PTRACE_POKETEXT: /* write the word at location addr. */
  756. case PTRACE_POKEDATA:
  757. ret = generic_ptrace_pokedata(child, addr, data);
  758. break;
  759. case PTRACE_POKEUSR: {
  760. struct pt_regs *regs;
  761. ret = 0;
  762. regs = task_pt_regs(child);
  763. switch (addr) {
  764. case 0 ... 31:
  765. regs->regs[addr] = data;
  766. /* System call number may have been changed */
  767. if (addr == 2)
  768. mips_syscall_update_nr(child, regs);
  769. else if (addr == 4 &&
  770. mips_syscall_is_indirect(child, regs))
  771. mips_syscall_update_nr(child, regs);
  772. break;
  773. case FPR_BASE ... FPR_BASE + 31: {
  774. union fpureg *fregs = get_fpu_regs(child);
  775. init_fp_ctx(child);
  776. #ifdef CONFIG_32BIT
  777. if (test_thread_flag(TIF_32BIT_FPREGS)) {
  778. /*
  779. * The odd registers are actually the high
  780. * order bits of the values stored in the even
  781. * registers - unless we're using r2k_switch.S.
  782. */
  783. set_fpr32(&fregs[(addr & ~1) - FPR_BASE],
  784. addr & 1, data);
  785. break;
  786. }
  787. #endif
  788. set_fpr64(&fregs[addr - FPR_BASE], 0, data);
  789. break;
  790. }
  791. case PC:
  792. regs->cp0_epc = data;
  793. break;
  794. case MMHI:
  795. regs->hi = data;
  796. break;
  797. case MMLO:
  798. regs->lo = data;
  799. break;
  800. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  801. case ACX:
  802. regs->acx = data;
  803. break;
  804. #endif
  805. case FPC_CSR:
  806. init_fp_ctx(child);
  807. ptrace_setfcr31(child, data);
  808. break;
  809. case DSP_BASE ... DSP_BASE + 5: {
  810. dspreg_t *dregs;
  811. if (!cpu_has_dsp) {
  812. ret = -EIO;
  813. break;
  814. }
  815. dregs = __get_dsp_regs(child);
  816. dregs[addr - DSP_BASE] = data;
  817. break;
  818. }
  819. case DSP_CONTROL:
  820. if (!cpu_has_dsp) {
  821. ret = -EIO;
  822. break;
  823. }
  824. child->thread.dsp.dspcontrol = data;
  825. break;
  826. default:
  827. /* The rest are not allowed. */
  828. ret = -EIO;
  829. break;
  830. }
  831. break;
  832. }
  833. case PTRACE_GETREGS:
  834. ret = ptrace_getregs(child, datavp);
  835. break;
  836. case PTRACE_SETREGS:
  837. ret = ptrace_setregs(child, datavp);
  838. break;
  839. case PTRACE_GETFPREGS:
  840. ret = ptrace_getfpregs(child, datavp);
  841. break;
  842. case PTRACE_SETFPREGS:
  843. ret = ptrace_setfpregs(child, datavp);
  844. break;
  845. case PTRACE_GET_THREAD_AREA:
  846. ret = put_user(task_thread_info(child)->tp_value, datalp);
  847. break;
  848. case PTRACE_GET_WATCH_REGS:
  849. ret = ptrace_get_watch_regs(child, addrp);
  850. break;
  851. case PTRACE_SET_WATCH_REGS:
  852. ret = ptrace_set_watch_regs(child, addrp);
  853. break;
  854. default:
  855. ret = ptrace_request(child, request, addr, data);
  856. break;
  857. }
  858. out:
  859. return ret;
  860. }
  861. /*
  862. * Notification of system call entry/exit
  863. * - triggered by current->work.syscall_trace
  864. */
  865. asmlinkage long syscall_trace_enter(struct pt_regs *regs, long syscall)
  866. {
  867. user_exit();
  868. current_thread_info()->syscall = syscall;
  869. if (test_thread_flag(TIF_SYSCALL_TRACE)) {
  870. if (tracehook_report_syscall_entry(regs))
  871. return -1;
  872. syscall = current_thread_info()->syscall;
  873. }
  874. #ifdef CONFIG_SECCOMP
  875. if (unlikely(test_thread_flag(TIF_SECCOMP))) {
  876. int ret, i;
  877. struct seccomp_data sd;
  878. unsigned long args[6];
  879. sd.nr = syscall;
  880. sd.arch = syscall_get_arch();
  881. syscall_get_arguments(current, regs, 0, 6, args);
  882. for (i = 0; i < 6; i++)
  883. sd.args[i] = args[i];
  884. sd.instruction_pointer = KSTK_EIP(current);
  885. ret = __secure_computing(&sd);
  886. if (ret == -1)
  887. return ret;
  888. syscall = current_thread_info()->syscall;
  889. }
  890. #endif
  891. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  892. trace_sys_enter(regs, regs->regs[2]);
  893. audit_syscall_entry(syscall, regs->regs[4], regs->regs[5],
  894. regs->regs[6], regs->regs[7]);
  895. /*
  896. * Negative syscall numbers are mistaken for rejected syscalls, but
  897. * won't have had the return value set appropriately, so we do so now.
  898. */
  899. if (syscall < 0)
  900. syscall_set_return_value(current, regs, -ENOSYS, 0);
  901. return syscall;
  902. }
  903. /*
  904. * Notification of system call entry/exit
  905. * - triggered by current->work.syscall_trace
  906. */
  907. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  908. {
  909. /*
  910. * We may come here right after calling schedule_user()
  911. * or do_notify_resume(), in which case we can be in RCU
  912. * user mode.
  913. */
  914. user_exit();
  915. audit_syscall_exit(regs);
  916. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  917. trace_sys_exit(regs, regs_return_value(regs));
  918. if (test_thread_flag(TIF_SYSCALL_TRACE))
  919. tracehook_report_syscall_exit(regs, 0);
  920. user_enter();
  921. }