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" 32*2c9d7471SLIU Zhiwei #include "exec/helper-proto.h" 3395799e36SBin Meng 3495799e36SBin Meng /* 3595799e36SBin Meng * The following M-mode trigger CSRs are implemented: 3695799e36SBin Meng * 3795799e36SBin Meng * - tselect 3895799e36SBin Meng * - tdata1 3995799e36SBin Meng * - tdata2 4095799e36SBin Meng * - tdata3 4131b9798dSFrank Chang * - tinfo 4295799e36SBin Meng * 43c472c142SFrank Chang * The following triggers are initialized by default: 4495799e36SBin Meng * 4595799e36SBin Meng * Index | Type | tdata mapping | Description 4695799e36SBin Meng * ------+------+------------------------+------------ 4795799e36SBin Meng * 0 | 2 | tdata1, tdata2 | Address / Data Match 4895799e36SBin Meng * 1 | 2 | tdata1, tdata2 | Address / Data Match 4995799e36SBin Meng */ 5095799e36SBin Meng 5195799e36SBin Meng /* tdata availability of a trigger */ 5295799e36SBin Meng typedef bool tdata_avail[TDATA_NUM]; 5395799e36SBin Meng 54a42bd001SFrank Chang static tdata_avail tdata_mapping[TRIGGER_TYPE_NUM] = { 55a42bd001SFrank Chang [TRIGGER_TYPE_NO_EXIST] = { false, false, false }, 56a42bd001SFrank Chang [TRIGGER_TYPE_AD_MATCH] = { true, true, true }, 57a42bd001SFrank Chang [TRIGGER_TYPE_INST_CNT] = { true, false, true }, 58a42bd001SFrank Chang [TRIGGER_TYPE_INT] = { true, true, true }, 59a42bd001SFrank Chang [TRIGGER_TYPE_EXCP] = { true, true, true }, 60a42bd001SFrank Chang [TRIGGER_TYPE_AD_MATCH6] = { true, true, true }, 61a42bd001SFrank Chang [TRIGGER_TYPE_EXT_SRC] = { true, false, false }, 62a42bd001SFrank Chang [TRIGGER_TYPE_UNAVAIL] = { true, true, true } 6395799e36SBin Meng }; 6495799e36SBin Meng 6595799e36SBin Meng /* only breakpoint size 1/2/4/8 supported */ 6695799e36SBin Meng static int access_size[SIZE_NUM] = { 6795799e36SBin Meng [SIZE_ANY] = 0, 6895799e36SBin Meng [SIZE_1B] = 1, 6995799e36SBin Meng [SIZE_2B] = 2, 7095799e36SBin Meng [SIZE_4B] = 4, 7195799e36SBin Meng [SIZE_6B] = -1, 7295799e36SBin Meng [SIZE_8B] = 8, 7395799e36SBin Meng [6 ... 15] = -1, 7495799e36SBin Meng }; 7595799e36SBin Meng 76a42bd001SFrank Chang static inline target_ulong extract_trigger_type(CPURISCVState *env, 77a42bd001SFrank Chang target_ulong tdata1) 78a42bd001SFrank Chang { 79a42bd001SFrank Chang switch (riscv_cpu_mxl(env)) { 80a42bd001SFrank Chang case MXL_RV32: 81a42bd001SFrank Chang return extract32(tdata1, 28, 4); 82a42bd001SFrank Chang case MXL_RV64: 83a42bd001SFrank Chang case MXL_RV128: 84a42bd001SFrank Chang return extract64(tdata1, 60, 4); 85a42bd001SFrank Chang default: 86a42bd001SFrank Chang g_assert_not_reached(); 87a42bd001SFrank Chang } 88a42bd001SFrank Chang } 89a42bd001SFrank Chang 90a42bd001SFrank Chang static inline target_ulong get_trigger_type(CPURISCVState *env, 91a42bd001SFrank Chang target_ulong trigger_index) 92a42bd001SFrank Chang { 939495c488SFrank Chang return extract_trigger_type(env, env->tdata1[trigger_index]); 94a42bd001SFrank Chang } 95a42bd001SFrank Chang 96d1c11141SFrank Chang static trigger_action_t get_trigger_action(CPURISCVState *env, 97d1c11141SFrank Chang target_ulong trigger_index) 98d1c11141SFrank Chang { 99d1c11141SFrank Chang target_ulong tdata1 = env->tdata1[trigger_index]; 100d1c11141SFrank Chang int trigger_type = get_trigger_type(env, trigger_index); 101d1c11141SFrank Chang trigger_action_t action = DBG_ACTION_NONE; 102d1c11141SFrank Chang 103d1c11141SFrank Chang switch (trigger_type) { 104d1c11141SFrank Chang case TRIGGER_TYPE_AD_MATCH: 105d1c11141SFrank Chang action = (tdata1 & TYPE2_ACTION) >> 12; 106d1c11141SFrank Chang break; 107c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 108c472c142SFrank Chang action = (tdata1 & TYPE6_ACTION) >> 12; 109c472c142SFrank Chang break; 110d1c11141SFrank Chang case TRIGGER_TYPE_INST_CNT: 111d1c11141SFrank Chang case TRIGGER_TYPE_INT: 112d1c11141SFrank Chang case TRIGGER_TYPE_EXCP: 113d1c11141SFrank Chang case TRIGGER_TYPE_EXT_SRC: 114d1c11141SFrank Chang qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n", 115d1c11141SFrank Chang trigger_type); 116d1c11141SFrank Chang break; 117d1c11141SFrank Chang case TRIGGER_TYPE_NO_EXIST: 118d1c11141SFrank Chang case TRIGGER_TYPE_UNAVAIL: 119d1c11141SFrank Chang qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n", 120d1c11141SFrank Chang trigger_type); 121d1c11141SFrank Chang break; 122d1c11141SFrank Chang default: 123d1c11141SFrank Chang g_assert_not_reached(); 124d1c11141SFrank Chang } 125d1c11141SFrank Chang 126d1c11141SFrank Chang return action; 127d1c11141SFrank Chang } 128d1c11141SFrank Chang 1299d5a84dbSFrank Chang static inline target_ulong build_tdata1(CPURISCVState *env, 1309d5a84dbSFrank Chang trigger_type_t type, 1319d5a84dbSFrank Chang bool dmode, target_ulong data) 13295799e36SBin Meng { 13395799e36SBin Meng target_ulong tdata1; 13495799e36SBin Meng 13595799e36SBin Meng switch (riscv_cpu_mxl(env)) { 13695799e36SBin Meng case MXL_RV32: 1379d5a84dbSFrank Chang tdata1 = RV32_TYPE(type) | 1389d5a84dbSFrank Chang (dmode ? RV32_DMODE : 0) | 1399d5a84dbSFrank Chang (data & RV32_DATA_MASK); 14095799e36SBin Meng break; 14195799e36SBin Meng case MXL_RV64: 142d1d85412SFrédéric Pétrot case MXL_RV128: 1439d5a84dbSFrank Chang tdata1 = RV64_TYPE(type) | 1449d5a84dbSFrank Chang (dmode ? RV64_DMODE : 0) | 1459d5a84dbSFrank Chang (data & RV64_DATA_MASK); 14695799e36SBin Meng break; 14795799e36SBin Meng default: 14895799e36SBin Meng g_assert_not_reached(); 14995799e36SBin Meng } 15095799e36SBin Meng 15195799e36SBin Meng return tdata1; 15295799e36SBin Meng } 15395799e36SBin Meng 15495799e36SBin Meng bool tdata_available(CPURISCVState *env, int tdata_index) 15595799e36SBin Meng { 156a42bd001SFrank Chang int trigger_type = get_trigger_type(env, env->trigger_cur); 157a42bd001SFrank Chang 15895799e36SBin Meng if (unlikely(tdata_index >= TDATA_NUM)) { 15995799e36SBin Meng return false; 16095799e36SBin Meng } 16195799e36SBin Meng 162a42bd001SFrank Chang return tdata_mapping[trigger_type][tdata_index]; 16395799e36SBin Meng } 16495799e36SBin Meng 16595799e36SBin Meng target_ulong tselect_csr_read(CPURISCVState *env) 16695799e36SBin Meng { 16795799e36SBin Meng return env->trigger_cur; 16895799e36SBin Meng } 16995799e36SBin Meng 17095799e36SBin Meng void tselect_csr_write(CPURISCVState *env, target_ulong val) 17195799e36SBin Meng { 1726ea8d3fcSFrank Chang if (val < RV_MAX_TRIGGERS) { 17395799e36SBin Meng env->trigger_cur = val; 17495799e36SBin Meng } 1756ea8d3fcSFrank Chang } 17695799e36SBin Meng 17795799e36SBin Meng static target_ulong tdata1_validate(CPURISCVState *env, target_ulong val, 17895799e36SBin Meng trigger_type_t t) 17995799e36SBin Meng { 18095799e36SBin Meng uint32_t type, dmode; 18195799e36SBin Meng target_ulong tdata1; 18295799e36SBin Meng 18395799e36SBin Meng switch (riscv_cpu_mxl(env)) { 18495799e36SBin Meng case MXL_RV32: 18595799e36SBin Meng type = extract32(val, 28, 4); 18695799e36SBin Meng dmode = extract32(val, 27, 1); 18795799e36SBin Meng tdata1 = RV32_TYPE(t); 18895799e36SBin Meng break; 18995799e36SBin Meng case MXL_RV64: 190d1d85412SFrédéric Pétrot case MXL_RV128: 19195799e36SBin Meng type = extract64(val, 60, 4); 19295799e36SBin Meng dmode = extract64(val, 59, 1); 19395799e36SBin Meng tdata1 = RV64_TYPE(t); 19495799e36SBin Meng break; 19595799e36SBin Meng default: 19695799e36SBin Meng g_assert_not_reached(); 19795799e36SBin Meng } 19895799e36SBin Meng 19995799e36SBin Meng if (type != t) { 20095799e36SBin Meng qemu_log_mask(LOG_GUEST_ERROR, 20195799e36SBin Meng "ignoring type write to tdata1 register\n"); 20295799e36SBin Meng } 203a42bd001SFrank Chang 20495799e36SBin Meng if (dmode != 0) { 20595799e36SBin Meng qemu_log_mask(LOG_UNIMP, "debug mode is not supported\n"); 20695799e36SBin Meng } 20795799e36SBin Meng 20895799e36SBin Meng return tdata1; 20995799e36SBin Meng } 21095799e36SBin Meng 21195799e36SBin Meng static inline void warn_always_zero_bit(target_ulong val, target_ulong mask, 21295799e36SBin Meng const char *msg) 21395799e36SBin Meng { 21495799e36SBin Meng if (val & mask) { 21595799e36SBin Meng qemu_log_mask(LOG_UNIMP, "%s bit is always zero\n", msg); 21695799e36SBin Meng } 21795799e36SBin Meng } 21895799e36SBin Meng 219d1c11141SFrank Chang static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index) 220d1c11141SFrank Chang { 221d1c11141SFrank Chang trigger_action_t action = get_trigger_action(env, trigger_index); 222d1c11141SFrank Chang 223d1c11141SFrank Chang switch (action) { 224d1c11141SFrank Chang case DBG_ACTION_NONE: 225d1c11141SFrank Chang break; 226d1c11141SFrank Chang case DBG_ACTION_BP: 227d1c11141SFrank Chang riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0); 228d1c11141SFrank Chang break; 229d1c11141SFrank Chang case DBG_ACTION_DBG_MODE: 230d1c11141SFrank Chang case DBG_ACTION_TRACE0: 231d1c11141SFrank Chang case DBG_ACTION_TRACE1: 232d1c11141SFrank Chang case DBG_ACTION_TRACE2: 233d1c11141SFrank Chang case DBG_ACTION_TRACE3: 234d1c11141SFrank Chang case DBG_ACTION_EXT_DBG0: 235d1c11141SFrank Chang case DBG_ACTION_EXT_DBG1: 236d1c11141SFrank Chang qemu_log_mask(LOG_UNIMP, "action: %d is not supported\n", action); 237d1c11141SFrank Chang break; 238d1c11141SFrank Chang default: 239d1c11141SFrank Chang g_assert_not_reached(); 240d1c11141SFrank Chang } 241d1c11141SFrank Chang } 242d1c11141SFrank Chang 2439495c488SFrank Chang /* type 2 trigger */ 2449495c488SFrank Chang 24595799e36SBin Meng static uint32_t type2_breakpoint_size(CPURISCVState *env, target_ulong ctrl) 24695799e36SBin Meng { 24766997c42SMarkus Armbruster uint32_t sizelo, sizehi = 0; 24895799e36SBin Meng 24995799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 25095799e36SBin Meng sizehi = extract32(ctrl, 21, 2); 25195799e36SBin Meng } 25295799e36SBin Meng sizelo = extract32(ctrl, 16, 2); 25366997c42SMarkus Armbruster return (sizehi << 2) | sizelo; 25495799e36SBin Meng } 25595799e36SBin Meng 25695799e36SBin Meng static inline bool type2_breakpoint_enabled(target_ulong ctrl) 25795799e36SBin Meng { 25895799e36SBin Meng bool mode = !!(ctrl & (TYPE2_U | TYPE2_S | TYPE2_M)); 25995799e36SBin Meng bool rwx = !!(ctrl & (TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 26095799e36SBin Meng 26195799e36SBin Meng return mode && rwx; 26295799e36SBin Meng } 26395799e36SBin Meng 26495799e36SBin Meng static target_ulong type2_mcontrol_validate(CPURISCVState *env, 26595799e36SBin Meng target_ulong ctrl) 26695799e36SBin Meng { 26795799e36SBin Meng target_ulong val; 26895799e36SBin Meng uint32_t size; 26995799e36SBin Meng 27095799e36SBin Meng /* validate the generic part first */ 27195799e36SBin Meng val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH); 27295799e36SBin Meng 27395799e36SBin Meng /* validate unimplemented (always zero) bits */ 27495799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_MATCH, "match"); 27595799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain"); 27695799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_ACTION, "action"); 27795799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing"); 27895799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_SELECT, "select"); 27995799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_HIT, "hit"); 28095799e36SBin Meng 28195799e36SBin Meng /* validate size encoding */ 28295799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 28395799e36SBin Meng if (access_size[size] == -1) { 28495799e36SBin Meng qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using SIZE_ANY\n", 28595799e36SBin Meng size); 28695799e36SBin Meng } else { 28795799e36SBin Meng val |= (ctrl & TYPE2_SIZELO); 28895799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 28995799e36SBin Meng val |= (ctrl & TYPE2_SIZEHI); 29095799e36SBin Meng } 29195799e36SBin Meng } 29295799e36SBin Meng 29395799e36SBin Meng /* keep the mode and attribute bits */ 29495799e36SBin Meng val |= (ctrl & (TYPE2_U | TYPE2_S | TYPE2_M | 29595799e36SBin Meng TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 29695799e36SBin Meng 29795799e36SBin Meng return val; 29895799e36SBin Meng } 29995799e36SBin Meng 30095799e36SBin Meng static void type2_breakpoint_insert(CPURISCVState *env, target_ulong index) 30195799e36SBin Meng { 3029495c488SFrank Chang target_ulong ctrl = env->tdata1[index]; 3039495c488SFrank Chang target_ulong addr = env->tdata2[index]; 30495799e36SBin Meng bool enabled = type2_breakpoint_enabled(ctrl); 30595799e36SBin Meng CPUState *cs = env_cpu(env); 30695799e36SBin Meng int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 30795799e36SBin Meng uint32_t size; 30895799e36SBin Meng 30995799e36SBin Meng if (!enabled) { 31095799e36SBin Meng return; 31195799e36SBin Meng } 31295799e36SBin Meng 31395799e36SBin Meng if (ctrl & TYPE2_EXEC) { 3149495c488SFrank Chang cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]); 31595799e36SBin Meng } 31695799e36SBin Meng 31795799e36SBin Meng if (ctrl & TYPE2_LOAD) { 31895799e36SBin Meng flags |= BP_MEM_READ; 31995799e36SBin Meng } 32095799e36SBin Meng if (ctrl & TYPE2_STORE) { 32195799e36SBin Meng flags |= BP_MEM_WRITE; 32295799e36SBin Meng } 32395799e36SBin Meng 32495799e36SBin Meng if (flags & BP_MEM_ACCESS) { 32595799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 32695799e36SBin Meng if (size != 0) { 32795799e36SBin Meng cpu_watchpoint_insert(cs, addr, size, flags, 3289495c488SFrank Chang &env->cpu_watchpoint[index]); 32995799e36SBin Meng } else { 33095799e36SBin Meng cpu_watchpoint_insert(cs, addr, 8, flags, 3319495c488SFrank Chang &env->cpu_watchpoint[index]); 33295799e36SBin Meng } 33395799e36SBin Meng } 33495799e36SBin Meng } 33595799e36SBin Meng 33695799e36SBin Meng static void type2_breakpoint_remove(CPURISCVState *env, target_ulong index) 33795799e36SBin Meng { 33895799e36SBin Meng CPUState *cs = env_cpu(env); 33995799e36SBin Meng 3409495c488SFrank Chang if (env->cpu_breakpoint[index]) { 3419495c488SFrank Chang cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]); 3429495c488SFrank Chang env->cpu_breakpoint[index] = NULL; 34395799e36SBin Meng } 34495799e36SBin Meng 3459495c488SFrank Chang if (env->cpu_watchpoint[index]) { 3469495c488SFrank Chang cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]); 3479495c488SFrank Chang env->cpu_watchpoint[index] = NULL; 34895799e36SBin Meng } 34995799e36SBin Meng } 35095799e36SBin Meng 351a42bd001SFrank Chang static void type2_reg_write(CPURISCVState *env, target_ulong index, 35295799e36SBin Meng int tdata_index, target_ulong val) 35395799e36SBin Meng { 35495799e36SBin Meng target_ulong new_val; 35595799e36SBin Meng 35695799e36SBin Meng switch (tdata_index) { 35795799e36SBin Meng case TDATA1: 35895799e36SBin Meng new_val = type2_mcontrol_validate(env, val); 3599495c488SFrank Chang if (new_val != env->tdata1[index]) { 3609495c488SFrank Chang env->tdata1[index] = new_val; 36195799e36SBin Meng type2_breakpoint_remove(env, index); 36295799e36SBin Meng type2_breakpoint_insert(env, index); 36395799e36SBin Meng } 36495799e36SBin Meng break; 36595799e36SBin Meng case TDATA2: 3669495c488SFrank Chang if (val != env->tdata2[index]) { 3679495c488SFrank Chang env->tdata2[index] = val; 36895799e36SBin Meng type2_breakpoint_remove(env, index); 36995799e36SBin Meng type2_breakpoint_insert(env, index); 37095799e36SBin Meng } 37195799e36SBin Meng break; 3729495c488SFrank Chang case TDATA3: 3739495c488SFrank Chang qemu_log_mask(LOG_UNIMP, 3749495c488SFrank Chang "tdata3 is not supported for type 2 trigger\n"); 3759495c488SFrank Chang break; 37695799e36SBin Meng default: 37795799e36SBin Meng g_assert_not_reached(); 37895799e36SBin Meng } 37995799e36SBin Meng 38095799e36SBin Meng return; 38195799e36SBin Meng } 38295799e36SBin Meng 383c472c142SFrank Chang /* type 6 trigger */ 384c472c142SFrank Chang 385c472c142SFrank Chang static inline bool type6_breakpoint_enabled(target_ulong ctrl) 386c472c142SFrank Chang { 387c472c142SFrank Chang bool mode = !!(ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M)); 388c472c142SFrank Chang bool rwx = !!(ctrl & (TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC)); 389c472c142SFrank Chang 390c472c142SFrank Chang return mode && rwx; 391c472c142SFrank Chang } 392c472c142SFrank Chang 393c472c142SFrank Chang static target_ulong type6_mcontrol6_validate(CPURISCVState *env, 394c472c142SFrank Chang target_ulong ctrl) 395c472c142SFrank Chang { 396c472c142SFrank Chang target_ulong val; 397c472c142SFrank Chang uint32_t size; 398c472c142SFrank Chang 399c472c142SFrank Chang /* validate the generic part first */ 400c472c142SFrank Chang val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH6); 401c472c142SFrank Chang 402c472c142SFrank Chang /* validate unimplemented (always zero) bits */ 403c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_MATCH, "match"); 404c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_CHAIN, "chain"); 405c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_ACTION, "action"); 406c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_TIMING, "timing"); 407c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_SELECT, "select"); 408c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_HIT, "hit"); 409c472c142SFrank Chang 410c472c142SFrank Chang /* validate size encoding */ 411c472c142SFrank Chang size = extract32(ctrl, 16, 4); 412c472c142SFrank Chang if (access_size[size] == -1) { 413c472c142SFrank Chang qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using SIZE_ANY\n", 414c472c142SFrank Chang size); 415c472c142SFrank Chang } else { 416c472c142SFrank Chang val |= (ctrl & TYPE6_SIZE); 417c472c142SFrank Chang } 418c472c142SFrank Chang 419c472c142SFrank Chang /* keep the mode and attribute bits */ 420c472c142SFrank Chang val |= (ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M | 421c472c142SFrank Chang TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC)); 422c472c142SFrank Chang 423c472c142SFrank Chang return val; 424c472c142SFrank Chang } 425c472c142SFrank Chang 426c472c142SFrank Chang static void type6_breakpoint_insert(CPURISCVState *env, target_ulong index) 427c472c142SFrank Chang { 428c472c142SFrank Chang target_ulong ctrl = env->tdata1[index]; 429c472c142SFrank Chang target_ulong addr = env->tdata2[index]; 430c472c142SFrank Chang bool enabled = type6_breakpoint_enabled(ctrl); 431c472c142SFrank Chang CPUState *cs = env_cpu(env); 432c472c142SFrank Chang int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 433c472c142SFrank Chang uint32_t size; 434c472c142SFrank Chang 435c472c142SFrank Chang if (!enabled) { 436c472c142SFrank Chang return; 437c472c142SFrank Chang } 438c472c142SFrank Chang 439c472c142SFrank Chang if (ctrl & TYPE6_EXEC) { 440c472c142SFrank Chang cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]); 441c472c142SFrank Chang } 442c472c142SFrank Chang 443c472c142SFrank Chang if (ctrl & TYPE6_LOAD) { 444c472c142SFrank Chang flags |= BP_MEM_READ; 445c472c142SFrank Chang } 446c472c142SFrank Chang 447c472c142SFrank Chang if (ctrl & TYPE6_STORE) { 448c472c142SFrank Chang flags |= BP_MEM_WRITE; 449c472c142SFrank Chang } 450c472c142SFrank Chang 451c472c142SFrank Chang if (flags & BP_MEM_ACCESS) { 452c472c142SFrank Chang size = extract32(ctrl, 16, 4); 453c472c142SFrank Chang if (size != 0) { 454c472c142SFrank Chang cpu_watchpoint_insert(cs, addr, size, flags, 455c472c142SFrank Chang &env->cpu_watchpoint[index]); 456c472c142SFrank Chang } else { 457c472c142SFrank Chang cpu_watchpoint_insert(cs, addr, 8, flags, 458c472c142SFrank Chang &env->cpu_watchpoint[index]); 459c472c142SFrank Chang } 460c472c142SFrank Chang } 461c472c142SFrank Chang } 462c472c142SFrank Chang 463c472c142SFrank Chang static void type6_breakpoint_remove(CPURISCVState *env, target_ulong index) 464c472c142SFrank Chang { 465c472c142SFrank Chang type2_breakpoint_remove(env, index); 466c472c142SFrank Chang } 467c472c142SFrank Chang 468c472c142SFrank Chang static void type6_reg_write(CPURISCVState *env, target_ulong index, 469c472c142SFrank Chang int tdata_index, target_ulong val) 470c472c142SFrank Chang { 471c472c142SFrank Chang target_ulong new_val; 472c472c142SFrank Chang 473c472c142SFrank Chang switch (tdata_index) { 474c472c142SFrank Chang case TDATA1: 475c472c142SFrank Chang new_val = type6_mcontrol6_validate(env, val); 476c472c142SFrank Chang if (new_val != env->tdata1[index]) { 477c472c142SFrank Chang env->tdata1[index] = new_val; 478c472c142SFrank Chang type6_breakpoint_remove(env, index); 479c472c142SFrank Chang type6_breakpoint_insert(env, index); 480c472c142SFrank Chang } 481c472c142SFrank Chang break; 482c472c142SFrank Chang case TDATA2: 483c472c142SFrank Chang if (val != env->tdata2[index]) { 484c472c142SFrank Chang env->tdata2[index] = val; 485c472c142SFrank Chang type6_breakpoint_remove(env, index); 486c472c142SFrank Chang type6_breakpoint_insert(env, index); 487c472c142SFrank Chang } 488c472c142SFrank Chang break; 489c472c142SFrank Chang case TDATA3: 490c472c142SFrank Chang qemu_log_mask(LOG_UNIMP, 491c472c142SFrank Chang "tdata3 is not supported for type 6 trigger\n"); 492c472c142SFrank Chang break; 493c472c142SFrank Chang default: 494c472c142SFrank Chang g_assert_not_reached(); 495c472c142SFrank Chang } 496c472c142SFrank Chang 497c472c142SFrank Chang return; 498c472c142SFrank Chang } 499c472c142SFrank Chang 500*2c9d7471SLIU Zhiwei /* icount trigger type */ 501*2c9d7471SLIU Zhiwei static inline int 502*2c9d7471SLIU Zhiwei itrigger_get_count(CPURISCVState *env, int index) 503*2c9d7471SLIU Zhiwei { 504*2c9d7471SLIU Zhiwei return get_field(env->tdata1[index], ITRIGGER_COUNT); 505*2c9d7471SLIU Zhiwei } 506*2c9d7471SLIU Zhiwei 507*2c9d7471SLIU Zhiwei static inline void 508*2c9d7471SLIU Zhiwei itrigger_set_count(CPURISCVState *env, int index, int value) 509*2c9d7471SLIU Zhiwei { 510*2c9d7471SLIU Zhiwei env->tdata1[index] = set_field(env->tdata1[index], 511*2c9d7471SLIU Zhiwei ITRIGGER_COUNT, value); 512*2c9d7471SLIU Zhiwei } 513*2c9d7471SLIU Zhiwei 514*2c9d7471SLIU Zhiwei static bool check_itrigger_priv(CPURISCVState *env, int index) 515*2c9d7471SLIU Zhiwei { 516*2c9d7471SLIU Zhiwei target_ulong tdata1 = env->tdata1[index]; 517*2c9d7471SLIU Zhiwei if (riscv_cpu_virt_enabled(env)) { 518*2c9d7471SLIU Zhiwei /* check VU/VS bit against current privilege level */ 519*2c9d7471SLIU Zhiwei return (get_field(tdata1, ITRIGGER_VS) == env->priv) || 520*2c9d7471SLIU Zhiwei (get_field(tdata1, ITRIGGER_VU) == env->priv); 521*2c9d7471SLIU Zhiwei } else { 522*2c9d7471SLIU Zhiwei /* check U/S/M bit against current privilege level */ 523*2c9d7471SLIU Zhiwei return (get_field(tdata1, ITRIGGER_M) == env->priv) || 524*2c9d7471SLIU Zhiwei (get_field(tdata1, ITRIGGER_S) == env->priv) || 525*2c9d7471SLIU Zhiwei (get_field(tdata1, ITRIGGER_U) == env->priv); 526*2c9d7471SLIU Zhiwei } 527*2c9d7471SLIU Zhiwei } 528*2c9d7471SLIU Zhiwei 529*2c9d7471SLIU Zhiwei bool riscv_itrigger_enabled(CPURISCVState *env) 530*2c9d7471SLIU Zhiwei { 531*2c9d7471SLIU Zhiwei int count; 532*2c9d7471SLIU Zhiwei for (int i = 0; i < RV_MAX_TRIGGERS; i++) { 533*2c9d7471SLIU Zhiwei if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) { 534*2c9d7471SLIU Zhiwei continue; 535*2c9d7471SLIU Zhiwei } 536*2c9d7471SLIU Zhiwei if (check_itrigger_priv(env, i)) { 537*2c9d7471SLIU Zhiwei continue; 538*2c9d7471SLIU Zhiwei } 539*2c9d7471SLIU Zhiwei count = itrigger_get_count(env, i); 540*2c9d7471SLIU Zhiwei if (!count) { 541*2c9d7471SLIU Zhiwei continue; 542*2c9d7471SLIU Zhiwei } 543*2c9d7471SLIU Zhiwei return true; 544*2c9d7471SLIU Zhiwei } 545*2c9d7471SLIU Zhiwei 546*2c9d7471SLIU Zhiwei return false; 547*2c9d7471SLIU Zhiwei } 548*2c9d7471SLIU Zhiwei 549*2c9d7471SLIU Zhiwei void helper_itrigger_match(CPURISCVState *env) 550*2c9d7471SLIU Zhiwei { 551*2c9d7471SLIU Zhiwei int count; 552*2c9d7471SLIU Zhiwei for (int i = 0; i < RV_MAX_TRIGGERS; i++) { 553*2c9d7471SLIU Zhiwei if (get_trigger_type(env, i) != TRIGGER_TYPE_INST_CNT) { 554*2c9d7471SLIU Zhiwei continue; 555*2c9d7471SLIU Zhiwei } 556*2c9d7471SLIU Zhiwei if (check_itrigger_priv(env, i)) { 557*2c9d7471SLIU Zhiwei continue; 558*2c9d7471SLIU Zhiwei } 559*2c9d7471SLIU Zhiwei count = itrigger_get_count(env, i); 560*2c9d7471SLIU Zhiwei if (!count) { 561*2c9d7471SLIU Zhiwei continue; 562*2c9d7471SLIU Zhiwei } 563*2c9d7471SLIU Zhiwei itrigger_set_count(env, i, count--); 564*2c9d7471SLIU Zhiwei if (!count) { 565*2c9d7471SLIU Zhiwei do_trigger_action(env, i); 566*2c9d7471SLIU Zhiwei } 567*2c9d7471SLIU Zhiwei } 568*2c9d7471SLIU Zhiwei } 569*2c9d7471SLIU Zhiwei 57095799e36SBin Meng target_ulong tdata_csr_read(CPURISCVState *env, int tdata_index) 57195799e36SBin Meng { 5729495c488SFrank Chang switch (tdata_index) { 5739495c488SFrank Chang case TDATA1: 5749495c488SFrank Chang return env->tdata1[env->trigger_cur]; 5759495c488SFrank Chang case TDATA2: 5769495c488SFrank Chang return env->tdata2[env->trigger_cur]; 5779495c488SFrank Chang case TDATA3: 5789495c488SFrank Chang return env->tdata3[env->trigger_cur]; 579a42bd001SFrank Chang default: 580a42bd001SFrank Chang g_assert_not_reached(); 581a42bd001SFrank Chang } 58295799e36SBin Meng } 58395799e36SBin Meng 58495799e36SBin Meng void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val) 58595799e36SBin Meng { 586a42bd001SFrank Chang int trigger_type; 58795799e36SBin Meng 588a42bd001SFrank Chang if (tdata_index == TDATA1) { 589a42bd001SFrank Chang trigger_type = extract_trigger_type(env, val); 590a42bd001SFrank Chang } else { 591a42bd001SFrank Chang trigger_type = get_trigger_type(env, env->trigger_cur); 592a42bd001SFrank Chang } 593a42bd001SFrank Chang 594a42bd001SFrank Chang switch (trigger_type) { 595a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 596a42bd001SFrank Chang type2_reg_write(env, env->trigger_cur, tdata_index, val); 597a42bd001SFrank Chang break; 598c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 599c472c142SFrank Chang type6_reg_write(env, env->trigger_cur, tdata_index, val); 600c472c142SFrank Chang break; 601a42bd001SFrank Chang case TRIGGER_TYPE_INST_CNT: 602a42bd001SFrank Chang case TRIGGER_TYPE_INT: 603a42bd001SFrank Chang case TRIGGER_TYPE_EXCP: 604a42bd001SFrank Chang case TRIGGER_TYPE_EXT_SRC: 605a42bd001SFrank Chang qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n", 606a42bd001SFrank Chang trigger_type); 607a42bd001SFrank Chang break; 608a42bd001SFrank Chang case TRIGGER_TYPE_NO_EXIST: 609a42bd001SFrank Chang case TRIGGER_TYPE_UNAVAIL: 610a42bd001SFrank Chang qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n", 611a42bd001SFrank Chang trigger_type); 612a42bd001SFrank Chang break; 613a42bd001SFrank Chang default: 614a42bd001SFrank Chang g_assert_not_reached(); 615a42bd001SFrank Chang } 61695799e36SBin Meng } 617b5f6379dSBin Meng 61831b9798dSFrank Chang target_ulong tinfo_csr_read(CPURISCVState *env) 61931b9798dSFrank Chang { 62031b9798dSFrank Chang /* assume all triggers support the same types of triggers */ 621c472c142SFrank Chang return BIT(TRIGGER_TYPE_AD_MATCH) | 622c472c142SFrank Chang BIT(TRIGGER_TYPE_AD_MATCH6); 62331b9798dSFrank Chang } 62431b9798dSFrank Chang 625b5f6379dSBin Meng void riscv_cpu_debug_excp_handler(CPUState *cs) 626b5f6379dSBin Meng { 627b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 628b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 629b5f6379dSBin Meng 630b5f6379dSBin Meng if (cs->watchpoint_hit) { 631b5f6379dSBin Meng if (cs->watchpoint_hit->flags & BP_CPU) { 632b5f6379dSBin Meng cs->watchpoint_hit = NULL; 633d1c11141SFrank Chang do_trigger_action(env, DBG_ACTION_BP); 634b5f6379dSBin Meng } 635b5f6379dSBin Meng } else { 636b5f6379dSBin Meng if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) { 637d1c11141SFrank Chang do_trigger_action(env, DBG_ACTION_BP); 638b5f6379dSBin Meng } 639b5f6379dSBin Meng } 640b5f6379dSBin Meng } 641b5f6379dSBin Meng 642b5f6379dSBin Meng bool riscv_cpu_debug_check_breakpoint(CPUState *cs) 643b5f6379dSBin Meng { 644b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 645b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 646b5f6379dSBin Meng CPUBreakpoint *bp; 647b5f6379dSBin Meng target_ulong ctrl; 648b5f6379dSBin Meng target_ulong pc; 649a42bd001SFrank Chang int trigger_type; 650b5f6379dSBin Meng int i; 651b5f6379dSBin Meng 652b5f6379dSBin Meng QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { 653a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 654a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 655a42bd001SFrank Chang 656a42bd001SFrank Chang switch (trigger_type) { 657a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 658c32461d8SFrank Chang /* type 2 trigger cannot be fired in VU/VS mode */ 659c32461d8SFrank Chang if (riscv_cpu_virt_enabled(env)) { 660c32461d8SFrank Chang return false; 661c32461d8SFrank Chang } 662c32461d8SFrank Chang 6639495c488SFrank Chang ctrl = env->tdata1[i]; 6649495c488SFrank Chang pc = env->tdata2[i]; 665b5f6379dSBin Meng 666b5f6379dSBin Meng if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) { 667b5f6379dSBin Meng /* check U/S/M bit against current privilege level */ 668b5f6379dSBin Meng if ((ctrl >> 3) & BIT(env->priv)) { 669b5f6379dSBin Meng return true; 670b5f6379dSBin Meng } 671b5f6379dSBin Meng } 672a42bd001SFrank Chang break; 673c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 674c472c142SFrank Chang ctrl = env->tdata1[i]; 675c472c142SFrank Chang pc = env->tdata2[i]; 676c472c142SFrank Chang 677c472c142SFrank Chang if ((ctrl & TYPE6_EXEC) && (bp->pc == pc)) { 678c472c142SFrank Chang if (riscv_cpu_virt_enabled(env)) { 679c472c142SFrank Chang /* check VU/VS bit against current privilege level */ 680c472c142SFrank Chang if ((ctrl >> 23) & BIT(env->priv)) { 681c472c142SFrank Chang return true; 682c472c142SFrank Chang } 683c472c142SFrank Chang } else { 684c472c142SFrank Chang /* check U/S/M bit against current privilege level */ 685c472c142SFrank Chang if ((ctrl >> 3) & BIT(env->priv)) { 686c472c142SFrank Chang return true; 687c472c142SFrank Chang } 688c472c142SFrank Chang } 689c472c142SFrank Chang } 690c472c142SFrank Chang break; 691a42bd001SFrank Chang default: 692a42bd001SFrank Chang /* other trigger types are not supported or irrelevant */ 693a42bd001SFrank Chang break; 694a42bd001SFrank Chang } 695b5f6379dSBin Meng } 696b5f6379dSBin Meng } 697b5f6379dSBin Meng 698b5f6379dSBin Meng return false; 699b5f6379dSBin Meng } 700b5f6379dSBin Meng 701b5f6379dSBin Meng bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp) 702b5f6379dSBin Meng { 703b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 704b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 705b5f6379dSBin Meng target_ulong ctrl; 706b5f6379dSBin Meng target_ulong addr; 707a42bd001SFrank Chang int trigger_type; 708b5f6379dSBin Meng int flags; 709b5f6379dSBin Meng int i; 710b5f6379dSBin Meng 711a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 712a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 713a42bd001SFrank Chang 714a42bd001SFrank Chang switch (trigger_type) { 715a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 716c32461d8SFrank Chang /* type 2 trigger cannot be fired in VU/VS mode */ 717c32461d8SFrank Chang if (riscv_cpu_virt_enabled(env)) { 718c32461d8SFrank Chang return false; 719c32461d8SFrank Chang } 720c32461d8SFrank Chang 7219495c488SFrank Chang ctrl = env->tdata1[i]; 7229495c488SFrank Chang addr = env->tdata2[i]; 723b5f6379dSBin Meng flags = 0; 724b5f6379dSBin Meng 725b5f6379dSBin Meng if (ctrl & TYPE2_LOAD) { 726b5f6379dSBin Meng flags |= BP_MEM_READ; 727b5f6379dSBin Meng } 728b5f6379dSBin Meng if (ctrl & TYPE2_STORE) { 729b5f6379dSBin Meng flags |= BP_MEM_WRITE; 730b5f6379dSBin Meng } 731b5f6379dSBin Meng 732b5f6379dSBin Meng if ((wp->flags & flags) && (wp->vaddr == addr)) { 733b5f6379dSBin Meng /* check U/S/M bit against current privilege level */ 734b5f6379dSBin Meng if ((ctrl >> 3) & BIT(env->priv)) { 735b5f6379dSBin Meng return true; 736b5f6379dSBin Meng } 737b5f6379dSBin Meng } 738a42bd001SFrank Chang break; 739c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 740c472c142SFrank Chang ctrl = env->tdata1[i]; 741c472c142SFrank Chang addr = env->tdata2[i]; 742c472c142SFrank Chang flags = 0; 743c472c142SFrank Chang 744c472c142SFrank Chang if (ctrl & TYPE6_LOAD) { 745c472c142SFrank Chang flags |= BP_MEM_READ; 746c472c142SFrank Chang } 747c472c142SFrank Chang if (ctrl & TYPE6_STORE) { 748c472c142SFrank Chang flags |= BP_MEM_WRITE; 749c472c142SFrank Chang } 750c472c142SFrank Chang 751c472c142SFrank Chang if ((wp->flags & flags) && (wp->vaddr == addr)) { 752c472c142SFrank Chang if (riscv_cpu_virt_enabled(env)) { 753c472c142SFrank Chang /* check VU/VS bit against current privilege level */ 754c472c142SFrank Chang if ((ctrl >> 23) & BIT(env->priv)) { 755c472c142SFrank Chang return true; 756c472c142SFrank Chang } 757c472c142SFrank Chang } else { 758c472c142SFrank Chang /* check U/S/M bit against current privilege level */ 759c472c142SFrank Chang if ((ctrl >> 3) & BIT(env->priv)) { 760c472c142SFrank Chang return true; 761c472c142SFrank Chang } 762c472c142SFrank Chang } 763c472c142SFrank Chang } 764c472c142SFrank Chang break; 765a42bd001SFrank Chang default: 766a42bd001SFrank Chang /* other trigger types are not supported */ 767a42bd001SFrank Chang break; 768a42bd001SFrank Chang } 769b5f6379dSBin Meng } 770b5f6379dSBin Meng 771b5f6379dSBin Meng return false; 772b5f6379dSBin Meng } 773b6092544SBin Meng 774b6092544SBin Meng void riscv_trigger_init(CPURISCVState *env) 775b6092544SBin Meng { 7769d5a84dbSFrank Chang target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_AD_MATCH, 0, 0); 777b6092544SBin Meng int i; 778b6092544SBin Meng 779a42bd001SFrank Chang /* init to type 2 triggers */ 780a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 781b6092544SBin Meng /* 782b6092544SBin Meng * type = TRIGGER_TYPE_AD_MATCH 783b6092544SBin Meng * dmode = 0 (both debug and M-mode can write tdata) 784b6092544SBin Meng * maskmax = 0 (unimplemented, always 0) 785b6092544SBin Meng * sizehi = 0 (match against any size, RV64 only) 786b6092544SBin Meng * hit = 0 (unimplemented, always 0) 787b6092544SBin Meng * select = 0 (always 0, perform match on address) 788b6092544SBin Meng * timing = 0 (always 0, trigger before instruction) 789b6092544SBin Meng * sizelo = 0 (match against any size) 790b6092544SBin Meng * action = 0 (always 0, raise a breakpoint exception) 791b6092544SBin Meng * chain = 0 (unimplemented, always 0) 792b6092544SBin Meng * match = 0 (always 0, when any compare value equals tdata2) 793b6092544SBin Meng */ 7949495c488SFrank Chang env->tdata1[i] = tdata1; 7959495c488SFrank Chang env->tdata2[i] = 0; 7969495c488SFrank Chang env->tdata3[i] = 0; 7979495c488SFrank Chang env->cpu_breakpoint[i] = NULL; 7989495c488SFrank Chang env->cpu_watchpoint[i] = NULL; 799b6092544SBin Meng } 800b6092544SBin Meng } 801