ptrace_32.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. [ORIG_EAX] = HOST_ORIG_AX,
  64. };
  65. int putreg(struct task_struct *child, int regno, unsigned long value)
  66. {
  67. regno >>= 2;
  68. switch (regno) {
  69. case EBX:
  70. case ECX:
  71. case EDX:
  72. case ESI:
  73. case EDI:
  74. case EBP:
  75. case EAX:
  76. case EIP:
  77. case UESP:
  78. case ORIG_EAX:
  79. break;
  80. case FS:
  81. if (value && (value & 3) != 3)
  82. return -EIO;
  83. break;
  84. case GS:
  85. if (value && (value & 3) != 3)
  86. return -EIO;
  87. break;
  88. case DS:
  89. case ES:
  90. if (value && (value & 3) != 3)
  91. return -EIO;
  92. value &= 0xffff;
  93. break;
  94. case SS:
  95. case CS:
  96. if ((value & 3) != 3)
  97. return -EIO;
  98. value &= 0xffff;
  99. break;
  100. case EFL:
  101. value &= FLAG_MASK;
  102. child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
  103. return 0;
  104. default :
  105. panic("Bad register in putreg() : %d\n", regno);
  106. }
  107. child->thread.regs.regs.gp[reg_offsets[regno]] = value;
  108. return 0;
  109. }
  110. int poke_user(struct task_struct *child, long addr, long data)
  111. {
  112. if ((addr & 3) || addr < 0)
  113. return -EIO;
  114. if (addr < MAX_REG_OFFSET)
  115. return putreg(child, addr, data);
  116. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  117. (addr <= offsetof(struct user, u_debugreg[7]))) {
  118. addr -= offsetof(struct user, u_debugreg[0]);
  119. addr = addr >> 2;
  120. if ((addr == 4) || (addr == 5))
  121. return -EIO;
  122. child->thread.arch.debugregs[addr] = data;
  123. return 0;
  124. }
  125. return -EIO;
  126. }
  127. unsigned long getreg(struct task_struct *child, int regno)
  128. {
  129. unsigned long mask = ~0UL;
  130. regno >>= 2;
  131. switch (regno) {
  132. case FS:
  133. case GS:
  134. case DS:
  135. case ES:
  136. case SS:
  137. case CS:
  138. mask = 0xffff;
  139. break;
  140. case EIP:
  141. case UESP:
  142. case EAX:
  143. case EBX:
  144. case ECX:
  145. case EDX:
  146. case ESI:
  147. case EDI:
  148. case EBP:
  149. case EFL:
  150. case ORIG_EAX:
  151. break;
  152. default:
  153. panic("Bad register in getreg() : %d\n", regno);
  154. }
  155. return mask & child->thread.regs.regs.gp[reg_offsets[regno]];
  156. }
  157. /* read the word at location addr in the USER area. */
  158. int peek_user(struct task_struct *child, long addr, long data)
  159. {
  160. unsigned long tmp;
  161. if ((addr & 3) || addr < 0)
  162. return -EIO;
  163. tmp = 0; /* Default return condition */
  164. if (addr < MAX_REG_OFFSET) {
  165. tmp = getreg(child, addr);
  166. }
  167. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  168. (addr <= offsetof(struct user, u_debugreg[7]))) {
  169. addr -= offsetof(struct user, u_debugreg[0]);
  170. addr = addr >> 2;
  171. tmp = child->thread.arch.debugregs[addr];
  172. }
  173. return put_user(tmp, (unsigned long __user *) data);
  174. }
  175. static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  176. {
  177. int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
  178. struct user_i387_struct fpregs;
  179. err = save_i387_registers(userspace_pid[cpu],
  180. (unsigned long *) &fpregs);
  181. if (err)
  182. return err;
  183. n = copy_to_user(buf, &fpregs, sizeof(fpregs));
  184. if(n > 0)
  185. return -EFAULT;
  186. return n;
  187. }
  188. static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  189. {
  190. int n, cpu = ((struct thread_info *) child->stack)->cpu;
  191. struct user_i387_struct fpregs;
  192. n = copy_from_user(&fpregs, buf, sizeof(fpregs));
  193. if (n > 0)
  194. return -EFAULT;
  195. return restore_i387_registers(userspace_pid[cpu],
  196. (unsigned long *) &fpregs);
  197. }
  198. static int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
  199. {
  200. int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
  201. struct user_fxsr_struct fpregs;
  202. err = save_fpx_registers(userspace_pid[cpu], (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_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
  211. {
  212. int n, cpu = ((struct thread_info *) child->stack)->cpu;
  213. struct user_fxsr_struct fpregs;
  214. n = copy_from_user(&fpregs, buf, sizeof(fpregs));
  215. if (n > 0)
  216. return -EFAULT;
  217. return restore_fpx_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_GETFPXREGS: /* Get the child FPU state. */
  233. ret = get_fpxregs(datap, child);
  234. break;
  235. case PTRACE_SETFPXREGS: /* Set the child FPU state. */
  236. ret = set_fpxregs(datap, child);
  237. break;
  238. default:
  239. ret = -EIO;
  240. }
  241. return ret;
  242. }