ptrace.c 26 KB

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