ptrace32.c 8.7 KB

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