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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 666f492 was d3808d3, checked in by Jakub Jermar <jakub@…>, 15 years ago

Do not clear TASK in task_destroy().

  • Property mode set to 100644
File size: 12.2 KB
Line 
1/*
2 * Copyright (c) 2010 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
29/** @addtogroup genericproc
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Task management.
36 */
37
38#include <proc/thread.h>
39#include <proc/task.h>
40#include <mm/as.h>
41#include <mm/slab.h>
42#include <atomic.h>
43#include <synch/spinlock.h>
44#include <synch/waitq.h>
45#include <arch.h>
46#include <arch/barrier.h>
47#include <adt/avl.h>
48#include <adt/btree.h>
49#include <adt/list.h>
50#include <ipc/ipc.h>
51#include <ipc/ipcrsc.h>
52#include <print.h>
53#include <errno.h>
54#include <func.h>
55#include <str.h>
56#include <memstr.h>
57#include <syscall/copy.h>
58#include <macros.h>
59#include <ipc/event.h>
60
61/** Spinlock protecting the tasks_tree AVL tree. */
62SPINLOCK_INITIALIZE(tasks_lock);
63
64/** AVL tree of active tasks.
65 *
66 * The task is guaranteed to exist after it was found in the tasks_tree as
67 * long as:
68 *
69 * @li the tasks_lock is held,
70 * @li the task's lock is held when task's lock is acquired before releasing
71 * tasks_lock or
72 * @li the task's refcount is greater than 0
73 *
74 */
75avltree_t tasks_tree;
76
77static task_id_t task_counter = 0;
78
79static slab_cache_t *task_slab;
80
81/* Forward declarations. */
82static void task_kill_internal(task_t *);
83static int tsk_constructor(void *, int);
84
85/** Initialize kernel tasks support. */
86void task_init(void)
87{
88 TASK = NULL;
89 avltree_create(&tasks_tree);
90 task_slab = slab_cache_create("task_slab", sizeof(task_t), 0,
91 tsk_constructor, NULL, 0);
92}
93
94/*
95 * The idea behind this walker is to kill and count all tasks different from
96 * TASK.
97 */
98static bool task_done_walker(avltree_node_t *node, void *arg)
99{
100 task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
101 unsigned *cnt = (unsigned *) arg;
102
103 if (t != TASK) {
104 (*cnt)++;
105#ifdef CONFIG_DEBUG
106 printf("[%"PRIu64"] ", t->taskid);
107#endif
108 task_kill_internal(t);
109 }
110
111 /* Continue the walk */
112 return true;
113}
114
115/** Kill all tasks except the current task. */
116void task_done(void)
117{
118 unsigned tasks_left;
119
120 do { /* Repeat until there are any tasks except TASK */
121 /* Messing with task structures, avoid deadlock */
122#ifdef CONFIG_DEBUG
123 printf("Killing tasks... ");
124#endif
125 ipl_t ipl = interrupts_disable();
126 spinlock_lock(&tasks_lock);
127 tasks_left = 0;
128 avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
129 spinlock_unlock(&tasks_lock);
130 interrupts_restore(ipl);
131 thread_sleep(1);
132#ifdef CONFIG_DEBUG
133 printf("\n");
134#endif
135 } while (tasks_left);
136}
137
138int tsk_constructor(void *obj, int kmflags)
139{
140 task_t *ta = obj;
141 int i;
142
143 atomic_set(&ta->refcount, 0);
144 atomic_set(&ta->lifecount, 0);
145 atomic_set(&ta->active_calls, 0);
146
147 spinlock_initialize(&ta->lock, "task_ta_lock");
148 mutex_initialize(&ta->futexes_lock, MUTEX_PASSIVE);
149
150 list_initialize(&ta->th_head);
151 list_initialize(&ta->sync_box_head);
152
153 ipc_answerbox_init(&ta->answerbox, ta);
154 for (i = 0; i < IPC_MAX_PHONES; i++)
155 ipc_phone_init(&ta->phones[i]);
156
157#ifdef CONFIG_UDEBUG
158 /* Init kbox stuff */
159 ta->kb.thread = NULL;
160 ipc_answerbox_init(&ta->kb.box, ta);
161 mutex_initialize(&ta->kb.cleanup_lock, MUTEX_PASSIVE);
162#endif
163
164 return 0;
165}
166
167/** Create new task with no threads.
168 *
169 * @param as Task's address space.
170 * @param name Symbolic name (a copy is made).
171 *
172 * @return New task's structure.
173 *
174 */
175task_t *task_create(as_t *as, const char *name)
176{
177 ipl_t ipl;
178 task_t *ta;
179
180 ta = (task_t *) slab_alloc(task_slab, 0);
181 task_create_arch(ta);
182 ta->as = as;
183 memcpy(ta->name, name, TASK_NAME_BUFLEN);
184 ta->name[TASK_NAME_BUFLEN - 1] = 0;
185
186 ta->context = CONTEXT;
187 ta->capabilities = 0;
188 ta->ucycles = 0;
189 ta->kcycles = 0;
190
191 ta->ipc_info.call_sent = 0;
192 ta->ipc_info.call_recieved = 0;
193 ta->ipc_info.answer_sent = 0;
194 ta->ipc_info.answer_recieved = 0;
195 ta->ipc_info.irq_notif_recieved = 0;
196 ta->ipc_info.forwarded = 0;
197
198#ifdef CONFIG_UDEBUG
199 /* Init debugging stuff */
200 udebug_task_init(&ta->udebug);
201
202 /* Init kbox stuff */
203 ta->kb.finished = false;
204#endif
205
206 if ((ipc_phone_0) &&
207 (context_check(ipc_phone_0->task->context, ta->context)))
208 ipc_phone_connect(&ta->phones[0], ipc_phone_0);
209
210 btree_create(&ta->futexes);
211
212 /*
213 * Get a reference to the address space.
214 */
215 as_hold(ta->as);
216
217 ipl = interrupts_disable();
218 spinlock_lock(&tasks_lock);
219 ta->taskid = ++task_counter;
220 avltree_node_initialize(&ta->tasks_tree_node);
221 ta->tasks_tree_node.key = ta->taskid;
222 avltree_insert(&tasks_tree, &ta->tasks_tree_node);
223 spinlock_unlock(&tasks_lock);
224 interrupts_restore(ipl);
225
226 return ta;
227}
228
229/** Destroy task.
230 *
231 * @param t Task to be destroyed.
232 *
233 */
234void task_destroy(task_t *t)
235{
236 /*
237 * Remove the task from the task B+tree.
238 */
239 spinlock_lock(&tasks_lock);
240 avltree_delete(&tasks_tree, &t->tasks_tree_node);
241 spinlock_unlock(&tasks_lock);
242
243 /*
244 * Perform architecture specific task destruction.
245 */
246 task_destroy_arch(t);
247
248 /*
249 * Free up dynamically allocated state.
250 */
251 btree_destroy(&t->futexes);
252
253 /*
254 * Drop our reference to the address space.
255 */
256 as_release(t->as);
257
258 slab_free(task_slab, t);
259}
260
261/** Hold a reference to a task.
262 *
263 * Holding a reference to a task prevents destruction of that task.
264 *
265 * @param t Task to be held.
266 */
267void task_hold(task_t *t)
268{
269 atomic_inc(&t->refcount);
270}
271
272/** Release a reference to a task.
273 *
274 * The last one to release a reference to a task destroys the task.
275 *
276 * @param t Task to be released.
277 */
278void task_release(task_t *t)
279{
280 if ((atomic_predec(&t->refcount)) == 0)
281 task_destroy(t);
282}
283
284/** Syscall for reading task ID from userspace.
285 *
286 * @param uspace_task_id Userspace address of 8-byte buffer
287 * where to store current task ID.
288 *
289 * @return Zero on success or an error code from @ref errno.h.
290 *
291 */
292unative_t sys_task_get_id(task_id_t *uspace_task_id)
293{
294 /*
295 * No need to acquire lock on TASK because taskid remains constant for
296 * the lifespan of the task.
297 */
298 return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
299 sizeof(TASK->taskid));
300}
301
302/** Syscall for setting the task name.
303 *
304 * The name simplifies identifying the task in the task list.
305 *
306 * @param name The new name for the task. (typically the same
307 * as the command used to execute it).
308 *
309 * @return 0 on success or an error code from @ref errno.h.
310 *
311 */
312unative_t sys_task_set_name(const char *uspace_name, size_t name_len)
313{
314 int rc;
315 char namebuf[TASK_NAME_BUFLEN];
316
317 /* Cap length of name and copy it from userspace. */
318
319 if (name_len > TASK_NAME_BUFLEN - 1)
320 name_len = TASK_NAME_BUFLEN - 1;
321
322 rc = copy_from_uspace(namebuf, uspace_name, name_len);
323 if (rc != 0)
324 return (unative_t) rc;
325
326 namebuf[name_len] = '\0';
327 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
328
329 return EOK;
330}
331
332/** Find task structure corresponding to task ID.
333 *
334 * The tasks_lock must be already held by the caller of this function and
335 * interrupts must be disabled.
336 *
337 * @param id Task ID.
338 *
339 * @return Task structure address or NULL if there is no such task ID.
340 *
341 */
342task_t *task_find_by_id(task_id_t id)
343{
344 avltree_node_t *node =
345 avltree_search(&tasks_tree, (avltree_key_t) id);
346
347 if (node)
348 return avltree_get_instance(node, task_t, tasks_tree_node);
349
350 return NULL;
351}
352
353/** Get accounting data of given task.
354 *
355 * Note that task lock of 't' must be already held and interrupts must be
356 * already disabled.
357 *
358 * @param t Pointer to thread.
359 * @param ucycles Out pointer to sum of all user cycles.
360 * @param kcycles Out pointer to sum of all kernel cycles.
361 *
362 */
363void task_get_accounting(task_t *t, uint64_t *ucycles, uint64_t *kcycles)
364{
365 /* Accumulated values of task */
366 uint64_t uret = t->ucycles;
367 uint64_t kret = t->kcycles;
368
369 /* Current values of threads */
370 link_t *cur;
371 for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
372 thread_t *thr = list_get_instance(cur, thread_t, th_link);
373
374 spinlock_lock(&thr->lock);
375 /* Process only counted threads */
376 if (!thr->uncounted) {
377 if (thr == THREAD) {
378 /* Update accounting of current thread */
379 thread_update_accounting(false);
380 }
381 uret += thr->ucycles;
382 kret += thr->kcycles;
383 }
384 spinlock_unlock(&thr->lock);
385 }
386
387 *ucycles = uret;
388 *kcycles = kret;
389}
390
391static void task_kill_internal(task_t *ta)
392{
393 link_t *cur;
394
395 /*
396 * Interrupt all threads.
397 */
398 spinlock_lock(&ta->lock);
399 for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
400 thread_t *thr;
401 bool sleeping = false;
402
403 thr = list_get_instance(cur, thread_t, th_link);
404
405 spinlock_lock(&thr->lock);
406 thr->interrupted = true;
407 if (thr->state == Sleeping)
408 sleeping = true;
409 spinlock_unlock(&thr->lock);
410
411 if (sleeping)
412 waitq_interrupt_sleep(thr);
413 }
414 spinlock_unlock(&ta->lock);
415}
416
417/** Kill task.
418 *
419 * This function is idempotent.
420 * It signals all the task's threads to bail it out.
421 *
422 * @param id ID of the task to be killed.
423 *
424 * @return Zero on success or an error code from errno.h.
425 *
426 */
427int task_kill(task_id_t id)
428{
429 ipl_t ipl;
430 task_t *ta;
431
432 if (id == 1)
433 return EPERM;
434
435 ipl = interrupts_disable();
436 spinlock_lock(&tasks_lock);
437 if (!(ta = task_find_by_id(id))) {
438 spinlock_unlock(&tasks_lock);
439 interrupts_restore(ipl);
440 return ENOENT;
441 }
442 task_kill_internal(ta);
443 spinlock_unlock(&tasks_lock);
444 interrupts_restore(ipl);
445 return 0;
446}
447
448static bool task_print_walker(avltree_node_t *node, void *arg)
449{
450 task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
451 int j;
452
453 spinlock_lock(&t->lock);
454
455 uint64_t ucycles;
456 uint64_t kcycles;
457 char usuffix, ksuffix;
458 task_get_accounting(t, &ucycles, &kcycles);
459 order_suffix(ucycles, &ucycles, &usuffix);
460 order_suffix(kcycles, &kcycles, &ksuffix);
461
462#ifdef __32_BITS__
463 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %10p %10p %9" PRIu64 "%c %9"
464 PRIu64 "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as,
465 ucycles, usuffix, kcycles, ksuffix, atomic_get(&t->refcount),
466 atomic_get(&t->active_calls));
467#endif
468
469#ifdef __64_BITS__
470 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %18p %18p %9" PRIu64 "%c %9"
471 PRIu64 "%c %7ld %6ld", t->taskid, t->name, t->context, t, t->as,
472 ucycles, usuffix, kcycles, ksuffix, atomic_get(&t->refcount),
473 atomic_get(&t->active_calls));
474#endif
475
476 for (j = 0; j < IPC_MAX_PHONES; j++) {
477 if (t->phones[j].callee)
478 printf(" %d:%p", j, t->phones[j].callee);
479 }
480 printf("\n");
481
482 spinlock_unlock(&t->lock);
483 return true;
484}
485
486/** Print task list */
487void task_print_list(void)
488{
489 ipl_t ipl;
490
491 /* Messing with task structures, avoid deadlock */
492 ipl = interrupts_disable();
493 spinlock_lock(&tasks_lock);
494
495#ifdef __32_BITS__
496 printf("taskid name ctx address as "
497 " ucycles kcycles threads calls callee\n");
498 printf("------ ------------ --- ---------- ----------"
499 " ---------- ---------- ------- ------ ------>\n");
500#endif
501
502#ifdef __64_BITS__
503 printf("taskid name ctx address as "
504 " ucycles kcycles threads calls callee\n");
505 printf("------ ------------ --- ------------------ ------------------"
506 " ---------- ---------- ---------- ------- ------ ------>\n");
507#endif
508
509 avltree_walk(&tasks_tree, task_print_walker, NULL);
510
511 spinlock_unlock(&tasks_lock);
512 interrupts_restore(ipl);
513}
514
515/** @}
516 */
Note: See TracBrowser for help on using the repository browser.