Add an MSR utility struct

This commit is contained in:
apio 2022-09-24 20:48:27 +02:00
parent 56ace11bb1
commit 198f4196c4
2 changed files with 48 additions and 0 deletions

18
kernel/include/misc/MSR.h Normal file
View 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
View 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)
{
}