877 lines · 21.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/errno.h>#include <linux/kernel.h>#include <linux/mm.h>#include <linux/smp.h>#include <linux/prctl.h>#include <linux/slab.h>#include <linux/sched.h>#include <linux/sched/idle.h>#include <linux/sched/debug.h>#include <linux/sched/task.h>#include <linux/sched/task_stack.h>#include <linux/init.h>#include <linux/export.h>#include <linux/pm.h>#include <linux/tick.h>#include <linux/random.h>#include <linux/user-return-notifier.h>#include <linux/dmi.h>#include <linux/utsname.h>#include <linux/stackprotector.h>#include <linux/cpuidle.h>#include <linux/acpi.h>#include <linux/elf-randomize.h>#include <trace/events/power.h>#include <linux/hw_breakpoint.h>#include <asm/cpu.h>#include <asm/apic.h>#include <asm/syscalls.h>#include <linux/uaccess.h>#include <asm/mwait.h>#include <asm/fpu/internal.h>#include <asm/debugreg.h>#include <asm/nmi.h>#include <asm/tlbflush.h>#include <asm/mce.h>#include <asm/vm86.h>#include <asm/switch_to.h>#include <asm/desc.h>#include <asm/prctl.h>#include <asm/spec-ctrl.h>#include <asm/proto.h> #include "process.h" /* * per-CPU TSS segments. Threads are completely 'soft' on Linux, * no more per-task TSS's. The TSS size is kept cacheline-aligned * so they are allowed to end up in the .data..cacheline_aligned * section. Since TSS's are completely CPU-local, we want them * on exact cacheline boundaries, to eliminate cacheline ping-pong. */__visible DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw) = {	.x86_tss = {		/*		 * .sp0 is only used when entering ring 0 from a lower		 * privilege level.  Since the init task never runs anything		 * but ring 0 code, there is no need for a valid value here.		 * Poison it.		 */		.sp0 = (1UL << (BITS_PER_LONG-1)) + 1, 		/*		 * .sp1 is cpu_current_top_of_stack.  The init task never		 * runs user code, but cpu_current_top_of_stack should still		 * be well defined before the first context switch.		 */		.sp1 = TOP_OF_INIT_STACK, #ifdef CONFIG_X86_32		.ss0 = __KERNEL_DS,		.ss1 = __KERNEL_CS,		.io_bitmap_base	= INVALID_IO_BITMAP_OFFSET,#endif	 },#ifdef CONFIG_X86_32	 /*	  * Note that the .io_bitmap member must be extra-big. This is because	  * the CPU will access an additional byte beyond the end of the IO	  * permission bitmap. The extra byte must be all 1 bits, and must	  * be within the limit.	  */	.io_bitmap		= { [0 ... IO_BITMAP_LONGS] = ~0 },#endif};EXPORT_PER_CPU_SYMBOL(cpu_tss_rw); DEFINE_PER_CPU(bool, __tss_limit_invalid);EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid); /* * this gets called so that we can store lazy state into memory and copy the * current task into the new thread. */int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src){	memcpy(dst, src, arch_task_struct_size);#ifdef CONFIG_VM86	dst->thread.vm86 = NULL;#endif 	return fpu__copy(dst, src);} /* * Free current thread data structures etc.. */void exit_thread(struct task_struct *tsk){	struct thread_struct *t = &tsk->thread;	unsigned long *bp = t->io_bitmap_ptr;	struct fpu *fpu = &t->fpu; 	if (bp) {		struct tss_struct *tss = &per_cpu(cpu_tss_rw, get_cpu()); 		t->io_bitmap_ptr = NULL;		clear_thread_flag(TIF_IO_BITMAP);		/*		 * Careful, clear this in the TSS too:		 */		memset(tss->io_bitmap, 0xff, t->io_bitmap_max);		t->io_bitmap_max = 0;		put_cpu();		kfree(bp);	} 	free_vm86(t); 	fpu__drop(fpu);} void flush_thread(void){	struct task_struct *tsk = current; 	flush_ptrace_hw_breakpoint(tsk);	memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array)); 	fpu__clear(&tsk->thread.fpu);} void disable_TSC(void){	preempt_disable();	if (!test_and_set_thread_flag(TIF_NOTSC))		/*		 * Must flip the CPU state synchronously with		 * TIF_NOTSC in the current running context.		 */		cr4_set_bits(X86_CR4_TSD);	preempt_enable();} static void enable_TSC(void){	preempt_disable();	if (test_and_clear_thread_flag(TIF_NOTSC))		/*		 * Must flip the CPU state synchronously with		 * TIF_NOTSC in the current running context.		 */		cr4_clear_bits(X86_CR4_TSD);	preempt_enable();} int get_tsc_mode(unsigned long adr){	unsigned int val; 	if (test_thread_flag(TIF_NOTSC))		val = PR_TSC_SIGSEGV;	else		val = PR_TSC_ENABLE; 	return put_user(val, (unsigned int __user *)adr);} int set_tsc_mode(unsigned int val){	if (val == PR_TSC_SIGSEGV)		disable_TSC();	else if (val == PR_TSC_ENABLE)		enable_TSC();	else		return -EINVAL; 	return 0;} DEFINE_PER_CPU(u64, msr_misc_features_shadow); static void set_cpuid_faulting(bool on){	u64 msrval; 	msrval = this_cpu_read(msr_misc_features_shadow);	msrval &= ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;	msrval |= (on << MSR_MISC_FEATURES_ENABLES_CPUID_FAULT_BIT);	this_cpu_write(msr_misc_features_shadow, msrval);	wrmsrl(MSR_MISC_FEATURES_ENABLES, msrval);} static void disable_cpuid(void){	preempt_disable();	if (!test_and_set_thread_flag(TIF_NOCPUID)) {		/*		 * Must flip the CPU state synchronously with		 * TIF_NOCPUID in the current running context.		 */		set_cpuid_faulting(true);	}	preempt_enable();} static void enable_cpuid(void){	preempt_disable();	if (test_and_clear_thread_flag(TIF_NOCPUID)) {		/*		 * Must flip the CPU state synchronously with		 * TIF_NOCPUID in the current running context.		 */		set_cpuid_faulting(false);	}	preempt_enable();} static int get_cpuid_mode(void){	return !test_thread_flag(TIF_NOCPUID);} static int set_cpuid_mode(struct task_struct *task, unsigned long cpuid_enabled){	if (!boot_cpu_has(X86_FEATURE_CPUID_FAULT))		return -ENODEV; 	if (cpuid_enabled)		enable_cpuid();	else		disable_cpuid(); 	return 0;} /* * Called immediately after a successful exec. */void arch_setup_new_exec(void){	/* If cpuid was previously disabled for this task, re-enable it. */	if (test_thread_flag(TIF_NOCPUID))		enable_cpuid(); 	/*	 * Don't inherit TIF_SSBD across exec boundary when	 * PR_SPEC_DISABLE_NOEXEC is used.	 */	if (test_thread_flag(TIF_SSBD) &&	    task_spec_ssb_noexec(current)) {		clear_thread_flag(TIF_SSBD);		task_clear_spec_ssb_disable(current);		task_clear_spec_ssb_noexec(current);		speculation_ctrl_update(task_thread_info(current)->flags);	}} static inline void switch_to_bitmap(struct thread_struct *prev,				    struct thread_struct *next,				    unsigned long tifp, unsigned long tifn){	struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw); 	if (tifn & _TIF_IO_BITMAP) {		/*		 * Copy the relevant range of the IO bitmap.		 * Normally this is 128 bytes or less:		 */		memcpy(tss->io_bitmap, next->io_bitmap_ptr,		       max(prev->io_bitmap_max, next->io_bitmap_max));		/*		 * Make sure that the TSS limit is correct for the CPU		 * to notice the IO bitmap.		 */		refresh_tss_limit();	} else if (tifp & _TIF_IO_BITMAP) {		/*		 * Clear any possible leftover bits:		 */		memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);	}} #ifdef CONFIG_SMP struct ssb_state {	struct ssb_state	*shared_state;	raw_spinlock_t		lock;	unsigned int		disable_state;	unsigned long		local_state;}; #define LSTATE_SSB	0 static DEFINE_PER_CPU(struct ssb_state, ssb_state); void speculative_store_bypass_ht_init(void){	struct ssb_state *st = this_cpu_ptr(&ssb_state);	unsigned int this_cpu = smp_processor_id();	unsigned int cpu; 	st->local_state = 0; 	/*	 * Shared state setup happens once on the first bringup	 * of the CPU. It's not destroyed on CPU hotunplug.	 */	if (st->shared_state)		return; 	raw_spin_lock_init(&st->lock); 	/*	 * Go over HT siblings and check whether one of them has set up the	 * shared state pointer already.	 */	for_each_cpu(cpu, topology_sibling_cpumask(this_cpu)) {		if (cpu == this_cpu)			continue; 		if (!per_cpu(ssb_state, cpu).shared_state)			continue; 		/* Link it to the state of the sibling: */		st->shared_state = per_cpu(ssb_state, cpu).shared_state;		return;	} 	/*	 * First HT sibling to come up on the core.  Link shared state of	 * the first HT sibling to itself. The siblings on the same core	 * which come up later will see the shared state pointer and link	 * themself to the state of this CPU.	 */	st->shared_state = st;} /* * Logic is: First HT sibling enables SSBD for both siblings in the core * and last sibling to disable it, disables it for the whole core. This how * MSR_SPEC_CTRL works in "hardware": * *  CORE_SPEC_CTRL = THREAD0_SPEC_CTRL | THREAD1_SPEC_CTRL */static __always_inline void amd_set_core_ssb_state(unsigned long tifn){	struct ssb_state *st = this_cpu_ptr(&ssb_state);	u64 msr = x86_amd_ls_cfg_base; 	if (!static_cpu_has(X86_FEATURE_ZEN)) {		msr |= ssbd_tif_to_amd_ls_cfg(tifn);		wrmsrl(MSR_AMD64_LS_CFG, msr);		return;	} 	if (tifn & _TIF_SSBD) {		/*		 * Since this can race with prctl(), block reentry on the		 * same CPU.		 */		if (__test_and_set_bit(LSTATE_SSB, &st->local_state))			return; 		msr |= x86_amd_ls_cfg_ssbd_mask; 		raw_spin_lock(&st->shared_state->lock);		/* First sibling enables SSBD: */		if (!st->shared_state->disable_state)			wrmsrl(MSR_AMD64_LS_CFG, msr);		st->shared_state->disable_state++;		raw_spin_unlock(&st->shared_state->lock);	} else {		if (!__test_and_clear_bit(LSTATE_SSB, &st->local_state))			return; 		raw_spin_lock(&st->shared_state->lock);		st->shared_state->disable_state--;		if (!st->shared_state->disable_state)			wrmsrl(MSR_AMD64_LS_CFG, msr);		raw_spin_unlock(&st->shared_state->lock);	}}#elsestatic __always_inline void amd_set_core_ssb_state(unsigned long tifn){	u64 msr = x86_amd_ls_cfg_base | ssbd_tif_to_amd_ls_cfg(tifn); 	wrmsrl(MSR_AMD64_LS_CFG, msr);}#endif static __always_inline void amd_set_ssb_virt_state(unsigned long tifn){	/*	 * SSBD has the same definition in SPEC_CTRL and VIRT_SPEC_CTRL,	 * so ssbd_tif_to_spec_ctrl() just works.	 */	wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, ssbd_tif_to_spec_ctrl(tifn));} /* * Update the MSRs managing speculation control, during context switch. * * tifp: Previous task's thread flags * tifn: Next task's thread flags */static __always_inline void __speculation_ctrl_update(unsigned long tifp,						      unsigned long tifn){	unsigned long tif_diff = tifp ^ tifn;	u64 msr = x86_spec_ctrl_base;	bool updmsr = false; 	lockdep_assert_irqs_disabled(); 	/*	 * If TIF_SSBD is different, select the proper mitigation	 * method. Note that if SSBD mitigation is disabled or permanentely	 * enabled this branch can't be taken because nothing can set	 * TIF_SSBD.	 */	if (tif_diff & _TIF_SSBD) {		if (static_cpu_has(X86_FEATURE_VIRT_SSBD)) {			amd_set_ssb_virt_state(tifn);		} else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {			amd_set_core_ssb_state(tifn);		} else if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||			   static_cpu_has(X86_FEATURE_AMD_SSBD)) {			msr |= ssbd_tif_to_spec_ctrl(tifn);			updmsr  = true;		}	} 	/*	 * Only evaluate TIF_SPEC_IB if conditional STIBP is enabled,	 * otherwise avoid the MSR write.	 */	if (IS_ENABLED(CONFIG_SMP) &&	    static_branch_unlikely(&switch_to_cond_stibp)) {		updmsr |= !!(tif_diff & _TIF_SPEC_IB);		msr |= stibp_tif_to_spec_ctrl(tifn);	} 	if (updmsr)		wrmsrl(MSR_IA32_SPEC_CTRL, msr);} static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk){	if (test_and_clear_tsk_thread_flag(tsk, TIF_SPEC_FORCE_UPDATE)) {		if (task_spec_ssb_disable(tsk))			set_tsk_thread_flag(tsk, TIF_SSBD);		else			clear_tsk_thread_flag(tsk, TIF_SSBD); 		if (task_spec_ib_disable(tsk))			set_tsk_thread_flag(tsk, TIF_SPEC_IB);		else			clear_tsk_thread_flag(tsk, TIF_SPEC_IB);	}	/* Return the updated threadinfo flags*/	return task_thread_info(tsk)->flags;} void speculation_ctrl_update(unsigned long tif){	unsigned long flags; 	/* Forced update. Make sure all relevant TIF flags are different */	local_irq_save(flags);	__speculation_ctrl_update(~tif, tif);	local_irq_restore(flags);} /* Called from seccomp/prctl update */void speculation_ctrl_update_current(void){	preempt_disable();	speculation_ctrl_update(speculation_ctrl_update_tif(current));	preempt_enable();} void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p){	struct thread_struct *prev, *next;

Lines 1–500 of 877.