2 * Copyright (c) 2003-2008 Apple Computer, 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.
57 #include <platforms.h>
60 #include <mach/i386/vm_param.h>
63 #include <mach/vm_param.h>
64 #include <mach/vm_prot.h>
65 #include <mach/machine.h>
66 #include <mach/time_value.h>
68 #include <kern/assert.h>
69 #include <kern/debug.h>
70 #include <kern/misc_protos.h>
71 #include <kern/cpu_data.h>
72 #include <kern/processor.h>
73 #include <vm/vm_page.h>
75 #include <vm/vm_kern.h>
76 #include <i386/pmap.h>
77 #include <i386/misc_protos.h>
79 #include <i386/cpuid.h>
80 #include <mach/thread_status.h>
81 #include <pexpert/i386/efi.h>
82 #include <i386/i386_lowmem.h>
83 #include <i386/lowglobals.h>
85 #include <mach-o/loader.h>
86 #include <libkern/kernel_mach_header.h>
89 #define DBG(x...) kprintf("DBG: " x)
90 #define PRINT_PMAP_MEMORY_TABLE
95 vm_size_t mem_size
= 0;
96 vm_offset_t first_avail
= 0;/* first after page tables */
98 uint64_t max_mem
; /* Size of physical memory (bytes), adjusted by maxmem */
100 uint64_t sane_size
= 0; /* Memory size to use for defaults calculations */
102 #define MAXBOUNCEPOOL (128 * 1024 * 1024)
103 #define MAXLORESERVE ( 32 * 1024 * 1024)
105 extern unsigned int bsd_mbuf_cluster_reserve(void);
108 uint32_t bounce_pool_base
= 0;
109 uint32_t bounce_pool_size
= 0;
111 static void reserve_bouncepool(uint32_t);
114 pmap_paddr_t avail_start
, avail_end
;
115 vm_offset_t virtual_avail
, virtual_end
;
116 static pmap_paddr_t avail_remaining
;
117 vm_offset_t static_memory_end
= 0;
119 vm_offset_t sHIB
, eHIB
, stext
, etext
, sdata
, edata
, end
;
121 boolean_t kernel_text_ps_4K
= TRUE
;
122 boolean_t wpkernel
= TRUE
;
124 extern void *KPTphys
;
127 * _mh_execute_header is the mach_header for the currently executing kernel
129 void *sectTEXTB
; unsigned long sectSizeTEXT
;
130 void *sectDATAB
; unsigned long sectSizeDATA
;
131 void *sectOBJCB
; unsigned long sectSizeOBJC
;
132 void *sectLINKB
; unsigned long sectSizeLINK
;
133 void *sectPRELINKB
; unsigned long sectSizePRELINK
;
134 void *sectHIBB
; unsigned long sectSizeHIB
;
135 void *sectINITPTB
; unsigned long sectSizeINITPT
;
138 extern uint64_t firmware_Conventional_bytes
;
139 extern uint64_t firmware_RuntimeServices_bytes
;
140 extern uint64_t firmware_ACPIReclaim_bytes
;
141 extern uint64_t firmware_ACPINVS_bytes
;
142 extern uint64_t firmware_PalCode_bytes
;
143 extern uint64_t firmware_Reserved_bytes
;
144 extern uint64_t firmware_Unusable_bytes
;
145 extern uint64_t firmware_other_bytes
;
146 uint64_t firmware_MMIO_bytes
;
149 * Basic VM initialization.
152 i386_vm_init(uint64_t maxmem
,
156 pmap_memory_region_t
*pmptr
;
157 pmap_memory_region_t
*prev_pmptr
;
158 EfiMemoryRange
*mptr
;
163 unsigned int safeboot
;
166 uint32_t maxbouncepoolsize
;
167 uint32_t maxloreserve
;
171 * Now retrieve addresses for end, edata, and etext
172 * from MACH-O headers.
175 sectTEXTB
= (void *) getsegdatafromheader(
176 &_mh_execute_header
, "__TEXT", §SizeTEXT
);
177 sectDATAB
= (void *) getsegdatafromheader(
178 &_mh_execute_header
, "__DATA", §SizeDATA
);
179 sectOBJCB
= (void *) getsegdatafromheader(
180 &_mh_execute_header
, "__OBJC", §SizeOBJC
);
181 sectLINKB
= (void *) getsegdatafromheader(
182 &_mh_execute_header
, "__LINKEDIT", §SizeLINK
);
183 sectHIBB
= (void *)getsegdatafromheader(
184 &_mh_execute_header
, "__HIB", §SizeHIB
);
185 sectINITPTB
= (void *)getsegdatafromheader(
186 &_mh_execute_header
, "__INITPT", §SizeINITPT
);
187 sectPRELINKB
= (void *) getsegdatafromheader(
188 &_mh_execute_header
, "__PRELINK_TEXT", §SizePRELINK
);
190 sHIB
= (vm_offset_t
) sectHIBB
;
191 eHIB
= (vm_offset_t
) sectHIBB
+ sectSizeHIB
;
192 /* Zero-padded from ehib to stext if text is 2M-aligned */
193 stext
= (vm_offset_t
) sectTEXTB
;
194 etext
= (vm_offset_t
) sectTEXTB
+ sectSizeTEXT
;
195 /* Zero-padded from etext to sdata if text is 2M-aligned */
196 sdata
= (vm_offset_t
) sectDATAB
;
197 edata
= (vm_offset_t
) sectDATAB
+ sectSizeDATA
;
200 kprintf("sectTEXTB = %p\n", sectTEXTB
);
201 kprintf("sectDATAB = %p\n", sectDATAB
);
202 kprintf("sectOBJCB = %p\n", sectOBJCB
);
203 kprintf("sectLINKB = %p\n", sectLINKB
);
204 kprintf("sectHIBB = %p\n", sectHIBB
);
205 kprintf("sectPRELINKB = %p\n", sectPRELINKB
);
206 kprintf("eHIB = %p\n", (void *) eHIB
);
207 kprintf("stext = %p\n", (void *) stext
);
208 kprintf("etext = %p\n", (void *) etext
);
209 kprintf("sdata = %p\n", (void *) sdata
);
210 kprintf("edata = %p\n", (void *) edata
);
216 * Compute the memory size.
219 if ((1 == vm_himemory_mode
) || PE_parse_boot_argn("-x", &safeboot
, sizeof (safeboot
))) {
220 maxpg
= 1 << (32 - I386_PGSHIFT
);
224 pmptr
= pmap_memory_regions
;
226 pmap_memory_region_count
= pmap_memory_region_current
= 0;
227 fap
= (ppnum_t
) i386_btop(first_avail
);
229 mptr
= (EfiMemoryRange
*)ml_static_ptovirt((vm_offset_t
)args
->MemoryMap
);
230 if (args
->MemoryMapDescriptorSize
== 0)
231 panic("Invalid memory map descriptor size");
232 msize
= args
->MemoryMapDescriptorSize
;
233 mcount
= args
->MemoryMapSize
/ msize
;
235 #define FOURGIG 0x0000000100000000ULL
237 for (i
= 0; i
< mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
239 uint64_t region_bytes
= 0;
241 if (pmap_memory_region_count
>= PMAP_MEMORY_REGIONS_SIZE
) {
242 kprintf("WARNING: truncating memory region count at %d\n", pmap_memory_region_count
);
245 base
= (ppnum_t
) (mptr
->PhysicalStart
>> I386_PGSHIFT
);
246 top
= (ppnum_t
) (((mptr
->PhysicalStart
) >> I386_PGSHIFT
) + mptr
->NumberOfPages
- 1);
247 region_bytes
= (uint64_t)(mptr
->NumberOfPages
<< I386_PGSHIFT
);
248 pmap_type
= mptr
->Type
;
250 switch (mptr
->Type
) {
253 case kEfiBootServicesCode
:
254 case kEfiBootServicesData
:
255 case kEfiConventionalMemory
:
257 * Consolidate usable memory types into one.
259 pmap_type
= kEfiConventionalMemory
;
260 sane_size
+= region_bytes
;
261 firmware_Conventional_bytes
+= region_bytes
;
264 * sane_size should reflect the total amount of physical
265 * RAM in the system, not just the amount that is
266 * available for the OS to use.
267 * FIXME:Consider deriving this value from SMBIOS tables
268 * rather than reverse engineering the memory map.
270 * <rdar://problem/4642773> Memory map should
271 * describe all memory
272 * Firmware on some systems guarantees that the memory
273 * map is complete via the "RomReservedMemoryTracked"
274 * feature field--consult that where possible to
275 * avoid the "round up to 128M" workaround below.
278 case kEfiRuntimeServicesCode
:
279 case kEfiRuntimeServicesData
:
280 firmware_RuntimeServices_bytes
+= region_bytes
;
281 sane_size
+= region_bytes
;
283 case kEfiACPIReclaimMemory
:
284 firmware_ACPIReclaim_bytes
+= region_bytes
;
285 sane_size
+= region_bytes
;
287 case kEfiACPIMemoryNVS
:
288 firmware_ACPINVS_bytes
+= region_bytes
;
289 sane_size
+= region_bytes
;
292 firmware_PalCode_bytes
+= region_bytes
;
293 sane_size
+= region_bytes
;
297 case kEfiReservedMemoryType
:
298 firmware_Reserved_bytes
+= region_bytes
;
300 case kEfiUnusableMemory
:
301 firmware_Unusable_bytes
+= region_bytes
;
303 case kEfiMemoryMappedIO
:
304 case kEfiMemoryMappedIOPortSpace
:
305 firmware_MMIO_bytes
+= region_bytes
;
308 firmware_other_bytes
+= region_bytes
;
312 kprintf("EFI region %d: type %u/%d, base 0x%x, top 0x%x\n",
313 i
, mptr
->Type
, pmap_type
, base
, top
);
318 top
= (top
> maxpg
) ? maxpg
: top
;
324 if ((mptr
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
||
325 pmap_type
!= kEfiConventionalMemory
) {
330 * Usable memory region
332 if (top
< I386_LOWMEM_RESERVED
) {
338 * entire range below first_avail
339 * salvage some low memory pages
340 * we use some very low memory at startup
341 * mark as already allocated here
343 if (base
>= I386_LOWMEM_RESERVED
)
346 pmptr
->base
= I386_LOWMEM_RESERVED
;
348 * mark as already mapped
350 pmptr
->alloc
= pmptr
->end
= top
;
351 pmptr
->type
= pmap_type
;
353 else if ( (base
< fap
) && (top
> fap
) ) {
356 * put mem below first avail in table but
357 * mark already allocated
360 pmptr
->alloc
= pmptr
->end
= (fap
- 1);
361 pmptr
->type
= pmap_type
;
363 * we bump these here inline so the accounting
364 * below works correctly
367 pmap_memory_region_count
++;
368 pmptr
->alloc
= pmptr
->base
= fap
;
369 pmptr
->type
= pmap_type
;
374 * entire range useable
376 pmptr
->alloc
= pmptr
->base
= base
;
377 pmptr
->type
= pmap_type
;
381 if (i386_ptob(pmptr
->end
) > avail_end
)
382 avail_end
= i386_ptob(pmptr
->end
);
384 avail_remaining
+= (pmptr
->end
- pmptr
->base
);
387 * Consolidate contiguous memory regions, if possible
390 pmptr
->type
== prev_pmptr
->type
&&
391 pmptr
->base
== pmptr
->alloc
&&
392 pmptr
->base
== (prev_pmptr
->end
+ 1)) {
393 prev_pmptr
->end
= pmptr
->end
;
395 pmap_memory_region_count
++;
402 #ifdef PRINT_PMAP_MEMORY_TABLE
405 pmap_memory_region_t
*p
= pmap_memory_regions
;
406 addr64_t region_start
, region_end
;
407 addr64_t efi_start
, efi_end
;
408 for (j
=0;j
<pmap_memory_region_count
;j
++, p
++) {
409 kprintf("pmap region %d type %d base 0x%llx alloc 0x%llx top 0x%llx\n",
411 (addr64_t
) p
->base
<< I386_PGSHIFT
,
412 (addr64_t
) p
->alloc
<< I386_PGSHIFT
,
413 (addr64_t
) p
->end
<< I386_PGSHIFT
);
414 region_start
= (addr64_t
) p
->base
<< I386_PGSHIFT
;
415 region_end
= ((addr64_t
) p
->end
<< I386_PGSHIFT
) - 1;
416 mptr
= (EfiMemoryRange
*) ml_static_ptovirt((vm_offset_t
)args
->MemoryMap
);
417 for (i
=0; i
<mcount
; i
++, mptr
= (EfiMemoryRange
*)(((vm_offset_t
)mptr
) + msize
)) {
418 if (mptr
->Type
!= kEfiLoaderCode
&&
419 mptr
->Type
!= kEfiLoaderData
&&
420 mptr
->Type
!= kEfiBootServicesCode
&&
421 mptr
->Type
!= kEfiBootServicesData
&&
422 mptr
->Type
!= kEfiConventionalMemory
) {
423 efi_start
= (addr64_t
)mptr
->PhysicalStart
;
424 efi_end
= efi_start
+ ((vm_offset_t
)mptr
->NumberOfPages
<< I386_PGSHIFT
) - 1;
425 if ((efi_start
>= region_start
&& efi_start
<= region_end
) ||
426 (efi_end
>= region_start
&& efi_end
<= region_end
)) {
427 kprintf(" *** Overlapping region with EFI runtime region %d\n", i
);
435 avail_start
= first_avail
;
436 mem_actual
= sane_size
;
439 * For user visible memory size, round up to 128 Mb - accounting for the various stolen memory
440 * not reported by EFI.
443 sane_size
= (sane_size
+ 128 * MB
- 1) & ~((uint64_t)(128 * MB
- 1));
446 * We cap at KERNEL_MAXMEM bytes (currently 32GB for K32, 64GB for K64).
447 * Unless overriden by the maxmem= boot-arg
448 * -- which is a non-zero maxmem argument to this function.
450 if (maxmem
== 0 && sane_size
> KERNEL_MAXMEM
) {
451 maxmem
= KERNEL_MAXMEM
;
452 printf("Physical memory %lld bytes capped at %dGB\n",
453 sane_size
, (uint32_t) (KERNEL_MAXMEM
/GB
));
457 * if user set maxmem, reduce memory sizes
459 if ( (maxmem
> (uint64_t)first_avail
) && (maxmem
< sane_size
)) {
460 ppnum_t discarded_pages
= (ppnum_t
)((sane_size
- maxmem
) >> I386_PGSHIFT
);
461 ppnum_t highest_pn
= 0;
462 ppnum_t cur_alloc
= 0;
463 uint64_t pages_to_use
;
464 unsigned cur_region
= 0;
468 if (avail_remaining
> discarded_pages
)
469 avail_remaining
-= discarded_pages
;
473 pages_to_use
= avail_remaining
;
475 while (cur_region
< pmap_memory_region_count
&& pages_to_use
) {
476 for (cur_alloc
= pmap_memory_regions
[cur_region
].alloc
;
477 cur_alloc
< pmap_memory_regions
[cur_region
].end
&& pages_to_use
;
479 if (cur_alloc
> highest_pn
)
480 highest_pn
= cur_alloc
;
483 if (pages_to_use
== 0)
484 pmap_memory_regions
[cur_region
].end
= cur_alloc
;
488 pmap_memory_region_count
= cur_region
;
490 avail_end
= i386_ptob(highest_pn
+ 1);
494 * mem_size is only a 32 bit container... follow the PPC route
495 * and pin it to a 2 Gbyte maximum
497 if (sane_size
> (FOURGIG
>> 1))
498 mem_size
= (vm_size_t
)(FOURGIG
>> 1);
500 mem_size
= (vm_size_t
)sane_size
;
503 kprintf("Physical memory %llu MB\n", sane_size
/MB
);
505 if (!PE_parse_boot_argn("max_valid_dma_addr", &maxdmaaddr
, sizeof (maxdmaaddr
)))
506 max_valid_dma_address
= 4 * GB
;
508 max_valid_dma_address
= ((uint64_t) maxdmaaddr
) * MB
;
510 if (!PE_parse_boot_argn("maxbouncepool", &maxbouncepoolsize
, sizeof (maxbouncepoolsize
)))
511 maxbouncepoolsize
= MAXBOUNCEPOOL
;
513 maxbouncepoolsize
= maxbouncepoolsize
* (1024 * 1024);
515 /* since bsd_mbuf_cluster_reserve() is going to be called, we need to check for server */
516 if (PE_parse_boot_argn("srv", &srv
, sizeof (srv
))) {
522 * bsd_mbuf_cluster_reserve depends on sane_size being set
523 * in order to correctly determine the size of the mbuf pool
524 * that will be reserved
526 if (!PE_parse_boot_argn("maxloreserve", &maxloreserve
, sizeof (maxloreserve
)))
527 maxloreserve
= MAXLORESERVE
+ bsd_mbuf_cluster_reserve();
529 maxloreserve
= maxloreserve
* (1024 * 1024);
532 if (avail_end
>= max_valid_dma_address
) {
533 if (maxbouncepoolsize
)
534 reserve_bouncepool(maxbouncepoolsize
);
537 vm_lopage_poolsize
= maxloreserve
/ PAGE_SIZE
;
541 * Initialize kernel physical map.
542 * Kernel virtual address starts at VM_KERNEL_MIN_ADDRESS.
544 pmap_bootstrap(0, IA32e
);
549 pmap_free_pages(void)
551 return (unsigned int)avail_remaining
;
554 #if defined(__LP64__)
555 /* On large memory systems, early allocations should prefer memory from the
556 * last region, which is typically all physical memory >4GB. This is used
557 * by pmap_steal_memory and pmap_pre_expand during init only. */
559 pmap_next_page_k64( ppnum_t
*pn
)
561 if(max_mem
>= (32*GB
)) {
562 pmap_memory_region_t
*last_region
= &pmap_memory_regions
[pmap_memory_region_count
-1];
563 if (last_region
->alloc
!= last_region
->end
) {
564 *pn
= last_region
->alloc
++;
569 return pmap_next_page(pn
);
577 if (avail_remaining
) while (pmap_memory_region_current
< pmap_memory_region_count
) {
578 if (pmap_memory_regions
[pmap_memory_region_current
].alloc
==
579 pmap_memory_regions
[pmap_memory_region_current
].end
) {
580 pmap_memory_region_current
++;
583 *pn
= pmap_memory_regions
[pmap_memory_region_current
].alloc
++;
597 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
599 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
600 if ( (pn
>= pmptr
->base
) && (pn
<= pmptr
->end
) )
608 reserve_bouncepool(uint32_t bounce_pool_wanted
)
610 pmap_memory_region_t
*pmptr
= pmap_memory_regions
;
611 pmap_memory_region_t
*lowest
= NULL
;
613 unsigned int pages_needed
;
615 pages_needed
= bounce_pool_wanted
/ PAGE_SIZE
;
617 for (i
= 0; i
< pmap_memory_region_count
; i
++, pmptr
++) {
618 if ( (pmptr
->end
- pmptr
->alloc
) >= pages_needed
) {
619 if ( (lowest
== NULL
) || (pmptr
->alloc
< lowest
->alloc
) )
623 if ( (lowest
!= NULL
) ) {
624 bounce_pool_base
= lowest
->alloc
* PAGE_SIZE
;
625 bounce_pool_size
= bounce_pool_wanted
;
627 lowest
->alloc
+= pages_needed
;
628 avail_remaining
-= pages_needed
;
633 * Called once VM is fully initialized so that we can release unused
634 * sections of low memory to the general pool.
635 * Also complete the set-up of identity-mapped sections of the kernel:
636 * 1) write-protect kernel text
637 * 2) map kernel text using large pages if possible
638 * 3) read and write-protect page zero (for K32)
639 * 4) map the global page at the appropriate virtual address.
643 * To effectively map and write-protect all kernel text pages, the text
644 * must be 2M-aligned at the base, and the data section above must also be
645 * 2M-aligned. That is, there's padding below and above. This is achieved
646 * through linker directives. Large pages are used only if this alignment
647 * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
652 * sdata: ================== 2Meg
656 * etext: ------------------
664 * stext: ================== 2Meg
668 * eHIB: ------------------
672 * Prior to changing the mapping from 4K to 2M, the zero-padding pages
673 * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
674 * 4K pages covering [stext,etext] are coalesced as 2M large pages.
675 * The now unused level-1 PTE pages are also freed.
678 pmap_lowmem_finalize(void)
683 /* Check the kernel is linked at the expected base address */
684 if (i386_btop(kvtophys((vm_offset_t
) &IdlePML4
)) !=
685 I386_KERNEL_IMAGE_BASE_PAGE
)
686 panic("pmap_lowmem_finalize() unexpected kernel base address");
689 * Free all pages in pmap regions below the base:
691 * We can't free all the pages to VM that EFI reports available.
692 * Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
693 * There's also a size miscalculation here: pend is one page less
694 * than it should be but this is not fixed to be backwards
696 * Due to this current EFI limitation, we take only the first
697 * entry in the memory region table. However, the loop is retained
698 * (with the intended termination criteria commented out) in the
699 * hope that some day we can free all low-memory ranges.
702 // pmap_memory_regions[i].end <= I386_KERNEL_IMAGE_BASE_PAGE;
705 vm_offset_t pbase
= (vm_offset_t
)i386_ptob(pmap_memory_regions
[i
].base
);
706 vm_offset_t pend
= (vm_offset_t
)i386_ptob(pmap_memory_regions
[i
].end
);
707 // vm_offset_t pend = i386_ptob(pmap_memory_regions[i].end+1);
709 DBG("ml_static_mfree(%p,%p) for pmap region %d\n",
710 (void *) ml_static_ptovirt(pbase
),
711 (void *) (pend
- pbase
), i
);
712 ml_static_mfree(ml_static_ptovirt(pbase
), pend
- pbase
);
716 * If text and data are both 2MB-aligned,
717 * we can map text with large-pages,
718 * unless the -kernel_text_ps_4K boot-arg overrides.
720 if ((stext
& I386_LPGMASK
) == 0 && (sdata
& I386_LPGMASK
) == 0) {
721 kprintf("Kernel text is 2MB aligned");
722 kernel_text_ps_4K
= FALSE
;
723 if (PE_parse_boot_argn("-kernel_text_ps_4K",
725 sizeof (kernel_text_ps_4K
)))
726 kprintf(" but will be mapped with 4K pages\n");
728 kprintf(" and will be mapped with 2M pages\n");
731 (void) PE_parse_boot_argn("wpkernel", &wpkernel
, sizeof (wpkernel
));
733 kprintf("Kernel text %p-%p to be write-protected\n",
734 (void *) stext
, (void *) etext
);
739 * Scan over text if mappings are to be changed:
740 * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0
741 * - Change to large-pages if possible and not overriden.
743 if (kernel_text_ps_4K
&& wpkernel
) {
745 for (myva
= stext
; myva
< etext
; myva
+= PAGE_SIZE
) {
748 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
750 pmap_store_pte(ptep
, *ptep
& ~INTEL_PTE_RW
);
754 if (!kernel_text_ps_4K
) {
758 * Release zero-filled page padding used for 2M-alignment.
760 DBG("ml_static_mfree(%p,%p) for padding below text\n",
761 (void *) eHIB
, (void *) (stext
- eHIB
));
762 ml_static_mfree(eHIB
, stext
- eHIB
);
763 DBG("ml_static_mfree(%p,%p) for padding above text\n",
764 (void *) etext
, (void *) (sdata
- etext
));
765 ml_static_mfree(etext
, sdata
- etext
);
768 * Coalesce text pages into large pages.
770 for (myva
= stext
; myva
< sdata
; myva
+= I386_LPGBYTES
) {
772 vm_offset_t pte_phys
;
776 pdep
= pmap_pde(kernel_pmap
, (vm_map_offset_t
)myva
);
777 ptep
= pmap_pte(kernel_pmap
, (vm_map_offset_t
)myva
);
778 DBG("myva: %p pdep: %p ptep: %p\n",
779 (void *) myva
, (void *) pdep
, (void *) ptep
);
780 if ((*ptep
& INTEL_PTE_VALID
) == 0)
782 pte_phys
= (vm_offset_t
)(*ptep
& PG_FRAME
);
783 pde
= *pdep
& PTMASK
; /* page attributes from pde */
784 pde
|= INTEL_PTE_PS
; /* make it a 2M entry */
785 pde
|= pte_phys
; /* take page frame from pte */
788 pde
&= ~INTEL_PTE_RW
;
789 DBG("pmap_store_pte(%p,0x%llx)\n",
791 pmap_store_pte(pdep
, pde
);
794 * Free the now-unused level-1 pte.
795 * Note: ptep is a virtual address to the pte in the
796 * recursive map. We can't use this address to free
797 * the page. Instead we need to compute its address
798 * in the Idle PTEs in "low memory".
800 vm_offset_t vm_ptep
= (vm_offset_t
) KPTphys
801 + (pte_phys
>> PTPGSHIFT
);
802 DBG("ml_static_mfree(%p,0x%x) for pte\n",
803 (void *) vm_ptep
, PAGE_SIZE
);
804 ml_static_mfree(vm_ptep
, PAGE_SIZE
);
807 /* Change variable read by sysctl machdep.pmap */
808 pmap_kernel_text_ps
= I386_LPGBYTES
;
811 #if defined(__i386__)
812 /* no matter what, kernel page zero is not accessible */
813 pmap_store_pte(pmap_pte(kernel_pmap
, 0), INTEL_PTE_INVALID
);
816 /* map lowmem global page into fixed addr */
817 pt_entry_t
*pte
= NULL
;
818 if (0 == (pte
= pmap_pte(kernel_pmap
,
819 VM_MIN_KERNEL_LOADED_ADDRESS
+ 0x2000)))
821 /* make sure it is defined on page boundary */
822 assert(0 == ((vm_offset_t
) &lowGlo
& PAGE_MASK
));
823 pmap_store_pte(pte
, kvtophys((vm_offset_t
)&lowGlo
)