ptrace_64.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. case ORIG_RAX:
  78. break;
  79. case FS:
  80. case GS:
  81. case DS:
  82. case ES:
  83. case SS:
  84. case CS:
  85. if (value && (value & 3) != 3)
  86. return -EIO;
  87. value &= 0xffff;
  88. break;
  89. case FS_BASE:
  90. case GS_BASE:
  91. if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
  92. return -EIO;
  93. break;
  94. case EFLAGS:
  95. value &= FLAG_MASK;
  96. child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
  97. return 0;
  98. default:
  99. panic("Bad register in putreg(): %d\n", regno);
  100. }
  101. child->thread.regs.regs.gp[reg_offsets[regno >> 3]] = value;
  102. return 0;
  103. }
  104. int poke_user(struct task_struct *child, long addr, long data)
  105. {
  106. if ((addr & 3) || addr < 0)
  107. return -EIO;
  108. if (addr < MAX_REG_OFFSET)
  109. return putreg(child, addr, data);
  110. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  111. (addr <= offsetof(struct user, u_debugreg[7]))) {
  112. addr -= offsetof(struct user, u_debugreg[0]);
  113. addr = addr >> 3;
  114. if ((addr == 4) || (addr == 5))
  115. return -EIO;
  116. child->thread.arch.debugregs[addr] = data;
  117. return 0;
  118. }
  119. return -EIO;
  120. }
  121. unsigned long getreg(struct task_struct *child, int regno)
  122. {
  123. unsigned long mask = ~0UL;
  124. #ifdef TIF_IA32
  125. if (test_tsk_thread_flag(child, TIF_IA32))
  126. mask = 0xffffffff;
  127. #endif
  128. switch (regno) {
  129. case R8:
  130. case R9:
  131. case R10:
  132. case R11:
  133. case R12:
  134. case R13:
  135. case R14:
  136. case R15:
  137. case RIP:
  138. case RSP:
  139. case RAX:
  140. case RBX:
  141. case RCX:
  142. case RDX:
  143. case RSI:
  144. case RDI:
  145. case RBP:
  146. case ORIG_RAX:
  147. case EFLAGS:
  148. case FS_BASE:
  149. case GS_BASE:
  150. break;
  151. case FS:
  152. case GS:
  153. case DS:
  154. case ES:
  155. case SS:
  156. case CS:
  157. mask = 0xffff;
  158. break;
  159. default:
  160. panic("Bad register in getreg: %d\n", regno);
  161. }
  162. return mask & child->thread.regs.regs.gp[reg_offsets[regno >> 3]];
  163. }
  164. int peek_user(struct task_struct *child, long addr, long data)
  165. {
  166. /* read the word at location addr in the USER area. */
  167. unsigned long tmp;
  168. if ((addr & 3) || addr < 0)
  169. return -EIO;
  170. tmp = 0; /* Default return condition */
  171. if (addr < MAX_REG_OFFSET)
  172. tmp = getreg(child, addr);
  173. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  174. (addr <= offsetof(struct user, u_debugreg[7]))) {
  175. addr -= offsetof(struct user, u_debugreg[0]);
  176. addr = addr >> 2;
  177. tmp = child->thread.arch.debugregs[addr];
  178. }
  179. return put_user(tmp, (unsigned long *) data);
  180. }
  181. /* XXX Mostly copied from sys-i386 */
  182. int is_syscall(unsigned long addr)
  183. {
  184. unsigned short instr;
  185. int n;
  186. n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
  187. if (n) {
  188. /*
  189. * access_process_vm() grants access to vsyscall and stub,
  190. * while copy_from_user doesn't. Maybe access_process_vm is
  191. * slow, but that doesn't matter, since it will be called only
  192. * in case of singlestepping, if copy_from_user failed.
  193. */
  194. n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
  195. if (n != sizeof(instr)) {
  196. printk("is_syscall : failed to read instruction from "
  197. "0x%lx\n", addr);
  198. return 1;
  199. }
  200. }
  201. /* sysenter */
  202. return instr == 0x050f;
  203. }
  204. static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  205. {
  206. int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
  207. long fpregs[HOST_FP_SIZE];
  208. BUG_ON(sizeof(*buf) != sizeof(fpregs));
  209. err = save_fp_registers(userspace_pid[cpu], fpregs);
  210. if (err)
  211. return err;
  212. n = copy_to_user(buf, fpregs, sizeof(fpregs));
  213. if (n > 0)
  214. return -EFAULT;
  215. return n;
  216. }
  217. static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  218. {
  219. int n, cpu = ((struct thread_info *) child->stack)->cpu;
  220. long fpregs[HOST_FP_SIZE];
  221. BUG_ON(sizeof(*buf) != sizeof(fpregs));
  222. n = copy_from_user(fpregs, buf, sizeof(fpregs));
  223. if (n > 0)
  224. return -EFAULT;
  225. return restore_fp_registers(userspace_pid[cpu], fpregs);
  226. }
  227. long subarch_ptrace(struct task_struct *child, long request,
  228. unsigned long addr, unsigned long data)
  229. {
  230. int ret = -EIO;
  231. void __user *datap = (void __user *) data;
  232. switch (request) {
  233. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  234. ret = get_fpregs(datap, child);
  235. break;
  236. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  237. ret = set_fpregs(datap, child);
  238. break;
  239. case PTRACE_ARCH_PRCTL:
  240. /* XXX Calls ptrace on the host - needs some SMP thinking */
  241. ret = arch_prctl(child, data, (void __user *) addr);
  242. break;
  243. }
  244. return ret;
  245. }