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