diff --git a/gensym.sh b/gensym.sh new file mode 100755 index 0000000..b7180a4 --- /dev/null +++ b/gensym.sh @@ -0,0 +1,2 @@ + +nm $1 | grep " T " | awk '{ print $1" "$3 }' > $1.sym diff --git a/kernel.settings b/kernel.settings index ec7cbfe..6dcff75 100644 --- a/kernel.settings +++ b/kernel.settings @@ -1,6 +1,6 @@ arch = x86_64 -#MODE = debug -MODE = release # enables optimization +MODE = debug +#MODE = release # enables optimization export PATH := $(HOME)/opt/bin:$(PATH) ARCHFLAGS = @@ -14,9 +14,9 @@ else endif ifeq ($(MODE),debug) - CFLAGS += -g3 -DDEBUG - CPPFLAGS += -g3 -DDEBUG + CFLAGS := -g3 -DDEBUG + CPPFLAGS := -g3 -DDEBUG else - CFLAGS += -O2 - CPPFLAGS += -O2 + CFLAGS := -O2 + CPPFLAGS := -O2 endif diff --git a/kernel/hal/x86/Makefile b/kernel/hal/x86/Makefile index e4c7cf2..3de515e 100644 --- a/kernel/hal/x86/Makefile +++ b/kernel/hal/x86/Makefile @@ -5,8 +5,8 @@ OBJS = $(addsuffix .o,$(basename $(SRCS))) CPP = $(PREFIX)g++ CC = $(PREFIX)gcc ASFLAGS = -m32 -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/ -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 +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/ -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 diff --git a/kernel/hal/x86_64/Makefile b/kernel/hal/x86_64/Makefile index 4462091..a492974 100644 --- a/kernel/hal/x86_64/Makefile +++ b/kernel/hal/x86_64/Makefile @@ -5,8 +5,8 @@ OBJS = $(addsuffix .o,$(basename $(SRCS))) CPP = $(PREFIX)g++ CC = $(PREFIX)gcc ASFLAGS = -m64 -CFLAGS = -m64 -Wall -fno-stack-protector -nostdinc -Ic_include/ -I../../kernel/c_include -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -CPPFLAGS = -m64 -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 -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 +CFLAGS += -m64 -Wall -fno-stack-protector -nostdinc -Ic_include/ -I../../kernel/c_include -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie -ffreestanding -mcmodel=large -mno-mmx -mno-sse -mno-sse2 +CPPFLAGS += -m64 -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 -ffreestanding -mcmodel=large -mno-mmx -mno-sse -mno-sse2 all: $(OBJS) diff --git a/kernel/hal/x86_64/asm/snippets.S b/kernel/hal/x86_64/asm/snippets.S index 5006c20..a64adbf 100644 --- a/kernel/hal/x86_64/asm/snippets.S +++ b/kernel/hal/x86_64/asm/snippets.S @@ -269,7 +269,7 @@ intr_stub 212 intr_stub 213 intr_stub 214 intr_stub 215 -intr_stub 4216 +intr_stub 216 intr_stub 217 intr_stub 218 intr_stub 219 @@ -341,7 +341,8 @@ intr_common_handler: mov $0x10, %ax mov %ax, %ds mov %ax, %es - mov %rsp, %rax + mov %rsp, %rdi + add $0x80, %rsp //Respect the useless red zone. call handleINT mov %rax, %rsp mov $8, %rcx diff --git a/kernel/hal/x86_64/boot/boot.S b/kernel/hal/x86_64/boot/boot.S index 3c3638a..d30c65d 100644 --- a/kernel/hal/x86_64/boot/boot.S +++ b/kernel/hal/x86_64/boot/boot.S @@ -14,6 +14,7 @@ .extern init .global _start _start: + cli finit mov $mb_ptr, %edi stosl @@ -48,6 +49,18 @@ x86_64_OK: stosl lodsl stosl + lodsl + stosl + lodsl + stosl + lodsl + stosl + lodsl + stosl + lodsl + stosl + lodsl + stosl //Activate PAE mov %cr4, %eax or $0x20, %al @@ -119,6 +132,9 @@ pmfill: .int 0 pdptfill: .quad 0x87 +.quad 0x40000087 +.quad 0x80000087 +.quad 0xC0000087 .section .bss mb_ptr: // These 8 Bytes will never be used when the stack comes near it diff --git a/kernel/hal/x86_64/init/idt.cpp b/kernel/hal/x86_64/init/idt.cpp index 8c1e6dd..2e3bf16 100644 --- a/kernel/hal/x86_64/init/idt.cpp +++ b/kernel/hal/x86_64/init/idt.cpp @@ -4,9 +4,11 @@ #include #include #include +bool epicfail=false; auto syscall(uint32_t syscall_num, void* handle, void* args) -> void*; extern void** progs; namespace MTGosHAL { + auto startup() -> void; IDT::IDT() { //Init PIC outb(0x20, 0x11); //Init Master-PIC @@ -37,12 +39,19 @@ namespace MTGosHAL { struct cpu_state* new_cpu=cpu; debug << "Interrupt 0x" << Base::HEXADECIMAL << (int) cpu->intr << " was raised.\n"; if(cpu->intr<=0x1F) { + if(epicfail) { + err << "While trying to output the stack another exception happened\n"; + while(1) { + asm volatile("cli; hlt"); + } + } + epicfail=true; err << "Exception 0x" << Base::HEXADECIMAL << (int) cpu->intr << "! Kernel halted!\n"; - err << "EAX = 0x" << (int)cpu->rax << " - EBX = 0x" << (int)cpu->rbx << "\n"; - err << "ECX = 0x" << (int)cpu->rcx << " - EDX = 0x" << (int)cpu->rdx << "\n"; - err << "ESI = 0x" << (int)cpu->rsi << " - EDI = 0x" << (int)cpu->rdi << "\n"; - err << "SS:ESP = 0x" << (int)cpu->ss << ":0x" << (int)cpu->rsp << " - SS:EBP = 0x" << (int)cpu->ss << ":0x" << (int)cpu->rbp << "\n"; - err << "CS:EIP = 0x" << (int)cpu->cs << ":0x" << (int)cpu->rip << " - INTR:ERR = 0x" << (int)cpu->intr << ":0x" << (int)cpu->error << "\n"; + err << "RAX = 0x" << (int)cpu->rax << " - RBX = 0x" << (int)cpu->rbx << "\n"; + err << "RCX = 0x" << (int)cpu->rcx << " - RDX = 0x" << (int)cpu->rdx << "\n"; + err << "RSI = 0x" << (int)cpu->rsi << " - RDI = 0x" << (int)cpu->rdi << "\n"; + err << "SS:RSP = 0x" << (int)cpu->ss << ":0x" << (int)cpu->rsp << " - SS:RBP = 0x" << (int)cpu->ss << ":0x" << (int)cpu->rbp << "\n"; + err << "CS:RIP = 0x" << (int)cpu->cs << ":0x" << (int)cpu->rip << " - INTR:ERR = 0x" << (int)cpu->intr << ":0x" << (int)cpu->error << "\n"; err << "------ END OF REGISTER DUMP ------ ------ START OF PROGRAM LOADPOINTS ------\n"; for(int i=0;i<1024;i++) { if(!progs[i]) @@ -54,7 +63,26 @@ namespace MTGosHAL { outb(0x43, 0xB6); outb(0x42, (uint8_t)counter); outb(0x42, (uint8_t)(counter>>8)); - outb(0x61, inb(0x61) | 3); + //outb(0x61, inb(0x61) | 3); + err << "A detailled traceback is being output over the serial connector.\n"; + debug << "Outputting 32 stacks...\n"; + uint64_t *rsp=(uint64_t*)cpu->rsp; + uint64_t *rbp=(uint64_t*)cpu->rbp; + uint64_t *rip=(uint64_t*)cpu->rip; + for(int i=0;i<32;i++) { + if(!(rsp||rbp||rip)) + break; + if((int64_t)(rbp-rsp)<1) + break; + debug << Base::HEXADECIMAL << "Execution at 0x" << (int64_t) rip << ":\n"; + debug << "Number of local variables: 0x" << (int64_t)(rbp-rsp)-1 << "\n"; + for(uintptr_t i=0;i<(uintptr_t)(rbp-rsp)-1;i++) { + debug << "0x"<< (int64_t)(i*8) << ": 0x" << (int64_t)rsp[i] << "\n"; + } + rip=(uint64_t*)rbp[1]; + rsp=rbp; + rbp=(uint64_t*)rbp[0]; + } while(1) { asm volatile("cli; hlt"); } @@ -72,8 +100,15 @@ namespace MTGosHAL { if(ivt[cpu->intr][i]) new_cpu=ivt[cpu->intr][i](new_cpu); } - if(cpu->intr>=48) + if(cpu->intr==48) new_cpu->rax=(uint64_t)(::syscall(cpu->rax, (void*)(cpu->rbx), (void*)(cpu->rsp))); + if(cpu->intr==49) { + startup(); + for(int i=0;i<16;i++) { + if(ivt[0x20][i]) + new_cpu=ivt[0x20][i](new_cpu); + } + } return new_cpu; } auto IDT::request(uint8_t intr, struct cpu_state* (*handler)(struct cpu_state*)) -> bool { diff --git a/kernel/hal/x86_64/init/init.cpp b/kernel/hal/x86_64/init/init.cpp index 52e4bb3..7d06b38 100644 --- a/kernel/hal/x86_64/init/init.cpp +++ b/kernel/hal/x86_64/init/init.cpp @@ -21,7 +21,9 @@ namespace MTGosHAL { Screen err; Keyboard in; Multitasking tasks; - void main(long eax, struct multiboot_info* ebx, uint64_t**** pt) { + struct multiboot_info* ebx; + void main(long eax, struct multiboot_info* mb, uint64_t**** pt) { + ebx=mb; new (&debug) Serial(); debug << "Hello debugger! This is MTGos v00r01\nThese logs are probably very long, so please redirect the output to a file.\n"; @@ -34,11 +36,11 @@ namespace MTGosHAL { debug << "Init GDT\n"; new (&gdt) GDT(); gdt.setEntry(0, 0, 0, 0); - gdt.setEntry(1, 0, 0, 0x298); - gdt.setEntry(2, 0, 0, 0x292); - gdt.setEntry(3, 0, 0, 0x2F8); - gdt.setEntry(4, 0, 0, 0x2F2); - gdt.setEntry(5, (uint64_t)tasks.tss, sizeof(tasks.tss), GDT_FLAG_RING3 | GDT_FLAG_TSS | GDT_FLAG_PRESENT); + gdt.setEntry(1, 0, 0, GDT_FLAG_PRESENT | GDT_FLAG_64_BIT | GDT_FLAG_4K_GRAN | GDT_FLAG_RING0 | GDT_FLAG_SEGMENT | GDT_FLAG_CODESEG); + gdt.setEntry(2, 0, 0, GDT_FLAG_PRESENT | GDT_FLAG_32_BIT | GDT_FLAG_4K_GRAN | GDT_FLAG_RING0 | GDT_FLAG_SEGMENT | GDT_FLAG_DATASEG); + gdt.setEntry(3, 0, 0, GDT_FLAG_PRESENT | GDT_FLAG_64_BIT | GDT_FLAG_4K_GRAN | GDT_FLAG_RING3 | GDT_FLAG_SEGMENT | GDT_FLAG_CODESEG); + gdt.setEntry(4, 0, 0, GDT_FLAG_PRESENT | GDT_FLAG_32_BIT | GDT_FLAG_4K_GRAN | GDT_FLAG_RING3 | GDT_FLAG_SEGMENT | GDT_FLAG_DATASEG); + gdt.setEntry(5, (uint64_t)tasks.tss, sizeof(tasks.tss), GDT_FLAG_RING0 | GDT_FLAG_TSS | GDT_FLAG_PRESENT); gdt.setEntry(6, 0, 0, GDT_FLAG_RING3 | GDT_FLAG_TSS | GDT_FLAG_PRESENT); gdt.apply(); @@ -50,7 +52,10 @@ namespace MTGosHAL { idt.setEntry(48, (void *)((uint64_t)&intr_stub_0+768*2), SEG_KERNEL, IDT_TRAP_GATE | IDT_SEG_32_BIT | IDT_RING_3 | IDT_USED); idt.setEntry(8, (void *)((uint64_t)&intr_stub_0+128*2), SEG_DBL_FAULT, IDT_TASK_GATE | IDT_SEG_32_BIT | IDT_RING_0 | IDT_USED); idt.apply(); - asm volatile("ltr %%ax" : : "a"(5<<3)); + asm volatile("int $49"); // Finish loading GDT + } + auto startup() -> void { + //asm volatile("ltr %%ax" : : "a"(5<<3)); debug << "Init MM\n"; new (&mm) PMM(ebx); diff --git a/kernel/hal/x86_64/io/serial.cpp b/kernel/hal/x86_64/io/serial.cpp index 4425d06..4d9505a 100644 --- a/kernel/hal/x86_64/io/serial.cpp +++ b/kernel/hal/x86_64/io/serial.cpp @@ -5,7 +5,7 @@ namespace MTGosHAL { return inb(port+SERIAL_LSR)&0x20; } auto Serial::putChar(char chr) -> void { -// if(!works) + if(!works) return; int tries=65535; while(!isTransmitEmpty()) { diff --git a/kernel/hal/x86_64/mm/pmm2.cpp b/kernel/hal/x86_64/mm/pmm2.cpp index 1471413..f6d13c4 100644 --- a/kernel/hal/x86_64/mm/pmm2.cpp +++ b/kernel/hal/x86_64/mm/pmm2.cpp @@ -17,16 +17,16 @@ PMM2::PMM2(): pmm3() { } auto PMM2::markUsed(const void * addr, uint32_t length) -> bool { uintptr_t add=(uintptr_t)addr; - uint32_t pagetid = SPLIT1_UNSHIFT(add); + uint64_t pagetid = SPLIT1_UNSHIFT(add); //Check if used - for(uintptr_t curr_addr=add+length;curr_addr>=add;curr_addr-=0x200000) { + for(uintptr_t curr_addr=add+length;curr_addr>add;curr_addr-=0x200000) { if(pageTable[SPLIT1_UNSHIFT(curr_addr)]) return false; } //Mark as used uint64_t counter=1; - for(uintptr_t curr_addr=add+length;curr_addr>=add;curr_addr-=0x200000) { + for(uintptr_t curr_addr=add+length;curr_addr>add;curr_addr-=0x200000) { pageTable[SPLIT1_UNSHIFT(curr_addr)]=counter++; pmm3.markUsed((void*)curr_addr); } diff --git a/kernel/kernel/Makefile b/kernel/kernel/Makefile index 7905def..d95fd16 100644 --- a/kernel/kernel/Makefile +++ b/kernel/kernel/Makefile @@ -4,8 +4,8 @@ OBJS = $(addsuffix .o,$(basename $(SRCS))) CPP = $(PREFIX)g++ CC = $(PREFIX)gcc ASFLAGS = $(ARCHFLAGS) -CFLAGS = $(ARCHFLAGS) -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie -CPPFLAGS = $(ARCHFLAGS) -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 +CFLAGS += $(ARCHFLAGS) -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie +CPPFLAGS += $(ARCHFLAGS) -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) %.o: %.cpp diff --git a/kernel/kernel/include/output.hpp b/kernel/kernel/include/output.hpp index 143b182..0edd86e 100644 --- a/kernel/kernel/include/output.hpp +++ b/kernel/kernel/include/output.hpp @@ -39,6 +39,8 @@ namespace MTGosHAL { template <> auto Output::operator<<(int output) -> Output &; template <> + auto Output::operator<<(long int output) -> Output &; + template <> auto Output::operator<<(char output) -> Output &; template <> auto Output::operator<<(char* output) -> Output &; diff --git a/kernel/kernel/io/output.cpp b/kernel/kernel/io/output.cpp index dda21a7..e909c75 100644 --- a/kernel/kernel/io/output.cpp +++ b/kernel/kernel/io/output.cpp @@ -14,7 +14,8 @@ namespace MTGosHAL { return *this; } template <> - auto Output::operator<<(int output) -> Output & { + auto Output::operator<<(int op) -> Output & { + uintptr_t output=op; const char* chars="0123456789ABCDEF"; char buf[33]; buf[32]='\0'; @@ -27,6 +28,20 @@ namespace MTGosHAL { return *this; } template <> + auto Output::operator<<(long int op) -> Output & { + uint64_t output=op; + const char* chars="0123456789ABCDEF"; + char buf[65]; + buf[64]='\0'; + char* ptr=buf+63; + do { + *(ptr--)=chars[output%base]; + output/=base; + } while(output && (ptr!=buf)); + puts(ptr+1); + return *this; + } + template <> auto Output::operator<<(char output) -> Output & { putChar(output); return *this; diff --git a/kernel/kernel/mm/pmm.cpp b/kernel/kernel/mm/pmm.cpp index 1eeabbe..15cfa1a 100644 --- a/kernel/kernel/mm/pmm.cpp +++ b/kernel/kernel/mm/pmm.cpp @@ -1,6 +1,17 @@ #include #include #include +#ifdef __LP64 +#define PAGESIZE 0x200000 +#define UNSHIFT(a) ((a)>>20) +#define SHIFT(a) ((a)<<20) +#define FLAGS 0x7ffff +#else +#define PAGESIZE 4096 +#define UNSHIFT(a) ((a)>>12) +#define SHIFT(a) ((a)<<12) +#define FLAGS 0xfff +#endif void *operator new(size_t size) { return MTGosHAL::mm.alloc(size); } @@ -24,12 +35,12 @@ namespace MTGosHAL { auto PMM::alloc(size_t length) -> void * { if(!head) { //Alloc space for head - if(length+sizeof(malloc_t)<=4096) { //Small optimization. The routine for allocating more than one continuous page is terribly slow. + if(length+sizeof(malloc_t)<=PAGESIZE) { //Small optimization. The routine for allocating more than one continuous page is terribly slow. void *tmp; *this >> tmp; head=(malloc_t*)tmp; } else - head=(malloc_t*)(*this)(((length+sizeof(malloc_t))>>12)+1); + head=(malloc_t*)(*this)(UNSHIFT((length+sizeof(malloc_t)))+1); if(!head) //The alloc() didn't work! We're out of RAM! return nullptr; head->len=length; @@ -42,7 +53,7 @@ namespace MTGosHAL { malloc_t* last=nullptr; do { uintptr_t loc=(uintptr_t)curr+sizeof(malloc_t)+curr->len; - if((loc+length+sizeof(malloc_t))<((loc&(~0xFFF))+4096) && + if((loc+length+sizeof(malloc_t))<((loc&(~FLAGS))+PAGESIZE) && ((!curr->next) || (loc+length+sizeof(malloc_t))<((uintptr_t)(curr->next)))) { malloc_t *allocd=(malloc_t *)loc; allocd->len=length; @@ -59,12 +70,12 @@ namespace MTGosHAL { curr=curr->next; } while(curr); malloc_t *allocd=nullptr; - if(length+sizeof(malloc_t)<=4096) { //Small optimization. The routine for allocating more than one continuous page is terribly slow. + if(length+sizeof(malloc_t)<=PAGESIZE) { //Small optimization. The routine for allocating more than one continuous page is terribly slow. void *tmp; *this >> tmp; allocd=(malloc_t*)tmp; } else - allocd=(malloc_t*)(*this)(((length+sizeof(malloc_t))>>12)+1); + allocd=(malloc_t*)(*this)(UNSHIFT(length+sizeof(malloc_t))+1); if(!allocd) //The alloc() didn't work! We're out of RAM! return nullptr; last->next=allocd; @@ -83,8 +94,8 @@ namespace MTGosHAL { chk--; do { if(curr==chk) { - uintptr_t start=((uintptr_t)chk)&(~0xFFF); - uintptr_t end=start+0x1000; + uintptr_t start=((uintptr_t)chk)&(~FLAGS); + uintptr_t end=start+PAGESIZE; if((((uintptr_t)(curr->last)last)>=end))&&(((uintptr_t)(curr->next)>=end)||((uintptr_t)(curr->next)