kvm_x86_64.c revision 1.2.2.2 1 1.2.2.2 thorpej /* $NetBSD: kvm_x86_64.c,v 1.2.2.2 2002/12/19 02:26:25 thorpej Exp $ */
2 1.2.2.2 thorpej
3 1.2.2.2 thorpej /*-
4 1.2.2.2 thorpej * Copyright (c) 1989, 1992, 1993
5 1.2.2.2 thorpej * The Regents of the University of California. All rights reserved.
6 1.2.2.2 thorpej *
7 1.2.2.2 thorpej * This code is derived from software developed by the Computer Systems
8 1.2.2.2 thorpej * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9 1.2.2.2 thorpej * BG 91-66 and contributed to Berkeley.
10 1.2.2.2 thorpej *
11 1.2.2.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.2.2.2 thorpej * modification, are permitted provided that the following conditions
13 1.2.2.2 thorpej * are met:
14 1.2.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.2.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.2.2.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.2.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.2.2.2 thorpej * must display the following acknowledgement:
21 1.2.2.2 thorpej * This product includes software developed by the University of
22 1.2.2.2 thorpej * California, Berkeley and its contributors.
23 1.2.2.2 thorpej * 4. Neither the name of the University nor the names of its contributors
24 1.2.2.2 thorpej * may be used to endorse or promote products derived from this software
25 1.2.2.2 thorpej * without specific prior written permission.
26 1.2.2.2 thorpej *
27 1.2.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.2.2.2 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.2.2.2 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.2.2.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.2.2.2 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.2.2.2 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.2.2.2 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.2.2.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.2.2.2 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.2.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.2.2.2 thorpej * SUCH DAMAGE.
38 1.2.2.2 thorpej */
39 1.2.2.2 thorpej
40 1.2.2.2 thorpej #include <sys/cdefs.h>
41 1.2.2.2 thorpej #if defined(LIBC_SCCS) && !defined(lint)
42 1.2.2.2 thorpej #if 0
43 1.2.2.2 thorpej static char sccsid[] = "@(#)kvm_hp300.c 8.1 (Berkeley) 6/4/93";
44 1.2.2.2 thorpej #else
45 1.2.2.2 thorpej __RCSID("$NetBSD: kvm_x86_64.c,v 1.2.2.2 2002/12/19 02:26:25 thorpej Exp $");
46 1.2.2.2 thorpej #endif
47 1.2.2.2 thorpej #endif /* LIBC_SCCS and not lint */
48 1.2.2.2 thorpej
49 1.2.2.2 thorpej /*
50 1.2.2.2 thorpej * x86-64 machine dependent routines for kvm.
51 1.2.2.2 thorpej */
52 1.2.2.2 thorpej
53 1.2.2.2 thorpej #include <sys/param.h>
54 1.2.2.2 thorpej #include <sys/user.h>
55 1.2.2.2 thorpej #include <sys/proc.h>
56 1.2.2.2 thorpej #include <sys/stat.h>
57 1.2.2.2 thorpej #include <sys/kcore.h>
58 1.2.2.2 thorpej #include <machine/kcore.h>
59 1.2.2.2 thorpej #include <stdlib.h>
60 1.2.2.2 thorpej #include <unistd.h>
61 1.2.2.2 thorpej #include <nlist.h>
62 1.2.2.2 thorpej #include <kvm.h>
63 1.2.2.2 thorpej
64 1.2.2.2 thorpej #include <uvm/uvm_extern.h>
65 1.2.2.2 thorpej
66 1.2.2.2 thorpej #include <limits.h>
67 1.2.2.2 thorpej #include <db.h>
68 1.2.2.2 thorpej
69 1.2.2.2 thorpej #include "kvm_private.h"
70 1.2.2.2 thorpej
71 1.2.2.2 thorpej #include <machine/pmap.h>
72 1.2.2.2 thorpej #include <machine/pte.h>
73 1.2.2.2 thorpej #include <machine/vmparam.h>
74 1.2.2.2 thorpej
75 1.2.2.2 thorpej void
76 1.2.2.2 thorpej _kvm_freevtop(kd)
77 1.2.2.2 thorpej kvm_t *kd;
78 1.2.2.2 thorpej {
79 1.2.2.2 thorpej
80 1.2.2.2 thorpej /* Not actually used for anything right now, but safe. */
81 1.2.2.2 thorpej if (kd->vmst != 0)
82 1.2.2.2 thorpej free(kd->vmst);
83 1.2.2.2 thorpej }
84 1.2.2.2 thorpej
85 1.2.2.2 thorpej /*ARGSUSED*/
86 1.2.2.2 thorpej int
87 1.2.2.2 thorpej _kvm_initvtop(kd)
88 1.2.2.2 thorpej kvm_t *kd;
89 1.2.2.2 thorpej {
90 1.2.2.2 thorpej
91 1.2.2.2 thorpej return (0);
92 1.2.2.2 thorpej }
93 1.2.2.2 thorpej
94 1.2.2.2 thorpej /*
95 1.2.2.2 thorpej * Translate a kernel virtual address to a physical address.
96 1.2.2.2 thorpej */
97 1.2.2.2 thorpej int
98 1.2.2.2 thorpej _kvm_kvatop(kd, va, pa)
99 1.2.2.2 thorpej kvm_t *kd;
100 1.2.2.2 thorpej u_long va;
101 1.2.2.2 thorpej u_long *pa;
102 1.2.2.2 thorpej {
103 1.2.2.2 thorpej cpu_kcore_hdr_t *cpu_kh;
104 1.2.2.2 thorpej u_long page_off;
105 1.2.2.2 thorpej pd_entry_t pde;
106 1.2.2.2 thorpej pt_entry_t pte;
107 1.2.2.2 thorpej paddr_t pde_pa, pte_pa;
108 1.2.2.2 thorpej
109 1.2.2.2 thorpej if (ISALIVE(kd)) {
110 1.2.2.2 thorpej _kvm_err(kd, 0, "vatop called in live kernel!");
111 1.2.2.2 thorpej return (0);
112 1.2.2.2 thorpej }
113 1.2.2.2 thorpej
114 1.2.2.2 thorpej cpu_kh = kd->cpu_data;
115 1.2.2.2 thorpej page_off = va & PGOFSET;
116 1.2.2.2 thorpej
117 1.2.2.2 thorpej /*
118 1.2.2.2 thorpej * Find and read all entries to get to the pa.
119 1.2.2.2 thorpej */
120 1.2.2.2 thorpej
121 1.2.2.2 thorpej /*
122 1.2.2.2 thorpej * Level 4.
123 1.2.2.2 thorpej */
124 1.2.2.2 thorpej pde_pa = cpu_kh->ptdpaddr + (pl4_i(va) * sizeof(pd_entry_t));
125 1.2.2.2 thorpej if (pread(kd->pmfd, (void *)&pde, sizeof(pde),
126 1.2.2.2 thorpej _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
127 1.2.2.2 thorpej _kvm_syserr(kd, 0, "could not read PT level 4 entry");
128 1.2.2.2 thorpej goto lose;
129 1.2.2.2 thorpej }
130 1.2.2.2 thorpej if ((pde & PG_V) == 0) {
131 1.2.2.2 thorpej _kvm_err(kd, 0, "invalid translation (invalid level 4 PDE)");
132 1.2.2.2 thorpej goto lose;
133 1.2.2.2 thorpej }
134 1.2.2.2 thorpej
135 1.2.2.2 thorpej /*
136 1.2.2.2 thorpej * Level 3.
137 1.2.2.2 thorpej */
138 1.2.2.2 thorpej pde_pa = (pde_pa + PG_FRAME) + (pl3_i(va) * sizeof(pd_entry_t));
139 1.2.2.2 thorpej if (pread(kd->pmfd, (void *)&pde, sizeof(pde),
140 1.2.2.2 thorpej _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
141 1.2.2.2 thorpej _kvm_syserr(kd, 0, "could not read PT level 3 entry");
142 1.2.2.2 thorpej goto lose;
143 1.2.2.2 thorpej }
144 1.2.2.2 thorpej if ((pde & PG_V) == 0) {
145 1.2.2.2 thorpej _kvm_err(kd, 0, "invalid translation (invalid level 3 PDE)");
146 1.2.2.2 thorpej goto lose;
147 1.2.2.2 thorpej }
148 1.2.2.2 thorpej
149 1.2.2.2 thorpej /*
150 1.2.2.2 thorpej * Level 2.
151 1.2.2.2 thorpej */
152 1.2.2.2 thorpej pde_pa = (pde_pa & PG_FRAME) + (pl2_i(va) * sizeof(pd_entry_t));
153 1.2.2.2 thorpej if (pread(kd->pmfd, (void *)&pde, sizeof(pde),
154 1.2.2.2 thorpej _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) {
155 1.2.2.2 thorpej _kvm_syserr(kd, 0, "could not read PT level 2 entry");
156 1.2.2.2 thorpej goto lose;
157 1.2.2.2 thorpej }
158 1.2.2.2 thorpej if ((pde & PG_V) == 0) {
159 1.2.2.2 thorpej _kvm_err(kd, 0, "invalid translation (invalid level 2 PDE)");
160 1.2.2.2 thorpej goto lose;
161 1.2.2.2 thorpej }
162 1.2.2.2 thorpej
163 1.2.2.2 thorpej
164 1.2.2.2 thorpej /*
165 1.2.2.2 thorpej * Level 1.
166 1.2.2.2 thorpej */
167 1.2.2.2 thorpej pte_pa = (pde_pa & PG_FRAME) + (pl1_i(va) * sizeof(pt_entry_t));
168 1.2.2.2 thorpej if (pread(kd->pmfd, (void *) &pte, sizeof(pte),
169 1.2.2.2 thorpej _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) {
170 1.2.2.2 thorpej _kvm_syserr(kd, 0, "could not read PTE");
171 1.2.2.2 thorpej goto lose;
172 1.2.2.2 thorpej }
173 1.2.2.2 thorpej /*
174 1.2.2.2 thorpej * Validate the PTE and return the physical address.
175 1.2.2.2 thorpej */
176 1.2.2.2 thorpej if ((pte & PG_V) == 0) {
177 1.2.2.2 thorpej _kvm_err(kd, 0, "invalid translation (invalid PTE)");
178 1.2.2.2 thorpej goto lose;
179 1.2.2.2 thorpej }
180 1.2.2.2 thorpej *pa = (pte & PG_FRAME) + page_off;
181 1.2.2.2 thorpej return (int)(NBPG - page_off);
182 1.2.2.2 thorpej
183 1.2.2.2 thorpej lose:
184 1.2.2.2 thorpej *pa = (u_long)~0L;
185 1.2.2.2 thorpej return (0);
186 1.2.2.2 thorpej }
187 1.2.2.2 thorpej
188 1.2.2.2 thorpej /*
189 1.2.2.2 thorpej * Translate a physical address to a file-offset in the crash dump.
190 1.2.2.2 thorpej */
191 1.2.2.2 thorpej off_t
192 1.2.2.2 thorpej _kvm_pa2off(kd, pa)
193 1.2.2.2 thorpej kvm_t *kd;
194 1.2.2.2 thorpej u_long pa;
195 1.2.2.2 thorpej {
196 1.2.2.2 thorpej cpu_kcore_hdr_t *cpu_kh;
197 1.2.2.2 thorpej phys_ram_seg_t *ramsegs;
198 1.2.2.2 thorpej off_t off;
199 1.2.2.2 thorpej int i;
200 1.2.2.2 thorpej
201 1.2.2.2 thorpej cpu_kh = kd->cpu_data;
202 1.2.2.2 thorpej ramsegs = (void *)((char *)(void *)cpu_kh + ALIGN(sizeof *cpu_kh));
203 1.2.2.2 thorpej
204 1.2.2.2 thorpej off = 0;
205 1.2.2.2 thorpej for (i = 0; i < cpu_kh->nmemsegs; i++) {
206 1.2.2.2 thorpej if (pa >= ramsegs[i].start &&
207 1.2.2.2 thorpej (pa - ramsegs[i].start) < ramsegs[i].size) {
208 1.2.2.2 thorpej off += (pa - ramsegs[i].start);
209 1.2.2.2 thorpej break;
210 1.2.2.2 thorpej }
211 1.2.2.2 thorpej off += ramsegs[i].size;
212 1.2.2.2 thorpej }
213 1.2.2.2 thorpej
214 1.2.2.2 thorpej return (kd->dump_off + off);
215 1.2.2.2 thorpej }
216 1.2.2.2 thorpej
217 1.2.2.2 thorpej /*
218 1.2.2.2 thorpej * Machine-dependent initialization for ALL open kvm descriptors,
219 1.2.2.2 thorpej * not just those for a kernel crash dump. Some architectures
220 1.2.2.2 thorpej * have to deal with these NOT being constants! (i.e. m68k)
221 1.2.2.2 thorpej */
222 1.2.2.2 thorpej int
223 1.2.2.2 thorpej _kvm_mdopen(kd)
224 1.2.2.2 thorpej kvm_t *kd;
225 1.2.2.2 thorpej {
226 1.2.2.2 thorpej
227 1.2.2.2 thorpej kd->usrstack = USRSTACK;
228 1.2.2.2 thorpej kd->min_uva = VM_MIN_ADDRESS;
229 1.2.2.2 thorpej kd->max_uva = VM_MAXUSER_ADDRESS;
230 1.2.2.2 thorpej
231 1.2.2.2 thorpej return (0);
232 1.2.2.2 thorpej }
233