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