summaryrefslogtreecommitdiff
path: root/kernel/task.c
blob: 1780b77b9e97f4079ea733fcc5f070ec4466dc11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// http://hosted.cjmovie.net/TutMultitask.htm
//
//
#include "kernel.h"
#include "lib/logger/log.h"	// logger facilities
#include "lib/buffer/ringbuffer.h"	
#include "mem.h"
#include "timer.h"
#include "console.h"
#include "x86.h"

#include "syscalls.h"
#include "fs/fs.h"
#include "fs/ext2.h"
#define FOOLOS_MODULE_NAME "task"


#define MAX_TASKS 10

static volatile int current_task=-2;

static volatile struct task_list_struct
{
    int parent;
    bool active;
    uint32_t esp; // stack pointer of the task;
    uint32_t vmem; // number of virtual memory table to switch to
    
}volatile task_list[MAX_TASKS];

int add_task(uint32_t esp, uint32_t vmem)
{

    for(int i=0;i<MAX_TASKS;i++)
    {
	if(task_list[i].active!=true)
	{

	    task_list[i].parent=current_task;
	    task_list[i].vmem=vmem;
	    task_list[i].esp=esp;
	    task_list[i].active=true;

	    return i;
	}
    }

    panic(FOOLOS_MODULE_NAME,"out of task slots!");
}

// this gets called by our clock interrupt regularly!
uint32_t task_switch_next(uint32_t oldesp)
{
    timer_tick();

    if(current_task==-2)return oldesp;
    
    task_list[current_task].esp=oldesp;
    
    for(int i=0;i<MAX_TASKS;i++)
    {
	int pid=(current_task+1+i)%MAX_TASKS; // schedule round robin style

	if(task_list[pid].active)
	{
	  //  log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"switch from %d to pid: %d (0x%08X) vmem: %d ", current_task,pid,task_list[pid].esp,task_list[pid].vmem);
	    current_task=pid;

	    vmem_set_dir(task_list[pid].vmem);
	    return task_list[pid].esp;
	}

    }

    return oldesp;

}


uint32_t task_fork(uint32_t oldesp)
{ 
    return add_task(oldesp,vmem_new_space_dir());
}

// init task (root of all other tasks / processes) //
void task_init()
{
    // this is our main task on slot 0 
    task_list[0].active=true;
    task_list[0].vmem=0;
    task_list[0].esp = 0; // will be set by next task_switch_next() call.
    current_task=0;

    static char *argv[]={"/bin/foolshell",NULL};
    static char *env[]={"PATH=/bin","PWD=/home/miguel","PS1=$ ",NULL};
    syscall_execve("/bin/init",argv,env); 
}


int task_get_current_pid()
{
    return current_task;
}