Rev 2436 | Rev 2504 | 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> |
||
1159 | jermar | 49 | #include <adt/btree.h> |
788 | jermar | 50 | #include <adt/list.h> |
955 | palkovsky | 51 | #include <ipc/ipc.h> |
1174 | jermar | 52 | #include <security/cap.h> |
955 | palkovsky | 53 | #include <memstr.h> |
1060 | palkovsky | 54 | #include <print.h> |
2000 | decky | 55 | #include <lib/elf.h> |
1579 | jermar | 56 | #include <errno.h> |
2050 | decky | 57 | #include <func.h> |
1288 | jermar | 58 | #include <syscall/copy.h> |
973 | palkovsky | 59 | |
1170 | vana | 60 | #ifndef LOADED_PROG_STACK_PAGES_NO |
61 | #define LOADED_PROG_STACK_PAGES_NO 1 |
||
62 | #endif |
||
1168 | vana | 63 | |
1636 | jermar | 64 | /** Spinlock protecting the tasks_btree B+tree. */ |
623 | jermar | 65 | SPINLOCK_INITIALIZE(tasks_lock); |
1636 | jermar | 66 | |
67 | /** B+tree of active tasks. |
||
68 | * |
||
2087 | jermar | 69 | * The task is guaranteed to exist after it was found in the tasks_btree as |
70 | * long as: |
||
1636 | jermar | 71 | * @li the tasks_lock is held, |
2087 | jermar | 72 | * @li the task's lock is held when task's lock is acquired before releasing |
73 | * tasks_lock or |
||
1880 | jermar | 74 | * @li the task's refcount is greater than 0 |
1636 | jermar | 75 | * |
76 | */ |
||
1159 | jermar | 77 | btree_t tasks_btree; |
1636 | jermar | 78 | |
1005 | palkovsky | 79 | static task_id_t task_counter = 0; |
1 | jermar | 80 | |
107 | decky | 81 | /** Initialize tasks |
82 | * |
||
83 | * Initialize kernel tasks support. |
||
84 | * |
||
85 | */ |
||
1 | jermar | 86 | void task_init(void) |
87 | { |
||
15 | jermar | 88 | TASK = NULL; |
1159 | jermar | 89 | btree_create(&tasks_btree); |
1 | jermar | 90 | } |
91 | |||
2227 | decky | 92 | /** Kill all tasks except the current task. |
93 | * |
||
94 | */ |
||
95 | void task_done(void) |
||
96 | { |
||
97 | task_t *t; |
||
98 | do { /* Repeat until there are any tasks except TASK */ |
||
99 | |||
100 | /* Messing with task structures, avoid deadlock */ |
||
101 | ipl_t ipl = interrupts_disable(); |
||
102 | spinlock_lock(&tasks_lock); |
||
103 | |||
104 | t = NULL; |
||
105 | link_t *cur; |
||
2436 | jermar | 106 | for (cur = tasks_btree.leaf_head.next; |
107 | cur != &tasks_btree.leaf_head; cur = cur->next) { |
||
108 | btree_node_t *node; |
||
2227 | decky | 109 | |
2436 | jermar | 110 | node = list_get_instance(cur, btree_node_t, leaf_link); |
111 | |||
2227 | decky | 112 | unsigned int i; |
113 | for (i = 0; i < node->keys; i++) { |
||
114 | if ((task_t *) node->value[i] != TASK) { |
||
115 | t = (task_t *) node->value[i]; |
||
116 | break; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | if (t != NULL) { |
||
122 | task_id_t id = t->taskid; |
||
123 | |||
124 | spinlock_unlock(&tasks_lock); |
||
125 | interrupts_restore(ipl); |
||
126 | |||
127 | #ifdef CONFIG_DEBUG |
||
128 | printf("Killing task %llu\n", id); |
||
129 | #endif |
||
130 | task_kill(id); |
||
131 | } else { |
||
132 | spinlock_unlock(&tasks_lock); |
||
133 | interrupts_restore(ipl); |
||
134 | } |
||
135 | |||
136 | } while (t != NULL); |
||
137 | } |
||
107 | decky | 138 | |
139 | /** Create new task |
||
140 | * |
||
141 | * Create new task with no threads. |
||
142 | * |
||
703 | jermar | 143 | * @param as Task's address space. |
1062 | jermar | 144 | * @param name Symbolic name. |
107 | decky | 145 | * |
973 | palkovsky | 146 | * @return New task's structure |
107 | decky | 147 | * |
148 | */ |
||
1062 | jermar | 149 | task_t *task_create(as_t *as, char *name) |
1 | jermar | 150 | { |
413 | jermar | 151 | ipl_t ipl; |
1 | jermar | 152 | task_t *ta; |
1040 | palkovsky | 153 | int i; |
1 | jermar | 154 | |
822 | palkovsky | 155 | ta = (task_t *) malloc(sizeof(task_t), 0); |
156 | |||
1185 | jermar | 157 | task_create_arch(ta); |
158 | |||
822 | palkovsky | 159 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
160 | list_initialize(&ta->th_head); |
||
161 | ta->as = as; |
||
1062 | jermar | 162 | ta->name = name; |
2446 | jermar | 163 | atomic_set(&ta->refcount, 0); |
164 | atomic_set(&ta->lifecount, 0); |
||
1839 | decky | 165 | ta->context = CONTEXT; |
1579 | jermar | 166 | |
1174 | jermar | 167 | ta->capabilities = 0; |
2039 | decky | 168 | ta->cycles = 0; |
1040 | palkovsky | 169 | |
955 | palkovsky | 170 | ipc_answerbox_init(&ta->answerbox); |
1839 | decky | 171 | for (i = 0; i < IPC_MAX_PHONES; i++) |
1040 | palkovsky | 172 | ipc_phone_init(&ta->phones[i]); |
2087 | jermar | 173 | if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, |
174 | ta->context))) |
||
1040 | palkovsky | 175 | ipc_phone_connect(&ta->phones[0], ipc_phone_0); |
998 | palkovsky | 176 | atomic_set(&ta->active_calls, 0); |
1460 | jermar | 177 | |
178 | mutex_initialize(&ta->futexes_lock); |
||
179 | btree_create(&ta->futexes); |
||
822 | palkovsky | 180 | |
181 | ipl = interrupts_disable(); |
||
1468 | jermar | 182 | |
183 | /* |
||
184 | * Increment address space reference count. |
||
185 | */ |
||
2183 | jermar | 186 | atomic_inc(&as->refcount); |
1468 | jermar | 187 | |
822 | palkovsky | 188 | spinlock_lock(&tasks_lock); |
1005 | palkovsky | 189 | ta->taskid = ++task_counter; |
1177 | jermar | 190 | btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL); |
822 | palkovsky | 191 | spinlock_unlock(&tasks_lock); |
192 | interrupts_restore(ipl); |
||
193 | |||
1 | jermar | 194 | return ta; |
195 | } |
||
196 | |||
1579 | jermar | 197 | /** Destroy task. |
198 | * |
||
199 | * @param t Task to be destroyed. |
||
200 | */ |
||
201 | void task_destroy(task_t *t) |
||
202 | { |
||
2446 | jermar | 203 | /* |
204 | * Remove the task from the task B+tree. |
||
205 | */ |
||
206 | spinlock_lock(&tasks_lock); |
||
207 | btree_remove(&tasks_btree, t->taskid, NULL); |
||
208 | spinlock_unlock(&tasks_lock); |
||
209 | |||
210 | /* |
||
211 | * Perform architecture specific task destruction. |
||
212 | */ |
||
1587 | jermar | 213 | task_destroy_arch(t); |
2446 | jermar | 214 | |
215 | /* |
||
216 | * Free up dynamically allocated state. |
||
217 | */ |
||
1587 | jermar | 218 | btree_destroy(&t->futexes); |
219 | |||
2446 | jermar | 220 | /* |
221 | * Drop our reference to the address space. |
||
222 | */ |
||
2183 | jermar | 223 | if (atomic_predec(&t->as->refcount) == 0) |
1587 | jermar | 224 | as_destroy(t->as); |
225 | |||
226 | free(t); |
||
227 | TASK = NULL; |
||
1579 | jermar | 228 | } |
229 | |||
973 | palkovsky | 230 | /** Create new task with 1 thread and run it |
231 | * |
||
1248 | jermar | 232 | * @param program_addr Address of program executable image. |
1062 | jermar | 233 | * @param name Program name. |
234 | * |
||
1229 | jermar | 235 | * @return Task of the running program or NULL on error. |
973 | palkovsky | 236 | */ |
2436 | jermar | 237 | task_t *task_run_program(void *program_addr, char *name) |
973 | palkovsky | 238 | { |
239 | as_t *as; |
||
240 | as_area_t *a; |
||
241 | int rc; |
||
2446 | jermar | 242 | thread_t *t; |
973 | palkovsky | 243 | task_t *task; |
1078 | jermar | 244 | uspace_arg_t *kernel_uarg; |
973 | palkovsky | 245 | |
246 | as = as_create(0); |
||
1115 | jermar | 247 | ASSERT(as); |
973 | palkovsky | 248 | |
1025 | palkovsky | 249 | rc = elf_load((elf_header_t *) program_addr, as); |
973 | palkovsky | 250 | if (rc != EE_OK) { |
1468 | jermar | 251 | as_destroy(as); |
973 | palkovsky | 252 | return NULL; |
253 | } |
||
254 | |||
1078 | jermar | 255 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
2087 | jermar | 256 | kernel_uarg->uspace_entry = |
257 | (void *) ((elf_header_t *) program_addr)->e_entry; |
||
1078 | jermar | 258 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
259 | kernel_uarg->uspace_thread_function = NULL; |
||
260 | kernel_uarg->uspace_thread_arg = NULL; |
||
261 | kernel_uarg->uspace_uarg = NULL; |
||
1066 | jermar | 262 | |
1062 | jermar | 263 | task = task_create(as, name); |
1115 | jermar | 264 | ASSERT(task); |
265 | |||
973 | palkovsky | 266 | /* |
267 | * Create the data as_area. |
||
268 | */ |
||
2087 | jermar | 269 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
270 | LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, |
||
271 | AS_AREA_ATTR_NONE, &anon_backend, NULL); |
||
1115 | jermar | 272 | |
1585 | jermar | 273 | /* |
274 | * Create the main thread. |
||
275 | */ |
||
2446 | jermar | 276 | t = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, |
2087 | jermar | 277 | "uinit", false); |
2446 | jermar | 278 | ASSERT(t); |
973 | palkovsky | 279 | |
2446 | jermar | 280 | thread_ready(t); |
1585 | jermar | 281 | |
973 | palkovsky | 282 | return task; |
283 | } |
||
1060 | palkovsky | 284 | |
1176 | jermar | 285 | /** Syscall for reading task ID from userspace. |
286 | * |
||
2087 | jermar | 287 | * @param uspace_task_id Userspace address of 8-byte buffer where to store |
288 | * current task ID. |
||
1176 | jermar | 289 | * |
1288 | jermar | 290 | * @return 0 on success or an error code from @ref errno.h. |
1176 | jermar | 291 | */ |
1780 | jermar | 292 | unative_t sys_task_get_id(task_id_t *uspace_task_id) |
1176 | jermar | 293 | { |
294 | /* |
||
295 | * No need to acquire lock on TASK because taskid |
||
296 | * remains constant for the lifespan of the task. |
||
297 | */ |
||
2087 | jermar | 298 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, |
299 | sizeof(TASK->taskid)); |
||
1176 | jermar | 300 | } |
301 | |||
1178 | jermar | 302 | /** Find task structure corresponding to task ID. |
303 | * |
||
304 | * The tasks_lock must be already held by the caller of this function |
||
305 | * and interrupts must be disabled. |
||
306 | * |
||
307 | * @param id Task ID. |
||
308 | * |
||
309 | * @return Task structure address or NULL if there is no such task ID. |
||
310 | */ |
||
311 | task_t *task_find_by_id(task_id_t id) |
||
312 | { |
||
313 | btree_node_t *leaf; |
||
314 | |||
315 | return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf); |
||
316 | } |
||
317 | |||
2039 | decky | 318 | /** Get accounting data of given task. |
319 | * |
||
2048 | jermar | 320 | * Note that task lock of 't' must be already held and |
2039 | decky | 321 | * interrupts must be already disabled. |
322 | * |
||
323 | * @param t Pointer to thread. |
||
324 | * |
||
325 | */ |
||
326 | uint64_t task_get_accounting(task_t *t) |
||
327 | { |
||
328 | /* Accumulated value of task */ |
||
329 | uint64_t ret = t->cycles; |
||
330 | |||
331 | /* Current values of threads */ |
||
332 | link_t *cur; |
||
333 | for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) { |
||
334 | thread_t *thr = list_get_instance(cur, thread_t, th_link); |
||
335 | |||
336 | spinlock_lock(&thr->lock); |
||
2042 | decky | 337 | /* Process only counted threads */ |
338 | if (!thr->uncounted) { |
||
2087 | jermar | 339 | if (thr == THREAD) { |
340 | /* Update accounting of current thread */ |
||
341 | thread_update_accounting(); |
||
342 | } |
||
2042 | decky | 343 | ret += thr->cycles; |
344 | } |
||
2039 | decky | 345 | spinlock_unlock(&thr->lock); |
346 | } |
||
347 | |||
348 | return ret; |
||
349 | } |
||
350 | |||
1579 | jermar | 351 | /** Kill task. |
352 | * |
||
2446 | jermar | 353 | * This function is idempotent. |
354 | * It signals all the task's threads to bail it out. |
||
355 | * |
||
1579 | jermar | 356 | * @param id ID of the task to be killed. |
357 | * |
||
358 | * @return 0 on success or an error code from errno.h |
||
359 | */ |
||
360 | int task_kill(task_id_t id) |
||
361 | { |
||
362 | ipl_t ipl; |
||
363 | task_t *ta; |
||
364 | link_t *cur; |
||
1600 | jermar | 365 | |
366 | if (id == 1) |
||
367 | return EPERM; |
||
1579 | jermar | 368 | |
369 | ipl = interrupts_disable(); |
||
370 | spinlock_lock(&tasks_lock); |
||
371 | if (!(ta = task_find_by_id(id))) { |
||
372 | spinlock_unlock(&tasks_lock); |
||
373 | interrupts_restore(ipl); |
||
374 | return ENOENT; |
||
375 | } |
||
1587 | jermar | 376 | spinlock_unlock(&tasks_lock); |
1579 | jermar | 377 | |
1585 | jermar | 378 | /* |
1687 | jermar | 379 | * Interrupt all threads except ktaskclnp. |
2446 | jermar | 380 | */ |
381 | spinlock_lock(&ta->lock); |
||
1579 | jermar | 382 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
383 | thread_t *thr; |
||
2446 | jermar | 384 | bool sleeping = false; |
1579 | jermar | 385 | |
386 | thr = list_get_instance(cur, thread_t, th_link); |
||
387 | |||
388 | spinlock_lock(&thr->lock); |
||
389 | thr->interrupted = true; |
||
390 | if (thr->state == Sleeping) |
||
391 | sleeping = true; |
||
392 | spinlock_unlock(&thr->lock); |
||
393 | |||
394 | if (sleeping) |
||
2109 | jermar | 395 | waitq_interrupt_sleep(thr); |
1579 | jermar | 396 | } |
1580 | jermar | 397 | spinlock_unlock(&ta->lock); |
398 | interrupts_restore(ipl); |
||
1579 | jermar | 399 | |
400 | return 0; |
||
401 | } |
||
402 | |||
1060 | palkovsky | 403 | /** Print task list */ |
404 | void task_print_list(void) |
||
405 | { |
||
406 | link_t *cur; |
||
407 | ipl_t ipl; |
||
408 | |||
2227 | decky | 409 | /* Messing with task structures, avoid deadlock */ |
1060 | palkovsky | 410 | ipl = interrupts_disable(); |
411 | spinlock_lock(&tasks_lock); |
||
2035 | decky | 412 | |
2087 | jermar | 413 | printf("taskid name ctx address as cycles threads " |
414 | "calls callee\n"); |
||
2446 | jermar | 415 | printf("------ ---------- --- ---------- ---------- ---------- ------- " |
416 | "------ ------>\n"); |
||
1060 | palkovsky | 417 | |
2087 | jermar | 418 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; |
419 | cur = cur->next) { |
||
1159 | jermar | 420 | btree_node_t *node; |
2118 | decky | 421 | unsigned int i; |
1159 | jermar | 422 | |
423 | node = list_get_instance(cur, btree_node_t, leaf_link); |
||
424 | for (i = 0; i < node->keys; i++) { |
||
425 | task_t *t; |
||
426 | int j; |
||
427 | |||
428 | t = (task_t *) node->value[i]; |
||
429 | |||
430 | spinlock_lock(&t->lock); |
||
2039 | decky | 431 | |
2050 | decky | 432 | uint64_t cycles; |
2039 | decky | 433 | char suffix; |
2050 | decky | 434 | order(task_get_accounting(t), &cycles, &suffix); |
2039 | decky | 435 | |
2216 | decky | 436 | printf("%-6llu %-10s %-3ld %#10zx %#10zx %9llu%c %7zd " |
2087 | jermar | 437 | "%6zd", t->taskid, t->name, t->context, t, t->as, |
438 | cycles, suffix, t->refcount, |
||
439 | atomic_get(&t->active_calls)); |
||
2035 | decky | 440 | for (j = 0; j < IPC_MAX_PHONES; j++) { |
1159 | jermar | 441 | if (t->phones[j].callee) |
2087 | jermar | 442 | printf(" %zd:%#zx", j, |
443 | t->phones[j].callee); |
||
1159 | jermar | 444 | } |
445 | printf("\n"); |
||
2039 | decky | 446 | |
1159 | jermar | 447 | spinlock_unlock(&t->lock); |
1060 | palkovsky | 448 | } |
449 | } |
||
450 | |||
451 | spinlock_unlock(&tasks_lock); |
||
452 | interrupts_restore(ipl); |
||
453 | } |
||
1579 | jermar | 454 | |
1757 | jermar | 455 | /** @} |
1702 | cejka | 456 | */ |