/*static char *SCCSID = "@(#)tklocal.h  6.4 91/08/07";*/
/*      static char *SCCSID = "@(#)tklocal.h    6.4 91/08/07"
 *
 *      IBM/Microsoft Confidential
 *
 *      Copyright (c) IBM Corporation 1987, 1989
 *      Copyright (c) Microsoft Corporation 1987, 1989
 *
 *      All Rights Reserved
 */

//      Tasking - Local Defines, Macros and Data Structures

#define TK_TYPE16       0
#define TK_TYPE32       1
#define TK_TYPEDT       2
// The value below is the size of ring 2 stack, the user thinks he has.
#define TKCBVR2STACK    0x200           // Ring 2 stack size
#define TKCBR2STACK     0x1000          // Ring 2 stack size
#define _4K             0x1000          // minimum stack required for libinit
                                        // or exit-list processing
#define CPGTSD          ((sizeof(tsd_t) + PAGESIZE - 1) >> PAGESHIFT)
#define CBOBJNAMEBUF    70              // size of loader error buffer
#define CBERRCODEBUF    10              // size of error  code  buffer


/***LT+ EXITLIST - Exit list data structures */

struct  ExEntry_s {
        struct ExEntry_s *exl_next;     // link to next block/order
        ptr_t            exl_addr;      // Exit list routine address
        USHORT           exl_class;     // order & position 0 thru 0x1FF
        USHORT           exl_type;      // 16:16 or 0:32
};
typedef struct  ExEntry_s EXENT;
typedef struct  ExEntry_s *PEXENT;
/*end*/


/***LT+ Priority Queue structure
 *
 */

struct pq_s {
        struct tcb_s *pq_ptcb;          /* TCB pointer */
        struct pq_s *pq_ppqlevnext;     /* Next priority level */
        struct pq_s *pq_ppqlevprev;     /* Previous priority level */
        struct pq_s *pq_ppqnext;        /* Next entry at same level */
        struct pq_s *pq_ppqprev;        /* Previous entry at same level */
};

typedef struct  pq_s    pq_t;           /* Priority queue element type */

// ASM ifdef pqfake_s                   ; Defined in tkexport.h
// ASM     .errnz  (size pqfake_s - size pq_s)
// ASM endif
/*end*/


/*
 * This describes the true mapping in memory of the TIB and
 * TIB2 -- the version dependent portion of the TIB. This structure
 * can be used internally to optimize access to TIB2.
 */

struct tibp_s {
   struct tib_s  tibp_indep;             // The version independent part
   struct tib2_s tibp_depend;            // The version dependent part
};

//XLATOFF

typedef struct tibp_s TIBP, *PTIBP;

/***EC+ TIB - TIB pool constant definitions */

#define CBPIB           (sizeof(pib_t) + sizeof(ULONG))
                                        /* Allow extra space for PM */
#define CBTIB           (sizeof(TIBP) + 2*sizeof(ULONG))
                                        /* Allow extra space for PM */
#define CTIBSKIP        ((CBPIB + CBTIB - 1) / CBTIB)
                                        /* Slots to skip to allow for PIB */
#define CTIBPERSEG      (_64K / CBTIB)  /* Count of TIBs per segment */
#define CSEGTIB         ((TKCTCBMAX + CTIBSKIP + CTIBPERSEG - 1) / CTIBPERSEG)
                                        /* Max. no. of TIB segments */
#define VMATIB          (VMAC_LOCANY | VMAC_ALIGNPAGE | VMAC_ARENAPRV | \
                        PG_SWAPPABLE | PG_ZEROFILL | PG_U | PG_R | PG_W)
#define VMSTIB          (VMS_COMMIT | PG_SWAPPABLE | PG_ZEROFILL | \
                        PG_U | PG_R | PG_W)
/*end*/


/***LP  tkGetPPIB - return PIB pointer
 *
 *      This function returns a pointer to the PIB for the specified
 *      PTDA.
 *
 *      tkGetPPIB (pptda)
 *
 *      ENTRY   pptda           - PTDA pointer
 *      RETURN  pib_t *         - Pointer to PIB for the process
 *
 *      CALLS   NONE
 *
 *      WARNING This procedure is implemented as a macro.
 *
 *      EFFECTS NONE
 */

#define tkGetPPIB(pptda)        ((pib_t *) (pptda)->ptda_avatib[0])


