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 <i386/pmap_internal.h>
32 * Each entry in the pv_head_table is locked by a bit in the
33 * pv_lock_table. The lock bits are accessed by the physical
34 * address of the page they lock.
37 char *pv_lock_table
; /* pointer to array of bits */
38 char *pv_hash_lock_table
;
40 pv_rooted_entry_t pv_head_table
; /* array of entries, one per
42 uint32_t pv_hashed_free_count
= 0;
43 uint32_t pv_hashed_kern_free_count
= 0;
45 pmap_pagetable_corruption_record_t pmap_pagetable_corruption_records
[PMAP_PAGETABLE_CORRUPTION_MAX_LOG
];
46 uint32_t pmap_pagetable_corruption_incidents
;
47 uint64_t pmap_pagetable_corruption_last_abstime
= (~(0ULL) >> 1);
48 uint64_t pmap_pagetable_corruption_interval_abstime
;
49 thread_call_t pmap_pagetable_corruption_log_call
;
50 static thread_call_data_t pmap_pagetable_corruption_log_call_data
;
51 boolean_t pmap_pagetable_corruption_timeout
= FALSE
;
53 volatile uint32_t mappingrecurse
= 0;
55 uint32_t pv_hashed_low_water_mark
, pv_hashed_kern_low_water_mark
, pv_hashed_alloc_chunk
, pv_hashed_kern_alloc_chunk
;
57 thread_t mapping_replenish_thread
;
58 event_t mapping_replenish_event
, pmap_user_pv_throttle_event
;
60 uint64_t pmap_pv_throttle_stat
, pmap_pv_throttled_waiters
;
62 unsigned int pmap_cache_attributes(ppnum_t pn
) {
63 if (pmap_get_cache_attributes(pn
) & INTEL_PTE_NCACHE
)
66 return (VM_WIMG_COPYBACK
);
69 void pmap_set_cache_attributes(ppnum_t pn
, unsigned int cacheattr
) {
70 unsigned int current
, template = 0;
73 if (cacheattr
& VM_MEM_NOT_CACHEABLE
) {
74 if(!(cacheattr
& VM_MEM_GUARDED
))
76 template |= PHYS_NCACHE
;
81 assert((pn
!= vm_page_fictitious_addr
) && (pn
!= vm_page_guard_addr
));
85 if (!IS_MANAGED_PAGE(pai
)) {
89 /* override cache attributes for this phys page
90 * Does not walk through existing mappings to adjust,
91 * assumes page is disconnected
96 pmap_update_cache_attributes_locked(pn
, template);
98 current
= pmap_phys_attributes
[pai
] & PHYS_CACHEABILITY_MASK
;
99 pmap_phys_attributes
[pai
] &= ~PHYS_CACHEABILITY_MASK
;
100 pmap_phys_attributes
[pai
] |= template;
104 if ((template & PHYS_NCACHE
) && !(current
& PHYS_NCACHE
)) {
105 pmap_sync_page_attributes_phys(pn
);
109 unsigned pmap_get_cache_attributes(ppnum_t pn
) {
110 if (last_managed_page
== 0)
113 if (!IS_MANAGED_PAGE(ppn_to_pai(pn
))) {
114 return INTEL_PTE_NCACHE
;
118 * The cache attributes are read locklessly for efficiency.
120 unsigned int attr
= pmap_phys_attributes
[ppn_to_pai(pn
)];
121 unsigned int template = 0;
124 template |= INTEL_PTE_PTA
;
125 if (attr
& PHYS_NCACHE
)
126 template |= INTEL_PTE_NCACHE
;
133 pmap_is_noencrypt(ppnum_t pn
)
137 pai
= ppn_to_pai(pn
);
139 if (!IS_MANAGED_PAGE(pai
))
142 if (pmap_phys_attributes
[pai
] & PHYS_NOENCRYPT
)
150 pmap_set_noencrypt(ppnum_t pn
)
154 pai
= ppn_to_pai(pn
);
156 if (IS_MANAGED_PAGE(pai
)) {
159 pmap_phys_attributes
[pai
] |= PHYS_NOENCRYPT
;
167 pmap_clear_noencrypt(ppnum_t pn
)
171 pai
= ppn_to_pai(pn
);
173 if (IS_MANAGED_PAGE(pai
)) {
176 pmap_phys_attributes
[pai
] &= ~PHYS_NOENCRYPT
;
183 compute_pmap_gc_throttle(void *arg __unused
)
189 __private_extern__
void
190 pmap_pagetable_corruption_msg_log(int (*log_func
)(const char * fmt
, ...)__printflike(1,2)) {
191 if (pmap_pagetable_corruption_incidents
> 0) {
192 int i
, e
= MIN(pmap_pagetable_corruption_incidents
, PMAP_PAGETABLE_CORRUPTION_MAX_LOG
);
193 (*log_func
)("%u pagetable corruption incident(s) detected, timeout: %u\n", pmap_pagetable_corruption_incidents
, pmap_pagetable_corruption_timeout
);
194 for (i
= 0; i
< e
; i
++) {
195 (*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
);
201 pmap_pagetable_corruption_log_setup(void) {
202 if (pmap_pagetable_corruption_log_call
== NULL
) {
203 nanotime_to_absolutetime(PMAP_PAGETABLE_CORRUPTION_INTERVAL
, 0, &pmap_pagetable_corruption_interval_abstime
);
204 thread_call_setup(&pmap_pagetable_corruption_log_call_data
,
205 (thread_call_func_t
) pmap_pagetable_corruption_msg_log
,
206 (thread_call_param_t
) &printf
);
207 pmap_pagetable_corruption_log_call
= &pmap_pagetable_corruption_log_call_data
;
212 mapping_free_prime(void)
215 pv_hashed_entry_t pvh_e
;
216 pv_hashed_entry_t pvh_eh
;
217 pv_hashed_entry_t pvh_et
;
220 /* Scale based on DRAM size */
221 pv_hashed_low_water_mark
= MAX(PV_HASHED_LOW_WATER_MARK_DEFAULT
, ((uint32_t)(sane_size
>> 30)) * 2000);
222 pv_hashed_low_water_mark
= MIN(pv_hashed_low_water_mark
, 16000);
223 /* Alterable via sysctl */
224 pv_hashed_kern_low_water_mark
= MAX(PV_HASHED_KERN_LOW_WATER_MARK_DEFAULT
, ((uint32_t)(sane_size
>> 30)) * 1000);
225 pv_hashed_kern_low_water_mark
= MIN(pv_hashed_kern_low_water_mark
, 16000);
226 pv_hashed_kern_alloc_chunk
= PV_HASHED_KERN_ALLOC_CHUNK_INITIAL
;
227 pv_hashed_alloc_chunk
= PV_HASHED_ALLOC_CHUNK_INITIAL
;
230 pvh_eh
= pvh_et
= PV_HASHED_ENTRY_NULL
;
232 for (i
= 0; i
< (5 * PV_HASHED_ALLOC_CHUNK_INITIAL
); i
++) {
233 pvh_e
= (pv_hashed_entry_t
) zalloc(pv_hashed_list_zone
);
235 pvh_e
->qlink
.next
= (queue_entry_t
)pvh_eh
;
238 if (pvh_et
== PV_HASHED_ENTRY_NULL
)
242 PV_HASHED_FREE_LIST(pvh_eh
, pvh_et
, pv_cnt
);
245 pvh_eh
= pvh_et
= PV_HASHED_ENTRY_NULL
;
246 for (i
= 0; i
< PV_HASHED_KERN_ALLOC_CHUNK_INITIAL
; i
++) {
247 pvh_e
= (pv_hashed_entry_t
) zalloc(pv_hashed_list_zone
);
249 pvh_e
->qlink
.next
= (queue_entry_t
)pvh_eh
;
252 if (pvh_et
== PV_HASHED_ENTRY_NULL
)
256 PV_HASHED_KERN_FREE_LIST(pvh_eh
, pvh_et
, pv_cnt
);
259 void mapping_replenish(void);
261 void mapping_adjust(void) {
264 pmap_pagetable_corruption_log_setup();
266 mres
= kernel_thread_start_priority((thread_continue_t
)mapping_replenish
, NULL
, MAXPRI_KERNEL
, &mapping_replenish_thread
);
267 if (mres
!= KERN_SUCCESS
) {
268 panic("pmap: mapping_replenish_thread creation failed");
270 thread_deallocate(mapping_replenish_thread
);
273 unsigned pmap_mapping_thread_wakeups
;
274 unsigned pmap_kernel_reserve_replenish_stat
;
275 unsigned pmap_user_reserve_replenish_stat
;
276 unsigned pmap_kern_reserve_alloc_stat
;
278 void mapping_replenish(void)
280 pv_hashed_entry_t pvh_e
;
281 pv_hashed_entry_t pvh_eh
;
282 pv_hashed_entry_t pvh_et
;
286 /* We qualify for VM privileges...*/
287 current_thread()->options
|= TH_OPT_VMPRIV
;
291 while (pv_hashed_kern_free_count
< pv_hashed_kern_low_water_mark
) {
293 pvh_eh
= pvh_et
= PV_HASHED_ENTRY_NULL
;
295 for (i
= 0; i
< pv_hashed_kern_alloc_chunk
; i
++) {
296 pvh_e
= (pv_hashed_entry_t
) zalloc(pv_hashed_list_zone
);
297 pvh_e
->qlink
.next
= (queue_entry_t
)pvh_eh
;
300 if (pvh_et
== PV_HASHED_ENTRY_NULL
)
304 pmap_kernel_reserve_replenish_stat
+= pv_cnt
;
305 PV_HASHED_KERN_FREE_LIST(pvh_eh
, pvh_et
, pv_cnt
);
309 pvh_eh
= pvh_et
= PV_HASHED_ENTRY_NULL
;
311 if (pv_hashed_free_count
< pv_hashed_low_water_mark
) {
312 for (i
= 0; i
< pv_hashed_alloc_chunk
; 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 pmap_user_reserve_replenish_stat
+= pv_cnt
;
323 PV_HASHED_FREE_LIST(pvh_eh
, pvh_et
, pv_cnt
);
325 /* Wake threads throttled while the kernel reserve was being replenished.
327 if (pmap_pv_throttled_waiters
) {
328 pmap_pv_throttled_waiters
= 0;
329 thread_wakeup(&pmap_user_pv_throttle_event
);
331 /* Check if the kernel pool has been depleted since the
332 * first pass, to reduce refill latency.
334 if (pv_hashed_kern_free_count
< pv_hashed_kern_low_water_mark
)
336 /* Block sans continuation to avoid yielding kernel stack */
337 assert_wait(&mapping_replenish_event
, THREAD_UNINT
);
339 thread_block(THREAD_CONTINUE_NULL
);
340 pmap_mapping_thread_wakeups
++;
345 * Set specified attribute bits.
356 assert(pn
!= vm_page_fictitious_addr
);
357 if (pn
== vm_page_guard_addr
)
360 pai
= ppn_to_pai(pn
);
362 if (!IS_MANAGED_PAGE(pai
)) {
363 /* Not a managed page. */
368 pmap_phys_attributes
[pai
] |= bits
;
373 * Set the modify bit on the specified physical page.
377 pmap_set_modify(ppnum_t pn
)
379 phys_attribute_set(pn
, PHYS_MODIFIED
);
383 * Clear the modify bits on the specified physical page.
387 pmap_clear_modify(ppnum_t pn
)
389 phys_attribute_clear(pn
, PHYS_MODIFIED
);
395 * Return whether or not the specified physical page is modified
396 * by any physical maps.
400 pmap_is_modified(ppnum_t pn
)
402 if (phys_attribute_test(pn
, PHYS_MODIFIED
))
409 * pmap_clear_reference:
411 * Clear the reference bit on the specified physical page.
415 pmap_clear_reference(ppnum_t pn
)
417 phys_attribute_clear(pn
, PHYS_REFERENCED
);
421 pmap_set_reference(ppnum_t pn
)
423 phys_attribute_set(pn
, PHYS_REFERENCED
);
427 * pmap_is_referenced:
429 * Return whether or not the specified physical page is referenced
430 * by any physical maps.
434 pmap_is_referenced(ppnum_t pn
)
436 if (phys_attribute_test(pn
, PHYS_REFERENCED
))
443 * pmap_get_refmod(phys)
444 * returns the referenced and modified bits of the specified
448 pmap_get_refmod(ppnum_t pn
)
451 unsigned int retval
= 0;
453 refmod
= phys_attribute_test(pn
, PHYS_MODIFIED
| PHYS_REFERENCED
);
455 if (refmod
& PHYS_MODIFIED
)
456 retval
|= VM_MEM_MODIFIED
;
457 if (refmod
& PHYS_REFERENCED
)
458 retval
|= VM_MEM_REFERENCED
;
464 * pmap_clear_refmod(phys, mask)
465 * clears the referenced and modified bits as specified by the mask
466 * of the specified physical page.
469 pmap_clear_refmod(ppnum_t pn
, unsigned int mask
)
471 unsigned int x86Mask
;
473 x86Mask
= ( ((mask
& VM_MEM_MODIFIED
)? PHYS_MODIFIED
: 0)
474 | ((mask
& VM_MEM_REFERENCED
)? PHYS_REFERENCED
: 0));
475 phys_attribute_clear(pn
, x86Mask
);
483 * Disconnect all mappings for this page and return reference and change status
488 pmap_disconnect(ppnum_t pa
)
490 unsigned refmod
, vmrefmod
= 0;
492 pmap_page_protect(pa
, 0); /* disconnect the page */
494 pmap_assert(pa
!= vm_page_fictitious_addr
);
495 if ((pa
== vm_page_guard_addr
) || !IS_MANAGED_PAGE(pa
))
497 refmod
= pmap_phys_attributes
[pa
] & (PHYS_MODIFIED
| PHYS_REFERENCED
);
499 if (refmod
& PHYS_MODIFIED
)
500 vmrefmod
|= VM_MEM_MODIFIED
;
501 if (refmod
& PHYS_REFERENCED
)
502 vmrefmod
|= VM_MEM_REFERENCED
;