]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/process.c
x86/speculation: Reorganize speculation control MSRs update
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / process.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
c767a54b
JP
2#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3
61c4628b
SS
4#include <linux/errno.h>
5#include <linux/kernel.h>
6#include <linux/mm.h>
7#include <linux/smp.h>
389d1fb1 8#include <linux/prctl.h>
61c4628b
SS
9#include <linux/slab.h>
10#include <linux/sched.h>
4c822698 11#include <linux/sched/idle.h>
b17b0153 12#include <linux/sched/debug.h>
29930025 13#include <linux/sched/task.h>
68db0cf1 14#include <linux/sched/task_stack.h>
186f4360
PG
15#include <linux/init.h>
16#include <linux/export.h>
7f424a8b 17#include <linux/pm.h>
162a688e 18#include <linux/tick.h>
9d62dcdf 19#include <linux/random.h>
7c68af6e 20#include <linux/user-return-notifier.h>
814e2c84
AI
21#include <linux/dmi.h>
22#include <linux/utsname.h>
90e24014
RW
23#include <linux/stackprotector.h>
24#include <linux/tick.h>
25#include <linux/cpuidle.h>
61613521 26#include <trace/events/power.h>
24f1e32c 27#include <linux/hw_breakpoint.h>
93789b32 28#include <asm/cpu.h>
d3ec5cae 29#include <asm/apic.h>
2c1b284e 30#include <asm/syscalls.h>
7c0f6ba6 31#include <linux/uaccess.h>
b253149b 32#include <asm/mwait.h>
78f7f1e5 33#include <asm/fpu/internal.h>
66cb5917 34#include <asm/debugreg.h>
90e24014 35#include <asm/nmi.h>
375074cc 36#include <asm/tlbflush.h>
8838eb6c 37#include <asm/mce.h>
9fda6a06 38#include <asm/vm86.h>
7b32aead 39#include <asm/switch_to.h>
b7ffc44d 40#include <asm/desc.h>
e9ea1e7f 41#include <asm/prctl.h>
5407b7f8 42#include <asm/spec-ctrl.h>
90e24014 43
45046892
TG
44/*
45 * per-CPU TSS segments. Threads are completely 'soft' on Linux,
46 * no more per-task TSS's. The TSS size is kept cacheline-aligned
47 * so they are allowed to end up in the .data..cacheline_aligned
48 * section. Since TSS's are completely CPU-local, we want them
49 * on exact cacheline boundaries, to eliminate cacheline ping-pong.
50 */
2fd9c41a 51__visible DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw) = {
d0a0de21 52 .x86_tss = {
20bb8344
AL
53 /*
54 * .sp0 is only used when entering ring 0 from a lower
55 * privilege level. Since the init task never runs anything
56 * but ring 0 code, there is no need for a valid value here.
57 * Poison it.
58 */
59 .sp0 = (1UL << (BITS_PER_LONG-1)) + 1,
9aaefe7b
AL
60
61#ifdef CONFIG_X86_64
62 /*
63 * .sp1 is cpu_current_top_of_stack. The init task never
64 * runs user code, but cpu_current_top_of_stack should still
65 * be well defined before the first context switch.
66 */
67 .sp1 = TOP_OF_INIT_STACK,
68#endif
69
d0a0de21
AL
70#ifdef CONFIG_X86_32
71 .ss0 = __KERNEL_DS,
72 .ss1 = __KERNEL_CS,
73 .io_bitmap_base = INVALID_IO_BITMAP_OFFSET,
74#endif
75 },
76#ifdef CONFIG_X86_32
77 /*
78 * Note that the .io_bitmap member must be extra-big. This is because
79 * the CPU will access an additional byte beyond the end of the IO
80 * permission bitmap. The extra byte must be all 1 bits, and must
81 * be within the limit.
82 */
83 .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 },
84#endif
85};
c482feef 86EXPORT_PER_CPU_SYMBOL(cpu_tss_rw);
45046892 87
b7ceaec1
AL
88DEFINE_PER_CPU(bool, __tss_limit_invalid);
89EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
b7ffc44d 90
55ccf3fe
SS
91/*
92 * this gets called so that we can store lazy state into memory and copy the
93 * current task into the new thread.
94 */
61c4628b
SS
95int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
96{
5aaeb5c0 97 memcpy(dst, src, arch_task_struct_size);
2459ee86
AL
98#ifdef CONFIG_VM86
99 dst->thread.vm86 = NULL;
100#endif
f1853505 101
c69e098b 102 return fpu__copy(&dst->thread.fpu, &src->thread.fpu);
61c4628b 103}
7f424a8b 104
389d1fb1
JF
105/*
106 * Free current thread data structures etc..
107 */
e6464694 108void exit_thread(struct task_struct *tsk)
389d1fb1 109{
e6464694 110 struct thread_struct *t = &tsk->thread;
250981e6 111 unsigned long *bp = t->io_bitmap_ptr;
ca6787ba 112 struct fpu *fpu = &t->fpu;
389d1fb1 113
250981e6 114 if (bp) {
c482feef 115 struct tss_struct *tss = &per_cpu(cpu_tss_rw, get_cpu());
389d1fb1 116
389d1fb1
JF
117 t->io_bitmap_ptr = NULL;
118 clear_thread_flag(TIF_IO_BITMAP);
119 /*
120 * Careful, clear this in the TSS too:
121 */
122 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
123 t->io_bitmap_max = 0;
124 put_cpu();
250981e6 125 kfree(bp);
389d1fb1 126 }
1dcc8d7b 127
9fda6a06
BG
128 free_vm86(t);
129
50338615 130 fpu__drop(fpu);
389d1fb1
JF
131}
132
133void flush_thread(void)
134{
135 struct task_struct *tsk = current;
136
24f1e32c 137 flush_ptrace_hw_breakpoint(tsk);
389d1fb1 138 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
110d7f75 139
04c8e01d 140 fpu__clear(&tsk->thread.fpu);
389d1fb1
JF
141}
142
389d1fb1
JF
143void disable_TSC(void)
144{
145 preempt_disable();
146 if (!test_and_set_thread_flag(TIF_NOTSC))
147 /*
148 * Must flip the CPU state synchronously with
149 * TIF_NOTSC in the current running context.
150 */
5a920155 151 cr4_set_bits(X86_CR4_TSD);
389d1fb1
JF
152 preempt_enable();
153}
154
389d1fb1
JF
155static void enable_TSC(void)
156{
157 preempt_disable();
158 if (test_and_clear_thread_flag(TIF_NOTSC))
159 /*
160 * Must flip the CPU state synchronously with
161 * TIF_NOTSC in the current running context.
162 */
5a920155 163 cr4_clear_bits(X86_CR4_TSD);
389d1fb1
JF
164 preempt_enable();
165}
166
167int get_tsc_mode(unsigned long adr)
168{
169 unsigned int val;
170
171 if (test_thread_flag(TIF_NOTSC))
172 val = PR_TSC_SIGSEGV;
173 else
174 val = PR_TSC_ENABLE;
175
176 return put_user(val, (unsigned int __user *)adr);
177}
178
179int set_tsc_mode(unsigned int val)
180{
181 if (val == PR_TSC_SIGSEGV)
182 disable_TSC();
183 else if (val == PR_TSC_ENABLE)
184 enable_TSC();
185 else
186 return -EINVAL;
187
188 return 0;
189}
190
e9ea1e7f
KH
191DEFINE_PER_CPU(u64, msr_misc_features_shadow);
192
193static void set_cpuid_faulting(bool on)
194{
195 u64 msrval;
196
197 msrval = this_cpu_read(msr_misc_features_shadow);
198 msrval &= ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
199 msrval |= (on << MSR_MISC_FEATURES_ENABLES_CPUID_FAULT_BIT);
200 this_cpu_write(msr_misc_features_shadow, msrval);
201 wrmsrl(MSR_MISC_FEATURES_ENABLES, msrval);
202}
203
204static void disable_cpuid(void)
205{
206 preempt_disable();
207 if (!test_and_set_thread_flag(TIF_NOCPUID)) {
208 /*
209 * Must flip the CPU state synchronously with
210 * TIF_NOCPUID in the current running context.
211 */
212 set_cpuid_faulting(true);
213 }
214 preempt_enable();
215}
216
217static void enable_cpuid(void)
218{
219 preempt_disable();
220 if (test_and_clear_thread_flag(TIF_NOCPUID)) {
221 /*
222 * Must flip the CPU state synchronously with
223 * TIF_NOCPUID in the current running context.
224 */
225 set_cpuid_faulting(false);
226 }
227 preempt_enable();
228}
229
230static int get_cpuid_mode(void)
231{
232 return !test_thread_flag(TIF_NOCPUID);
233}
234
235static int set_cpuid_mode(struct task_struct *task, unsigned long cpuid_enabled)
236{
237 if (!static_cpu_has(X86_FEATURE_CPUID_FAULT))
238 return -ENODEV;
239
240 if (cpuid_enabled)
241 enable_cpuid();
242 else
243 disable_cpuid();
244
245 return 0;
246}
247
248/*
249 * Called immediately after a successful exec.
250 */
251void arch_setup_new_exec(void)
252{
253 /* If cpuid was previously disabled for this task, re-enable it. */
254 if (test_thread_flag(TIF_NOCPUID))
255 enable_cpuid();
256}
257
af8b3cd3
KH
258static inline void switch_to_bitmap(struct tss_struct *tss,
259 struct thread_struct *prev,
260 struct thread_struct *next,
261 unsigned long tifp, unsigned long tifn)
262{
263 if (tifn & _TIF_IO_BITMAP) {
264 /*
265 * Copy the relevant range of the IO bitmap.
266 * Normally this is 128 bytes or less:
267 */
268 memcpy(tss->io_bitmap, next->io_bitmap_ptr,
269 max(prev->io_bitmap_max, next->io_bitmap_max));
270 /*
271 * Make sure that the TSS limit is correct for the CPU
272 * to notice the IO bitmap.
273 */
274 refresh_tss_limit();
275 } else if (tifp & _TIF_IO_BITMAP) {
276 /*
277 * Clear any possible leftover bits:
278 */
279 memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
280 }
281}
282
3f6a3b03
TG
283#ifdef CONFIG_SMP
284
285struct ssb_state {
286 struct ssb_state *shared_state;
287 raw_spinlock_t lock;
288 unsigned int disable_state;
289 unsigned long local_state;
290};
291
292#define LSTATE_SSB 0
293
294static DEFINE_PER_CPU(struct ssb_state, ssb_state);
295
296void speculative_store_bypass_ht_init(void)
5407b7f8 297{
3f6a3b03
TG
298 struct ssb_state *st = this_cpu_ptr(&ssb_state);
299 unsigned int this_cpu = smp_processor_id();
300 unsigned int cpu;
301
302 st->local_state = 0;
303
304 /*
305 * Shared state setup happens once on the first bringup
306 * of the CPU. It's not destroyed on CPU hotunplug.
307 */
308 if (st->shared_state)
309 return;
310
311 raw_spin_lock_init(&st->lock);
312
313 /*
314 * Go over HT siblings and check whether one of them has set up the
315 * shared state pointer already.
316 */
317 for_each_cpu(cpu, topology_sibling_cpumask(this_cpu)) {
318 if (cpu == this_cpu)
319 continue;
320
321 if (!per_cpu(ssb_state, cpu).shared_state)
322 continue;
323
324 /* Link it to the state of the sibling: */
325 st->shared_state = per_cpu(ssb_state, cpu).shared_state;
326 return;
327 }
328
329 /*
330 * First HT sibling to come up on the core. Link shared state of
331 * the first HT sibling to itself. The siblings on the same core
332 * which come up later will see the shared state pointer and link
333 * themself to the state of this CPU.
334 */
335 st->shared_state = st;
336}
5407b7f8 337
3f6a3b03
TG
338/*
339 * Logic is: First HT sibling enables SSBD for both siblings in the core
340 * and last sibling to disable it, disables it for the whole core. This how
341 * MSR_SPEC_CTRL works in "hardware":
342 *
343 * CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL
344 */
345static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
346{
347 struct ssb_state *st = this_cpu_ptr(&ssb_state);
348 u64 msr = x86_amd_ls_cfg_base;
349
350 if (!static_cpu_has(X86_FEATURE_ZEN)) {
351 msr |= ssbd_tif_to_amd_ls_cfg(tifn);
5407b7f8 352 wrmsrl(MSR_AMD64_LS_CFG, msr);
3f6a3b03
TG
353 return;
354 }
355
356 if (tifn & _TIF_SSBD) {
357 /*
358 * Since this can race with prctl(), block reentry on the
359 * same CPU.
360 */
361 if (__test_and_set_bit(LSTATE_SSB, &st->local_state))
362 return;
363
364 msr |= x86_amd_ls_cfg_ssbd_mask;
365
366 raw_spin_lock(&st->shared_state->lock);
367 /* First sibling enables SSBD: */
368 if (!st->shared_state->disable_state)
369 wrmsrl(MSR_AMD64_LS_CFG, msr);
370 st->shared_state->disable_state++;
371 raw_spin_unlock(&st->shared_state->lock);
5407b7f8 372 } else {
3f6a3b03
TG
373 if (!__test_and_clear_bit(LSTATE_SSB, &st->local_state))
374 return;
375
376 raw_spin_lock(&st->shared_state->lock);
377 st->shared_state->disable_state--;
378 if (!st->shared_state->disable_state)
379 wrmsrl(MSR_AMD64_LS_CFG, msr);
380 raw_spin_unlock(&st->shared_state->lock);
5407b7f8
TG
381 }
382}
3f6a3b03
TG
383#else
384static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
385{
386 u64 msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn);
387
388 wrmsrl(MSR_AMD64_LS_CFG, msr);
389}
390#endif
391
65e02bbd
TL
392static __always_inline void amd_set_ssb_virt_state(unsigned long tifn)
393{
394 /*
395 * SSBD has the same definition in SPEC_CTRL and VIRT_SPEC_CTRL,
396 * so ssbd_tif_to_spec_ctrl() just works.
397 */
398 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, ssbd_tif_to_spec_ctrl(tifn));
399}
400
1e145265
TC
401/*
402 * Update the MSRs managing speculation control, during context switch.
403 *
404 * tifp: Previous task's thread flags
405 * tifn: Next task's thread flags
406 */
407static __always_inline void __speculation_ctrl_update(unsigned long tifp,
408 unsigned long tifn)
3f6a3b03 409{
1e145265
TC
410 u64 msr = x86_spec_ctrl_base;
411 bool updmsr = false;
412
413 /* If TIF_SSBD is different, select the proper mitigation method */
414 if ((tifp ^ tifn) & _TIF_SSBD) {
415 if (static_cpu_has(X86_FEATURE_VIRT_SSBD)) {
416 amd_set_ssb_virt_state(tifn);
417 } else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
418 amd_set_core_ssb_state(tifn);
419 } else if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
420 static_cpu_has(X86_FEATURE_AMD_SSBD)) {
421 msr |= ssbd_tif_to_spec_ctrl(tifn);
422 updmsr = true;
423 }
424 }
3f6a3b03 425
1e145265
TC
426 if (updmsr)
427 wrmsrl(MSR_IA32_SPEC_CTRL, msr);
3f6a3b03 428}
5407b7f8 429
8fce7184 430void speculation_ctrl_update(unsigned long tif)
5407b7f8 431{
1e145265 432 /* Forced update. Make sure all relevant TIF flags are different */
3f6a3b03 433 preempt_disable();
1e145265 434 __speculation_ctrl_update(~tif, tif);
3f6a3b03 435 preempt_enable();
5407b7f8
TG
436}
437
389d1fb1
JF
438void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
439 struct tss_struct *tss)
440{
441 struct thread_struct *prev, *next;
af8b3cd3 442 unsigned long tifp, tifn;
389d1fb1
JF
443
444 prev = &prev_p->thread;
445 next = &next_p->thread;
446
af8b3cd3
KH
447 tifn = READ_ONCE(task_thread_info(next_p)->flags);
448 tifp = READ_ONCE(task_thread_info(prev_p)->flags);
449 switch_to_bitmap(tss, prev, next, tifp, tifn);
450
451 propagate_user_return_notify(prev_p, next_p);
452
b9894a2f
KH
453 if ((tifp & _TIF_BLOCKSTEP || tifn & _TIF_BLOCKSTEP) &&
454 arch_has_block_step()) {
455 unsigned long debugctl, msk;
ea8e61b7 456
b9894a2f 457 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
ea8e61b7 458 debugctl &= ~DEBUGCTLMSR_BTF;
b9894a2f
KH
459 msk = tifn & _TIF_BLOCKSTEP;
460 debugctl |= (msk >> TIF_BLOCKSTEP) << DEBUGCTLMSR_BTF_SHIFT;
461 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
ea8e61b7 462 }
389d1fb1 463
5a920155 464 if ((tifp ^ tifn) & _TIF_NOTSC)
9d0b6232 465 cr4_toggle_bits_irqsoff(X86_CR4_TSD);
e9ea1e7f
KH
466
467 if ((tifp ^ tifn) & _TIF_NOCPUID)
468 set_cpuid_faulting(!!(tifn & _TIF_NOCPUID));
5407b7f8 469
1e145265 470 __speculation_ctrl_update(tifp, tifn);
389d1fb1
JF
471}
472
00dba564
TG
473/*
474 * Idle related variables and functions
475 */
d1896049 476unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
00dba564
TG
477EXPORT_SYMBOL(boot_option_idle_override);
478
a476bda3 479static void (*x86_idle)(void);
00dba564 480
90e24014
RW
481#ifndef CONFIG_SMP
482static inline void play_dead(void)
483{
484 BUG();
485}
486#endif
487
7d1a9417
TG
488void arch_cpu_idle_enter(void)
489{
6a369583 490 tsc_verify_tsc_adjust(false);
7d1a9417 491 local_touch_nmi();
7d1a9417 492}
90e24014 493
7d1a9417
TG
494void arch_cpu_idle_dead(void)
495{
496 play_dead();
497}
90e24014 498
7d1a9417
TG
499/*
500 * Called from the generic idle code.
501 */
502void arch_cpu_idle(void)
503{
16f8b05a 504 x86_idle();
90e24014
RW
505}
506
00dba564 507/*
7d1a9417 508 * We use this if we don't have any better idle routine..
00dba564 509 */
6727ad9e 510void __cpuidle default_idle(void)
00dba564 511{
4d0e42cc 512 trace_cpu_idle_rcuidle(1, smp_processor_id());
7d1a9417 513 safe_halt();
4d0e42cc 514 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
00dba564 515}
60b8b1de 516#ifdef CONFIG_APM_MODULE
00dba564
TG
517EXPORT_SYMBOL(default_idle);
518#endif
519
6a377ddc
LB
520#ifdef CONFIG_XEN
521bool xen_set_default_idle(void)
e5fd47bf 522{
a476bda3 523 bool ret = !!x86_idle;
e5fd47bf 524
a476bda3 525 x86_idle = default_idle;
e5fd47bf
KRW
526
527 return ret;
528}
6a377ddc 529#endif
bba4ed01 530
d3ec5cae
IV
531void stop_this_cpu(void *dummy)
532{
533 local_irq_disable();
534 /*
535 * Remove this CPU:
536 */
4f062896 537 set_cpu_online(smp_processor_id(), false);
d3ec5cae 538 disable_local_APIC();
8838eb6c 539 mcheck_cpu_clear(this_cpu_ptr(&cpu_info));
d3ec5cae 540
f23d74f6
TL
541 /*
542 * Use wbinvd on processors that support SME. This provides support
543 * for performing a successful kexec when going from SME inactive
544 * to SME active (or vice-versa). The cache must be cleared so that
545 * if there are entries with the same physical address, both with and
546 * without the encryption bit, they don't race each other when flushed
547 * and potentially end up with the wrong entry being committed to
548 * memory.
549 */
550 if (boot_cpu_has(X86_FEATURE_SME))
551 native_wbinvd();
bba4ed01
TL
552 for (;;) {
553 /*
f23d74f6
TL
554 * Use native_halt() so that memory contents don't change
555 * (stack usage and variables) after possibly issuing the
556 * native_wbinvd() above.
bba4ed01 557 */
f23d74f6 558 native_halt();
bba4ed01 559 }
7f424a8b
PZ
560}
561
aa276e1c 562/*
07c94a38
BP
563 * AMD Erratum 400 aware idle routine. We handle it the same way as C3 power
564 * states (local apic timer and TSC stop).
aa276e1c 565 */
02c68a02 566static void amd_e400_idle(void)
aa276e1c 567{
07c94a38
BP
568 /*
569 * We cannot use static_cpu_has_bug() here because X86_BUG_AMD_APIC_C1E
570 * gets set after static_cpu_has() places have been converted via
571 * alternatives.
572 */
573 if (!boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
574 default_idle();
575 return;
aa276e1c
TG
576 }
577
07c94a38 578 tick_broadcast_enter();
aa276e1c 579
07c94a38 580 default_idle();
0beefa20 581
07c94a38
BP
582 /*
583 * The switch back from broadcast mode needs to be called with
584 * interrupts disabled.
585 */
586 local_irq_disable();
587 tick_broadcast_exit();
588 local_irq_enable();
aa276e1c
TG
589}
590
b253149b
LB
591/*
592 * Intel Core2 and older machines prefer MWAIT over HALT for C1.
593 * We can't rely on cpuidle installing MWAIT, because it will not load
594 * on systems that support only C1 -- so the boot default must be MWAIT.
595 *
596 * Some AMD machines are the opposite, they depend on using HALT.
597 *
598 * So for default C1, which is used during boot until cpuidle loads,
599 * use MWAIT-C1 on Intel HW that has it, else use HALT.
600 */
601static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
602{
603 if (c->x86_vendor != X86_VENDOR_INTEL)
604 return 0;
605
08e237fa 606 if (!cpu_has(c, X86_FEATURE_MWAIT) || static_cpu_has_bug(X86_BUG_MONITOR))
b253149b
LB
607 return 0;
608
609 return 1;
610}
611
612/*
0fb0328d
HR
613 * MONITOR/MWAIT with no hints, used for default C1 state. This invokes MWAIT
614 * with interrupts enabled and no flags, which is backwards compatible with the
615 * original MWAIT implementation.
b253149b 616 */
6727ad9e 617static __cpuidle void mwait_idle(void)
b253149b 618{
f8e617f4 619 if (!current_set_polling_and_test()) {
e43d0189 620 trace_cpu_idle_rcuidle(1, smp_processor_id());
f8e617f4 621 if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) {
ca59809f 622 mb(); /* quirk */
b253149b 623 clflush((void *)&current_thread_info()->flags);
ca59809f 624 mb(); /* quirk */
f8e617f4 625 }
b253149b
LB
626
627 __monitor((void *)&current_thread_info()->flags, 0, 0);
b253149b
LB
628 if (!need_resched())
629 __sti_mwait(0, 0);
630 else
631 local_irq_enable();
e43d0189 632 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
f8e617f4 633 } else {
b253149b 634 local_irq_enable();
f8e617f4
MG
635 }
636 __current_clr_polling();
b253149b
LB
637}
638
148f9bb8 639void select_idle_routine(const struct cpuinfo_x86 *c)
7f424a8b 640{
3e5095d1 641#ifdef CONFIG_SMP
7d1a9417 642 if (boot_option_idle_override == IDLE_POLL && smp_num_siblings > 1)
c767a54b 643 pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
7f424a8b 644#endif
7d1a9417 645 if (x86_idle || boot_option_idle_override == IDLE_POLL)
6ddd2a27
TG
646 return;
647
3344ed30 648 if (boot_cpu_has_bug(X86_BUG_AMD_E400)) {
c767a54b 649 pr_info("using AMD E400 aware idle routine\n");
a476bda3 650 x86_idle = amd_e400_idle;
b253149b
LB
651 } else if (prefer_mwait_c1_over_halt(c)) {
652 pr_info("using mwait in idle threads\n");
653 x86_idle = mwait_idle;
6ddd2a27 654 } else
a476bda3 655 x86_idle = default_idle;
7f424a8b
PZ
656}
657
07c94a38 658void amd_e400_c1e_apic_setup(void)
30e1e6d1 659{
07c94a38
BP
660 if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
661 pr_info("Switch to broadcast mode on CPU%d\n", smp_processor_id());
662 local_irq_disable();
663 tick_broadcast_force();
664 local_irq_enable();
665 }
30e1e6d1
RR
666}
667
e7ff3a47
TG
668void __init arch_post_acpi_subsys_init(void)
669{
670 u32 lo, hi;
671
672 if (!boot_cpu_has_bug(X86_BUG_AMD_E400))
673 return;
674
675 /*
676 * AMD E400 detection needs to happen after ACPI has been enabled. If
677 * the machine is affected K8_INTP_C1E_ACTIVE_MASK bits are set in
678 * MSR_K8_INT_PENDING_MSG.
679 */
680 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
681 if (!(lo & K8_INTP_C1E_ACTIVE_MASK))
682 return;
683
684 boot_cpu_set_bug(X86_BUG_AMD_APIC_C1E);
685
686 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
687 mark_tsc_unstable("TSC halt in AMD C1E");
688 pr_info("System has AMD C1E enabled\n");
689}
690
7f424a8b
PZ
691static int __init idle_setup(char *str)
692{
ab6bc3e3
CG
693 if (!str)
694 return -EINVAL;
695
7f424a8b 696 if (!strcmp(str, "poll")) {
c767a54b 697 pr_info("using polling idle threads\n");
d1896049 698 boot_option_idle_override = IDLE_POLL;
7d1a9417 699 cpu_idle_poll_ctrl(true);
d1896049 700 } else if (!strcmp(str, "halt")) {
c1e3b377
ZY
701 /*
702 * When the boot option of idle=halt is added, halt is
703 * forced to be used for CPU idle. In such case CPU C2/C3
704 * won't be used again.
705 * To continue to load the CPU idle driver, don't touch
706 * the boot_option_idle_override.
707 */
a476bda3 708 x86_idle = default_idle;
d1896049 709 boot_option_idle_override = IDLE_HALT;
da5e09a1
ZY
710 } else if (!strcmp(str, "nomwait")) {
711 /*
712 * If the boot option of "idle=nomwait" is added,
713 * it means that mwait will be disabled for CPU C2/C3
714 * states. In such case it won't touch the variable
715 * of boot_option_idle_override.
716 */
d1896049 717 boot_option_idle_override = IDLE_NOMWAIT;
c1e3b377 718 } else
7f424a8b
PZ
719 return -1;
720
7f424a8b
PZ
721 return 0;
722}
723early_param("idle", idle_setup);
724
9d62dcdf
AW
725unsigned long arch_align_stack(unsigned long sp)
726{
727 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
728 sp -= get_random_int() % 8192;
729 return sp & ~0xf;
730}
731
732unsigned long arch_randomize_brk(struct mm_struct *mm)
733{
9c6f0902 734 return randomize_page(mm->brk, 0x02000000);
9d62dcdf
AW
735}
736
7ba78053
TG
737/*
738 * Called from fs/proc with a reference on @p to find the function
739 * which called into schedule(). This needs to be done carefully
740 * because the task might wake up and we might look at a stack
741 * changing under us.
742 */
743unsigned long get_wchan(struct task_struct *p)
744{
74327a3e 745 unsigned long start, bottom, top, sp, fp, ip, ret = 0;
7ba78053
TG
746 int count = 0;
747
748 if (!p || p == current || p->state == TASK_RUNNING)
749 return 0;
750
74327a3e
AL
751 if (!try_get_task_stack(p))
752 return 0;
753
7ba78053
TG
754 start = (unsigned long)task_stack_page(p);
755 if (!start)
74327a3e 756 goto out;
7ba78053
TG
757
758 /*
759 * Layout of the stack page:
760 *
761 * ----------- topmax = start + THREAD_SIZE - sizeof(unsigned long)
762 * PADDING
763 * ----------- top = topmax - TOP_OF_KERNEL_STACK_PADDING
764 * stack
15f4eae7 765 * ----------- bottom = start
7ba78053
TG
766 *
767 * The tasks stack pointer points at the location where the
768 * framepointer is stored. The data on the stack is:
769 * ... IP FP ... IP FP
770 *
771 * We need to read FP and IP, so we need to adjust the upper
772 * bound by another unsigned long.
773 */
774 top = start + THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING;
775 top -= 2 * sizeof(unsigned long);
15f4eae7 776 bottom = start;
7ba78053
TG
777
778 sp = READ_ONCE(p->thread.sp);
779 if (sp < bottom || sp > top)
74327a3e 780 goto out;
7ba78053 781
7b32aead 782 fp = READ_ONCE_NOCHECK(((struct inactive_task_frame *)sp)->bp);
7ba78053
TG
783 do {
784 if (fp < bottom || fp > top)
74327a3e 785 goto out;
f7d27c35 786 ip = READ_ONCE_NOCHECK(*(unsigned long *)(fp + sizeof(unsigned long)));
74327a3e
AL
787 if (!in_sched_functions(ip)) {
788 ret = ip;
789 goto out;
790 }
f7d27c35 791 fp = READ_ONCE_NOCHECK(*(unsigned long *)fp);
7ba78053 792 } while (count++ < 16 && p->state != TASK_RUNNING);
74327a3e
AL
793
794out:
795 put_task_stack(p);
796 return ret;
7ba78053 797}
b0b9b014
KH
798
799long do_arch_prctl_common(struct task_struct *task, int option,
800 unsigned long cpuid_enabled)
801{
e9ea1e7f
KH
802 switch (option) {
803 case ARCH_GET_CPUID:
804 return get_cpuid_mode();
805 case ARCH_SET_CPUID:
806 return set_cpuid_mode(task, cpuid_enabled);
807 }
808
b0b9b014
KH
809 return -EINVAL;
810}