Added a malloc function. 90% guaranteed to work!

This commit is contained in:
Morten Delenk 2016-05-21 16:20:53 +00:00
parent 3e21cf22ee
commit 224d840cc9
14 changed files with 260 additions and 137 deletions

View file

@ -0,0 +1,2 @@
#define MIN(a,b) ((a)>(b))?(b):(a)
#define MAX(a,b) ((a)<(b))?(b):(a)

View file

@ -8,9 +8,9 @@ private:
public: public:
auto markUsed(void * addr) -> void; auto markUsed(void * addr) -> void;
auto init(struct multiboot_info*) -> void; auto init(struct multiboot_info*) -> void;
auto operator >> (void * &addr) -> QDPMM &; //alloc auto operator >> (void * &addr) -> PMM &; //alloc
auto operator << (const void * addr) -> QDPMM &; //free auto operator << (const void * addr) -> PMM &; //free
auto operator () (uint32_t len) -> void*;
}; };
} }
#endif #endif

View file

@ -0,0 +1,2 @@
#define MIN(a,b) ((a)>(b))?(b):(a)
#define MAX(a,b) ((a)<(b))?(b):(a)

View file

@ -1,20 +1,23 @@
#ifndef _PMM_HPP #ifndef _PMM_HPP
#define _PMM_HPP #define _PMM_HPP
#include <qdpmm.hpp> #include <pmm2.hpp>
#include <stdint.h> #include <stdint.h>
#include <multiboot.h> #include <multiboot.h>
namespace MTGosHAL { namespace MTGosHAL {
struct malloc_t {
uint32_t len;
malloc_t *last;
malloc_t *next;
};
class PMM { class PMM {
private: private:
uint16_t **pageTable; malloc_t *head;
QDPMM qdpmm; PMM2 pmm2;
public: public:
PMM(); PMM();
auto markUsed(const void * addr, uint32_t length) -> bool;
auto init(struct multiboot_info*) -> void; auto init(struct multiboot_info*) -> void;
auto operator >> (void * &addr) -> PMM &; //alloc auto alloc(uint32_t length) -> void *;
auto operator << (const void * addr) -> PMM &; //free auto free(void* ptr) -> bool;
auto operator()(int pages) -> void*; //alloc_multipage
}; };
} }
#endif #endif

View file

@ -0,0 +1,20 @@
#ifndef _PMM2_HPP
#define _PMM2_HPP
#include <pmm3.hpp>
#include <stdint.h>
#include <multiboot.h>
namespace MTGosHAL {
class PMM2 {
private:
uint16_t **pageTable;
PMM3 pmm3;
public:
PMM2();
auto markUsed(const void * addr, uint32_t length) -> bool;
auto init(struct multiboot_info*) -> void;
auto operator >> (void * &addr) -> PMM2 &; //alloc
auto operator << (const void * addr) -> PMM2 &; //free
auto operator()(int pages) -> void*; //alloc_multipage
};
}
#endif

View file

@ -1,18 +1,18 @@
#ifndef _QDPMM_HPP #ifndef _PMM3_HPP
#define _QDPMM_HPP #define _PMM3_HPP
#include <stdint.h> #include <stdint.h>
#include <multiboot.h> #include <multiboot.h>
namespace MTGosHAL { namespace MTGosHAL {
class QDPMM { class PMM3 {
private: private:
uint32_t bitmap[0x8000]; //Enough for 4 GB uint32_t bitmap[0x8000]; //Enough for 4 GB
protected: protected:
public: public:
QDPMM(); PMM3();
auto markUsed(const void * addr) -> void; auto markUsed(const void * addr) -> void;
auto init(struct multiboot_info*) -> void; auto init(struct multiboot_info*) -> void;
auto operator >> (void * &addr) -> QDPMM &; //alloc auto operator >> (void * &addr) -> PMM3 &; //alloc
auto operator << (const void * addr) -> QDPMM &; //free auto operator << (const void * addr) -> PMM3 &; //free
}; };
} }
#endif #endif

View file

