blob: 98289853f87a0cb6d419f5008a0c9b1113b81b81 [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
swissChili14d0b842023-01-01 02:22:44 -050016STACK_SIZE equ 0x1000
swissChilid3a652e2021-02-21 22:16:06 -080017
18
swissChilidc25b2b2021-02-23 17:07:13 -080019
20;;;;;;;;;;;;;;;;;;;;; DATA ;;;;;;;;;;;;;;;;;;;;;
21
swissChilid3a652e2021-02-21 22:16:06 -080022 [section .data align = 0x1000]
23page_directory:
swissChilie0a79bb2021-02-22 19:54:48 -080024 dd 0b010000011 ; Identity map first 4 megs
swissChilid3a652e2021-02-21 22:16:06 -080025 times (KERNEL_PAGE_NUMBER - 1) dd 0
swissChili14d0b842023-01-01 02:22:44 -050026 dd 0b010000011 ; Map kernel memory (4 megs) to zero
27 ; page too
swissChilid3a652e2021-02-21 22:16:06 -080028 times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0
swissChilid8137922021-02-17 15:34:07 -080029
swissChilidc25b2b2021-02-23 17:07:13 -080030gdt:
31 ;; First entry, null segment
32 dw 0 ; zero limit
33 dw 0 ; base low
34
35 db 0 ; base middle
36 db 0 ; access
37 db 0 ; granularity
38 db 0 ; base high
39
40 ;; Second entry, code segment
swissChili1b839222021-06-03 13:54:40 -070041 dw 0xffff ; max limit
swissChilidc25b2b2021-02-23 17:07:13 -080042 dw 0
43
44 db 0
45 db 0x9a ; access
46 db 0xcf ; granularity
47 db 0
48
49 ;; Third entry, data segment
swissChili1b839222021-06-03 13:54:40 -070050 dw 0xffff ; max limit
swissChilidc25b2b2021-02-23 17:07:13 -080051 dw 0
52
53 db 0
54 db 0x92 ; access
55 db 0xcf ; granularity
56 db 0
57
58 ;; Fourth entry, user code segment
swissChili1b839222021-06-03 13:54:40 -070059 dw 0xffff ; max limit
swissChilidc25b2b2021-02-23 17:07:13 -080060 dw 0
61
62 db 0
63 db 0xfa ; access
64 db 0xcf ; granularity
65 db 0
66
67 ;; Fifth entry, user data segment
swissChili1b839222021-06-03 13:54:40 -070068 dw 0xffff ; max limit
swissChilidc25b2b2021-02-23 17:07:13 -080069 dw 0
70
71 db 0
72 db 0xf2 ; access
73 db 0xcf ; granularity
74 db 0
75
76gdt_pointer:
77 dw (8 * 5 - 1) ; sizeof(gdt entry) * 5 - 1
78 dd (gdt - KERNEL_VIRTUAL_BASE) ; Remember, PHYSICAL address
79
80
81;;;;;;;;;;;;;;;;;;;;; CODE ;;;;;;;;;;;;;;;;;;;;;
82
swissChilid8137922021-02-17 15:34:07 -080083 [bits 32]
swissChilid3a652e2021-02-21 22:16:06 -080084 [section .text]
swissChilid8137922021-02-17 15:34:07 -080085 [global mboot]
86 [extern code]
87 [extern bss]
88 [extern end]
swissChilid8137922021-02-17 15:34:07 -080089mboot:
90 dd MBOOT_HEADER_MAGIC ; This tells GRUB to start executing here:
91 dd MBOOT_HEADER_FLAGS
92 dd MBOOT_CHECKSUM
93
94 dd mboot ; Current location
95 dd code ; .text section
96 dd bss ; End of .data
97 dd end ; End of kernel
98 dd start
99
100 [global start]
swissChili0b35bf22021-02-18 12:49:40 -0800101 [extern kmain] ; C code
swissChilid8137922021-02-17 15:34:07 -0800102
swissChilidc25b2b2021-02-23 17:07:13 -0800103start equ (_start)
swissChili4418ca52021-06-14 17:36:00 -0700104
swissChilid3a652e2021-02-21 22:16:06 -0800105_start:
swissChilidc25b2b2021-02-23 17:07:13 -0800106 ;; First set up GDT
107 mov eax, (gdt_pointer - KERNEL_VIRTUAL_BASE)
108 lgdt [eax] ; Load GDT
109
110 mov ax, 0x10 ; Offset of data segment
111 mov ds, ax
112 mov es, ax
113 mov fs, ax
114 mov gs, ax
115 mov ss, ax
116
117enable_paging:
swissChilid3a652e2021-02-21 22:16:06 -0800118 mov ecx, (page_directory - KERNEL_VIRTUAL_BASE) ; Physical address
119 mov cr3, ecx
swissChilid8137922021-02-17 15:34:07 -0800120
swissChilid3a652e2021-02-21 22:16:06 -0800121 mov ecx, cr4
122 or ecx, 0x00000010 ; Enable 4 meg pages
123 mov cr4, ecx
124
125 mov ecx, cr0
126 or ecx, 0x80000000 ; Enable paging
127 mov cr0, ecx
128
129 lea ecx, [in_higher_half] ; Long jump into high memory
130 jmp ecx
131
132in_higher_half:
133 mov dword [page_directory], 0
134 invlpg [0] ; Clear identity mapping
135
136 mov esp, (stack + STACK_SIZE)
137
swissChilie0a79bb2021-02-22 19:54:48 -0800138 add ebx, 0xC0000000 ; Translate to virtual address
139 push ebx ; Holds multiboot header location
swissChilie9289ee2021-03-20 21:54:28 -0700140 push esp ; Initial kernel stack
swissChili0b35bf22021-02-18 12:49:40 -0800141 call kmain
swissChilid3a652e2021-02-21 22:16:06 -0800142
swissChilie0a79bb2021-02-22 19:54:48 -0800143.end:
144 hlt
145 jmp .end
146
swissChilid3a652e2021-02-21 22:16:06 -0800147
148 [section .bss align = 32]
149stack:
150 resb STACK_SIZE