source: mainline/kernel/genarch/src/mm/page_ht.c@ d776329b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d776329b was fb63c06, checked in by Jakub Jermar <jakub@…>, 9 years ago

Make page hash table critical sections smaller

After the change of the page mapping interface to work exclusively with
a copy of the actual PTE, the critical section around page hash table
look-ups, insertions and deletions can be much smaller.

This change necessitated the change of the page_ht_lock mutex into a
spinlock, because the page mapping API can be used from within TLB
shootdown sequence, which is basically a spinlock-protected critical
section and we cannot take a mutex while holding a spinlock.

  • Property mode set to 100644
File size: 8.5 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup genarchmm
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Virtual Address Translation (VAT) for global page hash table.
36 */
37
38#include <genarch/mm/page_ht.h>
39#include <mm/page.h>
40#include <arch/mm/page.h>
41#include <mm/frame.h>
42#include <mm/slab.h>
43#include <mm/as.h>
44#include <arch/mm/asid.h>
45#include <typedefs.h>
46#include <arch/asm.h>
47#include <arch/barrier.h>
48#include <synch/spinlock.h>
49#include <arch.h>
50#include <debug.h>
51#include <memstr.h>
52#include <adt/hash_table.h>
53#include <align.h>
54
55static size_t hash(sysarg_t[]);
56static bool compare(sysarg_t[], size_t, link_t *);
57static void remove_callback(link_t *);
58
59static void ht_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
60static void ht_mapping_remove(as_t *, uintptr_t);
61static bool ht_mapping_find(as_t *, uintptr_t, bool, pte_t *);
62static void ht_mapping_update(as_t *, uintptr_t, bool, pte_t *);
63static void ht_mapping_make_global(uintptr_t, size_t);
64
65slab_cache_t *pte_cache = NULL;
66
67/**
68 * This lock protects the page hash table. It must be acquired
69 * after address space lock and after any address space area
70 * locks.
71 *
72 */
73IRQ_SPINLOCK_STATIC_INITIALIZE(page_ht_lock);
74
75/** Page hash table.
76 *
77 * The page hash table may be accessed only when page_ht_lock is held.
78 *
79 */
80hash_table_t page_ht;
81
82/** Hash table operations for page hash table. */
83hash_table_operations_t ht_operations = {
84 .hash = hash,
85 .compare = compare,
86 .remove_callback = remove_callback
87};
88
89/** Page mapping operations for page hash table architectures. */
90page_mapping_operations_t ht_mapping_operations = {
91 .mapping_insert = ht_mapping_insert,
92 .mapping_remove = ht_mapping_remove,
93 .mapping_find = ht_mapping_find,
94 .mapping_update = ht_mapping_update,
95 .mapping_make_global = ht_mapping_make_global
96};
97
98/** Compute page hash table index.
99 *
100 * @param key Array of two keys (i.e. page and address space).
101 *
102 * @return Index into page hash table.
103 *
104 */
105size_t hash(sysarg_t key[])
106{
107 as_t *as = (as_t *) key[KEY_AS];
108 uintptr_t page = (uintptr_t) key[KEY_PAGE];
109
110 /*
111 * Virtual page addresses have roughly the same probability
112 * of occurring. Least significant bits of VPN compose the
113 * hash index.
114 *
115 */
116 size_t index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES - 1));
117
118 /*
119 * Address space structures are likely to be allocated from
120 * similar addresses. Least significant bits compose the
121 * hash index.
122 *
123 */
124 index |= ((sysarg_t) as) & (PAGE_HT_ENTRIES - 1);
125
126 return index;
127}
128
129/** Compare page hash table item with page and/or address space.
130 *
131 * @param key Array of one or two keys (i.e. page and/or address space).
132 * @param keys Number of keys passed.
133 * @param item Item to compare the keys with.
134 *
135 * @return true on match, false otherwise.
136 *
137 */
138bool compare(sysarg_t key[], size_t keys, link_t *item)
139{
140 ASSERT(item);
141 ASSERT(keys > 0);
142 ASSERT(keys <= PAGE_HT_KEYS);
143
144 /*
145 * Convert item to PTE.
146 *
147 */
148 pte_t *pte = hash_table_get_instance(item, pte_t, link);
149
150 if (keys == PAGE_HT_KEYS)
151 return (key[KEY_AS] == (uintptr_t) pte->as) &&
152 (key[KEY_PAGE] == pte->page);
153
154 return (key[KEY_AS] == (uintptr_t) pte->as);
155}
156
157/** Callback on page hash table item removal.
158 *
159 * @param item Page hash table item being removed.
160 *
161 */
162void remove_callback(link_t *item)
163{
164 ASSERT(item);
165
166 /*
167 * Convert item to PTE.
168 *
169 */
170 pte_t *pte = hash_table_get_instance(item, pte_t, link);
171
172 slab_free(pte_cache, pte);
173}
174
175/** Map page to frame using page hash table.
176 *
177 * Map virtual address page to physical address frame
178 * using flags.
179 *
180 * @param as Address space to which page belongs.
181 * @param page Virtual address of the page to be mapped.
182 * @param frame Physical address of memory frame to which the mapping is done.
183 * @param flags Flags to be used for mapping.
184 *
185 */
186void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
187 unsigned int flags)
188{
189 sysarg_t key[2] = {
190 (uintptr_t) as,
191 page = ALIGN_DOWN(page, PAGE_SIZE)
192 };
193
194 ASSERT(page_table_locked(as));
195
196 irq_spinlock_lock(&page_ht_lock, true);
197
198 if (!hash_table_find(&page_ht, key)) {
199 pte_t *pte = slab_alloc(pte_cache, FRAME_LOWMEM | FRAME_ATOMIC);
200 ASSERT(pte != NULL);
201
202 pte->g = (flags & PAGE_GLOBAL) != 0;
203 pte->x = (flags & PAGE_EXEC) != 0;
204 pte->w = (flags & PAGE_WRITE) != 0;
205 pte->k = !(flags & PAGE_USER);
206 pte->c = (flags & PAGE_CACHEABLE) != 0;
207 pte->p = !(flags & PAGE_NOT_PRESENT);
208 pte->a = false;
209 pte->d = false;
210
211 pte->as = as;
212 pte->page = ALIGN_DOWN(page, PAGE_SIZE);
213 pte->frame = ALIGN_DOWN(frame, FRAME_SIZE);
214
215 /*
216 * Make sure that a concurrent ht_mapping_find() will see the
217 * new entry only after it is fully initialized.
218 */
219 write_barrier();
220
221 hash_table_insert(&page_ht, key, &pte->link);
222 }
223
224 irq_spinlock_unlock(&page_ht_lock, true);
225}
226
227/** Remove mapping of page from page hash table.
228 *
229 * Remove any mapping of page within address space as.
230 * TLB shootdown should follow in order to make effects of
231 * this call visible.
232 *
233 * @param as Address space to which page belongs.
234 * @param page Virtual address of the page to be demapped.
235 *
236 */
237void ht_mapping_remove(as_t *as, uintptr_t page)
238{
239 sysarg_t key[2] = {
240 (uintptr_t) as,
241 page = ALIGN_DOWN(page, PAGE_SIZE)
242 };
243
244 ASSERT(page_table_locked(as));
245
246 irq_spinlock_lock(&page_ht_lock, true);
247
248 /*
249 * Note that removed PTE's will be freed
250 * by remove_callback().
251 */
252 hash_table_remove(&page_ht, key, 2);
253
254 irq_spinlock_unlock(&page_ht_lock, true);
255}
256
257static pte_t *ht_mapping_find_internal(as_t *as, uintptr_t page, bool nolock)
258{
259 sysarg_t key[2] = {
260 (uintptr_t) as,
261 page = ALIGN_DOWN(page, PAGE_SIZE)
262 };
263
264 ASSERT(nolock || page_table_locked(as));
265
266 link_t *cur = hash_table_find(&page_ht, key);
267 if (cur)
268 return hash_table_get_instance(cur, pte_t, link);
269
270 return NULL;
271}
272
273/** Find mapping for virtual page in page hash table.
274 *
275 * @param as Address space to which page belongs.
276 * @param page Virtual page.
277 * @param nolock True if the page tables need not be locked.
278 * @param[out] pte Structure that will receive a copy of the found PTE.
279 *
280 * @return True if the mapping was found, false otherwise.
281 */
282bool ht_mapping_find(as_t *as, uintptr_t page, bool nolock, pte_t *pte)
283{
284 irq_spinlock_lock(&page_ht_lock, true);
285
286 pte_t *t = ht_mapping_find_internal(as, page, nolock);
287 if (t)
288 *pte = *t;
289
290 irq_spinlock_unlock(&page_ht_lock, true);
291
292 return t != NULL;
293}
294
295/** Update mapping for virtual page in page hash table.
296 *
297 * @param as Address space to which page belongs.
298 * @param page Virtual page.
299 * @param nolock True if the page tables need not be locked.
300 * @param pte New PTE.
301 */
302void ht_mapping_update(as_t *as, uintptr_t page, bool nolock, pte_t *pte)
303{
304 irq_spinlock_lock(&page_ht_lock, true);
305
306 pte_t *t = ht_mapping_find_internal(as, page, nolock);
307 if (!t)
308 panic("Updating non-existent PTE");
309
310 ASSERT(pte->as == t->as);
311 ASSERT(pte->page == t->page);
312 ASSERT(pte->frame == t->frame);
313 ASSERT(pte->g == t->g);
314 ASSERT(pte->x == t->x);
315 ASSERT(pte->w == t->w);
316 ASSERT(pte->k == t->k);
317 ASSERT(pte->c == t->c);
318 ASSERT(pte->p == t->p);
319
320 t->a = pte->a;
321 t->d = pte->d;
322
323 irq_spinlock_unlock(&page_ht_lock, true);
324}
325
326void ht_mapping_make_global(uintptr_t base, size_t size)
327{
328 /* nothing to do */
329}
330
331/** @}
332 */
Note: See TracBrowser for help on using the repository browser.