Skip to content

Commit

Permalink
Fixed crash - need to save and restore rbx around cpuid, since the co…
Browse files Browse the repository at this point in the history
…mpiler may be assuming the stack pointer isn't being modified when filling in %0.

I did it around each call to cpuid which isn't strictly necessary, but is definitely future proof. :)
  • Loading branch information
slouken committed Dec 2, 2009
1 parent 05cd41f commit 80bc2e6
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/cpuinfo/SDL_cpuinfo.c
Expand Up @@ -26,7 +26,7 @@
#include "SDL.h"
#include "SDL_cpuinfo.h"

#if defined(__MACOSX__) && defined(__ppc__)
#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
#include <sys/sysctl.h> /* For AltiVec check */
#elif SDL_ALTIVEC_BLITTERS && HAVE_SETJMP
#include <signal.h>
Expand Down Expand Up @@ -148,67 +148,69 @@ static __inline__ int CPU_getCPUIDFeatures(void)
int features = 0;
#if defined(__GNUC__) && defined(i386)
__asm__ (
" pushl %%ebx\n"
" xorl %%eax,%%eax # Set up for CPUID instruction \n"
" pushl %%ebx \n"
" cpuid # Get and save vendor ID \n"
" cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
" jl 1f # We dont have the CPUID instruction\n"
" xorl %%eax,%%eax \n"
" incl %%eax \n"
" cpuid # Get family/model/stepping/features\n"
" popl %%ebx \n"
" movl %%edx,%0 \n"
"1: \n"
" popl %%ebx\n"
: "=m" (features)
:
: "%eax", "%ecx", "%edx"
);
#elif defined(__GNUC__) && defined(__x86_64__)
__asm__ (
" pushq %%rbx\n"
" xorl %%eax,%%eax # Set up for CPUID instruction \n"
" pushq %%rbx\n"
" cpuid # Get and save vendor ID \n"
" cmpl $1,%%eax # Make sure 1 is valid input for CPUID\n"
" jl 1f # We dont have the CPUID instruction\n"
" xorl %%eax,%%eax \n"
" incl %%eax \n"
" cpuid # Get family/model/stepping/features\n"
" popq %%rbx\n"
" movl %%edx,%0 \n"
"1: \n"
" popq %%rbx\n"
: "=m" (features)
:
: "%rax", "%rcx", "%rdx"
);
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
__asm {
xor eax, eax ; Set up for CPUID instruction
push ebx
cpuid ; Get and save vendor ID
cmp eax, 1 ; Make sure 1 is valid input for CPUID
jl done ; We dont have the CPUID instruction
xor eax, eax
inc eax
cpuid ; Get family/model/stepping/features
pop ebx
mov features, edx
done:
}
#elif defined(__sun) && (defined(__i386) || defined(__amd64))
__asm(
" pushl %ebx\n"
" xorl %eax,%eax \n"
" pushl %ebx \n"
" cpuid \n"
" cmpl $1,%eax \n"
" jl 1f \n"
" xorl %eax,%eax \n"
" incl %eax \n"
" cpuid \n"
" popl %ebx \n"
#ifdef __i386
" movl %edx,-8(%ebp) \n"
#else
" movl %edx,-8(%rbp) \n"
#endif
"1: \n"
" popl %ebx\n" );
#endif
return features;
}
Expand All @@ -218,63 +220,65 @@ static __inline__ int CPU_getCPUIDFeaturesExt(void)
int features = 0;
#if defined(__GNUC__) && defined(i386)
__asm__ (
" pushl %%ebx\n"
" movl $0x80000000,%%eax # Query for extended functions \n"
" pushl %%ebx \n"
" cpuid # Get extended function limit \n"
" cmpl $0x80000001,%%eax \n"
" jl 1f # Nope, we dont have function 800000001h\n"
" movl $0x80000001,%%eax # Setup extended function 800000001h\n"
" cpuid # and get the information \n"
" popl %%ebx \n"
" movl %%edx,%0 \n"
"1: \n"
" popl %%ebx\n"
: "=m" (features)
:
: "%eax", "%ecx", "%edx"
);
#elif defined(__GNUC__) && defined (__x86_64__)
__asm__ (
" pushq %%rbx\n"
" movl $0x80000000,%%eax # Query for extended functions \n"
" pushq %%rbx \n"
" cpuid # Get extended function limit \n"
" cmpl $0x80000001,%%eax \n"
" jl 1f # Nope, we dont have function 800000001h\n"
" movl $0x80000001,%%eax # Setup extended function 800000001h\n"
" cpuid # and get the information \n"
" popq %%rbx \n"
" movl %%edx,%0 \n"
"1: \n"
" popq %%rbx\n"
: "=m" (features)
:
: "%rax", "%rcx", "%rdx"
);
#elif (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
__asm {
mov eax,80000000h ; Query for extended functions
push ebx
cpuid ; Get extended function limit
cmp eax,80000001h
jl done ; Nope, we dont have function 800000001h
mov eax,80000001h ; Setup extended function 800000001h
cpuid ; and get the information
pop ebx
mov features,edx
done:
}
#elif defined(__sun) && ( defined(__i386) || defined(__amd64) )
__asm (
" pushl %ebx\n"
" movl $0x80000000,%eax \n"
" pushl %ebx \n"
" cpuid \n"
" cmpl $0x80000001,%eax \n"
" jl 1f \n"
" movl $0x80000001,%eax \n"
" cpuid \n"
" popl %ebx \n"
#ifdef __i386
" movl %edx,-8(%ebp) \n"
#else
" movl %edx,-8(%rbp) \n"
#endif
"1: \n"
" popl %ebx\n"
);
#endif
return features;
Expand Down Expand Up @@ -339,7 +343,7 @@ static __inline__ int CPU_haveSSE2(void)
static __inline__ int CPU_haveAltiVec(void)
{
volatile int altivec = 0;
#if defined(__MACOSX__) && defined(__ppc__)
#if defined(__MACOSX__) && (defined(__ppc__) || defined(__ppc64__))
int selectors[2] = { CTL_HW, HW_VECTORUNIT };
int hasVectorUnit = 0;
size_t length = sizeof(hasVectorUnit);
Expand Down

0 comments on commit 80bc2e6

Please sign in to comment.