xref: /linux-6.15/arch/x86/include/asm/fixmap.h (revision 0f9a4810)
1 /*
2  * fixmap.h: compile-time virtual memory allocation
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (C) 1998 Ingo Molnar
9  *
10  * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
11  * x86_32 and x86_64 integration by Gustavo F. Padovan, February 2009
12  */
13 
14 #ifndef _ASM_X86_FIXMAP_H
15 #define _ASM_X86_FIXMAP_H
16 
17 #ifndef __ASSEMBLY__
18 #include <linux/kernel.h>
19 #include <asm/acpi.h>
20 #include <asm/apicdef.h>
21 #include <asm/page.h>
22 #ifdef CONFIG_X86_32
23 #include <linux/threads.h>
24 #include <asm/kmap_types.h>
25 #else
26 #include <uapi/asm/vsyscall.h>
27 #endif
28 
29 /*
30  * We can't declare FIXADDR_TOP as variable for x86_64 because vsyscall
31  * uses fixmaps that relies on FIXADDR_TOP for proper address calculation.
32  * Because of this, FIXADDR_TOP x86 integration was left as later work.
33  */
34 #ifdef CONFIG_X86_32
35 /* used by vmalloc.c, vsyscall.lds.S.
36  *
37  * Leave one empty page between vmalloc'ed areas and
38  * the start of the fixmap.
39  */
40 extern unsigned long __FIXADDR_TOP;
41 #define FIXADDR_TOP	((unsigned long)__FIXADDR_TOP)
42 #else
43 #define FIXADDR_TOP	(round_up(VSYSCALL_ADDR + PAGE_SIZE, 1<<PMD_SHIFT) - \
44 			 PAGE_SIZE)
45 #endif
46 
47 /*
48  * cpu_entry_area is a percpu region in the fixmap that contains things
49  * needed by the CPU and early entry/exit code.  Real types aren't used
50  * for all fields here to avoid circular header dependencies.
51  *
52  * Every field is a virtual alias of some other allocated backing store.
53  * There is no direct allocation of a struct cpu_entry_area.
54  */
55 struct cpu_entry_area {
56 	char gdt[PAGE_SIZE];
57 
58 	/*
59 	 * The GDT is just below cpu_tss and thus serves (on x86_64) as a
60 	 * a read-only guard page for the SYSENTER stack at the bottom
61 	 * of the TSS region.
62 	 */
63 	struct tss_struct tss;
64 
65 	char entry_trampoline[PAGE_SIZE];
66 
67 #ifdef CONFIG_X86_64
68 	/*
69 	 * Exception stacks used for IST entries.
70 	 *
71 	 * In the future, this should have a separate slot for each stack
72 	 * with guard pages between them.
73 	 */
74 	char exception_stacks[(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ];
75 #endif
76 };
77 
78 #define CPU_ENTRY_AREA_PAGES (sizeof(struct cpu_entry_area) / PAGE_SIZE)
79 
80 extern void setup_cpu_entry_areas(void);
81 
82 /*
83  * Here we define all the compile-time 'special' virtual
84  * addresses. The point is to have a constant address at
85  * compile time, but to set the physical address only
86  * in the boot process.
87  * for x86_32: We allocate these special addresses
88  * from the end of virtual memory (0xfffff000) backwards.
89  * Also this lets us do fail-safe vmalloc(), we
90  * can guarantee that these special addresses and
91  * vmalloc()-ed addresses never overlap.
92  *
93  * These 'compile-time allocated' memory buffers are
94  * fixed-size 4k pages (or larger if used with an increment
95  * higher than 1). Use set_fixmap(idx,phys) to associate
96  * physical memory with fixmap indices.
97  *
98  * TLB entries of such buffers will not be flushed across
99  * task switches.
100  */
101 enum fixed_addresses {
102 #ifdef CONFIG_X86_32
103 	FIX_HOLE,
104 #else
105 #ifdef CONFIG_X86_VSYSCALL_EMULATION
106 	VSYSCALL_PAGE = (FIXADDR_TOP - VSYSCALL_ADDR) >> PAGE_SHIFT,
107 #endif
108 #endif
109 	FIX_DBGP_BASE,
110 	FIX_EARLYCON_MEM_BASE,
111 #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
112 	FIX_OHCI1394_BASE,
113 #endif
114 #ifdef CONFIG_X86_LOCAL_APIC
115 	FIX_APIC_BASE,	/* local (CPU) APIC) -- required for SMP or not */
116 #endif
117 #ifdef CONFIG_X86_IO_APIC
118 	FIX_IO_APIC_BASE_0,
119 	FIX_IO_APIC_BASE_END = FIX_IO_APIC_BASE_0 + MAX_IO_APICS - 1,
120 #endif
121 	FIX_RO_IDT,	/* Virtual mapping for read-only IDT */
122 #ifdef CONFIG_X86_32
123 	FIX_KMAP_BEGIN,	/* reserved pte's for temporary kernel mappings */
124 	FIX_KMAP_END = FIX_KMAP_BEGIN+(KM_TYPE_NR*NR_CPUS)-1,
125 #ifdef CONFIG_PCI_MMCONFIG
126 	FIX_PCIE_MCFG,
127 #endif
128 #endif
129 #ifdef CONFIG_PARAVIRT
130 	FIX_PARAVIRT_BOOTMAP,
131 #endif
132 	FIX_TEXT_POKE1,	/* reserve 2 pages for text_poke() */
133 	FIX_TEXT_POKE0, /* first page is last, because allocation is backward */
134 #ifdef	CONFIG_X86_INTEL_MID
135 	FIX_LNW_VRTC,
136 #endif
137 	/* Fixmap entries to remap the GDTs, one per processor. */
138 	FIX_CPU_ENTRY_AREA_TOP,
139 	FIX_CPU_ENTRY_AREA_BOTTOM = FIX_CPU_ENTRY_AREA_TOP + (CPU_ENTRY_AREA_PAGES * NR_CPUS) - 1,
140 
141 #ifdef CONFIG_ACPI_APEI_GHES
142 	/* Used for GHES mapping from assorted contexts */
143 	FIX_APEI_GHES_IRQ,
144 	FIX_APEI_GHES_NMI,
145 #endif
146 
147 	__end_of_permanent_fixed_addresses,
148 
149 	/*
150 	 * 512 temporary boot-time mappings, used by early_ioremap(),
151 	 * before ioremap() is functional.
152 	 *
153 	 * If necessary we round it up to the next 512 pages boundary so
154 	 * that we can have a single pgd entry and a single pte table:
155 	 */
156 #define NR_FIX_BTMAPS		64
157 #define FIX_BTMAPS_SLOTS	8
158 #define TOTAL_FIX_BTMAPS	(NR_FIX_BTMAPS * FIX_BTMAPS_SLOTS)
159 	FIX_BTMAP_END =
160 	 (__end_of_permanent_fixed_addresses ^
161 	  (__end_of_permanent_fixed_addresses + TOTAL_FIX_BTMAPS - 1)) &
162 	 -PTRS_PER_PTE
163 	 ? __end_of_permanent_fixed_addresses + TOTAL_FIX_BTMAPS -
164 	   (__end_of_permanent_fixed_addresses & (TOTAL_FIX_BTMAPS - 1))
165 	 : __end_of_permanent_fixed_addresses,
166 	FIX_BTMAP_BEGIN = FIX_BTMAP_END + TOTAL_FIX_BTMAPS - 1,
167 #ifdef CONFIG_X86_32
168 	FIX_WP_TEST,
169 #endif
170 #ifdef CONFIG_INTEL_TXT
171 	FIX_TBOOT_BASE,
172 #endif
173 	__end_of_fixed_addresses
174 };
175 
176 
177 extern void reserve_top_address(unsigned long reserve);
178 
179 #define FIXADDR_SIZE	(__end_of_permanent_fixed_addresses << PAGE_SHIFT)
180 #define FIXADDR_START		(FIXADDR_TOP - FIXADDR_SIZE)
181 
182 extern int fixmaps_set;
183 
184 extern pte_t *kmap_pte;
185 #define kmap_prot PAGE_KERNEL
186 extern pte_t *pkmap_page_table;
187 
188 void __native_set_fixmap(enum fixed_addresses idx, pte_t pte);
189 void native_set_fixmap(enum fixed_addresses idx,
190 		       phys_addr_t phys, pgprot_t flags);
191 
192 #ifndef CONFIG_PARAVIRT
193 static inline void __set_fixmap(enum fixed_addresses idx,
194 				phys_addr_t phys, pgprot_t flags)
195 {
196 	native_set_fixmap(idx, phys, flags);
197 }
198 #endif
199 
200 /*
201  * FIXMAP_PAGE_NOCACHE is used for MMIO. Memory encryption is not
202  * supported for MMIO addresses, so make sure that the memory encryption
203  * mask is not part of the page attributes.
204  */
205 #define FIXMAP_PAGE_NOCACHE PAGE_KERNEL_IO_NOCACHE
206 
207 /*
208  * Early memremap routines used for in-place encryption. The mappings created
209  * by these routines are intended to be used as temporary mappings.
210  */
211 void __init *early_memremap_encrypted(resource_size_t phys_addr,
212 				      unsigned long size);
213 void __init *early_memremap_encrypted_wp(resource_size_t phys_addr,
214 					 unsigned long size);
215 void __init *early_memremap_decrypted(resource_size_t phys_addr,
216 				      unsigned long size);
217 void __init *early_memremap_decrypted_wp(resource_size_t phys_addr,
218 					 unsigned long size);
219 
220 #include <asm-generic/fixmap.h>
221 
222 #define __late_set_fixmap(idx, phys, flags) __set_fixmap(idx, phys, flags)
223 #define __late_clear_fixmap(idx) __set_fixmap(idx, 0, __pgprot(0))
224 
225 void __early_set_fixmap(enum fixed_addresses idx,
226 			phys_addr_t phys, pgprot_t flags);
227 
228 static inline unsigned int __get_cpu_entry_area_page_index(int cpu, int page)
229 {
230 	BUILD_BUG_ON(sizeof(struct cpu_entry_area) % PAGE_SIZE != 0);
231 
232 	return FIX_CPU_ENTRY_AREA_BOTTOM - cpu*CPU_ENTRY_AREA_PAGES - page;
233 }
234 
235 #define __get_cpu_entry_area_offset_index(cpu, offset) ({		\
236 	BUILD_BUG_ON(offset % PAGE_SIZE != 0);				\
237 	__get_cpu_entry_area_page_index(cpu, offset / PAGE_SIZE);	\
238 	})
239 
240 #define get_cpu_entry_area_index(cpu, field)				\
241 	__get_cpu_entry_area_offset_index((cpu), offsetof(struct cpu_entry_area, field))
242 
243 static inline struct cpu_entry_area *get_cpu_entry_area(int cpu)
244 {
245 	return (struct cpu_entry_area *)__fix_to_virt(__get_cpu_entry_area_page_index(cpu, 0));
246 }
247 
248 static inline struct SYSENTER_stack *cpu_SYSENTER_stack(int cpu)
249 {
250 	return &get_cpu_entry_area(cpu)->tss.SYSENTER_stack;
251 }
252 
253 #endif /* !__ASSEMBLY__ */
254 #endif /* _ASM_X86_FIXMAP_H */
255