ptrace.c 18 KB

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