[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 |
|
---|
[63e27ef] | 63 | #include <assert.h>
|
---|
[9aa72b4] | 64 | #include <synch/futex.h>
|
---|
[ee42e43] | 65 | #include <synch/mutex.h>
|
---|
[303c94c] | 66 | #include <synch/spinlock.h>
|
---|
[669f3d32] | 67 | #include <synch/rcu.h>
|
---|
[9aa72b4] | 68 | #include <mm/frame.h>
|
---|
| 69 | #include <mm/page.h>
|
---|
| 70 | #include <mm/slab.h>
|
---|
[303c94c] | 71 | #include <proc/thread.h>
|
---|
[4fded58] | 72 | #include <proc/task.h>
|
---|
[9aa72b4] | 73 | #include <genarch/mm/page_pt.h>
|
---|
| 74 | #include <genarch/mm/page_ht.h>
|
---|
[669f3d32] | 75 | #include <adt/cht.h>
|
---|
[9aa72b4] | 76 | #include <adt/hash_table.h>
|
---|
| 77 | #include <adt/list.h>
|
---|
| 78 | #include <arch.h>
|
---|
| 79 | #include <align.h>
|
---|
| 80 | #include <panic.h>
|
---|
| 81 | #include <errno.h>
|
---|
| 82 |
|
---|
| 83 | #define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */
|
---|
| 84 |
|
---|
[669f3d32] | 85 | /** Task specific pointer to a global kernel futex object. */
|
---|
| 86 | typedef struct futex_ptr {
|
---|
| 87 | /** CHT link. */
|
---|
| 88 | cht_link_t cht_link;
|
---|
| 89 | /** List of all futex pointers used by the task. */
|
---|
| 90 | link_t all_link;
|
---|
| 91 | /** Kernel futex object. */
|
---|
| 92 | futex_t *futex;
|
---|
| 93 | /** User space virtual address of the futex variable in the task. */
|
---|
| 94 | uintptr_t uaddr;
|
---|
| 95 | } futex_ptr_t;
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | static void destroy_task_cache(work_t *work);
|
---|
| 99 |
|
---|
| 100 | static void futex_initialize(futex_t *futex, uintptr_t paddr);
|
---|
| 101 | static void futex_add_ref(futex_t *futex);
|
---|
| 102 | static void futex_release_ref(futex_t *futex);
|
---|
| 103 | static void futex_release_ref_locked(futex_t *futex);
|
---|
| 104 |
|
---|
| 105 | static futex_t *get_futex(uintptr_t uaddr);
|
---|
| 106 | static futex_t *find_cached_futex(uintptr_t uaddr);
|
---|
| 107 | static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr);
|
---|
| 108 | static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *phys_addr);
|
---|
[9aa72b4] | 109 |
|
---|
[96b02eb9] | 110 | static size_t futex_ht_hash(sysarg_t *key);
|
---|
| 111 | static bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item);
|
---|
[9aa72b4] | 112 | static void futex_ht_remove_callback(link_t *item);
|
---|
| 113 |
|
---|
[669f3d32] | 114 | static size_t task_fut_ht_hash(const cht_link_t *link);
|
---|
| 115 | static size_t task_fut_ht_key_hash(void *key);
|
---|
| 116 | static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2);
|
---|
| 117 | static bool task_fut_ht_key_equal(void *key, const cht_link_t *item);
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | /** Mutex protecting the global futex hash table.
|
---|
| 121 | *
|
---|
| 122 | * Acquire task specific TASK->futex_list_lock before this mutex.
|
---|
[4fded58] | 123 | */
|
---|
[207e8880] | 124 | SPINLOCK_STATIC_INITIALIZE_NAME(futex_ht_lock, "futex-ht-lock");
|
---|
[9aa72b4] | 125 |
|
---|
[669f3d32] | 126 | /** Global kernel futex hash table. Lock futex_ht_lock before accessing.
|
---|
| 127 | *
|
---|
| 128 | * Physical address of the futex variable is the lookup key.
|
---|
| 129 | */
|
---|
[9aa72b4] | 130 | static hash_table_t futex_ht;
|
---|
| 131 |
|
---|
[669f3d32] | 132 | /** Global kernel futex hash table operations. */
|
---|
[9aa72b4] | 133 | static hash_table_operations_t futex_ht_ops = {
|
---|
| 134 | .hash = futex_ht_hash,
|
---|
| 135 | .compare = futex_ht_compare,
|
---|
| 136 | .remove_callback = futex_ht_remove_callback
|
---|
| 137 | };
|
---|
| 138 |
|
---|
[669f3d32] | 139 | /** Task futex cache CHT operations. */
|
---|
| 140 | static cht_ops_t task_futex_ht_ops = {
|
---|
| 141 | .hash = task_fut_ht_hash,
|
---|
| 142 | .key_hash = task_fut_ht_key_hash,
|
---|
| 143 | .equal = task_fut_ht_equal,
|
---|
| 144 | .key_equal = task_fut_ht_key_equal,
|
---|
| 145 | .remove_callback = NULL
|
---|
| 146 | };
|
---|
| 147 |
|
---|
[9aa72b4] | 148 | /** Initialize futex subsystem. */
|
---|
| 149 | void futex_init(void)
|
---|
| 150 | {
|
---|
| 151 | hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[669f3d32] | 154 | /** Initializes the futex structures for the new task. */
|
---|
| 155 | void futex_task_init(struct task *task)
|
---|
| 156 | {
|
---|
| 157 | task->futexes = malloc(sizeof(struct futex_cache), 0);
|
---|
| 158 |
|
---|
[c28413a9] | 159 | cht_create(&task->futexes->ht, 0, 0, 0, true, &task_futex_ht_ops);
|
---|
[3ac5086] | 160 |
|
---|
| 161 | list_initialize(&task->futexes->list);
|
---|
[207e8880] | 162 | spinlock_initialize(&task->futexes->list_lock, "futex-list-lock");
|
---|
[669f3d32] | 163 | }
|
---|
| 164 |
|
---|
| 165 | /** Destroys the futex structures for the dying task. */
|
---|
[3ac5086] | 166 | void futex_task_deinit(task_t *task)
|
---|
[669f3d32] | 167 | {
|
---|
| 168 | /* Interrupts are disabled so we must not block (cannot run cht_destroy). */
|
---|
| 169 | if (interrupts_disabled()) {
|
---|
| 170 | /* Invoke the blocking cht_destroy in the background. */
|
---|
| 171 | workq_global_enqueue_noblock(&task->futexes->destroy_work,
|
---|
| 172 | destroy_task_cache);
|
---|
| 173 | } else {
|
---|
| 174 | /* We can block. Invoke cht_destroy in this thread. */
|
---|
| 175 | destroy_task_cache(&task->futexes->destroy_work);
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /** Deallocates a task's CHT futex cache (must already be empty). */
|
---|
| 180 | static void destroy_task_cache(work_t *work)
|
---|
| 181 | {
|
---|
| 182 | struct futex_cache *cache =
|
---|
| 183 | member_to_inst(work, struct futex_cache, destroy_work);
|
---|
| 184 |
|
---|
[3ac5086] | 185 | /*
|
---|
| 186 | * Destroy the cache before manually freeing items of the cache in case
|
---|
| 187 | * table resize is in progress.
|
---|
| 188 | */
|
---|
| 189 | cht_destroy_unsafe(&cache->ht);
|
---|
| 190 |
|
---|
| 191 | /* Manually free futex_ptr cache items. */
|
---|
| 192 | list_foreach_safe(cache->list, cur_link, next_link) {
|
---|
| 193 | futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, all_link);
|
---|
| 194 |
|
---|
| 195 | list_remove(cur_link);
|
---|
| 196 | free(fut_ptr);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[669f3d32] | 199 | free(cache);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /** Remove references from futexes known to the current task. */
|
---|
| 203 | void futex_task_cleanup(void)
|
---|
| 204 | {
|
---|
[3ac5086] | 205 | struct futex_cache *futexes = TASK->futexes;
|
---|
| 206 |
|
---|
[669f3d32] | 207 | /* All threads of this task have terminated. This is the last thread. */
|
---|
[207e8880] | 208 | spinlock_lock(&futexes->list_lock);
|
---|
[669f3d32] | 209 |
|
---|
[3ac5086] | 210 | list_foreach_safe(futexes->list, cur_link, next_link) {
|
---|
| 211 | futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, all_link);
|
---|
| 212 |
|
---|
[669f3d32] | 213 | /*
|
---|
| 214 | * The function is free to free the futex. All other threads of this
|
---|
| 215 | * task have already terminated, so they have also definitely
|
---|
| 216 | * exited their CHT futex cache protecting rcu reader sections.
|
---|
| 217 | * Moreover release_ref() only frees the futex if this is the
|
---|
| 218 | * last task referencing the futex. Therefore, only threads
|
---|
| 219 | * of this task may have referenced the futex if it is to be freed.
|
---|
| 220 | */
|
---|
| 221 | futex_release_ref_locked(fut_ptr->futex);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[207e8880] | 224 | spinlock_unlock(&futexes->list_lock);
|
---|
[669f3d32] | 225 | }
|
---|
| 226 |
|
---|
| 227 |
|
---|
| 228 | /** Initialize the kernel futex structure.
|
---|
[9aa72b4] | 229 | *
|
---|
[669f3d32] | 230 | * @param futex Kernel futex structure.
|
---|
| 231 | * @param paddr Physical address of the futex variable.
|
---|
[9aa72b4] | 232 | */
|
---|
[669f3d32] | 233 | static void futex_initialize(futex_t *futex, uintptr_t paddr)
|
---|
[9aa72b4] | 234 | {
|
---|
| 235 | waitq_initialize(&futex->wq);
|
---|
| 236 | link_initialize(&futex->ht_link);
|
---|
[669f3d32] | 237 | futex->paddr = paddr;
|
---|
[4fded58] | 238 | futex->refcount = 1;
|
---|
[9aa72b4] | 239 | }
|
---|
| 240 |
|
---|
[669f3d32] | 241 | /** Increments the counter of tasks referencing the futex. */
|
---|
| 242 | static void futex_add_ref(futex_t *futex)
|
---|
| 243 | {
|
---|
[63e27ef] | 244 | assert(spinlock_locked(&futex_ht_lock));
|
---|
| 245 | assert(0 < futex->refcount);
|
---|
[669f3d32] | 246 | ++futex->refcount;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | /** Decrements the counter of tasks referencing the futex. May free the futex.*/
|
---|
| 250 | static void futex_release_ref(futex_t *futex)
|
---|
| 251 | {
|
---|
[63e27ef] | 252 | assert(spinlock_locked(&futex_ht_lock));
|
---|
| 253 | assert(0 < futex->refcount);
|
---|
[669f3d32] | 254 |
|
---|
| 255 | --futex->refcount;
|
---|
| 256 |
|
---|
| 257 | if (0 == futex->refcount) {
|
---|
| 258 | hash_table_remove(&futex_ht, &futex->paddr, 1);
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /** Decrements the counter of tasks referencing the futex. May free the futex.*/
|
---|
| 263 | static void futex_release_ref_locked(futex_t *futex)
|
---|
| 264 | {
|
---|
[207e8880] | 265 | spinlock_lock(&futex_ht_lock);
|
---|
[669f3d32] | 266 | futex_release_ref(futex);
|
---|
[207e8880] | 267 | spinlock_unlock(&futex_ht_lock);
|
---|
[669f3d32] | 268 | }
|
---|
| 269 |
|
---|
| 270 | /** Returns a futex for the virtual address @a uaddr (or creates one). */
|
---|
| 271 | static futex_t *get_futex(uintptr_t uaddr)
|
---|
| 272 | {
|
---|
| 273 | futex_t *futex = find_cached_futex(uaddr);
|
---|
| 274 |
|
---|
| 275 | if (futex)
|
---|
| 276 | return futex;
|
---|
| 277 |
|
---|
| 278 | uintptr_t paddr;
|
---|
| 279 |
|
---|
[207e8880] | 280 | if (!find_futex_paddr(uaddr, &paddr)) {
|
---|
[669f3d32] | 281 | return 0;
|
---|
[207e8880] | 282 | }
|
---|
[669f3d32] | 283 |
|
---|
| 284 | return get_and_cache_futex(paddr, uaddr);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 | /** Finds the physical address of the futex variable. */
|
---|
| 289 | static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *paddr)
|
---|
| 290 | {
|
---|
[207e8880] | 291 | page_table_lock(AS, false);
|
---|
[759ea0d] | 292 | spinlock_lock(&futex_ht_lock);
|
---|
[669f3d32] | 293 |
|
---|
[38dc82d] | 294 | bool success = false;
|
---|
| 295 |
|
---|
| 296 | pte_t t;
|
---|
| 297 | bool found;
|
---|
| 298 |
|
---|
| 299 | found = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), true, &t);
|
---|
| 300 | if (found && PTE_VALID(&t) && PTE_PRESENT(&t)) {
|
---|
| 301 | success = true;
|
---|
| 302 | *paddr = PTE_GET_FRAME(&t) +
|
---|
| 303 | (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
[669f3d32] | 304 | }
|
---|
| 305 |
|
---|
[207e8880] | 306 | spinlock_unlock(&futex_ht_lock);
|
---|
[759ea0d] | 307 | page_table_unlock(AS, false);
|
---|
[669f3d32] | 308 |
|
---|
[38dc82d] | 309 | return success;
|
---|
[669f3d32] | 310 | }
|
---|
| 311 |
|
---|
| 312 | /** Returns the futex cached in this task with the virtual address uaddr. */
|
---|
| 313 | static futex_t *find_cached_futex(uintptr_t uaddr)
|
---|
| 314 | {
|
---|
| 315 | cht_read_lock();
|
---|
| 316 |
|
---|
| 317 | futex_t *futex;
|
---|
[c0c26ac] | 318 | cht_link_t *futex_ptr_link = cht_find_lazy(&TASK->futexes->ht, &uaddr);
|
---|
[669f3d32] | 319 |
|
---|
| 320 | if (futex_ptr_link) {
|
---|
| 321 | futex_ptr_t *futex_ptr
|
---|
| 322 | = member_to_inst(futex_ptr_link, futex_ptr_t, cht_link);
|
---|
| 323 |
|
---|
| 324 | futex = futex_ptr->futex;
|
---|
| 325 | } else {
|
---|
| 326 | futex = NULL;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | cht_read_unlock();
|
---|
| 330 |
|
---|
| 331 | return futex;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 |
|
---|
| 335 | /**
|
---|
| 336 | * Returns a kernel futex for the physical address @a phys_addr and caches
|
---|
| 337 | * it in this task under the virtual address @a uaddr (if not already cached).
|
---|
| 338 | */
|
---|
| 339 | static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr)
|
---|
| 340 | {
|
---|
| 341 | futex_t *futex = malloc(sizeof(futex_t), 0);
|
---|
| 342 |
|
---|
| 343 | /*
|
---|
| 344 | * Find the futex object in the global futex table (or insert it
|
---|
| 345 | * if it is not present).
|
---|
| 346 | */
|
---|
[207e8880] | 347 | spinlock_lock(&futex_ht_lock);
|
---|
[669f3d32] | 348 |
|
---|
| 349 | link_t *fut_link = hash_table_find(&futex_ht, &phys_addr);
|
---|
| 350 |
|
---|
| 351 | if (fut_link) {
|
---|
| 352 | free(futex);
|
---|
| 353 | futex = member_to_inst(fut_link, futex_t, ht_link);
|
---|
| 354 | futex_add_ref(futex);
|
---|
| 355 | } else {
|
---|
| 356 | futex_initialize(futex, phys_addr);
|
---|
| 357 | hash_table_insert(&futex_ht, &phys_addr, &futex->ht_link);
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[207e8880] | 360 | spinlock_unlock(&futex_ht_lock);
|
---|
[669f3d32] | 361 |
|
---|
| 362 | /*
|
---|
| 363 | * Cache the link to the futex object for this task.
|
---|
| 364 | */
|
---|
| 365 | futex_ptr_t *fut_ptr = malloc(sizeof(futex_ptr_t), 0);
|
---|
| 366 | cht_link_t *dup_link;
|
---|
| 367 |
|
---|
| 368 | fut_ptr->futex = futex;
|
---|
| 369 | fut_ptr->uaddr = uaddr;
|
---|
| 370 |
|
---|
| 371 | cht_read_lock();
|
---|
| 372 |
|
---|
| 373 | /* Cache the mapping from the virtual address to the futex for this task. */
|
---|
| 374 | if (cht_insert_unique(&TASK->futexes->ht, &fut_ptr->cht_link, &dup_link)) {
|
---|
[207e8880] | 375 | spinlock_lock(&TASK->futexes->list_lock);
|
---|
[3ac5086] | 376 | list_append(&fut_ptr->all_link, &TASK->futexes->list);
|
---|
[207e8880] | 377 | spinlock_unlock(&TASK->futexes->list_lock);
|
---|
[669f3d32] | 378 | } else {
|
---|
| 379 | /* Another thread of this task beat us to it. Use that mapping instead.*/
|
---|
| 380 | free(fut_ptr);
|
---|
| 381 | futex_release_ref_locked(futex);
|
---|
| 382 |
|
---|
| 383 | futex_ptr_t *dup = member_to_inst(dup_link, futex_ptr_t, cht_link);
|
---|
| 384 | futex = dup->futex;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | cht_read_unlock();
|
---|
| 388 |
|
---|
| 389 | return futex;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
[9aa72b4] | 392 | /** Sleep in futex wait queue.
|
---|
| 393 | *
|
---|
[4774a32] | 394 | * @param uaddr Userspace address of the futex counter.
|
---|
[9aa72b4] | 395 | *
|
---|
[4774a32] | 396 | * @return If there is no physical mapping for uaddr ENOENT is
|
---|
| 397 | * returned. Otherwise returns a wait result as defined in
|
---|
| 398 | * synch.h.
|
---|
[9aa72b4] | 399 | */
|
---|
[96b02eb9] | 400 | sysarg_t sys_futex_sleep(uintptr_t uaddr)
|
---|
[9aa72b4] | 401 | {
|
---|
[669f3d32] | 402 | futex_t *futex = get_futex(uaddr);
|
---|
[9aa72b4] | 403 |
|
---|
[669f3d32] | 404 | if (!futex)
|
---|
[96b02eb9] | 405 | return (sysarg_t) ENOENT;
|
---|
[741fd16] | 406 |
|
---|
[496232e] | 407 | #ifdef CONFIG_UDEBUG
|
---|
| 408 | udebug_stoppable_begin();
|
---|
| 409 | #endif
|
---|
| 410 |
|
---|
[669f3d32] | 411 | int rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
[207e8880] | 412 |
|
---|
[496232e] | 413 | #ifdef CONFIG_UDEBUG
|
---|
| 414 | udebug_stoppable_end();
|
---|
| 415 | #endif
|
---|
| 416 |
|
---|
[96b02eb9] | 417 | return (sysarg_t) rc;
|
---|
[9aa72b4] | 418 | }
|
---|
| 419 |
|
---|
| 420 | /** Wakeup one thread waiting in futex wait queue.
|
---|
| 421 | *
|
---|
[4774a32] | 422 | * @param uaddr Userspace address of the futex counter.
|
---|
[9aa72b4] | 423 | *
|
---|
[4774a32] | 424 | * @return ENOENT if there is no physical mapping for uaddr.
|
---|
[9aa72b4] | 425 | */
|
---|
[96b02eb9] | 426 | sysarg_t sys_futex_wakeup(uintptr_t uaddr)
|
---|
[9aa72b4] | 427 | {
|
---|
[669f3d32] | 428 | futex_t *futex = get_futex(uaddr);
|
---|
[9aa72b4] | 429 |
|
---|
[669f3d32] | 430 | if (futex) {
|
---|
| 431 | waitq_wakeup(&futex->wq, WAKEUP_FIRST);
|
---|
| 432 | return 0;
|
---|
| 433 | } else {
|
---|
[96b02eb9] | 434 | return (sysarg_t) ENOENT;
|
---|
[9aa72b4] | 435 | }
|
---|
[a9ef68b] | 436 | }
|
---|
| 437 |
|
---|
[9aa72b4] | 438 |
|
---|
| 439 | /** Compute hash index into futex hash table.
|
---|
| 440 | *
|
---|
[4774a32] | 441 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 442 | * counter) is stored.
|
---|
[9aa72b4] | 443 | *
|
---|
[4774a32] | 444 | * @return Index into futex hash table.
|
---|
[9aa72b4] | 445 | */
|
---|
[96b02eb9] | 446 | size_t futex_ht_hash(sysarg_t *key)
|
---|
[9aa72b4] | 447 | {
|
---|
[98000fb] | 448 | return (*key & (FUTEX_HT_SIZE - 1));
|
---|
[9aa72b4] | 449 | }
|
---|
| 450 |
|
---|
| 451 | /** Compare futex hash table item with a key.
|
---|
| 452 | *
|
---|
[4774a32] | 453 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 454 | * counter) is stored.
|
---|
[9aa72b4] | 455 | *
|
---|
[4774a32] | 456 | * @return True if the item matches the key. False otherwise.
|
---|
[9aa72b4] | 457 | */
|
---|
[96b02eb9] | 458 | bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item)
|
---|
[9aa72b4] | 459 | {
|
---|
| 460 | futex_t *futex;
|
---|
| 461 |
|
---|
[63e27ef] | 462 | assert(keys == 1);
|
---|
[9aa72b4] | 463 |
|
---|
| 464 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 465 | return *key == futex->paddr;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | /** Callback for removal items from futex hash table.
|
---|
| 469 | *
|
---|
[4774a32] | 470 | * @param item Item removed from the hash table.
|
---|
[9aa72b4] | 471 | */
|
---|
| 472 | void futex_ht_remove_callback(link_t *item)
|
---|
| 473 | {
|
---|
| 474 | futex_t *futex;
|
---|
| 475 |
|
---|
| 476 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 477 | free(futex);
|
---|
| 478 | }
|
---|
[e090e1bc] | 479 |
|
---|
[669f3d32] | 480 | /*
|
---|
| 481 | * Operations of a task's CHT that caches mappings of futex user space
|
---|
| 482 | * virtual addresses to kernel futex objects.
|
---|
| 483 | */
|
---|
| 484 |
|
---|
| 485 | static size_t task_fut_ht_hash(const cht_link_t *link)
|
---|
[e090e1bc] | 486 | {
|
---|
[669f3d32] | 487 | const futex_ptr_t *fut_ptr = member_to_inst(link, futex_ptr_t, cht_link);
|
---|
| 488 | return fut_ptr->uaddr;
|
---|
| 489 | }
|
---|
[9c1c6771] | 490 |
|
---|
[669f3d32] | 491 | static size_t task_fut_ht_key_hash(void *key)
|
---|
| 492 | {
|
---|
| 493 | return *(uintptr_t*)key;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2)
|
---|
| 497 | {
|
---|
| 498 | const futex_ptr_t *fut_ptr1 = member_to_inst(item1, futex_ptr_t, cht_link);
|
---|
| 499 | const futex_ptr_t *fut_ptr2 = member_to_inst(item2, futex_ptr_t, cht_link);
|
---|
[9c1c6771] | 500 |
|
---|
[669f3d32] | 501 | return fut_ptr1->uaddr == fut_ptr2->uaddr;
|
---|
[e090e1bc] | 502 | }
|
---|
[b45c443] | 503 |
|
---|
[669f3d32] | 504 | static bool task_fut_ht_key_equal(void *key, const cht_link_t *item)
|
---|
| 505 | {
|
---|
| 506 | const futex_ptr_t *fut_ptr = member_to_inst(item, futex_ptr_t, cht_link);
|
---|
| 507 | uintptr_t uaddr = *(uintptr_t*)key;
|
---|
[9c1c6771] | 508 |
|
---|
[669f3d32] | 509 | return fut_ptr->uaddr == uaddr;
|
---|
[e090e1bc] | 510 | }
|
---|
[b45c443] | 511 |
|
---|
[cc73a8a1] | 512 | /** @}
|
---|
[b45c443] | 513 | */
|
---|