ptrace.c 10 KB

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