Files

100 lines
2.1 KiB
NASM
Raw Permalink Normal View History

FMBALIGN equ 1 << 0 ; align loaded modules on page boundaries
FMEMINFO equ 1 << 1 ; provide memory map
FVIDMODE equ 1 << 2 ; try to set graphics mode
FLAGS equ FMBALIGN | FMEMINFO | FVIDMODE
MAGIC equ 0x1BADB002
CHECKSUM equ -(MAGIC + FLAGS)
2018-09-28 16:10:20 -04:00
KERNEL_VIRTUAL_BASE equ 0xC0000000 ; 3GB
KERNEL_PAGE_NUMBER equ (KERNEL_VIRTUAL_BASE >> 22) ; 768
2018-09-28 16:10:20 -04:00
section .data
align 0x1000
boot_page_directory:
; 4MiB lower-half kernel page
dd 0x83
times (KERNEL_PAGE_NUMBER - 1) dd 0
2018-09-28 16:10:20 -04:00
; 4MiB higher-half kernel page
dd 0x83
times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0
2018-09-28 16:10:20 -04:00
section .multiboot
align 4
; header
2018-09-28 16:10:20 -04:00
dd MAGIC
dd FLAGS
dd CHECKSUM
2018-09-28 16:10:20 -04:00
; address tag
dd 0 ; header_addr
dd 0 ; load_addr
dd 0 ; load_end_addr
dd 0 ; bss_end_addr
dd 0 ; entry_addr
2018-09-28 16:10:20 -04:00
; graphics tag
dd 0 ; mode_type
2018-09-28 16:10:20 -04:00
dd 800 ; width
dd 600 ; height
dd 32 ; depth
2018-09-28 16:10:20 -04:00
; Create bootstrap stack
section .bss
align 16
stack_bottom:
2018-09-28 16:10:20 -04:00
resb 0x4000 ; 16 KiB
stack_top:
2018-09-28 16:10:20 -04:00
section .text
global _start:function (_start.end - _start)
_start:
2018-09-28 16:10:20 -04:00
mov ecx, (boot_page_directory - KERNEL_VIRTUAL_BASE)
mov cr3, ecx ; Load boot_page_directory into CR3
2018-09-28 16:10:20 -04:00
mov ecx, cr4
or ecx, (1 << 4) ; Set PSE bit in CR4 to enable 4MB pages.
mov cr4, ecx
mov ecx, cr0
or ecx, (1 << 31) ; Set PG bit in CR0 to enable paging.
mov cr0, ecx
lea ecx, [.start_higher_half] ; Far jump
2018-09-28 16:10:20 -04:00
jmp ecx
.start_higher_half:
; Unmap lower-half kernel page
2018-09-28 16:10:20 -04:00
mov dword [boot_page_directory], 0
invlpg [0]
; Set up stack
mov esp, stack_top
; Save multiboot information
2018-09-28 16:10:20 -04:00
push ebx ; multiboot_info
push eax ; multiboot_magic
2018-09-28 16:10:20 -04:00
; Enable SSE
mov eax, cr0
2018-10-01 20:51:57 -04:00
and ax, ~(1 << 2) ; clr CR0.EM
or ax, (1 << 1) ; set CR0.MP
mov cr0, eax
mov eax, cr4
2018-10-01 20:51:57 -04:00
or ax, (3 << 9) ; set CR4.OSFXSR | CR4.OSXMMEXCPT
mov cr4, eax
2018-09-28 16:10:20 -04:00
; Initialize GDT & IDT
extern init_descriptor_tables
call init_descriptor_tables
; Enter kernel
2018-09-28 16:10:20 -04:00
extern kernel_main
call kernel_main
2018-09-28 16:10:20 -04:00
cli
.hang:
hlt
2018-09-28 16:10:20 -04:00
jmp .hang
.end: