source: mainline/kernel/generic/src/synch/futex.c@ 82719589

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 82719589 was c0699467, checked in by Martin Decky <martin@…>, 14 years ago

do not provide general access to kernel headers from uspace, only allow specific headers to be accessed or shared
externalize headers which serve as kernel/uspace API/ABI into a special tree

  • Property mode set to 100644
File size: 7.6 KB
RevLine 
[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 <mm/frame.h>
42#include <mm/page.h>
43#include <mm/slab.h>
[303c94c]44#include <proc/thread.h>
[4fded58]45#include <proc/task.h>
[9aa72b4]46#include <genarch/mm/page_pt.h>
47#include <genarch/mm/page_ht.h>
48#include <adt/hash_table.h>
49#include <adt/list.h>
50#include <arch.h>
51#include <align.h>
52#include <panic.h>
53#include <errno.h>
54#include <print.h>
55
56#define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */
57
58static void futex_initialize(futex_t *futex);
59
[7f1c620]60static futex_t *futex_find(uintptr_t paddr);
[96b02eb9]61static size_t futex_ht_hash(sysarg_t *key);
62static bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item);
[9aa72b4]63static void futex_ht_remove_callback(link_t *item);
64
[4fded58]65/**
[ee42e43]66 * Mutex protecting global futex hash table.
[4fded58]67 * It is also used to serialize access to all futex_t structures.
68 * Must be acquired before the task futex B+tree lock.
69 */
[ee42e43]70static mutex_t futex_ht_lock;
[9aa72b4]71
72/** Futex hash table. */
73static hash_table_t futex_ht;
74
75/** Futex hash table operations. */
76static hash_table_operations_t futex_ht_ops = {
77 .hash = futex_ht_hash,
78 .compare = futex_ht_compare,
79 .remove_callback = futex_ht_remove_callback
80};
81
82/** Initialize futex subsystem. */
83void futex_init(void)
84{
[ee42e43]85 mutex_initialize(&futex_ht_lock, MUTEX_PASSIVE);
[9aa72b4]86 hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
87}
88
89/** Initialize kernel futex structure.
90 *
[4774a32]91 * @param futex Kernel futex structure.
[9aa72b4]92 */
93void futex_initialize(futex_t *futex)
94{
95 waitq_initialize(&futex->wq);
96 link_initialize(&futex->ht_link);
97 futex->paddr = 0;
[4fded58]98 futex->refcount = 1;
[9aa72b4]99}
100
101/** Sleep in futex wait queue.
102 *
[4774a32]103 * @param uaddr Userspace address of the futex counter.
[9aa72b4]104 *
[4774a32]105 * @return If there is no physical mapping for uaddr ENOENT is
106 * returned. Otherwise returns a wait result as defined in
107 * synch.h.
[9aa72b4]108 */
[96b02eb9]109sysarg_t sys_futex_sleep(uintptr_t uaddr)
[9aa72b4]110{
111 futex_t *futex;
[7f1c620]112 uintptr_t paddr;
[9aa72b4]113 pte_t *t;
[741fd16]114 int rc;
[9aa72b4]115
116 /*
117 * Find physical address of futex counter.
118 */
119 page_table_lock(AS, true);
[0ff03f3]120 t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), false);
[9aa72b4]121 if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
122 page_table_unlock(AS, true);
[96b02eb9]123 return (sysarg_t) ENOENT;
[9aa72b4]124 }
[9dfc69a]125 paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
[9aa72b4]126 page_table_unlock(AS, true);
127
[a9ef68b]128 futex = futex_find(paddr);
[741fd16]129
130#ifdef CONFIG_UDEBUG
131 udebug_stoppable_begin();
132#endif
[4774a32]133 rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
[741fd16]134#ifdef CONFIG_UDEBUG
135 udebug_stoppable_end();
136#endif
[96b02eb9]137 return (sysarg_t) rc;
[9aa72b4]138}
139
140/** Wakeup one thread waiting in futex wait queue.
141 *
[4774a32]142 * @param uaddr Userspace address of the futex counter.
[9aa72b4]143 *
[4774a32]144 * @return ENOENT if there is no physical mapping for uaddr.
[9aa72b4]145 */
[96b02eb9]146sysarg_t sys_futex_wakeup(uintptr_t uaddr)
[9aa72b4]147{
[a9ef68b]148 futex_t *futex;
[7f1c620]149 uintptr_t paddr;
[9aa72b4]150 pte_t *t;
151
152 /*
153 * Find physical address of futex counter.
154 */
155 page_table_lock(AS, true);
[0ff03f3]156 t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), false);
[9aa72b4]157 if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
158 page_table_unlock(AS, true);
[96b02eb9]159 return (sysarg_t) ENOENT;
[9aa72b4]160 }
[9dfc69a]161 paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
[9aa72b4]162 page_table_unlock(AS, true);
163
[a9ef68b]164 futex = futex_find(paddr);
165
166 waitq_wakeup(&futex->wq, WAKEUP_FIRST);
167
168 return 0;
169}
170
171/** Find kernel address of the futex structure corresponding to paddr.
172 *
[4fded58]173 * If the structure does not exist already, a new one is created.
[a9ef68b]174 *
[4774a32]175 * @param paddr Physical address of the userspace futex counter.
[a9ef68b]176 *
[4774a32]177 * @return Address of the kernel futex structure.
[a9ef68b]178 */
[7f1c620]179futex_t *futex_find(uintptr_t paddr)
[a9ef68b]180{
181 link_t *item;
182 futex_t *futex;
[4fded58]183 btree_node_t *leaf;
[a9ef68b]184
[9aa72b4]185 /*
[a9ef68b]186 * Find the respective futex structure
187 * or allocate new one if it does not exist already.
[9aa72b4]188 */
[ee42e43]189 mutex_lock(&futex_ht_lock);
[9aa72b4]190 item = hash_table_find(&futex_ht, &paddr);
191 if (item) {
192 futex = hash_table_get_instance(item, futex_t, ht_link);
[4fded58]193
194 /*
195 * See if the current task knows this futex.
196 */
197 mutex_lock(&TASK->futexes_lock);
198 if (!btree_search(&TASK->futexes, paddr, &leaf)) {
199 /*
200 * The futex is new to the current task.
[ee42e43]201 * Upgrade its reference count and put it to the
202 * current task's B+tree of known futexes.
[4fded58]203 */
[ee42e43]204 futex->refcount++;
205 btree_insert(&TASK->futexes, paddr, futex, leaf);
[4fded58]206 }
207 mutex_unlock(&TASK->futexes_lock);
[a9ef68b]208 } else {
[ee42e43]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
[a9ef68b]214 /*
[ee42e43]215 * This is the first task referencing the futex.
216 * It can be directly inserted into its
217 * B+tree of known futexes.
[a9ef68b]218 */
[ee42e43]219 mutex_lock(&TASK->futexes_lock);
220 btree_insert(&TASK->futexes, paddr, futex, NULL);
221 mutex_unlock(&TASK->futexes_lock);
222
[a9ef68b]223 }
[ee42e43]224 mutex_unlock(&futex_ht_lock);
[9aa72b4]225
[a9ef68b]226 return futex;
[9aa72b4]227}
228
229/** Compute hash index into futex hash table.
230 *
[4774a32]231 * @param key Address where the key (i.e. physical address of futex
232 * counter) is stored.
[9aa72b4]233 *
[4774a32]234 * @return Index into futex hash table.
[9aa72b4]235 */
[96b02eb9]236size_t futex_ht_hash(sysarg_t *key)
[9aa72b4]237{
[98000fb]238 return (*key & (FUTEX_HT_SIZE - 1));
[9aa72b4]239}
240
241/** Compare futex hash table item with a key.
242 *
[4774a32]243 * @param key Address where the key (i.e. physical address of futex
244 * counter) is stored.
[9aa72b4]245 *
[4774a32]246 * @return True if the item matches the key. False otherwise.
[9aa72b4]247 */
[96b02eb9]248bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item)
[9aa72b4]249{
250 futex_t *futex;
251
252 ASSERT(keys == 1);
253
254 futex = hash_table_get_instance(item, futex_t, ht_link);
255 return *key == futex->paddr;
256}
257
258/** Callback for removal items from futex hash table.
259 *
[4774a32]260 * @param item Item removed from the hash table.
[9aa72b4]261 */
262void futex_ht_remove_callback(link_t *item)
263{
264 futex_t *futex;
265
266 futex = hash_table_get_instance(item, futex_t, ht_link);
267 free(futex);
268}
[e090e1bc]269
270/** Remove references from futexes known to the current task. */
271void futex_cleanup(void)
272{
[ee42e43]273 mutex_lock(&futex_ht_lock);
[9c1c6771]274 mutex_lock(&TASK->futexes_lock);
275
[55b77d9]276 list_foreach(TASK->futexes.leaf_list, cur) {
[9c1c6771]277 btree_node_t *node;
[6c441cf8]278 unsigned int i;
[9c1c6771]279
280 node = list_get_instance(cur, btree_node_t, leaf_link);
281 for (i = 0; i < node->keys; i++) {
282 futex_t *ftx;
[7f1c620]283 uintptr_t paddr = node->key[i];
[9c1c6771]284
285 ftx = (futex_t *) node->value[i];
286 if (--ftx->refcount == 0)
287 hash_table_remove(&futex_ht, &paddr, 1);
288 }
289 }
290
291 mutex_unlock(&TASK->futexes_lock);
[ee42e43]292 mutex_unlock(&futex_ht_lock);
[e090e1bc]293}
[b45c443]294
[cc73a8a1]295/** @}
[b45c443]296 */
Note: See TracBrowser for help on using the repository browser.