Subversion Repositories HelenOS-historic

Rev

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

Rev 1579 Rev 1580
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 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    task.c
30
 * @file    task.c
31
 * @brief   Task management.
31
 * @brief   Task management.
32
 */
32
 */
33
 
33
 
34
#include <main/uinit.h>
34
#include <main/uinit.h>
35
#include <proc/thread.h>
35
#include <proc/thread.h>
36
#include <proc/task.h>
36
#include <proc/task.h>
37
#include <proc/uarg.h>
37
#include <proc/uarg.h>
38
#include <mm/as.h>
38
#include <mm/as.h>
39
#include <mm/slab.h>
39
#include <mm/slab.h>
40
#include <synch/spinlock.h>
40
#include <synch/spinlock.h>
41
#include <arch.h>
41
#include <arch.h>
42
#include <panic.h>
42
#include <panic.h>
43
#include <adt/btree.h>
43
#include <adt/btree.h>
44
#include <adt/list.h>
44
#include <adt/list.h>
45
#include <ipc/ipc.h>
45
#include <ipc/ipc.h>
46
#include <security/cap.h>
46
#include <security/cap.h>
47
#include <memstr.h>
47
#include <memstr.h>
48
#include <print.h>
48
#include <print.h>
49
#include <elf.h>
49
#include <elf.h>
50
#include <errno.h>
50
#include <errno.h>
51
#include <syscall/copy.h>
51
#include <syscall/copy.h>
52
 
52
 
53
#ifndef LOADED_PROG_STACK_PAGES_NO
53
#ifndef LOADED_PROG_STACK_PAGES_NO
54
#define LOADED_PROG_STACK_PAGES_NO 1
54
#define LOADED_PROG_STACK_PAGES_NO 1
55
#endif
55
#endif
56
 
56
 
57
SPINLOCK_INITIALIZE(tasks_lock);
57
SPINLOCK_INITIALIZE(tasks_lock);
58
btree_t tasks_btree;
58
btree_t tasks_btree;
59
static task_id_t task_counter = 0;
59
static task_id_t task_counter = 0;
60
 
60
 
61
static void ktask_cleanup(void *);
61
static void ktaskclnp(void *);
62
 
62
 
63
/** Initialize tasks
63
/** Initialize tasks
64
 *
64
 *
65
 * Initialize kernel tasks support.
65
 * Initialize kernel tasks support.
66
 *
66
 *
67
 */
67
 */
68
void task_init(void)
68
void task_init(void)
69
{
69
{
70
    TASK = NULL;
70
    TASK = NULL;
71
    btree_create(&tasks_btree);
71
    btree_create(&tasks_btree);
72
}
72
}
73
 
73
 
74
 
74
 
75
/** Create new task
75
/** Create new task
76
 *
76
 *
77
 * Create new task with no threads.
77
 * Create new task with no threads.
78
 *
78
 *
79
 * @param as Task's address space.
79
 * @param as Task's address space.
80
 * @param name Symbolic name.
80
 * @param name Symbolic name.
81
 *
81
 *
82
 * @return New task's structure
82
 * @return New task's structure
83
 *
83
 *
84
 */
84
 */
