ptrace_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. tmp = get_stack_long(child, EFL_OFFSET);
  135. tmp &= ~FLAG_MASK;
  136. value |= tmp;
  137. break;
  138. case offsetof(struct user_regs_struct,cs):
  139. if ((value & 3) != 3)
  140. return -EIO;
  141. value &= 0xffff;
  142. break;
  143. }
  144. put_stack_long(child, regno - sizeof(struct pt_regs), value);
  145. return 0;
  146. }
  147. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  148. {
  149. unsigned long val;
  150. switch (regno) {
  151. case offsetof(struct user_regs_struct, fs):
  152. return child->thread.fsindex;
  153. case offsetof(struct user_regs_struct, gs):
  154. return child->thread.gsindex;
  155. case offsetof(struct user_regs_struct, ds):
  156. return child->thread.ds;
  157. case offsetof(struct user_regs_struct, es):
  158. return child->thread.es;
  159. case offsetof(struct user_regs_struct, fs_base):
  160. /*
  161. * do_arch_prctl may have used a GDT slot instead of
  162. * the MSR. To userland, it appears the same either
  163. * way, except the %fs segment selector might not be 0.
  164. */
  165. if (child->thread.fs != 0)
  166. return child->thread.fs;
  167. if (child->thread.fsindex != FS_TLS_SEL)
  168. return 0;
  169. return get_desc_base(&child->thread.tls_array[FS_TLS]);
  170. case offsetof(struct user_regs_struct, gs_base):
  171. /*
  172. * Exactly the same here as the %fs handling above.
  173. */
  174. if (child->thread.gs != 0)
  175. return child->thread.gs;
  176. if (child->thread.gsindex != GS_TLS_SEL)
  177. return 0;
  178. return get_desc_base(&child->thread.tls_array[GS_TLS]);
  179. default:
  180. regno = regno - sizeof(struct pt_regs);
  181. val = get_stack_long(child, regno);
  182. if (test_tsk_thread_flag(child, TIF_IA32))
  183. val &= 0xffffffff;
  184. return val;
  185. }
  186. }
  187. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  188. {
  189. long i, ret;
  190. unsigned ui;
  191. switch (request) {
  192. /* when I and D space are separate, these will need to be fixed. */
  193. case PTRACE_PEEKTEXT: /* read word at location addr. */
  194. case PTRACE_PEEKDATA:
  195. ret = generic_ptrace_peekdata(child, addr, data);
  196. break;
  197. /* read the word at location addr in the USER area. */
  198. case PTRACE_PEEKUSR: {
  199. unsigned long tmp;
  200. ret = -EIO;
  201. if ((addr & 7) ||
  202. addr > sizeof(struct user) - 7)
  203. break;
  204. switch (addr) {
  205. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  206. tmp = getreg(child, addr);
  207. break;
  208. case offsetof(struct user, u_debugreg[0]):
  209. tmp = child->thread.debugreg0;
  210. break;
  211. case offsetof(struct user, u_debugreg[1]):
  212. tmp = child->thread.debugreg1;
  213. break;
  214. case offsetof(struct user, u_debugreg[2]):
  215. tmp = child->thread.debugreg2;
  216. break;
  217. case offsetof(struct user, u_debugreg[3]):
  218. tmp = child->thread.debugreg3;
  219. break;
  220. case offsetof(struct user, u_debugreg[6]):
  221. tmp = child->thread.debugreg6;
  222. break;
  223. case offsetof(struct user, u_debugreg[7]):
  224. tmp = child->thread.debugreg7;
  225. break;
  226. default:
  227. tmp = 0;
  228. break;
  229. }
  230. ret = put_user(tmp,(unsigned long __user *) data);
  231. break;
  232. }
  233. /* when I and D space are separate, this will have to be fixed. */
  234. case PTRACE_POKETEXT: /* write the word at location addr. */
  235. case PTRACE_POKEDATA:
  236. ret = generic_ptrace_pokedata(child, addr, data);
  237. break;
  238. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  239. {
  240. int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
  241. ret = -EIO;
  242. if ((addr & 7) ||
  243. addr > sizeof(struct user) - 7)
  244. break;
  245. switch (addr) {
  246. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  247. ret = putreg(child, addr, data);
  248. break;
  249. /* Disallows to set a breakpoint into the vsyscall */
  250. case offsetof(struct user, u_debugreg[0]):
  251. if (data >= TASK_SIZE_OF(child) - dsize) break;
  252. child->thread.debugreg0 = data;
  253. ret = 0;
  254. break;
  255. case offsetof(struct user, u_debugreg[1]):
  256. if (data >= TASK_SIZE_OF(child) - dsize) break;
  257. child->thread.debugreg1 = data;
  258. ret = 0;
  259. break;
  260. case offsetof(struct user, u_debugreg[2]):
  261. if (data >= TASK_SIZE_OF(child) - dsize) break;
  262. child->thread.debugreg2 = data;
  263. ret = 0;
  264. break;
  265. case offsetof(struct user, u_debugreg[3]):
  266. if (data >= TASK_SIZE_OF(child) - dsize) break;
  267. child->thread.debugreg3 = data;
  268. ret = 0;
  269. break;
  270. case offsetof(struct user, u_debugreg[6]):
  271. if (data >> 32)
  272. break;
  273. child->thread.debugreg6 = data;
  274. ret = 0;
  275. break;
  276. case offsetof(struct user, u_debugreg[7]):
  277. /* See arch/i386/kernel/ptrace.c for an explanation of
  278. * this awkward check.*/
  279. data &= ~DR_CONTROL_RESERVED;
  280. for(i=0; i<4; i++)
  281. if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  282. break;
  283. if (i == 4) {
  284. child->thread.debugreg7 = data;
  285. if (data)
  286. set_tsk_thread_flag(child, TIF_DEBUG);
  287. else
  288. clear_tsk_thread_flag(child, TIF_DEBUG);
  289. ret = 0;
  290. }
  291. break;
  292. }
  293. break;
  294. }
  295. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  296. case PTRACE_CONT: /* restart after signal. */
  297. ret = -EIO;
  298. if (!valid_signal(data))
  299. break;
  300. if (request == PTRACE_SYSCALL)
  301. set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  302. else
  303. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  304. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  305. child->exit_code = data;
  306. /* make sure the single step bit is not set. */
  307. user_disable_single_step(child);
  308. wake_up_process(child);
  309. ret = 0;
  310. break;
  311. #ifdef CONFIG_IA32_EMULATION
  312. /* This makes only sense with 32bit programs. Allow a
  313. 64bit debugger to fully examine them too. Better
  314. don't use it against 64bit processes, use
  315. PTRACE_ARCH_PRCTL instead. */
  316. case PTRACE_GET_THREAD_AREA:
  317. if (addr < 0)
  318. return -EIO;
  319. ret = do_get_thread_area(child, addr,
  320. (struct user_desc __user *) data);
  321. break;
  322. case PTRACE_SET_THREAD_AREA:
  323. if (addr < 0)
  324. return -EIO;
  325. ret = do_set_thread_area(child, addr,
  326. (struct user_desc __user *) data, 0);
  327. break;
  328. #endif
  329. /* normal 64bit interface to access TLS data.
  330. Works just like arch_prctl, except that the arguments
  331. are reversed. */
  332. case PTRACE_ARCH_PRCTL:
  333. ret = do_arch_prctl(child, data, addr);
  334. break;
  335. /*
  336. * make the child exit. Best I can do is send it a sigkill.
  337. * perhaps it should be put in the status that it wants to
  338. * exit.
  339. */
  340. case PTRACE_KILL:
  341. ret = 0;
  342. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  343. break;
  344. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  345. child->exit_code = SIGKILL;
  346. /* make sure the single step bit is not set. */
  347. user_disable_single_step(child);
  348. wake_up_process(child);
  349. break;
  350. case PTRACE_SINGLESTEP: /* set the trap flag. */
  351. ret = -EIO;
  352. if (!valid_signal(data))
  353. break;
  354. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  355. user_enable_single_step(child);
  356. child->exit_code = data;
  357. /* give it a chance to run. */
  358. wake_up_process(child);
  359. ret = 0;
  360. break;
  361. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  362. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  363. sizeof(struct user_regs_struct))) {
  364. ret = -EIO;
  365. break;
  366. }
  367. ret = 0;
  368. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  369. ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
  370. data += sizeof(long);
  371. }
  372. break;
  373. }
  374. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  375. unsigned long tmp;
  376. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  377. sizeof(struct user_regs_struct))) {
  378. ret = -EIO;
  379. break;
  380. }
  381. ret = 0;
  382. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  383. ret = __get_user(tmp, (unsigned long __user *) data);
  384. if (ret)
  385. break;
  386. ret = putreg(child, ui, tmp);
  387. if (ret)
  388. break;
  389. data += sizeof(long);
  390. }
  391. break;
  392. }
  393. case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
  394. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  395. sizeof(struct user_i387_struct))) {
  396. ret = -EIO;
  397. break;
  398. }
  399. ret = get_fpregs((struct user_i387_struct __user *)data, child);
  400. break;
  401. }
  402. case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
  403. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  404. sizeof(struct user_i387_struct))) {
  405. ret = -EIO;
  406. break;
  407. }
  408. set_stopped_child_used_math(child);
  409. ret = set_fpregs(child, (struct user_i387_struct __user *)data);
  410. break;
  411. }
  412. default:
  413. ret = ptrace_request(child, request, addr, data);
  414. break;
  415. }
  416. return ret;
  417. }
  418. static void syscall_trace(struct pt_regs *regs)
  419. {
  420. #if 0
  421. printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  422. current->comm,
  423. regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
  424. current_thread_info()->flags, current->ptrace);
  425. #endif
  426. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  427. ? 0x80 : 0));
  428. /*
  429. * this isn't the same as continuing with a signal, but it will do
  430. * for normal use. strace only continues with a signal if the
  431. * stopping signal is not SIGTRAP. -brl
  432. */
  433. if (current->exit_code) {
  434. send_sig(current->exit_code, current, 1);
  435. current->exit_code = 0;
  436. }
  437. }
  438. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  439. {
  440. /* do the secure computing check first */
  441. secure_computing(regs->orig_rax);
  442. if (test_thread_flag(TIF_SYSCALL_TRACE)
  443. && (current->ptrace & PT_PTRACED))
  444. syscall_trace(regs);
  445. if (unlikely(current->audit_context)) {
  446. if (test_thread_flag(TIF_IA32)) {
  447. audit_syscall_entry(AUDIT_ARCH_I386,
  448. regs->orig_rax,
  449. regs->rbx, regs->rcx,
  450. regs->rdx, regs->rsi);
  451. } else {
  452. audit_syscall_entry(AUDIT_ARCH_X86_64,
  453. regs->orig_rax,
  454. regs->rdi, regs->rsi,
  455. regs->rdx, regs->r10);
  456. }
  457. }
  458. }
  459. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  460. {
  461. if (unlikely(current->audit_context))
  462. audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
  463. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  464. || test_thread_flag(TIF_SINGLESTEP))
  465. && (current->ptrace & PT_PTRACED))
  466. syscall_trace(regs);
  467. }