Some refactor

This commit is contained in:
Morten Delenk 2016-06-16 20:10:07 +02:00
parent f0ae0c7c36
commit 21719edafc
41 changed files with 287 additions and 476 deletions

View file

@ -6,7 +6,7 @@ OBJS += $(addsuffix .o,$(basename $(KERNSRCS)))
LD = $(PREFIX)g++ LD = $(PREFIX)g++
LDFLAGS = -nostdlib -nodefaultlibs -nostdlib -fno-builtin LDFLAGS = -nostdlib -nodefaultlibs -nostdlib -fno-builtin
ifeq ($(arch),x86) ifeq ($(arch),x86)
LDFLAGS += -m32 -T kernel.ld LDFLAGS += -m32 -T kernel-$(arch).ld
endif endif
all: hal kernel all: hal kernel
$(LD) $(LDFLAGS) -o mtgos $(OBJS) $(LD) $(LDFLAGS) -o mtgos $(OBJS)

View file

@ -1,26 +0,0 @@
include ../../../kernel.settings
SRCS = $(shell find -name '*.cpp' -o -name '*.[cS]')
OBJS = $(addsuffix .o,$(basename $(SRCS)))
CPP = $(PREFIX)g++
CC = $(PREFIX)gcc
ASFLAGS =
CFLAGS = -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -march=native -std=c11 -fno-builtin -Werror -nostdlib -g
CPPFLAGS = -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g
LDFLAGS = -r
all: $(OBJS)
%.o: %.cpp
$(CPP) $(CPPFLAGS) -c -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $^
%.o: $.S
$(CC) $(CFLAGS) -c -o $@ $^
clean:
rm -rf $(OBJS)
.PHONY: clean all

View file

