Rev 757 | Rev 793 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 757 | Rev 792 | ||
|---|---|---|---|
| Line 26... | Line 26... | ||
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
27 | */ |
| 28 | 28 | ||
| 29 | #include <genarch/mm/page_ht.h> |
29 | #include <genarch/mm/page_ht.h> |
| 30 | #include <mm/page.h> |
30 | #include <mm/page.h> |
| - | 31 | #include <arch/mm/page.h> |
|
| 31 | #include <mm/frame.h> |
32 | #include <mm/frame.h> |
| 32 | #include <mm/heap.h> |
33 | #include <mm/heap.h> |
| 33 | #include <mm/as.h> |
34 | #include <mm/as.h> |
| 34 | #include <arch/mm/asid.h> |
35 | #include <arch/mm/asid.h> |
| 35 | #include <arch/types.h> |
36 | #include <arch/types.h> |
| Line 37... | Line 38... | ||
| 37 | #include <arch/asm.h> |
38 | #include <arch/asm.h> |
| 38 | #include <synch/spinlock.h> |
39 | #include <synch/spinlock.h> |
| 39 | #include <arch.h> |
40 | #include <arch.h> |
| 40 | #include <debug.h> |
41 | #include <debug.h> |
| 41 | #include <memstr.h> |
42 | #include <memstr.h> |
| - | 43 | #include <adt/hash_table.h> |
|
| - | 44 | ||
| - | 45 | static index_t hash(__native key[]); |
|
| - | 46 | static bool compare(__native key[], count_t keys, link_t *item); |
|
| - | 47 | static void remove_callback(link_t *item); |
|
| - | 48 | ||
| - | 49 | static void ht_mapping_insert(as_t *as, __address page, __address frame, int flags); |
|
| - | 50 | static pte_t *ht_mapping_find(as_t *as, __address page); |
|
| 42 | 51 | ||
| 43 | /** |
52 | /** |
| 44 | * This lock protects the page hash table. |
53 | * This lock protects the page hash table. |
| 45 | */ |
54 | */ |
| 46 | SPINLOCK_INITIALIZE(page_ht_lock); |
55 | SPINLOCK_INITIALIZE(page_ht_lock); |
| 47 | 56 | ||
| 48 | /** |
57 | /** |
| 49 | * Page hash table pointer. |
58 | * Page hash table. |
| 50 | * The page hash table may be accessed only when page_ht_lock is held. |
59 | * The page hash table may be accessed only when page_ht_lock is held. |
| 51 | */ |
60 | */ |
| 52 | pte_t *page_ht = NULL; |
61 | hash_table_t page_ht; |
| 53 | 62 | ||
| 54 | static void ht_mapping_insert(as_t *as, __address page, __address frame, int flags); |
63 | /** Hash table operations for page hash table. */ |
| 55 | static pte_t *ht_mapping_find(as_t *as, __address page); |
64 | hash_table_operations_t ht_operations = { |
| - | 65 | .hash = hash, |
|
| - | 66 | .compare = compare, |
|
| - | 67 | .remove_callback = remove_callback |
|
| - | 68 | }; |
|
| 56 | 69 | ||
| 57 | page_operations_t page_ht_operations = { |
70 | page_operations_t page_ht_operations = { |
| 58 | .mapping_insert = ht_mapping_insert, |
71 | .mapping_insert = ht_mapping_insert, |
| 59 | .mapping_find = ht_mapping_find |
72 | .mapping_find = ht_mapping_find |
| 60 | }; |
73 | }; |
| 61 | 74 | ||
| - | 75 | /** Compute page hash table index. |
|
| - | 76 | * |
|
| - | 77 | * @param key Array of two keys (i.e. page and address space). |
|
| - | 78 | * |
|
| - | 79 | * @return Index into page hash table. |
|
| - | 80 | */ |
|
| - | 81 | index_t hash(__native key[]) |
|
| - | 82 | { |
|
| - | 83 | as_t *as = (as_t *) key[KEY_AS]; |
|
| - | 84 | __address page = (__address) key[KEY_PAGE]; |
|
| - | 85 | index_t index; |
|
| - | 86 | ||
| - | 87 | /* |
|
| - | 88 | * Virtual page addresses have roughly the same probability |
|
| - | 89 | * of occurring. Least significant bits of VPN compose the |
|
| - | 90 | * hash index. |
|
| - | 91 | */ |
|
| - | 92 | index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES-1)); |
|
| - | 93 | ||
| - | 94 | /* |
|
| - | 95 | * Address space structures are likely to be allocated from |
|
| - | 96 | * similar addresses. Least significant bits compose the |
|
| - | 97 | * hash index. |
|
| - | 98 | */ |
|
| - | 99 | index |= ((__native) as) & (PAGE_HT_ENTRIES-1); |
|
| - | 100 | ||
| - | 101 | return index; |
|
| - | 102 | } |
|
| - | 103 | ||
| - | 104 | /** Compare page hash table item with page and/or address space. |
|
| - | 105 | * |
|
| - | 106 | * @param key Array of one or two keys (i.e. page and/or address space). |
|
| - | 107 | * @param keys Number of keys passed. |
|
| - | 108 | * @param item Item to compare the keys with. |
|
| - | 109 | * |
|
| - | 110 | * @return true on match, false otherwise. |
|
| - | 111 | */ |
|
| - | 112 | bool compare(__native key[], count_t keys, link_t *item) |
|
| - | 113 | { |
|
| - | 114 | pte_t *t; |
|
| - | 115 | ||
| - | 116 | ASSERT(item); |
|
| - | 117 | ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS)); |
|
| - | 118 | ||
| - | 119 | /* |
|
| - | 120 | * Convert item to PTE. |
|
| - | 121 | */ |
|
| - | 122 | t = list_get_instance(item, pte_t, link); |
|
| - | 123 | ||
| - | 124 | if (keys == PAGE_HT_KEYS) { |
|
| - | 125 | return (key[KEY_AS] == (__address) t->as) && (key[KEY_PAGE] == t->page); |
|
| - | 126 | } else { |
|
| - | 127 | return (key[KEY_AS] == (__address) t->as); |
|
| - | 128 | } |
|
| - | 129 | } |
|
| - | 130 | ||
| - | 131 | /** Callback on page hash table item removal. |
|
| - | 132 | * |
|
| - | 133 | * @param item Page hash table item being removed. |
|
| - | 134 | */ |
|
| - | 135 | void remove_callback(link_t *item) |
|
| - | 136 | { |
|
| - | 137 | pte_t *t; |
|
| - | 138 | ||
| - | 139 | ASSERT(item); |
|
| - | 140 | ||
| - | 141 | /* |
|
| - | 142 | * Convert item to PTE. |
|
| - | 143 | */ |
|
| - | 144 | t = list_get_instance(item, pte_t, link); |
|
| - | 145 | ||
| - | 146 | free(t); |
|
| - | 147 | } |
|
| - | 148 | ||
| 62 | /** Map page to frame using page hash table. |
149 | /** Map page to frame using page hash table. |
| 63 | * |
150 | * |
| 64 | * Map virtual address 'page' to physical address 'frame' |
151 | * Map virtual address 'page' to physical address 'frame' |
| 65 | * using 'flags'. |
152 | * using 'flags'. |
| 66 | * |
153 | * |
| Line 71... | Line 158... | ||
| 71 | * @param frame Physical address of memory frame to which the mapping is done. |
158 | * @param frame Physical address of memory frame to which the mapping is done. |
| 72 | * @param flags Flags to be used for mapping. |
159 | * @param flags Flags to be used for mapping. |
| 73 | */ |
160 | */ |
| 74 | void ht_mapping_insert(as_t *as, __address page, __address frame, int flags) |
161 | void ht_mapping_insert(as_t *as, __address page, __address frame, int flags) |
| 75 | { |
162 | { |
| 76 | pte_t *t, *u; |
163 | pte_t *t; |
| 77 | ipl_t ipl; |
164 | ipl_t ipl; |
| - | 165 | __native key[2] = { (__address) as, page }; |
|
| 78 | 166 | ||
| 79 | ipl = interrupts_disable(); |
167 | ipl = interrupts_disable(); |
| 80 | spinlock_lock(&page_ht_lock); |
168 | spinlock_lock(&page_ht_lock); |
| 81 | 169 | ||
| - | 170 | if (!hash_table_find(&page_ht, key)) { |
|
| 82 | t = HT_HASH(page, as->asid); |
171 | t = (pte_t *) malloc(sizeof(pte_t)); |
| 83 | if (!HT_SLOT_EMPTY(t)) { |
172 | ASSERT(t != NULL); |
| 84 | 173 | ||
| 85 | /* |
- | |
| 86 | * The slot is occupied. |
- | |
| 87 | * Walk through the collision chain and append the mapping to its end. |
- | |
| 88 | */ |
- | |
| 89 | - | ||
| 90 | do { |
- | |
| 91 | u = t; |
- | |
| 92 | if (HT_COMPARE(page, as->asid, t)) { |
- | |
| 93 | /* |
- | |
| 94 | * Nothing to do, |
- | |
| 95 | * the record is already there. |
- | |
| 96 | */ |
- | |
| 97 | spinlock_unlock(&page_ht_lock); |
174 | hash_table_insert(&page_ht, key, &t->link); |
| 98 | interrupts_restore(ipl); |
- | |
| 99 | return; |
- | |
| 100 | } |
- | |
| 101 | } while ((t = HT_GET_NEXT(t))); |
- | |
| 102 | - | ||
| 103 | t = (pte_t *) malloc(sizeof(pte_t)); /* FIXME: use slab allocator for this */ |
- | |
| 104 | if (!t) |
- | |
| 105 | panic("could not allocate memory\n"); |
- | |
| 106 | - | ||
| 107 | HT_SET_NEXT(u, t); |
- | |
| 108 | } |
175 | } |
| 109 | 176 | ||
| 110 | HT_SET_RECORD(t, page, as->asid, frame, flags); |
- | |
| 111 | HT_SET_NEXT(t, NULL); |
- | |
| 112 | - | ||
| 113 | spinlock_unlock(&page_ht_lock); |
177 | spinlock_unlock(&page_ht_lock); |
| 114 | interrupts_restore(ipl); |
178 | interrupts_restore(ipl); |
| 115 | } |
179 | } |
| 116 | 180 | ||
| 117 | /** Find mapping for virtual page in page hash table. |
181 | /** Find mapping for virtual page in page hash table. |
| Line 125... | Line 189... | ||
| 125 | * |
189 | * |
| 126 | * @return NULL if there is no such mapping; requested mapping otherwise. |
190 | * @return NULL if there is no such mapping; requested mapping otherwise. |
| 127 | */ |
191 | */ |
| 128 | pte_t *ht_mapping_find(as_t *as, __address page) |
192 | pte_t *ht_mapping_find(as_t *as, __address page) |
| 129 | { |
193 | { |
| - | 194 | link_t *hlp; |
|
| 130 | pte_t *t; |
195 | pte_t *t = NULL; |
| - | 196 | __native key[2] = { (__address) as, page }; |
|
| 131 | 197 | ||
| 132 | spinlock_lock(&page_ht_lock); |
198 | spinlock_lock(&page_ht_lock); |
| 133 | t = HT_HASH(page, as->asid); |
- | |
| 134 | if (!HT_SLOT_EMPTY(t)) { |
- | |
| 135 | while (!HT_COMPARE(page, as->asid, t) && HT_GET_NEXT(t)) |
- | |
| 136 | t = HT_GET_NEXT(t); |
- | |
| 137 | t = HT_COMPARE(page, as->asid, t) ? t : NULL; |
- | |
| 138 | } else { |
- | |
| 139 | t = NULL; |
- | |
| 140 | } |
- | |
| 141 | spinlock_unlock(&page_ht_lock); |
- | |
| 142 | return t; |
- | |
| 143 | } |
- | |
| 144 | 199 | ||
| 145 | /** Invalidate page hash table. |
- | |
| 146 | * |
- | |
| 147 | * Interrupts must be disabled. |
- | |
| 148 | */ |
- | |
| 149 | void ht_invalidate_all(void) |
- | |
| 150 | { |
- | |
| 151 | pte_t *t, *u; |
- | |
| 152 | int i; |
- | |
| 153 | - | ||
| 154 | spinlock_lock(&page_ht_lock); |
- | |
| 155 | for (i = 0; i < HT_ENTRIES; i++) { |
- | |
| 156 | if (!HT_SLOT_EMPTY(&page_ht[i])) { |
- | |
| 157 | t = HT_GET_NEXT(&page_ht[i]); |
200 | hlp = hash_table_find(&page_ht, key); |
| 158 | while (t) { |
201 | if (hlp) |
| 159 | u = t; |
- | |
| 160 | t = HT_GET_NEXT(t); |
- | |
| 161 | free(u); /* FIXME: use slab allocator for this */ |
- | |
| 162 | } |
- | |
| 163 | HT_INVALIDATE_SLOT(&page_ht[i]); |
202 | t = list_get_instance(hlp, pte_t, link); |
| 164 | } |
- | |
| 165 | } |
203 | |
| 166 | spinlock_unlock(&page_ht_lock); |
204 | spinlock_unlock(&page_ht_lock); |
| - | 205 | return t; |
|
| 167 | } |
206 | } |