1 1.12 msaitoh /* $NetBSD: db_disasm.c,v 1.12 2024/02/07 04:20:26 msaitoh Exp $ */ 2 1.1 matt 3 1.2 ryo /* 4 1.12 msaitoh * Copyright (c) 2017 Ryo Shimizu 5 1.1 matt * All rights reserved. 6 1.1 matt * 7 1.1 matt * Redistribution and use in source and binary forms, with or without 8 1.1 matt * modification, are permitted provided that the following conditions 9 1.1 matt * are met: 10 1.1 matt * 1. Redistributions of source code must retain the above copyright 11 1.1 matt * notice, this list of conditions and the following disclaimer. 12 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 matt * notice, this list of conditions and the following disclaimer in the 14 1.1 matt * documentation and/or other materials provided with the distribution. 15 1.1 matt * 16 1.2 ryo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.2 ryo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 1.2 ryo * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 1.2 ryo * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 1.2 ryo * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 1.2 ryo * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 1.2 ryo * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.2 ryo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 1.2 ryo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25 1.2 ryo * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 matt * POSSIBILITY OF SUCH DAMAGE. 27 1.1 matt */ 28 1.1 matt 29 1.1 matt #include <sys/cdefs.h> 30 1.12 msaitoh __KERNEL_RCSID(0, "$NetBSD: db_disasm.c,v 1.12 2024/02/07 04:20:26 msaitoh Exp $"); 31 1.9 ryo 32 1.9 ryo #ifdef _KERNEL_OPT 33 1.9 ryo #include "opt_compat_netbsd32.h" 34 1.9 ryo #endif 35 1.2 ryo 36 1.2 ryo #include <sys/param.h> 37 1.2 ryo #include <machine/db_machdep.h> 38 1.2 ryo #include <ddb/db_interface.h> 39 1.2 ryo #include <ddb/db_sym.h> 40 1.2 ryo #include <ddb/db_output.h> 41 1.2 ryo #include <ddb/db_access.h> 42 1.4 jakllsch #include <ddb/db_user.h> 43 1.2 ryo 44 1.9 ryo #include <aarch64/machdep.h> 45 1.2 ryo #include <arch/aarch64/aarch64/disasm.h> 46 1.2 ryo 47 1.11 skrll #include <arm/cpufunc.h> 48 1.11 skrll 49 1.2 ryo static uint32_t 50 1.2 ryo db_disasm_readword(uintptr_t address) 51 1.2 ryo { 52 1.2 ryo return db_get_value(address, sizeof(uint32_t), false); 53 1.2 ryo } 54 1.1 matt 55 1.2 ryo static void 56 1.2 ryo db_disasm_printaddr(uintptr_t address) 57 1.2 ryo { 58 1.2 ryo db_printf("%lx <", address); 59 1.2 ryo db_printsym((db_addr_t)address, DB_STGY_ANY, db_printf); 60 1.2 ryo db_printf(">"); 61 1.2 ryo } 62 1.1 matt 63 1.2 ryo static const disasm_interface_t db_disasm_interface = { 64 1.2 ryo .di_readword = db_disasm_readword, 65 1.2 ryo .di_printaddr = db_disasm_printaddr, 66 1.2 ryo .di_printf = db_printf 67 1.2 ryo }; 68 1.1 matt 69 1.2 ryo db_addr_t 70 1.2 ryo db_disasm(db_addr_t loc, bool altfmt) 71 1.2 ryo { 72 1.2 ryo return disasm(&db_disasm_interface, loc); 73 1.2 ryo } 74 1.3 ryo 75 1.3 ryo 76 1.3 ryo static char *strdisasm_ptr; 77 1.3 ryo static char strdisasm_buf[256]; 78 1.3 ryo 79 1.3 ryo static uint32_t 80 1.3 ryo strdisasm_readword(uintptr_t address) 81 1.3 ryo { 82 1.10 ryo #ifdef _KERNEL 83 1.8 ryo /* 84 1.8 ryo * if it cannot be read due to a EFAULT etc., 85 1.8 ryo * ignores the error and returns 0 86 1.8 ryo */ 87 1.8 ryo uint32_t word = 0; 88 1.8 ryo 89 1.8 ryo switch (aarch64_addressspace((vaddr_t)address)) { 90 1.8 ryo case AARCH64_ADDRSPACE_UPPER: 91 1.8 ryo kcopy((void*)address, &word, sizeof(word)); 92 1.8 ryo break; 93 1.8 ryo case AARCH64_ADDRSPACE_LOWER: 94 1.8 ryo ufetch_32((uint32_t *)address, &word); 95 1.8 ryo break; 96 1.8 ryo default: 97 1.8 ryo break; 98 1.8 ryo } 99 1.8 ryo 100 1.8 ryo return word; 101 1.10 ryo #else 102 1.10 ryo return *(uint32_t *)address; 103 1.10 ryo #endif 104 1.3 ryo } 105 1.3 ryo 106 1.7 joerg static void __printflike(1, 2) 107 1.3 ryo strdisasm_printf(const char *fmt, ...) 108 1.3 ryo { 109 1.3 ryo va_list ap; 110 1.3 ryo int len; 111 1.3 ryo 112 1.3 ryo /* calculation spaces to append a string */ 113 1.3 ryo len = strdisasm_buf + sizeof(strdisasm_buf) - strdisasm_ptr; 114 1.3 ryo 115 1.3 ryo va_start(ap, fmt); 116 1.3 ryo len = vsnprintf(strdisasm_ptr, len, fmt, ap); 117 1.3 ryo va_end(ap); 118 1.3 ryo 119 1.3 ryo strdisasm_ptr += len; 120 1.3 ryo } 121 1.3 ryo 122 1.3 ryo static void 123 1.3 ryo strdisasm_printaddr(uintptr_t address) 124 1.3 ryo { 125 1.3 ryo strdisasm_printf("0x%lx", address); 126 1.3 ryo } 127 1.3 ryo 128 1.3 ryo static const disasm_interface_t strdisasm_interface = { 129 1.3 ryo .di_readword = strdisasm_readword, 130 1.3 ryo .di_printaddr = strdisasm_printaddr, 131 1.3 ryo .di_printf = strdisasm_printf 132 1.3 ryo }; 133 1.3 ryo 134 1.3 ryo const char * 135 1.9 ryo strdisasm(vaddr_t pc, uint64_t spsr) 136 1.3 ryo { 137 1.9 ryo #ifdef COMPAT_NETBSD32 138 1.9 ryo if (spsr & SPSR_A32) { 139 1.9 ryo uint32_t insn = 0; 140 1.9 ryo int size; 141 1.9 ryo const char *arch = (spsr & SPSR_A32_T) ? "T32" : "A32"; 142 1.9 ryo 143 1.9 ryo size = fetch_arm_insn(pc, spsr, &insn); 144 1.9 ryo if (size != 2) 145 1.9 ryo size = 4; 146 1.9 ryo snprintf(strdisasm_buf, sizeof(strdisasm_buf), 147 1.9 ryo ".insn 0x%0*x (%s)", size * 2, insn, arch); 148 1.9 ryo } else 149 1.9 ryo #endif /* COMPAT_NETBSD32 */ 150 1.9 ryo { 151 1.9 ryo char *p; 152 1.9 ryo 153 1.9 ryo /* disasm an aarch64 instruction */ 154 1.9 ryo strdisasm_ptr = strdisasm_buf; 155 1.9 ryo disasm(&strdisasm_interface, (db_addr_t)pc); 156 1.9 ryo 157 1.9 ryo /* replace tab to space, and chomp '\n' */ 158 1.9 ryo for (p = strdisasm_buf; *p != '\0'; p++) { 159 1.9 ryo if (*p == '\t') 160 1.9 ryo *p = ' '; 161 1.9 ryo } 162 1.9 ryo if ((p > strdisasm_buf) && (p[-1] == '\n')) 163 1.9 ryo p[-1] = '\0'; 164 1.3 ryo } 165 1.3 ryo 166 1.3 ryo return strdisasm_buf; 167 1.3 ryo } 168