Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Kernlib split to multiple files & refactoring
  • Loading branch information
djphoenix committed Nov 9, 2016
1 parent 41a089d commit 734cb3b
Show file tree
Hide file tree
Showing 32 changed files with 541 additions and 306 deletions.
19 changes: 8 additions & 11 deletions ld/system.ld
Expand Up @@ -33,19 +33,8 @@ SECTIONS

. = ALIGN(64);

__init_start__ = . ;
*(.init_array*)
__init_end__ = . ;

*(.text .text.*)
*(.gnu.linkonce.t.*)

. = ALIGN(8);

___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
LONG (-1); LONG (-1); *(.ctors); *(.ctor); *(SORT(.ctors.*)); LONG (0); LONG (0);
___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
LONG (-1); LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*)); LONG (0); LONG (0);
PROVIDE (etext = .);
*(.fini)
. = ALIGN(64);
Expand All @@ -54,6 +43,14 @@ SECTIONS
.data BLOCK(1) : ALIGN(1) {
__data_start__ = . ;
*(.rodata .rodata.*)
. = ALIGN(8);

___INIT_LIST__ = .; __INIT_LIST__ = . ;
LONG (-1); LONG (-1); *(.init_array*) LONG (0); LONG (0);
___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
LONG (-1); LONG (-1); *(.ctors); *(.ctor); *(SORT(.ctors.*)); LONG (0); LONG (0);
___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
LONG (-1); LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*)); LONG (0); LONG (0);
*(.data .data.*)
*(.got .got.*)
__data_end__ = . ;
Expand Down
4 changes: 2 additions & 2 deletions src/boot/boot.cpp
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "pxlib.hpp"
#include "kernlib.hpp"

#include "pagetable.hpp"
#include "smp.hpp"
Expand All @@ -24,7 +24,7 @@

