22; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
33; Copyright (C) 2008-2025 Return Infinity -- see LICENSE.TXT
44;
5- ; Timer Functions
5+ ; Time Functions
66; =============================================================================
77
88
9- ; Note: There are 1,000,000 microseconds in a second
10- ; There are 1,000 milliseconds in a second
9+ ; Note: 1,000,000,000,000,000 femtoseconds / second
10+ ; 1,000,000,000,000 picoseconds /second
11+ ; 1,000,000,000 nanoseconds / second
12+ ; 1,000,000 microseconds / second
13+ ; 1,000 milliseconds / second
1114
1215
1316os_timer_init:
@@ -31,13 +34,15 @@ os_timer_init_phys:
3134 jz os_timer_init_error
3235 ; Initialize the HPET
3336 call init_timer_hpet
34- mov qword [ sys_timer ], hpet_delay
37+ mov qword [ sys_time ], hpet_ns
38+ mov qword [ sys_delay ], hpet_delay
3539 jmp os_timer_init_done
3640
3741os_timer_init_kvm:
3842 ; Initialize the KVM timer
3943 call init_timer_kvm
40- mov qword [ sys_timer ], kvm_delay
44+ mov qword [ sys_time ], kvm_ns
45+ mov qword [ sys_delay ], kvm_delay
4146 jmp os_timer_init_done
4247
4348os_timer_init_error:
@@ -74,10 +79,10 @@ os_hpet_init_error:
7479
7580
7681; -----------------------------------------------------------------------------
77- ; os_hpet_us -- Get current microseconds (us ) since HPET started
82+ ; hpet_ns -- Get current nanoseconds (ns ) since HPET started
7883; IN: Nothing
7984; OUT: RAX = Time in microseconds since start
80- os_hpet_us :
85+ hpet_ns :
8186 push rdx
8287 push rcx
8388
@@ -91,8 +96,8 @@ os_hpet_us:
9196 mov ecx , [ os_HPET_Frequency ]
9297 mul rcx ; RDX:RAX *= RCX
9398
94- ; Divide by # of femtoseconds in a microsecond
95- mov rcx , 1000000000
99+ ; Divide by # of femtoseconds in a nanosecond
100+ mov rcx , 1000000
96101 div rcx ; RAX = RDX:RAX / RCX
97102
98103 pop rcx
@@ -202,7 +207,7 @@ init_timer_kvm_configure:
202207; IN: Nothing
203208; OUT: RAX = microseconds elapsed since start
204209; All other registers preserved
205- kvm_get_usec :
210+ kvm_ns :
206211 push r10
207212 push r9
208213 push rdi
@@ -211,10 +216,10 @@ kvm_get_usec:
211216 push rbx
212217
213218 mov rdi , kvm_timer
214- kvm_get_usec_wait :
219+ kvm_ns_wait :
215220 mov r10d , [ rdi ] ; Get 32-bit version
216221 test r10d , 1 ; Check if version is odd (update in progress)
217- jnz kvm_get_usec_wait ; If so, retry
222+ jnz kvm_ns_wait ; If so, retry
218223
219224 lfence
220225
@@ -237,13 +242,13 @@ kvm_get_usec_wait:
237242
238243 ; Apply tsc_shift
239244 cmp cl , 0
240- jl kvm_get_usec_shift_right ; Signed comparison
245+ jl kvm_ns_shift_right ; Signed comparison
241246 shl rax , cl
242- jmp kvm_get_usec_shift_done
243- kvm_get_usec_shift_right :
247+ jmp kvm_ns_shift_done
248+ kvm_ns_shift_right :
244249 neg cl ; Ex: 0xFF = tsc shift of -1
245250 shr rax , cl
246- kvm_get_usec_shift_done :
251+ kvm_ns_shift_done :
247252
248253 pop rcx ; Restore tsc_to_system_mul
249254
@@ -260,12 +265,7 @@ kvm_get_usec_shift_done:
260265 lfence
261266 mov ecx , [ rdi ] ; Load 32-bit version
262267 cmp r10d , ecx ; Compare to first version read
263- jne kvm_get_usec_wait ; If not equal then an update occured, restart
264-
265- ; Convert nanoseconds to microseconds
266- xor edx , edx
267- mov ecx , 1000
268- div rcx
268+ jne kvm_ns_wait ; If not equal then an update occurred, restart
269269
270270 pop rbx
271271 pop rcx
@@ -284,19 +284,27 @@ kvm_get_usec_shift_done:
284284; Note: There are 1,000,000 microseconds in a second
285285; There are 1,000 milliseconds in a second
286286kvm_delay:
287+ push rdx
288+ push rcx
287289 push rbx
288290 push rax
289291
292+ ; Multiply time by 1000 to convert to nanoseconds
293+ mov ecx , 1000
294+ mul rcx ; RDX:RAX = RAX * RCX
295+
290296 mov rbx , rax ; Store delay in RBX
291- call kvm_get_usec
297+ call kvm_ns
292298 add rbx , rax ; Add elapsed time
293299kvm_delay_wait:
294- call kvm_get_usec
300+ call kvm_ns
295301 cmp rax , rbx
296302 jb kvm_delay_wait
297303
298304 pop rax
299305 pop rbx
306+ pop rcx
307+ pop rdx
300308 ret
301309; -----------------------------------------------------------------------------
302310
@@ -310,7 +318,7 @@ kvm_delay_wait:
310318timer_delay:
311319 push rax
312320
313- call [ sys_timer ]
321+ call [ sys_delay ]
314322
315323 pop rax
316324 ret
0 commit comments