[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>
|
---|
[ee42e43] | 39 | #include <synch/mutex.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 | /**
|
---|
[ee42e43] | 67 | * Mutex protecting global futex hash table.
|
---|
[4fded58] | 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 | */
|
---|
[ee42e43] | 71 | static mutex_t futex_ht_lock;
|
---|
[9aa72b4] | 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 | {
|
---|
[ee42e43] | 86 | mutex_initialize(&futex_ht_lock, MUTEX_PASSIVE);
|
---|
[9aa72b4] | 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;
|
---|
[741fd16] | 115 | int rc;
|
---|
[9aa72b4] | 116 |
|
---|
| 117 | /*
|
---|
| 118 | * Find physical address of futex counter.
|
---|
| 119 | */
|
---|
| 120 | page_table_lock(AS, true);
|
---|
| 121 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
| 122 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
|
---|
| 123 | page_table_unlock(AS, true);
|
---|
[7f1c620] | 124 | return (unative_t) ENOENT;
|
---|
[9aa72b4] | 125 | }
|
---|
[9dfc69a] | 126 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
[9aa72b4] | 127 | page_table_unlock(AS, true);
|
---|
| 128 |
|
---|
[a9ef68b] | 129 | futex = futex_find(paddr);
|
---|
[741fd16] | 130 |
|
---|
| 131 | #ifdef CONFIG_UDEBUG
|
---|
| 132 | udebug_stoppable_begin();
|
---|
| 133 | #endif
|
---|
[4774a32] | 134 | rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
[741fd16] | 135 | #ifdef CONFIG_UDEBUG
|
---|
| 136 | udebug_stoppable_end();
|
---|
| 137 | #endif
|
---|
| 138 | return (unative_t) rc;
|
---|
[9aa72b4] | 139 | }
|
---|
| 140 |
|
---|
| 141 | /** Wakeup one thread waiting in futex wait queue.
|
---|
| 142 | *
|
---|
[4774a32] | 143 | * @param uaddr Userspace address of the futex counter.
|
---|
[9aa72b4] | 144 | *
|
---|
[4774a32] | 145 | * @return ENOENT if there is no physical mapping for uaddr.
|
---|
[9aa72b4] | 146 | */
|
---|
[7f1c620] | 147 | unative_t sys_futex_wakeup(uintptr_t uaddr)
|
---|
[9aa72b4] | 148 | {
|
---|
[a9ef68b] | 149 | futex_t *futex;
|
---|
[7f1c620] | 150 | uintptr_t paddr;
|
---|
[9aa72b4] | 151 | pte_t *t;
|
---|
[303c94c] | 152 | ipl_t ipl;
|
---|
| 153 |
|
---|
| 154 | ipl = interrupts_disable();
|
---|
[9aa72b4] | 155 |
|
---|
| 156 | /*
|
---|
| 157 | * Find physical address of futex counter.
|
---|
| 158 | */
|
---|
| 159 | page_table_lock(AS, true);
|
---|
| 160 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
| 161 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
|
---|
| 162 | page_table_unlock(AS, true);
|
---|
[303c94c] | 163 | interrupts_restore(ipl);
|
---|
[7f1c620] | 164 | return (unative_t) ENOENT;
|
---|
[9aa72b4] | 165 | }
|
---|
[9dfc69a] | 166 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
[9aa72b4] | 167 | page_table_unlock(AS, true);
|
---|
| 168 |
|
---|
[303c94c] | 169 | interrupts_restore(ipl);
|
---|
| 170 |
|
---|
[a9ef68b] | 171 | futex = futex_find(paddr);
|
---|
| 172 |
|
---|
| 173 | waitq_wakeup(&futex->wq, WAKEUP_FIRST);
|
---|
| 174 |
|
---|
| 175 | return 0;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | /** Find kernel address of the futex structure corresponding to paddr.
|
---|
| 179 | *
|
---|
[4fded58] | 180 | * If the structure does not exist already, a new one is created.
|
---|
[a9ef68b] | 181 | *
|
---|
[4774a32] | 182 | * @param paddr Physical address of the userspace futex counter.
|
---|
[a9ef68b] | 183 | *
|
---|
[4774a32] | 184 | * @return Address of the kernel futex structure.
|
---|
[a9ef68b] | 185 | */
|
---|
[7f1c620] | 186 | futex_t *futex_find(uintptr_t paddr)
|
---|
[a9ef68b] | 187 | {
|
---|
| 188 | link_t *item;
|
---|
| 189 | futex_t *futex;
|
---|
[4fded58] | 190 | btree_node_t *leaf;
|
---|
[a9ef68b] | 191 |
|
---|
[9aa72b4] | 192 | /*
|
---|
[a9ef68b] | 193 | * Find the respective futex structure
|
---|
| 194 | * or allocate new one if it does not exist already.
|
---|
[9aa72b4] | 195 | */
|
---|
[ee42e43] | 196 | mutex_lock(&futex_ht_lock);
|
---|
[9aa72b4] | 197 | item = hash_table_find(&futex_ht, &paddr);
|
---|
| 198 | if (item) {
|
---|
| 199 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
[4fded58] | 200 |
|
---|
| 201 | /*
|
---|
| 202 | * See if the current task knows this futex.
|
---|
| 203 | */
|
---|
| 204 | mutex_lock(&TASK->futexes_lock);
|
---|
| 205 | if (!btree_search(&TASK->futexes, paddr, &leaf)) {
|
---|
| 206 | /*
|
---|
| 207 | * The futex is new to the current task.
|
---|
[ee42e43] | 208 | * Upgrade its reference count and put it to the
|
---|
| 209 | * current task's B+tree of known futexes.
|
---|
[4fded58] | 210 | */
|
---|
[ee42e43] | 211 | futex->refcount++;
|
---|
| 212 | btree_insert(&TASK->futexes, paddr, futex, leaf);
|
---|
[4fded58] | 213 | }
|
---|
| 214 | mutex_unlock(&TASK->futexes_lock);
|
---|
[a9ef68b] | 215 | } else {
|
---|
[ee42e43] | 216 | futex = (futex_t *) malloc(sizeof(futex_t), 0);
|
---|
| 217 | futex_initialize(futex);
|
---|
| 218 | futex->paddr = paddr;
|
---|
| 219 | hash_table_insert(&futex_ht, &paddr, &futex->ht_link);
|
---|
| 220 |
|
---|
[a9ef68b] | 221 | /*
|
---|
[ee42e43] | 222 | * This is the first task referencing the futex.
|
---|
| 223 | * It can be directly inserted into its
|
---|
| 224 | * B+tree of known futexes.
|
---|
[a9ef68b] | 225 | */
|
---|
[ee42e43] | 226 | mutex_lock(&TASK->futexes_lock);
|
---|
| 227 | btree_insert(&TASK->futexes, paddr, futex, NULL);
|
---|
| 228 | mutex_unlock(&TASK->futexes_lock);
|
---|
| 229 |
|
---|
[a9ef68b] | 230 | }
|
---|
[ee42e43] | 231 | mutex_unlock(&futex_ht_lock);
|
---|
[9aa72b4] | 232 |
|
---|
[a9ef68b] | 233 | return futex;
|
---|
[9aa72b4] | 234 | }
|
---|
| 235 |
|
---|
| 236 | /** Compute hash index into futex hash table.
|
---|
| 237 | *
|
---|
[4774a32] | 238 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 239 | * counter) is stored.
|
---|
[9aa72b4] | 240 | *
|
---|
[4774a32] | 241 | * @return Index into futex hash table.
|
---|
[9aa72b4] | 242 | */
|
---|
[98000fb] | 243 | size_t futex_ht_hash(unative_t *key)
|
---|
[9aa72b4] | 244 | {
|
---|
[98000fb] | 245 | return (*key & (FUTEX_HT_SIZE - 1));
|
---|
[9aa72b4] | 246 | }
|
---|
| 247 |
|
---|
| 248 | /** Compare futex hash table item with a key.
|
---|
| 249 | *
|
---|
[4774a32] | 250 | * @param key Address where the key (i.e. physical address of futex
|
---|
| 251 | * counter) is stored.
|
---|
[9aa72b4] | 252 | *
|
---|
[4774a32] | 253 | * @return True if the item matches the key. False otherwise.
|
---|
[9aa72b4] | 254 | */
|
---|
[98000fb] | 255 | bool futex_ht_compare(unative_t *key, size_t keys, link_t *item)
|
---|
[9aa72b4] | 256 | {
|
---|
| 257 | futex_t *futex;
|
---|
| 258 |
|
---|
| 259 | ASSERT(keys == 1);
|
---|
| 260 |
|
---|
| 261 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 262 | return *key == futex->paddr;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /** Callback for removal items from futex hash table.
|
---|
| 266 | *
|
---|
[4774a32] | 267 | * @param item Item removed from the hash table.
|
---|
[9aa72b4] | 268 | */
|
---|
| 269 | void futex_ht_remove_callback(link_t *item)
|
---|
| 270 | {
|
---|
| 271 | futex_t *futex;
|
---|
| 272 |
|
---|
| 273 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 274 | free(futex);
|
---|
| 275 | }
|
---|
[e090e1bc] | 276 |
|
---|
| 277 | /** Remove references from futexes known to the current task. */
|
---|
| 278 | void futex_cleanup(void)
|
---|
| 279 | {
|
---|
[9c1c6771] | 280 | link_t *cur;
|
---|
| 281 |
|
---|
[ee42e43] | 282 | mutex_lock(&futex_ht_lock);
|
---|
[9c1c6771] | 283 | mutex_lock(&TASK->futexes_lock);
|
---|
| 284 |
|
---|
[6f4495f5] | 285 | for (cur = TASK->futexes.leaf_head.next;
|
---|
| 286 | cur != &TASK->futexes.leaf_head; cur = cur->next) {
|
---|
[9c1c6771] | 287 | btree_node_t *node;
|
---|
[6c441cf8] | 288 | unsigned int i;
|
---|
[9c1c6771] | 289 |
|
---|
| 290 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 291 | for (i = 0; i < node->keys; i++) {
|
---|
| 292 | futex_t *ftx;
|
---|
[7f1c620] | 293 | uintptr_t paddr = node->key[i];
|
---|
[9c1c6771] | 294 |
|
---|
| 295 | ftx = (futex_t *) node->value[i];
|
---|
| 296 | if (--ftx->refcount == 0)
|
---|
| 297 | hash_table_remove(&futex_ht, &paddr, 1);
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | mutex_unlock(&TASK->futexes_lock);
|
---|
[ee42e43] | 302 | mutex_unlock(&futex_ht_lock);
|
---|
[e090e1bc] | 303 | }
|
---|
[b45c443] | 304 |
|
---|
[cc73a8a1] | 305 | /** @}
|
---|
[b45c443] | 306 | */
|
---|