ptrace.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/compiler.h>
  6. #include "linux/sched.h"
  7. #include "linux/mm.h"
  8. #include "asm/elf.h"
  9. #include "asm/ptrace.h"
  10. #include "asm/uaccess.h"
  11. #include "asm/unistd.h"
  12. #include "sysdep/ptrace.h"
  13. #include "sysdep/sigcontext.h"
  14. #include "sysdep/sc.h"
  15. void arch_switch_to_skas(struct task_struct *from, struct task_struct *to)
  16. {
  17. int err = arch_switch_tls_skas(from, to);
  18. if (!err)
  19. return;
  20. if (err != -EINVAL)
  21. printk(KERN_WARNING "arch_switch_tls_skas failed, errno %d, not EINVAL\n", -err);
  22. else
  23. printk(KERN_WARNING "arch_switch_tls_skas failed, errno = EINVAL\n");
  24. }
  25. int is_syscall(unsigned long addr)
  26. {
  27. unsigned short instr;
  28. int n;
  29. n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
  30. if(n){
  31. /* access_process_vm() grants access to vsyscall and stub,
  32. * while copy_from_user doesn't. Maybe access_process_vm is
  33. * slow, but that doesn't matter, since it will be called only
  34. * in case of singlestepping, if copy_from_user failed.
  35. */
  36. n = access_process_vm(current, addr, &instr, sizeof(instr), 0);
  37. if(n != sizeof(instr)) {
  38. printk("is_syscall : failed to read instruction from "
  39. "0x%lx\n", addr);
  40. return(1);
  41. }
  42. }
  43. /* int 0x80 or sysenter */
  44. return((instr == 0x80cd) || (instr == 0x340f));
  45. }
  46. /* determines which flags the user has access to. */
  47. /* 1 = access 0 = no access */
  48. #define FLAG_MASK 0x00044dd5
  49. int putreg(struct task_struct *child, int regno, unsigned long value)
  50. {
  51. regno >>= 2;
  52. switch (regno) {
  53. case FS:
  54. if (value && (value & 3) != 3)
  55. return -EIO;
  56. PT_REGS_FS(&child->thread.regs) = value;
  57. return 0;
  58. case GS:
  59. if (value && (value & 3) != 3)
  60. return -EIO;
  61. PT_REGS_GS(&child->thread.regs) = value;
  62. return 0;
  63. case DS:
  64. case ES:
  65. if (value && (value & 3) != 3)
  66. return -EIO;
  67. value &= 0xffff;
  68. break;
  69. case SS:
  70. case CS:
  71. if ((value & 3) != 3)
  72. return -EIO;
  73. value &= 0xffff;
  74. break;
  75. case EFL:
  76. value &= FLAG_MASK;
  77. value |= PT_REGS_EFLAGS(&child->thread.regs);
  78. break;
  79. }
  80. PT_REGS_SET(&child->thread.regs, regno, value);
  81. return 0;
  82. }
  83. int poke_user(struct task_struct *child, long addr, long data)
  84. {
  85. if ((addr & 3) || addr < 0)
  86. return -EIO;
  87. if (addr < MAX_REG_OFFSET)
  88. return putreg(child, addr, data);
  89. else if((addr >= offsetof(struct user, u_debugreg[0])) &&
  90. (addr <= offsetof(struct user, u_debugreg[7]))){
  91. addr -= offsetof(struct user, u_debugreg[0]);
  92. addr = addr >> 2;
  93. if((addr == 4) || (addr == 5)) return -EIO;
  94. child->thread.arch.debugregs[addr] = data;
  95. return 0;
  96. }
  97. return -EIO;
  98. }
  99. unsigned long getreg(struct task_struct *child, int regno)
  100. {
  101. unsigned long retval = ~0UL;
  102. regno >>= 2;
  103. switch (regno) {
  104. case FS:
  105. case GS:
  106. case DS:
  107. case ES:
  108. case SS:
  109. case CS:
  110. retval = 0xffff;
  111. /* fall through */
  112. default:
  113. retval &= PT_REG(&child->thread.regs, regno);
  114. }
  115. return retval;
  116. }
  117. int peek_user(struct task_struct *child, long addr, long data)
  118. {
  119. /* read the word at location addr in the USER area. */
  120. unsigned long tmp;
  121. if ((addr & 3) || addr < 0)
  122. return -EIO;
  123. tmp = 0; /* Default return condition */
  124. if(addr < MAX_REG_OFFSET){
  125. tmp = getreg(child, addr);
  126. }
  127. else if((addr >= offsetof(struct user, u_debugreg[0])) &&
  128. (addr <= offsetof(struct user, u_debugreg[7]))){
  129. addr -= offsetof(struct user, u_debugreg[0]);
  130. addr = addr >> 2;
  131. tmp = child->thread.arch.debugregs[addr];
  132. }
  133. return put_user(tmp, (unsigned long __user *) data);
  134. }
  135. struct i387_fxsave_struct {
  136. unsigned short cwd;
  137. unsigned short swd;
  138. unsigned short twd;
  139. unsigned short fop;
  140. long fip;
  141. long fcs;
  142. long foo;
  143. long fos;
  144. long mxcsr;
  145. long reserved;
  146. long st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  147. long xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  148. long padding[56];
  149. };
  150. /*
  151. * FPU tag word conversions.
  152. */
  153. static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
  154. {
  155. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  156. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  157. tmp = ~twd;
  158. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  159. /* and move the valid bits to the lower byte. */
  160. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  161. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  162. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  163. return tmp;
  164. }
  165. static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
  166. {
  167. struct _fpxreg *st = NULL;
  168. unsigned long twd = (unsigned long) fxsave->twd;
  169. unsigned long tag;
  170. unsigned long ret = 0xffff0000;
  171. int i;
  172. #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
  173. for ( i = 0 ; i < 8 ; i++ ) {
  174. if ( twd & 0x1 ) {
  175. st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
  176. switch ( st->exponent & 0x7fff ) {
  177. case 0x7fff:
  178. tag = 2; /* Special */
  179. break;
  180. case 0x0000:
  181. if ( !st->significand[0] &&
  182. !st->significand[1] &&
  183. !st->significand[2] &&
  184. !st->significand[3] ) {
  185. tag = 1; /* Zero */
  186. } else {
  187. tag = 2; /* Special */
  188. }
  189. break;
  190. default:
  191. if ( st->significand[3] & 0x8000 ) {
  192. tag = 0; /* Valid */
  193. } else {
  194. tag = 2; /* Special */
  195. }
  196. break;
  197. }
  198. } else {
  199. tag = 3; /* Empty */
  200. }
  201. ret |= (tag << (2 * i));
  202. twd = twd >> 1;
  203. }
  204. return ret;
  205. }
  206. static inline int convert_fxsr_to_user(struct _fpstate __user *buf,
  207. struct pt_regs *regs)
  208. {
  209. return 0;
  210. }
  211. static inline int convert_fxsr_from_user(struct pt_regs *regs,
  212. struct _fpstate __user *buf)
  213. {
  214. return 0;
  215. }
  216. int get_fpregs(unsigned long buf, struct task_struct *child)
  217. {
  218. int err;
  219. err = convert_fxsr_to_user((struct _fpstate __user *) buf,
  220. &child->thread.regs);
  221. if(err) return(-EFAULT);
  222. else return(0);
  223. }
  224. int set_fpregs(unsigned long buf, struct task_struct *child)
  225. {
  226. int err;
  227. err = convert_fxsr_from_user(&child->thread.regs,
  228. (struct _fpstate __user *) buf);
  229. if(err) return(-EFAULT);
  230. else return(0);
  231. }
  232. int get_fpxregs(unsigned long buf, struct task_struct *tsk)
  233. {
  234. return 0;
  235. }
  236. int set_fpxregs(unsigned long buf, struct task_struct *tsk)
  237. {
  238. return 0;
  239. }
  240. #ifdef notdef
  241. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  242. {
  243. fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) |
  244. (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff));
  245. fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
  246. fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs));
  247. fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
  248. fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs));
  249. fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs));
  250. fpu->fos = 0;
  251. memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)),
  252. sizeof(fpu->st_space));
  253. return(1);
  254. }
  255. #endif
  256. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu )
  257. {
  258. return 1;
  259. }
  260. /*
  261. * Overrides for Emacs so that we follow Linus's tabbing style.
  262. * Emacs will notice this stuff at the end of the file and automatically
  263. * adjust the settings for this buffer only. This must remain at the end
  264. * of the file.
  265. * ---------------------------------------------------------------------------
  266. * Local variables:
  267. * c-file-style: "linux"
  268. * End:
  269. */