The MASM Forum

General => The Campus => Topic started by: nonosto on January 11, 2018, 01:02:24 AM

Title: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 11, 2018, 01:02:24 AM
Hello world

This is a piece of code use RDMSR from GCC inline assembler:

__asm__ __volatile__ ("rdmsr"
                        :"=a"(low), "=d"(high)
                        :"c"(msr));


I need to translate it to Visual C++ inline assembler, and I try it:

__asm
{
rdmsr
eax, low;
edx, high;
ecx, msr;
}


But I have this log:

Severity Code Description Project File Line Suppression State
Warning C4405 'eax': identifier is reserved word
Error C2400 inline assembler syntax error in 'opcode'; found ','
Warning C4405 'edx': identifier is reserved word
Error C2400 inline assembler syntax error in 'opcode'; found ','
Warning C4405 'ecx': identifier is reserved word
Error C2400 inline assembler syntax error in 'opcode'; found ','


I am newbie and I need help. Could you helpme please?

THX
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: aw27 on January 11, 2018, 01:21:40 AM
You can't run RDMSR in user mode. It is a privileged instruction.
Given this, I don't need to tell you that eax, low is not an ASM instruction. May be you are thinking about mov eax, low.
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 11, 2018, 01:30:43 AM
thanks very much

QuoteYou can't run RDMSR in user mode. It is a privileged instruction.

I saw somewhere it, this instruction must be executed at a privilege level of 0, or in kernel mode. How to use so in this context?

QuoteGiven this, I don't need to tell you that eax, low is not an ASM instruction. May be you are thinking about mov eax, low.

If i understand, in GCC:

"=a"(low)

it's in VSC++ illine:

mov eax, low
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: jj2007 on January 11, 2018, 01:37:24 AM
Quote from: nonosto on January 11, 2018, 01:30:43 AMI saw somewhere it, this instruction must be executed at a privilege level of 0, or in kernel mode. How to use so in this context?

Write a driver, and it will work. Why do you need RDMSR?
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 11, 2018, 04:22:05 AM
THX.
I need patch kernel
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: Adamanteus on January 11, 2018, 06:06:54 AM
That's possible by such way :
Code (asm) Select

RDMSR EAX ; maybe AX - as it not go to opcode could be differ
MOV EAX, CR0 ; alternative for kernel
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 11, 2018, 10:04:25 AM
Thanks all

After your help I found this:

https://books.google.fr/books?id=5Cs46M9FV0sC&lpg=PA284&ots=pXHGJxvfIs&dq=RDMSR%20EAX%20%3B&hl=fr&pg=PA284#v=onepage&q=RDMSR%20EAX%20;&f=true (https://books.google.fr/books?id=5Cs46M9FV0sC&lpg=PA284&ots=pXHGJxvfIs&dq=RDMSR%20EAX%20%3B&hl=fr&pg=PA284#v=onepage&q=RDMSR%20EAX%20;&f=true)

(https://i62.servimg.com/u/f62/15/96/88/01/rdmsr10.png)
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 11, 2018, 11:11:45 AM
So I try this code:

__int64 rdmsr(__int32 msr, __int32* pHigh, __int32* pLow)
{
__int32 low, high;
__asm
{
mov msr, ecx;
rdmsr;
mov eax, low;
mov edx, high;
}
}


But I have this error:


Severity Code Description Project File Line Suppression State
Error C2400 inline assembler syntax error in 'second operand'; found 'newline' TEST_ASM
Error C2400 inline assembler syntax error in 'second operand'; found 'newline' TEST_ASM
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: hutch-- on January 11, 2018, 11:20:27 AM
> THX.
> I need patch kernel

Why the hell do you need to patch a 32 bit kernel ?

Answer this or the post will be removed and do not try and feed us bullsh*t.
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 11, 2018, 11:32:02 AM
I try to translate an application write on GCC inline to visual C++ 2003 for an old app (windows 2000).

Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: aw27 on January 11, 2018, 06:57:10 PM
It is a real pain to see what you are trying to do.
You should use the __readmsr intrinsics and forget about ASM for the time being.
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 12, 2018, 12:09:24 AM
intrinsic not support by VC 2003
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: aw27 on January 12, 2018, 02:04:34 AM
All right, if you are looking to run it on Windows 2000 your options are diminished.
But if you have the Platform Toolset Visual Studio 2008 (v90) you can build it even on VS 2017.
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 12, 2018, 02:31:00 AM
Unlike I must use VS2003 only....it's a very specific hardware.
I dont see my error how to fix it please?
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: aw27 on January 12, 2018, 02:58:34 AM
There are many errors, but the immediate ones are that "low" and "high" are keywords.


Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 12, 2018, 03:13:51 AM
Thank you very much, now no error in LOG with this code:


__int64 rdmsr(__int32 msr, __int32* pHigh, __int32* pLow)
{
__int32 tlow, thigh;
__asm
{
mov ecx, msr;
rdmsr;
mov tlow, eax;
mov thigh, edx;
}
__int64 value = ((__int64)thigh << 32) | (__int64)tlow;
*pLow = tlow;
*pHigh = thigh;
return value;
}


Last question please, do you think that gives the same result as this  especially the __volatile__ instruction:


uint64_t rdmsr(uint32_t msr, uint32_t* pHigh, uint32_t* pLow) {
  uint32_t low, high;

  __asm__ __volatile__ ("rdmsr"
                        :"=a"(low), "=d"(high)
                        :"c"(msr));

  uint64_t value = ((uint64_t)high << 32) | (uint64_t)low;
  *pLow = low;
  *pHigh = high;
  return value;
}

Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: aw27 on January 12, 2018, 03:48:47 AM
This should work as well (the return value is already in edx:eax):

__int64 rdmsr(unsigned _msr, __int32* pHigh, __int32* pLow)
{
   __asm {
      mov ecx, _msr
      rdmsr
      mov ecx, pHigh
      mov [ecx], edx
      mov ecx, pLow
      mov [ecx],eax
      leave
      ret
   }
}
Title: Re: RDMSR & inline assembler Visual Studio C++
Post by: nonosto on January 12, 2018, 10:42:35 AM
THX very much. I will test it quickly.