[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 | */
|
---|
[ee42e43] | 123 | static mutex_t 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 | {
|
---|
[ee42e43] | 150 | mutex_initialize(&futex_ht_lock, MUTEX_PASSIVE);
|
---|
[9aa72b4] | 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);
|
---|
[669f3d32] | 160 | }
|
---|
| 161 |
|
---|
| 162 | /** Destroys the futex structures for the dying task. */
|
---|
| 163 | void futex_task_deinit(struct task *task)
|
---|
| 164 | {
|
---|
| 165 | /* Interrupts are disabled so we must not block (cannot run cht_destroy). */
|
---|
| 166 | if (interrupts_disabled()) {
|
---|
| 167 | /* Invoke the blocking cht_destroy in the background. */
|
---|
| 168 | workq_global_enqueue_noblock(&task->futexes->destroy_work,
|
---|
| 169 | destroy_task_cache);
|
---|
| 170 | } else {
|
---|
| 171 | /* We can block. Invoke cht_destroy in this thread. */
|
---|
| 172 | destroy_task_cache(&task->futexes->destroy_work);
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /** Deallocates a task's CHT futex cache (must already be empty). */
|
---|
| 177 | static void destroy_task_cache(work_t *work)
|
---|
| 178 | {
|
---|
| 179 | struct futex_cache *cache =
|
---|
| 180 | member_to_inst(work, struct futex_cache, destroy_work);
|
---|
| 181 |
|
---|
| 182 | cht_destroy(&cache->ht);
|
---|
| 183 | free(cache);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /** Remove references from futexes known to the current task. */
|
---|
| 187 | void futex_task_cleanup(void)
|
---|
| 188 | {
|
---|
| 189 | /* All threads of this task have terminated. This is the last thread. */
|
---|
| 190 | mutex_lock(&TASK->futex_list_lock);
|
---|
| 191 |
|
---|
| 192 | list_foreach_safe(TASK->futex_list, cur_link, next_link) {
|
---|
| 193 | futex_ptr_t *fut_ptr = member_to_inst(cur_link, futex_ptr_t, cht_link);
|
---|
| 194 |
|
---|
| 195 | /*
|
---|
| 196 | * The function is free to free the futex. All other threads of this
|
---|
| 197 | * task have already terminated, so they have also definitely
|
---|
| 198 | * exited their CHT futex cache protecting rcu reader sections.
|
---|
| 199 | * Moreover release_ref() only frees the futex if this is the
|
---|
| 200 | * last task referencing the futex. Therefore, only threads
|
---|
| 201 | * of this task may have referenced the futex if it is to be freed.
|
---|
| 202 | */
|
---|
| 203 | futex_release_ref_locked(fut_ptr->futex);
|
---|
| 204 |
|
---|
| 205 | /*
|
---|
| 206 | * No need to remove the futex_ptr from the CHT cache of this task
|
---|
| 207 | * because futex_task_deinit() will destroy the CHT shortly.
|
---|
| 208 | */
|
---|
| 209 | list_remove(cur_link);
|
---|
| 210 | free(fut_ptr);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | mutex_unlock(&TASK->futex_list_lock);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | /** Initialize the kernel futex structure.
|
---|
[9aa72b4] | 218 | *
|
---|
[669f3d32] | 219 | * @param futex Kernel futex structure.
|
---|
| 220 | * @param paddr Physical address of the futex variable.
|
---|
[9aa72b4] | 221 | */
|
---|
[669f3d32] | 222 | static void futex_initialize(futex_t *futex, uintptr_t paddr)
|
---|
[9aa72b4] | 223 | {
|
---|
| 224 | waitq_initialize(&futex->wq);
|
---|
| 225 | link_initialize(&futex->ht_link);
|
---|
[669f3d32] | 226 | futex->paddr = paddr;
|
---|
[4fded58] | 227 | futex->refcount = 1;
|
---|
[9aa72b4] | 228 | }
|
---|
| 229 |
|
---|
[669f3d32] | 230 | /** Increments the counter of tasks referencing the futex. */
|
---|
| 231 | static void futex_add_ref(futex_t *futex)
|
---|
| 232 | {
|
---|
| 233 | ASSERT(mutex_locked(&futex_ht_lock));
|
---|
| 234 | ASSERT(0 < futex->refcount);
|
---|
| 235 | ++futex->refcount;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /** Decrements the counter of tasks referencing the futex. May free the futex.*/
|
---|
| 239 | static void futex_release_ref(futex_t *futex)
|
---|
| 240 | {
|
---|
| 241 | ASSERT(mutex_locked(&futex_ht_lock));
|
---|
| 242 | ASSERT(0 < futex->refcount);
|
---|
| 243 |
|
---|
| 244 | --futex->refcount;
|
---|
| 245 |
|
---|
| 246 | if (0 == futex->refcount) {
|
---|
| 247 | hash_table_remove(&futex_ht, &futex->paddr, 1);
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /** Decrements the counter of tasks referencing the futex. May free the futex.*/
|
---|
| 252 | static void futex_release_ref_locked(futex_t *futex)
|
---|
| 253 | {
|
---|
| 254 | mutex_lock(&futex_ht_lock);
|
---|
| 255 | futex_release_ref(futex);
|
---|
| 256 | mutex_unlock(&futex_ht_lock);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /** Returns a futex for the virtual address @a uaddr (or creates one). */
|
---|
| 260 | static futex_t *get_futex(uintptr_t uaddr)
|
---|
| 261 | {
|
---|
| 262 | futex_t *futex = find_cached_futex(uaddr);
|
---|
| 263 |
|
---|
| 264 | if (futex)
|
---|
| 265 | return futex;
|
---|
| 266 |
|
---|
| 267 | uintptr_t paddr;
|
---|
| 268 |
|
---|
| 269 | if (!find_futex_paddr(uaddr, &paddr))
|
---|
| 270 | return 0;
|
---|
| 271 |
|
---|
| 272 | return get_and_cache_futex(paddr, uaddr);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 | /** Finds the physical address of the futex variable. */
|
---|
| 277 | static bool find_futex_paddr(uintptr_t uaddr, uintptr_t *paddr)
|
---|
| 278 | {
|
---|
| 279 | page_table_lock(AS, true);
|
---|
| 280 |
|
---|
| 281 | bool found = false;
|
---|
| 282 | pte_t *t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), false);
|
---|
| 283 |
|
---|
| 284 | if (t && PTE_VALID(t) && PTE_PRESENT(t)) {
|
---|
| 285 | found = true;
|
---|
| 286 | *paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | page_table_unlock(AS, true);
|
---|
| 290 |
|
---|
| 291 | return found;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | /** Returns the futex cached in this task with the virtual address uaddr. */
|
---|
| 295 | static futex_t *find_cached_futex(uintptr_t uaddr)
|
---|
| 296 | {
|
---|
| 297 | cht_read_lock();
|
---|
| 298 |
|
---|
| 299 | futex_t *futex;
|
---|
| 300 | cht_link_t *futex_ptr_link = cht_find(&TASK->futexes->ht, &uaddr);
|
---|
| 301 |
|
---|
| 302 | if (futex_ptr_link) {
|
---|
| 303 | futex_ptr_t *futex_ptr
|
---|
| 304 | = member_to_inst(futex_ptr_link, futex_ptr_t, cht_link);
|
---|
| 305 |
|
---|
| 306 | futex = futex_ptr->futex;
|
---|
| 307 | } else {
|
---|
| 308 | futex = NULL;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | cht_read_unlock();
|
---|
| 312 |
|
---|
| 313 | return futex;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 |
|
---|
| 317 | /**
|
---|
| 318 | * Returns a kernel futex for the physical address @a phys_addr and caches
|
---|
| 319 | * it in this task under the virtual address @a uaddr (if not already cached).
|
---|
| 320 | */
|
---|
| 321 | static futex_t *get_and_cache_futex(uintptr_t phys_addr, uintptr_t uaddr)
|
---|
| 322 | {
|
---|
| 323 | futex_t *futex = malloc(sizeof(futex_t), 0);
|
---|
| 324 |
|
---|
| 325 | /*
|
---|
| 326 | * Find the futex object in the global futex table (or insert it
|
---|
| 327 | * if it is not present).
|
---|
| 328 | */
|
---|
| 329 | mutex_lock(&futex_ht_lock);
|
---|
| 330 |
|
---|
| 331 | link_t *fut_link = hash_table_find(&futex_ht, &phys_addr);
|
---|
| 332 |
|
---|
| 333 | if (fut_link) {
|
---|
| 334 | free(futex);
|
---|
| 335 | futex = member_to_inst(fut_link, futex_t, ht_link);
|
---|
| 336 | futex_add_ref(futex);
|
---|
| 337 | } else {
|
---|
| 338 | futex_initialize(futex, phys_addr);
|
---|
| 339 | hash_table_insert(&futex_ht, &phys_addr, &futex->ht_link);
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | mutex_unlock(&futex_ht_lock);
|
---|
| 343 |
|
---|
| 344 | /*
|
---|
| 345 | * Cache the link to the futex object for this task.
|
---|
| 346 | */
|
---|
| 347 | futex_ptr_t *fut_ptr = malloc(sizeof(futex_ptr_t), 0);
|
---|
| 348 | cht_link_t *dup_link;
|
---|
| 349 |
|
---|
| 350 | fut_ptr->futex = futex;
|
---|
| 351 | fut_ptr->uaddr = uaddr;
|
---|
| 352 |
|
---|
| 353 | cht_read_lock();
|
---|
| 354 |
|
---|
| 355 | /* Cache the mapping from the virtual address to the futex for this task. */
|
---|
| 356 | if (cht_insert_unique(&TASK->futexes->ht, &fut_ptr->cht_link, &dup_link)) {
|
---|
| 357 | mutex_lock(&TASK->futex_list_lock);
|
---|
| 358 | list_append(&fut_ptr->all_link, &TASK->futex_list);
|
---|
| 359 | mutex_unlock(&TASK->futex_list_lock);
|
---|
| 360 | } else {
|
---|
| 361 | /* Another thread of this task beat us to it. Use that mapping instead.*/
|
---|
| 362 | free(fut_ptr);
|
---|
| 363 | futex_release_ref_locked(futex);
|
---|
| 364 |
|
---|
| 365 | futex_ptr_t *dup = member_to_inst(dup_link, futex_ptr_t, cht_link);
|
---|
| 366 | futex = dup->futex;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | cht_read_unlock();
|
---|
| 370 |
|
---|
| 371 | return futex;
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[9aa72b4] | 374 | /** Sleep in futex wait queue.
|
---|
| 375 | *
|
---|
[4774a32] | 376 | * @param uaddr Userspace address of the futex counter.
|
---|
[9aa72b4] | 377 | *
|
---|
[4774a32] | 378 | * @return If there is no physical mapping for uaddr ENOENT is
|
---|
| 379 | * returned. Otherwise returns a wait result as defined in
|
---|
| 380 | * synch.h.
|
---|
[9aa72b4] | 381 | */
|
---|
[96b02eb9] | 382 | sysarg_t sys_futex_sleep(uintptr_t uaddr)
|
---|
[9aa72b4] | 383 | {
|
---|
[669f3d32] | 384 | futex_t *futex = get_futex(uaddr);
|
---|
[9aa72b4] | 385 |
|
---|
[669f3d32] | 386 | if (!futex)
|
---|
[96b02eb9] | 387 | return (sysarg_t) ENOENT;
|
---|
[741fd16] | 388 |
|
---|
| 389 | #ifdef CONFIG_UDEBUG
|
---|
| 390 | udebug_stoppable_begin();
|
---|
| 391 | #endif
|
---|
[669f3d32] | 392 | int rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
[741fd16] | 393 | #ifdef CONFIG_UDEBUG
|
---|
| 394 | udebug_stoppable_end();
|
---|
| 395 | #endif
|
---|
[96b02eb9] | 396 | return (sysarg_t) rc;
|
---|
[9aa72b4] | 397 | }
|
---|
| 398 |
|
---|
| 399 | /** Wakeup one thread waiting in futex wait queue.
|
---|
| 400 | *
|
---|
[4774a32] | 401 | * @param uaddr Userspace address of the futex counter.
|
---|
[9aa72b4] | 402 | *
|
---|
[4774a32] | 403 | * @return ENOENT if there is no physical mapping for uaddr.
|
---|
[9aa72b4] | 404 | */
|
---|
[96b02eb9] | 405 | sysarg_t sys_futex_wakeup(uintptr_t uaddr)
|
---|
[9aa72b4] | 406 | {
|
---|
[669f3d32] | 407 | futex_t *futex = get_futex(uaddr);
|
---|
[9aa72b4] | 408 |
|
---|
[669f3d32] | 409 | if (futex) {
|
---|
| 410 | waitq_wakeup(&futex->wq, WAKEUP_FIRST);
|
---|
| 411 | return 0;
|
---|
| 412 | } else {
|
---|
[96b02eb9] | 413 | return (sysarg_t) ENOENT;
|
---|
[9aa72b4] | 414 | }
|
---|
[a9ef68b] | 415 | }
|
---|
| 416 |
|
---|
[9aa72b4] | 417 |
|
---|
| 418 | /** Compute hash index into futex hash table.
|
---|
| 419 | *
|
---|
[4774a32] | 420 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 421 | * counter) is stored.
|
---|
[9aa72b4] | 422 | *
|
---|
[4774a32] | 423 | * @return Index into futex hash table.
|
---|
[9aa72b4] | 424 | */
|
---|
[96b02eb9] | 425 | size_t futex_ht_hash(sysarg_t *key)
|
---|
[9aa72b4] | 426 | {
|
---|
[98000fb] | 427 | return (*key & (FUTEX_HT_SIZE - 1));
|
---|
[9aa72b4] | 428 | }
|
---|
| 429 |
|
---|
| 430 | /** Compare futex hash table item with a key.
|
---|
| 431 | *
|
---|
[4774a32] | 432 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 433 | * counter) is stored.
|
---|
[9aa72b4] | 434 | *
|
---|
[4774a32] | 435 | * @return True if the item matches the key. False otherwise.
|
---|
[9aa72b4] | 436 | */
|
---|
[96b02eb9] | 437 | bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item)
|
---|
[9aa72b4] | 438 | {
|
---|
| 439 | futex_t *futex;
|
---|
| 440 |
|
---|
| 441 | ASSERT(keys == 1);
|
---|
| 442 |
|
---|
| 443 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 444 | return *key == futex->paddr;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | /** Callback for removal items from futex hash table.
|
---|
| 448 | *
|
---|
[4774a32] | 449 | * @param item Item removed from the hash table.
|
---|
[9aa72b4] | 450 | */
|
---|
| 451 | void futex_ht_remove_callback(link_t *item)
|
---|
| 452 | {
|
---|
| 453 | futex_t *futex;
|
---|
| 454 |
|
---|
| 455 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 456 | free(futex);
|
---|
| 457 | }
|
---|
[e090e1bc] | 458 |
|
---|
[669f3d32] | 459 | /*
|
---|
| 460 | * Operations of a task's CHT that caches mappings of futex user space
|
---|
| 461 | * virtual addresses to kernel futex objects.
|
---|
| 462 | */
|
---|
| 463 |
|
---|
| 464 | static size_t task_fut_ht_hash(const cht_link_t *link)
|
---|
[e090e1bc] | 465 | {
|
---|
[669f3d32] | 466 | const futex_ptr_t *fut_ptr = member_to_inst(link, futex_ptr_t, cht_link);
|
---|
| 467 | return fut_ptr->uaddr;
|
---|
| 468 | }
|
---|
[9c1c6771] | 469 |
|
---|
[669f3d32] | 470 | static size_t task_fut_ht_key_hash(void *key)
|
---|
| 471 | {
|
---|
| 472 | return *(uintptr_t*)key;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | static bool task_fut_ht_equal(const cht_link_t *item1, const cht_link_t *item2)
|
---|
| 476 | {
|
---|
| 477 | const futex_ptr_t *fut_ptr1 = member_to_inst(item1, futex_ptr_t, cht_link);
|
---|
| 478 | const futex_ptr_t *fut_ptr2 = member_to_inst(item2, futex_ptr_t, cht_link);
|
---|
[9c1c6771] | 479 |
|
---|
[669f3d32] | 480 | return fut_ptr1->uaddr == fut_ptr2->uaddr;
|
---|
[e090e1bc] | 481 | }
|
---|
[b45c443] | 482 |
|
---|
[669f3d32] | 483 | static bool task_fut_ht_key_equal(void *key, const cht_link_t *item)
|
---|
| 484 | {
|
---|
| 485 | const futex_ptr_t *fut_ptr = member_to_inst(item, futex_ptr_t, cht_link);
|
---|
| 486 | uintptr_t uaddr = *(uintptr_t*)key;
|
---|
| 487 |
|
---|
| 488 | return fut_ptr->uaddr == uaddr;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 |
|
---|
[cc73a8a1] | 492 | /** @}
|
---|
[b45c443] | 493 | */
|
---|