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