Wiki: CPUID

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
Post Reply
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Wiki: CPUID

Post by Hangin10 »

I've added a routine to the article that checks that the CPUID instruction is supported.

I am fairly confident that it is correct, but if someone could take a look and double check, I'd
appreciate it.
jackman
Posts: 7
Joined: Mon Mar 31, 2014 8:26 pm

Re: Wiki: CPUID

Post by jackman »

Hangin10 wrote:I've added a routine to the article that checks that the CPUID instruction is supported.

I am fairly confident that it is correct, but if someone could take a look and double check, I'd
appreciate it.
I believe this is the code in question:

Code: Select all

; returns 1 if CPUID is supported, 0 otherwise (ZF is also set accordingly)
pushfd ; get
pop eax
mov ecx, eax ; save 
xor eax, 0x200000 ; flip
push eax ; set
popfd
pushfd ; and test
pop eax
xor eax, ecx ; mask changed bits
shr eax, 21 ; move bit 21 to bit 0
and eax, 1 ; and mask others
push ecx
popfd ; restore original flags
ret
Forgive my ignorance. How is this called from C?

I don't see much reference to it in the forums or general googling, so I'm guessing that people just assume their CPU is CPUID capable.

Thank you.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Wiki: CPUID

Post by Combuster »

How is this called from C?
This code follows the i386 system V ABI. That means you just give it a name and declare it as (extern "C") int isCPUIDsupported(); like you would for any function call in a different object file.
people just assume their CPU is CPUID capable.
More likely, they're not using it in the first place.
ZF is also set accordingly
Considering the last two instructions are POPFD; RET; I call all flags preserved instead. :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Wiki: CPUID

Post by iansjack »

Combuster wrote:
ZF is also set accordingly
Considering the last two instructions are POPFD; RET; I call all flags preserved instead. :wink:
But isn't the POPFD restoring the flags as pushed earlier in the routine (by PUSH ECX), not as originally set?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Wiki: CPUID

Post by Combuster »

Find the only assignment to ECX :wink:

Meanwhile, Brendan demonstrated a much shorter version.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply