xref: /openbsd-src/lib/libkvm/kvm_sparc64.c (revision f28d1820ab22a971f7e3925363c43855d0ed1d08)
1*f28d1820Smiod /*	$OpenBSD: kvm_sparc64.c,v 1.14 2024/04/11 18:58:44 miod Exp $	*/
203599454Sart /*	$NetBSD: kvm_sparc64.c,v 1.7 2001/08/05 03:33:15 matt Exp $	*/
303599454Sart 
403599454Sart /*-
503599454Sart  * Copyright (c) 1992, 1993
603599454Sart  *	The Regents of the University of California.  All rights reserved.
703599454Sart  *
803599454Sart  * This code is derived from software developed by the Computer Systems
903599454Sart  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
1003599454Sart  * BG 91-66 and contributed to Berkeley.
1103599454Sart  *
1203599454Sart  * Redistribution and use in source and binary forms, with or without
1303599454Sart  * modification, are permitted provided that the following conditions
1403599454Sart  * are met:
1503599454Sart  * 1. Redistributions of source code must retain the above copyright
1603599454Sart  *    notice, this list of conditions and the following disclaimer.
1703599454Sart  * 2. Redistributions in binary form must reproduce the above copyright
1803599454Sart  *    notice, this list of conditions and the following disclaimer in the
1903599454Sart  *    documentation and/or other materials provided with the distribution.
206580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
2103599454Sart  *    may be used to endorse or promote products derived from this software
2203599454Sart  *    without specific prior written permission.
2303599454Sart  *
2403599454Sart  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2503599454Sart  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2603599454Sart  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2703599454Sart  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2803599454Sart  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2903599454Sart  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3003599454Sart  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3103599454Sart  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3203599454Sart  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3303599454Sart  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3403599454Sart  * SUCH DAMAGE.
3503599454Sart  */
3603599454Sart 
3703599454Sart /*
3803599454Sart  * Sparc machine dependent routines for kvm.  Hopefully, the forthcoming
3903599454Sart  * vm code will one day obsolete this module.
4003599454Sart  */
4103599454Sart 
42c23ad179Sderaadt #include <sys/types.h>
43c23ad179Sderaadt #include <sys/signal.h>
4403599454Sart #include <sys/exec.h>
4503599454Sart #include <sys/proc.h>
4603599454Sart #include <sys/stat.h>
4703599454Sart #include <sys/kcore.h>
4803599454Sart #include <unistd.h>
4903599454Sart #include <nlist.h>
5003599454Sart #include <kvm.h>
5103599454Sart 
5203599454Sart #include <uvm/uvm_extern.h>
5303599454Sart 
540e64ee4cSderaadt #include <machine/param.h>
5571007b88Sart #include <machine/vmparam.h>
56*f28d1820Smiod #include <machine/pte.h>
5703599454Sart #include <machine/kcore.h>
5803599454Sart 
5903599454Sart #include <limits.h>
6003599454Sart #include <db.h>
6103599454Sart 
6203599454Sart #include "kvm_private.h"
6303599454Sart 
641a7739feSkettenis /*
651a7739feSkettenis  * UltraSPARC T1 & T2 implement only a 40-bit real address range, just
661a7739feSkettenis  * like older UltraSPARC CPUs.
671a7739feSkettenis  */
681a7739feSkettenis #define TLB_PA_MASK	SUN4U_TLB_PA_MASK
691a7739feSkettenis 
70c72b5b24Smillert int _kvm_kvatop(kvm_t *, u_long, u_long *);
7103599454Sart 
7203599454Sart void
_kvm_freevtop(kvm_t * kd)73551fad64Sderaadt _kvm_freevtop(kvm_t *kd)
7403599454Sart {
75551fad64Sderaadt 	if (kd->vmst != NULL) {
7603599454Sart 		_kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
77551fad64Sderaadt 		kd->vmst = NULL;
7803599454Sart 	}
7903599454Sart }
8003599454Sart 
8103599454Sart /*
8203599454Sart  * Prepare for translation of kernel virtual addresses into offsets
8303599454Sart  * into crash dump files. We use the MMU specific goop written at the
8403599454Sart  * front of the crash dump by pmap_dumpmmu().
8503599454Sart  *
8603599454Sart  * We should read in and cache the ksegs here to speed up operations...
8703599454Sart  */
8803599454Sart int
_kvm_initvtop(kvm_t * kd)89551fad64Sderaadt _kvm_initvtop(kvm_t *kd)
9003599454Sart {
9103599454Sart 	return (0);
9203599454Sart }
9303599454Sart 
9403599454Sart /*
9503599454Sart  * Translate a kernel virtual address to a physical address using the
9603599454Sart  * mapping information in kd->vm.  Returns the result in pa, and returns
9703599454Sart  * the number of bytes that are contiguously available from this
9803599454Sart  * physical address.  This routine is used only for crashdumps.
9903599454Sart  */
10003599454Sart int
_kvm_kvatop(kvm_t * kd,u_long va,paddr_t * pa)101fdd3f45bSmickey _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa)
10203599454Sart {
10303599454Sart 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
10403599454Sart 	u_long kernbase = cpup->kernbase;
10503599454Sart 	uint64_t *pseg, *pdir, *ptbl;
10603599454Sart 	int64_t data;
10703599454Sart 
10803599454Sart 	if (va < kernbase)
10903599454Sart 		goto lose;
11003599454Sart 
11103599454Sart 	/* Handle the wired 4MB TTEs */
11203599454Sart 	if (va > cpup->ktextbase && va < (cpup->ktextbase + cpup->ktextsz)) {
11303599454Sart 		u_long vaddr;
11403599454Sart 
11503599454Sart 		vaddr = va - cpup->ktextbase;
11603599454Sart 		*pa = cpup->ktextp + vaddr;
11703599454Sart 		return (cpup->ktextsz - vaddr);
11803599454Sart 	}
11903599454Sart 
12003599454Sart 	if (va > cpup->kdatabase && va < (cpup->kdatabase + cpup->kdatasz)) {
12103599454Sart 		u_long vaddr;
12203599454Sart 
12303599454Sart 		vaddr = va - cpup->kdatabase;
12403599454Sart 		*pa = cpup->kdatap + vaddr;
12503599454Sart 		return (cpup->kdatasz - vaddr);
12603599454Sart 	}
12703599454Sart 
12803599454Sart 
12903599454Sart 	/*
13003599454Sart 	 * Parse kernel page table.
13103599454Sart 	 */
13203599454Sart 	pseg = (uint64_t *)(u_long)cpup->segmapoffset;
13303599454Sart 	if (pread(kd->pmfd, &pdir, sizeof(pdir),
134551fad64Sderaadt 	    _kvm_pa2off(kd, (u_long)&pseg[va_to_seg(va)])) != sizeof(pdir)) {
13503599454Sart 		_kvm_syserr(kd, 0, "could not read L1 PTE");
13603599454Sart 		goto lose;
13703599454Sart 	}
13803599454Sart 
13903599454Sart 	if (!pdir) {
14003599454Sart 		_kvm_err(kd, 0, "invalid L1 PTE");
14103599454Sart 		goto lose;
14203599454Sart 	}
14303599454Sart 
14403599454Sart 	if (pread(kd->pmfd, &ptbl, sizeof(ptbl),
145551fad64Sderaadt 	    _kvm_pa2off(kd, (u_long)&pdir[va_to_dir(va)])) != sizeof(ptbl)) {
14603599454Sart 		_kvm_syserr(kd, 0, "could not read L2 PTE");
14703599454Sart 		goto lose;
14803599454Sart 	}
14903599454Sart 
15003599454Sart 	if (!ptbl) {
15103599454Sart 		_kvm_err(kd, 0, "invalid L2 PTE");
15203599454Sart 		goto lose;
15303599454Sart 	}
15403599454Sart 
15503599454Sart 	if (pread(kd->pmfd, &data, sizeof(data),
156551fad64Sderaadt 	    _kvm_pa2off(kd, (u_long)&ptbl[va_to_pte(va)])) != sizeof(data)) {
15703599454Sart 		_kvm_syserr(kd, 0, "could not read TTE");
15803599454Sart 		goto lose;
15903599454Sart 	}
16003599454Sart 
16103599454Sart 	if (data >= 0) {
16203599454Sart 		_kvm_err(kd, 0, "invalid L2 TTE");
16303599454Sart 		goto lose;
16403599454Sart 	}
16503599454Sart 
16603599454Sart 	/*
16703599454Sart 	 * Calculate page offsets and things.
16803599454Sart 	 *
16903599454Sart 	 * XXXX -- We could support multiple page sizes.
17003599454Sart 	 */
17103599454Sart 	va = va & (kd->nbpg - 1);
17203599454Sart 	data &= TLB_PA_MASK;
17303599454Sart 	*pa = data + va;
17403599454Sart 
17503599454Sart 	/*
176551fad64Sderaadt 	 * Parse and translate our TTE.
17703599454Sart 	 */
17803599454Sart 	return (kd->nbpg - va);
17903599454Sart 
18003599454Sart lose:
18103599454Sart 	*pa = -1;
18203599454Sart 	_kvm_err(kd, 0, "invalid address (%lx)", va);
18303599454Sart 	return (0);
18403599454Sart }
18503599454Sart 
18603599454Sart 
18703599454Sart /*
18803599454Sart  * Translate a physical address to a file-offset in the crash-dump.
18903599454Sart  */
19003599454Sart off_t
_kvm_pa2off(kvm_t * kd,paddr_t pa)191fdd3f45bSmickey _kvm_pa2off(kvm_t *kd, paddr_t pa)
19203599454Sart {
19303599454Sart 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
19403599454Sart 	phys_ram_seg_t *mp;
19503599454Sart 	off_t off;
19603599454Sart 	int nmem;
19703599454Sart 
19803599454Sart 	/*
19903599454Sart 	 * Layout of CPU segment:
20003599454Sart 	 *	cpu_kcore_hdr_t;
20103599454Sart 	 *	[alignment]
20203599454Sart 	 *	phys_ram_seg_t[cpup->nmemseg];
20303599454Sart 	 */
20403599454Sart 	mp = (phys_ram_seg_t *)((long)kd->cpu_data + cpup->memsegoffset);
20503599454Sart 	off = 0;
20603599454Sart 
20703599454Sart 	/* Translate (sparse) pfnum to (packed) dump offset */
20803599454Sart 	for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
20903599454Sart 		if (mp->start <= pa && pa < mp->start + mp->size)
21003599454Sart 			break;
21103599454Sart 		off += mp->size;
21203599454Sart 	}
21303599454Sart 	if (nmem < 0) {
21403599454Sart 		_kvm_err(kd, 0, "invalid address (%lx)", pa);
21503599454Sart 		return (-1);
21603599454Sart 	}
21703599454Sart 
21803599454Sart 	return (kd->dump_off + off + pa - mp->start);
21903599454Sart }
220