xref: /onnv-gate/usr/src/uts/i86pc/boot/boot_mmu.c (revision 5084:7d838c5c0eed)
13446Smrj /*
23446Smrj  * CDDL HEADER START
33446Smrj  *
43446Smrj  * The contents of this file are subject to the terms of the
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * You may not use this file except in compliance with the License.
73446Smrj  *
83446Smrj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93446Smrj  * or http://www.opensolaris.org/os/licensing.
103446Smrj  * See the License for the specific language governing permissions
113446Smrj  * and limitations under the License.
123446Smrj  *
133446Smrj  * When distributing Covered Code, include this CDDL HEADER in each
143446Smrj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153446Smrj  * If applicable, add the following below this CDDL HEADER, with the
163446Smrj  * fields enclosed by brackets "[]" replaced with your own identifying
173446Smrj  * information: Portions Copyright [yyyy] [name of copyright owner]
183446Smrj  *
193446Smrj  * CDDL HEADER END
203446Smrj  */
213446Smrj 
223446Smrj /*
233446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
243446Smrj  * Use is subject to license terms.
253446Smrj  */
263446Smrj 
273446Smrj /*
283446Smrj  * WARNING: This file is used by both dboot and the kernel.
293446Smrj  */
303446Smrj 
313446Smrj #pragma ident	"%Z%%M%	%I%	%E% SMI"
323446Smrj 
333446Smrj #include <sys/param.h>
343446Smrj #include <sys/machparam.h>
353446Smrj #include <sys/mach_mmu.h>
36*5084Sjohnlev #ifdef __xpv
37*5084Sjohnlev #include <sys/hypervisor.h>
38*5084Sjohnlev #endif
393446Smrj 
403446Smrj #ifdef _BOOT
413446Smrj #include <dboot/dboot_printf.h>
423446Smrj #define	bop_panic dboot_panic
433446Smrj #else
443446Smrj #include <sys/bootconf.h>
453446Smrj #endif
463446Smrj 
473446Smrj uint_t shift_amt_nopae[] = {12, 22};
483446Smrj uint_t shift_amt_pae[] = {12, 21, 30, 39};
493446Smrj uint_t *shift_amt;
503446Smrj uint_t ptes_per_table;
513446Smrj uint_t pte_size;
523446Smrj uint32_t lpagesize;
533446Smrj paddr_t top_page_table;
543446Smrj uint_t top_level;
553446Smrj 
563446Smrj /*
573446Smrj  * Return the index corresponding to a virt address at a given page table level.
583446Smrj  */
593446Smrj static uint_t
vatoindex(uint64_t va,uint_t level)603446Smrj vatoindex(uint64_t va, uint_t level)
613446Smrj {
623446Smrj 	return ((va >> shift_amt[level]) & (ptes_per_table - 1));
633446Smrj }
643446Smrj 
653446Smrj /*
663446Smrj  * Return a pointer to the page table entry that maps a virtual address.
673446Smrj  * If there is no page table and probe_only is not set, one is created.
683446Smrj  */
693446Smrj x86pte_t *
find_pte(uint64_t va,paddr_t * pa,uint_t level,uint_t probe_only)703446Smrj find_pte(uint64_t va, paddr_t *pa, uint_t level, uint_t probe_only)
713446Smrj {
723446Smrj 	uint_t l;
733446Smrj 	uint_t index;
743446Smrj 	paddr_t table;
753446Smrj 
763446Smrj 	if (pa)
773446Smrj 		*pa = 0;
783446Smrj 
793446Smrj #ifndef _BOOT
803446Smrj 	if (IN_HYPERVISOR_VA(va))
813446Smrj 		return (NULL);
823446Smrj #endif
833446Smrj 
843446Smrj 	/*
853446Smrj 	 * Walk down the page tables creating any needed intermediate tables.
863446Smrj 	 */
873446Smrj 	table = top_page_table;
883446Smrj 	for (l = top_level; l != level; --l) {
893446Smrj 		uint64_t pteval;
903446Smrj 		paddr_t new_table;
913446Smrj 
923446Smrj 		index = vatoindex(va, l);
933446Smrj 		pteval = get_pteval(table, index);
943446Smrj 
953446Smrj 		/*
963446Smrj 		 * Life is easy if we find the pagetable.  We just use it.
973446Smrj 		 */
983446Smrj 		if (pteval & PT_VALID) {
993446Smrj 			table = ma_to_pa(pteval & MMU_PAGEMASK);
1003446Smrj 			if (table == -1) {
1013446Smrj 				if (probe_only)
1023446Smrj 					return (NULL);
1033446Smrj 				bop_panic("find_pte(): phys not found!");
1043446Smrj 			}
1053446Smrj 			continue;
1063446Smrj 		}
1073446Smrj 
1083446Smrj 		if (probe_only)
1093446Smrj 			return (NULL);
1103446Smrj 
1113446Smrj 		new_table = make_ptable(&pteval, l);
1123446Smrj 		set_pteval(table, index, l, pteval);
1133446Smrj 
1143446Smrj 		table = new_table;
1153446Smrj 	}
1163446Smrj 
1173446Smrj 	/*
1183446Smrj 	 * Return a pointer into the current pagetable.
1193446Smrj 	 */
1203446Smrj 	index = vatoindex(va, l);
1213446Smrj 	if (pa)
1223446Smrj 		*pa = table + index * pte_size;
1233446Smrj 	return (map_pte(table, index));
1243446Smrj }
125