xref: /openbsd-src/lib/libkvm/kvm_i386.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: kvm_i386.c,v 1.10 2001/12/05 02:23:11 art 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. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
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 #else
45 static char *rcsid = "$OpenBSD: kvm_i386.c,v 1.10 2001/12/05 02:23:11 art Exp $";
46 #endif
47 #endif /* LIBC_SCCS and not lint */
48 
49 /*
50  * i386 machine dependent routines for kvm.  Hopefully, the forthcoming
51  * vm code will one day obsolete this module.
52  */
53 
54 #include <sys/param.h>
55 #include <sys/user.h>
56 #include <sys/proc.h>
57 #include <sys/stat.h>
58 #include <stdlib.h>
59 #include <unistd.h>
60 #include <nlist.h>
61 #include <kvm.h>
62 
63 #include <uvm/uvm_extern.h>
64 #include <machine/vmparam.h>
65 #include <machine/pmap.h>
66 
67 #include <limits.h>
68 #include <db.h>
69 
70 #include "kvm_private.h"
71 
72 #include <machine/pte.h>
73 
74 struct vmstate {
75 	pd_entry_t *PTD;
76 };
77 
78 void
79 _kvm_freevtop(kd)
80 	kvm_t *kd;
81 {
82 	if (kd->vmst != 0) {
83 		if (kd->vmst->PTD != 0)
84 			free(kd->vmst->PTD);
85 
86 		free(kd->vmst);
87 	}
88 }
89 
90 int
91 _kvm_initvtop(kd)
92 	kvm_t *kd;
93 {
94 	struct vmstate *vm;
95 	struct nlist nlist[2];
96 	u_long pa;
97 
98 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
99 	if (vm == 0)
100 		return (-1);
101 	kd->vmst = vm;
102 
103 	nlist[0].n_name = "_PTDpaddr";
104 	nlist[1].n_name = 0;
105 
106 	if (kvm_nlist(kd, nlist) != 0) {
107 		_kvm_err(kd, kd->program, "bad namelist");
108 		return (-1);
109 	}
110 
111 	vm->PTD = 0;
112 
113 	if (_kvm_pread(kd, kd->pmfd, &pa, sizeof pa, (off_t)_kvm_pa2off(kd, nlist[0].n_value - KERNBASE)) != sizeof pa) {
114 		goto invalid;
115 	}
116 
117 	vm->PTD = (pd_entry_t *)_kvm_malloc(kd, NBPG);
118 
119 	if (_kvm_pread(kd, kd->pmfd, vm->PTD, NBPG, (off_t)_kvm_pa2off(kd, pa)) != NBPG) {
120 		goto invalid;
121 	}
122 
123 	return (0);
124 
125 invalid:
126 	if (vm->PTD != 0)
127 		free(vm->PTD);
128 	return (-1);
129 }
130 
131 /*
132  * Translate a kernel virtual address to a physical address.
133  */
134 int
135 _kvm_kvatop(kd, va, pa)
136 	kvm_t *kd;
137 	u_long va;
138 	u_long *pa;
139 {
140 	struct vmstate *vm;
141 	u_long offset;
142 	u_long pte_pa;
143 	pt_entry_t pte;
144 
145 	if (ISALIVE(kd)) {
146 		_kvm_err(kd, 0, "vatop called in live kernel!");
147 		return(0);
148 	}
149 
150 	vm = kd->vmst;
151 	offset = va & PGOFSET;
152 
153         /*
154          * If we are initializing (kernel page table descriptor pointer
155 	 * not yet set) * then return pa == va to avoid infinite recursion.
156          */
157         if (vm->PTD == 0) {
158                 *pa = va;
159                 return (NBPG - offset);
160         }
161 	if ((vm->PTD[pdei(va)] & PG_V) == 0)
162 		goto invalid;
163 
164 	pte_pa = (vm->PTD[pdei(va)] & PG_FRAME) +
165 	    (ptei(va) * sizeof(pt_entry_t));
166 	/* XXX READ PHYSICAL XXX */
167 	{
168 		if (_kvm_pread(kd, kd->pmfd, &pte, sizeof pte, (off_t)_kvm_pa2off(kd, pte_pa)) != sizeof pte) {
169 			goto invalid;
170 		}
171 	}
172 
173 	*pa = (pte & PG_FRAME) + offset;
174 	return (NBPG - offset);
175 
176 invalid:
177 	_kvm_err(kd, 0, "invalid address (%lx)", va);
178 	return (0);
179 }
180 
181 /*
182  * Translate a physical address to a file-offset in the crash-dump.
183  */
184 off_t
185 _kvm_pa2off(kd, pa)
186 	kvm_t *kd;
187 	u_long pa;
188 {
189 	return((off_t)(kd->dump_off + pa));
190 }
191