ptrace_64.c 5.6 KB

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