_status(Idle),
dataPort(name() + ".dcache_port", this),
instPort(name() + ".icache_port", this),
- contextDirty(true),
+ threadContextDirty(true),
+ kvmStateDirty(false),
vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
_kvmRun(NULL), mmioRing(NULL),
pageSize(sysconf(_SC_PAGE_SIZE)),
void
BaseKvmCPU::serializeThread(std::ostream &os, ThreadID tid)
{
+ // Update the thread context so we have something to serialize.
+ syncThreadContext();
+
assert(tid == 0);
assert(_status == Idle);
thread->serialize(os);
assert(tid == 0);
assert(_status == Idle);
thread->unserialize(cp, section);
- contextDirty = true;
+ threadContextDirty = true;
}
unsigned int
void
BaseKvmCPU::switchOut()
{
- BaseCPU::switchOut();
-
DPRINTF(Kvm, "switchOut\n");
+ // Make sure to update the thread context in case, the new CPU
+ // will need to access it.
+ syncThreadContext();
+
+ BaseCPU::switchOut();
+
// We should have drained prior to executing a switchOut, which
// means that the tick event shouldn't be scheduled and the CPU is
// idle.
assert(_status == Idle);
assert(threadContexts.size() == 1);
- // Force a gem5 -> KVM context synchronization
- contextDirty = true;
+ // The BaseCPU updated the thread context, make sure that we
+ // synchronize next time we enter start the CPU.
+ threadContextDirty = true;
}
void
suspendContext(thread_num);
}
+ThreadContext *
+BaseKvmCPU::getContext(int tn)
+{
+ assert(tn == 0);
+ syncThreadContext();
+ return tc;
+}
+
+
Counter
BaseKvmCPU::totalInsts() const
{
DPRINTF(KvmRun, "Entering KVM...\n");
- if (contextDirty) {
- contextDirty = false;
- updateKvmState();
- }
-
Tick ticksToExecute(mainEventQueue.nextTick() - curTick());
Tick ticksExecuted(kvmRun(ticksToExecute));
- updateThreadContext();
Tick delay(ticksExecuted + handleKvmExit());
uint64_t baseCycles(hwCycles.read());
uint64_t baseInstrs(hwInstructions.read());
+ // We might need to update the KVM state.
+ syncKvmState();
+ // Entering into KVM implies that we'll have to reload the thread
+ // context from KVM if we want to access it. Flag the KVM state as
+ // dirty with respect to the cached thread context.
+ kvmStateDirty = true;
+
if (ticks < runTimer->resolution()) {
DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
ticks, runTimer->resolution());
#endif
}
+void
+BaseKvmCPU::syncThreadContext()
+{
+ if (!kvmStateDirty)
+ return;
+
+ assert(!threadContextDirty);
+
+ updateThreadContext();
+ kvmStateDirty = false;
+}
+
+void
+BaseKvmCPU::syncKvmState()
+{
+ if (!threadContextDirty)
+ return;
+
+ assert(!kvmStateDirty);
+
+ updateKvmState();
+ threadContextDirty = false;
+}
+
Tick
BaseKvmCPU::handleKvmExit()
{
void deallocateContext(ThreadID thread_num);
void haltContext(ThreadID thread_num);
+ ThreadContext *getContext(int tn);
+
Counter totalInsts() const;
Counter totalOps() const;
/** Dump the internal state to the terminal. */
virtual void dump();
- /** SimpleThread object, provides all the architectural state. */
+ /**
+ * A cached copy of a thread's state in the form of a SimpleThread
+ * object.
+ *
+ * Normally the actual thread state is stored in the KVM vCPU. If KVM has
+ * been running this copy is will be out of date. If we recently handled
+ * some events within gem5 that required state to be updated this could be
+ * the most up-to-date copy. When getContext() or updateThreadContext() is
+ * called this copy gets updated. The method syncThreadContext can
+ * be used within a KVM CPU to update the thread context if the
+ * KVM state is dirty (i.e., the vCPU has been run since the last
+ * update).
+ */
SimpleThread *thread;
/** ThreadContext object, provides an interface for external
* and update gem5's thread state.
*/
virtual void updateThreadContext() = 0;
+
+ /**
+ * Update a thread context if the KVM state is dirty with respect
+ * to the cached thread context.
+ */
+ void syncThreadContext();
+
+ /**
+ * Update the KVM if the thread context is dirty.
+ */
+ void syncKvmState();
/** @} */
/** @{ */
* Is the gem5 context dirty? Set to true to force an update of
* the KVM vCPU state upon the next call to kvmRun().
*/
- bool contextDirty;
+ bool threadContextDirty;
+
+ /**
+ * Is the KVM state dirty? Set to true to force an update of
+ * the KVM vCPU state upon the next call to kvmRun().
+ */
+ bool kvmStateDirty;
/** KVM internal ID of the vCPU */
const long vcpuID;