Home | History | Annotate | Line # | Download | only in libkvm
kvm_sparc64.c revision 1.11
      1  1.11  martin /*	$NetBSD: kvm_sparc64.c,v 1.11 2007/11/05 00:46:23 martin Exp $	*/
      2   1.1     eeh 
      3   1.1     eeh /*-
      4   1.1     eeh  * Copyright (c) 1992, 1993
      5   1.1     eeh  *	The Regents of the University of California.  All rights reserved.
      6   1.1     eeh  *
      7   1.1     eeh  * This code is derived from software developed by the Computer Systems
      8   1.1     eeh  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
      9   1.1     eeh  * BG 91-66 and contributed to Berkeley.
     10   1.1     eeh  *
     11   1.1     eeh  * Redistribution and use in source and binary forms, with or without
     12   1.1     eeh  * modification, are permitted provided that the following conditions
     13   1.1     eeh  * are met:
     14   1.1     eeh  * 1. Redistributions of source code must retain the above copyright
     15   1.1     eeh  *    notice, this list of conditions and the following disclaimer.
     16   1.1     eeh  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1     eeh  *    notice, this list of conditions and the following disclaimer in the
     18   1.1     eeh  *    documentation and/or other materials provided with the distribution.
     19  1.10     agc  * 3. Neither the name of the University nor the names of its contributors
     20   1.1     eeh  *    may be used to endorse or promote products derived from this software
     21   1.1     eeh  *    without specific prior written permission.
     22   1.1     eeh  *
     23   1.1     eeh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1     eeh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1     eeh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1     eeh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1     eeh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1     eeh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1     eeh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1     eeh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1     eeh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1     eeh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1     eeh  * SUCH DAMAGE.
     34   1.1     eeh  */
     35   1.1     eeh 
     36   1.1     eeh #include <sys/cdefs.h>
     37   1.1     eeh #if defined(LIBC_SCCS) && !defined(lint)
     38   1.1     eeh #if 0
     39   1.1     eeh static char sccsid[] = "@(#)kvm_sparc.c	8.1 (Berkeley) 6/4/93";
     40   1.1     eeh #else
     41  1.11  martin __RCSID("$NetBSD: kvm_sparc64.c,v 1.11 2007/11/05 00:46:23 martin Exp $");
     42   1.1     eeh #endif
     43   1.1     eeh #endif /* LIBC_SCCS and not lint */
     44   1.1     eeh 
     45   1.1     eeh /*
     46   1.2  simonb  * Sparc machine dependent routines for kvm.  Hopefully, the forthcoming
     47   1.1     eeh  * vm code will one day obsolete this module.
     48   1.1     eeh  */
     49   1.1     eeh 
     50   1.1     eeh #include <sys/param.h>
     51   1.1     eeh #include <sys/exec.h>
     52   1.1     eeh #include <sys/user.h>
     53   1.1     eeh #include <sys/proc.h>
     54   1.1     eeh #include <sys/stat.h>
     55   1.1     eeh #include <sys/core.h>
     56   1.1     eeh #include <sys/kcore.h>
     57   1.1     eeh #include <unistd.h>
     58   1.1     eeh #include <nlist.h>
     59   1.1     eeh #include <kvm.h>
     60   1.1     eeh 
     61   1.4     mrg #include <uvm/uvm_extern.h>
     62   1.3     mrg 
     63   1.7    matt #include <machine/pmap.h>
     64   1.1     eeh #include <machine/kcore.h>
     65   1.8  martin #include <machine/vmparam.h>
     66   1.1     eeh 
     67   1.1     eeh #include <limits.h>
     68   1.1     eeh #include <db.h>
     69   1.1     eeh 
     70   1.1     eeh #include "kvm_private.h"
     71   1.1     eeh 
     72   1.1     eeh int _kvm_kvatop __P((kvm_t *, u_long, u_long *));
     73   1.1     eeh 
     74   1.1     eeh void
     75   1.1     eeh _kvm_freevtop(kd)
     76   1.1     eeh 	kvm_t *kd;
     77   1.1     eeh {
     78   1.1     eeh 	if (kd->vmst != 0) {
     79   1.1     eeh 		_kvm_err(kd, kd->program, "_kvm_freevtop: internal error");
     80   1.1     eeh 		kd->vmst = 0;
     81   1.1     eeh 	}
     82   1.1     eeh }
     83   1.1     eeh 
     84   1.1     eeh /*
     85   1.1     eeh  * Prepare for translation of kernel virtual addresses into offsets
     86   1.1     eeh  * into crash dump files. We use the MMU specific goop written at the
     87   1.1     eeh  * front of the crash dump by pmap_dumpmmu().
     88   1.5     eeh  *
     89   1.5     eeh  * We should read in and cache the ksegs here to speed up operations...
     90   1.1     eeh  */
     91   1.1     eeh int
     92   1.1     eeh _kvm_initvtop(kd)
     93   1.1     eeh 	kvm_t *kd;
     94   1.1     eeh {
     95   1.5     eeh 	kd->nbpg = 0x2000;
     96   1.5     eeh 
     97   1.1     eeh 	return (0);
     98   1.1     eeh }
     99   1.1     eeh 
    100   1.1     eeh /*
    101   1.1     eeh  * Translate a kernel virtual address to a physical address using the
    102   1.1     eeh  * mapping information in kd->vm.  Returns the result in pa, and returns
    103   1.2  simonb  * the number of bytes that are contiguously available from this
    104   1.9     wiz  * physical address.  This routine is used only for crash dumps.
    105   1.1     eeh  */
    106   1.1     eeh int
    107   1.1     eeh _kvm_kvatop(kd, va, pa)
    108   1.1     eeh 	kvm_t *kd;
    109   1.1     eeh 	u_long va;
    110   1.1     eeh 	u_long *pa;
    111   1.1     eeh {
    112   1.1     eeh 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
    113   1.1     eeh 	u_long kernbase = cpup->kernbase;
    114   1.5     eeh 	uint64_t *pseg, *pdir, *ptbl;
    115  1.11  martin 	struct cpu_kcore_4mbseg *ktlb;
    116   1.5     eeh 	int64_t data;
    117  1.11  martin 	int i;
    118   1.1     eeh 
    119   1.1     eeh 	if (va < kernbase)
    120   1.5     eeh 		goto lose;
    121   1.1     eeh 
    122   1.5     eeh 	/* Handle the wired 4MB TTEs */
    123  1.11  martin 	if (cpup->memsegoffset > sizeof(cpu_kcore_hdr_t) &&
    124  1.11  martin 	    cpup->newmagic == SPARC64_KCORE_NEWMAGIC) {
    125  1.11  martin 		/*
    126  1.11  martin 		 * new format: we have a list of 4 MB mappings
    127  1.11  martin 		 */
    128  1.11  martin 		ktlb = (struct cpu_kcore_4mbseg *)
    129  1.11  martin 			((uintptr_t)kd->cpu_data + cpup->off4mbsegs);
    130  1.11  martin 		for (i = 0; i < cpup->num4mbsegs; i++) {
    131  1.11  martin 			uint64_t start = ktlb[i].va;
    132  1.11  martin 			if (va < start || va >= start+PAGE_SIZE_4M)
    133  1.11  martin 				continue;
    134  1.11  martin 			*pa = ktlb[i].pa + va - start;
    135  1.11  martin 			return (int)(start+PAGE_SIZE_4M - va);
    136  1.11  martin 		}
    137  1.11  martin 
    138  1.11  martin 	} else {
    139  1.11  martin 		/*
    140  1.11  martin 		 * old format: just a textbase/size and database/size
    141  1.11  martin 		 */
    142  1.11  martin 		if (va > cpup->ktextbase && va <
    143  1.11  martin 		    (cpup->ktextbase + cpup->ktextsz)) {
    144  1.11  martin 			u_long vaddr;
    145  1.11  martin 
    146  1.11  martin 			vaddr = va - cpup->ktextbase;
    147  1.11  martin 			*pa = cpup->ktextp + vaddr;
    148  1.11  martin 			return (int)(cpup->ktextsz - vaddr);
    149  1.11  martin 		}
    150  1.11  martin 		if (va > cpup->kdatabase && va <
    151  1.11  martin 		    (cpup->kdatabase + cpup->kdatasz)) {
    152  1.11  martin 			u_long vaddr;
    153  1.11  martin 
    154  1.11  martin 			vaddr = va - cpup->kdatabase;
    155  1.11  martin 			*pa = cpup->kdatap + vaddr;
    156  1.11  martin 			return (int)(cpup->kdatasz - vaddr);
    157  1.11  martin 		}
    158   1.2  simonb 	}
    159   1.5     eeh 
    160   1.1     eeh 	/*
    161   1.5     eeh 	 * Parse kernel page table.
    162   1.1     eeh 	 */
    163   1.5     eeh 	pseg = (uint64_t *)(u_long)cpup->segmapoffset;
    164   1.5     eeh 	if (pread(kd->pmfd, &pdir, sizeof(pdir),
    165   1.5     eeh 		_kvm_pa2off(kd, (u_long)&pseg[va_to_seg(va)]))
    166   1.5     eeh 		!= sizeof(pdir)) {
    167   1.5     eeh 		_kvm_syserr(kd, 0, "could not read L1 PTE");
    168   1.5     eeh 		goto lose;
    169   1.5     eeh 	}
    170   1.5     eeh 
    171   1.5     eeh 	if (!pdir) {
    172   1.5     eeh 		_kvm_err(kd, 0, "invalid L1 PTE");
    173   1.5     eeh 		goto lose;
    174   1.5     eeh 	}
    175   1.5     eeh 
    176   1.5     eeh 	if (pread(kd->pmfd, &ptbl, sizeof(ptbl),
    177   1.5     eeh 		_kvm_pa2off(kd, (u_long)&pdir[va_to_dir(va)]))
    178   1.5     eeh 		!= sizeof(ptbl)) {
    179   1.5     eeh 		_kvm_syserr(kd, 0, "could not read L2 PTE");
    180   1.5     eeh 		goto lose;
    181   1.1     eeh 	}
    182   1.5     eeh 
    183   1.5     eeh 	if (!ptbl) {
    184   1.5     eeh 		_kvm_err(kd, 0, "invalid L2 PTE");
    185   1.5     eeh 		goto lose;
    186   1.5     eeh 	}
    187   1.5     eeh 
    188   1.5     eeh 	if (pread(kd->pmfd, &data, sizeof(data),
    189   1.5     eeh 		_kvm_pa2off(kd, (u_long)&ptbl[va_to_pte(va)]))
    190   1.5     eeh 		!= sizeof(data)) {
    191   1.5     eeh 		_kvm_syserr(kd, 0, "could not read TTE");
    192   1.5     eeh 		goto lose;
    193   1.5     eeh 	}
    194   1.5     eeh 
    195   1.5     eeh 	if (data >= 0) {
    196   1.5     eeh 		_kvm_err(kd, 0, "invalid L2 TTE");
    197   1.5     eeh 		goto lose;
    198   1.5     eeh 	}
    199   1.5     eeh 
    200   1.5     eeh 	/*
    201   1.5     eeh 	 * Calculate page offsets and things.
    202   1.5     eeh 	 *
    203   1.5     eeh 	 * XXXX -- We could support multiple page sizes.
    204   1.5     eeh 	 */
    205   1.5     eeh 	va = va & (kd->nbpg - 1);
    206   1.5     eeh 	data &= TLB_PA_MASK;
    207   1.5     eeh 	*pa = data + va;
    208   1.5     eeh 
    209   1.5     eeh 	/*
    210   1.5     eeh 	 * Parse and trnslate our TTE.
    211   1.5     eeh 	 */
    212   1.5     eeh 
    213  1.11  martin 	return (int)(kd->nbpg - va);
    214   1.5     eeh 
    215   1.5     eeh lose:
    216  1.11  martin 	*pa = (u_long)-1;
    217   1.6      he 	_kvm_err(kd, 0, "invalid address (%lx)", va);
    218   1.1     eeh 	return (0);
    219   1.1     eeh }
    220   1.1     eeh 
    221   1.1     eeh 
    222   1.2  simonb /*
    223   1.9     wiz  * Translate a physical address to a file-offset in the crash dump.
    224   1.2  simonb  */
    225   1.1     eeh off_t
    226   1.1     eeh _kvm_pa2off(kd, pa)
    227   1.1     eeh 	kvm_t   *kd;
    228   1.1     eeh 	u_long  pa;
    229   1.1     eeh {
    230   1.1     eeh 	cpu_kcore_hdr_t *cpup = kd->cpu_data;
    231   1.1     eeh 	phys_ram_seg_t *mp;
    232   1.1     eeh 	off_t off;
    233   1.1     eeh 	int nmem;
    234   1.1     eeh 
    235   1.1     eeh 	/*
    236   1.1     eeh 	 * Layout of CPU segment:
    237   1.1     eeh 	 *	cpu_kcore_hdr_t;
    238   1.1     eeh 	 *	[alignment]
    239   1.1     eeh 	 *	phys_ram_seg_t[cpup->nmemseg];
    240   1.1     eeh 	 */
    241   1.1     eeh 	mp = (phys_ram_seg_t *)((long)kd->cpu_data + cpup->memsegoffset);
    242   1.1     eeh 	off = 0;
    243   1.1     eeh 
    244   1.1     eeh 	/* Translate (sparse) pfnum to (packed) dump offset */
    245   1.1     eeh 	for (nmem = cpup->nmemseg; --nmem >= 0; mp++) {
    246   1.1     eeh 		if (mp->start <= pa && pa < mp->start + mp->size)
    247   1.1     eeh 			break;
    248   1.1     eeh 		off += mp->size;
    249   1.1     eeh 	}
    250   1.1     eeh 	if (nmem < 0) {
    251   1.6      he 		_kvm_err(kd, 0, "invalid address (%lx)", pa);
    252   1.1     eeh 		return (-1);
    253   1.1     eeh 	}
    254   1.1     eeh 
    255   1.1     eeh 	return (kd->dump_off + off + pa - mp->start);
    256   1.1     eeh }
    257   1.1     eeh 
    258   1.1     eeh /*
    259   1.1     eeh  * Machine-dependent initialization for ALL open kvm descriptors,
    260   1.1     eeh  * not just those for a kernel crash dump.  Some architectures
    261   1.1     eeh  * have to deal with these NOT being constants!  (i.e. m68k)
    262   1.1     eeh  */
    263   1.1     eeh int
    264   1.1     eeh _kvm_mdopen(kd)
    265   1.1     eeh 	kvm_t	*kd;
    266   1.1     eeh {
    267   1.1     eeh 	u_long max_uva;
    268   1.1     eeh 	extern struct ps_strings *__ps_strings;
    269   1.1     eeh 
    270   1.1     eeh 	max_uva = (u_long) (__ps_strings + 1);
    271   1.1     eeh 	kd->usrstack = max_uva;
    272   1.1     eeh 	kd->max_uva  = max_uva;
    273   1.1     eeh 	kd->min_uva  = 0;
    274   1.1     eeh 
    275   1.1     eeh 	return (0);
    276   1.1     eeh }
    277