2 * Copyright (c) 2012 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.
37 * Authors: Andreas Sandberg
40 #include <linux/kvm.h>
41 #include <sys/ioctl.h>
49 #include "arch/mmapped_ipr.hh"
50 #include "arch/utility.hh"
51 #include "cpu/kvm/base.hh"
52 #include "debug/Checkpoint.hh"
53 #include "debug/Drain.hh"
54 #include "debug/Kvm.hh"
55 #include "debug/KvmIO.hh"
56 #include "debug/KvmRun.hh"
57 #include "params/BaseKvmCPU.hh"
58 #include "sim/process.hh"
59 #include "sim/system.hh"
63 /* Used by some KVM macros */
64 #define PAGE_SIZE pageSize
66 BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams
*params
)
70 dataPort(name() + ".dcache_port", this),
71 instPort(name() + ".icache_port", this),
72 threadContextDirty(true),
74 vcpuID(vm
.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
75 _kvmRun(NULL
), mmioRing(NULL
),
76 pageSize(sysconf(_SC_PAGE_SIZE
)),
79 perfControlledByTimer(params
->usePerfOverflow
),
80 hostFactor(params
->hostFactor
),
85 panic("KVM: Failed to determine host page size (%i)\n",
89 thread
= new SimpleThread(this, 0, params
->system
, params
->itb
, params
->dtb
,
92 thread
= new SimpleThread(this, /* thread_num */ 0, params
->system
,
93 params
->workload
[0], params
->itb
,
94 params
->dtb
, params
->isa
[0]);
96 thread
->setStatus(ThreadContext::Halted
);
98 threadContexts
.push_back(tc
);
101 BaseKvmCPU::~BaseKvmCPU()
104 munmap(_kvmRun
, vcpuMMapSize
);
114 fatal("KVM: Multithreading not supported");
116 tc
->initMemProxies(tc
);
118 // initialize CPU, including PC
119 if (FullSystem
&& !switchedOut())
120 TheISA::initCPU(tc
, tc
->contextId());
122 mmio_req
.setThreadContext(tc
->contextId(), 0);
126 BaseKvmCPU::startup()
128 const BaseKvmCPUParams
* const p(
129 dynamic_cast<const BaseKvmCPUParams
*>(params()));
135 assert(vcpuFD
== -1);
137 // Tell the VM that a CPU is about to start.
140 // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
141 // not guaranteed that the parent KVM VM has initialized at that
142 // point. Initialize virtual CPUs here instead.
143 vcpuFD
= vm
.createVCPU(vcpuID
);
145 // Map the KVM run structure */
146 vcpuMMapSize
= kvm
.getVCPUMMapSize();
147 _kvmRun
= (struct kvm_run
*)mmap(0, vcpuMMapSize
,
148 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
150 if (_kvmRun
== MAP_FAILED
)
151 panic("KVM: Failed to map run data structure\n");
153 // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
154 // available. The offset into the KVM's communication page is
155 // provided by the coalesced MMIO capability.
156 int mmioOffset(kvm
.capCoalescedMMIO());
157 if (!p
->useCoalescedMMIO
) {
158 inform("KVM: Coalesced MMIO disabled by config.\n");
159 } else if (mmioOffset
) {
160 inform("KVM: Coalesced IO available\n");
161 mmioRing
= (struct kvm_coalesced_mmio_ring
*)(
162 (char *)_kvmRun
+ (mmioOffset
* pageSize
));
164 inform("KVM: Coalesced not supported by host OS\n");
170 new EventWrapper
<BaseKvmCPU
,
171 &BaseKvmCPU::startupThread
>(this, true));
172 schedule(startupEvent
, curTick());
176 BaseKvmCPU::startupThread()
178 // Do thread-specific initialization. We need to setup signal
179 // delivery for counters and timers from within the thread that
180 // will execute the event queue to ensure that signals are
181 // delivered to the right threads.
182 const BaseKvmCPUParams
* const p(
183 dynamic_cast<const BaseKvmCPUParams
*>(params()));
185 vcpuThread
= pthread_self();
187 // Setup signal handlers. This has to be done after the vCPU is
188 // created since it manipulates the vCPU signal mask.
189 setupSignalHandler();
193 if (p
->usePerfOverflow
)
194 runTimer
.reset(new PerfKvmTimer(hwCycles
,
199 runTimer
.reset(new PosixKvmTimer(KVM_KICK_SIGNAL
, CLOCK_MONOTONIC
,
206 BaseKvmCPU::regStats()
208 using namespace Stats
;
213 .name(name() + ".committedInsts")
214 .desc("Number of instructions committed")
218 .name(name() + ".numVMExits")
219 .desc("total number of KVM exits")
223 .name(name() + ".numVMHalfEntries")
224 .desc("number of KVM entries to finalize pending operations")
228 .name(name() + ".numExitSignal")
229 .desc("exits due to signal delivery")
233 .name(name() + ".numMMIO")
234 .desc("number of VM exits due to memory mapped IO")
238 .name(name() + ".numCoalescedMMIO")
239 .desc("number of coalesced memory mapped IO requests")
243 .name(name() + ".numIO")
244 .desc("number of VM exits due to legacy IO")
248 .name(name() + ".numHalt")
249 .desc("number of VM exits due to wait for interrupt instructions")
253 .name(name() + ".numInterrupts")
254 .desc("number of interrupts delivered")
258 .name(name() + ".numHypercalls")
259 .desc("number of hypercalls")
264 BaseKvmCPU::serializeThread(std::ostream
&os
, ThreadID tid
)
266 if (DTRACE(Checkpoint
)) {
267 DPRINTF(Checkpoint
, "KVM: Serializing thread %i:\n", tid
);
272 assert(_status
== Idle
);
273 thread
->serialize(os
);
277 BaseKvmCPU::unserializeThread(Checkpoint
*cp
, const std::string
§ion
,
280 DPRINTF(Checkpoint
, "KVM: Unserialize thread %i:\n", tid
);
283 assert(_status
== Idle
);
284 thread
->unserialize(cp
, section
);
285 threadContextDirty
= true;
289 BaseKvmCPU::drain(DrainManager
*dm
)
294 DPRINTF(Drain
, "BaseKvmCPU::drain\n");
297 // The base KVM code is normally ready when it is in the
298 // Running state, but the architecture specific code might be
299 // of a different opinion. This may happen when the CPU been
300 // notified of an event that hasn't been accepted by the vCPU
302 if (!archIsDrained()) {
307 // The state of the CPU is consistent, so we don't need to do
308 // anything special to drain it. We simply de-schedule the
309 // tick event and enter the Idle state to prevent nasty things
310 // like MMIOs from happening.
311 if (tickEvent
.scheduled())
312 deschedule(tickEvent
);
317 // Idle, no need to drain
318 assert(!tickEvent
.scheduled());
320 // Sync the thread context here since we'll need it when we
321 // switch CPUs or checkpoint the CPU.
326 case RunningServiceCompletion
:
327 // The CPU has just requested a service that was handled in
328 // the RunningService state, but the results have still not
329 // been reported to the CPU. Now, we /could/ probably just
330 // update the register state ourselves instead of letting KVM
331 // handle it, but that would be tricky. Instead, we enter KVM
332 // and let it do its stuff.
335 DPRINTF(Drain
, "KVM CPU is waiting for service completion, "
336 "requesting drain.\n");
340 // We need to drain since the CPU is waiting for service (e.g., MMIOs)
343 DPRINTF(Drain
, "KVM CPU is waiting for service, requesting drain.\n");
347 panic("KVM: Unhandled CPU state in drain()\n");
353 BaseKvmCPU::drainResume()
355 assert(!tickEvent
.scheduled());
357 // We might have been switched out. In that case, we don't need to
362 DPRINTF(Kvm
, "drainResume\n");
365 // The tick event is de-scheduled as a part of the draining
366 // process. Re-schedule it if the thread context is active.
367 if (tc
->status() == ThreadContext::Active
) {
368 schedule(tickEvent
, nextCycle());
376 BaseKvmCPU::switchOut()
378 DPRINTF(Kvm
, "switchOut\n");
380 BaseCPU::switchOut();
382 // We should have drained prior to executing a switchOut, which
383 // means that the tick event shouldn't be scheduled and the CPU is
385 assert(!tickEvent
.scheduled());
386 assert(_status
== Idle
);
390 BaseKvmCPU::takeOverFrom(BaseCPU
*cpu
)
392 DPRINTF(Kvm
, "takeOverFrom\n");
394 BaseCPU::takeOverFrom(cpu
);
396 // We should have drained prior to executing a switchOut, which
397 // means that the tick event shouldn't be scheduled and the CPU is
399 assert(!tickEvent
.scheduled());
400 assert(_status
== Idle
);
401 assert(threadContexts
.size() == 1);
403 // Force an update of the KVM state here instead of flagging the
404 // TC as dirty. This is not ideal from a performance point of
405 // view, but it makes debugging easier as it allows meaningful KVM
406 // state to be dumped before and after a takeover.
408 threadContextDirty
= false;
412 BaseKvmCPU::verifyMemoryMode() const
414 if (!(system
->isAtomicMode() && system
->bypassCaches())) {
415 fatal("The KVM-based CPUs requires the memory system to be in the "
416 "'atomic_noncaching' mode.\n");
423 DPRINTF(Kvm
, "wakeup()\n");
424 // This method might have been called from another
425 // context. Migrate to this SimObject's event queue when
426 // delivering the wakeup signal.
427 EventQueue::ScopedMigration
migrate(eventQueue());
429 // Kick the vCPU to get it to come out of KVM.
432 if (thread
->status() != ThreadContext::Suspended
)
439 BaseKvmCPU::activateContext(ThreadID thread_num
)
441 DPRINTF(Kvm
, "ActivateContext %d\n", thread_num
);
443 assert(thread_num
== 0);
446 assert(_status
== Idle
);
447 assert(!tickEvent
.scheduled());
449 numCycles
+= ticksToCycles(thread
->lastActivate
- thread
->lastSuspend
);
451 schedule(tickEvent
, clockEdge(Cycles(0)));
457 BaseKvmCPU::suspendContext(ThreadID thread_num
)
459 DPRINTF(Kvm
, "SuspendContext %d\n", thread_num
);
461 assert(thread_num
== 0);
467 assert(_status
== Running
|| _status
== RunningServiceCompletion
);
469 // The tick event may no be scheduled if the quest has requested
470 // the monitor to wait for interrupts. The normal CPU models can
471 // get their tick events descheduled by quiesce instructions, but
472 // that can't happen here.
473 if (tickEvent
.scheduled())
474 deschedule(tickEvent
);
480 BaseKvmCPU::deallocateContext(ThreadID thread_num
)
482 // for now, these are equivalent
483 suspendContext(thread_num
);
487 BaseKvmCPU::haltContext(ThreadID thread_num
)
489 // for now, these are equivalent
490 suspendContext(thread_num
);
494 BaseKvmCPU::getContext(int tn
)
503 BaseKvmCPU::totalInsts() const
509 BaseKvmCPU::totalOps() const
511 hack_once("Pretending totalOps is equivalent to totalInsts()\n");
518 inform("State dumping not implemented.");
525 assert(_status
!= Idle
);
529 // handleKvmExit() will determine the next state of the CPU
530 delay
= handleKvmExit();
536 case RunningServiceCompletion
:
538 EventQueue
*q
= curEventQueue();
539 Tick
ticksToExecute(q
->nextTick() - curTick());
541 // We might need to update the KVM state.
544 // Setup any pending instruction count breakpoints using
548 DPRINTF(KvmRun
, "Entering KVM...\n");
550 // Force an immediate exit from KVM after completing
551 // pending operations. The architecture-specific code
552 // takes care to run until it is in a state where it can
553 // safely be drained.
554 delay
= kvmRunDrain();
556 delay
= kvmRun(ticksToExecute
);
559 // The CPU might have been suspended before entering into
560 // KVM. Assume that the CPU was suspended /before/ entering
561 // into KVM and skip the exit handling.
565 // Entering into KVM implies that we'll have to reload the thread
566 // context from KVM if we want to access it. Flag the KVM state as
567 // dirty with respect to the cached thread context.
568 kvmStateDirty
= true;
570 // Enter into the RunningService state unless the
571 // simulation was stopped by a timer.
572 if (_kvmRun
->exit_reason
!= KVM_EXIT_INTR
) {
573 _status
= RunningService
;
579 // Service any pending instruction events. The vCPU should
580 // have exited in time for the event using the instruction
581 // counter configured by setupInstStop().
582 comInstEventQueue
[0]->serviceEvents(ctrInsts
);
583 system
->instEventQueue
.serviceEvents(system
->totalNumInsts
);
590 panic("BaseKvmCPU entered tick() in an illegal state (%i)\n",
594 // Schedule a new tick if we are still running
596 schedule(tickEvent
, clockEdge(ticksToCycles(delay
)));
600 BaseKvmCPU::kvmRunDrain()
602 // By default, the only thing we need to drain is a pending IO
603 // operation which assumes that we are in the
604 // RunningServiceCompletion state.
605 assert(_status
== RunningServiceCompletion
);
607 // Deliver the data from the pending IO operation and immediately
613 BaseKvmCPU::getHostCycles() const
615 return hwCycles
.read();
619 BaseKvmCPU::kvmRun(Tick ticks
)
622 DPRINTF(KvmRun
, "KVM: Executing for %i ticks\n", ticks
);
625 // Settings ticks == 0 is a special case which causes an entry
626 // into KVM that finishes pending operations (e.g., IO) and
627 // then immediately exits.
628 DPRINTF(KvmRun
, "KVM: Delivering IO without full guest entry\n");
632 // Send a KVM_KICK_SIGNAL to the vCPU thread (i.e., this
633 // thread). The KVM control signal is masked while executing
634 // in gem5 and gets unmasked temporarily as when entering
635 // KVM. See setSignalMask() and setupSignalHandler().
638 // Start the vCPU. KVM will check for signals after completing
639 // pending operations (IO). Since the KVM_KICK_SIGNAL is
640 // pending, this forces an immediate exit to gem5 again. We
641 // don't bother to setup timers since this shouldn't actually
642 // execute any code (other than completing half-executed IO
643 // instructions) in the guest.
646 // We always execute at least one cycle to prevent the
647 // BaseKvmCPU::tick() to be rescheduled on the same tick
649 ticksExecuted
= clockPeriod();
651 // This method is executed as a result of a tick event. That
652 // means that the event queue will be locked when entering the
653 // method. We temporarily unlock the event queue to allow
654 // other threads to steal control of this thread to inject
655 // interrupts. They will typically lock the queue and then
656 // force an exit from KVM by kicking the vCPU.
657 EventQueue::ScopedRelease
release(curEventQueue());
659 if (ticks
< runTimer
->resolution()) {
660 DPRINTF(KvmRun
, "KVM: Adjusting tick count (%i -> %i)\n",
661 ticks
, runTimer
->resolution());
662 ticks
= runTimer
->resolution();
665 // Get hardware statistics after synchronizing contexts. The KVM
666 // state update might affect guest cycle counters.
667 uint64_t baseCycles(getHostCycles());
668 uint64_t baseInstrs(hwInstructions
.read());
670 // Arm the run timer and start the cycle timer if it isn't
671 // controlled by the overflow timer. Starting/stopping the cycle
672 // timer automatically starts the other perf timers as they are in
673 // the same counter group.
674 runTimer
->arm(ticks
);
675 if (!perfControlledByTimer
)
681 if (!perfControlledByTimer
)
684 // The control signal may have been delivered after we exited
685 // from KVM. It will be pending in that case since it is
686 // masked when we aren't executing in KVM. Discard it to make
687 // sure we don't deliver it immediately next time we try to
689 discardPendingSignal(KVM_KICK_SIGNAL
);
691 const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles
);
692 const uint64_t simCyclesExecuted(hostCyclesExecuted
* hostFactor
);
693 const uint64_t instsExecuted(hwInstructions
.read() - baseInstrs
);
694 ticksExecuted
= runTimer
->ticksFromHostCycles(hostCyclesExecuted
);
696 /* Update statistics */
697 numCycles
+= simCyclesExecuted
;;
698 numInsts
+= instsExecuted
;
699 ctrInsts
+= instsExecuted
;
700 system
->totalNumInsts
+= instsExecuted
;
703 "KVM: Executed %i instructions in %i cycles "
704 "(%i ticks, sim cycles: %i).\n",
705 instsExecuted
, hostCyclesExecuted
, ticksExecuted
, simCyclesExecuted
);
710 return ticksExecuted
+ flushCoalescedMMIO();
714 BaseKvmCPU::kvmNonMaskableInterrupt()
717 if (ioctl(KVM_NMI
) == -1)
718 panic("KVM: Failed to deliver NMI to virtual CPU\n");
722 BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt
&interrupt
)
725 if (ioctl(KVM_INTERRUPT
, (void *)&interrupt
) == -1)
726 panic("KVM: Failed to deliver interrupt to virtual CPU\n");
730 BaseKvmCPU::getRegisters(struct kvm_regs
®s
) const
732 if (ioctl(KVM_GET_REGS
, ®s
) == -1)
733 panic("KVM: Failed to get guest registers\n");
737 BaseKvmCPU::setRegisters(const struct kvm_regs
®s
)
739 if (ioctl(KVM_SET_REGS
, (void *)®s
) == -1)
740 panic("KVM: Failed to set guest registers\n");
744 BaseKvmCPU::getSpecialRegisters(struct kvm_sregs
®s
) const
746 if (ioctl(KVM_GET_SREGS
, ®s
) == -1)
747 panic("KVM: Failed to get guest special registers\n");
751 BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs
®s
)
753 if (ioctl(KVM_SET_SREGS
, (void *)®s
) == -1)
754 panic("KVM: Failed to set guest special registers\n");
758 BaseKvmCPU::getFPUState(struct kvm_fpu
&state
) const
760 if (ioctl(KVM_GET_FPU
, &state
) == -1)
761 panic("KVM: Failed to get guest FPU state\n");
765 BaseKvmCPU::setFPUState(const struct kvm_fpu
&state
)
767 if (ioctl(KVM_SET_FPU
, (void *)&state
) == -1)
768 panic("KVM: Failed to set guest FPU state\n");
773 BaseKvmCPU::setOneReg(uint64_t id
, const void *addr
)
775 #ifdef KVM_SET_ONE_REG
776 struct kvm_one_reg reg
;
778 reg
.addr
= (uint64_t)addr
;
780 if (ioctl(KVM_SET_ONE_REG
, ®
) == -1) {
781 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
785 panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
790 BaseKvmCPU::getOneReg(uint64_t id
, void *addr
) const
792 #ifdef KVM_GET_ONE_REG
793 struct kvm_one_reg reg
;
795 reg
.addr
= (uint64_t)addr
;
797 if (ioctl(KVM_GET_ONE_REG
, ®
) == -1) {
798 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
802 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
807 BaseKvmCPU::getAndFormatOneReg(uint64_t id
) const
809 #ifdef KVM_GET_ONE_REG
810 std::ostringstream ss
;
812 ss
.setf(std::ios::hex
, std::ios::basefield
);
813 ss
.setf(std::ios::showbase
);
814 #define HANDLE_INTTYPE(len) \
815 case KVM_REG_SIZE_U ## len: { \
816 uint ## len ## _t value; \
817 getOneReg(id, &value); \
821 #define HANDLE_ARRAY(len) \
822 case KVM_REG_SIZE_U ## len: { \
823 uint8_t value[len / 8]; \
824 getOneReg(id, value); \
825 ss << "[" << value[0]; \
826 for (int i = 1; i < len / 8; ++i) \
827 ss << ", " << value[i]; \
831 switch (id
& KVM_REG_SIZE_MASK
) {
844 #undef HANDLE_INTTYPE
849 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
854 BaseKvmCPU::syncThreadContext()
859 assert(!threadContextDirty
);
861 updateThreadContext();
862 kvmStateDirty
= false;
866 BaseKvmCPU::syncKvmState()
868 if (!threadContextDirty
)
871 assert(!kvmStateDirty
);
874 threadContextDirty
= false;
878 BaseKvmCPU::handleKvmExit()
880 DPRINTF(KvmRun
, "handleKvmExit (exit_reason: %i)\n", _kvmRun
->exit_reason
);
881 assert(_status
== RunningService
);
883 // Switch into the running state by default. Individual handlers
884 // can override this.
886 switch (_kvmRun
->exit_reason
) {
887 case KVM_EXIT_UNKNOWN
:
888 return handleKvmExitUnknown();
890 case KVM_EXIT_EXCEPTION
:
891 return handleKvmExitException();
894 _status
= RunningServiceCompletion
;
896 return handleKvmExitIO();
898 case KVM_EXIT_HYPERCALL
:
900 return handleKvmExitHypercall();
903 /* The guest has halted and is waiting for interrupts */
904 DPRINTF(Kvm
, "handleKvmExitHalt\n");
907 // Suspend the thread until the next interrupt arrives
910 // This is actually ignored since the thread is suspended.
914 _status
= RunningServiceCompletion
;
915 /* Service memory mapped IO requests */
916 DPRINTF(KvmIO
, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
917 _kvmRun
->mmio
.is_write
,
918 _kvmRun
->mmio
.phys_addr
, _kvmRun
->mmio
.len
);
921 return doMMIOAccess(_kvmRun
->mmio
.phys_addr
, _kvmRun
->mmio
.data
,
922 _kvmRun
->mmio
.len
, _kvmRun
->mmio
.is_write
);
924 case KVM_EXIT_IRQ_WINDOW_OPEN
:
925 return handleKvmExitIRQWindowOpen();
927 case KVM_EXIT_FAIL_ENTRY
:
928 return handleKvmExitFailEntry();
931 /* KVM was interrupted by a signal, restart it in the next
935 case KVM_EXIT_INTERNAL_ERROR
:
936 panic("KVM: Internal error (suberror: %u)\n",
937 _kvmRun
->internal
.suberror
);
941 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun
->exit_reason
);
946 BaseKvmCPU::handleKvmExitIO()
948 panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
949 _kvmRun
->io
.direction
, _kvmRun
->io
.size
,
950 _kvmRun
->io
.port
, _kvmRun
->io
.count
);
954 BaseKvmCPU::handleKvmExitHypercall()
956 panic("KVM: Unhandled hypercall\n");
960 BaseKvmCPU::handleKvmExitIRQWindowOpen()
962 warn("KVM: Unhandled IRQ window.\n");
968 BaseKvmCPU::handleKvmExitUnknown()
971 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
972 _kvmRun
->hw
.hardware_exit_reason
);
976 BaseKvmCPU::handleKvmExitException()
979 panic("KVM: Got exception when starting vCPU "
980 "(exception: %u, error_code: %u)\n",
981 _kvmRun
->ex
.exception
, _kvmRun
->ex
.error_code
);
985 BaseKvmCPU::handleKvmExitFailEntry()
988 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
989 _kvmRun
->fail_entry
.hardware_entry_failure_reason
);
993 BaseKvmCPU::doMMIOAccess(Addr paddr
, void *data
, int size
, bool write
)
995 ThreadContext
*tc(thread
->getTC());
998 mmio_req
.setPhys(paddr
, size
, Request::UNCACHEABLE
, dataMasterId());
999 // Some architectures do need to massage physical addresses a bit
1000 // before they are inserted into the memory system. This enables
1001 // APIC accesses on x86 and m5ops where supported through a MMIO
1003 BaseTLB::Mode
tlb_mode(write
? BaseTLB::Write
: BaseTLB::Read
);
1004 Fault
fault(tc
->getDTBPtr()->finalizePhysical(&mmio_req
, tc
, tlb_mode
));
1005 if (fault
!= NoFault
)
1006 warn("Finalization of MMIO address failed: %s\n", fault
->name());
1009 const MemCmd
cmd(write
? MemCmd::WriteReq
: MemCmd::ReadReq
);
1010 Packet
pkt(&mmio_req
, cmd
);
1011 pkt
.dataStatic(data
);
1013 if (mmio_req
.isMmappedIpr()) {
1014 // We currently assume that there is no need to migrate to a
1015 // different event queue when doing IPRs. Currently, IPRs are
1016 // only used for m5ops, so it should be a valid assumption.
1017 const Cycles
ipr_delay(write
?
1018 TheISA::handleIprWrite(tc
, &pkt
) :
1019 TheISA::handleIprRead(tc
, &pkt
));
1020 threadContextDirty
= true;
1021 return clockPeriod() * ipr_delay
;
1023 // Temporarily lock and migrate to the event queue of the
1024 // VM. This queue is assumed to "own" all devices we need to
1025 // access if running in multi-core mode.
1026 EventQueue::ScopedMigration
migrate(vm
.eventQueue());
1028 return dataPort
.sendAtomic(&pkt
);
1033 BaseKvmCPU::setSignalMask(const sigset_t
*mask
)
1035 std::unique_ptr
<struct kvm_signal_mask
> kvm_mask
;
1038 kvm_mask
.reset((struct kvm_signal_mask
*)operator new(
1039 sizeof(struct kvm_signal_mask
) + sizeof(*mask
)));
1040 // The kernel and the user-space headers have different ideas
1041 // about the size of sigset_t. This seems like a massive hack,
1042 // but is actually what qemu does.
1043 assert(sizeof(*mask
) >= 8);
1045 memcpy(kvm_mask
->sigset
, mask
, kvm_mask
->len
);
1048 if (ioctl(KVM_SET_SIGNAL_MASK
, (void *)kvm_mask
.get()) == -1)
1049 panic("KVM: Failed to set vCPU signal mask (errno: %i)\n",
1054 BaseKvmCPU::ioctl(int request
, long p1
) const
1057 panic("KVM: CPU ioctl called before initialization\n");
1059 return ::ioctl(vcpuFD
, request
, p1
);
1063 BaseKvmCPU::flushCoalescedMMIO()
1068 DPRINTF(KvmIO
, "KVM: Flushing the coalesced MMIO ring buffer\n");
1070 // TODO: We might need to do synchronization when we start to
1071 // support multiple CPUs
1073 while (mmioRing
->first
!= mmioRing
->last
) {
1074 struct kvm_coalesced_mmio
&ent(
1075 mmioRing
->coalesced_mmio
[mmioRing
->first
]);
1077 DPRINTF(KvmIO
, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
1078 ent
.phys_addr
, ent
.len
);
1081 ticks
+= doMMIOAccess(ent
.phys_addr
, ent
.data
, ent
.len
, true);
1083 mmioRing
->first
= (mmioRing
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1090 * Dummy handler for KVM kick signals.
1092 * @note This function is usually not called since the kernel doesn't
1093 * seem to deliver signals when the signal is only unmasked when
1094 * running in KVM. This doesn't matter though since we are only
1095 * interested in getting KVM to exit, which happens as expected. See
1096 * setupSignalHandler() and kvmRun() for details about KVM signal
1100 onKickSignal(int signo
, siginfo_t
*si
, void *data
)
1105 BaseKvmCPU::setupSignalHandler()
1107 struct sigaction sa
;
1109 memset(&sa
, 0, sizeof(sa
));
1110 sa
.sa_sigaction
= onKickSignal
;
1111 sa
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1112 if (sigaction(KVM_KICK_SIGNAL
, &sa
, NULL
) == -1)
1113 panic("KVM: Failed to setup vCPU timer signal handler\n");
1116 if (pthread_sigmask(SIG_BLOCK
, NULL
, &sigset
) == -1)
1117 panic("KVM: Failed get signal mask\n");
1119 // Request KVM to setup the same signal mask as we're currently
1120 // running with except for the KVM control signal. We'll sometimes
1121 // need to raise the KVM_KICK_SIGNAL to cause immediate exits from
1122 // KVM after servicing IO requests. See kvmRun().
1123 sigdelset(&sigset
, KVM_KICK_SIGNAL
);
1124 setSignalMask(&sigset
);
1126 // Mask our control signals so they aren't delivered unless we're
1127 // actually executing inside KVM.
1128 sigaddset(&sigset
, KVM_KICK_SIGNAL
);
1129 if (pthread_sigmask(SIG_SETMASK
, &sigset
, NULL
) == -1)
1130 panic("KVM: Failed mask the KVM control signals\n");
1134 BaseKvmCPU::discardPendingSignal(int signum
) const
1136 int discardedSignal
;
1138 // Setting the timeout to zero causes sigtimedwait to return
1140 struct timespec timeout
;
1142 timeout
.tv_nsec
= 0;
1145 sigemptyset(&sigset
);
1146 sigaddset(&sigset
, signum
);
1149 discardedSignal
= sigtimedwait(&sigset
, NULL
, &timeout
);
1150 } while (discardedSignal
== -1 && errno
== EINTR
);
1152 if (discardedSignal
== signum
)
1154 else if (discardedSignal
== -1 && errno
== EAGAIN
)
1157 panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n",
1158 discardedSignal
, errno
);
1162 BaseKvmCPU::setupCounters()
1164 DPRINTF(Kvm
, "Attaching cycle counter...\n");
1165 PerfKvmCounterConfig
cfgCycles(PERF_TYPE_HARDWARE
,
1166 PERF_COUNT_HW_CPU_CYCLES
);
1167 cfgCycles
.disabled(true)
1170 // Try to exclude the host. We set both exclude_hv and
1171 // exclude_host since different architectures use slightly
1172 // different APIs in the kernel.
1173 cfgCycles
.exclude_hv(true)
1174 .exclude_host(true);
1176 if (perfControlledByTimer
) {
1177 // We need to configure the cycles counter to send overflows
1178 // since we are going to use it to trigger timer signals that
1179 // trap back into m5 from KVM. In practice, this means that we
1180 // need to set some non-zero sample period that gets
1181 // overridden when the timer is armed.
1182 cfgCycles
.wakeupEvents(1)
1186 hwCycles
.attach(cfgCycles
,
1187 0); // TID (0 => currentThread)
1193 BaseKvmCPU::tryDrain()
1198 if (!archIsDrained()) {
1199 DPRINTF(Drain
, "tryDrain: Architecture code is not ready.\n");
1203 if (_status
== Idle
|| _status
== Running
) {
1205 "tryDrain: CPU transitioned into the Idle state, drain done\n");
1206 drainManager
->signalDrainDone();
1207 drainManager
= NULL
;
1210 DPRINTF(Drain
, "tryDrain: CPU not ready.\n");
1216 BaseKvmCPU::ioctlRun()
1218 if (ioctl(KVM_RUN
) == -1) {
1220 panic("KVM: Failed to start virtual CPU (errno: %i)\n",
1226 BaseKvmCPU::setupInstStop()
1228 if (comInstEventQueue
[0]->empty()) {
1229 setupInstCounter(0);
1231 const uint64_t next(comInstEventQueue
[0]->nextTick());
1233 assert(next
> ctrInsts
);
1234 setupInstCounter(next
- ctrInsts
);
1239 BaseKvmCPU::setupInstCounter(uint64_t period
)
1241 // No need to do anything if we aren't attaching for the first
1242 // time or the period isn't changing.
1243 if (period
== activeInstPeriod
&& hwInstructions
.attached())
1246 PerfKvmCounterConfig
cfgInstructions(PERF_TYPE_HARDWARE
,
1247 PERF_COUNT_HW_INSTRUCTIONS
);
1249 // Try to exclude the host. We set both exclude_hv and
1250 // exclude_host since different architectures use slightly
1251 // different APIs in the kernel.
1252 cfgInstructions
.exclude_hv(true)
1253 .exclude_host(true);
1256 // Setup a sampling counter if that has been requested.
1257 cfgInstructions
.wakeupEvents(1)
1258 .samplePeriod(period
);
1261 // We need to detach and re-attach the counter to reliably change
1262 // sampling settings. See PerfKvmCounter::period() for details.
1263 if (hwInstructions
.attached())
1264 hwInstructions
.detach();
1265 assert(hwCycles
.attached());
1266 hwInstructions
.attach(cfgInstructions
,
1267 0, // TID (0 => currentThread)
1271 hwInstructions
.enableSignals(KVM_KICK_SIGNAL
);
1273 activeInstPeriod
= period
;