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