restructure thread constructor and pagetable

This commit is contained in:
2026-02-25 07:39:32 +01:00
parent 76f782c5ae
commit 22ab2fb2a5
8 changed files with 179 additions and 161 deletions

View File

@@ -19,7 +19,7 @@
*/
class IdleThread : public Thread {
public:
explicit IdleThread() : Thread(true) {}
explicit IdleThread() : Thread(true, 0) {}
/*! \brief Wait for a thread to become ready and sleep in the meantime.
*

View File

@@ -21,7 +21,7 @@ void Thread::kickoff(uintptr_t param1, uintptr_t param2, uintptr_t param3) {
Guard::leave();
if(!thread->isKernel)
Core::Ring::switchToUsermode(thread->StackPointer.user, (void*)0x4000000, 0);
Core::Ring::switchToUsermode(thread->StackPointer.user, thread->start, 0);
else
if(thread->start == nullptr)
thread->action();
@@ -33,52 +33,27 @@ void Thread::load_paging(Thread* t){
load_cr3((void*)(t->paging_tree->l4));
}
Thread::Thread (bool kernel, void * startframe, int num_pages): queue_link(nullptr), isKernel(kernel), start(startframe), id(idCounter++), kill_flag(false){
StackPointer.isr = reinterpret_cast<void *>((uintptr_t)PageFrameAllocator::alloc(true)+STACK_SIZE);//(reserved_stack_space_isr + STACK_SIZE);
StackPointer.user = (void*)(0x61FF000+STACK_SIZE);
void Thread::map_app(void* code_paddr, uint16_t code_page_num, void* stack_vaddr, uint16_t stack_page_num){
paging_tree = (four_lvl_paging_t*)PageFrameAllocator::alloc(true);
memset(paging_tree, 0, 4096);
create_basic_page_table(paging_tree, &identity_table);
appcode_table = (pagetable_t*) PageFrameAllocator::alloc(true);
memset(appcode_table, 0, 4096);
appstack_table = (pagetable_t*) PageFrameAllocator::alloc(true);
memset(appstack_table, 0, 4096);
assert(num_pages < 512); //limit for application code size is 2M
for(uint8_t i=0; i<num_pages; i++){
appcode_table->entries[i] = {
.present = 1,
.user = 1,
.address = ((uintptr_t)startframe >> 12) + i
};
// insert app code pages at 0x4000000
for(uintptr_t i=0; i<(code_page_num*4096); i+=4096){
setMapping(0x4000000+i, (void*)((uintptr_t)code_paddr+i), paging_tree);
}
// insert app code pages at 32*512*4096 = 0x4000000
paging_tree->l2->entries[32] = {
.present = 1,
.write = 1,
.user = 1,
.address = (uintptr_t)(appcode_table) >> 12
};
uintptr_t user_stackframe_p = ((uintptr_t)PageFrameAllocator::alloc(false));
appstack_table->entries[511] = {
.present = 1,
.write = 1,
.user = 1,
.address = user_stackframe_p >> 12
};
// insert stack page at 48*512*4096 = 0x6000000
paging_tree->l2->entries[48] = {
.present = 1,
.write = 1,
.user = 1,
.address = (uintptr_t)(appstack_table) >> 12
};
for(uintptr_t i=0; i<(stack_page_num*4096); i+=4096){
uintptr_t user_stackframe_p = ((uintptr_t)PageFrameAllocator::alloc(false));
setMapping((uintptr_t)(stack_vaddr)+i, (void*)((uintptr_t)user_stackframe_p), paging_tree);
}
}
Thread::Thread (bool kernel, void* start_addr, void * codeframe, int code_frame_num): queue_link(nullptr), isKernel(kernel), start(start_addr), id(idCounter++), kill_flag(false){
StackPointer.isr = reinterpret_cast<void *>((uintptr_t)PageFrameAllocator::alloc(true)+STACK_SIZE);// mapped due to identity mapping
StackPointer.user = (void*)(0x61FF000+STACK_SIZE);
map_app(codeframe, code_frame_num, StackPointer.user, 1);
prepareContext(StackPointer.isr, context, kickoff, reinterpret_cast<uintptr_t>(this), 0, 0);
}

View File

@@ -79,7 +79,7 @@ class Thread {
static void kickoff(uintptr_t param1, uintptr_t param2, uintptr_t param3);
static void kickoffUsermode (Thread *object);
void map_app(void* code_paddr, uint16_t code_page_num, void* stack_vaddr, uint16_t stack_page_num);
public:
struct{
void* user;
@@ -120,7 +120,7 @@ class Thread {
*
*/
explicit Thread();
explicit Thread(bool kernel, void * startframe = nullptr, int num_pages=1);
explicit Thread (bool kernel, void* start_addr, void * codeframe=0, int code_frame_num=1);
/*! \brief Activates the first thread on this CPU.
*