Software Tuning, Performance Optimization & Platform Monitoring
Discussion regarding monitoring and software tuning methodologies, Performance Monitoring Unit (PMU) of Intel microprocessors, and platform updating.

How to use RDSMR/WRMSR at ring 3

Fadel
Beginner
1,295 Views

Hello,

I am trying to use rdmsr to read the MSRs  like MSR_RAPL_POWER_UNIT, MSR_PP0_ENERGY_STATUS? etc. 

static inline uint64_t rdmsr(uint64_t msr)
{
	uint32_t low, high;
	asm volatile (
		"rdmsr"
		: "=a"(low), "=d"(high)
		: "c"(msr)
	);
	return ((uint64_t)high << 32) | low;
}

 

I understand that reading and writing a MSR should not possible from user space and when I run the above function it showed "segmentation fault". So, I would like know if I can create a kernel module in order to enable the rdmsr/wrmsr for the ring 3 ?

 

Side note: this code source is to enable the rdpmc 

https://github.com/softdevteam/user_rdpmc

 

Thank you for your time.

 

Regards,

Fadel

 

0 Kudos
1 Reply
McCalpinJohn
Honored Contributor III
1,237 Views

If you only need to read the core performance counters, then you can do that with the RDPMC instruction at user level if the system configuration bit CR4.PCE is set.  

For Linux systems there is already a kernel device driver interface to allow user-mode programs to request MSR reads or writes.  This is implemented in arch/x86/kernel/msr.c in the version of the kernel that I use.  

There is also a project that replicates the kernel MSR infrastructure, but adds whitelisting and other security features -- https://github.com/LLNL/msr-safe

0 Kudos
Reply