2 * Copyright (c) 2000-2010 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@
29 #include <kern/ledger.h>
30 #include <i386/pmap_internal.h>
34 * Each entry in the pv_head_table is locked by a bit in the
35 * pv_lock_table. The lock bits are accessed by the physical
36 * address of the page they lock.
39 char *pv_lock_table
; /* pointer to array of bits */
40 char *pv_hash_lock_table
;
42 pv_rooted_entry_t pv_head_table
; /* array of entries, one per
44 uint32_t pv_hashed_free_count
= 0;
45 uint32_t pv_hashed_kern_free_count
= 0;
47 pmap_pagetable_corruption_record_t pmap_pagetable_corruption_records
[PMAP_PAGETABLE_CORRUPTION_MAX_LOG
];
48 uint32_t pmap_pagetable_corruption_incidents
;
49 uint64_t pmap_pagetable_corruption_last_abstime
= (~(0ULL) >> 1);
50 uint64_t pmap_pagetable_corruption_interval_abstime
;
51 thread_call_t pmap_pagetable_corruption_log_call
;
52 static thread_call_data_t pmap_pagetable_corruption_log_call_data
;
53 boolean_t pmap_pagetable_corruption_timeout
= FALSE
;
55 volatile uint32_t mappingrecurse
= 0;
57 uint32_t pv_hashed_low_water_mark
, pv_hashed_kern_low_water_mark
, pv_hashed_alloc_chunk
, pv_hashed_kern_alloc_chunk
;
59 thread_t mapping_replenish_thread
;
60 event_t mapping_replenish_event
, pmap_user_pv_throttle_event
;
62 uint64_t pmap_pv_throttle_stat
, pmap_pv_throttled_waiters
;
64 unsigned int pmap_cache_attributes(ppnum_t pn
) {
65 if (pmap_get_cache_attributes(pn
, FALSE
) & INTEL_PTE_NCACHE
)
68 return (VM_WIMG_COPYBACK
);
71 void pmap_set_cache_attributes(ppnum_t pn
, unsigned int cacheattr
) {
72 unsigned int current
, template = 0;
75 if (cacheattr
& VM_MEM_NOT_CACHEABLE
) {
76 if(!(cacheattr
& VM_MEM_GUARDED
))
78 template |= PHYS_NCACHE
;
83 assert((pn
!= vm_page_fictitious_addr
) && (pn
!= vm_page_guard_addr
));
87 if (!IS_MANAGED_PAGE(pai
)) {
91 /* override cache attributes for this phys page
92 * Does not walk through existing mappings to adjust,
93 * assumes page is disconnected
98 pmap_update_cache_attributes_locked(pn
, template);
100 current
= pmap_phys_attributes
[pai
] & PHYS_CACHEABILITY_MASK
;
101 pmap_phys_attributes
[pai
] &= ~PHYS_CACHEABILITY_MASK
;
102 pmap_phys_attributes
[pai
] |= template;
106 if ((template & PHYS_NCACHE
) && !(current
& PHYS_NCACHE
)) {
107 pmap_sync_page_attributes_phys(pn
);
111 unsigned pmap_get_cache_attributes(ppnum_t pn
, boolean_t is_ept
) {
112 if (last_managed_page
== 0)
115 if (!IS_MANAGED_PAGE(ppn_to_pai(pn
)))
116 return PTE_NCACHE(is_ept
);
119 * The cache attributes are read locklessly for efficiency.
121 unsigned int attr
= pmap_phys_attributes
[ppn_to_pai(pn
)];
122 unsigned int template = 0;
125 * The PTA bit is currently unsupported for EPT PTEs.
127 if ((attr
& PHYS_PTA
) && !is_ept
)
128 template |= INTEL_PTE_PTA
;
131 * If the page isn't marked as NCACHE, the default for EPT entries
134 if (attr
& PHYS_NCACHE
)
135 template |= PTE_NCACHE(is_ept
);
137 template |= INTEL_EPT_WB
;
143 pmap_has_managed_page(ppnum_t first
, ppnum_t last
)
148 assert(last_managed_page
);
149 assert(first
<= last
);
151 for (result
= FALSE
, pn
= first
;
154 && (pn
<= last_managed_page
);
157 result
= (0 != (pmap_phys_attributes
[pn
] & PHYS_MANAGED
));
164 pmap_is_noencrypt(ppnum_t pn
)
168 pai
= ppn_to_pai(pn
);
170 if (!IS_MANAGED_PAGE(pai
))
173 if (pmap_phys_attributes
[pai
] & PHYS_NOENCRYPT
)
181 pmap_set_noencrypt(ppnum_t pn
)
185 pai
= ppn_to_pai(pn
);
187 if (IS_MANAGED_PAGE(pai
)) {
190 pmap_phys_attributes
[pai
] |= PHYS_NOENCRYPT
;
198 pmap_clear_noencrypt(ppnum_t pn
)
202 pai
= ppn_to_pai(pn
);
204 if (IS_MANAGED_PAGE(pai
)) {
206 * synchronization at VM layer prevents PHYS_NOENCRYPT
207 * from changing state, so we don't need the lock to inspect
209 if (pmap_phys_attributes
[pai
] & PHYS_NOENCRYPT
) {
212 pmap_phys_attributes
[pai
] &= ~PHYS_NOENCRYPT
;
220 compute_pmap_gc_throttle(void *arg __unused
)
227 pmap_lock_phys_page(ppnum_t pn
)
231 pai
= ppn_to_pai(pn
);
233 if (IS_MANAGED_PAGE(pai
)) {
236 simple_lock(&phys_backup_lock
);
241 pmap_unlock_phys_page(ppnum_t pn
)
245 pai
= ppn_to_pai(pn
);
247 if (IS_MANAGED_PAGE(pai
)) {
250 simple_unlock(&phys_backup_lock
);
255 __private_extern__
void
256 pmap_pagetable_corruption_msg_log(int (*log_func
)(const char * fmt
, ...)__printflike(1,2)) {
257 if (pmap_pagetable_corruption_incidents
> 0) {
258 int i
, e
= MIN(pmap_pagetable_corruption_incidents
, PMAP_PAGETABLE_CORRUPTION_MAX_LOG
);
259 (*log_func
)("%u pagetable corruption incident(s) detected, timeout: %u\n", pmap_pagetable_corruption_incidents
, pmap_pagetable_corruption_timeout
);
260 for (i
= 0; i
< e
; i
++) {
261 (*log_func
)("Incident 0x%x, reason: 0x%x, action: 0x%x, time: 0x%llx\n", pmap_pagetable_corruption_records
[i
].incident
, pmap_pagetable_corruption_records
[i
].reason
, pmap_pagetable_corruption_records
[i
].action
, pmap_pagetable_corruption_records
[i
].abstime
);
267 pmap_pagetable_corruption_log_setup(void) {
268 if (pmap_pagetable_corruption_log_call
== NULL
) {
269 nanotime_to_absolutetime(PMAP_PAGETABLE_CORRUPTION_INTERVAL
, 0, &pmap_pagetable_corruption_interval_abstime
);
270 thread_call_setup(&pmap_pagetable_corruption_log_call_data
,
271 (thread_call_func_t
) pmap_pagetable_corruption_msg_log
,
272 (thread_call_param_t
) &printf
);
273 pmap_pagetable_corruption_log_call
= &pmap_pagetable_corruption_log_call_data
;
278 mapping_free_prime(void)
281 pv_hashed_entry_t pvh_e
;
282 pv_hashed_entry_t pvh_eh
;
283 pv_hashed_entry_t pvh_et
;
286 /* Scale based on DRAM size */
287 pv_hashed_low_water_mark
= MAX(PV_HASHED_LOW_WATER_MARK_DEFAULT
, ((uint32_t)(sane_size
>> 30)) * 2000);
288 pv_hashed_low_water_mark
= MIN(pv_hashed_low_water_mark
, 16000);
289 /* Alterable via sysctl */
290 pv_hashed_kern_low_water_mark
= MAX(PV_HASHED_KERN_LOW_WATER_MARK_DEFAULT
, ((uint32_t)(sane_size
>> 30)) * 1000);
291 pv_hashed_kern_low_water_mark
= MIN(pv_hashed_kern_low_water_mark
, 16000);
292 pv_hashed_kern_alloc_chunk
= PV_HASHED_KERN_ALLOC_CHUNK_INITIAL
;
293 pv_hashed_alloc_chunk
= PV_HASHED_ALLOC_CHUNK_INITIAL
;
296 pvh_eh
= pvh_et
= PV_HASHED_ENTRY_NULL
;
298 for (i
= 0; i
< (5 * PV_HASHED_ALLOC_CHUNK_INITIAL
); i
++) {
299 pvh_e
= (pv_hashed_entry_t
) zalloc(pv_hashed_list_zone
);
301 pvh_e
->qlink
.next
= (queue_entry_t
)pvh_eh
;
304 if (pvh_et
== PV_HASHED_ENTRY_NULL
)
308 PV_HASHED_FREE_LIST(pvh_eh
, pvh_et
, pv_cnt
);
311 pvh_eh
= pvh_et
= PV_HASHED_ENTRY_NULL
;
312 for (i
= 0; i
< PV_HASHED_KERN_ALLOC_CHUNK_INITIAL
; i
++) {
313 pvh_e
= (pv_hashed_entry_t
) zalloc(pv_hashed_list_zone
);
315 pvh_e
->qlink
.next
= (queue_entry_t
)pvh_eh
;
318 if (pvh_et
== PV_HASHED_ENTRY_NULL
)
322 PV_HASHED_KERN_FREE_LIST(pvh_eh
, pvh_et
, pv_cnt
);
325 void mapping_replenish(void);
327 void mapping_adjust(void) {
330 pmap_pagetable_corruption_log_setup();
332 mres
= kernel_thread_start_priority((thread_continue_t
)mapping_replenish
, NULL
, MAXPRI_KERNEL
, &mapping_replenish_thread
);
333 if (mres
!= KERN_SUCCESS
) {
334 panic("pmap: mapping_replenish_thread creation failed");
336 thread_deallocate(mapping_replenish_thread
);
339 unsigned pmap_mapping_thread_wakeups
;
340 unsigned pmap_kernel_reserve_replenish_stat
;
341 unsigned pmap_user_reserve_replenish_stat
;
342 unsigned pmap_kern_reserve_alloc_stat
;
344 void mapping_replenish(void)
346 pv_hashed_entry_t pvh_e
;
347 pv_hashed_entry_t pvh_eh
;
348 pv_hashed_entry_t pvh_et
;
352 /* We qualify for VM privileges...*/
353 current_thread()->options
|= TH_OPT_VMPRIV
;
357 while (pv_hashed_kern_free_count
< pv_hashed_kern_low_water_mark
) {
359 pvh_eh
= pvh_et
= PV_HASHED_ENTRY_NULL
;
361 for (i
= 0; i
< pv_hashed_kern_alloc_chunk
; i
++) {
362 pvh_e
= (pv_hashed_entry_t
) zalloc(pv_hashed_list_zone
);
363 pvh_e
->qlink
.next
= (queue_entry_t
)pvh_eh
;
366 if (pvh_et
== PV_HASHED_ENTRY_NULL
)
370 pmap_kernel_reserve_replenish_stat
+= pv_cnt
;
371 PV_HASHED_KERN_FREE_LIST(pvh_eh
, pvh_et
, pv_cnt
);
375 pvh_eh
= pvh_et
= PV_HASHED_ENTRY_NULL
;
377 if (pv_hashed_free_count
< pv_hashed_low_water_mark
) {
378 for (i
= 0; i
< pv_hashed_alloc_chunk
; i
++) {
379 pvh_e
= (pv_hashed_entry_t
) zalloc(pv_hashed_list_zone
);
381 pvh_e
->qlink
.next
= (queue_entry_t
)pvh_eh
;
384 if (pvh_et
== PV_HASHED_ENTRY_NULL
)
388 pmap_user_reserve_replenish_stat
+= pv_cnt
;
389 PV_HASHED_FREE_LIST(pvh_eh
, pvh_et
, pv_cnt
);
391 /* Wake threads throttled while the kernel reserve was being replenished.
393 if (pmap_pv_throttled_waiters
) {
394 pmap_pv_throttled_waiters
= 0;
395 thread_wakeup(&pmap_user_pv_throttle_event
);
397 /* Check if the kernel pool has been depleted since the
398 * first pass, to reduce refill latency.
400 if (pv_hashed_kern_free_count
< pv_hashed_kern_low_water_mark
)
402 /* Block sans continuation to avoid yielding kernel stack */
403 assert_wait(&mapping_replenish_event
, THREAD_UNINT
);
405 thread_block(THREAD_CONTINUE_NULL
);
406 pmap_mapping_thread_wakeups
++;
411 * Set specified attribute bits.
422 assert(pn
!= vm_page_fictitious_addr
);
423 if (pn
== vm_page_guard_addr
)
426 pai
= ppn_to_pai(pn
);
428 if (!IS_MANAGED_PAGE(pai
)) {
429 /* Not a managed page. */
434 pmap_phys_attributes
[pai
] |= bits
;
439 * Set the modify bit on the specified physical page.
443 pmap_set_modify(ppnum_t pn
)
445 phys_attribute_set(pn
, PHYS_MODIFIED
);
449 * Clear the modify bits on the specified physical page.
453 pmap_clear_modify(ppnum_t pn
)
455 phys_attribute_clear(pn
, PHYS_MODIFIED
, 0, NULL
);
461 * Return whether or not the specified physical page is modified
462 * by any physical maps.
466 pmap_is_modified(ppnum_t pn
)
468 if (phys_attribute_test(pn
, PHYS_MODIFIED
))
475 * pmap_clear_reference:
477 * Clear the reference bit on the specified physical page.
481 pmap_clear_reference(ppnum_t pn
)
483 phys_attribute_clear(pn
, PHYS_REFERENCED
, 0, NULL
);
487 pmap_set_reference(ppnum_t pn
)
489 phys_attribute_set(pn
, PHYS_REFERENCED
);
493 * pmap_is_referenced:
495 * Return whether or not the specified physical page is referenced
496 * by any physical maps.
500 pmap_is_referenced(ppnum_t pn
)
502 if (phys_attribute_test(pn
, PHYS_REFERENCED
))
509 * pmap_get_refmod(phys)
510 * returns the referenced and modified bits of the specified
514 pmap_get_refmod(ppnum_t pn
)
517 unsigned int retval
= 0;
519 refmod
= phys_attribute_test(pn
, PHYS_MODIFIED
| PHYS_REFERENCED
);
521 if (refmod
& PHYS_MODIFIED
)
522 retval
|= VM_MEM_MODIFIED
;
523 if (refmod
& PHYS_REFERENCED
)
524 retval
|= VM_MEM_REFERENCED
;
531 pmap_clear_refmod_options(ppnum_t pn
, unsigned int mask
, unsigned int options
, void *arg
)
533 unsigned int x86Mask
;
535 x86Mask
= ( ((mask
& VM_MEM_MODIFIED
)? PHYS_MODIFIED
: 0)
536 | ((mask
& VM_MEM_REFERENCED
)? PHYS_REFERENCED
: 0));
538 phys_attribute_clear(pn
, x86Mask
, options
, arg
);
542 * pmap_clear_refmod(phys, mask)
543 * clears the referenced and modified bits as specified by the mask
544 * of the specified physical page.
547 pmap_clear_refmod(ppnum_t pn
, unsigned int mask
)
549 unsigned int x86Mask
;
551 x86Mask
= ( ((mask
& VM_MEM_MODIFIED
)? PHYS_MODIFIED
: 0)
552 | ((mask
& VM_MEM_REFERENCED
)? PHYS_REFERENCED
: 0));
554 phys_attribute_clear(pn
, x86Mask
, 0, NULL
);
558 pmap_disconnect(ppnum_t pa
)
560 return (pmap_disconnect_options(pa
, 0, NULL
));
565 * pmap_disconnect_options
568 * Disconnect all mappings for this page and return reference and change status
573 pmap_disconnect_options(ppnum_t pa
, unsigned int options
, void *arg
)
575 unsigned refmod
, vmrefmod
= 0;
577 pmap_page_protect_options(pa
, 0, options
, arg
); /* disconnect the page */
579 pmap_assert(pa
!= vm_page_fictitious_addr
);
580 if ((pa
== vm_page_guard_addr
) || !IS_MANAGED_PAGE(pa
) || (options
& PMAP_OPTIONS_NOREFMOD
))
582 refmod
= pmap_phys_attributes
[pa
] & (PHYS_MODIFIED
| PHYS_REFERENCED
);
584 if (refmod
& PHYS_MODIFIED
)
585 vmrefmod
|= VM_MEM_MODIFIED
;
586 if (refmod
& PHYS_REFERENCED
)
587 vmrefmod
|= VM_MEM_REFERENCED
;