xref: /kvm-unit-tests/lib/x86/setup.c (revision 2c96b77ec9d3b1fcec7525174e23a6240ee05949)
1 /*
2  * Initialize machine setup information
3  *
4  * Copyright (C) 2017, Red Hat Inc, Andrew Jones <drjones@redhat.com>
5  * Copyright (C) 2021, Google Inc, Zixuan Wang <zixuanwang@google.com>
6  *
7  * This work is licensed under the terms of the GNU LGPL, version 2.
8  */
9 #include "libcflat.h"
10 #include "fwcfg.h"
11 #include "alloc_phys.h"
12 #include "argv.h"
13 #include "desc.h"
14 #include "apic.h"
15 #include "apic-defs.h"
16 #include "asm/setup.h"
17 
18 extern char edata;
19 
20 struct mbi_bootinfo {
21 	u32 flags;
22 	u32 mem_lower;
23 	u32 mem_upper;
24 	u32 boot_device;
25 	u32 cmdline;
26 	u32 mods_count;
27 	u32 mods_addr;
28 	u32 reserved[4];   /* 28-43 */
29 	u32 mmap_length;
30 	u32 mmap_addr;
31 	u32 reserved0[3];  /* 52-63 */
32 	u32 bootloader;
33 	u32 reserved1[5];  /* 68-87 */
34 	u32 size;
35 };
36 
37 struct mbi_module {
38 	u32 start, end;
39 	u32 cmdline;
40 	u32 unused;
41 };
42 
43 struct mbi_mem {
44 	u32 size;
45 	u64 base_addr;
46 	u64 length;
47 	u32 type;
48 } __attribute__((packed));
49 
50 #define ENV_SIZE 16384
51 
52 void setup_env(char *env, int size);
53 void setup_multiboot(struct mbi_bootinfo *bootinfo);
54 void setup_libcflat(void);
55 
56 char *initrd;
57 u32 initrd_size;
58 
59 static char env[ENV_SIZE];
60 static struct mbi_bootinfo *bootinfo;
61 
62 #define HUGEPAGE_SIZE (1 << 21)
63 
64 #ifdef __x86_64__
65 void find_highmem(void)
66 {
67 	/* Memory above 4 GB is only supported on 64-bit systems.  */
68 	if (!(bootinfo->flags & 64))
69 	    	return;
70 
71 	u64 upper_end = bootinfo->mem_upper * 1024ull;
72 	u64 best_start = (uintptr_t) &edata;
73 	u64 best_end = upper_end;
74 	u64 max_end = fwcfg_get_u64(FW_CFG_MAX_RAM);
75 	if (max_end == 0)
76 		max_end = -1ull;
77 	bool found = false;
78 
79 	uintptr_t mmap = bootinfo->mmap_addr;
80 	while (mmap < bootinfo->mmap_addr + bootinfo->mmap_length) {
81 		struct mbi_mem *mem = (void *)mmap;
82 		mmap += mem->size + 4;
83 		if (mem->type != 1)
84 			continue;
85 		if (mem->base_addr <= (uintptr_t) &edata ||
86 		    (mem->base_addr <= upper_end && mem->base_addr + mem->length <= upper_end))
87 			continue;
88 		if (mem->length < best_end - best_start)
89 			continue;
90 		if (mem->base_addr >= max_end)
91 			continue;
92 		best_start = mem->base_addr;
93 		best_end = mem->base_addr + mem->length;
94 		if (best_end > max_end)
95 			best_end = max_end;
96 		found = true;
97 	}
98 
99 	if (found) {
100 		best_start = (best_start + HUGEPAGE_SIZE - 1) & -HUGEPAGE_SIZE;
101 		best_end = best_end & -HUGEPAGE_SIZE;
102 		phys_alloc_init(best_start, best_end - best_start);
103 	}
104 }
105 
106 /* Setup TSS for the current processor, and return TSS offset within GDT */
107 unsigned long setup_tss(u8 *stacktop)
108 {
109 	u32 id;
110 	tss64_t *tss_entry;
111 
112 	id = apic_id();
113 
114 	/* Runtime address of current TSS */
115 	tss_entry = &tss[id];
116 
117 	/* Update TSS */
118 	memset((void *)tss_entry, 0, sizeof(tss64_t));
119 
120 	/* Update TSS descriptors; each descriptor takes up 2 entries */
121 	set_gdt_entry(TSS_MAIN + id * 16, (unsigned long)tss_entry, 0xffff, 0x89, 0);
122 
123 	return TSS_MAIN + id * 16;
124 }
125 #else
126 /* Setup TSS for the current processor, and return TSS offset within GDT */
127 unsigned long setup_tss(u8 *stacktop)
128 {
129 	u32 id;
130 	tss32_t *tss_entry;
131 
132 	id = apic_id();
133 
134 	/* Runtime address of current TSS */
135 	tss_entry = &tss[id];
136 
137 	/* Update TSS */
138 	memset((void *)tss_entry, 0, sizeof(tss32_t));
139 	tss_entry->ss0 = KERNEL_DS;
140 
141 	/* Update descriptors for TSS and percpu data segment.  */
142 	set_gdt_entry(TSS_MAIN + id * 8,
143 		      (unsigned long)tss_entry, 0xffff, 0x89, 0);
144 	set_gdt_entry(TSS_MAIN + MAX_TEST_CPUS * 8 + id * 8,
145 		      (unsigned long)stacktop - 4096, 0xfffff, 0x93, 0xc0);
146 
147 	return TSS_MAIN + id * 8;
148 }
149 #endif
150 
151 void setup_multiboot(struct mbi_bootinfo *bi)
152 {
153 	struct mbi_module *mods;
154 
155 	bootinfo = bi;
156 
157 	u64 best_start = (uintptr_t) &edata;
158 	u64 best_end = bootinfo->mem_upper * 1024ull;
159 	phys_alloc_init(best_start, best_end - best_start);
160 
161 	if (bootinfo->mods_count != 1)
162 		return;
163 
164 	mods = (struct mbi_module *)(uintptr_t) bootinfo->mods_addr;
165 
166 	initrd = (char *)(uintptr_t) mods->start;
167 	initrd_size = mods->end - mods->start;
168 }
169 
170 #ifdef TARGET_EFI
171 
172 /* From x86/efi/efistart64.S */
173 extern void load_idt(void);
174 extern void load_gdt_tss(size_t tss_offset);
175 
176 static efi_status_t setup_memory_allocator(efi_bootinfo_t *efi_bootinfo)
177 {
178 	int i;
179 	unsigned long free_mem_pages = 0;
180 	unsigned long free_mem_start = 0;
181 	struct efi_boot_memmap *map = &(efi_bootinfo->mem_map);
182 	efi_memory_desc_t *buffer = *map->map;
183 	efi_memory_desc_t *d = NULL;
184 
185 	/*
186 	 * The 'buffer' contains multiple descriptors that describe memory
187 	 * regions maintained by UEFI. This code records the largest free
188 	 * EFI_CONVENTIONAL_MEMORY region which will be used to set up the
189 	 * memory allocator, so that the memory allocator can work in the
190 	 * largest free continuous memory region.
191 	 */
192 	for (i = 0; i < *(map->map_size); i += *(map->desc_size)) {
193 		d = (efi_memory_desc_t *)(&((u8 *)buffer)[i]);
194 		if (d->type == EFI_CONVENTIONAL_MEMORY) {
195 			if (free_mem_pages < d->num_pages) {
196 				free_mem_pages = d->num_pages;
197 				free_mem_start = d->phys_addr;
198 			}
199 		}
200 	}
201 
202 	if (free_mem_pages == 0) {
203 		return EFI_OUT_OF_RESOURCES;
204 	}
205 
206 	phys_alloc_init(free_mem_start, free_mem_pages << EFI_PAGE_SHIFT);
207 
208 	return EFI_SUCCESS;
209 }
210 
211 static efi_status_t setup_rsdp(efi_bootinfo_t *efi_bootinfo)
212 {
213 	efi_status_t status;
214 	struct rsdp_descriptor *rsdp;
215 
216 	/*
217 	 * RSDP resides in an EFI_ACPI_RECLAIM_MEMORY region, which is not used
218 	 * by kvm-unit-tests x86's memory allocator. So it is not necessary to
219 	 * copy the data structure to another memory region to prevent
220 	 * unintentional overwrite.
221 	 */
222 	status = efi_get_system_config_table(ACPI_TABLE_GUID, (void **)&rsdp);
223 	if (status != EFI_SUCCESS) {
224 		return status;
225 	}
226 
227 	set_efi_rsdp(rsdp);
228 
229 	return EFI_SUCCESS;
230 }
231 
232 /* Defined in cstart64.S or efistart64.S */
233 extern u8 ptl4;
234 extern u8 ptl3;
235 extern u8 ptl2;
236 
237 static void setup_page_table(void)
238 {
239 	pgd_t *curr_pt;
240 	phys_addr_t flags;
241 	int i;
242 
243 	/* Set default flags */
244 	flags = PT_PRESENT_MASK | PT_WRITABLE_MASK | PT_USER_MASK;
245 
246 	/* Set AMD SEV C-Bit for page table entries */
247 	flags |= get_amd_sev_c_bit_mask();
248 
249 	/* Level 4 */
250 	curr_pt = (pgd_t *)&ptl4;
251 	curr_pt[0] = ((phys_addr_t)&ptl3) | flags;
252 	/* Level 3 */
253 	curr_pt = (pgd_t *)&ptl3;
254 	for (i = 0; i < 4; i++) {
255 		curr_pt[i] = (((phys_addr_t)&ptl2) + i * PAGE_SIZE) | flags;
256 	}
257 	/* Level 2 */
258 	curr_pt = (pgd_t *)&ptl2;
259 	flags |= PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_PAGE_SIZE_MASK | PT_GLOBAL_MASK;
260 	for (i = 0; i < 4 * 512; i++)	{
261 		curr_pt[i] = ((phys_addr_t)(i << 21)) | flags;
262 	}
263 
264 	if (amd_sev_es_enabled()) {
265 		setup_ghcb_pte((pgd_t *)&ptl4);
266 	}
267 
268 	/* Load 4-level page table */
269 	write_cr3((ulong)&ptl4);
270 }
271 
272 static void setup_gdt_tss(void)
273 {
274 	size_t tss_offset;
275 
276 	/* 64-bit setup_tss does not use the stacktop argument.  */
277 	tss_offset = setup_tss(NULL);
278 	load_gdt_tss(tss_offset);
279 }
280 
281 efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
282 {
283 	efi_status_t status;
284 	const char *phase;
285 
286 	status = setup_memory_allocator(efi_bootinfo);
287 	if (status != EFI_SUCCESS) {
288 		printf("Failed to set up memory allocator: ");
289 		switch (status) {
290 		case EFI_OUT_OF_RESOURCES:
291 			printf("No free memory region\n");
292 			break;
293 		default:
294 			printf("Unknown error\n");
295 			break;
296 		}
297 		return status;
298 	}
299 
300 	status = setup_rsdp(efi_bootinfo);
301 	if (status != EFI_SUCCESS) {
302 		printf("Cannot find RSDP in EFI system table\n");
303 		return status;
304 	}
305 
306 	phase = "AMD SEV";
307 	status = setup_amd_sev();
308 
309 	/* Continue if AMD SEV is not supported, but skip SEV-ES setup */
310 	if (status == EFI_SUCCESS) {
311 		phase = "AMD SEV-ES";
312 		status = setup_amd_sev_es();
313 	}
314 
315 	if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED) {
316 		printf("%s setup failed, error = 0x%lx\n", phase, status);
317 		return status;
318 	}
319 
320 	reset_apic();
321 	setup_gdt_tss();
322 	setup_idt();
323 	load_idt();
324 	mask_pic_interrupts();
325 	enable_apic();
326 	enable_x2apic();
327 	smp_init();
328 	setup_page_table();
329 
330 	return EFI_SUCCESS;
331 }
332 
333 #endif /* TARGET_EFI */
334 
335 void setup_libcflat(void)
336 {
337 	if (initrd) {
338 		/* environ is currently the only file in the initrd */
339 		u32 size = MIN(initrd_size, ENV_SIZE);
340 		const char *str;
341 
342 		memcpy(env, initrd, size);
343 		setup_env(env, size);
344 		if ((str = getenv("BOOTLOADER")) && atol(str) != 0)
345 			add_setup_arg("bootloader");
346 	}
347 }
348