Home | History | Annotate | Line # | Download | only in include
db_machdep.h revision 1.3
      1 /* $NetBSD: db_machdep.h,v 1.3 2018/04/01 04:35:03 ryo 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 <aarch64/frame.h>
     71 
     72 typedef long long int db_expr_t;
     73 #define DDB_EXPR_FMT "ll"
     74 typedef uintptr_t db_addr_t;
     75 
     76 #define BKPT_ADDR(addr)		(addr)
     77 #define BKPT_SIZE		4
     78 #define BKPT_INSN		0xd4200000	/* brk #0 */
     79 #define BKPT_SET(insn, addr)	(BKPT_INSN)
     80 
     81 typedef struct trapframe db_regs_t;
     82 extern db_regs_t ddb_regs;
     83 #define DDB_REGS		(&ddb_regs)
     84 #define PC_REGS(tf)		((tf)->tf_pc)
     85 
     86 int kdb_trap(int, struct trapframe *);
     87 #define DB_TRAP_UNKNOWN		0
     88 #define DB_TRAP_BREAKPOINT	1
     89 #define DB_TRAP_BKPT_INSN	2
     90 #define DB_TRAP_WATCHPOINT	3
     91 #define DB_TRAP_SW_STEP		4
     92 
     93 #define IS_BREAKPOINT_TRAP(type, code) \
     94 	((type) == DB_TRAP_BREAKPOINT || (type) == DB_TRAP_BKPT_INSN)
     95 #define IS_WATCHPOINT_TRAP(type, code) \
     96 	((type) == DB_TRAP_WATCHPOINT)
     97 
     98 static inline bool
     99 inst_return(db_expr_t insn)
    100 {
    101 	return ((insn & 0xfffffc1f) == 0xd65f0000);	/* ret xN */
    102 }
    103 
    104 static inline bool
    105 inst_trap_return(db_expr_t insn)
    106 {
    107 	return insn == 0xd69f03e0;			/* eret */
    108 }
    109 
    110 static inline bool
    111 inst_call(db_expr_t insn)
    112 {
    113 	return ((insn & 0xfc000000) == 0x94000000)	/* bl */
    114 	    || ((insn & 0xfffffc1f) == 0xd63f0000);	/* blr */
    115 }
    116 
    117 static inline bool
    118 inst_load(db_expr_t insn)
    119 {
    120 	return
    121 	    ((((insn) & 0x3b000000) == 0x18000000) || /* literal */
    122 	     (((insn) & 0x3f400000) == 0x08400000) || /* exclusive */
    123 	     (((insn) & 0x3bc00000) == 0x28400000) || /* no-allocate pair */
    124 	     ((((insn) & 0x3b200c00) == 0x38000400) &&
    125 	      (((insn) & 0x3be00c00) != 0x38000400) &&
    126 	      (((insn) & 0xffe00c00) != 0x3c800400)) || /* imm post-indexed */
    127 	     ((((insn) & 0x3b200c00) == 0x38000c00) &&
    128 	      (((insn) & 0x3be00c00) != 0x38000c00) &&
    129 	      (((insn) & 0xffe00c00) != 0x3c800c00)) || /* imm pre-indexed */
    130 	     ((((insn) & 0x3b200c00) == 0x38200800) &&
    131 	      (((insn) & 0x3be00c00) != 0x38200800) &&
    132 	      (((insn) & 0xffe00c00) != 0x3ca00c80)) || /* register offset */
    133 	     ((((insn) & 0x3b200c00) == 0x38000800) &&
    134 	      (((insn) & 0x3be00c00) != 0x38000800)) || /* unprivileged */
    135 	     ((((insn) & 0x3b200c00) == 0x38000000) &&
    136 	      (((insn) & 0x3be00c00) != 0x38000000) &&
    137 	      (((insn) & 0xffe00c00) != 0x3c800000)) || /* unscaled imm */
    138 	     ((((insn) & 0x3b000000) == 0x39000000) &&
    139 	      (((insn) & 0x3bc00000) != 0x39000000) &&
    140 	      (((insn) & 0xffc00000) != 0x3d800000)) || /* unsigned imm */
    141 	     (((insn) & 0x3bc00000) == 0x28400000) || /* pair (offset) */
    142 	     (((insn) & 0x3bc00000) == 0x28c00000) || /* pair (post-indexed) */
    143 	     (((insn) & 0x3bc00000) == 0x29800000)); /* pair (pre-indexed) */
    144 }
    145 
    146 static inline bool
    147 inst_store(db_expr_t insn)
    148 {
    149 	return
    150 	    ((((insn) & 0x3f400000) == 0x08000000) || /* exclusive */
    151 	     (((insn) & 0x3bc00000) == 0x28000000) || /* no-allocate pair */
    152 	     ((((insn) & 0x3be00c00) == 0x38000400) ||
    153 	      (((insn) & 0xffe00c00) == 0x3c800400)) || /* imm post-indexed */
    154 	     ((((insn) & 0x3be00c00) == 0x38000c00) ||
    155 	      (((insn) & 0xffe00c00) == 0x3c800c00)) || /* imm pre-indexed */
    156 	     ((((insn) & 0x3be00c00) == 0x38200800) ||
    157 	      (((insn) & 0xffe00c00) == 0x3ca00800)) || /* register offset */
    158 	     (((insn) & 0x3be00c00) == 0x38000800) ||  /* unprivileged */
    159 	     ((((insn) & 0x3be00c00) == 0x38000000) ||
    160 	      (((insn) & 0xffe00c00) == 0x3c800000)) || /* unscaled imm */
    161 	     ((((insn) & 0x3bc00000) == 0x39000000) ||
    162 	      (((insn) & 0xffc00000) == 0x3d800000)) || /* unsigned imm */
    163 	     (((insn) & 0x3bc00000) == 0x28000000) || /* pair (offset) */
    164 	     (((insn) & 0x3bc00000) == 0x28800000) || /* pair (post-indexed) */
    165 	     (((insn) & 0x3bc00000) == 0x29800000)); /* pair (pre-indexed) */
    166 }
    167 
    168 #define SOFTWARE_SSTEP
    169 
    170 #ifdef SOFTWARE_SSTEP
    171 
    172 static inline bool
    173 inst_branch(db_expr_t insn)
    174 {
    175 	return
    176 	    ((insn & 0xff000010) == 0x54000000) ||	/* b.cond */
    177 	    ((insn & 0xfc000000) == 0x14000000) ||	/* b imm */
    178 	    ((insn & 0xfffffc1f) == 0xd61f0000) ||	/* br */
    179 	    ((insn & 0x7f000000) == 0x35000000) ||	/* cbnz */
    180 	    ((insn & 0x7f000000) == 0x34000000) ||	/* cbz */
    181 	    ((insn & 0x7f000000) == 0x37000000) ||	/* tbnz */
    182 	    ((insn & 0x7f000000) == 0x36000000);	/* tbz */
    183 }
    184 
    185 bool db_inst_unconditional_flow_transfer(db_expr_t);
    186 db_addr_t db_branch_taken(db_expr_t, db_addr_t, db_regs_t *);
    187 
    188 #define next_instr_address(pc, bd)		\
    189 	    ((bd) ? (pc) : ((pc) + 4))
    190 #define branch_taken(ins, pc, regs)		\
    191 	    db_branch_taken((ins), (pc), (regs))
    192 #define inst_unconditional_flow_transfer(ins)	\
    193 	    db_inst_unconditional_flow_transfer(ins)
    194 
    195 #endif /* SOFTWARE_SSTEP */
    196 
    197 #define DB_MACHINE_COMMANDS
    198 void dump_trapframe(struct trapframe *, void (*)(const char *, ...));
    199 
    200 
    201 
    202 void db_machdep_init(void);
    203 
    204 /* hardware breakpoint/watchpoint functions */
    205 void aarch64_breakpoint_clear(int);
    206 void aarch64_breakpoint_set(int, vaddr_t);
    207 void aarch64_watchpoint_clear(int);
    208 void aarch64_watchpoint_set(int, vaddr_t, int, int);
    209 #define WATCHPOINT_ACCESS_LOAD		0x01
    210 #define WATCHPOINT_ACCESS_STORE		0x02
    211 #define WATCHPOINT_ACCESS_LOADSTORE	0x03
    212 #define WATCHPOINT_ACCESS_MASK		0x03
    213 
    214 
    215 #elif defined(__arm__)
    216 
    217 #include <arm/db_machdep.h>
    218 
    219 #endif
    220 
    221 #endif /* _AARCH64_DB_MACHDEP_H_ */
    222