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 <assert.h>
|
---|
39 | #include <proc/thread.h>
|
---|
40 | #include <proc/task.h>
|
---|
41 | #include <mm/as.h>
|
---|
42 | #include <mm/slab.h>
|
---|
43 | #include <atomic.h>
|
---|
44 | #include <synch/futex.h>
|
---|
45 | #include <synch/spinlock.h>
|
---|
46 | #include <synch/waitq.h>
|
---|
47 | #include <arch.h>
|
---|
48 | #include <arch/barrier.h>
|
---|
49 | #include <adt/avl.h>
|
---|
50 | #include <adt/btree.h>
|
---|
51 | #include <adt/list.h>
|
---|
52 | #include <cap/cap.h>
|
---|
53 | #include <ipc/ipc.h>
|
---|
54 | #include <ipc/ipcrsc.h>
|
---|
55 | #include <ipc/event.h>
|
---|
56 | #include <print.h>
|
---|
57 | #include <errno.h>
|
---|
58 | #include <func.h>
|
---|
59 | #include <str.h>
|
---|
60 | #include <syscall/copy.h>
|
---|
61 | #include <macros.h>
|
---|
62 |
|
---|
63 | /** Spinlock protecting the tasks_tree AVL tree. */
|
---|
64 | IRQ_SPINLOCK_INITIALIZE(tasks_lock);
|
---|
65 |
|
---|
66 | /** AVL tree of active tasks.
|
---|
67 | *
|
---|
68 | * The task is guaranteed to exist after it was found in the tasks_tree as
|
---|
69 | * long as:
|
---|
70 | *
|
---|
71 | * @li the tasks_lock is held,
|
---|
72 | * @li the task's lock is held when task's lock is acquired before releasing
|
---|
73 | * tasks_lock or
|
---|
74 | * @li the task's refcount is greater than 0
|
---|
75 | *
|
---|
76 | */
|
---|
77 | avltree_t tasks_tree;
|
---|
78 |
|
---|
79 | static task_id_t task_counter = 0;
|
---|
80 |
|
---|
81 | static slab_cache_t *task_cache;
|
---|
82 |
|
---|
83 | /* Forward declarations. */
|
---|
84 | static void task_kill_internal(task_t *);
|
---|
85 | static int tsk_constructor(void *, unsigned int);
|
---|
86 | static size_t tsk_destructor(void *obj);
|
---|
87 |
|
---|
88 | /** Initialize kernel tasks support.
|
---|
89 | *
|
---|
90 | */
|
---|
91 | void task_init(void)
|
---|
92 | {
|
---|
93 | TASK = NULL;
|
---|
94 | avltree_create(&tasks_tree);
|
---|
95 | task_cache = slab_cache_create("task_t", sizeof(task_t), 0,
|
---|
96 | tsk_constructor, tsk_destructor, 0);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Task finish walker.
|
---|
100 | *
|
---|
101 | * The idea behind this walker is to kill and count all tasks different from
|
---|
102 | * TASK.
|
---|
103 | *
|
---|
104 | */
|
---|
105 | static bool task_done_walker(avltree_node_t *node, void *arg)
|
---|
106 | {
|
---|
107 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
108 | size_t *cnt = (size_t *) arg;
|
---|
109 |
|
---|
110 | if (task != TASK) {
|
---|
111 | (*cnt)++;
|
---|
112 |
|
---|
113 | #ifdef CONFIG_DEBUG
|
---|
114 | printf("[%"PRIu64"] ", task->taskid);
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | task_kill_internal(task);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /* Continue the walk */
|
---|
121 | return true;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** Kill all tasks except the current task.
|
---|
125 | *
|
---|
126 | */
|
---|
127 | void task_done(void)
|
---|
128 | {
|
---|
129 | size_t tasks_left;
|
---|
130 |
|
---|
131 | if (ipc_phone_0) {
|
---|
132 | task_t *task_0 = ipc_phone_0->task;
|
---|
133 | ipc_phone_0 = NULL;
|
---|
134 | /*
|
---|
135 | * The first task is held by kinit(), we need to release it or
|
---|
136 | * it will never finish cleanup.
|
---|
137 | */
|
---|
138 | task_release(task_0);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /* Repeat until there are any tasks except TASK */
|
---|
142 | do {
|
---|
143 | #ifdef CONFIG_DEBUG
|
---|
144 | printf("Killing tasks... ");
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | irq_spinlock_lock(&tasks_lock, true);
|
---|
148 | tasks_left = 0;
|
---|
149 | avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
|
---|
150 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
151 |
|
---|
152 | thread_sleep(1);
|
---|
153 |
|
---|
154 | #ifdef CONFIG_DEBUG
|
---|
155 | printf("\n");
|
---|
156 | #endif
|
---|
157 | } while (tasks_left > 0);
|
---|
158 | }
|
---|
159 |
|
---|
160 | int tsk_constructor(void *obj, unsigned int kmflags)
|
---|
161 | {
|
---|
162 | task_t *task = (task_t *) obj;
|
---|
163 |
|
---|
164 | int rc = caps_task_alloc(task);
|
---|
165 | if (rc != EOK)
|
---|
166 | return rc;
|
---|
167 |
|
---|
168 | atomic_set(&task->refcount, 0);
|
---|
169 | atomic_set(&task->lifecount, 0);
|
---|
170 |
|
---|
171 | irq_spinlock_initialize(&task->lock, "task_t_lock");
|
---|
172 |
|
---|
173 | list_initialize(&task->threads);
|
---|
174 |
|
---|
175 | ipc_answerbox_init(&task->answerbox, task);
|
---|
176 |
|
---|
177 | spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
|
---|
178 | list_initialize(&task->active_calls);
|
---|
179 |
|
---|
180 | #ifdef CONFIG_UDEBUG
|
---|
181 | /* Init kbox stuff */
|
---|
182 | task->kb.thread = NULL;
|
---|
183 | ipc_answerbox_init(&task->kb.box, task);
|
---|
184 | mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | return 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | size_t tsk_destructor(void *obj)
|
---|
191 | {
|
---|
192 | task_t *task = (task_t *) obj;
|
---|
193 |
|
---|
194 | caps_task_free(task);
|
---|
195 | return 0;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /** Create new task with no threads.
|
---|
199 | *
|
---|
200 | * @param as Task's address space.
|
---|
201 | * @param name Symbolic name (a copy is made).
|
---|
202 | *
|
---|
203 | * @return New task's structure.
|
---|
204 | *
|
---|
205 | */
|
---|
206 | task_t *task_create(as_t *as, const char *name)
|
---|
207 | {
|
---|
208 | task_t *task = (task_t *) slab_alloc(task_cache, 0);
|
---|
209 | task_create_arch(task);
|
---|
210 |
|
---|
211 | task->as = as;
|
---|
212 | str_cpy(task->name, TASK_NAME_BUFLEN, name);
|
---|
213 |
|
---|
214 | task->container = CONTAINER;
|
---|
215 | task->perms = 0;
|
---|
216 | task->ucycles = 0;
|
---|
217 | task->kcycles = 0;
|
---|
218 |
|
---|
219 | caps_task_init(task);
|
---|
220 |
|
---|
221 | task->ipc_info.call_sent = 0;
|
---|
222 | task->ipc_info.call_received = 0;
|
---|
223 | task->ipc_info.answer_sent = 0;
|
---|
224 | task->ipc_info.answer_received = 0;
|
---|
225 | task->ipc_info.irq_notif_received = 0;
|
---|
226 | task->ipc_info.forwarded = 0;
|
---|
227 |
|
---|
228 | event_task_init(task);
|
---|
229 |
|
---|
230 | task->answerbox.active = true;
|
---|
231 |
|
---|
232 | #ifdef CONFIG_UDEBUG
|
---|
233 | /* Init debugging stuff */
|
---|
234 | udebug_task_init(&task->udebug);
|
---|
235 |
|
---|
236 | /* Init kbox stuff */
|
---|
237 | task->kb.box.active = true;
|
---|
238 | task->kb.finished = false;
|
---|
239 | #endif
|
---|
240 |
|
---|
241 | if ((ipc_phone_0) &&
|
---|
242 | (container_check(ipc_phone_0->task->container, task->container))) {
|
---|
243 | cap_handle_t phone_handle = phone_alloc(task);
|
---|
244 | kobject_t *phone_obj = kobject_get(task, phone_handle,
|
---|
245 | KOBJECT_TYPE_PHONE);
|
---|
246 | (void) ipc_phone_connect(phone_obj->phone, ipc_phone_0);
|
---|
247 | }
|
---|
248 |
|
---|
249 | futex_task_init(task);
|
---|
250 |
|
---|
251 | /*
|
---|
252 | * Get a reference to the address space.
|
---|
253 | */
|
---|
254 | as_hold(task->as);
|
---|
255 |
|
---|
256 | irq_spinlock_lock(&tasks_lock, true);
|
---|
257 |
|
---|
258 | task->taskid = ++task_counter;
|
---|
259 | avltree_node_initialize(&task->tasks_tree_node);
|
---|
260 | task->tasks_tree_node.key = task->taskid;
|
---|
261 | avltree_insert(&tasks_tree, &task->tasks_tree_node);
|
---|
262 |
|
---|
263 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
264 |
|
---|
265 | return task;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /** Destroy task.
|
---|
269 | *
|
---|
270 | * @param task Task to be destroyed.
|
---|
271 | *
|
---|
272 | */
|
---|
273 | void task_destroy(task_t *task)
|
---|
274 | {
|
---|
275 | /*
|
---|
276 | * Remove the task from the task B+tree.
|
---|
277 | */
|
---|
278 | irq_spinlock_lock(&tasks_lock, true);
|
---|
279 | avltree_delete(&tasks_tree, &task->tasks_tree_node);
|
---|
280 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * Perform architecture specific task destruction.
|
---|
284 | */
|
---|
285 | task_destroy_arch(task);
|
---|
286 |
|
---|
287 | /*
|
---|
288 | * Free up dynamically allocated state.
|
---|
289 | */
|
---|
290 | futex_task_deinit(task);
|
---|
291 |
|
---|
292 | /*
|
---|
293 | * Drop our reference to the address space.
|
---|
294 | */
|
---|
295 | as_release(task->as);
|
---|
296 |
|
---|
297 | slab_free(task_cache, task);
|
---|
298 | }
|
---|
299 |
|
---|
300 | /** Hold a reference to a task.
|
---|
301 | *
|
---|
302 | * Holding a reference to a task prevents destruction of that task.
|
---|
303 | *
|
---|
304 | * @param task Task to be held.
|
---|
305 | *
|
---|
306 | */
|
---|
307 | void task_hold(task_t *task)
|
---|
308 | {
|
---|
309 | atomic_inc(&task->refcount);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /** Release a reference to a task.
|
---|
313 | *
|
---|
314 | * The last one to release a reference to a task destroys the task.
|
---|
315 | *
|
---|
316 | * @param task Task to be released.
|
---|
317 | *
|
---|
318 | */
|
---|
319 | void task_release(task_t *task)
|
---|
320 | {
|
---|
321 | if ((atomic_predec(&task->refcount)) == 0)
|
---|
322 | task_destroy(task);
|
---|
323 | }
|
---|
324 |
|
---|
325 | #ifdef __32_BITS__
|
---|
326 |
|
---|
327 | /** Syscall for reading task ID from userspace (32 bits)
|
---|
328 | *
|
---|
329 | * @param uspace_taskid Pointer to user-space buffer
|
---|
330 | * where to store current task ID.
|
---|
331 | *
|
---|
332 | * @return Zero on success or an error code from @ref errno.h.
|
---|
333 | *
|
---|
334 | */
|
---|
335 | sysarg_t sys_task_get_id(sysarg64_t *uspace_taskid)
|
---|
336 | {
|
---|
337 | /*
|
---|
338 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
339 | * the lifespan of the task.
|
---|
340 | */
|
---|
341 | return (sysarg_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
|
---|
342 | sizeof(TASK->taskid));
|
---|
343 | }
|
---|
344 |
|
---|
345 | #endif /* __32_BITS__ */
|
---|
346 |
|
---|
347 | #ifdef __64_BITS__
|
---|
348 |
|
---|
349 | /** Syscall for reading task ID from userspace (64 bits)
|
---|
350 | *
|
---|
351 | * @return Current task ID.
|
---|
352 | *
|
---|
353 | */
|
---|
354 | sysarg_t sys_task_get_id(void)
|
---|
355 | {
|
---|
356 | /*
|
---|
357 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
358 | * the lifespan of the task.
|
---|
359 | */
|
---|
360 | return TASK->taskid;
|
---|
361 | }
|
---|
362 |
|
---|
363 | #endif /* __64_BITS__ */
|
---|
364 |
|
---|
365 | /** Syscall for setting the task name.
|
---|
366 | *
|
---|
367 | * The name simplifies identifying the task in the task list.
|
---|
368 | *
|
---|
369 | * @param name The new name for the task. (typically the same
|
---|
370 | * as the command used to execute it).
|
---|
371 | *
|
---|
372 | * @return 0 on success or an error code from @ref errno.h.
|
---|
373 | *
|
---|
374 | */
|
---|
375 | sysarg_t sys_task_set_name(const char *uspace_name, size_t name_len)
|
---|
376 | {
|
---|
377 | char namebuf[TASK_NAME_BUFLEN];
|
---|
378 |
|
---|
379 | /* Cap length of name and copy it from userspace. */
|
---|
380 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
381 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
382 |
|
---|
383 | int rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
384 | if (rc != 0)
|
---|
385 | return (sysarg_t) rc;
|
---|
386 |
|
---|
387 | namebuf[name_len] = '\0';
|
---|
388 |
|
---|
389 | /*
|
---|
390 | * As the task name is referenced also from the
|
---|
391 | * threads, lock the threads' lock for the course
|
---|
392 | * of the update.
|
---|
393 | */
|
---|
394 |
|
---|
395 | irq_spinlock_lock(&tasks_lock, true);
|
---|
396 | irq_spinlock_lock(&TASK->lock, false);
|
---|
397 | irq_spinlock_lock(&threads_lock, false);
|
---|
398 |
|
---|
399 | /* Set task name */
|
---|
400 | str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
|
---|
401 |
|
---|
402 | irq_spinlock_unlock(&threads_lock, false);
|
---|
403 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
404 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
405 |
|
---|
406 | return EOK;
|
---|
407 | }
|
---|
408 |
|
---|
409 | /** Syscall to forcefully terminate a task
|
---|
410 | *
|
---|
411 | * @param uspace_taskid Pointer to task ID in user space.
|
---|
412 | *
|
---|
413 | * @return 0 on success or an error code from @ref errno.h.
|
---|
414 | *
|
---|
415 | */
|
---|
416 | sysarg_t sys_task_kill(task_id_t *uspace_taskid)
|
---|
417 | {
|
---|
418 | task_id_t taskid;
|
---|
419 | int rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
|
---|
420 | if (rc != 0)
|
---|
421 | return (sysarg_t) rc;
|
---|
422 |
|
---|
423 | return (sysarg_t) task_kill(taskid);
|
---|
424 | }
|
---|
425 |
|
---|
426 | /** Find task structure corresponding to task ID.
|
---|
427 | *
|
---|
428 | * The tasks_lock must be already held by the caller of this function and
|
---|
429 | * interrupts must be disabled.
|
---|
430 | *
|
---|
431 | * @param id Task ID.
|
---|
432 | *
|
---|
433 | * @return Task structure address or NULL if there is no such task ID.
|
---|
434 | *
|
---|
435 | */
|
---|
436 | task_t *task_find_by_id(task_id_t id)
|
---|
437 | {
|
---|
438 | assert(interrupts_disabled());
|
---|
439 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
440 |
|
---|
441 | avltree_node_t *node =
|
---|
442 | avltree_search(&tasks_tree, (avltree_key_t) id);
|
---|
443 |
|
---|
444 | if (node)
|
---|
445 | return avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
446 |
|
---|
447 | return NULL;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /** Get accounting data of given task.
|
---|
451 | *
|
---|
452 | * Note that task lock of 'task' must be already held and interrupts must be
|
---|
453 | * already disabled.
|
---|
454 | *
|
---|
455 | * @param task Pointer to the task.
|
---|
456 | * @param ucycles Out pointer to sum of all user cycles.
|
---|
457 | * @param kcycles Out pointer to sum of all kernel cycles.
|
---|
458 | *
|
---|
459 | */
|
---|
460 | void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
|
---|
461 | {
|
---|
462 | assert(interrupts_disabled());
|
---|
463 | assert(irq_spinlock_locked(&task->lock));
|
---|
464 |
|
---|
465 | /* Accumulated values of task */
|
---|
466 | uint64_t uret = task->ucycles;
|
---|
467 | uint64_t kret = task->kcycles;
|
---|
468 |
|
---|
469 | /* Current values of threads */
|
---|
470 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
471 | irq_spinlock_lock(&thread->lock, false);
|
---|
472 |
|
---|
473 | /* Process only counted threads */
|
---|
474 | if (!thread->uncounted) {
|
---|
475 | if (thread == THREAD) {
|
---|
476 | /* Update accounting of current thread */
|
---|
477 | thread_update_accounting(false);
|
---|
478 | }
|
---|
479 |
|
---|
480 | uret += thread->ucycles;
|
---|
481 | kret += thread->kcycles;
|
---|
482 | }
|
---|
483 |
|
---|
484 | irq_spinlock_unlock(&thread->lock, false);
|
---|
485 | }
|
---|
486 |
|
---|
487 | *ucycles = uret;
|
---|
488 | *kcycles = kret;
|
---|
489 | }
|
---|
490 |
|
---|
491 | static void task_kill_internal(task_t *task)
|
---|
492 | {
|
---|
493 | irq_spinlock_lock(&task->lock, false);
|
---|
494 | irq_spinlock_lock(&threads_lock, false);
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * Interrupt all threads.
|
---|
498 | */
|
---|
499 |
|
---|
500 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
501 | bool sleeping = false;
|
---|
502 |
|
---|
503 | irq_spinlock_lock(&thread->lock, false);
|
---|
504 |
|
---|
505 | thread->interrupted = true;
|
---|
506 | if (thread->state == Sleeping)
|
---|
507 | sleeping = true;
|
---|
508 |
|
---|
509 | irq_spinlock_unlock(&thread->lock, false);
|
---|
510 |
|
---|
511 | if (sleeping)
|
---|
512 | waitq_interrupt_sleep(thread);
|
---|
513 | }
|
---|
514 |
|
---|
515 | irq_spinlock_unlock(&threads_lock, false);
|
---|
516 | irq_spinlock_unlock(&task->lock, false);
|
---|
517 | }
|
---|
518 |
|
---|
519 | /** Kill task.
|
---|
520 | *
|
---|
521 | * This function is idempotent.
|
---|
522 | * It signals all the task's threads to bail it out.
|
---|
523 | *
|
---|
524 | * @param id ID of the task to be killed.
|
---|
525 | *
|
---|
526 | * @return Zero on success or an error code from errno.h.
|
---|
527 | *
|
---|
528 | */
|
---|
529 | int task_kill(task_id_t id)
|
---|
530 | {
|
---|
531 | if (id == 1)
|
---|
532 | return EPERM;
|
---|
533 |
|
---|
534 | irq_spinlock_lock(&tasks_lock, true);
|
---|
535 |
|
---|
536 | task_t *task = task_find_by_id(id);
|
---|
537 | if (!task) {
|
---|
538 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
539 | return ENOENT;
|
---|
540 | }
|
---|
541 |
|
---|
542 | task_kill_internal(task);
|
---|
543 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
544 |
|
---|
545 | return EOK;
|
---|
546 | }
|
---|
547 |
|
---|
548 | /** Kill the currently running task.
|
---|
549 | *
|
---|
550 | * @param notify Send out fault notifications.
|
---|
551 | *
|
---|
552 | * @return Zero on success or an error code from errno.h.
|
---|
553 | *
|
---|
554 | */
|
---|
555 | void task_kill_self(bool notify)
|
---|
556 | {
|
---|
557 | /*
|
---|
558 | * User space can subscribe for FAULT events to take action
|
---|
559 | * whenever a task faults (to take a dump, run a debugger, etc.).
|
---|
560 | * The notification is always available, but unless udebug is enabled,
|
---|
561 | * that's all you get.
|
---|
562 | */
|
---|
563 | if (notify) {
|
---|
564 | /* Notify the subscriber that a fault occurred. */
|
---|
565 | if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
|
---|
566 | UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
|
---|
567 | #ifdef CONFIG_UDEBUG
|
---|
568 | /* Wait for a debugging session. */
|
---|
569 | udebug_thread_fault();
|
---|
570 | #endif
|
---|
571 | }
|
---|
572 | }
|
---|
573 |
|
---|
574 | irq_spinlock_lock(&tasks_lock, true);
|
---|
575 | task_kill_internal(TASK);
|
---|
576 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
577 |
|
---|
578 | thread_exit();
|
---|
579 | }
|
---|
580 |
|
---|
581 | /** Process syscall to terminate the current task.
|
---|
582 | *
|
---|
583 | * @param notify Send out fault notifications.
|
---|
584 | *
|
---|
585 | */
|
---|
586 | sysarg_t sys_task_exit(sysarg_t notify)
|
---|
587 | {
|
---|
588 | task_kill_self(notify);
|
---|
589 |
|
---|
590 | /* Unreachable */
|
---|
591 | return EOK;
|
---|
592 | }
|
---|
593 |
|
---|
594 | static bool task_print_walker(avltree_node_t *node, void *arg)
|
---|
595 | {
|
---|
596 | bool *additional = (bool *) arg;
|
---|
597 | task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
|
---|
598 | irq_spinlock_lock(&task->lock, false);
|
---|
599 |
|
---|
600 | uint64_t ucycles;
|
---|
601 | uint64_t kcycles;
|
---|
602 | char usuffix, ksuffix;
|
---|
603 | task_get_accounting(task, &ucycles, &kcycles);
|
---|
604 | order_suffix(ucycles, &ucycles, &usuffix);
|
---|
605 | order_suffix(kcycles, &kcycles, &ksuffix);
|
---|
606 |
|
---|
607 | #ifdef __32_BITS__
|
---|
608 | if (*additional)
|
---|
609 | printf("%-8" PRIu64 " %9" PRIua, task->taskid,
|
---|
610 | atomic_get(&task->refcount));
|
---|
611 | else
|
---|
612 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
|
---|
613 | " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
|
---|
614 | task->name, task->container, task, task->as,
|
---|
615 | ucycles, usuffix, kcycles, ksuffix);
|
---|
616 | #endif
|
---|
617 |
|
---|
618 | #ifdef __64_BITS__
|
---|
619 | if (*additional)
|
---|
620 | printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
|
---|
621 | "%9" PRIua "\n", task->taskid, ucycles, usuffix, kcycles,
|
---|
622 | ksuffix, atomic_get(&task->refcount));
|
---|
623 | else
|
---|
624 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
|
---|
625 | task->taskid, task->name, task->container, task, task->as);
|
---|
626 | #endif
|
---|
627 |
|
---|
628 | irq_spinlock_unlock(&task->lock, false);
|
---|
629 | return true;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /** Print task list
|
---|
633 | *
|
---|
634 | * @param additional Print additional information.
|
---|
635 | *
|
---|
636 | */
|
---|
637 | void task_print_list(bool additional)
|
---|
638 | {
|
---|
639 | /* Messing with task structures, avoid deadlock */
|
---|
640 | irq_spinlock_lock(&tasks_lock, true);
|
---|
641 |
|
---|
642 | #ifdef __32_BITS__
|
---|
643 | if (additional)
|
---|
644 | printf("[id ] [threads] [calls] [callee\n");
|
---|
645 | else
|
---|
646 | printf("[id ] [name ] [ctn] [address ] [as ]"
|
---|
647 | " [ucycles ] [kcycles ]\n");
|
---|
648 | #endif
|
---|
649 |
|
---|
650 | #ifdef __64_BITS__
|
---|
651 | if (additional)
|
---|
652 | printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
|
---|
653 | " [callee\n");
|
---|
654 | else
|
---|
655 | printf("[id ] [name ] [ctn] [address ]"
|
---|
656 | " [as ]\n");
|
---|
657 | #endif
|
---|
658 |
|
---|
659 | avltree_walk(&tasks_tree, task_print_walker, &additional);
|
---|
660 |
|
---|
661 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
662 | }
|
---|
663 |
|
---|
664 | /** @}
|
---|
665 | */
|
---|