2 * Copyright (c) 2012, 2015, 2017 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "cpu/kvm/base.hh"
40 #include <linux/kvm.h>
41 #include <sys/ioctl.h>
49 #include "arch/mmapped_ipr.hh"
50 #include "arch/utility.hh"
51 #include "debug/Checkpoint.hh"
52 #include "debug/Drain.hh"
53 #include "debug/Kvm.hh"
54 #include "debug/KvmIO.hh"
55 #include "debug/KvmRun.hh"
56 #include "params/BaseKvmCPU.hh"
57 #include "sim/process.hh"
58 #include "sim/system.hh"
60 /* Used by some KVM macros */
61 #define PAGE_SIZE pageSize
63 BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams
*params
)
65 vm(*params
->system
->getKvmVM()),
67 dataPort(name() + ".dcache_port", this),
68 instPort(name() + ".icache_port", this),
69 alwaysSyncTC(params
->alwaysSyncTC
),
70 threadContextDirty(true),
72 vcpuID(vm
.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
73 _kvmRun(NULL
), mmioRing(NULL
),
74 pageSize(sysconf(_SC_PAGE_SIZE
)),
75 tickEvent([this]{ tick(); }, "BaseKvmCPU tick",
76 false, Event::CPU_Tick_Pri
),
78 perfControlledByTimer(params
->usePerfOverflow
),
79 hostFactor(params
->hostFactor
),
83 panic("KVM: Failed to determine host page size (%i)\n",
87 thread
= new SimpleThread(this, 0, params
->system
, params
->itb
, params
->dtb
,
90 thread
= new SimpleThread(this, /* thread_num */ 0, params
->system
,
91 params
->workload
[0], params
->itb
,
92 params
->dtb
, params
->isa
[0]);
94 thread
->setStatus(ThreadContext::Halted
);
96 threadContexts
.push_back(tc
);
99 BaseKvmCPU::~BaseKvmCPU()
102 munmap(_kvmRun
, vcpuMMapSize
);
112 fatal("KVM: Multithreading not supported");
114 tc
->initMemProxies(tc
);
118 BaseKvmCPU::startup()
120 const BaseKvmCPUParams
* const p(
121 dynamic_cast<const BaseKvmCPUParams
*>(params()));
127 assert(vcpuFD
== -1);
129 // Tell the VM that a CPU is about to start.
132 // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
133 // not guaranteed that the parent KVM VM has initialized at that
134 // point. Initialize virtual CPUs here instead.
135 vcpuFD
= vm
.createVCPU(vcpuID
);
137 // Map the KVM run structure */
138 vcpuMMapSize
= kvm
.getVCPUMMapSize();
139 _kvmRun
= (struct kvm_run
*)mmap(0, vcpuMMapSize
,
140 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
142 if (_kvmRun
== MAP_FAILED
)
143 panic("KVM: Failed to map run data structure\n");
145 // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
146 // available. The offset into the KVM's communication page is
147 // provided by the coalesced MMIO capability.
148 int mmioOffset(kvm
.capCoalescedMMIO());
149 if (!p
->useCoalescedMMIO
) {
150 inform("KVM: Coalesced MMIO disabled by config.\n");
151 } else if (mmioOffset
) {
152 inform("KVM: Coalesced IO available\n");
153 mmioRing
= (struct kvm_coalesced_mmio_ring
*)(
154 (char *)_kvmRun
+ (mmioOffset
* pageSize
));
156 inform("KVM: Coalesced not supported by host OS\n");
162 new EventFunctionWrapper([this]{ startupThread(); }, name(), true));
163 schedule(startupEvent
, curTick());
167 BaseKvmCPU::KVMCpuPort::nextIOState() const
169 return (activeMMIOReqs
|| pendingMMIOPkts
.size())
170 ? RunningMMIOPending
: RunningServiceCompletion
;
174 BaseKvmCPU::KVMCpuPort::submitIO(PacketPtr pkt
)
176 if (cpu
->system
->isAtomicMode()) {
177 Tick delay
= sendAtomic(pkt
);
181 if (pendingMMIOPkts
.empty() && sendTimingReq(pkt
)) {
184 pendingMMIOPkts
.push(pkt
);
186 // Return value is irrelevant for timing-mode accesses.
192 BaseKvmCPU::KVMCpuPort::recvTimingResp(PacketPtr pkt
)
194 DPRINTF(KvmIO
, "KVM: Finished timing request\n");
199 // We can switch back into KVM when all pending and in-flight MMIO
200 // operations have completed.
201 if (!(activeMMIOReqs
|| pendingMMIOPkts
.size())) {
202 DPRINTF(KvmIO
, "KVM: Finished all outstanding timing requests\n");
203 cpu
->finishMMIOPending();
209 BaseKvmCPU::KVMCpuPort::recvReqRetry()
211 DPRINTF(KvmIO
, "KVM: Retry for timing request\n");
213 assert(pendingMMIOPkts
.size());
215 // Assuming that we can issue infinite requests this cycle is a bit
216 // unrealistic, but it's not worth modeling something more complex in
218 while (pendingMMIOPkts
.size() && sendTimingReq(pendingMMIOPkts
.front())) {
219 pendingMMIOPkts
.pop();
225 BaseKvmCPU::finishMMIOPending()
227 assert(_status
= RunningMMIOPending
);
228 assert(!tickEvent
.scheduled());
230 _status
= RunningServiceCompletion
;
231 schedule(tickEvent
, nextCycle());
235 BaseKvmCPU::startupThread()
237 // Do thread-specific initialization. We need to setup signal
238 // delivery for counters and timers from within the thread that
239 // will execute the event queue to ensure that signals are
240 // delivered to the right threads.
241 const BaseKvmCPUParams
* const p(
242 dynamic_cast<const BaseKvmCPUParams
*>(params()));
244 vcpuThread
= pthread_self();
246 // Setup signal handlers. This has to be done after the vCPU is
247 // created since it manipulates the vCPU signal mask.
248 setupSignalHandler();
252 if (p
->usePerfOverflow
)
253 runTimer
.reset(new PerfKvmTimer(hwCycles
,
258 runTimer
.reset(new PosixKvmTimer(KVM_KICK_SIGNAL
, CLOCK_MONOTONIC
,
265 BaseKvmCPU::regStats()
267 using namespace Stats
;
272 .name(name() + ".committedInsts")
273 .desc("Number of instructions committed")
277 .name(name() + ".numVMExits")
278 .desc("total number of KVM exits")
282 .name(name() + ".numVMHalfEntries")
283 .desc("number of KVM entries to finalize pending operations")
287 .name(name() + ".numExitSignal")
288 .desc("exits due to signal delivery")
292 .name(name() + ".numMMIO")
293 .desc("number of VM exits due to memory mapped IO")
297 .name(name() + ".numCoalescedMMIO")
298 .desc("number of coalesced memory mapped IO requests")
302 .name(name() + ".numIO")
303 .desc("number of VM exits due to legacy IO")
307 .name(name() + ".numHalt")
308 .desc("number of VM exits due to wait for interrupt instructions")
312 .name(name() + ".numInterrupts")
313 .desc("number of interrupts delivered")
317 .name(name() + ".numHypercalls")
318 .desc("number of hypercalls")
323 BaseKvmCPU::serializeThread(CheckpointOut
&cp
, ThreadID tid
) const
325 if (DTRACE(Checkpoint
)) {
326 DPRINTF(Checkpoint
, "KVM: Serializing thread %i:\n", tid
);
331 assert(_status
== Idle
);
332 thread
->serialize(cp
);
336 BaseKvmCPU::unserializeThread(CheckpointIn
&cp
, ThreadID tid
)
338 DPRINTF(Checkpoint
, "KVM: Unserialize thread %i:\n", tid
);
341 assert(_status
== Idle
);
342 thread
->unserialize(cp
);
343 threadContextDirty
= true;
350 return DrainState::Drained
;
352 DPRINTF(Drain
, "BaseKvmCPU::drain\n");
354 // The event queue won't be locked when calling drain since that's
355 // not done from an event. Lock the event queue here to make sure
356 // that scoped migrations continue to work if we need to
357 // synchronize the thread context.
358 std::lock_guard
<EventQueue
> lock(*this->eventQueue());
362 // The base KVM code is normally ready when it is in the
363 // Running state, but the architecture specific code might be
364 // of a different opinion. This may happen when the CPU been
365 // notified of an event that hasn't been accepted by the vCPU
367 if (!archIsDrained())
368 return DrainState::Draining
;
370 // The state of the CPU is consistent, so we don't need to do
371 // anything special to drain it. We simply de-schedule the
372 // tick event and enter the Idle state to prevent nasty things
373 // like MMIOs from happening.
374 if (tickEvent
.scheduled())
375 deschedule(tickEvent
);
380 // Idle, no need to drain
381 assert(!tickEvent
.scheduled());
383 // Sync the thread context here since we'll need it when we
384 // switch CPUs or checkpoint the CPU.
387 return DrainState::Drained
;
389 case RunningServiceCompletion
:
390 // The CPU has just requested a service that was handled in
391 // the RunningService state, but the results have still not
392 // been reported to the CPU. Now, we /could/ probably just
393 // update the register state ourselves instead of letting KVM
394 // handle it, but that would be tricky. Instead, we enter KVM
395 // and let it do its stuff.
396 DPRINTF(Drain
, "KVM CPU is waiting for service completion, "
397 "requesting drain.\n");
398 return DrainState::Draining
;
400 case RunningMMIOPending
:
401 // We need to drain since there are in-flight timing accesses
402 DPRINTF(Drain
, "KVM CPU is waiting for timing accesses to complete, "
403 "requesting drain.\n");
404 return DrainState::Draining
;
407 // We need to drain since the CPU is waiting for service (e.g., MMIOs)
408 DPRINTF(Drain
, "KVM CPU is waiting for service, requesting drain.\n");
409 return DrainState::Draining
;
412 panic("KVM: Unhandled CPU state in drain()\n");
413 return DrainState::Drained
;
418 BaseKvmCPU::drainResume()
420 assert(!tickEvent
.scheduled());
422 // We might have been switched out. In that case, we don't need to
427 DPRINTF(Kvm
, "drainResume\n");
430 // The tick event is de-scheduled as a part of the draining
431 // process. Re-schedule it if the thread context is active.
432 if (tc
->status() == ThreadContext::Active
) {
433 schedule(tickEvent
, nextCycle());
441 BaseKvmCPU::notifyFork()
443 // We should have drained prior to forking, which means that the
444 // tick event shouldn't be scheduled and the CPU is idle.
445 assert(!tickEvent
.scheduled());
446 assert(_status
== Idle
);
449 if (close(vcpuFD
) == -1)
450 warn("kvm CPU: notifyFork failed to close vcpuFD\n");
453 munmap(_kvmRun
, vcpuMMapSize
);
458 hwInstructions
.detach();
464 BaseKvmCPU::switchOut()
466 DPRINTF(Kvm
, "switchOut\n");
468 BaseCPU::switchOut();
470 // We should have drained prior to executing a switchOut, which
471 // means that the tick event shouldn't be scheduled and the CPU is
473 assert(!tickEvent
.scheduled());
474 assert(_status
== Idle
);
478 BaseKvmCPU::takeOverFrom(BaseCPU
*cpu
)
480 DPRINTF(Kvm
, "takeOverFrom\n");
482 BaseCPU::takeOverFrom(cpu
);
484 // We should have drained prior to executing a switchOut, which
485 // means that the tick event shouldn't be scheduled and the CPU is
487 assert(!tickEvent
.scheduled());
488 assert(_status
== Idle
);
489 assert(threadContexts
.size() == 1);
491 // Force an update of the KVM state here instead of flagging the
492 // TC as dirty. This is not ideal from a performance point of
493 // view, but it makes debugging easier as it allows meaningful KVM
494 // state to be dumped before and after a takeover.
496 threadContextDirty
= false;
500 BaseKvmCPU::verifyMemoryMode() const
502 if (!(system
->bypassCaches())) {
503 fatal("The KVM-based CPUs requires the memory system to be in the "
504 "'noncaching' mode.\n");
509 BaseKvmCPU::wakeup(ThreadID tid
)
511 DPRINTF(Kvm
, "wakeup()\n");
512 // This method might have been called from another
513 // context. Migrate to this SimObject's event queue when
514 // delivering the wakeup signal.
515 EventQueue::ScopedMigration
migrate(eventQueue());
517 // Kick the vCPU to get it to come out of KVM.
520 if (thread
->status() != ThreadContext::Suspended
)
527 BaseKvmCPU::activateContext(ThreadID thread_num
)
529 DPRINTF(Kvm
, "ActivateContext %d\n", thread_num
);
531 assert(thread_num
== 0);
534 assert(_status
== Idle
);
535 assert(!tickEvent
.scheduled());
537 numCycles
+= ticksToCycles(thread
->lastActivate
- thread
->lastSuspend
);
539 schedule(tickEvent
, clockEdge(Cycles(0)));
545 BaseKvmCPU::suspendContext(ThreadID thread_num
)
547 DPRINTF(Kvm
, "SuspendContext %d\n", thread_num
);
549 assert(thread_num
== 0);
555 assert(_status
== Running
|| _status
== RunningServiceCompletion
);
557 // The tick event may no be scheduled if the quest has requested
558 // the monitor to wait for interrupts. The normal CPU models can
559 // get their tick events descheduled by quiesce instructions, but
560 // that can't happen here.
561 if (tickEvent
.scheduled())
562 deschedule(tickEvent
);
568 BaseKvmCPU::deallocateContext(ThreadID thread_num
)
570 // for now, these are equivalent
571 suspendContext(thread_num
);
575 BaseKvmCPU::haltContext(ThreadID thread_num
)
577 // for now, these are equivalent
578 suspendContext(thread_num
);
579 updateCycleCounters(BaseCPU::CPU_STATE_SLEEP
);
583 BaseKvmCPU::getContext(int tn
)
592 BaseKvmCPU::totalInsts() const
598 BaseKvmCPU::totalOps() const
600 hack_once("Pretending totalOps is equivalent to totalInsts()\n");
605 BaseKvmCPU::dump() const
607 inform("State dumping not implemented.");
614 assert(_status
!= Idle
&& _status
!= RunningMMIOPending
);
618 // handleKvmExit() will determine the next state of the CPU
619 delay
= handleKvmExit();
625 case RunningServiceCompletion
:
627 auto &queue
= thread
->comInstEventQueue
;
628 const uint64_t nextInstEvent(
629 queue
.empty() ? MaxTick
: queue
.nextTick());
630 // Enter into KVM and complete pending IO instructions if we
631 // have an instruction event pending.
632 const Tick
ticksToExecute(
633 nextInstEvent
> ctrInsts
?
634 curEventQueue()->nextTick() - curTick() : 0);
637 threadContextDirty
= true;
639 // We might need to update the KVM state.
642 // Setup any pending instruction count breakpoints using
643 // PerfEvent if we are going to execute more than just an IO
645 if (ticksToExecute
> 0)
648 DPRINTF(KvmRun
, "Entering KVM...\n");
649 if (drainState() == DrainState::Draining
) {
650 // Force an immediate exit from KVM after completing
651 // pending operations. The architecture-specific code
652 // takes care to run until it is in a state where it can
653 // safely be drained.
654 delay
= kvmRunDrain();
656 delay
= kvmRun(ticksToExecute
);
659 // The CPU might have been suspended before entering into
660 // KVM. Assume that the CPU was suspended /before/ entering
661 // into KVM and skip the exit handling.
665 // Entering into KVM implies that we'll have to reload the thread
666 // context from KVM if we want to access it. Flag the KVM state as
667 // dirty with respect to the cached thread context.
668 kvmStateDirty
= true;
673 // Enter into the RunningService state unless the
674 // simulation was stopped by a timer.
675 if (_kvmRun
->exit_reason
!= KVM_EXIT_INTR
) {
676 _status
= RunningService
;
682 // Service any pending instruction events. The vCPU should
683 // have exited in time for the event using the instruction
684 // counter configured by setupInstStop().
685 queue
.serviceEvents(ctrInsts
);
692 panic("BaseKvmCPU entered tick() in an illegal state (%i)\n",
696 // Schedule a new tick if we are still running
697 if (_status
!= Idle
&& _status
!= RunningMMIOPending
)
698 schedule(tickEvent
, clockEdge(ticksToCycles(delay
)));
702 BaseKvmCPU::kvmRunDrain()
704 // By default, the only thing we need to drain is a pending IO
705 // operation which assumes that we are in the
706 // RunningServiceCompletion or RunningMMIOPending state.
707 assert(_status
== RunningServiceCompletion
||
708 _status
== RunningMMIOPending
);
710 // Deliver the data from the pending IO operation and immediately
716 BaseKvmCPU::getHostCycles() const
718 return hwCycles
.read();
722 BaseKvmCPU::kvmRun(Tick ticks
)
725 fatal_if(vcpuFD
== -1,
726 "Trying to run a KVM CPU in a forked child process. "
727 "This is not supported.\n");
728 DPRINTF(KvmRun
, "KVM: Executing for %i ticks\n", ticks
);
731 // Settings ticks == 0 is a special case which causes an entry
732 // into KVM that finishes pending operations (e.g., IO) and
733 // then immediately exits.
734 DPRINTF(KvmRun
, "KVM: Delivering IO without full guest entry\n");
738 // Send a KVM_KICK_SIGNAL to the vCPU thread (i.e., this
739 // thread). The KVM control signal is masked while executing
740 // in gem5 and gets unmasked temporarily as when entering
741 // KVM. See setSignalMask() and setupSignalHandler().
744 // Start the vCPU. KVM will check for signals after completing
745 // pending operations (IO). Since the KVM_KICK_SIGNAL is
746 // pending, this forces an immediate exit to gem5 again. We
747 // don't bother to setup timers since this shouldn't actually
748 // execute any code (other than completing half-executed IO
749 // instructions) in the guest.
752 // We always execute at least one cycle to prevent the
753 // BaseKvmCPU::tick() to be rescheduled on the same tick
755 ticksExecuted
= clockPeriod();
757 // This method is executed as a result of a tick event. That
758 // means that the event queue will be locked when entering the
759 // method. We temporarily unlock the event queue to allow
760 // other threads to steal control of this thread to inject
761 // interrupts. They will typically lock the queue and then
762 // force an exit from KVM by kicking the vCPU.
763 EventQueue::ScopedRelease
release(curEventQueue());
765 if (ticks
< runTimer
->resolution()) {
766 DPRINTF(KvmRun
, "KVM: Adjusting tick count (%i -> %i)\n",
767 ticks
, runTimer
->resolution());
768 ticks
= runTimer
->resolution();
771 // Get hardware statistics after synchronizing contexts. The KVM
772 // state update might affect guest cycle counters.
773 uint64_t baseCycles(getHostCycles());
774 uint64_t baseInstrs(hwInstructions
.read());
776 // Arm the run timer and start the cycle timer if it isn't
777 // controlled by the overflow timer. Starting/stopping the cycle
778 // timer automatically starts the other perf timers as they are in
779 // the same counter group.
780 runTimer
->arm(ticks
);
781 if (!perfControlledByTimer
)
787 if (!perfControlledByTimer
)
790 // The control signal may have been delivered after we exited
791 // from KVM. It will be pending in that case since it is
792 // masked when we aren't executing in KVM. Discard it to make
793 // sure we don't deliver it immediately next time we try to
795 discardPendingSignal(KVM_KICK_SIGNAL
);
797 const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles
);
798 const uint64_t simCyclesExecuted(hostCyclesExecuted
* hostFactor
);
799 const uint64_t instsExecuted(hwInstructions
.read() - baseInstrs
);
800 ticksExecuted
= runTimer
->ticksFromHostCycles(hostCyclesExecuted
);
802 /* Update statistics */
803 numCycles
+= simCyclesExecuted
;;
804 numInsts
+= instsExecuted
;
805 ctrInsts
+= instsExecuted
;
806 system
->totalNumInsts
+= instsExecuted
;
809 "KVM: Executed %i instructions in %i cycles "
810 "(%i ticks, sim cycles: %i).\n",
811 instsExecuted
, hostCyclesExecuted
, ticksExecuted
, simCyclesExecuted
);
816 return ticksExecuted
+ flushCoalescedMMIO();
820 BaseKvmCPU::kvmNonMaskableInterrupt()
823 if (ioctl(KVM_NMI
) == -1)
824 panic("KVM: Failed to deliver NMI to virtual CPU\n");
828 BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt
&interrupt
)
831 if (ioctl(KVM_INTERRUPT
, (void *)&interrupt
) == -1)
832 panic("KVM: Failed to deliver interrupt to virtual CPU\n");
836 BaseKvmCPU::getRegisters(struct kvm_regs
®s
) const
838 if (ioctl(KVM_GET_REGS
, ®s
) == -1)
839 panic("KVM: Failed to get guest registers\n");
843 BaseKvmCPU::setRegisters(const struct kvm_regs
®s
)
845 if (ioctl(KVM_SET_REGS
, (void *)®s
) == -1)
846 panic("KVM: Failed to set guest registers\n");
850 BaseKvmCPU::getSpecialRegisters(struct kvm_sregs
®s
) const
852 if (ioctl(KVM_GET_SREGS
, ®s
) == -1)
853 panic("KVM: Failed to get guest special registers\n");
857 BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs
®s
)
859 if (ioctl(KVM_SET_SREGS
, (void *)®s
) == -1)
860 panic("KVM: Failed to set guest special registers\n");
864 BaseKvmCPU::getFPUState(struct kvm_fpu
&state
) const
866 if (ioctl(KVM_GET_FPU
, &state
) == -1)
867 panic("KVM: Failed to get guest FPU state\n");
871 BaseKvmCPU::setFPUState(const struct kvm_fpu
&state
)
873 if (ioctl(KVM_SET_FPU
, (void *)&state
) == -1)
874 panic("KVM: Failed to set guest FPU state\n");
879 BaseKvmCPU::setOneReg(uint64_t id
, const void *addr
)
881 #ifdef KVM_SET_ONE_REG
882 struct kvm_one_reg reg
;
884 reg
.addr
= (uint64_t)addr
;
886 if (ioctl(KVM_SET_ONE_REG
, ®
) == -1) {
887 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
891 panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
896 BaseKvmCPU::getOneReg(uint64_t id
, void *addr
) const
898 #ifdef KVM_GET_ONE_REG
899 struct kvm_one_reg reg
;
901 reg
.addr
= (uint64_t)addr
;
903 if (ioctl(KVM_GET_ONE_REG
, ®
) == -1) {
904 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
908 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
913 BaseKvmCPU::getAndFormatOneReg(uint64_t id
) const
915 #ifdef KVM_GET_ONE_REG
916 std::ostringstream ss
;
918 ss
.setf(std::ios::hex
, std::ios::basefield
);
919 ss
.setf(std::ios::showbase
);
920 #define HANDLE_INTTYPE(len) \
921 case KVM_REG_SIZE_U ## len: { \
922 uint ## len ## _t value; \
923 getOneReg(id, &value); \
927 #define HANDLE_ARRAY(len) \
928 case KVM_REG_SIZE_U ## len: { \
929 uint8_t value[len / 8]; \
930 getOneReg(id, value); \
931 ccprintf(ss, "[0x%x", value[0]); \
932 for (int i = 1; i < len / 8; ++i) \
933 ccprintf(ss, ", 0x%x", value[i]); \
937 switch (id
& KVM_REG_SIZE_MASK
) {
950 #undef HANDLE_INTTYPE
955 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
960 BaseKvmCPU::syncThreadContext()
965 assert(!threadContextDirty
);
967 updateThreadContext();
968 kvmStateDirty
= false;
972 BaseKvmCPU::syncKvmState()
974 if (!threadContextDirty
)
977 assert(!kvmStateDirty
);
980 threadContextDirty
= false;
984 BaseKvmCPU::handleKvmExit()
986 DPRINTF(KvmRun
, "handleKvmExit (exit_reason: %i)\n", _kvmRun
->exit_reason
);
987 assert(_status
== RunningService
);
989 // Switch into the running state by default. Individual handlers
990 // can override this.
992 switch (_kvmRun
->exit_reason
) {
993 case KVM_EXIT_UNKNOWN
:
994 return handleKvmExitUnknown();
996 case KVM_EXIT_EXCEPTION
:
997 return handleKvmExitException();
1002 Tick ticks
= handleKvmExitIO();
1003 _status
= dataPort
.nextIOState();
1007 case KVM_EXIT_HYPERCALL
:
1009 return handleKvmExitHypercall();
1012 /* The guest has halted and is waiting for interrupts */
1013 DPRINTF(Kvm
, "handleKvmExitHalt\n");
1016 // Suspend the thread until the next interrupt arrives
1019 // This is actually ignored since the thread is suspended.
1024 /* Service memory mapped IO requests */
1025 DPRINTF(KvmIO
, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
1026 _kvmRun
->mmio
.is_write
,
1027 _kvmRun
->mmio
.phys_addr
, _kvmRun
->mmio
.len
);
1030 Tick ticks
= doMMIOAccess(_kvmRun
->mmio
.phys_addr
, _kvmRun
->mmio
.data
,
1031 _kvmRun
->mmio
.len
, _kvmRun
->mmio
.is_write
);
1032 // doMMIOAccess could have triggered a suspend, in which case we don't
1033 // want to overwrite the _status.
1034 if (_status
!= Idle
)
1035 _status
= dataPort
.nextIOState();
1039 case KVM_EXIT_IRQ_WINDOW_OPEN
:
1040 return handleKvmExitIRQWindowOpen();
1042 case KVM_EXIT_FAIL_ENTRY
:
1043 return handleKvmExitFailEntry();
1046 /* KVM was interrupted by a signal, restart it in the next
1050 case KVM_EXIT_INTERNAL_ERROR
:
1051 panic("KVM: Internal error (suberror: %u)\n",
1052 _kvmRun
->internal
.suberror
);
1056 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun
->exit_reason
);
1061 BaseKvmCPU::handleKvmExitIO()
1063 panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
1064 _kvmRun
->io
.direction
, _kvmRun
->io
.size
,
1065 _kvmRun
->io
.port
, _kvmRun
->io
.count
);
1069 BaseKvmCPU::handleKvmExitHypercall()
1071 panic("KVM: Unhandled hypercall\n");
1075 BaseKvmCPU::handleKvmExitIRQWindowOpen()
1077 warn("KVM: Unhandled IRQ window.\n");
1083 BaseKvmCPU::handleKvmExitUnknown()
1086 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
1087 _kvmRun
->hw
.hardware_exit_reason
);
1091 BaseKvmCPU::handleKvmExitException()
1094 panic("KVM: Got exception when starting vCPU "
1095 "(exception: %u, error_code: %u)\n",
1096 _kvmRun
->ex
.exception
, _kvmRun
->ex
.error_code
);
1100 BaseKvmCPU::handleKvmExitFailEntry()
1103 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
1104 _kvmRun
->fail_entry
.hardware_entry_failure_reason
);
1108 BaseKvmCPU::doMMIOAccess(Addr paddr
, void *data
, int size
, bool write
)
1110 ThreadContext
*tc(thread
->getTC());
1111 syncThreadContext();
1113 RequestPtr mmio_req
= std::make_shared
<Request
>(
1114 paddr
, size
, Request::UNCACHEABLE
, dataMasterId());
1116 mmio_req
->setContext(tc
->contextId());
1117 // Some architectures do need to massage physical addresses a bit
1118 // before they are inserted into the memory system. This enables
1119 // APIC accesses on x86 and m5ops where supported through a MMIO
1121 BaseTLB::Mode
tlb_mode(write
? BaseTLB::Write
: BaseTLB::Read
);
1122 Fault
fault(tc
->getDTBPtr()->finalizePhysical(mmio_req
, tc
, tlb_mode
));
1123 if (fault
!= NoFault
)
1124 warn("Finalization of MMIO address failed: %s\n", fault
->name());
1127 const MemCmd
cmd(write
? MemCmd::WriteReq
: MemCmd::ReadReq
);
1128 PacketPtr pkt
= new Packet(mmio_req
, cmd
);
1129 pkt
->dataStatic(data
);
1131 if (mmio_req
->isMmappedIpr()) {
1132 // We currently assume that there is no need to migrate to a
1133 // different event queue when doing IPRs. Currently, IPRs are
1134 // only used for m5ops, so it should be a valid assumption.
1135 const Cycles
ipr_delay(write
?
1136 TheISA::handleIprWrite(tc
, pkt
) :
1137 TheISA::handleIprRead(tc
, pkt
));
1138 threadContextDirty
= true;
1140 return clockPeriod() * ipr_delay
;
1142 // Temporarily lock and migrate to the device event queue to
1143 // prevent races in multi-core mode.
1144 EventQueue::ScopedMigration
migrate(deviceEventQueue());
1146 return dataPort
.submitIO(pkt
);
1151 BaseKvmCPU::setSignalMask(const sigset_t
*mask
)
1153 std::unique_ptr
<struct kvm_signal_mask
> kvm_mask
;
1156 kvm_mask
.reset((struct kvm_signal_mask
*)operator new(
1157 sizeof(struct kvm_signal_mask
) + sizeof(*mask
)));
1158 // The kernel and the user-space headers have different ideas
1159 // about the size of sigset_t. This seems like a massive hack,
1160 // but is actually what qemu does.
1161 assert(sizeof(*mask
) >= 8);
1163 memcpy(kvm_mask
->sigset
, mask
, kvm_mask
->len
);
1166 if (ioctl(KVM_SET_SIGNAL_MASK
, (void *)kvm_mask
.get()) == -1)
1167 panic("KVM: Failed to set vCPU signal mask (errno: %i)\n",
1172 BaseKvmCPU::ioctl(int request
, long p1
) const
1175 panic("KVM: CPU ioctl called before initialization\n");
1177 return ::ioctl(vcpuFD
, request
, p1
);
1181 BaseKvmCPU::flushCoalescedMMIO()
1186 DPRINTF(KvmIO
, "KVM: Flushing the coalesced MMIO ring buffer\n");
1188 // TODO: We might need to do synchronization when we start to
1189 // support multiple CPUs
1191 while (mmioRing
->first
!= mmioRing
->last
) {
1192 struct kvm_coalesced_mmio
&ent(
1193 mmioRing
->coalesced_mmio
[mmioRing
->first
]);
1195 DPRINTF(KvmIO
, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
1196 ent
.phys_addr
, ent
.len
);
1199 ticks
+= doMMIOAccess(ent
.phys_addr
, ent
.data
, ent
.len
, true);
1201 mmioRing
->first
= (mmioRing
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1208 * Dummy handler for KVM kick signals.
1210 * @note This function is usually not called since the kernel doesn't
1211 * seem to deliver signals when the signal is only unmasked when
1212 * running in KVM. This doesn't matter though since we are only
1213 * interested in getting KVM to exit, which happens as expected. See
1214 * setupSignalHandler() and kvmRun() for details about KVM signal
1218 onKickSignal(int signo
, siginfo_t
*si
, void *data
)
1223 BaseKvmCPU::setupSignalHandler()
1225 struct sigaction sa
;
1227 memset(&sa
, 0, sizeof(sa
));
1228 sa
.sa_sigaction
= onKickSignal
;
1229 sa
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1230 if (sigaction(KVM_KICK_SIGNAL
, &sa
, NULL
) == -1)
1231 panic("KVM: Failed to setup vCPU timer signal handler\n");
1234 if (pthread_sigmask(SIG_BLOCK
, NULL
, &sigset
) == -1)
1235 panic("KVM: Failed get signal mask\n");
1237 // Request KVM to setup the same signal mask as we're currently
1238 // running with except for the KVM control signal. We'll sometimes
1239 // need to raise the KVM_KICK_SIGNAL to cause immediate exits from
1240 // KVM after servicing IO requests. See kvmRun().
1241 sigdelset(&sigset
, KVM_KICK_SIGNAL
);
1242 setSignalMask(&sigset
);
1244 // Mask our control signals so they aren't delivered unless we're
1245 // actually executing inside KVM.
1246 sigaddset(&sigset
, KVM_KICK_SIGNAL
);
1247 if (pthread_sigmask(SIG_SETMASK
, &sigset
, NULL
) == -1)
1248 panic("KVM: Failed mask the KVM control signals\n");
1252 BaseKvmCPU::discardPendingSignal(int signum
) const
1254 int discardedSignal
;
1256 // Setting the timeout to zero causes sigtimedwait to return
1258 struct timespec timeout
;
1260 timeout
.tv_nsec
= 0;
1263 sigemptyset(&sigset
);
1264 sigaddset(&sigset
, signum
);
1267 discardedSignal
= sigtimedwait(&sigset
, NULL
, &timeout
);
1268 } while (discardedSignal
== -1 && errno
== EINTR
);
1270 if (discardedSignal
== signum
)
1272 else if (discardedSignal
== -1 && errno
== EAGAIN
)
1275 panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n",
1276 discardedSignal
, errno
);
1280 BaseKvmCPU::setupCounters()
1282 DPRINTF(Kvm
, "Attaching cycle counter...\n");
1283 PerfKvmCounterConfig
cfgCycles(PERF_TYPE_HARDWARE
,
1284 PERF_COUNT_HW_CPU_CYCLES
);
1285 cfgCycles
.disabled(true)
1288 // Try to exclude the host. We set both exclude_hv and
1289 // exclude_host since different architectures use slightly
1290 // different APIs in the kernel.
1291 cfgCycles
.exclude_hv(true)
1292 .exclude_host(true);
1294 if (perfControlledByTimer
) {
1295 // We need to configure the cycles counter to send overflows
1296 // since we are going to use it to trigger timer signals that
1297 // trap back into m5 from KVM. In practice, this means that we
1298 // need to set some non-zero sample period that gets
1299 // overridden when the timer is armed.
1300 cfgCycles
.wakeupEvents(1)
1304 hwCycles
.attach(cfgCycles
,
1305 0); // TID (0 => currentThread)
1311 BaseKvmCPU::tryDrain()
1313 if (drainState() != DrainState::Draining
)
1316 if (!archIsDrained()) {
1317 DPRINTF(Drain
, "tryDrain: Architecture code is not ready.\n");
1321 if (_status
== Idle
|| _status
== Running
) {
1323 "tryDrain: CPU transitioned into the Idle state, drain done\n");
1327 DPRINTF(Drain
, "tryDrain: CPU not ready.\n");
1333 BaseKvmCPU::ioctlRun()
1335 if (ioctl(KVM_RUN
) == -1) {
1337 panic("KVM: Failed to start virtual CPU (errno: %i)\n",
1343 BaseKvmCPU::setupInstStop()
1345 if (thread
->comInstEventQueue
.empty()) {
1346 setupInstCounter(0);
1348 Tick next
= thread
->comInstEventQueue
.nextTick();
1349 assert(next
> ctrInsts
);
1350 setupInstCounter(next
- ctrInsts
);
1355 BaseKvmCPU::setupInstCounter(uint64_t period
)
1357 // No need to do anything if we aren't attaching for the first
1358 // time or the period isn't changing.
1359 if (period
== activeInstPeriod
&& hwInstructions
.attached())
1362 PerfKvmCounterConfig
cfgInstructions(PERF_TYPE_HARDWARE
,
1363 PERF_COUNT_HW_INSTRUCTIONS
);
1365 // Try to exclude the host. We set both exclude_hv and
1366 // exclude_host since different architectures use slightly
1367 // different APIs in the kernel.
1368 cfgInstructions
.exclude_hv(true)
1369 .exclude_host(true);
1372 // Setup a sampling counter if that has been requested.
1373 cfgInstructions
.wakeupEvents(1)
1374 .samplePeriod(period
);
1377 // We need to detach and re-attach the counter to reliably change
1378 // sampling settings. See PerfKvmCounter::period() for details.
1379 if (hwInstructions
.attached())
1380 hwInstructions
.detach();
1381 assert(hwCycles
.attached());
1382 hwInstructions
.attach(cfgInstructions
,
1383 0, // TID (0 => currentThread)
1387 hwInstructions
.enableSignals(KVM_KICK_SIGNAL
);
1389 activeInstPeriod
= period
;