/*static char *SCCSID = "@(#)ldrxport.h 6.6 92/02/21";*/
/*      SCCSID = @(#)ldrxport.h 6.6 92/02/21 */

/*
 *      This file contains the exported routines from the Loader
 *
 *      IBM/Microsoft Confidential
 *
 *      Copyright (c) IBM Corporation 1987, 1989
 *      Copyright (c) Microsoft Corporation 1987, 1989
 *
 *      All Rights Reserved
 *
 */

extern  int     LDRGetLibNameToHeap(VMHOB hMTE, ushort_t *pcchLibName,
                                    char **ppLibName);

extern  int     LDRGetLibName(VMHOB, char *, int);

//      See dynlocal.h for LDRGetDYNMteInfo due to it's limited scope.

extern  int     LDRGetContext(VMHOB hMTE, VMHOB *phptda);

extern  int     LDRIsResource(VMHOB hob, VMHOB hMTE, ulong_t prsrc);

extern  int     LDRCheckModHandle(VMHOB hMTE);

extern  int     LDRNumtoAddr(VMHOB hMTE, ulong_t lobjnum, ulong_t *paddar);

extern  int     LDRFreeSegment(VMHOB hMTE, ulong_t laddr, SEL sel);

extern  void    LDRClearSem(void);

extern  int     LDRTstnSetSem(void);

extern  void    LDRFreeTask(PPTDA pptda);

extern  int     LDRFreeObject(VMHOB hMTE, VMHOB hob, ulong_t lflags,
                                PPTDA pptda);

extern  int     LDRFreeModule(VMHOB hMTE, int flags);

extern  void    LDRUpdateGlobalErrTxt(VMHOB hMTE);

extern  void    LDRCleanupDLLTerm(void);

/* extern  void    LDRResetDLLOnExitList(VMHOB hMTE);   added - #74177 removed - #75809 */

extern  VMHOB   hMteDoscall1;           /* MTE handle of DOSCALL1.DLL */

extern  void    *pDosModMte;            /* Pointer to kernel mte */

extern  VMHOB   DosModHdl;              /* Handle to kernel mte */

extern  HKSEM           hNonResNamTbl;  /* Sem. of Non-Resident table */
extern  KSEMMTX         LdrSem;         /* Loader semaphore */
extern  ushort_t        LDRDDOwner;     /* latest DD owner */
extern  ushort_t        LDRFSDOwner;    /* latest FSD owner */
extern  ushort_t        cProcStartup;   /* # of process starting B724303 */

/*
 * Return values for DosQueryProcType
 */
#define PT_16BIT        0
#define PT_32BIT        1

extern  int LDRGetProcAddr( VMHOB hmte, ulong_t ulord, uchar_t *pchname,
                            ptr_t *paddress, int fflat, ulong_t *penttype );

/***ET+ ldrmi_t - Structure of Module info */

struct  ldrmi_s
{
    ushort_t  ldrmi_ldtype;   /* load type 16-bit or 32-bit module */
    ushort_t  ldrmi_attrib;   /* type of module */
    ptr_t     ldrmi_startaddr;/* start address of module */
    ptr_t     ldrmi_stackptr; /* starting stack address */
    ulong_t   ldrmi_stackbase;/* base of stack object */
    ushort_t  ldrmi_heap;     /* heap size */
    ushort_t  ldrmi_autods;   /* automatic data segment */
    ptr_t     ldrmi_phaddr;   /* porthole start addr */
    ushort_t  ldrmi_phflag;   /* flag to indicate porthole init */
};

typedef struct  ldrmi_s ldrmi_t;        /* loader module info ret buffer */

extern  int     LDRGetTKModuleInfo(VMHOB hMTE, ldrmi_t *pdata);

extern  int     LDRGetModuleType(VMHOB hMTE, ushort_t *pType);

extern  ulong_t LDRIsMTEDetaching       (VMHOB hmte);

