ptrace_32.c 12 KB

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