@ -1,53 +0,0 @@
#ifndef _IO_H
#define _IO_H
#include <stdint.h>
static inline void outb(uint16_t port, uint8_t val) __attribute__((always_inline));
static inline void outb(uint16_t port, uint8_t val) {
asm volatile("outb %0, %1" : : "a"(val), "Nd"(port));
}
static inline void outw(uint16_t port, uint16_t val) __attribute__((always_inline));
static inline void outw(uint16_t port, uint16_t val) {
asm volatile("outw %0, %1" : : "a"(val), "Nd"(port));
}
static inline void outl(uint16_t port, uint32_t val) __attribute__((always_inline));
static inline void outl(uint16_t port, uint32_t val) {
asm volatile("outl %0, %1" : : "a"(val), "Nd"(port));
}
static inline uint8_t inb(uint16_t port) __attribute__((always_inline));
static inline uint8_t inb(uint16_t port) {
uint8_t ret;
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline uint16_t inw(uint16_t port) __attribute__((always_inline));
static inline uint16_t inw(uint16_t port) {
uint16_t ret;
asm volatile("inw %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline uint32_t inl(uint16_t port) __attribute__((always_inline));
static inline uint32_t inl(uint16_t port) {
uint32_t ret;
asm volatile("inl %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline void io_wait() __attribute__((always_inline));
static inline void io_wait() {
asm volatile("outb %%al, $0x80" : : "a"(0));
}
static inline void cli() __attribute__((always_inline));
static inline void cli() {
asm volatile("cli");
}
static inline void sti() __attribute__((always_inline));
static inline void sti() {
asm volatile("sti");
}
static inline uint64_t rdtsc() __attribute__((always_inline));
static inline uint64_t rdtsc()
{
uint64_t ret;
asm volatile ( "rdtsc" : "=A"(ret) );
return ret;
}
#endif

View file

@ -1,9 +0,0 @@
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long long int int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long int uint64_t;
typedef unsigned int uintptr_t;

View file

@ -1,24 +0,0 @@
#ifndef _BASE_HPP
#define _BASE_HPP
#include <stdint.h>
namespace MTGosHAL {
class Output;
class Input;
class Serial;
class Screen;
class Keyboard;
class Multitasking;
class BlockDevice;
class Task;
class PMM;
enum class BG_color: uint16_t;
enum class FG_color: uint16_t;
extern Serial debug;
extern Screen out;
extern Screen err;
extern Keyboard in;
extern Multitasking tasks;
extern BlockDevice disk;
extern PMM mm;
}
#endif

View file

@ -1,16 +0,0 @@
#ifndef _PMM_HPP
#define _PMM_HPP
#include <stdint.h>
namespace MTGosHAL {
class PMM {
private:
uint32_t bitmap[0x8000]; //Enough for 4 GB
public:
auto markUsed(void * addr) -> void;
auto init(struct multiboot_info*) -> void;
auto operator >> (void * &addr) -> PMM &; //alloc
auto operator << (const void * addr) -> PMM &; //free
auto operator () (uint32_t len) -> void*;
};
}
#endif

View file

@ -1,70 +0,0 @@
#ifndef _TEXTDISP_H
#define _TEXTDISP_H
#include <stdint.h>
#include <output.hpp>
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 24
namespace MTGosHAL {
enum class BG_color : uint16_t {
BLACK=0x0000,
BLUE=0x1000,
GREEN=0x2000,
CYAN=0x3000,
RED=0x4000,
MAGENTA=0x5000,
BROWN=0x6000,
LIGHT_GREY=0x7000,
GREY=0x8000,
LIGHT_BLUE=0x9000,
LIGHT_GREEN=0xA000,
LIGHT_CYAN=0xB000,
LIGHT_RED=0xC000,
LIGHT_MAGENTA=0xD000,
YELLOW=0xE000,
WHITE=0xF000
};
enum class FG_color : uint16_t {
BLACK=0x000,
BLUE=0x100,
GREEN=0x200,
CYAN=0x300,
RED=0x400,
MAGENTA=0x500,
BROWN=0x600,
LIGHT_GREY=0x700,
GREY=0x800,
LIGHT_BLUE=0x900,
LIGHT_GREEN=0xA00,
LIGHT_CYAN=0xB00,
LIGHT_RED=0xC00,
LIGHT_MAGENTA=0xD00,
YELLOW=0xE00,
WHITE=0xF00
};
class Screen: public Output {
private:
FG_color fg;
BG_color bg;
uint16_t* vmem=(uint16_t*)0xB8000;
auto putChar(char c) -> void;
public:
Screen() {
clrscr();
}
template <typename T>
auto operator<< (T output) -> Screen & {
Output::operator<<<T>(output);
return *this;
}
auto clrscr() -> void;
auto scroll() -> void;
auto setColor(FG_color fg) -> Screen &;
auto setColor(BG_color bg) -> Screen &;
auto setColor(FG_color fg, BG_color bg) -> Screen &;
};
template <>
auto Screen::operator<<<FG_color>(FG_color fg) -> Screen &;
template <>
auto Screen::operator<<<BG_color>(BG_color bg) -> Screen &;
}
#endif

View file

@ -5,8 +5,8 @@ OBJS = $(addsuffix .o,$(basename $(SRCS)))
CPP = $(PREFIX)g++ CPP = $(PREFIX)g++
CC = $(PREFIX)gcc CC = $(PREFIX)gcc
ASFLAGS = -m32 ASFLAGS = -m32
CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -I../../kernel/c_include -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie
CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fno-pie -Wno-reorder CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -I../../kernel/c_include -I../../kernel/include -fno-rtti -fno-exceptions -ffreestanding -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fno-pie -Wno-reorder
LDFLAGS = -r -melf_i386 LDFLAGS = -r -melf_i386

View file

@ -1,34 +1,31 @@
#ifndef MULTITASKING_H #ifndef MULTITASKING_H
#define MULTITASKING_H #define MULTITASKING_H
#define PID_KERNEL 0
#define PID_INIT 1
#include <idt.hpp>
namespace MTGosHAL { namespace MTGosHAL {
class Multitasking class Multitasking
{ {
public: public:
Multitasking(); Multitasking();
auto schedule(struct cpu_state* cpu) -> struct cpu_state*; auto schedule(struct cpu_state* cpu) -> struct cpu_state*;
auto initTask(void(*entry)()) -> struct cpu_state*; auto initTask(void(*entry)()) -> struct cpu_state*;
uint32_t tss[32]; uint32_t tss[32];
protected: protected:
private: private:
Task* first_task; Task* first_task;
Task* curr_task; Task* curr_task;
}; };
class Task class Task
{ {
private: private:
struct cpu_state* cpu_state; struct cpu_state* cpu_state;
Task* next; Task* next;
public: public:
Task(struct cpu_state*); Task(struct cpu_state*);
auto unpause() -> struct cpu_state*; auto unpause() -> struct cpu_state*;
auto pause(struct cpu_state*) -> Task *; auto pause(struct cpu_state*) -> Task *;
auto addTask(Task*) -> void; auto addTask(Task*) -> void;
auto hasNext() -> bool; auto hasNext() -> bool;
}; };
} // namespace MTGosHAL } // namespace MTGosHAL

View file

@ -13,8 +13,10 @@
#define SEG_DBL_FAULT 0x28 /*Only use for double fault handler!!*/ #define SEG_DBL_FAULT 0x28 /*Only use for double fault handler!!*/
extern "C" { extern "C" {
void loadIDT(void * ptr); void loadIDT(void * ptr);
}
namespace MTGosHAL {
struct cpu_state { struct cpu_state {
// Von Hand gesicherte Register
uint32_t eax; uint32_t eax;
uint32_t ebx; uint32_t ebx;
uint32_t ecx; uint32_t ecx;
@ -22,17 +24,16 @@ extern "C" {
uint32_t esi; uint32_t esi;
uint32_t edi; uint32_t edi;
uint32_t ebp; uint32_t ebp;
uint32_t intr; uint32_t intr;
uint32_t error; uint32_t error;
// Von der CPU gesichert
uint32_t eip; uint32_t eip;
uint32_t cs; uint32_t cs;
uint32_t eflags; uint32_t eflags;
uint32_t esp; uint32_t esp;
uint32_t ss; uint32_t ss;
}; };
}
namespace MTGosHAL {
class IDT { class IDT {
private: private:
uint64_t idt[256]; uint64_t idt[256];
@ -50,5 +51,4 @@ namespace MTGosHAL {
auto request(uint8_t intr, struct cpu_state* (*handler)(struct cpu_state*)) -> bool; auto request(uint8_t intr, struct cpu_state* (*handler)(struct cpu_state*)) -> bool;
}; };
} }
extern "C" struct cpu_state* handleINT(struct cpu_state* cpu);
#endif #endif

View file

@ -1,29 +0,0 @@
#ifndef _INPUT_HPP
#define _INPUT_HPP
namespace MTGosHAL {
class Input {
private:
virtual auto getChar() -> char = 0;
public:
auto operator>>(char &input) -> Input & {
input=getChar();
return *this;
}
//Note that it receives up to 256 bytes.
auto operator>>(char* input) -> Input & {
int ptr=0;
char tmp='\0';
while(ptr<256 && tmp!='\r') {
while(!(tmp=getChar()));
if(tmp=='\r')
input[ptr++]='\0';
else
input[ptr++]=tmp;
}
input[255]='\0';
return *this;
}
//TODO: Add more possibilities.
};
}
#endif

View file

@ -1,41 +0,0 @@
#ifndef _KEYCODES_HPP
#define _KEYCODES_HPP
bool keydowns[0x69];
uint8_t keyboardKeycodes[2][128] {
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x00, 0x00, 0x54, 0x55,
0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}, {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x57, 0x58, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00,
0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5B,
0x5C, 0x5D, 0x00, 0x5E, 0x00, 0x5F, 0x00, 0x60,
0x61, 0x62, 0x63, 0x64, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x64, 0x65, 0x66, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}
};
#endif

View file

@ -1,17 +0,0 @@
#ifndef _KEYMAP_DE_HPP
#define _KEYMAP_DE_HPP
#include <keycodes.hpp>
#define SHIFT 1
#define CTRL 2
#define ALT 4
char keymap[8][0x80] = {
"\0\0001234567890\xe1\0\b\0qwertzuiop\x81+\n\0asdfghjkl\x94\x84^\0#yxcvbnm,.-\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\000789-456+1230,<\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // No modifier
"\0\000!\"\0$%&/()=?`\b\0QWERTZUIOP\x9A*\n\0ASDFGHJKL\x99\x8e\xF8\0'YXCVBNM;:_\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,>\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Shift modifier
"\0\0001234567890\xe1\0\b\0qwertzuiop\x81+\n\0asdfghjkl\x94\x84^\0#yxcvbnm,.-\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,<\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Control modifier
"\0\000!\"\0$%&/()=?`\b\0QWERTZUIOP\x9A*\n\0ASDFGHJKL\x99\x8e\xF8\0'YXCVBNM;:_\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,>\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Control+Shift modifiers
"\0\0001234567890\xe1\0\b\0qwertzuiop\x81+\n\0asdfghjkl\x94\x84^\0#yxcvbnm,.-\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,<\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Alt modifier
"\0\000!\"\0$%&/()=?`\b\0QWERTZUIOP\x9A*\n\0ASDFGHJKL\x99\x8e\xF8\0'YXCVBNM;:_\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,>\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Alt+Shift modifiers
"\0\0001\xFD\x33\xAC\xAB\xAA{[]}\\\xa7\b\0@w\xEErtzui\xEDp\x81~\n\0\x91sdfghjkl\x94^\0#\xAF\xAE\x9Bvbn\xe6,.-\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,|\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Control+Alt modifiers
"\0\000\xAD\"\x9C\x0F%&/(\xF1\xF8\xA8`\b\0\xEAW\xEERT\x9DUIOP\xF8*\n\0\x92SD\xA6GHJ&L\x99\x8e\xF8\0|YXCVBN\xF8;\xF6_\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,|\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Control+Shift modifiers
};
#endif

View file

@ -1,46 +0,0 @@
#ifndef _OUTPUT_HPP
#define _OUTPUT_HPP
#include <base.hpp>
#include <stdint.h>
namespace MTGosHAL {
enum class Base : int {
BINARY=2,
TERNARY,
BASE4,
BASE5,
BASE6,
BASE7,
OCTAL,
BASE9,
DECIMAL,
BASE11,
BASE12,
BASE13,
BASE14,
BASE15,
HEXADECIMAL
};
/* abstract */ class Output {
private:
virtual auto putChar(char c) -> void = 0;
auto puts(const char* s) -> void;
int base=10;
public:
template <typename T>
auto operator<<(T output) -> Output & {
puts(output); //kernel won't compile if class cannot be converted into string
return *this;
}
};
template <>
auto Output::operator<<<Base>(Base output) -> Output &;
template <>
auto Output::operator<<<int>(int output) -> Output &;
template <>
auto Output::operator<<<char>(char output) -> Output &;
template <>
auto Output::operator<<<char*>(char* output) -> Output &;
}
#endif

View file

@ -3,12 +3,12 @@
#include <Multitasking.hpp> #include <Multitasking.hpp>
#include <serial.hpp> #include <serial.hpp>
#include <blockdev.hpp> #include <blockdev.hpp>
#include <idt.hpp>
#include <vmm3.hpp> #include <vmm3.hpp>
namespace MTGosHAL {
auto schedule(struct cpu_state* cpu) -> struct cpu_state* { auto schedule(struct cpu_state* cpu) -> struct cpu_state* {
return MTGosHAL::tasks.schedule(cpu); return MTGosHAL::tasks.schedule(cpu);
} }
namespace MTGosHAL {
Multitasking::Multitasking(): curr_task(nullptr), first_task(nullptr) Multitasking::Multitasking(): curr_task(nullptr), first_task(nullptr)
{ {
for(int i=0;i<32;i++) { for(int i=0;i<32;i++) {
@ -19,7 +19,7 @@ Multitasking::Multitasking(): curr_task(nullptr), first_task(nullptr)
tss[2]=0x10; tss[2]=0x10;
//task_states[0] = initTask(stack_a, user_stack_a, task_a); //task_states[0] = initTask(stack_a, user_stack_a, task_a);
//task_states[1] = initTask(stack_b, user_stack_b, task_b); //task_states[1] = initTask(stack_b, user_stack_b, task_b);
if(!idt.request(0x20,::schedule)) { if(!idt.request(0x20,MTGosHAL::schedule)) {
err << "Could not start multitasking\nFatal error; Kernel halted!\n"; err << "Could not start multitasking\nFatal error; Kernel halted!\n";
while(true) while(true)
asm volatile("cli; hlt"); asm volatile("cli; hlt");

View file

@ -86,6 +86,6 @@ namespace MTGosHAL {
return false; return false;
} }
} }
extern "C" struct cpu_state* handleINT(struct cpu_state* cpu) { extern "C" void* handleINT(void* cpu) {
return MTGosHAL::idt.handle(cpu); return (void*)MTGosHAL::idt.handle((MTGosHAL::cpu_state*)cpu);
} }

View file

@ -3,10 +3,11 @@
#include <serial.hpp> #include <serial.hpp>
#include <keyboard.hpp> #include <keyboard.hpp>
#include <keymap_DE.hpp> #include <keymap_DE.hpp>
auto handleIRQ(struct cpu_state* cpu) -> struct cpu_state* {
return MTGosHAL::in.handleIRQ1(cpu);
}
namespace MTGosHAL { namespace MTGosHAL {
auto handleIRQ(struct cpu_state* cpu) -> struct cpu_state* {
return in.handleIRQ1(cpu);
}
auto Keyboard::getChar() -> char { auto Keyboard::getChar() -> char {
char chr=buf[0]; char chr=buf[0];
for(int i=0;i<15;i++) { for(int i=0;i<15;i++) {

View file

@ -2,85 +2,7 @@
#include <textDISP.hpp> #include <textDISP.hpp>
#include <string.h> #include <string.h>
#include <stdfnt.h> #include <stdfnt.h>
int x=0, y=0;
namespace MTGosHAL { namespace MTGosHAL {
auto Screen::putChar(char c) -> void {
switch(c) {
case '\n':
x=0; y++;
break;
case '\r':
x=0;
break;
case '\b':
x--;
if(x<0) x=0;
putChar(' ');
x--;
if(x<0) x=0;
break;
case '\0':
break;
default:
for(int lx=0;lx<8;lx++) {
for(int ly=0;ly<8;ly++) {
if(font[(int)((uint8_t)c)][ly]&(1<<lx)) {
lfb[(x*8+lx)+(y*8+ly)*1024]=0xFFFFFF;//static_cast<uint32_t>(fg);
} else {
lfb[(x*8+lx)+(y*8+ly)*1024]=0x000000;//static_cast<uint32_t>(bg);
}
}
}
x++;
if(x==SCREEN_WIDTH) {
x=0; y++;
}
break;
}
if(y>SCREEN_HEIGHT)
scroll();
}
auto Screen::clrscr() -> void {
for(int p=0;p<1024*786;p++) {
lfb[p]=0x000000;//static_cast<uint32_t>(bg);
}
x=y=0;
}
auto Screen::scroll() -> void {
for(int ly=0;ly<786-8;ly++) {
for(int lx=0;lx<1024;lx++) {
lfb[lx+ly*1024]=lfb[lx+(ly+8)*1024];
}
}
for(int ly=786-8;ly<786;ly++) {
for(int lx=0;lx<1024;lx++) {
lfb[lx+ly*1024]=0x000000;//static_cast<uint32_t>(bg);
}
}
y--;
}
template <>
auto Screen::operator<<<FG_color>(FG_color fg) -> Screen &{
this->fg=fg;
return *this;
}
template <>
auto Screen::operator<<<BG_color>(BG_color bg) -> Screen &{
this->bg=bg;
return *this;
}
auto Screen::setColor(FG_color fg) -> Screen &{
this->fg=fg;
return *this;
}
auto Screen::setColor(BG_color bg) -> Screen &{
this->bg=bg;
return *this;
}
auto Screen::setColor(FG_color fg, BG_color bg) -> Screen &{
return (*this).setColor(fg).setColor(bg);
}
auto Screen::init(struct multiboot_info* mb_info) -> void { auto Screen::init(struct multiboot_info* mb_info) -> void {
lfb=(uint32_t*)((uint32_t)mb_info->framebuffer_addr); lfb=(uint32_t*)((uint32_t)mb_info->framebuffer_addr);
//clrscr(); //clrscr();

31
kernel/kernel-x86_64.ld Normal file
View file

@ -0,0 +1,31 @@
ENTRY(_start)
OUTPUT_FORMAT(elf32-i386)
OUTPUT_ARCH(i386:x86-64)
SECTIONS
{
. = 0x100000;
kernel_start = .;
.text : {
*(multiboot)
*(.text)
}
.data ALIGN(4096) : {
start_ctors = .;
KEEP(*( .init_array ));
KEEP(*(SORT_BY_INIT_PRIORITY( .init_array.* )));
*(.ctors)
end_ctors = .;
start_dtors = .;
*(.dtors)
end_dtors = .;
*(.data)
}
.rodata ALIGN(4096) : {
*(.rodata)
}
.bss ALIGN(4096) : {
*(.bss)
}
. = ALIGN(4096);
kernel_end = .;
}

View file

@ -4,8 +4,8 @@ OBJS = $(addsuffix .o,$(basename $(SRCS)))
CPP = $(PREFIX)g++ CPP = $(PREFIX)g++
CC = $(PREFIX)gcc CC = $(PREFIX)gcc
ASFLAGS = -m32 ASFLAGS = -m32
CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -I../hal/dummy/c_include -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie
CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -I../hal/dummy/include -I../hal/dummy/c_include -fno-rtti -fno-exceptions -ffreestanding -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fpie CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fpie
all: $(OBJS) all: $(OBJS)
%.o: %.cpp %.o: %.cpp

View file

@ -0,0 +1,2 @@
struct cpu_state {
}; //Empty struct

View file

@ -0,0 +1,21 @@
#ifndef __LP64__
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long long int int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long int uint64_t;
typedef unsigned int uintptr_t;
#else
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long int int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long int uint64_t;
typedef unsigned long int uintptr_t;
#endif

View file

@ -1,11 +1,13 @@
#ifndef _STRING_H #ifndef _STRING_H
#define _STRING_H #define _STRING_H
#ifdef _cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <stdint.h> #include <stdint.h>
void memmove(void* dst, void* src, uint32_t size); void memmove(void* dst, void* src, uint32_t size);
#ifdef _cplusplus uint32_t strlen(const char* str);
int strcmp(const char* str1, const char* str2);
#ifdef __cplusplus
} }
#endif #endif
#endif #endif

View file

@ -1,5 +1,6 @@
#ifndef MULTITASKING_H #ifndef MULTITASKING_H
#define MULTITASKING_H #define MULTITASKING_H
#include <cpu_state.h>
namespace MTGosHAL { namespace MTGosHAL {
class Multitasking class Multitasking
@ -8,7 +9,6 @@ namespace MTGosHAL {
Multitasking(); Multitasking();
auto schedule(struct cpu_state* cpu) -> struct cpu_state*; auto schedule(struct cpu_state* cpu) -> struct cpu_state*;
auto initTask(void(*entry)()) -> struct cpu_state*; auto initTask(void(*entry)()) -> struct cpu_state*;
uint32_t tss[32];
protected: protected:
private: private:
Task* first_task; Task* first_task;

View file

@ -2,7 +2,6 @@
#define _KEYBOARD_HPP #define _KEYBOARD_HPP
#include <stdint.h> #include <stdint.h>
#include <input.hpp> #include <input.hpp>
#include <io.h>
namespace MTGosHAL { namespace MTGosHAL {
class Keyboard: public Input { class Keyboard: public Input {
@ -12,8 +11,6 @@ namespace MTGosHAL {
char buf[16]; char buf[16];
int len; int len;
auto getChar() -> char; auto getChar() -> char;
auto sendCommand(uint8_t command) -> void;
bool numlock, capslock, scrolllock, response;
public: public:
auto handleIRQ1(struct cpu_state* cpu) -> struct cpu_state*; auto handleIRQ1(struct cpu_state* cpu) -> struct cpu_state*;
Keyboard(); Keyboard();

View file

@ -19,23 +19,22 @@ uint8_t keyboardKeycodes[2][128] {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}, { }, {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x57, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57, 0x58, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00,
0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5B,
0x5C, 0x5D, 0x00, 0x5E, 0x00, 0x5F, 0x00, 0x60, 0x5C, 0x5D, 0x00, 0x5E, 0x00, 0x5F, 0x00, 0x60,
0x61, 0x62, 0x63, 0x64, 0x00, 0x00, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x64, 0x65, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x65, 0x66, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
} }
}; };
#endif #endif

View file

@ -0,0 +1,20 @@
#ifndef _PMM_HPP
#define _PMM_HPP
#include <stdint.h>
namespace MTGosHAL {
struct malloc_t {
uint32_t len;
malloc_t *last;
malloc_t *next;
};
class PMM {
private:
malloc_t *head;
public:
PMM();
auto init(void *) -> void;
auto alloc(uint32_t length) -> void *;
auto free(void* ptr) -> bool;
};
}
#endif

View file

@ -3,7 +3,6 @@
#include <output.hpp> #include <output.hpp>
#include <input.hpp> #include <input.hpp>
#include <textDISP.hpp> #include <textDISP.hpp>
#include <io.h>
#define SERIAL_IER 1 #define SERIAL_IER 1
#define SERIAL_IIR 2 #define SERIAL_IIR 2
#define SERIAL_FCR 2 #define SERIAL_FCR 2
@ -14,9 +13,6 @@
namespace MTGosHAL { namespace MTGosHAL {
class Serial: public Output, public Input { class Serial: public Output, public Input {
private: private:
uint16_t port;
uint64_t waittimes;
uint64_t transmits;
bool works; bool works;
auto isTransmitEmpty() -> int; auto isTransmitEmpty() -> int;
auto putChar(char chr) -> void; auto putChar(char chr) -> void;

View file

@ -1,6 +1,6 @@
//Thanks to masterq32 for this font! //Thanks to masterq32 for this font!
//You should check out his OS project (https://github.com/MasterQ32/DasOS) //You should check out his OS project (https://github.com/MasterQ32/DasOS)
uint8_t font[256][8] = { static uint8_t font[256][8] = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // ? { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // ?
{ 0x00, 0x00, 0x0D, 0x12, 0x12, 0x09, 0x09, 0x00 }, // ? { 0x00, 0x00, 0x0D, 0x12, 0x12, 0x09, 0x09, 0x00 }, // ?
{ 0x00, 0x00, 0x11, 0x11, 0x11, 0x19, 0x16, 0x00 }, // ? { 0x00, 0x00, 0x11, 0x11, 0x11, 0x19, 0x16, 0x00 }, // ?

View file

@ -0,0 +1,70 @@
#ifndef _TEXTDISP_H
#define _TEXTDISP_H
#include <base.hpp>
#include <stdint.h>
#include <output.hpp>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 96
namespace MTGosHAL {
enum class BG_color : uint32_t {
BLACK=0x000000,
BLUE=0x0000AA,
GREEN=0x00AA00,
CYAN=0x00AAAA,
RED=0xAA0000,
MAGENTA=0xAA00AA,
BROWN=0xAA5500,
LIGHT_GREY=0xAAAAAA,
GREY=0x555555,
LIGHT_BLUE=0x5555FF,
LIGHT_GREEN=0x55FF55,
LIGHT_CYAN=0x55FFFF,
LIGHT_RED=0xFF5555,
LIGHT_MAGENTA=0xFF55FF,
YELLOW=0xFFFF55,
WHITE=0xFFFFFF
};
enum class FG_color : uint32_t {
BLACK=0x000000,
BLUE=0x0000AA,
GREEN=0x00AA00,
CYAN=0x00AAAA,
RED=0xAA0000,
MAGENTA=0xAA00AA,
BROWN=0xAA5500,
LIGHT_GREY=0xAAAAAA,
GREY=0x555555,
LIGHT_BLUE=0x5555FF,
LIGHT_GREEN=0x55FF55,
LIGHT_CYAN=0x55FFFF,
LIGHT_RED=0xFF5555,
LIGHT_MAGENTA=0xFF55FF,
YELLOW=0xFFFF55,
WHITE=0xFFFFFF
};
class Screen: public Output {
private:
FG_color fg;
BG_color bg;
uint32_t* lfb;
auto putChar(char c) -> void;
public:
Screen(): fg(FG_color::WHITE), bg(BG_color::BLACK) {
}
template <typename T>
auto operator<< (T output) -> Screen & {
Output::operator<<<T>(output);
return *this;
}
auto clrscr() -> void;
auto scroll() -> void;
auto setColor(FG_color fg) -> Screen &;
auto setColor(BG_color bg) -> Screen &;
auto setColor(FG_color fg, BG_color bg) -> Screen &;
};
template <>
auto Screen::operator<<<FG_color>(FG_color fg) -> Screen &;
template <>
auto Screen::operator<<<BG_color>(BG_color bg) -> Screen &;
}
#endif

View file

@ -3,7 +3,7 @@
#include <serial.hpp> #include <serial.hpp>
#include <textDISP.hpp> #include <textDISP.hpp>
#include <keyboard.hpp> #include <keyboard.hpp>
#include <Multitasking.h> #include <Multitasking.hpp>
#include <blockdev.hpp> #include <blockdev.hpp>
#include <elf.hpp> #include <elf.hpp>
using namespace MTGosHAL; using namespace MTGosHAL;

View file

@ -0,0 +1,84 @@
#include <base.hpp>
#include <textDISP.hpp>
#include <string.h>
#include <stdfnt.h>
int x=0, y=0;
namespace MTGosHAL {
auto Screen::putChar(char c) -> void {
switch(c) {
case '\n':
x=0; y++;
break;
case '\r':
x=0;
break;
case '\b':
x--;
if(x<0) x=0;
putChar(' ');
x--;
if(x<0) x=0;
break;
case '\0':
break;
default:
for(int lx=0;lx<8;lx++) {
for(int ly=0;ly<8;ly++) {
if(font[(int)((uint8_t)c)][ly]&(1<<lx)) {
lfb[(x*8+lx)+(y*8+ly)*1024]=0xFFFFFF;//static_cast<uint32_t>(fg);
} else {
lfb[(x*8+lx)+(y*8+ly)*1024]=0x000000;//static_cast<uint32_t>(bg);
}
}
}
x++;
if(x==SCREEN_WIDTH) {
x=0; y++;
}
break;
}
if(y>SCREEN_HEIGHT)
scroll();
}
auto Screen::clrscr() -> void {
for(int p=0;p<1024*786;p++) {
lfb[p]=0x000000;//static_cast<uint32_t>(bg);
}
x=y=0;
}
auto Screen::scroll() -> void {
for(int ly=0;ly<786-8;ly++) {
for(int lx=0;lx<1024;lx++) {
lfb[lx+ly*1024]=lfb[lx+(ly+8)*1024];
}
}
for(int ly=786-8;ly<786;ly++) {
for(int lx=0;lx<1024;lx++) {
lfb[lx+ly*1024]=0x000000;//static_cast<uint32_t>(bg);
}
}
y--;
}
template <>
auto Screen::operator<<<FG_color>(FG_color fg) -> Screen &{
this->fg=fg;
return *this;
}
template <>
auto Screen::operator<<<BG_color>(BG_color bg) -> Screen &{
this->bg=bg;
return *this;
}
auto Screen::setColor(FG_color fg) -> Screen &{
this->fg=fg;
return *this;
}
auto Screen::setColor(BG_color bg) -> Screen &{
this->bg=bg;
return *this;
}
auto Screen::setColor(FG_color fg, BG_color bg) -> Screen &{
return (*this).setColor(fg).setColor(bg);
}
}