Home | History | Annotate | Line # | Download | only in libkvm
kvm_m68k_cmn.c revision 1.1
      1  1.1  gwr /*	$NetBSD: kvm_m68k_cmn.c,v 1.1 1997/03/21 18:44:24 gwr Exp $	*/
      2  1.1  gwr 
      3  1.1  gwr /*-
      4  1.1  gwr  * Copyright (c) 1989, 1992, 1993
      5  1.1  gwr  *	The Regents of the University of California.  All rights reserved.
      6  1.1  gwr  *
      7  1.1  gwr  * This code is derived from software developed by the Computer Systems
      8  1.1  gwr  * Engineering group at Lawrence Berkeley Laboratory under DARPA contract
      9  1.1  gwr  * BG 91-66 and contributed to Berkeley.
     10  1.1  gwr  *
     11  1.1  gwr  * Redistribution and use in source and binary forms, with or without
     12  1.1  gwr  * modification, are permitted provided that the following conditions
     13  1.1  gwr  * are met:
     14  1.1  gwr  * 1. Redistributions of source code must retain the above copyright
     15  1.1  gwr  *    notice, this list of conditions and the following disclaimer.
     16  1.1  gwr  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  gwr  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  gwr  *    documentation and/or other materials provided with the distribution.
     19  1.1  gwr  * 3. All advertising materials mentioning features or use of this software
     20  1.1  gwr  *    must display the following acknowledgement:
     21  1.1  gwr  *	This product includes software developed by the University of
     22  1.1  gwr  *	California, Berkeley and its contributors.
     23  1.1  gwr  * 4. Neither the name of the University nor the names of its contributors
     24  1.1  gwr  *    may be used to endorse or promote products derived from this software
     25  1.1  gwr  *    without specific prior written permission.
     26  1.1  gwr  *
     27  1.1  gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1  gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1  gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1  gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1  gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1  gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1  gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1  gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1  gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1  gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1  gwr  * SUCH DAMAGE.
     38  1.1  gwr  */
     39  1.1  gwr 
     40  1.1  gwr #if defined(LIBC_SCCS) && !defined(lint)
     41  1.1  gwr #if 0
     42  1.1  gwr static char sccsid[] = "@(#)kvm_hp300.c	8.1 (Berkeley) 6/4/93";
     43  1.1  gwr #else
     44  1.1  gwr static char *rcsid = "$NetBSD: kvm_m68k_cmn.c,v 1.1 1997/03/21 18:44:24 gwr Exp $";
     45  1.1  gwr #endif
     46  1.1  gwr #endif /* LIBC_SCCS and not lint */
     47  1.1  gwr 
     48  1.1  gwr /*
     49  1.1  gwr  * Common m68k machine dependent routines for kvm.
     50  1.1  gwr  *
     51  1.1  gwr  * Note: This file has to build on ALL m68k machines,
     52  1.1  gwr  * so do NOT include any <machine/*.h> files here.
     53  1.1  gwr  */
     54  1.1  gwr 
     55  1.1  gwr #include <sys/types.h>
     56  1.1  gwr #include <sys/kcore.h>
     57  1.1  gwr 
     58  1.1  gwr #include <unistd.h>
     59  1.1  gwr #include <limits.h>
     60  1.1  gwr #include <nlist.h>
     61  1.1  gwr #include <kvm.h>
     62  1.1  gwr #include <db.h>
     63  1.1  gwr 
     64  1.1  gwr /* XXX: Avoid <machine/pte.h> etc. (see below) */
     65  1.1  gwr typedef u_int pt_entry_t;		/* page table entry */
     66  1.1  gwr typedef u_int st_entry_t;		/* segment table entry */
     67  1.1  gwr 
     68  1.1  gwr #include <m68k/cpu.h>
     69  1.1  gwr #include <m68k/kcore.h>
     70  1.1  gwr 
     71  1.1  gwr #include "kvm_private.h"
     72  1.1  gwr #include "kvm_m68k.h"
     73  1.1  gwr 
     74  1.1  gwr int   _kvm_cmn_initvtop __P((kvm_t *));
     75  1.1  gwr void  _kvm_cmn_freevtop __P((kvm_t *));
     76  1.1  gwr int	  _kvm_cmn_kvatop   __P((kvm_t *, u_long, u_long *));
     77  1.1  gwr off_t _kvm_cmn_pa2off   __P((kvm_t *, u_long));
     78  1.1  gwr 
     79  1.1  gwr struct kvm_ops _kvm_ops_cmn = {
     80  1.1  gwr 	_kvm_cmn_initvtop,
     81  1.1  gwr 	_kvm_cmn_freevtop,
     82  1.1  gwr 	_kvm_cmn_kvatop,
     83  1.1  gwr 	_kvm_cmn_pa2off };
     84  1.1  gwr 
     85  1.1  gwr static int vatop_030 __P((kvm_t *, st_entry_t *, ulong, ulong *));
     86  1.1  gwr static int vatop_040 __P((kvm_t *, st_entry_t *, ulong, ulong *));
     87  1.1  gwr 
     88  1.1  gwr /*
     89  1.1  gwr  * XXX: I don't like this, but until all arch/.../include files
     90  1.1  gwr  * are exported into some user-accessable place, there is no
     91  1.1  gwr  * convenient alternative to copying these definitions here.
     92  1.1  gwr  */
     93  1.1  gwr 
     94  1.1  gwr /* Things from param.h */
     95  1.1  gwr #define PGSHIFT	13
     96  1.1  gwr #define NBPG	(1<<13)
     97  1.1  gwr #define PGOFSET (NBPG-1)
     98  1.1  gwr #define	btop(x)		(((unsigned)(x)) >> PGSHIFT)
     99  1.1  gwr 
    100  1.1  gwr /* Things from pte.h */
    101  1.1  gwr 
    102  1.1  gwr /* All variants */
    103  1.1  gwr #define SG_V		 2
    104  1.1  gwr #define	PG_NV		0x00000000
    105  1.1  gwr #define PG_FRAME	0xffffe000
    106  1.1  gwr 
    107  1.1  gwr /* MC68030 with MMU TCR set for 8/11/13 (bits) */
    108  1.1  gwr #define SG3_SHIFT	24	/* a.k.a SEGSHIFT */
    109  1.1  gwr #define SG3_FRAME	0xffffe000
    110  1.1  gwr #define SG3_PMASK	0x00ffe000
    111  1.1  gwr 
    112  1.1  gwr /* MC68040 with MMU set for 8K page size. */
    113  1.1  gwr #define SG4_MASK1	0xfe000000
    114  1.1  gwr #define SG4_SHIFT1	25
    115  1.1  gwr #define SG4_MASK2	0x01fc0000
    116  1.1  gwr #define SG4_SHIFT2	18
    117  1.1  gwr #define SG4_MASK3	0x0003e000
    118  1.1  gwr #define SG4_SHIFT3	13
    119  1.1  gwr #define SG4_ADDR1	0xfffffe00
    120  1.1  gwr #define SG4_ADDR2	0xffffff80
    121  1.1  gwr 
    122  1.1  gwr 
    123  1.1  gwr 
    124  1.1  gwr #define KREAD(kd, addr, p)\
    125  1.1  gwr 	(kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p)))
    126  1.1  gwr 
    127  1.1  gwr void
    128  1.1  gwr _kvm_cmn_freevtop(kd)
    129  1.1  gwr 	kvm_t *kd;
    130  1.1  gwr {
    131  1.1  gwr 	if (kd->vmst != 0)
    132  1.1  gwr 		free(kd->vmst);
    133  1.1  gwr }
    134  1.1  gwr 
    135  1.1  gwr int
    136  1.1  gwr _kvm_cmn_initvtop(kd)
    137  1.1  gwr 	kvm_t *kd;
    138  1.1  gwr {
    139  1.1  gwr 
    140  1.1  gwr 	return (0);
    141  1.1  gwr }
    142  1.1  gwr 
    143  1.1  gwr int
    144  1.1  gwr _kvm_cmn_kvatop(kd, va, pa)
    145  1.1  gwr 	kvm_t *kd;
    146  1.1  gwr 	u_long va;
    147  1.1  gwr 	u_long *pa;
    148  1.1  gwr {
    149  1.1  gwr 	register cpu_kcore_hdr_t *cpu_kh;
    150  1.1  gwr 	int (*vtopf) __P((kvm_t *, st_entry_t *, ulong, ulong *));
    151  1.1  gwr 
    152  1.1  gwr 	if (ISALIVE(kd)) {
    153  1.1  gwr 		_kvm_err(kd, 0, "vatop called in live kernel!");
    154  1.1  gwr 		return (0);
    155  1.1  gwr 	}
    156  1.1  gwr 
    157  1.1  gwr 	cpu_kh = kd->cpu_data;
    158  1.1  gwr 	switch (cpu_kh->mmutype) {
    159  1.1  gwr 
    160  1.1  gwr 	case MMU_68030:
    161  1.1  gwr 		vtopf = vatop_030;
    162  1.1  gwr 		break;
    163  1.1  gwr 
    164  1.1  gwr 	case MMU_68040:
    165  1.1  gwr 		vtopf = vatop_040;
    166  1.1  gwr 		break;
    167  1.1  gwr 
    168  1.1  gwr 	default:
    169  1.1  gwr 		_kvm_err(kd, 0, "vatop unknown MMU type!");
    170  1.1  gwr 		return (0);
    171  1.1  gwr 	}
    172  1.1  gwr 
    173  1.1  gwr 	return ((*vtopf)(kd, cpu_kh->sysseg_pa, va, pa));
    174  1.1  gwr }
    175  1.1  gwr 
    176  1.1  gwr /*
    177  1.1  gwr  * Translate a physical address to a file-offset in the crash-dump.
    178  1.1  gwr  */
    179  1.1  gwr off_t
    180  1.1  gwr _kvm_cmn_pa2off(kd, pa)
    181  1.1  gwr 	kvm_t	*kd;
    182  1.1  gwr 	u_long	pa;
    183  1.1  gwr {
    184  1.1  gwr 	off_t		off;
    185  1.1  gwr 	phys_ram_seg_t	*rsp;
    186  1.1  gwr 	register cpu_kcore_hdr_t *cpu_kh;
    187  1.1  gwr 
    188  1.1  gwr 	cpu_kh = kd->cpu_data;
    189  1.1  gwr 	off = 0;
    190  1.1  gwr 	for (rsp = cpu_kh->ram_segs; rsp->size; rsp++) {
    191  1.1  gwr 		if (pa >= rsp->start && pa < rsp->start + rsp->size) {
    192  1.1  gwr 			pa -= rsp->start;
    193  1.1  gwr 			break;
    194  1.1  gwr 		}
    195  1.1  gwr 		off += rsp->size;
    196  1.1  gwr 	}
    197  1.1  gwr 	return(kd->dump_off + off + pa);
    198  1.1  gwr }
    199  1.1  gwr 
    200  1.1  gwr /*****************************************************************
    201  1.1  gwr  * Local stuff...
    202  1.1  gwr  */
    203  1.1  gwr 
    204  1.1  gwr static int
    205  1.1  gwr vatop_030(kd, sta, va, pa)
    206  1.1  gwr 	kvm_t *kd;
    207  1.1  gwr 	st_entry_t *sta;
    208  1.1  gwr 	u_long va;
    209  1.1  gwr 	u_long *pa;
    210  1.1  gwr {
    211  1.1  gwr 	register cpu_kcore_hdr_t *cpu_kh;
    212  1.1  gwr 	register u_long addr;
    213  1.1  gwr 	int p, ste, pte;
    214  1.1  gwr 	int offset;
    215  1.1  gwr 
    216  1.1  gwr 	offset = va & PGOFSET;
    217  1.1  gwr 	cpu_kh = kd->cpu_data;
    218  1.1  gwr 
    219  1.1  gwr 	/*
    220  1.1  gwr 	 * If we are initializing (kernel segment table pointer not yet set)
    221  1.1  gwr 	 * then return pa == va to avoid infinite recursion.
    222  1.1  gwr 	 */
    223  1.1  gwr 	if (cpu_kh->sysseg_pa == 0) {
    224  1.1  gwr 		*pa = va + cpu_kh->kernel_pa;
    225  1.1  gwr 		return (NBPG - offset);
    226  1.1  gwr 	}
    227  1.1  gwr 
    228  1.1  gwr 	addr = (u_long)&sta[va >> SG3_SHIFT];
    229  1.1  gwr 	/*
    230  1.1  gwr 	 * Can't use KREAD to read kernel segment table entries.
    231  1.1  gwr 	 * Fortunately it is 1-to-1 mapped so we don't have to.
    232  1.1  gwr 	 */
    233  1.1  gwr 	if (sta == cpu_kh->sysseg_pa) {
    234  1.1  gwr 		if (lseek(kd->pmfd, _kvm_cmn_pa2off(kd, addr), 0) == -1 ||
    235  1.1  gwr 			read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
    236  1.1  gwr 			goto invalid;
    237  1.1  gwr 	} else if (KREAD(kd, addr, &ste))
    238  1.1  gwr 		goto invalid;
    239  1.1  gwr 	if ((ste & SG_V) == 0) {
    240  1.1  gwr 		_kvm_err(kd, 0, "invalid segment (%x)", ste);
    241  1.1  gwr 		return((off_t)0);
    242  1.1  gwr 	}
    243  1.1  gwr 	p = btop(va & SG3_PMASK);
    244  1.1  gwr 	addr = (ste & SG3_FRAME) + (p * sizeof(pt_entry_t));
    245  1.1  gwr 
    246  1.1  gwr 	/*
    247  1.1  gwr 	 * Address from STE is a physical address so don't use kvm_read.
    248  1.1  gwr 	 */
    249  1.1  gwr 	if (lseek(kd->pmfd, _kvm_cmn_pa2off(kd, addr), 0) == -1 ||
    250  1.1  gwr 	    read(kd->pmfd, (char *)&pte, sizeof(pte)) < 0)
    251  1.1  gwr 		goto invalid;
    252  1.1  gwr 	addr = pte & PG_FRAME;
    253  1.1  gwr 	if (pte == PG_NV) {
    254  1.1  gwr 		_kvm_err(kd, 0, "page not valid");
    255  1.1  gwr 		return (0);
    256  1.1  gwr 	}
    257  1.1  gwr 	*pa = addr + offset;
    258  1.1  gwr 
    259  1.1  gwr 	return (NBPG - offset);
    260  1.1  gwr invalid:
    261  1.1  gwr 	_kvm_err(kd, 0, "invalid address (%x)", va);
    262  1.1  gwr 	return (0);
    263  1.1  gwr }
    264  1.1  gwr 
    265  1.1  gwr static int
    266  1.1  gwr vatop_040(kd, sta, va, pa)
    267  1.1  gwr 	kvm_t *kd;
    268  1.1  gwr 	st_entry_t *sta;
    269  1.1  gwr 	u_long va;
    270  1.1  gwr 	u_long *pa;
    271  1.1  gwr {
    272  1.1  gwr 	register cpu_kcore_hdr_t *cpu_kh;
    273  1.1  gwr 	register u_long addr;
    274  1.1  gwr 	st_entry_t *sta2;
    275  1.1  gwr 	int p, ste, pte;
    276  1.1  gwr 	int offset;
    277  1.1  gwr 
    278  1.1  gwr 	offset = va & PGOFSET;
    279  1.1  gwr 	cpu_kh = kd->cpu_data;
    280  1.1  gwr 	/*
    281  1.1  gwr 	 * If we are initializing (kernel segment table pointer not yet set)
    282  1.1  gwr 	 * then return pa == va to avoid infinite recursion.
    283  1.1  gwr 	 */
    284  1.1  gwr 	if (cpu_kh->sysseg_pa == 0) {
    285  1.1  gwr 		*pa = va + cpu_kh->kernel_pa;
    286  1.1  gwr 		return (NBPG - offset);
    287  1.1  gwr 	}
    288  1.1  gwr 
    289  1.1  gwr 	addr = (u_long)&sta[va >> SG4_SHIFT1];
    290  1.1  gwr 	/*
    291  1.1  gwr 	 * Can't use KREAD to read kernel segment table entries.
    292  1.1  gwr 	 * Fortunately it is 1-to-1 mapped so we don't have to.
    293  1.1  gwr 	 */
    294  1.1  gwr 	if (sta == cpu_kh->sysseg_pa) {
    295  1.1  gwr 		if (lseek(kd->pmfd, _kvm_cmn_pa2off(kd, addr), 0) == -1 ||
    296  1.1  gwr 			read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
    297  1.1  gwr 			goto invalid;
    298  1.1  gwr 	} else if (KREAD(kd, addr, &ste))
    299  1.1  gwr 		goto invalid;
    300  1.1  gwr 	if ((ste & SG_V) == 0) {
    301  1.1  gwr 		_kvm_err(kd, 0, "invalid level 1 descriptor (%x)",
    302  1.1  gwr 				 ste);
    303  1.1  gwr 		return((off_t)0);
    304  1.1  gwr 	}
    305  1.1  gwr 	sta2 = (st_entry_t *)(ste & SG4_ADDR1);
    306  1.1  gwr 	addr = (u_long)&sta2[(va & SG4_MASK2) >> SG4_SHIFT2];
    307  1.1  gwr 	/*
    308  1.1  gwr 	 * Address from level 1 STE is a physical address,
    309  1.1  gwr 	 * so don't use kvm_read.
    310  1.1  gwr 	 */
    311  1.1  gwr 	if (lseek(kd->pmfd, _kvm_cmn_pa2off(kd, addr), 0) == -1 ||
    312  1.1  gwr 		read(kd->pmfd, (char *)&ste, sizeof(ste)) < 0)
    313  1.1  gwr 		goto invalid;
    314  1.1  gwr 	if ((ste & SG_V) == 0) {
    315  1.1  gwr 		_kvm_err(kd, 0, "invalid level 2 descriptor (%x)",
    316  1.1  gwr 				 ste);
    317  1.1  gwr 		return((off_t)0);
    318  1.1  gwr 	}
    319  1.1  gwr 	sta2 = (st_entry_t *)(ste & SG4_ADDR2);
    320  1.1  gwr 	addr = (u_long)&sta2[(va & SG4_MASK3) >> SG4_SHIFT3];
    321  1.1  gwr 
    322  1.1  gwr 
    323  1.1  gwr 	/*
    324  1.1  gwr 	 * Address from STE is a physical address so don't use kvm_read.
    325  1.1  gwr 	 */
    326  1.1  gwr 	if (lseek(kd->pmfd, _kvm_cmn_pa2off(kd, addr), 0) == -1 ||
    327  1.1  gwr 	    read(kd->pmfd, (char *)&pte, sizeof(pte)) < 0)
    328  1.1  gwr 		goto invalid;
    329  1.1  gwr 	addr = pte & PG_FRAME;
    330  1.1  gwr 	if (pte == PG_NV) {
    331  1.1  gwr 		_kvm_err(kd, 0, "page not valid");
    332  1.1  gwr 		return (0);
    333  1.1  gwr 	}
    334  1.1  gwr 	*pa = addr + offset;
    335  1.1  gwr 
    336  1.1  gwr 	return (NBPG - offset);
    337  1.1  gwr invalid:
    338  1.1  gwr 	_kvm_err(kd, 0, "invalid address (%x)", va);
    339  1.1  gwr 	return (0);
    340  1.1  gwr }
    341