1/* $NetBSD: kvm_aarch64.c,v 1.12 2023/08/23 14:00:11 rin Exp $ */ 2 3/*- 4 * Copyright (c) 2014, 2018 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Matt Thomas of 3am Software Foundry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/param.h> 33#include <sys/proc.h> 34#include <sys/stat.h> 35#include <sys/kcore.h> 36#include <sys/types.h> 37#include <unistd.h> 38#include <nlist.h> 39#include <kvm.h> 40 41#include <machine/kcore.h> 42#include <machine/armreg.h> 43#include <machine/pte.h> 44#include <machine/vmparam.h> 45 46#include <limits.h> 47#include <db.h> 48#include <stdlib.h> 49 50#include "kvm_private.h" 51 52__RCSID("$NetBSD: kvm_aarch64.c,v 1.12 2023/08/23 14:00:11 rin Exp $"); 53 54/*ARGSUSED*/ 55void 56_kvm_freevtop(kvm_t *kd) 57{ 58 return; 59} 60 61/*ARGSUSED*/ 62int 63_kvm_initvtop(kvm_t *kd) 64{ 65 return (0); 66} 67 68int 69_kvm_kvatop(kvm_t *kd, vaddr_t va, paddr_t *pa) 70{ 71 if (ISALIVE(kd)) { 72 _kvm_err(kd, 0, "vatop called in live kernel!"); 73 return(0); 74 } 75 76 if ((va & AARCH64_DIRECTMAP_MASK) != AARCH64_DIRECTMAP_START) { 77 /* 78 * Bogus address (not in KV space): punt. 79 */ 80 _kvm_err(kd, 0, "invalid kernel virtual address"); 81lose: 82 *pa = -1; 83 return 0; 84 } 85 86 const cpu_kcore_hdr_t * const cpu_kh = kd->cpu_data; 87 const uint64_t tg1 = cpu_kh->kh_tcr1 & TCR_TG1; 88 const u_int t1siz = __SHIFTOUT(cpu_kh->kh_tcr1, TCR_T1SZ); 89 const u_int inputsz = 64 - t1siz; 90 91 /* 92 * Real kernel virtual address: do the translation. 93 */ 94 95 u_int page_shift; 96 97 switch (tg1) { 98 case TCR_TG1_4KB: 99 page_shift = 12; 100 break; 101 case TCR_TG1_16KB: 102 page_shift = 14; 103 break; 104 case TCR_TG1_64KB: 105 page_shift = 16; 106 break; 107 default: 108 goto lose; 109 } 110 111 const size_t page_size = 1 << page_shift; 112 const uint64_t page_mask = __BITS(page_shift - 1, 0); 113 const uint64_t page_addr = __BITS(47, page_shift); 114 const u_int pte_shift = page_shift - 3; 115 116 /* how many levels of page tables do we have? */ 117 u_int levels = howmany(inputsz - page_shift, pte_shift); 118 119 /* restrict va to the valid VA bits */ 120 va &= __BITS(inputsz - 1, 0); 121 122 u_int addr_shift = page_shift + (levels - 1) * pte_shift; 123 124 /* clear out the unused low bits of the table address */ 125 paddr_t pte_addr = cpu_kh->kh_ttbr1 & TTBR_BADDR; 126 127 for (;;) { 128 pt_entry_t pte; 129 130 /* now index into the pte table */ 131 const uint64_t idx_mask = __BITS(addr_shift + pte_shift - 1, 132 addr_shift); 133 pte_addr += 8 * __SHIFTOUT(va, idx_mask); 134 135 /* Find and read the PTE. */ 136 if (_kvm_pread(kd, kd->pmfd, &pte, sizeof(pte), 137 _kvm_pa2off(kd, pte_addr)) != sizeof(pte)) { 138 _kvm_syserr(kd, 0, "could not read pte"); 139 goto lose; 140 } 141 142 /* Find and read the L2 PTE. */ 143 if ((pte & LX_VALID) == 0) { 144 _kvm_err(kd, 0, "invalid translation (invalid pte)"); 145 goto lose; 146 } 147 148 if ((pte & LX_TYPE) == LX_TYPE_BLK) { 149 const size_t blk_size = 1 << addr_shift; 150 const uint64_t blk_mask = __BITS(addr_shift - 1, 0); 151 152 *pa = (pte & page_addr & ~blk_mask) | (va & blk_mask); 153 return blk_size - (va & blk_mask); 154 } 155 if (--levels == 0) { 156 *pa = (pte & page_addr) | (va & page_mask); 157 return page_size - (va & page_mask); 158 } 159 160 /* 161 * Read next level of page table 162 */ 163 164 pte_addr = pte & page_addr; 165 addr_shift -= pte_shift; 166 } 167} 168 169/* 170 * Translate a physical address to a file-offset in the crash dump. 171 */ 172off_t 173_kvm_pa2off(kvm_t *kd, paddr_t pa) 174{ 175 const cpu_kcore_hdr_t * const cpu_kh = kd->cpu_data; 176 off_t off = 0; 177 178 for (const phys_ram_seg_t *ramsegs = cpu_kh->kh_ramsegs; 179 ramsegs->size != 0; ramsegs++) { 180 if (pa >= ramsegs->start 181 && pa < ramsegs->start + ramsegs->size) { 182 off += pa - ramsegs->start; 183 break; 184 } 185 off += ramsegs->size; 186 } 187 188 return kd->dump_off + off; 189} 190 191/* 192 * Machine-dependent initialization for ALL open kvm descriptors, 193 * not just those for a kernel crash dump. Some architectures 194 * have to deal with these NOT being constants! (i.e. m68k) 195 */ 196int 197_kvm_mdopen(kvm_t *kd) 198{ 199 200 kd->min_uva = VM_MIN_ADDRESS; 201 kd->max_uva = VM_MAXUSER_ADDRESS; 202 203 return (0); 204} 205