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
Line 
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/** @addtogroup sync
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Kernel backend for futexes.
36 */
37
38#include <synch/futex.h>
39#include <synch/mutex.h>
40#include <synch/spinlock.h>
41#include <mm/frame.h>
42#include <mm/page.h>
43#include <mm/slab.h>
44#include <proc/thread.h>
45#include <proc/task.h>
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
60static futex_t *futex_find(uintptr_t paddr);
61static size_t futex_ht_hash(sysarg_t *key);
62static bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item);
63static void futex_ht_remove_callback(link_t *item);
64
65/**
66 * Mutex protecting global futex hash table.
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 */
70static mutex_t futex_ht_lock;
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{
85 mutex_initialize(&futex_ht_lock, MUTEX_PASSIVE);
86 hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
87}
88
89/** Initialize kernel futex structure.
90 *
91 * @param futex Kernel futex structure.
92 */
93void futex_initialize(futex_t *futex)
94{
95 waitq_initialize(&futex->wq);
96 link_initialize(&futex->ht_link);
97 futex->paddr = 0;
98 futex->refcount = 1;
99}
100
101/** Sleep in futex wait queue.
102 *
103 * @param uaddr Userspace address of the futex counter.
104 *
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.
108 */
109sysarg_t sys_futex_sleep(uintptr_t uaddr)
110{
111 futex_t *futex;
112 uintptr_t paddr;
113 pte_t *t;
114 int rc;
115
116 /*
117 * Find physical address of futex counter.
118 */
119 page_table_lock(AS, true);
120 t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), false);
121 if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
122 page_table_unlock(AS, true);
123 return (sysarg_t) ENOENT;
124 }
125 paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
126 page_table_unlock(AS, true);
127
128 futex = futex_find(paddr);
129
130#ifdef CONFIG_UDEBUG
131 udebug_stoppable_begin();
132#endif
133 rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
134#ifdef CONFIG_UDEBUG
135 udebug_stoppable_end();
136#endif
137 return (sysarg_t) rc;
138}
139
140/** Wakeup one thread waiting in futex wait queue.
141 *
142 * @param uaddr Userspace address of the futex counter.
143 *
144 * @return ENOENT if there is no physical mapping for uaddr.
145 */
146sysarg_t sys_futex_wakeup(uintptr_t uaddr)
147{
148 futex_t *futex;
149 uintptr_t paddr;
150 pte_t *t;
151
152 /*
153 * Find physical address of futex counter.
154 */
155 page_table_lock(AS, true);
156 t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE), false);
157 if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
158 page_table_unlock(AS, true);
159 return (sysarg_t) ENOENT;
160 }
161 paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
162 page_table_unlock(AS, true);
163
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 *
173 * If the structure does not exist already, a new one is created.
174 *
175 * @param paddr Physical address of the userspace futex counter.
176 *
177 * @return Address of the kernel futex structure.
178 */
179futex_t *futex_find(uintptr_t paddr)
180{
181 link_t *item;
182 futex_t *futex;
183 btree_node_t *leaf;
184
185 /*
186 * Find the respective futex structure
187 * or allocate new one if it does not exist already.
188 */
189 mutex_lock(&futex_ht_lock);
190 item = hash_table_find(&futex_ht, &paddr);
191 if (item) {
192 futex = hash_table_get_instance(item, futex_t, ht_link);
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.
201 * Upgrade its reference count and put it to the
202 * current task's B+tree of known futexes.
203 */
204 futex->refcount++;
205 btree_insert(&TASK->futexes, paddr, futex, leaf);
206 }
207 mutex_unlock(&TASK->futexes_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
214 /*
215 * This is the first task referencing the futex.
216 * It can be directly inserted into its
217 * B+tree of known futexes.
218 */
219 mutex_lock(&TASK->futexes_lock);
220 btree_insert(&TASK->futexes, paddr, futex, NULL);
221 mutex_unlock(&TASK->futexes_lock);
222
223 }
224 mutex_unlock(&futex_ht_lock);
225
226 return futex;
227}
228
229/** Compute hash index into futex hash table.
230 *
231 * @param key Address where the key (i.e. physical address of futex
232 * counter) is stored.
233 *
234 * @return Index into futex hash table.
235 */
236size_t futex_ht_hash(sysarg_t *key)
237{
238 return (*key & (FUTEX_HT_SIZE - 1));
239}
240
241/** Compare futex hash table item with a key.
242 *
243 * @param key Address where the key (i.e. physical address of futex
244 * counter) is stored.
245 *
246 * @return True if the item matches the key. False otherwise.
247 */
248bool futex_ht_compare(sysarg_t *key, size_t keys, link_t *item)
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 *
260 * @param item Item removed from the hash table.
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}
269
270/** Remove references from futexes known to the current task. */
271void futex_cleanup(void)
272{
273 mutex_lock(&futex_ht_lock);
274 mutex_lock(&TASK->futexes_lock);
275
276 list_foreach(TASK->futexes.leaf_list, cur) {
277 btree_node_t *node;
278 unsigned int i;
279
280 node = list_get_instance(cur, btree_node_t, leaf_link);
281 for (i = 0; i < node->keys; i++) {
282 futex_t *ftx;
283 uintptr_t paddr = node->key[i];
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);
292 mutex_unlock(&futex_ht_lock);
293}
294
295/** @}
296 */
Note: See TracBrowser for help on using the repository browser.