summaryrefslogtreecommitdiff
path: root/cache.inc
blob: 0d9d453fdbb68910b43093ddb73233e50da11187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
; -*- fundamental -*- ---------------------------------------------------
;
;   Copyright 2004-2007 H. Peter Anvin - All Rights Reserved
;
;   This program is free software; you can redistribute it and/or modify
;   it under the terms of the GNU General Public License as published by
;   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
;   Boston MA 02111-1307, USA; either version 2 of the License, or
;   (at your option) any later version; incorporated herein by reference.
;
; -----------------------------------------------------------------------

		section .text

		struc cptr
.sector:	resd 1				; Sector number
.prev:		resw 1				; LRU pointer to previous (less recent)
.next:		resw 1				; LRU pointer to next (more recent)
		endstruc
cptr_size_lg2	equ 3

NCacheEntries	equ 65536/SECTOR_SIZE

;
; initcache: Initialize the cache data structures
;
initcache:
		xor eax,eax			; We don't care about sector 0
		mov di,CachePtrs
		mov cx,NCacheEntries+1
		mov bx,CachePtrs+NCacheEntries*cptr_size	; "prev" pointer
.loop:
		mov [di+cptr.sector],eax	; Zero sector number
		mov [di+cptr.prev],bx		; Previous pointer
		mov [bx+cptr.next],di		; Previous entry's next pointer
		mov bx,di
		add di,cptr_size
		loop .loop
		ret

;
; getcachesector: Check for a particular sector (EAX) in the sector cache,
;		  and if it is already there, return a pointer in GS:SI
;		  otherwise load it and return said pointer.
;
;		Assumes CS == DS.
;
getcachesector:
		push cx
		push bx
		push di
		mov si,cache_seg
		mov gs,si
		mov si,CachePtrs+cptr_size	; Real sector cache pointers
		mov cx,NCacheEntries
.search:
		cmp eax,[si]
		jz .hit
		add si,cptr_size
		loop .search

.miss:
		TRACER 'M'
		; Need to load it.
		push es
		push gs
		pop es
		mov bx,[CachePtrs+cptr.next]	; "Next most recent than head node"
		mov [bx+cptr.sector],eax
		mov si,bx
		sub bx,CachePtrs+cptr_size
		shl bx,SECTOR_SHIFT-cptr_size_lg2	; Buffer address
		pushad
%if IS_EXTLINUX
		call getonesec_ext
%else
		call getonesec
%endif
		popad
		pop es
.hit:
		; Update LRU, then compute buffer address
		TRACER 'H'

		; Remove from current position in the list
		mov bx,[si+cptr.prev]
		mov di,[si+cptr.next]
		mov [bx+cptr.next],di
		mov [di+cptr.prev],bx

		; Add to just before head node
		mov bx,[CachePtrs+cptr.prev]
		mov [si+cptr.prev],bx
		mov [bx+cptr.next],si
		mov [CachePtrs+cptr.prev],si
		mov word [si+cptr.next],CachePtrs

		sub si,CachePtrs+cptr_size
		shl si,SECTOR_SHIFT-cptr_size_lg2	; Buffer address

		pop di
		pop bx
		pop cx
		ret

		section .latebss

		; Each CachePtr contains:
		; - Block pointer
		; - LRU previous pointer
		; - LRU next pointer
		; The first entry is the head node of the list
		alignb 4
CachePtrs	resb (NCacheEntries+1)*cptr_size