mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Check multiboot magic number and for CPUID/long mode support
This commit is contained in:
@@ -5,10 +5,75 @@ bits 32
|
|||||||
start:
|
start:
|
||||||
mov esp, stack_top
|
mov esp, stack_top
|
||||||
|
|
||||||
|
call check_multiboot
|
||||||
|
call check_cpuid
|
||||||
|
call check_long_mode
|
||||||
|
|
||||||
; print `OK` to screen
|
; print `OK` to screen
|
||||||
mov dword [0xb8000], 0x2f4b2f4f
|
mov dword [0xb8000], 0x2f4b2f4f
|
||||||
hlt
|
hlt
|
||||||
|
|
||||||
|
check_multiboot:
|
||||||
|
cmp eax, 0x36d76289
|
||||||
|
jne .no_multiboot
|
||||||
|
ret
|
||||||
|
.no_multiboot:
|
||||||
|
mov al, "0"
|
||||||
|
jmp error
|
||||||
|
|
||||||
|
check_cpuid:
|
||||||
|
; Check if CPUID is supported by attempting to flip the ID bit (bit 21)
|
||||||
|
; in the FLAGS register. If we can flip it, CPUID is available.
|
||||||
|
|
||||||
|
; Copy FLAGS in to EAX via stack
|
||||||
|
pushfd
|
||||||
|
pop eax
|
||||||
|
|
||||||
|
; Copy to ECX as well for comparing later on
|
||||||
|
mov ecx, eax
|
||||||
|
|
||||||
|
; Flip the ID bit
|
||||||
|
xor eax, 1 << 21
|
||||||
|
|
||||||
|
; Copy EAX to FLAGS via the stack
|
||||||
|
push eax
|
||||||
|
popfd
|
||||||
|
|
||||||
|
; Copy FLAGS back to EAX (with the flipped bit if CPUID is supported)
|
||||||
|
pushfd
|
||||||
|
pop eax
|
||||||
|
|
||||||
|
; Restore FLAGS from the old version stored in ECX (i.e. flipping the
|
||||||
|
; ID bit back if it was ever flipped).
|
||||||
|
push ecx
|
||||||
|
popfd
|
||||||
|
|
||||||
|
; Compare EAX and ECX. If they are equal then that means the bit
|
||||||
|
; wasn't flipped, and CPUID isn't supported.
|
||||||
|
cmp eax, ecx
|
||||||
|
je .no_cpuid
|
||||||
|
ret
|
||||||
|
.no_cpuid:
|
||||||
|
mov al, "1"
|
||||||
|
jmp error
|
||||||
|
|
||||||
|
check_long_mode:
|
||||||
|
; test if extended processor info in available
|
||||||
|
mov eax, 0x80000000 ; implicit argument for cpuid
|
||||||
|
cpuid ; get highest supported argument
|
||||||
|
cmp eax, 0x80000001 ; it needs to be at least 0x80000001
|
||||||
|
jb .no_long_mode ; if it's less, the CPU is too old for long mode
|
||||||
|
|
||||||
|
; use extended info to test if long mode is available
|
||||||
|
mov eax, 0x80000001 ; argument for extended processor info
|
||||||
|
cpuid ; returns various feature bits in ecx and edx
|
||||||
|
test edx, 1 << 29 ; test if the LM-bit is set in the D-register
|
||||||
|
jz .no_long_mode ; If it's not set, there is no long mode
|
||||||
|
ret
|
||||||
|
.no_long_mode:
|
||||||
|
mov al, "2"
|
||||||
|
jmp error
|
||||||
|
|
||||||
; Prints `ERR: ` and the given error code to screen and hangs.
|
; Prints `ERR: ` and the given error code to screen and hangs.
|
||||||
; parameter: error code (in ascii) in al
|
; parameter: error code (in ascii) in al
|
||||||
error:
|
error:
|
||||||
|
|||||||
Reference in New Issue
Block a user