ptrace.c 26 KB

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