ptrace_64.c 5.5 KB

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