blob: 3ba155d24884dd812790ff0e5aef8bb6767100d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Zwane Mwaikambof3705132005-06-25 14:54:50 -070014#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/stddef.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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>
Andi Kleenc16b63e2006-09-26 10:52:28 +020036#include <linux/personality.h>
Ingo Molnar74167342007-02-16 01:28:07 -080037#include <linux/tick.h>
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +020038#include <linux/percpu.h>
Erik Bosman529e25f2008-04-14 00:24:18 +020039#include <linux/prctl.h>
Arjan van de Ven90f7d252008-09-16 11:27:30 -070040#include <linux/dmi.h>
Frederic Weisbecker8b96f012008-12-06 03:40:00 +010041#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/uaccess.h>
44#include <asm/pgtable.h>
45#include <asm/system.h>
46#include <asm/io.h>
47#include <asm/ldt.h>
48#include <asm/processor.h>
49#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/desc.h>
51#ifdef CONFIG_MATH_EMULATION
52#include <asm/math_emu.h>
53#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/err.h>
56
Zwane Mwaikambof3705132005-06-25 14:54:50 -070057#include <asm/tlbflush.h>
58#include <asm/cpu.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010059#include <asm/kdebug.h>
Marc Dionne1eda8142008-09-23 22:40:02 -040060#include <asm/idle.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053061#include <asm/syscalls.h>
Jaswinder Singhfb261322008-07-21 21:36:40 +053062#include <asm/smp.h>
Markus Metzgerbf53de92008-12-19 15:10:24 +010063#include <asm/ds.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
66
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +020067DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
68EXPORT_PER_CPU_SYMBOL(current_task);
69
70DEFINE_PER_CPU(int, cpu_number);
71EXPORT_PER_CPU_SYMBOL(cpu_number);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
74 * Return saved PC of a blocked thread.
75 */
76unsigned long thread_saved_pc(struct task_struct *tsk)
77{
H. Peter Anvinfaca6222008-01-30 13:31:02 +010078 return ((unsigned long *)tsk->thread.sp)[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Alex Nixon913da642008-09-03 14:30:23 +010081#ifndef CONFIG_SMP
82static inline void play_dead(void)
83{
84 BUG();
85}
86#endif
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * The idle thread. There's no useful work to be
90 * done, so just try to conserve power and have a
91 * low exit latency (ie sit in a loop waiting for
92 * somebody to say that they'd like to reschedule)
93 */
Zwane Mwaikambof3705132005-06-25 14:54:50 -070094void cpu_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Nick Piggin5bfb5d62005-11-08 21:39:01 -080096 int cpu = smp_processor_id();
Zwane Mwaikambof3705132005-06-25 14:54:50 -070097
Andi Kleen495ab9c2006-06-26 13:59:11 +020098 current_thread_info()->status |= TS_POLLING;
Nick Piggin64c7c8f2005-11-08 21:39:04 -080099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* endless idle loop with no priority at all */
101 while (1) {
Thomas Gleixnerb8f8c3c2008-07-18 17:27:28 +0200102 tick_nohz_stop_sched_tick(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 while (!need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Christoph Lameterf1d1a842007-05-12 11:15:24 -0700105 check_pgt_cache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Benjamin LaHaise0723a692008-01-30 13:33:13 +0100108 if (rcu_pending(cpu))
109 rcu_check_callbacks(cpu, 0);
110
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700111 if (cpu_is_offline(cpu))
112 play_dead();
113
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200114 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 __get_cpu_var(irq_stat).idle_timestamp = jiffies;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200116 /* Don't trace irqs off for idle */
117 stop_critical_timings();
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200118 pm_idle();
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200119 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Ingo Molnar74167342007-02-16 01:28:07 -0800121 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800122 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 schedule();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800124 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126}
127
Pekka Enberge2ce07c2008-04-03 16:40:48 +0300128void __show_regs(struct pt_regs *regs, int all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
Alan Sternbb1995d2007-07-21 17:10:42 +0200131 unsigned long d0, d1, d2, d3, d6, d7;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100132 unsigned long sp;
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200133 unsigned short ss, gs;
Arjan van de Ven90f7d252008-09-16 11:27:30 -0700134 const char *board;
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200135
136 if (user_mode_vm(regs)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100137 sp = regs->sp;
138 ss = regs->ss & 0xffff;
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200139 savesegment(gs, gs);
140 } else {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100141 sp = (unsigned long) (&regs->sp);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200142 savesegment(ss, ss);
143 savesegment(gs, gs);
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 printk("\n");
Arjan van de Ven90f7d252008-09-16 11:27:30 -0700147
148 board = dmi_get_system_info(DMI_PRODUCT_NAME);
149 if (!board)
150 board = "";
151 printk("Pid: %d, comm: %s %s (%s %.*s) %s\n",
Linus Torvalds60812a42007-10-19 15:06:00 -0700152 task_pid_nr(current), current->comm,
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200153 print_tainted(), init_utsname()->release,
154 (int)strcspn(init_utsname()->version, " "),
Arjan van de Ven90f7d252008-09-16 11:27:30 -0700155 init_utsname()->version, board);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200156
157 printk("EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
Harvey Harrison92bc2052008-02-08 12:09:56 -0800158 (u16)regs->cs, regs->ip, regs->flags,
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200159 smp_processor_id());
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100160 print_symbol("EIP is at %s\n", regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100163 regs->ax, regs->bx, regs->cx, regs->dx);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200164 printk("ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100165 regs->si, regs->di, regs->bp, sp);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200166 printk(" DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
Harvey Harrison92bc2052008-02-08 12:09:56 -0800167 (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200168
169 if (!all)
170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700172 cr0 = read_cr0();
173 cr2 = read_cr2();
174 cr3 = read_cr3();
Zachary Amsdenff6e8c02006-01-06 00:11:50 -0800175 cr4 = read_cr4_safe();
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200176 printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
177 cr0, cr2, cr3, cr4);
Alan Sternbb1995d2007-07-21 17:10:42 +0200178
179 get_debugreg(d0, 0);
180 get_debugreg(d1, 1);
181 get_debugreg(d2, 2);
182 get_debugreg(d3, 3);
183 printk("DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
184 d0, d1, d2, d3);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200185
Alan Sternbb1995d2007-07-21 17:10:42 +0200186 get_debugreg(d6, 6);
187 get_debugreg(d7, 7);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200188 printk("DR6: %08lx DR7: %08lx\n",
189 d6, d7);
190}
Alan Sternbb1995d2007-07-21 17:10:42 +0200191
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200192void show_regs(struct pt_regs *regs)
193{
Pekka Enberge2ce07c2008-04-03 16:40:48 +0300194 __show_regs(regs, 1);
Arjan van de Ven5bc27dc2008-01-30 13:33:07 +0100195 show_trace(NULL, regs, &regs->sp, regs->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100199 * This gets run with %bx containing the
200 * function to call, and %dx containing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * the "args".
202 */
203extern void kernel_thread_helper(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205/*
206 * Create a kernel thread
207 */
208int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
209{
210 struct pt_regs regs;
211
212 memset(&regs, 0, sizeof(regs));
213
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100214 regs.bx = (unsigned long) fn;
215 regs.dx = (unsigned long) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100217 regs.ds = __USER_DS;
218 regs.es = __USER_DS;
219 regs.fs = __KERNEL_PERCPU;
220 regs.orig_ax = -1;
221 regs.ip = (unsigned long) kernel_thread_helper;
222 regs.cs = __KERNEL_CS | get_kernel_rpl();
223 regs.flags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* Ok, create the new process.. */
Andi Kleen8cf2c512006-10-21 18:37:02 +0200226 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700228EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230/*
231 * Free current thread data structures etc..
232 */
233void exit_thread(void)
234{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* The process may have allocated an io port bitmap... nuke it. */
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400236 if (unlikely(test_thread_flag(TIF_IO_BITMAP))) {
237 struct task_struct *tsk = current;
238 struct thread_struct *t = &tsk->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int cpu = get_cpu();
240 struct tss_struct *tss = &per_cpu(init_tss, cpu);
241
242 kfree(t->io_bitmap_ptr);
243 t->io_bitmap_ptr = NULL;
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400244 clear_thread_flag(TIF_IO_BITMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /*
246 * Careful, clear this in the TSS too:
247 */
248 memset(tss->io_bitmap, 0xff, tss->io_bitmap_max);
249 t->io_bitmap_max = 0;
250 tss->io_bitmap_owner = NULL;
251 tss->io_bitmap_max = 0;
Rusty Russella75c54f2007-05-02 19:27:13 +0200252 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 put_cpu();
254 }
Markus Metzgerc2724772008-12-11 13:49:59 +0100255
Markus Metzgerbf53de92008-12-19 15:10:24 +0100256 ds_exit_thread(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259void flush_thread(void)
260{
261 struct task_struct *tsk = current;
262
Roland McGrath0f534092008-01-30 13:30:59 +0100263 tsk->thread.debugreg0 = 0;
264 tsk->thread.debugreg1 = 0;
265 tsk->thread.debugreg2 = 0;
266 tsk->thread.debugreg3 = 0;
267 tsk->thread.debugreg6 = 0;
268 tsk->thread.debugreg7 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400270 clear_tsk_thread_flag(tsk, TIF_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /*
272 * Forget coprocessor state..
273 */
Suresh Siddha75118a82008-06-13 15:47:12 -0700274 tsk->fpu_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 clear_fpu(tsk);
276 clear_used_math();
277}
278
279void release_thread(struct task_struct *dead_task)
280{
Zachary Amsden26849272006-01-06 00:11:59 -0800281 BUG_ON(dead_task->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 release_vm86_irqs(dead_task);
283}
284
285/*
286 * This gets called before we allocate a new thread and copy
287 * the current task into it.
288 */
289void prepare_to_copy(struct task_struct *tsk)
290{
291 unlazy_fpu(tsk);
292}
293
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100294int copy_thread(int nr, unsigned long clone_flags, unsigned long sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned long unused,
296 struct task_struct * p, struct pt_regs * regs)
297{
298 struct pt_regs * childregs;
299 struct task_struct *tsk;
300 int err;
301
akpm@osdl.org07b047f2006-01-12 01:05:41 -0800302 childregs = task_pt_regs(p);
Alexander Nybergf48d9662005-05-05 16:15:03 -0700303 *childregs = *regs;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100304 childregs->ax = 0;
305 childregs->sp = sp;
Alexander Nybergf48d9662005-05-05 16:15:03 -0700306
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100307 p->thread.sp = (unsigned long) childregs;
308 p->thread.sp0 = (unsigned long) (childregs+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100310 p->thread.ip = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100312 savesegment(gs, p->thread.gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 tsk = current;
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400315 if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700316 p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
317 IO_BITMAP_BYTES, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!p->thread.io_bitmap_ptr) {
319 p->thread.io_bitmap_max = 0;
320 return -ENOMEM;
321 }
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400322 set_tsk_thread_flag(p, TIF_IO_BITMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Roland McGrathefd1ca52008-01-30 13:30:46 +0100325 err = 0;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /*
328 * Set a new TLS for the child thread?
329 */
Roland McGrathefd1ca52008-01-30 13:30:46 +0100330 if (clone_flags & CLONE_SETTLS)
331 err = do_set_thread_area(p, -1,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100332 (struct user_desc __user *)childregs->si, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (err && p->thread.io_bitmap_ptr) {
335 kfree(p->thread.io_bitmap_ptr);
336 p->thread.io_bitmap_max = 0;
337 }
Markus Metzgerbf53de92008-12-19 15:10:24 +0100338
339 ds_copy_thread(p, current);
340
341 clear_tsk_thread_flag(p, TIF_DEBUGCTLMSR);
342 p->thread.debugctlmsr = 0;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return err;
345}
346
Ingo Molnar513ad842008-02-21 05:18:40 +0100347void
348start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
349{
350 __asm__("movl %0, %%gs" :: "r"(0));
351 regs->fs = 0;
352 set_fs(USER_DS);
353 regs->ds = __USER_DS;
354 regs->es = __USER_DS;
355 regs->ss = __USER_DS;
356 regs->cs = __USER_CS;
357 regs->ip = new_ip;
358 regs->sp = new_sp;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700359 /*
360 * Free the old FP and other extended state
361 */
362 free_thread_xstate(current);
Ingo Molnar513ad842008-02-21 05:18:40 +0100363}
364EXPORT_SYMBOL_GPL(start_thread);
365
Jan Beulichbdb4f152008-01-30 13:31:21 +0100366static void hard_disable_TSC(void)
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700367{
368 write_cr4(read_cr4() | X86_CR4_TSD);
369}
Erik Bosman529e25f2008-04-14 00:24:18 +0200370
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700371void disable_TSC(void)
372{
373 preempt_disable();
374 if (!test_and_set_thread_flag(TIF_NOTSC))
375 /*
376 * Must flip the CPU state synchronously with
377 * TIF_NOTSC in the current running context.
378 */
379 hard_disable_TSC();
380 preempt_enable();
381}
Erik Bosman529e25f2008-04-14 00:24:18 +0200382
Jan Beulichbdb4f152008-01-30 13:31:21 +0100383static void hard_enable_TSC(void)
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700384{
385 write_cr4(read_cr4() & ~X86_CR4_TSD);
386}
Erik Bosman529e25f2008-04-14 00:24:18 +0200387
Ingo Molnara4928cf2008-04-23 13:20:56 +0200388static void enable_TSC(void)
Erik Bosman529e25f2008-04-14 00:24:18 +0200389{
390 preempt_disable();
391 if (test_and_clear_thread_flag(TIF_NOTSC))
392 /*
393 * Must flip the CPU state synchronously with
394 * TIF_NOTSC in the current running context.
395 */
396 hard_enable_TSC();
397 preempt_enable();
398}
399
400int get_tsc_mode(unsigned long adr)
401{
402 unsigned int val;
403
404 if (test_thread_flag(TIF_NOTSC))
405 val = PR_TSC_SIGSEGV;
406 else
407 val = PR_TSC_ENABLE;
408
409 return put_user(val, (unsigned int __user *)adr);
410}
411
412int set_tsc_mode(unsigned int val)
413{
414 if (val == PR_TSC_SIGSEGV)
415 disable_TSC();
416 else if (val == PR_TSC_ENABLE)
417 enable_TSC();
418 else
419 return -EINVAL;
420
421 return 0;
422}
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700423
424static noinline void
425__switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
426 struct tss_struct *tss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Roland McGrath7e991602008-01-30 13:30:54 +0100428 struct thread_struct *prev, *next;
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400429
Roland McGrath7e991602008-01-30 13:30:54 +0100430 prev = &prev_p->thread;
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400431 next = &next_p->thread;
432
Markus Metzgerc2724772008-12-11 13:49:59 +0100433 if (test_tsk_thread_flag(next_p, TIF_DS_AREA_MSR) ||
434 test_tsk_thread_flag(prev_p, TIF_DS_AREA_MSR))
435 ds_switch_to(prev_p, next_p);
436 else if (next->debugctlmsr != prev->debugctlmsr)
Jan Beulich5b0e5082008-03-10 13:11:17 +0000437 update_debugctlmsr(next->debugctlmsr);
Roland McGrath7e991602008-01-30 13:30:54 +0100438
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400439 if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
Roland McGrath0f534092008-01-30 13:30:59 +0100440 set_debugreg(next->debugreg0, 0);
441 set_debugreg(next->debugreg1, 1);
442 set_debugreg(next->debugreg2, 2);
443 set_debugreg(next->debugreg3, 3);
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400444 /* no 4 and 5 */
Roland McGrath0f534092008-01-30 13:30:59 +0100445 set_debugreg(next->debugreg6, 6);
446 set_debugreg(next->debugreg7, 7);
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400447 }
448
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700449 if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
450 test_tsk_thread_flag(next_p, TIF_NOTSC)) {
451 /* prev and next are different */
452 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
453 hard_disable_TSC();
454 else
455 hard_enable_TSC();
456 }
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700457
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400458 if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /*
460 * Disable the bitmap via an invalid offset. We still cache
461 * the previous bitmap owner and the IO bitmap contents:
462 */
Rusty Russella75c54f2007-05-02 19:27:13 +0200463 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return;
465 }
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (likely(next == tss->io_bitmap_owner)) {
468 /*
469 * Previous owner of the bitmap (hence the bitmap content)
470 * matches the next task, we dont have to do anything but
471 * to set a valid offset in the TSS:
472 */
Rusty Russella75c54f2007-05-02 19:27:13 +0200473 tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return;
475 }
476 /*
477 * Lazy TSS's I/O bitmap copy. We set an invalid offset here
478 * and we let the task to get a GPF in case an I/O instruction
479 * is performed. The handler of the GPF will verify that the
480 * faulting task has a valid I/O bitmap and, it true, does the
481 * real copy and restart the instruction. This will save us
482 * redundant copies when the currently switched task does not
483 * perform any I/O during its timeslice.
484 */
Rusty Russella75c54f2007-05-02 19:27:13 +0200485 tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488/*
489 * switch_to(x,yn) should switch tasks from x to y.
490 *
491 * We fsave/fwait so that an exception goes off at the right time
492 * (as a call from the fsave or fwait in effect) rather than to
493 * the wrong process. Lazy FP saving no longer makes any sense
494 * with modern CPU's, and this simplifies a lot of things (SMP
495 * and UP become the same).
496 *
497 * NOTE! We used to use the x86 hardware context switching. The
498 * reason for not using it any more becomes apparent when you
499 * try to recover gracefully from saved state that is no longer
500 * valid (stale segment register values in particular). With the
501 * hardware task-switch, there is no way to fix up bad state in
502 * a reasonable manner.
503 *
504 * The fact that Intel documents the hardware task-switching to
505 * be slow is a fairly red herring - this code is not noticeably
506 * faster. However, there _is_ some room for improvement here,
507 * so the performance issues may eventually be a valid point.
508 * More important, however, is the fact that this allows us much
509 * more flexibility.
510 *
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100511 * The return value (in %ax) will be the "prev" task after
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * the task-switch, and shows up in ret_from_fork in entry.S,
513 * for example.
514 */
Frederic Weisbecker8b96f012008-12-06 03:40:00 +0100515__notrace_funcgraph struct task_struct *
516__switch_to(struct task_struct *prev_p, struct task_struct *next_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct thread_struct *prev = &prev_p->thread,
519 *next = &next_p->thread;
520 int cpu = smp_processor_id();
521 struct tss_struct *tss = &per_cpu(init_tss, cpu);
522
523 /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
524
525 __unlazy_fpu(prev_p);
526
Chuck Ebbertacc20762006-12-07 02:14:01 +0100527
528 /* we're going to use this soon, after a few expensive things */
529 if (next_p->fpu_counter > 5)
Suresh Siddha61c46282008-03-10 15:28:04 -0700530 prefetch(next->xstate);
Chuck Ebbertacc20762006-12-07 02:14:01 +0100531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700533 * Reload esp0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100535 load_sp0(tss, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /*
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100538 * Save away %gs. No need to save %fs, as it was saved on the
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100539 * stack on entry. No need to save %es and %ds, as those are
540 * always kernel segments while inside the kernel. Doing this
541 * before setting the new TLS descriptors avoids the situation
542 * where we temporarily have non-reloadable segments in %fs
543 * and %gs. This could be an issue if the NMI handler ever
544 * used %fs or %gs (it does not today), or if the kernel is
545 * running inside of a hypervisor layer.
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700546 */
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100547 savesegment(gs, prev->gs);
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700548
549 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * Load the per-thread Thread-Local Storage descriptor.
551 */
552 load_TLS(next, cpu);
553
554 /*
Zachary Amsden8b151142007-02-13 13:26:21 +0100555 * Restore IOPL if needed. In normal use, the flags restore
556 * in the switch assembly will handle this. But if the kernel
557 * is running virtualized at a non-zero CPL, the popf will
558 * not restore flags, so it must be done in a separate step.
559 */
560 if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
561 set_iopl_mask(next->iopl);
562
563 /*
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400564 * Now maybe handle debug registers and/or IO bitmaps
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700566 if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
567 task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
568 __switch_to_xtra(prev_p, next_p, tss);
Andrea Arcangeliffaa8bd2005-06-27 14:36:36 -0700569
Zachary Amsden9226d122007-02-13 13:26:21 +0100570 /*
571 * Leave lazy mode, flushing any hypercalls made here.
572 * This must be done before restoring TLS segments so
573 * the GDT and LDT are properly updated, and must be
574 * done before math_state_restore, so the TS bit is up
575 * to date.
576 */
577 arch_leave_lazy_cpu_mode();
578
Chuck Ebbertacc20762006-12-07 02:14:01 +0100579 /* If the task has used fpu the last 5 timeslices, just do a full
580 * restore of the math state immediately to avoid the trap; the
581 * chances of needing FPU soon are obviously high now
Suresh Siddha870568b2008-06-02 15:57:27 -0700582 *
583 * tsk_used_math() checks prevent calling math_state_restore(),
584 * which can sleep in the case of !tsk_used_math()
Chuck Ebbertacc20762006-12-07 02:14:01 +0100585 */
Suresh Siddha870568b2008-06-02 15:57:27 -0700586 if (tsk_used_math(next_p) && next_p->fpu_counter > 5)
Chuck Ebbertacc20762006-12-07 02:14:01 +0100587 math_state_restore();
588
Zachary Amsden9226d122007-02-13 13:26:21 +0100589 /*
590 * Restore %gs if needed (which is common)
591 */
592 if (prev->gs | next->gs)
593 loadsegment(gs, next->gs);
594
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200595 x86_write_percpu(current_task, next_p);
Zachary Amsden9226d122007-02-13 13:26:21 +0100596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return prev_p;
598}
599
600asmlinkage int sys_fork(struct pt_regs regs)
601{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100602 return do_fork(SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605asmlinkage int sys_clone(struct pt_regs regs)
606{
607 unsigned long clone_flags;
608 unsigned long newsp;
609 int __user *parent_tidptr, *child_tidptr;
610
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100611 clone_flags = regs.bx;
612 newsp = regs.cx;
613 parent_tidptr = (int __user *)regs.dx;
614 child_tidptr = (int __user *)regs.di;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!newsp)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100616 newsp = regs.sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
618}
619
620/*
621 * This is trivial, and on the face of it looks like it
622 * could equally well be done in user mode.
623 *
624 * Not so, for quite unobvious reasons - register pressure.
625 * In user mode vfork() cannot have a stack frame, and if
626 * done by calling the "clone()" system call directly, you
627 * do not have enough call-clobbered registers to hold all
628 * the information you need.
629 */
630asmlinkage int sys_vfork(struct pt_regs regs)
631{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100632 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.sp, &regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635/*
636 * sys_execve() executes a new program.
637 */
638asmlinkage int sys_execve(struct pt_regs regs)
639{
640 int error;
641 char * filename;
642
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100643 filename = getname((char __user *) regs.bx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 error = PTR_ERR(filename);
645 if (IS_ERR(filename))
646 goto out;
647 error = do_execve(filename,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100648 (char __user * __user *) regs.cx,
649 (char __user * __user *) regs.dx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 &regs);
651 if (error == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* Make sure we don't return using sysenter.. */
653 set_thread_flag(TIF_IRET);
654 }
655 putname(filename);
656out:
657 return error;
658}
659
660#define top_esp (THREAD_SIZE - sizeof(unsigned long))
661#define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
662
663unsigned long get_wchan(struct task_struct *p)
664{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100665 unsigned long bp, sp, ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 unsigned long stack_page;
667 int count = 0;
668 if (!p || p == current || p->state == TASK_RUNNING)
669 return 0;
Al Viro65e0fdf2006-01-12 01:05:41 -0800670 stack_page = (unsigned long)task_stack_page(p);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100671 sp = p->thread.sp;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100672 if (!stack_page || sp < stack_page || sp > top_esp+stack_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100674 /* include/asm-i386/system.h:switch_to() pushes bp last. */
675 bp = *(unsigned long *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 do {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100677 if (bp < stack_page || bp > top_ebp+stack_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100679 ip = *(unsigned long *) (bp+4);
680 if (!in_sched_functions(ip))
681 return ip;
682 bp = *(unsigned long *) bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 } while (count++ < 16);
684 return 0;
685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687unsigned long arch_align_stack(unsigned long sp)
688{
Andi Kleenc16b63e2006-09-26 10:52:28 +0200689 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 sp -= get_random_int() % 8192;
691 return sp & ~0xf;
692}
Jiri Kosinac1d171a2008-01-30 13:30:40 +0100693
694unsigned long arch_randomize_brk(struct mm_struct *mm)
695{
696 unsigned long range_end = mm->brk + 0x02000000;
697 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
698}