ptrace.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /* ptrace.c */
  2. /* By Ross Biro 1/23/92 */
  3. /*
  4. * Pentium III FXSR, SSE support
  5. * Gareth Hughes <gareth@valinux.com>, May 2000
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/smp.h>
  11. #include <linux/smp_lock.h>
  12. #include <linux/errno.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/user.h>
  15. #include <linux/security.h>
  16. #include <linux/audit.h>
  17. #include <linux/seccomp.h>
  18. #include <linux/signal.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/system.h>
  22. #include <asm/processor.h>
  23. #include <asm/i387.h>
  24. #include <asm/debugreg.h>
  25. #include <asm/ldt.h>
  26. #include <asm/desc.h>
  27. /*
  28. * does not yet catch signals sent when the child dies.
  29. * in exit.c or in signal.c.
  30. */
  31. /*
  32. * Determines which flags the user has access to [1 = access, 0 = no access].
  33. * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9).
  34. * Also masks reserved bits (31-22, 15, 5, 3, 1).
  35. */
  36. #define FLAG_MASK 0x00050dd5
  37. /* set's the trap flag. */
  38. #define TRAP_FLAG 0x100
  39. /*
  40. * Offset of eflags on child stack..
  41. */
  42. #define EFL_OFFSET offsetof(struct pt_regs, eflags)
  43. static inline struct pt_regs *get_child_regs(struct task_struct *task)
  44. {
  45. void *stack_top = (void *)task->thread.esp0;
  46. return stack_top - sizeof(struct pt_regs);
  47. }
  48. /*
  49. * This routine will get a word off of the processes privileged stack.
  50. * the offset is bytes into the pt_regs structure on the stack.
  51. * This routine assumes that all the privileged stacks are in our
  52. * data space.
  53. */
  54. static inline int get_stack_long(struct task_struct *task, int offset)
  55. {
  56. unsigned char *stack;
  57. stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs);
  58. stack += offset;
  59. return (*((int *)stack));
  60. }
  61. /*
  62. * This routine will put a word on the processes privileged stack.
  63. * the offset is bytes into the pt_regs structure on the stack.
  64. * This routine assumes that all the privileged stacks are in our
  65. * data space.
  66. */
  67. static inline int put_stack_long(struct task_struct *task, int offset,
  68. unsigned long data)
  69. {
  70. unsigned char * stack;
  71. stack = (unsigned char *)task->thread.esp0 - sizeof(struct pt_regs);
  72. stack += offset;
  73. *(unsigned long *) stack = data;
  74. return 0;
  75. }
  76. static int putreg(struct task_struct *child,
  77. unsigned long regno, unsigned long value)
  78. {
  79. switch (regno >> 2) {
  80. case FS:
  81. if (value && (value & 3) != 3)
  82. return -EIO;
  83. child->thread.fs = value;
  84. return 0;
  85. case DS:
  86. case ES:
  87. case GS:
  88. if (value && (value & 3) != 3)
  89. return -EIO;
  90. value &= 0xffff;
  91. break;
  92. case SS:
  93. case CS:
  94. if ((value & 3) != 3)
  95. return -EIO;
  96. value &= 0xffff;
  97. break;
  98. case EFL:
  99. value &= FLAG_MASK;
  100. value |= get_stack_long(child, EFL_OFFSET) & ~FLAG_MASK;
  101. break;
  102. }
  103. if (regno > ES*4)
  104. regno -= 1*4;
  105. put_stack_long(child, regno, value);
  106. return 0;
  107. }
  108. static unsigned long getreg(struct task_struct *child,
  109. unsigned long regno)
  110. {
  111. unsigned long retval = ~0UL;
  112. switch (regno >> 2) {
  113. case FS:
  114. retval = child->thread.fs;
  115. break;
  116. case DS:
  117. case ES:
  118. case GS:
  119. case SS:
  120. case CS:
  121. retval = 0xffff;
  122. /* fall through */
  123. default:
  124. if (regno > ES*4)
  125. regno -= 1*4;
  126. retval &= get_stack_long(child, regno);
  127. }
  128. return retval;
  129. }
  130. #define LDT_SEGMENT 4
  131. static unsigned long convert_eip_to_linear(struct task_struct *child, struct pt_regs *regs)
  132. {
  133. unsigned long addr, seg;
  134. addr = regs->eip;
  135. seg = regs->xcs & 0xffff;
  136. if (regs->eflags & VM_MASK) {
  137. addr = (addr & 0xffff) + (seg << 4);
  138. return addr;
  139. }
  140. /*
  141. * We'll assume that the code segments in the GDT
  142. * are all zero-based. That is largely true: the
  143. * TLS segments are used for data, and the PNPBIOS
  144. * and APM bios ones we just ignore here.
  145. */
  146. if (seg & LDT_SEGMENT) {
  147. u32 *desc;
  148. unsigned long base;
  149. down(&child->mm->context.sem);
  150. desc = child->mm->context.ldt + (seg & ~7);
  151. base = (desc[0] >> 16) | ((desc[1] & 0xff) << 16) | (desc[1] & 0xff000000);
  152. /* 16-bit code segment? */
  153. if (!((desc[1] >> 22) & 1))
  154. addr &= 0xffff;
  155. addr += base;
  156. up(&child->mm->context.sem);
  157. }
  158. return addr;
  159. }
  160. static inline int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
  161. {
  162. int i, copied;
  163. unsigned char opcode[15];
  164. unsigned long addr = convert_eip_to_linear(child, regs);
  165. copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
  166. for (i = 0; i < copied; i++) {
  167. switch (opcode[i]) {
  168. /* popf and iret */
  169. case 0x9d: case 0xcf:
  170. return 1;
  171. /* opcode and address size prefixes */
  172. case 0x66: case 0x67:
  173. continue;
  174. /* irrelevant prefixes (segment overrides and repeats) */
  175. case 0x26: case 0x2e:
  176. case 0x36: case 0x3e:
  177. case 0x64: case 0x65:
  178. case 0xf0: case 0xf2: case 0xf3:
  179. continue;
  180. /*
  181. * pushf: NOTE! We should probably not let
  182. * the user see the TF bit being set. But
  183. * it's more pain than it's worth to avoid
  184. * it, and a debugger could emulate this
  185. * all in user space if it _really_ cares.
  186. */
  187. case 0x9c:
  188. default:
  189. return 0;
  190. }
  191. }
  192. return 0;
  193. }
  194. static void set_singlestep(struct task_struct *child)
  195. {
  196. struct pt_regs *regs = get_child_regs(child);
  197. /*
  198. * Always set TIF_SINGLESTEP - this guarantees that
  199. * we single-step system calls etc.. This will also
  200. * cause us to set TF when returning to user mode.
  201. */
  202. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  203. /*
  204. * If TF was already set, don't do anything else
  205. */
  206. if (regs->eflags & TRAP_FLAG)
  207. return;
  208. /* Set TF on the kernel stack.. */
  209. regs->eflags |= TRAP_FLAG;
  210. /*
  211. * ..but if TF is changed by the instruction we will trace,
  212. * don't mark it as being "us" that set it, so that we
  213. * won't clear it by hand later.
  214. */
  215. if (is_setting_trap_flag(child, regs))
  216. return;
  217. child->ptrace |= PT_DTRACE;
  218. }
  219. static void clear_singlestep(struct task_struct *child)
  220. {
  221. /* Always clear TIF_SINGLESTEP... */
  222. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  223. /* But touch TF only if it was set by us.. */
  224. if (child->ptrace & PT_DTRACE) {
  225. struct pt_regs *regs = get_child_regs(child);
  226. regs->eflags &= ~TRAP_FLAG;
  227. child->ptrace &= ~PT_DTRACE;
  228. }
  229. }
  230. /*
  231. * Called by kernel/ptrace.c when detaching..
  232. *
  233. * Make sure the single step bit is not set.
  234. */
  235. void ptrace_disable(struct task_struct *child)
  236. {
  237. clear_singlestep(child);
  238. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  239. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  240. }
  241. /*
  242. * Perform get_thread_area on behalf of the traced child.
  243. */
  244. static int
  245. ptrace_get_thread_area(struct task_struct *child,
  246. int idx, struct user_desc __user *user_desc)
  247. {
  248. struct user_desc info;
  249. struct desc_struct *desc;
  250. /*
  251. * Get the current Thread-Local Storage area:
  252. */
  253. #define GET_BASE(desc) ( \
  254. (((desc)->a >> 16) & 0x0000ffff) | \
  255. (((desc)->b << 16) & 0x00ff0000) | \
  256. ( (desc)->b & 0xff000000) )
  257. #define GET_LIMIT(desc) ( \
  258. ((desc)->a & 0x0ffff) | \
  259. ((desc)->b & 0xf0000) )
  260. #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
  261. #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
  262. #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
  263. #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
  264. #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
  265. #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
  266. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  267. return -EINVAL;
  268. desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
  269. info.entry_number = idx;
  270. info.base_addr = GET_BASE(desc);
  271. info.limit = GET_LIMIT(desc);
  272. info.seg_32bit = GET_32BIT(desc);
  273. info.contents = GET_CONTENTS(desc);
  274. info.read_exec_only = !GET_WRITABLE(desc);
  275. info.limit_in_pages = GET_LIMIT_PAGES(desc);
  276. info.seg_not_present = !GET_PRESENT(desc);
  277. info.useable = GET_USEABLE(desc);
  278. if (copy_to_user(user_desc, &info, sizeof(info)))
  279. return -EFAULT;
  280. return 0;
  281. }
  282. /*
  283. * Perform set_thread_area on behalf of the traced child.
  284. */
  285. static int
  286. ptrace_set_thread_area(struct task_struct *child,
  287. int idx, struct user_desc __user *user_desc)
  288. {
  289. struct user_desc info;
  290. struct desc_struct *desc;
  291. if (copy_from_user(&info, user_desc, sizeof(info)))
  292. return -EFAULT;
  293. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  294. return -EINVAL;
  295. desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
  296. if (LDT_empty(&info)) {
  297. desc->a = 0;
  298. desc->b = 0;
  299. } else {
  300. desc->a = LDT_entry_a(&info);
  301. desc->b = LDT_entry_b(&info);
  302. }
  303. return 0;
  304. }
  305. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  306. {
  307. struct user * dummy = NULL;
  308. int i, ret;
  309. unsigned long __user *datap = (unsigned long __user *)data;
  310. switch (request) {
  311. /* when I and D space are separate, these will need to be fixed. */
  312. case PTRACE_PEEKTEXT: /* read word at location addr. */
  313. case PTRACE_PEEKDATA: {
  314. unsigned long tmp;
  315. int copied;
  316. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  317. ret = -EIO;
  318. if (copied != sizeof(tmp))
  319. break;
  320. ret = put_user(tmp, datap);
  321. break;
  322. }
  323. /* read the word at location addr in the USER area. */
  324. case PTRACE_PEEKUSR: {
  325. unsigned long tmp;
  326. ret = -EIO;
  327. if ((addr & 3) || addr < 0 ||
  328. addr > sizeof(struct user) - 3)
  329. break;
  330. tmp = 0; /* Default return condition */
  331. if(addr < FRAME_SIZE*sizeof(long))
  332. tmp = getreg(child, addr);
  333. if(addr >= (long) &dummy->u_debugreg[0] &&
  334. addr <= (long) &dummy->u_debugreg[7]){
  335. addr -= (long) &dummy->u_debugreg[0];
  336. addr = addr >> 2;
  337. tmp = child->thread.debugreg[addr];
  338. }
  339. ret = put_user(tmp, datap);
  340. break;
  341. }
  342. /* when I and D space are separate, this will have to be fixed. */
  343. case PTRACE_POKETEXT: /* write the word at location addr. */
  344. case PTRACE_POKEDATA:
  345. ret = 0;
  346. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  347. break;
  348. ret = -EIO;
  349. break;
  350. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  351. ret = -EIO;
  352. if ((addr & 3) || addr < 0 ||
  353. addr > sizeof(struct user) - 3)
  354. break;
  355. if (addr < FRAME_SIZE*sizeof(long)) {
  356. ret = putreg(child, addr, data);
  357. break;
  358. }
  359. /* We need to be very careful here. We implicitly
  360. want to modify a portion of the task_struct, and we
  361. have to be selective about what portions we allow someone
  362. to modify. */
  363. ret = -EIO;
  364. if(addr >= (long) &dummy->u_debugreg[0] &&
  365. addr <= (long) &dummy->u_debugreg[7]){
  366. if(addr == (long) &dummy->u_debugreg[4]) break;
  367. if(addr == (long) &dummy->u_debugreg[5]) break;
  368. if(addr < (long) &dummy->u_debugreg[4] &&
  369. ((unsigned long) data) >= TASK_SIZE-3) break;
  370. /* Sanity-check data. Take one half-byte at once with
  371. * check = (val >> (16 + 4*i)) & 0xf. It contains the
  372. * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
  373. * 2 and 3 are LENi. Given a list of invalid values,
  374. * we do mask |= 1 << invalid_value, so that
  375. * (mask >> check) & 1 is a correct test for invalid
  376. * values.
  377. *
  378. * R/Wi contains the type of the breakpoint /
  379. * watchpoint, LENi contains the length of the watched
  380. * data in the watchpoint case.
  381. *
  382. * The invalid values are:
  383. * - LENi == 0x10 (undefined), so mask |= 0x0f00.
  384. * - R/Wi == 0x10 (break on I/O reads or writes), so
  385. * mask |= 0x4444.
  386. * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
  387. * 0x1110.
  388. *
  389. * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
  390. *
  391. * See the Intel Manual "System Programming Guide",
  392. * 15.2.4
  393. *
  394. * Note that LENi == 0x10 is defined on x86_64 in long
  395. * mode (i.e. even for 32-bit userspace software, but
  396. * 64-bit kernel), so the x86_64 mask value is 0x5454.
  397. * See the AMD manual no. 24593 (AMD64 System
  398. * Programming)*/
  399. if(addr == (long) &dummy->u_debugreg[7]) {
  400. data &= ~DR_CONTROL_RESERVED;
  401. for(i=0; i<4; i++)
  402. if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  403. goto out_tsk;
  404. if (data)
  405. set_tsk_thread_flag(child, TIF_DEBUG);
  406. else
  407. clear_tsk_thread_flag(child, TIF_DEBUG);
  408. }
  409. addr -= (long) &dummy->u_debugreg;
  410. addr = addr >> 2;
  411. child->thread.debugreg[addr] = data;
  412. ret = 0;
  413. }
  414. break;
  415. case PTRACE_SYSEMU: /* continue and stop at next syscall, which will not be executed */
  416. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  417. case PTRACE_CONT: /* restart after signal. */
  418. ret = -EIO;
  419. if (!valid_signal(data))
  420. break;
  421. if (request == PTRACE_SYSEMU) {
  422. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  423. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  424. } else if (request == PTRACE_SYSCALL) {
  425. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  426. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  427. } else {
  428. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  429. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  430. }
  431. child->exit_code = data;
  432. /* make sure the single step bit is not set. */
  433. clear_singlestep(child);
  434. wake_up_process(child);
  435. ret = 0;
  436. break;
  437. /*
  438. * make the child exit. Best I can do is send it a sigkill.
  439. * perhaps it should be put in the status that it wants to
  440. * exit.
  441. */
  442. case PTRACE_KILL:
  443. ret = 0;
  444. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  445. break;
  446. child->exit_code = SIGKILL;
  447. /* make sure the single step bit is not set. */
  448. clear_singlestep(child);
  449. wake_up_process(child);
  450. break;
  451. case PTRACE_SYSEMU_SINGLESTEP: /* Same as SYSEMU, but singlestep if not syscall */
  452. case PTRACE_SINGLESTEP: /* set the trap flag. */
  453. ret = -EIO;
  454. if (!valid_signal(data))
  455. break;
  456. if (request == PTRACE_SYSEMU_SINGLESTEP)
  457. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  458. else
  459. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  460. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  461. set_singlestep(child);
  462. child->exit_code = data;
  463. /* give it a chance to run. */
  464. wake_up_process(child);
  465. ret = 0;
  466. break;
  467. case PTRACE_DETACH:
  468. /* detach a process that was attached. */
  469. ret = ptrace_detach(child, data);
  470. break;
  471. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  472. if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
  473. ret = -EIO;
  474. break;
  475. }
  476. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  477. __put_user(getreg(child, i), datap);
  478. datap++;
  479. }
  480. ret = 0;
  481. break;
  482. }
  483. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  484. unsigned long tmp;
  485. if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
  486. ret = -EIO;
  487. break;
  488. }
  489. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  490. __get_user(tmp, datap);
  491. putreg(child, i, tmp);
  492. datap++;
  493. }
  494. ret = 0;
  495. break;
  496. }
  497. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  498. if (!access_ok(VERIFY_WRITE, datap,
  499. sizeof(struct user_i387_struct))) {
  500. ret = -EIO;
  501. break;
  502. }
  503. ret = 0;
  504. if (!tsk_used_math(child))
  505. init_fpu(child);
  506. get_fpregs((struct user_i387_struct __user *)data, child);
  507. break;
  508. }
  509. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  510. if (!access_ok(VERIFY_READ, datap,
  511. sizeof(struct user_i387_struct))) {
  512. ret = -EIO;
  513. break;
  514. }
  515. set_stopped_child_used_math(child);
  516. set_fpregs(child, (struct user_i387_struct __user *)data);
  517. ret = 0;
  518. break;
  519. }
  520. case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
  521. if (!access_ok(VERIFY_WRITE, datap,
  522. sizeof(struct user_fxsr_struct))) {
  523. ret = -EIO;
  524. break;
  525. }
  526. if (!tsk_used_math(child))
  527. init_fpu(child);
  528. ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
  529. break;
  530. }
  531. case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
  532. if (!access_ok(VERIFY_READ, datap,
  533. sizeof(struct user_fxsr_struct))) {
  534. ret = -EIO;
  535. break;
  536. }
  537. set_stopped_child_used_math(child);
  538. ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
  539. break;
  540. }
  541. case PTRACE_GET_THREAD_AREA:
  542. ret = ptrace_get_thread_area(child, addr,
  543. (struct user_desc __user *) data);
  544. break;
  545. case PTRACE_SET_THREAD_AREA:
  546. ret = ptrace_set_thread_area(child, addr,
  547. (struct user_desc __user *) data);
  548. break;
  549. default:
  550. ret = ptrace_request(child, request, addr, data);
  551. break;
  552. }
  553. out_tsk:
  554. return ret;
  555. }
  556. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  557. {
  558. struct siginfo info;
  559. tsk->thread.trap_no = 1;
  560. tsk->thread.error_code = error_code;
  561. memset(&info, 0, sizeof(info));
  562. info.si_signo = SIGTRAP;
  563. info.si_code = TRAP_BRKPT;
  564. /* User-mode eip? */
  565. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->eip : NULL;
  566. /* Send us the fakey SIGTRAP */
  567. force_sig_info(SIGTRAP, &info, tsk);
  568. }
  569. /* notification of system call entry/exit
  570. * - triggered by current->work.syscall_trace
  571. */
  572. __attribute__((regparm(3)))
  573. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  574. {
  575. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  576. /*
  577. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  578. * interception
  579. */
  580. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  581. int ret = 0;
  582. /* do the secure computing check first */
  583. if (!entryexit)
  584. secure_computing(regs->orig_eax);
  585. if (unlikely(current->audit_context)) {
  586. if (entryexit)
  587. audit_syscall_exit(AUDITSC_RESULT(regs->eax),
  588. regs->eax);
  589. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  590. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  591. * not used, entry.S will call us only on syscall exit, not
  592. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  593. * calling send_sigtrap() on syscall entry.
  594. *
  595. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  596. * is_singlestep is false, despite his name, so we will still do
  597. * the correct thing.
  598. */
  599. else if (is_singlestep)
  600. goto out;
  601. }
  602. if (!(current->ptrace & PT_PTRACED))
  603. goto out;
  604. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  605. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  606. * here. We have to check this and return */
  607. if (is_sysemu && entryexit)
  608. return 0;
  609. /* Fake a debug trap */
  610. if (is_singlestep)
  611. send_sigtrap(current, regs, 0);
  612. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  613. goto out;
  614. /* the 0x80 provides a way for the tracing parent to distinguish
  615. between a syscall stop and SIGTRAP delivery */
  616. /* Note that the debugger could change the result of test_thread_flag!*/
  617. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  618. /*
  619. * this isn't the same as continuing with a signal, but it will do
  620. * for normal use. strace only continues with a signal if the
  621. * stopping signal is not SIGTRAP. -brl
  622. */
  623. if (current->exit_code) {
  624. send_sig(current->exit_code, current, 1);
  625. current->exit_code = 0;
  626. }
  627. ret = is_sysemu;
  628. out:
  629. if (unlikely(current->audit_context) && !entryexit)
  630. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_eax,
  631. regs->ebx, regs->ecx, regs->edx, regs->esi);
  632. if (ret == 0)
  633. return 0;
  634. regs->orig_eax = -1; /* force skip of syscall restarting */
  635. if (unlikely(current->audit_context))
  636. audit_syscall_exit(AUDITSC_RESULT(regs->eax), regs->eax);
  637. return 1;
  638. }