blob: bea304d48cdbb8fdc150e9033ad0e53a3961c439 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Mwaikambof3705132005-06-25 14:54:50 -070016#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Torvalds1da177e2005-04-16 15:20:36 -070031#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 Kleenc16b63e2006-09-26 10:52:28 +020040#include <linux/personality.h>
Ingo Molnar74167342007-02-16 01:28:07 -080041#include <linux/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/uaccess.h>
44#include <asm/pgtable.h>
45#include <asm/system.h>
46#include <asm/io.h>
47#include <asm/ldt.h>
48#include <asm/processor.h>
49#include <asm/i387.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/desc.h>
Matt Mackall64ca9002006-01-08 01:05:26 -080051#include <asm/vm86.h>
Stephane Eranian2ff2d3d2007-02-13 13:26:22 +010052#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifdef CONFIG_MATH_EMULATION
54#include <asm/math_emu.h>
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/err.h>
58
Zwane Mwaikambof3705132005-06-25 14:54:50 -070059#include <asm/tlbflush.h>
60#include <asm/cpu.h>
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +010061#include <asm/pda.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
64
65static int hlt_counter;
66
67unsigned long boot_option_idle_override = 0;
68EXPORT_SYMBOL(boot_option_idle_override);
69
70/*
71 * Return saved PC of a blocked thread.
72 */
73unsigned long thread_saved_pc(struct task_struct *tsk)
74{
75 return ((unsigned long *)tsk->thread.esp)[3];
76}
77
78/*
79 * Powermanagement idle function, if any..
80 */
81void (*pm_idle)(void);
Alexey Dobriyan129f6942005-06-23 00:08:33 -070082EXPORT_SYMBOL(pm_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static DEFINE_PER_CPU(unsigned int, cpu_idle_state);
84
Stephane Eranian2ff2d3d2007-02-13 13:26:22 +010085static ATOMIC_NOTIFIER_HEAD(idle_notifier);
86
87void idle_notifier_register(struct notifier_block *n)
88{
89 atomic_notifier_chain_register(&idle_notifier, n);
90}
91
92void idle_notifier_unregister(struct notifier_block *n)
93{
94 atomic_notifier_chain_unregister(&idle_notifier, n);
95}
96
97static DEFINE_PER_CPU(volatile unsigned long, idle_state);
98
99void enter_idle(void)
100{
101 /* needs to be atomic w.r.t. interrupts, not against other CPUs */
102 __set_bit(0, &__get_cpu_var(idle_state));
103 atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
104}
105
106static void __exit_idle(void)
107{
108 /* needs to be atomic w.r.t. interrupts, not against other CPUs */
109 if (__test_and_clear_bit(0, &__get_cpu_var(idle_state)) == 0)
110 return;
111 atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
112}
113
114void exit_idle(void)
115{
116 if (current->pid)
117 return;
118 __exit_idle();
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121void disable_hlt(void)
122{
123 hlt_counter++;
124}
125
126EXPORT_SYMBOL(disable_hlt);
127
128void enable_hlt(void)
129{
130 hlt_counter--;
131}
132
133EXPORT_SYMBOL(enable_hlt);
134
135/*
136 * We use this if we don't have any better
137 * idle routine..
138 */
139void default_idle(void)
140{
141 if (!hlt_counter && boot_cpu_data.hlt_works_ok) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200142 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800143 /*
144 * TS_POLLING-cleared state must be visible before we
145 * test NEED_RESCHED:
146 */
147 smp_mb();
148
Andi Kleen72690a22006-12-07 02:14:03 +0100149 local_irq_disable();
150 if (!need_resched())
151 safe_halt(); /* enables interrupts racelessly */
152 else
153 local_irq_enable();
Andi Kleen495ab9c2006-06-26 13:59:11 +0200154 current_thread_info()->status |= TS_POLLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else {
Andi Kleen72690a22006-12-07 02:14:03 +0100156 /* loop is done by the caller */
157 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700160#ifdef CONFIG_APM_MODULE
161EXPORT_SYMBOL(default_idle);
162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/*
165 * On SMP it's slightly faster (but much more power-consuming!)
166 * to poll the ->work.need_resched flag instead of waiting for the
167 * cross-CPU IPI to arrive. Use this option with caution.
168 */
169static void poll_idle (void)
170{
Stephane Eranian2ff2d3d2007-02-13 13:26:22 +0100171 local_irq_enable();
Andi Kleen72690a22006-12-07 02:14:03 +0100172 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700175#ifdef CONFIG_HOTPLUG_CPU
176#include <asm/nmi.h>
177/* We don't actually take CPU down, just spin without interrupts. */
178static inline void play_dead(void)
179{
Li Shaohuae1367da2005-06-25 14:54:56 -0700180 /* This must be done before dead CPU ack */
181 cpu_exit_clear();
182 wbinvd();
183 mb();
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700184 /* Ack it */
185 __get_cpu_var(cpu_state) = CPU_DEAD;
186
Li Shaohuae1367da2005-06-25 14:54:56 -0700187 /*
188 * With physical CPU hotplug, we should halt the cpu
189 */
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700190 local_irq_disable();
Li Shaohuae1367da2005-06-25 14:54:56 -0700191 while (1)
Zachary Amsdenf2ab4462005-09-03 15:56:42 -0700192 halt();
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700193}
194#else
195static inline void play_dead(void)
196{
197 BUG();
198}
199#endif /* CONFIG_HOTPLUG_CPU */
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
202 * The idle thread. There's no useful work to be
203 * done, so just try to conserve power and have a
204 * low exit latency (ie sit in a loop waiting for
205 * somebody to say that they'd like to reschedule)
206 */
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700207void cpu_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800209 int cpu = smp_processor_id();
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700210
Andi Kleen495ab9c2006-06-26 13:59:11 +0200211 current_thread_info()->status |= TS_POLLING;
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* endless idle loop with no priority at all */
214 while (1) {
Ingo Molnar74167342007-02-16 01:28:07 -0800215 tick_nohz_stop_sched_tick();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 while (!need_resched()) {
217 void (*idle)(void);
218
219 if (__get_cpu_var(cpu_idle_state))
220 __get_cpu_var(cpu_idle_state) = 0;
221
222 rmb();
223 idle = pm_idle;
224
225 if (!idle)
226 idle = default_idle;
227
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700228 if (cpu_is_offline(cpu))
229 play_dead();
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 __get_cpu_var(irq_stat).idle_timestamp = jiffies;
Stephane Eranian2ff2d3d2007-02-13 13:26:22 +0100232
233 /*
234 * Idle routines should keep interrupts disabled
235 * from here on, until they go to idle.
236 * Otherwise, idle callbacks can misfire.
237 */
238 local_irq_disable();
239 enter_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 idle();
Stephane Eranian2ff2d3d2007-02-13 13:26:22 +0100241 __exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Ingo Molnar74167342007-02-16 01:28:07 -0800243 tick_nohz_restart_sched_tick();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800244 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 schedule();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800246 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248}
249
250void cpu_idle_wait(void)
251{
252 unsigned int cpu, this_cpu = get_cpu();
Ingo Molnardc1829a2006-11-17 14:26:18 +0100253 cpumask_t map, tmp = current->cpus_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 set_cpus_allowed(current, cpumask_of_cpu(this_cpu));
256 put_cpu();
257
258 cpus_clear(map);
259 for_each_online_cpu(cpu) {
260 per_cpu(cpu_idle_state, cpu) = 1;
261 cpu_set(cpu, map);
262 }
263
264 __get_cpu_var(cpu_idle_state) = 0;
265
266 wmb();
267 do {
268 ssleep(1);
269 for_each_online_cpu(cpu) {
270 if (cpu_isset(cpu, map) && !per_cpu(cpu_idle_state, cpu))
271 cpu_clear(cpu, map);
272 }
273 cpus_and(map, map, cpu_online_map);
274 } while (!cpus_empty(map));
Ingo Molnardc1829a2006-11-17 14:26:18 +0100275
276 set_cpus_allowed(current, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278EXPORT_SYMBOL_GPL(cpu_idle_wait);
279
280/*
281 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
282 * which can obviate IPI to trigger checking of need_resched.
283 * We execute MONITOR against need_resched and enter optimized wait state
284 * through MWAIT. Whenever someone changes need_resched, we would be woken
285 * up from MWAIT (without an IPI).
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700286 *
287 * New with Core Duo processors, MWAIT can take some hints based on CPU
288 * capability.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700290void mwait_idle_with_hints(unsigned long eax, unsigned long ecx)
291{
292 if (!need_resched()) {
293 __monitor((void *)&current_thread_info()->flags, 0, 0);
294 smp_mb();
295 if (!need_resched())
Stephane Eranian2ff2d3d2007-02-13 13:26:22 +0100296 __sti_mwait(eax, ecx);
297 else
298 local_irq_enable();
299 } else {
300 local_irq_enable();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700301 }
302}
303
304/* Default MONITOR/MWAIT with no hints, used for default C1 state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static void mwait_idle(void)
306{
307 local_irq_enable();
Andi Kleen72690a22006-12-07 02:14:03 +0100308 mwait_idle_with_hints(0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Li Shaohua0bb31842005-06-25 14:54:55 -0700311void __devinit select_idle_routine(const struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 if (cpu_has(c, X86_FEATURE_MWAIT)) {
314 printk("monitor/mwait feature present.\n");
315 /*
316 * Skip, if setup has overridden idle.
317 * One CPU supports mwait => All CPUs supports mwait
318 */
319 if (!pm_idle) {
320 printk("using mwait in idle threads.\n");
321 pm_idle = mwait_idle;
322 }
323 }
324}
325
326static int __init idle_setup (char *str)
327{
328 if (!strncmp(str, "poll", 4)) {
329 printk("using polling idle threads.\n");
330 pm_idle = poll_idle;
331#ifdef CONFIG_X86_SMP
332 if (smp_num_siblings > 1)
333 printk("WARNING: polling idle and HT enabled, performance may degrade.\n");
334#endif
335 } else if (!strncmp(str, "halt", 4)) {
336 printk("using halt in idle threads.\n");
337 pm_idle = default_idle;
338 }
339
340 boot_option_idle_override = 1;
341 return 1;
342}
343
344__setup("idle=", idle_setup);
345
346void show_regs(struct pt_regs * regs)
347{
348 unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L;
349
350 printk("\n");
351 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
352 printk("EIP: %04x:[<%08lx>] CPU: %d\n",0xffff & regs->xcs,regs->eip, smp_processor_id());
353 print_symbol("EIP is at %s\n", regs->eip);
354
Jan Beulichdb753bd2006-03-23 02:59:46 -0800355 if (user_mode_vm(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp);
Chuck Ebbertb53e8f62006-02-04 23:28:04 -0800357 printk(" EFLAGS: %08lx %s (%s %.*s)\n",
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700358 regs->eflags, print_tainted(), init_utsname()->release,
359 (int)strcspn(init_utsname()->version, " "),
360 init_utsname()->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n",
362 regs->eax,regs->ebx,regs->ecx,regs->edx);
363 printk("ESI: %08lx EDI: %08lx EBP: %08lx",
364 regs->esi, regs->edi, regs->ebp);
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100365 printk(" DS: %04x ES: %04x FS: %04x\n",
366 0xffff & regs->xds,0xffff & regs->xes, 0xffff & regs->xfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Zachary Amsden4bb0d3e2005-09-03 15:56:36 -0700368 cr0 = read_cr0();
369 cr2 = read_cr2();
370 cr3 = read_cr3();
Zachary Amsdenff6e8c02006-01-06 00:11:50 -0800371 cr4 = read_cr4_safe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", cr0, cr2, cr3, cr4);
Jan Beulich176a2712006-06-26 13:57:41 +0200373 show_trace(NULL, regs, &regs->esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/*
377 * This gets run with %ebx containing the
378 * function to call, and %edx containing
379 * the "args".
380 */
381extern void kernel_thread_helper(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383/*
384 * Create a kernel thread
385 */
386int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
387{
388 struct pt_regs regs;
389
390 memset(&regs, 0, sizeof(regs));
391
392 regs.ebx = (unsigned long) fn;
393 regs.edx = (unsigned long) arg;
394
395 regs.xds = __USER_DS;
396 regs.xes = __USER_DS;
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100397 regs.xfs = __KERNEL_PDA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 regs.orig_eax = -1;
399 regs.eip = (unsigned long) kernel_thread_helper;
Rusty Russell78be3702006-09-26 10:52:39 +0200400 regs.xcs = __KERNEL_CS | get_kernel_rpl();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2;
402
403 /* Ok, create the new process.. */
Andi Kleen8cf2c512006-10-21 18:37:02 +0200404 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700406EXPORT_SYMBOL(kernel_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408/*
409 * Free current thread data structures etc..
410 */
411void exit_thread(void)
412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* The process may have allocated an io port bitmap... nuke it. */
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400414 if (unlikely(test_thread_flag(TIF_IO_BITMAP))) {
415 struct task_struct *tsk = current;
416 struct thread_struct *t = &tsk->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 int cpu = get_cpu();
418 struct tss_struct *tss = &per_cpu(init_tss, cpu);
419
420 kfree(t->io_bitmap_ptr);
421 t->io_bitmap_ptr = NULL;
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400422 clear_thread_flag(TIF_IO_BITMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /*
424 * Careful, clear this in the TSS too:
425 */
426 memset(tss->io_bitmap, 0xff, tss->io_bitmap_max);
427 t->io_bitmap_max = 0;
428 tss->io_bitmap_owner = NULL;
429 tss->io_bitmap_max = 0;
430 tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
431 put_cpu();
432 }
433}
434
435void flush_thread(void)
436{
437 struct task_struct *tsk = current;
438
439 memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
440 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400441 clear_tsk_thread_flag(tsk, TIF_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /*
443 * Forget coprocessor state..
444 */
445 clear_fpu(tsk);
446 clear_used_math();
447}
448
449void release_thread(struct task_struct *dead_task)
450{
Zachary Amsden26849272006-01-06 00:11:59 -0800451 BUG_ON(dead_task->mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 release_vm86_irqs(dead_task);
453}
454
455/*
456 * This gets called before we allocate a new thread and copy
457 * the current task into it.
458 */
459void prepare_to_copy(struct task_struct *tsk)
460{
461 unlazy_fpu(tsk);
462}
463
464int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
465 unsigned long unused,
466 struct task_struct * p, struct pt_regs * regs)
467{
468 struct pt_regs * childregs;
469 struct task_struct *tsk;
470 int err;
471
akpm@osdl.org07b047f2006-01-12 01:05:41 -0800472 childregs = task_pt_regs(p);
Alexander Nybergf48d9662005-05-05 16:15:03 -0700473 *childregs = *regs;
474 childregs->eax = 0;
475 childregs->esp = esp;
476
477 p->thread.esp = (unsigned long) childregs;
478 p->thread.esp0 = (unsigned long) (childregs+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 p->thread.eip = (unsigned long) ret_from_fork;
481
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100482 savesegment(gs,p->thread.gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 tsk = current;
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400485 if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700486 p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr,
487 IO_BITMAP_BYTES, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!p->thread.io_bitmap_ptr) {
489 p->thread.io_bitmap_max = 0;
490 return -ENOMEM;
491 }
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400492 set_tsk_thread_flag(p, TIF_IO_BITMAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
495 /*
496 * Set a new TLS for the child thread?
497 */
498 if (clone_flags & CLONE_SETTLS) {
499 struct desc_struct *desc;
500 struct user_desc info;
501 int idx;
502
503 err = -EFAULT;
504 if (copy_from_user(&info, (void __user *)childregs->esi, sizeof(info)))
505 goto out;
506 err = -EINVAL;
507 if (LDT_empty(&info))
508 goto out;
509
510 idx = info.entry_number;
511 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
512 goto out;
513
514 desc = p->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
515 desc->a = LDT_entry_a(&info);
516 desc->b = LDT_entry_b(&info);
517 }
518
519 err = 0;
520 out:
521 if (err && p->thread.io_bitmap_ptr) {
522 kfree(p->thread.io_bitmap_ptr);
523 p->thread.io_bitmap_max = 0;
524 }
525 return err;
526}
527
528/*
529 * fill in the user structure for a core dump..
530 */
531void dump_thread(struct pt_regs * regs, struct user * dump)
532{
533 int i;
534
535/* changed the size calculations - should hopefully work better. lbt */
536 dump->magic = CMAGIC;
537 dump->start_code = 0;
538 dump->start_stack = regs->esp & ~(PAGE_SIZE - 1);
539 dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
540 dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
541 dump->u_dsize -= dump->u_tsize;
542 dump->u_ssize = 0;
543 for (i = 0; i < 8; i++)
544 dump->u_debugreg[i] = current->thread.debugreg[i];
545
546 if (dump->start_stack < TASK_SIZE)
547 dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
548
549 dump->regs.ebx = regs->ebx;
550 dump->regs.ecx = regs->ecx;
551 dump->regs.edx = regs->edx;
552 dump->regs.esi = regs->esi;
553 dump->regs.edi = regs->edi;
554 dump->regs.ebp = regs->ebp;
555 dump->regs.eax = regs->eax;
556 dump->regs.ds = regs->xds;
557 dump->regs.es = regs->xes;
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100558 dump->regs.fs = regs->xfs;
559 savesegment(gs,dump->regs.gs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dump->regs.orig_eax = regs->orig_eax;
561 dump->regs.eip = regs->eip;
562 dump->regs.cs = regs->xcs;
563 dump->regs.eflags = regs->eflags;
564 dump->regs.esp = regs->esp;
565 dump->regs.ss = regs->xss;
566
567 dump->u_fpvalid = dump_fpu (regs, &dump->i387);
568}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700569EXPORT_SYMBOL(dump_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571/*
572 * Capture the user space registers if the task is not running (in user space)
573 */
574int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
575{
akpm@osdl.org07b047f2006-01-12 01:05:41 -0800576 struct pt_regs ptregs = *task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 ptregs.xcs &= 0xffff;
578 ptregs.xds &= 0xffff;
579 ptregs.xes &= 0xffff;
580 ptregs.xss &= 0xffff;
581
582 elf_core_copy_regs(regs, &ptregs);
583
584 return 1;
585}
586
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400587static noinline void __switch_to_xtra(struct task_struct *next_p,
588 struct tss_struct *tss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400590 struct thread_struct *next;
591
592 next = &next_p->thread;
593
594 if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
595 set_debugreg(next->debugreg[0], 0);
596 set_debugreg(next->debugreg[1], 1);
597 set_debugreg(next->debugreg[2], 2);
598 set_debugreg(next->debugreg[3], 3);
599 /* no 4 and 5 */
600 set_debugreg(next->debugreg[6], 6);
601 set_debugreg(next->debugreg[7], 7);
602 }
603
604 if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /*
606 * Disable the bitmap via an invalid offset. We still cache
607 * the previous bitmap owner and the IO bitmap contents:
608 */
609 tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
610 return;
611 }
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (likely(next == tss->io_bitmap_owner)) {
614 /*
615 * Previous owner of the bitmap (hence the bitmap content)
616 * matches the next task, we dont have to do anything but
617 * to set a valid offset in the TSS:
618 */
619 tss->io_bitmap_base = IO_BITMAP_OFFSET;
620 return;
621 }
622 /*
623 * Lazy TSS's I/O bitmap copy. We set an invalid offset here
624 * and we let the task to get a GPF in case an I/O instruction
625 * is performed. The handler of the GPF will verify that the
626 * faulting task has a valid I/O bitmap and, it true, does the
627 * real copy and restart the instruction. This will save us
628 * redundant copies when the currently switched task does not
629 * perform any I/O during its timeslice.
630 */
631 tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634/*
Andrea Arcangeliffaa8bd2005-06-27 14:36:36 -0700635 * This function selects if the context switch from prev to next
636 * has to tweak the TSC disable bit in the cr4.
637 */
638static inline void disable_tsc(struct task_struct *prev_p,
639 struct task_struct *next_p)
640{
641 struct thread_info *prev, *next;
642
643 /*
644 * gcc should eliminate the ->thread_info dereference if
645 * has_secure_computing returns 0 at compile time (SECCOMP=n).
646 */
Al Viro06b425d2006-01-12 01:05:40 -0800647 prev = task_thread_info(prev_p);
648 next = task_thread_info(next_p);
Andrea Arcangeliffaa8bd2005-06-27 14:36:36 -0700649
650 if (has_secure_computing(prev) || has_secure_computing(next)) {
651 /* slow path here */
652 if (has_secure_computing(prev) &&
653 !has_secure_computing(next)) {
654 write_cr4(read_cr4() & ~X86_CR4_TSD);
655 } else if (!has_secure_computing(prev) &&
656 has_secure_computing(next))
657 write_cr4(read_cr4() | X86_CR4_TSD);
658 }
659}
660
661/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * switch_to(x,yn) should switch tasks from x to y.
663 *
664 * We fsave/fwait so that an exception goes off at the right time
665 * (as a call from the fsave or fwait in effect) rather than to
666 * the wrong process. Lazy FP saving no longer makes any sense
667 * with modern CPU's, and this simplifies a lot of things (SMP
668 * and UP become the same).
669 *
670 * NOTE! We used to use the x86 hardware context switching. The
671 * reason for not using it any more becomes apparent when you
672 * try to recover gracefully from saved state that is no longer
673 * valid (stale segment register values in particular). With the
674 * hardware task-switch, there is no way to fix up bad state in
675 * a reasonable manner.
676 *
677 * The fact that Intel documents the hardware task-switching to
678 * be slow is a fairly red herring - this code is not noticeably
679 * faster. However, there _is_ some room for improvement here,
680 * so the performance issues may eventually be a valid point.
681 * More important, however, is the fact that this allows us much
682 * more flexibility.
683 *
684 * The return value (in %eax) will be the "prev" task after
685 * the task-switch, and shows up in ret_from_fork in entry.S,
686 * for example.
687 */
688struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
689{
690 struct thread_struct *prev = &prev_p->thread,
691 *next = &next_p->thread;
692 int cpu = smp_processor_id();
693 struct tss_struct *tss = &per_cpu(init_tss, cpu);
694
695 /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */
696
697 __unlazy_fpu(prev_p);
698
Chuck Ebbertacc20762006-12-07 02:14:01 +0100699
700 /* we're going to use this soon, after a few expensive things */
701 if (next_p->fpu_counter > 5)
702 prefetch(&next->i387.fxsave);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /*
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700705 * Reload esp0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
707 load_esp0(tss, next);
708
709 /*
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100710 * Save away %gs. No need to save %fs, as it was saved on the
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100711 * stack on entry. No need to save %es and %ds, as those are
712 * always kernel segments while inside the kernel. Doing this
713 * before setting the new TLS descriptors avoids the situation
714 * where we temporarily have non-reloadable segments in %fs
715 * and %gs. This could be an issue if the NMI handler ever
716 * used %fs or %gs (it does not today), or if the kernel is
717 * running inside of a hypervisor layer.
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700718 */
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100719 savesegment(gs, prev->gs);
Zachary Amsdene7a2ff52005-09-03 15:56:39 -0700720
721 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * Load the per-thread Thread-Local Storage descriptor.
723 */
724 load_TLS(next, cpu);
725
726 /*
Zachary Amsden8b151142007-02-13 13:26:21 +0100727 * Restore IOPL if needed. In normal use, the flags restore
728 * in the switch assembly will handle this. But if the kernel
729 * is running virtualized at a non-zero CPL, the popf will
730 * not restore flags, so it must be done in a separate step.
731 */
732 if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl))
733 set_iopl_mask(next->iopl);
734
735 /*
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400736 * Now maybe handle debug registers and/or IO bitmaps
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Chuck Ebbertfacf0142006-07-25 16:15:16 -0400738 if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW)
739 || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)))
Stephane Eranianb3cf2572006-07-09 21:12:39 -0400740 __switch_to_xtra(next_p, tss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Andrea Arcangeliffaa8bd2005-06-27 14:36:36 -0700742 disable_tsc(prev_p, next_p);
743
Zachary Amsden9226d122007-02-13 13:26:21 +0100744 /*
745 * Leave lazy mode, flushing any hypercalls made here.
746 * This must be done before restoring TLS segments so
747 * the GDT and LDT are properly updated, and must be
748 * done before math_state_restore, so the TS bit is up
749 * to date.
750 */
751 arch_leave_lazy_cpu_mode();
752
Chuck Ebbertacc20762006-12-07 02:14:01 +0100753 /* If the task has used fpu the last 5 timeslices, just do a full
754 * restore of the math state immediately to avoid the trap; the
755 * chances of needing FPU soon are obviously high now
756 */
757 if (next_p->fpu_counter > 5)
758 math_state_restore();
759
Zachary Amsden9226d122007-02-13 13:26:21 +0100760 /*
761 * Restore %gs if needed (which is common)
762 */
763 if (prev->gs | next->gs)
764 loadsegment(gs, next->gs);
765
766 write_pda(pcurrent, next_p);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return prev_p;
769}
770
771asmlinkage int sys_fork(struct pt_regs regs)
772{
773 return do_fork(SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
774}
775
776asmlinkage int sys_clone(struct pt_regs regs)
777{
778 unsigned long clone_flags;
779 unsigned long newsp;
780 int __user *parent_tidptr, *child_tidptr;
781
782 clone_flags = regs.ebx;
783 newsp = regs.ecx;
784 parent_tidptr = (int __user *)regs.edx;
785 child_tidptr = (int __user *)regs.edi;
786 if (!newsp)
787 newsp = regs.esp;
788 return do_fork(clone_flags, newsp, &regs, 0, parent_tidptr, child_tidptr);
789}
790
791/*
792 * This is trivial, and on the face of it looks like it
793 * could equally well be done in user mode.
794 *
795 * Not so, for quite unobvious reasons - register pressure.
796 * In user mode vfork() cannot have a stack frame, and if
797 * done by calling the "clone()" system call directly, you
798 * do not have enough call-clobbered registers to hold all
799 * the information you need.
800 */
801asmlinkage int sys_vfork(struct pt_regs regs)
802{
803 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, &regs, 0, NULL, NULL);
804}
805
806/*
807 * sys_execve() executes a new program.
808 */
809asmlinkage int sys_execve(struct pt_regs regs)
810{
811 int error;
812 char * filename;
813
814 filename = getname((char __user *) regs.ebx);
815 error = PTR_ERR(filename);
816 if (IS_ERR(filename))
817 goto out;
818 error = do_execve(filename,
819 (char __user * __user *) regs.ecx,
820 (char __user * __user *) regs.edx,
821 &regs);
822 if (error == 0) {
823 task_lock(current);
824 current->ptrace &= ~PT_DTRACE;
825 task_unlock(current);
826 /* Make sure we don't return using sysenter.. */
827 set_thread_flag(TIF_IRET);
828 }
829 putname(filename);
830out:
831 return error;
832}
833
834#define top_esp (THREAD_SIZE - sizeof(unsigned long))
835#define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long))
836
837unsigned long get_wchan(struct task_struct *p)
838{
839 unsigned long ebp, esp, eip;
840 unsigned long stack_page;
841 int count = 0;
842 if (!p || p == current || p->state == TASK_RUNNING)
843 return 0;
Al Viro65e0fdf2006-01-12 01:05:41 -0800844 stack_page = (unsigned long)task_stack_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 esp = p->thread.esp;
846 if (!stack_page || esp < stack_page || esp > top_esp+stack_page)
847 return 0;
848 /* include/asm-i386/system.h:switch_to() pushes ebp last. */
849 ebp = *(unsigned long *) esp;
850 do {
851 if (ebp < stack_page || ebp > top_ebp+stack_page)
852 return 0;
853 eip = *(unsigned long *) (ebp+4);
854 if (!in_sched_functions(eip))
855 return eip;
856 ebp = *(unsigned long *) ebp;
857 } while (count++ < 16);
858 return 0;
859}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861/*
862 * sys_alloc_thread_area: get a yet unused TLS descriptor index.
863 */
864static int get_free_idx(void)
865{
866 struct thread_struct *t = &current->thread;
867 int idx;
868
869 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
870 if (desc_empty(t->tls_array + idx))
871 return idx + GDT_ENTRY_TLS_MIN;
872 return -ESRCH;
873}
874
875/*
876 * Set a given TLS descriptor:
877 */
878asmlinkage int sys_set_thread_area(struct user_desc __user *u_info)
879{
880 struct thread_struct *t = &current->thread;
881 struct user_desc info;
882 struct desc_struct *desc;
883 int cpu, idx;
884
885 if (copy_from_user(&info, u_info, sizeof(info)))
886 return -EFAULT;
887 idx = info.entry_number;
888
889 /*
890 * index -1 means the kernel should try to find and
891 * allocate an empty descriptor:
892 */
893 if (idx == -1) {
894 idx = get_free_idx();
895 if (idx < 0)
896 return idx;
897 if (put_user(idx, &u_info->entry_number))
898 return -EFAULT;
899 }
900
901 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
902 return -EINVAL;
903
904 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
905
906 /*
907 * We must not get preempted while modifying the TLS.
908 */
909 cpu = get_cpu();
910
911 if (LDT_empty(&info)) {
912 desc->a = 0;
913 desc->b = 0;
914 } else {
915 desc->a = LDT_entry_a(&info);
916 desc->b = LDT_entry_b(&info);
917 }
918 load_TLS(t, cpu);
919
920 put_cpu();
921
922 return 0;
923}
924
925/*
926 * Get the current Thread-Local Storage area:
927 */
928
929#define GET_BASE(desc) ( \
930 (((desc)->a >> 16) & 0x0000ffff) | \
931 (((desc)->b << 16) & 0x00ff0000) | \
932 ( (desc)->b & 0xff000000) )
933
934#define GET_LIMIT(desc) ( \
935 ((desc)->a & 0x0ffff) | \
936 ((desc)->b & 0xf0000) )
937
938#define GET_32BIT(desc) (((desc)->b >> 22) & 1)
939#define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
940#define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
941#define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
942#define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
943#define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
944
945asmlinkage int sys_get_thread_area(struct user_desc __user *u_info)
946{
947 struct user_desc info;
948 struct desc_struct *desc;
949 int idx;
950
951 if (get_user(idx, &u_info->entry_number))
952 return -EFAULT;
953 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
954 return -EINVAL;
955
Blaisorblade71ae18e2005-07-27 11:45:18 -0700956 memset(&info, 0, sizeof(info));
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
959
960 info.entry_number = idx;
961 info.base_addr = GET_BASE(desc);
962 info.limit = GET_LIMIT(desc);
963 info.seg_32bit = GET_32BIT(desc);
964 info.contents = GET_CONTENTS(desc);
965 info.read_exec_only = !GET_WRITABLE(desc);
966 info.limit_in_pages = GET_LIMIT_PAGES(desc);
967 info.seg_not_present = !GET_PRESENT(desc);
968 info.useable = GET_USEABLE(desc);
969
970 if (copy_to_user(u_info, &info, sizeof(info)))
971 return -EFAULT;
972 return 0;
973}
974
975unsigned long arch_align_stack(unsigned long sp)
976{
Andi Kleenc16b63e2006-09-26 10:52:28 +0200977 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 sp -= get_random_int() % 8192;
979 return sp & ~0xf;
980}