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

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

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
  • Property mode set to 100644
File size: 12.4 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 atomic_set(&task->active_calls, 0);
154
155 irq_spinlock_initialize(&task->lock, "task_t_lock");
156 mutex_initialize(&task->futexes_lock, MUTEX_PASSIVE);
157
158 list_initialize(&task->th_head);
159 list_initialize(&task->sync_box_head);
160
161 ipc_answerbox_init(&task->answerbox, task);
162
163 size_t i;
164 for (i = 0; i < IPC_MAX_PHONES; i++)
165 ipc_phone_init(&task->phones[i]);
166
167#ifdef CONFIG_UDEBUG
168 /* Init kbox stuff */
169 task->kb.thread = NULL;
170 ipc_answerbox_init(&task->kb.box, task);
171 mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
172#endif
173
174 return 0;
175}
176
177/** Create new task with no threads.
178 *
179 * @param as Task's address space.
180 * @param name Symbolic name (a copy is made).
181 *
182 * @return New task's structure.
183 *
184 */
185task_t *task_create(as_t *as, const char *name)
186{
187 task_t *task = (task_t *) slab_alloc(task_slab, 0);
188 task_create_arch(task);
189
190 task->as = as;
191 str_cpy(task->name, TASK_NAME_BUFLEN, name);
192
193 task->context = CONTEXT;
194 task->capabilities = 0;
195 task->ucycles = 0;
196 task->kcycles = 0;
197
198 task->ipc_info.call_sent = 0;
199 task->ipc_info.call_recieved = 0;
200 task->ipc_info.answer_sent = 0;
201 task->ipc_info.answer_recieved = 0;
202 task->ipc_info.irq_notif_recieved = 0;
203 task->ipc_info.forwarded = 0;
204
205#ifdef CONFIG_UDEBUG
206 /* Init debugging stuff */
207 udebug_task_init(&task->udebug);
208
209 /* Init kbox stuff */
210 task->kb.finished = false;
211#endif
212
213 if ((ipc_phone_0) &&
214 (context_check(ipc_phone_0->task->context, task->context)))
215 ipc_phone_connect(&task->phones[0], ipc_phone_0);
216
217 btree_create(&task->futexes);
218
219 /*
220 * Get a reference to the address space.
221 */
222 as_hold(task->as);
223
224 irq_spinlock_lock(&tasks_lock, true);
225
226 task->taskid = ++task_counter;
227 avltree_node_initialize(&task->tasks_tree_node);
228 task->tasks_tree_node.key = task->taskid;
229 avltree_insert(&tasks_tree, &task->tasks_tree_node);
230
231 irq_spinlock_unlock(&tasks_lock, true);
232
233 return task;
234}
235
236/** Destroy task.
237 *
238 * @param task Task to be destroyed.
239 *
240 */
241void task_destroy(task_t *task)
242{
243 /*
244 * Remove the task from the task B+tree.
245 */
246 irq_spinlock_lock(&tasks_lock, true);
247 avltree_delete(&tasks_tree, &task->tasks_tree_node);
248 irq_spinlock_unlock(&tasks_lock, true);
249
250 /*
251 * Perform architecture specific task destruction.
252 */
253 task_destroy_arch(task);
254
255 /*
256 * Free up dynamically allocated state.
257 */
258 btree_destroy(&task->futexes);
259
260 /*
261 * Drop our reference to the address space.
262 */
263 as_release(task->as);
264
265 slab_free(task_slab, task);
266}
267
268/** Hold a reference to a task.
269 *
270 * Holding a reference to a task prevents destruction of that task.
271 *
272 * @param task Task to be held.
273 *
274 */
275void task_hold(task_t *task)
276{
277 atomic_inc(&task->refcount);
278}
279
280/** Release a reference to a task.
281 *
282 * The last one to release a reference to a task destroys the task.
283 *
284 * @param task Task to be released.
285 *
286 */
287void task_release(task_t *task)
288{
289 if ((atomic_predec(&task->refcount)) == 0)
290 task_destroy(task);
291}
292
293/** Syscall for reading task ID from userspace.
294 *
295 * @param uspace_task_id Userspace address of 8-byte buffer
296 * where to store current task ID.
297 *
298 * @return Zero on success or an error code from @ref errno.h.
299 *
300 */
301unative_t sys_task_get_id(task_id_t *uspace_task_id)
302{
303 /*
304 * No need to acquire lock on TASK because taskid remains constant for
305 * the lifespan of the task.
306 */
307 return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
308 sizeof(TASK->taskid));
309}
310
311/** Syscall for setting the task name.
312 *
313 * The name simplifies identifying the task in the task list.
314 *
315 * @param name The new name for the task. (typically the same
316 * as the command used to execute it).
317 *
318 * @return 0 on success or an error code from @ref errno.h.
319 *
320 */
321unative_t sys_task_set_name(const char *uspace_name, size_t name_len)
322{
323 int rc;
324 char namebuf[TASK_NAME_BUFLEN];
325
326 /* Cap length of name and copy it from userspace. */
327
328 if (name_len > TASK_NAME_BUFLEN - 1)
329 name_len = TASK_NAME_BUFLEN - 1;
330
331 rc = copy_from_uspace(namebuf, uspace_name, name_len);
332 if (rc != 0)
333 return (unative_t) rc;
334
335 namebuf[name_len] = '\0';
336 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
337
338 return EOK;
339}
340
341/** Find task structure corresponding to task ID.
342 *
343 * The tasks_lock must be already held by the caller of this function and
344 * interrupts must be disabled.
345 *
346 * @param id Task ID.
347 *
348 * @return Task structure address or NULL if there is no such task ID.
349 *
350 */
351task_t *task_find_by_id(task_id_t id)
352{
353 avltree_node_t *node =
354 avltree_search(&tasks_tree, (avltree_key_t) id);
355
356 if (node)
357 return avltree_get_instance(node, task_t, tasks_tree_node);
358
359 return NULL;
360}
361
362/** Get accounting data of given task.
363 *
364 * Note that task lock of 't' must be already held and interrupts must be
365 * already disabled.
366 *
367 * @param task Pointer to the task.
368 * @param ucycles Out pointer to sum of all user cycles.
369 * @param kcycles Out pointer to sum of all kernel cycles.
370 *
371 */
372void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
373{
374 /* Accumulated values of task */
375 uint64_t uret = task->ucycles;
376 uint64_t kret = task->kcycles;
377
378 /* Current values of threads */
379 link_t *cur;
380 for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
381 thread_t *thread = list_get_instance(cur, thread_t, th_link);
382
383 irq_spinlock_lock(&thread->lock, false);
384
385 /* Process only counted threads */
386 if (!thread->uncounted) {
387 if (thread == THREAD) {
388 /* Update accounting of current thread */
389 thread_update_accounting(false);
390 }
391
392 uret += thread->ucycles;
393 kret += thread->kcycles;
394 }
395
396 irq_spinlock_unlock(&thread->lock, false);
397 }
398
399 *ucycles = uret;
400 *kcycles = kret;
401}
402
403static void task_kill_internal(task_t *task)
404{
405 link_t *cur;
406
407 /*
408 * Interrupt all threads.
409 */
410 irq_spinlock_lock(&task->lock, false);
411 for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
412 thread_t *thread = list_get_instance(cur, thread_t, th_link);
413 bool sleeping = false;
414
415 irq_spinlock_lock(&thread->lock, false);
416
417 thread->interrupted = true;
418 if (thread->state == Sleeping)
419 sleeping = true;
420
421 irq_spinlock_unlock(&thread->lock, false);
422
423 if (sleeping)
424 waitq_interrupt_sleep(thread);
425 }
426
427 irq_spinlock_unlock(&task->lock, false);
428}
429
430/** Kill task.
431 *
432 * This function is idempotent.
433 * It signals all the task's threads to bail it out.
434 *
435 * @param id ID of the task to be killed.
436 *
437 * @return Zero on success or an error code from errno.h.
438 *
439 */
440int task_kill(task_id_t id)
441{
442 if (id == 1)
443 return EPERM;
444
445 irq_spinlock_lock(&tasks_lock, true);
446
447 task_t *task = task_find_by_id(id);
448 if (!task) {
449 irq_spinlock_unlock(&tasks_lock, true);
450 return ENOENT;
451 }
452
453 task_kill_internal(task);
454 irq_spinlock_unlock(&tasks_lock, true);
455
456 return EOK;
457}
458
459static bool task_print_walker(avltree_node_t *node, void *arg)
460{
461 task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
462 irq_spinlock_lock(&task->lock, false);
463
464 uint64_t ucycles;
465 uint64_t kcycles;
466 char usuffix, ksuffix;
467 task_get_accounting(task, &ucycles, &kcycles);
468 order_suffix(ucycles, &ucycles, &usuffix);
469 order_suffix(kcycles, &kcycles, &ksuffix);
470
471#ifdef __32_BITS__
472 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %10p %10p %9" PRIu64 "%c %9"
473 PRIu64 "%c %7ld %6ld", task->taskid, task->name, task->context,
474 task, task->as, ucycles, usuffix, kcycles, ksuffix,
475 atomic_get(&task->refcount), atomic_get(&task->active_calls));
476#endif
477
478#ifdef __64_BITS__
479 printf("%-6" PRIu64 " %-12s %-3" PRIu32 " %18p %18p %9" PRIu64 "%c %9"
480 PRIu64 "%c %7ld %6ld", task->taskid, task->name, task->context,
481 task, task->as, ucycles, usuffix, kcycles, ksuffix,
482 atomic_get(&task->refcount), atomic_get(&task->active_calls));
483#endif
484
485 size_t i;
486 for (i = 0; i < IPC_MAX_PHONES; i++) {
487 if (task->phones[i].callee)
488 printf(" %" PRIs ":%p", i, task->phones[i].callee);
489 }
490 printf("\n");
491
492 irq_spinlock_unlock(&task->lock, false);
493 return true;
494}
495
496/** Print task list */
497void task_print_list(void)
498{
499 /* Messing with task structures, avoid deadlock */
500 irq_spinlock_lock(&tasks_lock, true);
501
502#ifdef __32_BITS__
503 printf("taskid name ctx address as "
504 " ucycles kcycles threads calls callee\n");
505 printf("------ ------------ --- ---------- ----------"
506 " ---------- ---------- ------- ------ ------>\n");
507#endif
508
509#ifdef __64_BITS__
510 printf("taskid name ctx address as "
511 " ucycles kcycles threads calls callee\n");
512 printf("------ ------------ --- ------------------ ------------------"
513 " ---------- ---------- ---------- ------- ------ ------>\n");
514#endif
515
516 avltree_walk(&tasks_tree, task_print_walker, NULL);
517
518 irq_spinlock_unlock(&tasks_lock, true);
519}
520
521/** @}
522 */
Note: See TracBrowser for help on using the repository browser.