home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l331 / 1.ddi / EXAMPLES / 386VMM / PFHNDLR.ASM next >
Encoding:
Assembly Source File  |  1990-11-15  |  4.9 KB  |  200 lines

  1.     .prot
  2.     .xlisti
  3.     include dosx.ah
  4.     include hw386.ah
  5.  
  6. comment ~**********************************************
  7.  
  8. repl_hndlr(low_lin, high_lin, expf)
  9. ULONG    low_lin;
  10. ULONG    high_lin;
  11. ULONG    expf;
  12.  
  13. tick_hndlr(low_lin, high_lin)
  14. ULONG    low_lin;
  15. ULONG    high_lin;
  16.  
  17. pgin_hndlr(linadr)
  18. ULONG    linadr;
  19.  
  20. Description:
  21.     Handlers for page replacement.  As simple as 
  22.     possible -- we simply do a circular scan 
  23.     always selecting the first page we find
  24.     as the page to be swapped.
  25.     Since we don't take any program activity into
  26.     account when selecting a page to swap, 
  27.     neither the page aging handler called 
  28.     periodically, nor the handler called each
  29.     time a virtual page is read into memory,
  30.     need to do anything.
  31.  
  32. Calling arguments:
  33.     low_lin        bottom of virtual region
  34.     high_lin    top of virtual region
  35.     expf        T ==> EXP page only
  36.             F ==> any swappable page
  37.     linadr        lin addr of page just
  38.             swapped in
  39.  
  40. Returned values:
  41.     0        if no pages to swap
  42.     otherwise    linear addr of page to swap
  43. ~******************************************************
  44. ;
  45. ; Since the linker creates a single program segment
  46. ; for a protected mode program, and both CS and DS
  47. ; point to it, we can put any global data 
  48. ; needed in our code segment and still get to it
  49. ; from DS -- all we have to do is tell the assembler
  50. ; we can get to it by ASSUMEing that DS points to the
  51. ; code segment.  (Note, of course, that when the
  52. ; handler gets control DS is undefined and we have
  53. ; to set it to our data segment).
  54. ;
  55. ; The advantage of putting the data in the same segment
  56. ; as the handler code is that we know they will end
  57. ; up next to each other when the program is linked,
  58. ; which makes it simpler to lock our handler code and
  59. ; data in memory (if the code and data are in two
  60. ; different segments, they may not be adjacent so
  61. ; we would have to do two separate locks, one for the
  62. ; code and one for the data).
  63. ;
  64.     assume    cs:text,ds:text
  65. text    segment    byte public use32 'code'
  66.  
  67.     public    hndlr_startp,hndlr_endp
  68.     public    tick_hndlr,pgin_hndlr,repl_hndlr
  69.  
  70. ;
  71. ; Start of handler code & data to be locked in
  72. ; memory
  73. ;
  74. hndlr_startp label byte
  75.  
  76. ;
  77. ; Global data needed by handlers.  
  78. ;
  79. swap_lin dd    0    ; linear addr of last page 
  80.                 ; swapped
  81.  
  82. tick_hndlr proc far    ; page aging handler
  83.     ret
  84. tick_hndlr endp
  85.  
  86. pgin_hndlr proc far    ; page read in handler
  87.     ret
  88. pgin_hndlr endp
  89.  
  90. repl_hndlr proc far    ; select page for replacement
  91. ;
  92. ; Stack frame
  93. ;
  94. #EXPF    equ    (dword ptr 20[ebp])
  95. #HIGH_LIN equ    (dword ptr 16[ebp])
  96. #LOW_LIN equ    (dword ptr 12[ebp])
  97.  
  98.     push    ebp        ; Set up stack frame
  99.     mov    ebp,esp            ;
  100.     push    es        ; Save modified regs
  101.     push    fs            ;
  102.     push    ds            ;
  103.     push    esi            ;
  104.     push    edi            ;
  105.     mov    ax,14h        ; set up DS to our data
  106.     mov    ds,ax            ; segment
  107.     mov    ax,SS_PGTAB    ; set up ES to point to 
  108.     mov    es,ax            ; page table seg
  109.     mov    ax,SS_PTINF    ; set up FS to page table 
  110.     mov    fs,ax            ; info segment
  111.  
  112.     mov    eax,swap_lin    ; adjust last page 
  113.     cmp    eax,#LOW_LIN        ; swapped if out
  114.     jb    short #fix        ; of range
  115.     cmp    eax,#HIGH_LIN        ;
  116.     jb    short #ok        ;
  117. #fix:                    ;
  118.     mov    eax,#LOW_LIN        ;
  119.     mov    swap_lin,eax        ;
  120. #ok:
  121.  
  122. ;
  123. ; Perform a circular scan of all page table entries 
  124. ; in the virtual linear region, starting with 1 past the
  125. ; last page swapped, to find the first page we can swap.
  126. ;    EDI = page number of end of virtual region
  127. ;    ESI = page number of start of virtual region
  128. ;    EDX = current page number
  129. ;
  130.     mov    eax,#LOW_LIN    ; branch if virtual linear 
  131.     cmp    eax,#HIGH_LIN        ; region null
  132.     jae    #err            ;
  133.     mov    esi,#LOW_LIN    ; set ESI to start of range,
  134.     shr    esi,PAGE_SHIFT        ; EDI to end
  135.     mov    edi,#HIGH_LIN        ;
  136.     shr    edi,PAGE_SHIFT        ;
  137.     mov    edx,swap_lin    ; start with 1 past last 
  138.     shr    edx,PAGE_SHIFT        ; page swapped
  139.     inc    edx            ;
  140.     cmp    edx,edi            ;
  141.     jb    short #loop        ;
  142.     mov    edx,esi            ;
  143. #loop:
  144.     mov    eax,es:[edx*4]    ; get pg tbl entry
  145.     test    eax,PE_PRESENT    ; branch if not present
  146.     jz    short #next        ;
  147.     test    eax,PE_LOCKED    ; branch if locked
  148.     jnz    short #next        ;
  149.     test    eax,PE_ALLOC    ; branch if not allocated
  150.     jz    short #next        ; (mapped)
  151.     cmp    #EXPF,FALSE    ; if any page OK, 
  152.     je    short #done        ; we're done
  153.     mov    eax,fs:[edx*4]    ; get pg tbl info entry
  154.     test    eax,PTI_ONDISK    ; branch if not on disk
  155.     jz    short #next        ;
  156.     test    eax,PTI_INEXP    ; branch if in EXP file
  157.     jnz    short #done        ;
  158. #next:
  159.     inc    edx        ; step to next page
  160.     cmp    edx,edi            ; & continue
  161.     jb    #loop            ;
  162.     mov    edx,esi            ;
  163.     jmp    #loop            ;
  164. #done:
  165.  
  166. ;
  167. ; OK, done with loop - return linear addr of page 
  168. ; we found
  169. ;
  170.     mov    eax,edx        ; return linear addr of
  171.     shl    eax,PAGE_SHIFT        ; page to swap
  172.     mov    swap_lin,eax    ; update last pg swapped
  173.  
  174. #exit:
  175.     pop    edi        ; Restore modified regs
  176.     pop    esi            ; 
  177.     pop    ds            ;
  178.     pop    fs            ;
  179.     pop    es            ;
  180.     mov    esp,ebp        ; restore stack frame
  181.     pop    ebp            ; & exit
  182.     ret                ;
  183.  
  184. #err:
  185. ;
  186. ; Couldn't find any pages eligible for swapping
  187. ;
  188.     xor    eax,eax        ; return no available
  189.     jmp    #exit            ; pages to swap
  190. repl_hndlr endp
  191.  
  192. ;
  193. ; End of handler code and data
  194. ;
  195. hndlr_endp label byte
  196.  
  197. text    ends
  198.  
  199.     end
  200.