Skip to content

Commit b497c70

Browse files
author
Ian Seyler
committed
Time adjustments
1 parent 9642fc4 commit b497c70

4 files changed

Lines changed: 40 additions & 31 deletions

File tree

src/drivers/bus/xhci.asm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,14 +1486,14 @@ xhci_check_command_event:
14861486
push rsi
14871487
push rdx
14881488
push rcx
1489-
call os_hpet_us
1489+
call [sys_time]
14901490
mov rdx, rax
1491-
add rdx, 50000 ; Add 50,000 μs
1491+
add rdx, 50000000 ; Add 50,000,000 ns
14921492
mov rsi, os_usb_ERS ; Event segment for Command Ring
14931493
shl rcx, 12 ; Quick multiply by 4096
14941494
add rsi, rcx
14951495
load_event:
1496-
call os_hpet_us
1496+
call [sys_time]
14971497
cmp rax, rdx
14981498
ja xhci_check_command_event_timeout
14991499
mov rax, [rsi]

src/drivers/timer.asm

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
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

1316
os_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

3741
os_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

4348
os_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
286286
kvm_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
293299
kvm_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:
310318
timer_delay:
311319
push rax
312320

313-
call [sys_timer]
321+
call [sys_delay]
314322

315323
pop rax
316324
ret

src/syscalls/system.asm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ b_system_end:
3131
; Basic
3232

3333
b_system_timecounter:
34-
;TODO
34+
call [sys_time]
3535
ret
3636

3737
b_system_free_memory:
@@ -154,7 +154,7 @@ b_system_debug_dump_rax:
154154
ret
155155

156156
b_system_delay:
157-
call b_delay
157+
call [sys_delay]
158158
ret
159159

160160
b_system_reset:

src/sysvar.asm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ os_IOAPICAddress: equ os_SystemVariables + 0x0008
7474
os_SysConfEn: equ os_SystemVariables + 0x0010 ; Enabled bits: 0=PS/2 Keyboard, 2=Serial, 4=HPET, 5=xHCI
7575
os_StackBase: equ os_SystemVariables + 0x0020
7676
os_PacketBase: equ os_SystemVariables + 0x0028
77-
sys_timer: equ os_SystemVariables + 0x0048
77+
sys_time: equ os_SystemVariables + 0x0040
78+
sys_delay: equ os_SystemVariables + 0x0048
7879
os_HPET_Address: equ os_SystemVariables + 0x0050
7980
os_AHCI_Base: equ os_SystemVariables + 0x0058
8081
os_NetworkCallback: equ os_SystemVariables + 0x0060

0 commit comments

Comments
 (0)