ptrace.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2003 PathScale, Inc.
  3. * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. *
  5. * Licensed under the GPL
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/sched.h>
  9. #include <linux/errno.h>
  10. #define __FRAME_OFFSETS
  11. #include <asm/ptrace.h>
  12. #include <asm/uaccess.h>
  13. /*
  14. * determines which flags the user has access to.
  15. * 1 = access 0 = no access
  16. */
  17. #define FLAG_MASK 0x44dd5UL
  18. int putreg(struct task_struct *child, int regno, unsigned long value)
  19. {
  20. unsigned long tmp;
  21. #ifdef TIF_IA32
  22. /*
  23. * Some code in the 64bit emulation may not be 64bit clean.
  24. * Don't take any chances.
  25. */
  26. if (test_tsk_thread_flag(child, TIF_IA32))
  27. value &= 0xffffffff;
  28. #endif
  29. switch (regno) {
  30. case FS:
  31. case GS:
  32. case DS:
  33. case ES:
  34. case SS:
  35. case CS:
  36. if (value && (value & 3) != 3)
  37. return -EIO;
  38. value &= 0xffff;
  39. break;
  40. case FS_BASE:
  41. case GS_BASE:
  42. if (!((value >> 48) == 0 || (value >> 48) == 0xffff))
  43. return -EIO;
  44. break;
  45. case EFLAGS:
  46. value &= FLAG_MASK;
  47. tmp = PT_REGS_EFLAGS(&child->thread.regs) & ~FLAG_MASK;
  48. value |= tmp;
  49. break;
  50. }
  51. PT_REGS_SET(&child->thread.regs, regno, value);
  52. return 0;
  53. }
  54. int poke_user(struct task_struct *child, long addr, long data)
  55. {
  56. if ((addr & 3) || addr < 0)
  57. return -EIO;
  58. if (addr < MAX_REG_OFFSET)
  59. return putreg(child, addr, data);
  60. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  61. (addr <= offsetof(struct user, u_debugreg[7]))) {
  62. addr -= offsetof(struct user, u_debugreg[0]);
  63. addr = addr >> 2;
  64. if ((addr == 4) || (addr == 5))
  65. return -EIO;
  66. child->thread.arch.debugregs[addr] = data;
  67. return 0;
  68. }
  69. return -EIO;
  70. }
  71. unsigned long getreg(struct task_struct *child, int regno)
  72. {
  73. unsigned long retval = ~0UL;
  74. switch (regno) {
  75. case FS:
  76. case GS:
  77. case DS:
  78. case ES:
  79. case SS:
  80. case CS:
  81. retval = 0xffff;
  82. /* fall through */
  83. default:
  84. retval &= PT_REG(&child->thread.regs, regno);
  85. #ifdef TIF_IA32
  86. if (test_tsk_thread_flag(child, TIF_IA32))
  87. retval &= 0xffffffff;
  88. #endif
  89. }
  90. return retval;
  91. }
  92. int peek_user(struct task_struct *child, long addr, long data)
  93. {
  94. /* read the word at location addr in the USER area. */
  95. unsigned long tmp;
  96. if ((addr & 3) || addr < 0)
  97. return -EIO;
  98. tmp = 0; /* Default return condition */
  99. if (addr < MAX_REG_OFFSET)
  100. tmp = getreg(child, addr);
  101. else if ((addr >= offsetof(struct user, u_debugreg[0])) &&
  102. (addr <= offsetof(struct user, u_debugreg[7]))) {
  103. addr -= offsetof(struct user, u_debugreg[0]);
  104. addr = addr >> 2;
  105. tmp = child->thread.arch.debugregs[addr];
  106. }
  107. return put_user(tmp, (unsigned long *) data);
  108. }
  109. /* XXX Mostly copied from sys-i386 */
  110. int is_syscall(unsigned long addr)
  111. {
  112. unsigned short instr;
  113. int n;
  114. n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
  115. if (n) {
  116. /*
  117. * access_process_vm() grants access to vsyscall and stub,
  118. * while copy_from_user doesn't. Maybe access_process_vm is
  119. * slow, but that doesn't matter, since it will be called only
  120. * in case of singlestepping, if copy_from_user failed.
  121. */
  122. n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
  123. if (n != sizeof(instr)) {
  124. printk("is_syscall : failed to read instruction from "
  125. "0x%lx\n", addr);
  126. return 1;
  127. }
  128. }
  129. /* sysenter */
  130. return instr == 0x050f;
  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. long fpregs[HOST_FP_SIZE];
  136. BUG_ON(sizeof(*buf) != sizeof(fpregs));
  137. err = save_fp_registers(userspace_pid[cpu], fpregs);
  138. if (err)
  139. return err;
  140. n = copy_to_user(buf, fpregs, sizeof(fpregs));
  141. if (n > 0)
  142. return -EFAULT;
  143. return n;
  144. }
  145. int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
  146. {
  147. int n, cpu = ((struct thread_info *) child->stack)->cpu;
  148. long fpregs[HOST_FP_SIZE];
  149. BUG_ON(sizeof(*buf) != sizeof(fpregs));
  150. n = copy_from_user(fpregs, buf, sizeof(fpregs));
  151. if (n > 0)
  152. return -EFAULT;
  153. return restore_fp_registers(userspace_pid[cpu], fpregs);
  154. }
  155. long subarch_ptrace(struct task_struct *child, long request, long addr,
  156. long data)
  157. {
  158. int ret = -EIO;
  159. switch (request) {
  160. case PTRACE_GETFPXREGS: /* Get the child FPU state. */
  161. ret = get_fpregs((struct user_i387_struct __user *) data,
  162. child);
  163. break;
  164. case PTRACE_SETFPXREGS: /* Set the child FPU state. */
  165. ret = set_fpregs((struct user_i387_struct __user *) data,
  166. child);
  167. break;
  168. }
  169. return ret;
  170. }