From 0641e63407d21dbf485388411f36ed803b8f32f1 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Wed, 31 Aug 2016 14:01:43 +0200 Subject: [PATCH] fix bug in the initialization routine of the timer --- hermit/arch/x86/kernel/timer.c | 42 +++++++++++++++------------------- hermit/kernel/main.c | 1 + 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/hermit/arch/x86/kernel/timer.c b/hermit/arch/x86/kernel/timer.c index 16d62c6f7..8e07fe1b4 100644 --- a/hermit/arch/x86/kernel/timer.c +++ b/hermit/arch/x86/kernel/timer.c @@ -46,6 +46,7 @@ extern int32_t boot_processor; #ifdef DYNAMIC_TICKS DEFINE_PER_CORE(uint64_t, last_rdtsc, 0); +uint64_t boot_tsc = 0; void check_ticks(void) { @@ -53,28 +54,15 @@ void check_ticks(void) if (!cpu_freq) return; - if (has_rdtscp()){ - uint64_t curr_rdtsc = rdtscp(NULL); - uint64_t diff; + uint64_t curr_rdtsc = has_rdtscp() ? rdtscp(NULL) : rdtsc(); + uint64_t diff; + rmb(); + diff = ((curr_rdtsc - per_core(last_rdtsc)) * (uint64_t)TIMER_FREQ) / (1000000ULL*(uint64_t)get_cpu_frequency()); + if (diff > 0) { + set_per_core(timer_ticks, per_core(timer_ticks) + diff); + set_per_core(last_rdtsc, curr_rdtsc); rmb(); - diff = ((curr_rdtsc - per_core(last_rdtsc)) * (uint64_t)TIMER_FREQ) / (1000000ULL*(uint64_t)get_cpu_frequency()); - if (diff > 0) { - set_per_core(timer_ticks, per_core(timer_ticks) + diff); - set_per_core(last_rdtsc, curr_rdtsc); - rmb(); - } - } else { - uint64_t curr_rdtsc = rdtsc(); - uint64_t diff; - - rmb(); - diff = ((curr_rdtsc - per_core(last_rdtsc)) * (uint64_t)TIMER_FREQ) / (1000000ULL*(uint64_t)get_cpu_frequency()); - if (diff > 0) { - set_per_core(timer_ticks, per_core(timer_ticks) + diff); - set_per_core(last_rdtsc, curr_rdtsc); - rmb(); - } } } #endif @@ -182,6 +170,14 @@ static int pit_init(void) */ int timer_init(void) { +#ifdef DYNAMIC_TICKS + if (boot_tsc) + { + set_per_core(last_rdtsc, boot_tsc); + return 0; + } +#endif + /* * Installs 'timer_handler' for the PIC and APIC timer, * only one handler will be later used. @@ -191,10 +187,8 @@ int timer_init(void) irq_install_handler(121, wakeup_handler); #ifdef DYNAMIC_TICKS - if (has_rdtscp()) - last_rdtsc = rdtscp(NULL); - else - last_rdtsc = rdtsc(); + boot_tsc = has_rdtscp() ? rdtscp(NULL) : rdtsc(); + set_per_core(last_rdtsc, boot_tsc); #endif if (cpu_freq) // do we need to configure the timer? diff --git a/hermit/kernel/main.c b/hermit/kernel/main.c index 280369f4f..2a2a52dfe 100644 --- a/hermit/kernel/main.c +++ b/hermit/kernel/main.c @@ -285,6 +285,7 @@ int network_shutdown(void) #if MAX_CORES > 1 int smp_main(void) { + timer_init(); #ifdef DYNAMIC_TICKS enable_dynticks(); #endif