Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
core_ap.asm
Go to the documentation of this file.
1global cpu_trampoline
2
3org 0x8000
4bits 16
5
6cpu_trampoline:
7 jmp short start_trampoline
8 nop
9
10align 8
11signature: dq 0
12pml4_addr: dq 0
13data: dq 0
14
15start_trampoline:
16 cli
17 xor ax, ax
18 mov ds, ax
19 mov es, ax
20 mov ss, ax
21
22 ; cek signature
23 ; memastikan tidak ada corrupt
24 mov eax, [signature]
25 mov ebx, 0x00EEDDAB
26 cmp ebx, eax
27 je .signature_ok
28 ; ngehang
29 hlt
30 jmp $
31
32.signature_ok:
33 ; Enable A20 line
34 ; biar bisa pakai ram > 1mb
35 in al, 0x92
36 or al, 2
37 out 0x92, al
38
39 lgdt [gdt_descriptor]
40
41 ; Enable protected mode (32bit)
42 mov eax, cr0
43 or eax, 1
44 mov cr0, eax
45
46 jmp 0x08:core_ap_32
47
48
49bits 32
50core_ap_32:
51 mov ax, 0x10
52 mov ds, ax
53 mov es, ax
54 mov ss, ax
55 mov gs, ax
56 mov fs, ax
57
58 ; stack sementara
59 ; pakai area di bawah 0x8000 yang aman
60 mov esp, 0x7C00
61
62 ; Set up PAE paging
63 mov eax, [pml4_addr]
64 mov cr3, eax
65
66 mov eax, cr4
67 or eax, (1 << 5) ; PAE enable
68 mov cr4, eax
69
70 ; Enable long mode
71 mov ecx, 0xC0000080
72 rdmsr
73 or eax, (1 << 8) ; LME enable
74 or eax, (1 << 11) ; NXE - No-Execute Enable
75 wrmsr
76
77 ; Enable paging
78 mov eax, cr0
79 or eax, (1 << 31) ; PG enable
80 mov cr0, eax
81
82 ; read LAPIC ID
83 mov eax, 1
84 cpuid
85 shr ebx, 24 ; APIC ID ada di EBX[31:24]
86 mov [real_apic_id], ebx ; simpan dulu sebelum GDT setup
87
88 cli
89 lgdt [gdt64_descriptor]
90 jmp 0x08:core_ap_64
91
92
93; extern cpu_trampoline_phase_2
94bits 64
95core_ap_64:
96 mov ax, 0x10
97 mov ds, ax
98 mov es, ax
99 mov ss, ax
100 mov gs, ax
101 mov fs, ax
102
103 lea rbx, [rel signature]
104 mov r12, [rbx + 16] ; load data (signature+16)
105
106 cld
107 mov rax, [rbx + 8] ; load pml4_addr (signature+8)
108 mov cr3, rax
109 cli
110
111 mov rax, [r12 + 8] ; load stack_top
112 mov rsp, rax
113 and rsp, -16
114 sub rsp, 8
115
116 mov qword [r12 + 16], 1 ; set handshake[2] = 1
117
118 mov rax, [r12] ; load cpuTrampolinePhase2
119 mov rdi, qword [rel real_apic_id] ; load real_apic_id (32-bit)
120 call rax
121 jmp $
122
123gdt_start:
124 dq 0x0000000000000000 ; Null
125 dq 0x00CF9A000000FFFF ; Code32
126 dq 0x00CF92000000FFFF ; Data32
127gdt_end:
128
129gdt_descriptor:
130 dw gdt_end - gdt_start - 1
131 dd gdt_start
132
133; Dummy GDT 64 bit
134gdt64_start:
135 dq 0x0000000000000000 ; Null
136 dq 0x00209A0000000000 ; Code64
137 dq 0x0000920000000000 ; Data64
138gdt64_end:
139
140gdt64_descriptor:
141 dw gdt64_end - gdt64_start - 1
142 dq gdt64_start
143
144real_apic_id dd 0