kvm_mips.c revision 1.18.16.1 1 1.18.16.1 matt /* $NetBSD: kvm_mips.c,v 1.18.16.1 2010/01/28 17:16:42 matt Exp $ */
2 1.3 thorpej
3 1.10 thorpej /*
4 1.10 thorpej * Copyright (c) 1994, 1995 Carnegie-Mellon University.
5 1.10 thorpej * All rights reserved.
6 1.1 cgd *
7 1.10 thorpej * Author: Chris G. Demetriou
8 1.1 cgd *
9 1.10 thorpej * Permission to use, copy, modify and distribute this software and
10 1.10 thorpej * its documentation is hereby granted, provided that both the copyright
11 1.10 thorpej * notice and this permission notice appear in all copies of the
12 1.10 thorpej * software, derivative works or modified versions, and any portions
13 1.10 thorpej * thereof, and that both notices appear in supporting documentation.
14 1.12 simonb *
15 1.10 thorpej * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.10 thorpej * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.10 thorpej * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.12 simonb *
19 1.12 simonb * Carnegie Mellon requests users of this software to return to
20 1.12 simonb *
21 1.10 thorpej * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.10 thorpej * School of Computer Science
23 1.10 thorpej * Carnegie Mellon University
24 1.10 thorpej * Pittsburgh PA 15213-3890
25 1.1 cgd *
26 1.10 thorpej * any improvements or extensions that they make and grant Carnegie the
27 1.10 thorpej * rights to redistribute these changes.
28 1.12 simonb */
29 1.10 thorpej
30 1.10 thorpej /*
31 1.10 thorpej * Modified for NetBSD/mips by Jason R. Thorpe, Numerical Aerospace
32 1.10 thorpej * Simulation Facility, NASA Ames Research Center.
33 1.1 cgd */
34 1.1 cgd
35 1.7 mikel #include <sys/cdefs.h>
36 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
37 1.18.16.1 matt __RCSID("$NetBSD: kvm_mips.c,v 1.18.16.1 2010/01/28 17:16:42 matt Exp $");
38 1.1 cgd #endif /* LIBC_SCCS and not lint */
39 1.3 thorpej
40 1.1 cgd /*
41 1.10 thorpej * MIPS machine dependent routines for kvm.
42 1.1 cgd */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.1 cgd #include <sys/user.h>
46 1.1 cgd #include <sys/proc.h>
47 1.1 cgd #include <sys/stat.h>
48 1.10 thorpej #include <sys/kcore.h>
49 1.10 thorpej #include <machine/kcore.h>
50 1.8 jonathan #include <stdlib.h>
51 1.1 cgd #include <unistd.h>
52 1.1 cgd #include <nlist.h>
53 1.1 cgd #include <kvm.h>
54 1.1 cgd
55 1.15 mrg #include <uvm/uvm_extern.h>
56 1.1 cgd
57 1.1 cgd #include <limits.h>
58 1.1 cgd #include <db.h>
59 1.1 cgd
60 1.1 cgd #include "kvm_private.h"
61 1.1 cgd
62 1.4 jonathan #include <mips/cpuregs.h>
63 1.16 matt #include <mips/vmparam.h>
64 1.1 cgd
65 1.1 cgd void
66 1.1 cgd _kvm_freevtop(kd)
67 1.1 cgd kvm_t *kd;
68 1.1 cgd {
69 1.10 thorpej
70 1.10 thorpej /* Not actually used for anything right now, but safe. */
71 1.1 cgd if (kd->vmst != 0)
72 1.1 cgd free(kd->vmst);
73 1.1 cgd }
74 1.1 cgd
75 1.1 cgd int
76 1.1 cgd _kvm_initvtop(kd)
77 1.1 cgd kvm_t *kd;
78 1.1 cgd {
79 1.1 cgd
80 1.1 cgd return (0);
81 1.1 cgd }
82 1.1 cgd
83 1.1 cgd /*
84 1.1 cgd * Translate a kernel virtual address to a physical address.
85 1.1 cgd */
86 1.1 cgd int
87 1.1 cgd _kvm_kvatop(kd, va, pa)
88 1.1 cgd kvm_t *kd;
89 1.1 cgd u_long va;
90 1.1 cgd u_long *pa;
91 1.1 cgd {
92 1.10 thorpej cpu_kcore_hdr_t *cpu_kh;
93 1.10 thorpej int page_off;
94 1.10 thorpej u_int pte;
95 1.10 thorpej u_long pte_pa;
96 1.4 jonathan
97 1.1 cgd if (ISALIVE(kd)) {
98 1.1 cgd _kvm_err(kd, 0, "vatop called in live kernel!");
99 1.1 cgd return((off_t)0);
100 1.1 cgd }
101 1.4 jonathan
102 1.10 thorpej cpu_kh = kd->cpu_data;
103 1.10 thorpej page_off = va & PGOFSET;
104 1.10 thorpej
105 1.18.16.1 matt #ifdef _LP64
106 1.18.16.1 matt if (MIPS_XKPHYS_P(va)) {
107 1.18.16.1 matt /*
108 1.18.16.1 matt * Direct-mapped cached address: just convert it.
109 1.18.16.1 matt */
110 1.18.16.1 matt *pa = MIPS_XKPHYS_TO_PHYS(va);
111 1.18.16.1 matt return (NBPG - page_off);
112 1.18.16.1 matt }
113 1.18.16.1 matt
114 1.18.16.1 matt if (va < MIPS_XKPHYS_START) {
115 1.18.16.1 matt /*
116 1.18.16.1 matt * XUSEG (user virtual address space) - invalid.
117 1.18.16.1 matt */
118 1.18.16.1 matt _kvm_err(kd, 0, "invalid kernel virtual address");
119 1.18.16.1 matt goto lose;
120 1.18.16.1 matt }
121 1.18.16.1 matt #else
122 1.10 thorpej if (va < MIPS_KSEG0_START) {
123 1.10 thorpej /*
124 1.10 thorpej * KUSEG (user virtual address space) - invalid.
125 1.10 thorpej */
126 1.10 thorpej _kvm_err(kd, 0, "invalid kernel virtual address");
127 1.10 thorpej goto lose;
128 1.10 thorpej }
129 1.18.16.1 matt #endif
130 1.10 thorpej
131 1.18.16.1 matt if (MIPS_KSEG0_P(va)) {
132 1.10 thorpej /*
133 1.10 thorpej * Direct-mapped cached address: just convert it.
134 1.10 thorpej */
135 1.10 thorpej *pa = MIPS_KSEG0_TO_PHYS(va);
136 1.10 thorpej return (NBPG - page_off);
137 1.10 thorpej }
138 1.10 thorpej
139 1.18.16.1 matt if (MIPS_KSEG1_P(va)) {
140 1.10 thorpej /*
141 1.10 thorpej * Direct-mapped uncached address: just convert it.
142 1.10 thorpej */
143 1.10 thorpej *pa = MIPS_KSEG1_TO_PHYS(va);
144 1.10 thorpej return (NBPG - page_off);
145 1.10 thorpej }
146 1.10 thorpej
147 1.18.16.1 matt #ifdef _LP64
148 1.18.16.1 matt if (va >= MIPS_KSEG2_START) {
149 1.18.16.1 matt /*
150 1.18.16.1 matt * KUSEG (user virtual address space) - invalid.
151 1.18.16.1 matt */
152 1.18.16.1 matt _kvm_err(kd, 0, "invalid kernel virtual address");
153 1.18.16.1 matt goto lose;
154 1.18.16.1 matt }
155 1.18.16.1 matt #endif
156 1.18.16.1 matt
157 1.10 thorpej /*
158 1.10 thorpej * We now know that we're a KSEG2 (kernel virtually mapped)
159 1.10 thorpej * address. Translate the address using the pmap's kernel
160 1.10 thorpej * page table.
161 1.10 thorpej */
162 1.4 jonathan
163 1.10 thorpej /*
164 1.10 thorpej * Step 1: Make sure the kernel page table has a translation
165 1.10 thorpej * for the address.
166 1.10 thorpej */
167 1.18.16.1 matt #ifdef _LP64
168 1.18.16.1 matt if (va >= (MIPS_XKSEG_START + (cpu_kh->sysmapsize * NBPG))) {
169 1.18.16.1 matt _kvm_err(kd, 0, "invalid XKSEG address");
170 1.18.16.1 matt goto lose;
171 1.18.16.1 matt }
172 1.18.16.1 matt #else
173 1.10 thorpej if (va >= (MIPS_KSEG2_START + (cpu_kh->sysmapsize * NBPG))) {
174 1.10 thorpej _kvm_err(kd, 0, "invalid KSEG2 address");
175 1.10 thorpej goto lose;
176 1.4 jonathan }
177 1.18.16.1 matt #endif
178 1.10 thorpej
179 1.1 cgd /*
180 1.10 thorpej * Step 2: Locate and read the PTE.
181 1.1 cgd */
182 1.10 thorpej pte_pa = cpu_kh->sysmappa +
183 1.10 thorpej (((va - MIPS_KSEG2_START) >> PGSHIFT) * sizeof(u_int));
184 1.18 ad if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
185 1.18 ad _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) {
186 1.10 thorpej _kvm_syserr(kd, 0, "could not read PTE");
187 1.10 thorpej goto lose;
188 1.1 cgd }
189 1.4 jonathan
190 1.1 cgd /*
191 1.10 thorpej * Step 3: Validate the PTE and return the physical address.
192 1.1 cgd */
193 1.10 thorpej if ((pte & cpu_kh->pg_v) == 0) {
194 1.10 thorpej _kvm_err(kd, 0, "invalid translation (invalid PTE)");
195 1.10 thorpej goto lose;
196 1.10 thorpej }
197 1.10 thorpej *pa = (((pte & cpu_kh->pg_frame) >> cpu_kh->pg_shift) << PGSHIFT) +
198 1.10 thorpej page_off;
199 1.10 thorpej return (NBPG - page_off);
200 1.1 cgd
201 1.10 thorpej lose:
202 1.10 thorpej *pa = -1;
203 1.6 gwr return (0);
204 1.10 thorpej }
205 1.10 thorpej
206 1.10 thorpej /*
207 1.17 wiz * Translate a physical address to a file-offset in the crash dump.
208 1.10 thorpej */
209 1.10 thorpej off_t
210 1.10 thorpej _kvm_pa2off(kd, pa)
211 1.10 thorpej kvm_t *kd;
212 1.10 thorpej u_long pa;
213 1.10 thorpej {
214 1.10 thorpej cpu_kcore_hdr_t *cpu_kh;
215 1.10 thorpej phys_ram_seg_t *ramsegs;
216 1.10 thorpej off_t off;
217 1.10 thorpej int i;
218 1.10 thorpej
219 1.10 thorpej cpu_kh = kd->cpu_data;
220 1.10 thorpej ramsegs = (phys_ram_seg_t *)((char *)cpu_kh + ALIGN(sizeof *cpu_kh));
221 1.10 thorpej
222 1.10 thorpej off = 0;
223 1.10 thorpej for (i = 0; i < cpu_kh->nmemsegs; i++) {
224 1.10 thorpej if (pa >= ramsegs[i].start &&
225 1.10 thorpej (pa - ramsegs[i].start) < ramsegs[i].size) {
226 1.10 thorpej off += (pa - ramsegs[i].start);
227 1.10 thorpej break;
228 1.10 thorpej }
229 1.10 thorpej off += ramsegs[i].size;
230 1.10 thorpej }
231 1.10 thorpej
232 1.10 thorpej return (kd->dump_off + off);
233 1.6 gwr }
234 1.6 gwr
235 1.6 gwr /*
236 1.6 gwr * Machine-dependent initialization for ALL open kvm descriptors,
237 1.6 gwr * not just those for a kernel crash dump. Some architectures
238 1.6 gwr * have to deal with these NOT being constants! (i.e. m68k)
239 1.6 gwr */
240 1.6 gwr int
241 1.6 gwr _kvm_mdopen(kd)
242 1.6 gwr kvm_t *kd;
243 1.6 gwr {
244 1.6 gwr
245 1.6 gwr kd->usrstack = USRSTACK;
246 1.6 gwr kd->min_uva = VM_MIN_ADDRESS;
247 1.6 gwr kd->max_uva = VM_MAXUSER_ADDRESS;
248 1.6 gwr
249 1.1 cgd return (0);
250 1.1 cgd }
251