Why segfault instead of privilege instruction error?

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;
}
+4
source share
1 answer

, : SIGSEGV SIGILL, , ( , )


SIGILL si_code ILL_PRVOPC segfault. 3 , . : - (


segfault

, Linux SIGSEGV. : http://elixir.free-electrons.com/linux/v4.9/source/arch/x86/kernel/traps.c#L487 .

, SIGSEGV , . , cli.

, ?

Linux 4.9, - ( SIGSEGV) .

. SIGSEGV, siginfo_t si_code , SIGSEGV man 2 sigaction. SEGV_MAPERR, SEGV_ACCERR, SEGV_PKUERR, SI_KERNEL (0x80) . , SI_KERNEL - , si_code ". strace SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0}. .

grep dmesg .

, GPF .

: rdmsr . , , SIGSEGV.

+3

Source: https://habr.com/ru/post/1530914/


All Articles