Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2001-2004 Jakub Jermar
1 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
 
1757 jermar 29
/** @addtogroup genericproc
1702 cejka 30
 * @{
31
 */
32
 
1248 jermar 33
/**
1702 cejka 34
 * @file
1248 jermar 35
 * @brief	Task management.
36
 */
37
 
973 palkovsky 38
#include <main/uinit.h>
1 jermar 39
#include <proc/thread.h>
40
#include <proc/task.h>
1078 jermar 41
#include <proc/uarg.h>
703 jermar 42
#include <mm/as.h>
814 palkovsky 43
#include <mm/slab.h>
2183 jermar 44
#include <atomic.h>
1 jermar 45
#include <synch/spinlock.h>
2109 jermar 46
#include <synch/waitq.h>
1 jermar 47
#include <arch.h>
48
#include <panic.h>
1159 jermar 49
#include <adt/btree.h>
788 jermar 50
#include <adt/list.h>
955 palkovsky 51
#include <ipc/ipc.h>
1174 jermar 52
#include <security/cap.h>
955 palkovsky 53
#include <memstr.h>
1060 palkovsky 54
#include <print.h>
2000 decky 55
#include <lib/elf.h>
1579 jermar 56
#include <errno.h>
2050 decky 57
#include <func.h>
1288 jermar 58
#include <syscall/copy.h>
1597 palkovsky 59
#include <console/klog.h>
973 palkovsky 60
 
1170 vana 61
#ifndef LOADED_PROG_STACK_PAGES_NO
62
#define LOADED_PROG_STACK_PAGES_NO 1
63
#endif
1168 vana 64
 
1636 jermar 65
/** Spinlock protecting the tasks_btree B+tree. */
623 jermar 66
SPINLOCK_INITIALIZE(tasks_lock);
1636 jermar 67
 
68
/** B+tree of active tasks.
69
 *
2087 jermar 70
 * The task is guaranteed to exist after it was found in the tasks_btree as
71
 * long as:
1636 jermar 72
 * @li the tasks_lock is held,
2087 jermar 73
 * @li the task's lock is held when task's lock is acquired before releasing
74
 *     tasks_lock or
1880 jermar 75
 * @li the task's refcount is greater than 0
1636 jermar 76
 *
77
 */
1159 jermar 78
btree_t tasks_btree;
1636 jermar 79
 
1005 palkovsky 80
static task_id_t task_counter = 0;
1 jermar 81
 
1585 jermar 82
static void ktaskclnp(void *arg);
1661 jermar 83
static void ktaskgc(void *arg);
1579 jermar 84
 
107 decky 85
/** Initialize tasks
86
 *
87
 * Initialize kernel tasks support.
88
 *
89
 */
1 jermar 90
void task_init(void)
91
{
15 jermar 92
	TASK = NULL;
1159 jermar 93
	btree_create(&tasks_btree);
1 jermar 94
}
95
 
2227 decky 96
/** Kill all tasks except the current task.
97
 *
98
 */
99
void task_done(void)
100
{
101
	task_t *t;
102
	do { /* Repeat until there are any tasks except TASK */
103
 
104
		/* Messing with task structures, avoid deadlock */
105
		ipl_t ipl = interrupts_disable();
106
		spinlock_lock(&tasks_lock);
107
 
108
		t = NULL;
109
		link_t *cur;
110
		for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
111
			btree_node_t *node = list_get_instance(cur, btree_node_t, leaf_link);
112
 
113
			unsigned int i;
114
			for (i = 0; i < node->keys; i++) {
115
				if ((task_t *) node->value[i] != TASK) {
116
					t = (task_t *) node->value[i];
117
					break;
118
				}
119
			}
120
		}
121
 
122
		if (t != NULL) {
123
			task_id_t id = t->taskid;
124
 
125
			spinlock_unlock(&tasks_lock);
126
			interrupts_restore(ipl);
127
 
128
#ifdef CONFIG_DEBUG
129
			printf("Killing task %llu\n", id);
130
#endif			
131
			task_kill(id);
132
		} else {
133
			spinlock_unlock(&tasks_lock);
134
			interrupts_restore(ipl);
135
		}
136
 
137
	} while (t != NULL);
138
}
107 decky 139
 
