process_32.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. *
  4. * Pentium III FXSR, SSE support
  5. * Gareth Hughes <gareth@valinux.com>, May 2000
  6. */
  7. /*
  8. * This file handles the architecture-dependent parts of process handling..
  9. */
  10. #include <linux/cpu.h>
  11. #include <linux/errno.h>
  12. #include <linux/sched.h>
  13. #include <linux/fs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/elfcore.h>
  17. #include <linux/smp.h>
  18. #include <linux/stddef.h>
  19. #include <linux/slab.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/user.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/reboot.h>
  25. #include <linux/mc146818rtc.h>
  26. #include <linux/module.h>
  27. #include <linux/kallsyms.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/personality.h>
  30. #include <linux/percpu.h>
  31. #include <linux/prctl.h>
  32. #include <linux/ftrace.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/io.h>
  35. #include <linux/kdebug.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/ldt.h>
  38. #include <asm/processor.h>
  39. #include <asm/fpu/internal.h>
  40. #include <asm/desc.h>
  41. #ifdef CONFIG_MATH_EMULATION
  42. #include <asm/math_emu.h>
  43. #endif
  44. #include <linux/err.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/cpu.h>
  47. #include <asm/idle.h>
  48. #include <asm/syscalls.h>
  49. #include <asm/debugreg.h>
  50. #include <asm/switch_to.h>
  51. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  52. asmlinkage void ret_from_kernel_thread(void) __asm__("ret_from_kernel_thread");
  53. /*
  54. * Return saved PC of a blocked thread.
  55. */
  56. unsigned long thread_saved_pc(struct task_struct *tsk)
  57. {
  58. return ((unsigned long *)tsk->thread.sp)[3];
  59. }
  60. void __show_regs(struct pt_regs *regs, int all)
  61. {
  62. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
  63. unsigned long d0, d1, d2, d3, d6, d7;
  64. unsigned long sp;
  65. unsigned short ss, gs;
  66. if (user_mode(regs)) {
  67. sp = regs->sp;
  68. ss = regs->ss & 0xffff;
  69. gs = get_user_gs(regs);
  70. } else {
  71. sp = kernel_stack_pointer(regs);
  72. savesegment(ss, ss);
  73. savesegment(gs, gs);
  74. }
  75. printk(KERN_DEFAULT "EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
  76. (u16)regs->cs, regs->ip, regs->flags,
  77. smp_processor_id());
  78. print_symbol("EIP is at %s\n", regs->ip);
  79. printk(KERN_DEFAULT "EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
  80. regs->ax, regs->bx, regs->cx, regs->dx);
  81. printk(KERN_DEFAULT "ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
  82. regs->si, regs->di, regs->bp, sp);
  83. printk(KERN_DEFAULT " DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
  84. (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
  85. if (!all)
  86. return;
  87. cr0 = read_cr0();
  88. cr2 = read_cr2();
  89. cr3 = read_cr3();
  90. cr4 = __read_cr4_safe();
  91. printk(KERN_DEFAULT "CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
  92. cr0, cr2, cr3, cr4);
  93. get_debugreg(d0, 0);
  94. get_debugreg(d1, 1);
  95. get_debugreg(d2, 2);
  96. get_debugreg(d3, 3);
  97. get_debugreg(d6, 6);
  98. get_debugreg(d7, 7);
  99. /* Only print out debug registers if they are in their non-default state. */
  100. if ((d0 == 0) && (d1 == 0) && (d2 == 0) && (d3 == 0) &&
  101. (d6 == DR6_RESERVED) && (d7 == 0x400))
  102. return;
  103. printk(KERN_DEFAULT "DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
  104. d0, d1, d2, d3);
  105. printk(KERN_DEFAULT "DR6: %08lx DR7: %08lx\n",
  106. d6, d7);
  107. }
  108. void release_thread(struct task_struct *dead_task)
  109. {
  110. BUG_ON(dead_task->mm);
  111. release_vm86_irqs(dead_task);
  112. }
  113. int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
  114. unsigned long arg, struct task_struct *p, unsigned long tls)
  115. {
  116. struct pt_regs *childregs = task_pt_regs(p);
  117. struct task_struct *tsk;
  118. int err;
  119. p->thread.sp = (unsigned long) childregs;
  120. p->thread.sp0 = (unsigned long) (childregs+1);
  121. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  122. if (unlikely(p->flags & PF_KTHREAD)) {
  123. /* kernel thread */
  124. memset(childregs, 0, sizeof(struct pt_regs));
  125. p->thread.ip = (unsigned long) ret_from_kernel_thread;
  126. task_user_gs(p) = __KERNEL_STACK_CANARY;
  127. childregs->ds = __USER_DS;
  128. childregs->es = __USER_DS;
  129. childregs->fs = __KERNEL_PERCPU;
  130. childregs->bx = sp; /* function */
  131. childregs->bp = arg;
  132. childregs->orig_ax = -1;
  133. childregs->cs = __KERNEL_CS | get_kernel_rpl();
  134. childregs->flags = X86_EFLAGS_IF | X86_EFLAGS_FIXED;
  135. p->thread.io_bitmap_ptr = NULL;
  136. return 0;
  137. }
  138. *childregs = *current_pt_regs();
  139. childregs->ax = 0;
  140. if (sp)
  141. childregs->sp = sp;
  142. p->thread.ip = (unsigned long) ret_from_fork;
  143. task_user_gs(p) = get_user_gs(current_pt_regs());
  144. p->thread.io_bitmap_ptr = NULL;
  145. tsk = current;
  146. err = -ENOMEM;
  147. if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
  148. p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
  149. IO_BITMAP_BYTES, GFP_KERNEL);
  150. if (!p->thread.io_bitmap_ptr) {
  151. p->thread.io_bitmap_max = 0;
  152. return -ENOMEM;
  153. }
  154. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  155. }
  156. err = 0;
  157. /*
  158. * Set a new TLS for the child thread?
  159. */
  160. if (clone_flags & CLONE_SETTLS)
  161. err = do_set_thread_area(p, -1,
  162. (struct user_desc __user *)tls, 0);
  163. if (err && p->thread.io_bitmap_ptr) {
  164. kfree(p->thread.io_bitmap_ptr);
  165. p->thread.io_bitmap_max = 0;
  166. }
  167. return err;
  168. }
  169. void
  170. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  171. {
  172. set_user_gs(regs, 0);
  173. regs->fs = 0;
  174. regs->ds = __USER_DS;
  175. regs->es = __USER_DS;
  176. regs->ss = __USER_DS;
  177. regs->cs = __USER_CS;
  178. regs->ip = new_ip;
  179. regs->sp = new_sp;
  180. regs->flags = X86_EFLAGS_IF;
  181. force_iret();
  182. }
  183. EXPORT_SYMBOL_GPL(start_thread);
  184. /*
  185. * switch_to(x,y) should switch tasks from x to y.
  186. *
  187. * We fsave/fwait so that an exception goes off at the right time
  188. * (as a call from the fsave or fwait in effect) rather than to
  189. * the wrong process. Lazy FP saving no longer makes any sense
  190. * with modern CPU's, and this simplifies a lot of things (SMP
  191. * and UP become the same).
  192. *
  193. * NOTE! We used to use the x86 hardware context switching. The
  194. * reason for not using it any more becomes apparent when you
  195. * try to recover gracefully from saved state that is no longer
  196. * valid (stale segment register values in particular). With the
  197. * hardware task-switch, there is no way to fix up bad state in
  198. * a reasonable manner.
  199. *
  200. * The fact that Intel documents the hardware task-switching to
  201. * be slow is a fairly red herring - this code is not noticeably
  202. * faster. However, there _is_ some room for improvement here,
  203. * so the performance issues may eventually be a valid point.
  204. * More important, however, is the fact that this allows us much
  205. * more flexibility.
  206. *
  207. * The return value (in %ax) will be the "prev" task after
  208. * the task-switch, and shows up in ret_from_fork in entry.S,
  209. * for example.
  210. */
  211. __visible __notrace_funcgraph struct task_struct *
  212. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  213. {
  214. struct thread_struct *prev = &prev_p->thread,
  215. *next = &next_p->thread;
  216. struct fpu *prev_fpu = &prev->fpu;
  217. struct fpu *next_fpu = &next->fpu;
  218. int cpu = smp_processor_id();
  219. struct tss_struct *tss = &per_cpu(cpu_tss, cpu);
  220. fpu_switch_t fpu_switch;
  221. /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
  222. fpu_switch = switch_fpu_prepare(prev_fpu, next_fpu, cpu);
  223. /*
  224. * Save away %gs. No need to save %fs, as it was saved on the
  225. * stack on entry. No need to save %es and %ds, as those are
  226. * always kernel segments while inside the kernel. Doing this
  227. * before setting the new TLS descriptors avoids the situation
  228. * where we temporarily have non-reloadable segments in %fs
  229. * and %gs. This could be an issue if the NMI handler ever
  230. * used %fs or %gs (it does not today), or if the kernel is
  231. * running inside of a hypervisor layer.
  232. */
  233. lazy_save_gs(prev->gs);
  234. /*
  235. * Load the per-thread Thread-Local Storage descriptor.
  236. */
  237. load_TLS(next, cpu);
  238. /*
  239. * Restore IOPL if needed. In normal use, the flags restore
  240. * in the switch assembly will handle this. But if the kernel
  241. * is running virtualized at a non-zero CPL, the popf will
  242. * not restore flags, so it must be done in a separate step.
  243. */
  244. if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
  245. set_iopl_mask(next->iopl);
  246. /*
  247. * If it were not for PREEMPT_ACTIVE we could guarantee that the
  248. * preempt_count of all tasks was equal here and this would not be
  249. * needed.
  250. */
  251. task_thread_info(prev_p)->saved_preempt_count = this_cpu_read(__preempt_count);
  252. this_cpu_write(__preempt_count, task_thread_info(next_p)->saved_preempt_count);
  253. /*
  254. * Now maybe handle debug registers and/or IO bitmaps
  255. */
  256. if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
  257. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  258. __switch_to_xtra(prev_p, next_p, tss);
  259. /*
  260. * Leave lazy mode, flushing any hypercalls made here.
  261. * This must be done before restoring TLS segments so
  262. * the GDT and LDT are properly updated, and must be
  263. * done before fpu__restore(), so the TS bit is up
  264. * to date.
  265. */
  266. arch_end_context_switch(next_p);
  267. /*
  268. * Reload esp0 and cpu_current_top_of_stack. This changes
  269. * current_thread_info().
  270. */
  271. load_sp0(tss, next);
  272. this_cpu_write(cpu_current_top_of_stack,
  273. (unsigned long)task_stack_page(next_p) +
  274. THREAD_SIZE);
  275. /*
  276. * Restore %gs if needed (which is common)
  277. */
  278. if (prev->gs | next->gs)
  279. lazy_load_gs(next->gs);
  280. switch_fpu_finish(next_fpu, fpu_switch);
  281. this_cpu_write(current_task, next_p);
  282. return prev_p;
  283. }
  284. #define top_esp (THREAD_SIZE - sizeof(unsigned long))
  285. #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
  286. unsigned long get_wchan(struct task_struct *p)
  287. {
  288. unsigned long bp, sp, ip;
  289. unsigned long stack_page;
  290. int count = 0;
  291. if (!p || p == current || p->state == TASK_RUNNING)
  292. return 0;
  293. stack_page = (unsigned long)task_stack_page(p);
  294. sp = p->thread.sp;
  295. if (!stack_page || sp < stack_page || sp > top_esp+stack_page)
  296. return 0;
  297. /* include/asm-i386/system.h:switch_to() pushes bp last. */
  298. bp = *(unsigned long *) sp;
  299. do {
  300. if (bp < stack_page || bp > top_ebp+stack_page)
  301. return 0;
  302. ip = *(unsigned long *) (bp+4);
  303. if (!in_sched_functions(ip))
  304. return ip;
  305. bp = *(unsigned long *) bp;
  306. } while (count++ < 16);
  307. return 0;
  308. }