Merge commit 'v2.6.27-rc7' into x86/debug
[linux-2.6/kvm.git] / arch / x86 / kernel / process_32.c
blob62a4790e425dd1578e3f07244fb1422b0b4db5c2
1 /*
2 * Copyright (C) 1995 Linus Torvalds
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
6 */
8 /*
9 * This file handles the architecture-dependent parts of process handling..
12 #include <stdarg.h>
14 #include <linux/cpu.h>
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>
22 #include <linux/stddef.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/user.h>
26 #include <linux/interrupt.h>
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>
36 #include <linux/personality.h>
37 #include <linux/tick.h>
38 #include <linux/percpu.h>
39 #include <linux/prctl.h>
40 #include <linux/dmi.h>
42 #include <asm/uaccess.h>
43 #include <asm/pgtable.h>
44 #include <asm/system.h>
45 #include <asm/io.h>
46 #include <asm/ldt.h>
47 #include <asm/processor.h>
48 #include <asm/i387.h>
49 #include <asm/desc.h>
50 #ifdef CONFIG_MATH_EMULATION
51 #include <asm/math_emu.h>
52 #endif
54 #include <linux/err.h>
56 #include <asm/tlbflush.h>
57 #include <asm/cpu.h>
58 #include <asm/kdebug.h>
60 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
62 DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
63 EXPORT_PER_CPU_SYMBOL(current_task);
65 DEFINE_PER_CPU(int, cpu_number);
66 EXPORT_PER_CPU_SYMBOL(cpu_number);
69 * Return saved PC of a blocked thread.
71 unsigned long thread_saved_pc(struct task_struct *tsk)
73 return ((unsigned long *)tsk->thread.sp)[3];
76 #ifdef CONFIG_HOTPLUG_CPU
77 #include <asm/nmi.h>
79 static void cpu_exit_clear(void)
81 int cpu = raw_smp_processor_id();
83 idle_task_exit();
85 cpu_uninit();
86 irq_ctx_exit(cpu);
88 cpu_clear(cpu, cpu_callout_map);
89 cpu_clear(cpu, cpu_callin_map);
91 numa_remove_cpu(cpu);
94 /* We don't actually take CPU down, just spin without interrupts. */
95 static inline void play_dead(void)
97 /* This must be done before dead CPU ack */
98 cpu_exit_clear();
99 mb();
100 /* Ack it */
101 __get_cpu_var(cpu_state) = CPU_DEAD;
104 * With physical CPU hotplug, we should halt the cpu
106 local_irq_disable();
107 /* mask all interrupts, flush any and all caches, and halt */
108 wbinvd_halt();
110 #else
111 static inline void play_dead(void)
113 BUG();
115 #endif /* CONFIG_HOTPLUG_CPU */
118 * The idle thread. There's no useful work to be
119 * done, so just try to conserve power and have a
120 * low exit latency (ie sit in a loop waiting for
121 * somebody to say that they'd like to reschedule)
123 void cpu_idle(void)
125 int cpu = smp_processor_id();
127 current_thread_info()->status |= TS_POLLING;
129 /* endless idle loop with no priority at all */
130 while (1) {
131 tick_nohz_stop_sched_tick(1);
132 while (!need_resched()) {
134 check_pgt_cache();
135 rmb();
137 if (rcu_pending(cpu))
138 rcu_check_callbacks(cpu, 0);
140 if (cpu_is_offline(cpu))
141 play_dead();
143 local_irq_disable();
144 __get_cpu_var(irq_stat).idle_timestamp = jiffies;
145 /* Don't trace irqs off for idle */
146 stop_critical_timings();
147 pm_idle();
148 start_critical_timings();
150 tick_nohz_restart_sched_tick();
151 preempt_enable_no_resched();
152 schedule();
153 preempt_disable();
157 void __show_registers(struct pt_regs *regs, int all)
159 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
160 unsigned long d0, d1, d2, d3, d6, d7;
161 unsigned long sp;
162 unsigned short ss, gs;
163 const char *board;
165 if (user_mode_vm(regs)) {
166 sp = regs->sp;
167 ss = regs->ss & 0xffff;
168 savesegment(gs, gs);
169 } else {
170 sp = (unsigned long) (&regs->sp);
171 savesegment(ss, ss);
172 savesegment(gs, gs);
175 printk("\n");
177 board = dmi_get_system_info(DMI_PRODUCT_NAME);
178 if (!board)
179 board = "";
180 printk("Pid: %d, comm: %s %s (%s %.*s) %s\n",
181 task_pid_nr(current), current->comm,
182 print_tainted(), init_utsname()->release,
183 (int)strcspn(init_utsname()->version, " "),
184 init_utsname()->version, board);
186 printk("EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
187 (u16)regs->cs, regs->ip, regs->flags,
188 smp_processor_id());
189 print_symbol("EIP is at %s\n", regs->ip);
191 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
192 regs->ax, regs->bx, regs->cx, regs->dx);
193 printk("ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
194 regs->si, regs->di, regs->bp, sp);
195 printk(" DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
196 (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
198 if (!all)
199 return;
201 cr0 = read_cr0();
202 cr2 = read_cr2();
203 cr3 = read_cr3();
204 cr4 = read_cr4_safe();
205 printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
206 cr0, cr2, cr3, cr4);
208 get_debugreg(d0, 0);
209 get_debugreg(d1, 1);
210 get_debugreg(d2, 2);
211 get_debugreg(d3, 3);
212 printk("DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
213 d0, d1, d2, d3);
215 get_debugreg(d6, 6);
216 get_debugreg(d7, 7);
217 printk("DR6: %08lx DR7: %08lx\n",
218 d6, d7);
221 void show_regs(struct pt_regs *regs)
223 __show_registers(regs, 1);
224 show_trace(NULL, regs, &regs->sp, regs->bp);
228 * This gets run with %bx containing the
229 * function to call, and %dx containing
230 * the "args".
232 extern void kernel_thread_helper(void);
235 * Create a kernel thread
237 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
239 struct pt_regs regs;
241 memset(&regs, 0, sizeof(regs));
243 regs.bx = (unsigned long) fn;
244 regs.dx = (unsigned long) arg;
246 regs.ds = __USER_DS;
247 regs.es = __USER_DS;
248 regs.fs = __KERNEL_PERCPU;
249 regs.orig_ax = -1;
250 regs.ip = (unsigned long) kernel_thread_helper;
251 regs.cs = __KERNEL_CS | get_kernel_rpl();
252 regs.flags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
254 /* Ok, create the new process.. */
255 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
257 EXPORT_SYMBOL(kernel_thread);
260 * Free current thread data structures etc..
262 void exit_thread(void)
264 /* The process may have allocated an io port bitmap... nuke it. */
265 if (unlikely(test_thread_flag(TIF_IO_BITMAP))) {
266 struct task_struct *tsk = current;
267 struct thread_struct *t = &tsk->thread;
268 int cpu = get_cpu();
269 struct tss_struct *tss = &per_cpu(init_tss, cpu);
271 kfree(t->io_bitmap_ptr);
272 t->io_bitmap_ptr = NULL;
273 clear_thread_flag(TIF_IO_BITMAP);
275 * Careful, clear this in the TSS too:
277 memset(tss->io_bitmap, 0xff, tss->io_bitmap_max);
278 t->io_bitmap_max = 0;
279 tss->io_bitmap_owner = NULL;
280 tss->io_bitmap_max = 0;
281 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
282 put_cpu();
286 void flush_thread(void)
288 struct task_struct *tsk = current;
290 tsk->thread.debugreg0 = 0;
291 tsk->thread.debugreg1 = 0;
292 tsk->thread.debugreg2 = 0;
293 tsk->thread.debugreg3 = 0;
294 tsk->thread.debugreg6 = 0;
295 tsk->thread.debugreg7 = 0;
296 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
297 clear_tsk_thread_flag(tsk, TIF_DEBUG);
299 * Forget coprocessor state..
301 tsk->fpu_counter = 0;
302 clear_fpu(tsk);
303 clear_used_math();
306 void release_thread(struct task_struct *dead_task)
308 BUG_ON(dead_task->mm);
309 release_vm86_irqs(dead_task);
313 * This gets called before we allocate a new thread and copy
314 * the current task into it.
316 void prepare_to_copy(struct task_struct *tsk)
318 unlazy_fpu(tsk);
321 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
322 unsigned long unused,
323 struct task_struct * p, struct pt_regs * regs)
325 struct pt_regs * childregs;
326 struct task_struct *tsk;
327 int err;
329 childregs = task_pt_regs(p);
330 *childregs = *regs;
331 childregs->ax = 0;
332 childregs->sp = sp;
334 p->thread.sp = (unsigned long) childregs;
335 p->thread.sp0 = (unsigned long) (childregs+1);
337 p->thread.ip = (unsigned long) ret_from_fork;
339 savesegment(gs, p->thread.gs);
341 tsk = current;
342 if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
343 p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
344 IO_BITMAP_BYTES, GFP_KERNEL);
345 if (!p->thread.io_bitmap_ptr) {
346 p->thread.io_bitmap_max = 0;
347 return -ENOMEM;
349 set_tsk_thread_flag(p, TIF_IO_BITMAP);
352 err = 0;
355 * Set a new TLS for the child thread?
357 if (clone_flags & CLONE_SETTLS)
358 err = do_set_thread_area(p, -1,
359 (struct user_desc __user *)childregs->si, 0);
361 if (err && p->thread.io_bitmap_ptr) {
362 kfree(p->thread.io_bitmap_ptr);
363 p->thread.io_bitmap_max = 0;
365 return err;
368 void
369 start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
371 __asm__("movl %0, %%gs" :: "r"(0));
372 regs->fs = 0;
373 set_fs(USER_DS);
374 regs->ds = __USER_DS;
375 regs->es = __USER_DS;
376 regs->ss = __USER_DS;
377 regs->cs = __USER_CS;
378 regs->ip = new_ip;
379 regs->sp = new_sp;
381 * Free the old FP and other extended state
383 free_thread_xstate(current);
385 EXPORT_SYMBOL_GPL(start_thread);
387 static void hard_disable_TSC(void)
389 write_cr4(read_cr4() | X86_CR4_TSD);
392 void disable_TSC(void)
394 preempt_disable();
395 if (!test_and_set_thread_flag(TIF_NOTSC))
397 * Must flip the CPU state synchronously with
398 * TIF_NOTSC in the current running context.
400 hard_disable_TSC();
401 preempt_enable();
404 static void hard_enable_TSC(void)
406 write_cr4(read_cr4() & ~X86_CR4_TSD);
409 static void enable_TSC(void)
411 preempt_disable();
412 if (test_and_clear_thread_flag(TIF_NOTSC))
414 * Must flip the CPU state synchronously with
415 * TIF_NOTSC in the current running context.
417 hard_enable_TSC();
418 preempt_enable();
421 int get_tsc_mode(unsigned long adr)
423 unsigned int val;
425 if (test_thread_flag(TIF_NOTSC))
426 val = PR_TSC_SIGSEGV;
427 else
428 val = PR_TSC_ENABLE;
430 return put_user(val, (unsigned int __user *)adr);
433 int set_tsc_mode(unsigned int val)
435 if (val == PR_TSC_SIGSEGV)
436 disable_TSC();
437 else if (val == PR_TSC_ENABLE)
438 enable_TSC();
439 else
440 return -EINVAL;
442 return 0;
445 static noinline void
446 __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
447 struct tss_struct *tss)
449 struct thread_struct *prev, *next;
450 unsigned long debugctl;
452 prev = &prev_p->thread;
453 next = &next_p->thread;
455 debugctl = prev->debugctlmsr;
456 if (next->ds_area_msr != prev->ds_area_msr) {
457 /* we clear debugctl to make sure DS
458 * is not in use when we change it */
459 debugctl = 0;
460 update_debugctlmsr(0);
461 wrmsr(MSR_IA32_DS_AREA, next->ds_area_msr, 0);
464 if (next->debugctlmsr != debugctl)
465 update_debugctlmsr(next->debugctlmsr);
467 if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
468 set_debugreg(next->debugreg0, 0);
469 set_debugreg(next->debugreg1, 1);
470 set_debugreg(next->debugreg2, 2);
471 set_debugreg(next->debugreg3, 3);
472 /* no 4 and 5 */
473 set_debugreg(next->debugreg6, 6);
474 set_debugreg(next->debugreg7, 7);
477 if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
478 test_tsk_thread_flag(next_p, TIF_NOTSC)) {
479 /* prev and next are different */
480 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
481 hard_disable_TSC();
482 else
483 hard_enable_TSC();
486 #ifdef X86_BTS
487 if (test_tsk_thread_flag(prev_p, TIF_BTS_TRACE_TS))
488 ptrace_bts_take_timestamp(prev_p, BTS_TASK_DEPARTS);
490 if (test_tsk_thread_flag(next_p, TIF_BTS_TRACE_TS))
491 ptrace_bts_take_timestamp(next_p, BTS_TASK_ARRIVES);
492 #endif
495 if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
497 * Disable the bitmap via an invalid offset. We still cache
498 * the previous bitmap owner and the IO bitmap contents:
500 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
501 return;
504 if (likely(next == tss->io_bitmap_owner)) {
506 * Previous owner of the bitmap (hence the bitmap content)
507 * matches the next task, we dont have to do anything but
508 * to set a valid offset in the TSS:
510 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
511 return;
514 * Lazy TSS's I/O bitmap copy. We set an invalid offset here
515 * and we let the task to get a GPF in case an I/O instruction
516 * is performed. The handler of the GPF will verify that the
517 * faulting task has a valid I/O bitmap and, it true, does the
518 * real copy and restart the instruction. This will save us
519 * redundant copies when the currently switched task does not
520 * perform any I/O during its timeslice.
522 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
526 * switch_to(x,yn) should switch tasks from x to y.
528 * We fsave/fwait so that an exception goes off at the right time
529 * (as a call from the fsave or fwait in effect) rather than to
530 * the wrong process. Lazy FP saving no longer makes any sense
531 * with modern CPU's, and this simplifies a lot of things (SMP
532 * and UP become the same).
534 * NOTE! We used to use the x86 hardware context switching. The
535 * reason for not using it any more becomes apparent when you
536 * try to recover gracefully from saved state that is no longer
537 * valid (stale segment register values in particular). With the
538 * hardware task-switch, there is no way to fix up bad state in
539 * a reasonable manner.
541 * The fact that Intel documents the hardware task-switching to
542 * be slow is a fairly red herring - this code is not noticeably
543 * faster. However, there _is_ some room for improvement here,
544 * so the performance issues may eventually be a valid point.
545 * More important, however, is the fact that this allows us much
546 * more flexibility.
548 * The return value (in %ax) will be the "prev" task after
549 * the task-switch, and shows up in ret_from_fork in entry.S,
550 * for example.
552 struct task_struct * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
554 struct thread_struct *prev = &prev_p->thread,
555 *next = &next_p->thread;
556 int cpu = smp_processor_id();
557 struct tss_struct *tss = &per_cpu(init_tss, cpu);
559 /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
561 __unlazy_fpu(prev_p);
564 /* we're going to use this soon, after a few expensive things */
565 if (next_p->fpu_counter > 5)
566 prefetch(next->xstate);
569 * Reload esp0.
571 load_sp0(tss, next);
574 * Save away %gs. No need to save %fs, as it was saved on the
575 * stack on entry. No need to save %es and %ds, as those are
576 * always kernel segments while inside the kernel. Doing this
577 * before setting the new TLS descriptors avoids the situation
578 * where we temporarily have non-reloadable segments in %fs
579 * and %gs. This could be an issue if the NMI handler ever
580 * used %fs or %gs (it does not today), or if the kernel is
581 * running inside of a hypervisor layer.
583 savesegment(gs, prev->gs);
586 * Load the per-thread Thread-Local Storage descriptor.
588 load_TLS(next, cpu);
591 * Restore IOPL if needed. In normal use, the flags restore
592 * in the switch assembly will handle this. But if the kernel
593 * is running virtualized at a non-zero CPL, the popf will
594 * not restore flags, so it must be done in a separate step.
596 if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
597 set_iopl_mask(next->iopl);
600 * Now maybe handle debug registers and/or IO bitmaps
602 if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
603 task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
604 __switch_to_xtra(prev_p, next_p, tss);
607 * Leave lazy mode, flushing any hypercalls made here.
608 * This must be done before restoring TLS segments so
609 * the GDT and LDT are properly updated, and must be
610 * done before math_state_restore, so the TS bit is up
611 * to date.
613 arch_leave_lazy_cpu_mode();
615 /* If the task has used fpu the last 5 timeslices, just do a full
616 * restore of the math state immediately to avoid the trap; the
617 * chances of needing FPU soon are obviously high now
619 * tsk_used_math() checks prevent calling math_state_restore(),
620 * which can sleep in the case of !tsk_used_math()
622 if (tsk_used_math(next_p) && next_p->fpu_counter > 5)
623 math_state_restore();
626 * Restore %gs if needed (which is common)
628 if (prev->gs | next->gs)
629 loadsegment(gs, next->gs);
631 x86_write_percpu(current_task, next_p);
633 return prev_p;
636 asmlinkage int sys_fork(struct pt_regs regs)
638 return do_fork(SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
641 asmlinkage int sys_clone(struct pt_regs regs)
643 unsigned long clone_flags;
644 unsigned long newsp;
645 int __user *parent_tidptr, *child_tidptr;
647 clone_flags = regs.bx;
648 newsp = regs.cx;
649 parent_tidptr = (int __user *)regs.dx;
650 child_tidptr = (int __user *)regs.di;
651 if (!newsp)
652 newsp = regs.sp;
653 return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
657 * This is trivial, and on the face of it looks like it
658 * could equally well be done in user mode.
660 * Not so, for quite unobvious reasons - register pressure.
661 * In user mode vfork() cannot have a stack frame, and if
662 * done by calling the "clone()" system call directly, you
663 * do not have enough call-clobbered registers to hold all
664 * the information you need.
666 asmlinkage int sys_vfork(struct pt_regs regs)
668 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
672 * sys_execve() executes a new program.
674 asmlinkage int sys_execve(struct pt_regs regs)
676 int error;
677 char * filename;
679 filename = getname((char __user *) regs.bx);
680 error = PTR_ERR(filename);
681 if (IS_ERR(filename))
682 goto out;
683 error = do_execve(filename,
684 (char __user * __user *) regs.cx,
685 (char __user * __user *) regs.dx,
686 &regs);
687 if (error == 0) {
688 /* Make sure we don't return using sysenter.. */
689 set_thread_flag(TIF_IRET);
691 putname(filename);
692 out:
693 return error;
696 #define top_esp (THREAD_SIZE - sizeof(unsigned long))
697 #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
699 unsigned long get_wchan(struct task_struct *p)
701 unsigned long bp, sp, ip;
702 unsigned long stack_page;
703 int count = 0;
704 if (!p || p == current || p->state == TASK_RUNNING)
705 return 0;
706 stack_page = (unsigned long)task_stack_page(p);
707 sp = p->thread.sp;
708 if (!stack_page || sp < stack_page || sp > top_esp+stack_page)
709 return 0;
710 /* include/asm-i386/system.h:switch_to() pushes bp last. */
711 bp = *(unsigned long *) sp;
712 do {
713 if (bp < stack_page || bp > top_ebp+stack_page)
714 return 0;
715 ip = *(unsigned long *) (bp+4);
716 if (!in_sched_functions(ip))
717 return ip;
718 bp = *(unsigned long *) bp;
719 } while (count++ < 16);
720 return 0;
723 unsigned long arch_align_stack(unsigned long sp)
725 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
726 sp -= get_random_int() % 8192;
727 return sp & ~0xf;
730 unsigned long arch_randomize_brk(struct mm_struct *mm)
732 unsigned long range_end = mm->brk + 0x02000000;
733 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;