Purpose: This is a lot different post than some of the post on this blog. This is a very specific post that perhaps very few people might be using this in their daily work. But nevertheless I thought it would be at least useful for those “few” people. Do not underestimate this task just because very few people might find this useful. In fact people who have to use these tools are very capable engineers who understand the low-level hardware in great detail. So let’s get started…

What we will learn?

We are going to learn how to read and write CPU MSRs. MSR stands for “Model Specific Registers” or “Machine Specific Registers”. Anyone who can write to MSR needs highest level of system privileges to do that. Basically by reading and writing MSR, you are dealing with your CPU at a very direct level. You can basically alter your CPU behavior with the help of these tools. Basically these registers store data and setting information for the CPU. The addresses and definition of these registers might change from one CPU generation to another.

Also please note that playing (especially) writing to your CPU MSR can lead to potential damage to your system/CPU. You have been WARNED!

Step 1: Download MSR

Debian has the msr-tools package which basically contains two utilities:

RDMSR for reading

WRMSR for writing

# apt-get update
# apt-get install msr-tools

If your distribution does not have msr-tools, then you can download from Linux kernel tree also.

Step 2: Compile kernel MSR module

The above user-space utilities depend upon the “msr” kernel module. Make sure that your Linux kernel has the “msr” module compiled:

debian:/home/kushalk# cat /boot/config-2.6.29-2-686 | grep MSR
CONFIG_X86_MSR=m (or y)

If your CONFIG_X86_MSR is not set to either “m” or “y”, then you need to re-compile your Linux kernel after setting it to “y” or “m”.

Step 3: Load “msr” module

You need to make sure that the kernel “msr” module is loaded before you can start using rdmsr and wrmsr other you will get the following error:

debian:/home/kushalk# rdmsr 0x198
rdmsr:open: No such file or directory

You can load the “msr” module by the following command:

# modprobe msr

Another way you can really check if our “msr” module is loaded or not, is by checking for following directories:

debian:/home/kushalk# ls /dev/cpu/0/msr

Step 4: Read and Write to MSR

Once you load the “msr” module, you can read values from your CPU registers like this:

# rdmsr 0x198



Basically you are reading the value from the register number 198. You need to consult your CPU/Architecture manual to see the definition of each of these registers to interpret the output. As far as I know, the above register number, 0x198, tells you the maximum frequency your CPU supports and the frequency at which your CPU is currently running at.

If you have to multi-processor system then you can specify the CPU number also like this:

# rdmsr -p1 0x198

By default it uses the “-p0” option i.e. your first CPU.

Similarly you can write values to MSR like this:

# wrmsr 0x198 <value>

For more information read the man pages:

# man rdmsr
# man wrmsr

Also note that the above is true for Intel processors. If you are having AMD processor then the above command may not work (for register 0x198).

Happy MSR’ing!

Be Sociable, Share!