source: mainline/kernel/generic/src/proc/task.c@ 002e613

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 002e613 was cfffb290, checked in by Martin Decky <martin@…>, 19 years ago

finish security context isolation

  • Property mode set to 100644
File size: 11.3 KB
RevLine 
[f761f1eb]1/*
2 * Copyright (C) 2001-2004 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
[cc73a8a1]29/** @addtogroup genericproc
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[b45c443]34 * @file
[9179d0a]35 * @brief Task management.
36 */
37
[5be1923]38#include <main/uinit.h>
[f761f1eb]39#include <proc/thread.h>
40#include <proc/task.h>
[0f250f9]41#include <proc/uarg.h>
[20d50a1]42#include <mm/as.h>
[085d973]43#include <mm/slab.h>
[f761f1eb]44#include <synch/spinlock.h>
45#include <arch.h>
46#include <panic.h>
[7f6e755]47#include <adt/btree.h>
[5c9a08b]48#include <adt/list.h>
[6d9c49a]49#include <ipc/ipc.h>
[1077d91]50#include <security/cap.h>
[6d9c49a]51#include <memstr.h>
[37c57f2]52#include <print.h>
[5be1923]53#include <elf.h>
[7509ddc]54#include <errno.h>
[e3c762cd]55#include <syscall/copy.h>
[0dbc4e7]56#include <console/klog.h>
[8e5e78f]57
[a84af84]58#ifndef LOADED_PROG_STACK_PAGES_NO
59#define LOADED_PROG_STACK_PAGES_NO 1
60#endif
[8e5e78f]61
[88169d9]62/** Spinlock protecting the tasks_btree B+tree. */
[dc747e3]63SPINLOCK_INITIALIZE(tasks_lock);
[88169d9]64
65/** B+tree of active tasks.
66 *
67 * The task is guaranteed to exist after it was found in the tasks_btree as long as:
68 * @li the tasks_lock is held,
69 * @li the task's lock is held when task's lock is acquired before releasing tasks_lock or
70 * @li the task's refcount is grater than 0
71 *
72 */
[7f6e755]73btree_t tasks_btree;
[88169d9]74
[286e03d]75static task_id_t task_counter = 0;
[70527f1]76
[b91bb65]77static void ktaskclnp(void *arg);
[48e7dd6]78static void ktaskgc(void *arg);
[7509ddc]79
[70527f1]80/** Initialize tasks
81 *
82 * Initialize kernel tasks support.
83 *
84 */
[f761f1eb]85void task_init(void)
86{
[43114c5]87 TASK = NULL;
[7f6e755]88 btree_create(&tasks_btree);
[f761f1eb]89}
90
[70527f1]91
92/** Create new task
93 *
94 * Create new task with no threads.
95 *
[20d50a1]96 * @param as Task's address space.
[ff14c520]97 * @param name Symbolic name.
[70527f1]98 *
[5be1923]99 * @return New task's structure
[70527f1]100 *
101 */
[ff14c520]102task_t *task_create(as_t *as, char *name)
[f761f1eb]103{
[22f7769]104 ipl_t ipl;
[f761f1eb]105 task_t *ta;
[2ba7810]106 int i;
[f761f1eb]107
[bb68433]108 ta = (task_t *) malloc(sizeof(task_t), 0);
109
[963074b3]110 task_create_arch(ta);
111
[bb68433]112 spinlock_initialize(&ta->lock, "task_ta_lock");
113 list_initialize(&ta->th_head);
114 ta->as = as;
[ff14c520]115 ta->name = name;
[b91bb65]116 ta->main_thread = NULL;
[7509ddc]117 ta->refcount = 0;
[cfffb290]118 ta->context = CONTEXT;
[7509ddc]119
[1077d91]120 ta->capabilities = 0;
[7509ddc]121 ta->accept_new_threads = true;
[2ba7810]122
[6d9c49a]123 ipc_answerbox_init(&ta->answerbox);
[cfffb290]124 for (i = 0; i < IPC_MAX_PHONES; i++)
[2ba7810]125 ipc_phone_init(&ta->phones[i]);
[cfffb290]126 if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, ta->context)))
[2ba7810]127 ipc_phone_connect(&ta->phones[0], ipc_phone_0);
[5f62ef9]128 atomic_set(&ta->active_calls, 0);
[4fded58]129
130 mutex_initialize(&ta->futexes_lock);
131 btree_create(&ta->futexes);
[bb68433]132
133 ipl = interrupts_disable();
[482826d]134
135 /*
136 * Increment address space reference count.
137 * TODO: Reconsider the locking scheme.
138 */
139 mutex_lock(&as->lock);
140 as->refcount++;
141 mutex_unlock(&as->lock);
142
[bb68433]143 spinlock_lock(&tasks_lock);
[286e03d]144
145 ta->taskid = ++task_counter;
[b7f364e]146 btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
[286e03d]147
[bb68433]148 spinlock_unlock(&tasks_lock);
149 interrupts_restore(ipl);
150
[f761f1eb]151 return ta;
152}
153
[7509ddc]154/** Destroy task.
155 *
156 * @param t Task to be destroyed.
157 */
158void task_destroy(task_t *t)
159{
[31e8ddd]160 task_destroy_arch(t);
161 btree_destroy(&t->futexes);
162
163 mutex_lock_active(&t->as->lock);
164 if (--t->as->refcount == 0) {
165 mutex_unlock(&t->as->lock);
166 as_destroy(t->as);
167 /*
168 * t->as is destroyed.
169 */
170 } else {
171 mutex_unlock(&t->as->lock);
172 }
173
174 free(t);
175 TASK = NULL;
[7509ddc]176}
177
[5be1923]178/** Create new task with 1 thread and run it
[ff14c520]179 *
[9179d0a]180 * @param program_addr Address of program executable image.
[ff14c520]181 * @param name Program name.
[5be1923]182 *
[7f0837c]183 * @return Task of the running program or NULL on error.
[5be1923]184 */
[ff14c520]185task_t * task_run_program(void *program_addr, char *name)
[5be1923]186{
187 as_t *as;
188 as_area_t *a;
189 int rc;
[b91bb65]190 thread_t *t1, *t2;
[5be1923]191 task_t *task;
[0f250f9]192 uspace_arg_t *kernel_uarg;
[5be1923]193
194 as = as_create(0);
[a0bb10ef]195 ASSERT(as);
[5be1923]196
[649799a]197 rc = elf_load((elf_header_t *) program_addr, as);
[5be1923]198 if (rc != EE_OK) {
[482826d]199 as_destroy(as);
[5be1923]200 return NULL;
201 }
202
[0f250f9]203 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
204 kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
205 kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
206 kernel_uarg->uspace_thread_function = NULL;
207 kernel_uarg->uspace_thread_arg = NULL;
208 kernel_uarg->uspace_uarg = NULL;
[9f52563]209
[ff14c520]210 task = task_create(as, name);
[a0bb10ef]211 ASSERT(task);
212
[5be1923]213 /*
214 * Create the data as_area.
215 */
[0ee077ee]216 a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
217 LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
[8182031]218 USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
[5be1923]219
[b91bb65]220 /*
221 * Create the main thread.
222 */
223 t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
224 ASSERT(t1);
[a0bb10ef]225
[b91bb65]226 /*
227 * Create killer thread for the new task.
228 */
[48e7dd6]229 t2 = thread_create(ktaskgc, t1, task, 0, "ktaskgc");
[b91bb65]230 ASSERT(t2);
231 thread_ready(t2);
232
233 thread_ready(t1);
234
[5be1923]235 return task;
236}
[37c57f2]237
[ec55358]238/** Syscall for reading task ID from userspace.
239 *
[9179d0a]240 * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
[ec55358]241 *
[e3c762cd]242 * @return 0 on success or an error code from @ref errno.h.
[ec55358]243 */
[7f1c620]244unative_t sys_task_get_id(task_id_t *uspace_task_id)
[ec55358]245{
246 /*
247 * No need to acquire lock on TASK because taskid
248 * remains constant for the lifespan of the task.
249 */
[7f1c620]250 return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
[ec55358]251}
252
[9a8d91b]253/** Find task structure corresponding to task ID.
254 *
255 * The tasks_lock must be already held by the caller of this function
256 * and interrupts must be disabled.
257 *
258 * @param id Task ID.
259 *
260 * @return Task structure address or NULL if there is no such task ID.
261 */
262task_t *task_find_by_id(task_id_t id)
263{
264 btree_node_t *leaf;
265
266 return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
267}
268
[7509ddc]269/** Kill task.
270 *
271 * @param id ID of the task to be killed.
272 *
273 * @return 0 on success or an error code from errno.h
274 */
275int task_kill(task_id_t id)
276{
277 ipl_t ipl;
278 task_t *ta;
279 thread_t *t;
280 link_t *cur;
[9b6aae6]281
282 if (id == 1)
283 return EPERM;
[7509ddc]284
285 ipl = interrupts_disable();
286 spinlock_lock(&tasks_lock);
287
288 if (!(ta = task_find_by_id(id))) {
289 spinlock_unlock(&tasks_lock);
290 interrupts_restore(ipl);
291 return ENOENT;
292 }
[2569ec90]293
[7509ddc]294 spinlock_lock(&ta->lock);
295 ta->refcount++;
296 spinlock_unlock(&ta->lock);
[31e8ddd]297
[2569ec90]298 btree_remove(&tasks_btree, ta->taskid, NULL);
[31e8ddd]299 spinlock_unlock(&tasks_lock);
[7509ddc]300
[34dcd3f]301 t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
[7509ddc]302
303 spinlock_lock(&ta->lock);
[34dcd3f]304 ta->accept_new_threads = false;
[7509ddc]305 ta->refcount--;
[b91bb65]306
307 /*
[0182a665]308 * Interrupt all threads except ktaskclnp.
[b91bb65]309 */
[7509ddc]310 for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
311 thread_t *thr;
312 bool sleeping = false;
313
314 thr = list_get_instance(cur, thread_t, th_link);
315 if (thr == t)
316 continue;
317
318 spinlock_lock(&thr->lock);
319 thr->interrupted = true;
320 if (thr->state == Sleeping)
321 sleeping = true;
322 spinlock_unlock(&thr->lock);
323
324 if (sleeping)
325 waitq_interrupt_sleep(thr);
326 }
327
[34dcd3f]328 spinlock_unlock(&ta->lock);
329 interrupts_restore(ipl);
[7509ddc]330
[34dcd3f]331 if (t)
332 thread_ready(t);
333
[7509ddc]334 return 0;
335}
336
[37c57f2]337/** Print task list */
338void task_print_list(void)
339{
340 link_t *cur;
341 ipl_t ipl;
342
343 /* Messing with thread structures, avoid deadlock */
344 ipl = interrupts_disable();
345 spinlock_lock(&tasks_lock);
346
[7f6e755]347 for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
348 btree_node_t *node;
349 int i;
350
351 node = list_get_instance(cur, btree_node_t, leaf_link);
352 for (i = 0; i < node->keys; i++) {
353 task_t *t;
354 int j;
355
356 t = (task_t *) node->value[i];
357
358 spinlock_lock(&t->lock);
[764c302]359 printf("%s(%lld): context=%ld, address=%#zx, as=%#zx, ActiveCalls: %zd",
360 t->name, t->taskid, t->context, t, t->as, atomic_get(&t->active_calls));
[7f6e755]361 for (j=0; j < IPC_MAX_PHONES; j++) {
362 if (t->phones[j].callee)
[fbf7b4c]363 printf(" Ph(%zd): %#zx ", j, t->phones[j].callee);
[7f6e755]364 }
365 printf("\n");
366 spinlock_unlock(&t->lock);
[37c57f2]367 }
368 }
369
370 spinlock_unlock(&tasks_lock);
371 interrupts_restore(ipl);
372}
[7509ddc]373
[b91bb65]374/** Kernel thread used to cleanup the task after it is killed. */
[34dcd3f]375void ktaskclnp(void *arg)
[7509ddc]376{
[34dcd3f]377 ipl_t ipl;
[b91bb65]378 thread_t *t = NULL, *main_thread;
[34dcd3f]379 link_t *cur;
[48e7dd6]380 bool again;
[34dcd3f]381
382 thread_detach(THREAD);
383
384loop:
385 ipl = interrupts_disable();
386 spinlock_lock(&TASK->lock);
387
[b91bb65]388 main_thread = TASK->main_thread;
389
[34dcd3f]390 /*
391 * Find a thread to join.
392 */
[48e7dd6]393 again = false;
[34dcd3f]394 for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
395 t = list_get_instance(cur, thread_t, th_link);
[48e7dd6]396
397 spinlock_lock(&t->lock);
398 if (t == THREAD) {
399 spinlock_unlock(&t->lock);
400 continue;
401 } else if (t == main_thread) {
402 spinlock_unlock(&t->lock);
[34dcd3f]403 continue;
[48e7dd6]404 } else if (t->join_type != None) {
405 spinlock_unlock(&t->lock);
406 again = true;
[b91bb65]407 continue;
[48e7dd6]408 } else {
409 t->join_type = TaskClnp;
410 spinlock_unlock(&t->lock);
411 again = false;
[34dcd3f]412 break;
[48e7dd6]413 }
[34dcd3f]414 }
415
416 spinlock_unlock(&TASK->lock);
417 interrupts_restore(ipl);
418
[48e7dd6]419 if (again) {
420 /*
421 * Other cleanup (e.g. ktaskgc) is in progress.
422 */
423 scheduler();
424 goto loop;
425 }
426
[34dcd3f]427 if (t != THREAD) {
[48e7dd6]428 ASSERT(t != main_thread); /* uninit is joined and detached in ktaskgc */
[34dcd3f]429 thread_join(t);
430 thread_detach(t);
[b91bb65]431 goto loop; /* go for another thread */
[34dcd3f]432 }
433
434 /*
435 * Now there are no other threads in this task
436 * and no new threads can be created.
437 */
438
[e090e1bc]439 ipc_cleanup();
440 futex_cleanup();
[0dbc4e7]441 klog_printf("Cleanup of task %lld completed.", TASK->taskid);
[7509ddc]442}
[b91bb65]443
[88636f68]444/** Kernel thread used to kill the userspace task when its main thread exits.
[b91bb65]445 *
446 * This thread waits until the main userspace thread (i.e. uninit) exits.
[48e7dd6]447 * When this happens, the task is killed. In the meantime, exited threads
448 * are garbage collected.
[b91bb65]449 *
450 * @param arg Pointer to the thread structure of the task's main thread.
451 */
[48e7dd6]452void ktaskgc(void *arg)
[b91bb65]453{
454 thread_t *t = (thread_t *) arg;
[48e7dd6]455loop:
[b91bb65]456 /*
457 * Userspace threads cannot detach themselves,
458 * therefore the thread pointer is guaranteed to be valid.
459 */
[48e7dd6]460 if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) == ESYNCH_TIMEOUT) { /* sleep uninterruptibly here! */
461 ipl_t ipl;
462 link_t *cur;
463 thread_t *thr = NULL;
464
465 /*
466 * The join timed out. Try to do some garbage collection of Undead threads.
467 */
468more_gc:
469 ipl = interrupts_disable();
470 spinlock_lock(&TASK->lock);
471
472 for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
473 thr = list_get_instance(cur, thread_t, th_link);
474 spinlock_lock(&thr->lock);
[0182a665]475 if (thr != t && thr->state == Undead && thr->join_type == None) {
[48e7dd6]476 thr->join_type = TaskGC;
477 spinlock_unlock(&thr->lock);
478 break;
479 }
480 spinlock_unlock(&thr->lock);
481 thr = NULL;
482 }
483 spinlock_unlock(&TASK->lock);
[92922e6]484 interrupts_restore(ipl);
[48e7dd6]485
486 if (thr) {
487 thread_join(thr);
488 thread_detach(thr);
489 scheduler();
490 goto more_gc;
491 }
492
493 goto loop;
494 }
[b91bb65]495 thread_detach(t);
496 task_kill(TASK->taskid);
497}
[b45c443]498
[cc73a8a1]499/** @}
[b45c443]500 */
Note: See TracBrowser for help on using the repository browser.