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