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