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