base.cc revision 9690:8055cd04be78
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/utility.hh"
50#include "cpu/kvm/base.hh"
51#include "debug/Checkpoint.hh"
52#include "debug/Kvm.hh"
53#include "debug/KvmIO.hh"
54#include "debug/KvmRun.hh"
55#include "params/BaseKvmCPU.hh"
56#include "sim/process.hh"
57#include "sim/system.hh"
58
59/* Used by some KVM macros */
60#define PAGE_SIZE pageSize
61
62volatile bool timerOverflowed = false;
63
64static void
65onTimerOverflow(int signo, siginfo_t *si, void *data)
66{
67    timerOverflowed = true;
68}
69
70BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params)
71    : BaseCPU(params),
72      vm(*params->kvmVM),
73      _status(Idle),
74      dataPort(name() + ".dcache_port", this),
75      instPort(name() + ".icache_port", this),
76      threadContextDirty(true),
77      kvmStateDirty(false),
78      vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
79      _kvmRun(NULL), mmioRing(NULL),
80      pageSize(sysconf(_SC_PAGE_SIZE)),
81      tickEvent(*this),
82      perfControlledByTimer(params->usePerfOverflow),
83      hostFactor(params->hostFactor)
84{
85    if (pageSize == -1)
86        panic("KVM: Failed to determine host page size (%i)\n",
87              errno);
88
89    thread = new SimpleThread(this, 0, params->system,
90                              params->itb, params->dtb, params->isa[0]);
91    thread->setStatus(ThreadContext::Halted);
92    tc = thread->getTC();
93    threadContexts.push_back(tc);
94
95    setupCounters();
96    setupSignalHandler();
97
98    if (params->usePerfOverflow)
99        runTimer.reset(new PerfKvmTimer(hwCycles,
100                                        KVM_TIMER_SIGNAL,
101                                        params->hostFactor,
102                                        params->clock));
103    else
104        runTimer.reset(new PosixKvmTimer(KVM_TIMER_SIGNAL, CLOCK_MONOTONIC,
105                                         params->hostFactor,
106                                         params->clock));
107}
108
109BaseKvmCPU::~BaseKvmCPU()
110{
111    if (_kvmRun)
112        munmap(_kvmRun, vcpuMMapSize);
113    close(vcpuFD);
114}
115
116void
117BaseKvmCPU::init()
118{
119    BaseCPU::init();
120
121    if (numThreads != 1)
122        fatal("KVM: Multithreading not supported");
123
124    tc->initMemProxies(tc);
125
126    // initialize CPU, including PC
127    if (FullSystem && !switchedOut())
128        TheISA::initCPU(tc, tc->contextId());
129
130    mmio_req.setThreadContext(tc->contextId(), 0);
131}
132
133void
134BaseKvmCPU::startup()
135{
136    const BaseKvmCPUParams * const p(
137        dynamic_cast<const BaseKvmCPUParams *>(params()));
138
139    Kvm &kvm(vm.kvm);
140
141    BaseCPU::startup();
142
143    assert(vcpuFD == -1);
144
145    // Tell the VM that a CPU is about to start.
146    vm.cpuStartup();
147
148    // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
149    // not guaranteed that the parent KVM VM has initialized at that
150    // point. Initialize virtual CPUs here instead.
151    vcpuFD = vm.createVCPU(vcpuID);
152
153    // Map the KVM run structure */
154    vcpuMMapSize = kvm.getVCPUMMapSize();
155    _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize,
156                                     PROT_READ | PROT_WRITE, MAP_SHARED,
157                                     vcpuFD, 0);
158    if (_kvmRun == MAP_FAILED)
159        panic("KVM: Failed to map run data structure\n");
160
161    // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
162    // available. The offset into the KVM's communication page is
163    // provided by the coalesced MMIO capability.
164    int mmioOffset(kvm.capCoalescedMMIO());
165    if (!p->useCoalescedMMIO) {
166        inform("KVM: Coalesced MMIO disabled by config.\n");
167    } else if (mmioOffset) {
168        inform("KVM: Coalesced IO available\n");
169        mmioRing = (struct kvm_coalesced_mmio_ring *)(
170            (char *)_kvmRun + (mmioOffset * pageSize));
171    } else {
172        inform("KVM: Coalesced not supported by host OS\n");
173    }
174}
175
176void
177BaseKvmCPU::regStats()
178{
179    using namespace Stats;
180
181    BaseCPU::regStats();
182
183    numInsts
184        .name(name() + ".committedInsts")
185        .desc("Number of instructions committed")
186        ;
187
188    numVMExits
189        .name(name() + ".numVMExits")
190        .desc("total number of KVM exits")
191        ;
192
193    numMMIO
194        .name(name() + ".numMMIO")
195        .desc("number of VM exits due to memory mapped IO")
196        ;
197
198    numCoalescedMMIO
199        .name(name() + ".numCoalescedMMIO")
200        .desc("number of coalesced memory mapped IO requests")
201        ;
202
203    numIO
204        .name(name() + ".numIO")
205        .desc("number of VM exits due to legacy IO")
206        ;
207
208    numHalt
209        .name(name() + ".numHalt")
210        .desc("number of VM exits due to wait for interrupt instructions")
211        ;
212
213    numInterrupts
214        .name(name() + ".numInterrupts")
215        .desc("number of interrupts delivered")
216        ;
217
218    numHypercalls
219        .name(name() + ".numHypercalls")
220        .desc("number of hypercalls")
221        ;
222}
223
224void
225BaseKvmCPU::serializeThread(std::ostream &os, ThreadID tid)
226{
227    if (DTRACE(Checkpoint)) {
228        DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid);
229        dump();
230    }
231
232    // Update the thread context so we have something to serialize.
233    syncThreadContext();
234
235    assert(tid == 0);
236    assert(_status == Idle);
237    thread->serialize(os);
238}
239
240void
241BaseKvmCPU::unserializeThread(Checkpoint *cp, const std::string &section,
242                              ThreadID tid)
243{
244    DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid);
245
246    assert(tid == 0);
247    assert(_status == Idle);
248    thread->unserialize(cp, section);
249    threadContextDirty = true;
250}
251
252unsigned int
253BaseKvmCPU::drain(DrainManager *dm)
254{
255    if (switchedOut())
256        return 0;
257
258    DPRINTF(Kvm, "drain\n");
259
260    // De-schedule the tick event so we don't insert any more MMIOs
261    // into the system while it is draining.
262    if (tickEvent.scheduled())
263        deschedule(tickEvent);
264
265    _status = Idle;
266    return 0;
267}
268
269void
270BaseKvmCPU::drainResume()
271{
272    assert(!tickEvent.scheduled());
273
274    // We might have been switched out. In that case, we don't need to
275    // do anything.
276    if (switchedOut())
277        return;
278
279    DPRINTF(Kvm, "drainResume\n");
280    verifyMemoryMode();
281
282    // The tick event is de-scheduled as a part of the draining
283    // process. Re-schedule it if the thread context is active.
284    if (tc->status() == ThreadContext::Active) {
285        schedule(tickEvent, nextCycle());
286        _status = Running;
287    } else {
288        _status = Idle;
289    }
290}
291
292void
293BaseKvmCPU::switchOut()
294{
295    DPRINTF(Kvm, "switchOut\n");
296
297    // Make sure to update the thread context in case, the new CPU
298    // will need to access it.
299    syncThreadContext();
300
301    BaseCPU::switchOut();
302
303    // We should have drained prior to executing a switchOut, which
304    // means that the tick event shouldn't be scheduled and the CPU is
305    // idle.
306    assert(!tickEvent.scheduled());
307    assert(_status == Idle);
308}
309
310void
311BaseKvmCPU::takeOverFrom(BaseCPU *cpu)
312{
313    DPRINTF(Kvm, "takeOverFrom\n");
314
315    BaseCPU::takeOverFrom(cpu);
316
317    // We should have drained prior to executing a switchOut, which
318    // means that the tick event shouldn't be scheduled and the CPU is
319    // idle.
320    assert(!tickEvent.scheduled());
321    assert(_status == Idle);
322    assert(threadContexts.size() == 1);
323
324    // The BaseCPU updated the thread context, make sure that we
325    // synchronize next time we enter start the CPU.
326    threadContextDirty = true;
327}
328
329void
330BaseKvmCPU::verifyMemoryMode() const
331{
332    if (!(system->isAtomicMode() && system->bypassCaches())) {
333        fatal("The KVM-based CPUs requires the memory system to be in the "
334              "'atomic_noncaching' mode.\n");
335    }
336}
337
338void
339BaseKvmCPU::wakeup()
340{
341    DPRINTF(Kvm, "wakeup()\n");
342
343    if (thread->status() != ThreadContext::Suspended)
344        return;
345
346    thread->activate();
347}
348
349void
350BaseKvmCPU::activateContext(ThreadID thread_num, Cycles delay)
351{
352    DPRINTF(Kvm, "ActivateContext %d (%d cycles)\n", thread_num, delay);
353
354    assert(thread_num == 0);
355    assert(thread);
356
357    assert(_status == Idle);
358    assert(!tickEvent.scheduled());
359
360    numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend)
361        * hostFactor;
362
363    schedule(tickEvent, clockEdge(delay));
364    _status = Running;
365}
366
367
368void
369BaseKvmCPU::suspendContext(ThreadID thread_num)
370{
371    DPRINTF(Kvm, "SuspendContext %d\n", thread_num);
372
373    assert(thread_num == 0);
374    assert(thread);
375
376    if (_status == Idle)
377        return;
378
379    assert(_status == Running);
380
381    // The tick event may no be scheduled if the quest has requested
382    // the monitor to wait for interrupts. The normal CPU models can
383    // get their tick events descheduled by quiesce instructions, but
384    // that can't happen here.
385    if (tickEvent.scheduled())
386        deschedule(tickEvent);
387
388    _status = Idle;
389}
390
391void
392BaseKvmCPU::deallocateContext(ThreadID thread_num)
393{
394    // for now, these are equivalent
395    suspendContext(thread_num);
396}
397
398void
399BaseKvmCPU::haltContext(ThreadID thread_num)
400{
401    // for now, these are equivalent
402    suspendContext(thread_num);
403}
404
405ThreadContext *
406BaseKvmCPU::getContext(int tn)
407{
408    assert(tn == 0);
409    syncThreadContext();
410    return tc;
411}
412
413
414Counter
415BaseKvmCPU::totalInsts() const
416{
417    return hwInstructions.read();
418}
419
420Counter
421BaseKvmCPU::totalOps() const
422{
423    hack_once("Pretending totalOps is equivalent to totalInsts()\n");
424    return hwInstructions.read();
425}
426
427void
428BaseKvmCPU::dump()
429{
430    inform("State dumping not implemented.");
431}
432
433void
434BaseKvmCPU::tick()
435{
436    assert(_status == Running);
437
438    DPRINTF(KvmRun, "Entering KVM...\n");
439
440    Tick ticksToExecute(mainEventQueue.nextTick() - curTick());
441    Tick ticksExecuted(kvmRun(ticksToExecute));
442
443    Tick delay(ticksExecuted + handleKvmExit());
444
445    switch (_status) {
446      case Running:
447        schedule(tickEvent, clockEdge(ticksToCycles(delay)));
448        break;
449
450      default:
451        /* The CPU is halted or waiting for an interrupt from a
452         * device. Don't start it. */
453        break;
454    }
455}
456
457Tick
458BaseKvmCPU::kvmRun(Tick ticks)
459{
460    uint64_t baseCycles(hwCycles.read());
461    uint64_t baseInstrs(hwInstructions.read());
462
463    // We might need to update the KVM state.
464    syncKvmState();
465    // Entering into KVM implies that we'll have to reload the thread
466    // context from KVM if we want to access it. Flag the KVM state as
467    // dirty with respect to the cached thread context.
468    kvmStateDirty = true;
469
470    if (ticks < runTimer->resolution()) {
471        DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
472                ticks, runTimer->resolution());
473        ticks = runTimer->resolution();
474    }
475
476    DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks);
477    timerOverflowed = false;
478
479    // Arm the run timer and start the cycle timer if it isn't
480    // controlled by the overflow timer. Starting/stopping the cycle
481    // timer automatically starts the other perf timers as they are in
482    // the same counter group.
483    runTimer->arm(ticks);
484    if (!perfControlledByTimer)
485        hwCycles.start();
486
487    if (ioctl(KVM_RUN) == -1) {
488        if (errno != EINTR)
489            panic("KVM: Failed to start virtual CPU (errno: %i)\n",
490                  errno);
491    }
492
493    runTimer->disarm();
494    if (!perfControlledByTimer)
495        hwCycles.stop();
496
497
498    const uint64_t hostCyclesExecuted(hwCycles.read() - baseCycles);
499    const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor);
500    const uint64_t instsExecuted(hwInstructions.read() - baseInstrs);
501    const Tick ticksExecuted(runTimer->ticksFromHostCycles(hostCyclesExecuted));
502
503    if (ticksExecuted < ticks &&
504        timerOverflowed &&
505        _kvmRun->exit_reason == KVM_EXIT_INTR) {
506        // TODO: We should probably do something clever here...
507        warn("KVM: Early timer event, requested %i ticks but got %i ticks.\n",
508             ticks, ticksExecuted);
509    }
510
511    /* Update statistics */
512    numCycles += simCyclesExecuted;;
513    ++numVMExits;
514    numInsts += instsExecuted;
515
516    DPRINTF(KvmRun, "KVM: Executed %i instructions in %i cycles (%i ticks, sim cycles: %i).\n",
517            instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted);
518
519    return ticksExecuted + flushCoalescedMMIO();
520}
521
522void
523BaseKvmCPU::kvmNonMaskableInterrupt()
524{
525    ++numInterrupts;
526    if (ioctl(KVM_NMI) == -1)
527        panic("KVM: Failed to deliver NMI to virtual CPU\n");
528}
529
530void
531BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt)
532{
533    ++numInterrupts;
534    if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1)
535        panic("KVM: Failed to deliver interrupt to virtual CPU\n");
536}
537
538void
539BaseKvmCPU::getRegisters(struct kvm_regs &regs) const
540{
541    if (ioctl(KVM_GET_REGS, &regs) == -1)
542        panic("KVM: Failed to get guest registers\n");
543}
544
545void
546BaseKvmCPU::setRegisters(const struct kvm_regs &regs)
547{
548    if (ioctl(KVM_SET_REGS, (void *)&regs) == -1)
549        panic("KVM: Failed to set guest registers\n");
550}
551
552void
553BaseKvmCPU::getSpecialRegisters(struct kvm_sregs &regs) const
554{
555    if (ioctl(KVM_GET_SREGS, &regs) == -1)
556        panic("KVM: Failed to get guest special registers\n");
557}
558
559void
560BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs &regs)
561{
562    if (ioctl(KVM_SET_SREGS, (void *)&regs) == -1)
563        panic("KVM: Failed to set guest special registers\n");
564}
565
566void
567BaseKvmCPU::getFPUState(struct kvm_fpu &state) const
568{
569    if (ioctl(KVM_GET_FPU, &state) == -1)
570        panic("KVM: Failed to get guest FPU state\n");
571}
572
573void
574BaseKvmCPU::setFPUState(const struct kvm_fpu &state)
575{
576    if (ioctl(KVM_SET_FPU, (void *)&state) == -1)
577        panic("KVM: Failed to set guest FPU state\n");
578}
579
580
581void
582BaseKvmCPU::setOneReg(uint64_t id, const void *addr)
583{
584#ifdef KVM_SET_ONE_REG
585    struct kvm_one_reg reg;
586    reg.id = id;
587    reg.addr = (uint64_t)addr;
588
589    if (ioctl(KVM_SET_ONE_REG, &reg) == -1) {
590        panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
591              id, errno);
592    }
593#else
594    panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
595#endif
596}
597
598void
599BaseKvmCPU::getOneReg(uint64_t id, void *addr) const
600{
601#ifdef KVM_GET_ONE_REG
602    struct kvm_one_reg reg;
603    reg.id = id;
604    reg.addr = (uint64_t)addr;
605
606    if (ioctl(KVM_GET_ONE_REG, &reg) == -1) {
607        panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
608              id, errno);
609    }
610#else
611    panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
612#endif
613}
614
615std::string
616BaseKvmCPU::getAndFormatOneReg(uint64_t id) const
617{
618#ifdef KVM_GET_ONE_REG
619    std::ostringstream ss;
620
621    ss.setf(std::ios::hex, std::ios::basefield);
622    ss.setf(std::ios::showbase);
623#define HANDLE_INTTYPE(len)                      \
624    case KVM_REG_SIZE_U ## len: {                \
625        uint ## len ## _t value;                 \
626        getOneReg(id, &value);                   \
627        ss << value;                             \
628    }  break
629
630#define HANDLE_ARRAY(len)                       \
631    case KVM_REG_SIZE_U ## len: {               \
632        uint8_t value[len / 8];                 \
633        getOneReg(id, value);                   \
634        ss << "[" << value[0];                  \
635        for (int i = 1; i < len  / 8; ++i)      \
636            ss << ", " << value[i];             \
637        ss << "]";                              \
638      } break
639
640    switch (id & KVM_REG_SIZE_MASK) {
641        HANDLE_INTTYPE(8);
642        HANDLE_INTTYPE(16);
643        HANDLE_INTTYPE(32);
644        HANDLE_INTTYPE(64);
645        HANDLE_ARRAY(128);
646        HANDLE_ARRAY(256);
647        HANDLE_ARRAY(512);
648        HANDLE_ARRAY(1024);
649      default:
650        ss << "??";
651    }
652
653#undef HANDLE_INTTYPE
654#undef HANDLE_ARRAY
655
656    return ss.str();
657#else
658    panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
659#endif
660}
661
662void
663BaseKvmCPU::syncThreadContext()
664{
665    if (!kvmStateDirty)
666        return;
667
668    assert(!threadContextDirty);
669
670    updateThreadContext();
671    kvmStateDirty = false;
672}
673
674void
675BaseKvmCPU::syncKvmState()
676{
677    if (!threadContextDirty)
678        return;
679
680    assert(!kvmStateDirty);
681
682    updateKvmState();
683    threadContextDirty = false;
684}
685
686Tick
687BaseKvmCPU::handleKvmExit()
688{
689    DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason);
690
691    switch (_kvmRun->exit_reason) {
692      case KVM_EXIT_UNKNOWN:
693        return handleKvmExitUnknown();
694
695      case KVM_EXIT_EXCEPTION:
696        return handleKvmExitException();
697
698      case KVM_EXIT_IO:
699        ++numIO;
700        return handleKvmExitIO();
701
702      case KVM_EXIT_HYPERCALL:
703        ++numHypercalls;
704        return handleKvmExitHypercall();
705
706      case KVM_EXIT_HLT:
707        /* The guest has halted and is waiting for interrupts */
708        DPRINTF(Kvm, "handleKvmExitHalt\n");
709        ++numHalt;
710
711        // Suspend the thread until the next interrupt arrives
712        thread->suspend();
713
714        // This is actually ignored since the thread is suspended.
715        return 0;
716
717      case KVM_EXIT_MMIO:
718        /* Service memory mapped IO requests */
719        DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
720                _kvmRun->mmio.is_write,
721                _kvmRun->mmio.phys_addr, _kvmRun->mmio.len);
722
723        ++numMMIO;
724        return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data,
725                            _kvmRun->mmio.len, _kvmRun->mmio.is_write);
726
727      case KVM_EXIT_IRQ_WINDOW_OPEN:
728        return handleKvmExitIRQWindowOpen();
729
730      case KVM_EXIT_FAIL_ENTRY:
731        return handleKvmExitFailEntry();
732
733      case KVM_EXIT_INTR:
734        /* KVM was interrupted by a signal, restart it in the next
735         * tick. */
736        return 0;
737
738      case KVM_EXIT_INTERNAL_ERROR:
739        panic("KVM: Internal error (suberror: %u)\n",
740              _kvmRun->internal.suberror);
741
742      default:
743        dump();
744        panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason);
745    }
746}
747
748Tick
749BaseKvmCPU::handleKvmExitIO()
750{
751    panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
752          _kvmRun->io.direction, _kvmRun->io.size,
753          _kvmRun->io.port, _kvmRun->io.count);
754}
755
756Tick
757BaseKvmCPU::handleKvmExitHypercall()
758{
759    panic("KVM: Unhandled hypercall\n");
760}
761
762Tick
763BaseKvmCPU::handleKvmExitIRQWindowOpen()
764{
765    warn("KVM: Unhandled IRQ window.\n");
766    return 0;
767}
768
769
770Tick
771BaseKvmCPU::handleKvmExitUnknown()
772{
773    dump();
774    panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
775          _kvmRun->hw.hardware_exit_reason);
776}
777
778Tick
779BaseKvmCPU::handleKvmExitException()
780{
781    dump();
782    panic("KVM: Got exception when starting vCPU "
783          "(exception: %u, error_code: %u)\n",
784          _kvmRun->ex.exception, _kvmRun->ex.error_code);
785}
786
787Tick
788BaseKvmCPU::handleKvmExitFailEntry()
789{
790    dump();
791    panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
792          _kvmRun->fail_entry.hardware_entry_failure_reason);
793}
794
795Tick
796BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write)
797{
798    mmio_req.setPhys(paddr, size, Request::UNCACHEABLE, dataMasterId());
799
800    const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq);
801    Packet pkt(&mmio_req, cmd);
802    pkt.dataStatic(data);
803    return dataPort.sendAtomic(&pkt);
804}
805
806int
807BaseKvmCPU::ioctl(int request, long p1) const
808{
809    if (vcpuFD == -1)
810        panic("KVM: CPU ioctl called before initialization\n");
811
812    return ::ioctl(vcpuFD, request, p1);
813}
814
815Tick
816BaseKvmCPU::flushCoalescedMMIO()
817{
818    if (!mmioRing)
819        return 0;
820
821    DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n");
822
823    // TODO: We might need to do synchronization when we start to
824    // support multiple CPUs
825    Tick ticks(0);
826    while (mmioRing->first != mmioRing->last) {
827        struct kvm_coalesced_mmio &ent(
828            mmioRing->coalesced_mmio[mmioRing->first]);
829
830        DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
831                ent.phys_addr, ent.len);
832
833        ++numCoalescedMMIO;
834        ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true);
835
836        mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX;
837    }
838
839    return ticks;
840}
841
842void
843BaseKvmCPU::setupSignalHandler()
844{
845    struct sigaction sa;
846
847    memset(&sa, 0, sizeof(sa));
848    sa.sa_sigaction = onTimerOverflow;
849    sa.sa_flags = SA_SIGINFO | SA_RESTART;
850    if (sigaction(KVM_TIMER_SIGNAL, &sa, NULL) == -1)
851        panic("KVM: Failed to setup vCPU signal handler\n");
852}
853
854void
855BaseKvmCPU::setupCounters()
856{
857    DPRINTF(Kvm, "Attaching cycle counter...\n");
858    PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE,
859                                PERF_COUNT_HW_CPU_CYCLES);
860    cfgCycles.disabled(true)
861        .pinned(true);
862
863    if (perfControlledByTimer) {
864        // We need to configure the cycles counter to send overflows
865        // since we are going to use it to trigger timer signals that
866        // trap back into m5 from KVM. In practice, this means that we
867        // need to set some non-zero sample period that gets
868        // overridden when the timer is armed.
869        cfgCycles.wakeupEvents(1)
870            .samplePeriod(42);
871    }
872
873    hwCycles.attach(cfgCycles,
874                    0); // TID (0 => currentThread)
875
876    DPRINTF(Kvm, "Attaching instruction counter...\n");
877    PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE,
878                                      PERF_COUNT_HW_INSTRUCTIONS);
879    hwInstructions.attach(cfgInstructions,
880                          0, // TID (0 => currentThread)
881                          hwCycles);
882}
883