1 /* $NetBSD: kvm_sparc.c,v 1.34 2015/10/07 11:56:41 martin Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid
[] = "@(#)kvm_sparc.c 8.1 (Berkeley) 6/4/93";
41 __RCSID("$NetBSD: kvm_sparc.c,v 1.34 2015/10/07 11:56:41 martin Exp $");
43 #endif /* LIBC_SCCS and not lint */
46 * Sparc machine dependent routines for kvm. Hopefully, the forthcoming
47 * vm code will one day obsolete this module.
50 #include <sys/param.h>
55 #include <sys/kcore.h>
60 #include <uvm/uvm_extern.h>
62 #include <sparc/pmap.h>
63 #include <sparc/kcore.h>
68 #include "kvm_private.h"
71 static int cputyp
= -1;
73 static int nptesg
; /* [sun4/sun4c] only */
76 #define VA_VPG(va) ((cputyp == CPU_SUN4C || cputyp == CPU_SUN4M) \
81 #define VA_OFF(va) (va & (kd->nbpg - 1))
83 int _kvm_kvatop44c(kvm_t
*, vaddr_t
, paddr_t
*);
84 int _kvm_kvatop4m (kvm_t
*, vaddr_t
, paddr_t
*);
85 int _kvm_kvatop4u (kvm_t
*, vaddr_t
, paddr_t
*);
89 * taken from /sys/arch/sparc64/include/kcore.h.
90 * this is the same as the sparc one, except for the kphys addition,
91 * so luckily we can use this here...
93 typedef struct sparc64_cpu_kcore_hdr
{
94 int cputype
; /* CPU type associated with this dump */
95 u_long kernbase
; /* copy of KERNBASE goes here */
96 int nmemseg
; /* # of physical memory segments */
97 u_long memsegoffset
; /* start of memseg array (relative */
98 /* to the start of this header) */
99 int nsegmap
; /* # of segmaps following */
100 u_long segmapoffset
; /* start of segmap array (relative */
101 /* to the start of this header) */
102 int npmeg
; /* # of PMEGs; [sun4/sun4c] only */
103 u_long pmegoffset
; /* start of pmeg array (relative */
104 /* to the start of this header) */
106 paddr_t kphys
; /* Physical address of 4MB locked TLB */
107 } sparc64_cpu_kcore_hdr_t
;
110 _kvm_freevtop(kvm_t
*kd
)
113 _kvm_err(kd
, kd
->program
, "_kvm_freevtop: internal error");
119 * Prepare for translation of kernel virtual addresses into offsets
120 * into crash dump files. We use the MMU specific goop written at the
121 * front of the crash dump by pmap_dumpmmu().
124 _kvm_initvtop(kvm_t
*kd
)
126 sparc64_cpu_kcore_hdr_t
*cpup
= kd
->cpu_data
;
128 switch (cputyp
= cpup
->cputype
) {
140 _kvm_err(kd
, kd
->program
, "Unsupported CPU type");
143 nptesg
= NBPSG
/ kd
->nbpg
;
148 * Translate a kernel virtual address to a physical address using the
149 * mapping information in kd->vm. Returns the result in pa, and returns
150 * the number of bytes that are contiguously available from this
151 * physical address. This routine is used only for crash dumps.
154 _kvm_kvatop(kvm_t
*kd
, vaddr_t va
, paddr_t
*pa
)
157 if (_kvm_initvtop(kd
) != 0)
163 return _kvm_kvatop44c(kd
, va
, pa
);
166 return _kvm_kvatop4m(kd
, va
, pa
);
170 return _kvm_kvatop4u(kd
, va
, pa
);
175 * (note: sun4 3-level MMU not yet supported)
178 _kvm_kvatop44c(kvm_t
*kd
, vaddr_t va
, paddr_t
*pa
)
181 sparc64_cpu_kcore_hdr_t
*cpup
= kd
->cpu_data
;
182 struct segmap
*sp
, *segmaps
;
185 u_long kernbase
= cpup
->kernbase
;
191 * Layout of CPU segment:
194 * phys_ram_seg_t[cpup->nmemseg];
195 * segmap[cpup->nsegmap];
196 * ptes[cpup->npmegs];
198 segmaps
= (struct segmap
*)((long)kd
->cpu_data
+ cpup
->segmapoffset
);
199 ptes
= (int *)((int)kd
->cpu_data
+ cpup
->pmegoffset
);
200 nkreg
= ((int)((-(unsigned)kernbase
) / NBPRG
));
206 sp
= &segmaps
[(vr
-nureg
)*NSEGRG
+ vs
];
207 if (sp
->sg_npte
== 0)
209 if (sp
->sg_pmeg
== cpup
->npmeg
- 1) /* =seginval */
211 pte
= ptes
[sp
->sg_pmeg
* nptesg
+ VA_VPG(va
)];
212 if ((pte
& PG_V
) != 0) {
213 paddr_t p
, off
= VA_OFF(va
);
215 p
= (pte
& PG_PFNUM
) << pgshift
;
217 return (kd
->nbpg
- off
);
220 _kvm_err(kd
, 0, "invalid address (%#"PRIxVADDR
")", va
);
225 _kvm_kvatop4m(kvm_t
*kd
, vaddr_t va
, paddr_t
*pa
)
227 sparc64_cpu_kcore_hdr_t
*cpup
= kd
->cpu_data
;
231 struct segmap
*sp
, *segmaps
;
233 u_long kernbase
= cpup
->kernbase
;
239 * Layout of CPU segment:
242 * phys_ram_seg_t[cpup->nmemseg];
243 * segmap[cpup->nsegmap];
245 segmaps
= (struct segmap
*)((long)kd
->cpu_data
+ cpup
->segmapoffset
);
246 nkreg
= ((int)((-(unsigned)kernbase
) / NBPRG
));
252 sp
= &segmaps
[(vr
-nureg
)*NSEGRG
+ vs
];
253 if (sp
->sg_npte
== 0)
256 /* XXX - assume page tables in initial kernel DATA or BSS. */
257 foff
= _kvm_pa2off(kd
, (u_long
)&sp
->sg_pte
[VA_VPG(va
)] - kernbase
);
258 if (foff
== (off_t
)-1)
261 if (_kvm_pread(kd
, kd
->pmfd
, &pte
, sizeof(pte
), foff
) != sizeof(pte
)) {
262 _kvm_syserr(kd
, kd
->program
, "cannot read pte for "
267 if ((pte
& SRMMU_TETYPE
) == SRMMU_TEPTE
) {
268 long p
, off
= VA_OFF(va
);
270 p
= (pte
& SRMMU_PPNMASK
) << SRMMU_PPNPASHIFT
;
272 return (kd
->nbpg
- off
);
275 _kvm_err(kd
, 0, "invalid address (%#"PRIxVADDR
")", va
);
280 * sparc64 pmap's 32-bit page table format
283 _kvm_kvatop4u(kvm_t
*kd
, vaddr_t va
, paddr_t
*pa
)
285 sparc64_cpu_kcore_hdr_t
*cpup
= kd
->cpu_data
;
289 int64_t kphys
= cpup
->kphys
;
290 u_long kernbase
= cpup
->kernbase
;
299 * 4MB locked TLB (text+data+BSS)
300 * Random other stuff.
302 if (va
>= kernbase
&& va
< kernbase
+ 4*1024*1024)
303 return (va
- kernbase
) + kphys
;
305 /* XXX: from sparc64/include/pmap.h */
306 #define SPARC64_PTSZ (kd->nbpg/8)
307 #define SPARC64_STSZ (SPARC64_PTSZ)
308 #define SPARC64_PTMASK (SPARC64_PTSZ-1)
309 #define SPARC64_PTSHIFT (13)
310 #define SPARC64_PDSHIFT (10+SPARC64_PTSHIFT)
311 #define SPARC64_STSHIFT (10+SPARC64_PDSHIFT)
312 #define SPARC64_STMASK (SPARC64_STSZ-1)
313 #define sparc64_va_to_seg(v) (int)((((int64_t)(v))>>SPARC64_STSHIFT)&SPARC64_STMASK)
314 #define sparc64_va_to_pte(v) (int)((((int64_t)(v))>>SPARC64_PTSHIFT)&SPARC64_PTMASK)
316 /* XXX: from sparc64/include/pte.h */
317 #define SPARC64_TLB_V 0x8000000000000000LL
318 #define SPARC64_TLB_PA_MASK 0x000001ffffffe000LL
321 * Layout of CPU segment:
324 * phys_ram_seg_t[cpup->nmemseg];
325 * segmap[cpup->nsegmap];
327 segmaps
= (int64_t **)((long)kd
->cpu_data
+ cpup
->segmapoffset
);
328 ptes
= (int64_t *)(intptr_t)_kvm_pa2off(kd
,
329 (paddr_t
)(intptr_t)segmaps
[sparc64_va_to_seg(va
)]);
330 pte
= ptes
[sparc64_va_to_pte(va
)];
331 if ((pte
& SPARC64_TLB_V
) != 0)
332 return ((pte
& SPARC64_TLB_PA_MASK
) | (va
& (kd
->nbpg
- 1)));
334 _kvm_err(kd
, 0, "invalid address (%#"PRIxVADDR
")", va
);
340 * Translate a physical address to a file-offset in the crash dump.
343 _kvm_pa2off(kvm_t
*kd
, paddr_t pa
)
345 sparc64_cpu_kcore_hdr_t
*cpup
= kd
->cpu_data
;
351 * Layout of CPU segment:
354 * phys_ram_seg_t[cpup->nmemseg];
356 mp
= (phys_ram_seg_t
*)((int)kd
->cpu_data
+ cpup
->memsegoffset
);
359 /* Translate (sparse) pfnum to (packed) dump offset */
360 for (nmem
= cpup
->nmemseg
; --nmem
>= 0; mp
++) {
361 if (mp
->start
<= pa
&& pa
< mp
->start
+ mp
->size
)
366 _kvm_err(kd
, 0, "invalid address (%lx)", (unsigned long)pa
);
370 return (kd
->dump_off
+ off
+ pa
- mp
->start
);
374 * Machine-dependent initialization for ALL open kvm descriptors,
375 * not just those for a kernel crash dump. Some architectures
376 * have to deal with these NOT being constants! (i.e. m68k)
379 _kvm_mdopen(kvm_t
*kd
)
382 extern struct ps_strings
*__ps_strings
;
384 max_uva
= (u_long
) (__ps_strings
+ 1);
385 kd
->usrstack
= max_uva
;
386 kd
->max_uva
= max_uva
;