xref: /illumos-gate/usr/src/uts/i86pc/boot/boot_mmu.c (revision 2d6eb4a5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * WARNING: This file is used by both dboot and the kernel.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/machparam.h>
33 #include <sys/mach_mmu.h>
34 #ifdef __xpv
35 #include <sys/hypervisor.h>
36 #endif
37 
38 #ifdef _BOOT
39 #include <dboot/dboot_printf.h>
40 #define	bop_panic dboot_panic
41 #else
42 #include <sys/bootconf.h>
43 #endif
44 
45 uint_t shift_amt_nopae[] = {12, 22};
46 uint_t shift_amt_pae[] = {12, 21, 30, 39};
47 uint_t *shift_amt;
48 uint_t ptes_per_table;
49 uint_t pte_size;
50 uint32_t lpagesize;
51 paddr_t top_page_table;
52 uint_t top_level;
53 
54 /*
55  * Return the index corresponding to a virt address at a given page table level.
56  */
57 static uint_t
vatoindex(uint64_t va,uint_t level)58 vatoindex(uint64_t va, uint_t level)
59 {
60 	return ((va >> shift_amt[level]) & (ptes_per_table - 1));
61 }
62 
63 /*
64  * Return a pointer to the page table entry that maps a virtual address.
65  * If there is no page table and probe_only is not set, one is created.
66  */
67 x86pte_t *
find_pte(uint64_t va,paddr_t * pa,uint_t level,uint_t probe_only)68 find_pte(uint64_t va, paddr_t *pa, uint_t level, uint_t probe_only)
69 {
70 	uint_t l;
71 	uint_t index;
72 	paddr_t table;
73 
74 	if (pa)
75 		*pa = 0;
76 
77 #ifndef _BOOT
78 	if (IN_HYPERVISOR_VA(va))
79 		return (NULL);
80 #endif
81 
82 	/*
83 	 * Walk down the page tables creating any needed intermediate tables.
84 	 */
85 	table = top_page_table;
86 	for (l = top_level; l != level; --l) {
87 		uint64_t pteval;
88 		paddr_t new_table;
89 
90 		index = vatoindex(va, l);
91 		pteval = get_pteval(table, index);
92 
93 		/*
94 		 * Life is easy if we find the pagetable.  We just use it.
95 		 */
96 		if (pteval & PT_VALID) {
97 			table = ma_to_pa(pteval & MMU_PAGEMASK);
98 			if (table == -1) {
99 				if (probe_only)
100 					return (NULL);
101 				bop_panic("find_pte(): phys not found!");
102 			}
103 			continue;
104 		}
105 
106 		if (probe_only)
107 			return (NULL);
108 
109 		new_table = make_ptable(&pteval, l);
110 		set_pteval(table, index, l, pteval);
111 
112 		table = new_table;
113 	}
114 
115 	/*
116 	 * Return a pointer into the current pagetable.
117 	 */
118 	index = vatoindex(va, l);
119 	if (pa)
120 		*pa = table + index * pte_size;
121 	return (map_pte(table, index));
122 }
123