From 509009d1340127c05e605eab7e0fad9128c9b59b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 26 Jul 2017 13:41:23 -0400 Subject: [PATCH] get HOS building again on Ubuntu 17.04 with GCC 6 --- kernel/Makefile | 11 +- kernel/boot.asm | 28 ++-- kernel/fs/ext2/Ext2OpenFile.cpp | 4 +- kernel/functions.h | 6 + kernel/hos_defines.h | 6 + kernel/idt.inc | 2 +- kernel/lang/lang.asm | 76 +++++------ kernel/lang/lang.h | 2 +- kernel/lang/vector.h | 234 ++++++++++++++------------------ kernel/link.ld | 7 +- 10 files changed, 178 insertions(+), 198 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index 6074b82..83045a2 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -8,15 +8,16 @@ NASM=nasm NASM_FLAGS=-f aout # C/C++ Information: -CPPFLAGS=-fleading-underscore -fno-builtin -nostdlib -nostartfiles -nodefaultlibs -I. -Wall +CPPFLAGS=-ffreestanding -fno-builtin -nostdlib -nostartfiles -nodefaultlibs -I. -Wall -m32 -fno-pic # -S -masm=intel CC=gcc CXX=g++ -CXXFLAGS=-fno-rtti -fno-exceptions -D_HOS_CPP_ +CXXFLAGS=-fno-rtti -fno-exceptions -D_HOS_CPP_ -std=gnu++98 # Linker Information: -LD=ld -LDFLAGS=-nodefaultlibs -nostdlib --no-demangle -T link.ld +LD=gcc +LDFLAGS=-ffreestanding -fno-builtin -nostartfiles -nodefaultlibs -nostdlib -Wl,--no-demangle -T link.ld -m32 +LIBS=-static-libgcc # Files OBJS=boot.o lang/lang_a.o \ @@ -43,7 +44,7 @@ CXXSRC=lang/string.cpp lang/new.cpp char/misc_char.cpp char/vconsole.cpp \ .PHONY: all depend clean html all: $(OBJS) - $(LD) $(LDFLAGS) -Map kernel.map $(OBJS) -o kernel.bin + $(LD) $(LDFLAGS) -Wl,-Map -Wl,kernel.map $(OBJS) -o kernel.bin $(LIBS) @echo ' Kernel built: ' `ls -sk kernel.bin | cut -d' ' -f1`kb depend: diff --git a/kernel/boot.asm b/kernel/boot.asm index b189ff1..62f4f6a 100644 --- a/kernel/boot.asm +++ b/kernel/boot.asm @@ -25,23 +25,23 @@ %define KERNEL_P PHYS_START+0x8000 ;1mb+32kb - the kernel's physical address %define KERNEL_V KERNEL_P+VIRT_OFFSET ;3gb+1mb+32kb, the virtual address of the kernel -extern _k_init, _isr, _k_mbsave, _end, _rm_params, _initrd, _tss0 +extern k_init, isr, k_mbsave, end, rm_params, initrd, tss0 [bits 32] -[global _start] -_start: +[global start] +start: multiboot_header: dd MULTIBOOT_MAGIC ;magic dd MULTIBOOT_FLAGS ;flags dd -(MULTIBOOT_MAGIC + MULTIBOOT_FLAGS) ;checksum - dd multiboot_header-VIRT_OFFSET ;header_addr - dd _start-VIRT_OFFSET ;load_addr + dd 0x100000000+multiboot_header-VIRT_OFFSET ;header_addr + dd 0x100000000+start-VIRT_OFFSET ;load_addr dd 0 ;load_end_addr - dd _end-VIRT_OFFSET ;bss_end_addr - dd multiboot_entry-VIRT_OFFSET ;entry_addr + dd 0x100000000+end-VIRT_OFFSET ;bss_end_addr + dd 0x100000000+multiboot_entry-VIRT_OFFSET ;entry_addr ; dd 1 ;mode_type ; dd 80 ;width @@ -53,7 +53,7 @@ multiboot_entry: ;This is where the kernel begins execution from the bootloader. ;At this point, a temporary gdt is set up to "map" 0xC000_0000 to 0x0. cli ;should already be off... - lgdt [gdtrbs32-VIRT_OFFSET] + lgdt [0x100000000+gdtrbs32-VIRT_OFFSET] jmp KERNEL_CODE_BS32:segmented_start segmented_start: mov cx, KERNEL_DATA_BS32 @@ -68,7 +68,7 @@ segmented_start: add ebx, VIRT_OFFSET push eax push ebx ;pointer to multiboot info structure - call _k_mbsave ;save multiboot info structures + call k_mbsave ;save multiboot info structures add esp, 8 cmp eax, 0 ; eax = pointer to mb_module_t struct for rmmod @@ -86,7 +86,7 @@ segmented_start: rep movsd ; copy rmmod to first 1mb mov ebx, pm_return ; return address - mov ecx, _rm_params ; put real mode params here (video mode etc...) + mov ecx, rm_params ; put real mode params here (video mode etc...) sub ecx, VIRT_OFFSET jmp 0xC0005010 ; jump to rmmod @@ -161,8 +161,8 @@ newgdtcontinue: lidt [idtr] ;load idt mov [PDBR_V], dword 0 ;unmap 0x0, we are running completely paged at 0xC000_0000 - mov edi, GDT_V + TSS0_SEG + 2 ; copy _tss0 base address - mov eax, _tss0 ; into the GDT descrtiptor + mov edi, GDT_V + TSS0_SEG + 2 ; copy tss0 base address + mov eax, tss0 ; into the GDT descrtiptor stosw ; for TSS0 shr eax, 16 stosb @@ -170,7 +170,7 @@ newgdtcontinue: shr eax, 8 stosb - call _k_init ;C kernel initialization + call k_init ;C kernel initialization mov ax, TSS0_SEG ltr ax @@ -187,7 +187,7 @@ idle_loop: ; system idle loop ;------------------------------------------------------- gdtrbs32: dw gdt_endbs32-gdtbs32-1 - dd gdtbs32-VIRT_OFFSET + dd 0x100000000+gdtbs32-VIRT_OFFSET gdtbs32: ; 0 = null descriptor dd 0 dd 0 diff --git a/kernel/fs/ext2/Ext2OpenFile.cpp b/kernel/fs/ext2/Ext2OpenFile.cpp index 3857671..fd34cf1 100644 --- a/kernel/fs/ext2/Ext2OpenFile.cpp +++ b/kernel/fs/ext2/Ext2OpenFile.cpp @@ -29,7 +29,7 @@ Ext2OpenFile::Ext2OpenFile(Ext2fs *fs, u32_t inum, int mode) myBlockCacheStatus = 0; /* 0: invalid; 1: valid; 2: dirty */ fs->readInode(inum, &myInode); myCache = new Ext2BlockCache(fs, inum, &myInode); - lock(&Ext2OpenFileListLock); + lockit(&Ext2OpenFileListLock); if ((mode & VFS_MODE_RW_MASK) == VFS_MODE_WRITE) { if ((mode & VFS_MODE_WRITE_MASK) == VFS_MODE_TRUNCATE) @@ -62,7 +62,7 @@ Ext2OpenFile::Ext2OpenFile(Ext2fs *fs, u32_t inum, int mode) Ext2OpenFile::~Ext2OpenFile() { /* remove ourselves from the open file list */ - lock(&Ext2OpenFileListLock); + lockit(&Ext2OpenFileListLock); u32_t size = Ext2OpenFileList->size(); for (u32_t i = 0; i < size; i++) { diff --git a/kernel/functions.h b/kernel/functions.h index 0f94a4c..654ff00 100644 --- a/kernel/functions.h +++ b/kernel/functions.h @@ -9,9 +9,15 @@ #include "hos_defines.h" #include "sys/io.h" +#ifdef __cplusplus +extern "C" { +#endif extern u32_t _code; extern u32_t _bss; extern u32_t _end; +#ifdef __cplusplus +} +#endif //Enables (SeTs) Interrupt Flag on the processor static inline void enable_ints() diff --git a/kernel/hos_defines.h b/kernel/hos_defines.h index 8db9dd0..05c05b4 100644 --- a/kernel/hos_defines.h +++ b/kernel/hos_defines.h @@ -44,9 +44,15 @@ typedef unsigned char u8_t; typedef unsigned char byte; typedef int lock_t; +#ifdef __cplusplus +extern "C" { +#endif extern u32_t _end; extern u32_t _bss; extern u32_t start; +#ifdef __cplusplus +} +#endif #endif diff --git a/kernel/idt.inc b/kernel/idt.inc index 745799f..7310934 100644 --- a/kernel/idt.inc +++ b/kernel/idt.inc @@ -97,7 +97,7 @@ isr_main_nodup: push esp push eax ;interrupt number - call _isr + call isr add esp, 8 pop gs diff --git a/kernel/lang/lang.asm b/kernel/lang/lang.asm index 8972963..7306465 100644 --- a/kernel/lang/lang.asm +++ b/kernel/lang/lang.asm @@ -13,8 +13,8 @@ %endmacro ;implements a lock -[global _lock] -_lock: +[global lockit] +lockit: push ebp mov ebp, esp push eax @@ -35,8 +35,8 @@ _lock_loop0: ret ;releases a lock -[global _unlock] -_unlock: +[global unlock] +unlock: push ebp mov ebp, esp push esi @@ -50,15 +50,15 @@ _unlock: ;returns the value in the CR0 register ;extern dword read_cr0(); -[global _read_cr0] -_read_cr0: +[global read_cr0] +read_cr0: mov eax, cr0; ret ;stores the parameter to the CR0 register ;extern dword write_cr0(dword cr0); -[global _write_cr0] -_write_cr0: +[global write_cr0] +write_cr0: push ebp mov ebp, esp mov eax, [ebp+8] @@ -68,8 +68,8 @@ _write_cr0: ;returns the value in the CR2 register ;extern dword read_cr2(); -[global _read_cr2] -_read_cr2: +[global read_cr2] +read_cr2: mov eax, cr2; ret @@ -77,16 +77,16 @@ _read_cr2: ;returns the value in the CR3 register ;extern dword read_cr3(); -[global _read_cr3] -_read_cr3: +[global read_cr3] +read_cr3: mov eax, cr3; ret ;stores the parameter to the CR3 register ;extern dword write_cr3(dword cr3); -[global _write_cr3] -_write_cr3: +[global write_cr3] +write_cr3: push ebp mov ebp, esp mov eax, [ebp+8] @@ -96,16 +96,16 @@ _write_cr3: ; read ss register -[global _read_ss] -_read_ss: +[global read_ss] +read_ss: mov ax, ss ret ;copies a string from the source to the destination parameter ;extern void strcpy(char *dest, char *src); -[global _strcpy] -_strcpy: +[global strcpy] +strcpy: push ebp mov ebp, esp push esi @@ -124,8 +124,8 @@ strcpyloop: ;copies memory of n bytes from src to destination ;void memcpy(void *dest, void *src, dword n); -[global _memcpy] -_memcpy: +[global memcpy] +memcpy: push ebp mov ebp, esp push esi @@ -147,8 +147,8 @@ _memcpy: ;copies memory of n words (n*2 bytes) from src to destination ;void memcpyw(void *dest, void *src, dword n); -[global _memcpyw] -_memcpyw: +[global memcpyw] +memcpyw: push ebp mov ebp, esp push esi @@ -170,8 +170,8 @@ _memcpyw: ;copies memory of n dwords (n*4 bytes) from src to destination ;void memcpyd(void *dest, void *src, dword n); -[global _memcpyd] -_memcpyd: +[global memcpyd] +memcpyd: push ebp mov ebp, esp push esi @@ -193,8 +193,8 @@ _memcpyd: ;sets num bytes at buffer to the value of c ;void *memset(void *buffer, int c, int num); -[global _memset] -_memset: +[global memset] +memset: push ebp mov ebp, esp push edi @@ -215,8 +215,8 @@ _memset: ;sets num words at buffer to the value of c ;void *memsetw(void *buffer, int c, int num); -[global _memsetw] -_memsetw: +[global memsetw] +memsetw: push ebp mov ebp, esp push edi @@ -237,8 +237,8 @@ _memsetw: ;sets num dwords at buffer to the value of c ;void *memsetd(void *buffer, int c, int num); -[global _memsetd] -_memsetd: +[global memsetd] +memsetd: push ebp mov ebp, esp push edi @@ -259,8 +259,8 @@ _memsetd: ;returns the number of characters in a string ;extern dword strlen(char *str); -[global _strlen] -_strlen: +[global strlen] +strlen: push ebp mov ebp, esp push esi @@ -284,8 +284,8 @@ strlendone: ;this function invalidates the page directory/table entry that ; would be used to access the memory address given in the parameter ;extern void invlpg_(dword addr); -[global _invlpg_] -_invlpg_: +[global invlpg_] +invlpg_: mov eax, [esp+4] invlpg [eax] ret @@ -294,8 +294,8 @@ _invlpg_: ; ;void writeCursorPosition(word pos) ; -[global _writeCursorPosition] -_writeCursorPosition: +[global writeCursorPosition] +writeCursorPosition: push ebp mov ebp, esp @@ -333,8 +333,8 @@ _writeCursorPosition: ; ;word getCursorPosition() ; -[global _getCursorPosition] -_getCursorPosition: +[global getCursorPosition] +getCursorPosition: push ebx push edx diff --git a/kernel/lang/lang.h b/kernel/lang/lang.h index b2d3c05..2607776 100644 --- a/kernel/lang/lang.h +++ b/kernel/lang/lang.h @@ -9,7 +9,7 @@ #include "hos_defines.h" /* lang.asm */ -void lock(lock_t *addr); +void lockit(lock_t *addr); void unlock(lock_t *addr); u32_t read_cr0(); u32_t write_cr0(u32_t cr0); diff --git a/kernel/lang/vector.h b/kernel/lang/vector.h index 2e44872..ded371e 100644 --- a/kernel/lang/vector.h +++ b/kernel/lang/vector.h @@ -15,6 +15,8 @@ template class vector { protected: + typedef type * type_ptr_t; + /* Pointers to vector elements */ type **myData; @@ -25,157 +27,121 @@ protected: unsigned int myAllocated; /* Causes the vector to double in its allocated size */ - void grow(); + void grow() + { + myAllocated <<= 1; + if (myAllocated == 0) + myAllocated = 1; + type **data_new = new type_ptr_t[myAllocated]; + for (unsigned int i = 0; i < mySize; i++) + data_new[i] = myData[i]; + if (myData) + delete[] myData; + myData = data_new; + } public: /* Constructors/Destructor */ - vector(); - vector(unsigned int size); - vector(const vector & v1); - ~vector(); + vector() + { + myData = NULL; + mySize = 0; + myAllocated = 0; + } + + vector(unsigned int size) + { + myData = new type_ptr_t[size]; + mySize = 0; + myAllocated = size; + } + + vector(const vector & v1) + { + mySize = v1.mySize; + myAllocated = v1.myAllocated; + myData = new type_ptr_t[myAllocated]; + for (u32_t i = 0; i < mySize; i++) + myData[i] = new type(*v1.myData[i]); + } + + ~vector() + { + for (unsigned int i = 0; i < mySize; i++) + delete myData[i]; + delete[] myData; + } /* Assignment operator */ - vector & operator=(const vector & v1); + vector & operator=(const vector & v1) + { + mySize = v1.mySize; + myAllocated = v1.myAllocated; + myData = new type_ptr_t[myAllocated]; + for (u32_t i = 0; i < mySize; i++) + myData[i] = new type(*v1.myData[i]); + return *this; + } /* Returns the size of the vector */ - unsigned int size() const; + unsigned int size() const + { + return mySize; + } /* Add an element to the end of the vector */ - vector & add(type elem); + vector & add(type elem) + { + while (mySize >= myAllocated) + grow(); + myData[mySize++] = new type(elem); + return *this; + } /* Remove an element from the vector */ - vector & remove(unsigned int index); + vector & remove(unsigned int index) + { + if (index < mySize) + { + delete myData[index]; + for (unsigned int i = index; i < (mySize - 1); i++) + myData[i] = myData[i+1]; + mySize--; + } + return *this; + } /* Insert an element into a position in the vector */ - vector & insert(type elem, unsigned int position); + vector & insert(type elem, unsigned int position) + { + if (position <= mySize) + { + if (mySize >= myAllocated) + grow(); + for (unsigned int i = mySize; i > position; i--) + myData[i] = myData[i-1]; + myData[position] = new type(elem); + mySize++; + } + return *this; + } /* Direct access operators */ - const type & operator[](unsigned int index) const; - type & operator[](unsigned int index); + const type & operator[](unsigned int index) const + { + return *myData[index]; + } + + type & operator[](unsigned int index) + { + return *myData[index]; + } /* Returns pointer to data */ - type **data(); + type **data() + { + return myData; + } }; -template -vector::vector() -{ - myData = NULL; - mySize = 0; - myAllocated = 0; -} - -template -vector::vector(unsigned int size) -{ - myData = new (type *)[size]; - mySize = 0; - myAllocated = size; -} - -template -vector::vector(const vector & v1) -{ - mySize = v1.mySize; - myAllocated = v1.myAllocated; - myData = new (type *)[myAllocated]; - for (u32_t i = 0; i < mySize; i++) - myData[i] = new type(*v1.myData[i]); -} - -template -vector::~vector() -{ - for (unsigned int i = 0; i < mySize; i++) - delete myData[i]; - delete[] myData; -} - -template -vector & vector::operator=(const vector & v1) -{ - mySize = v1.mySize; - myAllocated = v1.myAllocated; - myData = new (type *)[myAllocated]; - for (u32_t i = 0; i < mySize; i++) - myData[i] = new type(*v1.myData[i]); - return *this; -} - -template -u32_t vector::size() const -{ - return mySize; -} - -template -const type & vector::operator[](unsigned int index) const -{ - return *myData[index]; -} - -template -type & vector::operator[](unsigned int index) -{ - return *myData[index]; -} - -template -vector & vector::add(type elem) -{ - while (mySize >= myAllocated) - grow(); - myData[mySize++] = new type(elem); - return *this; -} - -template -void vector::grow() -{ - myAllocated <<= 1; - if (myAllocated == 0) - myAllocated = 1; - type **data_new = new (type *)[myAllocated]; - for (unsigned int i = 0; i < mySize; i++) - data_new[i] = myData[i]; - if (myData) - delete[] myData; - myData = data_new; -} - -template -vector & vector::remove(unsigned int index) -{ - if (index < mySize) - { - delete myData[index]; - for (unsigned int i = index; i < (mySize - 1); i++) - myData[i] = myData[i+1]; - mySize--; - } - return *this; -} - -template -vector & vector::insert(type elem, unsigned int position) -{ - if (position <= mySize) - { - if (mySize >= myAllocated) - grow(); - for (unsigned int i = mySize; i > position; i--) - myData[i] = myData[i-1]; - myData[position] = new type(elem); - mySize++; - } - return *this; -} - - -template -type **vector::data() -{ - return myData; -} - #endif diff --git a/kernel/link.ld b/kernel/link.ld index 03e3617..f881b0f 100644 --- a/kernel/link.ld +++ b/kernel/link.ld @@ -1,10 +1,11 @@ OUTPUT_FORMAT("binary") -ENTRY(_start) +ENTRY(start) SECTIONS { .text 0xC0108000 : { code = .; _code = .; __code = .; *(.text) + *(.eh_frame*) } .gnulinkonce : { *(.gnu.linkonce*) @@ -12,8 +13,8 @@ SECTIONS } .data : { data = .; _data = .; __data = .; - *(.data) - *(.rodata) + *(.data*) + *(.rodata*) . = ALIGN(4096); } .bss : {