/*static char *SCCSID = "@(#)codelock.h	6.2 91/09/26";*/
//      SCCSID = @(#)codelock.h	6.2 91/09/26
//
// CODELOCK.INC -- macros and equates for code locking
//
// This file contains equates and macros useful for locking
// ranges of code.

/* ASM
if2
   EXTRNF32 CodeLockProc
   EXTRNF32 CodeUnlockProc
endif
*/

// Structure emitted by InitCodeLocks.  Contains run-time info needed by
// VMLock and the sempahore routines that guard its invocation.
//

/*
 * CodeLock entry.  Requires tkexport.h for toe_t definition
 */

struct     CodeLock_s   {
   struct toe_s   cl_toe;
   struct vmlh_s  cl_lhand;
   unsigned short cl_count;
   unsigned short cl_flags;
   unsigned long  cl_rangelen;
   unsigned long  cl_startaddr;
   KSEMMTX     cl_semaphore;
};

typedef struct CodeLock_s CodeLock_t;


//
// Equates that represent codelock groups.  To be used as parameters to the
// CodeLock and CodeUnlock macros.
//

#define FATEA_GROUP  (0* sizeof (CodeLock_s) )
#define NMPIPE_GROUP (1* sizeof (CodeLock_s) )
#define FSCTL_GROUP  (2* sizeof (CodeLock_s) )
#define FILEIO_GROUP (3* sizeof (CodeLock_s) )
#define OPLOCK_GROUP (4* sizeof (CodeLock_s) )
#define DRVLOCK_GROUP   (5* sizeof (CodeLock_s) )
#define NEWNODE_GROUP   (6* sizeof (CodeLock_s) )
#define FSSWAP_GROUP (7* sizeof (CodeLock_s) )
#define NOTIFY_GROUP (8* sizeof (CodeLock_s) )

#define NUMCODELOCKGROUPS   9

// some convenient equates in case a routine needs to keep track of whether
// its code is locked.

#define CODE_IS_LOCKED      1
#define CODE_NOT_LOCKED     0

#define CODELOCK_ATTEMPT_COUNT   3      // try the lock three times
#define CODELOCK_RETRY_WAIT   250       // 250 milliseconds
#define CODELOCK_BLOCK_ID  0xC0DE0000  // CODE lock block ID

#define CL_ISLOCKED     0x01   // section of code is locked
#define CL_UNLOCKPENDING   0x02   // code will be unlocked when hook runs
#define CL_TOESET    0x04   // timer is running

#define CODEUNLOCKDELAY    0x02000  // 2 seconds (2000 milliseconds)


//*** CodeLock -- macro to properly generate call to CodeLockProc
//
// This macro sets things up the way CodeLockProc expects and then
// calls that routine to actually accomplish the code locking.
//
// ENTRY:   group = lock group to be locked (see above)
//    errlabel = code label to branch to if CodeLockProc
//       returns an error and special cleanup is needed.
//       Ordinarily leave blank.
//
// USES: none (not even flags!) if no error
//    ax, Flags if lock fails
//
/* ASM
CodeLock macro group,errlabel
   local clnoerr

   CpuMode  386

   pushfd
   SaveReg  <ebx>
   mov   ebx, group
   CALLF32  CodeLockProc
   RestoreReg <ebx>  
   jnc   clnoerr

   popfd
   stc
   ifnb  <errlabel>     ; for special calling conventions
      jmp   errlabel
   else
      ret
   endif
clnoerr:
   popfd
   CpuMode reset
   ENDM
*/

//*** CodeUnlock -- macro to properly generate call to CodeUnlockProc
//
// This macro sets things up the way CodeUnlockProc expects and then
// calls that routine to actually accomplish the code unlocking.
//
// ENTRY:   group = lock group to be unlocked (see above)
//
// USES: none (not even flags!)
//
/* ASM
CodeUnlock  macro group

   CpuMode  386   

   pushfd
   SaveReg  <ebx>

   mov   ebx, group
   CALLF32  CodeUnlockProc

   RestoreReg <ebx>
   popfd

   CpuMode  reset
   ENDM
*/
