[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);
|
---|
[98000fb] | 62 | static size_t futex_ht_hash(unative_t *key);
|
---|
| 63 | static bool futex_ht_compare(unative_t *key, size_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 | *
|
---|
[4774a32] | 92 | * @param futex Kernel futex structure.
|
---|
[9aa72b4] | 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 | *
|
---|
[4774a32] | 104 | * @param uaddr Userspace address of the futex counter.
|
---|
[9aa72b4] | 105 | *
|
---|
[4774a32] | 106 | * @return If there is no physical mapping for uaddr ENOENT is
|
---|
| 107 | * returned. Otherwise returns a wait result as defined in
|
---|
| 108 | * synch.h.
|
---|
[9aa72b4] | 109 | */
|
---|
[4774a32] | 110 | unative_t sys_futex_sleep(uintptr_t uaddr)
|
---|
[9aa72b4] | 111 | {
|
---|
| 112 | futex_t *futex;
|
---|
[7f1c620] | 113 | uintptr_t paddr;
|
---|
[9aa72b4] | 114 | pte_t *t;
|
---|
[303c94c] | 115 | ipl_t ipl;
|
---|
[741fd16] | 116 | int rc;
|
---|
[9aa72b4] | 117 |
|
---|
[303c94c] | 118 | ipl = interrupts_disable();
|
---|
| 119 |
|
---|
[9aa72b4] | 120 | /*
|
---|
| 121 | * Find physical address of futex counter.
|
---|
| 122 | */
|
---|
| 123 | page_table_lock(AS, true);
|
---|
| 124 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
| 125 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
|
---|
| 126 | page_table_unlock(AS, true);
|
---|
[303c94c] | 127 | interrupts_restore(ipl);
|
---|
[7f1c620] | 128 | return (unative_t) ENOENT;
|
---|
[9aa72b4] | 129 | }
|
---|
[9dfc69a] | 130 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
[9aa72b4] | 131 | page_table_unlock(AS, true);
|
---|
| 132 |
|
---|
[303c94c] | 133 | interrupts_restore(ipl);
|
---|
| 134 |
|
---|
[a9ef68b] | 135 | futex = futex_find(paddr);
|
---|
[741fd16] | 136 |
|
---|
| 137 | #ifdef CONFIG_UDEBUG
|
---|
| 138 | udebug_stoppable_begin();
|
---|
| 139 | #endif
|
---|
[4774a32] | 140 | rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
[741fd16] | 141 | #ifdef CONFIG_UDEBUG
|
---|
| 142 | udebug_stoppable_end();
|
---|
| 143 | #endif
|
---|
| 144 | return (unative_t) rc;
|
---|
[9aa72b4] | 145 | }
|
---|
| 146 |
|
---|
| 147 | /** Wakeup one thread waiting in futex wait queue.
|
---|
| 148 | *
|
---|
[4774a32] | 149 | * @param uaddr Userspace address of the futex counter.
|
---|
[9aa72b4] | 150 | *
|
---|
[4774a32] | 151 | * @return ENOENT if there is no physical mapping for uaddr.
|
---|
[9aa72b4] | 152 | */
|
---|
[7f1c620] | 153 | unative_t sys_futex_wakeup(uintptr_t uaddr)
|
---|
[9aa72b4] | 154 | {
|
---|
[a9ef68b] | 155 | futex_t *futex;
|
---|
[7f1c620] | 156 | uintptr_t paddr;
|
---|
[9aa72b4] | 157 | pte_t *t;
|
---|
[303c94c] | 158 | ipl_t ipl;
|
---|
| 159 |
|
---|
| 160 | ipl = interrupts_disable();
|
---|
[9aa72b4] | 161 |
|
---|
| 162 | /*
|
---|
| 163 | * Find physical address of futex counter.
|
---|
| 164 | */
|
---|
| 165 | page_table_lock(AS, true);
|
---|
| 166 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
| 167 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
|
---|
| 168 | page_table_unlock(AS, true);
|
---|
[303c94c] | 169 | interrupts_restore(ipl);
|
---|
[7f1c620] | 170 | return (unative_t) ENOENT;
|
---|
[9aa72b4] | 171 | }
|
---|
[9dfc69a] | 172 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
[9aa72b4] | 173 | page_table_unlock(AS, true);
|
---|
| 174 |
|
---|
[303c94c] | 175 | interrupts_restore(ipl);
|
---|
| 176 |
|
---|
[a9ef68b] | 177 | futex = futex_find(paddr);
|
---|
| 178 |
|
---|
| 179 | waitq_wakeup(&futex->wq, WAKEUP_FIRST);
|
---|
| 180 |
|
---|
| 181 | return 0;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /** Find kernel address of the futex structure corresponding to paddr.
|
---|
| 185 | *
|
---|
[4fded58] | 186 | * If the structure does not exist already, a new one is created.
|
---|
[a9ef68b] | 187 | *
|
---|
[4774a32] | 188 | * @param paddr Physical address of the userspace futex counter.
|
---|
[a9ef68b] | 189 | *
|
---|
[4774a32] | 190 | * @return Address of the kernel futex structure.
|
---|
[a9ef68b] | 191 | */
|
---|
[7f1c620] | 192 | futex_t *futex_find(uintptr_t paddr)
|
---|
[a9ef68b] | 193 | {
|
---|
| 194 | link_t *item;
|
---|
| 195 | futex_t *futex;
|
---|
[4fded58] | 196 | btree_node_t *leaf;
|
---|
[a9ef68b] | 197 |
|
---|
[9aa72b4] | 198 | /*
|
---|
[a9ef68b] | 199 | * Find the respective futex structure
|
---|
| 200 | * or allocate new one if it does not exist already.
|
---|
[9aa72b4] | 201 | */
|
---|
| 202 | rwlock_read_lock(&futex_ht_lock);
|
---|
| 203 | item = hash_table_find(&futex_ht, &paddr);
|
---|
| 204 | if (item) {
|
---|
| 205 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
[4fded58] | 206 |
|
---|
| 207 | /*
|
---|
| 208 | * See if the current task knows this futex.
|
---|
| 209 | */
|
---|
| 210 | mutex_lock(&TASK->futexes_lock);
|
---|
| 211 | if (!btree_search(&TASK->futexes, paddr, &leaf)) {
|
---|
| 212 | /*
|
---|
| 213 | * The futex is new to the current task.
|
---|
| 214 | * However, we only have read access.
|
---|
| 215 | * Gain write access and try again.
|
---|
| 216 | */
|
---|
| 217 | mutex_unlock(&TASK->futexes_lock);
|
---|
| 218 | goto gain_write_access;
|
---|
| 219 | }
|
---|
| 220 | mutex_unlock(&TASK->futexes_lock);
|
---|
| 221 |
|
---|
[a9ef68b] | 222 | rwlock_read_unlock(&futex_ht_lock);
|
---|
| 223 | } else {
|
---|
[4fded58] | 224 | gain_write_access:
|
---|
[a9ef68b] | 225 | /*
|
---|
| 226 | * Upgrade to writer is not currently supported,
|
---|
[9dfc69a] | 227 | * therefore, it is necessary to release the read lock
|
---|
[a9ef68b] | 228 | * and reacquire it as a writer.
|
---|
| 229 | */
|
---|
| 230 | rwlock_read_unlock(&futex_ht_lock);
|
---|
[303c94c] | 231 |
|
---|
[a9ef68b] | 232 | rwlock_write_lock(&futex_ht_lock);
|
---|
| 233 | /*
|
---|
| 234 | * Avoid possible race condition by searching
|
---|
| 235 | * the hash table once again with write access.
|
---|
| 236 | */
|
---|
| 237 | item = hash_table_find(&futex_ht, &paddr);
|
---|
| 238 | if (item) {
|
---|
| 239 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
[4fded58] | 240 |
|
---|
| 241 | /*
|
---|
| 242 | * See if this futex is known to the current task.
|
---|
| 243 | */
|
---|
| 244 | mutex_lock(&TASK->futexes_lock);
|
---|
| 245 | if (!btree_search(&TASK->futexes, paddr, &leaf)) {
|
---|
| 246 | /*
|
---|
| 247 | * The futex is new to the current task.
|
---|
| 248 | * Upgrade its reference count and put it to the
|
---|
| 249 | * current task's B+tree of known futexes.
|
---|
| 250 | */
|
---|
| 251 | futex->refcount++;
|
---|
[6f4495f5] | 252 | btree_insert(&TASK->futexes, paddr, futex,
|
---|
| 253 | leaf);
|
---|
[4fded58] | 254 | }
|
---|
| 255 | mutex_unlock(&TASK->futexes_lock);
|
---|
| 256 |
|
---|
[a9ef68b] | 257 | rwlock_write_unlock(&futex_ht_lock);
|
---|
| 258 | } else {
|
---|
| 259 | futex = (futex_t *) malloc(sizeof(futex_t), 0);
|
---|
| 260 | futex_initialize(futex);
|
---|
| 261 | futex->paddr = paddr;
|
---|
| 262 | hash_table_insert(&futex_ht, &paddr, &futex->ht_link);
|
---|
[4fded58] | 263 |
|
---|
| 264 | /*
|
---|
| 265 | * This is the first task referencing the futex.
|
---|
| 266 | * It can be directly inserted into its
|
---|
| 267 | * B+tree of known futexes.
|
---|
| 268 | */
|
---|
| 269 | mutex_lock(&TASK->futexes_lock);
|
---|
| 270 | btree_insert(&TASK->futexes, paddr, futex, NULL);
|
---|
| 271 | mutex_unlock(&TASK->futexes_lock);
|
---|
| 272 |
|
---|
[a9ef68b] | 273 | rwlock_write_unlock(&futex_ht_lock);
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
[9aa72b4] | 276 |
|
---|
[a9ef68b] | 277 | return futex;
|
---|
[9aa72b4] | 278 | }
|
---|
| 279 |
|
---|
| 280 | /** Compute hash index into futex hash table.
|
---|
| 281 | *
|
---|
[4774a32] | 282 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 283 | * counter) is stored.
|
---|
[9aa72b4] | 284 | *
|
---|
[4774a32] | 285 | * @return Index into futex hash table.
|
---|
[9aa72b4] | 286 | */
|
---|
[98000fb] | 287 | size_t futex_ht_hash(unative_t *key)
|
---|
[9aa72b4] | 288 | {
|
---|
[98000fb] | 289 | return (*key & (FUTEX_HT_SIZE - 1));
|
---|
[9aa72b4] | 290 | }
|
---|
| 291 |
|
---|
| 292 | /** Compare futex hash table item with a key.
|
---|
| 293 | *
|
---|
[4774a32] | 294 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 295 | * counter) is stored.
|
---|
[9aa72b4] | 296 | *
|
---|
[4774a32] | 297 | * @return True if the item matches the key. False otherwise.
|
---|
[9aa72b4] | 298 | */
|
---|
[98000fb] | 299 | bool futex_ht_compare(unative_t *key, size_t keys, link_t *item)
|
---|
[9aa72b4] | 300 | {
|
---|
| 301 | futex_t *futex;
|
---|
| 302 |
|
---|
| 303 | ASSERT(keys == 1);
|
---|
| 304 |
|
---|
| 305 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 306 | return *key == futex->paddr;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /** Callback for removal items from futex hash table.
|
---|
| 310 | *
|
---|
[4774a32] | 311 | * @param item Item removed from the hash table.
|
---|
[9aa72b4] | 312 | */
|
---|
| 313 | void futex_ht_remove_callback(link_t *item)
|
---|
| 314 | {
|
---|
| 315 | futex_t *futex;
|
---|
| 316 |
|
---|
| 317 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 318 | free(futex);
|
---|
| 319 | }
|
---|
[e090e1bc] | 320 |
|
---|
| 321 | /** Remove references from futexes known to the current task. */
|
---|
| 322 | void futex_cleanup(void)
|
---|
| 323 | {
|
---|
[9c1c6771] | 324 | link_t *cur;
|
---|
| 325 |
|
---|
| 326 | rwlock_write_lock(&futex_ht_lock);
|
---|
| 327 | mutex_lock(&TASK->futexes_lock);
|
---|
| 328 |
|
---|
[6f4495f5] | 329 | for (cur = TASK->futexes.leaf_head.next;
|
---|
| 330 | cur != &TASK->futexes.leaf_head; cur = cur->next) {
|
---|
[9c1c6771] | 331 | btree_node_t *node;
|
---|
[6c441cf8] | 332 | unsigned int i;
|
---|
[9c1c6771] | 333 |
|
---|
| 334 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 335 | for (i = 0; i < node->keys; i++) {
|
---|
| 336 | futex_t *ftx;
|
---|
[7f1c620] | 337 | uintptr_t paddr = node->key[i];
|
---|
[9c1c6771] | 338 |
|
---|
| 339 | ftx = (futex_t *) node->value[i];
|
---|
| 340 | if (--ftx->refcount == 0)
|
---|
| 341 | hash_table_remove(&futex_ht, &paddr, 1);
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | mutex_unlock(&TASK->futexes_lock);
|
---|
| 346 | rwlock_write_unlock(&futex_ht_lock);
|
---|
[e090e1bc] | 347 | }
|
---|
[b45c443] | 348 |
|
---|
[cc73a8a1] | 349 | /** @}
|
---|
[b45c443] | 350 | */
|
---|