Subversion Repositories HelenOS-historic

Rev

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

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