Scheduler Fixed
This commit is contained in:
parent
faa833ee22
commit
ab9873fb6f
|
|
@ -351,6 +351,7 @@ _irq_common_stub:
|
||||||
pop ds
|
pop ds
|
||||||
popad
|
popad
|
||||||
add esp, 8
|
add esp, 8
|
||||||
|
; sti
|
||||||
iret
|
iret
|
||||||
|
|
||||||
global panic_hold
|
global panic_hold
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,18 @@
|
||||||
void test(){
|
void test(){
|
||||||
printf("Hello from thread!\n");
|
printf("Hello from thread!\n");
|
||||||
// thread_exit(1);
|
// thread_exit(1);
|
||||||
|
// enable_interrupts();
|
||||||
|
// asm volatile ("jmp .");
|
||||||
|
for(;;);
|
||||||
}
|
}
|
||||||
void test1(){
|
void test1(){
|
||||||
printf("Hello from thread1!\n");
|
// printf("Hello from thread1!\n");
|
||||||
uint32_t exit_code = -1;
|
uint16_t *test = (void *)0xb8000;
|
||||||
uint32_t pid = thread_start(test);
|
*test = 0x0f41;
|
||||||
// for(;;);
|
for(;;);
|
||||||
printf("Start");
|
// asm
|
||||||
// printf("PID: %d", pid);
|
}
|
||||||
|
void dead_proc(){
|
||||||
for(;;);
|
for(;;);
|
||||||
}
|
}
|
||||||
extern void kmain(kernel_info_t *kernel_info){
|
extern void kmain(kernel_info_t *kernel_info){
|
||||||
|
|
@ -31,6 +35,8 @@ extern void kmain(kernel_info_t *kernel_info){
|
||||||
//TODO: Start Initial Process
|
//TODO: Start Initial Process
|
||||||
uint32_t exit_code = -1;
|
uint32_t exit_code = -1;
|
||||||
init_scheduler();
|
init_scheduler();
|
||||||
|
thread_start(dead_proc);
|
||||||
|
thread_start(test);
|
||||||
thread_start(test1);
|
thread_start(test1);
|
||||||
enable_interrupts();
|
enable_interrupts();
|
||||||
for(;;);
|
for(;;);
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,21 @@
|
||||||
#include "../shared/kstdlib.h"
|
#include "../shared/kstdlib.h"
|
||||||
#include "../shared/memory.h"
|
#include "../shared/memory.h"
|
||||||
|
|
||||||
process_t volatile processes[0x10000];
|
volatile process_t *processes;
|
||||||
uint32_t volatile process_queue[0x10000];
|
volatile uint32_t process_queue[PROCESS_COUNT];
|
||||||
uint32_t volatile active_processes;
|
uint32_t volatile active_processes;
|
||||||
uint32_t volatile current_pid;
|
uint32_t volatile current_pid;
|
||||||
uint32_t volatile queue_length;
|
uint32_t volatile queue_length;
|
||||||
uint32_t volatile current_queue_index;
|
uint32_t volatile current_queue_index;
|
||||||
void init_scheduler(){
|
void init_scheduler(){
|
||||||
|
// printf("?");
|
||||||
|
// process_queue = kmalloc((PROCESS_COUNT * sizeof(uint32_t)) / 4096);
|
||||||
|
processes = kmalloc((PROCESS_COUNT * sizeof(process_t)) / 4096);
|
||||||
|
// process_queue = kmalloc(1);
|
||||||
|
// printf("%x, %x\n", (PROCESS_COUNT * sizeof(process_t)) / 4096, (PROCESS_COUNT * sizeof(uint32_t)) / 4096);
|
||||||
|
// printf("%x, %x\n", processes, process_queue);
|
||||||
for(uint32_t i = 0; i < 0x10000; i++){
|
for(uint32_t i = 0; i < 0x10000; i++){
|
||||||
|
// printf("Index: %x, %d\n", process_queue, sizeof(process_t));
|
||||||
processes[i].flags.present = 0;
|
processes[i].flags.present = 0;
|
||||||
process_queue[i] = 0;
|
process_queue[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -19,30 +26,48 @@ void init_scheduler(){
|
||||||
queue_length = 0;
|
queue_length = 0;
|
||||||
current_queue_index = 0;
|
current_queue_index = 0;
|
||||||
active_processes = 0;
|
active_processes = 0;
|
||||||
|
// printf("Init :3");
|
||||||
install_irq_handler(schedule, 0);
|
install_irq_handler(schedule, 0);
|
||||||
// printf("Installing scheduler");
|
// printf("Installing scheduler");
|
||||||
}
|
}
|
||||||
|
|
||||||
cpu_registers_t *schedule(cpu_registers_t *regs){
|
cpu_registers_t *schedule(cpu_registers_t *regs){
|
||||||
if(queue_length == 0){
|
// if(queue_length == 0){
|
||||||
return regs;
|
|
||||||
}
|
|
||||||
uint32_t i = current_queue_index;
|
|
||||||
for(; i < queue_length; i++){
|
|
||||||
if(processes[process_queue[i%queue_length]].flags.blocked) continue;
|
|
||||||
else break;
|
|
||||||
}
|
|
||||||
current_pid = process_queue[i];
|
|
||||||
// printf("\n%d", current_pid);
|
|
||||||
// *regs = processes[current_pid].cpuregs;
|
|
||||||
cpu_registers_t *newregs = (cpu_registers_t*)processes[current_pid].cpuregs.esp;
|
|
||||||
return newregs;
|
|
||||||
// return regs;
|
// return regs;
|
||||||
|
// }
|
||||||
|
uint32_t i = 0;
|
||||||
|
processes[current_pid].cpuregs = *regs;
|
||||||
|
processes[process_queue[current_queue_index]].flags.ran = 1;
|
||||||
|
uint32_t run_count = 0;
|
||||||
|
for(uint32_t i = 0; i < queue_length; i++){
|
||||||
|
run_count += processes[process_queue[i]].flags.ran;
|
||||||
|
}
|
||||||
|
if(run_count == queue_length){
|
||||||
|
for(uint32_t i = 0; i < queue_length; i++){
|
||||||
|
processes[process_queue[i]].flags.ran = 0;
|
||||||
|
}
|
||||||
|
current_queue_index = 0;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
current_queue_index++;
|
||||||
|
}
|
||||||
|
current_pid = process_queue[current_queue_index];
|
||||||
|
asm volatile("mov %0, %%cr3" : : "r"(processes[current_pid].page_dir));
|
||||||
|
|
||||||
|
cpu_registers_t *newregs = (cpu_registers_t*)processes[current_pid].cpuregs.esp;
|
||||||
|
// printf("EAX: %x EBX: %x ECX: %x EDX: %x\n", newregs->eax, newregs->ebx, newregs->ecx, newregs->edx);
|
||||||
|
// printf("ESI: %x EDI: %x ESP: %x EBP: %x\n", newregs->esi, newregs->edi, newregs->esp, newregs->ebp);
|
||||||
|
// printf("EIP: %x CS: %x DS: %x\n", newregs->eip, newregs->cs, newregs->ds);
|
||||||
|
return newregs;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_process_queue(uint32_t pid){
|
void add_process_queue(uint32_t pid){
|
||||||
|
asm volatile ("" : : :"memory");
|
||||||
process_queue[queue_length] = pid;
|
process_queue[queue_length] = pid;
|
||||||
queue_length++;
|
queue_length++;
|
||||||
|
// printf("adding %d @ %d", pid, queue_length-1);
|
||||||
|
// printf("queue: %x", queue_length);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
void remove_process_queue(uint32_t pid){
|
void remove_process_queue(uint32_t pid){
|
||||||
uint32_t last_queue_index = queue_length-1;
|
uint32_t last_queue_index = queue_length-1;
|
||||||
|
|
@ -58,16 +83,15 @@ void remove_process_queue(uint32_t pid){
|
||||||
process_queue[last_queue_index] = 0;
|
process_queue[last_queue_index] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void spawn_new_process(cpu_registers_t defaultregs, char **argv, uint32_t argc, void *cr3){
|
uint32_t spawn_new_process(cpu_registers_t defaultregs, char **argv, uint32_t argc, void *cr3){
|
||||||
uint32_t i = current_pid;
|
uint32_t i = current_pid;
|
||||||
while(processes[i].flags.present){
|
while(processes[i].flags.present){
|
||||||
i++;
|
i++;
|
||||||
if(i >= 0x10000) i = 0;
|
if(i >= 0x10000) i = 0;
|
||||||
if(i == current_pid) return;
|
if(i == current_pid) return -1;
|
||||||
}
|
}
|
||||||
i += !active_processes;
|
// i += !active_processes;
|
||||||
active_processes++;
|
active_processes++;
|
||||||
// printf("Found %d", i);
|
|
||||||
process_t new_proc = {0};
|
process_t new_proc = {0};
|
||||||
new_proc.argc = argc;
|
new_proc.argc = argc;
|
||||||
new_proc.argv = argv;
|
new_proc.argv = argv;
|
||||||
|
|
@ -80,34 +104,61 @@ void spawn_new_process(cpu_registers_t defaultregs, char **argv, uint32_t argc,
|
||||||
new_proc.flags.system = processes[current_pid].flags.system;
|
new_proc.flags.system = processes[current_pid].flags.system;
|
||||||
processes[i] = new_proc;
|
processes[i] = new_proc;
|
||||||
add_process_queue(i);
|
add_process_queue(i);
|
||||||
return;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fork(){
|
void fork(){
|
||||||
|
//make a copy of original process, create new address space,
|
||||||
|
//and copy all page entries with write disabled
|
||||||
|
//(so we con perform a copy on write)
|
||||||
}
|
}
|
||||||
void exec(){
|
void exec(char *filename, char **argv){
|
||||||
|
//open and read file {filename}
|
||||||
|
//then create a new address space, parse elf header and create
|
||||||
|
//a new schedulable entity
|
||||||
}
|
}
|
||||||
void exit(){
|
void kill(uint32_t pid){
|
||||||
|
void *pd = processes[pid].page_dir;
|
||||||
|
uint32_t *pd_ptr = kmalloc(1);
|
||||||
|
uint32_t *pt_ptr = kmalloc(1);
|
||||||
|
kfree(pt_ptr);
|
||||||
|
kfree(pd_ptr);
|
||||||
|
map(pd_ptr, pd, PT_PRESENT);
|
||||||
|
for(uint32_t i = 0; i < 1024; i++){
|
||||||
|
map(pt_ptr, (void *)pd_ptr[i], PT_PRESENT);
|
||||||
|
for(uint32_t j = 0; j < 1024; j++){
|
||||||
|
uint32_t pt_paddr = pt_ptr[j] & ~(0xfff);
|
||||||
|
pm_free(pt_paddr);
|
||||||
|
}
|
||||||
|
unmap(pt_ptr);
|
||||||
|
}
|
||||||
|
processes[pid].flags.present = 0;
|
||||||
|
processes[pid].page_dir = 0;
|
||||||
|
remove_process_queue(pid);
|
||||||
}
|
}
|
||||||
void thread_start(void (*function)(), uint32_t *exit_code){
|
uint32_t thread_start(void (*function)()){
|
||||||
cpu_registers_t regs = {0};
|
cpu_registers_t regs = {0};
|
||||||
regs.cs = 0x10;
|
regs.cs = 0x10;
|
||||||
regs.ds = 0x18;
|
regs.ds = 0x18;
|
||||||
regs.es = 0x18;
|
regs.es = 0x18;
|
||||||
regs.ss = 0x18;
|
regs.ss = 0x18;
|
||||||
|
regs.eflags = 0x202;
|
||||||
// regs.ebp =
|
// regs.ebp =
|
||||||
regs.esp = 0x7000 - sizeof(regs) + 8;
|
regs.esp = (uint32_t)kmalloc(8) - sizeof(regs) + 8;
|
||||||
regs.ebp = regs.esp;
|
regs.ebp = regs.esp;
|
||||||
regs.eip = (uint32_t)function;
|
regs.eip = (uint32_t)function;
|
||||||
memcpy((char *)®s, (char*)regs.esp, sizeof(regs));
|
memcpy((char *)®s, (char*)regs.esp, sizeof(regs));
|
||||||
spawn_new_process(regs, 0, 0, (void *)0x10000);
|
uint32_t retpid = spawn_new_process(regs, 0, 0, (void *)0x10000);
|
||||||
|
// printf("returning :3");
|
||||||
|
// for(;;);
|
||||||
|
return retpid;
|
||||||
}
|
}
|
||||||
void thread_join(uint32_t thread_id, uint32_t **exit_code){
|
void thread_join(uint32_t thread_id, uint32_t *exit_code){
|
||||||
|
while(processes[thread_id].flags.present);
|
||||||
|
*exit_code = processes[thread_id].exit_value;
|
||||||
}
|
}
|
||||||
void thread_exit(uint32_t *exit_code){
|
void thread_exit(uint32_t exit_code){
|
||||||
|
processes[current_pid].flags.present = 0;
|
||||||
|
processes[current_pid].exit_value = exit_code;
|
||||||
|
remove_process_queue(current_pid);
|
||||||
}
|
}
|
||||||
|
|
@ -12,14 +12,19 @@ typedef struct{
|
||||||
uint8_t cpu_lvl :1;
|
uint8_t cpu_lvl :1;
|
||||||
uint8_t priority:3;
|
uint8_t priority:3;
|
||||||
uint8_t present :1;
|
uint8_t present :1;
|
||||||
uint8_t unused :1;
|
uint8_t ran :1;
|
||||||
}flags;
|
}flags;
|
||||||
|
uint32_t exit_value;
|
||||||
uint32_t thread_id;
|
uint32_t thread_id;
|
||||||
uint32_t parent;
|
uint32_t parent;
|
||||||
uint32_t uid;
|
uint32_t uid;
|
||||||
uint32_t gid;
|
uint32_t gid;
|
||||||
}process_t;
|
}process_t;
|
||||||
|
|
||||||
|
#define PROCESS_COUNT 0x10000
|
||||||
|
|
||||||
cpu_registers_t* schedule(cpu_registers_t *regs);
|
cpu_registers_t* schedule(cpu_registers_t *regs);
|
||||||
void init_scheduler();
|
void init_scheduler();
|
||||||
void thread_start(void (*function)(), uint32_t *exit_code);
|
uint32_t thread_start(void (*function)());
|
||||||
|
void thread_join(uint32_t thread_id, uint32_t *exit_code);
|
||||||
|
void thread_exit(uint32_t exit_code);
|
||||||
Loading…
Reference in New Issue