ptrace.c 11 KB

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