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/utility.hh"
50 #include "cpu/kvm/base.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"
62 /* Used by some KVM macros */
63 #define PAGE_SIZE pageSize
65 volatile bool timerOverflowed
= false;
68 onTimerOverflow(int signo
, siginfo_t
*si
, void *data
)
70 timerOverflowed
= true;
73 BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams
*params
)
77 dataPort(name() + ".dcache_port", this),
78 instPort(name() + ".icache_port", this),
79 threadContextDirty(true),
81 vcpuID(vm
.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
82 _kvmRun(NULL
), mmioRing(NULL
),
83 pageSize(sysconf(_SC_PAGE_SIZE
)),
85 perfControlledByTimer(params
->usePerfOverflow
),
86 hostFreq(params
->hostFreq
),
87 hostFactor(params
->hostFactor
),
92 panic("KVM: Failed to determine host page size (%i)\n",
95 thread
= new SimpleThread(this, 0, params
->system
,
96 params
->itb
, params
->dtb
, params
->isa
[0]);
97 thread
->setStatus(ThreadContext::Halted
);
99 threadContexts
.push_back(tc
);
103 if (params
->usePerfOverflow
)
104 runTimer
.reset(new PerfKvmTimer(hwCycles
,
109 runTimer
.reset(new PosixKvmTimer(KVM_TIMER_SIGNAL
, CLOCK_MONOTONIC
,
114 BaseKvmCPU::~BaseKvmCPU()
117 munmap(_kvmRun
, vcpuMMapSize
);
127 fatal("KVM: Multithreading not supported");
129 tc
->initMemProxies(tc
);
131 // initialize CPU, including PC
132 if (FullSystem
&& !switchedOut())
133 TheISA::initCPU(tc
, tc
->contextId());
135 mmio_req
.setThreadContext(tc
->contextId(), 0);
139 BaseKvmCPU::startup()
141 const BaseKvmCPUParams
* const p(
142 dynamic_cast<const BaseKvmCPUParams
*>(params()));
148 assert(vcpuFD
== -1);
150 // Tell the VM that a CPU is about to start.
153 // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
154 // not guaranteed that the parent KVM VM has initialized at that
155 // point. Initialize virtual CPUs here instead.
156 vcpuFD
= vm
.createVCPU(vcpuID
);
158 // Setup signal handlers. This has to be done after the vCPU is
159 // created since it manipulates the vCPU signal mask.
160 setupSignalHandler();
162 // Map the KVM run structure */
163 vcpuMMapSize
= kvm
.getVCPUMMapSize();
164 _kvmRun
= (struct kvm_run
*)mmap(0, vcpuMMapSize
,
165 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
167 if (_kvmRun
== MAP_FAILED
)
168 panic("KVM: Failed to map run data structure\n");
170 // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
171 // available. The offset into the KVM's communication page is
172 // provided by the coalesced MMIO capability.
173 int mmioOffset(kvm
.capCoalescedMMIO());
174 if (!p
->useCoalescedMMIO
) {
175 inform("KVM: Coalesced MMIO disabled by config.\n");
176 } else if (mmioOffset
) {
177 inform("KVM: Coalesced IO available\n");
178 mmioRing
= (struct kvm_coalesced_mmio_ring
*)(
179 (char *)_kvmRun
+ (mmioOffset
* pageSize
));
181 inform("KVM: Coalesced not supported by host OS\n");
188 BaseKvmCPU::regStats()
190 using namespace Stats
;
195 .name(name() + ".committedInsts")
196 .desc("Number of instructions committed")
200 .name(name() + ".numVMExits")
201 .desc("total number of KVM exits")
205 .name(name() + ".numVMHalfEntries")
206 .desc("number of KVM entries to finalize pending operations")
210 .name(name() + ".numExitSignal")
211 .desc("exits due to signal delivery")
215 .name(name() + ".numMMIO")
216 .desc("number of VM exits due to memory mapped IO")
220 .name(name() + ".numCoalescedMMIO")
221 .desc("number of coalesced memory mapped IO requests")
225 .name(name() + ".numIO")
226 .desc("number of VM exits due to legacy IO")
230 .name(name() + ".numHalt")
231 .desc("number of VM exits due to wait for interrupt instructions")
235 .name(name() + ".numInterrupts")
236 .desc("number of interrupts delivered")
240 .name(name() + ".numHypercalls")
241 .desc("number of hypercalls")
246 BaseKvmCPU::serializeThread(std::ostream
&os
, ThreadID tid
)
248 if (DTRACE(Checkpoint
)) {
249 DPRINTF(Checkpoint
, "KVM: Serializing thread %i:\n", tid
);
254 assert(_status
== Idle
);
255 thread
->serialize(os
);
259 BaseKvmCPU::unserializeThread(Checkpoint
*cp
, const std::string
§ion
,
262 DPRINTF(Checkpoint
, "KVM: Unserialize thread %i:\n", tid
);
265 assert(_status
== Idle
);
266 thread
->unserialize(cp
, section
);
267 threadContextDirty
= true;
271 BaseKvmCPU::drain(DrainManager
*dm
)
276 DPRINTF(Drain
, "BaseKvmCPU::drain\n");
279 // The base KVM code is normally ready when it is in the
280 // Running state, but the architecture specific code might be
281 // of a different opinion. This may happen when the CPU been
282 // notified of an event that hasn't been accepted by the vCPU
284 if (!archIsDrained()) {
289 // The state of the CPU is consistent, so we don't need to do
290 // anything special to drain it. We simply de-schedule the
291 // tick event and enter the Idle state to prevent nasty things
292 // like MMIOs from happening.
293 if (tickEvent
.scheduled())
294 deschedule(tickEvent
);
299 // Idle, no need to drain
300 assert(!tickEvent
.scheduled());
302 // Sync the thread context here since we'll need it when we
303 // switch CPUs or checkpoint the CPU.
308 case RunningServiceCompletion
:
309 // The CPU has just requested a service that was handled in
310 // the RunningService state, but the results have still not
311 // been reported to the CPU. Now, we /could/ probably just
312 // update the register state ourselves instead of letting KVM
313 // handle it, but that would be tricky. Instead, we enter KVM
314 // and let it do its stuff.
317 DPRINTF(Drain
, "KVM CPU is waiting for service completion, "
318 "requesting drain.\n");
322 // We need to drain since the CPU is waiting for service (e.g., MMIOs)
325 DPRINTF(Drain
, "KVM CPU is waiting for service, requesting drain.\n");
329 panic("KVM: Unhandled CPU state in drain()\n");
335 BaseKvmCPU::drainResume()
337 assert(!tickEvent
.scheduled());
339 // We might have been switched out. In that case, we don't need to
344 DPRINTF(Kvm
, "drainResume\n");
347 // The tick event is de-scheduled as a part of the draining
348 // process. Re-schedule it if the thread context is active.
349 if (tc
->status() == ThreadContext::Active
) {
350 schedule(tickEvent
, nextCycle());
358 BaseKvmCPU::switchOut()
360 DPRINTF(Kvm
, "switchOut\n");
362 BaseCPU::switchOut();
364 // We should have drained prior to executing a switchOut, which
365 // means that the tick event shouldn't be scheduled and the CPU is
367 assert(!tickEvent
.scheduled());
368 assert(_status
== Idle
);
372 BaseKvmCPU::takeOverFrom(BaseCPU
*cpu
)
374 DPRINTF(Kvm
, "takeOverFrom\n");
376 BaseCPU::takeOverFrom(cpu
);
378 // We should have drained prior to executing a switchOut, which
379 // means that the tick event shouldn't be scheduled and the CPU is
381 assert(!tickEvent
.scheduled());
382 assert(_status
== Idle
);
383 assert(threadContexts
.size() == 1);
385 // Force an update of the KVM state here instead of flagging the
386 // TC as dirty. This is not ideal from a performance point of
387 // view, but it makes debugging easier as it allows meaningful KVM
388 // state to be dumped before and after a takeover.
390 threadContextDirty
= false;
394 BaseKvmCPU::verifyMemoryMode() const
396 if (!(system
->isAtomicMode() && system
->bypassCaches())) {
397 fatal("The KVM-based CPUs requires the memory system to be in the "
398 "'atomic_noncaching' mode.\n");
405 DPRINTF(Kvm
, "wakeup()\n");
407 if (thread
->status() != ThreadContext::Suspended
)
414 BaseKvmCPU::activateContext(ThreadID thread_num
, Cycles delay
)
416 DPRINTF(Kvm
, "ActivateContext %d (%d cycles)\n", thread_num
, delay
);
418 assert(thread_num
== 0);
421 assert(_status
== Idle
);
422 assert(!tickEvent
.scheduled());
424 numCycles
+= ticksToCycles(thread
->lastActivate
- thread
->lastSuspend
);
426 schedule(tickEvent
, clockEdge(delay
));
432 BaseKvmCPU::suspendContext(ThreadID thread_num
)
434 DPRINTF(Kvm
, "SuspendContext %d\n", thread_num
);
436 assert(thread_num
== 0);
442 assert(_status
== Running
);
444 // The tick event may no be scheduled if the quest has requested
445 // the monitor to wait for interrupts. The normal CPU models can
446 // get their tick events descheduled by quiesce instructions, but
447 // that can't happen here.
448 if (tickEvent
.scheduled())
449 deschedule(tickEvent
);
455 BaseKvmCPU::deallocateContext(ThreadID thread_num
)
457 // for now, these are equivalent
458 suspendContext(thread_num
);
462 BaseKvmCPU::haltContext(ThreadID thread_num
)
464 // for now, these are equivalent
465 suspendContext(thread_num
);
469 BaseKvmCPU::getContext(int tn
)
478 BaseKvmCPU::totalInsts() const
484 BaseKvmCPU::totalOps() const
486 hack_once("Pretending totalOps is equivalent to totalInsts()\n");
493 inform("State dumping not implemented.");
500 assert(_status
!= Idle
);
504 // handleKvmExit() will determine the next state of the CPU
505 delay
= handleKvmExit();
511 case RunningServiceCompletion
:
513 Tick
ticksToExecute(mainEventQueue
.nextTick() - curTick());
515 // We might need to update the KVM state.
518 DPRINTF(KvmRun
, "Entering KVM...\n");
520 // Force an immediate exit from KVM after completing
521 // pending operations. The architecture-specific code
522 // takes care to run until it is in a state where it can
523 // safely be drained.
524 delay
= kvmRunDrain();
526 delay
= kvmRun(ticksToExecute
);
529 // Entering into KVM implies that we'll have to reload the thread
530 // context from KVM if we want to access it. Flag the KVM state as
531 // dirty with respect to the cached thread context.
532 kvmStateDirty
= true;
534 // Enter into the RunningService state unless the
535 // simulation was stopped by a timer.
536 if (_kvmRun
->exit_reason
!= KVM_EXIT_INTR
) {
537 _status
= RunningService
;
548 panic("BaseKvmCPU entered tick() in an illegal state (%i)\n",
552 // Schedule a new tick if we are still running
554 schedule(tickEvent
, clockEdge(ticksToCycles(delay
)));
558 BaseKvmCPU::kvmRunDrain()
560 // By default, the only thing we need to drain is a pending IO
561 // operation which assumes that we are in the
562 // RunningServiceCompletion state.
563 assert(_status
== RunningServiceCompletion
);
565 // Deliver the data from the pending IO operation and immediately
571 BaseKvmCPU::getHostCycles() const
573 return hwCycles
.read();
577 BaseKvmCPU::kvmRun(Tick ticks
)
580 DPRINTF(KvmRun
, "KVM: Executing for %i ticks\n", ticks
);
581 timerOverflowed
= false;
584 // Settings ticks == 0 is a special case which causes an entry
585 // into KVM that finishes pending operations (e.g., IO) and
586 // then immediately exits.
587 DPRINTF(KvmRun
, "KVM: Delivering IO without full guest entry\n");
591 // This signal is always masked while we are executing in gem5
592 // and gets unmasked temporarily as soon as we enter into
593 // KVM. See setSignalMask() and setupSignalHandler().
594 raise(KVM_TIMER_SIGNAL
);
596 // Enter into KVM. KVM will check for signals after completing
597 // pending operations (IO). Since the KVM_TIMER_SIGNAL is
598 // pending, this forces an immediate exit into gem5 again. We
599 // don't bother to setup timers since this shouldn't actually
600 // execute any code in the guest.
603 // We always execute at least one cycle to prevent the
604 // BaseKvmCPU::tick() to be rescheduled on the same tick
606 ticksExecuted
= clockPeriod();
608 if (ticks
< runTimer
->resolution()) {
609 DPRINTF(KvmRun
, "KVM: Adjusting tick count (%i -> %i)\n",
610 ticks
, runTimer
->resolution());
611 ticks
= runTimer
->resolution();
614 // Get hardware statistics after synchronizing contexts. The KVM
615 // state update might affect guest cycle counters.
616 uint64_t baseCycles(getHostCycles());
617 uint64_t baseInstrs(hwInstructions
.read());
619 // Arm the run timer and start the cycle timer if it isn't
620 // controlled by the overflow timer. Starting/stopping the cycle
621 // timer automatically starts the other perf timers as they are in
622 // the same counter group.
623 runTimer
->arm(ticks
);
624 if (!perfControlledByTimer
)
630 if (!perfControlledByTimer
)
633 // The timer signal may have been delivered after we exited
634 // from KVM. It will be pending in that case since it is
635 // masked when we aren't executing in KVM. Discard it to make
636 // sure we don't deliver it immediately next time we try to
638 discardPendingSignal(KVM_TIMER_SIGNAL
);
640 const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles
);
641 const uint64_t simCyclesExecuted(hostCyclesExecuted
* hostFactor
);
642 const uint64_t instsExecuted(hwInstructions
.read() - baseInstrs
);
643 ticksExecuted
= runTimer
->ticksFromHostCycles(hostCyclesExecuted
);
645 if (ticksExecuted
< ticks
&&
647 _kvmRun
->exit_reason
== KVM_EXIT_INTR
) {
648 // TODO: We should probably do something clever here...
649 warn("KVM: Early timer event, requested %i ticks but got %i ticks.\n",
650 ticks
, ticksExecuted
);
653 /* Update statistics */
654 numCycles
+= simCyclesExecuted
;;
655 numInsts
+= instsExecuted
;
656 ctrInsts
+= instsExecuted
;
657 system
->totalNumInsts
+= instsExecuted
;
660 "KVM: Executed %i instructions in %i cycles "
661 "(%i ticks, sim cycles: %i).\n",
662 instsExecuted
, hostCyclesExecuted
, ticksExecuted
, simCyclesExecuted
);
667 return ticksExecuted
+ flushCoalescedMMIO();
671 BaseKvmCPU::kvmNonMaskableInterrupt()
674 if (ioctl(KVM_NMI
) == -1)
675 panic("KVM: Failed to deliver NMI to virtual CPU\n");
679 BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt
&interrupt
)
682 if (ioctl(KVM_INTERRUPT
, (void *)&interrupt
) == -1)
683 panic("KVM: Failed to deliver interrupt to virtual CPU\n");
687 BaseKvmCPU::getRegisters(struct kvm_regs
®s
) const
689 if (ioctl(KVM_GET_REGS
, ®s
) == -1)
690 panic("KVM: Failed to get guest registers\n");
694 BaseKvmCPU::setRegisters(const struct kvm_regs
®s
)
696 if (ioctl(KVM_SET_REGS
, (void *)®s
) == -1)
697 panic("KVM: Failed to set guest registers\n");
701 BaseKvmCPU::getSpecialRegisters(struct kvm_sregs
®s
) const
703 if (ioctl(KVM_GET_SREGS
, ®s
) == -1)
704 panic("KVM: Failed to get guest special registers\n");
708 BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs
®s
)
710 if (ioctl(KVM_SET_SREGS
, (void *)®s
) == -1)
711 panic("KVM: Failed to set guest special registers\n");
715 BaseKvmCPU::getFPUState(struct kvm_fpu
&state
) const
717 if (ioctl(KVM_GET_FPU
, &state
) == -1)
718 panic("KVM: Failed to get guest FPU state\n");
722 BaseKvmCPU::setFPUState(const struct kvm_fpu
&state
)
724 if (ioctl(KVM_SET_FPU
, (void *)&state
) == -1)
725 panic("KVM: Failed to set guest FPU state\n");
730 BaseKvmCPU::setOneReg(uint64_t id
, const void *addr
)
732 #ifdef KVM_SET_ONE_REG
733 struct kvm_one_reg reg
;
735 reg
.addr
= (uint64_t)addr
;
737 if (ioctl(KVM_SET_ONE_REG
, ®
) == -1) {
738 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
742 panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
747 BaseKvmCPU::getOneReg(uint64_t id
, void *addr
) const
749 #ifdef KVM_GET_ONE_REG
750 struct kvm_one_reg reg
;
752 reg
.addr
= (uint64_t)addr
;
754 if (ioctl(KVM_GET_ONE_REG
, ®
) == -1) {
755 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
759 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
764 BaseKvmCPU::getAndFormatOneReg(uint64_t id
) const
766 #ifdef KVM_GET_ONE_REG
767 std::ostringstream ss
;
769 ss
.setf(std::ios::hex
, std::ios::basefield
);
770 ss
.setf(std::ios::showbase
);
771 #define HANDLE_INTTYPE(len) \
772 case KVM_REG_SIZE_U ## len: { \
773 uint ## len ## _t value; \
774 getOneReg(id, &value); \
778 #define HANDLE_ARRAY(len) \
779 case KVM_REG_SIZE_U ## len: { \
780 uint8_t value[len / 8]; \
781 getOneReg(id, value); \
782 ss << "[" << value[0]; \
783 for (int i = 1; i < len / 8; ++i) \
784 ss << ", " << value[i]; \
788 switch (id
& KVM_REG_SIZE_MASK
) {
801 #undef HANDLE_INTTYPE
806 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
811 BaseKvmCPU::syncThreadContext()
816 assert(!threadContextDirty
);
818 updateThreadContext();
819 kvmStateDirty
= false;
823 BaseKvmCPU::syncKvmState()
825 if (!threadContextDirty
)
828 assert(!kvmStateDirty
);
831 threadContextDirty
= false;
835 BaseKvmCPU::handleKvmExit()
837 DPRINTF(KvmRun
, "handleKvmExit (exit_reason: %i)\n", _kvmRun
->exit_reason
);
838 assert(_status
== RunningService
);
840 // Switch into the running state by default. Individual handlers
841 // can override this.
843 switch (_kvmRun
->exit_reason
) {
844 case KVM_EXIT_UNKNOWN
:
845 return handleKvmExitUnknown();
847 case KVM_EXIT_EXCEPTION
:
848 return handleKvmExitException();
851 _status
= RunningServiceCompletion
;
853 return handleKvmExitIO();
855 case KVM_EXIT_HYPERCALL
:
857 return handleKvmExitHypercall();
860 /* The guest has halted and is waiting for interrupts */
861 DPRINTF(Kvm
, "handleKvmExitHalt\n");
864 // Suspend the thread until the next interrupt arrives
867 // This is actually ignored since the thread is suspended.
871 _status
= RunningServiceCompletion
;
872 /* Service memory mapped IO requests */
873 DPRINTF(KvmIO
, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
874 _kvmRun
->mmio
.is_write
,
875 _kvmRun
->mmio
.phys_addr
, _kvmRun
->mmio
.len
);
878 return doMMIOAccess(_kvmRun
->mmio
.phys_addr
, _kvmRun
->mmio
.data
,
879 _kvmRun
->mmio
.len
, _kvmRun
->mmio
.is_write
);
881 case KVM_EXIT_IRQ_WINDOW_OPEN
:
882 return handleKvmExitIRQWindowOpen();
884 case KVM_EXIT_FAIL_ENTRY
:
885 return handleKvmExitFailEntry();
888 /* KVM was interrupted by a signal, restart it in the next
892 case KVM_EXIT_INTERNAL_ERROR
:
893 panic("KVM: Internal error (suberror: %u)\n",
894 _kvmRun
->internal
.suberror
);
898 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun
->exit_reason
);
903 BaseKvmCPU::handleKvmExitIO()
905 panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
906 _kvmRun
->io
.direction
, _kvmRun
->io
.size
,
907 _kvmRun
->io
.port
, _kvmRun
->io
.count
);
911 BaseKvmCPU::handleKvmExitHypercall()
913 panic("KVM: Unhandled hypercall\n");
917 BaseKvmCPU::handleKvmExitIRQWindowOpen()
919 warn("KVM: Unhandled IRQ window.\n");
925 BaseKvmCPU::handleKvmExitUnknown()
928 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
929 _kvmRun
->hw
.hardware_exit_reason
);
933 BaseKvmCPU::handleKvmExitException()
936 panic("KVM: Got exception when starting vCPU "
937 "(exception: %u, error_code: %u)\n",
938 _kvmRun
->ex
.exception
, _kvmRun
->ex
.error_code
);
942 BaseKvmCPU::handleKvmExitFailEntry()
945 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
946 _kvmRun
->fail_entry
.hardware_entry_failure_reason
);
950 BaseKvmCPU::doMMIOAccess(Addr paddr
, void *data
, int size
, bool write
)
952 mmio_req
.setPhys(paddr
, size
, Request::UNCACHEABLE
, dataMasterId());
954 const MemCmd
cmd(write
? MemCmd::WriteReq
: MemCmd::ReadReq
);
955 Packet
pkt(&mmio_req
, cmd
);
956 pkt
.dataStatic(data
);
957 return dataPort
.sendAtomic(&pkt
);
961 BaseKvmCPU::setSignalMask(const sigset_t
*mask
)
963 std::unique_ptr
<struct kvm_signal_mask
> kvm_mask
;
966 kvm_mask
.reset((struct kvm_signal_mask
*)operator new(
967 sizeof(struct kvm_signal_mask
) + sizeof(*mask
)));
968 // The kernel and the user-space headers have different ideas
969 // about the size of sigset_t. This seems like a massive hack,
970 // but is actually what qemu does.
971 assert(sizeof(*mask
) >= 8);
973 memcpy(kvm_mask
->sigset
, mask
, kvm_mask
->len
);
976 if (ioctl(KVM_SET_SIGNAL_MASK
, (void *)kvm_mask
.get()) == -1)
977 panic("KVM: Failed to set vCPU signal mask (errno: %i)\n",
982 BaseKvmCPU::ioctl(int request
, long p1
) const
985 panic("KVM: CPU ioctl called before initialization\n");
987 return ::ioctl(vcpuFD
, request
, p1
);
991 BaseKvmCPU::flushCoalescedMMIO()
996 DPRINTF(KvmIO
, "KVM: Flushing the coalesced MMIO ring buffer\n");
998 // TODO: We might need to do synchronization when we start to
999 // support multiple CPUs
1001 while (mmioRing
->first
!= mmioRing
->last
) {
1002 struct kvm_coalesced_mmio
&ent(
1003 mmioRing
->coalesced_mmio
[mmioRing
->first
]);
1005 DPRINTF(KvmIO
, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
1006 ent
.phys_addr
, ent
.len
);
1009 ticks
+= doMMIOAccess(ent
.phys_addr
, ent
.data
, ent
.len
, true);
1011 mmioRing
->first
= (mmioRing
->first
+ 1) % KVM_COALESCED_MMIO_MAX
;
1018 BaseKvmCPU::setupSignalHandler()
1020 struct sigaction sa
;
1022 memset(&sa
, 0, sizeof(sa
));
1023 sa
.sa_sigaction
= onTimerOverflow
;
1024 sa
.sa_flags
= SA_SIGINFO
| SA_RESTART
;
1025 if (sigaction(KVM_TIMER_SIGNAL
, &sa
, NULL
) == -1)
1026 panic("KVM: Failed to setup vCPU signal handler\n");
1029 if (sigprocmask(SIG_BLOCK
, NULL
, &sigset
) == -1)
1030 panic("KVM: Failed get signal mask\n");
1032 // Request KVM to setup the same signal mask as we're currently
1033 // running with. We'll sometimes need to mask the KVM_TIMER_SIGNAL
1034 // to cause immediate exits from KVM after servicing IO
1035 // requests. See kvmRun().
1036 setSignalMask(&sigset
);
1038 // Mask the KVM_TIMER_SIGNAL so it isn't delivered unless we're
1039 // actually executing inside KVM.
1040 sigaddset(&sigset
, KVM_TIMER_SIGNAL
);
1041 if (sigprocmask(SIG_SETMASK
, &sigset
, NULL
) == -1)
1042 panic("KVM: Failed mask the KVM timer signal\n");
1046 BaseKvmCPU::discardPendingSignal(int signum
) const
1048 int discardedSignal
;
1050 // Setting the timeout to zero causes sigtimedwait to return
1052 struct timespec timeout
;
1054 timeout
.tv_nsec
= 0;
1057 sigemptyset(&sigset
);
1058 sigaddset(&sigset
, signum
);
1061 discardedSignal
= sigtimedwait(&sigset
, NULL
, &timeout
);
1062 } while (discardedSignal
== -1 && errno
== EINTR
);
1064 if (discardedSignal
== signum
)
1066 else if (discardedSignal
== -1 && errno
== EAGAIN
)
1069 panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n",
1070 discardedSignal
, errno
);
1074 BaseKvmCPU::setupCounters()
1076 DPRINTF(Kvm
, "Attaching cycle counter...\n");
1077 PerfKvmCounterConfig
cfgCycles(PERF_TYPE_HARDWARE
,
1078 PERF_COUNT_HW_CPU_CYCLES
);
1079 cfgCycles
.disabled(true)
1082 if (perfControlledByTimer
) {
1083 // We need to configure the cycles counter to send overflows
1084 // since we are going to use it to trigger timer signals that
1085 // trap back into m5 from KVM. In practice, this means that we
1086 // need to set some non-zero sample period that gets
1087 // overridden when the timer is armed.
1088 cfgCycles
.wakeupEvents(1)
1092 hwCycles
.attach(cfgCycles
,
1093 0); // TID (0 => currentThread)
1095 DPRINTF(Kvm
, "Attaching instruction counter...\n");
1096 PerfKvmCounterConfig
cfgInstructions(PERF_TYPE_HARDWARE
,
1097 PERF_COUNT_HW_INSTRUCTIONS
);
1098 hwInstructions
.attach(cfgInstructions
,
1099 0, // TID (0 => currentThread)
1104 BaseKvmCPU::tryDrain()
1109 if (!archIsDrained()) {
1110 DPRINTF(Drain
, "tryDrain: Architecture code is not ready.\n");
1114 if (_status
== Idle
|| _status
== Running
) {
1116 "tryDrain: CPU transitioned into the Idle state, drain done\n");
1117 drainManager
->signalDrainDone();
1118 drainManager
= NULL
;
1121 DPRINTF(Drain
, "tryDrain: CPU not ready.\n");
1127 BaseKvmCPU::ioctlRun()
1129 if (ioctl(KVM_RUN
) == -1) {
1131 panic("KVM: Failed to start virtual CPU (errno: %i)\n",