process_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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/stackprotector.h>
  11. #include <linux/cpu.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/fs.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/elfcore.h>
  18. #include <linux/smp.h>
  19. #include <linux/stddef.h>
  20. #include <linux/slab.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/user.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/reboot.h>
  26. #include <linux/init.h>
  27. #include <linux/mc146818rtc.h>
  28. #include <linux/module.h>
  29. #include <linux/kallsyms.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/personality.h>
  32. #include <linux/tick.h>
  33. #include <linux/percpu.h>
  34. #include <linux/prctl.h>
  35. #include <linux/ftrace.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/io.h>
  38. #include <linux/kdebug.h>
  39. #include <linux/cpuidle.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/system.h>
  42. #include <asm/ldt.h>
  43. #include <asm/processor.h>
  44. #include <asm/i387.h>
  45. #include <asm/desc.h>
  46. #ifdef CONFIG_MATH_EMULATION
  47. #include <asm/math_emu.h>
  48. #endif
  49. #include <linux/err.h>
  50. #include <asm/tlbflush.h>
  51. #include <asm/cpu.h>
  52. #include <asm/idle.h>
  53. #include <asm/syscalls.h>
  54. #include <asm/debugreg.h>
  55. #include <asm/nmi.h>
  56. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  57. /*
  58. * Return saved PC of a blocked thread.
  59. */
  60. unsigned long thread_saved_pc(struct task_struct *tsk)
  61. {
  62. return ((unsigned long *)tsk->thread.sp)[3];
  63. }
  64. #ifndef CONFIG_SMP
  65. static inline void play_dead(void)
  66. {
  67. BUG();
  68. }
  69. #endif
  70. /*
  71. * The idle thread. There's no useful work to be
  72. * done, so just try to conserve power and have a
  73. * low exit latency (ie sit in a loop waiting for
  74. * somebody to say that they'd like to reschedule)
  75. */
  76. void cpu_idle(void)
  77. {
  78. int cpu = smp_processor_id();
  79. /*
  80. * If we're the non-boot CPU, nothing set the stack canary up
  81. * for us. CPU0 already has it initialized but no harm in
  82. * doing it again. This is a good place for updating it, as
  83. * we wont ever return from this function (so the invalid
  84. * canaries already on the stack wont ever trigger).
  85. */
  86. boot_init_stack_canary();
  87. current_thread_info()->status |= TS_POLLING;
  88. /* endless idle loop with no priority at all */
  89. while (1) {
  90. tick_nohz_stop_sched_tick(1);
  91. while (!need_resched()) {
  92. check_pgt_cache();
  93. rmb();
  94. if (cpu_is_offline(cpu))
  95. play_dead();
  96. local_touch_nmi();
  97. local_irq_disable();
  98. /* Don't trace irqs off for idle */
  99. stop_critical_timings();
  100. if (cpuidle_idle_call())
  101. pm_idle();
  102. start_critical_timings();
  103. }
  104. tick_nohz_restart_sched_tick();
  105. preempt_enable_no_resched();
  106. schedule();
  107. preempt_disable();
  108. }
  109. }
  110. void __show_regs(struct pt_regs *regs, int all)
  111. {
  112. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
  113. unsigned long d0, d1, d2, d3, d6, d7;
  114. unsigned long sp;
  115. unsigned short ss, gs;
  116. if (user_mode_vm(regs)) {
  117. sp = regs->sp;
  118. ss = regs->ss & 0xffff;
  119. gs = get_user_gs(regs);
  120. } else {
  121. sp = kernel_stack_pointer(regs);
  122. savesegment(ss, ss);
  123. savesegment(gs, gs);
  124. }
  125. show_regs_common();
  126. printk(KERN_DEFAULT "EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
  127. (u16)regs->cs, regs->ip, regs->flags,
  128. smp_processor_id());
  129. print_symbol("EIP is at %s\n", regs->ip);
  130. printk(KERN_DEFAULT "EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
  131. regs->ax, regs->bx, regs->cx, regs->dx);
  132. printk(KERN_DEFAULT "ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
  133. regs->si, regs->di, regs->bp, sp);
  134. printk(KERN_DEFAULT " DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
  135. (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
  136. if (!all)
  137. return;
  138. cr0 = read_cr0();
  139. cr2 = read_cr2();
  140. cr3 = read_cr3();
  141. cr4 = read_cr4_safe();
  142. printk(KERN_DEFAULT "CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
  143. cr0, cr2, cr3, cr4);
  144. get_debugreg(d0, 0);
  145. get_debugreg(d1, 1);
  146. get_debugreg(d2, 2);
  147. get_debugreg(d3, 3);
  148. printk(KERN_DEFAULT "DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
  149. d0, d1, d2, d3);
  150. get_debugreg(d6, 6);
  151. get_debugreg(d7, 7);
  152. printk(KERN_DEFAULT "DR6: %08lx DR7: %08lx\n",
  153. d6, d7);
  154. }
  155. void release_thread(struct task_struct *dead_task)
  156. {
  157. BUG_ON(dead_task->mm);
  158. release_vm86_irqs(dead_task);
  159. }
  160. /*
  161. * This gets called before we allocate a new thread and copy
  162. * the current task into it.
  163. */
  164. void prepare_to_copy(struct task_struct *tsk)
  165. {
  166. unlazy_fpu(tsk);
  167. }
  168. int copy_thread(unsigned long clone_flags, unsigned long sp,
  169. unsigned long unused,
  170. struct task_struct *p, struct pt_regs *regs)
  171. {
  172. struct pt_regs *childregs;
  173. struct task_struct *tsk;
  174. int err;
  175. childregs = task_pt_regs(p);
  176. *childregs = *regs;
  177. childregs->ax = 0;
  178. childregs->sp = sp;
  179. p->thread.sp = (unsigned long) childregs;
  180. p->thread.sp0 = (unsigned long) (childregs+1);
  181. p->thread.ip = (unsigned long) ret_from_fork;
  182. task_user_gs(p) = get_user_gs(regs);
  183. p->thread.io_bitmap_ptr = NULL;
  184. tsk = current;
  185. err = -ENOMEM;
  186. memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
  187. if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
  188. p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
  189. IO_BITMAP_BYTES, GFP_KERNEL);
  190. if (!p->thread.io_bitmap_ptr) {
  191. p->thread.io_bitmap_max = 0;
  192. return -ENOMEM;
  193. }
  194. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  195. }
  196. err = 0;
  197. /*
  198. * Set a new TLS for the child thread?
  199. */
  200. if (clone_flags & CLONE_SETTLS)
  201. err = do_set_thread_area(p, -1,
  202. (struct user_desc __user *)childregs->si, 0);
  203. if (err && p->thread.io_bitmap_ptr) {
  204. kfree(p->thread.io_bitmap_ptr);
  205. p->thread.io_bitmap_max = 0;
  206. }
  207. return err;
  208. }
  209. void
  210. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  211. {
  212. set_user_gs(regs, 0);
  213. regs->fs = 0;
  214. regs->ds = __USER_DS;
  215. regs->es = __USER_DS;
  216. regs->ss = __USER_DS;
  217. regs->cs = __USER_CS;
  218. regs->ip = new_ip;
  219. regs->sp = new_sp;
  220. /*
  221. * Free the old FP and other extended state
  222. */
  223. free_thread_xstate(current);
  224. }
  225. EXPORT_SYMBOL_GPL(start_thread);
  226. /*
  227. * switch_to(x,y) should switch tasks from x to y.
  228. *
  229. * We fsave/fwait so that an exception goes off at the right time
  230. * (as a call from the fsave or fwait in effect) rather than to
  231. * the wrong process. Lazy FP saving no longer makes any sense
  232. * with modern CPU's, and this simplifies a lot of things (SMP
  233. * and UP become the same).
  234. *
  235. * NOTE! We used to use the x86 hardware context switching. The
  236. * reason for not using it any more becomes apparent when you
  237. * try to recover gracefully from saved state that is no longer
  238. * valid (stale segment register values in particular). With the
  239. * hardware task-switch, there is no way to fix up bad state in
  240. * a reasonable manner.
  241. *
  242. * The fact that Intel documents the hardware task-switching to
  243. * be slow is a fairly red herring - this code is not noticeably
  244. * faster. However, there _is_ some room for improvement here,
  245. * so the performance issues may eventually be a valid point.
  246. * More important, however, is the fact that this allows us much
  247. * more flexibility.
  248. *
  249. * The return value (in %ax) will be the "prev" task after
  250. * the task-switch, and shows up in ret_from_fork in entry.S,
  251. * for example.
  252. */
  253. __notrace_funcgraph struct task_struct *
  254. __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  255. {
  256. struct thread_struct *prev = &prev_p->thread,
  257. *next = &next_p->thread;
  258. int cpu = smp_processor_id();
  259. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  260. bool preload_fpu;
  261. /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
  262. /*
  263. * If the task has used fpu the last 5 timeslices, just do a full
  264. * restore of the math state immediately to avoid the trap; the
  265. * chances of needing FPU soon are obviously high now
  266. */
  267. preload_fpu = tsk_used_math(next_p) && next_p->fpu_counter > 5;
  268. __unlazy_fpu(prev_p);
  269. /* we're going to use this soon, after a few expensive things */
  270. if (preload_fpu)
  271. prefetch(next->fpu.state);
  272. /*
  273. * Reload esp0.
  274. */
  275. load_sp0(tss, next);
  276. /*
  277. * Save away %gs. No need to save %fs, as it was saved on the
  278. * stack on entry. No need to save %es and %ds, as those are
  279. * always kernel segments while inside the kernel. Doing this
  280. * before setting the new TLS descriptors avoids the situation
  281. * where we temporarily have non-reloadable segments in %fs
  282. * and %gs. This could be an issue if the NMI handler ever
  283. * used %fs or %gs (it does not today), or if the kernel is
  284. * running inside of a hypervisor layer.
  285. */
  286. lazy_save_gs(prev->gs);
  287. /*
  288. * Load the per-thread Thread-Local Storage descriptor.
  289. */
  290. load_TLS(next, cpu);
  291. /*
  292. * Restore IOPL if needed. In normal use, the flags restore
  293. * in the switch assembly will handle this. But if the kernel
  294. * is running virtualized at a non-zero CPL, the popf will
  295. * not restore flags, so it must be done in a separate step.
  296. */
  297. if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
  298. set_iopl_mask(next->iopl);
  299. /*
  300. * Now maybe handle debug registers and/or IO bitmaps
  301. */
  302. if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
  303. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  304. __switch_to_xtra(prev_p, next_p, tss);
  305. /* If we're going to preload the fpu context, make sure clts
  306. is run while we're batching the cpu state updates. */
  307. if (preload_fpu)
  308. clts();
  309. /*
  310. * Leave lazy mode, flushing any hypercalls made here.
  311. * This must be done before restoring TLS segments so
  312. * the GDT and LDT are properly updated, and must be
  313. * done before math_state_restore, so the TS bit is up
  314. * to date.
  315. */
  316. arch_end_context_switch(next_p);
  317. if (preload_fpu)
  318. __math_state_restore();
  319. /*
  320. * Restore %gs if needed (which is common)
  321. */
  322. if (prev->gs | next->gs)
  323. lazy_load_gs(next->gs);
  324. percpu_write(current_task, next_p);
  325. return prev_p;
  326. }
  327. #define top_esp (THREAD_SIZE - sizeof(unsigned long))
  328. #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
  329. unsigned long get_wchan(struct task_struct *p)
  330. {
  331. unsigned long bp, sp, ip;
  332. unsigned long stack_page;
  333. int count = 0;
  334. if (!p || p == current || p->state == TASK_RUNNING)
  335. return 0;
  336. stack_page = (unsigned long)task_stack_page(p);
  337. sp = p->thread.sp;
  338. if (!stack_page || sp < stack_page || sp > top_esp+stack_page)
  339. return 0;
  340. /* include/asm-i386/system.h:switch_to() pushes bp last. */
  341. bp = *(unsigned long *) sp;
  342. do {
  343. if (bp < stack_page || bp > top_ebp+stack_page)
  344. return 0;
  345. ip = *(unsigned long *) (bp+4);
  346. if (!in_sched_functions(ip))
  347. return ip;
  348. bp = *(unsigned long *) bp;
  349. } while (count++ < 16);
  350. return 0;
  351. }