140
/** Create new task
141
 *
142
 * Create new task with no threads.
143
 *
703 jermar 144
 * @param as Task's address space.
1062 jermar 145
 * @param name Symbolic name.
107 decky 146
 *
973 palkovsky 147
 * @return New task's structure
107 decky 148
 *
149
 */
1062 jermar 150
task_t *task_create(as_t *as, char *name)
1 jermar 151
{
413 jermar 152
	ipl_t ipl;
1 jermar 153
	task_t *ta;
1040 palkovsky 154
	int i;
1 jermar 155
 
822 palkovsky 156
	ta = (task_t *) malloc(sizeof(task_t), 0);
157
 
1185 jermar 158
	task_create_arch(ta);
159
 
822 palkovsky 160
	spinlock_initialize(&ta->lock, "task_ta_lock");
161
	list_initialize(&ta->th_head);
162
	ta->as = as;
1062 jermar 163
	ta->name = name;
1585 jermar 164
	ta->main_thread = NULL;
1579 jermar 165
	ta->refcount = 0;
1839 decky 166
	ta->context = CONTEXT;
1579 jermar 167
 
1174 jermar 168
	ta->capabilities = 0;
1579 jermar 169
	ta->accept_new_threads = true;
2039 decky 170
	ta->cycles = 0;
1040 palkovsky 171
 
955 palkovsky 172
	ipc_answerbox_init(&ta->answerbox);
1839 decky 173
	for (i = 0; i < IPC_MAX_PHONES; i++)
1040 palkovsky 174
		ipc_phone_init(&ta->phones[i]);
2087 jermar 175
	if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context,
176
	    ta->context)))
1040 palkovsky 177
		ipc_phone_connect(&ta->phones[0], ipc_phone_0);
998 palkovsky 178
	atomic_set(&ta->active_calls, 0);
1460 jermar 179
 
180
	mutex_initialize(&ta->futexes_lock);
181
	btree_create(&ta->futexes);
822 palkovsky 182
 
183
	ipl = interrupts_disable();
1468 jermar 184
 
185
	/*
186
	 * Increment address space reference count.
187
	 */
2183 jermar 188
	atomic_inc(&as->refcount);
1468 jermar 189
 
822 palkovsky 190
	spinlock_lock(&tasks_lock);
1005 palkovsky 191
 
192
	ta->taskid = ++task_counter;
1177 jermar 193
	btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
1005 palkovsky 194
 
822 palkovsky 195
	spinlock_unlock(&tasks_lock);
196
	interrupts_restore(ipl);
197
 
1 jermar 198
	return ta;
199
}
200
 
1579 jermar 201
/** Destroy task.
202
 *
203
 * @param t Task to be destroyed.
204
 */
205
void task_destroy(task_t *t)
206
{
1587 jermar 207
	task_destroy_arch(t);
208
	btree_destroy(&t->futexes);
209
 
2183 jermar 210
	if (atomic_predec(&t->as->refcount) == 0) 
1587 jermar 211
		as_destroy(t->as);
212
 
213
	free(t);
214
	TASK = NULL;
1579 jermar 215
}
216
 
973 palkovsky 217
/** Create new task with 1 thread and run it
218
 *
1248 jermar 219
 * @param program_addr Address of program executable image.
1062 jermar 220
 * @param name Program name. 
221
 *
1229 jermar 222
 * @return Task of the running program or NULL on error.
973 palkovsky 223
 */
1062 jermar 224
task_t * task_run_program(void *program_addr, char *name)
973 palkovsky 225
{
226
	as_t *as;
227
	as_area_t *a;
228
	int rc;
1585 jermar 229
	thread_t *t1, *t2;
973 palkovsky 230
	task_t *task;
1078 jermar 231
	uspace_arg_t *kernel_uarg;
973 palkovsky 232
 
233
	as = as_create(0);
1115 jermar 234
	ASSERT(as);
973 palkovsky 235
 
1025 palkovsky 236
	rc = elf_load((elf_header_t *) program_addr, as);
973 palkovsky 237
	if (rc != EE_OK) {
1468 jermar 238
		as_destroy(as);
973 palkovsky 239
		return NULL;
240
	} 
241
 
1078 jermar 242
	kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
2087 jermar 243
	kernel_uarg->uspace_entry =
244
	    (void *) ((elf_header_t *) program_addr)->e_entry;
1078 jermar 245
	kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
246
	kernel_uarg->uspace_thread_function = NULL;
247
	kernel_uarg->uspace_thread_arg = NULL;
248
	kernel_uarg->uspace_uarg = NULL;
1066 jermar 249
 
1062 jermar 250
	task = task_create(as, name);
1115 jermar 251
	ASSERT(task);
252
 
973 palkovsky 253
	/*
254
	 * Create the data as_area.
255
	 */
2087 jermar 256
	a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
257
	    LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
258
	    AS_AREA_ATTR_NONE, &anon_backend, NULL);