void NORETURN main() {
static_init();
clrscr();
Display::getInstance()->clean();
Pagetable::init();
SMP::init();
Interrupts::init();
Expand Down
2 changes: 1 addition & 1 deletion src/boot/include/multiboot_info.hpp
Expand Up @@ -15,7 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once
#include "pxlib.hpp"
#include "kernlib.hpp"

struct GRUBMODULE {
uint32_t start;
Expand Down
75 changes: 75 additions & 0 deletions src/kernlib/display.cpp
@@ -0,0 +1,75 @@
// PhoeniX OS Kernel library display functions
// Copyright (C) 2013 PhoeniX
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "kernlib.hpp"

ConsoleDisplay::ConsoleDisplay() {
display = base;
}

void ConsoleDisplay::putc(const char c) {
if (c == 0) return;
size_t pos = display - base;
if (c == '\n') {
display += 160 - (pos % 160);
} else if (c == '\t') {
do {
*(display++) = ' ';
*(display++) = 0x0F;
} while (pos % 8 != 0);
} else {
*(display++) = c;
*(display++) = 0x0F;
}
if (display >= top) {
size_t size = top - base;
Memory::copy(base, base + 160, size - 160);
display = top - 160;
Memory::fill(display, 0, 160);
}
}

void ConsoleDisplay::write(const char *str) {
uint64_t t = EnterCritical();
mutex.lock();
while (*str != 0) putc(*(str++));
mutex.release();
LeaveCritical(t);
}

void ConsoleDisplay::clean() {
mutex.lock();
size_t size = top - base;
Memory::fill(base, 0, size);
mutex.release();
}

static Mutex instanceMutex;
static ConsoleDisplay defaultDisplay;
Display *Display::instance;

Display *Display::getInstance() {
if (instance) return instance;
instanceMutex.lock();
if (!instance) {
instance = &defaultDisplay;
// TODO: create dynamic instance
}
instanceMutex.release();
return instance;
}

Display::~Display() {}
25 changes: 25 additions & 0 deletions src/kernlib/include/kernlib.hpp
@@ -0,0 +1,25 @@
// PhoeniX OS Kernel library functions
// Copyright (C) 2013 PhoeniX
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include "kernlib/cpu.hpp"
#include "kernlib/display.hpp"
#include "kernlib/mem.hpp"
#include "kernlib/mutex.hpp"
#include "kernlib/ports.hpp"
#include "kernlib/printf.hpp"
#include "kernlib/std.hpp"
34 changes: 34 additions & 0 deletions src/kernlib/include/kernlib/cpu.hpp
@@ -0,0 +1,34 @@
// PhoeniX OS Kernel library cpu functions
// Copyright (C) 2013 PhoeniX
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once
#include "std.hpp"

inline static uint64_t rdtsc() {
uint32_t eax, edx;
asm volatile("rdtsc":"=a"(eax), "=d"(edx));
return (((uint64_t)edx << 32) | eax);
}

inline static uint64_t EnterCritical() {
uint64_t flags;
asm volatile("pushfq; cli; pop %q0":"=r"(flags));
return flags;
}

inline static void LeaveCritical(uint64_t flags) {
asm volatile("push %q0; popfq"::"r"(flags));
}
42 changes: 42 additions & 0 deletions src/kernlib/include/kernlib/display.hpp
@@ -0,0 +1,42 @@
// PhoeniX OS Kernel library display functions
// Copyright (C) 2013 PhoeniX
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once
#include "std.hpp"
#include "mutex.hpp"

class Display {
private:
static Display *instance;
public:
static Display *getInstance();
virtual void clean() = 0;
virtual void write(const char*) = 0;
virtual ~Display() = 0;
};

class ConsoleDisplay: public Display {
private:
static constexpr char * const base = reinterpret_cast<char*>(0xB8000);
static constexpr char * const top = reinterpret_cast<char*>(0xB8FA0);
char *display;
void putc(const char c);
Mutex mutex;
public:
ConsoleDisplay();
void write(const char *str);
void clean();
};
25 changes: 25 additions & 0 deletions src/kernlib/include/kernlib/mem.hpp
@@ -0,0 +1,25 @@
// PhoeniX OS Kernel library memory functions
// Copyright (C) 2013 PhoeniX
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once
#include "std.hpp"

class Memory {
public:
static void copy(void* dest, const void* src, size_t count);
static void fill(void *addr, uint8_t value, size_t size);
static void zero(void *addr, size_t size);
};
29 changes: 29 additions & 0 deletions src/kernlib/include/kernlib/mutex.hpp
@@ -0,0 +1,29 @@
// PhoeniX OS Kernel library mutex functions
// Copyright (C) 2013 PhoeniX
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once
#include "std.hpp"

class Mutex {
private:
bool state;
public:
Mutex() {
state = 0;
}
void lock();
void release();
};
48 changes: 48 additions & 0 deletions src/kernlib/include/kernlib/ports.hpp
@@ -0,0 +1,48 @@
// PhoeniX OS Kernel library port I/O functions
// Copyright (C) 2013 PhoeniX
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once
#include "std.hpp"

inline static uint8_t inportb(uint16_t port) {
uint8_t c;
asm volatile("inb %w1, %b0":"=a"(c):"d"(port));
return c;
}

inline static uint16_t inports(uint16_t port) {
uint16_t c;
asm volatile("inw %w1, %w0":"=a"(c):"d"(port));
return c;
}

inline static uint32_t inportl(uint16_t port) {
uint32_t c;
asm volatile("inl %w1, %d0":"=a"(c):"d"(port));
return c;
}

inline static void outportb(uint16_t port, uint8_t c) {
asm volatile("outb %b0, %w1"::"a"(c), "d"(port));
}

inline static void outports(uint16_t port, uint16_t c) {
asm volatile("outw %w0, %w1"::"a"(c), "d"(port));
}

inline static void outportl(uint16_t port, uint32_t c) {
asm volatile("outl %d0, %w1"::"a"(c), "d"(port));
}
31 changes: 31 additions & 0 deletions src/kernlib/include/kernlib/printf.hpp
@@ -0,0 +1,31 @@
// PhoeniX OS Kernel library printf functions
// Copyright (C) 2013 PhoeniX
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#pragma once
#include "std.hpp"

extern "C" {

int printf(const char *format, ...)
__attribute__ ((format (printf, 1, 2)));

int snprintf(char *str, size_t size, const char *format, ...)
__attribute__ ((format (printf, 3, 4)));

int vprintf(const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);

}

0 comments on commit 734cb3b

Please sign in to comment.