summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86pc/boot/boot_mmu.c
blob: e652bb728d233fe5bf34799fff503dfd8b12bed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * WARNING: This file is used by both dboot and the kernel.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/param.h>
#include <sys/machparam.h>
#include <sys/mach_mmu.h>
#ifdef __xpv
#include <sys/hypervisor.h>
#endif

#ifdef _BOOT
#include <dboot/dboot_printf.h>
#define	bop_panic dboot_panic
#else
#include <sys/bootconf.h>
#endif

uint_t shift_amt_nopae[] = {12, 22};
uint_t shift_amt_pae[] = {12, 21, 30, 39};
uint_t *shift_amt;
uint_t ptes_per_table;
uint_t pte_size;
uint32_t lpagesize;
paddr_t top_page_table;
uint_t top_level;

/*
 * Return the index corresponding to a virt address at a given page table level.
 */
static uint_t
vatoindex(uint64_t va, uint_t level)
{
	return ((va >> shift_amt[level]) & (ptes_per_table - 1));
}

/*
 * Return a pointer to the page table entry that maps a virtual address.
 * If there is no page table and probe_only is not set, one is created.
 */
x86pte_t *
find_pte(uint64_t va, paddr_t *pa, uint_t level, uint_t probe_only)
{
	uint_t l;
	uint_t index;
	paddr_t table;

	if (pa)
		*pa = 0;

#ifndef _BOOT
	if (IN_HYPERVISOR_VA(va))
		return (NULL);
#endif

	/*
	 * Walk down the page tables creating any needed intermediate tables.
	 */
	table = top_page_table;
	for (l = top_level; l != level; --l) {
		uint64_t pteval;
		paddr_t new_table;

		index = vatoindex(va, l);
		pteval = get_pteval(table, index);

		/*
		 * Life is easy if we find the pagetable.  We just use it.
		 */
		if (pteval & PT_VALID) {
			table = ma_to_pa(pteval & MMU_PAGEMASK);
			if (table == -1) {
				if (probe_only)
					return (NULL);
				bop_panic("find_pte(): phys not found!");
			}
			continue;
		}

		if (probe_only)
			return (NULL);

		new_table = make_ptable(&pteval, l);
		set_pteval(table, index, l, pteval);

		table = new_table;
	}

	/*
	 * Return a pointer into the current pagetable.
	 */
	index = vatoindex(va, l);
	if (pa)
		*pa = table + index * pte_size;
	return (map_pte(table, index));
}