Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

587 lines
13 KiB

  1. TITLE GLRU - Primitives for LRU management
  2. .xlist
  3. include kernel.inc
  4. include newexe.inc
  5. include tdb.inc
  6. include protect.inc
  7. .list
  8. .386p
  9. DataBegin
  10. externB Kernel_InDOS
  11. externB Kernel_flags
  12. ;externW curTDB
  13. externW loadTDB
  14. externW pGlobalHeap
  15. ;externW hExeSweep
  16. ;externW WinFlags
  17. DataEnd
  18. sBegin CODE
  19. assumes CS,CODE
  20. externNP DPMIProc
  21. externW gdtdsc
  22. ;-----------------------------------------------------------------------;
  23. ; lrusweep ;
  24. ; ;
  25. ; Searches all of the exe headers in the system for segments that have ;
  26. ; been accessed since the last time through. For each segment found ;
  27. ; its referenced byte is reset and that segment is moved to the top ;
  28. ; of the lru chain. This routine is called (default) every 4 timer ;
  29. ; ticks from the int 8 handler. ;
  30. ; ;
  31. ; Arguments: ;
  32. ; none ;
  33. ; ;
  34. ; Returns: ;
  35. ; nothing ;
  36. ; ;
  37. ; Error Returns: ;
  38. ; ;
  39. ; Registers Preserved: ;
  40. ; DX,DI,SI,DS ;
  41. ; ;
  42. ; Registers Destroyed: ;
  43. ; AX,BX,CX,ES ;
  44. ; ;
  45. ; Calls: ;
  46. ; glrutop ;
  47. ; ;
  48. ; History: ;
  49. ; ;
  50. ; Tue Apr 21, 1987 06:22:41p -by- David N. Weise [davidw] ;
  51. ; Added the check for discarded segments. ;
  52. ; ;
  53. ; Wed Apr 08, 1987 11:00:59a -by- David N. Weise [davidw] ;
  54. ; Made it clear only the curTask's private handle table. ;
  55. ; ;
  56. ; Wed Feb 18, 1987 08:13:35p -by- David N. Weise [davidw] ;
  57. ; Added the sweeping of the private handle tables. ;
  58. ; ;
  59. ; Tue Feb 10, 1987 02:11:40a -by- David N. Weise [davidw] ;
  60. ; Added this nifty comment block. ;
  61. ;-----------------------------------------------------------------------;
  62. cProc lrusweep,<PUBLIC,FAR>
  63. cBegin nogen
  64. push edx
  65. push edi
  66. push esi
  67. push ds
  68. push es
  69. push fs
  70. xor edx,edx
  71. xor edi,edi
  72. SetKernelDS fs
  73. ; [notes from code review 4/91, added by donc 4/16/91
  74. ; 1) should check WinFlags1 for WF_PAGING,
  75. ; 2) insist on direct LDT access
  76. ; 3) try assembling without DPMI
  77. ; 4) have LRUSweepFreq= setting in .ini file to control update rate
  78. ; ]
  79. ; Excuses not to do the sweep:
  80. cmp kernel_InDOS,0 ; SMARTDrive and EMS may be present
  81. jnz short dont_do_it ; => disk buffers over the code segs
  82. cmp di,loadTDB ; Ignore interrupt if loading task
  83. jnz short dont_do_it
  84. mov ds,pGlobalHeap
  85. cmp di,ds:[di].gi_lrulock ; Ignore interrupt if inside memory
  86. jz short do_it ; manager
  87. dont_do_it:
  88. jmp sweepdone
  89. do_it:
  90. mov cx, ds:[di].gi_lrucount
  91. jcxz dont_do_it
  92. mov esi, ds:[di].gi_lruchain
  93. mov bx, gdtdsc
  94. or bx, bx ; See if we can poke at the LDT
  95. jz short sweep_loop_dpmi ; no, must use DPMI calls
  96. mov es, bx ; yes, ES points to the LDT
  97. ; Direct LDT access loop
  98. sweep_loop:
  99. mov esi, [esi].pga_lrunext
  100. mov bx, [esi].pga_handle
  101. sel_check bx
  102. if KDEBUG
  103. test es:[bx].dsc_hlimit, DSC_DISCARDABLE ; Ensure it really is discardable
  104. jnz short sweep_ok
  105. Debug_Out "lrusweep: Bad lru entry"
  106. sweep_ok:
  107. endif
  108. btr word ptr es:[bx].dsc_access, 0 ; Test and reset accessed bit
  109. .errnz DSC_ACCESSED-1
  110. jnc short sweep_next
  111. call glrutop ; Accessed objects are most recently used
  112. sweep_next:
  113. loop sweep_loop
  114. jmps sweepdone
  115. ; DPMI loop
  116. sweep_loop_dpmi:
  117. mov esi, [esi].pga_lrunext
  118. mov bx, [esi].pga_handle
  119. lar edx, ebx
  120. shr edx, 8 ; Access bits in DX
  121. if KDEBUG
  122. test dh, DSC_DISCARDABLE ; Ensure it really is discardable
  123. jnz short sweep_ok_dpmi
  124. Debug_Out "lrusweep: Bad lru entry"
  125. sweep_ok_dpmi:
  126. endif
  127. btr dx, 0 ; Segment accessed?
  128. .errnz DSC_ACCESSED-1
  129. jnc short sweep_next_dpmi ; no, leave it where it is on LRU list
  130. push cx
  131. mov cx, dx
  132. DPMICALL 0009h ; Set access word
  133. pop cx
  134. call glrutop ; Accessed objects are most recently used
  135. sweep_next_dpmi:
  136. loop sweep_loop_dpmi
  137. sweepdone:
  138. pop fs
  139. pop es
  140. pop ds
  141. pop esi
  142. pop edi
  143. pop edx
  144. ret
  145. UnSetKernelDS fs
  146. cEnd nogen
  147. ;
  148. ; Entry:
  149. ; DI == 0
  150. ; DS:SI.gi_lruchain -> head of list
  151. ; ES:0 -> arena header of object to insert
  152. ; DX = 0 => insert at head of list
  153. ; !=0 => insert at tail of list
  154. ;
  155. ; Exit:
  156. ; BX,DX destroyed
  157. ;
  158. cProc LRUInsert,<PUBLIC,NEAR>
  159. cBegin nogen
  160. inc ds:[di].gi_lrucount ; Increment count of LRU entries
  161. mov ebx,ds:[di].gi_lruchain ; BX = head of list
  162. or dx,dx ; Inserting at head of chain?
  163. jnz short lruins0 ; No, tail so dont update head
  164. mov ds:[di].gi_lruchain,esi ; Yes, make new one the new head
  165. lruins0:
  166. or ebx,ebx ; List empty?
  167. jnz short lruins1
  168. mov ds:[esi].pga_lruprev,esi; Yes, make circular
  169. mov ds:[esi].pga_lrunext,esi
  170. mov ds:[di].gi_lruchain,esi
  171. ret
  172. lruins1:
  173. mov edx,esi ; DX = new
  174. xchg ds:[ebx].pga_lruprev,edx
  175. mov ds:[edx].pga_lrunext,esi
  176. mov ds:[esi].pga_lruprev,edx
  177. mov ds:[esi].pga_lrunext,ebx
  178. ret
  179. cEnd nogen
  180. ;
  181. ; Entry:
  182. ; DI == 0
  183. ; DS:DI.gi_lruchain -> head of list
  184. ; DS_ESI -> arena header of object to delete
  185. ;
  186. ; Exit:
  187. ; EBX,EDX destroyed
  188. ;
  189. ;
  190. cProc LRUDelete,<PUBLIC,NEAR>
  191. cBegin nogen
  192. ;
  193. ; This is basically a consistency check, in case we don't fix
  194. ; GlobalRealloc() for 3.1.
  195. ;
  196. push eax
  197. mov eax,ds:[esi].pga_lrunext
  198. or eax,ds:[esi].pga_lruprev
  199. pop eax
  200. jz lrudel_ret
  201. dec ds:[di].gi_lrucount ; Decrement count of LRU entries
  202. jnz short lrudel0
  203. mov ds:[di].gi_lruchain,edi ; List empty, zero LRU chain.
  204. mov ds:[esi].pga_lruprev,edi; Zero pointers in deleted object
  205. mov ds:[esi].pga_lrunext,edi
  206. ret
  207. lrudel0:
  208. mov edx,esi
  209. cmp ds:[di].gi_lruchain,edx ; Are we deleting the head?
  210. jne short lrudel1
  211. mov edx,ds:[esi].pga_lrunext
  212. mov ds:[di].gi_lruchain,edx ; Yes, make it point to the next one
  213. lrudel1:
  214. xor ebx,ebx ; Zero pointers in deleted object
  215. xchg ds:[esi].pga_lrunext,ebx
  216. xor edx,edx
  217. xchg ds:[esi].pga_lruprev,edx
  218. mov ds:[edx].pga_lrunext,ebx
  219. mov ds:[ebx].pga_lruprev,edx
  220. lrudel_ret:
  221. ret
  222. cEnd nogen
  223. cProc glruSetup,<PUBLIC,NEAR>
  224. cBegin nogen
  225. mov bx,ds:[esi].pga_handle
  226. test bl, GA_FIXED
  227. jz short gsmoveable
  228. xor bx, bx ; Set ZF
  229. jmps gsdone
  230. gsmoveable:
  231. push ebx
  232. lar ebx, ebx
  233. test ebx, DSC_DISCARDABLE SHL 16
  234. pop ebx
  235. jz short gsdone
  236. or sp,sp
  237. gsdone:
  238. ret
  239. cEnd nogen
  240. ;-----------------------------------------------------------------------;
  241. ; glrutop ;
  242. ; ;
  243. ; Moves a discardable object to the head of the LRU chain. ;
  244. ; ;
  245. ; Arguments: ;
  246. ; DS:DI = address of global heap info ;
  247. ; ES:DI = global arena of moveable object ;
  248. ; ;
  249. ; Returns: ;
  250. ; Updated LRU chain ;
  251. ; ;
  252. ; Error Returns: ;
  253. ; ;
  254. ; Registers Preserved: ;
  255. ; CX,DX,SI,DS,ES ;
  256. ; ;
  257. ; Registers Destroyed: ;
  258. ; ;
  259. ; Calls: ;
  260. ; ;
  261. ; History: ;
  262. ; ;
  263. ; Wed Feb 18, 1987 08:30:45p -by- David N. Weise [davidw] ;
  264. ; Added support for EMS. ;
  265. ; ;
  266. ; Mon Oct 27, 1986 04:20:10p -by- David N. Weise [davidw] ;
  267. ; Rewrote it to eliminate the handle table as intermediary. ;
  268. ;-----------------------------------------------------------------------;
  269. cProc glrutop,<PUBLIC,NEAR>
  270. cBegin nogen
  271. push ebx
  272. push edx
  273. push esi
  274. call glruSetup
  275. jz short glrutop1
  276. call LRUDelete
  277. xor dx,dx ; DX == 0 means insert at head
  278. call LRUInsert
  279. glrutop1:
  280. pop esi
  281. pop edx
  282. pop ebx
  283. if KDEBUG
  284. call check_lru_list
  285. endif
  286. ret
  287. cEnd nogen
  288. ;-----------------------------------------------------------------------;
  289. ; glrubot ;
  290. ; ;
  291. ; Moves a discardable object to the tail of the LRU chain. ;
  292. ; ;
  293. ; Arguments: ;
  294. ; DS:DI = address of global heap info ;
  295. ; ES:DI = global arena of moveable object ;
  296. ; ;
  297. ; Returns: ;
  298. ; Updated LRU chain ;
  299. ; ;
  300. ; Error Returns: ;
  301. ; ;
  302. ; Registers Preserved: ;
  303. ; CX,DX,SI,DS,ES ;
  304. ; ;
  305. ; Registers Destroyed: ;
  306. ; ;
  307. ; Calls: ;
  308. ; ;
  309. ; History: ;
  310. ; ;
  311. ; Wed Feb 18, 1987 08:30:45p -by- David N. Weise [davidw] ;
  312. ; Added support for EMS. ;
  313. ; ;
  314. ; Mon Oct 27, 1986 04:20:10p -by- David N. Weise [davidw] ;
  315. ; Rewrote it to eliminate the handle table as intermediary. ;
  316. ;-----------------------------------------------------------------------;
  317. cProc glrubot,<PUBLIC,NEAR>
  318. cBegin nogen
  319. push bx
  320. push dx
  321. push si
  322. call glruSetup
  323. jz short glrubot1
  324. call LRUDelete
  325. mov dx,sp ; DX != 0 means insert at tail
  326. call LRUInsert
  327. glrubot1:
  328. pop si
  329. pop dx
  330. pop bx
  331. if KDEBUG
  332. call check_lru_list
  333. endif
  334. ret
  335. cEnd nogen
  336. ;-----------------------------------------------------------------------;
  337. ; glruadd ;
  338. ; ;
  339. ; Adds a discardable object to the head of the LRU chain. ;
  340. ; ;
  341. ; Arguments: ;
  342. ; DS:DI = address of global heap info ;
  343. ; ES:DI = arena header of object ;
  344. ; ;
  345. ; Returns: ;
  346. ; Updated LRU chain ;
  347. ; ;
  348. ; Error Returns: ;
  349. ; ;
  350. ; Registers Preserved: ;
  351. ; AX,BX,CX,DX,DI,SI,DS ;
  352. ; ;
  353. ; Registers Destroyed: ;
  354. ; none ;
  355. ; ;
  356. ; Calls: ;
  357. ; nothing ;
  358. ; ;
  359. ; History: ;
  360. ; ;
  361. ; Wed Feb 18, 1987 08:30:45p -by- David N. Weise [davidw] ;
  362. ; Added support for EMS. ;
  363. ; ;
  364. ; Mon Oct 27, 1986 04:23:35p -by- David N. Weise [davidw] ;
  365. ; Rewrote it to eliminate the handle table as intermediary. ;
  366. ;-----------------------------------------------------------------------;
  367. cProc glruadd,<PUBLIC,NEAR>
  368. cBegin nogen
  369. push bx
  370. push dx
  371. push si
  372. call glruSetup
  373. jz short glruadd1
  374. xor dx,dx ; DX == 0 means insert at head
  375. call LRUInsert
  376. glruadd1:
  377. pop si
  378. pop dx
  379. pop bx
  380. if KDEBUG
  381. call check_lru_list
  382. endif
  383. ret
  384. cEnd nogen
  385. ;-----------------------------------------------------------------------;
  386. ; glrudel ;
  387. ; ;
  388. ; Removes a discardable object from the LRU chain. ;
  389. ; ;
  390. ; Arguments: ;
  391. ; ES:DI = arena header of object ;
  392. ; DS:DI = address of global heap info ;
  393. ; ;
  394. ; Returns: ;
  395. ; Nothing. ;
  396. ; ;
  397. ; Error Returns: ;
  398. ; ;
  399. ; Registers Preserved: ;
  400. ; All ;
  401. ; ;
  402. ; Registers Destroyed: ;
  403. ; ;
  404. ; Calls: ;
  405. ; ;
  406. ; History: ;
  407. ; ;
  408. ; Wed Feb 18, 1987 08:30:45p -by- David N. Weise [davidw] ;
  409. ; Added support for EMS. ;
  410. ; ;
  411. ; Mon Oct 27, 1986 04:36:49p -by- David N. Weise [davidw] ;
  412. ; Rewrote it to eliminate the handle table as intermediary. ;
  413. ;-----------------------------------------------------------------------;
  414. cProc glrudel,<PUBLIC,NEAR>
  415. cBegin nogen
  416. push bx
  417. push dx
  418. push si
  419. call glruSetup
  420. jz short glrudel1
  421. call LRUDelete
  422. glrudel1:
  423. pop si
  424. pop dx
  425. pop bx
  426. if KDEBUG
  427. call check_lru_list
  428. endif
  429. ret
  430. cEnd nogen
  431. if KDEBUG
  432. ;-----------------------------------------------------------------------;
  433. ; check_lru_list ;
  434. ; ;
  435. ; Checks the glru list for consistency. ;
  436. ; ;
  437. ; Arguments: ;
  438. ; EDI 0 ;
  439. ; ;
  440. ; Returns: ;
  441. ; ;
  442. ; Error Returns: ;
  443. ; ;
  444. ; Registers Preserved: ;
  445. ; All ;
  446. ; ;
  447. ; Registers Destroyed: ;
  448. ; ;
  449. ; Calls: ;
  450. ; nothing ;
  451. ; ;
  452. ; History: ;
  453. ; ;
  454. ; Wed Feb 18, 1987 08:30:45p -by- David N. Weise [davidw] ;
  455. ; Added support for EMS. ;
  456. ; ;
  457. ; Wed Oct 29, 1986 10:13:42a -by- David N. Weise [davidw] ;
  458. ; Wrote it. ;
  459. ;-----------------------------------------------------------------------;
  460. cProc check_lru_list,<PUBLIC,NEAR>
  461. cBegin nogen
  462. push ds
  463. SetKernelDS
  464. test Kernel_flags,kf_check_free
  465. jz cll_ret
  466. push ax
  467. push ebx
  468. push cx
  469. push edx
  470. push esi
  471. mov ds,pGlobalHeap
  472. UnSetKernelDS
  473. do_it_again:
  474. mov ebx,[di].gi_lruchain
  475. mov cx,[di].gi_lrucount ; without ems gi_alt_count is 0
  476. or cx,cx
  477. jz all_done
  478. mov esi,ebx
  479. check_chain_loop:
  480. mov edx,ds:[esi].pga_lruprev
  481. mov esi,ds:[esi].pga_lrunext
  482. cmp ds:[edx].pga_lrunext,ebx
  483. jz short prev_okay
  484. kerror 0FFh,<lru: prev bad>,dx,bx
  485. prev_okay:
  486. cmp ds:[esi].pga_lruprev,ebx
  487. jz short next_okay
  488. kerror 0FFh,<lru: next bad>,bx,si
  489. next_okay:
  490. mov ebx,esi
  491. loop check_chain_loop
  492. cmp [di].gi_lruchain,ebx
  493. jz short all_done
  494. kerror 0FFh,<lru: count bad>,bx,[di].gi_lrucount
  495. all_done:
  496. pop esi
  497. pop edx
  498. pop cx
  499. pop ebx
  500. pop ax
  501. cll_ret:
  502. pop ds
  503. ret
  504. cEnd nogen
  505. endif
  506. sEnd CODE
  507. end