process_32.c 21 KB

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