ptrace.c 25 KB

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