85
task_t *task_create(as_t *as, char *name)
85
task_t *task_create(as_t *as, char *name)
86
{
86
{
87
    ipl_t ipl;
87
    ipl_t ipl;
88
    task_t *ta;
88
    task_t *ta;
89
    int i;
89
    int i;
90
   
90
   
91
    ta = (task_t *) malloc(sizeof(task_t), 0);
91
    ta = (task_t *) malloc(sizeof(task_t), 0);
92
 
92
 
93
    task_create_arch(ta);
93
    task_create_arch(ta);
94
 
94
 
95
    spinlock_initialize(&ta->lock, "task_ta_lock");
95
    spinlock_initialize(&ta->lock, "task_ta_lock");
96
    list_initialize(&ta->th_head);
96
    list_initialize(&ta->th_head);
97
    ta->as = as;
97
    ta->as = as;
98
    ta->name = name;
98
    ta->name = name;
99
 
99
 
100
    ta->refcount = 0;
100
    ta->refcount = 0;
101
 
101
 
102
    ta->capabilities = 0;
102
    ta->capabilities = 0;
103
    ta->accept_new_threads = true;
103
    ta->accept_new_threads = true;
104
   
104
   
105
    ipc_answerbox_init(&ta->answerbox);
105
    ipc_answerbox_init(&ta->answerbox);
106
    for (i=0; i < IPC_MAX_PHONES;i++)
106
    for (i=0; i < IPC_MAX_PHONES;i++)
107
        ipc_phone_init(&ta->phones[i]);
107
        ipc_phone_init(&ta->phones[i]);
108
    if (ipc_phone_0)
108
    if (ipc_phone_0)
109
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
109
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
110
    atomic_set(&ta->active_calls, 0);
110
    atomic_set(&ta->active_calls, 0);
111
 
111
 
112
    mutex_initialize(&ta->futexes_lock);
112
    mutex_initialize(&ta->futexes_lock);
113
    btree_create(&ta->futexes);
113
    btree_create(&ta->futexes);
114
   
114
   
115
    ipl = interrupts_disable();
115
    ipl = interrupts_disable();
116
 
116
 
117
    /*
117
    /*
118
     * Increment address space reference count.
118
     * Increment address space reference count.
119
     * TODO: Reconsider the locking scheme.
119
     * TODO: Reconsider the locking scheme.
120
     */
120
     */
121
    mutex_lock(&as->lock);
121
    mutex_lock(&as->lock);
122
    as->refcount++;
122
    as->refcount++;
123
    mutex_unlock(&as->lock);
123
    mutex_unlock(&as->lock);
124
 
124
 
125
    spinlock_lock(&tasks_lock);
125
    spinlock_lock(&tasks_lock);
126
 
126
 
127
    ta->taskid = ++task_counter;
127
    ta->taskid = ++task_counter;
128
    btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
128
    btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
129
 
129
 
130
    spinlock_unlock(&tasks_lock);
130
    spinlock_unlock(&tasks_lock);
131
    interrupts_restore(ipl);
131
    interrupts_restore(ipl);
132
 
132
 
133
    return ta;
133
    return ta;
134
}
134
}
135
 
135
 
136
/** Destroy task.
136
/** Destroy task.
137
 *
137
 *
138
 * @param t Task to be destroyed.
138
 * @param t Task to be destroyed.
139
 */
139
 */
140
void task_destroy(task_t *t)
140
void task_destroy(task_t *t)
141
{
141
{
142
}
142
}
143
 
143
 
144
/** Create new task with 1 thread and run it
144
/** Create new task with 1 thread and run it
145
 *
145
 *
146
 * @param program_addr Address of program executable image.
146
 * @param program_addr Address of program executable image.
147
 * @param name Program name.
147
 * @param name Program name.
148
 *
148
 *
149
 * @return Task of the running program or NULL on error.
149
 * @return Task of the running program or NULL on error.
150
 */
150
 */
151
task_t * task_run_program(void *program_addr, char *name)
151
task_t * task_run_program(void *program_addr, char *name)
152
{
152
{
153
    as_t *as;
153
    as_t *as;
154
    as_area_t *a;
154
    as_area_t *a;
155
    int rc;
155
    int rc;
156
    thread_t *t;
156
    thread_t *t;
157
    task_t *task;
157
    task_t *task;
158
    uspace_arg_t *kernel_uarg;
158
    uspace_arg_t *kernel_uarg;
159
 
159
 
160
    as = as_create(0);
160
    as = as_create(0);
161
    ASSERT(as);
161
    ASSERT(as);
162
 
162
 
163
    rc = elf_load((elf_header_t *) program_addr, as);
163
    rc = elf_load((elf_header_t *) program_addr, as);
164
    if (rc != EE_OK) {
164
    if (rc != EE_OK) {
165
        as_destroy(as);
165
        as_destroy(as);
166
        return NULL;
166
        return NULL;
167
    }
167
    }
168
   
168
   
169
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
169
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
170
    kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
170
    kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
171
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
171
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
172
    kernel_uarg->uspace_thread_function = NULL;
172
    kernel_uarg->uspace_thread_function = NULL;
173
    kernel_uarg->uspace_thread_arg = NULL;
173
    kernel_uarg->uspace_thread_arg = NULL;
174
    kernel_uarg->uspace_uarg = NULL;
174
    kernel_uarg->uspace_uarg = NULL;
175
   
175
   
176
    task = task_create(as, name);
176
    task = task_create(as, name);
177
    ASSERT(task);
177
    ASSERT(task);
178
 
178
 
179
    /*
179
    /*
180
     * Create the data as_area.
180
     * Create the data as_area.
181
     */
181
     */
182
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
182
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
183
        LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
183
        LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
184
        USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
184
        USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
185
 
185
 
186
    t = thread_create(uinit, kernel_uarg, task, 0, "uinit");
186
    t = thread_create(uinit, kernel_uarg, task, 0, "uinit");
187
    ASSERT(t);
187
    ASSERT(t);
188
    thread_ready(t);
188
    thread_ready(t);
189
   
189
   
190
    return task;
190
    return task;
191
}
191
}
192
 
