xref: /freebsd/lib/libkvm/kvm_amd64.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*-
2  * Copyright (c) 1989, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software developed by the Computer Systems
6  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
7  * BG 91-66 and contributed to Berkeley.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #if defined(LIBC_SCCS) && !defined(lint)
42 #if 0
43 static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46 
47 /*
48  * AMD64 machine dependent routines for kvm.  Hopefully, the forthcoming
49  * vm code will one day obsolete this module.
50  */
51 
52 #include <sys/param.h>
53 #include <sys/user.h>
54 #include <sys/proc.h>
55 #include <sys/stat.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <nlist.h>
59 #include <kvm.h>
60 
61 #include <vm/vm.h>
62 #include <vm/vm_param.h>
63 
64 #include <limits.h>
65 
66 #include "kvm_private.h"
67 
68 #ifndef btop
69 #define	btop(x)		(amd64_btop(x))
70 #define	ptob(x)		(amd64_ptob(x))
71 #endif
72 
73 struct vmstate {
74 	pml4_entry_t	*PML4;
75 };
76 
77 void
78 _kvm_freevtop(kvm_t *kd)
79 {
80 	if (kd->vmst != 0) {
81 		if (kd->vmst->PML4) {
82 			free(kd->vmst->PML4);
83 		}
84 		free(kd->vmst);
85 	}
86 }
87 
88 int
89 _kvm_initvtop(kvm_t *kd)
90 {
91 	struct vmstate *vm;
92 	struct nlist nlist[2];
93 	u_long pa;
94 	u_long kernbase;
95 	pml4_entry_t	*PML4;
96 
97 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
98 	if (vm == 0) {
99 		_kvm_err(kd, kd->program, "cannot allocate vm");
100 		return (-1);
101 	}
102 	kd->vmst = vm;
103 	vm->PML4 = 0;
104 
105 	nlist[0].n_name = "kernbase";
106 	nlist[1].n_name = 0;
107 
108 	if (kvm_nlist(kd, nlist) != 0) {
109 		_kvm_err(kd, kd->program, "bad namelist - no kernbase");
110 		return (-1);
111 	}
112 	kernbase = nlist[0].n_value;
113 
114 	nlist[0].n_name = "KPML4phys";
115 	nlist[1].n_name = 0;
116 
117 	if (kvm_nlist(kd, nlist) != 0) {
118 		_kvm_err(kd, kd->program, "bad namelist - no KPML4phys");
119 		return (-1);
120 	}
121 	if (kvm_read(kd, (nlist[0].n_value - kernbase), &pa, sizeof(pa)) !=
122 	    sizeof(pa)) {
123 		_kvm_err(kd, kd->program, "cannot read KPML4phys");
124 		return (-1);
125 	}
126 	PML4 = _kvm_malloc(kd, PAGE_SIZE);
127 	if (kvm_read(kd, pa, PML4, PAGE_SIZE) != PAGE_SIZE) {
128 		_kvm_err(kd, kd->program, "cannot read KPML4phys");
129 		return (-1);
130 	}
131 	vm->PML4 = PML4;
132 	return (0);
133 }
134 
135 static int
136 _kvm_vatop(kvm_t *kd, u_long va, u_long *pa)
137 {
138 	struct vmstate *vm;
139 	u_long offset;
140 	u_long pdpe_pa;
141 	u_long pde_pa;
142 	u_long pte_pa;
143 	pml4_entry_t pml4e;
144 	pdp_entry_t pdpe;
145 	pd_entry_t pde;
146 	pt_entry_t pte;
147 	u_long pml4eindex;
148 	u_long pdpeindex;
149 	u_long pdeindex;
150 	u_long pteindex;
151 	int i;
152 
153 	if (ISALIVE(kd)) {
154 		_kvm_err(kd, 0, "kvm_vatop called in live kernel!");
155 		return((off_t)0);
156 	}
157 
158 	vm = kd->vmst;
159 	offset = va & (PAGE_SIZE - 1);
160 
161 	/*
162 	 * If we are initializing (kernel page table descriptor pointer
163 	 * not yet set) then return pa == va to avoid infinite recursion.
164 	 */
165 	if (vm->PML4 == 0) {
166 		*pa = va;
167 		return (PAGE_SIZE - offset);
168 	}
169 
170 	pml4eindex = (va >> PML4SHIFT) & (NPML4EPG - 1);
171 	pml4e = vm->PML4[pml4eindex];
172 	if (((u_long)pml4e & PG_V) == 0)
173 		goto invalid;
174 
175 	pdpeindex = (va >> PDPSHIFT) & (NPDPEPG-1);
176 	pdpe_pa = ((u_long)pml4e & PG_FRAME) + (pdpeindex * sizeof(pdp_entry_t));
177 
178 	/* XXX This has to be a physical address read, kvm_read is virtual */
179 	if (lseek(kd->pmfd, pdpe_pa, 0) == -1) {
180 		_kvm_syserr(kd, kd->program, "_kvm_vatop: lseek pdpe_pa");
181 		goto invalid;
182 	}
183 	if (read(kd->pmfd, &pdpe, sizeof pdpe) != sizeof pdpe) {
184 		_kvm_syserr(kd, kd->program, "_kvm_vatop: read pdpe");
185 		goto invalid;
186 	}
187 	if (((u_long)pdpe & PG_V) == 0)
188 		goto invalid;
189 
190 
191 	pdeindex = (va >> PDRSHIFT) & (NPDEPG-1);
192 	pde_pa = ((u_long)pdpe & PG_FRAME) + (pdeindex * sizeof(pd_entry_t));
193 
194 	/* XXX This has to be a physical address read, kvm_read is virtual */
195 	if (lseek(kd->pmfd, pde_pa, 0) == -1) {
196 		_kvm_syserr(kd, kd->program, "_kvm_vatop: lseek pde_pa");
197 		goto invalid;
198 	}
199 	if (read(kd->pmfd, &pde, sizeof pde) != sizeof pde) {
200 		_kvm_syserr(kd, kd->program, "_kvm_vatop: read pde");
201 		goto invalid;
202 	}
203 	if (((u_long)pde & PG_V) == 0)
204 		goto invalid;
205 
206 	if ((u_long)pde & PG_PS) {
207 	      /*
208 	       * No final-level page table; ptd describes one 2MB page.
209 	       */
210 #define	PAGE2M_MASK	(NBPDR - 1)
211 #define	PG_FRAME2M	(~PAGE2M_MASK)
212 		*pa = ((u_long)pde & PG_FRAME2M) + (va & PAGE2M_MASK);
213 		return (NBPDR - (va & PAGE2M_MASK));
214 	}
215 
216 	pteindex = (va >> PAGE_SHIFT) & (NPTEPG-1);
217 	pte_pa = ((u_long)pde & PG_FRAME) + (pteindex * sizeof(pt_entry_t));
218 
219 	/* XXX This has to be a physical address read, kvm_read is virtual */
220 	if (lseek(kd->pmfd, pte_pa, 0) == -1) {
221 		_kvm_syserr(kd, kd->program, "_kvm_vatop: lseek");
222 		goto invalid;
223 	}
224 	if (read(kd->pmfd, &pte, sizeof pte) != sizeof pte) {
225 		_kvm_syserr(kd, kd->program, "_kvm_vatop: read");
226 		goto invalid;
227 	}
228 	if (((u_long)pte & PG_V) == 0)
229 		goto invalid;
230 
231 	*pa = ((u_long)pte & PG_FRAME) + offset;
232 	return (PAGE_SIZE - offset);
233 
234 invalid:
235 	_kvm_err(kd, 0, "invalid address (%x)", va);
236 	return (0);
237 }
238 
239 int
240 _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
241 {
242 	return (_kvm_vatop(kd, va, pa));
243 }
244