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