;***	EM86.INC
;
;	 Version 1.10.1.2, for src/dos/mvdm/em86.inc, em86, mer.krnl, 960610.1
;
;
;	Title:
;
;	Local 8086 Emulation Header File
;
;	Description:
;
;	Contains all the local (not exported) defines and
;	structures for the 8086 emulation component.
;
;	Gordon Kuo    02/24/96					       149007
;	Enhance VPMEXC structure to process V86 mode exception handler 149007
;
;



;***	Undocumented VDHInstallIntHook options
VDH_PRE_HOOK	EQU	000000002h ;BUGBUG - To be moved to v8086.inc


; WARNING: The INT_* constants can only use bits 0 and 1, because they
;  are stored in the low bits of dword-aligned heap pointers (grin) -JTP
;
INT_BP_HOOK	EQU	000000001h
INT_PRE_HOOK	EQU	000000002h



; Unchangeable misc. definitions

NUM_OPCODE	EQU	256	;number of opcodes in table
NUM_INTS	EQU	256	;number of software interrupts
NBITS_ULONG	EQU	32	;number of bits in a ULONG
NBITS_BYTE	EQU	8	;number of bits in a BYTE
MAX_PORT_NUM	EQU	0FFFFh	;highest port number
ARPL_OPCODE	EQU	063h	;VDM BP instruction
CLTS_OPCODE	EQU	06	;CLTS instruction (2nd byte)
MAX_HOOK_TYPE	EQU	6	;max hook type value for VDHAllocHook

; Adjustable misc. definitions

NUM_VDMBP_ENTRIES	EQU	128	;total number of VDM BPs
NUM_VDH_REGS	EQU	12	;number of reg for PushReg/PopReg
NUM_GLOBAL_CONTEXT	EQU	16	;number of global context hooks


;  This the port number when direct lookup is no longer used and
;  a port number hash table is used.  The current port number is
;  the highest port used on a PC.  Some of the newer ports on the
;  PS/2's are higher and are hashed.  The initial iopm size is
;  based on the number of local ports for the same reasons.
;

PORT_HASH_START		EQU	0400h	;start of I/O port hashing
INITIAL_IOPM_SIZE	EQU	(PORT_HASH_START/8)


;  The number of hash table entries is controlled by this
;  definition.  It must be a power of 2 (so a simple mask
;  can be used to index the table).
;

NUM_HASH_ENTRIES	EQU	020h	;number of hash entries
PORT_HASH_MASK	EQU	(NUM_HASH_ENTRIES - 1)

; A20 wrapping definitions

A20_WRAP_AT	EQU	( 0100000h)
A20_WRAP_TO	EQU	0
NUM_A20_PAGES	EQU	16

; I/O handler flags - determines which entry in the IOH to use

IO_HDLR_SHIFT	EQU	0
IO_HDLR_MASK	EQU	0000000FFh
IO_HDLR_BYTE_INPUT	EQU	(ioh_pbihByteInput)
IO_HDLR_BYTE_OUTPUT	EQU	(ioh_pbohByteOutput)
IO_HDLR_WORD_INPUT	EQU	(ioh_pwihWordInput)
IO_HDLR_WORD_OUTPUT	EQU	(ioh_pwohWordOutput)
IO_HDLR_OTHER	EQU	(ioh_pothOther)

; Instruction prefix flags - must be same as I/O type flags (v8086.inc)

PREFIX_OPER32	EQU	<IO_TYPE_DWORD>
PREFIX_ADDR32	EQU	<IO_TYPE_ADDR32>
PREFIX_SEG32	EQU	080000000h
;For the instructions we decode, the "rep" and "repne" prefixes mean
;the same thing.  The difference would only be significant for decoding
;stos, movs, etc. which will never happen.
PREFIX_REP	EQU	<IO_TYPE_REP>
PREFIX_REPNE	EQU	<IO_TYPE_REP>
PREFIX_SEG	EQU	000100000h
PREFIX_DS	EQU	(IO_SEG_DS or PREFIX_SEG)
PREFIX_CS	EQU	(IO_SEG_CS or PREFIX_SEG)
PREFIX_SS	EQU	(IO_SEG_SS or PREFIX_SEG)
PREFIX_ES	EQU	(IO_SEG_ES or PREFIX_SEG)
PREFIX_FS	EQU	(IO_SEG_FS or PREFIX_SEG)
PREFIX_GS	EQU	(IO_SEG_GS or PREFIX_SEG)

