#include <sys/types.h>
#include <machine/cpufunc.h>
#include <machine/vmm.h>
#include <machine/specialreg.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vmmapi.h>
#include "debug.h"
#include "xmsr.h"
static int cpu_vendor_intel, cpu_vendor_amd, cpu_vendor_hygon;
int
emulate_wrmsr(struct vcpu *vcpu __unused, uint32_t num, uint64_t val __unused)
{
if (cpu_vendor_intel) {
switch (num) {
#ifndef __FreeBSD__
case MSR_PERFCTR0:
case MSR_PERFCTR1:
case MSR_EVNTSEL0:
case MSR_EVNTSEL1:
return (0);
#endif
case 0xd04:
case 0xc24:
return (0);
case MSR_BIOS_UPDT_TRIG:
return (0);
case MSR_BIOS_SIGN:
return (0);
default:
break;
}
} else if (cpu_vendor_amd || cpu_vendor_hygon) {
switch (num) {
case MSR_HWCR:
return (0);
case MSR_NB_CFG1:
case MSR_LS_CFG:
case MSR_IC_CFG:
return (0);
case MSR_PERFEVSEL0:
case MSR_PERFEVSEL1:
case MSR_PERFEVSEL2:
case MSR_PERFEVSEL3:
return (0);
case MSR_K7_PERFCTR0:
case MSR_K7_PERFCTR1:
case MSR_K7_PERFCTR2:
case MSR_K7_PERFCTR3:
return (0);
case MSR_P_STATE_CONTROL:
return (0);
default:
break;
}
}
return (-1);
}
int
emulate_rdmsr(struct vcpu *vcpu __unused, uint32_t num, uint64_t *val)
{
int error = 0;
if (cpu_vendor_intel) {
switch (num) {
case MSR_BIOS_SIGN:
case MSR_IA32_PLATFORM_ID:
case MSR_PKG_ENERGY_STATUS:
case MSR_PP0_ENERGY_STATUS:
case MSR_PP1_ENERGY_STATUS:
case MSR_DRAM_ENERGY_STATUS:
case MSR_MISC_FEATURE_ENABLES:
*val = 0;
break;
case MSR_RAPL_POWER_UNIT:
*val = 0x000a1003;
break;
case MSR_IA32_FEATURE_CONTROL:
*val = IA32_FEATURE_CONTROL_LOCK;
break;
default:
error = -1;
break;
}
} else if (cpu_vendor_amd || cpu_vendor_hygon) {
switch (num) {
case MSR_BIOS_SIGN:
*val = 0;
break;
case MSR_HWCR:
*val = 0x01000010;
*val |= 1 << 9;
break;
case MSR_NB_CFG1:
case MSR_LS_CFG:
case MSR_IC_CFG:
*val = 0;
break;
case MSR_PERFEVSEL0:
case MSR_PERFEVSEL1:
case MSR_PERFEVSEL2:
case MSR_PERFEVSEL3:
*val = 0;
break;
case MSR_K7_PERFCTR0:
case MSR_K7_PERFCTR1:
case MSR_K7_PERFCTR2:
case MSR_K7_PERFCTR3:
*val = 0;
break;
case MSR_SMM_ADDR:
case MSR_SMM_MASK:
*val = 0;
break;
case MSR_P_STATE_LIMIT:
case MSR_P_STATE_CONTROL:
case MSR_P_STATE_STATUS:
case MSR_P_STATE_CONFIG(0):
*val = 0;
break;
case 0xC0011029:
*val = 1;
break;
#ifndef __FreeBSD__
case MSR_VM_CR:
*val = VM_CR_SVMDIS;
break;
#endif
default:
error = -1;
break;
}
} else {
error = -1;
}
return (error);
}
int
init_msr(void)
{
int error;
u_int regs[4];
char cpu_vendor[13];
do_cpuid(0, regs);
((u_int *)&cpu_vendor)[0] = regs[1];
((u_int *)&cpu_vendor)[1] = regs[3];
((u_int *)&cpu_vendor)[2] = regs[2];
cpu_vendor[12] = '\0';
error = 0;
if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
cpu_vendor_amd = 1;
} else if (strcmp(cpu_vendor, "HygonGenuine") == 0) {
cpu_vendor_hygon = 1;
} else if (strcmp(cpu_vendor, "GenuineIntel") == 0) {
cpu_vendor_intel = 1;
} else {
EPRINTLN("Unknown cpu vendor \"%s\"", cpu_vendor);
error = ENOENT;
}
return (error);
}