;       SCCSID = @(#)rasmacro.inc       6.2 91/01/03
;      SCCSID = @(#)rasmacro.inc        6.2 91/01/03
; ****************************************************************************
; *                                                                          *
; *                       IBM/Microsoft Confidential                         *
; *                                                                          *
; *                 Copyright (c) IBM Corporation  1987, 1990                *
; *                 Copyright (c) Microsoft Corp.  1987, 1990                *
; *                           All Rights Reserved                            *
; *                                                                          *
; ****************************************************************************

;
;rasmacro.inc - RAS Macro for kernel callers to System Trace and Dekko Hooks
;

;/************************** START OF SPECIFICATIONS *****************
;/*
;/*     SUBROUTINE NAME:   TestTrace
;/*
;/*     DESCRIPTIVE NAME:  Macros to test major event code
;/*
;/*     FUNCTION: For performance sensitive kernel callers to include
;/*               for querying the enable/disable status of the major
;/*               event code for System Trace. The Major Event Code
;/*               array in the global kernel data area is used to
;/*               check the status of the event code.
;/*
;/*     NOTES:
;/*
;/*     ENTRY POINT:  TestTrace
;/*        LINKAGE:  Macro with parameter N
;/*
;/*     INPUT: N = the caller's major event code (0 - 255)
;/*            and a segment register set up to point to the group
;/*            containing the kernel's global data segment (DOSGROUP)
;/*            and an 'ASSUME' must indicate which register it is.
;/*
;/*
;/*     EXIT-NORMAL: Zero Flag: 1 = event disabled or no System
;/*                                   Tracing
;/*                             0 = event enabled
;/*
;/*     EXIT-ERROR:
;/*           assembly error will result if 'N' parameter
;/*           is missing or out of range; an assembly error may result
;/*           if 'ASSUME' not set up correctly by user
;/*
;/*     EFFECTS:
;/*         Uses: Flags
;/*
;/*     INTERNAL REFERENCES: NONE
;/*
;/*     EXTERNAL REFERENCES: NONE
;/*
;/*************************** END OF SPECIFICATIONS ******************

TestTrace MACRO mec
   EXTRN ras_mec_table:BYTE
;;macro to test if specified major code is currently enabled - return
;;zero flag = zero if code is enabled or zero flag = 1 if disabled
;;or System Tracing doesn't exist.
  .ERRB <mec>                     ;;assembler error if mec blank
  .ERRNZ mec LT 0 OR mec GT 255   ;;assembler error if mec out of range
;test if specified major code is currently enabled - return in zero flag
  test DOSGROUP:ras_mec_table+(mec SHR 3), 80h SHR (mec AND 7)
endm


TestTrace32 MACRO mec
   EXTRN ras_mec_table:BYTE
;;macro to test if specified major code is currently enabled - return
;;zero flag = zero if code is enabled or zero flag = 1 if disabled
;;or System Tracing doesn't exist.
  .ERRB <mec>                     ;;assembler error if mec blank
  .ERRNZ mec LT 0 OR mec GT 255   ;;assembler error if mec out of range
;test if specified major code is currently enabled - return in zero flag
  test es:ras_mec_table+(mec SHR 3), 80h SHR (mec AND 7)
endm


VddTestTrace32 MACRO mec
;  EXTRN pMEC:BYTE
;;macro to test if specified major code is currently enabled - return
;;zero flag = zero if code is enabled or zero flag = 1 if disabled
;;or System Tracing doesn't exist.
  .ERRB <mec>                     ;;assembler error if mec blank
  .ERRNZ mec LT 0 OR mec GT 255   ;;assembler error if mec out of range
;test if specified major code is currently enabled - return in zero flag
; test [pMECtable]+(mec SHR 3), 80h SHR (mec AND 7)
  mov  eax, pMECtable
  test BYTE PTR [eax+(mec SHR 3)], 80h SHR (mec AND 7)
endm



TestTraceFlatCS MACRO mec
   EXTRN ras_mec_table:BYTE
;;macro to test if specified major code is currently enabled - return
;;zero flag = zero if code is enabled or zero flag = 1 if disabled
;;or System Tracing doesn't exist.
  .ERRB <mec>                     ;;assembler error if mec blank
  .ERRNZ mec LT 0 OR mec GT 255   ;;assembler error if mec out of range
;test if specified major code is currently enabled - return in zero flag
  test cs:[ras_mec_table+(mec SHR 3)], 80h SHR (mec AND 7)
endm


TestTraceDLL MACRO mec
;;macro to test if specified major code is currently enabled - return
;;zero flag = zero if code is enabled or zero flag = 1 if disabled
;;or System Tracing doesn't exist.
  .ERRB <globalseg>               ;;assembler error if mec blank
  .ERRB <mec>                     ;;assembler error if mec blank
  .ERRNZ mec LT 0 OR mec GT 255   ;;assembler error if mec out of range
;test if specified major code is currently enabled - return in zero flag
  test es:[0].SIS_mec_table+(mec SHR 3), 80h shr (mec AND 7)
endm


; Static trace major codes still in use...
; Minor codes are calculated in-place from traced data.

RAS_MC_MACHINEEXCEPT            EQU     03h     ; Machine Exceptions
RAS_MC_HARDINT                  EQU     04h     ; Hardware IRQ's
RAS_MC_KERNEL                   EQU     05h     ; Kernel DynamicTrace
RAS_MC_DEVHLP                   EQU     06h     ; Devhlps


; Stack Structure for the DevHelp RAS Routines.  This is needed
; for the routines which expect either DS or DX to contain DevHelp
; parameters.


RASDevHlpStack STRUC
 RASStackIP       dw      ?                     ; Return IP
 RASStackEBP      dd      ?
 RASStackEDX      dd      ?                     ; Device Driver EDX
 RASStackDS       dw      ?                     ; Device Driver DS
RASDevHlpStack ENDS

;****************************************************************************
;
;       MACRO NAME..... SetupTest(32)
;
;       FUNCTION....... Test if the specified Major code active for tracing.
;                       Then test if performance tracing is currently on.
;                       If either test fails, jumps to indicated label.
;                       Otherwise loads major/minor code to collection device.
;
;       ARGUMENTS...... MAJ    - Major code to test
;                       LABEL  - Label to jump to if test fails
;                       MAJMIN - Major/Minor code to load if test succeeds
;
;****************************************************************************

SetupTest       MACRO           MAJ,LABEL,MAJMIN
               .ERRB <MAJ>                              ;; assembler error if
               .ERRB <LABEL>                            ;;  any parameter is
               .ERRB <MAJMIN>                           ;;  not given

                push    es
                DosContext      es
                TestTrace32     MAJ                     ;; 32 uses the ES reg
                jnz             st1&LABEL
                jmp             LABEL                   ;; Hook not active
    st1&LABEL:
                TestTrace32     PERFTRACE               ;; 32 uses the ES reg
                jnz             st2&LABEL
                jmp             LABEL                   ;; Trace not active
    st2&LABEL:
                push            es:SISData.SIS_MMIOBase
                pop             es
                assume          es:nothing
                mov             es:[MAJMIN_CODE],MAJMIN
                ENDM


SetupTest32     MACRO           MAJ,LABEL,MAJMIN
               .ERRB <MAJ>                              ;; assembler error if
               .ERRB <LABEL>                            ;;  any parameter is
               .ERRB <MAJMIN>                           ;;  not given

                push            es
                DosContext      es
                TestTrace32     MAJ
                jnz             st1&LABEL
                jmp             LABEL                   ;; Hook not active
    st1&LABEL:
                TestTrace32     PERFTRACE
                jnz             st2&LABEL
                jmp             LABEL                   ;; Trace not active
    st2&LABEL:
                sub             esp,2                   ;; pop ES uses 4 bytes
                push            es:SISData.SIS_MMIOBase
                pop             es
                assume          es:nothing
                mov             es:[MAJMIN_CODE],MAJMIN
                ENDM

VddSetupTest32  MACRO           MAJ,LABEL
               .ERRB <MAJ>                              ;; assembler error if
               .ERRB <LABEL>                            ;;  any parameter is

;               push            es
;               DosContext      es
                VddTestTrace32  MAJ
                jnz             st1&LABEL
                jmp             LABEL                   ;; Hook not active
    st1&LABEL:
                VddTestTrace32  PERFTRACE
                jz              LABEL
                ENDM

;****************************************************************************
;
;       MACRO NAME..... SetupTestDLL
;
;       FUNCTION....... Performs the same test as the SetupTest macro defined
;                       above, but used by DLL hooks.
;
;       ARGUMENTS...... MAJ       - Major code to test
;                       LABEL     - Label to jump to if test fails
;                       MAJMIN    - Major/Minor code to load if test succeeds
;                       GLOBALSEG - Pointer to infoseg from which to get info
;                       BIT32     - If defined, hook is in 32-bit DLL code
;
;****************************************************************************

SetupTestDLL    MACRO           MAJ,LABEL,MAJMIN,GLOBALSEG,BIT32
               .ERRB <MAJ>                              ;; assembler error if
               .ERRB <LABEL>                            ;;  any parameter is
               .ERRB <MAJMIN>                           ;;  not given
               .ERRB <GLOBALSEG>

                push            es
                push            SEG GLOBALSEG           ;; set ES to data seg
                pop             es                      ;;    of infoseg ptr
                cmp             es:GLOBALSEG,0          ;; infoseg ptr set
                je              LABEL                   ;; infoseg is not set
                                                        ;;    skip hook
                IFNB <BIT32>                            ;; if 32 bit code, the
                sub             esp,2                   ;;    pop ES will free
                ENDIF                                   ;;    4 bytes of the stk
                push            es:GLOBALSEG            ;; set ES to the
                pop             es                      ;;    infoseg segment
                TestTraceDLL    MAJ                     ;; test if major code on
                jz              LABEL                   ;;    jump if not
                TestTraceDLL    PERFTRACE               ;; test if traceing on
                jz              LABEL                   ;;    jump if not

                IFNB <BIT32>                            ;; if 32 bit code, the
                sub             esp,2                   ;;    pop ES will free
                ENDIF                                   ;;    4 bytes of the stk
                push            es:[0].SIS_MMIOBase     ;; set ES to DEKKO area
                pop             es
                assume          es:nothing
                mov             es:[MAJMIN_CODE],MAJMIN ;; DEKKO: maj/min codes
                ENDM


;****************************************************************************
;
;       MACRO NAME..... ExitMMPH
;
;       FUNCTION....... Restores the ES register from the value set by the
;                       Setup(32,DLL) macro. Always the last thing done.
;
;****************************************************************************

ExitMMPH        MACRO
                pop     es
                assume  es:nothing
                ENDM
