[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>
|
---|
[2a003d5b] | 47 | #include <synch/spinlock.h>
|
---|
| 48 | #include <arch.h>
|
---|
[0c0410b] | 49 | #include <debug.h>
|
---|
[ef67bab] | 50 | #include <memstr.h>
|
---|
[c7ec94a4] | 51 | #include <adt/hash_table.h>
|
---|
[a2a46ba] | 52 | #include <align.h>
|
---|
[c7ec94a4] | 53 |
|
---|
[96b02eb9] | 54 | static size_t hash(sysarg_t[]);
|
---|
| 55 | static bool compare(sysarg_t[], size_t, link_t *);
|
---|
[da1bafb] | 56 | static void remove_callback(link_t *);
|
---|
[c7ec94a4] | 57 |
|
---|
[da1bafb] | 58 | static void ht_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
|
---|
| 59 | static void ht_mapping_remove(as_t *, uintptr_t);
|
---|
[235e6c7] | 60 | static pte_t *ht_mapping_find(as_t *, uintptr_t, bool);
|
---|
[c868e2d] | 61 | static void ht_mapping_make_global(uintptr_t, size_t);
|
---|
[6d7ffa65] | 62 |
|
---|
[a55ddc64] | 63 | slab_cache_t *pte_cache = NULL;
|
---|
| 64 |
|
---|
[2a003d5b] | 65 | /**
|
---|
[2299914] | 66 | * This lock protects the page hash table. It must be acquired
|
---|
| 67 | * after address space lock and after any address space area
|
---|
| 68 | * locks.
|
---|
[da1bafb] | 69 | *
|
---|
[2a003d5b] | 70 | */
|
---|
[1068f6a] | 71 | mutex_t page_ht_lock;
|
---|
[2a003d5b] | 72 |
|
---|
[da1bafb] | 73 | /** Page hash table.
|
---|
| 74 | *
|
---|
[2a003d5b] | 75 | * The page hash table may be accessed only when page_ht_lock is held.
|
---|
[da1bafb] | 76 | *
|
---|
[2a003d5b] | 77 | */
|
---|
[c7ec94a4] | 78 | hash_table_t page_ht;
|
---|
[2a003d5b] | 79 |
|
---|
[c7ec94a4] | 80 | /** Hash table operations for page hash table. */
|
---|
| 81 | hash_table_operations_t ht_operations = {
|
---|
| 82 | .hash = hash,
|
---|
| 83 | .compare = compare,
|
---|
| 84 | .remove_callback = remove_callback
|
---|
| 85 | };
|
---|
[6d7ffa65] | 86 |
|
---|
[f5935ed] | 87 | /** Page mapping operations for page hash table architectures. */
|
---|
| 88 | page_mapping_operations_t ht_mapping_operations = {
|
---|
[6d7ffa65] | 89 | .mapping_insert = ht_mapping_insert,
|
---|
[8f00329] | 90 | .mapping_remove = ht_mapping_remove,
|
---|
[c868e2d] | 91 | .mapping_find = ht_mapping_find,
|
---|
| 92 | .mapping_make_global = ht_mapping_make_global
|
---|
[6d7ffa65] | 93 | };
|
---|
| 94 |
|
---|
[c7ec94a4] | 95 | /** Compute page hash table index.
|
---|
| 96 | *
|
---|
| 97 | * @param key Array of two keys (i.e. page and address space).
|
---|
| 98 | *
|
---|
| 99 | * @return Index into page hash table.
|
---|
[da1bafb] | 100 | *
|
---|
[c7ec94a4] | 101 | */
|
---|
[96b02eb9] | 102 | size_t hash(sysarg_t key[])
|
---|
[c7ec94a4] | 103 | {
|
---|
| 104 | as_t *as = (as_t *) key[KEY_AS];
|
---|
[7f1c620] | 105 | uintptr_t page = (uintptr_t) key[KEY_PAGE];
|
---|
[c7ec94a4] | 106 |
|
---|
| 107 | /*
|
---|
| 108 | * Virtual page addresses have roughly the same probability
|
---|
| 109 | * of occurring. Least significant bits of VPN compose the
|
---|
| 110 | * hash index.
|
---|
[da1bafb] | 111 | *
|
---|
[c7ec94a4] | 112 | */
|
---|
[da1bafb] | 113 | size_t index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES - 1));
|
---|
[c7ec94a4] | 114 |
|
---|
| 115 | /*
|
---|
| 116 | * Address space structures are likely to be allocated from
|
---|
| 117 | * similar addresses. Least significant bits compose the
|
---|
| 118 | * hash index.
|
---|
[da1bafb] | 119 | *
|
---|
[c7ec94a4] | 120 | */
|
---|
[96b02eb9] | 121 | index |= ((sysarg_t) as) & (PAGE_HT_ENTRIES - 1);
|
---|
[c7ec94a4] | 122 |
|
---|
| 123 | return index;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /** Compare page hash table item with page and/or address space.
|
---|
| 127 | *
|
---|
[da1bafb] | 128 | * @param key Array of one or two keys (i.e. page and/or address space).
|
---|
[c7ec94a4] | 129 | * @param keys Number of keys passed.
|
---|
| 130 | * @param item Item to compare the keys with.
|
---|
| 131 | *
|
---|
| 132 | * @return true on match, false otherwise.
|
---|
[da1bafb] | 133 | *
|
---|
[c7ec94a4] | 134 | */
|
---|
[96b02eb9] | 135 | bool compare(sysarg_t key[], size_t keys, link_t *item)
|
---|
[c7ec94a4] | 136 | {
|
---|
| 137 | ASSERT(item);
|
---|
[da1bafb] | 138 | ASSERT(keys > 0);
|
---|
| 139 | ASSERT(keys <= PAGE_HT_KEYS);
|
---|
| 140 |
|
---|
[c7ec94a4] | 141 | /*
|
---|
| 142 | * Convert item to PTE.
|
---|
[da1bafb] | 143 | *
|
---|
[c7ec94a4] | 144 | */
|
---|
[da1bafb] | 145 | pte_t *pte = hash_table_get_instance(item, pte_t, link);
|
---|
| 146 |
|
---|
| 147 | if (keys == PAGE_HT_KEYS)
|
---|
| 148 | return (key[KEY_AS] == (uintptr_t) pte->as) &&
|
---|
| 149 | (key[KEY_PAGE] == pte->page);
|
---|
| 150 |
|
---|
| 151 | return (key[KEY_AS] == (uintptr_t) pte->as);
|
---|
[c7ec94a4] | 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Callback on page hash table item removal.
|
---|
| 155 | *
|
---|
| 156 | * @param item Page hash table item being removed.
|
---|
[da1bafb] | 157 | *
|
---|
[c7ec94a4] | 158 | */
|
---|
| 159 | void remove_callback(link_t *item)
|
---|
| 160 | {
|
---|
| 161 | ASSERT(item);
|
---|
[da1bafb] | 162 |
|
---|
[c7ec94a4] | 163 | /*
|
---|
| 164 | * Convert item to PTE.
|
---|
[da1bafb] | 165 | *
|
---|
[c7ec94a4] | 166 | */
|
---|
[da1bafb] | 167 | pte_t *pte = hash_table_get_instance(item, pte_t, link);
|
---|
| 168 |
|
---|
[a55ddc64] | 169 | slab_free(pte_cache, pte);
|
---|
[c7ec94a4] | 170 | }
|
---|
| 171 |
|
---|
[6d7ffa65] | 172 | /** Map page to frame using page hash table.
|
---|
| 173 | *
|
---|
[9179d0a] | 174 | * Map virtual address page to physical address frame
|
---|
[da1bafb] | 175 | * using flags.
|
---|
[6d7ffa65] | 176 | *
|
---|
[da1bafb] | 177 | * @param as Address space to which page belongs.
|
---|
| 178 | * @param page Virtual address of the page to be mapped.
|
---|
[6d7ffa65] | 179 | * @param frame Physical address of memory frame to which the mapping is done.
|
---|
| 180 | * @param flags Flags to be used for mapping.
|
---|
[da1bafb] | 181 | *
|
---|
[6d7ffa65] | 182 | */
|
---|
[da1bafb] | 183 | void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
|
---|
| 184 | unsigned int flags)
|
---|
[6d7ffa65] | 185 | {
|
---|
[96b02eb9] | 186 | sysarg_t key[2] = {
|
---|
[2057572] | 187 | (uintptr_t) as,
|
---|
| 188 | page = ALIGN_DOWN(page, PAGE_SIZE)
|
---|
| 189 | };
|
---|
[1d432f9] | 190 |
|
---|
| 191 | ASSERT(page_table_locked(as));
|
---|
[2a003d5b] | 192 |
|
---|
[c7ec94a4] | 193 | if (!hash_table_find(&page_ht, key)) {
|
---|
[a55ddc64] | 194 | pte_t *pte = slab_alloc(pte_cache, FRAME_LOWMEM | FRAME_ATOMIC);
|
---|
[da1bafb] | 195 | ASSERT(pte != NULL);
|
---|
| 196 |
|
---|
| 197 | pte->g = (flags & PAGE_GLOBAL) != 0;
|
---|
| 198 | pte->x = (flags & PAGE_EXEC) != 0;
|
---|
| 199 | pte->w = (flags & PAGE_WRITE) != 0;
|
---|
| 200 | pte->k = !(flags & PAGE_USER);
|
---|
| 201 | pte->c = (flags & PAGE_CACHEABLE) != 0;
|
---|
| 202 | pte->p = !(flags & PAGE_NOT_PRESENT);
|
---|
| 203 | pte->a = false;
|
---|
| 204 | pte->d = false;
|
---|
| 205 |
|
---|
| 206 | pte->as = as;
|
---|
| 207 | pte->page = ALIGN_DOWN(page, PAGE_SIZE);
|
---|
| 208 | pte->frame = ALIGN_DOWN(frame, FRAME_SIZE);
|
---|
| 209 |
|
---|
| 210 | hash_table_insert(&page_ht, key, &pte->link);
|
---|
[0c0410b] | 211 | }
|
---|
[6d7ffa65] | 212 | }
|
---|
| 213 |
|
---|
[8f00329] | 214 | /** Remove mapping of page from page hash table.
|
---|
| 215 | *
|
---|
[9179d0a] | 216 | * Remove any mapping of page within address space as.
|
---|
[8f00329] | 217 | * TLB shootdown should follow in order to make effects of
|
---|
| 218 | * this call visible.
|
---|
| 219 | *
|
---|
[235e6c7] | 220 | * @param as Address space to which page belongs.
|
---|
[8f00329] | 221 | * @param page Virtual address of the page to be demapped.
|
---|
[da1bafb] | 222 | *
|
---|
[8f00329] | 223 | */
|
---|
[7f1c620] | 224 | void ht_mapping_remove(as_t *as, uintptr_t page)
|
---|
[8f00329] | 225 | {
|
---|
[96b02eb9] | 226 | sysarg_t key[2] = {
|
---|
[2057572] | 227 | (uintptr_t) as,
|
---|
| 228 | page = ALIGN_DOWN(page, PAGE_SIZE)
|
---|
| 229 | };
|
---|
[1d432f9] | 230 |
|
---|
| 231 | ASSERT(page_table_locked(as));
|
---|
[8f00329] | 232 |
|
---|
| 233 | /*
|
---|
| 234 | * Note that removed PTE's will be freed
|
---|
| 235 | * by remove_callback().
|
---|
| 236 | */
|
---|
| 237 | hash_table_remove(&page_ht, key, 2);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
[6d7ffa65] | 241 | /** Find mapping for virtual page in page hash table.
|
---|
| 242 | *
|
---|
[235e6c7] | 243 | * @param as Address space to which page belongs.
|
---|
| 244 | * @param page Virtual page.
|
---|
| 245 | * @param nolock True if the page tables need not be locked.
|
---|
[6d7ffa65] | 246 | *
|
---|
[0c0410b] | 247 | * @return NULL if there is no such mapping; requested mapping otherwise.
|
---|
[da1bafb] | 248 | *
|
---|
[6d7ffa65] | 249 | */
|
---|
[235e6c7] | 250 | pte_t *ht_mapping_find(as_t *as, uintptr_t page, bool nolock)
|
---|
[6d7ffa65] | 251 | {
|
---|
[96b02eb9] | 252 | sysarg_t key[2] = {
|
---|
[2057572] | 253 | (uintptr_t) as,
|
---|
| 254 | page = ALIGN_DOWN(page, PAGE_SIZE)
|
---|
| 255 | };
|
---|
[1d432f9] | 256 |
|
---|
[235e6c7] | 257 | ASSERT(nolock || page_table_locked(as));
|
---|
[0c0410b] | 258 |
|
---|
[da1bafb] | 259 | link_t *cur = hash_table_find(&page_ht, key);
|
---|
| 260 | if (cur)
|
---|
| 261 | return hash_table_get_instance(cur, pte_t, link);
|
---|
| 262 |
|
---|
| 263 | return NULL;
|
---|
[6d7ffa65] | 264 | }
|
---|
[b45c443] | 265 |
|
---|
[c868e2d] | 266 | void ht_mapping_make_global(uintptr_t base, size_t size)
|
---|
| 267 | {
|
---|
| 268 | /* nothing to do */
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[f47fd19] | 271 | /** @}
|
---|
[b45c443] | 272 | */
|
---|