ptrace.c 4.3 KB

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