Changed scheduler.c/kill() away from using old trick to using new kmalloc_page_paddr(uint32_t, uint32_t) function
This commit is contained in:
parent
d16c1c68af
commit
806e011256
|
|
@ -4,7 +4,7 @@ LDFLAGS_i386="-Ttext 0xc0000000 --oformat binary -melf_i386"
|
||||||
cd src/kernel/
|
cd src/kernel/
|
||||||
gcc kmain.c $CFLAGS -o ../../bin/kernel/kmain.o
|
gcc kmain.c $CFLAGS -o ../../bin/kernel/kmain.o
|
||||||
for d in ./*/; do
|
for d in ./*/; do
|
||||||
if [ "$d" != "./obj/" ]; then
|
if [[ "$d" != "./obj/" ]]; then
|
||||||
echo "\033[1;32mCompiling files in $d\033[0m"
|
echo "\033[1;32mCompiling files in $d\033[0m"
|
||||||
for f in $d*.c; do
|
for f in $d*.c; do
|
||||||
echo -e "\033[1;36mfile: $f\033[0m"
|
echo -e "\033[1;36mfile: $f\033[0m"
|
||||||
|
|
|
||||||
|
|
@ -174,12 +174,10 @@ void *get_new_page(uint32_t flags){
|
||||||
map((void *)paddr+(i*4096), (void *)paddr, flags);
|
map((void *)paddr+(i*4096), (void *)paddr, flags);
|
||||||
}
|
}
|
||||||
void *kmalloc(uint32_t size_pgs){
|
void *kmalloc(uint32_t size_pgs){
|
||||||
// static uint8_t fake = 0;
|
|
||||||
uint32_t i = 0xc0000000 >> 12; //4mb/4096 (start search at 1mb line)
|
uint32_t i = 0xc0000000 >> 12; //4mb/4096 (start search at 1mb line)
|
||||||
while(i < (1 << 22)){
|
while(i < (1 << 22)){
|
||||||
uint8_t found = 1;
|
uint8_t found = 1;
|
||||||
for(uint32_t j = 0; j < size_pgs; j++){
|
for(uint32_t j = 0; j < size_pgs; j++){
|
||||||
// printf("found at %x, %x\n", (i + j) << 12, get_paddr((void*)((i + j) << 12)));
|
|
||||||
if(get_pflags((void *)((i + j) << 12))){
|
if(get_pflags((void *)((i + j) << 12))){
|
||||||
found = 0;
|
found = 0;
|
||||||
break;
|
break;
|
||||||
|
|
@ -192,13 +190,8 @@ void *kmalloc(uint32_t size_pgs){
|
||||||
for(uint32_t j = 0; j < size_pgs; j++){
|
for(uint32_t j = 0; j < size_pgs; j++){
|
||||||
uint32_t flags = PT_PRESENT | PT_SYS | (PT_LINK_L * (j != 0)) | (PT_LINK_N * (j < (size_pgs - 1)) | PT_PCD);
|
uint32_t flags = PT_PRESENT | PT_SYS | (PT_LINK_L * (j != 0)) | (PT_LINK_N * (j < (size_pgs - 1)) | PT_PCD);
|
||||||
uint32_t physaddr = pm_alloc();
|
uint32_t physaddr = pm_alloc();
|
||||||
// if(j == 0) printf("%x", physaddr);
|
|
||||||
// printf("Mapping %x to %x\n", (i + j) << 12, physaddr);
|
|
||||||
// if(fake) return 0;
|
|
||||||
map((void *)((i + j) << 12), (void*)physaddr, flags);
|
map((void *)((i + j) << 12), (void*)physaddr, flags);
|
||||||
}
|
}
|
||||||
// fake = !fake;
|
|
||||||
// for(;;);
|
|
||||||
return (void*)(i << 12);
|
return (void*)(i << 12);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -208,7 +201,6 @@ void *kmalloc_page_paddr(uint32_t paddr, uint32_t size_pgs){
|
||||||
while(i < (1 << 22)){
|
while(i < (1 << 22)){
|
||||||
uint8_t found = 1;
|
uint8_t found = 1;
|
||||||
for(uint32_t j = 0; j < size_pgs; j++){
|
for(uint32_t j = 0; j < size_pgs; j++){
|
||||||
// printf("found at %x, %x\n", (i + j) << 12, get_paddr((void*)((i + j) << 12)));
|
|
||||||
if(get_pflags((void *)((i + j) << 12))){
|
if(get_pflags((void *)((i + j) << 12))){
|
||||||
found = 0;
|
found = 0;
|
||||||
break;
|
break;
|
||||||
|
|
@ -221,13 +213,9 @@ void *kmalloc_page_paddr(uint32_t paddr, uint32_t size_pgs){
|
||||||
for(uint32_t j = 0; j < size_pgs; j++){
|
for(uint32_t j = 0; j < size_pgs; j++){
|
||||||
uint32_t flags = PT_PRESENT | PT_SYS | (PT_LINK_L * (j != 0)) | (PT_LINK_N * (j < (size_pgs - 1)) | PT_PCD);
|
uint32_t flags = PT_PRESENT | PT_SYS | (PT_LINK_L * (j != 0)) | (PT_LINK_N * (j < (size_pgs - 1)) | PT_PCD);
|
||||||
uint32_t physaddr = paddr + j * 4096;
|
uint32_t physaddr = paddr + j * 4096;
|
||||||
// if(j == 0) printf("%x", physaddr);
|
|
||||||
// printf("Mapping %x to %x\n", (i + j) << 12, physaddr);
|
|
||||||
// if(fake) return 0;
|
|
||||||
map((void *)((i + j) << 12), (void*)physaddr, flags);
|
map((void *)((i + j) << 12), (void*)physaddr, flags);
|
||||||
}
|
}
|
||||||
// fake = !fake;
|
|
||||||
// for(;;);
|
|
||||||
return (void*)(i << 12);
|
return (void*)(i << 12);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,4 +85,5 @@ inline void *get_cr3(){
|
||||||
int pm_init(kernel_info_t *kernel_info);
|
int pm_init(kernel_info_t *kernel_info);
|
||||||
|
|
||||||
void *kmalloc(uint32_t size_pgs);//allocates in multiples of 4096
|
void *kmalloc(uint32_t size_pgs);//allocates in multiples of 4096
|
||||||
void *kfree(void *vaddr);
|
void *kfree(void *vaddr);
|
||||||
|
void *kmalloc_page_paddr(uint32_t paddr, uint32_t size_pgs);
|
||||||
|
|
@ -11,14 +11,8 @@ 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 scheduler_init(){
|
void scheduler_init(){
|
||||||
// printf("?");
|
|
||||||
// process_queue = kmalloc((PROCESS_COUNT * sizeof(uint32_t)) / 4096);
|
|
||||||
processes = kmalloc((PROCESS_COUNT * sizeof(process_t) + 4095) / 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 < PROCESS_COUNT; 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;
|
processes[i].flags.present = 0;
|
||||||
process_queue[i] = 0;
|
process_queue[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -26,9 +20,7 @@ void scheduler_init(){
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
uint32_t timer = 0;
|
uint32_t timer = 0;
|
||||||
|
|
||||||
|
|
@ -37,8 +29,6 @@ void add_process_queue(uint32_t pid){
|
||||||
asm volatile ("" : : :"memory");
|
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);
|
|
||||||
asm("sti");
|
asm("sti");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -53,7 +43,7 @@ void remove_process_queue(uint32_t pid){
|
||||||
process_queue[i] = 0;
|
process_queue[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(old_index == last_queue_index || old_index < queue_length){
|
if(old_index == last_queue_index || old_index < queue_length || old_index < PROCESS_COUNT){
|
||||||
asm("sti");
|
asm("sti");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -68,22 +58,15 @@ void remove_process_queue(uint32_t pid){
|
||||||
cpu_registers_t *schedule(cpu_registers_t *regs){
|
cpu_registers_t *schedule(cpu_registers_t *regs){
|
||||||
if(queue_length == 0){
|
if(queue_length == 0){
|
||||||
add_process_queue(0);
|
add_process_queue(0);
|
||||||
// return regs;
|
|
||||||
}
|
}
|
||||||
// uint32_t i = 0;
|
|
||||||
processes[current_pid].cpuregs = *regs;
|
processes[current_pid].cpuregs = *regs;
|
||||||
current_queue_index++;
|
current_queue_index++;
|
||||||
if(current_queue_index >= queue_length){
|
if(current_queue_index >= queue_length){
|
||||||
current_queue_index = 0;
|
current_queue_index = 0;
|
||||||
}
|
}
|
||||||
// current_pid = [current_queue_index]
|
|
||||||
current_pid = process_queue[current_queue_index];
|
current_pid = process_queue[current_queue_index];
|
||||||
asm volatile("mov %0, %%cr3" : : "r"(processes[current_pid].page_dir));
|
asm volatile("mov %0, %%cr3" : : "r"(processes[current_pid].page_dir));
|
||||||
// printf("pid: %d, %d\n", run_count, queue_length);
|
|
||||||
cpu_registers_t *newregs = (cpu_registers_t*)processes[current_pid].cpuregs.esp;
|
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;
|
return newregs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -123,18 +106,20 @@ void exec(char *filename, char **argv){
|
||||||
}
|
}
|
||||||
void kill(uint32_t pid){
|
void kill(uint32_t pid){
|
||||||
void *pd = processes[pid].page_dir;
|
void *pd = processes[pid].page_dir;
|
||||||
uint32_t *pd_ptr = kmalloc(1);
|
// uint32_t *pd_ptr = kmalloc(1);
|
||||||
uint32_t *pt_ptr = kmalloc(1);
|
// uint32_t *pt_ptr = kmalloc(1);
|
||||||
kfree(pt_ptr);
|
// kfree(pt_ptr);
|
||||||
kfree(pd_ptr);
|
// kfree(pd_ptr);
|
||||||
map(pd_ptr, pd, PT_PRESENT);
|
// map(pd_ptr, pd, PT_PRESENT);
|
||||||
|
uint32_t *pd_ptr = kmalloc_page_paddr(pd, 1);
|
||||||
for(uint32_t i = 0; i < 1024; i++){
|
for(uint32_t i = 0; i < 1024; i++){
|
||||||
map(pt_ptr, (void *)pd_ptr[i], PT_PRESENT);
|
// map(pt_ptr, (void *)pd_ptr[i], PT_PRESENT);
|
||||||
|
uint32_t *pt_ptr = kmalloc_page_paddr((void *)pd_ptr[i], 1);
|
||||||
for(uint32_t j = 0; j < 1024; j++){
|
for(uint32_t j = 0; j < 1024; j++){
|
||||||
uint32_t pt_paddr = pt_ptr[j] & ~(0xfff);
|
uint32_t pt_paddr = pt_ptr[j] & ~(0xfff);
|
||||||
pm_free(pt_paddr);
|
pm_free(pt_paddr);
|
||||||
}
|
}
|
||||||
unmap(pt_ptr);
|
kfree(pt_ptr);
|
||||||
}
|
}
|
||||||
processes[pid].flags.present = 0;
|
processes[pid].flags.present = 0;
|
||||||
processes[pid].page_dir = 0;
|
processes[pid].page_dir = 0;
|
||||||
|
|
@ -159,23 +144,16 @@ uint32_t thread_start(void (*function)()){
|
||||||
regs.es = 0x18;
|
regs.es = 0x18;
|
||||||
regs.ss = 0x18;
|
regs.ss = 0x18;
|
||||||
regs.eflags = 0x202;
|
regs.eflags = 0x202;
|
||||||
// regs.ebp =
|
|
||||||
regs.esp = (uint32_t)kmalloc(8) - 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));
|
||||||
uint32_t retpid = spawn_new_process(regs, 0, 0, (void *)0x10000);
|
uint32_t retpid = spawn_new_process(regs, 0, 0, (void *)0x10000);
|
||||||
processes[retpid].parent = current_pid;
|
processes[retpid].parent = current_pid;
|
||||||
// printf("returning :3");
|
|
||||||
// for(;;);
|
|
||||||
return retpid;
|
return retpid;
|
||||||
}
|
}
|
||||||
void thread_join(uint32_t thread_id, uint32_t *exit_code){
|
void thread_join(uint32_t thread_id, uint32_t *exit_code){
|
||||||
asm volatile ("" : : :"memory");
|
asm volatile ("" : : :"memory");
|
||||||
// while(processes[thread_id].flags.present){
|
|
||||||
// // printf("%d", processes[thread_id].flags.present);
|
|
||||||
// };
|
|
||||||
// printf("Done");
|
|
||||||
set_pid_blocked(current_pid);
|
set_pid_blocked(current_pid);
|
||||||
asm("int $32");
|
asm("int $32");
|
||||||
*exit_code = processes[thread_id].exit_value;
|
*exit_code = processes[thread_id].exit_value;
|
||||||
|
|
@ -185,6 +163,5 @@ void thread_exit(uint32_t exit_code){
|
||||||
processes[current_pid].exit_value = exit_code;
|
processes[current_pid].exit_value = exit_code;
|
||||||
remove_process_queue(current_pid);
|
remove_process_queue(current_pid);
|
||||||
set_pid_unblocked(processes[current_pid].parent);
|
set_pid_unblocked(processes[current_pid].parent);
|
||||||
// printf("Exiting pid %d, %d\n", current_pid);
|
|
||||||
asm volatile ("int $32");//call scheduler via interrupt
|
asm volatile ("int $32");//call scheduler via interrupt
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue