Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/i386/kernel/process.c |
| 3 | * |
| 4 | * Copyright (C) 1995 Linus Torvalds |
| 5 | * |
| 6 | * Pentium III FXSR, SSE support |
| 7 | * Gareth Hughes <gareth@valinux.com>, May 2000 |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * This file handles the architecture-dependent parts of process handling.. |
| 12 | */ |
| 13 | |
| 14 | #include <stdarg.h> |
| 15 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 16 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/elfcore.h> |
| 23 | #include <linux/smp.h> |
| 24 | #include <linux/smp_lock.h> |
| 25 | #include <linux/stddef.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | #include <linux/user.h> |
| 29 | #include <linux/a.out.h> |
| 30 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/utsname.h> |
| 32 | #include <linux/delay.h> |
| 33 | #include <linux/reboot.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/mc146818rtc.h> |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/kallsyms.h> |
| 38 | #include <linux/ptrace.h> |
| 39 | #include <linux/random.h> |
Andi Kleen | c16b63e | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 40 | #include <linux/personality.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> |
Matt Mackall | 64ca900 | 2006-01-08 01:05:26 -0800 | [diff] [blame] | 50 | #include <asm/vm86.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #ifdef CONFIG_MATH_EMULATION |
| 52 | #include <asm/math_emu.h> |
| 53 | #endif |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/err.h> |
| 56 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 57 | #include <asm/tlbflush.h> |
| 58 | #include <asm/cpu.h> |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | asmlinkage void ret_from_fork(void) __asm__("ret_from_fork"); |
| 61 | |
| 62 | static int hlt_counter; |
| 63 | |
| 64 | unsigned long boot_option_idle_override = 0; |
| 65 | EXPORT_SYMBOL(boot_option_idle_override); |
| 66 | |
| 67 | /* |
| 68 | * Return saved PC of a blocked thread. |
| 69 | */ |
| 70 | unsigned long thread_saved_pc(struct task_struct *tsk) |
| 71 | { |
| 72 | return ((unsigned long *)tsk->thread.esp)[3]; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Powermanagement idle function, if any.. |
| 77 | */ |
| 78 | void (*pm_idle)(void); |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 79 | EXPORT_SYMBOL(pm_idle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | static DEFINE_PER_CPU(unsigned int, cpu_idle_state); |
| 81 | |
| 82 | void disable_hlt(void) |
| 83 | { |
| 84 | hlt_counter++; |
| 85 | } |
| 86 | |
| 87 | EXPORT_SYMBOL(disable_hlt); |
| 88 | |
| 89 | void enable_hlt(void) |
| 90 | { |
| 91 | hlt_counter--; |
| 92 | } |
| 93 | |
| 94 | EXPORT_SYMBOL(enable_hlt); |
| 95 | |
| 96 | /* |
| 97 | * We use this if we don't have any better |
| 98 | * idle routine.. |
| 99 | */ |
| 100 | void default_idle(void) |
| 101 | { |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 102 | local_irq_enable(); |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if (!hlt_counter && boot_cpu_data.hlt_works_ok) { |
Andi Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 105 | current_thread_info()->status &= ~TS_POLLING; |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 106 | smp_mb__after_clear_bit(); |
| 107 | while (!need_resched()) { |
| 108 | local_irq_disable(); |
| 109 | if (!need_resched()) |
| 110 | safe_halt(); |
| 111 | else |
| 112 | local_irq_enable(); |
| 113 | } |
Andi Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 114 | current_thread_info()->status |= TS_POLLING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } else { |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 116 | while (!need_resched()) |
| 117 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 120 | #ifdef CONFIG_APM_MODULE |
| 121 | EXPORT_SYMBOL(default_idle); |
| 122 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * On SMP it's slightly faster (but much more power-consuming!) |
| 126 | * to poll the ->work.need_resched flag instead of waiting for the |
| 127 | * cross-CPU IPI to arrive. Use this option with caution. |
| 128 | */ |
| 129 | static void poll_idle (void) |
| 130 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | local_irq_enable(); |
| 132 | |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 133 | asm volatile( |
| 134 | "2:" |
| 135 | "testl %0, %1;" |
| 136 | "rep; nop;" |
| 137 | "je 2b;" |
| 138 | : : "i"(_TIF_NEED_RESCHED), "m" (current_thread_info()->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 141 | #ifdef CONFIG_HOTPLUG_CPU |
| 142 | #include <asm/nmi.h> |
| 143 | /* We don't actually take CPU down, just spin without interrupts. */ |
| 144 | static inline void play_dead(void) |
| 145 | { |
Li Shaohua | e1367da | 2005-06-25 14:54:56 -0700 | [diff] [blame] | 146 | /* This must be done before dead CPU ack */ |
| 147 | cpu_exit_clear(); |
| 148 | wbinvd(); |
| 149 | mb(); |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 150 | /* Ack it */ |
| 151 | __get_cpu_var(cpu_state) = CPU_DEAD; |
| 152 | |
Li Shaohua | e1367da | 2005-06-25 14:54:56 -0700 | [diff] [blame] | 153 | /* |
| 154 | * With physical CPU hotplug, we should halt the cpu |
| 155 | */ |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 156 | local_irq_disable(); |
Li Shaohua | e1367da | 2005-06-25 14:54:56 -0700 | [diff] [blame] | 157 | while (1) |
Zachary Amsden | f2ab446 | 2005-09-03 15:56:42 -0700 | [diff] [blame] | 158 | halt(); |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 159 | } |
| 160 | #else |
| 161 | static inline void play_dead(void) |
| 162 | { |
| 163 | BUG(); |
| 164 | } |
| 165 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* |
| 168 | * The idle thread. There's no useful work to be |
| 169 | * done, so just try to conserve power and have a |
| 170 | * low exit latency (ie sit in a loop waiting for |
| 171 | * somebody to say that they'd like to reschedule) |
| 172 | */ |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 173 | void cpu_idle(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 175 | int cpu = smp_processor_id(); |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 176 | |
Andi Kleen | 495ab9c | 2006-06-26 13:59:11 +0200 | [diff] [blame] | 177 | current_thread_info()->status |= TS_POLLING; |
Nick Piggin | 64c7c8f | 2005-11-08 21:39:04 -0800 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | /* endless idle loop with no priority at all */ |
| 180 | while (1) { |
| 181 | while (!need_resched()) { |
| 182 | void (*idle)(void); |
| 183 | |
| 184 | if (__get_cpu_var(cpu_idle_state)) |
| 185 | __get_cpu_var(cpu_idle_state) = 0; |
| 186 | |
| 187 | rmb(); |
| 188 | idle = pm_idle; |
| 189 | |
| 190 | if (!idle) |
| 191 | idle = default_idle; |
| 192 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 193 | if (cpu_is_offline(cpu)) |
| 194 | play_dead(); |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | __get_cpu_var(irq_stat).idle_timestamp = jiffies; |
| 197 | idle(); |
| 198 | } |
Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 199 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | schedule(); |
Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 201 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | void cpu_idle_wait(void) |
| 206 | { |
| 207 | unsigned int cpu, this_cpu = get_cpu(); |
Ingo Molnar | dc1829a | 2006-11-17 14:26:18 +0100 | [diff] [blame] | 208 | cpumask_t map, tmp = current->cpus_allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | set_cpus_allowed(current, cpumask_of_cpu(this_cpu)); |
| 211 | put_cpu(); |
| 212 | |
| 213 | cpus_clear(map); |
| 214 | for_each_online_cpu(cpu) { |
| 215 | per_cpu(cpu_idle_state, cpu) = 1; |
| 216 | cpu_set(cpu, map); |
| 217 | } |
| 218 | |
| 219 | __get_cpu_var(cpu_idle_state) = 0; |
| 220 | |
| 221 | wmb(); |
| 222 | do { |
| 223 | ssleep(1); |
| 224 | for_each_online_cpu(cpu) { |
| 225 | if (cpu_isset(cpu, map) && !per_cpu(cpu_idle_state, cpu)) |
| 226 | cpu_clear(cpu, map); |
| 227 | } |
| 228 | cpus_and(map, map, cpu_online_map); |
| 229 | } while (!cpus_empty(map)); |
Ingo Molnar | dc1829a | 2006-11-17 14:26:18 +0100 | [diff] [blame] | 230 | |
| 231 | set_cpus_allowed(current, tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | EXPORT_SYMBOL_GPL(cpu_idle_wait); |
| 234 | |
| 235 | /* |
| 236 | * This uses new MONITOR/MWAIT instructions on P4 processors with PNI, |
| 237 | * which can obviate IPI to trigger checking of need_resched. |
| 238 | * We execute MONITOR against need_resched and enter optimized wait state |
| 239 | * through MWAIT. Whenever someone changes need_resched, we would be woken |
| 240 | * up from MWAIT (without an IPI). |
Venkatesh Pallipadi | 991528d | 2006-09-25 16:28:13 -0700 | [diff] [blame] | 241 | * |
| 242 | * New with Core Duo processors, MWAIT can take some hints based on CPU |
| 243 | * capability. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | */ |
Venkatesh Pallipadi | 991528d | 2006-09-25 16:28:13 -0700 | [diff] [blame] | 245 | void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) |
| 246 | { |
| 247 | if (!need_resched()) { |
| 248 | __monitor((void *)¤t_thread_info()->flags, 0, 0); |
| 249 | smp_mb(); |
| 250 | if (!need_resched()) |
| 251 | __mwait(eax, ecx); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* Default MONITOR/MWAIT with no hints, used for default C1 state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | static void mwait_idle(void) |
| 257 | { |
| 258 | local_irq_enable(); |
Venkatesh Pallipadi | 991528d | 2006-09-25 16:28:13 -0700 | [diff] [blame] | 259 | while (!need_resched()) |
| 260 | mwait_idle_with_hints(0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Li Shaohua | 0bb3184 | 2005-06-25 14:54:55 -0700 | [diff] [blame] | 263 | void __devinit select_idle_routine(const struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
| 265 | if (cpu_has(c, X86_FEATURE_MWAIT)) { |
| 266 | printk("monitor/mwait feature present.\n"); |
| 267 | /* |
| 268 | * Skip, if setup has overridden idle. |
| 269 | * One CPU supports mwait => All CPUs supports mwait |
| 270 | */ |
| 271 | if (!pm_idle) { |
| 272 | printk("using mwait in idle threads.\n"); |
| 273 | pm_idle = mwait_idle; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | static int __init idle_setup (char *str) |
| 279 | { |
| 280 | if (!strncmp(str, "poll", 4)) { |
| 281 | printk("using polling idle threads.\n"); |
| 282 | pm_idle = poll_idle; |
| 283 | #ifdef CONFIG_X86_SMP |
| 284 | if (smp_num_siblings > 1) |
| 285 | printk("WARNING: polling idle and HT enabled, performance may degrade.\n"); |
| 286 | #endif |
| 287 | } else if (!strncmp(str, "halt", 4)) { |
| 288 | printk("using halt in idle threads.\n"); |
| 289 | pm_idle = default_idle; |
| 290 | } |
| 291 | |
| 292 | boot_option_idle_override = 1; |
| 293 | return 1; |
| 294 | } |
| 295 | |
| 296 | __setup("idle=", idle_setup); |
| 297 | |
| 298 | void show_regs(struct pt_regs * regs) |
| 299 | { |
| 300 | unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L; |
| 301 | |
| 302 | printk("\n"); |
| 303 | printk("Pid: %d, comm: %20s\n", current->pid, current->comm); |
| 304 | printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id()); |
| 305 | print_symbol("EIP is at %s\n", regs->eip); |
| 306 | |
Jan Beulich | db753bd | 2006-03-23 02:59:46 -0800 | [diff] [blame] | 307 | if (user_mode_vm(regs)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp); |
Chuck Ebbert | b53e8f6 | 2006-02-04 23:28:04 -0800 | [diff] [blame] | 309 | printk(" EFLAGS: %08lx %s (%s %.*s)\n", |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 310 | regs->eflags, print_tainted(), init_utsname()->release, |
| 311 | (int)strcspn(init_utsname()->version, " "), |
| 312 | init_utsname()->version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n", |
| 314 | regs->eax,regs->ebx,regs->ecx,regs->edx); |
| 315 | printk("ESI: %08lx EDI: %08lx EBP: %08lx", |
| 316 | regs->esi, regs->edi, regs->ebp); |
| 317 | printk(" DS: %04x ES: %04x\n", |
| 318 | 0xffff & regs->xds,0xffff & regs->xes); |
| 319 | |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 320 | cr0 = read_cr0(); |
| 321 | cr2 = read_cr2(); |
| 322 | cr3 = read_cr3(); |
Zachary Amsden | ff6e8c0 | 2006-01-06 00:11:50 -0800 | [diff] [blame] | 323 | cr4 = read_cr4_safe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", cr0, cr2, cr3, cr4); |
Jan Beulich | 176a271 | 2006-06-26 13:57:41 +0200 | [diff] [blame] | 325 | show_trace(NULL, regs, ®s->esp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* |
| 329 | * This gets run with %ebx containing the |
| 330 | * function to call, and %edx containing |
| 331 | * the "args". |
| 332 | */ |
| 333 | extern void kernel_thread_helper(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * Create a kernel thread |
| 337 | */ |
| 338 | int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags) |
| 339 | { |
| 340 | struct pt_regs regs; |
| 341 | |
| 342 | memset(®s, 0, sizeof(regs)); |
| 343 | |
| 344 | regs.ebx = (unsigned long) fn; |
| 345 | regs.edx = (unsigned long) arg; |
| 346 | |
| 347 | regs.xds = __USER_DS; |
| 348 | regs.xes = __USER_DS; |
| 349 | regs.orig_eax = -1; |
| 350 | regs.eip = (unsigned long) kernel_thread_helper; |
Rusty Russell | 78be370 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 351 | regs.xcs = __KERNEL_CS | get_kernel_rpl(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2; |
| 353 | |
| 354 | /* Ok, create the new process.. */ |
Andi Kleen | 8cf2c51 | 2006-10-21 18:37:02 +0200 | [diff] [blame] | 355 | 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] | 356 | } |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 357 | EXPORT_SYMBOL(kernel_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | /* |
| 360 | * Free current thread data structures etc.. |
| 361 | */ |
| 362 | void exit_thread(void) |
| 363 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* The process may have allocated an io port bitmap... nuke it. */ |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 365 | if (unlikely(test_thread_flag(TIF_IO_BITMAP))) { |
| 366 | struct task_struct *tsk = current; |
| 367 | struct thread_struct *t = &tsk->thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | int cpu = get_cpu(); |
| 369 | struct tss_struct *tss = &per_cpu(init_tss, cpu); |
| 370 | |
| 371 | kfree(t->io_bitmap_ptr); |
| 372 | t->io_bitmap_ptr = NULL; |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 373 | clear_thread_flag(TIF_IO_BITMAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | /* |
| 375 | * Careful, clear this in the TSS too: |
| 376 | */ |
| 377 | memset(tss->io_bitmap, 0xff, tss->io_bitmap_max); |
| 378 | t->io_bitmap_max = 0; |
| 379 | tss->io_bitmap_owner = NULL; |
| 380 | tss->io_bitmap_max = 0; |
| 381 | tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET; |
| 382 | put_cpu(); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void flush_thread(void) |
| 387 | { |
| 388 | struct task_struct *tsk = current; |
| 389 | |
| 390 | memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8); |
| 391 | memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array)); |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 392 | clear_tsk_thread_flag(tsk, TIF_DEBUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | /* |
| 394 | * Forget coprocessor state.. |
| 395 | */ |
| 396 | clear_fpu(tsk); |
| 397 | clear_used_math(); |
| 398 | } |
| 399 | |
| 400 | void release_thread(struct task_struct *dead_task) |
| 401 | { |
Zachary Amsden | 2684927 | 2006-01-06 00:11:59 -0800 | [diff] [blame] | 402 | BUG_ON(dead_task->mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | release_vm86_irqs(dead_task); |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * This gets called before we allocate a new thread and copy |
| 408 | * the current task into it. |
| 409 | */ |
| 410 | void prepare_to_copy(struct task_struct *tsk) |
| 411 | { |
| 412 | unlazy_fpu(tsk); |
| 413 | } |
| 414 | |
| 415 | int copy_thread(int nr, unsigned long clone_flags, unsigned long esp, |
| 416 | unsigned long unused, |
| 417 | struct task_struct * p, struct pt_regs * regs) |
| 418 | { |
| 419 | struct pt_regs * childregs; |
| 420 | struct task_struct *tsk; |
| 421 | int err; |
| 422 | |
akpm@osdl.org | 07b047f | 2006-01-12 01:05:41 -0800 | [diff] [blame] | 423 | childregs = task_pt_regs(p); |
Alexander Nyberg | f48d966 | 2005-05-05 16:15:03 -0700 | [diff] [blame] | 424 | *childregs = *regs; |
| 425 | childregs->eax = 0; |
| 426 | childregs->esp = esp; |
| 427 | |
| 428 | p->thread.esp = (unsigned long) childregs; |
| 429 | p->thread.esp0 = (unsigned long) (childregs+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | p->thread.eip = (unsigned long) ret_from_fork; |
| 432 | |
| 433 | savesegment(fs,p->thread.fs); |
| 434 | savesegment(gs,p->thread.gs); |
| 435 | |
| 436 | tsk = current; |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 437 | if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) { |
Alexey Dobriyan | 52978be | 2006-09-30 23:27:21 -0700 | [diff] [blame] | 438 | p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr, |
| 439 | IO_BITMAP_BYTES, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (!p->thread.io_bitmap_ptr) { |
| 441 | p->thread.io_bitmap_max = 0; |
| 442 | return -ENOMEM; |
| 443 | } |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 444 | set_tsk_thread_flag(p, TIF_IO_BITMAP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Set a new TLS for the child thread? |
| 449 | */ |
| 450 | if (clone_flags & CLONE_SETTLS) { |
| 451 | struct desc_struct *desc; |
| 452 | struct user_desc info; |
| 453 | int idx; |
| 454 | |
| 455 | err = -EFAULT; |
| 456 | if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info))) |
| 457 | goto out; |
| 458 | err = -EINVAL; |
| 459 | if (LDT_empty(&info)) |
| 460 | goto out; |
| 461 | |
| 462 | idx = info.entry_number; |
| 463 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 464 | goto out; |
| 465 | |
| 466 | desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN; |
| 467 | desc->a = LDT_entry_a(&info); |
| 468 | desc->b = LDT_entry_b(&info); |
| 469 | } |
| 470 | |
| 471 | err = 0; |
| 472 | out: |
| 473 | if (err && p->thread.io_bitmap_ptr) { |
| 474 | kfree(p->thread.io_bitmap_ptr); |
| 475 | p->thread.io_bitmap_max = 0; |
| 476 | } |
| 477 | return err; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * fill in the user structure for a core dump.. |
| 482 | */ |
| 483 | void dump_thread(struct pt_regs * regs, struct user * dump) |
| 484 | { |
| 485 | int i; |
| 486 | |
| 487 | /* changed the size calculations - should hopefully work better. lbt */ |
| 488 | dump->magic = CMAGIC; |
| 489 | dump->start_code = 0; |
| 490 | dump->start_stack = regs->esp & ~(PAGE_SIZE - 1); |
| 491 | dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT; |
| 492 | dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT; |
| 493 | dump->u_dsize -= dump->u_tsize; |
| 494 | dump->u_ssize = 0; |
| 495 | for (i = 0; i < 8; i++) |
| 496 | dump->u_debugreg[i] = current->thread.debugreg[i]; |
| 497 | |
| 498 | if (dump->start_stack < TASK_SIZE) |
| 499 | dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT; |
| 500 | |
| 501 | dump->regs.ebx = regs->ebx; |
| 502 | dump->regs.ecx = regs->ecx; |
| 503 | dump->regs.edx = regs->edx; |
| 504 | dump->regs.esi = regs->esi; |
| 505 | dump->regs.edi = regs->edi; |
| 506 | dump->regs.ebp = regs->ebp; |
| 507 | dump->regs.eax = regs->eax; |
| 508 | dump->regs.ds = regs->xds; |
| 509 | dump->regs.es = regs->xes; |
| 510 | savesegment(fs,dump->regs.fs); |
| 511 | savesegment(gs,dump->regs.gs); |
| 512 | dump->regs.orig_eax = regs->orig_eax; |
| 513 | dump->regs.eip = regs->eip; |
| 514 | dump->regs.cs = regs->xcs; |
| 515 | dump->regs.eflags = regs->eflags; |
| 516 | dump->regs.esp = regs->esp; |
| 517 | dump->regs.ss = regs->xss; |
| 518 | |
| 519 | dump->u_fpvalid = dump_fpu (regs, &dump->i387); |
| 520 | } |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 521 | EXPORT_SYMBOL(dump_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | /* |
| 524 | * Capture the user space registers if the task is not running (in user space) |
| 525 | */ |
| 526 | int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs) |
| 527 | { |
akpm@osdl.org | 07b047f | 2006-01-12 01:05:41 -0800 | [diff] [blame] | 528 | struct pt_regs ptregs = *task_pt_regs(tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | ptregs.xcs &= 0xffff; |
| 530 | ptregs.xds &= 0xffff; |
| 531 | ptregs.xes &= 0xffff; |
| 532 | ptregs.xss &= 0xffff; |
| 533 | |
| 534 | elf_core_copy_regs(regs, &ptregs); |
| 535 | |
| 536 | return 1; |
| 537 | } |
| 538 | |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 539 | static noinline void __switch_to_xtra(struct task_struct *next_p, |
| 540 | struct tss_struct *tss) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 542 | struct thread_struct *next; |
| 543 | |
| 544 | next = &next_p->thread; |
| 545 | |
| 546 | if (test_tsk_thread_flag(next_p, TIF_DEBUG)) { |
| 547 | set_debugreg(next->debugreg[0], 0); |
| 548 | set_debugreg(next->debugreg[1], 1); |
| 549 | set_debugreg(next->debugreg[2], 2); |
| 550 | set_debugreg(next->debugreg[3], 3); |
| 551 | /* no 4 and 5 */ |
| 552 | set_debugreg(next->debugreg[6], 6); |
| 553 | set_debugreg(next->debugreg[7], 7); |
| 554 | } |
| 555 | |
| 556 | if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | /* |
| 558 | * Disable the bitmap via an invalid offset. We still cache |
| 559 | * the previous bitmap owner and the IO bitmap contents: |
| 560 | */ |
| 561 | tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET; |
| 562 | return; |
| 563 | } |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | if (likely(next == tss->io_bitmap_owner)) { |
| 566 | /* |
| 567 | * Previous owner of the bitmap (hence the bitmap content) |
| 568 | * matches the next task, we dont have to do anything but |
| 569 | * to set a valid offset in the TSS: |
| 570 | */ |
| 571 | tss->io_bitmap_base = IO_BITMAP_OFFSET; |
| 572 | return; |
| 573 | } |
| 574 | /* |
| 575 | * Lazy TSS's I/O bitmap copy. We set an invalid offset here |
| 576 | * and we let the task to get a GPF in case an I/O instruction |
| 577 | * is performed. The handler of the GPF will verify that the |
| 578 | * faulting task has a valid I/O bitmap and, it true, does the |
| 579 | * real copy and restart the instruction. This will save us |
| 580 | * redundant copies when the currently switched task does not |
| 581 | * perform any I/O during its timeslice. |
| 582 | */ |
| 583 | tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY; |
| 584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | /* |
Andrea Arcangeli | ffaa8bd | 2005-06-27 14:36:36 -0700 | [diff] [blame] | 587 | * This function selects if the context switch from prev to next |
| 588 | * has to tweak the TSC disable bit in the cr4. |
| 589 | */ |
| 590 | static inline void disable_tsc(struct task_struct *prev_p, |
| 591 | struct task_struct *next_p) |
| 592 | { |
| 593 | struct thread_info *prev, *next; |
| 594 | |
| 595 | /* |
| 596 | * gcc should eliminate the ->thread_info dereference if |
| 597 | * has_secure_computing returns 0 at compile time (SECCOMP=n). |
| 598 | */ |
Al Viro | 06b425d | 2006-01-12 01:05:40 -0800 | [diff] [blame] | 599 | prev = task_thread_info(prev_p); |
| 600 | next = task_thread_info(next_p); |
Andrea Arcangeli | ffaa8bd | 2005-06-27 14:36:36 -0700 | [diff] [blame] | 601 | |
| 602 | if (has_secure_computing(prev) || has_secure_computing(next)) { |
| 603 | /* slow path here */ |
| 604 | if (has_secure_computing(prev) && |
| 605 | !has_secure_computing(next)) { |
| 606 | write_cr4(read_cr4() & ~X86_CR4_TSD); |
| 607 | } else if (!has_secure_computing(prev) && |
| 608 | has_secure_computing(next)) |
| 609 | write_cr4(read_cr4() | X86_CR4_TSD); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | * switch_to(x,yn) should switch tasks from x to y. |
| 615 | * |
| 616 | * We fsave/fwait so that an exception goes off at the right time |
| 617 | * (as a call from the fsave or fwait in effect) rather than to |
| 618 | * the wrong process. Lazy FP saving no longer makes any sense |
| 619 | * with modern CPU's, and this simplifies a lot of things (SMP |
| 620 | * and UP become the same). |
| 621 | * |
| 622 | * NOTE! We used to use the x86 hardware context switching. The |
| 623 | * reason for not using it any more becomes apparent when you |
| 624 | * try to recover gracefully from saved state that is no longer |
| 625 | * valid (stale segment register values in particular). With the |
| 626 | * hardware task-switch, there is no way to fix up bad state in |
| 627 | * a reasonable manner. |
| 628 | * |
| 629 | * The fact that Intel documents the hardware task-switching to |
| 630 | * be slow is a fairly red herring - this code is not noticeably |
| 631 | * faster. However, there _is_ some room for improvement here, |
| 632 | * so the performance issues may eventually be a valid point. |
| 633 | * More important, however, is the fact that this allows us much |
| 634 | * more flexibility. |
| 635 | * |
| 636 | * The return value (in %eax) will be the "prev" task after |
| 637 | * the task-switch, and shows up in ret_from_fork in entry.S, |
| 638 | * for example. |
| 639 | */ |
| 640 | struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p) |
| 641 | { |
| 642 | struct thread_struct *prev = &prev_p->thread, |
| 643 | *next = &next_p->thread; |
| 644 | int cpu = smp_processor_id(); |
| 645 | struct tss_struct *tss = &per_cpu(init_tss, cpu); |
| 646 | |
| 647 | /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */ |
| 648 | |
| 649 | __unlazy_fpu(prev_p); |
| 650 | |
Chuck Ebbert | acc2076 | 2006-12-07 02:14:01 +0100 | [diff] [blame^] | 651 | |
| 652 | /* we're going to use this soon, after a few expensive things */ |
| 653 | if (next_p->fpu_counter > 5) |
| 654 | prefetch(&next->i387.fxsave); |
| 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | /* |
Zachary Amsden | e7a2ff5 | 2005-09-03 15:56:39 -0700 | [diff] [blame] | 657 | * Reload esp0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | */ |
| 659 | load_esp0(tss, next); |
| 660 | |
| 661 | /* |
Zachary Amsden | e7a2ff5 | 2005-09-03 15:56:39 -0700 | [diff] [blame] | 662 | * Save away %fs and %gs. No need to save %es and %ds, as |
| 663 | * those are always kernel segments while inside the kernel. |
| 664 | * Doing this before setting the new TLS descriptors avoids |
| 665 | * the situation where we temporarily have non-reloadable |
| 666 | * segments in %fs and %gs. This could be an issue if the |
| 667 | * NMI handler ever used %fs or %gs (it does not today), or |
| 668 | * if the kernel is running inside of a hypervisor layer. |
| 669 | */ |
| 670 | savesegment(fs, prev->fs); |
| 671 | savesegment(gs, prev->gs); |
| 672 | |
| 673 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | * Load the per-thread Thread-Local Storage descriptor. |
| 675 | */ |
| 676 | load_TLS(next, cpu); |
| 677 | |
| 678 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | * Restore %fs and %gs if needed. |
Linus Torvalds | b339a18 | 2005-07-22 15:23:47 -0400 | [diff] [blame] | 680 | * |
| 681 | * Glibc normally makes %fs be zero, and %gs is one of |
| 682 | * the TLS segments. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | */ |
Linus Torvalds | b339a18 | 2005-07-22 15:23:47 -0400 | [diff] [blame] | 684 | if (unlikely(prev->fs | next->fs)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | loadsegment(fs, next->fs); |
Linus Torvalds | b339a18 | 2005-07-22 15:23:47 -0400 | [diff] [blame] | 686 | |
| 687 | if (prev->gs | next->gs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | loadsegment(gs, next->gs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
| 690 | /* |
Zachary Amsden | a520112 | 2005-09-03 15:56:44 -0700 | [diff] [blame] | 691 | * Restore IOPL if needed. |
| 692 | */ |
| 693 | if (unlikely(prev->iopl != next->iopl)) |
| 694 | set_iopl_mask(next->iopl); |
| 695 | |
| 696 | /* |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 697 | * Now maybe handle debug registers and/or IO bitmaps |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | */ |
Chuck Ebbert | facf014 | 2006-07-25 16:15:16 -0400 | [diff] [blame] | 699 | if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW) |
| 700 | || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))) |
Stephane Eranian | b3cf257 | 2006-07-09 21:12:39 -0400 | [diff] [blame] | 701 | __switch_to_xtra(next_p, tss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
Andrea Arcangeli | ffaa8bd | 2005-06-27 14:36:36 -0700 | [diff] [blame] | 703 | disable_tsc(prev_p, next_p); |
| 704 | |
Chuck Ebbert | acc2076 | 2006-12-07 02:14:01 +0100 | [diff] [blame^] | 705 | /* If the task has used fpu the last 5 timeslices, just do a full |
| 706 | * restore of the math state immediately to avoid the trap; the |
| 707 | * chances of needing FPU soon are obviously high now |
| 708 | */ |
| 709 | if (next_p->fpu_counter > 5) |
| 710 | math_state_restore(); |
| 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | return prev_p; |
| 713 | } |
| 714 | |
| 715 | asmlinkage int sys_fork(struct pt_regs regs) |
| 716 | { |
| 717 | return do_fork(SIGCHLD, regs.esp, ®s, 0, NULL, NULL); |
| 718 | } |
| 719 | |
| 720 | asmlinkage int sys_clone(struct pt_regs regs) |
| 721 | { |
| 722 | unsigned long clone_flags; |
| 723 | unsigned long newsp; |
| 724 | int __user *parent_tidptr, *child_tidptr; |
| 725 | |
| 726 | clone_flags = regs.ebx; |
| 727 | newsp = regs.ecx; |
| 728 | parent_tidptr = (int __user *)regs.edx; |
| 729 | child_tidptr = (int __user *)regs.edi; |
| 730 | if (!newsp) |
| 731 | newsp = regs.esp; |
| 732 | return do_fork(clone_flags, newsp, ®s, 0, parent_tidptr, child_tidptr); |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * This is trivial, and on the face of it looks like it |
| 737 | * could equally well be done in user mode. |
| 738 | * |
| 739 | * Not so, for quite unobvious reasons - register pressure. |
| 740 | * In user mode vfork() cannot have a stack frame, and if |
| 741 | * done by calling the "clone()" system call directly, you |
| 742 | * do not have enough call-clobbered registers to hold all |
| 743 | * the information you need. |
| 744 | */ |
| 745 | asmlinkage int sys_vfork(struct pt_regs regs) |
| 746 | { |
| 747 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, ®s, 0, NULL, NULL); |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * sys_execve() executes a new program. |
| 752 | */ |
| 753 | asmlinkage int sys_execve(struct pt_regs regs) |
| 754 | { |
| 755 | int error; |
| 756 | char * filename; |
| 757 | |
| 758 | filename = getname((char __user *) regs.ebx); |
| 759 | error = PTR_ERR(filename); |
| 760 | if (IS_ERR(filename)) |
| 761 | goto out; |
| 762 | error = do_execve(filename, |
| 763 | (char __user * __user *) regs.ecx, |
| 764 | (char __user * __user *) regs.edx, |
| 765 | ®s); |
| 766 | if (error == 0) { |
| 767 | task_lock(current); |
| 768 | current->ptrace &= ~PT_DTRACE; |
| 769 | task_unlock(current); |
| 770 | /* Make sure we don't return using sysenter.. */ |
| 771 | set_thread_flag(TIF_IRET); |
| 772 | } |
| 773 | putname(filename); |
| 774 | out: |
| 775 | return error; |
| 776 | } |
| 777 | |
| 778 | #define top_esp (THREAD_SIZE - sizeof(unsigned long)) |
| 779 | #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long)) |
| 780 | |
| 781 | unsigned long get_wchan(struct task_struct *p) |
| 782 | { |
| 783 | unsigned long ebp, esp, eip; |
| 784 | unsigned long stack_page; |
| 785 | int count = 0; |
| 786 | if (!p || p == current || p->state == TASK_RUNNING) |
| 787 | return 0; |
Al Viro | 65e0fdf | 2006-01-12 01:05:41 -0800 | [diff] [blame] | 788 | stack_page = (unsigned long)task_stack_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | esp = p->thread.esp; |
| 790 | if (!stack_page || esp < stack_page || esp > top_esp+stack_page) |
| 791 | return 0; |
| 792 | /* include/asm-i386/system.h:switch_to() pushes ebp last. */ |
| 793 | ebp = *(unsigned long *) esp; |
| 794 | do { |
| 795 | if (ebp < stack_page || ebp > top_ebp+stack_page) |
| 796 | return 0; |
| 797 | eip = *(unsigned long *) (ebp+4); |
| 798 | if (!in_sched_functions(eip)) |
| 799 | return eip; |
| 800 | ebp = *(unsigned long *) ebp; |
| 801 | } while (count++ < 16); |
| 802 | return 0; |
| 803 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
| 805 | /* |
| 806 | * sys_alloc_thread_area: get a yet unused TLS descriptor index. |
| 807 | */ |
| 808 | static int get_free_idx(void) |
| 809 | { |
| 810 | struct thread_struct *t = ¤t->thread; |
| 811 | int idx; |
| 812 | |
| 813 | for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++) |
| 814 | if (desc_empty(t->tls_array + idx)) |
| 815 | return idx + GDT_ENTRY_TLS_MIN; |
| 816 | return -ESRCH; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * Set a given TLS descriptor: |
| 821 | */ |
| 822 | asmlinkage int sys_set_thread_area(struct user_desc __user *u_info) |
| 823 | { |
| 824 | struct thread_struct *t = ¤t->thread; |
| 825 | struct user_desc info; |
| 826 | struct desc_struct *desc; |
| 827 | int cpu, idx; |
| 828 | |
| 829 | if (copy_from_user(&info, u_info, sizeof(info))) |
| 830 | return -EFAULT; |
| 831 | idx = info.entry_number; |
| 832 | |
| 833 | /* |
| 834 | * index -1 means the kernel should try to find and |
| 835 | * allocate an empty descriptor: |
| 836 | */ |
| 837 | if (idx == -1) { |
| 838 | idx = get_free_idx(); |
| 839 | if (idx < 0) |
| 840 | return idx; |
| 841 | if (put_user(idx, &u_info->entry_number)) |
| 842 | return -EFAULT; |
| 843 | } |
| 844 | |
| 845 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 846 | return -EINVAL; |
| 847 | |
| 848 | desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN; |
| 849 | |
| 850 | /* |
| 851 | * We must not get preempted while modifying the TLS. |
| 852 | */ |
| 853 | cpu = get_cpu(); |
| 854 | |
| 855 | if (LDT_empty(&info)) { |
| 856 | desc->a = 0; |
| 857 | desc->b = 0; |
| 858 | } else { |
| 859 | desc->a = LDT_entry_a(&info); |
| 860 | desc->b = LDT_entry_b(&info); |
| 861 | } |
| 862 | load_TLS(t, cpu); |
| 863 | |
| 864 | put_cpu(); |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * Get the current Thread-Local Storage area: |
| 871 | */ |
| 872 | |
| 873 | #define GET_BASE(desc) ( \ |
| 874 | (((desc)->a >> 16) & 0x0000ffff) | \ |
| 875 | (((desc)->b << 16) & 0x00ff0000) | \ |
| 876 | ( (desc)->b & 0xff000000) ) |
| 877 | |
| 878 | #define GET_LIMIT(desc) ( \ |
| 879 | ((desc)->a & 0x0ffff) | \ |
| 880 | ((desc)->b & 0xf0000) ) |
| 881 | |
| 882 | #define GET_32BIT(desc) (((desc)->b >> 22) & 1) |
| 883 | #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3) |
| 884 | #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1) |
| 885 | #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1) |
| 886 | #define GET_PRESENT(desc) (((desc)->b >> 15) & 1) |
| 887 | #define GET_USEABLE(desc) (((desc)->b >> 20) & 1) |
| 888 | |
| 889 | asmlinkage int sys_get_thread_area(struct user_desc __user *u_info) |
| 890 | { |
| 891 | struct user_desc info; |
| 892 | struct desc_struct *desc; |
| 893 | int idx; |
| 894 | |
| 895 | if (get_user(idx, &u_info->entry_number)) |
| 896 | return -EFAULT; |
| 897 | if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) |
| 898 | return -EINVAL; |
| 899 | |
Blaisorblade | 71ae18e | 2005-07-27 11:45:18 -0700 | [diff] [blame] | 900 | memset(&info, 0, sizeof(info)); |
| 901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN; |
| 903 | |
| 904 | info.entry_number = idx; |
| 905 | info.base_addr = GET_BASE(desc); |
| 906 | info.limit = GET_LIMIT(desc); |
| 907 | info.seg_32bit = GET_32BIT(desc); |
| 908 | info.contents = GET_CONTENTS(desc); |
| 909 | info.read_exec_only = !GET_WRITABLE(desc); |
| 910 | info.limit_in_pages = GET_LIMIT_PAGES(desc); |
| 911 | info.seg_not_present = !GET_PRESENT(desc); |
| 912 | info.useable = GET_USEABLE(desc); |
| 913 | |
| 914 | if (copy_to_user(u_info, &info, sizeof(info))) |
| 915 | return -EFAULT; |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | unsigned long arch_align_stack(unsigned long sp) |
| 920 | { |
Andi Kleen | c16b63e | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 921 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | sp -= get_random_int() % 8192; |
| 923 | return sp & ~0xf; |
| 924 | } |