ptrace_32.c 6.0 KB

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