/***LP  tkUpdatePIB - force PIB update
 *
 *      This procedure causes the specified process' PIB to be
 *      updated the next time one of its threads exits the kernel.
 *
 *      tkUpdatePIB (pptda)
 *
 *      ENTRY   pptda           - PTDA pointer
 *      RETURN  NONE
 *
 *      CALLS   NONE
 *
 *      WARNING This procedure is implemented as a macro.
 *
 *      EFFECTS A forced action is set in the process' PTDA.
 */

#define tkUpdatePIB(pptda) \
        (pptda)->ptda_ProcType == LIS_PT_REALMODE || \
        ((pptda)->ptda_ForcedActions |= TK_FF_PIB)


/***LP  tkUpdateTIB - force TIB update
 *
 *      This procedure causes the specified thread's TIB to be
 *      updated the next time the thread exits the kernel.
 *
 *      tkUpdateTIB (ptcb)
 *
 *      ENTRY   ptcb            - TCB pointer
 *      RETURN  NONE
 *
 *      CALLS   NONE
 *
 *      WARNING This procedure is implemented as a macro.
 *
 *      EFFECTS A forced action is set in the thread's TCB.
 */

#define tkUpdateTIB(ptcb) \
        (ptcb)->TCBpPTDA->ptda_ProcType == LIS_PT_REALMODE || \
        ((ptcb)->TCBForcedActions |= TK_FF_TIB)


/***LP  tkTCBFree - return a TCB to the BMP pool
 *
 *      Given a TCB to free, this procedure calls BMPFree
 *      to free it.  No cleanup is done.  Most callers
 *      should use tkFreeTCB, not this procedure.
 *
 *      tkTCBFree (ptcb)
 *
 *      ENTRY   ptcb            - TCB pointer
 *      RETURN  NONE
 *
 *      Mark the TCB as free and call BMPFree.
 *
 *      CALLS   BMPFree
 *
 *      WARNING This procedure is implemented as a macro.
 *
 *      EFFECTS The TCB is returned to the pool of unallocated
 *              TCBs.
 */

#define tkTCBFree(ptcb) \
        { \
            (ptcb)->TCBpPTDA = NULL; \
            BMPFree(paTCB,ptcb); \
        }


/***LP  tkAssert - cause internal error if assertion fails
 *
 *      Given a boolean, this procedure causes an internal error if
 *      the boolean is false.  Typically, the value of this boolean is
 *      derived from some test expression.
 *
 *      tkAssert (f)
 *
 *      ENTRY   f               - boolean value (int)
 *      RETURN  NONE
 *
 *      CALLS   _assert
 *
 *      WARNING This procedure is implemented as a macro.
 *
 *      EFFECTS NONE
 */
#ifdef  MISCSTRICT
#  define tkAssert(e) _assert(e)
#else
#  define tkAssert(e)
#endif


/***LP  tkAssertTasktime - cause internal error if not running at Tasktime
 *
 *      This procedure causes an internal error if the system is not
 *      currently running at tasktime.
 *
 *      tkAssertTasktime ()
 *
 *      ENTRY   NONE
 *      RETURN  NONE
 *
 *      CALLS   _tkAssert
 *
 *      WARNING This procedure is implemented as a macro.
 *
 *      EFFECTS NONE
 */

extern LONG InterruptLevel;          /* Interrupt Level */

#define tkAssertTasktime()    tkAssert(InterruptLevel == -1)

extern int      tkAddExitListRoutine(ptr_t, USHORT, USHORT);
extern int      tkDeleteExitListRoutine(ptr_t, USHORT);
extern void     tkNextExitListRoutine(void);
extern void     tkCleanUpExitList(void);
extern int      tkGetExitListSem(void);
extern void     tkClearExitListSem(void);

extern void     tkLibiCleanup(PTCB);

extern int      tkScanBuffz(PINT, PCHAR, int, int);

extern void     tkThreadStart(void);
extern void     tkIThreadStart(void);
extern void     tkIThrdRetError(void);

extern int      tkAllocTCB(PPTDA pptda,
                        struct tkthrd_s *pti,
                        PTCB *pptcb,
                        ushort_t  stackcommit);

extern int      tkAllocPIB(PPTDA pptda);

extern int      tkAllocTIB(PTCB ptcb);

