restructure thread constructor and pagetable
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include "../sync/semaphore.h"
|
||||
#include "../thread/scheduler.h"
|
||||
#include "../memory/pageframealloc.h"
|
||||
#include "../memory/pagetable.h"
|
||||
void *operator new(size_t, void *);
|
||||
|
||||
//#include "../user/app1/appl.h"
|
||||
@@ -138,119 +139,6 @@ namespace Syscall {
|
||||
void kill(Vault &vault, size_t pid){
|
||||
//vault.scheduler.kill(&apps[pid]);
|
||||
}
|
||||
|
||||
|
||||
uintptr_t isMapped(uintptr_t vaddr, four_lvl_paging_t* flpt){
|
||||
uint16_t l4Index = (vaddr>>39) & 0x1FF;
|
||||
uint16_t l3Index = (vaddr>>30) & 0x1FF;
|
||||
uint16_t l2Index = (vaddr>>21) & 0x1FF;
|
||||
uint16_t l1Index = (vaddr>>12) & 0x1FF;
|
||||
|
||||
if(flpt->l4->entries[l4Index].present){
|
||||
pagetable_t* lvl3 = (pagetable_t*)(flpt->l4->entries[l4Index].address<<12);
|
||||
if(lvl3->entries[l3Index].present){
|
||||
pagetable_t* lvl2 = (pagetable_t*)(lvl3->entries[l3Index].address<<12);
|
||||
if(lvl2->entries[l2Index].present){
|
||||
pagetable_t* lvl1 = (pagetable_t*)(lvl2->entries[l2Index].address<<12);
|
||||
if(lvl1->entries[l1Index].present)
|
||||
return lvl1->entries[l1Index].address<<12;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void* getFreeVirtSpace(four_lvl_paging_t* search_table, uint8_t num_pages){
|
||||
uint32_t start_v = 0x4000;
|
||||
uint32_t stop_v = 0x6000;
|
||||
static uint32_t next_start_v = 0x4000;
|
||||
//static uint32_t next_start_v = start_v;
|
||||
|
||||
//start from last found address
|
||||
for (uint32_t v=next_start_v; v<stop_v; v++) {
|
||||
bool space_is_free = true;
|
||||
for(uint32_t i=0; i<num_pages; i++){
|
||||
if(isMapped((uintptr_t)(v+i)<<12, search_table)) {
|
||||
space_is_free = false;
|
||||
}
|
||||
}
|
||||
if(space_is_free){
|
||||
//next_start_v = v+num_pages;
|
||||
return (void*) ((uintptr_t) (v <<12));
|
||||
}
|
||||
}
|
||||
//start again from the real start address
|
||||
for (uint32_t v=start_v; v<next_start_v; v++) {
|
||||
bool space_is_free = true;
|
||||
for(uint32_t i=0; i<num_pages; i++){
|
||||
if(isMapped((uintptr_t)(v+i)<<12, search_table)) {
|
||||
space_is_free = false;
|
||||
}
|
||||
}
|
||||
if(space_is_free){
|
||||
next_start_v = v+num_pages;
|
||||
return (void*) ((uintptr_t) (v <<12));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void setMapping(uintptr_t vaddr, void* frame, four_lvl_paging_t* flpt){
|
||||
uint16_t l4Index = (vaddr>>39) & 0x1FF;
|
||||
uint16_t l3Index = (vaddr>>30) & 0x1FF;
|
||||
uint16_t l2Index = (vaddr>>21) & 0x1FF;
|
||||
uint16_t l1Index = (vaddr>>12) & 0x1FF;
|
||||
|
||||
if(!(flpt->l4->entries[l4Index].present)){
|
||||
pagetable_t* newl3 = (pagetable_t*)PageFrameAllocator::alloc(true);
|
||||
memset(newl3, 0, 4096);
|
||||
flpt->l4->entries[l4Index] = {
|
||||
.present = 1,
|
||||
.write = 1,
|
||||
.user = 1,
|
||||
.address = (uintptr_t)newl3 >> 12
|
||||
};
|
||||
}
|
||||
|
||||
pagetable_t* lvl3 = (pagetable_t*)(flpt->l4->entries[l4Index].address<<12);
|
||||
if(!(lvl3->entries[l3Index].present)){
|
||||
pagetable_t* newl2 = (pagetable_t*)PageFrameAllocator::alloc(true);
|
||||
memset(newl2, 0, 4096);
|
||||
lvl3->entries[l3Index] = {
|
||||
.present = 1,
|
||||
.write = 1,
|
||||
.user = 1,
|
||||
.address = (uintptr_t)newl2 >> 12
|
||||
};
|
||||
}
|
||||
|
||||
pagetable_t* lvl2 = (pagetable_t*)(lvl3->entries[l3Index].address<<12);
|
||||
if(!(lvl2->entries[l2Index].present)){
|
||||
pagetable_t* newl1 = (pagetable_t*)PageFrameAllocator::alloc(true);
|
||||
memset(newl1, 0, 4096);
|
||||
lvl2->entries[l2Index] = {
|
||||
.present = 1,
|
||||
.write = 1,
|
||||
.user = 1,
|
||||
.address = (uintptr_t)newl1 >> 12
|
||||
};
|
||||
}
|
||||
|
||||
pagetable_t* lvl1 = (pagetable_t*)(lvl2->entries[l2Index].address<<12);
|
||||
|
||||
if(frame){
|
||||
assert(!(lvl1->entries[l1Index].present)); // should not be present, bc its a new mapping
|
||||
lvl1->entries[l1Index] = {
|
||||
.present = 1,
|
||||
.write = 1,
|
||||
.user = 1,
|
||||
.address = (uintptr_t)frame >> 12
|
||||
};
|
||||
}
|
||||
else //unmap if nullptr
|
||||
lvl1->entries[l1Index].present = 0;
|
||||
}
|
||||
|
||||
void* map(Vault *vault, size_t size) {
|
||||
size_t num_pages = (size + 4096 - 1) / 4096;
|
||||
//pagetable_t* subbytable = vault->scheduler.active()->subtable;
|
||||
@@ -393,7 +281,10 @@ bool reply(Vault& v, const void* buffer, size_t size) {
|
||||
}
|
||||
|
||||
int fork(Vault &vault, InterruptContext *user_context) {
|
||||
return 0;
|
||||
Thread* parent = vault.scheduler.active();
|
||||
//Thread* child = new Thread(false,
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user