192
 
193
/** Syscall for reading task ID from userspace.
193
/** Syscall for reading task ID from userspace.
194
 *
194
 *
195
 * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
195
 * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
196
 *
196
 *
197
 * @return 0 on success or an error code from @ref errno.h.
197
 * @return 0 on success or an error code from @ref errno.h.
198
 */
198
 */
199
__native sys_task_get_id(task_id_t *uspace_task_id)
199
__native sys_task_get_id(task_id_t *uspace_task_id)
200
{
200
{
201
    /*
201
    /*
202
     * No need to acquire lock on TASK because taskid
202
     * No need to acquire lock on TASK because taskid
203
     * remains constant for the lifespan of the task.
203
     * remains constant for the lifespan of the task.
204
     */
204
     */
205
    return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
205
    return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
206
}
206
}
207
 
207
 
208
/** Find task structure corresponding to task ID.
208
/** Find task structure corresponding to task ID.
209
 *
209
 *
210
 * The tasks_lock must be already held by the caller of this function
210
 * The tasks_lock must be already held by the caller of this function
211
 * and interrupts must be disabled.
211
 * and interrupts must be disabled.
212
 *
212
 *
213
 * @param id Task ID.
213
 * @param id Task ID.
214
 *
214
 *
215
 * @return Task structure address or NULL if there is no such task ID.
215
 * @return Task structure address or NULL if there is no such task ID.
216
 */
216
 */
217
task_t *task_find_by_id(task_id_t id)
217
task_t *task_find_by_id(task_id_t id)
218
{
218
{
219
    btree_node_t *leaf;
219
    btree_node_t *leaf;
220
   
220
   
221
    return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
221
    return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
222
}
222
}
223
 
223
 
224
/** Kill task.
224
/** Kill task.
225
 *
225
 *
226
 * @param id ID of the task to be killed.
226
 * @param id ID of the task to be killed.
227
 *
227
 *
228
 * @return 0 on success or an error code from errno.h
228
 * @return 0 on success or an error code from errno.h
229
 */
229
 */
230
int task_kill(task_id_t id)
230
int task_kill(task_id_t id)
231
{
231
{
232
    ipl_t ipl;
232
    ipl_t ipl;
233
    task_t *ta;
233
    task_t *ta;
234
    thread_t *t;
234
    thread_t *t;
235
    link_t *cur;
235
    link_t *cur;
236
   
236
   
237
    ipl = interrupts_disable();
237
    ipl = interrupts_disable();
238
    spinlock_lock(&tasks_lock);
238
    spinlock_lock(&tasks_lock);
239
 
239
 
240
    if (!(ta = task_find_by_id(id))) {
240
    if (!(ta = task_find_by_id(id))) {
241
        spinlock_unlock(&tasks_lock);
241
        spinlock_unlock(&tasks_lock);
242
        interrupts_restore(ipl);
242
        interrupts_restore(ipl);
243
        return ENOENT;
243
        return ENOENT;
244
    }
244
    }
245
   
245
   
246
    spinlock_lock(&ta->lock);
246
    spinlock_lock(&ta->lock);
247
    ta->refcount++;
247
    ta->refcount++;
248
    spinlock_unlock(&ta->lock);
248
    spinlock_unlock(&ta->lock);
249
   
249
   
250
    t = thread_create(ktask_cleanup, NULL, ta, 0, "ktask_cleanup");
250
    t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
251
   
251
   
252
    spinlock_lock(&ta->lock);
252
    spinlock_lock(&ta->lock);
-
 
253
    ta->accept_new_threads = false;
253
    ta->refcount--;
254
    ta->refcount--;
254
   
255
   
255
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
256
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
256
        thread_t *thr;
257
        thread_t *thr;
257
        bool  sleeping = false;
258
        bool  sleeping = false;
258
       
259
       
259
        thr = list_get_instance(cur, thread_t, th_link);
260
        thr = list_get_instance(cur, thread_t, th_link);
260
        if (thr == t)
261
        if (thr == t)
261
            continue;
262
            continue;
262
           
263
           
263
        spinlock_lock(&thr->lock);
264
        spinlock_lock(&thr->lock);
264
        thr->interrupted = true;
265
        thr->interrupted = true;
265
        if (thr->state == Sleeping)
266
        if (thr->state == Sleeping)
266
            sleeping = true;
267
            sleeping = true;
267
        spinlock_unlock(&thr->lock);
268
        spinlock_unlock(&thr->lock);
268
       
269
       
269
        if (sleeping)
270
        if (sleeping)
270
            waitq_interrupt_sleep(thr);
271
            waitq_interrupt_sleep(thr);
271
    }
272
    }
