xref: /linux-6.15/arch/x86/kernel/process.c (revision 9f98a4f4)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c767a54bSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3c767a54bSJoe Perches 
461c4628bSSuresh Siddha #include <linux/errno.h>
561c4628bSSuresh Siddha #include <linux/kernel.h>
661c4628bSSuresh Siddha #include <linux/mm.h>
761c4628bSSuresh Siddha #include <linux/smp.h>
8b4c108d7SPhilippe Mathieu-Daudé #include <linux/cpu.h>
9389d1fb1SJeremy Fitzhardinge #include <linux/prctl.h>
1061c4628bSSuresh Siddha #include <linux/slab.h>
1161c4628bSSuresh Siddha #include <linux/sched.h>
124c822698SIngo Molnar #include <linux/sched/idle.h>
13b17b0153SIngo Molnar #include <linux/sched/debug.h>
1429930025SIngo Molnar #include <linux/sched/task.h>
1568db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
16186f4360SPaul Gortmaker #include <linux/init.h>
17186f4360SPaul Gortmaker #include <linux/export.h>
187f424a8bSPeter Zijlstra #include <linux/pm.h>
19162a688eSThomas Gleixner #include <linux/tick.h>
209d62dcdfSAmerigo Wang #include <linux/random.h>
217c68af6eSAvi Kivity #include <linux/user-return-notifier.h>
22814e2c84SAndy Isaacson #include <linux/dmi.h>
23814e2c84SAndy Isaacson #include <linux/utsname.h>
2490e24014SRichard Weinberger #include <linux/stackprotector.h>
2590e24014SRichard Weinberger #include <linux/cpuidle.h>
2689f579ceSYi Wang #include <linux/acpi.h>
2789f579ceSYi Wang #include <linux/elf-randomize.h>
28aaa3896bSPeter Zijlstra #include <linux/static_call.h>
2961613521SArjan van de Ven #include <trace/events/power.h>
3024f1e32cSFrederic Weisbecker #include <linux/hw_breakpoint.h>
313aec4ecbSBrian Gerst #include <linux/entry-common.h>
3293789b32SBorislav Petkov #include <asm/cpu.h>
33497f7028SDave Hansen #include <asm/cpuid.h>
34d3ec5caeSIvan Vecera #include <asm/apic.h>
357c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
36b253149bSLen Brown #include <asm/mwait.h>
37db8268dfSChang S. Bae #include <asm/fpu/api.h>
3863e81807SThomas Gleixner #include <asm/fpu/sched.h>
39500afbf6SChang S. Bae #include <asm/fpu/xstate.h>
4066cb5917SK.Prasad #include <asm/debugreg.h>
4190e24014SRichard Weinberger #include <asm/nmi.h>
42375074ccSAndy Lutomirski #include <asm/tlbflush.h>
438838eb6cSAshok Raj #include <asm/mce.h>
449fda6a06SBrian Gerst #include <asm/vm86.h>
457b32aeadSBrian Gerst #include <asm/switch_to.h>
46b7ffc44dSAndy Lutomirski #include <asm/desc.h>
47e9ea1e7fSKyle Huey #include <asm/prctl.h>
48885f82bfSThomas Gleixner #include <asm/spec-ctrl.h>
49577d5cd7SThomas Gleixner #include <asm/io_bitmap.h>
5089f579ceSYi Wang #include <asm/proto.h>
516f9885a3SJosh Poimboeuf #include <asm/frame.h>
525d1ceb39SPeter Zijlstra #include <asm/unwind.h>
53bfe6ed0cSKirill A. Shutemov #include <asm/tdx.h>
5474c228d2SKirill A. Shutemov #include <asm/mmu_context.h>
55b2926a36SRick Edgecombe #include <asm/shstk.h>
5690e24014SRichard Weinberger 
57ff16701aSThomas Gleixner #include "process.h"
58ff16701aSThomas Gleixner 
5945046892SThomas Gleixner /*
6045046892SThomas Gleixner  * per-CPU TSS segments. Threads are completely 'soft' on Linux,
6145046892SThomas Gleixner  * no more per-task TSS's. The TSS size is kept cacheline-aligned
6245046892SThomas Gleixner  * so they are allowed to end up in the .data..cacheline_aligned
6345046892SThomas Gleixner  * section. Since TSS's are completely CPU-local, we want them
6445046892SThomas Gleixner  * on exact cacheline boundaries, to eliminate cacheline ping-pong.
6545046892SThomas Gleixner  */
662fd9c41aSNick Desaulniers __visible DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw) = {
67d0a0de21SAndy Lutomirski 	.x86_tss = {
6820bb8344SAndy Lutomirski 		/*
6920bb8344SAndy Lutomirski 		 * .sp0 is only used when entering ring 0 from a lower
7020bb8344SAndy Lutomirski 		 * privilege level.  Since the init task never runs anything
7120bb8344SAndy Lutomirski 		 * but ring 0 code, there is no need for a valid value here.
7220bb8344SAndy Lutomirski 		 * Poison it.
7320bb8344SAndy Lutomirski 		 */
7420bb8344SAndy Lutomirski 		.sp0 = (1UL << (BITS_PER_LONG-1)) + 1,
759aaefe7bSAndy Lutomirski 
761591584eSLai Jiangshan #ifdef CONFIG_X86_32
779aaefe7bSAndy Lutomirski 		.sp1 = TOP_OF_INIT_STACK,
789aaefe7bSAndy Lutomirski 
79d0a0de21SAndy Lutomirski 		.ss0 = __KERNEL_DS,
80d0a0de21SAndy Lutomirski 		.ss1 = __KERNEL_CS,
81d0a0de21SAndy Lutomirski #endif
82ecc7e37dSThomas Gleixner 		.io_bitmap_base	= IO_BITMAP_OFFSET_INVALID,
83d0a0de21SAndy Lutomirski 	 },
84d0a0de21SAndy Lutomirski };
85c482feefSAndy Lutomirski EXPORT_PER_CPU_SYMBOL(cpu_tss_rw);
8645046892SThomas Gleixner 
87b7ceaec1SAndy Lutomirski DEFINE_PER_CPU(bool, __tss_limit_invalid);
88b7ceaec1SAndy Lutomirski EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
89b7ffc44dSAndy Lutomirski 
9055ccf3feSSuresh Siddha /*
9155ccf3feSSuresh Siddha  * this gets called so that we can store lazy state into memory and copy the
9255ccf3feSSuresh Siddha  * current task into the new thread.
9355ccf3feSSuresh Siddha  */
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)9461c4628bSSuresh Siddha int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
9561c4628bSSuresh Siddha {
965d3b81d4SBenjamin Berg 	/* init_task is not dynamically sized (incomplete FPU state) */
975d3b81d4SBenjamin Berg 	if (unlikely(src == &init_task))
985d3b81d4SBenjamin Berg 		memcpy_and_pad(dst, arch_task_struct_size, src, sizeof(init_task), 0);
995d3b81d4SBenjamin Berg 	else
1005aaeb5c0SIngo Molnar 		memcpy(dst, src, arch_task_struct_size);
1015d3b81d4SBenjamin Berg 
1022459ee86SAndy Lutomirski #ifdef CONFIG_VM86
1032459ee86SAndy Lutomirski 	dst->thread.vm86 = NULL;
1042459ee86SAndy Lutomirski #endif
105f0cbc8b3SThomas Gleixner 	/* Drop the copied pointer to current's fpstate */
106f0cbc8b3SThomas Gleixner 	dst->thread.fpu.fpstate = NULL;
107500afbf6SChang S. Bae 
1082d16a187SThomas Gleixner 	return 0;
10961c4628bSSuresh Siddha }
1107f424a8bSPeter Zijlstra 
111500afbf6SChang S. Bae #ifdef CONFIG_X86_64
arch_release_task_struct(struct task_struct * tsk)112500afbf6SChang S. Bae void arch_release_task_struct(struct task_struct *tsk)
113500afbf6SChang S. Bae {
114500afbf6SChang S. Bae 	if (fpu_state_size_dynamic())
115500afbf6SChang S. Bae 		fpstate_free(&tsk->thread.fpu);
116500afbf6SChang S. Bae }
117500afbf6SChang S. Bae #endif
118500afbf6SChang S. Bae 
11900dba564SThomas Gleixner /*
1204bfe6cceSJay Lang  * Free thread data structures etc..
121389d1fb1SJeremy Fitzhardinge  */
exit_thread(struct task_struct * tsk)122e6464694SJiri Slaby void exit_thread(struct task_struct *tsk)
123389d1fb1SJeremy Fitzhardinge {
124e6464694SJiri Slaby 	struct thread_struct *t = &tsk->thread;
125ca6787baSIngo Molnar 	struct fpu *fpu = &t->fpu;
126389d1fb1SJeremy Fitzhardinge 
127ea5f1cd7SThomas Gleixner 	if (test_thread_flag(TIF_IO_BITMAP))
1284bfe6cceSJay Lang 		io_bitmap_exit(tsk);
1291dcc8d7bSSuresh Siddha 
1309fda6a06SBrian Gerst 	free_vm86(t);
1319fda6a06SBrian Gerst 
132b2926a36SRick Edgecombe 	shstk_free(tsk);
13350338615SIngo Molnar 	fpu__drop(fpu);
134389d1fb1SJeremy Fitzhardinge }
135389d1fb1SJeremy Fitzhardinge 
set_new_tls(struct task_struct * p,unsigned long tls)1362fff071dSThomas Gleixner static int set_new_tls(struct task_struct *p, unsigned long tls)
1372fff071dSThomas Gleixner {
1382fff071dSThomas Gleixner 	struct user_desc __user *utls = (struct user_desc __user *)tls;
1392fff071dSThomas Gleixner 
1402fff071dSThomas Gleixner 	if (in_ia32_syscall())
1412fff071dSThomas Gleixner 		return do_set_thread_area(p, -1, utls, 0);
1422fff071dSThomas Gleixner 	else
1432fff071dSThomas Gleixner 		return do_set_thread_area_64(p, ARCH_SET_FS, tls);
1442fff071dSThomas Gleixner }
1452fff071dSThomas Gleixner 
ret_from_fork(struct task_struct * prev,struct pt_regs * regs,int (* fn)(void *),void * fn_arg)1463aec4ecbSBrian Gerst __visible void ret_from_fork(struct task_struct *prev, struct pt_regs *regs,
1473aec4ecbSBrian Gerst 				     int (*fn)(void *), void *fn_arg)
1483aec4ecbSBrian Gerst {
1493aec4ecbSBrian Gerst 	schedule_tail(prev);
1503aec4ecbSBrian Gerst 
1513aec4ecbSBrian Gerst 	/* Is this a kernel thread? */
1523aec4ecbSBrian Gerst 	if (unlikely(fn)) {
1533aec4ecbSBrian Gerst 		fn(fn_arg);
1543aec4ecbSBrian Gerst 		/*
1553aec4ecbSBrian Gerst 		 * A kernel thread is allowed to return here after successfully
1563aec4ecbSBrian Gerst 		 * calling kernel_execve().  Exit to userspace to complete the
1573aec4ecbSBrian Gerst 		 * execve() syscall.
1583aec4ecbSBrian Gerst 		 */
1593aec4ecbSBrian Gerst 		regs->ax = 0;
1603aec4ecbSBrian Gerst 	}
1613aec4ecbSBrian Gerst 
1623aec4ecbSBrian Gerst 	syscall_exit_to_user_mode(regs);
1633aec4ecbSBrian Gerst }
1643aec4ecbSBrian Gerst 
copy_thread(struct task_struct * p,const struct kernel_clone_args * args)165c5febea0SEric W. Biederman int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
1662fff071dSThomas Gleixner {
167c5febea0SEric W. Biederman 	unsigned long clone_flags = args->flags;
168c5febea0SEric W. Biederman 	unsigned long sp = args->stack;
169c5febea0SEric W. Biederman 	unsigned long tls = args->tls;
1702fff071dSThomas Gleixner 	struct inactive_task_frame *frame;
1712fff071dSThomas Gleixner 	struct fork_frame *fork_frame;
1722fff071dSThomas Gleixner 	struct pt_regs *childregs;
173b2926a36SRick Edgecombe 	unsigned long new_ssp;
1744804e382SThomas Gleixner 	int ret = 0;
1752fff071dSThomas Gleixner 
1762fff071dSThomas Gleixner 	childregs = task_pt_regs(p);
1772fff071dSThomas Gleixner 	fork_frame = container_of(childregs, struct fork_frame, regs);
1782fff071dSThomas Gleixner 	frame = &fork_frame->frame;
1792fff071dSThomas Gleixner 
1806f9885a3SJosh Poimboeuf 	frame->bp = encode_frame_pointer(childregs);
1813aec4ecbSBrian Gerst 	frame->ret_addr = (unsigned long) ret_from_fork_asm;
1822fff071dSThomas Gleixner 	p->thread.sp = (unsigned long) fork_frame;
183577d5cd7SThomas Gleixner 	p->thread.io_bitmap = NULL;
184b968e84bSPeter Zijlstra 	p->thread.iopl_warn = 0;
1852fff071dSThomas Gleixner 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
1862fff071dSThomas Gleixner 
1872fff071dSThomas Gleixner #ifdef CONFIG_X86_64
188005f141eSChang S. Bae 	current_save_fsgs();
189005f141eSChang S. Bae 	p->thread.fsindex = current->thread.fsindex;
190005f141eSChang S. Bae 	p->thread.fsbase = current->thread.fsbase;
191005f141eSChang S. Bae 	p->thread.gsindex = current->thread.gsindex;
192005f141eSChang S. Bae 	p->thread.gsbase = current->thread.gsbase;
193005f141eSChang S. Bae 
1942fff071dSThomas Gleixner 	savesegment(es, p->thread.es);
1952fff071dSThomas Gleixner 	savesegment(ds, p->thread.ds);
1962f8794bdSKirill A. Shutemov 
1972f8794bdSKirill A. Shutemov 	if (p->mm && (clone_flags & (CLONE_VM | CLONE_VFORK)) == CLONE_VM)
1982f8794bdSKirill A. Shutemov 		set_bit(MM_CONTEXT_LOCK_LAM, &p->mm->context.flags);
1992fff071dSThomas Gleixner #else
2002fff071dSThomas Gleixner 	p->thread.sp0 = (unsigned long) (childregs + 1);
2013a24a608SBrian Gerst 	savesegment(gs, p->thread.gs);
2022fff071dSThomas Gleixner 	/*
2032fff071dSThomas Gleixner 	 * Clear all status flags including IF and set fixed bit. 64bit
2042fff071dSThomas Gleixner 	 * does not have this initialization as the frame does not contain
2052fff071dSThomas Gleixner 	 * flags. The flags consistency (especially vs. AC) is there
2062fff071dSThomas Gleixner 	 * ensured via objtool, which lacks 32bit support.
2072fff071dSThomas Gleixner 	 */
2082fff071dSThomas Gleixner 	frame->flags = X86_EFLAGS_FIXED;
2092fff071dSThomas Gleixner #endif
2102fff071dSThomas Gleixner 
211b2926a36SRick Edgecombe 	/*
212b2926a36SRick Edgecombe 	 * Allocate a new shadow stack for thread if needed. If shadow stack,
213b2926a36SRick Edgecombe 	 * is disabled, new_ssp will remain 0, and fpu_clone() will know not to
214b2926a36SRick Edgecombe 	 * update it.
215b2926a36SRick Edgecombe 	 */
216b2926a36SRick Edgecombe 	new_ssp = shstk_alloc_thread_stack(p, clone_flags, args->stack_size);
217b2926a36SRick Edgecombe 	if (IS_ERR_VALUE(new_ssp))
218b2926a36SRick Edgecombe 		return PTR_ERR((void *)new_ssp);
219b2926a36SRick Edgecombe 
220b2926a36SRick Edgecombe 	fpu_clone(p, clone_flags, args->fn, new_ssp);
2212d16a187SThomas Gleixner 
2222fff071dSThomas Gleixner 	/* Kernel thread ? */
22350b7b6f2SStefan Metzmacher 	if (unlikely(p->flags & PF_KTHREAD)) {
2249782a712SDave Hansen 		p->thread.pkru = pkru_get_init_value();
2252fff071dSThomas Gleixner 		memset(childregs, 0, sizeof(struct pt_regs));
2265bd2e97cSEric W. Biederman 		kthread_frame_init(frame, args->fn, args->fn_arg);
2272fff071dSThomas Gleixner 		return 0;
2282fff071dSThomas Gleixner 	}
2292fff071dSThomas Gleixner 
2309782a712SDave Hansen 	/*
2319782a712SDave Hansen 	 * Clone current's PKRU value from hardware. tsk->thread.pkru
2329782a712SDave Hansen 	 * is only valid when scheduled out.
2339782a712SDave Hansen 	 */
2349782a712SDave Hansen 	p->thread.pkru = read_pkru();
2359782a712SDave Hansen 
2362fff071dSThomas Gleixner 	frame->bx = 0;
2372fff071dSThomas Gleixner 	*childregs = *current_pt_regs();
2382fff071dSThomas Gleixner 	childregs->ax = 0;
2392fff071dSThomas Gleixner 	if (sp)
2402fff071dSThomas Gleixner 		childregs->sp = sp;
2412fff071dSThomas Gleixner 
2425bd2e97cSEric W. Biederman 	if (unlikely(args->fn)) {
24350b7b6f2SStefan Metzmacher 		/*
2445bd2e97cSEric W. Biederman 		 * A user space thread, but it doesn't return to
2455bd2e97cSEric W. Biederman 		 * ret_after_fork().
24650b7b6f2SStefan Metzmacher 		 *
24750b7b6f2SStefan Metzmacher 		 * In order to indicate that to tools like gdb,
24850b7b6f2SStefan Metzmacher 		 * we reset the stack and instruction pointers.
24950b7b6f2SStefan Metzmacher 		 *
25050b7b6f2SStefan Metzmacher 		 * It does the same kernel frame setup to return to a kernel
25150b7b6f2SStefan Metzmacher 		 * function that a kernel thread does.
25250b7b6f2SStefan Metzmacher 		 */
25350b7b6f2SStefan Metzmacher 		childregs->sp = 0;
25450b7b6f2SStefan Metzmacher 		childregs->ip = 0;
2555bd2e97cSEric W. Biederman 		kthread_frame_init(frame, args->fn, args->fn_arg);
25650b7b6f2SStefan Metzmacher 		return 0;
25750b7b6f2SStefan Metzmacher 	}
25850b7b6f2SStefan Metzmacher 
2592fff071dSThomas Gleixner 	/* Set a new TLS for the child thread? */
2604804e382SThomas Gleixner 	if (clone_flags & CLONE_SETTLS)
2612fff071dSThomas Gleixner 		ret = set_new_tls(p, tls);
2624804e382SThomas Gleixner 
2634804e382SThomas Gleixner 	if (!ret && unlikely(test_tsk_thread_flag(current, TIF_IO_BITMAP)))
2644804e382SThomas Gleixner 		io_bitmap_share(p);
2654804e382SThomas Gleixner 
2662fff071dSThomas Gleixner 	return ret;
2672fff071dSThomas Gleixner }
2682fff071dSThomas Gleixner 
pkru_flush_thread(void)26933344368SAndy Lutomirski static void pkru_flush_thread(void)
27033344368SAndy Lutomirski {
27133344368SAndy Lutomirski 	/*
27233344368SAndy Lutomirski 	 * If PKRU is enabled the default PKRU value has to be loaded into
27333344368SAndy Lutomirski 	 * the hardware right here (similar to context switch).
27433344368SAndy Lutomirski 	 */
27533344368SAndy Lutomirski 	pkru_write_default();
27633344368SAndy Lutomirski }
27733344368SAndy Lutomirski 
flush_thread(void)278389d1fb1SJeremy Fitzhardinge void flush_thread(void)
279389d1fb1SJeremy Fitzhardinge {
280389d1fb1SJeremy Fitzhardinge 	struct task_struct *tsk = current;
281389d1fb1SJeremy Fitzhardinge 
28224f1e32cSFrederic Weisbecker 	flush_ptrace_hw_breakpoint(tsk);
283389d1fb1SJeremy Fitzhardinge 	memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
284110d7f75SOleg Nesterov 
285e7ecad17SThomas Gleixner 	fpu_flush_thread();
28633344368SAndy Lutomirski 	pkru_flush_thread();
287389d1fb1SJeremy Fitzhardinge }
288389d1fb1SJeremy Fitzhardinge 
disable_TSC(void)289389d1fb1SJeremy Fitzhardinge void disable_TSC(void)
290389d1fb1SJeremy Fitzhardinge {
291389d1fb1SJeremy Fitzhardinge 	preempt_disable();
292389d1fb1SJeremy Fitzhardinge 	if (!test_and_set_thread_flag(TIF_NOTSC))
293389d1fb1SJeremy Fitzhardinge 		/*
294389d1fb1SJeremy Fitzhardinge 		 * Must flip the CPU state synchronously with
295389d1fb1SJeremy Fitzhardinge 		 * TIF_NOTSC in the current running context.
296389d1fb1SJeremy Fitzhardinge 		 */
2975a920155SThomas Gleixner 		cr4_set_bits(X86_CR4_TSD);
298389d1fb1SJeremy Fitzhardinge 	preempt_enable();
299389d1fb1SJeremy Fitzhardinge }
300389d1fb1SJeremy Fitzhardinge 
enable_TSC(void)301389d1fb1SJeremy Fitzhardinge static void enable_TSC(void)
302389d1fb1SJeremy Fitzhardinge {
303389d1fb1SJeremy Fitzhardinge 	preempt_disable();
304389d1fb1SJeremy Fitzhardinge 	if (test_and_clear_thread_flag(TIF_NOTSC))
305389d1fb1SJeremy Fitzhardinge 		/*
306389d1fb1SJeremy Fitzhardinge 		 * Must flip the CPU state synchronously with
307389d1fb1SJeremy Fitzhardinge 		 * TIF_NOTSC in the current running context.
308389d1fb1SJeremy Fitzhardinge 		 */
3095a920155SThomas Gleixner 		cr4_clear_bits(X86_CR4_TSD);
310389d1fb1SJeremy Fitzhardinge 	preempt_enable();
311389d1fb1SJeremy Fitzhardinge }
312389d1fb1SJeremy Fitzhardinge 
get_tsc_mode(unsigned long adr)313389d1fb1SJeremy Fitzhardinge int get_tsc_mode(unsigned long adr)
314389d1fb1SJeremy Fitzhardinge {
315389d1fb1SJeremy Fitzhardinge 	unsigned int val;
316389d1fb1SJeremy Fitzhardinge 
317389d1fb1SJeremy Fitzhardinge 	if (test_thread_flag(TIF_NOTSC))
318389d1fb1SJeremy Fitzhardinge 		val = PR_TSC_SIGSEGV;
319389d1fb1SJeremy Fitzhardinge 	else
320389d1fb1SJeremy Fitzhardinge 		val = PR_TSC_ENABLE;
321389d1fb1SJeremy Fitzhardinge 
322389d1fb1SJeremy Fitzhardinge 	return put_user(val, (unsigned int __user *)adr);
323389d1fb1SJeremy Fitzhardinge }
324389d1fb1SJeremy Fitzhardinge 
set_tsc_mode(unsigned int val)325389d1fb1SJeremy Fitzhardinge int set_tsc_mode(unsigned int val)
326389d1fb1SJeremy Fitzhardinge {
327389d1fb1SJeremy Fitzhardinge 	if (val == PR_TSC_SIGSEGV)
328389d1fb1SJeremy Fitzhardinge 		disable_TSC();
329389d1fb1SJeremy Fitzhardinge 	else if (val == PR_TSC_ENABLE)
330389d1fb1SJeremy Fitzhardinge 		enable_TSC();
331389d1fb1SJeremy Fitzhardinge 	else
332389d1fb1SJeremy Fitzhardinge 		return -EINVAL;
333389d1fb1SJeremy Fitzhardinge 
334389d1fb1SJeremy Fitzhardinge 	return 0;
335389d1fb1SJeremy Fitzhardinge }
336389d1fb1SJeremy Fitzhardinge 
337e9ea1e7fSKyle Huey DEFINE_PER_CPU(u64, msr_misc_features_shadow);
338e9ea1e7fSKyle Huey 
set_cpuid_faulting(bool on)339e9ea1e7fSKyle Huey static void set_cpuid_faulting(bool on)
340e9ea1e7fSKyle Huey {
341e9ea1e7fSKyle Huey 	u64 msrval;
342e9ea1e7fSKyle Huey 
343e9ea1e7fSKyle Huey 	msrval = this_cpu_read(msr_misc_features_shadow);
344e9ea1e7fSKyle Huey 	msrval &= ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
345e9ea1e7fSKyle Huey 	msrval |= (on << MSR_MISC_FEATURES_ENABLES_CPUID_FAULT_BIT);
346e9ea1e7fSKyle Huey 	this_cpu_write(msr_misc_features_shadow, msrval);
347e9ea1e7fSKyle Huey 	wrmsrl(MSR_MISC_FEATURES_ENABLES, msrval);
348e9ea1e7fSKyle Huey }
349e9ea1e7fSKyle Huey 
disable_cpuid(void)350e9ea1e7fSKyle Huey static void disable_cpuid(void)
351e9ea1e7fSKyle Huey {
352e9ea1e7fSKyle Huey 	preempt_disable();
353e9ea1e7fSKyle Huey 	if (!test_and_set_thread_flag(TIF_NOCPUID)) {
354e9ea1e7fSKyle Huey 		/*
355e9ea1e7fSKyle Huey 		 * Must flip the CPU state synchronously with
356e9ea1e7fSKyle Huey 		 * TIF_NOCPUID in the current running context.
357e9ea1e7fSKyle Huey 		 */
358e9ea1e7fSKyle Huey 		set_cpuid_faulting(true);
359e9ea1e7fSKyle Huey 	}
360e9ea1e7fSKyle Huey 	preempt_enable();
361e9ea1e7fSKyle Huey }
362e9ea1e7fSKyle Huey 
enable_cpuid(void)363e9ea1e7fSKyle Huey static void enable_cpuid(void)
364e9ea1e7fSKyle Huey {
365e9ea1e7fSKyle Huey 	preempt_disable();
366e9ea1e7fSKyle Huey 	if (test_and_clear_thread_flag(TIF_NOCPUID)) {
367e9ea1e7fSKyle Huey 		/*
368e9ea1e7fSKyle Huey 		 * Must flip the CPU state synchronously with
369e9ea1e7fSKyle Huey 		 * TIF_NOCPUID in the current running context.
370e9ea1e7fSKyle Huey 		 */
371e9ea1e7fSKyle Huey 		set_cpuid_faulting(false);
372e9ea1e7fSKyle Huey 	}
373e9ea1e7fSKyle Huey 	preempt_enable();
374e9ea1e7fSKyle Huey }
375e9ea1e7fSKyle Huey 
get_cpuid_mode(void)376e9ea1e7fSKyle Huey static int get_cpuid_mode(void)
377e9ea1e7fSKyle Huey {
378e9ea1e7fSKyle Huey 	return !test_thread_flag(TIF_NOCPUID);
379e9ea1e7fSKyle Huey }
380e9ea1e7fSKyle Huey 
set_cpuid_mode(unsigned long cpuid_enabled)381f5c0b4f3SThomas Gleixner static int set_cpuid_mode(unsigned long cpuid_enabled)
382e9ea1e7fSKyle Huey {
38367e87d43SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_CPUID_FAULT))
384e9ea1e7fSKyle Huey 		return -ENODEV;
385e9ea1e7fSKyle Huey 
386e9ea1e7fSKyle Huey 	if (cpuid_enabled)
387e9ea1e7fSKyle Huey 		enable_cpuid();
388e9ea1e7fSKyle Huey 	else
389e9ea1e7fSKyle Huey 		disable_cpuid();
390e9ea1e7fSKyle Huey 
391e9ea1e7fSKyle Huey 	return 0;
392e9ea1e7fSKyle Huey }
393e9ea1e7fSKyle Huey 
394e9ea1e7fSKyle Huey /*
395e9ea1e7fSKyle Huey  * Called immediately after a successful exec.
396e9ea1e7fSKyle Huey  */
arch_setup_new_exec(void)397e9ea1e7fSKyle Huey void arch_setup_new_exec(void)
398e9ea1e7fSKyle Huey {
399e9ea1e7fSKyle Huey 	/* If cpuid was previously disabled for this task, re-enable it. */
400e9ea1e7fSKyle Huey 	if (test_thread_flag(TIF_NOCPUID))
401e9ea1e7fSKyle Huey 		enable_cpuid();
40271368af9SWaiman Long 
40371368af9SWaiman Long 	/*
40471368af9SWaiman Long 	 * Don't inherit TIF_SSBD across exec boundary when
40571368af9SWaiman Long 	 * PR_SPEC_DISABLE_NOEXEC is used.
40671368af9SWaiman Long 	 */
40771368af9SWaiman Long 	if (test_thread_flag(TIF_SSBD) &&
40871368af9SWaiman Long 	    task_spec_ssb_noexec(current)) {
40971368af9SWaiman Long 		clear_thread_flag(TIF_SSBD);
41071368af9SWaiman Long 		task_clear_spec_ssb_disable(current);
41171368af9SWaiman Long 		task_clear_spec_ssb_noexec(current);
412dca99fb6SMark Rutland 		speculation_ctrl_update(read_thread_flags());
41371368af9SWaiman Long 	}
41474c228d2SKirill A. Shutemov 
41574c228d2SKirill A. Shutemov 	mm_reset_untag_mask(current->mm);
416e9ea1e7fSKyle Huey }
417e9ea1e7fSKyle Huey 
418111e7b15SThomas Gleixner #ifdef CONFIG_X86_IOPL_IOPERM
switch_to_bitmap(unsigned long tifp)41922fe5b04SThomas Gleixner static inline void switch_to_bitmap(unsigned long tifp)
42022fe5b04SThomas Gleixner {
42122fe5b04SThomas Gleixner 	/*
42222fe5b04SThomas Gleixner 	 * Invalidate I/O bitmap if the previous task used it. This prevents
42322fe5b04SThomas Gleixner 	 * any possible leakage of an active I/O bitmap.
42422fe5b04SThomas Gleixner 	 *
42522fe5b04SThomas Gleixner 	 * If the next task has an I/O bitmap it will handle it on exit to
42622fe5b04SThomas Gleixner 	 * user mode.
42722fe5b04SThomas Gleixner 	 */
42822fe5b04SThomas Gleixner 	if (tifp & _TIF_IO_BITMAP)
429cadfad87SAndy Lutomirski 		tss_invalidate_io_bitmap();
43022fe5b04SThomas Gleixner }
43122fe5b04SThomas Gleixner 
tss_copy_io_bitmap(struct tss_struct * tss,struct io_bitmap * iobm)43222fe5b04SThomas Gleixner static void tss_copy_io_bitmap(struct tss_struct *tss, struct io_bitmap *iobm)
433060aa16fSThomas Gleixner {
434060aa16fSThomas Gleixner 	/*
435060aa16fSThomas Gleixner 	 * Copy at least the byte range of the incoming tasks bitmap which
436060aa16fSThomas Gleixner 	 * covers the permitted I/O ports.
437060aa16fSThomas Gleixner 	 *
438060aa16fSThomas Gleixner 	 * If the previous task which used an I/O bitmap had more bits
439060aa16fSThomas Gleixner 	 * permitted, then the copy needs to cover those as well so they
440060aa16fSThomas Gleixner 	 * get turned off.
441060aa16fSThomas Gleixner 	 */
442060aa16fSThomas Gleixner 	memcpy(tss->io_bitmap.bitmap, iobm->bitmap,
443060aa16fSThomas Gleixner 	       max(tss->io_bitmap.prev_max, iobm->max));
444060aa16fSThomas Gleixner 
445060aa16fSThomas Gleixner 	/*
446060aa16fSThomas Gleixner 	 * Store the new max and the sequence number of this bitmap
447060aa16fSThomas Gleixner 	 * and a pointer to the bitmap itself.
448060aa16fSThomas Gleixner 	 */
449060aa16fSThomas Gleixner 	tss->io_bitmap.prev_max = iobm->max;
450060aa16fSThomas Gleixner 	tss->io_bitmap.prev_sequence = iobm->sequence;
451060aa16fSThomas Gleixner }
452060aa16fSThomas Gleixner 
45322fe5b04SThomas Gleixner /**
454dbb5ab6dSJiapeng Chong  * native_tss_update_io_bitmap - Update I/O bitmap before exiting to user mode
45522fe5b04SThomas Gleixner  */
native_tss_update_io_bitmap(void)45699bcd4a6SJuergen Gross void native_tss_update_io_bitmap(void)
457389d1fb1SJeremy Fitzhardinge {
458ff16701aSThomas Gleixner 	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
4597b0b8cfdSBorislav Petkov 	struct thread_struct *t = &current->thread;
460c8137aceSThomas Gleixner 	u16 *base = &tss->x86_tss.io_bitmap_base;
461ff16701aSThomas Gleixner 
4627b0b8cfdSBorislav Petkov 	if (!test_thread_flag(TIF_IO_BITMAP)) {
463cadfad87SAndy Lutomirski 		native_tss_invalidate_io_bitmap();
4647b0b8cfdSBorislav Petkov 		return;
4657b0b8cfdSBorislav Petkov 	}
466577d5cd7SThomas Gleixner 
467e3cb0c71SAlexander Duyck 	if (IS_ENABLED(CONFIG_X86_IOPL_IOPERM) && t->iopl_emul == 3) {
468c8137aceSThomas Gleixner 		*base = IO_BITMAP_OFFSET_VALID_ALL;
469c8137aceSThomas Gleixner 	} else {
470c8137aceSThomas Gleixner 		struct io_bitmap *iobm = t->io_bitmap;
4717b0b8cfdSBorislav Petkov 
472389d1fb1SJeremy Fitzhardinge 		/*
4737b0b8cfdSBorislav Petkov 		 * Only copy bitmap data when the sequence number differs. The
4747b0b8cfdSBorislav Petkov 		 * update time is accounted to the incoming task.
475389d1fb1SJeremy Fitzhardinge 		 */
476060aa16fSThomas Gleixner 		if (tss->io_bitmap.prev_sequence != iobm->sequence)
47722fe5b04SThomas Gleixner 			tss_copy_io_bitmap(tss, iobm);
478ecc7e37dSThomas Gleixner 
479060aa16fSThomas Gleixner 		/* Enable the bitmap */
480c8137aceSThomas Gleixner 		*base = IO_BITMAP_OFFSET_VALID_MAP;
481c8137aceSThomas Gleixner 	}
4827b0b8cfdSBorislav Petkov 
483b7ffc44dSAndy Lutomirski 	/*
4847b0b8cfdSBorislav Petkov 	 * Make sure that the TSS limit is covering the IO bitmap. It might have
4857b0b8cfdSBorislav Petkov 	 * been cut down by a VMEXIT to 0x67 which would cause a subsequent I/O
48654aa699eSBjorn Helgaas 	 * access from user space to trigger a #GP because the bitmap is outside
4877b0b8cfdSBorislav Petkov 	 * the TSS limit.
488b7ffc44dSAndy Lutomirski 	 */
489b7ceaec1SAndy Lutomirski 	refresh_tss_limit();
490af8b3cd3SKyle Huey }
491111e7b15SThomas Gleixner #else /* CONFIG_X86_IOPL_IOPERM */
switch_to_bitmap(unsigned long tifp)492111e7b15SThomas Gleixner static inline void switch_to_bitmap(unsigned long tifp) { }
493111e7b15SThomas Gleixner #endif
494af8b3cd3SKyle Huey 
4951f50ddb4SThomas Gleixner #ifdef CONFIG_SMP
496885f82bfSThomas Gleixner 
4971f50ddb4SThomas Gleixner struct ssb_state {
4981f50ddb4SThomas Gleixner 	struct ssb_state	*shared_state;
4991f50ddb4SThomas Gleixner 	raw_spinlock_t		lock;
5001f50ddb4SThomas Gleixner 	unsigned int		disable_state;
5011f50ddb4SThomas Gleixner 	unsigned long		local_state;
5021f50ddb4SThomas Gleixner };
5031f50ddb4SThomas Gleixner 
5041f50ddb4SThomas Gleixner #define LSTATE_SSB	0
5051f50ddb4SThomas Gleixner 
5061f50ddb4SThomas Gleixner static DEFINE_PER_CPU(struct ssb_state, ssb_state);
5071f50ddb4SThomas Gleixner 
speculative_store_bypass_ht_init(void)5081f50ddb4SThomas Gleixner void speculative_store_bypass_ht_init(void)
5091f50ddb4SThomas Gleixner {
5101f50ddb4SThomas Gleixner 	struct ssb_state *st = this_cpu_ptr(&ssb_state);
5111f50ddb4SThomas Gleixner 	unsigned int this_cpu = smp_processor_id();
5121f50ddb4SThomas Gleixner 	unsigned int cpu;
5131f50ddb4SThomas Gleixner 
5141f50ddb4SThomas Gleixner 	st->local_state = 0;
5151f50ddb4SThomas Gleixner 
5161f50ddb4SThomas Gleixner 	/*
5171f50ddb4SThomas Gleixner 	 * Shared state setup happens once on the first bringup
5181f50ddb4SThomas Gleixner 	 * of the CPU. It's not destroyed on CPU hotunplug.
5191f50ddb4SThomas Gleixner 	 */
5201f50ddb4SThomas Gleixner 	if (st->shared_state)
5211f50ddb4SThomas Gleixner 		return;
5221f50ddb4SThomas Gleixner 
5231f50ddb4SThomas Gleixner 	raw_spin_lock_init(&st->lock);
5241f50ddb4SThomas Gleixner 
5251f50ddb4SThomas Gleixner 	/*
5261f50ddb4SThomas Gleixner 	 * Go over HT siblings and check whether one of them has set up the
5271f50ddb4SThomas Gleixner 	 * shared state pointer already.
5281f50ddb4SThomas Gleixner 	 */
5291f50ddb4SThomas Gleixner 	for_each_cpu(cpu, topology_sibling_cpumask(this_cpu)) {
5301f50ddb4SThomas Gleixner 		if (cpu == this_cpu)
5311f50ddb4SThomas Gleixner 			continue;
5321f50ddb4SThomas Gleixner 
5331f50ddb4SThomas Gleixner 		if (!per_cpu(ssb_state, cpu).shared_state)
5341f50ddb4SThomas Gleixner 			continue;
5351f50ddb4SThomas Gleixner 
5361f50ddb4SThomas Gleixner 		/* Link it to the state of the sibling: */
5371f50ddb4SThomas Gleixner 		st->shared_state = per_cpu(ssb_state, cpu).shared_state;
5381f50ddb4SThomas Gleixner 		return;
5391f50ddb4SThomas Gleixner 	}
5401f50ddb4SThomas Gleixner 
5411f50ddb4SThomas Gleixner 	/*
5421f50ddb4SThomas Gleixner 	 * First HT sibling to come up on the core.  Link shared state of
5431f50ddb4SThomas Gleixner 	 * the first HT sibling to itself. The siblings on the same core
5441f50ddb4SThomas Gleixner 	 * which come up later will see the shared state pointer and link
545d9f6e12fSIngo Molnar 	 * themselves to the state of this CPU.
5461f50ddb4SThomas Gleixner 	 */
5471f50ddb4SThomas Gleixner 	st->shared_state = st;
5481f50ddb4SThomas Gleixner }
5491f50ddb4SThomas Gleixner 
5501f50ddb4SThomas Gleixner /*
5511f50ddb4SThomas Gleixner  * Logic is: First HT sibling enables SSBD for both siblings in the core
5521f50ddb4SThomas Gleixner  * and last sibling to disable it, disables it for the whole core. This how
5531f50ddb4SThomas Gleixner  * MSR_SPEC_CTRL works in "hardware":
5541f50ddb4SThomas Gleixner  *
5551f50ddb4SThomas Gleixner  *  CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL
5561f50ddb4SThomas Gleixner  */
amd_set_core_ssb_state(unsigned long tifn)5571f50ddb4SThomas Gleixner static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
5581f50ddb4SThomas Gleixner {
5591f50ddb4SThomas Gleixner 	struct ssb_state *st = this_cpu_ptr(&ssb_state);
5601f50ddb4SThomas Gleixner 	u64 msr = x86_amd_ls_cfg_base;
5611f50ddb4SThomas Gleixner 
5621f50ddb4SThomas Gleixner 	if (!static_cpu_has(X86_FEATURE_ZEN)) {
5631f50ddb4SThomas Gleixner 		msr |= ssbd_tif_to_amd_ls_cfg(tifn);
564885f82bfSThomas Gleixner 		wrmsrl(MSR_AMD64_LS_CFG, msr);
5651f50ddb4SThomas Gleixner 		return;
5661f50ddb4SThomas Gleixner 	}
5671f50ddb4SThomas Gleixner 
5681f50ddb4SThomas Gleixner 	if (tifn & _TIF_SSBD) {
5691f50ddb4SThomas Gleixner 		/*
5701f50ddb4SThomas Gleixner 		 * Since this can race with prctl(), block reentry on the
5711f50ddb4SThomas Gleixner 		 * same CPU.
5721f50ddb4SThomas Gleixner 		 */
5731f50ddb4SThomas Gleixner 		if (__test_and_set_bit(LSTATE_SSB, &st->local_state))
5741f50ddb4SThomas Gleixner 			return;
5751f50ddb4SThomas Gleixner 
5761f50ddb4SThomas Gleixner 		msr |= x86_amd_ls_cfg_ssbd_mask;
5771f50ddb4SThomas Gleixner 
5781f50ddb4SThomas Gleixner 		raw_spin_lock(&st->shared_state->lock);
5791f50ddb4SThomas Gleixner 		/* First sibling enables SSBD: */
5801f50ddb4SThomas Gleixner 		if (!st->shared_state->disable_state)
5811f50ddb4SThomas Gleixner 			wrmsrl(MSR_AMD64_LS_CFG, msr);
5821f50ddb4SThomas Gleixner 		st->shared_state->disable_state++;
5831f50ddb4SThomas Gleixner 		raw_spin_unlock(&st->shared_state->lock);
584885f82bfSThomas Gleixner 	} else {
5851f50ddb4SThomas Gleixner 		if (!__test_and_clear_bit(LSTATE_SSB, &st->local_state))
5861f50ddb4SThomas Gleixner 			return;
5871f50ddb4SThomas Gleixner 
5881f50ddb4SThomas Gleixner 		raw_spin_lock(&st->shared_state->lock);
5891f50ddb4SThomas Gleixner 		st->shared_state->disable_state--;
5901f50ddb4SThomas Gleixner 		if (!st->shared_state->disable_state)
5911f50ddb4SThomas Gleixner 			wrmsrl(MSR_AMD64_LS_CFG, msr);
5921f50ddb4SThomas Gleixner 		raw_spin_unlock(&st->shared_state->lock);
5931f50ddb4SThomas Gleixner 	}
5941f50ddb4SThomas Gleixner }
5951f50ddb4SThomas Gleixner #else
amd_set_core_ssb_state(unsigned long tifn)5961f50ddb4SThomas Gleixner static __always_inline void amd_set_core_ssb_state(unsigned long tifn)
5971f50ddb4SThomas Gleixner {
5981f50ddb4SThomas Gleixner 	u64 msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn);
5991f50ddb4SThomas Gleixner 
6001f50ddb4SThomas Gleixner 	wrmsrl(MSR_AMD64_LS_CFG, msr);
6011f50ddb4SThomas Gleixner }
6021f50ddb4SThomas Gleixner #endif
6031f50ddb4SThomas Gleixner 
amd_set_ssb_virt_state(unsigned long tifn)60411fb0683STom Lendacky static __always_inline void amd_set_ssb_virt_state(unsigned long tifn)
60511fb0683STom Lendacky {
60611fb0683STom Lendacky 	/*
60711fb0683STom Lendacky 	 * SSBD has the same definition in SPEC_CTRL and VIRT_SPEC_CTRL,
60811fb0683STom Lendacky 	 * so ssbd_tif_to_spec_ctrl() just works.
60911fb0683STom Lendacky 	 */
61011fb0683STom Lendacky 	wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, ssbd_tif_to_spec_ctrl(tifn));
61111fb0683STom Lendacky }
61211fb0683STom Lendacky 
61301daf568STim Chen /*
61401daf568STim Chen  * Update the MSRs managing speculation control, during context switch.
61501daf568STim Chen  *
61601daf568STim Chen  * tifp: Previous task's thread flags
61701daf568STim Chen  * tifn: Next task's thread flags
61801daf568STim Chen  */
__speculation_ctrl_update(unsigned long tifp,unsigned long tifn)61901daf568STim Chen static __always_inline void __speculation_ctrl_update(unsigned long tifp,
62001daf568STim Chen 						      unsigned long tifn)
6211f50ddb4SThomas Gleixner {
6225bfbe3adSTim Chen 	unsigned long tif_diff = tifp ^ tifn;
62301daf568STim Chen 	u64 msr = x86_spec_ctrl_base;
62401daf568STim Chen 	bool updmsr = false;
6251f50ddb4SThomas Gleixner 
6262f5fb193SThomas Gleixner 	lockdep_assert_irqs_disabled();
6272f5fb193SThomas Gleixner 
628dbbe2ad0SAnthony Steinhauser 	/* Handle change of TIF_SSBD depending on the mitigation method. */
62901daf568STim Chen 	if (static_cpu_has(X86_FEATURE_VIRT_SSBD)) {
630dbbe2ad0SAnthony Steinhauser 		if (tif_diff & _TIF_SSBD)
63101daf568STim Chen 			amd_set_ssb_virt_state(tifn);
63201daf568STim Chen 	} else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
633dbbe2ad0SAnthony Steinhauser 		if (tif_diff & _TIF_SSBD)
63401daf568STim Chen 			amd_set_core_ssb_state(tifn);
63501daf568STim Chen 	} else if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
63601daf568STim Chen 		   static_cpu_has(X86_FEATURE_AMD_SSBD)) {
637dbbe2ad0SAnthony Steinhauser 		updmsr |= !!(tif_diff & _TIF_SSBD);
63801daf568STim Chen 		msr |= ssbd_tif_to_spec_ctrl(tifn);
639885f82bfSThomas Gleixner 	}
6401f50ddb4SThomas Gleixner 
641dbbe2ad0SAnthony Steinhauser 	/* Only evaluate TIF_SPEC_IB if conditional STIBP is enabled. */
6425bfbe3adSTim Chen 	if (IS_ENABLED(CONFIG_SMP) &&
6435bfbe3adSTim Chen 	    static_branch_unlikely(&switch_to_cond_stibp)) {
6445bfbe3adSTim Chen 		updmsr |= !!(tif_diff & _TIF_SPEC_IB);
6455bfbe3adSTim Chen 		msr |= stibp_tif_to_spec_ctrl(tifn);
6465bfbe3adSTim Chen 	}
6475bfbe3adSTim Chen 
64801daf568STim Chen 	if (updmsr)
64966065157SPawan Gupta 		update_spec_ctrl_cond(msr);
650885f82bfSThomas Gleixner }
6511f50ddb4SThomas Gleixner 
speculation_ctrl_update_tif(struct task_struct * tsk)6526d991ba5SThomas Gleixner static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)
6531f50ddb4SThomas Gleixner {
6546d991ba5SThomas Gleixner 	if (test_and_clear_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE)) {
6556d991ba5SThomas Gleixner 		if (task_spec_ssb_disable(tsk))
6566d991ba5SThomas Gleixner 			set_tsk_thread_flag(tsk, TIF_SSBD);
6571f50ddb4SThomas Gleixner 		else
6586d991ba5SThomas Gleixner 			clear_tsk_thread_flag(tsk, TIF_SSBD);
6599137bb27SThomas Gleixner 
6609137bb27SThomas Gleixner 		if (task_spec_ib_disable(tsk))
6619137bb27SThomas Gleixner 			set_tsk_thread_flag(tsk, TIF_SPEC_IB);
6629137bb27SThomas Gleixner 		else
6639137bb27SThomas Gleixner 			clear_tsk_thread_flag(tsk, TIF_SPEC_IB);
6646d991ba5SThomas Gleixner 	}
6656d991ba5SThomas Gleixner 	/* Return the updated threadinfo flags*/
666dca99fb6SMark Rutland 	return read_task_thread_flags(tsk);
667885f82bfSThomas Gleixner }
668885f82bfSThomas Gleixner 
speculation_ctrl_update(unsigned long tif)66926c4d75bSThomas Gleixner void speculation_ctrl_update(unsigned long tif)
670885f82bfSThomas Gleixner {
6712f5fb193SThomas Gleixner 	unsigned long flags;
6722f5fb193SThomas Gleixner 
67301daf568STim Chen 	/* Forced update. Make sure all relevant TIF flags are different */
6742f5fb193SThomas Gleixner 	local_irq_save(flags);
67501daf568STim Chen 	__speculation_ctrl_update(~tif, tif);
6762f5fb193SThomas Gleixner 	local_irq_restore(flags);
677885f82bfSThomas Gleixner }
678885f82bfSThomas Gleixner 
6796d991ba5SThomas Gleixner /* Called from seccomp/prctl update */
speculation_ctrl_update_current(void)6806d991ba5SThomas Gleixner void speculation_ctrl_update_current(void)
6816d991ba5SThomas Gleixner {
6826d991ba5SThomas Gleixner 	preempt_disable();
6836d991ba5SThomas Gleixner 	speculation_ctrl_update(speculation_ctrl_update_tif(current));
6846d991ba5SThomas Gleixner 	preempt_enable();
6856d991ba5SThomas Gleixner }
6866d991ba5SThomas Gleixner 
cr4_toggle_bits_irqsoff(unsigned long mask)687d8f0b353SThomas Gleixner static inline void cr4_toggle_bits_irqsoff(unsigned long mask)
688d8f0b353SThomas Gleixner {
689d8f0b353SThomas Gleixner 	unsigned long newval, cr4 = this_cpu_read(cpu_tlbstate.cr4);
690d8f0b353SThomas Gleixner 
691d8f0b353SThomas Gleixner 	newval = cr4 ^ mask;
692d8f0b353SThomas Gleixner 	if (newval != cr4) {
693d8f0b353SThomas Gleixner 		this_cpu_write(cpu_tlbstate.cr4, newval);
694d8f0b353SThomas Gleixner 		__write_cr4(newval);
695d8f0b353SThomas Gleixner 	}
696d8f0b353SThomas Gleixner }
697d8f0b353SThomas Gleixner 
__switch_to_xtra(struct task_struct * prev_p,struct task_struct * next_p)698ff16701aSThomas Gleixner void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p)
699af8b3cd3SKyle Huey {
700af8b3cd3SKyle Huey 	unsigned long tifp, tifn;
701af8b3cd3SKyle Huey 
702dca99fb6SMark Rutland 	tifn = read_task_thread_flags(next_p);
703dca99fb6SMark Rutland 	tifp = read_task_thread_flags(prev_p);
70422fe5b04SThomas Gleixner 
70522fe5b04SThomas Gleixner 	switch_to_bitmap(tifp);
706af8b3cd3SKyle Huey 
7077c68af6eSAvi Kivity 	propagate_user_return_notify(prev_p, next_p);
708af8b3cd3SKyle Huey 
709b9894a2fSKyle Huey 	if ((tifp & _TIF_BLOCKSTEP || tifn & _TIF_BLOCKSTEP) &&
710b9894a2fSKyle Huey 	    arch_has_block_step()) {
711b9894a2fSKyle Huey 		unsigned long debugctl, msk;
712af8b3cd3SKyle Huey 
713b9894a2fSKyle Huey 		rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
714af8b3cd3SKyle Huey 		debugctl &= ~DEBUGCTLMSR_BTF;
715b9894a2fSKyle Huey 		msk = tifn & _TIF_BLOCKSTEP;
716b9894a2fSKyle Huey 		debugctl |= (msk >> TIF_BLOCKSTEP) << DEBUGCTLMSR_BTF_SHIFT;
717b9894a2fSKyle Huey 		wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
718af8b3cd3SKyle Huey 	}
719af8b3cd3SKyle Huey 
7205a920155SThomas Gleixner 	if ((tifp ^ tifn) & _TIF_NOTSC)
7219d0b6232SNadav Amit 		cr4_toggle_bits_irqsoff(X86_CR4_TSD);
722e9ea1e7fSKyle Huey 
723e9ea1e7fSKyle Huey 	if ((tifp ^ tifn) & _TIF_NOCPUID)
724e9ea1e7fSKyle Huey 		set_cpuid_faulting(!!(tifn & _TIF_NOCPUID));
725885f82bfSThomas Gleixner 
7266d991ba5SThomas Gleixner 	if (likely(!((tifp | tifn) & _TIF_SPEC_FORCE_UPDATE))) {
72701daf568STim Chen 		__speculation_ctrl_update(tifp, tifn);
7286d991ba5SThomas Gleixner 	} else {
7296d991ba5SThomas Gleixner 		speculation_ctrl_update_tif(prev_p);
7306d991ba5SThomas Gleixner 		tifn = speculation_ctrl_update_tif(next_p);
7316d991ba5SThomas Gleixner 
7326d991ba5SThomas Gleixner 		/* Enforce MSR update to ensure consistent state */
7336d991ba5SThomas Gleixner 		__speculation_ctrl_update(~tifn, tifn);
7346d991ba5SThomas Gleixner 	}
735389d1fb1SJeremy Fitzhardinge }
736389d1fb1SJeremy Fitzhardinge 
737df59e7bfSBrian Gerst /*
73800dba564SThomas Gleixner  * Idle related variables and functions
73900dba564SThomas Gleixner  */
740d1896049SThomas Renninger unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
74100dba564SThomas Gleixner EXPORT_SYMBOL(boot_option_idle_override);
74200dba564SThomas Gleixner 
743aaa3896bSPeter Zijlstra /*
744aaa3896bSPeter Zijlstra  * We use this if we don't have any better idle routine..
745aaa3896bSPeter Zijlstra  */
default_idle(void)746aaa3896bSPeter Zijlstra void __cpuidle default_idle(void)
747aaa3896bSPeter Zijlstra {
748aaa3896bSPeter Zijlstra 	raw_safe_halt();
74989b30987SPeter Zijlstra 	raw_local_irq_disable();
750aaa3896bSPeter Zijlstra }
751aaa3896bSPeter Zijlstra #if defined(CONFIG_APM_MODULE) || defined(CONFIG_HALTPOLL_CPUIDLE_MODULE)
752aaa3896bSPeter Zijlstra EXPORT_SYMBOL(default_idle);
753aaa3896bSPeter Zijlstra #endif
754aaa3896bSPeter Zijlstra 
755aaa3896bSPeter Zijlstra DEFINE_STATIC_CALL_NULL(x86_idle, default_idle);
756aaa3896bSPeter Zijlstra 
x86_idle_set(void)757aaa3896bSPeter Zijlstra static bool x86_idle_set(void)
758aaa3896bSPeter Zijlstra {
759aaa3896bSPeter Zijlstra 	return !!static_call_query(x86_idle);
760aaa3896bSPeter Zijlstra }
76100dba564SThomas Gleixner 
76290e24014SRichard Weinberger #ifndef CONFIG_SMP
play_dead(void)763eab89405SJosh Poimboeuf static inline void __noreturn play_dead(void)
76490e24014SRichard Weinberger {
76590e24014SRichard Weinberger 	BUG();
76690e24014SRichard Weinberger }
76790e24014SRichard Weinberger #endif
76890e24014SRichard Weinberger 
arch_cpu_idle_enter(void)7697d1a9417SThomas Gleixner void arch_cpu_idle_enter(void)
7707d1a9417SThomas Gleixner {
7716a369583SThomas Gleixner 	tsc_verify_tsc_adjust(false);
77290e24014SRichard Weinberger 	local_touch_nmi();
77390e24014SRichard Weinberger }
77490e24014SRichard Weinberger 
arch_cpu_idle_dead(void)775071c44e4SJosh Poimboeuf void __noreturn arch_cpu_idle_dead(void)
7767d1a9417SThomas Gleixner {
7777d1a9417SThomas Gleixner 	play_dead();
77890e24014SRichard Weinberger }
77990e24014SRichard Weinberger 
78000dba564SThomas Gleixner /*
7817d1a9417SThomas Gleixner  * Called from the generic idle code.
7827d1a9417SThomas Gleixner  */
arch_cpu_idle(void)783aaa3896bSPeter Zijlstra void __cpuidle arch_cpu_idle(void)
7847d1a9417SThomas Gleixner {
785aaa3896bSPeter Zijlstra 	static_call(x86_idle)();
7867d1a9417SThomas Gleixner }
787716ff71aSLi RongQing EXPORT_SYMBOL_GPL(arch_cpu_idle);
7887d1a9417SThomas Gleixner 
7896a377ddcSLen Brown #ifdef CONFIG_XEN
xen_set_default_idle(void)7906a377ddcSLen Brown bool xen_set_default_idle(void)
791e5fd47bfSKonrad Rzeszutek Wilk {
792aaa3896bSPeter Zijlstra 	bool ret = x86_idle_set();
793e5fd47bfSKonrad Rzeszutek Wilk 
794aaa3896bSPeter Zijlstra 	static_call_update(x86_idle, default_idle);
795e5fd47bfSKonrad Rzeszutek Wilk 
796e5fd47bfSKonrad Rzeszutek Wilk 	return ret;
797e5fd47bfSKonrad Rzeszutek Wilk }
7986a377ddcSLen Brown #endif
799bba4ed01STom Lendacky 
8001f5e7eb7SThomas Gleixner struct cpumask cpus_stop_mask;
8011f5e7eb7SThomas Gleixner 
stop_this_cpu(void * dummy)802f9cdf7caSPeter Zijlstra void __noreturn stop_this_cpu(void *dummy)
803d3ec5caeSIvan Vecera {
8049b040453STony Battersby 	struct cpuinfo_x86 *c = this_cpu_ptr(&cpu_info);
8051f5e7eb7SThomas Gleixner 	unsigned int cpu = smp_processor_id();
8061f5e7eb7SThomas Gleixner 
807d3ec5caeSIvan Vecera 	local_irq_disable();
8081f5e7eb7SThomas Gleixner 
809d3ec5caeSIvan Vecera 	/*
8101f5e7eb7SThomas Gleixner 	 * Remove this CPU from the online mask and disable it
8111f5e7eb7SThomas Gleixner 	 * unconditionally. This might be redundant in case that the reboot
8121f5e7eb7SThomas Gleixner 	 * vector was handled late and stop_other_cpus() sent an NMI.
8131f5e7eb7SThomas Gleixner 	 *
8141f5e7eb7SThomas Gleixner 	 * According to SDM and APM NMIs can be accepted even after soft
8151f5e7eb7SThomas Gleixner 	 * disabling the local APIC.
816d3ec5caeSIvan Vecera 	 */
8171f5e7eb7SThomas Gleixner 	set_cpu_online(cpu, false);
818d3ec5caeSIvan Vecera 	disable_local_APIC();
8199b040453STony Battersby 	mcheck_cpu_clear(c);
820d3ec5caeSIvan Vecera 
821f23d74f6STom Lendacky 	/*
822f23d74f6STom Lendacky 	 * Use wbinvd on processors that support SME. This provides support
823f23d74f6STom Lendacky 	 * for performing a successful kexec when going from SME inactive
824f23d74f6STom Lendacky 	 * to SME active (or vice-versa). The cache must be cleared so that
825f23d74f6STom Lendacky 	 * if there are entries with the same physical address, both with and
826f23d74f6STom Lendacky 	 * without the encryption bit, they don't race each other when flushed
827f23d74f6STom Lendacky 	 * and potentially end up with the wrong entry being committed to
828f23d74f6STom Lendacky 	 * memory.
82908f253ecSMario Limonciello 	 *
83008f253ecSMario Limonciello 	 * Test the CPUID bit directly because the machine might've cleared
83108f253ecSMario Limonciello 	 * X86_FEATURE_SME due to cmdline options.
832f23d74f6STom Lendacky 	 */
8339b040453STony Battersby 	if (c->extended_cpuid_level >= 0x8000001f && (cpuid_eax(0x8000001f) & BIT(0)))
83429188c16SJuergen Gross 		wbinvd();
8351f5e7eb7SThomas Gleixner 
8361f5e7eb7SThomas Gleixner 	/*
8371f5e7eb7SThomas Gleixner 	 * This brings a cache line back and dirties it, but
8381f5e7eb7SThomas Gleixner 	 * native_stop_other_cpus() will overwrite cpus_stop_mask after it
8391f5e7eb7SThomas Gleixner 	 * observed that all CPUs reported stop. This write will invalidate
8401f5e7eb7SThomas Gleixner 	 * the related cache line on this CPU.
8411f5e7eb7SThomas Gleixner 	 */
8421f5e7eb7SThomas Gleixner 	cpumask_clear_cpu(cpu, &cpus_stop_mask);
8431f5e7eb7SThomas Gleixner 
84426ba7353SKirill A. Shutemov #ifdef CONFIG_SMP
84526ba7353SKirill A. Shutemov 	if (smp_ops.stop_this_cpu) {
84626ba7353SKirill A. Shutemov 		smp_ops.stop_this_cpu();
8472190966fSPeter Zijlstra 		BUG();
84826ba7353SKirill A. Shutemov 	}
84926ba7353SKirill A. Shutemov #endif
85026ba7353SKirill A. Shutemov 
851bba4ed01STom Lendacky 	for (;;) {
852bba4ed01STom Lendacky 		/*
853f23d74f6STom Lendacky 		 * Use native_halt() so that memory contents don't change
854f23d74f6STom Lendacky 		 * (stack usage and variables) after possibly issuing the
85529188c16SJuergen Gross 		 * wbinvd() above.
856bba4ed01STom Lendacky 		 */
857f23d74f6STom Lendacky 		native_halt();
858bba4ed01STom Lendacky 	}
859d3ec5caeSIvan Vecera }
8607f424a8bSPeter Zijlstra 
861aa276e1cSThomas Gleixner /*
862aebef63cSWyes Karny  * Prefer MWAIT over HALT if MWAIT is supported, MWAIT_CPUID leaf
863aebef63cSWyes Karny  * exists and whenever MONITOR/MWAIT extensions are present there is at
864aebef63cSWyes Karny  * least one C1 substate.
865b253149bSLen Brown  *
866aebef63cSWyes Karny  * Do not prefer MWAIT if MONITOR instruction has a bug or idle=nomwait
867aebef63cSWyes Karny  * is passed to kernel commandline parameter.
868b253149bSLen Brown  */
prefer_mwait_c1_over_halt(void)86935ce6492SThomas Gleixner static __init bool prefer_mwait_c1_over_halt(void)
870b253149bSLen Brown {
87135ce6492SThomas Gleixner 	const struct cpuinfo_x86 *c = &boot_cpu_data;
872aebef63cSWyes Karny 	u32 eax, ebx, ecx, edx;
873aebef63cSWyes Karny 
874f3d7eab7SThomas Gleixner 	/* If override is enforced on the command line, fall back to HALT. */
875f3d7eab7SThomas Gleixner 	if (boot_option_idle_override != IDLE_NO_OVERRIDE)
8765f75916eSThomas Gleixner 		return false;
877b253149bSLen Brown 
878aebef63cSWyes Karny 	/* MWAIT is not supported on this platform. Fallback to HALT */
879aebef63cSWyes Karny 	if (!cpu_has(c, X86_FEATURE_MWAIT))
8805f75916eSThomas Gleixner 		return false;
881b253149bSLen Brown 
882cb81deefSThomas Gleixner 	/* Monitor has a bug or APIC stops in C1E. Fallback to HALT */
883cb81deefSThomas Gleixner 	if (boot_cpu_has_bug(X86_BUG_MONITOR) || boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E))
8845f75916eSThomas Gleixner 		return false;
885b253149bSLen Brown 
886e5d3a578SDave Hansen 	cpuid(CPUID_LEAF_MWAIT, &eax, &ebx, &ecx, &edx);
887aebef63cSWyes Karny 
888aebef63cSWyes Karny 	/*
889aebef63cSWyes Karny 	 * If MWAIT extensions are not available, it is safe to use MWAIT
890aebef63cSWyes Karny 	 * with EAX=0, ECX=0.
891aebef63cSWyes Karny 	 */
892aebef63cSWyes Karny 	if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED))
8935f75916eSThomas Gleixner 		return true;
894aebef63cSWyes Karny 
895aebef63cSWyes Karny 	/*
896aebef63cSWyes Karny 	 * If MWAIT extensions are available, there should be at least one
897aebef63cSWyes Karny 	 * MWAIT C1 substate present.
898aebef63cSWyes Karny 	 */
8995f75916eSThomas Gleixner 	return !!(edx & MWAIT_C1_SUBSTATE_MASK);
900b253149bSLen Brown }
901b253149bSLen Brown 
902b253149bSLen Brown /*
9030fb0328dSHuang Rui  * MONITOR/MWAIT with no hints, used for default C1 state. This invokes MWAIT
9040fb0328dSHuang Rui  * with interrupts enabled and no flags, which is backwards compatible with the
9050fb0328dSHuang Rui  * original MWAIT implementation.
906b253149bSLen Brown  */
mwait_idle(void)9076727ad9eSChris Metcalf static __cpuidle void mwait_idle(void)
908b253149bSLen Brown {
909f8e617f4SMike Galbraith 	if (!current_set_polling_and_test()) {
910f8e617f4SMike Galbraith 		if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) {
911ca59809fSMichael S. Tsirkin 			mb(); /* quirk */
912b253149bSLen Brown 			clflush((void *)&current_thread_info()->flags);
913ca59809fSMichael S. Tsirkin 			mb(); /* quirk */
914f8e617f4SMike Galbraith 		}
915b253149bSLen Brown 
916b253149bSLen Brown 		__monitor((void *)&current_thread_info()->flags, 0, 0);
91789b30987SPeter Zijlstra 		if (!need_resched()) {
918b253149bSLen Brown 			__sti_mwait(0, 0);
91989b30987SPeter Zijlstra 			raw_local_irq_disable();
92089b30987SPeter Zijlstra 		}
921b253149bSLen Brown 	}
922f8e617f4SMike Galbraith 	__current_clr_polling();
923f8e617f4SMike Galbraith }
924b253149bSLen Brown 
select_idle_routine(void)92535ce6492SThomas Gleixner void __init select_idle_routine(void)
9267f424a8bSPeter Zijlstra {
9270ab56287SThomas Gleixner 	if (boot_option_idle_override == IDLE_POLL) {
928685d9821SLinus Torvalds 		if (IS_ENABLED(CONFIG_SMP) && __max_threads_per_core > 1)
929c767a54bSJoe Perches 			pr_warn_once("WARNING: polling idle and HT enabled, performance may degrade\n");
9300ab56287SThomas Gleixner 		return;
9310ab56287SThomas Gleixner 	}
9320ab56287SThomas Gleixner 
93335ce6492SThomas Gleixner 	/* Required to guard against xen_set_default_idle() */
9340ab56287SThomas Gleixner 	if (x86_idle_set())
9356ddd2a27SThomas Gleixner 		return;
9366ddd2a27SThomas Gleixner 
93735ce6492SThomas Gleixner 	if (prefer_mwait_c1_over_halt()) {
938b253149bSLen Brown 		pr_info("using mwait in idle threads\n");
939aaa3896bSPeter Zijlstra 		static_call_update(x86_idle, mwait_idle);
940bfe6ed0cSKirill A. Shutemov 	} else if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
941bfe6ed0cSKirill A. Shutemov 		pr_info("using TDX aware idle routine\n");
942*9f98a4f4SVishal Annapurve 		static_call_update(x86_idle, tdx_halt);
943cb81deefSThomas Gleixner 	} else {
944aaa3896bSPeter Zijlstra 		static_call_update(x86_idle, default_idle);
9457f424a8bSPeter Zijlstra 	}
946cb81deefSThomas Gleixner }
9477f424a8bSPeter Zijlstra 
amd_e400_c1e_apic_setup(void)94807c94a38SBorislav Petkov void amd_e400_c1e_apic_setup(void)
94930e1e6d1SRusty Russell {
95007c94a38SBorislav Petkov 	if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) {
95107c94a38SBorislav Petkov 		pr_info("Switch to broadcast mode on CPU%d\n", smp_processor_id());
95207c94a38SBorislav Petkov 		local_irq_disable();
95307c94a38SBorislav Petkov 		tick_broadcast_force();
95407c94a38SBorislav Petkov 		local_irq_enable();
95507c94a38SBorislav Petkov 	}
95630e1e6d1SRusty Russell }
95730e1e6d1SRusty Russell 
arch_post_acpi_subsys_init(void)958e7ff3a47SThomas Gleixner void __init arch_post_acpi_subsys_init(void)
959e7ff3a47SThomas Gleixner {
960e7ff3a47SThomas Gleixner 	u32 lo, hi;
961e7ff3a47SThomas Gleixner 
962e7ff3a47SThomas Gleixner 	if (!boot_cpu_has_bug(X86_BUG_AMD_E400))
963e7ff3a47SThomas Gleixner 		return;
964e7ff3a47SThomas Gleixner 
965e7ff3a47SThomas Gleixner 	/*
966e7ff3a47SThomas Gleixner 	 * AMD E400 detection needs to happen after ACPI has been enabled. If
967e7ff3a47SThomas Gleixner 	 * the machine is affected K8_INTP_C1E_ACTIVE_MASK bits are set in
968e7ff3a47SThomas Gleixner 	 * MSR_K8_INT_PENDING_MSG.
969e7ff3a47SThomas Gleixner 	 */
970e7ff3a47SThomas Gleixner 	rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
971e7ff3a47SThomas Gleixner 	if (!(lo & K8_INTP_C1E_ACTIVE_MASK))
972e7ff3a47SThomas Gleixner 		return;
973e7ff3a47SThomas Gleixner 
974e7ff3a47SThomas Gleixner 	boot_cpu_set_bug(X86_BUG_AMD_APIC_C1E);
975e7ff3a47SThomas Gleixner 
976e7ff3a47SThomas Gleixner 	if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
977e7ff3a47SThomas Gleixner 		mark_tsc_unstable("TSC halt in AMD C1E");
978cb81deefSThomas Gleixner 
979cb81deefSThomas Gleixner 	if (IS_ENABLED(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST_IDLE))
980cb81deefSThomas Gleixner 		static_branch_enable(&arch_needs_tick_broadcast);
981cb81deefSThomas Gleixner 	pr_info("System has AMD C1E erratum E400. Workaround enabled.\n");
982e7ff3a47SThomas Gleixner }
983e7ff3a47SThomas Gleixner 
idle_setup(char * str)9847f424a8bSPeter Zijlstra static int __init idle_setup(char *str)
9857f424a8bSPeter Zijlstra {
986ab6bc3e3SCyrill Gorcunov 	if (!str)
987ab6bc3e3SCyrill Gorcunov 		return -EINVAL;
988ab6bc3e3SCyrill Gorcunov 
9897f424a8bSPeter Zijlstra 	if (!strcmp(str, "poll")) {
990c767a54bSJoe Perches 		pr_info("using polling idle threads\n");
991d1896049SThomas Renninger 		boot_option_idle_override = IDLE_POLL;
9927d1a9417SThomas Gleixner 		cpu_idle_poll_ctrl(true);
993d1896049SThomas Renninger 	} else if (!strcmp(str, "halt")) {
994f3d7eab7SThomas Gleixner 		/* 'idle=halt' HALT for idle. C-states are disabled. */
995d1896049SThomas Renninger 		boot_option_idle_override = IDLE_HALT;
996da5e09a1SZhao Yakui 	} else if (!strcmp(str, "nomwait")) {
997f3d7eab7SThomas Gleixner 		/* 'idle=nomwait' disables MWAIT for idle */
998d1896049SThomas Renninger 		boot_option_idle_override = IDLE_NOMWAIT;
999f3d7eab7SThomas Gleixner 	} else {
1000f3d7eab7SThomas Gleixner 		return -EINVAL;
1001f3d7eab7SThomas Gleixner 	}
10027f424a8bSPeter Zijlstra 
10037f424a8bSPeter Zijlstra 	return 0;
10047f424a8bSPeter Zijlstra }
10057f424a8bSPeter Zijlstra early_param("idle", idle_setup);
10067f424a8bSPeter Zijlstra 
arch_align_stack(unsigned long sp)10079d62dcdfSAmerigo Wang unsigned long arch_align_stack(unsigned long sp)
10089d62dcdfSAmerigo Wang {
10099d62dcdfSAmerigo Wang 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
10108032bf12SJason A. Donenfeld 		sp -= get_random_u32_below(8192);
10119d62dcdfSAmerigo Wang 	return sp & ~0xf;
10129d62dcdfSAmerigo Wang }
10139d62dcdfSAmerigo Wang 
arch_randomize_brk(struct mm_struct * mm)10149d62dcdfSAmerigo Wang unsigned long arch_randomize_brk(struct mm_struct *mm)
10159d62dcdfSAmerigo Wang {
101644c76825SKees Cook 	if (mmap_is_ia32())
101744c76825SKees Cook 		return randomize_page(mm->brk, SZ_32M);
101844c76825SKees Cook 
101944c76825SKees Cook 	return randomize_page(mm->brk, SZ_1G);
10209d62dcdfSAmerigo Wang }
10219d62dcdfSAmerigo Wang 
10227ba78053SThomas Gleixner /*
10237ba78053SThomas Gleixner  * Called from fs/proc with a reference on @p to find the function
10247ba78053SThomas Gleixner  * which called into schedule(). This needs to be done carefully
10257ba78053SThomas Gleixner  * because the task might wake up and we might look at a stack
10267ba78053SThomas Gleixner  * changing under us.
10277ba78053SThomas Gleixner  */
__get_wchan(struct task_struct * p)102842a20f86SKees Cook unsigned long __get_wchan(struct task_struct *p)
10297ba78053SThomas Gleixner {
10305d1ceb39SPeter Zijlstra 	struct unwind_state state;
10315d1ceb39SPeter Zijlstra 	unsigned long addr = 0;
10327ba78053SThomas Gleixner 
10330dc636b3SPeter Zijlstra 	if (!try_get_task_stack(p))
10340dc636b3SPeter Zijlstra 		return 0;
10350dc636b3SPeter Zijlstra 
10365d1ceb39SPeter Zijlstra 	for (unwind_start(&state, p, NULL, NULL); !unwind_done(&state);
10375d1ceb39SPeter Zijlstra 	     unwind_next_frame(&state)) {
10385d1ceb39SPeter Zijlstra 		addr = unwind_get_return_address(&state);
10395d1ceb39SPeter Zijlstra 		if (!addr)
10405d1ceb39SPeter Zijlstra 			break;
10415d1ceb39SPeter Zijlstra 		if (in_sched_functions(addr))
10425d1ceb39SPeter Zijlstra 			continue;
10435d1ceb39SPeter Zijlstra 		break;
10445d1ceb39SPeter Zijlstra 	}
10455d1ceb39SPeter Zijlstra 
10460dc636b3SPeter Zijlstra 	put_task_stack(p);
10470dc636b3SPeter Zijlstra 
10485d1ceb39SPeter Zijlstra 	return addr;
10497ba78053SThomas Gleixner }
1050b0b9b014SKyle Huey 
SYSCALL_DEFINE2(arch_prctl,int,option,unsigned long,arg2)10512df1ad0dSBrian Gerst SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2)
1052b0b9b014SKyle Huey {
1053e9ea1e7fSKyle Huey 	switch (option) {
1054e9ea1e7fSKyle Huey 	case ARCH_GET_CPUID:
1055e9ea1e7fSKyle Huey 		return get_cpuid_mode();
1056e9ea1e7fSKyle Huey 	case ARCH_SET_CPUID:
1057f5c0b4f3SThomas Gleixner 		return set_cpuid_mode(arg2);
1058db8268dfSChang S. Bae 	case ARCH_GET_XCOMP_SUPP:
1059db8268dfSChang S. Bae 	case ARCH_GET_XCOMP_PERM:
1060db8268dfSChang S. Bae 	case ARCH_REQ_XCOMP_PERM:
1061980fe2fdSThomas Gleixner 	case ARCH_GET_XCOMP_GUEST_PERM:
1062980fe2fdSThomas Gleixner 	case ARCH_REQ_XCOMP_GUEST_PERM:
1063f5c0b4f3SThomas Gleixner 		return fpu_xstate_prctl(option, arg2);
1064e9ea1e7fSKyle Huey 	}
1065e9ea1e7fSKyle Huey 
10662df1ad0dSBrian Gerst 	if (!in_ia32_syscall())
10672df1ad0dSBrian Gerst 		return do_arch_prctl_64(current, option, arg2);
10682df1ad0dSBrian Gerst 
1069b0b9b014SKyle Huey 	return -EINVAL;
1070b0b9b014SKyle Huey }
10719a93e29fSBrian Gerst 
SYSCALL_DEFINE0(ni_syscall)10729a93e29fSBrian Gerst SYSCALL_DEFINE0(ni_syscall)
10739a93e29fSBrian Gerst {
10749a93e29fSBrian Gerst 	return -ENOSYS;
10759a93e29fSBrian Gerst }
1076