xref: /illumos-gate/usr/src/uts/i86pc/boot/boot_mmu.c (revision ae115bc7)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/param.h>
34 #include <sys/machparam.h>
35 #include <sys/mach_mmu.h>
36 
37 #ifdef _BOOT
38 #include <dboot/dboot_printf.h>
39 #define	bop_panic dboot_panic
40 #else
41 #include <sys/bootconf.h>
42 #endif
43 
44 uint_t shift_amt_nopae[] = {12, 22};
45 uint_t shift_amt_pae[] = {12, 21, 30, 39};
46 uint_t *shift_amt;
47 uint_t ptes_per_table;
48 uint_t pte_size;
49 uint32_t lpagesize;
50 paddr_t top_page_table;
51 uint_t top_level;
52 
53 /*
54  * Return the index corresponding to a virt address at a given page table level.
55  */
56 static uint_t
57 vatoindex(uint64_t va, uint_t level)
58 {
59 	return ((va >> shift_amt[level]) & (ptes_per_table - 1));
60 }
61 
62 /*
63  * Return a pointer to the page table entry that maps a virtual address.
64  * If there is no page table and probe_only is not set, one is created.
65  */
66 x86pte_t *
67 find_pte(uint64_t va, paddr_t *pa, uint_t level, uint_t probe_only)
68 {
69 	uint_t l;
70 	uint_t index;
71 	paddr_t table;
72 
73 	if (pa)
74 		*pa = 0;
75 
76 #ifndef _BOOT
77 	if (IN_HYPERVISOR_VA(va))
78 		return (NULL);
79 #endif
80 
81 	/*
82 	 * Walk down the page tables creating any needed intermediate tables.
83 	 */
84 	table = top_page_table;
85 	for (l = top_level; l != level; --l) {
86 		uint64_t pteval;
87 		paddr_t new_table;
88 
89 		index = vatoindex(va, l);
90 		pteval = get_pteval(table, index);
91 
92 		/*
93 		 * Life is easy if we find the pagetable.  We just use it.
94 		 */
95 		if (pteval & PT_VALID) {
96 			table = ma_to_pa(pteval & MMU_PAGEMASK);
97 			if (table == -1) {
98 				if (probe_only)
99 					return (NULL);
100 				bop_panic("find_pte(): phys not found!");
101 			}
102 			continue;
103 		}
104 
105 		if (probe_only)
106 			return (NULL);
107 
108 		new_table = make_ptable(&pteval, l);
109 		set_pteval(table, index, l, pteval);
110 
111 		table = new_table;
112 	}
113 
114 	/*
115 	 * Return a pointer into the current pagetable.
116 	 */
117 	index = vatoindex(va, l);
118 	if (pa)
119 		*pa = table + index * pte_size;
120 	return (map_pte(table, index));
121 }
122