-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.s
93 lines (75 loc) · 1.99 KB
/
kernel.s
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
; all constants taken from
; https://www.gnu.org/software/grub/manual/multiboot/html_node/multiboot_002eh.html
%define MULTIBOOT_HEADER_MAGIC 0x1BADB002
%define MULTIBOOT_PAGE_ALIGN 0x00000001
%define MULTIBOOT_FLAGS (MULTIBOOT_PAGE_ALIGN)
%macro export 1
global %1 ; func
%1
%endmacro
section .multiboot
align 4
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_FLAGS
dd -(MULTIBOOT_HEADER_MAGIC+MULTIBOOT_FLAGS)
section .bss
resb 16*1024 ; reserve 16KiB stack
stack:
section .multiboot.text
export _start:
extern page_directory
extern page_table0
extern kernel_begin
extern kernel_end
mov edi, page_table0
mov esi, 0
.map_page:
cmp esi, kernel_end ; mapped the entire kernel yet?
jge .setup_paging
; add page table entry
mov edx, esi
or edx, 3 ; writable, present
mov [edi], edx
add esi, 4096 ; move to the next page
add edi, 4 ; next table entry
jmp .map_page
.setup_paging:
; setup recursive paging, so we can modify the page directory
; while in paging mode (see http://www.rohitab.com/discuss/topic/31139-tutorial-paging-memory-mapping-with-a-recursive-page-directory/)
mov dword [page_directory + 1023 * 4], page_directory + 3
; our table is the page directory's 768th entry (i.e. 0xc0000000)
mov dword [page_directory + 768 * 4], page_table0 + 3
; also map at the beginning of the memory, so the
; CPU can still find the instructions after enabling
; paging (i.e. call higher_half). note that this is
; temporary, the actual page directory in C doesn't
; do this, else it wouldn't make sense to map the kernel
; in the higher half...
mov dword [page_directory], page_table0 + 3
mov ecx, page_directory
mov cr3, ecx
mov ecx, cr0
or ecx, 0x80000000
mov cr0, ecx
call higher_half
section .text
higher_half:
; kmain.c
extern kcommon
extern ktest
extern kmain
; setup stack pointer
mov esp, stack
push ebx ; magic
push eax ; struct multiboot_info *
call kcommon
%ifdef RUNTESTS
call ktest
call shutdown
%else
call kmain
.hang:
hlt
jmp .hang
%endif
%include "asmbits.s"