18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#ifndef _ASM_X86_CPU_ENTRY_AREA_H
48c2ecf20Sopenharmony_ci#define _ASM_X86_CPU_ENTRY_AREA_H
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/percpu-defs.h>
78c2ecf20Sopenharmony_ci#include <asm/processor.h>
88c2ecf20Sopenharmony_ci#include <asm/intel_ds.h>
98c2ecf20Sopenharmony_ci#include <asm/pgtable_areas.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#ifdef CONFIG_AMD_MEM_ENCRYPT
148c2ecf20Sopenharmony_ci#define VC_EXCEPTION_STKSZ	EXCEPTION_STKSZ
158c2ecf20Sopenharmony_ci#else
168c2ecf20Sopenharmony_ci#define VC_EXCEPTION_STKSZ	0
178c2ecf20Sopenharmony_ci#endif
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* Macro to enforce the same ordering and stack sizes */
208c2ecf20Sopenharmony_ci#define ESTACKS_MEMBERS(guardsize, optional_stack_size)		\
218c2ecf20Sopenharmony_ci	char	DF_stack_guard[guardsize];			\
228c2ecf20Sopenharmony_ci	char	DF_stack[EXCEPTION_STKSZ];			\
238c2ecf20Sopenharmony_ci	char	NMI_stack_guard[guardsize];			\
248c2ecf20Sopenharmony_ci	char	NMI_stack[EXCEPTION_STKSZ];			\
258c2ecf20Sopenharmony_ci	char	DB_stack_guard[guardsize];			\
268c2ecf20Sopenharmony_ci	char	DB_stack[EXCEPTION_STKSZ];			\
278c2ecf20Sopenharmony_ci	char	MCE_stack_guard[guardsize];			\
288c2ecf20Sopenharmony_ci	char	MCE_stack[EXCEPTION_STKSZ];			\
298c2ecf20Sopenharmony_ci	char	VC_stack_guard[guardsize];			\
308c2ecf20Sopenharmony_ci	char	VC_stack[optional_stack_size];			\
318c2ecf20Sopenharmony_ci	char	VC2_stack_guard[guardsize];			\
328c2ecf20Sopenharmony_ci	char	VC2_stack[optional_stack_size];			\
338c2ecf20Sopenharmony_ci	char	IST_top_guard[guardsize];			\
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* The exception stacks' physical storage. No guard pages required */
368c2ecf20Sopenharmony_cistruct exception_stacks {
378c2ecf20Sopenharmony_ci	ESTACKS_MEMBERS(0, VC_EXCEPTION_STKSZ)
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* The effective cpu entry area mapping with guard pages. */
418c2ecf20Sopenharmony_cistruct cea_exception_stacks {
428c2ecf20Sopenharmony_ci	ESTACKS_MEMBERS(PAGE_SIZE, EXCEPTION_STKSZ)
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/*
468c2ecf20Sopenharmony_ci * The exception stack ordering in [cea_]exception_stacks
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_cienum exception_stack_ordering {
498c2ecf20Sopenharmony_ci	ESTACK_DF,
508c2ecf20Sopenharmony_ci	ESTACK_NMI,
518c2ecf20Sopenharmony_ci	ESTACK_DB,
528c2ecf20Sopenharmony_ci	ESTACK_MCE,
538c2ecf20Sopenharmony_ci	ESTACK_VC,
548c2ecf20Sopenharmony_ci	ESTACK_VC2,
558c2ecf20Sopenharmony_ci	N_EXCEPTION_STACKS
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define CEA_ESTACK_SIZE(st)					\
598c2ecf20Sopenharmony_ci	sizeof(((struct cea_exception_stacks *)0)->st## _stack)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define CEA_ESTACK_BOT(ceastp, st)				\
628c2ecf20Sopenharmony_ci	((unsigned long)&(ceastp)->st## _stack)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define CEA_ESTACK_TOP(ceastp, st)				\
658c2ecf20Sopenharmony_ci	(CEA_ESTACK_BOT(ceastp, st) + CEA_ESTACK_SIZE(st))
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define CEA_ESTACK_OFFS(st)					\
688c2ecf20Sopenharmony_ci	offsetof(struct cea_exception_stacks, st## _stack)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define CEA_ESTACK_PAGES					\
718c2ecf20Sopenharmony_ci	(sizeof(struct cea_exception_stacks) / PAGE_SIZE)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#endif
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
768c2ecf20Sopenharmony_cistruct doublefault_stack {
778c2ecf20Sopenharmony_ci	unsigned long stack[(PAGE_SIZE - sizeof(struct x86_hw_tss)) / sizeof(unsigned long)];
788c2ecf20Sopenharmony_ci	struct x86_hw_tss tss;
798c2ecf20Sopenharmony_ci} __aligned(PAGE_SIZE);
808c2ecf20Sopenharmony_ci#endif
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/*
838c2ecf20Sopenharmony_ci * cpu_entry_area is a percpu region that contains things needed by the CPU
848c2ecf20Sopenharmony_ci * and early entry/exit code.  Real types aren't used for all fields here
858c2ecf20Sopenharmony_ci * to avoid circular header dependencies.
868c2ecf20Sopenharmony_ci *
878c2ecf20Sopenharmony_ci * Every field is a virtual alias of some other allocated backing store.
888c2ecf20Sopenharmony_ci * There is no direct allocation of a struct cpu_entry_area.
898c2ecf20Sopenharmony_ci */
908c2ecf20Sopenharmony_cistruct cpu_entry_area {
918c2ecf20Sopenharmony_ci	char gdt[PAGE_SIZE];
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	/*
948c2ecf20Sopenharmony_ci	 * The GDT is just below entry_stack and thus serves (on x86_64) as
958c2ecf20Sopenharmony_ci	 * a read-only guard page. On 32-bit the GDT must be writeable, so
968c2ecf20Sopenharmony_ci	 * it needs an extra guard page.
978c2ecf20Sopenharmony_ci	 */
988c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
998c2ecf20Sopenharmony_ci	char guard_entry_stack[PAGE_SIZE];
1008c2ecf20Sopenharmony_ci#endif
1018c2ecf20Sopenharmony_ci	struct entry_stack_page entry_stack_page;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_32
1048c2ecf20Sopenharmony_ci	char guard_doublefault_stack[PAGE_SIZE];
1058c2ecf20Sopenharmony_ci	struct doublefault_stack doublefault_stack;
1068c2ecf20Sopenharmony_ci#endif
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/*
1098c2ecf20Sopenharmony_ci	 * On x86_64, the TSS is mapped RO.  On x86_32, it's mapped RW because
1108c2ecf20Sopenharmony_ci	 * we need task switches to work, and task switches write to the TSS.
1118c2ecf20Sopenharmony_ci	 */
1128c2ecf20Sopenharmony_ci	struct tss_struct tss;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
1158c2ecf20Sopenharmony_ci	/*
1168c2ecf20Sopenharmony_ci	 * Exception stacks used for IST entries with guard pages.
1178c2ecf20Sopenharmony_ci	 */
1188c2ecf20Sopenharmony_ci	struct cea_exception_stacks estacks;
1198c2ecf20Sopenharmony_ci#endif
1208c2ecf20Sopenharmony_ci	/*
1218c2ecf20Sopenharmony_ci	 * Per CPU debug store for Intel performance monitoring. Wastes a
1228c2ecf20Sopenharmony_ci	 * full page at the moment.
1238c2ecf20Sopenharmony_ci	 */
1248c2ecf20Sopenharmony_ci	struct debug_store cpu_debug_store;
1258c2ecf20Sopenharmony_ci	/*
1268c2ecf20Sopenharmony_ci	 * The actual PEBS/BTS buffers must be mapped to user space
1278c2ecf20Sopenharmony_ci	 * Reserve enough fixmap PTEs.
1288c2ecf20Sopenharmony_ci	 */
1298c2ecf20Sopenharmony_ci	struct debug_store_buffers cpu_debug_buffers;
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define CPU_ENTRY_AREA_SIZE		(sizeof(struct cpu_entry_area))
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ciDECLARE_PER_CPU(struct cpu_entry_area *, cpu_entry_area);
1358c2ecf20Sopenharmony_ciDECLARE_PER_CPU(struct cea_exception_stacks *, cea_exception_stacks);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciextern void setup_cpu_entry_areas(void);
1388c2ecf20Sopenharmony_ciextern void cea_set_pte(void *cea_vaddr, phys_addr_t pa, pgprot_t flags);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciextern struct cpu_entry_area *get_cpu_entry_area(int cpu);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic inline struct entry_stack *cpu_entry_stack(int cpu)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	return &get_cpu_entry_area(cpu)->entry_stack_page.stack;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#define __this_cpu_ist_top_va(name)					\
1488c2ecf20Sopenharmony_ci	CEA_ESTACK_TOP(__this_cpu_read(cea_exception_stacks), name)
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci#define __this_cpu_ist_bottom_va(name)					\
1518c2ecf20Sopenharmony_ci	CEA_ESTACK_BOT(__this_cpu_read(cea_exception_stacks), name)
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci#endif
154