Minor fixes
This commit is contained in:
parent
5599072355
commit
d16c1c68af
|
|
@ -201,6 +201,7 @@ void *kmalloc(uint32_t size_pgs){
|
|||
// for(;;);
|
||||
return (void*)(i << 12);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void *kmalloc_page_paddr(uint32_t paddr, uint32_t size_pgs){
|
||||
uint32_t i = 0xc0000000 >> 12; //4mb/4096 (start search at 1mb line)
|
||||
|
|
@ -231,6 +232,9 @@ void *kmalloc_page_paddr(uint32_t paddr, uint32_t size_pgs){
|
|||
}
|
||||
}
|
||||
void *kfree(void *vaddr){
|
||||
if(!get_pflags(vaddr)){
|
||||
return 0;
|
||||
}
|
||||
uint32_t addr = (uint32_t)vaddr;
|
||||
addr &= ~0xfff;
|
||||
while(get_pflags((void *)addr)&PT_LINK_L) addr -= 0x1000;
|
||||
|
|
|
|||
|
|
@ -13,11 +13,11 @@ uint32_t volatile current_queue_index;
|
|||
void scheduler_init(){
|
||||
// printf("?");
|
||||
// process_queue = kmalloc((PROCESS_COUNT * sizeof(uint32_t)) / 4096);
|
||||
processes = kmalloc((PROCESS_COUNT * sizeof(process_t)) / 4096);
|
||||
processes = kmalloc((PROCESS_COUNT * sizeof(process_t) + 4095) / 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 < PROCESS_COUNT; i++){
|
||||
// printf("Index: %x, %d\n", process_queue, sizeof(process_t));
|
||||
processes[i].flags.present = 0;
|
||||
process_queue[i] = 0;
|
||||
|
|
@ -31,10 +31,45 @@ void scheduler_init(){
|
|||
// printf("Installing scheduler");
|
||||
}
|
||||
uint32_t timer = 0;
|
||||
|
||||
void add_process_queue(uint32_t pid){
|
||||
asm("cli");
|
||||
asm volatile ("" : : :"memory");
|
||||
process_queue[queue_length] = pid;
|
||||
queue_length++;
|
||||
// printf("adding %d @ %d", pid, queue_length-1);
|
||||
// printf("queue: %x", queue_length);
|
||||
asm("sti");
|
||||
return;
|
||||
}
|
||||
//Watch out for this function.
|
||||
void remove_process_queue(uint32_t pid){
|
||||
asm("cli");
|
||||
uint32_t last_queue_index = queue_length-1;
|
||||
uint32_t old_index = -1;
|
||||
for(uint32_t i = 0; i <= last_queue_index; i++){
|
||||
if(process_queue[i] == pid){
|
||||
old_index = i;
|
||||
process_queue[i] = 0;
|
||||
}
|
||||
}
|
||||
if(old_index == last_queue_index || old_index < queue_length){
|
||||
asm("sti");
|
||||
return;
|
||||
}
|
||||
process_queue[old_index] = process_queue[last_queue_index];
|
||||
process_queue[last_queue_index] = 0;
|
||||
queue_length--;
|
||||
if(current_queue_index > 0)
|
||||
current_queue_index--;
|
||||
asm("sti");
|
||||
}
|
||||
|
||||
cpu_registers_t *schedule(cpu_registers_t *regs){
|
||||
// if(queue_length == 0){
|
||||
// return regs;
|
||||
// }
|
||||
if(queue_length == 0){
|
||||
add_process_queue(0);
|
||||
// return regs;
|
||||
}
|
||||
// uint32_t i = 0;
|
||||
processes[current_pid].cpuregs = *regs;
|
||||
current_queue_index++;
|
||||
|
|
@ -52,35 +87,11 @@ cpu_registers_t *schedule(cpu_registers_t *regs){
|
|||
return newregs;
|
||||
}
|
||||
|
||||
void add_process_queue(uint32_t pid){
|
||||
asm volatile ("" : : :"memory");
|
||||
process_queue[queue_length] = pid;
|
||||
queue_length++;
|
||||
// printf("adding %d @ %d", pid, queue_length-1);
|
||||
// printf("queue: %x", queue_length);
|
||||
return;
|
||||
}
|
||||
void remove_process_queue(uint32_t pid){
|
||||
uint32_t last_queue_index = queue_length-1;
|
||||
uint32_t old_index = 0;
|
||||
for(uint32_t i = 0; i < last_queue_index; i++){
|
||||
if(process_queue[i] == pid){
|
||||
old_index = i;
|
||||
process_queue[i] = 0;
|
||||
}
|
||||
}
|
||||
if(old_index == last_queue_index) return;
|
||||
process_queue[old_index] = process_queue[last_queue_index];
|
||||
process_queue[last_queue_index] = 0;
|
||||
queue_length--;
|
||||
current_queue_index--;
|
||||
}
|
||||
|
||||
uint32_t spawn_new_process(cpu_registers_t defaultregs, char **argv, uint32_t argc, void *cr3){
|
||||
uint32_t i = current_pid;
|
||||
while(processes[i].flags.present){
|
||||
i++;
|
||||
if(i >= 0x10000) i = 0;
|
||||
if(i >= PROCESS_COUNT) i = 0;
|
||||
if(i == current_pid) return -1;
|
||||
}
|
||||
// i += !active_processes;
|
||||
|
|
@ -154,22 +165,26 @@ uint32_t thread_start(void (*function)()){
|
|||
regs.eip = (uint32_t)function;
|
||||
memcpy((char *)®s, (char*)regs.esp, sizeof(regs));
|
||||
uint32_t retpid = spawn_new_process(regs, 0, 0, (void *)0x10000);
|
||||
processes[retpid].parent = current_pid;
|
||||
// printf("returning :3");
|
||||
// for(;;);
|
||||
return retpid;
|
||||
}
|
||||
void thread_join(uint32_t thread_id, uint32_t *exit_code){
|
||||
asm volatile ("" : : :"memory");
|
||||
while(processes[thread_id].flags.present){
|
||||
// printf("%d", processes[thread_id].flags.present);
|
||||
};
|
||||
// while(processes[thread_id].flags.present){
|
||||
// // printf("%d", processes[thread_id].flags.present);
|
||||
// };
|
||||
// printf("Done");
|
||||
set_pid_blocked(current_pid);
|
||||
asm("int $32");
|
||||
*exit_code = processes[thread_id].exit_value;
|
||||
}
|
||||
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);
|
||||
set_pid_unblocked(processes[current_pid].parent);
|
||||
// printf("Exiting pid %d, %d\n", current_pid);
|
||||
asm volatile ("int $32");//call scheduler via interrupt
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue