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