CPUID: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
Kb (talk | contribs)
→‎Checking CPUID availability: increase comment quality
Line 10: Line 10:


<source lang="asm">
<source lang="asm">
pushfd ;Save EFLAGS
pushfd ; preserve EFLAGS to restore at the end of routine

pushfd ;Store EFLAGS
xor dword [esp],0x00200000 ;Invert the ID bit in stored EFLAGS
pushfd ; store EFLAGS (working copy)
popfd ;Load stored EFLAGS (with ID bit inverted)
xor dword [esp], 0x00200000 ; invert ID bit in our copy of EFLAGS
pushfd ;Store EFLAGS again (ID bit may or may not be inverted)
popfd ; attempt storing _altered_ EFLAGS

pop eax ;eax = modified EFLAGS (ID bit may or may not be inverted)
xor eax,[esp] ;eax = whichever bits were changed
pushfd ; Store EFLAGS again (ID bit may or may not be inverted)
popfd ;Restore original EFLAGS
pop eax ; eax = modified EFLAGS (ID bit may or may not be inverted)
and eax,0x00200000 ;eax = zero if ID bit can't be changed, else non-zero
xor eax, [esp] ; eax = whichever bits were changed

ret
popfd ; restore original EFLAGS
and eax, 0x00200000 ; eax = zero if ID bit can't be changed, else non-zero
ret
</source>
</source>


Line 25: Line 28:


Note 2: You can simply attempt to execute the CPUID instruction and see if you get an invalid opcode exception. This avoids problems with CPUs that do support CPUID but don't support the ID bit in EFLAGS; and is likely to be faster for CPUs that do support CPUID (and slower for CPUs that don't).
Note 2: You can simply attempt to execute the CPUID instruction and see if you get an invalid opcode exception. This avoids problems with CPUs that do support CPUID but don't support the ID bit in EFLAGS; and is likely to be faster for CPUs that do support CPUID (and slower for CPUs that don't).




=== Basic usage ===
=== Basic usage ===