[6d7ffa65] | 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 | #include <genarch/mm/page_ht.h>
|
---|
| 30 | #include <mm/page.h>
|
---|
[c7ec94a4] | 31 | #include <arch/mm/page.h>
|
---|
[6d7ffa65] | 32 | #include <mm/frame.h>
|
---|
[5e3757d] | 33 | #include <mm/slab.h>
|
---|
[fc1e4f6] | 34 | #include <mm/as.h>
|
---|
[677a6d5] | 35 | #include <arch/mm/asid.h>
|
---|
[6d7ffa65] | 36 | #include <arch/types.h>
|
---|
| 37 | #include <typedefs.h>
|
---|
| 38 | #include <arch/asm.h>
|
---|
[2a003d5b] | 39 | #include <synch/spinlock.h>
|
---|
| 40 | #include <arch.h>
|
---|
[0c0410b] | 41 | #include <debug.h>
|
---|
[ef67bab] | 42 | #include <memstr.h>
|
---|
[c7ec94a4] | 43 | #include <adt/hash_table.h>
|
---|
[a2a46ba] | 44 | #include <align.h>
|
---|
[c7ec94a4] | 45 |
|
---|
| 46 | static index_t hash(__native key[]);
|
---|
| 47 | static bool compare(__native key[], count_t keys, link_t *item);
|
---|
| 48 | static void remove_callback(link_t *item);
|
---|
| 49 |
|
---|
| 50 | static void ht_mapping_insert(as_t *as, __address page, __address frame, int flags);
|
---|
[8f00329] | 51 | static void ht_mapping_remove(as_t *as, __address page);
|
---|
[c7ec94a4] | 52 | static pte_t *ht_mapping_find(as_t *as, __address page);
|
---|
[6d7ffa65] | 53 |
|
---|
[2a003d5b] | 54 | /**
|
---|
[ef67bab] | 55 | * This lock protects the page hash table.
|
---|
[2a003d5b] | 56 | */
|
---|
| 57 | SPINLOCK_INITIALIZE(page_ht_lock);
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
[c7ec94a4] | 60 | * Page hash table.
|
---|
[2a003d5b] | 61 | * The page hash table may be accessed only when page_ht_lock is held.
|
---|
| 62 | */
|
---|
[c7ec94a4] | 63 | hash_table_t page_ht;
|
---|
[2a003d5b] | 64 |
|
---|
[c7ec94a4] | 65 | /** Hash table operations for page hash table. */
|
---|
| 66 | hash_table_operations_t ht_operations = {
|
---|
| 67 | .hash = hash,
|
---|
| 68 | .compare = compare,
|
---|
| 69 | .remove_callback = remove_callback
|
---|
| 70 | };
|
---|
[6d7ffa65] | 71 |
|
---|
[f5935ed] | 72 | /** Page mapping operations for page hash table architectures. */
|
---|
| 73 | page_mapping_operations_t ht_mapping_operations = {
|
---|
[6d7ffa65] | 74 | .mapping_insert = ht_mapping_insert,
|
---|
[8f00329] | 75 | .mapping_remove = ht_mapping_remove,
|
---|
[071a8ae6] | 76 | .mapping_find = ht_mapping_find
|
---|
[6d7ffa65] | 77 | };
|
---|
| 78 |
|
---|
[c7ec94a4] | 79 | /** Compute page hash table index.
|
---|
| 80 | *
|
---|
| 81 | * @param key Array of two keys (i.e. page and address space).
|
---|
| 82 | *
|
---|
| 83 | * @return Index into page hash table.
|
---|
| 84 | */
|
---|
| 85 | index_t hash(__native key[])
|
---|
| 86 | {
|
---|
| 87 | as_t *as = (as_t *) key[KEY_AS];
|
---|
| 88 | __address page = (__address) key[KEY_PAGE];
|
---|
| 89 | index_t index;
|
---|
| 90 |
|
---|
| 91 | /*
|
---|
| 92 | * Virtual page addresses have roughly the same probability
|
---|
| 93 | * of occurring. Least significant bits of VPN compose the
|
---|
| 94 | * hash index.
|
---|
| 95 | */
|
---|
| 96 | index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES-1));
|
---|
| 97 |
|
---|
| 98 | /*
|
---|
| 99 | * Address space structures are likely to be allocated from
|
---|
| 100 | * similar addresses. Least significant bits compose the
|
---|
| 101 | * hash index.
|
---|
| 102 | */
|
---|
| 103 | index |= ((__native) as) & (PAGE_HT_ENTRIES-1);
|
---|
| 104 |
|
---|
| 105 | return index;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /** Compare page hash table item with page and/or address space.
|
---|
| 109 | *
|
---|
| 110 | * @param key Array of one or two keys (i.e. page and/or address space).
|
---|
| 111 | * @param keys Number of keys passed.
|
---|
| 112 | * @param item Item to compare the keys with.
|
---|
| 113 | *
|
---|
| 114 | * @return true on match, false otherwise.
|
---|
| 115 | */
|
---|
| 116 | bool compare(__native key[], count_t keys, link_t *item)
|
---|
| 117 | {
|
---|
| 118 | pte_t *t;
|
---|
| 119 |
|
---|
| 120 | ASSERT(item);
|
---|
| 121 | ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS));
|
---|
| 122 |
|
---|
| 123 | /*
|
---|
| 124 | * Convert item to PTE.
|
---|
| 125 | */
|
---|
[f5935ed] | 126 | t = hash_table_get_instance(item, pte_t, link);
|
---|
[c7ec94a4] | 127 |
|
---|
| 128 | if (keys == PAGE_HT_KEYS) {
|
---|
| 129 | return (key[KEY_AS] == (__address) t->as) && (key[KEY_PAGE] == t->page);
|
---|
| 130 | } else {
|
---|
| 131 | return (key[KEY_AS] == (__address) t->as);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /** Callback on page hash table item removal.
|
---|
| 136 | *
|
---|
| 137 | * @param item Page hash table item being removed.
|
---|
| 138 | */
|
---|
| 139 | void remove_callback(link_t *item)
|
---|
| 140 | {
|
---|
| 141 | pte_t *t;
|
---|
| 142 |
|
---|
| 143 | ASSERT(item);
|
---|
| 144 |
|
---|
| 145 | /*
|
---|
| 146 | * Convert item to PTE.
|
---|
| 147 | */
|
---|
[f5935ed] | 148 | t = hash_table_get_instance(item, pte_t, link);
|
---|
[c7ec94a4] | 149 |
|
---|
| 150 | free(t);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[6d7ffa65] | 153 | /** Map page to frame using page hash table.
|
---|
| 154 | *
|
---|
| 155 | * Map virtual address 'page' to physical address 'frame'
|
---|
[ef67bab] | 156 | * using 'flags'.
|
---|
[6d7ffa65] | 157 | *
|
---|
[ef67bab] | 158 | * The address space must be locked and interruptsmust be disabled.
|
---|
| 159 | *
|
---|
| 160 | * @param as Address space to which page belongs.
|
---|
[6d7ffa65] | 161 | * @param page Virtual address of the page to be mapped.
|
---|
| 162 | * @param frame Physical address of memory frame to which the mapping is done.
|
---|
| 163 | * @param flags Flags to be used for mapping.
|
---|
| 164 | */
|
---|
[ef67bab] | 165 | void ht_mapping_insert(as_t *as, __address page, __address frame, int flags)
|
---|
[6d7ffa65] | 166 | {
|
---|
[c7ec94a4] | 167 | pte_t *t;
|
---|
[a2a46ba] | 168 | __native key[2] = { (__address) as, page = ALIGN_DOWN(page, PAGE_SIZE) };
|
---|
[2a003d5b] | 169 |
|
---|
| 170 | spinlock_lock(&page_ht_lock);
|
---|
[fc1e4f6] | 171 |
|
---|
[c7ec94a4] | 172 | if (!hash_table_find(&page_ht, key)) {
|
---|
[7e4e532] | 173 | t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
|
---|
[c7ec94a4] | 174 | ASSERT(t != NULL);
|
---|
[9ad03fe] | 175 |
|
---|
| 176 | t->g = (flags & PAGE_GLOBAL) != 0;
|
---|
| 177 | t->x = (flags & PAGE_EXEC) != 0;
|
---|
| 178 | t->w = (flags & PAGE_WRITE) != 0;
|
---|
| 179 | t->k = !(flags & PAGE_USER);
|
---|
| 180 | t->c = (flags & PAGE_CACHEABLE) != 0;
|
---|
| 181 | t->p = !(flags & PAGE_NOT_PRESENT);
|
---|
| 182 |
|
---|
| 183 | t->as = as;
|
---|
| 184 | t->page = page;
|
---|
| 185 | t->frame = frame;
|
---|
| 186 |
|
---|
[c7ec94a4] | 187 | hash_table_insert(&page_ht, key, &t->link);
|
---|
[0c0410b] | 188 | }
|
---|
[2a003d5b] | 189 |
|
---|
| 190 | spinlock_unlock(&page_ht_lock);
|
---|
[6d7ffa65] | 191 | }
|
---|
| 192 |
|
---|
[8f00329] | 193 | /** Remove mapping of page from page hash table.
|
---|
| 194 | *
|
---|
| 195 | * Remove any mapping of 'page' within address space 'as'.
|
---|
| 196 | * TLB shootdown should follow in order to make effects of
|
---|
| 197 | * this call visible.
|
---|
| 198 | *
|
---|
| 199 | * The address space must be locked and interrupts must be disabled.
|
---|
| 200 | *
|
---|
| 201 | * @param as Address space to wich page belongs.
|
---|
| 202 | * @param page Virtual address of the page to be demapped.
|
---|
| 203 | */
|
---|
| 204 | void ht_mapping_remove(as_t *as, __address page)
|
---|
| 205 | {
|
---|
[a2a46ba] | 206 | __native key[2] = { (__address) as, page = ALIGN_DOWN(page, PAGE_SIZE) };
|
---|
[8f00329] | 207 |
|
---|
| 208 | spinlock_lock(&page_ht_lock);
|
---|
| 209 |
|
---|
| 210 | /*
|
---|
| 211 | * Note that removed PTE's will be freed
|
---|
| 212 | * by remove_callback().
|
---|
| 213 | */
|
---|
| 214 | hash_table_remove(&page_ht, key, 2);
|
---|
| 215 |
|
---|
| 216 | spinlock_unlock(&page_ht_lock);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
[6d7ffa65] | 220 | /** Find mapping for virtual page in page hash table.
|
---|
| 221 | *
|
---|
| 222 | * Find mapping for virtual page.
|
---|
| 223 | *
|
---|
[ef67bab] | 224 | * The address space must be locked and interrupts must be disabled.
|
---|
[2a003d5b] | 225 | *
|
---|
[ef67bab] | 226 | * @param as Address space to wich page belongs.
|
---|
[6d7ffa65] | 227 | * @param page Virtual page.
|
---|
| 228 | *
|
---|
[0c0410b] | 229 | * @return NULL if there is no such mapping; requested mapping otherwise.
|
---|
[6d7ffa65] | 230 | */
|
---|
[ef67bab] | 231 | pte_t *ht_mapping_find(as_t *as, __address page)
|
---|
[6d7ffa65] | 232 | {
|
---|
[c7ec94a4] | 233 | link_t *hlp;
|
---|
| 234 | pte_t *t = NULL;
|
---|
[a2a46ba] | 235 | __native key[2] = { (__address) as, page = ALIGN_DOWN(page, PAGE_SIZE) };
|
---|
[0c0410b] | 236 |
|
---|
[2a003d5b] | 237 | spinlock_lock(&page_ht_lock);
|
---|
| 238 |
|
---|
[c7ec94a4] | 239 | hlp = hash_table_find(&page_ht, key);
|
---|
| 240 | if (hlp)
|
---|
[f5935ed] | 241 | t = hash_table_get_instance(hlp, pte_t, link);
|
---|
[c7ec94a4] | 242 |
|
---|
[2a003d5b] | 243 | spinlock_unlock(&page_ht_lock);
|
---|
[c7ec94a4] | 244 | return t;
|
---|
[6d7ffa65] | 245 | }
|
---|