ptrace32.c 9.5 KB

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