cpu: Delete authors lists from the cpu directory.
[gem5.git] / src / cpu / kvm / base.cc
1 /*
2 * Copyright (c) 2012, 2015, 2017 ARM Limited
3 * All rights reserved
4 *
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.
13 *
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.
24 *
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.
36 */
37
38 #include "cpu/kvm/base.hh"
39
40 #include <linux/kvm.h>
41 #include <sys/ioctl.h>
42 #include <sys/mman.h>
43 #include <unistd.h>
44
45 #include <cerrno>
46 #include <csignal>
47 #include <ostream>
48
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"
59
60 /* Used by some KVM macros */
61 #define PAGE_SIZE pageSize
62
63 BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params)
64 : BaseCPU(params),
65 vm(*params->system->getKvmVM()),
66 _status(Idle),
67 dataPort(name() + ".dcache_port", this),
68 instPort(name() + ".icache_port", this),
69 alwaysSyncTC(params->alwaysSyncTC),
70 threadContextDirty(true),
71 kvmStateDirty(false),
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),
77 activeInstPeriod(0),
78 perfControlledByTimer(params->usePerfOverflow),
79 hostFactor(params->hostFactor),
80 ctrInsts(0)
81 {
82 if (pageSize == -1)
83 panic("KVM: Failed to determine host page size (%i)\n",
84 errno);
85
86 if (FullSystem)
87 thread = new SimpleThread(this, 0, params->system, params->itb, params->dtb,
88 params->isa[0]);
89 else
90 thread = new SimpleThread(this, /* thread_num */ 0, params->system,
91 params->workload[0], params->itb,
92 params->dtb, params->isa[0]);
93
94 thread->setStatus(ThreadContext::Halted);
95 tc = thread->getTC();
96 threadContexts.push_back(tc);
97 }
98
99 BaseKvmCPU::~BaseKvmCPU()
100 {
101 if (_kvmRun)
102 munmap(_kvmRun, vcpuMMapSize);
103 close(vcpuFD);
104 }
105
106 void
107 BaseKvmCPU::init()
108 {
109 BaseCPU::init();
110
111 if (numThreads != 1)
112 fatal("KVM: Multithreading not supported");
113
114 tc->initMemProxies(tc);
115 }
116
117 void
118 BaseKvmCPU::startup()
119 {
120 const BaseKvmCPUParams * const p(
121 dynamic_cast<const BaseKvmCPUParams *>(params()));
122
123 Kvm &kvm(*vm.kvm);
124
125 BaseCPU::startup();
126
127 assert(vcpuFD == -1);
128
129 // Tell the VM that a CPU is about to start.
130 vm.cpuStartup();
131
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);
136
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,
141 vcpuFD, 0);
142 if (_kvmRun == MAP_FAILED)
143 panic("KVM: Failed to map run data structure\n");
144
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));
155 } else {
156 inform("KVM: Coalesced not supported by host OS\n");
157 }
158
159 thread->startup();
160
161 Event *startupEvent(
162 new EventFunctionWrapper([this]{ startupThread(); }, name(), true));
163 schedule(startupEvent, curTick());
164 }
165
166 BaseKvmCPU::Status
167 BaseKvmCPU::KVMCpuPort::nextIOState() const
168 {
169 return (activeMMIOReqs || pendingMMIOPkts.size())
170 ? RunningMMIOPending : RunningServiceCompletion;
171 }
172
173 Tick
174 BaseKvmCPU::KVMCpuPort::submitIO(PacketPtr pkt)
175 {
176 if (cpu->system->isAtomicMode()) {
177 Tick delay = sendAtomic(pkt);
178 delete pkt;
179 return delay;
180 } else {
181 if (pendingMMIOPkts.empty() && sendTimingReq(pkt)) {
182 activeMMIOReqs++;
183 } else {
184 pendingMMIOPkts.push(pkt);
185 }
186 // Return value is irrelevant for timing-mode accesses.
187 return 0;
188 }
189 }
190
191 bool
192 BaseKvmCPU::KVMCpuPort::recvTimingResp(PacketPtr pkt)
193 {
194 DPRINTF(KvmIO, "KVM: Finished timing request\n");
195
196 delete pkt;
197 activeMMIOReqs--;
198
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();
204 }
205 return true;
206 }
207
208 void
209 BaseKvmCPU::KVMCpuPort::recvReqRetry()
210 {
211 DPRINTF(KvmIO, "KVM: Retry for timing request\n");
212
213 assert(pendingMMIOPkts.size());
214
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
217 // KVM.
218 while (pendingMMIOPkts.size() && sendTimingReq(pendingMMIOPkts.front())) {
219 pendingMMIOPkts.pop();
220 activeMMIOReqs++;
221 }
222 }
223
224 void
225 BaseKvmCPU::finishMMIOPending()
226 {
227 assert(_status = RunningMMIOPending);
228 assert(!tickEvent.scheduled());
229
230 _status = RunningServiceCompletion;
231 schedule(tickEvent, nextCycle());
232 }
233
234 void
235 BaseKvmCPU::startupThread()
236 {
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()));
243
244 vcpuThread = pthread_self();
245
246 // Setup signal handlers. This has to be done after the vCPU is
247 // created since it manipulates the vCPU signal mask.
248 setupSignalHandler();
249
250 setupCounters();
251
252 if (p->usePerfOverflow)
253 runTimer.reset(new PerfKvmTimer(hwCycles,
254 KVM_KICK_SIGNAL,
255 p->hostFactor,
256 p->hostFreq));
257 else
258 runTimer.reset(new PosixKvmTimer(KVM_KICK_SIGNAL, CLOCK_MONOTONIC,
259 p->hostFactor,
260 p->hostFreq));
261
262 }
263
264 void
265 BaseKvmCPU::regStats()
266 {
267 using namespace Stats;
268
269 BaseCPU::regStats();
270
271 numInsts
272 .name(name() + ".committedInsts")
273 .desc("Number of instructions committed")
274 ;
275
276 numVMExits
277 .name(name() + ".numVMExits")
278 .desc("total number of KVM exits")
279 ;
280
281 numVMHalfEntries
282 .name(name() + ".numVMHalfEntries")
283 .desc("number of KVM entries to finalize pending operations")
284 ;
285
286 numExitSignal
287 .name(name() + ".numExitSignal")
288 .desc("exits due to signal delivery")
289 ;
290
291 numMMIO
292 .name(name() + ".numMMIO")
293 .desc("number of VM exits due to memory mapped IO")
294 ;
295
296 numCoalescedMMIO
297 .name(name() + ".numCoalescedMMIO")
298 .desc("number of coalesced memory mapped IO requests")
299 ;
300
301 numIO
302 .name(name() + ".numIO")
303 .desc("number of VM exits due to legacy IO")
304 ;
305
306 numHalt
307 .name(name() + ".numHalt")
308 .desc("number of VM exits due to wait for interrupt instructions")
309 ;
310
311 numInterrupts
312 .name(name() + ".numInterrupts")
313 .desc("number of interrupts delivered")
314 ;
315
316 numHypercalls
317 .name(name() + ".numHypercalls")
318 .desc("number of hypercalls")
319 ;
320 }
321
322 void
323 BaseKvmCPU::serializeThread(CheckpointOut &cp, ThreadID tid) const
324 {
325 if (DTRACE(Checkpoint)) {
326 DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid);
327 dump();
328 }
329
330 assert(tid == 0);
331 assert(_status == Idle);
332 thread->serialize(cp);
333 }
334
335 void
336 BaseKvmCPU::unserializeThread(CheckpointIn &cp, ThreadID tid)
337 {
338 DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid);
339
340 assert(tid == 0);
341 assert(_status == Idle);
342 thread->unserialize(cp);
343 threadContextDirty = true;
344 }
345
346 DrainState
347 BaseKvmCPU::drain()
348 {
349 if (switchedOut())
350 return DrainState::Drained;
351
352 DPRINTF(Drain, "BaseKvmCPU::drain\n");
353
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());
359
360 switch (_status) {
361 case Running:
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
366 // yet.
367 if (!archIsDrained())
368 return DrainState::Draining;
369
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);
376 _status = Idle;
377
378 M5_FALLTHROUGH;
379 case Idle:
380 // Idle, no need to drain
381 assert(!tickEvent.scheduled());
382
383 // Sync the thread context here since we'll need it when we
384 // switch CPUs or checkpoint the CPU.
385 syncThreadContext();
386
387 return DrainState::Drained;
388
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;
399
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;
405
406 case RunningService:
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;
410
411 default:
412 panic("KVM: Unhandled CPU state in drain()\n");
413 return DrainState::Drained;
414 }
415 }
416
417 void
418 BaseKvmCPU::drainResume()
419 {
420 assert(!tickEvent.scheduled());
421
422 // We might have been switched out. In that case, we don't need to
423 // do anything.
424 if (switchedOut())
425 return;
426
427 DPRINTF(Kvm, "drainResume\n");
428 verifyMemoryMode();
429
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());
434 _status = Running;
435 } else {
436 _status = Idle;
437 }
438 }
439
440 void
441 BaseKvmCPU::notifyFork()
442 {
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);
447
448 if (vcpuFD != -1) {
449 if (close(vcpuFD) == -1)
450 warn("kvm CPU: notifyFork failed to close vcpuFD\n");
451
452 if (_kvmRun)
453 munmap(_kvmRun, vcpuMMapSize);
454
455 vcpuFD = -1;
456 _kvmRun = NULL;
457
458 hwInstructions.detach();
459 hwCycles.detach();
460 }
461 }
462
463 void
464 BaseKvmCPU::switchOut()
465 {
466 DPRINTF(Kvm, "switchOut\n");
467
468 BaseCPU::switchOut();
469
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
472 // idle.
473 assert(!tickEvent.scheduled());
474 assert(_status == Idle);
475 }
476
477 void
478 BaseKvmCPU::takeOverFrom(BaseCPU *cpu)
479 {
480 DPRINTF(Kvm, "takeOverFrom\n");
481
482 BaseCPU::takeOverFrom(cpu);
483
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
486 // idle.
487 assert(!tickEvent.scheduled());
488 assert(_status == Idle);
489 assert(threadContexts.size() == 1);
490
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.
495 updateKvmState();
496 threadContextDirty = false;
497 }
498
499 void
500 BaseKvmCPU::verifyMemoryMode() const
501 {
502 if (!(system->bypassCaches())) {
503 fatal("The KVM-based CPUs requires the memory system to be in the "
504 "'noncaching' mode.\n");
505 }
506 }
507
508 void
509 BaseKvmCPU::wakeup(ThreadID tid)
510 {
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());
516
517 // Kick the vCPU to get it to come out of KVM.
518 kick();
519
520 if (thread->status() != ThreadContext::Suspended)
521 return;
522
523 thread->activate();
524 }
525
526 void
527 BaseKvmCPU::activateContext(ThreadID thread_num)
528 {
529 DPRINTF(Kvm, "ActivateContext %d\n", thread_num);
530
531 assert(thread_num == 0);
532 assert(thread);
533
534 assert(_status == Idle);
535 assert(!tickEvent.scheduled());
536
537 numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend);
538
539 schedule(tickEvent, clockEdge(Cycles(0)));
540 _status = Running;
541 }
542
543
544 void
545 BaseKvmCPU::suspendContext(ThreadID thread_num)
546 {
547 DPRINTF(Kvm, "SuspendContext %d\n", thread_num);
548
549 assert(thread_num == 0);
550 assert(thread);
551
552 if (_status == Idle)
553 return;
554
555 assert(_status == Running || _status == RunningServiceCompletion);
556
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);
563
564 _status = Idle;
565 }
566
567 void
568 BaseKvmCPU::deallocateContext(ThreadID thread_num)
569 {
570 // for now, these are equivalent
571 suspendContext(thread_num);
572 }
573
574 void
575 BaseKvmCPU::haltContext(ThreadID thread_num)
576 {
577 // for now, these are equivalent
578 suspendContext(thread_num);
579 updateCycleCounters(BaseCPU::CPU_STATE_SLEEP);
580 }
581
582 ThreadContext *
583 BaseKvmCPU::getContext(int tn)
584 {
585 assert(tn == 0);
586 syncThreadContext();
587 return tc;
588 }
589
590
591 Counter
592 BaseKvmCPU::totalInsts() const
593 {
594 return ctrInsts;
595 }
596
597 Counter
598 BaseKvmCPU::totalOps() const
599 {
600 hack_once("Pretending totalOps is equivalent to totalInsts()\n");
601 return ctrInsts;
602 }
603
604 void
605 BaseKvmCPU::dump() const
606 {
607 inform("State dumping not implemented.");
608 }
609
610 void
611 BaseKvmCPU::tick()
612 {
613 Tick delay(0);
614 assert(_status != Idle && _status != RunningMMIOPending);
615
616 switch (_status) {
617 case RunningService:
618 // handleKvmExit() will determine the next state of the CPU
619 delay = handleKvmExit();
620
621 if (tryDrain())
622 _status = Idle;
623 break;
624
625 case RunningServiceCompletion:
626 case Running: {
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);
635
636 if (alwaysSyncTC)
637 threadContextDirty = true;
638
639 // We might need to update the KVM state.
640 syncKvmState();
641
642 // Setup any pending instruction count breakpoints using
643 // PerfEvent if we are going to execute more than just an IO
644 // completion.
645 if (ticksToExecute > 0)
646 setupInstStop();
647
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();
655 } else {
656 delay = kvmRun(ticksToExecute);
657 }
658
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.
662 if (_status == Idle)
663 break;
664
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;
669
670 if (alwaysSyncTC)
671 syncThreadContext();
672
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;
677 } else {
678 ++numExitSignal;
679 _status = Running;
680 }
681
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);
686
687 if (tryDrain())
688 _status = Idle;
689 } break;
690
691 default:
692 panic("BaseKvmCPU entered tick() in an illegal state (%i)\n",
693 _status);
694 }
695
696 // Schedule a new tick if we are still running
697 if (_status != Idle && _status != RunningMMIOPending)
698 schedule(tickEvent, clockEdge(ticksToCycles(delay)));
699 }
700
701 Tick
702 BaseKvmCPU::kvmRunDrain()
703 {
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);
709
710 // Deliver the data from the pending IO operation and immediately
711 // exit.
712 return kvmRun(0);
713 }
714
715 uint64_t
716 BaseKvmCPU::getHostCycles() const
717 {
718 return hwCycles.read();
719 }
720
721 Tick
722 BaseKvmCPU::kvmRun(Tick ticks)
723 {
724 Tick ticksExecuted;
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);
729
730 if (ticks == 0) {
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");
735
736 ++numVMHalfEntries;
737
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().
742 kick();
743
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.
750 ioctlRun();
751
752 // We always execute at least one cycle to prevent the
753 // BaseKvmCPU::tick() to be rescheduled on the same tick
754 // twice.
755 ticksExecuted = clockPeriod();
756 } else {
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());
764
765 if (ticks < runTimer->resolution()) {
766 DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
767 ticks, runTimer->resolution());
768 ticks = runTimer->resolution();
769 }
770
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());
775
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)
782 hwCycles.start();
783
784 ioctlRun();
785
786 runTimer->disarm();
787 if (!perfControlledByTimer)
788 hwCycles.stop();
789
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
794 // enter into KVM.
795 discardPendingSignal(KVM_KICK_SIGNAL);
796
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);
801
802 /* Update statistics */
803 numCycles += simCyclesExecuted;;
804 numInsts += instsExecuted;
805 ctrInsts += instsExecuted;
806 system->totalNumInsts += instsExecuted;
807
808 DPRINTF(KvmRun,
809 "KVM: Executed %i instructions in %i cycles "
810 "(%i ticks, sim cycles: %i).\n",
811 instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted);
812 }
813
814 ++numVMExits;
815
816 return ticksExecuted + flushCoalescedMMIO();
817 }
818
819 void
820 BaseKvmCPU::kvmNonMaskableInterrupt()
821 {
822 ++numInterrupts;
823 if (ioctl(KVM_NMI) == -1)
824 panic("KVM: Failed to deliver NMI to virtual CPU\n");
825 }
826
827 void
828 BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt)
829 {
830 ++numInterrupts;
831 if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1)
832 panic("KVM: Failed to deliver interrupt to virtual CPU\n");
833 }
834
835 void
836 BaseKvmCPU::getRegisters(struct kvm_regs &regs) const
837 {
838 if (ioctl(KVM_GET_REGS, &regs) == -1)
839 panic("KVM: Failed to get guest registers\n");
840 }
841
842 void
843 BaseKvmCPU::setRegisters(const struct kvm_regs &regs)
844 {
845 if (ioctl(KVM_SET_REGS, (void *)&regs) == -1)
846 panic("KVM: Failed to set guest registers\n");
847 }
848
849 void
850 BaseKvmCPU::getSpecialRegisters(struct kvm_sregs &regs) const
851 {
852 if (ioctl(KVM_GET_SREGS, &regs) == -1)
853 panic("KVM: Failed to get guest special registers\n");
854 }
855
856 void
857 BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs &regs)
858 {
859 if (ioctl(KVM_SET_SREGS, (void *)&regs) == -1)
860 panic("KVM: Failed to set guest special registers\n");
861 }
862
863 void
864 BaseKvmCPU::getFPUState(struct kvm_fpu &state) const
865 {
866 if (ioctl(KVM_GET_FPU, &state) == -1)
867 panic("KVM: Failed to get guest FPU state\n");
868 }
869
870 void
871 BaseKvmCPU::setFPUState(const struct kvm_fpu &state)
872 {
873 if (ioctl(KVM_SET_FPU, (void *)&state) == -1)
874 panic("KVM: Failed to set guest FPU state\n");
875 }
876
877
878 void
879 BaseKvmCPU::setOneReg(uint64_t id, const void *addr)
880 {
881 #ifdef KVM_SET_ONE_REG
882 struct kvm_one_reg reg;
883 reg.id = id;
884 reg.addr = (uint64_t)addr;
885
886 if (ioctl(KVM_SET_ONE_REG, &reg) == -1) {
887 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
888 id, errno);
889 }
890 #else
891 panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
892 #endif
893 }
894
895 void
896 BaseKvmCPU::getOneReg(uint64_t id, void *addr) const
897 {
898 #ifdef KVM_GET_ONE_REG
899 struct kvm_one_reg reg;
900 reg.id = id;
901 reg.addr = (uint64_t)addr;
902
903 if (ioctl(KVM_GET_ONE_REG, &reg) == -1) {
904 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
905 id, errno);
906 }
907 #else
908 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
909 #endif
910 }
911
912 std::string
913 BaseKvmCPU::getAndFormatOneReg(uint64_t id) const
914 {
915 #ifdef KVM_GET_ONE_REG
916 std::ostringstream ss;
917
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); \
924 ss << value; \
925 } break
926
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]); \
934 ccprintf(ss, "]"); \
935 } break
936
937 switch (id & KVM_REG_SIZE_MASK) {
938 HANDLE_INTTYPE(8);
939 HANDLE_INTTYPE(16);
940 HANDLE_INTTYPE(32);
941 HANDLE_INTTYPE(64);
942 HANDLE_ARRAY(128);
943 HANDLE_ARRAY(256);
944 HANDLE_ARRAY(512);
945 HANDLE_ARRAY(1024);
946 default:
947 ss << "??";
948 }
949
950 #undef HANDLE_INTTYPE
951 #undef HANDLE_ARRAY
952
953 return ss.str();
954 #else
955 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
956 #endif
957 }
958
959 void
960 BaseKvmCPU::syncThreadContext()
961 {
962 if (!kvmStateDirty)
963 return;
964
965 assert(!threadContextDirty);
966
967 updateThreadContext();
968 kvmStateDirty = false;
969 }
970
971 void
972 BaseKvmCPU::syncKvmState()
973 {
974 if (!threadContextDirty)
975 return;
976
977 assert(!kvmStateDirty);
978
979 updateKvmState();
980 threadContextDirty = false;
981 }
982
983 Tick
984 BaseKvmCPU::handleKvmExit()
985 {
986 DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason);
987 assert(_status == RunningService);
988
989 // Switch into the running state by default. Individual handlers
990 // can override this.
991 _status = Running;
992 switch (_kvmRun->exit_reason) {
993 case KVM_EXIT_UNKNOWN:
994 return handleKvmExitUnknown();
995
996 case KVM_EXIT_EXCEPTION:
997 return handleKvmExitException();
998
999 case KVM_EXIT_IO:
1000 {
1001 ++numIO;
1002 Tick ticks = handleKvmExitIO();
1003 _status = dataPort.nextIOState();
1004 return ticks;
1005 }
1006
1007 case KVM_EXIT_HYPERCALL:
1008 ++numHypercalls;
1009 return handleKvmExitHypercall();
1010
1011 case KVM_EXIT_HLT:
1012 /* The guest has halted and is waiting for interrupts */
1013 DPRINTF(Kvm, "handleKvmExitHalt\n");
1014 ++numHalt;
1015
1016 // Suspend the thread until the next interrupt arrives
1017 thread->suspend();
1018
1019 // This is actually ignored since the thread is suspended.
1020 return 0;
1021
1022 case KVM_EXIT_MMIO:
1023 {
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);
1028
1029 ++numMMIO;
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();
1036 return ticks;
1037 }
1038
1039 case KVM_EXIT_IRQ_WINDOW_OPEN:
1040 return handleKvmExitIRQWindowOpen();
1041
1042 case KVM_EXIT_FAIL_ENTRY:
1043 return handleKvmExitFailEntry();
1044
1045 case KVM_EXIT_INTR:
1046 /* KVM was interrupted by a signal, restart it in the next
1047 * tick. */
1048 return 0;
1049
1050 case KVM_EXIT_INTERNAL_ERROR:
1051 panic("KVM: Internal error (suberror: %u)\n",
1052 _kvmRun->internal.suberror);
1053
1054 default:
1055 dump();
1056 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason);
1057 }
1058 }
1059
1060 Tick
1061 BaseKvmCPU::handleKvmExitIO()
1062 {
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);
1066 }
1067
1068 Tick
1069 BaseKvmCPU::handleKvmExitHypercall()
1070 {
1071 panic("KVM: Unhandled hypercall\n");
1072 }
1073
1074 Tick
1075 BaseKvmCPU::handleKvmExitIRQWindowOpen()
1076 {
1077 warn("KVM: Unhandled IRQ window.\n");
1078 return 0;
1079 }
1080
1081
1082 Tick
1083 BaseKvmCPU::handleKvmExitUnknown()
1084 {
1085 dump();
1086 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
1087 _kvmRun->hw.hardware_exit_reason);
1088 }
1089
1090 Tick
1091 BaseKvmCPU::handleKvmExitException()
1092 {
1093 dump();
1094 panic("KVM: Got exception when starting vCPU "
1095 "(exception: %u, error_code: %u)\n",
1096 _kvmRun->ex.exception, _kvmRun->ex.error_code);
1097 }
1098
1099 Tick
1100 BaseKvmCPU::handleKvmExitFailEntry()
1101 {
1102 dump();
1103 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
1104 _kvmRun->fail_entry.hardware_entry_failure_reason);
1105 }
1106
1107 Tick
1108 BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write)
1109 {
1110 ThreadContext *tc(thread->getTC());
1111 syncThreadContext();
1112
1113 RequestPtr mmio_req = std::make_shared<Request>(
1114 paddr, size, Request::UNCACHEABLE, dataMasterId());
1115
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
1120 // interface.
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());
1125
1126
1127 const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq);
1128 PacketPtr pkt = new Packet(mmio_req, cmd);
1129 pkt->dataStatic(data);
1130
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;
1139 delete pkt;
1140 return clockPeriod() * ipr_delay;
1141 } else {
1142 // Temporarily lock and migrate to the device event queue to
1143 // prevent races in multi-core mode.
1144 EventQueue::ScopedMigration migrate(deviceEventQueue());
1145
1146 return dataPort.submitIO(pkt);
1147 }
1148 }
1149
1150 void
1151 BaseKvmCPU::setSignalMask(const sigset_t *mask)
1152 {
1153 std::unique_ptr<struct kvm_signal_mask> kvm_mask;
1154
1155 if (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);
1162 kvm_mask->len = 8;
1163 memcpy(kvm_mask->sigset, mask, kvm_mask->len);
1164 }
1165
1166 if (ioctl(KVM_SET_SIGNAL_MASK, (void *)kvm_mask.get()) == -1)
1167 panic("KVM: Failed to set vCPU signal mask (errno: %i)\n",
1168 errno);
1169 }
1170
1171 int
1172 BaseKvmCPU::ioctl(int request, long p1) const
1173 {
1174 if (vcpuFD == -1)
1175 panic("KVM: CPU ioctl called before initialization\n");
1176
1177 return ::ioctl(vcpuFD, request, p1);
1178 }
1179
1180 Tick
1181 BaseKvmCPU::flushCoalescedMMIO()
1182 {
1183 if (!mmioRing)
1184 return 0;
1185
1186 DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n");
1187
1188 // TODO: We might need to do synchronization when we start to
1189 // support multiple CPUs
1190 Tick ticks(0);
1191 while (mmioRing->first != mmioRing->last) {
1192 struct kvm_coalesced_mmio &ent(
1193 mmioRing->coalesced_mmio[mmioRing->first]);
1194
1195 DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
1196 ent.phys_addr, ent.len);
1197
1198 ++numCoalescedMMIO;
1199 ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true);
1200
1201 mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX;
1202 }
1203
1204 return ticks;
1205 }
1206
1207 /**
1208 * Dummy handler for KVM kick signals.
1209 *
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
1215 * handling.
1216 */
1217 static void
1218 onKickSignal(int signo, siginfo_t *si, void *data)
1219 {
1220 }
1221
1222 void
1223 BaseKvmCPU::setupSignalHandler()
1224 {
1225 struct sigaction sa;
1226
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");
1232
1233 sigset_t sigset;
1234 if (pthread_sigmask(SIG_BLOCK, NULL, &sigset) == -1)
1235 panic("KVM: Failed get signal mask\n");
1236
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);
1243
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");
1249 }
1250
1251 bool
1252 BaseKvmCPU::discardPendingSignal(int signum) const
1253 {
1254 int discardedSignal;
1255
1256 // Setting the timeout to zero causes sigtimedwait to return
1257 // immediately.
1258 struct timespec timeout;
1259 timeout.tv_sec = 0;
1260 timeout.tv_nsec = 0;
1261
1262 sigset_t sigset;
1263 sigemptyset(&sigset);
1264 sigaddset(&sigset, signum);
1265
1266 do {
1267 discardedSignal = sigtimedwait(&sigset, NULL, &timeout);
1268 } while (discardedSignal == -1 && errno == EINTR);
1269
1270 if (discardedSignal == signum)
1271 return true;
1272 else if (discardedSignal == -1 && errno == EAGAIN)
1273 return false;
1274 else
1275 panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n",
1276 discardedSignal, errno);
1277 }
1278
1279 void
1280 BaseKvmCPU::setupCounters()
1281 {
1282 DPRINTF(Kvm, "Attaching cycle counter...\n");
1283 PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE,
1284 PERF_COUNT_HW_CPU_CYCLES);
1285 cfgCycles.disabled(true)
1286 .pinned(true);
1287
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);
1293
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)
1301 .samplePeriod(42);
1302 }
1303
1304 hwCycles.attach(cfgCycles,
1305 0); // TID (0 => currentThread)
1306
1307 setupInstCounter();
1308 }
1309
1310 bool
1311 BaseKvmCPU::tryDrain()
1312 {
1313 if (drainState() != DrainState::Draining)
1314 return false;
1315
1316 if (!archIsDrained()) {
1317 DPRINTF(Drain, "tryDrain: Architecture code is not ready.\n");
1318 return false;
1319 }
1320
1321 if (_status == Idle || _status == Running) {
1322 DPRINTF(Drain,
1323 "tryDrain: CPU transitioned into the Idle state, drain done\n");
1324 signalDrainDone();
1325 return true;
1326 } else {
1327 DPRINTF(Drain, "tryDrain: CPU not ready.\n");
1328 return false;
1329 }
1330 }
1331
1332 void
1333 BaseKvmCPU::ioctlRun()
1334 {
1335 if (ioctl(KVM_RUN) == -1) {
1336 if (errno != EINTR)
1337 panic("KVM: Failed to start virtual CPU (errno: %i)\n",
1338 errno);
1339 }
1340 }
1341
1342 void
1343 BaseKvmCPU::setupInstStop()
1344 {
1345 if (thread->comInstEventQueue.empty()) {
1346 setupInstCounter(0);
1347 } else {
1348 Tick next = thread->comInstEventQueue.nextTick();
1349 assert(next > ctrInsts);
1350 setupInstCounter(next - ctrInsts);
1351 }
1352 }
1353
1354 void
1355 BaseKvmCPU::setupInstCounter(uint64_t period)
1356 {
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())
1360 return;
1361
1362 PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE,
1363 PERF_COUNT_HW_INSTRUCTIONS);
1364
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);
1370
1371 if (period) {
1372 // Setup a sampling counter if that has been requested.
1373 cfgInstructions.wakeupEvents(1)
1374 .samplePeriod(period);
1375 }
1376
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)
1384 hwCycles);
1385
1386 if (period)
1387 hwInstructions.enableSignals(KVM_KICK_SIGNAL);
1388
1389 activeInstPeriod = period;
1390 }