1 /* 2 * Copyright 2016-2020 Haiku, Inc. All rights reserved. 3 * Copyright 2014, Jessica Hamilton, jessica.l.hamilton@gmail.com. 4 * Copyright 2014, Henry Harrington, henry.harrington@gmail.com. 5 * Distributed under the terms of the MIT License. 6 */ 7 8 9 #include <algorithm> 10 11 #include <boot/addr_range.h> 12 #include <boot/platform.h> 13 #include <boot/stage2.h> 14 #include <kernel/kernel.h> 15 16 #include "efi_platform.h" 17 #include "mmu.h" 18 19 20 //#define TRACE_MMU 21 #ifdef TRACE_MMU 22 # define TRACE(x...) dprintf("efi/mmu: " x) 23 #else 24 # define TRACE(x...) ; 25 #endif 26 27 28 struct memory_region { 29 memory_region *next; 30 addr_t vaddr; 31 phys_addr_t paddr; 32 size_t size; 33 34 void dprint(const char * msg) { 35 dprintf("%s memory_region v: %#lx p: %#lx size: %lu\n", msg, vaddr, 36 paddr, size); 37 } 38 39 bool matches(phys_addr_t expected_paddr, size_t expected_size) { 40 return paddr == expected_paddr && size == expected_size; 41 } 42 }; 43 44 45 #if defined(KERNEL_LOAD_BASE_64_BIT) 46 static addr_t sNextVirtualAddress = KERNEL_LOAD_BASE_64_BIT + 32 * 1024 * 1024; 47 #elif defined(KERNEL_LOAD_BASE) 48 static addr_t sNextVirtualAddress = KERNEL_LOAD_BASE + 32 * 1024 * 1024; 49 #else 50 #error Unable to find kernel load base on this architecture! 51 #endif 52 53 54 static memory_region *allocated_regions = NULL; 55 56 57 extern "C" phys_addr_t 58 mmu_allocate_page() 59 { 60 TRACE("%s: called\n", __func__); 61 62 efi_physical_addr addr; 63 efi_status s = kBootServices->AllocatePages(AllocateAnyPages, 64 EfiLoaderData, 1, &addr); 65 66 if (s != EFI_SUCCESS) 67 panic("Unabled to allocate memory: %li", s); 68 69 return addr; 70 } 71 72 73 extern "C" addr_t 74 get_next_virtual_address(size_t size) 75 { 76 TRACE("%s: called. size: %" B_PRIuSIZE "\n", __func__, size); 77 78 addr_t address = sNextVirtualAddress; 79 sNextVirtualAddress += ROUNDUP(size, B_PAGE_SIZE); 80 return address; 81 } 82 83 84 extern "C" addr_t 85 get_current_virtual_address() 86 { 87 TRACE("%s: called\n", __func__); 88 return sNextVirtualAddress; 89 } 90 91 92 // Platform allocator. 93 // The bootloader assumes that bootloader address space == kernel address space. 94 // This is not true until just before the kernel is booted, so an ugly hack is 95 // used to cover the difference. platform_allocate_region allocates addresses 96 // in bootloader space, but can convert them to kernel space. The ELF loader 97 // accesses kernel memory via Mao(), and much later in the boot process, 98 // addresses in the kernel argument struct are converted from bootloader 99 // addresses to kernel addresses. 100 101 extern "C" status_t 102 platform_allocate_region(void **_address, size_t size, uint8 /* protection */, 103 bool exactAddress) 104 { 105 TRACE("%s: called\n", __func__); 106 107 // We don't have any control over the page tables, give up right away if an 108 // exactAddress is wanted. 109 if (exactAddress) 110 return B_NO_MEMORY; 111 112 efi_physical_addr addr; 113 size_t pages = ROUNDUP(size, B_PAGE_SIZE) / B_PAGE_SIZE; 114 efi_status status = kBootServices->AllocatePages(AllocateAnyPages, 115 EfiLoaderData, pages, &addr); 116 if (status != EFI_SUCCESS) 117 return B_NO_MEMORY; 118 119 // Addresses above 512GB not supported. 120 // Memory map regions above 512GB can be ignored, but if EFI returns pages 121 // above that there's nothing that can be done to fix it. 122 if (addr + size > (512ull * 1024 * 1024 * 1024)) 123 panic("Can't currently support more than 512GB of RAM!"); 124 125 memory_region *region = new(std::nothrow) memory_region { 126 next: allocated_regions, 127 vaddr: *_address == NULL ? 0 : (addr_t)*_address, 128 paddr: (phys_addr_t)addr, 129 size: size 130 }; 131 132 if (region == NULL) { 133 kBootServices->FreePages(addr, pages); 134 return B_NO_MEMORY; 135 } 136 //region->dprint("Allocated"); 137 allocated_regions = region; 138 *_address = (void *)region->paddr; 139 return B_OK; 140 } 141 142 143 /*! 144 Neither \a virtualAddress nor \a size need to be aligned, but the function 145 will map all pages the range intersects with. 146 If physicalAddress is not page-aligned, the returned virtual address will 147 have the same "misalignment". 148 */ 149 extern "C" addr_t 150 mmu_map_physical_memory(addr_t physicalAddress, size_t size, uint32 flags) 151 { 152 TRACE("%s: called\n", __func__); 153 154 addr_t pageOffset = physicalAddress & (B_PAGE_SIZE - 1); 155 156 physicalAddress -= pageOffset; 157 size += pageOffset; 158 159 if (insert_physical_allocated_range(physicalAddress, 160 ROUNDUP(size, B_PAGE_SIZE)) != B_OK) 161 return B_NO_MEMORY; 162 163 return physicalAddress + pageOffset; 164 } 165 166 167 static void 168 convert_physical_ranges() 169 { 170 TRACE("%s: called\n", __func__); 171 172 addr_range *range = gKernelArgs.physical_allocated_range; 173 uint32 num_ranges = gKernelArgs.num_physical_allocated_ranges; 174 175 for (uint32 i = 0; i < num_ranges; ++i) { 176 // Addresses above 512GB not supported. 177 // Memory map regions above 512GB can be ignored, but if EFI returns 178 // pages above that there's nothing that can be done to fix it. 179 if (range[i].start + range[i].size > (512ull * 1024 * 1024 * 1024)) 180 panic("Can't currently support more than 512GB of RAM!"); 181 182 memory_region *region = new(std::nothrow) memory_region { 183 next: allocated_regions, 184 vaddr: 0, 185 paddr: (phys_addr_t)range[i].start, 186 size: (size_t)range[i].size 187 }; 188 189 if (!region) 190 panic("Couldn't add allocated region"); 191 192 allocated_regions = region; 193 194 // Clear out the allocated range 195 range[i].start = 0; 196 range[i].size = 0; 197 gKernelArgs.num_physical_allocated_ranges--; 198 } 199 } 200 201 202 extern "C" status_t 203 platform_bootloader_address_to_kernel_address(void *address, addr_t *_result) 204 { 205 TRACE("%s: called\n", __func__); 206 207 // Convert any physical ranges prior to looking up address 208 convert_physical_ranges(); 209 210 phys_addr_t addr = (phys_addr_t)address; 211 212 for (memory_region *region = allocated_regions; region; 213 region = region->next) { 214 if (region->paddr <= addr && addr < region->paddr + region->size) { 215 // Lazily allocate virtual memory. 216 if (region->vaddr == 0) { 217 region->vaddr = get_next_virtual_address(region->size); 218 } 219 *_result = region->vaddr + (addr - region->paddr); 220 //dprintf("Converted bootloader address %p in region %#lx-%#lx to %#lx\n", 221 // address, region->paddr, region->paddr + region->size, *_result); 222 return B_OK; 223 } 224 } 225 226 return B_ERROR; 227 } 228 229 230 extern "C" status_t 231 platform_kernel_address_to_bootloader_address(addr_t address, void **_result) 232 { 233 TRACE("%s: called\n", __func__); 234 235 for (memory_region *region = allocated_regions; region; 236 region = region->next) { 237 if (region->vaddr != 0 && region->vaddr <= address 238 && address < region->vaddr + region->size) { 239 *_result = (void *)(region->paddr + (address - region->vaddr)); 240 //dprintf("Converted kernel address %#lx in region %#lx-%#lx to %p\n", 241 // address, region->vaddr, region->vaddr + region->size, *_result); 242 return B_OK; 243 } 244 } 245 246 return B_ERROR; 247 } 248 249 250 extern "C" status_t 251 platform_free_region(void *address, size_t size) 252 { 253 TRACE("%s: called to release region %p (%" B_PRIuSIZE ")\n", __func__, 254 address, size); 255 256 for (memory_region **ref = &allocated_regions; *ref; 257 ref = &(*ref)->next) { 258 if ((*ref)->matches((phys_addr_t)address, size)) { 259 kBootServices->FreePages((efi_physical_addr)address, 260 ROUNDUP(size, B_PAGE_SIZE) / B_PAGE_SIZE); 261 memory_region* old = *ref; 262 //pointer to current allocated_memory_region* now points to next 263 *ref = (*ref)->next; 264 old->dprint("Freeing"); 265 delete old; 266 return B_OK; 267 } 268 } 269 panic("platform_free_region: Unknown region to free??"); 270 return B_ERROR; // NOT Reached 271 } 272