From e0c565f7ff7620dca9dfc6c607f4798f5291c7bf Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Tue, 28 Jul 2015 14:19:00 +0400 Subject: [PATCH] Go back to C and adapt code --- Readme.md | 4 +- kernel/GDT/Makefile | 4 +- kernel/GDT/gdt.c | 50 +++++++++++++ kernel/GDT/gdt.cpp | 58 --------------- kernel/GDT/gdt.h | 37 ++++++++++ kernel/GDT/gdt.hpp | 55 -------------- kernel/Helpers/Makefile | 14 ++-- kernel/Helpers/globalVars.hpp | 0 kernel/Helpers/memPrint.hpp | 81 --------------------- kernel/Helpers/{memory.cpp => memory.c} | 4 +- kernel/Helpers/{memory.hpp => memory.h} | 2 +- kernel/Helpers/{memPrint.cpp => memprint.c} | 70 +++++++----------- kernel/Helpers/memprint.h | 59 +++++++++++++++ kernel/Helpers/{types.hpp => types.h} | 0 kernel/Makefile | 18 +++-- kernel/entry.asm | 1 - kernel/main.c | 41 +++++++++++ kernel/main.cpp | 52 ------------- 18 files changed, 242 insertions(+), 308 deletions(-) create mode 100644 kernel/GDT/gdt.c delete mode 100644 kernel/GDT/gdt.cpp create mode 100644 kernel/GDT/gdt.h delete mode 100644 kernel/GDT/gdt.hpp delete mode 100644 kernel/Helpers/globalVars.hpp delete mode 100644 kernel/Helpers/memPrint.hpp rename kernel/Helpers/{memory.cpp => memory.c} (91%) rename kernel/Helpers/{memory.hpp => memory.h} (84%) rename kernel/Helpers/{memPrint.cpp => memprint.c} (51%) create mode 100644 kernel/Helpers/memprint.h rename kernel/Helpers/{types.hpp => types.h} (100%) create mode 100644 kernel/main.c delete mode 100644 kernel/main.cpp diff --git a/Readme.md b/Readme.md index 19fdfd1..42bd89b 100644 --- a/Readme.md +++ b/Readme.md @@ -7,13 +7,13 @@ ##Programmers Zone -> Langages utilisés: nasm, C++, AT&T ... +> Langages utilisés: nasm, C, AT&T ... ##Utilitaires requis pour la compilation > Shell Unix (avec dd, cp etc...)
> Make
-> G++, ld etc...
+> GCC, ld etc...
> Nasm ##Comment utilisé le Noyaux Générer ? diff --git a/kernel/GDT/Makefile b/kernel/GDT/Makefile index 4f5192b..0f08954 100644 --- a/kernel/GDT/Makefile +++ b/kernel/GDT/Makefile @@ -4,8 +4,8 @@ EXEC=gdt.o all:$(EXEC) #----- GDT ----- -$(EXEC): gdt.cpp - $(CXX) -c -o $(EXEC) gdt.cpp +$(EXEC): gdt.c + $(CC) $(CFLAGS) -c -o $(EXEC) $^ #--------------- diff --git a/kernel/GDT/gdt.c b/kernel/GDT/gdt.c new file mode 100644 index 0000000..4aae655 --- /dev/null +++ b/kernel/GDT/gdt.c @@ -0,0 +1,50 @@ +#include "gdt.h" +#include "../Helpers/types.h" +#include "../Helpers/memory.h" + +//Declare global vars +GdtDescriptorStruct GDT_DESCRIPTORS[4]; +GdtPointerStruct GDT_POINTER; + +//Adapt parameter to the gdt descriptor structure +void gdt_initGdtDesc(u32 base, u32 limit, u8 access, u8 flags, GdtDescriptorStruct *Descriptor){ + Descriptor->limit1 = limit & 0xFFFF; + Descriptor->base1 = base & 0xFFFF; + Descriptor->base2 = (base & 0xFF0000) >> 16; + Descriptor->access = access; + Descriptor->limit2 = (limit & 0xF0000 ) >> 16; + Descriptor->flags = flags & 0xFF; + Descriptor->base3 = (base & 0xFF000000) >> 24; +} + +//Copy the gdt into mémory and load it +void gdt_loadGdt(){ + + //Init default segment + gdt_initGdtDesc(0x0,0x0,0x0,0x0, &GDT_DESCRIPTORS[0]); + + //Init code segment + gdt_initGdtDesc(0x0,0xFFFFF,0x9A,0x0D, &GDT_DESCRIPTORS[1]); + + //Init data segment + gdt_initGdtDesc(0x0,0xFFFFF,0x92,0x0D, &GDT_DESCRIPTORS[2]); + + //Init stack segment + gdt_initGdtDesc(0x00B00000,0x00000500,0x96,0x0D, &GDT_DESCRIPTORS[3]); + + + //Init GDT Pointer + GDT_POINTER.size=4*sizeof(GDT_DESCRIPTORS); + GDT_POINTER.segment=0x00007E00; + + //Copy Gdt into memory and init registers + memcpy((u32)GDT_DESCRIPTORS, (u32)GDT_POINTER.segment, (u32)GDT_POINTER.size); + + //Load the GDT + __asm__("lgdtl (%0);" + : + :"r"(&GDT_POINTER) + ); +} + + diff --git a/kernel/GDT/gdt.cpp b/kernel/GDT/gdt.cpp deleted file mode 100644 index 8c6d9d9..0000000 --- a/kernel/GDT/gdt.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "gdt.hpp" -#include "../Helpers/types.hpp" -#include "../Helpers/memory.hpp" - -//Constructor -Gdt::Gdt(){ - - //Init conventional segment - this->initGdtDesc(0x0,0x0,0x0,0x0, &m_Descriptors[0]); - - //Init code segment - this->initGdtDesc(0x0,0xFFFFF,0x9A,0x0D, &m_Descriptors[1]); - - //Init data segment - this->initGdtDesc(0x0,0xFFFFF,0x92,0x0D, &m_Descriptors[2]); - - //Init stack segment - this->initGdtDesc(0x00B00000,0x00000500,0x96,0x0D, &m_Descriptors[3]); - - - //Init GDT Pointer - this->m_Pointer.size=4*sizeof(this->m_Descriptors); - this->m_Pointer.segment=0x00007E00; - - -} - -//Destructor -Gdt::~Gdt(){ - -} - -//Adapt parameter to the gdt descriptor structure -void Gdt::initGdtDesc(u32 base, u32 limit, u8 access, u8 flags, gdtDescriptorStruct *Descriptor){ - Descriptor->limit1 = limit & 0xFFFF; - Descriptor->base1 = base & 0xFFFF; - Descriptor->base2 = (base & 0xFF0000) >> 16; - Descriptor->access = access; - Descriptor->limit2 = (limit & 0xF0000 ) >> 16; - Descriptor->flags = flags & 0xFF; - Descriptor->base3 = (base & 0xFF000000) >> 24; -} - -//Copy the gdt into mémory and load it -void Gdt::loadGdt(){ - //Copy Gdt into memory and init registers - memcpy((u32)m_Descriptors, (u32)m_Pointer.segment, (u32)m_Pointer.size); - - //Put m_Pointer in a var to pass it to assembly code - int *gdtAdress=(int *)&m_Pointer; - - __asm__("lgdtl (%0);" - : - :"r"(gdtAdress) - ); -} - - diff --git a/kernel/GDT/gdt.h b/kernel/GDT/gdt.h new file mode 100644 index 0000000..86cdaf3 --- /dev/null +++ b/kernel/GDT/gdt.h @@ -0,0 +1,37 @@ +#ifndef __GDT__ +#define __GDT__ + +#include "../Helpers/types.h" + + +//Define GDT pointer +struct GdtPointerStruct{ + u16 size; + u32 segment; +} __attribute__ ((packed)); + + +//Define GDT descriptor +struct GdtDescriptorStruct{ + u16 limit1; + u16 base1; + u8 base2; + u8 access; + u8 limit2 : 4; + u8 flags : 4; + u8 base3; +} __attribute__ ((packed)); + + +//Typedef : +typedef struct GdtPointerStruct GdtPointerStruct; +typedef struct GdtDescriptorStruct GdtDescriptorStruct; + + +//Functions : +void gdt_initGdtDesc(u32 base, u32 limit, u8 access, u8 flags, GdtDescriptorStruct *Descriptor); +void gdt_loadGdt(); + + + +#endif diff --git a/kernel/GDT/gdt.hpp b/kernel/GDT/gdt.hpp deleted file mode 100644 index f6caa3d..0000000 --- a/kernel/GDT/gdt.hpp +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef __GDT__ -#define __GDT__ - -#include "../Helpers/types.hpp" - -//Define GDT pointer -struct gdtPointerStruct{ - u16 size; - u32 segment; -} __attribute__ ((packed)); - -//Define GDT descriptor -struct gdtDescriptorStruct{ - u16 limit1; - u16 base1; - u8 base2; - u8 access; - u8 limit2 : 4; - u8 flags : 4; - u8 base3; -} __attribute__ ((packed)); - -//Typedef : -typedef struct gdtPointerStruct gdtPointerStruct; -typedef struct gdtDescriptorStruct gdtDescriptorStruct; - - -//Gdt class -class Gdt{ - - private: - - //Data members - gdtDescriptorStruct m_Descriptors[4]; - gdtPointerStruct m_Pointer; - - //Methods - void initGdtDesc(u32 base, u32 limit, u8 access, u8 flags, gdtDescriptorStruct *Descriptor); - - - public: - - //Constructor - Gdt(); - - //Destructor - ~Gdt(); - - //Methods - void loadGdt(); - -}; - - -#endif diff --git a/kernel/Helpers/Makefile b/kernel/Helpers/Makefile index 76805bd..a813312 100644 --- a/kernel/Helpers/Makefile +++ b/kernel/Helpers/Makefile @@ -1,23 +1,23 @@ EXEC=helpers.o - all:$(EXEC) #----- Helpers ----- -$(EXEC): memory.o memPrint.o - ld -m elf_i386 -r -o $(EXEC) $^ +$(EXEC): memory.o memprint.o +# ld -m elf_i386 -r -o $(EXEC) $^ + ar -r -o $(EXEC) $^ #--------------- #----- Memory ----- -memory.o: memory.cpp memory.hpp - $(CXX) -c -o $@ $< +memory.o: memory.c memory.h + $(CC) $(CFLAGS) -c -o $@ $< #------------------ #----- memPrint ----- -memPrint.o: memPrint.cpp memPrint.hpp - $(CXX) -c -o $@ $< +memprint.o: memprint.c memprint.h + $(CC) $(CFLAGS) -c -o $@ $< #------------------- diff --git a/kernel/Helpers/globalVars.hpp b/kernel/Helpers/globalVars.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/kernel/Helpers/memPrint.hpp b/kernel/Helpers/memPrint.hpp deleted file mode 100644 index 19a6708..0000000 --- a/kernel/Helpers/memPrint.hpp +++ /dev/null @@ -1,81 +0,0 @@ -#ifndef __memPrint__ -#define __memPrint__ - -#include "./types.hpp" - - -#define MEMPRINTSTARTADR 0xB8000 -#define MAXCURSORX 80 -#define MAXCURSORY 25 - - - - -//Define the bios color -enum colorBios{ - - BLACK=0x0, - BLUE=0x1, - GREEN=0x2, - CYAN=0x3, - RED=0x4, - MAGENTA=0x5, - BROWN=0x6, - LIGHTGRAY=0x7, - DARKGRAY=0x8, - LIGHTBLUE=0x9, - LIGHTGREEN=0xA, - LIGHTCYAN=0xB, - LIGHTRED=0xC, - LIGHTMAGENTA=0xD, - YELLOW=0xE, - WHITE=0xF - -}; - -//Type def for biosColor -typedef enum colorBios colorBios; - - -//Class to print char on screen using Video Ram mapping -class memPrint{ - - private: - - //Cursor position - u8 m_cursorX; - u8 m_cursorY; - - //Current colors (background and foreground): - u8 m_colors; - - //Methods - void updateCursor(); - - public: - - //Constructor - memPrint(); - - //Destructor - ~memPrint(); - - //Set color - void setBackground(colorBios color); - void setForeground(colorBios color); - - //Putchar - void putChar(u8 character); - - //Print - void print(char *str); - - //Scroll up - void scrollUp(u8 number); - - //Clear screen - void clear(); - -}; - -#endif diff --git a/kernel/Helpers/memory.cpp b/kernel/Helpers/memory.c similarity index 91% rename from kernel/Helpers/memory.cpp rename to kernel/Helpers/memory.c index 8ace044..44c0e90 100644 --- a/kernel/Helpers/memory.cpp +++ b/kernel/Helpers/memory.c @@ -1,5 +1,5 @@ -#include "./memory.hpp" -#include "./types.hpp" +#include "./memory.h" +#include "./types.h" //Fonction to copy data into memory int memcpy(u32 source, u32 dest, u32 size){ diff --git a/kernel/Helpers/memory.hpp b/kernel/Helpers/memory.h similarity index 84% rename from kernel/Helpers/memory.hpp rename to kernel/Helpers/memory.h index ddc522c..5f399db 100644 --- a/kernel/Helpers/memory.hpp +++ b/kernel/Helpers/memory.h @@ -1,7 +1,7 @@ #ifndef __memory__ #define __memory__ -#include "./types.hpp" +#include "./types.h" //Fonction to copy data into memory int memcpy(u32 source, u32 dest, u32 size); diff --git a/kernel/Helpers/memPrint.cpp b/kernel/Helpers/memprint.c similarity index 51% rename from kernel/Helpers/memPrint.cpp rename to kernel/Helpers/memprint.c index 179b821..83eb8de 100644 --- a/kernel/Helpers/memPrint.cpp +++ b/kernel/Helpers/memprint.c @@ -1,50 +1,36 @@ -#include "./memPrint.hpp" +#include "./memprint.h" +//Define global vars +u8 MEMPRINT_CURSORX=0; +u8 MEMPRINT_CURSORY=0; +u8 MEMPRINT_COLORS=0x0F; -//Constructor -memPrint::memPrint(){ - - //Initialise position - this->m_cursorX=0; - this->m_cursorY=0; - - //Initialise color - this->setBackground(BLACK); - this->setForeground(WHITE); - -} - -//Destructor -memPrint::~memPrint(){ - -} - //Move cursor -void memPrint::updateCursor(){ +void memprint_updateCursor(){ //Update X axis - this->m_cursorX++; + MEMPRINT_CURSORX++; //Check X value - if(this->m_cursorX >= MAXCURSORX){ + if(MEMPRINT_CURSORX >= MAXCURSORX){ //If X is out of the screen - this->m_cursorX=0; + MEMPRINT_CURSORX=0; //Update Y - this->m_cursorY++; + MEMPRINT_CURSORY++; //Check Y value - if(this->m_cursorY >= MAXCURSORY){ + if(MEMPRINT_CURSORY > MAXCURSORY){ //If Y is out of the screen - this->scrollUp(1); + memprint_scrollUp(1); //Decrease Y value - this->m_cursorY--; + MEMPRINT_CURSORY--; } @@ -54,58 +40,58 @@ void memPrint::updateCursor(){ } //Change character background color -void memPrint::setBackground(colorBios color){ +void memprint_setBackground(colorBios color){ u8 newColor= (color << 4); - this->m_colors= newColor | ((this->m_colors << 4) >> 4); + MEMPRINT_COLORS= newColor | ((MEMPRINT_COLORS << 4) >> 4); } //Change character color -void memPrint::setForeground(colorBios color){ +void memprint_setForeground(colorBios color){ u8 newColor= color; - this->m_colors= newColor | ((this->m_colors >> 4) << 4); + MEMPRINT_COLORS= newColor | ((MEMPRINT_COLORS >> 4) << 4); } //Print a char -void memPrint::putChar(u8 character){ +void memprint_putChar(u8 character){ //Get the adresse with the cursor position - char *adress= ((char *) MEMPRINTSTARTADR) + (this->m_cursorX * 2) + (this->m_cursorY * MAXCURSORX * 2); + char *adress= ((char *) MEMPRINTSTARTADR) + (MEMPRINT_CURSORX * 2) + (MEMPRINT_CURSORY * MAXCURSORX * 2); //Copy the character *adress=character; //Copy his attribute adress++; - *adress=this->m_colors; + *adress=MEMPRINT_COLORS; //Update cursor position - this->updateCursor(); + memprint_updateCursor(); } //Print a char* -void memPrint::print(char *str){ +void memprint_print(char *str){ while(*str!=0x0){ - this->putChar(*str); + memprint_putChar(*str); str++; } } //Clear the screen -void memPrint::clear(){ - this->scrollUp(MAXCURSORY); +void memprint_clear(){ + memprint_scrollUp(MAXCURSORY); } //Scroll up "number" times -void memPrint::scrollUp(u8 number){ +void memprint_scrollUp(u8 number){ //Get number of adress (char & his attribute) to scroll int nbAdrToScroll=number*MAXCURSORX*2; - + int i=0; //Scroll all of the characters and attributes - for(int i=0;i!=MAXCURSORX*2*MAXCURSORY;i++){ + for(i;i!=MAXCURSORX*2*MAXCURSORY;i++){ //Get source character or attribute char* source=(((char *)MEMPRINTSTARTADR) + i); diff --git a/kernel/Helpers/memprint.h b/kernel/Helpers/memprint.h new file mode 100644 index 0000000..eb41941 --- /dev/null +++ b/kernel/Helpers/memprint.h @@ -0,0 +1,59 @@ +#ifndef __memprint__ +#define __memprint__ + +#include "./types.h" + + +#define MEMPRINTSTARTADR 0xB8000 +#define MAXCURSORX 80 +#define MAXCURSORY 25 + + +//Define the bios color +enum colorBios{ + + BLACK=0x0, + BLUE=0x1, + GREEN=0x2, + CYAN=0x3, + RED=0x4, + MAGENTA=0x5, + BROWN=0x6, + LIGHTGRAY=0x7, + DARKGRAY=0x8, + LIGHTBLUE=0x9, + LIGHTGREEN=0xA, + LIGHTCYAN=0xB, + LIGHTRED=0xC, + LIGHTMAGENTA=0xD, + YELLOW=0xE, + WHITE=0xF + +}; + +//Type def for biosColor +typedef enum colorBios colorBios; + + +//Update cursor position +void memprint_updateCursor(); + +//Set color +void memprint_setBackground(colorBios color); +void memprint_setForeground(colorBios color); + +//Putchar +void memprint_putChar(u8 character); + +//Print +void memprint_print(char *str); + +//Scroll up +void memprint_scrollUp(u8 number); + +//Clear screen +void memprint_clear(); + + + +#endif diff --git a/kernel/Helpers/types.hpp b/kernel/Helpers/types.h similarity index 100% rename from kernel/Helpers/types.hpp rename to kernel/Helpers/types.h diff --git a/kernel/Makefile b/kernel/Makefile index a5694c4..0c24011 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,21 +1,28 @@ -CXX=g++ -Wall -m32 +CC=gcc +LD=ld -m elf_i386 +CFLAGS=-m32 +LDFLAGS=-m elf_i386 --entry=_kernelEntry -Ttext=0x100000 + EXEC=kernel.bin -export CXX +export CC +export LD +export CFLAGS +export LDFLAGS all:$(EXEC) #----- Kernel ----- $(EXEC):entry.o main.o GDT/gdt.o Helpers/helpers.o - $(CXX) --entry=_kernelEntry -Ttext=0x100000 -o $@ $^ + $(LD) $(LDFLAGS) -o $@ $^ #----------------- #----- Entry & Main----- entry.o:entry.asm nasm -f elf $^ -main.o:main.cpp - $(CXX) -c $^ -o $@ +main.o:main.c + $(CC) $(CFLAGS) -c $^ -o $@ #----------------------- @@ -29,6 +36,7 @@ Helpers/helpers.o: cd ./Helpers/ && make #--------------- + #----- Other ----- .PHONY:clean diff --git a/kernel/entry.asm b/kernel/entry.asm index 1ec44db..57d395f 100644 --- a/kernel/entry.asm +++ b/kernel/entry.asm @@ -22,4 +22,3 @@ dd 0x1BADB002 ;Run kernel begin: call _boot ;Start kernel - diff --git a/kernel/main.c b/kernel/main.c new file mode 100644 index 0000000..b687369 --- /dev/null +++ b/kernel/main.c @@ -0,0 +1,41 @@ +//To load GDT +#include "GDT/gdt.h" +#include "./Helpers/memprint.h" + + +//----- PiegOS kernel main ----- +int main(){ + + //Welcome + memprint_print("Welcome to PiegOS"); + + //Infinite loop + while(1); + + //Exit code + return 0; +} + + +//----- PiegOS kernel boot function ----- +void _boot(){ + + //Load GDT + gdt_loadGdt(); + + //Init all segments and stack + __asm__("\ + movw $0x10, %ax; \n \ + movw %ax, %ds; \n \ + movw %ax, %es \n \ + ljmp $0x08, $updateDS;\ + updateDS: \n\ + movw $0x18, %ax \n \ + movw %ax, %ss \n \ + movl $0x00B00000, %esp \n\ + "); + + //Call main function after stack pointer changing (due to C optimisation) + main(); +} + diff --git a/kernel/main.cpp b/kernel/main.cpp deleted file mode 100644 index 0dec7e0..0000000 --- a/kernel/main.cpp +++ /dev/null @@ -1,52 +0,0 @@ -//To load GDT -#include "GDT/gdt.hpp" -#include "./Helpers/memPrint.hpp" - - -//----- Global Definition ----- -memPrint VideoRam; //Used to print data on screen -//----------------------------- - - - -//----- PiegOS kernel main ----- -int main(){ - - //Welcome - VideoRam.print("Welcome to PiegOS"); - - //Infinite loop - while(1); - - //Exit code - return 0; -} - - - -//----- PiegOS kernel boot ----- -//Mangling the _boot function -extern "C" void _boot(){ - - //Create Gdt instance - Gdt gdt; - - //Load Gdt into memory - gdt.loadGdt(); - - //Init all segments and stack - __asm__("\ - movw $0x10, %ax; \n \ - movw %ax, %ds; \n \ - movw %ax, %es \n \ - ljmp $0x08, $updateDS;\ - updateDS: \n\ - movw $0x18, %ax \n \ - movw %ax, %ss \n \ - movl $0x00B00000, %esp \n\ - "); - - //Call main function after stack pointer changing (due to C++ optimisation) - main(); -} -