summaryrefslogtreecommitdiff
path: root/kernel/scheduler.c
blob: b36782573e39a3e1a56681c67def036f9e5f71ee (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// http://hosted.cjmovie.net/TutMultitask.htm
//
//

#include "kernel.h"
#include "mem.h"
#include "asm/x86.h"

#include "vmem.h"
#include "syscalls.h"
#include "fs/fs.h"
#include "fs/ext2.h"

static volatile int volatile current_task=-1;

static volatile struct task_list_struct
{
    volatile int parent;
    volatile bool active;
    volatile uint32_t esp; // stack pointer of the task;
    volatile pdirectory *vmem; // number of virtual memory table to switch to
    volatile bool waiting; 
    volatile bool skipwait;
    volatile uint32_t brk;
    volatile uint32_t esp0;

    volatile bool syscall; // waiting for syscall to be processed.
    volatile uint32_t eax;
    volatile uint32_t ebx;
    volatile uint32_t ecx;
    volatile uint32_t edx;

    
}volatile task_list[MAX_TASKS];

volatile 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  = kballoc(4)+4*4096;
            task_list[i].esp0 = kballoc(4)+4*4096;
	    task_list[i].active=true;
	    task_list[i].waiting=false;
	    task_list[i].skipwait=false;
	    task_list[i].brk=task_list[current_task].brk;

            uint32_t *addi2=(uint32_t *)esp;
	    addi2+=14;

	    for(int x=0;x<15;x++)
	    {
		task_list[i].esp-=4;
		uint32_t *addi1=(uint32_t *)task_list[i].esp;
		*addi1=*addi2;
		addi2--;
	    }
	    return i;
	}
    }

    kpanic("out of task slots!");
}

void task_wake_syscall_worker()
{
    task_list[2].waiting=false; // todo: context switch immiditly?
}

void task_syscall_worker()
{
    klog("checking if any pending syscalls.");

    for(int i=0;i<MAX_TASKS;i++)
    {
	if(task_list[i].syscall)
	{
	    klog("task %d waiting on syscall %d. processing...",i,task_list[i].eax);

	    task_list[2].vmem=task_list[i].vmem; // switch syscall worker to pagedir of calling userprog
	    x86_set_page_directory(task_list[2].vmem);
	    syscall_generic(task_list[i].eax,
			task_list[i].ebx,
			task_list[i].ecx,
			task_list[i].edx);

	    task_list[i].syscall=false;
	}
    }

    task_list[current_task].waiting=true;
    __asm__("hlt"); //TODO: force task switch here... via syscall?
}

//
// REMEMBER WE ARE INSIDE AN INTERRUPT HERE - DON'T WASTE TIME!
//
// oldesp - is the adress of the stack pointer when pit_interrupt_handler was entered.
// registers have been pushed with pusha to this old stack.
//
// stack pointer was moved to the 16kb stack we have from multiboot.s
//
// we need to return a NEW stack pointer where popa will get the registers the new task requires
//
static int first=1;
volatile uint32_t my_scheduler(uint32_t oldesp)
{

    if(!first)
    {
	task_list[current_task].esp=oldesp;
    }
	first=0;

    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 && !task_list[pid].waiting && !task_list[pid].syscall)
	{
	    if(current_task!=pid)
		    klog("switch from %d to %d", current_task, pid);

	    current_task=pid;
	    install_tss(task_list[pid].esp0);

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

    }

    kpanic("nothing to schedule!");
}

// this gets called by our clock interrupt regularly!
volatile uint32_t task_switch_next(uint32_t oldesp)
{
    // check if multitasking has been started
    if(current_task<0)return oldesp; 
    return my_scheduler(oldesp);
}

volatile uint32_t task_syscall(uint32_t eax,uint32_t ebx, uint32_t ecx, uint32_t edx,uint32_t oldesp)
{
    task_list[current_task].syscall=true;
    task_list[current_task].eax=eax;
    task_list[current_task].ebx=ebx;
    task_list[current_task].ecx=ecx;
    task_list[current_task].edx=edx;
    return my_scheduler(oldesp);
}

//TODO: free vmem too!
//TODO: notify waiting parent when child finished;
volatile uint32_t task_exit(uint32_t oldesp)
{ 

    task_list[current_task].active=false;
    int parent_pid=task_list[current_task].parent;

    klog("[%d] exit ", current_task);

    if(task_list[parent_pid].active)
    {
	if(task_list[parent_pid].waiting)
	{
    
	   klog("[%d] wake up", parent_pid);
	   task_list[parent_pid].waiting=false;
	}
	else
	{
	   klog("[%d] skipwait", parent_pid);
	   task_list[parent_pid].skipwait=true;
	}
	
    }

    vmem_free_dir(task_list[current_task].vmem);

    return my_scheduler(oldesp);

}

volatile uint32_t task_wait(uint32_t oldesp)
{ 
    klog("[%d] wait", current_task);
    if(task_list[current_task].skipwait)
    {
	task_list[current_task].skipwait=false;
    }
    else
    {
	task_list[current_task].waiting=true;
    }
    return my_scheduler(oldesp);
}

volatile uint32_t task_fork(uint32_t oldesp)
{ 
   int pid=add_task(oldesp,vmem_new_space_dir(task_list[current_task].vmem));
   klog("[%d] forked -> [%d] (free blocks remaining: %d )", current_task, pid,mem_get_free_blocks_count());
   return pid;
}

// init task (root of all other tasks / processes) //
volatile void scheduler_init(pdirectory *dir)
{
    for(int i=0;i<MAX_TASKS;i++)
    {
	task_list[i].active=false;
    }

    current_task=0;
    
    // this is our main user task on slot 0 
    task_list[0].parent=0;
    task_list[0].active=true;
    task_list[0].waiting=false;
    task_list[0].vmem=dir;
    task_list[0].esp = kballoc(4)+4*4096;
    task_list[0].esp0 = kballoc(4)+4*4096;

    task_list[1].parent=0;
    task_list[1].active=true;
    task_list[1].waiting=false;
    task_list[1].vmem=dir;
    task_list[1].esp = kballoc(4)+4*4096; 
    task_list[1].esp0 = 0; // not needed by kernel space tasks

    task_list[2].parent=0;
    task_list[2].active=true;
    task_list[2].waiting=false;
    task_list[2].vmem=dir;
    task_list[2].esp = kballoc(4)+4*4096; 
    task_list[2].esp0 = 0; // not needed by kernel space tasks

    task_pusha(task_list[2].esp);
    task_pusha(task_list[1].esp);
    task_pusha(task_list[0].esp);

    // finally enable interrrupts so the scheduler is called (by timer)
    x86_sti(); 

    // loop until scheduler kicks in and reschedules us...
    while(1);
}

volatile int task_get_current_pid()
{
    return current_task;
}

volatile uint32_t task_get_brk()
{
    return task_list[current_task].brk;
}

volatile void task_set_brk(uint32_t brk)
{
    task_list[current_task].brk=brk;
}