2
* Copyright (c) 2006 Jakub Jermar
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
/** @addtogroup genarchmm
35
* @brief Virtual Address Translation (VAT) for global page hash table.
38
#include <genarch/mm/page_ht.h>
40
#include <arch/mm/page.h>
44
#include <arch/mm/asid.h>
45
#include <arch/types.h>
47
#include <synch/spinlock.h>
51
#include <adt/hash_table.h>
54
static size_t hash(unative_t key[]);
55
static bool compare(unative_t key[], size_t keys, link_t *item);
56
static void remove_callback(link_t *item);
58
static void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
60
static void ht_mapping_remove(as_t *as, uintptr_t page);
61
static pte_t *ht_mapping_find(as_t *as, uintptr_t page);
64
* This lock protects the page hash table. It must be acquired
65
* after address space lock and after any address space area
72
* The page hash table may be accessed only when page_ht_lock is held.
76
/** Hash table operations for page hash table. */
77
hash_table_operations_t ht_operations = {
80
.remove_callback = remove_callback
83
/** Page mapping operations for page hash table architectures. */
84
page_mapping_operations_t ht_mapping_operations = {
85
.mapping_insert = ht_mapping_insert,
86
.mapping_remove = ht_mapping_remove,
87
.mapping_find = ht_mapping_find
90
/** Compute page hash table index.
92
* @param key Array of two keys (i.e. page and address space).
94
* @return Index into page hash table.
96
size_t hash(unative_t key[])
98
as_t *as = (as_t *) key[KEY_AS];
99
uintptr_t page = (uintptr_t) key[KEY_PAGE];
103
* Virtual page addresses have roughly the same probability
104
* of occurring. Least significant bits of VPN compose the
107
index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES - 1));
110
* Address space structures are likely to be allocated from
111
* similar addresses. Least significant bits compose the
114
index |= ((unative_t) as) & (PAGE_HT_ENTRIES - 1);
119
/** Compare page hash table item with page and/or address space.
121
* @param key Array of one or two keys (i.e. page and/or address space).
122
* @param keys Number of keys passed.
123
* @param item Item to compare the keys with.
125
* @return true on match, false otherwise.
127
bool compare(unative_t key[], size_t keys, link_t *item)
132
ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS));
135
* Convert item to PTE.
137
t = hash_table_get_instance(item, pte_t, link);
139
if (keys == PAGE_HT_KEYS) {
140
return (key[KEY_AS] == (uintptr_t) t->as) &&
141
(key[KEY_PAGE] == t->page);
143
return (key[KEY_AS] == (uintptr_t) t->as);
147
/** Callback on page hash table item removal.
149
* @param item Page hash table item being removed.
151
void remove_callback(link_t *item)
158
* Convert item to PTE.
160
t = hash_table_get_instance(item, pte_t, link);
165
/** Map page to frame using page hash table.
167
* Map virtual address page to physical address frame
170
* The page table must be locked and interrupts must be disabled.
172
* @param as Address space to which page belongs.
173
* @param page Virtual address of the page to be mapped.
174
* @param frame Physical address of memory frame to which the mapping is done.
175
* @param flags Flags to be used for mapping.
177
void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags)
182
page = ALIGN_DOWN(page, PAGE_SIZE)
185
if (!hash_table_find(&page_ht, key)) {
186
t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
189
t->g = (flags & PAGE_GLOBAL) != 0;
190
t->x = (flags & PAGE_EXEC) != 0;
191
t->w = (flags & PAGE_WRITE) != 0;
192
t->k = !(flags & PAGE_USER);
193
t->c = (flags & PAGE_CACHEABLE) != 0;
194
t->p = !(flags & PAGE_NOT_PRESENT);
199
t->page = ALIGN_DOWN(page, PAGE_SIZE);
200
t->frame = ALIGN_DOWN(frame, FRAME_SIZE);
202
hash_table_insert(&page_ht, key, &t->link);
206
/** Remove mapping of page from page hash table.
208
* Remove any mapping of page within address space as.
209
* TLB shootdown should follow in order to make effects of
212
* The page table must be locked and interrupts must be disabled.
214
* @param as Address space to wich page belongs.
215
* @param page Virtual address of the page to be demapped.
217
void ht_mapping_remove(as_t *as, uintptr_t page)
221
page = ALIGN_DOWN(page, PAGE_SIZE)
225
* Note that removed PTE's will be freed
226
* by remove_callback().
228
hash_table_remove(&page_ht, key, 2);
232
/** Find mapping for virtual page in page hash table.
234
* Find mapping for virtual page.
236
* The page table must be locked and interrupts must be disabled.
238
* @param as Address space to wich page belongs.
239
* @param page Virtual page.
241
* @return NULL if there is no such mapping; requested mapping otherwise.
243
pte_t *ht_mapping_find(as_t *as, uintptr_t page)
249
page = ALIGN_DOWN(page, PAGE_SIZE)
252
hlp = hash_table_find(&page_ht, key);
254
t = hash_table_get_instance(hlp, pte_t, link);