KraKern

C++ Kernel
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/KraKern

commit a681e9f709bc9ab07d90a746ef748f076c25c41b
parent 1c9282b6f9198192d559c3da8197f501f531c93c
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 12 Jan 2016 22:30:23 +0100

Add(GDT): Now support GDT and some ->:

Add loading / done functions for init in Io class
Add a specific header for x86

Diffstat:
MMakefile | 2+-
Aboot/x86/GDT.cpp | 45+++++++++++++++++++++++++++++++++++++++++++++
Mboot/x86/Makefile | 2+-
Aincludes/boot/x86.hpp | 41+++++++++++++++++++++++++++++++++++++++++
Mincludes/core/Io.hpp | 47+++++++++++++++++++++++------------------------
Mincludes/core/Kernel.hpp | 7++++++-
Mkern.img | 0
Mkernel/Io.cpp | 21+++++++++++++++++----
Dkernel/Io.o | 0
Mkernel/Kernel.cpp | 29+++++++++++++++++++++++++++--
Dkernel/Kernel.o | 0
Mkernel/main.cpp | 6+++---
Dkernel/main.o | 0
13 files changed, 164 insertions(+), 36 deletions(-)

diff --git a/Makefile b/Makefile @@ -36,7 +36,7 @@ $(KERNEL): $(OBJS) %.o: %.asm $(ASM) $(ASMFLAGS) -c $< -o $@ -install: +install: $(KERNEL) sudo mount /dev/loop1 /mnt sudo cp $(KERNEL) /mnt/boot/. sudo umount /mnt diff --git a/boot/x86/GDT.cpp b/boot/x86/GDT.cpp @@ -0,0 +1,45 @@ +# include <x86.hpp> + +// ASM Access +struct gdtr gdtr; + +GDT __gdt; + +void GDT::init(void) { + __kern.io.loading("Global Descriptor Table ..."); + this->_init_gdt(); + __kern.io.done(); +} + +void GDT::_init_gdt(void) { + this->_init_gdt_desc(0x0, 0x0, 0x0, 0x0, &this->_gdtdesc[0]); + this->_init_gdt_desc(0x0, 0xFFFFF, 0x9B, 0x0D, &this->_gdtdesc[1]); /* code */ + this->_init_gdt_desc(0x0, 0xFFFFF, 0x93, 0x0D, &this->_gdtdesc[2]); /* data */ + this->_init_gdt_desc(0x0, 0x0, 0x97, 0x0D, &this->_gdtdesc[3]); /* stack */ + this->_init_gdt_desc(0x0, 0xFFFFF, 0xFF, 0x0D, &this->_gdtdesc[4]); /* ucode */ + this->_init_gdt_desc(0x0, 0xFFFFF, 0xF3, 0x0D, &this->_gdtdesc[5]); /* udata */ + this->_init_gdt_desc(0x0, 0x0, 0xF7, 0x0D, &this->_gdtdesc[6]); /* ustack */ + + this->_gdtr.limite = GDT_MAX * 8; + this->_gdtr.base = GDT_BASE; + memcpy((char *)GDT_BASE, (char *)this->_gdtdesc, this->_gdtr.limite); + gdtr = this->_gdtr; + asm("lgdtl (gdtr)"); + asm(" movw $0x10, %ax \n \ + movw %ax, %ds \n \ + movw %ax, %es \n \ + movw %ax, %fs \n \ + movw %ax, %gs \n \ + ljmp $0x08, $next \n \ + next: \n"); +} + +void GDT::_init_gdt_desc(u32 base, u32 limite, u8 acces, u8 other, struct gdtdesc *desc) { + desc->lim0_15 = (limite & 0xffff); + desc->base0_15 = (base & 0xffff); + desc->base16_23 = (base & 0xff0000) >> 16; + desc->acces = acces; + desc->lim16_19 = (limite & 0xf0000) >> 16; + desc->other = (other & 0xf); + desc->base24_31 = (base & 0xff000000) >> 24; +} diff --git a/boot/x86/Makefile b/boot/x86/Makefile @@ -1 +1 @@ -OBJS += boot/x86/start.o +OBJS += boot/x86/start.o boot/x86/GDT.o diff --git a/includes/boot/x86.hpp b/includes/boot/x86.hpp @@ -0,0 +1,41 @@ +#ifndef __x86__ +# define __x86__ +# include <os.h> + +# define GDT_BASE 0x00000800 +# define GDT_MAX 0xFF + +struct gdtr { + u16 limite; + u32 base; +} __attribute__ ((packed)); + +struct gdtdesc { + u16 lim0_15; + u16 base0_15; + u8 base16_23; + u8 acces; + u8 lim16_19:4; + u8 other:4; + u8 base24_31; +} __attribute__ ((packed)); + +class GDT { + public: + void init(void); + + private: + void _init_gdt(void); + void _init_gdt_desc(u32 base, u32 limite, u8 acces, u8 other, struct gdtdesc *desc); + + struct gdtr _gdtr; + struct gdtdesc _gdtdesc[GDT_MAX]; +}; + +# ifndef __KERN__ +# include <Kernel.hpp> +class Kernel; +extern Kernel __kern; +# endif + +#endif diff --git a/includes/core/Io.hpp b/includes/core/Io.hpp @@ -10,24 +10,23 @@ # define SIZE_SCREEN 0xFA0 # define SCREENLIM 0xB8FA0 -enum Color { - Black = 0, - Blue = 1, - Green = 2, - Cyan = 3, - Red = 4, - Magenta = 5, - Orange = 6, - LightGrey = 7, - DarkGrey = 8, - LightBlue = 9, - LightGreen =10, - LightCyan = 11, - LightRed = 12, - LightMagenta=13, - Yellow = 14, - White = 15 -}; +// Colors defines +# define COLOR_BLACK 0x0 +# define COLOR_BLUE 0x1 +# define COLOR_GREEN 0x2 +# define COLOR_CYAN 0x3 +# define COLOR_RED 0x4 +# define COLOR_MAGENTA 0x5 +# define COLOR_ORANGE 0x6 +# define COLOR_LIGHT_GREY 0x7 +# define COLOR_DARK_GREY 0x8 +# define COLOR_LIGHT_BLUE 0x9 +# define COLOR_LIGHT_GREEN 0xA +# define COLOR_LIGHT_CYAN 0xB +# define COLOR_LIGHT_RED 0xC +# define COLOR_LIGHT_MAGENTA 0xD +# define COLOR_YELLOW 0xE +# define COLOR_WHITE 0xF class Io { public: @@ -48,7 +47,7 @@ class Io { u32 read(char* buf,u32 count); void putc(char c); void puts(const char *s); - void set_color(char fcol,char bcol); + void set_color(char color); void setXY(char xc,char yc); void clear(void); void print(const char *s, ...); @@ -58,6 +57,8 @@ class Io { void scrollup(unsigned int n); void save_screen(void); void load_screen(void); + void loading(const char *); + void done(void); enum console_type { BUFFERED, @@ -76,8 +77,6 @@ class Io { int _inlock; int _keystate; - char _fcolor; - char _bcolor; char _x; char _y; char _kattr; @@ -85,10 +84,10 @@ class Io { static char *_vidmem; }; -# include <Kernel.hpp> +# ifndef __KERN__ +# include <Kernel.hpp> class Kernel; - - extern Kernel kern; +# endif #endif diff --git a/includes/core/Kernel.hpp b/includes/core/Kernel.hpp @@ -2,13 +2,18 @@ # define __KERNEL__ # include <os.h> +# include <x86.hpp> # include <Io.hpp> class Kernel { public: Kernel(void); - Io __io; + void init(void); + void welcome(void); + + Io io; + GDT gdt; }; #endif diff --git a/kern.img b/kern.img Binary files differ. diff --git a/kernel/Io.cpp b/kernel/Io.cpp @@ -16,6 +16,7 @@ Io::~Io(void) { void Io::init(void) { this->_real_screen = (char *)RAM_SCREEN; this->clear(); + this->_kattr = COLOR_BLACK; } void Io::scrollup(u32 n) { @@ -62,9 +63,8 @@ void Io::puts(const char *s) { void Io::putc(char c) { u8 *video; - this->_kattr = 0x07; video = (u8 *)(this->_real_screen + 2 * this->_x + 160 * this->_y); - if (c == 10) { + if (c == 10) { this->_x = 0; this->_y++; } else if (c == 8 && this->_x) { @@ -77,7 +77,7 @@ void Io::putc(char c) { } else { *video = c; *(video + 1) = this->_kattr; - this->_x++; + this->_x++; if (this->_x > 79) { this->_x = 0; this->_y++; @@ -124,6 +124,19 @@ u32 Io::read(char *buf, u32 count) { return strlen(buf); } +void Io::loading(const char *s) { + this->puts(s); + for (int padding = 80 - strlen(s) - 10; padding > 0; padding--, this->puts(" ")); +} + +void Io::done(void) { + this->puts("["); + this->set_color(COLOR_GREEN); + this->puts(" DONE "); + this->set_color(COLOR_WHITE); + this->puts("]\n"); +} + // Output void Io::obyte(u32 a, u8 v) { asmv("outb %%al, %%dx" :: "d" (a), "a" (v));; }; void Io::oword(u32 a, u16 v) { asmv("outw %%ax, %%dx" :: "d" (a), "a" (v));; }; @@ -137,5 +150,5 @@ u32 Io::iline(u32 a) { u32 v; asmv("inb %%dx, %%eax" : "=a" (v) : "d" (a)); ret u32 Io::getX(void) { return (u32)this->_x; }; u32 Io::getY(void) { return (u32)this->_y; }; -void Io::set_color(char fcol, char bcol) { this->_fcolor = fcol; this->_bcolor = bcol; }; +void Io::set_color(char color) { this->_kattr = color; }; void Io::setXY(char xc, char yc) { this->_x = xc; this->_y = yc; }; diff --git a/kernel/Io.o b/kernel/Io.o Binary files differ. diff --git a/kernel/Kernel.cpp b/kernel/Kernel.cpp @@ -1,7 +1,32 @@ #include <Kernel.hpp> -//Kernel kern; +extern Io __io; +extern GDT __gdt; +Kernel __kern; Kernel::Kernel(void) { - this->__io = Io(); +} + +void Kernel::init(void) { + this->io = __io; + this->gdt = __gdt; + + this->io.init(); + this->welcome(); + this->gdt.init(); +} + +void Kernel::welcome(void) { + this->io.set_color(COLOR_LIGHT_RED); + this->io.puts(" \n\ + _ __ _ __ \n\ + | |/ / | |/ / \n\ + | ' / _ __ __ _| ' / ___ _ __ _ __ \n\ + | < | '__/ _` | < / _ \\ '__| '_ \\ \n\ + | . \\| | | (_| | . \\ __/ | | | | | \n\ + |_|\\_\\_| \\__,_|_|\\_\\___|_| |_| |_|\n\n\n"); + + this->io.set_color(COLOR_DARK_GREY); + for (int i = 0; i < 80; i++, this->io.puts("#")); + this->io.set_color(COLOR_WHITE); } diff --git a/kernel/Kernel.o b/kernel/Kernel.o Binary files differ. diff --git a/kernel/main.cpp b/kernel/main.cpp @@ -1,11 +1,11 @@ # include <os.h> # include <boot.h> # include <Io.hpp> -extern Io __io; +extern Kernel __kern; extern "C" void kmain(multiboot_info *info) { (void)info; - __io.init(); - __io.puts("Hello World !"); + // Main class initialization + __kern.init(); return ; } diff --git a/kernel/main.o b/kernel/main.o Binary files differ.