summaryrefslogtreecommitdiff
path: root/sys/arch/aarch64/include/db_machdep.h
blob: 455307a5b951ae16fd7d5e0f85c85371d0e1ecd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* $NetBSD: db_machdep.h,v 1.16 2021/10/31 22:06:32 skrll Exp $ */

/*-
 * Copyright (c) 2014 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Matt Thomas of 3am Software Foundry.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*-
 * Copyright (c) 2014 Andrew Turner
 * Copyright (c) 2014-2015 The FreeBSD Foundation
 * All rights reserved.
 *
 * This software was developed by Semihalf under
 * sponsorship from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: head/sys/arm64/include/db_machdep.h 316001 2017-03-26 18:46:35Z bde $
 */

#ifndef _AARCH64_DB_MACHDEP_H_
#define _AARCH64_DB_MACHDEP_H_

#ifdef __aarch64__

#include <sys/types.h>

#include <uvm/uvm.h>

#include <aarch64/frame.h>
#include <aarch64/pmap.h>

#include <ddb/db_user.h>

typedef long long int db_expr_t;
#define DDB_EXPR_FMT "ll"
typedef uintptr_t db_addr_t;

#define BKPT_ADDR(addr)		(addr)
#define BKPT_SIZE		4
#ifdef __AARCH64EB__
#define BKPT_INSN		0x000020d4	/* brk #0 */
#else
#define BKPT_INSN		0xd4200000	/* brk #0 */
#endif
#define BKPT_SET(insn, addr)	(BKPT_INSN)

typedef struct trapframe db_regs_t;
extern db_regs_t ddb_regs;
#define DDB_REGS		(&ddb_regs)
#define PC_REGS(tf)		((tf)->tf_pc)

int kdb_trap(int, struct trapframe *);
#define DB_TRAP_UNKNOWN		0
#define DB_TRAP_BREAKPOINT	1
#define DB_TRAP_BKPT_INSN	2
#define DB_TRAP_WATCHPOINT	3
#define DB_TRAP_SW_STEP		4

#define IS_BREAKPOINT_TRAP(type, code) \
	((type) == DB_TRAP_BKPT_INSN)
#define IS_WATCHPOINT_TRAP(type, code) \
	((type) == DB_TRAP_BREAKPOINT || (type) == DB_TRAP_WATCHPOINT)

static inline bool
inst_return(db_expr_t insn)
{
	LE32TOH(insn);
	return ((insn & 0xfffffc1f) == 0xd65f0000);	/* ret xN */
}

static inline bool
inst_trap_return(db_expr_t insn)
{
	LE32TOH(insn);
	return insn == 0xd69f03e0;			/* eret */
}

static inline bool
inst_call(db_expr_t insn)
{
	LE32TOH(insn);
	return ((insn & 0xfc000000) == 0x94000000)	/* bl */
	    || ((insn & 0xfffffc1f) == 0xd63f0000);	/* blr */
}

static inline bool
inst_load(db_expr_t insn)
{
	LE32TOH(insn);
	return
	    ((insn & 0xffe00c00) == 0xb8800000) ||	/* ldursw */
	    /* ldrsw imm{pre,post}idx */
	    ((insn & 0xffe00400) == 0xb8800400) ||
	    ((insn & 0xffc00c00) == 0xb8800800) ||	/* ldrsw reg,ldtrsw */
	    ((insn & 0xffc00000) == 0xb9800000) ||	/* ldrsw immunsign */
	    ((insn & 0xffc00000) == 0x39400000) ||	/* ldrb immunsign */
	    ((insn & 0xff000000) == 0x98000000) ||	/* ldrsw literal */
	    /* ldpsw {pre,post}idx */
	    ((insn & 0xfec00000) == 0x68c00000) ||
	    /* ldrh immunsign,ldpsw signed */
	    ((insn & 0xefc00000) == 0x69400000) ||
	    ((insn & 0xbfff0000) == 0x887f0000) ||	/* ldaxp,ldxp */
	    ((insn & 0xbfc00000) == 0xb9400000) ||	/* ldr immunsign */
	    ((insn & 0xbfa00c00) == 0x38800000) ||	/* ldursh,ldursb */
	    /* ldrsh imm{pre,post}idx,ldrsb imm{pre,post}idx */
	    ((insn & 0xbfa00400) == 0x38800400) ||
	    /* ldrs[bh] reg,ldtrs[bh] */
	    ((insn & 0xbf800c00) == 0x38800800) ||
	    /* ldrsh immunsign,ldrsb immunsign */
	    ((insn & 0xbf800000) == 0x39800000) ||
	    ((insn & 0xbf000000) == 0x18000000) ||	/* ldr literal */
	    /* ldp {pre,post}idx,ldp signed,ldnp */
	    ((insn & 0x7e400000) == 0x28400000) ||
	    ((insn & 0x3ffffc00) == 0x085f7c00) ||	/* ldxr,ldxr[bh] */
	    ((insn & 0x3fe00c00) == 0x38400000) ||	/* ldur,ldur[bh] */
	    /* ldr imm{pre,post}idx,ldr[bh]_imm{pre,post}idx */
	    ((insn & 0x3fe00400) == 0x38400400) ||
	    /* ldtr,ldtr[bh],ldr_reg,ldr[bh]_reg */
	    ((insn & 0x3fc00c00) == 0x38400800) ||
	    /* ldaxr,ldaxr[bh],ldar,ldar[bh] */
	    ((insn & 0x3f7ffc00) == 0x085ffc00);
}

