Skip to content

Commit

Permalink
more scheduler crap
Browse files Browse the repository at this point in the history
preemptive scheduling is hard, so i went back to cooperative scheduling.
  • Loading branch information
tone121 committed Feb 20, 2018
1 parent 3fb04f8 commit 5663c3d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 66 deletions.
5 changes: 1 addition & 4 deletions include/sched/sched.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#ifndef SCHED_H
#define SCHED_H

#include <cpu/idt.h>
#include <cpu/pic.h>
#include <cpu/pit.h>
#include <tty/panic.h>
#include <sched/process.h>

void sched_init(process_t *proc);
void sched_add(process_t *proc);
void sched_remove(process_t *proc);
unsigned long sched_pid_alloc(void);
unsigned long sched_step(void);
void sched_step(void);

#endif
8 changes: 4 additions & 4 deletions src/boot/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ void init_arch(struct multiboot_info *mb)

void idle(void)
{
for (;;) {
asm ("nop");
}
tty_printf("a");
asm ("nop");
}

void init(struct multiboot_info *mb)
Expand All @@ -48,7 +47,8 @@ void init(struct multiboot_info *mb)

sched_init(process_init(idle, "idle"));

idle();
for (;;)
sched_step();

return;
}
57 changes: 7 additions & 50 deletions src/sched/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,13 @@ extern void sched_jump(unsigned long eip);
static process_t *curr = NULL;
static unsigned long pid = 0;

void sched_int(void)
{
asm ("cli");

asm ("out %%al, %%dx" : : "d"(0x20), "a"(0x20));

// save registers
asm ("mov %%ebx, %0" : "=r"(curr->regs.ebx));
asm ("mov %%ecx, %0" : "=r"(curr->regs.ecx));
asm ("mov %%edx, %0" : "=r"(curr->regs.edx));

asm ("mov %%esi, %0" : "=r"(curr->regs.esi));
asm ("mov %%edi, %0" : "=r"(curr->regs.edi));

asm ("mov %%esp, %0" : "=r"(curr->regs.esp));
asm ("mov %%ebp, %0" : "=r"(curr->regs.ebp));

asm ("mov 12(%%esp), %0" : "=r"(curr->regs.eip));

if (sched_step() == 1) {

// restore registers

asm ("mov %0, %%ebx" : "=r"(curr->regs.ebx));
asm ("mov %0, %%ecx" : "=r"(curr->regs.ecx));
asm ("mov %0, %%edx" : "=r"(curr->regs.edx));

asm ("mov %0, %%esi" : "=r"(curr->regs.esi));
asm ("mov %0, %%edi" : "=r"(curr->regs.edi));

// asm ("mov %0, %%esp" : "=r"(curr->regs.esp));
// asm ("mov %0, %%ebp" : "=r"(curr->regs.ebp));

sched_jump(curr->regs.eip);
}

asm ("sti; leave; iret");
}

void sched_init(process_t *proc)
{
if (proc) {
curr = proc;

curr->prev = curr;
curr->next = curr;

idt_set_entry(32, (unsigned long)sched_int);
pit_set_phase(PIT_HZ / 1000);
pic_enable_irq(0);
}
}

Expand All @@ -81,29 +38,29 @@ unsigned long sched_pid_alloc(void)
return pid++;
}

unsigned long sched_step(void)
void sched_step(void)
{
unsigned long schedule = 0;

if (curr && curr->next) {
switch (curr->state) {
case PROCESS_RUNNING:
PANIC("trying to schedule a running process!");
case PROCESS_RUNABLE:
schedule = 1;
curr->state = PROCESS_RUNNING;
sched_jump(curr->regs.eip);
curr->state = PROCESS_RUNABLE;
break;
case PROCESS_SLEEPING:
// do nothing and let it sleep
break;
case PROCESS_ZOMBIE:
curr = curr->next;
sched_remove(curr->prev);
return 0;
return;
default:
PANIC("cannot handle unknown process state!");
}
curr = curr->next;
} else {
PANIC("no more processes to schedule!");
}

return schedule;
}
10 changes: 2 additions & 8 deletions src/sched/switch.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
; void sched_jump(unsigned long eip);
global sched_jump
sched_jump:
pop eax
pushfd
or dword [esp], 0x200
push 0x8
push eax

iret
mov eax, [esp + 4]
jmp eax

0 comments on commit 5663c3d

Please # to comment.