[9aa72b4] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[669f3d32] | 3 | * Copyright (c) 2012 Adam Hraska
|
---|
[9aa72b4] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[cc73a8a1] | 30 | /** @addtogroup sync
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[9aa72b4] | 34 | /**
|
---|
[b45c443] | 35 | * @file
|
---|
[cf26ba9] | 36 | * @brief Kernel backend for futexes.
|
---|
[ba368a6] | 37 | *
|
---|
| 38 | * Kernel futex objects are stored in a global hash table futex_ht
|
---|
| 39 | * where the physical address of the futex variable (futex_t.paddr)
|
---|
| 40 | * is used as the lookup key. As a result multiple address spaces
|
---|
| 41 | * may share the same futex variable.
|
---|
| 42 | *
|
---|
| 43 | * A kernel futex object is created the first time a task accesses
|
---|
| 44 | * the futex (having a futex variable at a physical address not
|
---|
| 45 | * encountered before). Futex object's lifetime is governed by
|
---|
| 46 | * a reference count that represents the number of all the different
|
---|
| 47 | * user space virtual addresses from all tasks that map to the
|
---|
| 48 | * physical address of the futex variable. A futex object is freed
|
---|
| 49 | * when the last task having accessed the futex exits.
|
---|
| 50 | *
|
---|
| 51 | * Each task keeps track of the futex objects it accessed in a list
|
---|
| 52 | * of pointers (futex_ptr_t, task->futex_list) to the different futex
|
---|
| 53 | * objects.
|
---|
| 54 | *
|
---|
| 55 | * To speed up translation of futex variables' virtual addresses
|
---|
| 56 | * to their physical addresses, futex pointers accessed by the
|
---|
| 57 | * task are furthermore stored in a concurrent hash table (CHT,
|
---|
| 58 | * task->futexes->ht). A single lookup without locks or accesses
|
---|
| 59 | * to the page table translates a futex variable's virtual address
|
---|
| 60 | * into its futex kernel object.
|
---|
[9aa72b4] | 61 | */
|
---|
| 62 |
|
---|
| 63 | #include <synch/futex.h>
|
---|
[ee42e43] | 64 | #include <synch/mutex.h>
|
---|
[303c94c] | 65 | #include <synch/spinlock.h>
|
---|
[669f3d32] | 66 | #include <synch/rcu.h>
|
---|
[9aa72b4] | 67 | #include <mm/frame.h>
|
---|
| 68 | #include <mm/page.h>
|
---|
| 69 | #include <mm/slab.h>
|
---|
[303c94c] | 70 | #include <proc/thread.h>
|
---|
[4fded58] | 71 | #include <proc/task.h>
|
---|
[9aa72b4] | 72 | #include <genarch/mm/page_pt.h>
|
---|
| 73 | #include <genarch/mm/page_ht.h>
|
---|
[669f3d32] | 74 | #include <adt/cht.h>
|
---|
[9aa72b4] | 75 | #include <adt/hash_table.h>
|
---|
| 76 | #include <adt/list.h>
|
---|
| 77 | #include <arch.h>
|
---|
| 78 | #include <align.h>
|
---|
| 79 | #include <panic.h>
|
---|
| 80 | #include <errno.h>
|
---|
| 81 |
|
---|
| 82 | #define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */
|
---|
| 83 |
|
---|
[669f3d32] | 84 | /** Task specific pointer to a global kernel futex object. */
|
---|
| 85 | typedef struct futex_ptr {
|
---|
| 86 | /** CHT link. */
|
---|
| 87 | cht_link_t cht_link;
|
---|
| 88 | /** List of all futex pointers used by the task. */
|
---|
| 89 | link_t all_link;
|
---|
| 90 | /** Kernel futex object. */
|
---|
| 91 | futex_t *futex;
|
---|
| 92 | /** User space virtual address of the futex variable in the task. */
|
---|
| 93 | uintptr_t uaddr;
|
---|
| 94 | } futex_ptr_t;
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | static void destroy_task_cache(work_t *work);
|
---|
| 98 |
|
---|
| 99 | static void futex_initialize(futex_t *futex, uintptr_t paddr);
|
---|
| 100 | static void futex_add_ref(futex_t *futex);
|
---|
| 101 | static void futex_release_ref(futex_t *futex);
|
---|
| 102 | static void futex_release_ref_locked(futex_t *futex);
|
---|
| 103 |
|
---|
| 104 | static futex_t *get_futex(uintptr_t uaddr);
|
---|
| 105 | static futex_t *find_cached_futex(uintptr_t uaddr);
|
---|
| 106 | static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr);
|
---|
| 107 | static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *phys_addr);
|
---|
[9aa72b4] | 108 |
|
---|
[96b02eb9] | 109 | static size_t futex_ht_hash(sysarg_t *key);
|
---|
| 110 | static bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item);
|
---|
[9aa72b4] | 111 | static void futex_ht_remove_callback(link_t *item);
|
---|
| 112 |
|
---|
[669f3d32] | 113 | static size_t task_fut_ht_hash(const cht_link_t *link);
|
---|
| 114 | static size_t task_fut_ht_key_hash(void *key);
|
---|
| 115 | static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2);
|
---|
| 116 | static bool task_fut_ht_key_equal(void *key, const cht_link_t *item);
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | /** Mutex protecting the global futex hash table.
|
---|
| 120 | *
|
---|
| 121 | * Acquire task specific TASK->futex_list_lock before this mutex.
|
---|
[4fded58] | 122 | */
|
---|
[207e8880] | 123 | SPINLOCK_STATIC_INITIALIZE_NAME(futex_ht_lock, "futex-ht-lock");
|
---|
[9aa72b4] | 124 |
|
---|
[669f3d32] | 125 | /** Global kernel futex hash table. Lock futex_ht_lock before accessing.
|
---|
| 126 | *
|
---|
| 127 | * Physical address of the futex variable is the lookup key.
|
---|
| 128 | */
|
---|
[9aa72b4] | 129 | static hash_table_t futex_ht;
|
---|
| 130 |
|
---|
[669f3d32] | 131 | /** Global kernel futex hash table operations. */
|
---|
[9aa72b4] | 132 | static hash_table_operations_t futex_ht_ops = {
|
---|
| 133 | .hash = futex_ht_hash,
|
---|
| 134 | .compare = futex_ht_compare,
|
---|
| 135 | .remove_callback = futex_ht_remove_callback
|
---|
| 136 | };
|
---|
| 137 |
|
---|
[669f3d32] | 138 | /** Task futex cache CHT operations. */
|
---|
| 139 | static cht_ops_t task_futex_ht_ops = {
|
---|
| 140 | .hash = task_fut_ht_hash,
|
---|
| 141 | .key_hash = task_fut_ht_key_hash,
|
---|
| 142 | .equal = task_fut_ht_equal,
|
---|
| 143 | .key_equal = task_fut_ht_key_equal,
|
---|
| 144 | .remove_callback = NULL
|
---|
| 145 | };
|
---|
| 146 |
|
---|
[9aa72b4] | 147 | /** Initialize futex subsystem. */
|
---|
| 148 | void futex_init(void)
|
---|
| 149 | {
|
---|
| 150 | hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[669f3d32] | 153 | /** Initializes the futex structures for the new task. */
|
---|
| 154 | void futex_task_init(struct task *task)
|
---|
| 155 | {
|
---|
| 156 | task->futexes = malloc(sizeof(struct futex_cache), 0);
|
---|
| 157 |
|
---|
[c28413a9] | 158 | cht_create(&task->futexes->ht, 0, 0, 0, true, &task_futex_ht_ops);
|
---|
[3ac5086] | 159 |
|
---|
| 160 | list_initialize(&task->futexes->list);
|
---|
[207e8880] | 161 | spinlock_initialize(&task->futexes->list_lock, "futex-list-lock");
|
---|
[669f3d32] | 162 | }
|
---|
| 163 |
|
---|
| 164 | /** Destroys the futex structures for the dying task. */
|
---|
[3ac5086] | 165 | void futex_task_deinit(task_t *task)
|
---|
[669f3d32] | 166 | {
|
---|
| 167 | /* Interrupts are disabled so we must not block (cannot run cht_destroy). */
|
---|
| 168 | if (interrupts_disabled()) {
|
---|
| 169 | /* Invoke the blocking cht_destroy in the background. */
|
---|
| 170 | workq_global_enqueue_noblock(&task->futexes->destroy_work,
|
---|
| 171 | destroy_task_cache);
|
---|
| 172 | } else {
|
---|
| 173 | /* We can block. Invoke cht_destroy in this thread. */
|
---|
| 174 | destroy_task_cache(&task->futexes->destroy_work);
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /** Deallocates a task's CHT futex cache (must already be empty). */
|
---|
| 179 | static void destroy_task_cache(work_t *work)
|
---|
| 180 | {
|
---|
| 181 | struct futex_cache *cache =
|
---|
| 182 | member_to_inst(work, struct futex_cache, destroy_work);
|
---|
| 183 |
|
---|
[3ac5086] | 184 | /*
|
---|
| 185 | * Destroy the cache before manually freeing items of the cache in case
|
---|
| 186 | * table resize is in progress.
|
---|
| 187 | */
|
---|
| 188 | cht_destroy_unsafe(&cache->ht);
|
---|
| 189 |
|
---|
| 190 | /* Manually free futex_ptr cache items. */
|
---|
| 191 | list_foreach_safe(cache->list, cur_link, next_link) {
|
---|
| 192 | futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, all_link);
|
---|
| 193 |
|
---|
| 194 | list_remove(cur_link);
|
---|
| 195 | free(fut_ptr);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[669f3d32] | 198 | free(cache);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /** Remove references from futexes known to the current task. */
|
---|
| 202 | void futex_task_cleanup(void)
|
---|
| 203 | {
|
---|
[3ac5086] | 204 | struct futex_cache *futexes = TASK->futexes;
|
---|
| 205 |
|
---|
[669f3d32] | 206 | /* All threads of this task have terminated. This is the last thread. */
|
---|
[207e8880] | 207 | spinlock_lock(&futexes->list_lock);
|
---|
[669f3d32] | 208 |
|
---|
[3ac5086] | 209 | list_foreach_safe(futexes->list, cur_link, next_link) {
|
---|
| 210 | futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, all_link);
|
---|
| 211 |
|
---|
[669f3d32] | 212 | /*
|
---|
| 213 | * The function is free to free the futex. All other threads of this
|
---|
| 214 | * task have already terminated, so they have also definitely
|
---|
| 215 | * exited their CHT futex cache protecting rcu reader sections.
|
---|
| 216 | * Moreover release_ref() only frees the futex if this is the
|
---|
| 217 | * last task referencing the futex. Therefore, only threads
|
---|
| 218 | * of this task may have referenced the futex if it is to be freed.
|
---|
| 219 | */
|
---|
| 220 | futex_release_ref_locked(fut_ptr->futex);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[207e8880] | 223 | spinlock_unlock(&futexes->list_lock);
|
---|
[669f3d32] | 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | /** Initialize the kernel futex structure.
|
---|
[9aa72b4] | 228 | *
|
---|
[669f3d32] | 229 | * @param futex Kernel futex structure.
|
---|
| 230 | * @param paddr Physical address of the futex variable.
|
---|
[9aa72b4] | 231 | */
|
---|
[669f3d32] | 232 | static void futex_initialize(futex_t *futex, uintptr_t paddr)
|
---|
[9aa72b4] | 233 | {
|
---|
| 234 | waitq_initialize(&futex->wq);
|
---|
| 235 | link_initialize(&futex->ht_link);
|
---|
[669f3d32] | 236 | futex->paddr = paddr;
|
---|
[4fded58] | 237 | futex->refcount = 1;
|
---|
[9aa72b4] | 238 | }
|
---|
| 239 |
|
---|
[669f3d32] | 240 | /** Increments the counter of tasks referencing the futex. */
|
---|
| 241 | static void futex_add_ref(futex_t *futex)
|
---|
| 242 | {
|
---|
[207e8880] | 243 | ASSERT(spinlock_locked(&futex_ht_lock));
|
---|
[669f3d32] | 244 | ASSERT(0 < futex->refcount);
|
---|
| 245 | ++futex->refcount;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | /** Decrements the counter of tasks referencing the futex. May free the futex.*/
|
---|
| 249 | static void futex_release_ref(futex_t *futex)
|
---|
| 250 | {
|
---|
[207e8880] | 251 | ASSERT(spinlock_locked(&futex_ht_lock));
|
---|
[669f3d32] | 252 | ASSERT(0 < futex->refcount);
|
---|
| 253 |
|
---|
| 254 | --futex->refcount;
|
---|
| 255 |
|
---|
| 256 | if (0 == futex->refcount) {
|
---|
| 257 | hash_table_remove(&futex_ht, &futex->paddr, 1);
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /** Decrements the counter of tasks referencing the futex. May free the futex.*/
|
---|
| 262 | static void futex_release_ref_locked(futex_t *futex)
|
---|
| 263 | {
|
---|
[207e8880] | 264 | spinlock_lock(&futex_ht_lock);
|
---|
[669f3d32] | 265 | futex_release_ref(futex);
|
---|
[207e8880] | 266 | spinlock_unlock(&futex_ht_lock);
|
---|
[669f3d32] | 267 | }
|
---|
| 268 |
|
---|
| 269 | /** Returns a futex for the virtual address @a uaddr (or creates one). */
|
---|
| 270 | static futex_t *get_futex(uintptr_t uaddr)
|
---|
| 271 | {
|
---|
| 272 | futex_t *futex = find_cached_futex(uaddr);
|
---|
| 273 |
|
---|
| 274 | if (futex)
|
---|
| 275 | return futex;
|
---|
| 276 |
|
---|
| 277 | uintptr_t paddr;
|
---|
| 278 |
|
---|
[207e8880] | 279 | if (!find_futex_paddr(uaddr, &paddr)) {
|
---|
[669f3d32] | 280 | return 0;
|
---|
[207e8880] | 281 | }
|
---|
[669f3d32] | 282 |
|
---|
| 283 | return get_and_cache_futex(paddr, uaddr);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 |
|
---|
| 287 | /** Finds the physical address of the futex variable. */
|
---|
| 288 | static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *paddr)
|
---|
| 289 | {
|
---|
[207e8880] | 290 | page_table_lock(AS, false);
|
---|
[759ea0d] | 291 | spinlock_lock(&futex_ht_lock);
|
---|
[669f3d32] | 292 |
|
---|
| 293 | bool found = false;
|
---|
[207e8880] | 294 | pte_t *t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), true);
|
---|
[669f3d32] | 295 |
|
---|
| 296 | if (t && PTE_VALID(t) && PTE_PRESENT(t)) {
|
---|
| 297 | found = true;
|
---|
| 298 | *paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[207e8880] | 301 | spinlock_unlock(&futex_ht_lock);
|
---|
[759ea0d] | 302 | page_table_unlock(AS, false);
|
---|
[669f3d32] | 303 |
|
---|
| 304 | return found;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | /** Returns the futex cached in this task with the virtual address uaddr. */
|
---|
| 308 | static futex_t *find_cached_futex(uintptr_t uaddr)
|
---|
| 309 | {
|
---|
| 310 | cht_read_lock();
|
---|
| 311 |
|
---|
| 312 | futex_t *futex;
|
---|
[c0c26ac] | 313 | cht_link_t *futex_ptr_link = cht_find_lazy(&TASK->futexes->ht, &uaddr);
|
---|
[669f3d32] | 314 |
|
---|
| 315 | if (futex_ptr_link) {
|
---|
| 316 | futex_ptr_t *futex_ptr
|
---|
| 317 | = member_to_inst(futex_ptr_link, futex_ptr_t, cht_link);
|
---|
| 318 |
|
---|
| 319 | futex = futex_ptr->futex;
|
---|
| 320 | } else {
|
---|
| 321 | futex = NULL;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | cht_read_unlock();
|
---|
| 325 |
|
---|
| 326 | return futex;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 |
|
---|
| 330 | /**
|
---|
| 331 | * Returns a kernel futex for the physical address @a phys_addr and caches
|
---|
| 332 | * it in this task under the virtual address @a uaddr (if not already cached).
|
---|
| 333 | */
|
---|
| 334 | static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr)
|
---|
| 335 | {
|
---|
| 336 | futex_t *futex = malloc(sizeof(futex_t), 0);
|
---|
| 337 |
|
---|
| 338 | /*
|
---|
| 339 | * Find the futex object in the global futex table (or insert it
|
---|
| 340 | * if it is not present).
|
---|
| 341 | */
|
---|
[207e8880] | 342 | spinlock_lock(&futex_ht_lock);
|
---|
[669f3d32] | 343 |
|
---|
| 344 | link_t *fut_link = hash_table_find(&futex_ht, &phys_addr);
|
---|
| 345 |
|
---|
| 346 | if (fut_link) {
|
---|
| 347 | free(futex);
|
---|
| 348 | futex = member_to_inst(fut_link, futex_t, ht_link);
|
---|
| 349 | futex_add_ref(futex);
|
---|
| 350 | } else {
|
---|
| 351 | futex_initialize(futex, phys_addr);
|
---|
| 352 | hash_table_insert(&futex_ht, &phys_addr, &futex->ht_link);
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[207e8880] | 355 | spinlock_unlock(&futex_ht_lock);
|
---|
[669f3d32] | 356 |
|
---|
| 357 | /*
|
---|
| 358 | * Cache the link to the futex object for this task.
|
---|
| 359 | */
|
---|
| 360 | futex_ptr_t *fut_ptr = malloc(sizeof(futex_ptr_t), 0);
|
---|
| 361 | cht_link_t *dup_link;
|
---|
| 362 |
|
---|
| 363 | fut_ptr->futex = futex;
|
---|
| 364 | fut_ptr->uaddr = uaddr;
|
---|
| 365 |
|
---|
| 366 | cht_read_lock();
|
---|
| 367 |
|
---|
| 368 | /* Cache the mapping from the virtual address to the futex for this task. */
|
---|
| 369 | if (cht_insert_unique(&TASK->futexes->ht, &fut_ptr->cht_link, &dup_link)) {
|
---|
[207e8880] | 370 | spinlock_lock(&TASK->futexes->list_lock);
|
---|
[3ac5086] | 371 | list_append(&fut_ptr->all_link, &TASK->futexes->list);
|
---|
[207e8880] | 372 | spinlock_unlock(&TASK->futexes->list_lock);
|
---|
[669f3d32] | 373 | } else {
|
---|
| 374 | /* Another thread of this task beat us to it. Use that mapping instead.*/
|
---|
| 375 | free(fut_ptr);
|
---|
| 376 | futex_release_ref_locked(futex);
|
---|
| 377 |
|
---|
| 378 | futex_ptr_t *dup = member_to_inst(dup_link, futex_ptr_t, cht_link);
|
---|
| 379 | futex = dup->futex;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | cht_read_unlock();
|
---|
| 383 |
|
---|
| 384 | return futex;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
[9aa72b4] | 387 | /** Sleep in futex wait queue.
|
---|
| 388 | *
|
---|
[4774a32] | 389 | * @param uaddr Userspace address of the futex counter.
|
---|
[9aa72b4] | 390 | *
|
---|
[4774a32] | 391 | * @return If there is no physical mapping for uaddr ENOENT is
|
---|
| 392 | * returned. Otherwise returns a wait result as defined in
|
---|
| 393 | * synch.h.
|
---|
[9aa72b4] | 394 | */
|
---|
[96b02eb9] | 395 | sysarg_t sys_futex_sleep(uintptr_t uaddr)
|
---|
[9aa72b4] | 396 | {
|
---|
[669f3d32] | 397 | futex_t *futex = get_futex(uaddr);
|
---|
[9aa72b4] | 398 |
|
---|
[669f3d32] | 399 | if (!futex)
|
---|
[96b02eb9] | 400 | return (sysarg_t) ENOENT;
|
---|
[741fd16] | 401 |
|
---|
[496232e] | 402 | #ifdef CONFIG_UDEBUG
|
---|
| 403 | udebug_stoppable_begin();
|
---|
| 404 | #endif
|
---|
| 405 |
|
---|
[669f3d32] | 406 | int rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
[207e8880] | 407 |
|
---|
[496232e] | 408 | #ifdef CONFIG_UDEBUG
|
---|
| 409 | udebug_stoppable_end();
|
---|
| 410 | #endif
|
---|
| 411 |
|
---|
[96b02eb9] | 412 | return (sysarg_t) rc;
|
---|
[9aa72b4] | 413 | }
|
---|
| 414 |
|
---|
| 415 | /** Wakeup one thread waiting in futex wait queue.
|
---|
| 416 | *
|
---|
[4774a32] | 417 | * @param uaddr Userspace address of the futex counter.
|
---|
[9aa72b4] | 418 | *
|
---|
[4774a32] | 419 | * @return ENOENT if there is no physical mapping for uaddr.
|
---|
[9aa72b4] | 420 | */
|
---|
[96b02eb9] | 421 | sysarg_t sys_futex_wakeup(uintptr_t uaddr)
|
---|
[9aa72b4] | 422 | {
|
---|
[669f3d32] | 423 | futex_t *futex = get_futex(uaddr);
|
---|
[9aa72b4] | 424 |
|
---|
[669f3d32] | 425 | if (futex) {
|
---|
| 426 | waitq_wakeup(&futex->wq, WAKEUP_FIRST);
|
---|
| 427 | return 0;
|
---|
| 428 | } else {
|
---|
[96b02eb9] | 429 | return (sysarg_t) ENOENT;
|
---|
[9aa72b4] | 430 | }
|
---|
[a9ef68b] | 431 | }
|
---|
| 432 |
|
---|
[9aa72b4] | 433 |
|
---|
| 434 | /** Compute hash index into futex hash table.
|
---|
| 435 | *
|
---|
[4774a32] | 436 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 437 | * counter) is stored.
|
---|
[9aa72b4] | 438 | *
|
---|
[4774a32] | 439 | * @return Index into futex hash table.
|
---|
[9aa72b4] | 440 | */
|
---|
[96b02eb9] | 441 | size_t futex_ht_hash(sysarg_t *key)
|
---|
[9aa72b4] | 442 | {
|
---|
[98000fb] | 443 | return (*key & (FUTEX_HT_SIZE - 1));
|
---|
[9aa72b4] | 444 | }
|
---|
| 445 |
|
---|
| 446 | /** Compare futex hash table item with a key.
|
---|
| 447 | *
|
---|
[4774a32] | 448 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 449 | * counter) is stored.
|
---|
[9aa72b4] | 450 | *
|
---|
[4774a32] | 451 | * @return True if the item matches the key. False otherwise.
|
---|
[9aa72b4] | 452 | */
|
---|
[96b02eb9] | 453 | bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item)
|
---|
[9aa72b4] | 454 | {
|
---|
| 455 | futex_t *futex;
|
---|
| 456 |
|
---|
| 457 | ASSERT(keys == 1);
|
---|
| 458 |
|
---|
| 459 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 460 | return *key == futex->paddr;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /** Callback for removal items from futex hash table.
|
---|
| 464 | *
|
---|
[4774a32] | 465 | * @param item Item removed from the hash table.
|
---|
[9aa72b4] | 466 | */
|
---|
| 467 | void futex_ht_remove_callback(link_t *item)
|
---|
| 468 | {
|
---|
| 469 | futex_t *futex;
|
---|
| 470 |
|
---|
| 471 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 472 | free(futex);
|
---|
| 473 | }
|
---|
[e090e1bc] | 474 |
|
---|
[669f3d32] | 475 | /*
|
---|
| 476 | * Operations of a task's CHT that caches mappings of futex user space
|
---|
| 477 | * virtual addresses to kernel futex objects.
|
---|
| 478 | */
|
---|
| 479 |
|
---|
| 480 | static size_t task_fut_ht_hash(const cht_link_t *link)
|
---|
[e090e1bc] | 481 | {
|
---|
[669f3d32] | 482 | const futex_ptr_t *fut_ptr = member_to_inst(link, futex_ptr_t, cht_link);
|
---|
| 483 | return fut_ptr->uaddr;
|
---|
| 484 | }
|
---|
[9c1c6771] | 485 |
|
---|
[669f3d32] | 486 | static size_t task_fut_ht_key_hash(void *key)
|
---|
| 487 | {
|
---|
| 488 | return *(uintptr_t*)key;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2)
|
---|
| 492 | {
|
---|
| 493 | const futex_ptr_t *fut_ptr1 = member_to_inst(item1, futex_ptr_t, cht_link);
|
---|
| 494 | const futex_ptr_t *fut_ptr2 = member_to_inst(item2, futex_ptr_t, cht_link);
|
---|
[9c1c6771] | 495 |
|
---|
[669f3d32] | 496 | return fut_ptr1->uaddr == fut_ptr2->uaddr;
|
---|
[e090e1bc] | 497 | }
|
---|
[b45c443] | 498 |
|
---|
[669f3d32] | 499 | static bool task_fut_ht_key_equal(void *key, const cht_link_t *item)
|
---|
| 500 | {
|
---|
| 501 | const futex_ptr_t *fut_ptr = member_to_inst(item, futex_ptr_t, cht_link);
|
---|
| 502 | uintptr_t uaddr = *(uintptr_t*)key;
|
---|
[9c1c6771] | 503 |
|
---|
[669f3d32] | 504 | return fut_ptr->uaddr == uaddr;
|
---|
[e090e1bc] | 505 | }
|
---|
[b45c443] | 506 |
|
---|
[cc73a8a1] | 507 | /** @}
|
---|
[b45c443] | 508 | */
|
---|