ptrace_64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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/i387.h>
  24. #include <asm/debugreg.h>
  25. #include <asm/ldt.h>
  26. #include <asm/desc.h>
  27. #include <asm/proto.h>
  28. #include <asm/ia32.h>
  29. /*
  30. * does not yet catch signals sent when the child dies.
  31. * in exit.c or in signal.c.
  32. */
  33. /*
  34. * Determines which flags the user has access to [1 = access, 0 = no access].
  35. * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
  36. * Also masks reserved bits (63-22, 15, 5, 3, 1).
  37. */
  38. #define FLAG_MASK 0x54dd5UL
  39. /* set's the trap flag. */
  40. #define TRAP_FLAG 0x100UL
  41. /*
  42. * eflags and offset of eflags on child stack..
  43. */
  44. #define EFLAGS offsetof(struct pt_regs, eflags)
  45. #define EFL_OFFSET ((int)(EFLAGS-sizeof(struct pt_regs)))
  46. /*
  47. * this routine will get a word off of the processes privileged stack.
  48. * the offset is how far from the base addr as stored in the TSS.
  49. * this routine assumes that all the privileged stacks are in our
  50. * data space.
  51. */
  52. static inline unsigned long get_stack_long(struct task_struct *task, int offset)
  53. {
  54. unsigned char *stack;
  55. stack = (unsigned char *)task->thread.rsp0;
  56. stack += offset;
  57. return (*((unsigned long *)stack));
  58. }
  59. /*
  60. * this routine will put a word on the processes privileged stack.
  61. * the offset is how far from the base addr as stored in the TSS.
  62. * this routine assumes that all the privileged stacks are in our
  63. * data space.
  64. */
  65. static inline long put_stack_long(struct task_struct *task, int offset,
  66. unsigned long data)
  67. {
  68. unsigned char * stack;
  69. stack = (unsigned char *) task->thread.rsp0;
  70. stack += offset;
  71. *(unsigned long *) stack = data;
  72. return 0;
  73. }
  74. #define LDT_SEGMENT 4
  75. unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
  76. {
  77. unsigned long addr, seg;
  78. addr = regs->rip;
  79. seg = regs->cs & 0xffff;
  80. /*
  81. * We'll assume that the code segments in the GDT
  82. * are all zero-based. That is largely true: the
  83. * TLS segments are used for data, and the PNPBIOS
  84. * and APM bios ones we just ignore here.
  85. */
  86. if (seg & LDT_SEGMENT) {
  87. u32 *desc;
  88. unsigned long base;
  89. seg &= ~7UL;
  90. down(&child->mm->context.sem);
  91. if (unlikely((seg >> 3) >= child->mm->context.size))
  92. addr = -1L; /* bogus selector, access would fault */
  93. else {
  94. desc = child->mm->context.ldt + seg;
  95. base = ((desc[0] >> 16) |
  96. ((desc[1] & 0xff) << 16) |
  97. (desc[1] & 0xff000000));
  98. /* 16-bit code segment? */
  99. if (!((desc[1] >> 22) & 1))
  100. addr &= 0xffff;
  101. addr += base;
  102. }
  103. up(&child->mm->context.sem);
  104. }
  105. return addr;
  106. }
  107. static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
  108. {
  109. int i, copied;
  110. unsigned char opcode[15];
  111. unsigned long addr = convert_rip_to_linear(child, regs);
  112. copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
  113. for (i = 0; i < copied; i++) {
  114. switch (opcode[i]) {
  115. /* popf and iret */
  116. case 0x9d: case 0xcf:
  117. return 1;
  118. /* CHECKME: 64 65 */
  119. /* opcode and address size prefixes */
  120. case 0x66: case 0x67:
  121. continue;
  122. /* irrelevant prefixes (segment overrides and repeats) */
  123. case 0x26: case 0x2e:
  124. case 0x36: case 0x3e:
  125. case 0x64: case 0x65:
  126. case 0xf2: case 0xf3:
  127. continue;
  128. case 0x40 ... 0x4f:
  129. if (regs->cs != __USER_CS)
  130. /* 32-bit mode: register increment */
  131. return 0;
  132. /* 64-bit mode: REX prefix */
  133. continue;
  134. /* CHECKME: f2, f3 */
  135. /*
  136. * pushf: NOTE! We should probably not let
  137. * the user see the TF bit being set. But
  138. * it's more pain than it's worth to avoid
  139. * it, and a debugger could emulate this
  140. * all in user space if it _really_ cares.
  141. */
  142. case 0x9c:
  143. default:
  144. return 0;
  145. }
  146. }
  147. return 0;
  148. }
  149. static void set_singlestep(struct task_struct *child)
  150. {
  151. struct pt_regs *regs = task_pt_regs(child);
  152. /*
  153. * Always set TIF_SINGLESTEP - this guarantees that
  154. * we single-step system calls etc.. This will also
  155. * cause us to set TF when returning to user mode.
  156. */
  157. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  158. /*
  159. * If TF was already set, don't do anything else
  160. */
  161. if (regs->eflags & TRAP_FLAG)
  162. return;
  163. /* Set TF on the kernel stack.. */
  164. regs->eflags |= TRAP_FLAG;
  165. /*
  166. * ..but if TF is changed by the instruction we will trace,
  167. * don't mark it as being "us" that set it, so that we
  168. * won't clear it by hand later.
  169. */
  170. if (is_setting_trap_flag(child, regs))
  171. return;
  172. child->ptrace |= PT_DTRACE;
  173. }
  174. static void clear_singlestep(struct task_struct *child)
  175. {
  176. /* Always clear TIF_SINGLESTEP... */
  177. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  178. /* But touch TF only if it was set by us.. */
  179. if (child->ptrace & PT_DTRACE) {
  180. struct pt_regs *regs = task_pt_regs(child);
  181. regs->eflags &= ~TRAP_FLAG;
  182. child->ptrace &= ~PT_DTRACE;
  183. }
  184. }
  185. /*
  186. * Called by kernel/ptrace.c when detaching..
  187. *
  188. * Make sure the single step bit is not set.
  189. */
  190. void ptrace_disable(struct task_struct *child)
  191. {
  192. clear_singlestep(child);
  193. }
  194. static int putreg(struct task_struct *child,
  195. unsigned long regno, unsigned long value)
  196. {
  197. unsigned long tmp;
  198. switch (regno) {
  199. case offsetof(struct user_regs_struct,fs):
  200. if (value && (value & 3) != 3)
  201. return -EIO;
  202. child->thread.fsindex = value & 0xffff;
  203. return 0;
  204. case offsetof(struct user_regs_struct,gs):
  205. if (value && (value & 3) != 3)
  206. return -EIO;
  207. child->thread.gsindex = value & 0xffff;
  208. return 0;
  209. case offsetof(struct user_regs_struct,ds):
  210. if (value && (value & 3) != 3)
  211. return -EIO;
  212. child->thread.ds = value & 0xffff;
  213. return 0;
  214. case offsetof(struct user_regs_struct,es):
  215. if (value && (value & 3) != 3)
  216. return -EIO;
  217. child->thread.es = value & 0xffff;
  218. return 0;
  219. case offsetof(struct user_regs_struct,ss):
  220. if ((value & 3) != 3)
  221. return -EIO;
  222. value &= 0xffff;
  223. return 0;
  224. case offsetof(struct user_regs_struct,fs_base):
  225. if (value >= TASK_SIZE_OF(child))
  226. return -EIO;
  227. child->thread.fs = value;
  228. return 0;
  229. case offsetof(struct user_regs_struct,gs_base):
  230. if (value >= TASK_SIZE_OF(child))
  231. return -EIO;
  232. child->thread.gs = value;
  233. return 0;
  234. case offsetof(struct user_regs_struct, eflags):
  235. value &= FLAG_MASK;
  236. tmp = get_stack_long(child, EFL_OFFSET);
  237. tmp &= ~FLAG_MASK;
  238. value |= tmp;
  239. break;
  240. case offsetof(struct user_regs_struct,cs):
  241. if ((value & 3) != 3)
  242. return -EIO;
  243. value &= 0xffff;
  244. break;
  245. }
  246. put_stack_long(child, regno - sizeof(struct pt_regs), value);
  247. return 0;
  248. }
  249. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  250. {
  251. unsigned long val;
  252. switch (regno) {
  253. case offsetof(struct user_regs_struct, fs):
  254. return child->thread.fsindex;
  255. case offsetof(struct user_regs_struct, gs):
  256. return child->thread.gsindex;
  257. case offsetof(struct user_regs_struct, ds):
  258. return child->thread.ds;
  259. case offsetof(struct user_regs_struct, es):
  260. return child->thread.es;
  261. case offsetof(struct user_regs_struct, fs_base):
  262. return child->thread.fs;
  263. case offsetof(struct user_regs_struct, gs_base):
  264. return child->thread.gs;
  265. default:
  266. regno = regno - sizeof(struct pt_regs);
  267. val = get_stack_long(child, regno);
  268. if (test_tsk_thread_flag(child, TIF_IA32))
  269. val &= 0xffffffff;
  270. return val;
  271. }
  272. }
  273. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  274. {
  275. long i, ret;
  276. unsigned ui;
  277. switch (request) {
  278. /* when I and D space are separate, these will need to be fixed. */
  279. case PTRACE_PEEKTEXT: /* read word at location addr. */
  280. case PTRACE_PEEKDATA:
  281. ret = generic_ptrace_peekdata(child, addr, data);
  282. break;
  283. /* read the word at location addr in the USER area. */
  284. case PTRACE_PEEKUSR: {
  285. unsigned long tmp;
  286. ret = -EIO;
  287. if ((addr & 7) ||
  288. addr > sizeof(struct user) - 7)
  289. break;
  290. switch (addr) {
  291. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  292. tmp = getreg(child, addr);
  293. break;
  294. case offsetof(struct user, u_debugreg[0]):
  295. tmp = child->thread.debugreg0;
  296. break;
  297. case offsetof(struct user, u_debugreg[1]):
  298. tmp = child->thread.debugreg1;
  299. break;
  300. case offsetof(struct user, u_debugreg[2]):
  301. tmp = child->thread.debugreg2;
  302. break;
  303. case offsetof(struct user, u_debugreg[3]):
  304. tmp = child->thread.debugreg3;
  305. break;
  306. case offsetof(struct user, u_debugreg[6]):
  307. tmp = child->thread.debugreg6;
  308. break;
  309. case offsetof(struct user, u_debugreg[7]):
  310. tmp = child->thread.debugreg7;
  311. break;
  312. default:
  313. tmp = 0;
  314. break;
  315. }
  316. ret = put_user(tmp,(unsigned long __user *) data);
  317. break;
  318. }
  319. /* when I and D space are separate, this will have to be fixed. */
  320. case PTRACE_POKETEXT: /* write the word at location addr. */
  321. case PTRACE_POKEDATA:
  322. ret = generic_ptrace_pokedata(child, addr, data);
  323. break;
  324. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  325. {
  326. int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
  327. ret = -EIO;
  328. if ((addr & 7) ||
  329. addr > sizeof(struct user) - 7)
  330. break;
  331. switch (addr) {
  332. case 0 ... sizeof(struct user_regs_struct) - sizeof(long):
  333. ret = putreg(child, addr, data);
  334. break;
  335. /* Disallows to set a breakpoint into the vsyscall */
  336. case offsetof(struct user, u_debugreg[0]):
  337. if (data >= TASK_SIZE_OF(child) - dsize) break;
  338. child->thread.debugreg0 = data;
  339. ret = 0;
  340. break;
  341. case offsetof(struct user, u_debugreg[1]):
  342. if (data >= TASK_SIZE_OF(child) - dsize) break;
  343. child->thread.debugreg1 = data;
  344. ret = 0;
  345. break;
  346. case offsetof(struct user, u_debugreg[2]):
  347. if (data >= TASK_SIZE_OF(child) - dsize) break;
  348. child->thread.debugreg2 = data;
  349. ret = 0;
  350. break;
  351. case offsetof(struct user, u_debugreg[3]):
  352. if (data >= TASK_SIZE_OF(child) - dsize) break;
  353. child->thread.debugreg3 = data;
  354. ret = 0;
  355. break;
  356. case offsetof(struct user, u_debugreg[6]):
  357. if (data >> 32)
  358. break;
  359. child->thread.debugreg6 = data;
  360. ret = 0;
  361. break;
  362. case offsetof(struct user, u_debugreg[7]):
  363. /* See arch/i386/kernel/ptrace.c for an explanation of
  364. * this awkward check.*/
  365. data &= ~DR_CONTROL_RESERVED;
  366. for(i=0; i<4; i++)
  367. if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  368. break;
  369. if (i == 4) {
  370. child->thread.debugreg7 = data;
  371. if (data)
  372. set_tsk_thread_flag(child, TIF_DEBUG);
  373. else
  374. clear_tsk_thread_flag(child, TIF_DEBUG);
  375. ret = 0;
  376. }
  377. break;
  378. }
  379. break;
  380. }
  381. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  382. case PTRACE_CONT: /* restart after signal. */
  383. ret = -EIO;
  384. if (!valid_signal(data))
  385. break;
  386. if (request == PTRACE_SYSCALL)
  387. set_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  388. else
  389. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  390. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  391. child->exit_code = data;
  392. /* make sure the single step bit is not set. */
  393. clear_singlestep(child);
  394. wake_up_process(child);
  395. ret = 0;
  396. break;
  397. #ifdef CONFIG_IA32_EMULATION
  398. /* This makes only sense with 32bit programs. Allow a
  399. 64bit debugger to fully examine them too. Better
  400. don't use it against 64bit processes, use
  401. PTRACE_ARCH_PRCTL instead. */
  402. case PTRACE_SET_THREAD_AREA: {
  403. struct user_desc __user *p;
  404. int old;
  405. p = (struct user_desc __user *)data;
  406. get_user(old, &p->entry_number);
  407. put_user(addr, &p->entry_number);
  408. ret = do_set_thread_area(&child->thread, p);
  409. put_user(old, &p->entry_number);
  410. break;
  411. case PTRACE_GET_THREAD_AREA:
  412. p = (struct user_desc __user *)data;
  413. get_user(old, &p->entry_number);
  414. put_user(addr, &p->entry_number);
  415. ret = do_get_thread_area(&child->thread, p);
  416. put_user(old, &p->entry_number);
  417. break;
  418. }
  419. #endif
  420. /* normal 64bit interface to access TLS data.
  421. Works just like arch_prctl, except that the arguments
  422. are reversed. */
  423. case PTRACE_ARCH_PRCTL:
  424. ret = do_arch_prctl(child, data, addr);
  425. break;
  426. /*
  427. * make the child exit. Best I can do is send it a sigkill.
  428. * perhaps it should be put in the status that it wants to
  429. * exit.
  430. */
  431. case PTRACE_KILL:
  432. ret = 0;
  433. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  434. break;
  435. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  436. child->exit_code = SIGKILL;
  437. /* make sure the single step bit is not set. */
  438. clear_singlestep(child);
  439. wake_up_process(child);
  440. break;
  441. case PTRACE_SINGLESTEP: /* set the trap flag. */
  442. ret = -EIO;
  443. if (!valid_signal(data))
  444. break;
  445. clear_tsk_thread_flag(child,TIF_SYSCALL_TRACE);
  446. set_singlestep(child);
  447. child->exit_code = data;
  448. /* give it a chance to run. */
  449. wake_up_process(child);
  450. ret = 0;
  451. break;
  452. case PTRACE_DETACH:
  453. /* detach a process that was attached. */
  454. ret = ptrace_detach(child, data);
  455. break;
  456. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  457. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  458. sizeof(struct user_regs_struct))) {
  459. ret = -EIO;
  460. break;
  461. }
  462. ret = 0;
  463. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  464. ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
  465. data += sizeof(long);
  466. }
  467. break;
  468. }
  469. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  470. unsigned long tmp;
  471. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  472. sizeof(struct user_regs_struct))) {
  473. ret = -EIO;
  474. break;
  475. }
  476. ret = 0;
  477. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  478. ret = __get_user(tmp, (unsigned long __user *) data);
  479. if (ret)
  480. break;
  481. ret = putreg(child, ui, tmp);
  482. if (ret)
  483. break;
  484. data += sizeof(long);
  485. }
  486. break;
  487. }
  488. case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
  489. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  490. sizeof(struct user_i387_struct))) {
  491. ret = -EIO;
  492. break;
  493. }
  494. ret = get_fpregs((struct user_i387_struct __user *)data, child);
  495. break;
  496. }
  497. case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
  498. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  499. sizeof(struct user_i387_struct))) {
  500. ret = -EIO;
  501. break;
  502. }
  503. set_stopped_child_used_math(child);
  504. ret = set_fpregs(child, (struct user_i387_struct __user *)data);
  505. break;
  506. }
  507. default:
  508. ret = ptrace_request(child, request, addr, data);
  509. break;
  510. }
  511. return ret;
  512. }
  513. static void syscall_trace(struct pt_regs *regs)
  514. {
  515. #if 0
  516. printk("trace %s rip %lx rsp %lx rax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  517. current->comm,
  518. regs->rip, regs->rsp, regs->rax, regs->orig_rax, __builtin_return_address(0),
  519. current_thread_info()->flags, current->ptrace);
  520. #endif
  521. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  522. ? 0x80 : 0));
  523. /*
  524. * this isn't the same as continuing with a signal, but it will do
  525. * for normal use. strace only continues with a signal if the
  526. * stopping signal is not SIGTRAP. -brl
  527. */
  528. if (current->exit_code) {
  529. send_sig(current->exit_code, current, 1);
  530. current->exit_code = 0;
  531. }
  532. }
  533. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  534. {
  535. /* do the secure computing check first */
  536. secure_computing(regs->orig_rax);
  537. if (test_thread_flag(TIF_SYSCALL_TRACE)
  538. && (current->ptrace & PT_PTRACED))
  539. syscall_trace(regs);
  540. if (unlikely(current->audit_context)) {
  541. if (test_thread_flag(TIF_IA32)) {
  542. audit_syscall_entry(AUDIT_ARCH_I386,
  543. regs->orig_rax,
  544. regs->rbx, regs->rcx,
  545. regs->rdx, regs->rsi);
  546. } else {
  547. audit_syscall_entry(AUDIT_ARCH_X86_64,
  548. regs->orig_rax,
  549. regs->rdi, regs->rsi,
  550. regs->rdx, regs->r10);
  551. }
  552. }
  553. }
  554. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  555. {
  556. if (unlikely(current->audit_context))
  557. audit_syscall_exit(AUDITSC_RESULT(regs->rax), regs->rax);
  558. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  559. || test_thread_flag(TIF_SINGLESTEP))
  560. && (current->ptrace & PT_PTRACED))
  561. syscall_trace(regs);
  562. }