This source file includes following definitions.
- find_trampoline_placement
- paging_prepare
- cleanup_trampoline
1 #include <linux/efi.h>
2 #include <asm/e820/types.h>
3 #include <asm/processor.h>
4 #include <asm/efi.h>
5 #include "pgtable.h"
6 #include "../string.h"
7
8
9
10
11
12
13
14
15 unsigned long __force_order;
16
17 #define BIOS_START_MIN 0x20000U
18 #define BIOS_START_MAX 0x9f000U
19
20 struct paging_config {
21 unsigned long trampoline_start;
22 unsigned long l5_required;
23 };
24
25
26 static char trampoline_save[TRAMPOLINE_32BIT_SIZE];
27
28
29
30
31
32
33
34
35 unsigned long *trampoline_32bit __section(.data);
36
37 extern struct boot_params *boot_params;
38 int cmdline_find_option_bool(const char *option);
39
40 static unsigned long find_trampoline_placement(void)
41 {
42 unsigned long bios_start = 0, ebda_start = 0;
43 struct boot_e820_entry *entry;
44 char *signature;
45 int i;
46
47
48
49
50
51
52
53
54
55
56
57
58 signature = (char *)&boot_params->efi_info.efi_loader_signature;
59 if (strncmp(signature, EFI32_LOADER_SIGNATURE, 4) &&
60 strncmp(signature, EFI64_LOADER_SIGNATURE, 4)) {
61 ebda_start = *(unsigned short *)0x40e << 4;
62 bios_start = *(unsigned short *)0x413 << 10;
63 }
64
65 if (bios_start < BIOS_START_MIN || bios_start > BIOS_START_MAX)
66 bios_start = BIOS_START_MAX;
67
68 if (ebda_start > BIOS_START_MIN && ebda_start < bios_start)
69 bios_start = ebda_start;
70
71 bios_start = round_down(bios_start, PAGE_SIZE);
72
73
74 for (i = boot_params->e820_entries - 1; i >= 0; i--) {
75 unsigned long new = bios_start;
76
77 entry = &boot_params->e820_table[i];
78
79
80 if (bios_start <= entry->addr)
81 continue;
82
83
84 if (entry->type != E820_TYPE_RAM)
85 continue;
86
87
88 if (bios_start > entry->addr + entry->size)
89 new = entry->addr + entry->size;
90
91
92 new = round_down(new, PAGE_SIZE);
93
94
95 if (new - TRAMPOLINE_32BIT_SIZE < entry->addr)
96 continue;
97
98
99 if (new - TRAMPOLINE_32BIT_SIZE > bios_start)
100 break;
101
102 bios_start = new;
103 break;
104 }
105
106
107 return bios_start - TRAMPOLINE_32BIT_SIZE;
108 }
109
110 struct paging_config paging_prepare(void *rmode)
111 {
112 struct paging_config paging_config = {};
113
114
115 boot_params = rmode;
116
117
118
119
120
121
122
123
124
125
126
127
128
129 if (IS_ENABLED(CONFIG_X86_5LEVEL) &&
130 !cmdline_find_option_bool("no5lvl") &&
131 native_cpuid_eax(0) >= 7 &&
132 (native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31)))) {
133 paging_config.l5_required = 1;
134 }
135
136 paging_config.trampoline_start = find_trampoline_placement();
137
138 trampoline_32bit = (unsigned long *)paging_config.trampoline_start;
139
140
141 memcpy(trampoline_save, trampoline_32bit, TRAMPOLINE_32BIT_SIZE);
142
143
144 memset(trampoline_32bit, 0, TRAMPOLINE_32BIT_SIZE);
145
146
147 memcpy(trampoline_32bit + TRAMPOLINE_32BIT_CODE_OFFSET / sizeof(unsigned long),
148 &trampoline_32bit_src, TRAMPOLINE_32BIT_CODE_SIZE);
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 if (paging_config.l5_required == !!(native_read_cr4() & X86_CR4_LA57))
165 goto out;
166
167 if (paging_config.l5_required) {
168
169
170
171
172 trampoline_32bit[TRAMPOLINE_32BIT_PGTABLE_OFFSET] = __native_read_cr3() | _PAGE_TABLE_NOENC;
173 } else {
174 unsigned long src;
175
176
177
178
179
180
181
182
183
184 src = *(unsigned long *)__native_read_cr3() & PAGE_MASK;
185 memcpy(trampoline_32bit + TRAMPOLINE_32BIT_PGTABLE_OFFSET / sizeof(unsigned long),
186 (void *)src, PAGE_SIZE);
187 }
188
189 out:
190 return paging_config;
191 }
192
193 void cleanup_trampoline(void *pgtable)
194 {
195 void *trampoline_pgtable;
196
197 trampoline_pgtable = trampoline_32bit + TRAMPOLINE_32BIT_PGTABLE_OFFSET / sizeof(unsigned long);
198
199
200
201
202
203 if ((void *)__native_read_cr3() == trampoline_pgtable) {
204 memcpy(pgtable, trampoline_pgtable, PAGE_SIZE);
205 native_write_cr3((unsigned long)pgtable);
206 }
207
208
209 memcpy(trampoline_32bit, trampoline_save, TRAMPOLINE_32BIT_SIZE);
210 }