home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 February / psl_9403.zip / psl_9403 / DOS / UT_SYSTM / MWATCHTI.ZIP / MEMWATCH.ASM < prev    next >
Assembly Source File  |  1993-12-09  |  26KB  |  608 lines

  1. ;-------------------------------- MEMWATCH ---------------------------------
  2. ; A TSR to "watch" for all the ways a program might try to allocate memory.
  3. ;
  4. ; This includes EMS, XMS(UMB's and EMB's), Int 15, DPMI, and VCPI.
  5. ; Note: Programs that use higher level methods (like DPMI) may show a 
  6. ; "hit" on several different methods at the same time.
  7. ;---------------------------------------------------------------------------
  8. ; To effectively watch for EMS allocations, we need to do some trickery
  9. ; when we hook Int67.  Programs often look for an EMS driver by assuming
  10. ; Int67 is aimed into a device driver's code segment and then look into 
  11. ; what would be the device header at DDSeg:devicename.  We need to setup
  12. ; a "fake" device header they can scan through.
  13. ;---------------------------------------------------------------------------
  14. ; Things to do:
  15. ;
  16. ;  - Relocate some code/data into the PSP to reduce resident footprint.
  17. ;  - Think about adding usage counts
  18. ;  - Think about active PSP "scoped" reporting
  19. ;---------------------------------------------------------------------------
  20. ; Author: Tony Ingenoso 
  21. ;---------------------------------------------------------------------------
  22.  
  23. ;---------------------------------------------------------------------------
  24. ; We'll be monitoring these types of memory activities...
  25. ;---------------------------------------------------------------------------
  26. MemFlags struc
  27. fEMS32    db    0                       ; EMS 3.2 flag
  28. fEMS40    db    0                       ; EMS 4.0 flag
  29. fEEMS     db    0                       ; EEMS flag
  30. fHMA      db    0                       ; HMA flag
  31. fEMB      db    0                       ; XMS extended memory flag
  32. fUMB      db    0                       ; XMS UMB memory flag
  33. fDOSSTRAT db    0                       ; DOS UMB strategy flag
  34. fDOSLINK  db    0                       ; DOS UMB link flag
  35. fDOSHMA   db    0                       ; DOS managed HMA flag (slack space)
  36. fINT15    db    0                       ; Int 15 memory flag
  37. fDPMI     db    0                       ; DPMI memory flag
  38. fVCPI     db    0                       ; VCPI memory flag
  39.         ends
  40.  
  41. ShowMessage macro msg
  42.         mov     dx, offset msg
  43.         mov     ah, 9
  44.         int     21h
  45.         endm
  46.  
  47. memwatch segment para public 'code'
  48.         assume  cs:memwatch
  49.         org     100h                    ; This is a COM file.
  50. start   label   near
  51.         jmp     THROW_AWAY_CODE
  52.  
  53. ;--------------------------------------------------------------------------
  54. ; Old vectors/pointers to chain to
  55. ;--------------------------------------------------------------------------
  56.         align   4
  57. OldInt15 dd     0                       ; Saved Int 15 vector
  58. OldInt21 dd     0                       ; Saved Int 21 vector
  59. OldInt2F dd     0                       ; Saved Int 2F vector
  60. OldInt67 dd     0                       ; Saved Int 67 vector
  61. OldXMS   dd     0                       ; Saved XMS entry point
  62.  
  63. ;--------------------------------------------------------------------------
  64. ; Working flags......
  65. ;--------------------------------------------------------------------------
  66.         db      "MEMWATCH"              ; A signature
  67. MemVars MemFlags <>                     ; Flags structure
  68. MemVarsLen = $-MemVars
  69.  
  70. ;--------------------------------------------------------------------------
  71. ; The XMS hook to "watch" for XMS calls.
  72. ;--------------------------------------------------------------------------
  73. ; By taking over the Int 2F that returns the XMS entry point rather than
  74. ; patching into the existing driver, nice things happen.  All prior loaded
  75. ; TSR's and device drivers that call XMS will go directly to the original
  76. ; driver.  This means we won't see a bunch of "noise" that looks like it's
  77. ; comming from the app(s) being monitored.  We'll be seeing only his actions.
  78. ; We're really not interested in XMS activity from caches, VDISK's, etc.
  79. ;--------------------------------------------------------------------------
  80.         align   2
  81. XMShooker proc far
  82.         jmp     short XMS_hookability   ; So we can subsequently be hooked
  83.         nop                             ; out by some other program...  
  84.         nop                             ;
  85.         nop                             ;
  86.  
  87. XMS_hookability:
  88.         cmp     ah, 1                   ; Trying to allocate HMA?
  89.         jne     CheckAllocEMB           ;
  90.         mov     MemVars.fHMA, 1         ;
  91.         jmp     short ChainXMS          ;
  92.  
  93. CheckAllocEMB:
  94.         cmp     ah, 9                   ; Trying to allocate EMB?
  95.         jne     CheckAllocUMB           ;
  96.         mov     MemVars.fEMB, 1         ;
  97.         jmp     short ChainXMS          ;
  98.  
  99. CheckAllocUMB:
  100.         cmp     ah, 10h                 ; Trying to allocate UMB?
  101.         jne     ChainXMS                ;
  102.         mov     MemVars.fUMB, 1         ;
  103.  
  104. ChainXMS: jmp     [OldXMS]
  105. XMShooker endp
  106.  
  107. ;--------------------------------------------------------------------------
  108. ; The 15 hook to "watch" for extended memory moves.
  109. ;--------------------------------------------------------------------------
  110.         align   2
  111. Int15hooker proc far
  112.         cmp     ah, 87h                 ; Extended memory move?
  113.         jne     No15                    ;
  114.         mov     MemVars.fINT15, 1      ; Yes, we've seen one
  115. No15:   jmp     [OldInt15]              ; Chain it forward
  116. Int15hooker endp
  117.  
  118. ;--------------------------------------------------------------------------
  119. ; The 21 hook to "watch" for UMB activity
  120. ;--------------------------------------------------------------------------
  121.         align   2
  122. Int21hooker proc far
  123.         cmp     ax, 5801                ; Set strategy?
  124.         jne     LookForLink             ;
  125.  
  126.         ;-------------------------------------------------------------
  127.         ; Check the "set" strateg to see if it includes UMB's
  128.         ;-------------------------------------------------------------
  129.         cmp     bl, 40h                 ; Is the allocation
  130.         je      YesStrat                ;  strategy being set
  131.         cmp     bl, 41h                 ;   to something that
  132.         je      YesStrat                ;    will cause an
  133.         cmp     bl, 42h                 ;     allocation to come
  134.         je      YesStrat                ;      from a UMB?
  135.         cmp     bl, 80h                 ;
  136.         je      YesStrat                ;
  137.         cmp     bl, 81h                 ;
  138.         je      YesStrat                ;
  139.         cmp     bl, 82h                 ;
  140.         je      YesStrat                ;
  141.  
  142.         ;-------------------------------------------------------------
  143.         ; Check the "link" option to see if it's "linking in" UMB's
  144.         ;-------------------------------------------------------------
  145. LookForLink:                            ;
  146.         cmp     ax, 5803h               ; Is he trying to (un)link UMB's?
  147.         jne     No21                    ; Nope...
  148.         cmp     bx, 0001h               ; Add UMB's to chain?
  149.         je      YesLink                 ; Yes,...
  150.         jmp     short No21              ; No,...
  151.  
  152. YesStrat:
  153.         mov     MemVars.fDOSSTRAT, 1    ; Yes, we've seen one
  154.         jmp     short   No21
  155.  
  156. YesLink:
  157.         mov     MemVars.fDOSLINK, 1     ; Yes, we've seen one
  158.  
  159. No21:   jmp     [OldInt21]              ; Chain it forward
  160. Int21hooker endp
  161.  
  162. ;--------------------------------------------------------------------------
  163. ;     The 2F hook responds to our presence check and other things....
  164. ;--------------------------------------------------------------------------
  165.         align   2
  166. Int2Fhooker proc far
  167.         cmp     ax, 'EM'                ; Int2F presence check?
  168.         jne     TryDOSmanagedHMA        ; We're
  169.         cmp     bx, 'WM'                ;   looking
  170.         jne     TryDOSmanagedHMA        ;     for
  171.         cmp     cx, 'TA'                ;       "MEMWATCH"
  172.         jne     TryDOSmanagedHMA        ;         in the
  173.         cmp     dx, 'HC'                ;           AX:BX:CX:DX
  174.         jne     TryDOSmanagedHMA        ;