ptrace32.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 see the extended
  9. * register contents.
  10. *
  11. * $Id: ptrace32.c,v 1.16 2003/03/14 16:06:35 ak Exp $
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/stddef.h>
  15. #include <linux/sched.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/unistd.h>
  18. #include <linux/mm.h>
  19. #include <linux/ptrace.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/compat.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/user32.h>
  24. #include <asm/user.h>
  25. #include <asm/errno.h>
  26. #include <asm/debugreg.h>
  27. #include <asm/i387.h>
  28. #include <asm/fpu32.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): stack[offsetof(struct pt_regs, q)/8] = val; break
  37. static int putreg32(struct task_struct *child, unsigned regno, u32 val)
  38. {
  39. int i;
  40. __u64 *stack = (__u64 *)task_pt_regs(child);
  41. switch (regno) {
  42. case offsetof(struct user32, regs.fs):
  43. if (val && (val & 3) != 3) return -EIO;
  44. child->thread.fsindex = val & 0xffff;
  45. break;
  46. case offsetof(struct user32, regs.gs):
  47. if (val && (val & 3) != 3) return -EIO;
  48. child->thread.gsindex = val & 0xffff;
  49. break;
  50. case offsetof(struct user32, regs.ds):
  51. if (val && (val & 3) != 3) return -EIO;
  52. child->thread.ds = val & 0xffff;
  53. break;
  54. case offsetof(struct user32, regs.es):
  55. child->thread.es = val & 0xffff;
  56. break;
  57. case offsetof(struct user32, regs.ss):
  58. if ((val & 3) != 3) return -EIO;
  59. stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff;
  60. break;
  61. case offsetof(struct user32, regs.cs):
  62. if ((val & 3) != 3) return -EIO;
  63. stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff;
  64. break;
  65. R32(ebx, rbx);
  66. R32(ecx, rcx);
  67. R32(edx, rdx);
  68. R32(edi, rdi);
  69. R32(esi, rsi);
  70. R32(ebp, rbp);
  71. R32(eax, rax);
  72. R32(orig_eax, orig_rax);
  73. R32(eip, rip);
  74. R32(esp, rsp);
  75. case offsetof(struct user32, regs.eflags): {
  76. __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8];
  77. val &= FLAG_MASK;
  78. *flags = val | (*flags & ~FLAG_MASK);
  79. break;
  80. }
  81. case offsetof(struct user32, u_debugreg[4]):
  82. case offsetof(struct user32, u_debugreg[5]):
  83. return -EIO;
  84. case offsetof(struct user32, u_debugreg[0]):
  85. child->thread.debugreg0 = val;
  86. break;
  87. case offsetof(struct user32, u_debugreg[1]):
  88. child->thread.debugreg1 = val;
  89. break;
  90. case offsetof(struct user32, u_debugreg[2]):
  91. child->thread.debugreg2 = val;
  92. break;
  93. case offsetof(struct user32, u_debugreg[3]):
  94. child->thread.debugreg3 = val;
  95. break;
  96. case offsetof(struct user32, u_debugreg[6]):
  97. child->thread.debugreg6 = val;
  98. break;
  99. case offsetof(struct user32, u_debugreg[7]):
  100. val &= ~DR_CONTROL_RESERVED;
  101. /* See arch/i386/kernel/ptrace.c for an explanation of
  102. * this awkward check.*/
  103. for(i=0; i<4; i++)
  104. if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
  105. return -EIO;
  106. child->thread.debugreg7 = val;
  107. break;
  108. default:
  109. if (regno > sizeof(struct user32) || (regno & 3))
  110. return -EIO;
  111. /* Other dummy fields in the virtual user structure are ignored */
  112. break;
  113. }
  114. return 0;
  115. }
  116. #undef R32
  117. #define R32(l,q) \
  118. case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break
  119. static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
  120. {
  121. __u64 *stack = (__u64 *)task_pt_regs(child);
  122. switch (regno) {
  123. case offsetof(struct user32, regs.fs):
  124. *val = child->thread.fsindex;
  125. break;
  126. case offsetof(struct user32, regs.gs):
  127. *val = child->thread.gsindex;
  128. break;
  129. case offsetof(struct user32, regs.ds):
  130. *val = child->thread.ds;
  131. break;
  132. case offsetof(struct user32, regs.es):
  133. *val = child->thread.es;
  134. break;
  135. R32(cs, cs);
  136. R32(ss, ss);
  137. R32(ebx, rbx);
  138. R32(ecx, rcx);
  139. R32(edx, rdx);
  140. R32(edi, rdi);
  141. R32(esi, rsi);
  142. R32(ebp, rbp);
  143. R32(eax, rax);
  144. R32(orig_eax, orig_rax);
  145. R32(eip, rip);
  146. R32(eflags, eflags);
  147. R32(esp, rsp);
  148. case offsetof(struct user32, u_debugreg[0]):
  149. *val = child->thread.debugreg0;
  150. break;
  151. case offsetof(struct user32, u_debugreg[1]):
  152. *val = child->thread.debugreg1;
  153. break;
  154. case offsetof(struct user32, u_debugreg[2]):
  155. *val = child->thread.debugreg2;
  156. break;
  157. case offsetof(struct user32, u_debugreg[3]):
  158. *val = child->thread.debugreg3;
  159. break;
  160. case offsetof(struct user32, u_debugreg[6]):
  161. *val = child->thread.debugreg6;
  162. break;
  163. case offsetof(struct user32, u_debugreg[7]):
  164. *val = child->thread.debugreg7;
  165. break;
  166. default:
  167. if (regno > sizeof(struct user32) || (regno & 3))
  168. return -EIO;
  169. /* Other dummy fields in the virtual user structure are ignored */
  170. *val = 0;
  171. break;
  172. }
  173. return 0;
  174. }
  175. #undef R32
  176. asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
  177. {
  178. struct task_struct *child;
  179. struct pt_regs *childregs;
  180. void __user *datap = compat_ptr(data);
  181. int ret;
  182. __u32 val;
  183. switch (request) {
  184. default:
  185. return sys_ptrace(request, pid, addr, data);
  186. case PTRACE_PEEKTEXT:
  187. case PTRACE_PEEKDATA:
  188. case PTRACE_POKEDATA:
  189. case PTRACE_POKETEXT:
  190. case PTRACE_POKEUSR:
  191. case PTRACE_PEEKUSR:
  192. case PTRACE_GETREGS:
  193. case PTRACE_SETREGS:
  194. case PTRACE_SETFPREGS:
  195. case PTRACE_GETFPREGS:
  196. case PTRACE_SETFPXREGS:
  197. case PTRACE_GETFPXREGS:
  198. case PTRACE_GETEVENTMSG:
  199. break;
  200. }
  201. if (request == PTRACE_TRACEME)
  202. return ptrace_traceme();
  203. child = ptrace_get_task_struct(pid);
  204. if (IS_ERR(child))
  205. return PTR_ERR(child);
  206. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  207. if (ret < 0)
  208. goto out;
  209. childregs = task_pt_regs(child);
  210. switch (request) {
  211. case PTRACE_PEEKDATA:
  212. case PTRACE_PEEKTEXT:
  213. ret = 0;
  214. if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32))
  215. ret = -EIO;
  216. else
  217. ret = put_user(val, (unsigned int __user *)datap);
  218. break;
  219. case PTRACE_POKEDATA:
  220. case PTRACE_POKETEXT:
  221. ret = 0;
  222. if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32))
  223. ret = -EIO;
  224. break;
  225. case PTRACE_PEEKUSR:
  226. ret = getreg32(child, addr, &val);
  227. if (ret == 0)
  228. ret = put_user(val, (__u32 __user *)datap);
  229. break;
  230. case PTRACE_POKEUSR:
  231. ret = putreg32(child, addr, data);
  232. break;
  233. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  234. int i;
  235. if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
  236. ret = -EIO;
  237. break;
  238. }
  239. ret = 0;
  240. for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) {
  241. getreg32(child, i, &val);
  242. ret |= __put_user(val,(u32 __user *)datap);
  243. datap += sizeof(u32);
  244. }
  245. break;
  246. }
  247. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  248. unsigned long tmp;
  249. int i;
  250. if (!access_ok(VERIFY_READ, datap, 16*4)) {
  251. ret = -EIO;
  252. break;
  253. }
  254. ret = 0;
  255. for ( i = 0; i <= 16*4; i += sizeof(u32) ) {
  256. ret |= __get_user(tmp, (u32 __user *)datap);
  257. putreg32(child, i, tmp);
  258. datap += sizeof(u32);
  259. }
  260. break;
  261. }
  262. case PTRACE_GETFPREGS:
  263. ret = -EIO;
  264. if (!access_ok(VERIFY_READ, compat_ptr(data),
  265. sizeof(struct user_i387_struct)))
  266. break;
  267. save_i387_ia32(child, datap, childregs, 1);
  268. ret = 0;
  269. break;
  270. case PTRACE_SETFPREGS:
  271. ret = -EIO;
  272. if (!access_ok(VERIFY_WRITE, datap,
  273. sizeof(struct user_i387_struct)))
  274. break;
  275. ret = 0;
  276. /* don't check EFAULT to be bug-to-bug compatible to i386 */
  277. restore_i387_ia32(child, datap, 1);
  278. break;
  279. case PTRACE_GETFPXREGS: {
  280. struct user32_fxsr_struct __user *u = datap;
  281. init_fpu(child);
  282. ret = -EIO;
  283. if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
  284. break;
  285. ret = -EFAULT;
  286. if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
  287. break;
  288. ret = __put_user(childregs->cs, &u->fcs);
  289. ret |= __put_user(child->thread.ds, &u->fos);
  290. break;
  291. }
  292. case PTRACE_SETFPXREGS: {
  293. struct user32_fxsr_struct __user *u = datap;
  294. unlazy_fpu(child);
  295. ret = -EIO;
  296. if (!access_ok(VERIFY_READ, u, sizeof(*u)))
  297. break;
  298. /* no checking to be bug-to-bug compatible with i386 */
  299. __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u));
  300. set_stopped_child_used_math(child);
  301. child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
  302. ret = 0;
  303. break;
  304. }
  305. case PTRACE_GETEVENTMSG:
  306. ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data));
  307. break;
  308. default:
  309. ret = -EINVAL;
  310. break;
  311. }
  312. out:
  313. put_task_struct(child);
  314. return ret;
  315. }