ptrace_32.c 6.0 KB

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