/***EP  LDRGetSem - Get the loader semaphore
 *
 *      Call KSEMRequestMutex to obtain semaphore.
 *
 *      ENTRY   None
 *
 *      EXIT    int             - return code (NO_ERROR if successful)
 *                                or ERROR_INTERRUPT
 *
 *      EFFECTS                 - semaphore owned by current thread
 *
 *      NOTE:   This is implemented as a macro.
 */

// XLATOFF
#define LDRGetSem()     KSEMRequestMutex((HKSEM)&LdrSem, -1)
// XLATON

/* ASM

LDRGetSem macro
ifndef  _KSEMRequestMutex
        EXTRN32 _KSEMRequestMutex
endif
ifndef  _KSEMQueryMutex
        EXTRN32 _KSEMQueryMutex
endif
ifndef  _KSEMReleaseMutex
        EXTRN32 _KSEMReleaseMutex
endif
ifndef  _LdrSem
        ReferFlat       _LdrSem,DWORD
endif
        CCALL32 _KSEMRequestMutex, <<offset FLAT:_LdrSem>, -1>
        endm

 */

#define LDR_16bit       0               /* 16-bit module */
#define LDR_32bit       1               /* 32-bit module */
#define LDR_PORTHOLE    2               /* Porthole Init module */

extern  int     LDRSetTKLibiFlags(VMHOB hMTE, int flags, int fSet);

// XLATOFF
#define LDRSetTKGlobalSetup(hMTE)   LDRSetTKLibiFlags(hMTE, LDRGLOBALSETUP, TRUE)
#define LDRResetTKGlobalSetup(hMTE) LDRSetTKLibiFlags(hMTE, LDRGLOBALSETUP, FALSE)
#define LDRSetTKGlobalDone(hMTE)    LDRSetTKLibiFlags(hMTE, LDRGLOBALDONE,  TRUE)
// XLATON

#define LDRPROGRAM      0x1   /* this is a Program module */
#define LDRLIBRARY      0x2   /* this is a Library module */
#define LDRDEVICE       0x4   /* this is a Device Driver module */
#define LDRFSD          0x8   /* this is a Installable file system */
#define LDRLIBINIT      0x10  /* per-process library init */
#define LDRGLOBALSETUP  0x20  /* flag set and used by TK */
#define LDRDLLTERM      0x40  /* per-process DLL termination */
#define LDRGLOBALDONE   0x80  /* flag set and used by TK */

/* DOSIQAPPTYPE/ldrQAppType values */

#define QAT_DYNLINK             0x00000010
#define QAT_DOSEXE              0x00000020
#define QAT_PDD                 0x00000040
#define QAT_VDD                 0x00000080

#define QAT_PROTDYNLINK         0x00000100
#define QAT_WINDOWS_REAL        0x00000200      /* Windows Real Mode app */
#define QAT_WINDOWS_PROT        0x00000400      /* Windows Prot Mode app */
#define QAT_WINDOWS_PROT31      0x00001000      /* Windows Prot Mode app */
#define QAT_BOUNDEXE            0x00000800

#define QAT_32BIT               0x00004000

// Shift to use !
#define QAT_NOPMW               0x0100          /* Not Window compatible */
#define QAT_PMW                 0x0200          /* Window compatible */
#define QAT_PMAPI               0x0300          /* PM app */
#define QAT_EXETYPE             0x0700

#define QAT_EXETYPE_VDD         QAT_VDD

#define QAT_MASK                ( QAT_EXETYPE | QAT_BOUNDEXE )
#define QAT_EXETYPE_MASK        ( QAT_MASK  >> 8 )
#define QAT_EXETYPE_PMAPI       ( QAT_PMAPI >> 8 )
#define QAT_EXETYPE_PMW         ( QAT_PMW   >> 8 )  /* WINDOWCOMPAT */
#define QAT_EXETYPE_NOPMW       ( QAT_NOPMW >> 8 )  /* NOTWINDOWCOMPAT */


