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