kvm_sparc64.c revision 1.14 1 /* $NetBSD: kvm_sparc64.c,v 1.14 2010/09/19 02:07:00 jym Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software developed by the Computer Systems
8 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
9 * BG 91-66 and contributed to Berkeley.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: kvm_sparc64.c,v 1.14 2010/09/19 02:07:00 jym Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 /*
46 * Sparc 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/exec.h>
52 #include <sys/user.h>
53 #include <sys/proc.h>
54 #include <sys/stat.h>
55 #include <sys/core.h>
56 #include <sys/kcore.h>
57 #include <unistd.h>
58 #include <nlist.h>
59 #include <kvm.h>
60
61 #include <uvm/uvm_extern.h>
62
63 #include <machine/pmap.h>
64 #include <machine/kcore.h>
65 #include <machine/vmparam.h>
66
67 #include <limits.h>
68 #include <db.h>
69
70 #include "kvm_private.h"
71
72 void
73 _kvm_freevtop(kvm_t *kd)
74 {
75 if (kd->vmst != 0) {
76 _kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
77 kd->vmst = 0;
78 }
79 }
80
81 /*
82 * Prepare for translation of kernel virtual addresses into offsets
83 * into crash dump files. We use the MMU specific goop written at the
84 * front of the crash dump by pmap_dumpmmu().
85 *
86 * We should read in and cache the ksegs here to speed up operations...
87 */
88 int
89 _kvm_initvtop(kvm_t *kd)
90 {
91 kd->nbpg = 0x2000;
92
93 return (0);
94 }
95
96 /*
97 * Translate a kernel virtual address to a physical address using the
98 * mapping information in kd->vm. Returns the result in pa, and returns
99 * the number of bytes that are contiguously available from this
100 * physical address. This routine is used only for crash dumps.
101 */
102 int
103 _kvm_kvatop(kvm_t *kd, u_long va, u_long *pa)
104 {
105 cpu_kcore_hdr_t *cpup = kd->cpu_data;
106 u_long kernbase = cpup->kernbase;
107 uint64_t *pseg, *pdir, *ptbl;
108 struct cpu_kcore_4mbseg *ktlb;
109 int64_t data;
110 int i;
111
112 if (va < kernbase)
113 goto lose;
114
115 /* Handle the wired 4MB TTEs and per-CPU mappings */
116 if (cpup->memsegoffset > sizeof(cpu_kcore_hdr_t) &&
117 cpup->newmagic == SPARC64_KCORE_NEWMAGIC) {
118 /*
119 * new format: we have a list of 4 MB mappings
120 */
121 ktlb = (struct cpu_kcore_4mbseg *)
122 ((uintptr_t)kd->cpu_data + cpup->off4mbsegs);
123 for (i = 0; i < cpup->num4mbsegs; i++) {
124 uint64_t start = ktlb[i].va;
125 if (va < start || va >= start+PAGE_SIZE_4M)
126 continue;
127 *pa = ktlb[i].pa + va - start;
128 return (int)(start+PAGE_SIZE_4M - va);
129 }
130
131 if (cpup->numcpuinfos > 0) {
132 /* we have per-CPU mapping info */
133 uint64_t start, base;
134
135 base = cpup->cpubase - 32*1024;
136 if (va >= base && va < (base + cpup->percpusz)) {
137 start = va - base;
138 *pa = cpup->cpusp
139 + cpup->thiscpu*cpup->percpusz
140 + start;
141 return cpup->percpusz - start;
142 }
143 }
144 } else {
145 /*
146 * old format: just a textbase/size and database/size
147 */
148 if (va > cpup->ktextbase && va <
149 (cpup->ktextbase + cpup->ktextsz)) {
150 u_long vaddr;
151
152 vaddr = va - cpup->ktextbase;
153 *pa = cpup->ktextp + vaddr;
154 return (int)(cpup->ktextsz - vaddr);
155 }
156 if (va > cpup->kdatabase && va <
157 (cpup->kdatabase + cpup->kdatasz)) {
158 u_long vaddr;
159
160 vaddr = va - cpup->kdatabase;
161 *pa = cpup->kdatap + vaddr;
162 return (int)(cpup->kdatasz - vaddr);
163 }
164 }
165
166 /*
167 * Parse kernel page table.
168 */
169 pseg = (uint64_t *)(u_long)cpup->segmapoffset;
170 if (_kvm_pread(kd, kd->pmfd, &pdir, sizeof(pdir),
171 _kvm_pa2off(kd, (u_long)&pseg[va_to_seg(va)]))
172 != sizeof(pdir)) {
173 _kvm_syserr(kd, 0, "could not read L1 PTE");
174 goto lose;
175 }
176
177 if (!pdir) {
178 _kvm_err(kd, 0, "invalid L1 PTE");
179 goto lose;
180 }
181
182 if (_kvm_pread(kd, kd->pmfd, &ptbl, sizeof(ptbl),
183 _kvm_pa2off(kd, (u_long)&pdir[va_to_dir(va)]))
184 != sizeof(ptbl)) {
185 _kvm_syserr(kd, 0, "could not read L2 PTE");
186 goto lose;
187 }
188
189 if (!ptbl) {
190 _kvm_err(kd, 0, "invalid L2 PTE");
191 goto lose;
192 }
193
194 if (_kvm_pread(kd, kd->pmfd, &data, sizeof(data),
195 _kvm_pa2off(kd, (u_long)&ptbl[va_to_pte(va)]))
196 != sizeof(data)) {
197 _kvm_syserr(kd, 0, "could not read TTE");
198 goto lose;
199 }
200
201 if (data >= 0) {
202 _kvm_err(kd, 0, "invalid L2 TTE");
203 goto lose;
204 }
205
206 /*
207 * Calculate page offsets and things.
208 *
209 * XXXX -- We could support multiple page sizes.
210 */
211 va = va & (kd->nbpg - 1);
212 data &= TLB_PA_MASK;
213 *pa = data + va;
214
215 /*
216 * Parse and trnslate our TTE.
217 */
218
219 return (int)(kd->nbpg - va);
220
221 lose:
222 *pa = (u_long)-1;
223 _kvm_err(kd, 0, "invalid address (%lx)", va);
224 return (0);
225 }
226
227
228 /*
229 * Translate a physical address to a file-offset in the crash dump.
230 */
231 off_t
232 _kvm_pa2off(kvm_t *kd, u_long pa)
233 {
234 cpu_kcore_hdr_t *cpup = kd->cpu_data;
235 phys_ram_seg_t *mp;
236 off_t off;
237 int nmem;
238
239 /*
240 * Layout of CPU segment:
241 * cpu_kcore_hdr_t;
242 * [alignment]
243 * phys_ram_seg_t[cpup->nmemseg];
244 */
245 mp = (phys_ram_seg_t *)((long)kd->cpu_data + cpup->memsegoffset);
246 off = 0;
247
248 /* Translate (sparse) pfnum to (packed) dump offset */
249 for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
250 if (mp->start <= pa && pa < mp->start + mp->size)
251 break;
252 off += mp->size;
253 }
254 if (nmem < 0) {
255 _kvm_err(kd, 0, "invalid address (%lx)", pa);
256 return (-1);
257 }
258
259 return (kd->dump_off + off + pa - mp->start);
260 }
261
262 /*
263 * Machine-dependent initialization for ALL open kvm descriptors,
264 * not just those for a kernel crash dump. Some architectures
265 * have to deal with these NOT being constants! (i.e. m68k)
266 */
267 int
268 _kvm_mdopen(kvm_t *kd)
269 {
270 u_long max_uva;
271 extern struct ps_strings *__ps_strings;
272
273 max_uva = (u_long) (__ps_strings + 1);
274 kd->usrstack = max_uva;
275 kd->max_uva = max_uva;
276 kd->min_uva = 0;
277
278 return (0);
279 }
280