diff --git a/buildcrosscompiler.sh b/buildcrosscompiler.sh index 1ec85a2..b518946 100644 --- a/buildcrosscompiler.sh +++ b/buildcrosscompiler.sh @@ -14,7 +14,7 @@ function buildscript() { echo "[$(date +%c)] Building gcc for $1." | tee -a buildlog mkdir build-gcc cd build-gcc - ../gcc-5.3.0/configure --prefix=$CROSSPATH --target=$1 --disable-nls --enable-languages=c,c++ --without-headers 2>&1 > /dev/null + ../gcc-6.1.0/configure --prefix=$CROSSPATH --target=$1 --disable-nls --enable-languages=c,c++ --without-headers 2>&1 > /dev/null make all-gcc -j8 2>&1 > /dev/null make all-target-libgcc -j8 2>&1 > /dev/null make install-gcc 2>&1 > /dev/null @@ -27,17 +27,21 @@ tempdir=$(mktemp -d) cd $tempdir echo "Temponary files are in $tempdir. Build log can be found under $tempdir/buildlog" | tee -a buildlog echo "Downloading GCC, Binutils, MPC, MPFR and GMP" | tee -a buildlog -wget ftp://ftp.gnu.org/gnu/gcc/gcc-5.3.0/gcc-5.3.0.tar.bz2 ftp://ftp.gnu.org/gnu/binutils/binutils-2.26.tar.bz2 ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz ftp://ftp.gnu.org/gnu/mpfr/mpfr-3.1.3.tar.xz ftp://ftp.gnu.org/gnu/gmp/gmp-6.1.0.tar.xz +wget ftp://ftp.gnu.org/gnu/gcc/gcc-6.1.0/gcc-6.1.0.tar.bz2 ftp://ftp.gnu.org/gnu/binutils/binutils-2.26.tar.bz2 ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz ftp://ftp.gnu.org/gnu/mpfr/mpfr-3.1.3.tar.xz ftp://ftp.gnu.org/gnu/gmp/gmp-6.1.0.tar.xz http://isl.gforge.inria.fr/isl-0.16.tar.xz http://www.bastoul.net/cloog/pages/download/cloog-0.18.4.tar.gz echo "Untaring..." 2>&1 | tee -a buildlog -tar -xf gcc-5.3.0.tar.bz2 +tar -xf gcc-6.1.0.tar.bz2 tar -xf binutils-2.26.tar.bz2 -cd gcc-5.3.0 +cd gcc-6.1.0 tar -xf ../mpc-1.0.3.tar.gz mv mpc-1.0.3 mpc tar -xf ../mpfr-3.1.3.tar.xz mv mpfr-3.1.3 mpfr tar -xf ../gmp-6.1.0.tar.xz mv gmp-6.1.0 gmp +tar -xf ../isl-0.16.tar.xz +mv isl-0.16 isl +tar -xf ../cloog-0.18.4.tar.gz +mv cloog-0.18.4 cloog cd .. echo "Preperation done. Beginning the compilation now." 2>&1 | tee -a buildlog buildscript i686-elf #x86 port diff --git a/kernel/hal/dummy/include/pmm.hpp b/kernel/hal/dummy/include/pmm.hpp index 4b17591..96d33ef 100644 --- a/kernel/hal/dummy/include/pmm.hpp +++ b/kernel/hal/dummy/include/pmm.hpp @@ -5,12 +5,14 @@ namespace MTGosHAL { class PMM { private: uint32_t bitmap[0x8000]; //Enough for 4 GB - auto markUsed(void * addr) -> void; public: - PMM(); - auto operator >> (void * &addr) -> PMM &; //alloc - auto operator << (const void * addr) -> PMM &; //free - auto operator()(int pages) -> void*; //alloc_multipage + template + auto markUsed(T * addr) -> void; + auto init(struct multiboot_info*) -> void; + template + auto operator >> (T * &addr) -> QDPMM &; //alloc + template + auto operator << (const T * addr) -> QDPMM &; //free }; } diff --git a/kernel/hal/x86/include/base.hpp b/kernel/hal/x86/include/base.hpp index affce63..1256123 100644 --- a/kernel/hal/x86/include/base.hpp +++ b/kernel/hal/x86/include/base.hpp @@ -12,7 +12,7 @@ namespace MTGosHAL { class Multitasking; class BlockDevice; class Task; - class PMM; + class QDPMM; enum class BG_color: uint16_t; enum class FG_color: uint16_t; extern Serial debug; @@ -23,6 +23,6 @@ namespace MTGosHAL { extern IDT idt; extern Multitasking tasks; extern BlockDevice disk; - extern PMM mm; + extern QDPMM qdmm; } #endif diff --git a/kernel/hal/x86/include/pmm.hpp b/kernel/hal/x86/include/pmm.hpp index 2d8d089..29e10c9 100644 --- a/kernel/hal/x86/include/pmm.hpp +++ b/kernel/hal/x86/include/pmm.hpp @@ -1,19 +1,23 @@ #ifndef _PMM_HPP #define _PMM_HPP +#include #include #include namespace MTGosHAL { class PMM { private: - uint32_t bitmap[0x8000]; //Enough for 4 GB + uint16_t **pageTable; + QDPMM qdpmm; auto markUsed(void * addr) -> void; public: PMM(); - auto init(struct multiboot_info*) -> void; - auto operator >> (void * &addr) -> PMM &; //alloc - auto operator << (const void * addr) -> PMM &; //free - auto operator()(int pages) -> void*; //alloc_multipage - + template + auto markUsed(T * addr, uint32_t length) -> void; + auto init(struct multiboot_info*) -> void; + template + auto operator >> (T * &addr) -> QDPMM &; //alloc + template + auto operator << (const T * addr) -> QDPMM &; //free }; } #endif diff --git a/kernel/hal/x86/include/qdpmm.hpp b/kernel/hal/x86/include/qdpmm.hpp new file mode 100644 index 0000000..b87a4ae --- /dev/null +++ b/kernel/hal/x86/include/qdpmm.hpp @@ -0,0 +1,21 @@ +#ifndef _QDPMM_HPP +#define _QDPMM_HPP +#include +#include +namespace MTGosHAL { +class QDPMM { +private: + uint32_t bitmap[0x8000]; //Enough for 4 GB +protected: +public: + QDPMM(); + template + auto markUsed(T * addr) -> void; + auto init(struct multiboot_info*) -> void; + template + auto operator >> (T * &addr) -> QDPMM &; //alloc + template + auto operator << (const T * addr) -> QDPMM &; //free +}; +} +#endif diff --git a/kernel/hal/x86/init/Multitasking.cpp b/kernel/hal/x86/init/Multitasking.cpp index 94443d1..c7477c6 100644 --- a/kernel/hal/x86/init/Multitasking.cpp +++ b/kernel/hal/x86/init/Multitasking.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include auto schedule(struct cpu_state* cpu) -> struct cpu_state* { return MTGosHAL::tasks.schedule(cpu); } @@ -21,14 +21,14 @@ Multitasking::Multitasking(): curr_task(nullptr), first_task(nullptr) //task_states[1] = initTask(stack_b, user_stack_b, task_b); if(!idt.request(0x20,::schedule)) { err << "Could not start multitasking\nFatal error; Kernel halted!\n"; - while(true); - asm volatile("cli; hlt"); + while(true) + asm volatile("cli; hlt"); } } auto Multitasking::initTask(void(* entry)()) -> struct cpu_state* { void *tmp1, *tmp2; - mm >> tmp1 >> tmp2; + qdmm >> tmp1 >> tmp2; uint8_t *stack=(uint8_t*)tmp1, *user_stack=(uint8_t*)tmp2; struct cpu_state new_state = { 0, //EAX diff --git a/kernel/hal/x86/init/init.cpp b/kernel/hal/x86/init/init.cpp index 2786739..bb80975 100644 --- a/kernel/hal/x86/init/init.cpp +++ b/kernel/hal/x86/init/init.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include extern "C" void intr_stub_0(void); void main(void ** programs); namespace MTGosHAL { @@ -20,7 +20,7 @@ namespace MTGosHAL { GDT gdt; Multitasking tasks; BlockDevice disk; - PMM mm; + QDPMM qdmm; void main(int eax, struct multiboot_info* ebx) { out << BG_color::BLACK << FG_color::WHITE << "Loading MTGos...\n"; err << BG_color::BLACK << FG_color::RED; @@ -43,16 +43,18 @@ namespace MTGosHAL { idt.setEntry(48, (void *)((uint32_t)&intr_stub_0+768), SEG_KERNEL, IDT_TRAP_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(); - mm.init(ebx); + qdmm.init(ebx); multiboot_mod_list *mods = (multiboot_mod_list*) ebx->mods_addr; void** progs=nullptr; void* tmp; - mm >> tmp; + qdmm >> tmp; progs=(void**)tmp; 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. progs[i]=(void*)(mods[i].mod_start); + debug << "Found module!\n"; } + i++; progs[i]=nullptr; ::main(progs); sti(); diff --git a/kernel/hal/x86/mm/pmm.cpp b/kernel/hal/x86/mm/pmm.cpp index eea4f9b..78844fd 100644 --- a/kernel/hal/x86/mm/pmm.cpp +++ b/kernel/hal/x86/mm/pmm.cpp @@ -1,99 +1,43 @@ -#include -#include #include -#include -extern "C" const int kernel_start; -extern "C" const int kernel_end; //those are voids actually -void *operator new(size_t size) { - if(size>4096) { - asm("int $0x1F"); - } - void *ptr; - MTGosHAL::mm >> ptr; - return ptr; -} -void *operator new[](size_t size) { - if(size>4096) { - asm("int $0x1F"); - } - void *ptr; - MTGosHAL::mm >> ptr; - return ptr; -} -void operator delete(void* p) { - MTGosHAL::mm << p; -} -void operator delete[](void* p) { - MTGosHAL::mm << p; -} -void operator delete(void* p, size_t size) { - MTGosHAL::mm << p; -} -void operator delete[](void* p, size_t size) { - MTGosHAL::mm << p; -} +//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 namespace MTGosHAL { -PMM::PMM() {} -auto PMM::init(struct multiboot_info * mb_info) -> void { - for(int i=0;i<0x8000;i++) - bitmap[i]=0; - 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) { - // Memory is free - uintptr_t addr = mmap->addr; - uintptr_t end_addr = addr + mmap->len; - while (addr < end_addr) { - *this << (void*) addr; - addr += 0x1000; - } - } - mmap++; - } - unsigned int addr = (unsigned int) &kernel_start; - while(addr < (unsigned int) &kernel_end) { - markUsed((void*)addr); - addr+=0x1000; - } - markUsed(nullptr); - multiboot_mod_list *mods = (multiboot_mod_list*) mb_info->mods_addr; - for(uint32_t i=0;imods_count;i++) { - markUsed((void*)((uint32_t)(&mods[i])&(~0xFFF))); //Mark all of the module table as used - for(uint32_t start=(uint32_t)(mods[i].mod_start)&(~0xFFF);start<(uint32_t)(mods[i].mod_end);start+=0x1000) { - markUsed((void*)start); //Protect all multiboot modules - } - } +PMM::PMM(): qdpmm() { } -auto PMM::markUsed(void * addr) -> void { - unsigned int address=(unsigned int)addr; - address>>=12; - int index=address>>5; - int bit=1<<(address&0x1F); - bitmap[index]&=~bit; +template +auto PMM::markUsed(T * addr, uint32_t length) -> void { + uint32_t add=(uint32_t)addr; + uint32_t pagetid = SPLIT1_UNSHIFT(add); + if(length > 256*1024*1024) //Maximum allocation limit is 256MB + return; + if(!pageTable[pagetid]) { + void* temp; + qdpmm >> temp; + pageTable[pagetid]=(uint16_t*)temp; + markUsed(pageTable[pagetid],1024); //Not a mistake + } + 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); + } } -auto PMM::operator >> (void * &addr) -> PMM & { - for(int i=0;i<0x8000;i++) { - if(!bitmap[i]) - continue; - for(int j=0;j<32;j++) { - if(bitmap[i]&(1< PMM & { - unsigned int address=(unsigned int)addr; - address>>=12; - int index=address>>5; - int bit=1<<(address&0x1F); - bitmap[index]|=bit; - return *this; +auto PMM::init(struct multiboot_info* mb_info) -> void { + qdpmm.init(mb_info); + void *temp; + qdpmm >> temp; + pageTable=(uint16_t**)temp; + markUsed(pageTable,1024); + for(int i=0;i<4096;i++) + pageTable[i]=nullptr; } } diff --git a/kernel/hal/x86/mm/qdmm.cpp b/kernel/hal/x86/mm/qdmm.cpp new file mode 100644 index 0000000..9561110 --- /dev/null +++ b/kernel/hal/x86/mm/qdmm.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +extern "C" const int kernel_start; +extern "C" const int kernel_end; //those are voids actually +void *operator new(size_t size) { + if(size>4096) { + asm("int $0x1F"); + } + void *ptr; + MTGosHAL::qdmm >> ptr; + return ptr; +} +void *operator new[](size_t size) { + if(size>4096) { + asm("int $0x1F"); + } + void *ptr; + MTGosHAL::qdmm >> ptr; + return ptr; +} +void operator delete(void* p) { + MTGosHAL::qdmm << p; +} +void operator delete[](void* p) { + MTGosHAL::qdmm << p; +} +void operator delete(void* p, size_t size) { + MTGosHAL::qdmm << p; +} +void operator delete[](void* p, size_t size) { + MTGosHAL::qdmm << p; +} +namespace MTGosHAL { +QDPMM::QDPMM() {} +auto QDPMM::init(struct multiboot_info * mb_info) -> void { + for(int i=0;i<0x8000;i++) + bitmap[i]=0; + 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) { + // Memory is free + uintptr_t addr = mmap->addr; + uintptr_t end_addr = addr + mmap->len; + while (addr < end_addr) { + *this << (void*) addr; + addr += 0x1000; + } + } + mmap++; + } + unsigned int addr = (unsigned int) &kernel_start; + while(addr < (unsigned int) &kernel_end) { + markUsed((void*)addr); + addr+=0x1000; + } + markUsed((void*)0); + multiboot_mod_list *mods = (multiboot_mod_list*) mb_info->mods_addr; + for(uint32_t i=0;imods_count;i++) { + markUsed((void*)((uint32_t)(&mods[i])&(~0xFFF))); //Mark all of the module table as used + for(uint32_t start=(uint32_t)(mods[i].mod_start)&(~0xFFF);start<(uint32_t)(mods[i].mod_end);start+=0x1000) { + markUsed((void*)start); //Protect all multiboot modules + } + } +} +template +auto QDPMM::markUsed(T * addr) -> void { + unsigned int address=(unsigned int)addr; + address>>=12; + int index=address>>5; + int bit=1<<(address&0x1F); + bitmap[index]&=~bit; +} +template +auto QDPMM::operator >> (T * &addr) -> QDPMM & { + for(int i=0;i<0x8000;i++) { + if(!bitmap[i]) + continue; + for(int j=0;j<32;j++) { + if(bitmap[i]&(1< +auto QDPMM::operator << (const T * addr) -> QDPMM & { + unsigned int address=(unsigned int)addr; + address>>=12; + int index=address>>5; + int bit=1<<(address&0x1F); + bitmap[index]|=bit; + return *this; +} +} diff --git a/kernel/kernel/elf.cpp b/kernel/kernel/elf.cpp index fb5cd68..29afdeb 100644 --- a/kernel/kernel/elf.cpp +++ b/kernel/kernel/elf.cpp @@ -1,16 +1,35 @@ #include +#include +#include +#include +using namespace MTGosHAL; auto load(Elf32_Ehdr* file) -> void* { if((file->e_ident[0]!=ELFMAG0)|| (file->e_ident[1]!=ELFMAG1)|| (file->e_ident[2]!=ELFMAG2)|| - (file->e_ident[3]!=ELFMAG3) ) + (file->e_ident[3]!=ELFMAG3) ) { return nullptr; + debug << "File is not a valid ELF file!\n"; + } Elf32_Phdr *phdr = (Elf32_Phdr*)((uint32_t)(file->e_phoff)+(uint32_t)file); + debug << "entry="; + debug << Base::HEXADECIMAL; + debug << (int)file->e_entry; for(int i=0;ie_phnum;i++) { uint32_t start=phdr[i].p_vaddr; + debug << "start="; + debug << Base::HEXADECIMAL; + debug << (int)start; uint32_t end=start+phdr[i].p_memsz; - if((start < file->e_entry) && (file->e_entry < end)) + debug << "end="; + debug << Base::HEXADECIMAL; + debug << (int)end; + if((start <= file->e_entry) && (file->e_entry < end)) { + debug << "start="; + debug << Base::HEXADECIMAL; + debug << (int)(file->e_entry-start+phdr[i].p_offset+(uint32_t)file); return (void*) (file->e_entry-start+phdr[i].p_offset+(uint32_t)file); //Calculate _start address + } } return nullptr; } diff --git a/test.elf b/test.elf index 46afc35..d9d6f5e 100755 Binary files a/test.elf and b/test.elf differ diff --git a/user/test.c b/user/test.c index 22988e8..3f86876 100644 --- a/user/test.c +++ b/user/test.c @@ -1,11 +1,15 @@ -unsigned short* videomem = (unsigned short*) 0xb8000; -void _start(void) -{ - int i; +void temp() { + static unsigned short* videomem = (unsigned short*) 0xb8000; + int i; for (i = 0; i < 3; i++) { *videomem++ = (0x07 << 8) | ('0' + i); } - - while(1); +} +void _start(void) +{ + int i; + for(i=0;i<100;i++) + temp(); + while(1); }