blob: 297ffff2ffc2c88aaf5c1e08f8d18315d9eeea7a [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
Tejun Heo5c79d2a2009-02-11 16:31:00 +090014#include <linux/stackprotector.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070015#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/sched.h>
18#include <linux/fs.h>
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/elfcore.h>
22#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/stddef.h>
24#include <linux/slab.h>
25#include <linux/vmalloc.h>
26#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/utsname.h>
29#include <linux/delay.h>
30#include <linux/reboot.h>
31#include <linux/init.h>
32#include <linux/mc146818rtc.h>
33#include <linux/module.h>
34#include <linux/kallsyms.h>
35#include <linux/ptrace.h>
36#include <linux/random.h>
Andi Kleenc16b63e02006-09-26 10:52:28 +020037#include <linux/personality.h>
Ingo Molnar74167342007-02-16 01:28:07 -080038#include <linux/tick.h>
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +020039#include <linux/percpu.h>
Erik Bosman529e25f2008-04-14 00:24:18 +020040#include <linux/prctl.h>
Arjan van de Ven90f7d252008-09-16 11:27:30 -070041#include <linux/dmi.h>
Frederic Weisbecker8b96f012008-12-06 03:40:00 +010042#include <linux/ftrace.h>
Jaswinder Singh Rajputbefa9e72009-01-04 16:18:56 +053043#include <linux/uaccess.h>
44#include <linux/io.h>
45#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/pgtable.h>
48#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/ldt.h>
50#include <asm/processor.h>
51#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/desc.h>
53#ifdef CONFIG_MATH_EMULATION
54#include <asm/math_emu.h>
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/err.h>
58
Zwane Mwaikambof3705132005-06-25 14:54:50 -070059#include <asm/tlbflush.h>
60#include <asm/cpu.h>
Marc Dionne1eda8142008-09-23 22:40:02 -040061#include <asm/idle.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053062#include <asm/syscalls.h>
Markus Metzgerbf53de92008-12-19 15:10:24 +010063#include <asm/ds.h>
K.Prasad66cb5912009-06-01 23:44:55 +053064#include <asm/debugreg.h>
65#include <asm/hw_breakpoint.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
68
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +020069DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
70EXPORT_PER_CPU_SYMBOL(current_task);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * Return saved PC of a blocked thread.
74 */
75unsigned long thread_saved_pc(struct task_struct *tsk)
76{
H. Peter Anvinfaca6222008-01-30 13:31:02 +010077 return ((unsigned long *)tsk->thread.sp)[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Alex Nixon913da642008-09-03 14:30:23 +010080#ifndef CONFIG_SMP
81static inline void play_dead(void)
82{
83 BUG();
84}
85#endif
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * The idle thread. There's no useful work to be
89 * done, so just try to conserve power and have a
90 * low exit latency (ie sit in a loop waiting for
91 * somebody to say that they'd like to reschedule)
92 */
Zwane Mwaikambof3705132005-06-25 14:54:50 -070093void cpu_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Nick Piggin5bfb5d62005-11-08 21:39:01 -080095 int cpu = smp_processor_id();
Zwane Mwaikambof3705132005-06-25 14:54:50 -070096
Tejun Heo5c79d2a2009-02-11 16:31:00 +090097 /*
98 * If we're the non-boot CPU, nothing set the stack canary up
99 * for us. CPU0 already has it initialized but no harm in
100 * doing it again. This is a good place for updating it, as
101 * we wont ever return from this function (so the invalid
102 * canaries already on the stack wont ever trigger).
103 */
104 boot_init_stack_canary();
105
Andi Kleen495ab9c2006-06-26 13:59:11 +0200106 current_thread_info()->status |= TS_POLLING;
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* endless idle loop with no priority at all */
109 while (1) {
Thomas Gleixnerb8f8c3c2008-07-18 17:27:28 +0200110 tick_nohz_stop_sched_tick(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 while (!need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Lameterf1d1a842007-05-12 11:15:24 -0700113 check_pgt_cache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700116 if (cpu_is_offline(cpu))
117 play_dead();
118
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200119 local_irq_disable();
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200120 /* Don't trace irqs off for idle */
121 stop_critical_timings();
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200122 pm_idle();
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200123 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Ingo Molnar74167342007-02-16 01:28:07 -0800125 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800126 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 schedule();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800128 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130}
131
Pekka Enberge2ce07c2008-04-03 16:40:48 +0300132void __show_regs(struct pt_regs *regs, int all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
Alan Sternbb1995d2007-07-21 17:10:42 +0200135 unsigned long d0, d1, d2, d3, d6, d7;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100136 unsigned long sp;
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200137 unsigned short ss, gs;
Arjan van de Ven90f7d252008-09-16 11:27:30 -0700138 const char *board;
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200139
140 if (user_mode_vm(regs)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100141 sp = regs->sp;
142 ss = regs->ss & 0xffff;
Tejun Heod9a89a22009-02-09 22:17:40 +0900143 gs = get_user_gs(regs);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200144 } else {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100145 sp = (unsigned long) (&regs->sp);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200146 savesegment(ss, ss);
147 savesegment(gs, gs);
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 printk("\n");
Arjan van de Ven90f7d252008-09-16 11:27:30 -0700151
152 board = dmi_get_system_info(DMI_PRODUCT_NAME);
153 if (!board)
154 board = "";
155 printk("Pid: %d, comm: %s %s (%s %.*s) %s\n",
Linus Torvalds60812a42007-10-19 15:06:00 -0700156 task_pid_nr(current), current->comm,
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200157 print_tainted(), init_utsname()->release,
158 (int)strcspn(init_utsname()->version, " "),
Arjan van de Ven90f7d252008-09-16 11:27:30 -0700159 init_utsname()->version, board);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200160
161 printk("EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n",
Harvey Harrison92bc2052008-02-08 12:09:56 -0800162 (u16)regs->cs, regs->ip, regs->flags,
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200163 smp_processor_id());
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100164 print_symbol("EIP is at %s\n", regs->ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100167 regs->ax, regs->bx, regs->cx, regs->dx);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200168 printk("ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100169 regs->si, regs->di, regs->bp, sp);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200170 printk(" DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n",
Harvey Harrison92bc2052008-02-08 12:09:56 -0800171 (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200172
173 if (!all)
174 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700176 cr0 = read_cr0();
177 cr2 = read_cr2();
178 cr3 = read_cr3();
Zachary Amsdenff6e8c02006-01-06 00:11:50 -0800179 cr4 = read_cr4_safe();
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200180 printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n",
181 cr0, cr2, cr3, cr4);
Alan Sternbb1995d2007-07-21 17:10:42 +0200182
183 get_debugreg(d0, 0);
184 get_debugreg(d1, 1);
185 get_debugreg(d2, 2);
186 get_debugreg(d3, 3);
187 printk("DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n",
188 d0, d1, d2, d3);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200189
Alan Sternbb1995d2007-07-21 17:10:42 +0200190 get_debugreg(d6, 6);
191 get_debugreg(d7, 7);
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200192 printk("DR6: %08lx DR7: %08lx\n",
193 d6, d7);
194}
Alan Sternbb1995d2007-07-21 17:10:42 +0200195
Pavel Emelyanov9d975eb2007-10-19 20:35:03 +0200196void show_regs(struct pt_regs *regs)
197{
Pekka Enberge2ce07c2008-04-03 16:40:48 +0300198 __show_regs(regs, 1);
Arjan van de Ven5bc27dc2008-01-30 13:33:07 +0100199 show_trace(NULL, regs, &regs->sp, regs->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100203 * This gets run with %bx containing the
204 * function to call, and %dx containing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * the "args".
206 */
207extern void kernel_thread_helper(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/*
210 * Create a kernel thread
211 */
Jaswinder Singh Rajputbefa9e72009-01-04 16:18:56 +0530212int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct pt_regs regs;
215
216 memset(&regs, 0, sizeof(regs));
217
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100218 regs.bx = (unsigned long) fn;
219 regs.dx = (unsigned long) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100221 regs.ds = __USER_DS;
222 regs.es = __USER_DS;
223 regs.fs = __KERNEL_PERCPU;
Tejun Heo60a53172009-02-09 22:17:40 +0900224 regs.gs = __KERNEL_STACK_CANARY;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100225 regs.orig_ax = -1;
226 regs.ip = (unsigned long) kernel_thread_helper;
227 regs.cs = __KERNEL_CS | get_kernel_rpl();
228 regs.flags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* Ok, create the new process.. */
Andi Kleen8cf2c512006-10-21 18:37:02 +0200231 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700233EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235void release_thread(struct task_struct *dead_task)
236{
Zachary Amsden26849272006-01-06 00:11:59 -0800237 BUG_ON(dead_task->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 release_vm86_irqs(dead_task);
239}
240
241/*
242 * This gets called before we allocate a new thread and copy
243 * the current task into it.
244 */
245void prepare_to_copy(struct task_struct *tsk)
246{
247 unlazy_fpu(tsk);
248}
249
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700250int copy_thread(unsigned long clone_flags, unsigned long sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 unsigned long unused,
Jaswinder Singh Rajputbefa9e72009-01-04 16:18:56 +0530252 struct task_struct *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Jaswinder Singh Rajputbefa9e72009-01-04 16:18:56 +0530254 struct pt_regs *childregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct task_struct *tsk;
256 int err;
257
akpm@osdl.org07b047f2006-01-12 01:05:41 -0800258 childregs = task_pt_regs(p);
Alexander Nybergf48d9662005-05-05 16:15:03 -0700259 *childregs = *regs;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100260 childregs->ax = 0;
261 childregs->sp = sp;
Alexander Nybergf48d9662005-05-05 16:15:03 -0700262
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100263 p->thread.sp = (unsigned long) childregs;
264 p->thread.sp0 = (unsigned long) (childregs+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100266 p->thread.ip = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Tejun Heod9a89a22009-02-09 22:17:40 +0900268 task_user_gs(p) = get_user_gs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
K.Prasad66cb5912009-06-01 23:44:55 +0530270 p->thread.io_bitmap_ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 tsk = current;
K.Prasad66cb5912009-06-01 23:44:55 +0530272 err = -ENOMEM;
273 if (unlikely(test_tsk_thread_flag(tsk, TIF_DEBUG)))
274 if (copy_thread_hw_breakpoint(tsk, p, clone_flags))
275 goto out;
276
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400277 if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700278 p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
279 IO_BITMAP_BYTES, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!p->thread.io_bitmap_ptr) {
281 p->thread.io_bitmap_max = 0;
282 return -ENOMEM;
283 }
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400284 set_tsk_thread_flag(p, TIF_IO_BITMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Roland McGrathefd1ca52008-01-30 13:30:46 +0100287 err = 0;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /*
290 * Set a new TLS for the child thread?
291 */
Roland McGrathefd1ca52008-01-30 13:30:46 +0100292 if (clone_flags & CLONE_SETTLS)
293 err = do_set_thread_area(p, -1,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100294 (struct user_desc __user *)childregs->si, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
K.Prasad66cb5912009-06-01 23:44:55 +0530296out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (err && p->thread.io_bitmap_ptr) {
298 kfree(p->thread.io_bitmap_ptr);
299 p->thread.io_bitmap_max = 0;
300 }
K.Prasad66cb5912009-06-01 23:44:55 +0530301 if (err)
302 flush_thread_hw_breakpoint(p);
Markus Metzgerbf53de92008-12-19 15:10:24 +0100303
Markus Metzger2311f0d2009-04-03 16:43:46 +0200304 clear_tsk_thread_flag(p, TIF_DS_AREA_MSR);
305 p->thread.ds_ctx = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +0100306
307 clear_tsk_thread_flag(p, TIF_DEBUGCTLMSR);
308 p->thread.debugctlmsr = 0;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return err;
311}
312
Ingo Molnar513ad842008-02-21 05:18:40 +0100313void
314start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
315{
Tejun Heod9a89a22009-02-09 22:17:40 +0900316 set_user_gs(regs, 0);
Ingo Molnar513ad842008-02-21 05:18:40 +0100317 regs->fs = 0;
318 set_fs(USER_DS);
319 regs->ds = __USER_DS;
320 regs->es = __USER_DS;
321 regs->ss = __USER_DS;
322 regs->cs = __USER_CS;
323 regs->ip = new_ip;
324 regs->sp = new_sp;
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700325 /*
326 * Free the old FP and other extended state
327 */
328 free_thread_xstate(current);
Ingo Molnar513ad842008-02-21 05:18:40 +0100329}
330EXPORT_SYMBOL_GPL(start_thread);
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333/*
334 * switch_to(x,yn) should switch tasks from x to y.
335 *
336 * We fsave/fwait so that an exception goes off at the right time
337 * (as a call from the fsave or fwait in effect) rather than to
338 * the wrong process. Lazy FP saving no longer makes any sense
339 * with modern CPU's, and this simplifies a lot of things (SMP
340 * and UP become the same).
341 *
342 * NOTE! We used to use the x86 hardware context switching. The
343 * reason for not using it any more becomes apparent when you
344 * try to recover gracefully from saved state that is no longer
345 * valid (stale segment register values in particular). With the
346 * hardware task-switch, there is no way to fix up bad state in
347 * a reasonable manner.
348 *
349 * The fact that Intel documents the hardware task-switching to
350 * be slow is a fairly red herring - this code is not noticeably
351 * faster. However, there _is_ some room for improvement here,
352 * so the performance issues may eventually be a valid point.
353 * More important, however, is the fact that this allows us much
354 * more flexibility.
355 *
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100356 * The return value (in %ax) will be the "prev" task after
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * the task-switch, and shows up in ret_from_fork in entry.S,
358 * for example.
359 */
Frederic Weisbecker8b96f012008-12-06 03:40:00 +0100360__notrace_funcgraph struct task_struct *
361__switch_to(struct task_struct *prev_p, struct task_struct *next_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct thread_struct *prev = &prev_p->thread,
364 *next = &next_p->thread;
365 int cpu = smp_processor_id();
366 struct tss_struct *tss = &per_cpu(init_tss, cpu);
367
368 /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
369
370 __unlazy_fpu(prev_p);
371
Chuck Ebbertacc20762006-12-07 02:14:01 +0100372
373 /* we're going to use this soon, after a few expensive things */
374 if (next_p->fpu_counter > 5)
Suresh Siddha61c46282008-03-10 15:28:04 -0700375 prefetch(next->xstate);
Chuck Ebbertacc20762006-12-07 02:14:01 +0100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700378 * Reload esp0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100380 load_sp0(tss, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /*
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100383 * Save away %gs. No need to save %fs, as it was saved on the
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100384 * stack on entry. No need to save %es and %ds, as those are
385 * always kernel segments while inside the kernel. Doing this
386 * before setting the new TLS descriptors avoids the situation
387 * where we temporarily have non-reloadable segments in %fs
388 * and %gs. This could be an issue if the NMI handler ever
389 * used %fs or %gs (it does not today), or if the kernel is
390 * running inside of a hypervisor layer.
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700391 */
Tejun Heoccbeed32009-02-09 22:17:40 +0900392 lazy_save_gs(prev->gs);
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700393
394 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * Load the per-thread Thread-Local Storage descriptor.
396 */
397 load_TLS(next, cpu);
398
399 /*
Zachary Amsden8b151142007-02-13 13:26:21 +0100400 * Restore IOPL if needed. In normal use, the flags restore
401 * in the switch assembly will handle this. But if the kernel
402 * is running virtualized at a non-zero CPL, the popf will
403 * not restore flags, so it must be done in a separate step.
404 */
405 if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
406 set_iopl_mask(next->iopl);
407
408 /*
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400409 * Now maybe handle debug registers and/or IO bitmaps
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700411 if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
412 task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
413 __switch_to_xtra(prev_p, next_p, tss);
Andrea Arcangeliffaa8bd2005-06-27 14:36:36 -0700414
Zachary Amsden9226d122007-02-13 13:26:21 +0100415 /*
416 * Leave lazy mode, flushing any hypercalls made here.
417 * This must be done before restoring TLS segments so
418 * the GDT and LDT are properly updated, and must be
419 * done before math_state_restore, so the TS bit is up
420 * to date.
421 */
422 arch_leave_lazy_cpu_mode();
423
Chuck Ebbertacc20762006-12-07 02:14:01 +0100424 /* If the task has used fpu the last 5 timeslices, just do a full
425 * restore of the math state immediately to avoid the trap; the
426 * chances of needing FPU soon are obviously high now
Suresh Siddha870568b2008-06-02 15:57:27 -0700427 *
428 * tsk_used_math() checks prevent calling math_state_restore(),
429 * which can sleep in the case of !tsk_used_math()
Chuck Ebbertacc20762006-12-07 02:14:01 +0100430 */
Suresh Siddha870568b2008-06-02 15:57:27 -0700431 if (tsk_used_math(next_p) && next_p->fpu_counter > 5)
Chuck Ebbertacc20762006-12-07 02:14:01 +0100432 math_state_restore();
433
Zachary Amsden9226d122007-02-13 13:26:21 +0100434 /*
435 * Restore %gs if needed (which is common)
436 */
437 if (prev->gs | next->gs)
Tejun Heoccbeed32009-02-09 22:17:40 +0900438 lazy_load_gs(next->gs);
Zachary Amsden9226d122007-02-13 13:26:21 +0100439
Ingo Molnar6dbde352009-01-15 22:15:53 +0900440 percpu_write(current_task, next_p);
K.Prasad66cb5912009-06-01 23:44:55 +0530441 /*
442 * There's a problem with moving the arch_install_thread_hw_breakpoint()
443 * call before current is updated. Suppose a kernel breakpoint is
444 * triggered in between the two, the hw-breakpoint handler will see that
445 * the 'current' task does not have TIF_DEBUG flag set and will think it
446 * is leftover from an old task (lazy switching) and will erase it. Then
447 * until the next context switch, no user-breakpoints will be installed.
448 *
449 * The real problem is that it's impossible to update both current and
450 * physical debug registers at the same instant, so there will always be
451 * a window in which they disagree and a breakpoint might get triggered.
452 * Since we use lazy switching, we are forced to assume that a
453 * disagreement means that current is correct and the exception is due
454 * to lazy debug register switching.
455 */
456 if (unlikely(test_tsk_thread_flag(next_p, TIF_DEBUG)))
457 arch_install_thread_hw_breakpoint(next_p);
Zachary Amsden9226d122007-02-13 13:26:21 +0100458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return prev_p;
460}
461
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500462int sys_clone(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500464 unsigned long clone_flags;
465 unsigned long newsp;
466 int __user *parent_tidptr, *child_tidptr;
467
468 clone_flags = regs->bx;
469 newsp = regs->cx;
470 parent_tidptr = (int __user *)regs->dx;
471 child_tidptr = (int __user *)regs->di;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!newsp)
Brian Gerst253f29a2009-02-10 09:51:46 -0500473 newsp = regs->sp;
474 return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, child_tidptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * sys_execve() executes a new program.
479 */
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500480int sys_execve(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 int error;
Jaswinder Singh Rajputbefa9e72009-01-04 16:18:56 +0530483 char *filename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500485 filename = getname((char __user *) regs->bx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 error = PTR_ERR(filename);
487 if (IS_ERR(filename))
488 goto out;
Brian Gerstb12bdaf2009-02-11 16:43:58 -0500489 error = do_execve(filename,
490 (char __user * __user *) regs->cx,
491 (char __user * __user *) regs->dx,
492 regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (error == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Make sure we don't return using sysenter.. */
495 set_thread_flag(TIF_IRET);
496 }
497 putname(filename);
498out:
499 return error;
500}
501
502#define top_esp (THREAD_SIZE - sizeof(unsigned long))
503#define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
504
505unsigned long get_wchan(struct task_struct *p)
506{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100507 unsigned long bp, sp, ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 unsigned long stack_page;
509 int count = 0;
510 if (!p || p == current || p->state == TASK_RUNNING)
511 return 0;
Al Viro65e0fdf2006-01-12 01:05:41 -0800512 stack_page = (unsigned long)task_stack_page(p);
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100513 sp = p->thread.sp;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100514 if (!stack_page || sp < stack_page || sp > top_esp+stack_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100516 /* include/asm-i386/system.h:switch_to() pushes bp last. */
517 bp = *(unsigned long *) sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 do {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100519 if (bp < stack_page || bp > top_ebp+stack_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100521 ip = *(unsigned long *) (bp+4);
522 if (!in_sched_functions(ip))
523 return ip;
524 bp = *(unsigned long *) bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } while (count++ < 16);
526 return 0;
527}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529unsigned long arch_align_stack(unsigned long sp)
530{
Andi Kleenc16b63e02006-09-26 10:52:28 +0200531 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 sp -= get_random_int() % 8192;
533 return sp & ~0xf;
534}
Jiri Kosinac1d171a2008-01-30 13:30:40 +0100535
536unsigned long arch_randomize_brk(struct mm_struct *mm)
537{
538 unsigned long range_end = mm->brk + 0x02000000;
539 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
540}