Add an MSR utility struct
This commit is contained in:
parent
56ace11bb1
commit
198f4196c4
18
kernel/include/misc/MSR.h
Normal file
18
kernel/include/misc/MSR.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#define IA32_EFER_MSR 0xC0000080
|
||||
|
||||
struct MSR
|
||||
{
|
||||
void write(uint64_t value);
|
||||
uint64_t read();
|
||||
|
||||
MSR(uint32_t msr_num);
|
||||
|
||||
static void write_to(uint32_t msr_num, uint64_t value);
|
||||
static uint64_t read_from(uint32_t msr_num);
|
||||
|
||||
private:
|
||||
uint32_t m_msr_num;
|
||||
};
|
30
kernel/src/misc/MSR.cpp
Normal file
30
kernel/src/misc/MSR.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include "misc/MSR.h"
|
||||
|
||||
void MSR::write_to(uint32_t msr_num, uint64_t value)
|
||||
{
|
||||
uint32_t lo = value & 0xFFFFFFFF;
|
||||
uint32_t hi = value << 32;
|
||||
asm volatile("wrmsr" : : "a"(lo), "d"(hi), "c"(msr_num));
|
||||
}
|
||||
|
||||
uint64_t MSR::read_from(uint32_t msr_num)
|
||||
{
|
||||
uint32_t lo;
|
||||
uint32_t hi;
|
||||
asm volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(msr_num));
|
||||
return (uint64_t)hi << 32 | (uint64_t)lo;
|
||||
}
|
||||
|
||||
void MSR::write(uint64_t value)
|
||||
{
|
||||
write_to(m_msr_num, value);
|
||||
}
|
||||
|
||||
uint64_t MSR::read()
|
||||
{
|
||||
return read_from(m_msr_num);
|
||||
}
|
||||
|
||||
MSR::MSR(uint32_t msr_num) : m_msr_num(msr_num)
|
||||
{
|
||||
}
|
Loading…
Reference in New Issue
Block a user