Page 1 of 1

need help with getting uptime in a 32-bit kernel

Posted: Fri Mar 06, 2026 12:55 pm
by orcapet3910YT
Hi, I've been developing an OS kernel for a while now (which as of now is x86-32 only) and I've been wondering what I can do to get (count) the uptime - I don't know that much about timers yet because I'm still quite new to OS dev.
I also couldn't find a good example or explanation about timer hardware and the good OS kernel repos I looked at (like old Linux) used stuff like a function calling a function calling a function to finally get the uptime in a data type I don't even know.
The kernel doesn't really have multi-core features either (obviously).
Does anyone know how to get something like that done or at least a good source explaining this?

Re: need help with getting uptime in a 32-bit kernel

Posted: Fri Mar 06, 2026 1:15 pm
by Octocontrabass
Pick one of the time sources available to you (PIT, RTC, TSC, HPET, APIC, ACPI...). Figure out whether it runs at a constant frequency, and if so, what frequency it runs at. For some timers, you may want or need to program the timer to generate a periodic interrupt, and your interrupt handler can increment a counter to keep track of how many interrupts the timer has generated.

When you want to check the uptime, you either read the timer directly or read the interrupt counter, then do some math to convert the value to whatever unit of time you prefer.

Re: need help with getting uptime in a 32-bit kernel

Posted: Fri Mar 06, 2026 2:52 pm
by orcapet3910YT
Octocontrabass wrote: Fri Mar 06, 2026 1:15 pm Pick one of the time sources available to you (PIT, RTC, TSC, HPET, APIC, ACPI...). Figure out whether it runs at a constant frequency, and if so, what frequency it runs at. For some timers, you may want or need to program the timer to generate a periodic interrupt, and your interrupt handler can increment a counter to keep track of how many interrupts the timer has generated.

When you want to check the uptime, you either read the timer directly or read the interrupt counter, then do some math to convert the value to whatever unit of time you prefer.
Yeah, but the problem is I haven't figured out interrupts yet.

Re: need help with getting uptime in a 32-bit kernel

Posted: Fri Mar 06, 2026 3:22 pm
by Octocontrabass
orcapet3910YT wrote: Fri Mar 06, 2026 2:52 pmYeah, but the problem is I haven't figured out interrupts yet.
Perhaps you should start with interrupts and worry about getting uptime later.

Re: need help with getting uptime in a 32-bit kernel

Posted: Sat Mar 07, 2026 6:50 am
by orcapet3910YT
Okay, will do soon. Just implemented `printf`.

Re: need help with getting uptime in a 32-bit kernel

Posted: Mon Mar 09, 2026 3:00 pm
by orcapet3910YT
Update 2 days later.
Holy dang, I just got interrupts to work and fire. I'm in shock.