xref: /netbsd/lib/libkvm/kvm_sun2.c (revision bf9ec67e)
1 /*	$NetBSD: kvm_sun2.c,v 1.1 2001/05/21 14:56:30 fredette 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_sun2.c,v 1.1 2001/05/21 14:56:30 fredette Exp $");
46 #endif
47 #endif /* LIBC_SCCS and not lint */
48 
49 /*
50  * Sun2 machine dependent routines for kvm.
51  *
52  * Note: This file has to build on ALL m68000 machines,
53  * so do NOT include any <machine / *.h> files here.
54  */
55 
56 #include <sys/types.h>
57 #include <sys/kcore.h>
58 
59 #include <unistd.h>
60 #include <limits.h>
61 #include <nlist.h>
62 #include <kvm.h>
63 #include <db.h>
64 
65 #include <m68k/kcore.h>
66 
67 #include "kvm_private.h"
68 #include "kvm_m68k.h"
69 
70 int   _kvm_sun2_initvtop __P((kvm_t *));
71 void  _kvm_sun2_freevtop __P((kvm_t *));
72 int	  _kvm_sun2_kvatop   __P((kvm_t *, u_long, u_long *));
73 off_t _kvm_sun2_pa2off   __P((kvm_t *, u_long));
74 
75 struct kvm_ops _kvm_ops_sun2 = {
76 	_kvm_sun2_initvtop,
77 	_kvm_sun2_freevtop,
78 	_kvm_sun2_kvatop,
79 	_kvm_sun2_pa2off };
80 
81 #define	_kvm_pg_pa(v, s, pte)	\
82 	(((pte) & (s)->pg_frame) << (v)->pgshift)
83 
84 #define	_kvm_va_segnum(s, x)	\
85 	((u_int)(x) >> (s)->segshift)
86 #define	_kvm_pte_num_mask(v)	\
87 	(0xf << (v)->pgshift)
88 #define	_kvm_va_pte_num(v, va)	\
89 	(((va) & _kvm_pte_num_mask((v))) >> (v)->pgshift)
90 
91 /*
92  * XXX Re-define these here, no other place for them.
93  */
94 #define	NKSEG		512	/* kernel segmap entries */
95 #define	NPAGSEG		16	/* pages per segment */
96 
97 /* Finally, our local stuff... */
98 struct private_vmstate {
99 	/* Page Map Entry Group (PMEG) */
100 	int   pmeg[NKSEG][NPAGSEG];
101 };
102 
103 /*
104  * Prepare for translation of kernel virtual addresses into offsets
105  * into crash dump files. We use the MMU specific goop written at the
106  * beginning of a crash dump by dumpsys()
107  * Note: sun2 MMU specific!
108  */
109 int
110 _kvm_sun2_initvtop(kd)
111 	kvm_t *kd;
112 {
113 	cpu_kcore_hdr_t *h = kd->cpu_data;
114 	char *p;
115 
116 	p = kd->cpu_data;
117 	p += (h->page_size - sizeof(kcore_seg_t));
118 	kd->vmst->private = p;
119 
120 	return (0);
121 }
122 
123 void
124 _kvm_sun2_freevtop(kd)
125 	kvm_t *kd;
126 {
127 	/* This was set by pointer arithmetic, not allocation. */
128 	kd->vmst->private = (void*)0;
129 }
130 
131 /*
132  * Translate a kernel virtual address to a physical address using the
133  * mapping information in kd->vm.  Returns the result in pa, and returns
134  * the number of bytes that are contiguously available from this
135  * physical address.  This routine is used only for crashdumps.
136  */
137 int
138 _kvm_sun2_kvatop(kd, va, pap)
139 	kvm_t *kd;
140 	u_long va;
141 	u_long *pap;
142 {
143 	cpu_kcore_hdr_t *h = kd->cpu_data;
144 	struct sun2_kcore_hdr *s = &h->un._sun2;
145 	struct vmstate *v = kd->vmst;
146 	struct private_vmstate *pv = v->private;
147 	int pte, offset;
148 	u_int segnum, sme, ptenum;
149 	u_long pa;
150 
151 	if (ISALIVE(kd)) {
152 		_kvm_err(kd, 0, "vatop called in live kernel!");
153 		return(0);
154 	}
155 
156 	if (va < h->kernbase) {
157 		_kvm_err(kd, 0, "not a kernel address");
158 		return(0);
159 	}
160 
161 	/*
162 	 * Get the segmap entry (sme) from the kernel segmap.
163 	 * Note: only have segmap entries from KERNBASE to end.
164 	 */
165 	segnum = _kvm_va_segnum(s, va - h->kernbase);
166 	ptenum = _kvm_va_pte_num(v, va);
167 	offset = va & v->pgofset;
168 
169 	/* The segmap entry selects a PMEG. */
170 	sme = s->ksegmap[segnum];
171 	pte = pv->pmeg[sme][ptenum];
172 
173 	if ((pte & (s)->pg_valid) == 0) {
174 		_kvm_err(kd, 0, "page not valid (VA=0x%lx)", va);
175 		return (0);
176 	}
177 	pa = _kvm_pg_pa(v, s, pte) + offset;
178 
179 	*pap = pa;
180 	return (h->page_size - offset);
181 }
182 
183 /*
184  * Translate a physical address to a file-offset in the crash-dump.
185  */
186 off_t
187 _kvm_sun2_pa2off(kd, pa)
188 	kvm_t	*kd;
189 	u_long	pa;
190 {
191 	return(kd->dump_off + pa);
192 }
193