INCLUDE  VCALL.inc

;constant selectors
     Rg0FlatCodeSelector  EQU  28H        ;dogma in Win 3.x 
     Rg0FlatDataSelector  EQU  30H        ;and Win 9x


;VMM structures from MSDN
;-------------------------------------------------------------------------------

pmcb_s  struc
PMCB_Flags   dd  ?  ; control-block flags
PMCB_Parent  dd  ?  ; parent of the protected-mode application
pmcb_s  ends

;Application control block. Contains information about a protected-mode
;application. 
;-------------------------------------------------------------------------------

Client_Reg_Struc   struc
Client_EDI      dd      ?       ; client's EDI
Client_ESI      dd      ?       ; client's ESI
Client_EBP      dd      ?       ; client's EBP
                dd      ?       ; ESP when pusha instruction is executed
Client_EBX      dd      ?       ; client's EBX
Client_EDX      dd      ?       ; client's EDX
Client_ECX      dd      ?       ; client's ECX
Client_EAX      dd      ?       ; client's EAX
Client_Error    dd      ?       ; doubleword error code
Client_EIP      dd      ?       ; EIP
Client_CS       dw      ?       ; CS
                dw      ?       ;   (padding)
Client_EFlags   dd      ?       ; EFLAGS
Client_ESP      dd      ?       ; ESP
Client_SS       dw      ?       ; SS
                dw      ?       ;   (padding)
Client_ES       dw      ?       ; ES
                dw      ?       ;   (padding)
Client_DS       dw      ?       ; DS
                dw      ?       ;   (padding)
Client_FS       dw      ?       ; FS
                dw      ?       ;   (padding)
Client_GS       dw      ?       ; GS
                dw      ?       ;   (padding)
Client_Alt_EIP  dd      ?
Client_Alt_CS   dw      ?
                dw      ?
Client_Alt_EFlags  dd   ?
Client_Alt_ESP  dd      ?
Client_Alt_SS   dw      ?
                dw      ?
Client_Alt_ES   dw      ?
                dw      ?
Client_Alt_DS   dw      ?
                dw      ?
Client_Alt_FS   dw      ?
                dw      ?
Client_Alt_GS   dw      ?
                dw      ?
Client_Reg_Struc   ends

;The Client_Reg_Struc structure contains the CPU register values of the virtual
;device or other program calling a service. 
;-------------------------------------------------------------------------------

cb_s    struc
CB_VM_Status        dd   ?  ; VM status; see below
CB_High_Linear      dd   ?  ; base linear address; see below
CB_Client_Pointer   dd   ?  ; see below
CB_VMID             dd   ?  ; virtual machine ID
CB_Signature        dd   ?  ; see below
cb_s    ends

COMMENT $

Virtual machine control block. Contains information about and the status of
a virtual machine. The handle of a virtual machine is also the address of its
corresponding cb_s structure. All fields in this structure are read-only. 

CB_VM_Status -        Value specifying the status of the virtual machine. Can be
                      zero or more of these values: 

Value                 Meaning 

VMStat_Awakening      Virtual machine is waking up after being blocked on
                      a semaphore. 
VMStat_Background     Virtual machine runs in the background. 
VMStat_Blocked        Virtual machine is blocked on a semaphore. 
VMStat_Closing        Virtual machine has received a Close_VM message. 
VMStat_Creating       Virtual machine is being created. 
VMStat_Exclusive      Virtual machine is in exclusive mode. 
VMStat_High_Pri_Back  Virtual machine has high priority background execution. 
VMStat_Idle           Virtual machine has released its time slice. 
VMStat_Not_Executable Virtual machine is partially destroyed. 
VMStat_PageableV86    Virtual machine has pageable V86 memory (protected-mode
                      application). The default behavior for one or more pages
                      in V86 memory has been modified and the
                      _GetV86PageableArray service returns at least one nonzero
                      bit in the array. 
VMStat_PM_App         Virtual machine contains a protected-mode application. 
VMStat_PM_Exec        Virtual machine execution currently in a protected-mode
                      application. 