1115 jermar 259
 
1585 jermar 260
	/*
261
	 * Create the main thread.
262
	 */
2087 jermar 263
	t1 = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE,
264
	    "uinit", false);
1585 jermar 265
	ASSERT(t1);
973 palkovsky 266
 
1585 jermar 267
	/*
268
	 * Create killer thread for the new task.
269
	 */
2042 decky 270
	t2 = thread_create(ktaskgc, t1, task, 0, "ktaskgc", true);
1585 jermar 271
	ASSERT(t2);
272
	thread_ready(t2);
273
 
274
	thread_ready(t1);
275
 
973 palkovsky 276
	return task;
277
}
1060 palkovsky 278
 
1176 jermar 279
/** Syscall for reading task ID from userspace.
280
 *
2087 jermar 281
 * @param uspace_task_id Userspace address of 8-byte buffer where to store
282
 * current task ID.
1176 jermar 283
 *
1288 jermar 284
 * @return 0 on success or an error code from @ref errno.h.
1176 jermar 285
 */
1780 jermar 286
unative_t sys_task_get_id(task_id_t *uspace_task_id)
1176 jermar 287
{
288
	/*
289
	 * No need to acquire lock on TASK because taskid
290
	 * remains constant for the lifespan of the task.
291
	 */
2087 jermar 292
	return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
293
	    sizeof(TASK->taskid));
1176 jermar 294
}
295
 
1178 jermar 296
/** Find task structure corresponding to task ID.
297
 *
298
 * The tasks_lock must be already held by the caller of this function
299
 * and interrupts must be disabled.
300
 *
301
 * @param id Task ID.
302
 *
303
 * @return Task structure address or NULL if there is no such task ID.
304
 */
305
task_t *task_find_by_id(task_id_t id)
306
{
307
	btree_node_t *leaf;
308
 
309
	return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
310
}
311
 
2039 decky 312
/** Get accounting data of given task.
313
 *
2048 jermar 314
 * Note that task lock of 't' must be already held and
2039 decky 315
 * interrupts must be already disabled.
316
 *
317
 * @param t Pointer to thread.
318
 *
319
 */
320
uint64_t task_get_accounting(task_t *t)
321
{
322
	/* Accumulated value of task */
323
	uint64_t ret = t->cycles;
324
 
325
	/* Current values of threads */
326
	link_t *cur;
327
	for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
328
		thread_t *thr = list_get_instance(cur, thread_t, th_link);
329
 
330
		spinlock_lock(&thr->lock);
2042 decky 331
		/* Process only counted threads */
332
		if (!thr->uncounted) {
2087 jermar 333
			if (thr == THREAD) {
334
				/* Update accounting of current thread */
335
				thread_update_accounting();
336
			} 
2042 decky 337
			ret += thr->cycles;
338
		}
2039 decky 339
		spinlock_unlock(&thr->lock);
340
	}
341
 
342
	return ret;
343
}
344
 
1579 jermar 345
/** Kill task.
346
 *
347
 * @param id ID of the task to be killed.
348
 *
349
 * @return 0 on success or an error code from errno.h
350
 */
351
int task_kill(task_id_t id)
352
{
353
	ipl_t ipl;
354
	task_t *ta;
355
	thread_t *t;
356
	link_t *cur;
1600 jermar 357
 
358
	if (id == 1)
359
		return EPERM;
1579 jermar 360
 
361
	ipl = interrupts_disable();
362
	spinlock_lock(&tasks_lock);
363
 
364
	if (!(ta = task_find_by_id(id))) {
365
		spinlock_unlock(&tasks_lock);
366
		interrupts_restore(ipl);
367
		return ENOENT;
368
	}
1588 jermar 369
 
1579 jermar 370
	spinlock_lock(&ta->lock);
371
	ta->refcount++;
372
	spinlock_unlock(&ta->lock);
1587 jermar 373
 
1588 jermar 374
	btree_remove(&tasks_btree, ta->taskid, NULL);
1587 jermar 375
	spinlock_unlock(&tasks_lock);
1579 jermar 376
 
2042 decky 377
	t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp", true);
1579 jermar 378
 
379
	spinlock_lock(&ta->lock);
1580 jermar 380
	ta->accept_new_threads = false;
1579 jermar 381
	ta->refcount--;
1585 jermar 382
 
383
	/*
1687 jermar 384
	 * Interrupt all threads except ktaskclnp.
1585 jermar 385
	 */	
1579 jermar 386
	for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
387
		thread_t *thr;
388
		bool  sleeping = false;
389
 
390
		thr = list_get_instance(cur, thread_t, th_link);
391
		if (thr == t)
392
			continue;
393
 
394
		spinlock_lock(&thr->lock);
395
		thr->interrupted = true;
396
		if (thr->state == Sleeping)
397
			sleeping = true;
398
		spinlock_unlock(&thr->lock);
399
 
400
		if (sleeping)
2109 jermar 401
			waitq_interrupt_sleep(thr);
1579 jermar 402
	}
403
 
1580 jermar 404
	spinlock_unlock(&ta->lock);
405
	interrupts_restore(ipl);
1579 jermar 406
 
1580 jermar 407
	if (t)
408
		thread_ready(t);
409
 
1579 jermar 410
	return 0;
411
}
412
 
1060 palkovsky 413
/** Print task list */
414
void task_print_list(void)
415
{
416
	link_t *cur;
417
	ipl_t ipl;
418
 
2227 decky 419
	/* Messing with task structures, avoid deadlock */
1060 palkovsky 420
	ipl = interrupts_disable();
421
	spinlock_lock(&tasks_lock);
2035 decky 422
 
2087 jermar 423
	printf("taskid name       ctx address    as         cycles     threads "
424
	    "calls  callee\n");
425
	printf("------ ---------- --- ---------- ---------- ---------- ------- "	    "------ ------>\n");
1060 palkovsky 426
 
2087 jermar 427
	for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head;
428
	    cur = cur->next) {
1159 jermar 429
		btree_node_t *node;
2118 decky 430
		unsigned int i;
1159 jermar 431
 
432
		node = list_get_instance(cur, btree_node_t, leaf_link);
433
		for (i = 0; i < node->keys; i++) {
434
			task_t *t;
435
			int j;
436
 
437
			t = (task_t *) node->value[i];
438
 
439
			spinlock_lock(&t->lock);
2039 decky 440
 
2050 decky 441
			uint64_t cycles;
2039 decky 442
			char suffix;
2050 decky 443
			order(task_get_accounting(t), &cycles, &suffix);
2039 decky 444
 
2216 decky 445
			printf("%-6llu %-10s %-3ld %#10zx %#10zx %9llu%c %7zd "
2087 jermar 446
			    "%6zd", t->taskid, t->name, t->context, t, t->as,
447
			    cycles, suffix, t->refcount,
448
			    atomic_get(&t->active_calls));
2035 decky 449
			for (j = 0; j < IPC_MAX_PHONES; j++) {
1159 jermar 450
				if (t->phones[j].callee)
2087 jermar 451
					printf(" %zd:%#zx", j,
452
					    t->phones[j].callee);
1159 jermar 453
			}
454
			printf("\n");
2039 decky 455
 
1159 jermar 456
			spinlock_unlock(&t->lock);
1060 palkovsky 457
		}
458
	}
459
 
460
	spinlock_unlock(&tasks_lock);
461
	interrupts_restore(ipl);
462
}
1579 jermar 463
 
