From bd11ed9a6f92d097972a4e3414ee03c2fc1bbaca Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Thu, 13 Aug 2015 13:09:13 +0200 Subject: [PATCH] Check for Multiboot, CPUID, and Long Mode --- src/arch/x86_64/boot.asm | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/arch/x86_64/boot.asm b/src/arch/x86_64/boot.asm index c2f8dd7b..1b80840b 100644 --- a/src/arch/x86_64/boot.asm +++ b/src/arch/x86_64/boot.asm @@ -19,6 +19,10 @@ bits 32 start: mov esp, stack_top + call check_multiboot + call check_cpuid + call check_long_mode + ; print `OK` to screen mov dword [0xb8000], 0x2f4b2f4f hlt @@ -32,6 +36,49 @@ error: mov byte [0xb800a], al hlt +; Throw error 0 if eax doesn't contain the Multiboot 2 magic value (0x36d76289). +check_multiboot: + cmp eax, 0x36d76289 + jne .no_multiboot + ret +.no_multiboot: + mov al, "0" + jmp error + +; Throw error 1 if the CPU doesn't support the CPUID command. +check_cpuid: + pushfd ; Store the FLAGS-register. + pop eax ; Restore the A-register. + mov ecx, eax ; Set the C-register to the A-register. + xor eax, 1 << 21 ; Flip the ID-bit, which is bit 21. + push eax ; Store the A-register. + popfd ; Restore the FLAGS-register. + pushfd ; Store the FLAGS-register. + pop eax ; Restore the A-register. + push ecx ; Store the C-register. + popfd ; Restore the FLAGS-register. + xor eax, ecx ; Do a XOR-operation on the A-register and the C-register. + jz .no_cpuid ; The zero flag is set, no CPUID. + ret ; CPUID is available for use. +.no_cpuid: + mov al, "1" + jmp error + +; Throw error 2 if the CPU doesn't support Long Mode. +check_long_mode: + mov eax, 0x80000000 ; Set the A-register to 0x80000000. + cpuid ; CPU identification. + cmp eax, 0x80000001 ; Compare the A-register with 0x80000001. + jb .no_long_mode ; It is less, there is no long mode. + mov eax, 0x80000000 ; Set the A-register to 0x80000000. + cpuid ; CPU identification. + cmp eax, 0x80000001 ; Compare the A-register with 0x80000001. + jb .no_long_mode ; It is less, there is no long mode. + ret +.no_long_mode: + mov al, "2" + jmp error + section .bss stack_bottom: resb 64