xref: /openbsd-src/lib/libkvm/kvm_i386.c (revision ac9b4aacc1da35008afea06a5d23c2f2dea9b93e)
1 /*	$OpenBSD: kvm_i386.c,v 1.22 2012/07/09 08:43:10 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/param.h>
43 #include <sys/user.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/vmparam.h>
53 #include <machine/pmap.h>
54 
55 #include <limits.h>
56 #include <db.h>
57 
58 #include "kvm_private.h"
59 
60 #include <machine/pte.h>
61 
62 struct vmstate {
63 	pd_entry_t *PTD;
64 };
65 
66 void
67 _kvm_freevtop(kvm_t *kd)
68 {
69 	if (kd->vmst != NULL) {
70 		if (kd->vmst->PTD != NULL)
71 			free(kd->vmst->PTD);
72 
73 		free(kd->vmst);
74 		kd->vmst = NULL;
75 	}
76 }
77 
78 int
79 _kvm_initvtop(kvm_t *kd)
80 {
81 	struct nlist nl[2];
82 	struct vmstate *vm;
83 	u_long pa;
84 
85 	vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm));
86 	if (vm == NULL)
87 		return (-1);
88 	kd->vmst = vm;
89 
90 	vm->PTD = NULL;
91 
92 	nl[0].n_name = "_PTDpaddr";
93 	nl[1].n_name = NULL;
94 
95 	if (kvm_nlist(kd, nl) != 0) {
96 		_kvm_err(kd, kd->program, "bad namelist");
97 		return (-1);
98 	}
99 
100 	if (_kvm_pread(kd, kd->pmfd, &pa, sizeof pa,
101 	    (off_t)_kvm_pa2off(kd, nl[0].n_value - KERNBASE)) != sizeof pa)
102 		goto invalid;
103 
104 	vm->PTD = (pd_entry_t *)_kvm_malloc(kd, PAGE_SIZE);
105 
106 	if (_kvm_pread(kd, kd->pmfd, vm->PTD, PAGE_SIZE,
107 	    (off_t)_kvm_pa2off(kd, pa)) != PAGE_SIZE)
108 		goto invalid;
109 
110 	return (0);
111 
112 invalid:
113 	if (vm->PTD != NULL) {
114 		free(vm->PTD);
115 		vm->PTD = NULL;
116 	}
117 	return (-1);
118 }
119 
120 /*
121  * Translate a kernel virtual address to a physical address.
122  */
123 int
124 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
125 {
126 	u_long offset, pte_pa;
127 	struct vmstate *vm;
128 	pt_entry_t pte;
129 
130 	if (!kd->vmst) {
131 		_kvm_err(kd, 0, "vatop called before initvtop");
132 		return (0);
133 	}
134 
135 	if (ISALIVE(kd)) {
136 		_kvm_err(kd, 0, "vatop called in live kernel!");
137 		return (0);
138 	}
139 
140 	vm = kd->vmst;
141 	offset = va & PAGE_MASK;
142 
143 	/*
144 	 * If we are initializing (kernel page table descriptor pointer
145 	 * not yet set) * then return pa == va to avoid infinite recursion.
146 	 */
147 	if (vm->PTD == NULL) {
148 		*pa = va;
149 		return (PAGE_SIZE - (int)offset);
150 	}
151 	if ((vm->PTD[pdei(va)] & PG_V) == 0)
152 		goto invalid;
153 
154 	pte_pa = (vm->PTD[pdei(va)] & PG_FRAME) +
155 	    (ptei(va) * sizeof(pt_entry_t));
156 
157 	/* XXX READ PHYSICAL XXX */
158 	if (_kvm_pread(kd, kd->pmfd, &pte, sizeof pte,
159 	    (off_t)_kvm_pa2off(kd, pte_pa)) != sizeof pte)
160 		goto invalid;
161 
162 	*pa = (pte & PG_FRAME) + offset;
163 	return (PAGE_SIZE - (int)offset);
164 
165 invalid:
166 	_kvm_err(kd, 0, "invalid address (%lx)", va);
167 	return (0);
168 }
169 
170 /*
171  * Translate a physical address to a file-offset in the crash-dump.
172  */
173 off_t
174 _kvm_pa2off(kvm_t *kd, paddr_t pa)
175 {
176 	return ((off_t)(kd->dump_off + pa));
177 }
178