x86/cpu: Expose arch_cpu_idle_dead()'s prototype definition
[linux-2.6-block.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>
b4c108d7 8#include <linux/cpu.h>
389d1fb1 9#include <linux/prctl.h>
61c4628b
SS
10#include <linux/slab.h>
11#include <linux/sched.h>
4c822698 12#include <linux/sched/idle.h>
b17b0153 13#include <linux/sched/debug.h>
29930025 14#include <linux/sched/task.h>
68db0cf1 15#include <linux/sched/task_stack.h>
186f4360
PG
16#include <linux/init.h>
17#include <linux/export.h>
7f424a8b 18#include <linux/pm.h>
162a688e 19#include <linux/tick.h>
9d62dcdf 20#include <linux/random.h>
7c68af6e 21#include <linux/user-return-notifier.h>
814e2c84
AI
22#include <linux/dmi.h>
23#include <linux/utsname.h>
90e24014 24#include <linux/stackprotector.h>
90e24014 25#include <linux/cpuidle.h>
89f579ce
YW
26#include <linux/acpi.h>
27#include <linux/elf-randomize.h>
aaa3896b 28#include <linux/static_call.h>
61613521 29#include <trace/events/power.h>
24f1e32c 30#include <linux/hw_breakpoint.h>
93789b32 31#include <asm/cpu.h>
d3ec5cae 32#include <asm/apic.h>
7c0f6ba6 33#include <linux/uaccess.h>
b253149b 34#include <asm/mwait.h>
db8268df 35#include <asm/fpu/api.h>
63e81807 36#include <asm/fpu/sched.h>
500afbf6 37#include <asm/fpu/xstate.h>
66cb5917 38#include <asm/debugreg.h>
90e24014 39#include <asm/nmi.h>
375074cc 40#include <asm/tlbflush.h>
8838eb6c 41#include <asm/mce.h>
9fda6a06 42#include <asm/vm86.h>
7b32aead 43#include <asm/switch_to.h>
b7ffc44d 44#include <asm/desc.h>
e9ea1e7f 45#include <asm/prctl.h>
885f82bf 46#include <asm/spec-ctrl.h>
577d5cd7 47#include <asm/io_bitmap.h>
89f579ce 48#include <asm/proto.h>
6f9885a3 49#include <asm/frame.h>
5d1ceb39 50#include <asm/unwind.h>
bfe6ed0c 51#include <asm/tdx.h>
90e24014 52
ff16701a
TG
53#include "process.h"
54
45046892
TG
55/*
56 * per-CPU TSS segments. Threads are completely 'soft' on Linux,
57 * no more per-task TSS's. The TSS size is kept cacheline-aligned
58 * so they are allowed to end up in the .data..cacheline_aligned
59 * section. Since TSS's are completely CPU-local, we want them
60 * on exact cacheline boundaries, to eliminate cacheline ping-pong.
61 */
2fd9c41a 62__visible DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw) = {
d0a0de21 63 .x86_tss = {
20bb8344
AL
64 /*
65 * .sp0 is only used when entering ring 0 from a lower
66 * privilege level. Since the init task never runs anything
67 * but ring 0 code, there is no need for a valid value here.
68 * Poison it.
69 */
70 .sp0 = (1UL << (BITS_PER_LONG-1)) + 1,
9aaefe7b 71
1591584e 72#ifdef CONFIG_X86_32
9aaefe7b 73 .sp1 = TOP_OF_INIT_STACK,
9aaefe7b 74
d0a0de21
AL
75 .ss0 = __KERNEL_DS,
76 .ss1 = __KERNEL_CS,
d0a0de21 77#endif
ecc7e37d 78 .io_bitmap_base = IO_BITMAP_OFFSET_INVALID,
d0a0de21 79 },
d0a0de21 80};
c482feef 81EXPORT_PER_CPU_SYMBOL(cpu_tss_rw);
45046892 82
b7ceaec1
AL
83DEFINE_PER_CPU(bool, __tss_limit_invalid);
84EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
b7ffc44d 85
55ccf3fe
SS
86/*
87 * this gets called so that we can store lazy state into memory and copy the
88 * current task into the new thread.
89 */
61c4628b
SS
90int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
91{
5aaeb5c0 92 memcpy(dst, src, arch_task_struct_size);
2459ee86
AL
93#ifdef CONFIG_VM86
94 dst->thread.vm86 = NULL;
95#endif
f0cbc8b3
TG
96 /* Drop the copied pointer to current's fpstate */
97 dst->thread.fpu.fpstate = NULL;
500afbf6 98
2d16a187 99 return 0;
61c4628b 100}
7f424a8b 101
500afbf6
CB
102#ifdef CONFIG_X86_64
103void arch_release_task_struct(struct task_struct *tsk)
104{
105 if (fpu_state_size_dynamic())
106 fpstate_free(&tsk->thread.fpu);
61c4628b 107}
500afbf6 108#endif
7f424a8b 109
389d1fb1 110/*
4bfe6cce 111 * Free thread data structures etc..
389d1fb1 112 */
e6464694 113void exit_thread(struct task_struct *tsk)
389d1fb1 114{
e6464694 115 struct thread_struct *t = &tsk->thread;
ca6787ba 116 struct fpu *fpu = &t->fpu;
ea5f1cd7
TG
117
118 if (test_thread_flag(TIF_IO_BITMAP))
4bfe6cce 119 io_bitmap_exit(tsk);
1dcc8d7b 120
9fda6a06
BG
121 free_vm86(t);
122
50338615 123 fpu__drop(fpu);
389d1fb1
JF
124}
125
2fff071d
TG
126static int set_new_tls(struct task_struct *p, unsigned long tls)
127{
128 struct user_desc __user *utls = (struct user_desc __user *)tls;
129
130 if (in_ia32_syscall())
131 return do_set_thread_area(p, -1, utls, 0);
132 else
133 return do_set_thread_area_64(p, ARCH_SET_FS, tls);
134}
135
c5febea0 136int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
2fff071d 137{
c5febea0
EB
138 unsigned long clone_flags = args->flags;
139 unsigned long sp = args->stack;
c5febea0 140 unsigned long tls = args->tls;
2fff071d
TG
141 struct inactive_task_frame *frame;
142 struct fork_frame *fork_frame;
143 struct pt_regs *childregs;
4804e382 144 int ret = 0;
2fff071d
TG
145
146 childregs = task_pt_regs(p);
147 fork_frame = container_of(childregs, struct fork_frame, regs);
148 frame = &fork_frame->frame;
149
6f9885a3 150 frame->bp = encode_frame_pointer(childregs);
2fff071d
TG
151 frame->ret_addr = (unsigned long) ret_from_fork;
152 p->thread.sp = (unsigned long) fork_frame;
577d5cd7 153 p->thread.io_bitmap = NULL;
b968e84b 154 p->thread.iopl_warn = 0;
2fff071d
TG
155 memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
156
157#ifdef CONFIG_X86_64
005f141e
CB
158 current_save_fsgs();
159 p->thread.fsindex = current->thread.fsindex;
160 p->thread.fsbase = current->thread.fsbase;
161 p->thread.gsindex = current->thread.gsindex;
162 p->thread.gsbase = current->thread.gsbase;
163
2fff071d
TG
164 savesegment(es, p->thread.es);
165 savesegment(ds, p->thread.ds);
166#else
167 p->thread.sp0 = (unsigned long) (childregs + 1);
3a24a608 168 savesegment(gs, p->thread.gs);
2fff071d
TG
169 /*
170 * Clear all status flags including IF and set fixed bit. 64bit
171 * does not have this initialization as the frame does not contain
172 * flags. The flags consistency (especially vs. AC) is there
173 * ensured via objtool, which lacks 32bit support.
174 */
175 frame->flags = X86_EFLAGS_FIXED;
176#endif
177
5bd2e97c 178 fpu_clone(p, clone_flags, args->fn);
2d16a187 179
2fff071d 180 /* Kernel thread ? */
50b7b6f2 181 if (unlikely(p->flags & PF_KTHREAD)) {
9782a712 182 p->thread.pkru = pkru_get_init_value();
2fff071d 183 memset(childregs, 0, sizeof(struct pt_regs));
5bd2e97c 184 kthread_frame_init(frame, args->fn, args->fn_arg);
2fff071d
TG
185 return 0;
186 }
187
9782a712
DH
188 /*
189 * Clone current's PKRU value from hardware. tsk->thread.pkru
190 * is only valid when scheduled out.
191 */
192 p->thread.pkru = read_pkru();
193
2fff071d
TG
194 frame->bx = 0;
195 *childregs = *current_pt_regs();
196 childregs->ax = 0;
197 if (sp)
198 childregs->sp = sp;
199
5bd2e97c 200 if (unlikely(args->fn)) {
50b7b6f2 201 /*
5bd2e97c
EB
202 * A user space thread, but it doesn't return to
203 * ret_after_fork().
50b7b6f2
SM
204 *
205 * In order to indicate that to tools like gdb,
206 * we reset the stack and instruction pointers.
207 *
208 * It does the same kernel frame setup to return to a kernel
209 * function that a kernel thread does.
210 */
211 childregs->sp = 0;
212 childregs->ip = 0;
5bd2e97c 213 kthread_frame_init(frame, args->fn, args->fn_arg);
50b7b6f2
SM
214 return 0;
215 }
216
2fff071d 217 /* Set a new TLS for the child thread? */
4804e382 218 if (clone_flags & CLONE_SETTLS)
2fff071d 219 ret = set_new_tls(p, tls);
4804e382
TG
220
221 if (!ret && unlikely(test_tsk_thread_flag(current, TIF_IO_BITMAP)))
222 io_bitmap_share(p);
223
2fff071d
TG
224 return ret;
225}
226
33344368
AL
227static void pkru_flush_thread(void)
228{
229 /*
230 * If PKRU is enabled the default PKRU value has to be loaded into
231 * the hardware right here (similar to context switch).
232 */
233 pkru_write_default();
234}
235
389d1fb1
JF
236void flush_thread(void)
237{
238 struct task_struct *tsk = current;
239
24f1e32c 240 flush_ptrace_hw_breakpoint(tsk);
389d1fb1 241 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
110d7f75 242
e7ecad17 243 fpu_flush_thread();
33344368 244 pkru_flush_thread();
389d1fb1
JF
245}
246
389d1fb1
JF
247void disable_TSC(void)
248{
249 preempt_disable();
250 if (!test_and_set_thread_flag(TIF_NOTSC))
251 /*
252 * Must flip the CPU state synchronously with
253 * TIF_NOTSC in the current running context.
254 */
5a920155 255 cr4_set_bits(X86_CR4_TSD);
389d1fb1
JF
256 preempt_enable();
257}
258
389d1fb1
JF
259static void enable_TSC(void)
260{
261 preempt_disable();
262 if (test_and_clear_thread_flag(TIF_NOTSC))
263 /*
264 * Must flip the CPU state synchronously with
265 * TIF_NOTSC in the current running context.
266 */
5a920155 267 cr4_clear_bits(X86_CR4_TSD);
389d1fb1
JF
268 preempt_enable();
269}
270
271int get_tsc_mode(unsigned long adr)
272{
273 unsigned int val;
274
275 if (test_thread_flag(TIF_NOTSC))
276 val = PR_TSC_SIGSEGV;
277 else
278 val = PR_TSC_ENABLE;
279
280 return put_user(val, (unsigned int __user *)adr);
281}
282
283int set_tsc_mode(unsigned int val)
284{
285 if (val == PR_TSC_SIGSEGV)
286 disable_TSC();
287 else if (val == PR_TSC_ENABLE)
288 enable_TSC();
289 else
290 return -EINVAL;
291
292 return 0;
293}
294
e9ea1e7f
KH
295DEFINE_PER_CPU(u64, msr_misc_features_shadow);
296
297static void set_cpuid_faulting(bool on)
298{
299 u64 msrval;
300
301 msrval = this_cpu_read(msr_misc_features_shadow);
302 msrval &= ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
303 msrval |= (on << MSR_MISC_FEATURES_ENABLES_CPUID_FAULT_BIT);
304 this_cpu_write(msr_misc_features_shadow, msrval);
305 wrmsrl(MSR_MISC_FEATURES_ENABLES, msrval);
306}
307
308static void disable_cpuid(void)
309{
310 preempt_disable();
311 if (!test_and_set_thread_flag(TIF_NOCPUID)) {
312 /*
313 * Must flip the CPU state synchronously with
314 * TIF_NOCPUID in the current running context.
315 */
316 set_cpuid_faulting(true);
317 }
318 preempt_enable();
319}
320
321static void enable_cpuid(void)
322{
323 preempt_disable();
324 if (test_and_clear_thread_flag(TIF_NOCPUID)) {
325 /*
326 * Must flip the CPU state synchronously with
327 * TIF_NOCPUID in the current running context.
328 */
329 set_cpuid_faulting(false);
330 }
331 preempt_enable();
332}
333
334static int get_cpuid_mode(void)
335{
336 return !test_thread_flag(TIF_NOCPUID);
337}
338
f5c0b4f3 339static int set_cpuid_mode(unsigned long cpuid_enabled)
e9ea1e7f 340{
67e87d43 341 if (!boot_cpu_has(X86_FEATURE_CPUID_FAULT))
e9ea1e7f
KH
342 return -ENODEV;
343
344 if (cpuid_enabled)
345 enable_cpuid();
346 else
347 disable_cpuid();
348
349 return 0;
350}
351
352/*
353 * Called immediately after a successful exec.
354 */
355void arch_setup_new_exec(void)
356{
357 /* If cpuid was previously disabled for this task, re-enable it. */
358 if (test_thread_flag(TIF_NOCPUID))
359 enable_cpuid();
71368af9
WL
360
361 /*
362 * Don't inherit TIF_SSBD across exec boundary when
363 * PR_SPEC_DISABLE_NOEXEC is used.
364 */
365 if (test_thread_flag(TIF_SSBD) &&
366 task_spec_ssb_noexec(current)) {
367 clear_thread_flag(TIF_SSBD);
368 task_clear_spec_ssb_disable(current);
369 task_clear_spec_ssb_noexec(current);
dca99fb6 370 speculation_ctrl_update(read_thread_flags());
71368af9 371 }
e9ea1e7f
KH
372}
373
111e7b15 374#ifdef CONFIG_X86_IOPL_IOPERM
22fe5b04
TG
375static inline void switch_to_bitmap(unsigned long tifp)
376{
377 /*
378 * Invalidate I/O bitmap if the previous task used it. This prevents
379 * any possible leakage of an active I/O bitmap.
380 *
381 * If the next task has an I/O bitmap it will handle it on exit to
382 * user mode.
383 */
384 if (tifp & _TIF_IO_BITMAP)
cadfad87 385 tss_invalidate_io_bitmap();
22fe5b04
TG
386}
387
388static void tss_copy_io_bitmap(struct tss_struct *tss, struct io_bitmap *iobm)
060aa16f
TG
389{
390 /*
391 * Copy at least the byte range of the incoming tasks bitmap which
392 * covers the permitted I/O ports.
393 *
394 * If the previous task which used an I/O bitmap had more bits
395 * permitted, then the copy needs to cover those as well so they
396 * get turned off.
397 */
398 memcpy(tss->io_bitmap.bitmap, iobm->bitmap,
399 max(tss->io_bitmap.prev_max, iobm->max));
400
401 /*
402 * Store the new max and the sequence number of this bitmap
403 * and a pointer to the bitmap itself.
404 */
405 tss->io_bitmap.prev_max = iobm->max;
406 tss->io_bitmap.prev_sequence = iobm->sequence;
407}
408
22fe5b04 409/**
dbb5ab6d 410 * native_tss_update_io_bitmap - Update I/O bitmap before exiting to user mode
22fe5b04 411 */
99bcd4a6 412void native_tss_update_io_bitmap(void)
af8b3cd3 413{
ff16701a 414 struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
7b0b8cfd 415 struct thread_struct *t = &current->thread;
c8137ace 416 u16 *base = &tss->x86_tss.io_bitmap_base;
ff16701a 417
7b0b8cfd 418 if (!test_thread_flag(TIF_IO_BITMAP)) {
cadfad87 419 native_tss_invalidate_io_bitmap();
7b0b8cfd
BP
420 return;
421 }
422
423 if (IS_ENABLED(CONFIG_X86_IOPL_IOPERM) && t->iopl_emul == 3) {
424 *base = IO_BITMAP_OFFSET_VALID_ALL;
425 } else {
426 struct io_bitmap *iobm = t->io_bitmap;
427
af8b3cd3 428 /*
7b0b8cfd
BP
429 * Only copy bitmap data when the sequence number differs. The
430 * update time is accounted to the incoming task.
af8b3cd3 431 */
7b0b8cfd
BP
432 if (tss->io_bitmap.prev_sequence != iobm->sequence)
433 tss_copy_io_bitmap(tss, iobm);
434
435 /* Enable the bitmap */
436 *base = IO_BITMAP_OFFSET_VALID_MAP;
af8b3cd3 437 }
7b0b8cfd
BP
438
439 /*
440 * Make sure that the TSS limit is covering the IO bitmap. It might have
441 * been cut down by a VMEXIT to 0x67 which would cause a subsequent I/O
442 * access from user space to trigger a #GP because tbe bitmap is outside
443 * the TSS limit.
444 */
445 refresh_tss_limit();
af8b3cd3 446}
111e7b15
TG
447#else /* CONFIG_X86_IOPL_IOPERM */
448static inline void switch_to_bitmap(unsigned long tifp) { }
449#endif
af8b3cd3 450
1f50ddb4
TG
451#ifdef CONFIG_SMP
452
453struct ssb_state {
454 struct ssb_state *shared_state;
455 raw_spinlock_t lock;
456 unsigned int disable_state;
457 unsigned long local_state;
458};
459
460#define LSTATE_SSB 0
461
462static DEFINE_PER_CPU(struct ssb_state, ssb_state);
463
464void speculative_store_bypass_ht_init(void)
885f82bf 465{
1f50ddb4
TG
466 struct ssb_state *st = this_cpu_ptr(&ssb_state);
467 unsigned int this_cpu = smp_processor_id();
468 unsigned int cpu;
469
470 st->local_state = 0;
471
472 /*
473 * Shared state setup happens once on the first bringup
474 * of the CPU. It's not destroyed on CPU hotunplug.
475 */
476 if (st->shared_state)
477 return;
478
479 raw_spin_lock_init(&st->lock);
480
481 /*
482 * Go over HT siblings and check whether one of them has set up the
483 * shared state pointer already.
484 */
485 for_each_cpu(cpu, topology_sibling_cpumask(this_cpu)) {
486 if (cpu == this_cpu)
487 continue;
488
489 if (!per_cpu(ssb_state, cpu).shared_state)
490 continue;
491
492 /* Link it to the state of the sibling: */
493 st->shared_state = per_cpu(ssb_state, cpu).shared_state;
494 return;
495 }
496
497 /*
498 * First HT sibling to come up on the core. Link shared state of
499 * the first HT sibling to itself. The siblings on the same core
500 * which come up later will see the shared state pointer and link
d9f6e12f 501 * themselves to the state of this CPU.
1f50ddb4
TG
502 */
503 st->shared_state = st;
504}
885f82bf 505
1f50ddb4
TG
506/*
507 * Logic is: First HT sibling enables SSBD for both siblings in the core
508 * and last sibling to disable it, disables it for the whole core. This how
509 * MSR_SPEC_CTRL works in "hardware":
510 *
511 * CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL
512 */
513static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
514{
515 struct ssb_state *st = this_cpu_ptr(&ssb_state);
516 u64 msr = x86_amd_ls_cfg_base;
517
518 if (!static_cpu_has(X86_FEATURE_ZEN)) {
519 msr |= ssbd_tif_to_amd_ls_cfg(tifn);
885f82bf 520 wrmsrl(MSR_AMD64_LS_CFG, msr);
1f50ddb4
TG
521 return;
522 }
523
524 if (tifn & _TIF_SSBD) {
525 /*
526 * Since this can race with prctl(), block reentry on the
527 * same CPU.
528 */
529 if (__test_and_set_bit(LSTATE_SSB, &st->local_state))
530 return;
531
532 msr |= x86_amd_ls_cfg_ssbd_mask;
533
534 raw_spin_lock(&st->shared_state->lock);
535 /* First sibling enables SSBD: */
536 if (!st->shared_state->disable_state)
537 wrmsrl(MSR_AMD64_LS_CFG, msr);
538 st->shared_state->disable_state++;
539 raw_spin_unlock(&st->shared_state->lock);
885f82bf 540 } else {
1f50ddb4
TG
541 if (!__test_and_clear_bit(LSTATE_SSB, &st->local_state))
542 return;
543
544 raw_spin_lock(&st->shared_state->lock);
545 st->shared_state->disable_state--;
546 if (!st->shared_state->disable_state)
547 wrmsrl(MSR_AMD64_LS_CFG, msr);
548 raw_spin_unlock(&st->shared_state->lock);
885f82bf
TG
549 }
550}
1f50ddb4
TG
551#else
552static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
553{
554 u64 msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn);
555
556 wrmsrl(MSR_AMD64_LS_CFG, msr);
557}
558#endif
559
11fb0683
TL
560static __always_inline void amd_set_ssb_virt_state(unsigned long tifn)
561{
562 /*
563 * SSBD has the same definition in SPEC_CTRL and VIRT_SPEC_CTRL,
564 * so ssbd_tif_to_spec_ctrl() just works.
565 */
566 wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, ssbd_tif_to_spec_ctrl(tifn));
567}
568
01daf568
TC
569/*
570 * Update the MSRs managing speculation control, during context switch.
571 *
572 * tifp: Previous task's thread flags
573 * tifn: Next task's thread flags
574 */
575static __always_inline void __speculation_ctrl_update(unsigned long tifp,
576 unsigned long tifn)
1f50ddb4 577{
5bfbe3ad 578 unsigned long tif_diff = tifp ^ tifn;
01daf568
TC
579 u64 msr = x86_spec_ctrl_base;
580 bool updmsr = false;
581
2f5fb193
TG
582 lockdep_assert_irqs_disabled();
583
dbbe2ad0
AS
584 /* Handle change of TIF_SSBD depending on the mitigation method. */
585 if (static_cpu_has(X86_FEATURE_VIRT_SSBD)) {
586 if (tif_diff & _TIF_SSBD)
01daf568 587 amd_set_ssb_virt_state(tifn);
dbbe2ad0
AS
588 } else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
589 if (tif_diff & _TIF_SSBD)
01daf568 590 amd_set_core_ssb_state(tifn);
dbbe2ad0
AS
591 } else if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
592 static_cpu_has(X86_FEATURE_AMD_SSBD)) {
593 updmsr |= !!(tif_diff & _TIF_SSBD);
594 msr |= ssbd_tif_to_spec_ctrl(tifn);
01daf568 595 }
1f50ddb4 596
dbbe2ad0 597 /* Only evaluate TIF_SPEC_IB if conditional STIBP is enabled. */
5bfbe3ad
TC
598 if (IS_ENABLED(CONFIG_SMP) &&
599 static_branch_unlikely(&switch_to_cond_stibp)) {
600 updmsr |= !!(tif_diff & _TIF_SPEC_IB);
601 msr |= stibp_tif_to_spec_ctrl(tifn);
602 }
603
01daf568 604 if (updmsr)
66065157 605 update_spec_ctrl_cond(msr);
1f50ddb4
TG
606}
607
6d991ba5 608static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)
1f50ddb4 609{
6d991ba5
TG
610 if (test_and_clear_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE)) {
611 if (task_spec_ssb_disable(tsk))
612 set_tsk_thread_flag(tsk, TIF_SSBD);
613 else
614 clear_tsk_thread_flag(tsk, TIF_SSBD);
9137bb27
TG
615
616 if (task_spec_ib_disable(tsk))
617 set_tsk_thread_flag(tsk, TIF_SPEC_IB);
618 else
619 clear_tsk_thread_flag(tsk, TIF_SPEC_IB);
6d991ba5
TG
620 }
621 /* Return the updated threadinfo flags*/
dca99fb6 622 return read_task_thread_flags(tsk);
1f50ddb4 623}
885f82bf 624
26c4d75b 625void speculation_ctrl_update(unsigned long tif)
885f82bf 626{
2f5fb193
TG
627 unsigned long flags;
628
01daf568 629 /* Forced update. Make sure all relevant TIF flags are different */
2f5fb193 630 local_irq_save(flags);
01daf568 631 __speculation_ctrl_update(~tif, tif);
2f5fb193 632 local_irq_restore(flags);
885f82bf
TG
633}
634
6d991ba5
TG
635/* Called from seccomp/prctl update */
636void speculation_ctrl_update_current(void)
637{
638 preempt_disable();
639 speculation_ctrl_update(speculation_ctrl_update_tif(current));
640 preempt_enable();
641}
642
d8f0b353
TG
643static inline void cr4_toggle_bits_irqsoff(unsigned long mask)
644{
645 unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
646
647 newval = cr4 ^ mask;
648 if (newval != cr4) {
649 this_cpu_write(cpu_tlbstate.cr4, newval);
650 __write_cr4(newval);
651 }
652}
653
ff16701a 654void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p)
389d1fb1 655{
af8b3cd3 656 unsigned long tifp, tifn;
389d1fb1 657
dca99fb6
MR
658 tifn = read_task_thread_flags(next_p);
659 tifp = read_task_thread_flags(prev_p);
22fe5b04
TG
660
661 switch_to_bitmap(tifp);
af8b3cd3
KH
662
663 propagate_user_return_notify(prev_p, next_p);
664
b9894a2f
KH
665 if ((tifp & _TIF_BLOCKSTEP || tifn & _TIF_BLOCKSTEP) &&
666 arch_has_block_step()) {
667 unsigned long debugctl, msk;
ea8e61b7 668
b9894a2f 669 rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
ea8e61b7 670 debugctl &= ~DEBUGCTLMSR_BTF;
b9894a2f
KH
671 msk = tifn & _TIF_BLOCKSTEP;
672 debugctl |= (msk >> TIF_BLOCKSTEP) << DEBUGCTLMSR_BTF_SHIFT;
673 wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
ea8e61b7 674 }
389d1fb1 675
5a920155 676 if ((tifp ^ tifn) & _TIF_NOTSC)
9d0b6232 677 cr4_toggle_bits_irqsoff(X86_CR4_TSD);
e9ea1e7f
KH
678
679 if ((tifp ^ tifn) & _TIF_NOCPUID)
680 set_cpuid_faulting(!!(tifn & _TIF_NOCPUID));
885f82bf 681
6d991ba5
TG
682 if (likely(!((tifp | tifn) & _TIF_SPEC_FORCE_UPDATE))) {
683 __speculation_ctrl_update(tifp, tifn);
684 } else {
685 speculation_ctrl_update_tif(prev_p);
686 tifn = speculation_ctrl_update_tif(next_p);
687
688 /* Enforce MSR update to ensure consistent state */
689 __speculation_ctrl_update(~tifn, tifn);
690 }
389d1fb1
JF
691}
692
00dba564
TG
693/*
694 * Idle related variables and functions
695 */
d1896049 696unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
00dba564
TG
697EXPORT_SYMBOL(boot_option_idle_override);
698
aaa3896b
PZ
699/*
700 * We use this if we don't have any better idle routine..
701 */
702void __cpuidle default_idle(void)
703{
704 raw_safe_halt();
89b30987 705 raw_local_irq_disable();
aaa3896b
PZ
706}
707#if defined(CONFIG_APM_MODULE) || defined(CONFIG_HALTPOLL_CPUIDLE_MODULE)
708EXPORT_SYMBOL(default_idle);
709#endif
710
711DEFINE_STATIC_CALL_NULL(x86_idle, default_idle);
712
713static bool x86_idle_set(void)
714{
715 return !!static_call_query(x86_idle);
716}
00dba564 717
90e24014 718#ifndef CONFIG_SMP
eab89405 719static inline void __noreturn play_dead(void)
90e24014
RW
720{
721 BUG();
722}
723#endif
724
7d1a9417
TG
725void arch_cpu_idle_enter(void)
726{
6a369583 727 tsc_verify_tsc_adjust(false);
7d1a9417 728 local_touch_nmi();
7d1a9417 729}
90e24014 730
071c44e4 731void __noreturn arch_cpu_idle_dead(void)
7d1a9417
TG
732{
733 play_dead();
734}
90e24014 735
7d1a9417
TG
736/*
737 * Called from the generic idle code.
738 */
aaa3896b 739void __cpuidle arch_cpu_idle(void)
00dba564 740{
aaa3896b 741 static_call(x86_idle)();
00dba564 742}
716ff71a 743EXPORT_SYMBOL_GPL(arch_cpu_idle);
00dba564 744
6a377ddc
LB
745#ifdef CONFIG_XEN
746bool xen_set_default_idle(void)
e5fd47bf 747{
aaa3896b 748 bool ret = x86_idle_set();
e5fd47bf 749
aaa3896b 750 static_call_update(x86_idle, default_idle);
e5fd47bf
KRW
751
752 return ret;
753}
6a377ddc 754#endif
bba4ed01 755
f9cdf7ca 756void __noreturn stop_this_cpu(void *dummy)
d3ec5cae
IV
757{
758 local_irq_disable();
759 /*
760 * Remove this CPU:
761 */
4f062896 762 set_cpu_online(smp_processor_id(), false);
d3ec5cae 763 disable_local_APIC();
8838eb6c 764 mcheck_cpu_clear(this_cpu_ptr(&cpu_info));
d3ec5cae 765
f23d74f6
TL
766 /*
767 * Use wbinvd on processors that support SME. This provides support
768 * for performing a successful kexec when going from SME inactive
769 * to SME active (or vice-versa). The cache must be cleared so that
770 * if there are entries with the same physical address, both with and
771 * without the encryption bit, they don't race each other when flushed
772 * and potentially end up with the wrong entry being committed to
773 * memory.
08f253ec
ML
774 *
775 * Test the CPUID bit directly because the machine might've cleared
776 * X86_FEATURE_SME due to cmdline options.
f23d74f6 777 */
08f253ec 778 if (cpuid_eax(0x8000001f) & BIT(0))
f23d74f6 779 native_wbinvd();
bba4ed01
TL
780 for (;;) {
781 /*
f23d74f6
TL
782 * Use native_halt() so that memory contents don't change
783 * (stack usage and variables) after possibly issuing the
784 * native_wbinvd() above.
bba4ed01 785 */
f23d74f6 786 native_halt();
bba4ed01 787 }
7f424a8b
PZ
788}
789
aa276e1c 790/*
07c94a38
BP
791 * AMD Erratum 400 aware idle routine. We handle it the same way as C3 power
792 * states (local apic timer and TSC stop).
58c644ba
PZ
793 *
794 * XXX this function is completely buggered vs RCU and tracing.
aa276e1c 795 */
02c68a02 796static void amd_e400_idle(void)
aa276e1c 797{
07c94a38
BP
798 /*
799 * We cannot use static_cpu_has_bug() here because X86_BUG_AMD_APIC_C1E
800 * gets set after static_cpu_has() places have been converted via
801 * alternatives.
802 */
803 if (!boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
804 default_idle();
805 return;
aa276e1c
TG
806 }
807
07c94a38 808 tick_broadcast_enter();
aa276e1c 809
07c94a38 810 default_idle();
0beefa20 811
07c94a38 812 tick_broadcast_exit();
aa276e1c
TG
813}
814
b253149b 815/*
aebef63c
WK
816 * Prefer MWAIT over HALT if MWAIT is supported, MWAIT_CPUID leaf
817 * exists and whenever MONITOR/MWAIT extensions are present there is at
818 * least one C1 substate.
b253149b 819 *
aebef63c
WK
820 * Do not prefer MWAIT if MONITOR instruction has a bug or idle=nomwait
821 * is passed to kernel commandline parameter.
b253149b
LB
822 */
823static int prefer_mwait_c1_over_halt(const struct cpuinfo_x86 *c)
824{
aebef63c
WK
825 u32 eax, ebx, ecx, edx;
826
8bcedb4c
WK
827 /* User has disallowed the use of MWAIT. Fallback to HALT */
828 if (boot_option_idle_override == IDLE_NOMWAIT)
b253149b
LB
829 return 0;
830
aebef63c
WK
831 /* MWAIT is not supported on this platform. Fallback to HALT */
832 if (!cpu_has(c, X86_FEATURE_MWAIT))
b253149b
LB
833 return 0;
834
aebef63c
WK
835 /* Monitor has a bug. Fallback to HALT */
836 if (boot_cpu_has_bug(X86_BUG_MONITOR))
b253149b
LB
837 return 0;
838
aebef63c
WK
839 cpuid(CPUID_MWAIT_LEAF, &eax, &ebx, &ecx, &edx);
840
841 /*
842 * If MWAIT extensions are not available, it is safe to use MWAIT
843 * with EAX=0, ECX=0.
844 */
845 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED))
846 return 1;
847
848 /*
849 * If MWAIT extensions are available, there should be at least one
850 * MWAIT C1 substate present.
851 */
852 return (edx & MWAIT_C1_SUBSTATE_MASK);
b253149b
LB
853}
854
855/*
0fb0328d
HR
856 * MONITOR/MWAIT with no hints, used for default C1 state. This invokes MWAIT
857 * with interrupts enabled and no flags, which is backwards compatible with the
858 * original MWAIT implementation.
b253149b 859 */
6727ad9e 860static __cpuidle void mwait_idle(void)
b253149b 861{
f8e617f4
MG
862 if (!current_set_polling_and_test()) {
863 if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) {
ca59809f 864 mb(); /* quirk */
b253149b 865 clflush((void *)&current_thread_info()->flags);
ca59809f 866 mb(); /* quirk */
f8e617f4 867 }
b253149b
LB
868
869 __monitor((void *)&current_thread_info()->flags, 0, 0);
89b30987 870 if (!need_resched()) {
b253149b 871 __sti_mwait(0, 0);
89b30987
PZ
872 raw_local_irq_disable();
873 }
f8e617f4
MG
874 }
875 __current_clr_polling();
b253149b
LB
876}
877
148f9bb8 878void select_idle_routine(const struct cpuinfo_x86 *c)
7f424a8b 879{
3e5095d1 880#ifdef CONFIG_SMP
7d1a9417 881 if (boot_option_idle_override == IDLE_POLL && smp_num_siblings > 1)
c767a54b 882 pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
7f424a8b 883#endif
aaa3896b 884 if (x86_idle_set() || boot_option_idle_override == IDLE_POLL)
6ddd2a27
TG
885 return;
886
3344ed30 887 if (boot_cpu_has_bug(X86_BUG_AMD_E400)) {
c767a54b 888 pr_info("using AMD E400 aware idle routine\n");
aaa3896b 889 static_call_update(x86_idle, amd_e400_idle);
b253149b
LB
890 } else if (prefer_mwait_c1_over_halt(c)) {
891 pr_info("using mwait in idle threads\n");
aaa3896b 892 static_call_update(x86_idle, mwait_idle);
bfe6ed0c
KS
893 } else if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
894 pr_info("using TDX aware idle routine\n");
aaa3896b 895 static_call_update(x86_idle, tdx_safe_halt);
6ddd2a27 896 } else
aaa3896b 897 static_call_update(x86_idle, default_idle);
7f424a8b
PZ
898}
899
07c94a38 900void amd_e400_c1e_apic_setup(void)
30e1e6d1 901{
07c94a38
BP
902 if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
903 pr_info("Switch to broadcast mode on CPU%d\n", smp_processor_id());
904 local_irq_disable();
905 tick_broadcast_force();
906 local_irq_enable();
907 }
30e1e6d1
RR
908}
909
e7ff3a47
TG
910void __init arch_post_acpi_subsys_init(void)
911{
912 u32 lo, hi;
913
914 if (!boot_cpu_has_bug(X86_BUG_AMD_E400))
915 return;
916
917 /*
918 * AMD E400 detection needs to happen after ACPI has been enabled. If
919 * the machine is affected K8_INTP_C1E_ACTIVE_MASK bits are set in
920 * MSR_K8_INT_PENDING_MSG.
921 */
922 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
923 if (!(lo & K8_INTP_C1E_ACTIVE_MASK))
924 return;
925
926 boot_cpu_set_bug(X86_BUG_AMD_APIC_C1E);
927
928 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
929 mark_tsc_unstable("TSC halt in AMD C1E");
930 pr_info("System has AMD C1E enabled\n");
931}
932
7f424a8b
PZ
933static int __init idle_setup(char *str)
934{
ab6bc3e3
CG
935 if (!str)
936 return -EINVAL;
937
7f424a8b 938 if (!strcmp(str, "poll")) {
c767a54b 939 pr_info("using polling idle threads\n");
d1896049 940 boot_option_idle_override = IDLE_POLL;
7d1a9417 941 cpu_idle_poll_ctrl(true);
d1896049 942 } else if (!strcmp(str, "halt")) {
c1e3b377
ZY
943 /*
944 * When the boot option of idle=halt is added, halt is
945 * forced to be used for CPU idle. In such case CPU C2/C3
946 * won't be used again.
947 * To continue to load the CPU idle driver, don't touch
948 * the boot_option_idle_override.
949 */
aaa3896b 950 static_call_update(x86_idle, default_idle);
d1896049 951 boot_option_idle_override = IDLE_HALT;
da5e09a1
ZY
952 } else if (!strcmp(str, "nomwait")) {
953 /*
954 * If the boot option of "idle=nomwait" is added,
8bcedb4c
WK
955 * it means that mwait will be disabled for CPU C1/C2/C3
956 * states.
da5e09a1 957 */
d1896049 958 boot_option_idle_override = IDLE_NOMWAIT;
c1e3b377 959 } else
7f424a8b
PZ
960 return -1;
961
7f424a8b
PZ
962 return 0;
963}
964early_param("idle", idle_setup);
965
9d62dcdf
AW
966unsigned long arch_align_stack(unsigned long sp)
967{
968 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
8032bf12 969 sp -= get_random_u32_below(8192);
9d62dcdf
AW
970 return sp & ~0xf;
971}
972
973unsigned long arch_randomize_brk(struct mm_struct *mm)
974{
9c6f0902 975 return randomize_page(mm->brk, 0x02000000);
9d62dcdf
AW
976}
977
7ba78053
TG
978/*
979 * Called from fs/proc with a reference on @p to find the function
980 * which called into schedule(). This needs to be done carefully
981 * because the task might wake up and we might look at a stack
982 * changing under us.
983 */
42a20f86 984unsigned long __get_wchan(struct task_struct *p)
7ba78053 985{
5d1ceb39
PZ
986 struct unwind_state state;
987 unsigned long addr = 0;
7ba78053 988
0dc636b3
PZ
989 if (!try_get_task_stack(p))
990 return 0;
991
5d1ceb39
PZ
992 for (unwind_start(&state, p, NULL, NULL); !unwind_done(&state);
993 unwind_next_frame(&state)) {
994 addr = unwind_get_return_address(&state);
995 if (!addr)
996 break;
997 if (in_sched_functions(addr))
998 continue;
999 break;
1000 }
1001
0dc636b3
PZ
1002 put_task_stack(p);
1003
5d1ceb39 1004 return addr;
7ba78053 1005}
b0b9b014 1006
f5c0b4f3 1007long do_arch_prctl_common(int option, unsigned long arg2)
b0b9b014 1008{
e9ea1e7f
KH
1009 switch (option) {
1010 case ARCH_GET_CPUID:
1011 return get_cpuid_mode();
1012 case ARCH_SET_CPUID:
f5c0b4f3 1013 return set_cpuid_mode(arg2);
db8268df
CB
1014 case ARCH_GET_XCOMP_SUPP:
1015 case ARCH_GET_XCOMP_PERM:
1016 case ARCH_REQ_XCOMP_PERM:
980fe2fd
TG
1017 case ARCH_GET_XCOMP_GUEST_PERM:
1018 case ARCH_REQ_XCOMP_GUEST_PERM:
f5c0b4f3 1019 return fpu_xstate_prctl(option, arg2);
e9ea1e7f
KH
1020 }
1021
b0b9b014
KH
1022 return -EINVAL;
1023}