xref: /haiku/src/system/boot/platform/efi/mmu.cpp (revision f6373f2244fd555c134f3266657ffe8b540da397)
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: %#" B_PRIxADDR " p: %#" B_PRIxPHYSADDR " 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 static addr_t sNextVirtualAddress = KERNEL_LOAD_BASE + 32 * 1024 * 1024;
46 static memory_region *allocated_regions = NULL;
47 
48 
49 extern "C" phys_addr_t
50 mmu_allocate_page()
51 {
52 	TRACE("%s: called\n", __func__);
53 
54 	efi_physical_addr addr;
55 	efi_status s = kBootServices->AllocatePages(AllocateAnyPages,
56 		EfiLoaderData, 1, &addr);
57 
58 	if (s != EFI_SUCCESS)
59 		panic("Unabled to allocate memory: %li", s);
60 
61 	return addr;
62 }
63 
64 
65 extern "C" addr_t
66 get_next_virtual_address(size_t size)
67 {
68 	TRACE("%s: called. size: %" B_PRIuSIZE "\n", __func__, size);
69 
70 	addr_t address = sNextVirtualAddress;
71 	sNextVirtualAddress += ROUNDUP(size, B_PAGE_SIZE);
72 	return address;
73 }
74 
75 
76 extern "C" addr_t
77 get_current_virtual_address()
78 {
79 	TRACE("%s: called\n", __func__);
80 	return sNextVirtualAddress;
81 }
82 
83 
84 // Platform allocator.
85 // The bootloader assumes that bootloader address space == kernel address space.
86 // This is not true until just before the kernel is booted, so an ugly hack is
87 // used to cover the difference. platform_allocate_region allocates addresses
88 // in bootloader space, but can convert them to kernel space. The ELF loader
89 // accesses kernel memory via Mao(), and much later in the boot process,
90 // addresses in the kernel argument struct are converted from bootloader
91 // addresses to kernel addresses.
92 
93 extern "C" status_t
94 platform_allocate_region(void **_address, size_t size, uint8 /* protection */,
95 	bool exactAddress)
96 {
97 	TRACE("%s: called\n", __func__);
98 
99 	// We don't have any control over the page tables, give up right away if an
100 	// exactAddress is wanted.
101 	if (exactAddress)
102 		return B_NO_MEMORY;
103 
104 	efi_physical_addr addr;
105 	size_t pages = ROUNDUP(size, B_PAGE_SIZE) / B_PAGE_SIZE;
106 	efi_status status = kBootServices->AllocatePages(AllocateAnyPages,
107 		EfiLoaderData, pages, &addr);
108 	if (status != EFI_SUCCESS)
109 		return B_NO_MEMORY;
110 
111 	// Addresses above 512GB not supported.
112 	// Memory map regions above 512GB can be ignored, but if EFI returns pages
113 	// above that there's nothing that can be done to fix it.
114 	if (addr + size > (512ull * 1024 * 1024 * 1024))
115 		panic("Can't currently support more than 512GB of RAM!");
116 
117 	memory_region *region = new(std::nothrow) memory_region {
118 		next: allocated_regions,
119 #ifdef __riscv
120 		// Disables allocation at fixed virtual address
121 		vaddr: 0,
122 #else
123 		vaddr: *_address == NULL ? 0 : (addr_t)*_address,
124 #endif
125 		paddr: (phys_addr_t)addr,
126 		size: size
127 	};
128 
129 	if (region == NULL) {
130 		kBootServices->FreePages(addr, pages);
131 		return B_NO_MEMORY;
132 	}
133 
134 #ifdef TRACE_MMU
135 	//region->dprint("Allocated");
136 #endif
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 #ifdef TRACE_MMU
265 			old->dprint("Freeing");
266 #endif
267 			delete old;
268 			return B_OK;
269 		}
270 	}
271 	panic("platform_free_region: Unknown region to free??");
272 	return B_ERROR; // NOT Reached
273 }
274 
275 
276 bool
277 mmu_next_region(void** cookie, addr_t* vaddr, phys_addr_t* paddr, size_t* size)
278 {
279 	if (*cookie == NULL)
280 		*cookie = allocated_regions;
281 	else
282 		*cookie = ((memory_region*)*cookie)->next;
283 
284 	memory_region* region = (memory_region*)*cookie;
285 	if (region == NULL)
286 		return false;
287 
288 	if (region->vaddr == 0)
289 		region->vaddr = get_next_virtual_address(region->size);
290 
291 	*vaddr = region->vaddr;
292 	*paddr = region->paddr;
293 	*size = region->size;
294 	return true;
295 }
296