Rev 3149 | Rev 3153 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2001-2004 Jakub Jermar |
1 | 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 | |||
1757 | jermar | 29 | /** @addtogroup genericproc |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | |||
1248 | jermar | 33 | /** |
1702 | cejka | 34 | * @file |
1248 | jermar | 35 | * @brief Task management. |
36 | */ |
||
37 | |||
973 | palkovsky | 38 | #include <main/uinit.h> |
1 | jermar | 39 | #include <proc/thread.h> |
40 | #include <proc/task.h> |
||
1078 | jermar | 41 | #include <proc/uarg.h> |
703 | jermar | 42 | #include <mm/as.h> |
814 | palkovsky | 43 | #include <mm/slab.h> |
2183 | jermar | 44 | #include <atomic.h> |
1 | jermar | 45 | #include <synch/spinlock.h> |
2109 | jermar | 46 | #include <synch/waitq.h> |
1 | jermar | 47 | #include <arch.h> |
48 | #include <panic.h> |
||
2504 | jermar | 49 | #include <adt/avl.h> |
1159 | jermar | 50 | #include <adt/btree.h> |
788 | jermar | 51 | #include <adt/list.h> |
955 | palkovsky | 52 | #include <ipc/ipc.h> |
3004 | svoboda | 53 | #include <ipc/ipcrsc.h> |
1174 | jermar | 54 | #include <security/cap.h> |
955 | palkovsky | 55 | #include <memstr.h> |
1060 | palkovsky | 56 | #include <print.h> |
2000 | decky | 57 | #include <lib/elf.h> |
1579 | jermar | 58 | #include <errno.h> |
2050 | decky | 59 | #include <func.h> |
1288 | jermar | 60 | #include <syscall/copy.h> |
973 | palkovsky | 61 | |
1170 | vana | 62 | #ifndef LOADED_PROG_STACK_PAGES_NO |
63 | #define LOADED_PROG_STACK_PAGES_NO 1 |
||
64 | #endif |
||
1168 | vana | 65 | |
2504 | jermar | 66 | /** Spinlock protecting the tasks_tree AVL tree. */ |
623 | jermar | 67 | SPINLOCK_INITIALIZE(tasks_lock); |
1636 | jermar | 68 | |
2504 | jermar | 69 | /** AVL tree of active tasks. |
1636 | jermar | 70 | * |
2504 | jermar | 71 | * The task is guaranteed to exist after it was found in the tasks_tree as |
2087 | jermar | 72 | * long as: |
1636 | jermar | 73 | * @li the tasks_lock is held, |
2087 | jermar | 74 | * @li the task's lock is held when task's lock is acquired before releasing |
75 | * tasks_lock or |
||
1880 | jermar | 76 | * @li the task's refcount is greater than 0 |
1636 | jermar | 77 | * |
78 | */ |
||
2504 | jermar | 79 | avltree_t tasks_tree; |
1636 | jermar | 80 | |
1005 | palkovsky | 81 | static task_id_t task_counter = 0; |
1 | jermar | 82 | |
3001 | svoboda | 83 | /** |
84 | * Points to the binary image used as the program loader. All non-initial |
||
85 | * tasks are created from this executable image. |
||
86 | */ |
||
87 | void *program_loader = NULL; |
||
88 | |||
89 | |||
107 | decky | 90 | /** Initialize tasks |
91 | * |
||
92 | * Initialize kernel tasks support. |
||
93 | * |
||
94 | */ |
||
1 | jermar | 95 | void task_init(void) |
96 | { |
||
15 | jermar | 97 | TASK = NULL; |
2504 | jermar | 98 | avltree_create(&tasks_tree); |
1 | jermar | 99 | } |
100 | |||
2504 | jermar | 101 | /* |
102 | * The idea behind this walker is to remember a single task different from TASK. |
||
103 | */ |
||
104 | static bool task_done_walker(avltree_node_t *node, void *arg) |
||
105 | { |
||
106 | task_t *t = avltree_get_instance(node, task_t, tasks_tree_node); |
||
107 | task_t **tp = (task_t **) arg; |
||
108 | |||
109 | if (t != TASK) { |
||
110 | *tp = t; |
||
111 | return false; /* stop walking */ |
||
112 | } |
||
113 | |||
114 | return true; /* continue the walk */ |
||
115 | } |
||
116 | |||
2227 | decky | 117 | /** Kill all tasks except the current task. |
118 | * |
||
119 | */ |
||
120 | void task_done(void) |
||
121 | { |
||
122 | task_t *t; |
||
123 | do { /* Repeat until there are any tasks except TASK */ |
||
124 | |||
125 | /* Messing with task structures, avoid deadlock */ |
||
126 | ipl_t ipl = interrupts_disable(); |
||
127 | spinlock_lock(&tasks_lock); |
||
128 | |||
129 | t = NULL; |
||
2504 | jermar | 130 | avltree_walk(&tasks_tree, task_done_walker, &t); |
2227 | decky | 131 | |
132 | if (t != NULL) { |
||
133 | task_id_t id = t->taskid; |
||
134 | |||
135 | spinlock_unlock(&tasks_lock); |
||
136 | interrupts_restore(ipl); |
||
137 | |||
138 | #ifdef CONFIG_DEBUG |
||
3149 | svoboda | 139 | printf("Killing task %" PRIu64 "\n", id); |
2227 | decky | 140 | #endif |
141 | task_kill(id); |
||
2632 | decky | 142 | thread_usleep(10000); |
2227 | decky | 143 | } else { |
144 | spinlock_unlock(&tasks_lock); |
||
145 | interrupts_restore(ipl); |
||
146 | } |
||
147 | |||
148 | } while (t != NULL); |
||
149 | } |
||
107 | decky | 150 | |
151 | /** Create new task |
||
152 | * |
||
153 | * Create new task with no threads. |
||
154 | * |
||
703 | jermar | 155 | * @param as Task's address space. |
1062 | jermar | 156 | * @param name Symbolic name. |
107 | decky | 157 | * |
973 | palkovsky | 158 | * @return New task's structure |
107 | decky | 159 | * |
160 | */ |
||
1062 | jermar | 161 | task_t *task_create(as_t *as, char *name) |
1 | jermar | 162 | { |
413 | jermar | 163 | ipl_t ipl; |
1 | jermar | 164 | task_t *ta; |
1040 | palkovsky | 165 | int i; |
1 | jermar | 166 | |
822 | palkovsky | 167 | ta = (task_t *) malloc(sizeof(task_t), 0); |
168 | |||
1185 | jermar | 169 | task_create_arch(ta); |
170 | |||
822 | palkovsky | 171 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
172 | list_initialize(&ta->th_head); |
||
173 | ta->as = as; |
||
1062 | jermar | 174 | ta->name = name; |
2446 | jermar | 175 | atomic_set(&ta->refcount, 0); |
176 | atomic_set(&ta->lifecount, 0); |
||
1839 | decky | 177 | ta->context = CONTEXT; |
1579 | jermar | 178 | |
1174 | jermar | 179 | ta->capabilities = 0; |
2039 | decky | 180 | ta->cycles = 0; |
1040 | palkovsky | 181 | |
2802 | jermar | 182 | ipc_answerbox_init(&ta->answerbox, ta); |
1839 | decky | 183 | for (i = 0; i < IPC_MAX_PHONES; i++) |
1040 | palkovsky | 184 | ipc_phone_init(&ta->phones[i]); |
2087 | jermar | 185 | if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, |
186 | ta->context))) |
||
1040 | palkovsky | 187 | ipc_phone_connect(&ta->phones[0], ipc_phone_0); |
998 | palkovsky | 188 | atomic_set(&ta->active_calls, 0); |
1460 | jermar | 189 | |
190 | mutex_initialize(&ta->futexes_lock); |
||
191 | btree_create(&ta->futexes); |
||
822 | palkovsky | 192 | |
193 | ipl = interrupts_disable(); |
||
1468 | jermar | 194 | |
195 | /* |
||
196 | * Increment address space reference count. |
||
197 | */ |
||
2183 | jermar | 198 | atomic_inc(&as->refcount); |
1468 | jermar | 199 | |
822 | palkovsky | 200 | spinlock_lock(&tasks_lock); |
1005 | palkovsky | 201 | ta->taskid = ++task_counter; |
2504 | jermar | 202 | avltree_node_initialize(&ta->tasks_tree_node); |
203 | ta->tasks_tree_node.key = ta->taskid; |
||
204 | avltree_insert(&tasks_tree, &ta->tasks_tree_node); |
||
822 | palkovsky | 205 | spinlock_unlock(&tasks_lock); |
206 | interrupts_restore(ipl); |
||
207 | |||
1 | jermar | 208 | return ta; |
209 | } |
||
210 | |||
1579 | jermar | 211 | /** Destroy task. |
212 | * |
||
213 | * @param t Task to be destroyed. |
||
214 | */ |
||
215 | void task_destroy(task_t *t) |
||
216 | { |
||
2446 | jermar | 217 | /* |
218 | * Remove the task from the task B+tree. |
||
219 | */ |
||
220 | spinlock_lock(&tasks_lock); |
||
2504 | jermar | 221 | avltree_delete(&tasks_tree, &t->tasks_tree_node); |
2446 | jermar | 222 | spinlock_unlock(&tasks_lock); |
223 | |||
224 | /* |
||
225 | * Perform architecture specific task destruction. |
||
226 | */ |
||
1587 | jermar | 227 | task_destroy_arch(t); |
2446 | jermar | 228 | |
229 | /* |
||
230 | * Free up dynamically allocated state. |
||
231 | */ |
||
1587 | jermar | 232 | btree_destroy(&t->futexes); |
233 | |||
2446 | jermar | 234 | /* |
235 | * Drop our reference to the address space. |
||
236 | */ |
||
2183 | jermar | 237 | if (atomic_predec(&t->as->refcount) == 0) |
1587 | jermar | 238 | as_destroy(t->as); |
239 | |||
240 | free(t); |
||
241 | TASK = NULL; |
||
1579 | jermar | 242 | } |
243 | |||
973 | palkovsky | 244 | /** Create new task with 1 thread and run it |
245 | * |
||
3001 | svoboda | 246 | * @param as Address space containing a binary program image. |
247 | * @param entry_addr Program entry-point address in program address space. |
||
248 | * @param name Program name. |
||
1062 | jermar | 249 | * |
1229 | jermar | 250 | * @return Task of the running program or NULL on error. |
973 | palkovsky | 251 | */ |
3150 | svoboda | 252 | task_t *task_create_from_as(as_t *as, uintptr_t entry_addr, char *name, |
3149 | svoboda | 253 | thread_t **thr) |
973 | palkovsky | 254 | { |
255 | as_area_t *a; |
||
2446 | jermar | 256 | thread_t *t; |
973 | palkovsky | 257 | task_t *task; |
1078 | jermar | 258 | uspace_arg_t *kernel_uarg; |
973 | palkovsky | 259 | |
1078 | jermar | 260 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
3001 | svoboda | 261 | kernel_uarg->uspace_entry = (void *) entry_addr; |
1078 | jermar | 262 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
263 | kernel_uarg->uspace_thread_function = NULL; |
||
264 | kernel_uarg->uspace_thread_arg = NULL; |
||
265 | kernel_uarg->uspace_uarg = NULL; |
||
1066 | jermar | 266 | |
1062 | jermar | 267 | task = task_create(as, name); |
1115 | jermar | 268 | ASSERT(task); |
269 | |||
973 | palkovsky | 270 | /* |
271 | * Create the data as_area. |
||
272 | */ |
||
2087 | jermar | 273 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
274 | LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, |
||
275 | AS_AREA_ATTR_NONE, &anon_backend, NULL); |
||
1115 | jermar | 276 | |
1585 | jermar | 277 | /* |
278 | * Create the main thread. |
||
279 | */ |
||
2446 | jermar | 280 | t = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, |
2087 | jermar | 281 | "uinit", false); |
2446 | jermar | 282 | ASSERT(t); |
3149 | svoboda | 283 | |
284 | *thr = t; |
||
973 | palkovsky | 285 | |
286 | return task; |
||
287 | } |
||
1060 | palkovsky | 288 | |
3001 | svoboda | 289 | /** Parse an executable image in the physical memory. |
290 | * |
||
291 | * If the image belongs to a program loader, it is registered as such, |
||
292 | * (and *task is set to NULL). Otherwise a task is created from the |
||
293 | * executable image. The task is returned in *task. |
||
294 | * |
||
295 | * @param program_addr Address of program executable image. |
||
296 | * @param name Program name. |
||
297 | * @param task Where to store the pointer to the newly created task. |
||
298 | * |
||
299 | * @return EOK on success or negative error code. |
||
300 | */ |
||
3149 | svoboda | 301 | int task_parse_initial(void *program_addr, char *name, thread_t **t) |
3001 | svoboda | 302 | { |
303 | as_t *as; |
||
304 | unsigned int rc; |
||
3149 | svoboda | 305 | task_t *task; |
3001 | svoboda | 306 | |
307 | as = as_create(0); |
||
308 | ASSERT(as); |
||
309 | |||
310 | rc = elf_load((elf_header_t *) program_addr, as, 0); |
||
311 | if (rc != EE_OK) { |
||
312 | as_destroy(as); |
||
3150 | svoboda | 313 | *t = NULL; |
3001 | svoboda | 314 | if (rc != EE_LOADER) |
315 | return ENOTSUP; |
||
316 | |||
317 | /* Register image as the program loader */ |
||
318 | ASSERT(program_loader == NULL); |
||
319 | program_loader = program_addr; |
||
320 | return EOK; |
||
321 | } |
||
322 | |||
3149 | svoboda | 323 | task = task_create_from_as(as, ((elf_header_t *) program_addr)->e_entry, |
324 | name, t); |
||
3001 | svoboda | 325 | |
326 | return EOK; |
||
327 | } |
||
328 | |||
329 | /** Create a task from the program loader image. |
||
330 | * |
||
331 | * @param name Program name. |
||
3004 | svoboda | 332 | * @param t Buffer for storing pointer to the newly created task. |
3001 | svoboda | 333 | * |
334 | * @return Task of the running program or NULL on error. |
||
335 | */ |
||
3004 | svoboda | 336 | int task_create_from_loader(char *name, task_t **t) |
3001 | svoboda | 337 | { |
338 | as_t *as; |
||
339 | unsigned int rc; |
||
3004 | svoboda | 340 | void *loader; |
3150 | svoboda | 341 | thread_t *thr; |
3001 | svoboda | 342 | |
343 | as = as_create(0); |
||
344 | ASSERT(as); |
||
345 | |||
3004 | svoboda | 346 | loader = program_loader; |
347 | if (!loader) return ENOENT; |
||
348 | |||
3001 | svoboda | 349 | rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER); |
350 | if (rc != EE_OK) { |
||
351 | as_destroy(as); |
||
3004 | svoboda | 352 | return ENOENT; |
3001 | svoboda | 353 | } |
354 | |||
3004 | svoboda | 355 | *t = task_create_from_as( |
3150 | svoboda | 356 | as, ((elf_header_t *) program_loader)->e_entry, name, &thr); |
3004 | svoboda | 357 | |
358 | return EOK; |
||
3001 | svoboda | 359 | } |
360 | |||
361 | /** Make task ready. |
||
362 | * |
||
363 | * Switch task's thread to the ready state. |
||
364 | * |
||
365 | * @param ta Task to make ready. |
||
366 | */ |
||
367 | void task_ready(task_t *t) |
||
368 | { |
||
369 | thread_t *th; |
||
370 | |||
371 | th = list_get_instance(t->th_head.next, thread_t, th_link); |
||
372 | thread_ready(th); |
||
373 | } |
||
374 | |||
1176 | jermar | 375 | /** Syscall for reading task ID from userspace. |
376 | * |
||
2087 | jermar | 377 | * @param uspace_task_id Userspace address of 8-byte buffer where to store |
378 | * current task ID. |
||
1176 | jermar | 379 | * |
1288 | jermar | 380 | * @return 0 on success or an error code from @ref errno.h. |
1176 | jermar | 381 | */ |
1780 | jermar | 382 | unative_t sys_task_get_id(task_id_t *uspace_task_id) |
1176 | jermar | 383 | { |
384 | /* |
||
385 | * No need to acquire lock on TASK because taskid |
||
386 | * remains constant for the lifespan of the task. |
||
387 | */ |
||
2087 | jermar | 388 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, |
389 | sizeof(TASK->taskid)); |
||
1176 | jermar | 390 | } |
391 | |||
3001 | svoboda | 392 | /** Syscall for creating a new task from userspace. |
393 | * |
||
3004 | svoboda | 394 | * Creates a new task from the program loader image, connects a phone |
395 | * to it and stores the phone id into the provided buffer. |
||
3001 | svoboda | 396 | * |
3004 | svoboda | 397 | * @param uspace_phone_id Userspace address where to store the phone id. |
3001 | svoboda | 398 | * |
399 | * @return 0 on success or an error code from @ref errno.h. |
||
400 | */ |
||
3149 | svoboda | 401 | unative_t sys_task_spawn_loader(int *uspace_phone_id) |
3001 | svoboda | 402 | { |
403 | task_t *t; |
||
3004 | svoboda | 404 | int fake_id; |
3001 | svoboda | 405 | int rc; |
3004 | svoboda | 406 | int phone_id; |
3001 | svoboda | 407 | |
3004 | svoboda | 408 | fake_id = 0; |
409 | |||
3001 | svoboda | 410 | /* Before we even try creating the task, see if we can write the id */ |
3004 | svoboda | 411 | rc = (unative_t) copy_to_uspace(uspace_phone_id, &fake_id, |
3001 | svoboda | 412 | sizeof(fake_id)); |
413 | if (rc != 0) |
||
414 | return rc; |
||
415 | |||
3004 | svoboda | 416 | phone_id = phone_alloc(); |
417 | if (phone_id < 0) |
||
418 | return ELIMIT; |
||
3001 | svoboda | 419 | |
3004 | svoboda | 420 | rc = task_create_from_loader("loader", &t); |
421 | if (rc != 0) |
||
422 | return rc; |
||
423 | |||
424 | phone_connect(phone_id, &t->answerbox); |
||
425 | |||
3001 | svoboda | 426 | /* No need to aquire lock before task_ready() */ |
3004 | svoboda | 427 | rc = (unative_t) copy_to_uspace(uspace_phone_id, &phone_id, |
428 | sizeof(phone_id)); |
||
3001 | svoboda | 429 | if (rc != 0) { |
430 | /* Ooops */ |
||
3004 | svoboda | 431 | ipc_phone_hangup(&TASK->phones[phone_id]); |
3001 | svoboda | 432 | task_kill(t->taskid); |
433 | return rc; |
||
434 | } |
||
435 | |||
436 | task_ready(t); |
||
437 | |||
438 | return EOK; |
||
439 | } |
||
440 | |||
3149 | svoboda | 441 | unative_t sys_task_spawn(void *image, size_t size) |
442 | { |
||
443 | void *kimage = malloc(size, 0); |
||
444 | if (kimage == NULL) |
||
445 | return ENOMEM; |
||
446 | |||
447 | int rc = copy_from_uspace(kimage, image, size); |
||
448 | if (rc != EOK) |
||
449 | return rc; |
||
450 | |||
451 | uspace_arg_t *kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
||
452 | if (kernel_uarg == NULL) { |
||
453 | free(kimage); |
||
454 | return ENOMEM; |
||
455 | } |
||
456 | |||
457 | kernel_uarg->uspace_entry = |
||
458 | (void *) ((elf_header_t *) kimage)->e_entry; |
||
459 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
||
460 | kernel_uarg->uspace_thread_function = NULL; |
||
461 | kernel_uarg->uspace_thread_arg = NULL; |
||
462 | kernel_uarg->uspace_uarg = NULL; |
||
463 | |||
464 | as_t *as = as_create(0); |
||
465 | if (as == NULL) { |
||
466 | free(kernel_uarg); |
||
467 | free(kimage); |
||
468 | return ENOMEM; |
||
469 | } |
||
470 | |||
3150 | svoboda | 471 | unsigned int erc = elf_load((elf_header_t *) kimage, as, ELD_F_NONE); |
3149 | svoboda | 472 | if (erc != EE_OK) { |
473 | as_destroy(as); |
||
474 | free(kernel_uarg); |
||
475 | free(kimage); |
||
476 | return ENOENT; |
||
477 | } |
||
478 | |||
479 | as_area_t *area = as_area_create(as, |
||
480 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
||
481 | LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, |
||
482 | AS_AREA_ATTR_NONE, &anon_backend, NULL); |
||
483 | if (area == NULL) { |
||
484 | as_destroy(as); |
||
485 | free(kernel_uarg); |
||
486 | free(kimage); |
||
487 | return ENOMEM; |
||
488 | } |
||
489 | |||
490 | task_t *task = task_create(as, "app"); |
||
491 | if (task == NULL) { |
||
492 | as_destroy(as); |
||
493 | free(kernel_uarg); |
||
494 | free(kimage); |
||
495 | return ENOENT; |
||
496 | } |
||
497 | |||
498 | // FIXME: control the capabilities |
||
499 | cap_set(task, cap_get(TASK)); |
||
500 | |||
501 | thread_t *thread = thread_create(uinit, kernel_uarg, task, |
||
502 | THREAD_FLAG_USPACE, "user", false); |
||
503 | if (thread == NULL) { |
||
504 | task_destroy(task); |
||
505 | as_destroy(as); |
||
506 | free(kernel_uarg); |
||
507 | free(kimage); |
||
508 | return ENOENT; |
||
509 | } |
||
510 | |||
511 | thread_ready(thread); |
||
512 | |||
513 | return EOK; |
||
514 | } |
||
515 | |||
1178 | jermar | 516 | /** Find task structure corresponding to task ID. |
517 | * |
||
518 | * The tasks_lock must be already held by the caller of this function |
||
519 | * and interrupts must be disabled. |
||
520 | * |
||
521 | * @param id Task ID. |
||
522 | * |
||
523 | * @return Task structure address or NULL if there is no such task ID. |
||
524 | */ |
||
525 | task_t *task_find_by_id(task_id_t id) |
||
526 | { |
||
2504 | jermar | 527 | avltree_node_t *node; |
1178 | jermar | 528 | |
2504 | jermar | 529 | node = avltree_search(&tasks_tree, (avltree_key_t) id); |
530 | |||
531 | if (node) |
||
532 | return avltree_get_instance(node, task_t, tasks_tree_node); |
||
533 | return NULL; |
||
1178 | jermar | 534 | } |
535 | |||
2039 | decky | 536 | /** Get accounting data of given task. |
537 | * |
||
2048 | jermar | 538 | * Note that task lock of 't' must be already held and |
2039 | decky | 539 | * interrupts must be already disabled. |
540 | * |
||
541 | * @param t Pointer to thread. |
||
542 | * |
||
543 | */ |
||
544 | uint64_t task_get_accounting(task_t *t) |
||
545 | { |
||
546 | /* Accumulated value of task */ |
||
547 | uint64_t ret = t->cycles; |
||
548 | |||
549 | /* Current values of threads */ |
||
550 | link_t *cur; |
||
551 | for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) { |
||
552 | thread_t *thr = list_get_instance(cur, thread_t, th_link); |
||
553 | |||
554 | spinlock_lock(&thr->lock); |
||
2042 | decky | 555 | /* Process only counted threads */ |
556 | if (!thr->uncounted) { |
||
2087 | jermar | 557 | if (thr == THREAD) { |
558 | /* Update accounting of current thread */ |
||
559 | thread_update_accounting(); |
||
560 | } |
||
2042 | decky | 561 | ret += thr->cycles; |
562 | } |
||
2039 | decky | 563 | spinlock_unlock(&thr->lock); |
564 | } |
||
565 | |||
566 | return ret; |
||
567 | } |
||
568 | |||
1579 | jermar | 569 | /** Kill task. |
570 | * |
||
2446 | jermar | 571 | * This function is idempotent. |
572 | * It signals all the task's threads to bail it out. |
||
573 | * |
||
1579 | jermar | 574 | * @param id ID of the task to be killed. |
575 | * |
||
576 | * @return 0 on success or an error code from errno.h |
||
577 | */ |
||
578 | int task_kill(task_id_t id) |
||
579 | { |
||
580 | ipl_t ipl; |
||
581 | task_t *ta; |
||
582 | link_t *cur; |
||
1600 | jermar | 583 | |
584 | if (id == 1) |
||
585 | return EPERM; |
||
1579 | jermar | 586 | |
587 | ipl = interrupts_disable(); |
||
588 | spinlock_lock(&tasks_lock); |
||
589 | if (!(ta = task_find_by_id(id))) { |
||
590 | spinlock_unlock(&tasks_lock); |
||
591 | interrupts_restore(ipl); |
||
592 | return ENOENT; |
||
593 | } |
||
1587 | jermar | 594 | spinlock_unlock(&tasks_lock); |
1579 | jermar | 595 | |
1585 | jermar | 596 | /* |
1687 | jermar | 597 | * Interrupt all threads except ktaskclnp. |
2446 | jermar | 598 | */ |
599 | spinlock_lock(&ta->lock); |
||
1579 | jermar | 600 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
601 | thread_t *thr; |
||
2446 | jermar | 602 | bool sleeping = false; |
1579 | jermar | 603 | |
604 | thr = list_get_instance(cur, thread_t, th_link); |
||
605 | |||
606 | spinlock_lock(&thr->lock); |
||
607 | thr->interrupted = true; |
||
608 | if (thr->state == Sleeping) |
||
609 | sleeping = true; |
||
610 | spinlock_unlock(&thr->lock); |
||
611 | |||
612 | if (sleeping) |
||
2109 | jermar | 613 | waitq_interrupt_sleep(thr); |
1579 | jermar | 614 | } |
1580 | jermar | 615 | spinlock_unlock(&ta->lock); |
616 | interrupts_restore(ipl); |
||
1579 | jermar | 617 | |
618 | return 0; |
||
619 | } |
||
620 | |||
2504 | jermar | 621 | static bool task_print_walker(avltree_node_t *node, void *arg) |
622 | { |
||
623 | task_t *t = avltree_get_instance(node, task_t, tasks_tree_node); |
||
624 | int j; |
||
625 | |||
626 | spinlock_lock(&t->lock); |
||
627 | |||
628 | uint64_t cycles; |
||
629 | char suffix; |
||
630 | order(task_get_accounting(t), &cycles, &suffix); |
||
3149 | svoboda | 631 | |
632 | #ifdef __32_BITS__ |
||
633 | printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %10p %10p %9" PRIu64 "%c %7ld %6ld", |
||
634 | t->taskid, t->name, t->context, t, t->as, cycles, suffix, |
||
635 | atomic_get(&t->refcount), atomic_get(&t->active_calls)); |
||
636 | #endif |
||
637 | |||
638 | #ifdef __64_BITS__ |
||
639 | printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %18p %18p %9" PRIu64 "%c %7ld %6ld", |
||
640 | t->taskid, t->name, t->context, t, t->as, cycles, suffix, |
||
641 | atomic_get(&t->refcount), atomic_get(&t->active_calls)); |
||
642 | #endif |
||
643 | |||
2504 | jermar | 644 | for (j = 0; j < IPC_MAX_PHONES; j++) { |
645 | if (t->phones[j].callee) |
||
3149 | svoboda | 646 | printf(" %d:%p", j, t->phones[j].callee); |
2504 | jermar | 647 | } |
648 | printf("\n"); |
||
649 | |||
650 | spinlock_unlock(&t->lock); |
||
651 | return true; |
||
652 | } |
||
653 | |||
1060 | palkovsky | 654 | /** Print task list */ |
655 | void task_print_list(void) |
||
656 | { |
||
657 | ipl_t ipl; |
||
658 | |||
2227 | decky | 659 | /* Messing with task structures, avoid deadlock */ |
1060 | palkovsky | 660 | ipl = interrupts_disable(); |
661 | spinlock_lock(&tasks_lock); |
||
662 | |||
3149 | svoboda | 663 | #ifdef __32_BITS__ |
664 | printf("taskid name ctx address as " |
||
665 | "cycles threads calls callee\n"); |
||
666 | printf("------ ---------- --- ---------- ---------- " |
||
667 | "---------- ------- ------ ------>\n"); |
||
668 | #endif |
||
669 | |||
670 | #ifdef __64_BITS__ |
||
671 | printf("taskid name ctx address as " |
||
672 | "cycles threads calls callee\n"); |
||
673 | printf("------ ---------- --- ------------------ ------------------ " |
||
674 | "---------- ------- ------ ------>\n"); |
||
675 | #endif |
||
676 | |||
2504 | jermar | 677 | avltree_walk(&tasks_tree, task_print_walker, NULL); |
1159 | jermar | 678 | |
1060 | palkovsky | 679 | spinlock_unlock(&tasks_lock); |
680 | interrupts_restore(ipl); |
||
681 | } |
||
1579 | jermar | 682 | |
1757 | jermar | 683 | /** @} |
1702 | cejka | 684 | */ |