xref: /openbsd/lib/libkvm/kvm_i386.c (revision 4bdff4be)
1 /*	$OpenBSD: kvm_i386.c,v 1.28 2021/12/01 16:53:28 deraadt Exp $ */
2 /*	$NetBSD: kvm_i386.c,v 1.9 1996/03/18 22:33:38 thorpej Exp $	*/
3 
4 /*-
5  * Copyright (c) 1989, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software developed by the Computer Systems
9  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
10  * BG 91-66 and contributed to Berkeley.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 /*
38  * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
39  * vm code will one day obsolete this module.
40  */
41 
42 #include <sys/types.h>
43 #include <sys/signal.h>
44 #include <sys/proc.h>
45 #include <sys/stat.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <nlist.h>
49 #include <kvm.h>
50 
51 #include <uvm/uvm_extern.h>
52 #include <machine/param.h>
53 #include <machine/vmparam.h>
54 #include <machine/pmap.h>
55 
56 #include <limits.h>
57 #include <db.h>
58 
59 #include "kvm_private.h"
60 
61 #include <machine/pte.h>
62 
63 /*
64  * We access both normal and PAE entries in 32bit chunks.
65  * Use a local name to avoid conflicting with the kernel's maybe-public,
66  * maybe-not p[td]_entry_t typedefs.
67  */
68 typedef u_long ptd_entry_t;
69 
70 /*
71  * These must match the values in pmap.c/pmapae.c
72  * First the non-PAE versions
73  */
74 #define PD_MASK		0xffc00000	/* page directory address bits */
75 #define PT_MASK		0x003ff000	/* page table address bits */
76 
77 /*
78  * PAE versions
79  *
80  * paddr_t is still 32bits, so the top 32bits of PDEs and PTEs only
81  * matters for the NX bit...which libkvm doesn't care about
82  */
83 #define PAE_PDSHIFT	21
84 #define PAE_PD_MASK	0xffe00000	/* page directory address bits */
85 #define PAE_PT_MASK	0x001ff000	/* page table address bits */
86 
87 #define PG_FRAME	0xfffff000
88 
89 static int cpu_pae;
90 
91 struct vmstate {
92 	ptd_entry_t *PTD;
93 	ptd_entry_t PD_mask;
94 	ptd_entry_t PT_mask;
95 	int PD_shift;
96 	int PG_shift;
97 };
98 
99 #define pdei(vm,VA)	(((VA) & (vm)->PD_mask) >> (vm)->PD_shift)
100 #define ptei(vm,VA)	(((VA) & (vm)->PT_mask) >> PAGE_SHIFT)
101 
102 void
103 _kvm_freevtop(kvm_t *kd)
104 {
105 	if (kd->vmst != NULL) {
106 		free(kd->vmst->PTD);
107 
108 		free(kd->vmst);
109 		kd->vmst = NULL;
110 	}
111 }
112 
113 int
114 _kvm_initvtop(kvm_t *kd)
115 {
116 	struct nlist nl[4];
117 	struct vmstate *vm;
118 	u_long pa, PTDsize;
119 
120 	vm = _kvm_malloc(kd, sizeof(*vm));
121 	if (vm == NULL)
122 		return (-1);
123 	kd->vmst = vm;
124 
125 	vm->PTD = NULL;
126 
127 	nl[0].n_name = "_PTDpaddr";
128 	nl[1].n_name = "_PTDsize";
129 	nl[2].n_name = "_cpu_pae";
130 	nl[3].n_name = NULL;
131 
132 	if (kvm_nlist(kd, nl) != 0) {
133 		_kvm_err(kd, kd->program, "bad namelist");
134 		return (-1);
135 	}
136 
137 	if (_kvm_pread(kd, kd->pmfd, &cpu_pae, sizeof cpu_pae,
138 	    _kvm_pa2off(kd, nl[2].n_value - KERNBASE)) != sizeof cpu_pae)
139 		goto invalid;
140 
141 	if (_kvm_pread(kd, kd->pmfd, &PTDsize, sizeof PTDsize,
142 	    _kvm_pa2off(kd, nl[1].n_value - KERNBASE)) != sizeof PTDsize)
143 		goto invalid;
144 
145 	if (_kvm_pread(kd, kd->pmfd, &pa, sizeof pa,
146 	    _kvm_pa2off(kd, nl[0].n_value - KERNBASE)) != sizeof pa)
147 		goto invalid;
148 
149 	vm->PTD = _kvm_malloc(kd, PTDsize);
150 
151 	if (_kvm_pread(kd, kd->pmfd, vm->PTD, PTDsize,
152 	    _kvm_pa2off(kd, pa)) != PTDsize)
153 		goto invalid;
154 
155 	if (cpu_pae) {
156 		vm->PD_mask = PAE_PD_MASK;
157 		vm->PT_mask = PAE_PT_MASK;
158 		/* -1 here because entries are twice as large */
159 		vm->PD_shift = PAE_PDSHIFT - 1;
160 		vm->PG_shift = PAGE_SHIFT - 1;
161 	} else {
162 		vm->PD_mask = PD_MASK;
163 		vm->PT_mask = PT_MASK;
164 		vm->PD_shift = PDSHIFT;
165 		vm->PG_shift = PAGE_SHIFT;
166 	}
167 
168 	return (0);
169 
170 invalid:
171 	free(vm->PTD);
172 	vm->PTD = NULL;
173 	return (-1);
174 }
175 
176 /*
177  * Translate a kernel virtual address to a physical address.
178  */
179 int
180 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
181 {
182 	u_long offset, pte_pa;
183 	struct vmstate *vm;
184 	ptd_entry_t pte;
185 
186 	if (!kd->vmst) {
187 		_kvm_err(kd, 0, "vatop called before initvtop");
188 		return (0);
189 	}
190 
191 	if (ISALIVE(kd)) {
192 		_kvm_err(kd, 0, "vatop called in live kernel!");
193 		return (0);
194 	}
195 
196 	vm = kd->vmst;
197 	offset = va & (kd->nbpg - 1);
198 
199 	/*
200 	 * If we are initializing (kernel page table descriptor pointer
201 	 * not yet set) * then return pa == va to avoid infinite recursion.
202 	 */
203 	if (vm->PTD == NULL) {
204 		*pa = va;
205 		return (kd->nbpg - (int)offset);
206 	}
207 	if ((vm->PTD[pdei(vm,va)] & PG_V) == 0)
208 		goto invalid;
209 
210 	pte_pa = (vm->PTD[pdei(vm,va)] & PG_FRAME) +
211 	    (ptei(vm,va) * sizeof(ptd_entry_t));
212 
213 	/* XXX READ PHYSICAL XXX */
214 	if (_kvm_pread(kd, kd->pmfd, &pte, sizeof pte,
215 	    _kvm_pa2off(kd, pte_pa)) != sizeof pte)
216 		goto invalid;
217 
218 	if ((pte & PG_V) == 0)
219 		goto invalid;
220 	*pa = (pte & PG_FRAME) + offset;
221 	return (kd->nbpg - (int)offset);
222 
223 invalid:
224 	_kvm_err(kd, 0, "invalid address (%lx)", va);
225 	return (0);
226 }
227 
228 /*
229  * Translate a physical address to a file-offset in the crash-dump.
230  */
231 off_t
232 _kvm_pa2off(kvm_t *kd, paddr_t pa)
233 {
234 	return ((off_t)(kd->dump_off + pa));
235 }
236