I am trying to execute a privileged statement rdmsrin user mode, and I expect to get some privilege error, but get segfault instead. I checked asm, and I load 0x186in ecx, which should be PERFEVTSEL0, based on manual , p. 1171.
What is the reason for segfault and how can I change the code below to fix it?
I want to resolve this before hacking the kernel module, because I don't want this segfault to explode my kernel.
Update: I am running Intel(R) Xeon(R) CPU X3470.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <sched.h>
#include <assert.h>
uint64_t
read_msr(int ecx)
{
unsigned int a, d;
__asm __volatile("rdmsr" : "=a"(a), "=d"(d) : "c"(ecx));
return ((uint64_t)a) | (((uint64_t)d) << 32);
}
int main(int ac, char **av)
{
uint64_t start, end;
cpu_set_t cpuset;
unsigned int c = 0x186;
int i = 0;
CPU_ZERO(&cpuset);
CPU_SET(i, &cpuset);
assert(sched_setaffinity(0, sizeof(cpuset), &cpuset) == 0);
printf("%lu\n", read_msr(c));
return 0;
}
source
share