2 * Copyright (c) 2003-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989, 1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
58 #include <mach/i386/vm_param.h>
61 #include <mach/vm_param.h>
62 #include <mach/vm_prot.h>
63 #include <mach/machine.h>
64 #include <mach/time_value.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>
73 #include <vm/vm_kern.h>
74 #include <i386/pmap.h>
75 #include <i386/misc_protos.h>
76 #include <i386/cpuid.h>
77 #include <mach/thread_status.h>
78 #include <pexpert/i386/efi.h>
79 #include <pexpert/pexpert.h>
80 #include <i386/i386_lowmem.h>
81 #include <i386/misc_protos.h>
82 #include <x86_64/lowglobals.h>
83 #include <i386/pal_routines.h>
85 #include <mach-o/loader.h>
86 #include <libkern/kernel_mach_header.h>
88 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
90 vm_size_t mem_size
= 0;
91 pmap_paddr_t first_avail
= 0;/* first after page tables */
93 uint64_t max_mem
; /* Size of physical memory minus carveouts (bytes), adjusted by maxmem */
94 uint64_t max_mem_actual
; /* Actual size of physical memory (bytes) adjusted by
95 * the maxmem boot-arg */
97 uint64_t sane_size
= 0; /* Memory size for defaults calculations */
102 ppnum_t vm_kernel_base_page
;
103 vm_offset_t vm_kernel_base
;
104 vm_offset_t vm_kernel_top
;
105 vm_offset_t vm_kernel_stext
;
106 vm_offset_t vm_kernel_etext
;
107 vm_offset_t vm_kernel_slide
;
108 vm_offset_t vm_kernel_slid_base
;
109 vm_offset_t vm_kernel_slid_top
;
110 vm_offset_t vm_hib_base
;
111 vm_offset_t vm_kext_base
= VM_MIN_KERNEL_AND_KEXT_ADDRESS
;
112 vm_offset_t vm_kext_top
= VM_MIN_KERNEL_ADDRESS
;
114 vm_offset_t vm_prelink_stext
;
115 vm_offset_t vm_prelink_etext
;
116 vm_offset_t vm_prelink_sinfo
;
117 vm_offset_t vm_prelink_einfo
;
118 vm_offset_t vm_slinkedit
;
119 vm_offset_t vm_elinkedit
;
121 vm_offset_t vm_kernel_builtinkmod_text
;
122 vm_offset_t vm_kernel_builtinkmod_text_end
;
124 #define MAXLORESERVE (32 * 1024 * 1024)
126 ppnum_t max_ppnum
= 0;
129 * pmap_high_used* are the highest range of physical memory used for kernel
130 * internals (page tables, vm_pages) via pmap_steal_memory() that don't
131 * need to be encrypted in hibernation images. There can be one gap in
132 * the middle of this due to fragmentation when using a mix of small
133 * and large pages. In that case, the fragment lives between the high
136 ppnum_t pmap_high_used_top
= 0;
137 ppnum_t pmap_high_used_bottom
= 0;
138 ppnum_t pmap_middle_used_top
= 0;
139 ppnum_t pmap_middle_used_bottom
= 0;
141 enum {PMAP_MAX_RESERVED_RANGES
= 32};
142 uint32_t pmap_reserved_pages_allocated
= 0;
143 uint32_t pmap_reserved_range_indices
[PMAP_MAX_RESERVED_RANGES
];
144 uint32_t pmap_last_reserved_range_index
= 0;
145 uint32_t pmap_reserved_ranges
= 0;
147 extern unsigned int bsd_mbuf_cluster_reserve(boolean_t
*);
149 pmap_paddr_t avail_start
, avail_end
;
150 vm_offset_t virtual_avail
, virtual_end
;
151 static pmap_paddr_t avail_remaining
;
152 vm_offset_t static_memory_end
= 0;
154 vm_offset_t sHIB
, eHIB
, stext
, etext
, sdata
, edata
, end
, sconst
, econst
;
157 * _mh_execute_header is the mach_header for the currently executing kernel
159 vm_offset_t segTEXTB
; unsigned long segSizeTEXT
;
160 vm_offset_t segDATAB
; unsigned long segSizeDATA
;
161 vm_offset_t segLINKB
; unsigned long segSizeLINK
;
162 vm_offset_t segPRELINKTEXTB
; unsigned long segSizePRELINKTEXT
;
163 vm_offset_t segPRELINKINFOB
; unsigned long segSizePRELINKINFO
;
164 vm_offset_t segHIBB
; unsigned long segSizeHIB
;
165 unsigned long segSizeConst
;
167 static kernel_segment_command_t
*segTEXT
, *segDATA
;
168 static kernel_section_t
*cursectTEXT
, *lastsectTEXT
;
169 static kernel_segment_command_t
*segCONST
;
171 extern uint64_t firmware_Conventional_bytes
;
172 extern uint64_t firmware_RuntimeServices_bytes
;
173 extern uint64_t firmware_ACPIReclaim_bytes
;
174 extern uint64_t firmware_ACPINVS_bytes
;
175 extern uint64_t firmware_PalCode_bytes
;
176 extern uint64_t firmware_Reserved_bytes
;
177 extern uint64_t firmware_Unusable_bytes
;
178 extern uint64_t firmware_other_bytes
;
179 uint64_t firmware_MMIO_bytes
;
182 * Linker magic to establish the highest address in the kernel.
184 extern void *last_kernel_symbol
;
186 #define LG_PPNUM_PAGES (I386_LPGBYTES >> PAGE_SHIFT)
187 #define LG_PPNUM_MASK (I386_LPGMASK >> PAGE_SHIFT)
189 /* set so no region large page fragment pages exist */
190 #define RESET_FRAG(r) (((r)->alloc_frag_up = 1), ((r)->alloc_frag_down = 0))
192 boolean_t memmap
= FALSE
;
193 #if DEBUG || DEVELOPMENT
195 kprint_memmap(vm_offset_t maddr
, unsigned int msize
, unsigned int mcount
)
199 pmap_memory_region_t
*p
= pmap_memory_regions
;
200 EfiMemoryRange
*mptr
;
201 addr64_t region_start
, region_end
;
202 addr64_t efi_start
, efi_end
;
204 for (j
= 0; j
< pmap_memory_region_count
; j
++, p
++) {
205 kprintf("pmap region %d type %d base 0x%llx alloc_up 0x%llx alloc_down 0x%llx"
206 " alloc_frag_up 0x%llx alloc_frag_down 0x%llx top 0x%llx\n",
208 (addr64_t
) p
->base
<< I386_PGSHIFT
,
209 (addr64_t
) p
->alloc_up
<< I386_PGSHIFT
,
210 (addr64_t
) p
->alloc_down
<< I386_PGSHIFT
,
211 (addr64_t
) p
->alloc_frag_up
<< I386_PGSHIFT
,
212 (addr64_t
) p
->alloc_frag_down
<< I386_PGSHIFT
,
213 (addr64_t
) p
->end
<< I386_PGSHIFT
);
214 region_start
= (addr64_t
) p
->base
<< I386_PGSHIFT
;
215 region_end
= ((addr64_t
) p
->end
<< I386_PGSHIFT
) - 1;
216 mptr
= (EfiMemoryRange
*) maddr
;
219 i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
220 if (mptr
->Type
!= kEfiLoaderCode
&&
221 mptr
->Type
!= kEfiLoaderData
&&
222 mptr
->Type
!= kEfiBootServicesCode
&&
223 mptr
->Type
!= kEfiBootServicesData
&&
224 mptr
->Type
!= kEfiConventionalMemory
) {
225 efi_start
= (addr64_t
)mptr
->PhysicalStart
;
226 efi_end
= efi_start
+ ((vm_offset_t
)mptr
->NumberOfPages
<< I386_PGSHIFT
) - 1;
227 if ((efi_start
>= region_start
&& efi_start
<= region_end
) ||
228 (efi_end
>= region_start
&& efi_end
<= region_end
)) {
229 kprintf(" *** Overlapping region with EFI runtime region %d\n", i
);
235 #define DPRINTF(x...) do { if (memmap) kprintf(x); } while (0)
240 kprint_memmap(vm_offset_t maddr
, unsigned int msize
, unsigned int mcount
)
242 #pragma unused(maddr, msize, mcount)
245 #define DPRINTF(x...)
249 * Basic VM initialization.
252 i386_vm_init(uint64_t maxmem
,
256 pmap_memory_region_t
*pmptr
;
257 pmap_memory_region_t
*prev_pmptr
;
258 EfiMemoryRange
*mptr
;
266 uint32_t maxloreserve
;
268 uint32_t mbuf_reserve
= 0;
269 boolean_t mbuf_override
= FALSE
;
270 boolean_t coalescing_permitted
;
271 vm_kernel_base_page
= i386_btop(args
->kaddr
);
272 vm_offset_t base_address
;
273 vm_offset_t static_base_address
;
275 PE_parse_boot_argn("memmap", &memmap
, sizeof(memmap
));
278 * Establish the KASLR parameters.
280 static_base_address
= ml_static_ptovirt(KERNEL_BASE_OFFSET
);
281 base_address
= ml_static_ptovirt(args
->kaddr
);
282 vm_kernel_slide
= base_address
- static_base_address
;
284 kprintf("KASLR slide: 0x%016lx dynamic\n", vm_kernel_slide
);
285 if (vm_kernel_slide
!= ((vm_offset_t
)args
->kslide
)) {
286 panic("Kernel base inconsistent with slide - rebased?");
289 /* No slide relative to on-disk symbols */
290 kprintf("KASLR slide: 0x%016lx static and ignored\n",
296 * Zero out local relocations to avoid confusing kxld.
297 * TODO: might be better to move this code to OSKext::initialize
299 if (_mh_execute_header
.flags
& MH_PIE
) {
300 struct load_command
*loadcmd
;
303 loadcmd
= (struct load_command
*)((uintptr_t)&_mh_execute_header
+
304 sizeof(_mh_execute_header
));
306 for (cmd
= 0; cmd
< _mh_execute_header
.ncmds
; cmd
++) {
307 if (loadcmd
->cmd
== LC_DYSYMTAB
) {
308 struct dysymtab_command
*dysymtab
;
310 dysymtab
= (struct dysymtab_command
*)loadcmd
;
311 dysymtab
->nlocrel
= 0;
312 dysymtab
->locreloff
= 0;
313 kprintf("Hiding local relocations\n");
316 loadcmd
= (struct load_command
*)((uintptr_t)loadcmd
+ loadcmd
->cmdsize
);
321 * Now retrieve addresses for end, edata, and etext
322 * from MACH-O headers.
324 segTEXTB
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
,
325 "__TEXT", &segSizeTEXT
);
326 segDATAB
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
,
327 "__DATA", &segSizeDATA
);
328 segLINKB
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
,
329 "__LINKEDIT", &segSizeLINK
);
330 segHIBB
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
,
331 "__HIB", &segSizeHIB
);
332 segPRELINKTEXTB
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
,
333 "__PRELINK_TEXT", &segSizePRELINKTEXT
);
334 segPRELINKINFOB
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
,
335 "__PRELINK_INFO", &segSizePRELINKINFO
);
336 segTEXT
= getsegbynamefromheader(&_mh_execute_header
,
338 segDATA
= getsegbynamefromheader(&_mh_execute_header
,
340 segCONST
= getsegbynamefromheader(&_mh_execute_header
,
342 cursectTEXT
= lastsectTEXT
= firstsect(segTEXT
);
343 /* Discover the last TEXT section within the TEXT segment */
344 while ((cursectTEXT
= nextsect(segTEXT
, cursectTEXT
)) != NULL
) {
345 lastsectTEXT
= cursectTEXT
;
349 eHIB
= segHIBB
+ segSizeHIB
;
351 /* Zero-padded from ehib to stext if text is 2M-aligned */
353 lowGlo
.lgStext
= stext
;
354 etext
= (vm_offset_t
) round_page_64(lastsectTEXT
->addr
+ lastsectTEXT
->size
);
355 /* Zero-padded from etext to sdata if text is 2M-aligned */
357 edata
= segDATAB
+ segSizeDATA
;
359 sconst
= segCONST
->vmaddr
;
360 segSizeConst
= segCONST
->vmsize
;
361 econst
= sconst
+ segSizeConst
;
363 kc_format_t kc_format
= KCFormatUnknown
;
365 /* XXX: FIXME_IN_dyld: For new-style kernel caches, the ending address of __DATA_CONST may not be page-aligned */
366 if (PE_get_primary_kc_format(&kc_format
) && kc_format
== KCFormatFileset
) {
367 /* Round up the end */
368 econst
= P2ROUNDUP(econst
, PAGE_SIZE
);
369 edata
= P2ROUNDUP(edata
, PAGE_SIZE
);
371 assert(((sconst
| econst
) & PAGE_MASK
) == 0);
372 assert(((sdata
| edata
) & PAGE_MASK
) == 0);
375 DPRINTF("segTEXTB = %p\n", (void *) segTEXTB
);
376 DPRINTF("segDATAB = %p\n", (void *) segDATAB
);
377 DPRINTF("segLINKB = %p\n", (void *) segLINKB
);
378 DPRINTF("segHIBB = %p\n", (void *) segHIBB
);
379 DPRINTF("segPRELINKTEXTB = %p\n", (void *) segPRELINKTEXTB
);
380 DPRINTF("segPRELINKINFOB = %p\n", (void *) segPRELINKINFOB
);
381 DPRINTF("sHIB = %p\n", (void *) sHIB
);
382 DPRINTF("eHIB = %p\n", (void *) eHIB
);
383 DPRINTF("stext = %p\n", (void *) stext
);
384 DPRINTF("etext = %p\n", (void *) etext
);
385 DPRINTF("sdata = %p\n", (void *) sdata
);
386 DPRINTF("edata = %p\n", (void *) edata
);
387 DPRINTF("sconst = %p\n", (void *) sconst
);
388 DPRINTF("econst = %p\n", (void *) econst
);
389 DPRINTF("kernel_top = %p\n", (void *) &last_kernel_symbol
);
391 vm_kernel_base
= sHIB
;
392 vm_kernel_top
= (vm_offset_t
) &last_kernel_symbol
;
393 vm_kernel_stext
= stext
;
394 vm_kernel_etext
= etext
;
395 vm_prelink_stext
= segPRELINKTEXTB
;
396 vm_prelink_etext
= segPRELINKTEXTB
+ segSizePRELINKTEXT
;
397 vm_prelink_sinfo
= segPRELINKINFOB
;
398 vm_prelink_einfo
= segPRELINKINFOB
+ segSizePRELINKINFO
;
399 vm_slinkedit
= segLINKB
;
400 vm_elinkedit
= segLINKB
+ segSizeLINK
;
403 * In the fileset world, we want to be able to (un)slide addresses from
404 * the kernel or any of the kexts (e.g., for kernel logging metadata
405 * passed between the kernel and logd in userspace). VM_KERNEL_UNSLIDE
406 * (via VM_KERNEL_IS_SLID) should apply to the addresses in the range
407 * from the first basement address to the last boot kc address.
412 * vm_kernel_slid_top - ---------------------------------------------
415 * : Boot kc (kexts in the boot kc here)
416 * : - - - - - - - - - - - - - - - - - - - - - - -
419 * | Boot kc (kernel here)
420 * - ---------------------------------------------
423 * | Basement (kexts in pageable and aux kcs here)
424 * vm_kernel_slid_base - ---------------------------------------------
428 vm_kernel_slid_base
= vm_kext_base
+ vm_kernel_slide
;
429 vm_kernel_slid_top
= (kc_format
== KCFormatFileset
) ?
430 vm_slinkedit
: vm_prelink_einfo
;
432 vm_page_kernelcache_count
= (unsigned int) (atop_64(vm_kernel_top
- vm_kernel_base
));
437 * Compute the memory size.
442 pmptr
= pmap_memory_regions
;
444 pmap_memory_region_count
= pmap_memory_region_current
= 0;
445 fap
= (ppnum_t
) i386_btop(first_avail
);
447 maddr
= ml_static_ptovirt((vm_offset_t
)args
->MemoryMap
);
448 mptr
= (EfiMemoryRange
*)maddr
;
449 if (args
->MemoryMapDescriptorSize
== 0) {
450 panic("Invalid memory map descriptor size");
452 msize
= args
->MemoryMapDescriptorSize
;
453 mcount
= args
->MemoryMapSize
/ msize
;
455 #define FOURGIG 0x0000000100000000ULL
456 #define ONEGIG 0x0000000040000000ULL
458 for (i
= 0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
460 uint64_t region_bytes
= 0;
462 if (pmap_memory_region_count
>= PMAP_MEMORY_REGIONS_SIZE
) {
463 kprintf("WARNING: truncating memory region count at %d\n", pmap_memory_region_count
);
466 base
= (ppnum_t
) (mptr
->PhysicalStart
>> I386_PGSHIFT
);
467 top
= (ppnum_t
) (((mptr
->PhysicalStart
) >> I386_PGSHIFT
) + mptr
->NumberOfPages
- 1);
471 * Avoid having to deal with the edge case of the
472 * very first possible physical page and the roll-over
473 * to -1; just ignore that page.
475 kprintf("WARNING: ignoring first page in [0x%llx:0x%llx]\n", (uint64_t) base
, (uint64_t) top
);
480 * Avoid having to deal with the edge case of the
481 * very last possible physical page and the roll-over
482 * to 0; just ignore that page.
484 kprintf("WARNING: ignoring last page in [0x%llx:0x%llx]\n", (uint64_t) base
, (uint64_t) top
);
489 * That was the only page in that region, so
490 * ignore the whole region.
496 static uint32_t nmr
= 0;
497 if ((base
> 0x20000) && (nmr
++ < 4)) {
498 mptr
->Attribute
|= EFI_MEMORY_KERN_RESERVED
;
501 region_bytes
= (uint64_t)(mptr
->NumberOfPages
<< I386_PGSHIFT
);
502 pmap_type
= mptr
->Type
;
504 switch (mptr
->Type
) {
507 case kEfiBootServicesCode
:
508 case kEfiBootServicesData
:
509 case kEfiConventionalMemory
:
511 * Consolidate usable memory types into one.
513 pmap_type
= kEfiConventionalMemory
;
514 sane_size
+= region_bytes
;
515 firmware_Conventional_bytes
+= region_bytes
;
518 * sane_size should reflect the total amount of physical
519 * RAM in the system, not just the amount that is
520 * available for the OS to use.
521 * We now get this value from SMBIOS tables
522 * rather than reverse engineering the memory map.
523 * But the legacy computation of "sane_size" is kept
524 * for diagnostic information.
527 case kEfiRuntimeServicesCode
:
528 case kEfiRuntimeServicesData
:
529 firmware_RuntimeServices_bytes
+= region_bytes
;
530 sane_size
+= region_bytes
;
532 case kEfiACPIReclaimMemory
:
533 firmware_ACPIReclaim_bytes
+= region_bytes
;
534 sane_size
+= region_bytes
;
536 case kEfiACPIMemoryNVS
:
537 firmware_ACPINVS_bytes
+= region_bytes
;
538 sane_size
+= region_bytes
;
541 firmware_PalCode_bytes
+= region_bytes
;
542 sane_size
+= region_bytes
;
545 case kEfiReservedMemoryType
:
546 firmware_Reserved_bytes
+= region_bytes
;
548 case kEfiUnusableMemory
:
549 firmware_Unusable_bytes
+= region_bytes
;
551 case kEfiMemoryMappedIO
:
552 case kEfiMemoryMappedIOPortSpace
:
553 firmware_MMIO_bytes
+= region_bytes
;
556 firmware_other_bytes
+= region_bytes
;
560 DPRINTF("EFI region %d: type %u/%d, base 0x%x, top 0x%x %s\n",
561 i
, mptr
->Type
, pmap_type
, base
, top
,
562 (mptr
->Attribute
& EFI_MEMORY_KERN_RESERVED
)? "RESERVED" :
563 (mptr
->Attribute
& EFI_MEMORY_RUNTIME
)? "RUNTIME" : "");
569 top
= (top
> maxpg
) ? maxpg
: top
;
575 if ((mptr
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
||
576 pmap_type
!= kEfiConventionalMemory
) {
581 * Usable memory region
583 if (top
< I386_LOWMEM_RESERVED
||
584 !pal_is_usable_memory(base
, top
)) {
589 * A range may be marked with with the
590 * EFI_MEMORY_KERN_RESERVED attribute
591 * on some systems, to indicate that the range
592 * must not be made available to devices.
595 if (mptr
->Attribute
& EFI_MEMORY_KERN_RESERVED
) {
596 if (++pmap_reserved_ranges
> PMAP_MAX_RESERVED_RANGES
) {
597 panic("Too many reserved ranges %u\n", pmap_reserved_ranges
);
603 * entire range below first_avail
604 * salvage some low memory pages
605 * we use some very low memory at startup
606 * mark as already allocated here
608 if (base
>= I386_LOWMEM_RESERVED
) {
611 pmptr
->base
= I386_LOWMEM_RESERVED
;
617 if ((mptr
->Attribute
& EFI_MEMORY_KERN_RESERVED
) &&
618 (top
< vm_kernel_base_page
)) {
619 pmptr
->alloc_up
= pmptr
->base
;
620 pmptr
->alloc_down
= pmptr
->end
;
622 pmap_reserved_range_indices
[pmap_last_reserved_range_index
++] = pmap_memory_region_count
;
625 * mark as already mapped
627 pmptr
->alloc_up
= top
+ 1;
628 pmptr
->alloc_down
= top
;
631 pmptr
->type
= pmap_type
;
632 pmptr
->attribute
= mptr
->Attribute
;
633 } else if ((base
< fap
) && (top
> fap
)) {
636 * put mem below first avail in table but
637 * mark already allocated
640 pmptr
->end
= (fap
- 1);
641 pmptr
->alloc_up
= pmptr
->end
+ 1;
642 pmptr
->alloc_down
= pmptr
->end
;
644 pmptr
->type
= pmap_type
;
645 pmptr
->attribute
= mptr
->Attribute
;
647 * we bump these here inline so the accounting
648 * below works correctly
651 pmap_memory_region_count
++;
653 pmptr
->alloc_up
= pmptr
->base
= fap
;
654 pmptr
->type
= pmap_type
;
655 pmptr
->attribute
= mptr
->Attribute
;
656 pmptr
->alloc_down
= pmptr
->end
= top
;
659 if (mptr
->Attribute
& EFI_MEMORY_KERN_RESERVED
) {
660 pmap_reserved_range_indices
[pmap_last_reserved_range_index
++] = pmap_memory_region_count
;
664 * entire range useable
666 pmptr
->alloc_up
= pmptr
->base
= base
;
667 pmptr
->type
= pmap_type
;
668 pmptr
->attribute
= mptr
->Attribute
;
669 pmptr
->alloc_down
= pmptr
->end
= top
;
671 if (mptr
->Attribute
& EFI_MEMORY_KERN_RESERVED
) {
672 pmap_reserved_range_indices
[pmap_last_reserved_range_index
++] = pmap_memory_region_count
;
676 if (i386_ptob(pmptr
->end
) > avail_end
) {
677 avail_end
= i386_ptob(pmptr
->end
);
680 avail_remaining
+= (pmptr
->end
- pmptr
->base
);
681 coalescing_permitted
= (prev_pmptr
&& (pmptr
->attribute
== prev_pmptr
->attribute
) && ((pmptr
->attribute
& EFI_MEMORY_KERN_RESERVED
) == 0));
683 * Consolidate contiguous memory regions, if possible
686 (pmptr
->type
== prev_pmptr
->type
) &&
687 (coalescing_permitted
) &&
688 (pmptr
->base
== pmptr
->alloc_up
) &&
689 (prev_pmptr
->end
== prev_pmptr
->alloc_down
) &&
690 (pmptr
->base
== (prev_pmptr
->end
+ 1))) {
691 prev_pmptr
->end
= pmptr
->end
;
692 prev_pmptr
->alloc_down
= pmptr
->alloc_down
;
695 pmap_memory_region_count
++;
703 kprint_memmap(maddr
, msize
, mcount
);
706 avail_start
= first_avail
;
707 mem_actual
= args
->PhysicalMemorySize
;
710 * For user visible memory size, round up to 128 Mb
711 * - accounting for the various stolen memory not reported by EFI.
712 * This is maintained for historical, comparison purposes but
713 * we now use the memory size reported by EFI/Booter.
715 sane_size
= (sane_size
+ 128 * MB
- 1) & ~((uint64_t)(128 * MB
- 1));
716 if (sane_size
!= mem_actual
) {
717 printf("mem_actual: 0x%llx\n legacy sane_size: 0x%llx\n",
718 mem_actual
, sane_size
);
720 sane_size
= mem_actual
;
723 * We cap at KERNEL_MAXMEM bytes (currently 1536GB).
724 * Unless overriden by the maxmem= boot-arg
725 * -- which is a non-zero maxmem argument to this function.
727 if (maxmem
== 0 && sane_size
> KERNEL_MAXMEM
) {
728 maxmem
= KERNEL_MAXMEM
;
729 printf("Physical memory %lld bytes capped at %dGB\n",
730 sane_size
, (uint32_t) (KERNEL_MAXMEM
/ GB
));
734 * if user set maxmem, reduce memory sizes
736 if ((maxmem
> (uint64_t)first_avail
) && (maxmem
< sane_size
)) {
737 ppnum_t discarded_pages
= (ppnum_t
)((sane_size
- maxmem
) >> I386_PGSHIFT
);
738 ppnum_t highest_pn
= 0;
740 uint64_t pages_to_use
;
741 unsigned cur_region
= 0;
745 if (avail_remaining
> discarded_pages
) {
746 avail_remaining
-= discarded_pages
;
751 pages_to_use
= avail_remaining
;
753 while (cur_region
< pmap_memory_region_count
&& pages_to_use
) {
754 for (cur_end
= pmap_memory_regions
[cur_region
].base
;
755 cur_end
< pmap_memory_regions
[cur_region
].end
&& pages_to_use
;
757 if (cur_end
> highest_pn
) {
758 highest_pn
= cur_end
;
762 if (pages_to_use
== 0) {
763 pmap_memory_regions
[cur_region
].end
= cur_end
;
764 pmap_memory_regions
[cur_region
].alloc_down
= cur_end
;
765 RESET_FRAG(&pmap_memory_regions
[cur_region
]);
770 pmap_memory_region_count
= cur_region
;
772 avail_end
= i386_ptob(highest_pn
+ 1);
776 * mem_size is only a 32 bit container... follow the PPC route
777 * and pin it to a 2 Gbyte maximum
779 if (sane_size
> (FOURGIG
>> 1)) {
780 mem_size
= (vm_size_t
)(FOURGIG
>> 1);
782 mem_size
= (vm_size_t
)sane_size
;
785 max_mem_actual
= sane_size
;
787 kprintf("Physical memory %llu MB\n", sane_size
/ MB
);
789 max_valid_low_ppnum
= (2 * GB
) / PAGE_SIZE
;
791 if (!PE_parse_boot_argn("max_valid_dma_addr", &maxdmaaddr
, sizeof(maxdmaaddr
))) {
792 max_valid_dma_address
= (uint64_t)4 * (uint64_t)GB
;
794 max_valid_dma_address
= ((uint64_t) maxdmaaddr
) * MB
;
796 if ((max_valid_dma_address
/ PAGE_SIZE
) < max_valid_low_ppnum
) {
797 max_valid_low_ppnum
= (ppnum_t
)(max_valid_dma_address
/ PAGE_SIZE
);
800 if (avail_end
>= max_valid_dma_address
) {
801 if (!PE_parse_boot_argn("maxloreserve", &maxloreserve
, sizeof(maxloreserve
))) {
802 if (sane_size
>= (ONEGIG
* 15)) {
803 maxloreserve
= (MAXLORESERVE
/ PAGE_SIZE
) * 4;
804 } else if (sane_size
>= (ONEGIG
* 7)) {
805 maxloreserve
= (MAXLORESERVE
/ PAGE_SIZE
) * 2;
807 maxloreserve
= MAXLORESERVE
/ PAGE_SIZE
;
811 mbuf_reserve
= bsd_mbuf_cluster_reserve(&mbuf_override
) / PAGE_SIZE
;
814 maxloreserve
= (maxloreserve
* (1024 * 1024)) / PAGE_SIZE
;
818 vm_lopage_free_limit
= maxloreserve
;
820 if (mbuf_override
== TRUE
) {
821 vm_lopage_free_limit
+= mbuf_reserve
;
822 vm_lopage_lowater
= 0;
824 vm_lopage_lowater
= vm_lopage_free_limit
/ 16;
827 vm_lopage_refill
= TRUE
;
828 vm_lopage_needed
= TRUE
;
833 * Initialize kernel physical map.
834 * Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS.
836 kprintf("avail_remaining = 0x%lx\n", (unsigned long)avail_remaining
);
837 pmap_bootstrap(0, IA32e
);
842 pmap_free_pages(void)
844 return (unsigned int)avail_remaining
;
847 boolean_t
pmap_next_page_reserved(ppnum_t
*);
850 * Pick a page from a "kernel private" reserved range; works around
851 * errata on some hardware. EFI marks pages which can't be used for
852 * certain kinds of I/O-ish activities as reserved. We reserve them for
853 * kernel internal usage and prevent them from ever going on regular
857 pmap_next_page_reserved(
861 pmap_memory_region_t
*region
;
862 uint32_t reserved_index
;
864 if (pmap_reserved_ranges
) {
865 for (n
= 0; n
< pmap_last_reserved_range_index
; n
++) {
866 reserved_index
= pmap_reserved_range_indices
[n
];
867 region
= &pmap_memory_regions
[reserved_index
];
868 if (region
->alloc_up
<= region
->alloc_down
) {
869 *pn
= region
->alloc_up
++;
870 } else if (region
->alloc_frag_up
<= region
->alloc_frag_down
) {
871 *pn
= region
->alloc_frag_up
++;
877 if (*pn
> max_ppnum
) {
881 pmap_reserved_pages_allocated
++;
883 if (region
->alloc_up
> region
->alloc_down
) {
884 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
);
894 * Return the highest large page available. Fails once there are no more large pages.
897 pmap_next_page_large(
901 pmap_memory_region_t
*region
;
905 if (avail_remaining
< LG_PPNUM_PAGES
) {
909 for (r
= pmap_memory_region_count
- 1; r
>= 0; r
--) {
910 region
= &pmap_memory_regions
[r
];
913 * First check if there is enough memory.
915 if (region
->alloc_down
< region
->alloc_up
||
916 (region
->alloc_down
- region
->alloc_up
+ 1) < LG_PPNUM_PAGES
) {
921 * Find the starting large page, creating a fragment if needed.
923 if ((region
->alloc_down
& LG_PPNUM_MASK
) == LG_PPNUM_MASK
) {
924 lgpg
= (region
->alloc_down
& ~LG_PPNUM_MASK
);
926 /* Can only have 1 fragment per region at a time */
927 if (region
->alloc_frag_up
<= region
->alloc_frag_down
) {
931 /* Check for enough room below any fragment. */
932 frag_start
= (region
->alloc_down
& ~LG_PPNUM_MASK
);
933 if (frag_start
< region
->alloc_up
||
934 frag_start
- region
->alloc_up
< LG_PPNUM_PAGES
) {
938 lgpg
= frag_start
- LG_PPNUM_PAGES
;
939 region
->alloc_frag_up
= frag_start
;
940 region
->alloc_frag_down
= region
->alloc_down
;
944 region
->alloc_down
= lgpg
- 1;
947 avail_remaining
-= LG_PPNUM_PAGES
;
948 if (*pn
+ LG_PPNUM_MASK
> max_ppnum
) {
949 max_ppnum
= *pn
+ LG_PPNUM_MASK
;
960 boolean_t might_free
)
962 pmap_memory_region_t
*region
;
965 if (!might_free
&& pmap_next_page_reserved(pn
)) {
969 if (avail_remaining
) {
970 for (n
= pmap_memory_region_count
- 1; n
>= 0; n
--) {
971 region
= &pmap_memory_regions
[n
];
972 if (region
->alloc_frag_up
<= region
->alloc_frag_down
) {
973 *pn
= region
->alloc_frag_down
--;
974 } else if (region
->alloc_down
>= region
->alloc_up
) {
975 *pn
= region
->alloc_down
--;
982 if (*pn
> max_ppnum
) {
993 * Record which high pages have been allocated so far,
994 * so that pmap_init() can mark them PMAP_NOENCRYPT, which
995 * makes hibernation faster.
997 * Because of the code in pmap_next_page_large(), we could
998 * theoretically have fragments in several regions.
999 * In practice that just doesn't happen. The last pmap region
1000 * is normally the largest and will satisfy all pmap_next_hi/large()
1001 * allocations. Since this information is used as an optimization
1002 * and it's ok to be conservative, we'll just record the information
1003 * for the final region.
1006 pmap_hi_pages_done(void)
1008 pmap_memory_region_t
*r
;
1010 r
= &pmap_memory_regions
[pmap_memory_region_count
- 1];
1011 pmap_high_used_top
= r
->end
;
1012 if (r
->alloc_frag_up
<= r
->alloc_frag_down
) {
1013 pmap_high_used_bottom
= r
->alloc_frag_down
+ 1;
1014 pmap_middle_used_top
= r
->alloc_frag_up
- 1;
1015 if (r
->alloc_up
<= r
->alloc_down
) {
1016 pmap_middle_used_bottom
= r
->alloc_down
+ 1;
1018 pmap_high_used_bottom
= r
->base
;
1021 if (r
->alloc_up
<= r
->alloc_down
) {
1022 pmap_high_used_bottom
= r
->alloc_down
+ 1;
1024 pmap_high_used_bottom
= r
->base
;
1027 #if DEBUG || DEVELOPMENT
1028 kprintf("pmap_high_used_top 0x%x\n", pmap_high_used_top
);
1029 kprintf("pmap_high_used_bottom 0x%x\n", pmap_high_used_bottom
);
1030 kprintf("pmap_middle_used_top 0x%x\n", pmap_middle_used_top
);
1031 kprintf("pmap_middle_used_bottom 0x%x\n", pmap_middle_used_bottom
);
1036 * Return the next available page from lowest memory for general use.
1042 pmap_memory_region_t
*region
;
1044 if (avail_remaining
) {
1045 while (pmap_memory_region_current
< pmap_memory_region_count
) {
1046 region
= &pmap_memory_regions
[pmap_memory_region_current
];
1047 if (region
->alloc_up
<= region
->alloc_down
) {
1048 *pn
= region
->alloc_up
++;
1049 } else if (region
->alloc_frag_up
<= region
->alloc_frag_down
) {
1050 *pn
= region
->alloc_frag_up
++;
1052 pmap_memory_region_current
++;
1057 if (*pn
> max_ppnum
) {
1073 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
1075 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
1076 if ((pn
>= pmptr
->base
) && (pn
<= pmptr
->end
)) {