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