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

topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dcd8214 was 111b9b9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Reimplement waitq using thread_wait/wakeup

This adds a few functions to the thread API which can be
summarized as "stop running until woken up by others".
The ordering and context-switching concerns are thus yeeted
to this abstraction and waitq only deals with maintaining
the queues. Overall, this makes the control flow in waitq
much easier to navigate.

  • Property mode set to 100644
File size: 16.1 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/spinlock.h>
46#include <synch/waitq.h>
47#include <arch.h>
48#include <barrier.h>
49#include <adt/list.h>
50#include <adt/odict.h>
51#include <cap/cap.h>
52#include <ipc/ipc.h>
53#include <ipc/ipcrsc.h>
54#include <ipc/event.h>
55#include <stdio.h>
56#include <errno.h>
57#include <halt.h>
58#include <str.h>
59#include <syscall/copy.h>
60#include <macros.h>
61
62/** Spinlock protecting the @c tasks ordered dictionary. */
63IRQ_SPINLOCK_INITIALIZE(tasks_lock);
64
65/** Ordered dictionary of active tasks by task ID.
66 *
67 * Members are task_t structures.
68 *
69 * The task is guaranteed to exist after it was found in the @c tasks
70 * dictionary as long as:
71 *
72 * @li the tasks_lock is held,
73 * @li the task's lock is held when task's lock is acquired before releasing
74 * tasks_lock or
75 * @li the task's refcount is greater than 0
76 *
77 */
78odict_t tasks;
79
80static task_id_t task_counter = 0;
81
82static slab_cache_t *task_cache;
83
84/* Forward declarations. */
85static void task_kill_internal(task_t *);
86static errno_t tsk_constructor(void *, unsigned int);
87static size_t tsk_destructor(void *);
88
89static void *tasks_getkey(odlink_t *);
90static int tasks_cmp(void *, void *);
91
92/** Initialize kernel tasks support.
93 *
94 */
95void task_init(void)
96{
97 TASK = NULL;
98 odict_initialize(&tasks, tasks_getkey, tasks_cmp);
99 task_cache = slab_cache_create("task_t", sizeof(task_t), 0,
100 tsk_constructor, tsk_destructor, 0);
101}
102
103/** Kill all tasks except the current task.
104 *
105 */
106void task_done(void)
107{
108 size_t tasks_left;
109 task_t *task;
110
111 if (ipc_box_0) {
112 task_t *task_0 = ipc_box_0->task;
113 ipc_box_0 = NULL;
114 /*
115 * The first task is held by kinit(), we need to release it or
116 * it will never finish cleanup.
117 */
118 task_release(task_0);
119 }
120
121 /* Repeat until there are any tasks except TASK */
122 do {
123#ifdef CONFIG_DEBUG
124 printf("Killing tasks... ");
125#endif
126 irq_spinlock_lock(&tasks_lock, true);
127 tasks_left = 0;
128
129 task = task_first();
130 while (task != NULL) {
131 if (task != TASK) {
132 tasks_left++;
133#ifdef CONFIG_DEBUG
134 printf("[%" PRIu64 "] ", task->taskid);
135#endif
136 task_kill_internal(task);
137 }
138
139 task = task_next(task);
140 }
141
142 irq_spinlock_unlock(&tasks_lock, true);
143
144 thread_sleep(1);
145
146#ifdef CONFIG_DEBUG
147 printf("\n");
148#endif
149 } while (tasks_left > 0);
150}
151
152errno_t tsk_constructor(void *obj, unsigned int kmflags)
153{
154 task_t *task = (task_t *) obj;
155
156 errno_t rc = caps_task_alloc(task);
157 if (rc != EOK)
158 return rc;
159
160 atomic_store(&task->refcount, 0);
161 atomic_store(&task->lifecount, 0);
162
163 irq_spinlock_initialize(&task->lock, "task_t_lock");
164
165 list_initialize(&task->threads);
166
167 ipc_answerbox_init(&task->answerbox, task);
168
169 spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
170 list_initialize(&task->active_calls);
171
172#ifdef CONFIG_UDEBUG
173 /* Init kbox stuff */
174 task->kb.thread = NULL;
175 ipc_answerbox_init(&task->kb.box, task);
176 mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
177#endif
178
179 return EOK;
180}
181
182size_t tsk_destructor(void *obj)
183{
184 task_t *task = (task_t *) obj;
185
186 caps_task_free(task);
187 return 0;
188}
189
190/** Create new task with no threads.
191 *
192 * @param as Task's address space.
193 * @param name Symbolic name (a copy is made).
194 *
195 * @return New task's structure.
196 *
197 */
198task_t *task_create(as_t *as, const char *name)
199{
200 task_t *task = (task_t *) slab_alloc(task_cache, FRAME_ATOMIC);
201 if (!task)
202 return NULL;
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 irq_spinlock_lock(&tasks_lock, true);
253
254 task->taskid = ++task_counter;
255 odlink_initialize(&task->ltasks);
256 odict_insert(&task->ltasks, &tasks, NULL);
257
258 irq_spinlock_unlock(&tasks_lock, true);
259
260 return task;
261}
262
263/** Destroy task.
264 *
265 * @param task Task to be destroyed.
266 *
267 */
268void task_destroy(task_t *task)
269{
270 /*
271 * Remove the task from the task odict.
272 */
273 irq_spinlock_lock(&tasks_lock, true);
274 odict_remove(&task->ltasks);
275 irq_spinlock_unlock(&tasks_lock, true);
276
277 /*
278 * Perform architecture specific task destruction.
279 */
280 task_destroy_arch(task);
281
282 /*
283 * Drop our reference to the address space.
284 */
285 as_release(task->as);
286
287 slab_free(task_cache, task);
288}
289
290/** Hold a reference to a task.
291 *
292 * Holding a reference to a task prevents destruction of that task.
293 *
294 * @param task Task to be held.
295 *
296 */
297void task_hold(task_t *task)
298{
299 atomic_inc(&task->refcount);
300}
301
302/** Release a reference to a task.
303 *
304 * The last one to release a reference to a task destroys the task.
305 *
306 * @param task Task to be released.
307 *
308 */
309void task_release(task_t *task)
310{
311 if ((atomic_predec(&task->refcount)) == 0)
312 task_destroy(task);
313}
314
315#ifdef __32_BITS__
316
317/** Syscall for reading task ID from userspace (32 bits)
318 *
319 * @param uspace_taskid Pointer to user-space buffer
320 * where to store current task ID.
321 *
322 * @return Zero on success or an error code from @ref errno.h.
323 *
324 */
325sys_errno_t sys_task_get_id(uspace_ptr_sysarg64_t uspace_taskid)
326{
327 /*
328 * No need to acquire lock on TASK because taskid remains constant for
329 * the lifespan of the task.
330 */
331 return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
332 sizeof(TASK->taskid));
333}
334
335#endif /* __32_BITS__ */
336
337#ifdef __64_BITS__
338
339/** Syscall for reading task ID from userspace (64 bits)
340 *
341 * @return Current task ID.
342 *
343 */
344sysarg_t sys_task_get_id(void)
345{
346 /*
347 * No need to acquire lock on TASK because taskid remains constant for
348 * the lifespan of the task.
349 */
350 return TASK->taskid;
351}
352
353#endif /* __64_BITS__ */
354
355/** Syscall for setting the task name.
356 *
357 * The name simplifies identifying the task in the task list.
358 *
359 * @param name The new name for the task. (typically the same
360 * as the command used to execute it).
361 *
362 * @return 0 on success or an error code from @ref errno.h.
363 *
364 */
365sys_errno_t sys_task_set_name(const uspace_ptr_char uspace_name, size_t name_len)
366{
367 char namebuf[TASK_NAME_BUFLEN];
368
369 /* Cap length of name and copy it from userspace. */
370 if (name_len > TASK_NAME_BUFLEN - 1)
371 name_len = TASK_NAME_BUFLEN - 1;
372
373 errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
374 if (rc != EOK)
375 return (sys_errno_t) rc;
376
377 namebuf[name_len] = '\0';
378
379 /*
380 * As the task name is referenced also from the
381 * threads, lock the threads' lock for the course
382 * of the update.
383 */
384
385 irq_spinlock_lock(&tasks_lock, true);
386 irq_spinlock_lock(&TASK->lock, false);
387
388 /* Set task name */
389 str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
390
391 irq_spinlock_unlock(&TASK->lock, false);
392 irq_spinlock_unlock(&tasks_lock, true);
393
394 return EOK;
395}
396
397/** Syscall to forcefully terminate a task
398 *
399 * @param uspace_taskid Pointer to task ID in user space.
400 *
401 * @return 0 on success or an error code from @ref errno.h.
402 *
403 */
404sys_errno_t sys_task_kill(uspace_ptr_task_id_t uspace_taskid)
405{
406 task_id_t taskid;
407 errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
408 if (rc != EOK)
409 return (sys_errno_t) rc;
410
411 return (sys_errno_t) task_kill(taskid);
412}
413
414/** Find task structure corresponding to task ID.
415 *
416 * The tasks_lock must be already held by the caller of this function and
417 * interrupts must be disabled.
418 *
419 * @param id Task ID.
420 *
421 * @return Task structure address or NULL if there is no such task ID.
422 *
423 */
424task_t *task_find_by_id(task_id_t id)
425{
426 assert(interrupts_disabled());
427 assert(irq_spinlock_locked(&tasks_lock));
428
429 odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
430 if (odlink != NULL)
431 return odict_get_instance(odlink, task_t, ltasks);
432
433 return NULL;
434}
435
436/** Get count of tasks.
437 *
438 * @return Number of tasks in the system
439 */
440size_t task_count(void)
441{
442 assert(interrupts_disabled());
443 assert(irq_spinlock_locked(&tasks_lock));
444
445 return odict_count(&tasks);
446}
447
448/** Get first task (task with lowest ID).
449 *
450 * @return Pointer to first task or @c NULL if there are none.
451 */
452task_t *task_first(void)
453{
454 odlink_t *odlink;
455
456 assert(interrupts_disabled());
457 assert(irq_spinlock_locked(&tasks_lock));
458
459 odlink = odict_first(&tasks);
460 if (odlink == NULL)
461 return NULL;
462
463 return odict_get_instance(odlink, task_t, ltasks);
464}
465
466/** Get next task (with higher task ID).
467 *
468 * @param cur Current task
469 * @return Pointer to next task or @c NULL if there are no more tasks.
470 */
471task_t *task_next(task_t *cur)
472{
473 odlink_t *odlink;
474
475 assert(interrupts_disabled());
476 assert(irq_spinlock_locked(&tasks_lock));
477
478 odlink = odict_next(&cur->ltasks, &tasks);
479 if (odlink == NULL)
480 return NULL;
481
482 return odict_get_instance(odlink, task_t, ltasks);
483}
484
485/** Get accounting data of given task.
486 *
487 * Note that task lock of 'task' must be already held and interrupts must be
488 * already disabled.
489 *
490 * @param task Pointer to the task.
491 * @param ucycles Out pointer to sum of all user cycles.
492 * @param kcycles Out pointer to sum of all kernel cycles.
493 *
494 */
495void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
496{
497 assert(interrupts_disabled());
498 assert(irq_spinlock_locked(&task->lock));
499
500 /* Accumulated values of task */
501 uint64_t uret = task->ucycles;
502 uint64_t kret = task->kcycles;
503
504 /* Current values of threads */
505 list_foreach(task->threads, th_link, thread_t, thread) {
506 irq_spinlock_lock(&thread->lock, false);
507
508 /* Process only counted threads */
509 if (!thread->uncounted) {
510 if (thread == THREAD) {
511 /* Update accounting of current thread */
512 thread_update_accounting(false);
513 }
514
515 uret += thread->ucycles;
516 kret += thread->kcycles;
517 }
518
519 irq_spinlock_unlock(&thread->lock, false);
520 }
521
522 *ucycles = uret;
523 *kcycles = kret;
524}
525
526static void task_kill_internal(task_t *task)
527{
528 irq_spinlock_lock(&task->lock, false);
529
530 /*
531 * Interrupt all threads.
532 */
533
534 list_foreach(task->threads, th_link, thread_t, thread) {
535 thread_interrupt(thread);
536 }
537
538 irq_spinlock_unlock(&task->lock, false);
539}
540
541/** Kill task.
542 *
543 * This function is idempotent.
544 * It signals all the task's threads to bail it out.
545 *
546 * @param id ID of the task to be killed.
547 *
548 * @return Zero on success or an error code from errno.h.
549 *
550 */
551errno_t task_kill(task_id_t id)
552{
553 if (id == 1)
554 return EPERM;
555
556 irq_spinlock_lock(&tasks_lock, true);
557
558 task_t *task = task_find_by_id(id);
559 if (!task) {
560 irq_spinlock_unlock(&tasks_lock, true);
561 return ENOENT;
562 }
563
564 task_kill_internal(task);
565 irq_spinlock_unlock(&tasks_lock, true);
566
567 return EOK;
568}
569
570/** Kill the currently running task.
571 *
572 * @param notify Send out fault notifications.
573 *
574 * @return Zero on success or an error code from errno.h.
575 *
576 */
577void task_kill_self(bool notify)
578{
579 /*
580 * User space can subscribe for FAULT events to take action
581 * whenever a task faults (to take a dump, run a debugger, etc.).
582 * The notification is always available, but unless udebug is enabled,
583 * that's all you get.
584 */
585 if (notify) {
586 /* Notify the subscriber that a fault occurred. */
587 if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
588 UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
589#ifdef CONFIG_UDEBUG
590 /* Wait for a debugging session. */
591 udebug_thread_fault();
592#endif
593 }
594 }
595
596 irq_spinlock_lock(&tasks_lock, true);
597 task_kill_internal(TASK);
598 irq_spinlock_unlock(&tasks_lock, true);
599
600 thread_exit();
601}
602
603/** Process syscall to terminate the current task.
604 *
605 * @param notify Send out fault notifications.
606 *
607 */
608sys_errno_t sys_task_exit(sysarg_t notify)
609{
610 task_kill_self(notify);
611 unreachable();
612}
613
614static void task_print(task_t *task, bool additional)
615{
616 irq_spinlock_lock(&task->lock, false);
617
618 uint64_t ucycles;
619 uint64_t kcycles;
620 char usuffix, ksuffix;
621 task_get_accounting(task, &ucycles, &kcycles);
622 order_suffix(ucycles, &ucycles, &usuffix);
623 order_suffix(kcycles, &kcycles, &ksuffix);
624
625#ifdef __32_BITS__
626 if (additional)
627 printf("%-8" PRIu64 " %9zu", task->taskid,
628 atomic_load(&task->refcount));
629 else
630 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
631 " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
632 task->name, task->container, task, task->as,
633 ucycles, usuffix, kcycles, ksuffix);
634#endif
635
636#ifdef __64_BITS__
637 if (additional)
638 printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
639 "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
640 ksuffix, atomic_load(&task->refcount));
641 else
642 printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
643 task->taskid, task->name, task->container, task, task->as);
644#endif
645
646 irq_spinlock_unlock(&task->lock, false);
647}
648
649/** Print task list
650 *
651 * @param additional Print additional information.
652 *
653 */
654void task_print_list(bool additional)
655{
656 /* Messing with task structures, avoid deadlock */
657 irq_spinlock_lock(&tasks_lock, true);
658
659#ifdef __32_BITS__
660 if (additional)
661 printf("[id ] [threads] [calls] [callee\n");
662 else
663 printf("[id ] [name ] [ctn] [address ] [as ]"
664 " [ucycles ] [kcycles ]\n");
665#endif
666
667#ifdef __64_BITS__
668 if (additional)
669 printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
670 " [callee\n");
671 else
672 printf("[id ] [name ] [ctn] [address ]"
673 " [as ]\n");
674#endif
675
676 task_t *task;
677
678 task = task_first();
679 while (task != NULL) {
680 task_print(task, additional);
681 task = task_next(task);
682 }
683
684 irq_spinlock_unlock(&tasks_lock, true);
685}
686
687/** Get key function for the @c tasks ordered dictionary.
688 *
689 * @param odlink Link
690 * @return Pointer to task ID cast as 'void *'
691 */
692static void *tasks_getkey(odlink_t *odlink)
693{
694 task_t *task = odict_get_instance(odlink, task_t, ltasks);
695 return (void *) &task->taskid;
696}
697
698/** Key comparison function for the @c tasks ordered dictionary.
699 *
700 * @param a Pointer to thread A ID
701 * @param b Pointer to thread B ID
702 * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
703 */
704static int tasks_cmp(void *a, void *b)
705{
706 task_id_t ida = *(task_id_t *)a;
707 task_id_t idb = *(task_id_t *)b;
708
709 if (ida < idb)
710 return -1;
711 else if (ida == idb)
712 return 0;
713 else
714 return +1;
715}
716
717/** @}
718 */
Note: See TracBrowser for help on using the repository browser.