ptrace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. /*
  26. * does not yet catch signals sent when the child dies.
  27. * in exit.c or in signal.c.
  28. */
  29. /*
  30. * Determines which flags the user has access to [1 = access, 0 = no access].
  31. */
  32. #define FLAG_MASK_32 ((unsigned long) \
  33. (X86_EFLAGS_CF | X86_EFLAGS_PF | \
  34. X86_EFLAGS_AF | X86_EFLAGS_ZF | \
  35. X86_EFLAGS_SF | X86_EFLAGS_TF | \
  36. X86_EFLAGS_DF | X86_EFLAGS_OF | \
  37. X86_EFLAGS_RF | X86_EFLAGS_AC))
  38. #define FLAG_MASK FLAG_MASK_32
  39. static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  40. {
  41. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  42. regno >>= 2;
  43. if (regno > FS)
  44. --regno;
  45. return &regs->bx + regno;
  46. }
  47. static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
  48. {
  49. /*
  50. * Returning the value truncates it to 16 bits.
  51. */
  52. unsigned int retval;
  53. if (offset != offsetof(struct user_regs_struct, gs))
  54. retval = *pt_regs_access(task_pt_regs(task), offset);
  55. else {
  56. retval = task->thread.gs;
  57. if (task == current)
  58. savesegment(gs, retval);
  59. }
  60. return retval;
  61. }
  62. static int set_segment_reg(struct task_struct *task,
  63. unsigned long offset, u16 value)
  64. {
  65. /*
  66. * The value argument was already truncated to 16 bits.
  67. */
  68. if (value && (value & 3) != 3)
  69. return -EIO;
  70. if (offset != offsetof(struct user_regs_struct, gs))
  71. *pt_regs_access(task_pt_regs(task), offset) = value;
  72. else {
  73. task->thread.gs = value;
  74. if (task == current)
  75. /*
  76. * The user-mode %gs is not affected by
  77. * kernel entry, so we must update the CPU.
  78. */
  79. loadsegment(gs, value);
  80. }
  81. return 0;
  82. }
  83. static unsigned long get_flags(struct task_struct *task)
  84. {
  85. unsigned long retval = task_pt_regs(task)->flags;
  86. /*
  87. * If the debugger set TF, hide it from the readout.
  88. */
  89. if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  90. retval &= ~X86_EFLAGS_TF;
  91. return retval;
  92. }
  93. static int set_flags(struct task_struct *task, unsigned long value)
  94. {
  95. struct pt_regs *regs = task_pt_regs(task);
  96. /*
  97. * If the user value contains TF, mark that
  98. * it was not "us" (the debugger) that set it.
  99. * If not, make sure it stays set if we had.
  100. */
  101. if (value & X86_EFLAGS_TF)
  102. clear_tsk_thread_flag(task, TIF_FORCED_TF);
  103. else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
  104. value |= X86_EFLAGS_TF;
  105. regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
  106. return 0;
  107. }
  108. static int putreg(struct task_struct *child,
  109. unsigned long offset, unsigned long value)
  110. {
  111. switch (offset) {
  112. case offsetof(struct user_regs_struct, cs):
  113. case offsetof(struct user_regs_struct, ds):
  114. case offsetof(struct user_regs_struct, es):
  115. case offsetof(struct user_regs_struct, fs):
  116. case offsetof(struct user_regs_struct, gs):
  117. case offsetof(struct user_regs_struct, ss):
  118. return set_segment_reg(child, offset, value);
  119. case offsetof(struct user_regs_struct, flags):
  120. return set_flags(child, value);
  121. }
  122. *pt_regs_access(task_pt_regs(child), offset) = value;
  123. return 0;
  124. }
  125. static unsigned long getreg(struct task_struct *task, unsigned long offset)
  126. {
  127. switch (offset) {
  128. case offsetof(struct user_regs_struct, cs):
  129. case offsetof(struct user_regs_struct, ds):
  130. case offsetof(struct user_regs_struct, es):
  131. case offsetof(struct user_regs_struct, fs):
  132. case offsetof(struct user_regs_struct, gs):
  133. case offsetof(struct user_regs_struct, ss):
  134. return get_segment_reg(task, offset);
  135. case offsetof(struct user_regs_struct, flags):
  136. return get_flags(task);
  137. }
  138. return *pt_regs_access(task_pt_regs(task), offset);
  139. }
  140. /*
  141. * This function is trivial and will be inlined by the compiler.
  142. * Having it separates the implementation details of debug
  143. * registers from the interface details of ptrace.
  144. */
  145. static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
  146. {
  147. switch (n) {
  148. case 0: return child->thread.debugreg0;
  149. case 1: return child->thread.debugreg1;
  150. case 2: return child->thread.debugreg2;
  151. case 3: return child->thread.debugreg3;
  152. case 6: return child->thread.debugreg6;
  153. case 7: return child->thread.debugreg7;
  154. }
  155. return 0;
  156. }
  157. static int ptrace_set_debugreg(struct task_struct *child,
  158. int n, unsigned long data)
  159. {
  160. int i;
  161. if (unlikely(n == 4 || n == 5))
  162. return -EIO;
  163. if (n < 4 && unlikely(data >= TASK_SIZE - 3))
  164. return -EIO;
  165. switch (n) {
  166. case 0: child->thread.debugreg0 = data; break;
  167. case 1: child->thread.debugreg1 = data; break;
  168. case 2: child->thread.debugreg2 = data; break;
  169. case 3: child->thread.debugreg3 = data; break;
  170. case 6:
  171. child->thread.debugreg6 = data;
  172. break;
  173. case 7:
  174. /*
  175. * Sanity-check data. Take one half-byte at once with
  176. * check = (val >> (16 + 4*i)) & 0xf. It contains the
  177. * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
  178. * 2 and 3 are LENi. Given a list of invalid values,
  179. * we do mask |= 1 << invalid_value, so that
  180. * (mask >> check) & 1 is a correct test for invalid
  181. * values.
  182. *
  183. * R/Wi contains the type of the breakpoint /
  184. * watchpoint, LENi contains the length of the watched
  185. * data in the watchpoint case.
  186. *
  187. * The invalid values are:
  188. * - LENi == 0x10 (undefined), so mask |= 0x0f00.
  189. * - R/Wi == 0x10 (break on I/O reads or writes), so
  190. * mask |= 0x4444.
  191. * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
  192. * 0x1110.
  193. *
  194. * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
  195. *
  196. * See the Intel Manual "System Programming Guide",
  197. * 15.2.4
  198. *
  199. * Note that LENi == 0x10 is defined on x86_64 in long
  200. * mode (i.e. even for 32-bit userspace software, but
  201. * 64-bit kernel), so the x86_64 mask value is 0x5454.
  202. * See the AMD manual no. 24593 (AMD64 System Programming)
  203. */
  204. data &= ~DR_CONTROL_RESERVED;
  205. for (i = 0; i < 4; i++)
  206. if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  207. return -EIO;
  208. child->thread.debugreg7 = data;
  209. if (data)
  210. set_tsk_thread_flag(child, TIF_DEBUG);
  211. else
  212. clear_tsk_thread_flag(child, TIF_DEBUG);
  213. break;
  214. }
  215. return 0;
  216. }
  217. /*
  218. * Called by kernel/ptrace.c when detaching..
  219. *
  220. * Make sure the single step bit is not set.
  221. */
  222. void ptrace_disable(struct task_struct *child)
  223. {
  224. user_disable_single_step(child);
  225. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  226. }
  227. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  228. {
  229. struct user * dummy = NULL;
  230. int i, ret;
  231. unsigned long __user *datap = (unsigned long __user *)data;
  232. switch (request) {
  233. /* when I and D space are separate, these will need to be fixed. */
  234. case PTRACE_PEEKTEXT: /* read word at location addr. */
  235. case PTRACE_PEEKDATA:
  236. ret = generic_ptrace_peekdata(child, addr, data);
  237. break;
  238. /* read the word at location addr in the USER area. */
  239. case PTRACE_PEEKUSR: {
  240. unsigned long tmp;
  241. ret = -EIO;
  242. if ((addr & 3) || addr < 0 ||
  243. addr > sizeof(struct user) - 3)
  244. break;
  245. tmp = 0; /* Default return condition */
  246. if(addr < FRAME_SIZE*sizeof(long))
  247. tmp = getreg(child, addr);
  248. if(addr >= (long) &dummy->u_debugreg[0] &&
  249. addr <= (long) &dummy->u_debugreg[7]){
  250. addr -= (long) &dummy->u_debugreg[0];
  251. addr = addr >> 2;
  252. tmp = ptrace_get_debugreg(child, addr);
  253. }
  254. ret = put_user(tmp, datap);
  255. break;
  256. }
  257. /* when I and D space are separate, this will have to be fixed. */
  258. case PTRACE_POKETEXT: /* write the word at location addr. */
  259. case PTRACE_POKEDATA:
  260. ret = generic_ptrace_pokedata(child, addr, data);
  261. break;
  262. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  263. ret = -EIO;
  264. if ((addr & 3) || addr < 0 ||
  265. addr > sizeof(struct user) - 3)
  266. break;
  267. if (addr < FRAME_SIZE*sizeof(long)) {
  268. ret = putreg(child, addr, data);
  269. break;
  270. }
  271. /* We need to be very careful here. We implicitly
  272. want to modify a portion of the task_struct, and we
  273. have to be selective about what portions we allow someone
  274. to modify. */
  275. ret = -EIO;
  276. if(addr >= (long) &dummy->u_debugreg[0] &&
  277. addr <= (long) &dummy->u_debugreg[7]){
  278. addr -= (long) &dummy->u_debugreg;
  279. addr = addr >> 2;
  280. ret = ptrace_set_debugreg(child, addr, data);
  281. }
  282. break;
  283. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  284. if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
  285. ret = -EIO;
  286. break;
  287. }
  288. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  289. __put_user(getreg(child, i), datap);
  290. datap++;
  291. }
  292. ret = 0;
  293. break;
  294. }
  295. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  296. unsigned long tmp;
  297. if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
  298. ret = -EIO;
  299. break;
  300. }
  301. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  302. __get_user(tmp, datap);
  303. putreg(child, i, tmp);
  304. datap++;
  305. }
  306. ret = 0;
  307. break;
  308. }
  309. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  310. if (!access_ok(VERIFY_WRITE, datap,
  311. sizeof(struct user_i387_struct))) {
  312. ret = -EIO;
  313. break;
  314. }
  315. ret = 0;
  316. if (!tsk_used_math(child))
  317. init_fpu(child);
  318. get_fpregs((struct user_i387_struct __user *)data, child);
  319. break;
  320. }
  321. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  322. if (!access_ok(VERIFY_READ, datap,
  323. sizeof(struct user_i387_struct))) {
  324. ret = -EIO;
  325. break;
  326. }
  327. set_stopped_child_used_math(child);
  328. set_fpregs(child, (struct user_i387_struct __user *)data);
  329. ret = 0;
  330. break;
  331. }
  332. case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
  333. if (!access_ok(VERIFY_WRITE, datap,
  334. sizeof(struct user_fxsr_struct))) {
  335. ret = -EIO;
  336. break;
  337. }
  338. if (!tsk_used_math(child))
  339. init_fpu(child);
  340. ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
  341. break;
  342. }
  343. case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
  344. if (!access_ok(VERIFY_READ, datap,
  345. sizeof(struct user_fxsr_struct))) {
  346. ret = -EIO;
  347. break;
  348. }
  349. set_stopped_child_used_math(child);
  350. ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
  351. break;
  352. }
  353. case PTRACE_GET_THREAD_AREA:
  354. if (addr < 0)
  355. return -EIO;
  356. ret = do_get_thread_area(child, addr,
  357. (struct user_desc __user *) data);
  358. break;
  359. case PTRACE_SET_THREAD_AREA:
  360. if (addr < 0)
  361. return -EIO;
  362. ret = do_set_thread_area(child, addr,
  363. (struct user_desc __user *) data, 0);
  364. break;
  365. default:
  366. ret = ptrace_request(child, request, addr, data);
  367. break;
  368. }
  369. return ret;
  370. }
  371. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  372. {
  373. struct siginfo info;
  374. tsk->thread.trap_no = 1;
  375. tsk->thread.error_code = error_code;
  376. memset(&info, 0, sizeof(info));
  377. info.si_signo = SIGTRAP;
  378. info.si_code = TRAP_BRKPT;
  379. /* User-mode ip? */
  380. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  381. /* Send us the fake SIGTRAP */
  382. force_sig_info(SIGTRAP, &info, tsk);
  383. }
  384. /* notification of system call entry/exit
  385. * - triggered by current->work.syscall_trace
  386. */
  387. __attribute__((regparm(3)))
  388. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  389. {
  390. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  391. /*
  392. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  393. * interception
  394. */
  395. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  396. int ret = 0;
  397. /* do the secure computing check first */
  398. if (!entryexit)
  399. secure_computing(regs->orig_ax);
  400. if (unlikely(current->audit_context)) {
  401. if (entryexit)
  402. audit_syscall_exit(AUDITSC_RESULT(regs->ax),
  403. regs->ax);
  404. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  405. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  406. * not used, entry.S will call us only on syscall exit, not
  407. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  408. * calling send_sigtrap() on syscall entry.
  409. *
  410. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  411. * is_singlestep is false, despite his name, so we will still do
  412. * the correct thing.
  413. */
  414. else if (is_singlestep)
  415. goto out;
  416. }
  417. if (!(current->ptrace & PT_PTRACED))
  418. goto out;
  419. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  420. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  421. * here. We have to check this and return */
  422. if (is_sysemu && entryexit)
  423. return 0;
  424. /* Fake a debug trap */
  425. if (is_singlestep)
  426. send_sigtrap(current, regs, 0);
  427. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  428. goto out;
  429. /* the 0x80 provides a way for the tracing parent to distinguish
  430. between a syscall stop and SIGTRAP delivery */
  431. /* Note that the debugger could change the result of test_thread_flag!*/
  432. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  433. /*
  434. * this isn't the same as continuing with a signal, but it will do
  435. * for normal use. strace only continues with a signal if the
  436. * stopping signal is not SIGTRAP. -brl
  437. */
  438. if (current->exit_code) {
  439. send_sig(current->exit_code, current, 1);
  440. current->exit_code = 0;
  441. }
  442. ret = is_sysemu;
  443. out:
  444. if (unlikely(current->audit_context) && !entryexit)
  445. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
  446. regs->bx, regs->cx, regs->dx, regs->si);
  447. if (ret == 0)
  448. return 0;
  449. regs->orig_ax = -1; /* force skip of syscall restarting */
  450. if (unlikely(current->audit_context))
  451. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  452. return 1;
  453. }