Skip to content

Commit

Permalink
drivers/timer/arm_arch_timer: Fix cycles overflow with GDB stub
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-vedral committed May 19, 2023
1 parent 787af42 commit 0c0bedd
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion drivers/timer/arm_arch_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ static uint32_t cyc_per_tick;
/ CONFIG_SYS_CLOCK_TICKS_PER_SEC)
#endif

#if defined(CONFIG_GDBSTUB)
/* When interactively debugging, the cycle diff can overflow 32-bit variable */
#define TO_CYCLE_DIFF(x) (x)
#else
/* Convert to 32-bit for fast division */
#define TO_CYCLE_DIFF(x) ((cycle_diff_t)(x))
#endif

/* the unsigned long cast limits divisors to native CPU register width */
#define cycle_diff_t unsigned long

Expand Down Expand Up @@ -58,7 +66,7 @@ static void arm_arch_timer_compare_isr(const void *arg)

uint64_t curr_cycle = arm_arch_timer_count();
uint64_t delta_cycles = curr_cycle - last_cycle;
uint32_t delta_ticks = (cycle_diff_t)delta_cycles / CYC_PER_TICK;
uint32_t delta_ticks = TO_CYCLE_DIFF(delta_cycles) / CYC_PER_TICK;

last_cycle += (cycle_diff_t)delta_ticks * CYC_PER_TICK;
last_tick += delta_ticks;
Expand Down

0 comments on commit 0c0bedd

Please sign in to comment.