ptrace.c 11 KB

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