Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Test whether rdmsr instruction is supported.

96 views
Skip to first unread message

RossettoeCioccolato

unread,
Jul 30, 2009, 10:01:54 PM7/30/09
to
How do I test whether rdmsr instruction is supported by the processor within
the Windows programming environment. There is a __readmsr intrinsic which
works. But I don't see anything equivalent to IsMsr() provided on some
other platforms. Do I have to use inline assembler?

Regards,

Rossetoecioccolato.


Burkhardt Braun

unread,
Jul 31, 2009, 3:38:55 AM7/31/09
to
On 31 Jul., 04:01, "RossettoeCioccolato"
<rossetoecioccol...@newsgroup.nospam> wrote:

> How do I test whether rdmsr instruction is supported by the processor within

Testing bit 5 in edx after cpuid instruction.

> the Windows programming environment.  There is a __readmsr intrinsic which
> works.  But I don't see anything equivalent to IsMsr() provided on some
> other platforms.  Do I have to use inline assembler?

donno exactly but I guess.
the code below should work on 32-bit platforms

bool IsMsr( void )
{ int _edx;
_asm{
xor eax,eax
xor ebx,ebx
xor ecx,ecx
xor edx,edx
cpuid
mov _edx,edx
}

return( _edx & 32 ? true : false );
}

Regards
Burkhardt Braun


>
> Regards,
>
> Rossetoecioccolato.

Pavel A.

unread,
Jul 31, 2009, 3:57:28 AM7/31/09
to
There is intrinsic for cpuid as well: __cpuid and __cpuidex

-pa

"Burkhardt Braun" <burkhar...@gmx.net> wrote in message
news:d3e7df2a-d799-4297...@k19g2000yqn.googlegroups.com...

Kalle Olavi Niemitalo

unread,
Jul 31, 2009, 5:43:10 AM7/31/09
to
Burkhardt Braun <burkhar...@gmx.net> writes:

> On 31 Jul., 04:01, "RossettoeCioccolato"
> <rossetoecioccol...@newsgroup.nospam> wrote:
>
>> How do I test whether rdmsr instruction is supported by the processor within
> Testing bit 5 in edx after cpuid instruction.

Does one then have to ensure that the cpuid and rdmsr run on the
same CPU? (By setting the affinity or raising to DISPATCH_LEVEL.)
Or can one assume that, were the CPUs so different that only some
of them support rdmsr at all, Windows would crash anyway?
(I'm just asking out of curiosity; if one is reading model-specific
registers of a CPU, then I suppose one already wants to know which
CPU in a multiprocessor system is being queried.)

Jonathan Wilson

unread,
Jul 31, 2009, 6:21:38 AM7/31/09
to
Its my understanding that all CPUs in a multi-cpu system have to be the same.

Volodymyr Shcherbyna

unread,
Jul 31, 2009, 6:50:33 AM7/31/09
to
>>bool IsMsr( void )
>>{ int _edx;
>> _asm{
>> xor eax,eax
>> xor ebx,ebx
>> xor ecx,ecx
>> xor edx,edx
>> cpuid
>> mov _edx,edx
>> }

>> return( _edx & 32 ? true : false );
>>}

Why not saving registers in stack before using them in _asm block and then
restoring them back from stack when finished?

--
Volodymyr M. Shcherbyna, blog: http://www.shcherbyna.com/
(This posting is provided "AS IS" with no warranties, and confers no
rights)


"Burkhardt Braun" <burkhar...@gmx.net> wrote in message
news:d3e7df2a-d799-4297...@k19g2000yqn.googlegroups.com...

Volodymyr Shcherbyna

unread,
Jul 31, 2009, 6:56:22 AM7/31/09
to
I don't think so. Microsoft is using CPUID to check if RDMSR is supported in
HAL without any speacial tricks like affinity or raising irql.

--
Volodymyr M. Shcherbyna, blog: http://www.shcherbyna.com/
(This posting is provided "AS IS" with no warranties, and confers no
rights)

"Kalle Olavi Niemitalo" <k...@iki.fi> wrote in message
news:87vdl9w...@Astalo.kon.iki.fi...

RossettoeCioccolato

unread,
Jul 31, 2009, 9:09:59 AM7/31/09
to
Thanks to all who responded. I see that the MSR feature information
returned by __cpuid and __cpuidex is documented:
http://msdn.microsoft.com/en-us/library/hskdteyh.aspx.

>I don't think so. Microsoft is using CPUID to check if RDMSR is supported
>in HAL without any speacial tricks like affinity or raising irql.

Volodymyr, could you provide a symbol which contains this code. I would
like to document that MS relies on this algorithm for my records.

> Testing bit 5 in edx after cpuid instruction.<

Not to be anal, but should I also test whether the processor supports the
cpuid instruction? See for example:

