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

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

Introduce a slab cache for task_t and allocate/free tasks from/to it.

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