Home | History | Annotate | Line # | Download | only in libkvm
kvm_powerpc64.c revision 1.1
      1  1.1  ross /*	$NetBSD: kvm_powerpc64.c,v 1.1 2006/07/01 19:21:11 ross Exp $	*/
      2  1.1  ross 
      3  1.1  ross /*
      4  1.1  ross  * Copyright (c) 2005 Wasabi Systems, Inc.
      5  1.1  ross  * All rights reserved.
      6  1.1  ross  *
      7  1.1  ross  * Written by Allen Briggs for Wasabi Systems, Inc.
      8  1.1  ross  *
      9  1.1  ross  * Redistribution and use in source and binary forms, with or without
     10  1.1  ross  * modification, are permitted provided that the following conditions
     11  1.1  ross  * are met:
     12  1.1  ross  * 1. Redistributions of source code must retain the above copyright
     13  1.1  ross  *    notice, this list of conditions and the following disclaimer.
     14  1.1  ross  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  ross  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  ross  *    documentation and/or other materials provided with the distribution.
     17  1.1  ross  * 3. All advertising materials mentioning features or use of this software
     18  1.1  ross  *    must display the following acknowledgement:
     19  1.1  ross  *      This product includes software developed for the NetBSD Project by
     20  1.1  ross  *      Wasabi Systems, Inc.
     21  1.1  ross  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1  ross  *    or promote products derived from this software without specific prior
     23  1.1  ross  *    written permission.
     24  1.1  ross  *
     25  1.1  ross  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1  ross  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  ross  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  ross  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1  ross  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  ross  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  ross  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  ross  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  ross  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  ross  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  ross  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1  ross  */
     37  1.1  ross /*-
     38  1.1  ross  * Copyright (C) 1996 Wolfgang Solfrank.
     39  1.1  ross  * Copyright (C) 1996 TooLs GmbH.
     40  1.1  ross  * All rights reserved.
     41  1.1  ross  *
     42  1.1  ross  * Redistribution and use in source and binary forms, with or without
     43  1.1  ross  * modification, are permitted provided that the following conditions
     44  1.1  ross  * are met:
     45  1.1  ross  * 1. Redistributions of source code must retain the above copyright
     46  1.1  ross  *    notice, this list of conditions and the following disclaimer.
     47  1.1  ross  * 2. Redistributions in binary form must reproduce the above copyright
     48  1.1  ross  *    notice, this list of conditions and the following disclaimer in the
     49  1.1  ross  *    documentation and/or other materials provided with the distribution.
     50  1.1  ross  * 3. All advertising materials mentioning features or use of this software
     51  1.1  ross  *    must display the following acknowledgement:
     52  1.1  ross  *	This product includes software developed by TooLs GmbH.
     53  1.1  ross  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     54  1.1  ross  *    derived from this software without specific prior written permission.
     55  1.1  ross  *
     56  1.1  ross  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     57  1.1  ross  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     58  1.1  ross  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     59  1.1  ross  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     60  1.1  ross  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     61  1.1  ross  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     62  1.1  ross  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     63  1.1  ross  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     64  1.1  ross  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     65  1.1  ross  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     66  1.1  ross  */
     67  1.1  ross 
     68  1.1  ross /*
     69  1.1  ross  * PowerPC machine dependent routines for kvm.
     70  1.1  ross  */
     71  1.1  ross 
     72  1.1  ross #include <sys/param.h>
     73  1.1  ross #include <sys/exec.h>
     74  1.1  ross 
     75  1.1  ross #include <uvm/uvm_extern.h>
     76  1.1  ross 
     77  1.1  ross #include <db.h>
     78  1.1  ross #include <limits.h>
     79  1.1  ross #include <kvm.h>
     80  1.1  ross #include <stdlib.h>
     81  1.1  ross #include <unistd.h>
     82  1.1  ross 
     83  1.1  ross #include "kvm_private.h"
     84  1.1  ross 
     85  1.1  ross #include <sys/kcore.h>
     86  1.1  ross #include <machine/kcore.h>
     87  1.1  ross 
     88  1.1  ross #include <powerpc/spr.h>
     89  1.1  ross #include <powerpc/oea/bat.h>
     90  1.1  ross #include <powerpc/oea/pte.h>
     91  1.1  ross 
     92  1.1  ross static int	_kvm_match_601bat(kvm_t *kd, u_long va, u_long *pa, int *off);
     93  1.1  ross static int	_kvm_match_bat(kvm_t *kd, u_long va, u_long *pa, int *off);
     94  1.1  ross static int	_kvm_match_sr(kvm_t *kd, u_long va, u_long *pa, int *off);
     95  1.1  ross static struct pte *_kvm_scan_pteg(struct pteg *pteg, uint32_t vsid,
     96  1.1  ross 				  uint32_t api, int secondary);
     97  1.1  ross 
     98  1.1  ross void
     99  1.1  ross _kvm_freevtop(kd)
    100  1.1  ross 	kvm_t *kd;
    101  1.1  ross {
    102  1.1  ross 	if (kd->vmst != 0)
    103  1.1  ross 		free(kd->vmst);
    104  1.1  ross }
    105  1.1  ross 
    106  1.1  ross /*ARGSUSED*/
    107  1.1  ross int
    108  1.1  ross _kvm_initvtop(kd)
    109  1.1  ross 	kvm_t *kd;
    110  1.1  ross {
    111  1.1  ross 
    112  1.1  ross 	return 0;
    113  1.1  ross }
    114  1.1  ross 
    115  1.1  ross #define SR_VSID_HASH_MASK	0x0007ffff
    116  1.1  ross 
    117  1.1  ross static struct pte *
    118  1.1  ross _kvm_scan_pteg(pteg, vsid, api, secondary)
    119  1.1  ross 	struct pteg *pteg;
    120  1.1  ross 	uint32_t vsid;
    121  1.1  ross 	uint32_t api;
    122  1.1  ross 	int secondary;
    123  1.1  ross {
    124  1.1  ross 	struct pte	*pte;
    125  1.1  ross 	u_long		ptehi;
    126  1.1  ross 	int		i;
    127  1.1  ross 
    128  1.1  ross 	for (i=0 ; i<8 ; i++) {
    129  1.1  ross 		pte = &pteg->pt[i];
    130  1.1  ross 		ptehi = (u_long) pte->pte_hi;
    131  1.1  ross 		if ((ptehi & PTE_VALID) == 0)
    132  1.1  ross 			continue;
    133  1.1  ross 		if ((ptehi & PTE_HID) != secondary)
    134  1.1  ross 			continue;
    135  1.1  ross 		if (((ptehi & PTE_VSID) >> PTE_VSID_SHFT) != vsid)
    136  1.1  ross 			continue;
    137  1.1  ross 		if (((ptehi & PTE_API) >> PTE_API_SHFT) != api)
    138  1.1  ross 			continue;
    139  1.1  ross 		return pte;
    140  1.1  ross 	}
    141  1.1  ross 	return NULL;
    142  1.1  ross }
    143  1.1  ross 
    144  1.1  ross #define HASH_MASK	0x0007ffff
    145  1.1  ross 
    146  1.1  ross static int
    147  1.1  ross _kvm_match_sr(kd, va, pa, off)
    148  1.1  ross 	kvm_t *kd;
    149  1.1  ross 	u_long va;
    150  1.1  ross 	u_long *pa;
    151  1.1  ross 	int *off;
    152  1.1  ross {
    153  1.1  ross 	cpu_kcore_hdr_t	*cpu_kh;
    154  1.1  ross 	struct pteg	pteg;
    155  1.1  ross 	struct pte	*pte;
    156  1.1  ross 	uint32_t	sr, pgoff, vsid, pgidx, api, hash;
    157  1.1  ross 	uint32_t	htaborg, htabmask, mhash;
    158  1.1  ross 	u_long		pteg_vaddr;
    159  1.1  ross 
    160  1.1  ross 	cpu_kh = kd->cpu_data;
    161  1.1  ross 
    162  1.1  ross 	sr = cpu_kh->sr[(va >> 28) & 0xf];
    163  1.1  ross 	if ((sr & SR_TYPE) != 0) {
    164  1.1  ross 		/* Direct-store segment (shouldn't be) */
    165  1.1  ross 		return 0;
    166  1.1  ross 	}
    167  1.1  ross 
    168  1.1  ross 	pgoff = va & ADDR_POFF;
    169  1.1  ross 	vsid = sr & SR_VSID;
    170  1.1  ross 	pgidx = (va & ADDR_PIDX) >> ADDR_PIDX_SHFT;
    171  1.1  ross 	api = pgidx >> 10;
    172  1.1  ross 	hash = (vsid & HASH_MASK) ^ pgidx;
    173  1.1  ross 
    174  1.1  ross 	htaborg = cpu_kh->sdr1 & 0xffff0000;
    175  1.1  ross 	htabmask = cpu_kh->sdr1 & 0x1ff;
    176  1.1  ross 
    177  1.1  ross 	mhash = (hash >> 10) & htabmask;
    178  1.1  ross 
    179  1.1  ross 	pteg_vaddr = ( htaborg & 0xfe000000) | ((hash & 0x3ff) << 6)
    180  1.1  ross 		   | ((htaborg & 0x01ff0000) | (mhash << 16));
    181  1.1  ross 
    182  1.1  ross 	if (pread(kd->pmfd, (void *) &pteg, sizeof(pteg),
    183  1.1  ross 		  _kvm_pa2off(kd, pteg_vaddr)) != sizeof(pteg)) {
    184  1.1  ross 		_kvm_syserr(kd, 0, "could not read primary PTEG");
    185  1.1  ross 		return 0;
    186  1.1  ross 	}
    187  1.1  ross 
    188  1.1  ross 	if ((pte = _kvm_scan_pteg(&pteg, vsid, api, 0)) != NULL) {
    189  1.1  ross 		*pa = (pte->pte_lo & PTE_RPGN) | pgoff;
    190  1.1  ross 		*off = NBPG - pgoff;
    191  1.1  ross 		return 1;
    192  1.1  ross 	}
    193  1.1  ross 
    194  1.1  ross 	hash = (~hash) & HASH_MASK;
    195  1.1  ross 	mhash = (hash >> 10) & htabmask;
    196  1.1  ross 
    197  1.1  ross 	pteg_vaddr = ( htaborg & 0xfe000000) | ((hash & 0x3ff) << 6)
    198  1.1  ross 		   | ((htaborg & 0x01ff0000) | (mhash << 16));
    199  1.1  ross 
    200  1.1  ross 	if (pread(kd->pmfd, (void *) &pteg, sizeof(pteg),
    201  1.1  ross 		  _kvm_pa2off(kd, pteg_vaddr)) != sizeof(pteg)) {
    202  1.1  ross 		_kvm_syserr(kd, 0, "could not read secondary PTEG");
    203  1.1  ross 		return 0;
    204  1.1  ross 	}
    205  1.1  ross 
    206  1.1  ross 	if ((pte = _kvm_scan_pteg(&pteg, vsid, api, 0)) != NULL) {
    207  1.1  ross 		*pa = (pte->pte_lo & PTE_RPGN) | pgoff;
    208  1.1  ross 		*off = NBPG - pgoff;
    209  1.1  ross 		return 1;
    210  1.1  ross 	}
    211  1.1  ross 
    212  1.1  ross 	return 0;
    213  1.1  ross }
    214  1.1  ross 
    215  1.1  ross /*
    216  1.1  ross  * Translate a KVA to a PA
    217  1.1  ross  */
    218  1.1  ross int
    219  1.1  ross _kvm_kvatop(kd, va, pa)
    220  1.1  ross 	kvm_t *kd;
    221  1.1  ross 	u_long va;
    222  1.1  ross 	u_long *pa;
    223  1.1  ross {
    224  1.1  ross 	cpu_kcore_hdr_t	*cpu_kh;
    225  1.1  ross 	int		offs;
    226  1.1  ross 	uint32_t	pvr;
    227  1.1  ross 
    228  1.1  ross 	if (ISALIVE(kd)) {
    229  1.1  ross 		_kvm_err(kd, 0, "vatop called in live kernel!");
    230  1.1  ross 		return 0;
    231  1.1  ross 	}
    232  1.1  ross 
    233  1.1  ross 	cpu_kh = kd->cpu_data;
    234  1.1  ross 
    235  1.1  ross 	pvr = (cpu_kh->pvr >> 16);
    236  1.1  ross 
    237  1.1  ross 	switch (pvr) {
    238  1.1  ross 
    239  1.1  ross 	if (_kvm_match_sr(kd, va, pa, &offs))
    240  1.1  ross 		return offs;
    241  1.1  ross 
    242  1.1  ross 	/* No hit -- no translation */
    243  1.1  ross 	*pa = (u_long)~0UL;
    244  1.1  ross 	return 0;
    245  1.1  ross }
    246  1.1  ross 
    247  1.1  ross off_t
    248  1.1  ross _kvm_pa2off(kd, pa)
    249  1.1  ross 	kvm_t *kd;
    250  1.1  ross 	u_long pa;
    251  1.1  ross {
    252  1.1  ross 	cpu_kcore_hdr_t	*cpu_kh;
    253  1.1  ross 	phys_ram_seg_t	*ram;
    254  1.1  ross 	off_t		off;
    255  1.1  ross 	void		*e;
    256  1.1  ross 
    257  1.1  ross 	cpu_kh = kd->cpu_data;
    258  1.1  ross 	e = (char *) kd->cpu_data + kd->cpu_dsize;
    259  1.1  ross         ram = (void *)((char *)(void *)cpu_kh + ALIGN(sizeof *cpu_kh));
    260  1.1  ross 	off = kd->dump_off;
    261  1.1  ross 	do {
    262  1.1  ross 		if (pa >= ram->start && (pa - ram->start) < ram->size) {
    263  1.1  ross 			return off + (pa - ram->start);
    264  1.1  ross 		}
    265  1.1  ross 		ram++;
    266  1.1  ross 		off += ram->size;
    267  1.1  ross 	} while ((void *) ram < e && ram->size);
    268  1.1  ross 
    269  1.1  ross 	_kvm_err(kd, 0, "pa2off failed for pa 0x%08lx\n", pa);
    270  1.1  ross 	return (off_t) -1;
    271  1.1  ross }
    272  1.1  ross 
    273  1.1  ross /*
    274  1.1  ross  * Machine-dependent initialization for ALL open kvm descriptors,
    275  1.1  ross  * not just those for a kernel crash dump.  Some architectures
    276  1.1  ross  * have to deal with these NOT being constants!  (i.e. m68k)
    277  1.1  ross  */
    278  1.1  ross int
    279  1.1  ross _kvm_mdopen(kd)
    280  1.1  ross 	kvm_t	*kd;
    281  1.1  ross {
    282  1.1  ross 	uintptr_t max_uva;
    283  1.1  ross 	extern struct ps_strings *__ps_strings;
    284  1.1  ross 
    285  1.1  ross #if 0   /* XXX - These vary across powerpc machines... */
    286  1.1  ross 	kd->usrstack = USRSTACK;
    287  1.1  ross 	kd->min_uva = VM_MIN_ADDRESS;
    288  1.1  ross 	kd->max_uva = VM_MAXUSER_ADDRESS;
    289  1.1  ross #endif
    290  1.1  ross 	/* This is somewhat hack-ish, but it works. */
    291  1.1  ross 	max_uva = (uintptr_t) (__ps_strings + 1);
    292  1.1  ross 	kd->usrstack = max_uva;
    293  1.1  ross 	kd->max_uva  = max_uva;
    294  1.1  ross 	kd->min_uva  = 0;
    295  1.1  ross 
    296  1.1  ross 	return (0);
    297  1.1  ross }
    298