Rev 2089 | Rev 4490 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2089 | Rev 2141 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Jakub Jermar |
2 | * Copyright (c) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genarchmm |
29 | /** @addtogroup genarchmm |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Virtual Address Translation (VAT) for global page hash table. |
35 | * @brief Virtual Address Translation (VAT) for global page hash table. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <genarch/mm/page_ht.h> |
38 | #include <genarch/mm/page_ht.h> |
39 | #include <mm/page.h> |
39 | #include <mm/page.h> |
40 | #include <arch/mm/page.h> |
40 | #include <arch/mm/page.h> |
41 | #include <mm/frame.h> |
41 | #include <mm/frame.h> |
42 | #include <mm/slab.h> |
42 | #include <mm/slab.h> |
43 | #include <mm/as.h> |
43 | #include <mm/as.h> |
44 | #include <arch/mm/asid.h> |
44 | #include <arch/mm/asid.h> |
45 | #include <arch/types.h> |
45 | #include <arch/types.h> |
46 | #include <arch/asm.h> |
46 | #include <arch/asm.h> |
47 | #include <synch/spinlock.h> |
47 | #include <synch/spinlock.h> |
48 | #include <arch.h> |
48 | #include <arch.h> |
49 | #include <debug.h> |
49 | #include <debug.h> |
50 | #include <memstr.h> |
50 | #include <memstr.h> |
51 | #include <adt/hash_table.h> |
51 | #include <adt/hash_table.h> |
52 | #include <align.h> |
52 | #include <align.h> |
53 | 53 | ||
54 | static index_t hash(unative_t key[]); |
54 | static index_t hash(unative_t key[]); |
55 | static bool compare(unative_t key[], count_t keys, link_t *item); |
55 | static bool compare(unative_t key[], count_t keys, link_t *item); |
56 | static void remove_callback(link_t *item); |
56 | static void remove_callback(link_t *item); |
57 | 57 | ||
58 | static void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags); |
58 | static void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, |
- | 59 | int flags); |
|
59 | static void ht_mapping_remove(as_t *as, uintptr_t page); |
60 | static void ht_mapping_remove(as_t *as, uintptr_t page); |
60 | static pte_t *ht_mapping_find(as_t *as, uintptr_t page); |
61 | static pte_t *ht_mapping_find(as_t *as, uintptr_t page); |
61 | 62 | ||
62 | /** |
63 | /** |
63 | * This lock protects the page hash table. It must be acquired |
64 | * This lock protects the page hash table. It must be acquired |
64 | * after address space lock and after any address space area |
65 | * after address space lock and after any address space area |
65 | * locks. |
66 | * locks. |
66 | */ |
67 | */ |
67 | mutex_t page_ht_lock; |
68 | mutex_t page_ht_lock; |
68 | 69 | ||
69 | /** |
70 | /** |
70 | * Page hash table. |
71 | * Page hash table. |
71 | * The page hash table may be accessed only when page_ht_lock is held. |
72 | * The page hash table may be accessed only when page_ht_lock is held. |
72 | */ |
73 | */ |
73 | hash_table_t page_ht; |
74 | hash_table_t page_ht; |
74 | 75 | ||
75 | /** Hash table operations for page hash table. */ |
76 | /** Hash table operations for page hash table. */ |
76 | hash_table_operations_t ht_operations = { |
77 | hash_table_operations_t ht_operations = { |
77 | .hash = hash, |
78 | .hash = hash, |
78 | .compare = compare, |
79 | .compare = compare, |
79 | .remove_callback = remove_callback |
80 | .remove_callback = remove_callback |
80 | }; |
81 | }; |
81 | 82 | ||
82 | /** Page mapping operations for page hash table architectures. */ |
83 | /** Page mapping operations for page hash table architectures. */ |
83 | page_mapping_operations_t ht_mapping_operations = { |
84 | page_mapping_operations_t ht_mapping_operations = { |
84 | .mapping_insert = ht_mapping_insert, |
85 | .mapping_insert = ht_mapping_insert, |
85 | .mapping_remove = ht_mapping_remove, |
86 | .mapping_remove = ht_mapping_remove, |
86 | .mapping_find = ht_mapping_find |
87 | .mapping_find = ht_mapping_find |
87 | }; |
88 | }; |
88 | 89 | ||
89 | /** Compute page hash table index. |
90 | /** Compute page hash table index. |
90 | * |
91 | * |
91 | * @param key Array of two keys (i.e. page and address space). |
92 | * @param key Array of two keys (i.e. page and address space). |
92 | * |
93 | * |
93 | * @return Index into page hash table. |
94 | * @return Index into page hash table. |
94 | */ |
95 | */ |
95 | index_t hash(unative_t key[]) |
96 | index_t hash(unative_t key[]) |
96 | { |
97 | { |
97 | as_t *as = (as_t *) key[KEY_AS]; |
98 | as_t *as = (as_t *) key[KEY_AS]; |
98 | uintptr_t page = (uintptr_t) key[KEY_PAGE]; |
99 | uintptr_t page = (uintptr_t) key[KEY_PAGE]; |
99 | index_t index; |
100 | index_t index; |
100 | 101 | ||
101 | /* |
102 | /* |
102 | * Virtual page addresses have roughly the same probability |
103 | * Virtual page addresses have roughly the same probability |
103 | * of occurring. Least significant bits of VPN compose the |
104 | * of occurring. Least significant bits of VPN compose the |
104 | * hash index. |
105 | * hash index. |
105 | */ |
106 | */ |
106 | index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES-1)); |
107 | index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES - 1)); |
107 | 108 | ||
108 | /* |
109 | /* |
109 | * Address space structures are likely to be allocated from |
110 | * Address space structures are likely to be allocated from |
110 | * similar addresses. Least significant bits compose the |
111 | * similar addresses. Least significant bits compose the |
111 | * hash index. |
112 | * hash index. |
112 | */ |
113 | */ |
113 | index |= ((unative_t) as) & (PAGE_HT_ENTRIES-1); |
114 | index |= ((unative_t) as) & (PAGE_HT_ENTRIES - 1); |
114 | 115 | ||
115 | return index; |
116 | return index; |
116 | } |
117 | } |
117 | 118 | ||
118 | /** Compare page hash table item with page and/or address space. |
119 | /** Compare page hash table item with page and/or address space. |
119 | * |
120 | * |
120 | * @param key Array of one or two keys (i.e. page and/or address space). |
121 | * @param key Array of one or two keys (i.e. page and/or address space). |
121 | * @param keys Number of keys passed. |
122 | * @param keys Number of keys passed. |
122 | * @param item Item to compare the keys with. |
123 | * @param item Item to compare the keys with. |
123 | * |
124 | * |
124 | * @return true on match, false otherwise. |
125 | * @return true on match, false otherwise. |
125 | */ |
126 | */ |
126 | bool compare(unative_t key[], count_t keys, link_t *item) |
127 | bool compare(unative_t key[], count_t keys, link_t *item) |
127 | { |
128 | { |
128 | pte_t *t; |
129 | pte_t *t; |
129 | 130 | ||
130 | ASSERT(item); |
131 | ASSERT(item); |
131 | ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS)); |
132 | ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS)); |
132 | 133 | ||
133 | /* |
134 | /* |
134 | * Convert item to PTE. |
135 | * Convert item to PTE. |
135 | */ |
136 | */ |
136 | t = hash_table_get_instance(item, pte_t, link); |
137 | t = hash_table_get_instance(item, pte_t, link); |
137 | 138 | ||
138 | if (keys == PAGE_HT_KEYS) { |
139 | if (keys == PAGE_HT_KEYS) { |
139 | return (key[KEY_AS] == (uintptr_t) t->as) && (key[KEY_PAGE] == t->page); |
140 | return (key[KEY_AS] == (uintptr_t) t->as) && |
- | 141 | (key[KEY_PAGE] == t->page); |
|
140 | } else { |
142 | } else { |
141 | return (key[KEY_AS] == (uintptr_t) t->as); |
143 | return (key[KEY_AS] == (uintptr_t) t->as); |
142 | } |
144 | } |
143 | } |
145 | } |
144 | 146 | ||
145 | /** Callback on page hash table item removal. |
147 | /** Callback on page hash table item removal. |
146 | * |
148 | * |
147 | * @param item Page hash table item being removed. |
149 | * @param item Page hash table item being removed. |
148 | */ |
150 | */ |
149 | void remove_callback(link_t *item) |
151 | void remove_callback(link_t *item) |
150 | { |
152 | { |
151 | pte_t *t; |
153 | pte_t *t; |
152 | 154 | ||
153 | ASSERT(item); |
155 | ASSERT(item); |
154 | 156 | ||
155 | /* |
157 | /* |
156 | * Convert item to PTE. |
158 | * Convert item to PTE. |
157 | */ |
159 | */ |
158 | t = hash_table_get_instance(item, pte_t, link); |
160 | t = hash_table_get_instance(item, pte_t, link); |
159 | 161 | ||
160 | free(t); |
162 | free(t); |
161 | } |
163 | } |
162 | 164 | ||
163 | /** Map page to frame using page hash table. |
165 | /** Map page to frame using page hash table. |
164 | * |
166 | * |
165 | * Map virtual address page to physical address frame |
167 | * Map virtual address page to physical address frame |
166 | * using flags. |
168 | * using flags. |
167 | * |
169 | * |
168 | * The page table must be locked and interrupts must be disabled. |
170 | * The page table must be locked and interrupts must be disabled. |
169 | * |
171 | * |
170 | * @param as Address space to which page belongs. |
172 | * @param as Address space to which page belongs. |
171 | * @param page Virtual address of the page to be mapped. |
173 | * @param page Virtual address of the page to be mapped. |
172 | * @param frame Physical address of memory frame to which the mapping is done. |
174 | * @param frame Physical address of memory frame to which the mapping is done. |
173 | * @param flags Flags to be used for mapping. |
175 | * @param flags Flags to be used for mapping. |
174 | */ |
176 | */ |
175 | void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags) |
177 | void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags) |
176 | { |
178 | { |
177 | pte_t *t; |
179 | pte_t *t; |
- | 180 | unative_t key[2] = { |
|
- | 181 | (uintptr_t) as, |
|
178 | unative_t key[2] = { (uintptr_t) as, page = ALIGN_DOWN(page, PAGE_SIZE) }; |
182 | page = ALIGN_DOWN(page, PAGE_SIZE) |
- | 183 | }; |
|
179 | 184 | ||
180 | if (!hash_table_find(&page_ht, key)) { |
185 | if (!hash_table_find(&page_ht, key)) { |
181 | t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC); |
186 | t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC); |
182 | ASSERT(t != NULL); |
187 | ASSERT(t != NULL); |
183 | 188 | ||
184 | t->g = (flags & PAGE_GLOBAL) != 0; |
189 | t->g = (flags & PAGE_GLOBAL) != 0; |
185 | t->x = (flags & PAGE_EXEC) != 0; |
190 | t->x = (flags & PAGE_EXEC) != 0; |
186 | t->w = (flags & PAGE_WRITE) != 0; |
191 | t->w = (flags & PAGE_WRITE) != 0; |
187 | t->k = !(flags & PAGE_USER); |
192 | t->k = !(flags & PAGE_USER); |
188 | t->c = (flags & PAGE_CACHEABLE) != 0; |
193 | t->c = (flags & PAGE_CACHEABLE) != 0; |
189 | t->p = !(flags & PAGE_NOT_PRESENT); |
194 | t->p = !(flags & PAGE_NOT_PRESENT); |
190 | 195 | ||
191 | t->as = as; |
196 | t->as = as; |
192 | t->page = ALIGN_DOWN(page, PAGE_SIZE); |
197 | t->page = ALIGN_DOWN(page, PAGE_SIZE); |
193 | t->frame = ALIGN_DOWN(frame, FRAME_SIZE); |
198 | t->frame = ALIGN_DOWN(frame, FRAME_SIZE); |
194 | 199 | ||
195 | hash_table_insert(&page_ht, key, &t->link); |
200 | hash_table_insert(&page_ht, key, &t->link); |
196 | } |
201 | } |
197 | } |
202 | } |
198 | 203 | ||
199 | /** Remove mapping of page from page hash table. |
204 | /** Remove mapping of page from page hash table. |
200 | * |
205 | * |
201 | * Remove any mapping of page within address space as. |
206 | * Remove any mapping of page within address space as. |
202 | * TLB shootdown should follow in order to make effects of |
207 | * TLB shootdown should follow in order to make effects of |
203 | * this call visible. |
208 | * this call visible. |
204 | * |
209 | * |
205 | * The page table must be locked and interrupts must be disabled. |
210 | * The page table must be locked and interrupts must be disabled. |
206 | * |
211 | * |
207 | * @param as Address space to wich page belongs. |
212 | * @param as Address space to wich page belongs. |
208 | * @param page Virtual address of the page to be demapped. |
213 | * @param page Virtual address of the page to be demapped. |
209 | */ |
214 | */ |
210 | void ht_mapping_remove(as_t *as, uintptr_t page) |
215 | void ht_mapping_remove(as_t *as, uintptr_t page) |
211 | { |
216 | { |
- | 217 | unative_t key[2] = { |
|
- | 218 | (uintptr_t) as, |
|
212 | unative_t key[2] = { (uintptr_t) as, page = ALIGN_DOWN(page, PAGE_SIZE) }; |
219 | page = ALIGN_DOWN(page, PAGE_SIZE) |
- | 220 | }; |
|
213 | 221 | ||
214 | /* |
222 | /* |
215 | * Note that removed PTE's will be freed |
223 | * Note that removed PTE's will be freed |
216 | * by remove_callback(). |
224 | * by remove_callback(). |
217 | */ |
225 | */ |
218 | hash_table_remove(&page_ht, key, 2); |
226 | hash_table_remove(&page_ht, key, 2); |
219 | } |
227 | } |
220 | 228 | ||
221 | 229 | ||
222 | /** Find mapping for virtual page in page hash table. |
230 | /** Find mapping for virtual page in page hash table. |
223 | * |
231 | * |
224 | * Find mapping for virtual page. |
232 | * Find mapping for virtual page. |
225 | * |
233 | * |
226 | * The page table must be locked and interrupts must be disabled. |
234 | * The page table must be locked and interrupts must be disabled. |
227 | * |
235 | * |
228 | * @param as Address space to wich page belongs. |
236 | * @param as Address space to wich page belongs. |
229 | * @param page Virtual page. |
237 | * @param page Virtual page. |
230 | * |
238 | * |
231 | * @return NULL if there is no such mapping; requested mapping otherwise. |
239 | * @return NULL if there is no such mapping; requested mapping otherwise. |
232 | */ |
240 | */ |
233 | pte_t *ht_mapping_find(as_t *as, uintptr_t page) |
241 | pte_t *ht_mapping_find(as_t *as, uintptr_t page) |
234 | { |
242 | { |
235 | link_t *hlp; |
243 | link_t *hlp; |
236 | pte_t *t = NULL; |
244 | pte_t *t = NULL; |
- | 245 | unative_t key[2] = { |
|
- | 246 | (uintptr_t) as, |
|
237 | unative_t key[2] = { (uintptr_t) as, page = ALIGN_DOWN(page, PAGE_SIZE) }; |
247 | page = ALIGN_DOWN(page, PAGE_SIZE) |
- | 248 | }; |
|
238 | 249 | ||
239 | hlp = hash_table_find(&page_ht, key); |
250 | hlp = hash_table_find(&page_ht, key); |
240 | if (hlp) |
251 | if (hlp) |
241 | t = hash_table_get_instance(hlp, pte_t, link); |
252 | t = hash_table_get_instance(hlp, pte_t, link); |
242 | 253 | ||
243 | return t; |
254 | return t; |
244 | } |
255 | } |
245 | 256 | ||
246 | /** @} |
257 | /** @} |
247 | */ |
258 | */ |
248 | 259 |