0

From intel software developer manual 3b, I came to know MSR (10H) equal to RDTSC. So I wanted to verify it using piece of code as follows in my program:

asm volatile("rdmsr":"=a"(lo),"=d"(hi):"c"(0x10));

But when I run my program it showed segmentation fault. Then a realized the caution that it work only in privilege level 0. So I run the program again with sudo access. This time the program ran without seg fault but the statement after asm volatile(..) are not getting executed. Even it dint work out.

What shall I do to make rdmsr work in my program? (I am using linux on i7 core which supports these counters. I verified it.)

Some related posts are Cannot read back from MSR and rdmsr,wrmsr from c/c++ code

Community
  • 1
  • 1
ANTHONY
  • 333
  • 5
  • 18

1 Answers1

2

You confuse CPU/hardware privileges (restrictions on instructions to execute, memory accesses) with OS/system privileges (OS functions, files, etc.). A root application does not run at a different CPU privilege level than code of a normal user. It just has more allowance for OS functions.

You have to run that instruction from the OS kernel which typically runs at ring 0.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • oh, thanks @Olaf But any idea how to run it from OS kernel level.? – ANTHONY May 11 '16 at 11:13
  • In order to run code at OS kernel level you can develop kernel module. Linux kernel modules run in ring 0. – desowin May 11 '16 at 11:22
  • @ANTHONY: As desowin wrote: If only the kernel runs in Ring 0, you apparently need a kernel module. Note that is not like a simple C program and can cause damage to hardware and filesystem, up to complete loss of data - because ring 0 allows code to do basically **anything**. – too honest for this site May 11 '16 at 14:27
  • @ANTHONY: If the answer was helpful, please remember to upvote and/or accept. – too honest for this site May 11 '16 at 14:28