ptrace.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/config.h>
  6. #include <linux/compiler.h>
  7. #include "linux/sched.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(void)
  16. {
  17. update_debugregs(current->thread.arch.debugregs_seq);
  18. }
  19. int is_syscall(unsigned long addr)
  20. {
  21. unsigned short instr;
  22. int n;
  23. n = copy_from_user(&instr, (void __user *) addr, sizeof(instr));
  24. if(n){
  25. printk("is_syscall : failed to read instruction from 0x%lx\n",
  26. addr);
  27. return(0);
  28. }
  29. /* int 0x80 or sysenter */
  30. return((instr == 0x80cd) || (instr == 0x340f));
  31. }
  32. /* determines which flags the user has access to. */
  33. /* 1 = access 0 = no access */
  34. #define FLAG_MASK 0x00044dd5
  35. int putreg(struct task_struct *child, int regno, unsigned long value)
  36. {
  37. regno >>= 2;
  38. switch (regno) {
  39. case FS:
  40. if (value && (value & 3) != 3)
  41. return -EIO;
  42. PT_REGS_FS(&child->thread.regs) = value;
  43. return 0;
  44. case GS:
  45. if (value && (value & 3) != 3)
  46. return -EIO;
  47. PT_REGS_GS(&child->thread.regs) = value;
  48. return 0;
  49. case DS:
  50. case ES:
  51. if (value && (value & 3) != 3)
  52. return -EIO;
  53. value &= 0xffff;
  54. break;
  55. case SS:
  56. case CS:
  57. if ((value & 3) != 3)
  58. return -EIO;
  59. value &= 0xffff;
  60. break;
  61. case EFL:
  62. value &= FLAG_MASK;
  63. value |= PT_REGS_EFLAGS(&child->thread.regs);
  64. break;
  65. }
  66. PT_REGS_SET(&child->thread.regs, regno, value);
  67. return 0;
  68. }
  69. int poke_user(struct task_struct *child, long addr, long data)
  70. {
  71. if ((addr & 3) || addr < 0)
  72. return -EIO;
  73. if (addr < MAX_REG_OFFSET)
  74. return putreg(child, addr, data);
  75. else if((addr >= offsetof(struct user, u_debugreg[0])) &&
  76. (addr <= offsetof(struct user, u_debugreg[7]))){
  77. addr -= offsetof(struct user, u_debugreg[0]);
  78. addr = addr >> 2;
  79. if((addr == 4) || (addr == 5)) return -EIO;
  80. child->thread.arch.debugregs[addr] = data;
  81. return 0;
  82. }
  83. return -EIO;
  84. }
  85. unsigned long getreg(struct task_struct *child, int regno)
  86. {
  87. unsigned long retval = ~0UL;
  88. regno >>= 2;
  89. switch (regno) {
  90. case FS:
  91. case GS:
  92. case DS:
  93. case ES:
  94. case SS:
  95. case CS:
  96. retval = 0xffff;
  97. /* fall through */
  98. default:
  99. retval &= PT_REG(&child->thread.regs, regno);
  100. }
  101. return retval;
  102. }
  103. int peek_user(struct task_struct *child, long addr, long data)
  104. {
  105. /* read the word at location addr in the USER area. */
  106. unsigned long tmp;
  107. if ((addr & 3) || addr < 0)
  108. return -EIO;
  109. tmp = 0; /* Default return condition */
  110. if(addr < MAX_REG_OFFSET){
  111. tmp = getreg(child, addr);
  112. }
  113. else if((addr >= offsetof(struct user, u_debugreg[0])) &&
  114. (addr <= offsetof(struct user, u_debugreg[7]))){
  115. addr -= offsetof(struct user, u_debugreg[0]);
  116. addr = addr >> 2;
  117. tmp = child->thread.arch.debugregs[addr];
  118. }
  119. return put_user(tmp, (unsigned long *) data);
  120. }
  121. struct i387_fxsave_struct {
  122. unsigned short cwd;
  123. unsigned short swd;
  124. unsigned short twd;
  125. unsigned short fop;
  126. long fip;
  127. long fcs;
  128. long foo;
  129. long fos;
  130. long mxcsr;
  131. long reserved;
  132. long st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  133. long xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  134. long padding[56];
  135. };
  136. /*
  137. * FPU tag word conversions.
  138. */
  139. static inline unsigned short twd_i387_to_fxsr( unsigned short twd )
  140. {
  141. unsigned int tmp; /* to avoid 16 bit prefixes in the code */
  142. /* Transform each pair of bits into 01 (valid) or 00 (empty) */
  143. tmp = ~twd;
  144. tmp = (tmp | (tmp>>1)) & 0x5555; /* 0V0V0V0V0V0V0V0V */
  145. /* and move the valid bits to the lower byte. */
  146. tmp = (tmp | (tmp >> 1)) & 0x3333; /* 00VV00VV00VV00VV */
  147. tmp = (tmp | (tmp >> 2)) & 0x0f0f; /* 0000VVVV0000VVVV */
  148. tmp = (tmp | (tmp >> 4)) & 0x00ff; /* 00000000VVVVVVVV */
  149. return tmp;
  150. }
  151. static inline unsigned long twd_fxsr_to_i387( struct i387_fxsave_struct *fxsave )
  152. {
  153. struct _fpxreg *st = NULL;
  154. unsigned long twd = (unsigned long) fxsave->twd;
  155. unsigned long tag;
  156. unsigned long ret = 0xffff0000;
  157. int i;
  158. #define FPREG_ADDR(f, n) ((char *)&(f)->st_space + (n) * 16);
  159. for ( i = 0 ; i < 8 ; i++ ) {
  160. if ( twd & 0x1 ) {
  161. st = (struct _fpxreg *) FPREG_ADDR( fxsave, i );
  162. switch ( st->exponent & 0x7fff ) {
  163. case 0x7fff:
  164. tag = 2; /* Special */
  165. break;
  166. case 0x0000:
  167. if ( !st->significand[0] &&
  168. !st->significand[1] &&
  169. !st->significand[2] &&
  170. !st->significand[3] ) {
  171. tag = 1; /* Zero */
  172. } else {
  173. tag = 2; /* Special */
  174. }
  175. break;
  176. default:
  177. if ( st->significand[3] & 0x8000 ) {
  178. tag = 0; /* Valid */
  179. } else {
  180. tag = 2; /* Special */
  181. }
  182. break;
  183. }
  184. } else {
  185. tag = 3; /* Empty */
  186. }
  187. ret |= (tag << (2 * i));
  188. twd = twd >> 1;
  189. }
  190. return ret;
  191. }
  192. /*
  193. * FXSR floating point environment conversions.
  194. */
  195. #ifdef CONFIG_MODE_TT
  196. static inline int convert_fxsr_to_user_tt(struct _fpstate __user *buf,
  197. struct pt_regs *regs)
  198. {
  199. struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
  200. unsigned long env[7];
  201. struct _fpreg __user *to;
  202. struct _fpxreg *from;
  203. int i;
  204. env[0] = (unsigned long)fxsave->cwd | 0xffff0000;
  205. env[1] = (unsigned long)fxsave->swd | 0xffff0000;
  206. env[2] = twd_fxsr_to_i387(fxsave);
  207. env[3] = fxsave->fip;
  208. env[4] = fxsave->fcs | ((unsigned long)fxsave->fop << 16);
  209. env[5] = fxsave->foo;
  210. env[6] = fxsave->fos;
  211. if ( __copy_to_user( buf, env, 7 * sizeof(unsigned long) ) )
  212. return 1;
  213. to = &buf->_st[0];
  214. from = (struct _fpxreg *) &fxsave->st_space[0];
  215. for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
  216. if ( __copy_to_user( to, from, sizeof(*to) ) )
  217. return 1;
  218. }
  219. return 0;
  220. }
  221. #endif
  222. static inline int convert_fxsr_to_user(struct _fpstate __user *buf,
  223. struct pt_regs *regs)
  224. {
  225. return(CHOOSE_MODE(convert_fxsr_to_user_tt(buf, regs), 0));
  226. }
  227. #ifdef CONFIG_MODE_TT
  228. static inline int convert_fxsr_from_user_tt(struct pt_regs *regs,
  229. struct _fpstate __user *buf)
  230. {
  231. struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
  232. unsigned long env[7];
  233. struct _fpxreg *to;
  234. struct _fpreg __user *from;
  235. int i;
  236. if ( __copy_from_user( env, buf, 7 * sizeof(long) ) )
  237. return 1;
  238. fxsave->cwd = (unsigned short)(env[0] & 0xffff);
  239. fxsave->swd = (unsigned short)(env[1] & 0xffff);
  240. fxsave->twd = twd_i387_to_fxsr((unsigned short)(env[2] & 0xffff));
  241. fxsave->fip = env[3];
  242. fxsave->fop = (unsigned short)((env[4] & 0xffff0000) >> 16);
  243. fxsave->fcs = (env[4] & 0xffff);
  244. fxsave->foo = env[5];
  245. fxsave->fos = env[6];
  246. to = (struct _fpxreg *) &fxsave->st_space[0];
  247. from = &buf->_st[0];
  248. for ( i = 0 ; i < 8 ; i++, to++, from++ ) {
  249. if ( __copy_from_user( to, from, sizeof(*from) ) )
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. #endif
  255. static inline int convert_fxsr_from_user(struct pt_regs *regs,
  256. struct _fpstate __user *buf)
  257. {
  258. return(CHOOSE_MODE(convert_fxsr_from_user_tt(regs, buf), 0));
  259. }
  260. int get_fpregs(unsigned long buf, struct task_struct *child)
  261. {
  262. int err;
  263. err = convert_fxsr_to_user((struct _fpstate __user *) buf,
  264. &child->thread.regs);
  265. if(err) return(-EFAULT);
  266. else return(0);
  267. }
  268. int set_fpregs(unsigned long buf, struct task_struct *child)
  269. {
  270. int err;
  271. err = convert_fxsr_from_user(&child->thread.regs,
  272. (struct _fpstate __user *) buf);
  273. if(err) return(-EFAULT);
  274. else return(0);
  275. }
  276. #ifdef CONFIG_MODE_TT
  277. int get_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
  278. {
  279. struct pt_regs *regs = &tsk->thread.regs;
  280. struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
  281. int err;
  282. err = __copy_to_user((void __user *) buf, fxsave,
  283. sizeof(struct user_fxsr_struct));
  284. if(err) return -EFAULT;
  285. else return 0;
  286. }
  287. #endif
  288. int get_fpxregs(unsigned long buf, struct task_struct *tsk)
  289. {
  290. return(CHOOSE_MODE(get_fpxregs_tt(buf, tsk), 0));
  291. }
  292. #ifdef CONFIG_MODE_TT
  293. int set_fpxregs_tt(unsigned long buf, struct task_struct *tsk)
  294. {
  295. struct pt_regs *regs = &tsk->thread.regs;
  296. struct i387_fxsave_struct *fxsave = SC_FXSR_ENV(PT_REGS_SC(regs));
  297. int err;
  298. err = __copy_from_user(fxsave, (void __user *) buf,
  299. sizeof(struct user_fxsr_struct) );
  300. if(err) return -EFAULT;
  301. else return 0;
  302. }
  303. #endif
  304. int set_fpxregs(unsigned long buf, struct task_struct *tsk)
  305. {
  306. return(CHOOSE_MODE(set_fpxregs_tt(buf, tsk), 0));
  307. }
  308. #ifdef notdef
  309. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  310. {
  311. fpu->cwd = (((SC_FP_CW(PT_REGS_SC(regs)) & 0xffff) << 16) |
  312. (SC_FP_SW(PT_REGS_SC(regs)) & 0xffff));
  313. fpu->swd = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
  314. fpu->twd = SC_FP_IPOFF(PT_REGS_SC(regs));
  315. fpu->fip = SC_FP_CSSEL(PT_REGS_SC(regs)) & 0xffff;
  316. fpu->fcs = SC_FP_DATAOFF(PT_REGS_SC(regs));
  317. fpu->foo = SC_FP_DATASEL(PT_REGS_SC(regs));
  318. fpu->fos = 0;
  319. memcpy(fpu->st_space, (void *) SC_FP_ST(PT_REGS_SC(regs)),
  320. sizeof(fpu->st_space));
  321. return(1);
  322. }
  323. #endif
  324. #ifdef CONFIG_MODE_TT
  325. static inline void copy_fpu_fxsave_tt(struct pt_regs *regs,
  326. struct user_i387_struct *buf)
  327. {
  328. struct i387_fxsave_struct *fpu = SC_FXSR_ENV(PT_REGS_SC(regs));
  329. unsigned short *to;
  330. unsigned short *from;
  331. int i;
  332. memcpy( buf, fpu, 7 * sizeof(long) );
  333. to = (unsigned short *) &buf->st_space[0];
  334. from = (unsigned short *) &fpu->st_space[0];
  335. for ( i = 0 ; i < 8 ; i++, to += 5, from += 8 ) {
  336. memcpy( to, from, 5 * sizeof(unsigned short) );
  337. }
  338. }
  339. #endif
  340. static inline void copy_fpu_fxsave(struct pt_regs *regs,
  341. struct user_i387_struct *buf)
  342. {
  343. (void) CHOOSE_MODE(copy_fpu_fxsave_tt(regs, buf), 0);
  344. }
  345. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu )
  346. {
  347. copy_fpu_fxsave(regs, (struct user_i387_struct *) fpu);
  348. return(1);
  349. }
  350. /*
  351. * Overrides for Emacs so that we follow Linus's tabbing style.
  352. * Emacs will notice this stuff at the end of the file and automatically
  353. * adjust the settings for this buffer only. This must remain at the end
  354. * of the file.
  355. * ---------------------------------------------------------------------------
  356. * Local variables:
  357. * c-file-style: "linux"
  358. * End:
  359. */