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