xref: /openbmc/qemu/target/riscv/debug.c (revision c4db48cc24a5e254697a3d4d9201f0e5227fce02)
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"
322c9d7471SLIU Zhiwei #include "exec/helper-proto.h"
335a4ae64cSLIU Zhiwei #include "sysemu/cpu-timers.h"
3495799e36SBin Meng 
3595799e36SBin Meng /*
3695799e36SBin Meng  * The following M-mode trigger CSRs are implemented:
3795799e36SBin Meng  *
3895799e36SBin Meng  * - tselect
3995799e36SBin Meng  * - tdata1
4095799e36SBin Meng  * - tdata2
4195799e36SBin Meng  * - tdata3
4231b9798dSFrank Chang  * - tinfo
4395799e36SBin Meng  *
44c472c142SFrank Chang  * The following triggers are initialized by default:
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 
97d1c11141SFrank Chang static trigger_action_t get_trigger_action(CPURISCVState *env,
98d1c11141SFrank Chang                                            target_ulong trigger_index)
99d1c11141SFrank Chang {
100d1c11141SFrank Chang     target_ulong tdata1 = env->tdata1[trigger_index];
101d1c11141SFrank Chang     int trigger_type = get_trigger_type(env, trigger_index);
102d1c11141SFrank Chang     trigger_action_t action = DBG_ACTION_NONE;
103d1c11141SFrank Chang 
104d1c11141SFrank Chang     switch (trigger_type) {
105d1c11141SFrank Chang     case TRIGGER_TYPE_AD_MATCH:
106d1c11141SFrank Chang         action = (tdata1 & TYPE2_ACTION) >> 12;
107d1c11141SFrank Chang         break;
108c472c142SFrank Chang     case TRIGGER_TYPE_AD_MATCH6:
109c472c142SFrank Chang         action = (tdata1 & TYPE6_ACTION) >> 12;
110c472c142SFrank Chang         break;
111d1c11141SFrank Chang     case TRIGGER_TYPE_INST_CNT:
112d1c11141SFrank Chang     case TRIGGER_TYPE_INT:
113d1c11141SFrank Chang     case TRIGGER_TYPE_EXCP:
114d1c11141SFrank Chang     case TRIGGER_TYPE_EXT_SRC:
115d1c11141SFrank Chang         qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n",
116d1c11141SFrank Chang                       trigger_type);
117d1c11141SFrank Chang         break;
118d1c11141SFrank Chang     case TRIGGER_TYPE_NO_EXIST:
119d1c11141SFrank Chang     case TRIGGER_TYPE_UNAVAIL:
120d1c11141SFrank Chang         qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n",
121d1c11141SFrank Chang                       trigger_type);
122d1c11141SFrank Chang         break;
123d1c11141SFrank Chang     default:
124d1c11141SFrank Chang         g_assert_not_reached();
125d1c11141SFrank Chang     }
126d1c11141SFrank Chang 
127d1c11141SFrank Chang     return action;
128d1c11141SFrank Chang }
129d1c11141SFrank Chang 
1309d5a84dbSFrank Chang static inline target_ulong build_tdata1(CPURISCVState *env,
1319d5a84dbSFrank Chang                                         trigger_type_t type,
1329d5a84dbSFrank Chang                                         bool dmode, target_ulong data)
13395799e36SBin Meng {
13495799e36SBin Meng     target_ulong tdata1;
13595799e36SBin Meng 
13695799e36SBin Meng     switch (riscv_cpu_mxl(env)) {
13795799e36SBin Meng     case MXL_RV32:
1389d5a84dbSFrank Chang         tdata1 = RV32_TYPE(type) |
1399d5a84dbSFrank Chang                  (dmode ? RV32_DMODE : 0) |
1409d5a84dbSFrank Chang                  (data & RV32_DATA_MASK);
14195799e36SBin Meng         break;
14295799e36SBin Meng     case MXL_RV64:
143d1d85412SFrédéric Pétrot     case MXL_RV128:
1449d5a84dbSFrank Chang         tdata1 = RV64_TYPE(type) |
1459d5a84dbSFrank Chang                  (dmode ? RV64_DMODE : 0) |
1469d5a84dbSFrank Chang                  (data & RV64_DATA_MASK);
14795799e36SBin Meng         break;
14895799e36SBin Meng     default:
14995799e36SBin Meng         g_assert_not_reached();
15095799e36SBin Meng     }
15195799e36SBin Meng 
15295799e36SBin Meng     return tdata1;
15395799e36SBin Meng }
15495799e36SBin Meng 
15595799e36SBin Meng bool tdata_available(CPURISCVState *env, int tdata_index)
15695799e36SBin Meng {
157a42bd001SFrank Chang     int trigger_type = get_trigger_type(env, env->trigger_cur);
158a42bd001SFrank Chang 
15995799e36SBin Meng     if (unlikely(tdata_index >= TDATA_NUM)) {
16095799e36SBin Meng         return false;
16195799e36SBin Meng     }
16295799e36SBin Meng 
163a42bd001SFrank Chang     return tdata_mapping[trigger_type][tdata_index];
16495799e36SBin Meng }
16595799e36SBin Meng 
16695799e36SBin Meng target_ulong tselect_csr_read(CPURISCVState *env)
16795799e36SBin Meng {
16895799e36SBin Meng     return env->trigger_cur;
16995799e36SBin Meng }
17095799e36SBin Meng 
17195799e36SBin Meng void tselect_csr_write(CPURISCVState *env, target_ulong val)
17295799e36SBin Meng {
1736ea8d3fcSFrank Chang     if (val < RV_MAX_TRIGGERS) {
17495799e36SBin Meng         env->trigger_cur = val;
17595799e36SBin Meng     }
1766ea8d3fcSFrank Chang }
17795799e36SBin Meng 
17895799e36SBin Meng static target_ulong tdata1_validate(CPURISCVState *env, target_ulong val,
17995799e36SBin Meng                                     trigger_type_t t)
18095799e36SBin Meng {
18195799e36SBin Meng     uint32_t type, dmode;
18295799e36SBin Meng     target_ulong tdata1;
18395799e36SBin Meng 
18495799e36SBin Meng     switch (riscv_cpu_mxl(env)) {
18595799e36SBin Meng     case MXL_RV32:
18695799e36SBin Meng         type = extract32(val, 28, 4);
18795799e36SBin Meng         dmode = extract32(val, 27, 1);
18895799e36SBin Meng         tdata1 = RV32_TYPE(t);
18995799e36SBin Meng         break;
19095799e36SBin Meng     case MXL_RV64:
191d1d85412SFrédéric Pétrot     case MXL_RV128:
19295799e36SBin Meng         type = extract64(val, 60, 4);
19395799e36SBin Meng         dmode = extract64(val, 59, 1);
19495799e36SBin Meng         tdata1 = RV64_TYPE(t);
19595799e36SBin Meng         break;
19695799e36SBin Meng     default:
19795799e36SBin Meng         g_assert_not_reached();
19895799e36SBin Meng     }
19995799e36SBin Meng 
20095799e36SBin Meng     if (type != t) {
20195799e36SBin Meng         qemu_log_mask(LOG_GUEST_ERROR,
20295799e36SBin Meng                       "ignoring type write to tdata1 register\n");
20395799e36SBin Meng     }
204a42bd001SFrank Chang 
20595799e36SBin Meng     if (dmode != 0) {
20695799e36SBin Meng         qemu_log_mask(LOG_UNIMP, "debug mode is not supported\n");
20795799e36SBin Meng     }
20895799e36SBin Meng 
20995799e36SBin Meng     return tdata1;
21095799e36SBin Meng }
21195799e36SBin Meng 
21295799e36SBin Meng static inline void warn_always_zero_bit(target_ulong val, target_ulong mask,
21395799e36SBin Meng                                         const char *msg)
21495799e36SBin Meng {
21595799e36SBin Meng     if (val & mask) {
21695799e36SBin Meng         qemu_log_mask(LOG_UNIMP, "%s bit is always zero\n", msg);
21795799e36SBin Meng     }
21895799e36SBin Meng }
21995799e36SBin Meng 
220*c4db48ccSAlvin Chang static target_ulong textra_validate(CPURISCVState *env, target_ulong tdata3)
221*c4db48ccSAlvin Chang {
222*c4db48ccSAlvin Chang     target_ulong mhvalue, mhselect;
223*c4db48ccSAlvin Chang     target_ulong mhselect_new;
224*c4db48ccSAlvin Chang     target_ulong textra;
225*c4db48ccSAlvin Chang     const uint32_t mhselect_no_rvh[8] = { 0, 0, 0, 0, 4, 4, 4, 4 };
226*c4db48ccSAlvin Chang 
227*c4db48ccSAlvin Chang     switch (riscv_cpu_mxl(env)) {
228*c4db48ccSAlvin Chang     case MXL_RV32:
229*c4db48ccSAlvin Chang         mhvalue  = get_field(tdata3, TEXTRA32_MHVALUE);
230*c4db48ccSAlvin Chang         mhselect = get_field(tdata3, TEXTRA32_MHSELECT);
231*c4db48ccSAlvin Chang         /* Validate unimplemented (always zero) bits */
232*c4db48ccSAlvin Chang         warn_always_zero_bit(tdata3, (target_ulong)TEXTRA32_SBYTEMASK,
233*c4db48ccSAlvin Chang                              "sbytemask");
234*c4db48ccSAlvin Chang         warn_always_zero_bit(tdata3, (target_ulong)TEXTRA32_SVALUE,
235*c4db48ccSAlvin Chang                              "svalue");
236*c4db48ccSAlvin Chang         warn_always_zero_bit(tdata3, (target_ulong)TEXTRA32_SSELECT,
237*c4db48ccSAlvin Chang                              "sselect");
238*c4db48ccSAlvin Chang         break;
239*c4db48ccSAlvin Chang     case MXL_RV64:
240*c4db48ccSAlvin Chang     case MXL_RV128:
241*c4db48ccSAlvin Chang         mhvalue  = get_field(tdata3, TEXTRA64_MHVALUE);
242*c4db48ccSAlvin Chang         mhselect = get_field(tdata3, TEXTRA64_MHSELECT);
243*c4db48ccSAlvin Chang         /* Validate unimplemented (always zero) bits */
244*c4db48ccSAlvin Chang         warn_always_zero_bit(tdata3, (target_ulong)TEXTRA64_SBYTEMASK,
245*c4db48ccSAlvin Chang                              "sbytemask");
246*c4db48ccSAlvin Chang         warn_always_zero_bit(tdata3, (target_ulong)TEXTRA64_SVALUE,
247*c4db48ccSAlvin Chang                              "svalue");
248*c4db48ccSAlvin Chang         warn_always_zero_bit(tdata3, (target_ulong)TEXTRA64_SSELECT,
249*c4db48ccSAlvin Chang                              "sselect");
250*c4db48ccSAlvin Chang         break;
251*c4db48ccSAlvin Chang     default:
252*c4db48ccSAlvin Chang         g_assert_not_reached();
253*c4db48ccSAlvin Chang     }
254*c4db48ccSAlvin Chang 
255*c4db48ccSAlvin Chang     /* Validate mhselect. */
256*c4db48ccSAlvin Chang     mhselect_new = mhselect_no_rvh[mhselect];
257*c4db48ccSAlvin Chang     if (mhselect != mhselect_new) {
258*c4db48ccSAlvin Chang         qemu_log_mask(LOG_UNIMP, "mhselect only supports 0 or 4 for now\n");
259*c4db48ccSAlvin Chang     }
260*c4db48ccSAlvin Chang 
261*c4db48ccSAlvin Chang     /* Write legal values into textra */
262*c4db48ccSAlvin Chang     textra = 0;
263*c4db48ccSAlvin Chang     switch (riscv_cpu_mxl(env)) {
264*c4db48ccSAlvin Chang     case MXL_RV32:
265*c4db48ccSAlvin Chang         textra = set_field(textra, TEXTRA32_MHVALUE,  mhvalue);
266*c4db48ccSAlvin Chang         textra = set_field(textra, TEXTRA32_MHSELECT, mhselect_new);
267*c4db48ccSAlvin Chang         break;
268*c4db48ccSAlvin Chang     case MXL_RV64:
269*c4db48ccSAlvin Chang     case MXL_RV128:
270*c4db48ccSAlvin Chang         textra = set_field(textra, TEXTRA64_MHVALUE,  mhvalue);
271*c4db48ccSAlvin Chang         textra = set_field(textra, TEXTRA64_MHSELECT, mhselect_new);
272*c4db48ccSAlvin Chang         break;
273*c4db48ccSAlvin Chang     default:
274*c4db48ccSAlvin Chang         g_assert_not_reached();
275*c4db48ccSAlvin Chang     }
276*c4db48ccSAlvin Chang 
277*c4db48ccSAlvin Chang     return textra;
278*c4db48ccSAlvin Chang }
279*c4db48ccSAlvin Chang 
280d1c11141SFrank Chang static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index)
281d1c11141SFrank Chang {
282d1c11141SFrank Chang     trigger_action_t action = get_trigger_action(env, trigger_index);
283d1c11141SFrank Chang 
284d1c11141SFrank Chang     switch (action) {
285d1c11141SFrank Chang     case DBG_ACTION_NONE:
286d1c11141SFrank Chang         break;
287d1c11141SFrank Chang     case DBG_ACTION_BP:
288d1c11141SFrank Chang         riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0);
289d1c11141SFrank Chang         break;
290d1c11141SFrank Chang     case DBG_ACTION_DBG_MODE:
291d1c11141SFrank Chang     case DBG_ACTION_TRACE0:
292d1c11141SFrank Chang     case DBG_ACTION_TRACE1:
293d1c11141SFrank Chang     case DBG_ACTION_TRACE2:
294d1c11141SFrank Chang     case DBG_ACTION_TRACE3:
295d1c11141SFrank Chang     case DBG_ACTION_EXT_DBG0:
296d1c11141SFrank Chang     case DBG_ACTION_EXT_DBG1:
297d1c11141SFrank Chang         qemu_log_mask(LOG_UNIMP, "action: %d is not supported\n", action);
298d1c11141SFrank Chang         break;
299d1c11141SFrank Chang     default:
300d1c11141SFrank Chang         g_assert_not_reached();
301d1c11141SFrank Chang     }
302d1c11141SFrank Chang }
303d1c11141SFrank Chang 
3045e20b889SAlvin Chang /*
3055e20b889SAlvin Chang  * Check the privilege level of specific trigger matches CPU's current privilege
3065e20b889SAlvin Chang  * level.
3075e20b889SAlvin Chang  */
3085e20b889SAlvin Chang static bool trigger_priv_match(CPURISCVState *env, trigger_type_t type,
3095e20b889SAlvin Chang                                int trigger_index)
3105e20b889SAlvin Chang {
3115e20b889SAlvin Chang     target_ulong ctrl = env->tdata1[trigger_index];
3125e20b889SAlvin Chang 
3135e20b889SAlvin Chang     switch (type) {
3145e20b889SAlvin Chang     case TRIGGER_TYPE_AD_MATCH:
3155e20b889SAlvin Chang         /* type 2 trigger cannot be fired in VU/VS mode */
3165e20b889SAlvin Chang         if (env->virt_enabled) {
3175e20b889SAlvin Chang             return false;
3185e20b889SAlvin Chang         }
3195e20b889SAlvin Chang         /* check U/S/M bit against current privilege level */
3205e20b889SAlvin Chang         if ((ctrl >> 3) & BIT(env->priv)) {
3215e20b889SAlvin Chang             return true;
3225e20b889SAlvin Chang         }
3235e20b889SAlvin Chang         break;
3245e20b889SAlvin Chang     case TRIGGER_TYPE_AD_MATCH6:
3255e20b889SAlvin Chang         if (env->virt_enabled) {
3265e20b889SAlvin Chang             /* check VU/VS bit against current privilege level */
3275e20b889SAlvin Chang             if ((ctrl >> 23) & BIT(env->priv)) {
3285e20b889SAlvin Chang                 return true;
3295e20b889SAlvin Chang             }
3305e20b889SAlvin Chang         } else {
3315e20b889SAlvin Chang             /* check U/S/M bit against current privilege level */
3325e20b889SAlvin Chang             if ((ctrl >> 3) & BIT(env->priv)) {
3335e20b889SAlvin Chang                 return true;
3345e20b889SAlvin Chang             }
3355e20b889SAlvin Chang         }
3365e20b889SAlvin Chang         break;
3375e20b889SAlvin Chang     case TRIGGER_TYPE_INST_CNT:
3385e20b889SAlvin Chang         if (env->virt_enabled) {
3395e20b889SAlvin Chang             /* check VU/VS bit against current privilege level */
3405e20b889SAlvin Chang             if ((ctrl >> 25) & BIT(env->priv)) {
3415e20b889SAlvin Chang                 return true;
3425e20b889SAlvin Chang             }
3435e20b889SAlvin Chang         } else {
3445e20b889SAlvin Chang             /* check U/S/M bit against current privilege level */
3455e20b889SAlvin Chang             if ((ctrl >> 6) & BIT(env->priv)) {
3465e20b889SAlvin Chang                 return true;
3475e20b889SAlvin Chang             }
3485e20b889SAlvin Chang         }
3495e20b889SAlvin Chang         break;
3505e20b889SAlvin Chang     case TRIGGER_TYPE_INT:
3515e20b889SAlvin Chang     case TRIGGER_TYPE_EXCP:
3525e20b889SAlvin Chang     case TRIGGER_TYPE_EXT_SRC:
3535e20b889SAlvin Chang         qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n", type);
3545e20b889SAlvin Chang         break;
3555e20b889SAlvin Chang     case TRIGGER_TYPE_NO_EXIST:
3565e20b889SAlvin Chang     case TRIGGER_TYPE_UNAVAIL:
3575e20b889SAlvin Chang         qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exist\n",
3585e20b889SAlvin Chang                       type);
3595e20b889SAlvin Chang         break;
3605e20b889SAlvin Chang     default:
3615e20b889SAlvin Chang         g_assert_not_reached();
3625e20b889SAlvin Chang     }
3635e20b889SAlvin Chang 
3645e20b889SAlvin Chang     return false;
3655e20b889SAlvin Chang }
3665e20b889SAlvin Chang 
3675e20b889SAlvin Chang /* Common matching conditions for all types of the triggers. */
3685e20b889SAlvin Chang static bool trigger_common_match(CPURISCVState *env, trigger_type_t type,
3695e20b889SAlvin Chang                                  int trigger_index)
3705e20b889SAlvin Chang {
3715e20b889SAlvin Chang     return trigger_priv_match(env, type, trigger_index);
3725e20b889SAlvin Chang }
3735e20b889SAlvin Chang 
3749495c488SFrank Chang /* type 2 trigger */
3759495c488SFrank Chang 
37695799e36SBin Meng static uint32_t type2_breakpoint_size(CPURISCVState *env, target_ulong ctrl)
37795799e36SBin Meng {
37866997c42SMarkus Armbruster     uint32_t sizelo, sizehi = 0;
37995799e36SBin Meng 
38095799e36SBin Meng     if (riscv_cpu_mxl(env) == MXL_RV64) {
38195799e36SBin Meng         sizehi = extract32(ctrl, 21, 2);
38295799e36SBin Meng     }
38395799e36SBin Meng     sizelo = extract32(ctrl, 16, 2);
38466997c42SMarkus Armbruster     return (sizehi << 2) | sizelo;
38595799e36SBin Meng }
38695799e36SBin Meng 
38795799e36SBin Meng static inline bool type2_breakpoint_enabled(target_ulong ctrl)
38895799e36SBin Meng {
38995799e36SBin Meng     bool mode = !!(ctrl & (TYPE2_U | TYPE2_S | TYPE2_M));
39095799e36SBin Meng     bool rwx = !!(ctrl & (TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC));
39195799e36SBin Meng 
39295799e36SBin Meng     return mode && rwx;
39395799e36SBin Meng }
39495799e36SBin Meng 
39595799e36SBin Meng static target_ulong type2_mcontrol_validate(CPURISCVState *env,
39695799e36SBin Meng                                             target_ulong ctrl)
39795799e36SBin Meng {
39895799e36SBin Meng     target_ulong val;
39995799e36SBin Meng     uint32_t size;
40095799e36SBin Meng 
40195799e36SBin Meng     /* validate the generic part first */
40295799e36SBin Meng     val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH);
40395799e36SBin Meng 
40495799e36SBin Meng     /* validate unimplemented (always zero) bits */
40595799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_MATCH, "match");
40695799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain");
40795799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_ACTION, "action");
40895799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing");
40995799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_SELECT, "select");
41095799e36SBin Meng     warn_always_zero_bit(ctrl, TYPE2_HIT, "hit");
41195799e36SBin Meng 
41295799e36SBin Meng     /* validate size encoding */
41395799e36SBin Meng     size = type2_breakpoint_size(env, ctrl);
41495799e36SBin Meng     if (access_size[size] == -1) {
415246f8796SWeiwei Li         qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using "
416246f8796SWeiwei Li                                  "SIZE_ANY\n", size);
41795799e36SBin Meng     } else {
41895799e36SBin Meng         val |= (ctrl & TYPE2_SIZELO);
41995799e36SBin Meng         if (riscv_cpu_mxl(env) == MXL_RV64) {
42095799e36SBin Meng             val |= (ctrl & TYPE2_SIZEHI);
42195799e36SBin Meng         }
42295799e36SBin Meng     }
42395799e36SBin Meng 
42495799e36SBin Meng     /* keep the mode and attribute bits */
42595799e36SBin Meng     val |= (ctrl & (TYPE2_U | TYPE2_S | TYPE2_M |
42695799e36SBin Meng                     TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC));
42795799e36SBin Meng 
42895799e36SBin Meng     return val;
42995799e36SBin Meng }
43095799e36SBin Meng 
43195799e36SBin Meng static void type2_breakpoint_insert(CPURISCVState *env, target_ulong index)
43295799e36SBin Meng {
4339495c488SFrank Chang     target_ulong ctrl = env->tdata1[index];
4349495c488SFrank Chang     target_ulong addr = env->tdata2[index];
43595799e36SBin Meng     bool enabled = type2_breakpoint_enabled(ctrl);
43695799e36SBin Meng     CPUState *cs = env_cpu(env);
43795799e36SBin Meng     int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
43895799e36SBin Meng     uint32_t size;
43995799e36SBin Meng 
44095799e36SBin Meng     if (!enabled) {
44195799e36SBin Meng         return;
44295799e36SBin Meng     }
44395799e36SBin Meng 
44495799e36SBin Meng     if (ctrl & TYPE2_EXEC) {
4459495c488SFrank Chang         cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]);
44695799e36SBin Meng     }
44795799e36SBin Meng 
44895799e36SBin Meng     if (ctrl & TYPE2_LOAD) {
44995799e36SBin Meng         flags |= BP_MEM_READ;
45095799e36SBin Meng     }
45195799e36SBin Meng     if (ctrl & TYPE2_STORE) {
45295799e36SBin Meng         flags |= BP_MEM_WRITE;
45395799e36SBin Meng     }
45495799e36SBin Meng 
45595799e36SBin Meng     if (flags & BP_MEM_ACCESS) {
45695799e36SBin Meng         size = type2_breakpoint_size(env, ctrl);
45795799e36SBin Meng         if (size != 0) {
45895799e36SBin Meng             cpu_watchpoint_insert(cs, addr, size, flags,
4599495c488SFrank Chang                                   &env->cpu_watchpoint[index]);
46095799e36SBin Meng         } else {
46195799e36SBin Meng             cpu_watchpoint_insert(cs, addr, 8, flags,
4629495c488SFrank Chang                                   &env->cpu_watchpoint[index]);
46395799e36SBin Meng         }
46495799e36SBin Meng     }
46595799e36SBin Meng }
46695799e36SBin Meng 
46795799e36SBin Meng static void type2_breakpoint_remove(CPURISCVState *env, target_ulong index)
46895799e36SBin Meng {
46995799e36SBin Meng     CPUState *cs = env_cpu(env);
47095799e36SBin Meng 
4719495c488SFrank Chang     if (env->cpu_breakpoint[index]) {
4729495c488SFrank Chang         cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]);
4739495c488SFrank Chang         env->cpu_breakpoint[index] = NULL;
47495799e36SBin Meng     }
47595799e36SBin Meng 
4769495c488SFrank Chang     if (env->cpu_watchpoint[index]) {
4779495c488SFrank Chang         cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]);
4789495c488SFrank Chang         env->cpu_watchpoint[index] = NULL;
47995799e36SBin Meng     }
48095799e36SBin Meng }
48195799e36SBin Meng 
482a42bd001SFrank Chang static void type2_reg_write(CPURISCVState *env, target_ulong index,
48395799e36SBin Meng                             int tdata_index, target_ulong val)
48495799e36SBin Meng {
48595799e36SBin Meng     target_ulong new_val;
48695799e36SBin Meng 
48795799e36SBin Meng     switch (tdata_index) {
48895799e36SBin Meng     case TDATA1:
48995799e36SBin Meng         new_val = type2_mcontrol_validate(env, val);
4909495c488SFrank Chang         if (new_val != env->tdata1[index]) {
4919495c488SFrank Chang             env->tdata1[index] = new_val;
49295799e36SBin Meng             type2_breakpoint_remove(env, index);
49395799e36SBin Meng             type2_breakpoint_insert(env, index);
49495799e36SBin Meng         }
49595799e36SBin Meng         break;
49695799e36SBin Meng     case TDATA2:
4979495c488SFrank Chang         if (val != env->tdata2[index]) {
4989495c488SFrank Chang             env->tdata2[index] = val;
49995799e36SBin Meng             type2_breakpoint_remove(env, index);
50095799e36SBin Meng             type2_breakpoint_insert(env, index);
50195799e36SBin Meng         }
50295799e36SBin Meng         break;
5039495c488SFrank Chang     case TDATA3:
504*c4db48ccSAlvin Chang         env->tdata3[index] = textra_validate(env, val);
5059495c488SFrank Chang         break;
50695799e36SBin Meng     default:
50795799e36SBin Meng         g_assert_not_reached();
50895799e36SBin Meng     }
50995799e36SBin Meng 
51095799e36SBin Meng     return;
51195799e36SBin Meng }
51295799e36SBin Meng 
513c472c142SFrank Chang /* type 6 trigger */
514c472c142SFrank Chang 
515c472c142SFrank Chang static inline bool type6_breakpoint_enabled(target_ulong ctrl)
516c472c142SFrank Chang {
517c472c142SFrank Chang     bool mode = !!(ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M));
518c472c142SFrank Chang     bool rwx = !!(ctrl & (TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC));
519c472c142SFrank Chang 
520c472c142SFrank Chang     return mode && rwx;
521c472c142SFrank Chang }
522c472c142SFrank Chang 
523c472c142SFrank Chang static target_ulong type6_mcontrol6_validate(CPURISCVState *env,
524c472c142SFrank Chang                                              target_ulong ctrl)
525c472c142SFrank Chang {
526c472c142SFrank Chang     target_ulong val;
527c472c142SFrank Chang     uint32_t size;
528c472c142SFrank Chang 
529c472c142SFrank Chang     /* validate the generic part first */
530c472c142SFrank Chang     val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH6);
531c472c142SFrank Chang 
532c472c142SFrank Chang     /* validate unimplemented (always zero) bits */
533c472c142SFrank Chang     warn_always_zero_bit(ctrl, TYPE6_MATCH, "match");
534c472c142SFrank Chang     warn_always_zero_bit(ctrl, TYPE6_CHAIN, "chain");
535c472c142SFrank Chang     warn_always_zero_bit(ctrl, TYPE6_ACTION, "action");
536c472c142SFrank Chang     warn_always_zero_bit(ctrl, TYPE6_TIMING, "timing");
537c472c142SFrank Chang     warn_always_zero_bit(ctrl, TYPE6_SELECT, "select");
538c472c142SFrank Chang     warn_always_zero_bit(ctrl, TYPE6_HIT, "hit");
539c472c142SFrank Chang 
540c472c142SFrank Chang     /* validate size encoding */
541c472c142SFrank Chang     size = extract32(ctrl, 16, 4);
542c472c142SFrank Chang     if (access_size[size] == -1) {
543246f8796SWeiwei Li         qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using "
544246f8796SWeiwei Li                                  "SIZE_ANY\n", size);
545c472c142SFrank Chang     } else {
546c472c142SFrank Chang         val |= (ctrl & TYPE6_SIZE);
547c472c142SFrank Chang     }
548c472c142SFrank Chang 
549c472c142SFrank Chang     /* keep the mode and attribute bits */
550c472c142SFrank Chang     val |= (ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M |
551c472c142SFrank Chang                     TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC));
552c472c142SFrank Chang 
553c472c142SFrank Chang     return val;
554c472c142SFrank Chang }
555c472c142SFrank Chang 
556c472c142SFrank Chang static void type6_breakpoint_insert(CPURISCVState *env, target_ulong index)
557c472c142SFrank Chang {
558c472c142SFrank Chang     target_ulong ctrl = env->tdata1[index];
559c472c142SFrank Chang     target_ulong addr = env->tdata2[index];
560c472c142SFrank Chang     bool enabled = type6_breakpoint_enabled(ctrl);
561c472c142SFrank Chang     CPUState *cs = env_cpu(env);
562c472c142SFrank Chang     int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
563c472c142SFrank Chang     uint32_t size;
564c472c142SFrank Chang 
565c472c142SFrank Chang     if (!enabled) {
566c472c142SFrank Chang         return;
567c472c142SFrank Chang     }
568c472c142SFrank Chang 
569c472c142SFrank Chang     if (ctrl & TYPE6_EXEC) {
570c472c142SFrank Chang         cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]);
571c472c142SFrank Chang     }
572c472c142SFrank Chang 
573c472c142SFrank Chang     if (ctrl & TYPE6_LOAD) {
574c472c142SFrank Chang         flags |= BP_MEM_READ;
575c472c142SFrank Chang     }
576c472c142SFrank Chang 
577c472c142SFrank Chang     if (ctrl & TYPE6_STORE) {
578c472c142SFrank Chang         flags |= BP_MEM_WRITE;
579c472c142SFrank Chang     }
580c472c142SFrank Chang 
581c472c142SFrank Chang     if (flags & BP_MEM_ACCESS) {
582c472c142SFrank Chang         size = extract32(ctrl, 16, 4);
583c472c142SFrank Chang         if (size != 0) {
584c472c142SFrank Chang             cpu_watchpoint_insert(cs, addr, size, flags,
585c472c142SFrank Chang                                   &env->cpu_watchpoint[index]);
586c472c142SFrank Chang         } else {
587c472c142SFrank Chang             cpu_watchpoint_insert(cs, addr, 8, flags,
588c472c142SFrank Chang                                   &env->cpu_watchpoint[index]);
589c472c142SFrank Chang         }
590c472c142SFrank Chang     }
591c472c142SFrank Chang }
592c472c142SFrank Chang 
593c472c142SFrank Chang static void type6_breakpoint_remove(CPURISCVState *env, target_ulong index)
594c472c142SFrank Chang {
595c472c142SFrank Chang     type2_breakpoint_remove(env, index);
596c472c142SFrank Chang }
597c472c142SFrank Chang 
598c472c142SFrank Chang static void type6_reg_write(CPURISCVState *env, target_ulong index,
599c472c142SFrank Chang                             int tdata_index, target_ulong val)
600c472c142SFrank Chang {
601c472c142SFrank Chang     target_ulong new_val;
602c472c142SFrank Chang 
603c472c142SFrank Chang     switch (tdata_index) {
604c472c142SFrank Chang     case TDATA1:
605c472c142SFrank Chang         new_val = type6_mcontrol6_validate(env, val);
606c472c142SFrank Chang         if (new_val != env->tdata1[index]) {
607c472c142SFrank Chang             env->tdata1[index] = new_val;
608c472c142SFrank Chang             type6_breakpoint_remove(env, index);
609c472c142SFrank Chang             type6_breakpoint_insert(env, index);
610c472c142SFrank Chang         }
611c472c142SFrank Chang         break;
612c472c142SFrank Chang     case TDATA2:
613c472c142SFrank Chang         if (val != env->tdata2[index]) {
614c472c142SFrank Chang             env->tdata2[index] = val;
615c472c142SFrank Chang             type6_breakpoint_remove(env, index);
616c472c142SFrank Chang             type6_breakpoint_insert(env, index);
617c472c142SFrank Chang         }
618c472c142SFrank Chang         break;
619c472c142SFrank Chang     case TDATA3:
620*c4db48ccSAlvin Chang         env->tdata3[index] = textra_validate(env, val);
621c472c142SFrank Chang         break;
622c472c142SFrank Chang     default:
623c472c142SFrank Chang         g_assert_not_reached();
624c472c142SFrank Chang     }
625c472c142SFrank Chang 
626c472c142SFrank Chang     return;
627c472c142SFrank Chang }
628c472c142SFrank Chang 
6292c9d7471SLIU Zhiwei /* icount trigger type */
6302c9d7471SLIU Zhiwei static inline int
6312c9d7471SLIU Zhiwei itrigger_get_count(CPURISCVState *env, int index)
6322c9d7471SLIU Zhiwei {
6332c9d7471SLIU Zhiwei     return get_field(env->tdata1[index], ITRIGGER_COUNT);
6342c9d7471SLIU Zhiwei }
6352c9d7471SLIU Zhiwei 
6362c9d7471SLIU Zhiwei static inline void
6372c9d7471SLIU Zhiwei itrigger_set_count(CPURISCVState *env, int index, int value)
6382c9d7471SLIU Zhiwei {
6392c9d7471SLIU Zhiwei     env->tdata1[index] = set_field(env->tdata1[index],
6402c9d7471SLIU Zhiwei                                    ITRIGGER_COUNT, value);
6412c9d7471SLIU Zhiwei }
6422c9d7471SLIU Zhiwei 
6432c9d7471SLIU Zhiwei static bool check_itrigger_priv(CPURISCVState *env, int index)
6442c9d7471SLIU Zhiwei {
6452c9d7471SLIU Zhiwei     target_ulong tdata1 = env->tdata1[index];
64638256529SWeiwei Li     if (env->virt_enabled) {
6472c9d7471SLIU Zhiwei         /* check VU/VS bit against current privilege level */
6482c9d7471SLIU Zhiwei         return (get_field(tdata1, ITRIGGER_VS) == env->priv) ||
6492c9d7471SLIU Zhiwei                (get_field(tdata1, ITRIGGER_VU) == env->priv);
6502c9d7471SLIU Zhiwei     } else {
6512c9d7471SLIU Zhiwei         /* check U/S/M bit against current privilege level */
6522c9d7471SLIU Zhiwei         return (get_field(tdata1, ITRIGGER_M) == env->priv) ||
6532c9d7471SLIU Zhiwei                (get_field(tdata1, ITRIGGER_S) == env->priv) ||
6542c9d7471SLIU Zhiwei                (get_field(tdata1, ITRIGGER_U) == env->priv);
6552c9d7471SLIU Zhiwei     }
6562c9d7471SLIU Zhiwei }
6572c9d7471SLIU Zhiwei 
6582c9d7471SLIU Zhiwei bool riscv_itrigger_enabled(CPURISCVState *env)
6592c9d7471SLIU Zhiwei {
6602c9d7471SLIU Zhiwei     int count;
6612c9d7471SLIU Zhiwei     for (int i = 0; i < RV_MAX_TRIGGERS; i++) {
6622c9d7471SLIU Zhiwei         if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) {
6632c9d7471SLIU Zhiwei             continue;
6642c9d7471SLIU Zhiwei         }
6652c9d7471SLIU Zhiwei         if (check_itrigger_priv(env, i)) {
6662c9d7471SLIU Zhiwei             continue;
6672c9d7471SLIU Zhiwei         }
6682c9d7471SLIU Zhiwei         count = itrigger_get_count(env, i);
6692c9d7471SLIU Zhiwei         if (!count) {
6702c9d7471SLIU Zhiwei             continue;
6712c9d7471SLIU Zhiwei         }
6722c9d7471SLIU Zhiwei         return true;
6732c9d7471SLIU Zhiwei     }
6742c9d7471SLIU Zhiwei 
6752c9d7471SLIU Zhiwei     return false;
6762c9d7471SLIU Zhiwei }
6772c9d7471SLIU Zhiwei 
6782c9d7471SLIU Zhiwei void helper_itrigger_match(CPURISCVState *env)
6792c9d7471SLIU Zhiwei {
6802c9d7471SLIU Zhiwei     int count;
6812c9d7471SLIU Zhiwei     for (int i = 0; i < RV_MAX_TRIGGERS; i++) {
6822c9d7471SLIU Zhiwei         if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) {
6832c9d7471SLIU Zhiwei             continue;
6842c9d7471SLIU Zhiwei         }
6852f5a2315SAlvin Chang         if (!trigger_common_match(env, TRIGGER_TYPE_INST_CNT, i)) {
6862c9d7471SLIU Zhiwei             continue;
6872c9d7471SLIU Zhiwei         }
6882c9d7471SLIU Zhiwei         count = itrigger_get_count(env, i);
6892c9d7471SLIU Zhiwei         if (!count) {
6902c9d7471SLIU Zhiwei             continue;
6912c9d7471SLIU Zhiwei         }
6922c9d7471SLIU Zhiwei         itrigger_set_count(env, i, count--);
6932c9d7471SLIU Zhiwei         if (!count) {
694577f0286SLIU Zhiwei             env->itrigger_enabled = riscv_itrigger_enabled(env);
6952c9d7471SLIU Zhiwei             do_trigger_action(env, i);
6962c9d7471SLIU Zhiwei         }
6972c9d7471SLIU Zhiwei     }
6982c9d7471SLIU Zhiwei }
6992c9d7471SLIU Zhiwei 
7005a4ae64cSLIU Zhiwei static void riscv_itrigger_update_count(CPURISCVState *env)
7015a4ae64cSLIU Zhiwei {
7025a4ae64cSLIU Zhiwei     int count, executed;
7035a4ae64cSLIU Zhiwei     /*
7045a4ae64cSLIU Zhiwei      * Record last icount, so that we can evaluate the executed instructions
70542fe7499SMichael Tokarev      * since last privilege mode change or timer expire.
7065a4ae64cSLIU Zhiwei      */
7075a4ae64cSLIU Zhiwei     int64_t last_icount = env->last_icount, current_icount;
7085a4ae64cSLIU Zhiwei     current_icount = env->last_icount = icount_get_raw();
7095a4ae64cSLIU Zhiwei 
7105a4ae64cSLIU Zhiwei     for (int i = 0; i < RV_MAX_TRIGGERS; i++) {
7115a4ae64cSLIU Zhiwei         if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) {
7125a4ae64cSLIU Zhiwei             continue;
7135a4ae64cSLIU Zhiwei         }
7145a4ae64cSLIU Zhiwei         count = itrigger_get_count(env, i);
7155a4ae64cSLIU Zhiwei         if (!count) {
7165a4ae64cSLIU Zhiwei             continue;
7175a4ae64cSLIU Zhiwei         }
7185a4ae64cSLIU Zhiwei         /*
71942fe7499SMichael Tokarev          * Only when privilege is changed or itrigger timer expires,
7205a4ae64cSLIU Zhiwei          * the count field in itrigger tdata1 register is updated.
7215a4ae64cSLIU Zhiwei          * And the count field in itrigger only contains remaining value.
7225a4ae64cSLIU Zhiwei          */
7235a4ae64cSLIU Zhiwei         if (check_itrigger_priv(env, i)) {
7245a4ae64cSLIU Zhiwei             /*
72542fe7499SMichael Tokarev              * If itrigger enabled in this privilege mode, the number of
72642fe7499SMichael Tokarev              * executed instructions since last privilege change
7275a4ae64cSLIU Zhiwei              * should be reduced from current itrigger count.
7285a4ae64cSLIU Zhiwei              */
7295a4ae64cSLIU Zhiwei             executed = current_icount - last_icount;
7305a4ae64cSLIU Zhiwei             itrigger_set_count(env, i, count - executed);
7315a4ae64cSLIU Zhiwei             if (count == executed) {
7325a4ae64cSLIU Zhiwei                 do_trigger_action(env, i);
7335a4ae64cSLIU Zhiwei             }
7345a4ae64cSLIU Zhiwei         } else {
7355a4ae64cSLIU Zhiwei             /*
73642fe7499SMichael Tokarev              * If itrigger is not enabled in this privilege mode,
7375a4ae64cSLIU Zhiwei              * the number of executed instructions will be discard and
7385a4ae64cSLIU Zhiwei              * the count field in itrigger will not change.
7395a4ae64cSLIU Zhiwei              */
7405a4ae64cSLIU Zhiwei             timer_mod(env->itrigger_timer[i],
7415a4ae64cSLIU Zhiwei                       current_icount + count);
7425a4ae64cSLIU Zhiwei         }
7435a4ae64cSLIU Zhiwei     }
7445a4ae64cSLIU Zhiwei }
7455a4ae64cSLIU Zhiwei 
7465a4ae64cSLIU Zhiwei static void riscv_itrigger_timer_cb(void *opaque)
7475a4ae64cSLIU Zhiwei {
7485a4ae64cSLIU Zhiwei     riscv_itrigger_update_count((CPURISCVState *)opaque);
7495a4ae64cSLIU Zhiwei }
7505a4ae64cSLIU Zhiwei 
7515a4ae64cSLIU Zhiwei void riscv_itrigger_update_priv(CPURISCVState *env)
7525a4ae64cSLIU Zhiwei {
7535a4ae64cSLIU Zhiwei     riscv_itrigger_update_count(env);
7545a4ae64cSLIU Zhiwei }
7555a4ae64cSLIU Zhiwei 
75691809598SLIU Zhiwei static target_ulong itrigger_validate(CPURISCVState *env,
75791809598SLIU Zhiwei                                       target_ulong ctrl)
75895799e36SBin Meng {
75991809598SLIU Zhiwei     target_ulong val;
76091809598SLIU Zhiwei 
76191809598SLIU Zhiwei     /* validate the generic part first */
76291809598SLIU Zhiwei     val = tdata1_validate(env, ctrl, TRIGGER_TYPE_INST_CNT);
76391809598SLIU Zhiwei 
76491809598SLIU Zhiwei     /* validate unimplemented (always zero) bits */
76591809598SLIU Zhiwei     warn_always_zero_bit(ctrl, ITRIGGER_ACTION, "action");
76691809598SLIU Zhiwei     warn_always_zero_bit(ctrl, ITRIGGER_HIT, "hit");
76791809598SLIU Zhiwei     warn_always_zero_bit(ctrl, ITRIGGER_PENDING, "pending");
76891809598SLIU Zhiwei 
76991809598SLIU Zhiwei     /* keep the mode and attribute bits */
77091809598SLIU Zhiwei     val |= ctrl & (ITRIGGER_VU | ITRIGGER_VS | ITRIGGER_U | ITRIGGER_S |
77191809598SLIU Zhiwei                    ITRIGGER_M | ITRIGGER_COUNT);
77291809598SLIU Zhiwei 
77391809598SLIU Zhiwei     return val;
77491809598SLIU Zhiwei }
77591809598SLIU Zhiwei 
77691809598SLIU Zhiwei static void itrigger_reg_write(CPURISCVState *env, target_ulong index,
77791809598SLIU Zhiwei                                int tdata_index, target_ulong val)
77891809598SLIU Zhiwei {
77991809598SLIU Zhiwei     target_ulong new_val;
78091809598SLIU Zhiwei 
7819495c488SFrank Chang     switch (tdata_index) {
7829495c488SFrank Chang     case TDATA1:
78391809598SLIU Zhiwei         /* set timer for icount */
78491809598SLIU Zhiwei         new_val = itrigger_validate(env, val);
78591809598SLIU Zhiwei         if (new_val != env->tdata1[index]) {
78691809598SLIU Zhiwei             env->tdata1[index] = new_val;
78791809598SLIU Zhiwei             if (icount_enabled()) {
78891809598SLIU Zhiwei                 env->last_icount = icount_get_raw();
78991809598SLIU Zhiwei                 /* set the count to timer */
79091809598SLIU Zhiwei                 timer_mod(env->itrigger_timer[index],
79191809598SLIU Zhiwei                           env->last_icount + itrigger_get_count(env, index));
792577f0286SLIU Zhiwei             } else {
793577f0286SLIU Zhiwei                 env->itrigger_enabled = riscv_itrigger_enabled(env);
79491809598SLIU Zhiwei             }
79591809598SLIU Zhiwei         }
79691809598SLIU Zhiwei         break;
79791809598SLIU Zhiwei     case TDATA2:
79891809598SLIU Zhiwei         qemu_log_mask(LOG_UNIMP,
79991809598SLIU Zhiwei                       "tdata2 is not supported for icount trigger\n");
80091809598SLIU Zhiwei         break;
80191809598SLIU Zhiwei     case TDATA3:
802*c4db48ccSAlvin Chang         env->tdata3[index] = textra_validate(env, val);
80391809598SLIU Zhiwei         break;
80491809598SLIU Zhiwei     default:
80591809598SLIU Zhiwei         g_assert_not_reached();
80691809598SLIU Zhiwei     }
80791809598SLIU Zhiwei 
80891809598SLIU Zhiwei     return;
80991809598SLIU Zhiwei }
81091809598SLIU Zhiwei 
81191809598SLIU Zhiwei static int itrigger_get_adjust_count(CPURISCVState *env)
81291809598SLIU Zhiwei {
81391809598SLIU Zhiwei     int count = itrigger_get_count(env, env->trigger_cur), executed;
81491809598SLIU Zhiwei     if ((count != 0) && check_itrigger_priv(env, env->trigger_cur)) {
81591809598SLIU Zhiwei         executed = icount_get_raw() - env->last_icount;
81691809598SLIU Zhiwei         count += executed;
81791809598SLIU Zhiwei     }
81891809598SLIU Zhiwei     return count;
81991809598SLIU Zhiwei }
82091809598SLIU Zhiwei 
82191809598SLIU Zhiwei target_ulong tdata_csr_read(CPURISCVState *env, int tdata_index)
82291809598SLIU Zhiwei {
82391809598SLIU Zhiwei     int trigger_type;
82491809598SLIU Zhiwei     switch (tdata_index) {
82591809598SLIU Zhiwei     case TDATA1:
826246f8796SWeiwei Li         trigger_type = extract_trigger_type(env,
827246f8796SWeiwei Li                                             env->tdata1[env->trigger_cur]);
82891809598SLIU Zhiwei         if ((trigger_type == TRIGGER_TYPE_INST_CNT) && icount_enabled()) {
82991809598SLIU Zhiwei             return deposit64(env->tdata1[env->trigger_cur], 10, 14,
83091809598SLIU Zhiwei                              itrigger_get_adjust_count(env));
83191809598SLIU Zhiwei         }
8329495c488SFrank Chang         return env->tdata1[env->trigger_cur];
8339495c488SFrank Chang     case TDATA2:
8349495c488SFrank Chang         return env->tdata2[env->trigger_cur];
8359495c488SFrank Chang     case TDATA3:
8369495c488SFrank Chang         return env->tdata3[env->trigger_cur];
837a42bd001SFrank Chang     default:
838a42bd001SFrank Chang         g_assert_not_reached();
839a42bd001SFrank Chang     }
84095799e36SBin Meng }
84195799e36SBin Meng 
84295799e36SBin Meng void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val)
84395799e36SBin Meng {
844a42bd001SFrank Chang     int trigger_type;
84595799e36SBin Meng 
846a42bd001SFrank Chang     if (tdata_index == TDATA1) {
847a42bd001SFrank Chang         trigger_type = extract_trigger_type(env, val);
848a42bd001SFrank Chang     } else {
849a42bd001SFrank Chang         trigger_type = get_trigger_type(env, env->trigger_cur);
850a42bd001SFrank Chang     }
851a42bd001SFrank Chang 
852a42bd001SFrank Chang     switch (trigger_type) {
853a42bd001SFrank Chang     case TRIGGER_TYPE_AD_MATCH:
854a42bd001SFrank Chang         type2_reg_write(env, env->trigger_cur, tdata_index, val);
855a42bd001SFrank Chang         break;
856c472c142SFrank Chang     case TRIGGER_TYPE_AD_MATCH6:
857c472c142SFrank Chang         type6_reg_write(env, env->trigger_cur, tdata_index, val);
858c472c142SFrank Chang         break;
859a42bd001SFrank Chang     case TRIGGER_TYPE_INST_CNT:
86091809598SLIU Zhiwei         itrigger_reg_write(env, env->trigger_cur, tdata_index, val);
86191809598SLIU Zhiwei         break;
862a42bd001SFrank Chang     case TRIGGER_TYPE_INT:
863a42bd001SFrank Chang     case TRIGGER_TYPE_EXCP:
864a42bd001SFrank Chang     case TRIGGER_TYPE_EXT_SRC:
865a42bd001SFrank Chang         qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n",
866a42bd001SFrank Chang                       trigger_type);
867a42bd001SFrank Chang         break;
868a42bd001SFrank Chang     case TRIGGER_TYPE_NO_EXIST:
869a42bd001SFrank Chang     case TRIGGER_TYPE_UNAVAIL:
870a42bd001SFrank Chang         qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n",
871a42bd001SFrank Chang                       trigger_type);
872a42bd001SFrank Chang         break;
873a42bd001SFrank Chang     default:
874a42bd001SFrank Chang         g_assert_not_reached();
875a42bd001SFrank Chang     }
87695799e36SBin Meng }
877b5f6379dSBin Meng 
87831b9798dSFrank Chang target_ulong tinfo_csr_read(CPURISCVState *env)
87931b9798dSFrank Chang {
88031b9798dSFrank Chang     /* assume all triggers support the same types of triggers */
881c472c142SFrank Chang     return BIT(TRIGGER_TYPE_AD_MATCH) |
882c472c142SFrank Chang            BIT(TRIGGER_TYPE_AD_MATCH6);
88331b9798dSFrank Chang }
88431b9798dSFrank Chang 
885b5f6379dSBin Meng void riscv_cpu_debug_excp_handler(CPUState *cs)
886b5f6379dSBin Meng {
887b5f6379dSBin Meng     RISCVCPU *cpu = RISCV_CPU(cs);
888b5f6379dSBin Meng     CPURISCVState *env = &cpu->env;
889b5f6379dSBin Meng 
890b5f6379dSBin Meng     if (cs->watchpoint_hit) {
891b5f6379dSBin Meng         if (cs->watchpoint_hit->flags & BP_CPU) {
892d1c11141SFrank Chang             do_trigger_action(env, DBG_ACTION_BP);
893b5f6379dSBin Meng         }
894b5f6379dSBin Meng     } else {
895b5f6379dSBin Meng         if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) {
896d1c11141SFrank Chang             do_trigger_action(env, DBG_ACTION_BP);
897b5f6379dSBin Meng         }
898b5f6379dSBin Meng     }
899b5f6379dSBin Meng }
900b5f6379dSBin Meng 
901b5f6379dSBin Meng bool riscv_cpu_debug_check_breakpoint(CPUState *cs)
902b5f6379dSBin Meng {
903b5f6379dSBin Meng     RISCVCPU *cpu = RISCV_CPU(cs);
904b5f6379dSBin Meng     CPURISCVState *env = &cpu->env;
905b5f6379dSBin Meng     CPUBreakpoint *bp;
906b5f6379dSBin Meng     target_ulong ctrl;
907b5f6379dSBin Meng     target_ulong pc;
908a42bd001SFrank Chang     int trigger_type;
909b5f6379dSBin Meng     int i;
910b5f6379dSBin Meng 
911b5f6379dSBin Meng     QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
912a42bd001SFrank Chang         for (i = 0; i < RV_MAX_TRIGGERS; i++) {
913a42bd001SFrank Chang             trigger_type = get_trigger_type(env, i);
914a42bd001SFrank Chang 
9155e20b889SAlvin Chang             if (!trigger_common_match(env, trigger_type, i)) {
9165e20b889SAlvin Chang                 continue;
917c32461d8SFrank Chang             }
918c32461d8SFrank Chang 
9195e20b889SAlvin Chang             switch (trigger_type) {
9205e20b889SAlvin Chang             case TRIGGER_TYPE_AD_MATCH:
9219495c488SFrank Chang                 ctrl = env->tdata1[i];
9229495c488SFrank Chang                 pc = env->tdata2[i];
923b5f6379dSBin Meng 
924b5f6379dSBin Meng                 if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) {
9250099f605SDaniel Henrique Barboza                     env->badaddr = pc;
926b5f6379dSBin Meng                     return true;
927b5f6379dSBin Meng                 }
928a42bd001SFrank Chang                 break;
929c472c142SFrank Chang             case TRIGGER_TYPE_AD_MATCH6:
930c472c142SFrank Chang                 ctrl = env->tdata1[i];
931c472c142SFrank Chang                 pc = env->tdata2[i];
932c472c142SFrank Chang 
933c472c142SFrank Chang                 if ((ctrl & TYPE6_EXEC) && (bp->pc == pc)) {
9340099f605SDaniel Henrique Barboza                     env->badaddr = pc;
935c472c142SFrank Chang                     return true;
936c472c142SFrank Chang                 }
937c472c142SFrank Chang                 break;
938a42bd001SFrank Chang             default:
939a42bd001SFrank Chang                 /* other trigger types are not supported or irrelevant */
940a42bd001SFrank Chang                 break;
941a42bd001SFrank Chang             }
942b5f6379dSBin Meng         }
943b5f6379dSBin Meng     }
944b5f6379dSBin Meng 
945b5f6379dSBin Meng     return false;
946b5f6379dSBin Meng }
947b5f6379dSBin Meng 
948b5f6379dSBin Meng bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
949b5f6379dSBin Meng {
950b5f6379dSBin Meng     RISCVCPU *cpu = RISCV_CPU(cs);
951b5f6379dSBin Meng     CPURISCVState *env = &cpu->env;
952b5f6379dSBin Meng     target_ulong ctrl;
953b5f6379dSBin Meng     target_ulong addr;
954a42bd001SFrank Chang     int trigger_type;
955b5f6379dSBin Meng     int flags;
956b5f6379dSBin Meng     int i;
957b5f6379dSBin Meng 
958a42bd001SFrank Chang     for (i = 0; i < RV_MAX_TRIGGERS; i++) {
959a42bd001SFrank Chang         trigger_type = get_trigger_type(env, i);
960a42bd001SFrank Chang 
96172dec166SAlvin Chang         if (!trigger_common_match(env, trigger_type, i)) {
96272dec166SAlvin Chang             continue;
963c32461d8SFrank Chang         }
964c32461d8SFrank Chang 
96572dec166SAlvin Chang         switch (trigger_type) {
96672dec166SAlvin Chang         case TRIGGER_TYPE_AD_MATCH:
9679495c488SFrank Chang             ctrl = env->tdata1[i];
9689495c488SFrank Chang             addr = env->tdata2[i];
969b5f6379dSBin Meng             flags = 0;
970b5f6379dSBin Meng 
971b5f6379dSBin Meng             if (ctrl & TYPE2_LOAD) {
972b5f6379dSBin Meng                 flags |= BP_MEM_READ;
973b5f6379dSBin Meng             }
974b5f6379dSBin Meng             if (ctrl & TYPE2_STORE) {
975b5f6379dSBin Meng                 flags |= BP_MEM_WRITE;
976b5f6379dSBin Meng             }
977b5f6379dSBin Meng 
978b5f6379dSBin Meng             if ((wp->flags & flags) && (wp->vaddr == addr)) {
979b5f6379dSBin Meng                 return true;
980b5f6379dSBin Meng             }
981a42bd001SFrank Chang             break;
982c472c142SFrank Chang         case TRIGGER_TYPE_AD_MATCH6:
983c472c142SFrank Chang             ctrl = env->tdata1[i];
984c472c142SFrank Chang             addr = env->tdata2[i];
985c472c142SFrank Chang             flags = 0;
986c472c142SFrank Chang 
987c472c142SFrank Chang             if (ctrl & TYPE6_LOAD) {
988c472c142SFrank Chang                 flags |= BP_MEM_READ;
989c472c142SFrank Chang             }
990c472c142SFrank Chang             if (ctrl & TYPE6_STORE) {
991c472c142SFrank Chang                 flags |= BP_MEM_WRITE;
992c472c142SFrank Chang             }
993c472c142SFrank Chang 
994c472c142SFrank Chang             if ((wp->flags & flags) && (wp->vaddr == addr)) {
995c472c142SFrank Chang                 return true;
996c472c142SFrank Chang             }
997c472c142SFrank Chang             break;
998a42bd001SFrank Chang         default:
999a42bd001SFrank Chang             /* other trigger types are not supported */
1000a42bd001SFrank Chang             break;
1001a42bd001SFrank Chang         }
1002b5f6379dSBin Meng     }
1003b5f6379dSBin Meng 
1004b5f6379dSBin Meng     return false;
1005b5f6379dSBin Meng }
1006b6092544SBin Meng 
1007a7c272dfSAkihiko Odaki void riscv_trigger_realize(CPURISCVState *env)
1008a7c272dfSAkihiko Odaki {
1009a7c272dfSAkihiko Odaki     int i;
1010a7c272dfSAkihiko Odaki 
1011a7c272dfSAkihiko Odaki     for (i = 0; i < RV_MAX_TRIGGERS; i++) {
1012a7c272dfSAkihiko Odaki         env->itrigger_timer[i] = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1013a7c272dfSAkihiko Odaki                                               riscv_itrigger_timer_cb, env);
1014a7c272dfSAkihiko Odaki     }
1015a7c272dfSAkihiko Odaki }
1016a7c272dfSAkihiko Odaki 
1017a7c272dfSAkihiko Odaki void riscv_trigger_reset_hold(CPURISCVState *env)
1018b6092544SBin Meng {
10199d5a84dbSFrank Chang     target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_AD_MATCH, 0, 0);
1020b6092544SBin Meng     int i;
1021b6092544SBin Meng 
1022a42bd001SFrank Chang     /* init to type 2 triggers */
1023a42bd001SFrank Chang     for (i = 0; i < RV_MAX_TRIGGERS; i++) {
1024b6092544SBin Meng         /*
1025b6092544SBin Meng          * type = TRIGGER_TYPE_AD_MATCH
1026b6092544SBin Meng          * dmode = 0 (both debug and M-mode can write tdata)
1027b6092544SBin Meng          * maskmax = 0 (unimplemented, always 0)
1028b6092544SBin Meng          * sizehi = 0 (match against any size, RV64 only)
1029b6092544SBin Meng          * hit = 0 (unimplemented, always 0)
1030b6092544SBin Meng          * select = 0 (always 0, perform match on address)
1031b6092544SBin Meng          * timing = 0 (always 0, trigger before instruction)
1032b6092544SBin Meng          * sizelo = 0 (match against any size)
1033b6092544SBin Meng          * action = 0 (always 0, raise a breakpoint exception)
1034b6092544SBin Meng          * chain = 0 (unimplemented, always 0)
1035b6092544SBin Meng          * match = 0 (always 0, when any compare value equals tdata2)
1036b6092544SBin Meng          */
10379495c488SFrank Chang         env->tdata1[i] = tdata1;
10389495c488SFrank Chang         env->tdata2[i] = 0;
10399495c488SFrank Chang         env->tdata3[i] = 0;
10409495c488SFrank Chang         env->cpu_breakpoint[i] = NULL;
10419495c488SFrank Chang         env->cpu_watchpoint[i] = NULL;
1042a7c272dfSAkihiko Odaki         timer_del(env->itrigger_timer[i]);
1043b6092544SBin Meng     }
10440c4e579aSAlvin Chang 
10450c4e579aSAlvin Chang     env->mcontext = 0;
1046b6092544SBin Meng }
1047