metalsvm/kernel/tasks.c
stefan 0aea73bdc7 - add a simple memory mangement unit
- use a bitmask to mark used pages (see lecture notes "BS")


git-svn-id: http://svn.lfbs.rwth-aachen.de/svn/scc/trunk/MetalSVM@7 315a16e6-25f9-4109-90ae-ca3045a26c18
2010-08-02 07:43:56 +00:00

146 lines
3.5 KiB
C

/*
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
* RWTH Aachen University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of MetalSVM.
*/
#include <metalsvm/stdio.h>
#include <metalsvm/stdlib.h>
#include <metalsvm/string.h>
#include <metalsvm/irqflags.h>
#include <metalsvm/mmu.h>
#include <metalsvm/tasks.h>
#include <metalsvm/processor.h>
task_t* current_task = NULL;
static task_t task_table[MAX_TASKS];
int multitasking_init(void) {
memset(task_table, 0, sizeof(task_t)*MAX_TASKS);
task_table[0].id = 0;
task_table[0].mm.usage = 0;
task_table[0].stack = NULL;
task_table[0].stack_size = 8192;
task_table[0].status = TASK_RUNNING;
task_table[0].idle = 0;
current_task = task_table;
return 0;
}
void NORETURN leave_task(void) {
kprintf("Terminate task: %u\n", current_task->id);
current_task->status = TASK_FINISHED;
schedule();
kputs("Kernel panic: scheduler found no valid task\n");
while(1) {
NOP8;
}
}
void NORETURN abort(void) {
kprintf("Abbort task: %u\n", current_task->id);
current_task->status = TASK_FINISHED;
schedule();
kputs("Kernel panic: scheduler found no valid task\n");
while(1) {
NOP8;
}
}
int create_kernel_task(tid_t* id, entry_point_t ep, void* arg, size_t stack_size)
{
int ret = -1;
unsigned int i;
if (BUILTIN_EXPECT(!ep, 0))
return -1;
if (!stack_size)
stack_size = DEFAULT_STACK_SIZE;
irq_disable();
for(i=0; i<MAX_TASKS; i++) {
if (task_table[i].status == TASK_INVALID) {
if (task_table[i].stack)
kfree(task_table[i].stack, task_table[i].stack_size);
if (task_table[i].mm.usage) {
kprintf("Task %d has a memory leax (%d byte)\n", task_table[i].id, task_table[i].mm.usage);
task_table[i].mm.usage = 0;
}
task_table[i].stack = create_stack(task_table+i, stack_size);
if (!task_table[i].stack)
break;
task_table[i].stack_size = stack_size;
task_table[i].top = task_table[i].stack + stack_size - sizeof(size_t);
task_table[i].ip = 0;
task_table[i].id = i;
task_table[i].status = TASK_READY;
task_table[i].idle = 0;
if (id)
*id = i;
ret = create_default_frame(task_table+i, ep, arg);
break;
}
}
irq_enable();
return ret;
}
int join_kernel_task(tid_t id)
{
return -1;
}
task_t* get_new_task(void)
{
unsigned int i, new_id;
/* signalize that this task could be reuse */
if (current_task->status == TASK_FINISHED)
current_task = TASK_INVALID;
for(i=1; i <= MAX_TASKS; i++) {
new_id = (current_task->id + i) % MAX_TASKS;
if (!task_table[new_id].idle && task_table[new_id].status == TASK_READY) {
if (current_task->status == TASK_RUNNING)
current_task->status = TASK_READY;
task_table[new_id].status = TASK_RUNNING;
return task_table+new_id;
}
}
if (!(current_task->idle) && (current_task->status == TASK_RUNNING))
return current_task;
/*
* we switch to the idle task (id=0), if the current task terminates
* and no other is ready
*/
task_table[0].status = TASK_RUNNING;
return task_table+0;
}