-
Notifications
You must be signed in to change notification settings - Fork 47
/
acpi16.asm
375 lines (317 loc) · 6.09 KB
/
acpi16.asm
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
; --------------------------------------- 16 bit APIC functions ---------------------------------------
USE16
include 'mutex16.asm'
; Returns APIC in EBX
; implemented as FAR to allow calling from elsewhere
GetMyApic16f:
push eax
push ecx
push edx
mov eax,1
cpuid
and ebx,0xff000000
shr ebx,24
pop edx
pop ecx
pop eax
retf
;-------------------------------------------------------------------------------------------
; Function ChecksumValid : Check the sum. EDI physical addr, ECX count
;-------------------------------------------------------------------------------------------
ChecksumValid:
PUSH ECX
PUSH EDI
XOR EAX,EAX
.St:
ADD EAX,[FS:EDI]
INC EDI
DEC ECX
JECXZ .End
JMP .St
.End:
TEST EAX,0xFF
JNZ .F
MOV EAX,1
.F:
POP EDI
POP ECX
RETF
;-------------------------------------------------------------------------------------------
; Function FillACPI : Finds RDSP, and then RDST or XDST
;-------------------------------------------------------------------------------------------
FillACPI:
pushadxeax
push es
mov es,[fs:040eh]
xor edi,edi
mov di,[es:0]
pop es
mov edi, 0x000E0000
.s:
cmp edi, 0x000FFFFF ;
jge .noACPI ; Fail.
mov eax,[fs:edi]
add edi,4
mov edx,[fs:edi]
add edi,4
cmp eax,0x20445352
jnz .s
cmp edx,0x20525450
jnz .s
jmp .found
.noACPI:
popadxeax
mov EAX,0xFFFFFFFF
RETF
.found:
; Found at EDI
sub edi,8
mov esi,edi
; 36 bytes for ACPI 2
mov ecx,36
push cs
call ChecksumValid
cmp eax,1
jnz .noACPI2
mov eax,[fs:edi + 24]
mov dword [ds:XsdtAddress],eax
mov eax,[fs:edi + 28]
mov dword [ds:XsdtAddress + 4],eax
mov edi,dword [ds:XsdtAddress]
mov eax,[fs:edi]
cmp eax, 'XSDT' ; Valid?
jnz .noACPI2
popadxeax
RETF
.noACPI2:
mov edi,esi
mov ecx,20
push cs
call ChecksumValid
cmp eax,1
jnz .noACPI
mov eax,[fs:edi + 16]
mov dword [ds:XsdtAddress],eax
mov edi,dword [ds:XsdtAddress]
mov eax,[fs:edi]
cmp eax, 'RSDT' ; Valid?
jnz .noACPI
mov edi,dword [ds:XsdtAddress]
mov dword [ds:XsdtAddress],0
mov dword [ds:RsdtAddress],edi
popadxeax
RETF
;-------------------------------------------------------------------------------------------
; Function FindACPITableX : Finds EAX Table, edi is rsdt/xsdt address and ecx is 4 or 8
;-------------------------------------------------------------------------------------------
FindACPITableX:
cmp edi,0
jz .f
; len, must be more than 36
mov ebx,[fs:edi + 4]
cmp ebx,36
jle .f
sub ebx,36
xor edx,edx
.loop:
cmp edx,ebx
jz .f
mov esi,[fs:edi + 36 + edx]
cmp eax,[fs:esi]
jnz .c
mov eax,esi
RETF
.c:
add edx,ecx
jmp .loop
.f:
mov eax,0ffffffffh
RETF
;-------------------------------------------------------------------------------------------
; Function DumpMadt : Fills from EAX MADT
;-------------------------------------------------------------------------------------------
DumpMadt: ; EAX
pushad
mov edi,eax
mov [ds:numcpus],0
mov ecx,[fs:edi + 4] ; length
mov eax,[fs:edi + 0x24] ; Local APIC
mov [ds:LocalApic],eax
add edi,0x2C
sub ecx,0x2C
.l1:
xor ebx,ebx
mov bl,[fs:edi + 1] ; length
cmp bl,0
jz .end ; duh
sub ecx,ebx
mov al,[fs:edi] ; type
cmp al,0
jnz .no0
; This is a CPU!
xor eax,eax
mov al,[ds:numcpus]
inc [ds:numcpus]
mov edx,cpusstructize
mul edx
xor esi,esi
mov si,cpus
add esi,eax
mov al,[fs:edi + 2]; ACPI id
mov byte [ds:si],al
mov al,[fs:edi + 3]; APIC id
mov byte [ds:si + 4],al
.no0:
add edi,ebx
jecxz .end
jmp .l1
.end:
popad
RETF
;-------------------------------------------------------------------------------------------
; Function DumpAll ; edi = xsdt or rsdt
;-------------------------------------------------------------------------------------------
DumpAll:
mov ebp, 32 ; max count
cmp edi,0
jz .f
; len, must be more than 36
mov ebx,[fs:edi + 4]
cmp ebx,36
jle .f
sub ebx,36
xor edx,edx
.loop:
cmp edx,ebx
jz .f
mov esi,[fs:edi + 36 + edx]
mov eax,[fs:esi]
; eax to show
push edx
mov edx,eax
mov ah,2
int 21h
shr edx,8
mov ah,2
int 21h
shr edx,8
mov ah,2
int 21h
shr edx,8
mov ah,2
int 21h
mov dl,' ';
mov ah,2
int 21h
pop edx
add edx,ecx
dec ebp
cmp ebp, 0
jz .f
jmp .loop
.f:
RETF
;-------------------------------------------------------------------------------------------
; Function SendSIPIf : Sends SIPI. EBX = CPU Index, EAX = linear
;-------------------------------------------------------------------------------------------
SendSIPIf:
PUSHAD
PUSH DS
mov cx,DATA16
mov ds,cx
XOR ECX,ECX
; Spurious
MOV EDI,[DS:LocalApic]
ADD EDI,0x0F0
MOV EDX,[FS:EDI]
OR EDX,0x1FF
MOV [FS:EDI],EDX
; Vector
.L1:
MOV ECX,EAX
TEST EAX,0xFFF
JZ .L2
INC EAX
JMP .L1
.L2:
MOV ESI,EAX
SHR ESI,12
; Init
MOV ECX,0x04500
OR ECX,ESI
push cs
call SendIPI16
; Delay 10 ms = 0,01 s = (100 Hz)
; 1193182/100
; sleep16 11931
MOV AH,86H
MOV CX,0
MOV DX,10*1000 ;10 ms
INT 15H
; SIPI 1
MOV ECX,0x05600
OR ECX,ESI
push cs
call SendIPI16
; Delay 200 us = 0,2 ms = 0,0002 s = (5000 Hz)
; 1193182/5000
; sleep16 238
MOV AH,86H
MOV CX,0
MOV DX,200 ; 200us
INT 15H
; SIPI 2
MOV ECX,0x05600
OR ECX,ESI
push cs
call SendIPI16
POP DS
POPAD
RETF
;-------------------------------------------------------------------------------------------
; Function SendIPI16 : Sends IPI. EBX = CPU Index, ECX = IPI
;-------------------------------------------------------------------------------------------
SendIPI16: ; EBX = CPU INDEX, ECX = IPI
PUSHAD
; Lock Mutex
mov ax,mut_ipi
push cs
call qwaitlock16
; Write it to 0x310
; EBX is CPU INDEX
; MAKE IT APIC ID
xor eax,eax
mov ax,cpusstructize
mul bx
add ax,cpus
mov di,ax
add di,4
mov bl,[ds:di]
MOV EDI,[DS:LocalApic]
ADD EDI,0x310
MOV EDX,[FS:EDI]
AND EDX,0xFFFFFF
XOR EAX,EAX
MOV AL,BL
SHL EAX,24
OR EDX,EAX
MOV [FS:EDI],EDX
; Write it to 0x300
MOV EDI,[DS:LocalApic]
ADD EDI,0x300
MOV [FS:EDI],ECX
; Verify it got delivered
.Verify:
PAUSE
MOV EAX,[FS:EDI];
SHR EAX,12
TEST EAX,1
JNZ .Verify
; Write it to 0xB0 (EOI)
; MOV EDI,[DS:LocalApic]
; ADD EDI,0xB0
; MOV dword [FS:EDI],0
; Release Mutex
qunlock16 mut_ipi
POPAD
RETF