ptrace.c 10 KB

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