KraKern

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

commit 63b86e321e26cd22e58a08988264fcd6039fa1a4
parent 9d464fb8961febe352c44cf741ac10d43c61105d
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Sun, 17 Jan 2016 22:21:42 +0100

Add(Filesystem): Base filesystem

Diffstat:
Aincludes/core/File.hpp | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aincludes/core/FileSystem.hpp | 27+++++++++++++++++++++++++++
Mincludes/core/Kernel.hpp | 2++
Aincludes/core/Process.hpp | 8++++++++
Mincludes/core/base_lib.h | 1+
Mincludes/core/kern_types.h | 2++
Akernel/File.cpp | 108+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Akernel/FileSystem.cpp | 17+++++++++++++++++
Mkernel/Kernel.cpp | 4+++-
Mkernel/Makefile | 2+-
Akernel/Process.cpp | 1+
Mlib/baselib.cpp | 10++++++++++
Mlib/cxx.cpp | 54++++++++++++++++++++++++++++++++----------------------
13 files changed, 286 insertions(+), 24 deletions(-)

diff --git a/includes/core/File.hpp b/includes/core/File.hpp @@ -0,0 +1,74 @@ +#ifndef __FILE_H__ +# define __FILE_H__ + +# include <os.h> + +enum { + TYPE_FILE, + TYPE_DIRECTORY, + TYPE_DEVICE, + TYPE_PROCESS, + TYPE_LINK +}; + +class File { + public: + File(char *name, u8 t); + ~File(void); + + // Virtual functions for filesystem heritage. + virtual u32 open(u32 flag) { return -1; }; + virtual u32 close(void) { return -1; }; + virtual u32 read(u32 pos, u8 *buffer, u32 size) { return -1; }; + virtual u32 write(u32 pos, u8 *buffer, u32 size) { return -1; }; + virtual u32 ioctl(u32 id, u8 *buffer) { return -1; }; + virtual u32 remove(void) { return -1; }; + virtual void scan(void) {}; + + u32 add_child(File *n); + File *create_child(char *n, u8 t); + File *find(char *n); + u32 mmap(u32 size, u32 flags, u32 offset, u32 prot); + + void set_size(u32 t); + void set_type(u8 t); + void set_parent(File *n); + void set_child(File *n); + void set_next(File *n); + void set_prev(File *n); + void set_link(File *n); + void set_name(char *n); + + char *get_name(void); + File *get_parent(void); + File *get_child(void); + File *get_next(void); + File *get_prev(void); + File *get_link(void); + u8 get_type(void); + u32 get_size(void); + u32 get_inode(void); + + protected: + static u32 _inode_system; + char *_map_memory; + char *_name; + u32 _size; + u8 _type; + u32 _inode; + File *_dev; + File *_link; + File *_master; + File *_parent; + File *_child; + File *_next; + File *_prev; + File *_device; + + private: + void _check_name(void); +}; + +# include <Kernel.hpp> + +#endif diff --git a/includes/core/FileSystem.hpp b/includes/core/FileSystem.hpp @@ -0,0 +1,27 @@ +#ifndef __FILESYSTEM__ +# define __FILESYSTEM__ + +# include <os.h> + +class File; + +class FileSystem { + public: + void init(void); + //void mknod(char *module, char *name, u32 flag); + File *path(char *p); + File *path_parent(char *p, char *fname); + u32 link(char *fname, char *newf); + u32 add_file(char *dir, File *fp); + File *get_root(); + + private: + File *_root; + File *_dev; + File *_var; +}; + +# include <File.hpp> +# include <Kernel.hpp> + +#endif diff --git a/includes/core/Kernel.hpp b/includes/core/Kernel.hpp @@ -7,6 +7,7 @@ # include <Io.hpp> # include <Memory.hpp> # include <Keyboard.hpp> +# include <FileSystem.hpp> class Kernel { public: @@ -20,6 +21,7 @@ class Kernel { IDT idt; Memory mem; Keyboard keyboard; + FileSystem fs; }; #endif diff --git a/includes/core/Process.hpp b/includes/core/Process.hpp @@ -0,0 +1,8 @@ +#ifndef __PROCESS__ +# define __PROCESS__ + +class Process { + +}; + +#endif diff --git a/includes/core/base_lib.h b/includes/core/base_lib.h @@ -12,6 +12,7 @@ extern "C" { void *memcpy(void *dst, const void *src, u32 n); void *memset(void *dst, int c, u32 n); void itoa(char *buf, u32 n, int base); + void strreplace(char *s, char a, char b); } #endif diff --git a/includes/core/kern_types.h b/includes/core/kern_types.h @@ -11,4 +11,6 @@ typedef signed short s16; typedef signed int s32; typedef signed long long s64; +# define NULL 0x0 + #endif diff --git a/kernel/File.cpp b/kernel/File.cpp @@ -0,0 +1,108 @@ +# include <File.hpp> + +File::File(char *n, u8 t) { + this->_name = (char *)__kern.mem.kmalloc(strlen(n) + 1); + strcpy(this->_name, n); + this->_check_name(); + this->_inode = File::_inode_system++; + this->_size = 0; + this->_type = t; + this->_parent = this->_child = this->_next = this->_prev = this->_link = 0x0; + this->_map_memory = 0x0; +} + +File::~File(void) { + File *n = this->_child, *nn = 0x0; + + __kern.mem.kfree(this->_name); + if (!this->_prev) { + this->_parent->set_child(this->_next); + this->_next->set_prev(0x0); + } else if (!this->_next) { + this->_parent->set_next(0x0); + } else { + this->_prev->set_next(this->_next); + this->_next->set_prev(this->_prev); + } + while (n) { + nn = n->get_next(); + delete n; + n = nn; + } +} + +u32 File::add_child(File *n) { + if (!n) + return 1; + n->set_parent(this); + n->set_prev(0x0); + n->set_next(this->_child); + if (this->_child) + this->_child->set_prev(n); + this->_child = n; + return 0; +} + +File *File::create_child(char *n, u8 t) { + File *fp = new File(n, t); + + this->add_child(fp); + return fp; +} + +File *File::find(char *n) { + File *fp = this->_child; + + while (fp) { + if (!strcmp(fp->get_name(), n)) + return fp; + fp = fp->get_next(); + } + return 0x0; +} + +u32 File::mmap(u32 size, u32 flags, u32 offset, u32 prot) { + return -1; +} + +void File::_check_name(void) { + char a = '_'; + + strreplace(this->_name, '/', a); + strreplace(this->_name, ' ', a); + strreplace(this->_name, '\\', a); + strreplace(this->_name, '?', a); + strreplace(this->_name, ':', a); + strreplace(this->_name, '>', a); + strreplace(this->_name, '<', a); + strreplace(this->_name, '*', a); + strreplace(this->_name, '"', a); + strreplace(this->_name, ':', a); +} + +char *File::get_name(void) { return this->_name; }; +File *File::get_parent(void) { return this->_parent; }; +File *File::get_child(void) { return this->_child; }; +File *File::get_next(void) { return this->_next; }; +File *File::get_prev(void) { return this->_prev; }; +File *File::get_link(void) { return this->_link; }; +u32 File::get_size(void) { return this->_size; }; +u8 File::get_type(void) { return this->_type; }; +u32 File::get_inode(void) { return this->_inode; }; + +void File::set_size(u32 t) { this->_size = t; }; +void File::set_type(u8 t) { this->_type = t; }; +void File::set_parent(File *f) { this->_parent = f; }; +void File::set_child(File *f) { this->_child = f; }; +void File::set_next(File *f) { this->_next = f; }; +void File::set_prev(File *f) { this->_prev = f; }; +void File::set_link(File *f) { this->_link = f; }; + +void File::set_name(char *n) { + __kern.mem.kfree(this->_name); + this->_name = (char *)__kern.mem.kmalloc(strlen(n) + 1); + strcpy(this->_name, n); + this->_check_name(); +} + +u32 File::_inode_system = 0; diff --git a/kernel/FileSystem.cpp b/kernel/FileSystem.cpp @@ -0,0 +1,17 @@ +# include <FileSystem.hpp> + +void FileSystem::init(void) { + File *sysd; + + __kern.io.loading("Filesystem ..."); + this->_root = new File("/", TYPE_DIRECTORY); + this->_dev = this->_root->create_child("dev", TYPE_DIRECTORY); + this->_var = this->_root->create_child("env", TYPE_DIRECTORY); + this->_root = this->_root->create_child("proc", TYPE_DIRECTORY); + this->_root = this->_root->create_child("mnt", TYPE_DIRECTORY); + sysd = this->_root->create_child("sys", TYPE_DIRECTORY); + sysd->create_child("usr", TYPE_DIRECTORY); + sysd->create_child("modules", TYPE_DIRECTORY); + sysd->create_child("sockets", TYPE_DIRECTORY); + __kern.io.done(); +} diff --git a/kernel/Kernel.cpp b/kernel/Kernel.cpp @@ -15,14 +15,16 @@ void Kernel::init(multiboot_info *mbi) { this->gdt = __gdt; this->idt = __idt; this->mem = __mem; - this->keyboard = __keyboard; this->io.init(); this->welcome(); this->gdt.init(); this->idt.init(); this->mem.init(mbi->high_mem); + this->keyboard = *(new Keyboard()); this->keyboard.init(); + this->fs = *(new FileSystem()); + this->fs.init(); while (1); } diff --git a/kernel/Makefile b/kernel/Makefile @@ -1 +1 @@ -OBJS += kernel/main.o kernel/Io.o kernel/Kernel.o kernel/Memory.o kernel/Keyboard.o +OBJS += kernel/main.o kernel/Io.o kernel/Kernel.o kernel/Memory.o kernel/Keyboard.o kernel/Process.o kernel/File.o kernel/FileSystem.o diff --git a/kernel/Process.cpp b/kernel/Process.cpp @@ -0,0 +1 @@ +# include <Process.hpp> diff --git a/lib/baselib.cpp b/lib/baselib.cpp @@ -84,4 +84,14 @@ extern "C" { buf[i] = tmp; } } + + void strreplace(char *s, char a, char b) { + if (!s) + return ; + while (*s) { + if (*s == a) + *s = b; + s++; + } + } } diff --git a/lib/cxx.cpp b/lib/cxx.cpp @@ -1,7 +1,7 @@ # include <os.h> +# include <Kernel.hpp> -extern "C" -{ +extern "C" { int __cxa_atexit(void (*Destructor) (void *), void *Parameter, void *HomeDSO); void __cxa_finalize(void *); void __cxa_pure_virtual(); @@ -13,13 +13,10 @@ extern "C" void *__dso_handle; void *__stack_chk_guard(0); - -namespace __cxxabiv1 -{ +namespace __cxxabiv1 { __extension__ typedef int __guard __attribute__((mode(__DI__))); - extern "C" - { + extern "C" { int __cxa_guard_acquire(__guard *Guard) { return ! *(char *) (Guard); } void __cxa_guard_release(__guard *Guard) { *(char *) Guard = 1; } void __cxa_guard_abort(__guard *) { } @@ -27,24 +24,13 @@ namespace __cxxabiv1 } -int __cxa_atexit(void (*) (void *), void *, void *) -{ +int __cxa_atexit(void (*) (void *), void *, void *) { return 0; } -void _Unwind_Resume(){ - -} - -void __cxa_finalize(void *) -{ - -} - -void __cxa_pure_virtual() -{ - -} +void _Unwind_Resume() {}; +void __cxa_finalize(void *) {}; +void __cxa_pure_virtual() {}; void __stack_chk_guard_setup() { @@ -54,3 +40,27 @@ void __stack_chk_guard_setup() Guard[sizeof(__stack_chk_guard) - 2] = '\n'; Guard[0] = 0; } + +void __attribute__((noreturn)) __stack_chk_fail() +{ + __kern.io.panic("Buffer Overflow (SSP Signal)"); + for(;;) ; +} + +void operator delete(void *ptr) { + __kern.mem.kfree(ptr); +} + +void* operator new(u32 len) { + return (void*)__kern.mem.kmalloc(len); +} + +void operator delete[](void *ptr) { + ::operator delete(ptr); +} + +void* operator new[](u32 len) { + return ::operator new(len); +} + +void *__gxx_personality_v0=(void*)0xDEADBEAF;