ptrace_32.c 14 KB

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