1 /* $OpenBSD: kvm_mips64.c,v 1.7 2007/10/18 04:32:05 miod Exp $ */ 2 /* $NetBSD: kvm_mips.c,v 1.3 1996/03/18 22:33:44 thorpej Exp $ */ 3 4 /*- 5 * Copyright (c) 1989, 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software developed by the Computer Systems 9 * Engineering group at Lawrence Berkeley Laboratory under DARPA contract 10 * BG 91-66 and contributed to Berkeley. Modified for MIPS by Ralph Campbell. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #if defined(LIBC_SCCS) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)kvm_mips.c 8.1 (Berkeley) 6/4/93"; 40 #else 41 static char *rcsid = "$OpenBSD: kvm_mips64.c,v 1.7 2007/10/18 04:32:05 miod Exp $"; 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 /* 46 * MIPS machine dependent routines for kvm. Hopefully, the forthcoming 47 * vm code will one day obsolete this module. 48 */ 49 50 #include <sys/param.h> 51 #include <sys/user.h> 52 #include <sys/proc.h> 53 #include <sys/stat.h> 54 #include <unistd.h> 55 #include <stdlib.h> 56 #include <nlist.h> 57 #include <kvm.h> 58 59 #include <limits.h> 60 #include <db.h> 61 62 #include "kvm_private.h" 63 64 #include <machine/cpu.h> 65 #include <machine/pte.h> 66 #include <machine/pmap.h> 67 68 struct vmstate { 69 pt_entry_t *Sysmap; 70 u_int Sysmapsize; 71 }; 72 73 #define KREAD(kd, addr, p)\ 74 (kvm_read(kd, addr, (char *)(p), sizeof(*(p))) != sizeof(*(p))) 75 76 void 77 _kvm_freevtop(kvm_t *kd) 78 { 79 if (kd->vmst != 0) 80 free(kd->vmst); 81 } 82 83 int 84 _kvm_initvtop(kvm_t *kd) 85 { 86 struct vmstate *vm; 87 struct nlist nlist[3]; 88 89 vm = (struct vmstate *)_kvm_malloc(kd, sizeof(*vm)); 90 if (vm == 0) 91 return (-1); 92 kd->vmst = vm; 93 94 nlist[0].n_name = "Sysmap"; 95 nlist[1].n_name = "Sysmapsize"; 96 nlist[2].n_name = 0; 97 98 if (kvm_nlist(kd, nlist) != 0) { 99 _kvm_err(kd, kd->program, "bad namelist"); 100 return (-1); 101 } 102 if (KREAD(kd, (u_long)nlist[0].n_value, &vm->Sysmap)) { 103 _kvm_err(kd, kd->program, "cannot read Sysmap"); 104 return (-1); 105 } 106 if (KREAD(kd, (u_long)nlist[1].n_value, &vm->Sysmapsize)) { 107 _kvm_err(kd, kd->program, "cannot read Sysmapsize"); 108 return (-1); 109 } 110 return (0); 111 } 112 113 /* 114 * Translate a kernel virtual address to a physical address. 115 */ 116 int 117 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa) 118 { 119 struct vmstate *vm; 120 pt_entry_t pte; 121 u_long idx, addr, offset; 122 123 if (ISALIVE(kd)) { 124 _kvm_err(kd, 0, "vatop called in live kernel!"); 125 return((off_t)0); 126 } 127 vm = kd->vmst; 128 offset = va & PAGE_MASK; 129 /* 130 * If we are initializing (kernel segment table pointer not yet set) 131 * then return pa == va to avoid infinite recursion. 132 */ 133 if (vm->Sysmap == 0) { 134 *pa = va; 135 return (int)(PAGE_SIZE - offset); 136 } 137 /* 138 * Check for direct-mapped segments 139 */ 140 if (IS_XKPHYS(va)) { 141 *pa = XKPHYS_TO_PHYS(va); 142 return (int)(PAGE_SIZE - offset); 143 } 144 if (va >= (vaddr_t)KSEG0_BASE && va < (vaddr_t)KSSEG_BASE) { 145 *pa = KSEG0_TO_PHYS(va); 146 return (int)(PAGE_SIZE - offset); 147 } 148 if (va < VM_MIN_KERNEL_ADDRESS) 149 goto invalid; 150 idx = (va - VM_MIN_KERNEL_ADDRESS) >> PGSHIFT; 151 if (idx >= vm->Sysmapsize) 152 goto invalid; 153 addr = (u_long)(vm->Sysmap + idx); 154 /* 155 * Can't use KREAD to read kernel segment table entries. 156 * Fortunately it is 1-to-1 mapped so we don't have to. 157 */ 158 if (_kvm_pread(kd, kd->pmfd, (char *)&pte, sizeof(pte), 159 (off_t)addr) < 0) 160 goto invalid; 161 if (!(pte & PG_V)) 162 goto invalid; 163 *pa = (pte & PG_FRAME) | offset; 164 return (int)(PAGE_SIZE - offset); 165 166 invalid: 167 _kvm_err(kd, 0, "invalid address (%lx)", va); 168 return (0); 169 } 170 171 off_t 172 _kvm_pa2off(kvm_t *kd, paddr_t pa) 173 { 174 _kvm_err(kd, 0, "pa2off going to be implemented!"); 175 return 0; 176 } 177