base.cc revision 11435:0f1b46dde3fa
1/*
2 * Copyright (c) 2012, 2015 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 * Authors: Andreas Sandberg
38 */
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 "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"
60
61#include <signal.h>
62
63/* Used by some KVM macros */
64#define PAGE_SIZE pageSize
65
66BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params)
67    : BaseCPU(params),
68      vm(*params->kvmVM),
69      _status(Idle),
70      dataPort(name() + ".dcache_port", this),
71      instPort(name() + ".icache_port", this),
72      alwaysSyncTC(params->alwaysSyncTC),
73      threadContextDirty(true),
74      kvmStateDirty(false),
75      vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
76      _kvmRun(NULL), mmioRing(NULL),
77      pageSize(sysconf(_SC_PAGE_SIZE)),
78      tickEvent(*this),
79      activeInstPeriod(0),
80      perfControlledByTimer(params->usePerfOverflow),
81      hostFactor(params->hostFactor),
82      ctrInsts(0)
83{
84    if (pageSize == -1)
85        panic("KVM: Failed to determine host page size (%i)\n",
86              errno);
87
88    if (FullSystem)
89        thread = new SimpleThread(this, 0, params->system, params->itb, params->dtb,
90                                  params->isa[0]);
91    else
92        thread = new SimpleThread(this, /* thread_num */ 0, params->system,
93                                  params->workload[0], params->itb,
94                                  params->dtb, params->isa[0]);
95
96    thread->setStatus(ThreadContext::Halted);
97    tc = thread->getTC();
98    threadContexts.push_back(tc);
99}
100
101BaseKvmCPU::~BaseKvmCPU()
102{
103    if (_kvmRun)
104        munmap(_kvmRun, vcpuMMapSize);
105    close(vcpuFD);
106}
107
108void
109BaseKvmCPU::init()
110{
111    BaseCPU::init();
112
113    if (numThreads != 1)
114        fatal("KVM: Multithreading not supported");
115
116    tc->initMemProxies(tc);
117
118    // initialize CPU, including PC
119    if (FullSystem && !switchedOut())
120        TheISA::initCPU(tc, tc->contextId());
121}
122
123void
124BaseKvmCPU::startup()
125{
126    const BaseKvmCPUParams * const p(
127        dynamic_cast<const BaseKvmCPUParams *>(params()));
128
129    Kvm &kvm(*vm.kvm);
130
131    BaseCPU::startup();
132
133    assert(vcpuFD == -1);
134
135    // Tell the VM that a CPU is about to start.
136    vm.cpuStartup();
137
138    // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
139    // not guaranteed that the parent KVM VM has initialized at that
140    // point. Initialize virtual CPUs here instead.
141    vcpuFD = vm.createVCPU(vcpuID);
142
143    // Map the KVM run structure */
144    vcpuMMapSize = kvm.getVCPUMMapSize();
145    _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize,
146                                     PROT_READ | PROT_WRITE, MAP_SHARED,
147                                     vcpuFD, 0);
148    if (_kvmRun == MAP_FAILED)
149        panic("KVM: Failed to map run data structure\n");
150
151    // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
152    // available. The offset into the KVM's communication page is
153    // provided by the coalesced MMIO capability.
154    int mmioOffset(kvm.capCoalescedMMIO());
155    if (!p->useCoalescedMMIO) {
156        inform("KVM: Coalesced MMIO disabled by config.\n");
157    } else if (mmioOffset) {
158        inform("KVM: Coalesced IO available\n");
159        mmioRing = (struct kvm_coalesced_mmio_ring *)(
160            (char *)_kvmRun + (mmioOffset * pageSize));
161    } else {
162        inform("KVM: Coalesced not supported by host OS\n");
163    }
164
165    thread->startup();
166
167    Event *startupEvent(
168        new EventWrapper<BaseKvmCPU,
169                         &BaseKvmCPU::startupThread>(this, true));
170    schedule(startupEvent, curTick());
171}
172
173void
174BaseKvmCPU::startupThread()
175{
176    // Do thread-specific initialization. We need to setup signal
177    // delivery for counters and timers from within the thread that
178    // will execute the event queue to ensure that signals are
179    // delivered to the right threads.
180    const BaseKvmCPUParams * const p(
181        dynamic_cast<const BaseKvmCPUParams *>(params()));
182
183    vcpuThread = pthread_self();
184
185    // Setup signal handlers. This has to be done after the vCPU is
186    // created since it manipulates the vCPU signal mask.
187    setupSignalHandler();
188
189    setupCounters();
190
191    if (p->usePerfOverflow)
192        runTimer.reset(new PerfKvmTimer(hwCycles,
193                                        KVM_KICK_SIGNAL,
194                                        p->hostFactor,
195                                        p->hostFreq));
196    else
197        runTimer.reset(new PosixKvmTimer(KVM_KICK_SIGNAL, CLOCK_MONOTONIC,
198                                         p->hostFactor,
199                                         p->hostFreq));
200
201}
202
203void
204BaseKvmCPU::regStats()
205{
206    using namespace Stats;
207
208    BaseCPU::regStats();
209
210    numInsts
211        .name(name() + ".committedInsts")
212        .desc("Number of instructions committed")
213        ;
214
215    numVMExits
216        .name(name() + ".numVMExits")
217        .desc("total number of KVM exits")
218        ;
219
220    numVMHalfEntries
221        .name(name() + ".numVMHalfEntries")
222        .desc("number of KVM entries to finalize pending operations")
223        ;
224
225    numExitSignal
226        .name(name() + ".numExitSignal")
227        .desc("exits due to signal delivery")
228        ;
229
230    numMMIO
231        .name(name() + ".numMMIO")
232        .desc("number of VM exits due to memory mapped IO")
233        ;
234
235    numCoalescedMMIO
236        .name(name() + ".numCoalescedMMIO")
237        .desc("number of coalesced memory mapped IO requests")
238        ;
239
240    numIO
241        .name(name() + ".numIO")
242        .desc("number of VM exits due to legacy IO")
243        ;
244
245    numHalt
246        .name(name() + ".numHalt")
247        .desc("number of VM exits due to wait for interrupt instructions")
248        ;
249
250    numInterrupts
251        .name(name() + ".numInterrupts")
252        .desc("number of interrupts delivered")
253        ;
254
255    numHypercalls
256        .name(name() + ".numHypercalls")
257        .desc("number of hypercalls")
258        ;
259}
260
261void
262BaseKvmCPU::serializeThread(CheckpointOut &cp, ThreadID tid) const
263{
264    if (DTRACE(Checkpoint)) {
265        DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid);
266        dump();
267    }
268
269    assert(tid == 0);
270    assert(_status == Idle);
271    thread->serialize(cp);
272}
273
274void
275BaseKvmCPU::unserializeThread(CheckpointIn &cp, ThreadID tid)
276{
277    DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid);
278
279    assert(tid == 0);
280    assert(_status == Idle);
281    thread->unserialize(cp);
282    threadContextDirty = true;
283}
284
285DrainState
286BaseKvmCPU::drain()
287{
288    if (switchedOut())
289        return DrainState::Drained;
290
291    DPRINTF(Drain, "BaseKvmCPU::drain\n");
292    switch (_status) {
293      case Running:
294        // The base KVM code is normally ready when it is in the
295        // Running state, but the architecture specific code might be
296        // of a different opinion. This may happen when the CPU been
297        // notified of an event that hasn't been accepted by the vCPU
298        // yet.
299        if (!archIsDrained())
300            return DrainState::Draining;
301
302        // The state of the CPU is consistent, so we don't need to do
303        // anything special to drain it. We simply de-schedule the
304        // tick event and enter the Idle state to prevent nasty things
305        // like MMIOs from happening.
306        if (tickEvent.scheduled())
307            deschedule(tickEvent);
308        _status = Idle;
309
310        /** FALLTHROUGH */
311      case Idle:
312        // Idle, no need to drain
313        assert(!tickEvent.scheduled());
314
315        // Sync the thread context here since we'll need it when we
316        // switch CPUs or checkpoint the CPU.
317        syncThreadContext();
318
319        return DrainState::Drained;
320
321      case RunningServiceCompletion:
322        // The CPU has just requested a service that was handled in
323        // the RunningService state, but the results have still not
324        // been reported to the CPU. Now, we /could/ probably just
325        // update the register state ourselves instead of letting KVM
326        // handle it, but that would be tricky. Instead, we enter KVM
327        // and let it do its stuff.
328        DPRINTF(Drain, "KVM CPU is waiting for service completion, "
329                "requesting drain.\n");
330        return DrainState::Draining;
331
332      case RunningService:
333        // We need to drain since the CPU is waiting for service (e.g., MMIOs)
334        DPRINTF(Drain, "KVM CPU is waiting for service, requesting drain.\n");
335        return DrainState::Draining;
336
337      default:
338        panic("KVM: Unhandled CPU state in drain()\n");
339        return DrainState::Drained;
340    }
341}
342
343void
344BaseKvmCPU::drainResume()
345{
346    assert(!tickEvent.scheduled());
347
348    // We might have been switched out. In that case, we don't need to
349    // do anything.
350    if (switchedOut())
351        return;
352
353    DPRINTF(Kvm, "drainResume\n");
354    verifyMemoryMode();
355
356    // The tick event is de-scheduled as a part of the draining
357    // process. Re-schedule it if the thread context is active.
358    if (tc->status() == ThreadContext::Active) {
359        schedule(tickEvent, nextCycle());
360        _status = Running;
361    } else {
362        _status = Idle;
363    }
364}
365
366void
367BaseKvmCPU::notifyFork()
368{
369    // We should have drained prior to forking, which means that the
370    // tick event shouldn't be scheduled and the CPU is idle.
371    assert(!tickEvent.scheduled());
372    assert(_status == Idle);
373
374    if (vcpuFD != -1) {
375        if (close(vcpuFD) == -1)
376            warn("kvm CPU: notifyFork failed to close vcpuFD\n");
377
378        if (_kvmRun)
379            munmap(_kvmRun, vcpuMMapSize);
380
381        vcpuFD = -1;
382        _kvmRun = NULL;
383
384        hwInstructions.detach();
385        hwCycles.detach();
386    }
387}
388
389void
390BaseKvmCPU::switchOut()
391{
392    DPRINTF(Kvm, "switchOut\n");
393
394    BaseCPU::switchOut();
395
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
398    // idle.
399    assert(!tickEvent.scheduled());
400    assert(_status == Idle);
401}
402
403void
404BaseKvmCPU::takeOverFrom(BaseCPU *cpu)
405{
406    DPRINTF(Kvm, "takeOverFrom\n");
407
408    BaseCPU::takeOverFrom(cpu);
409
410    // We should have drained prior to executing a switchOut, which
411    // means that the tick event shouldn't be scheduled and the CPU is
412    // idle.
413    assert(!tickEvent.scheduled());
414    assert(_status == Idle);
415    assert(threadContexts.size() == 1);
416
417    // Force an update of the KVM state here instead of flagging the
418    // TC as dirty. This is not ideal from a performance point of
419    // view, but it makes debugging easier as it allows meaningful KVM
420    // state to be dumped before and after a takeover.
421    updateKvmState();
422    threadContextDirty = false;
423}
424
425void
426BaseKvmCPU::verifyMemoryMode() const
427{
428    if (!(system->isAtomicMode() && system->bypassCaches())) {
429        fatal("The KVM-based CPUs requires the memory system to be in the "
430              "'atomic_noncaching' mode.\n");
431    }
432}
433
434void
435BaseKvmCPU::wakeup(ThreadID tid)
436{
437    DPRINTF(Kvm, "wakeup()\n");
438    // This method might have been called from another
439    // context. Migrate to this SimObject's event queue when
440    // delivering the wakeup signal.
441    EventQueue::ScopedMigration migrate(eventQueue());
442
443    // Kick the vCPU to get it to come out of KVM.
444    kick();
445
446    if (thread->status() != ThreadContext::Suspended)
447        return;
448
449    thread->activate();
450}
451
452void
453BaseKvmCPU::activateContext(ThreadID thread_num)
454{
455    DPRINTF(Kvm, "ActivateContext %d\n", thread_num);
456
457    assert(thread_num == 0);
458    assert(thread);
459
460    assert(_status == Idle);
461    assert(!tickEvent.scheduled());
462
463    numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend);
464
465    schedule(tickEvent, clockEdge(Cycles(0)));
466    _status = Running;
467}
468
469
470void
471BaseKvmCPU::suspendContext(ThreadID thread_num)
472{
473    DPRINTF(Kvm, "SuspendContext %d\n", thread_num);
474
475    assert(thread_num == 0);
476    assert(thread);
477
478    if (_status == Idle)
479        return;
480
481    assert(_status == Running || _status == RunningServiceCompletion);
482
483    // The tick event may no be scheduled if the quest has requested
484    // the monitor to wait for interrupts. The normal CPU models can
485    // get their tick events descheduled by quiesce instructions, but
486    // that can't happen here.
487    if (tickEvent.scheduled())
488        deschedule(tickEvent);
489
490    _status = Idle;
491}
492
493void
494BaseKvmCPU::deallocateContext(ThreadID thread_num)
495{
496    // for now, these are equivalent
497    suspendContext(thread_num);
498}
499
500void
501BaseKvmCPU::haltContext(ThreadID thread_num)
502{
503    // for now, these are equivalent
504    suspendContext(thread_num);
505}
506
507ThreadContext *
508BaseKvmCPU::getContext(int tn)
509{
510    assert(tn == 0);
511    syncThreadContext();
512    return tc;
513}
514
515
516Counter
517BaseKvmCPU::totalInsts() const
518{
519    return ctrInsts;
520}
521
522Counter
523BaseKvmCPU::totalOps() const
524{
525    hack_once("Pretending totalOps is equivalent to totalInsts()\n");
526    return ctrInsts;
527}
528
529void
530BaseKvmCPU::dump() const
531{
532    inform("State dumping not implemented.");
533}
534
535void
536BaseKvmCPU::tick()
537{
538    Tick delay(0);
539    assert(_status != Idle);
540
541    switch (_status) {
542      case RunningService:
543        // handleKvmExit() will determine the next state of the CPU
544        delay = handleKvmExit();
545
546        if (tryDrain())
547            _status = Idle;
548        break;
549
550      case RunningServiceCompletion:
551      case Running: {
552          const uint64_t nextInstEvent(
553              !comInstEventQueue[0]->empty() ?
554              comInstEventQueue[0]->nextTick() : UINT64_MAX);
555          // Enter into KVM and complete pending IO instructions if we
556          // have an instruction event pending.
557          const Tick ticksToExecute(
558              nextInstEvent > ctrInsts ?
559              curEventQueue()->nextTick() - curTick() : 0);
560
561          if (alwaysSyncTC)
562              threadContextDirty = true;
563
564          // We might need to update the KVM state.
565          syncKvmState();
566
567          // Setup any pending instruction count breakpoints using
568          // PerfEvent if we are going to execute more than just an IO
569          // completion.
570          if (ticksToExecute > 0)
571              setupInstStop();
572
573          DPRINTF(KvmRun, "Entering KVM...\n");
574          if (drainState() == DrainState::Draining) {
575              // Force an immediate exit from KVM after completing
576              // pending operations. The architecture-specific code
577              // takes care to run until it is in a state where it can
578              // safely be drained.
579              delay = kvmRunDrain();
580          } else {
581              delay = kvmRun(ticksToExecute);
582          }
583
584          // The CPU might have been suspended before entering into
585          // KVM. Assume that the CPU was suspended /before/ entering
586          // into KVM and skip the exit handling.
587          if (_status == Idle)
588              break;
589
590          // Entering into KVM implies that we'll have to reload the thread
591          // context from KVM if we want to access it. Flag the KVM state as
592          // dirty with respect to the cached thread context.
593          kvmStateDirty = true;
594
595          if (alwaysSyncTC)
596              syncThreadContext();
597
598          // Enter into the RunningService state unless the
599          // simulation was stopped by a timer.
600          if (_kvmRun->exit_reason !=  KVM_EXIT_INTR) {
601              _status = RunningService;
602          } else {
603              ++numExitSignal;
604              _status = Running;
605          }
606
607          // Service any pending instruction events. The vCPU should
608          // have exited in time for the event using the instruction
609          // counter configured by setupInstStop().
610          comInstEventQueue[0]->serviceEvents(ctrInsts);
611          system->instEventQueue.serviceEvents(system->totalNumInsts);
612
613          if (tryDrain())
614              _status = Idle;
615      } break;
616
617      default:
618        panic("BaseKvmCPU entered tick() in an illegal state (%i)\n",
619              _status);
620    }
621
622    // Schedule a new tick if we are still running
623    if (_status != Idle)
624        schedule(tickEvent, clockEdge(ticksToCycles(delay)));
625}
626
627Tick
628BaseKvmCPU::kvmRunDrain()
629{
630    // By default, the only thing we need to drain is a pending IO
631    // operation which assumes that we are in the
632    // RunningServiceCompletion state.
633    assert(_status == RunningServiceCompletion);
634
635    // Deliver the data from the pending IO operation and immediately
636    // exit.
637    return kvmRun(0);
638}
639
640uint64_t
641BaseKvmCPU::getHostCycles() const
642{
643    return hwCycles.read();
644}
645
646Tick
647BaseKvmCPU::kvmRun(Tick ticks)
648{
649    Tick ticksExecuted;
650    fatal_if(vcpuFD == -1,
651             "Trying to run a KVM CPU in a forked child process. "
652             "This is not supported.\n");
653    DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks);
654
655    if (ticks == 0) {
656        // Settings ticks == 0 is a special case which causes an entry
657        // into KVM that finishes pending operations (e.g., IO) and
658        // then immediately exits.
659        DPRINTF(KvmRun, "KVM: Delivering IO without full guest entry\n");
660
661        ++numVMHalfEntries;
662
663        // Send a KVM_KICK_SIGNAL to the vCPU thread (i.e., this
664        // thread). The KVM control signal is masked while executing
665        // in gem5 and gets unmasked temporarily as when entering
666        // KVM. See setSignalMask() and setupSignalHandler().
667        kick();
668
669        // Start the vCPU. KVM will check for signals after completing
670        // pending operations (IO). Since the KVM_KICK_SIGNAL is
671        // pending, this forces an immediate exit to gem5 again. We
672        // don't bother to setup timers since this shouldn't actually
673        // execute any code (other than completing half-executed IO
674        // instructions) in the guest.
675        ioctlRun();
676
677        // We always execute at least one cycle to prevent the
678        // BaseKvmCPU::tick() to be rescheduled on the same tick
679        // twice.
680        ticksExecuted = clockPeriod();
681    } else {
682        // This method is executed as a result of a tick event. That
683        // means that the event queue will be locked when entering the
684        // method. We temporarily unlock the event queue to allow
685        // other threads to steal control of this thread to inject
686        // interrupts. They will typically lock the queue and then
687        // force an exit from KVM by kicking the vCPU.
688        EventQueue::ScopedRelease release(curEventQueue());
689
690        if (ticks < runTimer->resolution()) {
691            DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
692                    ticks, runTimer->resolution());
693            ticks = runTimer->resolution();
694        }
695
696        // Get hardware statistics after synchronizing contexts. The KVM
697        // state update might affect guest cycle counters.
698        uint64_t baseCycles(getHostCycles());
699        uint64_t baseInstrs(hwInstructions.read());
700
701        // Arm the run timer and start the cycle timer if it isn't
702        // controlled by the overflow timer. Starting/stopping the cycle
703        // timer automatically starts the other perf timers as they are in
704        // the same counter group.
705        runTimer->arm(ticks);
706        if (!perfControlledByTimer)
707            hwCycles.start();
708
709        ioctlRun();
710
711        runTimer->disarm();
712        if (!perfControlledByTimer)
713            hwCycles.stop();
714
715        // The control signal may have been delivered after we exited
716        // from KVM. It will be pending in that case since it is
717        // masked when we aren't executing in KVM. Discard it to make
718        // sure we don't deliver it immediately next time we try to
719        // enter into KVM.
720        discardPendingSignal(KVM_KICK_SIGNAL);
721
722        const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles);
723        const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor);
724        const uint64_t instsExecuted(hwInstructions.read() - baseInstrs);
725        ticksExecuted = runTimer->ticksFromHostCycles(hostCyclesExecuted);
726
727        /* Update statistics */
728        numCycles += simCyclesExecuted;;
729        numInsts += instsExecuted;
730        ctrInsts += instsExecuted;
731        system->totalNumInsts += instsExecuted;
732
733        DPRINTF(KvmRun,
734                "KVM: Executed %i instructions in %i cycles "
735                "(%i ticks, sim cycles: %i).\n",
736                instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted);
737    }
738
739    ++numVMExits;
740
741    return ticksExecuted + flushCoalescedMMIO();
742}
743
744void
745BaseKvmCPU::kvmNonMaskableInterrupt()
746{
747    ++numInterrupts;
748    if (ioctl(KVM_NMI) == -1)
749        panic("KVM: Failed to deliver NMI to virtual CPU\n");
750}
751
752void
753BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt)
754{
755    ++numInterrupts;
756    if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1)
757        panic("KVM: Failed to deliver interrupt to virtual CPU\n");
758}
759
760void
761BaseKvmCPU::getRegisters(struct kvm_regs &regs) const
762{
763    if (ioctl(KVM_GET_REGS, &regs) == -1)
764        panic("KVM: Failed to get guest registers\n");
765}
766
767void
768BaseKvmCPU::setRegisters(const struct kvm_regs &regs)
769{
770    if (ioctl(KVM_SET_REGS, (void *)&regs) == -1)
771        panic("KVM: Failed to set guest registers\n");
772}
773
774void
775BaseKvmCPU::getSpecialRegisters(struct kvm_sregs &regs) const
776{
777    if (ioctl(KVM_GET_SREGS, &regs) == -1)
778        panic("KVM: Failed to get guest special registers\n");
779}
780
781void
782BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs &regs)
783{
784    if (ioctl(KVM_SET_SREGS, (void *)&regs) == -1)
785        panic("KVM: Failed to set guest special registers\n");
786}
787
788void
789BaseKvmCPU::getFPUState(struct kvm_fpu &state) const
790{
791    if (ioctl(KVM_GET_FPU, &state) == -1)
792        panic("KVM: Failed to get guest FPU state\n");
793}
794
795void
796BaseKvmCPU::setFPUState(const struct kvm_fpu &state)
797{
798    if (ioctl(KVM_SET_FPU, (void *)&state) == -1)
799        panic("KVM: Failed to set guest FPU state\n");
800}
801
802
803void
804BaseKvmCPU::setOneReg(uint64_t id, const void *addr)
805{
806#ifdef KVM_SET_ONE_REG
807    struct kvm_one_reg reg;
808    reg.id = id;
809    reg.addr = (uint64_t)addr;
810
811    if (ioctl(KVM_SET_ONE_REG, &reg) == -1) {
812        panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
813              id, errno);
814    }
815#else
816    panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
817#endif
818}
819
820void
821BaseKvmCPU::getOneReg(uint64_t id, void *addr) const
822{
823#ifdef KVM_GET_ONE_REG
824    struct kvm_one_reg reg;
825    reg.id = id;
826    reg.addr = (uint64_t)addr;
827
828    if (ioctl(KVM_GET_ONE_REG, &reg) == -1) {
829        panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
830              id, errno);
831    }
832#else
833    panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
834#endif
835}
836
837std::string
838BaseKvmCPU::getAndFormatOneReg(uint64_t id) const
839{
840#ifdef KVM_GET_ONE_REG
841    std::ostringstream ss;
842
843    ss.setf(std::ios::hex, std::ios::basefield);
844    ss.setf(std::ios::showbase);
845#define HANDLE_INTTYPE(len)                      \
846    case KVM_REG_SIZE_U ## len: {                \
847        uint ## len ## _t value;                 \
848        getOneReg(id, &value);                   \
849        ss << value;                             \
850    }  break
851
852#define HANDLE_ARRAY(len)                               \
853    case KVM_REG_SIZE_U ## len: {                       \
854        uint8_t value[len / 8];                         \
855        getOneReg(id, value);                           \
856        ccprintf(ss, "[0x%x", value[0]);                \
857        for (int i = 1; i < len  / 8; ++i)              \
858            ccprintf(ss, ", 0x%x", value[i]);           \
859        ccprintf(ss, "]");                              \
860      } break
861
862    switch (id & KVM_REG_SIZE_MASK) {
863        HANDLE_INTTYPE(8);
864        HANDLE_INTTYPE(16);
865        HANDLE_INTTYPE(32);
866        HANDLE_INTTYPE(64);
867        HANDLE_ARRAY(128);
868        HANDLE_ARRAY(256);
869        HANDLE_ARRAY(512);
870        HANDLE_ARRAY(1024);
871      default:
872        ss << "??";
873    }
874
875#undef HANDLE_INTTYPE
876#undef HANDLE_ARRAY
877
878    return ss.str();
879#else
880    panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
881#endif
882}
883
884void
885BaseKvmCPU::syncThreadContext()
886{
887    if (!kvmStateDirty)
888        return;
889
890    assert(!threadContextDirty);
891
892    updateThreadContext();
893    kvmStateDirty = false;
894}
895
896void
897BaseKvmCPU::syncKvmState()
898{
899    if (!threadContextDirty)
900        return;
901
902    assert(!kvmStateDirty);
903
904    updateKvmState();
905    threadContextDirty = false;
906}
907
908Tick
909BaseKvmCPU::handleKvmExit()
910{
911    DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason);
912    assert(_status == RunningService);
913
914    // Switch into the running state by default. Individual handlers
915    // can override this.
916    _status = Running;
917    switch (_kvmRun->exit_reason) {
918      case KVM_EXIT_UNKNOWN:
919        return handleKvmExitUnknown();
920
921      case KVM_EXIT_EXCEPTION:
922        return handleKvmExitException();
923
924      case KVM_EXIT_IO:
925        _status = RunningServiceCompletion;
926        ++numIO;
927        return handleKvmExitIO();
928
929      case KVM_EXIT_HYPERCALL:
930        ++numHypercalls;
931        return handleKvmExitHypercall();
932
933      case KVM_EXIT_HLT:
934        /* The guest has halted and is waiting for interrupts */
935        DPRINTF(Kvm, "handleKvmExitHalt\n");
936        ++numHalt;
937
938        // Suspend the thread until the next interrupt arrives
939        thread->suspend();
940
941        // This is actually ignored since the thread is suspended.
942        return 0;
943
944      case KVM_EXIT_MMIO:
945        _status = RunningServiceCompletion;
946        /* Service memory mapped IO requests */
947        DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
948                _kvmRun->mmio.is_write,
949                _kvmRun->mmio.phys_addr, _kvmRun->mmio.len);
950
951        ++numMMIO;
952        return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data,
953                            _kvmRun->mmio.len, _kvmRun->mmio.is_write);
954
955      case KVM_EXIT_IRQ_WINDOW_OPEN:
956        return handleKvmExitIRQWindowOpen();
957
958      case KVM_EXIT_FAIL_ENTRY:
959        return handleKvmExitFailEntry();
960
961      case KVM_EXIT_INTR:
962        /* KVM was interrupted by a signal, restart it in the next
963         * tick. */
964        return 0;
965
966      case KVM_EXIT_INTERNAL_ERROR:
967        panic("KVM: Internal error (suberror: %u)\n",
968              _kvmRun->internal.suberror);
969
970      default:
971        dump();
972        panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason);
973    }
974}
975
976Tick
977BaseKvmCPU::handleKvmExitIO()
978{
979    panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
980          _kvmRun->io.direction, _kvmRun->io.size,
981          _kvmRun->io.port, _kvmRun->io.count);
982}
983
984Tick
985BaseKvmCPU::handleKvmExitHypercall()
986{
987    panic("KVM: Unhandled hypercall\n");
988}
989
990Tick
991BaseKvmCPU::handleKvmExitIRQWindowOpen()
992{
993    warn("KVM: Unhandled IRQ window.\n");
994    return 0;
995}
996
997
998Tick
999BaseKvmCPU::handleKvmExitUnknown()
1000{
1001    dump();
1002    panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
1003          _kvmRun->hw.hardware_exit_reason);
1004}
1005
1006Tick
1007BaseKvmCPU::handleKvmExitException()
1008{
1009    dump();
1010    panic("KVM: Got exception when starting vCPU "
1011          "(exception: %u, error_code: %u)\n",
1012          _kvmRun->ex.exception, _kvmRun->ex.error_code);
1013}
1014
1015Tick
1016BaseKvmCPU::handleKvmExitFailEntry()
1017{
1018    dump();
1019    panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
1020          _kvmRun->fail_entry.hardware_entry_failure_reason);
1021}
1022
1023Tick
1024BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write)
1025{
1026    ThreadContext *tc(thread->getTC());
1027    syncThreadContext();
1028
1029    Request mmio_req(paddr, size, Request::UNCACHEABLE, dataMasterId());
1030    mmio_req.setContext(tc->contextId());
1031    // Some architectures do need to massage physical addresses a bit
1032    // before they are inserted into the memory system. This enables
1033    // APIC accesses on x86 and m5ops where supported through a MMIO
1034    // interface.
1035    BaseTLB::Mode tlb_mode(write ? BaseTLB::Write : BaseTLB::Read);
1036    Fault fault(tc->getDTBPtr()->finalizePhysical(&mmio_req, tc, tlb_mode));
1037    if (fault != NoFault)
1038        warn("Finalization of MMIO address failed: %s\n", fault->name());
1039
1040
1041    const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq);
1042    Packet pkt(&mmio_req, cmd);
1043    pkt.dataStatic(data);
1044
1045    if (mmio_req.isMmappedIpr()) {
1046        // We currently assume that there is no need to migrate to a
1047        // different event queue when doing IPRs. Currently, IPRs are
1048        // only used for m5ops, so it should be a valid assumption.
1049        const Cycles ipr_delay(write ?
1050                             TheISA::handleIprWrite(tc, &pkt) :
1051                             TheISA::handleIprRead(tc, &pkt));
1052        threadContextDirty = true;
1053        return clockPeriod() * ipr_delay;
1054    } else {
1055        // Temporarily lock and migrate to the event queue of the
1056        // VM. This queue is assumed to "own" all devices we need to
1057        // access if running in multi-core mode.
1058        EventQueue::ScopedMigration migrate(vm.eventQueue());
1059
1060        return dataPort.sendAtomic(&pkt);
1061    }
1062}
1063
1064void
1065BaseKvmCPU::setSignalMask(const sigset_t *mask)
1066{
1067    std::unique_ptr<struct kvm_signal_mask> kvm_mask;
1068
1069    if (mask) {
1070        kvm_mask.reset((struct kvm_signal_mask *)operator new(
1071                           sizeof(struct kvm_signal_mask) + sizeof(*mask)));
1072        // The kernel and the user-space headers have different ideas
1073        // about the size of sigset_t. This seems like a massive hack,
1074        // but is actually what qemu does.
1075        assert(sizeof(*mask) >= 8);
1076        kvm_mask->len = 8;
1077        memcpy(kvm_mask->sigset, mask, kvm_mask->len);
1078    }
1079
1080    if (ioctl(KVM_SET_SIGNAL_MASK, (void *)kvm_mask.get()) == -1)
1081        panic("KVM: Failed to set vCPU signal mask (errno: %i)\n",
1082              errno);
1083}
1084
1085int
1086BaseKvmCPU::ioctl(int request, long p1) const
1087{
1088    if (vcpuFD == -1)
1089        panic("KVM: CPU ioctl called before initialization\n");
1090
1091    return ::ioctl(vcpuFD, request, p1);
1092}
1093
1094Tick
1095BaseKvmCPU::flushCoalescedMMIO()
1096{
1097    if (!mmioRing)
1098        return 0;
1099
1100    DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n");
1101
1102    // TODO: We might need to do synchronization when we start to
1103    // support multiple CPUs
1104    Tick ticks(0);
1105    while (mmioRing->first != mmioRing->last) {
1106        struct kvm_coalesced_mmio &ent(
1107            mmioRing->coalesced_mmio[mmioRing->first]);
1108
1109        DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
1110                ent.phys_addr, ent.len);
1111
1112        ++numCoalescedMMIO;
1113        ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true);
1114
1115        mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX;
1116    }
1117
1118    return ticks;
1119}
1120
1121/**
1122 * Dummy handler for KVM kick signals.
1123 *
1124 * @note This function is usually not called since the kernel doesn't
1125 * seem to deliver signals when the signal is only unmasked when
1126 * running in KVM. This doesn't matter though since we are only
1127 * interested in getting KVM to exit, which happens as expected. See
1128 * setupSignalHandler() and kvmRun() for details about KVM signal
1129 * handling.
1130 */
1131static void
1132onKickSignal(int signo, siginfo_t *si, void *data)
1133{
1134}
1135
1136void
1137BaseKvmCPU::setupSignalHandler()
1138{
1139    struct sigaction sa;
1140
1141    memset(&sa, 0, sizeof(sa));
1142    sa.sa_sigaction = onKickSignal;
1143    sa.sa_flags = SA_SIGINFO | SA_RESTART;
1144    if (sigaction(KVM_KICK_SIGNAL, &sa, NULL) == -1)
1145        panic("KVM: Failed to setup vCPU timer signal handler\n");
1146
1147    sigset_t sigset;
1148    if (pthread_sigmask(SIG_BLOCK, NULL, &sigset) == -1)
1149        panic("KVM: Failed get signal mask\n");
1150
1151    // Request KVM to setup the same signal mask as we're currently
1152    // running with except for the KVM control signal. We'll sometimes
1153    // need to raise the KVM_KICK_SIGNAL to cause immediate exits from
1154    // KVM after servicing IO requests. See kvmRun().
1155    sigdelset(&sigset, KVM_KICK_SIGNAL);
1156    setSignalMask(&sigset);
1157
1158    // Mask our control signals so they aren't delivered unless we're
1159    // actually executing inside KVM.
1160    sigaddset(&sigset, KVM_KICK_SIGNAL);
1161    if (pthread_sigmask(SIG_SETMASK, &sigset, NULL) == -1)
1162        panic("KVM: Failed mask the KVM control signals\n");
1163}
1164
1165bool
1166BaseKvmCPU::discardPendingSignal(int signum) const
1167{
1168    int discardedSignal;
1169
1170    // Setting the timeout to zero causes sigtimedwait to return
1171    // immediately.
1172    struct timespec timeout;
1173    timeout.tv_sec = 0;
1174    timeout.tv_nsec = 0;
1175
1176    sigset_t sigset;
1177    sigemptyset(&sigset);
1178    sigaddset(&sigset, signum);
1179
1180    do {
1181        discardedSignal = sigtimedwait(&sigset, NULL, &timeout);
1182    } while (discardedSignal == -1 && errno == EINTR);
1183
1184    if (discardedSignal == signum)
1185        return true;
1186    else if (discardedSignal == -1 && errno == EAGAIN)
1187        return false;
1188    else
1189        panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n",
1190              discardedSignal, errno);
1191}
1192
1193void
1194BaseKvmCPU::setupCounters()
1195{
1196    DPRINTF(Kvm, "Attaching cycle counter...\n");
1197    PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE,
1198                                PERF_COUNT_HW_CPU_CYCLES);
1199    cfgCycles.disabled(true)
1200        .pinned(true);
1201
1202    // Try to exclude the host. We set both exclude_hv and
1203    // exclude_host since different architectures use slightly
1204    // different APIs in the kernel.
1205    cfgCycles.exclude_hv(true)
1206        .exclude_host(true);
1207
1208    if (perfControlledByTimer) {
1209        // We need to configure the cycles counter to send overflows
1210        // since we are going to use it to trigger timer signals that
1211        // trap back into m5 from KVM. In practice, this means that we
1212        // need to set some non-zero sample period that gets
1213        // overridden when the timer is armed.
1214        cfgCycles.wakeupEvents(1)
1215            .samplePeriod(42);
1216    }
1217
1218    hwCycles.attach(cfgCycles,
1219                    0); // TID (0 => currentThread)
1220
1221    setupInstCounter();
1222}
1223
1224bool
1225BaseKvmCPU::tryDrain()
1226{
1227    if (drainState() != DrainState::Draining)
1228        return false;
1229
1230    if (!archIsDrained()) {
1231        DPRINTF(Drain, "tryDrain: Architecture code is not ready.\n");
1232        return false;
1233    }
1234
1235    if (_status == Idle || _status == Running) {
1236        DPRINTF(Drain,
1237                "tryDrain: CPU transitioned into the Idle state, drain done\n");
1238        signalDrainDone();
1239        return true;
1240    } else {
1241        DPRINTF(Drain, "tryDrain: CPU not ready.\n");
1242        return false;
1243    }
1244}
1245
1246void
1247BaseKvmCPU::ioctlRun()
1248{
1249    if (ioctl(KVM_RUN) == -1) {
1250        if (errno != EINTR)
1251            panic("KVM: Failed to start virtual CPU (errno: %i)\n",
1252                  errno);
1253    }
1254}
1255
1256void
1257BaseKvmCPU::setupInstStop()
1258{
1259    if (comInstEventQueue[0]->empty()) {
1260        setupInstCounter(0);
1261    } else {
1262        const uint64_t next(comInstEventQueue[0]->nextTick());
1263
1264        assert(next > ctrInsts);
1265        setupInstCounter(next - ctrInsts);
1266    }
1267}
1268
1269void
1270BaseKvmCPU::setupInstCounter(uint64_t period)
1271{
1272    // No need to do anything if we aren't attaching for the first
1273    // time or the period isn't changing.
1274    if (period == activeInstPeriod && hwInstructions.attached())
1275        return;
1276
1277    PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE,
1278                                         PERF_COUNT_HW_INSTRUCTIONS);
1279
1280    // Try to exclude the host. We set both exclude_hv and
1281    // exclude_host since different architectures use slightly
1282    // different APIs in the kernel.
1283    cfgInstructions.exclude_hv(true)
1284        .exclude_host(true);
1285
1286    if (period) {
1287        // Setup a sampling counter if that has been requested.
1288        cfgInstructions.wakeupEvents(1)
1289            .samplePeriod(period);
1290    }
1291
1292    // We need to detach and re-attach the counter to reliably change
1293    // sampling settings. See PerfKvmCounter::period() for details.
1294    if (hwInstructions.attached())
1295        hwInstructions.detach();
1296    assert(hwCycles.attached());
1297    hwInstructions.attach(cfgInstructions,
1298                          0, // TID (0 => currentThread)
1299                          hwCycles);
1300
1301    if (period)
1302        hwInstructions.enableSignals(KVM_KICK_SIGNAL);
1303
1304    activeInstPeriod = period;
1305}
1306