1 /* $OpenBSD: kvm_amd64.c,v 1.5 2007/01/08 18:54:12 deraadt Exp $ */ 2 /* $NetBSD: kvm_x86_64.c,v 1.3 2002/06/05 22:01:55 fvdl 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. 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 #include <sys/cdefs.h> 38 39 /* 40 * x86-64 machine dependent routines for kvm. 41 */ 42 43 #include <sys/param.h> 44 #include <sys/user.h> 45 #include <sys/proc.h> 46 #include <sys/stat.h> 47 #include <sys/kcore.h> 48 #include <machine/kcore.h> 49 #include <stdlib.h> 50 #include <unistd.h> 51 #include <nlist.h> 52 #include <kvm.h> 53 54 #include <uvm/uvm_extern.h> 55 56 #include <limits.h> 57 #include <db.h> 58 59 #include "kvm_private.h" 60 61 #include <machine/pmap.h> 62 #include <machine/pte.h> 63 #include <machine/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 != NULL) { 71 free(kd->vmst); 72 kd->vmst = NULL; 73 } 74 } 75 76 /*ARGSUSED*/ 77 int 78 _kvm_initvtop(kvm_t *kd) 79 { 80 81 return (0); 82 } 83 84 /* 85 * Translate a kernel virtual address to a physical address. 86 */ 87 int 88 _kvm_kvatop(kvm_t *kd, u_long va, paddr_t *pa) 89 { 90 cpu_kcore_hdr_t *cpu_kh; 91 paddr_t pde_pa, pte_pa; 92 u_long page_off; 93 pd_entry_t pde; 94 pt_entry_t pte; 95 96 if (ISALIVE(kd)) { 97 _kvm_err(kd, 0, "vatop called in live kernel!"); 98 return (0); 99 } 100 101 cpu_kh = kd->cpu_data; 102 page_off = va & PGOFSET; 103 104 /* 105 * Find and read all entries to get to the pa. 106 */ 107 108 /* 109 * Level 4. 110 */ 111 pde_pa = cpu_kh->ptdpaddr + (pl4_pi(va) * sizeof(pd_entry_t)); 112 if (pread(kd->pmfd, (void *)&pde, sizeof(pde), 113 _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) { 114 _kvm_syserr(kd, 0, "could not read PT level 4 entry"); 115 goto lose; 116 } 117 if ((pde & PG_V) == 0) { 118 _kvm_err(kd, 0, "invalid translation (invalid level 4 PDE)"); 119 goto lose; 120 } 121 122 /* 123 * Level 3. 124 */ 125 pde_pa = (pde & PG_FRAME) + (pl3_pi(va) * sizeof(pd_entry_t)); 126 if (pread(kd->pmfd, (void *)&pde, sizeof(pde), 127 _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) { 128 _kvm_syserr(kd, 0, "could not read PT level 3 entry"); 129 goto lose; 130 } 131 if ((pde & PG_V) == 0) { 132 _kvm_err(kd, 0, "invalid translation (invalid level 3 PDE)"); 133 goto lose; 134 } 135 136 /* 137 * Level 2. 138 */ 139 pde_pa = (pde & PG_FRAME) + (pl2_pi(va) * sizeof(pd_entry_t)); 140 if (pread(kd->pmfd, (void *)&pde, sizeof(pde), 141 _kvm_pa2off(kd, pde_pa)) != sizeof(pde)) { 142 _kvm_syserr(kd, 0, "could not read PT level 2 entry"); 143 goto lose; 144 } 145 if ((pde & PG_V) == 0) { 146 _kvm_err(kd, 0, "invalid translation (invalid level 2 PDE)"); 147 goto lose; 148 } 149 150 151 /* 152 * Level 1. 153 */ 154 pte_pa = (pde & PG_FRAME) + (pl1_pi(va) * sizeof(pt_entry_t)); 155 if (pread(kd->pmfd, (void *) &pte, sizeof(pte), 156 _kvm_pa2off(kd, pte_pa)) != sizeof(pte)) { 157 _kvm_syserr(kd, 0, "could not read PTE"); 158 goto lose; 159 } 160 /* 161 * Validate the PTE and return the physical address. 162 */ 163 if ((pte & PG_V) == 0) { 164 _kvm_err(kd, 0, "invalid translation (invalid PTE)"); 165 goto lose; 166 } 167 *pa = (pte & PG_FRAME) + page_off; 168 return (int)(NBPG - page_off); 169 170 lose: 171 *pa = (u_long)~0L; 172 return (0); 173 } 174 175 /* 176 * Translate a physical address to a file-offset in the crash dump. 177 */ 178 off_t 179 _kvm_pa2off(kvm_t *kd, paddr_t pa) 180 { 181 cpu_kcore_hdr_t *cpu_kh; 182 phys_ram_seg_t *ramsegs; 183 off_t off; 184 int i; 185 186 cpu_kh = kd->cpu_data; 187 ramsegs = (void *)((char *)(void *)cpu_kh + ALIGN(sizeof *cpu_kh)); 188 189 off = 0; 190 for (i = 0; i < cpu_kh->nmemsegs; i++) { 191 if (pa >= ramsegs[i].start && 192 (pa - ramsegs[i].start) < ramsegs[i].size) { 193 off += (pa - ramsegs[i].start); 194 break; 195 } 196 off += ramsegs[i].size; 197 } 198 199 return (kd->dump_off + off); 200 } 201