Rev 1109 | Rev 1153 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1109 | Rev 1117 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Jakub Jermar |
2 | * Copyright (C) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** |
29 | /** |
30 | * Kernel backend for futexes. |
30 | * Kernel backend for futexes. |
31 | * Deallocation of orphaned kernel-side futex structures is not currently implemented. |
31 | * Deallocation of orphaned kernel-side futex structures is not currently implemented. |
32 | * Timeouting futexes are currently not implemented. |
- | |
33 | */ |
32 | */ |
34 | 33 | ||
35 | #include <synch/futex.h> |
34 | #include <synch/futex.h> |
36 | #include <synch/rwlock.h> |
35 | #include <synch/rwlock.h> |
- | 36 | #include <synch/spinlock.h> |
|
37 | #include <synch/synch.h> |
37 | #include <synch/synch.h> |
38 | #include <mm/frame.h> |
38 | #include <mm/frame.h> |
39 | #include <mm/page.h> |
39 | #include <mm/page.h> |
40 | #include <mm/slab.h> |
40 | #include <mm/slab.h> |
- | 41 | #include <proc/thread.h> |
|
41 | #include <genarch/mm/page_pt.h> |
42 | #include <genarch/mm/page_pt.h> |
42 | #include <genarch/mm/page_ht.h> |
43 | #include <genarch/mm/page_ht.h> |
43 | #include <adt/hash_table.h> |
44 | #include <adt/hash_table.h> |
44 | #include <adt/list.h> |
45 | #include <adt/list.h> |
45 | #include <arch.h> |
46 | #include <arch.h> |
46 | #include <align.h> |
47 | #include <align.h> |
47 | #include <panic.h> |
48 | #include <panic.h> |
48 | #include <errno.h> |
49 | #include <errno.h> |
49 | #include <print.h> |
50 | #include <print.h> |
50 | 51 | ||
51 | #define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */ |
52 | #define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */ |
52 | 53 | ||
53 | static void futex_initialize(futex_t *futex); |
54 | static void futex_initialize(futex_t *futex); |
54 | 55 | ||
55 | static index_t futex_ht_hash(__native *key); |
56 | static index_t futex_ht_hash(__native *key); |
56 | static bool futex_ht_compare(__native *key, count_t keys, link_t *item); |
57 | static bool futex_ht_compare(__native *key, count_t keys, link_t *item); |
57 | static void futex_ht_remove_callback(link_t *item); |
58 | static void futex_ht_remove_callback(link_t *item); |
58 | 59 | ||
59 | /** Read-write lock protecting global futex hash table. */ |
60 | /** Read-write lock protecting global futex hash table. */ |
60 | static rwlock_t futex_ht_lock; |
61 | static rwlock_t futex_ht_lock; |
61 | 62 | ||
62 | /** Futex hash table. */ |
63 | /** Futex hash table. */ |
63 | static hash_table_t futex_ht; |
64 | static hash_table_t futex_ht; |
64 | 65 | ||
65 | /** Futex hash table operations. */ |
66 | /** Futex hash table operations. */ |
66 | static hash_table_operations_t futex_ht_ops = { |
67 | static hash_table_operations_t futex_ht_ops = { |
67 | .hash = futex_ht_hash, |
68 | .hash = futex_ht_hash, |
68 | .compare = futex_ht_compare, |
69 | .compare = futex_ht_compare, |
69 | .remove_callback = futex_ht_remove_callback |
70 | .remove_callback = futex_ht_remove_callback |
70 | }; |
71 | }; |
71 | 72 | ||
72 | /** Initialize futex subsystem. */ |
73 | /** Initialize futex subsystem. */ |
73 | void futex_init(void) |
74 | void futex_init(void) |
74 | { |
75 | { |
75 | rwlock_initialize(&futex_ht_lock); |
76 | rwlock_initialize(&futex_ht_lock); |
76 | hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops); |
77 | hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops); |
77 | } |
78 | } |
78 | 79 | ||
79 | /** Initialize kernel futex structure. |
80 | /** Initialize kernel futex structure. |
80 | * |
81 | * |
81 | * @param futex Kernel futex structure. |
82 | * @param futex Kernel futex structure. |
82 | */ |
83 | */ |
83 | void futex_initialize(futex_t *futex) |
84 | void futex_initialize(futex_t *futex) |
84 | { |
85 | { |
85 | waitq_initialize(&futex->wq); |
86 | waitq_initialize(&futex->wq); |
86 | link_initialize(&futex->ht_link); |
87 | link_initialize(&futex->ht_link); |
87 | futex->paddr = 0; |
88 | futex->paddr = 0; |
88 | } |
89 | } |
89 | 90 | ||
90 | /** Sleep in futex wait queue. |
91 | /** Sleep in futex wait queue. |
91 | * |
92 | * |
92 | * @param uaddr Userspace address of the futex counter. |
93 | * @param uaddr Userspace address of the futex counter. |
- | 94 | * @param usec If non-zero, number of microseconds this thread is willing to sleep. |
|
- | 95 | * @param trydown If usec is zero and trydown is non-zero, conditional operation will be attempted. |
|
93 | * |
96 | * |
94 | * @return One of ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED. See synch.h. |
97 | * @return One of ESYNCH_TIMEOUT, ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED. See synch.h. |
95 | * If there is no physical mapping for uaddr ENOENT is returned. |
98 | * If there is no physical mapping for uaddr ENOENT is returned. |
96 | */ |
99 | */ |
97 | __native sys_futex_sleep(__address uaddr) |
100 | __native sys_futex_sleep_timeout(__address uaddr, __u32 usec, int trydown) |
98 | { |
101 | { |
99 | futex_t *futex; |
102 | futex_t *futex; |
100 | __address paddr; |
103 | __address paddr; |
101 | link_t *item; |
104 | link_t *item; |
102 | pte_t *t; |
105 | pte_t *t; |
- | 106 | ipl_t ipl; |
|
103 | 107 | ||
- | 108 | ipl = interrupts_disable(); |
|
- | 109 | ||
104 | /* |
110 | /* |
105 | * Find physical address of futex counter. |
111 | * Find physical address of futex counter. |
106 | */ |
112 | */ |
107 | page_table_lock(AS, true); |
113 | page_table_lock(AS, true); |
108 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
114 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
109 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
115 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
110 | page_table_unlock(AS, true); |
116 | page_table_unlock(AS, true); |
- | 117 | interrupts_restore(ipl); |
|
111 | return (__native) ENOENT; |
118 | return (__native) ENOENT; |
112 | } |
119 | } |
113 | paddr = PFN2ADDR(PTE_GET_FRAME(t)) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
120 | paddr = PFN2ADDR(PTE_GET_FRAME(t)) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
114 | page_table_unlock(AS, true); |
121 | page_table_unlock(AS, true); |
115 | 122 | ||
- | 123 | interrupts_restore(ipl); |
|
- | 124 | ||
116 | /* |
125 | /* |
117 | * Find the respective futex structure |
126 | * Find the respective futex structure |
118 | * or allocate new one if it does not exist already. |
127 | * or allocate new one if it does not exist already. |
119 | */ |
128 | */ |
120 | rwlock_read_lock(&futex_ht_lock); |
129 | rwlock_read_lock(&futex_ht_lock); |
121 | item = hash_table_find(&futex_ht, &paddr); |
130 | item = hash_table_find(&futex_ht, &paddr); |
122 | if (item) { |
131 | if (item) { |
123 | futex = hash_table_get_instance(item, futex_t, ht_link); |
132 | futex = hash_table_get_instance(item, futex_t, ht_link); |
124 | rwlock_read_unlock(&futex_ht_lock); |
133 | rwlock_read_unlock(&futex_ht_lock); |
125 | } else { |
134 | } else { |
126 | /* |
135 | /* |
127 | * Upgrade to writer is not currently supported, |
136 | * Upgrade to writer is not currently supported, |
128 | * Therefore, it is necessary to release the read lock |
137 | * Therefore, it is necessary to release the read lock |
129 | * and reacquire it as a writer. |
138 | * and reacquire it as a writer. |
130 | */ |
139 | */ |
131 | rwlock_read_unlock(&futex_ht_lock); |
140 | rwlock_read_unlock(&futex_ht_lock); |
132 | 141 | ||
133 | rwlock_write_lock(&futex_ht_lock); |
142 | rwlock_write_lock(&futex_ht_lock); |
134 | /* |
143 | /* |
135 | * Detect possible race condition by searching |
144 | * Detect possible race condition by searching |
136 | * the hash table once again with write access. |
145 | * the hash table once again with write access. |
137 | */ |
146 | */ |
138 | item = hash_table_find(&futex_ht, &paddr); |
147 | item = hash_table_find(&futex_ht, &paddr); |
139 | if (item) { |
148 | if (item) { |
140 | futex = hash_table_get_instance(item, futex_t, ht_link); |
149 | futex = hash_table_get_instance(item, futex_t, ht_link); |
141 | rwlock_write_unlock(&futex_ht_lock); |
150 | rwlock_write_unlock(&futex_ht_lock); |
142 | } else { |
151 | } else { |
143 | futex = (futex_t *) malloc(sizeof(futex_t), 0); |
152 | futex = (futex_t *) malloc(sizeof(futex_t), 0); |
144 | futex_initialize(futex); |
153 | futex_initialize(futex); |
145 | futex->paddr = paddr; |
154 | futex->paddr = paddr; |
146 | hash_table_insert(&futex_ht, &paddr, &futex->ht_link); |
155 | hash_table_insert(&futex_ht, &paddr, &futex->ht_link); |
147 | rwlock_write_unlock(&futex_ht_lock); |
156 | rwlock_write_unlock(&futex_ht_lock); |
148 | } |
157 | } |
149 | } |
158 | } |
150 | 159 | ||
151 | return (__native) waitq_sleep(&futex->wq); |
160 | return (__native) waitq_sleep_timeout(&futex->wq, usec, trydown); |
152 | } |
161 | } |
153 | 162 | ||
154 | /** Wakeup one thread waiting in futex wait queue. |
163 | /** Wakeup one thread waiting in futex wait queue. |
155 | * |
164 | * |
156 | * @param uaddr Userspace address of the futex counter. |
165 | * @param uaddr Userspace address of the futex counter. |
157 | * |
166 | * |
158 | * @return ENOENT if there is no futex associated with the address |
167 | * @return ENOENT if there is no futex associated with the address |
159 | * or if there is no physical mapping for uaddr. |
168 | * or if there is no physical mapping for uaddr. |
160 | */ |
169 | */ |
161 | __native sys_futex_wakeup(__address uaddr) |
170 | __native sys_futex_wakeup(__address uaddr) |
162 | { |
171 | { |
163 | futex_t *futex = NULL; |
172 | futex_t *futex = NULL; |
164 | __address paddr; |
173 | __address paddr; |
165 | link_t *item; |
174 | link_t *item; |
166 | pte_t *t; |
175 | pte_t *t; |
- | 176 | ipl_t ipl; |
|
- | 177 | ||
- | 178 | ipl = interrupts_disable(); |
|
167 | 179 | ||
168 | /* |
180 | /* |
169 | * Find physical address of futex counter. |
181 | * Find physical address of futex counter. |
170 | */ |
182 | */ |
171 | page_table_lock(AS, true); |
183 | page_table_lock(AS, true); |
172 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
184 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
173 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
185 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
174 | page_table_unlock(AS, true); |
186 | page_table_unlock(AS, true); |
- | 187 | interrupts_restore(ipl); |
|
175 | return (__native) ENOENT; |
188 | return (__native) ENOENT; |
176 | } |
189 | } |
177 | paddr = PFN2ADDR(PTE_GET_FRAME(t)) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
190 | paddr = PFN2ADDR(PTE_GET_FRAME(t)) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
178 | page_table_unlock(AS, true); |
191 | page_table_unlock(AS, true); |
179 | 192 | ||
- | 193 | interrupts_restore(ipl); |
|
- | 194 | ||
180 | /* |
195 | /* |
181 | * Find the respective futex structure. |
196 | * Find the respective futex structure. |
182 | */ |
197 | */ |
183 | rwlock_read_lock(&futex_ht_lock); |
198 | rwlock_read_lock(&futex_ht_lock); |
184 | item = hash_table_find(&futex_ht, &paddr); |
199 | item = hash_table_find(&futex_ht, &paddr); |
185 | if (item) { |
200 | if (item) { |
186 | futex = hash_table_get_instance(item, futex_t, ht_link); |
201 | futex = hash_table_get_instance(item, futex_t, ht_link); |
187 | } |
202 | } |
188 | rwlock_read_unlock(&futex_ht_lock); |
203 | rwlock_read_unlock(&futex_ht_lock); |
189 | 204 | ||
190 | if (!futex) |
205 | if (!futex) |
191 | return (__native) ENOENT; |
206 | return (__native) ENOENT; |
192 | 207 | ||
193 | waitq_wakeup(&futex->wq, WAKEUP_FIRST); |
208 | waitq_wakeup(&futex->wq, WAKEUP_FIRST); |
194 | 209 | ||
195 | return 0; |
210 | return 0; |
196 | } |
211 | } |
197 | 212 | ||
198 | /** Compute hash index into futex hash table. |
213 | /** Compute hash index into futex hash table. |
199 | * |
214 | * |
200 | * @param key Address where the key (i.e. physical address of futex counter) is stored. |
215 | * @param key Address where the key (i.e. physical address of futex counter) is stored. |
201 | * |
216 | * |
202 | * @return Index into futex hash table. |
217 | * @return Index into futex hash table. |
203 | */ |
218 | */ |
204 | index_t futex_ht_hash(__native *key) |
219 | index_t futex_ht_hash(__native *key) |
205 | { |
220 | { |
206 | return *key & (FUTEX_HT_SIZE-1); |
221 | return *key & (FUTEX_HT_SIZE-1); |
207 | } |
222 | } |
208 | 223 | ||
209 | /** Compare futex hash table item with a key. |
224 | /** Compare futex hash table item with a key. |
210 | * |
225 | * |
211 | * @param key Address where the key (i.e. physical address of futex counter) is stored. |
226 | * @param key Address where the key (i.e. physical address of futex counter) is stored. |
212 | * |
227 | * |
213 | * @return True if the item matches the key. False otherwise. |
228 | * @return True if the item matches the key. False otherwise. |
214 | */ |
229 | */ |
215 | bool futex_ht_compare(__native *key, count_t keys, link_t *item) |
230 | bool futex_ht_compare(__native *key, count_t keys, link_t *item) |
216 | { |
231 | { |
217 | futex_t *futex; |
232 | futex_t *futex; |
218 | 233 | ||
219 | ASSERT(keys == 1); |
234 | ASSERT(keys == 1); |
220 | 235 | ||
221 | futex = hash_table_get_instance(item, futex_t, ht_link); |
236 | futex = hash_table_get_instance(item, futex_t, ht_link); |
222 | return *key == futex->paddr; |
237 | return *key == futex->paddr; |
223 | } |
238 | } |
224 | 239 | ||
225 | /** Callback for removal items from futex hash table. |
240 | /** Callback for removal items from futex hash table. |
226 | * |
241 | * |
227 | * @param item Item removed from the hash table. |
242 | * @param item Item removed from the hash table. |
228 | */ |
243 | */ |
229 | void futex_ht_remove_callback(link_t *item) |
244 | void futex_ht_remove_callback(link_t *item) |
230 | { |
245 | { |
231 | futex_t *futex; |
246 | futex_t *futex; |
232 | 247 | ||
233 | futex = hash_table_get_instance(item, futex_t, ht_link); |
248 | futex = hash_table_get_instance(item, futex_t, ht_link); |
234 | free(futex); |
249 | free(futex); |
235 | } |
250 | } |
236 | 251 |