62 lines
1.5 KiB
C
62 lines
1.5 KiB
C
#include "../../tasking.h"
|
|
#include "../paging.h"
|
|
#include "../tasking_helpers.h"
|
|
#include "../../pmem.h"
|
|
#include <stddef.h>
|
|
|
|
static void** kstacks=(void*)0xC8000000;
|
|
static char kstack_bmap[(218*1024)/8]={0};
|
|
|
|
static char get_bmap_bit(size_t index) {
|
|
size_t byte=index/8;
|
|
size_t bit=index%8;
|
|
char entry=kstack_bmap[byte];
|
|
return (entry&(1<<bit))>0;
|
|
}
|
|
|
|
static void set_bmap_bit(size_t index) {
|
|
size_t byte=index/8;
|
|
size_t bit=index%8;
|
|
kstack_bmap[byte]=kstack_bmap[byte]|(1<<bit);
|
|
}
|
|
|
|
int new_kstack() {
|
|
int num=-1;
|
|
for (int i=0;i<(218*1024);i++) {
|
|
if (get_bmap_bit(i)==0) {
|
|
num=i;
|
|
break;
|
|
}
|
|
}
|
|
if (num==-1) {
|
|
return -1;
|
|
}
|
|
set_bmap_bit(num);
|
|
map_pages(((char*)kstacks+num*0x1000),pmem_alloc(1),1,1,1);
|
|
return num;
|
|
}
|
|
|
|
void setup_kstack(Thread* thread,void* param1,void* param2,char kmode,void* eip) {
|
|
void* old_cr3=get_cr3();
|
|
size_t kstack_num=new_kstack();
|
|
load_address_space(thread->cr3);
|
|
if (kmode) {
|
|
size_t top_idx=(1024*(kstack_num+1));
|
|
thread->kernel_esp=((void*)(&kstacks[top_idx-5]));
|
|
thread->kernel_esp_top=thread->kernel_esp;
|
|
kstacks[top_idx-1]=(void*)eip;
|
|
} else {
|
|
size_t top_idx=(1024*(kstack_num+1));
|
|
thread->kernel_esp=((void*)(&kstacks[top_idx-7]));
|
|
thread->kernel_esp_top=thread->kernel_esp;
|
|
void** user_stack=(void**)(((char*)alloc_pages(2))+0x2000);
|
|
user_stack-=2;
|
|
user_stack[0]=param1;
|
|
user_stack[1]=param2;
|
|
kstacks[top_idx-3]=(void*)task_init;
|
|
kstacks[top_idx-2]=(void*)user_stack;
|
|
kstacks[top_idx-1]=(void*)eip;
|
|
}
|
|
load_address_space(old_cr3);
|
|
}
|