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 220d1c11141SFrank Chang static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index) 221d1c11141SFrank Chang { 222d1c11141SFrank Chang trigger_action_t action = get_trigger_action(env, trigger_index); 223d1c11141SFrank Chang 224d1c11141SFrank Chang switch (action) { 225d1c11141SFrank Chang case DBG_ACTION_NONE: 226d1c11141SFrank Chang break; 227d1c11141SFrank Chang case DBG_ACTION_BP: 228d1c11141SFrank Chang riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0); 229d1c11141SFrank Chang break; 230d1c11141SFrank Chang case DBG_ACTION_DBG_MODE: 231d1c11141SFrank Chang case DBG_ACTION_TRACE0: 232d1c11141SFrank Chang case DBG_ACTION_TRACE1: 233d1c11141SFrank Chang case DBG_ACTION_TRACE2: 234d1c11141SFrank Chang case DBG_ACTION_TRACE3: 235d1c11141SFrank Chang case DBG_ACTION_EXT_DBG0: 236d1c11141SFrank Chang case DBG_ACTION_EXT_DBG1: 237d1c11141SFrank Chang qemu_log_mask(LOG_UNIMP, "action: %d is not supported\n", action); 238d1c11141SFrank Chang break; 239d1c11141SFrank Chang default: 240d1c11141SFrank Chang g_assert_not_reached(); 241d1c11141SFrank Chang } 242d1c11141SFrank Chang } 243d1c11141SFrank Chang 2449495c488SFrank Chang /* type 2 trigger */ 2459495c488SFrank Chang 24695799e36SBin Meng static uint32_t type2_breakpoint_size(CPURISCVState *env, target_ulong ctrl) 24795799e36SBin Meng { 24866997c42SMarkus Armbruster uint32_t sizelo, sizehi = 0; 24995799e36SBin Meng 25095799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 25195799e36SBin Meng sizehi = extract32(ctrl, 21, 2); 25295799e36SBin Meng } 25395799e36SBin Meng sizelo = extract32(ctrl, 16, 2); 25466997c42SMarkus Armbruster return (sizehi << 2) | sizelo; 25595799e36SBin Meng } 25695799e36SBin Meng 25795799e36SBin Meng static inline bool type2_breakpoint_enabled(target_ulong ctrl) 25895799e36SBin Meng { 25995799e36SBin Meng bool mode = !!(ctrl & (TYPE2_U | TYPE2_S | TYPE2_M)); 26095799e36SBin Meng bool rwx = !!(ctrl & (TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 26195799e36SBin Meng 26295799e36SBin Meng return mode && rwx; 26395799e36SBin Meng } 26495799e36SBin Meng 26595799e36SBin Meng static target_ulong type2_mcontrol_validate(CPURISCVState *env, 26695799e36SBin Meng target_ulong ctrl) 26795799e36SBin Meng { 26895799e36SBin Meng target_ulong val; 26995799e36SBin Meng uint32_t size; 27095799e36SBin Meng 27195799e36SBin Meng /* validate the generic part first */ 27295799e36SBin Meng val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH); 27395799e36SBin Meng 27495799e36SBin Meng /* validate unimplemented (always zero) bits */ 27595799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_MATCH, "match"); 27695799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain"); 27795799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_ACTION, "action"); 27895799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing"); 27995799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_SELECT, "select"); 28095799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_HIT, "hit"); 28195799e36SBin Meng 28295799e36SBin Meng /* validate size encoding */ 28395799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 28495799e36SBin Meng if (access_size[size] == -1) { 285246f8796SWeiwei Li qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using " 286246f8796SWeiwei Li "SIZE_ANY\n", size); 28795799e36SBin Meng } else { 28895799e36SBin Meng val |= (ctrl & TYPE2_SIZELO); 28995799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 29095799e36SBin Meng val |= (ctrl & TYPE2_SIZEHI); 29195799e36SBin Meng } 29295799e36SBin Meng } 29395799e36SBin Meng 29495799e36SBin Meng /* keep the mode and attribute bits */ 29595799e36SBin Meng val |= (ctrl & (TYPE2_U | TYPE2_S | TYPE2_M | 29695799e36SBin Meng TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 29795799e36SBin Meng 29895799e36SBin Meng return val; 29995799e36SBin Meng } 30095799e36SBin Meng 30195799e36SBin Meng static void type2_breakpoint_insert(CPURISCVState *env, target_ulong index) 30295799e36SBin Meng { 3039495c488SFrank Chang target_ulong ctrl = env->tdata1[index]; 3049495c488SFrank Chang target_ulong addr = env->tdata2[index]; 30595799e36SBin Meng bool enabled = type2_breakpoint_enabled(ctrl); 30695799e36SBin Meng CPUState *cs = env_cpu(env); 30795799e36SBin Meng int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 30895799e36SBin Meng uint32_t size; 30995799e36SBin Meng 31095799e36SBin Meng if (!enabled) { 31195799e36SBin Meng return; 31295799e36SBin Meng } 31395799e36SBin Meng 31495799e36SBin Meng if (ctrl & TYPE2_EXEC) { 3159495c488SFrank Chang cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]); 31695799e36SBin Meng } 31795799e36SBin Meng 31895799e36SBin Meng if (ctrl & TYPE2_LOAD) { 31995799e36SBin Meng flags |= BP_MEM_READ; 32095799e36SBin Meng } 32195799e36SBin Meng if (ctrl & TYPE2_STORE) { 32295799e36SBin Meng flags |= BP_MEM_WRITE; 32395799e36SBin Meng } 32495799e36SBin Meng 32595799e36SBin Meng if (flags & BP_MEM_ACCESS) { 32695799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 32795799e36SBin Meng if (size != 0) { 32895799e36SBin Meng cpu_watchpoint_insert(cs, addr, size, flags, 3299495c488SFrank Chang &env->cpu_watchpoint[index]); 33095799e36SBin Meng } else { 33195799e36SBin Meng cpu_watchpoint_insert(cs, addr, 8, flags, 3329495c488SFrank Chang &env->cpu_watchpoint[index]); 33395799e36SBin Meng } 33495799e36SBin Meng } 33595799e36SBin Meng } 33695799e36SBin Meng 33795799e36SBin Meng static void type2_breakpoint_remove(CPURISCVState *env, target_ulong index) 33895799e36SBin Meng { 33995799e36SBin Meng CPUState *cs = env_cpu(env); 34095799e36SBin Meng 3419495c488SFrank Chang if (env->cpu_breakpoint[index]) { 3429495c488SFrank Chang cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]); 3439495c488SFrank Chang env->cpu_breakpoint[index] = NULL; 34495799e36SBin Meng } 34595799e36SBin Meng 3469495c488SFrank Chang if (env->cpu_watchpoint[index]) { 3479495c488SFrank Chang cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]); 3489495c488SFrank Chang env->cpu_watchpoint[index] = NULL; 34995799e36SBin Meng } 35095799e36SBin Meng } 35195799e36SBin Meng 352a42bd001SFrank Chang static void type2_reg_write(CPURISCVState *env, target_ulong index, 35395799e36SBin Meng int tdata_index, target_ulong val) 35495799e36SBin Meng { 35595799e36SBin Meng target_ulong new_val; 35695799e36SBin Meng 35795799e36SBin Meng switch (tdata_index) { 35895799e36SBin Meng case TDATA1: 35995799e36SBin Meng new_val = type2_mcontrol_validate(env, val); 3609495c488SFrank Chang if (new_val != env->tdata1[index]) { 3619495c488SFrank Chang env->tdata1[index] = new_val; 36295799e36SBin Meng type2_breakpoint_remove(env, index); 36395799e36SBin Meng type2_breakpoint_insert(env, index); 36495799e36SBin Meng } 36595799e36SBin Meng break; 36695799e36SBin Meng case TDATA2: 3679495c488SFrank Chang if (val != env->tdata2[index]) { 3689495c488SFrank Chang env->tdata2[index] = val; 36995799e36SBin Meng type2_breakpoint_remove(env, index); 37095799e36SBin Meng type2_breakpoint_insert(env, index); 37195799e36SBin Meng } 37295799e36SBin Meng break; 3739495c488SFrank Chang case TDATA3: 3749495c488SFrank Chang qemu_log_mask(LOG_UNIMP, 3759495c488SFrank Chang "tdata3 is not supported for type 2 trigger\n"); 3769495c488SFrank Chang break; 37795799e36SBin Meng default: 37895799e36SBin Meng g_assert_not_reached(); 37995799e36SBin Meng } 38095799e36SBin Meng 38195799e36SBin Meng return; 38295799e36SBin Meng } 38395799e36SBin Meng 384c472c142SFrank Chang /* type 6 trigger */ 385c472c142SFrank Chang 386c472c142SFrank Chang static inline bool type6_breakpoint_enabled(target_ulong ctrl) 387c472c142SFrank Chang { 388c472c142SFrank Chang bool mode = !!(ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M)); 389c472c142SFrank Chang bool rwx = !!(ctrl & (TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC)); 390c472c142SFrank Chang 391c472c142SFrank Chang return mode && rwx; 392c472c142SFrank Chang } 393c472c142SFrank Chang 394c472c142SFrank Chang static target_ulong type6_mcontrol6_validate(CPURISCVState *env, 395c472c142SFrank Chang target_ulong ctrl) 396c472c142SFrank Chang { 397c472c142SFrank Chang target_ulong val; 398c472c142SFrank Chang uint32_t size; 399c472c142SFrank Chang 400c472c142SFrank Chang /* validate the generic part first */ 401c472c142SFrank Chang val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH6); 402c472c142SFrank Chang 403c472c142SFrank Chang /* validate unimplemented (always zero) bits */ 404c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_MATCH, "match"); 405c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_CHAIN, "chain"); 406c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_ACTION, "action"); 407c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_TIMING, "timing"); 408c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_SELECT, "select"); 409c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_HIT, "hit"); 410c472c142SFrank Chang 411c472c142SFrank Chang /* validate size encoding */ 412c472c142SFrank Chang size = extract32(ctrl, 16, 4); 413c472c142SFrank Chang if (access_size[size] == -1) { 414246f8796SWeiwei Li qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using " 415246f8796SWeiwei Li "SIZE_ANY\n", size); 416c472c142SFrank Chang } else { 417c472c142SFrank Chang val |= (ctrl & TYPE6_SIZE); 418c472c142SFrank Chang } 419c472c142SFrank Chang 420c472c142SFrank Chang /* keep the mode and attribute bits */ 421c472c142SFrank Chang val |= (ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M | 422c472c142SFrank Chang TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC)); 423c472c142SFrank Chang 424c472c142SFrank Chang return val; 425c472c142SFrank Chang } 426c472c142SFrank Chang 427c472c142SFrank Chang static void type6_breakpoint_insert(CPURISCVState *env, target_ulong index) 428c472c142SFrank Chang { 429c472c142SFrank Chang target_ulong ctrl = env->tdata1[index]; 430c472c142SFrank Chang target_ulong addr = env->tdata2[index]; 431c472c142SFrank Chang bool enabled = type6_breakpoint_enabled(ctrl); 432c472c142SFrank Chang CPUState *cs = env_cpu(env); 433c472c142SFrank Chang int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 434c472c142SFrank Chang uint32_t size; 435c472c142SFrank Chang 436c472c142SFrank Chang if (!enabled) { 437c472c142SFrank Chang return; 438c472c142SFrank Chang } 439c472c142SFrank Chang 440c472c142SFrank Chang if (ctrl & TYPE6_EXEC) { 441c472c142SFrank Chang cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]); 442c472c142SFrank Chang } 443c472c142SFrank Chang 444c472c142SFrank Chang if (ctrl & TYPE6_LOAD) { 445c472c142SFrank Chang flags |= BP_MEM_READ; 446c472c142SFrank Chang } 447c472c142SFrank Chang 448c472c142SFrank Chang if (ctrl & TYPE6_STORE) { 449c472c142SFrank Chang flags |= BP_MEM_WRITE; 450c472c142SFrank Chang } 451c472c142SFrank Chang 452c472c142SFrank Chang if (flags & BP_MEM_ACCESS) { 453c472c142SFrank Chang size = extract32(ctrl, 16, 4); 454c472c142SFrank Chang if (size != 0) { 455c472c142SFrank Chang cpu_watchpoint_insert(cs, addr, size, flags, 456c472c142SFrank Chang &env->cpu_watchpoint[index]); 457c472c142SFrank Chang } else { 458c472c142SFrank Chang cpu_watchpoint_insert(cs, addr, 8, flags, 459c472c142SFrank Chang &env->cpu_watchpoint[index]); 460c472c142SFrank Chang } 461c472c142SFrank Chang } 462c472c142SFrank Chang } 463c472c142SFrank Chang 464c472c142SFrank Chang static void type6_breakpoint_remove(CPURISCVState *env, target_ulong index) 465c472c142SFrank Chang { 466c472c142SFrank Chang type2_breakpoint_remove(env, index); 467c472c142SFrank Chang } 468c472c142SFrank Chang 469c472c142SFrank Chang static void type6_reg_write(CPURISCVState *env, target_ulong index, 470c472c142SFrank Chang int tdata_index, target_ulong val) 471c472c142SFrank Chang { 472c472c142SFrank Chang target_ulong new_val; 473c472c142SFrank Chang 474c472c142SFrank Chang switch (tdata_index) { 475c472c142SFrank Chang case TDATA1: 476c472c142SFrank Chang new_val = type6_mcontrol6_validate(env, val); 477c472c142SFrank Chang if (new_val != env->tdata1[index]) { 478c472c142SFrank Chang env->tdata1[index] = new_val; 479c472c142SFrank Chang type6_breakpoint_remove(env, index); 480c472c142SFrank Chang type6_breakpoint_insert(env, index); 481c472c142SFrank Chang } 482c472c142SFrank Chang break; 483c472c142SFrank Chang case TDATA2: 484c472c142SFrank Chang if (val != env->tdata2[index]) { 485c472c142SFrank Chang env->tdata2[index] = val; 486c472c142SFrank Chang type6_breakpoint_remove(env, index); 487c472c142SFrank Chang type6_breakpoint_insert(env, index); 488c472c142SFrank Chang } 489c472c142SFrank Chang break; 490c472c142SFrank Chang case TDATA3: 491c472c142SFrank Chang qemu_log_mask(LOG_UNIMP, 492c472c142SFrank Chang "tdata3 is not supported for type 6 trigger\n"); 493c472c142SFrank Chang break; 494c472c142SFrank Chang default: 495c472c142SFrank Chang g_assert_not_reached(); 496c472c142SFrank Chang } 497c472c142SFrank Chang 498c472c142SFrank Chang return; 499c472c142SFrank Chang } 500c472c142SFrank Chang 5012c9d7471SLIU Zhiwei /* icount trigger type */ 5022c9d7471SLIU Zhiwei static inline int 5032c9d7471SLIU Zhiwei itrigger_get_count(CPURISCVState *env, int index) 5042c9d7471SLIU Zhiwei { 5052c9d7471SLIU Zhiwei return get_field(env->tdata1[index], ITRIGGER_COUNT); 5062c9d7471SLIU Zhiwei } 5072c9d7471SLIU Zhiwei 5082c9d7471SLIU Zhiwei static inline void 5092c9d7471SLIU Zhiwei itrigger_set_count(CPURISCVState *env, int index, int value) 5102c9d7471SLIU Zhiwei { 5112c9d7471SLIU Zhiwei env->tdata1[index] = set_field(env->tdata1[index], 5122c9d7471SLIU Zhiwei ITRIGGER_COUNT, value); 5132c9d7471SLIU Zhiwei } 5142c9d7471SLIU Zhiwei 5152c9d7471SLIU Zhiwei static bool check_itrigger_priv(CPURISCVState *env, int index) 5162c9d7471SLIU Zhiwei { 5172c9d7471SLIU Zhiwei target_ulong tdata1 = env->tdata1[index]; 51838256529SWeiwei Li if (env->virt_enabled) { 5192c9d7471SLIU Zhiwei /* check VU/VS bit against current privilege level */ 5202c9d7471SLIU Zhiwei return (get_field(tdata1, ITRIGGER_VS) == env->priv) || 5212c9d7471SLIU Zhiwei (get_field(tdata1, ITRIGGER_VU) == env->priv); 5222c9d7471SLIU Zhiwei } else { 5232c9d7471SLIU Zhiwei /* check U/S/M bit against current privilege level */ 5242c9d7471SLIU Zhiwei return (get_field(tdata1, ITRIGGER_M) == env->priv) || 5252c9d7471SLIU Zhiwei (get_field(tdata1, ITRIGGER_S) == env->priv) || 5262c9d7471SLIU Zhiwei (get_field(tdata1, ITRIGGER_U) == env->priv); 5272c9d7471SLIU Zhiwei } 5282c9d7471SLIU Zhiwei } 5292c9d7471SLIU Zhiwei 5302c9d7471SLIU Zhiwei bool riscv_itrigger_enabled(CPURISCVState *env) 5312c9d7471SLIU Zhiwei { 5322c9d7471SLIU Zhiwei int count; 5332c9d7471SLIU Zhiwei for (int i = 0; i < RV_MAX_TRIGGERS; i++) { 5342c9d7471SLIU Zhiwei if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) { 5352c9d7471SLIU Zhiwei continue; 5362c9d7471SLIU Zhiwei } 5372c9d7471SLIU Zhiwei if (check_itrigger_priv(env, i)) { 5382c9d7471SLIU Zhiwei continue; 5392c9d7471SLIU Zhiwei } 5402c9d7471SLIU Zhiwei count = itrigger_get_count(env, i); 5412c9d7471SLIU Zhiwei if (!count) { 5422c9d7471SLIU Zhiwei continue; 5432c9d7471SLIU Zhiwei } 5442c9d7471SLIU Zhiwei return true; 5452c9d7471SLIU Zhiwei } 5462c9d7471SLIU Zhiwei 5472c9d7471SLIU Zhiwei return false; 5482c9d7471SLIU Zhiwei } 5492c9d7471SLIU Zhiwei 5502c9d7471SLIU Zhiwei void helper_itrigger_match(CPURISCVState *env) 5512c9d7471SLIU Zhiwei { 5522c9d7471SLIU Zhiwei int count; 5532c9d7471SLIU Zhiwei for (int i = 0; i < RV_MAX_TRIGGERS; i++) { 5542c9d7471SLIU Zhiwei if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) { 5552c9d7471SLIU Zhiwei continue; 5562c9d7471SLIU Zhiwei } 5572c9d7471SLIU Zhiwei if (check_itrigger_priv(env, i)) { 5582c9d7471SLIU Zhiwei continue; 5592c9d7471SLIU Zhiwei } 5602c9d7471SLIU Zhiwei count = itrigger_get_count(env, i); 5612c9d7471SLIU Zhiwei if (!count) { 5622c9d7471SLIU Zhiwei continue; 5632c9d7471SLIU Zhiwei } 5642c9d7471SLIU Zhiwei itrigger_set_count(env, i, count--); 5652c9d7471SLIU Zhiwei if (!count) { 566577f0286SLIU Zhiwei env->itrigger_enabled = riscv_itrigger_enabled(env); 5672c9d7471SLIU Zhiwei do_trigger_action(env, i); 5682c9d7471SLIU Zhiwei } 5692c9d7471SLIU Zhiwei } 5702c9d7471SLIU Zhiwei } 5712c9d7471SLIU Zhiwei 5725a4ae64cSLIU Zhiwei static void riscv_itrigger_update_count(CPURISCVState *env) 5735a4ae64cSLIU Zhiwei { 5745a4ae64cSLIU Zhiwei int count, executed; 5755a4ae64cSLIU Zhiwei /* 5765a4ae64cSLIU Zhiwei * Record last icount, so that we can evaluate the executed instructions 57742fe7499SMichael Tokarev * since last privilege mode change or timer expire. 5785a4ae64cSLIU Zhiwei */ 5795a4ae64cSLIU Zhiwei int64_t last_icount = env->last_icount, current_icount; 5805a4ae64cSLIU Zhiwei current_icount = env->last_icount = icount_get_raw(); 5815a4ae64cSLIU Zhiwei 5825a4ae64cSLIU Zhiwei for (int i = 0; i < RV_MAX_TRIGGERS; i++) { 5835a4ae64cSLIU Zhiwei if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) { 5845a4ae64cSLIU Zhiwei continue; 5855a4ae64cSLIU Zhiwei } 5865a4ae64cSLIU Zhiwei count = itrigger_get_count(env, i); 5875a4ae64cSLIU Zhiwei if (!count) { 5885a4ae64cSLIU Zhiwei continue; 5895a4ae64cSLIU Zhiwei } 5905a4ae64cSLIU Zhiwei /* 59142fe7499SMichael Tokarev * Only when privilege is changed or itrigger timer expires, 5925a4ae64cSLIU Zhiwei * the count field in itrigger tdata1 register is updated. 5935a4ae64cSLIU Zhiwei * And the count field in itrigger only contains remaining value. 5945a4ae64cSLIU Zhiwei */ 5955a4ae64cSLIU Zhiwei if (check_itrigger_priv(env, i)) { 5965a4ae64cSLIU Zhiwei /* 59742fe7499SMichael Tokarev * If itrigger enabled in this privilege mode, the number of 59842fe7499SMichael Tokarev * executed instructions since last privilege change 5995a4ae64cSLIU Zhiwei * should be reduced from current itrigger count. 6005a4ae64cSLIU Zhiwei */ 6015a4ae64cSLIU Zhiwei executed = current_icount - last_icount; 6025a4ae64cSLIU Zhiwei itrigger_set_count(env, i, count - executed); 6035a4ae64cSLIU Zhiwei if (count == executed) { 6045a4ae64cSLIU Zhiwei do_trigger_action(env, i); 6055a4ae64cSLIU Zhiwei } 6065a4ae64cSLIU Zhiwei } else { 6075a4ae64cSLIU Zhiwei /* 60842fe7499SMichael Tokarev * If itrigger is not enabled in this privilege mode, 6095a4ae64cSLIU Zhiwei * the number of executed instructions will be discard and 6105a4ae64cSLIU Zhiwei * the count field in itrigger will not change. 6115a4ae64cSLIU Zhiwei */ 6125a4ae64cSLIU Zhiwei timer_mod(env->itrigger_timer[i], 6135a4ae64cSLIU Zhiwei current_icount + count); 6145a4ae64cSLIU Zhiwei } 6155a4ae64cSLIU Zhiwei } 6165a4ae64cSLIU Zhiwei } 6175a4ae64cSLIU Zhiwei 6185a4ae64cSLIU Zhiwei static void riscv_itrigger_timer_cb(void *opaque) 6195a4ae64cSLIU Zhiwei { 6205a4ae64cSLIU Zhiwei riscv_itrigger_update_count((CPURISCVState *)opaque); 6215a4ae64cSLIU Zhiwei } 6225a4ae64cSLIU Zhiwei 6235a4ae64cSLIU Zhiwei void riscv_itrigger_update_priv(CPURISCVState *env) 6245a4ae64cSLIU Zhiwei { 6255a4ae64cSLIU Zhiwei riscv_itrigger_update_count(env); 6265a4ae64cSLIU Zhiwei } 6275a4ae64cSLIU Zhiwei 62891809598SLIU Zhiwei static target_ulong itrigger_validate(CPURISCVState *env, 62991809598SLIU Zhiwei target_ulong ctrl) 63095799e36SBin Meng { 63191809598SLIU Zhiwei target_ulong val; 63291809598SLIU Zhiwei 63391809598SLIU Zhiwei /* validate the generic part first */ 63491809598SLIU Zhiwei val = tdata1_validate(env, ctrl, TRIGGER_TYPE_INST_CNT); 63591809598SLIU Zhiwei 63691809598SLIU Zhiwei /* validate unimplemented (always zero) bits */ 63791809598SLIU Zhiwei warn_always_zero_bit(ctrl, ITRIGGER_ACTION, "action"); 63891809598SLIU Zhiwei warn_always_zero_bit(ctrl, ITRIGGER_HIT, "hit"); 63991809598SLIU Zhiwei warn_always_zero_bit(ctrl, ITRIGGER_PENDING, "pending"); 64091809598SLIU Zhiwei 64191809598SLIU Zhiwei /* keep the mode and attribute bits */ 64291809598SLIU Zhiwei val |= ctrl & (ITRIGGER_VU | ITRIGGER_VS | ITRIGGER_U | ITRIGGER_S | 64391809598SLIU Zhiwei ITRIGGER_M | ITRIGGER_COUNT); 64491809598SLIU Zhiwei 64591809598SLIU Zhiwei return val; 64691809598SLIU Zhiwei } 64791809598SLIU Zhiwei 64891809598SLIU Zhiwei static void itrigger_reg_write(CPURISCVState *env, target_ulong index, 64991809598SLIU Zhiwei int tdata_index, target_ulong val) 65091809598SLIU Zhiwei { 65191809598SLIU Zhiwei target_ulong new_val; 65291809598SLIU Zhiwei 6539495c488SFrank Chang switch (tdata_index) { 6549495c488SFrank Chang case TDATA1: 65591809598SLIU Zhiwei /* set timer for icount */ 65691809598SLIU Zhiwei new_val = itrigger_validate(env, val); 65791809598SLIU Zhiwei if (new_val != env->tdata1[index]) { 65891809598SLIU Zhiwei env->tdata1[index] = new_val; 65991809598SLIU Zhiwei if (icount_enabled()) { 66091809598SLIU Zhiwei env->last_icount = icount_get_raw(); 66191809598SLIU Zhiwei /* set the count to timer */ 66291809598SLIU Zhiwei timer_mod(env->itrigger_timer[index], 66391809598SLIU Zhiwei env->last_icount + itrigger_get_count(env, index)); 664577f0286SLIU Zhiwei } else { 665577f0286SLIU Zhiwei env->itrigger_enabled = riscv_itrigger_enabled(env); 66691809598SLIU Zhiwei } 66791809598SLIU Zhiwei } 66891809598SLIU Zhiwei break; 66991809598SLIU Zhiwei case TDATA2: 67091809598SLIU Zhiwei qemu_log_mask(LOG_UNIMP, 67191809598SLIU Zhiwei "tdata2 is not supported for icount trigger\n"); 67291809598SLIU Zhiwei break; 67391809598SLIU Zhiwei case TDATA3: 67491809598SLIU Zhiwei qemu_log_mask(LOG_UNIMP, 67591809598SLIU Zhiwei "tdata3 is not supported for icount trigger\n"); 67691809598SLIU Zhiwei break; 67791809598SLIU Zhiwei default: 67891809598SLIU Zhiwei g_assert_not_reached(); 67991809598SLIU Zhiwei } 68091809598SLIU Zhiwei 68191809598SLIU Zhiwei return; 68291809598SLIU Zhiwei } 68391809598SLIU Zhiwei 68491809598SLIU Zhiwei static int itrigger_get_adjust_count(CPURISCVState *env) 68591809598SLIU Zhiwei { 68691809598SLIU Zhiwei int count = itrigger_get_count(env, env->trigger_cur), executed; 68791809598SLIU Zhiwei if ((count != 0) && check_itrigger_priv(env, env->trigger_cur)) { 68891809598SLIU Zhiwei executed = icount_get_raw() - env->last_icount; 68991809598SLIU Zhiwei count += executed; 69091809598SLIU Zhiwei } 69191809598SLIU Zhiwei return count; 69291809598SLIU Zhiwei } 69391809598SLIU Zhiwei 69491809598SLIU Zhiwei target_ulong tdata_csr_read(CPURISCVState *env, int tdata_index) 69591809598SLIU Zhiwei { 69691809598SLIU Zhiwei int trigger_type; 69791809598SLIU Zhiwei switch (tdata_index) { 69891809598SLIU Zhiwei case TDATA1: 699246f8796SWeiwei Li trigger_type = extract_trigger_type(env, 700246f8796SWeiwei Li env->tdata1[env->trigger_cur]); 70191809598SLIU Zhiwei if ((trigger_type == TRIGGER_TYPE_INST_CNT) && icount_enabled()) { 70291809598SLIU Zhiwei return deposit64(env->tdata1[env->trigger_cur], 10, 14, 70391809598SLIU Zhiwei itrigger_get_adjust_count(env)); 70491809598SLIU Zhiwei } 7059495c488SFrank Chang return env->tdata1[env->trigger_cur]; 7069495c488SFrank Chang case TDATA2: 7079495c488SFrank Chang return env->tdata2[env->trigger_cur]; 7089495c488SFrank Chang case TDATA3: 7099495c488SFrank Chang return env->tdata3[env->trigger_cur]; 710a42bd001SFrank Chang default: 711a42bd001SFrank Chang g_assert_not_reached(); 712a42bd001SFrank Chang } 71395799e36SBin Meng } 71495799e36SBin Meng 71595799e36SBin Meng void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val) 71695799e36SBin Meng { 717a42bd001SFrank Chang int trigger_type; 71895799e36SBin Meng 719a42bd001SFrank Chang if (tdata_index == TDATA1) { 720a42bd001SFrank Chang trigger_type = extract_trigger_type(env, val); 721a42bd001SFrank Chang } else { 722a42bd001SFrank Chang trigger_type = get_trigger_type(env, env->trigger_cur); 723a42bd001SFrank Chang } 724a42bd001SFrank Chang 725a42bd001SFrank Chang switch (trigger_type) { 726a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 727a42bd001SFrank Chang type2_reg_write(env, env->trigger_cur, tdata_index, val); 728a42bd001SFrank Chang break; 729c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 730c472c142SFrank Chang type6_reg_write(env, env->trigger_cur, tdata_index, val); 731c472c142SFrank Chang break; 732a42bd001SFrank Chang case TRIGGER_TYPE_INST_CNT: 73391809598SLIU Zhiwei itrigger_reg_write(env, env->trigger_cur, tdata_index, val); 73491809598SLIU Zhiwei break; 735a42bd001SFrank Chang case TRIGGER_TYPE_INT: 736a42bd001SFrank Chang case TRIGGER_TYPE_EXCP: 737a42bd001SFrank Chang case TRIGGER_TYPE_EXT_SRC: 738a42bd001SFrank Chang qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n", 739a42bd001SFrank Chang trigger_type); 740a42bd001SFrank Chang break; 741a42bd001SFrank Chang case TRIGGER_TYPE_NO_EXIST: 742a42bd001SFrank Chang case TRIGGER_TYPE_UNAVAIL: 743a42bd001SFrank Chang qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n", 744a42bd001SFrank Chang trigger_type); 745a42bd001SFrank Chang break; 746a42bd001SFrank Chang default: 747a42bd001SFrank Chang g_assert_not_reached(); 748a42bd001SFrank Chang } 74995799e36SBin Meng } 750b5f6379dSBin Meng 75131b9798dSFrank Chang target_ulong tinfo_csr_read(CPURISCVState *env) 75231b9798dSFrank Chang { 75331b9798dSFrank Chang /* assume all triggers support the same types of triggers */ 754c472c142SFrank Chang return BIT(TRIGGER_TYPE_AD_MATCH) | 755c472c142SFrank Chang BIT(TRIGGER_TYPE_AD_MATCH6); 75631b9798dSFrank Chang } 75731b9798dSFrank Chang 758b5f6379dSBin Meng void riscv_cpu_debug_excp_handler(CPUState *cs) 759b5f6379dSBin Meng { 760b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 761b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 762b5f6379dSBin Meng 763b5f6379dSBin Meng if (cs->watchpoint_hit) { 764b5f6379dSBin Meng if (cs->watchpoint_hit->flags & BP_CPU) { 765d1c11141SFrank Chang do_trigger_action(env, DBG_ACTION_BP); 766b5f6379dSBin Meng } 767b5f6379dSBin Meng } else { 768b5f6379dSBin Meng if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) { 769d1c11141SFrank Chang do_trigger_action(env, DBG_ACTION_BP); 770b5f6379dSBin Meng } 771b5f6379dSBin Meng } 772b5f6379dSBin Meng } 773b5f6379dSBin Meng 774b5f6379dSBin Meng bool riscv_cpu_debug_check_breakpoint(CPUState *cs) 775b5f6379dSBin Meng { 776b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 777b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 778b5f6379dSBin Meng CPUBreakpoint *bp; 779b5f6379dSBin Meng target_ulong ctrl; 780b5f6379dSBin Meng target_ulong pc; 781a42bd001SFrank Chang int trigger_type; 782b5f6379dSBin Meng int i; 783b5f6379dSBin Meng 784b5f6379dSBin Meng QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { 785a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 786a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 787a42bd001SFrank Chang 788a42bd001SFrank Chang switch (trigger_type) { 789a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 790c32461d8SFrank Chang /* type 2 trigger cannot be fired in VU/VS mode */ 79138256529SWeiwei Li if (env->virt_enabled) { 792c32461d8SFrank Chang return false; 793c32461d8SFrank Chang } 794c32461d8SFrank Chang 7959495c488SFrank Chang ctrl = env->tdata1[i]; 7969495c488SFrank Chang pc = env->tdata2[i]; 797b5f6379dSBin Meng 798b5f6379dSBin Meng if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) { 799b5f6379dSBin Meng /* check U/S/M bit against current privilege level */ 800b5f6379dSBin Meng if ((ctrl >> 3) & BIT(env->priv)) { 801*0099f605SDaniel Henrique Barboza env->badaddr = pc; 802b5f6379dSBin Meng return true; 803b5f6379dSBin Meng } 804b5f6379dSBin Meng } 805a42bd001SFrank Chang break; 806c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 807c472c142SFrank Chang ctrl = env->tdata1[i]; 808c472c142SFrank Chang pc = env->tdata2[i]; 809c472c142SFrank Chang 810c472c142SFrank Chang if ((ctrl & TYPE6_EXEC) && (bp->pc == pc)) { 81138256529SWeiwei Li if (env->virt_enabled) { 812c472c142SFrank Chang /* check VU/VS bit against current privilege level */ 813c472c142SFrank Chang if ((ctrl >> 23) & BIT(env->priv)) { 814*0099f605SDaniel Henrique Barboza env->badaddr = pc; 815c472c142SFrank Chang return true; 816c472c142SFrank Chang } 817c472c142SFrank Chang } else { 818c472c142SFrank Chang /* check U/S/M bit against current privilege level */ 819c472c142SFrank Chang if ((ctrl >> 3) & BIT(env->priv)) { 820*0099f605SDaniel Henrique Barboza env->badaddr = pc; 821c472c142SFrank Chang return true; 822c472c142SFrank Chang } 823c472c142SFrank Chang } 824c472c142SFrank Chang } 825c472c142SFrank Chang break; 826a42bd001SFrank Chang default: 827a42bd001SFrank Chang /* other trigger types are not supported or irrelevant */ 828a42bd001SFrank Chang break; 829a42bd001SFrank Chang } 830b5f6379dSBin Meng } 831b5f6379dSBin Meng } 832b5f6379dSBin Meng 833b5f6379dSBin Meng return false; 834b5f6379dSBin Meng } 835b5f6379dSBin Meng 836b5f6379dSBin Meng bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp) 837b5f6379dSBin Meng { 838b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 839b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 840b5f6379dSBin Meng target_ulong ctrl; 841b5f6379dSBin Meng target_ulong addr; 842a42bd001SFrank Chang int trigger_type; 843b5f6379dSBin Meng int flags; 844b5f6379dSBin Meng int i; 845b5f6379dSBin Meng 846a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 847a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 848a42bd001SFrank Chang 849a42bd001SFrank Chang switch (trigger_type) { 850a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 851c32461d8SFrank Chang /* type 2 trigger cannot be fired in VU/VS mode */ 85238256529SWeiwei Li if (env->virt_enabled) { 853c32461d8SFrank Chang return false; 854c32461d8SFrank Chang } 855c32461d8SFrank Chang 8569495c488SFrank Chang ctrl = env->tdata1[i]; 8579495c488SFrank Chang addr = env->tdata2[i]; 858b5f6379dSBin Meng flags = 0; 859b5f6379dSBin Meng 860b5f6379dSBin Meng if (ctrl & TYPE2_LOAD) { 861b5f6379dSBin Meng flags |= BP_MEM_READ; 862b5f6379dSBin Meng } 863b5f6379dSBin Meng if (ctrl & TYPE2_STORE) { 864b5f6379dSBin Meng flags |= BP_MEM_WRITE; 865b5f6379dSBin Meng } 866b5f6379dSBin Meng 867b5f6379dSBin Meng if ((wp->flags & flags) && (wp->vaddr == addr)) { 868b5f6379dSBin Meng /* check U/S/M bit against current privilege level */ 869b5f6379dSBin Meng if ((ctrl >> 3) & BIT(env->priv)) { 870b5f6379dSBin Meng return true; 871b5f6379dSBin Meng } 872b5f6379dSBin Meng } 873a42bd001SFrank Chang break; 874c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 875c472c142SFrank Chang ctrl = env->tdata1[i]; 876c472c142SFrank Chang addr = env->tdata2[i]; 877c472c142SFrank Chang flags = 0; 878c472c142SFrank Chang 879c472c142SFrank Chang if (ctrl & TYPE6_LOAD) { 880c472c142SFrank Chang flags |= BP_MEM_READ; 881c472c142SFrank Chang } 882c472c142SFrank Chang if (ctrl & TYPE6_STORE) { 883c472c142SFrank Chang flags |= BP_MEM_WRITE; 884c472c142SFrank Chang } 885c472c142SFrank Chang 886c472c142SFrank Chang if ((wp->flags & flags) && (wp->vaddr == addr)) { 88738256529SWeiwei Li if (env->virt_enabled) { 888c472c142SFrank Chang /* check VU/VS bit against current privilege level */ 889c472c142SFrank Chang if ((ctrl >> 23) & BIT(env->priv)) { 890c472c142SFrank Chang return true; 891c472c142SFrank Chang } 892c472c142SFrank Chang } else { 893c472c142SFrank Chang /* check U/S/M bit against current privilege level */ 894c472c142SFrank Chang if ((ctrl >> 3) & BIT(env->priv)) { 895c472c142SFrank Chang return true; 896c472c142SFrank Chang } 897c472c142SFrank Chang } 898c472c142SFrank Chang } 899c472c142SFrank Chang break; 900a42bd001SFrank Chang default: 901a42bd001SFrank Chang /* other trigger types are not supported */ 902a42bd001SFrank Chang break; 903a42bd001SFrank Chang } 904b5f6379dSBin Meng } 905b5f6379dSBin Meng 906b5f6379dSBin Meng return false; 907b5f6379dSBin Meng } 908b6092544SBin Meng 909a7c272dfSAkihiko Odaki void riscv_trigger_realize(CPURISCVState *env) 910a7c272dfSAkihiko Odaki { 911a7c272dfSAkihiko Odaki int i; 912a7c272dfSAkihiko Odaki 913a7c272dfSAkihiko Odaki for (i = 0; i < RV_MAX_TRIGGERS; i++) { 914a7c272dfSAkihiko Odaki env->itrigger_timer[i] = timer_new_ns(QEMU_CLOCK_VIRTUAL, 915a7c272dfSAkihiko Odaki riscv_itrigger_timer_cb, env); 916a7c272dfSAkihiko Odaki } 917a7c272dfSAkihiko Odaki } 918a7c272dfSAkihiko Odaki 919a7c272dfSAkihiko Odaki void riscv_trigger_reset_hold(CPURISCVState *env) 920b6092544SBin Meng { 9219d5a84dbSFrank Chang target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_AD_MATCH, 0, 0); 922b6092544SBin Meng int i; 923b6092544SBin Meng 924a42bd001SFrank Chang /* init to type 2 triggers */ 925a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 926b6092544SBin Meng /* 927b6092544SBin Meng * type = TRIGGER_TYPE_AD_MATCH 928b6092544SBin Meng * dmode = 0 (both debug and M-mode can write tdata) 929b6092544SBin Meng * maskmax = 0 (unimplemented, always 0) 930b6092544SBin Meng * sizehi = 0 (match against any size, RV64 only) 931b6092544SBin Meng * hit = 0 (unimplemented, always 0) 932b6092544SBin Meng * select = 0 (always 0, perform match on address) 933b6092544SBin Meng * timing = 0 (always 0, trigger before instruction) 934b6092544SBin Meng * sizelo = 0 (match against any size) 935b6092544SBin Meng * action = 0 (always 0, raise a breakpoint exception) 936b6092544SBin Meng * chain = 0 (unimplemented, always 0) 937b6092544SBin Meng * match = 0 (always 0, when any compare value equals tdata2) 938b6092544SBin Meng */ 9399495c488SFrank Chang env->tdata1[i] = tdata1; 9409495c488SFrank Chang env->tdata2[i] = 0; 9419495c488SFrank Chang env->tdata3[i] = 0; 9429495c488SFrank Chang env->cpu_breakpoint[i] = NULL; 9439495c488SFrank Chang env->cpu_watchpoint[i] = NULL; 944a7c272dfSAkihiko Odaki timer_del(env->itrigger_timer[i]); 945b6092544SBin Meng } 9460c4e579aSAlvin Chang 9470c4e579aSAlvin Chang env->mcontext = 0; 948b6092544SBin Meng } 949