[6d7ffa65] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[6d7ffa65] | 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 |
|
---|
[f47fd19] | 29 | /** @addtogroup genarchmm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[0f27b4c] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[da1bafb] | 35 | * @brief Virtual Address Translation (VAT) for global page hash table.
|
---|
[0f27b4c] | 36 | */
|
---|
| 37 |
|
---|
[6d7ffa65] | 38 | #include <genarch/mm/page_ht.h>
|
---|
| 39 | #include <mm/page.h>
|
---|
[c7ec94a4] | 40 | #include <arch/mm/page.h>
|
---|
[6d7ffa65] | 41 | #include <mm/frame.h>
|
---|
[5e3757d] | 42 | #include <mm/slab.h>
|
---|
[fc1e4f6] | 43 | #include <mm/as.h>
|
---|
[677a6d5] | 44 | #include <arch/mm/asid.h>
|
---|
[d99c1d2] | 45 | #include <typedefs.h>
|
---|
[6d7ffa65] | 46 | #include <arch/asm.h>
|
---|
[05882233] | 47 | #include <barrier.h>
|
---|
[2a003d5b] | 48 | #include <synch/spinlock.h>
|
---|
| 49 | #include <arch.h>
|
---|
[63e27ef] | 50 | #include <assert.h>
|
---|
[82cbf8c6] | 51 | #include <adt/hash.h>
|
---|
[c7ec94a4] | 52 | #include <adt/hash_table.h>
|
---|
[a2a46ba] | 53 | #include <align.h>
|
---|
[c7ec94a4] | 54 |
|
---|
[82cbf8c6] | 55 | static size_t ht_hash(const ht_link_t *);
|
---|
| 56 | static size_t ht_key_hash(void *);
|
---|
| 57 | static bool ht_key_equal(void *, const ht_link_t *);
|
---|
| 58 | static void ht_remove_callback(ht_link_t *);
|
---|
[c7ec94a4] | 59 |
|
---|
[da1bafb] | 60 | static void ht_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
|
---|
| 61 | static void ht_mapping_remove(as_t *, uintptr_t);
|
---|
[38dc82d] | 62 | static bool ht_mapping_find(as_t *, uintptr_t, bool, pte_t *);
|
---|
[346b12a2] | 63 | static void ht_mapping_update(as_t *, uintptr_t, bool, pte_t *);
|
---|
[c868e2d] | 64 | static void ht_mapping_make_global(uintptr_t, size_t);
|
---|
[6d7ffa65] | 65 |
|
---|
[a55ddc64] | 66 | slab_cache_t *pte_cache = NULL;
|
---|
| 67 |
|
---|
[2a003d5b] | 68 | /**
|
---|
[2299914] | 69 | * This lock protects the page hash table. It must be acquired
|
---|
| 70 | * after address space lock and after any address space area
|
---|
| 71 | * locks.
|
---|
[da1bafb] | 72 | *
|
---|
[2a003d5b] | 73 | */
|
---|
[fb63c06] | 74 | IRQ_SPINLOCK_STATIC_INITIALIZE(page_ht_lock);
|
---|
[2a003d5b] | 75 |
|
---|
[da1bafb] | 76 | /** Page hash table.
|
---|
| 77 | *
|
---|
[2a003d5b] | 78 | * The page hash table may be accessed only when page_ht_lock is held.
|
---|
[da1bafb] | 79 | *
|
---|
[2a003d5b] | 80 | */
|
---|
[c7ec94a4] | 81 | hash_table_t page_ht;
|
---|
[2a003d5b] | 82 |
|
---|
[c7ec94a4] | 83 | /** Hash table operations for page hash table. */
|
---|
[82cbf8c6] | 84 | hash_table_ops_t ht_ops = {
|
---|
| 85 | .hash = ht_hash,
|
---|
| 86 | .key_hash = ht_key_hash,
|
---|
| 87 | .key_equal = ht_key_equal,
|
---|
| 88 | .remove_callback = ht_remove_callback
|
---|
[c7ec94a4] | 89 | };
|
---|
[6d7ffa65] | 90 |
|
---|
[f5935ed] | 91 | /** Page mapping operations for page hash table architectures. */
|
---|
| 92 | page_mapping_operations_t ht_mapping_operations = {
|
---|
[6d7ffa65] | 93 | .mapping_insert = ht_mapping_insert,
|
---|
[8f00329] | 94 | .mapping_remove = ht_mapping_remove,
|
---|
[c868e2d] | 95 | .mapping_find = ht_mapping_find,
|
---|
[346b12a2] | 96 | .mapping_update = ht_mapping_update,
|
---|
[c868e2d] | 97 | .mapping_make_global = ht_mapping_make_global
|
---|
[6d7ffa65] | 98 | };
|
---|
| 99 |
|
---|
[82cbf8c6] | 100 | /** Return the hash of the key stored in the item */
|
---|
| 101 | size_t ht_hash(const ht_link_t *item)
|
---|
[c7ec94a4] | 102 | {
|
---|
[82cbf8c6] | 103 | pte_t *pte = hash_table_get_inst(item, pte_t, link);
|
---|
| 104 | size_t hash = 0;
|
---|
| 105 | hash = hash_combine(hash, (uintptr_t) pte->as);
|
---|
| 106 | hash = hash_combine(hash, pte->page >> PAGE_WIDTH);
|
---|
| 107 | return hash;
|
---|
[c7ec94a4] | 108 | }
|
---|
| 109 |
|
---|
[82cbf8c6] | 110 | /** Return the hash of the key. */
|
---|
| 111 | size_t ht_key_hash(void *arg)
|
---|
[c7ec94a4] | 112 | {
|
---|
[82cbf8c6] | 113 | uintptr_t *key = (uintptr_t *) arg;
|
---|
| 114 | size_t hash = 0;
|
---|
| 115 | hash = hash_combine(hash, key[KEY_AS]);
|
---|
| 116 | hash = hash_combine(hash, key[KEY_PAGE] >> PAGE_WIDTH);
|
---|
| 117 | return hash;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /** Return true if the key is equal to the item's lookup key. */
|
---|
| 121 | bool ht_key_equal(void *arg, const ht_link_t *item)
|
---|
| 122 | {
|
---|
| 123 | uintptr_t *key = (uintptr_t *) arg;
|
---|
| 124 | pte_t *pte = hash_table_get_inst(item, pte_t, link);
|
---|
| 125 | return (key[KEY_AS] == (uintptr_t) pte->as) &&
|
---|
| 126 | (key[KEY_PAGE] == pte->page);
|
---|
[c7ec94a4] | 127 | }
|
---|
| 128 |
|
---|
| 129 | /** Callback on page hash table item removal.
|
---|
| 130 | *
|
---|
| 131 | * @param item Page hash table item being removed.
|
---|
[da1bafb] | 132 | *
|
---|
[c7ec94a4] | 133 | */
|
---|
[82cbf8c6] | 134 | void ht_remove_callback(ht_link_t *item)
|
---|
[c7ec94a4] | 135 | {
|
---|
[63e27ef] | 136 | assert(item);
|
---|
[a35b458] | 137 |
|
---|
[82cbf8c6] | 138 | pte_t *pte = hash_table_get_inst(item, pte_t, link);
|
---|
[a55ddc64] | 139 | slab_free(pte_cache, pte);
|
---|
[c7ec94a4] | 140 | }
|
---|
| 141 |
|
---|
[6d7ffa65] | 142 | /** Map page to frame using page hash table.
|
---|
| 143 | *
|
---|
[9179d0a] | 144 | * Map virtual address page to physical address frame
|
---|
[da1bafb] | 145 | * using flags.
|
---|
[6d7ffa65] | 146 | *
|
---|
[da1bafb] | 147 | * @param as Address space to which page belongs.
|
---|
| 148 | * @param page Virtual address of the page to be mapped.
|
---|
[6d7ffa65] | 149 | * @param frame Physical address of memory frame to which the mapping is done.
|
---|
| 150 | * @param flags Flags to be used for mapping.
|
---|
[da1bafb] | 151 | *
|
---|
[6d7ffa65] | 152 | */
|
---|
[da1bafb] | 153 | void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
|
---|
| 154 | unsigned int flags)
|
---|
[6d7ffa65] | 155 | {
|
---|
[82cbf8c6] | 156 | uintptr_t key[2] = {
|
---|
| 157 | [KEY_AS] = (uintptr_t) as,
|
---|
| 158 | [KEY_PAGE] = ALIGN_DOWN(page, PAGE_SIZE)
|
---|
[2057572] | 159 | };
|
---|
[1d432f9] | 160 |
|
---|
[63e27ef] | 161 | assert(page_table_locked(as));
|
---|
[fb63c06] | 162 |
|
---|
| 163 | irq_spinlock_lock(&page_ht_lock, true);
|
---|
[a35b458] | 164 |
|
---|
[c7ec94a4] | 165 | if (!hash_table_find(&page_ht, key)) {
|
---|
[a55ddc64] | 166 | pte_t *pte = slab_alloc(pte_cache, FRAME_LOWMEM | FRAME_ATOMIC);
|
---|
[63e27ef] | 167 | assert(pte != NULL);
|
---|
[a35b458] | 168 |
|
---|
[da1bafb] | 169 | pte->g = (flags & PAGE_GLOBAL) != 0;
|
---|
| 170 | pte->x = (flags & PAGE_EXEC) != 0;
|
---|
| 171 | pte->w = (flags & PAGE_WRITE) != 0;
|
---|
| 172 | pte->k = !(flags & PAGE_USER);
|
---|
| 173 | pte->c = (flags & PAGE_CACHEABLE) != 0;
|
---|
| 174 | pte->p = !(flags & PAGE_NOT_PRESENT);
|
---|
| 175 | pte->a = false;
|
---|
| 176 | pte->d = false;
|
---|
[a35b458] | 177 |
|
---|
[da1bafb] | 178 | pte->as = as;
|
---|
| 179 | pte->page = ALIGN_DOWN(page, PAGE_SIZE);
|
---|
| 180 | pte->frame = ALIGN_DOWN(frame, FRAME_SIZE);
|
---|
[7d68da80] | 181 |
|
---|
[de73242] | 182 | /*
|
---|
| 183 | * Make sure that a concurrent ht_mapping_find() will see the
|
---|
| 184 | * new entry only after it is fully initialized.
|
---|
| 185 | */
|
---|
[7d68da80] | 186 | write_barrier();
|
---|
[a35b458] | 187 |
|
---|
[82cbf8c6] | 188 | hash_table_insert(&page_ht, &pte->link);
|
---|
[0c0410b] | 189 | }
|
---|
[fb63c06] | 190 |
|
---|
| 191 | irq_spinlock_unlock(&page_ht_lock, true);
|
---|
[6d7ffa65] | 192 | }
|
---|
| 193 |
|
---|
[8f00329] | 194 | /** Remove mapping of page from page hash table.
|
---|
| 195 | *
|
---|
[9179d0a] | 196 | * Remove any mapping of page within address space as.
|
---|
[8f00329] | 197 | * TLB shootdown should follow in order to make effects of
|
---|
| 198 | * this call visible.
|
---|
| 199 | *
|
---|
[235e6c7] | 200 | * @param as Address space to which page belongs.
|
---|
[8f00329] | 201 | * @param page Virtual address of the page to be demapped.
|
---|
[da1bafb] | 202 | *
|
---|
[8f00329] | 203 | */
|
---|
[7f1c620] | 204 | void ht_mapping_remove(as_t *as, uintptr_t page)
|
---|
[8f00329] | 205 | {
|
---|
[82cbf8c6] | 206 | uintptr_t key[2] = {
|
---|
| 207 | [KEY_AS] = (uintptr_t) as,
|
---|
| 208 | [KEY_PAGE] = ALIGN_DOWN(page, PAGE_SIZE)
|
---|
[2057572] | 209 | };
|
---|
[1d432f9] | 210 |
|
---|
[63e27ef] | 211 | assert(page_table_locked(as));
|
---|
[a35b458] | 212 |
|
---|
[fb63c06] | 213 | irq_spinlock_lock(&page_ht_lock, true);
|
---|
| 214 |
|
---|
[8f00329] | 215 | /*
|
---|
| 216 | * Note that removed PTE's will be freed
|
---|
| 217 | * by remove_callback().
|
---|
| 218 | */
|
---|
[82cbf8c6] | 219 | hash_table_remove(&page_ht, key);
|
---|
[fb63c06] | 220 |
|
---|
| 221 | irq_spinlock_unlock(&page_ht_lock, true);
|
---|
[8f00329] | 222 | }
|
---|
| 223 |
|
---|
[1433ecda] | 224 | static pte_t *ht_mapping_find_internal(as_t *as, uintptr_t page, bool nolock)
|
---|
[346b12a2] | 225 | {
|
---|
[82cbf8c6] | 226 | uintptr_t key[2] = {
|
---|
| 227 | [KEY_AS] = (uintptr_t) as,
|
---|
| 228 | [KEY_PAGE] = ALIGN_DOWN(page, PAGE_SIZE)
|
---|
[346b12a2] | 229 | };
|
---|
| 230 |
|
---|
[63e27ef] | 231 | assert(nolock || page_table_locked(as));
|
---|
[fb63c06] | 232 |
|
---|
[82cbf8c6] | 233 | ht_link_t *cur = hash_table_find(&page_ht, key);
|
---|
[346b12a2] | 234 | if (cur)
|
---|
[82cbf8c6] | 235 | return hash_table_get_inst(cur, pte_t, link);
|
---|
[a35b458] | 236 |
|
---|
[346b12a2] | 237 | return NULL;
|
---|
| 238 | }
|
---|
[8f00329] | 239 |
|
---|
[6d7ffa65] | 240 | /** Find mapping for virtual page in page hash table.
|
---|
| 241 | *
|
---|
[38dc82d] | 242 | * @param as Address space to which page belongs.
|
---|
| 243 | * @param page Virtual page.
|
---|
| 244 | * @param nolock True if the page tables need not be locked.
|
---|
| 245 | * @param[out] pte Structure that will receive a copy of the found PTE.
|
---|
[da1bafb] | 246 | *
|
---|
[38dc82d] | 247 | * @return True if the mapping was found, false otherwise.
|
---|
[6d7ffa65] | 248 | */
|
---|
[38dc82d] | 249 | bool ht_mapping_find(as_t *as, uintptr_t page, bool nolock, pte_t *pte)
|
---|
[6d7ffa65] | 250 | {
|
---|
[fb63c06] | 251 | irq_spinlock_lock(&page_ht_lock, true);
|
---|
| 252 |
|
---|
[346b12a2] | 253 | pte_t *t = ht_mapping_find_internal(as, page, nolock);
|
---|
| 254 | if (t)
|
---|
| 255 | *pte = *t;
|
---|
[fb63c06] | 256 |
|
---|
| 257 | irq_spinlock_unlock(&page_ht_lock, true);
|
---|
[a35b458] | 258 |
|
---|
[346b12a2] | 259 | return t != NULL;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /** Update mapping for virtual page in page hash table.
|
---|
| 263 | *
|
---|
| 264 | * @param as Address space to which page belongs.
|
---|
| 265 | * @param page Virtual page.
|
---|
| 266 | * @param nolock True if the page tables need not be locked.
|
---|
| 267 | * @param pte New PTE.
|
---|
| 268 | */
|
---|
| 269 | void ht_mapping_update(as_t *as, uintptr_t page, bool nolock, pte_t *pte)
|
---|
| 270 | {
|
---|
[fb63c06] | 271 | irq_spinlock_lock(&page_ht_lock, true);
|
---|
| 272 |
|
---|
[346b12a2] | 273 | pte_t *t = ht_mapping_find_internal(as, page, nolock);
|
---|
| 274 | if (!t)
|
---|
| 275 | panic("Updating non-existent PTE");
|
---|
[a35b458] | 276 |
|
---|
[63e27ef] | 277 | assert(pte->as == t->as);
|
---|
| 278 | assert(pte->page == t->page);
|
---|
| 279 | assert(pte->frame == t->frame);
|
---|
| 280 | assert(pte->g == t->g);
|
---|
| 281 | assert(pte->x == t->x);
|
---|
| 282 | assert(pte->w == t->w);
|
---|
| 283 | assert(pte->k == t->k);
|
---|
| 284 | assert(pte->c == t->c);
|
---|
| 285 | assert(pte->p == t->p);
|
---|
[346b12a2] | 286 |
|
---|
| 287 | t->a = pte->a;
|
---|
| 288 | t->d = pte->d;
|
---|
[fb63c06] | 289 |
|
---|
| 290 | irq_spinlock_unlock(&page_ht_lock, true);
|
---|
[6d7ffa65] | 291 | }
|
---|
[b45c443] | 292 |
|
---|
[c868e2d] | 293 | void ht_mapping_make_global(uintptr_t base, size_t size)
|
---|
| 294 | {
|
---|
| 295 | /* nothing to do */
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[f47fd19] | 298 | /** @}
|
---|
[b45c443] | 299 | */
|
---|