ptrace.c 10 KB

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