-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_entry.asm
More file actions
78 lines (75 loc) · 2.11 KB
/
kernel_entry.asm
File metadata and controls
78 lines (75 loc) · 2.11 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
[bits 32]
[extern entry_point]
[extern kernel_end]
[extern kernel_start]
[extern gdt_descriptor]
[extern load_memmap]
KERNEL_OFFSET equ 0xC0000000
section .text
global _start
_start:
lea eax, [load_memmap - KERNEL_OFFSET]
call eax
lea eax, [root_page_directory - KERNEL_OFFSET]
clear_page_table:
mov [eax], dword 0
add eax, 4
cmp eax, (end_page_table - KERNEL_OFFSET)
jl clear_page_table
mov eax, (kernel_end - KERNEL_OFFSET + 0x100 + 3) ; Get where we need to map up to
mov ebx, (virtual_page_table - KERNEL_OFFSET + 16 * 4) ; Start table entry
mov ecx, (kernel_start - KERNEL_OFFSET + 0x3) ; Start physical offset
fill_table:
cmp ecx, (video_memory - KERNEL_OFFSET + 0x3)
jne default_map
; If we are the page reserved for video memory we should redirect that to video mem
mov dword [ebx], 0x00b8003
jmp increment
default_map:
mov [ebx], ecx ; Write to page table entry
increment:
add ecx, 4096
add ebx, 4
cmp ecx, eax
jl fill_table
; Identity mapping
mov dword [root_page_directory - KERNEL_OFFSET], (virtual_page_table - KERNEL_OFFSET + 0x03)
; Actual mapping
mov dword [root_page_directory - KERNEL_OFFSET + 768 * 4], (virtual_page_table - KERNEL_OFFSET + 0x03)
; Last entry of page directory maps to itself
; This treats the root dir as a page table so all pages can be accessed through last 4MiB of virtual address
mov dword [root_page_directory - KERNEL_OFFSET + 1023 * 4], (root_page_directory - KERNEL_OFFSET + 0x03)
; Set cr3 with page directory
mov eax, (root_page_directory - KERNEL_OFFSET)
mov cr3, eax
; Update cr0 to enable paging
mov eax, cr0
or eax, 0x80010000
mov cr0, eax
; Jump to the virtual address of the code
lea eax, [vm_mode]
jmp eax
vm_mode:
; Load our mapped copy of the gdt
lgdt [gdt_descriptor]
; Wipe old identity mapping
mov dword [root_page_directory], 0x0
; Reload cr3
mov eax, cr3
mov cr3, eax
mov esp, stack_top ; Set the stack to our temporary kernel stack
call entry_point
jmp $
section .bss
align 0x1000
root_page_directory:
resb 4096
virtual_page_table:
resb 4096
end_page_table:
global video_memory
video_memory:
resb 4096
stack_bottom:
resb 16384
stack_top: