Subversion Repositories HelenOS

Rev

Rev 1597 | Rev 1636 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 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
 
1248 jermar 29
/**
30
 * @file	task.c
31
 * @brief	Task management.
32
 */
33
 
973 palkovsky 34
#include <main/uinit.h>
1 jermar 35
#include <proc/thread.h>
36
#include <proc/task.h>
1078 jermar 37
#include <proc/uarg.h>
703 jermar 38
#include <mm/as.h>
814 palkovsky 39
#include <mm/slab.h>
1 jermar 40
#include <synch/spinlock.h>
41
#include <arch.h>
42
#include <panic.h>
1159 jermar 43
#include <adt/btree.h>
788 jermar 44
#include <adt/list.h>
955 palkovsky 45
#include <ipc/ipc.h>
1174 jermar 46
#include <security/cap.h>
955 palkovsky 47
#include <memstr.h>
1060 palkovsky 48
#include <print.h>
973 palkovsky 49
#include <elf.h>
1579 jermar 50
#include <errno.h>
1288 jermar 51
#include <syscall/copy.h>
1597 palkovsky 52
#include <console/klog.h>
973 palkovsky 53
 
1170 vana 54
#ifndef LOADED_PROG_STACK_PAGES_NO
55
#define LOADED_PROG_STACK_PAGES_NO 1
56
#endif
1168 vana 57
 
623 jermar 58
SPINLOCK_INITIALIZE(tasks_lock);
1159 jermar 59
btree_t tasks_btree;
1005 palkovsky 60
static task_id_t task_counter = 0;
1 jermar 61
 
1585 jermar 62
static void ktaskclnp(void *arg);
63
static void ktaskkill(void *arg);
1579 jermar 64
 
107 decky 65
/** Initialize tasks
66
 *
67
 * Initialize kernel tasks support.
68
 *
69
 */
1 jermar 70
void task_init(void)
71
{
15 jermar 72
	TASK = NULL;
1159 jermar 73
	btree_create(&tasks_btree);
1 jermar 74
}
75
 
107 decky 76
 
77
/** Create new task
78
 *
79
 * Create new task with no threads.
80
 *
703 jermar 81
 * @param as Task's address space.
1062 jermar 82
 * @param name Symbolic name.
107 decky 83
 *
973 palkovsky 84
 * @return New task's structure
107 decky 85
 *
86
 */
1062 jermar 87
task_t *task_create(as_t *as, char *name)
1 jermar 88
{
413 jermar 89
	ipl_t ipl;
1 jermar 90
	task_t *ta;
1040 palkovsky 91
	int i;
1 jermar 92
 
822 palkovsky 93
	ta = (task_t *) malloc(sizeof(task_t), 0);
94
 
1185 jermar 95
	task_create_arch(ta);
96
 
822 palkovsky 97
	spinlock_initialize(&ta->lock, "task_ta_lock");
98
	list_initialize(&ta->th_head);
99
	ta->as = as;
1062 jermar 100
	ta->name = name;
1585 jermar 101
	ta->main_thread = NULL;
1579 jermar 102
	ta->refcount = 0;
103
 
1174 jermar 104
	ta->capabilities = 0;
1579 jermar 105
	ta->accept_new_threads = true;
1040 palkovsky 106
 
955 palkovsky 107
	ipc_answerbox_init(&ta->answerbox);
1040 palkovsky 108
	for (i=0; i < IPC_MAX_PHONES;i++)
109
		ipc_phone_init(&ta->phones[i]);
965 palkovsky 110
	if (ipc_phone_0)
1040 palkovsky 111
		ipc_phone_connect(&ta->phones[0], ipc_phone_0);
998 palkovsky 112
	atomic_set(&ta->active_calls, 0);
1460 jermar 113
 
114
	mutex_initialize(&ta->futexes_lock);
115
	btree_create(&ta->futexes);
822 palkovsky 116
 
117
	ipl = interrupts_disable();
1468 jermar 118
 
119
	/*
120
	 * Increment address space reference count.
121
	 * TODO: Reconsider the locking scheme.
122
	 */
123
	mutex_lock(&as->lock);
124
	as->refcount++;
125
	mutex_unlock(&as->lock);
126
 
822 palkovsky 127
	spinlock_lock(&tasks_lock);
1005 palkovsky 128
 
129
	ta->taskid = ++task_counter;
1177 jermar 130
	btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
1005 palkovsky 131
 
822 palkovsky 132
	spinlock_unlock(&tasks_lock);
133
	interrupts_restore(ipl);
134
 
1 jermar 135
	return ta;
136
}
137
 
1579 jermar 138
/** Destroy task.
139
 *
140
 * @param t Task to be destroyed.
141
 */
142
void task_destroy(task_t *t)
143
{
1587 jermar 144
	task_destroy_arch(t);
145
	btree_destroy(&t->futexes);
146
 
147
	mutex_lock_active(&t->as->lock);
148
	if (--t->as->refcount == 0) {
149
		mutex_unlock(&t->as->lock);
150
		as_destroy(t->as);
151
		/*
152
		 * t->as is destroyed.
153
		 */
154
	} else {
155
		mutex_unlock(&t->as->lock);
156
	}
157
 
158
	free(t);
159
	TASK = NULL;
1579 jermar 160
}
161
 
973 palkovsky 162
/** Create new task with 1 thread and run it
163
 *
1248 jermar 164
 * @param program_addr Address of program executable image.
1062 jermar 165
 * @param name Program name. 
166
 *
1229 jermar 167
 * @return Task of the running program or NULL on error.
973 palkovsky 168
 */
1062 jermar 169
task_t * task_run_program(void *program_addr, char *name)
973 palkovsky 170
{
171
	as_t *as;
172
	as_area_t *a;
173
	int rc;
1585 jermar 174
	thread_t *t1, *t2;
973 palkovsky 175
	task_t *task;
1078 jermar 176
	uspace_arg_t *kernel_uarg;
973 palkovsky 177
 
178
	as = as_create(0);
1115 jermar 179
	ASSERT(as);
973 palkovsky 180
 
1025 palkovsky 181
	rc = elf_load((elf_header_t *) program_addr, as);
973 palkovsky 182
	if (rc != EE_OK) {
1468 jermar 183
		as_destroy(as);
973 palkovsky 184
		return NULL;
185
	} 
186
 
1078 jermar 187
	kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
188
	kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
189
	kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
190
	kernel_uarg->uspace_thread_function = NULL;
191
	kernel_uarg->uspace_thread_arg = NULL;
192
	kernel_uarg->uspace_uarg = NULL;
1066 jermar 193
 
1062 jermar 194
	task = task_create(as, name);
1115 jermar 195
	ASSERT(task);
196
 
973 palkovsky 197
	/*
198
	 * Create the data as_area.
199
	 */
1424 jermar 200
	a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, 
201
		LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
1409 jermar 202
		USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
1115 jermar 203
 
1585 jermar 204
	/*
205
	 * Create the main thread.
206
	 */
207
	t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
208
	ASSERT(t1);
973 palkovsky 209
 
1585 jermar 210
	/*
211
	 * Create killer thread for the new task.
212
	 */
213
	t2 = thread_create(ktaskkill, t1, task, 0, "ktaskkill");
214
	ASSERT(t2);
215
	thread_ready(t2);
216
 
217
	thread_ready(t1);
218
 
973 palkovsky 219
	return task;
220
}
1060 palkovsky 221
 
1176 jermar 222
/** Syscall for reading task ID from userspace.
223
 *
1248 jermar 224
 * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
1176 jermar 225
 *
1288 jermar 226
 * @return 0 on success or an error code from @ref errno.h.
1176 jermar 227
 */
1227 jermar 228
__native sys_task_get_id(task_id_t *uspace_task_id)
1176 jermar 229
{
230
	/*
231
	 * No need to acquire lock on TASK because taskid
232
	 * remains constant for the lifespan of the task.
233
	 */
1288 jermar 234
	return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
1176 jermar 235
}
236
 
1178 jermar 237
/** Find task structure corresponding to task ID.
238
 *
239
 * The tasks_lock must be already held by the caller of this function
240
 * and interrupts must be disabled.
241
 *
1589 jermar 242
 * The task is guaranteed to exist after it was found in the tasks_btree as long as:
243
 * @li the tasks_lock is held,
244
 * @li the task's lock is held when task's lock is acquired before releasing tasks_lock or
245
 * @li the task's refcount is grater than 0
246
 *
1178 jermar 247
 * @param id Task ID.
248
 *
249
 * @return Task structure address or NULL if there is no such task ID.
250
 */
251
task_t *task_find_by_id(task_id_t id)
252
{
253
	btree_node_t *leaf;
254
 
255
	return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
256
}
257
 
1579 jermar 258
/** Kill task.
259
 *
260
 * @param id ID of the task to be killed.
261
 *
262
 * @return 0 on success or an error code from errno.h
263
 */
264
int task_kill(task_id_t id)
265
{
266
	ipl_t ipl;
267
	task_t *ta;
268
	thread_t *t;
269
	link_t *cur;
1600 jermar 270
 
271
	if (id == 1)
272
		return EPERM;
1579 jermar 273
 
274
	ipl = interrupts_disable();
275
	spinlock_lock(&tasks_lock);
276
 
277
	if (!(ta = task_find_by_id(id))) {
278
		spinlock_unlock(&tasks_lock);
279
		interrupts_restore(ipl);
280
		return ENOENT;
281
	}
1588 jermar 282
 
1579 jermar 283
	spinlock_lock(&ta->lock);
284
	ta->refcount++;
285
	spinlock_unlock(&ta->lock);
1587 jermar 286
 
1588 jermar 287
	btree_remove(&tasks_btree, ta->taskid, NULL);
1587 jermar 288
	spinlock_unlock(&tasks_lock);
1579 jermar 289
 
1580 jermar 290
	t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
1579 jermar 291
 
292
	spinlock_lock(&ta->lock);
1580 jermar 293
	ta->accept_new_threads = false;
1579 jermar 294
	ta->refcount--;
1585 jermar 295
 
296
	/*
297
	 * Interrupt all threads except this one.
298
	 */	
1579 jermar 299
	for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
300
		thread_t *thr;
301
		bool  sleeping = false;
302
 
303
		thr = list_get_instance(cur, thread_t, th_link);
304
		if (thr == t)
305
			continue;
306
 
307
		spinlock_lock(&thr->lock);
308
		thr->interrupted = true;
309
		if (thr->state == Sleeping)
310
			sleeping = true;
311
		spinlock_unlock(&thr->lock);
312
 
313
		if (sleeping)
314
			waitq_interrupt_sleep(thr);
315
	}
316
 
1580 jermar 317
	spinlock_unlock(&ta->lock);
318
	interrupts_restore(ipl);
1579 jermar 319
 
1580 jermar 320
	if (t)
321
		thread_ready(t);
322
 
1579 jermar 323
	return 0;
324
}
325
 
1060 palkovsky 326
/** Print task list */
327
void task_print_list(void)
328
{
329
	link_t *cur;
330
	ipl_t ipl;
331
 
332
	/* Messing with thread structures, avoid deadlock */
333
	ipl = interrupts_disable();
334
	spinlock_lock(&tasks_lock);
335
 
1159 jermar 336
	for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
337
		btree_node_t *node;
338
		int i;
339
 
340
		node = list_get_instance(cur, btree_node_t, leaf_link);
341
		for (i = 0; i < node->keys; i++) {
342
			task_t *t;
343
			int j;
344
 
345
			t = (task_t *) node->value[i];
346
 
347
			spinlock_lock(&t->lock);
1573 palkovsky 348
			printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
349
				t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
1159 jermar 350
			for (j=0; j < IPC_MAX_PHONES; j++) {
351
				if (t->phones[j].callee)
1196 cejka 352
					printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
1159 jermar 353
			}
354
			printf("\n");
355
			spinlock_unlock(&t->lock);
1060 palkovsky 356
		}
357
	}
358
 
359
	spinlock_unlock(&tasks_lock);
360
	interrupts_restore(ipl);
361
}
1579 jermar 362
 
1585 jermar 363
/** Kernel thread used to cleanup the task after it is killed. */
1580 jermar 364
void ktaskclnp(void *arg)
1579 jermar 365
{
1580 jermar 366
	ipl_t ipl;
1585 jermar 367
	thread_t *t = NULL, *main_thread;
1580 jermar 368
	link_t *cur;
369
 
370
	thread_detach(THREAD);
371
 
372
loop:
373
	ipl = interrupts_disable();
374
	spinlock_lock(&TASK->lock);
375
 
1585 jermar 376
	main_thread = TASK->main_thread;
377
 
1579 jermar 378
	/*
1580 jermar 379
	 * Find a thread to join.
380
	 */
381
	for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
382
		t = list_get_instance(cur, thread_t, th_link);
383
		if (t == THREAD)
384
			continue;
1585 jermar 385
		else if (t == main_thread)
386
			continue;
1580 jermar 387
		else
388
			break;
389
	}
390
 
391
	spinlock_unlock(&TASK->lock);
392
	interrupts_restore(ipl);
393
 
394
	if (t != THREAD) {
1585 jermar 395
		ASSERT(t != main_thread);	/* uninit is joined and detached in ktaskkill */
1580 jermar 396
		thread_join(t);
397
		thread_detach(t);
1585 jermar 398
		goto loop;	/* go for another thread */
1580 jermar 399
	}
400
 
401
	/*
402
	 * Now there are no other threads in this task
403
	 * and no new threads can be created.
404
	 */
405
 
1583 jermar 406
	ipc_cleanup();
407
	futex_cleanup();
1597 palkovsky 408
	klog_printf("Cleanup of task %lld completed.", TASK->taskid);
1579 jermar 409
}
1585 jermar 410
 
411
/** Kernel task used to kill a userspace task when its main thread exits.
412
 *
413
 * This thread waits until the main userspace thread (i.e. uninit) exits.
414
 * When this happens, the task is killed.
415
 *
416
 * @param arg Pointer to the thread structure of the task's main thread.
417
 */
418
void ktaskkill(void *arg)
419
{
420
	thread_t *t = (thread_t *) arg;
421
 
422
	/*
423
	 * Userspace threads cannot detach themselves,
424
	 * therefore the thread pointer is guaranteed to be valid.
425
	 */
426
	thread_join(t);	/* sleep uninterruptibly here! */
427
	thread_detach(t);
428
	task_kill(TASK->taskid);
429
}