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