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