ptrace.c 19 KB

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