cmsr

Root Has Segfault Executing RDMSR Assembly Code


I would like to read msr 0x19a (IA32_CLOCK_MODULATIOn) directly from C code WITH root privilege. However, I get the following segfault error.

a.out[27843] general protection ip:40053b sp:7fffefc38020 error:0 in a.out[400000+1000]

Does anyone know whether this way of calling rdmsr is a viable option?

Thanks in advance!

#include <stdio.h>
#define __init

typedef unsigned uint32_t;
static int __init test3_init(void)
{
     uint32_t hi,lo;
     hi=0x0; lo=0x0;
     asm volatile("mov $0x19a,%ecx");
     asm volatile("rdmsr":"=a"(lo),"=d"(hi));
     printf("exit_readmsr: hi=%08x lo=%08x\n",hi,lo);
     return 0;
 }

 int main(void)
 {                                          
      return test3_init();                      
 }

BTW, the code is extract from this answer.


Solution

  • This instruction must be executed at privilege level 0. In other words, you must be inside the kernel.