VMStat_PM_Use32       Virtual machine contains a 32-bit protected-mode
                      application. 
VMStat_Suspended      Virtual machine is not scheduled. 
VMStat_TS_Sched       Virtual machine is scheduled by the time slicer. 
VMStat_V86IntsLocked  Virtual machine locks any V86 memory that cannot be paged.
                      Locking regardless of the pager type has been enabled for
                      the virtual machine. 
VMStat_VxD_Exec       Virtual machine has received a call from a virtual device. 


CB_High_Linear -      Base address of the virtual machine's memory in the ring-0
                      linear address space. Virtual devices can access memory in
                      a virtual machine by adding the appropriate V86-mode
                      linear address to this value. 
CB_Client_Pointer -   Address of a Client_Reg_Struc structure containing the 
                      register values for the virtual machine. 
CB_VMID -             Virtual machine identifier. 
CB_Signature -        Value VMCB_ID.                                           $
;-------------------------------------------------------------------------------

Exception_Handler_Struc struc
EH_Reserved     dd      ?
EH_Start_EIP    dd      ?
EH_End_EIP      dd      ?
EH_Handler      dd      ?
Exception_Handler_Struc ends

COMMENT $
The Exception_Handler_Struc structure contains information about a ring-0 
exception handler. 

EH_Reserved -         Reserved; must be zero. 
EH_Start_EIP -        Specifies the starting address of the exception handler's
                      supported range of addresses. 
EH_End_EIP -          Specifies the ending address of the exception handler's 
                      supported range of addresses. 
EH_Handler -          Points to the exception handler.                         $
;-------------------------------------------------------------------------------

IPF_Data    struc
IPF_LinAddr     dd  ?   ; CR2 address of fault
IPF_MapPageNum  dd  ?   ; possible converted page number of fault
IPF_PTEEntry    dd  ?   ; contents of PTE that faulted
IPF_FaultingVM  dd  ?   ; may not = current VM (IPF_V86PgH set)
IPF_Flags       dd  ?   ; flags
IPF_Data    ends

COMMENT $
The IPF_Data structure contains information about the current invalid page
fault. 

IPF_LinAddr -         Specifies the CR2 address of the page fault. Do not read
                      from the CR2 register directly; use this value instead. 
IPF_MapPageNum -      Specifies the possible converted page number of the fault. 
IPF_PTEEntry -        Specifies the contents of the page-table entry that caused
                      the fault. 
IPF_FaultingVM -      Specifies the handle identifying the virtual machine that
                      caused the fault. This is not necessarily the current 
                      virtual machine. 
IPF_Flags -           Specifies the invalid page-fault flags. It can be a 
                      combination of the following values: 

Value                 Meaning 

IPF_InvTyp            Page has invalid not-present type. 
IPF_PgDir             Page directory entry not present (not-present page table). 
IPF_PgErr             Page swap device could not page for some reason. 
IPF_PM                Page fault caused by virtual machine running in protected
                      mode. 
IPF_ReFlt             Re-entrant page fault. 
IPF_V86               Page fault caused by virtual machine running in V86 mode. 
IPF_V86Pg             Unexpected not-present page in V86. 
IPF_V86PgH            Unexpected not-present page in V86 at high linear address. 
IPF_VMM               Page fault caused by a virtual device. 

Invalid page faults occur in a virtual machine other than the current virtual
machine if the high linear address of the virtual machine is accessed. In this
case, the IPF_FaultingVM field is set to the handle of the virtual machine that
owns the high linear address.                                                  $
;-------------------------------------------------------------------------------

Pushad_Struc struc
Pushad_EDI  dd  ?  ; EDI register at time of pushad
Pushad_ESI  dd  ?  ; ESI register at time of pushad
Pushad_EBP  dd  ?  ; EBP register at time of pushad
Pushad_ESP  dd  ?  ; ESP register at time of pushad
Pushad_EBX  dd  ?  ; EBX register at time of pushad
Pushad_EDX  dd  ?  ; EDX register at time of pushad
Pushad_ECX  dd  ?  ; ECX register at time of pushad
Pushad_EAX  dd  ?  ; EAX register at time of pushad
Pushad_Struc ends

