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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6f7071b was 6f7071b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Use ordered dictionary for kernel pareas instead of B+ tree.

  • Property mode set to 100644
File size: 16.6 KB
Line 
1/*
2 * Copyright (c) 2010 Jakub Jermar
3 * Copyright (c) 2018 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup kernel_generic_proc
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Task management.
37 */
38
39#include <assert.h>
40#include <proc/thread.h>
41#include <proc/task.h>
42#include <mm/as.h>
43#include <mm/slab.h>
44#include <atomic.h>
45#include <synch/futex.h>
46#include <synch/spinlock.h>
47#include <synch/waitq.h>
48#include <arch.h>
49#include <barrier.h>
50#include <adt/list.h>
51#include <adt/odict.h>
52#include <cap/cap.h>
53#include <ipc/ipc.h>
54#include <ipc/ipcrsc.h>
55#include <ipc/event.h>
56#include <stdio.h>
57#include <errno.h>
58#include <halt.h>
59#include <str.h>
60#include <syscall/copy.h>
61#include <macros.h>
62
63/** Spinlock protecting the @c tasks ordered dictionary. */
64IRQ_SPINLOCK_INITIALIZE(tasks_lock);
65
66/** Ordered dictionary of active tasks.
67 *
68 * The task is guaranteed to exist after it was found in the @c tasks
69 * dictionary as 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 */
77odict_t tasks;
78
79static task_id_t task_counter = 0;
80
81static slab_cache_t *task_cache;
82
83/* Forward declarations. */
84static void task_kill_internal(task_t *);
85static errno_t tsk_constructor(void *, unsigned int);
86static size_t tsk_destructor(void *);
87
88static void *tasks_getkey(odlink_t *);
89static int tasks_cmp(void *, void *);
90
91/** Initialize kernel tasks support.
92 *
93 */
94void task_init(void)
95{
96 TASK = NULL;
97 odict_initialize(&tasks, tasks_getkey, tasks_cmp);
98 task_cache = slab_cache_create("task_t", sizeof(task_t), 0,
99 tsk_constructor, tsk_destructor, 0);
100}
101
102/** Kill all tasks except the current task.
103 *
104 */
105void task_done(void)
106{
107 size_t tasks_left;
108 task_t *task;
109
110 if (ipc_box_0) {
111 task_t *task_0 = ipc_box_0->task;
112 ipc_box_0 = NULL;
113 /*
114 * The first task is held by kinit(), we need to release it or
115 * it will never finish cleanup.
116 */
117 task_release(task_0);
118 }
119
120 /* Repeat until there are any tasks except TASK */
121 do {
122#ifdef CONFIG_DEBUG
123 printf("Killing tasks... ");
124#endif
125 irq_spinlock_lock(&tasks_lock, true);
126 tasks_left = 0;
127
128 task = task_first();
129 while (task != NULL) {
130 if (task != TASK) {
131 tasks_left++;
132#ifdef CONFIG_DEBUG
133 printf("[%" PRIu64 "] ", task->taskid);
134#endif
135 task_kill_internal(task);
136 }
137
138 task = task_next(task);
139 }
140
141 irq_spinlock_unlock(&tasks_lock, true);
142
143 thread_sleep(1);
144
145#ifdef CONFIG_DEBUG
146 printf("\n");
147#endif
148 } while (tasks_left > 0);
149}
150
151errno_t tsk_constructor(void *obj, unsigned int kmflags)
152{
153 task_t *task = (task_t *) obj;
154
155 errno_t rc = caps_task_alloc(task);
156 if (rc != EOK)
157 return rc;
158
159 atomic_store(&task->refcount, 0);
160 atomic_store(&task->lifecount, 0);
161
162 irq_spinlock_initialize(&task->lock, "task_t_lock");
163
164 list_initialize(&task->threads);
165
166 ipc_answerbox_init(&task->answerbox, task);
167
168 spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
169 list_initialize(&task->active_calls);
170
171#ifdef CONFIG_UDEBUG
172 /* Init kbox stuff */
173 task->kb.thread = NULL;
174 ipc_answerbox_init(&task->kb.box, task);
175 mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
176#endif
177
178 return EOK;
179}
180
181size_t tsk_destructor(void *obj)
182{
183 task_t *task = (task_t *) obj;
184
185 caps_task_free(task);
186 return 0;
187}
188
189/** Create new task with no threads.
190 *
191 * @param as Task's address space.
192 * @param name Symbolic name (a copy is made).
193 *
194 * @return New task's structure.
195 *
196 */
197task_t *task_create(as_t *as, const char *name)
198{
199 task_t *task = (task_t *) slab_alloc(task_cache, 0);
200 if (task == NULL) {
201 return NULL;
202 }
203
204 task_create_arch(task);
205
206 task->as = as;
207 str_cpy(task->name, TASK_NAME_BUFLEN, name);
208
209 task->container = CONTAINER;
210 task->perms = 0;
211 task->ucycles = 0;
212 task->kcycles = 0;
213
214 caps_task_init(task);
215
216 task->ipc_info.call_sent = 0;
217 task->ipc_info.call_received = 0;
218 task->ipc_info.answer_sent = 0;
219 task->ipc_info.answer_received = 0;
220 task->ipc_info.irq_notif_received = 0;
221 task->ipc_info.forwarded = 0;
222
223 event_task_init(task);
224
225 task->answerbox.active = true;
226
227#ifdef CONFIG_UDEBUG
228 /* Init debugging stuff */
229 udebug_task_init(&task->udebug);
230
231 /* Init kbox stuff */
232 task->kb.box.active = true;
233 task->kb.finished = false;
234#endif
235
236 if ((ipc_box_0) &&
237 (container_check(ipc_box_0->task->container, task->container))) {
238 cap_phone_handle_t phone_handle;
239 errno_t rc = phone_alloc(task, true, &phone_handle, NULL);
240 if (rc != EOK) {
241 task->as = NULL;
242 task_destroy_arch(task);
243 slab_free(task_cache, task);
244 return NULL;
245 }
246
247 kobject_t *phone_obj = kobject_get(task, phone_handle,
248 KOBJECT_TYPE_PHONE);
249 (void) ipc_phone_connect(phone_obj->phone, ipc_box_0);
250 }
251
252 futex_task_init(task);
253
254 /*
255 * Get a reference to the address space.
256 */
257 as_hold(task->as);
258
259 irq_spinlock_lock(&tasks_lock, true);
260
261 task->taskid = ++task_counter;
262 odlink_initialize(&task->ltasks);
263 odict_insert(&task->ltasks, &tasks, NULL);
264
265 irq_spinlock_unlock(&tasks_lock, true);
266
267 return task;
268}
269
270/** Destroy task.
271 *
272 * @param task Task to be destroyed.
273 *
274 */
275void task_destroy(task_t *task)
276{
277 /*
278 * Remove the task from the task B+tree.
279 */
280 irq_spinlock_lock(&tasks_lock, true);
281 odict_remove(&task->ltasks);
282 irq_spinlock_unlock(&tasks_lock, true);
283
284 /*
285 * Perform architecture specific task destruction.
286 */
287 task_destroy_arch(task);
288
289 /*
290 * Free up dynamically allocated state.
291 */
292 futex_task_deinit(task);
293
294 /*
295 * Drop our reference to the address space.
296 */
297 as_release(task->as);
298
299 slab_free(task_cache, task);
300}
301
302/** Hold a reference to a task.
303 *
304 * Holding a reference to a task prevents destruction of that task.
305 *
306 * @param task Task to be held.
307 *
308 */
309void task_hold(task_t *task)
310{
311 atomic_inc(&task->refcount);
312}
313
314/** Release a reference to a task.
315 *
316 * The last one to release a reference to a task destroys the task.
317 *
318 * @param task Task to be released.
319 *
320 */
321void task_release(task_t *task)
322{
323 if ((atomic_predec(&task->refcount)) == 0)
324 task_destroy(task);
325}
326
327#ifdef __32_BITS__
328
329/** Syscall for reading task ID from userspace (32 bits)
330 *
331 * @param uspace_taskid Pointer to user-space buffer
332 * where to store current task ID.
333 *
334 * @return Zero on success or an error code from @ref errno.h.
335 *
336 */
337sys_errno_t sys_task_get_id(sysarg64_t *uspace_taskid)
338{
339 /*
340 * No need to acquire lock on TASK because taskid remains constant for
341 * the lifespan of the task.
342 */
343 return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
344 sizeof(TASK->taskid));
345}
346
347#endif /* __32_BITS__ */
348
349#ifdef __64_BITS__
350
351/** Syscall for reading task ID from userspace (64 bits)
352 *
353 * @return Current task ID.
354 *
355 */
356sysarg_t sys_task_get_id(void)
357{
358 /*
359 * No need to acquire lock on TASK because taskid remains constant for
360 * the lifespan of the task.
361 */
362 return TASK->taskid;
363}
364
365#endif /* __64_BITS__ */
366
367/** Syscall for setting the task name.
368 *
369 * The name simplifies identifying the task in the task list.
370 *
371 * @param name The new name for the task. (typically the same
372 * as the command used to execute it).
373 *
374 * @return 0 on success or an error code from @ref errno.h.
375 *
376 */
377sys_errno_t sys_task_set_name(const char *uspace_name, size_t name_len)
378{
379 char namebuf[TASK_NAME_BUFLEN];
380
381 /* Cap length of name and copy it from userspace. */
382 if (name_len > TASK_NAME_BUFLEN - 1)
383 name_len = TASK_NAME_BUFLEN - 1;
384
385 errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
386 if (rc != EOK)
387 return (sys_errno_t) rc;
388
389 namebuf[name_len] = '\0';
390
391 /*
392 * As the task name is referenced also from the
393 * threads, lock the threads' lock for the course
394 * of the update.
395 */
396
397 irq_spinlock_lock(&tasks_lock, true);
398 irq_spinlock_lock(&TASK->lock, false);
399 irq_spinlock_lock(&threads_lock, false);
400
401 /* Set task name */
402 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
403
404 irq_spinlock_unlock(&threads_lock, false);
405 irq_spinlock_unlock(&TASK->lock, false);
406 irq_spinlock_unlock(&tasks_lock, true);
407
408 return EOK;
409}
410
411/** Syscall to forcefully terminate a task
412 *
413 * @param uspace_taskid Pointer to task ID in user space.
414 *
415 * @return 0 on success or an error code from @ref errno.h.
416 *
417 */
418sys_errno_t sys_task_kill(task_id_t *uspace_taskid)
419{
420 task_id_t taskid;
421 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
422 if (rc != EOK)
423 return (sys_errno_t) rc;
424
425 return (sys_errno_t) task_kill(taskid);
426}
427
428/** Find task structure corresponding to task ID.
429 *
430 * The tasks_lock must be already held by the caller of this function and
431 * interrupts must be disabled.
432 *
433 * @param id Task ID.
434 *
435 * @return Task structure address or NULL if there is no such task ID.
436 *
437 */
438task_t *task_find_by_id(task_id_t id)
439{
440 assert(interrupts_disabled());
441 assert(irq_spinlock_locked(&tasks_lock));
442
443 odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
444 if (odlink != NULL)
445 return odict_get_instance(odlink, task_t, ltasks);
446
447 return NULL;
448}
449
450/** Get count of tasks.
451 *
452 * @return Number of tasks in the system
453 */
454size_t task_count(void)
455{
456 assert(interrupts_disabled());
457 assert(irq_spinlock_locked(&tasks_lock));
458
459 return odict_count(&tasks);
460}
461
462/** Get first task (task with lowest ID).
463 *
464 * @return Pointer to first task or @c NULL if there are none.
465 */
466task_t *task_first(void)
467{
468 odlink_t *odlink;
469
470 assert(interrupts_disabled());
471 assert(irq_spinlock_locked(&tasks_lock));
472
473 odlink = odict_first(&tasks);
474 if (odlink == NULL)
475 return NULL;
476
477 return odict_get_instance(odlink, task_t, ltasks);
478}
479
480/** Get next task (with higher task ID).
481 *
482 * @param cur Current task
483 * @return Pointer to next task or @c NULL if there are no more tasks.
484 */
485task_t *task_next(task_t *cur)
486{
487 odlink_t *odlink;
488
489 assert(interrupts_disabled());
490 assert(irq_spinlock_locked(&tasks_lock));
491
492 odlink = odict_next(&cur->ltasks, &tasks);
493 if (odlink == NULL)
494 return NULL;
495
496 return odict_get_instance(odlink, task_t, ltasks);
497}
498
499/** Get accounting data of given task.
500 *
501 * Note that task lock of 'task' must be already held and interrupts must be
502 * already disabled.
503 *
504 * @param task Pointer to the task.
505 * @param ucycles Out pointer to sum of all user cycles.
506 * @param kcycles Out pointer to sum of all kernel cycles.
507 *
508 */
509void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
510{
511 assert(interrupts_disabled());
512 assert(irq_spinlock_locked(&task->lock));
513
514 /* Accumulated values of task */
515 uint64_t uret = task->ucycles;
516 uint64_t kret = task->kcycles;
517
518 /* Current values of threads */
519 list_foreach(task->threads, th_link, thread_t, thread) {
520 irq_spinlock_lock(&thread->lock, false);
521
522 /* Process only counted threads */
523 if (!thread->uncounted) {
524 if (thread == THREAD) {
525 /* Update accounting of current thread */
526 thread_update_accounting(false);
527 }
528
529 uret += thread->ucycles;
530 kret += thread->kcycles;
531 }
532
533 irq_spinlock_unlock(&thread->lock, false);
534 }
535
536 *ucycles = uret;
537 *kcycles = kret;
538}
539
540static void task_kill_internal(task_t *task)
541{
542 irq_spinlock_lock(&task->lock, false);
543 irq_spinlock_lock(&threads_lock, false);
544
545 /*
546 * Interrupt all threads.
547 */
548
549 list_foreach(task->threads, th_link, thread_t, thread) {
550 bool sleeping = false;
551
552 irq_spinlock_lock(&thread->lock, false);
553
554 thread->interrupted = true;
555 if (thread->state == Sleeping)
556 sleeping = true;
557
558 irq_spinlock_unlock(&thread->lock, false);
559
560 if (sleeping)
561 waitq_interrupt_sleep(thread);
562 }
563
564 irq_spinlock_unlock(&threads_lock, false);
565 irq_spinlock_unlock(&task->lock, false);
566}
567
568/** Kill task.
569 *
570 * This function is idempotent.
571 * It signals all the task's threads to bail it out.
572 *
573 * @param id ID of the task to be killed.
574 *
575 * @return Zero on success or an error code from errno.h.
576 *
577 */
578errno_t task_kill(task_id_t id)
579{
580 if (id == 1)
581 return EPERM;
582
583 irq_spinlock_lock(&tasks_lock, true);
584
585 task_t *task = task_find_by_id(id);
586 if (!task) {
587 irq_spinlock_unlock(&tasks_lock, true);
588 return ENOENT;
589 }
590
591 task_kill_internal(task);
592 irq_spinlock_unlock(&tasks_lock, true);
593
594 return EOK;
595}
596
597/** Kill the currently running task.
598 *
599 * @param notify Send out fault notifications.
600 *
601 * @return Zero on success or an error code from errno.h.
602 *
603 */
604void task_kill_self(bool notify)
605{
606 /*
607 * User space can subscribe for FAULT events to take action
608 * whenever a task faults (to take a dump, run a debugger, etc.).
609 * The notification is always available, but unless udebug is enabled,
610 * that's all you get.
611 */
612 if (notify) {
613 /* Notify the subscriber that a fault occurred. */
614 if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
615 UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
616#ifdef CONFIG_UDEBUG
617 /* Wait for a debugging session. */
618 udebug_thread_fault();
619#endif
620 }
621 }
622
623 irq_spinlock_lock(&tasks_lock, true);
624 task_kill_internal(TASK);
625 irq_spinlock_unlock(&tasks_lock, true);
626
627 thread_exit();
628}
629
630/** Process syscall to terminate the current task.
631 *
632 * @param notify Send out fault notifications.
633 *
634 */
635sys_errno_t sys_task_exit(sysarg_t notify)
636{
637 task_kill_self(notify);
638
639 /* Unreachable */
640 return EOK;
641}
642
643static void task_print(task_t *task, bool additional)
644{
645 irq_spinlock_lock(&task->lock, false);
646
647 uint64_t ucycles;
648 uint64_t kcycles;
649 char usuffix, ksuffix;
650 task_get_accounting(task, &ucycles, &kcycles);
651 order_suffix(ucycles, &ucycles, &usuffix);
652 order_suffix(kcycles, &kcycles, &ksuffix);
653
654#ifdef __32_BITS__
655 if (additional)
656 printf("%-8" PRIu64 " %9zu", task->taskid,
657 atomic_load(&task->refcount));
658 else
659 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
660 " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
661 task->name, task->container, task, task->as,
662 ucycles, usuffix, kcycles, ksuffix);
663#endif
664
665#ifdef __64_BITS__
666 if (additional)
667 printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
668 "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
669 ksuffix, atomic_load(&task->refcount));
670 else
671 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
672 task->taskid, task->name, task->container, task, task->as);
673#endif
674
675 irq_spinlock_unlock(&task->lock, false);
676}
677
678/** Print task list
679 *
680 * @param additional Print additional information.
681 *
682 */
683void task_print_list(bool additional)
684{
685 /* Messing with task structures, avoid deadlock */
686 irq_spinlock_lock(&tasks_lock, true);
687
688#ifdef __32_BITS__
689 if (additional)
690 printf("[id ] [threads] [calls] [callee\n");
691 else
692 printf("[id ] [name ] [ctn] [address ] [as ]"
693 " [ucycles ] [kcycles ]\n");
694#endif
695
696#ifdef __64_BITS__
697 if (additional)
698 printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
699 " [callee\n");
700 else
701 printf("[id ] [name ] [ctn] [address ]"
702 " [as ]\n");
703#endif
704
705 task_t *task;
706
707 task = task_first();
708 while (task != NULL) {
709 task_print(task, additional);
710 task = task_next(task);
711 }
712
713 irq_spinlock_unlock(&tasks_lock, true);
714}
715
716/** Get key function for the @c tasks ordered dictionary.
717 *
718 * @param odlink Link
719 * @return Pointer to task ID cast as 'void *'
720 */
721static void *tasks_getkey(odlink_t *odlink)
722{
723 task_t *task = odict_get_instance(odlink, task_t, ltasks);
724 return (void *) &task->taskid;
725}
726
727/** Key comparison function for the @c tasks ordered dictionary.
728 *
729 * @param a Pointer to thread A ID
730 * @param b Pointer to thread B ID
731 * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
732 */
733static int tasks_cmp(void *a, void *b)
734{
735 task_id_t ida = *(task_id_t *)a;
736 task_id_t idb = *(task_id_t *)b;
737
738 if (ida < idb)
739 return -1;
740 else if (ida == idb)
741 return 0;
742 else
743 return +1;
744}
745
746/** @}
747 */
Note: See TracBrowser for help on using the repository browser.