x86: unify x86_32 and x86_64 play_dead into one function
[safe/jmp/linux-2.6] / arch / x86 / kernel / process_32.c
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 /*
9  * This file handles the architecture-dependent parts of process handling..
10  */
11
12 #include <stdarg.h>
13
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
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>
48 #include <asm/desc.h>
49 #ifdef CONFIG_MATH_EMULATION
50 #include <asm/math_emu.h>
51 #endif
52
53 #include <linux/err.h>
54
55 #include <asm/tlbflush.h>
56 #include <asm/cpu.h>
57 #include <asm/kdebug.h>
58
59 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
60
61 DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
62 EXPORT_PER_CPU_SYMBOL(current_task);
63
64 DEFINE_PER_CPU(int, cpu_number);
65 EXPORT_PER_CPU_SYMBOL(cpu_number);
66
67 /*
68  * Return saved PC of a blocked thread.
69  */
70 unsigned long thread_saved_pc(struct task_struct *tsk)
71 {
72         return ((unsigned long *)tsk->thread.sp)[3];
73 }
74
75 /*
76  * The idle thread. There's no useful work to be
77  * done, so just try to conserve power and have a
78  * low exit latency (ie sit in a loop waiting for
79  * somebody to say that they'd like to reschedule)
80  */
81 void cpu_idle(void)
82 {
83         int cpu = smp_processor_id();
84
85         current_thread_info()->status |= TS_POLLING;
86
87         /* endless idle loop with no priority at all */
88         while (1) {
89                 tick_nohz_stop_sched_tick(1);
90                 while (!need_resched()) {
91
92                         check_pgt_cache();
93                         rmb();
94
95                         if (rcu_pending(cpu))
96                                 rcu_check_callbacks(cpu, 0);
97
98                         if (cpu_is_offline(cpu))
99                                 play_dead();
100
101                         local_irq_disable();
102                         __get_cpu_var(irq_stat).idle_timestamp = jiffies;
103                         /* Don't trace irqs off for idle */
104                         stop_critical_timings();
105                         pm_idle();
106                         start_critical_timings();
107                 }
108                 tick_nohz_restart_sched_tick();
109                 preempt_enable_no_resched();
110                 schedule();
111                 preempt_disable();
112         }
113 }
114
115 void __show_registers(struct pt_regs *regs, int all)
116 {
117         unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
118         unsigned long d0, d1, d2, d3, d6, d7;
119         unsigned long sp;
120         unsigned short ss, gs;
121
122         if (user_mode_vm(regs)) {
123                 sp = regs->sp;
124                 ss = regs->ss & 0xffff;
125                 savesegment(gs, gs);
126         } else {
127                 sp = (unsigned long) (&regs->sp);
128                 savesegment(ss, ss);
129                 savesegment(gs, gs);
130         }
131
132         printk("\n");
133         printk("Pid: %d, comm: %s %s (%s %.*s)\n",
134                         task_pid_nr(current), current->comm,
135                         print_tainted(), init_utsname()->release,
136                         (int)strcspn(init_utsname()->version, " "),
137                         init_utsname()->version);
138
139         printk("EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
140                         (u16)regs->cs, regs->ip, regs->flags,
141                         smp_processor_id());
142         print_symbol("EIP is at %s\n", regs->ip);
143
144         printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
145                 regs->ax, regs->bx, regs->cx, regs->dx);
146         printk("ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
147                 regs->si, regs->di, regs->bp, sp);
148         printk(" DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
149                (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
150
151         if (!all)
152                 return;
153
154         cr0 = read_cr0();
155         cr2 = read_cr2();
156         cr3 = read_cr3();
157         cr4 = read_cr4_safe();
158         printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
159                         cr0, cr2, cr3, cr4);
160
161         get_debugreg(d0, 0);
162         get_debugreg(d1, 1);
163         get_debugreg(d2, 2);
164         get_debugreg(d3, 3);
165         printk("DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
166                         d0, d1, d2, d3);
167
168         get_debugreg(d6, 6);
169         get_debugreg(d7, 7);
170         printk("DR6: %08lx DR7: %08lx\n",
171                         d6, d7);
172 }
173
174 void show_regs(struct pt_regs *regs)
175 {
176         __show_registers(regs, 1);
177         show_trace(NULL, regs, &regs->sp, regs->bp);
178 }
179
180 /*
181  * This gets run with %bx containing the
182  * function to call, and %dx containing
183  * the "args".
184  */
185 extern void kernel_thread_helper(void);
186
187 /*
188  * Create a kernel thread
189  */
190 int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
191 {
192         struct pt_regs regs;
193
194         memset(&regs, 0, sizeof(regs));
195
196         regs.bx = (unsigned long) fn;
197         regs.dx = (unsigned long) arg;
198
199         regs.ds = __USER_DS;
200         regs.es = __USER_DS;
201         regs.fs = __KERNEL_PERCPU;
202         regs.orig_ax = -1;
203         regs.ip = (unsigned long) kernel_thread_helper;
204         regs.cs = __KERNEL_CS | get_kernel_rpl();
205         regs.flags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
206
207         /* Ok, create the new process.. */
208         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
209 }
210 EXPORT_SYMBOL(kernel_thread);
211
212 /*
213  * Free current thread data structures etc..
214  */
215 void exit_thread(void)
216 {
217         /* The process may have allocated an io port bitmap... nuke it. */
218         if (unlikely(test_thread_flag(TIF_IO_BITMAP))) {
219                 struct task_struct *tsk = current;
220                 struct thread_struct *t = &tsk->thread;
221                 int cpu = get_cpu();
222                 struct tss_struct *tss = &per_cpu(init_tss, cpu);
223
224                 kfree(t->io_bitmap_ptr);
225                 t->io_bitmap_ptr = NULL;
226                 clear_thread_flag(TIF_IO_BITMAP);
227                 /*
228                  * Careful, clear this in the TSS too:
229                  */
230                 memset(tss->io_bitmap, 0xff, tss->io_bitmap_max);
231                 t->io_bitmap_max = 0;
232                 tss->io_bitmap_owner = NULL;
233                 tss->io_bitmap_max = 0;
234                 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
235                 put_cpu();
236         }
237 }
238
239 void flush_thread(void)
240 {
241         struct task_struct *tsk = current;
242
243         tsk->thread.debugreg0 = 0;
244         tsk->thread.debugreg1 = 0;
245         tsk->thread.debugreg2 = 0;
246         tsk->thread.debugreg3 = 0;
247         tsk->thread.debugreg6 = 0;
248         tsk->thread.debugreg7 = 0;
249         memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));        
250         clear_tsk_thread_flag(tsk, TIF_DEBUG);
251         /*
252          * Forget coprocessor state..
253          */
254         tsk->fpu_counter = 0;
255         clear_fpu(tsk);
256         clear_used_math();
257 }
258
259 void release_thread(struct task_struct *dead_task)
260 {
261         BUG_ON(dead_task->mm);
262         release_vm86_irqs(dead_task);
263 }
264
265 /*
266  * This gets called before we allocate a new thread and copy
267  * the current task into it.
268  */
269 void prepare_to_copy(struct task_struct *tsk)
270 {
271         unlazy_fpu(tsk);
272 }
273
274 int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
275         unsigned long unused,
276         struct task_struct * p, struct pt_regs * regs)
277 {
278         struct pt_regs * childregs;
279         struct task_struct *tsk;
280         int err;
281
282         childregs = task_pt_regs(p);
283         *childregs = *regs;
284         childregs->ax = 0;
285         childregs->sp = sp;
286
287         p->thread.sp = (unsigned long) childregs;
288         p->thread.sp0 = (unsigned long) (childregs+1);
289
290         p->thread.ip = (unsigned long) ret_from_fork;
291
292         savesegment(gs, p->thread.gs);
293
294         tsk = current;
295         if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
296                 p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
297                                                 IO_BITMAP_BYTES, GFP_KERNEL);
298                 if (!p->thread.io_bitmap_ptr) {
299                         p->thread.io_bitmap_max = 0;
300                         return -ENOMEM;
301                 }
302                 set_tsk_thread_flag(p, TIF_IO_BITMAP);
303         }
304
305         err = 0;
306
307         /*
308          * Set a new TLS for the child thread?
309          */
310         if (clone_flags & CLONE_SETTLS)
311                 err = do_set_thread_area(p, -1,
312                         (struct user_desc __user *)childregs->si, 0);
313
314         if (err && p->thread.io_bitmap_ptr) {
315                 kfree(p->thread.io_bitmap_ptr);
316                 p->thread.io_bitmap_max = 0;
317         }
318         return err;
319 }
320
321 void
322 start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
323 {
324         __asm__("movl %0, %%gs" :: "r"(0));
325         regs->fs                = 0;
326         set_fs(USER_DS);
327         regs->ds                = __USER_DS;
328         regs->es                = __USER_DS;
329         regs->ss                = __USER_DS;
330         regs->cs                = __USER_CS;
331         regs->ip                = new_ip;
332         regs->sp                = new_sp;
333         /*
334          * Free the old FP and other extended state
335          */
336         free_thread_xstate(current);
337 }
338 EXPORT_SYMBOL_GPL(start_thread);
339
340 static void hard_disable_TSC(void)
341 {
342         write_cr4(read_cr4() | X86_CR4_TSD);
343 }
344
345 void disable_TSC(void)
346 {
347         preempt_disable();
348         if (!test_and_set_thread_flag(TIF_NOTSC))
349                 /*
350                  * Must flip the CPU state synchronously with
351                  * TIF_NOTSC in the current running context.
352                  */
353                 hard_disable_TSC();
354         preempt_enable();
355 }
356
357 static void hard_enable_TSC(void)
358 {
359         write_cr4(read_cr4() & ~X86_CR4_TSD);
360 }
361
362 static void enable_TSC(void)
363 {
364         preempt_disable();
365         if (test_and_clear_thread_flag(TIF_NOTSC))
366                 /*
367                  * Must flip the CPU state synchronously with
368                  * TIF_NOTSC in the current running context.
369                  */
370                 hard_enable_TSC();
371         preempt_enable();
372 }
373
374 int get_tsc_mode(unsigned long adr)
375 {
376         unsigned int val;
377
378         if (test_thread_flag(TIF_NOTSC))
379                 val = PR_TSC_SIGSEGV;
380         else
381                 val = PR_TSC_ENABLE;
382
383         return put_user(val, (unsigned int __user *)adr);
384 }
385
386 int set_tsc_mode(unsigned int val)
387 {
388         if (val == PR_TSC_SIGSEGV)
389                 disable_TSC();
390         else if (val == PR_TSC_ENABLE)
391                 enable_TSC();
392         else
393                 return -EINVAL;
394
395         return 0;
396 }
397
398 static noinline void
399 __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
400                  struct tss_struct *tss)
401 {
402         struct thread_struct *prev, *next;
403         unsigned long debugctl;
404
405         prev = &prev_p->thread;
406         next = &next_p->thread;
407
408         debugctl = prev->debugctlmsr;
409         if (next->ds_area_msr != prev->ds_area_msr) {
410                 /* we clear debugctl to make sure DS
411                  * is not in use when we change it */
412                 debugctl = 0;
413                 update_debugctlmsr(0);
414                 wrmsr(MSR_IA32_DS_AREA, next->ds_area_msr, 0);
415         }
416
417         if (next->debugctlmsr != debugctl)
418                 update_debugctlmsr(next->debugctlmsr);
419
420         if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
421                 set_debugreg(next->debugreg0, 0);
422                 set_debugreg(next->debugreg1, 1);
423                 set_debugreg(next->debugreg2, 2);
424                 set_debugreg(next->debugreg3, 3);
425                 /* no 4 and 5 */
426                 set_debugreg(next->debugreg6, 6);
427                 set_debugreg(next->debugreg7, 7);
428         }
429
430         if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
431             test_tsk_thread_flag(next_p, TIF_NOTSC)) {
432                 /* prev and next are different */
433                 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
434                         hard_disable_TSC();
435                 else
436                         hard_enable_TSC();
437         }
438
439 #ifdef X86_BTS
440         if (test_tsk_thread_flag(prev_p, TIF_BTS_TRACE_TS))
441                 ptrace_bts_take_timestamp(prev_p, BTS_TASK_DEPARTS);
442
443         if (test_tsk_thread_flag(next_p, TIF_BTS_TRACE_TS))
444                 ptrace_bts_take_timestamp(next_p, BTS_TASK_ARRIVES);
445 #endif
446
447
448         if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
449                 /*
450                  * Disable the bitmap via an invalid offset. We still cache
451                  * the previous bitmap owner and the IO bitmap contents:
452                  */
453                 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
454                 return;
455         }
456
457         if (likely(next == tss->io_bitmap_owner)) {
458                 /*
459                  * Previous owner of the bitmap (hence the bitmap content)
460                  * matches the next task, we dont have to do anything but
461                  * to set a valid offset in the TSS:
462                  */
463                 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
464                 return;
465         }
466         /*
467          * Lazy TSS's I/O bitmap copy. We set an invalid offset here
468          * and we let the task to get a GPF in case an I/O instruction
469          * is performed.  The handler of the GPF will verify that the
470          * faulting task has a valid I/O bitmap and, it true, does the
471          * real copy and restart the instruction.  This will save us
472          * redundant copies when the currently switched task does not
473          * perform any I/O during its timeslice.
474          */
475         tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
476 }
477
478 /*
479  *      switch_to(x,yn) should switch tasks from x to y.
480  *
481  * We fsave/fwait so that an exception goes off at the right time
482  * (as a call from the fsave or fwait in effect) rather than to
483  * the wrong process. Lazy FP saving no longer makes any sense
484  * with modern CPU's, and this simplifies a lot of things (SMP
485  * and UP become the same).
486  *
487  * NOTE! We used to use the x86 hardware context switching. The
488  * reason for not using it any more becomes apparent when you
489  * try to recover gracefully from saved state that is no longer
490  * valid (stale segment register values in particular). With the
491  * hardware task-switch, there is no way to fix up bad state in
492  * a reasonable manner.
493  *
494  * The fact that Intel documents the hardware task-switching to
495  * be slow is a fairly red herring - this code is not noticeably
496  * faster. However, there _is_ some room for improvement here,
497  * so the performance issues may eventually be a valid point.
498  * More important, however, is the fact that this allows us much
499  * more flexibility.
500  *
501  * The return value (in %ax) will be the "prev" task after
502  * the task-switch, and shows up in ret_from_fork in entry.S,
503  * for example.
504  */
505 struct task_struct * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
506 {
507         struct thread_struct *prev = &prev_p->thread,
508                                  *next = &next_p->thread;
509         int cpu = smp_processor_id();
510         struct tss_struct *tss = &per_cpu(init_tss, cpu);
511
512         /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
513
514         __unlazy_fpu(prev_p);
515
516
517         /* we're going to use this soon, after a few expensive things */
518         if (next_p->fpu_counter > 5)
519                 prefetch(next->xstate);
520
521         /*
522          * Reload esp0.
523          */
524         load_sp0(tss, next);
525
526         /*
527          * Save away %gs. No need to save %fs, as it was saved on the
528          * stack on entry.  No need to save %es and %ds, as those are
529          * always kernel segments while inside the kernel.  Doing this
530          * before setting the new TLS descriptors avoids the situation
531          * where we temporarily have non-reloadable segments in %fs
532          * and %gs.  This could be an issue if the NMI handler ever
533          * used %fs or %gs (it does not today), or if the kernel is
534          * running inside of a hypervisor layer.
535          */
536         savesegment(gs, prev->gs);
537
538         /*
539          * Load the per-thread Thread-Local Storage descriptor.
540          */
541         load_TLS(next, cpu);
542
543         /*
544          * Restore IOPL if needed.  In normal use, the flags restore
545          * in the switch assembly will handle this.  But if the kernel
546          * is running virtualized at a non-zero CPL, the popf will
547          * not restore flags, so it must be done in a separate step.
548          */
549         if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
550                 set_iopl_mask(next->iopl);
551
552         /*
553          * Now maybe handle debug registers and/or IO bitmaps
554          */
555         if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
556                      task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
557                 __switch_to_xtra(prev_p, next_p, tss);
558
559         /*
560          * Leave lazy mode, flushing any hypercalls made here.
561          * This must be done before restoring TLS segments so
562          * the GDT and LDT are properly updated, and must be
563          * done before math_state_restore, so the TS bit is up
564          * to date.
565          */
566         arch_leave_lazy_cpu_mode();
567
568         /* If the task has used fpu the last 5 timeslices, just do a full
569          * restore of the math state immediately to avoid the trap; the
570          * chances of needing FPU soon are obviously high now
571          *
572          * tsk_used_math() checks prevent calling math_state_restore(),
573          * which can sleep in the case of !tsk_used_math()
574          */
575         if (tsk_used_math(next_p) && next_p->fpu_counter > 5)
576                 math_state_restore();
577
578         /*
579          * Restore %gs if needed (which is common)
580          */
581         if (prev->gs | next->gs)
582                 loadsegment(gs, next->gs);
583
584         x86_write_percpu(current_task, next_p);
585
586         return prev_p;
587 }
588
589 asmlinkage int sys_fork(struct pt_regs regs)
590 {
591         return do_fork(SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
592 }
593
594 asmlinkage int sys_clone(struct pt_regs regs)
595 {
596         unsigned long clone_flags;
597         unsigned long newsp;
598         int __user *parent_tidptr, *child_tidptr;
599
600         clone_flags = regs.bx;
601         newsp = regs.cx;
602         parent_tidptr = (int __user *)regs.dx;
603         child_tidptr = (int __user *)regs.di;
604         if (!newsp)
605                 newsp = regs.sp;
606         return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
607 }
608
609 /*
610  * This is trivial, and on the face of it looks like it
611  * could equally well be done in user mode.
612  *
613  * Not so, for quite unobvious reasons - register pressure.
614  * In user mode vfork() cannot have a stack frame, and if
615  * done by calling the "clone()" system call directly, you
616  * do not have enough call-clobbered registers to hold all
617  * the information you need.
618  */
619 asmlinkage int sys_vfork(struct pt_regs regs)
620 {
621         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
622 }
623
624 /*
625  * sys_execve() executes a new program.
626  */
627 asmlinkage int sys_execve(struct pt_regs regs)
628 {
629         int error;
630         char * filename;
631
632         filename = getname((char __user *) regs.bx);
633         error = PTR_ERR(filename);
634         if (IS_ERR(filename))
635                 goto out;
636         error = do_execve(filename,
637                         (char __user * __user *) regs.cx,
638                         (char __user * __user *) regs.dx,
639                         &regs);
640         if (error == 0) {
641                 /* Make sure we don't return using sysenter.. */
642                 set_thread_flag(TIF_IRET);
643         }
644         putname(filename);
645 out:
646         return error;
647 }
648
649 #define top_esp                (THREAD_SIZE - sizeof(unsigned long))
650 #define top_ebp                (THREAD_SIZE - 2*sizeof(unsigned long))
651
652 unsigned long get_wchan(struct task_struct *p)
653 {
654         unsigned long bp, sp, ip;
655         unsigned long stack_page;
656         int count = 0;
657         if (!p || p == current || p->state == TASK_RUNNING)
658                 return 0;
659         stack_page = (unsigned long)task_stack_page(p);
660         sp = p->thread.sp;
661         if (!stack_page || sp < stack_page || sp > top_esp+stack_page)
662                 return 0;
663         /* include/asm-i386/system.h:switch_to() pushes bp last. */
664         bp = *(unsigned long *) sp;
665         do {
666                 if (bp < stack_page || bp > top_ebp+stack_page)
667                         return 0;
668                 ip = *(unsigned long *) (bp+4);
669                 if (!in_sched_functions(ip))
670                         return ip;
671                 bp = *(unsigned long *) bp;
672         } while (count++ < 16);
673         return 0;
674 }
675
676 unsigned long arch_align_stack(unsigned long sp)
677 {
678         if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
679                 sp -= get_random_int() % 8192;
680         return sp & ~0xf;
681 }
682
683 unsigned long arch_randomize_brk(struct mm_struct *mm)
684 {
685         unsigned long range_end = mm->brk + 0x02000000;
686         return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
687 }