* add p cc
[mascara-docs.git] / amd64 / bareMetalOS-0.5.2 / pure64.boot0 / src / init_smp_acpi.asm
blob21babc9ac9f0df62d5dea3f55107beba2e16a119
1 ; =============================================================================
2 ; Pure64 -- a 64-bit OS loader written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2011 Return Infinity -- see LICENSE.TXT
5 ; INIT SMP ACPI
6 ; =============================================================================
9 init_smp_acpi:
10 lodsb ; Checksum
11 lodsd ; OEMID (First 4 bytes)
12 lodsw ; OEMID (Last 2 bytes)
13 lodsb ; Grab the Revision value (0 is v1.0, 1 is v2.0, 2 is v3.0, etc)
14 add al, 49
15 mov [0x000B8098], al ; Print the ACPI version number
16 sub al, 49
17 cmp al, 0
18 je foundACPIv1 ; If AL is 0 then the system is using ACPI v1.0
19 jmp foundACPIv2 ; Otherwise it is v2.0 or higher
21 foundACPIv1:
22 xor eax, eax
23 lodsd ; Grab the 32 bit physical address of the RSDT (Offset 16).
24 mov rsi, rax ; RSI now points to the RSDT
25 lodsd ; Grab the Signiture
26 cmp eax, 'RSDT' ; Make sure the signiture is valid
27 jne novalidacpi ; Not the same? Bail out
28 sub rsi, 4
29 mov [os_ACPITableAddress], rsi ; Save the RSDT Table Address
30 add rsi, 4
31 xor eax, eax
32 lodsd ; Length
33 add rsi, 28 ; Skip to the Entry offset
34 sub eax, 36 ; EAX holds the table size. Subtract the preamble
35 shr eax, 2 ; Divide by 4
36 mov rdx, rax ; RDX is the entry count
37 xor ecx, ecx
38 foundACPIv1_nextentry:
39 lodsd
40 push rax
41 add ecx, 1
42 cmp ecx, edx
43 je findAPICTable
44 jmp foundACPIv1_nextentry
46 foundACPIv2:
47 lodsd ; RSDT Address
48 lodsd ; Length
49 lodsq ; Grab the 64 bit physical address of the XSDT (Offset 24).
50 mov rsi, rax ; RSI now points to the XSDT
51 lodsd ; Grab the Signiture
52 cmp eax, 'XSDT' ; Make sure the signiture is valid
53 jne novalidacpi ; Not the same? Bail out
54 sub rsi, 4
55 mov [os_ACPITableAddress], rsi ; Save the XSDT Table Address
56 add rsi, 4
57 xor eax, eax
58 lodsd ; Length
59 add rsi, 28 ; Skip to the start of the Entries (offset 36)
60 sub eax, 36 ; EAX holds the table size. Subtract the preamble
61 shr eax, 3 ; Divide by 8
62 mov rdx, rax ; RDX is the entry count
63 xor ecx, ecx
64 foundACPIv2_nextentry:
65 lodsq
66 push rax
67 add ecx, 1
68 cmp ecx, edx
69 jne foundACPIv2_nextentry
71 findAPICTable:
72 mov al, '3' ; Search for the APIC table
73 mov [0x000B809C], al
74 mov al, '4'
75 mov [0x000B809E], al
76 mov ebx, 'APIC'
77 xor ecx, ecx
78 searchingforAPIC:
79 pop rsi
80 lodsd
81 add ecx, 1
82 cmp eax, ebx
83 je foundAPICTable
84 cmp ecx, edx
85 jne searchingforAPIC
86 jmp noMP
88 fixstack:
89 pop rax
90 add ecx, 1
92 foundAPICTable:
93 ; fix the stack
94 cmp ecx, edx
95 jne fixstack
97 lodsd ; Length of MADT in bytes
98 mov ecx, eax
99 xor ebx, ebx
100 lodsb ; Revision
101 lodsb ; Checksum
102 lodsd ; OEMID (First 4 bytes)
103 lodsw ; OEMID (Last 2 bytes)
104 lodsq ; OEM Table ID
105 lodsd ; OEM Revision
106 lodsd ; Creator ID
107 lodsd ; Creator Revision
108 xor eax, eax
109 lodsd ; Local APIC Address
110 mov [os_LocalAPICAddress], rax ; Save the Address of the Local APIC
111 lodsd ; Flags
112 add ebx, 44
113 mov rdi, 0x0000000000005800
115 readAPICstructures:
116 cmp ebx, ecx
117 jge init_smp_acpi_done
118 lodsb ; APIC Structure Type
119 cmp al, 0
120 je APICcpu
121 cmp al, 1
122 je APICioapic
123 jmp APICignore
125 APICcpu:
126 inc word [cpu_detected]
127 xor eax, eax
128 lodsb ; Length (will be set to 8)
129 add ebx, eax
130 lodsb ; ACPI Processor ID
131 lodsb ; APIC ID
132 push rdi
133 add rdi, rax
134 lodsd ; Flags
135 stosb
136 pop rdi
137 jmp readAPICstructures ; Read the next structure
139 APICioapic:
140 xor eax, eax
141 lodsb ; Length (will be set to 12)
142 add ebx, eax
143 lodsb ; IO APIC ID
144 lodsb ; Reserved
145 xor eax, eax
146 lodsd ; IO APIC Address
147 mov [os_IOAPICAddress], rax
148 lodsd ; System Vector Base
149 jmp readAPICstructures ; Read the next structure
151 APICignore:
152 xor eax, eax
153 lodsb ; We have a type that we ignore, read the next byte
154 add ebx, eax
155 add rsi, rax
156 sub rsi, 2 ; For the two bytes just read
157 jmp readAPICstructures ; Read the next structure
159 init_smp_acpi_done:
160 jmp makempgonow
162 novalidacpi:
163 mov al, 'X'
164 mov [0x000B809A], al
165 jmp $
166 ; =============================================================================
167 ; EOF