ptrace.c 19 KB

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