ptrace.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. int putreg(struct task_struct *child, int regno, unsigned long value)
  46. {
  47. regno >>= 2;
  48. switch (regno) {
  49. case FS:
  50. if (value && (value & 3) != 3)
  51. return -EIO;
  52. PT_REGS_FS(&child->thread.regs) = value;
  53. return 0;
  54. case GS:
  55. if (value && (value & 3) != 3)
  56. return -EIO;
  57. PT_REGS_GS(&child->thread.regs) = value;
  58. return 0;
  59. case DS:
  60. case ES:
  61. if (value && (value & 3) != 3)
  62. return -EIO;
  63. value &= 0xffff;
  64. break;
  65. case SS:
  66. case CS:
  67. if ((value & 3) != 3)
  68. return -EIO;
  69. value &= 0xffff;
  70. break;
  71. case EFL:
  72. value &= FLAG_MASK;
  73. value |= PT_REGS_EFLAGS(&child->thread.regs);
  74. break;
  75. }
  76. PT_REGS_SET(&child->thread.regs, regno, value);
  77. return 0;
  78. }
  79. int poke_user(struct task_struct *child, long addr, long data)
  80. {
  81. if ((addr & 3) || addr < 0)
  82. return -EIO;
  83. if (addr < MAX_REG_OFFSET)
  84. return putreg(child, addr, data);
  85. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  86. (addr <= offsetof(struct user, u_debugreg[7]))) {
  87. addr -= offsetof(struct user, u_debugreg[0]);
  88. addr = addr >> 2;
  89. if ((addr == 4) || (addr == 5))
  90. return -EIO;
  91. child->thread.arch.debugregs[addr] = data;
  92. return 0;
  93. }
  94. return -EIO;
  95. }
  96. unsigned long getreg(struct task_struct *child, int regno)
  97. {
  98. unsigned long retval = ~0UL;
  99. regno >>= 2;
  100. switch (regno) {
  101. case FS:
  102. case GS:
  103. case DS:
  104. case ES:
  105. case SS:
  106. case CS:
  107. retval = 0xffff;
  108. /* fall through */
  109. default:
  110. retval &= PT_REG(&child->thread.regs, regno);
  111. }
  112. return retval;
  113. }
  114. /* read the word at location addr in the USER area. */
  115. int peek_user(struct task_struct *child, long addr, long data)
  116. {
  117. unsigned long tmp;
  118. if ((addr & 3) || addr < 0)
  119. return -EIO;
  120. tmp = 0; /* Default return condition */
  121. if (addr < MAX_REG_OFFSET) {
  122. tmp = getreg(child, addr);
  123. }
  124. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  125. (addr <= offsetof(struct user, u_debugreg[7]))) {
  126. addr -= offsetof(struct user, u_debugreg[0]);
  127. addr = addr >> 2;
  128. tmp = child->thread.arch.debugregs[addr];
  129. }
  130. return put_user(tmp, (unsigned long __user *) data);
  131. }
  132. int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  133. {
  134. int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
  135. struct user_i387_struct fpregs;
  136. err = save_fp_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
  137. if (err)
  138. return err;
  139. n = copy_to_user(buf, &fpregs, sizeof(fpregs));
  140. if(n > 0)
  141. return -EFAULT;
  142. return n;
  143. }
  144. int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  145. {
  146. int n, cpu = ((struct thread_info *) child->stack)->cpu;
  147. struct user_i387_struct fpregs;
  148. n = copy_from_user(&fpregs, buf, sizeof(fpregs));
  149. if (n > 0)
  150. return -EFAULT;
  151. return restore_fp_registers(userspace_pid[cpu],
  152. (unsigned long *) &fpregs);
  153. }
  154. int get_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
  155. {
  156. int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
  157. struct user_fxsr_struct fpregs;
  158. err = save_fpx_registers(userspace_pid[cpu], (unsigned long *) &fpregs);
  159. if (err)
  160. return err;
  161. n = copy_to_user(buf, &fpregs, sizeof(fpregs));
  162. if(n > 0)
  163. return -EFAULT;
  164. return n;
  165. }
  166. int set_fpxregs(struct user_fxsr_struct __user *buf, struct task_struct *child)
  167. {
  168. int n, cpu = ((struct thread_info *) child->stack)->cpu;
  169. struct user_fxsr_struct fpregs;
  170. n = copy_from_user(&fpregs, buf, sizeof(fpregs));
  171. if (n > 0)
  172. return -EFAULT;
  173. return restore_fpx_registers(userspace_pid[cpu],
  174. (unsigned long *) &fpregs);
  175. }
  176. long subarch_ptrace(struct task_struct *child, long request,
  177. unsigned long addr, unsigned long data)
  178. {
  179. return -EIO;
  180. }