vserver 1.9.3
[linux-2.6.git] / arch / x86_64 / kernel / process.c
1 /*
2  *  linux/arch/x86-64/kernel/process.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *
6  *  Pentium III FXSR, SSE support
7  *      Gareth Hughes <gareth@valinux.com>, May 2000
8  * 
9  *  X86-64 port
10  *      Andi Kleen.
11  * 
12  *  $Id: process.c,v 1.38 2002/01/15 10:08:03 ak Exp $
13  */
14
15 /*
16  * This file handles the architecture-dependent parts of process handling..
17  */
18
19 #include <stdarg.h>
20
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>
36
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>
50
51 asmlinkage extern void ret_from_fork(void);
52
53 unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;
54
55 atomic_t hlt_counter = ATOMIC_INIT(0);
56
57 /*
58  * Powermanagement idle function, if any..
59  */
60 void (*pm_idle)(void);
61
62 void disable_hlt(void)
63 {
64         atomic_inc(&hlt_counter);
65 }
66
67 EXPORT_SYMBOL(disable_hlt);
68
69 void enable_hlt(void)
70 {
71         atomic_dec(&hlt_counter);
72 }
73
74 EXPORT_SYMBOL(enable_hlt);
75
76 /*
77  * We use this if we don't have any better
78  * idle routine..
79  */
80 void default_idle(void)
81 {
82         if (!atomic_read(&hlt_counter)) {
83                 local_irq_disable();
84                 if (!need_resched())
85                         safe_halt();
86                 else
87                         local_irq_enable();
88         }
89 }
90
91 /*
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.
95  */
96 static void poll_idle (void)
97 {
98         int oldval;
99
100         local_irq_enable();
101
102         /*
103          * Deal with another CPU just having chosen a thread to
104          * run here:
105          */
106         oldval = test_and_clear_thread_flag(TIF_NEED_RESCHED);
107
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;"
115                         : :
116                         "i" (_TIF_NEED_RESCHED), 
117                         "m" (current_thread_info()->flags));
118         } else {
119                 set_need_resched();
120         }
121 }
122
123 /*
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)
128  */
129 void cpu_idle (void)
130 {
131         /* endless idle loop with no priority at all */
132         while (1) {
133                 while (!need_resched()) {
134                         void (*idle)(void);
135                         /*
136                          * Mark this as an RCU critical section so that
137                          * synchronize_kernel() in the unload path waits
138                          * for our completion.
139                          */
140                         rcu_read_lock();
141                         idle = pm_idle;
142                         if (!idle)
143                                 idle = default_idle;
144                         idle();
145                         rcu_read_unlock();
146                 }
147                 schedule();
148         }
149 }
150
151 /*
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).
157  */
158 static void mwait_idle(void)
159 {
160         local_irq_enable();
161
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);
171         }
172 }
173
174 void __init select_idle_routine(const struct cpuinfo_x86 *c)
175 {
176         static int printed;
177         if (cpu_has(c, X86_FEATURE_MWAIT)) {
178                 /*
179                  * Skip, if setup has overridden idle.
180                  * One CPU supports mwait => All CPUs supports mwait
181                  */
182                 if (!pm_idle) {
183                         if (!printed) {
184                                 printk("using mwait in idle threads.\n");
185                                 printed = 1;
186                         }
187                         pm_idle = mwait_idle;
188                 }
189         }
190 }
191
192 static int __init idle_setup (char *str)
193 {
194         if (!strncmp(str, "poll", 4)) {
195                 printk("using polling idle threads.\n");
196                 pm_idle = poll_idle;
197         }
198
199         return 1;
200 }
201
202 __setup("idle=", idle_setup);
203
204 /* Prints also some state that isn't saved in the pt_regs */ 
205 void __show_regs(struct pt_regs * regs)
206 {
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; 
210
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); 
228
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));
234
235         rdmsrl(MSR_FS_BASE, fs);
236         rdmsrl(MSR_GS_BASE, gs); 
237         rdmsrl(MSR_KERNEL_GS_BASE, shadowgs); 
238
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));
243
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);
248 }
249
250 void show_regs(struct pt_regs *regs)
251 {
252         __show_regs(regs);
253         show_trace(&regs->rsp);
254 }
255
256 /*
257  * Free current thread data structures etc..
258  */
259 void exit_thread(void)
260 {
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());
265
266                 kfree(t->io_bitmap_ptr);
267                 t->io_bitmap_ptr = NULL;
268                 /*
269                  * Careful, clear this in the TSS too:
270                  */
271                 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
272                 t->io_bitmap_max = 0;
273                 put_cpu();
274         }
275 }
276
277 void flush_thread(void)
278 {
279         struct task_struct *tsk = current;
280         struct thread_info *t = current_thread_info();
281
282         if (t->flags & _TIF_ABI_PENDING)
283                 t->flags ^= (_TIF_ABI_PENDING | _TIF_IA32);
284
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));        
292         /*
293          * Forget coprocessor state..
294          */
295         clear_fpu(tsk);
296         tsk->used_math = 0;
297 }
298
299 void release_thread(struct task_struct *dead_task)
300 {
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();
308                 }
309         }
310 }
311
312 static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
313 {
314         struct user_desc ud = { 
315                 .base_addr = addr,
316                 .limit = 0xfffff,
317                 .seg_32bit = 1,
318                 .limit_in_pages = 1,
319                 .useable = 1,
320         };
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); 
325 }
326
327 static inline u32 read_32bit_tls(struct task_struct *t, int tls)
328 {
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);
334 }
335
336 /*
337  * This gets called before we allocate a new thread and copy
338  * the current task into it.
339  */
340 void prepare_to_copy(struct task_struct *tsk)
341 {
342         unlazy_fpu(tsk);
343 }
344
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)
348 {
349         int err;
350         struct pt_regs * childregs;
351         struct task_struct *me = current;
352
353         childregs = ((struct pt_regs *) (THREAD_SIZE + (unsigned long) p->thread_info)) - 1;
354
355         *childregs = *regs;
356
357         childregs->rax = 0;
358         childregs->rsp = rsp;
359         if (rsp == ~0UL) {
360                 childregs->rsp = (unsigned long)childregs;
361         }
362         p->set_child_tid = p->clear_child_tid = NULL;
363
364         p->thread.rsp = (unsigned long) childregs;
365         p->thread.rsp0 = (unsigned long) (childregs+1);
366         p->thread.userrsp = me->thread.userrsp; 
367
368         set_ti_thread_flag(p->thread_info, TIF_FORK);
369
370         p->thread.fs = me->thread.fs;
371         p->thread.gs = me->thread.gs;
372
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));
377
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;
383                 }
384                 memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr, IO_BITMAP_BYTES);
385         } 
386
387         /*
388          * Set a new TLS for the child thread?
389          */
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;
399         }
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;
405         }
406         return err;
407 }
408
409 /*
410  * This special macro can be used to load a debugging register
411  */
412 #define loaddebug(thread,r) set_debug(thread->debugreg ## r, r)
413
414 /*
415  *      switch_to(x,y) should switch tasks from x to y.
416  *
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
420  */
421 struct task_struct *__switch_to(struct task_struct *prev_p, struct task_struct *next_p)
422 {
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);
427
428         unlazy_fpu(prev_p);
429
430         /*
431          * Reload esp0, LDT and the page table pointer:
432          */
433         tss->rsp0 = next->rsp0;
434
435         /* 
436          * Switch DS and ES.
437          * This won't pick up thread selector changes, but I guess that is ok.
438          */
439         asm volatile("movl %%es,%0" : "=m" (prev->es)); 
440         if (unlikely(next->es | prev->es))
441                 loadsegment(es, next->es); 
442         
443         asm volatile ("movl %%ds,%0" : "=m" (prev->ds)); 
444         if (unlikely(next->ds | prev->ds))
445                 loadsegment(ds, next->ds);
446
447         load_TLS(next, cpu);
448
449         /* 
450          * Switch FS and GS.
451          */
452         { 
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
464                          */
465                         if (fsindex)
466                         prev->fs = 0;                           
467                 }
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;
472         }
473         { 
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;                           
480                 }
481                 if (next->gs)
482                         wrmsrl(MSR_KERNEL_GS_BASE, next->gs); 
483                 prev->gsindex = gsindex;
484         }
485
486         /* 
487          * Switch the PDA context.
488          */
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);
493
494         /*
495          * Now maybe reload the debug registers
496          */
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);
505         }
506
507
508         /* 
509          * Handle the IO bitmap 
510          */ 
511         if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr)) {
512                 if (next->io_bitmap_ptr)
513                         /*
514                          * Copy the relevant range of the IO bitmap.
515                          * Normally this is 128 bytes or less:
516                          */
517                         memcpy(tss->io_bitmap, next->io_bitmap_ptr,
518                                 max(prev->io_bitmap_max, next->io_bitmap_max));
519                 else {
520                         /*
521                          * Clear any possible leftover bits:
522                          */
523                         memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
524                 }
525         }
526
527         return prev_p;
528 }
529
530 /*
531  * sys_execve() executes a new program.
532  */
533 asmlinkage 
534 long sys_execve(char __user *name, char __user * __user *argv,
535                 char __user * __user *envp, struct pt_regs regs)
536 {
537         long error;
538         char * filename;
539
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;
549 }
550
551 void set_personality_64bit(void)
552 {
553         /* inherit personality from parent */
554
555         /* Make sure to be in 64bit mode */
556         clear_thread_flag(TIF_IA32); 
557 }
558
559 asmlinkage long sys_fork(struct pt_regs *regs)
560 {
561         return do_fork(SIGCHLD, regs->rsp, regs, 0, NULL, NULL);
562 }
563
564 asmlinkage long sys_clone(unsigned long clone_flags, unsigned long newsp, void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
565 {
566         if (!newsp)
567                 newsp = regs->rsp;
568         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
569 }
570
571 /*
572  * This is trivial, and on the face of it looks like it
573  * could equally well be done in user mode.
574  *
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.
580  */
581 asmlinkage long sys_vfork(struct pt_regs *regs)
582 {
583         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->rsp, regs, 0,
584                     NULL, NULL);
585 }
586
587 unsigned long get_wchan(struct task_struct *p)
588 {
589         unsigned long stack;
590         u64 fp,rip;
591         int count = 0;
592
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;
608 }
609
610 long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
611
612         int ret = 0; 
613         int doit = task == current;
614         int cpu;
615
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); 
628                         }
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); 
637                         } 
638                 }
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));
654                         }
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); 
665                         }
666                 }
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; 
679         }
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;
690         }
691
692         default:
693                 ret = -EINVAL;
694                 break;
695         } 
696
697         return ret;     
698
699
700 long sys_arch_prctl(int code, unsigned long addr)
701 {
702         return do_arch_prctl(current, code, addr);
703
704
705 /* 
706  * Capture the user space registers if the task is not running (in user space)
707  */
708 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
709 {
710         struct pt_regs *pp, ptregs;
711
712         pp = (struct pt_regs *)(tsk->thread.rsp0);
713         --pp; 
714
715         ptregs = *pp; 
716         ptregs.cs &= 0xffff;
717         ptregs.ss &= 0xffff;
718
719         elf_core_copy_regs(regs, &ptregs);
720  
721         return 1;
722 }