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 220c4db48ccSAlvin Chang static target_ulong textra_validate(CPURISCVState *env, target_ulong tdata3) 221c4db48ccSAlvin Chang { 222c4db48ccSAlvin Chang target_ulong mhvalue, mhselect; 223c4db48ccSAlvin Chang target_ulong mhselect_new; 224c4db48ccSAlvin Chang target_ulong textra; 225c4db48ccSAlvin Chang const uint32_t mhselect_no_rvh[8] = { 0, 0, 0, 0, 4, 4, 4, 4 }; 226c4db48ccSAlvin Chang 227c4db48ccSAlvin Chang switch (riscv_cpu_mxl(env)) { 228c4db48ccSAlvin Chang case MXL_RV32: 229c4db48ccSAlvin Chang mhvalue = get_field(tdata3, TEXTRA32_MHVALUE); 230c4db48ccSAlvin Chang mhselect = get_field(tdata3, TEXTRA32_MHSELECT); 231c4db48ccSAlvin Chang /* Validate unimplemented (always zero) bits */ 232c4db48ccSAlvin Chang warn_always_zero_bit(tdata3, (target_ulong)TEXTRA32_SBYTEMASK, 233c4db48ccSAlvin Chang "sbytemask"); 234c4db48ccSAlvin Chang warn_always_zero_bit(tdata3, (target_ulong)TEXTRA32_SVALUE, 235c4db48ccSAlvin Chang "svalue"); 236c4db48ccSAlvin Chang warn_always_zero_bit(tdata3, (target_ulong)TEXTRA32_SSELECT, 237c4db48ccSAlvin Chang "sselect"); 238c4db48ccSAlvin Chang break; 239c4db48ccSAlvin Chang case MXL_RV64: 240c4db48ccSAlvin Chang case MXL_RV128: 241c4db48ccSAlvin Chang mhvalue = get_field(tdata3, TEXTRA64_MHVALUE); 242c4db48ccSAlvin Chang mhselect = get_field(tdata3, TEXTRA64_MHSELECT); 243c4db48ccSAlvin Chang /* Validate unimplemented (always zero) bits */ 244c4db48ccSAlvin Chang warn_always_zero_bit(tdata3, (target_ulong)TEXTRA64_SBYTEMASK, 245c4db48ccSAlvin Chang "sbytemask"); 246c4db48ccSAlvin Chang warn_always_zero_bit(tdata3, (target_ulong)TEXTRA64_SVALUE, 247c4db48ccSAlvin Chang "svalue"); 248c4db48ccSAlvin Chang warn_always_zero_bit(tdata3, (target_ulong)TEXTRA64_SSELECT, 249c4db48ccSAlvin Chang "sselect"); 250c4db48ccSAlvin Chang break; 251c4db48ccSAlvin Chang default: 252c4db48ccSAlvin Chang g_assert_not_reached(); 253c4db48ccSAlvin Chang } 254c4db48ccSAlvin Chang 255c4db48ccSAlvin Chang /* Validate mhselect. */ 256c4db48ccSAlvin Chang mhselect_new = mhselect_no_rvh[mhselect]; 257c4db48ccSAlvin Chang if (mhselect != mhselect_new) { 258c4db48ccSAlvin Chang qemu_log_mask(LOG_UNIMP, "mhselect only supports 0 or 4 for now\n"); 259c4db48ccSAlvin Chang } 260c4db48ccSAlvin Chang 261c4db48ccSAlvin Chang /* Write legal values into textra */ 262c4db48ccSAlvin Chang textra = 0; 263c4db48ccSAlvin Chang switch (riscv_cpu_mxl(env)) { 264c4db48ccSAlvin Chang case MXL_RV32: 265c4db48ccSAlvin Chang textra = set_field(textra, TEXTRA32_MHVALUE, mhvalue); 266c4db48ccSAlvin Chang textra = set_field(textra, TEXTRA32_MHSELECT, mhselect_new); 267c4db48ccSAlvin Chang break; 268c4db48ccSAlvin Chang case MXL_RV64: 269c4db48ccSAlvin Chang case MXL_RV128: 270c4db48ccSAlvin Chang textra = set_field(textra, TEXTRA64_MHVALUE, mhvalue); 271c4db48ccSAlvin Chang textra = set_field(textra, TEXTRA64_MHSELECT, mhselect_new); 272c4db48ccSAlvin Chang break; 273c4db48ccSAlvin Chang default: 274c4db48ccSAlvin Chang g_assert_not_reached(); 275c4db48ccSAlvin Chang } 276c4db48ccSAlvin Chang 277c4db48ccSAlvin Chang return textra; 278c4db48ccSAlvin Chang } 279c4db48ccSAlvin 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 367*6ffe9b66SAlvin Chang static bool trigger_textra_match(CPURISCVState *env, trigger_type_t type, 368*6ffe9b66SAlvin Chang int trigger_index) 369*6ffe9b66SAlvin Chang { 370*6ffe9b66SAlvin Chang target_ulong textra = env->tdata3[trigger_index]; 371*6ffe9b66SAlvin Chang target_ulong mhvalue, mhselect; 372*6ffe9b66SAlvin Chang 373*6ffe9b66SAlvin Chang if (type < TRIGGER_TYPE_AD_MATCH || type > TRIGGER_TYPE_AD_MATCH6) { 374*6ffe9b66SAlvin Chang /* textra checking is only applicable when type is 2, 3, 4, 5, or 6 */ 375*6ffe9b66SAlvin Chang return true; 376*6ffe9b66SAlvin Chang } 377*6ffe9b66SAlvin Chang 378*6ffe9b66SAlvin Chang switch (riscv_cpu_mxl(env)) { 379*6ffe9b66SAlvin Chang case MXL_RV32: 380*6ffe9b66SAlvin Chang mhvalue = get_field(textra, TEXTRA32_MHVALUE); 381*6ffe9b66SAlvin Chang mhselect = get_field(textra, TEXTRA32_MHSELECT); 382*6ffe9b66SAlvin Chang break; 383*6ffe9b66SAlvin Chang case MXL_RV64: 384*6ffe9b66SAlvin Chang case MXL_RV128: 385*6ffe9b66SAlvin Chang mhvalue = get_field(textra, TEXTRA64_MHVALUE); 386*6ffe9b66SAlvin Chang mhselect = get_field(textra, TEXTRA64_MHSELECT); 387*6ffe9b66SAlvin Chang break; 388*6ffe9b66SAlvin Chang default: 389*6ffe9b66SAlvin Chang g_assert_not_reached(); 390*6ffe9b66SAlvin Chang } 391*6ffe9b66SAlvin Chang 392*6ffe9b66SAlvin Chang /* Check mhvalue and mhselect. */ 393*6ffe9b66SAlvin Chang switch (mhselect) { 394*6ffe9b66SAlvin Chang case MHSELECT_IGNORE: 395*6ffe9b66SAlvin Chang break; 396*6ffe9b66SAlvin Chang case MHSELECT_MCONTEXT: 397*6ffe9b66SAlvin Chang /* Match if the low bits of mcontext/hcontext equal mhvalue. */ 398*6ffe9b66SAlvin Chang if (mhvalue != env->mcontext) { 399*6ffe9b66SAlvin Chang return false; 400*6ffe9b66SAlvin Chang } 401*6ffe9b66SAlvin Chang break; 402*6ffe9b66SAlvin Chang default: 403*6ffe9b66SAlvin Chang break; 404*6ffe9b66SAlvin Chang } 405*6ffe9b66SAlvin Chang 406*6ffe9b66SAlvin Chang return true; 407*6ffe9b66SAlvin Chang } 408*6ffe9b66SAlvin Chang 4095e20b889SAlvin Chang /* Common matching conditions for all types of the triggers. */ 4105e20b889SAlvin Chang static bool trigger_common_match(CPURISCVState *env, trigger_type_t type, 4115e20b889SAlvin Chang int trigger_index) 4125e20b889SAlvin Chang { 413*6ffe9b66SAlvin Chang return trigger_priv_match(env, type, trigger_index) && 414*6ffe9b66SAlvin Chang trigger_textra_match(env, type, trigger_index); 4155e20b889SAlvin Chang } 4165e20b889SAlvin Chang 4179495c488SFrank Chang /* type 2 trigger */ 4189495c488SFrank Chang 41995799e36SBin Meng static uint32_t type2_breakpoint_size(CPURISCVState *env, target_ulong ctrl) 42095799e36SBin Meng { 42166997c42SMarkus Armbruster uint32_t sizelo, sizehi = 0; 42295799e36SBin Meng 42395799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 42495799e36SBin Meng sizehi = extract32(ctrl, 21, 2); 42595799e36SBin Meng } 42695799e36SBin Meng sizelo = extract32(ctrl, 16, 2); 42766997c42SMarkus Armbruster return (sizehi << 2) | sizelo; 42895799e36SBin Meng } 42995799e36SBin Meng 43095799e36SBin Meng static inline bool type2_breakpoint_enabled(target_ulong ctrl) 43195799e36SBin Meng { 43295799e36SBin Meng bool mode = !!(ctrl & (TYPE2_U | TYPE2_S | TYPE2_M)); 43395799e36SBin Meng bool rwx = !!(ctrl & (TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 43495799e36SBin Meng 43595799e36SBin Meng return mode && rwx; 43695799e36SBin Meng } 43795799e36SBin Meng 43895799e36SBin Meng static target_ulong type2_mcontrol_validate(CPURISCVState *env, 43995799e36SBin Meng target_ulong ctrl) 44095799e36SBin Meng { 44195799e36SBin Meng target_ulong val; 44295799e36SBin Meng uint32_t size; 44395799e36SBin Meng 44495799e36SBin Meng /* validate the generic part first */ 44595799e36SBin Meng val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH); 44695799e36SBin Meng 44795799e36SBin Meng /* validate unimplemented (always zero) bits */ 44895799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_MATCH, "match"); 44995799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain"); 45095799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_ACTION, "action"); 45195799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing"); 45295799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_SELECT, "select"); 45395799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_HIT, "hit"); 45495799e36SBin Meng 45595799e36SBin Meng /* validate size encoding */ 45695799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 45795799e36SBin Meng if (access_size[size] == -1) { 458246f8796SWeiwei Li qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using " 459246f8796SWeiwei Li "SIZE_ANY\n", size); 46095799e36SBin Meng } else { 46195799e36SBin Meng val |= (ctrl & TYPE2_SIZELO); 46295799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 46395799e36SBin Meng val |= (ctrl & TYPE2_SIZEHI); 46495799e36SBin Meng } 46595799e36SBin Meng } 46695799e36SBin Meng 46795799e36SBin Meng /* keep the mode and attribute bits */ 46895799e36SBin Meng val |= (ctrl & (TYPE2_U | TYPE2_S | TYPE2_M | 46995799e36SBin Meng TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 47095799e36SBin Meng 47195799e36SBin Meng return val; 47295799e36SBin Meng } 47395799e36SBin Meng 47495799e36SBin Meng static void type2_breakpoint_insert(CPURISCVState *env, target_ulong index) 47595799e36SBin Meng { 4769495c488SFrank Chang target_ulong ctrl = env->tdata1[index]; 4779495c488SFrank Chang target_ulong addr = env->tdata2[index]; 47895799e36SBin Meng bool enabled = type2_breakpoint_enabled(ctrl); 47995799e36SBin Meng CPUState *cs = env_cpu(env); 48095799e36SBin Meng int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 48195799e36SBin Meng uint32_t size; 48295799e36SBin Meng 48395799e36SBin Meng if (!enabled) { 48495799e36SBin Meng return; 48595799e36SBin Meng } 48695799e36SBin Meng 48795799e36SBin Meng if (ctrl & TYPE2_EXEC) { 4889495c488SFrank Chang cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]); 48995799e36SBin Meng } 49095799e36SBin Meng 49195799e36SBin Meng if (ctrl & TYPE2_LOAD) { 49295799e36SBin Meng flags |= BP_MEM_READ; 49395799e36SBin Meng } 49495799e36SBin Meng if (ctrl & TYPE2_STORE) { 49595799e36SBin Meng flags |= BP_MEM_WRITE; 49695799e36SBin Meng } 49795799e36SBin Meng 49895799e36SBin Meng if (flags & BP_MEM_ACCESS) { 49995799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 50095799e36SBin Meng if (size != 0) { 50195799e36SBin Meng cpu_watchpoint_insert(cs, addr, size, flags, 5029495c488SFrank Chang &env->cpu_watchpoint[index]); 50395799e36SBin Meng } else { 50495799e36SBin Meng cpu_watchpoint_insert(cs, addr, 8, flags, 5059495c488SFrank Chang &env->cpu_watchpoint[index]); 50695799e36SBin Meng } 50795799e36SBin Meng } 50895799e36SBin Meng } 50995799e36SBin Meng 51095799e36SBin Meng static void type2_breakpoint_remove(CPURISCVState *env, target_ulong index) 51195799e36SBin Meng { 51295799e36SBin Meng CPUState *cs = env_cpu(env); 51395799e36SBin Meng 5149495c488SFrank Chang if (env->cpu_breakpoint[index]) { 5159495c488SFrank Chang cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]); 5169495c488SFrank Chang env->cpu_breakpoint[index] = NULL; 51795799e36SBin Meng } 51895799e36SBin Meng 5199495c488SFrank Chang if (env->cpu_watchpoint[index]) { 5209495c488SFrank Chang cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]); 5219495c488SFrank Chang env->cpu_watchpoint[index] = NULL; 52295799e36SBin Meng } 52395799e36SBin Meng } 52495799e36SBin Meng 525a42bd001SFrank Chang static void type2_reg_write(CPURISCVState *env, target_ulong index, 52695799e36SBin Meng int tdata_index, target_ulong val) 52795799e36SBin Meng { 52895799e36SBin Meng target_ulong new_val; 52995799e36SBin Meng 53095799e36SBin Meng switch (tdata_index) { 53195799e36SBin Meng case TDATA1: 53295799e36SBin Meng new_val = type2_mcontrol_validate(env, val); 5339495c488SFrank Chang if (new_val != env->tdata1[index]) { 5349495c488SFrank Chang env->tdata1[index] = new_val; 53595799e36SBin Meng type2_breakpoint_remove(env, index); 53695799e36SBin Meng type2_breakpoint_insert(env, index); 53795799e36SBin Meng } 53895799e36SBin Meng break; 53995799e36SBin Meng case TDATA2: 5409495c488SFrank Chang if (val != env->tdata2[index]) { 5419495c488SFrank Chang env->tdata2[index] = val; 54295799e36SBin Meng type2_breakpoint_remove(env, index); 54395799e36SBin Meng type2_breakpoint_insert(env, index); 54495799e36SBin Meng } 54595799e36SBin Meng break; 5469495c488SFrank Chang case TDATA3: 547c4db48ccSAlvin Chang env->tdata3[index] = textra_validate(env, val); 5489495c488SFrank Chang break; 54995799e36SBin Meng default: 55095799e36SBin Meng g_assert_not_reached(); 55195799e36SBin Meng } 55295799e36SBin Meng 55395799e36SBin Meng return; 55495799e36SBin Meng } 55595799e36SBin Meng 556c472c142SFrank Chang /* type 6 trigger */ 557c472c142SFrank Chang 558c472c142SFrank Chang static inline bool type6_breakpoint_enabled(target_ulong ctrl) 559c472c142SFrank Chang { 560c472c142SFrank Chang bool mode = !!(ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M)); 561c472c142SFrank Chang bool rwx = !!(ctrl & (TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC)); 562c472c142SFrank Chang 563c472c142SFrank Chang return mode && rwx; 564c472c142SFrank Chang } 565c472c142SFrank Chang 566c472c142SFrank Chang static target_ulong type6_mcontrol6_validate(CPURISCVState *env, 567c472c142SFrank Chang target_ulong ctrl) 568c472c142SFrank Chang { 569c472c142SFrank Chang target_ulong val; 570c472c142SFrank Chang uint32_t size; 571c472c142SFrank Chang 572c472c142SFrank Chang /* validate the generic part first */ 573c472c142SFrank Chang val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH6); 574c472c142SFrank Chang 575c472c142SFrank Chang /* validate unimplemented (always zero) bits */ 576c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_MATCH, "match"); 577c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_CHAIN, "chain"); 578c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_ACTION, "action"); 579c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_TIMING, "timing"); 580c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_SELECT, "select"); 581c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_HIT, "hit"); 582c472c142SFrank Chang 583c472c142SFrank Chang /* validate size encoding */ 584c472c142SFrank Chang size = extract32(ctrl, 16, 4); 585c472c142SFrank Chang if (access_size[size] == -1) { 586246f8796SWeiwei Li qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using " 587246f8796SWeiwei Li "SIZE_ANY\n", size); 588c472c142SFrank Chang } else { 589c472c142SFrank Chang val |= (ctrl & TYPE6_SIZE); 590c472c142SFrank Chang } 591c472c142SFrank Chang 592c472c142SFrank Chang /* keep the mode and attribute bits */ 593c472c142SFrank Chang val |= (ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M | 594c472c142SFrank Chang TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC)); 595c472c142SFrank Chang 596c472c142SFrank Chang return val; 597c472c142SFrank Chang } 598c472c142SFrank Chang 599c472c142SFrank Chang static void type6_breakpoint_insert(CPURISCVState *env, target_ulong index) 600c472c142SFrank Chang { 601c472c142SFrank Chang target_ulong ctrl = env->tdata1[index]; 602c472c142SFrank Chang target_ulong addr = env->tdata2[index]; 603c472c142SFrank Chang bool enabled = type6_breakpoint_enabled(ctrl); 604c472c142SFrank Chang CPUState *cs = env_cpu(env); 605c472c142SFrank Chang int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 606c472c142SFrank Chang uint32_t size; 607c472c142SFrank Chang 608c472c142SFrank Chang if (!enabled) { 609c472c142SFrank Chang return; 610c472c142SFrank Chang } 611c472c142SFrank Chang 612c472c142SFrank Chang if (ctrl & TYPE6_EXEC) { 613c472c142SFrank Chang cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]); 614c472c142SFrank Chang } 615c472c142SFrank Chang 616c472c142SFrank Chang if (ctrl & TYPE6_LOAD) { 617c472c142SFrank Chang flags |= BP_MEM_READ; 618c472c142SFrank Chang } 619c472c142SFrank Chang 620c472c142SFrank Chang if (ctrl & TYPE6_STORE) { 621c472c142SFrank Chang flags |= BP_MEM_WRITE; 622c472c142SFrank Chang } 623c472c142SFrank Chang 624c472c142SFrank Chang if (flags & BP_MEM_ACCESS) { 625c472c142SFrank Chang size = extract32(ctrl, 16, 4); 626c472c142SFrank Chang if (size != 0) { 627c472c142SFrank Chang cpu_watchpoint_insert(cs, addr, size, flags, 628c472c142SFrank Chang &env->cpu_watchpoint[index]); 629c472c142SFrank Chang } else { 630c472c142SFrank Chang cpu_watchpoint_insert(cs, addr, 8, flags, 631c472c142SFrank Chang &env->cpu_watchpoint[index]); 632c472c142SFrank Chang } 633c472c142SFrank Chang } 634c472c142SFrank Chang } 635c472c142SFrank Chang 636c472c142SFrank Chang static void type6_breakpoint_remove(CPURISCVState *env, target_ulong index) 637c472c142SFrank Chang { 638c472c142SFrank Chang type2_breakpoint_remove(env, index); 639c472c142SFrank Chang } 640c472c142SFrank Chang 641c472c142SFrank Chang static void type6_reg_write(CPURISCVState *env, target_ulong index, 642c472c142SFrank Chang int tdata_index, target_ulong val) 643c472c142SFrank Chang { 644c472c142SFrank Chang target_ulong new_val; 645c472c142SFrank Chang 646c472c142SFrank Chang switch (tdata_index) { 647c472c142SFrank Chang case TDATA1: 648c472c142SFrank Chang new_val = type6_mcontrol6_validate(env, val); 649c472c142SFrank Chang if (new_val != env->tdata1[index]) { 650c472c142SFrank Chang env->tdata1[index] = new_val; 651c472c142SFrank Chang type6_breakpoint_remove(env, index); 652c472c142SFrank Chang type6_breakpoint_insert(env, index); 653c472c142SFrank Chang } 654c472c142SFrank Chang break; 655c472c142SFrank Chang case TDATA2: 656c472c142SFrank Chang if (val != env->tdata2[index]) { 657c472c142SFrank Chang env->tdata2[index] = val; 658c472c142SFrank Chang type6_breakpoint_remove(env, index); 659c472c142SFrank Chang type6_breakpoint_insert(env, index); 660c472c142SFrank Chang } 661c472c142SFrank Chang break; 662c472c142SFrank Chang case TDATA3: 663c4db48ccSAlvin Chang env->tdata3[index] = textra_validate(env, val); 664c472c142SFrank Chang break; 665c472c142SFrank Chang default: 666c472c142SFrank Chang g_assert_not_reached(); 667c472c142SFrank Chang } 668c472c142SFrank Chang 669c472c142SFrank Chang return; 670c472c142SFrank Chang } 671c472c142SFrank Chang 6722c9d7471SLIU Zhiwei /* icount trigger type */ 6732c9d7471SLIU Zhiwei static inline int 6742c9d7471SLIU Zhiwei itrigger_get_count(CPURISCVState *env, int index) 6752c9d7471SLIU Zhiwei { 6762c9d7471SLIU Zhiwei return get_field(env->tdata1[index], ITRIGGER_COUNT); 6772c9d7471SLIU Zhiwei } 6782c9d7471SLIU Zhiwei 6792c9d7471SLIU Zhiwei static inline void 6802c9d7471SLIU Zhiwei itrigger_set_count(CPURISCVState *env, int index, int value) 6812c9d7471SLIU Zhiwei { 6822c9d7471SLIU Zhiwei env->tdata1[index] = set_field(env->tdata1[index], 6832c9d7471SLIU Zhiwei ITRIGGER_COUNT, value); 6842c9d7471SLIU Zhiwei } 6852c9d7471SLIU Zhiwei 6862c9d7471SLIU Zhiwei static bool check_itrigger_priv(CPURISCVState *env, int index) 6872c9d7471SLIU Zhiwei { 6882c9d7471SLIU Zhiwei target_ulong tdata1 = env->tdata1[index]; 68938256529SWeiwei Li if (env->virt_enabled) { 6902c9d7471SLIU Zhiwei /* check VU/VS bit against current privilege level */ 6912c9d7471SLIU Zhiwei return (get_field(tdata1, ITRIGGER_VS) == env->priv) || 6922c9d7471SLIU Zhiwei (get_field(tdata1, ITRIGGER_VU) == env->priv); 6932c9d7471SLIU Zhiwei } else { 6942c9d7471SLIU Zhiwei /* check U/S/M bit against current privilege level */ 6952c9d7471SLIU Zhiwei return (get_field(tdata1, ITRIGGER_M) == env->priv) || 6962c9d7471SLIU Zhiwei (get_field(tdata1, ITRIGGER_S) == env->priv) || 6972c9d7471SLIU Zhiwei (get_field(tdata1, ITRIGGER_U) == env->priv); 6982c9d7471SLIU Zhiwei } 6992c9d7471SLIU Zhiwei } 7002c9d7471SLIU Zhiwei 7012c9d7471SLIU Zhiwei bool riscv_itrigger_enabled(CPURISCVState *env) 7022c9d7471SLIU Zhiwei { 7032c9d7471SLIU Zhiwei int count; 7042c9d7471SLIU Zhiwei for (int i = 0; i < RV_MAX_TRIGGERS; i++) { 7052c9d7471SLIU Zhiwei if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) { 7062c9d7471SLIU Zhiwei continue; 7072c9d7471SLIU Zhiwei } 7082c9d7471SLIU Zhiwei if (check_itrigger_priv(env, i)) { 7092c9d7471SLIU Zhiwei continue; 7102c9d7471SLIU Zhiwei } 7112c9d7471SLIU Zhiwei count = itrigger_get_count(env, i); 7122c9d7471SLIU Zhiwei if (!count) { 7132c9d7471SLIU Zhiwei continue; 7142c9d7471SLIU Zhiwei } 7152c9d7471SLIU Zhiwei return true; 7162c9d7471SLIU Zhiwei } 7172c9d7471SLIU Zhiwei 7182c9d7471SLIU Zhiwei return false; 7192c9d7471SLIU Zhiwei } 7202c9d7471SLIU Zhiwei 7212c9d7471SLIU Zhiwei void helper_itrigger_match(CPURISCVState *env) 7222c9d7471SLIU Zhiwei { 7232c9d7471SLIU Zhiwei int count; 7242c9d7471SLIU Zhiwei for (int i = 0; i < RV_MAX_TRIGGERS; i++) { 7252c9d7471SLIU Zhiwei if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) { 7262c9d7471SLIU Zhiwei continue; 7272c9d7471SLIU Zhiwei } 7282f5a2315SAlvin Chang if (!trigger_common_match(env, TRIGGER_TYPE_INST_CNT, i)) { 7292c9d7471SLIU Zhiwei continue; 7302c9d7471SLIU Zhiwei } 7312c9d7471SLIU Zhiwei count = itrigger_get_count(env, i); 7322c9d7471SLIU Zhiwei if (!count) { 7332c9d7471SLIU Zhiwei continue; 7342c9d7471SLIU Zhiwei } 7352c9d7471SLIU Zhiwei itrigger_set_count(env, i, count--); 7362c9d7471SLIU Zhiwei if (!count) { 737577f0286SLIU Zhiwei env->itrigger_enabled = riscv_itrigger_enabled(env); 7382c9d7471SLIU Zhiwei do_trigger_action(env, i); 7392c9d7471SLIU Zhiwei } 7402c9d7471SLIU Zhiwei } 7412c9d7471SLIU Zhiwei } 7422c9d7471SLIU Zhiwei 7435a4ae64cSLIU Zhiwei static void riscv_itrigger_update_count(CPURISCVState *env) 7445a4ae64cSLIU Zhiwei { 7455a4ae64cSLIU Zhiwei int count, executed; 7465a4ae64cSLIU Zhiwei /* 7475a4ae64cSLIU Zhiwei * Record last icount, so that we can evaluate the executed instructions 74842fe7499SMichael Tokarev * since last privilege mode change or timer expire. 7495a4ae64cSLIU Zhiwei */ 7505a4ae64cSLIU Zhiwei int64_t last_icount = env->last_icount, current_icount; 7515a4ae64cSLIU Zhiwei current_icount = env->last_icount = icount_get_raw(); 7525a4ae64cSLIU Zhiwei 7535a4ae64cSLIU Zhiwei for (int i = 0; i < RV_MAX_TRIGGERS; i++) { 7545a4ae64cSLIU Zhiwei if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) { 7555a4ae64cSLIU Zhiwei continue; 7565a4ae64cSLIU Zhiwei } 7575a4ae64cSLIU Zhiwei count = itrigger_get_count(env, i); 7585a4ae64cSLIU Zhiwei if (!count) { 7595a4ae64cSLIU Zhiwei continue; 7605a4ae64cSLIU Zhiwei } 7615a4ae64cSLIU Zhiwei /* 76242fe7499SMichael Tokarev * Only when privilege is changed or itrigger timer expires, 7635a4ae64cSLIU Zhiwei * the count field in itrigger tdata1 register is updated. 7645a4ae64cSLIU Zhiwei * And the count field in itrigger only contains remaining value. 7655a4ae64cSLIU Zhiwei */ 7665a4ae64cSLIU Zhiwei if (check_itrigger_priv(env, i)) { 7675a4ae64cSLIU Zhiwei /* 76842fe7499SMichael Tokarev * If itrigger enabled in this privilege mode, the number of 76942fe7499SMichael Tokarev * executed instructions since last privilege change 7705a4ae64cSLIU Zhiwei * should be reduced from current itrigger count. 7715a4ae64cSLIU Zhiwei */ 7725a4ae64cSLIU Zhiwei executed = current_icount - last_icount; 7735a4ae64cSLIU Zhiwei itrigger_set_count(env, i, count - executed); 7745a4ae64cSLIU Zhiwei if (count == executed) { 7755a4ae64cSLIU Zhiwei do_trigger_action(env, i); 7765a4ae64cSLIU Zhiwei } 7775a4ae64cSLIU Zhiwei } else { 7785a4ae64cSLIU Zhiwei /* 77942fe7499SMichael Tokarev * If itrigger is not enabled in this privilege mode, 7805a4ae64cSLIU Zhiwei * the number of executed instructions will be discard and 7815a4ae64cSLIU Zhiwei * the count field in itrigger will not change. 7825a4ae64cSLIU Zhiwei */ 7835a4ae64cSLIU Zhiwei timer_mod(env->itrigger_timer[i], 7845a4ae64cSLIU Zhiwei current_icount + count); 7855a4ae64cSLIU Zhiwei } 7865a4ae64cSLIU Zhiwei } 7875a4ae64cSLIU Zhiwei } 7885a4ae64cSLIU Zhiwei 7895a4ae64cSLIU Zhiwei static void riscv_itrigger_timer_cb(void *opaque) 7905a4ae64cSLIU Zhiwei { 7915a4ae64cSLIU Zhiwei riscv_itrigger_update_count((CPURISCVState *)opaque); 7925a4ae64cSLIU Zhiwei } 7935a4ae64cSLIU Zhiwei 7945a4ae64cSLIU Zhiwei void riscv_itrigger_update_priv(CPURISCVState *env) 7955a4ae64cSLIU Zhiwei { 7965a4ae64cSLIU Zhiwei riscv_itrigger_update_count(env); 7975a4ae64cSLIU Zhiwei } 7985a4ae64cSLIU Zhiwei 79991809598SLIU Zhiwei static target_ulong itrigger_validate(CPURISCVState *env, 80091809598SLIU Zhiwei target_ulong ctrl) 80195799e36SBin Meng { 80291809598SLIU Zhiwei target_ulong val; 80391809598SLIU Zhiwei 80491809598SLIU Zhiwei /* validate the generic part first */ 80591809598SLIU Zhiwei val = tdata1_validate(env, ctrl, TRIGGER_TYPE_INST_CNT); 80691809598SLIU Zhiwei 80791809598SLIU Zhiwei /* validate unimplemented (always zero) bits */ 80891809598SLIU Zhiwei warn_always_zero_bit(ctrl, ITRIGGER_ACTION, "action"); 80991809598SLIU Zhiwei warn_always_zero_bit(ctrl, ITRIGGER_HIT, "hit"); 81091809598SLIU Zhiwei warn_always_zero_bit(ctrl, ITRIGGER_PENDING, "pending"); 81191809598SLIU Zhiwei 81291809598SLIU Zhiwei /* keep the mode and attribute bits */ 81391809598SLIU Zhiwei val |= ctrl & (ITRIGGER_VU | ITRIGGER_VS | ITRIGGER_U | ITRIGGER_S | 81491809598SLIU Zhiwei ITRIGGER_M | ITRIGGER_COUNT); 81591809598SLIU Zhiwei 81691809598SLIU Zhiwei return val; 81791809598SLIU Zhiwei } 81891809598SLIU Zhiwei 81991809598SLIU Zhiwei static void itrigger_reg_write(CPURISCVState *env, target_ulong index, 82091809598SLIU Zhiwei int tdata_index, target_ulong val) 82191809598SLIU Zhiwei { 82291809598SLIU Zhiwei target_ulong new_val; 82391809598SLIU Zhiwei 8249495c488SFrank Chang switch (tdata_index) { 8259495c488SFrank Chang case TDATA1: 82691809598SLIU Zhiwei /* set timer for icount */ 82791809598SLIU Zhiwei new_val = itrigger_validate(env, val); 82891809598SLIU Zhiwei if (new_val != env->tdata1[index]) { 82991809598SLIU Zhiwei env->tdata1[index] = new_val; 83091809598SLIU Zhiwei if (icount_enabled()) { 83191809598SLIU Zhiwei env->last_icount = icount_get_raw(); 83291809598SLIU Zhiwei /* set the count to timer */ 83391809598SLIU Zhiwei timer_mod(env->itrigger_timer[index], 83491809598SLIU Zhiwei env->last_icount + itrigger_get_count(env, index)); 835577f0286SLIU Zhiwei } else { 836577f0286SLIU Zhiwei env->itrigger_enabled = riscv_itrigger_enabled(env); 83791809598SLIU Zhiwei } 83891809598SLIU Zhiwei } 83991809598SLIU Zhiwei break; 84091809598SLIU Zhiwei case TDATA2: 84191809598SLIU Zhiwei qemu_log_mask(LOG_UNIMP, 84291809598SLIU Zhiwei "tdata2 is not supported for icount trigger\n"); 84391809598SLIU Zhiwei break; 84491809598SLIU Zhiwei case TDATA3: 845c4db48ccSAlvin Chang env->tdata3[index] = textra_validate(env, val); 84691809598SLIU Zhiwei break; 84791809598SLIU Zhiwei default: 84891809598SLIU Zhiwei g_assert_not_reached(); 84991809598SLIU Zhiwei } 85091809598SLIU Zhiwei 85191809598SLIU Zhiwei return; 85291809598SLIU Zhiwei } 85391809598SLIU Zhiwei 85491809598SLIU Zhiwei static int itrigger_get_adjust_count(CPURISCVState *env) 85591809598SLIU Zhiwei { 85691809598SLIU Zhiwei int count = itrigger_get_count(env, env->trigger_cur), executed; 85791809598SLIU Zhiwei if ((count != 0) && check_itrigger_priv(env, env->trigger_cur)) { 85891809598SLIU Zhiwei executed = icount_get_raw() - env->last_icount; 85991809598SLIU Zhiwei count += executed; 86091809598SLIU Zhiwei } 86191809598SLIU Zhiwei return count; 86291809598SLIU Zhiwei } 86391809598SLIU Zhiwei 86491809598SLIU Zhiwei target_ulong tdata_csr_read(CPURISCVState *env, int tdata_index) 86591809598SLIU Zhiwei { 86691809598SLIU Zhiwei int trigger_type; 86791809598SLIU Zhiwei switch (tdata_index) { 86891809598SLIU Zhiwei case TDATA1: 869246f8796SWeiwei Li trigger_type = extract_trigger_type(env, 870246f8796SWeiwei Li env->tdata1[env->trigger_cur]); 87191809598SLIU Zhiwei if ((trigger_type == TRIGGER_TYPE_INST_CNT) && icount_enabled()) { 87291809598SLIU Zhiwei return deposit64(env->tdata1[env->trigger_cur], 10, 14, 87391809598SLIU Zhiwei itrigger_get_adjust_count(env)); 87491809598SLIU Zhiwei } 8759495c488SFrank Chang return env->tdata1[env->trigger_cur]; 8769495c488SFrank Chang case TDATA2: 8779495c488SFrank Chang return env->tdata2[env->trigger_cur]; 8789495c488SFrank Chang case TDATA3: 8799495c488SFrank Chang return env->tdata3[env->trigger_cur]; 880a42bd001SFrank Chang default: 881a42bd001SFrank Chang g_assert_not_reached(); 882a42bd001SFrank Chang } 88395799e36SBin Meng } 88495799e36SBin Meng 88595799e36SBin Meng void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val) 88695799e36SBin Meng { 887a42bd001SFrank Chang int trigger_type; 88895799e36SBin Meng 889a42bd001SFrank Chang if (tdata_index == TDATA1) { 890a42bd001SFrank Chang trigger_type = extract_trigger_type(env, val); 891a42bd001SFrank Chang } else { 892a42bd001SFrank Chang trigger_type = get_trigger_type(env, env->trigger_cur); 893a42bd001SFrank Chang } 894a42bd001SFrank Chang 895a42bd001SFrank Chang switch (trigger_type) { 896a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 897a42bd001SFrank Chang type2_reg_write(env, env->trigger_cur, tdata_index, val); 898a42bd001SFrank Chang break; 899c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 900c472c142SFrank Chang type6_reg_write(env, env->trigger_cur, tdata_index, val); 901c472c142SFrank Chang break; 902a42bd001SFrank Chang case TRIGGER_TYPE_INST_CNT: 90391809598SLIU Zhiwei itrigger_reg_write(env, env->trigger_cur, tdata_index, val); 90491809598SLIU Zhiwei break; 905a42bd001SFrank Chang case TRIGGER_TYPE_INT: 906a42bd001SFrank Chang case TRIGGER_TYPE_EXCP: 907a42bd001SFrank Chang case TRIGGER_TYPE_EXT_SRC: 908a42bd001SFrank Chang qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n", 909a42bd001SFrank Chang trigger_type); 910a42bd001SFrank Chang break; 911a42bd001SFrank Chang case TRIGGER_TYPE_NO_EXIST: 912a42bd001SFrank Chang case TRIGGER_TYPE_UNAVAIL: 913a42bd001SFrank Chang qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n", 914a42bd001SFrank Chang trigger_type); 915a42bd001SFrank Chang break; 916a42bd001SFrank Chang default: 917a42bd001SFrank Chang g_assert_not_reached(); 918a42bd001SFrank Chang } 91995799e36SBin Meng } 920b5f6379dSBin Meng 92131b9798dSFrank Chang target_ulong tinfo_csr_read(CPURISCVState *env) 92231b9798dSFrank Chang { 92331b9798dSFrank Chang /* assume all triggers support the same types of triggers */ 924c472c142SFrank Chang return BIT(TRIGGER_TYPE_AD_MATCH) | 925c472c142SFrank Chang BIT(TRIGGER_TYPE_AD_MATCH6); 92631b9798dSFrank Chang } 92731b9798dSFrank Chang 928b5f6379dSBin Meng void riscv_cpu_debug_excp_handler(CPUState *cs) 929b5f6379dSBin Meng { 930b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 931b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 932b5f6379dSBin Meng 933b5f6379dSBin Meng if (cs->watchpoint_hit) { 934b5f6379dSBin Meng if (cs->watchpoint_hit->flags & BP_CPU) { 935d1c11141SFrank Chang do_trigger_action(env, DBG_ACTION_BP); 936b5f6379dSBin Meng } 937b5f6379dSBin Meng } else { 938b5f6379dSBin Meng if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) { 939d1c11141SFrank Chang do_trigger_action(env, DBG_ACTION_BP); 940b5f6379dSBin Meng } 941b5f6379dSBin Meng } 942b5f6379dSBin Meng } 943b5f6379dSBin Meng 944b5f6379dSBin Meng bool riscv_cpu_debug_check_breakpoint(CPUState *cs) 945b5f6379dSBin Meng { 946b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 947b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 948b5f6379dSBin Meng CPUBreakpoint *bp; 949b5f6379dSBin Meng target_ulong ctrl; 950b5f6379dSBin Meng target_ulong pc; 951a42bd001SFrank Chang int trigger_type; 952b5f6379dSBin Meng int i; 953b5f6379dSBin Meng 954b5f6379dSBin Meng QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { 955a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 956a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 957a42bd001SFrank Chang 9585e20b889SAlvin Chang if (!trigger_common_match(env, trigger_type, i)) { 9595e20b889SAlvin Chang continue; 960c32461d8SFrank Chang } 961c32461d8SFrank Chang 9625e20b889SAlvin Chang switch (trigger_type) { 9635e20b889SAlvin Chang case TRIGGER_TYPE_AD_MATCH: 9649495c488SFrank Chang ctrl = env->tdata1[i]; 9659495c488SFrank Chang pc = env->tdata2[i]; 966b5f6379dSBin Meng 967b5f6379dSBin Meng if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) { 9680099f605SDaniel Henrique Barboza env->badaddr = pc; 969b5f6379dSBin Meng return true; 970b5f6379dSBin Meng } 971a42bd001SFrank Chang break; 972c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 973c472c142SFrank Chang ctrl = env->tdata1[i]; 974c472c142SFrank Chang pc = env->tdata2[i]; 975c472c142SFrank Chang 976c472c142SFrank Chang if ((ctrl & TYPE6_EXEC) && (bp->pc == pc)) { 9770099f605SDaniel Henrique Barboza env->badaddr = pc; 978c472c142SFrank Chang return true; 979c472c142SFrank Chang } 980c472c142SFrank Chang break; 981a42bd001SFrank Chang default: 982a42bd001SFrank Chang /* other trigger types are not supported or irrelevant */ 983a42bd001SFrank Chang break; 984a42bd001SFrank Chang } 985b5f6379dSBin Meng } 986b5f6379dSBin Meng } 987b5f6379dSBin Meng 988b5f6379dSBin Meng return false; 989b5f6379dSBin Meng } 990b5f6379dSBin Meng 991b5f6379dSBin Meng bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp) 992b5f6379dSBin Meng { 993b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 994b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 995b5f6379dSBin Meng target_ulong ctrl; 996b5f6379dSBin Meng target_ulong addr; 997a42bd001SFrank Chang int trigger_type; 998b5f6379dSBin Meng int flags; 999b5f6379dSBin Meng int i; 1000b5f6379dSBin Meng 1001a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 1002a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 1003a42bd001SFrank Chang 100472dec166SAlvin Chang if (!trigger_common_match(env, trigger_type, i)) { 100572dec166SAlvin Chang continue; 1006c32461d8SFrank Chang } 1007c32461d8SFrank Chang 100872dec166SAlvin Chang switch (trigger_type) { 100972dec166SAlvin Chang case TRIGGER_TYPE_AD_MATCH: 10109495c488SFrank Chang ctrl = env->tdata1[i]; 10119495c488SFrank Chang addr = env->tdata2[i]; 1012b5f6379dSBin Meng flags = 0; 1013b5f6379dSBin Meng 1014b5f6379dSBin Meng if (ctrl & TYPE2_LOAD) { 1015b5f6379dSBin Meng flags |= BP_MEM_READ; 1016b5f6379dSBin Meng } 1017b5f6379dSBin Meng if (ctrl & TYPE2_STORE) { 1018b5f6379dSBin Meng flags |= BP_MEM_WRITE; 1019b5f6379dSBin Meng } 1020b5f6379dSBin Meng 1021b5f6379dSBin Meng if ((wp->flags & flags) && (wp->vaddr == addr)) { 1022b5f6379dSBin Meng return true; 1023b5f6379dSBin Meng } 1024a42bd001SFrank Chang break; 1025c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 1026c472c142SFrank Chang ctrl = env->tdata1[i]; 1027c472c142SFrank Chang addr = env->tdata2[i]; 1028c472c142SFrank Chang flags = 0; 1029c472c142SFrank Chang 1030c472c142SFrank Chang if (ctrl & TYPE6_LOAD) { 1031c472c142SFrank Chang flags |= BP_MEM_READ; 1032c472c142SFrank Chang } 1033c472c142SFrank Chang if (ctrl & TYPE6_STORE) { 1034c472c142SFrank Chang flags |= BP_MEM_WRITE; 1035c472c142SFrank Chang } 1036c472c142SFrank Chang 1037c472c142SFrank Chang if ((wp->flags & flags) && (wp->vaddr == addr)) { 1038c472c142SFrank Chang return true; 1039c472c142SFrank Chang } 1040c472c142SFrank Chang break; 1041a42bd001SFrank Chang default: 1042a42bd001SFrank Chang /* other trigger types are not supported */ 1043a42bd001SFrank Chang break; 1044a42bd001SFrank Chang } 1045b5f6379dSBin Meng } 1046b5f6379dSBin Meng 1047b5f6379dSBin Meng return false; 1048b5f6379dSBin Meng } 1049b6092544SBin Meng 1050a7c272dfSAkihiko Odaki void riscv_trigger_realize(CPURISCVState *env) 1051a7c272dfSAkihiko Odaki { 1052a7c272dfSAkihiko Odaki int i; 1053a7c272dfSAkihiko Odaki 1054a7c272dfSAkihiko Odaki for (i = 0; i < RV_MAX_TRIGGERS; i++) { 1055a7c272dfSAkihiko Odaki env->itrigger_timer[i] = timer_new_ns(QEMU_CLOCK_VIRTUAL, 1056a7c272dfSAkihiko Odaki riscv_itrigger_timer_cb, env); 1057a7c272dfSAkihiko Odaki } 1058a7c272dfSAkihiko Odaki } 1059a7c272dfSAkihiko Odaki 1060a7c272dfSAkihiko Odaki void riscv_trigger_reset_hold(CPURISCVState *env) 1061b6092544SBin Meng { 10629d5a84dbSFrank Chang target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_AD_MATCH, 0, 0); 1063b6092544SBin Meng int i; 1064b6092544SBin Meng 1065a42bd001SFrank Chang /* init to type 2 triggers */ 1066a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 1067b6092544SBin Meng /* 1068b6092544SBin Meng * type = TRIGGER_TYPE_AD_MATCH 1069b6092544SBin Meng * dmode = 0 (both debug and M-mode can write tdata) 1070b6092544SBin Meng * maskmax = 0 (unimplemented, always 0) 1071b6092544SBin Meng * sizehi = 0 (match against any size, RV64 only) 1072b6092544SBin Meng * hit = 0 (unimplemented, always 0) 1073b6092544SBin Meng * select = 0 (always 0, perform match on address) 1074b6092544SBin Meng * timing = 0 (always 0, trigger before instruction) 1075b6092544SBin Meng * sizelo = 0 (match against any size) 1076b6092544SBin Meng * action = 0 (always 0, raise a breakpoint exception) 1077b6092544SBin Meng * chain = 0 (unimplemented, always 0) 1078b6092544SBin Meng * match = 0 (always 0, when any compare value equals tdata2) 1079b6092544SBin Meng */ 10809495c488SFrank Chang env->tdata1[i] = tdata1; 10819495c488SFrank Chang env->tdata2[i] = 0; 10829495c488SFrank Chang env->tdata3[i] = 0; 10839495c488SFrank Chang env->cpu_breakpoint[i] = NULL; 10849495c488SFrank Chang env->cpu_watchpoint[i] = NULL; 1085a7c272dfSAkihiko Odaki timer_del(env->itrigger_timer[i]); 1086b6092544SBin Meng } 10870c4e579aSAlvin Chang 10880c4e579aSAlvin Chang env->mcontext = 0; 1089b6092544SBin Meng } 1090