MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / arch / x86_64 / kernel / process.c
blob6e835be5f26a6aba9da3ffc878e3419599927513
1 /*
2 * linux/arch/x86-64/kernel/process.c
4 * Copyright (C) 1995 Linus Torvalds
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
8 *
9 * X86-64 port
10 * Andi Kleen.
12 * $Id: process.c,v 1.38 2002/01/15 10:08:03 ak Exp $
16 * This file handles the architecture-dependent parts of process handling..
19 #include <stdarg.h>
21 #include <linux/errno.h>
22 #include <linux/sched.h>
23 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/elfcore.h>
26 #include <linux/smp.h>
27 #include <linux/slab.h>
28 #include <linux/user.h>
29 #include <linux/module.h>
30 #include <linux/a.out.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/irq.h>
34 #include <linux/ptrace.h>
35 #include <linux/version.h>
37 #include <asm/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/processor.h>
42 #include <asm/i387.h>
43 #include <asm/mmu_context.h>
44 #include <asm/pda.h>
45 #include <asm/prctl.h>
46 #include <asm/kdebug.h>
47 #include <asm/desc.h>
48 #include <asm/proto.h>
49 #include <asm/ia32.h>
51 asmlinkage extern void ret_from_fork(void);
53 unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;
55 atomic_t hlt_counter = ATOMIC_INIT(0);
58 * Powermanagement idle function, if any..
60 void (*pm_idle)(void);
62 void disable_hlt(void)
64 atomic_inc(&hlt_counter);
67 EXPORT_SYMBOL(disable_hlt);
69 void enable_hlt(void)
71 atomic_dec(&hlt_counter);
74 EXPORT_SYMBOL(enable_hlt);
77 * We use this if we don't have any better
78 * idle routine..
80 void default_idle(void)
82 if (!atomic_read(&hlt_counter)) {
83 local_irq_disable();
84 if (!need_resched())
85 safe_halt();
86 else
87 local_irq_enable();
92 * On SMP it's slightly faster (but much more power-consuming!)
93 * to poll the ->need_resched flag instead of waiting for the
94 * cross-CPU IPI to arrive. Use this option with caution.
96 static void poll_idle (void)
98 int oldval;
100 local_irq_enable();
103 * Deal with another CPU just having chosen a thread to
104 * run here:
106 oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
108 if (!oldval) {
109 set_thread_flag(TIF_POLLING_NRFLAG);
110 asm volatile(
111 "2:"
112 "testl %0,%1;"
113 "rep; nop;"
114 "je 2b;"
116 "i" (_TIF_NEED_RESCHED),
117 "m" (current_thread_info()->flags));
118 } else {
119 set_need_resched();
124 * The idle thread. There's no useful work to be
125 * done, so just try to conserve power and have a
126 * low exit latency (ie sit in a loop waiting for
127 * somebody to say that they'd like to reschedule)
129 void cpu_idle (void)
131 /* endless idle loop with no priority at all */
132 while (1) {
133 while (!need_resched()) {
134 void (*idle)(void);
136 * Mark this as an RCU critical section so that
137 * synchronize_kernel() in the unload path waits
138 * for our completion.
140 rcu_read_lock();
141 idle = pm_idle;
142 if (!idle)
143 idle = default_idle;
144 idle();
145 rcu_read_unlock();
147 schedule();
152 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
153 * which can obviate IPI to trigger checking of need_resched.
154 * We execute MONITOR against need_resched and enter optimized wait state
155 * through MWAIT. Whenever someone changes need_resched, we would be woken
156 * up from MWAIT (without an IPI).
158 static void mwait_idle(void)
160 local_irq_enable();
162 if (!need_resched()) {
163 set_thread_flag(TIF_POLLING_NRFLAG);
164 do {
165 __monitor((void *)&current_thread_info()->flags, 0, 0);
166 if (need_resched())
167 break;
168 __mwait(0, 0);
169 } while (!need_resched());
170 clear_thread_flag(TIF_POLLING_NRFLAG);
174 void __init select_idle_routine(const struct cpuinfo_x86 *c)
176 static int printed;
177 if (cpu_has(c, X86_FEATURE_MWAIT)) {
179 * Skip, if setup has overridden idle.
180 * One CPU supports mwait => All CPUs supports mwait
182 if (!pm_idle) {
183 if (!printed) {
184 printk("using mwait in idle threads.\n");
185 printed = 1;
187 pm_idle = mwait_idle;
192 static int __init idle_setup (char *str)
194 if (!strncmp(str, "poll", 4)) {
195 printk("using polling idle threads.\n");
196 pm_idle = poll_idle;
199 return 1;
202 __setup("idle=", idle_setup);
204 /* Prints also some state that isn't saved in the pt_regs */
205 void __show_regs(struct pt_regs * regs)
207 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
208 unsigned int fsindex,gsindex;
209 unsigned int ds,cs,es;
211 printk("\n");
212 print_modules();
213 printk("Pid: %d, comm: %.20s %s %s\n",
214 current->pid, current->comm, print_tainted(), UTS_RELEASE);
215 printk("RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->rip);
216 printk_address(regs->rip);
217 printk("\nRSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss, regs->rsp, regs->eflags);
218 printk("RAX: %016lx RBX: %016lx RCX: %016lx\n",
219 regs->rax, regs->rbx, regs->rcx);
220 printk("RDX: %016lx RSI: %016lx RDI: %016lx\n",
221 regs->rdx, regs->rsi, regs->rdi);
222 printk("RBP: %016lx R08: %016lx R09: %016lx\n",
223 regs->rbp, regs->r8, regs->r9);
224 printk("R10: %016lx R11: %016lx R12: %016lx\n",
225 regs->r10, regs->r11, regs->r12);
226 printk("R13: %016lx R14: %016lx R15: %016lx\n",
227 regs->r13, regs->r14, regs->r15);
229 asm("movl %%ds,%0" : "=r" (ds));
230 asm("movl %%cs,%0" : "=r" (cs));
231 asm("movl %%es,%0" : "=r" (es));
232 asm("movl %%fs,%0" : "=r" (fsindex));
233 asm("movl %%gs,%0" : "=r" (gsindex));
235 rdmsrl(MSR_FS_BASE, fs);
236 rdmsrl(MSR_GS_BASE, gs);
237 rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
239 asm("movq %%cr0, %0": "=r" (cr0));
240 asm("movq %%cr2, %0": "=r" (cr2));
241 asm("movq %%cr3, %0": "=r" (cr3));
242 asm("movq %%cr4, %0": "=r" (cr4));
244 printk("FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
245 fs,fsindex,gs,gsindex,shadowgs);
246 printk("CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds, es, cr0);
247 printk("CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3, cr4);
250 void show_regs(struct pt_regs *regs)
252 __show_regs(regs);
253 show_trace(&regs->rsp);
257 * Free current thread data structures etc..
259 void exit_thread(void)
261 struct task_struct *me = current;
262 struct thread_struct *t = &me->thread;
263 if (me->thread.io_bitmap_ptr) {
264 struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
266 kfree(t->io_bitmap_ptr);
267 t->io_bitmap_ptr = NULL;
269 * Careful, clear this in the TSS too:
271 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
272 t->io_bitmap_max = 0;
273 put_cpu();
277 void flush_thread(void)
279 struct task_struct *tsk = current;
280 struct thread_info *t = current_thread_info();
282 if (t->flags & _TIF_ABI_PENDING)
283 t->flags ^= (_TIF_ABI_PENDING | _TIF_IA32);
285 tsk->thread.debugreg0 = 0;
286 tsk->thread.debugreg1 = 0;
287 tsk->thread.debugreg2 = 0;
288 tsk->thread.debugreg3 = 0;
289 tsk->thread.debugreg6 = 0;
290 tsk->thread.debugreg7 = 0;
291 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
293 * Forget coprocessor state..
295 clear_fpu(tsk);
296 tsk->used_math = 0;
299 void release_thread(struct task_struct *dead_task)
301 if (dead_task->mm) {
302 if (dead_task->mm->context.size) {
303 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
304 dead_task->comm,
305 dead_task->mm->context.ldt,
306 dead_task->mm->context.size);
307 BUG();
312 static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
314 struct user_desc ud = {
315 .base_addr = addr,
316 .limit = 0xfffff,
317 .seg_32bit = 1,
318 .limit_in_pages = 1,
319 .useable = 1,
321 struct n_desc_struct *desc = (void *)t->thread.tls_array;
322 desc += tls;
323 desc->a = LDT_entry_a(&ud);
324 desc->b = LDT_entry_b(&ud);
327 static inline u32 read_32bit_tls(struct task_struct *t, int tls)
329 struct desc_struct *desc = (void *)t->thread.tls_array;
330 desc += tls;
331 return desc->base0 |
332 (((u32)desc->base1) << 16) |
333 (((u32)desc->base2) << 24);
337 * This gets called before we allocate a new thread and copy
338 * the current task into it.
340 void prepare_to_copy(struct task_struct *tsk)
342 unlazy_fpu(tsk);
345 int copy_thread(int nr, unsigned long clone_flags, unsigned long rsp,
346 unsigned long unused,
347 struct task_struct * p, struct pt_regs * regs)
349 int err;
350 struct pt_regs * childregs;
351 struct task_struct *me = current;
353 childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p->thread_info)) - 1;
355 *childregs = *regs;
357 childregs->rax = 0;
358 childregs->rsp = rsp;
359 if (rsp == ~0UL) {
360 childregs->rsp = (unsigned long)childregs;
362 p->set_child_tid = p->clear_child_tid = NULL;
364 p->thread.rsp = (unsigned long) childregs;
365 p->thread.rsp0 = (unsigned long) (childregs+1);
366 p->thread.userrsp = me->thread.userrsp;
368 set_ti_thread_flag(p->thread_info, TIF_FORK);
370 p->thread.fs = me->thread.fs;
371 p->thread.gs = me->thread.gs;
373 asm("movl %%gs,%0" : "=m" (p->thread.gsindex));
374 asm("movl %%fs,%0" : "=m" (p->thread.fsindex));
375 asm("movl %%es,%0" : "=m" (p->thread.es));
376 asm("movl %%ds,%0" : "=m" (p->thread.ds));
378 if (unlikely(me->thread.io_bitmap_ptr != NULL)) {
379 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
380 if (!p->thread.io_bitmap_ptr) {
381 p->thread.io_bitmap_max = 0;
382 return -ENOMEM;
384 memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr, IO_BITMAP_BYTES);
388 * Set a new TLS for the child thread?
390 if (clone_flags & CLONE_SETTLS) {
391 #ifdef CONFIG_IA32_EMULATION
392 if (test_thread_flag(TIF_IA32))
393 err = ia32_child_tls(p, childregs);
394 else
395 #endif
396 err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
397 if (err)
398 goto out;
400 err = 0;
401 out:
402 if (err && p->thread.io_bitmap_ptr) {
403 kfree(p->thread.io_bitmap_ptr);
404 p->thread.io_bitmap_max = 0;
406 return err;
410 * This special macro can be used to load a debugging register
412 #define loaddebug(thread,r) set_debug(thread->debugreg ## r, r)
415 * switch_to(x,y) should switch tasks from x to y.
417 * This could still be optimized:
418 * - fold all the options into a flag word and test it with a single test.
419 * - could test fs/gs bitsliced
421 struct task_struct *__switch_to(struct task_struct *prev_p, struct task_struct *next_p)
423 struct thread_struct *prev = &prev_p->thread,
424 *next = &next_p->thread;
425 int cpu = smp_processor_id();
426 struct tss_struct *tss = &per_cpu(init_tss, cpu);
428 unlazy_fpu(prev_p);
431 * Reload esp0, LDT and the page table pointer:
433 tss->rsp0 = next->rsp0;
436 * Switch DS and ES.
437 * This won't pick up thread selector changes, but I guess that is ok.
439 asm volatile("movl %%es,%0" : "=m" (prev->es));
440 if (unlikely(next->es | prev->es))
441 loadsegment(es, next->es);
443 asm volatile ("movl %%ds,%0" : "=m" (prev->ds));
444 if (unlikely(next->ds | prev->ds))
445 loadsegment(ds, next->ds);
447 load_TLS(next, cpu);
450 * Switch FS and GS.
453 unsigned fsindex;
454 asm volatile("movl %%fs,%0" : "=g" (fsindex));
455 /* segment register != 0 always requires a reload.
456 also reload when it has changed.
457 when prev process used 64bit base always reload
458 to avoid an information leak. */
459 if (unlikely(fsindex | next->fsindex | prev->fs)) {
460 loadsegment(fs, next->fsindex);
461 /* check if the user used a selector != 0
462 * if yes clear 64bit base, since overloaded base
463 * is always mapped to the Null selector
465 if (fsindex)
466 prev->fs = 0;
468 /* when next process has a 64bit base use it */
469 if (next->fs)
470 wrmsrl(MSR_FS_BASE, next->fs);
471 prev->fsindex = fsindex;
474 unsigned gsindex;
475 asm volatile("movl %%gs,%0" : "=g" (gsindex));
476 if (unlikely(gsindex | next->gsindex | prev->gs)) {
477 load_gs_index(next->gsindex);
478 if (gsindex)
479 prev->gs = 0;
481 if (next->gs)
482 wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
483 prev->gsindex = gsindex;
487 * Switch the PDA context.
489 prev->userrsp = read_pda(oldrsp);
490 write_pda(oldrsp, next->userrsp);
491 write_pda(pcurrent, next_p);
492 write_pda(kernelstack, (unsigned long)next_p->thread_info + THREAD_SIZE - PDA_STACKOFFSET);
495 * Now maybe reload the debug registers
497 if (unlikely(next->debugreg7)) {
498 loaddebug(next, 0);
499 loaddebug(next, 1);
500 loaddebug(next, 2);
501 loaddebug(next, 3);
502 /* no 4 and 5 */
503 loaddebug(next, 6);
504 loaddebug(next, 7);
509 * Handle the IO bitmap
511 if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr)) {
512 if (next->io_bitmap_ptr)
514 * Copy the relevant range of the IO bitmap.
515 * Normally this is 128 bytes or less:
517 memcpy(tss->io_bitmap, next->io_bitmap_ptr,
518 max(prev->io_bitmap_max, next->io_bitmap_max));
519 else {
521 * Clear any possible leftover bits:
523 memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
527 return prev_p;
531 * sys_execve() executes a new program.
533 asmlinkage
534 long sys_execve(char __user *name, char __user * __user *argv,
535 char __user * __user *envp, struct pt_regs regs)
537 long error;
538 char * filename;
540 filename = getname(name);
541 error = PTR_ERR(filename);
542 if (IS_ERR(filename))
543 return error;
544 error = do_execve(filename, argv, envp, &regs);
545 if (error == 0)
546 current->ptrace &= ~PT_DTRACE;
547 putname(filename);
548 return error;
551 void set_personality_64bit(void)
553 /* inherit personality from parent */
555 /* Make sure to be in 64bit mode */
556 clear_thread_flag(TIF_IA32);
559 asmlinkage long sys_fork(struct pt_regs *regs)
561 return do_fork(SIGCHLD, regs->rsp, regs, 0, NULL, NULL);
564 asmlinkage long sys_clone(unsigned long clone_flags, unsigned long newsp, void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
566 if (!newsp)
567 newsp = regs->rsp;
568 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
572 * This is trivial, and on the face of it looks like it
573 * could equally well be done in user mode.
575 * Not so, for quite unobvious reasons - register pressure.
576 * In user mode vfork() cannot have a stack frame, and if
577 * done by calling the "clone()" system call directly, you
578 * do not have enough call-clobbered registers to hold all
579 * the information you need.
581 asmlinkage long sys_vfork(struct pt_regs *regs)
583 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->rsp, regs, 0,
584 NULL, NULL);
587 unsigned long get_wchan(struct task_struct *p)
589 unsigned long stack;
590 u64 fp,rip;
591 int count = 0;
593 if (!p || p == current || p->state==TASK_RUNNING)
594 return 0;
595 stack = (unsigned long)p->thread_info;
596 if (p->thread.rsp < stack || p->thread.rsp > stack+THREAD_SIZE)
597 return 0;
598 fp = *(u64 *)(p->thread.rsp);
599 do {
600 if (fp < (unsigned long)stack || fp > (unsigned long)stack+THREAD_SIZE)
601 return 0;
602 rip = *(u64 *)(fp+8);
603 if (!in_sched_functions(rip))
604 return rip;
605 fp = *(u64 *)fp;
606 } while (count++ < 16);
607 return 0;
610 long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
612 int ret = 0;
613 int doit = task == current;
614 int cpu;
616 switch (code) {
617 case ARCH_SET_GS:
618 if (addr >= TASK_SIZE)
619 return -EPERM;
620 cpu = get_cpu();
621 /* handle small bases via the GDT because that's faster to
622 switch. */
623 if (addr <= 0xffffffff) {
624 set_32bit_tls(task, GS_TLS, addr);
625 if (doit) {
626 load_TLS(&task->thread, cpu);
627 load_gs_index(GS_TLS_SEL);
629 task->thread.gsindex = GS_TLS_SEL;
630 task->thread.gs = 0;
631 } else {
632 task->thread.gsindex = 0;
633 task->thread.gs = addr;
634 if (doit) {
635 load_gs_index(0);
636 ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
639 put_cpu();
640 break;
641 case ARCH_SET_FS:
642 /* Not strictly needed for fs, but do it for symmetry
643 with gs */
644 if (addr >= TASK_SIZE)
645 return -EPERM;
646 cpu = get_cpu();
647 /* handle small bases via the GDT because that's faster to
648 switch. */
649 if (addr <= 0xffffffff) {
650 set_32bit_tls(task, FS_TLS, addr);
651 if (doit) {
652 load_TLS(&task->thread, cpu);
653 asm volatile("movl %0,%%fs" :: "r" (FS_TLS_SEL));
655 task->thread.fsindex = FS_TLS_SEL;
656 task->thread.fs = 0;
657 } else {
658 task->thread.fsindex = 0;
659 task->thread.fs = addr;
660 if (doit) {
661 /* set the selector to 0 to not confuse
662 __switch_to */
663 asm volatile("movl %0,%%fs" :: "r" (0));
664 ret = checking_wrmsrl(MSR_FS_BASE, addr);
667 put_cpu();
668 break;
669 case ARCH_GET_FS: {
670 unsigned long base;
671 if (task->thread.fsindex == FS_TLS_SEL)
672 base = read_32bit_tls(task, FS_TLS);
673 else if (doit) {
674 rdmsrl(MSR_FS_BASE, base);
675 } else
676 base = task->thread.fs;
677 ret = put_user(base, (unsigned long __user *)addr);
678 break;
680 case ARCH_GET_GS: {
681 unsigned long base;
682 if (task->thread.gsindex == GS_TLS_SEL)
683 base = read_32bit_tls(task, GS_TLS);
684 else if (doit) {
685 rdmsrl(MSR_KERNEL_GS_BASE, base);
686 } else
687 base = task->thread.gs;
688 ret = put_user(base, (unsigned long __user *)addr);
689 break;
692 default:
693 ret = -EINVAL;
694 break;
697 return ret;
700 long sys_arch_prctl(int code, unsigned long addr)
702 return do_arch_prctl(current, code, addr);
706 * Capture the user space registers if the task is not running (in user space)
708 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
710 struct pt_regs *pp, ptregs;
712 pp = (struct pt_regs *)(tsk->thread.rsp0);
713 --pp;
715 ptregs = *pp;
716 ptregs.cs &= 0xffff;
717 ptregs.ss &= 0xffff;
719 elf_core_copy_regs(regs, &ptregs);
721 return 1;