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