From 28495108926c88557b2264d70f04c12f004e3aab Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Mon, 12 Jun 2023 09:19:07 -0600
Subject: [PATCH] Copy HPET divide by zero workaround to x86 arch

---
 src/arch/x86/time.rs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/arch/x86/time.rs b/src/arch/x86/time.rs
index 665f8ce5..d610050a 100644
--- a/src/arch/x86/time.rs
+++ b/src/arch/x86/time.rs
@@ -14,7 +14,15 @@ pub fn counter() -> u128 {
         let comparator = unsafe { hpet.base_address.read_u64(hpet::T0_COMPARATOR_OFFSET) };
         // Get period in femtoseconds
         let capability = unsafe { hpet.base_address.read_u64(hpet::CAPABILITY_OFFSET) };
-        let period_fs = capability >> 32;
+
+        // There seems to be a bug in qemu on macos that causes the calculation to produce 0 for
+        // period_fs and hence a divide by zero calculating the divisor - workaround it while we
+        // try and get a fix from qemu: https://gitlab.com/qemu-project/qemu/-/issues/1570
+        let mut period_fs = capability >> 32;
+        if period_fs == 0 {
+            period_fs = 10_000_000;
+        }
+
         // Calculate divisor
         let divisor = (pit::RATE as u64 * 1_000_000) / period_fs;
         // Calculate last interrupt
-- 
GitLab