xref: /netbsd-src/lib/libkvm/kvm_sun3x.c (revision e375a9464f7c6a00756c974dc43a656e7fb7f42f)
1*e375a946Schristos /*	$NetBSD: kvm_sun3x.c,v 1.12 2011/09/14 12:37:55 christos Exp $	*/
295883471Sgwr 
395883471Sgwr /*-
495883471Sgwr  * Copyright (c) 1997 The NetBSD Foundation, Inc.
595883471Sgwr  * All rights reserved.
695883471Sgwr  *
795883471Sgwr  * This code is derived from software contributed to The NetBSD Foundation
895883471Sgwr  * by Gordon W. Ross.
995883471Sgwr  *
1095883471Sgwr  * Redistribution and use in source and binary forms, with or without
1195883471Sgwr  * modification, are permitted provided that the following conditions
1295883471Sgwr  * are met:
1395883471Sgwr  * 1. Redistributions of source code must retain the above copyright
1495883471Sgwr  *    notice, this list of conditions and the following disclaimer.
1595883471Sgwr  * 2. Redistributions in binary form must reproduce the above copyright
1695883471Sgwr  *    notice, this list of conditions and the following disclaimer in the
1795883471Sgwr  *    documentation and/or other materials provided with the distribution.
1895883471Sgwr  *
1995883471Sgwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2095883471Sgwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2195883471Sgwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2295883471Sgwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2395883471Sgwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2495883471Sgwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2595883471Sgwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2695883471Sgwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2795883471Sgwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2895883471Sgwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2995883471Sgwr  * POSSIBILITY OF SUCH DAMAGE.
3095883471Sgwr  */
3195883471Sgwr 
32b4119f6bSmikel #include <sys/cdefs.h>
3395883471Sgwr #if defined(LIBC_SCCS) && !defined(lint)
3495883471Sgwr #if 0
3595883471Sgwr static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
3695883471Sgwr #else
37*e375a946Schristos __RCSID("$NetBSD: kvm_sun3x.c,v 1.12 2011/09/14 12:37:55 christos Exp $");
3895883471Sgwr #endif
3995883471Sgwr #endif /* LIBC_SCCS and not lint */
4095883471Sgwr 
4195883471Sgwr /*
4295883471Sgwr  * Sun3x machine dependent routines for kvm.
4395883471Sgwr  *
4495883471Sgwr  * Note: This file has to build on ALL m68k machines,
4595883471Sgwr  * so do NOT include any <machine / *.h> files here.
4695883471Sgwr  */
47*e375a946Schristos #include <sys/param.h>
4895883471Sgwr #include <sys/types.h>
4995883471Sgwr #include <sys/kcore.h>
5095883471Sgwr 
5195883471Sgwr #include <unistd.h>
5295883471Sgwr #include <limits.h>
5395883471Sgwr #include <nlist.h>
5495883471Sgwr #include <kvm.h>
5595883471Sgwr #include <db.h>
5695883471Sgwr 
57b6c0c9a2Sthorpej #include <m68k/kcore.h>
58b6c0c9a2Sthorpej 
5995883471Sgwr #include "kvm_private.h"
6095883471Sgwr #include "kvm_m68k.h"
6195883471Sgwr 
626dc46b92Sjym int   _kvm_sun3x_initvtop(kvm_t *);
636dc46b92Sjym void  _kvm_sun3x_freevtop(kvm_t *);
64962a341dSjym int   _kvm_sun3x_kvatop  (kvm_t *, vaddr_t, paddr_t *);
65962a341dSjym off_t _kvm_sun3x_pa2off  (kvm_t *, paddr_t);
6695883471Sgwr 
6795883471Sgwr struct kvm_ops _kvm_ops_sun3x = {
6895883471Sgwr 	_kvm_sun3x_initvtop,
6995883471Sgwr 	_kvm_sun3x_freevtop,
7095883471Sgwr 	_kvm_sun3x_kvatop,
7195883471Sgwr 	_kvm_sun3x_pa2off };
7295883471Sgwr 
73b6c0c9a2Sthorpej #define	_kvm_kvas_size(h)	\
74b6c0c9a2Sthorpej 	(-((h)->kernbase))
75b6c0c9a2Sthorpej #define	_kvm_nkptes(h, v)	\
76b6c0c9a2Sthorpej 	(_kvm_kvas_size((h)) >> (v)->pgshift)
77b6c0c9a2Sthorpej #define	_kvm_pg_pa(pte, h)	\
78b6c0c9a2Sthorpej 	((pte) & (h)->pg_frame)
7995883471Sgwr 
8095883471Sgwr /*
8195883471Sgwr  * Prepare for translation of kernel virtual addresses into offsets
8295883471Sgwr  * into crash dump files.  Nothing to do here.
8395883471Sgwr  */
8495883471Sgwr int
_kvm_sun3x_initvtop(kvm_t * kd)856dc46b92Sjym _kvm_sun3x_initvtop(kvm_t *kd)
8695883471Sgwr {
876dc46b92Sjym 	return 0;
8895883471Sgwr }
8995883471Sgwr 
9095883471Sgwr void
_kvm_sun3x_freevtop(kvm_t * kd)916dc46b92Sjym _kvm_sun3x_freevtop(kvm_t *kd)
9295883471Sgwr {
9395883471Sgwr }
9495883471Sgwr 
9595883471Sgwr /*
9695883471Sgwr  * Translate a kernel virtual address to a physical address using the
9795883471Sgwr  * mapping information in kd->vm.  Returns the result in pa, and returns
9895883471Sgwr  * the number of bytes that are contiguously available from this
9995883471Sgwr  * physical address.  This routine is used only for crash dumps.
10095883471Sgwr  */
10195883471Sgwr int
_kvm_sun3x_kvatop(kvm_t * kd,vaddr_t va,paddr_t * pap)102962a341dSjym _kvm_sun3x_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pap)
10395883471Sgwr {
104b6c0c9a2Sthorpej 	cpu_kcore_hdr_t *h = kd->cpu_data;
105b6c0c9a2Sthorpej 	struct sun3x_kcore_hdr *s = &h->un._sun3x;
106b6c0c9a2Sthorpej 	struct vmstate *v = kd->vmst;
10795883471Sgwr 	int idx, len, offset, pte;
10895883471Sgwr 	u_long pteva, pa;
10995883471Sgwr 
11095883471Sgwr 	if (ISALIVE(kd)) {
11195883471Sgwr 		_kvm_err(kd, 0, "vatop called in live kernel!");
112b6c0c9a2Sthorpej 		return(0);
11395883471Sgwr 	}
11495883471Sgwr 
115b6c0c9a2Sthorpej 	if (va < h->kernbase) {
11695883471Sgwr 		_kvm_err(kd, 0, "not a kernel address");
11795883471Sgwr 		return(0);
11895883471Sgwr 	}
11995883471Sgwr 
12095883471Sgwr 	/*
12195883471Sgwr 	 * If this VA is in the contiguous range, short-cut.
12295883471Sgwr 	 * Note that this ends our recursion when we call
12395883471Sgwr 	 * kvm_read to access the kernel page table, which
12495883471Sgwr 	 * is guaranteed to be in the contiguous range.
12595883471Sgwr 	 */
126b6c0c9a2Sthorpej 	if (va < s->contig_end) {
127f2ab2a35Sgwr 		len = s->contig_end - va;
128b6c0c9a2Sthorpej 		pa = va - h->kernbase;
12995883471Sgwr 		goto done;
13095883471Sgwr 	}
13195883471Sgwr 
13295883471Sgwr 	/*
13395883471Sgwr 	 * The KVA is beyond the contiguous range, so we must
13495883471Sgwr 	 * read the PTE for this KVA from the page table.
13595883471Sgwr 	 */
136b6c0c9a2Sthorpej 	idx = ((va - h->kernbase) >> v->pgshift);
137b6c0c9a2Sthorpej 	pteva = s->kernCbase + (idx * 4);
13895883471Sgwr 	if (kvm_read(kd, pteva, &pte, 4) != 4) {
13995883471Sgwr 		_kvm_err(kd, 0, "can not read PTE!");
14095883471Sgwr 		return (0);
14195883471Sgwr 	}
142b6c0c9a2Sthorpej 	if ((pte & s->pg_valid) == 0) {
143dd0d61e3She 		_kvm_err(kd, 0, "page not valid (VA=0x%lx)", va);
14495883471Sgwr 		return (0);
14595883471Sgwr 	}
146b6c0c9a2Sthorpej 	offset = va & v->pgofset;
147b6c0c9a2Sthorpej 	len = (h->page_size - offset);
148b6c0c9a2Sthorpej 	pa = _kvm_pg_pa(pte, s) + offset;
14995883471Sgwr 
15095883471Sgwr done:
15195883471Sgwr 	*pap = pa;
15295883471Sgwr 	return (len);
15395883471Sgwr }
15495883471Sgwr 
15595883471Sgwr /*
156b976c559Swiz  * Translate a physical address to a file-offset in the crash dump.
15795883471Sgwr  */
15895883471Sgwr off_t
_kvm_sun3x_pa2off(kvm_t * kd,paddr_t pa)159962a341dSjym _kvm_sun3x_pa2off(kvm_t *kd, paddr_t pa)
16095883471Sgwr {
16195883471Sgwr 	off_t		off;
162b6c0c9a2Sthorpej 	phys_ram_seg_t	*rsp;
163b6c0c9a2Sthorpej 	cpu_kcore_hdr_t *h = kd->cpu_data;
164b6c0c9a2Sthorpej 	struct sun3x_kcore_hdr *s = &h->un._sun3x;
16595883471Sgwr 
16695883471Sgwr 	off = 0;
167b6c0c9a2Sthorpej 	for (rsp = s->ram_segs; rsp->size; rsp++) {
16895883471Sgwr 		if (pa >= rsp->start && pa < rsp->start + rsp->size) {
16995883471Sgwr 			pa -= rsp->start;
17095883471Sgwr 			break;
17195883471Sgwr 		}
17295883471Sgwr 		off += rsp->size;
17395883471Sgwr 	}
17495883471Sgwr 	return (kd->dump_off + off + pa);
17595883471Sgwr }
17695883471Sgwr 
177