; iop_flags values

IOP_ASM_HOOK	EQU	00001h	;assembly language hook
IOP_ALWAYS_TRAP		EQU	00002h	;port will always be trapped
IOP_SIMULATE_DWORD	EQU	00004h	;dword I/O is always simulated
IOP_TRAP_ENABLED	EQU	00008h	;if set, trapping is enabled
IOP_IGNORE	EQU	00010h	;system ignore hook installed
; V86 and VDM protected mode IDT definations

IDT_START_EXCEPTIONS	EQU	000h
IDT_END_EXCEPTIONS	EQU	01fh
IDT_START_SOFTWARE_INTS		EQU	020h
IDT_END_SOFTWARE_INTS	EQU	04fh
IDT_START_HARDWARE_INTS		EQU	050h
IDT_END_HARDWARE_INTS	EQU	07fh
IDT_START_VPM_SOFTWARE_INTS	EQU	080h
IDT_END_VPM_SOFTWARE_INTS	EQU	0ffh
IDT_SIZE	EQU	080h	;size of OS/2 PM IDT

; DPMI private definitions

GATE_SIZE		      equ     (size gate)

VPM_NUM_EXCEPTIONS	EQU	020h
VPM_NUM_INTS	EQU	0100h
VPM_IDT_SIZE	EQU	(VPM_NUM_INTS * GATE_SIZE)
VPM_STACK_SIZE	EQU	(_1K * 4)
VPM_STACK_ALLOC_FLAGS	EQU	(VMAC_VDM  OR  VMAC_LOCANY  OR	\
					 VMAC_ALIGNPAGE	 OR  VMAC_ARENAPRV  OR	\
					 PG_R  OR  PG_W	 OR  PG_U  OR  PG_SWAPPABLE)

;The static VDM protected mode breakpoint number definitions

VPM_INT_BP_START	EQU	0
VPM_INT_BP_END	EQU	(VPM_NUM_INTS - 1)
VPM_EXC_BP_START	EQU	(VPM_INT_BP_END + 1)
VPM_EXC_BP_END	EQU	(VPM_INT_BP_END + VPM_NUM_EXCEPTIONS)
VPM_V86EXC_BP_START	EQU	(VPM_EXC_BP_END + 1)		      ;149007
VPM_V86EXC_BP_END	EQU	(VPM_EXC_BP_END + VPM_NUM_EXCEPTIONS) ;149007
VPM_VDMBP_START		EQU	(VPM_V86EXC_BP_END + 1)		      ;149007
VPM_VDMBP_END	EQU	(VPM_V86EXC_BP_END + NUM_VDMBP_ENTRIES);149007
VPM_NUM_BPS	EQU	(VPM_VDMBP_END + 1)			;149007

;The user level VDM protected mode breakpoint table (filled with HLTs)

VPM_BP_SIZE	EQU	(_1K * 4)
VPM_BP_ALLOC_FLAGS	EQU	(VMAC_VDM  OR  VMAC_LOCANY  OR	\
					 VMAC_ALIGNPAGE	 OR  VMAC_ARENAPRV  OR	\
					 PG_X  OR  PG_R	 OR  PG_W  OR  PG_U  OR	 \
					 PG_SWAPPABLE)
VPM_BP_SET_FLAGS	EQU	(PG_X  OR  PG_R	 OR  PG_U)

;Sizes of the IDT regions that are the same access all IDTs

VPM_EXCEPTION_INTS_SIZE		EQU	(((IDT_END_EXCEPTIONS - \
					 IDT_START_EXCEPTIONS) + 1) * \
					  GATE_SIZE)

VPM_HARDWARE_INTS_SIZE	EQU	(((IDT_END_HARDWARE_INTS - \
					 IDT_START_HARDWARE_INTS) + 1) * \
					  GATE_SIZE)


;***LP	em86Assert - data structure assertion checking macros
;
;	ENTRY	t - typedef name for structure (i.e. INTH, IOP, etc.)
;		p - pointer to structure to be checked
;
;	EXIT	NONE
;
;	A define "<t>_SIGNATURE" must be defined for each structure
;	where "t" is the typedef name used in the assert macros.  This define
;	is the value for the signature field.  It is a 32 bit value
;	that should be the typedef name (i.e. 048544e49h for "INTH").
;

ifdef  EM86STRICT