static inline bool
inst_store(db_expr_t insn)
{
	LE32TOH(insn);
	return
	    ((insn & 0xbfe00000) == 0x88200000) ||	/* stlxp,stxp */
	    /* stp {pre,post}idx,stp signed,stnp */
	    ((insn & 0x7e400000) == 0x28000000) ||
	    ((insn & 0x3ffffc00) == 0x089ffc00) ||	/* stlr,stlr[bh] */
	    /* stlxr,stlxr[bh],stxr,stxr[bh] */
	    ((insn & 0x3fe07c00) == 0x08007c00) ||
	    ((insn & 0x3fe00c00) == 0x38000000) ||	/* stur,stur[bh] */
	    /* str imm{pre,post}idx,str[bh] imm{pre,post}idx */
	    ((insn & 0x3fe00400) == 0x38000400) ||
	    /* str reg,str[bh] reg,sttr,sttr[bh] */
	    ((insn & 0x3fc00c00) == 0x38000800) ||
	    /* str immunsign,str[bh] immunsign */
	    ((insn & 0x3fc00000) == 0x39000000);
}

#define SOFTWARE_SSTEP

#ifdef SOFTWARE_SSTEP

static inline bool
inst_branch(db_expr_t insn)
{
	LE32TOH(insn);
	return
	    ((insn & 0xff000010) == 0x54000000) ||	/* b.cond */
	    ((insn & 0xfc000000) == 0x14000000) ||	/* b imm */
	    ((insn & 0xfffffc1f) == 0xd61f0000) ||	/* br */
	    ((insn & 0x7f000000) == 0x35000000) ||	/* cbnz */
	    ((insn & 0x7f000000) == 0x34000000) ||	/* cbz */
	    ((insn & 0x7f000000) == 0x37000000) ||	/* tbnz */
	    ((insn & 0x7f000000) == 0x36000000);	/* tbz */
}

bool db_inst_unconditional_flow_transfer(db_expr_t);
db_addr_t db_branch_taken(db_expr_t, db_addr_t, db_regs_t *);

#define next_instr_address(pc, bd)		\
	    ((bd) ? (pc) : ((pc) + 4))
#define branch_taken(ins, pc, regs)		\
	    db_branch_taken((ins), (pc), (regs))
#define inst_unconditional_flow_transfer(ins)	\
	    db_inst_unconditional_flow_transfer(ins)

#endif /* SOFTWARE_SSTEP */

#define DB_MACHINE_COMMANDS

#ifdef _KERNEL
void db_pteinfo(vaddr_t, void (*)(const char *, ...) __printflike(1, 2));
void db_pte_print(pt_entry_t, int, void (*)(const char *, ...) __printflike(1, 2));
void db_ttbrdump(bool, vaddr_t, void (*pr)(const char *, ...) __printflike(1, 2));
void db_machdep_cpu_init(void);
void db_machdep_init(struct cpu_info * const);

/* hardware breakpoint/watchpoint functions */
void aarch64_breakpoint_set(int, vaddr_t);
void aarch64_watchpoint_set(int, vaddr_t, u_int, u_int);
#define WATCHPOINT_ACCESS_LOAD		0x01
#define WATCHPOINT_ACCESS_STORE		0x02
#define WATCHPOINT_ACCESS_LOADSTORE	0x03
#define WATCHPOINT_ACCESS_MASK		0x03
#endif

void dump_trapframe(struct trapframe *, void (*)(const char *, ...) __printflike(1, 2));

void dump_switchframe(struct trapframe *, void (*)(const char *, ...) __printflike(1, 2));
const char *strdisasm(vaddr_t, uint64_t);

#define DB_ELF_SYMBOLS

#elif defined(__arm__)

#include <arm/db_machdep.h>

#endif

#endif /* _AARCH64_DB_MACHDEP_H_ */