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

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

32/64 ABI split for SYS_TASK_GET_ID

  • Property mode set to 100644
File size: 13.0 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. */
62IRQ_SPINLOCK_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 *, unsigned int);
84
85/** Initialize kernel tasks support.
86 *
87 */
88void task_init(void)
89{
90 TASK = NULL;
91 avltree_create(&tasks_tree);
92 task_slab = slab_cache_create("task_slab", sizeof(task_t), 0,
93 tsk_constructor, NULL, 0);
94}
95
96/** Task finish walker.
97 *
98 * The idea behind this walker is to kill and count all tasks different from
99 * TASK.
100 *
101 */
102static bool task_done_walker(avltree_node_t *node, void *arg)
103{
104 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
105 size_t *cnt = (size_t *) arg;
106
107 if (task != TASK) {
108 (*cnt)++;
109
110#ifdef CONFIG_DEBUG
111 printf("[%"PRIu64"] ", task->taskid);
112#endif
113
114 task_kill_internal(task);
115 }
116
117 /* Continue the walk */
118 return true;
119}
120
121/** Kill all tasks except the current task.
122 *
123 */
124void task_done(void)
125{
126 size_t tasks_left;
127
128 /* Repeat until there are any tasks except TASK */
129 do {
130#ifdef CONFIG_DEBUG
131 printf("Killing tasks... ");
132#endif
133
134 irq_spinlock_lock(&tasks_lock, true);
135 tasks_left = 0;
136 avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
137 irq_spinlock_unlock(&tasks_lock, true);
138
139 thread_sleep(1);
140
141#ifdef CONFIG_DEBUG
142 printf("\n");
143#endif
144 } while (tasks_left > 0);
145}
146
147int tsk_constructor(void *obj, unsigned int kmflags)
148{
149 task_t *task = (task_t *) obj;
150
151 atomic_set(&task->refcount, 0);
152 atomic_set(&task->lifecount, 0);
153
154 irq_spinlock_initialize(&task->lock, "task_t_lock");
155 mutex_initialize(&task->futexes_lock, MUTEX_PASSIVE);
156
157 list_initialize(&task->th_head);
158 list_initialize(&task->sync_box_head);
159
160 ipc_answerbox_init(&task->answerbox, task);
161
162 size_t i;
163 for (i = 0; i < IPC_MAX_PHONES; i++)
164 ipc_phone_init(&task->phones[i]);
165
166#ifdef CONFIG_UDEBUG
167 /* Init kbox stuff */
168 task->kb.thread = NULL;
169 ipc_answerbox_init(&task->kb.box, task);
170 mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
171#endif
172
173 return 0;
174}
175
176/** Create new task with no threads.
177 *
178 * @param as Task's address space.
179 * @param name Symbolic name (a copy is made).
180 *
181 * @return New task's structure.
182 *
183 */
184task_t *task_create(as_t *as, const char *name)
185{
186 task_t *task = (task_t *) slab_alloc(task_slab, 0);
187 task_create_arch(task);
188
189 task->as = as;
190 str_cpy(task->name, TASK_NAME_BUFLEN, name);
191
192 task->context = CONTEXT;
193 task->capabilities = 0;
194 task->ucycles = 0;
195 task->kcycles = 0;
196
197 task->ipc_info.call_sent = 0;
198 task->ipc_info.call_received = 0;
199 task->ipc_info.answer_sent = 0;
200 task->ipc_info.answer_received = 0;
201 task->ipc_info.irq_notif_received = 0;
202 task->ipc_info.forwarded = 0;
203
204#ifdef CONFIG_UDEBUG
205 /* Init debugging stuff */
206 udebug_task_init(&task->udebug);
207
208 /* Init kbox stuff */
209 task->kb.finished = false;
210#endif
211
212 if ((ipc_phone_0) &&
213 (context_check(ipc_phone_0->task->context, task->context)))
214 ipc_phone_connect(&task->phones[0], ipc_phone_0);
215
216 btree_create(&task->futexes);
217
218 /*
219 * Get a reference to the address space.
220 */
221 as_hold(task->as);
222
223 irq_spinlock_lock(&tasks_lock, true);
224
225 task->taskid = ++task_counter;
226 avltree_node_initialize(&task->tasks_tree_node);
227 task->tasks_tree_node.key = task->taskid;
228 avltree_insert(&tasks_tree, &task->tasks_tree_node);
229
230 irq_spinlock_unlock(&tasks_lock, true);
231
232 return task;
233}
234
235/** Destroy task.
236 *
237 * @param task Task to be destroyed.
238 *
239 */
240void task_destroy(task_t *task)
241{
242 /*
243 * Remove the task from the task B+tree.
244 */
245 irq_spinlock_lock(&tasks_lock, true);
246 avltree_delete(&tasks_tree, &task->tasks_tree_node);
247 irq_spinlock_unlock(&tasks_lock, true);
248
249 /*
250 * Perform architecture specific task destruction.
251 */
252 task_destroy_arch(task);
253
254 /*
255 * Free up dynamically allocated state.
256 */
257 btree_destroy(&task->futexes);
258
259 /*
260 * Drop our reference to the address space.
261 */
262 as_release(task->as);
263
264 slab_free(task_slab, task);
265}
266
267/** Hold a reference to a task.
268 *
269 * Holding a reference to a task prevents destruction of that task.
270 *
271 * @param task Task to be held.
272 *
273 */
274void task_hold(task_t *task)
275{
276 atomic_inc(&task->refcount);
277}
278
279/** Release a reference to a task.
280 *
281 * The last one to release a reference to a task destroys the task.
282 *
283 * @param task Task to be released.
284 *
285 */
286void task_release(task_t *task)
287{
288 if ((atomic_predec(&task->refcount)) == 0)
289 task_destroy(task);
290}
291
292#ifdef __32_BITS__
293
294/** Syscall for reading task ID from userspace (32 bits)
295 *
296 * @param uspace_taskid Pointer to user-space buffer
297 * where to store current task ID.
298 *
299 * @return Zero on success or an error code from @ref errno.h.
300 *
301 */
302sysarg_t sys_task_get_id(sysarg64_t *uspace_taskid)
303{
304 /*
305 * No need to acquire lock on TASK because taskid remains constant for
306 * the lifespan of the task.
307 */
308 return (sysarg_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
309 sizeof(TASK->taskid));
310}
311
312#endif /* __32_BITS__ */
313
314#ifdef __64_BITS__
315
316/** Syscall for reading task ID from userspace (64 bits)
317 *
318 * @return Current task ID.
319 *
320 */
321sysarg_t sys_task_get_id(void)
322{
323 /*
324 * No need to acquire lock on TASK because taskid remains constant for
325 * the lifespan of the task.
326 */
327 return TASK->taskid;
328}
329
330#endif /* __64_BITS__ */
331
332/** Syscall for setting the task name.
333 *
334 * The name simplifies identifying the task in the task list.
335 *
336 * @param name The new name for the task. (typically the same
337 * as the command used to execute it).
338 *
339 * @return 0 on success or an error code from @ref errno.h.
340 *
341 */
342sysarg_t sys_task_set_name(const char *uspace_name, size_t name_len)
343{
344 int rc;
345 char namebuf[TASK_NAME_BUFLEN];
346
347 /* Cap length of name and copy it from userspace. */
348
349 if (name_len > TASK_NAME_BUFLEN - 1)
350 name_len = TASK_NAME_BUFLEN - 1;
351
352 rc = copy_from_uspace(namebuf, uspace_name, name_len);
353 if (rc != 0)
354 return (sysarg_t) rc;
355
356 namebuf[name_len] = '\0';
357 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
358
359 return EOK;
360}
361
362/** Find task structure corresponding to task ID.
363 *
364 * The tasks_lock must be already held by the caller of this function and
365 * interrupts must be disabled.
366 *
367 * @param id Task ID.
368 *
369 * @return Task structure address or NULL if there is no such task ID.
370 *
371 */
372task_t *task_find_by_id(task_id_t id)
373{
374 ASSERT(interrupts_disabled());
375 ASSERT(irq_spinlock_locked(&tasks_lock));
376
377 avltree_node_t *node =
378 avltree_search(&tasks_tree, (avltree_key_t) id);
379
380 if (node)
381 return avltree_get_instance(node, task_t, tasks_tree_node);
382
383 return NULL;
384}
385
386/** Get accounting data of given task.
387 *
388 * Note that task lock of 'task' must be already held and interrupts must be
389 * already disabled.
390 *
391 * @param task Pointer to the task.
392 * @param ucycles Out pointer to sum of all user cycles.
393 * @param kcycles Out pointer to sum of all kernel cycles.
394 *
395 */
396void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
397{
398 ASSERT(interrupts_disabled());
399 ASSERT(irq_spinlock_locked(&task->lock));
400
401 /* Accumulated values of task */
402 uint64_t uret = task->ucycles;
403 uint64_t kret = task->kcycles;
404
405 /* Current values of threads */
406 link_t *cur;
407 for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
408 thread_t *thread = list_get_instance(cur, thread_t, th_link);
409
410 irq_spinlock_lock(&thread->lock, false);
411
412 /* Process only counted threads */
413 if (!thread->uncounted) {
414 if (thread == THREAD) {
415 /* Update accounting of current thread */
416 thread_update_accounting(false);
417 }
418
419 uret += thread->ucycles;
420 kret += thread->kcycles;
421 }
422
423 irq_spinlock_unlock(&thread->lock, false);
424 }
425
426 *ucycles = uret;
427 *kcycles = kret;
428}
429
430static void task_kill_internal(task_t *task)
431{
432 link_t *cur;
433
434 /*
435 * Interrupt all threads.
436 */
437 irq_spinlock_lock(&task->lock, false);
438 for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
439 thread_t *thread = list_get_instance(cur, thread_t, th_link);
440 bool sleeping = false;
441
442 irq_spinlock_lock(&thread->lock, false);
443
444 thread->interrupted = true;
445 if (thread->state == Sleeping)
446 sleeping = true;
447
448 irq_spinlock_unlock(&thread->lock, false);
449
450 if (sleeping)
451 waitq_interrupt_sleep(thread);
452 }
453
454 irq_spinlock_unlock(&task->lock, false);
455}
456
457/** Kill task.
458 *
459 * This function is idempotent.
460 * It signals all the task's threads to bail it out.
461 *
462 * @param id ID of the task to be killed.
463 *
464 * @return Zero on success or an error code from errno.h.
465 *
466 */
467int task_kill(task_id_t id)
468{
469 if (id == 1)
470 return EPERM;
471
472 irq_spinlock_lock(&tasks_lock, true);
473
474 task_t *task = task_find_by_id(id);
475 if (!task) {
476 irq_spinlock_unlock(&tasks_lock, true);
477 return ENOENT;
478 }
479
480 task_kill_internal(task);
481 irq_spinlock_unlock(&tasks_lock, true);
482
483 return EOK;
484}
485
486static bool task_print_walker(avltree_node_t *node, void *arg)
487{
488 bool *additional = (bool *) arg;
489 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
490 irq_spinlock_lock(&task->lock, false);
491
492 uint64_t ucycles;
493 uint64_t kcycles;
494 char usuffix, ksuffix;
495 task_get_accounting(task, &ucycles, &kcycles);
496 order_suffix(ucycles, &ucycles, &usuffix);
497 order_suffix(kcycles, &kcycles, &ksuffix);
498
499#ifdef __32_BITS__
500 if (*additional)
501 printf("%-8" PRIu64 " %9" PRIua, task->taskid,
502 atomic_get(&task->refcount));
503 else
504 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
505 " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
506 task->name, task->context, task, task->as,
507 ucycles, usuffix, kcycles, ksuffix);
508#endif
509
510#ifdef __64_BITS__
511 if (*additional)
512 printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
513 "%9" PRIua, task->taskid, ucycles, usuffix, kcycles,
514 ksuffix, atomic_get(&task->refcount));
515 else
516 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
517 task->taskid, task->name, task->context, task, task->as);
518#endif
519
520 if (*additional) {
521 size_t i;
522 for (i = 0; i < IPC_MAX_PHONES; i++) {
523 if (task->phones[i].callee)
524 printf(" %zu:%p", i, task->phones[i].callee);
525 }
526 printf("\n");
527 }
528
529 irq_spinlock_unlock(&task->lock, false);
530 return true;
531}
532
533/** Print task list
534 *
535 * @param additional Print additional information.
536 *
537 */
538void task_print_list(bool additional)
539{
540 /* Messing with task structures, avoid deadlock */
541 irq_spinlock_lock(&tasks_lock, true);
542
543#ifdef __32_BITS__
544 if (additional)
545 printf("[id ] [threads] [calls] [callee\n");
546 else
547 printf("[id ] [name ] [ctx] [address ] [as ]"
548 " [ucycles ] [kcycles ]\n");
549#endif
550
551#ifdef __64_BITS__
552 if (additional)
553 printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
554 " [callee\n");
555 else
556 printf("[id ] [name ] [ctx] [address ]"
557 " [as ]\n");
558#endif
559
560 avltree_walk(&tasks_tree, task_print_walker, &additional);
561
562 irq_spinlock_unlock(&tasks_lock, true);
563}
564
565/** @}
566 */
Note: See TracBrowser for help on using the repository browser.