BAD_SIGNATURE	EQU	053444142h


em86AssertDef macro   t
      ULONG   &t&_sig
      endm

em86AssertCreate macro t, p
      mov     [p].&t&_sig,&t&_SIGNATURE
      endm

em86AssertP   macro   t, p
      local   l1
      cmp     [p].&t&_sig,&t&_SIGNATURE	      ; check signature
      je      short l1
      Panic   <"em86AssertP">
l1:
      endm

em86AssertDestroy macro t, p
      mov     [p].&t&_sig,BAD_SIGNATURE
      endm

em86AssertVSF macro   ptcb
      local   l1
      ReferFlat _criVSF,dword,ifundef
      push    eax
      mov     eax,[ptcb]
      cmp     [eax].TCB_pcriFrameType,offset FLAT:_criVSF
      je      short l1
      Panic   <"em86AssertVSF: %08x">, <[eax].TCB_pcriFrameType>
l1:   pop     eax
      endm

em86AssertPVSF macro  r
      local   l1
      ReferFlat _pTCBCur,dword,ifundef
      ReferFlat _criVSF,dword,ifundef
      ifidni  <r>,<eax>
	      %out em86AssertPVSF: can't use eax
	      .err
      endif
      push    eax
      mov     eax,[_pTCBCur]
      cmp     [eax].TCB_pFrameBase,r
      je      short l1
      cmp     [eax].TCB_pcriFrameType,offset FLAT:_criVSF
      je      short l1
      Panic   <"em86AssertPVSF: %08x %08x">, <&r&, [eax].TCB_pcriFrameType>
l1:   pop     eax
      endm

else


em86AssertDef	      macro   t
      endm
em86AssertCreate      macro   t, p
      endm
em86AssertDestroy     macro   t, p
      endm
em86AssertP	      macro   t, p
      endm
em86AssertVSF	      macro   ptcb
      endm
em86AssertPVSF	      macro   r
      endm

endif


;***LP	em86InvalidParameter - macro for invalid VDH service parameters
;***LP	em86InternalError - macro for internal consistency errors
;
;	ENTRY	s - string to be printed
;
;	EXIT	NONE
;


em86InvalidParameter macro s
      Panic   <s>
      endm

em86InternalError macro s
      Panic   <s>
      endm

;***	IOP - I/O port entry
;
;	A per VDM array (apiop) is indexed by port number
;	(if below the port hash start) containing a pointer
;	to this structure.  This structure contains a copy
;	of the VDD's IOH, a pointer to the VDD IOH for validation
;	during set trapping state and deinstallation and a flag
;	field.  If I/O hooks were done with "C" interface option
;	the iop_ioh contains the IOH assembly language stubs that
;	call the "C" hook routines.  The iop_iohVDD contains the
;	real VDD handlers.
;

DefStruc ,iop_s
	IOH iop_ioh		       ;I/O port hook entry
	IOH iop_iohVDD		       ;VDD's I/O port hook entry
	FLAGS iop_flags		       ;I/O hook flags
ifdef  VDHSTRICT
	PIOH iop_piohVDD	       ;Pointer to VDD's ioh
	PORT iop_port		       ;starting port
	ULONG iop_cport		       ;number of ports
endif
	em86AssertDef IOP		;assertion checking
EndStruc IOP

DefType PIOP,dd

IOP_SIGNATURE	EQU	020504f49h	;"IOP "


;***	PHA - I/O port hash entry
;
;	This is the structure for the hash entry for I/O port
;	trap handlers above PORT_HASH_START (400H).  It contains
;	an IOP, the port number for this IOP and a pointer to the
;	next entry on the linked list.  The heads of the lists
;	are kept in the "appha" array.
;

DefStruc ,pha_s
	IOP pha_iop		       ;I/O port entry
	PORT pha_port		       ;port number
	PVOID pha_ppha	       ;ptr to next entry in list
	em86AssertDef PHA		;assertion checking
EndStruc PHA

DefType PPHA,dd			;ptr to PHA entry

PHA_SIGNATURE	EQU	020414850h	;"PHA "


;***	INTH - interrupt hook entry
;
;	This structure describes the VDD interrupt handler
;	list entries.  The head of the list for each interrupt
;	number is in the apinthHead array.
;

DefStruc ,inth_s
	PVOID inth_pinth      ;ptr to next entry in list
	PFN inth_pfn		       ;assembly hook address
	PFNHOOK inth_pfnhook	       ;"C" interface hook address
	em86AssertDef INTH		;assertion checking
