1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PGTABLE_64_DEFS_H
3 #define _ASM_X86_PGTABLE_64_DEFS_H
4 
5 #include <asm/sparsemem.h>
6 
7 #ifndef __ASSEMBLY__
8 #include <linux/types.h>
9 #include <asm/kaslr.h>
10 
11 /*
12  * These are used to make use of C type-checking..
13  */
14 typedef unsigned long	pteval_t;
15 typedef unsigned long	pmdval_t;
16 typedef unsigned long	pudval_t;
17 typedef unsigned long	p4dval_t;
18 typedef unsigned long	pgdval_t;
19 typedef unsigned long	pgprotval_t;
20 
21 typedef struct { pteval_t pte; } pte_t;
22 
23 #ifdef CONFIG_X86_5LEVEL
24 extern unsigned int __pgtable_l5_enabled;
25 #ifndef pgtable_l5_enabled
26 #define pgtable_l5_enabled cpu_feature_enabled(X86_FEATURE_LA57)
27 #endif
28 #else
29 #define pgtable_l5_enabled 0
30 #endif
31 
32 extern unsigned int pgdir_shift;
33 extern unsigned int ptrs_per_p4d;
34 
35 #endif	/* !__ASSEMBLY__ */
36 
37 #define SHARED_KERNEL_PMD	0
38 
39 #ifdef CONFIG_X86_5LEVEL
40 
41 /*
42  * PGDIR_SHIFT determines what a top-level page table entry can map
43  */
44 #define PGDIR_SHIFT	pgdir_shift
45 #define PTRS_PER_PGD	512
46 
47 /*
48  * 4th level page in 5-level paging case
49  */
50 #define P4D_SHIFT		39
51 #define MAX_PTRS_PER_P4D	512
52 #define PTRS_PER_P4D		ptrs_per_p4d
53 #define P4D_SIZE		(_AC(1, UL) << P4D_SHIFT)
54 #define P4D_MASK		(~(P4D_SIZE - 1))
55 
56 #define MAX_POSSIBLE_PHYSMEM_BITS	52
57 
58 #else /* CONFIG_X86_5LEVEL */
59 
60 /*
61  * PGDIR_SHIFT determines what a top-level page table entry can map
62  */
63 #define PGDIR_SHIFT		39
64 #define PTRS_PER_PGD		512
65 #define MAX_PTRS_PER_P4D	1
66 
67 #endif /* CONFIG_X86_5LEVEL */
68 
69 /*
70  * 3rd level page
71  */
72 #define PUD_SHIFT	30
73 #define PTRS_PER_PUD	512
74 
75 /*
76  * PMD_SHIFT determines the size of the area a middle-level
77  * page table can map
78  */
79 #define PMD_SHIFT	21
80 #define PTRS_PER_PMD	512
81 
82 /*
83  * entries per page directory level
84  */
85 #define PTRS_PER_PTE	512
86 
87 #define PMD_SIZE	(_AC(1, UL) << PMD_SHIFT)
88 #define PMD_MASK	(~(PMD_SIZE - 1))
89 #define PUD_SIZE	(_AC(1, UL) << PUD_SHIFT)
90 #define PUD_MASK	(~(PUD_SIZE - 1))
91 #define PGDIR_SIZE	(_AC(1, UL) << PGDIR_SHIFT)
92 #define PGDIR_MASK	(~(PGDIR_SIZE - 1))
93 
94 /*
95  * See Documentation/x86/x86_64/mm.txt for a description of the memory map.
96  *
97  * Be very careful vs. KASLR when changing anything here. The KASLR address
98  * range must not overlap with anything except the KASAN shadow area, which
99  * is correct as KASAN disables KASLR.
100  */
101 #define MAXMEM			(1UL << MAX_PHYSMEM_BITS)
102 
103 #define LDT_PGD_ENTRY_L4	-3UL
104 #define LDT_PGD_ENTRY_L5	-112UL
105 #define LDT_PGD_ENTRY		(pgtable_l5_enabled ? LDT_PGD_ENTRY_L5 : LDT_PGD_ENTRY_L4)
106 #define LDT_BASE_ADDR		(LDT_PGD_ENTRY << PGDIR_SHIFT)
107 
108 #define __VMALLOC_BASE_L4	0xffffc90000000000UL
109 #define __VMALLOC_BASE_L5 	0xffa0000000000000UL
110 
111 #define VMALLOC_SIZE_TB_L4	32UL
112 #define VMALLOC_SIZE_TB_L5	12800UL
113 
114 #define __VMEMMAP_BASE_L4	0xffffea0000000000UL
115 #define __VMEMMAP_BASE_L5	0xffd4000000000000UL
116 
117 #ifdef CONFIG_DYNAMIC_MEMORY_LAYOUT
118 # define VMALLOC_START		vmalloc_base
119 # define VMALLOC_SIZE_TB	(pgtable_l5_enabled ? VMALLOC_SIZE_TB_L5 : VMALLOC_SIZE_TB_L4)
120 # define VMEMMAP_START		vmemmap_base
121 #else
122 # define VMALLOC_START		__VMALLOC_BASE_L4
123 # define VMALLOC_SIZE_TB	VMALLOC_SIZE_TB_L4
124 # define VMEMMAP_START		__VMEMMAP_BASE_L4
125 #endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */
126 
127 #define VMALLOC_END		(VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1)
128 
129 #define MODULES_VADDR		(__START_KERNEL_map + KERNEL_IMAGE_SIZE)
130 /* The module sections ends with the start of the fixmap */
131 #define MODULES_END		_AC(0xffffffffff000000, UL)
132 #define MODULES_LEN		(MODULES_END - MODULES_VADDR)
133 
134 #define ESPFIX_PGD_ENTRY	_AC(-2, UL)
135 #define ESPFIX_BASE_ADDR	(ESPFIX_PGD_ENTRY << P4D_SHIFT)
136 
137 #define CPU_ENTRY_AREA_PGD	_AC(-4, UL)
138 #define CPU_ENTRY_AREA_BASE	(CPU_ENTRY_AREA_PGD << P4D_SHIFT)
139 
140 #define EFI_VA_START		( -4 * (_AC(1, UL) << 30))
141 #define EFI_VA_END		(-68 * (_AC(1, UL) << 30))
142 
143 #define EARLY_DYNAMIC_PAGE_TABLES	64
144 
145 #endif /* _ASM_X86_PGTABLE_64_DEFS_H */
146