Home | History | Annotate | Line # | Download | only in include
      1 /* $NetBSD: db_machdep.h,v 1.17 2026/01/09 07:37:13 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 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 /*-
     33  * Copyright (c) 2014 Andrew Turner
     34  * Copyright (c) 2014-2015 The FreeBSD Foundation
     35  * All rights reserved.
     36  *
     37  * This software was developed by Semihalf under
     38  * sponsorship from the FreeBSD Foundation.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  * SUCH DAMAGE.
     60  *
     61  * $FreeBSD: head/sys/arm64/include/db_machdep.h 316001 2017-03-26 18:46:35Z bde $
     62  */
     63 
     64 #ifndef _AARCH64_DB_MACHDEP_H_
     65 #define _AARCH64_DB_MACHDEP_H_
     66 
     67 #ifdef __aarch64__
     68 
     69 #include <sys/types.h>
     70 #include <sys/endian.h>
     71 
     72 #include <uvm/uvm.h>
     73 
     74 #include <aarch64/frame.h>
     75 #include <aarch64/pmap.h>
     76 
     77 #include <ddb/db_user.h>
     78 
     79 typedef long long int db_expr_t;
     80 #define DDB_EXPR_FMT "ll"
     81 typedef uintptr_t db_addr_t;
     82 
     83 #define BKPT_ADDR(addr)		(addr)
     84 #define BKPT_SIZE		4
     85 #ifdef __AARCH64EB__
     86 #define BKPT_INSN		0x000020d4	/* brk #0 */
     87 #else
     88 #define BKPT_INSN		0xd4200000	/* brk #0 */
     89 #endif
     90 #define BKPT_SET(insn, addr)	(BKPT_INSN)
     91 
     92 typedef struct trapframe db_regs_t;
     93 extern db_regs_t ddb_regs;
     94 #define DDB_REGS		(&ddb_regs)
     95 #define PC_REGS(tf)		((tf)->tf_pc)
     96 
     97 int kdb_trap(int, struct trapframe *);
     98 #define DB_TRAP_UNKNOWN		0
     99 #define DB_TRAP_BREAKPOINT	1
    100 #define DB_TRAP_BKPT_INSN	2
    101 #define DB_TRAP_WATCHPOINT	3
    102 #define DB_TRAP_SW_STEP		4
    103 
    104 #define IS_BREAKPOINT_TRAP(type, code) \
    105 	((type) == DB_TRAP_BKPT_INSN)
    106 #define IS_WATCHPOINT_TRAP(type, code) \
    107 	((type) == DB_TRAP_BREAKPOINT || (type) == DB_TRAP_WATCHPOINT)
    108 
    109 static inline bool
    110 inst_return(db_expr_t insn)
    111 {
    112 	LE32TOH(insn);
    113 	return ((insn & 0xfffffc1f) == 0xd65f0000);	/* ret xN */
    114 }
    115 
    116 static inline bool
    117 inst_trap_return(db_expr_t insn)
    118 {
    119 	LE32TOH(insn);
    120 	return insn == 0xd69f03e0;			/* eret */
    121 }
    122 
    123 static inline bool
    124 inst_call(db_expr_t insn)
    125 {
    126 	LE32TOH(insn);
    127 	return ((insn & 0xfc000000) == 0x94000000)	/* bl */
    128 	    || ((insn & 0xfffffc1f) == 0xd63f0000);	/* blr */
    129 }
    130 
    131 static inline bool
    132 inst_load(db_expr_t insn)
    133 {
    134 	LE32TOH(insn);
    135 	return
    136 	    ((insn & 0xffe00c00) == 0xb8800000) ||	/* ldursw */
    137 	    /* ldrsw imm{pre,post}idx */
    138 	    ((insn & 0xffe00400) == 0xb8800400) ||
    139 	    ((insn & 0xffc00c00) == 0xb8800800) ||	/* ldrsw reg,ldtrsw */
    140 	    ((insn & 0xffc00000) == 0xb9800000) ||	/* ldrsw immunsign */
    141 	    ((insn & 0xffc00000) == 0x39400000) ||	/* ldrb immunsign */
    142 	    ((insn & 0xff000000) == 0x98000000) ||	/* ldrsw literal */
    143 	    /* ldpsw {pre,post}idx */
    144 	    ((insn & 0xfec00000) == 0x68c00000) ||
    145 	    /* ldrh immunsign,ldpsw signed */
    146 	    ((insn & 0xefc00000) == 0x69400000) ||
    147 	    ((insn & 0xbfff0000) == 0x887f0000) ||	/* ldaxp,ldxp */
    148 	    ((insn & 0xbfc00000) == 0xb9400000) ||	/* ldr immunsign */
    149 	    ((insn & 0xbfa00c00) == 0x38800000) ||	/* ldursh,ldursb */
    150 	    /* ldrsh imm{pre,post}idx,ldrsb imm{pre,post}idx */
    151 	    ((insn & 0xbfa00400) == 0x38800400) ||
    152 	    /* ldrs[bh] reg,ldtrs[bh] */
    153 	    ((insn & 0xbf800c00) == 0x38800800) ||
    154 	    /* ldrsh immunsign,ldrsb immunsign */
    155 	    ((insn & 0xbf800000) == 0x39800000) ||
    156 	    ((insn & 0xbf000000) == 0x18000000) ||	/* ldr literal */
    157 	    /* ldp {pre,post}idx,ldp signed,ldnp */
    158 	    ((insn & 0x7e400000) == 0x28400000) ||
    159 	    ((insn & 0x3ffffc00) == 0x085f7c00) ||	/* ldxr,ldxr[bh] */
    160 	    ((insn & 0x3fe00c00) == 0x38400000) ||	/* ldur,ldur[bh] */
    161 	    /* ldr imm{pre,post}idx,ldr[bh]_imm{pre,post}idx */
    162 	    ((insn & 0x3fe00400) == 0x38400400) ||
    163 	    /* ldtr,ldtr[bh],ldr_reg,ldr[bh]_reg */
    164 	    ((insn & 0x3fc00c00) == 0x38400800) ||
    165 	    /* ldaxr,ldaxr[bh],ldar,ldar[bh] */
    166 	    ((insn & 0x3f7ffc00) == 0x085ffc00);
    167 }
    168 
    169 static inline bool
    170 inst_store(db_expr_t insn)
    171 {
    172 	LE32TOH(insn);
    173 	return
    174 	    ((insn & 0xbfe00000) == 0x88200000) ||	/* stlxp,stxp */
    175 	    /* stp {pre,post}idx,stp signed,stnp */
    176 	    ((insn & 0x7e400000) == 0x28000000) ||
    177 	    ((insn & 0x3ffffc00) == 0x089ffc00) ||	/* stlr,stlr[bh] */
    178 	    /* stlxr,stlxr[bh],stxr,stxr[bh] */
    179 	    ((insn & 0x3fe07c00) == 0x08007c00) ||
    180 	    ((insn & 0x3fe00c00) == 0x38000000) ||	/* stur,stur[bh] */
    181 	    /* str imm{pre,post}idx,str[bh] imm{pre,post}idx */
    182 	    ((insn & 0x3fe00400) == 0x38000400) ||
    183 	    /* str reg,str[bh] reg,sttr,sttr[bh] */
    184 	    ((insn & 0x3fc00c00) == 0x38000800) ||
    185 	    /* str immunsign,str[bh] immunsign */
    186 	    ((insn & 0x3fc00000) == 0x39000000);
    187 }
    188 
    189 #define SOFTWARE_SSTEP
    190 
    191 #ifdef SOFTWARE_SSTEP
    192 
    193 static inline bool
    194 inst_branch(db_expr_t insn)
    195 {
    196 	LE32TOH(insn);
    197 	return
    198 	    ((insn & 0xff000010) == 0x54000000) ||	/* b.cond */
    199 	    ((insn & 0xfc000000) == 0x14000000) ||	/* b imm */
    200 	    ((insn & 0xfffffc1f) == 0xd61f0000) ||	/* br */
    201 	    ((insn & 0x7f000000) == 0x35000000) ||	/* cbnz */
    202 	    ((insn & 0x7f000000) == 0x34000000) ||	/* cbz */
    203 	    ((insn & 0x7f000000) == 0x37000000) ||	/* tbnz */
    204 	    ((insn & 0x7f000000) == 0x36000000);	/* tbz */
    205 }
    206 
    207 bool db_inst_unconditional_flow_transfer(db_expr_t);
    208 db_addr_t db_branch_taken(db_expr_t, db_addr_t, db_regs_t *);
    209 
    210 #define next_instr_address(pc, bd)		\
    211 	    ((bd) ? (pc) : ((pc) + 4))
    212 #define branch_taken(ins, pc, regs)		\
    213 	    db_branch_taken((ins), (pc), (regs))
    214 #define inst_unconditional_flow_transfer(ins)	\
    215 	    db_inst_unconditional_flow_transfer(ins)
    216 
    217 #endif /* SOFTWARE_SSTEP */
    218 
    219 #define DB_MACHINE_COMMANDS
    220 
    221 #ifdef _KERNEL
    222 void db_pteinfo(vaddr_t, void (*)(const char *, ...) __printflike(1, 2));
    223 void db_pte_print(pt_entry_t, int, void (*)(const char *, ...) __printflike(1, 2));
    224 void db_ttbrdump(bool, vaddr_t, void (*pr)(const char *, ...) __printflike(1, 2));
    225 void db_machdep_cpu_init(void);
    226 void db_machdep_init(struct cpu_info * const);
    227 
    228 /* hardware breakpoint/watchpoint functions */
    229 void aarch64_breakpoint_set(int, vaddr_t);
    230 void aarch64_watchpoint_set(int, vaddr_t, u_int, u_int);
    231 #define WATCHPOINT_ACCESS_LOAD		0x01
    232 #define WATCHPOINT_ACCESS_STORE		0x02
    233 #define WATCHPOINT_ACCESS_LOADSTORE	0x03
    234 #define WATCHPOINT_ACCESS_MASK		0x03
    235 #endif
    236 
    237 void dump_trapframe(struct trapframe *, void (*)(const char *, ...) __printflike(1, 2));
    238 
    239 void dump_switchframe(struct trapframe *, void (*)(const char *, ...) __printflike(1, 2));
    240 const char *strdisasm(vaddr_t, uint64_t);
    241 
    242 #define DB_ELF_SYMBOLS
    243 
    244 #elif defined(__arm__)
    245 
    246 #include <arm/db_machdep.h>
    247 
    248 #endif
    249 
    250 #endif /* _AARCH64_DB_MACHDEP_H_ */
    251