EndStruc INTH

DefType PINTH,dd			;pointer to int handler entry

INTH_SIGNATURE	EQU	048544e49h	;"INTH"


DefType HOOK,hook_s		      ; define HOOK macro

;***	VDMBP - VDM breakpoint entry
;
;	This is the VDM breakpoint table entry structure.  This is
;	basiclly a hook structure with a few VDMBP specific fields.  The
;	vdmbp_pfnDispatch address is the routine used to call the
;	hook function.
;

DefStruc ,vdmbp_s
	HOOK vdmbp_hook		       ;hook block
	PFN vdmbp_pfnDispatch	       ;VDMBP dispatcher address
	VPVOID vdmbp_vpOldCSIP	       ;original v86 mode CS:IP
	FPFN vdmbp_fpfnOldCSEIP	       ;original VDM protected mode CS:EIP
	em86AssertDef VDMBP		;assertion checking
EndStruc VDMBP

DefType PVDMBP,dd			;ptr to VDMBP entry

VDMBP_SIGNATURE		EQU	050424d56h	;"VMBP"


;***	RETH - return hook entry
;
;	This is the return hook entry structure.  This is basiclly a
;	hook structure with a few VDMBP specific fields.
;

DefStruc ,reth_s
	VDMBP reth_vdmbp	       ;vdm bp hook block
	VPVOID reth_vpvdmbp	       ;vdm bp address
	em86AssertDef RETH		;assertion checking
EndStruc RETH

DefType PRETH,dd			;ptr to VDMBP entry

RETH_SIGNATURE	EQU	048544552h	;"RETH"


;***	TIMH - timer hook data structure
;
;	This is the data structure used by VDHArmTimerHook and
;	VDHDisarmTimerHook over the basic hook structure.
;
;	The armed timer blocks are kept on a delta list.  The
;	first entry's ulmsDelta field is the number of milliseconds
;	until the first entry hits.  The second entry is the number
;	of milliseconds after the first entry hits until the second
;	entry hits and so on.
;
;	timh_ptcb is either a ptcb or 0 for an automatic local context
;	arming, -1 for automatic global context hook arming or 1 for the
;	normal interrupt time call.
;

DefStruc ,timh_s
	HOOK timh_hook		       ;hook block
	ULONG timh_ulmsDelta	       ;millisecond delta
	PVOID timh_ptcb		       ;flag or ptcb for context hook
	em86AssertDef TIMH		;assertion checking
EndStruc TIMH

DefType PTIMH,dd			;ptr to TIMH entry

TIMH_SIGNATURE	EQU	0484d4954h	;"TIMH"


DHD_V86_STACK_SIZE	EQU	0400h	   ;was x204


DHD_VPM_SAVE_START	      equ     VSF_ALT_START


;***	VPMINT - VDM protected mode interrupt data structure
;
;	This structure is shadows the IDT with the VPM applications
;	handler address and a flags mask to make it easier to setup
;	the interrupt flag when emulating an interrupt.
;

DefStruc ,vpmint_s
	ULONG vpmint_off	       ;app exception handler offset
	SEL vpmint_sel		       ;app exception handler selector
	USHORT vpmint_usFlagMask       ;cpu flag mask (Trace/Interrupt)
	em86AssertDef VPMINT		;assertion checking
EndStruc VPMINT


VPMINT_SIGNATURE	EQU	0494d5056h	;"VPMI"


;***	VPMEXC - VDM protected exception data structure
;
;	This is the VDM protected mode application exception
;	handler data structure.
;

DefStruc ,vpmexc_s
	ULONG vpmexc_off	       ;app exception handler offset
	SEL vpmexc_sel		       ;app exception handler selector
	USHORT vpmexc_usFlags  ;flags ;GK: This flags never be used???
	USHORT vpmexc_fb	       ;type of exception handler  149007 DPMI 1.0 subset
			   ;149007
	em86AssertDef VPMEXC		;assertion checking
EndStruc VPMEXC


;***	EXCST - VDM protected mode 16 bit exception stack structure
;
;  This is the structure of the stack for a DPMI exception handler for a 16 bit
;  process.  The application exception handler can modify these values.
;
DefStruc ,excst_s
	USHORT excst_errcode	      ;Error code for exception
	USHORT excst_ip		      ;Client IP at time of fault
	SEL    excst_cs		      ;Client CS at fault time
	USHORT excst_flags	      ;Client flags at fault time
	USHORT excst_sp		      ;Client SP at fault time
	SEL    excst_ss		      ;Client SS at fault time
