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