ptrace32.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * 32bit ptrace for x86-64.
  3. *
  4. * Copyright 2001,2002 Andi Kleen, SuSE Labs.
  5. * Some parts copied from arch/i386/kernel/ptrace.c. See that file for earlier
  6. * copyright.
  7. *
  8. * This allows to access 64bit processes too; but there is no way to
  9. * see the extended register contents.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/stddef.h>
  13. #include <linux/sched.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/unistd.h>
  16. #include <linux/mm.h>
  17. #include <linux/err.h>
  18. #include <linux/ptrace.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/compat.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/user32.h>
  23. #include <asm/user.h>
  24. #include <asm/errno.h>
  25. #include <asm/debugreg.h>
  26. #include <asm/i387.h>
  27. #include <asm/fpu32.h>
  28. #include <asm/ia32.h>
  29. /*
  30. * Determines which flags the user has access to [1 = access, 0 = no access].
  31. * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9).
  32. * Also masks reserved bits (31-22, 15, 5, 3, 1).
  33. */
  34. #define FLAG_MASK 0x54dd5UL
  35. #define R32(l,q) \
  36. case offsetof(struct user32, regs.l): \
  37. regs->q = val; break;
  38. static int putreg32(struct task_struct *child, unsigned regno, u32 val)
  39. {
  40. struct pt_regs *regs = task_pt_regs(child);
  41. switch (regno) {
  42. case offsetof(struct user32, regs.fs):
  43. if (val && (val & 3) != 3)
  44. return -EIO;
  45. child->thread.fsindex = val & 0xffff;
  46. if (child == current)
  47. loadsegment(fs, child->thread.fsindex);
  48. break;
  49. case offsetof(struct user32, regs.gs):
  50. if (val && (val & 3) != 3)
  51. return -EIO;
  52. child->thread.gsindex = val & 0xffff;
  53. if (child == current)
  54. load_gs_index(child->thread.gsindex);
  55. break;
  56. case offsetof(struct user32, regs.ds):
  57. if (val && (val & 3) != 3)
  58. return -EIO;
  59. child->thread.ds = val & 0xffff;
  60. if (child == current)
  61. loadsegment(ds, child->thread.ds);
  62. break;
  63. case offsetof(struct user32, regs.es):
  64. child->thread.es = val & 0xffff;
  65. if (child == current)
  66. loadsegment(es, child->thread.ds);
  67. break;
  68. case offsetof(struct user32, regs.ss):
  69. if ((val & 3) != 3)
  70. return -EIO;
  71. regs->ss = val & 0xffff;
  72. break;
  73. case offsetof(struct user32, regs.cs):
  74. if ((val & 3) != 3)
  75. return -EIO;
  76. regs->cs = val & 0xffff;
  77. break;
  78. R32(ebx, bx);
  79. R32(ecx, cx);
  80. R32(edx, dx);
  81. R32(edi, di);
  82. R32(esi, si);
  83. R32(ebp, bp);
  84. R32(eax, ax);
  85. R32(orig_eax, orig_ax);
  86. R32(eip, ip);
  87. R32(esp, sp);
  88. case offsetof(struct user32, regs.eflags):
  89. val &= FLAG_MASK;
  90. /*
  91. * If the user value contains TF, mark that
  92. * it was not "us" (the debugger) that set it.
  93. * If not, make sure it stays set if we had.
  94. */
  95. if (val & X86_EFLAGS_TF)
  96. clear_tsk_thread_flag(child, TIF_FORCED_TF);
  97. else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  98. val |= X86_EFLAGS_TF;
  99. regs->flags = val | (regs->flags & ~FLAG_MASK);
  100. break;
  101. case offsetof(struct user32, u_debugreg[0]) ...
  102. offsetof(struct user32, u_debugreg[7]):
  103. regno -= offsetof(struct user32, u_debugreg[0]);
  104. return ptrace_set_debugreg(child, regno / 4, val);
  105. default:
  106. if (regno > sizeof(struct user32) || (regno & 3))
  107. return -EIO;
  108. /*
  109. * Other dummy fields in the virtual user structure
  110. * are ignored
  111. */
  112. break;
  113. }
  114. return 0;
  115. }
  116. #undef R32
  117. #define R32(l,q) \
  118. case offsetof(struct user32, regs.l): \
  119. *val = regs->q; break
  120. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  121. {
  122. struct pt_regs *regs = task_pt_regs(child);
  123. switch (regno) {
  124. case offsetof(struct user32, regs.fs):
  125. *val = child->thread.fsindex;
  126. if (child == current)
  127. asm("movl %%fs,%0" : "=r" (*val));
  128. break;
  129. case offsetof(struct user32, regs.gs):
  130. *val = child->thread.gsindex;
  131. if (child == current)
  132. asm("movl %%gs,%0" : "=r" (*val));
  133. break;
  134. case offsetof(struct user32, regs.ds):
  135. *val = child->thread.ds;
  136. if (child == current)
  137. asm("movl %%ds,%0" : "=r" (*val));
  138. break;
  139. case offsetof(struct user32, regs.es):
  140. *val = child->thread.es;
  141. if (child == current)
  142. asm("movl %%es,%0" : "=r" (*val));
  143. break;
  144. R32(cs, cs);
  145. R32(ss, ss);
  146. R32(ebx, bx);
  147. R32(ecx, cx);
  148. R32(edx, dx);
  149. R32(edi, di);
  150. R32(esi, si);
  151. R32(ebp, bp);
  152. R32(eax, ax);
  153. R32(orig_eax, orig_ax);
  154. R32(eip, ip);
  155. R32(esp, sp);
  156. case offsetof(struct user32, regs.eflags):
  157. /*
  158. * If the debugger set TF, hide it from the readout.
  159. */
  160. *val = regs->flags;
  161. if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  162. *val &= ~X86_EFLAGS_TF;
  163. break;
  164. case offsetof(struct user32, u_debugreg[0]) ...
  165. offsetof(struct user32, u_debugreg[7]):
  166. regno -= offsetof(struct user32, u_debugreg[0]);
  167. *val = ptrace_get_debugreg(child, regno / 4);
  168. break;
  169. default:
  170. if (regno > sizeof(struct user32) || (regno & 3))
  171. return -EIO;
  172. /*
  173. * Other dummy fields in the virtual user structure
  174. * are ignored
  175. */
  176. *val = 0;
  177. break;
  178. }
  179. return 0;
  180. }
  181. #undef R32
  182. static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
  183. {
  184. siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
  185. compat_siginfo_t __user *si32 = compat_ptr(data);
  186. siginfo_t ssi;
  187. int ret;
  188. if (request == PTRACE_SETSIGINFO) {
  189. memset(&ssi, 0, sizeof(siginfo_t));
  190. ret = copy_siginfo_from_user32(&ssi, si32);
  191. if (ret)
  192. return ret;
  193. if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
  194. return -EFAULT;
  195. }
  196. ret = sys_ptrace(request, pid, addr, (unsigned long)si);
  197. if (ret)
  198. return ret;
  199. if (request == PTRACE_GETSIGINFO) {
  200. if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
  201. return -EFAULT;
  202. ret = copy_siginfo_to_user32(si32, &ssi);
  203. }
  204. return ret;
  205. }
  206. asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
  207. {
  208. struct task_struct *child;
  209. struct pt_regs *childregs;
  210. void __user *datap = compat_ptr(data);
  211. int ret;
  212. __u32 val;
  213. switch (request) {
  214. case PTRACE_TRACEME:
  215. case PTRACE_ATTACH:
  216. case PTRACE_KILL:
  217. case PTRACE_CONT:
  218. case PTRACE_SINGLESTEP:
  219. case PTRACE_SINGLEBLOCK:
  220. case PTRACE_DETACH:
  221. case PTRACE_SYSCALL:
  222. case PTRACE_OLDSETOPTIONS:
  223. case PTRACE_SETOPTIONS:
  224. case PTRACE_SET_THREAD_AREA:
  225. case PTRACE_GET_THREAD_AREA:
  226. return sys_ptrace(request, pid, addr, data);
  227. default:
  228. return -EINVAL;
  229. case PTRACE_PEEKTEXT:
  230. case PTRACE_PEEKDATA:
  231. case PTRACE_POKEDATA:
  232. case PTRACE_POKETEXT:
  233. case PTRACE_POKEUSR:
  234. case PTRACE_PEEKUSR:
  235. case PTRACE_GETREGS:
  236. case PTRACE_SETREGS:
  237. case PTRACE_SETFPREGS:
  238. case PTRACE_GETFPREGS:
  239. case PTRACE_SETFPXREGS:
  240. case PTRACE_GETFPXREGS:
  241. case PTRACE_GETEVENTMSG:
  242. break;
  243. case PTRACE_SETSIGINFO:
  244. case PTRACE_GETSIGINFO:
  245. return ptrace32_siginfo(request, pid, addr, data);
  246. }
  247. child = ptrace_get_task_struct(pid);
  248. if (IS_ERR(child))
  249. return PTR_ERR(child);
  250. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  251. if (ret < 0)
  252. goto out;
  253. childregs = task_pt_regs(child);
  254. switch (request) {
  255. case PTRACE_PEEKDATA:
  256. case PTRACE_PEEKTEXT:
  257. ret = 0;
  258. if (access_process_vm(child, addr, &val, sizeof(u32), 0) !=
  259. sizeof(u32))
  260. ret = -EIO;
  261. else
  262. ret = put_user(val, (unsigned int __user *)datap);
  263. break;
  264. case PTRACE_POKEDATA:
  265. case PTRACE_POKETEXT:
  266. ret = 0;
  267. if (access_process_vm(child, addr, &data, sizeof(u32), 1) !=
  268. sizeof(u32))
  269. ret = -EIO;
  270. break;
  271. case PTRACE_PEEKUSR:
  272. ret = getreg32(child, addr, &val);
  273. if (ret == 0)
  274. ret = put_user(val, (__u32 __user *)datap);
  275. break;
  276. case PTRACE_POKEUSR:
  277. ret = putreg32(child, addr, data);
  278. break;
  279. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  280. int i;
  281. if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
  282. ret = -EIO;
  283. break;
  284. }
  285. ret = 0;
  286. for (i = 0; i <= 16*4; i += sizeof(__u32)) {
  287. getreg32(child, i, &val);
  288. ret |= __put_user(val, (u32 __user *)datap);
  289. datap += sizeof(u32);
  290. }
  291. break;
  292. }
  293. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  294. unsigned long tmp;
  295. int i;
  296. if (!access_ok(VERIFY_READ, datap, 16*4)) {
  297. ret = -EIO;
  298. break;
  299. }
  300. ret = 0;
  301. for (i = 0; i <= 16*4; i += sizeof(u32)) {
  302. ret |= __get_user(tmp, (u32 __user *)datap);
  303. putreg32(child, i, tmp);
  304. datap += sizeof(u32);
  305. }
  306. break;
  307. }
  308. case PTRACE_GETFPREGS:
  309. ret = -EIO;
  310. if (!access_ok(VERIFY_READ, compat_ptr(data),
  311. sizeof(struct user_i387_struct)))
  312. break;
  313. save_i387_ia32(child, datap, childregs, 1);
  314. ret = 0;
  315. break;
  316. case PTRACE_SETFPREGS:
  317. ret = -EIO;
  318. if (!access_ok(VERIFY_WRITE, datap,
  319. sizeof(struct user_i387_struct)))
  320. break;
  321. ret = 0;
  322. /* don't check EFAULT to be bug-to-bug compatible to i386 */
  323. restore_i387_ia32(child, datap, 1);
  324. break;
  325. case PTRACE_GETFPXREGS: {
  326. struct user32_fxsr_struct __user *u = datap;
  327. init_fpu(child);
  328. ret = -EIO;
  329. if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
  330. break;
  331. ret = -EFAULT;
  332. if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
  333. break;
  334. ret = __put_user(childregs->cs, &u->fcs);
  335. ret |= __put_user(child->thread.ds, &u->fos);
  336. break;
  337. }
  338. case PTRACE_SETFPXREGS: {
  339. struct user32_fxsr_struct __user *u = datap;
  340. unlazy_fpu(child);
  341. ret = -EIO;
  342. if (!access_ok(VERIFY_READ, u, sizeof(*u)))
  343. break;
  344. /*
  345. * no checking to be bug-to-bug compatible with i386.
  346. * but silence warning
  347. */
  348. if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u)))
  349. ;
  350. set_stopped_child_used_math(child);
  351. child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
  352. ret = 0;
  353. break;
  354. }
  355. case PTRACE_GETEVENTMSG:
  356. ret = put_user(child->ptrace_message,
  357. (unsigned int __user *)compat_ptr(data));
  358. break;
  359. default:
  360. BUG();
  361. }
  362. out:
  363. put_task_struct(child);
  364. return ret;
  365. }