]> git.dujemihanovic.xyz Git - nameless-os.git/blob - boot/x86/stage3/paging.s
Enable paging in bootloader
[nameless-os.git] / boot / x86 / stage3 / paging.s
1 ; Code for enabling paging before calling the kernel
2 ; Identity maps the VGA framebuffer memory and high-half maps the kernel memory
3 bits 32
4
5 section .text
6
7 ; The problem with this code is that it assumes that the kernel's various
8 ; sections occupy a certain number of pages. As of writing it is correct, but as
9 ; the kernel grows this code may fail to map those pages, which is not good. The
10 ; solution to this is to use ELF instead of a flat binary. This was not done
11 ; before because it would require paging, but now that paging works using ELF is
12 ; a possibility which must be exploited.
13
14 enable_paging:
15 push eax
16 mov eax, cr0
17 or eax, 0x80000000
18 mov cr0, eax
19 pop eax
20 ret
21
22 load_paging_structs:
23 push eax
24 push ebx
25 xor ebx, ebx
26 .pt_low_loop:
27 mov eax, ebx
28 shl eax, 12
29 or eax, 1|2 ; P and R/W flags
30 mov [page_table_low+ebx*4], eax
31 inc ebx
32 cmp ebx, 0x100
33 jl .pt_low_loop
34
35 xor ebx, ebx
36 .pt_high_ro_loop:
37 mov eax, ebx
38 add eax, 0x100
39 shl eax, 12
40 or eax, 1 ; P flag
41 mov [page_table_high+ebx*4], eax
42 inc ebx
43 cmp ebx, 0x2
44 jl .pt_high_ro_loop
45
46 mov ebx, 0x2
47 .pt_high_rw_loop:
48 mov eax, ebx
49 add eax, 0x100
50 shl eax, 12
51 or eax, 1|2 ; P and R/W flags
52 mov [page_table_high+ebx*4], eax
53 inc ebx
54 cmp ebx, 0x9
55 jl .pt_high_rw_loop
56
57 mov eax, page_table_low
58 and eax, 0xfffff000
59 or eax, 1|2
60 mov [page_directory], eax
61
62 mov eax, page_table_high
63 and eax, 0xfffff000
64 or eax, 1|2
65 mov [page_directory+768*4], eax
66
67 mov eax, page_directory
68 mov cr3, eax
69 pop ebx
70 pop eax
71 ret
72
73 section .data
74 align 4096
75 page_table_low:
76 times 1024 dd 0
77 page_table_high:
78 times 1024 dd 0
79 page_directory:
80 times 1024 dd 0