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 4031b9798dSFrank Chang * - tinfo 4195799e36SBin Meng * 42c472c142SFrank Chang * The following triggers are initialized by default: 4395799e36SBin Meng * 4495799e36SBin Meng * Index | Type | tdata mapping | Description 4595799e36SBin Meng * ------+------+------------------------+------------ 4695799e36SBin Meng * 0 | 2 | tdata1, tdata2 | Address / Data Match 4795799e36SBin Meng * 1 | 2 | tdata1, tdata2 | Address / Data Match 4895799e36SBin Meng */ 4995799e36SBin Meng 5095799e36SBin Meng /* tdata availability of a trigger */ 5195799e36SBin Meng typedef bool tdata_avail[TDATA_NUM]; 5295799e36SBin Meng 53a42bd001SFrank Chang static tdata_avail tdata_mapping[TRIGGER_TYPE_NUM] = { 54a42bd001SFrank Chang [TRIGGER_TYPE_NO_EXIST] = { false, false, false }, 55a42bd001SFrank Chang [TRIGGER_TYPE_AD_MATCH] = { true, true, true }, 56a42bd001SFrank Chang [TRIGGER_TYPE_INST_CNT] = { true, false, true }, 57a42bd001SFrank Chang [TRIGGER_TYPE_INT] = { true, true, true }, 58a42bd001SFrank Chang [TRIGGER_TYPE_EXCP] = { true, true, true }, 59a42bd001SFrank Chang [TRIGGER_TYPE_AD_MATCH6] = { true, true, true }, 60a42bd001SFrank Chang [TRIGGER_TYPE_EXT_SRC] = { true, false, false }, 61a42bd001SFrank Chang [TRIGGER_TYPE_UNAVAIL] = { true, true, true } 6295799e36SBin Meng }; 6395799e36SBin Meng 6495799e36SBin Meng /* only breakpoint size 1/2/4/8 supported */ 6595799e36SBin Meng static int access_size[SIZE_NUM] = { 6695799e36SBin Meng [SIZE_ANY] = 0, 6795799e36SBin Meng [SIZE_1B] = 1, 6895799e36SBin Meng [SIZE_2B] = 2, 6995799e36SBin Meng [SIZE_4B] = 4, 7095799e36SBin Meng [SIZE_6B] = -1, 7195799e36SBin Meng [SIZE_8B] = 8, 7295799e36SBin Meng [6 ... 15] = -1, 7395799e36SBin Meng }; 7495799e36SBin Meng 75a42bd001SFrank Chang static inline target_ulong extract_trigger_type(CPURISCVState *env, 76a42bd001SFrank Chang target_ulong tdata1) 77a42bd001SFrank Chang { 78a42bd001SFrank Chang switch (riscv_cpu_mxl(env)) { 79a42bd001SFrank Chang case MXL_RV32: 80a42bd001SFrank Chang return extract32(tdata1, 28, 4); 81a42bd001SFrank Chang case MXL_RV64: 82a42bd001SFrank Chang case MXL_RV128: 83a42bd001SFrank Chang return extract64(tdata1, 60, 4); 84a42bd001SFrank Chang default: 85a42bd001SFrank Chang g_assert_not_reached(); 86a42bd001SFrank Chang } 87a42bd001SFrank Chang } 88a42bd001SFrank Chang 89a42bd001SFrank Chang static inline target_ulong get_trigger_type(CPURISCVState *env, 90a42bd001SFrank Chang target_ulong trigger_index) 91a42bd001SFrank Chang { 929495c488SFrank Chang return extract_trigger_type(env, env->tdata1[trigger_index]); 93a42bd001SFrank Chang } 94a42bd001SFrank Chang 95d1c11141SFrank Chang static trigger_action_t get_trigger_action(CPURISCVState *env, 96d1c11141SFrank Chang target_ulong trigger_index) 97d1c11141SFrank Chang { 98d1c11141SFrank Chang target_ulong tdata1 = env->tdata1[trigger_index]; 99d1c11141SFrank Chang int trigger_type = get_trigger_type(env, trigger_index); 100d1c11141SFrank Chang trigger_action_t action = DBG_ACTION_NONE; 101d1c11141SFrank Chang 102d1c11141SFrank Chang switch (trigger_type) { 103d1c11141SFrank Chang case TRIGGER_TYPE_AD_MATCH: 104d1c11141SFrank Chang action = (tdata1 & TYPE2_ACTION) >> 12; 105d1c11141SFrank Chang break; 106c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 107c472c142SFrank Chang action = (tdata1 & TYPE6_ACTION) >> 12; 108c472c142SFrank Chang break; 109d1c11141SFrank Chang case TRIGGER_TYPE_INST_CNT: 110d1c11141SFrank Chang case TRIGGER_TYPE_INT: 111d1c11141SFrank Chang case TRIGGER_TYPE_EXCP: 112d1c11141SFrank Chang case TRIGGER_TYPE_EXT_SRC: 113d1c11141SFrank Chang qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n", 114d1c11141SFrank Chang trigger_type); 115d1c11141SFrank Chang break; 116d1c11141SFrank Chang case TRIGGER_TYPE_NO_EXIST: 117d1c11141SFrank Chang case TRIGGER_TYPE_UNAVAIL: 118d1c11141SFrank Chang qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n", 119d1c11141SFrank Chang trigger_type); 120d1c11141SFrank Chang break; 121d1c11141SFrank Chang default: 122d1c11141SFrank Chang g_assert_not_reached(); 123d1c11141SFrank Chang } 124d1c11141SFrank Chang 125d1c11141SFrank Chang return action; 126d1c11141SFrank Chang } 127d1c11141SFrank Chang 1289d5a84dbSFrank Chang static inline target_ulong build_tdata1(CPURISCVState *env, 1299d5a84dbSFrank Chang trigger_type_t type, 1309d5a84dbSFrank Chang bool dmode, target_ulong data) 13195799e36SBin Meng { 13295799e36SBin Meng target_ulong tdata1; 13395799e36SBin Meng 13495799e36SBin Meng switch (riscv_cpu_mxl(env)) { 13595799e36SBin Meng case MXL_RV32: 1369d5a84dbSFrank Chang tdata1 = RV32_TYPE(type) | 1379d5a84dbSFrank Chang (dmode ? RV32_DMODE : 0) | 1389d5a84dbSFrank Chang (data & RV32_DATA_MASK); 13995799e36SBin Meng break; 14095799e36SBin Meng case MXL_RV64: 141d1d85412SFrédéric Pétrot case MXL_RV128: 1429d5a84dbSFrank Chang tdata1 = RV64_TYPE(type) | 1439d5a84dbSFrank Chang (dmode ? RV64_DMODE : 0) | 1449d5a84dbSFrank Chang (data & RV64_DATA_MASK); 14595799e36SBin Meng break; 14695799e36SBin Meng default: 14795799e36SBin Meng g_assert_not_reached(); 14895799e36SBin Meng } 14995799e36SBin Meng 15095799e36SBin Meng return tdata1; 15195799e36SBin Meng } 15295799e36SBin Meng 15395799e36SBin Meng bool tdata_available(CPURISCVState *env, int tdata_index) 15495799e36SBin Meng { 155a42bd001SFrank Chang int trigger_type = get_trigger_type(env, env->trigger_cur); 156a42bd001SFrank Chang 15795799e36SBin Meng if (unlikely(tdata_index >= TDATA_NUM)) { 15895799e36SBin Meng return false; 15995799e36SBin Meng } 16095799e36SBin Meng 161a42bd001SFrank Chang return tdata_mapping[trigger_type][tdata_index]; 16295799e36SBin Meng } 16395799e36SBin Meng 16495799e36SBin Meng target_ulong tselect_csr_read(CPURISCVState *env) 16595799e36SBin Meng { 16695799e36SBin Meng return env->trigger_cur; 16795799e36SBin Meng } 16895799e36SBin Meng 16995799e36SBin Meng void tselect_csr_write(CPURISCVState *env, target_ulong val) 17095799e36SBin Meng { 1716ea8d3fcSFrank Chang if (val < RV_MAX_TRIGGERS) { 17295799e36SBin Meng env->trigger_cur = val; 17395799e36SBin Meng } 1746ea8d3fcSFrank Chang } 17595799e36SBin Meng 17695799e36SBin Meng static target_ulong tdata1_validate(CPURISCVState *env, target_ulong val, 17795799e36SBin Meng trigger_type_t t) 17895799e36SBin Meng { 17995799e36SBin Meng uint32_t type, dmode; 18095799e36SBin Meng target_ulong tdata1; 18195799e36SBin Meng 18295799e36SBin Meng switch (riscv_cpu_mxl(env)) { 18395799e36SBin Meng case MXL_RV32: 18495799e36SBin Meng type = extract32(val, 28, 4); 18595799e36SBin Meng dmode = extract32(val, 27, 1); 18695799e36SBin Meng tdata1 = RV32_TYPE(t); 18795799e36SBin Meng break; 18895799e36SBin Meng case MXL_RV64: 189d1d85412SFrédéric Pétrot case MXL_RV128: 19095799e36SBin Meng type = extract64(val, 60, 4); 19195799e36SBin Meng dmode = extract64(val, 59, 1); 19295799e36SBin Meng tdata1 = RV64_TYPE(t); 19395799e36SBin Meng break; 19495799e36SBin Meng default: 19595799e36SBin Meng g_assert_not_reached(); 19695799e36SBin Meng } 19795799e36SBin Meng 19895799e36SBin Meng if (type != t) { 19995799e36SBin Meng qemu_log_mask(LOG_GUEST_ERROR, 20095799e36SBin Meng "ignoring type write to tdata1 register\n"); 20195799e36SBin Meng } 202a42bd001SFrank Chang 20395799e36SBin Meng if (dmode != 0) { 20495799e36SBin Meng qemu_log_mask(LOG_UNIMP, "debug mode is not supported\n"); 20595799e36SBin Meng } 20695799e36SBin Meng 20795799e36SBin Meng return tdata1; 20895799e36SBin Meng } 20995799e36SBin Meng 21095799e36SBin Meng static inline void warn_always_zero_bit(target_ulong val, target_ulong mask, 21195799e36SBin Meng const char *msg) 21295799e36SBin Meng { 21395799e36SBin Meng if (val & mask) { 21495799e36SBin Meng qemu_log_mask(LOG_UNIMP, "%s bit is always zero\n", msg); 21595799e36SBin Meng } 21695799e36SBin Meng } 21795799e36SBin Meng 218d1c11141SFrank Chang static void do_trigger_action(CPURISCVState *env, target_ulong trigger_index) 219d1c11141SFrank Chang { 220d1c11141SFrank Chang trigger_action_t action = get_trigger_action(env, trigger_index); 221d1c11141SFrank Chang 222d1c11141SFrank Chang switch (action) { 223d1c11141SFrank Chang case DBG_ACTION_NONE: 224d1c11141SFrank Chang break; 225d1c11141SFrank Chang case DBG_ACTION_BP: 226d1c11141SFrank Chang riscv_raise_exception(env, RISCV_EXCP_BREAKPOINT, 0); 227d1c11141SFrank Chang break; 228d1c11141SFrank Chang case DBG_ACTION_DBG_MODE: 229d1c11141SFrank Chang case DBG_ACTION_TRACE0: 230d1c11141SFrank Chang case DBG_ACTION_TRACE1: 231d1c11141SFrank Chang case DBG_ACTION_TRACE2: 232d1c11141SFrank Chang case DBG_ACTION_TRACE3: 233d1c11141SFrank Chang case DBG_ACTION_EXT_DBG0: 234d1c11141SFrank Chang case DBG_ACTION_EXT_DBG1: 235d1c11141SFrank Chang qemu_log_mask(LOG_UNIMP, "action: %d is not supported\n", action); 236d1c11141SFrank Chang break; 237d1c11141SFrank Chang default: 238d1c11141SFrank Chang g_assert_not_reached(); 239d1c11141SFrank Chang } 240d1c11141SFrank Chang } 241d1c11141SFrank Chang 2429495c488SFrank Chang /* type 2 trigger */ 2439495c488SFrank Chang 24495799e36SBin Meng static uint32_t type2_breakpoint_size(CPURISCVState *env, target_ulong ctrl) 24595799e36SBin Meng { 246*66997c42SMarkus Armbruster uint32_t sizelo, sizehi = 0; 24795799e36SBin Meng 24895799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 24995799e36SBin Meng sizehi = extract32(ctrl, 21, 2); 25095799e36SBin Meng } 25195799e36SBin Meng sizelo = extract32(ctrl, 16, 2); 252*66997c42SMarkus Armbruster return (sizehi << 2) | sizelo; 25395799e36SBin Meng } 25495799e36SBin Meng 25595799e36SBin Meng static inline bool type2_breakpoint_enabled(target_ulong ctrl) 25695799e36SBin Meng { 25795799e36SBin Meng bool mode = !!(ctrl & (TYPE2_U | TYPE2_S | TYPE2_M)); 25895799e36SBin Meng bool rwx = !!(ctrl & (TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 25995799e36SBin Meng 26095799e36SBin Meng return mode && rwx; 26195799e36SBin Meng } 26295799e36SBin Meng 26395799e36SBin Meng static target_ulong type2_mcontrol_validate(CPURISCVState *env, 26495799e36SBin Meng target_ulong ctrl) 26595799e36SBin Meng { 26695799e36SBin Meng target_ulong val; 26795799e36SBin Meng uint32_t size; 26895799e36SBin Meng 26995799e36SBin Meng /* validate the generic part first */ 27095799e36SBin Meng val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH); 27195799e36SBin Meng 27295799e36SBin Meng /* validate unimplemented (always zero) bits */ 27395799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_MATCH, "match"); 27495799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_CHAIN, "chain"); 27595799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_ACTION, "action"); 27695799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_TIMING, "timing"); 27795799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_SELECT, "select"); 27895799e36SBin Meng warn_always_zero_bit(ctrl, TYPE2_HIT, "hit"); 27995799e36SBin Meng 28095799e36SBin Meng /* validate size encoding */ 28195799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 28295799e36SBin Meng if (access_size[size] == -1) { 28395799e36SBin Meng qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using SIZE_ANY\n", 28495799e36SBin Meng size); 28595799e36SBin Meng } else { 28695799e36SBin Meng val |= (ctrl & TYPE2_SIZELO); 28795799e36SBin Meng if (riscv_cpu_mxl(env) == MXL_RV64) { 28895799e36SBin Meng val |= (ctrl & TYPE2_SIZEHI); 28995799e36SBin Meng } 29095799e36SBin Meng } 29195799e36SBin Meng 29295799e36SBin Meng /* keep the mode and attribute bits */ 29395799e36SBin Meng val |= (ctrl & (TYPE2_U | TYPE2_S | TYPE2_M | 29495799e36SBin Meng TYPE2_LOAD | TYPE2_STORE | TYPE2_EXEC)); 29595799e36SBin Meng 29695799e36SBin Meng return val; 29795799e36SBin Meng } 29895799e36SBin Meng 29995799e36SBin Meng static void type2_breakpoint_insert(CPURISCVState *env, target_ulong index) 30095799e36SBin Meng { 3019495c488SFrank Chang target_ulong ctrl = env->tdata1[index]; 3029495c488SFrank Chang target_ulong addr = env->tdata2[index]; 30395799e36SBin Meng bool enabled = type2_breakpoint_enabled(ctrl); 30495799e36SBin Meng CPUState *cs = env_cpu(env); 30595799e36SBin Meng int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 30695799e36SBin Meng uint32_t size; 30795799e36SBin Meng 30895799e36SBin Meng if (!enabled) { 30995799e36SBin Meng return; 31095799e36SBin Meng } 31195799e36SBin Meng 31295799e36SBin Meng if (ctrl & TYPE2_EXEC) { 3139495c488SFrank Chang cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]); 31495799e36SBin Meng } 31595799e36SBin Meng 31695799e36SBin Meng if (ctrl & TYPE2_LOAD) { 31795799e36SBin Meng flags |= BP_MEM_READ; 31895799e36SBin Meng } 31995799e36SBin Meng if (ctrl & TYPE2_STORE) { 32095799e36SBin Meng flags |= BP_MEM_WRITE; 32195799e36SBin Meng } 32295799e36SBin Meng 32395799e36SBin Meng if (flags & BP_MEM_ACCESS) { 32495799e36SBin Meng size = type2_breakpoint_size(env, ctrl); 32595799e36SBin Meng if (size != 0) { 32695799e36SBin Meng cpu_watchpoint_insert(cs, addr, size, flags, 3279495c488SFrank Chang &env->cpu_watchpoint[index]); 32895799e36SBin Meng } else { 32995799e36SBin Meng cpu_watchpoint_insert(cs, addr, 8, flags, 3309495c488SFrank Chang &env->cpu_watchpoint[index]); 33195799e36SBin Meng } 33295799e36SBin Meng } 33395799e36SBin Meng } 33495799e36SBin Meng 33595799e36SBin Meng static void type2_breakpoint_remove(CPURISCVState *env, target_ulong index) 33695799e36SBin Meng { 33795799e36SBin Meng CPUState *cs = env_cpu(env); 33895799e36SBin Meng 3399495c488SFrank Chang if (env->cpu_breakpoint[index]) { 3409495c488SFrank Chang cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]); 3419495c488SFrank Chang env->cpu_breakpoint[index] = NULL; 34295799e36SBin Meng } 34395799e36SBin Meng 3449495c488SFrank Chang if (env->cpu_watchpoint[index]) { 3459495c488SFrank Chang cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]); 3469495c488SFrank Chang env->cpu_watchpoint[index] = NULL; 34795799e36SBin Meng } 34895799e36SBin Meng } 34995799e36SBin Meng 350a42bd001SFrank Chang static void type2_reg_write(CPURISCVState *env, target_ulong index, 35195799e36SBin Meng int tdata_index, target_ulong val) 35295799e36SBin Meng { 35395799e36SBin Meng target_ulong new_val; 35495799e36SBin Meng 35595799e36SBin Meng switch (tdata_index) { 35695799e36SBin Meng case TDATA1: 35795799e36SBin Meng new_val = type2_mcontrol_validate(env, val); 3589495c488SFrank Chang if (new_val != env->tdata1[index]) { 3599495c488SFrank Chang env->tdata1[index] = new_val; 36095799e36SBin Meng type2_breakpoint_remove(env, index); 36195799e36SBin Meng type2_breakpoint_insert(env, index); 36295799e36SBin Meng } 36395799e36SBin Meng break; 36495799e36SBin Meng case TDATA2: 3659495c488SFrank Chang if (val != env->tdata2[index]) { 3669495c488SFrank Chang env->tdata2[index] = val; 36795799e36SBin Meng type2_breakpoint_remove(env, index); 36895799e36SBin Meng type2_breakpoint_insert(env, index); 36995799e36SBin Meng } 37095799e36SBin Meng break; 3719495c488SFrank Chang case TDATA3: 3729495c488SFrank Chang qemu_log_mask(LOG_UNIMP, 3739495c488SFrank Chang "tdata3 is not supported for type 2 trigger\n"); 3749495c488SFrank Chang break; 37595799e36SBin Meng default: 37695799e36SBin Meng g_assert_not_reached(); 37795799e36SBin Meng } 37895799e36SBin Meng 37995799e36SBin Meng return; 38095799e36SBin Meng } 38195799e36SBin Meng 382c472c142SFrank Chang /* type 6 trigger */ 383c472c142SFrank Chang 384c472c142SFrank Chang static inline bool type6_breakpoint_enabled(target_ulong ctrl) 385c472c142SFrank Chang { 386c472c142SFrank Chang bool mode = !!(ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M)); 387c472c142SFrank Chang bool rwx = !!(ctrl & (TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC)); 388c472c142SFrank Chang 389c472c142SFrank Chang return mode && rwx; 390c472c142SFrank Chang } 391c472c142SFrank Chang 392c472c142SFrank Chang static target_ulong type6_mcontrol6_validate(CPURISCVState *env, 393c472c142SFrank Chang target_ulong ctrl) 394c472c142SFrank Chang { 395c472c142SFrank Chang target_ulong val; 396c472c142SFrank Chang uint32_t size; 397c472c142SFrank Chang 398c472c142SFrank Chang /* validate the generic part first */ 399c472c142SFrank Chang val = tdata1_validate(env, ctrl, TRIGGER_TYPE_AD_MATCH6); 400c472c142SFrank Chang 401c472c142SFrank Chang /* validate unimplemented (always zero) bits */ 402c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_MATCH, "match"); 403c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_CHAIN, "chain"); 404c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_ACTION, "action"); 405c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_TIMING, "timing"); 406c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_SELECT, "select"); 407c472c142SFrank Chang warn_always_zero_bit(ctrl, TYPE6_HIT, "hit"); 408c472c142SFrank Chang 409c472c142SFrank Chang /* validate size encoding */ 410c472c142SFrank Chang size = extract32(ctrl, 16, 4); 411c472c142SFrank Chang if (access_size[size] == -1) { 412c472c142SFrank Chang qemu_log_mask(LOG_UNIMP, "access size %d is not supported, using SIZE_ANY\n", 413c472c142SFrank Chang size); 414c472c142SFrank Chang } else { 415c472c142SFrank Chang val |= (ctrl & TYPE6_SIZE); 416c472c142SFrank Chang } 417c472c142SFrank Chang 418c472c142SFrank Chang /* keep the mode and attribute bits */ 419c472c142SFrank Chang val |= (ctrl & (TYPE6_VU | TYPE6_VS | TYPE6_U | TYPE6_S | TYPE6_M | 420c472c142SFrank Chang TYPE6_LOAD | TYPE6_STORE | TYPE6_EXEC)); 421c472c142SFrank Chang 422c472c142SFrank Chang return val; 423c472c142SFrank Chang } 424c472c142SFrank Chang 425c472c142SFrank Chang static void type6_breakpoint_insert(CPURISCVState *env, target_ulong index) 426c472c142SFrank Chang { 427c472c142SFrank Chang target_ulong ctrl = env->tdata1[index]; 428c472c142SFrank Chang target_ulong addr = env->tdata2[index]; 429c472c142SFrank Chang bool enabled = type6_breakpoint_enabled(ctrl); 430c472c142SFrank Chang CPUState *cs = env_cpu(env); 431c472c142SFrank Chang int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 432c472c142SFrank Chang uint32_t size; 433c472c142SFrank Chang 434c472c142SFrank Chang if (!enabled) { 435c472c142SFrank Chang return; 436c472c142SFrank Chang } 437c472c142SFrank Chang 438c472c142SFrank Chang if (ctrl & TYPE6_EXEC) { 439c472c142SFrank Chang cpu_breakpoint_insert(cs, addr, flags, &env->cpu_breakpoint[index]); 440c472c142SFrank Chang } 441c472c142SFrank Chang 442c472c142SFrank Chang if (ctrl & TYPE6_LOAD) { 443c472c142SFrank Chang flags |= BP_MEM_READ; 444c472c142SFrank Chang } 445c472c142SFrank Chang 446c472c142SFrank Chang if (ctrl & TYPE6_STORE) { 447c472c142SFrank Chang flags |= BP_MEM_WRITE; 448c472c142SFrank Chang } 449c472c142SFrank Chang 450c472c142SFrank Chang if (flags & BP_MEM_ACCESS) { 451c472c142SFrank Chang size = extract32(ctrl, 16, 4); 452c472c142SFrank Chang if (size != 0) { 453c472c142SFrank Chang cpu_watchpoint_insert(cs, addr, size, flags, 454c472c142SFrank Chang &env->cpu_watchpoint[index]); 455c472c142SFrank Chang } else { 456c472c142SFrank Chang cpu_watchpoint_insert(cs, addr, 8, flags, 457c472c142SFrank Chang &env->cpu_watchpoint[index]); 458c472c142SFrank Chang } 459c472c142SFrank Chang } 460c472c142SFrank Chang } 461c472c142SFrank Chang 462c472c142SFrank Chang static void type6_breakpoint_remove(CPURISCVState *env, target_ulong index) 463c472c142SFrank Chang { 464c472c142SFrank Chang type2_breakpoint_remove(env, index); 465c472c142SFrank Chang } 466c472c142SFrank Chang 467c472c142SFrank Chang static void type6_reg_write(CPURISCVState *env, target_ulong index, 468c472c142SFrank Chang int tdata_index, target_ulong val) 469c472c142SFrank Chang { 470c472c142SFrank Chang target_ulong new_val; 471c472c142SFrank Chang 472c472c142SFrank Chang switch (tdata_index) { 473c472c142SFrank Chang case TDATA1: 474c472c142SFrank Chang new_val = type6_mcontrol6_validate(env, val); 475c472c142SFrank Chang if (new_val != env->tdata1[index]) { 476c472c142SFrank Chang env->tdata1[index] = new_val; 477c472c142SFrank Chang type6_breakpoint_remove(env, index); 478c472c142SFrank Chang type6_breakpoint_insert(env, index); 479c472c142SFrank Chang } 480c472c142SFrank Chang break; 481c472c142SFrank Chang case TDATA2: 482c472c142SFrank Chang if (val != env->tdata2[index]) { 483c472c142SFrank Chang env->tdata2[index] = val; 484c472c142SFrank Chang type6_breakpoint_remove(env, index); 485c472c142SFrank Chang type6_breakpoint_insert(env, index); 486c472c142SFrank Chang } 487c472c142SFrank Chang break; 488c472c142SFrank Chang case TDATA3: 489c472c142SFrank Chang qemu_log_mask(LOG_UNIMP, 490c472c142SFrank Chang "tdata3 is not supported for type 6 trigger\n"); 491c472c142SFrank Chang break; 492c472c142SFrank Chang default: 493c472c142SFrank Chang g_assert_not_reached(); 494c472c142SFrank Chang } 495c472c142SFrank Chang 496c472c142SFrank Chang return; 497c472c142SFrank Chang } 498c472c142SFrank Chang 49995799e36SBin Meng target_ulong tdata_csr_read(CPURISCVState *env, int tdata_index) 50095799e36SBin Meng { 5019495c488SFrank Chang switch (tdata_index) { 5029495c488SFrank Chang case TDATA1: 5039495c488SFrank Chang return env->tdata1[env->trigger_cur]; 5049495c488SFrank Chang case TDATA2: 5059495c488SFrank Chang return env->tdata2[env->trigger_cur]; 5069495c488SFrank Chang case TDATA3: 5079495c488SFrank Chang return env->tdata3[env->trigger_cur]; 508a42bd001SFrank Chang default: 509a42bd001SFrank Chang g_assert_not_reached(); 510a42bd001SFrank Chang } 51195799e36SBin Meng } 51295799e36SBin Meng 51395799e36SBin Meng void tdata_csr_write(CPURISCVState *env, int tdata_index, target_ulong val) 51495799e36SBin Meng { 515a42bd001SFrank Chang int trigger_type; 51695799e36SBin Meng 517a42bd001SFrank Chang if (tdata_index == TDATA1) { 518a42bd001SFrank Chang trigger_type = extract_trigger_type(env, val); 519a42bd001SFrank Chang } else { 520a42bd001SFrank Chang trigger_type = get_trigger_type(env, env->trigger_cur); 521a42bd001SFrank Chang } 522a42bd001SFrank Chang 523a42bd001SFrank Chang switch (trigger_type) { 524a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 525a42bd001SFrank Chang type2_reg_write(env, env->trigger_cur, tdata_index, val); 526a42bd001SFrank Chang break; 527c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 528c472c142SFrank Chang type6_reg_write(env, env->trigger_cur, tdata_index, val); 529c472c142SFrank Chang break; 530a42bd001SFrank Chang case TRIGGER_TYPE_INST_CNT: 531a42bd001SFrank Chang case TRIGGER_TYPE_INT: 532a42bd001SFrank Chang case TRIGGER_TYPE_EXCP: 533a42bd001SFrank Chang case TRIGGER_TYPE_EXT_SRC: 534a42bd001SFrank Chang qemu_log_mask(LOG_UNIMP, "trigger type: %d is not supported\n", 535a42bd001SFrank Chang trigger_type); 536a42bd001SFrank Chang break; 537a42bd001SFrank Chang case TRIGGER_TYPE_NO_EXIST: 538a42bd001SFrank Chang case TRIGGER_TYPE_UNAVAIL: 539a42bd001SFrank Chang qemu_log_mask(LOG_GUEST_ERROR, "trigger type: %d does not exit\n", 540a42bd001SFrank Chang trigger_type); 541a42bd001SFrank Chang break; 542a42bd001SFrank Chang default: 543a42bd001SFrank Chang g_assert_not_reached(); 544a42bd001SFrank Chang } 54595799e36SBin Meng } 546b5f6379dSBin Meng 54731b9798dSFrank Chang target_ulong tinfo_csr_read(CPURISCVState *env) 54831b9798dSFrank Chang { 54931b9798dSFrank Chang /* assume all triggers support the same types of triggers */ 550c472c142SFrank Chang return BIT(TRIGGER_TYPE_AD_MATCH) | 551c472c142SFrank Chang BIT(TRIGGER_TYPE_AD_MATCH6); 55231b9798dSFrank Chang } 55331b9798dSFrank Chang 554b5f6379dSBin Meng void riscv_cpu_debug_excp_handler(CPUState *cs) 555b5f6379dSBin Meng { 556b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 557b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 558b5f6379dSBin Meng 559b5f6379dSBin Meng if (cs->watchpoint_hit) { 560b5f6379dSBin Meng if (cs->watchpoint_hit->flags & BP_CPU) { 561b5f6379dSBin Meng cs->watchpoint_hit = NULL; 562d1c11141SFrank Chang do_trigger_action(env, DBG_ACTION_BP); 563b5f6379dSBin Meng } 564b5f6379dSBin Meng } else { 565b5f6379dSBin Meng if (cpu_breakpoint_test(cs, env->pc, BP_CPU)) { 566d1c11141SFrank Chang do_trigger_action(env, DBG_ACTION_BP); 567b5f6379dSBin Meng } 568b5f6379dSBin Meng } 569b5f6379dSBin Meng } 570b5f6379dSBin Meng 571b5f6379dSBin Meng bool riscv_cpu_debug_check_breakpoint(CPUState *cs) 572b5f6379dSBin Meng { 573b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 574b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 575b5f6379dSBin Meng CPUBreakpoint *bp; 576b5f6379dSBin Meng target_ulong ctrl; 577b5f6379dSBin Meng target_ulong pc; 578a42bd001SFrank Chang int trigger_type; 579b5f6379dSBin Meng int i; 580b5f6379dSBin Meng 581b5f6379dSBin Meng QTAILQ_FOREACH(bp, &cs->breakpoints, entry) { 582a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 583a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 584a42bd001SFrank Chang 585a42bd001SFrank Chang switch (trigger_type) { 586a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 587c32461d8SFrank Chang /* type 2 trigger cannot be fired in VU/VS mode */ 588c32461d8SFrank Chang if (riscv_cpu_virt_enabled(env)) { 589c32461d8SFrank Chang return false; 590c32461d8SFrank Chang } 591c32461d8SFrank Chang 5929495c488SFrank Chang ctrl = env->tdata1[i]; 5939495c488SFrank Chang pc = env->tdata2[i]; 594b5f6379dSBin Meng 595b5f6379dSBin Meng if ((ctrl & TYPE2_EXEC) && (bp->pc == pc)) { 596b5f6379dSBin Meng /* check U/S/M bit against current privilege level */ 597b5f6379dSBin Meng if ((ctrl >> 3) & BIT(env->priv)) { 598b5f6379dSBin Meng return true; 599b5f6379dSBin Meng } 600b5f6379dSBin Meng } 601a42bd001SFrank Chang break; 602c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 603c472c142SFrank Chang ctrl = env->tdata1[i]; 604c472c142SFrank Chang pc = env->tdata2[i]; 605c472c142SFrank Chang 606c472c142SFrank Chang if ((ctrl & TYPE6_EXEC) && (bp->pc == pc)) { 607c472c142SFrank Chang if (riscv_cpu_virt_enabled(env)) { 608c472c142SFrank Chang /* check VU/VS bit against current privilege level */ 609c472c142SFrank Chang if ((ctrl >> 23) & BIT(env->priv)) { 610c472c142SFrank Chang return true; 611c472c142SFrank Chang } 612c472c142SFrank Chang } else { 613c472c142SFrank Chang /* check U/S/M bit against current privilege level */ 614c472c142SFrank Chang if ((ctrl >> 3) & BIT(env->priv)) { 615c472c142SFrank Chang return true; 616c472c142SFrank Chang } 617c472c142SFrank Chang } 618c472c142SFrank Chang } 619c472c142SFrank Chang break; 620a42bd001SFrank Chang default: 621a42bd001SFrank Chang /* other trigger types are not supported or irrelevant */ 622a42bd001SFrank Chang break; 623a42bd001SFrank Chang } 624b5f6379dSBin Meng } 625b5f6379dSBin Meng } 626b5f6379dSBin Meng 627b5f6379dSBin Meng return false; 628b5f6379dSBin Meng } 629b5f6379dSBin Meng 630b5f6379dSBin Meng bool riscv_cpu_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp) 631b5f6379dSBin Meng { 632b5f6379dSBin Meng RISCVCPU *cpu = RISCV_CPU(cs); 633b5f6379dSBin Meng CPURISCVState *env = &cpu->env; 634b5f6379dSBin Meng target_ulong ctrl; 635b5f6379dSBin Meng target_ulong addr; 636a42bd001SFrank Chang int trigger_type; 637b5f6379dSBin Meng int flags; 638b5f6379dSBin Meng int i; 639b5f6379dSBin Meng 640a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 641a42bd001SFrank Chang trigger_type = get_trigger_type(env, i); 642a42bd001SFrank Chang 643a42bd001SFrank Chang switch (trigger_type) { 644a42bd001SFrank Chang case TRIGGER_TYPE_AD_MATCH: 645c32461d8SFrank Chang /* type 2 trigger cannot be fired in VU/VS mode */ 646c32461d8SFrank Chang if (riscv_cpu_virt_enabled(env)) { 647c32461d8SFrank Chang return false; 648c32461d8SFrank Chang } 649c32461d8SFrank Chang 6509495c488SFrank Chang ctrl = env->tdata1[i]; 6519495c488SFrank Chang addr = env->tdata2[i]; 652b5f6379dSBin Meng flags = 0; 653b5f6379dSBin Meng 654b5f6379dSBin Meng if (ctrl & TYPE2_LOAD) { 655b5f6379dSBin Meng flags |= BP_MEM_READ; 656b5f6379dSBin Meng } 657b5f6379dSBin Meng if (ctrl & TYPE2_STORE) { 658b5f6379dSBin Meng flags |= BP_MEM_WRITE; 659b5f6379dSBin Meng } 660b5f6379dSBin Meng 661b5f6379dSBin Meng if ((wp->flags & flags) && (wp->vaddr == addr)) { 662b5f6379dSBin Meng /* check U/S/M bit against current privilege level */ 663b5f6379dSBin Meng if ((ctrl >> 3) & BIT(env->priv)) { 664b5f6379dSBin Meng return true; 665b5f6379dSBin Meng } 666b5f6379dSBin Meng } 667a42bd001SFrank Chang break; 668c472c142SFrank Chang case TRIGGER_TYPE_AD_MATCH6: 669c472c142SFrank Chang ctrl = env->tdata1[i]; 670c472c142SFrank Chang addr = env->tdata2[i]; 671c472c142SFrank Chang flags = 0; 672c472c142SFrank Chang 673c472c142SFrank Chang if (ctrl & TYPE6_LOAD) { 674c472c142SFrank Chang flags |= BP_MEM_READ; 675c472c142SFrank Chang } 676c472c142SFrank Chang if (ctrl & TYPE6_STORE) { 677c472c142SFrank Chang flags |= BP_MEM_WRITE; 678c472c142SFrank Chang } 679c472c142SFrank Chang 680c472c142SFrank Chang if ((wp->flags & flags) && (wp->vaddr == addr)) { 681c472c142SFrank Chang if (riscv_cpu_virt_enabled(env)) { 682c472c142SFrank Chang /* check VU/VS bit against current privilege level */ 683c472c142SFrank Chang if ((ctrl >> 23) & BIT(env->priv)) { 684c472c142SFrank Chang return true; 685c472c142SFrank Chang } 686c472c142SFrank Chang } else { 687c472c142SFrank Chang /* check U/S/M bit against current privilege level */ 688c472c142SFrank Chang if ((ctrl >> 3) & BIT(env->priv)) { 689c472c142SFrank Chang return true; 690c472c142SFrank Chang } 691c472c142SFrank Chang } 692c472c142SFrank Chang } 693c472c142SFrank Chang break; 694a42bd001SFrank Chang default: 695a42bd001SFrank Chang /* other trigger types are not supported */ 696a42bd001SFrank Chang break; 697a42bd001SFrank Chang } 698b5f6379dSBin Meng } 699b5f6379dSBin Meng 700b5f6379dSBin Meng return false; 701b5f6379dSBin Meng } 702b6092544SBin Meng 703b6092544SBin Meng void riscv_trigger_init(CPURISCVState *env) 704b6092544SBin Meng { 7059d5a84dbSFrank Chang target_ulong tdata1 = build_tdata1(env, TRIGGER_TYPE_AD_MATCH, 0, 0); 706b6092544SBin Meng int i; 707b6092544SBin Meng 708a42bd001SFrank Chang /* init to type 2 triggers */ 709a42bd001SFrank Chang for (i = 0; i < RV_MAX_TRIGGERS; i++) { 710b6092544SBin Meng /* 711b6092544SBin Meng * type = TRIGGER_TYPE_AD_MATCH 712b6092544SBin Meng * dmode = 0 (both debug and M-mode can write tdata) 713b6092544SBin Meng * maskmax = 0 (unimplemented, always 0) 714b6092544SBin Meng * sizehi = 0 (match against any size, RV64 only) 715b6092544SBin Meng * hit = 0 (unimplemented, always 0) 716b6092544SBin Meng * select = 0 (always 0, perform match on address) 717b6092544SBin Meng * timing = 0 (always 0, trigger before instruction) 718b6092544SBin Meng * sizelo = 0 (match against any size) 719b6092544SBin Meng * action = 0 (always 0, raise a breakpoint exception) 720b6092544SBin Meng * chain = 0 (unimplemented, always 0) 721b6092544SBin Meng * match = 0 (always 0, when any compare value equals tdata2) 722b6092544SBin Meng */ 7239495c488SFrank Chang env->tdata1[i] = tdata1; 7249495c488SFrank Chang env->tdata2[i] = 0; 7259495c488SFrank Chang env->tdata3[i] = 0; 7269495c488SFrank Chang env->cpu_breakpoint[i] = NULL; 7279495c488SFrank Chang env->cpu_watchpoint[i] = NULL; 728b6092544SBin Meng } 729b6092544SBin Meng } 730