Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
f3705136 | 16 | #include <linux/cpu.h> |
1da177e4 LT |
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> | |
1da177e4 LT |
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> | |
c16b63e0 | 40 | #include <linux/personality.h> |
74167347 | 41 | #include <linux/tick.h> |
1da177e4 LT |
42 | |
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> | |
1da177e4 | 50 | #include <asm/desc.h> |
64ca9004 | 51 | #include <asm/vm86.h> |
2ff2d3d7 | 52 | #include <asm/idle.h> |
1da177e4 LT |
53 | #ifdef CONFIG_MATH_EMULATION |
54 | #include <asm/math_emu.h> | |
55 | #endif | |
56 | ||
1da177e4 LT |
57 | #include <linux/err.h> |
58 | ||
f3705136 ZM |
59 | #include <asm/tlbflush.h> |
60 | #include <asm/cpu.h> | |
f95d47ca | 61 | #include <asm/pda.h> |
f3705136 | 62 | |
1da177e4 LT |
63 | asmlinkage void ret_from_fork(void) __asm__("ret_from_fork"); |
64 | ||
65 | static int hlt_counter; | |
66 | ||
67 | unsigned long boot_option_idle_override = 0; | |
68 | EXPORT_SYMBOL(boot_option_idle_override); | |
69 | ||
70 | /* | |
71 | * Return saved PC of a blocked thread. | |
72 | */ | |
73 | unsigned 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 | */ | |
81 | void (*pm_idle)(void); | |
129f6946 | 82 | EXPORT_SYMBOL(pm_idle); |
1da177e4 LT |
83 | static DEFINE_PER_CPU(unsigned int, cpu_idle_state); |
84 | ||
2ff2d3d7 SE |
85 | static ATOMIC_NOTIFIER_HEAD(idle_notifier); |
86 | ||
87 | void idle_notifier_register(struct notifier_block *n) | |
88 | { | |
89 | atomic_notifier_chain_register(&idle_notifier, n); | |
90 | } | |
91 | ||
92 | void idle_notifier_unregister(struct notifier_block *n) | |
93 | { | |
94 | atomic_notifier_chain_unregister(&idle_notifier, n); | |
95 | } | |
96 | ||
97 | static DEFINE_PER_CPU(volatile unsigned long, idle_state); | |
98 | ||
99 | void 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 | ||
106 | static 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 | ||
114 | void exit_idle(void) | |
115 | { | |
116 | if (current->pid) | |
117 | return; | |
118 | __exit_idle(); | |
119 | } | |
120 | ||
1da177e4 LT |
121 | void disable_hlt(void) |
122 | { | |
123 | hlt_counter++; | |
124 | } | |
125 | ||
126 | EXPORT_SYMBOL(disable_hlt); | |
127 | ||
128 | void enable_hlt(void) | |
129 | { | |
130 | hlt_counter--; | |
131 | } | |
132 | ||
133 | EXPORT_SYMBOL(enable_hlt); | |
134 | ||
135 | /* | |
136 | * We use this if we don't have any better | |
137 | * idle routine.. | |
138 | */ | |
139 | void default_idle(void) | |
140 | { | |
141 | if (!hlt_counter && boot_cpu_data.hlt_works_ok) { | |
495ab9c0 | 142 | current_thread_info()->status &= ~TS_POLLING; |
0888f06a IM |
143 | /* |
144 | * TS_POLLING-cleared state must be visible before we | |
145 | * test NEED_RESCHED: | |
146 | */ | |
147 | smp_mb(); | |
148 | ||
72690a21 AK |
149 | local_irq_disable(); |
150 | if (!need_resched()) | |
151 | safe_halt(); /* enables interrupts racelessly */ | |
152 | else | |
153 | local_irq_enable(); | |
495ab9c0 | 154 | current_thread_info()->status |= TS_POLLING; |
1da177e4 | 155 | } else { |
72690a21 AK |
156 | /* loop is done by the caller */ |
157 | cpu_relax(); | |
1da177e4 LT |
158 | } |
159 | } | |
129f6946 AD |
160 | #ifdef CONFIG_APM_MODULE |
161 | EXPORT_SYMBOL(default_idle); | |
162 | #endif | |
1da177e4 LT |
163 | |
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 | */ | |
169 | static void poll_idle (void) | |
170 | { | |
2ff2d3d7 | 171 | local_irq_enable(); |
72690a21 | 172 | cpu_relax(); |
1da177e4 LT |
173 | } |
174 | ||
f3705136 ZM |
175 | #ifdef CONFIG_HOTPLUG_CPU |
176 | #include <asm/nmi.h> | |
177 | /* We don't actually take CPU down, just spin without interrupts. */ | |
178 | static inline void play_dead(void) | |
179 | { | |
e1367daf LS |
180 | /* This must be done before dead CPU ack */ |
181 | cpu_exit_clear(); | |
182 | wbinvd(); | |
183 | mb(); | |
f3705136 ZM |
184 | /* Ack it */ |
185 | __get_cpu_var(cpu_state) = CPU_DEAD; | |
186 | ||
e1367daf LS |
187 | /* |
188 | * With physical CPU hotplug, we should halt the cpu | |
189 | */ | |
f3705136 | 190 | local_irq_disable(); |
e1367daf | 191 | while (1) |
f2ab4461 | 192 | halt(); |
f3705136 ZM |
193 | } |
194 | #else | |
195 | static inline void play_dead(void) | |
196 | { | |
197 | BUG(); | |
198 | } | |
199 | #endif /* CONFIG_HOTPLUG_CPU */ | |
200 | ||
1da177e4 LT |
201 | /* |
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 | */ | |
f3705136 | 207 | void cpu_idle(void) |
1da177e4 | 208 | { |
5bfb5d69 | 209 | int cpu = smp_processor_id(); |
f3705136 | 210 | |
495ab9c0 | 211 | current_thread_info()->status |= TS_POLLING; |
64c7c8f8 | 212 | |
1da177e4 LT |
213 | /* endless idle loop with no priority at all */ |
214 | while (1) { | |
74167347 | 215 | tick_nohz_stop_sched_tick(); |
1da177e4 LT |
216 | 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 | ||
f3705136 ZM |
228 | if (cpu_is_offline(cpu)) |
229 | play_dead(); | |
230 | ||
1da177e4 | 231 | __get_cpu_var(irq_stat).idle_timestamp = jiffies; |
2ff2d3d7 SE |
232 | |
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(); | |
1da177e4 | 240 | idle(); |
2ff2d3d7 | 241 | __exit_idle(); |
1da177e4 | 242 | } |
74167347 | 243 | tick_nohz_restart_sched_tick(); |
5bfb5d69 | 244 | preempt_enable_no_resched(); |
1da177e4 | 245 | schedule(); |
5bfb5d69 | 246 | preempt_disable(); |
1da177e4 LT |
247 | } |
248 | } | |
249 | ||
250 | void cpu_idle_wait(void) | |
251 | { | |
252 | unsigned int cpu, this_cpu = get_cpu(); | |
dc1829a4 | 253 | cpumask_t map, tmp = current->cpus_allowed; |
1da177e4 LT |
254 | |
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)); | |
dc1829a4 IM |
275 | |
276 | set_cpus_allowed(current, tmp); | |
1da177e4 LT |
277 | } |
278 | EXPORT_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). | |
991528d7 VP |
286 | * |
287 | * New with Core Duo processors, MWAIT can take some hints based on CPU | |
288 | * capability. | |
1da177e4 | 289 | */ |
991528d7 | 290 | void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) |
1da177e4 | 291 | { |
991528d7 | 292 | if (!need_resched()) { |
64c7c8f8 NP |
293 | __monitor((void *)¤t_thread_info()->flags, 0, 0); |
294 | smp_mb(); | |
991528d7 | 295 | if (!need_resched()) |
2ff2d3d7 SE |
296 | __sti_mwait(eax, ecx); |
297 | else | |
298 | local_irq_enable(); | |
299 | } else { | |
300 | local_irq_enable(); | |
1da177e4 LT |
301 | } |
302 | } | |
303 | ||
991528d7 VP |
304 | /* Default MONITOR/MWAIT with no hints, used for default C1 state */ |
305 | static void mwait_idle(void) | |
306 | { | |
307 | local_irq_enable(); | |
72690a21 | 308 | mwait_idle_with_hints(0, 0); |
991528d7 VP |
309 | } |
310 | ||
0bb3184d | 311 | void __devinit select_idle_routine(const struct cpuinfo_x86 *c) |
1da177e4 LT |
312 | { |
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 | ||
326 | static 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 | ||
346 | void 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 | ||
db753bdf | 355 | if (user_mode_vm(regs)) |
1da177e4 | 356 | printk(" ESP: %04x:%08lx",0xffff & regs->xss,regs->esp); |
b53e8f68 | 357 | printk(" EFLAGS: %08lx %s (%s %.*s)\n", |
96b644bd SH |
358 | regs->eflags, print_tainted(), init_utsname()->release, |
359 | (int)strcspn(init_utsname()->version, " "), | |
360 | init_utsname()->version); | |
1da177e4 LT |
361 | 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); | |
464d1a78 JF |
365 | printk(" DS: %04x ES: %04x FS: %04x\n", |
366 | 0xffff & regs->xds,0xffff & regs->xes, 0xffff & regs->xfs); | |
1da177e4 | 367 | |
4bb0d3ec ZA |
368 | cr0 = read_cr0(); |
369 | cr2 = read_cr2(); | |
370 | cr3 = read_cr3(); | |
ff6e8c0d | 371 | cr4 = read_cr4_safe(); |
1da177e4 | 372 | printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", cr0, cr2, cr3, cr4); |
176a2718 | 373 | show_trace(NULL, regs, ®s->esp); |
1da177e4 LT |
374 | } |
375 | ||
376 | /* | |
377 | * This gets run with %ebx containing the | |
378 | * function to call, and %edx containing | |
379 | * the "args". | |
380 | */ | |
381 | extern void kernel_thread_helper(void); | |
1da177e4 LT |
382 | |
383 | /* | |
384 | * Create a kernel thread | |
385 | */ | |
386 | int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags) | |
387 | { | |
388 | struct pt_regs regs; | |
389 | ||
390 | memset(®s, 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; | |
464d1a78 | 397 | regs.xfs = __KERNEL_PDA; |
1da177e4 LT |
398 | regs.orig_eax = -1; |
399 | regs.eip = (unsigned long) kernel_thread_helper; | |
78be3706 | 400 | regs.xcs = __KERNEL_CS | get_kernel_rpl(); |
1da177e4 LT |
401 | regs.eflags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2; |
402 | ||
403 | /* Ok, create the new process.. */ | |
8cf2c519 | 404 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL); |
1da177e4 | 405 | } |
129f6946 | 406 | EXPORT_SYMBOL(kernel_thread); |
1da177e4 LT |
407 | |
408 | /* | |
409 | * Free current thread data structures etc.. | |
410 | */ | |
411 | void exit_thread(void) | |
412 | { | |
1da177e4 | 413 | /* The process may have allocated an io port bitmap... nuke it. */ |
b3cf2576 SE |
414 | if (unlikely(test_thread_flag(TIF_IO_BITMAP))) { |
415 | struct task_struct *tsk = current; | |
416 | struct thread_struct *t = &tsk->thread; | |
1da177e4 LT |
417 | 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; | |
b3cf2576 | 422 | clear_thread_flag(TIF_IO_BITMAP); |
1da177e4 LT |
423 | /* |
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 | ||
435 | void 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)); | |
b3cf2576 | 441 | clear_tsk_thread_flag(tsk, TIF_DEBUG); |
1da177e4 LT |
442 | /* |
443 | * Forget coprocessor state.. | |
444 | */ | |
445 | clear_fpu(tsk); | |
446 | clear_used_math(); | |
447 | } | |
448 | ||
449 | void release_thread(struct task_struct *dead_task) | |
450 | { | |
2684927c | 451 | BUG_ON(dead_task->mm); |
1da177e4 LT |
452 | 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 | */ | |
459 | void prepare_to_copy(struct task_struct *tsk) | |
460 | { | |
461 | unlazy_fpu(tsk); | |
462 | } | |
463 | ||
464 | int 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 | ||
07b047fc | 472 | childregs = task_pt_regs(p); |
f48d9663 AN |
473 | *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); | |
1da177e4 LT |
479 | |
480 | p->thread.eip = (unsigned long) ret_from_fork; | |
481 | ||
464d1a78 | 482 | savesegment(gs,p->thread.gs); |
1da177e4 LT |
483 | |
484 | tsk = current; | |
b3cf2576 | 485 | if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) { |
52978be6 AD |
486 | p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr, |
487 | IO_BITMAP_BYTES, GFP_KERNEL); | |
1da177e4 LT |
488 | if (!p->thread.io_bitmap_ptr) { |
489 | p->thread.io_bitmap_max = 0; | |
490 | return -ENOMEM; | |
491 | } | |
b3cf2576 | 492 | set_tsk_thread_flag(p, TIF_IO_BITMAP); |
1da177e4 LT |
493 | } |
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 | */ | |
531 | void 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; | |
464d1a78 JF |
558 | dump->regs.fs = regs->xfs; |
559 | savesegment(gs,dump->regs.gs); | |
1da177e4 LT |
560 | 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 | } | |
129f6946 | 569 | EXPORT_SYMBOL(dump_thread); |
1da177e4 LT |
570 | |
571 | /* | |
572 | * Capture the user space registers if the task is not running (in user space) | |
573 | */ | |
574 | int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs) | |
575 | { | |
07b047fc | 576 | struct pt_regs ptregs = *task_pt_regs(tsk); |
1da177e4 LT |
577 | 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 | ||
b3cf2576 SE |
587 | static noinline void __switch_to_xtra(struct task_struct *next_p, |
588 | struct tss_struct *tss) | |
1da177e4 | 589 | { |
b3cf2576 SE |
590 | 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)) { | |
1da177e4 LT |
605 | /* |
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 | } | |
b3cf2576 | 612 | |
1da177e4 LT |
613 | 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 | } | |
1da177e4 | 633 | |
ffaa8bd6 AA |
634 | /* |
635 | * This function selects if the context switch from prev to next | |
636 | * has to tweak the TSC disable bit in the cr4. | |
637 | */ | |
638 | static 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 | */ | |
06b425d8 AV |
647 | prev = task_thread_info(prev_p); |
648 | next = task_thread_info(next_p); | |
ffaa8bd6 AA |
649 | |
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 | ||
1da177e4 LT |
661 | /* |
662 | * 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 | */ | |
688 | struct 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 | ||
acc20761 CE |
699 | |
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 | ||
1da177e4 | 704 | /* |
e7a2ff59 | 705 | * Reload esp0. |
1da177e4 LT |
706 | */ |
707 | load_esp0(tss, next); | |
708 | ||
709 | /* | |
464d1a78 | 710 | * Save away %gs. No need to save %fs, as it was saved on the |
f95d47ca JF |
711 | * 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. | |
1da177e4 | 718 | */ |
464d1a78 | 719 | savesegment(gs, prev->gs); |
1da177e4 LT |
720 | |
721 | /* | |
e7a2ff59 | 722 | * Load the per-thread Thread-Local Storage descriptor. |
1da177e4 | 723 | */ |
e7a2ff59 | 724 | load_TLS(next, cpu); |
1da177e4 | 725 | |
8b151144 ZA |
726 | /* |
727 | * 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 | ||
1da177e4 | 735 | /* |
b3cf2576 | 736 | * Now maybe handle debug registers and/or IO bitmaps |
1da177e4 | 737 | */ |
facf0147 CE |
738 | if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW) |
739 | || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))) | |
b3cf2576 | 740 | __switch_to_xtra(next_p, tss); |
1da177e4 | 741 | |
ffaa8bd6 AA |
742 | disable_tsc(prev_p, next_p); |
743 | ||
9226d125 ZA |
744 | /* |
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 | ||
acc20761 CE |
753 | /* 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 | ||
9226d125 ZA |
760 | /* |
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 | ||
1da177e4 LT |
768 | return prev_p; |
769 | } | |
770 | ||
771 | asmlinkage int sys_fork(struct pt_regs regs) | |
772 | { | |
773 | return do_fork(SIGCHLD, regs.esp, ®s, 0, NULL, NULL); | |
774 | } | |
775 | ||
776 | asmlinkage 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, ®s, 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 | */ | |
801 | asmlinkage int sys_vfork(struct pt_regs regs) | |
802 | { | |
803 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, ®s, 0, NULL, NULL); | |
804 | } | |
805 | ||
806 | /* | |
807 | * sys_execve() executes a new program. | |
808 | */ | |
809 | asmlinkage 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 | ®s); | |
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); | |
830 | out: | |
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 | ||
837 | unsigned 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; | |
65e0fdff | 844 | stack_page = (unsigned long)task_stack_page(p); |
1da177e4 LT |
845 | 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 | } | |
860 | ||
861 | /* | |
862 | * sys_alloc_thread_area: get a yet unused TLS descriptor index. | |
863 | */ | |
864 | static int get_free_idx(void) | |
865 | { | |
866 | struct thread_struct *t = ¤t->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 | */ | |
878 | asmlinkage int sys_set_thread_area(struct user_desc __user *u_info) | |
879 | { | |
880 | struct thread_struct *t = ¤t->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 | ||
945 | asmlinkage 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 | ||
71ae18ec PBG |
956 | memset(&info, 0, sizeof(info)); |
957 | ||
1da177e4 LT |
958 | 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 | ||
975 | unsigned long arch_align_stack(unsigned long sp) | |
976 | { | |
c16b63e0 | 977 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) |
1da177e4 LT |
978 | sp -= get_random_int() % 8192; |
979 | return sp & ~0xf; | |
980 | } |