;       SCCSID = @(#)segmngr.inc	6.1 90/11/17
;      SCCSID = @(#)segmngr.inc	13.5 89/09/22

;	Segment	managment structures
;
;
;	Modification History
;
;	24-Apr-87     IDB     Support Per-Instance Library Initialization
;			      (PTM 4093, DCR 75); Added	INSTLIBINIT and
;			      renamed INSTANCE as INSTANCEDS.
;
;	29-Apr-87     IDB     Search for programs correctly (PTM 2527).
;
;	17-May-87     IDB     Added MTE_DISCARD	bit to mte_flags (PTM 2627).
;
;	10-Jun-87     IDB     Added EXT_DEVICE (for PTM	4659).
;
;	27-Jul-87     IDB     Added SetLTrap2 macro (for PTM 6402).
;
;	 5-Aug-87     SRW     Removed ifdef'd WINTHORN definitions
;
;	 3-Oct-87      IS     added DEVDRVMOD bit to mte_flags (PTM 7572)
;
;	 8-Oct-87      IS     added MTE_INTNL_FLGS_MASK	(PTM 886)
;
;	15-Oct-87      IS     remove MTE_DISCARD from MTE_INTNL_FLGS_MASK
;
;	15-Mar-88     FJD     Added GINITSETUP to MTE_INTNL_FLGS_MASK (PTM1696)
;
;	16-Mar-88     FJD     Redefined	DOSCALLS MTE (PTM 1977)


;	Equates

MAX_NAME_LEN	EQU	8

;	Thunk instruction equates

INTINST		EQU	03FCDh		; INT 03Fh
JMPINST		EQU	0EAh		; JMP nnnn:nnnn
MOVAXINST	EQU	0B8h		; MOV AX,#

;	scan_mte_list function codes

SML_SCAN_STE	EQU	0		; Scan ste's for specified selector
SML_SET_LOADED	EQU	1		; Set the MTELOADED bit
SML_SCAN_STE_NORPL EQU	2		; scan ste for selector	(ignore	RPL)


;	Call Thunk Entry format

cte_s	STRUC
cte_flags	db	?		;
cte_inst	db	2 DUP(?)	; space	for INT	or JMP instructions
cte_seg		db	?		; file segment table index
cte_off		dw	?		; offset of routine entry point
cte_s	ENDS

cte_sel		EQU (word ptr cte_inst)	; selector for ring 2 callgates

;	cte_flags bit definitions

EF_EXPORT	EQU	01h		; entry	is exported
EF_GDATA	EQU	02h		; entry	uses global shared data


;	Exported Entry format

ent_s	STRUC
ent_flags	db	?		;
ent_off		dw	?		; offset of routine entry point
ent_s	ENDS

;	Entry table types

ENTAB_ABSOLUTE	equ	0feh	; indicates entry is for an absolute sym
ENTAB_MOVABLE	equ	0ffh	; indicates that entry is movable.


;	get_module ex_type definitions

EXT_PROGRAM	EQU	0
EXT_LIBRARY	EQU	1
EXT_DEVICE	EQU	2		; load a device driver
EXT_FSD		EQU	3		; load an installable file system


;     Class Related Equates

CLASS_PROGRAM EQU     0040h	      ; Program class module
CLASS_GLOBAL  EQU     0080h	      ; Global class module
CLASS_SPECIFIC	      EQU     00C0h	      ; Specific class module
CLASS_MASK    EQU     00C0h	      ; Mask for class field

;     Search Flags for find_module calls

SEARCH_BY_PATH	      EQU     1	      ; find_module will use path for search


;	Module Table Entry format

mte_s	STRUC

;	New EXE	Header format

mte_signature		dw	?	; must contain "NE"
mte_version		db	?	;
mte_revision		db	?	;
mte_entrytbl_off	dw	?	; file entry table offset
mte_entrytbl_len	dw	?	; length of export entry table (bytes)
mte_crc			dd	?	; crc of entire	file contents
mte_flags		dw	?	;
mte_autods		dw	?	; auto data segment table index
mte_heapsiz		dw	?	; dynamic heap bytes added to data seg
mte_stacksiz		dw	?	; stack	bytes added to data segment
mte_startproc		dd	?	;
mte_stack		dd	?	;
mte_segtbl_cnt		dw	?	; # of entries in segment table
mte_modref_cnt		dw	?	; # of entries in module reference table
mte_nonresnam_len	dw	?	; length nonresident name table	(bytes)
mte_segtbl_off		dw	?	; file segment table offset
mte_resrctbl_off	dw	?	; file resource	table offset
mte_resnamtbl_off	dw	?	; file resident	name table offset
mte_modref_off		dw	?	; file module reference	table offset
mte_importnam_off	dw	?	; file import name table offset
mte_nonresnam_off	dw	2 DUP (?);file nonresident name	table offset
mte_movent_cnt		dw	?	; count	of movable entries
mte_alignshft		dw	?	; segment alignment shift count
mte_resrctbl_cnt	dw	?	; count	of resource table entries
			dw	?	; reserved
			dw	?	; reserved
mte_dyntrchndl		dw	?	; dynamic trace	segment	handle
mte_usecount		dw	?	; usage	count for program module
mte_pathname		dw	?	; offset to module pathname
mte_s	ENDS

;	Overloaded MTE field definitions

mte_link	EQU	(word ptr mte_version)	; link to next mte, 0 is end
mte_modptrs	EQU	mte_entrytbl_len
mte_sfn		EQU	(word ptr mte_crc)	; system file number for module
mte_size	EQU	(word ptr mte_crc+2)	; size of the mte

EXE_VALID_NEWSIGNATURE	EQU	"EN"

;	mte_flags bit definitions

AUTODS_MASK	EQU	0003h		; bits used in decoding	DS
NOAUTODS	EQU	0000h
SOLO		EQU	0001h
INSTANCEDS	EQU	0002h		; Per-instance DS (non shared)
INSTLIBINIT	EQU	0004h		; Per-instance LibInit
GINITSETUP	EQU	0008h		; Global Init has been set-up
MTELOADED	EQU	0100h
USED		EQU	0200h
DOSLIB		EQU	0400h		; set if DOSCALL1
DOSMOD		EQU	0800h		; set if DOSCALLS
MTE_DISCARD	EQU	1000h
INVALID		EQU	2000h
MTEVALID	EQU	INVALID		; We overload this bit:	once an	mte
					; is linked into the chain, this bit
					; is used to indicate (if set) that
					; the mte contains valid information
					; that demand loaders (who are not
					; excluded by the loader semaphore)
					; may examine.
DEVDRVMOD	EQU	4000h		; device driver	module
LIBRARYMOD	EQU	8000h		; DLL module
FSDMOD		EQU	0010h		; FSD MTE
FSHMOD		EQU	0020h		; FS Helper MTE

NENEWFILES	equ	01h		; newfiles support bit in mte_flags2


;	MTE_INTNL_FLGS_MASK is the set of initially CLEAR mte_flags.
;	Remaining bits aren't touched.

MTE_ABBREV	EQU	DOSLIB+DOSMOD+DEVDRVMOD+USED	; abbreviation...
MTE_INTNL_FLGS_MASK	EQU	MTE_ABBREV or (MTEVALID+MTELOADED+GINITSETUP)


;	Old EXE	Header format

oh_s	STRUC
oh_signature	dw	?		; Must contain 4D5A
oh_len_mod_512	dw	?		; Low 9	bits of	length
oh_pages	dw	?		; Number of 512	byte pages
oh_rle_count	dw	?		; Count	of reloc entries
oh_par_dir	dw	?		; # of paras before image
oh_min_BSS	dw	?		; Min #	of paras of BSS
oh_max_BSS	dw	?		; Max #	of paras of BSS
oh_SS		dw	?		; SS of	image
oh_SP		dw	?		; SP of	image
oh_chksum	dw	?		; Ignored checksum of file
oh_IP		dw	?		; IP of	image
oh_CS		dw	?		; CS of	image
oh_rle_table	dw	?		; Byte offset to reloc table
oh_iov		dw	?		; Overlay number( 0 for	root )
oh_sym_tab	dd	?		; offset of symbol table in file
oh_40_BB	dw	?		; DOS 4.0 behavior bits
oh_win_BB	dw	?		; Windows behavior bits
oh_oem_ID	dw	?		; Reserved for OEM's
oh_oem_info	dw	?		; Reserved for OEM's
oh_res		dw	10 DUP (?)	; Reserved for future use
oh_nhdr_offset	dd	?		; offset to new	EXE header
oh_s	ENDS

EXE_VALID_SIGNATURE	EQU	"ZM"
EXE_VALID_OLDSIGNATURE	EQU	"MZ"


;	Relocation Data	Item format

ri_s	STRUC
ri_stype	db	?		; relocation source type
ri_flags	db	?		; relocation target type
ri_source	dw	?		; relocation source offset
ri_target_seg	dw	?		; internal target segment index
ri_target_off	dw	?		; internal target entry	offset index
ri_s	ENDS

ri_target_modnam EQU	ri_target_seg	; import module	name offset
ri_target_impnam EQU	ri_target_off	; imoprt procedure name	offset

SOURCE_MASK	EQU	07h
BYTE_ADR	EQU	00h		; Byte source location
SEG_ADR		EQU	02h		; Base source location
FAR_ADR		EQU	03h		; Pointer source location
OFF_ADR		EQU	05h		; Offset source	location
TARGET_MASK	EQU	03h
INTERNALREF	EQU	00h
IMPORTORDINAL	EQU	01h
IMPORTNAME	EQU	02h
ADDITIVE	EQU	04h

RELOCSPERBUF	EQU	8
RELOCBUFLEN	EQU	((SIZE ri_s)*RELOCSPERBUF)


;	Module Segment Table Entry format

ste_s	STRUC
ste_offset	dw	?		; file offset to segment data
ste_size	dw	?		; file data size (paragraphs)
ste_flags	dw	?		; type and attribute flags
ste_minsiz	dw	?		; minimum allocation size
ste_seghdl	dw	?		; segment handle
ste_selector	dw	?		; selector allocated for segment
ste_s	ENDS

;	ste_flags bit definitions

TYPE_MASK	EQU	0007h		; segment type field
CODE_TYPE	EQU	0000h		; code segment type
DATA_TYPE	EQU	0001h		; data segment type
ITERATED	EQU	0008h		; segment data is iterated
MOVABLE		EQU	0010h		; segment is not fixed
SHARED		EQU	0020h		; segment is shared
PRELOAD		EQU	0040h		; segment is not demand	loaded
ERONLY		EQU	0080h		; execute only (code segment)
RELOCINFO	EQU	0100h		; segment has relocation records
CONFORMING	EQU	0200h		; conforming code segment
SEGDPL		EQU	0C00h		; reserved for 286 DPL bits
RING_2		EQU	0800h		; Privilege level 2
RING_3		EQU	0c00h		; privilege level 3
DISCARD		EQU	1000h		; discardable
;;;;;;;		EQU	2000h		; unused bit (used to be for resources)
SELALLOC	EQU	4000h		; indicates selector allocated


;	Following flags are used by LdrZeroSte routine to determine
;	whether the segment handle, selector, are to zeroed out in the ste

ZEROHNDL	EQU	1		; zero out segment hndl in ste
ZEROSEL		EQU	2		; zero out selector in ste

;	buffer to save module name or procedure	name

BUF_LENGTH	equ	64

;	record structure to be maintained by the RMP (Record Management
;	Package)
;
;	dld_cnt		is incremented everytime a process makes a call
;			to DOSLOADMODULE for loading a certain module. It is
;			decremented when the process makes a call to
;			DOSFREEMODULE requesting that the module be freed.

dld_s	STRUC
	dld_ptda	dw	?
	dld_mte		dw	?
	dld_cnt		db	?
dld_s	ENDS


if 0	; BUGBUG 11-Jul-1989 sudeepb: No one uses these two macros and they
	; wont work with current SetTrap

;*	SetLTrap - make	"load_trap" GP fault handler for this thread
;
;	The SetTrap macro is found in except.inc, which	must be	included.
;
SetLTrap macro
	SetTrap	<offset	DOSHIGHCODE:load_trap>
	endm


;*	SetLTrap2 - make "load_trap2" GP fault handler for this	thread
;
;	The SetTrap macro is found in except.inc, which	must be	included.
;
SetLTrap2 macro
	SetTrap	<offset	DOSHIGHCODE:load_trap2>
	endm

endif	;BUGBUG 11-Jul-1989 sudeepb
