Merge tag 'pull-aspeed-20220714' of https://github.com/legoater/qemu into staging
[qemu/ar7.git] / target / riscv / debug.c
blobfc6e13222f2e491a86829564ccab722fc325dced
1 /*
2 * QEMU RISC-V Native Debug Support
4 * Copyright (c) 2022 Wind River Systems, Inc.
6 * Author:
7 * Bin Meng <bin.meng@windriver.com>
9 * This provides the native debug support via the Trigger Module, as defined
10 * in the RISC-V Debug Specification:
11 * https://github.com/riscv/riscv-debug-spec/raw/master/riscv-debug-stable.pdf
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms and conditions of the GNU General Public License,
15 * version 2 or later, as published by the Free Software Foundation.
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
22 * You should have received a copy of the GNU General Public License along with
23 * this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "qemu/osdep.h"
27 #include "qemu/log.h"
28 #include "qapi/error.h"
29 #include "cpu.h"
30 #include "trace.h"
31 #include "exec/exec-all.h"
34 * The following M-mode trigger CSRs are implemented:
36 * - tselect
37 * - tdata1
38 * - tdata2
39 * - tdata3
41 * We don't support writable 'type' field in the tdata1 register, so there is
42 * no need to implement the "tinfo" CSR.
44 * The following triggers are implemented:
46 * Index | Type | tdata mapping | Description
47 * ------+------+------------------------+------------
48 * 0 | 2 | tdata1, tdata2 | Address / Data Match
49 * 1 | 2 | tdata1, tdata2 | Address / Data Match
52 /* tdata availability of a trigger */
53 typedef bool tdata_avail[TDATA_NUM];
55 static tdata_avail tdata_mapping[TRIGGER_NUM] = {
56 [TRIGGER_TYPE2_IDX_0 ... TRIGGER_TYPE2_IDX_1] = { true, true, false },
59 /* only breakpoint size 1/2/4/8 supported */
60 static int access_size[SIZE_NUM] = {
61 [SIZE_ANY] = 0,
62 [SIZE_1B] = 1,
63 [SIZE_2B] = 2,
64 [SIZE_4B] = 4,
65 [SIZE_6B] = -1,
66 [SIZE_8B] = 8,
67 [6 ... 15] = -1,
70 static inline target_ulong trigger_type(CPURISCVState *env,
71 trigger_type_t type)
73 target_ulong tdata1;
75 switch (riscv_cpu_mxl(env)) {
76 case MXL_RV32:
77 tdata1 = RV32_TYPE(type);
78 break;
79 case MXL_RV64:
80 case MXL_RV128:
81 tdata1 = RV64_TYPE(type);
82 break;
83 default:
84 g_assert_not_reached();
87 return tdata1;
90 bool tdata_available(CPURISCVState *env, int tdata_index)
92 if (unlikely(tdata_index >= TDATA_NUM)) {
93 return false;
96 if (unlikely(env->trigger_cur >= TRIGGER_NUM)) {
97 return false;
100 return tdata_mapping[env->trigger_cur][tdata_index];
103 target_ulong tselect_csr_read(CPURISCVState *env)
105 return env->trigger_cur;
108 void tselect_csr_write(CPURISCVState *env, target_ulong val)
110 /* all target_ulong bits of tselect are implemented */
111 env->trigger_cur = val;
114 static target_ulong tdata1_validate(CPURISCVState *env, target_ulong val,
115 trigger_type_t t)
117 uint32_t type, dmode;
118 target_ulong tdata1;
120 switch (riscv_cpu_mxl(env)) {
121 case MXL_RV32:
122 type = extract32(val, 28, 4);
123 dmode = extract32(val, 27, 1);
124 tdata1 = RV32_TYPE(t);
125 break;
126 case MXL_RV64:
127 case MXL_RV128:
128 type = extract64(val, 60, 4);
129 dmode = extract64(val, 59, 1);
130 tdata1 = RV64_TYPE(t);
131 break;
132 default:
133 g_assert_not_reached();
136 if (type != t) {
137 qemu_log_mask(LOG_GUEST_ERROR,
138 "ignoring type write to tdata1 register\n");
140 if (dmode != 0) {
141 qemu_log_mask(LOG_UNIMP, "debug mode is not supported\n");
144 return tdata1;
147 static inline void warn_always_zero_bit(target_ulong val, target_ulong mask,
148 const char *msg)
150 if (val & mask) {
151 qemu_log_mask(LOG_UNIMP, "%s bit is always zero\n", msg);
155 static uint32_t type2_breakpoint_size(CPURISCVState *env, target_ulong ctrl)
157 uint32_t size, sizelo, sizehi = 0;
159 if (riscv_cpu_mxl(env) == MXL_RV64) {
160 sizehi = extract32(ctrl, 21, 2);
162 sizelo = extract32(ctrl, 16, 2);
163 size = (sizehi << 2) | sizelo;
165 return size;
168 static inline bool type2_breakpoint_enabled(target_ulong ctrl)
170 bool mode = !!(ctrl & (TYPE2_U | TYPE2_S | TYPE2_M));
171 bool rwx = !!(ctrl & (TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC));
173 return mode && rwx;
176 static target_ulong type2_mcontrol_validate(CPURISCVState *env,
177 target_ulong ctrl)
179 target_ulong val;
180 uint32_t size;
182 /* validate the generic part first */
183 val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH);
185 /* validate unimplemented (always zero) bits */
186 warn_always_zero_bit(ctrl, TYPE2_MATCH, "match");
187 warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain");
188 warn_always_zero_bit(ctrl, TYPE2_ACTION, "action");
189 warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing");
190 warn_always_zero_bit(ctrl, TYPE2_SELECT, "select");
191 warn_always_zero_bit(ctrl, TYPE2_HIT, "hit");
193 /* validate size encoding */
194 size = type2_breakpoint_size(env, ctrl);
195 if (access_size[size] == -1) {
196 qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using SIZE_ANY\n",
197 size);
198 } else {
199 val |= (ctrl & TYPE2_SIZELO);
200 if (riscv_cpu_mxl(env) == MXL_RV64) {
201 val |= (ctrl & TYPE2_SIZEHI);
205 /* keep the mode and attribute bits */
206 val |= (ctrl & (TYPE2_U | TYPE2_S | TYPE2_M |
207 TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC));
209 return val;
212 static void type2_breakpoint_insert(CPURISCVState *env, target_ulong index)
214 target_ulong ctrl = env->type2_trig[index].mcontrol;
215 target_ulong addr = env->type2_trig[index].maddress;
216 bool enabled = type2_breakpoint_enabled(ctrl);
217 CPUState *cs = env_cpu(env);
218 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
219 uint32_t size;
221 if (!enabled) {
222 return;
225 if (ctrl & TYPE2_EXEC) {
226 cpu_breakpoint_insert(cs, addr, flags, &env->type2_trig[index].bp);
229 if (ctrl & TYPE2_LOAD) {
230 flags |= BP_MEM_READ;
232 if (ctrl & TYPE2_STORE) {
233 flags |= BP_MEM_WRITE;
236 if (flags & BP_MEM_ACCESS) {
237 size = type2_breakpoint_size(env, ctrl);
238 if (size != 0) {
239 cpu_watchpoint_insert(cs, addr, size, flags,
240 &env->type2_trig[index].wp);
241 } else {
242 cpu_watchpoint_insert(cs, addr, 8, flags,
243 &env->type2_trig[index].wp);
248 static void type2_breakpoint_remove(CPURISCVState *env, target_ulong index)
250 CPUState *cs = env_cpu(env);
252 if (env->type2_trig[index].bp) {
253 cpu_breakpoint_remove_by_ref(cs, env->type2_trig[index].bp);
254 env->type2_trig[index].bp = NULL;
257 if (env->type2_trig[index].wp) {
258 cpu_watchpoint_remove_by_ref(cs, env->type2_trig[index].wp);
259 env->type2_trig[index].wp = NULL;
263 static target_ulong type2_reg_read(CPURISCVState *env,
264 target_ulong trigger_index, int tdata_index)
266 uint32_t index = trigger_index - TRIGGER_TYPE2_IDX_0;
267 target_ulong tdata;
269 switch (tdata_index) {
270 case TDATA1:
271 tdata = env->type2_trig[index].mcontrol;
272 break;
273 case TDATA2:
274 tdata = env->type2_trig[index].maddress;
275 break;
276 default:
277 g_assert_not_reached();
280 return tdata;
283 static void type2_reg_write(CPURISCVState *env, target_ulong trigger_index,
284 int tdata_index, target_ulong val)
286 uint32_t index = trigger_index - TRIGGER_TYPE2_IDX_0;
287 target_ulong new_val;
289 switch (tdata_index) {
290 case TDATA1:
291 new_val = type2_mcontrol_validate(env, val);
292 if (new_val != env->type2_trig[index].mcontrol) {
293 env->type2_trig[index].mcontrol = new_val;
294 type2_breakpoint_remove(env, index);
295 type2_breakpoint_insert(env, index);
297 break;
298 case TDATA2:
299 if (val != env->type2_trig[index].maddress) {
300 env->type2_trig[index].maddress = val;
301 type2_breakpoint_remove(env, index);
302 type2_breakpoint_insert(env, index);
304 break;
305 default:
306 g_assert_not_reached();
309 return;
312 typedef target_ulong (*tdata_read_func)(CPURISCVState *env,
313 target_ulong trigger_index,
314 int tdata_index);
316 static tdata_read_func trigger_read_funcs[TRIGGER_NUM] = {
317 [TRIGGER_TYPE2_IDX_0 ... TRIGGER_TYPE2_IDX_1] = type2_reg_read,
320 typedef void (*tdata_write_func)(CPURISCVState *env,
321 target_ulong trigger_index,
322 int tdata_index,
323 target_ulong val);
325 static tdata_write_func trigger_write_funcs[TRIGGER_NUM] = {
326 [TRIGGER_TYPE2_IDX_0 ... TRIGGER_TYPE2_IDX_1] = type2_reg_write,
329 target_ulong tdata_csr_read(CPURISCVState *env, int tdata_index)
331 tdata_read_func read_func = trigger_read_funcs[env->trigger_cur];
333 return read_func(env, env->trigger_cur, tdata_index);
336 void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
338 tdata_write_func write_func = trigger_write_funcs[env->trigger_cur];
340 return write_func(env, env->trigger_cur, tdata_index, val);
343 void riscv_cpu_debug_excp_handler(CPUState *cs)
345 RISCVCPU *cpu = RISCV_CPU(cs);
346 CPURISCVState *env = &cpu->env;
348 if (cs->watchpoint_hit) {
349 if (cs->watchpoint_hit->flags & BP_CPU) {
350 cs->watchpoint_hit = NULL;
351 riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
353 } else {
354 if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) {
355 riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
360 bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
362 RISCVCPU *cpu = RISCV_CPU(cs);
363 CPURISCVState *env = &cpu->env;
364 CPUBreakpoint *bp;
365 target_ulong ctrl;
366 target_ulong pc;
367 int i;
369 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
370 for (i = 0; i < TRIGGER_TYPE2_NUM; i++) {
371 ctrl = env->type2_trig[i].mcontrol;
372 pc = env->type2_trig[i].maddress;
374 if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) {
375 /* check U/S/M bit against current privilege level */
376 if ((ctrl >> 3) & BIT(env->priv)) {
377 return true;
383 return false;
386 bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
388 RISCVCPU *cpu = RISCV_CPU(cs);
389 CPURISCVState *env = &cpu->env;
390 target_ulong ctrl;
391 target_ulong addr;
392 int flags;
393 int i;
395 for (i = 0; i < TRIGGER_TYPE2_NUM; i++) {
396 ctrl = env->type2_trig[i].mcontrol;
397 addr = env->type2_trig[i].maddress;
398 flags = 0;
400 if (ctrl & TYPE2_LOAD) {
401 flags |= BP_MEM_READ;
403 if (ctrl & TYPE2_STORE) {
404 flags |= BP_MEM_WRITE;
407 if ((wp->flags & flags) && (wp->vaddr == addr)) {
408 /* check U/S/M bit against current privilege level */
409 if ((ctrl >> 3) & BIT(env->priv)) {
410 return true;
415 return false;
418 void riscv_trigger_init(CPURISCVState *env)
420 target_ulong type2 = trigger_type(env, TRIGGER_TYPE_AD_MATCH);
421 int i;
423 /* type 2 triggers */
424 for (i = 0; i < TRIGGER_TYPE2_NUM; i++) {
426 * type = TRIGGER_TYPE_AD_MATCH
427 * dmode = 0 (both debug and M-mode can write tdata)
428 * maskmax = 0 (unimplemented, always 0)
429 * sizehi = 0 (match against any size, RV64 only)
430 * hit = 0 (unimplemented, always 0)
431 * select = 0 (always 0, perform match on address)
432 * timing = 0 (always 0, trigger before instruction)
433 * sizelo = 0 (match against any size)
434 * action = 0 (always 0, raise a breakpoint exception)
435 * chain = 0 (unimplemented, always 0)
436 * match = 0 (always 0, when any compare value equals tdata2)
438 env->type2_trig[i].mcontrol = type2;
439 env->type2_trig[i].maddress = 0;
440 env->type2_trig[i].bp = NULL;
441 env->type2_trig[i].wp = NULL;