extern void     tkCheckStackUsage(PTCB ptcb);

extern void     tkFreeTCB(PTCB ptcb);

extern void     tkFreeTSD(PTCB ptcb);

extern void     tkInitSysInfo(void);            /* INIT time only */

extern void     tkInitThrdKStack(PTCB ptcb,
                        struct tkthrd_s *pti);

extern int      tkInitThrdUStack(PTCB ptcb,
                        struct tkthrd_s *pti);

extern void     tkLinkTCB(PTCB ptcb);

extern void     tkSetTIB(void);

extern void     tkStop(int rc, ULONG uladdr);

extern void     tkTSDDaemon(void);

extern void     tkUnlinkTCB(PTCB ptcb);

extern void     tkUnlinkTCBSub(PTCB ptcb);

extern void     tkUpdateAllTIBs(PPTDA pptda);

extern USHORT   tkNewPid(void);

extern void     tkEvapAll(void);

extern void     tkEvapPTDA(PPTDA );

extern void     tkFreeUserStacks(PTCB ptcb);

extern int      tkSetPTDAVars(PPTDA pptda,
                        struct tkthrd_s *pti);

extern void     tkSchedNext(void);

extern void     tkDupProcData(PPTDA pptda);

extern void     tkStartProcess(PPTDA pptda,
                        ULONG usstate,
                        ULONG ustype,
                        struct _RESULTCODES *prc);

extern USHORT   tkGetTCBReqPkt(PTCB ptcb);

extern void     tkFreeTCBReqPkt(PTCB ptcb);

extern void     tkRunWaitThreads(void);

//extern int      tkUsrMemError(ULONG errcode);                //157624
extern int      tkUsrMemError(ULONG faultaddr, ULONG errcode); //157624

extern int      tkSuBuff386(PCHAR pMemDst,
                        PCHAR pMemSrc,
                        int cb,
                        int fl);

extern int      tkSuBuff486(PCHAR pMemDst,
                        PCHAR pMemSrc,
                        int cb,
                        int fl);

extern  void    tkSleepQInsert (PTCB ptcb);

extern  void    tkSleepQRemove (PTCB ptcb);

extern  void    tkPriQRemoveChain (PTCB *pptcb, PTCB ptcb);

extern  void    tkPriQInsert (PTCB *pptcb, PTCB ptcb);

extern  void    tkPriQRemove (PTCB *pptcb, PTCB ptcb);

extern VOID  SigReset (ULONG ulExceptionNum, ULONG fHandled);

extern VOID  SigResetAPTERM (ULONG ulExceptionNum, ULONG fHandled);    //108449

extern int      tkBufferWithinUserDSLimit(PCHAR Buffer, int BufSize);  //b723248


extern  PPTDA   pPTDAFirst;
extern  PPTDA   pPTDAFirstZombie;
extern  PPTDA   pPTDACur;
extern  USHORT  FirstPTDA;
extern  USHORT  FirstZombiePTDA;
extern  struct  InfoSegGDT SISData;
extern  struct  infoseg_s Lis2;
extern  char    cInDos;
extern  int     (*pfntkSuBuff)(char *, char *, int, int);
extern  ulong_t tkTSDSwapRateMax;
extern  ulong_t tkMaxSlice;
extern  ulong_t tkMinSlice;
extern  ulong_t tkMaxWait;
extern  toe_t   toeTimeslice;
extern  toe_t   toeIdleDetect;
extern  ushort_t TaskNumber;
extern  ulong_t cmsecStarveLength;

/*
 *      PTDA and TCB BMP arrays.
 *      The first blocks in paPTDA and paTCB are actually the second
 *      PTDA and TCB in the system.  The Sysinit PTDA and TCB are
 *      not currently held there.  So, don't use these as arrays!
 */

extern  PPTDA   paPTDA;         // PTDA array (BMP object)
extern  PTCB    paTCB;          // TCB array (BMP object)
extern  TSS     tssTK;          // System TSS

/***LD  DosCall1 Thunk Addresses.
 */
extern  PFN             pfnXcptDispatcher32;    // 32-bit Exception Dispatcher
extern  PFN             pfnXcptFRestore32;      // 32-bit FP Restore
extern  PFN             pfntkDos32Exit;         // Dos32Exit thunk address
extern  struct FarPtr   pfntkDosExit;           // DosExit thunk address

// XLATON
