]>
Commit | Line | Data |
---|---|---|
55e303ae | 1 | /* |
39236c6e | 2 | * Copyright (c) 2003-2012 Apple Inc. All rights reserved. |
55e303ae | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
55e303ae | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
55e303ae A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989, 1988 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
36 | * Permission to use, copy, modify and distribute this software and its | |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
41 | * | |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | ||
55e303ae A |
57 | |
58 | #include <mach/i386/vm_param.h> | |
59 | ||
60 | #include <string.h> | |
61 | #include <mach/vm_param.h> | |
62 | #include <mach/vm_prot.h> | |
63 | #include <mach/machine.h> | |
64 | #include <mach/time_value.h> | |
55e303ae A |
65 | #include <kern/spl.h> |
66 | #include <kern/assert.h> | |
67 | #include <kern/debug.h> | |
68 | #include <kern/misc_protos.h> | |
69 | #include <kern/cpu_data.h> | |
70 | #include <kern/processor.h> | |
71 | #include <vm/vm_page.h> | |
72 | #include <vm/pmap.h> | |
73 | #include <vm/vm_kern.h> | |
74 | #include <i386/pmap.h> | |
55e303ae | 75 | #include <i386/misc_protos.h> |
91447636 | 76 | #include <i386/cpuid.h> |
55e303ae | 77 | #include <mach/thread_status.h> |
0c530ab8 | 78 | #include <pexpert/i386/efi.h> |
b0d623f7 | 79 | #include <i386/i386_lowmem.h> |
316670eb | 80 | #include <x86_64/lowglobals.h> |
6d2010ae | 81 | #include <i386/pal_routines.h> |
b0d623f7 A |
82 | |
83 | #include <mach-o/loader.h> | |
84 | #include <libkern/kernel_mach_header.h> | |
85 | ||
316670eb | 86 | |
55e303ae | 87 | vm_size_t mem_size = 0; |
6d2010ae | 88 | pmap_paddr_t first_avail = 0;/* first after page tables */ |
55e303ae | 89 | |
0c530ab8 A |
90 | uint64_t max_mem; /* Size of physical memory (bytes), adjusted by maxmem */ |
91 | uint64_t mem_actual; | |
316670eb A |
92 | uint64_t sane_size = 0; /* Memory size for defaults calculations */ |
93 | ||
94 | /* | |
95 | * KASLR parameters | |
96 | */ | |
97 | ppnum_t vm_kernel_base_page; | |
98 | vm_offset_t vm_kernel_base; | |
99 | vm_offset_t vm_kernel_top; | |
100 | vm_offset_t vm_kernel_stext; | |
101 | vm_offset_t vm_kernel_etext; | |
102 | vm_offset_t vm_kernel_slide; | |
39037602 A |
103 | vm_offset_t vm_kernel_slid_base; |
104 | vm_offset_t vm_kernel_slid_top; | |
a1c7dba1 | 105 | vm_offset_t vm_hib_base; |
39236c6e A |
106 | vm_offset_t vm_kext_base = VM_MIN_KERNEL_AND_KEXT_ADDRESS; |
107 | vm_offset_t vm_kext_top = VM_MIN_KERNEL_ADDRESS; | |
0c530ab8 | 108 | |
a1c7dba1 A |
109 | vm_offset_t vm_prelink_stext; |
110 | vm_offset_t vm_prelink_etext; | |
111 | vm_offset_t vm_prelink_sinfo; | |
112 | vm_offset_t vm_prelink_einfo; | |
113 | vm_offset_t vm_slinkedit; | |
114 | vm_offset_t vm_elinkedit; | |
115 | ||
6d2010ae | 116 | #define MAXLORESERVE (32 * 1024 * 1024) |
0c530ab8 | 117 | |
0b4c1975 A |
118 | ppnum_t max_ppnum = 0; |
119 | ppnum_t lowest_lo = 0; | |
120 | ppnum_t lowest_hi = 0; | |
121 | ppnum_t highest_hi = 0; | |
0c530ab8 | 122 | |
7ddcb079 | 123 | enum {PMAP_MAX_RESERVED_RANGES = 32}; |
060df5ea | 124 | uint32_t pmap_reserved_pages_allocated = 0; |
7ddcb079 A |
125 | uint32_t pmap_reserved_range_indices[PMAP_MAX_RESERVED_RANGES]; |
126 | uint32_t pmap_last_reserved_range_index = 0; | |
060df5ea A |
127 | uint32_t pmap_reserved_ranges = 0; |
128 | ||
0b4c1975 | 129 | extern unsigned int bsd_mbuf_cluster_reserve(boolean_t *); |
0c530ab8 | 130 | |
2d21ac55 | 131 | pmap_paddr_t avail_start, avail_end; |
55e303ae | 132 | vm_offset_t virtual_avail, virtual_end; |
0c530ab8 | 133 | static pmap_paddr_t avail_remaining; |
91447636 | 134 | vm_offset_t static_memory_end = 0; |
55e303ae | 135 | |
39037602 | 136 | vm_offset_t sHIB, eHIB, stext, etext, sdata, edata, end, sconst, econst; |
0c530ab8 | 137 | |
b0d623f7 A |
138 | /* |
139 | * _mh_execute_header is the mach_header for the currently executing kernel | |
140 | */ | |
316670eb A |
141 | vm_offset_t segTEXTB; unsigned long segSizeTEXT; |
142 | vm_offset_t segDATAB; unsigned long segSizeDATA; | |
143 | vm_offset_t segLINKB; unsigned long segSizeLINK; | |
39037602 | 144 | vm_offset_t segPRELINKTEXTB; unsigned long segSizePRELINKTEXT; |
a1c7dba1 | 145 | vm_offset_t segPRELINKINFOB; unsigned long segSizePRELINKINFO; |
316670eb | 146 | vm_offset_t segHIBB; unsigned long segSizeHIB; |
39037602 | 147 | unsigned long segSizeConst; |
b0d623f7 | 148 | |
316670eb A |
149 | static kernel_segment_command_t *segTEXT, *segDATA; |
150 | static kernel_section_t *cursectTEXT, *lastsectTEXT; | |
39037602 | 151 | static kernel_segment_command_t *segCONST; |
6d2010ae | 152 | |
b0d623f7 A |
153 | extern uint64_t firmware_Conventional_bytes; |
154 | extern uint64_t firmware_RuntimeServices_bytes; | |
155 | extern uint64_t firmware_ACPIReclaim_bytes; | |
156 | extern uint64_t firmware_ACPINVS_bytes; | |
157 | extern uint64_t firmware_PalCode_bytes; | |
158 | extern uint64_t firmware_Reserved_bytes; | |
159 | extern uint64_t firmware_Unusable_bytes; | |
160 | extern uint64_t firmware_other_bytes; | |
161 | uint64_t firmware_MMIO_bytes; | |
55e303ae | 162 | |
316670eb A |
163 | /* |
164 | * Linker magic to establish the highest address in the kernel. | |
316670eb | 165 | */ |
39236c6e | 166 | extern void *last_kernel_symbol; |
316670eb | 167 | |
6d2010ae A |
168 | #if DEBUG |
169 | #define PRINT_PMAP_MEMORY_TABLE | |
316670eb A |
170 | #define DBG(x...) kprintf(x) |
171 | #else | |
172 | #define DBG(x...) | |
6d2010ae | 173 | #endif /* DEBUG */ |
55e303ae A |
174 | /* |
175 | * Basic VM initialization. | |
176 | */ | |
177 | void | |
0c530ab8 A |
178 | i386_vm_init(uint64_t maxmem, |
179 | boolean_t IA32e, | |
180 | boot_args *args) | |
55e303ae | 181 | { |
91447636 | 182 | pmap_memory_region_t *pmptr; |
0c530ab8 A |
183 | pmap_memory_region_t *prev_pmptr; |
184 | EfiMemoryRange *mptr; | |
185 | unsigned int mcount; | |
186 | unsigned int msize; | |
91447636 A |
187 | ppnum_t fap; |
188 | unsigned int i; | |
0c530ab8 A |
189 | ppnum_t maxpg = 0; |
190 | uint32_t pmap_type; | |
6d2010ae | 191 | uint32_t maxloreserve; |
0c530ab8 | 192 | uint32_t maxdmaaddr; |
6d2010ae A |
193 | uint32_t mbuf_reserve = 0; |
194 | boolean_t mbuf_override = FALSE; | |
7ddcb079 | 195 | boolean_t coalescing_permitted; |
316670eb | 196 | vm_kernel_base_page = i386_btop(args->kaddr); |
316670eb A |
197 | vm_offset_t base_address; |
198 | vm_offset_t static_base_address; | |
3e170ce0 | 199 | |
316670eb A |
200 | /* |
201 | * Establish the KASLR parameters. | |
202 | */ | |
203 | static_base_address = ml_static_ptovirt(KERNEL_BASE_OFFSET); | |
204 | base_address = ml_static_ptovirt(args->kaddr); | |
205 | vm_kernel_slide = base_address - static_base_address; | |
206 | if (args->kslide) { | |
207 | kprintf("KASLR slide: 0x%016lx dynamic\n", vm_kernel_slide); | |
208 | if (vm_kernel_slide != ((vm_offset_t)args->kslide)) | |
209 | panic("Kernel base inconsistent with slide - rebased?"); | |
210 | } else { | |
211 | /* No slide relative to on-disk symbols */ | |
212 | kprintf("KASLR slide: 0x%016lx static and ignored\n", | |
213 | vm_kernel_slide); | |
214 | vm_kernel_slide = 0; | |
215 | } | |
55e303ae | 216 | |
316670eb A |
217 | /* |
218 | * Zero out local relocations to avoid confusing kxld. | |
219 | * TODO: might be better to move this code to OSKext::initialize | |
220 | */ | |
221 | if (_mh_execute_header.flags & MH_PIE) { | |
222 | struct load_command *loadcmd; | |
223 | uint32_t cmd; | |
224 | ||
225 | loadcmd = (struct load_command *)((uintptr_t)&_mh_execute_header + | |
226 | sizeof (_mh_execute_header)); | |
227 | ||
228 | for (cmd = 0; cmd < _mh_execute_header.ncmds; cmd++) { | |
229 | if (loadcmd->cmd == LC_DYSYMTAB) { | |
230 | struct dysymtab_command *dysymtab; | |
231 | ||
232 | dysymtab = (struct dysymtab_command *)loadcmd; | |
233 | dysymtab->nlocrel = 0; | |
234 | dysymtab->locreloff = 0; | |
235 | kprintf("Hiding local relocations\n"); | |
236 | break; | |
237 | } | |
238 | loadcmd = (struct load_command *)((uintptr_t)loadcmd + loadcmd->cmdsize); | |
239 | } | |
240 | } | |
241 | ||
0c530ab8 A |
242 | /* |
243 | * Now retrieve addresses for end, edata, and etext | |
55e303ae A |
244 | * from MACH-O headers. |
245 | */ | |
316670eb A |
246 | segTEXTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, |
247 | "__TEXT", &segSizeTEXT); | |
248 | segDATAB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, | |
249 | "__DATA", &segSizeDATA); | |
250 | segLINKB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, | |
251 | "__LINKEDIT", &segSizeLINK); | |
252 | segHIBB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, | |
253 | "__HIB", &segSizeHIB); | |
39037602 A |
254 | segPRELINKTEXTB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, |
255 | "__PRELINK_TEXT", &segSizePRELINKTEXT); | |
256 | segPRELINKINFOB = (vm_offset_t) getsegdatafromheader(&_mh_execute_header, | |
257 | "__PRELINK_INFO", &segSizePRELINKINFO); | |
316670eb A |
258 | segTEXT = getsegbynamefromheader(&_mh_execute_header, |
259 | "__TEXT"); | |
260 | segDATA = getsegbynamefromheader(&_mh_execute_header, | |
261 | "__DATA"); | |
39037602 A |
262 | segCONST = getsegbynamefromheader(&_mh_execute_header, |
263 | "__CONST"); | |
6d2010ae A |
264 | cursectTEXT = lastsectTEXT = firstsect(segTEXT); |
265 | /* Discover the last TEXT section within the TEXT segment */ | |
266 | while ((cursectTEXT = nextsect(segTEXT, cursectTEXT)) != NULL) { | |
267 | lastsectTEXT = cursectTEXT; | |
268 | } | |
269 | ||
316670eb A |
270 | sHIB = segHIBB; |
271 | eHIB = segHIBB + segSizeHIB; | |
fe8ab488 | 272 | vm_hib_base = sHIB; |
b0d623f7 | 273 | /* Zero-padded from ehib to stext if text is 2M-aligned */ |
316670eb | 274 | stext = segTEXTB; |
316670eb | 275 | lowGlo.lgStext = stext; |
6d2010ae | 276 | etext = (vm_offset_t) round_page_64(lastsectTEXT->addr + lastsectTEXT->size); |
b0d623f7 | 277 | /* Zero-padded from etext to sdata if text is 2M-aligned */ |
316670eb A |
278 | sdata = segDATAB; |
279 | edata = segDATAB + segSizeDATA; | |
280 | ||
39037602 A |
281 | sconst = segCONST->vmaddr; |
282 | segSizeConst = segCONST->vmsize; | |
283 | econst = sconst + segSizeConst; | |
316670eb | 284 | |
39037602 A |
285 | assert(((sconst|econst) & PAGE_MASK) == 0); |
286 | ||
316670eb A |
287 | DBG("segTEXTB = %p\n", (void *) segTEXTB); |
288 | DBG("segDATAB = %p\n", (void *) segDATAB); | |
289 | DBG("segLINKB = %p\n", (void *) segLINKB); | |
290 | DBG("segHIBB = %p\n", (void *) segHIBB); | |
39037602 | 291 | DBG("segPRELINKTEXTB = %p\n", (void *) segPRELINKTEXTB); |
3e170ce0 | 292 | DBG("segPRELINKINFOB = %p\n", (void *) segPRELINKINFOB); |
316670eb A |
293 | DBG("sHIB = %p\n", (void *) sHIB); |
294 | DBG("eHIB = %p\n", (void *) eHIB); | |
295 | DBG("stext = %p\n", (void *) stext); | |
296 | DBG("etext = %p\n", (void *) etext); | |
297 | DBG("sdata = %p\n", (void *) sdata); | |
298 | DBG("edata = %p\n", (void *) edata); | |
39037602 A |
299 | DBG("sconst = %p\n", (void *) sconst); |
300 | DBG("econst = %p\n", (void *) econst); | |
39236c6e | 301 | DBG("kernel_top = %p\n", (void *) &last_kernel_symbol); |
316670eb A |
302 | |
303 | vm_kernel_base = sHIB; | |
39236c6e | 304 | vm_kernel_top = (vm_offset_t) &last_kernel_symbol; |
316670eb A |
305 | vm_kernel_stext = stext; |
306 | vm_kernel_etext = etext; | |
39037602 A |
307 | vm_prelink_stext = segPRELINKTEXTB; |
308 | vm_prelink_etext = segPRELINKTEXTB + segSizePRELINKTEXT; | |
309 | vm_prelink_sinfo = segPRELINKINFOB; | |
310 | vm_prelink_einfo = segPRELINKINFOB + segSizePRELINKINFO; | |
311 | vm_slinkedit = segLINKB; | |
312 | vm_elinkedit = segLINKB + segSizePRELINKTEXT; | |
313 | vm_kernel_slid_base = vm_kext_base; | |
314 | vm_kernel_slid_top = vm_elinkedit; | |
b0d623f7 | 315 | |
55e303ae A |
316 | vm_set_page_size(); |
317 | ||
55e303ae A |
318 | /* |
319 | * Compute the memory size. | |
320 | */ | |
321 | ||
91447636 A |
322 | avail_remaining = 0; |
323 | avail_end = 0; | |
324 | pmptr = pmap_memory_regions; | |
0c530ab8 | 325 | prev_pmptr = 0; |
91447636 A |
326 | pmap_memory_region_count = pmap_memory_region_current = 0; |
327 | fap = (ppnum_t) i386_btop(first_avail); | |
91447636 | 328 | |
b0d623f7 | 329 | mptr = (EfiMemoryRange *)ml_static_ptovirt((vm_offset_t)args->MemoryMap); |
0c530ab8 A |
330 | if (args->MemoryMapDescriptorSize == 0) |
331 | panic("Invalid memory map descriptor size"); | |
332 | msize = args->MemoryMapDescriptorSize; | |
333 | mcount = args->MemoryMapSize / msize; | |
334 | ||
6601e61a | 335 | #define FOURGIG 0x0000000100000000ULL |
0b4c1975 | 336 | #define ONEGIG 0x0000000040000000ULL |
0c530ab8 A |
337 | |
338 | for (i = 0; i < mcount; i++, mptr = (EfiMemoryRange *)(((vm_offset_t)mptr) + msize)) { | |
339 | ppnum_t base, top; | |
b0d623f7 | 340 | uint64_t region_bytes = 0; |
0c530ab8 A |
341 | |
342 | if (pmap_memory_region_count >= PMAP_MEMORY_REGIONS_SIZE) { | |
343 | kprintf("WARNING: truncating memory region count at %d\n", pmap_memory_region_count); | |
344 | break; | |
345 | } | |
346 | base = (ppnum_t) (mptr->PhysicalStart >> I386_PGSHIFT); | |
b0d623f7 | 347 | top = (ppnum_t) (((mptr->PhysicalStart) >> I386_PGSHIFT) + mptr->NumberOfPages - 1); |
7ddcb079 | 348 | |
39236c6e A |
349 | if (base == 0) { |
350 | /* | |
351 | * Avoid having to deal with the edge case of the | |
352 | * very first possible physical page and the roll-over | |
353 | * to -1; just ignore that page. | |
354 | */ | |
355 | kprintf("WARNING: ignoring first page in [0x%llx:0x%llx]\n", (uint64_t) base, (uint64_t) top); | |
356 | base++; | |
357 | } | |
358 | if (top + 1 == 0) { | |
359 | /* | |
360 | * Avoid having to deal with the edge case of the | |
361 | * very last possible physical page and the roll-over | |
362 | * to 0; just ignore that page. | |
363 | */ | |
364 | kprintf("WARNING: ignoring last page in [0x%llx:0x%llx]\n", (uint64_t) base, (uint64_t) top); | |
365 | top--; | |
366 | } | |
367 | if (top < base) { | |
368 | /* | |
369 | * That was the only page in that region, so | |
370 | * ignore the whole region. | |
371 | */ | |
372 | continue; | |
373 | } | |
374 | ||
7ddcb079 A |
375 | #if MR_RSV_TEST |
376 | static uint32_t nmr = 0; | |
377 | if ((base > 0x20000) && (nmr++ < 4)) | |
378 | mptr->Attribute |= EFI_MEMORY_KERN_RESERVED; | |
379 | #endif | |
b0d623f7 A |
380 | region_bytes = (uint64_t)(mptr->NumberOfPages << I386_PGSHIFT); |
381 | pmap_type = mptr->Type; | |
0c530ab8 A |
382 | |
383 | switch (mptr->Type) { | |
384 | case kEfiLoaderCode: | |
385 | case kEfiLoaderData: | |
386 | case kEfiBootServicesCode: | |
387 | case kEfiBootServicesData: | |
388 | case kEfiConventionalMemory: | |
389 | /* | |
390 | * Consolidate usable memory types into one. | |
391 | */ | |
392 | pmap_type = kEfiConventionalMemory; | |
b0d623f7 A |
393 | sane_size += region_bytes; |
394 | firmware_Conventional_bytes += region_bytes; | |
0c530ab8 | 395 | break; |
b0d623f7 A |
396 | /* |
397 | * sane_size should reflect the total amount of physical | |
398 | * RAM in the system, not just the amount that is | |
399 | * available for the OS to use. | |
3e170ce0 | 400 | * We now get this value from SMBIOS tables |
b0d623f7 | 401 | * rather than reverse engineering the memory map. |
3e170ce0 A |
402 | * But the legacy computation of "sane_size" is kept |
403 | * for diagnostic information. | |
b0d623f7 | 404 | */ |
0c530ab8 A |
405 | |
406 | case kEfiRuntimeServicesCode: | |
407 | case kEfiRuntimeServicesData: | |
b0d623f7 A |
408 | firmware_RuntimeServices_bytes += region_bytes; |
409 | sane_size += region_bytes; | |
410 | break; | |
0c530ab8 | 411 | case kEfiACPIReclaimMemory: |
b0d623f7 A |
412 | firmware_ACPIReclaim_bytes += region_bytes; |
413 | sane_size += region_bytes; | |
414 | break; | |
0c530ab8 | 415 | case kEfiACPIMemoryNVS: |
b0d623f7 A |
416 | firmware_ACPINVS_bytes += region_bytes; |
417 | sane_size += region_bytes; | |
418 | break; | |
0c530ab8 | 419 | case kEfiPalCode: |
b0d623f7 A |
420 | firmware_PalCode_bytes += region_bytes; |
421 | sane_size += region_bytes; | |
422 | break; | |
423 | ||
b0d623f7 A |
424 | case kEfiReservedMemoryType: |
425 | firmware_Reserved_bytes += region_bytes; | |
426 | break; | |
0c530ab8 | 427 | case kEfiUnusableMemory: |
b0d623f7 A |
428 | firmware_Unusable_bytes += region_bytes; |
429 | break; | |
0c530ab8 A |
430 | case kEfiMemoryMappedIO: |
431 | case kEfiMemoryMappedIOPortSpace: | |
b0d623f7 A |
432 | firmware_MMIO_bytes += region_bytes; |
433 | break; | |
0c530ab8 | 434 | default: |
b0d623f7 A |
435 | firmware_other_bytes += region_bytes; |
436 | break; | |
0c530ab8 A |
437 | } |
438 | ||
316670eb A |
439 | DBG("EFI region %d: type %u/%d, base 0x%x, top 0x%x %s\n", |
440 | i, mptr->Type, pmap_type, base, top, | |
441 | (mptr->Attribute&EFI_MEMORY_KERN_RESERVED)? "RESERVED" : | |
442 | (mptr->Attribute&EFI_MEMORY_RUNTIME)? "RUNTIME" : ""); | |
0c530ab8 A |
443 | |
444 | if (maxpg) { | |
445 | if (base >= maxpg) | |
446 | break; | |
447 | top = (top > maxpg) ? maxpg : top; | |
448 | } | |
449 | ||
450 | /* | |
451 | * handle each region | |
452 | */ | |
2d21ac55 A |
453 | if ((mptr->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME || |
454 | pmap_type != kEfiConventionalMemory) { | |
0c530ab8 A |
455 | prev_pmptr = 0; |
456 | continue; | |
457 | } else { | |
458 | /* | |
459 | * Usable memory region | |
460 | */ | |
6d2010ae A |
461 | if (top < I386_LOWMEM_RESERVED || |
462 | !pal_is_usable_memory(base, top)) { | |
0c530ab8 A |
463 | prev_pmptr = 0; |
464 | continue; | |
465 | } | |
7ddcb079 A |
466 | /* |
467 | * A range may be marked with with the | |
468 | * EFI_MEMORY_KERN_RESERVED attribute | |
469 | * on some systems, to indicate that the range | |
470 | * must not be made available to devices. | |
471 | */ | |
472 | ||
473 | if (mptr->Attribute & EFI_MEMORY_KERN_RESERVED) { | |
474 | if (++pmap_reserved_ranges > PMAP_MAX_RESERVED_RANGES) { | |
475 | panic("Too many reserved ranges %u\n", pmap_reserved_ranges); | |
476 | } | |
477 | } | |
478 | ||
0c530ab8 A |
479 | if (top < fap) { |
480 | /* | |
481 | * entire range below first_avail | |
482 | * salvage some low memory pages | |
483 | * we use some very low memory at startup | |
484 | * mark as already allocated here | |
485 | */ | |
486 | if (base >= I386_LOWMEM_RESERVED) | |
487 | pmptr->base = base; | |
488 | else | |
489 | pmptr->base = I386_LOWMEM_RESERVED; | |
060df5ea A |
490 | |
491 | pmptr->end = top; | |
492 | ||
060df5ea A |
493 | |
494 | if ((mptr->Attribute & EFI_MEMORY_KERN_RESERVED) && | |
316670eb | 495 | (top < vm_kernel_base_page)) { |
39236c6e A |
496 | pmptr->alloc_up = pmptr->base; |
497 | pmptr->alloc_down = pmptr->end; | |
7ddcb079 | 498 | pmap_reserved_range_indices[pmap_last_reserved_range_index++] = pmap_memory_region_count; |
060df5ea A |
499 | } |
500 | else { | |
501 | /* | |
502 | * mark as already mapped | |
503 | */ | |
39236c6e A |
504 | pmptr->alloc_up = top + 1; |
505 | pmptr->alloc_down = top; | |
060df5ea | 506 | } |
0c530ab8 | 507 | pmptr->type = pmap_type; |
7ddcb079 | 508 | pmptr->attribute = mptr->Attribute; |
0c530ab8 A |
509 | } |
510 | else if ( (base < fap) && (top > fap) ) { | |
511 | /* | |
512 | * spans first_avail | |
513 | * put mem below first avail in table but | |
514 | * mark already allocated | |
515 | */ | |
516 | pmptr->base = base; | |
39236c6e A |
517 | pmptr->end = (fap - 1); |
518 | pmptr->alloc_up = pmptr->end + 1; | |
519 | pmptr->alloc_down = pmptr->end; | |
0c530ab8 | 520 | pmptr->type = pmap_type; |
7ddcb079 | 521 | pmptr->attribute = mptr->Attribute; |
0c530ab8 A |
522 | /* |
523 | * we bump these here inline so the accounting | |
524 | * below works correctly | |
525 | */ | |
526 | pmptr++; | |
527 | pmap_memory_region_count++; | |
7ddcb079 | 528 | |
39236c6e | 529 | pmptr->alloc_up = pmptr->base = fap; |
0c530ab8 | 530 | pmptr->type = pmap_type; |
7ddcb079 | 531 | pmptr->attribute = mptr->Attribute; |
39236c6e | 532 | pmptr->alloc_down = pmptr->end = top; |
7ddcb079 A |
533 | |
534 | if (mptr->Attribute & EFI_MEMORY_KERN_RESERVED) | |
535 | pmap_reserved_range_indices[pmap_last_reserved_range_index++] = pmap_memory_region_count; | |
536 | } else { | |
0c530ab8 A |
537 | /* |
538 | * entire range useable | |
539 | */ | |
39236c6e | 540 | pmptr->alloc_up = pmptr->base = base; |
0c530ab8 | 541 | pmptr->type = pmap_type; |
7ddcb079 | 542 | pmptr->attribute = mptr->Attribute; |
39236c6e | 543 | pmptr->alloc_down = pmptr->end = top; |
7ddcb079 A |
544 | if (mptr->Attribute & EFI_MEMORY_KERN_RESERVED) |
545 | pmap_reserved_range_indices[pmap_last_reserved_range_index++] = pmap_memory_region_count; | |
0c530ab8 A |
546 | } |
547 | ||
548 | if (i386_ptob(pmptr->end) > avail_end ) | |
549 | avail_end = i386_ptob(pmptr->end); | |
550 | ||
551 | avail_remaining += (pmptr->end - pmptr->base); | |
7ddcb079 | 552 | coalescing_permitted = (prev_pmptr && (pmptr->attribute == prev_pmptr->attribute) && ((pmptr->attribute & EFI_MEMORY_KERN_RESERVED) == 0)); |
0c530ab8 A |
553 | /* |
554 | * Consolidate contiguous memory regions, if possible | |
555 | */ | |
556 | if (prev_pmptr && | |
7ddcb079 A |
557 | (pmptr->type == prev_pmptr->type) && |
558 | (coalescing_permitted) && | |
39236c6e A |
559 | (pmptr->base == pmptr->alloc_up) && |
560 | (prev_pmptr->end == prev_pmptr->alloc_down) && | |
7ddcb079 | 561 | (pmptr->base == (prev_pmptr->end + 1))) |
6d2010ae | 562 | { |
6d2010ae | 563 | prev_pmptr->end = pmptr->end; |
39236c6e | 564 | prev_pmptr->alloc_down = pmptr->alloc_down; |
0c530ab8 A |
565 | } else { |
566 | pmap_memory_region_count++; | |
567 | prev_pmptr = pmptr; | |
568 | pmptr++; | |
569 | } | |
570 | } | |
6601e61a | 571 | } |
0c530ab8 | 572 | |
91447636 | 573 | #ifdef PRINT_PMAP_MEMORY_TABLE |
0c530ab8 A |
574 | { |
575 | unsigned int j; | |
576 | pmap_memory_region_t *p = pmap_memory_regions; | |
b0d623f7 A |
577 | addr64_t region_start, region_end; |
578 | addr64_t efi_start, efi_end; | |
0c530ab8 | 579 | for (j=0;j<pmap_memory_region_count;j++, p++) { |
39236c6e | 580 | kprintf("pmap region %d type %d base 0x%llx alloc_up 0x%llx alloc_down 0x%llx top 0x%llx\n", |
b0d623f7 A |
581 | j, p->type, |
582 | (addr64_t) p->base << I386_PGSHIFT, | |
39236c6e A |
583 | (addr64_t) p->alloc_up << I386_PGSHIFT, |
584 | (addr64_t) p->alloc_down << I386_PGSHIFT, | |
b0d623f7 A |
585 | (addr64_t) p->end << I386_PGSHIFT); |
586 | region_start = (addr64_t) p->base << I386_PGSHIFT; | |
587 | region_end = ((addr64_t) p->end << I386_PGSHIFT) - 1; | |
588 | mptr = (EfiMemoryRange *) ml_static_ptovirt((vm_offset_t)args->MemoryMap); | |
0c530ab8 A |
589 | for (i=0; i<mcount; i++, mptr = (EfiMemoryRange *)(((vm_offset_t)mptr) + msize)) { |
590 | if (mptr->Type != kEfiLoaderCode && | |
591 | mptr->Type != kEfiLoaderData && | |
592 | mptr->Type != kEfiBootServicesCode && | |
593 | mptr->Type != kEfiBootServicesData && | |
594 | mptr->Type != kEfiConventionalMemory) { | |
b0d623f7 | 595 | efi_start = (addr64_t)mptr->PhysicalStart; |
0c530ab8 A |
596 | efi_end = efi_start + ((vm_offset_t)mptr->NumberOfPages << I386_PGSHIFT) - 1; |
597 | if ((efi_start >= region_start && efi_start <= region_end) || | |
598 | (efi_end >= region_start && efi_end <= region_end)) { | |
599 | kprintf(" *** Overlapping region with EFI runtime region %d\n", i); | |
600 | } | |
b0d623f7 | 601 | } |
0c530ab8 | 602 | } |
b0d623f7 | 603 | } |
0c530ab8 | 604 | } |
91447636 | 605 | #endif |
55e303ae | 606 | |
91447636 | 607 | avail_start = first_avail; |
3e170ce0 | 608 | mem_actual = args->PhysicalMemorySize; |
4452a7af | 609 | |
0c530ab8 | 610 | /* |
3e170ce0 A |
611 | * For user visible memory size, round up to 128 Mb |
612 | * - accounting for the various stolen memory not reported by EFI. | |
613 | * This is maintained for historical, comparison purposes but | |
614 | * we now use the memory size reported by EFI/Booter. | |
0c530ab8 | 615 | */ |
b0d623f7 | 616 | sane_size = (sane_size + 128 * MB - 1) & ~((uint64_t)(128 * MB - 1)); |
3e170ce0 A |
617 | if (sane_size != mem_actual) |
618 | printf("mem_actual: 0x%llx\n legacy sane_size: 0x%llx\n", | |
619 | mem_actual, sane_size); | |
620 | sane_size = mem_actual; | |
0c530ab8 | 621 | |
c910b4d9 | 622 | /* |
6d2010ae | 623 | * We cap at KERNEL_MAXMEM bytes (currently 32GB for K32, 96GB for K64). |
c910b4d9 A |
624 | * Unless overriden by the maxmem= boot-arg |
625 | * -- which is a non-zero maxmem argument to this function. | |
626 | */ | |
b0d623f7 A |
627 | if (maxmem == 0 && sane_size > KERNEL_MAXMEM) { |
628 | maxmem = KERNEL_MAXMEM; | |
629 | printf("Physical memory %lld bytes capped at %dGB\n", | |
630 | sane_size, (uint32_t) (KERNEL_MAXMEM/GB)); | |
c910b4d9 | 631 | } |
b0d623f7 | 632 | |
0c530ab8 A |
633 | /* |
634 | * if user set maxmem, reduce memory sizes | |
635 | */ | |
636 | if ( (maxmem > (uint64_t)first_avail) && (maxmem < sane_size)) { | |
b0d623f7 | 637 | ppnum_t discarded_pages = (ppnum_t)((sane_size - maxmem) >> I386_PGSHIFT); |
2d21ac55 | 638 | ppnum_t highest_pn = 0; |
316670eb | 639 | ppnum_t cur_end = 0; |
2d21ac55 A |
640 | uint64_t pages_to_use; |
641 | unsigned cur_region = 0; | |
642 | ||
643 | sane_size = maxmem; | |
644 | ||
0c530ab8 A |
645 | if (avail_remaining > discarded_pages) |
646 | avail_remaining -= discarded_pages; | |
647 | else | |
648 | avail_remaining = 0; | |
2d21ac55 A |
649 | |
650 | pages_to_use = avail_remaining; | |
651 | ||
652 | while (cur_region < pmap_memory_region_count && pages_to_use) { | |
316670eb A |
653 | for (cur_end = pmap_memory_regions[cur_region].base; |
654 | cur_end < pmap_memory_regions[cur_region].end && pages_to_use; | |
655 | cur_end++) { | |
656 | if (cur_end > highest_pn) | |
657 | highest_pn = cur_end; | |
2d21ac55 A |
658 | pages_to_use--; |
659 | } | |
39236c6e | 660 | if (pages_to_use == 0) { |
316670eb | 661 | pmap_memory_regions[cur_region].end = cur_end; |
39236c6e A |
662 | pmap_memory_regions[cur_region].alloc_down = cur_end; |
663 | } | |
2d21ac55 A |
664 | |
665 | cur_region++; | |
666 | } | |
667 | pmap_memory_region_count = cur_region; | |
668 | ||
669 | avail_end = i386_ptob(highest_pn + 1); | |
55e303ae | 670 | } |
4452a7af | 671 | |
0c530ab8 A |
672 | /* |
673 | * mem_size is only a 32 bit container... follow the PPC route | |
674 | * and pin it to a 2 Gbyte maximum | |
675 | */ | |
676 | if (sane_size > (FOURGIG >> 1)) | |
677 | mem_size = (vm_size_t)(FOURGIG >> 1); | |
678 | else | |
679 | mem_size = (vm_size_t)sane_size; | |
6601e61a | 680 | max_mem = sane_size; |
5d5c5d0d | 681 | |
b0d623f7 | 682 | kprintf("Physical memory %llu MB\n", sane_size/MB); |
4452a7af | 683 | |
0b4c1975 | 684 | max_valid_low_ppnum = (2 * GB) / PAGE_SIZE; |
0c530ab8 | 685 | |
0b4c1975 A |
686 | if (!PE_parse_boot_argn("max_valid_dma_addr", &maxdmaaddr, sizeof (maxdmaaddr))) { |
687 | max_valid_dma_address = (uint64_t)4 * (uint64_t)GB; | |
688 | } else { | |
689 | max_valid_dma_address = ((uint64_t) maxdmaaddr) * MB; | |
21362eb3 | 690 | |
0b4c1975 A |
691 | if ((max_valid_dma_address / PAGE_SIZE) < max_valid_low_ppnum) |
692 | max_valid_low_ppnum = (ppnum_t)(max_valid_dma_address / PAGE_SIZE); | |
693 | } | |
0c530ab8 | 694 | if (avail_end >= max_valid_dma_address) { |
0b4c1975 A |
695 | |
696 | if (!PE_parse_boot_argn("maxloreserve", &maxloreserve, sizeof (maxloreserve))) { | |
697 | ||
698 | if (sane_size >= (ONEGIG * 15)) | |
699 | maxloreserve = (MAXLORESERVE / PAGE_SIZE) * 4; | |
700 | else if (sane_size >= (ONEGIG * 7)) | |
701 | maxloreserve = (MAXLORESERVE / PAGE_SIZE) * 2; | |
702 | else | |
703 | maxloreserve = MAXLORESERVE / PAGE_SIZE; | |
704 | ||
39236c6e | 705 | #if SOCKETS |
0b4c1975 | 706 | mbuf_reserve = bsd_mbuf_cluster_reserve(&mbuf_override) / PAGE_SIZE; |
39236c6e | 707 | #endif |
0b4c1975 A |
708 | } else |
709 | maxloreserve = (maxloreserve * (1024 * 1024)) / PAGE_SIZE; | |
710 | ||
711 | if (maxloreserve) { | |
712 | vm_lopage_free_limit = maxloreserve; | |
713 | ||
714 | if (mbuf_override == TRUE) { | |
715 | vm_lopage_free_limit += mbuf_reserve; | |
716 | vm_lopage_lowater = 0; | |
717 | } else | |
718 | vm_lopage_lowater = vm_lopage_free_limit / 16; | |
719 | ||
720 | vm_lopage_refill = TRUE; | |
721 | vm_lopage_needed = TRUE; | |
722 | } | |
0c530ab8 | 723 | } |
6d2010ae | 724 | |
0c530ab8 A |
725 | /* |
726 | * Initialize kernel physical map. | |
727 | * Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS. | |
728 | */ | |
6d2010ae | 729 | kprintf("avail_remaining = 0x%lx\n", (unsigned long)avail_remaining); |
0c530ab8 | 730 | pmap_bootstrap(0, IA32e); |
6601e61a A |
731 | } |
732 | ||
0c530ab8 | 733 | |
55e303ae A |
734 | unsigned int |
735 | pmap_free_pages(void) | |
736 | { | |
b0d623f7 | 737 | return (unsigned int)avail_remaining; |
55e303ae A |
738 | } |
739 | ||
6d2010ae | 740 | |
060df5ea A |
741 | boolean_t pmap_next_page_reserved(ppnum_t *); |
742 | ||
743 | /* | |
744 | * Pick a page from a "kernel private" reserved range; works around | |
745 | * errata on some hardware. | |
746 | */ | |
747 | boolean_t | |
748 | pmap_next_page_reserved(ppnum_t *pn) { | |
7ddcb079 | 749 | if (pmap_reserved_ranges) { |
060df5ea A |
750 | uint32_t n; |
751 | pmap_memory_region_t *region; | |
7ddcb079 A |
752 | for (n = 0; n < pmap_last_reserved_range_index; n++) { |
753 | uint32_t reserved_index = pmap_reserved_range_indices[n]; | |
754 | region = &pmap_memory_regions[reserved_index]; | |
39236c6e A |
755 | if (region->alloc_up <= region->alloc_down) { |
756 | *pn = region->alloc_up++; | |
060df5ea A |
757 | avail_remaining--; |
758 | ||
759 | if (*pn > max_ppnum) | |
760 | max_ppnum = *pn; | |
761 | ||
762 | if (lowest_lo == 0 || *pn < lowest_lo) | |
763 | lowest_lo = *pn; | |
764 | ||
765 | pmap_reserved_pages_allocated++; | |
7ddcb079 | 766 | #if DEBUG |
39236c6e | 767 | if (region->alloc_up > region->alloc_down) { |
7ddcb079 A |
768 | kprintf("Exhausted reserved range index: %u, base: 0x%x end: 0x%x, type: 0x%x, attribute: 0x%llx\n", reserved_index, region->base, region->end, region->type, region->attribute); |
769 | } | |
770 | #endif | |
060df5ea A |
771 | return TRUE; |
772 | } | |
773 | } | |
774 | } | |
775 | return FALSE; | |
776 | } | |
777 | ||
778 | ||
b0d623f7 | 779 | boolean_t |
0b4c1975 A |
780 | pmap_next_page_hi( |
781 | ppnum_t *pn) | |
b0d623f7 | 782 | { |
0b4c1975 A |
783 | pmap_memory_region_t *region; |
784 | int n; | |
785 | ||
060df5ea A |
786 | if (pmap_next_page_reserved(pn)) |
787 | return TRUE; | |
788 | ||
0b4c1975 A |
789 | if (avail_remaining) { |
790 | for (n = pmap_memory_region_count - 1; n >= 0; n--) { | |
791 | region = &pmap_memory_regions[n]; | |
792 | ||
39236c6e A |
793 | if (region->alloc_down >= region->alloc_up) { |
794 | *pn = region->alloc_down--; | |
0b4c1975 A |
795 | avail_remaining--; |
796 | ||
797 | if (*pn > max_ppnum) | |
798 | max_ppnum = *pn; | |
799 | ||
800 | if (lowest_lo == 0 || *pn < lowest_lo) | |
801 | lowest_lo = *pn; | |
802 | ||
803 | if (lowest_hi == 0 || *pn < lowest_hi) | |
804 | lowest_hi = *pn; | |
805 | ||
806 | if (*pn > highest_hi) | |
807 | highest_hi = *pn; | |
808 | ||
809 | return TRUE; | |
810 | } | |
b0d623f7 A |
811 | } |
812 | } | |
0b4c1975 | 813 | return FALSE; |
b0d623f7 | 814 | } |
0b4c1975 | 815 | |
0c530ab8 | 816 | |
55e303ae A |
817 | boolean_t |
818 | pmap_next_page( | |
819 | ppnum_t *pn) | |
820 | { | |
0c530ab8 | 821 | if (avail_remaining) while (pmap_memory_region_current < pmap_memory_region_count) { |
39236c6e A |
822 | if (pmap_memory_regions[pmap_memory_region_current].alloc_up > |
823 | pmap_memory_regions[pmap_memory_region_current].alloc_down) { | |
b0d623f7 | 824 | pmap_memory_region_current++; |
0c530ab8 A |
825 | continue; |
826 | } | |
39236c6e | 827 | *pn = pmap_memory_regions[pmap_memory_region_current].alloc_up++; |
0c530ab8 A |
828 | avail_remaining--; |
829 | ||
0b4c1975 A |
830 | if (*pn > max_ppnum) |
831 | max_ppnum = *pn; | |
832 | ||
833 | if (lowest_lo == 0 || *pn < lowest_lo) | |
834 | lowest_lo = *pn; | |
835 | ||
0c530ab8 | 836 | return TRUE; |
91447636 A |
837 | } |
838 | return FALSE; | |
55e303ae A |
839 | } |
840 | ||
0c530ab8 | 841 | |
55e303ae A |
842 | boolean_t |
843 | pmap_valid_page( | |
91447636 | 844 | ppnum_t pn) |
55e303ae | 845 | { |
0c530ab8 A |
846 | unsigned int i; |
847 | pmap_memory_region_t *pmptr = pmap_memory_regions; | |
848 | ||
0c530ab8 | 849 | for (i = 0; i < pmap_memory_region_count; i++, pmptr++) { |
2d21ac55 | 850 | if ( (pn >= pmptr->base) && (pn <= pmptr->end) ) |
0c530ab8 A |
851 | return TRUE; |
852 | } | |
853 | return FALSE; | |
854 | } | |
855 |