Rev 3665 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1109 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2006 Jakub Jermar |
1109 | 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 | |||
1757 | jermar | 29 | /** @addtogroup sync |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | |||
1109 | jermar | 33 | /** |
1702 | cejka | 34 | * @file |
1264 | jermar | 35 | * @brief Kernel backend for futexes. |
1109 | jermar | 36 | */ |
37 | |||
38 | #include <synch/futex.h> |
||
39 | #include <synch/rwlock.h> |
||
1117 | jermar | 40 | #include <synch/spinlock.h> |
1109 | jermar | 41 | #include <synch/synch.h> |
42 | #include <mm/frame.h> |
||
43 | #include <mm/page.h> |
||
44 | #include <mm/slab.h> |
||
1117 | jermar | 45 | #include <proc/thread.h> |
1460 | jermar | 46 | #include <proc/task.h> |
1109 | jermar | 47 | #include <genarch/mm/page_pt.h> |
48 | #include <genarch/mm/page_ht.h> |
||
49 | #include <adt/hash_table.h> |
||
50 | #include <adt/list.h> |
||
51 | #include <arch.h> |
||
52 | #include <align.h> |
||
53 | #include <panic.h> |
||
54 | #include <errno.h> |
||
55 | #include <print.h> |
||
3862 | rimsky | 56 | #include <arch/asm.h> |
1109 | jermar | 57 | |
58 | #define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */ |
||
59 | |||
60 | static void futex_initialize(futex_t *futex); |
||
61 | |||
1780 | jermar | 62 | static futex_t *futex_find(uintptr_t paddr); |
63 | static index_t futex_ht_hash(unative_t *key); |
||
64 | static bool futex_ht_compare(unative_t *key, count_t keys, link_t *item); |
||
1109 | jermar | 65 | static void futex_ht_remove_callback(link_t *item); |
66 | |||
1460 | jermar | 67 | /** |
68 | * Read-write lock protecting global futex hash table. |
||
69 | * It is also used to serialize access to all futex_t structures. |
||
70 | * Must be acquired before the task futex B+tree lock. |
||
71 | */ |
||
1109 | jermar | 72 | static rwlock_t futex_ht_lock; |
73 | |||
74 | /** Futex hash table. */ |
||
75 | static hash_table_t futex_ht; |
||
76 | |||
77 | /** Futex hash table operations. */ |
||
78 | static hash_table_operations_t futex_ht_ops = { |
||
79 | .hash = futex_ht_hash, |
||
80 | .compare = futex_ht_compare, |
||
81 | .remove_callback = futex_ht_remove_callback |
||
82 | }; |
||
83 | |||
84 | /** Initialize futex subsystem. */ |
||
85 | void futex_init(void) |
||
86 | { |
||
87 | rwlock_initialize(&futex_ht_lock); |
||
88 | hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops); |
||
89 | } |
||
90 | |||
91 | /** Initialize kernel futex structure. |
||
92 | * |
||
93 | * @param futex Kernel futex structure. |
||
94 | */ |
||
95 | void futex_initialize(futex_t *futex) |
||
96 | { |
||
97 | waitq_initialize(&futex->wq); |
||
98 | link_initialize(&futex->ht_link); |
||
99 | futex->paddr = 0; |
||
1460 | jermar | 100 | futex->refcount = 1; |
1109 | jermar | 101 | } |
102 | |||
103 | /** Sleep in futex wait queue. |
||
104 | * |
||
105 | * @param uaddr Userspace address of the futex counter. |
||
2087 | jermar | 106 | * @param usec If non-zero, number of microseconds this thread is willing to |
107 | * sleep. |
||
1502 | jermar | 108 | * @param flags Select mode of operation. |
1109 | jermar | 109 | * |
2087 | jermar | 110 | * @return One of ESYNCH_TIMEOUT, ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED. See |
111 | * synch.h. If there is no physical mapping for uaddr ENOENT is returned. |
||
1109 | jermar | 112 | */ |
1780 | jermar | 113 | unative_t sys_futex_sleep_timeout(uintptr_t uaddr, uint32_t usec, int flags) |
1109 | jermar | 114 | { |
115 | futex_t *futex; |
||
1780 | jermar | 116 | uintptr_t paddr; |
1109 | jermar | 117 | pte_t *t; |
1117 | jermar | 118 | ipl_t ipl; |
3665 | rimsky | 119 | int rc; |
1109 | jermar | 120 | |
1117 | jermar | 121 | ipl = interrupts_disable(); |
122 | |||
1109 | jermar | 123 | /* |
124 | * Find physical address of futex counter. |
||
125 | */ |
||
126 | page_table_lock(AS, true); |
||
127 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
||
128 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
||
129 | page_table_unlock(AS, true); |
||
1117 | jermar | 130 | interrupts_restore(ipl); |
1780 | jermar | 131 | return (unative_t) ENOENT; |
1109 | jermar | 132 | } |
1414 | jermar | 133 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
1109 | jermar | 134 | page_table_unlock(AS, true); |
135 | |||
1117 | jermar | 136 | interrupts_restore(ipl); |
137 | |||
1153 | jermar | 138 | futex = futex_find(paddr); |
3665 | rimsky | 139 | |
140 | #ifdef CONFIG_UDEBUG |
||
141 | udebug_stoppable_begin(); |
||
142 | #endif |
||
143 | rc = waitq_sleep_timeout(&futex->wq, usec, flags | |
||
2087 | jermar | 144 | SYNCH_FLAGS_INTERRUPTIBLE); |
3665 | rimsky | 145 | |
146 | #ifdef CONFIG_UDEBUG |
||
147 | udebug_stoppable_end(); |
||
148 | #endif |
||
149 | return (unative_t) rc; |
||
1109 | jermar | 150 | } |
151 | |||
152 | /** Wakeup one thread waiting in futex wait queue. |
||
153 | * |
||
154 | * @param uaddr Userspace address of the futex counter. |
||
155 | * |
||
1414 | jermar | 156 | * @return ENOENT if there is no physical mapping for uaddr. |
1109 | jermar | 157 | */ |
1780 | jermar | 158 | unative_t sys_futex_wakeup(uintptr_t uaddr) |
1109 | jermar | 159 | { |
1153 | jermar | 160 | futex_t *futex; |
1780 | jermar | 161 | uintptr_t paddr; |
1109 | jermar | 162 | pte_t *t; |
1117 | jermar | 163 | ipl_t ipl; |
1109 | jermar | 164 | |
1117 | jermar | 165 | ipl = interrupts_disable(); |
166 | |||
1109 | jermar | 167 | /* |
168 | * Find physical address of futex counter. |
||
169 | */ |
||
170 | page_table_lock(AS, true); |
||
171 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
||
172 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
||
173 | page_table_unlock(AS, true); |
||
1117 | jermar | 174 | interrupts_restore(ipl); |
1780 | jermar | 175 | return (unative_t) ENOENT; |
1109 | jermar | 176 | } |
1414 | jermar | 177 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
1109 | jermar | 178 | page_table_unlock(AS, true); |
179 | |||
1117 | jermar | 180 | interrupts_restore(ipl); |
181 | |||
1153 | jermar | 182 | futex = futex_find(paddr); |
183 | |||
184 | waitq_wakeup(&futex->wq, WAKEUP_FIRST); |
||
185 | |||
186 | return 0; |
||
187 | } |
||
188 | |||
189 | /** Find kernel address of the futex structure corresponding to paddr. |
||
190 | * |
||
1460 | jermar | 191 | * If the structure does not exist already, a new one is created. |
1153 | jermar | 192 | * |
193 | * @param paddr Physical address of the userspace futex counter. |
||
194 | * |
||
195 | * @return Address of the kernel futex structure. |
||
196 | */ |
||
1780 | jermar | 197 | futex_t *futex_find(uintptr_t paddr) |
1153 | jermar | 198 | { |
199 | link_t *item; |
||
200 | futex_t *futex; |
||
1460 | jermar | 201 | btree_node_t *leaf; |
1153 | jermar | 202 | |
1109 | jermar | 203 | /* |
1153 | jermar | 204 | * Find the respective futex structure |
205 | * or allocate new one if it does not exist already. |
||
1109 | jermar | 206 | */ |
207 | rwlock_read_lock(&futex_ht_lock); |
||
208 | item = hash_table_find(&futex_ht, &paddr); |
||
209 | if (item) { |
||
210 | futex = hash_table_get_instance(item, futex_t, ht_link); |
||
1460 | jermar | 211 | |
212 | /* |
||
213 | * See if the current task knows this futex. |
||
214 | */ |
||
215 | mutex_lock(&TASK->futexes_lock); |
||
216 | if (!btree_search(&TASK->futexes, paddr, &leaf)) { |
||
217 | /* |
||
218 | * The futex is new to the current task. |
||
219 | * However, we only have read access. |
||
220 | * Gain write access and try again. |
||
221 | */ |
||
222 | mutex_unlock(&TASK->futexes_lock); |
||
223 | goto gain_write_access; |
||
224 | } |
||
225 | mutex_unlock(&TASK->futexes_lock); |
||
226 | |||
1153 | jermar | 227 | rwlock_read_unlock(&futex_ht_lock); |
228 | } else { |
||
1460 | jermar | 229 | gain_write_access: |
1153 | jermar | 230 | /* |
231 | * Upgrade to writer is not currently supported, |
||
1414 | jermar | 232 | * therefore, it is necessary to release the read lock |
1153 | jermar | 233 | * and reacquire it as a writer. |
234 | */ |
||
235 | rwlock_read_unlock(&futex_ht_lock); |
||
236 | |||
237 | rwlock_write_lock(&futex_ht_lock); |
||
238 | /* |
||
239 | * Avoid possible race condition by searching |
||
240 | * the hash table once again with write access. |
||
241 | */ |
||
242 | item = hash_table_find(&futex_ht, &paddr); |
||
243 | if (item) { |
||
244 | futex = hash_table_get_instance(item, futex_t, ht_link); |
||
1460 | jermar | 245 | |
246 | /* |
||
247 | * See if this futex is known to the current task. |
||
248 | */ |
||
249 | mutex_lock(&TASK->futexes_lock); |
||
250 | if (!btree_search(&TASK->futexes, paddr, &leaf)) { |
||
251 | /* |
||
252 | * The futex is new to the current task. |
||
253 | * Upgrade its reference count and put it to the |
||
254 | * current task's B+tree of known futexes. |
||
255 | */ |
||
256 | futex->refcount++; |
||
2087 | jermar | 257 | btree_insert(&TASK->futexes, paddr, futex, |
258 | leaf); |
||
1460 | jermar | 259 | } |
260 | mutex_unlock(&TASK->futexes_lock); |
||
261 | |||
1153 | jermar | 262 | rwlock_write_unlock(&futex_ht_lock); |
263 | } else { |
||
264 | futex = (futex_t *) malloc(sizeof(futex_t), 0); |
||
265 | futex_initialize(futex); |
||
266 | futex->paddr = paddr; |
||
267 | hash_table_insert(&futex_ht, &paddr, &futex->ht_link); |
||
1460 | jermar | 268 | |
269 | /* |
||
270 | * This is the first task referencing the futex. |
||
271 | * It can be directly inserted into its |
||
272 | * B+tree of known futexes. |
||
273 | */ |
||
274 | mutex_lock(&TASK->futexes_lock); |
||
275 | btree_insert(&TASK->futexes, paddr, futex, NULL); |
||
276 | mutex_unlock(&TASK->futexes_lock); |
||
277 | |||
1153 | jermar | 278 | rwlock_write_unlock(&futex_ht_lock); |
279 | } |
||
1109 | jermar | 280 | } |
281 | |||
1153 | jermar | 282 | return futex; |
1109 | jermar | 283 | } |
284 | |||
285 | /** Compute hash index into futex hash table. |
||
286 | * |
||
2087 | jermar | 287 | * @param key Address where the key (i.e. physical address of futex counter) is |
288 | * stored. |
||
1109 | jermar | 289 | * |
290 | * @return Index into futex hash table. |
||
291 | */ |
||
1780 | jermar | 292 | index_t futex_ht_hash(unative_t *key) |
1109 | jermar | 293 | { |
294 | return *key & (FUTEX_HT_SIZE-1); |
||
295 | } |
||
296 | |||
297 | /** Compare futex hash table item with a key. |
||
298 | * |
||
2087 | jermar | 299 | * @param key Address where the key (i.e. physical address of futex counter) is |
300 | * stored. |
||
1109 | jermar | 301 | * |
1248 | jermar | 302 | * @return True if the item matches the key. False otherwise. |
1109 | jermar | 303 | */ |
1780 | jermar | 304 | bool futex_ht_compare(unative_t *key, count_t keys, link_t *item) |
1109 | jermar | 305 | { |
306 | futex_t *futex; |
||
307 | |||
308 | ASSERT(keys == 1); |
||
309 | |||
310 | futex = hash_table_get_instance(item, futex_t, ht_link); |
||
311 | return *key == futex->paddr; |
||
312 | } |
||
313 | |||
314 | /** Callback for removal items from futex hash table. |
||
315 | * |
||
316 | * @param item Item removed from the hash table. |
||
317 | */ |
||
318 | void futex_ht_remove_callback(link_t *item) |
||
319 | { |
||
320 | futex_t *futex; |
||
321 | |||
322 | futex = hash_table_get_instance(item, futex_t, ht_link); |
||
323 | free(futex); |
||
324 | } |
||
1583 | jermar | 325 | |
326 | /** Remove references from futexes known to the current task. */ |
||
327 | void futex_cleanup(void) |
||
328 | { |
||
1586 | jermar | 329 | link_t *cur; |
330 | |||
331 | rwlock_write_lock(&futex_ht_lock); |
||
332 | mutex_lock(&TASK->futexes_lock); |
||
333 | |||
2087 | jermar | 334 | for (cur = TASK->futexes.leaf_head.next; |
335 | cur != &TASK->futexes.leaf_head; cur = cur->next) { |
||
1586 | jermar | 336 | btree_node_t *node; |
2745 | decky | 337 | unsigned int i; |
1586 | jermar | 338 | |
339 | node = list_get_instance(cur, btree_node_t, leaf_link); |
||
340 | for (i = 0; i < node->keys; i++) { |
||
341 | futex_t *ftx; |
||
1780 | jermar | 342 | uintptr_t paddr = node->key[i]; |
1586 | jermar | 343 | |
344 | ftx = (futex_t *) node->value[i]; |
||
345 | if (--ftx->refcount == 0) |
||
346 | hash_table_remove(&futex_ht, &paddr, 1); |
||
347 | } |
||
348 | } |
||
349 | |||
350 | mutex_unlock(&TASK->futexes_lock); |
||
351 | rwlock_write_unlock(&futex_ht_lock); |
||
1583 | jermar | 352 | } |
1702 | cejka | 353 | |
1757 | jermar | 354 | /** @} |
1702 | cejka | 355 | */ |