1 /* 2 * Copyright 2019-2020 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 14 extern "C" void arch_enter_kernel(struct kernel_args *kernelArgs, 15 addr_t kernelEntry, addr_t kernelStackTop); 16 17 void 18 arch_start_kernel(addr_t kernelEntry) 19 { 20 // Prepare to exit EFI boot services. 21 // Read the memory map. 22 // First call is to determine the buffer size. 23 size_t memory_map_size = 0; 24 efi_memory_descriptor dummy; 25 efi_memory_descriptor *memory_map; 26 size_t map_key; 27 size_t descriptor_size; 28 uint32_t descriptor_version; 29 if (kBootServices->GetMemoryMap(&memory_map_size, &dummy, &map_key, 30 &descriptor_size, &descriptor_version) != EFI_BUFFER_TOO_SMALL) { 31 panic("Unable to determine size of system memory map"); 32 } 33 34 // Allocate a buffer twice as large as needed just in case it gets bigger 35 // between calls to ExitBootServices. 36 size_t actual_memory_map_size = memory_map_size * 2; 37 memory_map 38 = (efi_memory_descriptor *)kernel_args_malloc(actual_memory_map_size); 39 40 if (memory_map == NULL) 41 panic("Unable to allocate memory map."); 42 43 // Read (and print) the memory map. 44 memory_map_size = actual_memory_map_size; 45 if (kBootServices->GetMemoryMap(&memory_map_size, memory_map, &map_key, 46 &descriptor_size, &descriptor_version) != EFI_SUCCESS) { 47 panic("Unable to fetch system memory map."); 48 } 49 50 addr_t addr = (addr_t)memory_map; 51 dprintf("System provided memory map:\n"); 52 for (size_t i = 0; i < memory_map_size / descriptor_size; ++i) { 53 efi_memory_descriptor *entry 54 = (efi_memory_descriptor *)(addr + i * descriptor_size); 55 dprintf(" %#lx-%#lx %#lx %#x %#lx\n", entry->PhysicalStart, 56 entry->PhysicalStart + entry->NumberOfPages * B_PAGE_SIZE, 57 entry->VirtualStart, entry->Type, entry->Attribute); 58 } 59 60 // Attempt to fetch the memory map and exit boot services. 61 // This needs to be done in a loop, as ExitBootServices can change the 62 // memory map. 63 // Even better: Only GetMemoryMap and ExitBootServices can be called after 64 // the first call to ExitBootServices, as the firmware is permitted to 65 // partially exit. This is why twice as much space was allocated for the 66 // memory map, as it's impossible to allocate more now. 67 // A changing memory map shouldn't affect the generated page tables, as 68 // they only needed to know about the maximum address, not any specific 69 // entry. 70 dprintf("Calling ExitBootServices. So long, EFI!\n"); 71 while (true) { 72 if (kBootServices->ExitBootServices(kImage, map_key) == EFI_SUCCESS) { 73 // The console was provided by boot services, disable it. 74 stdout = NULL; 75 stderr = NULL; 76 // Can we adjust gKernelArgs.platform_args.serial_base_ports[0] 77 // to something fixed in qemu for debugging? 78 break; 79 } 80 81 memory_map_size = actual_memory_map_size; 82 if (kBootServices->GetMemoryMap(&memory_map_size, memory_map, &map_key, 83 &descriptor_size, &descriptor_version) != EFI_SUCCESS) { 84 panic("Unable to fetch system memory map."); 85 } 86 } 87 88 // Update EFI, generate final kernel physical memory map, etc. 89 //arch_mmu_post_efi_setup(memory_map_size, memory_map, 90 // descriptor_size, descriptor_version); 91 92 //smp_boot_other_cpus(final_pml4, kernelEntry); 93 94 // Enter the kernel! 95 arch_enter_kernel(&gKernelArgs, kernelEntry, 96 gKernelArgs.cpu_kstack[0].start + gKernelArgs.cpu_kstack[0].size); 97 } 98