Subversion Repositories HelenOS

Rev

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