ptrace_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* By Ross Biro 1/23/92 */
  2. /*
  3. * Pentium III FXSR, SSE support
  4. * Gareth Hughes <gareth@valinux.com>, May 2000
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/smp.h>
  10. #include <linux/errno.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/user.h>
  13. #include <linux/security.h>
  14. #include <linux/audit.h>
  15. #include <linux/seccomp.h>
  16. #include <linux/signal.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/system.h>
  20. #include <asm/processor.h>
  21. #include <asm/i387.h>
  22. #include <asm/debugreg.h>
  23. #include <asm/ldt.h>
  24. #include <asm/desc.h>
  25. /*
  26. * does not yet catch signals sent when the child dies.
  27. * in exit.c or in signal.c.
  28. */
  29. /*
  30. * Determines which flags the user has access to [1 = access, 0 = no access].
  31. * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), NT(14), IOPL(12-13), IF(9).
  32. * Also masks reserved bits (31-22, 15, 5, 3, 1).
  33. */
  34. #define FLAG_MASK 0x00050dd5
  35. static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
  36. {
  37. BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
  38. if (regno > FS)
  39. --regno;
  40. return &regs->bx + regno;
  41. }
  42. static int putreg(struct task_struct *child,
  43. unsigned long regno, unsigned long value)
  44. {
  45. struct pt_regs *regs = task_pt_regs(child);
  46. regno >>= 2;
  47. switch (regno) {
  48. case GS:
  49. if (value && (value & 3) != 3)
  50. return -EIO;
  51. child->thread.gs = value;
  52. if (child == current)
  53. /*
  54. * The user-mode %gs is not affected by
  55. * kernel entry, so we must update the CPU.
  56. */
  57. loadsegment(gs, value);
  58. return 0;
  59. case DS:
  60. case ES:
  61. case FS:
  62. if (value && (value & 3) != 3)
  63. return -EIO;
  64. value &= 0xffff;
  65. break;
  66. case SS:
  67. case CS:
  68. if ((value & 3) != 3)
  69. return -EIO;
  70. value &= 0xffff;
  71. break;
  72. case EFL:
  73. value &= FLAG_MASK;
  74. /*
  75. * If the user value contains TF, mark that
  76. * it was not "us" (the debugger) that set it.
  77. * If not, make sure it stays set if we had.
  78. */
  79. if (value & X86_EFLAGS_TF)
  80. clear_tsk_thread_flag(child, TIF_FORCED_TF);
  81. else if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  82. value |= X86_EFLAGS_TF;
  83. value |= regs->flags & ~FLAG_MASK;
  84. break;
  85. }
  86. *pt_regs_access(regs, regno) = value;
  87. return 0;
  88. }
  89. static unsigned long getreg(struct task_struct *child, unsigned long regno)
  90. {
  91. struct pt_regs *regs = task_pt_regs(child);
  92. unsigned long retval = ~0UL;
  93. regno >>= 2;
  94. switch (regno) {
  95. case EFL:
  96. /*
  97. * If the debugger set TF, hide it from the readout.
  98. */
  99. retval = regs->flags;
  100. if (test_tsk_thread_flag(child, TIF_FORCED_TF))
  101. retval &= ~X86_EFLAGS_TF;
  102. break;
  103. case GS:
  104. retval = child->thread.gs;
  105. if (child == current)
  106. savesegment(gs, retval);
  107. break;
  108. case DS:
  109. case ES:
  110. case FS:
  111. case SS:
  112. case CS:
  113. retval = 0xffff;
  114. /* fall through */
  115. default:
  116. retval &= *pt_regs_access(regs, regno);
  117. }
  118. return retval;
  119. }
  120. /*
  121. * This function is trivial and will be inlined by the compiler.
  122. * Having it separates the implementation details of debug
  123. * registers from the interface details of ptrace.
  124. */
  125. static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
  126. {
  127. return child->thread.debugreg[n];
  128. }
  129. static int ptrace_set_debugreg(struct task_struct *child,
  130. int n, unsigned long data)
  131. {
  132. if (unlikely(n == 4 || n == 5))
  133. return -EIO;
  134. if (n < 4 && unlikely(data >= TASK_SIZE - 3))
  135. return -EIO;
  136. if (n == 7) {
  137. /*
  138. * Sanity-check data. Take one half-byte at once with
  139. * check = (val >> (16 + 4*i)) & 0xf. It contains the
  140. * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
  141. * 2 and 3 are LENi. Given a list of invalid values,
  142. * we do mask |= 1 << invalid_value, so that
  143. * (mask >> check) & 1 is a correct test for invalid
  144. * values.
  145. *
  146. * R/Wi contains the type of the breakpoint /
  147. * watchpoint, LENi contains the length of the watched
  148. * data in the watchpoint case.
  149. *
  150. * The invalid values are:
  151. * - LENi == 0x10 (undefined), so mask |= 0x0f00.
  152. * - R/Wi == 0x10 (break on I/O reads or writes), so
  153. * mask |= 0x4444.
  154. * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
  155. * 0x1110.
  156. *
  157. * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
  158. *
  159. * See the Intel Manual "System Programming Guide",
  160. * 15.2.4
  161. *
  162. * Note that LENi == 0x10 is defined on x86_64 in long
  163. * mode (i.e. even for 32-bit userspace software, but
  164. * 64-bit kernel), so the x86_64 mask value is 0x5454.
  165. * See the AMD manual no. 24593 (AMD64 System Programming)
  166. */
  167. int i;
  168. data &= ~DR_CONTROL_RESERVED;
  169. for (i = 0; i < 4; i++)
  170. if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
  171. return -EIO;
  172. if (data)
  173. set_tsk_thread_flag(child, TIF_DEBUG);
  174. else
  175. clear_tsk_thread_flag(child, TIF_DEBUG);
  176. }
  177. child->thread.debugreg[n] = data;
  178. return 0;
  179. }
  180. /*
  181. * Called by kernel/ptrace.c when detaching..
  182. *
  183. * Make sure the single step bit is not set.
  184. */
  185. void ptrace_disable(struct task_struct *child)
  186. {
  187. user_disable_single_step(child);
  188. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  189. }
  190. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  191. {
  192. struct user * dummy = NULL;
  193. int i, ret;
  194. unsigned long __user *datap = (unsigned long __user *)data;
  195. switch (request) {
  196. /* when I and D space are separate, these will need to be fixed. */
  197. case PTRACE_PEEKTEXT: /* read word at location addr. */
  198. case PTRACE_PEEKDATA:
  199. ret = generic_ptrace_peekdata(child, addr, data);
  200. break;
  201. /* read the word at location addr in the USER area. */
  202. case PTRACE_PEEKUSR: {
  203. unsigned long tmp;
  204. ret = -EIO;
  205. if ((addr & 3) || addr < 0 ||
  206. addr > sizeof(struct user) - 3)
  207. break;
  208. tmp = 0; /* Default return condition */
  209. if(addr < FRAME_SIZE*sizeof(long))
  210. tmp = getreg(child, addr);
  211. if(addr >= (long) &dummy->u_debugreg[0] &&
  212. addr <= (long) &dummy->u_debugreg[7]){
  213. addr -= (long) &dummy->u_debugreg[0];
  214. addr = addr >> 2;
  215. tmp = ptrace_get_debugreg(child, addr);
  216. }
  217. ret = put_user(tmp, datap);
  218. break;
  219. }
  220. /* when I and D space are separate, this will have to be fixed. */
  221. case PTRACE_POKETEXT: /* write the word at location addr. */
  222. case PTRACE_POKEDATA:
  223. ret = generic_ptrace_pokedata(child, addr, data);
  224. break;
  225. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  226. ret = -EIO;
  227. if ((addr & 3) || addr < 0 ||
  228. addr > sizeof(struct user) - 3)
  229. break;
  230. if (addr < FRAME_SIZE*sizeof(long)) {
  231. ret = putreg(child, addr, data);
  232. break;
  233. }
  234. /* We need to be very careful here. We implicitly
  235. want to modify a portion of the task_struct, and we
  236. have to be selective about what portions we allow someone
  237. to modify. */
  238. ret = -EIO;
  239. if(addr >= (long) &dummy->u_debugreg[0] &&
  240. addr <= (long) &dummy->u_debugreg[7]){
  241. addr -= (long) &dummy->u_debugreg;
  242. addr = addr >> 2;
  243. ret = ptrace_set_debugreg(child, addr, data);
  244. }
  245. break;
  246. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  247. if (!access_ok(VERIFY_WRITE, datap, FRAME_SIZE*sizeof(long))) {
  248. ret = -EIO;
  249. break;
  250. }
  251. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  252. __put_user(getreg(child, i), datap);
  253. datap++;
  254. }
  255. ret = 0;
  256. break;
  257. }
  258. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  259. unsigned long tmp;
  260. if (!access_ok(VERIFY_READ, datap, FRAME_SIZE*sizeof(long))) {
  261. ret = -EIO;
  262. break;
  263. }
  264. for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
  265. __get_user(tmp, datap);
  266. putreg(child, i, tmp);
  267. datap++;
  268. }
  269. ret = 0;
  270. break;
  271. }
  272. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  273. if (!access_ok(VERIFY_WRITE, datap,
  274. sizeof(struct user_i387_struct))) {
  275. ret = -EIO;
  276. break;
  277. }
  278. ret = 0;
  279. if (!tsk_used_math(child))
  280. init_fpu(child);
  281. get_fpregs((struct user_i387_struct __user *)data, child);
  282. break;
  283. }
  284. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  285. if (!access_ok(VERIFY_READ, datap,
  286. sizeof(struct user_i387_struct))) {
  287. ret = -EIO;
  288. break;
  289. }
  290. set_stopped_child_used_math(child);
  291. set_fpregs(child, (struct user_i387_struct __user *)data);
  292. ret = 0;
  293. break;
  294. }
  295. case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
  296. if (!access_ok(VERIFY_WRITE, datap,
  297. sizeof(struct user_fxsr_struct))) {
  298. ret = -EIO;
  299. break;
  300. }
  301. if (!tsk_used_math(child))
  302. init_fpu(child);
  303. ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
  304. break;
  305. }
  306. case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
  307. if (!access_ok(VERIFY_READ, datap,
  308. sizeof(struct user_fxsr_struct))) {
  309. ret = -EIO;
  310. break;
  311. }
  312. set_stopped_child_used_math(child);
  313. ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
  314. break;
  315. }
  316. case PTRACE_GET_THREAD_AREA:
  317. if (addr < 0)
  318. return -EIO;
  319. ret = do_get_thread_area(child, addr,
  320. (struct user_desc __user *) data);
  321. break;
  322. case PTRACE_SET_THREAD_AREA:
  323. if (addr < 0)
  324. return -EIO;
  325. ret = do_set_thread_area(child, addr,
  326. (struct user_desc __user *) data, 0);
  327. break;
  328. default:
  329. ret = ptrace_request(child, request, addr, data);
  330. break;
  331. }
  332. return ret;
  333. }
  334. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
  335. {
  336. struct siginfo info;
  337. tsk->thread.trap_no = 1;
  338. tsk->thread.error_code = error_code;
  339. memset(&info, 0, sizeof(info));
  340. info.si_signo = SIGTRAP;
  341. info.si_code = TRAP_BRKPT;
  342. /* User-mode ip? */
  343. info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
  344. /* Send us the fake SIGTRAP */
  345. force_sig_info(SIGTRAP, &info, tsk);
  346. }
  347. /* notification of system call entry/exit
  348. * - triggered by current->work.syscall_trace
  349. */
  350. __attribute__((regparm(3)))
  351. int do_syscall_trace(struct pt_regs *regs, int entryexit)
  352. {
  353. int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
  354. /*
  355. * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
  356. * interception
  357. */
  358. int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
  359. int ret = 0;
  360. /* do the secure computing check first */
  361. if (!entryexit)
  362. secure_computing(regs->orig_ax);
  363. if (unlikely(current->audit_context)) {
  364. if (entryexit)
  365. audit_syscall_exit(AUDITSC_RESULT(regs->ax),
  366. regs->ax);
  367. /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
  368. * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
  369. * not used, entry.S will call us only on syscall exit, not
  370. * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
  371. * calling send_sigtrap() on syscall entry.
  372. *
  373. * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
  374. * is_singlestep is false, despite his name, so we will still do
  375. * the correct thing.
  376. */
  377. else if (is_singlestep)
  378. goto out;
  379. }
  380. if (!(current->ptrace & PT_PTRACED))
  381. goto out;
  382. /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
  383. * and then is resumed with SYSEMU_SINGLESTEP, it will come in
  384. * here. We have to check this and return */
  385. if (is_sysemu && entryexit)
  386. return 0;
  387. /* Fake a debug trap */
  388. if (is_singlestep)
  389. send_sigtrap(current, regs, 0);
  390. if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
  391. goto out;
  392. /* the 0x80 provides a way for the tracing parent to distinguish
  393. between a syscall stop and SIGTRAP delivery */
  394. /* Note that the debugger could change the result of test_thread_flag!*/
  395. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
  396. /*
  397. * this isn't the same as continuing with a signal, but it will do
  398. * for normal use. strace only continues with a signal if the
  399. * stopping signal is not SIGTRAP. -brl
  400. */
  401. if (current->exit_code) {
  402. send_sig(current->exit_code, current, 1);
  403. current->exit_code = 0;
  404. }
  405. ret = is_sysemu;
  406. out:
  407. if (unlikely(current->audit_context) && !entryexit)
  408. audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
  409. regs->bx, regs->cx, regs->dx, regs->si);
  410. if (ret == 0)
  411. return 0;
  412. regs->orig_ax = -1; /* force skip of syscall restarting */
  413. if (unlikely(current->audit_context))
  414. audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
  415. return 1;
  416. }