Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [BITS 32]
- global start
- MB_MAGIC equ 0x1BADB002
- MB_FLAGS equ (1 << 0) | (1 << 1)
- MB_CHECKSUM equ (0 - (MB_MAGIC + MB_FLAGS))
- section .multiboot
- align 4
- dd MB_MAGIC
- dd MB_FLAGS
- dd MB_CHECKSUM
- section .bss
- align 16
- stack_bottom:
- resb 4096*4096
- stack_top:
- section .text
- GDT64:
- .Null: equ $ - GDT64
- dw 0xFFFF ; Limit (low).
- dw 0 ; Base (low).
- db 0 ; Base (middle)
- db 0 ; Access.
- db 1 ; Granularity.
- db 0 ; Base (high).
- .Code: equ $ - GDT64 ; The code descriptor.
- dw 0 ; Limit (low).
- dw 0 ; Base (low).
- db 0 ; Base (middle)
- db 10011010b ; Access (exec/read).
- db 10101111b ; Granularity, 64 bits flag, limit19:16.
- db 0 ; Base (high).
- .Data: equ $ - GDT64 ; The data descriptor.
- dw 0 ; Limit (low).
- dw 0 ; Base (low).
- db 0 ; Base (middle)
- db 10010010b ; Access (read/write).
- db 00000000b ; Granularity.
- db 0 ; Base (high).
- .Pointer: ; The GDT-pointer.
- dw $ - GDT64 - 1 ; Limit.
- dq GDT64
- start:
- jmp cpuid_check
- kernel:
- mov esp, stack_top
- call kernel_main
- jmp hang
- cpuid_check:
- pushfd
- pop eax
- mov ecx, eax
- xor eax, 1 << 21
- push eax
- popfd
- pushfd
- pop eax
- push ecx
- popfd
- xor ecx, eax ;cpuid is supported if jne
- jne no_cpuid
- jmp long_mode_check
- no_cpuid:
- xor eax, eax
- jmp hang
- long_mode_check:
- mov eax, 0x80000000
- cpuid
- cmp eax, 0x80000001
- jb no_long_mode
- mov eax, 0x80000001
- cpuid
- test edx, 1 << 29
- jz no_long_mode
- table_clear:
- mov edi, 0x1000
- mov cr3, edi
- xor eax, eax
- mov ecx, 4096
- rep stosd
- mov edi, cr3
- set_tables:
- mov DWORD [edi], 0x2003
- add edi, 0x1000
- mov DWORD [edi], 0x3003
- add edi, 0x1000
- mov DWORD [edi], 0x4003
- add edi, 0x1000
- mov ebx, 0x00000003
- mov ecx, 512
- entry_set:
- mov [edi], ebx
- add ebx, 0x1000
- add edi, 8
- loop entry_set
- enable_paging:
- mov eax, cr4
- or eax, 1 << 5
- mov cr4, eax
- long_mode_switch:
- mov ecx, 0xC0000080
- rdmsr
- or eax, 1 << 8
- mov cr0, eax
- lgdt [GDT64.Pointer]
- jmp GDT64.Code:kernel
- no_long_mode:
- jmp hang
- hang:
- cli
- hlt
- hanging:
- jmp hanging
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement