ptrace32.c 8.7 KB

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