]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/pmap.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / osfmk / i386 / pmap.c
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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@
1c79356b
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/*
57 */
58
59/*
60 * File: pmap.c
61 * Author: Avadis Tevanian, Jr., Michael Wayne Young
62 * (These guys wrote the Vax version)
63 *
64 * Physical Map management code for Intel i386, i486, and i860.
65 *
66 * Manages physical address maps.
67 *
68 * In addition to hardware address maps, this
69 * module is called upon to provide software-use-only
70 * maps which may or may not be stored in the same
71 * form as hardware maps. These pseudo-maps are
72 * used to store intermediate results from copy
73 * operations to and from address spaces.
74 *
75 * Since the information managed by this module is
76 * also stored by the logical address mapping module,
77 * this module may throw away valid virtual-to-physical
78 * mappings at almost any time. However, invalidations
79 * of virtual-to-physical mappings must be done as
80 * requested.
81 *
82 * In order to cope with hardware architectures which
83 * make virtual-to-physical map invalidates expensive,
84 * this module may delay invalidate or reduced protection
85 * operations until such time as they are actually
86 * necessary. This module is given full information as
87 * to which processors are currently using which maps,
88 * and to when physical maps must be made correct.
89 */
90
1c79356b 91#include <string.h>
1c79356b
A
92#include <mach_kdb.h>
93#include <mach_ldebug.h>
94
2d21ac55
A
95#include <libkern/OSAtomic.h>
96
1c79356b
A
97#include <mach/machine/vm_types.h>
98
99#include <mach/boolean.h>
100#include <kern/thread.h>
101#include <kern/zalloc.h>
2d21ac55 102#include <kern/queue.h>
1c79356b
A
103
104#include <kern/lock.h>
91447636 105#include <kern/kalloc.h>
1c79356b
A
106#include <kern/spl.h>
107
108#include <vm/pmap.h>
109#include <vm/vm_map.h>
110#include <vm/vm_kern.h>
111#include <mach/vm_param.h>
112#include <mach/vm_prot.h>
113#include <vm/vm_object.h>
114#include <vm/vm_page.h>
115
116#include <mach/machine/vm_param.h>
117#include <machine/thread.h>
118
119#include <kern/misc_protos.h> /* prototyping */
120#include <i386/misc_protos.h>
121
122#include <i386/cpuid.h>
91447636 123#include <i386/cpu_data.h>
55e303ae
A
124#include <i386/cpu_number.h>
125#include <i386/machine_cpu.h>
0c530ab8 126#include <i386/seg.h>
2d21ac55 127#include <i386/serial_io.h>
0c530ab8 128#include <i386/cpu_capabilities.h>
2d21ac55
A
129#include <i386/machine_routines.h>
130#include <i386/proc_reg.h>
131#include <i386/tsc.h>
b0d623f7
A
132#include <i386/acpi.h>
133#include <i386/pmap_internal.h>
1c79356b
A
134
135#if MACH_KDB
136#include <ddb/db_command.h>
137#include <ddb/db_output.h>
138#include <ddb/db_sym.h>
139#include <ddb/db_print.h>
140#endif /* MACH_KDB */
141
91447636
A
142#include <vm/vm_protos.h>
143
144#include <i386/mp.h>
0c530ab8 145#include <i386/mp_desc.h>
b0d623f7 146#include <i386/i386_lowmem.h>
6d2010ae 147#include <i386/lowglobals.h>
0c530ab8 148
0c530ab8 149
2d21ac55
A
150/* #define DEBUGINTERRUPTS 1 uncomment to ensure pmap callers have interrupts enabled */
151#ifdef DEBUGINTERRUPTS
152#define pmap_intr_assert() {if (processor_avail_count > 1 && !ml_get_interrupts_enabled()) panic("pmap interrupt assert %s, %d",__FILE__, __LINE__);}
153#else
154#define pmap_intr_assert()
155#endif
156
0c530ab8
A
157#ifdef IWANTTODEBUG
158#undef DEBUG
159#define DEBUG 1
160#define POSTCODE_DELAY 1
161#include <i386/postcode.h>
162#endif /* IWANTTODEBUG */
1c79356b 163
0c530ab8
A
164#ifdef PMAP_DEBUG
165void dump_pmap(pmap_t);
166void dump_4GB_pdpt(pmap_t p);
167void dump_4GB_pdpt_thread(thread_t tp);
168#endif
1c79356b 169
0c530ab8 170int nx_enabled = 1; /* enable no-execute protection */
4a3eedf9
A
171#ifdef CONFIG_EMBEDDED
172int allow_data_exec = 0; /* no exec from data, embedded is hardcore like that */
173#else
2d21ac55 174int allow_data_exec = VM_ABI_32; /* 32-bit apps may execute data by default, 64-bit apps may not */
4a3eedf9 175#endif
2d21ac55 176int allow_stack_exec = 0; /* No apps may execute from the stack by default */
0c530ab8 177
6d2010ae 178#if CONFIG_YONAH
b0d623f7 179boolean_t cpu_64bit = FALSE;
6d2010ae
A
180#else
181const boolean_t cpu_64bit = TRUE;
182#endif
b0d623f7 183boolean_t pmap_trace = FALSE;
1c79356b 184
2d21ac55
A
185uint64_t max_preemption_latency_tsc = 0;
186
2d21ac55
A
187pv_hashed_entry_t *pv_hash_table; /* hash lists */
188
189uint32_t npvhash = 0;
190
1c79356b
A
191/*
192 * pv_list entries are kept on a list that can only be accessed
193 * with the pmap system locked (at SPLVM, not in the cpus_active set).
2d21ac55 194 * The list is refilled from the pv_hashed_list_zone if it becomes empty.
1c79356b 195 */
2d21ac55
A
196pv_rooted_entry_t pv_free_list = PV_ROOTED_ENTRY_NULL; /* free list at SPLVM */
197pv_hashed_entry_t pv_hashed_free_list = PV_HASHED_ENTRY_NULL;
198pv_hashed_entry_t pv_hashed_kern_free_list = PV_HASHED_ENTRY_NULL;
199decl_simple_lock_data(,pv_hashed_free_list_lock)
200decl_simple_lock_data(,pv_hashed_kern_free_list_lock)
201decl_simple_lock_data(,pv_hash_table_lock)
202
2d21ac55 203zone_t pv_hashed_list_zone; /* zone of pv_hashed_entry structures */
1c79356b 204
91447636 205static zone_t pdpt_zone;
91447636 206
1c79356b
A
207/*
208 * First and last physical addresses that we maintain any information
209 * for. Initialized to zero so that pmap operations done before
210 * pmap_init won't touch any non-existent structures.
211 */
1c79356b
A
212boolean_t pmap_initialized = FALSE;/* Has pmap_init completed? */
213
91447636
A
214static struct vm_object kptobj_object_store;
215static vm_object_t kptobj;
91447636 216
6d2010ae
A
217/*
218 * Index into pv_head table, its lock bits, and the modify/reference and managed bits
219 */
220
1c79356b
A
221/*
222 * Array of physical page attribites for managed pages.
223 * One byte per physical page.
224 */
225char *pmap_phys_attributes;
2d21ac55 226unsigned int last_managed_page = 0;
1c79356b 227
0c530ab8 228uint64_t pde_mapped_size;
1c79356b 229
1c79356b
A
230/*
231 * Locking and TLB invalidation
232 */
233
234/*
2d21ac55 235 * Locking Protocols: (changed 2/2007 JK)
1c79356b
A
236 *
237 * There are two structures in the pmap module that need locking:
238 * the pmaps themselves, and the per-page pv_lists (which are locked
239 * by locking the pv_lock_table entry that corresponds to the pv_head
240 * for the list in question.) Most routines want to lock a pmap and
241 * then do operations in it that require pv_list locking -- however
242 * pmap_remove_all and pmap_copy_on_write operate on a physical page
243 * basis and want to do the locking in the reverse order, i.e. lock
244 * a pv_list and then go through all the pmaps referenced by that list.
1c79356b 245 *
2d21ac55
A
246 * The system wide pmap lock has been removed. Now, paths take a lock
247 * on the pmap before changing its 'shape' and the reverse order lockers
248 * (coming in by phys ppn) take a lock on the corresponding pv and then
249 * retest to be sure nothing changed during the window before they locked
250 * and can then run up/down the pv lists holding the list lock. This also
251 * lets the pmap layer run (nearly completely) interrupt enabled, unlike
252 * previously.
1c79356b 253 */
1c79356b 254
1c79356b 255
2d21ac55
A
256/*
257 * PV locking
258 */
259
260#define LOCK_PVH(index) { \
261 mp_disable_preemption(); \
262 lock_pvh_pai(index); \
1c79356b
A
263}
264
2d21ac55
A
265#define UNLOCK_PVH(index) { \
266 unlock_pvh_pai(index); \
267 mp_enable_preemption(); \
1c79356b
A
268}
269
2d21ac55
A
270/*
271 * PV hash locking
272 */
1c79356b 273
2d21ac55
A
274#define LOCK_PV_HASH(hash) lock_hash_hash(hash)
275
276#define UNLOCK_PV_HASH(hash) unlock_hash_hash(hash)
1c79356b 277
55e303ae
A
278#if USLOCK_DEBUG
279extern int max_lock_loops;
91447636
A
280#define LOOP_VAR \
281 unsigned int loop_count; \
2d21ac55 282 loop_count = disable_serial_output ? max_lock_loops \
91447636 283 : max_lock_loops*100
55e303ae 284#define LOOP_CHECK(msg, pmap) \
91447636 285 if (--loop_count == 0) { \
55e303ae 286 mp_disable_preemption(); \
0c530ab8
A
287 kprintf("%s: cpu %d pmap %x\n", \
288 msg, cpu_number(), pmap); \
55e303ae
A
289 Debugger("deadlock detection"); \
290 mp_enable_preemption(); \
91447636 291 loop_count = max_lock_loops; \
55e303ae
A
292 }
293#else /* USLOCK_DEBUG */
294#define LOOP_VAR
295#define LOOP_CHECK(msg, pmap)
296#endif /* USLOCK_DEBUG */
1c79356b 297
b0d623f7
A
298unsigned pmap_memory_region_count;
299unsigned pmap_memory_region_current;
1c79356b 300
91447636 301pmap_memory_region_t pmap_memory_regions[PMAP_MEMORY_REGIONS_SIZE];
1c79356b
A
302
303/*
304 * Other useful macros.
305 */
91447636 306#define current_pmap() (vm_map_pmap(current_thread()->map))
1c79356b
A
307
308struct pmap kernel_pmap_store;
309pmap_t kernel_pmap;
310
0c530ab8
A
311pd_entry_t high_shared_pde;
312pd_entry_t commpage64_pde;
91447636 313
1c79356b
A
314struct zone *pmap_zone; /* zone of pmap structures */
315
316int pmap_debug = 0; /* flag for debugging prints */
91447636 317
2d21ac55 318unsigned int inuse_ptepages_count = 0;
6d2010ae
A
319long long alloc_ptepages_count __attribute__((aligned(8))) = 0LL; /* aligned for atomic access */
320unsigned int bootstrap_wired_pages = 0;
321int pt_fake_zone_index = -1;
322
323extern long NMIPI_acks;
324
325static inline void
326PMAP_ZINFO_SALLOC(vm_size_t bytes)
327{
328 current_thread()->tkm_shared.alloc += bytes;
329}
330
331static inline void
332PMAP_ZINFO_SFREE(vm_size_t bytes)
333{
334 current_thread()->tkm_shared.free += (bytes);
335}
1c79356b 336
0c530ab8
A
337addr64_t kernel64_cr3;
338boolean_t no_shared_cr3 = FALSE; /* -no_shared_cr3 boot arg */
339
6d2010ae
A
340boolean_t kernel_text_ps_4K = TRUE;
341boolean_t wpkernel = TRUE;
1c79356b 342
1c79356b 343extern char end;
91447636
A
344static int nkpt;
345
346pt_entry_t *DMAP1, *DMAP2;
347caddr_t DADDR1;
348caddr_t DADDR2;
6d2010ae 349
0c530ab8
A
350/*
351 * for legacy, returns the address of the pde entry.
352 * for 64 bit, causes the pdpt page containing the pde entry to be mapped,
353 * then returns the mapped address of the pde entry in that page
354 */
355pd_entry_t *
356pmap_pde(pmap_t m, vm_map_offset_t v)
4452a7af 357{
0c530ab8
A
358 pd_entry_t *pde;
359 if (!cpu_64bit || (m == kernel_pmap)) {
360 pde = (&((m)->dirbase[(vm_offset_t)(v) >> PDESHIFT]));
361 } else {
362 assert(m);
363 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
364 pde = pmap64_pde(m, v);
365 }
366 return pde;
4452a7af
A
367}
368
4452a7af 369/*
0c530ab8
A
370 * the single pml4 page per pmap is allocated at pmap create time and exists
371 * for the duration of the pmap. we allocate this page in kernel vm (to save us one
372 * level of page table dynamic mapping.
373 * this returns the address of the requested pml4 entry in the top level page.
4452a7af 374 */
0c530ab8
A
375static inline
376pml4_entry_t *
377pmap64_pml4(pmap_t pmap, vm_map_offset_t vaddr)
378{
379 return ((pml4_entry_t *)pmap->pm_hold + ((vm_offset_t)((vaddr>>PML4SHIFT)&(NPML4PG-1))));
380}
381
382/*
383 * maps in the pml4 page, if any, containing the pdpt entry requested
384 * and returns the address of the pdpt entry in that mapped page
385 */
386pdpt_entry_t *
387pmap64_pdpt(pmap_t pmap, vm_map_offset_t vaddr)
388{
389 pml4_entry_t newpf;
390 pml4_entry_t *pml4;
391 int i;
392
393 assert(pmap);
394 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
395 if ((vaddr > 0x00007FFFFFFFFFFFULL) && (vaddr < 0xFFFF800000000000ULL)) {
396 return(0);
4452a7af 397 }
0c530ab8
A
398
399 pml4 = pmap64_pml4(pmap, vaddr);
400
401 if (pml4 && ((*pml4 & INTEL_PTE_VALID))) {
402
403 newpf = *pml4 & PG_FRAME;
404
405
406 for (i=PMAP_PDPT_FIRST_WINDOW; i < PMAP_PDPT_FIRST_WINDOW+PMAP_PDPT_NWINDOWS; i++) {
407 if (((*(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP)) & PG_FRAME) == newpf) {
408 return((pdpt_entry_t *)(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR) +
409 ((vm_offset_t)((vaddr>>PDPTSHIFT)&(NPDPTPG-1))));
410 }
411 }
412
413 current_cpu_datap()->cpu_pmap->pdpt_window_index++;
414 if (current_cpu_datap()->cpu_pmap->pdpt_window_index > (PMAP_PDPT_FIRST_WINDOW+PMAP_PDPT_NWINDOWS-1))
415 current_cpu_datap()->cpu_pmap->pdpt_window_index = PMAP_PDPT_FIRST_WINDOW;
416 pmap_store_pte(
417 (current_cpu_datap()->cpu_pmap->mapwindow[current_cpu_datap()->cpu_pmap->pdpt_window_index].prv_CMAP),
418 newpf | INTEL_PTE_RW | INTEL_PTE_VALID);
419 invlpg((u_int)(current_cpu_datap()->cpu_pmap->mapwindow[current_cpu_datap()->cpu_pmap->pdpt_window_index].prv_CADDR));
420 return ((pdpt_entry_t *)(current_cpu_datap()->cpu_pmap->mapwindow[current_cpu_datap()->cpu_pmap->pdpt_window_index].prv_CADDR) +
421 ((vm_offset_t)((vaddr>>PDPTSHIFT)&(NPDPTPG-1))));
422 }
423
2d21ac55 424 return (NULL);
4452a7af
A
425}
426
0c530ab8
A
427/*
428 * maps in the pdpt page, if any, containing the pde entry requested
429 * and returns the address of the pde entry in that mapped page
430 */
431pd_entry_t *
432pmap64_pde(pmap_t pmap, vm_map_offset_t vaddr)
4452a7af 433{
0c530ab8
A
434 pdpt_entry_t newpf;
435 pdpt_entry_t *pdpt;
436 int i;
4452a7af 437
0c530ab8
A
438 assert(pmap);
439 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
440 if ((vaddr > 0x00007FFFFFFFFFFFULL) && (vaddr < 0xFFFF800000000000ULL)) {
441 return(0);
442 }
443
444 /* if (vaddr & (1ULL << 63)) panic("neg addr");*/
445 pdpt = pmap64_pdpt(pmap, vaddr);
446
447 if (pdpt && ((*pdpt & INTEL_PTE_VALID))) {
448
449 newpf = *pdpt & PG_FRAME;
450
451 for (i=PMAP_PDE_FIRST_WINDOW; i < PMAP_PDE_FIRST_WINDOW+PMAP_PDE_NWINDOWS; i++) {
452 if (((*(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP)) & PG_FRAME) == newpf) {
453 return((pd_entry_t *)(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR) +
454 ((vm_offset_t)((vaddr>>PDSHIFT)&(NPDPG-1))));
455 }
4452a7af 456 }
0c530ab8
A
457
458 current_cpu_datap()->cpu_pmap->pde_window_index++;
459 if (current_cpu_datap()->cpu_pmap->pde_window_index > (PMAP_PDE_FIRST_WINDOW+PMAP_PDE_NWINDOWS-1))
460 current_cpu_datap()->cpu_pmap->pde_window_index = PMAP_PDE_FIRST_WINDOW;
461 pmap_store_pte(
462 (current_cpu_datap()->cpu_pmap->mapwindow[current_cpu_datap()->cpu_pmap->pde_window_index].prv_CMAP),
463 newpf | INTEL_PTE_RW | INTEL_PTE_VALID);
464 invlpg((u_int)(current_cpu_datap()->cpu_pmap->mapwindow[current_cpu_datap()->cpu_pmap->pde_window_index].prv_CADDR));
465 return ((pd_entry_t *)(current_cpu_datap()->cpu_pmap->mapwindow[current_cpu_datap()->cpu_pmap->pde_window_index].prv_CADDR) +
466 ((vm_offset_t)((vaddr>>PDSHIFT)&(NPDPG-1))));
21362eb3 467 }
4452a7af 468
2d21ac55 469 return (NULL);
0c530ab8
A
470}
471
2d21ac55
A
472/*
473 * Because the page tables (top 3 levels) are mapped into per cpu windows,
474 * callers must either disable interrupts or disable preemption before calling
475 * one of the pte mapping routines (e.g. pmap_pte()) as the returned vaddr
476 * is in one of those mapped windows and that cannot be allowed to change until
477 * the caller is done using the returned pte pointer. When done, the caller
478 * restores interrupts or preemption to its previous state after which point the
479 * vaddr for the returned pte can no longer be used
480 */
0c530ab8
A
481
482
483/*
484 * return address of mapped pte for vaddr va in pmap pmap.
485 * must be called with pre-emption or interrupts disabled
486 * if targeted pmap is not the kernel pmap
487 * since we may be passing back a virtual address that is
488 * associated with this cpu... pre-emption or interrupts
489 * must remain disabled until the caller is done using
490 * the pointer that was passed back .
491 *
492 * maps the pde page, if any, containing the pte in and returns
493 * the address of the pte in that mapped page
494 */
495pt_entry_t *
496pmap_pte(pmap_t pmap, vm_map_offset_t vaddr)
497{
498 pd_entry_t *pde;
499 pd_entry_t newpf;
500 int i;
501
502 assert(pmap);
503 pde = pmap_pde(pmap,vaddr);
504
505 if (pde && ((*pde & INTEL_PTE_VALID))) {
b0d623f7
A
506 if (*pde & INTEL_PTE_PS)
507 return pde;
2d21ac55
A
508 if (pmap == kernel_pmap)
509 return (vtopte(vaddr)); /* compat kernel still has pte's mapped */
510#if TESTING
511 if (ml_get_interrupts_enabled() && get_preemption_level() == 0)
512 panic("pmap_pte: unsafe call");
513#endif
0c530ab8
A
514 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
515
516 newpf = *pde & PG_FRAME;
517
518 for (i=PMAP_PTE_FIRST_WINDOW; i < PMAP_PTE_FIRST_WINDOW+PMAP_PTE_NWINDOWS; i++) {
519 if (((*(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP)) & PG_FRAME) == newpf) {
520 return((pt_entry_t *)(current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR) +
521 ((vm_offset_t)i386_btop(vaddr) & (NPTEPG-1)));
522 }
523 }
524
525 current_cpu_datap()->cpu_pmap->pte_window_index++;
526 if (current_cpu_datap()->cpu_pmap->pte_window_index > (PMAP_PTE_FIRST_WINDOW+PMAP_PTE_NWINDOWS-1))
527 current_cpu_datap()->cpu_pmap->pte_window_index = PMAP_PTE_FIRST_WINDOW;
528 pmap_store_pte(
529 (current_cpu_datap()->cpu_pmap->mapwindow[current_cpu_datap()->cpu_pmap->pte_window_index].prv_CMAP),
530 newpf | INTEL_PTE_RW | INTEL_PTE_VALID);
531 invlpg((u_int)(current_cpu_datap()->cpu_pmap->mapwindow[current_cpu_datap()->cpu_pmap->pte_window_index].prv_CADDR));
532 return ((pt_entry_t *)(current_cpu_datap()->cpu_pmap->mapwindow[current_cpu_datap()->cpu_pmap->pte_window_index].prv_CADDR) +
533 ((vm_offset_t)i386_btop(vaddr) & (NPTEPG-1)));
6601e61a 534 }
0c530ab8 535
2d21ac55 536 return(NULL);
1c79356b 537}
2d21ac55 538
1c79356b
A
539
540/*
541 * Map memory at initialization. The physical addresses being
542 * mapped are not managed and are never unmapped.
543 *
544 * For now, VM is already on, we only need to map the
545 * specified memory.
546 */
547vm_offset_t
548pmap_map(
0c530ab8
A
549 vm_offset_t virt,
550 vm_map_offset_t start_addr,
551 vm_map_offset_t end_addr,
552 vm_prot_t prot,
553 unsigned int flags)
1c79356b 554{
0c530ab8 555 int ps;
1c79356b
A
556
557 ps = PAGE_SIZE;
91447636 558 while (start_addr < end_addr) {
0c530ab8
A
559 pmap_enter(kernel_pmap, (vm_map_offset_t)virt,
560 (ppnum_t) i386_btop(start_addr), prot, flags, FALSE);
1c79356b 561 virt += ps;
91447636 562 start_addr += ps;
1c79356b
A
563 }
564 return(virt);
565}
566
567/*
568 * Back-door routine for mapping kernel VM at initialization.
569 * Useful for mapping memory outside the range
570 * Sets no-cache, A, D.
1c79356b
A
571 * Otherwise like pmap_map.
572 */
573vm_offset_t
574pmap_map_bd(
0c530ab8
A
575 vm_offset_t virt,
576 vm_map_offset_t start_addr,
577 vm_map_offset_t end_addr,
578 vm_prot_t prot,
579 unsigned int flags)
1c79356b 580{
0c530ab8 581 pt_entry_t template;
b0d623f7 582 pt_entry_t *pte;
2d21ac55 583 spl_t spl;
1c79356b 584
91447636 585 template = pa_to_pte(start_addr)
1c79356b
A
586 | INTEL_PTE_REF
587 | INTEL_PTE_MOD
588 | INTEL_PTE_WIRED
589 | INTEL_PTE_VALID;
0c530ab8
A
590
591 if(flags & (VM_MEM_NOT_CACHEABLE | VM_WIMG_USE_DEFAULT)) {
592 template |= INTEL_PTE_NCACHE;
593 if(!(flags & (VM_MEM_GUARDED | VM_WIMG_USE_DEFAULT)))
594 template |= INTEL_PTE_PTA;
595 }
596
1c79356b
A
597 if (prot & VM_PROT_WRITE)
598 template |= INTEL_PTE_WRITE;
599
91447636 600 while (start_addr < end_addr) {
2d21ac55 601 spl = splhigh();
0c530ab8 602 pte = pmap_pte(kernel_pmap, (vm_map_offset_t)virt);
91447636 603 if (pte == PT_ENTRY_NULL) {
1c79356b 604 panic("pmap_map_bd: Invalid kernel address\n");
91447636 605 }
0c530ab8 606 pmap_store_pte(pte, template);
2d21ac55 607 splx(spl);
1c79356b
A
608 pte_increment_pa(template);
609 virt += PAGE_SIZE;
91447636 610 start_addr += PAGE_SIZE;
b0d623f7
A
611 }
612
55e303ae 613 flush_tlb();
1c79356b
A
614 return(virt);
615}
616
6d2010ae 617extern pmap_paddr_t first_avail;
b0d623f7
A
618extern vm_offset_t virtual_avail, virtual_end;
619extern pmap_paddr_t avail_start, avail_end;
6d2010ae
A
620extern vm_offset_t sHIB;
621extern vm_offset_t eHIB;
622extern vm_offset_t stext;
623extern vm_offset_t etext;
624extern vm_offset_t sdata;
625
626extern void *KPTphys;
1c79356b 627
2d21ac55
A
628void
629pmap_cpu_init(void)
630{
631 /*
632 * Here early in the life of a processor (from cpu_mode_init()).
2d21ac55 633 */
2d21ac55
A
634
635 /*
636 * Initialize the per-cpu, TLB-related fields.
637 */
638 current_cpu_datap()->cpu_active_cr3 = kernel_pmap->pm_cr3;
639 current_cpu_datap()->cpu_tlb_invalid = FALSE;
640}
0c530ab8
A
641
642vm_offset_t
643pmap_high_shared_remap(enum high_fixed_addresses e, vm_offset_t va, int sz)
644{
645 vm_offset_t ve = pmap_index_to_virt(e);
646 pt_entry_t *ptep;
647 pmap_paddr_t pa;
648 int i;
2d21ac55 649 spl_t s;
0c530ab8
A
650
651 assert(0 == (va & PAGE_MASK)); /* expecting page aligned */
2d21ac55 652 s = splhigh();
0c530ab8
A
653 ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)ve);
654
655 for (i=0; i< sz; i++) {
656 pa = (pmap_paddr_t) kvtophys(va);
657 pmap_store_pte(ptep, (pa & PG_FRAME)
658 | INTEL_PTE_VALID
659 | INTEL_PTE_GLOBAL
660 | INTEL_PTE_RW
661 | INTEL_PTE_REF
662 | INTEL_PTE_MOD);
663 va+= PAGE_SIZE;
664 ptep++;
665 }
2d21ac55 666 splx(s);
0c530ab8
A
667 return ve;
668}
669
670vm_offset_t
671pmap_cpu_high_shared_remap(int cpu, enum high_cpu_types e, vm_offset_t va, int sz)
672{
673 enum high_fixed_addresses a = e + HIGH_CPU_END * cpu;
674 return pmap_high_shared_remap(HIGH_FIXED_CPUS_BEGIN + a, va, sz);
675}
676
677void pmap_init_high_shared(void);
678
679extern vm_offset_t gdtptr, idtptr;
680
681extern uint32_t low_intstack;
682
683extern struct fake_descriptor ldt_desc_pattern;
684extern struct fake_descriptor tss_desc_pattern;
685
686extern char hi_remap_text, hi_remap_etext;
687extern char t_zero_div;
688
689pt_entry_t *pte_unique_base;
690
691void
692pmap_init_high_shared(void)
693{
694
695 vm_offset_t haddr;
2d21ac55 696 spl_t s;
0c530ab8
A
697#if MACH_KDB
698 struct i386_tss *ttss;
699#endif
700
b0d623f7
A
701 cpu_desc_index_t * cdi = &cpu_data_master.cpu_desc_index;
702
0c530ab8
A
703 kprintf("HIGH_MEM_BASE 0x%x fixed per-cpu begin 0x%x\n",
704 HIGH_MEM_BASE,pmap_index_to_virt(HIGH_FIXED_CPUS_BEGIN));
2d21ac55 705 s = splhigh();
0c530ab8 706 pte_unique_base = pmap_pte(kernel_pmap, (vm_map_offset_t)pmap_index_to_virt(HIGH_FIXED_CPUS_BEGIN));
2d21ac55 707 splx(s);
0c530ab8
A
708
709 if (i386_btop(&hi_remap_etext - &hi_remap_text + 1) >
710 HIGH_FIXED_TRAMPS_END - HIGH_FIXED_TRAMPS + 1)
711 panic("tramps too large");
712 haddr = pmap_high_shared_remap(HIGH_FIXED_TRAMPS,
713 (vm_offset_t) &hi_remap_text, 3);
714 kprintf("tramp: 0x%x, ",haddr);
0c530ab8
A
715 /* map gdt up high and update ptr for reload */
716 haddr = pmap_high_shared_remap(HIGH_FIXED_GDT,
717 (vm_offset_t) master_gdt, 1);
b0d623f7 718 cdi->cdi_gdt.ptr = (void *)haddr;
0c530ab8
A
719 kprintf("GDT: 0x%x, ",haddr);
720 /* map ldt up high */
721 haddr = pmap_high_shared_remap(HIGH_FIXED_LDT_BEGIN,
722 (vm_offset_t) master_ldt,
723 HIGH_FIXED_LDT_END - HIGH_FIXED_LDT_BEGIN + 1);
b0d623f7 724 cdi->cdi_ldt = (struct fake_descriptor *)haddr;
0c530ab8
A
725 kprintf("LDT: 0x%x, ",haddr);
726 /* put new ldt addr into gdt */
b0d623f7
A
727 struct fake_descriptor temp_fake_desc;
728 temp_fake_desc = ldt_desc_pattern;
729 temp_fake_desc.offset = (vm_offset_t) haddr;
730 fix_desc(&temp_fake_desc, 1);
731
732 *(struct fake_descriptor *) &master_gdt[sel_idx(KERNEL_LDT)] = temp_fake_desc;
733 *(struct fake_descriptor *) &master_gdt[sel_idx(USER_LDT)] = temp_fake_desc;
0c530ab8
A
734
735 /* map idt up high */
736 haddr = pmap_high_shared_remap(HIGH_FIXED_IDT,
737 (vm_offset_t) master_idt, 1);
b0d623f7 738 cdi->cdi_idt.ptr = (void *)haddr;
0c530ab8
A
739 kprintf("IDT: 0x%x, ", haddr);
740 /* remap ktss up high and put new high addr into gdt */
741 haddr = pmap_high_shared_remap(HIGH_FIXED_KTSS,
742 (vm_offset_t) &master_ktss, 1);
b0d623f7
A
743
744 temp_fake_desc = tss_desc_pattern;
745 temp_fake_desc.offset = (vm_offset_t) haddr;
746 fix_desc(&temp_fake_desc, 1);
747 *(struct fake_descriptor *) &master_gdt[sel_idx(KERNEL_TSS)] = temp_fake_desc;
0c530ab8
A
748 kprintf("KTSS: 0x%x, ",haddr);
749#if MACH_KDB
750 /* remap dbtss up high and put new high addr into gdt */
751 haddr = pmap_high_shared_remap(HIGH_FIXED_DBTSS,
752 (vm_offset_t) &master_dbtss, 1);
b0d623f7
A
753 temp_fake_desc = tss_desc_pattern;
754 temp_fake_desc.offset = (vm_offset_t) haddr;
755 fix_desc(&temp_fake_desc, 1);
756 *(struct fake_descriptor *)&master_gdt[sel_idx(DEBUG_TSS)] = temp_fake_desc;
0c530ab8
A
757 ttss = (struct i386_tss *)haddr;
758 kprintf("DBTSS: 0x%x, ",haddr);
759#endif /* MACH_KDB */
760
761 /* remap dftss up high and put new high addr into gdt */
762 haddr = pmap_high_shared_remap(HIGH_FIXED_DFTSS,
763 (vm_offset_t) &master_dftss, 1);
b0d623f7
A
764 temp_fake_desc = tss_desc_pattern;
765 temp_fake_desc.offset = (vm_offset_t) haddr;
766 fix_desc(&temp_fake_desc, 1);
767 *(struct fake_descriptor *) &master_gdt[sel_idx(DF_TSS)] = temp_fake_desc;
0c530ab8
A
768 kprintf("DFTSS: 0x%x\n",haddr);
769
770 /* remap mctss up high and put new high addr into gdt */
771 haddr = pmap_high_shared_remap(HIGH_FIXED_DFTSS,
772 (vm_offset_t) &master_mctss, 1);
b0d623f7
A
773 temp_fake_desc = tss_desc_pattern;
774 temp_fake_desc.offset = (vm_offset_t) haddr;
775 fix_desc(&temp_fake_desc, 1);
776 *(struct fake_descriptor *) &master_gdt[sel_idx(MC_TSS)] = temp_fake_desc;
0c530ab8
A
777 kprintf("MCTSS: 0x%x\n",haddr);
778
b0d623f7 779 cpu_desc_load(&cpu_data_master);
0c530ab8
A
780}
781
782
1c79356b
A
783/*
784 * Bootstrap the system enough to run with virtual memory.
785 * Map the kernel's code and data, and allocate the system page table.
786 * Called with mapping OFF. Page_size must already be set.
1c79356b
A
787 */
788
789void
790pmap_bootstrap(
0c530ab8
A
791 __unused vm_offset_t load_start,
792 boolean_t IA32e)
1c79356b 793{
91447636 794 vm_offset_t va;
91447636 795 int i;
0c530ab8 796 pdpt_entry_t *pdpt;
2d21ac55 797 spl_t s;
1c79356b 798
91447636
A
799 vm_last_addr = VM_MAX_KERNEL_ADDRESS; /* Set the highest address
800 * known to VM */
1c79356b
A
801 /*
802 * The kernel's pmap is statically allocated so we don't
803 * have to use pmap_create, which is unlikely to work
804 * correctly at this part of the boot sequence.
805 */
806
0c530ab8 807
1c79356b 808 kernel_pmap = &kernel_pmap_store;
91447636 809 kernel_pmap->ref_count = 1;
0c530ab8 810 kernel_pmap->nx_enabled = FALSE;
2d21ac55 811 kernel_pmap->pm_task_map = TASK_MAP_32BIT;
91447636
A
812 kernel_pmap->pm_obj = (vm_object_t) NULL;
813 kernel_pmap->dirbase = (pd_entry_t *)((unsigned int)IdlePTD | KERNBASE);
0c530ab8
A
814 kernel_pmap->pdirbase = (pmap_paddr_t)((int)IdlePTD);
815 pdpt = (pd_entry_t *)((unsigned int)IdlePDPT | KERNBASE );
816 kernel_pmap->pm_pdpt = pdpt;
817 kernel_pmap->pm_cr3 = (pmap_paddr_t)((int)IdlePDPT);
1c79356b 818
b0d623f7 819
91447636
A
820 va = (vm_offset_t)kernel_pmap->dirbase;
821 /* setup self referential mapping(s) */
0c530ab8 822 for (i = 0; i< NPGPTD; i++, pdpt++) {
91447636 823 pmap_paddr_t pa;
b0d623f7 824 pa = (pmap_paddr_t) kvtophys((vm_offset_t)(va + i386_ptob(i)));
0c530ab8
A
825 pmap_store_pte(
826 (pd_entry_t *) (kernel_pmap->dirbase + PTDPTDI + i),
91447636 827 (pa & PG_FRAME) | INTEL_PTE_VALID | INTEL_PTE_RW | INTEL_PTE_REF |
0c530ab8
A
828 INTEL_PTE_MOD | INTEL_PTE_WIRED) ;
829 pmap_store_pte(pdpt, pa | INTEL_PTE_VALID);
91447636 830 }
1c79356b 831
6d2010ae
A
832#if CONFIG_YONAH
833 /* 32-bit and legacy support depends on IA32e mode being disabled */
0c530ab8 834 cpu_64bit = IA32e;
6d2010ae 835#endif
0c530ab8
A
836
837 lo_kernel_cr3 = kernel_pmap->pm_cr3;
838 current_cpu_datap()->cpu_kernel_cr3 = (addr64_t) kernel_pmap->pm_cr3;
839
840 /* save the value we stuff into created pmaps to share the gdts etc */
841 high_shared_pde = *pmap_pde(kernel_pmap, HIGH_MEM_BASE);
842 /* make sure G bit is on for high shared pde entry */
843 high_shared_pde |= INTEL_PTE_GLOBAL;
2d21ac55 844 s = splhigh();
0c530ab8 845 pmap_store_pte(pmap_pde(kernel_pmap, HIGH_MEM_BASE), high_shared_pde);
2d21ac55 846 splx(s);
0c530ab8 847
91447636 848 nkpt = NKPT;
b0d623f7 849 OSAddAtomic(NKPT, &inuse_ptepages_count);
6d2010ae
A
850 OSAddAtomic64(NKPT, &alloc_ptepages_count);
851 bootstrap_wired_pages = NKPT;
1c79356b 852
91447636
A
853 virtual_avail = (vm_offset_t)VADDR(KPTDI,0) + (vm_offset_t)first_avail;
854 virtual_end = (vm_offset_t)(VM_MAX_KERNEL_ADDRESS);
1c79356b
A
855
856 /*
91447636
A
857 * Reserve some special page table entries/VA space for temporary
858 * mapping of pages.
1c79356b 859 */
91447636 860 va = virtual_avail;
6d2010ae 861 pt_entry_t *pte;
0c530ab8 862 pte = vtopte(va);
6d2010ae
A
863#define SYSMAP(c, p, v, n) \
864 v = (c)va; va += ((n)*INTEL_PGBYTES); p = pte; pte += (n)
6601e61a 865
0c530ab8
A
866 for (i=0; i<PMAP_NWINDOWS; i++) {
867 SYSMAP(caddr_t,
868 (current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP),
869 (current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CADDR),
870 1);
871 *current_cpu_datap()->cpu_pmap->mapwindow[i].prv_CMAP = 0;
872 }
1c79356b 873
91447636
A
874 /* DMAP user for debugger */
875 SYSMAP(caddr_t, DMAP1, DADDR1, 1);
876 SYSMAP(caddr_t, DMAP2, DADDR2, 1); /* XXX temporary - can remove */
1c79356b 877
91447636 878 virtual_avail = va;
1c79356b 879
593a1d5f 880 if (PE_parse_boot_argn("npvhash", &npvhash, sizeof (npvhash))) {
2d21ac55
A
881 if (0 != ((npvhash+1) & npvhash)) {
882 kprintf("invalid hash %d, must be ((2^N)-1), using default %d\n",npvhash,NPVHASH);
883 npvhash = NPVHASH;
884 }
885 } else {
886 npvhash = NPVHASH;
887 }
888 printf("npvhash=%d\n",npvhash);
889
91447636 890 simple_lock_init(&kernel_pmap->lock, 0);
2d21ac55
A
891 simple_lock_init(&pv_hashed_free_list_lock, 0);
892 simple_lock_init(&pv_hashed_kern_free_list_lock, 0);
893 simple_lock_init(&pv_hash_table_lock,0);
1c79356b 894
2d21ac55 895 pmap_init_high_shared();
0c530ab8
A
896
897 pde_mapped_size = PDE_MAPPED_SIZE;
898
899 if (cpu_64bit) {
b0d623f7 900 pdpt_entry_t *ppdpt = IdlePDPT;
0c530ab8
A
901 pdpt_entry_t *ppdpt64 = (pdpt_entry_t *)IdlePDPT64;
902 pdpt_entry_t *ppml4 = (pdpt_entry_t *)IdlePML4;
903 int istate = ml_set_interrupts_enabled(FALSE);
904
905 /*
906 * Clone a new 64-bit 3rd-level page table directory, IdlePML4,
907 * with page bits set for the correct IA-32e operation and so that
908 * the legacy-mode IdlePDPT is retained for slave processor start-up.
909 * This is necessary due to the incompatible use of page bits between
910 * 64-bit and legacy modes.
911 */
912 kernel_pmap->pm_cr3 = (pmap_paddr_t)((int)IdlePML4); /* setup in start.s for us */
913 kernel_pmap->pm_pml4 = IdlePML4;
914 kernel_pmap->pm_pdpt = (pd_entry_t *)
915 ((unsigned int)IdlePDPT64 | KERNBASE );
916#define PAGE_BITS INTEL_PTE_VALID|INTEL_PTE_RW|INTEL_PTE_USER|INTEL_PTE_REF
917 pmap_store_pte(kernel_pmap->pm_pml4,
918 (uint32_t)IdlePDPT64 | PAGE_BITS);
919 pmap_store_pte((ppdpt64+0), *(ppdpt+0) | PAGE_BITS);
920 pmap_store_pte((ppdpt64+1), *(ppdpt+1) | PAGE_BITS);
921 pmap_store_pte((ppdpt64+2), *(ppdpt+2) | PAGE_BITS);
922 pmap_store_pte((ppdpt64+3), *(ppdpt+3) | PAGE_BITS);
923
924 /*
925 * The kernel is also mapped in the uber-sapce at the 4GB starting
926 * 0xFFFFFF80:00000000. This is the highest entry in the 4th-level.
927 */
928 pmap_store_pte((ppml4+KERNEL_UBER_PML4_INDEX), *(ppml4+0));
929
930 kernel64_cr3 = (addr64_t) kernel_pmap->pm_cr3;
0c530ab8 931
2d21ac55 932 /* Re-initialize descriptors and prepare to switch modes */
b0d623f7 933 cpu_desc_init64(&cpu_data_master);
2d21ac55
A
934 current_cpu_datap()->cpu_is64bit = TRUE;
935 current_cpu_datap()->cpu_active_cr3 = kernel64_cr3;
0c530ab8
A
936
937 pde_mapped_size = 512*4096 ;
938
939 ml_set_interrupts_enabled(istate);
0c530ab8 940 }
2d21ac55 941
b0d623f7 942 /* Sets 64-bit mode if required. */
2d21ac55 943 cpu_mode_init(&cpu_data_master);
b0d623f7
A
944 /* Update in-kernel CPUID information if we're now in 64-bit mode */
945 if (IA32e)
946 cpuid_set_info();
2d21ac55 947
0c530ab8 948 kernel_pmap->pm_hold = (vm_offset_t)kernel_pmap->pm_pml4;
1c79356b 949
91447636
A
950 kprintf("Kernel virtual space from 0x%x to 0x%x.\n",
951 VADDR(KPTDI,0), virtual_end);
6601e61a 952 printf("PAE enabled\n");
0c530ab8
A
953 if (cpu_64bit){
954 printf("64 bit mode enabled\n");kprintf("64 bit mode enabled\n"); }
955
956 kprintf("Available physical space from 0x%llx to 0x%llx\n",
6601e61a 957 avail_start, avail_end);
0c530ab8
A
958
959 /*
960 * By default for 64-bit users loaded at 4GB, share kernel mapping.
961 * But this may be overridden by the -no_shared_cr3 boot-arg.
962 */
593a1d5f 963 if (PE_parse_boot_argn("-no_shared_cr3", &no_shared_cr3, sizeof (no_shared_cr3))) {
0c530ab8 964 kprintf("Shared kernel address space disabled\n");
2d21ac55
A
965 }
966
967#ifdef PMAP_TRACES
593a1d5f 968 if (PE_parse_boot_argn("-pmap_trace", &pmap_trace, sizeof (pmap_trace))) {
2d21ac55
A
969 kprintf("Kernel traces for pmap operations enabled\n");
970 }
971#endif /* PMAP_TRACES */
1c79356b
A
972}
973
974void
975pmap_virtual_space(
976 vm_offset_t *startp,
977 vm_offset_t *endp)
978{
979 *startp = virtual_avail;
980 *endp = virtual_end;
981}
982
983/*
984 * Initialize the pmap module.
985 * Called by vm_init, to initialize any structures that the pmap
986 * system needs to map virtual memory.
987 */
988void
989pmap_init(void)
990{
0b4c1975
A
991 long npages;
992 vm_map_offset_t vaddr;
993 vm_offset_t addr;
994 vm_size_t s, vsize;
995 ppnum_t ppn;
1c79356b
A
996
997 /*
998 * Allocate memory for the pv_head_table and its lock bits,
999 * the modify bit array, and the pte_page table.
1000 */
1001
2d21ac55
A
1002 /*
1003 * zero bias all these arrays now instead of off avail_start
1004 * so we cover all memory
1005 */
1006
b0d623f7 1007 npages = (long)i386_btop(avail_end);
2d21ac55
A
1008 s = (vm_size_t) (sizeof(struct pv_rooted_entry) * npages
1009 + (sizeof (struct pv_hashed_entry_t *) * (npvhash+1))
1010 + pv_lock_table_size(npages)
1011 + pv_hash_lock_table_size((npvhash+1))
1c79356b
A
1012 + npages);
1013
1014 s = round_page(s);
b0d623f7
A
1015 if (kernel_memory_allocate(kernel_map, &addr, s, 0,
1016 KMA_KOBJECT | KMA_PERMANENT)
1017 != KERN_SUCCESS)
1c79356b
A
1018 panic("pmap_init");
1019
1020 memset((char *)addr, 0, s);
1021
0b4c1975
A
1022 vaddr = addr;
1023 vsize = s;
1024
2d21ac55
A
1025#if PV_DEBUG
1026 if (0 == npvhash) panic("npvhash not initialized");
1027#endif
1028
1c79356b
A
1029 /*
1030 * Allocate the structures first to preserve word-alignment.
1031 */
2d21ac55 1032 pv_head_table = (pv_rooted_entry_t) addr;
1c79356b
A
1033 addr = (vm_offset_t) (pv_head_table + npages);
1034
2d21ac55
A
1035 pv_hash_table = (pv_hashed_entry_t *)addr;
1036 addr = (vm_offset_t) (pv_hash_table + (npvhash + 1));
1037
1c79356b
A
1038 pv_lock_table = (char *) addr;
1039 addr = (vm_offset_t) (pv_lock_table + pv_lock_table_size(npages));
1040
2d21ac55
A
1041 pv_hash_lock_table = (char *) addr;
1042 addr = (vm_offset_t) (pv_hash_lock_table + pv_hash_lock_table_size((npvhash+1)));
1043
1c79356b 1044 pmap_phys_attributes = (char *) addr;
2d21ac55
A
1045 {
1046 unsigned int i;
1047 unsigned int pn;
1048 ppnum_t last_pn;
1049 pmap_memory_region_t *pmptr = pmap_memory_regions;
1050
b0d623f7 1051 last_pn = (ppnum_t)i386_btop(avail_end);
2d21ac55
A
1052
1053 for (i = 0; i < pmap_memory_region_count; i++, pmptr++) {
1054 if (pmptr->type == kEfiConventionalMemory) {
b0d623f7 1055
2d21ac55
A
1056 for (pn = pmptr->base; pn <= pmptr->end; pn++) {
1057 if (pn < last_pn) {
1058 pmap_phys_attributes[pn] |= PHYS_MANAGED;
1059
1060 if (pn > last_managed_page)
1061 last_managed_page = pn;
0b4c1975
A
1062
1063 if (pn < lowest_lo)
1064 pmap_phys_attributes[pn] |= PHYS_NOENCRYPT;
1065 else if (pn >= lowest_hi && pn <= highest_hi)
1066 pmap_phys_attributes[pn] |= PHYS_NOENCRYPT;
2d21ac55
A
1067 }
1068 }
1069 }
1070 }
1071 }
0b4c1975
A
1072 while (vsize) {
1073 ppn = pmap_find_phys(kernel_pmap, vaddr);
1074
1075 pmap_phys_attributes[ppn] |= PHYS_NOENCRYPT;
1076
1077 vaddr += PAGE_SIZE;
1078 vsize -= PAGE_SIZE;
1079 }
1c79356b
A
1080 /*
1081 * Create the zone of physical maps,
1082 * and of the physical-to-virtual entries.
1083 */
1084 s = (vm_size_t) sizeof(struct pmap);
1085 pmap_zone = zinit(s, 400*s, 4096, "pmap"); /* XXX */
0b4c1975
A
1086 zone_change(pmap_zone, Z_NOENCRYPT, TRUE);
1087
2d21ac55 1088 s = (vm_size_t) sizeof(struct pv_hashed_entry);
6d2010ae
A
1089 pv_hashed_list_zone = zinit(s, 10000*s /* Expandable zone */,
1090 4096 * 4 /* LCM i386 */, "pv_list");
0b4c1975
A
1091 zone_change(pv_hashed_list_zone, Z_NOENCRYPT, TRUE);
1092
91447636
A
1093 s = 63;
1094 pdpt_zone = zinit(s, 400*s, 4096, "pdpt"); /* XXX */
0b4c1975 1095 zone_change(pdpt_zone, Z_NOENCRYPT, TRUE);
55e303ae 1096
91447636 1097 kptobj = &kptobj_object_store;
2d21ac55 1098 _vm_object_allocate((vm_object_size_t)(NPGPTD*NPTDPG), kptobj);
91447636 1099 kernel_pmap->pm_obj = kptobj;
91447636
A
1100
1101 /* create pv entries for kernel pages mapped by low level
1102 startup code. these have to exist so we can pmap_remove()
1103 e.g. kext pages from the middle of our addr space */
1104
0c530ab8 1105 vaddr = (vm_map_offset_t)0;
91447636 1106 for (ppn = 0; ppn < i386_btop(avail_start) ; ppn++ ) {
2d21ac55 1107 pv_rooted_entry_t pv_e;
91447636
A
1108
1109 pv_e = pai_to_pvh(ppn);
1110 pv_e->va = vaddr;
1111 vaddr += PAGE_SIZE;
1112 pv_e->pmap = kernel_pmap;
2d21ac55 1113 queue_init(&pv_e->qlink);
91447636
A
1114 }
1115
1c79356b
A
1116 pmap_initialized = TRUE;
1117
6d2010ae
A
1118 max_preemption_latency_tsc = tmrCvt((uint64_t)MAX_PREEMPTION_LATENCY_NS, tscFCvtn2t);
1119
1120}
1121
1122#ifdef PMAP_DEBUG
1123#define DBG(x...) kprintf("DBG: " x)
1124#else
1125#define DBG(x...)
1126#endif
1127
1128/*
1129 * Called once VM is fully initialized so that we can release unused
1130 * sections of low memory to the general pool.
1131 * Also complete the set-up of identity-mapped sections of the kernel:
1132 * 1) write-protect kernel text
1133 * 2) map kernel text using large pages if possible
1134 * 3) read and write-protect page zero (for K32)
1135 * 4) map the global page at the appropriate virtual address.
1136 *
1137 * Use of large pages
1138 * ------------------
1139 * To effectively map and write-protect all kernel text pages, the text
1140 * must be 2M-aligned at the base, and the data section above must also be
1141 * 2M-aligned. That is, there's padding below and above. This is achieved
1142 * through linker directives. Large pages are used only if this alignment
1143 * exists (and not overriden by the -kernel_text_page_4K boot-arg). The
1144 * memory layout is:
1145 *
1146 * : :
1147 * | __DATA |
1148 * sdata: ================== 2Meg
1149 * | |
1150 * | zero-padding |
1151 * | |
1152 * etext: ------------------
1153 * | |
1154 * : :
1155 * | |
1156 * | __TEXT |
1157 * | |
1158 * : :
1159 * | |
1160 * stext: ================== 2Meg
1161 * | |
1162 * | zero-padding |
1163 * | |
1164 * eHIB: ------------------
1165 * | __HIB |
1166 * : :
1167 *
1168 * Prior to changing the mapping from 4K to 2M, the zero-padding pages
1169 * [eHIB,stext] and [etext,sdata] are ml_static_mfree()'d. Then all the
1170 * 4K pages covering [stext,etext] are coalesced as 2M large pages.
1171 * The now unused level-1 PTE pages are also freed.
1172 */
1173extern uint32_t pmap_reserved_ranges;
1174void
1175pmap_lowmem_finalize(void)
1176{
1177 spl_t spl;
1178 int i;
1179
1180 /* Check the kernel is linked at the expected base address */
1181 if (i386_btop(kvtophys((vm_offset_t) &IdlePML4)) !=
1182 I386_KERNEL_IMAGE_BASE_PAGE)
1183 panic("pmap_lowmem_finalize() unexpected kernel base address");
1184
1c79356b 1185 /*
6d2010ae 1186 * Update wired memory statistics for early boot pages
1c79356b 1187 */
6d2010ae 1188 PMAP_ZINFO_PALLOC(bootstrap_wired_pages * PAGE_SIZE);
2d21ac55 1189
6d2010ae
A
1190 /*
1191 * Free all pages in pmap regions below the base:
1192 * rdar://6332712
1193 * We can't free all the pages to VM that EFI reports available.
1194 * Pages in the range 0xc0000-0xff000 aren't safe over sleep/wake.
1195 * There's also a size miscalculation here: pend is one page less
1196 * than it should be but this is not fixed to be backwards
1197 * compatible.
1198 * Due to this current EFI limitation, we take only the first
1199 * entry in the memory region table. However, the loop is retained
1200 * (with the intended termination criteria commented out) in the
1201 * hope that some day we can free all low-memory ranges.
1202 */
1203 for (i = 0;
1204// pmap_memory_regions[i].end <= I386_KERNEL_IMAGE_BASE_PAGE;
1205 i < 1 && (pmap_reserved_ranges == 0);
1206 i++) {
1207 vm_offset_t pbase = (vm_offset_t)i386_ptob(pmap_memory_regions[i].base);
1208 vm_offset_t pend = (vm_offset_t)i386_ptob(pmap_memory_regions[i].end);
1209// vm_offset_t pend = i386_ptob(pmap_memory_regions[i].end+1);
1210
1211 DBG("ml_static_mfree(%p,%p) for pmap region %d\n",
1212 (void *) ml_static_ptovirt(pbase),
1213 (void *) (pend - pbase), i);
1214 ml_static_mfree(ml_static_ptovirt(pbase), pend - pbase);
1215 }
2d21ac55 1216
6d2010ae
A
1217 /*
1218 * If text and data are both 2MB-aligned,
1219 * we can map text with large-pages,
1220 * unless the -kernel_text_ps_4K boot-arg overrides.
1221 */
1222 if ((stext & I386_LPGMASK) == 0 && (sdata & I386_LPGMASK) == 0) {
1223 kprintf("Kernel text is 2MB aligned");
1224 kernel_text_ps_4K = FALSE;
1225 if (PE_parse_boot_argn("-kernel_text_ps_4K",
1226 &kernel_text_ps_4K,
1227 sizeof (kernel_text_ps_4K)))
1228 kprintf(" but will be mapped with 4K pages\n");
1229 else
1230 kprintf(" and will be mapped with 2M pages\n");
1231 }
1232
1233 (void) PE_parse_boot_argn("wpkernel", &wpkernel, sizeof (wpkernel));
1234 if (wpkernel)
1235 kprintf("Kernel text %p-%p to be write-protected\n",
1236 (void *) stext, (void *) etext);
1237
1238 spl = splhigh();
1239
1240 /*
1241 * Scan over text if mappings are to be changed:
1242 * - Remap kernel text readonly unless the "wpkernel" boot-arg is 0
1243 * - Change to large-pages if possible and not overriden.
1244 */
1245 if (kernel_text_ps_4K && wpkernel) {
1246 vm_offset_t myva;
1247 for (myva = stext; myva < etext; myva += PAGE_SIZE) {
1248 pt_entry_t *ptep;
1249
1250 ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)myva);
1251 if (ptep)
1252 pmap_store_pte(ptep, *ptep & ~INTEL_PTE_RW);
1253 }
1254 }
1255
1256 if (!kernel_text_ps_4K) {
1257 vm_offset_t myva;
1258
1259 /*
1260 * Release zero-filled page padding used for 2M-alignment.
1261 */
1262 DBG("ml_static_mfree(%p,%p) for padding below text\n",
1263 (void *) eHIB, (void *) (stext - eHIB));
1264 ml_static_mfree(eHIB, stext - eHIB);
1265 DBG("ml_static_mfree(%p,%p) for padding above text\n",
1266 (void *) etext, (void *) (sdata - etext));
1267 ml_static_mfree(etext, sdata - etext);
1268
1269 /*
1270 * Coalesce text pages into large pages.
1271 */
1272 for (myva = stext; myva < sdata; myva += I386_LPGBYTES) {
1273 pt_entry_t *ptep;
1274 vm_offset_t pte_phys;
1275 pt_entry_t *pdep;
1276 pt_entry_t pde;
1277
1278 pdep = pmap_pde(kernel_pmap, (vm_map_offset_t)myva);
1279 ptep = pmap_pte(kernel_pmap, (vm_map_offset_t)myva);
1280 DBG("myva: %p pdep: %p ptep: %p\n",
1281 (void *) myva, (void *) pdep, (void *) ptep);
1282 if ((*ptep & INTEL_PTE_VALID) == 0)
1283 continue;
1284 pte_phys = (vm_offset_t)(*ptep & PG_FRAME);
1285 pde = *pdep & PTMASK; /* page attributes from pde */
1286 pde |= INTEL_PTE_PS; /* make it a 2M entry */
1287 pde |= pte_phys; /* take page frame from pte */
1288
1289 if (wpkernel)
1290 pde &= ~INTEL_PTE_RW;
1291 DBG("pmap_store_pte(%p,0x%llx)\n",
1292 (void *)pdep, pde);
1293 pmap_store_pte(pdep, pde);
1294
1295 /*
1296 * Free the now-unused level-1 pte.
1297 * Note: ptep is a virtual address to the pte in the
1298 * recursive map. We can't use this address to free
1299 * the page. Instead we need to compute its address
1300 * in the Idle PTEs in "low memory".
1301 */
1302 vm_offset_t vm_ptep = (vm_offset_t) KPTphys
1303 + (pte_phys >> PTPGSHIFT);
1304 DBG("ml_static_mfree(%p,0x%x) for pte\n",
1305 (void *) vm_ptep, PAGE_SIZE);
1306 ml_static_mfree(vm_ptep, PAGE_SIZE);
1307 }
1308
1309 /* Change variable read by sysctl machdep.pmap */
1310 pmap_kernel_text_ps = I386_LPGBYTES;
1311 }
1c79356b 1312
6d2010ae
A
1313 /* no matter what, kernel page zero is not accessible */
1314 pmap_store_pte(pmap_pte(kernel_pmap, 0), INTEL_PTE_INVALID);
1315
1316 /* map lowmem global page into fixed addr */
1317 pt_entry_t *pte = NULL;
1318 if (0 == (pte = pmap_pte(kernel_pmap,
1319 VM_MIN_KERNEL_LOADED_ADDRESS + 0x2000)))
1320 panic("lowmem pte");
1321 /* make sure it is defined on page boundary */
1322 assert(0 == ((vm_offset_t) &lowGlo & PAGE_MASK));
1323 pmap_store_pte(pte, kvtophys((vm_offset_t)&lowGlo)
1324 | INTEL_PTE_REF
1325 | INTEL_PTE_MOD
1326 | INTEL_PTE_WIRED
1327 | INTEL_PTE_VALID
1328 | INTEL_PTE_RW);
1329 splx(spl);
1330 flush_tlb();
1331}
1c79356b 1332
2d21ac55 1333#define managed_page(x) ( (unsigned int)x <= last_managed_page && (pmap_phys_attributes[x] & PHYS_MANAGED) )
1c79356b 1334
2d21ac55
A
1335/*
1336 * this function is only used for debugging fron the vm layer
1337 */
1c79356b
A
1338boolean_t
1339pmap_verify_free(
55e303ae 1340 ppnum_t pn)
1c79356b 1341{
2d21ac55 1342 pv_rooted_entry_t pv_h;
1c79356b 1343 int pai;
1c79356b
A
1344 boolean_t result;
1345
55e303ae 1346 assert(pn != vm_page_fictitious_addr);
2d21ac55 1347
1c79356b
A
1348 if (!pmap_initialized)
1349 return(TRUE);
1350
2d21ac55
A
1351 if (pn == vm_page_guard_addr)
1352 return TRUE;
1c79356b 1353
2d21ac55
A
1354 pai = ppn_to_pai(pn);
1355 if (!managed_page(pai))
1356 return(FALSE);
1357 pv_h = pai_to_pvh(pn);
1358 result = (pv_h->pmap == PMAP_NULL);
1359 return(result);
1360}
1c79356b 1361
2d21ac55
A
1362boolean_t
1363pmap_is_empty(
1364 pmap_t pmap,
b0d623f7
A
1365 vm_map_offset_t va_start,
1366 vm_map_offset_t va_end)
2d21ac55
A
1367{
1368 vm_map_offset_t offset;
1369 ppnum_t phys_page;
1c79356b 1370
2d21ac55
A
1371 if (pmap == PMAP_NULL) {
1372 return TRUE;
1373 }
b0d623f7
A
1374
1375 /*
1376 * Check the resident page count
1377 * - if it's zero, the pmap is completely empty.
1378 * This short-circuit test prevents a virtual address scan which is
1379 * painfully slow for 64-bit spaces.
1380 * This assumes the count is correct
1381 * .. the debug kernel ought to be checking perhaps by page table walk.
1382 */
1383 if (pmap->stats.resident_count == 0)
1384 return TRUE;
1385
1386 for (offset = va_start;
1387 offset < va_end;
2d21ac55
A
1388 offset += PAGE_SIZE_64) {
1389 phys_page = pmap_find_phys(pmap, offset);
1390 if (phys_page) {
1391 if (pmap != kernel_pmap &&
1392 pmap->pm_task_map == TASK_MAP_32BIT &&
1393 offset >= HIGH_MEM_BASE) {
1394 /*
1395 * The "high_shared_pde" is used to share
1396 * the entire top-most 2MB of address space
1397 * between the kernel and all 32-bit tasks.
1398 * So none of this can be removed from 32-bit
1399 * tasks.
1400 * Let's pretend there's nothing up
1401 * there...
1402 */
1403 return TRUE;
1404 }
1405 kprintf("pmap_is_empty(%p,0x%llx,0x%llx): "
1406 "page %d at 0x%llx\n",
b0d623f7 1407 pmap, va_start, va_end, phys_page, offset);
2d21ac55
A
1408 return FALSE;
1409 }
1410 }
1c79356b 1411
2d21ac55 1412 return TRUE;
1c79356b
A
1413}
1414
2d21ac55 1415
1c79356b
A
1416/*
1417 * Create and return a physical map.
1418 *
1419 * If the size specified for the map
1420 * is zero, the map is an actual physical
1421 * map, and may be referenced by the
1422 * hardware.
1423 *
1424 * If the size specified is non-zero,
1425 * the map will be used in software only, and
1426 * is bounded by that size.
1427 */
1428pmap_t
1429pmap_create(
0c530ab8 1430 vm_map_size_t sz,
2d21ac55 1431 boolean_t is_64bit)
1c79356b 1432{
2d21ac55 1433 pmap_t p;
0c530ab8
A
1434 int i;
1435 vm_offset_t va;
1436 vm_size_t size;
1437 pdpt_entry_t *pdpt;
1438 pml4_entry_t *pml4p;
0c530ab8 1439 pd_entry_t *pdp;
2d21ac55 1440 int template;
0c530ab8
A
1441 spl_t s;
1442
2d21ac55
A
1443 PMAP_TRACE(PMAP_CODE(PMAP__CREATE) | DBG_FUNC_START,
1444 (int) (sz>>32), (int) sz, (int) is_64bit, 0, 0);
1445
0c530ab8 1446 size = (vm_size_t) sz;
1c79356b
A
1447
1448 /*
1449 * A software use-only map doesn't even need a map.
1450 */
1451
1452 if (size != 0) {
1453 return(PMAP_NULL);
1454 }
1455
91447636
A
1456 p = (pmap_t) zalloc(pmap_zone);
1457 if (PMAP_NULL == p)
2d21ac55 1458 panic("pmap_create zalloc");
6601e61a 1459
0c530ab8
A
1460 /* init counts now since we'll be bumping some */
1461 simple_lock_init(&p->lock, 0);
1c79356b 1462 p->stats.resident_count = 0;
2d21ac55 1463 p->stats.resident_max = 0;
1c79356b 1464 p->stats.wired_count = 0;
1c79356b 1465 p->ref_count = 1;
0c530ab8 1466 p->nx_enabled = 1;
0c530ab8
A
1467 p->pm_shared = FALSE;
1468
2d21ac55
A
1469 assert(!is_64bit || cpu_64bit);
1470 p->pm_task_map = is_64bit ? TASK_MAP_64BIT : TASK_MAP_32BIT;;
1471
0c530ab8 1472 if (!cpu_64bit) {
2d21ac55
A
1473 /* legacy 32 bit setup */
1474 /* in the legacy case the pdpt layer is hardwired to 4 entries and each
1475 * entry covers 1GB of addr space */
b0d623f7
A
1476 if (KERN_SUCCESS != kmem_alloc_kobject(kernel_map, (vm_offset_t *)(&p->dirbase), NBPTD))
1477 panic("pmap_create kmem_alloc_kobject");
2d21ac55
A
1478 p->pm_hold = (vm_offset_t)zalloc(pdpt_zone);
1479 if ((vm_offset_t)NULL == p->pm_hold) {
1480 panic("pdpt zalloc");
1481 }
1482 pdpt = (pdpt_entry_t *) (( p->pm_hold + 31) & ~31);
1483 p->pm_cr3 = (pmap_paddr_t)kvtophys((vm_offset_t)pdpt);
1484 if (NULL == (p->pm_obj = vm_object_allocate((vm_object_size_t)(NPGPTD*NPTDPG))))
1485 panic("pmap_create vm_object_allocate");
0c530ab8 1486
2d21ac55 1487 memset((char *)p->dirbase, 0, NBPTD);
0c530ab8 1488
2d21ac55
A
1489 va = (vm_offset_t)p->dirbase;
1490 p->pdirbase = kvtophys(va);
0c530ab8 1491
6d2010ae
A
1492 PMAP_ZINFO_SALLOC(NBPTD);
1493
b7266188 1494 template = INTEL_PTE_VALID;
2d21ac55
A
1495 for (i = 0; i< NPGPTD; i++, pdpt++ ) {
1496 pmap_paddr_t pa;
b0d623f7 1497 pa = (pmap_paddr_t) kvtophys((vm_offset_t)(va + i386_ptob(i)));
2d21ac55
A
1498 pmap_store_pte(pdpt, pa | template);
1499 }
0c530ab8 1500
2d21ac55
A
1501 /* map the high shared pde */
1502 s = splhigh();
1503 pmap_store_pte(pmap_pde(p, HIGH_MEM_BASE), high_shared_pde);
1504 splx(s);
4452a7af 1505
0c530ab8 1506 } else {
2d21ac55 1507 /* 64 bit setup */
4452a7af 1508
2d21ac55 1509 /* alloc the pml4 page in kernel vm */
b0d623f7
A
1510 if (KERN_SUCCESS != kmem_alloc_kobject(kernel_map, (vm_offset_t *)(&p->pm_hold), PAGE_SIZE))
1511 panic("pmap_create kmem_alloc_kobject pml4");
4452a7af 1512
2d21ac55
A
1513 memset((char *)p->pm_hold, 0, PAGE_SIZE);
1514 p->pm_cr3 = (pmap_paddr_t)kvtophys((vm_offset_t)p->pm_hold);
0c530ab8 1515
b0d623f7 1516 OSAddAtomic(1, &inuse_ptepages_count);
6d2010ae
A
1517 OSAddAtomic64(1, &alloc_ptepages_count);
1518 PMAP_ZINFO_SALLOC(PAGE_SIZE);
0c530ab8 1519
2d21ac55 1520 /* allocate the vm_objs to hold the pdpt, pde and pte pages */
0c530ab8 1521
2d21ac55
A
1522 if (NULL == (p->pm_obj_pml4 = vm_object_allocate((vm_object_size_t)(NPML4PGS))))
1523 panic("pmap_create pdpt obj");
0c530ab8 1524
2d21ac55
A
1525 if (NULL == (p->pm_obj_pdpt = vm_object_allocate((vm_object_size_t)(NPDPTPGS))))
1526 panic("pmap_create pdpt obj");
0c530ab8 1527
2d21ac55
A
1528 if (NULL == (p->pm_obj = vm_object_allocate((vm_object_size_t)(NPDEPGS))))
1529 panic("pmap_create pte obj");
0c530ab8 1530
2d21ac55
A
1531 /* uber space points to uber mapped kernel */
1532 s = splhigh();
1533 pml4p = pmap64_pml4(p, 0ULL);
6d2010ae 1534 pmap_store_pte((pml4p+KERNEL_UBER_PML4_INDEX),*kernel_pmap->pm_pml4);
0c530ab8 1535
0c530ab8 1536
2d21ac55
A
1537 if (!is_64bit) {
1538 while ((pdp = pmap64_pde(p, (uint64_t)HIGH_MEM_BASE)) == PD_ENTRY_NULL) {
1539 splx(s);
1540 pmap_expand_pdpt(p, (uint64_t)HIGH_MEM_BASE); /* need room for another pde entry */
1541 s = splhigh();
1542 }
1543 pmap_store_pte(pdp, high_shared_pde);
1544 }
1545 splx(s);
0c530ab8 1546 }
1c79356b 1547
2d21ac55
A
1548 PMAP_TRACE(PMAP_CODE(PMAP__CREATE) | DBG_FUNC_START,
1549 (int) p, is_64bit, 0, 0, 0);
1550
1c79356b
A
1551 return(p);
1552}
1553
2d21ac55
A
1554/*
1555 * The following routines implement the shared address optmization for 64-bit
1556 * users with a 4GB page zero.
1557 *
1558 * pmap_set_4GB_pagezero()
1559 * is called in the exec and fork paths to mirror the kernel's
1560 * mapping in the bottom 4G of the user's pmap. The task mapping changes
1561 * from TASK_MAP_64BIT to TASK_MAP_64BIT_SHARED. This routine returns
1562 * without doing anything if the -no_shared_cr3 boot-arg is set.
1563 *
1564 * pmap_clear_4GB_pagezero()
1565 * is called in the exec/exit paths to undo this mirror. The task mapping
1566 * reverts to TASK_MAP_64BIT. In addition, we switch to the kernel's
1567 * CR3 by calling pmap_load_kernel_cr3().
1568 *
1569 * pmap_load_kernel_cr3()
1570 * loads cr3 with the kernel's page table. In addition to being called
1571 * by pmap_clear_4GB_pagezero(), it is used both prior to teardown and
1572 * when we go idle in the context of a shared map.
1573 *
1574 * Further notes on per-cpu data used:
1575 *
1576 * cpu_kernel_cr3 is the cr3 for the kernel's pmap.
1577 * This is loaded in a trampoline on entering the kernel
1578 * from a 32-bit user (or non-shared-cr3 64-bit user).
1579 * cpu_task_cr3 is the cr3 for the current thread.
1580 * This is loaded in a trampoline as we exit the kernel.
1581 * cpu_active_cr3 reflects the cr3 currently loaded.
1582 * However, the low order bit is set when the
1583 * processor is idle or interrupts are disabled
1584 * while the system pmap lock is held. It is used by
1585 * tlb shoot-down.
1586 * cpu_task_map indicates whether the task cr3 belongs to
1587 * a 32-bit, a 64-bit or a 64-bit shared map.
1588 * The latter allows the avoidance of the cr3 load
1589 * on kernel entry and exit.
1590 * cpu_tlb_invalid set TRUE when a tlb flush is requested.
1591 * If the cr3 is "inactive" (the cpu is idle or the
1592 * system-wide pmap lock is held) this not serviced by
1593 * an IPI but at time when the cr3 becomes "active".
1594 */
1595
0c530ab8
A
1596void
1597pmap_set_4GB_pagezero(pmap_t p)
1598{
0c530ab8
A
1599 pdpt_entry_t *user_pdptp;
1600 pdpt_entry_t *kern_pdptp;
1601
2d21ac55 1602 assert(p->pm_task_map != TASK_MAP_32BIT);
0c530ab8
A
1603
1604 /* Kernel-shared cr3 may be disabled by boot arg. */
1605 if (no_shared_cr3)
1606 return;
1607
1608 /*
1609 * Set the bottom 4 3rd-level pte's to be the kernel's.
1610 */
2d21ac55 1611 PMAP_LOCK(p);
0c530ab8 1612 while ((user_pdptp = pmap64_pdpt(p, 0x0)) == PDPT_ENTRY_NULL) {
2d21ac55 1613 PMAP_UNLOCK(p);
0c530ab8 1614 pmap_expand_pml4(p, 0x0);
2d21ac55 1615 PMAP_LOCK(p);
0c530ab8
A
1616 }
1617 kern_pdptp = kernel_pmap->pm_pdpt;
1618 pmap_store_pte(user_pdptp+0, *(kern_pdptp+0));
1619 pmap_store_pte(user_pdptp+1, *(kern_pdptp+1));
1620 pmap_store_pte(user_pdptp+2, *(kern_pdptp+2));
1621 pmap_store_pte(user_pdptp+3, *(kern_pdptp+3));
2d21ac55
A
1622 p->pm_task_map = TASK_MAP_64BIT_SHARED;
1623 PMAP_UNLOCK(p);
0c530ab8
A
1624}
1625
1626void
1627pmap_clear_4GB_pagezero(pmap_t p)
1628{
0c530ab8 1629 pdpt_entry_t *user_pdptp;
6d2010ae 1630 boolean_t istate;
0c530ab8 1631
2d21ac55 1632 if (p->pm_task_map != TASK_MAP_64BIT_SHARED)
0c530ab8
A
1633 return;
1634
2d21ac55
A
1635 PMAP_LOCK(p);
1636
1637 p->pm_task_map = TASK_MAP_64BIT;
1638
6d2010ae
A
1639 istate = ml_set_interrupts_enabled(FALSE);
1640 if (current_cpu_datap()->cpu_task_map == TASK_MAP_64BIT_SHARED)
1641 current_cpu_datap()->cpu_task_map = TASK_MAP_64BIT;
2d21ac55
A
1642 pmap_load_kernel_cr3();
1643
0c530ab8
A
1644 user_pdptp = pmap64_pdpt(p, 0x0);
1645 pmap_store_pte(user_pdptp+0, 0);
1646 pmap_store_pte(user_pdptp+1, 0);
1647 pmap_store_pte(user_pdptp+2, 0);
1648 pmap_store_pte(user_pdptp+3, 0);
1649
6d2010ae
A
1650 ml_set_interrupts_enabled(istate);
1651
2d21ac55
A
1652 PMAP_UNLOCK(p);
1653}
0c530ab8 1654
2d21ac55
A
1655void
1656pmap_load_kernel_cr3(void)
1657{
1658 uint64_t kernel_cr3;
0c530ab8 1659
2d21ac55
A
1660 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
1661
1662 /*
1663 * Reload cr3 with the true kernel cr3.
1664 */
1665 kernel_cr3 = current_cpu_datap()->cpu_kernel_cr3;
1666 set64_cr3(kernel_cr3);
1667 current_cpu_datap()->cpu_active_cr3 = kernel_cr3;
1668 current_cpu_datap()->cpu_tlb_invalid = FALSE;
1669 __asm__ volatile("mfence");
0c530ab8
A
1670}
1671
1c79356b
A
1672/*
1673 * Retire the given physical map from service.
1674 * Should only be called if the map contains
1675 * no valid mappings.
1676 */
1677
1678void
1679pmap_destroy(
1680 register pmap_t p)
1681{
1c79356b 1682 register int c;
1c79356b
A
1683
1684 if (p == PMAP_NULL)
1685 return;
2d21ac55
A
1686
1687 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_START,
1688 (int) p, 0, 0, 0, 0);
1689
1690 PMAP_LOCK(p);
1691
1c79356b 1692 c = --p->ref_count;
2d21ac55 1693
1c79356b 1694 if (c == 0) {
1c79356b
A
1695 /*
1696 * If some cpu is not using the physical pmap pointer that it
1697 * is supposed to be (see set_dirbase), we might be using the
1698 * pmap that is being destroyed! Make sure we are
1699 * physically on the right pmap:
1700 */
55e303ae 1701 PMAP_UPDATE_TLBS(p,
2d21ac55
A
1702 0x0ULL,
1703 0xFFFFFFFFFFFFF000ULL);
1c79356b 1704 }
2d21ac55
A
1705
1706 PMAP_UNLOCK(p);
1c79356b
A
1707
1708 if (c != 0) {
2d21ac55
A
1709 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_END,
1710 (int) p, 1, 0, 0, 0);
1711 return; /* still in use */
1c79356b
A
1712 }
1713
1714 /*
1715 * Free the memory maps, then the
1716 * pmap structure.
1717 */
0c530ab8 1718 if (!cpu_64bit) {
b0d623f7 1719 OSAddAtomic(-p->pm_obj->resident_page_count, &inuse_ptepages_count);
6d2010ae 1720 PMAP_ZINFO_PFREE(p->pm_obj->resident_page_count * PAGE_SIZE);
91447636 1721
2d21ac55 1722 kmem_free(kernel_map, (vm_offset_t)p->dirbase, NBPTD);
6d2010ae
A
1723 PMAP_ZINFO_SFREE(NBPTD);
1724
2d21ac55 1725 zfree(pdpt_zone, (void *)p->pm_hold);
0c530ab8 1726
2d21ac55
A
1727 vm_object_deallocate(p->pm_obj);
1728 } else {
1729 /* 64 bit */
1730 int inuse_ptepages = 0;
0c530ab8 1731
2d21ac55 1732 /* free 64 bit mode structs */
2d21ac55 1733 kmem_free(kernel_map, (vm_offset_t)p->pm_hold, PAGE_SIZE);
6d2010ae 1734 PMAP_ZINFO_SFREE(PAGE_SIZE);
2d21ac55
A
1735
1736 inuse_ptepages += p->pm_obj_pml4->resident_page_count;
1737 vm_object_deallocate(p->pm_obj_pml4);
1738
1739 inuse_ptepages += p->pm_obj_pdpt->resident_page_count;
1740 vm_object_deallocate(p->pm_obj_pdpt);
0c530ab8 1741
2d21ac55
A
1742 inuse_ptepages += p->pm_obj->resident_page_count;
1743 vm_object_deallocate(p->pm_obj);
1744
6d2010ae
A
1745 OSAddAtomic(-(inuse_ptepages+1), &inuse_ptepages_count);
1746 PMAP_ZINFO_PFREE(inuse_ptepages * PAGE_SIZE);
2d21ac55 1747 }
6d2010ae 1748
2d21ac55 1749 zfree(pmap_zone, p);
1c79356b 1750
2d21ac55
A
1751 PMAP_TRACE(PMAP_CODE(PMAP__DESTROY) | DBG_FUNC_END,
1752 0, 0, 0, 0, 0);
0c530ab8 1753
1c79356b
A
1754}
1755
1756/*
1757 * Add a reference to the specified pmap.
1758 */
1759
1760void
1761pmap_reference(
1762 register pmap_t p)
1763{
1c79356b
A
1764
1765 if (p != PMAP_NULL) {
2d21ac55 1766 PMAP_LOCK(p);
1c79356b 1767 p->ref_count++;
2d21ac55 1768 PMAP_UNLOCK(p);;
1c79356b
A
1769 }
1770}
1771
0b4e3aa0
A
1772/*
1773 * Remove phys addr if mapped in specified map
1774 *
1775 */
1776void
1777pmap_remove_some_phys(
91447636
A
1778 __unused pmap_t map,
1779 __unused ppnum_t pn)
0b4e3aa0
A
1780{
1781
1782/* Implement to support working set code */
1783
1784}
1785
1c79356b
A
1786/*
1787 * Set the physical protection on the
1788 * specified range of this map as requested.
1789 * Will not increase permissions.
1790 */
1791void
1792pmap_protect(
1793 pmap_t map,
0c530ab8
A
1794 vm_map_offset_t sva,
1795 vm_map_offset_t eva,
1c79356b
A
1796 vm_prot_t prot)
1797{
1798 register pt_entry_t *pde;
1799 register pt_entry_t *spte, *epte;
0c530ab8
A
1800 vm_map_offset_t lva;
1801 vm_map_offset_t orig_sva;
0c530ab8 1802 boolean_t set_NX;
2d21ac55
A
1803 int num_found = 0;
1804
1805 pmap_intr_assert();
1c79356b
A
1806
1807 if (map == PMAP_NULL)
1808 return;
1809
0c530ab8
A
1810 if (prot == VM_PROT_NONE) {
1811 pmap_remove(map, sva, eva);
1c79356b
A
1812 return;
1813 }
1814
2d21ac55
A
1815 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT) | DBG_FUNC_START,
1816 (int) map,
1817 (int) (sva>>32), (int) sva,
1818 (int) (eva>>32), (int) eva);
1819
0c530ab8
A
1820 if ( (prot & VM_PROT_EXECUTE) || !nx_enabled || !map->nx_enabled )
1821 set_NX = FALSE;
1822 else
1823 set_NX = TRUE;
1824
2d21ac55 1825 PMAP_LOCK(map);
1c79356b 1826
0c530ab8
A
1827 orig_sva = sva;
1828 while (sva < eva) {
1829 lva = (sva + pde_mapped_size) & ~(pde_mapped_size-1);
1830 if (lva > eva)
1831 lva = eva;
1832 pde = pmap_pde(map, sva);
1833 if (pde && (*pde & INTEL_PTE_VALID)) {
1834 spte = (pt_entry_t *)pmap_pte(map, (sva & ~(pde_mapped_size-1)));
1835 spte = &spte[ptenum(sva)];
1836 epte = &spte[intel_btop(lva-sva)];
1c79356b
A
1837
1838 while (spte < epte) {
2d21ac55 1839
0c530ab8
A
1840 if (*spte & INTEL_PTE_VALID) {
1841
1842 if (prot & VM_PROT_WRITE)
2d21ac55 1843 pmap_update_pte(spte, *spte, (*spte | INTEL_PTE_WRITE));
0c530ab8 1844 else
2d21ac55 1845 pmap_update_pte(spte, *spte, (*spte & ~INTEL_PTE_WRITE));
0c530ab8
A
1846
1847 if (set_NX == TRUE)
2d21ac55 1848 pmap_update_pte(spte, *spte, (*spte | INTEL_PTE_NX));
0c530ab8 1849 else
2d21ac55 1850 pmap_update_pte(spte, *spte, (*spte & ~INTEL_PTE_NX));
0c530ab8
A
1851
1852 num_found++;
0c530ab8 1853 }
1c79356b
A
1854 spte++;
1855 }
1856 }
0c530ab8 1857 sva = lva;
1c79356b 1858 }
0c530ab8 1859 if (num_found)
6d2010ae 1860 {
2d21ac55 1861 PMAP_UPDATE_TLBS(map, orig_sva, eva);
6d2010ae 1862 }
2d21ac55
A
1863
1864 PMAP_UNLOCK(map);
1865
1866 PMAP_TRACE(PMAP_CODE(PMAP__PROTECT) | DBG_FUNC_END,
1867 0, 0, 0, 0, 0);
91447636 1868
1c79356b
A
1869}
1870
0c530ab8
A
1871/* Map a (possibly) autogenned block */
1872void
1873pmap_map_block(
1874 pmap_t pmap,
1875 addr64_t va,
1876 ppnum_t pa,
1877 uint32_t size,
1878 vm_prot_t prot,
1879 int attr,
1880 __unused unsigned int flags)
1881{
2d21ac55 1882 uint32_t page;
0c530ab8 1883
2d21ac55
A
1884 for (page = 0; page < size; page++) {
1885 pmap_enter(pmap, va, pa, prot, attr, TRUE);
1886 va += PAGE_SIZE;
1887 pa++;
1888 }
0c530ab8 1889}
1c79356b 1890
1c79356b
A
1891/*
1892 * Routine: pmap_extract
1893 * Function:
1894 * Extract the physical page address associated
1895 * with the given map/virtual_address pair.
91447636
A
1896 * Change to shim for backwards compatibility but will not
1897 * work for 64 bit systems. Some old drivers that we cannot
1898 * change need this.
1c79356b
A
1899 */
1900
1901vm_offset_t
1902pmap_extract(
1903 register pmap_t pmap,
0c530ab8 1904 vm_map_offset_t vaddr)
1c79356b 1905{
0c530ab8
A
1906 ppnum_t ppn;
1907 vm_offset_t paddr;
91447636 1908
0c530ab8
A
1909 paddr = (vm_offset_t)0;
1910 ppn = pmap_find_phys(pmap, vaddr);
2d21ac55 1911
0c530ab8 1912 if (ppn) {
b0d623f7 1913 paddr = ((vm_offset_t)i386_ptob(ppn)) | ((vm_offset_t)vaddr & INTEL_OFFMASK);
0c530ab8
A
1914 }
1915 return (paddr);
1c79356b
A
1916}
1917
1c79356b 1918void
0c530ab8
A
1919pmap_expand_pml4(
1920 pmap_t map,
1921 vm_map_offset_t vaddr)
1c79356b 1922{
1c79356b 1923 register vm_page_t m;
91447636 1924 register pmap_paddr_t pa;
0c530ab8 1925 uint64_t i;
1c79356b 1926 spl_t spl;
55e303ae 1927 ppnum_t pn;
0c530ab8 1928 pml4_entry_t *pml4p;
89b3af67 1929
0c530ab8
A
1930 if (kernel_pmap == map) panic("expand kernel pml4");
1931
1932 spl = splhigh();
2d21ac55
A
1933 pml4p = pmap64_pml4(map, vaddr);
1934 splx(spl);
1935 if (PML4_ENTRY_NULL == pml4p) panic("pmap_expand_pml4 no pml4p");
1c79356b
A
1936
1937 /*
0c530ab8 1938 * Allocate a VM page for the pml4 page
1c79356b
A
1939 */
1940 while ((m = vm_page_grab()) == VM_PAGE_NULL)
1941 VM_PAGE_WAIT();
1942
1943 /*
91447636 1944 * put the page into the pmap's obj list so it
1c79356b
A
1945 * can be found later.
1946 */
55e303ae
A
1947 pn = m->phys_page;
1948 pa = i386_ptob(pn);
0c530ab8
A
1949 i = pml4idx(map, vaddr);
1950
2d21ac55
A
1951 /*
1952 * Zero the page.
1953 */
1954 pmap_zero_page(pn);
0c530ab8 1955
b0d623f7 1956 vm_page_lockspin_queues();
1c79356b 1957 vm_page_wire(m);
2d21ac55 1958 vm_page_unlock_queues();
1c79356b 1959
b0d623f7 1960 OSAddAtomic(1, &inuse_ptepages_count);
6d2010ae
A
1961 OSAddAtomic64(1, &alloc_ptepages_count);
1962 PMAP_ZINFO_PALLOC(PAGE_SIZE);
b0d623f7 1963
2d21ac55
A
1964 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
1965 vm_object_lock(map->pm_obj_pml4);
1c79356b 1966
2d21ac55 1967 PMAP_LOCK(map);
1c79356b
A
1968 /*
1969 * See if someone else expanded us first
1970 */
0c530ab8 1971 if (pmap64_pdpt(map, vaddr) != PDPT_ENTRY_NULL) {
2d21ac55
A
1972 PMAP_UNLOCK(map);
1973 vm_object_unlock(map->pm_obj_pml4);
1974
b0d623f7 1975 VM_PAGE_FREE(m);
2d21ac55 1976
b0d623f7 1977 OSAddAtomic(-1, &inuse_ptepages_count);
6d2010ae 1978 PMAP_ZINFO_PFREE(PAGE_SIZE);
1c79356b
A
1979 return;
1980 }
0b4c1975 1981 pmap_set_noencrypt(pn);
1c79356b 1982
2d21ac55
A
1983#if 0 /* DEBUG */
1984 if (0 != vm_page_lookup(map->pm_obj_pml4, (vm_object_offset_t)i)) {
1985 panic("pmap_expand_pml4: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
1986 map, map->pm_obj_pml4, vaddr, i);
1987 }
1988#endif
1989 vm_page_insert(m, map->pm_obj_pml4, (vm_object_offset_t)i);
1990 vm_object_unlock(map->pm_obj_pml4);
1991
1c79356b
A
1992 /*
1993 * Set the page directory entry for this page table.
1c79356b 1994 */
0c530ab8 1995 pml4p = pmap64_pml4(map, vaddr); /* refetch under lock */
c0fea474 1996
0c530ab8
A
1997 pmap_store_pte(pml4p, pa_to_pte(pa)
1998 | INTEL_PTE_VALID
1999 | INTEL_PTE_USER
2000 | INTEL_PTE_WRITE);
5d5c5d0d 2001
2d21ac55 2002 PMAP_UNLOCK(map);
89b3af67 2003
6601e61a 2004 return;
0c530ab8 2005
6601e61a 2006}
89b3af67 2007
6601e61a 2008void
0c530ab8
A
2009pmap_expand_pdpt(
2010 pmap_t map,
2011 vm_map_offset_t vaddr)
6601e61a 2012{
0c530ab8
A
2013 register vm_page_t m;
2014 register pmap_paddr_t pa;
2015 uint64_t i;
2016 spl_t spl;
2017 ppnum_t pn;
2018 pdpt_entry_t *pdptp;
89b3af67 2019
0c530ab8 2020 if (kernel_pmap == map) panic("expand kernel pdpt");
89b3af67 2021
0c530ab8 2022 spl = splhigh();
2d21ac55
A
2023 while ((pdptp = pmap64_pdpt(map, vaddr)) == PDPT_ENTRY_NULL) {
2024 splx(spl);
2025 pmap_expand_pml4(map, vaddr); /* need room for another pdpt entry */
2026 spl = splhigh();
2027 }
2028 splx(spl);
4452a7af 2029
0c530ab8
A
2030 /*
2031 * Allocate a VM page for the pdpt page
2032 */
2033 while ((m = vm_page_grab()) == VM_PAGE_NULL)
2034 VM_PAGE_WAIT();
4452a7af 2035
4452a7af 2036 /*
0c530ab8
A
2037 * put the page into the pmap's obj list so it
2038 * can be found later.
4452a7af 2039 */
0c530ab8
A
2040 pn = m->phys_page;
2041 pa = i386_ptob(pn);
2042 i = pdptidx(map, vaddr);
4452a7af 2043
2d21ac55
A
2044 /*
2045 * Zero the page.
2046 */
2047 pmap_zero_page(pn);
0c530ab8 2048
b0d623f7 2049 vm_page_lockspin_queues();
0c530ab8 2050 vm_page_wire(m);
2d21ac55 2051 vm_page_unlock_queues();
0c530ab8 2052
b0d623f7 2053 OSAddAtomic(1, &inuse_ptepages_count);
6d2010ae
A
2054 OSAddAtomic64(1, &alloc_ptepages_count);
2055 PMAP_ZINFO_PALLOC(PAGE_SIZE);
b0d623f7 2056
2d21ac55
A
2057 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
2058 vm_object_lock(map->pm_obj_pdpt);
0c530ab8 2059
2d21ac55 2060 PMAP_LOCK(map);
0c530ab8
A
2061 /*
2062 * See if someone else expanded us first
2063 */
2064 if (pmap64_pde(map, vaddr) != PD_ENTRY_NULL) {
2d21ac55
A
2065 PMAP_UNLOCK(map);
2066 vm_object_unlock(map->pm_obj_pdpt);
2067
b0d623f7 2068 VM_PAGE_FREE(m);
2d21ac55 2069
b0d623f7 2070 OSAddAtomic(-1, &inuse_ptepages_count);
6d2010ae 2071 PMAP_ZINFO_PFREE(PAGE_SIZE);
0c530ab8
A
2072 return;
2073 }
0b4c1975 2074 pmap_set_noencrypt(pn);
0c530ab8 2075
2d21ac55
A
2076#if 0 /* DEBUG */
2077 if (0 != vm_page_lookup(map->pm_obj_pdpt, (vm_object_offset_t)i)) {
2078 panic("pmap_expand_pdpt: obj not empty, pmap %p pm_obj %p vaddr 0x%llx i 0x%llx\n",
2079 map, map->pm_obj_pdpt, vaddr, i);
2080 }
2081#endif
2082 vm_page_insert(m, map->pm_obj_pdpt, (vm_object_offset_t)i);
2083 vm_object_unlock(map->pm_obj_pdpt);
2084
0c530ab8
A
2085 /*
2086 * Set the page directory entry for this page table.
0c530ab8 2087 */
0c530ab8
A
2088 pdptp = pmap64_pdpt(map, vaddr); /* refetch under lock */
2089
2090 pmap_store_pte(pdptp, pa_to_pte(pa)
2091 | INTEL_PTE_VALID
2092 | INTEL_PTE_USER
2093 | INTEL_PTE_WRITE);
2094
2d21ac55 2095 PMAP_UNLOCK(map);
0c530ab8
A
2096
2097 return;
2098
2099}
2100
2101
2102
2103/*
2104 * Routine: pmap_expand
2105 *
2106 * Expands a pmap to be able to map the specified virtual address.
2107 *
2108 * Allocates new virtual memory for the P0 or P1 portion of the
2109 * pmap, then re-maps the physical pages that were in the old
2110 * pmap to be in the new pmap.
2111 *
2112 * Must be called with the pmap system and the pmap unlocked,
2113 * since these must be unlocked to use vm_allocate or vm_deallocate.
2114 * Thus it must be called in a loop that checks whether the map
2115 * has been expanded enough.
2116 * (We won't loop forever, since page tables aren't shrunk.)
2117 */
2118void
2119pmap_expand(
2120 pmap_t map,
2121 vm_map_offset_t vaddr)
2122{
2123 pt_entry_t *pdp;
2124 register vm_page_t m;
2125 register pmap_paddr_t pa;
2126 uint64_t i;
2127 spl_t spl;
2128 ppnum_t pn;
2129
2130 /*
2131 * if not the kernel map (while we are still compat kernel mode)
2132 * and we are 64 bit, propagate expand upwards
2133 */
2134
2135 if (cpu_64bit && (map != kernel_pmap)) {
2d21ac55
A
2136 spl = splhigh();
2137 while ((pdp = pmap64_pde(map, vaddr)) == PD_ENTRY_NULL) {
2138 splx(spl);
2139 pmap_expand_pdpt(map, vaddr); /* need room for another pde entry */
2140 spl = splhigh();
2141 }
2142 splx(spl);
0c530ab8
A
2143 }
2144
0c530ab8
A
2145 /*
2146 * Allocate a VM page for the pde entries.
2147 */
2148 while ((m = vm_page_grab()) == VM_PAGE_NULL)
2149 VM_PAGE_WAIT();
2150
2151 /*
2152 * put the page into the pmap's obj list so it
2153 * can be found later.
2154 */
2155 pn = m->phys_page;
2156 pa = i386_ptob(pn);
2157 i = pdeidx(map, vaddr);
2158
2d21ac55
A
2159 /*
2160 * Zero the page.
2161 */
2162 pmap_zero_page(pn);
0c530ab8 2163
b0d623f7 2164 vm_page_lockspin_queues();
0c530ab8 2165 vm_page_wire(m);
0c530ab8 2166 vm_page_unlock_queues();
0c530ab8 2167
b0d623f7 2168 OSAddAtomic(1, &inuse_ptepages_count);
6d2010ae
A
2169 OSAddAtomic64(1, &alloc_ptepages_count);
2170 PMAP_ZINFO_PALLOC(PAGE_SIZE);
b0d623f7 2171
2d21ac55
A
2172 /* Take the oject lock (mutex) before the PMAP_LOCK (spinlock) */
2173 vm_object_lock(map->pm_obj);
0c530ab8 2174
2d21ac55 2175 PMAP_LOCK(map);
0c530ab8
A
2176 /*
2177 * See if someone else expanded us first
2178 */
2d21ac55 2179
0c530ab8 2180 if (pmap_pte(map, vaddr) != PT_ENTRY_NULL) {
2d21ac55
A
2181 PMAP_UNLOCK(map);
2182 vm_object_unlock(map->pm_obj);
0c530ab8 2183
b0d623f7 2184 VM_PAGE_FREE(m);
2d21ac55 2185
b0d623f7 2186 OSAddAtomic(-1, &inuse_ptepages_count);
6d2010ae 2187 PMAP_ZINFO_PFREE(PAGE_SIZE);
0c530ab8
A
2188 return;
2189 }
0b4c1975 2190 pmap_set_noencrypt(pn);
0c530ab8 2191
2d21ac55
A
2192#if 0 /* DEBUG */
2193 if (0 != vm_page_lookup(map->pm_obj, (vm_object_offset_t)i)) {
2194 panic("pmap_expand: obj not empty, pmap 0x%x pm_obj 0x%x vaddr 0x%llx i 0x%llx\n",
2195 map, map->pm_obj, vaddr, i);
2196 }
2197#endif
2198 vm_page_insert(m, map->pm_obj, (vm_object_offset_t)i);
2199 vm_object_unlock(map->pm_obj);
0c530ab8
A
2200
2201 /*
2d21ac55 2202 * refetch while locked
0c530ab8
A
2203 */
2204
2d21ac55
A
2205 pdp = pmap_pde(map, vaddr);
2206
2207 /*
2208 * Set the page directory entry for this page table.
2209 */
0c530ab8
A
2210 pmap_store_pte(pdp, pa_to_pte(pa)
2211 | INTEL_PTE_VALID
2212 | INTEL_PTE_USER
2213 | INTEL_PTE_WRITE);
0c530ab8 2214
2d21ac55 2215 PMAP_UNLOCK(map);
0c530ab8
A
2216
2217 return;
2218}
2219
2220
2221/*
2222 * pmap_sync_page_data_phys(ppnum_t pa)
2223 *
2224 * Invalidates all of the instruction cache on a physical page and
2225 * pushes any dirty data from the data cache for the same physical page
2226 * Not required in i386.
2227 */
2228void
2229pmap_sync_page_data_phys(__unused ppnum_t pa)
2230{
2231 return;
2232}
2233
2234/*
2235 * pmap_sync_page_attributes_phys(ppnum_t pa)
2236 *
2237 * Write back and invalidate all cachelines on a physical page.
2238 */
2239void
2240pmap_sync_page_attributes_phys(ppnum_t pa)
2241{
2242 cache_flush_page_phys(pa);
2243}
2244
2d21ac55
A
2245
2246
2247#ifdef CURRENTLY_UNUSED_AND_UNTESTED
2248
0c530ab8
A
2249int collect_ref;
2250int collect_unref;
2251
2252/*
2253 * Routine: pmap_collect
2254 * Function:
2255 * Garbage collects the physical map system for
2256 * pages which are no longer used.
2257 * Success need not be guaranteed -- that is, there
2258 * may well be pages which are not referenced, but
2259 * others may be collected.
2260 * Usage:
2261 * Called by the pageout daemon when pages are scarce.
2262 */
2263void
2264pmap_collect(
2265 pmap_t p)
2266{
2267 register pt_entry_t *pdp, *ptp;
2268 pt_entry_t *eptp;
2269 int wired;
0c530ab8
A
2270
2271 if (p == PMAP_NULL)
2272 return;
2273
2274 if (p == kernel_pmap)
2275 return;
2276
2277 /*
2278 * Garbage collect map.
2279 */
2d21ac55 2280 PMAP_LOCK(p);
0c530ab8
A
2281
2282 for (pdp = (pt_entry_t *)p->dirbase;
4452a7af
A
2283 pdp < (pt_entry_t *)&p->dirbase[(UMAXPTDI+1)];
2284 pdp++)
2285 {
2286 if (*pdp & INTEL_PTE_VALID) {
2287 if(*pdp & INTEL_PTE_REF) {
0c530ab8 2288 pmap_store_pte(pdp, *pdp & ~INTEL_PTE_REF);
4452a7af
A
2289 collect_ref++;
2290 } else {
2291 collect_unref++;
2292 ptp = pmap_pte(p, pdetova(pdp - (pt_entry_t *)p->dirbase));
2293 eptp = ptp + NPTEPG;
2294
2295 /*
2296 * If the pte page has any wired mappings, we cannot
2297 * free it.
2298 */
2299 wired = 0;
2300 {
2301 register pt_entry_t *ptep;
2302 for (ptep = ptp; ptep < eptp; ptep++) {
2303 if (iswired(*ptep)) {
2304 wired = 1;
5d5c5d0d 2305 break;
1c79356b
A
2306 }
2307 }
2308 }
2309 if (!wired) {
2310 /*
2311 * Remove the virtual addresses mapped by this pte page.
2312 */
2313 pmap_remove_range(p,
91447636 2314 pdetova(pdp - (pt_entry_t *)p->dirbase),
1c79356b
A
2315 ptp,
2316 eptp);
2317
2318 /*
2319 * Invalidate the page directory pointer.
2320 */
0c530ab8 2321 pmap_store_pte(pdp, 0x0);
91447636 2322
2d21ac55 2323 PMAP_UNLOCK(p);
1c79356b
A
2324
2325 /*
2326 * And free the pte page itself.
2327 */
2328 {
2329 register vm_page_t m;
2330
91447636 2331 vm_object_lock(p->pm_obj);
2d21ac55 2332
91447636 2333 m = vm_page_lookup(p->pm_obj,(vm_object_offset_t)(pdp - (pt_entry_t *)&p->dirbase[0]));
1c79356b
A
2334 if (m == VM_PAGE_NULL)
2335 panic("pmap_collect: pte page not in object");
2d21ac55 2336
6d2010ae
A
2337 vm_object_unlock(p->pm_obj);
2338
b0d623f7
A
2339 VM_PAGE_FREE(m);
2340
2341 OSAddAtomic(-1, &inuse_ptepages_count);
6d2010ae 2342 PMAP_ZINFO_PFREE(PAGE_SIZE);
1c79356b
A
2343 }
2344
2d21ac55 2345 PMAP_LOCK(p);
1c79356b 2346 }
91447636
A
2347 }
2348 }
1c79356b 2349 }
0c530ab8 2350
2d21ac55
A
2351 PMAP_UPDATE_TLBS(p, 0x0, 0xFFFFFFFFFFFFF000ULL);
2352 PMAP_UNLOCK(p);
1c79356b
A
2353 return;
2354
2355}
2d21ac55 2356#endif
1c79356b 2357
1c79356b 2358
1c79356b 2359void
2d21ac55 2360pmap_copy_page(ppnum_t src, ppnum_t dst)
1c79356b 2361{
2d21ac55
A
2362 bcopy_phys((addr64_t)i386_ptob(src),
2363 (addr64_t)i386_ptob(dst),
2364 PAGE_SIZE);
1c79356b 2365}
1c79356b 2366
1c79356b
A
2367
2368/*
2369 * Routine: pmap_pageable
2370 * Function:
2371 * Make the specified pages (by pmap, offset)
2372 * pageable (or not) as requested.
2373 *
2374 * A page which is not pageable may not take
2375 * a fault; therefore, its page table entry
2376 * must remain valid for the duration.
2377 *
2378 * This routine is merely advisory; pmap_enter
2379 * will specify that these pages are to be wired
2380 * down (or not) as appropriate.
2381 */
2382void
2383pmap_pageable(
91447636 2384 __unused pmap_t pmap,
0c530ab8
A
2385 __unused vm_map_offset_t start_addr,
2386 __unused vm_map_offset_t end_addr,
91447636 2387 __unused boolean_t pageable)
1c79356b
A
2388{
2389#ifdef lint
91447636 2390 pmap++; start_addr++; end_addr++; pageable++;
1c79356b
A
2391#endif /* lint */
2392}
2393
1c79356b 2394void
91447636
A
2395invalidate_icache(__unused vm_offset_t addr,
2396 __unused unsigned cnt,
2397 __unused int phys)
1c79356b
A
2398{
2399 return;
2400}
2401void
91447636
A
2402flush_dcache(__unused vm_offset_t addr,
2403 __unused unsigned count,
2404 __unused int phys)
1c79356b
A
2405{
2406 return;
2407}
2408
2d21ac55
A
2409#if CONFIG_DTRACE
2410/*
2411 * Constrain DTrace copyin/copyout actions
2412 */
2413extern kern_return_t dtrace_copyio_preflight(addr64_t);
2414extern kern_return_t dtrace_copyio_postflight(addr64_t);
2415
2416kern_return_t dtrace_copyio_preflight(__unused addr64_t va)
2417{
2418 thread_t thread = current_thread();
2419
2420 if (current_map() == kernel_map)
2421 return KERN_FAILURE;
2422 else if (thread->machine.specFlags & CopyIOActive)
2423 return KERN_FAILURE;
2424 else
2425 return KERN_SUCCESS;
2426}
2427
2428kern_return_t dtrace_copyio_postflight(__unused addr64_t va)
2429{
2430 return KERN_SUCCESS;
2431}
2432#endif /* CONFIG_DTRACE */
2433
0c530ab8 2434#if MACH_KDB
6601e61a 2435
0c530ab8 2436/* show phys page mappings and attributes */
6601e61a 2437
0c530ab8 2438extern void db_show_page(pmap_paddr_t pa);
6601e61a 2439
2d21ac55 2440#if 0
6601e61a 2441void
0c530ab8 2442db_show_page(pmap_paddr_t pa)
6601e61a 2443{
0c530ab8
A
2444 pv_entry_t pv_h;
2445 int pai;
2446 char attr;
2447
2448 pai = pa_index(pa);
2449 pv_h = pai_to_pvh(pai);
1c79356b
A
2450
2451 attr = pmap_phys_attributes[pai];
2d21ac55 2452 printf("phys page %llx ", pa);
1c79356b
A
2453 if (attr & PHYS_MODIFIED)
2454 printf("modified, ");
2455 if (attr & PHYS_REFERENCED)
2456 printf("referenced, ");
2457 if (pv_h->pmap || pv_h->next)
2458 printf(" mapped at\n");
2459 else
2460 printf(" not mapped\n");
2461 for (; pv_h; pv_h = pv_h->next)
2462 if (pv_h->pmap)
2d21ac55 2463 printf("%llx in pmap %p\n", pv_h->va, pv_h->pmap);
1c79356b 2464}
2d21ac55 2465#endif
1c79356b
A
2466
2467#endif /* MACH_KDB */
2468
2469#if MACH_KDB
2d21ac55 2470#if 0
1c79356b
A
2471void db_kvtophys(vm_offset_t);
2472void db_show_vaddrs(pt_entry_t *);
2473
2474/*
2475 * print out the results of kvtophys(arg)
2476 */
2477void
2478db_kvtophys(
2479 vm_offset_t vaddr)
2480{
0c530ab8 2481 db_printf("0x%qx", kvtophys(vaddr));
1c79356b
A
2482}
2483
2484/*
2485 * Walk the pages tables.
2486 */
2487void
2488db_show_vaddrs(
2489 pt_entry_t *dirbase)
2490{
2491 pt_entry_t *ptep, *pdep, tmp;
0c530ab8 2492 unsigned int x, y, pdecnt, ptecnt;
1c79356b
A
2493
2494 if (dirbase == 0) {
2495 dirbase = kernel_pmap->dirbase;
2496 }
2497 if (dirbase == 0) {
2498 db_printf("need a dirbase...\n");
2499 return;
2500 }
0c530ab8 2501 dirbase = (pt_entry_t *) (int) ((unsigned long) dirbase & ~INTEL_OFFMASK);
1c79356b
A
2502
2503 db_printf("dirbase: 0x%x\n", dirbase);
2504
2505 pdecnt = ptecnt = 0;
2506 pdep = &dirbase[0];
91447636 2507 for (y = 0; y < NPDEPG; y++, pdep++) {
1c79356b
A
2508 if (((tmp = *pdep) & INTEL_PTE_VALID) == 0) {
2509 continue;
2510 }
2511 pdecnt++;
2d21ac55 2512 ptep = (pt_entry_t *) ((unsigned long)(*pdep) & ~INTEL_OFFMASK);
1c79356b 2513 db_printf("dir[%4d]: 0x%x\n", y, *pdep);
91447636 2514 for (x = 0; x < NPTEPG; x++, ptep++) {
1c79356b
A
2515 if (((tmp = *ptep) & INTEL_PTE_VALID) == 0) {
2516 continue;
2517 }
2518 ptecnt++;
2519 db_printf(" tab[%4d]: 0x%x, va=0x%x, pa=0x%x\n",
2520 x,
2521 *ptep,
2522 (y << 22) | (x << 12),
2523 *ptep & ~INTEL_OFFMASK);
2524 }
2525 }
2526
2527 db_printf("total: %d tables, %d page table entries.\n", pdecnt, ptecnt);
2528
2529}
2d21ac55 2530#endif
1c79356b
A
2531#endif /* MACH_KDB */
2532
2533#include <mach_vm_debug.h>
2534#if MACH_VM_DEBUG
2535#include <vm/vm_debug.h>
2536
2537int
2538pmap_list_resident_pages(
91447636
A
2539 __unused pmap_t pmap,
2540 __unused vm_offset_t *listp,
2541 __unused int space)
1c79356b
A
2542{
2543 return 0;
2544}
2545#endif /* MACH_VM_DEBUG */
2546
6601e61a 2547
1c79356b 2548
91447636
A
2549/* temporary workaround */
2550boolean_t
0c530ab8 2551coredumpok(__unused vm_map_t map, __unused vm_offset_t va)
91447636 2552{
0c530ab8 2553#if 0
91447636 2554 pt_entry_t *ptep;
1c79356b 2555
91447636
A
2556 ptep = pmap_pte(map->pmap, va);
2557 if (0 == ptep)
2558 return FALSE;
2559 return ((*ptep & (INTEL_PTE_NCACHE | INTEL_PTE_WIRED)) != (INTEL_PTE_NCACHE | INTEL_PTE_WIRED));
0c530ab8
A
2560#else
2561 return TRUE;
1c79356b 2562#endif
1c79356b
A
2563}
2564
1c79356b 2565
9bccf70c 2566boolean_t
91447636
A
2567phys_page_exists(
2568 ppnum_t pn)
9bccf70c 2569{
91447636
A
2570 assert(pn != vm_page_fictitious_addr);
2571
2572 if (!pmap_initialized)
2573 return (TRUE);
2d21ac55
A
2574
2575 if (pn == vm_page_guard_addr)
2576 return FALSE;
2577
2578 if (!managed_page(ppn_to_pai(pn)))
91447636
A
2579 return (FALSE);
2580
2581 return TRUE;
2582}
2583
91447636 2584void
0c530ab8 2585pmap_commpage32_init(vm_offset_t kernel_commpage, vm_offset_t user_commpage, int cnt)
91447636 2586{
2d21ac55
A
2587 int i;
2588 pt_entry_t *opte, *npte;
2589 pt_entry_t pte;
2590 spl_t s;
2591
2592 for (i = 0; i < cnt; i++) {
2593 s = splhigh();
2594 opte = pmap_pte(kernel_pmap, (vm_map_offset_t)kernel_commpage);
2595 if (0 == opte)
2596 panic("kernel_commpage");
2597 pte = *opte | INTEL_PTE_USER|INTEL_PTE_GLOBAL;
2598 pte &= ~INTEL_PTE_WRITE; // ensure read only
2599 npte = pmap_pte(kernel_pmap, (vm_map_offset_t)user_commpage);
2600 if (0 == npte)
2601 panic("user_commpage");
2602 pmap_store_pte(npte, pte);
2603 splx(s);
2604 kernel_commpage += INTEL_PGBYTES;
2605 user_commpage += INTEL_PGBYTES;
2606 }
91447636
A
2607}
2608
2d21ac55 2609
0c530ab8
A
2610#define PMAP_COMMPAGE64_CNT (_COMM_PAGE64_AREA_USED/PAGE_SIZE)
2611pt_entry_t pmap_commpage64_ptes[PMAP_COMMPAGE64_CNT];
2612
2613void
2614pmap_commpage64_init(vm_offset_t kernel_commpage, __unused vm_map_offset_t user_commpage, int cnt)
2615{
2d21ac55
A
2616 int i;
2617 pt_entry_t *kptep;
0c530ab8 2618
2d21ac55 2619 PMAP_LOCK(kernel_pmap);
0c530ab8 2620
2d21ac55
A
2621 for (i = 0; i < cnt; i++) {
2622 kptep = pmap_pte(kernel_pmap, (uint64_t)kernel_commpage + (i*PAGE_SIZE));
2623 if ((0 == kptep) || (0 == (*kptep & INTEL_PTE_VALID)))
2624 panic("pmap_commpage64_init pte");
2625 pmap_commpage64_ptes[i] = ((*kptep & ~INTEL_PTE_WRITE) | INTEL_PTE_USER);
2626 }
2627 PMAP_UNLOCK(kernel_pmap);
0c530ab8
A
2628}
2629
0c530ab8 2630
91447636 2631static cpu_pmap_t cpu_pmap_master;
91447636
A
2632
2633struct cpu_pmap *
2634pmap_cpu_alloc(boolean_t is_boot_cpu)
2635{
2636 int ret;
2637 int i;
2638 cpu_pmap_t *cp;
91447636 2639 vm_offset_t address;
0c530ab8 2640 vm_map_address_t mapaddr;
91447636 2641 vm_map_entry_t entry;
0c530ab8 2642 pt_entry_t *pte;
91447636
A
2643
2644 if (is_boot_cpu) {
2645 cp = &cpu_pmap_master;
91447636
A
2646 } else {
2647 /*
2648 * The per-cpu pmap data structure itself.
2649 */
2650 ret = kmem_alloc(kernel_map,
2651 (vm_offset_t *) &cp, sizeof(cpu_pmap_t));
2652 if (ret != KERN_SUCCESS) {
2653 printf("pmap_cpu_alloc() failed ret=%d\n", ret);
2654 return NULL;
2655 }
2656 bzero((void *)cp, sizeof(cpu_pmap_t));
2657
2658 /*
0c530ab8 2659 * The temporary windows used for copy/zero - see loose_ends.c
91447636 2660 */
0c530ab8
A
2661 ret = vm_map_find_space(kernel_map,
2662 &mapaddr, PMAP_NWINDOWS*PAGE_SIZE, (vm_map_offset_t)0, 0, &entry);
91447636 2663 if (ret != KERN_SUCCESS) {
0c530ab8
A
2664 printf("pmap_cpu_alloc() "
2665 "vm_map_find_space ret=%d\n", ret);
91447636
A
2666 pmap_cpu_free(cp);
2667 return NULL;
2668 }
0c530ab8 2669 address = (vm_offset_t)mapaddr;
4452a7af 2670
0c530ab8 2671 for (i = 0; i < PMAP_NWINDOWS; i++, address += PAGE_SIZE) {
2d21ac55
A
2672 spl_t s;
2673 s = splhigh();
0c530ab8
A
2674 while ((pte = pmap_pte(kernel_pmap, (vm_map_offset_t)address)) == 0)
2675 pmap_expand(kernel_pmap, (vm_map_offset_t)address);
2676 * (int *) pte = 0;
6601e61a 2677 cp->mapwindow[i].prv_CADDR = (caddr_t) address;
0c530ab8 2678 cp->mapwindow[i].prv_CMAP = pte;
2d21ac55 2679 splx(s);
4452a7af 2680 }
0c530ab8 2681 vm_map_unlock(kernel_map);
4452a7af
A
2682 }
2683
0c530ab8
A
2684 cp->pdpt_window_index = PMAP_PDPT_FIRST_WINDOW;
2685 cp->pde_window_index = PMAP_PDE_FIRST_WINDOW;
2686 cp->pte_window_index = PMAP_PTE_FIRST_WINDOW;
4452a7af 2687
6601e61a 2688 return cp;
4452a7af
A
2689}
2690
2691void
6601e61a 2692pmap_cpu_free(struct cpu_pmap *cp)
4452a7af 2693{
6601e61a 2694 if (cp != NULL && cp != &cpu_pmap_master) {
6601e61a 2695 kfree((void *) cp, sizeof(cpu_pmap_t));
4452a7af 2696 }
4452a7af 2697}
0c530ab8 2698
0c530ab8
A
2699mapwindow_t *
2700pmap_get_mapwindow(pt_entry_t pentry)
2701{
2702 mapwindow_t *mp;
2703 int i;
0c530ab8 2704
2d21ac55 2705 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
6d2010ae
A
2706 /* fold in cache attributes for this physical page */
2707 pentry |= pmap_get_cache_attributes(i386_btop(pte_to_pa(pentry)));
0c530ab8
A
2708 /*
2709 * Note: 0th map reserved for pmap_pte()
2710 */
2711 for (i = PMAP_NWINDOWS_FIRSTFREE; i < PMAP_NWINDOWS; i++) {
2712 mp = &current_cpu_datap()->cpu_pmap->mapwindow[i];
2713
2714 if (*mp->prv_CMAP == 0) {
6d2010ae 2715 pmap_store_pte(mp->prv_CMAP, pentry);
2d21ac55 2716
6d2010ae 2717 invlpg((uintptr_t)mp->prv_CADDR);
2d21ac55 2718
6d2010ae 2719 return (mp);
0c530ab8
A
2720 }
2721 }
2d21ac55
A
2722 panic("pmap_get_mapwindow: no windows available");
2723
2724 return NULL;
2725}
2726
2727
2728void
2729pmap_put_mapwindow(mapwindow_t *mp)
2730{
2731 pmap_store_pte(mp->prv_CMAP, 0);
0c530ab8
A
2732}
2733
0c530ab8
A
2734void
2735pmap_switch(pmap_t tpmap)
2736{
2737 spl_t s;
0c530ab8
A
2738
2739 s = splhigh(); /* Make sure interruptions are disabled */
0c530ab8 2740
b0d623f7 2741 set_dirbase(tpmap, current_thread());
0c530ab8
A
2742
2743 splx(s);
2744}
2745
2746
2747/*
2748 * disable no-execute capability on
2749 * the specified pmap
2750 */
2751void pmap_disable_NX(pmap_t pmap) {
2752
2753 pmap->nx_enabled = 0;
2754}
2755
2756void
6d2010ae
A
2757pt_fake_zone_init(int zone_index)
2758{
2759 pt_fake_zone_index = zone_index;
2760}
2761
2762void
2763pt_fake_zone_info(int *count,
2764 vm_size_t *cur_size, vm_size_t *max_size, vm_size_t *elem_size, vm_size_t *alloc_size,
2765 uint64_t *sum_size, int *collectable, int *exhaustable, int *caller_acct)
0c530ab8
A
2766{
2767 *count = inuse_ptepages_count;
2768 *cur_size = PAGE_SIZE * inuse_ptepages_count;
2769 *max_size = PAGE_SIZE * (inuse_ptepages_count + vm_page_inactive_count + vm_page_active_count + vm_page_free_count);
2770 *elem_size = PAGE_SIZE;
2771 *alloc_size = PAGE_SIZE;
6d2010ae 2772 *sum_size = alloc_ptepages_count * PAGE_SIZE;
0c530ab8
A
2773
2774 *collectable = 1;
2775 *exhaustable = 0;
6d2010ae 2776 *caller_acct = 1;
0c530ab8
A
2777}
2778
2779vm_offset_t pmap_cpu_high_map_vaddr(int cpu, enum high_cpu_types e)
2780{
2781 enum high_fixed_addresses a;
2782 a = e + HIGH_CPU_END * cpu;
2783 return pmap_index_to_virt(HIGH_FIXED_CPUS_BEGIN + a);
2784}
2785
2786vm_offset_t pmap_high_map_vaddr(enum high_cpu_types e)
2787{
2788 return pmap_cpu_high_map_vaddr(cpu_number(), e);
2789}
2790
2791vm_offset_t pmap_high_map(pt_entry_t pte, enum high_cpu_types e)
2792{
2793 enum high_fixed_addresses a;
2794 vm_offset_t vaddr;
2795
2796 a = e + HIGH_CPU_END * cpu_number();
2797 vaddr = (vm_offset_t)pmap_index_to_virt(HIGH_FIXED_CPUS_BEGIN + a);
2d21ac55 2798 pmap_store_pte(pte_unique_base + a, pte);
0c530ab8
A
2799
2800 /* TLB flush for this page for this cpu */
2801 invlpg((uintptr_t)vaddr);
2802
2803 return vaddr;
2804}
2805
935ed37a
A
2806static inline void
2807pmap_cpuset_NMIPI(cpu_set cpu_mask) {
2808 unsigned int cpu, cpu_bit;
2809 uint64_t deadline;
2810
2811 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
2812 if (cpu_mask & cpu_bit)
2813 cpu_NMI_interrupt(cpu);
2814 }
6d2010ae 2815 deadline = mach_absolute_time() + (((uint64_t)LockTimeOut) * 3);
935ed37a
A
2816 while (mach_absolute_time() < deadline)
2817 cpu_pause();
2818}
2819
0c530ab8
A
2820/*
2821 * Called with pmap locked, we:
2822 * - scan through per-cpu data to see which other cpus need to flush
2823 * - send an IPI to each non-idle cpu to be flushed
2824 * - wait for all to signal back that they are inactive or we see that
2825 * they are in an interrupt handler or at a safe point
2826 * - flush the local tlb is active for this pmap
2827 * - return ... the caller will unlock the pmap
2828 */
2829void
6d2010ae 2830pmap_flush_tlbs(pmap_t pmap, vm_map_offset_t startv, vm_map_offset_t endv)
0c530ab8
A
2831{
2832 unsigned int cpu;
2833 unsigned int cpu_bit;
2834 cpu_set cpus_to_signal;
2835 unsigned int my_cpu = cpu_number();
2836 pmap_paddr_t pmap_cr3 = pmap->pm_cr3;
2837 boolean_t flush_self = FALSE;
2838 uint64_t deadline;
2839
2d21ac55
A
2840 assert((processor_avail_count < 2) ||
2841 (ml_get_interrupts_enabled() && get_preemption_level() != 0));
0c530ab8
A
2842
2843 /*
2844 * Scan other cpus for matching active or task CR3.
2845 * For idle cpus (with no active map) we mark them invalid but
2846 * don't signal -- they'll check as they go busy.
2847 * Note: for the kernel pmap we look for 64-bit shared address maps.
2848 */
2849 cpus_to_signal = 0;
2850 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
2851 if (!cpu_datap(cpu)->cpu_running)
2852 continue;
2d21ac55
A
2853 if ((cpu_datap(cpu)->cpu_task_cr3 == pmap_cr3) ||
2854 (CPU_GET_ACTIVE_CR3(cpu) == pmap_cr3) ||
0c530ab8
A
2855 (pmap->pm_shared) ||
2856 ((pmap == kernel_pmap) &&
2857 (!CPU_CR3_IS_ACTIVE(cpu) ||
2858 cpu_datap(cpu)->cpu_task_map == TASK_MAP_64BIT_SHARED))) {
2859 if (cpu == my_cpu) {
2860 flush_self = TRUE;
2861 continue;
2862 }
2863 cpu_datap(cpu)->cpu_tlb_invalid = TRUE;
2864 __asm__ volatile("mfence");
2865
2866 if (CPU_CR3_IS_ACTIVE(cpu)) {
2867 cpus_to_signal |= cpu_bit;
2868 i386_signal_cpu(cpu, MP_TLB_FLUSH, ASYNC);
2869 }
2870 }
2871 }
2872
6d2010ae
A
2873 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS) | DBG_FUNC_START,
2874 (uintptr_t) pmap, cpus_to_signal, flush_self, startv, 0);
0c530ab8 2875
2d21ac55 2876 if (cpus_to_signal) {
935ed37a
A
2877 cpu_set cpus_to_respond = cpus_to_signal;
2878
0c530ab8
A
2879 deadline = mach_absolute_time() + LockTimeOut;
2880 /*
2881 * Wait for those other cpus to acknowledge
2882 */
935ed37a 2883 while (cpus_to_respond != 0) {
060df5ea 2884 long orig_acks = 0;
6d2010ae 2885
935ed37a
A
2886 for (cpu = 0, cpu_bit = 1; cpu < real_ncpus; cpu++, cpu_bit <<= 1) {
2887 if ((cpus_to_respond & cpu_bit) != 0) {
2888 if (!cpu_datap(cpu)->cpu_running ||
2889 cpu_datap(cpu)->cpu_tlb_invalid == FALSE ||
2890 !CPU_CR3_IS_ACTIVE(cpu)) {
2891 cpus_to_respond &= ~cpu_bit;
2892 }
2893 cpu_pause();
2d21ac55 2894 }
935ed37a
A
2895 if (cpus_to_respond == 0)
2896 break;
0c530ab8 2897 }
6d2010ae
A
2898
2899 if (cpus_to_respond && (mach_absolute_time() > deadline)) {
060df5ea
A
2900 if (machine_timeout_suspended())
2901 continue;
2902 pmap_tlb_flush_timeout = TRUE;
2903 orig_acks = NMIPI_acks;
2904 pmap_cpuset_NMIPI(cpus_to_respond);
2905
2906 panic("TLB invalidation IPI timeout: "
2907 "CPU(s) failed to respond to interrupts, unresponsive CPU bitmap: 0x%lx, NMIPI acks: orig: 0x%lx, now: 0x%lx",
2908 cpus_to_respond, orig_acks, NMIPI_acks);
2909 }
0c530ab8 2910 }
0c530ab8 2911 }
0c530ab8
A
2912 /*
2913 * Flush local tlb if required.
2914 * We need this flush even if the pmap being changed
2915 * is the user map... in case we do a copyin/out
2916 * before returning to user mode.
2917 */
2918 if (flush_self)
2919 flush_tlb();
2920
b0d623f7
A
2921 if ((pmap == kernel_pmap) && (flush_self != TRUE)) {
2922 panic("pmap_flush_tlbs: pmap == kernel_pmap && flush_self != TRUE; kernel CR3: 0x%llX, CPU active CR3: 0x%llX, CPU Task Map: %d", kernel_pmap->pm_cr3, current_cpu_datap()->cpu_active_cr3, current_cpu_datap()->cpu_task_map);
2923 }
2924
6d2010ae
A
2925 PMAP_TRACE_CONSTANT(PMAP_CODE(PMAP__FLUSH_TLBS) | DBG_FUNC_END,
2926 (uintptr_t) pmap, cpus_to_signal, startv, endv, 0);
0c530ab8
A
2927}
2928
2929void
2930process_pmap_updates(void)
2931{
2d21ac55
A
2932 assert(ml_get_interrupts_enabled() == 0 || get_preemption_level() != 0);
2933
0c530ab8
A
2934 flush_tlb();
2935
2936 current_cpu_datap()->cpu_tlb_invalid = FALSE;
2937 __asm__ volatile("mfence");
2938}
2939
2940void
2941pmap_update_interrupt(void)
2942{
2d21ac55
A
2943 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT) | DBG_FUNC_START,
2944 0, 0, 0, 0, 0);
0c530ab8
A
2945
2946 process_pmap_updates();
2947
2d21ac55
A
2948 PMAP_TRACE(PMAP_CODE(PMAP__UPDATE_INTERRUPT) | DBG_FUNC_END,
2949 0, 0, 0, 0, 0);
0c530ab8 2950}
0c530ab8
A
2951#ifdef PMAP_DEBUG
2952void
2953pmap_dump(pmap_t p)
2954{
2955 int i;
2956
2957 kprintf("pmap 0x%x\n",p);
2958
2959 kprintf(" pm_cr3 0x%llx\n",p->pm_cr3);
2960 kprintf(" pm_pml4 0x%x\n",p->pm_pml4);
2961 kprintf(" pm_pdpt 0x%x\n",p->pm_pdpt);
2962
2963 kprintf(" pml4[0] 0x%llx\n",*p->pm_pml4);
2964 for (i=0;i<8;i++)
2965 kprintf(" pdpt[%d] 0x%llx\n",i, p->pm_pdpt[i]);
2966}
2967
2968void pmap_dump_wrap(void)
2969{
2970 pmap_dump(current_cpu_datap()->cpu_active_thread->task->map->pmap);
2971}
2972
2973void
2974dump_4GB_pdpt(pmap_t p)
2975{
2976 int spl;
2977 pdpt_entry_t *user_pdptp;
2978 pdpt_entry_t *kern_pdptp;
2979 pdpt_entry_t *pml4p;
2980
2981 spl = splhigh();
2982 while ((user_pdptp = pmap64_pdpt(p, 0x0)) == PDPT_ENTRY_NULL) {
2983 splx(spl);
2984 pmap_expand_pml4(p, 0x0);
2985 spl = splhigh();
2986 }
2987 kern_pdptp = kernel_pmap->pm_pdpt;
2988 if (kern_pdptp == NULL)
2989 panic("kern_pdptp == NULL");
2990 kprintf("dump_4GB_pdpt(%p)\n"
2991 "kern_pdptp=%p (phys=0x%016llx)\n"
2992 "\t 0x%08x: 0x%016llx\n"
2993 "\t 0x%08x: 0x%016llx\n"
2994 "\t 0x%08x: 0x%016llx\n"
2995 "\t 0x%08x: 0x%016llx\n"
2996 "\t 0x%08x: 0x%016llx\n"
2997 "user_pdptp=%p (phys=0x%016llx)\n"
2998 "\t 0x%08x: 0x%016llx\n"
2999 "\t 0x%08x: 0x%016llx\n"
3000 "\t 0x%08x: 0x%016llx\n"
3001 "\t 0x%08x: 0x%016llx\n"
3002 "\t 0x%08x: 0x%016llx\n",
3003 p, kern_pdptp, kvtophys(kern_pdptp),
3004 kern_pdptp+0, *(kern_pdptp+0),
3005 kern_pdptp+1, *(kern_pdptp+1),
3006 kern_pdptp+2, *(kern_pdptp+2),
3007 kern_pdptp+3, *(kern_pdptp+3),
3008 kern_pdptp+4, *(kern_pdptp+4),
3009 user_pdptp, kvtophys(user_pdptp),
3010 user_pdptp+0, *(user_pdptp+0),
3011 user_pdptp+1, *(user_pdptp+1),
3012 user_pdptp+2, *(user_pdptp+2),
3013 user_pdptp+3, *(user_pdptp+3),
3014 user_pdptp+4, *(user_pdptp+4));
3015 kprintf("user pm_cr3=0x%016llx pm_hold=0x%08x pm_pml4=0x%08x\n",
3016 p->pm_cr3, p->pm_hold, p->pm_pml4);
3017 pml4p = (pdpt_entry_t *)p->pm_hold;
3018 if (pml4p == NULL)
3019 panic("user pml4p == NULL");
3020 kprintf("\t 0x%08x: 0x%016llx\n"
3021 "\t 0x%08x: 0x%016llx\n",
3022 pml4p+0, *(pml4p),
3023 pml4p+KERNEL_UBER_PML4_INDEX, *(pml4p+KERNEL_UBER_PML4_INDEX));
3024 kprintf("kern pm_cr3=0x%016llx pm_hold=0x%08x pm_pml4=0x%08x\n",
3025 kernel_pmap->pm_cr3, kernel_pmap->pm_hold, kernel_pmap->pm_pml4);
3026 pml4p = (pdpt_entry_t *)kernel_pmap->pm_hold;
3027 if (pml4p == NULL)
3028 panic("kern pml4p == NULL");
3029 kprintf("\t 0x%08x: 0x%016llx\n"
3030 "\t 0x%08x: 0x%016llx\n",
3031 pml4p+0, *(pml4p),
3032 pml4p+511, *(pml4p+511));
3033 splx(spl);
3034}
3035
3036void dump_4GB_pdpt_thread(thread_t tp)
3037{
3038 dump_4GB_pdpt(tp->map->pmap);
3039}
3040
3041
3042#endif