Home | History | Annotate | Line # | Download | only in libkvm
kvm_mips.c revision 1.18.16.1
      1 /* $NetBSD: kvm_mips.c,v 1.18.16.1 2010/01/28 17:16:42 matt 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.18.16.1 2010/01/28 17:16:42 matt 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(kd)
     67 	kvm_t *kd;
     68 {
     69 
     70 	/* Not actually used for anything right now, but safe. */
     71 	if (kd->vmst != 0)
     72 		free(kd->vmst);
     73 }
     74 
     75 int
     76 _kvm_initvtop(kd)
     77 	kvm_t *kd;
     78 {
     79 
     80 	return (0);
     81 }
     82 
     83 /*
     84  * Translate a kernel virtual address to a physical address.
     85  */
     86 int
     87 _kvm_kvatop(kd, va, pa)
     88 	kvm_t *kd;
     89 	u_long va;
     90 	u_long *pa;
     91 {
     92 	cpu_kcore_hdr_t *cpu_kh;
     93 	int page_off;
     94 	u_int pte;
     95 	u_long pte_pa;
     96 
     97 	if (ISALIVE(kd)) {
     98 		_kvm_err(kd, 0, "vatop called in live kernel!");
     99 		return((off_t)0);
    100 	}
    101 
    102 	cpu_kh = kd->cpu_data;
    103 	page_off = va & PGOFSET;
    104 
    105 #ifdef _LP64
    106 	if (MIPS_XKPHYS_P(va)) {
    107 		/*
    108 		 * Direct-mapped cached address: just convert it.
    109 		 */
    110 		*pa = MIPS_XKPHYS_TO_PHYS(va);
    111 		return (NBPG - page_off);
    112 	}
    113 
    114 	if (va < MIPS_XKPHYS_START) {
    115 		/*
    116 		 * XUSEG (user virtual address space) - invalid.
    117 		 */
    118 		_kvm_err(kd, 0, "invalid kernel virtual address");
    119 		goto lose;
    120 	}
    121 #else
    122 	if (va < MIPS_KSEG0_START) {
    123 		/*
    124 		 * KUSEG (user virtual address space) - invalid.
    125 		 */
    126 		_kvm_err(kd, 0, "invalid kernel virtual address");
    127 		goto lose;
    128 	}
    129 #endif
    130 
    131 	if (MIPS_KSEG0_P(va)) {
    132 		/*
    133 		 * Direct-mapped cached address: just convert it.
    134 		 */
    135 		*pa = MIPS_KSEG0_TO_PHYS(va);
    136 		return (NBPG - page_off);
    137 	}
    138 
    139 	if (MIPS_KSEG1_P(va)) {
    140 		/*
    141 		 * Direct-mapped uncached address: just convert it.
    142 		 */
    143 		*pa = MIPS_KSEG1_TO_PHYS(va);
    144 		return (NBPG - page_off);
    145 	}
    146 
    147 #ifdef _LP64
    148 	if (va >= MIPS_KSEG2_START) {
    149 		/*
    150 		 * KUSEG (user virtual address space) - invalid.
    151 		 */
    152 		_kvm_err(kd, 0, "invalid kernel virtual address");
    153 		goto lose;
    154 	}
    155 #endif
    156 
    157 	/*
    158 	 * We now know that we're a KSEG2 (kernel virtually mapped)
    159 	 * address.  Translate the address using the pmap's kernel
    160 	 * page table.
    161 	 */
    162 
    163 	/*
    164 	 * Step 1: Make sure the kernel page table has a translation
    165 	 * for the address.
    166 	 */
    167 #ifdef _LP64
    168 	if (va >= (MIPS_XKSEG_START + (cpu_kh->sysmapsize * NBPG))) {
    169 		_kvm_err(kd, 0, "invalid XKSEG address");
    170 		goto lose;
    171 	}
    172 #else
    173 	if (va >= (MIPS_KSEG2_START + (cpu_kh->sysmapsize * NBPG))) {
    174 		_kvm_err(kd, 0, "invalid KSEG2 address");
    175 		goto lose;
    176 	}
    177 #endif
    178 
    179 	/*
    180 	 * Step 2: Locate and read the PTE.
    181 	 */
    182 	pte_pa = cpu_kh->sysmappa +
    183 	    (((va - MIPS_KSEG2_START) >> PGSHIFT) * sizeof(u_int));
    184 	if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte),
    185 	    _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) {
    186 		_kvm_syserr(kd, 0, "could not read PTE");
    187 		goto lose;
    188 	}
    189 
    190 	/*
    191 	 * Step 3: Validate the PTE and return the physical address.
    192 	 */
    193 	if ((pte & cpu_kh->pg_v) == 0) {
    194 		_kvm_err(kd, 0, "invalid translation (invalid PTE)");
    195 		goto lose;
    196 	}
    197 	*pa = (((pte & cpu_kh->pg_frame) >> cpu_kh->pg_shift) << PGSHIFT) +
    198 	    page_off;
    199 	return (NBPG - page_off);
    200 
    201  lose:
    202 	*pa = -1;
    203 	return (0);
    204 }
    205 
    206 /*
    207  * Translate a physical address to a file-offset in the crash dump.
    208  */
    209 off_t
    210 _kvm_pa2off(kd, pa)
    211 	kvm_t *kd;
    212 	u_long pa;
    213 {
    214 	cpu_kcore_hdr_t *cpu_kh;
    215 	phys_ram_seg_t *ramsegs;
    216 	off_t off;
    217 	int i;
    218 
    219 	cpu_kh = kd->cpu_data;
    220 	ramsegs = (phys_ram_seg_t *)((char *)cpu_kh + ALIGN(sizeof *cpu_kh));
    221 
    222 	off = 0;
    223 	for (i = 0; i < cpu_kh->nmemsegs; i++) {
    224 		if (pa >= ramsegs[i].start &&
    225 		    (pa - ramsegs[i].start) < ramsegs[i].size) {
    226 			off += (pa - ramsegs[i].start);
    227 			break;
    228 		}
    229 		off += ramsegs[i].size;
    230 	}
    231 
    232 	return (kd->dump_off + off);
    233 }
    234 
    235 /*
    236  * Machine-dependent initialization for ALL open kvm descriptors,
    237  * not just those for a kernel crash dump.  Some architectures
    238  * have to deal with these NOT being constants!  (i.e. m68k)
    239  */
    240 int
    241 _kvm_mdopen(kd)
    242 	kvm_t	*kd;
    243 {
    244 
    245 	kd->usrstack = USRSTACK;
    246 	kd->min_uva = VM_MIN_ADDRESS;
    247 	kd->max_uva = VM_MAXUSER_ADDRESS;
    248 
    249 	return (0);
    250 }
    251