ptrace_64.c 5.5 KB

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