@@ -66,9 +66,15 @@ init_timer_hpet:
6666 mov rbx , rax ; Move Counter Clock Period to RBX
6767 xor rdx , rdx
6868 mov rax , 1000000000000000 ; femotoseconds per second
69- div rbx ; RDX:RAX / RBX
69+ div rbx ; RDX:RAX / RBX = frequency in Hz
7070 mov [ p_HPET_Frequency ], eax ; Save the HPET frequency
7171
72+ ; Precompute cycles per microsecond (freq_Hz / 1,000,000)
73+ xor rdx , rdx
74+ mov ecx , 1000000
75+ div rcx
76+ mov [ p_HPET_CyclesPerUs ], rax
77+
7278 ; Disable interrupts on all timers
7379 xor ebx , ebx
7480 mov bl , [ p_HPET_Timers ]
@@ -140,24 +146,18 @@ hpet_delay:
140146 push rax
141147
142148 mov rbx , rax ; Save delay to RBX
143- xor edx , edx
144- xor ecx , ecx
145- call hpet_read ; Get HPET General Capabilities and ID Register
146- shr rax , 32
147- mov rcx , rax ; RCX = RAX >> 32 (timer period in femtoseconds)
148- mov rax , 1000000000
149- div rcx ; Divide 1000000000 (RDX:RAX) / RCX (converting from period in femtoseconds to frequency in MHz)
150- mul rbx ; RAX *= RBX, should get number of HPET cycles to wait, save result in RBX
149+ mov rax , [ p_HPET_CyclesPerUs ] ; Use precomputed cycles per microsecond
150+ mul rbx ; RAX = cycles to wait
151151 mov rbx , rax
152152 mov ecx , HPET_MAIN_COUNTER
153153 call hpet_read ; Get HPET counter in RAX
154- add rbx , rax ; RBX += RAX Until when to wait
154+ add rbx , rax ; RBX = target counter value
155155hpet_delay_loop: ; Stay in this loop until the HPET timer reaches the expected value
156+ pause
156157 mov ecx , HPET_MAIN_COUNTER
157158 call hpet_read ; Get HPET counter in RAX
158- cmp rax , rbx ; If RAX >= RBX then jump to end, otherwise jump to loop
159- jae hpet_delay_end
160- jmp hpet_delay_loop
159+ cmp rax , rbx
160+ jb hpet_delay_loop
161161hpet_delay_end:
162162
163163 pop rax
@@ -291,6 +291,7 @@ kvm_delay:
291291 call kvm_get_usec
292292 add rbx , rax ; Add elapsed time
293293kvm_delay_wait:
294+ pause
294295 call kvm_get_usec
295296 cmp rax , rbx
296297 jb kvm_delay_wait
0 commit comments