195799e36SBin Meng /* 295799e36SBin Meng * QEMU RISC-V Native Debug Support 395799e36SBin Meng * 495799e36SBin Meng * Copyright (c) 2022 Wind River Systems, Inc. 595799e36SBin Meng * 695799e36SBin Meng * Author: 795799e36SBin Meng * Bin Meng <bin.meng@windriver.com> 895799e36SBin Meng * 995799e36SBin Meng * This provides the native debug support via the Trigger Module, as defined 1095799e36SBin Meng * in the RISC-V Debug Specification: 1195799e36SBin Meng * https://github.com/riscv/riscv-debug-spec/raw/master/riscv-debug-stable.pdf 1295799e36SBin Meng * 1395799e36SBin Meng * This program is free software; you can redistribute it and/or modify it 1495799e36SBin Meng * under the terms and conditions of the GNU General Public License, 1595799e36SBin Meng * version 2 or later, as published by the Free Software Foundation. 1695799e36SBin Meng * 1795799e36SBin Meng * This program is distributed in the hope it will be useful, but WITHOUT 1895799e36SBin Meng * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1995799e36SBin Meng * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 2095799e36SBin Meng * more details. 2195799e36SBin Meng * 2295799e36SBin Meng * You should have received a copy of the GNU General Public License along with 2395799e36SBin Meng * this program. If not, see <http://www.gnu.org/licenses/>. 2495799e36SBin Meng */ 2595799e36SBin Meng 2695799e36SBin Meng #include "qemu/osdep.h" 2795799e36SBin Meng #include "qemu/log.h" 2895799e36SBin Meng #include "qapi/error.h" 2995799e36SBin Meng #include "cpu.h" 3095799e36SBin Meng #include "trace.h" 3195799e36SBin Meng #include "exec/exec-all.h" 3295799e36SBin Meng 3395799e36SBin Meng /* 3495799e36SBin Meng * The following M-mode trigger CSRs are implemented: 3595799e36SBin Meng * 3695799e36SBin Meng * - tselect 3795799e36SBin Meng * - tdata1 3895799e36SBin Meng * - tdata2 3995799e36SBin Meng * - tdata3 4095799e36SBin Meng * 4195799e36SBin Meng * We don't support writable 'type' field in the tdata1 register, so there is 4295799e36SBin Meng * no need to implement the "tinfo" CSR. 4395799e36SBin Meng * 4495799e36SBin Meng * The following triggers are implemented: 4595799e36SBin Meng * 4695799e36SBin Meng * Index | Type | tdata mapping | Description 4795799e36SBin Meng * ------+------+------------------------+------------ 4895799e36SBin Meng * 0 | 2 | tdata1, tdata2 | Address / Data Match 4995799e36SBin Meng * 1 | 2 | tdata1, tdata2 | Address / Data Match 5095799e36SBin Meng */ 5195799e36SBin Meng 5295799e36SBin Meng /* tdata availability of a trigger */ 5395799e36SBin Meng typedef bool tdata_avail[TDATA_NUM]; 5495799e36SBin Meng 55*a42bd001SFrank Chang static tdata_avail tdata_mapping[TRIGGER_TYPE_NUM] = { 56*a42bd001SFrank Chang [TRIGGER_TYPE_NO_EXIST] = { false, false, false }, 57*a42bd001SFrank Chang [TRIGGER_TYPE_AD_MATCH] = { true, true, true }, 58*a42bd001SFrank Chang [TRIGGER_TYPE_INST_CNT] = { true, false, true }, 59*a42bd001SFrank Chang [TRIGGER_TYPE_INT] = { true, true, true }, 60*a42bd001SFrank Chang [TRIGGER_TYPE_EXCP] = { true, true, true }, 61*a42bd001SFrank Chang [TRIGGER_TYPE_AD_MATCH6] = { true, true, true }, 62*a42bd001SFrank Chang [TRIGGER_TYPE_EXT_SRC] = { true, false, false }, 63*a42bd001SFrank 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 77*a42bd001SFrank Chang static inline target_ulong extract_trigger_type(CPURISCVState *env, 78*a42bd001SFrank Chang target_ulong tdata1) 79*a42bd001SFrank Chang { 80*a42bd001SFrank Chang switch (riscv_cpu_mxl(env)) { 81*a42bd001SFrank Chang case MXL_RV32: 82*a42bd001SFrank Chang return extract32(tdata1, 28, 4); 83*a42bd001SFrank Chang case MXL_RV64: 84*a42bd001SFrank Chang case MXL_RV128: 85*a42bd001SFrank Chang return extract64(tdata1, 60, 4); 86*a42bd001SFrank Chang default: 87*a42bd001SFrank Chang g_assert_not_reached(); 88*a42bd001SFrank Chang } 89*a42bd001SFrank Chang } 90*a42bd001SFrank Chang 91*a42bd001SFrank Chang static inline target_ulong get_trigger_type(CPURISCVState *env, 92*a42bd001SFrank Chang target_ulong trigger_index) 93*a42bd001SFrank Chang { 94*a42bd001SFrank Chang target_ulong tdata1 = env->type2_trig[trigger_index].mcontrol; 95*a42bd001SFrank Chang return extract_trigger_type(env, tdata1); 96*a42bd001SFrank Chang } 97*a42bd001SFrank Chang 9895799e36SBin Meng static inline target_ulong trigger_type(CPURISCVState *env, 9995799e36SBin Meng trigger_type_t type) 10095799e36SBin Meng { 10195799e36SBin Meng target_ulong tdata1; 10295799e36SBin Meng 10395799e36SBin Meng switch (riscv_cpu_mxl(env)) { 10495799e36SBin Meng case MXL_RV32: 10595799e36SBin Meng tdata1 = RV32_TYPE(type); 10695799e36SBin Meng break; 10795799e36SBin Meng case MXL_RV64: 108d1d85412SFrédéric Pétrot case MXL_RV128: 10995799e36SBin Meng tdata1 = RV64_TYPE(type); 11095799e36SBin Meng break; 11195799e36SBin Meng default: 11295799e36SBin Meng g_assert_not_reached(); 11395799e36SBin Meng } 11495799e36SBin Meng 11595799e36SBin Meng return tdata1; 11695799e36SBin Meng } 11795799e36SBin Meng 11895799e36SBin Meng bool tdata_available(CPURISCVState *env, int tdata_index) 11995799e36SBin Meng { 120*a42bd001SFrank Chang int trigger_type = get_trigger_type(env, env->trigger_cur); 121*a42bd001SFrank Chang 12295799e36SBin Meng if (unlikely(tdata_index >= TDATA_NUM)) { 12395799e36SBin Meng return false; 12495799e36SBin Meng } 12595799e36SBin Meng 126*a42bd001SFrank Chang if (unlikely(env->trigger_cur >= RV_MAX_TRIGGERS)) { 12795799e36SBin Meng return false; 12895799e36SBin Meng } 12995799e36SBin Meng 130*a42bd001SFrank Chang return tdata_mapping[trigger_type][tdata_index]; 13195799e36SBin Meng } 13295799e36SBin Meng 13395799e36SBin Meng target_ulong tselect_csr_read(CPURISCVState *env) 13495799e36SBin Meng { 13595799e36SBin Meng return env->trigger_cur; 13695799e36SBin Meng } 13795799e36SBin Meng 13895799e36SBin Meng void tselect_csr_write(CPURISCVState *env, target_ulong val) 13995799e36SBin Meng { 14095799e36SBin Meng /* all target_ulong bits of tselect are implemented */ 14195799e36SBin Meng env->trigger_cur = val; 14295799e36SBin Meng } 14395799e36SBin Meng 14495799e36SBin Meng static target_ulong tdata1_validate(CPURISCVState *env, target_ulong val, 14595799e36SBin Meng trigger_type_t t) 14695799e36SBin Meng { 14795799e36SBin Meng uint32_t type, dmode; 14895799e36SBin Meng target_ulong tdata1; 14995799e36SBin Meng 15095799e36SBin Meng switch (riscv_cpu_mxl(env)) { 15195799e36SBin Meng case MXL_RV32: 15295799e36SBin Meng type = extract32(val, 28, 4); 15395799e36SBin Meng dmode = extract32(val, 27, 1); 15495799e36SBin Meng tdata1 = RV32_TYPE(t); 15595799e36SBin Meng break; 15695799e36SBin Meng case MXL_RV64: 157d1d85412SFrédéric Pétrot case MXL_RV128: 15895799e36SBin Meng type = extract64(val, 60, 4); 15995799e36SBin Meng dmode = extract64(val, 59, 1); 16095799e36SBin Meng tdata1 = RV64_TYPE(t); 16195799e36SBin Meng break; 16295799e36SBin Meng default: 16395799e36SBin Meng g_assert_not_reached(); 16495799e36SBin Meng } 16595799e36SBin Meng 16695799e36SBin Meng if (type != t) { 16795799e36SBin Meng qemu_log_mask(LOG_GUEST_ERROR, 16895799e36SBin Meng "ignoring type write to tdata1 register\n"); 16995799e36SBin Meng } 170*a42bd001SFrank Chang 17195799e36SBin Meng if (dmode != 0) { 17295799e36SBin Meng qemu_log_mask(LOG_UNIMP, "debug mode is not supported\n"); 17395799e36SBin Meng } 17495799e36SBin Meng 17595799e36SBin Meng return tdata1; 17695799e36SBin Meng } 17795799e36SBin Meng 17895799e36SBin Meng static inline void warn_always_zero_bit(target_ulong val, target_ulong mask, 17995799e36SBin Meng const char *msg) 18095799e36SBin Meng { 18195799e36SBin Meng if (val & mask) { 18295799e36SBin Meng qemu_log_mask(LOG_UNIMP, "%s bit is always zero\n", msg); 18395799e36SBin Meng } 18495799e36SBin Meng } 18595799e36SBin Meng 18695799e36SBin Meng static uint32_t type2_breakpoint_size(CPURISCVState *env, target_ulong ctrl) 18795799e36SBin Meng { 18895799e36SBin Meng uint32_t size, sizelo, sizehi = 0; 18995799e36SBin Meng 19095799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 19195799e36SBin Meng sizehi = extract32(ctrl, 21, 2); 19295799e36SBin Meng } 19395799e36SBin Meng sizelo = extract32(ctrl, 16, 2); 19495799e36SBin Meng size = (sizehi << 2) | sizelo; 19595799e36SBin Meng 19695799e36SBin Meng return size; 19795799e36SBin Meng } 19895799e36SBin Meng 19995799e36SBin Meng static inline bool type2_breakpoint_enabled(target_ulong ctrl) 20095799e36SBin Meng { 20195799e36SBin Meng bool mode = !!(ctrl & (TYPE2_U | TYPE2_S | TYPE2_M)); 20295799e36SBin Meng bool rwx = !!(ctrl & (TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 20395799e36SBin Meng 20495799e36SBin Meng return mode && rwx; 20595799e36SBin Meng } 20695799e36SBin Meng 20795799e36SBin Meng static target_ulong type2_mcontrol_validate(CPURISCVState *env, 20895799e36SBin Meng target_ulong ctrl) 20995799e36SBin Meng { 21095799e36SBin Meng target_ulong val; 21195799e36SBin Meng uint32_t size; 21295799e36SBin Meng 21395799e36SBin Meng /* validate the generic part first */ 21495799e36SBin Meng val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH); 21595799e36SBin Meng 21695799e36SBin Meng /* validate unimplemented (always zero) bits */ 21795799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_MATCH, "match"); 21895799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain"); 21995799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_ACTION, "action"); 22095799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing"); 22195799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_SELECT, "select"); 22295799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_HIT, "hit"); 22395799e36SBin Meng 22495799e36SBin Meng /* validate size encoding */ 22595799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 22695799e36SBin Meng if (access_size[size] == -1) { 22795799e36SBin Meng qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using SIZE_ANY\n", 22895799e36SBin Meng size); 22995799e36SBin Meng } else { 23095799e36SBin Meng val |= (ctrl & TYPE2_SIZELO); 23195799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 23295799e36SBin Meng val |= (ctrl & TYPE2_SIZEHI); 23395799e36SBin Meng } 23495799e36SBin Meng } 23595799e36SBin Meng 23695799e36SBin Meng /* keep the mode and attribute bits */ 23795799e36SBin Meng val |= (ctrl & (TYPE2_U | TYPE2_S | TYPE2_M | 23895799e36SBin Meng TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 23995799e36SBin Meng 24095799e36SBin Meng return val; 24195799e36SBin Meng } 24295799e36SBin Meng 24395799e36SBin Meng static void type2_breakpoint_insert(CPURISCVState *env, target_ulong index) 24495799e36SBin Meng { 24595799e36SBin Meng target_ulong ctrl = env->type2_trig[index].mcontrol; 24695799e36SBin Meng target_ulong addr = env->type2_trig[index].maddress; 24795799e36SBin Meng bool enabled = type2_breakpoint_enabled(ctrl); 24895799e36SBin Meng CPUState *cs = env_cpu(env); 24995799e36SBin Meng int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 25095799e36SBin Meng uint32_t size; 25195799e36SBin Meng 25295799e36SBin Meng if (!enabled) { 25395799e36SBin Meng return; 25495799e36SBin Meng } 25595799e36SBin Meng 25695799e36SBin Meng if (ctrl & TYPE2_EXEC) { 25795799e36SBin Meng cpu_breakpoint_insert(cs, addr, flags, &env->type2_trig[index].bp); 25895799e36SBin Meng } 25995799e36SBin Meng 26095799e36SBin Meng if (ctrl & TYPE2_LOAD) { 26195799e36SBin Meng flags |= BP_MEM_READ; 26295799e36SBin Meng } 26395799e36SBin Meng if (ctrl & TYPE2_STORE) { 26495799e36SBin Meng flags |= BP_MEM_WRITE; 26595799e36SBin Meng } 26695799e36SBin Meng 26795799e36SBin Meng if (flags & BP_MEM_ACCESS) { 26895799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 26995799e36SBin Meng if (size != 0) { 27095799e36SBin Meng cpu_watchpoint_insert(cs, addr, size, flags, 27195799e36SBin Meng &env->type2_trig[index].wp); 27295799e36SBin Meng } else { 27395799e36SBin Meng cpu_watchpoint_insert(cs, addr, 8, flags, 27495799e36SBin Meng &env->type2_trig[index].wp); 27595799e36SBin Meng } 27695799e36SBin Meng } 27795799e36SBin Meng } 27895799e36SBin Meng 27995799e36SBin Meng static void type2_breakpoint_remove(CPURISCVState *env, target_ulong index) 28095799e36SBin Meng { 28195799e36SBin Meng CPUState *cs = env_cpu(env); 28295799e36SBin Meng 28395799e36SBin Meng if (env->type2_trig[index].bp) { 28495799e36SBin Meng cpu_breakpoint_remove_by_ref(cs, env->type2_trig[index].bp); 28595799e36SBin Meng env->type2_trig[index].bp = NULL; 28695799e36SBin Meng } 28795799e36SBin Meng 28895799e36SBin Meng if (env->type2_trig[index].wp) { 28995799e36SBin Meng cpu_watchpoint_remove_by_ref(cs, env->type2_trig[index].wp); 29095799e36SBin Meng env->type2_trig[index].wp = NULL; 29195799e36SBin Meng } 29295799e36SBin Meng } 29395799e36SBin Meng 29495799e36SBin Meng static target_ulong type2_reg_read(CPURISCVState *env, 295*a42bd001SFrank Chang target_ulong index, int tdata_index) 29695799e36SBin Meng { 29795799e36SBin Meng target_ulong tdata; 29895799e36SBin Meng 29995799e36SBin Meng switch (tdata_index) { 30095799e36SBin Meng case TDATA1: 30195799e36SBin Meng tdata = env->type2_trig[index].mcontrol; 30295799e36SBin Meng break; 30395799e36SBin Meng case TDATA2: 30495799e36SBin Meng tdata = env->type2_trig[index].maddress; 30595799e36SBin Meng break; 30695799e36SBin Meng default: 30795799e36SBin Meng g_assert_not_reached(); 30895799e36SBin Meng } 30995799e36SBin Meng 31095799e36SBin Meng return tdata; 31195799e36SBin Meng } 31295799e36SBin Meng 313*a42bd001SFrank Chang static void type2_reg_write(CPURISCVState *env, target_ulong index, 31495799e36SBin Meng int tdata_index, target_ulong val) 31595799e36SBin Meng { 31695799e36SBin Meng target_ulong new_val; 31795799e36SBin Meng 31895799e36SBin Meng switch (tdata_index) { 31995799e36SBin Meng case TDATA1: 32095799e36SBin Meng new_val = type2_mcontrol_validate(env, val); 32195799e36SBin Meng if (new_val != env->type2_trig[index].mcontrol) { 32295799e36SBin Meng env->type2_trig[index].mcontrol = new_val; 32395799e36SBin Meng type2_breakpoint_remove(env, index); 32495799e36SBin Meng type2_breakpoint_insert(env, index); 32595799e36SBin Meng } 32695799e36SBin Meng break; 32795799e36SBin Meng case TDATA2: 32895799e36SBin Meng if (val != env->type2_trig[index].maddress) { 32995799e36SBin Meng env->type2_trig[index].maddress = val; 33095799e36SBin Meng type2_breakpoint_remove(env, index); 33195799e36SBin Meng type2_breakpoint_insert(env, index); 33295799e36SBin Meng } 33395799e36SBin Meng break; 33495799e36SBin Meng default: 33595799e36SBin Meng g_assert_not_reached(); 33695799e36SBin Meng } 33795799e36SBin Meng 33895799e36SBin Meng return; 33995799e36SBin Meng } 34095799e36SBin Meng 34195799e36SBin Meng target_ulong tdata_csr_read(CPURISCVState *env, int tdata_index) 34295799e36SBin Meng { 343*a42bd001SFrank Chang int trigger_type = get_trigger_type(env, env->trigger_cur); 34495799e36SBin Meng 345*a42bd001SFrank Chang switch (trigger_type) { 346*a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 347*a42bd001SFrank Chang return type2_reg_read(env, env->trigger_cur, tdata_index); 348*a42bd001SFrank Chang break; 349*a42bd001SFrank Chang case TRIGGER_TYPE_INST_CNT: 350*a42bd001SFrank Chang case TRIGGER_TYPE_INT: 351*a42bd001SFrank Chang case TRIGGER_TYPE_EXCP: 352*a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 353*a42bd001SFrank Chang case TRIGGER_TYPE_EXT_SRC: 354*a42bd001SFrank Chang qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n", 355*a42bd001SFrank Chang trigger_type); 356*a42bd001SFrank Chang break; 357*a42bd001SFrank Chang case TRIGGER_TYPE_NO_EXIST: 358*a42bd001SFrank Chang case TRIGGER_TYPE_UNAVAIL: 359*a42bd001SFrank Chang qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n", 360*a42bd001SFrank Chang trigger_type); 361*a42bd001SFrank Chang break; 362*a42bd001SFrank Chang default: 363*a42bd001SFrank Chang g_assert_not_reached(); 364*a42bd001SFrank Chang } 365*a42bd001SFrank Chang 366*a42bd001SFrank Chang return 0; 36795799e36SBin Meng } 36895799e36SBin Meng 36995799e36SBin Meng void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val) 37095799e36SBin Meng { 371*a42bd001SFrank Chang int trigger_type; 37295799e36SBin Meng 373*a42bd001SFrank Chang if (tdata_index == TDATA1) { 374*a42bd001SFrank Chang trigger_type = extract_trigger_type(env, val); 375*a42bd001SFrank Chang } else { 376*a42bd001SFrank Chang trigger_type = get_trigger_type(env, env->trigger_cur); 377*a42bd001SFrank Chang } 378*a42bd001SFrank Chang 379*a42bd001SFrank Chang switch (trigger_type) { 380*a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 381*a42bd001SFrank Chang type2_reg_write(env, env->trigger_cur, tdata_index, val); 382*a42bd001SFrank Chang break; 383*a42bd001SFrank Chang case TRIGGER_TYPE_INST_CNT: 384*a42bd001SFrank Chang case TRIGGER_TYPE_INT: 385*a42bd001SFrank Chang case TRIGGER_TYPE_EXCP: 386*a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 387*a42bd001SFrank Chang case TRIGGER_TYPE_EXT_SRC: 388*a42bd001SFrank Chang qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n", 389*a42bd001SFrank Chang trigger_type); 390*a42bd001SFrank Chang break; 391*a42bd001SFrank Chang case TRIGGER_TYPE_NO_EXIST: 392*a42bd001SFrank Chang case TRIGGER_TYPE_UNAVAIL: 393*a42bd001SFrank Chang qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n", 394*a42bd001SFrank Chang trigger_type); 395*a42bd001SFrank Chang break; 396*a42bd001SFrank Chang default: 397*a42bd001SFrank Chang g_assert_not_reached(); 398*a42bd001SFrank Chang } 39995799e36SBin Meng } 400b5f6379dSBin Meng 401b5f6379dSBin Meng void riscv_cpu_debug_excp_handler(CPUState *cs) 402b5f6379dSBin Meng { 403b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 404b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 405b5f6379dSBin Meng 406b5f6379dSBin Meng if (cs->watchpoint_hit) { 407b5f6379dSBin Meng if (cs->watchpoint_hit->flags & BP_CPU) { 408b5f6379dSBin Meng cs->watchpoint_hit = NULL; 409b5f6379dSBin Meng riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0); 410b5f6379dSBin Meng } 411b5f6379dSBin Meng } else { 412b5f6379dSBin Meng if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) { 413b5f6379dSBin Meng riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0); 414b5f6379dSBin Meng } 415b5f6379dSBin Meng } 416b5f6379dSBin Meng } 417b5f6379dSBin Meng 418b5f6379dSBin Meng bool riscv_cpu_debug_check_breakpoint(CPUState *cs) 419b5f6379dSBin Meng { 420b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 421b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 422b5f6379dSBin Meng CPUBreakpoint *bp; 423b5f6379dSBin Meng target_ulong ctrl; 424b5f6379dSBin Meng target_ulong pc; 425*a42bd001SFrank Chang int trigger_type; 426b5f6379dSBin Meng int i; 427b5f6379dSBin Meng 428b5f6379dSBin Meng QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { 429*a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 430*a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 431*a42bd001SFrank Chang 432*a42bd001SFrank Chang switch (trigger_type) { 433*a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 434b5f6379dSBin Meng ctrl = env->type2_trig[i].mcontrol; 435b5f6379dSBin Meng pc = env->type2_trig[i].maddress; 436b5f6379dSBin Meng 437b5f6379dSBin Meng if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) { 438b5f6379dSBin Meng /* check U/S/M bit against current privilege level */ 439b5f6379dSBin Meng if ((ctrl >> 3) & BIT(env->priv)) { 440b5f6379dSBin Meng return true; 441b5f6379dSBin Meng } 442b5f6379dSBin Meng } 443*a42bd001SFrank Chang break; 444*a42bd001SFrank Chang default: 445*a42bd001SFrank Chang /* other trigger types are not supported or irrelevant */ 446*a42bd001SFrank Chang break; 447*a42bd001SFrank Chang } 448b5f6379dSBin Meng } 449b5f6379dSBin Meng } 450b5f6379dSBin Meng 451b5f6379dSBin Meng return false; 452b5f6379dSBin Meng } 453b5f6379dSBin Meng 454b5f6379dSBin Meng bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp) 455b5f6379dSBin Meng { 456b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 457b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 458b5f6379dSBin Meng target_ulong ctrl; 459b5f6379dSBin Meng target_ulong addr; 460*a42bd001SFrank Chang int trigger_type; 461b5f6379dSBin Meng int flags; 462b5f6379dSBin Meng int i; 463b5f6379dSBin Meng 464*a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 465*a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 466*a42bd001SFrank Chang 467*a42bd001SFrank Chang switch (trigger_type) { 468*a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 469b5f6379dSBin Meng ctrl = env->type2_trig[i].mcontrol; 470b5f6379dSBin Meng addr = env->type2_trig[i].maddress; 471b5f6379dSBin Meng flags = 0; 472b5f6379dSBin Meng 473b5f6379dSBin Meng if (ctrl & TYPE2_LOAD) { 474b5f6379dSBin Meng flags |= BP_MEM_READ; 475b5f6379dSBin Meng } 476b5f6379dSBin Meng if (ctrl & TYPE2_STORE) { 477b5f6379dSBin Meng flags |= BP_MEM_WRITE; 478b5f6379dSBin Meng } 479b5f6379dSBin Meng 480b5f6379dSBin Meng if ((wp->flags & flags) && (wp->vaddr == addr)) { 481b5f6379dSBin Meng /* check U/S/M bit against current privilege level */ 482b5f6379dSBin Meng if ((ctrl >> 3) & BIT(env->priv)) { 483b5f6379dSBin Meng return true; 484b5f6379dSBin Meng } 485b5f6379dSBin Meng } 486*a42bd001SFrank Chang break; 487*a42bd001SFrank Chang default: 488*a42bd001SFrank Chang /* other trigger types are not supported */ 489*a42bd001SFrank Chang break; 490*a42bd001SFrank Chang } 491b5f6379dSBin Meng } 492b5f6379dSBin Meng 493b5f6379dSBin Meng return false; 494b5f6379dSBin Meng } 495b6092544SBin Meng 496b6092544SBin Meng void riscv_trigger_init(CPURISCVState *env) 497b6092544SBin Meng { 498*a42bd001SFrank Chang target_ulong tdata1 = trigger_type(env, TRIGGER_TYPE_AD_MATCH); 499b6092544SBin Meng int i; 500b6092544SBin Meng 501*a42bd001SFrank Chang /* init to type 2 triggers */ 502*a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 503b6092544SBin Meng /* 504b6092544SBin Meng * type = TRIGGER_TYPE_AD_MATCH 505b6092544SBin Meng * dmode = 0 (both debug and M-mode can write tdata) 506b6092544SBin Meng * maskmax = 0 (unimplemented, always 0) 507b6092544SBin Meng * sizehi = 0 (match against any size, RV64 only) 508b6092544SBin Meng * hit = 0 (unimplemented, always 0) 509b6092544SBin Meng * select = 0 (always 0, perform match on address) 510b6092544SBin Meng * timing = 0 (always 0, trigger before instruction) 511b6092544SBin Meng * sizelo = 0 (match against any size) 512b6092544SBin Meng * action = 0 (always 0, raise a breakpoint exception) 513b6092544SBin Meng * chain = 0 (unimplemented, always 0) 514b6092544SBin Meng * match = 0 (always 0, when any compare value equals tdata2) 515b6092544SBin Meng */ 516*a42bd001SFrank Chang env->type2_trig[i].mcontrol = tdata1; 517b6092544SBin Meng env->type2_trig[i].maddress = 0; 518b6092544SBin Meng env->type2_trig[i].bp = NULL; 519b6092544SBin Meng env->type2_trig[i].wp = NULL; 520b6092544SBin Meng } 521b6092544SBin Meng } 522