111 lines
3.3 KiB
C++
111 lines
3.3 KiB
C++
// vim: set noet ts=4 sw=4:
|
|
|
|
#include "thread.h"
|
|
#include "../arch/core_ring.h"
|
|
#include "../memory/pageframealloc.h"
|
|
#include "../memory/pagetable.h"
|
|
#include "../debug/kernelpanic.h"
|
|
#include "../interrupt/guard.h"
|
|
#include "debug/output.h"
|
|
|
|
static int idCounter = 1; // counter for task IDs
|
|
//extern four_lvl_paging_t paging_tree;
|
|
extern pagetable_t identity_table;
|
|
|
|
void Thread::kickoff(uintptr_t param1, uintptr_t param2, uintptr_t param3) {
|
|
Thread *thread = reinterpret_cast<Thread *>(param1);
|
|
assert(thread != nullptr && "Kickoff got nullptr pointer to Thread");
|
|
(void)param2; // will be used later
|
|
(void)param3; // will be used later
|
|
// The core must have entered level 1/2 to cause a thread to be scheduled.
|
|
Guard::leave();
|
|
|
|
if(!thread->isKernel)
|
|
Core::Ring::switchToUsermode(thread->StackPointer.user, (void*)0x4000000, 0);
|
|
else
|
|
if(thread->start == nullptr)
|
|
thread->action();
|
|
else
|
|
((void(*)())thread->start)();
|
|
}
|
|
|
|
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);
|
|
|
|
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 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
|
|
};
|
|
|
|
prepareContext(StackPointer.isr, context, kickoff, reinterpret_cast<uintptr_t>(this), 0, 0);
|
|
}
|
|
|
|
Thread::Thread() : Thread(false, 0) {}
|
|
//Thread::Thread() : queue_link(nullptr), id(idCounter++), kill_flag(false) {
|
|
// void *tos = reinterpret_cast<void *>(reserved_stack_space_isr + STACK_SIZE);
|
|
// prepareContext(tos, context, kickoff, reinterpret_cast<uintptr_t>(this), 0, 0);
|
|
//}
|
|
|
|
void Thread::resume(Thread *next) {
|
|
load_paging(next);
|
|
context_switch(&next->context, &context);
|
|
}
|
|
|
|
void* Thread::operator new(size_t sz) noexcept
|
|
{
|
|
if (sz == 0)
|
|
++sz; // avoid std::malloc(0) which may return nullptr on success
|
|
|
|
void *ptr = PageFrameAllocator::alloc(true);
|
|
return ptr;
|
|
}
|
|
|
|
|
|
void Thread::go() {
|
|
load_paging(this);
|
|
context_launch(&context);
|
|
}
|
|
|
|
void Thread::action() { kernelpanic("Wrong entry / missing action in Thread"); }
|