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