1585 jermar 464
/** Kernel thread used to cleanup the task after it is killed. */
1580 jermar 465
void ktaskclnp(void *arg)
1579 jermar 466
{
1580 jermar 467
	ipl_t ipl;
1585 jermar 468
	thread_t *t = NULL, *main_thread;
1580 jermar 469
	link_t *cur;
1661 jermar 470
	bool again;
1580 jermar 471
 
472
	thread_detach(THREAD);
473
 
474
loop:
475
	ipl = interrupts_disable();
476
	spinlock_lock(&TASK->lock);
477
 
1585 jermar 478
	main_thread = TASK->main_thread;
479
 
1579 jermar 480
	/*
1580 jermar 481
	 * Find a thread to join.
482
	 */
1661 jermar 483
	again = false;
1580 jermar 484
	for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
485
		t = list_get_instance(cur, thread_t, th_link);
1661 jermar 486
 
487
		spinlock_lock(&t->lock);
488
		if (t == THREAD) {
489
			spinlock_unlock(&t->lock);
1580 jermar 490
			continue;
1661 jermar 491
		} else if (t == main_thread) {
492
			spinlock_unlock(&t->lock);
1585 jermar 493
			continue;
1661 jermar 494
		} else if (t->join_type != None) {
495
			spinlock_unlock(&t->lock);
496
			again = true;
497
			continue;
498
		} else {
499
			t->join_type = TaskClnp;
500
			spinlock_unlock(&t->lock);
501
			again = false;
1580 jermar 502
			break;
1661 jermar 503
		}
1580 jermar 504
	}
505
 
506
	spinlock_unlock(&TASK->lock);
507
	interrupts_restore(ipl);
508
 
1661 jermar 509
	if (again) {
510
		/*
511
		 * Other cleanup (e.g. ktaskgc) is in progress.
512
		 */
513
		scheduler();
514
		goto loop;
515
	}
516
 
1580 jermar 517
	if (t != THREAD) {
2087 jermar 518
		ASSERT(t != main_thread);	/* uninit is joined and detached
519
						 * in ktaskgc */
1580 jermar 520
		thread_join(t);
521
		thread_detach(t);
2087 jermar 522
		goto loop;			/* go for another thread */
1580 jermar 523
	}
524
 
525
	/*
526
	 * Now there are no other threads in this task
527
	 * and no new threads can be created.
528
	 */
1880 jermar 529
 
1583 jermar 530
	ipc_cleanup();
531
	futex_cleanup();
2216 decky 532
	klog_printf("Cleanup of task %llu completed.", TASK->taskid);
1579 jermar 533
}
1585 jermar 534
 
1684 jermar 535
/** Kernel thread used to kill the userspace task when its main thread exits.
1585 jermar 536
 *
537
 * This thread waits until the main userspace thread (i.e. uninit) exits.
1661 jermar 538
 * When this happens, the task is killed. In the meantime, exited threads
539
 * are garbage collected.
1585 jermar 540
 *
541
 * @param arg Pointer to the thread structure of the task's main thread.
542
 */
1661 jermar 543
void ktaskgc(void *arg)
1585 jermar 544
{
545
	thread_t *t = (thread_t *) arg;
1661 jermar 546
loop:	
1585 jermar 547
	/*
548
	 * Userspace threads cannot detach themselves,
549
	 * therefore the thread pointer is guaranteed to be valid.
550
	 */
2087 jermar 551
	if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) ==
552
	    ESYNCH_TIMEOUT) {	/* sleep uninterruptibly here! */
1661 jermar 553
		ipl_t ipl;
554
		link_t *cur;
555
		thread_t *thr = NULL;
556
 
557
		/*
2087 jermar 558
		 * The join timed out. Try to do some garbage collection of
559
		 * Undead threads.
1661 jermar 560
		 */
561
more_gc:		
562
		ipl = interrupts_disable();
563
		spinlock_lock(&TASK->lock);
564
 
2087 jermar 565
		for (cur = TASK->th_head.next; cur != &TASK->th_head;
566
		    cur = cur->next) {
1661 jermar 567
			thr = list_get_instance(cur, thread_t, th_link);
568
			spinlock_lock(&thr->lock);
2087 jermar 569
			if (thr != t && thr->state == Undead &&
570
			    thr->join_type == None) {
1661 jermar 571
				thr->join_type = TaskGC;
572
				spinlock_unlock(&thr->lock);
573
				break;
574
			}
575
			spinlock_unlock(&thr->lock);
576
			thr = NULL;
577
		}
578
		spinlock_unlock(&TASK->lock);
1676 jermar 579
		interrupts_restore(ipl);
1661 jermar 580
 
581
		if (thr) {
582
			thread_join(thr);
583
			thread_detach(thr);
584
			scheduler();
585
			goto more_gc;
586
		}
587
 
588
		goto loop;
589
	}
1585 jermar 590
	thread_detach(t);
591
	task_kill(TASK->taskid);
592
}
1702 cejka 593
 
1757 jermar 594
/** @}
1702 cejka 595
 */