Home | History | Annotate | Line # | Download | only in libkvm
kvm_mips.c revision 1.16.2.2
      1  1.16.2.2  matt /* $NetBSD: kvm_mips.c,v 1.16.2.2 2001/08/05 03:33:16 matt Exp $ */
      2  1.16.2.2  matt 
      3  1.16.2.2  matt /*
      4  1.16.2.2  matt  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
      5  1.16.2.2  matt  * All rights reserved.
      6  1.16.2.2  matt  *
      7  1.16.2.2  matt  * Author: Chris G. Demetriou
      8  1.16.2.2  matt  *
      9  1.16.2.2  matt  * Permission to use, copy, modify and distribute this software and
     10  1.16.2.2  matt  * its documentation is hereby granted, provided that both the copyright
     11  1.16.2.2  matt  * notice and this permission notice appear in all copies of the
     12  1.16.2.2  matt  * software, derivative works or modified versions, and any portions
     13  1.16.2.2  matt  * thereof, and that both notices appear in supporting documentation.
     14  1.16.2.2  matt  *
     15  1.16.2.2  matt  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  1.16.2.2  matt  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  1.16.2.2  matt  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  1.16.2.2  matt  *
     19  1.16.2.2  matt  * Carnegie Mellon requests users of this software to return to
     20  1.16.2.2  matt  *
     21  1.16.2.2  matt  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  1.16.2.2  matt  *  School of Computer Science
     23  1.16.2.2  matt  *  Carnegie Mellon University
     24  1.16.2.2  matt  *  Pittsburgh PA 15213-3890
     25  1.16.2.2  matt  *
     26  1.16.2.2  matt  * any improvements or extensions that they make and grant Carnegie the
     27  1.16.2.2  matt  * rights to redistribute these changes.
     28  1.16.2.2  matt  */
     29  1.16.2.2  matt 
     30  1.16.2.2  matt /*
     31  1.16.2.2  matt  * Modified for NetBSD/mips by Jason R. Thorpe, Numerical Aerospace
     32  1.16.2.2  matt  * Simulation Facility, NASA Ames Research Center.
     33  1.16.2.2  matt  */
     34  1.16.2.2  matt 
     35  1.16.2.2  matt #include <sys/cdefs.h>
     36  1.16.2.2  matt #if defined(LIBC_SCCS) && !defined(lint)
     37  1.16.2.2  matt __RCSID("$NetBSD: kvm_mips.c,v 1.16.2.2 2001/08/05 03:33:16 matt Exp $");
     38  1.16.2.2  matt #endif /* LIBC_SCCS and not lint */
     39  1.16.2.2  matt 
     40  1.16.2.2  matt /*
     41  1.16.2.2  matt  * MIPS machine dependent routines for kvm.
     42  1.16.2.2  matt  */
     43  1.16.2.2  matt 
     44  1.16.2.2  matt #include <sys/param.h>
     45  1.16.2.2  matt #include <sys/user.h>
     46  1.16.2.2  matt #include <sys/proc.h>
     47  1.16.2.2  matt #include <sys/stat.h>
     48  1.16.2.2  matt #include <sys/kcore.h>
     49  1.16.2.2  matt #include <machine/kcore.h>
     50  1.16.2.2  matt #include <stdlib.h>
     51  1.16.2.2  matt #include <unistd.h>
     52  1.16.2.2  matt #include <nlist.h>
     53  1.16.2.2  matt #include <kvm.h>
     54  1.16.2.2  matt 
     55  1.16.2.2  matt #include <uvm/uvm_extern.h>
     56  1.16.2.2  matt 
     57  1.16.2.2  matt #include <limits.h>
     58  1.16.2.2  matt #include <db.h>
     59  1.16.2.2  matt 
     60  1.16.2.2  matt #include "kvm_private.h"
     61  1.16.2.2  matt 
     62  1.16.2.2  matt #include <mips/cpuregs.h>
     63  1.16.2.2  matt #include <mips/vmparam.h>
     64  1.16.2.2  matt 
     65  1.16.2.2  matt void
     66  1.16.2.2  matt _kvm_freevtop(kd)
     67  1.16.2.2  matt 	kvm_t *kd;
     68  1.16.2.2  matt {
     69  1.16.2.2  matt 
     70  1.16.2.2  matt 	/* Not actually used for anything right now, but safe. */
     71  1.16.2.2  matt 	if (kd->vmst != 0)
     72  1.16.2.2  matt 		free(kd->vmst);
     73  1.16.2.2  matt }
     74  1.16.2.2  matt 
     75  1.16.2.2  matt int
     76  1.16.2.2  matt _kvm_initvtop(kd)
     77  1.16.2.2  matt 	kvm_t *kd;
     78  1.16.2.2  matt {
     79  1.16.2.2  matt 
     80  1.16.2.2  matt 	return (0);
     81  1.16.2.2  matt }
     82  1.16.2.2  matt 
     83  1.16.2.2  matt /*
     84  1.16.2.2  matt  * Translate a kernel virtual address to a physical address.
     85  1.16.2.2  matt  */
     86  1.16.2.2  matt int
     87  1.16.2.2  matt _kvm_kvatop(kd, va, pa)
     88  1.16.2.2  matt 	kvm_t *kd;
     89  1.16.2.2  matt 	u_long va;
     90  1.16.2.2  matt 	u_long *pa;
     91  1.16.2.2  matt {
     92  1.16.2.2  matt 	cpu_kcore_hdr_t *cpu_kh;
     93  1.16.2.2  matt 	int page_off;
     94  1.16.2.2  matt 	u_int pte;
     95  1.16.2.2  matt 	u_long pte_pa;
     96  1.16.2.2  matt 
     97  1.16.2.2  matt 	if (ISALIVE(kd)) {
     98  1.16.2.2  matt 		_kvm_err(kd, 0, "vatop called in live kernel!");
     99  1.16.2.2  matt 		return((off_t)0);
    100  1.16.2.2  matt 	}
    101  1.16.2.2  matt 
    102  1.16.2.2  matt 	cpu_kh = kd->cpu_data;
    103  1.16.2.2  matt 	page_off = va & PGOFSET;
    104  1.16.2.2  matt 
    105  1.16.2.2  matt 	if (va < MIPS_KSEG0_START) {
    106  1.16.2.2  matt 		/*
    107  1.16.2.2  matt 		 * KUSEG (user virtual address space) - invalid.
    108  1.16.2.2  matt 		 */
    109  1.16.2.2  matt 		_kvm_err(kd, 0, "invalid kernel virtual address");
    110  1.16.2.2  matt 		goto lose;
    111  1.16.2.2  matt 	}
    112  1.16.2.2  matt 
    113  1.16.2.2  matt 	if (va >= MIPS_KSEG0_START && va < MIPS_KSEG1_START) {
    114  1.16.2.2  matt 		/*
    115  1.16.2.2  matt 		 * Direct-mapped cached address: just convert it.
    116  1.16.2.2  matt 		 */
    117  1.16.2.2  matt 		*pa = MIPS_KSEG0_TO_PHYS(va);
    118  1.16.2.2  matt 		return (NBPG - page_off);
    119  1.16.2.2  matt 	}
    120  1.16.2.2  matt 
    121  1.16.2.2  matt 	if (va >= MIPS_KSEG1_START && va < MIPS_KSEG2_START) {
    122  1.16.2.2  matt 		/*
    123  1.16.2.2  matt 		 * Direct-mapped uncached address: just convert it.
    124  1.16.2.2  matt 		 */
    125  1.16.2.2  matt 		*pa = MIPS_KSEG1_TO_PHYS(va);
    126  1.16.2.2  matt 		return (NBPG - page_off);
    127  1.16.2.2  matt 	}
    128  1.16.2.2  matt 
    129  1.16.2.2  matt 	/*
    130  1.16.2.2  matt 	 * We now know that we're a KSEG2 (kernel virtually mapped)
    131  1.16.2.2  matt 	 * address.  Translate the address using the pmap's kernel
    132  1.16.2.2  matt 	 * page table.
    133  1.16.2.2  matt 	 */
    134  1.16.2.2  matt 
    135  1.16.2.2  matt 	/*
    136  1.16.2.2  matt 	 * Step 1: Make sure the kernel page table has a translation
    137  1.16.2.2  matt 	 * for the address.
    138  1.16.2.2  matt 	 */
    139  1.16.2.2  matt 	if (va >= (MIPS_KSEG2_START + (cpu_kh->sysmapsize * NBPG))) {
    140  1.16.2.2  matt 		_kvm_err(kd, 0, "invalid KSEG2 address");
    141  1.16.2.2  matt 		goto lose;
    142  1.16.2.2  matt 	}
    143  1.16.2.2  matt 
    144  1.16.2.2  matt 	/*
    145  1.16.2.2  matt 	 * Step 2: Locate and read the PTE.
    146  1.16.2.2  matt 	 */
    147  1.16.2.2  matt 	pte_pa = cpu_kh->sysmappa +
    148  1.16.2.2  matt 	    (((va - MIPS_KSEG2_START) >> PGSHIFT) * sizeof(u_int));
    149  1.16.2.2  matt 	if (pread(kd->pmfd, &pte, sizeof(pte), _kvm_pa2off(kd, pte_pa)) !=
    150  1.16.2.2  matt 	    sizeof(pte)) {
    151  1.16.2.2  matt 		_kvm_syserr(kd, 0, "could not read PTE");
    152  1.16.2.2  matt 		goto lose;
    153  1.16.2.2  matt 	}
    154  1.16.2.2  matt 
    155  1.16.2.2  matt 	/*
    156  1.16.2.2  matt 	 * Step 3: Validate the PTE and return the physical address.
    157  1.16.2.2  matt 	 */
    158  1.16.2.2  matt 	if ((pte & cpu_kh->pg_v) == 0) {
    159  1.16.2.2  matt 		_kvm_err(kd, 0, "invalid translation (invalid PTE)");
    160  1.16.2.2  matt 		goto lose;
    161  1.16.2.2  matt 	}
    162  1.16.2.2  matt 	*pa = (((pte & cpu_kh->pg_frame) >> cpu_kh->pg_shift) << PGSHIFT) +
    163  1.16.2.2  matt 	    page_off;
    164  1.16.2.2  matt 	return (NBPG - page_off);
    165  1.16.2.2  matt 
    166  1.16.2.2  matt  lose:
    167  1.16.2.2  matt 	*pa = -1;
    168  1.16.2.2  matt 	return (0);
    169  1.16.2.2  matt }
    170  1.16.2.2  matt 
    171  1.16.2.2  matt /*
    172  1.16.2.2  matt  * Translate a physical address to a file-offset in the crash-dump.
    173  1.16.2.2  matt  */
    174  1.16.2.2  matt off_t
    175  1.16.2.2  matt _kvm_pa2off(kd, pa)
    176  1.16.2.2  matt 	kvm_t *kd;
    177  1.16.2.2  matt 	u_long pa;
    178  1.16.2.2  matt {
    179  1.16.2.2  matt 	cpu_kcore_hdr_t *cpu_kh;
    180  1.16.2.2  matt 	phys_ram_seg_t *ramsegs;
    181  1.16.2.2  matt 	off_t off;
    182  1.16.2.2  matt 	int i;
    183  1.16.2.2  matt 
    184  1.16.2.2  matt 	cpu_kh = kd->cpu_data;
    185  1.16.2.2  matt 	ramsegs = (phys_ram_seg_t *)((char *)cpu_kh + ALIGN(sizeof *cpu_kh));
    186  1.16.2.2  matt 
    187  1.16.2.2  matt 	off = 0;
    188  1.16.2.2  matt 	for (i = 0; i < cpu_kh->nmemsegs; i++) {
    189  1.16.2.2  matt 		if (pa >= ramsegs[i].start &&
    190  1.16.2.2  matt 		    (pa - ramsegs[i].start) < ramsegs[i].size) {
    191  1.16.2.2  matt 			off += (pa - ramsegs[i].start);
    192  1.16.2.2  matt 			break;
    193  1.16.2.2  matt 		}
    194  1.16.2.2  matt 		off += ramsegs[i].size;
    195  1.16.2.2  matt 	}
    196  1.16.2.2  matt 
    197  1.16.2.2  matt 	return (kd->dump_off + off);
    198  1.16.2.2  matt }
    199  1.16.2.2  matt 
    200  1.16.2.2  matt /*
    201  1.16.2.2  matt  * Machine-dependent initialization for ALL open kvm descriptors,
    202  1.16.2.2  matt  * not just those for a kernel crash dump.  Some architectures
    203  1.16.2.2  matt  * have to deal with these NOT being constants!  (i.e. m68k)
    204  1.16.2.2  matt  */
    205  1.16.2.2  matt int
    206  1.16.2.2  matt _kvm_mdopen(kd)
    207  1.16.2.2  matt 	kvm_t	*kd;
    208  1.16.2.2  matt {
    209  1.16.2.2  matt 
    210  1.16.2.2  matt 	kd->usrstack = USRSTACK;
    211  1.16.2.2  matt 	kd->min_uva = VM_MIN_ADDRESS;
    212  1.16.2.2  matt 	kd->max_uva = VM_MAXUSER_ADDRESS;
    213  1.16.2.2  matt 
    214  1.16.2.2  matt 	return (0);
    215  1.16.2.2  matt }
    216