@ -27,9 +27,7 @@ Multitasking::Multitasking(): curr_task(nullptr), first_task(nullptr)
} }
auto Multitasking::initTask(void(* entry)()) -> struct cpu_state* auto Multitasking::initTask(void(* entry)()) -> struct cpu_state*
{ {
void *tmp1, *tmp2; uint8_t *stack=(uint8_t*)mm.alloc(4096), *user_stack=(uint8_t*)mm.alloc(4096);
mm >> tmp1 >> tmp2;
uint8_t *stack=(uint8_t*)tmp1, *user_stack=(uint8_t*)tmp2;
struct cpu_state new_state = { struct cpu_state new_state = {
0, //EAX 0, //EAX
0, //EBX 0, //EBX

View file

@ -44,11 +44,29 @@ namespace MTGosHAL {
idt.setEntry(8, (void *)((uint32_t)&intr_stub_0+128), SEG_DBL_FAULT, IDT_TASK_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); idt.setEntry(8, (void *)((uint32_t)&intr_stub_0+128), SEG_DBL_FAULT, IDT_TASK_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED);
idt.apply(); idt.apply();
mm.init(ebx); mm.init(ebx);
debug << "Now tesing the PMM... Allocating 1 byte\n";
char* tmp=(char*)mm.alloc(1);
debug << "Allocating another byte\n";
char* tmp2=(char*)mm.alloc(1);
tmp2[0]='A';
debug << "Freeing the first byte...\n";
mm.free((void*)tmp);
debug << "Allocating 14 bytes... \n";
tmp=(char*)mm.alloc(14);
debug << "Changing the last byte... \n";
tmp[13]='B';
debug << "Changing if the second byte has changed...\n";
if(tmp2[0]=='B') {
err << "The allocate function is broken.\n";
debug << "The allocate function is broken.\n";
} else {
debug << "The allocate function works!\n";
}
debug << "Freeing up both pointers.";
mm.free((void*)tmp);
mm.free((void*)tmp2);
multiboot_mod_list *mods = (multiboot_mod_list*) ebx->mods_addr; multiboot_mod_list *mods = (multiboot_mod_list*) ebx->mods_addr;
void** progs=nullptr; void** progs=(void**)mm.alloc(4096);
void* tmp;
mm >> tmp;
progs=(void**)tmp;
uint32_t i; uint32_t i;
for(i=0;i<(ebx->mods_count<1023?ebx->mods_count:1023);i++) { //Basically until MIN(ebx->mods_count, 1023), as we only support loading up to 1023 programs directly. for(i=0;i<(ebx->mods_count<1023?ebx->mods_count:1023);i++) { //Basically until MIN(ebx->mods_count, 1023), as we only support loading up to 1023 programs directly.
progs[i]=(void*)(mods[i].mod_start); progs[i]=(void*)(mods[i].mod_start);

View file

@ -1,125 +1,107 @@
#include <pmm.hpp>
#include <base.hpp> #include <base.hpp>
//In this part, please remember that the address is split into 3 parts. Bitmap: #include <pmm.hpp>
// AAAA AAAA ABBB BBBB BBBB CCCC CCCC CCCC
#define SPLIT1_FLAG 0xFF800000ul
#define SPLIT1_SHIFT(a) ((a)<<23)
#define SPLIT1_UNSHIFT(a) ((a)>>23)
#define SPLIT2_FLAG 0x007FF000ul
#define SPLIT2_SHIFT(a) ((a)<<12)
#define SPLIT2_UNSHIFT(a) (((a)<<23)&0x7FF)
#define SPLIT3_FLAG 0x00000FFFul
#define SPLIT3_SHIFT(a) ((a))
#define SPLIT3_UNSHIFT(a) ((a)&0xFFF)
extern "C" const int kernel_start; extern "C" const int kernel_start;
extern "C" const int kernel_end; //those are voids actually extern "C" const int kernel_end; //those are voids actually
void *operator new(size_t size) { void *operator new(size_t size) {
if(size>4096) { return MTGosHAL::mm.alloc(size);
return MTGosHAL::mm(size/4096);
}
void *ptr;
MTGosHAL::mm >> ptr;
return ptr;
} }
void *operator new[](size_t size) { void *operator new[](size_t size) {
if(size>4096) { return MTGosHAL::mm.alloc(size);
return MTGosHAL::mm(size/4096);
}
void *ptr;
MTGosHAL::mm >> ptr;
return ptr;
} }
void operator delete(void* p) { void operator delete(void* p) {
MTGosHAL::mm << p; MTGosHAL::mm.free(p);
} }
void operator delete[](void* p) { void operator delete[](void* p) {
MTGosHAL::mm << p; MTGosHAL::mm.free(p);
} }
void operator delete(void* p, size_t size) { void operator delete(void* p, size_t size) {
MTGosHAL::mm << p; MTGosHAL::mm.free(p);
} }
void operator delete[](void* p, size_t size) { void operator delete[](void* p, size_t size) {
MTGosHAL::mm << p; MTGosHAL::mm.free(p);
} }
namespace MTGosHAL { namespace MTGosHAL {
PMM::PMM(): qdpmm() { PMM::PMM(): head(nullptr), pmm2() {}
}
auto PMM::markUsed(const void * addr, uint32_t length) -> bool {
uint32_t add=(uint32_t)addr;
uint32_t pagetid = SPLIT1_UNSHIFT(add);
if(length > 256*1024*1024) //Maximum allocation limit is 256MB
return false;
if(!pageTable[pagetid]) {
void* temp;
qdpmm >> temp;
pageTable[pagetid]=(uint16_t*)temp;
for(int i=0;i<2048;i++)
pageTable[pagetid][i]=0;
markUsed(pageTable[pagetid],1024); //Not a mistake
}
//Check if used
for(uint32_t curr_addr=add+length;curr_addr>=add;curr_addr-=0x1000) {
if(pageTable[SPLIT1_UNSHIFT(curr_addr)][SPLIT2_UNSHIFT(curr_addr)])
return false;
}
//Mark as used
uint16_t counter=1;
for(uint32_t curr_addr=add+length;curr_addr>=add;curr_addr-=0x1000) {
pageTable[SPLIT1_UNSHIFT(curr_addr)][SPLIT2_UNSHIFT(curr_addr)]=counter++;
qdpmm.markUsed((void*)curr_addr);
}
return true;
}
auto PMM::operator >> (void * &addr) -> PMM & {
qdpmm >> addr;
markUsed(addr,4096);
return *this;
}
auto PMM::operator << (const void *addr) -> PMM & {
uint32_t add=(uint32_t)addr;
if(!pageTable[SPLIT1_UNSHIFT(add)])
return *this; //Prevent nullptr derefs
for(int i=0;i<pageTable[SPLIT1_UNSHIFT(add)][SPLIT2_UNSHIFT(add)];i++) {
pageTable[SPLIT1_UNSHIFT(add+i*4096)][SPLIT2_UNSHIFT(add+i*4096)]=0;
qdpmm << (void*)(add+i*4096);
}
return *this;
}
auto PMM::operator()(int pages) -> void* {
//I want to get this working so:
//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
// NEVER USE A BRUTE-FORCE ALGO TO ALLOC PAGES!
//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
for(uint32_t i=0;i<(uint32_t)(-(pages*4096));i+=4096) {
if(markUsed((void*)i,pages*4096))
return (void*)i;
}
return nullptr;
}
auto PMM::init(struct multiboot_info* mb_info) -> void { auto PMM::init(struct multiboot_info* mb_info) -> void {
qdpmm.init(mb_info); pmm2.init(mb_info);
void *temp; }
qdpmm >> temp; auto PMM::alloc(uint32_t length) -> void * {
pageTable=(uint16_t**)temp; if(!head) {
for(int i=0;i<4096;i++) //Alloc space for head
pageTable[i]=nullptr; if(length+sizeof(malloc_t)<=4096) { //Small optimization. The routine for allocating more than one continuous page is terribly slow.
markUsed(pageTable,4096); void *tmp;
markUsed((void*)nullptr,4096); pmm2 >> tmp;
struct multiboot_mmap_entry* mmap = (struct multiboot_mmap_entry*) mb_info->mmap_addr; head=(malloc_t*)tmp;
struct multiboot_mmap_entry* mmap_end = (struct multiboot_mmap_entry*) ((unsigned int) mb_info->mmap_addr + mb_info->mmap_length); } else
while(mmap < mmap_end) { head=(malloc_t*)pmm2(((length+sizeof(malloc_t))>>12)+1);
if(mmap->type != 1) { if(!head) //The alloc() didn't work! We're out of RAM!
markUsed((void*)mmap->addr,mmap->len); return nullptr;
head->len=length;
head->next=head->last=nullptr;
malloc_t* tmp=head;
tmp++;
return (void*)tmp;
} }
mmap++; 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)) {
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;
} }
markUsed(&kernel_start,((uint32_t)&kernel_end)-((uint32_t)&kernel_start)); //Protect kernel) last=curr;
multiboot_mod_list *mods = (multiboot_mod_list*) mb_info->mods_addr; curr=curr->next;
for(uint32_t i=0;i<mb_info->mods_count;i++) { } while(curr);
markUsed((void*)((uint32_t)(&mods[i])&(~0xFFF)),4096); //Mark all of the module table as used malloc_t *allocd=nullptr;
markUsed((void*)mods[i].mod_start,mods[i].mod_end-mods[i].mod_start); 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;
} }
} }

View file

@ -0,0 +1,98 @@
#include <pmm2.hpp>
#include <base.hpp>
//In this part, please remember that the address is split into 3 parts. Bitmap:
// AAAA AAAA ABBB BBBB BBBB CCCC CCCC CCCC
#define SPLIT1_FLAG 0xFF800000ul
#define SPLIT1_SHIFT(a) ((a)<<23)
#define SPLIT1_UNSHIFT(a) ((a)>>23)
#define SPLIT2_FLAG 0x007FF000ul
#define SPLIT2_SHIFT(a) ((a)<<12)
#define SPLIT2_UNSHIFT(a) (((a)<<23)&0x7FF)
#define SPLIT3_FLAG 0x00000FFFul
#define SPLIT3_SHIFT(a) ((a))
#define SPLIT3_UNSHIFT(a) ((a)&0xFFF)
extern "C" const int kernel_start;
extern "C" const int kernel_end; //those are voids actually
namespace MTGosHAL {
PMM2::PMM2(): pmm3() {
}
auto PMM2::markUsed(const void * addr, uint32_t length) -> bool {
uint32_t add=(uint32_t)addr;
uint32_t pagetid = SPLIT1_UNSHIFT(add);
if(length > 256*1024*1024) //Maximum allocation limit is 256MB
return false;
if(!pageTable[pagetid]) {
void* temp;
pmm3 >> temp;
pageTable[pagetid]=(uint16_t*)temp;
for(int i=0;i<2048;i++)
pageTable[pagetid][i]=0;
markUsed(pageTable[pagetid],1024); //Not a mistake
}
//Check if used
for(uint32_t curr_addr=add+length;curr_addr>=add;curr_addr-=0x1000) {
if(pageTable[SPLIT1_UNSHIFT(curr_addr)][SPLIT2_UNSHIFT(curr_addr)])
return false;
}
//Mark as used
uint16_t counter=1;
for(uint32_t curr_addr=add+length;curr_addr>=add;curr_addr-=0x1000) {
pageTable[SPLIT1_UNSHIFT(curr_addr)][SPLIT2_UNSHIFT(curr_addr)]=counter++;
pmm3.markUsed((void*)curr_addr);
}
return true;
}
auto PMM2::operator >> (void * &addr) -> PMM2 & {
pmm3 >> addr;
markUsed(addr,4096);
return *this;
}
auto PMM2::operator << (const void *addr) -> PMM2 & {
uint32_t add=(uint32_t)addr;
if(!pageTable[SPLIT1_UNSHIFT(add)])
return *this; //Prevent nullptr derefs
for(int i=0;i<pageTable[SPLIT1_UNSHIFT(add)][SPLIT2_UNSHIFT(add)];i++) {
pageTable[SPLIT1_UNSHIFT(add+i*4096)][SPLIT2_UNSHIFT(add+i*4096)]=0;
pmm3 << (void*)(add+i*4096);
}
return *this;
}
auto PMM2::operator()(int pages) -> void* {
//I want to get this working so:
//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
// NEVER USE A BRUTE-FORCE ALGO TO ALLOC PAGES!
//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
for(uint32_t i=0;i<(uint32_t)(-(pages*4096));i+=4096) {
if(markUsed((void*)i,pages*4096))
return (void*)i;
}
return nullptr;
}
auto PMM2::init(struct multiboot_info* mb_info) -> void {
pmm3.init(mb_info);
void *temp;
pmm3 >> temp;
pageTable=(uint16_t**)temp;
for(int i=0;i<4096;i++)
pageTable[i]=nullptr;
markUsed(pageTable,4096);
markUsed((void*)nullptr,4096);
struct multiboot_mmap_entry* mmap = (struct multiboot_mmap_entry*) mb_info->mmap_addr;
struct multiboot_mmap_entry* mmap_end = (struct multiboot_mmap_entry*) ((unsigned int) mb_info->mmap_addr + mb_info->mmap_length);
while(mmap < mmap_end) {
if(mmap->type != 1) {
markUsed((void*)mmap->addr,mmap->len);
}
mmap++;
}
markUsed(&kernel_start,((uint32_t)&kernel_end)-((uint32_t)&kernel_start)); //Protect kernel)
multiboot_mod_list *mods = (multiboot_mod_list*) mb_info->mods_addr;
for(uint32_t i=0;i<mb_info->mods_count;i++) {
markUsed((void*)((uint32_t)(&mods[i])&(~0xFFF)),4096); //Mark all of the module table as used
markUsed((void*)mods[i].mod_start,mods[i].mod_end-mods[i].mod_start);
}
}
}

View file

@ -1,12 +1,12 @@
#include <stdint.h> #include <stdint.h>
#include <base.hpp> #include <base.hpp>
#include <qdpmm.hpp> #include <pmm3.hpp>
#include <multiboot.h> #include <multiboot.h>
extern "C" const int kernel_start; extern "C" const int kernel_start;
extern "C" const int kernel_end; //those are voids actually extern "C" const int kernel_end; //those are voids actually
namespace MTGosHAL { namespace MTGosHAL {
QDPMM::QDPMM() {} PMM3::PMM3() {}
auto QDPMM::init(struct multiboot_info * mb_info) -> void { auto PMM3::init(struct multiboot_info * mb_info) -> void {
for(int i=0;i<0x8000;i++) for(int i=0;i<0x8000;i++)
bitmap[i]=0; bitmap[i]=0;
struct multiboot_mmap_entry* mmap = (struct multiboot_mmap_entry*) mb_info->mmap_addr; struct multiboot_mmap_entry* mmap = (struct multiboot_mmap_entry*) mb_info->mmap_addr;
@ -37,14 +37,14 @@ auto QDPMM::init(struct multiboot_info * mb_info) -> void {
} }
} }
} }
auto QDPMM::markUsed(const void * addr) -> void { auto PMM3::markUsed(const void * addr) -> void {
unsigned int address=(unsigned int)addr; unsigned int address=(unsigned int)addr;
address>>=12; address>>=12;
int index=address>>5; int index=address>>5;
int bit=1<<(address&0x1F); int bit=1<<(address&0x1F);
bitmap[index]&=~bit; bitmap[index]&=~bit;
} }
auto QDPMM::operator >> (void * &addr) -> QDPMM & { auto PMM3::operator >> (void * &addr) -> PMM3 & {
for(int i=0;i<0x8000;i++) { for(int i=0;i<0x8000;i++) {
if(!bitmap[i]) if(!bitmap[i])
continue; continue;
@ -60,7 +60,7 @@ auto QDPMM::operator >> (void * &addr) -> QDPMM & {
addr=nullptr; addr=nullptr;
return *this; return *this;
} }
auto QDPMM::operator << (const void * addr) -> QDPMM & { auto PMM3::operator << (const void * addr) -> PMM3 & {
unsigned int address=(unsigned int)addr; unsigned int address=(unsigned int)addr;
address>>=12; address>>=12;
int index=address>>5; int index=address>>5;

BIN
mtgos.old Executable file

Binary file not shown.

BIN
test.elf

Binary file not shown.

View file

@ -1,6 +1,6 @@
static unsigned short* videomem = (unsigned short*) 0xb8000;
void temp() { void temp() {
static unsigned short* videomem = (unsigned short*) 0xb8000;
int i; int i;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
*videomem++ = (0x07 << 8) | ('0' + i); *videomem++ = (0x07 << 8) | ('0' + i);