ptrace_64.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2003 PathScale, Inc.
  3. * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. *
  5. * Licensed under the GPL
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/sched.h>
  9. #include <linux/errno.h>
  10. #define __FRAME_OFFSETS
  11. #include <asm/ptrace.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/ptrace-abi.h>
  14. /*
  15. * determines which flags the user has access to.
  16. * 1 = access 0 = no access
  17. */
  18. #define FLAG_MASK 0x44dd5UL
  19. static const int reg_offsets[] =
  20. {
  21. [R8 >> 3] = HOST_R8,
  22. [R9 >> 3] = HOST_R9,
  23. [R10 >> 3] = HOST_R10,
  24. [R11 >> 3] = HOST_R11,
  25. [R12 >> 3] = HOST_R12,
  26. [R13 >> 3] = HOST_R13,
  27. [R14 >> 3] = HOST_R14,
  28. [R15 >> 3] = HOST_R15,
  29. [RIP >> 3] = HOST_IP,
  30. [RSP >> 3] = HOST_SP,
  31. [RAX >> 3] = HOST_AX,
  32. [RBX >> 3] = HOST_BX,
  33. [RCX >> 3] = HOST_CX,
  34. [RDX >> 3] = HOST_DX,
  35. [RSI >> 3] = HOST_SI,
  36. [RDI >> 3] = HOST_DI,
  37. [RBP >> 3] = HOST_BP,
  38. [CS >> 3] = HOST_CS,
  39. [SS >> 3] = HOST_SS,
  40. [FS_BASE >> 3] = HOST_FS_BASE,
  41. [GS_BASE >> 3] = HOST_GS_BASE,
  42. [DS >> 3] = HOST_DS,
  43. [ES >> 3] = HOST_ES,
  44. [FS >> 3] = HOST_FS,
  45. [GS >> 3] = HOST_GS,
  46. [EFLAGS >> 3] = HOST_EFLAGS,
  47. [ORIG_RAX >> 3] = HOST_ORIG_AX,
  48. };
  49. int putreg(struct task_struct *child, int regno, unsigned long value)
  50. {
  51. #ifdef TIF_IA32
  52. /*
  53. * Some code in the 64bit emulation may not be 64bit clean.
  54. * Don't take any chances.
  55. */
  56. if (test_tsk_thread_flag(child, TIF_IA32))
  57. value &= 0xffffffff;
  58. #endif
  59. switch (regno) {
  60. case R8:
  61. case R9:
  62. case R10:
  63. case R11:
  64. case R12:
  65. case R13:
  66. case R14:
  67. case R15:
  68. case RIP:
  69. case RSP:
  70. case RAX:
  71. case RBX:
  72. case RCX:
  73. case RDX:
  74. case RSI:
  75. case RDI:
  76. case RBP:
  77. break;
  78. case ORIG_RAX:
  79. /* Update the syscall number. */
  80. UPT_SYSCALL_NR(&child->thread.regs.regs) = value;
  81. break;
  82. case FS:
  83. case GS:
  84. case DS:
  85. case ES:
  86. case SS:
  87. case CS:
  88. if (value && (value & 3) != 3)
  89. return -EIO;
  90. value &= 0xffff;
  91. break;
  92. case FS_BASE:
  93. case GS_BASE:
  94. if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
  95. return -EIO;
  96. break;
  97. case EFLAGS:
  98. value &= FLAG_MASK;
  99. child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
  100. return 0;
  101. default:
  102. panic("Bad register in putreg(): %d\n", regno);
  103. }
  104. child->thread.regs.regs.gp[reg_offsets[regno >> 3]] = value;
  105. return 0;
  106. }
  107. int poke_user(struct task_struct *child, long addr, long data)
  108. {
  109. if ((addr & 3) || addr < 0)
  110. return -EIO;
  111. if (addr < MAX_REG_OFFSET)
  112. return putreg(child, addr, data);
  113. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  114. (addr <= offsetof(struct user, u_debugreg[7]))) {
  115. addr -= offsetof(struct user, u_debugreg[0]);
  116. addr = addr >> 3;
  117. if ((addr == 4) || (addr == 5))
  118. return -EIO;
  119. child->thread.arch.debugregs[addr] = data;
  120. return 0;
  121. }
  122. return -EIO;
  123. }
  124. unsigned long getreg(struct task_struct *child, int regno)
  125. {
  126. unsigned long mask = ~0UL;
  127. #ifdef TIF_IA32
  128. if (test_tsk_thread_flag(child, TIF_IA32))
  129. mask = 0xffffffff;
  130. #endif
  131. switch (regno) {
  132. case R8:
  133. case R9:
  134. case R10:
  135. case R11:
  136. case R12:
  137. case R13:
  138. case R14:
  139. case R15:
  140. case RIP:
  141. case RSP:
  142. case RAX:
  143. case RBX:
  144. case RCX:
  145. case RDX:
  146. case RSI:
  147. case RDI:
  148. case RBP:
  149. case ORIG_RAX:
  150. case EFLAGS:
  151. case FS_BASE:
  152. case GS_BASE:
  153. break;
  154. case FS:
  155. case GS:
  156. case DS:
  157. case ES:
  158. case SS:
  159. case CS:
  160. mask = 0xffff;
  161. break;
  162. default:
  163. panic("Bad register in getreg: %d\n", regno);
  164. }
  165. return mask & child->thread.regs.regs.gp[reg_offsets[regno >> 3]];
  166. }
  167. int peek_user(struct task_struct *child, long addr, long data)
  168. {
  169. /* read the word at location addr in the USER area. */
  170. unsigned long tmp;
  171. if ((addr & 3) || addr < 0)
  172. return -EIO;
  173. tmp = 0; /* Default return condition */
  174. if (addr < MAX_REG_OFFSET)
  175. tmp = getreg(child, addr);
  176. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  177. (addr <= offsetof(struct user, u_debugreg[7]))) {
  178. addr -= offsetof(struct user, u_debugreg[0]);
  179. addr = addr >> 2;
  180. tmp = child->thread.arch.debugregs[addr];
  181. }
  182. return put_user(tmp, (unsigned long *) data);
  183. }
  184. /* XXX Mostly copied from sys-i386 */
  185. int is_syscall(unsigned long addr)
  186. {
  187. unsigned short instr;
  188. int n;
  189. n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
  190. if (n) {
  191. /*
  192. * access_process_vm() grants access to vsyscall and stub,
  193. * while copy_from_user doesn't. Maybe access_process_vm is
  194. * slow, but that doesn't matter, since it will be called only
  195. * in case of singlestepping, if copy_from_user failed.
  196. */
  197. n = access_process_vm(current, addr, &instr, sizeof(instr),
  198. FOLL_FORCE);
  199. if (n != sizeof(instr)) {
  200. printk("is_syscall : failed to read instruction from "
  201. "0x%lx\n", addr);
  202. return 1;
  203. }
  204. }
  205. /* sysenter */
  206. return instr == 0x050f;
  207. }
  208. static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  209. {
  210. int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
  211. struct user_i387_struct fpregs;
  212. err = save_i387_registers(userspace_pid[cpu],
  213. (unsigned long *) &fpregs);
  214. if (err)
  215. return err;
  216. n = copy_to_user(buf, &fpregs, sizeof(fpregs));
  217. if (n > 0)
  218. return -EFAULT;
  219. return n;
  220. }
  221. static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  222. {
  223. int n, cpu = ((struct thread_info *) child->stack)->cpu;
  224. struct user_i387_struct fpregs;
  225. n = copy_from_user(&fpregs, buf, sizeof(fpregs));
  226. if (n > 0)
  227. return -EFAULT;
  228. return restore_i387_registers(userspace_pid[cpu],
  229. (unsigned long *) &fpregs);
  230. }
  231. long subarch_ptrace(struct task_struct *child, long request,
  232. unsigned long addr, unsigned long data)
  233. {
  234. int ret = -EIO;
  235. void __user *datap = (void __user *) data;
  236. switch (request) {
  237. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  238. ret = get_fpregs(datap, child);
  239. break;
  240. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  241. ret = set_fpregs(datap, child);
  242. break;
  243. case PTRACE_ARCH_PRCTL:
  244. /* XXX Calls ptrace on the host - needs some SMP thinking */
  245. ret = arch_prctl(child, data, (void __user *) addr);
  246. break;
  247. }
  248. return ret;
  249. }