source: mainline/generic/src/proc/task.c@ b7f364e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b7f364e was b7f364e, checked in by Jakub Jermar <jakub@…>, 19 years ago

Modify B+tree node key width to be 64-bit wide on all platforms.

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[f761f1eb]1/*
2 * Copyright (C) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[5be1923]29#include <main/uinit.h>
[f761f1eb]30#include <proc/thread.h>
31#include <proc/task.h>
[0f250f9]32#include <proc/uarg.h>
[20d50a1]33#include <mm/as.h>
[085d973]34#include <mm/slab.h>
[f761f1eb]35#include <synch/spinlock.h>
36#include <arch.h>
37#include <panic.h>
[7f6e755]38#include <adt/btree.h>
[5c9a08b]39#include <adt/list.h>
[6d9c49a]40#include <ipc/ipc.h>
[1077d91]41#include <security/cap.h>
[6d9c49a]42#include <memstr.h>
[37c57f2]43#include <print.h>
[5be1923]44#include <elf.h>
45
[8e5e78f]46
[a84af84]47#ifndef LOADED_PROG_STACK_PAGES_NO
48#define LOADED_PROG_STACK_PAGES_NO 1
49#endif
[8e5e78f]50
[dc747e3]51SPINLOCK_INITIALIZE(tasks_lock);
[7f6e755]52btree_t tasks_btree;
[286e03d]53static task_id_t task_counter = 0;
[70527f1]54
55/** Initialize tasks
56 *
57 * Initialize kernel tasks support.
58 *
59 */
[f761f1eb]60void task_init(void)
61{
[43114c5]62 TASK = NULL;
[7f6e755]63 btree_create(&tasks_btree);
[f761f1eb]64}
65
[70527f1]66
67/** Create new task
68 *
69 * Create new task with no threads.
70 *
[20d50a1]71 * @param as Task's address space.
[ff14c520]72 * @param name Symbolic name.
[70527f1]73 *
[5be1923]74 * @return New task's structure
[70527f1]75 *
76 */
[ff14c520]77task_t *task_create(as_t *as, char *name)
[f761f1eb]78{
[22f7769]79 ipl_t ipl;
[f761f1eb]80 task_t *ta;
[2ba7810]81 int i;
[f761f1eb]82
[bb68433]83 ta = (task_t *) malloc(sizeof(task_t), 0);
84
85 spinlock_initialize(&ta->lock, "task_ta_lock");
86 list_initialize(&ta->th_head);
87 ta->as = as;
[ff14c520]88 ta->name = name;
[6d9c49a]89
[1077d91]90 ta->capabilities = 0;
[2ba7810]91
[6d9c49a]92 ipc_answerbox_init(&ta->answerbox);
[2ba7810]93 for (i=0; i < IPC_MAX_PHONES;i++)
94 ipc_phone_init(&ta->phones[i]);
[e74cb73]95 if (ipc_phone_0)
[2ba7810]96 ipc_phone_connect(&ta->phones[0], ipc_phone_0);
[5f62ef9]97 atomic_set(&ta->active_calls, 0);
[bb68433]98
99 ipl = interrupts_disable();
100 spinlock_lock(&tasks_lock);
[286e03d]101
102 ta->taskid = ++task_counter;
[b7f364e]103 btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
[286e03d]104
[bb68433]105 spinlock_unlock(&tasks_lock);
106 interrupts_restore(ipl);
107
[f761f1eb]108 return ta;
109}
110
[5be1923]111/** Create new task with 1 thread and run it
[ff14c520]112 *
113 * @param programe_addr Address of program executable image.
114 * @param name Program name.
[5be1923]115 *
116 * @return Task of the running program or NULL on error
117 */
[ff14c520]118task_t * task_run_program(void *program_addr, char *name)
[5be1923]119{
120 as_t *as;
121 as_area_t *a;
122 int rc;
123 thread_t *t;
124 task_t *task;
[0f250f9]125 uspace_arg_t *kernel_uarg;
[5be1923]126
127 as = as_create(0);
[a0bb10ef]128 ASSERT(as);
[5be1923]129
[649799a]130 rc = elf_load((elf_header_t *) program_addr, as);
[5be1923]131 if (rc != EE_OK) {
132 as_free(as);
133 return NULL;
134 }
135
[0f250f9]136 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
137 kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
138 kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
139 kernel_uarg->uspace_thread_function = NULL;
140 kernel_uarg->uspace_thread_arg = NULL;
141 kernel_uarg->uspace_uarg = NULL;
[9f52563]142
[ff14c520]143 task = task_create(as, name);
[a0bb10ef]144 ASSERT(task);
145
[5be1923]146 /*
147 * Create the data as_area.
148 */
[8e5e78f]149 a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE, LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE, USTACK_ADDRESS);
[5be1923]150
[a0bb10ef]151 t = thread_create(uinit, kernel_uarg, task, 0, "uinit");
152 ASSERT(t);
153 thread_ready(t);
154
[5be1923]155 return task;
156}
[37c57f2]157
[ec55358]158/** Syscall for reading task ID from userspace.
159 *
160 * @param uaddr Userspace address of 8-byte buffer where to store current task ID.
161 *
162 * @return Always returns 0.
163 */
164__native sys_get_task_id(task_id_t *uspace_task_id)
165{
166 /*
167 * No need to acquire lock on TASK because taskid
168 * remains constant for the lifespan of the task.
169 */
170 copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
171
172 return 0;
173}
174
[37c57f2]175/** Print task list */
176void task_print_list(void)
177{
178 link_t *cur;
179 ipl_t ipl;
180
181 /* Messing with thread structures, avoid deadlock */
182 ipl = interrupts_disable();
183 spinlock_lock(&tasks_lock);
184
[7f6e755]185 for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
186 btree_node_t *node;
187 int i;
188
189 node = list_get_instance(cur, btree_node_t, leaf_link);
190 for (i = 0; i < node->keys; i++) {
191 task_t *t;
192 int j;
193
194 t = (task_t *) node->value[i];
195
196 spinlock_lock(&t->lock);
197 printf("%s: address=%P, taskid=%Q, as=%P, ActiveCalls: %d",
198 t->name, t, t->taskid, t->as, atomic_get(&t->active_calls));
199 for (j=0; j < IPC_MAX_PHONES; j++) {
200 if (t->phones[j].callee)
201 printf(" Ph(%d): %P ", j, t->phones[j].callee);
202 }
203 printf("\n");
204 spinlock_unlock(&t->lock);
[37c57f2]205 }
206 }
207
208 spinlock_unlock(&tasks_lock);
209 interrupts_restore(ipl);
210
211}
Note: See TracBrowser for help on using the repository browser.