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