First commit. Added an untested Serial Port write driver, which is used for debug.

This commit is contained in:
Morten Delenk 2015-10-10 14:59:23 +02:00
commit 6f6ff0d773
13 changed files with 284 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*~
mtgos
*.o

12
Makefile Normal file
View file

@ -0,0 +1,12 @@
all: mtgos
mtgos:
make -C kernel
mv kernel/mtgos mtgos
clean:
make -C kernel clean
rm -rf mtgos
.PHONY: all mtgos clean

BIN
core Normal file

Binary file not shown.

10
kernel/Makefile Normal file
View file

@ -0,0 +1,10 @@
LD = ld
LDFLAGS = -melf_i386 -Tkernel.ld
all: hal
$(LD) $(LDFLAGS) -o mtgos hal/hal.o
hal:
make -C hal
clean:
make -C hal clean
.PHONY: all hal clean

12
kernel/hal/Makefile Normal file
View file

@ -0,0 +1,12 @@
all: x86
#Currently only do X86
mv x86/hal.o hal.o
x86:
make -C x86
clean:
rm -rf hal.o
make -C x86 clean
.PHONY: all x86 clean

27
kernel/hal/x86/Makefile Normal file
View file

@ -0,0 +1,27 @@
SRCS = $(shell find -name '*.cpp' -o -name '*.[cS]')
OBJS = $(addsuffix .o,$(basename $(SRCS)))
CPP = g++
CC = gcc
LD = ld
ASFLAGS = -m32
CFLAGS = -m32 -Wall -g -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -fbuiltin
CPPFLAGS = -m32 -Wall -g -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -fbuiltin
LDFLAGS = -r -melf_i386
hal.o: $(OBJS)
$(LD) $(LDFLAGS) -o $@ $^
%.o: %.cpp
$(CPP) $(CPPFLAGS) -c -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $^
%.o: $.S
$(CC) $(CFLAGS) -c -o $@ $^
clean:
rm -rf $(OBJS)
.PHONY: clean

View file

@ -0,0 +1,21 @@
.section multiboot
#define MB_MAGIC 0x1BADB002
#define MB_FLAGS 0x0
#define MB_CHECKSUM -(MB_MAGIC + MB_FLAGS)
.align 4
.int MB_MAGIC
.int MB_FLAGS
.int MB_CHECKSUM
.section .text
.extern init
.global _start
_start:
mov $kernel_stack, %esp
call init
_stop:
cli
hlt
jmp _stop
.section .bss
.space 65536
kernel_stack:

View file

@ -0,0 +1,38 @@
#ifndef _IO_H
#define _IO_H
#include <stdint.h>
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) {
asm volatile("outw %0, %1" : : "a"(val), "Nd"(port));
}
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) {
uint8_t ret;
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
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) {
uint32_t ret;
asm volatile("inl %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
static inline void io_wait() {
asm volatile("outb %%al, $0x80" : : "a"(0));
}
static inline void cli() {
asm volatile("cli");
}
static inline void sti() {
asm volatile("sti");
}
#endif

View file

@ -0,0 +1,8 @@
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;

View file

@ -0,0 +1,67 @@
#ifndef _OUTPUT_HPP
#define _OUTPUT_HPP
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 void putChar(char c) { /*ignore*/ };
auto puts(const char* s) -> void {
int i=0;
while(s[i]!='\0')
putChar(s[i++]);
}
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 & {
base=static_cast<int>(output);
return *this;
}
template <>
auto Output::operator<<<int>(int output) -> Output & {
const char* chars="0123456789ABCDEF";
char buf[33];
buf[32]='\0';
char* ptr=buf+31;
do {
*(ptr--)=chars[output%base];
output/=base;
} while(output && (ptr!=buf));
puts(ptr+1);
return *this;
}
template <>
auto Output::operator<<<char>(char output) -> Output & {
putChar(output);
return *this;
}
template <>
auto Output::operator<<<char*>(char* output) -> Output & {
puts(output);
return *this;
}
}
#endif

View file

@ -0,0 +1,52 @@
#ifndef _SERIAL_HPP
#define _SERIAL_HPP
#include <output.hpp>
#include <io.h>
#define SERIAL_IER 1
#define SERIAL_IIR 2
#define SERIAL_FCR 2
#define SERIAL_LCR 3
#define SERIAL_MCR 4
#define SERIAL_LSR 5
#define SERIAL_MSR 6
namespace MTGosHAL {
class Serial: public Output {
private:
uint16_t port;
auto isTransmitEmpty() -> int {
return inb(port+SERIAL_LSR)&0x20;
}
auto putChar(char chr) -> void {
while(!isTransmitEmpty());
outb(port, chr);
}
auto puts(const char* s) -> void {
int i=0;
while(s[i]!='\0')
putChar(s[i++]);
}
public:
Serial(uint32_t baud) {
port=*((uint16_t*)0x0400);
union {
uint8_t b[2];
uint16_t w;
} divisor;
divisor.w = 115200/baud;
//Turn off interrupts
outb(port+SERIAL_IER, 0x00);
//Set DLAB-bit
outb(port+SERIAL_LCR, 0x80);
//Set baud divisor
outb(port, divisor.b[0]);
outb(port+1, divisor.b[1]);
//Set bit count, parity and reset DLAB
outb(port+SERIAL_LCR, 3);
//Finish init
outb(port+SERIAL_FCR, 0xC7);
outb(port+SERIAL_MCR, 0x0B);
}
};
}
#endif

View file

@ -0,0 +1,16 @@
#include <output.hpp>
#include <serial.hpp>
namespace MTGosHAL {
Serial serialOUT(115200);
void main() {
serialOUT=Serial(115200);
serialOUT << "This is the debug output of MTGos\n";
}
}
extern "C" void init() {
MTGosHAL::main();
}
extern "C" void __cxa_pure_virtual() {
for(;;);
}

18
kernel/kernel.ld Normal file
View file

@ -0,0 +1,18 @@
ENTRY(_start)
SECTIONS
{
. = 0x100000;
.text : {
*(multiboot)
*(.text)
}
.data ALIGN(4096) : {
*(.data)
}
.rodata ALIGN(4096) : {
*(.rodata)
}
.bss ALIGN(4096) : {
*(.bss)
}
}