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  * __force_order is used by special_insns.h asm code to force instruction
10  * serialization.
11  *
12  * It is not referenced from the code, but GCC < 5 with -fPIE would fail
13  * due to an undefined symbol. Define it to make these ancient GCCs work.
14  */
15 unsigned long __force_order;
16 
17 #define BIOS_START_MIN		0x20000U	/* 128K, less than this is insane */
18 #define BIOS_START_MAX		0x9f000U	/* 640K, absolute maximum */
19 
20 struct paging_config {
21 	unsigned long trampoline_start;
22 	unsigned long l5_required;
23 };
24 
25 /* Buffer to preserve trampoline memory */
26 static char trampoline_save[TRAMPOLINE_32BIT_SIZE];
27 
28 /*
29  * Trampoline address will be printed by extract_kernel() for debugging
30  * purposes.
31  *
32  * Avoid putting the pointer into .bss as it will be cleared between
33  * paging_prepare() and extract_kernel().
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 	unsigned long trampoline_start;
44 	struct boot_e820_entry *entry;
45 	char *signature;
46 	int i;
47 
48 	/*
49 	 * Find a suitable spot for the trampoline.
50 	 * This code is based on reserve_bios_regions().
51 	 */
52 
53 	/*
54 	 * EFI systems may not provide legacy ROM. The memory may not be mapped
55 	 * at all.
56 	 *
57 	 * Only look for values in the legacy ROM for non-EFI system.
58 	 */
59 	signature = (char *)&boot_params->efi_info.efi_loader_signature;
60 	if (strncmp(signature, EFI32_LOADER_SIGNATURE, 4) &&
61 	    strncmp(signature, EFI64_LOADER_SIGNATURE, 4)) {
62 		ebda_start = *(unsigned short *)0x40e << 4;
63 		bios_start = *(unsigned short *)0x413 << 10;
64 	}
65 
66 	if (bios_start < BIOS_START_MIN || bios_start > BIOS_START_MAX)
67 		bios_start = BIOS_START_MAX;
68 
69 	if (ebda_start > BIOS_START_MIN && ebda_start < bios_start)
70 		bios_start = ebda_start;
71 
72 	bios_start = round_down(bios_start, PAGE_SIZE);
73 
74 	/* Find the first usable memory region under bios_start. */
75 	for (i = boot_params->e820_entries - 1; i >= 0; i--) {
76 		entry = &boot_params->e820_table[i];
77 
78 		/* Skip all entries above bios_start. */
79 		if (bios_start <= entry->addr)
80 			continue;
81 
82 		/* Skip non-RAM entries. */
83 		if (entry->type != E820_TYPE_RAM)
84 			continue;
85 
86 		/* Adjust bios_start to the end of the entry if needed. */
87 		if (bios_start > entry->addr + entry->size)
88 			bios_start = entry->addr + entry->size;
89 
90 		/* Keep bios_start page-aligned. */
91 		bios_start = round_down(bios_start, PAGE_SIZE);
92 
93 		/* Skip the entry if it's too small. */
94 		if (bios_start - TRAMPOLINE_32BIT_SIZE < entry->addr)
95 			continue;
96 
97 		break;
98 	}
99 
100 	/* Place the trampoline just below the end of low memory */
101 	return bios_start - TRAMPOLINE_32BIT_SIZE;
102 }
103 
104 struct paging_config paging_prepare(void *rmode)
105 {
106 	struct paging_config paging_config = {};
107 
108 	/* Initialize boot_params. Required for cmdline_find_option_bool(). */
109 	boot_params = rmode;
110 
111 	/*
112 	 * Check if LA57 is desired and supported.
113 	 *
114 	 * There are several parts to the check:
115 	 *   - if the kernel supports 5-level paging: CONFIG_X86_5LEVEL=y
116 	 *   - if user asked to disable 5-level paging: no5lvl in cmdline
117 	 *   - if the machine supports 5-level paging:
118 	 *     + CPUID leaf 7 is supported
119 	 *     + the leaf has the feature bit set
120 	 *
121 	 * That's substitute for boot_cpu_has() in early boot code.
122 	 */
123 	if (IS_ENABLED(CONFIG_X86_5LEVEL) &&
124 			!cmdline_find_option_bool("no5lvl") &&
125 			native_cpuid_eax(0) >= 7 &&
126 			(native_cpuid_ecx(7) & (1 << (X86_FEATURE_LA57 & 31)))) {
127 		paging_config.l5_required = 1;
128 	}
129 
130 	paging_config.trampoline_start = find_trampoline_placement();
131 
132 	trampoline_32bit = (unsigned long *)paging_config.trampoline_start;
133 
134 	/* Preserve trampoline memory */
135 	memcpy(trampoline_save, trampoline_32bit, TRAMPOLINE_32BIT_SIZE);
136 
137 	/* Clear trampoline memory first */
138 	memset(trampoline_32bit, 0, TRAMPOLINE_32BIT_SIZE);
139 
140 	/* Copy trampoline code in place */
141 	memcpy(trampoline_32bit + TRAMPOLINE_32BIT_CODE_OFFSET / sizeof(unsigned long),
142 			&trampoline_32bit_src, TRAMPOLINE_32BIT_CODE_SIZE);
143 
144 	/*
145 	 * The code below prepares page table in trampoline memory.
146 	 *
147 	 * The new page table will be used by trampoline code for switching
148 	 * from 4- to 5-level paging or vice versa.
149 	 *
150 	 * If switching is not required, the page table is unused: trampoline
151 	 * code wouldn't touch CR3.
152 	 */
153 
154 	/*
155 	 * We are not going to use the page table in trampoline memory if we
156 	 * are already in the desired paging mode.
157 	 */
158 	if (paging_config.l5_required == !!(native_read_cr4() & X86_CR4_LA57))
159 		goto out;
160 
161 	if (paging_config.l5_required) {
162 		/*
163 		 * For 4- to 5-level paging transition, set up current CR3 as
164 		 * the first and the only entry in a new top-level page table.
165 		 */
166 		trampoline_32bit[TRAMPOLINE_32BIT_PGTABLE_OFFSET] = __native_read_cr3() | _PAGE_TABLE_NOENC;
167 	} else {
168 		unsigned long src;
169 
170 		/*
171 		 * For 5- to 4-level paging transition, copy page table pointed
172 		 * by first entry in the current top-level page table as our
173 		 * new top-level page table.
174 		 *
175 		 * We cannot just point to the page table from trampoline as it
176 		 * may be above 4G.
177 		 */
178 		src = *(unsigned long *)__native_read_cr3() & PAGE_MASK;
179 		memcpy(trampoline_32bit + TRAMPOLINE_32BIT_PGTABLE_OFFSET / sizeof(unsigned long),
180 		       (void *)src, PAGE_SIZE);
181 	}
182 
183 out:
184 	return paging_config;
185 }
186 
187 void cleanup_trampoline(void *pgtable)
188 {
189 	void *trampoline_pgtable;
190 
191 	trampoline_pgtable = trampoline_32bit + TRAMPOLINE_32BIT_PGTABLE_OFFSET / sizeof(unsigned long);
192 
193 	/*
194 	 * Move the top level page table out of trampoline memory,
195 	 * if it's there.
196 	 */
197 	if ((void *)__native_read_cr3() == trampoline_pgtable) {
198 		memcpy(pgtable, trampoline_pgtable, PAGE_SIZE);
199 		native_write_cr3((unsigned long)pgtable);
200 	}
201 
202 	/* Restore trampoline memory */
203 	memcpy(trampoline_32bit, trampoline_save, TRAMPOLINE_32BIT_SIZE);
204 }
205