📄 cw32.asm
字号:
call MakeDesc
;
;Kernal (RAW) data seg.
;
xor esi,esi
mov si,_cwRaw
shl esi,4
mov ecx,65535
xor al,al
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,KernalDS
call MakeDesc
;
;Kernal (RAW) stack seg.
;
movzx esi,RawStackReal
shl esi,4
mov ecx,65535
mov al,b[RawSystemFlags]
xor al,1
shl al,6
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,KernalSS
call MakeDesc
;
;Kernal PL3 to PL0 call gate.
;
xor ecx,ecx
mov esi,KernalCS0
xor al,al
mov ah,DescPresent+DescPL3+Desc386Call
mov di,KernalPL3_2_PL0
call MakeDesc
;
;DPMI emulator code seg.
;
xor esi,esi
mov si,_cwDPMIEMU
shl esi,4
mov ecx,offset cwDPMIEMUEnd-cwDPMIEMUStart
mov al,1 shl 6
mov ah,DescPresent+DescPL3+DescMemory+DescERCode
mov di,DpmiEmuCS
call MakeDesc
;
;DPMI emulator code seg at PL0
;
xor esi,esi
mov si,_cwDPMIEMU
shl esi,4
mov ecx,offset cwDPMIEMUEnd-cwDPMIEMUStart
mov al,1 shl 6
mov ah,DescPresent+DescPL0+DescMemory+DescERCode
mov di,DpmiEmuCS0
call MakeDesc
;
;DPMI emulator data seg.
;
xor esi,esi
mov si,_cwDPMIEMU
shl esi,4
mov ecx,offset cwDPMIEMUEnd-cwDPMIEMUStart
mov al,1 shl 6
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,DpmiEmuDS
call MakeDesc
;
;Init PL3 to PL0 call gate.
;
xor ecx,ecx
mov esi,InitCS0
xor al,al
mov ah,DescPresent+DescPL3+Desc386Call
mov di,InitPL3_2_PL0
call MakeDesc
;
;DPMI emulator PL3 to PL0 call gate.
;
xor ecx,ecx
mov esi,DpmiEmuCS0
xor al,al
mov ah,DescPresent+DescPL3+Desc386Call
mov di,DpmiEmuPL3_2_PL0
call MakeDesc
;
;Zero to 4G segment.
;
xor esi,esi
or ecx,-1
xor al,al
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,KernalZero
call MakeDesc
;
;Main PSP segment.
;
push ds
mov ax,_cwMain
mov ds,ax
assume ds:_cwMain
movzx esi,RealPspSegment
assume ds:_cwRaw
pop ds
shl esi,4
mov ecx,256
mov al,0
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,MainPSP
call MakeDesc
;
;Main environment var.
;
push ds
mov ax,_cwMain
mov ds,ax
assume ds:_cwMain
movzx esi,RealEnvSegment
assume ds:_cwRaw
pop ds
shl esi,4
mov ecx,0FFFFh
xor al,al
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,MainEnv
call MakeDesc
;
;Main code seg.
;
xor esi,esi
mov si,_cwMain
shl esi,4
mov ecx,65535
xor al,al
mov ah,DescPresent+DescPL3+DescMemory+DescERCode
mov di,MainCS
call MakeDesc
;
;Main data seg.
;
xor esi,esi
mov si,_cwMain
shl esi,4
mov ecx,65535
xor al,al
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,MainDS
call MakeDesc
;
;Main stack.
;
xor esi,esi
mov si,_cwStack
shl esi,4
mov ecx,65535
mov al,b[RawSystemFlags]
xor al,1
shl al,6
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,MainSS
call MakeDesc
;
;Init code seg.
;
xor esi,esi
mov si,_cwInit
shl esi,4
mov ecx,65535
xor al,al
mov ah,DescPresent+DescPL3+DescMemory+DescERCode
mov di,InitCS
call MakeDesc
;
;Init code seg at PL0
;
xor esi,esi
mov si,_cwInit
shl esi,4
mov ecx,65535
xor al,al
mov ah,DescPresent+DescPL0+DescMemory+DescERCode
mov di,InitCS0
call MakeDesc
;
;Init data seg.
;
xor esi,esi
mov si,_cwInit
shl esi,4
mov ecx,65535
xor al,al
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,InitDS
call MakeDesc
;
;Init GDT data alias.
;
mov esi,GDTLinear
mov ecx,65535
xor al,al
mov ah,DescPresent+DescPL3+DescMemory+DescRWData
mov di,GDTData
call MakeDesc
;
;Setup TSS registers to run in protected mode. Setup GDT & IDT values.
;
mov es,KernalTSSReal
xor di,di
mov cx,size TSSFields ;+(4096/8)
xor al,al
cld
rep stosb ;clear TSS & IO map.
or ax,-1
stosw ;mark end of IO map.
mov es:SS0,KernalPL0
mov es:ESP0,tPL0StackSize-4
mov es:tLDT,KernalLDT
mov es:IOMap,size TSSFields ;set displacement to IO table.
;
;Setup GDT load value.
;
movzx eax,GDTReal
shl eax,4
mov d[GDTVal+2],eax
mov w[GDTVal],-1
;
;Setup Windows enhanced mode denial patch.
;
mov ax,352fh ;get existing vector.
int 21h
mov w[OldInt2F],bx
mov w[OldInt2F+2],es
mov ax,252fh
mov dx,offset Int2FPatch
int 21h
;
;Now patch RAW specific calls.
;
push ds
mov ax,_cwMain
mov ds,ax
assume ds:_cwMain
cmp ProtectedType,1 ;VCPI?
assume ds:_cwRaw
pop ds
jz @@VCPI
;
;Use RAW method to switch to protected mode.
;
@@RAW: .386p
;
;Need to initialise 1st entry of page dir & alias.
;
movzx eax,Page1stReal
shl eax,4
or eax,111b ;user+write+present
mov es,PageDirReal
xor di,di
mov es:[di],eax
mov es,PageAliasReal
mov es:[di],eax
mov es,KernalTSSReal
mov es:[di].tCR3,eax ;set CR3 in TSS as well.
;
;map alias into page dir as well.
;
movzx eax,PageAliasReal ;get para address.
shl eax,4 ;make linear.
or eax,111b ;user+write+present.
mov es,PageDirReal
mov di,1023*4
mov es:[di],eax ;setup in last page dir entry.
;
; pushfd
; pop eax
; mov EFlagsSav,eax
mov eax,cr3
mov CR3Sav,eax
mov eax,cr0
mov CR0Sav,eax
sidt IDTSav ;save old IDT value for switch back.
sgdt GDTSav
pop RetAdd
cli ;Don't want interupts interfering.
lgdt GDTVal ;Setup GDT &
lidt f[IDTVal] ;IDT.
mov eax,VCPI_CR3
mov cr3,eax ;set page dir address.
mov eax,cr0 ;Get machine status &
or eax,080000001h ;set PM+PG bits.
mov cr0,eax ;/
; MED 10/15/96
mov CR0ProtSav,eax ; save protected mode status of CR0
db 0eah ;Absolute 16-bit jump, to clear
dw @@RAW0,InitCS0 ;instruction pre-fetch & load CS.
@@RAW0: mov ax,KernalLDT ;Point to empty LDT descriptor.
lldt ax ;and set LDT.
mov cx,KernalTS ;Get value for task register.
ltr cx ;and set it.
;
mov Protected2Real,offset RawProt2Real
mov Real2Protected,offset RawReal2Prot
jmp @@inProt
;
;Use VCPI method to switch to protected mode.
;
@@VCPI: mov cs:IErrorNumber,6
cli
push ds
xor di,di ;Page table offset.
mov es,Page1stReal ;Page table segment
mov si,VCPI_0 ;VCPI GDT entries offset.
and si,not 3
mov ds,GDTReal ;GDT segment.
mov ax,0de01h ;Let VCPI server prepare.
int 67h
pop ds
or ah,ah
jnz InitError
mov d[VCPI_Entry],ebx ;Store entry point.
; MED 11/05/96
mov FirstUninitPage,di ; VCPI server advanced to first uninitialized page
; table entry in client's page
;
;Now update PHYSICAL addresses of dir & 1st page tables.
;
mov es,Page1stReal
movzx edi,Page1stReal ;get linear address.
shl edi,4 ;/
shr edi,12 ;page number.
shl edi,2 ;*4 bytes per entry.
mov eax,es:[di] ;get physical address.
and eax,not 4095 ;clear status bits.
or eax,111b ;set our bits.
mov es,PageDirReal
xor di,di
mov es:[di],eax
mov es,PageAliasReal
mov es:[di],eax
;
mov es,Page1stReal
movzx edi,PageDirReal ;get linear address.
shl edi,4 ;/
shr edi,12 ;page number.
shl edi,2 ;*4 bytes per entry.
mov eax,es:[di] ;get physical address.
and eax,0FFFFFFFFh-4095 ;clear status bits.
mov VCPI_CR3,eax ;set VCPI CR3 value as well.
mov es,KernalTSSReal
xor di,di
mov es:[di].tCR3,eax ;set CR3 in TSS as well.
;
mov es,Page1stReal
movzx edi,PageAliasReal ;get linear address.
shl edi,4 ;/
shr edi,12 ;page number.
shl edi,2 ;*4 bytes per entry.
mov eax,es:[di] ;get physical address.
and eax,0FFFFFFFFh-4095 ;clear status bits.
or eax,111b ;user+write+present.
mov es,PageDirReal
mov di,1023*4
mov es:[di],eax ;setup in last page dir entry.
;
mov VCPI_LDT,KernalLDT
mov VCPI_EIP,offset @@InProt
mov VCPI_TR,KernalTS ;Get value for task register.
mov VCPI_CS,InitCS0
xor eax,eax
mov ax,seg _cwRaw
shl eax,4
add eax,offset GDTVal
mov VCPI_pGDT,eax
xor eax,eax
mov ax,seg _cwRaw
shl eax,4
add eax,offset IDTVal
mov VCPI_pIDT,eax
cli
mov ax,0de0ch
mov si,seg _cwRaw
movzx esi,si
shl esi,4
add esi,offset VCPI_CR3
int 67h
;
mov ax,_cwStack
mov ss,ax
mov esp,offset _cwStackEnd-4
mov ax,_cwRaw
mov ds,ax
jmp InitError ;Shouldn't come through here.
;
;We're in protected mode at last, now we just have to move the DPMI emulation
;stuff into extended memory and build a useful IDT.
;
@@InProt:
mov ax,InitDS
mov ds,ax
assume ds:_cwInit
or IProtectedMode,-1
mov ax,KernalDS ;Get data descriptor.
mov ds,ax ;/
assume ds:_cwRaw
mov es,ax ;/
mov fs,ax ;/
mov gs,ax ;/
mov ax,KernalPL0
mov ss,ax ;/
mov esp,offset tPL0StackSize-4
;
pushfd
pop eax
and ax,1011111111111111b ;clear NT.
push eax
popfd
cld
clts
;
;Switch to PL3 code seg for the hell of it.
;
.386
mov edx,offset _cwStackEnd-4
mov ecx,MainSS
push ecx ;SS
push edx ;ESP
pushfd ;EFlags
pop eax
and ax,1000111111111111b ;clear NT & IOPL.
or ax,0011000000000000b ;force IOPL.
push eax
popfd
push eax
xor eax,eax
mov ax,InitCS
push eax ;CS
mov eax,offset @@pl3
push eax ;EIP
db 66h
iretd
;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -