Rev 2927 | Rev 3004 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2927 | Rev 3001 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericproc |
29 | /** @addtogroup genericproc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Task management. |
35 | * @brief Task management. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <main/uinit.h> |
38 | #include <main/uinit.h> |
39 | #include <proc/thread.h> |
39 | #include <proc/thread.h> |
40 | #include <proc/task.h> |
40 | #include <proc/task.h> |
41 | #include <proc/uarg.h> |
41 | #include <proc/uarg.h> |
42 | #include <mm/as.h> |
42 | #include <mm/as.h> |
43 | #include <mm/slab.h> |
43 | #include <mm/slab.h> |
44 | #include <atomic.h> |
44 | #include <atomic.h> |
45 | #include <synch/spinlock.h> |
45 | #include <synch/spinlock.h> |
46 | #include <synch/waitq.h> |
46 | #include <synch/waitq.h> |
47 | #include <arch.h> |
47 | #include <arch.h> |
48 | #include <panic.h> |
48 | #include <panic.h> |
49 | #include <adt/avl.h> |
49 | #include <adt/avl.h> |
50 | #include <adt/btree.h> |
50 | #include <adt/btree.h> |
51 | #include <adt/list.h> |
51 | #include <adt/list.h> |
52 | #include <ipc/ipc.h> |
52 | #include <ipc/ipc.h> |
53 | #include <security/cap.h> |
53 | #include <security/cap.h> |
54 | #include <memstr.h> |
54 | #include <memstr.h> |
55 | #include <print.h> |
55 | #include <print.h> |
56 | #include <lib/elf.h> |
56 | #include <lib/elf.h> |
57 | #include <errno.h> |
57 | #include <errno.h> |
58 | #include <func.h> |
58 | #include <func.h> |
59 | #include <syscall/copy.h> |
59 | #include <syscall/copy.h> |
60 | 60 | ||
61 | #ifndef LOADED_PROG_STACK_PAGES_NO |
61 | #ifndef LOADED_PROG_STACK_PAGES_NO |
62 | #define LOADED_PROG_STACK_PAGES_NO 1 |
62 | #define LOADED_PROG_STACK_PAGES_NO 1 |
63 | #endif |
63 | #endif |
64 | 64 | ||
65 | /** Spinlock protecting the tasks_tree AVL tree. */ |
65 | /** Spinlock protecting the tasks_tree AVL tree. */ |
66 | SPINLOCK_INITIALIZE(tasks_lock); |
66 | SPINLOCK_INITIALIZE(tasks_lock); |
67 | 67 | ||
68 | /** AVL tree of active tasks. |
68 | /** AVL tree of active tasks. |
69 | * |
69 | * |
70 | * The task is guaranteed to exist after it was found in the tasks_tree as |
70 | * The task is guaranteed to exist after it was found in the tasks_tree as |
71 | * long as: |
71 | * long as: |
72 | * @li the tasks_lock is held, |
72 | * @li the tasks_lock is held, |
73 | * @li the task's lock is held when task's lock is acquired before releasing |
73 | * @li the task's lock is held when task's lock is acquired before releasing |
74 | * tasks_lock or |
74 | * tasks_lock or |
75 | * @li the task's refcount is greater than 0 |
75 | * @li the task's refcount is greater than 0 |
76 | * |
76 | * |
77 | */ |
77 | */ |
78 | avltree_t tasks_tree; |
78 | avltree_t tasks_tree; |
79 | 79 | ||
80 | static task_id_t task_counter = 0; |
80 | static task_id_t task_counter = 0; |
81 | 81 | ||
- | 82 | /** |
|
- | 83 | * Points to the binary image used as the program loader. All non-initial |
|
- | 84 | * tasks are created from this executable image. |
|
- | 85 | */ |
|
- | 86 | void *program_loader = NULL; |
|
- | 87 | ||
- | 88 | ||
82 | /** Initialize tasks |
89 | /** Initialize tasks |
83 | * |
90 | * |
84 | * Initialize kernel tasks support. |
91 | * Initialize kernel tasks support. |
85 | * |
92 | * |
86 | */ |
93 | */ |
87 | void task_init(void) |
94 | void task_init(void) |
88 | { |
95 | { |
89 | TASK = NULL; |
96 | TASK = NULL; |
90 | avltree_create(&tasks_tree); |
97 | avltree_create(&tasks_tree); |
91 | } |
98 | } |
92 | 99 | ||
93 | /* |
100 | /* |
94 | * The idea behind this walker is to remember a single task different from TASK. |
101 | * The idea behind this walker is to remember a single task different from TASK. |
95 | */ |
102 | */ |
96 | static bool task_done_walker(avltree_node_t *node, void *arg) |
103 | static bool task_done_walker(avltree_node_t *node, void *arg) |
97 | { |
104 | { |
98 | task_t *t = avltree_get_instance(node, task_t, tasks_tree_node); |
105 | task_t *t = avltree_get_instance(node, task_t, tasks_tree_node); |
99 | task_t **tp = (task_t **) arg; |
106 | task_t **tp = (task_t **) arg; |
100 | 107 | ||
101 | if (t != TASK) { |
108 | if (t != TASK) { |
102 | *tp = t; |
109 | *tp = t; |
103 | return false; /* stop walking */ |
110 | return false; /* stop walking */ |
104 | } |
111 | } |
105 | 112 | ||
106 | return true; /* continue the walk */ |
113 | return true; /* continue the walk */ |
107 | } |
114 | } |
108 | 115 | ||
109 | /** Kill all tasks except the current task. |
116 | /** Kill all tasks except the current task. |
110 | * |
117 | * |
111 | */ |
118 | */ |
112 | void task_done(void) |
119 | void task_done(void) |
113 | { |
120 | { |
114 | task_t *t; |
121 | task_t *t; |
115 | do { /* Repeat until there are any tasks except TASK */ |
122 | do { /* Repeat until there are any tasks except TASK */ |
116 | 123 | ||
117 | /* Messing with task structures, avoid deadlock */ |
124 | /* Messing with task structures, avoid deadlock */ |
118 | ipl_t ipl = interrupts_disable(); |
125 | ipl_t ipl = interrupts_disable(); |
119 | spinlock_lock(&tasks_lock); |
126 | spinlock_lock(&tasks_lock); |
120 | 127 | ||
121 | t = NULL; |
128 | t = NULL; |
122 | avltree_walk(&tasks_tree, task_done_walker, &t); |
129 | avltree_walk(&tasks_tree, task_done_walker, &t); |
123 | 130 | ||
124 | if (t != NULL) { |
131 | if (t != NULL) { |
125 | task_id_t id = t->taskid; |
132 | task_id_t id = t->taskid; |
126 | 133 | ||
127 | spinlock_unlock(&tasks_lock); |
134 | spinlock_unlock(&tasks_lock); |
128 | interrupts_restore(ipl); |
135 | interrupts_restore(ipl); |
129 | 136 | ||
130 | #ifdef CONFIG_DEBUG |
137 | #ifdef CONFIG_DEBUG |
131 | printf("Killing task %llu\n", id); |
138 | printf("Killing task %llu\n", id); |
132 | #endif |
139 | #endif |
133 | task_kill(id); |
140 | task_kill(id); |
134 | thread_usleep(10000); |
141 | thread_usleep(10000); |
135 | } else { |
142 | } else { |
136 | spinlock_unlock(&tasks_lock); |
143 | spinlock_unlock(&tasks_lock); |
137 | interrupts_restore(ipl); |
144 | interrupts_restore(ipl); |
138 | } |
145 | } |
139 | 146 | ||
140 | } while (t != NULL); |
147 | } while (t != NULL); |
141 | } |
148 | } |
142 | 149 | ||
143 | /** Create new task |
150 | /** Create new task |
144 | * |
151 | * |
145 | * Create new task with no threads. |
152 | * Create new task with no threads. |
146 | * |
153 | * |
147 | * @param as Task's address space. |
154 | * @param as Task's address space. |
148 | * @param name Symbolic name. |
155 | * @param name Symbolic name. |
149 | * |
156 | * |
150 | * @return New task's structure |
157 | * @return New task's structure |
151 | * |
158 | * |
152 | */ |
159 | */ |
153 | task_t *task_create(as_t *as, char *name) |
160 | task_t *task_create(as_t *as, char *name) |
154 | { |
161 | { |
155 | ipl_t ipl; |
162 | ipl_t ipl; |
156 | task_t *ta; |
163 | task_t *ta; |
157 | int i; |
164 | int i; |
158 | 165 | ||
159 | ta = (task_t *) malloc(sizeof(task_t), 0); |
166 | ta = (task_t *) malloc(sizeof(task_t), 0); |
160 | 167 | ||
161 | task_create_arch(ta); |
168 | task_create_arch(ta); |
162 | 169 | ||
163 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
170 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
164 | list_initialize(&ta->th_head); |
171 | list_initialize(&ta->th_head); |
165 | ta->as = as; |
172 | ta->as = as; |
166 | ta->name = name; |
173 | ta->name = name; |
167 | atomic_set(&ta->refcount, 0); |
174 | atomic_set(&ta->refcount, 0); |
168 | atomic_set(&ta->lifecount, 0); |
175 | atomic_set(&ta->lifecount, 0); |
169 | ta->context = CONTEXT; |
176 | ta->context = CONTEXT; |
170 | 177 | ||
171 | ta->capabilities = 0; |
178 | ta->capabilities = 0; |
172 | ta->cycles = 0; |
179 | ta->cycles = 0; |
173 | 180 | ||
174 | ipc_answerbox_init(&ta->answerbox, ta); |
181 | ipc_answerbox_init(&ta->answerbox, ta); |
175 | for (i = 0; i < IPC_MAX_PHONES; i++) |
182 | for (i = 0; i < IPC_MAX_PHONES; i++) |
176 | ipc_phone_init(&ta->phones[i]); |
183 | ipc_phone_init(&ta->phones[i]); |
177 | if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, |
184 | if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, |
178 | ta->context))) |
185 | ta->context))) |
179 | ipc_phone_connect(&ta->phones[0], ipc_phone_0); |
186 | ipc_phone_connect(&ta->phones[0], ipc_phone_0); |
180 | atomic_set(&ta->active_calls, 0); |
187 | atomic_set(&ta->active_calls, 0); |
181 | 188 | ||
182 | mutex_initialize(&ta->futexes_lock); |
189 | mutex_initialize(&ta->futexes_lock); |
183 | btree_create(&ta->futexes); |
190 | btree_create(&ta->futexes); |
184 | 191 | ||
185 | ipl = interrupts_disable(); |
192 | ipl = interrupts_disable(); |
186 | 193 | ||
187 | /* |
194 | /* |
188 | * Increment address space reference count. |
195 | * Increment address space reference count. |
189 | */ |
196 | */ |
190 | atomic_inc(&as->refcount); |
197 | atomic_inc(&as->refcount); |
191 | 198 | ||
192 | spinlock_lock(&tasks_lock); |
199 | spinlock_lock(&tasks_lock); |
193 | ta->taskid = ++task_counter; |
200 | ta->taskid = ++task_counter; |
194 | avltree_node_initialize(&ta->tasks_tree_node); |
201 | avltree_node_initialize(&ta->tasks_tree_node); |
195 | ta->tasks_tree_node.key = ta->taskid; |
202 | ta->tasks_tree_node.key = ta->taskid; |
196 | avltree_insert(&tasks_tree, &ta->tasks_tree_node); |
203 | avltree_insert(&tasks_tree, &ta->tasks_tree_node); |
197 | spinlock_unlock(&tasks_lock); |
204 | spinlock_unlock(&tasks_lock); |
198 | interrupts_restore(ipl); |
205 | interrupts_restore(ipl); |
199 | 206 | ||
200 | return ta; |
207 | return ta; |
201 | } |
208 | } |
202 | 209 | ||
203 | /** Destroy task. |
210 | /** Destroy task. |
204 | * |
211 | * |
205 | * @param t Task to be destroyed. |
212 | * @param t Task to be destroyed. |
206 | */ |
213 | */ |
207 | void task_destroy(task_t *t) |
214 | void task_destroy(task_t *t) |
208 | { |
215 | { |
209 | /* |
216 | /* |
210 | * Remove the task from the task B+tree. |
217 | * Remove the task from the task B+tree. |
211 | */ |
218 | */ |
212 | spinlock_lock(&tasks_lock); |
219 | spinlock_lock(&tasks_lock); |
213 | avltree_delete(&tasks_tree, &t->tasks_tree_node); |
220 | avltree_delete(&tasks_tree, &t->tasks_tree_node); |
214 | spinlock_unlock(&tasks_lock); |
221 | spinlock_unlock(&tasks_lock); |
215 | 222 | ||
216 | /* |
223 | /* |
217 | * Perform architecture specific task destruction. |
224 | * Perform architecture specific task destruction. |
218 | */ |
225 | */ |
219 | task_destroy_arch(t); |
226 | task_destroy_arch(t); |
220 | 227 | ||
221 | /* |
228 | /* |
222 | * Free up dynamically allocated state. |
229 | * Free up dynamically allocated state. |
223 | */ |
230 | */ |
224 | btree_destroy(&t->futexes); |
231 | btree_destroy(&t->futexes); |
225 | 232 | ||
226 | /* |
233 | /* |
227 | * Drop our reference to the address space. |
234 | * Drop our reference to the address space. |
228 | */ |
235 | */ |
229 | if (atomic_predec(&t->as->refcount) == 0) |
236 | if (atomic_predec(&t->as->refcount) == 0) |
230 | as_destroy(t->as); |
237 | as_destroy(t->as); |
231 | 238 | ||
232 | free(t); |
239 | free(t); |
233 | TASK = NULL; |
240 | TASK = NULL; |
234 | } |
241 | } |
235 | 242 | ||
236 | /** Create new task with 1 thread and run it |
243 | /** Create new task with 1 thread and run it |
237 | * |
244 | * |
- | 245 | * @param as Address space containing a binary program image. |
|
238 | * @param program_addr Address of program executable image. |
246 | * @param entry_addr Program entry-point address in program address space. |
239 | * @param name Program name. |
247 | * @param name Program name. |
240 | * |
248 | * |
241 | * @return Task of the running program or NULL on error. |
249 | * @return Task of the running program or NULL on error. |
242 | */ |
250 | */ |
243 | task_t *task_run_program(void *program_addr, char *name) |
251 | task_t *task_create_from_as(as_t *as, uintptr_t entry_addr, char *name) |
244 | { |
252 | { |
245 | as_t *as; |
- | |
246 | as_area_t *a; |
253 | as_area_t *a; |
247 | unsigned int rc; |
- | |
248 | thread_t *t; |
254 | thread_t *t; |
249 | task_t *task; |
255 | task_t *task; |
250 | uspace_arg_t *kernel_uarg; |
256 | uspace_arg_t *kernel_uarg; |
251 | 257 | ||
252 | as = as_create(0); |
- | |
253 | ASSERT(as); |
- | |
254 | - | ||
255 | rc = elf_load((elf_header_t *) program_addr, as); |
- | |
256 | if (rc != EE_OK) { |
- | |
257 | as_destroy(as); |
- | |
258 | return NULL; |
- | |
259 | } |
- | |
260 | - | ||
261 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
258 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
262 | kernel_uarg->uspace_entry = |
259 | kernel_uarg->uspace_entry = (void *) entry_addr; |
263 | (void *) ((elf_header_t *) program_addr)->e_entry; |
- | |
264 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
260 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
265 | kernel_uarg->uspace_thread_function = NULL; |
261 | kernel_uarg->uspace_thread_function = NULL; |
266 | kernel_uarg->uspace_thread_arg = NULL; |
262 | kernel_uarg->uspace_thread_arg = NULL; |
267 | kernel_uarg->uspace_uarg = NULL; |
263 | kernel_uarg->uspace_uarg = NULL; |
268 | 264 | ||
269 | task = task_create(as, name); |
265 | task = task_create(as, name); |
270 | ASSERT(task); |
266 | ASSERT(task); |
271 | 267 | ||
272 | /* |
268 | /* |
273 | * Create the data as_area. |
269 | * Create the data as_area. |
274 | */ |
270 | */ |
275 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
271 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
276 | LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, |
272 | LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, |
277 | AS_AREA_ATTR_NONE, &anon_backend, NULL); |
273 | AS_AREA_ATTR_NONE, &anon_backend, NULL); |
278 | 274 | ||
279 | /* |
275 | /* |
280 | * Create the main thread. |
276 | * Create the main thread. |
281 | */ |
277 | */ |
282 | t = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, |
278 | t = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, |
283 | "uinit", false); |
279 | "uinit", false); |
284 | ASSERT(t); |
280 | ASSERT(t); |
285 | 281 | ||
286 | thread_ready(t); |
- | |
287 | - | ||
288 | return task; |
282 | return task; |
289 | } |
283 | } |
290 | 284 | ||
- | 285 | /** Parse an executable image in the physical memory. |
|
- | 286 | * |
|
- | 287 | * If the image belongs to a program loader, it is registered as such, |
|
- | 288 | * (and *task is set to NULL). Otherwise a task is created from the |
|
- | 289 | * executable image. The task is returned in *task. |
|
- | 290 | * |
|
- | 291 | * @param program_addr Address of program executable image. |
|
- | 292 | * @param name Program name. |
|
- | 293 | * @param task Where to store the pointer to the newly created task. |
|
- | 294 | * |
|
- | 295 | * @return EOK on success or negative error code. |
|
- | 296 | */ |
|
- | 297 | int task_parse_initial(void *program_addr, char *name, task_t **task) |
|
- | 298 | { |
|
- | 299 | as_t *as; |
|
- | 300 | unsigned int rc; |
|
- | 301 | ||
- | 302 | as = as_create(0); |
|
- | 303 | ASSERT(as); |
|
- | 304 | ||
- | 305 | rc = elf_load((elf_header_t *) program_addr, as, 0); |
|
- | 306 | if (rc != EE_OK) { |
|
- | 307 | as_destroy(as); |
|
- | 308 | *task = NULL; |
|
- | 309 | if (rc != EE_LOADER) |
|
- | 310 | return ENOTSUP; |
|
- | 311 | ||
- | 312 | /* Register image as the program loader */ |
|
- | 313 | ASSERT(program_loader == NULL); |
|
- | 314 | program_loader = program_addr; |
|
- | 315 | return EOK; |
|
- | 316 | } |
|
- | 317 | ||
- | 318 | *task = task_create_from_as(as, ((elf_header_t *) program_addr)->e_entry, |
|
- | 319 | name); |
|
- | 320 | ||
- | 321 | return EOK; |
|
- | 322 | } |
|
- | 323 | ||
- | 324 | /** Create a task from the program loader image. |
|
- | 325 | * |
|
- | 326 | * @param program_addr Address of program executable image. |
|
- | 327 | * @param name Program name. |
|
- | 328 | * |
|
- | 329 | * @return Task of the running program or NULL on error. |
|
- | 330 | */ |
|
- | 331 | task_t *task_create_from_loader(char *name) |
|
- | 332 | { |
|
- | 333 | as_t *as; |
|
- | 334 | unsigned int rc; |
|
- | 335 | ||
- | 336 | as = as_create(0); |
|
- | 337 | ASSERT(as); |
|
- | 338 | ||
- | 339 | rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER); |
|
- | 340 | if (rc != EE_OK) { |
|
- | 341 | as_destroy(as); |
|
- | 342 | return NULL; |
|
- | 343 | } |
|
- | 344 | ||
- | 345 | return task_create_from_as( |
|
- | 346 | as, ((elf_header_t *) program_loader)->e_entry, name); |
|
- | 347 | } |
|
- | 348 | ||
- | 349 | /** Make task ready. |
|
- | 350 | * |
|
- | 351 | * Switch task's thread to the ready state. |
|
- | 352 | * |
|
- | 353 | * @param ta Task to make ready. |
|
- | 354 | */ |
|
- | 355 | void task_ready(task_t *t) |
|
- | 356 | { |
|
- | 357 | thread_t *th; |
|
- | 358 | ||
- | 359 | th = list_get_instance(t->th_head.next, thread_t, th_link); |
|
- | 360 | thread_ready(th); |
|
- | 361 | } |
|
- | 362 | ||
291 | /** Syscall for reading task ID from userspace. |
363 | /** Syscall for reading task ID from userspace. |
292 | * |
364 | * |
293 | * @param uspace_task_id Userspace address of 8-byte buffer where to store |
365 | * @param uspace_task_id Userspace address of 8-byte buffer where to store |
294 | * current task ID. |
366 | * current task ID. |
295 | * |
367 | * |
296 | * @return 0 on success or an error code from @ref errno.h. |
368 | * @return 0 on success or an error code from @ref errno.h. |
297 | */ |
369 | */ |
298 | unative_t sys_task_get_id(task_id_t *uspace_task_id) |
370 | unative_t sys_task_get_id(task_id_t *uspace_task_id) |
299 | { |
371 | { |
300 | /* |
372 | /* |
301 | * No need to acquire lock on TASK because taskid |
373 | * No need to acquire lock on TASK because taskid |
302 | * remains constant for the lifespan of the task. |
374 | * remains constant for the lifespan of the task. |
303 | */ |
375 | */ |
304 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, |
376 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, |
305 | sizeof(TASK->taskid)); |
377 | sizeof(TASK->taskid)); |
306 | } |
378 | } |
- | 379 | ||
- | 380 | /** Syscall for creating a new task from userspace. |
|
- | 381 | * |
|
- | 382 | * Creates a new task from the program loader image and stores its |
|
- | 383 | * task id into the provided buffer. |
|
- | 384 | * |
|
- | 385 | * @param uspace_task_id Userspace address of 8-byte buffer where to store |
|
- | 386 | * current task ID. |
|
- | 387 | * |
|
- | 388 | * @return 0 on success or an error code from @ref errno.h. |
|
- | 389 | */ |
|
- | 390 | unative_t sys_task_spawn(task_id_t *uspace_task_id) |
|
- | 391 | { |
|
- | 392 | task_t *t; |
|
- | 393 | task_id_t fake_id; |
|
- | 394 | int rc; |
|
- | 395 | ||
- | 396 | /* Before we even try creating the task, see if we can write the id */ |
|
- | 397 | rc = (unative_t) copy_to_uspace(uspace_task_id, &fake_id, |
|
- | 398 | sizeof(fake_id)); |
|
- | 399 | if (rc != 0) |
|
- | 400 | return rc; |
|
- | 401 | ||
- | 402 | t = task_create_from_loader("loader"); |
|
- | 403 | ||
- | 404 | /* No need to aquire lock before task_ready() */ |
|
- | 405 | rc = (unative_t) copy_to_uspace(uspace_task_id, &t->taskid, |
|
- | 406 | sizeof(t->taskid)); |
|
- | 407 | if (rc != 0) { |
|
- | 408 | /* Ooops */ |
|
- | 409 | task_kill(t->taskid); |
|
- | 410 | return rc; |
|
- | 411 | } |
|
- | 412 | ||
- | 413 | task_ready(t); |
|
- | 414 | ||
- | 415 | return EOK; |
|
- | 416 | } |
|
307 | 417 | ||
308 | /** Find task structure corresponding to task ID. |
418 | /** Find task structure corresponding to task ID. |
309 | * |
419 | * |
310 | * The tasks_lock must be already held by the caller of this function |
420 | * The tasks_lock must be already held by the caller of this function |
311 | * and interrupts must be disabled. |
421 | * and interrupts must be disabled. |
312 | * |
422 | * |
313 | * @param id Task ID. |
423 | * @param id Task ID. |
314 | * |
424 | * |
315 | * @return Task structure address or NULL if there is no such task ID. |
425 | * @return Task structure address or NULL if there is no such task ID. |
316 | */ |
426 | */ |
317 | task_t *task_find_by_id(task_id_t id) |
427 | task_t *task_find_by_id(task_id_t id) |
318 | { |
428 | { |
319 | avltree_node_t *node; |
429 | avltree_node_t *node; |
320 | 430 | ||
321 | node = avltree_search(&tasks_tree, (avltree_key_t) id); |
431 | node = avltree_search(&tasks_tree, (avltree_key_t) id); |
322 | 432 | ||
323 | if (node) |
433 | if (node) |
324 | return avltree_get_instance(node, task_t, tasks_tree_node); |
434 | return avltree_get_instance(node, task_t, tasks_tree_node); |
325 | return NULL; |
435 | return NULL; |
326 | } |
436 | } |
327 | 437 | ||
328 | /** Get accounting data of given task. |
438 | /** Get accounting data of given task. |
329 | * |
439 | * |
330 | * Note that task lock of 't' must be already held and |
440 | * Note that task lock of 't' must be already held and |
331 | * interrupts must be already disabled. |
441 | * interrupts must be already disabled. |
332 | * |
442 | * |
333 | * @param t Pointer to thread. |
443 | * @param t Pointer to thread. |
334 | * |
444 | * |
335 | */ |
445 | */ |
336 | uint64_t task_get_accounting(task_t *t) |
446 | uint64_t task_get_accounting(task_t *t) |
337 | { |
447 | { |
338 | /* Accumulated value of task */ |
448 | /* Accumulated value of task */ |
339 | uint64_t ret = t->cycles; |
449 | uint64_t ret = t->cycles; |
340 | 450 | ||
341 | /* Current values of threads */ |
451 | /* Current values of threads */ |
342 | link_t *cur; |
452 | link_t *cur; |
343 | for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) { |
453 | for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) { |
344 | thread_t *thr = list_get_instance(cur, thread_t, th_link); |
454 | thread_t *thr = list_get_instance(cur, thread_t, th_link); |
345 | 455 | ||
346 | spinlock_lock(&thr->lock); |
456 | spinlock_lock(&thr->lock); |
347 | /* Process only counted threads */ |
457 | /* Process only counted threads */ |
348 | if (!thr->uncounted) { |
458 | if (!thr->uncounted) { |
349 | if (thr == THREAD) { |
459 | if (thr == THREAD) { |
350 | /* Update accounting of current thread */ |
460 | /* Update accounting of current thread */ |
351 | thread_update_accounting(); |
461 | thread_update_accounting(); |
352 | } |
462 | } |
353 | ret += thr->cycles; |
463 | ret += thr->cycles; |
354 | } |
464 | } |
355 | spinlock_unlock(&thr->lock); |
465 | spinlock_unlock(&thr->lock); |
356 | } |
466 | } |
357 | 467 | ||
358 | return ret; |
468 | return ret; |
359 | } |
469 | } |
360 | 470 | ||
361 | /** Kill task. |
471 | /** Kill task. |
362 | * |
472 | * |
363 | * This function is idempotent. |
473 | * This function is idempotent. |
364 | * It signals all the task's threads to bail it out. |
474 | * It signals all the task's threads to bail it out. |
365 | * |
475 | * |
366 | * @param id ID of the task to be killed. |
476 | * @param id ID of the task to be killed. |
367 | * |
477 | * |
368 | * @return 0 on success or an error code from errno.h |
478 | * @return 0 on success or an error code from errno.h |
369 | */ |
479 | */ |
370 | int task_kill(task_id_t id) |
480 | int task_kill(task_id_t id) |
371 | { |
481 | { |
372 | ipl_t ipl; |
482 | ipl_t ipl; |
373 | task_t *ta; |
483 | task_t *ta; |
374 | link_t *cur; |
484 | link_t *cur; |
375 | 485 | ||
376 | if (id == 1) |
486 | if (id == 1) |
377 | return EPERM; |
487 | return EPERM; |
378 | 488 | ||
379 | ipl = interrupts_disable(); |
489 | ipl = interrupts_disable(); |
380 | spinlock_lock(&tasks_lock); |
490 | spinlock_lock(&tasks_lock); |
381 | if (!(ta = task_find_by_id(id))) { |
491 | if (!(ta = task_find_by_id(id))) { |
382 | spinlock_unlock(&tasks_lock); |
492 | spinlock_unlock(&tasks_lock); |
383 | interrupts_restore(ipl); |
493 | interrupts_restore(ipl); |
384 | return ENOENT; |
494 | return ENOENT; |
385 | } |
495 | } |
386 | spinlock_unlock(&tasks_lock); |
496 | spinlock_unlock(&tasks_lock); |
387 | 497 | ||
388 | /* |
498 | /* |
389 | * Interrupt all threads except ktaskclnp. |
499 | * Interrupt all threads except ktaskclnp. |
390 | */ |
500 | */ |
391 | spinlock_lock(&ta->lock); |
501 | spinlock_lock(&ta->lock); |
392 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
502 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
393 | thread_t *thr; |
503 | thread_t *thr; |
394 | bool sleeping = false; |
504 | bool sleeping = false; |
395 | 505 | ||
396 | thr = list_get_instance(cur, thread_t, th_link); |
506 | thr = list_get_instance(cur, thread_t, th_link); |
397 | 507 | ||
398 | spinlock_lock(&thr->lock); |
508 | spinlock_lock(&thr->lock); |
399 | thr->interrupted = true; |
509 | thr->interrupted = true; |
400 | if (thr->state == Sleeping) |
510 | if (thr->state == Sleeping) |
401 | sleeping = true; |
511 | sleeping = true; |
402 | spinlock_unlock(&thr->lock); |
512 | spinlock_unlock(&thr->lock); |
403 | 513 | ||
404 | if (sleeping) |
514 | if (sleeping) |
405 | waitq_interrupt_sleep(thr); |
515 | waitq_interrupt_sleep(thr); |
406 | } |
516 | } |
407 | spinlock_unlock(&ta->lock); |
517 | spinlock_unlock(&ta->lock); |
408 | interrupts_restore(ipl); |
518 | interrupts_restore(ipl); |
409 | 519 | ||
410 | return 0; |
520 | return 0; |
411 | } |
521 | } |
412 | 522 | ||
413 | static bool task_print_walker(avltree_node_t *node, void *arg) |
523 | static bool task_print_walker(avltree_node_t *node, void *arg) |
414 | { |
524 | { |
415 | task_t *t = avltree_get_instance(node, task_t, tasks_tree_node); |
525 | task_t *t = avltree_get_instance(node, task_t, tasks_tree_node); |
416 | int j; |
526 | int j; |
417 | 527 | ||
418 | spinlock_lock(&t->lock); |
528 | spinlock_lock(&t->lock); |
419 | 529 | ||
420 | uint64_t cycles; |
530 | uint64_t cycles; |
421 | char suffix; |
531 | char suffix; |
422 | order(task_get_accounting(t), &cycles, &suffix); |
532 | order(task_get_accounting(t), &cycles, &suffix); |
423 | 533 | ||
424 | if (sizeof(void *) == 4) |
534 | if (sizeof(void *) == 4) |
425 | printf("%-6llu %-10s %-3ld %#10zx %#10zx %9llu%c %7zd %6zd", |
535 | printf("%-6llu %-10s %-3ld %#10zx %#10zx %9llu%c %7zd %6zd", |
426 | t->taskid, t->name, t->context, t, t->as, cycles, suffix, |
536 | t->taskid, t->name, t->context, t, t->as, cycles, suffix, |
427 | t->refcount, atomic_get(&t->active_calls)); |
537 | t->refcount, atomic_get(&t->active_calls)); |
428 | else |
538 | else |
429 | printf("%-6llu %-10s %-3ld %#18zx %#18zx %9llu%c %7zd %6zd", |
539 | printf("%-6llu %-10s %-3ld %#18zx %#18zx %9llu%c %7zd %6zd", |
430 | t->taskid, t->name, t->context, t, t->as, cycles, suffix, |
540 | t->taskid, t->name, t->context, t, t->as, cycles, suffix, |
431 | t->refcount, atomic_get(&t->active_calls)); |
541 | t->refcount, atomic_get(&t->active_calls)); |
432 | for (j = 0; j < IPC_MAX_PHONES; j++) { |
542 | for (j = 0; j < IPC_MAX_PHONES; j++) { |
433 | if (t->phones[j].callee) |
543 | if (t->phones[j].callee) |
434 | printf(" %zd:%#zx", j, t->phones[j].callee); |
544 | printf(" %zd:%#zx", j, t->phones[j].callee); |
435 | } |
545 | } |
436 | printf("\n"); |
546 | printf("\n"); |
437 | 547 | ||
438 | spinlock_unlock(&t->lock); |
548 | spinlock_unlock(&t->lock); |
439 | return true; |
549 | return true; |
440 | } |
550 | } |
441 | 551 | ||
442 | /** Print task list */ |
552 | /** Print task list */ |
443 | void task_print_list(void) |
553 | void task_print_list(void) |
444 | { |
554 | { |
445 | ipl_t ipl; |
555 | ipl_t ipl; |
446 | 556 | ||
447 | /* Messing with task structures, avoid deadlock */ |
557 | /* Messing with task structures, avoid deadlock */ |
448 | ipl = interrupts_disable(); |
558 | ipl = interrupts_disable(); |
449 | spinlock_lock(&tasks_lock); |
559 | spinlock_lock(&tasks_lock); |
450 | 560 | ||
451 | if (sizeof(void *) == 4) { |
561 | if (sizeof(void *) == 4) { |
452 | printf("taskid name ctx address as " |
562 | printf("taskid name ctx address as " |
453 | "cycles threads calls callee\n"); |
563 | "cycles threads calls callee\n"); |
454 | printf("------ ---------- --- ---------- ---------- " |
564 | printf("------ ---------- --- ---------- ---------- " |
455 | "---------- ------- ------ ------>\n"); |
565 | "---------- ------- ------ ------>\n"); |
456 | } else { |
566 | } else { |
457 | printf("taskid name ctx address as " |
567 | printf("taskid name ctx address as " |
458 | "cycles threads calls callee\n"); |
568 | "cycles threads calls callee\n"); |
459 | printf("------ ---------- --- ------------------ ------------------ " |
569 | printf("------ ---------- --- ------------------ ------------------ " |
460 | "---------- ------- ------ ------>\n"); |
570 | "---------- ------- ------ ------>\n"); |
461 | } |
571 | } |
462 | 572 | ||
463 | avltree_walk(&tasks_tree, task_print_walker, NULL); |
573 | avltree_walk(&tasks_tree, task_print_walker, NULL); |
464 | 574 | ||
465 | spinlock_unlock(&tasks_lock); |
575 | spinlock_unlock(&tasks_lock); |
466 | interrupts_restore(ipl); |
576 | interrupts_restore(ipl); |
467 | } |
577 | } |
468 | 578 | ||
469 | /** @} |
579 | /** @} |
470 | */ |
580 | */ |
471 | 581 |