xref: /linux-6.15/arch/x86/include/asm/processor.h (revision abc22418)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21965aae3SH. Peter Anvin #ifndef _ASM_X86_PROCESSOR_H
31965aae3SH. Peter Anvin #define _ASM_X86_PROCESSOR_H
4bb898558SAl Viro 
5bb898558SAl Viro #include <asm/processor-flags.h>
6bb898558SAl Viro 
7bb898558SAl Viro /* Forward declaration, a strange C thing */
8bb898558SAl Viro struct task_struct;
9bb898558SAl Viro struct mm_struct;
10577d5cd7SThomas Gleixner struct io_bitmap;
119fda6a06SBrian Gerst struct vm86;
12bb898558SAl Viro 
13bb898558SAl Viro #include <asm/math_emu.h>
14bb898558SAl Viro #include <asm/segment.h>
15bb898558SAl Viro #include <asm/types.h>
16decb4c41SIngo Molnar #include <uapi/asm/sigcontext.h>
17bb898558SAl Viro #include <asm/current.h>
18cd4d09ecSBorislav Petkov #include <asm/cpufeatures.h>
19bb898558SAl Viro #include <asm/page.h>
2054321d94SJeremy Fitzhardinge #include <asm/pgtable_types.h>
21bb898558SAl Viro #include <asm/percpu.h>
22bb898558SAl Viro #include <asm/msr.h>
23bb898558SAl Viro #include <asm/desc_defs.h>
24bb898558SAl Viro #include <asm/nops.h>
25f05e798aSDavid Howells #include <asm/special_insns.h>
2614b9675aSIngo Molnar #include <asm/fpu/types.h>
2776846bf3SJosh Poimboeuf #include <asm/unwind_hints.h>
2815934878SSean Christopherson #include <asm/vmxfeatures.h>
29*abc22418SVincenzo Frascino #include <asm/vdso/processor.h>
30bb898558SAl Viro 
31bb898558SAl Viro #include <linux/personality.h>
32bb898558SAl Viro #include <linux/cache.h>
33bb898558SAl Viro #include <linux/threads.h>
345cbc19a9SPeter Zijlstra #include <linux/math64.h>
35faa4602eSPeter Zijlstra #include <linux/err.h>
36f05e798aSDavid Howells #include <linux/irqflags.h>
3721729f81STom Lendacky #include <linux/mem_encrypt.h>
38f05e798aSDavid Howells 
39f05e798aSDavid Howells /*
40f05e798aSDavid Howells  * We handle most unaligned accesses in hardware.  On the other hand
41f05e798aSDavid Howells  * unaligned DMA can be quite expensive on some Nehalem processors.
42f05e798aSDavid Howells  *
43f05e798aSDavid Howells  * Based on this we disable the IP header alignment in network drivers.
44f05e798aSDavid Howells  */
45f05e798aSDavid Howells #define NET_IP_ALIGN	0
46bb898558SAl Viro 
47b332828cSK.Prasad #define HBP_NUM 4
48bb898558SAl Viro 
49b8c1b8eaSIngo Molnar /*
50b8c1b8eaSIngo Molnar  * These alignment constraints are for performance in the vSMP case,
51b8c1b8eaSIngo Molnar  * but in the task_struct case we must also meet hardware imposed
52b8c1b8eaSIngo Molnar  * alignment requirements of the FPU state:
53b8c1b8eaSIngo Molnar  */
54bb898558SAl Viro #ifdef CONFIG_X86_VSMP
55bb898558SAl Viro # define ARCH_MIN_TASKALIGN		(1 << INTERNODE_CACHE_SHIFT)
56bb898558SAl Viro # define ARCH_MIN_MMSTRUCT_ALIGN	(1 << INTERNODE_CACHE_SHIFT)
57bb898558SAl Viro #else
58b8c1b8eaSIngo Molnar # define ARCH_MIN_TASKALIGN		__alignof__(union fpregs_state)
59bb898558SAl Viro # define ARCH_MIN_MMSTRUCT_ALIGN	0
60bb898558SAl Viro #endif
61bb898558SAl Viro 
62e0ba94f1SAlex Shi enum tlb_infos {
63e0ba94f1SAlex Shi 	ENTRIES,
64e0ba94f1SAlex Shi 	NR_INFO
65e0ba94f1SAlex Shi };
66e0ba94f1SAlex Shi 
67e0ba94f1SAlex Shi extern u16 __read_mostly tlb_lli_4k[NR_INFO];
68e0ba94f1SAlex Shi extern u16 __read_mostly tlb_lli_2m[NR_INFO];
69e0ba94f1SAlex Shi extern u16 __read_mostly tlb_lli_4m[NR_INFO];
70e0ba94f1SAlex Shi extern u16 __read_mostly tlb_lld_4k[NR_INFO];
71e0ba94f1SAlex Shi extern u16 __read_mostly tlb_lld_2m[NR_INFO];
72e0ba94f1SAlex Shi extern u16 __read_mostly tlb_lld_4m[NR_INFO];
73dd360393SKirill A. Shutemov extern u16 __read_mostly tlb_lld_1g[NR_INFO];
74c4211f42SAlex Shi 
75bb898558SAl Viro /*
76bb898558SAl Viro  *  CPU type and hardware bug flags. Kept separately for each CPU.
7704402116SMathias Krause  *  Members of this structure are referenced in head_32.S, so think twice
78bb898558SAl Viro  *  before touching them. [mj]
79bb898558SAl Viro  */
80bb898558SAl Viro 
81bb898558SAl Viro struct cpuinfo_x86 {
82bb898558SAl Viro 	__u8			x86;		/* CPU family */
83bb898558SAl Viro 	__u8			x86_vendor;	/* CPU vendor */
84bb898558SAl Viro 	__u8			x86_model;
85b399151cSJia Zhang 	__u8			x86_stepping;
866415813bSMathias Krause #ifdef CONFIG_X86_64
87bb898558SAl Viro 	/* Number of 4K pages in DTLB/ITLB combined(in pages): */
88bb898558SAl Viro 	int			x86_tlbsize;
8913c6c532SJan Beulich #endif
90b47ce1feSSean Christopherson #ifdef CONFIG_X86_VMX_FEATURE_NAMES
91b47ce1feSSean Christopherson 	__u32			vmx_capability[NVMXINTS];
92b47ce1feSSean Christopherson #endif
93bb898558SAl Viro 	__u8			x86_virt_bits;
94bb898558SAl Viro 	__u8			x86_phys_bits;
95bb898558SAl Viro 	/* CPUID returned core id bits: */
96bb898558SAl Viro 	__u8			x86_coreid_bits;
9779a8b9aaSBorislav Petkov 	__u8			cu_id;
98bb898558SAl Viro 	/* Max extended CPUID function supported: */
99bb898558SAl Viro 	__u32			extended_cpuid_level;
100bb898558SAl Viro 	/* Maximum supported CPUID level, -1=no CPUID: */
101bb898558SAl Viro 	int			cpuid_level;
102db8c33f8SFenghua Yu 	/*
103db8c33f8SFenghua Yu 	 * Align to size of unsigned long because the x86_capability array
104db8c33f8SFenghua Yu 	 * is passed to bitops which require the alignment. Use unnamed
105db8c33f8SFenghua Yu 	 * union to enforce the array is aligned to size of unsigned long.
106db8c33f8SFenghua Yu 	 */
107db8c33f8SFenghua Yu 	union {
10865fc985bSBorislav Petkov 		__u32		x86_capability[NCAPINTS + NBUGINTS];
109db8c33f8SFenghua Yu 		unsigned long	x86_capability_alignment;
110db8c33f8SFenghua Yu 	};
111bb898558SAl Viro 	char			x86_vendor_id[16];
112bb898558SAl Viro 	char			x86_model_id[64];
113bb898558SAl Viro 	/* in KB - valid for CPUS which support this call: */
11424dbc600SGustavo A. R. Silva 	unsigned int		x86_cache_size;
115bb898558SAl Viro 	int			x86_cache_alignment;	/* In bytes */
116cbc82b17SPeter P Waskiewicz Jr 	/* Cache QoS architectural values: */
117cbc82b17SPeter P Waskiewicz Jr 	int			x86_cache_max_rmid;	/* max index */
118cbc82b17SPeter P Waskiewicz Jr 	int			x86_cache_occ_scale;	/* scale to bytes */
119bb898558SAl Viro 	int			x86_power;
120bb898558SAl Viro 	unsigned long		loops_per_jiffy;
121bb898558SAl Viro 	/* cpuid returned max cores value: */
122bb898558SAl Viro 	u16			x86_max_cores;
123bb898558SAl Viro 	u16			apicid;
124bb898558SAl Viro 	u16			initial_apicid;
125bb898558SAl Viro 	u16			x86_clflush_size;
126bb898558SAl Viro 	/* number of cores as seen by the OS: */
127bb898558SAl Viro 	u16			booted_cores;
128bb898558SAl Viro 	/* Physical processor id: */
129bb898558SAl Viro 	u16			phys_proc_id;
1301f12e32fSThomas Gleixner 	/* Logical processor id: */
1311f12e32fSThomas Gleixner 	u16			logical_proc_id;
132bb898558SAl Viro 	/* Core id: */
133bb898558SAl Viro 	u16			cpu_core_id;
1347745f03eSLen Brown 	u16			cpu_die_id;
135212bf4fdSLen Brown 	u16			logical_die_id;
136bb898558SAl Viro 	/* Index into per_cpu list: */
137bb898558SAl Viro 	u16			cpu_index;
138506ed6b5SAndi Kleen 	u32			microcode;
139cc51e542SAndi Kleen 	/* Address space bits used by the cache internally */
140cc51e542SAndi Kleen 	u8			x86_cache_bits;
14130bb9811SAndi Kleen 	unsigned		initialized : 1;
1423859a271SKees Cook } __randomize_layout;
143bb898558SAl Viro 
14447f10a36SHe Chen struct cpuid_regs {
14547f10a36SHe Chen 	u32 eax, ebx, ecx, edx;
14647f10a36SHe Chen };
14747f10a36SHe Chen 
14847f10a36SHe Chen enum cpuid_regs_idx {
14947f10a36SHe Chen 	CPUID_EAX = 0,
15047f10a36SHe Chen 	CPUID_EBX,
15147f10a36SHe Chen 	CPUID_ECX,
15247f10a36SHe Chen 	CPUID_EDX,
15347f10a36SHe Chen };
15447f10a36SHe Chen 
155bb898558SAl Viro #define X86_VENDOR_INTEL	0
156bb898558SAl Viro #define X86_VENDOR_CYRIX	1
157bb898558SAl Viro #define X86_VENDOR_AMD		2
158bb898558SAl Viro #define X86_VENDOR_UMC		3
159bb898558SAl Viro #define X86_VENDOR_CENTAUR	5
160bb898558SAl Viro #define X86_VENDOR_TRANSMETA	7
161bb898558SAl Viro #define X86_VENDOR_NSC		8
162c9661c1eSPu Wen #define X86_VENDOR_HYGON	9
163761fdd5eSTony W Wang-oc #define X86_VENDOR_ZHAOXIN	10
164761fdd5eSTony W Wang-oc #define X86_VENDOR_NUM		11
165bb898558SAl Viro 
166bb898558SAl Viro #define X86_VENDOR_UNKNOWN	0xff
167bb898558SAl Viro 
168bb898558SAl Viro /*
169bb898558SAl Viro  * capabilities of CPUs
170bb898558SAl Viro  */
171bb898558SAl Viro extern struct cpuinfo_x86	boot_cpu_data;
172bb898558SAl Viro extern struct cpuinfo_x86	new_cpu_data;
173bb898558SAl Viro 
1746cbd2171SThomas Gleixner extern __u32			cpu_caps_cleared[NCAPINTS + NBUGINTS];
1756cbd2171SThomas Gleixner extern __u32			cpu_caps_set[NCAPINTS + NBUGINTS];
176bb898558SAl Viro 
177bb898558SAl Viro #ifdef CONFIG_SMP
1782c773dd3SJan Beulich DECLARE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
179bb898558SAl Viro #define cpu_data(cpu)		per_cpu(cpu_info, cpu)
180bb898558SAl Viro #else
1817b543a53STejun Heo #define cpu_info		boot_cpu_data
182bb898558SAl Viro #define cpu_data(cpu)		boot_cpu_data
183bb898558SAl Viro #endif
184bb898558SAl Viro 
185bb898558SAl Viro extern const struct seq_operations cpuinfo_op;
186bb898558SAl Viro 
187bb898558SAl Viro #define cache_line_size()	(boot_cpu_data.x86_cache_alignment)
188bb898558SAl Viro 
189bb898558SAl Viro extern void cpu_detect(struct cpuinfo_x86 *c);
190bb898558SAl Viro 
1919df95169SVlastimil Babka static inline unsigned long long l1tf_pfn_limit(void)
19217dbca11SAndi Kleen {
193cc51e542SAndi Kleen 	return BIT_ULL(boot_cpu_data.x86_cache_bits - 1 - PAGE_SHIFT);
19417dbca11SAndi Kleen }
19517dbca11SAndi Kleen 
196bb898558SAl Viro extern void early_cpu_init(void);
197bb898558SAl Viro extern void identify_boot_cpu(void);
198bb898558SAl Viro extern void identify_secondary_cpu(struct cpuinfo_x86 *);
199bb898558SAl Viro extern void print_cpu_info(struct cpuinfo_x86 *);
20021c3fcf3SYinghai Lu void print_cpu_msr(struct cpuinfo_x86 *);
201bb898558SAl Viro 
202d288e1cfSFenghua Yu #ifdef CONFIG_X86_32
203d288e1cfSFenghua Yu extern int have_cpuid_p(void);
204d288e1cfSFenghua Yu #else
205d288e1cfSFenghua Yu static inline int have_cpuid_p(void)
206d288e1cfSFenghua Yu {
207d288e1cfSFenghua Yu 	return 1;
208d288e1cfSFenghua Yu }
209d288e1cfSFenghua Yu #endif
210bb898558SAl Viro static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
211bb898558SAl Viro 				unsigned int *ecx, unsigned int *edx)
212bb898558SAl Viro {
213bb898558SAl Viro 	/* ecx is often an input as well as an output. */
21445a94d7cSSuresh Siddha 	asm volatile("cpuid"
215bb898558SAl Viro 	    : "=a" (*eax),
216bb898558SAl Viro 	      "=b" (*ebx),
217bb898558SAl Viro 	      "=c" (*ecx),
218bb898558SAl Viro 	      "=d" (*edx)
219506ed6b5SAndi Kleen 	    : "0" (*eax), "2" (*ecx)
220506ed6b5SAndi Kleen 	    : "memory");
221bb898558SAl Viro }
222bb898558SAl Viro 
2235dedade6SBorislav Petkov #define native_cpuid_reg(reg)					\
2245dedade6SBorislav Petkov static inline unsigned int native_cpuid_##reg(unsigned int op)	\
2255dedade6SBorislav Petkov {								\
2265dedade6SBorislav Petkov 	unsigned int eax = op, ebx, ecx = 0, edx;		\
2275dedade6SBorislav Petkov 								\
2285dedade6SBorislav Petkov 	native_cpuid(&eax, &ebx, &ecx, &edx);			\
2295dedade6SBorislav Petkov 								\
2305dedade6SBorislav Petkov 	return reg;						\
2315dedade6SBorislav Petkov }
2325dedade6SBorislav Petkov 
2335dedade6SBorislav Petkov /*
2345dedade6SBorislav Petkov  * Native CPUID functions returning a single datum.
2355dedade6SBorislav Petkov  */
2365dedade6SBorislav Petkov native_cpuid_reg(eax)
2375dedade6SBorislav Petkov native_cpuid_reg(ebx)
2385dedade6SBorislav Petkov native_cpuid_reg(ecx)
2395dedade6SBorislav Petkov native_cpuid_reg(edx)
2405dedade6SBorislav Petkov 
2416c690ee1SAndy Lutomirski /*
2426c690ee1SAndy Lutomirski  * Friendlier CR3 helpers.
2436c690ee1SAndy Lutomirski  */
2446c690ee1SAndy Lutomirski static inline unsigned long read_cr3_pa(void)
2456c690ee1SAndy Lutomirski {
2466c690ee1SAndy Lutomirski 	return __read_cr3() & CR3_ADDR_MASK;
2476c690ee1SAndy Lutomirski }
2486c690ee1SAndy Lutomirski 
249eef9c4abSTom Lendacky static inline unsigned long native_read_cr3_pa(void)
250eef9c4abSTom Lendacky {
251eef9c4abSTom Lendacky 	return __native_read_cr3() & CR3_ADDR_MASK;
252eef9c4abSTom Lendacky }
253eef9c4abSTom Lendacky 
254bb898558SAl Viro static inline void load_cr3(pgd_t *pgdir)
255bb898558SAl Viro {
25621729f81STom Lendacky 	write_cr3(__sme_pa(pgdir));
257bb898558SAl Viro }
258bb898558SAl Viro 
2597fb983b4SAndy Lutomirski /*
2607fb983b4SAndy Lutomirski  * Note that while the legacy 'TSS' name comes from 'Task State Segment',
2617fb983b4SAndy Lutomirski  * on modern x86 CPUs the TSS also holds information important to 64-bit mode,
2627fb983b4SAndy Lutomirski  * unrelated to the task-switch mechanism:
2637fb983b4SAndy Lutomirski  */
264bb898558SAl Viro #ifdef CONFIG_X86_32
265bb898558SAl Viro /* This is the TSS defined by the hardware. */
266bb898558SAl Viro struct x86_hw_tss {
267bb898558SAl Viro 	unsigned short		back_link, __blh;
268bb898558SAl Viro 	unsigned long		sp0;
269bb898558SAl Viro 	unsigned short		ss0, __ss0h;
270cf9328ccSAndy Lutomirski 	unsigned long		sp1;
27176e4c490SAndy Lutomirski 
27276e4c490SAndy Lutomirski 	/*
273cf9328ccSAndy Lutomirski 	 * We don't use ring 1, so ss1 is a convenient scratch space in
274cf9328ccSAndy Lutomirski 	 * the same cacheline as sp0.  We use ss1 to cache the value in
275cf9328ccSAndy Lutomirski 	 * MSR_IA32_SYSENTER_CS.  When we context switch
276cf9328ccSAndy Lutomirski 	 * MSR_IA32_SYSENTER_CS, we first check if the new value being
277cf9328ccSAndy Lutomirski 	 * written matches ss1, and, if it's not, then we wrmsr the new
278cf9328ccSAndy Lutomirski 	 * value and update ss1.
27976e4c490SAndy Lutomirski 	 *
280cf9328ccSAndy Lutomirski 	 * The only reason we context switch MSR_IA32_SYSENTER_CS is
281cf9328ccSAndy Lutomirski 	 * that we set it to zero in vm86 tasks to avoid corrupting the
282cf9328ccSAndy Lutomirski 	 * stack if we were to go through the sysenter path from vm86
283cf9328ccSAndy Lutomirski 	 * mode.
28476e4c490SAndy Lutomirski 	 */
28576e4c490SAndy Lutomirski 	unsigned short		ss1;	/* MSR_IA32_SYSENTER_CS */
28676e4c490SAndy Lutomirski 
28776e4c490SAndy Lutomirski 	unsigned short		__ss1h;
288bb898558SAl Viro 	unsigned long		sp2;
289bb898558SAl Viro 	unsigned short		ss2, __ss2h;
290bb898558SAl Viro 	unsigned long		__cr3;
291bb898558SAl Viro 	unsigned long		ip;
292bb898558SAl Viro 	unsigned long		flags;
293bb898558SAl Viro 	unsigned long		ax;
294bb898558SAl Viro 	unsigned long		cx;
295bb898558SAl Viro 	unsigned long		dx;
296bb898558SAl Viro 	unsigned long		bx;
297bb898558SAl Viro 	unsigned long		sp;
298bb898558SAl Viro 	unsigned long		bp;
299bb898558SAl Viro 	unsigned long		si;
300bb898558SAl Viro 	unsigned long		di;
301bb898558SAl Viro 	unsigned short		es, __esh;
302bb898558SAl Viro 	unsigned short		cs, __csh;
303bb898558SAl Viro 	unsigned short		ss, __ssh;
304bb898558SAl Viro 	unsigned short		ds, __dsh;
305bb898558SAl Viro 	unsigned short		fs, __fsh;
306bb898558SAl Viro 	unsigned short		gs, __gsh;
307bb898558SAl Viro 	unsigned short		ldt, __ldth;
308bb898558SAl Viro 	unsigned short		trace;
309bb898558SAl Viro 	unsigned short		io_bitmap_base;
310bb898558SAl Viro 
311bb898558SAl Viro } __attribute__((packed));
312bb898558SAl Viro #else
313bb898558SAl Viro struct x86_hw_tss {
314bb898558SAl Viro 	u32			reserved1;
315bb898558SAl Viro 	u64			sp0;
3169aaefe7bSAndy Lutomirski 
3179aaefe7bSAndy Lutomirski 	/*
3189aaefe7bSAndy Lutomirski 	 * We store cpu_current_top_of_stack in sp1 so it's always accessible.
3199aaefe7bSAndy Lutomirski 	 * Linux does not use ring 1, so sp1 is not otherwise needed.
3209aaefe7bSAndy Lutomirski 	 */
321bb898558SAl Viro 	u64			sp1;
3229aaefe7bSAndy Lutomirski 
32398f05b51SAndy Lutomirski 	/*
32498f05b51SAndy Lutomirski 	 * Since Linux does not use ring 2, the 'sp2' slot is unused by
32598f05b51SAndy Lutomirski 	 * hardware.  entry_SYSCALL_64 uses it as scratch space to stash
32698f05b51SAndy Lutomirski 	 * the user RSP value.
32798f05b51SAndy Lutomirski 	 */
328bb898558SAl Viro 	u64			sp2;
32998f05b51SAndy Lutomirski 
330bb898558SAl Viro 	u64			reserved2;
331bb898558SAl Viro 	u64			ist[7];
332bb898558SAl Viro 	u32			reserved3;
333bb898558SAl Viro 	u32			reserved4;
334bb898558SAl Viro 	u16			reserved5;
335bb898558SAl Viro 	u16			io_bitmap_base;
336bb898558SAl Viro 
337d3273deaSAndy Lutomirski } __attribute__((packed));
338bb898558SAl Viro #endif
339bb898558SAl Viro 
340bb898558SAl Viro /*
341bb898558SAl Viro  * IO-bitmap sizes:
342bb898558SAl Viro  */
343bb898558SAl Viro #define IO_BITMAP_BITS			65536
344f5848e5fSThomas Gleixner #define IO_BITMAP_BYTES			(IO_BITMAP_BITS / BITS_PER_BYTE)
345bb898558SAl Viro #define IO_BITMAP_LONGS			(IO_BITMAP_BYTES / sizeof(long))
346ecc7e37dSThomas Gleixner 
347c8137aceSThomas Gleixner #define IO_BITMAP_OFFSET_VALID_MAP				\
348f5848e5fSThomas Gleixner 	(offsetof(struct tss_struct, io_bitmap.bitmap) -	\
349ecc7e37dSThomas Gleixner 	 offsetof(struct tss_struct, x86_tss))
350ecc7e37dSThomas Gleixner 
351c8137aceSThomas Gleixner #define IO_BITMAP_OFFSET_VALID_ALL				\
352c8137aceSThomas Gleixner 	(offsetof(struct tss_struct, io_bitmap.mapall) -	\
353c8137aceSThomas Gleixner 	 offsetof(struct tss_struct, x86_tss))
354c8137aceSThomas Gleixner 
355111e7b15SThomas Gleixner #ifdef CONFIG_X86_IOPL_IOPERM
356ecc7e37dSThomas Gleixner /*
357c8137aceSThomas Gleixner  * sizeof(unsigned long) coming from an extra "long" at the end of the
358c8137aceSThomas Gleixner  * iobitmap. The limit is inclusive, i.e. the last valid byte.
359ecc7e37dSThomas Gleixner  */
360ecc7e37dSThomas Gleixner # define __KERNEL_TSS_LIMIT	\
361c8137aceSThomas Gleixner 	(IO_BITMAP_OFFSET_VALID_ALL + IO_BITMAP_BYTES + \
362c8137aceSThomas Gleixner 	 sizeof(unsigned long) - 1)
363111e7b15SThomas Gleixner #else
364111e7b15SThomas Gleixner # define __KERNEL_TSS_LIMIT	\
365111e7b15SThomas Gleixner 	(offsetof(struct tss_struct, x86_tss) + sizeof(struct x86_hw_tss) - 1)
366111e7b15SThomas Gleixner #endif
367ecc7e37dSThomas Gleixner 
368ecc7e37dSThomas Gleixner /* Base offset outside of TSS_LIMIT so unpriviledged IO causes #GP */
369ecc7e37dSThomas Gleixner #define IO_BITMAP_OFFSET_INVALID	(__KERNEL_TSS_LIMIT + 1)
370bb898558SAl Viro 
3714fe2d8b1SDave Hansen struct entry_stack {
3720f9a4810SAndy Lutomirski 	unsigned long		words[64];
3730f9a4810SAndy Lutomirski };
3740f9a4810SAndy Lutomirski 
3754fe2d8b1SDave Hansen struct entry_stack_page {
3764fe2d8b1SDave Hansen 	struct entry_stack stack;
377c482feefSAndy Lutomirski } __aligned(PAGE_SIZE);
3781a935bc3SAndy Lutomirski 
379f5848e5fSThomas Gleixner /*
380f5848e5fSThomas Gleixner  * All IO bitmap related data stored in the TSS:
381f5848e5fSThomas Gleixner  */
382f5848e5fSThomas Gleixner struct x86_io_bitmap {
383060aa16fSThomas Gleixner 	/* The sequence number of the last active bitmap. */
384060aa16fSThomas Gleixner 	u64			prev_sequence;
385060aa16fSThomas Gleixner 
386f5848e5fSThomas Gleixner 	/*
387f5848e5fSThomas Gleixner 	 * Store the dirty size of the last io bitmap offender. The next
388f5848e5fSThomas Gleixner 	 * one will have to do the cleanup as the switch out to a non io
389f5848e5fSThomas Gleixner 	 * bitmap user will just set x86_tss.io_bitmap_base to a value
390f5848e5fSThomas Gleixner 	 * outside of the TSS limit. So for sane tasks there is no need to
391f5848e5fSThomas Gleixner 	 * actually touch the io_bitmap at all.
392f5848e5fSThomas Gleixner 	 */
393f5848e5fSThomas Gleixner 	unsigned int		prev_max;
394f5848e5fSThomas Gleixner 
395f5848e5fSThomas Gleixner 	/*
396f5848e5fSThomas Gleixner 	 * The extra 1 is there because the CPU will access an
397f5848e5fSThomas Gleixner 	 * additional byte beyond the end of the IO permission
398f5848e5fSThomas Gleixner 	 * bitmap. The extra byte must be all 1 bits, and must
399f5848e5fSThomas Gleixner 	 * be within the limit.
400f5848e5fSThomas Gleixner 	 */
401f5848e5fSThomas Gleixner 	unsigned long		bitmap[IO_BITMAP_LONGS + 1];
402c8137aceSThomas Gleixner 
403c8137aceSThomas Gleixner 	/*
404c8137aceSThomas Gleixner 	 * Special I/O bitmap to emulate IOPL(3). All bytes zero,
405c8137aceSThomas Gleixner 	 * except the additional byte at the end.
406c8137aceSThomas Gleixner 	 */
407c8137aceSThomas Gleixner 	unsigned long		mapall[IO_BITMAP_LONGS + 1];
408f5848e5fSThomas Gleixner };
409f5848e5fSThomas Gleixner 
410bb898558SAl Viro struct tss_struct {
411bb898558SAl Viro 	/*
4121a935bc3SAndy Lutomirski 	 * The fixed hardware portion.  This must not cross a page boundary
4131a935bc3SAndy Lutomirski 	 * at risk of violating the SDM's advice and potentially triggering
4141a935bc3SAndy Lutomirski 	 * errata.
415bb898558SAl Viro 	 */
416bb898558SAl Viro 	struct x86_hw_tss	x86_tss;
417bb898558SAl Viro 
418f5848e5fSThomas Gleixner 	struct x86_io_bitmap	io_bitmap;
4191a935bc3SAndy Lutomirski } __aligned(PAGE_SIZE);
420bb898558SAl Viro 
421c482feefSAndy Lutomirski DECLARE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw);
422bb898558SAl Viro 
423e6401c13SAndy Lutomirski /* Per CPU interrupt stacks */
424e6401c13SAndy Lutomirski struct irq_stack {
425e6401c13SAndy Lutomirski 	char		stack[IRQ_STACK_SIZE];
426e6401c13SAndy Lutomirski } __aligned(IRQ_STACK_SIZE);
427e6401c13SAndy Lutomirski 
428e6401c13SAndy Lutomirski DECLARE_PER_CPU(struct irq_stack *, hardirq_stack_ptr);
429e6401c13SAndy Lutomirski 
430a7fcf28dSAndy Lutomirski #ifdef CONFIG_X86_32
431a7fcf28dSAndy Lutomirski DECLARE_PER_CPU(unsigned long, cpu_current_top_of_stack);
4329aaefe7bSAndy Lutomirski #else
433c482feefSAndy Lutomirski /* The RO copy can't be accessed with this_cpu_xyz(), so use the RW copy. */
434c482feefSAndy Lutomirski #define cpu_current_top_of_stack cpu_tss_rw.x86_tss.sp1
435a7fcf28dSAndy Lutomirski #endif
436a7fcf28dSAndy Lutomirski 
437bb898558SAl Viro #ifdef CONFIG_X86_64
438e6401c13SAndy Lutomirski struct fixed_percpu_data {
439947e76cdSBrian Gerst 	/*
440947e76cdSBrian Gerst 	 * GCC hardcodes the stack canary as %gs:40.  Since the
441947e76cdSBrian Gerst 	 * irq_stack is the object at %gs:0, we reserve the bottom
442947e76cdSBrian Gerst 	 * 48 bytes of the irq stack for the canary.
443947e76cdSBrian Gerst 	 */
444947e76cdSBrian Gerst 	char		gs_base[40];
445947e76cdSBrian Gerst 	unsigned long	stack_canary;
446947e76cdSBrian Gerst };
447947e76cdSBrian Gerst 
448e6401c13SAndy Lutomirski DECLARE_PER_CPU_FIRST(struct fixed_percpu_data, fixed_percpu_data) __visible;
449e6401c13SAndy Lutomirski DECLARE_INIT_PER_CPU(fixed_percpu_data);
4502add8e23SBrian Gerst 
45135060ed6SVitaly Kuznetsov static inline unsigned long cpu_kernelmode_gs_base(int cpu)
45235060ed6SVitaly Kuznetsov {
453e6401c13SAndy Lutomirski 	return (unsigned long)per_cpu(fixed_percpu_data.gs_base, cpu);
45435060ed6SVitaly Kuznetsov }
45535060ed6SVitaly Kuznetsov 
4569766cdbcSJaswinder Singh Rajput DECLARE_PER_CPU(unsigned int, irq_count);
4579766cdbcSJaswinder Singh Rajput extern asmlinkage void ignore_sysret(void);
45842b933b5SVitaly Kuznetsov 
45942b933b5SVitaly Kuznetsov #if IS_ENABLED(CONFIG_KVM)
46042b933b5SVitaly Kuznetsov /* Save actual FS/GS selectors and bases to current->thread */
46142b933b5SVitaly Kuznetsov void save_fsgs_for_kvm(void);
46242b933b5SVitaly Kuznetsov #endif
46360a5317fSTejun Heo #else	/* X86_64 */
464050e9baaSLinus Torvalds #ifdef CONFIG_STACKPROTECTOR
4651ea0d14eSJeremy Fitzhardinge /*
4661ea0d14eSJeremy Fitzhardinge  * Make sure stack canary segment base is cached-aligned:
4671ea0d14eSJeremy Fitzhardinge  *   "For Intel Atom processors, avoid non zero segment base address
4681ea0d14eSJeremy Fitzhardinge  *    that is not aligned to cache line boundary at all cost."
4691ea0d14eSJeremy Fitzhardinge  * (Optim Ref Manual Assembly/Compiler Coding Rule 15.)
4701ea0d14eSJeremy Fitzhardinge  */
4711ea0d14eSJeremy Fitzhardinge struct stack_canary {
4721ea0d14eSJeremy Fitzhardinge 	char __pad[20];		/* canary at %gs:20 */
4731ea0d14eSJeremy Fitzhardinge 	unsigned long canary;
4741ea0d14eSJeremy Fitzhardinge };
47553f82452SJeremy Fitzhardinge DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary);
476bb898558SAl Viro #endif
477e6401c13SAndy Lutomirski /* Per CPU softirq stack pointer */
478a754fe2bSThomas Gleixner DECLARE_PER_CPU(struct irq_stack *, softirq_stack_ptr);
47960a5317fSTejun Heo #endif	/* X86_64 */
480bb898558SAl Viro 
481bf15a8cfSFenghua Yu extern unsigned int fpu_kernel_xstate_size;
482a1141e0bSFenghua Yu extern unsigned int fpu_user_xstate_size;
483bb898558SAl Viro 
48424f1e32cSFrederic Weisbecker struct perf_event;
48524f1e32cSFrederic Weisbecker 
48613d4ea09SAndy Lutomirski typedef struct {
48713d4ea09SAndy Lutomirski 	unsigned long		seg;
48813d4ea09SAndy Lutomirski } mm_segment_t;
48913d4ea09SAndy Lutomirski 
490bb898558SAl Viro struct thread_struct {
491bb898558SAl Viro 	/* Cached TLS descriptors: */
492bb898558SAl Viro 	struct desc_struct	tls_array[GDT_ENTRY_TLS_ENTRIES];
493d375cf15SAndy Lutomirski #ifdef CONFIG_X86_32
494bb898558SAl Viro 	unsigned long		sp0;
495d375cf15SAndy Lutomirski #endif
496bb898558SAl Viro 	unsigned long		sp;
497bb898558SAl Viro #ifdef CONFIG_X86_32
498bb898558SAl Viro 	unsigned long		sysenter_cs;
499bb898558SAl Viro #else
500bb898558SAl Viro 	unsigned short		es;
501bb898558SAl Viro 	unsigned short		ds;
502bb898558SAl Viro 	unsigned short		fsindex;
503bb898558SAl Viro 	unsigned short		gsindex;
504bb898558SAl Viro #endif
505b9d989c7SAndy Lutomirski 
506d756f4adSAlexey Dobriyan #ifdef CONFIG_X86_64
507296f781aSAndy Lutomirski 	unsigned long		fsbase;
508296f781aSAndy Lutomirski 	unsigned long		gsbase;
509296f781aSAndy Lutomirski #else
510296f781aSAndy Lutomirski 	/*
511296f781aSAndy Lutomirski 	 * XXX: this could presumably be unsigned short.  Alternatively,
512296f781aSAndy Lutomirski 	 * 32-bit kernels could be taught to use fsindex instead.
513296f781aSAndy Lutomirski 	 */
514bb898558SAl Viro 	unsigned long fs;
515bb898558SAl Viro 	unsigned long gs;
516296f781aSAndy Lutomirski #endif
517c5bedc68SIngo Molnar 
51824f1e32cSFrederic Weisbecker 	/* Save middle states of ptrace breakpoints */
51924f1e32cSFrederic Weisbecker 	struct perf_event	*ptrace_bps[HBP_NUM];
52024f1e32cSFrederic Weisbecker 	/* Debug status used for traps, single steps, etc... */
521bb898558SAl Viro 	unsigned long           debugreg6;
522326264a0SFrederic Weisbecker 	/* Keep track of the exact dr7 value set by the user */
523326264a0SFrederic Weisbecker 	unsigned long           ptrace_dr7;
524bb898558SAl Viro 	/* Fault info: */
525bb898558SAl Viro 	unsigned long		cr2;
52651e7dc70SSrikar Dronamraju 	unsigned long		trap_nr;
527bb898558SAl Viro 	unsigned long		error_code;
5289fda6a06SBrian Gerst #ifdef CONFIG_VM86
529bb898558SAl Viro 	/* Virtual 86 mode info */
5309fda6a06SBrian Gerst 	struct vm86		*vm86;
531bb898558SAl Viro #endif
532bb898558SAl Viro 	/* IO permissions: */
533577d5cd7SThomas Gleixner 	struct io_bitmap	*io_bitmap;
534c8137aceSThomas Gleixner 
535c8137aceSThomas Gleixner 	/*
536a24ca997SThomas Gleixner 	 * IOPL. Priviledge level dependent I/O permission which is
537a24ca997SThomas Gleixner 	 * emulated via the I/O bitmap to prevent user space from disabling
538a24ca997SThomas Gleixner 	 * interrupts.
539c8137aceSThomas Gleixner 	 */
540c8137aceSThomas Gleixner 	unsigned long		iopl_emul;
5410c8c0f03SDave Hansen 
54213d4ea09SAndy Lutomirski 	mm_segment_t		addr_limit;
54313d4ea09SAndy Lutomirski 
5442a53ccbcSIngo Molnar 	unsigned int		sig_on_uaccess_err:1;
545dfa9a942SAndy Lutomirski 	unsigned int		uaccess_err:1;	/* uaccess failed */
546dfa9a942SAndy Lutomirski 
5470c8c0f03SDave Hansen 	/* Floating point and extended processor state */
5480c8c0f03SDave Hansen 	struct fpu		fpu;
5490c8c0f03SDave Hansen 	/*
5500c8c0f03SDave Hansen 	 * WARNING: 'fpu' is dynamically-sized.  It *MUST* be at
5510c8c0f03SDave Hansen 	 * the end.
5520c8c0f03SDave Hansen 	 */
553bb898558SAl Viro };
554bb898558SAl Viro 
555f7d83c1cSKees Cook /* Whitelist the FPU state from the task_struct for hardened usercopy. */
556f7d83c1cSKees Cook static inline void arch_thread_struct_whitelist(unsigned long *offset,
557f7d83c1cSKees Cook 						unsigned long *size)
558f7d83c1cSKees Cook {
559f7d83c1cSKees Cook 	*offset = offsetof(struct thread_struct, fpu.state);
560f7d83c1cSKees Cook 	*size = fpu_kernel_xstate_size;
561f7d83c1cSKees Cook }
562f7d83c1cSKees Cook 
563bb898558SAl Viro /*
564b9d989c7SAndy Lutomirski  * Thread-synchronous status.
565b9d989c7SAndy Lutomirski  *
566b9d989c7SAndy Lutomirski  * This is different from the flags in that nobody else
567b9d989c7SAndy Lutomirski  * ever touches our thread-synchronous status, so we don't
568b9d989c7SAndy Lutomirski  * have to worry about atomic accesses.
569b9d989c7SAndy Lutomirski  */
570b9d989c7SAndy Lutomirski #define TS_COMPAT		0x0002	/* 32bit syscall active (64BIT)*/
571b9d989c7SAndy Lutomirski 
572bb898558SAl Viro static inline void
573da51da18SAndy Lutomirski native_load_sp0(unsigned long sp0)
574bb898558SAl Viro {
575c482feefSAndy Lutomirski 	this_cpu_write(cpu_tss_rw.x86_tss.sp0, sp0);
576bb898558SAl Viro }
577bb898558SAl Viro 
578bb898558SAl Viro static inline void native_swapgs(void)
579bb898558SAl Viro {
580bb898558SAl Viro #ifdef CONFIG_X86_64
581bb898558SAl Viro 	asm volatile("swapgs" ::: "memory");
582bb898558SAl Viro #endif
583bb898558SAl Viro }
584bb898558SAl Viro 
585a7fcf28dSAndy Lutomirski static inline unsigned long current_top_of_stack(void)
5868ef46a67SAndy Lutomirski {
5879aaefe7bSAndy Lutomirski 	/*
5889aaefe7bSAndy Lutomirski 	 *  We can't read directly from tss.sp0: sp0 on x86_32 is special in
5899aaefe7bSAndy Lutomirski 	 *  and around vm86 mode and sp0 on x86_64 is special because of the
5909aaefe7bSAndy Lutomirski 	 *  entry trampoline.
5919aaefe7bSAndy Lutomirski 	 */
592a7fcf28dSAndy Lutomirski 	return this_cpu_read_stable(cpu_current_top_of_stack);
5938ef46a67SAndy Lutomirski }
5948ef46a67SAndy Lutomirski 
5953383642cSAndy Lutomirski static inline bool on_thread_stack(void)
5963383642cSAndy Lutomirski {
5973383642cSAndy Lutomirski 	return (unsigned long)(current_top_of_stack() -
5983383642cSAndy Lutomirski 			       current_stack_pointer) < THREAD_SIZE;
5993383642cSAndy Lutomirski }
6003383642cSAndy Lutomirski 
6019bad5658SJuergen Gross #ifdef CONFIG_PARAVIRT_XXL
602bb898558SAl Viro #include <asm/paravirt.h>
603bb898558SAl Viro #else
604bb898558SAl Viro #define __cpuid			native_cpuid
605bb898558SAl Viro 
606da51da18SAndy Lutomirski static inline void load_sp0(unsigned long sp0)
607bb898558SAl Viro {
608da51da18SAndy Lutomirski 	native_load_sp0(sp0);
609bb898558SAl Viro }
610bb898558SAl Viro 
6119bad5658SJuergen Gross #endif /* CONFIG_PARAVIRT_XXL */
612bb898558SAl Viro 
613bb898558SAl Viro /* Free all resources held by a thread. */
614bb898558SAl Viro extern void release_thread(struct task_struct *);
615bb898558SAl Viro 
616bb898558SAl Viro unsigned long get_wchan(struct task_struct *p);
617bb898558SAl Viro 
618bb898558SAl Viro /*
619bb898558SAl Viro  * Generic CPUID function
620bb898558SAl Viro  * clear %ecx since some cpus (Cyrix MII) do not set or clear %ecx
621bb898558SAl Viro  * resulting in stale register contents being returned.
622bb898558SAl Viro  */
623bb898558SAl Viro static inline void cpuid(unsigned int op,
624bb898558SAl Viro 			 unsigned int *eax, unsigned int *ebx,
625bb898558SAl Viro 			 unsigned int *ecx, unsigned int *edx)
626bb898558SAl Viro {
627bb898558SAl Viro 	*eax = op;
628bb898558SAl Viro 	*ecx = 0;
629bb898558SAl Viro 	__cpuid(eax, ebx, ecx, edx);
630bb898558SAl Viro }
631bb898558SAl Viro 
632bb898558SAl Viro /* Some CPUID calls want 'count' to be placed in ecx */
633bb898558SAl Viro static inline void cpuid_count(unsigned int op, int count,
634bb898558SAl Viro 			       unsigned int *eax, unsigned int *ebx,
635bb898558SAl Viro 			       unsigned int *ecx, unsigned int *edx)
636bb898558SAl Viro {
637bb898558SAl Viro 	*eax = op;
638bb898558SAl Viro 	*ecx = count;
639bb898558SAl Viro 	__cpuid(eax, ebx, ecx, edx);
640bb898558SAl Viro }
641bb898558SAl Viro 
642bb898558SAl Viro /*
643bb898558SAl Viro  * CPUID functions returning a single datum
644bb898558SAl Viro  */
645bb898558SAl Viro static inline unsigned int cpuid_eax(unsigned int op)
646bb898558SAl Viro {
647bb898558SAl Viro 	unsigned int eax, ebx, ecx, edx;
648bb898558SAl Viro 
649bb898558SAl Viro 	cpuid(op, &eax, &ebx, &ecx, &edx);
650bb898558SAl Viro 
651bb898558SAl Viro 	return eax;
652bb898558SAl Viro }
653bb898558SAl Viro 
654bb898558SAl Viro static inline unsigned int cpuid_ebx(unsigned int op)
655bb898558SAl Viro {
656bb898558SAl Viro 	unsigned int eax, ebx, ecx, edx;
657bb898558SAl Viro 
658bb898558SAl Viro 	cpuid(op, &eax, &ebx, &ecx, &edx);
659bb898558SAl Viro 
660bb898558SAl Viro 	return ebx;
661bb898558SAl Viro }
662bb898558SAl Viro 
663bb898558SAl Viro static inline unsigned int cpuid_ecx(unsigned int op)
664bb898558SAl Viro {
665bb898558SAl Viro 	unsigned int eax, ebx, ecx, edx;
666bb898558SAl Viro 
667bb898558SAl Viro 	cpuid(op, &eax, &ebx, &ecx, &edx);
668bb898558SAl Viro 
669bb898558SAl Viro 	return ecx;
670bb898558SAl Viro }
671bb898558SAl Viro 
672bb898558SAl Viro static inline unsigned int cpuid_edx(unsigned int op)
673bb898558SAl Viro {
674bb898558SAl Viro 	unsigned int eax, ebx, ecx, edx;
675bb898558SAl Viro 
676bb898558SAl Viro 	cpuid(op, &eax, &ebx, &ecx, &edx);
677bb898558SAl Viro 
678bb898558SAl Viro 	return edx;
679bb898558SAl Viro }
680bb898558SAl Viro 
681c198b121SAndy Lutomirski /*
682c198b121SAndy Lutomirski  * This function forces the icache and prefetched instruction stream to
683c198b121SAndy Lutomirski  * catch up with reality in two very specific cases:
684c198b121SAndy Lutomirski  *
685c198b121SAndy Lutomirski  *  a) Text was modified using one virtual address and is about to be executed
686c198b121SAndy Lutomirski  *     from the same physical page at a different virtual address.
687c198b121SAndy Lutomirski  *
688c198b121SAndy Lutomirski  *  b) Text was modified on a different CPU, may subsequently be
689c198b121SAndy Lutomirski  *     executed on this CPU, and you want to make sure the new version
690c198b121SAndy Lutomirski  *     gets executed.  This generally means you're calling this in a IPI.
691c198b121SAndy Lutomirski  *
692c198b121SAndy Lutomirski  * If you're calling this for a different reason, you're probably doing
693c198b121SAndy Lutomirski  * it wrong.
694c198b121SAndy Lutomirski  */
695bb898558SAl Viro static inline void sync_core(void)
696bb898558SAl Viro {
697c198b121SAndy Lutomirski 	/*
698c198b121SAndy Lutomirski 	 * There are quite a few ways to do this.  IRET-to-self is nice
699c198b121SAndy Lutomirski 	 * because it works on every CPU, at any CPL (so it's compatible
700c198b121SAndy Lutomirski 	 * with paravirtualization), and it never exits to a hypervisor.
701c198b121SAndy Lutomirski 	 * The only down sides are that it's a bit slow (it seems to be
702c198b121SAndy Lutomirski 	 * a bit more than 2x slower than the fastest options) and that
703c198b121SAndy Lutomirski 	 * it unmasks NMIs.  The "push %cs" is needed because, in
704c198b121SAndy Lutomirski 	 * paravirtual environments, __KERNEL_CS may not be a valid CS
705c198b121SAndy Lutomirski 	 * value when we do IRET directly.
706c198b121SAndy Lutomirski 	 *
707c198b121SAndy Lutomirski 	 * In case NMI unmasking or performance ever becomes a problem,
708c198b121SAndy Lutomirski 	 * the next best option appears to be MOV-to-CR2 and an
709c198b121SAndy Lutomirski 	 * unconditional jump.  That sequence also works on all CPUs,
710ecda85e7SJuergen Gross 	 * but it will fault at CPL3 (i.e. Xen PV).
711c198b121SAndy Lutomirski 	 *
712c198b121SAndy Lutomirski 	 * CPUID is the conventional way, but it's nasty: it doesn't
713c198b121SAndy Lutomirski 	 * exist on some 486-like CPUs, and it usually exits to a
714c198b121SAndy Lutomirski 	 * hypervisor.
715c198b121SAndy Lutomirski 	 *
716c198b121SAndy Lutomirski 	 * Like all of Linux's memory ordering operations, this is a
717c198b121SAndy Lutomirski 	 * compiler barrier as well.
718c198b121SAndy Lutomirski 	 */
7191c52d859SAndy Lutomirski #ifdef CONFIG_X86_32
720c198b121SAndy Lutomirski 	asm volatile (
721c198b121SAndy Lutomirski 		"pushfl\n\t"
722c198b121SAndy Lutomirski 		"pushl %%cs\n\t"
723c198b121SAndy Lutomirski 		"pushl $1f\n\t"
724c198b121SAndy Lutomirski 		"iret\n\t"
72545c39fb0SH. Peter Anvin 		"1:"
726f5caf621SJosh Poimboeuf 		: ASM_CALL_CONSTRAINT : : "memory");
72745c39fb0SH. Peter Anvin #else
728c198b121SAndy Lutomirski 	unsigned int tmp;
729c198b121SAndy Lutomirski 
730c198b121SAndy Lutomirski 	asm volatile (
73176846bf3SJosh Poimboeuf 		UNWIND_HINT_SAVE
732c198b121SAndy Lutomirski 		"mov %%ss, %0\n\t"
733c198b121SAndy Lutomirski 		"pushq %q0\n\t"
734c198b121SAndy Lutomirski 		"pushq %%rsp\n\t"
735c198b121SAndy Lutomirski 		"addq $8, (%%rsp)\n\t"
736c198b121SAndy Lutomirski 		"pushfq\n\t"
737c198b121SAndy Lutomirski 		"mov %%cs, %0\n\t"
738c198b121SAndy Lutomirski 		"pushq %q0\n\t"
739c198b121SAndy Lutomirski 		"pushq $1f\n\t"
740c198b121SAndy Lutomirski 		"iretq\n\t"
74176846bf3SJosh Poimboeuf 		UNWIND_HINT_RESTORE
742c198b121SAndy Lutomirski 		"1:"
743f5caf621SJosh Poimboeuf 		: "=&r" (tmp), ASM_CALL_CONSTRAINT : : "cc", "memory");
74445c39fb0SH. Peter Anvin #endif
745bb898558SAl Viro }
746bb898558SAl Viro 
747bb898558SAl Viro extern void select_idle_routine(const struct cpuinfo_x86 *c);
74807c94a38SBorislav Petkov extern void amd_e400_c1e_apic_setup(void);
749bb898558SAl Viro 
750bb898558SAl Viro extern unsigned long		boot_option_idle_override;
751bb898558SAl Viro 
752d1896049SThomas Renninger enum idle_boot_override {IDLE_NO_OVERRIDE=0, IDLE_HALT, IDLE_NOMWAIT,
75369fb3676SLen Brown 			 IDLE_POLL};
754d1896049SThomas Renninger 
755bb898558SAl Viro extern void enable_sep_cpu(void);
756bb898558SAl Viro extern int sysenter_setup(void);
757bb898558SAl Viro 
75829c84391SJan Kiszka 
759bb898558SAl Viro /* Defined in head.S */
760bb898558SAl Viro extern struct desc_ptr		early_gdt_descr;
761bb898558SAl Viro 
762552be871SBrian Gerst extern void switch_to_new_gdt(int);
76345fc8757SThomas Garnier extern void load_direct_gdt(int);
76469218e47SThomas Garnier extern void load_fixmap_gdt(int);
76511e3a840SJeremy Fitzhardinge extern void load_percpu_segment(int);
766bb898558SAl Viro extern void cpu_init(void);
7677652ac92SThomas Gleixner extern void cr4_init(void);
768bb898558SAl Viro 
769c2724775SMarkus Metzger static inline unsigned long get_debugctlmsr(void)
770c2724775SMarkus Metzger {
771c2724775SMarkus Metzger 	unsigned long debugctlmsr = 0;
772c2724775SMarkus Metzger 
773c2724775SMarkus Metzger #ifndef CONFIG_X86_DEBUGCTLMSR
774c2724775SMarkus Metzger 	if (boot_cpu_data.x86 < 6)
775c2724775SMarkus Metzger 		return 0;
776c2724775SMarkus Metzger #endif
777c2724775SMarkus Metzger 	rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
778c2724775SMarkus Metzger 
779c2724775SMarkus Metzger 	return debugctlmsr;
780c2724775SMarkus Metzger }
781c2724775SMarkus Metzger 
782bb898558SAl Viro static inline void update_debugctlmsr(unsigned long debugctlmsr)
783bb898558SAl Viro {
784bb898558SAl Viro #ifndef CONFIG_X86_DEBUGCTLMSR
785bb898558SAl Viro 	if (boot_cpu_data.x86 < 6)
786bb898558SAl Viro 		return;
787bb898558SAl Viro #endif
788bb898558SAl Viro 	wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctlmsr);
789bb898558SAl Viro }
790bb898558SAl Viro 
7919bd1190aSOleg Nesterov extern void set_task_blockstep(struct task_struct *task, bool on);
7929bd1190aSOleg Nesterov 
793bb898558SAl Viro /* Boot loader type from the setup header: */
794bb898558SAl Viro extern int			bootloader_type;
7955031296cSH. Peter Anvin extern int			bootloader_version;
796bb898558SAl Viro 
797bb898558SAl Viro extern char			ignore_fpu_irq;
798bb898558SAl Viro 
799bb898558SAl Viro #define HAVE_ARCH_PICK_MMAP_LAYOUT 1
800bb898558SAl Viro #define ARCH_HAS_PREFETCHW
801bb898558SAl Viro #define ARCH_HAS_SPINLOCK_PREFETCH
802bb898558SAl Viro 
803bb898558SAl Viro #ifdef CONFIG_X86_32
804a930dc45SBorislav Petkov # define BASE_PREFETCH		""
805bb898558SAl Viro # define ARCH_HAS_PREFETCH
806bb898558SAl Viro #else
807a930dc45SBorislav Petkov # define BASE_PREFETCH		"prefetcht0 %P1"
808bb898558SAl Viro #endif
809bb898558SAl Viro 
810bb898558SAl Viro /*
811bb898558SAl Viro  * Prefetch instructions for Pentium III (+) and AMD Athlon (+)
812bb898558SAl Viro  *
813bb898558SAl Viro  * It's not worth to care about 3dnow prefetches for the K6
814bb898558SAl Viro  * because they are microcoded there and very slow.
815bb898558SAl Viro  */
816bb898558SAl Viro static inline void prefetch(const void *x)
817bb898558SAl Viro {
818a930dc45SBorislav Petkov 	alternative_input(BASE_PREFETCH, "prefetchnta %P1",
819bb898558SAl Viro 			  X86_FEATURE_XMM,
820a930dc45SBorislav Petkov 			  "m" (*(const char *)x));
821bb898558SAl Viro }
822bb898558SAl Viro 
823bb898558SAl Viro /*
824bb898558SAl Viro  * 3dnow prefetch to get an exclusive cache line.
825bb898558SAl Viro  * Useful for spinlocks to avoid one state transition in the
826bb898558SAl Viro  * cache coherency protocol:
827bb898558SAl Viro  */
828bb898558SAl Viro static inline void prefetchw(const void *x)
829bb898558SAl Viro {
830a930dc45SBorislav Petkov 	alternative_input(BASE_PREFETCH, "prefetchw %P1",
831a930dc45SBorislav Petkov 			  X86_FEATURE_3DNOWPREFETCH,
832a930dc45SBorislav Petkov 			  "m" (*(const char *)x));
833bb898558SAl Viro }
834bb898558SAl Viro 
835bb898558SAl Viro static inline void spin_lock_prefetch(const void *x)
836bb898558SAl Viro {
837bb898558SAl Viro 	prefetchw(x);
838bb898558SAl Viro }
839bb898558SAl Viro 
840d9e05cc5SAndy Lutomirski #define TOP_OF_INIT_STACK ((unsigned long)&init_stack + sizeof(init_stack) - \
841d9e05cc5SAndy Lutomirski 			   TOP_OF_KERNEL_STACK_PADDING)
842d9e05cc5SAndy Lutomirski 
8433500130bSAndy Lutomirski #define task_top_of_stack(task) ((unsigned long)(task_pt_regs(task) + 1))
8443500130bSAndy Lutomirski 
845d375cf15SAndy Lutomirski #define task_pt_regs(task) \
846d375cf15SAndy Lutomirski ({									\
847d375cf15SAndy Lutomirski 	unsigned long __ptr = (unsigned long)task_stack_page(task);	\
848d375cf15SAndy Lutomirski 	__ptr += THREAD_SIZE - TOP_OF_KERNEL_STACK_PADDING;		\
849d375cf15SAndy Lutomirski 	((struct pt_regs *)__ptr) - 1;					\
850d375cf15SAndy Lutomirski })
851d375cf15SAndy Lutomirski 
852bb898558SAl Viro #ifdef CONFIG_X86_32
853bb898558SAl Viro /*
854bb898558SAl Viro  * User space process size: 3GB (default).
855bb898558SAl Viro  */
8568f3e474fSDmitry Safonov #define IA32_PAGE_OFFSET	PAGE_OFFSET
857bb898558SAl Viro #define TASK_SIZE		PAGE_OFFSET
858b569bab7SKirill A. Shutemov #define TASK_SIZE_LOW		TASK_SIZE
859d9517346SIngo Molnar #define TASK_SIZE_MAX		TASK_SIZE
86044b04912SKirill A. Shutemov #define DEFAULT_MAP_WINDOW	TASK_SIZE
861bb898558SAl Viro #define STACK_TOP		TASK_SIZE
862bb898558SAl Viro #define STACK_TOP_MAX		STACK_TOP
863bb898558SAl Viro 
864bb898558SAl Viro #define INIT_THREAD  {							  \
865d9e05cc5SAndy Lutomirski 	.sp0			= TOP_OF_INIT_STACK,			  \
866bb898558SAl Viro 	.sysenter_cs		= __KERNEL_CS,				  \
86713d4ea09SAndy Lutomirski 	.addr_limit		= KERNEL_DS,				  \
868bb898558SAl Viro }
869bb898558SAl Viro 
870bb898558SAl Viro #define KSTK_ESP(task)		(task_pt_regs(task)->sp)
871bb898558SAl Viro 
872bb898558SAl Viro #else
873bb898558SAl Viro /*
874f55f0501SAndy Lutomirski  * User space process size.  This is the first address outside the user range.
875f55f0501SAndy Lutomirski  * There are a few constraints that determine this:
876f55f0501SAndy Lutomirski  *
877f55f0501SAndy Lutomirski  * On Intel CPUs, if a SYSCALL instruction is at the highest canonical
878f55f0501SAndy Lutomirski  * address, then that syscall will enter the kernel with a
879f55f0501SAndy Lutomirski  * non-canonical return address, and SYSRET will explode dangerously.
880f55f0501SAndy Lutomirski  * We avoid this particular problem by preventing anything executable
881f55f0501SAndy Lutomirski  * from being mapped at the maximum canonical address.
882f55f0501SAndy Lutomirski  *
883f55f0501SAndy Lutomirski  * On AMD CPUs in the Ryzen family, there's a nasty bug in which the
884f55f0501SAndy Lutomirski  * CPUs malfunction if they execute code from the highest canonical page.
885f55f0501SAndy Lutomirski  * They'll speculate right off the end of the canonical space, and
886f55f0501SAndy Lutomirski  * bad things happen.  This is worked around in the same way as the
887f55f0501SAndy Lutomirski  * Intel problem.
888f55f0501SAndy Lutomirski  *
889f55f0501SAndy Lutomirski  * With page table isolation enabled, we map the LDT in ... [stay tuned]
890bb898558SAl Viro  */
891ee00f4a3SKirill A. Shutemov #define TASK_SIZE_MAX	((1UL << __VIRTUAL_MASK_SHIFT) - PAGE_SIZE)
892bb898558SAl Viro 
893ee00f4a3SKirill A. Shutemov #define DEFAULT_MAP_WINDOW	((1UL << 47) - PAGE_SIZE)
894bb898558SAl Viro 
895bb898558SAl Viro /* This decides where the kernel will search for a free chunk of vm
896bb898558SAl Viro  * space during mmap's.
897bb898558SAl Viro  */
898bb898558SAl Viro #define IA32_PAGE_OFFSET	((current->personality & ADDR_LIMIT_3GB) ? \
899bb898558SAl Viro 					0xc0000000 : 0xFFFFe000)
900bb898558SAl Viro 
901b569bab7SKirill A. Shutemov #define TASK_SIZE_LOW		(test_thread_flag(TIF_ADDR32) ? \
902b569bab7SKirill A. Shutemov 					IA32_PAGE_OFFSET : DEFAULT_MAP_WINDOW)
9036bd33008SH. Peter Anvin #define TASK_SIZE		(test_thread_flag(TIF_ADDR32) ? \
904d9517346SIngo Molnar 					IA32_PAGE_OFFSET : TASK_SIZE_MAX)
9056bd33008SH. Peter Anvin #define TASK_SIZE_OF(child)	((test_tsk_thread_flag(child, TIF_ADDR32)) ? \
906d9517346SIngo Molnar 					IA32_PAGE_OFFSET : TASK_SIZE_MAX)
907bb898558SAl Viro 
908b569bab7SKirill A. Shutemov #define STACK_TOP		TASK_SIZE_LOW
909d9517346SIngo Molnar #define STACK_TOP_MAX		TASK_SIZE_MAX
910bb898558SAl Viro 
911bb898558SAl Viro #define INIT_THREAD  {						\
91213d4ea09SAndy Lutomirski 	.addr_limit		= KERNEL_DS,			\
913bb898558SAl Viro }
914bb898558SAl Viro 
91589240ba0SStefani Seibold extern unsigned long KSTK_ESP(struct task_struct *task);
916d046ff8bSH. J. Lu 
917bb898558SAl Viro #endif /* CONFIG_X86_64 */
918bb898558SAl Viro 
919bb898558SAl Viro extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
920bb898558SAl Viro 					       unsigned long new_sp);
921bb898558SAl Viro 
922bb898558SAl Viro /*
923bb898558SAl Viro  * This decides where the kernel will search for a free chunk of vm
924bb898558SAl Viro  * space during mmap's.
925bb898558SAl Viro  */
9268f3e474fSDmitry Safonov #define __TASK_UNMAPPED_BASE(task_size)	(PAGE_ALIGN(task_size / 3))
927b569bab7SKirill A. Shutemov #define TASK_UNMAPPED_BASE		__TASK_UNMAPPED_BASE(TASK_SIZE_LOW)
928bb898558SAl Viro 
929bb898558SAl Viro #define KSTK_EIP(task)		(task_pt_regs(task)->ip)
930bb898558SAl Viro 
931bb898558SAl Viro /* Get/set a process' ability to use the timestamp counter instruction */
932bb898558SAl Viro #define GET_TSC_CTL(adr)	get_tsc_mode((adr))
933bb898558SAl Viro #define SET_TSC_CTL(val)	set_tsc_mode((val))
934bb898558SAl Viro 
935bb898558SAl Viro extern int get_tsc_mode(unsigned long adr);
936bb898558SAl Viro extern int set_tsc_mode(unsigned int val);
937bb898558SAl Viro 
938e9ea1e7fSKyle Huey DECLARE_PER_CPU(u64, msr_misc_features_shadow);
939e9ea1e7fSKyle Huey 
940bc8e80d5SBorislav Petkov #ifdef CONFIG_CPU_SUP_AMD
9418b84c8dfSDaniel J Blueman extern u16 amd_get_nb_id(int cpu);
942cc2749e4SAravind Gopalakrishnan extern u32 amd_get_nodes_per_socket(void);
943bc8e80d5SBorislav Petkov #else
944bc8e80d5SBorislav Petkov static inline u16 amd_get_nb_id(int cpu)		{ return 0; }
945bc8e80d5SBorislav Petkov static inline u32 amd_get_nodes_per_socket(void)	{ return 0; }
946bc8e80d5SBorislav Petkov #endif
9476a812691SAndreas Herrmann 
94896e39ac0SJason Wang static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
94996e39ac0SJason Wang {
95096e39ac0SJason Wang 	uint32_t base, eax, signature[3];
95196e39ac0SJason Wang 
95296e39ac0SJason Wang 	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
95396e39ac0SJason Wang 		cpuid(base, &eax, &signature[0], &signature[1], &signature[2]);
95496e39ac0SJason Wang 
95596e39ac0SJason Wang 		if (!memcmp(sig, signature, 12) &&
95696e39ac0SJason Wang 		    (leaves == 0 || ((eax - base) >= leaves)))
95796e39ac0SJason Wang 			return base;
95896e39ac0SJason Wang 	}
95996e39ac0SJason Wang 
96096e39ac0SJason Wang 	return 0;
96196e39ac0SJason Wang }
96296e39ac0SJason Wang 
963f05e798aSDavid Howells extern unsigned long arch_align_stack(unsigned long sp);
964e5cb113fSAlexey Dobriyan void free_init_pages(const char *what, unsigned long begin, unsigned long end);
9655494c3a6SKees Cook extern void free_kernel_image_pages(const char *what, void *begin, void *end);
966f05e798aSDavid Howells 
967f05e798aSDavid Howells void default_idle(void);
9686a377ddcSLen Brown #ifdef	CONFIG_XEN
9696a377ddcSLen Brown bool xen_set_default_idle(void);
9706a377ddcSLen Brown #else
9716a377ddcSLen Brown #define xen_set_default_idle 0
9726a377ddcSLen Brown #endif
973f05e798aSDavid Howells 
974f05e798aSDavid Howells void stop_this_cpu(void *dummy);
9751008c52cSBorislav Petkov void microcode_check(void);
976d90a7a0eSJiri Kosina 
977d90a7a0eSJiri Kosina enum l1tf_mitigations {
978d90a7a0eSJiri Kosina 	L1TF_MITIGATION_OFF,
979d90a7a0eSJiri Kosina 	L1TF_MITIGATION_FLUSH_NOWARN,
980d90a7a0eSJiri Kosina 	L1TF_MITIGATION_FLUSH,
981d90a7a0eSJiri Kosina 	L1TF_MITIGATION_FLUSH_NOSMT,
982d90a7a0eSJiri Kosina 	L1TF_MITIGATION_FULL,
983d90a7a0eSJiri Kosina 	L1TF_MITIGATION_FULL_FORCE
984d90a7a0eSJiri Kosina };
985d90a7a0eSJiri Kosina 
986d90a7a0eSJiri Kosina extern enum l1tf_mitigations l1tf_mitigation;
987d90a7a0eSJiri Kosina 
988bc124170SThomas Gleixner enum mds_mitigations {
989bc124170SThomas Gleixner 	MDS_MITIGATION_OFF,
990bc124170SThomas Gleixner 	MDS_MITIGATION_FULL,
99122dd8365SThomas Gleixner 	MDS_MITIGATION_VMWERV,
992bc124170SThomas Gleixner };
993bc124170SThomas Gleixner 
9941965aae3SH. Peter Anvin #endif /* _ASM_X86_PROCESSOR_H */
995