extern  void    LDRFreeNonResNamSegs(void);

extern  void    LDRUseNonResNamSegs(void);


/***ET+ ldrrei_t - Structure of exec info for return from LDRLoadExe */

struct  ldrrei_s
{
    ptr_t     ei_startaddr;   /* instruction pointer */
    ptr_t     ei_stackaddr;   /* stack pointer */
    ptr_t     ei_pstackmax;   /* stack pointer max only for 32-bit */
    ushort_t  ei_ds;          /* starting ds only for 16-bit */
    ushort_t  ei_stacksize;   /* size of stack only for 16-bit */
    ushort_t  ei_heapsize;    /* size of heap only for 16-bit */
    ushort_t  ei_loadtype;    /* Load type 16-bit or 32-bit */
};

typedef struct ldrrei_s ldrrei_t;       /* Loader return exec info */
/*end*/

extern int LDRLoadExe(uchar_t *pachModname, ldrrei_t *pexec_info);

/***ET+ ldrlf_t - Structure used during demand loading pages during fault */

struct ldrlf_s
{
    ulong_t  lf_pallocbuf;          /* Address of PGAllocBuf */
    ulong_t  lf_faultaddr;          /* Address of faulting page */
    ulong_t  lf_sallocbuf;          /* seg addr of alloc buf from PG */
    ulong_t  lf_objaddr;            /* TRUE address of page, if aliased */
    ulong_t  lf_bno;                /* Block number */
    ushort_t lf_hMTE;               /* MTE handle */
};
typedef struct ldrlf_s ldrlf_t;         /* LDRGetPage parameters */

/*end*/

extern  int     LDRGetPage(ldrlf_t *plf);

extern  int     LDRGetSegment(VMHOB hMTE, ulong_t laddr, ushort_t sel);

/***ET+ ldroi_t - Structure of return info for object info call */

struct  ldroi_s
{
    ulong_t oi_size;       /* object virtual size */
    ulong_t oi_base;       /* object base virtual address */
    ulong_t oi_flags;      /* object attribute flags */
    ulong_t oi_pagemap;    /* object page map index or block num */
};

typedef struct  ldroi_s ldroi_t;        /* Loader return object info */

extern  int     LDRObjInfo(void *pmte, ushort_t hob, ldroi_t *pret);

/***ET+ ldrpi_t - Structure of return info for page info call */

struct  ldrpi_s
{
    uchar_t pi_attrib;        /* page attributes */
};

typedef struct  ldrpi_s ldrpi_t;        /* Loader return page info */

extern  int     LDRPageInfo(void *pmte, ulong_t bno, ldrpi_t *pret);

extern  int     LDRCheckSig(void);

extern  int     LDRLoadVDD(char *pachModname);

extern  int     LDRLoadVDDInstance(VMHOB hMTE);

extern  int     LDRUnloadVDD(VMHOB hMTE);

extern  void    LDRFreeVDDMTEs(void);

#define ZEROHNDL        1               /* zero out segment hndl in ste */
#define ZEROSEL         2               /* zero out selector in ste */

/*end*/


struct adce_s
{
    ushort_t    adce_ring0;
    ushort_t    adce_ring3;
};

/*XLATOFF*/
extern struct adce_s ldrMteAux[];
/*XLATON*/
#define AUXORD_DOSGETINFOSEG            0
#define AUXORD_DOSLOCALINFO             1
#define AUXORD_DOSGLOBALINFO            2
#define AUXORD_DOSFLATDS                3
#define AUXORD_DOSFLATCS                4
#define AUXORD_DOSTIB                   5


/********** Dirty Worker Command Codes
 */

#define LDRDWFIRSTCMD   1     /* Start of command table */
#define LDRDWDDIFSDONE  1     /* SysInit to call to say PDD/IFS init complete */
#define LDRDWSYIDONE    2     /* SysInit to call to say all init complete */
#define LDRDWLASTCMD    2     /* End of command table */

