Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 1995 Linus Torvalds |
| 3 | * |
| 4 | * Pentium III FXSR, SSE support |
| 5 | * Gareth Hughes <gareth@valinux.com>, May 2000 |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * This file handles the architecture-dependent parts of process handling.. |
| 10 | */ |
| 11 | |
| 12 | #include <stdarg.h> |
| 13 | |
Tejun Heo | 5c79d2a | 2009-02-11 16:31:00 +0900 | [diff] [blame] | 14 | #include <linux/stackprotector.h> |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 15 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/stddef.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/vmalloc.h> |
| 26 | #include <linux/user.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #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 Kleen | c16b63e0 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 37 | #include <linux/personality.h> |
Ingo Molnar | 7416734 | 2007-02-16 01:28:07 -0800 | [diff] [blame] | 38 | #include <linux/tick.h> |
Jeremy Fitzhardinge | 7c3576d | 2007-05-02 19:27:16 +0200 | [diff] [blame] | 39 | #include <linux/percpu.h> |
Erik Bosman | 529e25f | 2008-04-14 00:24:18 +0200 | [diff] [blame] | 40 | #include <linux/prctl.h> |
Arjan van de Ven | 90f7d25 | 2008-09-16 11:27:30 -0700 | [diff] [blame] | 41 | #include <linux/dmi.h> |
Frederic Weisbecker | 8b96f01 | 2008-12-06 03:40:00 +0100 | [diff] [blame] | 42 | #include <linux/ftrace.h> |
Jaswinder Singh Rajput | befa9e7 | 2009-01-04 16:18:56 +0530 | [diff] [blame] | 43 | #include <linux/uaccess.h> |
| 44 | #include <linux/io.h> |
| 45 | #include <linux/kdebug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <asm/pgtable.h> |
| 48 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <asm/ldt.h> |
| 50 | #include <asm/processor.h> |
| 51 | #include <asm/i387.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <asm/desc.h> |
| 53 | #ifdef CONFIG_MATH_EMULATION |
| 54 | #include <asm/math_emu.h> |
| 55 | #endif |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <linux/err.h> |
| 58 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 59 | #include <asm/tlbflush.h> |
| 60 | #include <asm/cpu.h> |
Marc Dionne | 1eda814 | 2008-09-23 22:40:02 -0400 | [diff] [blame] | 61 | #include <asm/idle.h> |
Jaswinder Singh | bbc1f69 | 2008-07-21 21:34:13 +0530 | [diff] [blame] | 62 | #include <asm/syscalls.h> |
Markus Metzger | bf53de9 | 2008-12-19 15:10:24 +0100 | [diff] [blame] | 63 | #include <asm/ds.h> |
K.Prasad | 66cb591 | 2009-06-01 23:44:55 +0530 | [diff] [blame^] | 64 | #include <asm/debugreg.h> |
| 65 | #include <asm/hw_breakpoint.h> |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | asmlinkage void ret_from_fork(void) __asm__("ret_from_fork"); |
| 68 | |
Jeremy Fitzhardinge | 7c3576d | 2007-05-02 19:27:16 +0200 | [diff] [blame] | 69 | DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task; |
| 70 | EXPORT_PER_CPU_SYMBOL(current_task); |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Return saved PC of a blocked thread. |
| 74 | */ |
| 75 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 76 | { |
H. Peter Anvin | faca622 | 2008-01-30 13:31:02 +0100 | [diff] [blame] | 77 | return ((unsigned long *)tsk->thread.sp)[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alex Nixon | 913da64 | 2008-09-03 14:30:23 +0100 | [diff] [blame] | 80 | #ifndef CONFIG_SMP |
| 81 | static inline void play_dead(void) |
| 82 | { |
| 83 | BUG(); |
| 84 | } |
| 85 | #endif |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* |
| 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 Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 93 | void cpu_idle(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 95 | int cpu = smp_processor_id(); |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 96 | |
Tejun Heo | 5c79d2a | 2009-02-11 16:31:00 +0900 | [diff] [blame] | 97 | /* |
| 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 Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 106 | current_thread_info()->status |= TS_POLLING; |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | /* endless idle loop with no priority at all */ |
| 109 | while (1) { |
Thomas Gleixner | b8f8c3c | 2008-07-18 17:27:28 +0200 | [diff] [blame] | 110 | tick_nohz_stop_sched_tick(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | while (!need_resched()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Christoph Lameter | f1d1a84 | 2007-05-12 11:15:24 -0700 | [diff] [blame] | 113 | check_pgt_cache(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | rmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 116 | if (cpu_is_offline(cpu)) |
| 117 | play_dead(); |
| 118 | |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 119 | local_irq_disable(); |
Steven Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 120 | /* Don't trace irqs off for idle */ |
| 121 | stop_critical_timings(); |
Thomas Gleixner | 6ddd2a2 | 2008-06-09 16:59:53 +0200 | [diff] [blame] | 122 | pm_idle(); |
Steven Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 123 | start_critical_timings(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
Ingo Molnar | 7416734 | 2007-02-16 01:28:07 -0800 | [diff] [blame] | 125 | tick_nohz_restart_sched_tick(); |
Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 126 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | schedule(); |
Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 128 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Pekka Enberg | e2ce07c | 2008-04-03 16:40:48 +0300 | [diff] [blame] | 132 | void __show_regs(struct pt_regs *regs, int all) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L; |
Alan Stern | bb1995d | 2007-07-21 17:10:42 +0200 | [diff] [blame] | 135 | unsigned long d0, d1, d2, d3, d6, d7; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 136 | unsigned long sp; |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 137 | unsigned short ss, gs; |
Arjan van de Ven | 90f7d25 | 2008-09-16 11:27:30 -0700 | [diff] [blame] | 138 | const char *board; |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 139 | |
| 140 | if (user_mode_vm(regs)) { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 141 | sp = regs->sp; |
| 142 | ss = regs->ss & 0xffff; |
Tejun Heo | d9a89a2 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 143 | gs = get_user_gs(regs); |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 144 | } else { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 145 | sp = (unsigned long) (®s->sp); |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 146 | savesegment(ss, ss); |
| 147 | savesegment(gs, gs); |
| 148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | printk("\n"); |
Arjan van de Ven | 90f7d25 | 2008-09-16 11:27:30 -0700 | [diff] [blame] | 151 | |
| 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 Torvalds | 60812a4 | 2007-10-19 15:06:00 -0700 | [diff] [blame] | 156 | task_pid_nr(current), current->comm, |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 157 | print_tainted(), init_utsname()->release, |
| 158 | (int)strcspn(init_utsname()->version, " "), |
Arjan van de Ven | 90f7d25 | 2008-09-16 11:27:30 -0700 | [diff] [blame] | 159 | init_utsname()->version, board); |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 160 | |
| 161 | printk("EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n", |
Harvey Harrison | 92bc205 | 2008-02-08 12:09:56 -0800 | [diff] [blame] | 162 | (u16)regs->cs, regs->ip, regs->flags, |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 163 | smp_processor_id()); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 164 | print_symbol("EIP is at %s\n", regs->ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n", |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 167 | regs->ax, regs->bx, regs->cx, regs->dx); |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 168 | printk("ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n", |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 169 | regs->si, regs->di, regs->bp, sp); |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 170 | printk(" DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n", |
Harvey Harrison | 92bc205 | 2008-02-08 12:09:56 -0800 | [diff] [blame] | 171 | (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss); |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 172 | |
| 173 | if (!all) |
| 174 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 176 | cr0 = read_cr0(); |
| 177 | cr2 = read_cr2(); |
| 178 | cr3 = read_cr3(); |
Zachary Amsden | ff6e8c0 | 2006-01-06 00:11:50 -0800 | [diff] [blame] | 179 | cr4 = read_cr4_safe(); |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 180 | printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", |
| 181 | cr0, cr2, cr3, cr4); |
Alan Stern | bb1995d | 2007-07-21 17:10:42 +0200 | [diff] [blame] | 182 | |
| 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 Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 189 | |
Alan Stern | bb1995d | 2007-07-21 17:10:42 +0200 | [diff] [blame] | 190 | get_debugreg(d6, 6); |
| 191 | get_debugreg(d7, 7); |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 192 | printk("DR6: %08lx DR7: %08lx\n", |
| 193 | d6, d7); |
| 194 | } |
Alan Stern | bb1995d | 2007-07-21 17:10:42 +0200 | [diff] [blame] | 195 | |
Pavel Emelyanov | 9d975eb | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 196 | void show_regs(struct pt_regs *regs) |
| 197 | { |
Pekka Enberg | e2ce07c | 2008-04-03 16:40:48 +0300 | [diff] [blame] | 198 | __show_regs(regs, 1); |
Arjan van de Ven | 5bc27dc | 2008-01-30 13:33:07 +0100 | [diff] [blame] | 199 | show_trace(NULL, regs, ®s->sp, regs->bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 203 | * This gets run with %bx containing the |
| 204 | * function to call, and %dx containing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | * the "args". |
| 206 | */ |
| 207 | extern void kernel_thread_helper(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * Create a kernel thread |
| 211 | */ |
Jaswinder Singh Rajput | befa9e7 | 2009-01-04 16:18:56 +0530 | [diff] [blame] | 212 | int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
| 214 | struct pt_regs regs; |
| 215 | |
| 216 | memset(®s, 0, sizeof(regs)); |
| 217 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 218 | regs.bx = (unsigned long) fn; |
| 219 | regs.dx = (unsigned long) arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 221 | regs.ds = __USER_DS; |
| 222 | regs.es = __USER_DS; |
| 223 | regs.fs = __KERNEL_PERCPU; |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 224 | regs.gs = __KERNEL_STACK_CANARY; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 225 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | /* Ok, create the new process.. */ |
Andi Kleen | 8cf2c51 | 2006-10-21 18:37:02 +0200 | [diff] [blame] | 231 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 233 | EXPORT_SYMBOL(kernel_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | void release_thread(struct task_struct *dead_task) |
| 236 | { |
Zachary Amsden | 2684927 | 2006-01-06 00:11:59 -0800 | [diff] [blame] | 237 | BUG_ON(dead_task->mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | 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 | */ |
| 245 | void prepare_to_copy(struct task_struct *tsk) |
| 246 | { |
| 247 | unlazy_fpu(tsk); |
| 248 | } |
| 249 | |
Alexey Dobriyan | 6f2c55b | 2009-04-02 16:56:59 -0700 | [diff] [blame] | 250 | int copy_thread(unsigned long clone_flags, unsigned long sp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | unsigned long unused, |
Jaswinder Singh Rajput | befa9e7 | 2009-01-04 16:18:56 +0530 | [diff] [blame] | 252 | struct task_struct *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Jaswinder Singh Rajput | befa9e7 | 2009-01-04 16:18:56 +0530 | [diff] [blame] | 254 | struct pt_regs *childregs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | struct task_struct *tsk; |
| 256 | int err; |
| 257 | |
akpm@osdl.org | 07b047f | 2006-01-12 01:05:41 -0800 | [diff] [blame] | 258 | childregs = task_pt_regs(p); |
Alexander Nyberg | f48d966 | 2005-05-05 16:15:03 -0700 | [diff] [blame] | 259 | *childregs = *regs; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 260 | childregs->ax = 0; |
| 261 | childregs->sp = sp; |
Alexander Nyberg | f48d966 | 2005-05-05 16:15:03 -0700 | [diff] [blame] | 262 | |
H. Peter Anvin | faca622 | 2008-01-30 13:31:02 +0100 | [diff] [blame] | 263 | p->thread.sp = (unsigned long) childregs; |
| 264 | p->thread.sp0 = (unsigned long) (childregs+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
H. Peter Anvin | faca622 | 2008-01-30 13:31:02 +0100 | [diff] [blame] | 266 | p->thread.ip = (unsigned long) ret_from_fork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Tejun Heo | d9a89a2 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 268 | task_user_gs(p) = get_user_gs(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
K.Prasad | 66cb591 | 2009-06-01 23:44:55 +0530 | [diff] [blame^] | 270 | p->thread.io_bitmap_ptr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | tsk = current; |
K.Prasad | 66cb591 | 2009-06-01 23:44:55 +0530 | [diff] [blame^] | 272 | 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 Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 277 | if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) { |
Alexey Dobriyan | 52978be | 2006-09-30 23:27:21 -0700 | [diff] [blame] | 278 | p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr, |
| 279 | IO_BITMAP_BYTES, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | if (!p->thread.io_bitmap_ptr) { |
| 281 | p->thread.io_bitmap_max = 0; |
| 282 | return -ENOMEM; |
| 283 | } |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 284 | set_tsk_thread_flag(p, TIF_IO_BITMAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 287 | err = 0; |
| 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | /* |
| 290 | * Set a new TLS for the child thread? |
| 291 | */ |
Roland McGrath | efd1ca5 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 292 | if (clone_flags & CLONE_SETTLS) |
| 293 | err = do_set_thread_area(p, -1, |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 294 | (struct user_desc __user *)childregs->si, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
K.Prasad | 66cb591 | 2009-06-01 23:44:55 +0530 | [diff] [blame^] | 296 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | if (err && p->thread.io_bitmap_ptr) { |
| 298 | kfree(p->thread.io_bitmap_ptr); |
| 299 | p->thread.io_bitmap_max = 0; |
| 300 | } |
K.Prasad | 66cb591 | 2009-06-01 23:44:55 +0530 | [diff] [blame^] | 301 | if (err) |
| 302 | flush_thread_hw_breakpoint(p); |
Markus Metzger | bf53de9 | 2008-12-19 15:10:24 +0100 | [diff] [blame] | 303 | |
Markus Metzger | 2311f0d | 2009-04-03 16:43:46 +0200 | [diff] [blame] | 304 | clear_tsk_thread_flag(p, TIF_DS_AREA_MSR); |
| 305 | p->thread.ds_ctx = NULL; |
Markus Metzger | bf53de9 | 2008-12-19 15:10:24 +0100 | [diff] [blame] | 306 | |
| 307 | clear_tsk_thread_flag(p, TIF_DEBUGCTLMSR); |
| 308 | p->thread.debugctlmsr = 0; |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | return err; |
| 311 | } |
| 312 | |
Ingo Molnar | 513ad84 | 2008-02-21 05:18:40 +0100 | [diff] [blame] | 313 | void |
| 314 | start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp) |
| 315 | { |
Tejun Heo | d9a89a2 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 316 | set_user_gs(regs, 0); |
Ingo Molnar | 513ad84 | 2008-02-21 05:18:40 +0100 | [diff] [blame] | 317 | 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 Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 325 | /* |
| 326 | * Free the old FP and other extended state |
| 327 | */ |
| 328 | free_thread_xstate(current); |
Ingo Molnar | 513ad84 | 2008-02-21 05:18:40 +0100 | [diff] [blame] | 329 | } |
| 330 | EXPORT_SYMBOL_GPL(start_thread); |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 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 Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 356 | * The return value (in %ax) will be the "prev" task after |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | * the task-switch, and shows up in ret_from_fork in entry.S, |
| 358 | * for example. |
| 359 | */ |
Frederic Weisbecker | 8b96f01 | 2008-12-06 03:40:00 +0100 | [diff] [blame] | 360 | __notrace_funcgraph struct task_struct * |
| 361 | __switch_to(struct task_struct *prev_p, struct task_struct *next_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
| 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 Ebbert | acc2076 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 372 | |
| 373 | /* we're going to use this soon, after a few expensive things */ |
| 374 | if (next_p->fpu_counter > 5) |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 375 | prefetch(next->xstate); |
Chuck Ebbert | acc2076 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | /* |
Zachary Amsden | e7a2ff5 | 2005-09-03 15:56:39 -0700 | [diff] [blame] | 378 | * Reload esp0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | */ |
H. Peter Anvin | faca622 | 2008-01-30 13:31:02 +0100 | [diff] [blame] | 380 | load_sp0(tss, next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | /* |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 383 | * Save away %gs. No need to save %fs, as it was saved on the |
Jeremy Fitzhardinge | f95d47c | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 384 | * 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 Amsden | e7a2ff5 | 2005-09-03 15:56:39 -0700 | [diff] [blame] | 391 | */ |
Tejun Heo | ccbeed3 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 392 | lazy_save_gs(prev->gs); |
Zachary Amsden | e7a2ff5 | 2005-09-03 15:56:39 -0700 | [diff] [blame] | 393 | |
| 394 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * Load the per-thread Thread-Local Storage descriptor. |
| 396 | */ |
| 397 | load_TLS(next, cpu); |
| 398 | |
| 399 | /* |
Zachary Amsden | 8b15114 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 400 | * 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 Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 409 | * Now maybe handle debug registers and/or IO bitmaps |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | */ |
Andrea Arcangeli | cf99aba | 2007-07-15 23:41:33 -0700 | [diff] [blame] | 411 | 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 Arcangeli | ffaa8bd | 2005-06-27 14:36:36 -0700 | [diff] [blame] | 414 | |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 415 | /* |
| 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 Ebbert | acc2076 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 424 | /* 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 Siddha | 870568b | 2008-06-02 15:57:27 -0700 | [diff] [blame] | 427 | * |
| 428 | * tsk_used_math() checks prevent calling math_state_restore(), |
| 429 | * which can sleep in the case of !tsk_used_math() |
Chuck Ebbert | acc2076 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 430 | */ |
Suresh Siddha | 870568b | 2008-06-02 15:57:27 -0700 | [diff] [blame] | 431 | if (tsk_used_math(next_p) && next_p->fpu_counter > 5) |
Chuck Ebbert | acc2076 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 432 | math_state_restore(); |
| 433 | |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 434 | /* |
| 435 | * Restore %gs if needed (which is common) |
| 436 | */ |
| 437 | if (prev->gs | next->gs) |
Tejun Heo | ccbeed3 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 438 | lazy_load_gs(next->gs); |
Zachary Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 439 | |
Ingo Molnar | 6dbde35 | 2009-01-15 22:15:53 +0900 | [diff] [blame] | 440 | percpu_write(current_task, next_p); |
K.Prasad | 66cb591 | 2009-06-01 23:44:55 +0530 | [diff] [blame^] | 441 | /* |
| 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 Amsden | 9226d12 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | return prev_p; |
| 460 | } |
| 461 | |
Brian Gerst | b12bdaf | 2009-02-11 16:43:58 -0500 | [diff] [blame] | 462 | int sys_clone(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
Brian Gerst | b12bdaf | 2009-02-11 16:43:58 -0500 | [diff] [blame] | 464 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (!newsp) |
Brian Gerst | 253f29a | 2009-02-10 09:51:46 -0500 | [diff] [blame] | 473 | newsp = regs->sp; |
| 474 | return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, child_tidptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | * sys_execve() executes a new program. |
| 479 | */ |
Brian Gerst | b12bdaf | 2009-02-11 16:43:58 -0500 | [diff] [blame] | 480 | int sys_execve(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { |
| 482 | int error; |
Jaswinder Singh Rajput | befa9e7 | 2009-01-04 16:18:56 +0530 | [diff] [blame] | 483 | char *filename; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
Brian Gerst | b12bdaf | 2009-02-11 16:43:58 -0500 | [diff] [blame] | 485 | filename = getname((char __user *) regs->bx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | error = PTR_ERR(filename); |
| 487 | if (IS_ERR(filename)) |
| 488 | goto out; |
Brian Gerst | b12bdaf | 2009-02-11 16:43:58 -0500 | [diff] [blame] | 489 | error = do_execve(filename, |
| 490 | (char __user * __user *) regs->cx, |
| 491 | (char __user * __user *) regs->dx, |
| 492 | regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | if (error == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | /* Make sure we don't return using sysenter.. */ |
| 495 | set_thread_flag(TIF_IRET); |
| 496 | } |
| 497 | putname(filename); |
| 498 | out: |
| 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 | |
| 505 | unsigned long get_wchan(struct task_struct *p) |
| 506 | { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 507 | unsigned long bp, sp, ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | unsigned long stack_page; |
| 509 | int count = 0; |
| 510 | if (!p || p == current || p->state == TASK_RUNNING) |
| 511 | return 0; |
Al Viro | 65e0fdf | 2006-01-12 01:05:41 -0800 | [diff] [blame] | 512 | stack_page = (unsigned long)task_stack_page(p); |
H. Peter Anvin | faca622 | 2008-01-30 13:31:02 +0100 | [diff] [blame] | 513 | sp = p->thread.sp; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 514 | if (!stack_page || sp < stack_page || sp > top_esp+stack_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | return 0; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 516 | /* include/asm-i386/system.h:switch_to() pushes bp last. */ |
| 517 | bp = *(unsigned long *) sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | do { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 519 | if (bp < stack_page || bp > top_ebp+stack_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return 0; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 521 | ip = *(unsigned long *) (bp+4); |
| 522 | if (!in_sched_functions(ip)) |
| 523 | return ip; |
| 524 | bp = *(unsigned long *) bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } while (count++ < 16); |
| 526 | return 0; |
| 527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | unsigned long arch_align_stack(unsigned long sp) |
| 530 | { |
Andi Kleen | c16b63e0 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 531 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | sp -= get_random_int() % 8192; |
| 533 | return sp & ~0xf; |
| 534 | } |
Jiri Kosina | c1d171a | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 535 | |
| 536 | unsigned 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 | } |