272
   
273
   
-
 
274
    spinlock_unlock(&ta->lock);
273
    thread_ready(t);
275
    interrupts_restore(ipl);
274
   
276
   
-
 
277
    if (t)
-
 
278
        thread_ready(t);
-
 
279
 
275
    return 0;
280
    return 0;
276
}
281
}
277
 
282
 
278
/** Print task list */
283
/** Print task list */
279
void task_print_list(void)
284
void task_print_list(void)
280
{
285
{
281
    link_t *cur;
286
    link_t *cur;
282
    ipl_t ipl;
287
    ipl_t ipl;
283
   
288
   
284
    /* Messing with thread structures, avoid deadlock */
289
    /* Messing with thread structures, avoid deadlock */
285
    ipl = interrupts_disable();
290
    ipl = interrupts_disable();
286
    spinlock_lock(&tasks_lock);
291
    spinlock_lock(&tasks_lock);
287
 
292
 
288
    for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
293
    for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
289
        btree_node_t *node;
294
        btree_node_t *node;
290
        int i;
295
        int i;
291
       
296
       
292
        node = list_get_instance(cur, btree_node_t, leaf_link);
297
        node = list_get_instance(cur, btree_node_t, leaf_link);
293
        for (i = 0; i < node->keys; i++) {
298
        for (i = 0; i < node->keys; i++) {
294
            task_t *t;
299
            task_t *t;
295
            int j;
300
            int j;
296
 
301
 
297
            t = (task_t *) node->value[i];
302
            t = (task_t *) node->value[i];
298
       
303
       
299
            spinlock_lock(&t->lock);
304
            spinlock_lock(&t->lock);
300
            printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
305
            printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
301
                t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
306
                t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
302
            for (j=0; j < IPC_MAX_PHONES; j++) {
307
            for (j=0; j < IPC_MAX_PHONES; j++) {
303
                if (t->phones[j].callee)
308
                if (t->phones[j].callee)
304
                    printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
309
                    printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
305
            }
310
            }
306
            printf("\n");
311
            printf("\n");
307
            spinlock_unlock(&t->lock);
312
            spinlock_unlock(&t->lock);
308
        }
313
        }
309
    }
314
    }
310
 
315
 
311
    spinlock_unlock(&tasks_lock);
316
    spinlock_unlock(&tasks_lock);
312
    interrupts_restore(ipl);
317
    interrupts_restore(ipl);
313
}
318
}
314
 
319
 
315
/** Kernel thread used to cleanup the task. */
320
/** Kernel thread used to cleanup the task. */
316
void ktask_cleanup(void *arg)
321
void ktaskclnp(void *arg)
317
{
322
{
-
 
323
    ipl_t ipl;
-
 
324
    thread_t *t = NULL;
-
 
325
    link_t *cur;
-
 
326
 
-
 
327
    thread_detach(THREAD);
-
 
328
 
-
 
329
loop:
-
 
330
    ipl = interrupts_disable();
-
 
331
    spinlock_lock(&TASK->lock);
-
 
332
   
-
 
333
    /*
-
 
334
     * Find a thread to join.
-
 
335
     */
-
 
336
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
-
 
337
        t = list_get_instance(cur, thread_t, th_link);
-
 
338
        if (t == THREAD)
-
 
339
            continue;
-
 
340
        else
-
 
341
            break;
-
 
342
    }
-
 
343
   
-
 
344
    spinlock_unlock(&TASK->lock);
-
 
345
    interrupts_restore(ipl);
-
 
346
   
-
 
347
    if (t != THREAD) {
-
 
348
        thread_join(t);
-
 
349
        thread_detach(t);
-
 
350
        goto loop;
-
 
351
    }
-
 
352
   
-
 
353
    /*
-
 
354
     * Now there are no other threads in this task
-
 
355
     * and no new threads can be created.
-
 
356
     */
-
 
357
   
318
    /*
358
    /*
319
     * TODO:
359
     * TODO:
320
     * Wait until it is save to cleanup the task (i.e. all other threads exit)
-
 
321
     * and do the cleanup (e.g. close IPC communication and release used futexes).
360
     * Close IPC communication and release used futexes.
322
     * When this thread exits, the task refcount drops to zero and the task structure is
361
     * When this thread exits, the task refcount drops to zero and the task structure is
323
     * cleaned.
362
     * cleaned.
324
     */
363
     */
325
}
364
}
326
 
365