;      SCCSID = @(#)fartnks.asm 6.13 92/03/06

        TITLE   fartnks - Far Thunk entry points for 32 bit workers
        page    ,132

;       IBM/Microsoft Confidential
;
;       Copyright (c) IBM Corporation 1987, 1989
;       Copyright (c) Microsoft Corporation 1987, 1989
;
;       All Rights Reserved
;d75220 added 32SETEXTLIBPATH and 32QUERYEXTLIBPATH
;
; 88344 Roll over defect 81849 from R206

;***  Change History                                              ***
;***   noto  11/18/93  Define which segment for Page Tuning DLL   ***
;***   W.Gu  07/11/96  Accurate CPU Initialization                ***
;***                                                              ***



.xlist
include mi.inc
include basemaca.inc
include dc1segs.inc                                             ; DCR 704
.list

.386p


;***    FarThunk - Define a far thunk
;
;       FarThunk takes a base routine name and generates an
;       entry point with DOS prepended to the base name, followed
;       by a far call to a kernel entry point.  The kernel entry
;       point will be the base name with T prepended.
;
;       FarThunk macro  entryname,int3
;
;               entryname - base name for entry point
;                       (eg for DOS32ALLOCMEM, the basename would be
;                               32ALLOCMEM)
;               int3      - generate an int 3 upon entry to the far thunk
;                               (for debugging purposes)

FarThunk macro  entryname,int3

        public  DOS&entryname           ;; this is the name used by apps
        extrn   T&entryname:far         ;; kernel entry point (call gate)

Procedure DOS&entryname
    ifnb <int3>
        int     3
    endif
        call    T&entryname             ;; generate far call to kernel
        ret
EndProc DOS&entryname

endm


;***    FarThunk16 - Define a far thunk from 16bit code
;
;       FarThunk16 takes a base routine name and generates an
;       entry point with DOS prepended to the base name, followed
;       by a far call to a kernel entry point.  The kernel entry
;       point will be the base name with T prepended.
;
;       FarThunk16 macro  entryname,int3
;
;               entryname - base name for entry point
;                       (eg for DOS32ALLOCMEM, the basename would be
;                               32ALLOCMEM)
;               int3      - generate an int 3 upon entry to the far thunk
;                               (for debugging purposes)

FarThunk16 macro  entryname,int3

        public  _DOS&entryname           ;; this is the name used by apps
        extrn   T&entryname:far         ;; kernel entry point (call gate)

Procedure _DOS&entryname,far
    ifnb <int3>
        int     3
    endif
        call    T&entryname             ;; generate far call to kernel
        retf
EndProc _DOS&entryname

endm

        extrn InitDOSCALL1:far

;***    INITSTK - Stack for DOSCALL1 initialization code
;
;       Stack segment to be used by the 16-bit initialization routine
;       (InitDOSCALL1) in order to avoid a stack segment overrun or not
;       present fault (trap C) that may occur by thunking the 32-bit ESP

INITSTK segment
        dw      2048 dup (?)
INITSTKBASE     label   dword           ; Initial SP
INITSTK ends


CODE16_TUNE1 SEGMENT DWORD USE16 PUBLIC 'CODE'
CODE16_TUNE1 ENDS

CODE16C      SEGMENT DWORD USE16 PUBLIC 'CODE'
CODE16C      ENDS

CODE32_TUNE1 SEGMENT DWORD USE32 PUBLIC 'CODE'
CODE32_TUNE1 ENDS

CODE32       SEGMENT DWORD USE32 PUBLIC 'CODE'
CODE32       ENDS




CODE16C segment                                                  ; DCR 704
    assume cs:CODE16C
        FarThunk16      32IMONREAD
        FarThunk16      32IMONWRITE

CODE16C ends


CODE32_TUNE1 segment                                ; PAGE TUNING DOSCALL1
    assume cs:FLAT, ds:FLAT, es:FLAT, ss:FLAT, fs:nothing, gs:nothing
        FarThunk        32RELEASEMUTEXSEM
        FarThunk        32REQUESTMUTEXSEM
        FarThunk        32GIVESHAREDMEM
        FarThunk        32OPENEVENTSEM
        FarThunk        32GETRESOURCE
        FarThunk        32QUERYMUTEXSEM
        FarThunk        32QUERYDOSPROPERTY
        FarThunk        32ALLOCMEM
        FarThunk        32SETMEM
        FarThunk        32FREEMEM
        FarThunk        32POSTEVENTSEM
        FarThunk        32EXITLIST
        FarThunk        32WAITEVENTSEM
        FarThunk        32IREAD
        FarThunk        32RESETEVENTSEM
        FarThunk        32QUERYPROCADDR
        FarThunk        32OPENMUTEXSEM
        FarThunk        32REQUESTVDD
        FarThunk        32QUERYMEM
        FarThunk        32QUERYPROCTYPE
        FarThunk        32GETSHAREDMEM
        FarThunk        32FREERESOURCE
        FarThunk        32ALLOCSHAREDMEM
        FarThunk        32OPENVDD
        FarThunk        32GETNAMEDSHAREDMEM
        FarThunk        32CREATEMUTEXSEM
        FarThunk        32DEBUG

        FarThunk        32VERIFYPIDTID       ; 88344

        FarThunk        32CREATEEVENTSEM
        FarThunk        32CREATETHREAD

; Add entries for the PM Semaphore calls

        FarThunk        32PMPOSTEVENTSEM
;                       32PMPOSTEVENTSEM

        FarThunk        32PMWAITEVENTSEM
;                       32PMWAITEVENTSEM

        FarThunk        32PMWAITMUXWAITSEM
;                       32PMWAITMUXWAITSEM

        FarThunk        32PMREQUESTMUTEXSEM
;                       32PMREQUESTMUTEXSEM

        FarThunk        32PM16SEMCHK
        FarThunk        32PM16SEMRST

CODE32_TUNE1 ends


CODE32 segment                                                  ; DCR 704
    assume cs:FLAT, ds:FLAT, es:FLAT, ss:FLAT, fs:nothing, gs:nothing


        FarThunk        32ADDMUXWAITSEM
        FarThunk        32CLOSEEVENTSEM
        FarThunk        I32ASYNCTIMER
        FarThunk        32ALIASPERFCTRS
        FarThunk        32QUERYMEMSTATE
        FarThunk        32STARTTIMER
        FarThunk        32STOPTIMER
        FarThunk        32SETDOSPROPERTY
        FarThunk        32ALLOCPROTECTEDMEM
        FarThunk        32ALIASMEM
        FarThunk        32QUERYRESOURCESIZE

        FarThunk        32CLOSEVDD
        FarThunk        32DUMPPROCESS
        FarThunk        32SUPPRESSPOPUPS

        FarThunk        32FORCESYSTEMDUMP
        FarThunk        32DYNAMICTRACE


        FarThunk        32CANCELLOCKREQUEST
        FarThunk        32QUERYEVENTSEM
        FarThunk        32CLOSEMUTEXSEM
        FarThunk        32CREATEMUXWAITSEM
        FarThunk        32OPENMUXWAITSEM
        FarThunk        32CLOSEMUXWAITSEM
        FarThunk        32WAITMUXWAITSEM
        FarThunk        32DELETEMUXWAITSEM
        FarThunk        32QUERYMUXWAITSEM
        FarThunk        32QUERYSYSINFO
        FarThunk        32WAITTHREAD
        FarThunk        32IPROTECTREAD
        FarThunk        32IWRITE
        FarThunk        32IPROTECTWRITE
        FarThunk        32SETFILELOCKS
        FarThunk        32PROTECTSETFILELOCKS

        FarThunk        32EXCEPTIONCALLBACK

        FarThunk        32TMRQUERYFREQ
        FarThunk        32TMRQUERYTIME

        FarThunk        32CONFIGUREPERF
        FarThunk        32DECONPERF
        FarThunk        32REGISTERPERFCTRS

        FarThunk        32QUERYPAGEUSAGE
        FarThunk        32QUERYSYSSTATE


        FarThunk        32PROFILE

        FarThunk        32SETSIGNALEXCEPTIONFOC
        FarThunk        32SENDSIGNALEXCEPTION

        FarThunk        32ASYNCTIMER
        FarThunk        I32STARTTIMER


        FarThunk        32IEXITMUSTCOMPLETE
        FarThunk        32IRAISEEXCEPTION

        FarThunk        32INITIALIZEPORTHOLE
        FarThunk        32QUERYHEADERINFO

        FarThunk        32QUERYMODFROMEIP
        FarThunk        32ICACHEMODULE

        FarThunk        32ACKNOWLEDGESIGNALEXC
        FarThunk        32KILLTHREAD
        FarThunk        32QUERYABIOSSUPPORT
        FarThunk        QUERYRASINFO
        FarThunk        32ALLOCTHREADLOCALMEMORY
        FarThunk        32FREETHREADLOCALMEMORY

        FarThunk        32SETEXTLIBPATH
        FarThunk        32QUERYEXTLIBPATH

	FarThunk	32SYSCTL

	FarThunk	32QUERYTHREADCONTEXT
  
        FarThunk        32PERFSYSCALL                           ; D159047


CODE32 ends


CODE32_TUNE1 segment

;**     Init32Entry - 32 bit entry point for Doscall1
;
;       Since Doscall1.dll is a 32 bit DLL, its init routine is
;       called with a 32 bit near call.  Rewriting the old 16 bit
;       init routine (InitDOSCALL1), would require thunking all
;       the calls it makes outside of itself.  Rather than do
;       that, install Init32Entry as a near callable 32 bit entry
;       point, have it switch over to a 16:16 stack, and then call
;       the old init routine.
;
;       ENTRY   none
;       EXIT    none
;
;       USES    flags


Procedure Init32Entry,near
    assume cs:FLAT, ds:FLAT, es:FLAT, ss:FLAT

        nop                             ; patch space for int 3

        cmp     dword ptr [esp+8],0     ; Is this really initialization?
        jne     short idc20             ; No, simply return

        SaveReg <ebx,ecx,edx>           ; Save scratch registers

;       Switch to a 16-bit stack segment for the initialization routine in
;       the event that the ESP, when converted to a tiled SS:SP, does not
;       allow enough stack space to run through the initialization code
;       (i.e. ESP is 0x3001C and can decrease to 0x20000, but tiled SS:SP
;       is 1f:001C and can only decrease to 1f:0000, effectively leaving
;       18 bytes of stack space even though there is really 64K+18 bytes
;       left in the stack object)

        mov     ebx,ss                  ; (EBX) = 32-bit SS
        mov     edx,esp                 ; (EDX) = 32-bit ESP
        mov     ecx,OFFSET DGROUP:INITSTKBASE ; (ECX) = New SP
        ror     ecx,16                  ; Move SP into high word
        mov     cx,INITSTK              ; (CX) = New SS
        ror     ecx,16                  ; Move SS into high word

        push    ecx                     ; (ECX) = SS:SP
        lss     sp,[esp]                ; Establish new SS:SP

        push    ebx                     ; Save 32-bit SS for restoration later
        push    edx                     ; Save 32-bit ESP for restoration later
        jmp     far ptr CODE16_TUNE1:InitDC1  ; far jump to 16 bit code

;       Following label is jumped to from 16-bit code

idc10:  movzx   eax,ax

        movzx   esp,sp
        lss     esp,[esp]               ; Restore initial SS:ESP

        RestoreReg <edx,ecx,ebx>        ; Restore scratch registers

idc20:  ret
EndProc Init32Entry

CODE32_TUNE1 ends


CODE16_TUNE1 segment

;       The following label is jumped to from Init32Entry.  The
;       stack has been converted to tiled from FLAT.  This
;       routine will jump back into the 32 bit code when the
;       init code (InitDOSCALL1) is complete.

assume cs:CODE16_TUNE1
InitDC1:
        call    InitDOSCALL1
        jmp     far ptr FLAT:idc10

CODE16_TUNE1 ends

        END     Init32Entry             ; Starting cs:ip
