xref: /minix/lib/libkvm/kvm_sparc64.c (revision b89261ba)
1 /*	$NetBSD: kvm_sparc64.c,v 1.17 2014/02/21 18:00:09 palle 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.17 2014/02/21 18:00:09 palle 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/proc.h>
53 #include <sys/stat.h>
54 #include <sys/core.h>
55 #include <sys/kcore.h>
56 #include <sys/types.h>
57 
58 #include <unistd.h>
59 #include <nlist.h>
60 #include <kvm.h>
61 
62 #include <uvm/uvm_extern.h>
63 
64 #include <machine/pmap.h>
65 #include <machine/kcore.h>
66 #include <machine/vmparam.h>
67 #include <machine/param.h>
68 
69 #include <limits.h>
70 #include <db.h>
71 
72 #include "kvm_private.h"
73 
74 void
75 _kvm_freevtop(kvm_t *kd)
76 {
77 	if (kd->vmst != 0) {
78 		_kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
79 		kd->vmst = 0;
80 	}
81 }
82 
83 /*
84  * Prepare for translation of kernel virtual addresses into offsets
85  * into crash dump files. We use the MMU specific goop written at the
86  * front of the crash dump by pmap_dumpmmu().
87  *
88  * We should read in and cache the ksegs here to speed up operations...
89  */
90 int
91 _kvm_initvtop(kvm_t *kd)
92 {
93 	kd->nbpg = 0x2000;
94 
95 	return (0);
96 }
97 
98 /*
99  * Translate a kernel virtual address to a physical address using the
100  * mapping information in kd->vm.  Returns the result in pa, and returns
101  * the number of bytes that are contiguously available from this
102  * physical address.  This routine is used only for crash dumps.
103  */
104 int
105 _kvm_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pa)
106 {
107 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
108 	u_long kernbase = cpup->kernbase;
109 	uint64_t *pseg, *pdir, *ptbl;
110 	struct cpu_kcore_4mbseg *ktlb;
111 	int64_t data;
112 	int i;
113 
114 	if (va < kernbase)
115 		goto lose;
116 
117 	/* Handle the wired 4MB TTEs and per-CPU mappings */
118 	if (cpup->memsegoffset > sizeof(cpu_kcore_hdr_t) &&
119 	    cpup->newmagic == SPARC64_KCORE_NEWMAGIC) {
120 		/*
121 		 * new format: we have a list of 4 MB mappings
122 		 */
123 		ktlb = (struct cpu_kcore_4mbseg *)
124 			((uintptr_t)kd->cpu_data + cpup->off4mbsegs);
125 		for (i = 0; i < cpup->num4mbsegs; i++) {
126 			uint64_t start = ktlb[i].va;
127 			if (va < start || va >= start+PAGE_SIZE_4M)
128 				continue;
129 			*pa = ktlb[i].pa + va - start;
130 			return (int)(start+PAGE_SIZE_4M - va);
131 		}
132 
133 		if (cpup->numcpuinfos > 0) {
134 			/* we have per-CPU mapping info */
135 			uint64_t start, base;
136 
137 			base = cpup->cpubase - 32*1024;
138 			if (va >= base && va < (base + cpup->percpusz)) {
139 				start = va - base;
140 				*pa = cpup->cpusp
141 				    + cpup->thiscpu*cpup->percpusz
142 				    + start;
143 				return cpup->percpusz - start;
144 			}
145 		}
146 	} else {
147 		/*
148 		 * old format: just a textbase/size and database/size
149 		 */
150 		if (va > cpup->ktextbase && va <
151 		    (cpup->ktextbase + cpup->ktextsz)) {
152 			u_long vaddr;
153 
154 			vaddr = va - cpup->ktextbase;
155 			*pa = cpup->ktextp + vaddr;
156 			return (int)(cpup->ktextsz - vaddr);
157 		}
158 		if (va > cpup->kdatabase && va <
159 		    (cpup->kdatabase + cpup->kdatasz)) {
160 			u_long vaddr;
161 
162 			vaddr = va - cpup->kdatabase;
163 			*pa = cpup->kdatap + vaddr;
164 			return (int)(cpup->kdatasz - vaddr);
165 		}
166 	}
167 
168 	/*
169 	 * Parse kernel page table.
170 	 */
171 	pseg = (uint64_t *)(u_long)cpup->segmapoffset;
172 	if (_kvm_pread(kd, kd->pmfd, &pdir, sizeof(pdir),
173 		_kvm_pa2off(kd, (paddr_t)&pseg[va_to_seg(va)]))
174 		!= sizeof(pdir)) {
175 		_kvm_syserr(kd, 0, "could not read L1 PTE");
176 		goto lose;
177 	}
178 
179 	if (!pdir) {
180 		_kvm_err(kd, 0, "invalid L1 PTE");
181 		goto lose;
182 	}
183 
184 	if (_kvm_pread(kd, kd->pmfd, &ptbl, sizeof(ptbl),
185 		_kvm_pa2off(kd, (paddr_t)&pdir[va_to_dir(va)]))
186 		!= sizeof(ptbl)) {
187 		_kvm_syserr(kd, 0, "could not read L2 PTE");
188 		goto lose;
189 	}
190 
191 	if (!ptbl) {
192 		_kvm_err(kd, 0, "invalid L2 PTE");
193 		goto lose;
194 	}
195 
196 	if (_kvm_pread(kd, kd->pmfd, &data, sizeof(data),
197 		_kvm_pa2off(kd, (paddr_t)&ptbl[va_to_pte(va)]))
198 		!= sizeof(data)) {
199 		_kvm_syserr(kd, 0, "could not read TTE");
200 		goto lose;
201 	}
202 
203 	if (data >= 0) {
204 		_kvm_err(kd, 0, "invalid L2 TTE");
205 		goto lose;
206 	}
207 
208 	/*
209 	 * Calculate page offsets and things.
210 	 *
211 	 * XXXX -- We could support multiple page sizes.
212 	 */
213 	va = va & (kd->nbpg - 1);
214 	data &= SUN4U_TLB_PA_MASK; /* XXX handle sun4u/sun4v */
215 	*pa = data + va;
216 
217 	/*
218 	 * Parse and trnslate our TTE.
219 	 */
220 
221 	return (int)(kd->nbpg - va);
222 
223 lose:
224 	*pa = (u_long)-1;
225 	_kvm_err(kd, 0, "invalid address (%#"PRIxVADDR")", va);
226 	return (0);
227 }
228 
229 
230 /*
231  * Translate a physical address to a file-offset in the crash dump.
232  */
233 off_t
234 _kvm_pa2off(kvm_t *kd, paddr_t pa)
235 {
236 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
237 	phys_ram_seg_t *mp;
238 	off_t off;
239 	int nmem;
240 
241 	/*
242 	 * Layout of CPU segment:
243 	 *	cpu_kcore_hdr_t;
244 	 *	[alignment]
245 	 *	phys_ram_seg_t[cpup->nmemseg];
246 	 */
247 	mp = (phys_ram_seg_t *)((long)kd->cpu_data + cpup->memsegoffset);
248 	off = 0;
249 
250 	/* Translate (sparse) pfnum to (packed) dump offset */
251 	for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
252 		if (mp->start <= pa && pa < mp->start + mp->size)
253 			break;
254 		off += mp->size;
255 	}
256 	if (nmem < 0) {
257 		_kvm_err(kd, 0, "invalid address (%#"PRIxPADDR")", pa);
258 		return (-1);
259 	}
260 
261 	return (kd->dump_off + off + pa - mp->start);
262 }
263 
264 /*
265  * Machine-dependent initialization for ALL open kvm descriptors,
266  * not just those for a kernel crash dump.  Some architectures
267  * have to deal with these NOT being constants!  (i.e. m68k)
268  */
269 int
270 _kvm_mdopen(kvm_t *kd)
271 {
272 	u_long max_uva;
273 	extern struct ps_strings *__ps_strings;
274 
275 	max_uva = (u_long) (__ps_strings + 1);
276 	kd->usrstack = max_uva;
277 	kd->max_uva  = max_uva;
278 	kd->min_uva  = 0;
279 
280 	return (0);
281 }
282