Subversion Repositories HelenOS-historic

Rev

Rev 1117 | Rev 1229 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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