loadfile_machdep.c revision 1.1 1 1.1 thorpej /* $NetBSD: loadfile_machdep.c,v 1.1 2021/02/28 20:27:40 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include "boot.h"
33 1.1 thorpej
34 1.1 thorpej #include <sys/param.h>
35 1.1 thorpej #include <sys/boot_flag.h>
36 1.1 thorpej #include <sys/disklabel.h>
37 1.1 thorpej
38 1.1 thorpej #include <lib/libsa/stand.h>
39 1.1 thorpej #include <lib/libsa/loadfile.h>
40 1.1 thorpej #include <lib/libkern/libkern.h>
41 1.1 thorpej
42 1.1 thorpej #include "openfirm.h"
43 1.1 thorpej
44 1.1 thorpej #ifdef KMAPPING_DEBUG
45 1.1 thorpej #define DPRINTF printf
46 1.1 thorpej #else
47 1.1 thorpej #define DPRINTF while (0) printf
48 1.1 thorpej #endif
49 1.1 thorpej
50 1.1 thorpej static int
51 1.1 thorpej ofw_claimphys(paddr_t pa, vsize_t size)
52 1.1 thorpej {
53 1.1 thorpej /* (2) phys, (2) size, (1) align -> 2 (phys) */
54 1.1 thorpej uint32_t cells[2+2+1+2];
55 1.1 thorpej uint32_t *p = cells;
56 1.1 thorpej paddr_t result;
57 1.1 thorpej
58 1.1 thorpej if (ofw_address_cells == 2) {
59 1.1 thorpej /* order of cells is phys.lo ... phys.hi */
60 1.1 thorpej *p++ = (uint32_t)pa;
61 1.1 thorpej *p++ = ((uint64_t)pa) >> 32;
62 1.1 thorpej } else {
63 1.1 thorpej *p++ = (uint32_t)pa;
64 1.1 thorpej }
65 1.1 thorpej
66 1.1 thorpej #if 0 /* No known Mac systems with 2, and spec is ambiguous about order. */
67 1.1 thorpej if (ofw_size_cells == 2) {
68 1.1 thorpej *p++ = ((uint64_t)size) >> 32;
69 1.1 thorpej *p++ = (uint32_t)size;
70 1.1 thorpej } else
71 1.1 thorpej #endif
72 1.1 thorpej *p++ = (uint32_t)size;
73 1.1 thorpej
74 1.1 thorpej *p++ = 0; /* align */
75 1.1 thorpej
76 1.1 thorpej if (OF_call_method("claim", ofw_memory_ihandle,
77 1.1 thorpej ofw_address_cells + ofw_size_cells + 1,
78 1.1 thorpej ofw_address_cells, (int *)cells) == -1) {
79 1.1 thorpej return -1;
80 1.1 thorpej }
81 1.1 thorpej
82 1.1 thorpej if (ofw_address_cells == 2) {
83 1.1 thorpej /* order of cells is base.lo ... base.hi */
84 1.1 thorpej uint64_t v;
85 1.1 thorpej v = *p++;
86 1.1 thorpej v |= (uint64_t)(*p++) << 32;
87 1.1 thorpej result = (paddr_t)v;
88 1.1 thorpej } else {
89 1.1 thorpej result = *p++;
90 1.1 thorpej }
91 1.1 thorpej
92 1.1 thorpej if (result != pa) {
93 1.1 thorpej printf("!!! CLAIM PHYS 0x%lx RETURNED 0x%lx\n",
94 1.1 thorpej pa, result);
95 1.1 thorpej return -1;
96 1.1 thorpej }
97 1.1 thorpej
98 1.1 thorpej return 0;
99 1.1 thorpej }
100 1.1 thorpej
101 1.1 thorpej static int
102 1.1 thorpej ofw_releasephys(paddr_t pa, vsize_t size)
103 1.1 thorpej {
104 1.1 thorpej /* (2) phys, (2) size, -> nil */
105 1.1 thorpej uint32_t cells[2+2];
106 1.1 thorpej uint32_t *p = cells;
107 1.1 thorpej
108 1.1 thorpej if (ofw_address_cells == 2) {
109 1.1 thorpej /* order of cells is phys.lo ... phys.hi */
110 1.1 thorpej *p++ = (uint32_t)pa;
111 1.1 thorpej *p++ = ((uint64_t)pa) >> 32;
112 1.1 thorpej } else {
113 1.1 thorpej *p++ = (uint32_t)pa;
114 1.1 thorpej }
115 1.1 thorpej
116 1.1 thorpej #if 0 /* No known Mac systems with 2, and spec is ambiguous about order. */
117 1.1 thorpej if (ofw_size_cells == 2) {
118 1.1 thorpej *p++ = ((uint64_t)size) >> 32;
119 1.1 thorpej *p++ = (uint32_t)size;
120 1.1 thorpej } else
121 1.1 thorpej #endif
122 1.1 thorpej *p++ = (uint32_t)size;
123 1.1 thorpej
124 1.1 thorpej if (OF_call_method("release", ofw_memory_ihandle,
125 1.1 thorpej ofw_address_cells + ofw_size_cells,
126 1.1 thorpej 0, (int *)cells) == -1) {
127 1.1 thorpej return -1;
128 1.1 thorpej }
129 1.1 thorpej
130 1.1 thorpej return 0;
131 1.1 thorpej }
132 1.1 thorpej
133 1.1 thorpej static int
134 1.1 thorpej ofw_claimvirt(vaddr_t va, vsize_t size)
135 1.1 thorpej {
136 1.1 thorpej /* (1) virt, (1) size, (1) align -> (1) virt */
137 1.1 thorpej uint32_t cells[1+1+1+1];
138 1.1 thorpej uint32_t *p = cells;
139 1.1 thorpej vaddr_t result;
140 1.1 thorpej
141 1.1 thorpej *p++ = va;
142 1.1 thorpej *p++ = size;
143 1.1 thorpej *p++ = 0; /* align */
144 1.1 thorpej
145 1.1 thorpej if (OF_call_method("claim", ofw_mmu_ihandle,
146 1.1 thorpej 3, 1, (int *)cells) == -1) {
147 1.1 thorpej return -1;
148 1.1 thorpej }
149 1.1 thorpej
150 1.1 thorpej result = *p++;
151 1.1 thorpej
152 1.1 thorpej if (result != va) {
153 1.1 thorpej printf("!!! CLAIM VIRT 0x%lx RETURNED 0x%lx\n",
154 1.1 thorpej va, result);
155 1.1 thorpej return -1;
156 1.1 thorpej }
157 1.1 thorpej
158 1.1 thorpej return 0;
159 1.1 thorpej }
160 1.1 thorpej
161 1.1 thorpej static int
162 1.1 thorpej ofw_releasevirt(vaddr_t va, vsize_t size)
163 1.1 thorpej {
164 1.1 thorpej /* (1) virt, (1) size -> nil */
165 1.1 thorpej uint32_t cells[1+1];
166 1.1 thorpej uint32_t *p = cells;
167 1.1 thorpej
168 1.1 thorpej *p++ = va;
169 1.1 thorpej *p++ = size;
170 1.1 thorpej
171 1.1 thorpej if (OF_call_method("release", ofw_mmu_ihandle,
172 1.1 thorpej 2, 0, (int *)cells) == -1) {
173 1.1 thorpej return -1;
174 1.1 thorpej }
175 1.1 thorpej
176 1.1 thorpej return 0;
177 1.1 thorpej }
178 1.1 thorpej
179 1.1 thorpej static int
180 1.1 thorpej ofw_map(vaddr_t va, paddr_t pa, vsize_t size)
181 1.1 thorpej {
182 1.1 thorpej /* (2) phys, (1) virt, (1) size, (1) mode -> nil */
183 1.1 thorpej uint32_t cells[2+1+1+1];
184 1.1 thorpej uint32_t *p = cells;
185 1.1 thorpej
186 1.1 thorpej if (ofw_address_cells == 2) {
187 1.1 thorpej /* order of cells is phys.lo ... phys.hi */
188 1.1 thorpej *p++ = (uint32_t)pa;
189 1.1 thorpej *p++ = ((uint64_t)pa) >> 32;
190 1.1 thorpej } else {
191 1.1 thorpej *p++ = (uint32_t)pa;
192 1.1 thorpej }
193 1.1 thorpej
194 1.1 thorpej *p++ = va;
195 1.1 thorpej *p++ = size;
196 1.1 thorpej *p++ = 0x00000010 /* PTE_SO | PTE_M */; /* XXX magic numbers */
197 1.1 thorpej
198 1.1 thorpej if (OF_call_method("map", ofw_mmu_ihandle,
199 1.1 thorpej ofw_address_cells + 3, 0, (int *)cells) == -1) {
200 1.1 thorpej return -1;
201 1.1 thorpej }
202 1.1 thorpej
203 1.1 thorpej return 0;
204 1.1 thorpej }
205 1.1 thorpej
206 1.1 thorpej static int
207 1.1 thorpej ofw_create_mapping(vaddr_t va, paddr_t pa, vsize_t size)
208 1.1 thorpej {
209 1.1 thorpej if (ofw_claimphys(pa, size) == -1) {
210 1.1 thorpej printf("!!! FAILED TO CLAIM PHYS 0x%lx size 0x%lx\n",
211 1.1 thorpej pa, size);
212 1.1 thorpej return -1;
213 1.1 thorpej }
214 1.1 thorpej
215 1.1 thorpej /*
216 1.1 thorpej * If we're running in real-mode, the claimphys is enough.
217 1.1 thorpej */
218 1.1 thorpej if (ofw_real_mode) {
219 1.1 thorpej return 0;
220 1.1 thorpej }
221 1.1 thorpej
222 1.1 thorpej if (ofw_claimvirt(va, size) == -1) {
223 1.1 thorpej printf("!!! FAILED TO CLAIM VIRT 0x%lx size 0x%lx\n",
224 1.1 thorpej va, size);
225 1.1 thorpej ofw_releasephys(pa, size);
226 1.1 thorpej return -1;
227 1.1 thorpej }
228 1.1 thorpej if (ofw_map(va, pa, size) == -1) {
229 1.1 thorpej printf("!!! FAILED TO MAP PHYS 0x%lx @ 0x%lx size 0x%lx\n",
230 1.1 thorpej pa, va, size);
231 1.1 thorpej ofw_releasevirt(va, size);
232 1.1 thorpej ofw_releasephys(pa, size);
233 1.1 thorpej return -1;
234 1.1 thorpej }
235 1.1 thorpej
236 1.1 thorpej return 0;
237 1.1 thorpej }
238 1.1 thorpej
239 1.1 thorpej /*
240 1.1 thorpej * This structure describes a physically contiguous mapping
241 1.1 thorpej * for the loaded kernel. We assume VA==PA, which would be
242 1.1 thorpej * equivalent to running in real-mode.
243 1.1 thorpej */
244 1.1 thorpej #define MAX_KMAPPINGS 64
245 1.1 thorpej struct kmapping {
246 1.1 thorpej vaddr_t vstart;
247 1.1 thorpej vaddr_t vend;
248 1.1 thorpej } kmappings[MAX_KMAPPINGS];
249 1.1 thorpej
250 1.1 thorpej #define TRUNC_PAGE(x) ((x) & ~((unsigned long)NBPG - 1))
251 1.1 thorpej #define ROUND_PAGE(x) TRUNC_PAGE((x) + (NBPG - 1))
252 1.1 thorpej
253 1.1 thorpej /*
254 1.1 thorpej * Enter a mapping for the loaded kernel. If the start is within an
255 1.1 thorpej * already mapped region, or if it starts immediately following a
256 1.1 thorpej * previous region, we extend it.
257 1.1 thorpej */
258 1.1 thorpej static int
259 1.1 thorpej kmapping_enter(vaddr_t va, vsize_t size)
260 1.1 thorpej {
261 1.1 thorpej struct kmapping *km, *km_prev, *km_next, *km_last;
262 1.1 thorpej vaddr_t va_end;
263 1.1 thorpej int i;
264 1.1 thorpej
265 1.1 thorpej km_last = &kmappings[MAX_KMAPPINGS - 1];
266 1.1 thorpej
267 1.1 thorpej /* Round the region to a page. */
268 1.1 thorpej va_end = ROUND_PAGE(va + size);
269 1.1 thorpej
270 1.1 thorpej /* Truncate the region to the nearest page boundary. */
271 1.1 thorpej va = TRUNC_PAGE(va);
272 1.1 thorpej
273 1.1 thorpej /* Get the rounded size. */
274 1.1 thorpej size = va_end - va;
275 1.1 thorpej
276 1.1 thorpej DPRINTF("kmapping_enter: va=0x%lx size=0x%lx\n",
277 1.1 thorpej va, size);
278 1.1 thorpej
279 1.1 thorpej /* Check to see if there's an overlapping entry in the table. */
280 1.1 thorpej for (i = 0, km_prev = NULL; i < MAX_KMAPPINGS; i++, km_prev = km) {
281 1.1 thorpej km = &kmappings[i];
282 1.1 thorpej km_next = (km == km_last) ? NULL : (km + 1);
283 1.1 thorpej
284 1.1 thorpej if (km->vstart == km->vend) {
285 1.1 thorpej /*
286 1.1 thorpej * Found an empty slot. We are guaranteed there
287 1.1 thorpej * is not slot after this to merge with.
288 1.1 thorpej */
289 1.1 thorpej DPRINTF("!!! entering into empty slot (%d)\n", i);
290 1.1 thorpej goto enter_new;
291 1.1 thorpej }
292 1.1 thorpej
293 1.1 thorpej if (va >= km->vstart) {
294 1.1 thorpej if (va_end <= km->vend) {
295 1.1 thorpej /* This region is already fully mapped. */
296 1.1 thorpej DPRINTF("!!! matches existing region "
297 1.1 thorpej "va=0x%lx-0x%lx\n",
298 1.1 thorpej km->vstart, km->vend);
299 1.1 thorpej return 0;
300 1.1 thorpej }
301 1.1 thorpej if (va > km->vend) {
302 1.1 thorpej /* Requested region falls after this one. */
303 1.1 thorpej continue;
304 1.1 thorpej }
305 1.1 thorpej
306 1.1 thorpej /*
307 1.1 thorpej * We will extend this region. Adjust the
308 1.1 thorpej * start and size.
309 1.1 thorpej */
310 1.1 thorpej va = km->vend;
311 1.1 thorpej size = va_end - va;
312 1.1 thorpej
313 1.1 thorpej /*
314 1.1 thorpej * If there is a slot after this one and it's
315 1.1 thorpej * not empty, see if these two can be merged.
316 1.1 thorpej */
317 1.1 thorpej if (km_next != NULL &&
318 1.1 thorpej km_next->vstart != km_next->vend &&
319 1.1 thorpej va_end >= km_next->vstart) {
320 1.1 thorpej if (va_end > km_next->vend) {
321 1.1 thorpej /* Crazy! */
322 1.1 thorpej printf("!!! GOBBLED UP KM_NEXT!\n");
323 1.1 thorpej return 1;
324 1.1 thorpej }
325 1.1 thorpej va_end = km_next->vstart;
326 1.1 thorpej size = va_end - va;
327 1.1 thorpej
328 1.1 thorpej DPRINTF("!!! merging va=0x%lx-0x%lx and "
329 1.1 thorpej "va=0x%lx-0x%lx\n",
330 1.1 thorpej km->vstart, km->vend,
331 1.1 thorpej km_next->vstart, km_next->vend);
332 1.1 thorpej goto merge_two;
333 1.1 thorpej }
334 1.1 thorpej
335 1.1 thorpej DPRINTF("!!! extending existing region "
336 1.1 thorpej "va=0x%lx-0x%lx to "
337 1.1 thorpej "va=0x%lx-0x%lx\n",
338 1.1 thorpej km->vstart, km->vend,
339 1.1 thorpej km->vstart, va_end);
340 1.1 thorpej goto extend_existing;
341 1.1 thorpej }
342 1.1 thorpej
343 1.1 thorpej /*
344 1.1 thorpej * We know that the new region starts before the current
345 1.1 thorpej * one. Check to see of the end overlaps.
346 1.1 thorpej */
347 1.1 thorpej if (va_end >= km->vstart) {
348 1.1 thorpej va_end = km->vstart;
349 1.1 thorpej size = va_end - va;
350 1.1 thorpej
351 1.1 thorpej /*
352 1.1 thorpej * No need to check for a merge here; we would
353 1.1 thorpej * have caught it above.
354 1.1 thorpej */
355 1.1 thorpej goto prepend_existing;
356 1.1 thorpej }
357 1.1 thorpej
358 1.1 thorpej /*
359 1.1 thorpej * Need to the new region in front of the current one.
360 1.1 thorpej * Make sure there's room.
361 1.1 thorpej */
362 1.1 thorpej if (km_next == NULL || km_last->vstart != km_last->vend) {
363 1.1 thorpej printf("!!! NO ROOM TO INSERT MAPPING\n");
364 1.1 thorpej return 1;
365 1.1 thorpej }
366 1.1 thorpej
367 1.1 thorpej if (km_prev == NULL) {
368 1.1 thorpej DPRINTF("!!! entering before 0x%lx-0x%lx\n",
369 1.1 thorpej km->vstart, km->vend);
370 1.1 thorpej } else {
371 1.1 thorpej DPRINTF("!!! entering between 0x%lx-0x%lx and "
372 1.1 thorpej "0x%lx-0x%lx\n",
373 1.1 thorpej km_prev->vstart, km_prev->vend,
374 1.1 thorpej km->vstart, km->vend);
375 1.1 thorpej }
376 1.1 thorpej
377 1.1 thorpej if (ofw_create_mapping(va, va, size) == -1) {
378 1.1 thorpej return 1;
379 1.1 thorpej }
380 1.1 thorpej size = (uintptr_t)(&kmappings[MAX_KMAPPINGS]) -
381 1.1 thorpej (uintptr_t)(km_next + 1);
382 1.1 thorpej memmove(km_next, km, size);
383 1.1 thorpej km->vstart = va;
384 1.1 thorpej km->vend = va_end;
385 1.1 thorpej return 0;
386 1.1 thorpej }
387 1.1 thorpej
388 1.1 thorpej extend_existing:
389 1.1 thorpej if (ofw_create_mapping(va, va, size) == -1) {
390 1.1 thorpej return 1;
391 1.1 thorpej }
392 1.1 thorpej km->vend = va_end;
393 1.1 thorpej return 0;
394 1.1 thorpej
395 1.1 thorpej prepend_existing:
396 1.1 thorpej if (ofw_create_mapping(va, va, size) == -1) {
397 1.1 thorpej return 1;
398 1.1 thorpej }
399 1.1 thorpej km->vstart = va;
400 1.1 thorpej return 0;
401 1.1 thorpej
402 1.1 thorpej enter_new:
403 1.1 thorpej if (ofw_create_mapping(va, va, size) == -1) {
404 1.1 thorpej return 1;
405 1.1 thorpej }
406 1.1 thorpej km->vstart = va;
407 1.1 thorpej km->vend = va_end;
408 1.1 thorpej return 0;
409 1.1 thorpej
410 1.1 thorpej merge_two:
411 1.1 thorpej if (ofw_create_mapping(va, va, size) == -1) {
412 1.1 thorpej return 1;
413 1.1 thorpej }
414 1.1 thorpej km->vend = km_next->vend;
415 1.1 thorpej size =
416 1.1 thorpej (uintptr_t)(&kmappings[MAX_KMAPPINGS]) - (uintptr_t)(km_next + 1);
417 1.1 thorpej if (size != 0) {
418 1.1 thorpej memmove(km_next, km_next + 1, size);
419 1.1 thorpej }
420 1.1 thorpej km_last->vstart = km_last->vend = 0;
421 1.1 thorpej return 0;
422 1.1 thorpej }
423 1.1 thorpej
424 1.1 thorpej /*
425 1.1 thorpej * loadfile() hooks.
426 1.1 thorpej */
427 1.1 thorpej ssize_t
428 1.1 thorpej macppc_read(int f, void *addr, size_t size)
429 1.1 thorpej {
430 1.1 thorpej if (kmapping_enter((vaddr_t)addr, size)) {
431 1.1 thorpej return -1;
432 1.1 thorpej }
433 1.1 thorpej return read(f, addr, size);
434 1.1 thorpej }
435 1.1 thorpej
436 1.1 thorpej void *
437 1.1 thorpej macppc_memcpy(void *dst, const void *src, size_t size)
438 1.1 thorpej {
439 1.1 thorpej if (kmapping_enter((vaddr_t)dst, size)) {
440 1.1 thorpej panic("macppc_memcpy: kmapping_enter failed");
441 1.1 thorpej }
442 1.1 thorpej return memcpy(dst, src, size);
443 1.1 thorpej }
444 1.1 thorpej
445 1.1 thorpej void *
446 1.1 thorpej macppc_memset(void *dst, int c, size_t size)
447 1.1 thorpej {
448 1.1 thorpej if (kmapping_enter((vaddr_t)dst, size)) {
449 1.1 thorpej panic("macppc_memset: kmapping_enter failed");
450 1.1 thorpej }
451 1.1 thorpej return memset(dst, c, size);
452 1.1 thorpej }
453