Moved more code to kernel

This commit is contained in:
Morten Delenk 2016-06-16 20:43:41 +02:00
parent 21719edafc
commit f8c9766476
12 changed files with 162 additions and 124 deletions

View file

@ -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

View file

@ -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

View file

@ -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);

View file

@ -3,107 +3,23 @@
#include <vmm3.hpp>
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)<start)||((uint32_t)(curr->last)>=end))&&(((uint32_t)(curr->next)>=end)||((uint32_t)(curr->next)<start))) {
pmm2 << (void*)start;
}
if(curr->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<<addr;
return *this;
} //free
auto PMM::operator()(int pages) -> void*{
return pmm2(pages);
} //alloc_multipage
}

View file

@ -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++) {

View file

@ -0,0 +1,24 @@
#include <base.hpp>
#include <textDISP.hpp>
#include <Multitasking.hpp>
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;
}
}

View file

@ -1,2 +0,0 @@
struct cpu_state {
}; //Empty struct

View file

@ -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;

View file

@ -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

View file

@ -0,0 +1,4 @@
namespace MTGosHAL {
struct cpu_state {
}; //Empty struct
}

View file

@ -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

104
kernel/kernel/mm/pmm.cpp Normal file
View file

@ -0,0 +1,104 @@
#include <base.hpp>
#include <pmm.hpp>
#include <stdint.h>
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)<start)||((uint32_t)(curr->last)>=end))&&(((uint32_t)(curr->next)>=end)||((uint32_t)(curr->next)<start))) {
*this << (void*)start;
}
if(curr->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;
}
}