ptrace_64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. * Called by kernel/ptrace.c when detaching..
  42. *
  43. * Make sure the single step bit is not set.
  44. */
  45. void ptrace_disable(struct task_struct *child)
  46. {
  47. user_disable_single_step(child);
  48. }
  49. static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
  50. {
  51. BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
  52. return &regs->r15 + (offset / sizeof(regs->r15));
  53. }
  54. static int putreg(struct task_struct *child,
  55. unsigned long regno, unsigned long value)
  56. {
  57. struct pt_regs *regs = task_pt_regs(child);
  58. switch (regno) {
  59. case offsetof(struct user_regs_struct,fs):
  60. if (value && (value & 3) != 3)
  61. return -EIO;
  62. child->thread.fsindex = value & 0xffff;
  63. if (child == current)
  64. loadsegment(fs, child->thread.fsindex);
  65. return 0;
  66. case offsetof(struct user_regs_struct,gs):
  67. if (value && (value & 3) != 3)
  68. return -EIO;
  69. child->thread.gsindex = value & 0xffff;
  70. if (child == current)
  71. load_gs_index(child->thread.gsindex);
  72. return 0;
  73. case offsetof(struct user_regs_struct,ds):
  74. if (value && (value & 3) != 3)
  75. return -EIO;
  76. child->thread.ds = value & 0xffff;
  77. if (child == current)
  78. loadsegment(ds, child->thread.ds);
  79. return 0;
  80. case offsetof(struct user_regs_struct,es):
  81. if (value && (value & 3) != 3)
  82. return -EIO;
  83. child->thread.es = value & 0xffff;
  84. if (child == current)
  85. loadsegment(es, child->thread.es);
  86. return 0;
  87. case offsetof(struct user_regs_struct,ss):
  88. if ((value & 3) != 3)
  89. return -EIO;
  90. value &= 0xffff;
  91. return 0;
  92. case offsetof(struct user_regs_struct,fs_base):
  93. if (value >= TASK_SIZE_OF(child))
  94. return -EIO;
  95. /*
  96. * When changing the segment base, use do_arch_prctl
  97. * to set either thread.fs or thread.fsindex and the
  98. * corresponding GDT slot.
  99. */
  100. if (child->thread.fs != value)
  101. return do_arch_prctl(child, ARCH_SET_FS, value);
  102. return 0;
  103. case offsetof(struct user_regs_struct,gs_base):
  104. /*
  105. * Exactly the same here as the %fs handling above.
  106. */
  107. if (value >= TASK_SIZE_OF(child))
  108. return -EIO;
  109. if (child->thread.gs != value)
  110. return do_arch_prctl(child, ARCH_SET_GS, value);
  111. return 0;
  112. case offsetof(struct user_regs_struct,flags):
  113. value &= FLAG_MASK;
  114. /*
  115. * If the user value contains TF, mark that
  116. * it was not "us" (the debugger) that set it.
  117. * If not, make sure it stays set if we had.
  118. */
  119. if (value & X86_EFLAGS_TF)
  120. clear_tsk_thread_flag(child, TIF_FORCED_TF);
  121. else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  122. value |= X86_EFLAGS_TF;
  123. value |= regs->flags & ~FLAG_MASK;
  124. break;
  125. case offsetof(struct user_regs_struct,cs):
  126. if ((value & 3) != 3)
  127. return -EIO;
  128. value &= 0xffff;
  129. break;
  130. }
  131. *pt_regs_access(regs, regno) = value;
  132. return 0;
  133. }
  134. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  135. {
  136. struct pt_regs *regs = task_pt_regs(child);
  137. unsigned long val;
  138. unsigned int seg;
  139. switch (regno) {
  140. case offsetof(struct user_regs_struct, fs):
  141. if (child == current) {
  142. /* Older gas can't assemble movq %?s,%r?? */
  143. asm("movl %%fs,%0" : "=r" (seg));
  144. return seg;
  145. }
  146. return child->thread.fsindex;
  147. case offsetof(struct user_regs_struct, gs):
  148. if (child == current) {
  149. asm("movl %%gs,%0" : "=r" (seg));
  150. return seg;
  151. }
  152. return child->thread.gsindex;
  153. case offsetof(struct user_regs_struct, ds):
  154. if (child == current) {
  155. asm("movl %%ds,%0" : "=r" (seg));
  156. return seg;
  157. }
  158. return child->thread.ds;
  159. case offsetof(struct user_regs_struct, es):
  160. if (child == current) {
  161. asm("movl %%es,%0" : "=r" (seg));
  162. return seg;
  163. }
  164. return child->thread.es;
  165. case offsetof(struct user_regs_struct, fs_base):
  166. /*
  167. * do_arch_prctl may have used a GDT slot instead of
  168. * the MSR. To userland, it appears the same either
  169. * way, except the %fs segment selector might not be 0.
  170. */
  171. if (child->thread.fs != 0)
  172. return child->thread.fs;
  173. seg = child->thread.fsindex;
  174. if (child == current)
  175. asm("movl %%fs,%0" : "=r" (seg));
  176. if (seg != 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. seg = child->thread.gsindex;
  186. if (child == current)
  187. asm("movl %%gs,%0" : "=r" (seg));
  188. if (seg != GS_TLS_SEL)
  189. return 0;
  190. return get_desc_base(&child->thread.tls_array[GS_TLS]);
  191. case offsetof(struct user_regs_struct, flags):
  192. /*
  193. * If the debugger set TF, hide it from the readout.
  194. */
  195. val = regs->flags;
  196. if (test_tsk_thread_flag(child, TIF_IA32))
  197. val &= 0xffffffff;
  198. if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  199. val &= ~X86_EFLAGS_TF;
  200. return val;
  201. default:
  202. val = *pt_regs_access(regs, regno);
  203. if (test_tsk_thread_flag(child, TIF_IA32))
  204. val &= 0xffffffff;
  205. return val;
  206. }
  207. }
  208. unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
  209. {
  210. switch (n) {
  211. case 0: return child->thread.debugreg0;
  212. case 1: return child->thread.debugreg1;
  213. case 2: return child->thread.debugreg2;
  214. case 3: return child->thread.debugreg3;
  215. case 6: return child->thread.debugreg6;
  216. case 7: return child->thread.debugreg7;
  217. }
  218. return 0;
  219. }
  220. int ptrace_set_debugreg(struct task_struct *child, int n, unsigned long data)
  221. {
  222. int i;
  223. if (n < 4) {
  224. int dsize = test_tsk_thread_flag(child, TIF_IA32) ? 3 : 7;
  225. if (unlikely(data >= TASK_SIZE_OF(child) - dsize))
  226. return -EIO;
  227. }
  228. switch (n) {
  229. case 0: child->thread.debugreg0 = data; break;
  230. case 1: child->thread.debugreg1 = data; break;
  231. case 2: child->thread.debugreg2 = data; break;
  232. case 3: child->thread.debugreg3 = data; break;
  233. case 6:
  234. if (data >> 32)
  235. return -EIO;
  236. child->thread.debugreg6 = data;
  237. break;
  238. case 7:
  239. /*
  240. * See ptrace_32.c for an explanation of this awkward check.
  241. */
  242. data &= ~DR_CONTROL_RESERVED;
  243. for (i = 0; i < 4; i++)
  244. if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  245. return -EIO;
  246. child->thread.debugreg7 = data;
  247. if (data)
  248. set_tsk_thread_flag(child, TIF_DEBUG);
  249. else
  250. clear_tsk_thread_flag(child, TIF_DEBUG);
  251. break;
  252. }
  253. return 0;
  254. }
  255. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  256. {
  257. long ret;
  258. unsigned ui;
  259. switch (request) {
  260. /* when I and D space are separate, these will need to be fixed. */
  261. case PTRACE_PEEKTEXT: /* read word at location addr. */
  262. case PTRACE_PEEKDATA:
  263. ret = generic_ptrace_peekdata(child, addr, data);
  264. break;
  265. /* read the word at location addr in the USER area. */
  266. case PTRACE_PEEKUSR: {
  267. unsigned long tmp;
  268. ret = -EIO;
  269. if ((addr & 7) ||
  270. addr > sizeof(struct user) - 7)
  271. break;
  272. tmp = 0;
  273. if (addr < sizeof(struct user_regs_struct))
  274. tmp = getreg(child, addr);
  275. else if (addr >= offsetof(struct user, u_debugreg[0])) {
  276. addr -= offsetof(struct user, u_debugreg[0]);
  277. tmp = ptrace_get_debugreg(child, addr / sizeof(long));
  278. }
  279. ret = put_user(tmp,(unsigned long __user *) data);
  280. break;
  281. }
  282. /* when I and D space are separate, this will have to be fixed. */
  283. case PTRACE_POKETEXT: /* write the word at location addr. */
  284. case PTRACE_POKEDATA:
  285. ret = generic_ptrace_pokedata(child, addr, data);
  286. break;
  287. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  288. ret = -EIO;
  289. if ((addr & 7) ||
  290. addr > sizeof(struct user) - 7)
  291. break;
  292. if (addr < sizeof(struct user_regs_struct))
  293. ret = putreg(child, addr, data);
  294. else if (addr >= offsetof(struct user, u_debugreg[0])) {
  295. addr -= offsetof(struct user, u_debugreg[0]);
  296. ret = ptrace_set_debugreg(child,
  297. addr / sizeof(long), data);
  298. }
  299. break;
  300. #ifdef CONFIG_IA32_EMULATION
  301. /* This makes only sense with 32bit programs. Allow a
  302. 64bit debugger to fully examine them too. Better
  303. don't use it against 64bit processes, use
  304. PTRACE_ARCH_PRCTL instead. */
  305. case PTRACE_GET_THREAD_AREA:
  306. if (addr < 0)
  307. return -EIO;
  308. ret = do_get_thread_area(child, addr,
  309. (struct user_desc __user *) data);
  310. break;
  311. case PTRACE_SET_THREAD_AREA:
  312. if (addr < 0)
  313. return -EIO;
  314. ret = do_set_thread_area(child, addr,
  315. (struct user_desc __user *) data, 0);
  316. break;
  317. #endif
  318. /* normal 64bit interface to access TLS data.
  319. Works just like arch_prctl, except that the arguments
  320. are reversed. */
  321. case PTRACE_ARCH_PRCTL:
  322. ret = do_arch_prctl(child, data, addr);
  323. break;
  324. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  325. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  326. sizeof(struct user_regs_struct))) {
  327. ret = -EIO;
  328. break;
  329. }
  330. ret = 0;
  331. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  332. ret |= __put_user(getreg(child, ui),(unsigned long __user *) data);
  333. data += sizeof(long);
  334. }
  335. break;
  336. }
  337. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  338. unsigned long tmp;
  339. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  340. sizeof(struct user_regs_struct))) {
  341. ret = -EIO;
  342. break;
  343. }
  344. ret = 0;
  345. for (ui = 0; ui < sizeof(struct user_regs_struct); ui += sizeof(long)) {
  346. ret = __get_user(tmp, (unsigned long __user *) data);
  347. if (ret)
  348. break;
  349. ret = putreg(child, ui, tmp);
  350. if (ret)
  351. break;
  352. data += sizeof(long);
  353. }
  354. break;
  355. }
  356. case PTRACE_GETFPREGS: { /* Get the child extended FPU state. */
  357. if (!access_ok(VERIFY_WRITE, (unsigned __user *)data,
  358. sizeof(struct user_i387_struct))) {
  359. ret = -EIO;
  360. break;
  361. }
  362. ret = get_fpregs((struct user_i387_struct __user *)data, child);
  363. break;
  364. }
  365. case PTRACE_SETFPREGS: { /* Set the child extended FPU state. */
  366. if (!access_ok(VERIFY_READ, (unsigned __user *)data,
  367. sizeof(struct user_i387_struct))) {
  368. ret = -EIO;
  369. break;
  370. }
  371. set_stopped_child_used_math(child);
  372. ret = set_fpregs(child, (struct user_i387_struct __user *)data);
  373. break;
  374. }
  375. default:
  376. ret = ptrace_request(child, request, addr, data);
  377. break;
  378. }
  379. return ret;
  380. }
  381. static void syscall_trace(struct pt_regs *regs)
  382. {
  383. #if 0
  384. printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
  385. current->comm,
  386. regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
  387. current_thread_info()->flags, current->ptrace);
  388. #endif
  389. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  390. ? 0x80 : 0));
  391. /*
  392. * this isn't the same as continuing with a signal, but it will do
  393. * for normal use. strace only continues with a signal if the
  394. * stopping signal is not SIGTRAP. -brl
  395. */
  396. if (current->exit_code) {
  397. send_sig(current->exit_code, current, 1);
  398. current->exit_code = 0;
  399. }
  400. }
  401. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  402. {
  403. /* do the secure computing check first */
  404. secure_computing(regs->orig_ax);
  405. if (test_thread_flag(TIF_SYSCALL_TRACE)
  406. && (current->ptrace & PT_PTRACED))
  407. syscall_trace(regs);
  408. if (unlikely(current->audit_context)) {
  409. if (test_thread_flag(TIF_IA32)) {
  410. audit_syscall_entry(AUDIT_ARCH_I386,
  411. regs->orig_ax,
  412. regs->bx, regs->cx,
  413. regs->dx, regs->si);
  414. } else {
  415. audit_syscall_entry(AUDIT_ARCH_X86_64,
  416. regs->orig_ax,
  417. regs->di, regs->si,
  418. regs->dx, regs->r10);
  419. }
  420. }
  421. }
  422. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  423. {
  424. if (unlikely(current->audit_context))
  425. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  426. if ((test_thread_flag(TIF_SYSCALL_TRACE)
  427. || test_thread_flag(TIF_SINGLESTEP))
  428. && (current->ptrace & PT_PTRACED))
  429. syscall_trace(regs);
  430. }