blob: f7b276d4b3fb6436c0b879ec81f565847b6dd0fc [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
Hiroshi Shimamoto66125382008-01-30 13:31:03 +01006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * X86-64 port
8 * Andi Kleen.
Ashok Raj76e4f662005-06-25 14:55:00 -07009 *
10 * CPU hotplug support - ashok.raj@intel.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13/*
14 * This file handles the architecture-dependent parts of process handling..
15 */
16
17#include <stdarg.h>
18
Ingo Molnar42059422008-02-14 09:44:08 +010019#include <linux/stackprotector.h>
Ashok Raj76e4f662005-06-25 14:55:00 -070020#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
22#include <linux/sched.h>
Hiroshi Shimamoto66125382008-01-30 13:31:03 +010023#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/mm.h>
26#include <linux/elfcore.h>
27#include <linux/smp.h>
28#include <linux/slab.h>
29#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/utsname.h>
Hiroshi Shimamoto66125382008-01-30 13:31:03 +010032#include <linux/delay.h>
33#include <linux/module.h>
34#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/random.h>
Andi Kleen95833c82006-01-11 22:44:36 +010036#include <linux/notifier.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080037#include <linux/kprobes.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070038#include <linux/kdebug.h>
Chris Wright02290682007-10-12 23:04:07 +020039#include <linux/tick.h>
Erik Bosman529e25f2008-04-14 00:24:18 +020040#include <linux/prctl.h>
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -030041#include <linux/uaccess.h>
42#include <linux/io.h>
Frederic Weisbecker8b96f012008-12-06 03:40:00 +010043#include <linux/ftrace.h>
Kyle McMartin48ec4d92009-02-04 15:54:45 -050044#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/pgtable.h>
47#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/processor.h>
49#include <asm/i387.h>
50#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/desc.h>
53#include <asm/proto.h>
54#include <asm/ia32.h>
Andi Kleen95833c82006-01-11 22:44:36 +010055#include <asm/idle.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053056#include <asm/syscalls.h>
Markus Metzgerbf53de92008-12-19 15:10:24 +010057#include <asm/ds.h>
K.Prasad66cb5912009-06-01 23:44:55 +053058#include <asm/debugreg.h>
59#include <asm/hw_breakpoint.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61asmlinkage extern void ret_from_fork(void);
62
Brian Gerstc6f5e0a2009-01-19 00:38:58 +090063DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
64EXPORT_PER_CPU_SYMBOL(current_task);
65
Brian Gerst3d1e42a2009-01-19 00:38:58 +090066DEFINE_PER_CPU(unsigned long, old_rsp);
Brian Gerstc2558e02009-01-19 00:38:59 +090067static DEFINE_PER_CPU(unsigned char, is_idle);
Brian Gerst3d1e42a2009-01-19 00:38:58 +090068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069unsigned long kernel_thread_flags = CLONE_VM | CLONE_UNTRACED;
70
Alan Sterne041c682006-03-27 01:16:30 -080071static ATOMIC_NOTIFIER_HEAD(idle_notifier);
Andi Kleen95833c82006-01-11 22:44:36 +010072
73void idle_notifier_register(struct notifier_block *n)
74{
Alan Sterne041c682006-03-27 01:16:30 -080075 atomic_notifier_chain_register(&idle_notifier, n);
Andi Kleen95833c82006-01-11 22:44:36 +010076}
Venkatesh Pallipadic7d87d72008-10-16 16:34:43 -040077EXPORT_SYMBOL_GPL(idle_notifier_register);
78
79void idle_notifier_unregister(struct notifier_block *n)
80{
81 atomic_notifier_chain_unregister(&idle_notifier, n);
82}
83EXPORT_SYMBOL_GPL(idle_notifier_unregister);
Andi Kleen95833c82006-01-11 22:44:36 +010084
Andi Kleen95833c82006-01-11 22:44:36 +010085void enter_idle(void)
86{
Brian Gerstc2558e02009-01-19 00:38:59 +090087 percpu_write(is_idle, 1);
Alan Sterne041c682006-03-27 01:16:30 -080088 atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
Andi Kleen95833c82006-01-11 22:44:36 +010089}
90
91static void __exit_idle(void)
92{
Brian Gerstc2558e02009-01-19 00:38:59 +090093 if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
Andi Kleena15da492006-09-26 10:52:40 +020094 return;
Alan Sterne041c682006-03-27 01:16:30 -080095 atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
Andi Kleen95833c82006-01-11 22:44:36 +010096}
97
98/* Called from interrupts to signify idle end */
99void exit_idle(void)
100{
Andi Kleena15da492006-09-26 10:52:40 +0200101 /* idle loop has pid 0 */
102 if (current->pid)
Andi Kleen95833c82006-01-11 22:44:36 +0100103 return;
104 __exit_idle();
105}
106
Alex Nixon913da642008-09-03 14:30:23 +0100107#ifndef CONFIG_SMP
Ashok Raj76e4f662005-06-25 14:55:00 -0700108static inline void play_dead(void)
109{
110 BUG();
111}
Alex Nixon913da642008-09-03 14:30:23 +0100112#endif
Ashok Raj76e4f662005-06-25 14:55:00 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * The idle thread. There's no useful work to be
116 * done, so just try to conserve power and have a
117 * low exit latency (ie sit in a loop waiting for
118 * somebody to say that they'd like to reschedule)
119 */
Pavel Machekb10db7f2008-01-30 13:30:00 +0100120void cpu_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Andi Kleen495ab9c2006-06-26 13:59:11 +0200122 current_thread_info()->status |= TS_POLLING;
Arjan van de Vence22bd92008-05-12 15:44:31 +0200123
Arjan van de Vence22bd92008-05-12 15:44:31 +0200124 /*
Tejun Heo5c79d2a2009-02-11 16:31:00 +0900125 * If we're the non-boot CPU, nothing set the stack canary up
126 * for us. CPU0 already has it initialized but no harm in
127 * doing it again. This is a good place for updating it, as
128 * we wont ever return from this function (so the invalid
129 * canaries already on the stack wont ever trigger).
Arjan van de Vence22bd92008-05-12 15:44:31 +0200130 */
Ingo Molnar18aa8bb2008-02-14 09:42:02 +0100131 boot_init_stack_canary();
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* endless idle loop with no priority at all */
134 while (1) {
Thomas Gleixnerb8f8c3c2008-07-18 17:27:28 +0200135 tick_nohz_stop_sched_tick(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 while (!need_resched()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 rmb();
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200139
Ashok Raj76e4f662005-06-25 14:55:00 -0700140 if (cpu_is_offline(smp_processor_id()))
141 play_dead();
Venkatesh Pallipadid331e732006-12-07 02:14:13 +0100142 /*
143 * Idle routines should keep interrupts disabled
144 * from here on, until they go to idle.
145 * Otherwise, idle callbacks can misfire.
146 */
147 local_irq_disable();
Andi Kleen95833c82006-01-11 22:44:36 +0100148 enter_idle();
Steven Rostedt81d68a92008-05-12 21:20:42 +0200149 /* Don't trace irqs off for idle */
150 stop_critical_timings();
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200151 pm_idle();
Steven Rostedt81d68a92008-05-12 21:20:42 +0200152 start_critical_timings();
Andi Kleena15da492006-09-26 10:52:40 +0200153 /* In many cases the interrupt that ended idle
154 has already called exit_idle. But some idle
155 loops can be woken up without interrupt. */
Andi Kleen95833c82006-01-11 22:44:36 +0100156 __exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
Chris Wright02290682007-10-12 23:04:07 +0200159 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800160 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 schedule();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800162 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164}
165
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100166/* Prints also some state that isn't saved in the pt_regs */
Pekka Enberge2ce07c2008-04-03 16:40:48 +0300167void __show_regs(struct pt_regs *regs, int all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
Alan Sternbb1995d2007-07-21 17:10:42 +0200170 unsigned long d0, d1, d2, d3, d6, d7;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100171 unsigned int fsindex, gsindex;
172 unsigned int ds, cs, es;
Kyle McMartin48ec4d92009-02-04 15:54:45 -0500173 const char *board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 printk("\n");
176 print_modules();
Kyle McMartin48ec4d92009-02-04 15:54:45 -0500177 board = dmi_get_system_info(DMI_PRODUCT_NAME);
178 if (!board)
179 board = "";
180 printk(KERN_INFO "Pid: %d, comm: %.20s %s %s %.*s %s\n",
Andi Kleen9acf23c2005-09-12 18:49:24 +0200181 current->pid, current->comm, print_tainted(),
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700182 init_utsname()->release,
183 (int)strcspn(init_utsname()->version, " "),
Kyle McMartin48ec4d92009-02-04 15:54:45 -0500184 init_utsname()->version, board);
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300185 printk(KERN_INFO "RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->ip);
Arjan van de Venaafbd7e2008-01-30 13:33:08 +0100186 printk_address(regs->ip, 1);
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300187 printk(KERN_INFO "RSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss,
188 regs->sp, regs->flags);
189 printk(KERN_INFO "RAX: %016lx RBX: %016lx RCX: %016lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100190 regs->ax, regs->bx, regs->cx);
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300191 printk(KERN_INFO "RDX: %016lx RSI: %016lx RDI: %016lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100192 regs->dx, regs->si, regs->di);
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300193 printk(KERN_INFO "RBP: %016lx R08: %016lx R09: %016lx\n",
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100194 regs->bp, regs->r8, regs->r9);
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300195 printk(KERN_INFO "R10: %016lx R11: %016lx R12: %016lx\n",
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300196 regs->r10, regs->r11, regs->r12);
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300197 printk(KERN_INFO "R13: %016lx R14: %016lx R15: %016lx\n",
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300198 regs->r13, regs->r14, regs->r15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300200 asm("movl %%ds,%0" : "=r" (ds));
201 asm("movl %%cs,%0" : "=r" (cs));
202 asm("movl %%es,%0" : "=r" (es));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 asm("movl %%fs,%0" : "=r" (fsindex));
204 asm("movl %%gs,%0" : "=r" (gsindex));
205
206 rdmsrl(MSR_FS_BASE, fs);
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300207 rdmsrl(MSR_GS_BASE, gs);
208 rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Pekka Enberge2ce07c2008-04-03 16:40:48 +0300210 if (!all)
211 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200213 cr0 = read_cr0();
214 cr2 = read_cr2();
215 cr3 = read_cr3();
216 cr4 = read_cr4();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300218 printk(KERN_INFO "FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300219 fs, fsindex, gs, gsindex, shadowgs);
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300220 printk(KERN_INFO "CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds,
221 es, cr0);
222 printk(KERN_INFO "CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3,
223 cr4);
Alan Sternbb1995d2007-07-21 17:10:42 +0200224
225 get_debugreg(d0, 0);
226 get_debugreg(d1, 1);
227 get_debugreg(d2, 2);
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300228 printk(KERN_INFO "DR0: %016lx DR1: %016lx DR2: %016lx\n", d0, d1, d2);
Alan Sternbb1995d2007-07-21 17:10:42 +0200229 get_debugreg(d3, 3);
230 get_debugreg(d6, 6);
231 get_debugreg(d7, 7);
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300232 printk(KERN_INFO "DR3: %016lx DR6: %016lx DR7: %016lx\n", d3, d6, d7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235void show_regs(struct pt_regs *regs)
236{
Gustavo F. Padovan8092c652008-07-29 02:48:52 -0300237 printk(KERN_INFO "CPU %d:", smp_processor_id());
Pekka Enberge2ce07c2008-04-03 16:40:48 +0300238 __show_regs(regs, 1);
Arjan van de Venbc850d62008-01-30 13:33:07 +0100239 show_trace(NULL, regs, (void *)(regs + 1), regs->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242void release_thread(struct task_struct *dead_task)
243{
244 if (dead_task->mm) {
245 if (dead_task->mm->context.size) {
246 printk("WARNING: dead process %8s still has LDT? <%p/%d>\n",
247 dead_task->comm,
248 dead_task->mm->context.ldt,
249 dead_task->mm->context.size);
250 BUG();
251 }
252 }
K.Prasad66cb5912009-06-01 23:44:55 +0530253 if (unlikely(dead_task->thread.debugreg7))
254 flush_thread_hw_breakpoint(dead_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
258{
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100259 struct user_desc ud = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .base_addr = addr,
261 .limit = 0xfffff,
262 .seg_32bit = 1,
263 .limit_in_pages = 1,
264 .useable = 1,
265 };
Jan Engelhardtade1af72008-01-30 13:33:23 +0100266 struct desc_struct *desc = t->thread.tls_array;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 desc += tls;
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +0100268 fill_ldt(desc, &ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271static inline u32 read_32bit_tls(struct task_struct *t, int tls)
272{
Roland McGrath91394eb2008-01-30 13:30:45 +0100273 return get_desc_base(&t->thread.tls_array[tls]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/*
277 * This gets called before we allocate a new thread and copy
278 * the current task into it.
279 */
280void prepare_to_copy(struct task_struct *tsk)
281{
282 unlazy_fpu(tsk);
283}
284
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700285int copy_thread(unsigned long clone_flags, unsigned long sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unsigned long unused,
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300287 struct task_struct *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int err;
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300290 struct pt_regs *childregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct task_struct *me = current;
292
Andi Kleena88cde12005-11-05 17:25:54 +0100293 childregs = ((struct pt_regs *)
Al Viro57eafdc2006-01-12 01:05:39 -0800294 (THREAD_SIZE + task_stack_page(p))) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *childregs = *regs;
296
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100297 childregs->ax = 0;
298 childregs->sp = sp;
299 if (sp == ~0UL)
300 childregs->sp = (unsigned long)childregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100302 p->thread.sp = (unsigned long) childregs;
303 p->thread.sp0 = (unsigned long) (childregs+1);
304 p->thread.usersp = me->thread.usersp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Al Viroe4f17c42006-01-12 01:05:38 -0800306 set_tsk_thread_flag(p, TIF_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 p->thread.fs = me->thread.fs;
309 p->thread.gs = me->thread.gs;
K.Prasad66cb5912009-06-01 23:44:55 +0530310 p->thread.io_bitmap_ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Jeremy Fitzhardingeada85702008-06-25 00:19:00 -0400312 savesegment(gs, p->thread.gsindex);
313 savesegment(fs, p->thread.fsindex);
314 savesegment(es, p->thread.es);
315 savesegment(ds, p->thread.ds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
K.Prasad66cb5912009-06-01 23:44:55 +0530317 err = -ENOMEM;
318 if (unlikely(test_tsk_thread_flag(me, TIF_DEBUG)))
319 if (copy_thread_hw_breakpoint(me, p, clone_flags))
320 goto out;
321
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200322 if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
324 if (!p->thread.io_bitmap_ptr) {
325 p->thread.io_bitmap_max = 0;
326 return -ENOMEM;
327 }
Andi Kleena88cde12005-11-05 17:25:54 +0100328 memcpy(p->thread.io_bitmap_ptr, me->thread.io_bitmap_ptr,
329 IO_BITMAP_BYTES);
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200330 set_tsk_thread_flag(p, TIF_IO_BITMAP);
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /*
334 * Set a new TLS for the child thread?
335 */
336 if (clone_flags & CLONE_SETTLS) {
337#ifdef CONFIG_IA32_EMULATION
338 if (test_thread_flag(TIF_IA32))
Roland McGrathefd1ca52008-01-30 13:30:46 +0100339 err = do_set_thread_area(p, -1,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100340 (struct user_desc __user *)childregs->si, 0);
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300341 else
342#endif
343 err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
344 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto out;
346 }
Markus Metzgerbf53de92008-12-19 15:10:24 +0100347
Markus Metzger2311f0d2009-04-03 16:43:46 +0200348 clear_tsk_thread_flag(p, TIF_DS_AREA_MSR);
349 p->thread.ds_ctx = NULL;
Markus Metzgerbf53de92008-12-19 15:10:24 +0100350
351 clear_tsk_thread_flag(p, TIF_DEBUGCTLMSR);
352 p->thread.debugctlmsr = 0;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 err = 0;
355out:
356 if (err && p->thread.io_bitmap_ptr) {
357 kfree(p->thread.io_bitmap_ptr);
358 p->thread.io_bitmap_max = 0;
359 }
K.Prasad66cb5912009-06-01 23:44:55 +0530360 if (err)
361 flush_thread_hw_breakpoint(p);
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return err;
364}
365
Ingo Molnar513ad842008-02-21 05:18:40 +0100366void
367start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
368{
Jeremy Fitzhardingeada85702008-06-25 00:19:00 -0400369 loadsegment(fs, 0);
370 loadsegment(es, 0);
371 loadsegment(ds, 0);
Ingo Molnar513ad842008-02-21 05:18:40 +0100372 load_gs_index(0);
373 regs->ip = new_ip;
374 regs->sp = new_sp;
Brian Gerst3d1e42a2009-01-19 00:38:58 +0900375 percpu_write(old_rsp, new_sp);
Ingo Molnar513ad842008-02-21 05:18:40 +0100376 regs->cs = __USER_CS;
377 regs->ss = __USER_DS;
378 regs->flags = 0x200;
379 set_fs(USER_DS);
Suresh Siddhaaa283f42008-03-10 15:28:05 -0700380 /*
381 * Free the old FP and other extended state
382 */
383 free_thread_xstate(current);
Ingo Molnar513ad842008-02-21 05:18:40 +0100384}
385EXPORT_SYMBOL_GPL(start_thread);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
388 * switch_to(x,y) should switch tasks from x to y.
389 *
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100390 * This could still be optimized:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * - fold all the options into a flag word and test it with a single test.
392 * - could test fs/gs bitsliced
Andi Kleen099f3182006-02-03 21:51:38 +0100393 *
394 * Kprobes not supported here. Set the probe on schedule instead.
Frederic Weisbecker8b96f012008-12-06 03:40:00 +0100395 * Function graph tracer not supported too.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
Frederic Weisbecker8b96f012008-12-06 03:40:00 +0100397__notrace_funcgraph struct task_struct *
Andi Kleena88cde12005-11-05 17:25:54 +0100398__switch_to(struct task_struct *prev_p, struct task_struct *next_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Jeremy Fitzhardinge87b935a2008-07-08 15:06:26 -0700400 struct thread_struct *prev = &prev_p->thread;
401 struct thread_struct *next = &next_p->thread;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100402 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct tss_struct *tss = &per_cpu(init_tss, cpu);
Jeremy Fitzhardinge478de5a2008-06-25 00:19:24 -0400404 unsigned fsindex, gsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Arjan van de Vene07e23e2006-09-26 10:52:36 +0200406 /* we're going to use this soon, after a few expensive things */
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300407 if (next_p->fpu_counter > 5)
Suresh Siddha61c46282008-03-10 15:28:04 -0700408 prefetch(next->xstate);
Arjan van de Vene07e23e2006-09-26 10:52:36 +0200409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * Reload esp0, LDT and the page table pointer:
412 */
Glauber de Oliveira Costa7818a1e2008-01-30 13:31:31 +0100413 load_sp0(tss, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300415 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * Switch DS and ES.
417 * This won't pick up thread selector changes, but I guess that is ok.
418 */
Jeremy Fitzhardingeada85702008-06-25 00:19:00 -0400419 savesegment(es, prev->es);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (unlikely(next->es | prev->es))
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300421 loadsegment(es, next->es);
Jeremy Fitzhardingeada85702008-06-25 00:19:00 -0400422
423 savesegment(ds, prev->ds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (unlikely(next->ds | prev->ds))
425 loadsegment(ds, next->ds);
426
Jeremy Fitzhardinge478de5a2008-06-25 00:19:24 -0400427
428 /* We must save %fs and %gs before load_TLS() because
429 * %fs and %gs may be cleared by load_TLS().
430 *
431 * (e.g. xen_load_tls())
432 */
433 savesegment(fs, fsindex);
434 savesegment(gs, gsindex);
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 load_TLS(next, cpu);
437
Jeremy Fitzhardinge3fe0a632008-06-25 00:19:23 -0400438 /*
439 * Leave lazy mode, flushing any hypercalls made here.
440 * This must be done before restoring TLS segments so
441 * the GDT and LDT are properly updated, and must be
442 * done before math_state_restore, so the TS bit is up
443 * to date.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
Jeremy Fitzhardinge3fe0a632008-06-25 00:19:23 -0400445 arch_leave_lazy_cpu_mode();
446
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300447 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * Switch FS and GS.
Jeremy Fitzhardinge87b935a2008-07-08 15:06:26 -0700449 *
450 * Segment register != 0 always requires a reload. Also
451 * reload when it has changed. When prev process used 64bit
452 * base always reload to avoid an information leak.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
Jeremy Fitzhardinge87b935a2008-07-08 15:06:26 -0700454 if (unlikely(fsindex | next->fsindex | prev->fs)) {
455 loadsegment(fs, next->fsindex);
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300456 /*
Jeremy Fitzhardinge87b935a2008-07-08 15:06:26 -0700457 * Check if the user used a selector != 0; if yes
458 * clear 64bit base, since overloaded base is always
459 * mapped to the Null selector
460 */
461 if (fsindex)
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300462 prev->fs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Jeremy Fitzhardinge87b935a2008-07-08 15:06:26 -0700464 /* when next process has a 64bit base use it */
465 if (next->fs)
466 wrmsrl(MSR_FS_BASE, next->fs);
467 prev->fsindex = fsindex;
468
469 if (unlikely(gsindex | next->gsindex | prev->gs)) {
470 load_gs_index(next->gsindex);
471 if (gsindex)
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300472 prev->gs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Jeremy Fitzhardinge87b935a2008-07-08 15:06:26 -0700474 if (next->gs)
475 wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
476 prev->gsindex = gsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Andi Kleen0a5ace22006-10-05 18:47:22 +0200478 /* Must be after DS reload */
479 unlazy_fpu(prev_p);
480
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300481 /*
Jan Beulich45948d72006-03-25 16:29:25 +0100482 * Switch the PDA and FPU contexts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Brian Gerst3d1e42a2009-01-19 00:38:58 +0900484 prev->usersp = percpu_read(old_rsp);
485 percpu_write(old_rsp, next->usersp);
Brian Gerstc6f5e0a2009-01-19 00:38:58 +0900486 percpu_write(current_task, next_p);
Andi Kleen18bd0572006-04-20 02:36:45 +0200487
Brian Gerst9af45652009-01-19 00:38:58 +0900488 percpu_write(kernel_stack,
Jeremy Fitzhardinge87b935a2008-07-08 15:06:26 -0700489 (unsigned long)task_stack_page(next_p) +
Brian Gerst9af45652009-01-19 00:38:58 +0900490 THREAD_SIZE - KERNEL_STACK_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /*
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200493 * Now maybe reload the debug registers and handle I/O bitmaps
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
Markus Metzgereee3af42008-01-30 13:31:09 +0100495 if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT ||
496 task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
Stephane Eraniand3a4f482006-09-26 10:52:28 +0200497 __switch_to_xtra(prev_p, next_p, tss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Arjan van de Vene07e23e2006-09-26 10:52:36 +0200499 /* If the task has used fpu the last 5 timeslices, just do a full
500 * restore of the math state immediately to avoid the trap; the
501 * chances of needing FPU soon are obviously high now
Suresh Siddha870568b2008-06-02 15:57:27 -0700502 *
503 * tsk_used_math() checks prevent calling math_state_restore(),
504 * which can sleep in the case of !tsk_used_math()
Arjan van de Vene07e23e2006-09-26 10:52:36 +0200505 */
Suresh Siddha870568b2008-06-02 15:57:27 -0700506 if (tsk_used_math(next_p) && next_p->fpu_counter > 5)
Arjan van de Vene07e23e2006-09-26 10:52:36 +0200507 math_state_restore();
K.Prasad66cb5912009-06-01 23:44:55 +0530508 /*
509 * There's a problem with moving the arch_install_thread_hw_breakpoint()
510 * call before current is updated. Suppose a kernel breakpoint is
511 * triggered in between the two, the hw-breakpoint handler will see that
512 * the 'current' task does not have TIF_DEBUG flag set and will think it
513 * is leftover from an old task (lazy switching) and will erase it. Then
514 * until the next context switch, no user-breakpoints will be installed.
515 *
516 * The real problem is that it's impossible to update both current and
517 * physical debug registers at the same instant, so there will always be
518 * a window in which they disagree and a breakpoint might get triggered.
519 * Since we use lazy switching, we are forced to assume that a
520 * disagreement means that current is correct and the exception is due
521 * to lazy debug register switching.
522 */
523 if (unlikely(test_tsk_thread_flag(next_p, TIF_DEBUG)))
524 arch_install_thread_hw_breakpoint(next_p);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return prev_p;
527}
528
529/*
530 * sys_execve() executes a new program.
531 */
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100532asmlinkage
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533long sys_execve(char __user *name, char __user * __user *argv,
Ingo Molnar5d119b22008-02-26 12:55:57 +0100534 char __user * __user *envp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 long error;
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300537 char *filename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 filename = getname(name);
540 error = PTR_ERR(filename);
Ingo Molnar5d119b22008-02-26 12:55:57 +0100541 if (IS_ERR(filename))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return error;
Ingo Molnar5d119b22008-02-26 12:55:57 +0100543 error = do_execve(filename, argv, envp, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 putname(filename);
545 return error;
546}
547
548void set_personality_64bit(void)
549{
550 /* inherit personality from parent */
551
552 /* Make sure to be in 64bit mode */
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100553 clear_thread_flag(TIF_IA32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* TBD: overwrites user setup. Should have two bits.
556 But 64bit processes have always behaved this way,
557 so it's not too bad. The main problem is just that
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100558 32bit childs are affected again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 current->personality &= ~READ_IMPLIES_EXEC;
560}
561
Andi Kleena88cde12005-11-05 17:25:54 +0100562asmlinkage long
563sys_clone(unsigned long clone_flags, unsigned long newsp,
564 void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 if (!newsp)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100567 newsp = regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571unsigned long get_wchan(struct task_struct *p)
572{
573 unsigned long stack;
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300574 u64 fp, ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int count = 0;
576
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300577 if (!p || p == current || p->state == TASK_RUNNING)
578 return 0;
Al Viro57eafdc2006-01-12 01:05:39 -0800579 stack = (unsigned long)task_stack_page(p);
David Rientjese1e23bb2008-10-07 14:15:11 -0700580 if (p->thread.sp < stack || p->thread.sp >= stack+THREAD_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
H. Peter Anvinfaca6222008-01-30 13:31:02 +0100582 fp = *(u64 *)(p->thread.sp);
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300583 do {
Andi Kleena88cde12005-11-05 17:25:54 +0100584 if (fp < (unsigned long)stack ||
David Rientjese1e23bb2008-10-07 14:15:11 -0700585 fp >= (unsigned long)stack+THREAD_SIZE)
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300586 return 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100587 ip = *(u64 *)(fp+8);
588 if (!in_sched_functions(ip))
589 return ip;
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300590 fp = *(u64 *)fp;
591 } while (count++ < 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0;
593}
594
595long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300596{
597 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int doit = task == current;
599 int cpu;
600
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300601 switch (code) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 case ARCH_SET_GS:
Suresh Siddha84929802005-06-21 17:14:32 -0700603 if (addr >= TASK_SIZE_OF(task))
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300604 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 cpu = get_cpu();
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300606 /* handle small bases via the GDT because that's faster to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 switch. */
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300608 if (addr <= 0xffffffff) {
609 set_32bit_tls(task, GS_TLS, addr);
610 if (doit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 load_TLS(&task->thread, cpu);
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300612 load_gs_index(GS_TLS_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300614 task->thread.gsindex = GS_TLS_SEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 task->thread.gs = 0;
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300616 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 task->thread.gsindex = 0;
618 task->thread.gs = addr;
619 if (doit) {
Andi Kleena88cde12005-11-05 17:25:54 +0100620 load_gs_index(0);
621 ret = checking_wrmsrl(MSR_KERNEL_GS_BASE, addr);
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624 put_cpu();
625 break;
626 case ARCH_SET_FS:
627 /* Not strictly needed for fs, but do it for symmetry
628 with gs */
Suresh Siddha84929802005-06-21 17:14:32 -0700629 if (addr >= TASK_SIZE_OF(task))
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100630 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 cpu = get_cpu();
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100632 /* handle small bases via the GDT because that's faster to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 switch. */
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100634 if (addr <= 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 set_32bit_tls(task, FS_TLS, addr);
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100636 if (doit) {
637 load_TLS(&task->thread, cpu);
Jeremy Fitzhardingeada85702008-06-25 00:19:00 -0400638 loadsegment(fs, FS_TLS_SEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 task->thread.fsindex = FS_TLS_SEL;
641 task->thread.fs = 0;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100642 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 task->thread.fsindex = 0;
644 task->thread.fs = addr;
645 if (doit) {
646 /* set the selector to 0 to not confuse
647 __switch_to */
Jeremy Fitzhardingeada85702008-06-25 00:19:00 -0400648 loadsegment(fs, 0);
Andi Kleena88cde12005-11-05 17:25:54 +0100649 ret = checking_wrmsrl(MSR_FS_BASE, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 }
652 put_cpu();
653 break;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100654 case ARCH_GET_FS: {
655 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (task->thread.fsindex == FS_TLS_SEL)
657 base = read_32bit_tls(task, FS_TLS);
Andi Kleena88cde12005-11-05 17:25:54 +0100658 else if (doit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 rdmsrl(MSR_FS_BASE, base);
Andi Kleena88cde12005-11-05 17:25:54 +0100660 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 base = task->thread.fs;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100662 ret = put_user(base, (unsigned long __user *)addr);
663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100665 case ARCH_GET_GS: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 unsigned long base;
John Blackwood97c28032006-04-07 19:50:25 +0200667 unsigned gsindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (task->thread.gsindex == GS_TLS_SEL)
669 base = read_32bit_tls(task, GS_TLS);
John Blackwood97c28032006-04-07 19:50:25 +0200670 else if (doit) {
Jeremy Fitzhardingeada85702008-06-25 00:19:00 -0400671 savesegment(gs, gsindex);
John Blackwood97c28032006-04-07 19:50:25 +0200672 if (gsindex)
673 rdmsrl(MSR_KERNEL_GS_BASE, base);
674 else
675 base = task->thread.gs;
Gustavo F. Padovan7de08b42008-07-29 02:48:51 -0300676 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 base = task->thread.gs;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100678 ret = put_user(base, (unsigned long __user *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 break;
680 }
681
682 default:
683 ret = -EINVAL;
684 break;
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Hiroshi Shimamoto66125382008-01-30 13:31:03 +0100687 return ret;
688}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690long sys_arch_prctl(int code, unsigned long addr)
691{
692 return do_arch_prctl(current, code, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695unsigned long arch_align_stack(unsigned long sp)
696{
Andi Kleenc16b63e2006-09-26 10:52:28 +0200697 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 sp -= get_random_int() % 8192;
699 return sp & ~0xf;
700}
Jiri Kosinac1d171a2008-01-30 13:30:40 +0100701
702unsigned long arch_randomize_brk(struct mm_struct *mm)
703{
704 unsigned long range_end = mm->brk + 0x02000000;
705 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
706}