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 "skas.h"
  9. extern int arch_switch_tls(struct task_struct *to);
  10. void arch_switch_to(struct task_struct *to)
  11. {
  12. int err = arch_switch_tls(to);
  13. if (!err)
  14. return;
  15. if (err != -EINVAL)
  16. printk(KERN_WARNING "arch_switch_tls failed, errno %d, "
  17. "not EINVAL\n", -err);
  18. else
  19. printk(KERN_WARNING "arch_switch_tls failed, errno = EINVAL\n");
  20. }
  21. int is_syscall(unsigned long addr)
  22. {
  23. unsigned short instr;
  24. int n;
  25. n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
  26. if (n) {
  27. /* access_process_vm() grants access to vsyscall and stub,
  28. * while copy_from_user doesn't. Maybe access_process_vm is
  29. * slow, but that doesn't matter, since it will be called only
  30. * in case of singlestepping, if copy_from_user failed.
  31. */
  32. n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
  33. if (n != sizeof(instr)) {
  34. printk(KERN_ERR "is_syscall : failed to read "
  35. "instruction from 0x%lx\n", addr);
  36. return 1;
  37. }
  38. }
  39. /* int 0x80 or sysenter */
  40. return (instr == 0x80cd) || (instr == 0x340f);
  41. }
  42. /* determines which flags the user has access to. */
  43. /* 1 = access 0 = no access */
  44. #define FLAG_MASK 0x00044dd5
  45. static const int reg_offsets[] = {
  46. [EBX] = HOST_BX,
  47. [ECX] = HOST_CX,
  48. [EDX] = HOST_DX,
  49. [ESI] = HOST_SI,
  50. [EDI] = HOST_DI,
  51. [EBP] = HOST_BP,
  52. [EAX] = HOST_AX,
  53. [DS] = HOST_DS,
  54. [ES] = HOST_ES,
  55. [FS] = HOST_FS,
  56. [GS] = HOST_GS,
  57. [EIP] = HOST_IP,
  58. [CS] = HOST_CS,
  59. [EFL] = HOST_EFLAGS,
  60. [UESP] = HOST_SP,
  61. [SS] = HOST_SS,
  62. };
  63. int putreg(struct task_struct *child, int regno, unsigned long value)
  64. {
  65. regno >>= 2;
  66. switch (regno) {
  67. case EBX:
  68. case ECX:
  69. case EDX:
  70. case ESI:
  71. case EDI:
  72. case EBP:
  73. case EAX:
  74. case EIP:
  75. case UESP:
  76. break;
  77. case FS:
  78. if (value && (value & 3) != 3)
  79. return -EIO;
  80. break;
  81. case GS:
  82. if (value && (value & 3) != 3)
  83. return -EIO;
  84. break;
  85. case DS:
  86. case ES:
  87. if (value && (value & 3) != 3)
  88. return -EIO;
  89. value &= 0xffff;
  90. break;
  91. case SS:
  92. case CS:
  93. if ((value & 3) != 3)
  94. return -EIO;
  95. value &= 0xffff;
  96. break;
  97. case EFL:
  98. value &= FLAG_MASK;
  99. child->thread.regs.regs.gp[HOST_EFLAGS] |= value;
  100. return 0;
  101. case ORIG_EAX:
  102. child->thread.regs.regs.syscall = 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 ORIG_EAX:
  133. return child->thread.regs.regs.syscall;
  134. case FS:
  135. case GS:
  136. case DS:
  137. case ES:
  138. case SS:
  139. case CS:
  140. mask = 0xffff;
  141. break;
  142. case EIP:
  143. case UESP:
  144. case EAX:
  145. case EBX:
  146. case ECX:
  147. case EDX:
  148. case ESI:
  149. case EDI:
  150. case EBP:
  151. case EFL:
  152. break;
  153. default:
  154. panic("Bad register in getreg() : %d\n", regno);
  155. }
  156. return mask & child->thread.regs.regs.gp[reg_offsets[regno]];
  157. }
  158. /* read the word at location addr in the USER area. */
  159. int peek_user(struct task_struct *child, long addr, long data)
  160. {
  161. unsigned long tmp;
  162. if ((addr & 3) || addr < 0)
  163. return -EIO;
  164. tmp = 0; /* Default return condition */
  165. if (addr < MAX_REG_OFFSET) {
  166. tmp = getreg(child, addr);
  167. }
  168. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  169. (addr <= offsetof(struct user, u_debugreg[7]))) {
  170. addr -= offsetof(struct user, u_debugreg[0]);
  171. addr = addr >> 2;
  172. tmp = child->thread.arch.debugregs[addr];
  173. }
  174. return put_user(tmp, (unsigned long __user *) data);
  175. }
  176. static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  177. {
  178. int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
  179. struct user_i387_struct fpregs;
  180. err = save_fp_registers(userspace_pid[cpu], (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_fp_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. }