Fixed crash - need to save and restore rbx around cpuid, since the compiler may be assuming the stack pointer isn't being modified when filling in %0.
authorSam Lantinga <slouken@libsdl.org>
Mon, 30 Nov 2009 19:52:34 +0000
changeset 3498a4ce84c4f211
parent 3497 74d2f44a85de
child 3499 4cf8a1423d57
Fixed crash - need to save and restore rbx around cpuid, since the compiler 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. :)
src/cpuinfo/SDL_cpuinfo.c
     1.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Mon Nov 30 17:57:12 2009 +0000
     1.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Mon Nov 30 19:52:34 2009 +0000
     1.3 @@ -170,17 +170,19 @@
     1.4  	);
     1.5  #elif defined(__GNUC__) && defined(__x86_64__)
     1.6  	__asm__ (
     1.7 +"        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
     1.8  "        pushq   %%rbx\n"
     1.9 -"        xorl    %%eax,%%eax         # Set up for CPUID instruction    \n"
    1.10  "        cpuid                       # Get and save vendor ID          \n"
    1.11 +"        popq    %%rbx\n"
    1.12  "        cmpl    $1,%%eax            # Make sure 1 is valid input for CPUID\n"
    1.13  "        jl      1f                  # We dont have the CPUID instruction\n"
    1.14  "        xorl    %%eax,%%eax                                           \n"
    1.15  "        incl    %%eax                                                 \n"
    1.16 +"        pushq   %%rbx\n"
    1.17  "        cpuid                       # Get family/model/stepping/features\n"
    1.18 +"        popq    %%rbx\n"
    1.19  "        movl    %%edx,%0                                              \n"
    1.20  "1:                                                                    \n"
    1.21 -"        popq    %%rbx\n"
    1.22  	: "=m" (features)
    1.23  	:
    1.24  	: "%rax", "%rcx", "%rdx"
    1.25 @@ -242,16 +244,18 @@
    1.26  	);
    1.27  #elif defined(__GNUC__) && defined (__x86_64__)
    1.28  	__asm__ (
    1.29 +"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
    1.30  "        pushq   %%rbx\n"
    1.31 -"        movl    $0x80000000,%%eax   # Query for extended functions    \n"
    1.32  "        cpuid                       # Get extended function limit     \n"
    1.33 +"        popq    %%rbx\n"
    1.34  "        cmpl    $0x80000001,%%eax                                     \n"
    1.35  "        jl      1f                  # Nope, we dont have function 800000001h\n"
    1.36  "        movl    $0x80000001,%%eax   # Setup extended function 800000001h\n"
    1.37 +"        pushq   %%rbx\n"
    1.38  "        cpuid                       # and get the information         \n"
    1.39 +"        popq    %%rbx\n"
    1.40  "        movl    %%edx,%0                                              \n"
    1.41  "1:                                                                    \n"
    1.42 -"        popq    %%rbx\n"
    1.43  	: "=m" (features)
    1.44  	:
    1.45  	: "%rax", "%rcx", "%rdx"