ptrace.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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. /* ...and FCSR zeroed */
  56. target->thread.fpu.fcr31 = 0;
  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. * Read a general register set. We always use the 64-bit format, even
  76. * for 32-bit kernels and for 32-bit processes on a 64-bit kernel.
  77. * Registers are sign extended to fill the available space.
  78. */
  79. int ptrace_getregs(struct task_struct *child, struct user_pt_regs __user *data)
  80. {
  81. struct pt_regs *regs;
  82. int i;
  83. if (!access_ok(VERIFY_WRITE, data, 38 * 8))
  84. return -EIO;
  85. regs = task_pt_regs(child);
  86. for (i = 0; i < 32; i++)
  87. __put_user((long)regs->regs[i], (__s64 __user *)&data->regs[i]);
  88. __put_user((long)regs->lo, (__s64 __user *)&data->lo);
  89. __put_user((long)regs->hi, (__s64 __user *)&data->hi);
  90. __put_user((long)regs->cp0_epc, (__s64 __user *)&data->cp0_epc);
  91. __put_user((long)regs->cp0_badvaddr, (__s64 __user *)&data->cp0_badvaddr);
  92. __put_user((long)regs->cp0_status, (__s64 __user *)&data->cp0_status);
  93. __put_user((long)regs->cp0_cause, (__s64 __user *)&data->cp0_cause);
  94. return 0;
  95. }
  96. /*
  97. * Write a general register set. As for PTRACE_GETREGS, we always use
  98. * the 64-bit format. On a 32-bit kernel only the lower order half
  99. * (according to endianness) will be used.
  100. */
  101. int ptrace_setregs(struct task_struct *child, struct user_pt_regs __user *data)
  102. {
  103. struct pt_regs *regs;
  104. int i;
  105. if (!access_ok(VERIFY_READ, data, 38 * 8))
  106. return -EIO;
  107. regs = task_pt_regs(child);
  108. for (i = 0; i < 32; i++)
  109. __get_user(regs->regs[i], (__s64 __user *)&data->regs[i]);
  110. __get_user(regs->lo, (__s64 __user *)&data->lo);
  111. __get_user(regs->hi, (__s64 __user *)&data->hi);
  112. __get_user(regs->cp0_epc, (__s64 __user *)&data->cp0_epc);
  113. /* badvaddr, status, and cause may not be written. */
  114. return 0;
  115. }
  116. int ptrace_getfpregs(struct task_struct *child, __u32 __user *data)
  117. {
  118. int i;
  119. if (!access_ok(VERIFY_WRITE, data, 33 * 8))
  120. return -EIO;
  121. if (tsk_used_math(child)) {
  122. union fpureg *fregs = get_fpu_regs(child);
  123. for (i = 0; i < 32; i++)
  124. __put_user(get_fpr64(&fregs[i], 0),
  125. i + (__u64 __user *)data);
  126. } else {
  127. for (i = 0; i < 32; i++)
  128. __put_user((__u64) -1, i + (__u64 __user *) data);
  129. }
  130. __put_user(child->thread.fpu.fcr31, data + 64);
  131. __put_user(boot_cpu_data.fpu_id, data + 65);
  132. return 0;
  133. }
  134. int ptrace_setfpregs(struct task_struct *child, __u32 __user *data)
  135. {
  136. union fpureg *fregs;
  137. u64 fpr_val;
  138. u32 fcr31;
  139. u32 value;
  140. u32 mask;
  141. int i;
  142. if (!access_ok(VERIFY_READ, data, 33 * 8))
  143. return -EIO;
  144. init_fp_ctx(child);
  145. fregs = get_fpu_regs(child);
  146. for (i = 0; i < 32; i++) {
  147. __get_user(fpr_val, i + (__u64 __user *)data);
  148. set_fpr64(&fregs[i], 0, fpr_val);
  149. }
  150. __get_user(value, data + 64);
  151. fcr31 = child->thread.fpu.fcr31;
  152. mask = boot_cpu_data.fpu_msk31;
  153. child->thread.fpu.fcr31 = (value & ~mask) | (fcr31 & mask);
  154. /* FIR may not be written. */
  155. return 0;
  156. }
  157. int ptrace_get_watch_regs(struct task_struct *child,
  158. struct pt_watch_regs __user *addr)
  159. {
  160. enum pt_watch_style style;
  161. int i;
  162. if (!cpu_has_watch || boot_cpu_data.watch_reg_use_cnt == 0)
  163. return -EIO;
  164. if (!access_ok(VERIFY_WRITE, addr, sizeof(struct pt_watch_regs)))
  165. return -EIO;
  166. #ifdef CONFIG_32BIT
  167. style = pt_watch_style_mips32;
  168. #define WATCH_STYLE mips32
  169. #else
  170. style = pt_watch_style_mips64;
  171. #define WATCH_STYLE mips64
  172. #endif
  173. __put_user(style, &addr->style);
  174. __put_user(boot_cpu_data.watch_reg_use_cnt,
  175. &addr->WATCH_STYLE.num_valid);
  176. for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
  177. __put_user(child->thread.watch.mips3264.watchlo[i],
  178. &addr->WATCH_STYLE.watchlo[i]);
  179. __put_user(child->thread.watch.mips3264.watchhi[i] & 0xfff,
  180. &addr->WATCH_STYLE.watchhi[i]);
  181. __put_user(boot_cpu_data.watch_reg_masks[i],
  182. &addr->WATCH_STYLE.watch_masks[i]);
  183. }
  184. for (; i < 8; i++) {
  185. __put_user(0, &addr->WATCH_STYLE.watchlo[i]);
  186. __put_user(0, &addr->WATCH_STYLE.watchhi[i]);
  187. __put_user(0, &addr->WATCH_STYLE.watch_masks[i]);
  188. }
  189. return 0;
  190. }
  191. int ptrace_set_watch_regs(struct task_struct *child,
  192. struct pt_watch_regs __user *addr)
  193. {
  194. int i;
  195. int watch_active = 0;
  196. unsigned long lt[NUM_WATCH_REGS];
  197. u16 ht[NUM_WATCH_REGS];
  198. if (!cpu_has_watch || boot_cpu_data.watch_reg_use_cnt == 0)
  199. return -EIO;
  200. if (!access_ok(VERIFY_READ, addr, sizeof(struct pt_watch_regs)))
  201. return -EIO;
  202. /* Check the values. */
  203. for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
  204. __get_user(lt[i], &addr->WATCH_STYLE.watchlo[i]);
  205. #ifdef CONFIG_32BIT
  206. if (lt[i] & __UA_LIMIT)
  207. return -EINVAL;
  208. #else
  209. if (test_tsk_thread_flag(child, TIF_32BIT_ADDR)) {
  210. if (lt[i] & 0xffffffff80000000UL)
  211. return -EINVAL;
  212. } else {
  213. if (lt[i] & __UA_LIMIT)
  214. return -EINVAL;
  215. }
  216. #endif
  217. __get_user(ht[i], &addr->WATCH_STYLE.watchhi[i]);
  218. if (ht[i] & ~0xff8)
  219. return -EINVAL;
  220. }
  221. /* Install them. */
  222. for (i = 0; i < boot_cpu_data.watch_reg_use_cnt; i++) {
  223. if (lt[i] & 7)
  224. watch_active = 1;
  225. child->thread.watch.mips3264.watchlo[i] = lt[i];
  226. /* Set the G bit. */
  227. child->thread.watch.mips3264.watchhi[i] = ht[i];
  228. }
  229. if (watch_active)
  230. set_tsk_thread_flag(child, TIF_LOAD_WATCH);
  231. else
  232. clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
  233. return 0;
  234. }
  235. /* regset get/set implementations */
  236. #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
  237. static int gpr32_get(struct task_struct *target,
  238. const struct user_regset *regset,
  239. unsigned int pos, unsigned int count,
  240. void *kbuf, void __user *ubuf)
  241. {
  242. struct pt_regs *regs = task_pt_regs(target);
  243. u32 uregs[ELF_NGREG] = {};
  244. unsigned i;
  245. for (i = MIPS32_EF_R1; i <= MIPS32_EF_R31; i++) {
  246. /* k0/k1 are copied as zero. */
  247. if (i == MIPS32_EF_R26 || i == MIPS32_EF_R27)
  248. continue;
  249. uregs[i] = regs->regs[i - MIPS32_EF_R0];
  250. }
  251. uregs[MIPS32_EF_LO] = regs->lo;
  252. uregs[MIPS32_EF_HI] = regs->hi;
  253. uregs[MIPS32_EF_CP0_EPC] = regs->cp0_epc;
  254. uregs[MIPS32_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  255. uregs[MIPS32_EF_CP0_STATUS] = regs->cp0_status;
  256. uregs[MIPS32_EF_CP0_CAUSE] = regs->cp0_cause;
  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. return 0;
  300. }
  301. #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
  302. #ifdef CONFIG_64BIT
  303. static int gpr64_get(struct task_struct *target,
  304. const struct user_regset *regset,
  305. unsigned int pos, unsigned int count,
  306. void *kbuf, void __user *ubuf)
  307. {
  308. struct pt_regs *regs = task_pt_regs(target);
  309. u64 uregs[ELF_NGREG] = {};
  310. unsigned i;
  311. for (i = MIPS64_EF_R1; i <= MIPS64_EF_R31; i++) {
  312. /* k0/k1 are copied as zero. */
  313. if (i == MIPS64_EF_R26 || i == MIPS64_EF_R27)
  314. continue;
  315. uregs[i] = regs->regs[i - MIPS64_EF_R0];
  316. }
  317. uregs[MIPS64_EF_LO] = regs->lo;
  318. uregs[MIPS64_EF_HI] = regs->hi;
  319. uregs[MIPS64_EF_CP0_EPC] = regs->cp0_epc;
  320. uregs[MIPS64_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
  321. uregs[MIPS64_EF_CP0_STATUS] = regs->cp0_status;
  322. uregs[MIPS64_EF_CP0_CAUSE] = regs->cp0_cause;
  323. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
  324. sizeof(uregs));
  325. }
  326. static int gpr64_set(struct task_struct *target,
  327. const struct user_regset *regset,
  328. unsigned int pos, unsigned int count,
  329. const void *kbuf, const void __user *ubuf)
  330. {
  331. struct pt_regs *regs = task_pt_regs(target);
  332. u64 uregs[ELF_NGREG];
  333. unsigned start, num_regs, i;
  334. int err;
  335. start = pos / sizeof(u64);
  336. num_regs = count / sizeof(u64);
  337. if (start + num_regs > ELF_NGREG)
  338. return -EIO;
  339. err = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
  340. sizeof(uregs));
  341. if (err)
  342. return err;
  343. for (i = start; i < num_regs; i++) {
  344. switch (i) {
  345. case MIPS64_EF_R1 ... MIPS64_EF_R25:
  346. /* k0/k1 are ignored. */
  347. case MIPS64_EF_R28 ... MIPS64_EF_R31:
  348. regs->regs[i - MIPS64_EF_R0] = uregs[i];
  349. break;
  350. case MIPS64_EF_LO:
  351. regs->lo = uregs[i];
  352. break;
  353. case MIPS64_EF_HI:
  354. regs->hi = uregs[i];
  355. break;
  356. case MIPS64_EF_CP0_EPC:
  357. regs->cp0_epc = uregs[i];
  358. break;
  359. }
  360. }
  361. return 0;
  362. }
  363. #endif /* CONFIG_64BIT */
  364. static int fpr_get(struct task_struct *target,
  365. const struct user_regset *regset,
  366. unsigned int pos, unsigned int count,
  367. void *kbuf, void __user *ubuf)
  368. {
  369. unsigned i;
  370. int err;
  371. u64 fpr_val;
  372. /* XXX fcr31 */
  373. if (sizeof(target->thread.fpu.fpr[i]) == sizeof(elf_fpreg_t))
  374. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  375. &target->thread.fpu,
  376. 0, sizeof(elf_fpregset_t));
  377. for (i = 0; i < NUM_FPU_REGS; i++) {
  378. fpr_val = get_fpr64(&target->thread.fpu.fpr[i], 0);
  379. err = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  380. &fpr_val, i * sizeof(elf_fpreg_t),
  381. (i + 1) * sizeof(elf_fpreg_t));
  382. if (err)
  383. return err;
  384. }
  385. return 0;
  386. }
  387. static int fpr_set(struct task_struct *target,
  388. const struct user_regset *regset,
  389. unsigned int pos, unsigned int count,
  390. const void *kbuf, const void __user *ubuf)
  391. {
  392. unsigned i;
  393. int err;
  394. u64 fpr_val;
  395. /* XXX fcr31 */
  396. init_fp_ctx(target);
  397. if (sizeof(target->thread.fpu.fpr[i]) == sizeof(elf_fpreg_t))
  398. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  399. &target->thread.fpu,
  400. 0, sizeof(elf_fpregset_t));
  401. for (i = 0; i < NUM_FPU_REGS; i++) {
  402. err = user_regset_copyin(&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. set_fpr64(&target->thread.fpu.fpr[i], 0, fpr_val);
  408. }
  409. return 0;
  410. }
  411. enum mips_regset {
  412. REGSET_GPR,
  413. REGSET_FPR,
  414. };
  415. struct pt_regs_offset {
  416. const char *name;
  417. int offset;
  418. };
  419. #define REG_OFFSET_NAME(reg, r) { \
  420. .name = #reg, \
  421. .offset = offsetof(struct pt_regs, r) \
  422. }
  423. #define REG_OFFSET_END { \
  424. .name = NULL, \
  425. .offset = 0 \
  426. }
  427. static const struct pt_regs_offset regoffset_table[] = {
  428. REG_OFFSET_NAME(r0, regs[0]),
  429. REG_OFFSET_NAME(r1, regs[1]),
  430. REG_OFFSET_NAME(r2, regs[2]),
  431. REG_OFFSET_NAME(r3, regs[3]),
  432. REG_OFFSET_NAME(r4, regs[4]),
  433. REG_OFFSET_NAME(r5, regs[5]),
  434. REG_OFFSET_NAME(r6, regs[6]),
  435. REG_OFFSET_NAME(r7, regs[7]),
  436. REG_OFFSET_NAME(r8, regs[8]),
  437. REG_OFFSET_NAME(r9, regs[9]),
  438. REG_OFFSET_NAME(r10, regs[10]),
  439. REG_OFFSET_NAME(r11, regs[11]),
  440. REG_OFFSET_NAME(r12, regs[12]),
  441. REG_OFFSET_NAME(r13, regs[13]),
  442. REG_OFFSET_NAME(r14, regs[14]),
  443. REG_OFFSET_NAME(r15, regs[15]),
  444. REG_OFFSET_NAME(r16, regs[16]),
  445. REG_OFFSET_NAME(r17, regs[17]),
  446. REG_OFFSET_NAME(r18, regs[18]),
  447. REG_OFFSET_NAME(r19, regs[19]),
  448. REG_OFFSET_NAME(r20, regs[20]),
  449. REG_OFFSET_NAME(r21, regs[21]),
  450. REG_OFFSET_NAME(r22, regs[22]),
  451. REG_OFFSET_NAME(r23, regs[23]),
  452. REG_OFFSET_NAME(r24, regs[24]),
  453. REG_OFFSET_NAME(r25, regs[25]),
  454. REG_OFFSET_NAME(r26, regs[26]),
  455. REG_OFFSET_NAME(r27, regs[27]),
  456. REG_OFFSET_NAME(r28, regs[28]),
  457. REG_OFFSET_NAME(r29, regs[29]),
  458. REG_OFFSET_NAME(r30, regs[30]),
  459. REG_OFFSET_NAME(r31, regs[31]),
  460. REG_OFFSET_NAME(c0_status, cp0_status),
  461. REG_OFFSET_NAME(hi, hi),
  462. REG_OFFSET_NAME(lo, lo),
  463. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  464. REG_OFFSET_NAME(acx, acx),
  465. #endif
  466. REG_OFFSET_NAME(c0_badvaddr, cp0_badvaddr),
  467. REG_OFFSET_NAME(c0_cause, cp0_cause),
  468. REG_OFFSET_NAME(c0_epc, cp0_epc),
  469. #ifdef CONFIG_MIPS_MT_SMTC
  470. REG_OFFSET_NAME(c0_tcstatus, cp0_tcstatus),
  471. #endif
  472. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  473. REG_OFFSET_NAME(mpl0, mpl[0]),
  474. REG_OFFSET_NAME(mpl1, mpl[1]),
  475. REG_OFFSET_NAME(mpl2, mpl[2]),
  476. REG_OFFSET_NAME(mtp0, mtp[0]),
  477. REG_OFFSET_NAME(mtp1, mtp[1]),
  478. REG_OFFSET_NAME(mtp2, mtp[2]),
  479. #endif
  480. REG_OFFSET_END,
  481. };
  482. /**
  483. * regs_query_register_offset() - query register offset from its name
  484. * @name: the name of a register
  485. *
  486. * regs_query_register_offset() returns the offset of a register in struct
  487. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  488. */
  489. int regs_query_register_offset(const char *name)
  490. {
  491. const struct pt_regs_offset *roff;
  492. for (roff = regoffset_table; roff->name != NULL; roff++)
  493. if (!strcmp(roff->name, name))
  494. return roff->offset;
  495. return -EINVAL;
  496. }
  497. #if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
  498. static const struct user_regset mips_regsets[] = {
  499. [REGSET_GPR] = {
  500. .core_note_type = NT_PRSTATUS,
  501. .n = ELF_NGREG,
  502. .size = sizeof(unsigned int),
  503. .align = sizeof(unsigned int),
  504. .get = gpr32_get,
  505. .set = gpr32_set,
  506. },
  507. [REGSET_FPR] = {
  508. .core_note_type = NT_PRFPREG,
  509. .n = ELF_NFPREG,
  510. .size = sizeof(elf_fpreg_t),
  511. .align = sizeof(elf_fpreg_t),
  512. .get = fpr_get,
  513. .set = fpr_set,
  514. },
  515. };
  516. static const struct user_regset_view user_mips_view = {
  517. .name = "mips",
  518. .e_machine = ELF_ARCH,
  519. .ei_osabi = ELF_OSABI,
  520. .regsets = mips_regsets,
  521. .n = ARRAY_SIZE(mips_regsets),
  522. };
  523. #endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
  524. #ifdef CONFIG_64BIT
  525. static const struct user_regset mips64_regsets[] = {
  526. [REGSET_GPR] = {
  527. .core_note_type = NT_PRSTATUS,
  528. .n = ELF_NGREG,
  529. .size = sizeof(unsigned long),
  530. .align = sizeof(unsigned long),
  531. .get = gpr64_get,
  532. .set = gpr64_set,
  533. },
  534. [REGSET_FPR] = {
  535. .core_note_type = NT_PRFPREG,
  536. .n = ELF_NFPREG,
  537. .size = sizeof(elf_fpreg_t),
  538. .align = sizeof(elf_fpreg_t),
  539. .get = fpr_get,
  540. .set = fpr_set,
  541. },
  542. };
  543. static const struct user_regset_view user_mips64_view = {
  544. .name = "mips64",
  545. .e_machine = ELF_ARCH,
  546. .ei_osabi = ELF_OSABI,
  547. .regsets = mips64_regsets,
  548. .n = ARRAY_SIZE(mips64_regsets),
  549. };
  550. #endif /* CONFIG_64BIT */
  551. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  552. {
  553. #ifdef CONFIG_32BIT
  554. return &user_mips_view;
  555. #else
  556. #ifdef CONFIG_MIPS32_O32
  557. if (test_tsk_thread_flag(task, TIF_32BIT_REGS))
  558. return &user_mips_view;
  559. #endif
  560. return &user_mips64_view;
  561. #endif
  562. }
  563. long arch_ptrace(struct task_struct *child, long request,
  564. unsigned long addr, unsigned long data)
  565. {
  566. int ret;
  567. void __user *addrp = (void __user *) addr;
  568. void __user *datavp = (void __user *) data;
  569. unsigned long __user *datalp = (void __user *) data;
  570. switch (request) {
  571. /* when I and D space are separate, these will need to be fixed. */
  572. case PTRACE_PEEKTEXT: /* read word at location addr. */
  573. case PTRACE_PEEKDATA:
  574. ret = generic_ptrace_peekdata(child, addr, data);
  575. break;
  576. /* Read the word at location addr in the USER area. */
  577. case PTRACE_PEEKUSR: {
  578. struct pt_regs *regs;
  579. union fpureg *fregs;
  580. unsigned long tmp = 0;
  581. regs = task_pt_regs(child);
  582. ret = 0; /* Default return value. */
  583. switch (addr) {
  584. case 0 ... 31:
  585. tmp = regs->regs[addr];
  586. break;
  587. case FPR_BASE ... FPR_BASE + 31:
  588. if (!tsk_used_math(child)) {
  589. /* FP not yet used */
  590. tmp = -1;
  591. break;
  592. }
  593. fregs = get_fpu_regs(child);
  594. #ifdef CONFIG_32BIT
  595. if (test_thread_flag(TIF_32BIT_FPREGS)) {
  596. /*
  597. * The odd registers are actually the high
  598. * order bits of the values stored in the even
  599. * registers - unless we're using r2k_switch.S.
  600. */
  601. tmp = get_fpr32(&fregs[(addr & ~1) - FPR_BASE],
  602. addr & 1);
  603. break;
  604. }
  605. #endif
  606. tmp = get_fpr32(&fregs[addr - FPR_BASE], 0);
  607. break;
  608. case PC:
  609. tmp = regs->cp0_epc;
  610. break;
  611. case CAUSE:
  612. tmp = regs->cp0_cause;
  613. break;
  614. case BADVADDR:
  615. tmp = regs->cp0_badvaddr;
  616. break;
  617. case MMHI:
  618. tmp = regs->hi;
  619. break;
  620. case MMLO:
  621. tmp = regs->lo;
  622. break;
  623. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  624. case ACX:
  625. tmp = regs->acx;
  626. break;
  627. #endif
  628. case FPC_CSR:
  629. tmp = child->thread.fpu.fcr31;
  630. break;
  631. case FPC_EIR:
  632. /* implementation / version register */
  633. tmp = boot_cpu_data.fpu_id;
  634. break;
  635. case DSP_BASE ... DSP_BASE + 5: {
  636. dspreg_t *dregs;
  637. if (!cpu_has_dsp) {
  638. tmp = 0;
  639. ret = -EIO;
  640. goto out;
  641. }
  642. dregs = __get_dsp_regs(child);
  643. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  644. break;
  645. }
  646. case DSP_CONTROL:
  647. if (!cpu_has_dsp) {
  648. tmp = 0;
  649. ret = -EIO;
  650. goto out;
  651. }
  652. tmp = child->thread.dsp.dspcontrol;
  653. break;
  654. default:
  655. tmp = 0;
  656. ret = -EIO;
  657. goto out;
  658. }
  659. ret = put_user(tmp, datalp);
  660. break;
  661. }
  662. /* when I and D space are separate, this will have to be fixed. */
  663. case PTRACE_POKETEXT: /* write the word at location addr. */
  664. case PTRACE_POKEDATA:
  665. ret = generic_ptrace_pokedata(child, addr, data);
  666. break;
  667. case PTRACE_POKEUSR: {
  668. struct pt_regs *regs;
  669. ret = 0;
  670. regs = task_pt_regs(child);
  671. switch (addr) {
  672. case 0 ... 31:
  673. regs->regs[addr] = data;
  674. break;
  675. case FPR_BASE ... FPR_BASE + 31: {
  676. union fpureg *fregs = get_fpu_regs(child);
  677. init_fp_ctx(child);
  678. #ifdef CONFIG_32BIT
  679. if (test_thread_flag(TIF_32BIT_FPREGS)) {
  680. /*
  681. * The odd registers are actually the high
  682. * order bits of the values stored in the even
  683. * registers - unless we're using r2k_switch.S.
  684. */
  685. set_fpr32(&fregs[(addr & ~1) - FPR_BASE],
  686. addr & 1, data);
  687. break;
  688. }
  689. #endif
  690. set_fpr64(&fregs[addr - FPR_BASE], 0, data);
  691. break;
  692. }
  693. case PC:
  694. regs->cp0_epc = data;
  695. break;
  696. case MMHI:
  697. regs->hi = data;
  698. break;
  699. case MMLO:
  700. regs->lo = data;
  701. break;
  702. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  703. case ACX:
  704. regs->acx = data;
  705. break;
  706. #endif
  707. case FPC_CSR:
  708. child->thread.fpu.fcr31 = data & ~FPU_CSR_ALL_X;
  709. break;
  710. case DSP_BASE ... DSP_BASE + 5: {
  711. dspreg_t *dregs;
  712. if (!cpu_has_dsp) {
  713. ret = -EIO;
  714. break;
  715. }
  716. dregs = __get_dsp_regs(child);
  717. dregs[addr - DSP_BASE] = data;
  718. break;
  719. }
  720. case DSP_CONTROL:
  721. if (!cpu_has_dsp) {
  722. ret = -EIO;
  723. break;
  724. }
  725. child->thread.dsp.dspcontrol = data;
  726. break;
  727. default:
  728. /* The rest are not allowed. */
  729. ret = -EIO;
  730. break;
  731. }
  732. break;
  733. }
  734. case PTRACE_GETREGS:
  735. ret = ptrace_getregs(child, datavp);
  736. break;
  737. case PTRACE_SETREGS:
  738. ret = ptrace_setregs(child, datavp);
  739. break;
  740. case PTRACE_GETFPREGS:
  741. ret = ptrace_getfpregs(child, datavp);
  742. break;
  743. case PTRACE_SETFPREGS:
  744. ret = ptrace_setfpregs(child, datavp);
  745. break;
  746. case PTRACE_GET_THREAD_AREA:
  747. ret = put_user(task_thread_info(child)->tp_value, datalp);
  748. break;
  749. case PTRACE_GET_WATCH_REGS:
  750. ret = ptrace_get_watch_regs(child, addrp);
  751. break;
  752. case PTRACE_SET_WATCH_REGS:
  753. ret = ptrace_set_watch_regs(child, addrp);
  754. break;
  755. default:
  756. ret = ptrace_request(child, request, addr, data);
  757. break;
  758. }
  759. out:
  760. return ret;
  761. }
  762. /*
  763. * Notification of system call entry/exit
  764. * - triggered by current->work.syscall_trace
  765. */
  766. asmlinkage long syscall_trace_enter(struct pt_regs *regs, long syscall)
  767. {
  768. long ret = 0;
  769. user_exit();
  770. current_thread_info()->syscall = syscall;
  771. if (secure_computing() == -1)
  772. return -1;
  773. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  774. tracehook_report_syscall_entry(regs))
  775. ret = -1;
  776. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  777. trace_sys_enter(regs, regs->regs[2]);
  778. audit_syscall_entry(syscall, regs->regs[4], regs->regs[5],
  779. regs->regs[6], regs->regs[7]);
  780. return syscall;
  781. }
  782. /*
  783. * Notification of system call entry/exit
  784. * - triggered by current->work.syscall_trace
  785. */
  786. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  787. {
  788. /*
  789. * We may come here right after calling schedule_user()
  790. * or do_notify_resume(), in which case we can be in RCU
  791. * user mode.
  792. */
  793. user_exit();
  794. audit_syscall_exit(regs);
  795. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  796. trace_sys_exit(regs, regs->regs[2]);
  797. if (test_thread_flag(TIF_SYSCALL_TRACE))
  798. tracehook_report_syscall_exit(regs, 0);
  799. user_enter();
  800. }