xref: /haiku/src/system/boot/platform/efi/arch/arm64/arch_start.cpp (revision 9e25244c5e9051f6cd333820d6332397361abd6c)
1 /*
2  * Copyright 2019-2022 Haiku, Inc. All rights reserved.
3  * Released under the terms of the MIT License.
4  */
5 
6 
7 #include <boot/platform.h>
8 #include <boot/stage2.h>
9 #include <boot/stdio.h>
10 
11 #include "efi_platform.h"
12 
13 #include "aarch64.h"
14 
15 extern "C" void arch_enter_kernel(struct kernel_args *kernelArgs,
16 	addr_t kernelEntry, addr_t kernelStackTop);
17 
18 extern void arch_mmu_dump_present_tables();
19 extern const char* granule_type_str(int tg);
20 
21 extern uint32_t arch_mmu_generate_post_efi_page_tables(size_t memory_map_size,
22 	efi_memory_descriptor *memory_map, size_t descriptor_size,
23 	uint32_t descriptor_version);
24 
25 extern void arch_mmu_post_efi_setup(size_t memory_map_size,
26 	efi_memory_descriptor *memory_map, size_t descriptor_size,
27 	uint32_t descriptor_version);
28 
29 extern void arch_mmu_setup_EL1();
30 
31 
32 static const char*
33 memory_region_type_str(int type)
34 {
35 	switch (type)	{
36 	case EfiReservedMemoryType:
37 		return "ReservedMemoryType";
38 	case EfiLoaderCode:
39 		return "LoaderCode";
40 	case EfiLoaderData:
41 		return "LoaderData";
42 	case EfiBootServicesCode:
43 		return "BootServicesCode";
44 	case EfiBootServicesData:
45 		return "BootServicesData";
46 	case EfiRuntimeServicesCode:
47 		return "RuntimeServicesCode";
48 	case EfiRuntimeServicesData:
49 		return "RuntimeServicesData";
50 	case EfiConventionalMemory:
51 		return "ConventionalMemory";
52 	case EfiUnusableMemory:
53 		return "UnusableMemory";
54 	case EfiACPIReclaimMemory:
55 		return "ACPIReclaimMemory";
56 	case EfiACPIMemoryNVS:
57 		return "ACPIMemoryNVS";
58 	case EfiMemoryMappedIO:
59 		return "MMIO";
60 	case EfiMemoryMappedIOPortSpace:
61 		return "MMIOPortSpace";
62 	case EfiPalCode:
63 		return "PalCode";
64 	case EfiPersistentMemory:
65 		return "PersistentMemory";
66 	default:
67 		return "unknown";
68 	}
69 }
70 
71 
72 void
73 arch_convert_kernel_args(void)
74 {
75 	// empty
76 }
77 
78 
79 void
80 arch_start_kernel(addr_t kernelEntry)
81 {
82 	// Prepare to exit EFI boot services.
83 	// Read the memory map.
84 	// First call is to determine the buffer size.
85 	size_t memory_map_size = 0;
86 	efi_memory_descriptor dummy;
87 	efi_memory_descriptor *memory_map;
88 	size_t map_key;
89 	size_t descriptor_size;
90 	uint32_t descriptor_version;
91 	if (kBootServices->GetMemoryMap(&memory_map_size, &dummy, &map_key,
92 			&descriptor_size, &descriptor_version) != EFI_BUFFER_TOO_SMALL) {
93 		panic("Unable to determine size of system memory map");
94 	}
95 
96 	// Allocate a buffer twice as large as needed just in case it gets bigger
97 	// between calls to ExitBootServices.
98 	size_t actual_memory_map_size = memory_map_size * 2;
99 	memory_map
100 		= (efi_memory_descriptor *)kernel_args_malloc(actual_memory_map_size);
101 
102 	if (memory_map == NULL)
103 		panic("Unable to allocate memory map.");
104 
105 	// Read (and print) the memory map.
106 	memory_map_size = actual_memory_map_size;
107 	if (kBootServices->GetMemoryMap(&memory_map_size, memory_map, &map_key,
108 			&descriptor_size, &descriptor_version) != EFI_SUCCESS) {
109 		panic("Unable to fetch system memory map.");
110 	}
111 
112 	addr_t addr = (addr_t)memory_map;
113 	efi_physical_addr loaderCode = 0LL;
114 	dprintf("System provided memory map:\n");
115 	for (size_t i = 0; i < memory_map_size / descriptor_size; ++i) {
116 		efi_memory_descriptor *entry
117 			= (efi_memory_descriptor *)(addr + i * descriptor_size);
118 		dprintf("  phys: 0x%0lx-0x%0lx, virt: 0x%0lx-0x%0lx, size = 0x%0lx, type: %s (%#x), attr: %#lx\n",
119 			entry->PhysicalStart,
120 			entry->PhysicalStart + entry->NumberOfPages * B_PAGE_SIZE,
121 			entry->VirtualStart,
122 			entry->VirtualStart + entry->NumberOfPages * B_PAGE_SIZE,
123 			entry->NumberOfPages * B_PAGE_SIZE,
124 			memory_region_type_str(entry->Type), entry->Type,
125 			entry->Attribute);
126 		if (entry->Type == EfiLoaderCode)
127 			loaderCode = entry->PhysicalStart;
128 	}
129 	// This is where our efi loader got relocated, therefore we need to use this
130 	// offset for properly align symbols
131 	dprintf("Efi loader symbols offset: 0x%0lx:\n", loaderCode);
132 
133 	// Generate page tables for use after ExitBootServices.
134 	arch_mmu_generate_post_efi_page_tables(
135 		memory_map_size, memory_map, descriptor_size, descriptor_version);
136 
137 	bool el2toel1 = false;
138 /*
139 *   "The AArch64 exception model is made up of a number of exception levels
140 *    (EL0 - EL3), with EL0 and EL1 having a secure and a non-secure
141 *    counterpart.  EL2 is the hypervisor level and exists only in non-secure
142 *    mode. EL3 is the highest priority level and exists only in secure mode."
143 *
144 *	"2.3 UEFI System Environment and Configuration
145 *    The resident UEFI boot-time environment shall use the highest non-secure
146 *    privilege level available. The exact meaning of this is architecture
147 *    dependent, as detailed below."
148 
149 *	"2.3.1 AArch64 Exception Levels
150 *    On AArch64 UEFI shall execute as 64-bit code at either EL1 or EL2,
151 *    depending on whether or not virtualization is available at OS load time."
152 */
153 	if (arch_exception_level() != 1) {
154 		dprintf("Current Exception Level EL%1ld\n", arch_exception_level());
155 		if (arch_exception_level() == 2) {
156 			/* Transitioning from EL we lose present MMU configuration
157 			 * which we would like to preserve e.g. peripherals mappings */
158 			if (arch_mmu_enabled()) {
159 				dprintf("MMU Enabled, Translation Table @ %lx Granularity %s, bits %d\n",
160 					arch_mmu_base_register(),
161 					granule_type_str(arch_mmu_user_granule()),
162 					arch_mmu_user_address_bits());
163 
164 				dprintf("Kernel entry accessibility W: %x R: %x\n",
165 					arch_mmu_write_access(kernelEntry),
166 					arch_mmu_read_access(kernelEntry));
167 
168 				arch_mmu_dump_present_tables();
169 
170 				el2toel1 = true; // we want to print before exit services
171 			}
172 
173 		} else {
174 			// Not ready, undexpected any transition different than EL2 >> EL1
175 			panic("Unexpected Exception Level\n");
176 		}
177 	}
178 
179 
180 	// Attempt to fetch the memory map and exit boot services.
181 	// This needs to be done in a loop, as ExitBootServices can change the
182 	// memory map.
183 	// Even better: Only GetMemoryMap and ExitBootServices can be called after
184 	// the first call to ExitBootServices, as the firmware is permitted to
185 	// partially exit. This is why twice as much space was allocated for the
186 	// memory map, as it's impossible to allocate more now.
187 	// A changing memory map shouldn't affect the generated page tables, as
188 	// they only needed to know about the maximum address, not any specific
189 	// entry.
190 	dprintf("Calling ExitBootServices. So long, EFI!\n");
191 	while (true) {
192 		if (kBootServices->ExitBootServices(kImage, map_key) == EFI_SUCCESS) {
193 			// The console was provided by boot services, disable it.
194 			stdout = NULL;
195 			stderr = NULL;
196 			// Can we adjust gKernelArgs.platform_args.serial_base_ports[0]
197 			// to something fixed in qemu for debugging?
198 			break;
199 		}
200 
201 		memory_map_size = actual_memory_map_size;
202 		if (kBootServices->GetMemoryMap(&memory_map_size, memory_map, &map_key,
203 				&descriptor_size, &descriptor_version) != EFI_SUCCESS) {
204 			panic("Unable to fetch system memory map.");
205 		}
206 	}
207 
208 	// Update EFI, generate final kernel physical memory map, etc.
209 	//arch_mmu_post_efi_setup(memory_map_size, memory_map,
210 	//		descriptor_size, descriptor_version);
211 
212 	if (el2toel1) {
213 		arch_mmu_setup_EL1();
214 		arch_cache_disable();
215 
216 		_arch_transition_EL2_EL1();
217 
218 		arch_cache_enable();
219 	}
220 
221 	//smp_boot_other_cpus(final_pml4, kernelEntry);
222 
223 	if (arch_mmu_read_access(kernelEntry) && arch_mmu_read_access(gKernelArgs.cpu_kstack[0].start)) {
224 		// Enter the kernel!
225 		arch_enter_kernel(&gKernelArgs, kernelEntry,
226 			gKernelArgs.cpu_kstack[0].start + gKernelArgs.cpu_kstack[0].size - 8);
227 	} else {
228 		_arch_exception_panic("Kernel or Stack memory not accessible\n", __LINE__);
229 	}
230 }
231