changeset 2bad3d5120e5 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=2bad3d5120e5
description:
kvm: FPU synchronization support on x86
This changeset adds support for synchronizing the FPU and SIMD state
of a virtual x86 CPU with gem5. It supports both the XSave API and the
KVM_(GET|SET)_FPU kernel API. The XSave interface can be disabled
using the useXSave parameter (in case of kernel
issues). Unfortunately, KVM_(GET|SET)_FPU interface seems to be buggy
in some kernels (specifically, the MXCSR register isn't always
synchronized), which means that it might not be possible to
synchronize MXCSR on old kernels without the XSave interface.
This changeset depends on the __float80 type in gcc and might not
build using llvm.
diffstat:
src/cpu/kvm/X86KvmCPU.py | 2 +
src/cpu/kvm/x86_cpu.cc | 287 +++++++++++++++++++++++++++++++++++++++++++---
src/cpu/kvm/x86_cpu.hh | 36 +++++-
3 files changed, 300 insertions(+), 25 deletions(-)
diffs (truncated from 451 to 300 lines):
diff -r 2dbc34e3b922 -r 2bad3d5120e5 src/cpu/kvm/X86KvmCPU.py
--- a/src/cpu/kvm/X86KvmCPU.py Mon Sep 30 09:42:30 2013 +0200
+++ b/src/cpu/kvm/X86KvmCPU.py Mon Sep 30 09:43:43 2013 +0200
@@ -43,3 +43,5 @@
void dumpXSave();
void dumpVCpuEvents();
''')
+
+ useXSave = Param.Bool(True, "Use XSave to synchronize FPU/SIMD registers")
diff -r 2dbc34e3b922 -r 2bad3d5120e5 src/cpu/kvm/x86_cpu.cc
--- a/src/cpu/kvm/x86_cpu.cc Mon Sep 30 09:42:30 2013 +0200
+++ b/src/cpu/kvm/x86_cpu.cc Mon Sep 30 09:43:43 2013 +0200
@@ -67,6 +67,38 @@
// data) is used to indicate that a segment has been accessed.
#define SEG_TYPE_BIT_ACCESSED 1
+struct FXSave
+{
+ uint16_t fcw;
+ uint16_t fsw;
+ uint8_t ftwx;
+ uint8_t pad0;
+ uint16_t last_opcode;
+ union {
+ struct {
+ uint32_t fpu_ip;
+ uint16_t fpu_cs;
+ uint16_t pad1;
+ uint32_t fpu_dp;
+ uint16_t fpu_ds;
+ uint16_t pad2;
+ } ctrl32;
+
+ struct {
+ uint64_t fpu_ip;
+ uint64_t fpu_dp;
+ } ctrl64;
+ };
+ uint32_t mxcsr;
+ uint32_t mxcsr_mask;
+
+ uint8_t fpr[8][16];
+ uint8_t xmm[16][16];
+
+ uint64_t reserved[12];
+} M5_ATTR_PACKED;
+
+static_assert(sizeof(FXSave) == 512, "Unexpected size of FXSave");
#define FOREACH_IREG() \
do { \
@@ -208,23 +240,61 @@
#endif
static void
-dumpKvm(const struct kvm_fpu &fpu)
+dumpFpuSpec(const struct FXSave &xs)
{
- inform("FPU registers:\n");
+ inform("\tlast_ip: 0x%x\n", xs.ctrl64.fpu_ip);
+ inform("\tlast_dp: 0x%x\n", xs.ctrl64.fpu_dp);
+ inform("\tmxcsr_mask: 0x%x\n", xs.mxcsr_mask);
+}
+
+static void
+dumpFpuSpec(const struct kvm_fpu &fpu)
+{
+ inform("\tlast_ip: 0x%x\n", fpu.last_ip);
+ inform("\tlast_dp: 0x%x\n", fpu.last_dp);
+}
+
+template<typename T>
+static void
+dumpFpuCommon(const T &fpu)
+{
+ const unsigned top((fpu.fsw >> 11) & 0x7);
inform("\tfcw: 0x%x\n", fpu.fcw);
- inform("\tfsw: 0x%x\n", fpu.fsw);
+
+ inform("\tfsw: 0x%x (top: %i, "
+ "conditions: %s%s%s%s, exceptions: %s%s%s%s%s%s %s%s%s)\n",
+ fpu.fsw, top,
+
+ (fpu.fsw & CC0Bit) ? "C0" : "",
+ (fpu.fsw & CC1Bit) ? "C1" : "",
+ (fpu.fsw & CC2Bit) ? "C2" : "",
+ (fpu.fsw & CC3Bit) ? "C3" : "",
+
+ (fpu.fsw & IEBit) ? "I" : "",
+ (fpu.fsw & DEBit) ? "D" : "",
+ (fpu.fsw & ZEBit) ? "Z" : "",
+ (fpu.fsw & OEBit) ? "O" : "",
+ (fpu.fsw & UEBit) ? "U" : "",
+ (fpu.fsw & PEBit) ? "P" : "",
+
+ (fpu.fsw & StackFaultBit) ? "SF " : "",
+ (fpu.fsw & ErrSummaryBit) ? "ES " : "",
+ (fpu.fsw & BusyBit) ? "BUSY " : ""
+ );
inform("\tftwx: 0x%x\n", fpu.ftwx);
inform("\tlast_opcode: 0x%x\n", fpu.last_opcode);
- inform("\tlast_ip: 0x%x\n", fpu.last_ip);
- inform("\tlast_dp: 0x%x\n", fpu.last_dp);
+ dumpFpuSpec(fpu);
inform("\tmxcsr: 0x%x\n", fpu.mxcsr);
inform("\tFP Stack:\n");
for (int i = 0; i < 8; ++i) {
- const bool empty(!((fpu.ftwx >> i) & 0x1));
+ const unsigned reg_idx((i + top) & 0x7);
+ const bool empty(!((fpu.ftwx >> reg_idx) & 0x1));
+ const double value(X86ISA::loadFloat80(fpu.fpr[i]));
char hex[33];
- for (int j = 0; j < 16; ++j)
+ for (int j = 0; j < 10; ++j)
snprintf(&hex[j*2], 3, "%.2x", fpu.fpr[i][j]);
- inform("\t\t%i: 0x%s%s\n", i, hex, empty ? " (e)" : "");
+ inform("\t\tST%i/%i: 0x%s (%f)%s\n", i, reg_idx,
+ hex, value, empty ? " (e)" : "");
}
inform("\tXMM registers:\n");
for (int i = 0; i < 16; ++i) {
@@ -236,6 +306,20 @@
}
static void
+dumpKvm(const struct kvm_fpu &fpu)
+{
+ inform("FPU registers:\n");
+ dumpFpuCommon(fpu);
+}
+
+static void
+dumpKvm(const struct kvm_xsave &xsave)
+{
+ inform("FPU registers (XSave):\n");
+ dumpFpuCommon(*(FXSave *)xsave.region);
+}
+
+static void
dumpKvm(const struct kvm_msrs &msrs)
{
inform("MSRs:\n");
@@ -261,15 +345,6 @@
}
static void
-dumpKvm(const struct kvm_xsave &xsave)
-{
- inform("KVM XSAVE:\n");
-
- Trace::dump((Tick)-1, "xsave.region",
- xsave.region, sizeof(xsave.region));
-}
-
-static void
dumpKvm(const struct kvm_vcpu_events &events)
{
inform("vCPU events:\n");
@@ -441,7 +516,8 @@
}
X86KvmCPU::X86KvmCPU(X86KvmCPUParams *params)
- : BaseKvmCPU(params)
+ : BaseKvmCPU(params),
+ useXSave(params->useXSave)
{
Kvm &kvm(vm.kvm);
@@ -457,6 +533,14 @@
haveDebugRegs = kvm.capDebugRegs();
haveXSave = kvm.capXSave();
haveXCRs = kvm.capXCRs();
+
+ if (useXSave && !haveXSave) {
+ warn("KVM: XSAVE not supported by host. MXCSR synchronization might be
"
+ "unreliable due to kernel bugs.\n");
+ useXSave = false;
+ } else if (!useXSave) {
+ warn("KVM: XSave FPU/SIMD synchronization disabled by user.\n");
+ }
}
X86KvmCPU::~X86KvmCPU()
@@ -483,13 +567,15 @@
X86KvmCPU::dump()
{
dumpIntRegs();
- dumpFpuRegs();
+ if (useXSave)
+ dumpXSave();
+ else
+ dumpFpuRegs();
dumpSpecRegs();
dumpDebugRegs();
dumpXCRs();
dumpVCpuEvents();
dumpMSRs();
- dumpXSave();
}
void
@@ -729,10 +815,100 @@
setSpecialRegisters(sregs);
}
+
+template <typename T>
+static void
+updateKvmStateFPUCommon(ThreadContext *tc, T &fpu)
+{
+ static_assert(sizeof(X86ISA::FloatRegBits) == 8,
+ "Unexpected size of X86ISA::FloatRegBits");
+
+ fpu.mxcsr = tc->readMiscRegNoEffect(MISCREG_MXCSR);
+ fpu.fcw = tc->readMiscRegNoEffect(MISCREG_FCW);
+ // No need to rebuild from MISCREG_FSW and MISCREG_TOP if we read
+ // with effects.
+ fpu.fsw = tc->readMiscReg(MISCREG_FSW);
+
+ uint64_t ftw(tc->readMiscRegNoEffect(MISCREG_FTW));
+ fpu.ftwx = X86ISA::convX87TagsToXTags(ftw);
+
+ fpu.last_opcode = tc->readMiscRegNoEffect(MISCREG_FOP);
+
+ const unsigned top((fpu.fsw >> 11) & 0x7);
+ for (int i = 0; i < 8; ++i) {
+ const unsigned reg_idx((i + top) & 0x7);
+ const double value(tc->readFloatReg(FLOATREG_FPR(reg_idx)));
+ DPRINTF(KvmContext, "Setting KVM FP reg %i (st[%i]) := %f\n",
+ reg_idx, i, value);
+ X86ISA::storeFloat80(fpu.fpr[i], value);
+ }
+
+ // TODO: We should update the MMX state
+
+ for (int i = 0; i < 16; ++i) {
+ *(X86ISA::FloatRegBits *)&fpu.xmm[i][0] =
+ tc->readFloatRegBits(FLOATREG_XMM_LOW(i));
+ *(X86ISA::FloatRegBits *)&fpu.xmm[i][8] =
+ tc->readFloatRegBits(FLOATREG_XMM_HIGH(i));
+ }
+}
+
+void
+X86KvmCPU::updateKvmStateFPULegacy()
+{
+ struct kvm_fpu fpu;
+
+ // There is some padding in the FP registers, so we'd better zero
+ // the whole struct.
+ memset(&fpu, 0, sizeof(fpu));
+
+ updateKvmStateFPUCommon(tc, fpu);
+
+ if (tc->readMiscRegNoEffect(MISCREG_FISEG))
+ warn_once("MISCREG_FISEG is non-zero.\n");
+
+ fpu.last_ip = tc->readMiscRegNoEffect(MISCREG_FIOFF);
+
+ if (tc->readMiscRegNoEffect(MISCREG_FOSEG))
+ warn_once("MISCREG_FOSEG is non-zero.\n");
+
+ fpu.last_dp = tc->readMiscRegNoEffect(MISCREG_FOOFF);
+
+ setFPUState(fpu);
+}
+
+void
+X86KvmCPU::updateKvmStateFPUXSave()
+{
+ struct kvm_xsave kxsave;
+ FXSave &xsave(*(FXSave *)kxsave.region);
+
+ // There is some padding and reserved fields in the structure, so
+ // we'd better zero the whole thing.
+ memset(&kxsave, 0, sizeof(kxsave));
+
+ updateKvmStateFPUCommon(tc, xsave);
+
+ if (tc->readMiscRegNoEffect(MISCREG_FISEG))
+ warn_once("MISCREG_FISEG is non-zero.\n");
+
+ xsave.ctrl64.fpu_ip = tc->readMiscRegNoEffect(MISCREG_FIOFF);
+
+ if (tc->readMiscRegNoEffect(MISCREG_FOSEG))
+ warn_once("MISCREG_FOSEG is non-zero.\n");
+
+ xsave.ctrl64.fpu_dp = tc->readMiscRegNoEffect(MISCREG_FOOFF);
+
+ setXSave(kxsave);
+}
+
void
X86KvmCPU::updateKvmStateFPU()
{
- warn_once("X86KvmCPU::updateKvmStateFPU not implemented\n");
+ if (useXSave)
+ updateKvmStateFPUXSave();
+ else
+ updateKvmStateFPULegacy();
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev