ptrace_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* By Ross Biro 1/23/92 */
  2. /*
  3. * Pentium III FXSR, SSE support
  4. * Gareth Hughes <gareth@valinux.com>, May 2000
  5. *
  6. * x86-64 port 2000-2002 Andi Kleen
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/mm.h>
  11. #include <linux/smp.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/prctl.h>
  24. #include <asm/i387.h>
  25. #include <asm/debugreg.h>
  26. #include <asm/ldt.h>
  27. #include <asm/desc.h>
  28. #include <asm/proto.h>
  29. #include <asm/ia32.h>
  30. /*
  31. * does not yet catch signals sent when the child dies.
  32. * in exit.c or in signal.c.
  33. */
  34. /*
  35. * Determines which flags the user has access to [1 = access, 0 = no access].
  36. * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
  37. * Also masks reserved bits (63-22, 15, 5, 3, 1).
  38. */
  39. #define FLAG_MASK 0x54dd5UL
  40. /*
  41. * eflags and offset of eflags on child stack..
  42. */
  43. #define EFLAGS offsetof(struct pt_regs, eflags)
  44. #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
  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 unsigned long get_stack_long(struct task_struct *task, int offset)
  52. {
  53. unsigned char *stack;
  54. stack = (unsigned char *)task->thread.rsp0;
  55. stack += offset;
  56. return (*((unsigned long *)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 long put_stack_long(struct task_struct *task, int offset,
  65. unsigned long data)
  66. {
  67. unsigned char * stack;
  68. stack = (unsigned char *) task->thread.rsp0;
  69. stack += offset;
  70. *(unsigned long *) stack = data;
  71. return 0;
  72. }
  73. /*
  74. * Called by kernel/ptrace.c when detaching..
  75. *
  76. * Make sure the single step bit is not set.
  77. */
  78. void ptrace_disable(struct task_struct *child)
  79. {
  80. user_disable_single_step(child);
  81. }
  82. static int putreg(struct task_struct *child,
  83. unsigned long regno, unsigned long value)
  84. {
  85. unsigned long tmp;
  86. switch (regno) {
  87. case offsetof(struct user_regs_struct,fs):
  88. if (value && (value & 3) != 3)
  89. return -EIO;
  90. child->thread.fsindex = value & 0xffff;
  91. return 0;
  92. case offsetof(struct user_regs_struct,gs):
  93. if (value && (value & 3) != 3)
  94. return -EIO;
  95. child->thread.gsindex = value & 0xffff;
  96. return 0;
  97. case offsetof(struct user_regs_struct,ds):
  98. if (value && (value & 3) != 3)
  99. return -EIO;
  100. child->thread.ds = value & 0xffff;
  101. return 0;
  102. case offsetof(struct user_regs_struct,es):
  103. if (value && (value & 3) != 3)
  104. return -EIO;
  105. child->thread.es = value & 0xffff;
  106. return 0;
  107. case offsetof(struct user_regs_struct,ss):
  108. if ((value & 3) != 3)
  109. return -EIO;
  110. value &= 0xffff;
  111. return 0;
  112. case offsetof(struct user_regs_struct,fs_base):
  113. if (value >= TASK_SIZE_OF(child))
  114. return -EIO;
  115. /*
  116. * When changing the segment base, use do_arch_prctl
  117. * to set either thread.fs or thread.fsindex and the
  118. * corresponding GDT slot.
  119. */
  120. if (child->thread.fs != value)
  121. return do_arch_prctl(child, ARCH_SET_FS, value);
  122. return 0;
  123. case offsetof(struct user_regs_struct,gs_base):
  124. /*
  125. * Exactly the same here as the %fs handling above.
  126. */
  127. if (value >= TASK_SIZE_OF(child))
  128. return -EIO;
  129. if (child->thread.gs != value)
  130. return do_arch_prctl(child, ARCH_SET_GS, value);
  131. return 0;
  132. case offsetof(struct user_regs_struct, eflags):
  133. value &= FLAG_MASK;
  134. /*
  135. * If the user value contains TF, mark that
  136. * it was not "us" (the debugger) that set it.
  137. * If not, make sure it stays set if we had.
  138. */
  139. if (value & X86_EFLAGS_TF)
  140. clear_tsk_thread_flag(child, TIF_FORCED_TF);
  141. else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  142. value |= X86_EFLAGS_TF;
  143. tmp = get_stack_long(child, EFL_OFFSET);
  144. tmp &= ~FLAG_MASK;
  145. value |= tmp;
  146. break;
  147. case offsetof(struct user_regs_struct,cs):
  148. if ((value & 3) != 3)
  149. return -EIO;
  150. value &= 0xffff;
  151. break;
  152. }
  153. put_stack_long(child, regno - sizeof(struct pt_regs), value);
  154. return 0;
  155. }
  156. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  157. {
  158. unsigned long val;
  159. switch (regno) {
  160. case offsetof(struct user_regs_struct, fs):
  161. return child->thread.fsindex;
  162. case offsetof(struct user_regs_struct, gs):
  163. return child->thread.gsindex;
  164. case offsetof(struct user_regs_struct, ds):
  165. return child->thread.ds;
  166. case offsetof(struct user_regs_struct, es):
  167. return child->thread.es;
  168. case offsetof(struct user_regs_struct, fs_base):
  169. /*
  170. * do_arch_prctl may have used a GDT slot instead of
  171. * the MSR. To userland, it appears the same either
  172. * way, except the %fs segment selector might not be 0.
  173. */
  174. if (child->thread.fs != 0)
  175. return child->thread.fs;
  176. if (child->thread.fsindex != FS_TLS_SEL)
  177. return 0;
  178. return get_desc_base(&child->thread.tls_array[FS_TLS]);
  179. case offsetof(struct user_regs_struct, gs_base):
  180. /*
  181. * Exactly the same here as the %fs handling above.
  182. */
  183. if (child->thread.gs != 0)
  184. return child->thread.gs;
  185. if (child->thread.gsindex != GS_TLS_SEL)
  186. return 0;
  187. return get_desc_base(&child->thread.tls_array[GS_TLS]);
  188. case offsetof(struct user_regs_struct, eflags):
  189. /*
  190. * If the debugger set TF, hide it from the readout.
  191. */
  192. regno = regno - sizeof(struct pt_regs);
  193. val = get_stack_long(child, regno);
  194. if (test_tsk_thread_flag(child, TIF_IA32))
  195. val &= 0xffffffff;
  196. if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  197. val &= ~X86_EFLAGS_TF;
  198. return val;
  199. default:
  200. regno = regno - sizeof(struct pt_regs);
  201. val = get_stack_long(child, regno);
  202. if (test_tsk_thread_flag(child, TIF_IA32))
  203. val &= 0xffffffff;
  204. return val;
  205. }
  206. }
  207. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  208. {
  209. long i, ret;
  210. unsigned ui;
  211. switch (request) {
  212. /* when I and D space are separate, these will need to be fixed. */
  213. case PTRACE_PEEKTEXT: /* read word at location addr. */
  214. case PTRACE_PEEKDATA:
  215. ret = generic_ptrace_peekdata(child, addr, data);
  216. break;
  217. /* read the word at location addr in the USER area. */
  218. case PTRACE_PEEKUSR: {
  219. unsigned long tmp;
  220. ret = -EIO;
  221. if ((addr & 7) ||
  222. addr > sizeof(struct user) - 7)
  223. break;
  224. switch (addr) {
  225. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  226. tmp = getreg(child, addr);
  227. break;
  228. case offsetof(struct user, u_debugreg[0]):
  229. tmp = child->thread.debugreg0;
  230. break;
  231. case offsetof(struct user, u_debugreg[1]):
  232. tmp = child->thread.debugreg1;
  233. break;
  234. case offsetof(struct user, u_debugreg[2]):
  235. tmp = child->thread.debugreg2;
  236. break;
  237. case offsetof(struct user, u_debugreg[3]):
  238. tmp = child->thread.debugreg3;
  239. break;
  240. case offsetof(struct user, u_debugreg[6]):
  241. tmp = child->thread.debugreg6;
  242. break;
  243. case offsetof(struct user, u_debugreg[7]):
  244. tmp = child->thread.debugreg7;
  245. break;
  246. default:
  247. tmp = 0;
  248. break;
  249. }
  250. ret = put_user(tmp,(unsigned long __user *) data);
  251. break;
  252. }
  253. /* when I and D space are separate, this will have to be fixed. */
  254. case PTRACE_POKETEXT: /* write the word at location addr. */
  255. case PTRACE_POKEDATA:
  256. ret = generic_ptrace_pokedata(child, addr, data);
  257. break;
  258. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  259. {
  260. int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
  261. ret = -EIO;
  262. if ((addr & 7) ||
  263. addr > sizeof(struct user) - 7)
  264. break;
  265. switch (addr) {
  266. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  267. ret = putreg(child, addr, data);
  268. break;
  269. /* Disallows to set a breakpoint into the vsyscall */
  270. case offsetof(struct user, u_debugreg[0]):
  271. if (data >= TASK_SIZE_OF(child) - dsize) break;
  272. child->thread.debugreg0 = data;
  273. ret = 0;
  274. break;
  275. case offsetof(struct user, u_debugreg[1]):
  276. if (data >= TASK_SIZE_OF(child) - dsize) break;
  277. child->thread.debugreg1 = data;
  278. ret = 0;
  279. break;
  280. case offsetof(struct user, u_debugreg[2]):
  281. if (data >= TASK_SIZE_OF(child) - dsize) break;
  282. child->thread.debugreg2 = data;
  283. ret = 0;
  284. break;
  285. case offsetof(struct user, u_debugreg[3]):
  286. if (data >= TASK_SIZE_OF(child) - dsize) break;
  287. child->thread.debugreg3 = data;
  288. ret = 0;
  289. break;
  290. case offsetof(struct user, u_debugreg[6]):
  291. if (data >> 32)
  292. break;
  293. child->thread.debugreg6 = data;
  294. ret = 0;
  295. break;
  296. case offsetof(struct user, u_debugreg[7]):
  297. /* See arch/i386/kernel/ptrace.c for an explanation of
  298. * this awkward check.*/
  299. data &= ~DR_CONTROL_RESERVED;
  300. for(i=0; i<4; i++)
  301. if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  302. break;
  303. if (i == 4) {
  304. child->thread.debugreg7 = data;
  305. if (data)
  306. set_tsk_thread_flag(child, TIF_DEBUG);
  307. else
  308. clear_tsk_thread_flag(child, TIF_DEBUG);
  309. ret = 0;
  310. }
  311. break;
  312. }
  313. break;
  314. }
  315. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  316. case PTRACE_CONT: /* restart after signal. */
  317. ret = -EIO;
  318. if (!valid_signal(data))
  319. break;
  320. if (request == PTRACE_SYSCALL)
  321. set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  322. else
  323. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  324. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  325. child->exit_code = data;
  326. /* make sure the single step bit is not set. */
  327. user_disable_single_step(child);
  328. wake_up_process(child);
  329. ret = 0;
  330. break;
  331. #ifdef CONFIG_IA32_EMULATION
  332. /* This makes only sense with 32bit programs. Allow a
  333. 64bit debugger to fully examine them too. Better
  334. don't use it against 64bit processes, use
  335. PTRACE_ARCH_PRCTL instead. */
  336. case PTRACE_GET_THREAD_AREA:
  337. if (addr < 0)
  338. return -EIO;
  339. ret = do_get_thread_area(child, addr,
  340. (struct user_desc __user *) data);
  341. break;
  342. case PTRACE_SET_THREAD_AREA:
  343. if (addr < 0)
  344. return -EIO;
  345. ret = do_set_thread_area(child, addr,
  346. (struct user_desc __user *) data, 0);
  347. break;
  348. #endif
  349. /* normal 64bit interface to access TLS data.
  350. Works just like arch_prctl, except that the arguments
  351. are reversed. */
  352. case PTRACE_ARCH_PRCTL:
  353. ret = do_arch_prctl(child, data, addr);
  354. break;
  355. /*
  356. * make the child exit. Best I can do is send it a sigkill.
  357. * perhaps it should be put in the status that it wants to
  358. * exit.
  359. */
  360. case PTRACE_KILL:
  361. ret = 0;
  362. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  363. break;
  364. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  365. child->exit_code = SIGKILL;
  366. /* make sure the single step bit is not set. */
  367. user_disable_single_step(child);
  368. wake_up_process(child);
  369. break;
  370. case PTRACE_SINGLESTEP: /* set the trap flag. */
  371. ret = -EIO;
  372. if (!valid_signal(data))
  373. break;
  374. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  375. user_enable_single_step(child);
  376. child->exit_code = data;
  377. /* give it a chance to run. */
  378. wake_up_process(child);
  379. ret = 0;
  380. break;
  381. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  382. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  383. sizeof(struct user_regs_struct))) {
  384. ret = -EIO;
  385. break;
  386. }
  387. ret = 0;
  388. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  389. ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
  390. data += sizeof(long);
  391. }
  392. break;
  393. }
  394. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  395. unsigned long tmp;
  396. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  397. sizeof(struct user_regs_struct))) {
  398. ret = -EIO;
  399. break;
  400. }
  401. ret = 0;
  402. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  403. ret = __get_user(tmp, (unsigned long __user *) data);
  404. if (ret)
  405. break;
  406. ret = putreg(child, ui, tmp);
  407. if (ret)
  408. break;
  409. data += sizeof(long);
  410. }
  411. break;
  412. }
  413. case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
  414. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  415. sizeof(struct user_i387_struct))) {
  416. ret = -EIO;
  417. break;
  418. }
  419. ret = get_fpregs((struct user_i387_struct __user *)data, child);
  420. break;
  421. }
  422. case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
  423. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  424. sizeof(struct user_i387_struct))) {
  425. ret = -EIO;
  426. break;
  427. }
  428. set_stopped_child_used_math(child);
  429. ret = set_fpregs(child, (struct user_i387_struct __user *)data);
  430. break;
  431. }
  432. default:
  433. ret = ptrace_request(child, request, addr, data);
  434. break;
  435. }
  436. return ret;
  437. }
  438. static void syscall_trace(struct pt_regs *regs)
  439. {
  440. #if 0
  441. printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  442. current->comm,
  443. regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
  444. current_thread_info()->flags, current->ptrace);
  445. #endif
  446. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  447. ? 0x80 : 0));
  448. /*
  449. * this isn't the same as continuing with a signal, but it will do
  450. * for normal use. strace only continues with a signal if the
  451. * stopping signal is not SIGTRAP. -brl
  452. */
  453. if (current->exit_code) {
  454. send_sig(current->exit_code, current, 1);
  455. current->exit_code = 0;
  456. }
  457. }
  458. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  459. {
  460. /* do the secure computing check first */
  461. secure_computing(regs->orig_rax);
  462. if (test_thread_flag(TIF_SYSCALL_TRACE)
  463. && (current->ptrace & PT_PTRACED))
  464. syscall_trace(regs);
  465. if (unlikely(current->audit_context)) {
  466. if (test_thread_flag(TIF_IA32)) {
  467. audit_syscall_entry(AUDIT_ARCH_I386,
  468. regs->orig_rax,
  469. regs->rbx, regs->rcx,
  470. regs->rdx, regs->rsi);
  471. } else {
  472. audit_syscall_entry(AUDIT_ARCH_X86_64,
  473. regs->orig_rax,
  474. regs->rdi, regs->rsi,
  475. regs->rdx, regs->r10);
  476. }
  477. }
  478. }
  479. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  480. {
  481. if (unlikely(current->audit_context))
  482. audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
  483. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  484. || test_thread_flag(TIF_SINGLESTEP))
  485. && (current->ptrace & PT_PTRACED))
  486. syscall_trace(regs);
  487. }