mm/fault, um: Fix compile error
[deliverable/linux.git] / arch / x86 / kernel / process.c
CommitLineData
c767a54b
JP
1#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
61c4628b
SS
3#include <linux/errno.h>
4#include <linux/kernel.h>
5#include <linux/mm.h>
6#include <linux/smp.h>
389d1fb1 7#include <linux/prctl.h>
61c4628b
SS
8#include <linux/slab.h>
9#include <linux/sched.h>
7f424a8b
PZ
10#include <linux/module.h>
11#include <linux/pm.h>
162a688e 12#include <linux/tick.h>
9d62dcdf 13#include <linux/random.h>
7c68af6e 14#include <linux/user-return-notifier.h>
814e2c84
AI
15#include <linux/dmi.h>
16#include <linux/utsname.h>
90e24014
RW
17#include <linux/stackprotector.h>
18#include <linux/tick.h>
19#include <linux/cpuidle.h>
61613521 20#include <trace/events/power.h>
24f1e32c 21#include <linux/hw_breakpoint.h>
93789b32 22#include <asm/cpu.h>
d3ec5cae 23#include <asm/apic.h>
2c1b284e 24#include <asm/syscalls.h>
389d1fb1
JF
25#include <asm/idle.h>
26#include <asm/uaccess.h>
b253149b 27#include <asm/mwait.h>
389d1fb1 28#include <asm/i387.h>
1361b83a 29#include <asm/fpu-internal.h>
66cb5917 30#include <asm/debugreg.h>
90e24014 31#include <asm/nmi.h>
375074cc 32#include <asm/tlbflush.h>
90e24014 33
45046892
TG
34/*
35 * per-CPU TSS segments. Threads are completely 'soft' on Linux,
36 * no more per-task TSS's. The TSS size is kept cacheline-aligned
37 * so they are allowed to end up in the .data..cacheline_aligned
38 * section. Since TSS's are completely CPU-local, we want them
39 * on exact cacheline boundaries, to eliminate cacheline ping-pong.
40 */
d0a0de21
AL
41__visible DEFINE_PER_CPU_SHARED_ALIGNED(struct tss_struct, cpu_tss) = {
42 .x86_tss = {
d9e05cc5 43 .sp0 = TOP_OF_INIT_STACK,
d0a0de21
AL
44#ifdef CONFIG_X86_32
45 .ss0 = __KERNEL_DS,
46 .ss1 = __KERNEL_CS,
47 .io_bitmap_base = INVALID_IO_BITMAP_OFFSET,
48#endif
49 },
50#ifdef CONFIG_X86_32
51 /*
52 * Note that the .io_bitmap member must be extra-big. This is because
53 * the CPU will access an additional byte beyond the end of the IO
54 * permission bitmap. The extra byte must be all 1 bits, and must
55 * be within the limit.
56 */
57 .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 },
58#endif
59};
de71ad2c 60EXPORT_PER_CPU_SYMBOL(cpu_tss);
45046892 61
90e24014
RW
62#ifdef CONFIG_X86_64
63static DEFINE_PER_CPU(unsigned char, is_idle);
64static ATOMIC_NOTIFIER_HEAD(idle_notifier);
65
66void idle_notifier_register(struct notifier_block *n)
67{
68 atomic_notifier_chain_register(&idle_notifier, n);
69}
70EXPORT_SYMBOL_GPL(idle_notifier_register);
71
72void idle_notifier_unregister(struct notifier_block *n)
73{
74 atomic_notifier_chain_unregister(&idle_notifier, n);
75}
76EXPORT_SYMBOL_GPL(idle_notifier_unregister);
77#endif
c1e3b377 78
aa283f49 79struct kmem_cache *task_xstate_cachep;
5ee481da 80EXPORT_SYMBOL_GPL(task_xstate_cachep);
61c4628b 81
55ccf3fe
SS
82/*
83 * this gets called so that we can store lazy state into memory and copy the
84 * current task into the new thread.
85 */
61c4628b
SS
86int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
87{
88 *dst = *src;
f1853505 89
dc56c0f9 90 dst->thread.fpu_counter = 0;
5e23fee2 91 dst->thread.fpu.has_fpu = 0;
5e23fee2 92 dst->thread.fpu.state = NULL;
6a5fe895 93 task_disable_lazy_fpu_restore(dst);
f1853505
ON
94 if (tsk_used_math(src)) {
95 int err = fpu_alloc(&dst->thread.fpu);
96 if (err)
97 return err;
304bceda 98 fpu_copy(dst, src);
aa283f49 99 }
61c4628b
SS
100 return 0;
101}
102
aa283f49 103void free_thread_xstate(struct task_struct *tsk)
61c4628b 104{
86603283 105 fpu_free(&tsk->thread.fpu);
aa283f49
SS
106}
107
38e7c572 108void arch_release_task_struct(struct task_struct *tsk)
aa283f49 109{
38e7c572 110 free_thread_xstate(tsk);
61c4628b
SS
111}
112
113void arch_task_cache_init(void)
114{
115 task_xstate_cachep =
116 kmem_cache_create("task_xstate", xstate_size,
117 __alignof__(union thread_xstate),
2dff4405 118 SLAB_PANIC | SLAB_NOTRACK, NULL);
7496d645 119 setup_xstate_comp();
61c4628b 120}
7f424a8b 121
389d1fb1
JF
122/*
123 * Free current thread data structures etc..
124 */
125void exit_thread(void)
126{
127 struct task_struct *me = current;
128 struct thread_struct *t = &me->thread;
250981e6 129 unsigned long *bp = t->io_bitmap_ptr;
389d1fb1 130
250981e6 131 if (bp) {
24933b82 132 struct tss_struct *tss = &per_cpu(cpu_tss, get_cpu());
389d1fb1 133
389d1fb1
JF
134 t->io_bitmap_ptr = NULL;
135 clear_thread_flag(TIF_IO_BITMAP);
136 /*
137 * Careful, clear this in the TSS too:
138 */
139 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
140 t->io_bitmap_max = 0;
141 put_cpu();
250981e6 142 kfree(bp);
389d1fb1 143 }
1dcc8d7b
SS
144
145 drop_fpu(me);
389d1fb1
JF
146}
147
148void flush_thread(void)
149{
150 struct task_struct *tsk = current;
151
24f1e32c 152 flush_ptrace_hw_breakpoint(tsk);
389d1fb1 153 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
110d7f75 154
f893959b
ON
155 if (!use_eager_fpu()) {
156 /* FPU state will be reallocated lazily at the first use. */
157 drop_fpu(tsk);
304bceda 158 free_thread_xstate(tsk);
c88d4748
BP
159 } else {
160 if (!tsk_used_math(tsk)) {
161 /* kthread execs. TODO: cleanup this horror. */
162 if (WARN_ON(init_fpu(tsk)))
163 force_sig(SIGKILL, tsk);
164 user_fpu_begin();
165 }
9cb6ce82 166 restore_init_xstate();
110d7f75 167 }
389d1fb1
JF
168}
169
170static void hard_disable_TSC(void)
171{
375074cc 172 cr4_set_bits(X86_CR4_TSD);
389d1fb1
JF
173}
174
175void disable_TSC(void)
176{
177 preempt_disable();
178 if (!test_and_set_thread_flag(TIF_NOTSC))
179 /*
180 * Must flip the CPU state synchronously with
181 * TIF_NOTSC in the current running context.
182 */
183 hard_disable_TSC();
184 preempt_enable();
185}
186
187static void hard_enable_TSC(void)
188{
375074cc 189 cr4_clear_bits(X86_CR4_TSD);
389d1fb1
JF
190}
191
192static void enable_TSC(void)
193{
194 preempt_disable();
195 if (test_and_clear_thread_flag(TIF_NOTSC))
196 /*
197 * Must flip the CPU state synchronously with
198 * TIF_NOTSC in the current running context.
199 */
200 hard_enable_TSC();
201 preempt_enable();
202}
203
204int get_tsc_mode(unsigned long adr)
205{
206 unsigned int val;
207
208 if (test_thread_flag(TIF_NOTSC))
209 val = PR_TSC_SIGSEGV;
210 else
211 val = PR_TSC_ENABLE;
212
213 return put_user(val, (unsigned int __user *)adr);
214}
215
216int set_tsc_mode(unsigned int val)
217{
218 if (val == PR_TSC_SIGSEGV)
219 disable_TSC();
220 else if (val == PR_TSC_ENABLE)
221 enable_TSC();
222 else
223 return -EINVAL;
224
225 return 0;
226}
227
228void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
229 struct tss_struct *tss)
230{
231 struct thread_struct *prev, *next;
232
233 prev = &prev_p->thread;
234 next = &next_p->thread;
235
ea8e61b7
PZ
236 if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
237 test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
238 unsigned long debugctl = get_debugctlmsr();
239
240 debugctl &= ~DEBUGCTLMSR_BTF;
241 if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
242 debugctl |= DEBUGCTLMSR_BTF;
243
244 update_debugctlmsr(debugctl);
245 }
389d1fb1 246
389d1fb1
JF
247 if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
248 test_tsk_thread_flag(next_p, TIF_NOTSC)) {
249 /* prev and next are different */
250 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
251 hard_disable_TSC();
252 else
253 hard_enable_TSC();
254 }
255
256 if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
257 /*
258 * Copy the relevant range of the IO bitmap.
259 * Normally this is 128 bytes or less:
260 */
261 memcpy(tss->io_bitmap, next->io_bitmap_ptr,
262 max(prev->io_bitmap_max, next->io_bitmap_max));
263 } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
264 /*
265 * Clear any possible leftover bits:
266 */
267 memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
268 }
7c68af6e 269 propagate_user_return_notify(prev_p, next_p);
389d1fb1
JF
270}
271
00dba564
TG
272/*
273 * Idle related variables and functions
274 */
d1896049 275unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
00dba564
TG
276EXPORT_SYMBOL(boot_option_idle_override);
277
a476bda3 278static void (*x86_idle)(void);
00dba564 279
90e24014
RW
280#ifndef CONFIG_SMP
281static inline void play_dead(void)
282{
283 BUG();
284}
285#endif
286
287#ifdef CONFIG_X86_64
288void enter_idle(void)
289{
c6ae41e7 290 this_cpu_write(is_idle, 1);
90e24014
RW
291 atomic_notifier_call_chain(&idle_notifier, IDLE_START, NULL);
292}
293
294static void __exit_idle(void)
295{
296 if (x86_test_and_clear_bit_percpu(0, is_idle) == 0)
297 return;
298 atomic_notifier_call_chain(&idle_notifier, IDLE_END, NULL);
299}
300
301/* Called from interrupts to signify idle end */
302void exit_idle(void)
303{
304 /* idle loop has pid 0 */
305 if (current->pid)
306 return;
307 __exit_idle();
308}
309#endif
310
7d1a9417
TG
311void arch_cpu_idle_enter(void)
312{
313 local_touch_nmi();
314 enter_idle();
315}
90e24014 316
7d1a9417
TG
317void arch_cpu_idle_exit(void)
318{
319 __exit_idle();
320}
90e24014 321
7d1a9417
TG
322void arch_cpu_idle_dead(void)
323{
324 play_dead();
325}
90e24014 326
7d1a9417
TG
327/*
328 * Called from the generic idle code.
329 */
330void arch_cpu_idle(void)
331{
16f8b05a 332 x86_idle();
90e24014
RW
333}
334
00dba564 335/*
7d1a9417 336 * We use this if we don't have any better idle routine..
00dba564
TG
337 */
338void default_idle(void)
339{
4d0e42cc 340 trace_cpu_idle_rcuidle(1, smp_processor_id());
7d1a9417 341 safe_halt();
4d0e42cc 342 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
00dba564 343}
60b8b1de 344#ifdef CONFIG_APM_MODULE
00dba564
TG
345EXPORT_SYMBOL(default_idle);
346#endif
347
6a377ddc
LB
348#ifdef CONFIG_XEN
349bool xen_set_default_idle(void)
e5fd47bf 350{
a476bda3 351 bool ret = !!x86_idle;
e5fd47bf 352
a476bda3 353 x86_idle = default_idle;
e5fd47bf
KRW
354
355 return ret;
356}
6a377ddc 357#endif
d3ec5cae
IV
358void stop_this_cpu(void *dummy)
359{
360 local_irq_disable();
361 /*
362 * Remove this CPU:
363 */
4f062896 364 set_cpu_online(smp_processor_id(), false);
d3ec5cae
IV
365 disable_local_APIC();
366
27be4570
LB
367 for (;;)
368 halt();
7f424a8b
PZ
369}
370
02c68a02
LB
371bool amd_e400_c1e_detected;
372EXPORT_SYMBOL(amd_e400_c1e_detected);
aa276e1c 373
02c68a02 374static cpumask_var_t amd_e400_c1e_mask;
4faac97d 375
02c68a02 376void amd_e400_remove_cpu(int cpu)
4faac97d 377{
02c68a02
LB
378 if (amd_e400_c1e_mask != NULL)
379 cpumask_clear_cpu(cpu, amd_e400_c1e_mask);
4faac97d
TG
380}
381
aa276e1c 382/*
02c68a02 383 * AMD Erratum 400 aware idle routine. We check for C1E active in the interrupt
aa276e1c
TG
384 * pending message MSR. If we detect C1E, then we handle it the same
385 * way as C3 power states (local apic timer and TSC stop)
386 */
02c68a02 387static void amd_e400_idle(void)
aa276e1c 388{
02c68a02 389 if (!amd_e400_c1e_detected) {
aa276e1c
TG
390 u32 lo, hi;
391
392 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
e8c534ec 393
aa276e1c 394 if (lo & K8_INTP_C1E_ACTIVE_MASK) {
02c68a02 395 amd_e400_c1e_detected = true;
40fb1715 396 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
09bfeea1 397 mark_tsc_unstable("TSC halt in AMD C1E");
c767a54b 398 pr_info("System has AMD C1E enabled\n");
aa276e1c
TG
399 }
400 }
401
02c68a02 402 if (amd_e400_c1e_detected) {
aa276e1c
TG
403 int cpu = smp_processor_id();
404
02c68a02
LB
405 if (!cpumask_test_cpu(cpu, amd_e400_c1e_mask)) {
406 cpumask_set_cpu(cpu, amd_e400_c1e_mask);
162a688e
TG
407 /* Force broadcast so ACPI can not interfere. */
408 tick_broadcast_force();
c767a54b 409 pr_info("Switch to broadcast mode on CPU%d\n", cpu);
aa276e1c 410 }
435c350e 411 tick_broadcast_enter();
0beefa20 412
aa276e1c 413 default_idle();
0beefa20
TG
414
415 /*
416 * The switch back from broadcast mode needs to be
417 * called with interrupts disabled.
418 */
ea811747 419 local_irq_disable();
435c350e 420 tick_broadcast_exit();
ea811747 421 local_irq_enable();
aa276e1c
TG
422 } else
423 default_idle();
424}
425
b253149b
LB
426/*
427 * Intel Core2 and older machines prefer MWAIT over HALT for C1.
428 * We can't rely on cpuidle installing MWAIT, because it will not load
429 * on systems that support only C1 -- so the boot default must be MWAIT.
430 *
431 * Some AMD machines are the opposite, they depend on using HALT.
432 *
433 * So for default C1, which is used during boot until cpuidle loads,
434 * use MWAIT-C1 on Intel HW that has it, else use HALT.
435 */
436static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
437{
438 if (c->x86_vendor != X86_VENDOR_INTEL)
439 return 0;
440
441 if (!cpu_has(c, X86_FEATURE_MWAIT))
442 return 0;
443
444 return 1;
445}
446
447/*
448 * MONITOR/MWAIT with no hints, used for default default C1 state.
449 * This invokes MWAIT with interrutps enabled and no flags,
450 * which is backwards compatible with the original MWAIT implementation.
451 */
452
453static void mwait_idle(void)
454{
f8e617f4
MG
455 if (!current_set_polling_and_test()) {
456 if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) {
457 smp_mb(); /* quirk */
b253149b 458 clflush((void *)&current_thread_info()->flags);
f8e617f4
MG
459 smp_mb(); /* quirk */
460 }
b253149b
LB
461
462 __monitor((void *)&current_thread_info()->flags, 0, 0);
b253149b
LB
463 if (!need_resched())
464 __sti_mwait(0, 0);
465 else
466 local_irq_enable();
f8e617f4 467 } else {
b253149b 468 local_irq_enable();
f8e617f4
MG
469 }
470 __current_clr_polling();
b253149b
LB
471}
472
148f9bb8 473void select_idle_routine(const struct cpuinfo_x86 *c)
7f424a8b 474{
3e5095d1 475#ifdef CONFIG_SMP
7d1a9417 476 if (boot_option_idle_override == IDLE_POLL && smp_num_siblings > 1)
c767a54b 477 pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
7f424a8b 478#endif
7d1a9417 479 if (x86_idle || boot_option_idle_override == IDLE_POLL)
6ddd2a27
TG
480 return;
481
7d7dc116 482 if (cpu_has_bug(c, X86_BUG_AMD_APIC_C1E)) {
9d8888c2 483 /* E400: APIC timer interrupt does not wake up CPU from C1e */
c767a54b 484 pr_info("using AMD E400 aware idle routine\n");
a476bda3 485 x86_idle = amd_e400_idle;
b253149b
LB
486 } else if (prefer_mwait_c1_over_halt(c)) {
487 pr_info("using mwait in idle threads\n");
488 x86_idle = mwait_idle;
6ddd2a27 489 } else
a476bda3 490 x86_idle = default_idle;
7f424a8b
PZ
491}
492
02c68a02 493void __init init_amd_e400_c1e_mask(void)
30e1e6d1 494{
02c68a02 495 /* If we're using amd_e400_idle, we need to allocate amd_e400_c1e_mask. */
a476bda3 496 if (x86_idle == amd_e400_idle)
02c68a02 497 zalloc_cpumask_var(&amd_e400_c1e_mask, GFP_KERNEL);
30e1e6d1
RR
498}
499
7f424a8b
PZ
500static int __init idle_setup(char *str)
501{
ab6bc3e3
CG
502 if (!str)
503 return -EINVAL;
504
7f424a8b 505 if (!strcmp(str, "poll")) {
c767a54b 506 pr_info("using polling idle threads\n");
d1896049 507 boot_option_idle_override = IDLE_POLL;
7d1a9417 508 cpu_idle_poll_ctrl(true);
d1896049 509 } else if (!strcmp(str, "halt")) {
c1e3b377
ZY
510 /*
511 * When the boot option of idle=halt is added, halt is
512 * forced to be used for CPU idle. In such case CPU C2/C3
513 * won't be used again.
514 * To continue to load the CPU idle driver, don't touch
515 * the boot_option_idle_override.
516 */
a476bda3 517 x86_idle = default_idle;
d1896049 518 boot_option_idle_override = IDLE_HALT;
da5e09a1
ZY
519 } else if (!strcmp(str, "nomwait")) {
520 /*
521 * If the boot option of "idle=nomwait" is added,
522 * it means that mwait will be disabled for CPU C2/C3
523 * states. In such case it won't touch the variable
524 * of boot_option_idle_override.
525 */
d1896049 526 boot_option_idle_override = IDLE_NOMWAIT;
c1e3b377 527 } else
7f424a8b
PZ
528 return -1;
529
7f424a8b
PZ
530 return 0;
531}
532early_param("idle", idle_setup);
533
9d62dcdf
AW
534unsigned long arch_align_stack(unsigned long sp)
535{
536 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
537 sp -= get_random_int() % 8192;
538 return sp & ~0xf;
539}
540
541unsigned long arch_randomize_brk(struct mm_struct *mm)
542{
543 unsigned long range_end = mm->brk + 0x02000000;
544 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
545}
546
This page took 0.775692 seconds and 5 git commands to generate.