ptrace_32.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/sched.h>
  7. #include <linux/uaccess.h>
  8. #include <asm/ptrace-abi.h>
  9. #include <registers.h>
  10. #include <skas.h>
  11. extern int arch_switch_tls(struct task_struct *to);
  12. void arch_switch_to(struct task_struct *to)
  13. {
  14. int err = arch_switch_tls(to);
  15. if (!err)
  16. return;
  17. if (err != -EINVAL)
  18. printk(KERN_WARNING "arch_switch_tls failed, errno %d, "
  19. "not EINVAL\n", -err);
  20. else
  21. printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n");
  22. }
  23. int is_syscall(unsigned long addr)
  24. {
  25. unsigned short instr;
  26. int n;
  27. n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
  28. if (n) {
  29. /* access_process_vm() grants access to vsyscall and stub,
  30. * while copy_from_user doesn't. Maybe access_process_vm is
  31. * slow, but that doesn't matter, since it will be called only
  32. * in case of singlestepping, if copy_from_user failed.
  33. */
  34. n = access_process_vm(current, addr, &instr, sizeof(instr),
  35. FOLL_FORCE);
  36. if (n != sizeof(instr)) {
  37. printk(KERN_ERR "is_syscall : failed to read "
  38. "instruction from 0x%lx\n", addr);
  39. return 1;
  40. }
  41. }
  42. /* int 0x80 or sysenter */
  43. return (instr == 0x80cd) || (instr == 0x340f);
  44. }
  45. /* determines which flags the user has access to. */
  46. /* 1 = access 0 = no access */
  47. #define FLAG_MASK 0x00044dd5
  48. static const int reg_offsets[] = {
  49. [EBX] = HOST_BX,
  50. [ECX] = HOST_CX,
  51. [EDX] = HOST_DX,
  52. [ESI] = HOST_SI,
  53. [EDI] = HOST_DI,
  54. [EBP] = HOST_BP,
  55. [EAX] = HOST_AX,
  56. [DS] = HOST_DS,
  57. [ES] = HOST_ES,
  58. [FS] = HOST_FS,
  59. [GS] = HOST_GS,
  60. [EIP] = HOST_IP,
  61. [CS] = HOST_CS,
  62. [EFL] = HOST_EFLAGS,
  63. [UESP] = HOST_SP,
  64. [SS] = HOST_SS,
  65. [ORIG_EAX] = HOST_ORIG_AX,
  66. };
  67. int putreg(struct task_struct *child, int regno, unsigned long value)
  68. {
  69. regno >>= 2;
  70. switch (regno) {
  71. case EBX:
  72. case ECX:
  73. case EDX:
  74. case ESI:
  75. case EDI:
  76. case EBP:
  77. case EAX:
  78. case EIP:
  79. case UESP:
  80. break;
  81. case ORIG_EAX:
  82. /* Update the syscall number. */
  83. UPT_SYSCALL_NR(&child->thread.regs.regs) = value;
  84. break;
  85. case FS:
  86. if (value && (value & 3) != 3)
  87. return -EIO;
  88. break;
  89. case GS:
  90. if (value && (value & 3) != 3)
  91. return -EIO;
  92. break;
  93. case DS:
  94. case ES:
  95. if (value && (value & 3) != 3)
  96. return -EIO;
  97. value &= 0xffff;
  98. break;
  99. case SS:
  100. case CS:
  101. if ((value & 3) != 3)
  102. return -EIO;
  103. value &= 0xffff;
  104. break;
  105. case EFL:
  106. value &= FLAG_MASK;
  107. child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
  108. return 0;
  109. default :
  110. panic("Bad register in putreg() : %d\n", regno);
  111. }
  112. child->thread.regs.regs.gp[reg_offsets[regno]] = value;
  113. return 0;
  114. }
  115. int poke_user(struct task_struct *child, long addr, long data)
  116. {
  117. if ((addr & 3) || addr < 0)
  118. return -EIO;
  119. if (addr < MAX_REG_OFFSET)
  120. return putreg(child, addr, data);
  121. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  122. (addr <= offsetof(struct user, u_debugreg[7]))) {
  123. addr -= offsetof(struct user, u_debugreg[0]);
  124. addr = addr >> 2;
  125. if ((addr == 4) || (addr == 5))
  126. return -EIO;
  127. child->thread.arch.debugregs[addr] = data;
  128. return 0;
  129. }
  130. return -EIO;
  131. }
  132. unsigned long getreg(struct task_struct *child, int regno)
  133. {
  134. unsigned long mask = ~0UL;
  135. regno >>= 2;
  136. switch (regno) {
  137. case FS:
  138. case GS:
  139. case DS:
  140. case ES:
  141. case SS:
  142. case CS:
  143. mask = 0xffff;
  144. break;
  145. case EIP:
  146. case UESP:
  147. case EAX:
  148. case EBX:
  149. case ECX:
  150. case EDX:
  151. case ESI:
  152. case EDI:
  153. case EBP:
  154. case EFL:
  155. case ORIG_EAX:
  156. break;
  157. default:
  158. panic("Bad register in getreg() : %d\n", regno);
  159. }
  160. return mask & child->thread.regs.regs.gp[reg_offsets[regno]];
  161. }
  162. /* read the word at location addr in the USER area. */
  163. int peek_user(struct task_struct *child, long addr, long data)
  164. {
  165. unsigned long tmp;
  166. if ((addr & 3) || addr < 0)
  167. return -EIO;
  168. tmp = 0; /* Default return condition */
  169. if (addr < MAX_REG_OFFSET) {
  170. tmp = getreg(child, addr);
  171. }
  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 __user *) data);
  179. }
  180. static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  181. {
  182. int err, n, cpu = task_cpu(child);
  183. struct user_i387_struct fpregs;
  184. err = save_i387_registers(userspace_pid[cpu],
  185. (unsigned long *) &fpregs);
  186. if (err)
  187. return err;
  188. n = copy_to_user(buf, &fpregs, sizeof(fpregs));
  189. if(n > 0)
  190. return -EFAULT;
  191. return n;
  192. }
  193. static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  194. {
  195. int n, cpu = task_cpu(child);
  196. struct user_i387_struct fpregs;
  197. n = copy_from_user(&fpregs, buf, sizeof(fpregs));
  198. if (n > 0)
  199. return -EFAULT;
  200. return restore_i387_registers(userspace_pid[cpu],
  201. (unsigned long *) &fpregs);
  202. }
  203. static int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
  204. {
  205. int err, n, cpu = task_cpu(child);
  206. struct user_fxsr_struct fpregs;
  207. err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
  208. if (err)
  209. return err;
  210. n = copy_to_user(buf, &fpregs, sizeof(fpregs));
  211. if(n > 0)
  212. return -EFAULT;
  213. return n;
  214. }
  215. static int set_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
  216. {
  217. int n, cpu = task_cpu(child);
  218. struct user_fxsr_struct fpregs;
  219. n = copy_from_user(&fpregs, buf, sizeof(fpregs));
  220. if (n > 0)
  221. return -EFAULT;
  222. return restore_fpx_registers(userspace_pid[cpu],
  223. (unsigned long *) &fpregs);
  224. }
  225. long subarch_ptrace(struct task_struct *child, long request,
  226. unsigned long addr, unsigned long data)
  227. {
  228. int ret = -EIO;
  229. void __user *datap = (void __user *) data;
  230. switch (request) {
  231. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  232. ret = get_fpregs(datap, child);
  233. break;
  234. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  235. ret = set_fpregs(datap, child);
  236. break;
  237. case PTRACE_GETFPXREGS: /* Get the child FPU state. */
  238. ret = get_fpxregs(datap, child);
  239. break;
  240. case PTRACE_SETFPXREGS: /* Set the child FPU state. */
  241. ret = set_fpxregs(datap, child);
  242. break;
  243. default:
  244. ret = -EIO;
  245. }
  246. return ret;
  247. }