xref: /openbsd-src/lib/libkvm/kvm_i386.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
1 /*	$OpenBSD: kvm_i386.c,v 1.13 2004/07/01 02:04:10 mickey 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 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
40 #else
41 static char *rcsid = "$OpenBSD: kvm_i386.c,v 1.13 2004/07/01 02:04:10 mickey Exp $";
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44 
45 /*
46  * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
47  * vm code will one day obsolete this module.
48  */
49 
50 #include <sys/param.h>
51 #include <sys/user.h>
52 #include <sys/proc.h>
53 #include <sys/stat.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <nlist.h>
57 #include <kvm.h>
58 
59 #include <uvm/uvm_extern.h>
60 #include <machine/vmparam.h>
61 #include <machine/pmap.h>
62 
63 #include <limits.h>
64 #include <db.h>
65 
66 #include "kvm_private.h"
67 
68 #include <machine/pte.h>
69 
70 struct vmstate {
71 	pd_entry_t *PTD;
72 };
73 
74 void
75 _kvm_freevtop(kvm_t *kd)
76 {
77 	if (kd->vmst != NULL) {
78 		if (kd->vmst->PTD != NULL)
79 			free(kd->vmst->PTD);
80 
81 		free(kd->vmst);
82 		kd->vmst = NULL;
83 	}
84 }
85 
86 int
87 _kvm_initvtop(kvm_t *kd)
88 {
89 	struct nlist nlist[2];
90 	struct vmstate *vm;
91 	u_long pa;
92 
93 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
94 	if (vm == 0)
95 		return (-1);
96 	kd->vmst = vm;
97 
98 	nlist[0].n_name = "_PTDpaddr";
99 	nlist[1].n_name = 0;
100 
101 	if (kvm_nlist(kd, nlist) != 0) {
102 		_kvm_err(kd, kd->program, "bad namelist");
103 		return (-1);
104 	}
105 
106 	vm->PTD = 0;
107 
108 	if (_kvm_pread(kd, kd->pmfd, &pa, sizeof pa,
109 	    (off_t)_kvm_pa2off(kd, nlist[0].n_value - KERNBASE)) != sizeof pa)
110 		goto invalid;
111 
112 	vm->PTD = (pd_entry_t *)_kvm_malloc(kd, NBPG);
113 
114 	if (_kvm_pread(kd, kd->pmfd, vm->PTD, NBPG,
115 	    (off_t)_kvm_pa2off(kd, pa)) != NBPG)
116 		goto invalid;
117 
118 	return (0);
119 
120 invalid:
121 	if (vm->PTD != 0)
122 		free(vm->PTD);
123 	return (-1);
124 }
125 
126 /*
127  * Translate a kernel virtual address to a physical address.
128  */
129 int
130 _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
131 {
132 	u_long offset, pte_pa;
133 	struct vmstate *vm;
134 	pt_entry_t pte;
135 
136 	if (!kd->vmst) {
137 		_kvm_err(kd, 0, "vatop called before initvtop");
138 		return (0);
139 	}
140 
141 	if (ISALIVE(kd)) {
142 		_kvm_err(kd, 0, "vatop called in live kernel!");
143 		return (0);
144 	}
145 
146 	vm = kd->vmst;
147 	offset = va & PGOFSET;
148 
149 	/*
150 	 * If we are initializing (kernel page table descriptor pointer
151 	 * not yet set) * then return pa == va to avoid infinite recursion.
152 	 */
153 	if (vm->PTD == 0) {
154 		*pa = va;
155 		return (NBPG - offset);
156 	}
157 	if ((vm->PTD[pdei(va)] & PG_V) == 0)
158 		goto invalid;
159 
160 	pte_pa = (vm->PTD[pdei(va)] & PG_FRAME) +
161 	    (ptei(va) * sizeof(pt_entry_t));
162 
163 	/* XXX READ PHYSICAL XXX */
164 	if (_kvm_pread(kd, kd->pmfd, &pte, sizeof pte,
165 	    (off_t)_kvm_pa2off(kd, pte_pa)) != sizeof pte)
166 		goto invalid;
167 
168 	*pa = (pte & PG_FRAME) + offset;
169 	return (NBPG - offset);
170 
171 invalid:
172 	_kvm_err(kd, 0, "invalid address (%lx)", va);
173 	return (0);
174 }
175 
176 /*
177  * Translate a physical address to a file-offset in the crash-dump.
178  */
179 off_t
180 _kvm_pa2off(kvm_t *kd, u_long pa)
181 {
182 	return ((off_t)(kd->dump_off + pa));
183 }
184