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

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

benchmarking with statistics (initial)

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