@@ -290,7 +290,6 @@ static const virt2phys_info_t sv57x4 = {
290290
291291static enum riscv_halt_reason riscv_halt_reason (struct target * target );
292292static void riscv_info_init (struct target * target , struct riscv_info * r );
293- static void riscv_invalidate_register_cache (struct target * target );
294293static int riscv_step_rtos_hart (struct target * target );
295294
296295static void riscv_sample_buf_maybe_add_timestamp (struct target * target , bool before )
@@ -2485,10 +2484,15 @@ static int riscv_halt_go_all_harts(struct target *target)
24852484 return ERROR_FAIL ;
24862485 }
24872486 } else {
2487+ // Safety check:
2488+ if (riscv_reg_cache_any_dirty (target , LOG_LVL_ERROR ))
2489+ LOG_TARGET_INFO (target , "BUG: Registers should not be dirty while "
2490+ "the target is not halted!" );
2491+
2492+ riscv_reg_cache_invalidate_all (target );
2493+
24882494 if (r -> halt_go (target ) != ERROR_OK )
24892495 return ERROR_FAIL ;
2490-
2491- riscv_invalidate_register_cache (target );
24922496 }
24932497
24942498 return ERROR_OK ;
@@ -2572,7 +2576,11 @@ static int riscv_assert_reset(struct target *target)
25722576 struct target_type * tt = get_target_type (target );
25732577 if (!tt )
25742578 return ERROR_FAIL ;
2575- riscv_invalidate_register_cache (target );
2579+
2580+ if (riscv_reg_cache_any_dirty (target , LOG_LVL_INFO ))
2581+ LOG_TARGET_INFO (target , "Discarding values of dirty registers." );
2582+
2583+ riscv_reg_cache_invalidate_all (target );
25762584 return tt -> assert_reset (target );
25772585}
25782586
@@ -2699,7 +2707,15 @@ static int resume_go(struct target *target, int current,
26992707static int resume_finish (struct target * target , int debug_execution )
27002708{
27012709 assert (target -> state == TARGET_HALTED );
2702- register_cache_invalidate (target -> reg_cache );
2710+ if (riscv_reg_cache_any_dirty (target , LOG_LVL_ERROR )) {
2711+ /* If this happens, it means there is a bug in the previous
2712+ * register-flushing algorithm: not all registers were flushed
2713+ * back to the target in preparation for the resume.*/
2714+ LOG_TARGET_ERROR (target ,
2715+ "BUG: registers should have been flushed by this point." );
2716+ }
2717+
2718+ riscv_reg_cache_invalidate_all (target );
27032719
27042720 target -> state = debug_execution ? TARGET_DEBUG_RUNNING : TARGET_RUNNING ;
27052721 target -> debug_reason = DBG_REASON_NOTHALTED ;
@@ -4001,7 +4017,15 @@ static int riscv_openocd_step_impl(struct target *target, int current,
40014017 LOG_TARGET_ERROR (target , "Unable to step rtos hart." );
40024018 }
40034019
4004- register_cache_invalidate (target -> reg_cache );
4020+ if (riscv_reg_cache_any_dirty (target , LOG_LVL_ERROR )) {
4021+ /* If this happens, it means there is a bug in the previous
4022+ * register-flushing algorithm: not all registers were flushed
4023+ * back to the target prior to single-step. */
4024+ LOG_TARGET_ERROR (target ,
4025+ "BUG: registers should have been flushed by this point." );
4026+ }
4027+
4028+ riscv_reg_cache_invalidate_all (target );
40054029
40064030 if (info -> isrmask_mode == RISCV_ISRMASK_STEPONLY )
40074031 if (riscv_interrupts_restore (target , current_mstatus ) != ERROR_OK ) {
@@ -5177,7 +5201,7 @@ COMMAND_HANDLER(riscv_exec_progbuf)
51775201 if (riscv_reg_flush_all (target ) != ERROR_OK )
51785202 return ERROR_FAIL ;
51795203 int error = riscv_program_exec (& prog , target );
5180- riscv_invalidate_register_cache (target );
5204+ riscv_reg_cache_invalidate_all (target );
51815205
51825206 if (error != ERROR_OK ) {
51835207 LOG_TARGET_ERROR (target , "exec_progbuf: Program buffer execution failed." );
@@ -5731,8 +5755,6 @@ static int riscv_resume_go_all_harts(struct target *target)
57315755 } else {
57325756 LOG_TARGET_DEBUG (target , "Hart requested resume, but was already resumed." );
57335757 }
5734-
5735- riscv_invalidate_register_cache (target );
57365758 return ERROR_OK ;
57375759}
57385760
@@ -5826,17 +5848,6 @@ unsigned int riscv_vlenb(const struct target *target)
58265848 return r -> vlenb ;
58275849}
58285850
5829- static void riscv_invalidate_register_cache (struct target * target )
5830- {
5831- /* Do not invalidate the register cache if it is not yet set up
5832- * (e.g. when the target failed to get examined). */
5833- if (!target -> reg_cache )
5834- return ;
5835-
5836- LOG_TARGET_DEBUG (target , "Invalidating register cache." );
5837- register_cache_invalidate (target -> reg_cache );
5838- }
5839-
58405851int riscv_get_hart_state (struct target * target , enum riscv_hart_state * state )
58415852{
58425853 RISCV_INFO (r );
0 commit comments