process_32.c 21 KB

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