Rev 815 | Rev 826 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 815 | Rev 823 | ||
---|---|---|---|
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 | 44 | ||
45 | static index_t hash(__native key[]); |
45 | static index_t hash(__native key[]); |
46 | static bool compare(__native key[], count_t keys, link_t *item); |
46 | static bool compare(__native key[], count_t keys, link_t *item); |
47 | static void remove_callback(link_t *item); |
47 | static void remove_callback(link_t *item); |
48 | 48 | ||
49 | static void ht_mapping_insert(as_t *as, __address page, __address frame, int flags); |
49 | static void ht_mapping_insert(as_t *as, __address page, __address frame, int flags); |
50 | static pte_t *ht_mapping_find(as_t *as, __address page); |
50 | static pte_t *ht_mapping_find(as_t *as, __address page); |
51 | 51 | ||
52 | /** |
52 | /** |
53 | * This lock protects the page hash table. |
53 | * This lock protects the page hash table. |
54 | */ |
54 | */ |
55 | SPINLOCK_INITIALIZE(page_ht_lock); |
55 | SPINLOCK_INITIALIZE(page_ht_lock); |
56 | 56 | ||
57 | /** |
57 | /** |
58 | * Page hash table. |
58 | * Page hash table. |
59 | * The page hash table may be accessed only when page_ht_lock is held. |
59 | * The page hash table may be accessed only when page_ht_lock is held. |
60 | */ |
60 | */ |
61 | hash_table_t page_ht; |
61 | hash_table_t page_ht; |
62 | 62 | ||
63 | /** Hash table operations for page hash table. */ |
63 | /** Hash table operations for page hash table. */ |
64 | hash_table_operations_t ht_operations = { |
64 | hash_table_operations_t ht_operations = { |
65 | .hash = hash, |
65 | .hash = hash, |
66 | .compare = compare, |
66 | .compare = compare, |
67 | .remove_callback = remove_callback |
67 | .remove_callback = remove_callback |
68 | }; |
68 | }; |
69 | 69 | ||
70 | /** Page mapping operations for page hash table architectures. */ |
70 | /** Page mapping operations for page hash table architectures. */ |
71 | page_mapping_operations_t ht_mapping_operations = { |
71 | page_mapping_operations_t ht_mapping_operations = { |
72 | .mapping_insert = ht_mapping_insert, |
72 | .mapping_insert = ht_mapping_insert, |
73 | .mapping_find = ht_mapping_find |
73 | .mapping_find = ht_mapping_find |
74 | }; |
74 | }; |
75 | 75 | ||
76 | /** Compute page hash table index. |
76 | /** Compute page hash table index. |
77 | * |
77 | * |
78 | * @param key Array of two keys (i.e. page and address space). |
78 | * @param key Array of two keys (i.e. page and address space). |
79 | * |
79 | * |
80 | * @return Index into page hash table. |
80 | * @return Index into page hash table. |
81 | */ |
81 | */ |
82 | index_t hash(__native key[]) |
82 | index_t hash(__native key[]) |
83 | { |
83 | { |
84 | as_t *as = (as_t *) key[KEY_AS]; |
84 | as_t *as = (as_t *) key[KEY_AS]; |
85 | __address page = (__address) key[KEY_PAGE]; |
85 | __address page = (__address) key[KEY_PAGE]; |
86 | index_t index; |
86 | index_t index; |
87 | 87 | ||
88 | /* |
88 | /* |
89 | * Virtual page addresses have roughly the same probability |
89 | * Virtual page addresses have roughly the same probability |
90 | * of occurring. Least significant bits of VPN compose the |
90 | * of occurring. Least significant bits of VPN compose the |
91 | * hash index. |
91 | * hash index. |
92 | */ |
92 | */ |
93 | index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES-1)); |
93 | index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES-1)); |
94 | 94 | ||
95 | /* |
95 | /* |
96 | * Address space structures are likely to be allocated from |
96 | * Address space structures are likely to be allocated from |
97 | * similar addresses. Least significant bits compose the |
97 | * similar addresses. Least significant bits compose the |
98 | * hash index. |
98 | * hash index. |
99 | */ |
99 | */ |
100 | index |= ((__native) as) & (PAGE_HT_ENTRIES-1); |
100 | index |= ((__native) as) & (PAGE_HT_ENTRIES-1); |
101 | 101 | ||
102 | return index; |
102 | return index; |
103 | } |
103 | } |
104 | 104 | ||
105 | /** Compare page hash table item with page and/or address space. |
105 | /** Compare page hash table item with page and/or address space. |
106 | * |
106 | * |
107 | * @param key Array of one or two keys (i.e. page and/or address space). |
107 | * @param key Array of one or two keys (i.e. page and/or address space). |
108 | * @param keys Number of keys passed. |
108 | * @param keys Number of keys passed. |
109 | * @param item Item to compare the keys with. |
109 | * @param item Item to compare the keys with. |
110 | * |
110 | * |
111 | * @return true on match, false otherwise. |
111 | * @return true on match, false otherwise. |
112 | */ |
112 | */ |
113 | bool compare(__native key[], count_t keys, link_t *item) |
113 | bool compare(__native key[], count_t keys, link_t *item) |
114 | { |
114 | { |
115 | pte_t *t; |
115 | pte_t *t; |
116 | 116 | ||
117 | ASSERT(item); |
117 | ASSERT(item); |
118 | ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS)); |
118 | ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS)); |
119 | 119 | ||
120 | /* |
120 | /* |
121 | * Convert item to PTE. |
121 | * Convert item to PTE. |
122 | */ |
122 | */ |
123 | t = hash_table_get_instance(item, pte_t, link); |
123 | t = hash_table_get_instance(item, pte_t, link); |
124 | 124 | ||
125 | if (keys == PAGE_HT_KEYS) { |
125 | if (keys == PAGE_HT_KEYS) { |
126 | return (key[KEY_AS] == (__address) t->as) && (key[KEY_PAGE] == t->page); |
126 | return (key[KEY_AS] == (__address) t->as) && (key[KEY_PAGE] == t->page); |
127 | } else { |
127 | } else { |
128 | return (key[KEY_AS] == (__address) t->as); |
128 | return (key[KEY_AS] == (__address) t->as); |
129 | } |
129 | } |
130 | } |
130 | } |
131 | 131 | ||
132 | /** Callback on page hash table item removal. |
132 | /** Callback on page hash table item removal. |
133 | * |
133 | * |
134 | * @param item Page hash table item being removed. |
134 | * @param item Page hash table item being removed. |
135 | */ |
135 | */ |
136 | void remove_callback(link_t *item) |
136 | void remove_callback(link_t *item) |
137 | { |
137 | { |
138 | pte_t *t; |
138 | pte_t *t; |
139 | 139 | ||
140 | ASSERT(item); |
140 | ASSERT(item); |
141 | 141 | ||
142 | /* |
142 | /* |
143 | * Convert item to PTE. |
143 | * Convert item to PTE. |
144 | */ |
144 | */ |
145 | t = hash_table_get_instance(item, pte_t, link); |
145 | t = hash_table_get_instance(item, pte_t, link); |
146 | 146 | ||
147 | free(t); |
147 | free(t); |
148 | } |
148 | } |
149 | 149 | ||
150 | /** Map page to frame using page hash table. |
150 | /** Map page to frame using page hash table. |
151 | * |
151 | * |
152 | * Map virtual address 'page' to physical address 'frame' |
152 | * Map virtual address 'page' to physical address 'frame' |
153 | * using 'flags'. |
153 | * using 'flags'. |
154 | * |
154 | * |
155 | * The address space must be locked and interruptsmust be disabled. |
155 | * The address space must be locked and interruptsmust be disabled. |
156 | * |
156 | * |
157 | * @param as Address space to which page belongs. |
157 | * @param as Address space to which page belongs. |
158 | * @param page Virtual address of the page to be mapped. |
158 | * @param page Virtual address of the page to be mapped. |
159 | * @param frame Physical address of memory frame to which the mapping is done. |
159 | * @param frame Physical address of memory frame to which the mapping is done. |
160 | * @param flags Flags to be used for mapping. |
160 | * @param flags Flags to be used for mapping. |
161 | */ |
161 | */ |
162 | void ht_mapping_insert(as_t *as, __address page, __address frame, int flags) |
162 | void ht_mapping_insert(as_t *as, __address page, __address frame, int flags) |
163 | { |
163 | { |
164 | pte_t *t; |
164 | pte_t *t; |
165 | ipl_t ipl; |
165 | ipl_t ipl; |
166 | __native key[2] = { (__address) as, page }; |
166 | __native key[2] = { (__address) as, page }; |
167 | 167 | ||
168 | ipl = interrupts_disable(); |
168 | ipl = interrupts_disable(); |
169 | spinlock_lock(&page_ht_lock); |
169 | spinlock_lock(&page_ht_lock); |
170 | 170 | ||
171 | if (!hash_table_find(&page_ht, key)) { |
171 | if (!hash_table_find(&page_ht, key)) { |
172 | t = (pte_t *) malloc(sizeof(pte_t)); |
172 | t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC); |
173 | ASSERT(t != NULL); |
173 | ASSERT(t != NULL); |
174 | 174 | ||
175 | hash_table_insert(&page_ht, key, &t->link); |
175 | hash_table_insert(&page_ht, key, &t->link); |
176 | } |
176 | } |
177 | 177 | ||
178 | spinlock_unlock(&page_ht_lock); |
178 | spinlock_unlock(&page_ht_lock); |
179 | interrupts_restore(ipl); |
179 | interrupts_restore(ipl); |
180 | } |
180 | } |
181 | 181 | ||
182 | /** Find mapping for virtual page in page hash table. |
182 | /** Find mapping for virtual page in page hash table. |
183 | * |
183 | * |
184 | * Find mapping for virtual page. |
184 | * Find mapping for virtual page. |
185 | * |
185 | * |
186 | * The address space must be locked and interrupts must be disabled. |
186 | * The address space must be locked and interrupts must be disabled. |
187 | * |
187 | * |
188 | * @param as Address space to wich page belongs. |
188 | * @param as Address space to wich page belongs. |
189 | * @param page Virtual page. |
189 | * @param page Virtual page. |
190 | * |
190 | * |
191 | * @return NULL if there is no such mapping; requested mapping otherwise. |
191 | * @return NULL if there is no such mapping; requested mapping otherwise. |
192 | */ |
192 | */ |
193 | pte_t *ht_mapping_find(as_t *as, __address page) |
193 | pte_t *ht_mapping_find(as_t *as, __address page) |
194 | { |
194 | { |
195 | link_t *hlp; |
195 | link_t *hlp; |
196 | pte_t *t = NULL; |
196 | pte_t *t = NULL; |
197 | __native key[2] = { (__address) as, page }; |
197 | __native key[2] = { (__address) as, page }; |
198 | 198 | ||
199 | spinlock_lock(&page_ht_lock); |
199 | spinlock_lock(&page_ht_lock); |
200 | 200 | ||
201 | hlp = hash_table_find(&page_ht, key); |
201 | hlp = hash_table_find(&page_ht, key); |
202 | if (hlp) |
202 | if (hlp) |
203 | t = hash_table_get_instance(hlp, pte_t, link); |
203 | t = hash_table_get_instance(hlp, pte_t, link); |
204 | 204 | ||
205 | spinlock_unlock(&page_ht_lock); |
205 | spinlock_unlock(&page_ht_lock); |
206 | return t; |
206 | return t; |
207 | } |
207 | } |
208 | 208 |