"...if you want to use the CPUID instruction to check processor properties,
you should first ensure that the processor supports the CPUID instruction."
http://books.google.com/books?id=ZFDqpSUPwAgC&pg=PA232&lpg=PA232&dq=test+cpuid+instruction+supported&source=bl&ots=X4QbQJs9dj&sig=PXrAZbyD_Dbdkx8O8_1mZC-HGBY&hl=en&ei=kelySrafDYu6Nd7I6LAM&sa=X&oi=book_result&ct=result&resnum=5#v=onepage&q=test%20cpuid%20instruction%20supported&f=false.

Or can I just assume that it is because I am running under Windows? If the
HAL makes this assumption then presumably it also is safe for ISV's.
Correct?

Regards,

Rossetoecioccolato.


Alexander Grigoriev

unread,
Jul 31, 2009, 11:09:27 AM7/31/09
to
All modern processors (at least those you can run XP on) support cpuid.

"RossettoeCioccolato" <rossetoec...@newsgroup.nospam> wrote in message
news:%23r%23r5AeE...@TK2MSFTNGP05.phx.gbl...

Alexander Grigoriev

unread,
Jul 31, 2009, 11:08:22 AM7/31/09
to
C compiler will save the registers.

On the other hand, one better use _cpuid intrinsic, because otherwise you
won't be able to compile this for x64.

"Volodymyr Shcherbyna" <v_sch...@online.mvps.org> wrote in message
news:OzMI8ycE...@TK2MSFTNGP05.phx.gbl...

Volodymyr Shcherbyna

unread,
Jul 31, 2009, 12:09:45 PM7/31/09
to
"Alexander Grigoriev" <al...@earthlink.net> wrote in message
news:uYPjADfE...@TK2MSFTNGP02.phx.gbl...

>C compiler will save the registers.

Nope, it would not :) Try to write snippet of code and test it using OllyDbg
or Ida Pro. Moreover, everywhere in HAL you can see pushes and pops for
registers used in _asm block.

Pavel A.

unread,
Jul 31, 2009, 3:22:11 PM7/31/09
to
"RossettoeCioccolato" <rossetoec...@newsgroup.nospam> wrote in message
news:#r#r5AeEK...@TK2MSFTNGP05.phx.gbl...
>...

> Not to be anal, but should I also test whether the processor supports the
> cpuid instruction? See for example:

If you want to play safe: try and catch...

__try { __cpuid( regs, 0); }
__except (EXCEPTION_EXECUTE_HANDLER) { /* cpuid caused exception, strange
/ }

-pa

Volodymyr M. Shcherbyna

unread,
Jul 31, 2009, 5:10:09 PM7/31/09
to
Hello,

Le Fri, 31 Jul 2009 09:09:59 -0400, RossettoeCioccolato a écrit :

> Volodymyr, could you provide a symbol which contains this code. I would
> like to document that MS relies on this algorithm for my records.

Isn't the link you provided not a good point to reference in
documentation? The only symbol I can suggest is HAL ...

--
Volodymyr, www.shcherbyna.com

Ben Voigt [C++ MVP]

unread,
Jul 31, 2009, 6:16:26 PM7/31/09
to

"Volodymyr Shcherbyna" <v_sch...@online.mvps.org> wrote in message

news:eqJBUlfE...@TK2MSFTNGP04.phx.gbl...


> "Alexander Grigoriev" <al...@earthlink.net> wrote in message
> news:uYPjADfE...@TK2MSFTNGP02.phx.gbl...
>>C compiler will save the registers.
>
> Nope, it would not :) Try to write snippet of code and test it using
> OllyDbg or Ida Pro. Moreover, everywhere in HAL you can see pushes and
> pops for registers used in _asm block.

What compiler are you using? With Microsoft C++ (cl.exe), if you aren't
using declspec(naked) then registers should be saved.

Volodymyr Shcherbyna

unread,
Aug 7, 2009, 11:03:43 AM8/7/09
to
"Ben Voigt [C++ MVP]" <r...@newsgroups.nospam> wrote in message
news:ADF9BC63-C9D3-423E...@microsoft.com...

> What compiler are you using? With Microsoft C++ (cl.exe), if you aren't
> using declspec(naked) then registers should be saved.

Yes and no :) If you are changing ECX in _asm block inside class function,
the compiler saves the register in stack and restore it after, so that
program does not cause access violation. But you can play with ECX in non
class functions and compiler does not save it. The notion "compiler always
saves registers which are used in _asm block" is wrong, instead it seems to
be sticking to "compiler always saves register in stack and restore it later
if it's use in _asm block might cause instability of code".

Maxim S. Shatskih

unread,
Aug 7, 2009, 11:13:34 AM8/7/09
to
> Yes and no :) If you are changing ECX in _asm block inside class function,
> the compiler saves the register in stack and restore it after, so that
> program does not cause access violation. But you can play with ECX in non

The notion of "nonvolatile registers" is well-known, for __thiscall convention in C++/COM ECX is also among them.

--
Maxim S. Shatskih
Windows DDK MVP
ma...@storagecraft.com
http://www.storagecraft.com

0 new messages