;Describes the stack frame built by the pushad instruction. Its typical uses are
;to reach back into a 'pushad' frame to recover a value that was saved in it, or
;to shove a value into it to be restored by a subsequent 'popad'. 
;-------------------------------------------------------------------------------

tcb_s    struc
TCB_Flags            dd  ?  ; thread status flags; see below
TCB_Reserved1        dd  ?  ; reserved for use by VMM
TCB_Reserved2        dd  ?  ; reserved for use by VMM
TCB_Signature        dd  ?  ; SCHED_OBJ_THREAD_ID
TCB_ClientPtr        dd  ?  ; address of client registers
TCB_VMHandle         dd  ?  ; VM to which this thread belongs
TCB_ThreadId         dw  ?  ; thread ID 
TCB_PMLockOrigSS     dw  ?  ; client SS at Begin_Use_Locked_PM_Stack
TCB_PMLockOrigESP    dd  ?  ; client ESP at Begin_Use_Locked_PM_Stack
TCB_PMLockOrigEIP    dd  ?  ; client EIP at Begin_Use_Locked_PM_Stack
TCB_PMLockStackCount dd  ?  ; number of outstanding 
                            ;   Begin_Use_Locked_PM_Stack calls
TCB_PMLockOrigCS     dw  ?  ; client CS at Begin_Use_Locked_PM_Stack
TCB_PMPSPSelector    dw  ?  ; used by Vwin32
TCB_ThreadType       dd  ?  ; used by Vwin32
TCB_pad1             db  ?  ; padding
TCB_pad2             db  ?  ; padding
TCB_extErrLocus      db  ?  ; used by Vwin32
TCB_extErrAction     db  ?  ; used by Vwin32
TCB_extErrClass      db  ?  ; used by Vwin32
TCB_extErrPtr        dd  ?  ; used by Vwin32
tcb_s ends

COMMENT $
Thread control block. Contains information about, and the status of, a thread. The handle of a thread is also the address of its corresponding tcb_s structure. All fields in this structure are read-only. 

TCB_Flags -           Value specifying the status of the thread. Can be zero or
                      more of these values: 

Value                  Meaning 



THFLAG_Suspended       Thread is not scheduled. 
THFLAG_Not_Executeable Thread has stopped executing. 
THFLAG_Thread_Creation Thread being 
created. 
THFLAG_Thread_Blocked  Thread is blocked on a semaphore. 
THFLAG_Ring0_Thread    Thread runs completely at ring 0. 

THFLAG_ANSI THFLAG_OEM THFLAG_UNICODE THFLAG_RESERVED 
                       Used by Vwin32 to indicate file system character set. 


THFLAG_Extended_Handles       Thread uses extended handles. 
THFLAG_Open_As_Immovable_File Used by Vwin32 to prevent defragmenter from moving
                              an open file. 
These bits are read-only. Virtual devices should not attempt to modify any of 
these bits. Some of the descriptions might not be directly meaningful; don't 
worry. Only Vwin32 really cares about them. 

TCB_Reserved1, TCB_Reserved2 -  Reserved by VMM for internal bookkeeping. 
TCB_Signature - The value SCHED_OBJ_THREAD_ID. 
TCB_ClientPtr - Pointer to thread client registers. 
TCB_VMHandle  - Handle of VM to which this thread belongs. 
TCB_ThreadId  - 16-bit value that uniquely identifies the thread during its
                lifetime. Note that thread IDs are recycled. When a thread is 
                destroyed, its ID becomes available for use. 
TCB_PMLockOrigSS, TCB_PMLockOrigESP, TCB_PMLockStackCount, and TCB_PMLockOrigCS 
 - Used internally by the Begin_Use_Locked_PM_Stack service. 
