From aec200a3b8b4d5aabcc945536f9c872e7b4e4996 Mon Sep 17 00:00:00 2001
From: Ian Moffett <ian@osmora.org>
Date: Sat, 8 Jun 2024 18:05:18 -0400
Subject: kernel/amd64: lapic: Serialize lapic_timer_init()

Signed-off-by: Ian Moffett <ian@osmora.org>
---
 sys/arch/amd64/amd64/lapic.c | 6 ++++++
 1 file changed, 6 insertions(+)

(limited to 'sys/arch')

diff --git a/sys/arch/amd64/amd64/lapic.c b/sys/arch/amd64/amd64/lapic.c
index 3a87b74..7b3bd98 100644
--- a/sys/arch/amd64/amd64/lapic.c
+++ b/sys/arch/amd64/amd64/lapic.c
@@ -32,6 +32,7 @@
 #include <sys/panic.h>
 #include <sys/mmio.h>
 #include <sys/syslog.h>
+#include <sys/spinlock.h>
 #include <dev/timer.h>
 #include <machine/intr.h>
 #include <machine/isa/i8254.h>
@@ -262,6 +263,9 @@ lapic_timer_init(void)
     uint16_t ticks_start, ticks_end;
     size_t ticks_total, freq;
     const uint16_t MAX_SAMPLES = 0xFFFF;
+    static struct spinlock lock = {0};
+
+    spinlock_acquire(&lock);
 
     lapic_timer_stop();
     i8254_set_reload(MAX_SAMPLES);
@@ -275,6 +279,8 @@ lapic_timer_init(void)
 
     freq = (MAX_SAMPLES / ticks_total) * I8254_DIVIDEND;
     lapic_timer_stop();
+
+    spinlock_release(&lock);
     return freq;
 }
 
-- 
cgit v1.2.3