cw32.asm

来自「开放源码的编译器open watcom 1.6.0版的源代码」· 汇编 代码 · 共 2,222 行 · 第 1/5 页

ASM
2,222
字号
END COMMENT !

;
;Set address for DOS INT 21h PM to Real transfer buffer.
;

; MED 09/19/96
;       mov     bx,Page1stReal
        mov     bx,PageDirReal

        push    ds
        mov     ax,_cwMain
        mov     ds,ax
        assume ds:_cwMain
        mov     TransferReal,bx
        assume ds:_cwRaw
        pop     ds
;
;Allocate the GDT entries.
;
        mov     es,GDTReal
;
;Fill in the null entry just for the hell of it.
;
        xor     esi,esi         ;Null entry at 0.
        xor     ecx,ecx
        xor     ax,ax
        xor     di,di
        call    MakeDesc
;
;Fill in the VCPI entries so we don't try to use them later.
;
        xor     esi,esi
        xor     ecx,ecx
        mov     ah,DescPresent+DescPL3+DescMemory+DescRWData
        mov     di,VCPI_0
        call    MakeDesc
        xor     esi,esi
        xor     ecx,ecx
        mov     ah,DescPresent+DescPL3+DescMemory+DescRWData
        mov     di,VCPI_1
        call    MakeDesc
        xor     esi,esi
        xor     ecx,ecx
        mov     ah,DescPresent+DescPL3+DescMemory+DescRWData
        mov     di,VCPI_2
        call    MakeDesc
;
;Allocate 40h descriptor.
;
        mov     esi,400h
        mov     ecx,65535
        xor     al,al
        mov     ah,DescPresent+DescPL3+DescMemory+DescRWData
        mov     di,Kernal40h
        call    MakeDesc
;
;Allocate kernal task descriptors, TSS itself first.
;
        movzx   esi,KernalTSSReal
        shl     esi,4
        mov     ecx,size TSSFields+2
        xor     al,al
        mov     ah,DescPresent+DescPL3+Desc386Tss
        mov     di,KernalTS
        call    MakeDesc
;
;TSS PL0 stack.
;
        movzx   esi,KernalTSSReal
        shl     esi,4
        add     esi,TSSFields.tPL1Stack

        mov     ecx,65535
        mov     al,b[RawSystemFlags]
        xor     al,1
        shl     al,6
        mov     ah,DescPresent+DescPL0+DescMemory+DescRWData
        mov     di,KernalPL0
        call    MakeDesc
;
;Mode switch PL0 stack.
;
        movzx   esi,KernalTSSReal
        shl     esi,4
        add     esi,TSSFields.tPL1Stack

        mov     ecx,65535
        xor     al,al
        mov     ah,DescPresent+DescPL0+DescMemory+DescRWData
        mov     di,KernalSwitchPL0
        call    MakeDesc
;
;LDT
;
        xor     esi,esi
        xor     ecx,ecx
        xor     al,al
        mov     ah,DescPresent+DescPL3+DescLDT
        mov     di,KernalLDT
        call    MakeDesc
;
;Kernal (RAW) code seg.
;
        xor     esi,esi
        mov     si,_cwRaw
        shl     esi,4
        mov     ecx,65535
        xor     al,al
        mov     ah,DescPresent+DescPL3+DescMemory+DescERCode
        mov     di,KernalCS
        call    MakeDesc
;
;Kernal (RAW) code seg at PL0
;
        xor     esi,esi
        mov     si,_cwRaw
        shl     esi,4
        mov     ecx,65535
        xor     al,al
        mov     ah,DescPresent+DescPL0+DescMemory+DescERCode
        mov     di,KernalCS0
        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:TSSFields.SS0,KernalPL0
        mov     es:TSSFields.ESP0,tPL0StackSize-4
        mov     es:TSSFields.tLDT,KernalLDT
        mov     es:TSSFields.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      cw5_VCPI
;
;Use RAW method to switch to protected mode.
;
cw5_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].TSSFields.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 cw5_RAW0,InitCS0             ;instruction pre-fetch & load CS.
cw5_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     cw5_inProt
;
;Use VCPI method to switch to protected mode.
;
cw5_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.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?