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> | |
1da177e4 | 26 | #include <linux/interrupt.h> |
1da177e4 LT |
27 | #include <linux/utsname.h> |
28 | #include <linux/delay.h> | |
29 | #include <linux/reboot.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/mc146818rtc.h> | |
32 | #include <linux/module.h> | |
33 | #include <linux/kallsyms.h> | |
34 | #include <linux/ptrace.h> | |
35 | #include <linux/random.h> | |
c16b63e0 | 36 | #include <linux/personality.h> |
74167347 | 37 | #include <linux/tick.h> |
7c3576d2 | 38 | #include <linux/percpu.h> |
1da177e4 LT |
39 | |
40 | #include <asm/uaccess.h> | |
41 | #include <asm/pgtable.h> | |
42 | #include <asm/system.h> | |
43 | #include <asm/io.h> | |
44 | #include <asm/ldt.h> | |
45 | #include <asm/processor.h> | |
46 | #include <asm/i387.h> | |
1da177e4 | 47 | #include <asm/desc.h> |
64ca9004 | 48 | #include <asm/vm86.h> |
1da177e4 LT |
49 | #ifdef CONFIG_MATH_EMULATION |
50 | #include <asm/math_emu.h> | |
51 | #endif | |
52 | ||
1da177e4 LT |
53 | #include <linux/err.h> |
54 | ||
f3705136 ZM |
55 | #include <asm/tlbflush.h> |
56 | #include <asm/cpu.h> | |
718fc13b | 57 | #include <asm/kdebug.h> |
f3705136 | 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 | { | |
faca6227 | 77 | return ((unsigned long *)tsk->thread.sp)[3]; |
1da177e4 LT |
78 | } |
79 | ||
80 | /* | |
81 | * Powermanagement idle function, if any.. | |
82 | */ | |
83 | void (*pm_idle)(void); | |
129f6946 | 84 | EXPORT_SYMBOL(pm_idle); |
1da177e4 LT |
85 | |
86 | void disable_hlt(void) | |
87 | { | |
88 | hlt_counter++; | |
89 | } | |
90 | ||
91 | EXPORT_SYMBOL(disable_hlt); | |
92 | ||
93 | void enable_hlt(void) | |
94 | { | |
95 | hlt_counter--; | |
96 | } | |
97 | ||
98 | EXPORT_SYMBOL(enable_hlt); | |
99 | ||
100 | /* | |
101 | * We use this if we don't have any better | |
102 | * idle routine.. | |
103 | */ | |
104 | void default_idle(void) | |
105 | { | |
106 | if (!hlt_counter && boot_cpu_data.hlt_works_ok) { | |
495ab9c0 | 107 | current_thread_info()->status &= ~TS_POLLING; |
0888f06a IM |
108 | /* |
109 | * TS_POLLING-cleared state must be visible before we | |
110 | * test NEED_RESCHED: | |
111 | */ | |
112 | smp_mb(); | |
113 | ||
72690a21 | 114 | local_irq_disable(); |
5ee613b6 | 115 | if (!need_resched()) { |
72690a21 | 116 | safe_halt(); /* enables interrupts racelessly */ |
5ee613b6 | 117 | local_irq_disable(); |
5ee613b6 IM |
118 | } |
119 | local_irq_enable(); | |
495ab9c0 | 120 | current_thread_info()->status |= TS_POLLING; |
1da177e4 | 121 | } else { |
3b22ec7b | 122 | local_irq_enable(); |
72690a21 AK |
123 | /* loop is done by the caller */ |
124 | cpu_relax(); | |
1da177e4 LT |
125 | } |
126 | } | |
129f6946 AD |
127 | #ifdef CONFIG_APM_MODULE |
128 | EXPORT_SYMBOL(default_idle); | |
129 | #endif | |
1da177e4 LT |
130 | |
131 | /* | |
132 | * On SMP it's slightly faster (but much more power-consuming!) | |
133 | * to poll the ->work.need_resched flag instead of waiting for the | |
134 | * cross-CPU IPI to arrive. Use this option with caution. | |
135 | */ | |
6612538c | 136 | static void poll_idle(void) |
1da177e4 | 137 | { |
3b22ec7b | 138 | local_irq_enable(); |
72690a21 | 139 | cpu_relax(); |
1da177e4 LT |
140 | } |
141 | ||
f3705136 ZM |
142 | #ifdef CONFIG_HOTPLUG_CPU |
143 | #include <asm/nmi.h> | |
144 | /* We don't actually take CPU down, just spin without interrupts. */ | |
145 | static inline void play_dead(void) | |
146 | { | |
e1367daf LS |
147 | /* This must be done before dead CPU ack */ |
148 | cpu_exit_clear(); | |
149 | wbinvd(); | |
150 | mb(); | |
f3705136 ZM |
151 | /* Ack it */ |
152 | __get_cpu_var(cpu_state) = CPU_DEAD; | |
153 | ||
e1367daf LS |
154 | /* |
155 | * With physical CPU hotplug, we should halt the cpu | |
156 | */ | |
f3705136 | 157 | local_irq_disable(); |
e1367daf | 158 | while (1) |
f2ab4461 | 159 | halt(); |
f3705136 ZM |
160 | } |
161 | #else | |
162 | static inline void play_dead(void) | |
163 | { | |
164 | BUG(); | |
165 | } | |
166 | #endif /* CONFIG_HOTPLUG_CPU */ | |
167 | ||
1da177e4 LT |
168 | /* |
169 | * The idle thread. There's no useful work to be | |
170 | * done, so just try to conserve power and have a | |
171 | * low exit latency (ie sit in a loop waiting for | |
172 | * somebody to say that they'd like to reschedule) | |
173 | */ | |
f3705136 | 174 | void cpu_idle(void) |
1da177e4 | 175 | { |
5bfb5d69 | 176 | int cpu = smp_processor_id(); |
f3705136 | 177 | |
495ab9c0 | 178 | current_thread_info()->status |= TS_POLLING; |
64c7c8f8 | 179 | |
1da177e4 LT |
180 | /* endless idle loop with no priority at all */ |
181 | while (1) { | |
74167347 | 182 | tick_nohz_stop_sched_tick(); |
1da177e4 LT |
183 | while (!need_resched()) { |
184 | void (*idle)(void); | |
185 | ||
f1d1a842 | 186 | check_pgt_cache(); |
1da177e4 LT |
187 | rmb(); |
188 | idle = pm_idle; | |
189 | ||
0723a69a BL |
190 | if (rcu_pending(cpu)) |
191 | rcu_check_callbacks(cpu, 0); | |
192 | ||
1da177e4 LT |
193 | if (!idle) |
194 | idle = default_idle; | |
195 | ||
f3705136 ZM |
196 | if (cpu_is_offline(cpu)) |
197 | play_dead(); | |
198 | ||
1da177e4 LT |
199 | __get_cpu_var(irq_stat).idle_timestamp = jiffies; |
200 | idle(); | |
201 | } | |
74167347 | 202 | tick_nohz_restart_sched_tick(); |
5bfb5d69 | 203 | preempt_enable_no_resched(); |
1da177e4 | 204 | schedule(); |
5bfb5d69 | 205 | preempt_disable(); |
1da177e4 LT |
206 | } |
207 | } | |
208 | ||
40d6a146 SR |
209 | static void do_nothing(void *unused) |
210 | { | |
211 | } | |
212 | ||
783e391b VP |
213 | /* |
214 | * cpu_idle_wait - Used to ensure that all the CPUs discard old value of | |
215 | * pm_idle and update to new pm_idle value. Required while changing pm_idle | |
216 | * handler on SMP systems. | |
217 | * | |
218 | * Caller must have changed pm_idle to the new value before the call. Old | |
219 | * pm_idle value will not be used by any CPU after the return of this function. | |
220 | */ | |
1da177e4 LT |
221 | void cpu_idle_wait(void) |
222 | { | |
783e391b VP |
223 | smp_mb(); |
224 | /* kick all the CPUs so that they exit out of pm_idle */ | |
225 | smp_call_function(do_nothing, NULL, 0, 1); | |
1da177e4 LT |
226 | } |
227 | EXPORT_SYMBOL_GPL(cpu_idle_wait); | |
228 | ||
229 | /* | |
230 | * This uses new MONITOR/MWAIT instructions on P4 processors with PNI, | |
231 | * which can obviate IPI to trigger checking of need_resched. | |
232 | * We execute MONITOR against need_resched and enter optimized wait state | |
233 | * through MWAIT. Whenever someone changes need_resched, we would be woken | |
234 | * up from MWAIT (without an IPI). | |
991528d7 VP |
235 | * |
236 | * New with Core Duo processors, MWAIT can take some hints based on CPU | |
237 | * capability. | |
1da177e4 | 238 | */ |
65ea5b03 | 239 | void mwait_idle_with_hints(unsigned long ax, unsigned long cx) |
1da177e4 | 240 | { |
991528d7 | 241 | if (!need_resched()) { |
64c7c8f8 NP |
242 | __monitor((void *)¤t_thread_info()->flags, 0, 0); |
243 | smp_mb(); | |
991528d7 | 244 | if (!need_resched()) |
3b22ec7b GOC |
245 | __sti_mwait(ax, cx); |
246 | else | |
247 | local_irq_enable(); | |
248 | } else | |
249 | local_irq_enable(); | |
1da177e4 LT |
250 | } |
251 | ||
991528d7 VP |
252 | /* Default MONITOR/MWAIT with no hints, used for default C1 state */ |
253 | static void mwait_idle(void) | |
254 | { | |
255 | local_irq_enable(); | |
72690a21 | 256 | mwait_idle_with_hints(0, 0); |
991528d7 VP |
257 | } |
258 | ||
4c02ad1e | 259 | static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c) |
0c07ee38 AK |
260 | { |
261 | if (force_mwait) | |
262 | return 1; | |
263 | /* Any C1 states supported? */ | |
264 | return c->cpuid_level >= 5 && ((cpuid_edx(5) >> 4) & 0xf) > 0; | |
265 | } | |
266 | ||
3446fa05 | 267 | void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c) |
1da177e4 | 268 | { |
27415a4f HS |
269 | static int selected; |
270 | ||
271 | if (selected) | |
272 | return; | |
273 | #ifdef CONFIG_X86_SMP | |
274 | if (pm_idle == poll_idle && smp_num_siblings > 1) { | |
275 | printk(KERN_WARNING "WARNING: polling idle and HT enabled," | |
276 | " performance may degrade.\n"); | |
277 | } | |
278 | #endif | |
0c07ee38 | 279 | if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) { |
1da177e4 LT |
280 | /* |
281 | * Skip, if setup has overridden idle. | |
282 | * One CPU supports mwait => All CPUs supports mwait | |
283 | */ | |
284 | if (!pm_idle) { | |
27415a4f | 285 | printk(KERN_INFO "using mwait in idle threads.\n"); |
1da177e4 LT |
286 | pm_idle = mwait_idle; |
287 | } | |
288 | } | |
27415a4f | 289 | selected = 1; |
1da177e4 LT |
290 | } |
291 | ||
f039b754 | 292 | static int __init idle_setup(char *str) |
1da177e4 | 293 | { |
f039b754 | 294 | if (!strcmp(str, "poll")) { |
1da177e4 LT |
295 | printk("using polling idle threads.\n"); |
296 | pm_idle = poll_idle; | |
f039b754 AK |
297 | } else if (!strcmp(str, "mwait")) |
298 | force_mwait = 1; | |
299 | else | |
300 | return -1; | |
1da177e4 LT |
301 | |
302 | boot_option_idle_override = 1; | |
f039b754 | 303 | return 0; |
1da177e4 | 304 | } |
f039b754 | 305 | early_param("idle", idle_setup); |
1da177e4 | 306 | |
9d975ebd | 307 | void __show_registers(struct pt_regs *regs, int all) |
1da177e4 LT |
308 | { |
309 | unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L; | |
bb1995d5 | 310 | unsigned long d0, d1, d2, d3, d6, d7; |
65ea5b03 | 311 | unsigned long sp; |
9d975ebd PE |
312 | unsigned short ss, gs; |
313 | ||
314 | if (user_mode_vm(regs)) { | |
65ea5b03 PA |
315 | sp = regs->sp; |
316 | ss = regs->ss & 0xffff; | |
9d975ebd PE |
317 | savesegment(gs, gs); |
318 | } else { | |
65ea5b03 | 319 | sp = (unsigned long) (®s->sp); |
9d975ebd PE |
320 | savesegment(ss, ss); |
321 | savesegment(gs, gs); | |
322 | } | |
1da177e4 LT |
323 | |
324 | printk("\n"); | |
60812a4a LT |
325 | printk("Pid: %d, comm: %s %s (%s %.*s)\n", |
326 | task_pid_nr(current), current->comm, | |
9d975ebd PE |
327 | print_tainted(), init_utsname()->release, |
328 | (int)strcspn(init_utsname()->version, " "), | |
329 | init_utsname()->version); | |
330 | ||
331 | printk("EIP: %04x:[<%08lx>] EFLAGS: %08lx CPU: %d\n", | |
92bc2056 | 332 | (u16)regs->cs, regs->ip, regs->flags, |
9d975ebd | 333 | smp_processor_id()); |
65ea5b03 | 334 | print_symbol("EIP is at %s\n", regs->ip); |
1da177e4 | 335 | |
1da177e4 | 336 | printk("EAX: %08lx EBX: %08lx ECX: %08lx EDX: %08lx\n", |
65ea5b03 | 337 | regs->ax, regs->bx, regs->cx, regs->dx); |
9d975ebd | 338 | printk("ESI: %08lx EDI: %08lx EBP: %08lx ESP: %08lx\n", |
65ea5b03 | 339 | regs->si, regs->di, regs->bp, sp); |
9d975ebd | 340 | printk(" DS: %04x ES: %04x FS: %04x GS: %04x SS: %04x\n", |
92bc2056 | 341 | (u16)regs->ds, (u16)regs->es, (u16)regs->fs, gs, ss); |
9d975ebd PE |
342 | |
343 | if (!all) | |
344 | return; | |
1da177e4 | 345 | |
4bb0d3ec ZA |
346 | cr0 = read_cr0(); |
347 | cr2 = read_cr2(); | |
348 | cr3 = read_cr3(); | |
ff6e8c0d | 349 | cr4 = read_cr4_safe(); |
9d975ebd PE |
350 | printk("CR0: %08lx CR2: %08lx CR3: %08lx CR4: %08lx\n", |
351 | cr0, cr2, cr3, cr4); | |
bb1995d5 AS |
352 | |
353 | get_debugreg(d0, 0); | |
354 | get_debugreg(d1, 1); | |
355 | get_debugreg(d2, 2); | |
356 | get_debugreg(d3, 3); | |
357 | printk("DR0: %08lx DR1: %08lx DR2: %08lx DR3: %08lx\n", | |
358 | d0, d1, d2, d3); | |
9d975ebd | 359 | |
bb1995d5 AS |
360 | get_debugreg(d6, 6); |
361 | get_debugreg(d7, 7); | |
9d975ebd PE |
362 | printk("DR6: %08lx DR7: %08lx\n", |
363 | d6, d7); | |
364 | } | |
bb1995d5 | 365 | |
9d975ebd PE |
366 | void show_regs(struct pt_regs *regs) |
367 | { | |
368 | __show_registers(regs, 1); | |
5bc27dc2 | 369 | show_trace(NULL, regs, ®s->sp, regs->bp); |
1da177e4 LT |
370 | } |
371 | ||
372 | /* | |
65ea5b03 PA |
373 | * This gets run with %bx containing the |
374 | * function to call, and %dx containing | |
1da177e4 LT |
375 | * the "args". |
376 | */ | |
377 | extern void kernel_thread_helper(void); | |
1da177e4 LT |
378 | |
379 | /* | |
380 | * Create a kernel thread | |
381 | */ | |
382 | int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags) | |
383 | { | |
384 | struct pt_regs regs; | |
385 | ||
386 | memset(®s, 0, sizeof(regs)); | |
387 | ||
65ea5b03 PA |
388 | regs.bx = (unsigned long) fn; |
389 | regs.dx = (unsigned long) arg; | |
1da177e4 | 390 | |
65ea5b03 PA |
391 | regs.ds = __USER_DS; |
392 | regs.es = __USER_DS; | |
393 | regs.fs = __KERNEL_PERCPU; | |
394 | regs.orig_ax = -1; | |
395 | regs.ip = (unsigned long) kernel_thread_helper; | |
396 | regs.cs = __KERNEL_CS | get_kernel_rpl(); | |
397 | regs.flags = X86_EFLAGS_IF | X86_EFLAGS_SF | X86_EFLAGS_PF | 0x2; | |
1da177e4 LT |
398 | |
399 | /* Ok, create the new process.. */ | |
8cf2c519 | 400 | return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, ®s, 0, NULL, NULL); |
1da177e4 | 401 | } |
129f6946 | 402 | EXPORT_SYMBOL(kernel_thread); |
1da177e4 LT |
403 | |
404 | /* | |
405 | * Free current thread data structures etc.. | |
406 | */ | |
407 | void exit_thread(void) | |
408 | { | |
1da177e4 | 409 | /* The process may have allocated an io port bitmap... nuke it. */ |
b3cf2576 SE |
410 | if (unlikely(test_thread_flag(TIF_IO_BITMAP))) { |
411 | struct task_struct *tsk = current; | |
412 | struct thread_struct *t = &tsk->thread; | |
1da177e4 LT |
413 | int cpu = get_cpu(); |
414 | struct tss_struct *tss = &per_cpu(init_tss, cpu); | |
415 | ||
416 | kfree(t->io_bitmap_ptr); | |
417 | t->io_bitmap_ptr = NULL; | |
b3cf2576 | 418 | clear_thread_flag(TIF_IO_BITMAP); |
1da177e4 LT |
419 | /* |
420 | * Careful, clear this in the TSS too: | |
421 | */ | |
422 | memset(tss->io_bitmap, 0xff, tss->io_bitmap_max); | |
423 | t->io_bitmap_max = 0; | |
424 | tss->io_bitmap_owner = NULL; | |
425 | tss->io_bitmap_max = 0; | |
a75c54f9 | 426 | tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET; |
1da177e4 LT |
427 | put_cpu(); |
428 | } | |
429 | } | |
430 | ||
431 | void flush_thread(void) | |
432 | { | |
433 | struct task_struct *tsk = current; | |
434 | ||
0f534093 RM |
435 | tsk->thread.debugreg0 = 0; |
436 | tsk->thread.debugreg1 = 0; | |
437 | tsk->thread.debugreg2 = 0; | |
438 | tsk->thread.debugreg3 = 0; | |
439 | tsk->thread.debugreg6 = 0; | |
440 | tsk->thread.debugreg7 = 0; | |
1da177e4 | 441 | memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array)); |
b3cf2576 | 442 | clear_tsk_thread_flag(tsk, TIF_DEBUG); |
1da177e4 LT |
443 | /* |
444 | * Forget coprocessor state.. | |
445 | */ | |
446 | clear_fpu(tsk); | |
447 | clear_used_math(); | |
448 | } | |
449 | ||
450 | void release_thread(struct task_struct *dead_task) | |
451 | { | |
2684927c | 452 | BUG_ON(dead_task->mm); |
1da177e4 LT |
453 | release_vm86_irqs(dead_task); |
454 | } | |
455 | ||
456 | /* | |
457 | * This gets called before we allocate a new thread and copy | |
458 | * the current task into it. | |
459 | */ | |
460 | void prepare_to_copy(struct task_struct *tsk) | |
461 | { | |
462 | unlazy_fpu(tsk); | |
463 | } | |
464 | ||
65ea5b03 | 465 | int copy_thread(int nr, unsigned long clone_flags, unsigned long sp, |
1da177e4 LT |
466 | unsigned long unused, |
467 | struct task_struct * p, struct pt_regs * regs) | |
468 | { | |
469 | struct pt_regs * childregs; | |
470 | struct task_struct *tsk; | |
471 | int err; | |
472 | ||
07b047fc | 473 | childregs = task_pt_regs(p); |
f48d9663 | 474 | *childregs = *regs; |
65ea5b03 PA |
475 | childregs->ax = 0; |
476 | childregs->sp = sp; | |
f48d9663 | 477 | |
faca6227 PA |
478 | p->thread.sp = (unsigned long) childregs; |
479 | p->thread.sp0 = (unsigned long) (childregs+1); | |
1da177e4 | 480 | |
faca6227 | 481 | p->thread.ip = (unsigned long) ret_from_fork; |
1da177e4 | 482 | |
6612538c | 483 | savesegment(gs, p->thread.gs); |
1da177e4 LT |
484 | |
485 | tsk = current; | |
b3cf2576 | 486 | if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) { |
52978be6 AD |
487 | p->thread.io_bitmap_ptr = kmemdup(tsk->thread.io_bitmap_ptr, |
488 | IO_BITMAP_BYTES, GFP_KERNEL); | |
1da177e4 LT |
489 | if (!p->thread.io_bitmap_ptr) { |
490 | p->thread.io_bitmap_max = 0; | |
491 | return -ENOMEM; | |
492 | } | |
b3cf2576 | 493 | set_tsk_thread_flag(p, TIF_IO_BITMAP); |
1da177e4 LT |
494 | } |
495 | ||
efd1ca52 RM |
496 | err = 0; |
497 | ||
1da177e4 LT |
498 | /* |
499 | * Set a new TLS for the child thread? | |
500 | */ | |
efd1ca52 RM |
501 | if (clone_flags & CLONE_SETTLS) |
502 | err = do_set_thread_area(p, -1, | |
65ea5b03 | 503 | (struct user_desc __user *)childregs->si, 0); |
1da177e4 | 504 | |
1da177e4 LT |
505 | if (err && p->thread.io_bitmap_ptr) { |
506 | kfree(p->thread.io_bitmap_ptr); | |
507 | p->thread.io_bitmap_max = 0; | |
508 | } | |
509 | return err; | |
510 | } | |
511 | ||
513ad84b IM |
512 | void |
513 | start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp) | |
514 | { | |
515 | __asm__("movl %0, %%gs" :: "r"(0)); | |
516 | regs->fs = 0; | |
517 | set_fs(USER_DS); | |
518 | regs->ds = __USER_DS; | |
519 | regs->es = __USER_DS; | |
520 | regs->ss = __USER_DS; | |
521 | regs->cs = __USER_CS; | |
522 | regs->ip = new_ip; | |
523 | regs->sp = new_sp; | |
524 | } | |
525 | EXPORT_SYMBOL_GPL(start_thread); | |
526 | ||
cf99abac | 527 | #ifdef CONFIG_SECCOMP |
bdb4f156 | 528 | static void hard_disable_TSC(void) |
cf99abac AA |
529 | { |
530 | write_cr4(read_cr4() | X86_CR4_TSD); | |
531 | } | |
532 | void disable_TSC(void) | |
533 | { | |
534 | preempt_disable(); | |
535 | if (!test_and_set_thread_flag(TIF_NOTSC)) | |
536 | /* | |
537 | * Must flip the CPU state synchronously with | |
538 | * TIF_NOTSC in the current running context. | |
539 | */ | |
540 | hard_disable_TSC(); | |
541 | preempt_enable(); | |
542 | } | |
bdb4f156 | 543 | static void hard_enable_TSC(void) |
cf99abac AA |
544 | { |
545 | write_cr4(read_cr4() & ~X86_CR4_TSD); | |
546 | } | |
547 | #endif /* CONFIG_SECCOMP */ | |
548 | ||
549 | static noinline void | |
550 | __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p, | |
551 | struct tss_struct *tss) | |
1da177e4 | 552 | { |
7e991604 | 553 | struct thread_struct *prev, *next; |
eee3af4a | 554 | unsigned long debugctl; |
b3cf2576 | 555 | |
7e991604 | 556 | prev = &prev_p->thread; |
b3cf2576 SE |
557 | next = &next_p->thread; |
558 | ||
eee3af4a MM |
559 | debugctl = prev->debugctlmsr; |
560 | if (next->ds_area_msr != prev->ds_area_msr) { | |
561 | /* we clear debugctl to make sure DS | |
562 | * is not in use when we change it */ | |
563 | debugctl = 0; | |
5b0e5084 | 564 | update_debugctlmsr(0); |
eee3af4a MM |
565 | wrmsr(MSR_IA32_DS_AREA, next->ds_area_msr, 0); |
566 | } | |
567 | ||
568 | if (next->debugctlmsr != debugctl) | |
5b0e5084 | 569 | update_debugctlmsr(next->debugctlmsr); |
7e991604 | 570 | |
b3cf2576 | 571 | if (test_tsk_thread_flag(next_p, TIF_DEBUG)) { |
0f534093 RM |
572 | set_debugreg(next->debugreg0, 0); |
573 | set_debugreg(next->debugreg1, 1); | |
574 | set_debugreg(next->debugreg2, 2); | |
575 | set_debugreg(next->debugreg3, 3); | |
b3cf2576 | 576 | /* no 4 and 5 */ |
0f534093 RM |
577 | set_debugreg(next->debugreg6, 6); |
578 | set_debugreg(next->debugreg7, 7); | |
b3cf2576 SE |
579 | } |
580 | ||
cf99abac AA |
581 | #ifdef CONFIG_SECCOMP |
582 | if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^ | |
583 | test_tsk_thread_flag(next_p, TIF_NOTSC)) { | |
584 | /* prev and next are different */ | |
585 | if (test_tsk_thread_flag(next_p, TIF_NOTSC)) | |
586 | hard_disable_TSC(); | |
587 | else | |
588 | hard_enable_TSC(); | |
589 | } | |
590 | #endif | |
591 | ||
b4ef95de | 592 | #ifdef X86_BTS |
eee3af4a MM |
593 | if (test_tsk_thread_flag(prev_p, TIF_BTS_TRACE_TS)) |
594 | ptrace_bts_take_timestamp(prev_p, BTS_TASK_DEPARTS); | |
595 | ||
596 | if (test_tsk_thread_flag(next_p, TIF_BTS_TRACE_TS)) | |
597 | ptrace_bts_take_timestamp(next_p, BTS_TASK_ARRIVES); | |
b4ef95de | 598 | #endif |
eee3af4a MM |
599 | |
600 | ||
b3cf2576 | 601 | if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) { |
1da177e4 LT |
602 | /* |
603 | * Disable the bitmap via an invalid offset. We still cache | |
604 | * the previous bitmap owner and the IO bitmap contents: | |
605 | */ | |
a75c54f9 | 606 | tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET; |
1da177e4 LT |
607 | return; |
608 | } | |
b3cf2576 | 609 | |
1da177e4 LT |
610 | if (likely(next == tss->io_bitmap_owner)) { |
611 | /* | |
612 | * Previous owner of the bitmap (hence the bitmap content) | |
613 | * matches the next task, we dont have to do anything but | |
614 | * to set a valid offset in the TSS: | |
615 | */ | |
a75c54f9 | 616 | tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET; |
1da177e4 LT |
617 | return; |
618 | } | |
619 | /* | |
620 | * Lazy TSS's I/O bitmap copy. We set an invalid offset here | |
621 | * and we let the task to get a GPF in case an I/O instruction | |
622 | * is performed. The handler of the GPF will verify that the | |
623 | * faulting task has a valid I/O bitmap and, it true, does the | |
624 | * real copy and restart the instruction. This will save us | |
625 | * redundant copies when the currently switched task does not | |
626 | * perform any I/O during its timeslice. | |
627 | */ | |
a75c54f9 | 628 | tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY; |
1da177e4 | 629 | } |
1da177e4 LT |
630 | |
631 | /* | |
632 | * switch_to(x,yn) should switch tasks from x to y. | |
633 | * | |
634 | * We fsave/fwait so that an exception goes off at the right time | |
635 | * (as a call from the fsave or fwait in effect) rather than to | |
636 | * the wrong process. Lazy FP saving no longer makes any sense | |
637 | * with modern CPU's, and this simplifies a lot of things (SMP | |
638 | * and UP become the same). | |
639 | * | |
640 | * NOTE! We used to use the x86 hardware context switching. The | |
641 | * reason for not using it any more becomes apparent when you | |
642 | * try to recover gracefully from saved state that is no longer | |
643 | * valid (stale segment register values in particular). With the | |
644 | * hardware task-switch, there is no way to fix up bad state in | |
645 | * a reasonable manner. | |
646 | * | |
647 | * The fact that Intel documents the hardware task-switching to | |
648 | * be slow is a fairly red herring - this code is not noticeably | |
649 | * faster. However, there _is_ some room for improvement here, | |
650 | * so the performance issues may eventually be a valid point. | |
651 | * More important, however, is the fact that this allows us much | |
652 | * more flexibility. | |
653 | * | |
65ea5b03 | 654 | * The return value (in %ax) will be the "prev" task after |
1da177e4 LT |
655 | * the task-switch, and shows up in ret_from_fork in entry.S, |
656 | * for example. | |
657 | */ | |
75604d7f | 658 | struct task_struct * __switch_to(struct task_struct *prev_p, struct task_struct *next_p) |
1da177e4 LT |
659 | { |
660 | struct thread_struct *prev = &prev_p->thread, | |
661 | *next = &next_p->thread; | |
662 | int cpu = smp_processor_id(); | |
663 | struct tss_struct *tss = &per_cpu(init_tss, cpu); | |
664 | ||
665 | /* never put a printk in __switch_to... printk() calls wake_up*() indirectly */ | |
666 | ||
667 | __unlazy_fpu(prev_p); | |
668 | ||
acc20761 CE |
669 | |
670 | /* we're going to use this soon, after a few expensive things */ | |
671 | if (next_p->fpu_counter > 5) | |
672 | prefetch(&next->i387.fxsave); | |
673 | ||
1da177e4 | 674 | /* |
e7a2ff59 | 675 | * Reload esp0. |
1da177e4 | 676 | */ |
faca6227 | 677 | load_sp0(tss, next); |
1da177e4 LT |
678 | |
679 | /* | |
464d1a78 | 680 | * Save away %gs. No need to save %fs, as it was saved on the |
f95d47ca JF |
681 | * stack on entry. No need to save %es and %ds, as those are |
682 | * always kernel segments while inside the kernel. Doing this | |
683 | * before setting the new TLS descriptors avoids the situation | |
684 | * where we temporarily have non-reloadable segments in %fs | |
685 | * and %gs. This could be an issue if the NMI handler ever | |
686 | * used %fs or %gs (it does not today), or if the kernel is | |
687 | * running inside of a hypervisor layer. | |
1da177e4 | 688 | */ |
464d1a78 | 689 | savesegment(gs, prev->gs); |
1da177e4 LT |
690 | |
691 | /* | |
e7a2ff59 | 692 | * Load the per-thread Thread-Local Storage descriptor. |
1da177e4 | 693 | */ |
e7a2ff59 | 694 | load_TLS(next, cpu); |
1da177e4 | 695 | |
8b151144 ZA |
696 | /* |
697 | * Restore IOPL if needed. In normal use, the flags restore | |
698 | * in the switch assembly will handle this. But if the kernel | |
699 | * is running virtualized at a non-zero CPL, the popf will | |
700 | * not restore flags, so it must be done in a separate step. | |
701 | */ | |
702 | if (get_kernel_rpl() && unlikely(prev->iopl != next->iopl)) | |
703 | set_iopl_mask(next->iopl); | |
704 | ||
1da177e4 | 705 | /* |
b3cf2576 | 706 | * Now maybe handle debug registers and/or IO bitmaps |
1da177e4 | 707 | */ |
cf99abac AA |
708 | if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV || |
709 | task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT)) | |
710 | __switch_to_xtra(prev_p, next_p, tss); | |
ffaa8bd6 | 711 | |
9226d125 ZA |
712 | /* |
713 | * Leave lazy mode, flushing any hypercalls made here. | |
714 | * This must be done before restoring TLS segments so | |
715 | * the GDT and LDT are properly updated, and must be | |
716 | * done before math_state_restore, so the TS bit is up | |
717 | * to date. | |
718 | */ | |
719 | arch_leave_lazy_cpu_mode(); | |
720 | ||
acc20761 CE |
721 | /* If the task has used fpu the last 5 timeslices, just do a full |
722 | * restore of the math state immediately to avoid the trap; the | |
723 | * chances of needing FPU soon are obviously high now | |
724 | */ | |
725 | if (next_p->fpu_counter > 5) | |
726 | math_state_restore(); | |
727 | ||
9226d125 ZA |
728 | /* |
729 | * Restore %gs if needed (which is common) | |
730 | */ | |
731 | if (prev->gs | next->gs) | |
732 | loadsegment(gs, next->gs); | |
733 | ||
7c3576d2 | 734 | x86_write_percpu(current_task, next_p); |
9226d125 | 735 | |
1da177e4 LT |
736 | return prev_p; |
737 | } | |
738 | ||
739 | asmlinkage int sys_fork(struct pt_regs regs) | |
740 | { | |
65ea5b03 | 741 | return do_fork(SIGCHLD, regs.sp, ®s, 0, NULL, NULL); |
1da177e4 LT |
742 | } |
743 | ||
744 | asmlinkage int sys_clone(struct pt_regs regs) | |
745 | { | |
746 | unsigned long clone_flags; | |
747 | unsigned long newsp; | |
748 | int __user *parent_tidptr, *child_tidptr; | |
749 | ||
65ea5b03 PA |
750 | clone_flags = regs.bx; |
751 | newsp = regs.cx; | |
752 | parent_tidptr = (int __user *)regs.dx; | |
753 | child_tidptr = (int __user *)regs.di; | |
1da177e4 | 754 | if (!newsp) |
65ea5b03 | 755 | newsp = regs.sp; |
1da177e4 LT |
756 | return do_fork(clone_flags, newsp, ®s, 0, parent_tidptr, child_tidptr); |
757 | } | |
758 | ||
759 | /* | |
760 | * This is trivial, and on the face of it looks like it | |
761 | * could equally well be done in user mode. | |
762 | * | |
763 | * Not so, for quite unobvious reasons - register pressure. | |
764 | * In user mode vfork() cannot have a stack frame, and if | |
765 | * done by calling the "clone()" system call directly, you | |
766 | * do not have enough call-clobbered registers to hold all | |
767 | * the information you need. | |
768 | */ | |
769 | asmlinkage int sys_vfork(struct pt_regs regs) | |
770 | { | |
65ea5b03 | 771 | return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.sp, ®s, 0, NULL, NULL); |
1da177e4 LT |
772 | } |
773 | ||
774 | /* | |
775 | * sys_execve() executes a new program. | |
776 | */ | |
777 | asmlinkage int sys_execve(struct pt_regs regs) | |
778 | { | |
779 | int error; | |
780 | char * filename; | |
781 | ||
65ea5b03 | 782 | filename = getname((char __user *) regs.bx); |
1da177e4 LT |
783 | error = PTR_ERR(filename); |
784 | if (IS_ERR(filename)) | |
785 | goto out; | |
786 | error = do_execve(filename, | |
65ea5b03 PA |
787 | (char __user * __user *) regs.cx, |
788 | (char __user * __user *) regs.dx, | |
1da177e4 LT |
789 | ®s); |
790 | if (error == 0) { | |
1da177e4 LT |
791 | /* Make sure we don't return using sysenter.. */ |
792 | set_thread_flag(TIF_IRET); | |
793 | } | |
794 | putname(filename); | |
795 | out: | |
796 | return error; | |
797 | } | |
798 | ||
799 | #define top_esp (THREAD_SIZE - sizeof(unsigned long)) | |
800 | #define top_ebp (THREAD_SIZE - 2*sizeof(unsigned long)) | |
801 | ||
802 | unsigned long get_wchan(struct task_struct *p) | |
803 | { | |
65ea5b03 | 804 | unsigned long bp, sp, ip; |
1da177e4 LT |
805 | unsigned long stack_page; |
806 | int count = 0; | |
807 | if (!p || p == current || p->state == TASK_RUNNING) | |
808 | return 0; | |
65e0fdff | 809 | stack_page = (unsigned long)task_stack_page(p); |
faca6227 | 810 | sp = p->thread.sp; |
65ea5b03 | 811 | if (!stack_page || sp < stack_page || sp > top_esp+stack_page) |
1da177e4 | 812 | return 0; |
65ea5b03 PA |
813 | /* include/asm-i386/system.h:switch_to() pushes bp last. */ |
814 | bp = *(unsigned long *) sp; | |
1da177e4 | 815 | do { |
65ea5b03 | 816 | if (bp < stack_page || bp > top_ebp+stack_page) |
1da177e4 | 817 | return 0; |
65ea5b03 PA |
818 | ip = *(unsigned long *) (bp+4); |
819 | if (!in_sched_functions(ip)) | |
820 | return ip; | |
821 | bp = *(unsigned long *) bp; | |
1da177e4 LT |
822 | } while (count++ < 16); |
823 | return 0; | |
824 | } | |
825 | ||
1da177e4 LT |
826 | unsigned long arch_align_stack(unsigned long sp) |
827 | { | |
c16b63e0 | 828 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space) |
1da177e4 LT |
829 | sp -= get_random_int() % 8192; |
830 | return sp & ~0xf; | |
831 | } | |
c1d171a0 JK |
832 | |
833 | unsigned long arch_randomize_brk(struct mm_struct *mm) | |
834 | { | |
835 | unsigned long range_end = mm->brk + 0x02000000; | |
836 | return randomize_range(mm->brk, range_end, 0) ? : mm->brk; | |
837 | } |