TCB_PMPSPSelector, TCB_ThreadType, TCB_extErrLocus, TCB_extErrAction, 
TCB_extErrClass, and TCB_extErrPtr - Used internally by Vwin32.                $
;-------------------------------------------------------------------------------

VMFaultInfo    struc
VMFI_EIP   dd      ?   ; 
VMFI_CS    dw      ?   ; 
VMFI_Ints  dw      ?   ; 
VMFaultInfo    ends


COMMENT $
The VMFaultInfo structure contains data which describes the client state at the
time of a fault in the virtual machine. 

VMFI_EIP and VMFI_CS - Specifies the value of the instruction pointer and the
                       code segment address at the time the fault occurred. 
VMFI_Ints -            Indicates the interrupt request lines (IRQs) that were

                       in service when the fault occurred. Bit 0 is set if IRQ 0
                       was in service, bit 1 is set if IRQ 1 was in service, and
                       so on.                                                  $
;-------------------------------------------------------------------------------

ReverseParameters MACRO  __P0,__P1,__P2,__P3,__P4,__P5,__P6,__P7,__P8,__P9
       __C = 0
        IFNB  <__P9> 
         PUSH  __P9
         __C = __C + 4
        ENDIF
        IFNB  <__P8> 
         PUSH  __P8
         __C = __C + 4
        ENDIF
        IFNB  <__P7> 
         PUSH  __P7
         __C = __C + 4
        ENDIF
        IFNB  <__P6> 
         PUSH  __P6
         __C = __C + 4
        ENDIF
        IFNB  <__P5> 
         PUSH  __P5
         __C = __C + 4
        ENDIF
        IFNB  <__P4> 
         PUSH  __P4
         __C = __C + 4
        ENDIF
        IFNB  <__P3> 
         PUSH  __P3
         __C = __C + 4
        ENDIF
        IFNB  <__P2> 
         PUSH  __P2
         __C = __C + 4
        ENDIF
        IFNB  <__P1> 
         PUSH  __P1
         __C = __C + 4
        ENDIF
        IFNB  <__P0> 
         PUSH  __P0
         __C = __C + 4
        ENDIF
ENDM



VxDCall MACRO  Service, __P0,__P1,__P2,__P3,__P4,__P5,__P6,__P7,__P8,__P9
        ReverseParameters <__P0>,<__P1>,<__P2>,<__P3>,<__P4>,<__P5>,<__P6>,<__P7>,<__P8>,<__P9>
	INT    20H
	DD     Service
        IF     __C GT 0
        ADD    ESP, __C
        ENDIF
ENDM

VxDJmp  MACRO  Service
	INT    20H
	DD     Service OR 000008000H
	ENDM

VMMCall MACRO  Service, __P0,__P1,__P2,__P3,__P4,__P5,__P6,__P7,__P8,__P9
        IF     (Service SHR 16) EQ VMM
        VxDCall Service, <__P0>,<__P1>,<__P2>,<__P3>,<__P4>,<__P5>,<__P6>,<__P7>,<__P8>,<__P9> 
        ELSE
        ERR    "This is not VMM service"
        ENDIF
	ENDM

VMMJmp  MACRO  Service
        IF     (Service SHR 16) EQ VMM
        VxDJmp Service 
        ELSE
        ERR    "This is not VMM service"
        ENDIF
	ENDM


VxDint  MACRO  Int_Number
        VMMCall Exec_VxD_Int, <Int_Number>
	ENDM


;Ports related
;Values for these constants I've obtained by testing in WinICE
Byte_Input    EQU 0000H
Byte_Output   EQU 0004H
Word_Input    EQU 0008H
Word_Output   EQU 000CH
Dword_Input   EQU 0010H
Dword_Output  EQU 0014H
String_IO     EQU 0020H   ;INS?/OUTS?
Rep_IO        EQU 0040H   ;REP INS?/OUTS?
Addr_32_IO    EQU 0080H   ;in 32bit code
Reverse_IO    EQU 0100H   ;STD REP INS?/OUTS?


;for _HeapAlloc
HEAPZEROINIT  EQU  000000001H

