diff --git a/kernel/hal/x86/init/idt.cpp b/kernel/hal/x86/init/idt.cpp index a66e527..059d541 100644 --- a/kernel/hal/x86/init/idt.cpp +++ b/kernel/hal/x86/init/idt.cpp @@ -3,6 +3,7 @@ #include #include #include +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 { diff --git a/kernel/hal/x86/init/init.cpp b/kernel/hal/x86/init/init.cpp index 5ff6b7c..ce68dcf 100644 --- a/kernel/hal/x86/init/init.cpp +++ b/kernel/hal/x86/init/init.cpp @@ -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; diff --git a/kernel/hal/x86/mm/pmm.cpp b/kernel/hal/x86/mm/pmm.cpp index 595ba58..4276832 100644 --- a/kernel/hal/x86/mm/pmm.cpp +++ b/kernel/hal/x86/mm/pmm.cpp @@ -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; diff --git a/kernel/kernel/include/syscall.hpp b/kernel/kernel/include/syscall.hpp new file mode 100644 index 0000000..815f933 --- /dev/null +++ b/kernel/kernel/include/syscall.hpp @@ -0,0 +1,81 @@ +#ifndef _SYSCALL_HPP +#define _SYSCALL_HPP +#include +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 diff --git a/kernel/kernel/syscall.cpp b/kernel/kernel/syscall.cpp new file mode 100644 index 0000000..b0c2225 --- /dev/null +++ b/kernel/kernel/syscall.cpp @@ -0,0 +1,71 @@ + +#include +#include +#include +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(bg) << static_cast(fg) ; + else + MTGosHAL::out << static_cast(bg) << static_cast(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(((uint32_t *)args)[0]); + FGColor fg=static_cast(((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; +} diff --git a/user/Makefile b/user/Makefile index 56470e0..60ceb65 100644 --- a/user/Makefile +++ b/user/Makefile @@ -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 diff --git a/user/include/mtgos.hpp b/user/include/mtgos.hpp new file mode 100644 index 0000000..06fe8ba --- /dev/null +++ b/user/include/mtgos.hpp @@ -0,0 +1,47 @@ +#include +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(); +}; diff --git a/user/include/stdint.h b/user/include/stdint.h new file mode 100644 index 0000000..2926bb5 --- /dev/null +++ b/user/include/stdint.h @@ -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; diff --git a/user/mtgos.cpp b/user/mtgos.cpp new file mode 100644 index 0000000..5d0decb --- /dev/null +++ b/user/mtgos.cpp @@ -0,0 +1,31 @@ +#include +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(bg), static_cast(fg)); + return *this; +} +ScreenOut::~ScreenOut() { + handle=screenout_destroy(handle); +} +void main(); +extern "C" void _start() { + main(); + for(;;); +} diff --git a/user/test.c b/user/test.cpp similarity index 53% rename from user/test.c rename to user/test.cpp index 79cf4c1..ec7e56a 100644 --- a/user/test.c +++ b/user/test.cpp @@ -1,4 +1,4 @@ - +#include 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; } diff --git a/user/test.ld b/user/test.ld index e757f9c..a40752a 100644 --- a/user/test.ld +++ b/user/test.ld @@ -2,7 +2,7 @@ ENTRY(_start) OUTPUT_FORMAT(elf32-i386) SECTIONS { - . = 0x0; + . = 0x131000; .text : { *(.text) } diff --git a/user/x86/syscall.S b/user/x86/syscall.S new file mode 100644 index 0000000..d3ef6cf --- /dev/null +++ b/user/x86/syscall.S @@ -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