From f8c9766476e66e32e40d732408a141778d8c1f1b Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Thu, 16 Jun 2016 20:43:41 +0200 Subject: [PATCH] Moved more code to kernel --- kernel/hal/x86/include/pmm.hpp | 6 +- kernel/hal/x86/init/Multitasking.cpp | 21 +---- kernel/hal/x86/init/init.cpp | 2 +- kernel/hal/x86/mm/pmm.cpp | 110 ++++----------------------- kernel/hal/x86/mm/vmm3.cpp | 2 +- kernel/kernel/Multitasking.cpp | 24 ++++++ kernel/kernel/c_include/cpu_state.h | 2 - kernel/kernel/c_include/stdint.h | 2 + kernel/kernel/include/base.hpp | 4 +- kernel/kernel/include/cpu_state.h | 4 + kernel/kernel/include/pmm.hpp | 5 +- kernel/kernel/mm/pmm.cpp | 104 +++++++++++++++++++++++++ 12 files changed, 162 insertions(+), 124 deletions(-) create mode 100644 kernel/kernel/Multitasking.cpp delete mode 100644 kernel/kernel/c_include/cpu_state.h create mode 100644 kernel/kernel/include/cpu_state.h create mode 100644 kernel/kernel/mm/pmm.cpp diff --git a/kernel/hal/x86/include/pmm.hpp b/kernel/hal/x86/include/pmm.hpp index 6bb47d2..ec1f058 100644 --- a/kernel/hal/x86/include/pmm.hpp +++ b/kernel/hal/x86/include/pmm.hpp @@ -12,12 +12,16 @@ struct malloc_t { class PMM { private: malloc_t *head; -public: PMM2 pmm2; +public: PMM(); auto init(struct multiboot_info*) -> void; auto alloc(uint32_t length) -> void *; auto free(void* ptr) -> bool; + auto markUsed(const void * addr, uint32_t length) -> bool; + auto operator >> (void * &addr) -> PMM &; //alloc + auto operator << (const void * addr) -> PMM &; //free + auto operator()(int pages) -> void*; //alloc_multipage }; } #endif diff --git a/kernel/hal/x86/init/Multitasking.cpp b/kernel/hal/x86/init/Multitasking.cpp index ff20343..a84d1d6 100644 --- a/kernel/hal/x86/init/Multitasking.cpp +++ b/kernel/hal/x86/init/Multitasking.cpp @@ -28,7 +28,7 @@ Multitasking::Multitasking(): curr_task(nullptr), first_task(nullptr) auto Multitasking::initTask(void(* entry)()) -> struct cpu_state* { void* tmp1, *tmp2; - mm.pmm.pmm2 >> tmp1 >> tmp2; + mm >> tmp1 >> tmp2; uint8_t *stack=(uint8_t*)tmp1, *user_stack=(uint8_t*)tmp2; struct cpu_state new_state = { 0, //EAX @@ -67,25 +67,8 @@ auto Multitasking::schedule(struct cpu_state* cpu) -> struct cpu_state* next=first_task; } curr_task=next; - return next->unpause(); -} -Task::Task(struct cpu_state* cpu): cpu_state(cpu), next(nullptr) {}; -//This is run every time this task is chosen by the scheduler -auto Task::unpause() -> struct cpu_state* { + struct cpu_state* cpu_state=next->unpause(); MTGosHAL::tasks.tss[1] = (uint32_t) (cpu_state + 1); return cpu_state; } -//This is run every time the timer ticks and a task is running -auto Task::pause(struct cpu_state* cpu) -> Task * { - cpu_state=cpu; - return next; -} -auto Task::addTask(Task* task) -> void { - if(next) - return next->addTask(task); - next=task; -} -auto Task::hasNext() -> bool { - return next!=nullptr; -} } // namespace MTGosHAL diff --git a/kernel/hal/x86/init/init.cpp b/kernel/hal/x86/init/init.cpp index 5a4c719..bc34d1c 100644 --- a/kernel/hal/x86/init/init.cpp +++ b/kernel/hal/x86/init/init.cpp @@ -21,7 +21,7 @@ namespace MTGosHAL { GDT gdt; Multitasking tasks; BlockDevice disk; - VMM3 mm; + PMM mm; void main(int eax, struct multiboot_info* ebx) { out.init(ebx); err.init(ebx); diff --git a/kernel/hal/x86/mm/pmm.cpp b/kernel/hal/x86/mm/pmm.cpp index 4276832..d9cbd66 100644 --- a/kernel/hal/x86/mm/pmm.cpp +++ b/kernel/hal/x86/mm/pmm.cpp @@ -3,107 +3,23 @@ #include extern "C" const int kernel_start; extern "C" const int kernel_end; //those are voids actually -void *operator new(size_t size) { - return MTGosHAL::mm.alloc(size); -} -void *operator new[](size_t size) { - return MTGosHAL::mm.alloc(size); -} -void operator delete(void* p) { - MTGosHAL::mm.free(p); -} -void operator delete[](void* p) { - MTGosHAL::mm.free(p); -} -void operator delete(void* p, size_t size) { - MTGosHAL::mm.free(p); -} -void operator delete[](void* p, size_t size) { - MTGosHAL::mm.free(p); -} namespace MTGosHAL { PMM::PMM(): head(nullptr), pmm2() {} auto PMM::init(struct multiboot_info* mb_info) -> void { pmm2.init(mb_info); } -auto PMM::alloc(uint32_t length) -> void * { - if(!head) { - //Alloc space for head - if(length+sizeof(malloc_t)<=4096) { //Small optimization. The routine for allocating more than one continuous page is terribly slow. - void *tmp; - pmm2 >> tmp; - head=(malloc_t*)tmp; - } else - head=(malloc_t*)pmm2(((length+sizeof(malloc_t))>>12)+1); - if(!head) //The alloc() didn't work! We're out of RAM! - return nullptr; - head->len=length; - head->next=head->last=nullptr; - malloc_t* tmp=head; - tmp++; - return (void*)tmp; - } - malloc_t* curr=head; - malloc_t* last=nullptr; - do { - uint32_t loc=(uint32_t)curr+sizeof(malloc_t)+curr->len; - if((loc+length+sizeof(malloc_t))<((loc&(~0xFFF))+4096) && - ((!curr->next) || (loc+length+sizeof(malloc_t))<((uint32_t)(curr->next)))) { - malloc_t *allocd=(malloc_t *)loc; - allocd->len=length; - allocd->last=curr; - allocd->next=curr->next; //Set double linked list - curr->next=allocd; - if(allocd->next) - allocd->next->last=allocd; - malloc_t *tmp=allocd; - tmp++; - return (void*)tmp; - } - last=curr; - curr=curr->next; - } while(curr); - malloc_t *allocd=nullptr; - if(length+sizeof(malloc_t)<=4096) { //Small optimization. The routine for allocating more than one continuous page is terribly slow. - void *tmp; - pmm2 >> tmp; - allocd=(malloc_t*)tmp; - } else - allocd=(malloc_t*)pmm2(((length+sizeof(malloc_t))>>12)+1); - if(!allocd) //The alloc() didn't work! We're out of RAM! - return nullptr; - last->next=allocd; - allocd->len=length; - allocd->last=last; - allocd->next=nullptr; - malloc_t *tmp=allocd; - tmp++; - return (void*)tmp; -} -auto PMM::free(void* ptr) -> bool { - if(!ptr) - return false; - malloc_t* curr=head; - malloc_t* chk=(malloc_t*)ptr; - chk--; - do { - if(curr==chk) { - uint32_t start=((uint32_t)chk)&(~0xFFF); - uint32_t end=start+0x1000; - if((((uint32_t)(curr->last)last)>=end))&&(((uint32_t)(curr->next)>=end)||((uint32_t)(curr->next)last) - curr->last->next=curr->next; - else { - head=curr->next; - } - if(curr->next) - curr->next->last=curr->last; - return true; - } - curr=curr->next; - } while(curr); - return false; +auto PMM::markUsed(const void * addr, uint32_t length) -> bool { + return pmm2.markUsed(addr, length); } +auto PMM::operator >> (void * &addr) -> PMM & { + pmm2>>addr; + return *this; +} //alloc +auto PMM::operator << (const void * addr) -> PMM & { + pmm2< void*{ + return pmm2(pages); +} //alloc_multipage } diff --git a/kernel/hal/x86/mm/vmm3.cpp b/kernel/hal/x86/mm/vmm3.cpp index 1cc7147..c786204 100644 --- a/kernel/hal/x86/mm/vmm3.cpp +++ b/kernel/hal/x86/mm/vmm3.cpp @@ -7,7 +7,7 @@ VMM3::VMM3(): pmm() { auto VMM3::init(struct multiboot_info* mb_info) -> void { pmm.init(mb_info); void* tmp; - pmm.pmm2 >> tmp; + pmm >> tmp; pd=(uint32_t**)tmp; uint32_t ent=0x87; for(int i=0;i<1024;i++) { diff --git a/kernel/kernel/Multitasking.cpp b/kernel/kernel/Multitasking.cpp new file mode 100644 index 0000000..4be6779 --- /dev/null +++ b/kernel/kernel/Multitasking.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +namespace MTGosHAL { + Task::Task(struct cpu_state* cpu): cpu_state(cpu), next(nullptr) {}; + //This is run every time this task is chosen by the scheduler + auto Task::unpause() -> struct cpu_state* { + + return cpu_state; + } + //This is run every time the timer ticks and a task is running + auto Task::pause(struct cpu_state* cpu) -> Task * { + cpu_state=cpu; + return next; + } + auto Task::addTask(Task* task) -> void { + if(next) + return next->addTask(task); + next=task; + } + auto Task::hasNext() -> bool { + return next!=nullptr; + } +} diff --git a/kernel/kernel/c_include/cpu_state.h b/kernel/kernel/c_include/cpu_state.h deleted file mode 100644 index ad3d683..0000000 --- a/kernel/kernel/c_include/cpu_state.h +++ /dev/null @@ -1,2 +0,0 @@ -struct cpu_state { -}; //Empty struct diff --git a/kernel/kernel/c_include/stdint.h b/kernel/kernel/c_include/stdint.h index 314b8da..c233efc 100644 --- a/kernel/kernel/c_include/stdint.h +++ b/kernel/kernel/c_include/stdint.h @@ -8,6 +8,7 @@ typedef unsigned short uint16_t; typedef unsigned int uint32_t; typedef unsigned long long int uint64_t; typedef unsigned int uintptr_t; + #else typedef signed char int8_t; typedef signed short int16_t; @@ -19,3 +20,4 @@ typedef unsigned int uint32_t; typedef unsigned long int uint64_t; typedef unsigned long int uintptr_t; #endif +typedef unsigned long int size_t; diff --git a/kernel/kernel/include/base.hpp b/kernel/kernel/include/base.hpp index ce7305f..60069d0 100644 --- a/kernel/kernel/include/base.hpp +++ b/kernel/kernel/include/base.hpp @@ -12,7 +12,7 @@ namespace MTGosHAL { class Multitasking; class BlockDevice; class Task; - class VMM3; + class PMM; enum class BG_color: uint32_t; enum class FG_color: uint32_t; extern Serial debug; @@ -23,6 +23,6 @@ namespace MTGosHAL { extern IDT idt; extern Multitasking tasks; extern BlockDevice disk; - extern VMM3 mm; + extern PMM mm; } #endif diff --git a/kernel/kernel/include/cpu_state.h b/kernel/kernel/include/cpu_state.h new file mode 100644 index 0000000..4812634 --- /dev/null +++ b/kernel/kernel/include/cpu_state.h @@ -0,0 +1,4 @@ +namespace MTGosHAL { + struct cpu_state { + }; //Empty struct +} diff --git a/kernel/kernel/include/pmm.hpp b/kernel/kernel/include/pmm.hpp index dbe0dc2..3d0a02a 100644 --- a/kernel/kernel/include/pmm.hpp +++ b/kernel/kernel/include/pmm.hpp @@ -12,9 +12,12 @@ private: malloc_t *head; public: PMM(); - auto init(void *) -> void; auto alloc(uint32_t length) -> void *; auto free(void* ptr) -> bool; + auto markUsed(const void * addr, uint32_t length) -> bool; + auto operator >> (void * &addr) -> PMM &; //alloc + auto operator << (const void * addr) -> PMM &; //free + auto operator()(int pages) -> void*; //alloc_multipage }; } #endif diff --git a/kernel/kernel/mm/pmm.cpp b/kernel/kernel/mm/pmm.cpp new file mode 100644 index 0000000..e5e791a --- /dev/null +++ b/kernel/kernel/mm/pmm.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +void *operator new(size_t size) { + return MTGosHAL::mm.alloc(size); +} +void *operator new[](size_t size) { + return MTGosHAL::mm.alloc(size); +} +void operator delete(void* p) { + MTGosHAL::mm.free(p); +} +void operator delete[](void* p) { + MTGosHAL::mm.free(p); +} +void operator delete(void* p, size_t size) { + MTGosHAL::mm.free(p); +} +void operator delete[](void* p, size_t size) { + MTGosHAL::mm.free(p); +} +namespace MTGosHAL { + + auto PMM::alloc(uint32_t length) -> void * { + if(!head) { + //Alloc space for head + if(length+sizeof(malloc_t)<=4096) { //Small optimization. The routine for allocating more than one continuous page is terribly slow. + void *tmp; + *this >> tmp; + head=(malloc_t*)tmp; + } else + head=(malloc_t*)(*this)(((length+sizeof(malloc_t))>>12)+1); + if(!head) //The alloc() didn't work! We're out of RAM! + return nullptr; + head->len=length; + head->next=head->last=nullptr; + malloc_t* tmp=head; + tmp++; + return (void*)tmp; + } + malloc_t* curr=head; + malloc_t* last=nullptr; + do { + uint32_t loc=(uint32_t)curr+sizeof(malloc_t)+curr->len; + if((loc+length+sizeof(malloc_t))<((loc&(~0xFFF))+4096) && + ((!curr->next) || (loc+length+sizeof(malloc_t))<((uint32_t)(curr->next)))) { + malloc_t *allocd=(malloc_t *)loc; + allocd->len=length; + allocd->last=curr; + allocd->next=curr->next; //Set double linked list + curr->next=allocd; + if(allocd->next) + allocd->next->last=allocd; + malloc_t *tmp=allocd; + tmp++; + return (void*)tmp; + } + last=curr; + curr=curr->next; + } while(curr); + malloc_t *allocd=nullptr; + if(length+sizeof(malloc_t)<=4096) { //Small optimization. The routine for allocating more than one continuous page is terribly slow. + void *tmp; + *this >> tmp; + allocd=(malloc_t*)tmp; + } else + allocd=(malloc_t*)(*this)(((length+sizeof(malloc_t))>>12)+1); + if(!allocd) //The alloc() didn't work! We're out of RAM! + return nullptr; + last->next=allocd; + allocd->len=length; + allocd->last=last; + allocd->next=nullptr; + malloc_t *tmp=allocd; + tmp++; + return (void*)tmp; + } + auto PMM::free(void* ptr) -> bool { + if(!ptr) + return false; + malloc_t* curr=head; + malloc_t* chk=(malloc_t*)ptr; + chk--; + do { + if(curr==chk) { + uint32_t start=((uint32_t)chk)&(~0xFFF); + uint32_t end=start+0x1000; + if((((uint32_t)(curr->last)last)>=end))&&(((uint32_t)(curr->next)>=end)||((uint32_t)(curr->next)last) + curr->last->next=curr->next; + else { + head=curr->next; + } + if(curr->next) + curr->next->last=curr->last; + return true; + } + curr=curr->next; + } while(curr); + return false; + } +}