blob: a65e2d8d2877591de3d6916bc0838bf7f28c92cc [file] [log] [blame]
swissChilid8137922021-02-17 15:34:07 -08001;;; GRUB Multiboot header, calls main() in main.c
2
swissChilid3a652e2021-02-21 22:16:06 -08003 ;; Some constants
swissChilid8137922021-02-17 15:34:07 -08004MBOOT_PAGE_ALIGN equ 1<<0
5MBOOT_MEM_INFO equ 1<<1
6MBOOT_HEADER_MAGIC equ 0x1BADB002
7
8MBOOT_HEADER_FLAGS equ MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO
9MBOOT_CHECKSUM equ -(MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS)
10
swissChilid3a652e2021-02-21 22:16:06 -080011 ;; Kernel memory start
12KERNEL_VIRTUAL_BASE equ 0xC0000000
13 ;; Index in page directory
14KERNEL_PAGE_NUMBER equ (KERNEL_VIRTUAL_BASE >> 22)
15
16STACK_SIZE equ 0x4000
17
18
19 [section .data align = 0x1000]
20page_directory:
21 dd 0b010000011 ; Identity map first 4 megs
22 times (KERNEL_PAGE_NUMBER - 1) dd 0
23 dd 0b010000011 ; Map kernel memory to zero page too
24 times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0
swissChilid8137922021-02-17 15:34:07 -080025
26 [bits 32]
swissChilid3a652e2021-02-21 22:16:06 -080027 [section .text]
swissChilid8137922021-02-17 15:34:07 -080028 [global mboot]
29 [extern code]
30 [extern bss]
31 [extern end]
swissChilid8137922021-02-17 15:34:07 -080032mboot:
33 dd MBOOT_HEADER_MAGIC ; This tells GRUB to start executing here:
34 dd MBOOT_HEADER_FLAGS
35 dd MBOOT_CHECKSUM
36
37 dd mboot ; Current location
38 dd code ; .text section
39 dd bss ; End of .data
40 dd end ; End of kernel
41 dd start
42
43 [global start]
swissChili0b35bf22021-02-18 12:49:40 -080044 [extern kmain] ; C code
swissChilid8137922021-02-17 15:34:07 -080045
swissChilid3a652e2021-02-21 22:16:06 -080046start equ (_start - KERNEL_VIRTUAL_BASE)
47
48_start:
49 mov ecx, (page_directory - KERNEL_VIRTUAL_BASE) ; Physical address
50 mov cr3, ecx
swissChilid8137922021-02-17 15:34:07 -080051
swissChilid3a652e2021-02-21 22:16:06 -080052 mov ecx, cr4
53 or ecx, 0x00000010 ; Enable 4 meg pages
54 mov cr4, ecx
55
56 mov ecx, cr0
57 or ecx, 0x80000000 ; Enable paging
58 mov cr0, ecx
59
60 lea ecx, [in_higher_half] ; Long jump into high memory
61 jmp ecx
62
63in_higher_half:
64 mov dword [page_directory], 0
65 invlpg [0] ; Clear identity mapping
66
67 mov esp, (stack + STACK_SIZE)
68
69 push ebx ; Holds multiboot header location, PHYSICAL addr
swissChili0b35bf22021-02-18 12:49:40 -080070 call kmain
swissChilid8137922021-02-17 15:34:07 -080071 jmp $
swissChilid3a652e2021-02-21 22:16:06 -080072
73
74 [section .bss align = 32]
75stack:
76 resb STACK_SIZE