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