-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboot.s
More file actions
129 lines (101 loc) · 2.98 KB
/
Copy pathboot.s
File metadata and controls
129 lines (101 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
; Multiboot Kernel Bootloader
BITS 32
; Multiboot flags
MODULEALIGN equ 1 << 0 ; Align loaded modules on page boundaries
MEMINFO equ 1 << 1 ; Provide memory map (BIOS e820)
FLAGS equ MODULEALIGN | MEMINFO
; Multiboot header magic number and checksum
MAGIC equ 0x1BADB002 ; Magic number for multiboot
CHECKSUM equ -(MAGIC + FLAGS)
; Kernel constants
KERNEL_VIRTUAL_BASE equ 0xC0000000 ; Virtual base of the kernel (3GB)
PAGE_SIZE equ 4096 ; Page size (4KB)
PAGE_PERM equ 0x3 ; Page permissions: present, read/write
STACK_SIZE equ 4 * PAGE_SIZE ; Kernel stack size (16KB)
extern _KERNEL_START
extern _KERNEL_END
extern _EARLY_KERNEL_START
extern _EARLY_KERNEL_END
extern kernel_main
; Multiboot header section
section .__mbHeader
align 4
dd MAGIC
dd FLAGS
dd CHECKSUM
; Early boot code section
section .early_text progbits
global _start
_start:
; Save multiboot information from GRUB
mov [multiboot_magic], eax
mov [multiboot_info], ebx
; Identity map low memory (physical address = virtual address)
; first 1M
mov eax, lowmem_page_table
mov [page_directory], eax
or dword [page_directory], PAGE_PERM
xor eax, eax ; Start at physical address 0
.lowmem:
mov ecx, eax
shr ecx, 12
and ecx, 1023 ; Index for page table entry
; Map the first MB
mov [lowmem_page_table + ecx * 4], eax
or dword [lowmem_page_table + ecx * 4], PAGE_PERM
add eax, PAGE_SIZE
cmp eax, _EARLY_KERNEL_END
jl .lowmem
; Map the kernel in the higher half (starting at KERNEL_BASE)
mov ecx, (KERNEL_VIRTUAL_BASE >> 22)
mov eax, kernel_page_table
mov [page_directory + ecx * 4], eax
or dword [page_directory + ecx * 4], PAGE_PERM
mov eax, _KERNEL_START
.higher:
mov ecx, eax
shr ecx, 12
and ecx, 1023 ; Index for page table entry
mov ebx, eax
sub ebx, KERNEL_VIRTUAL_BASE ; Virtual to physical
mov [kernel_page_table + ecx * 4], ebx
or dword [kernel_page_table + ecx * 4], PAGE_PERM
add eax, PAGE_SIZE
cmp eax, _KERNEL_END
jl .higher
; Load page directory and enable paging
mov eax, page_directory
mov cr3, eax
mov eax, cr0
or eax, 0x80000000
mov cr0, eax ; Enable paging
; Enable RFLAGS overflow register
pushfd
mov dword [esp], 0x0800
popfd
; Adjust the stack and call the kernel
mov esp, stack + STACK_SIZE
push dword [multiboot_info]
call kernel_main ; Jump to kernel main function
.loop:
hlt
jmp short .loop
; Page directories and page tables
section .early_bss nobits
alignb 4096
page_directory:
resd 1024 ; Page directory (1024 entries)
lowmem_page_table:
resd 1024 ; Page table for low memory
kernel_page_table:
resd 1024 ; Page table for kernel
section .early_data
multiboot_magic:
dd 0
multiboot_info:
dd 0
; Stack section
section .bss
align 4
stack:
resb STACK_SIZE ; Kernel stack (16KB)