【发布时间】:2014-03-10 05:05:34
【问题描述】:
我试图在用户模式下执行特权指令rdmsr,我希望得到某种特权错误,但我得到了一个段错误。我检查了asm,我正在将0x186 加载到ecx,它应该是PERFEVTSEL0,基于manual,第1171 页。
段错误的原因是什么,如何修改下面的代码来修复它?
我想在破解内核模块之前解决这个问题,因为我不希望这个段错误炸毁我的内核。
更新:我正在运行 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;
}
【问题讨论】:
-
查看
dmesg输出,我想你会看到traps,这表明你正在尝试从用户模式执行特权指令。 -
@rakib,dmesg 的输出:
general protection ip:4005d0 sp:7fff6dea4070 error:0。我想这就是我需要知道的。我会接受这个答案,并单独提出后续问题。 -
真的需要在单独的问题中提出这个问题吗?你得到了你想要的。
-
我对此感到好奇,因为当我深入研究
asm时,我想更普遍地了解哪些事情会导致假segfaults。
标签: linux performance assembly x86