process_32.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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 <stdarg.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/utsname.h>
  25. #include <linux/delay.h>
  26. #include <linux/reboot.h>
  27. #include <linux/init.h>
  28. #include <linux/mc146818rtc.h>
  29. #include <linux/module.h>
  30. #include <linux/kallsyms.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/random.h>
  33. #include <linux/personality.h>
  34. #include <linux/tick.h>
  35. #include <linux/percpu.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/system.h>
  39. #include <asm/io.h>
  40. #include <asm/ldt.h>
  41. #include <asm/processor.h>
  42. #include <asm/i387.h>
  43. #include <asm/desc.h>
  44. #include <asm/vm86.h>
  45. #ifdef CONFIG_MATH_EMULATION
  46. #include <asm/math_emu.h>
  47. #endif
  48. #include <linux/err.h>
  49. #include <asm/tlbflush.h>
  50. #include <asm/cpu.h>
  51. #include <asm/kdebug.h>
  52. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  53. static int hlt_counter;
  54. unsigned long boot_option_idle_override = 0;
  55. EXPORT_SYMBOL(boot_option_idle_override);
  56. DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
  57. EXPORT_PER_CPU_SYMBOL(current_task);
  58. DEFINE_PER_CPU(int, cpu_number);
  59. EXPORT_PER_CPU_SYMBOL(cpu_number);
  60. /*
  61. * Return saved PC of a blocked thread.
  62. */
  63. unsigned long thread_saved_pc(struct task_struct *tsk)
  64. {
  65. return ((unsigned long *)tsk->thread.sp)[3];
  66. }
  67. /*
  68. * Powermanagement idle function, if any..
  69. */
  70. void (*pm_idle)(void);
  71. EXPORT_SYMBOL(pm_idle);
  72. void disable_hlt(void)
  73. {
  74. hlt_counter++;
  75. }
  76. EXPORT_SYMBOL(disable_hlt);
  77. void enable_hlt(void)
  78. {
  79. hlt_counter--;
  80. }
  81. EXPORT_SYMBOL(enable_hlt);
  82. /*
  83. * We use this if we don't have any better
  84. * idle routine..
  85. */
  86. void default_idle(void)
  87. {
  88. if (!hlt_counter && boot_cpu_data.hlt_works_ok) {
  89. current_thread_info()->status &= ~TS_POLLING;
  90. /*
  91. * TS_POLLING-cleared state must be visible before we
  92. * test NEED_RESCHED:
  93. */
  94. smp_mb();
  95. local_irq_disable();
  96. if (!need_resched()) {
  97. ktime_t t0, t1;
  98. u64 t0n, t1n;
  99. t0 = ktime_get();
  100. t0n = ktime_to_ns(t0);
  101. safe_halt(); /* enables interrupts racelessly */
  102. local_irq_disable();
  103. t1 = ktime_get();
  104. t1n = ktime_to_ns(t1);
  105. sched_clock_idle_wakeup_event(t1n - t0n);
  106. }
  107. local_irq_enable();
  108. current_thread_info()->status |= TS_POLLING;
  109. } else {
  110. local_irq_enable();
  111. /* loop is done by the caller */
  112. cpu_relax();
  113. }
  114. }
  115. #ifdef CONFIG_APM_MODULE
  116. EXPORT_SYMBOL(default_idle);
  117. #endif
  118. /*
  119. * On SMP it's slightly faster (but much more power-consuming!)
  120. * to poll the ->work.need_resched flag instead of waiting for the
  121. * cross-CPU IPI to arrive. Use this option with caution.
  122. */
  123. static void poll_idle(void)
  124. {
  125. local_irq_enable();
  126. cpu_relax();
  127. }
  128. #ifdef CONFIG_HOTPLUG_CPU
  129. #include <asm/nmi.h>
  130. /* We don't actually take CPU down, just spin without interrupts. */
  131. static inline void play_dead(void)
  132. {
  133. /* This must be done before dead CPU ack */
  134. cpu_exit_clear();
  135. wbinvd();
  136. mb();
  137. /* Ack it */
  138. __get_cpu_var(cpu_state) = CPU_DEAD;
  139. /*
  140. * With physical CPU hotplug, we should halt the cpu
  141. */
  142. local_irq_disable();
  143. while (1)
  144. halt();
  145. }
  146. #else
  147. static inline void play_dead(void)
  148. {
  149. BUG();
  150. }
  151. #endif /* CONFIG_HOTPLUG_CPU */
  152. /*
  153. * The idle thread. There's no useful work to be
  154. * done, so just try to conserve power and have a
  155. * low exit latency (ie sit in a loop waiting for
  156. * somebody to say that they'd like to reschedule)
  157. */
  158. void cpu_idle(void)
  159. {
  160. int cpu = smp_processor_id();
  161. current_thread_info()->status |= TS_POLLING;
  162. /* endless idle loop with no priority at all */
  163. while (1) {
  164. tick_nohz_stop_sched_tick();
  165. while (!need_resched()) {
  166. void (*idle)(void);
  167. check_pgt_cache();
  168. rmb();
  169. idle = pm_idle;
  170. if (rcu_pending(cpu))
  171. rcu_check_callbacks(cpu, 0);
  172. if (!idle)
  173. idle = default_idle;
  174. if (cpu_is_offline(cpu))
  175. play_dead();
  176. __get_cpu_var(irq_stat).idle_timestamp = jiffies;
  177. idle();
  178. }
  179. tick_nohz_restart_sched_tick();
  180. preempt_enable_no_resched();
  181. schedule();
  182. preempt_disable();
  183. }
  184. }
  185. static void do_nothing(void *unused)
  186. {
  187. }
  188. /*
  189. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  190. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  191. * handler on SMP systems.
  192. *
  193. * Caller must have changed pm_idle to the new value before the call. Old
  194. * pm_idle value will not be used by any CPU after the return of this function.
  195. */
  196. void cpu_idle_wait(void)
  197. {
  198. smp_mb();
  199. /* kick all the CPUs so that they exit out of pm_idle */
  200. smp_call_function(do_nothing, NULL, 0, 1);
  201. }
  202. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  203. /*
  204. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  205. * which can obviate IPI to trigger checking of need_resched.
  206. * We execute MONITOR against need_resched and enter optimized wait state
  207. * through MWAIT. Whenever someone changes need_resched, we would be woken
  208. * up from MWAIT (without an IPI).
  209. *
  210. * New with Core Duo processors, MWAIT can take some hints based on CPU
  211. * capability.
  212. */
  213. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  214. {
  215. if (!need_resched()) {
  216. __monitor((void *)&current_thread_info()->flags, 0, 0);
  217. smp_mb();
  218. if (!need_resched())
  219. __sti_mwait(ax, cx);
  220. else
  221. local_irq_enable();
  222. } else
  223. local_irq_enable();
  224. }
  225. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  226. static void mwait_idle(void)
  227. {
  228. local_irq_enable();
  229. mwait_idle_with_hints(0, 0);
  230. }
  231. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  232. {
  233. if (force_mwait)
  234. return 1;
  235. /* Any C1 states supported? */
  236. return c->cpuid_level >= 5 && ((cpuid_edx(5) >> 4) & 0xf) > 0;
  237. }
  238. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  239. {
  240. static int selected;
  241. if (selected)
  242. return;
  243. #ifdef CONFIG_X86_SMP
  244. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  245. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  246. " performance may degrade.\n");
  247. }
  248. #endif
  249. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  250. /*
  251. * Skip, if setup has overridden idle.
  252. * One CPU supports mwait => All CPUs supports mwait
  253. */
  254. if (!pm_idle) {
  255. printk(KERN_INFO "using mwait in idle threads.\n");
  256. pm_idle = mwait_idle;
  257. }
  258. }
  259. selected = 1;
  260. }
  261. static int __init idle_setup(char *str)
  262. {
  263. if (!strcmp(str, "poll")) {
  264. printk("using polling idle threads.\n");
  265. pm_idle = poll_idle;
  266. } else if (!strcmp(str, "mwait"))
  267. force_mwait = 1;
  268. else
  269. return -1;
  270. boot_option_idle_override = 1;
  271. return 0;
  272. }
  273. early_param("idle", idle_setup);
  274. void __show_registers(struct pt_regs *regs, int all)
  275. {
  276. unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
  277. unsigned long d0, d1, d2, d3, d6, d7;
  278. unsigned long sp;
  279. unsigned short ss, gs;
  280. if (user_mode_vm(regs)) {
  281. sp = regs->sp;
  282. ss = regs->ss & 0xffff;
  283. savesegment(gs, gs);
  284. } else {
  285. sp = (unsigned long) (&regs->sp);
  286. savesegment(ss, ss);
  287. savesegment(gs, gs);
  288. }
  289. printk("\n");
  290. printk("Pid: %d, comm: %s %s (%s %.*s)\n",
  291. task_pid_nr(current), current->comm,
  292. print_tainted(), init_utsname()->release,
  293. (int)strcspn(init_utsname()->version, " "),
  294. init_utsname()->version);
  295. printk("EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
  296. (u16)regs->cs, regs->ip, regs->flags,
  297. smp_processor_id());
  298. print_symbol("EIP is at %s\n", regs->ip);
  299. printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
  300. regs->ax, regs->bx, regs->cx, regs->dx);
  301. printk("ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
  302. regs->si, regs->di, regs->bp, sp);
  303. printk(" DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
  304. (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
  305. if (!all)
  306. return;
  307. cr0 = read_cr0();
  308. cr2 = read_cr2();
  309. cr3 = read_cr3();
  310. cr4 = read_cr4_safe();
  311. printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
  312. cr0, cr2, cr3, cr4);
  313. get_debugreg(d0, 0);
  314. get_debugreg(d1, 1);
  315. get_debugreg(d2, 2);
  316. get_debugreg(d3, 3);
  317. printk("DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
  318. d0, d1, d2, d3);
  319. get_debugreg(d6, 6);
  320. get_debugreg(d7, 7);
  321. printk("DR6: %08lx DR7: %08lx\n",
  322. d6, d7);
  323. }
  324. void show_regs(struct pt_regs *regs)
  325. {
  326. __show_registers(regs, 1);
  327. show_trace(NULL, regs, &regs->sp, regs->bp);
  328. }
  329. /*
  330. * This gets run with %bx containing the
  331. * function to call, and %dx containing
  332. * the "args".
  333. */
  334. extern void kernel_thread_helper(void);
  335. /*
  336. * Create a kernel thread
  337. */
  338. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  339. {
  340. struct pt_regs regs;
  341. memset(&regs, 0, sizeof(regs));
  342. regs.bx = (unsigned long) fn;
  343. regs.dx = (unsigned long) arg;
  344. regs.ds = __USER_DS;
  345. regs.es = __USER_DS;
  346. regs.fs = __KERNEL_PERCPU;
  347. regs.orig_ax = -1;
  348. regs.ip = (unsigned long) kernel_thread_helper;
  349. regs.cs = __KERNEL_CS | get_kernel_rpl();
  350. regs.flags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
  351. /* Ok, create the new process.. */
  352. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  353. }
  354. EXPORT_SYMBOL(kernel_thread);
  355. /*
  356. * Free current thread data structures etc..
  357. */
  358. void exit_thread(void)
  359. {
  360. /* The process may have allocated an io port bitmap... nuke it. */
  361. if (unlikely(test_thread_flag(TIF_IO_BITMAP))) {
  362. struct task_struct *tsk = current;
  363. struct thread_struct *t = &tsk->thread;
  364. int cpu = get_cpu();
  365. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  366. kfree(t->io_bitmap_ptr);
  367. t->io_bitmap_ptr = NULL;
  368. clear_thread_flag(TIF_IO_BITMAP);
  369. /*
  370. * Careful, clear this in the TSS too:
  371. */
  372. memset(tss->io_bitmap, 0xff, tss->io_bitmap_max);
  373. t->io_bitmap_max = 0;
  374. tss->io_bitmap_owner = NULL;
  375. tss->io_bitmap_max = 0;
  376. tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
  377. put_cpu();
  378. }
  379. }
  380. void flush_thread(void)
  381. {
  382. struct task_struct *tsk = current;
  383. tsk->thread.debugreg0 = 0;
  384. tsk->thread.debugreg1 = 0;
  385. tsk->thread.debugreg2 = 0;
  386. tsk->thread.debugreg3 = 0;
  387. tsk->thread.debugreg6 = 0;
  388. tsk->thread.debugreg7 = 0;
  389. memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
  390. clear_tsk_thread_flag(tsk, TIF_DEBUG);
  391. /*
  392. * Forget coprocessor state..
  393. */
  394. clear_fpu(tsk);
  395. clear_used_math();
  396. }
  397. void release_thread(struct task_struct *dead_task)
  398. {
  399. BUG_ON(dead_task->mm);
  400. release_vm86_irqs(dead_task);
  401. }
  402. /*
  403. * This gets called before we allocate a new thread and copy
  404. * the current task into it.
  405. */
  406. void prepare_to_copy(struct task_struct *tsk)
  407. {
  408. unlazy_fpu(tsk);
  409. }
  410. int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
  411. unsigned long unused,
  412. struct task_struct * p, struct pt_regs * regs)
  413. {
  414. struct pt_regs * childregs;
  415. struct task_struct *tsk;
  416. int err;
  417. childregs = task_pt_regs(p);
  418. *childregs = *regs;
  419. childregs->ax = 0;
  420. childregs->sp = sp;
  421. p->thread.sp = (unsigned long) childregs;
  422. p->thread.sp0 = (unsigned long) (childregs+1);
  423. p->thread.ip = (unsigned long) ret_from_fork;
  424. savesegment(gs, p->thread.gs);
  425. tsk = current;
  426. if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
  427. p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
  428. IO_BITMAP_BYTES, GFP_KERNEL);
  429. if (!p->thread.io_bitmap_ptr) {
  430. p->thread.io_bitmap_max = 0;
  431. return -ENOMEM;
  432. }
  433. set_tsk_thread_flag(p, TIF_IO_BITMAP);
  434. }
  435. err = 0;
  436. /*
  437. * Set a new TLS for the child thread?
  438. */
  439. if (clone_flags & CLONE_SETTLS)
  440. err = do_set_thread_area(p, -1,
  441. (struct user_desc __user *)childregs->si, 0);
  442. if (err && p->thread.io_bitmap_ptr) {
  443. kfree(p->thread.io_bitmap_ptr);
  444. p->thread.io_bitmap_max = 0;
  445. }
  446. return err;
  447. }
  448. void
  449. start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
  450. {
  451. __asm__("movl %0, %%gs" :: "r"(0));
  452. regs->fs = 0;
  453. set_fs(USER_DS);
  454. regs->ds = __USER_DS;
  455. regs->es = __USER_DS;
  456. regs->ss = __USER_DS;
  457. regs->cs = __USER_CS;
  458. regs->ip = new_ip;
  459. regs->sp = new_sp;
  460. }
  461. EXPORT_SYMBOL_GPL(start_thread);
  462. #ifdef CONFIG_SECCOMP
  463. static void hard_disable_TSC(void)
  464. {
  465. write_cr4(read_cr4() | X86_CR4_TSD);
  466. }
  467. void disable_TSC(void)
  468. {
  469. preempt_disable();
  470. if (!test_and_set_thread_flag(TIF_NOTSC))
  471. /*
  472. * Must flip the CPU state synchronously with
  473. * TIF_NOTSC in the current running context.
  474. */
  475. hard_disable_TSC();
  476. preempt_enable();
  477. }
  478. static void hard_enable_TSC(void)
  479. {
  480. write_cr4(read_cr4() & ~X86_CR4_TSD);
  481. }
  482. #endif /* CONFIG_SECCOMP */
  483. static noinline void
  484. __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
  485. struct tss_struct *tss)
  486. {
  487. struct thread_struct *prev, *next;
  488. unsigned long debugctl;
  489. prev = &prev_p->thread;
  490. next = &next_p->thread;
  491. debugctl = prev->debugctlmsr;
  492. if (next->ds_area_msr != prev->ds_area_msr) {
  493. /* we clear debugctl to make sure DS
  494. * is not in use when we change it */
  495. debugctl = 0;
  496. update_debugctlmsr(0);
  497. wrmsr(MSR_IA32_DS_AREA, next->ds_area_msr, 0);
  498. }
  499. if (next->debugctlmsr != debugctl)
  500. update_debugctlmsr(next->debugctlmsr);
  501. if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
  502. set_debugreg(next->debugreg0, 0);
  503. set_debugreg(next->debugreg1, 1);
  504. set_debugreg(next->debugreg2, 2);
  505. set_debugreg(next->debugreg3, 3);
  506. /* no 4 and 5 */
  507. set_debugreg(next->debugreg6, 6);
  508. set_debugreg(next->debugreg7, 7);
  509. }
  510. #ifdef CONFIG_SECCOMP
  511. if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
  512. test_tsk_thread_flag(next_p, TIF_NOTSC)) {
  513. /* prev and next are different */
  514. if (test_tsk_thread_flag(next_p, TIF_NOTSC))
  515. hard_disable_TSC();
  516. else
  517. hard_enable_TSC();
  518. }
  519. #endif
  520. #ifdef X86_BTS
  521. if (test_tsk_thread_flag(prev_p, TIF_BTS_TRACE_TS))
  522. ptrace_bts_take_timestamp(prev_p, BTS_TASK_DEPARTS);
  523. if (test_tsk_thread_flag(next_p, TIF_BTS_TRACE_TS))
  524. ptrace_bts_take_timestamp(next_p, BTS_TASK_ARRIVES);
  525. #endif
  526. if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
  527. /*
  528. * Disable the bitmap via an invalid offset. We still cache
  529. * the previous bitmap owner and the IO bitmap contents:
  530. */
  531. tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
  532. return;
  533. }
  534. if (likely(next == tss->io_bitmap_owner)) {
  535. /*
  536. * Previous owner of the bitmap (hence the bitmap content)
  537. * matches the next task, we dont have to do anything but
  538. * to set a valid offset in the TSS:
  539. */
  540. tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
  541. return;
  542. }
  543. /*
  544. * Lazy TSS's I/O bitmap copy. We set an invalid offset here
  545. * and we let the task to get a GPF in case an I/O instruction
  546. * is performed. The handler of the GPF will verify that the
  547. * faulting task has a valid I/O bitmap and, it true, does the
  548. * real copy and restart the instruction. This will save us
  549. * redundant copies when the currently switched task does not
  550. * perform any I/O during its timeslice.
  551. */
  552. tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
  553. }
  554. /*
  555. * switch_to(x,yn) should switch tasks from x to y.
  556. *
  557. * We fsave/fwait so that an exception goes off at the right time
  558. * (as a call from the fsave or fwait in effect) rather than to
  559. * the wrong process. Lazy FP saving no longer makes any sense
  560. * with modern CPU's, and this simplifies a lot of things (SMP
  561. * and UP become the same).
  562. *
  563. * NOTE! We used to use the x86 hardware context switching. The
  564. * reason for not using it any more becomes apparent when you
  565. * try to recover gracefully from saved state that is no longer
  566. * valid (stale segment register values in particular). With the
  567. * hardware task-switch, there is no way to fix up bad state in
  568. * a reasonable manner.
  569. *
  570. * The fact that Intel documents the hardware task-switching to
  571. * be slow is a fairly red herring - this code is not noticeably
  572. * faster. However, there _is_ some room for improvement here,
  573. * so the performance issues may eventually be a valid point.
  574. * More important, however, is the fact that this allows us much
  575. * more flexibility.
  576. *
  577. * The return value (in %ax) will be the "prev" task after
  578. * the task-switch, and shows up in ret_from_fork in entry.S,
  579. * for example.
  580. */
  581. struct task_struct * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
  582. {
  583. struct thread_struct *prev = &prev_p->thread,
  584. *next = &next_p->thread;
  585. int cpu = smp_processor_id();
  586. struct tss_struct *tss = &per_cpu(init_tss, cpu);
  587. /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
  588. __unlazy_fpu(prev_p);
  589. /* we're going to use this soon, after a few expensive things */
  590. if (next_p->fpu_counter > 5)
  591. prefetch(&next->i387.fxsave);
  592. /*
  593. * Reload esp0.
  594. */
  595. load_sp0(tss, next);
  596. /*
  597. * Save away %gs. No need to save %fs, as it was saved on the
  598. * stack on entry. No need to save %es and %ds, as those are
  599. * always kernel segments while inside the kernel. Doing this
  600. * before setting the new TLS descriptors avoids the situation
  601. * where we temporarily have non-reloadable segments in %fs
  602. * and %gs. This could be an issue if the NMI handler ever
  603. * used %fs or %gs (it does not today), or if the kernel is
  604. * running inside of a hypervisor layer.
  605. */
  606. savesegment(gs, prev->gs);
  607. /*
  608. * Load the per-thread Thread-Local Storage descriptor.
  609. */
  610. load_TLS(next, cpu);
  611. /*
  612. * Restore IOPL if needed. In normal use, the flags restore
  613. * in the switch assembly will handle this. But if the kernel
  614. * is running virtualized at a non-zero CPL, the popf will
  615. * not restore flags, so it must be done in a separate step.
  616. */
  617. if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
  618. set_iopl_mask(next->iopl);
  619. /*
  620. * Now maybe handle debug registers and/or IO bitmaps
  621. */
  622. if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
  623. task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
  624. __switch_to_xtra(prev_p, next_p, tss);
  625. /*
  626. * Leave lazy mode, flushing any hypercalls made here.
  627. * This must be done before restoring TLS segments so
  628. * the GDT and LDT are properly updated, and must be
  629. * done before math_state_restore, so the TS bit is up
  630. * to date.
  631. */
  632. arch_leave_lazy_cpu_mode();
  633. /* If the task has used fpu the last 5 timeslices, just do a full
  634. * restore of the math state immediately to avoid the trap; the
  635. * chances of needing FPU soon are obviously high now
  636. */
  637. if (next_p->fpu_counter > 5)
  638. math_state_restore();
  639. /*
  640. * Restore %gs if needed (which is common)
  641. */
  642. if (prev->gs | next->gs)
  643. loadsegment(gs, next->gs);
  644. x86_write_percpu(current_task, next_p);
  645. return prev_p;
  646. }
  647. asmlinkage int sys_fork(struct pt_regs regs)
  648. {
  649. return do_fork(SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
  650. }
  651. asmlinkage int sys_clone(struct pt_regs regs)
  652. {
  653. unsigned long clone_flags;
  654. unsigned long newsp;
  655. int __user *parent_tidptr, *child_tidptr;
  656. clone_flags = regs.bx;
  657. newsp = regs.cx;
  658. parent_tidptr = (int __user *)regs.dx;
  659. child_tidptr = (int __user *)regs.di;
  660. if (!newsp)
  661. newsp = regs.sp;
  662. return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
  663. }
  664. /*
  665. * This is trivial, and on the face of it looks like it
  666. * could equally well be done in user mode.
  667. *
  668. * Not so, for quite unobvious reasons - register pressure.
  669. * In user mode vfork() cannot have a stack frame, and if
  670. * done by calling the "clone()" system call directly, you
  671. * do not have enough call-clobbered registers to hold all
  672. * the information you need.
  673. */
  674. asmlinkage int sys_vfork(struct pt_regs regs)
  675. {
  676. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
  677. }
  678. /*
  679. * sys_execve() executes a new program.
  680. */
  681. asmlinkage int sys_execve(struct pt_regs regs)
  682. {
  683. int error;
  684. char * filename;
  685. filename = getname((char __user *) regs.bx);
  686. error = PTR_ERR(filename);
  687. if (IS_ERR(filename))
  688. goto out;
  689. error = do_execve(filename,
  690. (char __user * __user *) regs.cx,
  691. (char __user * __user *) regs.dx,
  692. &regs);
  693. if (error == 0) {
  694. /* Make sure we don't return using sysenter.. */
  695. set_thread_flag(TIF_IRET);
  696. }
  697. putname(filename);
  698. out:
  699. return error;
  700. }
  701. #define top_esp (THREAD_SIZE - sizeof(unsigned long))
  702. #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
  703. unsigned long get_wchan(struct task_struct *p)
  704. {
  705. unsigned long bp, sp, ip;
  706. unsigned long stack_page;
  707. int count = 0;
  708. if (!p || p == current || p->state == TASK_RUNNING)
  709. return 0;
  710. stack_page = (unsigned long)task_stack_page(p);
  711. sp = p->thread.sp;
  712. if (!stack_page || sp < stack_page || sp > top_esp+stack_page)
  713. return 0;
  714. /* include/asm-i386/system.h:switch_to() pushes bp last. */
  715. bp = *(unsigned long *) sp;
  716. do {
  717. if (bp < stack_page || bp > top_ebp+stack_page)
  718. return 0;
  719. ip = *(unsigned long *) (bp+4);
  720. if (!in_sched_functions(ip))
  721. return ip;
  722. bp = *(unsigned long *) bp;
  723. } while (count++ < 16);
  724. return 0;
  725. }
  726. unsigned long arch_align_stack(unsigned long sp)
  727. {
  728. if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
  729. sp -= get_random_int() % 8192;
  730. return sp & ~0xf;
  731. }
  732. unsigned long arch_randomize_brk(struct mm_struct *mm)
  733. {
  734. unsigned long range_end = mm->brk + 0x02000000;
  735. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  736. }