ptrace_64.c 12 KB

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