Code: Select all
Code: Select all
typedef struct task
{
u32int pid; // Process ID
u32int stack; // Task's stack
u32int state : 1; // Is task alive?
u8int name[32]; // Task's name
void (*thread)(); // Actual thread entry
}task_t;
task_t tasks[2]; // Our tasklist
int pid = 0; // Current Process ID
int t=-1;
void CreateTask(int id, unsigned char name[32], void (*thread)())
{
asm volatile("cli");
unsigned int *stack;
task_t *task = (task_t*) kmalloc(sizeof(task_t));
task->stack = kmalloc_a(0x4000) + 0x4000; // Allocate 4 kilobytes of space
stack = (unsigned int*)task->stack;
// Expand down stack
// processor data
*--stack = 0x202; // EFLAGS
*--stack = 0x08; // CS
*--stack = (unsigned int) thread; // EIP
// pusha
*--stack = 0; // EDI
*--stack = 0; // ESI
*--stack = 0; // EBP
*--stack = 0; // NULL
*--stack = 0; // EBX
*--stack = 0; // EDX
*--stack = 0; // ECX
*--stack = 0; // EAX
// data segments
*--stack = 0x10; // DS
*--stack = 0x10; // ES
*--stack = 0x10; // FS
*--stack = 0x10; // GS
task->state = 1;
task->stack = (unsigned int) stack;
task->thread = thread;
strncpy(task->name, name, strlen(name));
tasks[id] = *task;
t++;
asm volatile("sti");
}
unsigned int sched(unsigned int context) // Our assembly code provides us with the context
{
tasks[pid].stack = context; // save the old context into current task
switch(pid)
{
case 0:
if(t>0)
pid = 1;
break;
case 1:
pid = 0;
break;
}
return tasks[pid].stack; // Return new task's context.
}
this is the assmbly file..
Code: Select all
.global _sched
.global _irq0
_irq0: #timer irq
pusha
push %ds # push ds onto stack
push %es # push es
push %fs # push fs
push %gs # push gs
mov $0x10,%eax # save selector
mov %eax,%ds
mov %eax,%es
mov %eax,%fs
mov %eax,%gs
mov %esp,%eax # stack => eax
push %eax # push eax (esp)
call _sched # sched(eax) returns stack of next task
mov %eax,%esp # return value => stack
call _timer_irq
pop %gs # pop gs off task stack
pop %fs # pop fs
pop %es # pop es
pop %ds # pop ds
popa # pop general registers
iretwhat is the problem


