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. */
|
---|
63 | IRQ_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 | */
|
---|
78 | odict_t tasks;
|
---|
79 |
|
---|
80 | static task_id_t task_counter = 0;
|
---|
81 |
|
---|
82 | static slab_cache_t *task_cache;
|
---|
83 |
|
---|
84 | /* Forward declarations. */
|
---|
85 | static void task_kill_internal(task_t *);
|
---|
86 | static errno_t tsk_constructor(void *, unsigned int);
|
---|
87 | static size_t tsk_destructor(void *);
|
---|
88 |
|
---|
89 | static void *tasks_getkey(odlink_t *);
|
---|
90 | static int tasks_cmp(void *, void *);
|
---|
91 |
|
---|
92 | /** Initialize kernel tasks support.
|
---|
93 | *
|
---|
94 | */
|
---|
95 | void 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 | */
|
---|
106 | void 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 |
|
---|
152 | errno_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 |
|
---|
182 | size_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 | */
|
---|
198 | task_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 | task->debug_sections = NULL;
|
---|
228 |
|
---|
229 | #ifdef CONFIG_UDEBUG
|
---|
230 | /* Init debugging stuff */
|
---|
231 | udebug_task_init(&task->udebug);
|
---|
232 |
|
---|
233 | /* Init kbox stuff */
|
---|
234 | task->kb.box.active = true;
|
---|
235 | task->kb.finished = false;
|
---|
236 | #endif
|
---|
237 |
|
---|
238 | if ((ipc_box_0) &&
|
---|
239 | (container_check(ipc_box_0->task->container, task->container))) {
|
---|
240 | cap_phone_handle_t phone_handle;
|
---|
241 | errno_t rc = phone_alloc(task, true, &phone_handle, NULL);
|
---|
242 | if (rc != EOK) {
|
---|
243 | task->as = NULL;
|
---|
244 | task_destroy_arch(task);
|
---|
245 | slab_free(task_cache, task);
|
---|
246 | return NULL;
|
---|
247 | }
|
---|
248 |
|
---|
249 | kobject_t *phone_obj = kobject_get(task, phone_handle,
|
---|
250 | KOBJECT_TYPE_PHONE);
|
---|
251 | (void) ipc_phone_connect(phone_obj->phone, ipc_box_0);
|
---|
252 | }
|
---|
253 |
|
---|
254 | irq_spinlock_lock(&tasks_lock, true);
|
---|
255 |
|
---|
256 | task->taskid = ++task_counter;
|
---|
257 | odlink_initialize(&task->ltasks);
|
---|
258 | odict_insert(&task->ltasks, &tasks, NULL);
|
---|
259 |
|
---|
260 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
261 |
|
---|
262 | return task;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /** Destroy task.
|
---|
266 | *
|
---|
267 | * @param task Task to be destroyed.
|
---|
268 | *
|
---|
269 | */
|
---|
270 | void task_destroy(task_t *task)
|
---|
271 | {
|
---|
272 | /*
|
---|
273 | * Remove the task from the task odict.
|
---|
274 | */
|
---|
275 | irq_spinlock_lock(&tasks_lock, true);
|
---|
276 | odict_remove(&task->ltasks);
|
---|
277 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
278 |
|
---|
279 | /*
|
---|
280 | * Perform architecture specific task destruction.
|
---|
281 | */
|
---|
282 | task_destroy_arch(task);
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Drop our reference to the address space.
|
---|
286 | */
|
---|
287 | as_release(task->as);
|
---|
288 |
|
---|
289 | slab_free(task_cache, task);
|
---|
290 | }
|
---|
291 |
|
---|
292 | /** Hold a reference to a task.
|
---|
293 | *
|
---|
294 | * Holding a reference to a task prevents destruction of that task.
|
---|
295 | *
|
---|
296 | * @param task Task to be held.
|
---|
297 | *
|
---|
298 | */
|
---|
299 | void task_hold(task_t *task)
|
---|
300 | {
|
---|
301 | atomic_inc(&task->refcount);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /** Release a reference to a task.
|
---|
305 | *
|
---|
306 | * The last one to release a reference to a task destroys the task.
|
---|
307 | *
|
---|
308 | * @param task Task to be released.
|
---|
309 | *
|
---|
310 | */
|
---|
311 | void task_release(task_t *task)
|
---|
312 | {
|
---|
313 | if ((atomic_predec(&task->refcount)) == 0)
|
---|
314 | task_destroy(task);
|
---|
315 | }
|
---|
316 |
|
---|
317 | #ifdef __32_BITS__
|
---|
318 |
|
---|
319 | /** Syscall for reading task ID from userspace (32 bits)
|
---|
320 | *
|
---|
321 | * @param uspace_taskid Pointer to user-space buffer
|
---|
322 | * where to store current task ID.
|
---|
323 | *
|
---|
324 | * @return Zero on success or an error code from @ref errno.h.
|
---|
325 | *
|
---|
326 | */
|
---|
327 | sys_errno_t sys_task_get_id(uspace_ptr_sysarg64_t uspace_taskid)
|
---|
328 | {
|
---|
329 | /*
|
---|
330 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
331 | * the lifespan of the task.
|
---|
332 | */
|
---|
333 | return (sys_errno_t) copy_to_uspace(uspace_taskid, &TASK->taskid,
|
---|
334 | sizeof(TASK->taskid));
|
---|
335 | }
|
---|
336 |
|
---|
337 | #endif /* __32_BITS__ */
|
---|
338 |
|
---|
339 | #ifdef __64_BITS__
|
---|
340 |
|
---|
341 | /** Syscall for reading task ID from userspace (64 bits)
|
---|
342 | *
|
---|
343 | * @return Current task ID.
|
---|
344 | *
|
---|
345 | */
|
---|
346 | sysarg_t sys_task_get_id(void)
|
---|
347 | {
|
---|
348 | /*
|
---|
349 | * No need to acquire lock on TASK because taskid remains constant for
|
---|
350 | * the lifespan of the task.
|
---|
351 | */
|
---|
352 | return TASK->taskid;
|
---|
353 | }
|
---|
354 |
|
---|
355 | #endif /* __64_BITS__ */
|
---|
356 |
|
---|
357 | /** Syscall for setting the task name.
|
---|
358 | *
|
---|
359 | * The name simplifies identifying the task in the task list.
|
---|
360 | *
|
---|
361 | * @param name The new name for the task. (typically the same
|
---|
362 | * as the command used to execute it).
|
---|
363 | *
|
---|
364 | * @return 0 on success or an error code from @ref errno.h.
|
---|
365 | *
|
---|
366 | */
|
---|
367 | sys_errno_t sys_task_set_name(const uspace_ptr_char uspace_name, size_t name_len)
|
---|
368 | {
|
---|
369 | char namebuf[TASK_NAME_BUFLEN];
|
---|
370 |
|
---|
371 | /* Cap length of name and copy it from userspace. */
|
---|
372 | if (name_len > TASK_NAME_BUFLEN - 1)
|
---|
373 | name_len = TASK_NAME_BUFLEN - 1;
|
---|
374 |
|
---|
375 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
376 | if (rc != EOK)
|
---|
377 | return (sys_errno_t) rc;
|
---|
378 |
|
---|
379 | namebuf[name_len] = '\0';
|
---|
380 |
|
---|
381 | /*
|
---|
382 | * As the task name is referenced also from the
|
---|
383 | * threads, lock the threads' lock for the course
|
---|
384 | * of the update.
|
---|
385 | */
|
---|
386 |
|
---|
387 | irq_spinlock_lock(&tasks_lock, true);
|
---|
388 | irq_spinlock_lock(&TASK->lock, false);
|
---|
389 |
|
---|
390 | /* Set task name */
|
---|
391 | str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
|
---|
392 |
|
---|
393 | irq_spinlock_unlock(&TASK->lock, false);
|
---|
394 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
395 |
|
---|
396 | return EOK;
|
---|
397 | }
|
---|
398 |
|
---|
399 | /** Syscall to forcefully terminate a task
|
---|
400 | *
|
---|
401 | * @param uspace_taskid Pointer to task ID in user space.
|
---|
402 | *
|
---|
403 | * @return 0 on success or an error code from @ref errno.h.
|
---|
404 | *
|
---|
405 | */
|
---|
406 | sys_errno_t sys_task_kill(uspace_ptr_task_id_t uspace_taskid)
|
---|
407 | {
|
---|
408 | task_id_t taskid;
|
---|
409 | errno_t rc = copy_from_uspace(&taskid, uspace_taskid, sizeof(taskid));
|
---|
410 | if (rc != EOK)
|
---|
411 | return (sys_errno_t) rc;
|
---|
412 |
|
---|
413 | return (sys_errno_t) task_kill(taskid);
|
---|
414 | }
|
---|
415 |
|
---|
416 | /** Find task structure corresponding to task ID.
|
---|
417 | *
|
---|
418 | * The tasks_lock must be already held by the caller of this function and
|
---|
419 | * interrupts must be disabled.
|
---|
420 | *
|
---|
421 | * @param id Task ID.
|
---|
422 | *
|
---|
423 | * @return Task structure address or NULL if there is no such task ID.
|
---|
424 | *
|
---|
425 | */
|
---|
426 | task_t *task_find_by_id(task_id_t id)
|
---|
427 | {
|
---|
428 | assert(interrupts_disabled());
|
---|
429 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
430 |
|
---|
431 | odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
|
---|
432 | if (odlink != NULL)
|
---|
433 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
434 |
|
---|
435 | return NULL;
|
---|
436 | }
|
---|
437 |
|
---|
438 | /** Get count of tasks.
|
---|
439 | *
|
---|
440 | * @return Number of tasks in the system
|
---|
441 | */
|
---|
442 | size_t task_count(void)
|
---|
443 | {
|
---|
444 | assert(interrupts_disabled());
|
---|
445 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
446 |
|
---|
447 | return odict_count(&tasks);
|
---|
448 | }
|
---|
449 |
|
---|
450 | /** Get first task (task with lowest ID).
|
---|
451 | *
|
---|
452 | * @return Pointer to first task or @c NULL if there are none.
|
---|
453 | */
|
---|
454 | task_t *task_first(void)
|
---|
455 | {
|
---|
456 | odlink_t *odlink;
|
---|
457 |
|
---|
458 | assert(interrupts_disabled());
|
---|
459 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
460 |
|
---|
461 | odlink = odict_first(&tasks);
|
---|
462 | if (odlink == NULL)
|
---|
463 | return NULL;
|
---|
464 |
|
---|
465 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
466 | }
|
---|
467 |
|
---|
468 | /** Get next task (with higher task ID).
|
---|
469 | *
|
---|
470 | * @param cur Current task
|
---|
471 | * @return Pointer to next task or @c NULL if there are no more tasks.
|
---|
472 | */
|
---|
473 | task_t *task_next(task_t *cur)
|
---|
474 | {
|
---|
475 | odlink_t *odlink;
|
---|
476 |
|
---|
477 | assert(interrupts_disabled());
|
---|
478 | assert(irq_spinlock_locked(&tasks_lock));
|
---|
479 |
|
---|
480 | odlink = odict_next(&cur->ltasks, &tasks);
|
---|
481 | if (odlink == NULL)
|
---|
482 | return NULL;
|
---|
483 |
|
---|
484 | return odict_get_instance(odlink, task_t, ltasks);
|
---|
485 | }
|
---|
486 |
|
---|
487 | /** Get accounting data of given task.
|
---|
488 | *
|
---|
489 | * Note that task lock of 'task' must be already held and interrupts must be
|
---|
490 | * already disabled.
|
---|
491 | *
|
---|
492 | * @param task Pointer to the task.
|
---|
493 | * @param ucycles Out pointer to sum of all user cycles.
|
---|
494 | * @param kcycles Out pointer to sum of all kernel cycles.
|
---|
495 | *
|
---|
496 | */
|
---|
497 | void task_get_accounting(task_t *task, uint64_t *ucycles, uint64_t *kcycles)
|
---|
498 | {
|
---|
499 | assert(interrupts_disabled());
|
---|
500 | assert(irq_spinlock_locked(&task->lock));
|
---|
501 |
|
---|
502 | /* Accumulated values of task */
|
---|
503 | uint64_t uret = task->ucycles;
|
---|
504 | uint64_t kret = task->kcycles;
|
---|
505 |
|
---|
506 | /* Current values of threads */
|
---|
507 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
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 += atomic_time_read(&thread->ucycles);
|
---|
516 | kret += atomic_time_read(&thread->kcycles);
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | *ucycles = uret;
|
---|
521 | *kcycles = kret;
|
---|
522 | }
|
---|
523 |
|
---|
524 | static void task_kill_internal(task_t *task)
|
---|
525 | {
|
---|
526 | irq_spinlock_lock(&task->lock, false);
|
---|
527 |
|
---|
528 | /*
|
---|
529 | * Interrupt all threads.
|
---|
530 | */
|
---|
531 |
|
---|
532 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
533 | thread_interrupt(thread);
|
---|
534 | }
|
---|
535 |
|
---|
536 | irq_spinlock_unlock(&task->lock, false);
|
---|
537 | }
|
---|
538 |
|
---|
539 | /** Kill task.
|
---|
540 | *
|
---|
541 | * This function is idempotent.
|
---|
542 | * It signals all the task's threads to bail it out.
|
---|
543 | *
|
---|
544 | * @param id ID of the task to be killed.
|
---|
545 | *
|
---|
546 | * @return Zero on success or an error code from errno.h.
|
---|
547 | *
|
---|
548 | */
|
---|
549 | errno_t task_kill(task_id_t id)
|
---|
550 | {
|
---|
551 | if (id == 1)
|
---|
552 | return EPERM;
|
---|
553 |
|
---|
554 | irq_spinlock_lock(&tasks_lock, true);
|
---|
555 |
|
---|
556 | task_t *task = task_find_by_id(id);
|
---|
557 | if (!task) {
|
---|
558 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
559 | return ENOENT;
|
---|
560 | }
|
---|
561 |
|
---|
562 | task_kill_internal(task);
|
---|
563 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
564 |
|
---|
565 | return EOK;
|
---|
566 | }
|
---|
567 |
|
---|
568 | /** Kill the currently running task.
|
---|
569 | *
|
---|
570 | * @param notify Send out fault notifications.
|
---|
571 | *
|
---|
572 | * @return Zero on success or an error code from errno.h.
|
---|
573 | *
|
---|
574 | */
|
---|
575 | void task_kill_self(bool notify)
|
---|
576 | {
|
---|
577 | /*
|
---|
578 | * User space can subscribe for FAULT events to take action
|
---|
579 | * whenever a task faults (to take a dump, run a debugger, etc.).
|
---|
580 | * The notification is always available, but unless udebug is enabled,
|
---|
581 | * that's all you get.
|
---|
582 | */
|
---|
583 | if (notify) {
|
---|
584 | /* Notify the subscriber that a fault occurred. */
|
---|
585 | if (event_notify_3(EVENT_FAULT, false, LOWER32(TASK->taskid),
|
---|
586 | UPPER32(TASK->taskid), (sysarg_t) THREAD) == EOK) {
|
---|
587 | #ifdef CONFIG_UDEBUG
|
---|
588 | /* Wait for a debugging session. */
|
---|
589 | udebug_thread_fault();
|
---|
590 | #endif
|
---|
591 | }
|
---|
592 | }
|
---|
593 |
|
---|
594 | irq_spinlock_lock(&tasks_lock, true);
|
---|
595 | task_kill_internal(TASK);
|
---|
596 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
597 |
|
---|
598 | thread_exit();
|
---|
599 | }
|
---|
600 |
|
---|
601 | /** Process syscall to terminate the current task.
|
---|
602 | *
|
---|
603 | * @param notify Send out fault notifications.
|
---|
604 | *
|
---|
605 | */
|
---|
606 | sys_errno_t sys_task_exit(sysarg_t notify)
|
---|
607 | {
|
---|
608 | task_kill_self(notify);
|
---|
609 | unreachable();
|
---|
610 | }
|
---|
611 |
|
---|
612 | static void task_print(task_t *task, bool additional)
|
---|
613 | {
|
---|
614 | irq_spinlock_lock(&task->lock, false);
|
---|
615 |
|
---|
616 | uint64_t ucycles;
|
---|
617 | uint64_t kcycles;
|
---|
618 | char usuffix, ksuffix;
|
---|
619 | task_get_accounting(task, &ucycles, &kcycles);
|
---|
620 | order_suffix(ucycles, &ucycles, &usuffix);
|
---|
621 | order_suffix(kcycles, &kcycles, &ksuffix);
|
---|
622 |
|
---|
623 | #ifdef __32_BITS__
|
---|
624 | if (additional)
|
---|
625 | printf("%-8" PRIu64 " %9zu", task->taskid,
|
---|
626 | atomic_load(&task->refcount));
|
---|
627 | else
|
---|
628 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
|
---|
629 | " %9" PRIu64 "%c %9" PRIu64 "%c\n", task->taskid,
|
---|
630 | task->name, task->container, task, task->as,
|
---|
631 | ucycles, usuffix, kcycles, ksuffix);
|
---|
632 | #endif
|
---|
633 |
|
---|
634 | #ifdef __64_BITS__
|
---|
635 | if (additional)
|
---|
636 | printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
|
---|
637 | "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
|
---|
638 | ksuffix, atomic_load(&task->refcount));
|
---|
639 | else
|
---|
640 | printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
|
---|
641 | task->taskid, task->name, task->container, task, task->as);
|
---|
642 | #endif
|
---|
643 |
|
---|
644 | irq_spinlock_unlock(&task->lock, false);
|
---|
645 | }
|
---|
646 |
|
---|
647 | /** Print task list
|
---|
648 | *
|
---|
649 | * @param additional Print additional information.
|
---|
650 | *
|
---|
651 | */
|
---|
652 | void task_print_list(bool additional)
|
---|
653 | {
|
---|
654 | /* Messing with task structures, avoid deadlock */
|
---|
655 | irq_spinlock_lock(&tasks_lock, true);
|
---|
656 |
|
---|
657 | #ifdef __32_BITS__
|
---|
658 | if (additional)
|
---|
659 | printf("[id ] [threads] [calls] [callee\n");
|
---|
660 | else
|
---|
661 | printf("[id ] [name ] [ctn] [address ] [as ]"
|
---|
662 | " [ucycles ] [kcycles ]\n");
|
---|
663 | #endif
|
---|
664 |
|
---|
665 | #ifdef __64_BITS__
|
---|
666 | if (additional)
|
---|
667 | printf("[id ] [ucycles ] [kcycles ] [threads] [calls]"
|
---|
668 | " [callee\n");
|
---|
669 | else
|
---|
670 | printf("[id ] [name ] [ctn] [address ]"
|
---|
671 | " [as ]\n");
|
---|
672 | #endif
|
---|
673 |
|
---|
674 | task_t *task;
|
---|
675 |
|
---|
676 | task = task_first();
|
---|
677 | while (task != NULL) {
|
---|
678 | task_print(task, additional);
|
---|
679 | task = task_next(task);
|
---|
680 | }
|
---|
681 |
|
---|
682 | irq_spinlock_unlock(&tasks_lock, true);
|
---|
683 | }
|
---|
684 |
|
---|
685 | /** Get key function for the @c tasks ordered dictionary.
|
---|
686 | *
|
---|
687 | * @param odlink Link
|
---|
688 | * @return Pointer to task ID cast as 'void *'
|
---|
689 | */
|
---|
690 | static void *tasks_getkey(odlink_t *odlink)
|
---|
691 | {
|
---|
692 | task_t *task = odict_get_instance(odlink, task_t, ltasks);
|
---|
693 | return (void *) &task->taskid;
|
---|
694 | }
|
---|
695 |
|
---|
696 | /** Key comparison function for the @c tasks ordered dictionary.
|
---|
697 | *
|
---|
698 | * @param a Pointer to thread A ID
|
---|
699 | * @param b Pointer to thread B ID
|
---|
700 | * @return -1, 0, 1 iff ID A is less than, equal to, greater than B
|
---|
701 | */
|
---|
702 | static int tasks_cmp(void *a, void *b)
|
---|
703 | {
|
---|
704 | task_id_t ida = *(task_id_t *)a;
|
---|
705 | task_id_t idb = *(task_id_t *)b;
|
---|
706 |
|
---|
707 | if (ida < idb)
|
---|
708 | return -1;
|
---|
709 | else if (ida == idb)
|
---|
710 | return 0;
|
---|
711 | else
|
---|
712 | return +1;
|
---|
713 | }
|
---|
714 |
|
---|
715 | /** @}
|
---|
716 | */
|
---|