xref: /netbsd-src/lib/libkvm/kvm_sparc64.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: kvm_sparc64.c,v 1.4 2000/06/29 06:34:26 mrg 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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 #if defined(LIBC_SCCS) && !defined(lint)
42 #if 0
43 static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
44 #else
45 __RCSID("$NetBSD: kvm_sparc64.c,v 1.4 2000/06/29 06:34:26 mrg Exp $");
46 #endif
47 #endif /* LIBC_SCCS and not lint */
48 
49 /*
50  * Sparc machine dependent routines for kvm.  Hopefully, the forthcoming
51  * vm code will one day obsolete this module.
52  */
53 
54 #include <sys/param.h>
55 #include <sys/exec.h>
56 #include <sys/user.h>
57 #include <sys/proc.h>
58 #include <sys/stat.h>
59 #include <sys/core.h>
60 #include <sys/kcore.h>
61 #include <unistd.h>
62 #include <nlist.h>
63 #include <kvm.h>
64 
65 #include <uvm/uvm_extern.h>
66 
67 #include <machine/kcore.h>
68 
69 #include <limits.h>
70 #include <db.h>
71 
72 #include "kvm_private.h"
73 
74 int _kvm_kvatop __P((kvm_t *, u_long, u_long *));
75 
76 void
77 _kvm_freevtop(kd)
78 	kvm_t *kd;
79 {
80 	if (kd->vmst != 0) {
81 		_kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
82 		kd->vmst = 0;
83 	}
84 }
85 
86 /*
87  * Prepare for translation of kernel virtual addresses into offsets
88  * into crash dump files. We use the MMU specific goop written at the
89  * front of the crash dump by pmap_dumpmmu().
90  */
91 int
92 _kvm_initvtop(kd)
93 	kvm_t *kd;
94 {
95 	kd->nbpg = 8196;
96 	return (0);
97 }
98 
99 /*
100  * Translate a kernel virtual address to a physical address using the
101  * mapping information in kd->vm.  Returns the result in pa, and returns
102  * the number of bytes that are contiguously available from this
103  * physical address.  This routine is used only for crashdumps.
104  */
105 int
106 _kvm_kvatop(kd, va, pa)
107 	kvm_t *kd;
108 	u_long va;
109 	u_long *pa;
110 {
111 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
112 	u_long kernbase = cpup->kernbase;
113 
114 	if (va < kernbase)
115 		goto err;
116 
117 	/* Handle the wired 4MB TTE */
118 	if (va > cpup->kernbase && va < cpup->kernbase + 4*1024*1024) {
119 		u_long vaddr;
120 
121 		vaddr = va - cpup->kernbase;
122 		*pa = cpup->kphys + va;
123 		return (4*1024*1024 - va);
124 	}
125 #if 0
126 	/*
127 	 * Layout of CPU segment:
128 	 *	cpu_kcore_hdr_t;
129 	 *	[alignment]
130 	 *	phys_ram_seg_t[cpup->nmemseg];
131 	 *	segmap[cpup->nsegmap];
132 	 *	ptes[cpup->npmegs];
133 	 */
134 	segmaps = (struct segmap *)((long)kd->cpu_data + cpup->segmapoffset);
135 	ptes = (int *)((int)kd->cpu_data + cpup->pmegoffset);
136 	nkreg = ((int)((-(unsigned)kernbase) / NBPRG));
137 	nureg = 256 - nkreg;
138 
139 	vr = VA_VREG(va);
140 	vs = VA_VSEG(va);
141 
142 	sp = &segmaps[(vr-nureg)*NSEGRG + vs];
143 	if (sp->sg_npte == 0)
144 		goto err;
145 	if (sp->sg_pmeg == cpup->npmeg - 1) /* =seginval */
146 		goto err;
147 	pte = ptes[sp->sg_pmeg * nptesg + VA_VPG(va)];
148 	if ((pte & PG_V) != 0) {
149 		long p, off = VA_OFF(va);
150 
151 		p = (pte & PG_PFNUM) << pgshift;
152 		*pa = p + off;
153 		return (kd->nbpg - off);
154 	}
155 #endif
156 err:
157 	_kvm_err(kd, 0, "invalid address (%x)", va);
158 	return (0);
159 }
160 
161 
162 /*
163  * Translate a physical address to a file-offset in the crash-dump.
164  */
165 off_t
166 _kvm_pa2off(kd, pa)
167 	kvm_t   *kd;
168 	u_long  pa;
169 {
170 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
171 	phys_ram_seg_t *mp;
172 	off_t off;
173 	int nmem;
174 
175 	/*
176 	 * Layout of CPU segment:
177 	 *	cpu_kcore_hdr_t;
178 	 *	[alignment]
179 	 *	phys_ram_seg_t[cpup->nmemseg];
180 	 */
181 	mp = (phys_ram_seg_t *)((long)kd->cpu_data + cpup->memsegoffset);
182 	off = 0;
183 
184 	/* Translate (sparse) pfnum to (packed) dump offset */
185 	for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
186 		if (mp->start <= pa && pa < mp->start + mp->size)
187 			break;
188 		off += mp->size;
189 	}
190 	if (nmem < 0) {
191 		_kvm_err(kd, 0, "invalid address (%x)", pa);
192 		return (-1);
193 	}
194 
195 	return (kd->dump_off + off + pa - mp->start);
196 }
197 
198 /*
199  * Machine-dependent initialization for ALL open kvm descriptors,
200  * not just those for a kernel crash dump.  Some architectures
201  * have to deal with these NOT being constants!  (i.e. m68k)
202  */
203 int
204 _kvm_mdopen(kd)
205 	kvm_t	*kd;
206 {
207 	u_long max_uva;
208 	extern struct ps_strings *__ps_strings;
209 
210 	max_uva = (u_long) (__ps_strings + 1);
211 	kd->usrstack = max_uva;
212 	kd->max_uva  = max_uva;
213 	kd->min_uva  = 0;
214 
215 	return (0);
216 }
217