ptrace.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. #include <asm/prctl.h>
  26. #include <asm/proto.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. */
  34. #define FLAG_MASK_32 ((unsigned long) \
  35. (X86_EFLAGS_CF | X86_EFLAGS_PF | \
  36. X86_EFLAGS_AF | X86_EFLAGS_ZF | \
  37. X86_EFLAGS_SF | X86_EFLAGS_TF | \
  38. X86_EFLAGS_DF | X86_EFLAGS_OF | \
  39. X86_EFLAGS_RF | X86_EFLAGS_AC))
  40. /*
  41. * Determines whether a value may be installed in a segment register.
  42. */
  43. static inline bool invalid_selector(u16 value)
  44. {
  45. return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
  46. }
  47. #ifdef CONFIG_X86_32
  48. #define FLAG_MASK FLAG_MASK_32
  49. static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  50. {
  51. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  52. regno >>= 2;
  53. if (regno > FS)
  54. --regno;
  55. return &regs->bx + regno;
  56. }
  57. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  58. {
  59. /*
  60. * Returning the value truncates it to 16 bits.
  61. */
  62. unsigned int retval;
  63. if (offset != offsetof(struct user_regs_struct, gs))
  64. retval = *pt_regs_access(task_pt_regs(task), offset);
  65. else {
  66. retval = task->thread.gs;
  67. if (task == current)
  68. savesegment(gs, retval);
  69. }
  70. return retval;
  71. }
  72. static int set_segment_reg(struct task_struct *task,
  73. unsigned long offset, u16 value)
  74. {
  75. /*
  76. * The value argument was already truncated to 16 bits.
  77. */
  78. if (invalid_selector(value))
  79. return -EIO;
  80. if (offset != offsetof(struct user_regs_struct, gs))
  81. *pt_regs_access(task_pt_regs(task), offset) = value;
  82. else {
  83. task->thread.gs = value;
  84. if (task == current)
  85. /*
  86. * The user-mode %gs is not affected by
  87. * kernel entry, so we must update the CPU.
  88. */
  89. loadsegment(gs, value);
  90. }
  91. return 0;
  92. }
  93. static unsigned long debugreg_addr_limit(struct task_struct *task)
  94. {
  95. return TASK_SIZE - 3;
  96. }
  97. #else /* CONFIG_X86_64 */
  98. #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
  99. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
  100. {
  101. BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
  102. return &regs->r15 + (offset / sizeof(regs->r15));
  103. }
  104. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  105. {
  106. /*
  107. * Returning the value truncates it to 16 bits.
  108. */
  109. unsigned int seg;
  110. switch (offset) {
  111. case offsetof(struct user_regs_struct, fs):
  112. if (task == current) {
  113. /* Older gas can't assemble movq %?s,%r?? */
  114. asm("movl %%fs,%0" : "=r" (seg));
  115. return seg;
  116. }
  117. return task->thread.fsindex;
  118. case offsetof(struct user_regs_struct, gs):
  119. if (task == current) {
  120. asm("movl %%gs,%0" : "=r" (seg));
  121. return seg;
  122. }
  123. return task->thread.gsindex;
  124. case offsetof(struct user_regs_struct, ds):
  125. if (task == current) {
  126. asm("movl %%ds,%0" : "=r" (seg));
  127. return seg;
  128. }
  129. return task->thread.ds;
  130. case offsetof(struct user_regs_struct, es):
  131. if (task == current) {
  132. asm("movl %%es,%0" : "=r" (seg));
  133. return seg;
  134. }
  135. return task->thread.es;
  136. case offsetof(struct user_regs_struct, cs):
  137. case offsetof(struct user_regs_struct, ss):
  138. break;
  139. }
  140. return *pt_regs_access(task_pt_regs(task), offset);
  141. }
  142. static int set_segment_reg(struct task_struct *task,
  143. unsigned long offset, u16 value)
  144. {
  145. /*
  146. * The value argument was already truncated to 16 bits.
  147. */
  148. if (invalid_selector(value))
  149. return -EIO;
  150. switch (offset) {
  151. case offsetof(struct user_regs_struct,fs):
  152. /*
  153. * If this is setting fs as for normal 64-bit use but
  154. * setting fs_base has implicitly changed it, leave it.
  155. */
  156. if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
  157. task->thread.fs != 0) ||
  158. (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
  159. task->thread.fs == 0))
  160. break;
  161. task->thread.fsindex = value;
  162. if (task == current)
  163. loadsegment(fs, task->thread.fsindex);
  164. break;
  165. case offsetof(struct user_regs_struct,gs):
  166. /*
  167. * If this is setting gs as for normal 64-bit use but
  168. * setting gs_base has implicitly changed it, leave it.
  169. */
  170. if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
  171. task->thread.gs != 0) ||
  172. (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
  173. task->thread.gs == 0))
  174. break;
  175. task->thread.gsindex = value;
  176. if (task == current)
  177. load_gs_index(task->thread.gsindex);
  178. break;
  179. case offsetof(struct user_regs_struct,ds):
  180. task->thread.ds = value;
  181. if (task == current)
  182. loadsegment(ds, task->thread.ds);
  183. break;
  184. case offsetof(struct user_regs_struct,es):
  185. task->thread.es = value;
  186. if (task == current)
  187. loadsegment(es, task->thread.es);
  188. break;
  189. /*
  190. * Can't actually change these in 64-bit mode.
  191. */
  192. case offsetof(struct user_regs_struct,cs):
  193. #ifdef CONFIG_IA32_EMULATION
  194. if (test_tsk_thread_flag(task, TIF_IA32))
  195. task_pt_regs(task)->cs = value;
  196. break;
  197. #endif
  198. case offsetof(struct user_regs_struct,ss):
  199. #ifdef CONFIG_IA32_EMULATION
  200. if (test_tsk_thread_flag(task, TIF_IA32))
  201. task_pt_regs(task)->ss = value;
  202. break;
  203. #endif
  204. }
  205. return 0;
  206. }
  207. static unsigned long debugreg_addr_limit(struct task_struct *task)
  208. {
  209. #ifdef CONFIG_IA32_EMULATION
  210. if (test_tsk_thread_flag(task, TIF_IA32))
  211. return IA32_PAGE_OFFSET - 3;
  212. #endif
  213. return TASK_SIZE64 - 7;
  214. }
  215. #endif /* CONFIG_X86_32 */
  216. static unsigned long get_flags(struct task_struct *task)
  217. {
  218. unsigned long retval = task_pt_regs(task)->flags;
  219. /*
  220. * If the debugger set TF, hide it from the readout.
  221. */
  222. if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  223. retval &= ~X86_EFLAGS_TF;
  224. return retval;
  225. }
  226. static int set_flags(struct task_struct *task, unsigned long value)
  227. {
  228. struct pt_regs *regs = task_pt_regs(task);
  229. /*
  230. * If the user value contains TF, mark that
  231. * it was not "us" (the debugger) that set it.
  232. * If not, make sure it stays set if we had.
  233. */
  234. if (value & X86_EFLAGS_TF)
  235. clear_tsk_thread_flag(task, TIF_FORCED_TF);
  236. else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  237. value |= X86_EFLAGS_TF;
  238. regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
  239. return 0;
  240. }
  241. static int putreg(struct task_struct *child,
  242. unsigned long offset, unsigned long value)
  243. {
  244. switch (offset) {
  245. case offsetof(struct user_regs_struct, cs):
  246. case offsetof(struct user_regs_struct, ds):
  247. case offsetof(struct user_regs_struct, es):
  248. case offsetof(struct user_regs_struct, fs):
  249. case offsetof(struct user_regs_struct, gs):
  250. case offsetof(struct user_regs_struct, ss):
  251. return set_segment_reg(child, offset, value);
  252. case offsetof(struct user_regs_struct, flags):
  253. return set_flags(child, value);
  254. #ifdef CONFIG_X86_64
  255. case offsetof(struct user_regs_struct,fs_base):
  256. if (value >= TASK_SIZE_OF(child))
  257. return -EIO;
  258. /*
  259. * When changing the segment base, use do_arch_prctl
  260. * to set either thread.fs or thread.fsindex and the
  261. * corresponding GDT slot.
  262. */
  263. if (child->thread.fs != value)
  264. return do_arch_prctl(child, ARCH_SET_FS, value);
  265. return 0;
  266. case offsetof(struct user_regs_struct,gs_base):
  267. /*
  268. * Exactly the same here as the %fs handling above.
  269. */
  270. if (value >= TASK_SIZE_OF(child))
  271. return -EIO;
  272. if (child->thread.gs != value)
  273. return do_arch_prctl(child, ARCH_SET_GS, value);
  274. return 0;
  275. #endif
  276. }
  277. *pt_regs_access(task_pt_regs(child), offset) = value;
  278. return 0;
  279. }
  280. static unsigned long getreg(struct task_struct *task, unsigned long offset)
  281. {
  282. switch (offset) {
  283. case offsetof(struct user_regs_struct, cs):
  284. case offsetof(struct user_regs_struct, ds):
  285. case offsetof(struct user_regs_struct, es):
  286. case offsetof(struct user_regs_struct, fs):
  287. case offsetof(struct user_regs_struct, gs):
  288. case offsetof(struct user_regs_struct, ss):
  289. return get_segment_reg(task, offset);
  290. case offsetof(struct user_regs_struct, flags):
  291. return get_flags(task);
  292. #ifdef CONFIG_X86_64
  293. case offsetof(struct user_regs_struct, fs_base): {
  294. /*
  295. * do_arch_prctl may have used a GDT slot instead of
  296. * the MSR. To userland, it appears the same either
  297. * way, except the %fs segment selector might not be 0.
  298. */
  299. unsigned int seg = task->thread.fsindex;
  300. if (task->thread.fs != 0)
  301. return task->thread.fs;
  302. if (task == current)
  303. asm("movl %%fs,%0" : "=r" (seg));
  304. if (seg != FS_TLS_SEL)
  305. return 0;
  306. return get_desc_base(&task->thread.tls_array[FS_TLS]);
  307. }
  308. case offsetof(struct user_regs_struct, gs_base): {
  309. /*
  310. * Exactly the same here as the %fs handling above.
  311. */
  312. unsigned int seg = task->thread.gsindex;
  313. if (task->thread.gs != 0)
  314. return task->thread.gs;
  315. if (task == current)
  316. asm("movl %%gs,%0" : "=r" (seg));
  317. if (seg != GS_TLS_SEL)
  318. return 0;
  319. return get_desc_base(&task->thread.tls_array[GS_TLS]);
  320. }
  321. #endif
  322. }
  323. return *pt_regs_access(task_pt_regs(task), offset);
  324. }
  325. /*
  326. * This function is trivial and will be inlined by the compiler.
  327. * Having it separates the implementation details of debug
  328. * registers from the interface details of ptrace.
  329. */
  330. static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
  331. {
  332. switch (n) {
  333. case 0: return child->thread.debugreg0;
  334. case 1: return child->thread.debugreg1;
  335. case 2: return child->thread.debugreg2;
  336. case 3: return child->thread.debugreg3;
  337. case 6: return child->thread.debugreg6;
  338. case 7: return child->thread.debugreg7;
  339. }
  340. return 0;
  341. }
  342. static int ptrace_set_debugreg(struct task_struct *child,
  343. int n, unsigned long data)
  344. {
  345. int i;
  346. if (unlikely(n == 4 || n == 5))
  347. return -EIO;
  348. if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
  349. return -EIO;
  350. switch (n) {
  351. case 0: child->thread.debugreg0 = data; break;
  352. case 1: child->thread.debugreg1 = data; break;
  353. case 2: child->thread.debugreg2 = data; break;
  354. case 3: child->thread.debugreg3 = data; break;
  355. case 6:
  356. if ((data & ~0xffffffffUL) != 0)
  357. return -EIO;
  358. child->thread.debugreg6 = data;
  359. break;
  360. case 7:
  361. /*
  362. * Sanity-check data. Take one half-byte at once with
  363. * check = (val >> (16 + 4*i)) & 0xf. It contains the
  364. * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
  365. * 2 and 3 are LENi. Given a list of invalid values,
  366. * we do mask |= 1 << invalid_value, so that
  367. * (mask >> check) & 1 is a correct test for invalid
  368. * values.
  369. *
  370. * R/Wi contains the type of the breakpoint /
  371. * watchpoint, LENi contains the length of the watched
  372. * data in the watchpoint case.
  373. *
  374. * The invalid values are:
  375. * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
  376. * - R/Wi == 0x10 (break on I/O reads or writes), so
  377. * mask |= 0x4444.
  378. * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
  379. * 0x1110.
  380. *
  381. * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
  382. *
  383. * See the Intel Manual "System Programming Guide",
  384. * 15.2.4
  385. *
  386. * Note that LENi == 0x10 is defined on x86_64 in long
  387. * mode (i.e. even for 32-bit userspace software, but
  388. * 64-bit kernel), so the x86_64 mask value is 0x5454.
  389. * See the AMD manual no. 24593 (AMD64 System Programming)
  390. */
  391. #ifdef CONFIG_X86_32
  392. #define DR7_MASK 0x5f54
  393. #else
  394. #define DR7_MASK 0x5554
  395. #endif
  396. data &= ~DR_CONTROL_RESERVED;
  397. for (i = 0; i < 4; i++)
  398. if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  399. return -EIO;
  400. child->thread.debugreg7 = data;
  401. if (data)
  402. set_tsk_thread_flag(child, TIF_DEBUG);
  403. else
  404. clear_tsk_thread_flag(child, TIF_DEBUG);
  405. break;
  406. }
  407. return 0;
  408. }
  409. /*
  410. * Called by kernel/ptrace.c when detaching..
  411. *
  412. * Make sure the single step bit is not set.
  413. */
  414. void ptrace_disable(struct task_struct *child)
  415. {
  416. user_disable_single_step(child);
  417. #ifdef TIF_SYSCALL_EMU
  418. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  419. #endif
  420. }
  421. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  422. {
  423. int i, ret;
  424. unsigned long __user *datap = (unsigned long __user *)data;
  425. switch (request) {
  426. /* when I and D space are separate, these will need to be fixed. */
  427. case PTRACE_PEEKTEXT: /* read word at location addr. */
  428. case PTRACE_PEEKDATA:
  429. ret = generic_ptrace_peekdata(child, addr, data);
  430. break;
  431. /* read the word at location addr in the USER area. */
  432. case PTRACE_PEEKUSR: {
  433. unsigned long tmp;
  434. ret = -EIO;
  435. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  436. addr >= sizeof(struct user))
  437. break;
  438. tmp = 0; /* Default return condition */
  439. if (addr < sizeof(struct user_regs_struct))
  440. tmp = getreg(child, addr);
  441. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  442. addr <= offsetof(struct user, u_debugreg[7])) {
  443. addr -= offsetof(struct user, u_debugreg[0]);
  444. tmp = ptrace_get_debugreg(child, addr / sizeof(data));
  445. }
  446. ret = put_user(tmp, datap);
  447. break;
  448. }
  449. /* when I and D space are separate, this will have to be fixed. */
  450. case PTRACE_POKETEXT: /* write the word at location addr. */
  451. case PTRACE_POKEDATA:
  452. ret = generic_ptrace_pokedata(child, addr, data);
  453. break;
  454. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  455. ret = -EIO;
  456. if ((addr & (sizeof(data) - 1)) || addr < 0 ||
  457. addr >= sizeof(struct user))
  458. break;
  459. if (addr < sizeof(struct user_regs_struct))
  460. ret = putreg(child, addr, data);
  461. else if (addr >= offsetof(struct user, u_debugreg[0]) &&
  462. addr <= offsetof(struct user, u_debugreg[7])) {
  463. addr -= offsetof(struct user, u_debugreg[0]);
  464. ret = ptrace_set_debugreg(child,
  465. addr / sizeof(data), data);
  466. }
  467. break;
  468. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  469. if (!access_ok(VERIFY_WRITE, datap, sizeof(struct user_regs_struct))) {
  470. ret = -EIO;
  471. break;
  472. }
  473. for (i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long)) {
  474. __put_user(getreg(child, i), datap);
  475. datap++;
  476. }
  477. ret = 0;
  478. break;
  479. }
  480. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  481. unsigned long tmp;
  482. if (!access_ok(VERIFY_READ, datap, sizeof(struct user_regs_struct))) {
  483. ret = -EIO;
  484. break;
  485. }
  486. for (i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long)) {
  487. __get_user(tmp, datap);
  488. putreg(child, i, tmp);
  489. datap++;
  490. }
  491. ret = 0;
  492. break;
  493. }
  494. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  495. if (!access_ok(VERIFY_WRITE, datap,
  496. sizeof(struct user_i387_struct))) {
  497. ret = -EIO;
  498. break;
  499. }
  500. ret = 0;
  501. if (!tsk_used_math(child))
  502. init_fpu(child);
  503. get_fpregs((struct user_i387_struct __user *)data, child);
  504. break;
  505. }
  506. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  507. if (!access_ok(VERIFY_READ, datap,
  508. sizeof(struct user_i387_struct))) {
  509. ret = -EIO;
  510. break;
  511. }
  512. set_stopped_child_used_math(child);
  513. set_fpregs(child, (struct user_i387_struct __user *)data);
  514. ret = 0;
  515. break;
  516. }
  517. #ifdef CONFIG_X86_32
  518. case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
  519. if (!access_ok(VERIFY_WRITE, datap,
  520. sizeof(struct user_fxsr_struct))) {
  521. ret = -EIO;
  522. break;
  523. }
  524. if (!tsk_used_math(child))
  525. init_fpu(child);
  526. ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
  527. break;
  528. }
  529. case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
  530. if (!access_ok(VERIFY_READ, datap,
  531. sizeof(struct user_fxsr_struct))) {
  532. ret = -EIO;
  533. break;
  534. }
  535. set_stopped_child_used_math(child);
  536. ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
  537. break;
  538. }
  539. #endif
  540. #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
  541. case PTRACE_GET_THREAD_AREA:
  542. if (addr < 0)
  543. return -EIO;
  544. ret = do_get_thread_area(child, addr,
  545. (struct user_desc __user *) data);
  546. break;
  547. case PTRACE_SET_THREAD_AREA:
  548. if (addr < 0)
  549. return -EIO;
  550. ret = do_set_thread_area(child, addr,
  551. (struct user_desc __user *) data, 0);
  552. break;
  553. #endif
  554. #ifdef CONFIG_X86_64
  555. /* normal 64bit interface to access TLS data.
  556. Works just like arch_prctl, except that the arguments
  557. are reversed. */
  558. case PTRACE_ARCH_PRCTL:
  559. ret = do_arch_prctl(child, data, addr);
  560. break;
  561. #endif
  562. default:
  563. ret = ptrace_request(child, request, addr, data);
  564. break;
  565. }
  566. return ret;
  567. }
  568. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  569. {
  570. struct siginfo info;
  571. tsk->thread.trap_no = 1;
  572. tsk->thread.error_code = error_code;
  573. memset(&info, 0, sizeof(info));
  574. info.si_signo = SIGTRAP;
  575. info.si_code = TRAP_BRKPT;
  576. /* User-mode ip? */
  577. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  578. /* Send us the fake SIGTRAP */
  579. force_sig_info(SIGTRAP, &info, tsk);
  580. }
  581. /* notification of system call entry/exit
  582. * - triggered by current->work.syscall_trace
  583. */
  584. __attribute__((regparm(3)))
  585. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  586. {
  587. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  588. /*
  589. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  590. * interception
  591. */
  592. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  593. int ret = 0;
  594. /* do the secure computing check first */
  595. if (!entryexit)
  596. secure_computing(regs->orig_ax);
  597. if (unlikely(current->audit_context)) {
  598. if (entryexit)
  599. audit_syscall_exit(AUDITSC_RESULT(regs->ax),
  600. regs->ax);
  601. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  602. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  603. * not used, entry.S will call us only on syscall exit, not
  604. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  605. * calling send_sigtrap() on syscall entry.
  606. *
  607. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  608. * is_singlestep is false, despite his name, so we will still do
  609. * the correct thing.
  610. */
  611. else if (is_singlestep)
  612. goto out;
  613. }
  614. if (!(current->ptrace & PT_PTRACED))
  615. goto out;
  616. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  617. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  618. * here. We have to check this and return */
  619. if (is_sysemu && entryexit)
  620. return 0;
  621. /* Fake a debug trap */
  622. if (is_singlestep)
  623. send_sigtrap(current, regs, 0);
  624. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  625. goto out;
  626. /* the 0x80 provides a way for the tracing parent to distinguish
  627. between a syscall stop and SIGTRAP delivery */
  628. /* Note that the debugger could change the result of test_thread_flag!*/
  629. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  630. /*
  631. * this isn't the same as continuing with a signal, but it will do
  632. * for normal use. strace only continues with a signal if the
  633. * stopping signal is not SIGTRAP. -brl
  634. */
  635. if (current->exit_code) {
  636. send_sig(current->exit_code, current, 1);
  637. current->exit_code = 0;
  638. }
  639. ret = is_sysemu;
  640. out:
  641. if (unlikely(current->audit_context) && !entryexit)
  642. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
  643. regs->bx, regs->cx, regs->dx, regs->si);
  644. if (ret == 0)
  645. return 0;
  646. regs->orig_ax = -1; /* force skip of syscall restarting */
  647. if (unlikely(current->audit_context))
  648. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  649. return 1;
  650. }