From 58034f56ab8478b9524c3f09cdf9f93fcb439691 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 24 Aug 2014 19:39:37 +0800 Subject: [PATCH] x86_64 build working with new compiler --- BuildConf/x86_64/Makefile.cfg | 7 +-- Externals/config.mk | 2 + .../patches/binutils/ld/configure.tgt.patch | 2 +- .../binutils/ld/emulparams/acess2_amd64.sh | 7 ++- .../patches/gcc/gcc/config.gcc.patch | 7 ++- KernelLand/Kernel/arch/x86_64/include/arch.h | 2 + .../Kernel/arch/x86_64/include/mm_virt.h | 1 + KernelLand/Kernel/arch/x86_64/lib.c | 12 ++++ KernelLand/Kernel/arch/x86_64/mm_virt.c | 60 +++++++++++++++---- Usermode/Applications/init_src/main.c | 3 +- Usermode/Libraries/crt0.o_src/Makefile | 6 +- Usermode/Libraries/crt0.o_src/x86_64-crti.S | 15 +++++ Usermode/Libraries/crt0.o_src/x86_64-crtn.S | 9 +++ Usermode/Libraries/ld-acess.so_src/elf.c | 6 +- .../libc.so_src/include_exp/inttypes.h | 16 +++-- Usermode/common_settings.mk | 2 + 16 files changed, 121 insertions(+), 36 deletions(-) create mode 100644 Usermode/Libraries/crt0.o_src/x86_64-crti.S create mode 100644 Usermode/Libraries/crt0.o_src/x86_64-crtn.S diff --git a/BuildConf/x86_64/Makefile.cfg b/BuildConf/x86_64/Makefile.cfg index cc252bc5..ee10d852 100644 --- a/BuildConf/x86_64/Makefile.cfg +++ b/BuildConf/x86_64/Makefile.cfg @@ -1,10 +1,5 @@ -#PREFIX := x86_64-pc-elf -PREFIX := x86_64-none-elf - -CC := $(PREFIX)-gcc -LD := $(PREFIX)-ld -DISASM = $(PREFIX)-objdump -d -M x86-64 -S +TRIPLET = x86_64-pc-acess2 KERNEL_CFLAGS := -mcmodel=kernel -nostdlib -mno-red-zone -Wall -mno-sse DYNMOD_CFLAGS := -mcmodel=small -fPIC -mno-red-zone -mno-sse diff --git a/Externals/config.mk b/Externals/config.mk index f11a1d1b..9db8f0de 100644 --- a/Externals/config.mk +++ b/Externals/config.mk @@ -7,6 +7,8 @@ ifeq ($(ARCH),x86) BFD := i686 else ifeq ($(ARCH),x86_64) BFD := x86_64 +else ifeq ($(ARCH),armv7) + BFD := arm else $(error No BFD translation for $(ARCH) in Externals/config.mk) endif diff --git a/Externals/cross-compiler/patches/binutils/ld/configure.tgt.patch b/Externals/cross-compiler/patches/binutils/ld/configure.tgt.patch index 8d6a302a..cc3dc88b 100644 --- a/Externals/cross-compiler/patches/binutils/ld/configure.tgt.patch +++ b/Externals/cross-compiler/patches/binutils/ld/configure.tgt.patch @@ -3,5 +3,5 @@ @@ -167,1 +167,3 @@ i[3-7]86-*-nto-qnx*) targ_emul=i386nto ;; +i[3-7]86-*-acess2*) targ_emul=acess2_i386 ;; -+x86_64-*-acess2*) targ_emul=acess2_amd64 ;; ++x86_64-*-acess2*) targ_emul=acess2_amd64 ;; diff --git a/Externals/cross-compiler/patches/binutils/ld/emulparams/acess2_amd64.sh b/Externals/cross-compiler/patches/binutils/ld/emulparams/acess2_amd64.sh index d31dae98..3d027850 100644 --- a/Externals/cross-compiler/patches/binutils/ld/emulparams/acess2_amd64.sh +++ b/Externals/cross-compiler/patches/binutils/ld/emulparams/acess2_amd64.sh @@ -1,11 +1,12 @@ SCRIPT_NAME=elf -OUTPUT_FORMAT=elf64-x86_64 +OUTPUT_FORMAT=elf64-x86-64 TEXT_START_ADDR=0x00400000 MAXPAGESIZE="CONSTANT (MAXPAGESIZE)" COMMONPAGESIZE="CONSTANT (COMMONPAGESIZE)" -TEMPLATE_NAME=elf64 +ELFSIZE=64 +TEMPLATE_NAME=elf32 -ARCH=x86_64 +ARCH="i386:x86-64" MACHINE= NOP=0x90909090 GENERATE_SHLIB_SCRIPT=yes diff --git a/Externals/cross-compiler/patches/gcc/gcc/config.gcc.patch b/Externals/cross-compiler/patches/gcc/gcc/config.gcc.patch index d50ab47d..8f65ac69 100644 --- a/Externals/cross-compiler/patches/gcc/gcc/config.gcc.patch +++ b/Externals/cross-compiler/patches/gcc/gcc/config.gcc.patch @@ -12,11 +12,16 @@ + ;; *-*-darwin*) -@@ -1192,2 +1196,7 @@ +@@ -1192,2 +1196,12 @@ ;; +i[3-7]86-*-acess2*) + tm_file="${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h i386/i386elf.h newlib-stdint.h acess2.h" + tmake_file="i386/t-i386elf i386/t-crtstuff t-svr4" + use_fixproto=yes ++ ;; ++x86_64-*-acess2*) ++ tm_file="${tm_file} i386/unix.h i386/att.h dbxelf.h elfos.h newlib-stdint.h i386/i386elf.h i386/x86-64.h acess2.h" ++ tmake_file="i386/t-i386elf i386/t-crtstuff t-svr4" ++ use_fixproto=yes + ;; i[34567]86-*-elf*) diff --git a/KernelLand/Kernel/arch/x86_64/include/arch.h b/KernelLand/Kernel/arch/x86_64/include/arch.h index ec7444c7..1360b192 100644 --- a/KernelLand/Kernel/arch/x86_64/include/arch.h +++ b/KernelLand/Kernel/arch/x86_64/include/arch.h @@ -105,5 +105,7 @@ extern void SHORTREL(struct sShortSpinlock *Lock); extern void Debug_PutCharDebug(char ch); extern void Debug_PutStringDebug(const char *Str); +extern void __AtomicTestSetLoop(Uint *Ptr, Uint Value); + #endif diff --git a/KernelLand/Kernel/arch/x86_64/include/mm_virt.h b/KernelLand/Kernel/arch/x86_64/include/mm_virt.h index 120afb3f..b03875fb 100644 --- a/KernelLand/Kernel/arch/x86_64/include/mm_virt.h +++ b/KernelLand/Kernel/arch/x86_64/include/mm_virt.h @@ -48,6 +48,7 @@ #define MM_USER_MIN 0x00000000##00010000 #define USER_LIB_MAX 0x00007000##00000000 +#define MM_USER_MAX USER_LIB_MAX #define USER_STACK_PREALLOC 0x00000000##00002000 // 8 KiB #define USER_STACK_SZ 0x00000000##00020000 // 64 KiB #define USER_STACK_TOP 0x00008000##00000000 diff --git a/KernelLand/Kernel/arch/x86_64/lib.c b/KernelLand/Kernel/arch/x86_64/lib.c index b35db69c..977cffd1 100644 --- a/KernelLand/Kernel/arch/x86_64/lib.c +++ b/KernelLand/Kernel/arch/x86_64/lib.c @@ -146,6 +146,18 @@ void SHORTREL(struct sShortSpinlock *Lock) #endif } +void __AtomicTestSetLoop(Uint *Ptr, Uint Value) +{ + __ASM__( + "1:\n\t" + "xor %%eax, %%eax;\n\t" + "lock cmpxchg %0, (%1);\n\t" // if( Ptr==0 ) { ZF=1; Ptr=Value } else { ZF=0; _=Ptr } + "jnz 1b;\n\t" + :: "r"(Value), "r"(Ptr) + : "eax" // EAX clobbered + ); +} + // === DEBUG IO === #if USE_GDB_STUB void initGdbSerial(void) diff --git a/KernelLand/Kernel/arch/x86_64/mm_virt.c b/KernelLand/Kernel/arch/x86_64/mm_virt.c index 57308d7a..42b5e92a 100644 --- a/KernelLand/Kernel/arch/x86_64/mm_virt.c +++ b/KernelLand/Kernel/arch/x86_64/mm_virt.c @@ -53,6 +53,17 @@ #define INVLPG_ALL() __asm__ __volatile__ ("mov %cr3,%rax;\n\tmov %rax,%cr3;") #define INVLPG_GLOBAL() __asm__ __volatile__ ("mov %cr4,%rax;\n\txorl $0x80, %eax;\n\tmov %rax,%cr4;\n\txorl $0x80, %eax;\n\tmov %rax,%cr4") +// TODO: INVLPG_ALL is expensive +#define GET_TEMP_MAPPING(cr3) do { \ + __ASM__("cli"); \ + __AtomicTestSetLoop( (Uint *)TMPCR3(), (cr3) | 3 ); \ + INVLPG_ALL(); \ +} while(0) +#define REL_TEMP_MAPPING() do { \ + TMPCR3() = 0; \ + __ASM__("sti"); \ +} while(0) + // === CONSTS === //tPAddr * const gaPageTable = MM_FRACTAL_BASE; @@ -70,6 +81,7 @@ void MM_int_ClonePageEnt( Uint64 *Ent, void *NextLevel, tVAddr Addr, int bTable void MM_int_DumpTablesEnt(tVAddr RangeStart, size_t Length, tPAddr Expected); //void MM_DumpTables(tVAddr Start, tVAddr End); int MM_GetPageEntryPtr(tVAddr Addr, BOOL bTemp, BOOL bAllocate, BOOL bLargePage, tPAddr **Pointer); +tPAddr MM_GetPageFromAS(tProcess *Process, volatile const void *Addr); int MM_MapEx(volatile void *VAddr, tPAddr PAddr, BOOL bTemp, BOOL bLarge); // int MM_Map(tVAddr VAddr, tPAddr PAddr); void MM_Unmap(tVAddr VAddr); @@ -78,7 +90,6 @@ void MM_int_ClearTableLevel(tVAddr VAddr, int LevelBits, int MaxEnts); int MM_GetPageEntry(tVAddr Addr, tPAddr *Phys, Uint *Flags); // === GLOBALS === -tMutex glMM_TempFractalLock; tShortSpinlock glMM_ZeroPage; tPAddr gMM_ZeroPage; @@ -625,6 +636,27 @@ tPAddr MM_GetPhysAddr(volatile const void *Ptr) return (*ptr & PADDR_MASK) | (Addr & 0xFFF); } +/** + * \brief Get the address of a page from another addres space + * \return Refenced physical address (or 0 on error) + */ +tPAddr MM_GetPageFromAS(tProcess *Process, volatile const void *Addr) +{ + GET_TEMP_MAPPING(Process->MemState.CR3); + tPAddr ret = 0; + tPAddr *ptr; + if(MM_GetPageEntryPtr((tVAddr)Addr, 1,0,0, &ptr) == 0) // Temp, NoAlloc, NotLarge + { + if( *ptr & 1 ) + { + ret = (*ptr & ~0xFFF) | ((tVAddr)Addr & 0xFFF); + MM_RefPhys( ret ); + } + } + REL_TEMP_MAPPING(); + return ret; +} + /** * \brief Sets the flags on a page */ @@ -918,6 +950,15 @@ void *MM_MapTemp(tPAddr PAddr) return 0; } +void *MM_MapTempFromProc(tProcess *Process, const void *VAddr) +{ + // Get paddr + tPAddr paddr = MM_GetPageFromAS(Process, VAddr); + if( paddr == 0 ) + return NULL; + return MM_MapTemp(paddr); +} + void MM_FreeTemp(void *Ptr) { MM_Deallocate(Ptr); @@ -935,9 +976,7 @@ tPAddr MM_Clone(int bNoUserCopy) if(!ret) return 0; // #2 Alter the fractal pointer - Mutex_Acquire(&glMM_TempFractalLock); - TMPCR3() = ret | 3; - INVLPG_ALL(); + GET_TEMP_MAPPING(ret); // #3 Set Copy-On-Write to all user pages if( Threads_GetPID() != 0 && !bNoUserCopy ) @@ -1015,9 +1054,7 @@ tPAddr MM_Clone(int bNoUserCopy) // MAGIC_BREAK(); // #7 Return - TMPCR3() = 0; - INVLPG_ALL(); - Mutex_Release(&glMM_TempFractalLock); + REL_TEMP_MAPPING(); // Log("MM_Clone: RETURN %P", ret); return ret; } @@ -1061,9 +1098,7 @@ tVAddr MM_NewWorkerStack(void *StackData, size_t StackSize) int i; // #1 Set temp fractal to PID0 - Mutex_Acquire(&glMM_TempFractalLock); - TMPCR3() = ((tPAddr)gInitialPML4 - KERNEL_BASE) | 3; - INVLPG_ALL(); + GET_TEMP_MAPPING( ((tPAddr)gInitialPML4 - KERNEL_BASE) ); // #2 Scan for a free stack addresss < 2^47 for(ret = 0x100000; ret < (1ULL << 47); ret += KERNEL_STACK_SIZE) @@ -1073,7 +1108,7 @@ tVAddr MM_NewWorkerStack(void *StackData, size_t StackSize) if( !(*ptr & 1) ) break; } if( ret >= (1ULL << 47) ) { - Mutex_Release(&glMM_TempFractalLock); + REL_TEMP_MAPPING(); return 0; } @@ -1105,8 +1140,7 @@ tVAddr MM_NewWorkerStack(void *StackData, size_t StackSize) MM_FreeTemp(tmp_addr); } - TMPCR3() = 0; - Mutex_Release(&glMM_TempFractalLock); + REL_TEMP_MAPPING(); return ret + i*0x1000; } diff --git a/Usermode/Applications/init_src/main.c b/Usermode/Applications/init_src/main.c index e9c3ef91..73411b93 100644 --- a/Usermode/Applications/init_src/main.c +++ b/Usermode/Applications/init_src/main.c @@ -7,6 +7,7 @@ #include #include "common.h" #include +#include // === CONSTANTS === #define DEFAULT_SHELL "/Acess/SBin/login" @@ -499,7 +500,7 @@ int SpawnDaemon(tInitProgram *Program) // Log spawn header { char buffer[101]; - size_t len = snprintf(buffer, 100, "[%lli] init spawning ", _SysTimestamp()); + size_t len = snprintf(buffer, 100, "[%"PRIi64"] init spawning ", _SysTimestamp()); _SysWrite(out, buffer, len); for( int i = 0; Program->Command[i]; i ++ ) { diff --git a/Usermode/Libraries/crt0.o_src/Makefile b/Usermode/Libraries/crt0.o_src/Makefile index c1966ad3..9d84b923 100644 --- a/Usermode/Libraries/crt0.o_src/Makefile +++ b/Usermode/Libraries/crt0.o_src/Makefile @@ -21,12 +21,12 @@ clean: utest generate_exp utest-build utest-run: @echo > /dev/null -$(OUTPUTDIR)Libs/%.o: %.c Makefile - @mkdir -p $(dir $@) - $(CC) $(CFLAGS) -c $< -o $@ $(OUTPUTDIR)Libs/%S.o: %S.c Makefile @mkdir -p $(dir $@) $(CC) $(CFLAGS) -c $< -o $@ -fPIC +$(OUTPUTDIR)Libs/%.o: %.c Makefile + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) -c $< -o $@ $(OUTPUTDIR)Libs/%.o: $(ARCHDIR)-%.S @mkdir -p $(dir $@) $(CC) $(CFLAGS) -c $< -o $@ diff --git a/Usermode/Libraries/crt0.o_src/x86_64-crti.S b/Usermode/Libraries/crt0.o_src/x86_64-crti.S new file mode 100644 index 00000000..fb7dad33 --- /dev/null +++ b/Usermode/Libraries/crt0.o_src/x86_64-crti.S @@ -0,0 +1,15 @@ +.section .init +.global _init +.type _init, @function +_init: + push %rbp + mov %rsp, %rbp + /* gcc will nicely put the contents of crtbegin.o's .init section here. */ + +.section .fini +.global _fini +.type _fini, @function +_fini: + push %rbp + mov %rsp, %rbp + /* gcc will nicely put the contents of crtbegin.o's .fini section here. */ diff --git a/Usermode/Libraries/crt0.o_src/x86_64-crtn.S b/Usermode/Libraries/crt0.o_src/x86_64-crtn.S new file mode 100644 index 00000000..ec78f0b3 --- /dev/null +++ b/Usermode/Libraries/crt0.o_src/x86_64-crtn.S @@ -0,0 +1,9 @@ +.section .init + /* gcc will nicely put the contents of crtend.o's .init section here. */ + pop %rbp + ret + +.section .fini + /* gcc will nicely put the contents of crtend.o's .fini section here. */ + pop %rbp + ret diff --git a/Usermode/Libraries/ld-acess.so_src/elf.c b/Usermode/Libraries/ld-acess.so_src/elf.c index e3109b4a..1e31f771 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf.c +++ b/Usermode/Libraries/ld-acess.so_src/elf.c @@ -119,7 +119,7 @@ int ElfGetSymbol(void *Base, const char *Name, void **ret, size_t *Size) int elf_doRelocate_386(tElfRelocInfo *Info, uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int bRela) { const Elf32_Sym *sym = &Info->symtab[ ELF32_R_SYM(r_info) ]; - void *symval = (void*)sym->st_value; + void *symval = (void*)(intptr_t)sym->st_value; size_t size = sym->st_size; TRACE("%i '%s'", ELF32_R_TYPE(r_info), Info->strtab + sym->st_name); switch( ELF32_R_TYPE(r_info) ) @@ -183,7 +183,7 @@ int elf_doRelocate_386(tElfRelocInfo *Info, uint32_t r_info, uint32_t *ptr, Elf3 int elf_doRelocate_arm(tElfRelocInfo *Info, uint32_t r_info, uint32_t *ptr, Elf32_Addr addend, int bRela) { const Elf32_Sym *sym = &Info->symtab[ ELF32_R_SYM(r_info) ]; - void *symval = (void*)sym->st_value; + void *symval = (void*)(intptr_t)sym->st_value; size_t size = sym->st_size; TRACE("%i '%s'", ELF32_R_TYPE(r_info), Info->strtab + sym->st_name); uintptr_t val = (uintptr_t)symval; @@ -679,7 +679,7 @@ int Elf32GetSymbolInfo(void *Base, const char *Name, void **Addr, size_t *Size, TRACE("*sym = {value:0x%x,size:0x%x,info:0x%x,other:0x%x,shndx:%i}", sym->st_value, sym->st_size, sym->st_info, sym->st_other, sym->st_shndx); - if(Addr) *Addr = (void*)( sym->st_value ); + if(Addr) *Addr = (void*)(intptr_t)( sym->st_value ); if(Size) *Size = sym->st_size; if(Binding) *Binding = ELF32_ST_BIND(sym->st_info); if(Type) *Type = ELF32_ST_TYPE(sym->st_info); diff --git a/Usermode/Libraries/libc.so_src/include_exp/inttypes.h b/Usermode/Libraries/libc.so_src/include_exp/inttypes.h index 0200aa6a..110fddad 100644 --- a/Usermode/Libraries/libc.so_src/include_exp/inttypes.h +++ b/Usermode/Libraries/libc.so_src/include_exp/inttypes.h @@ -12,17 +12,23 @@ #include -#define PRId64 "lld" -#define PRIdLEAST64 "lld" -#define PRIdFAST64 "lld" +#if INT64MAX == LONG_MAX +# define _PRI64 "l" +#else +# define _PRI64 "ll" +#endif + +#define PRId64 _PRI64"ld" +#define PRIdLEAST64 _PRI64"ld" +#define PRIdFAST64 _PRI64"ld" #define PRIdMAX #define PRIdPTR -#define PRIi64 "lli" +#define PRIi64 _PRI64"i" #define PRIiLEAST64 #define PRIiFAST64 #define PRIiMAX #define PRIiPTR -#define PRIx64 "llx" +#define PRIx64 _PRI64"i" #endif diff --git a/Usermode/common_settings.mk b/Usermode/common_settings.mk index 1ac039dd..e6f9231e 100644 --- a/Usermode/common_settings.mk +++ b/Usermode/common_settings.mk @@ -8,8 +8,10 @@ LDFLAGS += -L $(ACESSDIR)/Externals/Output/$(ARCHDIR)/lib CRTI := $(OUTPUTDIR)Libs/crti.o CRTBEGIN := $(shell $(CC) $(CFLAGS) -print-file-name=crtbegin.o 2>/dev/null) +CRTBEGINS := $(shell $(CC) $(CFLAGS) -print-file-name=crtbeginS.o 2>/dev/null) CRT0 := $(OUTPUTDIR)Libs/crt0.o CRT0S := $(OUTPUTDIR)Libs/crt0S.o CRTEND := $(shell $(CC) $(CFLAGS) -print-file-name=crtend.o 2>/dev/null) +CRTENDS := $(shell $(CC) $(CFLAGS) -print-file-name=crtendS.o 2>/dev/null) CRTN := $(OUTPUTDIR)Libs/crtn.o LIBGCC_PATH = $(shell $(CC) -print-libgcc-file-name 2>/dev/null) -- 2.20.1