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