[9aa72b4] | 1 | /*
|
---|
| 2 | * Copyright (C) 2006 Jakub Jermar
|
---|
| 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 |
|
---|
| 29 | /**
|
---|
[cf26ba9] | 30 | * @file futex.c
|
---|
| 31 | * @brief Kernel backend for futexes.
|
---|
| 32 | *
|
---|
| 33 | * @todo Deallocation of orphaned kernel-side futex structures is not currently implemented.
|
---|
[9aa72b4] | 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <synch/futex.h>
|
---|
| 37 | #include <synch/rwlock.h>
|
---|
[303c94c] | 38 | #include <synch/spinlock.h>
|
---|
[9aa72b4] | 39 | #include <synch/synch.h>
|
---|
| 40 | #include <mm/frame.h>
|
---|
| 41 | #include <mm/page.h>
|
---|
| 42 | #include <mm/slab.h>
|
---|
[303c94c] | 43 | #include <proc/thread.h>
|
---|
[9aa72b4] | 44 | #include <genarch/mm/page_pt.h>
|
---|
| 45 | #include <genarch/mm/page_ht.h>
|
---|
| 46 | #include <adt/hash_table.h>
|
---|
| 47 | #include <adt/list.h>
|
---|
| 48 | #include <arch.h>
|
---|
| 49 | #include <align.h>
|
---|
| 50 | #include <panic.h>
|
---|
| 51 | #include <errno.h>
|
---|
| 52 | #include <print.h>
|
---|
| 53 |
|
---|
| 54 | #define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */
|
---|
| 55 |
|
---|
| 56 | static void futex_initialize(futex_t *futex);
|
---|
| 57 |
|
---|
[a9ef68b] | 58 | static futex_t *futex_find(__address paddr);
|
---|
[9aa72b4] | 59 | static index_t futex_ht_hash(__native *key);
|
---|
| 60 | static bool futex_ht_compare(__native *key, count_t keys, link_t *item);
|
---|
| 61 | static void futex_ht_remove_callback(link_t *item);
|
---|
| 62 |
|
---|
| 63 | /** Read-write lock protecting global futex hash table. */
|
---|
| 64 | static rwlock_t futex_ht_lock;
|
---|
| 65 |
|
---|
| 66 | /** Futex hash table. */
|
---|
| 67 | static hash_table_t futex_ht;
|
---|
| 68 |
|
---|
| 69 | /** Futex hash table operations. */
|
---|
| 70 | static hash_table_operations_t futex_ht_ops = {
|
---|
| 71 | .hash = futex_ht_hash,
|
---|
| 72 | .compare = futex_ht_compare,
|
---|
| 73 | .remove_callback = futex_ht_remove_callback
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 | /** Initialize futex subsystem. */
|
---|
| 77 | void futex_init(void)
|
---|
| 78 | {
|
---|
| 79 | rwlock_initialize(&futex_ht_lock);
|
---|
| 80 | hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Initialize kernel futex structure.
|
---|
| 84 | *
|
---|
| 85 | * @param futex Kernel futex structure.
|
---|
| 86 | */
|
---|
| 87 | void futex_initialize(futex_t *futex)
|
---|
| 88 | {
|
---|
| 89 | waitq_initialize(&futex->wq);
|
---|
| 90 | link_initialize(&futex->ht_link);
|
---|
| 91 | futex->paddr = 0;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /** Sleep in futex wait queue.
|
---|
| 95 | *
|
---|
| 96 | * @param uaddr Userspace address of the futex counter.
|
---|
[303c94c] | 97 | * @param usec If non-zero, number of microseconds this thread is willing to sleep.
|
---|
[9179d0a] | 98 | * @param trydown If usec is zero and trydown is non-zero, conditional operation will be attempted.
|
---|
[9aa72b4] | 99 | *
|
---|
[303c94c] | 100 | * @return One of ESYNCH_TIMEOUT, ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED. See synch.h.
|
---|
[9aa72b4] | 101 | * If there is no physical mapping for uaddr ENOENT is returned.
|
---|
| 102 | */
|
---|
[303c94c] | 103 | __native sys_futex_sleep_timeout(__address uaddr, __u32 usec, int trydown)
|
---|
[9aa72b4] | 104 | {
|
---|
| 105 | futex_t *futex;
|
---|
| 106 | __address paddr;
|
---|
| 107 | pte_t *t;
|
---|
[303c94c] | 108 | ipl_t ipl;
|
---|
[9aa72b4] | 109 |
|
---|
[303c94c] | 110 | ipl = interrupts_disable();
|
---|
| 111 |
|
---|
[9aa72b4] | 112 | /*
|
---|
| 113 | * Find physical address of futex counter.
|
---|
| 114 | */
|
---|
| 115 | page_table_lock(AS, true);
|
---|
| 116 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
| 117 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
|
---|
| 118 | page_table_unlock(AS, true);
|
---|
[303c94c] | 119 | interrupts_restore(ipl);
|
---|
[9aa72b4] | 120 | return (__native) ENOENT;
|
---|
| 121 | }
|
---|
[9dfc69a] | 122 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
[9aa72b4] | 123 | page_table_unlock(AS, true);
|
---|
| 124 |
|
---|
[303c94c] | 125 | interrupts_restore(ipl);
|
---|
| 126 |
|
---|
[a9ef68b] | 127 | futex = futex_find(paddr);
|
---|
[9aa72b4] | 128 |
|
---|
[303c94c] | 129 | return (__native) waitq_sleep_timeout(&futex->wq, usec, trydown);
|
---|
[9aa72b4] | 130 | }
|
---|
| 131 |
|
---|
| 132 | /** Wakeup one thread waiting in futex wait queue.
|
---|
| 133 | *
|
---|
| 134 | * @param uaddr Userspace address of the futex counter.
|
---|
| 135 | *
|
---|
[9dfc69a] | 136 | * @return ENOENT if there is no physical mapping for uaddr.
|
---|
[9aa72b4] | 137 | */
|
---|
| 138 | __native sys_futex_wakeup(__address uaddr)
|
---|
| 139 | {
|
---|
[a9ef68b] | 140 | futex_t *futex;
|
---|
[9aa72b4] | 141 | __address paddr;
|
---|
| 142 | pte_t *t;
|
---|
[303c94c] | 143 | ipl_t ipl;
|
---|
| 144 |
|
---|
| 145 | ipl = interrupts_disable();
|
---|
[9aa72b4] | 146 |
|
---|
| 147 | /*
|
---|
| 148 | * Find physical address of futex counter.
|
---|
| 149 | */
|
---|
| 150 | page_table_lock(AS, true);
|
---|
| 151 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
| 152 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
|
---|
| 153 | page_table_unlock(AS, true);
|
---|
[303c94c] | 154 | interrupts_restore(ipl);
|
---|
[9aa72b4] | 155 | return (__native) ENOENT;
|
---|
| 156 | }
|
---|
[9dfc69a] | 157 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
|
---|
[9aa72b4] | 158 | page_table_unlock(AS, true);
|
---|
| 159 |
|
---|
[303c94c] | 160 | interrupts_restore(ipl);
|
---|
| 161 |
|
---|
[a9ef68b] | 162 | futex = futex_find(paddr);
|
---|
| 163 |
|
---|
| 164 | waitq_wakeup(&futex->wq, WAKEUP_FIRST);
|
---|
| 165 |
|
---|
| 166 | return 0;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /** Find kernel address of the futex structure corresponding to paddr.
|
---|
| 170 | *
|
---|
| 171 | * If the structure does not exist alreay, a new one is created.
|
---|
| 172 | *
|
---|
| 173 | * @param paddr Physical address of the userspace futex counter.
|
---|
| 174 | *
|
---|
| 175 | * @return Address of the kernel futex structure.
|
---|
| 176 | */
|
---|
| 177 | futex_t *futex_find(__address paddr)
|
---|
| 178 | {
|
---|
| 179 | link_t *item;
|
---|
| 180 | futex_t *futex;
|
---|
| 181 |
|
---|
[9aa72b4] | 182 | /*
|
---|
[a9ef68b] | 183 | * Find the respective futex structure
|
---|
| 184 | * or allocate new one if it does not exist already.
|
---|
[9aa72b4] | 185 | */
|
---|
| 186 | rwlock_read_lock(&futex_ht_lock);
|
---|
| 187 | item = hash_table_find(&futex_ht, &paddr);
|
---|
| 188 | if (item) {
|
---|
| 189 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
[a9ef68b] | 190 | rwlock_read_unlock(&futex_ht_lock);
|
---|
| 191 | } else {
|
---|
| 192 | /*
|
---|
| 193 | * Upgrade to writer is not currently supported,
|
---|
[9dfc69a] | 194 | * therefore, it is necessary to release the read lock
|
---|
[a9ef68b] | 195 | * and reacquire it as a writer.
|
---|
| 196 | */
|
---|
| 197 | rwlock_read_unlock(&futex_ht_lock);
|
---|
[303c94c] | 198 |
|
---|
[a9ef68b] | 199 | rwlock_write_lock(&futex_ht_lock);
|
---|
| 200 | /*
|
---|
| 201 | * Avoid possible race condition by searching
|
---|
| 202 | * the hash table once again with write access.
|
---|
| 203 | */
|
---|
| 204 | item = hash_table_find(&futex_ht, &paddr);
|
---|
| 205 | if (item) {
|
---|
| 206 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 207 | rwlock_write_unlock(&futex_ht_lock);
|
---|
| 208 | } else {
|
---|
| 209 | futex = (futex_t *) malloc(sizeof(futex_t), 0);
|
---|
| 210 | futex_initialize(futex);
|
---|
| 211 | futex->paddr = paddr;
|
---|
| 212 | hash_table_insert(&futex_ht, &paddr, &futex->ht_link);
|
---|
| 213 | rwlock_write_unlock(&futex_ht_lock);
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
[9aa72b4] | 216 |
|
---|
[a9ef68b] | 217 | return futex;
|
---|
[9aa72b4] | 218 | }
|
---|
| 219 |
|
---|
| 220 | /** Compute hash index into futex hash table.
|
---|
| 221 | *
|
---|
[9179d0a] | 222 | * @param key Address where the key (i.e. physical address of futex counter) is stored.
|
---|
[9aa72b4] | 223 | *
|
---|
| 224 | * @return Index into futex hash table.
|
---|
| 225 | */
|
---|
| 226 | index_t futex_ht_hash(__native *key)
|
---|
| 227 | {
|
---|
| 228 | return *key & (FUTEX_HT_SIZE-1);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /** Compare futex hash table item with a key.
|
---|
| 232 | *
|
---|
| 233 | * @param key Address where the key (i.e. physical address of futex counter) is stored.
|
---|
| 234 | *
|
---|
[9179d0a] | 235 | * @return True if the item matches the key. False otherwise.
|
---|
[9aa72b4] | 236 | */
|
---|
| 237 | bool futex_ht_compare(__native *key, count_t keys, link_t *item)
|
---|
| 238 | {
|
---|
| 239 | futex_t *futex;
|
---|
| 240 |
|
---|
| 241 | ASSERT(keys == 1);
|
---|
| 242 |
|
---|
| 243 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 244 | return *key == futex->paddr;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /** Callback for removal items from futex hash table.
|
---|
| 248 | *
|
---|
| 249 | * @param item Item removed from the hash table.
|
---|
| 250 | */
|
---|
| 251 | void futex_ht_remove_callback(link_t *item)
|
---|
| 252 | {
|
---|
| 253 | futex_t *futex;
|
---|
| 254 |
|
---|
| 255 | futex = hash_table_get_instance(item, futex_t, ht_link);
|
---|
| 256 | free(futex);
|
---|
| 257 | }
|
---|