1

I have a small class for getting CpuID information. Inside a class constructor I have an inline function using asm code to get cpuid information. It works fine in Windows and it worked fine in Xcode 3, but now the class itself gets destroyed.

Here is the function beginning:

inline void CCPUDetector::_DetectCPU()
{
    char lvendor[13] = "UnknownVendr";

    unsigned int lfamily;
    unsigned int lmodel;
    unsigned int lstepping;

    unsigned int lfeatures  = 0;
    unsigned int lBasicInfo = 0; 

    uint32_t cpu_cores = 0; 
    uint32_t cpu_count = 1;

    unsigned int signature = 0;

    // Step 1: check if a processor supports CPUID instruction
    // ============================================================
    try
    {
        __asm
        {
            // Reset all registers
            xor    eax, eax
            xor    ebx, ebx
            xor    ecx, ecx
            xor    edx, edx

            // Call CPUID with eax register == 0
            cpuid
        };
    }

    // Old processors that do not support cpuid will throw exception
    // which is caught in the command below "__except"
    catch(...)
    {
        return;
    }
}

After the __asm block the CPUDetector class itself, the one that I am in it's constructor become invalid (NULL). I tried to disable different Xor's or cpuid but I got the same results every time.

Can someone suggest what am I doing wrong?

Iron-Eagle
  • 1,707
  • 2
  • 17
  • 34
  • How do you know it's this code that's destroying the class? – Ross Ridge May 03 '15 at 15:46
  • 1
    You should probably use [extended asm](https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html) and tell the compiler that you destroyed the registers `eax`, `ebx`, `ecx` and `edx`. Otherwise it may use one of those for something important itself... See this [answer with sample code](http://stackoverflow.com/a/4823889/547981). – Jester May 03 '15 at 15:52
  • Ross, I see it in debugger. "this" pointer becomes NULL, all the members are rubbish. – Iron-Eagle May 05 '15 at 06:14
  • Jester, can you please specify how can I tell compiler registers are destroyed? – Iron-Eagle May 05 '15 at 08:04

1 Answers1

0

It seems that clang does not supports handling ASM this way. In the end, I just used the code that I needed from those 2 files.

http://www.opensource.apple.com/source/xnu/xnu-1228.5.20/osfmk/i386/cpuid.h http://www.opensource.apple.com/source/xnu/xnu-1228.5.20/osfmk/i386/cpuid.c

Iron-Eagle
  • 1,707
  • 2
  • 17
  • 34