Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Kernlib split to multiple files & refactoring
- Loading branch information
Showing
32 changed files
with
541 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |
Oops, something went wrong.