blob: 047b326b08b6a381fcfbf5e666ac30f29ca71f8c [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
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
swissChilie0a79bb2021-02-22 19:54:48 -080026 dd 0b010000011 ; Map kernel memory to zero page too
swissChilid3a652e2021-02-21 22:16:06 -080027 times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0
swissChilid8137922021-02-17 15:34:07 -080028
swissChilidc25b2b2021-02-23 17:07:13 -080029gdt:
30 ;; First entry, null segment
31 dw 0 ; zero limit
32 dw 0 ; base low
33
34 db 0 ; base middle
35 db 0 ; access
36 db 0 ; granularity
37 db 0 ; base high
38
39 ;; Second entry, code segment
swissChili1b839222021-06-03 13:54:40 -070040 dw 0xffff ; max limit
swissChilidc25b2b2021-02-23 17:07:13 -080041 dw 0
42
43 db 0
44 db 0x9a ; access
45 db 0xcf ; granularity
46 db 0
47
48 ;; Third entry, data segment
swissChili1b839222021-06-03 13:54:40 -070049 dw 0xffff ; max limit
swissChilidc25b2b2021-02-23 17:07:13 -080050 dw 0
51
52 db 0
53 db 0x92 ; access
54 db 0xcf ; granularity
55 db 0
56
57 ;; Fourth entry, user code segment
swissChili1b839222021-06-03 13:54:40 -070058 dw 0xffff ; max limit
swissChilidc25b2b2021-02-23 17:07:13 -080059 dw 0
60
61 db 0
62 db 0xfa ; access
63 db 0xcf ; granularity
64 db 0
65
66 ;; Fifth entry, user data segment
swissChili1b839222021-06-03 13:54:40 -070067 dw 0xffff ; max limit
swissChilidc25b2b2021-02-23 17:07:13 -080068 dw 0
69
70 db 0
71 db 0xf2 ; access
72 db 0xcf ; granularity
73 db 0
74
75gdt_pointer:
76 dw (8 * 5 - 1) ; sizeof(gdt entry) * 5 - 1
77 dd (gdt - KERNEL_VIRTUAL_BASE) ; Remember, PHYSICAL address
78
79
80;;;;;;;;;;;;;;;;;;;;; CODE ;;;;;;;;;;;;;;;;;;;;;
81
swissChilid8137922021-02-17 15:34:07 -080082 [bits 32]
swissChilid3a652e2021-02-21 22:16:06 -080083 [section .text]
swissChilid8137922021-02-17 15:34:07 -080084 [global mboot]
85 [extern code]
86 [extern bss]
87 [extern end]
swissChilid8137922021-02-17 15:34:07 -080088mboot:
89 dd MBOOT_HEADER_MAGIC ; This tells GRUB to start executing here:
90 dd MBOOT_HEADER_FLAGS
91 dd MBOOT_CHECKSUM
92
93 dd mboot ; Current location
94 dd code ; .text section
95 dd bss ; End of .data
96 dd end ; End of kernel
97 dd start
98
99 [global start]
swissChili0b35bf22021-02-18 12:49:40 -0800100 [extern kmain] ; C code
swissChilid8137922021-02-17 15:34:07 -0800101
swissChilidc25b2b2021-02-23 17:07:13 -0800102start equ (_start)
swissChili4418ca52021-06-14 17:36:00 -0700103
swissChilid3a652e2021-02-21 22:16:06 -0800104_start:
swissChilidc25b2b2021-02-23 17:07:13 -0800105 ;; First set up GDT
106 mov eax, (gdt_pointer - KERNEL_VIRTUAL_BASE)
107 lgdt [eax] ; Load GDT
108
109 mov ax, 0x10 ; Offset of data segment
110 mov ds, ax
111 mov es, ax
112 mov fs, ax
113 mov gs, ax
114 mov ss, ax
115
116enable_paging:
swissChilid3a652e2021-02-21 22:16:06 -0800117 mov ecx, (page_directory - KERNEL_VIRTUAL_BASE) ; Physical address
118 mov cr3, ecx
swissChilid8137922021-02-17 15:34:07 -0800119
swissChilid3a652e2021-02-21 22:16:06 -0800120 mov ecx, cr4
121 or ecx, 0x00000010 ; Enable 4 meg pages
122 mov cr4, ecx
123
124 mov ecx, cr0
125 or ecx, 0x80000000 ; Enable paging
126 mov cr0, ecx
127
128 lea ecx, [in_higher_half] ; Long jump into high memory
129 jmp ecx
130
131in_higher_half:
132 mov dword [page_directory], 0
133 invlpg [0] ; Clear identity mapping
134
135 mov esp, (stack + STACK_SIZE)
136
swissChilie0a79bb2021-02-22 19:54:48 -0800137 add ebx, 0xC0000000 ; Translate to virtual address
138 push ebx ; Holds multiboot header location
swissChilie9289ee2021-03-20 21:54:28 -0700139 push esp ; Initial kernel stack
swissChili0b35bf22021-02-18 12:49:40 -0800140 call kmain
swissChilid3a652e2021-02-21 22:16:06 -0800141
swissChilie0a79bb2021-02-22 19:54:48 -0800142.end:
143 hlt
144 jmp .end
145
swissChilid3a652e2021-02-21 22:16:06 -0800146
147 [section .bss align = 32]
148stack:
149 resb STACK_SIZE