EndStruc EXCST

;***	EXCST - VDM protected mode 32 bit exception stack structure
;
;  This is the structure of the stack for a DPMI exception handler for a 32 bit
;  process.  The application exception handler can modify these values.
;
DefStruc ,exc32_s
	ULONG  exc32_errcode	      ;Error code for exception
	ULONG  exc32_eip	      ;Client EIP at time of fault
	SEL    exc32_cs		      ;Client CS at time of fault
	USHORT exc32_padcs
	ULONG  exc32_eflags	      ;Client eflags at time of fault
	ULONG  exc32_esp	      ;Client ESP at time of fault
	SEL    exc32_ss		      ;Client SS at time of fault
	USHORT exc32_padss
EndStruc EXC32

;***	EXCST - VDM protected mode 16 bit exception stack structure  149007
;								     149007
;  This is the structure of the stack for a DPMI exception handler for a 16 bit
;  process.  The application exception handler can modify these values. 149007
;
DefStruc ,exexcst_s
   EXC32  oldexc32	     ;0.9 Stack Frame. fit in 16 bit and 32 Bit
			      ;149007
   ULONG  exexcst_ret_eip    ;return EIP, CS:IP for 16 bit	  149007
   SEL	  exexcst_ret_cs     ;Return CS for 32 bit		  149007
   USHORT exexcst_ret_cs_pad ;pad				  149007
   ULONG  exexcst_errcode    ;Error code			  149007
   ULONG  exexcst_eip	     ;eip or ip of VDM			  149007
   SEL	  exexcst_cs	     ;cs of VDM				  149007
   USHORT exexcst_info	     ;Exception Information bits	  149007
   ULONG  exexcst_eflags     ;eflags of VDM			  149007
   ULONG  exexcst_esp	     ;esp of sp of VDM			  149007
   SEL	  exexcst_ss	     ;ss of VDM				  149007
   USHORT exexcst_ss_pad     ;pad				  149007
   SEL	  exexcst_es	     ;es of VDM				  149007
   USHORT exexcst_es_pad     ;pad				  149007
   SEL	  exexcst_ds	     ;ds of VDM				  149007
   USHORT exexcst_ds_pad     ;pad				  149007
   SEL	  exexcst_fs	     ;fs of VDM				  149007
   USHORT exexcst_fs_pad     ;pad				  149007
   SEL	  exexcst_gs	     ;gs of VDM				  149007
   USHORT exexcst_gs_pad     ;pad				  149007
   ULONG  exexcst_cr2	     ;CR2				  149007
   ULONG  exexcst_pte	     ;PTE				  149007
EndStruc EXEXCST		    ;149007



VPMEXC_SIGNATURE	EQU	0454d5056h	;"VPME"


;Equates for flrestart flag
VPM_RESTART_INST	EQU	1	;restart the faulting instruction
VPM_RETV86	EQU	2	;return to V86 mode after fault
VPM_RETV86_BIT	EQU	1	;corresponding bit for VPM_RETV86

;***	VDDAPI - VDD API handler data structure
;
;	This structure contains the VDD IDs and the hook handles for the V86
;	or protect mode API handlers the VDD has registered.
;

DefStruc ,vddapi_s
	CHAR   vapi_szVDD,,MAXLENXDD+1 ;VDD name
	HHOOK  vapi_hhookV86	       ;hook handle for V86 API entry
	HHOOK  vapi_hhookVPM	       ;hook handle for VPM API entry
	VPVOID vapi_vpV86BP	       ;V86 breakpoint address
	VPVOID vapi_vpVPMBP	       ;VPM breakpoint address
	PVOID vapi_next	    ;ptr to next entry in list
	em86AssertDef VDDAPI		;assertion checking
EndStruc VDDAPI

DefType PVDDAPI,dd			;ptr to VDDAPI entry
VDDAPI_SIGNATURE	EQU	049504156h     ;"VAPI"


; Processor types for VDHDetectProcessor
P_80586		EQU	5
P_80486		EQU	4
P_80386		EQU	3
VME_supported	EQU	2      ;Processor supports virt. mode ext.

; External interfaces within 8086 emulation

