ptrace_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* By Ross Biro 1/23/92 */
  2. /*
  3. * Pentium III FXSR, SSE support
  4. * Gareth Hughes <gareth@valinux.com>, May 2000
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/smp.h>
  10. #include <linux/errno.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/user.h>
  13. #include <linux/security.h>
  14. #include <linux/audit.h>
  15. #include <linux/seccomp.h>
  16. #include <linux/signal.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/system.h>
  20. #include <asm/processor.h>
  21. #include <asm/i387.h>
  22. #include <asm/debugreg.h>
  23. #include <asm/ldt.h>
  24. #include <asm/desc.h>
  25. /*
  26. * does not yet catch signals sent when the child dies.
  27. * in exit.c or in signal.c.
  28. */
  29. /*
  30. * Determines which flags the user has access to [1 = access, 0 = no access].
  31. * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9).
  32. * Also masks reserved bits (31-22, 15, 5, 3, 1).
  33. */
  34. #define FLAG_MASK 0x00050dd5
  35. static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  36. {
  37. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  38. if (regno > FS)
  39. --regno;
  40. return &regs->bx + regno;
  41. }
  42. static int putreg(struct task_struct *child,
  43. unsigned long regno, unsigned long value)
  44. {
  45. struct pt_regs *regs = task_pt_regs(child);
  46. regno >>= 2;
  47. switch (regno) {
  48. case GS:
  49. if (value && (value & 3) != 3)
  50. return -EIO;
  51. child->thread.gs = value;
  52. return 0;
  53. case DS:
  54. case ES:
  55. case FS:
  56. if (value && (value & 3) != 3)
  57. return -EIO;
  58. value &= 0xffff;
  59. break;
  60. case SS:
  61. case CS:
  62. if ((value & 3) != 3)
  63. return -EIO;
  64. value &= 0xffff;
  65. break;
  66. case EFL:
  67. value &= FLAG_MASK;
  68. /*
  69. * If the user value contains TF, mark that
  70. * it was not "us" (the debugger) that set it.
  71. * If not, make sure it stays set if we had.
  72. */
  73. if (value & X86_EFLAGS_TF)
  74. clear_tsk_thread_flag(child, TIF_FORCED_TF);
  75. else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  76. value |= X86_EFLAGS_TF;
  77. value |= regs->flags & ~FLAG_MASK;
  78. break;
  79. }
  80. *pt_regs_access(regs, regno) = value;
  81. return 0;
  82. }
  83. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  84. {
  85. struct pt_regs *regs = task_pt_regs(child);
  86. unsigned long retval = ~0UL;
  87. regno >>= 2;
  88. switch (regno) {
  89. case EFL:
  90. /*
  91. * If the debugger set TF, hide it from the readout.
  92. */
  93. retval = regs->flags;
  94. if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  95. retval &= ~X86_EFLAGS_TF;
  96. break;
  97. case GS:
  98. retval = child->thread.gs;
  99. break;
  100. case DS:
  101. case ES:
  102. case FS:
  103. case SS:
  104. case CS:
  105. retval = 0xffff;
  106. /* fall through */
  107. default:
  108. retval &= *pt_regs_access(regs, regno);
  109. }
  110. return retval;
  111. }
  112. /*
  113. * This function is trivial and will be inlined by the compiler.
  114. * Having it separates the implementation details of debug
  115. * registers from the interface details of ptrace.
  116. */
  117. static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
  118. {
  119. return child->thread.debugreg[n];
  120. }
  121. static int ptrace_set_debugreg(struct task_struct *child,
  122. int n, unsigned long data)
  123. {
  124. if (unlikely(n == 4 || n == 5))
  125. return -EIO;
  126. if (n < 4 && unlikely(data >= TASK_SIZE - 3))
  127. return -EIO;
  128. if (n == 7) {
  129. /*
  130. * Sanity-check data. Take one half-byte at once with
  131. * check = (val >> (16 + 4*i)) & 0xf. It contains the
  132. * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
  133. * 2 and 3 are LENi. Given a list of invalid values,
  134. * we do mask |= 1 << invalid_value, so that
  135. * (mask >> check) & 1 is a correct test for invalid
  136. * values.
  137. *
  138. * R/Wi contains the type of the breakpoint /
  139. * watchpoint, LENi contains the length of the watched
  140. * data in the watchpoint case.
  141. *
  142. * The invalid values are:
  143. * - LENi == 0x10 (undefined), so mask |= 0x0f00.
  144. * - R/Wi == 0x10 (break on I/O reads or writes), so
  145. * mask |= 0x4444.
  146. * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
  147. * 0x1110.
  148. *
  149. * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
  150. *
  151. * See the Intel Manual "System Programming Guide",
  152. * 15.2.4
  153. *
  154. * Note that LENi == 0x10 is defined on x86_64 in long
  155. * mode (i.e. even for 32-bit userspace software, but
  156. * 64-bit kernel), so the x86_64 mask value is 0x5454.
  157. * See the AMD manual no. 24593 (AMD64 System Programming)
  158. */
  159. int i;
  160. data &= ~DR_CONTROL_RESERVED;
  161. for (i = 0; i < 4; i++)
  162. if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  163. return -EIO;
  164. if (data)
  165. set_tsk_thread_flag(child, TIF_DEBUG);
  166. else
  167. clear_tsk_thread_flag(child, TIF_DEBUG);
  168. }
  169. child->thread.debugreg[n] = data;
  170. return 0;
  171. }
  172. /*
  173. * Called by kernel/ptrace.c when detaching..
  174. *
  175. * Make sure the single step bit is not set.
  176. */
  177. void ptrace_disable(struct task_struct *child)
  178. {
  179. user_disable_single_step(child);
  180. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  181. }
  182. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  183. {
  184. struct user * dummy = NULL;
  185. int i, ret;
  186. unsigned long __user *datap = (unsigned long __user *)data;
  187. switch (request) {
  188. /* when I and D space are separate, these will need to be fixed. */
  189. case PTRACE_PEEKTEXT: /* read word at location addr. */
  190. case PTRACE_PEEKDATA:
  191. ret = generic_ptrace_peekdata(child, addr, data);
  192. break;
  193. /* read the word at location addr in the USER area. */
  194. case PTRACE_PEEKUSR: {
  195. unsigned long tmp;
  196. ret = -EIO;
  197. if ((addr & 3) || addr < 0 ||
  198. addr > sizeof(struct user) - 3)
  199. break;
  200. tmp = 0; /* Default return condition */
  201. if(addr < FRAME_SIZE*sizeof(long))
  202. tmp = getreg(child, addr);
  203. if(addr >= (long) &dummy->u_debugreg[0] &&
  204. addr <= (long) &dummy->u_debugreg[7]){
  205. addr -= (long) &dummy->u_debugreg[0];
  206. addr = addr >> 2;
  207. tmp = ptrace_get_debugreg(child, addr);
  208. }
  209. ret = put_user(tmp, datap);
  210. break;
  211. }
  212. /* when I and D space are separate, this will have to be fixed. */
  213. case PTRACE_POKETEXT: /* write the word at location addr. */
  214. case PTRACE_POKEDATA:
  215. ret = generic_ptrace_pokedata(child, addr, data);
  216. break;
  217. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  218. ret = -EIO;
  219. if ((addr & 3) || addr < 0 ||
  220. addr > sizeof(struct user) - 3)
  221. break;
  222. if (addr < FRAME_SIZE*sizeof(long)) {
  223. ret = putreg(child, addr, data);
  224. break;
  225. }
  226. /* We need to be very careful here. We implicitly
  227. want to modify a portion of the task_struct, and we
  228. have to be selective about what portions we allow someone
  229. to modify. */
  230. ret = -EIO;
  231. if(addr >= (long) &dummy->u_debugreg[0] &&
  232. addr <= (long) &dummy->u_debugreg[7]){
  233. addr -= (long) &dummy->u_debugreg;
  234. addr = addr >> 2;
  235. ret = ptrace_set_debugreg(child, addr, data);
  236. }
  237. break;
  238. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  239. if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
  240. ret = -EIO;
  241. break;
  242. }
  243. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  244. __put_user(getreg(child, i), datap);
  245. datap++;
  246. }
  247. ret = 0;
  248. break;
  249. }
  250. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  251. unsigned long tmp;
  252. if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
  253. ret = -EIO;
  254. break;
  255. }
  256. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  257. __get_user(tmp, datap);
  258. putreg(child, i, tmp);
  259. datap++;
  260. }
  261. ret = 0;
  262. break;
  263. }
  264. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  265. if (!access_ok(VERIFY_WRITE, datap,
  266. sizeof(struct user_i387_struct))) {
  267. ret = -EIO;
  268. break;
  269. }
  270. ret = 0;
  271. if (!tsk_used_math(child))
  272. init_fpu(child);
  273. get_fpregs((struct user_i387_struct __user *)data, child);
  274. break;
  275. }
  276. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  277. if (!access_ok(VERIFY_READ, datap,
  278. sizeof(struct user_i387_struct))) {
  279. ret = -EIO;
  280. break;
  281. }
  282. set_stopped_child_used_math(child);
  283. set_fpregs(child, (struct user_i387_struct __user *)data);
  284. ret = 0;
  285. break;
  286. }
  287. case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
  288. if (!access_ok(VERIFY_WRITE, datap,
  289. sizeof(struct user_fxsr_struct))) {
  290. ret = -EIO;
  291. break;
  292. }
  293. if (!tsk_used_math(child))
  294. init_fpu(child);
  295. ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
  296. break;
  297. }
  298. case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
  299. if (!access_ok(VERIFY_READ, datap,
  300. sizeof(struct user_fxsr_struct))) {
  301. ret = -EIO;
  302. break;
  303. }
  304. set_stopped_child_used_math(child);
  305. ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
  306. break;
  307. }
  308. case PTRACE_GET_THREAD_AREA:
  309. if (addr < 0)
  310. return -EIO;
  311. ret = do_get_thread_area(child, addr,
  312. (struct user_desc __user *) data);
  313. break;
  314. case PTRACE_SET_THREAD_AREA:
  315. if (addr < 0)
  316. return -EIO;
  317. ret = do_set_thread_area(child, addr,
  318. (struct user_desc __user *) data, 0);
  319. break;
  320. default:
  321. ret = ptrace_request(child, request, addr, data);
  322. break;
  323. }
  324. return ret;
  325. }
  326. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  327. {
  328. struct siginfo info;
  329. tsk->thread.trap_no = 1;
  330. tsk->thread.error_code = error_code;
  331. memset(&info, 0, sizeof(info));
  332. info.si_signo = SIGTRAP;
  333. info.si_code = TRAP_BRKPT;
  334. /* User-mode ip? */
  335. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  336. /* Send us the fake SIGTRAP */
  337. force_sig_info(SIGTRAP, &info, tsk);
  338. }
  339. /* notification of system call entry/exit
  340. * - triggered by current->work.syscall_trace
  341. */
  342. __attribute__((regparm(3)))
  343. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  344. {
  345. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  346. /*
  347. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  348. * interception
  349. */
  350. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  351. int ret = 0;
  352. /* do the secure computing check first */
  353. if (!entryexit)
  354. secure_computing(regs->orig_ax);
  355. if (unlikely(current->audit_context)) {
  356. if (entryexit)
  357. audit_syscall_exit(AUDITSC_RESULT(regs->ax),
  358. regs->ax);
  359. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  360. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  361. * not used, entry.S will call us only on syscall exit, not
  362. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  363. * calling send_sigtrap() on syscall entry.
  364. *
  365. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  366. * is_singlestep is false, despite his name, so we will still do
  367. * the correct thing.
  368. */
  369. else if (is_singlestep)
  370. goto out;
  371. }
  372. if (!(current->ptrace & PT_PTRACED))
  373. goto out;
  374. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  375. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  376. * here. We have to check this and return */
  377. if (is_sysemu && entryexit)
  378. return 0;
  379. /* Fake a debug trap */
  380. if (is_singlestep)
  381. send_sigtrap(current, regs, 0);
  382. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  383. goto out;
  384. /* the 0x80 provides a way for the tracing parent to distinguish
  385. between a syscall stop and SIGTRAP delivery */
  386. /* Note that the debugger could change the result of test_thread_flag!*/
  387. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  388. /*
  389. * this isn't the same as continuing with a signal, but it will do
  390. * for normal use. strace only continues with a signal if the
  391. * stopping signal is not SIGTRAP. -brl
  392. */
  393. if (current->exit_code) {
  394. send_sig(current->exit_code, current, 1);
  395. current->exit_code = 0;
  396. }
  397. ret = is_sysemu;
  398. out:
  399. if (unlikely(current->audit_context) && !entryexit)
  400. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
  401. regs->bx, regs->cx, regs->dx, regs->si);
  402. if (ret == 0)
  403. return 0;
  404. regs->orig_ax = -1; /* force skip of syscall restarting */
  405. if (unlikely(current->audit_context))
  406. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  407. return 1;
  408. }