From e1203d82717ce3f4bb77a031bf9e28ffab98b9ef Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Sat, 6 Feb 2016 19:57:44 +0100 Subject: [PATCH] Added an unfinished PMM. Breaks build --- buildcrosscompiler.sh | 20 +++++++++---------- kernel/hal/x86/Makefile | 4 ++-- kernel/hal/x86/include/pmm.hpp | 17 +++++++++++++++++ kernel/hal/x86/init/init.cpp | 5 +++++ kernel/hal/x86/mm/pmm.cpp | 35 ++++++++++++++++++++++++++++++++++ kernel/kernel.ld | 3 +++ kernel/kernel/Makefile | 4 ++-- 7 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 kernel/hal/x86/include/pmm.hpp create mode 100644 kernel/hal/x86/mm/pmm.cpp diff --git a/buildcrosscompiler.sh b/buildcrosscompiler.sh index aa63aa1..eb05e99 100644 --- a/buildcrosscompiler.sh +++ b/buildcrosscompiler.sh @@ -1,12 +1,11 @@ -CROSSPATH=$HOME/opt # You can change it to whatever you want +CROSSPATH=/usr/local # You can change it to whatever you want export PATH=$CROSSPATH/bin:$PATH function buildscript() { - date "[+%c] " | tr -d '\n' | tee buildlog - echo "Building binutils for $1" | tee buildlog + echo "[$(date +%c)] Building binutils for $1" | tee buildlog mkdir build-binutils cd build-binutils ../binutils-2.26/configure --prefix=$CROSSPATH --target=$1 --with-sysroot --disable-nls --disable-werror - make -j2 + make -j8 make install cd .. rm -rf build-binutils @@ -15,8 +14,8 @@ function buildscript() { mkdir build-gcc cd build-gcc ../gcc-5.3.0/configure --prefix=$CROSSPATH --target=$1 --disable-nls --enable-languages=c,c++ --without-headers - make all-gcc - make all-target-libgcc + make all-gcc -j8 + make all-target-libgcc -j8 make install-gcc make install-target-libgcc cd .. @@ -33,11 +32,11 @@ echo "Untaring..." tar -xf gcc-5.3.0.tar.bz2 tar -xf binutils-2.26.tar.bz2 cd gcc-5.3.0 -tar -xf mpc-1.0.3.tar.gz +tar -xf ../mpc-1.0.3.tar.gz mv mpc-1.0.3 mpc -tar -xf mpfr-3.1.3.tar.xz +tar -xf ../mpfr-3.1.3.tar.xz mv mpfr-3.1.3 mpfr -tar -xf gmp-6.1.0.tar.xz +tar -xf ../gmp-6.1.0.tar.xz mv gmp-6.1.0 gmp cd .. echo "Preperation done. Beginning the compilation now." @@ -45,5 +44,6 @@ buildscript i686-elf #x86 port buildscript arm-none-eabi #ARM ports (3DS, pi) buildscript armeb-eabi #Wii port buildscript ppc-elf #Wii port +buildscript x86_64-elf #x86_64 port rm -rf gcc* binutils* mpc* mpfr* gmp* -echo "Done! Have fun with your cross compilers!" \ No newline at end of file +echo "Done! Have fun with your cross compilers!" diff --git a/kernel/hal/x86/Makefile b/kernel/hal/x86/Makefile index 39d8554..04e9200 100644 --- a/kernel/hal/x86/Makefile +++ b/kernel/hal/x86/Makefile @@ -5,8 +5,8 @@ OBJS = $(addsuffix .o,$(basename $(SRCS))) CPP = $(PREFIX)g++ CC = $(PREFIX)gcc ASFLAGS = -m32 -CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -march=native -std=c11 -fno-builtin -Werror -nostdlib -g -CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g +CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -march=native -std=c11 -fno-builtin -Werror -nostdlib -g -fpie +CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fpie LDFLAGS = -r -melf_i386 diff --git a/kernel/hal/x86/include/pmm.hpp b/kernel/hal/x86/include/pmm.hpp new file mode 100644 index 0000000..300abc2 --- /dev/null +++ b/kernel/hal/x86/include/pmm.hpp @@ -0,0 +1,17 @@ +#ifndef _PMM_HPP +#define _PMM_HPP +#include +#include +class PMM { +private: + uint32_t bitmap[0x8000]; //Enough for 4 GB + PMM() {} + auto markUsed(void * addr) -> void; +public: + PMM(struct multiboot_info*); + auto operator >> (void * &addr) -> PMM &; //alloc + auto operator << (const void * addr) -> PMM &; //free + auto operator()(int pages) -> void*; //alloc_multipage + +}; +#endif \ No newline at end of file diff --git a/kernel/hal/x86/init/init.cpp b/kernel/hal/x86/init/init.cpp index 317c95e..99141f2 100644 --- a/kernel/hal/x86/init/init.cpp +++ b/kernel/hal/x86/init/init.cpp @@ -44,12 +44,17 @@ namespace MTGosHAL { } } typedef void (*constructor)(); +typedef void (*destructor)(); extern "C" constructor start_ctors; extern "C" constructor end_ctors; +extern "C" destructor start_dtors; +extern "C" destructor end_dtors; extern "C" void init(int eax, struct multiboot_info* ebx) { for(constructor* i = &start_ctors; i != &end_ctors; ++i) (*i)(); MTGosHAL::main(eax, ebx); + for(destructor* i = &start_dtors; i != &end_dtors; i++) + (*i)(); } extern "C" void __cxa_pure_virtual() { MTGosHAL::debug << "A pure virtual function just got called.\n"; diff --git a/kernel/hal/x86/mm/pmm.cpp b/kernel/hal/x86/mm/pmm.cpp new file mode 100644 index 0000000..89a2b4d --- /dev/null +++ b/kernel/hal/x86/mm/pmm.cpp @@ -0,0 +1,35 @@ +#include +#include +extern "C" const int kernel_start; +extern "C" const int kernel_end; //those are voids actually +PMM::PMM(struct multiboot_info * mb_info) { + 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) { + // Der Speicherbereich ist frei, entsprechend markieren + 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; + } +} +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; +} +//auto PMM::operator >> (void * &addr) -> PMM &; \ No newline at end of file diff --git a/kernel/kernel.ld b/kernel/kernel.ld index 4a93b4d..0bff516 100644 --- a/kernel/kernel.ld +++ b/kernel/kernel.ld @@ -4,6 +4,7 @@ OUTPUT_ARCH(i386:i386) SECTIONS { . = 0x100000; + kernel_start = .; .text : { *(multiboot) *(.text) @@ -25,4 +26,6 @@ SECTIONS .bss ALIGN(4096) : { *(.bss) } + . = ALIGN(4096); + kernel_end = .; } diff --git a/kernel/kernel/Makefile b/kernel/kernel/Makefile index ed799b3..a671a1d 100644 --- a/kernel/kernel/Makefile +++ b/kernel/kernel/Makefile @@ -4,8 +4,8 @@ OBJS = $(addsuffix .o,$(basename $(SRCS))) CPP = $(PREFIX)g++ CC = $(PREFIX)gcc ASFLAGS = -m32 -CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -I../hal/dummy/c_include -ffreestanding -march=native -std=c11 -fno-builtin -Werror -nostdlib -g -CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -I../hal/dummy/include -I../hal/dummy/c_include -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g +CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -I../hal/dummy/c_include -ffreestanding -march=native -std=c11 -fno-builtin -Werror -nostdlib -g -fpie +CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -I../hal/dummy/include -I../hal/dummy/c_include -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fpie all: $(OBJS) %.o: %.cpp