📄 apic.asm
字号:
.586p
include pt.inc
sseg segment stack use16
db 8 dup ('stack===')
sseg ends
dseg segment use16
gdt_begin label byte
null_desc dq 0 ;0
scode seg_desc {0ffffh,0,0,9ah,0cfh,0} ;8
sdata seg_desc {0ffffh,0,0,92h,0cfh,0} ;10h
rcode seg_desc {0ffffh,0,0,9ah,0,0} ;18h
rdata seg_desc {0ffffh,0,0,92h,0,0} ;20h
icode seg_desc {0ffffh,0,0,9ah,0cfh,0} ;28h
tdata seg_desc {0ffffh,0,0,92h,0cfh,0} ;30h
gdt_end label byte
gdtr pseudo_desc {gdt_end-gdt_begin-1,0}
idtr pseudo_desc {idt_end-idt_begin-1,0}
real_idt df 0
real_ss dd 0
pic0 db 0
pic1 db 0
dseg ends
idt segment use16
idt_begin label byte
rept 20h
gate {0,28h,0,8eh,0}
endm
intx20 gate {0,28h,0,8eh,0}
intx21 gate {0,28h,0,8eh,0}
idt_end label byte
idt ends
temp segment use32
timer_count dd 0
smp_cpuid0 db 0
smp_cpuid1 db 0
temp ends
cseg segment use16
assume cs:cseg,ds:dseg,ss:sseg
start:
mov cx,dseg
mov ds,cx
imul eax,ecx,16
mov gdtr.base_address,eax
mov cx,temp
imul eax,ecx,16
mov tdata.base_0_15,ax
shr eax,16
mov tdata.base_16_23,al
mov tdata.base_24_31,ah
sidt real_idt
mov ax,ss
shl eax,16
mov ax,sp
mov real_ss,eax
mov cx,cseg
imul eax,ecx,16
mov rcode.base_0_15,ax
shr eax,16
mov rcode.base_16_23,al
mov rcode.base_24_31,ah
mov cx,text
imul eax,ecx,16
add eax,offset start_32
; mov prot_addr,eax
mov scode.base_0_15,ax
shr eax,16
mov scode.base_16_23,al
mov scode.base_24_31,ah
mov cx,idt
mov es,cx
imul eax,ecx,16
mov idtr.base_address,eax
mov cx,intseg
imul eax,ecx,16
;add eax,offset intx00
mov edx,eax
mov ebx,17dfbh ;control byte
mov ecx,20h
mov di,offset idt_begin
start1:
shr ebx,1
jnc start2
push edx
mov es:[di],dx
shr edx,16
mov es:[di+6],dx
pop edx
start2:
add edx,4
lea di,es:[di+8]
loop start1
mov edx,eax
add edx,offset timer
mov es:intx20.entry_0_15,dx
shr edx,16
mov es:intx20.entry_16_31,dx
mov edx,eax
add edx,offset keyb
mov es:intx21.entry_0_15,dx
shr edx,16
mov es:intx21.entry_16_31,dx
seta20:
cli
in al,64h
test al,2
jnz seta20
mov al,0d1h
out 64h,al
seta201:
in al,64h
test al,2
jnz seta201
mov al,0dfh
out 60h,al
in al,70h
or al,80h
out 70h,al
in al,21h
mov pic0,al
in al,0a1h
mov pic1,al
mov al,0ffh
out 21h,al
out 0a1h,al
mov bx,2820h
call setpic
push ds ;mov ap cpu code to 90000h
push es
mov ax,apcode
mov ds,ax
mov ax,9000h
mov es,ax
mov si,offset start_ap
xor di,di
mov cx,ap_length
cld
rep movsb
pop es
pop ds
cli
lidt idtr
lgdt gdtr
mov eax,cr0
or eax,1
mov cr0,eax
; jmp fword ptr prot_addr
far_jump 8h,0
return_real:
;pic mode
mov al,70h
out 22h,al ; select IMCR
xor al,al
out 23h,al ; connect 8259s/NMI
mov esi,0fee00350h ;LINT0
mov eax,[esi]
and eax,0fffe58ffh
or eax,700h
mov [esi],eax
mov esi,0fee00360h ;LINT1
mov eax,[esi]
and eax,0fffe58ffh
or eax,400h
mov [esi],eax
mov ax,20h
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
mov ss,ax
mov eax,cr0
and eax,7ffffffeh
; and eax,0fffffffeh
mov cr0,eax
xor eax,eax
mov cr3,eax
far_jump <seg return_dos>,<offset return_dos>
return_dos:
cli
mov ax,dseg
mov ds,ax
mov bx,7008h
call setpic
lidt real_idt
lss sp,real_ss
mov al,pic0
out 21h,al
mov al,pic1
out 0a1h,al
close20:
; in al,64h
; test al,2
; jnz close20
; mov al,0d1h
; out 64h,al
close201:
; in al,64h
; test al,2
; jnz close201
; mov al,0ddh
; out 60h,al
xor al,al
out 70h,al
mov dl,'2'
mov ah,2
int 21h
sti
mov ah,4ch
int 21h
setpic:
mov al,11h
out 20h,al ;icw1 to master
out 0a0h,al ;to slave
mov al,bl ;icw2
out 21h,al
mov al,bh
out 0a1h,al
mov al,4 ;icw3
out 21h,al
mov al,2
out 0a1h,al
mov al,1 ;icw4
out 21h,al
out 0a1h,al
ret
cseg ends
text segment use32
assume cs:text,gs:temp
start_32:
mov ax,10h
mov ds,ax
mov es,ax
mov fs,ax
mov ss,ax
mov esp,40000h
mov ax,30h
mov gs,ax
cld
in al,70h
and al,7fh
out 70h,al
;
;created page dir
;
init:
mov ecx,8
mov eax,21000h
or eax,7
mov edx,1000h
mov edi,20000h
init1:
stosd
add eax,edx
loop init1
mov edi,20000h
mov eax,29000h
or eax,3
mov es:[edi+0fech],eax ;apic
;
;created page table
;
mov ecx,1024
xor eax,eax
or al,7
mov edi,21000h
init2:
stosd
add eax,edx
loop init2
mov edi,29000h
mov eax,0fec0007bh
init3:
mov es:[edi],eax
add edi,4
add eax,1000h
cmp edi,30000h
jne init3
mov eax,20000h
mov cr3,eax
mov eax,cr0
or eax,80000000h
mov cr0,eax
jmp $+2
mov esi,0fee00020h ;Local APIC ID Register
mov eax,[esi]
and eax,00f000000h
shr eax,24
mov smp_cpuid0,al
;
;leave pic mode
;
mov al,70h
out 22h,al ; select IMCR
in al,23h ; current contents
or al,1 ; mask external INTR
out 23h,al ; disconnect 8259s/NMI
mov esi,0fee000d0h ;Logical Destination Register (LDR)
mov eax,10h ;logical ID
mov [esi],eax
mov eax,esi
; mov esi,0fee000f0h ;Spurious-Interrupt Vector Register (SVR)
; mov eax,[esi]
; or eax,200h ;Focus Processor Checking disable
; mov [esi],eax
mov esi,0fee00080h ;Task Priority Register (TPR)
mov eax,10h
mov [esi],eax
mov esi,0fee00090h ;Arbitration Priority Register (APR)
mov eax,0h
mov [esi],eax
; mov esi,0fee000b0h
; xor eax,eax
; mov [esi],eax
mov esi,0fee00350h ;LINT0
mov eax,10000h
mov [esi],eax ;mask
mov esi,0fee00360h ;LINT1
mov eax,400h ;NMI
mov [esi],eax
; mov esi,0fee00370h ;LVT Error Register
; mov eax,10000h
; mov [esi],eax ;mask
; mov esi,0fee00340h ;LVT Performance Counter Register
; mov eax,10000h
; mov [esi],eax ;mask
mov esi,0fec00000h
mov eax,10h
mov [esi],eax
mov esi,0fec00010h
mov eax,00008120h
mov [esi],eax
mov esi,0fec00000h
mov eax,11h
mov [esi],eax
mov esi,0fec00010h
mov eax,10000000h
mov [esi],eax
mov esi,0fec00000h
mov eax,12h
mov [esi],eax
mov esi,0fec00010h
mov eax,00008121h
mov [esi],eax
mov esi,0fec00000h
mov eax,13h
mov [esi],eax
mov esi,0fec00010h
mov eax,10000000h
mov [esi],eax
xor eax,eax
mov esi,0fee00280h ;ESR
mov [esi],eax
mov eax,[esi]
mov esi,0fee00300h
mov eax,0c0500h ;INIT IPI
mov [esi],eax
mov ecx,4800
call delay
mov esi,0fee00300h
mov eax,90000h
shr eax,12
or eax,0c0600h ;startup IPI
mov [esi],eax
mov eax,[esi]
mov ecx,4800
call delay
mov esi,0fee000e0h ;Destination Format Register (DFR)
; mov eax,[esi]
xor eax,eax
or eax,0f0000000h
mov [esi],eax
sti
wait_tick:
cmp gs:timer_count,90000h
jne wait_tick
task_end:
cli
mov ebx,0b8500h
mov eax,82418242h
mov [ebx],eax
long_jump 18h,return_real
hex321:
rol eax,4
mov edx,eax
and al,0fh
add al,30h
cmp al,39h
ja add371
jmp print1
add371:
add al,7
print1:
mov ah,4
mov [ebx],ax
add ebx,2
mov eax,edx
loop hex321
ret
Delay:
push eax
delay1:
in al,80h
loop delay1
pop eax
ret
text ends
intseg segment use32
assume cs:intseg,gs:temp
intx00:
push 0
jmp exec
push 1
jmp exec
push 2
jmp exec ;NMI interrupt
push 3
jmp exec
push 4
jmp exec
push 5
jmp exec
push 6
jmp exec
push 7
jmp exec
push 8
jmp exec
push 9 ;FPU seg overrun
jmp exec
push 0ah
jmp exec
push 0bh
jmp exec
push 0ch
jmp exec
push 0dh
jmp ex_noc
push 0eh
jmp exec
push 0fh ;reserved
jmp exec
push 10h
jmp exec
ex_noc:
exec:
pushad
push ds
mov ax,10h
mov ds,ax
mov eax,[esp]
mov ebx,0b8000h
mov ecx,8
call hex32
add ebx,4
mov eax,[esp+4]
mov ecx,8
call hex32
add ebx,4
mov eax,[esp+8]
mov ecx,8
call hex32
add ebx,4
mov eax,[esp+12]
mov ecx,8
call hex32
add ebx,4
mov eax,[esp+16]
mov ecx,8
call hex32
add ebx,4
mov eax,[esp+20]
mov ecx,8
call hex32
pop ds
popad
long_jump 18h,<offset return_real>
timer:
pushad
push gs
mov ax,30h
mov gs,ax
inc gs:timer_count
mov eax,gs:timer_count
mov ebx,0b8100h
mov ecx,8
call hex32
mov esi,0fee000b0h
xor eax,eax
mov [esi],eax
pop gs
popad
iretd
keyb:
pushad
push gs
mov ax,30h
mov gs,ax
xor eax,eax
in al,60h
mov ebx,0b8110h
mov ecx,8
call hex32
mov esi,0fee000b0h
xor eax,eax
mov [esi],eax
pop gs
popad
iretd
hex32:
rol eax,4
mov edx,eax
and al,0fh
add al,30h
cmp al,39h
ja add37
jmp print
add37:
add al,7
print:
mov ah,2
mov [ebx],ax
add ebx,2
mov eax,edx
loop hex32
ret
intseg ends
apcode segment use16
assume cs:apcode,ds:dseg
org 0
start_ap:
mov ax,dseg
mov ds,ax
cli
lidt idtr
lgdt gdtr
mov eax,cr0
or eax,1
mov cr0,eax
jmp $+2
mov ax,10h
mov ds,ax
mov es,ax
mov fs,ax
mov ss,ax
mov esp,50000h
mov ax,30h
mov gs,ax
mov eax,20000h
mov cr3,eax
mov eax,cr0
or eax,80000000h
mov cr0,eax
push protmode
ret
protmode:
; sti
mov esi,0fee00020h ;Local APIC ID Register
mov eax,[esi]
and eax,00f000000h
shr eax,24
mov gs:smp_cpuid1,al
mov ebx,0b8200h
mov ecx,8
call hex322
add_esi:
inc eax
mov [esi],eax
cmp eax,0fffffh
; jne add_esi
mov ecx,1bh
rdmsr
add ebx,4
mov ecx,8
call hex322
hlt
mov ecx,1bh
rdmsr
mov ebx,0b8300h
mov ecx,8
call hex322
hex322:
rol eax,4
mov edx,eax
and al,0fh
add al,30h
cmp al,39h
ja add372
jmp print2
add372:
add al,7
print2:
mov ah,2
mov [ebx],ax
add ebx,2
mov eax,edx
loop hex322
ret
ap_length = $
base equ 90000h
apcode ends
end start
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -