xref: /openbmc/qemu/target/riscv/debug.c (revision 6ea8d3fc40a8db8d22d00255cea9f9f8c927d643)
195799e36SBin Meng /*
295799e36SBin Meng  * QEMU RISC-V Native Debug Support
395799e36SBin Meng  *
495799e36SBin Meng  * Copyright (c) 2022 Wind River Systems, Inc.
595799e36SBin Meng  *
695799e36SBin Meng  * Author:
795799e36SBin Meng  *   Bin Meng <bin.meng@windriver.com>
895799e36SBin Meng  *
995799e36SBin Meng  * This provides the native debug support via the Trigger Module, as defined
1095799e36SBin Meng  * in the RISC-V Debug Specification:
1195799e36SBin Meng  * https://github.com/riscv/riscv-debug-spec/raw/master/riscv-debug-stable.pdf
1295799e36SBin Meng  *
1395799e36SBin Meng  * This program is free software; you can redistribute it and/or modify it
1495799e36SBin Meng  * under the terms and conditions of the GNU General Public License,
1595799e36SBin Meng  * version 2 or later, as published by the Free Software Foundation.
1695799e36SBin Meng  *
1795799e36SBin Meng  * This program is distributed in the hope it will be useful, but WITHOUT
1895799e36SBin Meng  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1995799e36SBin Meng  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
2095799e36SBin Meng  * more details.
2195799e36SBin Meng  *
2295799e36SBin Meng  * You should have received a copy of the GNU General Public License along with
2395799e36SBin Meng  * this program.  If not, see <http://www.gnu.org/licenses/>.
2495799e36SBin Meng  */
2595799e36SBin Meng 
2695799e36SBin Meng #include "qemu/osdep.h"
2795799e36SBin Meng #include "qemu/log.h"
2895799e36SBin Meng #include "qapi/error.h"
2995799e36SBin Meng #include "cpu.h"
3095799e36SBin Meng #include "trace.h"
3195799e36SBin Meng #include "exec/exec-all.h"
3295799e36SBin Meng 
3395799e36SBin Meng /*
3495799e36SBin Meng  * The following M-mode trigger CSRs are implemented:
3595799e36SBin Meng  *
3695799e36SBin Meng  * - tselect
3795799e36SBin Meng  * - tdata1
3895799e36SBin Meng  * - tdata2
3995799e36SBin Meng  * - tdata3
4095799e36SBin Meng  *
4195799e36SBin Meng  * We don't support writable 'type' field in the tdata1 register, so there is
4295799e36SBin Meng  * no need to implement the "tinfo" CSR.
4395799e36SBin Meng  *
4495799e36SBin Meng  * The following triggers are implemented:
4595799e36SBin Meng  *
4695799e36SBin Meng  * Index | Type |          tdata mapping | Description
4795799e36SBin Meng  * ------+------+------------------------+------------
4895799e36SBin Meng  *     0 |    2 |         tdata1, tdata2 | Address / Data Match
4995799e36SBin Meng  *     1 |    2 |         tdata1, tdata2 | Address / Data Match
5095799e36SBin Meng  */
5195799e36SBin Meng 
5295799e36SBin Meng /* tdata availability of a trigger */
5395799e36SBin Meng typedef bool tdata_avail[TDATA_NUM];
5495799e36SBin Meng 
55a42bd001SFrank Chang static tdata_avail tdata_mapping[TRIGGER_TYPE_NUM] = {
56a42bd001SFrank Chang     [TRIGGER_TYPE_NO_EXIST] = { false, false, false },
57a42bd001SFrank Chang     [TRIGGER_TYPE_AD_MATCH] = { true, true, true },
58a42bd001SFrank Chang     [TRIGGER_TYPE_INST_CNT] = { true, false, true },
59a42bd001SFrank Chang     [TRIGGER_TYPE_INT] = { true, true, true },
60a42bd001SFrank Chang     [TRIGGER_TYPE_EXCP] = { true, true, true },
61a42bd001SFrank Chang     [TRIGGER_TYPE_AD_MATCH6] = { true, true, true },
62a42bd001SFrank Chang     [TRIGGER_TYPE_EXT_SRC] = { true, false, false },
63a42bd001SFrank Chang     [TRIGGER_TYPE_UNAVAIL] = { true, true, true }
6495799e36SBin Meng };
6595799e36SBin Meng 
6695799e36SBin Meng /* only breakpoint size 1/2/4/8 supported */
6795799e36SBin Meng static int access_size[SIZE_NUM] = {
6895799e36SBin Meng     [SIZE_ANY] = 0,
6995799e36SBin Meng     [SIZE_1B]  = 1,
7095799e36SBin Meng     [SIZE_2B]  = 2,
7195799e36SBin Meng     [SIZE_4B]  = 4,
7295799e36SBin Meng     [SIZE_6B]  = -1,
7395799e36SBin Meng     [SIZE_8B]  = 8,
7495799e36SBin Meng     [6 ... 15] = -1,
7595799e36SBin Meng };
7695799e36SBin Meng 
77a42bd001SFrank Chang static inline target_ulong extract_trigger_type(CPURISCVState *env,
78a42bd001SFrank Chang                                                 target_ulong tdata1)
79a42bd001SFrank Chang {
80a42bd001SFrank Chang     switch (riscv_cpu_mxl(env)) {
81a42bd001SFrank Chang     case MXL_RV32:
82a42bd001SFrank Chang         return extract32(tdata1, 28, 4);
83a42bd001SFrank Chang     case MXL_RV64:
84a42bd001SFrank Chang     case MXL_RV128:
85a42bd001SFrank Chang         return extract64(tdata1, 60, 4);
86a42bd001SFrank Chang     default:
87a42bd001SFrank Chang         g_assert_not_reached();
88a42bd001SFrank Chang     }
89a42bd001SFrank Chang }
90a42bd001SFrank Chang 
91a42bd001SFrank Chang static inline target_ulong get_trigger_type(CPURISCVState *env,
92a42bd001SFrank Chang                                             target_ulong trigger_index)
93a42bd001SFrank Chang {
949495c488SFrank Chang     return extract_trigger_type(env, env->tdata1[trigger_index]);
95a42bd001SFrank Chang }
96a42bd001SFrank Chang 
979d5a84dbSFrank Chang static inline target_ulong build_tdata1(CPURISCVState *env,
989d5a84dbSFrank Chang                                         trigger_type_t type,
999d5a84dbSFrank Chang                                         bool dmode, target_ulong data)
10095799e36SBin Meng {
10195799e36SBin Meng     target_ulong tdata1;
10295799e36SBin Meng 
10395799e36SBin Meng     switch (riscv_cpu_mxl(env)) {
10495799e36SBin Meng     case MXL_RV32:
1059d5a84dbSFrank Chang         tdata1 = RV32_TYPE(type) |
1069d5a84dbSFrank Chang                  (dmode ? RV32_DMODE : 0) |
1079d5a84dbSFrank Chang                  (data & RV32_DATA_MASK);
10895799e36SBin Meng         break;
10995799e36SBin Meng     case MXL_RV64:
110d1d85412SFrédéric Pétrot     case MXL_RV128:
1119d5a84dbSFrank Chang         tdata1 = RV64_TYPE(type) |
1129d5a84dbSFrank Chang                  (dmode ? RV64_DMODE : 0) |
1139d5a84dbSFrank Chang                  (data & RV64_DATA_MASK);
11495799e36SBin Meng         break;
11595799e36SBin Meng     default:
11695799e36SBin Meng         g_assert_not_reached();
11795799e36SBin Meng     }
11895799e36SBin Meng 
11995799e36SBin Meng     return tdata1;
12095799e36SBin Meng }
12195799e36SBin Meng 
12295799e36SBin Meng bool tdata_available(CPURISCVState *env, int tdata_index)
12395799e36SBin Meng {
124a42bd001SFrank Chang     int trigger_type = get_trigger_type(env, env->trigger_cur);
125a42bd001SFrank Chang 
12695799e36SBin Meng     if (unlikely(tdata_index >= TDATA_NUM)) {
12795799e36SBin Meng         return false;
12895799e36SBin Meng     }
12995799e36SBin Meng 
130a42bd001SFrank Chang     return tdata_mapping[trigger_type][tdata_index];
13195799e36SBin Meng }
13295799e36SBin Meng 
13395799e36SBin Meng target_ulong tselect_csr_read(CPURISCVState *env)
13495799e36SBin Meng {
13595799e36SBin Meng     return env->trigger_cur;
13695799e36SBin Meng }
13795799e36SBin Meng 
13895799e36SBin Meng void tselect_csr_write(CPURISCVState *env, target_ulong val)
13995799e36SBin Meng {
140*6ea8d3fcSFrank Chang     if (val < RV_MAX_TRIGGERS) {
14195799e36SBin Meng         env->trigger_cur = val;
14295799e36SBin Meng     }
143*6ea8d3fcSFrank Chang }
14495799e36SBin Meng 
14595799e36SBin Meng static target_ulong tdata1_validate(CPURISCVState *env, target_ulong val,
14695799e36SBin Meng                                     trigger_type_t t)
14795799e36SBin Meng {
14895799e36SBin Meng     uint32_t type, dmode;
14995799e36SBin Meng     target_ulong tdata1;
15095799e36SBin Meng 
15195799e36SBin Meng     switch (riscv_cpu_mxl(env)) {
15295799e36SBin Meng     case MXL_RV32:
15395799e36SBin Meng         type = extract32(val, 28, 4);
15495799e36SBin Meng         dmode = extract32(val, 27, 1);
15595799e36SBin Meng         tdata1 = RV32_TYPE(t);
15695799e36SBin Meng         break;
15795799e36SBin Meng     case MXL_RV64:
158d1d85412SFrédéric Pétrot     case MXL_RV128:
15995799e36SBin Meng         type = extract64(val, 60, 4);
16095799e36SBin Meng         dmode = extract64(val, 59, 1);
16195799e36SBin Meng         tdata1 = RV64_TYPE(t);
16295799e36SBin Meng         break;
16395799e36SBin Meng     default:
16495799e36SBin Meng         g_assert_not_reached();
16595799e36SBin Meng     }
16695799e36SBin Meng 
16795799e36SBin Meng     if (type != t) {
16895799e36SBin Meng         qemu_log_mask(LOG_GUEST_ERROR,
16995799e36SBin Meng                       "ignoring type write to tdata1 register\n");
17095799e36SBin Meng     }
171a42bd001SFrank Chang 
17295799e36SBin Meng     if (dmode != 0) {
17395799e36SBin Meng         qemu_log_mask(LOG_UNIMP, "debug mode is not supported\n");
17495799e36SBin Meng     }
17595799e36SBin Meng 
17695799e36SBin Meng     return tdata1;
17795799e36SBin Meng }
17895799e36SBin Meng 
17995799e36SBin Meng static inline void warn_always_zero_bit(target_ulong val, target_ulong mask,
18095799e36SBin Meng                                         const char *msg)
18195799e36SBin Meng {
18295799e36SBin Meng     if (val & mask) {
18395799e36SBin Meng         qemu_log_mask(LOG_UNIMP, "%s bit is always zero\n", msg);
18495799e36SBin Meng     }
18595799e36SBin Meng }
18695799e36SBin Meng 
1879495c488SFrank Chang /* type 2 trigger */
1889495c488SFrank Chang 
18995799e36SBin Meng static uint32_t type2_breakpoint_size(CPURISCVState *env, target_ulong ctrl)
19095799e36SBin Meng {
19195799e36SBin Meng     uint32_t size, sizelo, sizehi = 0;
19295799e36SBin Meng 
19395799e36SBin Meng     if (riscv_cpu_mxl(env) == MXL_RV64) {
19495799e36SBin Meng         sizehi = extract32(ctrl, 21, 2);
19595799e36SBin Meng     }
19695799e36SBin Meng     sizelo = extract32(ctrl, 16, 2);
19795799e36SBin Meng     size = (sizehi << 2) | sizelo;
19895799e36SBin Meng 
19995799e36SBin Meng     return size;
20095799e36SBin Meng }
20195799e36SBin Meng 
20295799e36SBin Meng static inline bool type2_breakpoint_enabled(target_ulong ctrl)
20395799e36SBin Meng {
20495799e36SBin Meng     bool mode = !!(ctrl & (TYPE2_U | TYPE2_S | TYPE2_M));
20595799e36SBin Meng     bool rwx = !!(ctrl & (TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC));
20695799e36SBin Meng 
20795799e36SBin Meng     return mode && rwx;
20895799e36SBin Meng }
20995799e36SBin Meng 
21095799e36SBin Meng static target_ulong type2_mcontrol_validate(CPURISCVState *env,
21195799e36SBin Meng                                             target_ulong ctrl)
21295799e36SBin Meng {
21395799e36SBin Meng     target_ulong val;
21495799e36SBin Meng     uint32_t size;
21595799e36SBin Meng 
21695799e36SBin Meng     /* validate the generic part first */
21795799e36SBin Meng     val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH);
21895799e36SBin Meng 
21995799e36SBin Meng     /* validate unimplemented (always zero) bits */
22095799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_MATCH, "match");
22195799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain");
22295799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_ACTION, "action");
22395799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing");
22495799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_SELECT, "select");
22595799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_HIT, "hit");
22695799e36SBin Meng 
22795799e36SBin Meng     /* validate size encoding */
22895799e36SBin Meng     size = type2_breakpoint_size(env, ctrl);
22995799e36SBin Meng     if (access_size[size] == -1) {
23095799e36SBin Meng         qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using SIZE_ANY\n",
23195799e36SBin Meng                       size);
23295799e36SBin Meng     } else {
23395799e36SBin Meng         val |= (ctrl & TYPE2_SIZELO);
23495799e36SBin Meng         if (riscv_cpu_mxl(env) == MXL_RV64) {
23595799e36SBin Meng             val |= (ctrl & TYPE2_SIZEHI);
23695799e36SBin Meng         }
23795799e36SBin Meng     }
23895799e36SBin Meng 
23995799e36SBin Meng     /* keep the mode and attribute bits */
24095799e36SBin Meng     val |= (ctrl & (TYPE2_U | TYPE2_S | TYPE2_M |
24195799e36SBin Meng                     TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC));
24295799e36SBin Meng 
24395799e36SBin Meng     return val;
24495799e36SBin Meng }
24595799e36SBin Meng 
24695799e36SBin Meng static void type2_breakpoint_insert(CPURISCVState *env, target_ulong index)
24795799e36SBin Meng {
2489495c488SFrank Chang     target_ulong ctrl = env->tdata1[index];
2499495c488SFrank Chang     target_ulong addr = env->tdata2[index];
25095799e36SBin Meng     bool enabled = type2_breakpoint_enabled(ctrl);
25195799e36SBin Meng     CPUState *cs = env_cpu(env);
25295799e36SBin Meng     int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
25395799e36SBin Meng     uint32_t size;
25495799e36SBin Meng 
25595799e36SBin Meng     if (!enabled) {
25695799e36SBin Meng         return;
25795799e36SBin Meng     }
25895799e36SBin Meng 
25995799e36SBin Meng     if (ctrl & TYPE2_EXEC) {
2609495c488SFrank Chang         cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]);
26195799e36SBin Meng     }
26295799e36SBin Meng 
26395799e36SBin Meng     if (ctrl & TYPE2_LOAD) {
26495799e36SBin Meng         flags |= BP_MEM_READ;
26595799e36SBin Meng     }
26695799e36SBin Meng     if (ctrl & TYPE2_STORE) {
26795799e36SBin Meng         flags |= BP_MEM_WRITE;
26895799e36SBin Meng     }
26995799e36SBin Meng 
27095799e36SBin Meng     if (flags & BP_MEM_ACCESS) {
27195799e36SBin Meng         size = type2_breakpoint_size(env, ctrl);
27295799e36SBin Meng         if (size != 0) {
27395799e36SBin Meng             cpu_watchpoint_insert(cs, addr, size, flags,
2749495c488SFrank Chang                                   &env->cpu_watchpoint[index]);
27595799e36SBin Meng         } else {
27695799e36SBin Meng             cpu_watchpoint_insert(cs, addr, 8, flags,
2779495c488SFrank Chang                                   &env->cpu_watchpoint[index]);
27895799e36SBin Meng         }
27995799e36SBin Meng     }
28095799e36SBin Meng }
28195799e36SBin Meng 
28295799e36SBin Meng static void type2_breakpoint_remove(CPURISCVState *env, target_ulong index)
28395799e36SBin Meng {
28495799e36SBin Meng     CPUState *cs = env_cpu(env);
28595799e36SBin Meng 
2869495c488SFrank Chang     if (env->cpu_breakpoint[index]) {
2879495c488SFrank Chang         cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]);
2889495c488SFrank Chang         env->cpu_breakpoint[index] = NULL;
28995799e36SBin Meng     }
29095799e36SBin Meng 
2919495c488SFrank Chang     if (env->cpu_watchpoint[index]) {
2929495c488SFrank Chang         cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]);
2939495c488SFrank Chang         env->cpu_watchpoint[index] = NULL;
29495799e36SBin Meng     }
29595799e36SBin Meng }
29695799e36SBin Meng 
297a42bd001SFrank Chang static void type2_reg_write(CPURISCVState *env, target_ulong index,
29895799e36SBin Meng                             int tdata_index, target_ulong val)
29995799e36SBin Meng {
30095799e36SBin Meng     target_ulong new_val;
30195799e36SBin Meng 
30295799e36SBin Meng     switch (tdata_index) {
30395799e36SBin Meng     case TDATA1:
30495799e36SBin Meng         new_val = type2_mcontrol_validate(env, val);
3059495c488SFrank Chang         if (new_val != env->tdata1[index]) {
3069495c488SFrank Chang             env->tdata1[index] = new_val;
30795799e36SBin Meng             type2_breakpoint_remove(env, index);
30895799e36SBin Meng             type2_breakpoint_insert(env, index);
30995799e36SBin Meng         }
31095799e36SBin Meng         break;
31195799e36SBin Meng     case TDATA2:
3129495c488SFrank Chang         if (val != env->tdata2[index]) {
3139495c488SFrank Chang             env->tdata2[index] = val;
31495799e36SBin Meng             type2_breakpoint_remove(env, index);
31595799e36SBin Meng             type2_breakpoint_insert(env, index);
31695799e36SBin Meng         }
31795799e36SBin Meng         break;
3189495c488SFrank Chang     case TDATA3:
3199495c488SFrank Chang         qemu_log_mask(LOG_UNIMP,
3209495c488SFrank Chang                       "tdata3 is not supported for type 2 trigger\n");
3219495c488SFrank Chang         break;
32295799e36SBin Meng     default:
32395799e36SBin Meng         g_assert_not_reached();
32495799e36SBin Meng     }
32595799e36SBin Meng 
32695799e36SBin Meng     return;
32795799e36SBin Meng }
32895799e36SBin Meng 
32995799e36SBin Meng target_ulong tdata_csr_read(CPURISCVState *env, int tdata_index)
33095799e36SBin Meng {
3319495c488SFrank Chang     switch (tdata_index) {
3329495c488SFrank Chang     case TDATA1:
3339495c488SFrank Chang         return env->tdata1[env->trigger_cur];
3349495c488SFrank Chang     case TDATA2:
3359495c488SFrank Chang         return env->tdata2[env->trigger_cur];
3369495c488SFrank Chang     case TDATA3:
3379495c488SFrank Chang         return env->tdata3[env->trigger_cur];
338a42bd001SFrank Chang     default:
339a42bd001SFrank Chang         g_assert_not_reached();
340a42bd001SFrank Chang     }
34195799e36SBin Meng }
34295799e36SBin Meng 
34395799e36SBin Meng void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
34495799e36SBin Meng {
345a42bd001SFrank Chang     int trigger_type;
34695799e36SBin Meng 
347a42bd001SFrank Chang     if (tdata_index == TDATA1) {
348a42bd001SFrank Chang         trigger_type = extract_trigger_type(env, val);
349a42bd001SFrank Chang     } else {
350a42bd001SFrank Chang         trigger_type = get_trigger_type(env, env->trigger_cur);
351a42bd001SFrank Chang     }
352a42bd001SFrank Chang 
353a42bd001SFrank Chang     switch (trigger_type) {
354a42bd001SFrank Chang     case TRIGGER_TYPE_AD_MATCH:
355a42bd001SFrank Chang         type2_reg_write(env, env->trigger_cur, tdata_index, val);
356a42bd001SFrank Chang         break;
357a42bd001SFrank Chang     case TRIGGER_TYPE_INST_CNT:
358a42bd001SFrank Chang     case TRIGGER_TYPE_INT:
359a42bd001SFrank Chang     case TRIGGER_TYPE_EXCP:
360a42bd001SFrank Chang     case TRIGGER_TYPE_AD_MATCH6:
361a42bd001SFrank Chang     case TRIGGER_TYPE_EXT_SRC:
362a42bd001SFrank Chang         qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n",
363a42bd001SFrank Chang                       trigger_type);
364a42bd001SFrank Chang         break;
365a42bd001SFrank Chang     case TRIGGER_TYPE_NO_EXIST:
366a42bd001SFrank Chang     case TRIGGER_TYPE_UNAVAIL:
367a42bd001SFrank Chang         qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n",
368a42bd001SFrank Chang                       trigger_type);
369a42bd001SFrank Chang         break;
370a42bd001SFrank Chang     default:
371a42bd001SFrank Chang         g_assert_not_reached();
372a42bd001SFrank Chang     }
37395799e36SBin Meng }
374b5f6379dSBin Meng 
375b5f6379dSBin Meng void riscv_cpu_debug_excp_handler(CPUState *cs)
376b5f6379dSBin Meng {
377b5f6379dSBin Meng     RISCVCPU *cpu = RISCV_CPU(cs);
378b5f6379dSBin Meng     CPURISCVState *env = &cpu->env;
379b5f6379dSBin Meng 
380b5f6379dSBin Meng     if (cs->watchpoint_hit) {
381b5f6379dSBin Meng         if (cs->watchpoint_hit->flags & BP_CPU) {
382b5f6379dSBin Meng             cs->watchpoint_hit = NULL;
383b5f6379dSBin Meng             riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
384b5f6379dSBin Meng         }
385b5f6379dSBin Meng     } else {
386b5f6379dSBin Meng         if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) {
387b5f6379dSBin Meng             riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
388b5f6379dSBin Meng         }
389b5f6379dSBin Meng     }
390b5f6379dSBin Meng }
391b5f6379dSBin Meng 
392b5f6379dSBin Meng bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
393b5f6379dSBin Meng {
394b5f6379dSBin Meng     RISCVCPU *cpu = RISCV_CPU(cs);
395b5f6379dSBin Meng     CPURISCVState *env = &cpu->env;
396b5f6379dSBin Meng     CPUBreakpoint *bp;
397b5f6379dSBin Meng     target_ulong ctrl;
398b5f6379dSBin Meng     target_ulong pc;
399a42bd001SFrank Chang     int trigger_type;
400b5f6379dSBin Meng     int i;
401b5f6379dSBin Meng 
402b5f6379dSBin Meng     QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
403a42bd001SFrank Chang         for (i = 0; i < RV_MAX_TRIGGERS; i++) {
404a42bd001SFrank Chang             trigger_type = get_trigger_type(env, i);
405a42bd001SFrank Chang 
406a42bd001SFrank Chang             switch (trigger_type) {
407a42bd001SFrank Chang             case TRIGGER_TYPE_AD_MATCH:
4089495c488SFrank Chang                 ctrl = env->tdata1[i];
4099495c488SFrank Chang                 pc = env->tdata2[i];
410b5f6379dSBin Meng 
411b5f6379dSBin Meng                 if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) {
412b5f6379dSBin Meng                     /* check U/S/M bit against current privilege level */
413b5f6379dSBin Meng                     if ((ctrl >> 3) & BIT(env->priv)) {
414b5f6379dSBin Meng                         return true;
415b5f6379dSBin Meng                     }
416b5f6379dSBin Meng                 }
417a42bd001SFrank Chang                 break;
418a42bd001SFrank Chang             default:
419a42bd001SFrank Chang                 /* other trigger types are not supported or irrelevant */
420a42bd001SFrank Chang                 break;
421a42bd001SFrank Chang             }
422b5f6379dSBin Meng         }
423b5f6379dSBin Meng     }
424b5f6379dSBin Meng 
425b5f6379dSBin Meng     return false;
426b5f6379dSBin Meng }
427b5f6379dSBin Meng 
428b5f6379dSBin Meng bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
429b5f6379dSBin Meng {
430b5f6379dSBin Meng     RISCVCPU *cpu = RISCV_CPU(cs);
431b5f6379dSBin Meng     CPURISCVState *env = &cpu->env;
432b5f6379dSBin Meng     target_ulong ctrl;
433b5f6379dSBin Meng     target_ulong addr;
434a42bd001SFrank Chang     int trigger_type;
435b5f6379dSBin Meng     int flags;
436b5f6379dSBin Meng     int i;
437b5f6379dSBin Meng 
438a42bd001SFrank Chang     for (i = 0; i < RV_MAX_TRIGGERS; i++) {
439a42bd001SFrank Chang         trigger_type = get_trigger_type(env, i);
440a42bd001SFrank Chang 
441a42bd001SFrank Chang         switch (trigger_type) {
442a42bd001SFrank Chang         case TRIGGER_TYPE_AD_MATCH:
4439495c488SFrank Chang             ctrl = env->tdata1[i];
4449495c488SFrank Chang             addr = env->tdata2[i];
445b5f6379dSBin Meng             flags = 0;
446b5f6379dSBin Meng 
447b5f6379dSBin Meng             if (ctrl & TYPE2_LOAD) {
448b5f6379dSBin Meng                 flags |= BP_MEM_READ;
449b5f6379dSBin Meng             }
450b5f6379dSBin Meng             if (ctrl & TYPE2_STORE) {
451b5f6379dSBin Meng                 flags |= BP_MEM_WRITE;
452b5f6379dSBin Meng             }
453b5f6379dSBin Meng 
454b5f6379dSBin Meng             if ((wp->flags & flags) && (wp->vaddr == addr)) {
455b5f6379dSBin Meng                 /* check U/S/M bit against current privilege level */
456b5f6379dSBin Meng                 if ((ctrl >> 3) & BIT(env->priv)) {
457b5f6379dSBin Meng                     return true;
458b5f6379dSBin Meng                 }
459b5f6379dSBin Meng             }
460a42bd001SFrank Chang             break;
461a42bd001SFrank Chang         default:
462a42bd001SFrank Chang             /* other trigger types are not supported */
463a42bd001SFrank Chang             break;
464a42bd001SFrank Chang         }
465b5f6379dSBin Meng     }
466b5f6379dSBin Meng 
467b5f6379dSBin Meng     return false;
468b5f6379dSBin Meng }
469b6092544SBin Meng 
470b6092544SBin Meng void riscv_trigger_init(CPURISCVState *env)
471b6092544SBin Meng {
4729d5a84dbSFrank Chang     target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_AD_MATCH, 0, 0);
473b6092544SBin Meng     int i;
474b6092544SBin Meng 
475a42bd001SFrank Chang     /* init to type 2 triggers */
476a42bd001SFrank Chang     for (i = 0; i < RV_MAX_TRIGGERS; i++) {
477b6092544SBin Meng         /*
478b6092544SBin Meng          * type = TRIGGER_TYPE_AD_MATCH
479b6092544SBin Meng          * dmode = 0 (both debug and M-mode can write tdata)
480b6092544SBin Meng          * maskmax = 0 (unimplemented, always 0)
481b6092544SBin Meng          * sizehi = 0 (match against any size, RV64 only)
482b6092544SBin Meng          * hit = 0 (unimplemented, always 0)
483b6092544SBin Meng          * select = 0 (always 0, perform match on address)
484b6092544SBin Meng          * timing = 0 (always 0, trigger before instruction)
485b6092544SBin Meng          * sizelo = 0 (match against any size)
486b6092544SBin Meng          * action = 0 (always 0, raise a breakpoint exception)
487b6092544SBin Meng          * chain = 0 (unimplemented, always 0)
488b6092544SBin Meng          * match = 0 (always 0, when any compare value equals tdata2)
489b6092544SBin Meng          */
4909495c488SFrank Chang         env->tdata1[i] = tdata1;
4919495c488SFrank Chang         env->tdata2[i] = 0;
4929495c488SFrank Chang         env->tdata3[i] = 0;
4939495c488SFrank Chang         env->cpu_breakpoint[i] = NULL;
4949495c488SFrank Chang         env->cpu_watchpoint[i] = NULL;
495b6092544SBin Meng     }
496b6092544SBin Meng }
497