This is v0.01 release. I added the first syscall!

! Fixes in malloc. Now space-areas will no longer overlap.
This commit is contained in:
Morten Delenk 2016-05-22 18:13:06 +00:00
parent 3f6435fb31
commit f71e781839
12 changed files with 325 additions and 37 deletions

View file

@ -3,6 +3,7 @@
#include <idt.hpp>
#include <serial.hpp>
#include <textDISP.hpp>
auto syscall(uint32_t syscall_num, void* handle, void* args) -> void*;
namespace MTGosHAL {
IDT::IDT() {
//Init PIC
@ -52,6 +53,8 @@ namespace MTGosHAL {
if(ivt[cpu->intr][i])
new_cpu=ivt[cpu->intr][i](new_cpu);
}
if(cpu->intr>=48)
cpu->eax=(uint32_t)(::syscall(cpu->eax, (void*)(cpu->ebx), (void*)(cpu->esp)));
return new_cpu;
}
auto IDT::request(uint8_t intr, struct cpu_state* (*handler)(struct cpu_state*)) -> bool {

View file

@ -40,31 +40,10 @@ namespace MTGosHAL {
for(int i=0;i<256;i++) {
idt.setEntry(i, (void *)((uint32_t)&intr_stub_0+i*16), SEG_KERNEL, IDT_INTERRUPT_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED);
}
idt.setEntry(48, (void *)((uint32_t)&intr_stub_0+768), SEG_KERNEL, IDT_TRAP_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED);
idt.setEntry(48, (void *)((uint32_t)&intr_stub_0+768), SEG_KERNEL, IDT_TRAP_GATE | IDT_SEG_32_BIT | IDT_RING_3 | IDT_USED);
idt.setEntry(8, (void *)((uint32_t)&intr_stub_0+128), SEG_DBL_FAULT, IDT_TASK_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED);
idt.apply();
mm.init(ebx);
debug << "Now tesing the PMM... Allocating 1 byte\n";
char* tmp=(char*)mm.alloc(1);
debug << "Allocating another byte\n";
char* tmp2=(char*)mm.alloc(1);
tmp2[0]='A';
debug << "Freeing the first byte...\n";
mm.free((void*)tmp);
debug << "Allocating 14 bytes... \n";
tmp=(char*)mm.alloc(14);
debug << "Changing the last byte... \n";
tmp[13]='B';
debug << "Changing if the second byte has changed...\n";
if(tmp2[0]=='B') {
err << "The allocate function is broken.\n";
debug << "The allocate function is broken.\n";
} else {
debug << "The allocate function works!\n";
}
debug << "Freeing up both pointers.";
mm.free((void*)tmp);
mm.free((void*)tmp2);
multiboot_mod_list *mods = (multiboot_mod_list*) ebx->mods_addr;
void** progs=(void**)mm.alloc(4096);
uint32_t i;

View file

@ -47,7 +47,8 @@ auto PMM::alloc(uint32_t length) -> void * {
malloc_t* last=nullptr;
do {
uint32_t loc=(uint32_t)curr+sizeof(malloc_t)+curr->len;
if((loc+length+sizeof(malloc_t))<((loc&(~0xFFF))+4096)) {
if((loc+length+sizeof(malloc_t))<((loc&(~0xFFF))+4096) &&
((!curr->next) || (loc+length+sizeof(malloc_t))<((uint32_t)(curr->next)))) {
malloc_t *allocd=(malloc_t *)loc;
allocd->len=length;
allocd->last=curr;

View file

@ -0,0 +1,81 @@
#ifndef _SYSCALL_HPP
#define _SYSCALL_HPP
#include <stdint.h>
enum class BGColor : uint32_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 FGColor : uint32_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 ScreenOut {
private:
bool err;
public:
ScreenOut(bool err);
auto operator <<(char*) -> ScreenOut &; // puts(char *)
auto clrscr() -> ScreenOut &;
auto setColor(BGColor, FGColor) -> ScreenOut &;
~ScreenOut();
};
class Keyboard {
public:
Keyboard();
auto operator >>(char &) -> Keyboard &; // getc()
auto operator >>(char * &) -> Keyboard &; // gets an entire line
auto getKeyCode() -> uint32_t; // gets a RAW keycode
~Keyboard();
};
class Serial {
public:
Serial();
auto operator <<(char *) -> Serial &; // puts(char *)
auto operator >>(char &) -> Serial &; // getc()
auto operator >>(char * &) -> Serial &; // gets an entire line
~Serial();
};
class Disk {
public:
Disk(char * diskName);
auto readSector(uint64_t sectorNum, uint8_t buf[512]) -> Disk &;
auto writeSector(uint64_t sectorNum, uint8_t buf[512]) -> Disk &;
~Disk();
};
class MM {
public:
MM();
auto getPage() -> void *;
auto freePage(void*) -> void;
~MM();
};
#endif

71
kernel/kernel/syscall.cpp Normal file
View file

@ -0,0 +1,71 @@
#include <syscall.hpp>
#include <base.hpp>
#include <textDISP.hpp>
ScreenOut::ScreenOut(bool err): err(err) {
}
auto ScreenOut::operator<<(char *text) -> ScreenOut & {
if(!text)
return *this;
if(err)
MTGosHAL::err << text;
else
MTGosHAL::out << text;
return *this;
}
auto ScreenOut::clrscr() -> ScreenOut & {
if(err)
MTGosHAL::err.clrscr();
else
MTGosHAL::out.clrscr();
return *this;
}
auto ScreenOut::setColor(BGColor bg, FGColor fg) -> ScreenOut & {
if(err)
MTGosHAL::err << static_cast<MTGosHAL::BG_color>(bg) << static_cast<MTGosHAL::FG_color>(fg) ;
else
MTGosHAL::out << static_cast<MTGosHAL::BG_color>(bg) << static_cast<MTGosHAL::FG_color>(fg) ;
return *this;
}
ScreenOut::~ScreenOut() {}
auto syscall(uint32_t syscall_num, void* handle, void* args) -> void* {
uint16_t objnum=(uint16_t)(syscall_num>>16);
uint16_t funcnum=(uint16_t)syscall_num;
switch(objnum) {
case 0: {
ScreenOut *obj = (ScreenOut*)handle;
switch(funcnum) {
case 0:
return (void*)(new ScreenOut(((bool *)args)[0]));
break;
case 1:
*obj << ((char**)args)[0];
return handle;
break;
case 2:
obj->clrscr();
return handle;
break;
case 3: {
BGColor bg=static_cast<BGColor>(((uint32_t *)args)[0]);
FGColor fg=static_cast<FGColor>(((uint32_t *)args)[1]);
obj->setColor(bg,fg);
return handle;
break;
}
case 0xFFFF:
delete obj;
return nullptr;
break;
default:
break;
}
break;
}
default:
break;
}
return nullptr;
}

View file

@ -1,17 +1,19 @@
include ../kernel.settings
SRCS = $(shell find -name '*.c')
SRCS = $(shell find -name '*.cpp')
OBJS = $(addsuffix .o,$(basename $(SRCS)))
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc
CPP = $(PREFIX)g++
LD = $(PREFIX)ld
ASFLAGS = -m32
CFLAGS = -m32 -w -g -fno-stack-protector -nostdinc -I include -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
LDFLAGS = -melf_i386 -Ttest.ld
test.elf: $(OBJS)
test.elf: $(OBJS) $(arch)/syscall.o
$(LD) $(LDFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $^
%.o: %.cpp
$(CPP) $(CPPFLAGS) -c -o $@ $^
$(arch)/syscall.o: $(arch)/syscall.S
$(AS) $(ASFLAGS) -c -o $@ $^
clean:
rm $(OBJS)
rm $(OBJS) $(arch)/syscall.o
.PHONY: clean

47
user/include/mtgos.hpp Normal file
View file

@ -0,0 +1,47 @@
#include <stdint.h>
enum class BGColor : uint32_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 FGColor : uint32_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 ScreenOut {
private:
void* handle;
public:
ScreenOut(bool err);
auto operator <<(char*) -> ScreenOut &; // puts(char *)
auto clrscr() -> ScreenOut &;
auto setColor(BGColor, FGColor) -> ScreenOut &;
~ScreenOut();
};

9
user/include/stdint.h Normal file
View file

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

31
user/mtgos.cpp Normal file
View file

@ -0,0 +1,31 @@
#include <mtgos.hpp>
extern "C" {
void * screenout_init(int err);
void * screenout_out(void* handle, char *str);
void * screenout_clear(void* handle);
void * screenout_setcolor(void* handle, uint32_t BG, uint32_t FG);
void * screenout_destroy(void * handle);
}
ScreenOut::ScreenOut(bool err) {
handle=screenout_init(err);
}
auto ScreenOut::operator<<(char * str) -> ScreenOut & {
screenout_out(handle, str);
return *this;
}
auto ScreenOut::clrscr() -> ScreenOut & {
screenout_clear(handle);
return *this;
}
auto ScreenOut::setColor(BGColor bg, FGColor fg) -> ScreenOut & {
screenout_setcolor(handle, static_cast<uint32_t>(bg), static_cast<uint32_t>(fg));
return *this;
}
ScreenOut::~ScreenOut() {
handle=screenout_destroy(handle);
}
void main();
extern "C" void _start() {
main();
for(;;);
}

View file

@ -1,4 +1,4 @@
#include <mtgos.hpp>
static unsigned short* videomem = (unsigned short*) 0xb8000;
void temp() {
int i;
@ -6,10 +6,12 @@ void temp() {
*videomem++ = (0x07 << 8) | ('0' + i);
}
}
void _start(void)
void main()
{
char arr[4]="123";
ScreenOut out=ScreenOut(false);
int i;
for(i=0;i<100;i++)
temp();
while(1);
out.setColor(BGColor::BLUE, FGColor::YELLOW);
for(;;)
out << arr;
}

View file

@ -2,7 +2,7 @@ ENTRY(_start)
OUTPUT_FORMAT(elf32-i386)
SECTIONS
{
. = 0x0;
. = 0x131000;
.text : {
*(.text)
}

62
user/x86/syscall.S Normal file
View file

@ -0,0 +1,62 @@
.global screenout_init
// void * screenout_init(int err);
screenout_init:
mov 0x4(%esp), %eax
push %eax
xor %eax, %eax
int $0x30
pop %ecx
ret
.global screenout_out
// void * screenout_out(void* handle, char *str);
screenout_out:
push %ebx
mov 0xC(%esp), %eax
mov 0x8(%esp), %ebx
push %eax
xor %eax, %eax
inc %eax
int $0x30
pop %ebx
pop %ebx
ret
.global screenout_clear
// void * screenout_clear(void* handle);
screenout_clear:
push %ebx
mov 0x8(%esp), %ebx
xor %eax, %eax
inc %eax
inc %eax
int $0x30
pop %ebx
ret
.global screenout_setcolor
// void * screenout_setcolor(void* handle, uint32_t BG, uint32_t FG)
screenout_setcolor:
push %ebx
mov 0x8(%esp), %ebx
mov 0xC(%esp), %eax
mov 0x10(%esp), %ecx
push %ecx
push %eax
mov $3, %eax
int $0x30
pop %ebx
pop %ebx
pop %ebx
ret
.global screenout_destroy
// void * screenout_destroy(void * handle)
screenout_destroy:
push %ebx
mov 0x8(%esp), %ebx
xor %eax, %eax
dec %ax
int $0x30
pop %ebx
ret