base.cc revision 9983
19651SAndreas.Sandberg@ARM.com/*
29651SAndreas.Sandberg@ARM.com * Copyright (c) 2012 ARM Limited
39651SAndreas.Sandberg@ARM.com * All rights reserved
49651SAndreas.Sandberg@ARM.com *
59651SAndreas.Sandberg@ARM.com * The license below extends only to copyright in the software and shall
69651SAndreas.Sandberg@ARM.com * not be construed as granting a license to any other intellectual
79651SAndreas.Sandberg@ARM.com * property including but not limited to intellectual property relating
89651SAndreas.Sandberg@ARM.com * to a hardware implementation of the functionality of the software
99651SAndreas.Sandberg@ARM.com * licensed hereunder.  You may use the software subject to the license
109651SAndreas.Sandberg@ARM.com * terms below provided that you ensure that this notice is replicated
119651SAndreas.Sandberg@ARM.com * unmodified and in its entirety in all distributions of the software,
129651SAndreas.Sandberg@ARM.com * modified or unmodified, in source code or in binary form.
139651SAndreas.Sandberg@ARM.com *
149651SAndreas.Sandberg@ARM.com * Redistribution and use in source and binary forms, with or without
159651SAndreas.Sandberg@ARM.com * modification, are permitted provided that the following conditions are
169651SAndreas.Sandberg@ARM.com * met: redistributions of source code must retain the above copyright
179651SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer;
189651SAndreas.Sandberg@ARM.com * redistributions in binary form must reproduce the above copyright
199651SAndreas.Sandberg@ARM.com * notice, this list of conditions and the following disclaimer in the
209651SAndreas.Sandberg@ARM.com * documentation and/or other materials provided with the distribution;
219651SAndreas.Sandberg@ARM.com * neither the name of the copyright holders nor the names of its
229651SAndreas.Sandberg@ARM.com * contributors may be used to endorse or promote products derived from
239651SAndreas.Sandberg@ARM.com * this software without specific prior written permission.
249651SAndreas.Sandberg@ARM.com *
259651SAndreas.Sandberg@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
269651SAndreas.Sandberg@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
279651SAndreas.Sandberg@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
289651SAndreas.Sandberg@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
299651SAndreas.Sandberg@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
309651SAndreas.Sandberg@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
319651SAndreas.Sandberg@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
329651SAndreas.Sandberg@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
339651SAndreas.Sandberg@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
349651SAndreas.Sandberg@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
359651SAndreas.Sandberg@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
369651SAndreas.Sandberg@ARM.com *
379651SAndreas.Sandberg@ARM.com * Authors: Andreas Sandberg
389651SAndreas.Sandberg@ARM.com */
399651SAndreas.Sandberg@ARM.com
409651SAndreas.Sandberg@ARM.com#include <linux/kvm.h>
419651SAndreas.Sandberg@ARM.com#include <sys/ioctl.h>
429651SAndreas.Sandberg@ARM.com#include <sys/mman.h>
439651SAndreas.Sandberg@ARM.com#include <unistd.h>
449651SAndreas.Sandberg@ARM.com
459651SAndreas.Sandberg@ARM.com#include <cerrno>
469651SAndreas.Sandberg@ARM.com#include <csignal>
479651SAndreas.Sandberg@ARM.com#include <ostream>
489651SAndreas.Sandberg@ARM.com
499760Sandreas@sandberg.pp.se#include "arch/mmapped_ipr.hh"
509651SAndreas.Sandberg@ARM.com#include "arch/utility.hh"
519651SAndreas.Sandberg@ARM.com#include "cpu/kvm/base.hh"
529683Sandreas@sandberg.pp.se#include "debug/Checkpoint.hh"
539753Sandreas@sandberg.pp.se#include "debug/Drain.hh"
549651SAndreas.Sandberg@ARM.com#include "debug/Kvm.hh"
559651SAndreas.Sandberg@ARM.com#include "debug/KvmIO.hh"
569651SAndreas.Sandberg@ARM.com#include "debug/KvmRun.hh"
579651SAndreas.Sandberg@ARM.com#include "params/BaseKvmCPU.hh"
589651SAndreas.Sandberg@ARM.com#include "sim/process.hh"
599651SAndreas.Sandberg@ARM.com#include "sim/system.hh"
609651SAndreas.Sandberg@ARM.com
619753Sandreas@sandberg.pp.se#include <signal.h>
629753Sandreas@sandberg.pp.se
639651SAndreas.Sandberg@ARM.com/* Used by some KVM macros */
649651SAndreas.Sandberg@ARM.com#define PAGE_SIZE pageSize
659651SAndreas.Sandberg@ARM.com
669651SAndreas.Sandberg@ARM.comvolatile bool timerOverflowed = false;
679651SAndreas.Sandberg@ARM.com
689651SAndreas.Sandberg@ARM.comBaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params)
699651SAndreas.Sandberg@ARM.com    : BaseCPU(params),
709651SAndreas.Sandberg@ARM.com      vm(*params->kvmVM),
719651SAndreas.Sandberg@ARM.com      _status(Idle),
729651SAndreas.Sandberg@ARM.com      dataPort(name() + ".dcache_port", this),
739651SAndreas.Sandberg@ARM.com      instPort(name() + ".icache_port", this),
749652SAndreas.Sandberg@ARM.com      threadContextDirty(true),
759652SAndreas.Sandberg@ARM.com      kvmStateDirty(false),
769651SAndreas.Sandberg@ARM.com      vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
779651SAndreas.Sandberg@ARM.com      _kvmRun(NULL), mmioRing(NULL),
789651SAndreas.Sandberg@ARM.com      pageSize(sysconf(_SC_PAGE_SIZE)),
799651SAndreas.Sandberg@ARM.com      tickEvent(*this),
809892Sandreas@sandberg.pp.se      activeInstPeriod(0),
819655SAndreas.Sandberg@ARM.com      perfControlledByTimer(params->usePerfOverflow),
829754Sandreas@sandberg.pp.se      hostFreq(params->hostFreq),
839752Sandreas@sandberg.pp.se      hostFactor(params->hostFactor),
849753Sandreas@sandberg.pp.se      drainManager(NULL),
859752Sandreas@sandberg.pp.se      ctrInsts(0)
869651SAndreas.Sandberg@ARM.com{
879651SAndreas.Sandberg@ARM.com    if (pageSize == -1)
889651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to determine host page size (%i)\n",
899651SAndreas.Sandberg@ARM.com              errno);
909651SAndreas.Sandberg@ARM.com
919651SAndreas.Sandberg@ARM.com    thread = new SimpleThread(this, 0, params->system,
929651SAndreas.Sandberg@ARM.com                              params->itb, params->dtb, params->isa[0]);
939651SAndreas.Sandberg@ARM.com    thread->setStatus(ThreadContext::Halted);
949651SAndreas.Sandberg@ARM.com    tc = thread->getTC();
959651SAndreas.Sandberg@ARM.com    threadContexts.push_back(tc);
969651SAndreas.Sandberg@ARM.com
979651SAndreas.Sandberg@ARM.com    setupCounters();
989651SAndreas.Sandberg@ARM.com
999655SAndreas.Sandberg@ARM.com    if (params->usePerfOverflow)
1009655SAndreas.Sandberg@ARM.com        runTimer.reset(new PerfKvmTimer(hwCycles,
1019655SAndreas.Sandberg@ARM.com                                        KVM_TIMER_SIGNAL,
1029655SAndreas.Sandberg@ARM.com                                        params->hostFactor,
1039754Sandreas@sandberg.pp.se                                        params->hostFreq));
1049655SAndreas.Sandberg@ARM.com    else
1059655SAndreas.Sandberg@ARM.com        runTimer.reset(new PosixKvmTimer(KVM_TIMER_SIGNAL, CLOCK_MONOTONIC,
1069655SAndreas.Sandberg@ARM.com                                         params->hostFactor,
1079754Sandreas@sandberg.pp.se                                         params->hostFreq));
1089651SAndreas.Sandberg@ARM.com}
1099651SAndreas.Sandberg@ARM.com
1109651SAndreas.Sandberg@ARM.comBaseKvmCPU::~BaseKvmCPU()
1119651SAndreas.Sandberg@ARM.com{
1129651SAndreas.Sandberg@ARM.com    if (_kvmRun)
1139651SAndreas.Sandberg@ARM.com        munmap(_kvmRun, vcpuMMapSize);
1149651SAndreas.Sandberg@ARM.com    close(vcpuFD);
1159651SAndreas.Sandberg@ARM.com}
1169651SAndreas.Sandberg@ARM.com
1179651SAndreas.Sandberg@ARM.comvoid
1189651SAndreas.Sandberg@ARM.comBaseKvmCPU::init()
1199651SAndreas.Sandberg@ARM.com{
1209651SAndreas.Sandberg@ARM.com    BaseCPU::init();
1219651SAndreas.Sandberg@ARM.com
1229651SAndreas.Sandberg@ARM.com    if (numThreads != 1)
1239651SAndreas.Sandberg@ARM.com        fatal("KVM: Multithreading not supported");
1249651SAndreas.Sandberg@ARM.com
1259651SAndreas.Sandberg@ARM.com    tc->initMemProxies(tc);
1269651SAndreas.Sandberg@ARM.com
1279651SAndreas.Sandberg@ARM.com    // initialize CPU, including PC
1289651SAndreas.Sandberg@ARM.com    if (FullSystem && !switchedOut())
1299651SAndreas.Sandberg@ARM.com        TheISA::initCPU(tc, tc->contextId());
1309651SAndreas.Sandberg@ARM.com
1319651SAndreas.Sandberg@ARM.com    mmio_req.setThreadContext(tc->contextId(), 0);
1329651SAndreas.Sandberg@ARM.com}
1339651SAndreas.Sandberg@ARM.com
1349651SAndreas.Sandberg@ARM.comvoid
1359651SAndreas.Sandberg@ARM.comBaseKvmCPU::startup()
1369651SAndreas.Sandberg@ARM.com{
1379690Sandreas@sandberg.pp.se    const BaseKvmCPUParams * const p(
1389690Sandreas@sandberg.pp.se        dynamic_cast<const BaseKvmCPUParams *>(params()));
1399690Sandreas@sandberg.pp.se
1409651SAndreas.Sandberg@ARM.com    Kvm &kvm(vm.kvm);
1419651SAndreas.Sandberg@ARM.com
1429651SAndreas.Sandberg@ARM.com    BaseCPU::startup();
1439651SAndreas.Sandberg@ARM.com
1449651SAndreas.Sandberg@ARM.com    assert(vcpuFD == -1);
1459651SAndreas.Sandberg@ARM.com
1469651SAndreas.Sandberg@ARM.com    // Tell the VM that a CPU is about to start.
1479651SAndreas.Sandberg@ARM.com    vm.cpuStartup();
1489651SAndreas.Sandberg@ARM.com
1499651SAndreas.Sandberg@ARM.com    // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
1509651SAndreas.Sandberg@ARM.com    // not guaranteed that the parent KVM VM has initialized at that
1519651SAndreas.Sandberg@ARM.com    // point. Initialize virtual CPUs here instead.
1529651SAndreas.Sandberg@ARM.com    vcpuFD = vm.createVCPU(vcpuID);
1539651SAndreas.Sandberg@ARM.com
1549753Sandreas@sandberg.pp.se    // Setup signal handlers. This has to be done after the vCPU is
1559753Sandreas@sandberg.pp.se    // created since it manipulates the vCPU signal mask.
1569753Sandreas@sandberg.pp.se    setupSignalHandler();
1579753Sandreas@sandberg.pp.se
1589651SAndreas.Sandberg@ARM.com    // Map the KVM run structure */
1599651SAndreas.Sandberg@ARM.com    vcpuMMapSize = kvm.getVCPUMMapSize();
1609651SAndreas.Sandberg@ARM.com    _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize,
1619651SAndreas.Sandberg@ARM.com                                     PROT_READ | PROT_WRITE, MAP_SHARED,
1629651SAndreas.Sandberg@ARM.com                                     vcpuFD, 0);
1639651SAndreas.Sandberg@ARM.com    if (_kvmRun == MAP_FAILED)
1649651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to map run data structure\n");
1659651SAndreas.Sandberg@ARM.com
1669651SAndreas.Sandberg@ARM.com    // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
1679651SAndreas.Sandberg@ARM.com    // available. The offset into the KVM's communication page is
1689651SAndreas.Sandberg@ARM.com    // provided by the coalesced MMIO capability.
1699651SAndreas.Sandberg@ARM.com    int mmioOffset(kvm.capCoalescedMMIO());
1709690Sandreas@sandberg.pp.se    if (!p->useCoalescedMMIO) {
1719690Sandreas@sandberg.pp.se        inform("KVM: Coalesced MMIO disabled by config.\n");
1729690Sandreas@sandberg.pp.se    } else if (mmioOffset) {
1739651SAndreas.Sandberg@ARM.com        inform("KVM: Coalesced IO available\n");
1749651SAndreas.Sandberg@ARM.com        mmioRing = (struct kvm_coalesced_mmio_ring *)(
1759651SAndreas.Sandberg@ARM.com            (char *)_kvmRun + (mmioOffset * pageSize));
1769651SAndreas.Sandberg@ARM.com    } else {
1779651SAndreas.Sandberg@ARM.com        inform("KVM: Coalesced not supported by host OS\n");
1789651SAndreas.Sandberg@ARM.com    }
1799732Sandreas@sandberg.pp.se
1809732Sandreas@sandberg.pp.se    thread->startup();
1819651SAndreas.Sandberg@ARM.com}
1829651SAndreas.Sandberg@ARM.com
1839651SAndreas.Sandberg@ARM.comvoid
1849651SAndreas.Sandberg@ARM.comBaseKvmCPU::regStats()
1859651SAndreas.Sandberg@ARM.com{
1869651SAndreas.Sandberg@ARM.com    using namespace Stats;
1879651SAndreas.Sandberg@ARM.com
1889651SAndreas.Sandberg@ARM.com    BaseCPU::regStats();
1899651SAndreas.Sandberg@ARM.com
1909684Sandreas@sandberg.pp.se    numInsts
1919684Sandreas@sandberg.pp.se        .name(name() + ".committedInsts")
1929684Sandreas@sandberg.pp.se        .desc("Number of instructions committed")
1939684Sandreas@sandberg.pp.se        ;
1949684Sandreas@sandberg.pp.se
1959651SAndreas.Sandberg@ARM.com    numVMExits
1969651SAndreas.Sandberg@ARM.com        .name(name() + ".numVMExits")
1979651SAndreas.Sandberg@ARM.com        .desc("total number of KVM exits")
1989651SAndreas.Sandberg@ARM.com        ;
1999651SAndreas.Sandberg@ARM.com
2009755Sandreas@sandberg.pp.se    numVMHalfEntries
2019755Sandreas@sandberg.pp.se        .name(name() + ".numVMHalfEntries")
2029755Sandreas@sandberg.pp.se        .desc("number of KVM entries to finalize pending operations")
2039755Sandreas@sandberg.pp.se        ;
2049755Sandreas@sandberg.pp.se
2059755Sandreas@sandberg.pp.se    numExitSignal
2069755Sandreas@sandberg.pp.se        .name(name() + ".numExitSignal")
2079755Sandreas@sandberg.pp.se        .desc("exits due to signal delivery")
2089755Sandreas@sandberg.pp.se        ;
2099755Sandreas@sandberg.pp.se
2109651SAndreas.Sandberg@ARM.com    numMMIO
2119651SAndreas.Sandberg@ARM.com        .name(name() + ".numMMIO")
2129651SAndreas.Sandberg@ARM.com        .desc("number of VM exits due to memory mapped IO")
2139651SAndreas.Sandberg@ARM.com        ;
2149651SAndreas.Sandberg@ARM.com
2159651SAndreas.Sandberg@ARM.com    numCoalescedMMIO
2169651SAndreas.Sandberg@ARM.com        .name(name() + ".numCoalescedMMIO")
2179651SAndreas.Sandberg@ARM.com        .desc("number of coalesced memory mapped IO requests")
2189651SAndreas.Sandberg@ARM.com        ;
2199651SAndreas.Sandberg@ARM.com
2209651SAndreas.Sandberg@ARM.com    numIO
2219651SAndreas.Sandberg@ARM.com        .name(name() + ".numIO")
2229651SAndreas.Sandberg@ARM.com        .desc("number of VM exits due to legacy IO")
2239651SAndreas.Sandberg@ARM.com        ;
2249651SAndreas.Sandberg@ARM.com
2259651SAndreas.Sandberg@ARM.com    numHalt
2269651SAndreas.Sandberg@ARM.com        .name(name() + ".numHalt")
2279651SAndreas.Sandberg@ARM.com        .desc("number of VM exits due to wait for interrupt instructions")
2289651SAndreas.Sandberg@ARM.com        ;
2299651SAndreas.Sandberg@ARM.com
2309651SAndreas.Sandberg@ARM.com    numInterrupts
2319651SAndreas.Sandberg@ARM.com        .name(name() + ".numInterrupts")
2329651SAndreas.Sandberg@ARM.com        .desc("number of interrupts delivered")
2339651SAndreas.Sandberg@ARM.com        ;
2349651SAndreas.Sandberg@ARM.com
2359651SAndreas.Sandberg@ARM.com    numHypercalls
2369651SAndreas.Sandberg@ARM.com        .name(name() + ".numHypercalls")
2379651SAndreas.Sandberg@ARM.com        .desc("number of hypercalls")
2389651SAndreas.Sandberg@ARM.com        ;
2399651SAndreas.Sandberg@ARM.com}
2409651SAndreas.Sandberg@ARM.com
2419651SAndreas.Sandberg@ARM.comvoid
2429651SAndreas.Sandberg@ARM.comBaseKvmCPU::serializeThread(std::ostream &os, ThreadID tid)
2439651SAndreas.Sandberg@ARM.com{
2449683Sandreas@sandberg.pp.se    if (DTRACE(Checkpoint)) {
2459683Sandreas@sandberg.pp.se        DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid);
2469683Sandreas@sandberg.pp.se        dump();
2479683Sandreas@sandberg.pp.se    }
2489683Sandreas@sandberg.pp.se
2499651SAndreas.Sandberg@ARM.com    assert(tid == 0);
2509651SAndreas.Sandberg@ARM.com    assert(_status == Idle);
2519651SAndreas.Sandberg@ARM.com    thread->serialize(os);
2529651SAndreas.Sandberg@ARM.com}
2539651SAndreas.Sandberg@ARM.com
2549651SAndreas.Sandberg@ARM.comvoid
2559651SAndreas.Sandberg@ARM.comBaseKvmCPU::unserializeThread(Checkpoint *cp, const std::string &section,
2569651SAndreas.Sandberg@ARM.com                              ThreadID tid)
2579651SAndreas.Sandberg@ARM.com{
2589683Sandreas@sandberg.pp.se    DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid);
2599683Sandreas@sandberg.pp.se
2609651SAndreas.Sandberg@ARM.com    assert(tid == 0);
2619651SAndreas.Sandberg@ARM.com    assert(_status == Idle);
2629651SAndreas.Sandberg@ARM.com    thread->unserialize(cp, section);
2639652SAndreas.Sandberg@ARM.com    threadContextDirty = true;
2649651SAndreas.Sandberg@ARM.com}
2659651SAndreas.Sandberg@ARM.com
2669651SAndreas.Sandberg@ARM.comunsigned int
2679651SAndreas.Sandberg@ARM.comBaseKvmCPU::drain(DrainManager *dm)
2689651SAndreas.Sandberg@ARM.com{
2699651SAndreas.Sandberg@ARM.com    if (switchedOut())
2709651SAndreas.Sandberg@ARM.com        return 0;
2719651SAndreas.Sandberg@ARM.com
2729753Sandreas@sandberg.pp.se    DPRINTF(Drain, "BaseKvmCPU::drain\n");
2739753Sandreas@sandberg.pp.se    switch (_status) {
2749753Sandreas@sandberg.pp.se      case Running:
2759753Sandreas@sandberg.pp.se        // The base KVM code is normally ready when it is in the
2769753Sandreas@sandberg.pp.se        // Running state, but the architecture specific code might be
2779753Sandreas@sandberg.pp.se        // of a different opinion. This may happen when the CPU been
2789753Sandreas@sandberg.pp.se        // notified of an event that hasn't been accepted by the vCPU
2799753Sandreas@sandberg.pp.se        // yet.
2809753Sandreas@sandberg.pp.se        if (!archIsDrained()) {
2819753Sandreas@sandberg.pp.se            drainManager = dm;
2829753Sandreas@sandberg.pp.se            return 1;
2839753Sandreas@sandberg.pp.se        }
2849651SAndreas.Sandberg@ARM.com
2859753Sandreas@sandberg.pp.se        // The state of the CPU is consistent, so we don't need to do
2869753Sandreas@sandberg.pp.se        // anything special to drain it. We simply de-schedule the
2879753Sandreas@sandberg.pp.se        // tick event and enter the Idle state to prevent nasty things
2889753Sandreas@sandberg.pp.se        // like MMIOs from happening.
2899753Sandreas@sandberg.pp.se        if (tickEvent.scheduled())
2909753Sandreas@sandberg.pp.se            deschedule(tickEvent);
2919753Sandreas@sandberg.pp.se        _status = Idle;
2929651SAndreas.Sandberg@ARM.com
2939753Sandreas@sandberg.pp.se        /** FALLTHROUGH */
2949753Sandreas@sandberg.pp.se      case Idle:
2959753Sandreas@sandberg.pp.se        // Idle, no need to drain
2969753Sandreas@sandberg.pp.se        assert(!tickEvent.scheduled());
2979753Sandreas@sandberg.pp.se
2989753Sandreas@sandberg.pp.se        // Sync the thread context here since we'll need it when we
2999753Sandreas@sandberg.pp.se        // switch CPUs or checkpoint the CPU.
3009753Sandreas@sandberg.pp.se        syncThreadContext();
3019753Sandreas@sandberg.pp.se
3029753Sandreas@sandberg.pp.se        return 0;
3039753Sandreas@sandberg.pp.se
3049753Sandreas@sandberg.pp.se      case RunningServiceCompletion:
3059753Sandreas@sandberg.pp.se        // The CPU has just requested a service that was handled in
3069753Sandreas@sandberg.pp.se        // the RunningService state, but the results have still not
3079753Sandreas@sandberg.pp.se        // been reported to the CPU. Now, we /could/ probably just
3089753Sandreas@sandberg.pp.se        // update the register state ourselves instead of letting KVM
3099753Sandreas@sandberg.pp.se        // handle it, but that would be tricky. Instead, we enter KVM
3109753Sandreas@sandberg.pp.se        // and let it do its stuff.
3119753Sandreas@sandberg.pp.se        drainManager = dm;
3129753Sandreas@sandberg.pp.se
3139753Sandreas@sandberg.pp.se        DPRINTF(Drain, "KVM CPU is waiting for service completion, "
3149753Sandreas@sandberg.pp.se                "requesting drain.\n");
3159753Sandreas@sandberg.pp.se        return 1;
3169753Sandreas@sandberg.pp.se
3179753Sandreas@sandberg.pp.se      case RunningService:
3189753Sandreas@sandberg.pp.se        // We need to drain since the CPU is waiting for service (e.g., MMIOs)
3199753Sandreas@sandberg.pp.se        drainManager = dm;
3209753Sandreas@sandberg.pp.se
3219753Sandreas@sandberg.pp.se        DPRINTF(Drain, "KVM CPU is waiting for service, requesting drain.\n");
3229753Sandreas@sandberg.pp.se        return 1;
3239753Sandreas@sandberg.pp.se
3249753Sandreas@sandberg.pp.se      default:
3259753Sandreas@sandberg.pp.se        panic("KVM: Unhandled CPU state in drain()\n");
3269753Sandreas@sandberg.pp.se        return 0;
3279753Sandreas@sandberg.pp.se    }
3289651SAndreas.Sandberg@ARM.com}
3299651SAndreas.Sandberg@ARM.com
3309651SAndreas.Sandberg@ARM.comvoid
3319651SAndreas.Sandberg@ARM.comBaseKvmCPU::drainResume()
3329651SAndreas.Sandberg@ARM.com{
3339651SAndreas.Sandberg@ARM.com    assert(!tickEvent.scheduled());
3349651SAndreas.Sandberg@ARM.com
3359651SAndreas.Sandberg@ARM.com    // We might have been switched out. In that case, we don't need to
3369651SAndreas.Sandberg@ARM.com    // do anything.
3379651SAndreas.Sandberg@ARM.com    if (switchedOut())
3389651SAndreas.Sandberg@ARM.com        return;
3399651SAndreas.Sandberg@ARM.com
3409651SAndreas.Sandberg@ARM.com    DPRINTF(Kvm, "drainResume\n");
3419651SAndreas.Sandberg@ARM.com    verifyMemoryMode();
3429651SAndreas.Sandberg@ARM.com
3439651SAndreas.Sandberg@ARM.com    // The tick event is de-scheduled as a part of the draining
3449651SAndreas.Sandberg@ARM.com    // process. Re-schedule it if the thread context is active.
3459651SAndreas.Sandberg@ARM.com    if (tc->status() == ThreadContext::Active) {
3469651SAndreas.Sandberg@ARM.com        schedule(tickEvent, nextCycle());
3479651SAndreas.Sandberg@ARM.com        _status = Running;
3489651SAndreas.Sandberg@ARM.com    } else {
3499651SAndreas.Sandberg@ARM.com        _status = Idle;
3509651SAndreas.Sandberg@ARM.com    }
3519651SAndreas.Sandberg@ARM.com}
3529651SAndreas.Sandberg@ARM.com
3539651SAndreas.Sandberg@ARM.comvoid
3549651SAndreas.Sandberg@ARM.comBaseKvmCPU::switchOut()
3559651SAndreas.Sandberg@ARM.com{
3569652SAndreas.Sandberg@ARM.com    DPRINTF(Kvm, "switchOut\n");
3579652SAndreas.Sandberg@ARM.com
3589651SAndreas.Sandberg@ARM.com    BaseCPU::switchOut();
3599651SAndreas.Sandberg@ARM.com
3609651SAndreas.Sandberg@ARM.com    // We should have drained prior to executing a switchOut, which
3619651SAndreas.Sandberg@ARM.com    // means that the tick event shouldn't be scheduled and the CPU is
3629651SAndreas.Sandberg@ARM.com    // idle.
3639651SAndreas.Sandberg@ARM.com    assert(!tickEvent.scheduled());
3649651SAndreas.Sandberg@ARM.com    assert(_status == Idle);
3659651SAndreas.Sandberg@ARM.com}
3669651SAndreas.Sandberg@ARM.com
3679651SAndreas.Sandberg@ARM.comvoid
3689651SAndreas.Sandberg@ARM.comBaseKvmCPU::takeOverFrom(BaseCPU *cpu)
3699651SAndreas.Sandberg@ARM.com{
3709651SAndreas.Sandberg@ARM.com    DPRINTF(Kvm, "takeOverFrom\n");
3719651SAndreas.Sandberg@ARM.com
3729651SAndreas.Sandberg@ARM.com    BaseCPU::takeOverFrom(cpu);
3739651SAndreas.Sandberg@ARM.com
3749651SAndreas.Sandberg@ARM.com    // We should have drained prior to executing a switchOut, which
3759651SAndreas.Sandberg@ARM.com    // means that the tick event shouldn't be scheduled and the CPU is
3769651SAndreas.Sandberg@ARM.com    // idle.
3779651SAndreas.Sandberg@ARM.com    assert(!tickEvent.scheduled());
3789651SAndreas.Sandberg@ARM.com    assert(_status == Idle);
3799651SAndreas.Sandberg@ARM.com    assert(threadContexts.size() == 1);
3809651SAndreas.Sandberg@ARM.com
3819753Sandreas@sandberg.pp.se    // Force an update of the KVM state here instead of flagging the
3829753Sandreas@sandberg.pp.se    // TC as dirty. This is not ideal from a performance point of
3839753Sandreas@sandberg.pp.se    // view, but it makes debugging easier as it allows meaningful KVM
3849753Sandreas@sandberg.pp.se    // state to be dumped before and after a takeover.
3859753Sandreas@sandberg.pp.se    updateKvmState();
3869753Sandreas@sandberg.pp.se    threadContextDirty = false;
3879651SAndreas.Sandberg@ARM.com}
3889651SAndreas.Sandberg@ARM.com
3899651SAndreas.Sandberg@ARM.comvoid
3909651SAndreas.Sandberg@ARM.comBaseKvmCPU::verifyMemoryMode() const
3919651SAndreas.Sandberg@ARM.com{
3929651SAndreas.Sandberg@ARM.com    if (!(system->isAtomicMode() && system->bypassCaches())) {
3939651SAndreas.Sandberg@ARM.com        fatal("The KVM-based CPUs requires the memory system to be in the "
3949651SAndreas.Sandberg@ARM.com              "'atomic_noncaching' mode.\n");
3959651SAndreas.Sandberg@ARM.com    }
3969651SAndreas.Sandberg@ARM.com}
3979651SAndreas.Sandberg@ARM.com
3989651SAndreas.Sandberg@ARM.comvoid
3999651SAndreas.Sandberg@ARM.comBaseKvmCPU::wakeup()
4009651SAndreas.Sandberg@ARM.com{
4019651SAndreas.Sandberg@ARM.com    DPRINTF(Kvm, "wakeup()\n");
4029651SAndreas.Sandberg@ARM.com
4039651SAndreas.Sandberg@ARM.com    if (thread->status() != ThreadContext::Suspended)
4049651SAndreas.Sandberg@ARM.com        return;
4059651SAndreas.Sandberg@ARM.com
4069651SAndreas.Sandberg@ARM.com    thread->activate();
4079651SAndreas.Sandberg@ARM.com}
4089651SAndreas.Sandberg@ARM.com
4099651SAndreas.Sandberg@ARM.comvoid
4109651SAndreas.Sandberg@ARM.comBaseKvmCPU::activateContext(ThreadID thread_num, Cycles delay)
4119651SAndreas.Sandberg@ARM.com{
4129651SAndreas.Sandberg@ARM.com    DPRINTF(Kvm, "ActivateContext %d (%d cycles)\n", thread_num, delay);
4139651SAndreas.Sandberg@ARM.com
4149651SAndreas.Sandberg@ARM.com    assert(thread_num == 0);
4159651SAndreas.Sandberg@ARM.com    assert(thread);
4169651SAndreas.Sandberg@ARM.com
4179651SAndreas.Sandberg@ARM.com    assert(_status == Idle);
4189651SAndreas.Sandberg@ARM.com    assert(!tickEvent.scheduled());
4199651SAndreas.Sandberg@ARM.com
4209754Sandreas@sandberg.pp.se    numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend);
4219651SAndreas.Sandberg@ARM.com
4229651SAndreas.Sandberg@ARM.com    schedule(tickEvent, clockEdge(delay));
4239651SAndreas.Sandberg@ARM.com    _status = Running;
4249651SAndreas.Sandberg@ARM.com}
4259651SAndreas.Sandberg@ARM.com
4269651SAndreas.Sandberg@ARM.com
4279651SAndreas.Sandberg@ARM.comvoid
4289651SAndreas.Sandberg@ARM.comBaseKvmCPU::suspendContext(ThreadID thread_num)
4299651SAndreas.Sandberg@ARM.com{
4309651SAndreas.Sandberg@ARM.com    DPRINTF(Kvm, "SuspendContext %d\n", thread_num);
4319651SAndreas.Sandberg@ARM.com
4329651SAndreas.Sandberg@ARM.com    assert(thread_num == 0);
4339651SAndreas.Sandberg@ARM.com    assert(thread);
4349651SAndreas.Sandberg@ARM.com
4359651SAndreas.Sandberg@ARM.com    if (_status == Idle)
4369651SAndreas.Sandberg@ARM.com        return;
4379651SAndreas.Sandberg@ARM.com
4389651SAndreas.Sandberg@ARM.com    assert(_status == Running);
4399651SAndreas.Sandberg@ARM.com
4409651SAndreas.Sandberg@ARM.com    // The tick event may no be scheduled if the quest has requested
4419651SAndreas.Sandberg@ARM.com    // the monitor to wait for interrupts. The normal CPU models can
4429651SAndreas.Sandberg@ARM.com    // get their tick events descheduled by quiesce instructions, but
4439651SAndreas.Sandberg@ARM.com    // that can't happen here.
4449651SAndreas.Sandberg@ARM.com    if (tickEvent.scheduled())
4459651SAndreas.Sandberg@ARM.com        deschedule(tickEvent);
4469651SAndreas.Sandberg@ARM.com
4479651SAndreas.Sandberg@ARM.com    _status = Idle;
4489651SAndreas.Sandberg@ARM.com}
4499651SAndreas.Sandberg@ARM.com
4509651SAndreas.Sandberg@ARM.comvoid
4519651SAndreas.Sandberg@ARM.comBaseKvmCPU::deallocateContext(ThreadID thread_num)
4529651SAndreas.Sandberg@ARM.com{
4539651SAndreas.Sandberg@ARM.com    // for now, these are equivalent
4549651SAndreas.Sandberg@ARM.com    suspendContext(thread_num);
4559651SAndreas.Sandberg@ARM.com}
4569651SAndreas.Sandberg@ARM.com
4579651SAndreas.Sandberg@ARM.comvoid
4589651SAndreas.Sandberg@ARM.comBaseKvmCPU::haltContext(ThreadID thread_num)
4599651SAndreas.Sandberg@ARM.com{
4609651SAndreas.Sandberg@ARM.com    // for now, these are equivalent
4619651SAndreas.Sandberg@ARM.com    suspendContext(thread_num);
4629651SAndreas.Sandberg@ARM.com}
4639651SAndreas.Sandberg@ARM.com
4649652SAndreas.Sandberg@ARM.comThreadContext *
4659652SAndreas.Sandberg@ARM.comBaseKvmCPU::getContext(int tn)
4669652SAndreas.Sandberg@ARM.com{
4679652SAndreas.Sandberg@ARM.com    assert(tn == 0);
4689652SAndreas.Sandberg@ARM.com    syncThreadContext();
4699652SAndreas.Sandberg@ARM.com    return tc;
4709652SAndreas.Sandberg@ARM.com}
4719652SAndreas.Sandberg@ARM.com
4729652SAndreas.Sandberg@ARM.com
4739651SAndreas.Sandberg@ARM.comCounter
4749651SAndreas.Sandberg@ARM.comBaseKvmCPU::totalInsts() const
4759651SAndreas.Sandberg@ARM.com{
4769752Sandreas@sandberg.pp.se    return ctrInsts;
4779651SAndreas.Sandberg@ARM.com}
4789651SAndreas.Sandberg@ARM.com
4799651SAndreas.Sandberg@ARM.comCounter
4809651SAndreas.Sandberg@ARM.comBaseKvmCPU::totalOps() const
4819651SAndreas.Sandberg@ARM.com{
4829651SAndreas.Sandberg@ARM.com    hack_once("Pretending totalOps is equivalent to totalInsts()\n");
4839752Sandreas@sandberg.pp.se    return ctrInsts;
4849651SAndreas.Sandberg@ARM.com}
4859651SAndreas.Sandberg@ARM.com
4869651SAndreas.Sandberg@ARM.comvoid
4879651SAndreas.Sandberg@ARM.comBaseKvmCPU::dump()
4889651SAndreas.Sandberg@ARM.com{
4899651SAndreas.Sandberg@ARM.com    inform("State dumping not implemented.");
4909651SAndreas.Sandberg@ARM.com}
4919651SAndreas.Sandberg@ARM.com
4929651SAndreas.Sandberg@ARM.comvoid
4939651SAndreas.Sandberg@ARM.comBaseKvmCPU::tick()
4949651SAndreas.Sandberg@ARM.com{
4959753Sandreas@sandberg.pp.se    Tick delay(0);
4969753Sandreas@sandberg.pp.se    assert(_status != Idle);
4979651SAndreas.Sandberg@ARM.com
4989651SAndreas.Sandberg@ARM.com    switch (_status) {
4999753Sandreas@sandberg.pp.se      case RunningService:
5009753Sandreas@sandberg.pp.se        // handleKvmExit() will determine the next state of the CPU
5019753Sandreas@sandberg.pp.se        delay = handleKvmExit();
5029753Sandreas@sandberg.pp.se
5039753Sandreas@sandberg.pp.se        if (tryDrain())
5049753Sandreas@sandberg.pp.se            _status = Idle;
5059651SAndreas.Sandberg@ARM.com        break;
5069651SAndreas.Sandberg@ARM.com
5079753Sandreas@sandberg.pp.se      case RunningServiceCompletion:
5089753Sandreas@sandberg.pp.se      case Running: {
5099983Sstever@gmail.com          EventQueue *q = curEventQueue();
5109983Sstever@gmail.com          Tick ticksToExecute(q->nextTick() - curTick());
5119753Sandreas@sandberg.pp.se
5129753Sandreas@sandberg.pp.se          // We might need to update the KVM state.
5139753Sandreas@sandberg.pp.se          syncKvmState();
5149753Sandreas@sandberg.pp.se
5159892Sandreas@sandberg.pp.se          // Setup any pending instruction count breakpoints using
5169892Sandreas@sandberg.pp.se          // PerfEvent.
5179892Sandreas@sandberg.pp.se          setupInstStop();
5189892Sandreas@sandberg.pp.se
5199753Sandreas@sandberg.pp.se          DPRINTF(KvmRun, "Entering KVM...\n");
5209753Sandreas@sandberg.pp.se          if (drainManager) {
5219753Sandreas@sandberg.pp.se              // Force an immediate exit from KVM after completing
5229753Sandreas@sandberg.pp.se              // pending operations. The architecture-specific code
5239753Sandreas@sandberg.pp.se              // takes care to run until it is in a state where it can
5249753Sandreas@sandberg.pp.se              // safely be drained.
5259753Sandreas@sandberg.pp.se              delay = kvmRunDrain();
5269753Sandreas@sandberg.pp.se          } else {
5279753Sandreas@sandberg.pp.se              delay = kvmRun(ticksToExecute);
5289753Sandreas@sandberg.pp.se          }
5299753Sandreas@sandberg.pp.se
5309753Sandreas@sandberg.pp.se          // Entering into KVM implies that we'll have to reload the thread
5319753Sandreas@sandberg.pp.se          // context from KVM if we want to access it. Flag the KVM state as
5329753Sandreas@sandberg.pp.se          // dirty with respect to the cached thread context.
5339753Sandreas@sandberg.pp.se          kvmStateDirty = true;
5349753Sandreas@sandberg.pp.se
5359753Sandreas@sandberg.pp.se          // Enter into the RunningService state unless the
5369753Sandreas@sandberg.pp.se          // simulation was stopped by a timer.
5379755Sandreas@sandberg.pp.se          if (_kvmRun->exit_reason !=  KVM_EXIT_INTR) {
5389753Sandreas@sandberg.pp.se              _status = RunningService;
5399755Sandreas@sandberg.pp.se          } else {
5409755Sandreas@sandberg.pp.se              ++numExitSignal;
5419753Sandreas@sandberg.pp.se              _status = Running;
5429755Sandreas@sandberg.pp.se          }
5439753Sandreas@sandberg.pp.se
5449904Sandreas@sandberg.pp.se          // Service any pending instruction events. The vCPU should
5459904Sandreas@sandberg.pp.se          // have exited in time for the event using the instruction
5469904Sandreas@sandberg.pp.se          // counter configured by setupInstStop().
5479904Sandreas@sandberg.pp.se          comInstEventQueue[0]->serviceEvents(ctrInsts);
5489904Sandreas@sandberg.pp.se          system->instEventQueue.serviceEvents(system->totalNumInsts);
5499904Sandreas@sandberg.pp.se
5509753Sandreas@sandberg.pp.se          if (tryDrain())
5519753Sandreas@sandberg.pp.se              _status = Idle;
5529753Sandreas@sandberg.pp.se      } break;
5539753Sandreas@sandberg.pp.se
5549651SAndreas.Sandberg@ARM.com      default:
5559753Sandreas@sandberg.pp.se        panic("BaseKvmCPU entered tick() in an illegal state (%i)\n",
5569753Sandreas@sandberg.pp.se              _status);
5579651SAndreas.Sandberg@ARM.com    }
5589753Sandreas@sandberg.pp.se
5599753Sandreas@sandberg.pp.se    // Schedule a new tick if we are still running
5609753Sandreas@sandberg.pp.se    if (_status != Idle)
5619753Sandreas@sandberg.pp.se        schedule(tickEvent, clockEdge(ticksToCycles(delay)));
5629753Sandreas@sandberg.pp.se}
5639753Sandreas@sandberg.pp.se
5649753Sandreas@sandberg.pp.seTick
5659753Sandreas@sandberg.pp.seBaseKvmCPU::kvmRunDrain()
5669753Sandreas@sandberg.pp.se{
5679753Sandreas@sandberg.pp.se    // By default, the only thing we need to drain is a pending IO
5689753Sandreas@sandberg.pp.se    // operation which assumes that we are in the
5699753Sandreas@sandberg.pp.se    // RunningServiceCompletion state.
5709753Sandreas@sandberg.pp.se    assert(_status == RunningServiceCompletion);
5719753Sandreas@sandberg.pp.se
5729753Sandreas@sandberg.pp.se    // Deliver the data from the pending IO operation and immediately
5739753Sandreas@sandberg.pp.se    // exit.
5749753Sandreas@sandberg.pp.se    return kvmRun(0);
5759651SAndreas.Sandberg@ARM.com}
5769651SAndreas.Sandberg@ARM.com
5779735Sandreas@sandberg.pp.seuint64_t
5789735Sandreas@sandberg.pp.seBaseKvmCPU::getHostCycles() const
5799735Sandreas@sandberg.pp.se{
5809735Sandreas@sandberg.pp.se    return hwCycles.read();
5819735Sandreas@sandberg.pp.se}
5829735Sandreas@sandberg.pp.se
5839651SAndreas.Sandberg@ARM.comTick
5849651SAndreas.Sandberg@ARM.comBaseKvmCPU::kvmRun(Tick ticks)
5859651SAndreas.Sandberg@ARM.com{
5869753Sandreas@sandberg.pp.se    Tick ticksExecuted;
5879651SAndreas.Sandberg@ARM.com    DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks);
5889651SAndreas.Sandberg@ARM.com    timerOverflowed = false;
5899655SAndreas.Sandberg@ARM.com
5909753Sandreas@sandberg.pp.se    if (ticks == 0) {
5919753Sandreas@sandberg.pp.se        // Settings ticks == 0 is a special case which causes an entry
5929753Sandreas@sandberg.pp.se        // into KVM that finishes pending operations (e.g., IO) and
5939753Sandreas@sandberg.pp.se        // then immediately exits.
5949753Sandreas@sandberg.pp.se        DPRINTF(KvmRun, "KVM: Delivering IO without full guest entry\n");
5959735Sandreas@sandberg.pp.se
5969755Sandreas@sandberg.pp.se        ++numVMHalfEntries;
5979755Sandreas@sandberg.pp.se
5989753Sandreas@sandberg.pp.se        // This signal is always masked while we are executing in gem5
5999753Sandreas@sandberg.pp.se        // and gets unmasked temporarily as soon as we enter into
6009753Sandreas@sandberg.pp.se        // KVM. See setSignalMask() and setupSignalHandler().
6019753Sandreas@sandberg.pp.se        raise(KVM_TIMER_SIGNAL);
6029655SAndreas.Sandberg@ARM.com
6039753Sandreas@sandberg.pp.se        // Enter into KVM. KVM will check for signals after completing
6049753Sandreas@sandberg.pp.se        // pending operations (IO). Since the KVM_TIMER_SIGNAL is
6059753Sandreas@sandberg.pp.se        // pending, this forces an immediate exit into gem5 again. We
6069753Sandreas@sandberg.pp.se        // don't bother to setup timers since this shouldn't actually
6079753Sandreas@sandberg.pp.se        // execute any code in the guest.
6089753Sandreas@sandberg.pp.se        ioctlRun();
6099753Sandreas@sandberg.pp.se
6109753Sandreas@sandberg.pp.se        // We always execute at least one cycle to prevent the
6119753Sandreas@sandberg.pp.se        // BaseKvmCPU::tick() to be rescheduled on the same tick
6129753Sandreas@sandberg.pp.se        // twice.
6139753Sandreas@sandberg.pp.se        ticksExecuted = clockPeriod();
6149753Sandreas@sandberg.pp.se    } else {
6159753Sandreas@sandberg.pp.se        if (ticks < runTimer->resolution()) {
6169753Sandreas@sandberg.pp.se            DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
6179753Sandreas@sandberg.pp.se                    ticks, runTimer->resolution());
6189753Sandreas@sandberg.pp.se            ticks = runTimer->resolution();
6199753Sandreas@sandberg.pp.se        }
6209753Sandreas@sandberg.pp.se
6219753Sandreas@sandberg.pp.se        // Get hardware statistics after synchronizing contexts. The KVM
6229753Sandreas@sandberg.pp.se        // state update might affect guest cycle counters.
6239753Sandreas@sandberg.pp.se        uint64_t baseCycles(getHostCycles());
6249753Sandreas@sandberg.pp.se        uint64_t baseInstrs(hwInstructions.read());
6259753Sandreas@sandberg.pp.se
6269753Sandreas@sandberg.pp.se        // Arm the run timer and start the cycle timer if it isn't
6279753Sandreas@sandberg.pp.se        // controlled by the overflow timer. Starting/stopping the cycle
6289753Sandreas@sandberg.pp.se        // timer automatically starts the other perf timers as they are in
6299753Sandreas@sandberg.pp.se        // the same counter group.
6309753Sandreas@sandberg.pp.se        runTimer->arm(ticks);
6319753Sandreas@sandberg.pp.se        if (!perfControlledByTimer)
6329753Sandreas@sandberg.pp.se            hwCycles.start();
6339753Sandreas@sandberg.pp.se
6349753Sandreas@sandberg.pp.se        ioctlRun();
6359753Sandreas@sandberg.pp.se
6369753Sandreas@sandberg.pp.se        runTimer->disarm();
6379753Sandreas@sandberg.pp.se        if (!perfControlledByTimer)
6389753Sandreas@sandberg.pp.se            hwCycles.stop();
6399753Sandreas@sandberg.pp.se
6409753Sandreas@sandberg.pp.se        // The timer signal may have been delivered after we exited
6419753Sandreas@sandberg.pp.se        // from KVM. It will be pending in that case since it is
6429753Sandreas@sandberg.pp.se        // masked when we aren't executing in KVM. Discard it to make
6439753Sandreas@sandberg.pp.se        // sure we don't deliver it immediately next time we try to
6449753Sandreas@sandberg.pp.se        // enter into KVM.
6459753Sandreas@sandberg.pp.se        discardPendingSignal(KVM_TIMER_SIGNAL);
6469892Sandreas@sandberg.pp.se        discardPendingSignal(KVM_INST_SIGNAL);
6479753Sandreas@sandberg.pp.se
6489753Sandreas@sandberg.pp.se        const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles);
6499753Sandreas@sandberg.pp.se        const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor);
6509753Sandreas@sandberg.pp.se        const uint64_t instsExecuted(hwInstructions.read() - baseInstrs);
6519753Sandreas@sandberg.pp.se        ticksExecuted = runTimer->ticksFromHostCycles(hostCyclesExecuted);
6529753Sandreas@sandberg.pp.se
6539753Sandreas@sandberg.pp.se        if (ticksExecuted < ticks &&
6549753Sandreas@sandberg.pp.se            timerOverflowed &&
6559753Sandreas@sandberg.pp.se            _kvmRun->exit_reason == KVM_EXIT_INTR) {
6569753Sandreas@sandberg.pp.se            // TODO: We should probably do something clever here...
6579753Sandreas@sandberg.pp.se            warn("KVM: Early timer event, requested %i ticks but got %i ticks.\n",
6589753Sandreas@sandberg.pp.se                 ticks, ticksExecuted);
6599753Sandreas@sandberg.pp.se        }
6609753Sandreas@sandberg.pp.se
6619753Sandreas@sandberg.pp.se        /* Update statistics */
6629753Sandreas@sandberg.pp.se        numCycles += simCyclesExecuted;;
6639753Sandreas@sandberg.pp.se        numInsts += instsExecuted;
6649753Sandreas@sandberg.pp.se        ctrInsts += instsExecuted;
6659753Sandreas@sandberg.pp.se        system->totalNumInsts += instsExecuted;
6669753Sandreas@sandberg.pp.se
6679753Sandreas@sandberg.pp.se        DPRINTF(KvmRun,
6689753Sandreas@sandberg.pp.se                "KVM: Executed %i instructions in %i cycles "
6699753Sandreas@sandberg.pp.se                "(%i ticks, sim cycles: %i).\n",
6709753Sandreas@sandberg.pp.se                instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted);
6719651SAndreas.Sandberg@ARM.com    }
6729655SAndreas.Sandberg@ARM.com
6739651SAndreas.Sandberg@ARM.com    ++numVMExits;
6749651SAndreas.Sandberg@ARM.com
6759651SAndreas.Sandberg@ARM.com    return ticksExecuted + flushCoalescedMMIO();
6769651SAndreas.Sandberg@ARM.com}
6779651SAndreas.Sandberg@ARM.com
6789651SAndreas.Sandberg@ARM.comvoid
6799651SAndreas.Sandberg@ARM.comBaseKvmCPU::kvmNonMaskableInterrupt()
6809651SAndreas.Sandberg@ARM.com{
6819651SAndreas.Sandberg@ARM.com    ++numInterrupts;
6829651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_NMI) == -1)
6839651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to deliver NMI to virtual CPU\n");
6849651SAndreas.Sandberg@ARM.com}
6859651SAndreas.Sandberg@ARM.com
6869651SAndreas.Sandberg@ARM.comvoid
6879651SAndreas.Sandberg@ARM.comBaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt)
6889651SAndreas.Sandberg@ARM.com{
6899651SAndreas.Sandberg@ARM.com    ++numInterrupts;
6909651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1)
6919651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to deliver interrupt to virtual CPU\n");
6929651SAndreas.Sandberg@ARM.com}
6939651SAndreas.Sandberg@ARM.com
6949651SAndreas.Sandberg@ARM.comvoid
6959651SAndreas.Sandberg@ARM.comBaseKvmCPU::getRegisters(struct kvm_regs &regs) const
6969651SAndreas.Sandberg@ARM.com{
6979651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_GET_REGS, &regs) == -1)
6989651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to get guest registers\n");
6999651SAndreas.Sandberg@ARM.com}
7009651SAndreas.Sandberg@ARM.com
7019651SAndreas.Sandberg@ARM.comvoid
7029651SAndreas.Sandberg@ARM.comBaseKvmCPU::setRegisters(const struct kvm_regs &regs)
7039651SAndreas.Sandberg@ARM.com{
7049651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_SET_REGS, (void *)&regs) == -1)
7059651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to set guest registers\n");
7069651SAndreas.Sandberg@ARM.com}
7079651SAndreas.Sandberg@ARM.com
7089651SAndreas.Sandberg@ARM.comvoid
7099651SAndreas.Sandberg@ARM.comBaseKvmCPU::getSpecialRegisters(struct kvm_sregs &regs) const
7109651SAndreas.Sandberg@ARM.com{
7119651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_GET_SREGS, &regs) == -1)
7129651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to get guest special registers\n");
7139651SAndreas.Sandberg@ARM.com}
7149651SAndreas.Sandberg@ARM.com
7159651SAndreas.Sandberg@ARM.comvoid
7169651SAndreas.Sandberg@ARM.comBaseKvmCPU::setSpecialRegisters(const struct kvm_sregs &regs)
7179651SAndreas.Sandberg@ARM.com{
7189651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_SET_SREGS, (void *)&regs) == -1)
7199651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to set guest special registers\n");
7209651SAndreas.Sandberg@ARM.com}
7219651SAndreas.Sandberg@ARM.com
7229651SAndreas.Sandberg@ARM.comvoid
7239651SAndreas.Sandberg@ARM.comBaseKvmCPU::getFPUState(struct kvm_fpu &state) const
7249651SAndreas.Sandberg@ARM.com{
7259651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_GET_FPU, &state) == -1)
7269651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to get guest FPU state\n");
7279651SAndreas.Sandberg@ARM.com}
7289651SAndreas.Sandberg@ARM.com
7299651SAndreas.Sandberg@ARM.comvoid
7309651SAndreas.Sandberg@ARM.comBaseKvmCPU::setFPUState(const struct kvm_fpu &state)
7319651SAndreas.Sandberg@ARM.com{
7329651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_SET_FPU, (void *)&state) == -1)
7339651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to set guest FPU state\n");
7349651SAndreas.Sandberg@ARM.com}
7359651SAndreas.Sandberg@ARM.com
7369651SAndreas.Sandberg@ARM.com
7379651SAndreas.Sandberg@ARM.comvoid
7389651SAndreas.Sandberg@ARM.comBaseKvmCPU::setOneReg(uint64_t id, const void *addr)
7399651SAndreas.Sandberg@ARM.com{
7409651SAndreas.Sandberg@ARM.com#ifdef KVM_SET_ONE_REG
7419651SAndreas.Sandberg@ARM.com    struct kvm_one_reg reg;
7429651SAndreas.Sandberg@ARM.com    reg.id = id;
7439651SAndreas.Sandberg@ARM.com    reg.addr = (uint64_t)addr;
7449651SAndreas.Sandberg@ARM.com
7459651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_SET_ONE_REG, &reg) == -1) {
7469651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
7479651SAndreas.Sandberg@ARM.com              id, errno);
7489651SAndreas.Sandberg@ARM.com    }
7499651SAndreas.Sandberg@ARM.com#else
7509651SAndreas.Sandberg@ARM.com    panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
7519651SAndreas.Sandberg@ARM.com#endif
7529651SAndreas.Sandberg@ARM.com}
7539651SAndreas.Sandberg@ARM.com
7549651SAndreas.Sandberg@ARM.comvoid
7559651SAndreas.Sandberg@ARM.comBaseKvmCPU::getOneReg(uint64_t id, void *addr) const
7569651SAndreas.Sandberg@ARM.com{
7579651SAndreas.Sandberg@ARM.com#ifdef KVM_GET_ONE_REG
7589651SAndreas.Sandberg@ARM.com    struct kvm_one_reg reg;
7599651SAndreas.Sandberg@ARM.com    reg.id = id;
7609651SAndreas.Sandberg@ARM.com    reg.addr = (uint64_t)addr;
7619651SAndreas.Sandberg@ARM.com
7629651SAndreas.Sandberg@ARM.com    if (ioctl(KVM_GET_ONE_REG, &reg) == -1) {
7639651SAndreas.Sandberg@ARM.com        panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
7649651SAndreas.Sandberg@ARM.com              id, errno);
7659651SAndreas.Sandberg@ARM.com    }
7669651SAndreas.Sandberg@ARM.com#else
7679651SAndreas.Sandberg@ARM.com    panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
7689651SAndreas.Sandberg@ARM.com#endif
7699651SAndreas.Sandberg@ARM.com}
7709651SAndreas.Sandberg@ARM.com
7719651SAndreas.Sandberg@ARM.comstd::string
7729651SAndreas.Sandberg@ARM.comBaseKvmCPU::getAndFormatOneReg(uint64_t id) const
7739651SAndreas.Sandberg@ARM.com{
7749651SAndreas.Sandberg@ARM.com#ifdef KVM_GET_ONE_REG
7759651SAndreas.Sandberg@ARM.com    std::ostringstream ss;
7769651SAndreas.Sandberg@ARM.com
7779651SAndreas.Sandberg@ARM.com    ss.setf(std::ios::hex, std::ios::basefield);
7789651SAndreas.Sandberg@ARM.com    ss.setf(std::ios::showbase);
7799651SAndreas.Sandberg@ARM.com#define HANDLE_INTTYPE(len)                      \
7809651SAndreas.Sandberg@ARM.com    case KVM_REG_SIZE_U ## len: {                \
7819651SAndreas.Sandberg@ARM.com        uint ## len ## _t value;                 \
7829651SAndreas.Sandberg@ARM.com        getOneReg(id, &value);                   \
7839651SAndreas.Sandberg@ARM.com        ss << value;                             \
7849651SAndreas.Sandberg@ARM.com    }  break
7859651SAndreas.Sandberg@ARM.com
7869651SAndreas.Sandberg@ARM.com#define HANDLE_ARRAY(len)                       \
7879651SAndreas.Sandberg@ARM.com    case KVM_REG_SIZE_U ## len: {               \
7889651SAndreas.Sandberg@ARM.com        uint8_t value[len / 8];                 \
7899651SAndreas.Sandberg@ARM.com        getOneReg(id, value);                   \
7909651SAndreas.Sandberg@ARM.com        ss << "[" << value[0];                  \
7919651SAndreas.Sandberg@ARM.com        for (int i = 1; i < len  / 8; ++i)      \
7929651SAndreas.Sandberg@ARM.com            ss << ", " << value[i];             \
7939651SAndreas.Sandberg@ARM.com        ss << "]";                              \
7949651SAndreas.Sandberg@ARM.com      } break
7959651SAndreas.Sandberg@ARM.com
7969651SAndreas.Sandberg@ARM.com    switch (id & KVM_REG_SIZE_MASK) {
7979651SAndreas.Sandberg@ARM.com        HANDLE_INTTYPE(8);
7989651SAndreas.Sandberg@ARM.com        HANDLE_INTTYPE(16);
7999651SAndreas.Sandberg@ARM.com        HANDLE_INTTYPE(32);
8009651SAndreas.Sandberg@ARM.com        HANDLE_INTTYPE(64);
8019651SAndreas.Sandberg@ARM.com        HANDLE_ARRAY(128);
8029651SAndreas.Sandberg@ARM.com        HANDLE_ARRAY(256);
8039651SAndreas.Sandberg@ARM.com        HANDLE_ARRAY(512);
8049651SAndreas.Sandberg@ARM.com        HANDLE_ARRAY(1024);
8059651SAndreas.Sandberg@ARM.com      default:
8069651SAndreas.Sandberg@ARM.com        ss << "??";
8079651SAndreas.Sandberg@ARM.com    }
8089651SAndreas.Sandberg@ARM.com
8099651SAndreas.Sandberg@ARM.com#undef HANDLE_INTTYPE
8109651SAndreas.Sandberg@ARM.com#undef HANDLE_ARRAY
8119651SAndreas.Sandberg@ARM.com
8129651SAndreas.Sandberg@ARM.com    return ss.str();
8139651SAndreas.Sandberg@ARM.com#else
8149651SAndreas.Sandberg@ARM.com    panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
8159651SAndreas.Sandberg@ARM.com#endif
8169651SAndreas.Sandberg@ARM.com}
8179651SAndreas.Sandberg@ARM.com
8189652SAndreas.Sandberg@ARM.comvoid
8199652SAndreas.Sandberg@ARM.comBaseKvmCPU::syncThreadContext()
8209652SAndreas.Sandberg@ARM.com{
8219652SAndreas.Sandberg@ARM.com    if (!kvmStateDirty)
8229652SAndreas.Sandberg@ARM.com        return;
8239652SAndreas.Sandberg@ARM.com
8249652SAndreas.Sandberg@ARM.com    assert(!threadContextDirty);
8259652SAndreas.Sandberg@ARM.com
8269652SAndreas.Sandberg@ARM.com    updateThreadContext();
8279652SAndreas.Sandberg@ARM.com    kvmStateDirty = false;
8289652SAndreas.Sandberg@ARM.com}
8299652SAndreas.Sandberg@ARM.com
8309652SAndreas.Sandberg@ARM.comvoid
8319652SAndreas.Sandberg@ARM.comBaseKvmCPU::syncKvmState()
8329652SAndreas.Sandberg@ARM.com{
8339652SAndreas.Sandberg@ARM.com    if (!threadContextDirty)
8349652SAndreas.Sandberg@ARM.com        return;
8359652SAndreas.Sandberg@ARM.com
8369652SAndreas.Sandberg@ARM.com    assert(!kvmStateDirty);
8379652SAndreas.Sandberg@ARM.com
8389652SAndreas.Sandberg@ARM.com    updateKvmState();
8399652SAndreas.Sandberg@ARM.com    threadContextDirty = false;
8409652SAndreas.Sandberg@ARM.com}
8419652SAndreas.Sandberg@ARM.com
8429651SAndreas.Sandberg@ARM.comTick
8439651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExit()
8449651SAndreas.Sandberg@ARM.com{
8459651SAndreas.Sandberg@ARM.com    DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason);
8469753Sandreas@sandberg.pp.se    assert(_status == RunningService);
8479651SAndreas.Sandberg@ARM.com
8489753Sandreas@sandberg.pp.se    // Switch into the running state by default. Individual handlers
8499753Sandreas@sandberg.pp.se    // can override this.
8509753Sandreas@sandberg.pp.se    _status = Running;
8519651SAndreas.Sandberg@ARM.com    switch (_kvmRun->exit_reason) {
8529651SAndreas.Sandberg@ARM.com      case KVM_EXIT_UNKNOWN:
8539651SAndreas.Sandberg@ARM.com        return handleKvmExitUnknown();
8549651SAndreas.Sandberg@ARM.com
8559651SAndreas.Sandberg@ARM.com      case KVM_EXIT_EXCEPTION:
8569651SAndreas.Sandberg@ARM.com        return handleKvmExitException();
8579651SAndreas.Sandberg@ARM.com
8589651SAndreas.Sandberg@ARM.com      case KVM_EXIT_IO:
8599753Sandreas@sandberg.pp.se        _status = RunningServiceCompletion;
8609651SAndreas.Sandberg@ARM.com        ++numIO;
8619651SAndreas.Sandberg@ARM.com        return handleKvmExitIO();
8629651SAndreas.Sandberg@ARM.com
8639651SAndreas.Sandberg@ARM.com      case KVM_EXIT_HYPERCALL:
8649651SAndreas.Sandberg@ARM.com        ++numHypercalls;
8659651SAndreas.Sandberg@ARM.com        return handleKvmExitHypercall();
8669651SAndreas.Sandberg@ARM.com
8679651SAndreas.Sandberg@ARM.com      case KVM_EXIT_HLT:
8689651SAndreas.Sandberg@ARM.com        /* The guest has halted and is waiting for interrupts */
8699651SAndreas.Sandberg@ARM.com        DPRINTF(Kvm, "handleKvmExitHalt\n");
8709651SAndreas.Sandberg@ARM.com        ++numHalt;
8719651SAndreas.Sandberg@ARM.com
8729651SAndreas.Sandberg@ARM.com        // Suspend the thread until the next interrupt arrives
8739651SAndreas.Sandberg@ARM.com        thread->suspend();
8749651SAndreas.Sandberg@ARM.com
8759651SAndreas.Sandberg@ARM.com        // This is actually ignored since the thread is suspended.
8769651SAndreas.Sandberg@ARM.com        return 0;
8779651SAndreas.Sandberg@ARM.com
8789651SAndreas.Sandberg@ARM.com      case KVM_EXIT_MMIO:
8799753Sandreas@sandberg.pp.se        _status = RunningServiceCompletion;
8809651SAndreas.Sandberg@ARM.com        /* Service memory mapped IO requests */
8819651SAndreas.Sandberg@ARM.com        DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
8829651SAndreas.Sandberg@ARM.com                _kvmRun->mmio.is_write,
8839651SAndreas.Sandberg@ARM.com                _kvmRun->mmio.phys_addr, _kvmRun->mmio.len);
8849651SAndreas.Sandberg@ARM.com
8859651SAndreas.Sandberg@ARM.com        ++numMMIO;
8869651SAndreas.Sandberg@ARM.com        return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data,
8879651SAndreas.Sandberg@ARM.com                            _kvmRun->mmio.len, _kvmRun->mmio.is_write);
8889651SAndreas.Sandberg@ARM.com
8899651SAndreas.Sandberg@ARM.com      case KVM_EXIT_IRQ_WINDOW_OPEN:
8909651SAndreas.Sandberg@ARM.com        return handleKvmExitIRQWindowOpen();
8919651SAndreas.Sandberg@ARM.com
8929651SAndreas.Sandberg@ARM.com      case KVM_EXIT_FAIL_ENTRY:
8939651SAndreas.Sandberg@ARM.com        return handleKvmExitFailEntry();
8949651SAndreas.Sandberg@ARM.com
8959651SAndreas.Sandberg@ARM.com      case KVM_EXIT_INTR:
8969651SAndreas.Sandberg@ARM.com        /* KVM was interrupted by a signal, restart it in the next
8979651SAndreas.Sandberg@ARM.com         * tick. */
8989651SAndreas.Sandberg@ARM.com        return 0;
8999651SAndreas.Sandberg@ARM.com
9009651SAndreas.Sandberg@ARM.com      case KVM_EXIT_INTERNAL_ERROR:
9019651SAndreas.Sandberg@ARM.com        panic("KVM: Internal error (suberror: %u)\n",
9029651SAndreas.Sandberg@ARM.com              _kvmRun->internal.suberror);
9039651SAndreas.Sandberg@ARM.com
9049651SAndreas.Sandberg@ARM.com      default:
9059689Sandreas@sandberg.pp.se        dump();
9069651SAndreas.Sandberg@ARM.com        panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason);
9079651SAndreas.Sandberg@ARM.com    }
9089651SAndreas.Sandberg@ARM.com}
9099651SAndreas.Sandberg@ARM.com
9109651SAndreas.Sandberg@ARM.comTick
9119651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitIO()
9129651SAndreas.Sandberg@ARM.com{
9139651SAndreas.Sandberg@ARM.com    panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
9149651SAndreas.Sandberg@ARM.com          _kvmRun->io.direction, _kvmRun->io.size,
9159651SAndreas.Sandberg@ARM.com          _kvmRun->io.port, _kvmRun->io.count);
9169651SAndreas.Sandberg@ARM.com}
9179651SAndreas.Sandberg@ARM.com
9189651SAndreas.Sandberg@ARM.comTick
9199651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitHypercall()
9209651SAndreas.Sandberg@ARM.com{
9219651SAndreas.Sandberg@ARM.com    panic("KVM: Unhandled hypercall\n");
9229651SAndreas.Sandberg@ARM.com}
9239651SAndreas.Sandberg@ARM.com
9249651SAndreas.Sandberg@ARM.comTick
9259651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitIRQWindowOpen()
9269651SAndreas.Sandberg@ARM.com{
9279651SAndreas.Sandberg@ARM.com    warn("KVM: Unhandled IRQ window.\n");
9289651SAndreas.Sandberg@ARM.com    return 0;
9299651SAndreas.Sandberg@ARM.com}
9309651SAndreas.Sandberg@ARM.com
9319651SAndreas.Sandberg@ARM.com
9329651SAndreas.Sandberg@ARM.comTick
9339651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitUnknown()
9349651SAndreas.Sandberg@ARM.com{
9359689Sandreas@sandberg.pp.se    dump();
9369651SAndreas.Sandberg@ARM.com    panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
9379651SAndreas.Sandberg@ARM.com          _kvmRun->hw.hardware_exit_reason);
9389651SAndreas.Sandberg@ARM.com}
9399651SAndreas.Sandberg@ARM.com
9409651SAndreas.Sandberg@ARM.comTick
9419651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitException()
9429651SAndreas.Sandberg@ARM.com{
9439689Sandreas@sandberg.pp.se    dump();
9449651SAndreas.Sandberg@ARM.com    panic("KVM: Got exception when starting vCPU "
9459651SAndreas.Sandberg@ARM.com          "(exception: %u, error_code: %u)\n",
9469651SAndreas.Sandberg@ARM.com          _kvmRun->ex.exception, _kvmRun->ex.error_code);
9479651SAndreas.Sandberg@ARM.com}
9489651SAndreas.Sandberg@ARM.com
9499651SAndreas.Sandberg@ARM.comTick
9509651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitFailEntry()
9519651SAndreas.Sandberg@ARM.com{
9529689Sandreas@sandberg.pp.se    dump();
9539651SAndreas.Sandberg@ARM.com    panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
9549651SAndreas.Sandberg@ARM.com          _kvmRun->fail_entry.hardware_entry_failure_reason);
9559651SAndreas.Sandberg@ARM.com}
9569651SAndreas.Sandberg@ARM.com
9579651SAndreas.Sandberg@ARM.comTick
9589651SAndreas.Sandberg@ARM.comBaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write)
9599651SAndreas.Sandberg@ARM.com{
9609760Sandreas@sandberg.pp.se    ThreadContext *tc(thread->getTC());
9619760Sandreas@sandberg.pp.se    syncThreadContext();
9629760Sandreas@sandberg.pp.se
9639682Sandreas@sandberg.pp.se    mmio_req.setPhys(paddr, size, Request::UNCACHEABLE, dataMasterId());
9649760Sandreas@sandberg.pp.se    // Some architectures do need to massage physical addresses a bit
9659760Sandreas@sandberg.pp.se    // before they are inserted into the memory system. This enables
9669760Sandreas@sandberg.pp.se    // APIC accesses on x86 and m5ops where supported through a MMIO
9679760Sandreas@sandberg.pp.se    // interface.
9689760Sandreas@sandberg.pp.se    BaseTLB::Mode tlb_mode(write ? BaseTLB::Write : BaseTLB::Read);
9699760Sandreas@sandberg.pp.se    Fault fault(tc->getDTBPtr()->finalizePhysical(&mmio_req, tc, tlb_mode));
9709760Sandreas@sandberg.pp.se    if (fault != NoFault)
9719760Sandreas@sandberg.pp.se        warn("Finalization of MMIO address failed: %s\n", fault->name());
9729760Sandreas@sandberg.pp.se
9739651SAndreas.Sandberg@ARM.com
9749651SAndreas.Sandberg@ARM.com    const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq);
9759651SAndreas.Sandberg@ARM.com    Packet pkt(&mmio_req, cmd);
9769651SAndreas.Sandberg@ARM.com    pkt.dataStatic(data);
9779760Sandreas@sandberg.pp.se
9789760Sandreas@sandberg.pp.se    if (mmio_req.isMmappedIpr()) {
9799882Sandreas@sandberg.pp.se        const Cycles ipr_delay(write ?
9809882Sandreas@sandberg.pp.se                             TheISA::handleIprWrite(tc, &pkt) :
9819882Sandreas@sandberg.pp.se                             TheISA::handleIprRead(tc, &pkt));
9829925Sandreas@sandberg.pp.se        return clockPeriod() * ipr_delay;
9839760Sandreas@sandberg.pp.se    } else {
9849760Sandreas@sandberg.pp.se        return dataPort.sendAtomic(&pkt);
9859760Sandreas@sandberg.pp.se    }
9869651SAndreas.Sandberg@ARM.com}
9879651SAndreas.Sandberg@ARM.com
9889753Sandreas@sandberg.pp.sevoid
9899753Sandreas@sandberg.pp.seBaseKvmCPU::setSignalMask(const sigset_t *mask)
9909753Sandreas@sandberg.pp.se{
9919753Sandreas@sandberg.pp.se    std::unique_ptr<struct kvm_signal_mask> kvm_mask;
9929753Sandreas@sandberg.pp.se
9939753Sandreas@sandberg.pp.se    if (mask) {
9949753Sandreas@sandberg.pp.se        kvm_mask.reset((struct kvm_signal_mask *)operator new(
9959753Sandreas@sandberg.pp.se                           sizeof(struct kvm_signal_mask) + sizeof(*mask)));
9969753Sandreas@sandberg.pp.se        // The kernel and the user-space headers have different ideas
9979753Sandreas@sandberg.pp.se        // about the size of sigset_t. This seems like a massive hack,
9989753Sandreas@sandberg.pp.se        // but is actually what qemu does.
9999753Sandreas@sandberg.pp.se        assert(sizeof(*mask) >= 8);
10009753Sandreas@sandberg.pp.se        kvm_mask->len = 8;
10019753Sandreas@sandberg.pp.se        memcpy(kvm_mask->sigset, mask, kvm_mask->len);
10029753Sandreas@sandberg.pp.se    }
10039753Sandreas@sandberg.pp.se
10049753Sandreas@sandberg.pp.se    if (ioctl(KVM_SET_SIGNAL_MASK, (void *)kvm_mask.get()) == -1)
10059753Sandreas@sandberg.pp.se        panic("KVM: Failed to set vCPU signal mask (errno: %i)\n",
10069753Sandreas@sandberg.pp.se              errno);
10079753Sandreas@sandberg.pp.se}
10089753Sandreas@sandberg.pp.se
10099651SAndreas.Sandberg@ARM.comint
10109651SAndreas.Sandberg@ARM.comBaseKvmCPU::ioctl(int request, long p1) const
10119651SAndreas.Sandberg@ARM.com{
10129651SAndreas.Sandberg@ARM.com    if (vcpuFD == -1)
10139651SAndreas.Sandberg@ARM.com        panic("KVM: CPU ioctl called before initialization\n");
10149651SAndreas.Sandberg@ARM.com
10159651SAndreas.Sandberg@ARM.com    return ::ioctl(vcpuFD, request, p1);
10169651SAndreas.Sandberg@ARM.com}
10179651SAndreas.Sandberg@ARM.com
10189651SAndreas.Sandberg@ARM.comTick
10199651SAndreas.Sandberg@ARM.comBaseKvmCPU::flushCoalescedMMIO()
10209651SAndreas.Sandberg@ARM.com{
10219651SAndreas.Sandberg@ARM.com    if (!mmioRing)
10229651SAndreas.Sandberg@ARM.com        return 0;
10239651SAndreas.Sandberg@ARM.com
10249651SAndreas.Sandberg@ARM.com    DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n");
10259651SAndreas.Sandberg@ARM.com
10269651SAndreas.Sandberg@ARM.com    // TODO: We might need to do synchronization when we start to
10279651SAndreas.Sandberg@ARM.com    // support multiple CPUs
10289651SAndreas.Sandberg@ARM.com    Tick ticks(0);
10299651SAndreas.Sandberg@ARM.com    while (mmioRing->first != mmioRing->last) {
10309651SAndreas.Sandberg@ARM.com        struct kvm_coalesced_mmio &ent(
10319651SAndreas.Sandberg@ARM.com            mmioRing->coalesced_mmio[mmioRing->first]);
10329651SAndreas.Sandberg@ARM.com
10339651SAndreas.Sandberg@ARM.com        DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
10349651SAndreas.Sandberg@ARM.com                ent.phys_addr, ent.len);
10359651SAndreas.Sandberg@ARM.com
10369651SAndreas.Sandberg@ARM.com        ++numCoalescedMMIO;
10379651SAndreas.Sandberg@ARM.com        ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true);
10389651SAndreas.Sandberg@ARM.com
10399651SAndreas.Sandberg@ARM.com        mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX;
10409651SAndreas.Sandberg@ARM.com    }
10419651SAndreas.Sandberg@ARM.com
10429651SAndreas.Sandberg@ARM.com    return ticks;
10439651SAndreas.Sandberg@ARM.com}
10449651SAndreas.Sandberg@ARM.com
10459892Sandreas@sandberg.pp.se/**
10469892Sandreas@sandberg.pp.se * Cycle timer overflow when running in KVM. Forces the KVM syscall to
10479892Sandreas@sandberg.pp.se * exit with EINTR and allows us to run the event queue.
10489892Sandreas@sandberg.pp.se */
10499892Sandreas@sandberg.pp.sestatic void
10509892Sandreas@sandberg.pp.seonTimerOverflow(int signo, siginfo_t *si, void *data)
10519892Sandreas@sandberg.pp.se{
10529892Sandreas@sandberg.pp.se    timerOverflowed = true;
10539892Sandreas@sandberg.pp.se}
10549892Sandreas@sandberg.pp.se
10559892Sandreas@sandberg.pp.se/**
10569892Sandreas@sandberg.pp.se * Instruction counter overflow when running in KVM. Forces the KVM
10579892Sandreas@sandberg.pp.se * syscall to exit with EINTR and allows us to handle instruction
10589892Sandreas@sandberg.pp.se * count events.
10599892Sandreas@sandberg.pp.se */
10609892Sandreas@sandberg.pp.sestatic void
10619892Sandreas@sandberg.pp.seonInstEvent(int signo, siginfo_t *si, void *data)
10629892Sandreas@sandberg.pp.se{
10639892Sandreas@sandberg.pp.se}
10649892Sandreas@sandberg.pp.se
10659651SAndreas.Sandberg@ARM.comvoid
10669651SAndreas.Sandberg@ARM.comBaseKvmCPU::setupSignalHandler()
10679651SAndreas.Sandberg@ARM.com{
10689651SAndreas.Sandberg@ARM.com    struct sigaction sa;
10699651SAndreas.Sandberg@ARM.com
10709651SAndreas.Sandberg@ARM.com    memset(&sa, 0, sizeof(sa));
10719651SAndreas.Sandberg@ARM.com    sa.sa_sigaction = onTimerOverflow;
10729651SAndreas.Sandberg@ARM.com    sa.sa_flags = SA_SIGINFO | SA_RESTART;
10739651SAndreas.Sandberg@ARM.com    if (sigaction(KVM_TIMER_SIGNAL, &sa, NULL) == -1)
10749892Sandreas@sandberg.pp.se        panic("KVM: Failed to setup vCPU timer signal handler\n");
10759892Sandreas@sandberg.pp.se
10769892Sandreas@sandberg.pp.se    memset(&sa, 0, sizeof(sa));
10779892Sandreas@sandberg.pp.se    sa.sa_sigaction = onInstEvent;
10789892Sandreas@sandberg.pp.se    sa.sa_flags = SA_SIGINFO | SA_RESTART;
10799892Sandreas@sandberg.pp.se    if (sigaction(KVM_INST_SIGNAL, &sa, NULL) == -1)
10809892Sandreas@sandberg.pp.se        panic("KVM: Failed to setup vCPU instruction signal handler\n");
10819753Sandreas@sandberg.pp.se
10829753Sandreas@sandberg.pp.se    sigset_t sigset;
10839753Sandreas@sandberg.pp.se    if (sigprocmask(SIG_BLOCK, NULL, &sigset) == -1)
10849753Sandreas@sandberg.pp.se        panic("KVM: Failed get signal mask\n");
10859753Sandreas@sandberg.pp.se
10869753Sandreas@sandberg.pp.se    // Request KVM to setup the same signal mask as we're currently
10879753Sandreas@sandberg.pp.se    // running with. We'll sometimes need to mask the KVM_TIMER_SIGNAL
10889753Sandreas@sandberg.pp.se    // to cause immediate exits from KVM after servicing IO
10899753Sandreas@sandberg.pp.se    // requests. See kvmRun().
10909753Sandreas@sandberg.pp.se    setSignalMask(&sigset);
10919753Sandreas@sandberg.pp.se
10929892Sandreas@sandberg.pp.se    // Mask our control signals so they aren't delivered unless we're
10939753Sandreas@sandberg.pp.se    // actually executing inside KVM.
10949753Sandreas@sandberg.pp.se    sigaddset(&sigset, KVM_TIMER_SIGNAL);
10959892Sandreas@sandberg.pp.se    sigaddset(&sigset, KVM_INST_SIGNAL);
10969753Sandreas@sandberg.pp.se    if (sigprocmask(SIG_SETMASK, &sigset, NULL) == -1)
10979892Sandreas@sandberg.pp.se        panic("KVM: Failed mask the KVM control signals\n");
10989753Sandreas@sandberg.pp.se}
10999753Sandreas@sandberg.pp.se
11009753Sandreas@sandberg.pp.sebool
11019753Sandreas@sandberg.pp.seBaseKvmCPU::discardPendingSignal(int signum) const
11029753Sandreas@sandberg.pp.se{
11039753Sandreas@sandberg.pp.se    int discardedSignal;
11049753Sandreas@sandberg.pp.se
11059753Sandreas@sandberg.pp.se    // Setting the timeout to zero causes sigtimedwait to return
11069753Sandreas@sandberg.pp.se    // immediately.
11079753Sandreas@sandberg.pp.se    struct timespec timeout;
11089753Sandreas@sandberg.pp.se    timeout.tv_sec = 0;
11099753Sandreas@sandberg.pp.se    timeout.tv_nsec = 0;
11109753Sandreas@sandberg.pp.se
11119753Sandreas@sandberg.pp.se    sigset_t sigset;
11129753Sandreas@sandberg.pp.se    sigemptyset(&sigset);
11139753Sandreas@sandberg.pp.se    sigaddset(&sigset, signum);
11149753Sandreas@sandberg.pp.se
11159753Sandreas@sandberg.pp.se    do {
11169753Sandreas@sandberg.pp.se        discardedSignal = sigtimedwait(&sigset, NULL, &timeout);
11179753Sandreas@sandberg.pp.se    } while (discardedSignal == -1 && errno == EINTR);
11189753Sandreas@sandberg.pp.se
11199753Sandreas@sandberg.pp.se    if (discardedSignal == signum)
11209753Sandreas@sandberg.pp.se        return true;
11219753Sandreas@sandberg.pp.se    else if (discardedSignal == -1 && errno == EAGAIN)
11229753Sandreas@sandberg.pp.se        return false;
11239753Sandreas@sandberg.pp.se    else
11249753Sandreas@sandberg.pp.se        panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n",
11259753Sandreas@sandberg.pp.se              discardedSignal, errno);
11269651SAndreas.Sandberg@ARM.com}
11279651SAndreas.Sandberg@ARM.com
11289651SAndreas.Sandberg@ARM.comvoid
11299651SAndreas.Sandberg@ARM.comBaseKvmCPU::setupCounters()
11309651SAndreas.Sandberg@ARM.com{
11319651SAndreas.Sandberg@ARM.com    DPRINTF(Kvm, "Attaching cycle counter...\n");
11329651SAndreas.Sandberg@ARM.com    PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE,
11339651SAndreas.Sandberg@ARM.com                                PERF_COUNT_HW_CPU_CYCLES);
11349651SAndreas.Sandberg@ARM.com    cfgCycles.disabled(true)
11359651SAndreas.Sandberg@ARM.com        .pinned(true);
11369655SAndreas.Sandberg@ARM.com
11379655SAndreas.Sandberg@ARM.com    if (perfControlledByTimer) {
11389655SAndreas.Sandberg@ARM.com        // We need to configure the cycles counter to send overflows
11399655SAndreas.Sandberg@ARM.com        // since we are going to use it to trigger timer signals that
11409655SAndreas.Sandberg@ARM.com        // trap back into m5 from KVM. In practice, this means that we
11419655SAndreas.Sandberg@ARM.com        // need to set some non-zero sample period that gets
11429655SAndreas.Sandberg@ARM.com        // overridden when the timer is armed.
11439655SAndreas.Sandberg@ARM.com        cfgCycles.wakeupEvents(1)
11449655SAndreas.Sandberg@ARM.com            .samplePeriod(42);
11459655SAndreas.Sandberg@ARM.com    }
11469655SAndreas.Sandberg@ARM.com
11479651SAndreas.Sandberg@ARM.com    hwCycles.attach(cfgCycles,
11489651SAndreas.Sandberg@ARM.com                    0); // TID (0 => currentThread)
11499651SAndreas.Sandberg@ARM.com
11509892Sandreas@sandberg.pp.se    setupInstCounter();
11519651SAndreas.Sandberg@ARM.com}
11529753Sandreas@sandberg.pp.se
11539753Sandreas@sandberg.pp.sebool
11549753Sandreas@sandberg.pp.seBaseKvmCPU::tryDrain()
11559753Sandreas@sandberg.pp.se{
11569753Sandreas@sandberg.pp.se    if (!drainManager)
11579753Sandreas@sandberg.pp.se        return false;
11589753Sandreas@sandberg.pp.se
11599753Sandreas@sandberg.pp.se    if (!archIsDrained()) {
11609753Sandreas@sandberg.pp.se        DPRINTF(Drain, "tryDrain: Architecture code is not ready.\n");
11619753Sandreas@sandberg.pp.se        return false;
11629753Sandreas@sandberg.pp.se    }
11639753Sandreas@sandberg.pp.se
11649753Sandreas@sandberg.pp.se    if (_status == Idle || _status == Running) {
11659753Sandreas@sandberg.pp.se        DPRINTF(Drain,
11669753Sandreas@sandberg.pp.se                "tryDrain: CPU transitioned into the Idle state, drain done\n");
11679753Sandreas@sandberg.pp.se        drainManager->signalDrainDone();
11689753Sandreas@sandberg.pp.se        drainManager = NULL;
11699753Sandreas@sandberg.pp.se        return true;
11709753Sandreas@sandberg.pp.se    } else {
11719753Sandreas@sandberg.pp.se        DPRINTF(Drain, "tryDrain: CPU not ready.\n");
11729753Sandreas@sandberg.pp.se        return false;
11739753Sandreas@sandberg.pp.se    }
11749753Sandreas@sandberg.pp.se}
11759753Sandreas@sandberg.pp.se
11769753Sandreas@sandberg.pp.sevoid
11779753Sandreas@sandberg.pp.seBaseKvmCPU::ioctlRun()
11789753Sandreas@sandberg.pp.se{
11799753Sandreas@sandberg.pp.se    if (ioctl(KVM_RUN) == -1) {
11809753Sandreas@sandberg.pp.se        if (errno != EINTR)
11819753Sandreas@sandberg.pp.se            panic("KVM: Failed to start virtual CPU (errno: %i)\n",
11829753Sandreas@sandberg.pp.se                  errno);
11839753Sandreas@sandberg.pp.se    }
11849753Sandreas@sandberg.pp.se}
11859892Sandreas@sandberg.pp.se
11869892Sandreas@sandberg.pp.sevoid
11879892Sandreas@sandberg.pp.seBaseKvmCPU::setupInstStop()
11889892Sandreas@sandberg.pp.se{
11899892Sandreas@sandberg.pp.se    if (comInstEventQueue[0]->empty()) {
11909892Sandreas@sandberg.pp.se        setupInstCounter(0);
11919892Sandreas@sandberg.pp.se    } else {
11929892Sandreas@sandberg.pp.se        const uint64_t next(comInstEventQueue[0]->nextTick());
11939892Sandreas@sandberg.pp.se
11949892Sandreas@sandberg.pp.se        assert(next > ctrInsts);
11959892Sandreas@sandberg.pp.se        setupInstCounter(next - ctrInsts);
11969892Sandreas@sandberg.pp.se    }
11979892Sandreas@sandberg.pp.se}
11989892Sandreas@sandberg.pp.se
11999892Sandreas@sandberg.pp.sevoid
12009892Sandreas@sandberg.pp.seBaseKvmCPU::setupInstCounter(uint64_t period)
12019892Sandreas@sandberg.pp.se{
12029892Sandreas@sandberg.pp.se    // No need to do anything if we aren't attaching for the first
12039892Sandreas@sandberg.pp.se    // time or the period isn't changing.
12049892Sandreas@sandberg.pp.se    if (period == activeInstPeriod && hwInstructions.attached())
12059892Sandreas@sandberg.pp.se        return;
12069892Sandreas@sandberg.pp.se
12079892Sandreas@sandberg.pp.se    PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE,
12089892Sandreas@sandberg.pp.se                                         PERF_COUNT_HW_INSTRUCTIONS);
12099892Sandreas@sandberg.pp.se
12109892Sandreas@sandberg.pp.se    if (period) {
12119892Sandreas@sandberg.pp.se        // Setup a sampling counter if that has been requested.
12129892Sandreas@sandberg.pp.se        cfgInstructions.wakeupEvents(1)
12139892Sandreas@sandberg.pp.se            .samplePeriod(period);
12149892Sandreas@sandberg.pp.se    }
12159892Sandreas@sandberg.pp.se
12169892Sandreas@sandberg.pp.se    // We need to detach and re-attach the counter to reliably change
12179892Sandreas@sandberg.pp.se    // sampling settings. See PerfKvmCounter::period() for details.
12189892Sandreas@sandberg.pp.se    if (hwInstructions.attached())
12199892Sandreas@sandberg.pp.se        hwInstructions.detach();
12209892Sandreas@sandberg.pp.se    assert(hwCycles.attached());
12219892Sandreas@sandberg.pp.se    hwInstructions.attach(cfgInstructions,
12229892Sandreas@sandberg.pp.se                          0, // TID (0 => currentThread)
12239892Sandreas@sandberg.pp.se                          hwCycles);
12249892Sandreas@sandberg.pp.se
12259892Sandreas@sandberg.pp.se    if (period)
12269892Sandreas@sandberg.pp.se        hwInstructions.enableSignals(KVM_INST_SIGNAL);
12279892Sandreas@sandberg.pp.se
12289892Sandreas@sandberg.pp.se    activeInstPeriod = period;
12299892Sandreas@sandberg.pp.se}
1230