ptrace.c 26 KB

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