From 153357f640516b0213889442f90568f554ee7d68 Mon Sep 17 00:00:00 2001 From: historic_bruno Date: Thu, 21 Jul 2011 01:44:29 +0000 Subject: [PATCH] Replaces architecture-dependent cpuid function with inline GCC assembly. Should fix #670 by eliminating nasm dependency. This was SVN commit r9891. --- source/lib/sysdep/arch/amd64/amd64_abi.inc | 12 ----- source/lib/sysdep/arch/amd64/amd64_asm.asm | 48 -------------------- source/lib/sysdep/arch/amd64/amd64_asm.h | 41 ----------------- source/lib/sysdep/arch/ia32/ia32.cpp | 3 +- source/lib/sysdep/arch/ia32/ia32.inc | 24 ---------- source/lib/sysdep/arch/ia32/ia32_asm.asm | 52 ---------------------- source/lib/sysdep/arch/ia32/ia32_asm.h | 33 -------------- source/lib/sysdep/arch/x86_x64/x86_x64.cpp | 27 ++++------- 8 files changed, 10 insertions(+), 230 deletions(-) delete mode 100644 source/lib/sysdep/arch/amd64/amd64_abi.inc delete mode 100644 source/lib/sysdep/arch/amd64/amd64_asm.asm delete mode 100644 source/lib/sysdep/arch/amd64/amd64_asm.h delete mode 100644 source/lib/sysdep/arch/ia32/ia32.inc delete mode 100644 source/lib/sysdep/arch/ia32/ia32_asm.asm delete mode 100644 source/lib/sysdep/arch/ia32/ia32_asm.h diff --git a/source/lib/sysdep/arch/amd64/amd64_abi.inc b/source/lib/sysdep/arch/amd64/amd64_abi.inc deleted file mode 100644 index 19e9ab78cc..0000000000 --- a/source/lib/sysdep/arch/amd64/amd64_abi.inc +++ /dev/null @@ -1,12 +0,0 @@ -%ifdef OS_UNIX -%define arg0 rdi -%define arg1 rsi -%define arg2 rdx -%define arg3 rcx -%else -%define arg0 rcx -%define arg1 rdx -%define arg2 r8 -%define arg3 r9 -%endif - diff --git a/source/lib/sysdep/arch/amd64/amd64_asm.asm b/source/lib/sysdep/arch/amd64/amd64_asm.asm deleted file mode 100644 index 7e6edd3f65..0000000000 --- a/source/lib/sysdep/arch/amd64/amd64_asm.asm +++ /dev/null @@ -1,48 +0,0 @@ -; Copyright (c) 2010 Wildfire Games -; -; Permission is hereby granted, free of charge, to any person obtaining -; a copy of this software and associated documentation files (the -; "Software"), to deal in the Software without restriction, including -; without limitation the rights to use, copy, modify, merge, publish, -; distribute, sublicense, and/or sell copies of the Software, and to -; permit persons to whom the Software is furnished to do so, subject to -; the following conditions: -; -; The above copyright notice and this permission notice shall be included -; in all copies or substantial portions of the Software. -; -; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -; For the sym() macro -%include "../ia32/ia32.inc" -; For arg0..3 -%include "amd64_abi.inc" - -BITS 64 - -; extern "C" void CALL_CONV amd64_asm_cpuid(x86_x64_CpuidRegs* reg); -; reference: http://softwarecommunity.intel.com/articles/eng/2669.htm -global sym(amd64_asm_cpuid) - ALIGN 8 -sym(amd64_asm_cpuid): - push rbx ; rbx is the only caller-save register we clobber - - mov r8, arg0 - mov eax, DWORD [r8+0] - mov ecx, DWORD [r8+8] - cpuid - mov DWORD [r8+0], eax - mov DWORD [r8+4], ebx - mov DWORD [r8+8], ecx - mov DWORD [r8+12], edx - - pop rbx - - ret - ALIGN 8 diff --git a/source/lib/sysdep/arch/amd64/amd64_asm.h b/source/lib/sysdep/arch/amd64/amd64_asm.h deleted file mode 100644 index 0177758760..0000000000 --- a/source/lib/sysdep/arch/amd64/amd64_asm.h +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright (c) 2010 Wildfire Games - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* - * interface to various AMD64 functions (written in asm) - */ - -#ifndef INCLUDED_AMD64_ASM -#define INCLUDED_AMD64_ASM - -#ifdef __cplusplus -extern "C" { -#endif - -struct x86_x64_CpuidRegs; -extern void CALL_CONV amd64_asm_cpuid(x86_x64_CpuidRegs* reg); - -#ifdef __cplusplus -} -#endif - -#endif // #ifndef INCLUDED_AMD64_ASM diff --git a/source/lib/sysdep/arch/ia32/ia32.cpp b/source/lib/sysdep/arch/ia32/ia32.cpp index 62126e2a28..67f0c27474 100644 --- a/source/lib/sysdep/arch/ia32/ia32.cpp +++ b/source/lib/sysdep/arch/ia32/ia32.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2010 Wildfire Games +/* Copyright (c) 2011 Wildfire Games * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -28,7 +28,6 @@ #include "lib/sysdep/cpu.h" #include "lib/sysdep/arch/ia32/ia32.h" -#include "lib/sysdep/arch/ia32/ia32_asm.h" #if MSC_VERSION diff --git a/source/lib/sysdep/arch/ia32/ia32.inc b/source/lib/sysdep/arch/ia32/ia32.inc deleted file mode 100644 index 908b986a9d..0000000000 --- a/source/lib/sysdep/arch/ia32/ia32.inc +++ /dev/null @@ -1,24 +0,0 @@ -; disable executable stack -%ifidn __OUTPUT_FORMAT__,elf -section .note.GNU-stack noalloc noexec nowrite progbits -%endif -%ifidn __OUTPUT_FORMAT__,elf64 -section .note.GNU-stack noalloc noexec nowrite progbits -%endif - -; set section attributes -; section .data data align=32 use32 -; section .bss bss align=16 use32 -; section .text code align=64 use32 -; activate .text (needs to be separate because __SECT__ will otherwise -; complain that the above definition is redeclaring attributes) -section .text - -; Usage: -; use sym(ia32_cap) instead of _ia32_cap - on relevant platforms, sym() will add -; the underlines automagically, on others it won't -%ifdef DONT_USE_UNDERLINE -%define sym(a) a -%else -%define sym(a) _ %+ a -%endif diff --git a/source/lib/sysdep/arch/ia32/ia32_asm.asm b/source/lib/sysdep/arch/ia32/ia32_asm.asm deleted file mode 100644 index 509549de74..0000000000 --- a/source/lib/sysdep/arch/ia32/ia32_asm.asm +++ /dev/null @@ -1,52 +0,0 @@ -; Copyright (c) 2010 Wildfire Games -; -; Permission is hereby granted, free of charge, to any person obtaining -; a copy of this software and associated documentation files (the -; "Software"), to deal in the Software without restriction, including -; without limitation the rights to use, copy, modify, merge, publish, -; distribute, sublicense, and/or sell copies of the Software, and to -; permit persons to whom the Software is furnished to do so, subject to -; the following conditions: -; -; The above copyright notice and this permission notice shall be included -; in all copies or substantial portions of the Software. -; -; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -; assembly code for IA-32 (avoid inline assembly due to differing -; compiler support and/or syntax). - -%include "ia32.inc" - -;------------------------------------------------------------------------------- -; CPUID support -;------------------------------------------------------------------------------- - -; extern "C" void __cdecl ia32_asm_cpuid(x86_x64_CpuidRegs* regs); -global sym(ia32_asm_cpuid) -sym(ia32_asm_cpuid): - push ebx ; (clobbered by CPUID) - push edi ; (need a register other than eax..edx) - - mov edi, [esp+8+4] ; -> regs - - mov eax, [edi+0] ; eax (function) - mov ecx, [edi+8] ; ecx (count) - cpuid - stosd - xchg eax, ebx - stosd - xchg eax, ecx - stosd - xchg eax, edx - stosd - - pop edi - pop ebx - ret diff --git a/source/lib/sysdep/arch/ia32/ia32_asm.h b/source/lib/sysdep/arch/ia32/ia32_asm.h deleted file mode 100644 index d42145604e..0000000000 --- a/source/lib/sysdep/arch/ia32/ia32_asm.h +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright (c) 2010 Wildfire Games - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* - * interface to various IA-32 functions (written in assembly language) - */ - -#ifndef INCLUDED_IA32_ASM -#define INCLUDED_IA32_ASM - -struct x86_x64_CpuidRegs; -EXTERN_C void CALL_CONV ia32_asm_cpuid(x86_x64_CpuidRegs* regs); - -#endif // #ifndef INCLUDED_IA32_ASM diff --git a/source/lib/sysdep/arch/x86_x64/x86_x64.cpp b/source/lib/sysdep/arch/x86_x64/x86_x64.cpp index e32645d210..81e37d17c9 100644 --- a/source/lib/sysdep/arch/x86_x64/x86_x64.cpp +++ b/source/lib/sysdep/arch/x86_x64/x86_x64.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2010 Wildfire Games +/* Copyright (c) 2011 Wildfire Games * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -44,16 +44,15 @@ # include // __rdtsc #endif -#define HAVE_CPUIDEX 0 -#if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 150030729 // __cpuidex available on VC10+ and VC9 SP1 (allows setting ecx beforehand) -# undef HAVE_CPUIDEX -# define HAVE_CPUIDEX 1 +#if defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 150030729 +// VC10+ and VC9 SP1: __cpuidex is already available +#elif GCC_VERSION +# define __cpuidex(regsArray, level, index)\ + __asm__ __volatile__ ("cpuid"\ + : "=a" ((regsArray)[0]), "=b" ((regsArray)[1]), "=c" ((regsArray)[2]), "=d" ((regsArray)[3])\ + : "0" (level), "2" (index)); #else -# if ARCH_AMD64 -# include "lib/sysdep/arch/amd64/amd64_asm.h" -# else -# include "lib/sysdep/arch/ia32/ia32_asm.h" -# endif +# error "compiler not supported" #endif @@ -72,17 +71,9 @@ static void cpuid(x86_x64_CpuidRegs* regs) { -#if HAVE_CPUIDEX cassert(sizeof(regs->eax) == sizeof(int)); cassert(sizeof(*regs) == 4*sizeof(int)); __cpuidex((int*)regs, regs->eax, regs->ecx); -#else -# if ARCH_AMD64 - amd64_asm_cpuid(regs); -# else - ia32_asm_cpuid(regs); -# endif -#endif } static u32 cpuid_maxFunction;