Notes about MSR tools in Linux,并结合一个具体的例子,展示其用法。本文大部分内容源于Linux读写CPU MSR寄存器命令rdmsr/wrmsr

Linux内核源码提供了读写CPU MSR寄存器模块,使得用户空间可以直接读写MSR寄存器。

开源社区提供msr寄存器读写工具:msr-tools,其中有两个命令,rdmsr/wrmsr

Prerequisite

要使rdmsr/wrmsr命令可以读写msr寄存器,系统中必须有msr模块,或将msr模块编译进内核。下面是从内核配置选项中选取的内容:

1
2
Processor type and features  —>
<M> /dev/cpu/*/msr – Model-specific register support

1
2
3
modprobe msr

apt-get install -y msr-tools

Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
root@kvm:~# rdmsr -h
Usage: rdmsr [options] regno
--help -h Print this help
--version -V Print current version
--hexadecimal -x Hexadecimal output (lower case)
--capital-hex -X Hexadecimal output (upper case)
--decimal -d Signed decimal output
--unsigned -u Unsigned decimal output
--octal -o Octal output
--c-language -c Format output as a C language constant
--zero-pad -0 Output leading zeroes
--raw -r Raw binary output
--all -a all processors
--processor # -p Select processor number (default 0)
--bitfield h:l -f Output bits [h:l] only
root@kvm:~# wrmsr -h
Usage: wrmsr [options] regno value...
--help -h Print this help
--version -V Print current version
--all -a all processors
--processor # -p Select processor number (default 0)
1
2
3
4
root@kvm:~# rdmsr -p 0 0x1b
fee00d00
root@kvm:~# rdmsr -p 1 0x1b
fee00c00

由此可加:

  • CPU 0 has set BSP flag, while CPU 1 has cleared BSP flag
  • x2APIC mode is enabled
  • APIC Global enabled

Interface

/dev/cpu/CPUNUM/msr provides an interface to read and write the model-specific registers (MSRs) of an x86 CPU. CPUNUM is the number of the CPU to access as listed in /proc/cpuinfo.

The register access is done by opening the file and seeking to the MSR number as offset in the file, and then reading or writing in chunks of 8 bytes.

Resource