base.cc revision 9752
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 499651SAndreas.Sandberg@ARM.com#include "arch/utility.hh" 509651SAndreas.Sandberg@ARM.com#include "cpu/kvm/base.hh" 519683Sandreas@sandberg.pp.se#include "debug/Checkpoint.hh" 529651SAndreas.Sandberg@ARM.com#include "debug/Kvm.hh" 539651SAndreas.Sandberg@ARM.com#include "debug/KvmIO.hh" 549651SAndreas.Sandberg@ARM.com#include "debug/KvmRun.hh" 559651SAndreas.Sandberg@ARM.com#include "params/BaseKvmCPU.hh" 569651SAndreas.Sandberg@ARM.com#include "sim/process.hh" 579651SAndreas.Sandberg@ARM.com#include "sim/system.hh" 589651SAndreas.Sandberg@ARM.com 599651SAndreas.Sandberg@ARM.com/* Used by some KVM macros */ 609651SAndreas.Sandberg@ARM.com#define PAGE_SIZE pageSize 619651SAndreas.Sandberg@ARM.com 629651SAndreas.Sandberg@ARM.comvolatile bool timerOverflowed = false; 639651SAndreas.Sandberg@ARM.com 649651SAndreas.Sandberg@ARM.comstatic void 659651SAndreas.Sandberg@ARM.comonTimerOverflow(int signo, siginfo_t *si, void *data) 669651SAndreas.Sandberg@ARM.com{ 679651SAndreas.Sandberg@ARM.com timerOverflowed = true; 689651SAndreas.Sandberg@ARM.com} 699651SAndreas.Sandberg@ARM.com 709651SAndreas.Sandberg@ARM.comBaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params) 719651SAndreas.Sandberg@ARM.com : BaseCPU(params), 729651SAndreas.Sandberg@ARM.com vm(*params->kvmVM), 739651SAndreas.Sandberg@ARM.com _status(Idle), 749651SAndreas.Sandberg@ARM.com dataPort(name() + ".dcache_port", this), 759651SAndreas.Sandberg@ARM.com instPort(name() + ".icache_port", this), 769652SAndreas.Sandberg@ARM.com threadContextDirty(true), 779652SAndreas.Sandberg@ARM.com kvmStateDirty(false), 789651SAndreas.Sandberg@ARM.com vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0), 799651SAndreas.Sandberg@ARM.com _kvmRun(NULL), mmioRing(NULL), 809651SAndreas.Sandberg@ARM.com pageSize(sysconf(_SC_PAGE_SIZE)), 819651SAndreas.Sandberg@ARM.com tickEvent(*this), 829655SAndreas.Sandberg@ARM.com perfControlledByTimer(params->usePerfOverflow), 839752Sandreas@sandberg.pp.se hostFactor(params->hostFactor), 849752Sandreas@sandberg.pp.se ctrInsts(0) 859651SAndreas.Sandberg@ARM.com{ 869651SAndreas.Sandberg@ARM.com if (pageSize == -1) 879651SAndreas.Sandberg@ARM.com panic("KVM: Failed to determine host page size (%i)\n", 889651SAndreas.Sandberg@ARM.com errno); 899651SAndreas.Sandberg@ARM.com 909651SAndreas.Sandberg@ARM.com thread = new SimpleThread(this, 0, params->system, 919651SAndreas.Sandberg@ARM.com params->itb, params->dtb, params->isa[0]); 929651SAndreas.Sandberg@ARM.com thread->setStatus(ThreadContext::Halted); 939651SAndreas.Sandberg@ARM.com tc = thread->getTC(); 949651SAndreas.Sandberg@ARM.com threadContexts.push_back(tc); 959651SAndreas.Sandberg@ARM.com 969651SAndreas.Sandberg@ARM.com setupCounters(); 979651SAndreas.Sandberg@ARM.com setupSignalHandler(); 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, 1039655SAndreas.Sandberg@ARM.com params->clock)); 1049655SAndreas.Sandberg@ARM.com else 1059655SAndreas.Sandberg@ARM.com runTimer.reset(new PosixKvmTimer(KVM_TIMER_SIGNAL, CLOCK_MONOTONIC, 1069655SAndreas.Sandberg@ARM.com params->hostFactor, 1079655SAndreas.Sandberg@ARM.com params->clock)); 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 1549651SAndreas.Sandberg@ARM.com // Map the KVM run structure */ 1559651SAndreas.Sandberg@ARM.com vcpuMMapSize = kvm.getVCPUMMapSize(); 1569651SAndreas.Sandberg@ARM.com _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize, 1579651SAndreas.Sandberg@ARM.com PROT_READ | PROT_WRITE, MAP_SHARED, 1589651SAndreas.Sandberg@ARM.com vcpuFD, 0); 1599651SAndreas.Sandberg@ARM.com if (_kvmRun == MAP_FAILED) 1609651SAndreas.Sandberg@ARM.com panic("KVM: Failed to map run data structure\n"); 1619651SAndreas.Sandberg@ARM.com 1629651SAndreas.Sandberg@ARM.com // Setup a pointer to the MMIO ring buffer if coalesced MMIO is 1639651SAndreas.Sandberg@ARM.com // available. The offset into the KVM's communication page is 1649651SAndreas.Sandberg@ARM.com // provided by the coalesced MMIO capability. 1659651SAndreas.Sandberg@ARM.com int mmioOffset(kvm.capCoalescedMMIO()); 1669690Sandreas@sandberg.pp.se if (!p->useCoalescedMMIO) { 1679690Sandreas@sandberg.pp.se inform("KVM: Coalesced MMIO disabled by config.\n"); 1689690Sandreas@sandberg.pp.se } else if (mmioOffset) { 1699651SAndreas.Sandberg@ARM.com inform("KVM: Coalesced IO available\n"); 1709651SAndreas.Sandberg@ARM.com mmioRing = (struct kvm_coalesced_mmio_ring *)( 1719651SAndreas.Sandberg@ARM.com (char *)_kvmRun + (mmioOffset * pageSize)); 1729651SAndreas.Sandberg@ARM.com } else { 1739651SAndreas.Sandberg@ARM.com inform("KVM: Coalesced not supported by host OS\n"); 1749651SAndreas.Sandberg@ARM.com } 1759732Sandreas@sandberg.pp.se 1769732Sandreas@sandberg.pp.se thread->startup(); 1779651SAndreas.Sandberg@ARM.com} 1789651SAndreas.Sandberg@ARM.com 1799651SAndreas.Sandberg@ARM.comvoid 1809651SAndreas.Sandberg@ARM.comBaseKvmCPU::regStats() 1819651SAndreas.Sandberg@ARM.com{ 1829651SAndreas.Sandberg@ARM.com using namespace Stats; 1839651SAndreas.Sandberg@ARM.com 1849651SAndreas.Sandberg@ARM.com BaseCPU::regStats(); 1859651SAndreas.Sandberg@ARM.com 1869684Sandreas@sandberg.pp.se numInsts 1879684Sandreas@sandberg.pp.se .name(name() + ".committedInsts") 1889684Sandreas@sandberg.pp.se .desc("Number of instructions committed") 1899684Sandreas@sandberg.pp.se ; 1909684Sandreas@sandberg.pp.se 1919651SAndreas.Sandberg@ARM.com numVMExits 1929651SAndreas.Sandberg@ARM.com .name(name() + ".numVMExits") 1939651SAndreas.Sandberg@ARM.com .desc("total number of KVM exits") 1949651SAndreas.Sandberg@ARM.com ; 1959651SAndreas.Sandberg@ARM.com 1969651SAndreas.Sandberg@ARM.com numMMIO 1979651SAndreas.Sandberg@ARM.com .name(name() + ".numMMIO") 1989651SAndreas.Sandberg@ARM.com .desc("number of VM exits due to memory mapped IO") 1999651SAndreas.Sandberg@ARM.com ; 2009651SAndreas.Sandberg@ARM.com 2019651SAndreas.Sandberg@ARM.com numCoalescedMMIO 2029651SAndreas.Sandberg@ARM.com .name(name() + ".numCoalescedMMIO") 2039651SAndreas.Sandberg@ARM.com .desc("number of coalesced memory mapped IO requests") 2049651SAndreas.Sandberg@ARM.com ; 2059651SAndreas.Sandberg@ARM.com 2069651SAndreas.Sandberg@ARM.com numIO 2079651SAndreas.Sandberg@ARM.com .name(name() + ".numIO") 2089651SAndreas.Sandberg@ARM.com .desc("number of VM exits due to legacy IO") 2099651SAndreas.Sandberg@ARM.com ; 2109651SAndreas.Sandberg@ARM.com 2119651SAndreas.Sandberg@ARM.com numHalt 2129651SAndreas.Sandberg@ARM.com .name(name() + ".numHalt") 2139651SAndreas.Sandberg@ARM.com .desc("number of VM exits due to wait for interrupt instructions") 2149651SAndreas.Sandberg@ARM.com ; 2159651SAndreas.Sandberg@ARM.com 2169651SAndreas.Sandberg@ARM.com numInterrupts 2179651SAndreas.Sandberg@ARM.com .name(name() + ".numInterrupts") 2189651SAndreas.Sandberg@ARM.com .desc("number of interrupts delivered") 2199651SAndreas.Sandberg@ARM.com ; 2209651SAndreas.Sandberg@ARM.com 2219651SAndreas.Sandberg@ARM.com numHypercalls 2229651SAndreas.Sandberg@ARM.com .name(name() + ".numHypercalls") 2239651SAndreas.Sandberg@ARM.com .desc("number of hypercalls") 2249651SAndreas.Sandberg@ARM.com ; 2259651SAndreas.Sandberg@ARM.com} 2269651SAndreas.Sandberg@ARM.com 2279651SAndreas.Sandberg@ARM.comvoid 2289651SAndreas.Sandberg@ARM.comBaseKvmCPU::serializeThread(std::ostream &os, ThreadID tid) 2299651SAndreas.Sandberg@ARM.com{ 2309683Sandreas@sandberg.pp.se if (DTRACE(Checkpoint)) { 2319683Sandreas@sandberg.pp.se DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid); 2329683Sandreas@sandberg.pp.se dump(); 2339683Sandreas@sandberg.pp.se } 2349683Sandreas@sandberg.pp.se 2359652SAndreas.Sandberg@ARM.com // Update the thread context so we have something to serialize. 2369652SAndreas.Sandberg@ARM.com syncThreadContext(); 2379652SAndreas.Sandberg@ARM.com 2389651SAndreas.Sandberg@ARM.com assert(tid == 0); 2399651SAndreas.Sandberg@ARM.com assert(_status == Idle); 2409651SAndreas.Sandberg@ARM.com thread->serialize(os); 2419651SAndreas.Sandberg@ARM.com} 2429651SAndreas.Sandberg@ARM.com 2439651SAndreas.Sandberg@ARM.comvoid 2449651SAndreas.Sandberg@ARM.comBaseKvmCPU::unserializeThread(Checkpoint *cp, const std::string §ion, 2459651SAndreas.Sandberg@ARM.com ThreadID tid) 2469651SAndreas.Sandberg@ARM.com{ 2479683Sandreas@sandberg.pp.se DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid); 2489683Sandreas@sandberg.pp.se 2499651SAndreas.Sandberg@ARM.com assert(tid == 0); 2509651SAndreas.Sandberg@ARM.com assert(_status == Idle); 2519651SAndreas.Sandberg@ARM.com thread->unserialize(cp, section); 2529652SAndreas.Sandberg@ARM.com threadContextDirty = true; 2539651SAndreas.Sandberg@ARM.com} 2549651SAndreas.Sandberg@ARM.com 2559651SAndreas.Sandberg@ARM.comunsigned int 2569651SAndreas.Sandberg@ARM.comBaseKvmCPU::drain(DrainManager *dm) 2579651SAndreas.Sandberg@ARM.com{ 2589651SAndreas.Sandberg@ARM.com if (switchedOut()) 2599651SAndreas.Sandberg@ARM.com return 0; 2609651SAndreas.Sandberg@ARM.com 2619651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "drain\n"); 2629651SAndreas.Sandberg@ARM.com 2639651SAndreas.Sandberg@ARM.com // De-schedule the tick event so we don't insert any more MMIOs 2649651SAndreas.Sandberg@ARM.com // into the system while it is draining. 2659651SAndreas.Sandberg@ARM.com if (tickEvent.scheduled()) 2669651SAndreas.Sandberg@ARM.com deschedule(tickEvent); 2679651SAndreas.Sandberg@ARM.com 2689651SAndreas.Sandberg@ARM.com _status = Idle; 2699651SAndreas.Sandberg@ARM.com return 0; 2709651SAndreas.Sandberg@ARM.com} 2719651SAndreas.Sandberg@ARM.com 2729651SAndreas.Sandberg@ARM.comvoid 2739651SAndreas.Sandberg@ARM.comBaseKvmCPU::drainResume() 2749651SAndreas.Sandberg@ARM.com{ 2759651SAndreas.Sandberg@ARM.com assert(!tickEvent.scheduled()); 2769651SAndreas.Sandberg@ARM.com 2779651SAndreas.Sandberg@ARM.com // We might have been switched out. In that case, we don't need to 2789651SAndreas.Sandberg@ARM.com // do anything. 2799651SAndreas.Sandberg@ARM.com if (switchedOut()) 2809651SAndreas.Sandberg@ARM.com return; 2819651SAndreas.Sandberg@ARM.com 2829651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "drainResume\n"); 2839651SAndreas.Sandberg@ARM.com verifyMemoryMode(); 2849651SAndreas.Sandberg@ARM.com 2859651SAndreas.Sandberg@ARM.com // The tick event is de-scheduled as a part of the draining 2869651SAndreas.Sandberg@ARM.com // process. Re-schedule it if the thread context is active. 2879651SAndreas.Sandberg@ARM.com if (tc->status() == ThreadContext::Active) { 2889651SAndreas.Sandberg@ARM.com schedule(tickEvent, nextCycle()); 2899651SAndreas.Sandberg@ARM.com _status = Running; 2909651SAndreas.Sandberg@ARM.com } else { 2919651SAndreas.Sandberg@ARM.com _status = Idle; 2929651SAndreas.Sandberg@ARM.com } 2939651SAndreas.Sandberg@ARM.com} 2949651SAndreas.Sandberg@ARM.com 2959651SAndreas.Sandberg@ARM.comvoid 2969651SAndreas.Sandberg@ARM.comBaseKvmCPU::switchOut() 2979651SAndreas.Sandberg@ARM.com{ 2989652SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "switchOut\n"); 2999652SAndreas.Sandberg@ARM.com 3009652SAndreas.Sandberg@ARM.com // Make sure to update the thread context in case, the new CPU 3019652SAndreas.Sandberg@ARM.com // will need to access it. 3029652SAndreas.Sandberg@ARM.com syncThreadContext(); 3039652SAndreas.Sandberg@ARM.com 3049651SAndreas.Sandberg@ARM.com BaseCPU::switchOut(); 3059651SAndreas.Sandberg@ARM.com 3069651SAndreas.Sandberg@ARM.com // We should have drained prior to executing a switchOut, which 3079651SAndreas.Sandberg@ARM.com // means that the tick event shouldn't be scheduled and the CPU is 3089651SAndreas.Sandberg@ARM.com // idle. 3099651SAndreas.Sandberg@ARM.com assert(!tickEvent.scheduled()); 3109651SAndreas.Sandberg@ARM.com assert(_status == Idle); 3119651SAndreas.Sandberg@ARM.com} 3129651SAndreas.Sandberg@ARM.com 3139651SAndreas.Sandberg@ARM.comvoid 3149651SAndreas.Sandberg@ARM.comBaseKvmCPU::takeOverFrom(BaseCPU *cpu) 3159651SAndreas.Sandberg@ARM.com{ 3169651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "takeOverFrom\n"); 3179651SAndreas.Sandberg@ARM.com 3189651SAndreas.Sandberg@ARM.com BaseCPU::takeOverFrom(cpu); 3199651SAndreas.Sandberg@ARM.com 3209651SAndreas.Sandberg@ARM.com // We should have drained prior to executing a switchOut, which 3219651SAndreas.Sandberg@ARM.com // means that the tick event shouldn't be scheduled and the CPU is 3229651SAndreas.Sandberg@ARM.com // idle. 3239651SAndreas.Sandberg@ARM.com assert(!tickEvent.scheduled()); 3249651SAndreas.Sandberg@ARM.com assert(_status == Idle); 3259651SAndreas.Sandberg@ARM.com assert(threadContexts.size() == 1); 3269651SAndreas.Sandberg@ARM.com 3279652SAndreas.Sandberg@ARM.com // The BaseCPU updated the thread context, make sure that we 3289652SAndreas.Sandberg@ARM.com // synchronize next time we enter start the CPU. 3299652SAndreas.Sandberg@ARM.com threadContextDirty = true; 3309651SAndreas.Sandberg@ARM.com} 3319651SAndreas.Sandberg@ARM.com 3329651SAndreas.Sandberg@ARM.comvoid 3339651SAndreas.Sandberg@ARM.comBaseKvmCPU::verifyMemoryMode() const 3349651SAndreas.Sandberg@ARM.com{ 3359651SAndreas.Sandberg@ARM.com if (!(system->isAtomicMode() && system->bypassCaches())) { 3369651SAndreas.Sandberg@ARM.com fatal("The KVM-based CPUs requires the memory system to be in the " 3379651SAndreas.Sandberg@ARM.com "'atomic_noncaching' mode.\n"); 3389651SAndreas.Sandberg@ARM.com } 3399651SAndreas.Sandberg@ARM.com} 3409651SAndreas.Sandberg@ARM.com 3419651SAndreas.Sandberg@ARM.comvoid 3429651SAndreas.Sandberg@ARM.comBaseKvmCPU::wakeup() 3439651SAndreas.Sandberg@ARM.com{ 3449651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "wakeup()\n"); 3459651SAndreas.Sandberg@ARM.com 3469651SAndreas.Sandberg@ARM.com if (thread->status() != ThreadContext::Suspended) 3479651SAndreas.Sandberg@ARM.com return; 3489651SAndreas.Sandberg@ARM.com 3499651SAndreas.Sandberg@ARM.com thread->activate(); 3509651SAndreas.Sandberg@ARM.com} 3519651SAndreas.Sandberg@ARM.com 3529651SAndreas.Sandberg@ARM.comvoid 3539651SAndreas.Sandberg@ARM.comBaseKvmCPU::activateContext(ThreadID thread_num, Cycles delay) 3549651SAndreas.Sandberg@ARM.com{ 3559651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "ActivateContext %d (%d cycles)\n", thread_num, delay); 3569651SAndreas.Sandberg@ARM.com 3579651SAndreas.Sandberg@ARM.com assert(thread_num == 0); 3589651SAndreas.Sandberg@ARM.com assert(thread); 3599651SAndreas.Sandberg@ARM.com 3609651SAndreas.Sandberg@ARM.com assert(_status == Idle); 3619651SAndreas.Sandberg@ARM.com assert(!tickEvent.scheduled()); 3629651SAndreas.Sandberg@ARM.com 3639651SAndreas.Sandberg@ARM.com numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend) 3649651SAndreas.Sandberg@ARM.com * hostFactor; 3659651SAndreas.Sandberg@ARM.com 3669651SAndreas.Sandberg@ARM.com schedule(tickEvent, clockEdge(delay)); 3679651SAndreas.Sandberg@ARM.com _status = Running; 3689651SAndreas.Sandberg@ARM.com} 3699651SAndreas.Sandberg@ARM.com 3709651SAndreas.Sandberg@ARM.com 3719651SAndreas.Sandberg@ARM.comvoid 3729651SAndreas.Sandberg@ARM.comBaseKvmCPU::suspendContext(ThreadID thread_num) 3739651SAndreas.Sandberg@ARM.com{ 3749651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "SuspendContext %d\n", thread_num); 3759651SAndreas.Sandberg@ARM.com 3769651SAndreas.Sandberg@ARM.com assert(thread_num == 0); 3779651SAndreas.Sandberg@ARM.com assert(thread); 3789651SAndreas.Sandberg@ARM.com 3799651SAndreas.Sandberg@ARM.com if (_status == Idle) 3809651SAndreas.Sandberg@ARM.com return; 3819651SAndreas.Sandberg@ARM.com 3829651SAndreas.Sandberg@ARM.com assert(_status == Running); 3839651SAndreas.Sandberg@ARM.com 3849651SAndreas.Sandberg@ARM.com // The tick event may no be scheduled if the quest has requested 3859651SAndreas.Sandberg@ARM.com // the monitor to wait for interrupts. The normal CPU models can 3869651SAndreas.Sandberg@ARM.com // get their tick events descheduled by quiesce instructions, but 3879651SAndreas.Sandberg@ARM.com // that can't happen here. 3889651SAndreas.Sandberg@ARM.com if (tickEvent.scheduled()) 3899651SAndreas.Sandberg@ARM.com deschedule(tickEvent); 3909651SAndreas.Sandberg@ARM.com 3919651SAndreas.Sandberg@ARM.com _status = Idle; 3929651SAndreas.Sandberg@ARM.com} 3939651SAndreas.Sandberg@ARM.com 3949651SAndreas.Sandberg@ARM.comvoid 3959651SAndreas.Sandberg@ARM.comBaseKvmCPU::deallocateContext(ThreadID thread_num) 3969651SAndreas.Sandberg@ARM.com{ 3979651SAndreas.Sandberg@ARM.com // for now, these are equivalent 3989651SAndreas.Sandberg@ARM.com suspendContext(thread_num); 3999651SAndreas.Sandberg@ARM.com} 4009651SAndreas.Sandberg@ARM.com 4019651SAndreas.Sandberg@ARM.comvoid 4029651SAndreas.Sandberg@ARM.comBaseKvmCPU::haltContext(ThreadID thread_num) 4039651SAndreas.Sandberg@ARM.com{ 4049651SAndreas.Sandberg@ARM.com // for now, these are equivalent 4059651SAndreas.Sandberg@ARM.com suspendContext(thread_num); 4069651SAndreas.Sandberg@ARM.com} 4079651SAndreas.Sandberg@ARM.com 4089652SAndreas.Sandberg@ARM.comThreadContext * 4099652SAndreas.Sandberg@ARM.comBaseKvmCPU::getContext(int tn) 4109652SAndreas.Sandberg@ARM.com{ 4119652SAndreas.Sandberg@ARM.com assert(tn == 0); 4129652SAndreas.Sandberg@ARM.com syncThreadContext(); 4139652SAndreas.Sandberg@ARM.com return tc; 4149652SAndreas.Sandberg@ARM.com} 4159652SAndreas.Sandberg@ARM.com 4169652SAndreas.Sandberg@ARM.com 4179651SAndreas.Sandberg@ARM.comCounter 4189651SAndreas.Sandberg@ARM.comBaseKvmCPU::totalInsts() const 4199651SAndreas.Sandberg@ARM.com{ 4209752Sandreas@sandberg.pp.se return ctrInsts; 4219651SAndreas.Sandberg@ARM.com} 4229651SAndreas.Sandberg@ARM.com 4239651SAndreas.Sandberg@ARM.comCounter 4249651SAndreas.Sandberg@ARM.comBaseKvmCPU::totalOps() const 4259651SAndreas.Sandberg@ARM.com{ 4269651SAndreas.Sandberg@ARM.com hack_once("Pretending totalOps is equivalent to totalInsts()\n"); 4279752Sandreas@sandberg.pp.se return ctrInsts; 4289651SAndreas.Sandberg@ARM.com} 4299651SAndreas.Sandberg@ARM.com 4309651SAndreas.Sandberg@ARM.comvoid 4319651SAndreas.Sandberg@ARM.comBaseKvmCPU::dump() 4329651SAndreas.Sandberg@ARM.com{ 4339651SAndreas.Sandberg@ARM.com inform("State dumping not implemented."); 4349651SAndreas.Sandberg@ARM.com} 4359651SAndreas.Sandberg@ARM.com 4369651SAndreas.Sandberg@ARM.comvoid 4379651SAndreas.Sandberg@ARM.comBaseKvmCPU::tick() 4389651SAndreas.Sandberg@ARM.com{ 4399651SAndreas.Sandberg@ARM.com assert(_status == Running); 4409651SAndreas.Sandberg@ARM.com 4419651SAndreas.Sandberg@ARM.com DPRINTF(KvmRun, "Entering KVM...\n"); 4429651SAndreas.Sandberg@ARM.com 4439651SAndreas.Sandberg@ARM.com Tick ticksToExecute(mainEventQueue.nextTick() - curTick()); 4449651SAndreas.Sandberg@ARM.com Tick ticksExecuted(kvmRun(ticksToExecute)); 4459651SAndreas.Sandberg@ARM.com 4469651SAndreas.Sandberg@ARM.com Tick delay(ticksExecuted + handleKvmExit()); 4479651SAndreas.Sandberg@ARM.com 4489651SAndreas.Sandberg@ARM.com switch (_status) { 4499651SAndreas.Sandberg@ARM.com case Running: 4509651SAndreas.Sandberg@ARM.com schedule(tickEvent, clockEdge(ticksToCycles(delay))); 4519651SAndreas.Sandberg@ARM.com break; 4529651SAndreas.Sandberg@ARM.com 4539651SAndreas.Sandberg@ARM.com default: 4549651SAndreas.Sandberg@ARM.com /* The CPU is halted or waiting for an interrupt from a 4559651SAndreas.Sandberg@ARM.com * device. Don't start it. */ 4569651SAndreas.Sandberg@ARM.com break; 4579651SAndreas.Sandberg@ARM.com } 4589651SAndreas.Sandberg@ARM.com} 4599651SAndreas.Sandberg@ARM.com 4609735Sandreas@sandberg.pp.seuint64_t 4619735Sandreas@sandberg.pp.seBaseKvmCPU::getHostCycles() const 4629735Sandreas@sandberg.pp.se{ 4639735Sandreas@sandberg.pp.se return hwCycles.read(); 4649735Sandreas@sandberg.pp.se} 4659735Sandreas@sandberg.pp.se 4669651SAndreas.Sandberg@ARM.comTick 4679651SAndreas.Sandberg@ARM.comBaseKvmCPU::kvmRun(Tick ticks) 4689651SAndreas.Sandberg@ARM.com{ 4699652SAndreas.Sandberg@ARM.com // We might need to update the KVM state. 4709652SAndreas.Sandberg@ARM.com syncKvmState(); 4719652SAndreas.Sandberg@ARM.com // Entering into KVM implies that we'll have to reload the thread 4729652SAndreas.Sandberg@ARM.com // context from KVM if we want to access it. Flag the KVM state as 4739652SAndreas.Sandberg@ARM.com // dirty with respect to the cached thread context. 4749652SAndreas.Sandberg@ARM.com kvmStateDirty = true; 4759652SAndreas.Sandberg@ARM.com 4769651SAndreas.Sandberg@ARM.com if (ticks < runTimer->resolution()) { 4779651SAndreas.Sandberg@ARM.com DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n", 4789651SAndreas.Sandberg@ARM.com ticks, runTimer->resolution()); 4799651SAndreas.Sandberg@ARM.com ticks = runTimer->resolution(); 4809651SAndreas.Sandberg@ARM.com } 4819651SAndreas.Sandberg@ARM.com 4829651SAndreas.Sandberg@ARM.com DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks); 4839651SAndreas.Sandberg@ARM.com timerOverflowed = false; 4849655SAndreas.Sandberg@ARM.com 4859735Sandreas@sandberg.pp.se // Get hardware statistics after synchronizing contexts. The KVM 4869735Sandreas@sandberg.pp.se // state update might affect guest cycle counters. 4879735Sandreas@sandberg.pp.se uint64_t baseCycles(getHostCycles()); 4889735Sandreas@sandberg.pp.se uint64_t baseInstrs(hwInstructions.read()); 4899735Sandreas@sandberg.pp.se 4909655SAndreas.Sandberg@ARM.com // Arm the run timer and start the cycle timer if it isn't 4919655SAndreas.Sandberg@ARM.com // controlled by the overflow timer. Starting/stopping the cycle 4929655SAndreas.Sandberg@ARM.com // timer automatically starts the other perf timers as they are in 4939655SAndreas.Sandberg@ARM.com // the same counter group. 4949651SAndreas.Sandberg@ARM.com runTimer->arm(ticks); 4959655SAndreas.Sandberg@ARM.com if (!perfControlledByTimer) 4969655SAndreas.Sandberg@ARM.com hwCycles.start(); 4979655SAndreas.Sandberg@ARM.com 4989651SAndreas.Sandberg@ARM.com if (ioctl(KVM_RUN) == -1) { 4999651SAndreas.Sandberg@ARM.com if (errno != EINTR) 5009651SAndreas.Sandberg@ARM.com panic("KVM: Failed to start virtual CPU (errno: %i)\n", 5019651SAndreas.Sandberg@ARM.com errno); 5029651SAndreas.Sandberg@ARM.com } 5039655SAndreas.Sandberg@ARM.com 5049651SAndreas.Sandberg@ARM.com runTimer->disarm(); 5059655SAndreas.Sandberg@ARM.com if (!perfControlledByTimer) 5069655SAndreas.Sandberg@ARM.com hwCycles.stop(); 5079655SAndreas.Sandberg@ARM.com 5089651SAndreas.Sandberg@ARM.com 5099735Sandreas@sandberg.pp.se const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles); 5109684Sandreas@sandberg.pp.se const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor); 5119684Sandreas@sandberg.pp.se const uint64_t instsExecuted(hwInstructions.read() - baseInstrs); 5129684Sandreas@sandberg.pp.se const Tick ticksExecuted(runTimer->ticksFromHostCycles(hostCyclesExecuted)); 5139651SAndreas.Sandberg@ARM.com 5149651SAndreas.Sandberg@ARM.com if (ticksExecuted < ticks && 5159651SAndreas.Sandberg@ARM.com timerOverflowed && 5169651SAndreas.Sandberg@ARM.com _kvmRun->exit_reason == KVM_EXIT_INTR) { 5179651SAndreas.Sandberg@ARM.com // TODO: We should probably do something clever here... 5189651SAndreas.Sandberg@ARM.com warn("KVM: Early timer event, requested %i ticks but got %i ticks.\n", 5199651SAndreas.Sandberg@ARM.com ticks, ticksExecuted); 5209651SAndreas.Sandberg@ARM.com } 5219651SAndreas.Sandberg@ARM.com 5229684Sandreas@sandberg.pp.se /* Update statistics */ 5239684Sandreas@sandberg.pp.se numCycles += simCyclesExecuted;; 5249651SAndreas.Sandberg@ARM.com ++numVMExits; 5259684Sandreas@sandberg.pp.se numInsts += instsExecuted; 5269752Sandreas@sandberg.pp.se ctrInsts += instsExecuted; 5279752Sandreas@sandberg.pp.se system->totalNumInsts += instsExecuted; 5289651SAndreas.Sandberg@ARM.com 5299651SAndreas.Sandberg@ARM.com DPRINTF(KvmRun, "KVM: Executed %i instructions in %i cycles (%i ticks, sim cycles: %i).\n", 5309684Sandreas@sandberg.pp.se instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted); 5319651SAndreas.Sandberg@ARM.com 5329651SAndreas.Sandberg@ARM.com return ticksExecuted + flushCoalescedMMIO(); 5339651SAndreas.Sandberg@ARM.com} 5349651SAndreas.Sandberg@ARM.com 5359651SAndreas.Sandberg@ARM.comvoid 5369651SAndreas.Sandberg@ARM.comBaseKvmCPU::kvmNonMaskableInterrupt() 5379651SAndreas.Sandberg@ARM.com{ 5389651SAndreas.Sandberg@ARM.com ++numInterrupts; 5399651SAndreas.Sandberg@ARM.com if (ioctl(KVM_NMI) == -1) 5409651SAndreas.Sandberg@ARM.com panic("KVM: Failed to deliver NMI to virtual CPU\n"); 5419651SAndreas.Sandberg@ARM.com} 5429651SAndreas.Sandberg@ARM.com 5439651SAndreas.Sandberg@ARM.comvoid 5449651SAndreas.Sandberg@ARM.comBaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt) 5459651SAndreas.Sandberg@ARM.com{ 5469651SAndreas.Sandberg@ARM.com ++numInterrupts; 5479651SAndreas.Sandberg@ARM.com if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1) 5489651SAndreas.Sandberg@ARM.com panic("KVM: Failed to deliver interrupt to virtual CPU\n"); 5499651SAndreas.Sandberg@ARM.com} 5509651SAndreas.Sandberg@ARM.com 5519651SAndreas.Sandberg@ARM.comvoid 5529651SAndreas.Sandberg@ARM.comBaseKvmCPU::getRegisters(struct kvm_regs ®s) const 5539651SAndreas.Sandberg@ARM.com{ 5549651SAndreas.Sandberg@ARM.com if (ioctl(KVM_GET_REGS, ®s) == -1) 5559651SAndreas.Sandberg@ARM.com panic("KVM: Failed to get guest registers\n"); 5569651SAndreas.Sandberg@ARM.com} 5579651SAndreas.Sandberg@ARM.com 5589651SAndreas.Sandberg@ARM.comvoid 5599651SAndreas.Sandberg@ARM.comBaseKvmCPU::setRegisters(const struct kvm_regs ®s) 5609651SAndreas.Sandberg@ARM.com{ 5619651SAndreas.Sandberg@ARM.com if (ioctl(KVM_SET_REGS, (void *)®s) == -1) 5629651SAndreas.Sandberg@ARM.com panic("KVM: Failed to set guest registers\n"); 5639651SAndreas.Sandberg@ARM.com} 5649651SAndreas.Sandberg@ARM.com 5659651SAndreas.Sandberg@ARM.comvoid 5669651SAndreas.Sandberg@ARM.comBaseKvmCPU::getSpecialRegisters(struct kvm_sregs ®s) const 5679651SAndreas.Sandberg@ARM.com{ 5689651SAndreas.Sandberg@ARM.com if (ioctl(KVM_GET_SREGS, ®s) == -1) 5699651SAndreas.Sandberg@ARM.com panic("KVM: Failed to get guest special registers\n"); 5709651SAndreas.Sandberg@ARM.com} 5719651SAndreas.Sandberg@ARM.com 5729651SAndreas.Sandberg@ARM.comvoid 5739651SAndreas.Sandberg@ARM.comBaseKvmCPU::setSpecialRegisters(const struct kvm_sregs ®s) 5749651SAndreas.Sandberg@ARM.com{ 5759651SAndreas.Sandberg@ARM.com if (ioctl(KVM_SET_SREGS, (void *)®s) == -1) 5769651SAndreas.Sandberg@ARM.com panic("KVM: Failed to set guest special registers\n"); 5779651SAndreas.Sandberg@ARM.com} 5789651SAndreas.Sandberg@ARM.com 5799651SAndreas.Sandberg@ARM.comvoid 5809651SAndreas.Sandberg@ARM.comBaseKvmCPU::getFPUState(struct kvm_fpu &state) const 5819651SAndreas.Sandberg@ARM.com{ 5829651SAndreas.Sandberg@ARM.com if (ioctl(KVM_GET_FPU, &state) == -1) 5839651SAndreas.Sandberg@ARM.com panic("KVM: Failed to get guest FPU state\n"); 5849651SAndreas.Sandberg@ARM.com} 5859651SAndreas.Sandberg@ARM.com 5869651SAndreas.Sandberg@ARM.comvoid 5879651SAndreas.Sandberg@ARM.comBaseKvmCPU::setFPUState(const struct kvm_fpu &state) 5889651SAndreas.Sandberg@ARM.com{ 5899651SAndreas.Sandberg@ARM.com if (ioctl(KVM_SET_FPU, (void *)&state) == -1) 5909651SAndreas.Sandberg@ARM.com panic("KVM: Failed to set guest FPU state\n"); 5919651SAndreas.Sandberg@ARM.com} 5929651SAndreas.Sandberg@ARM.com 5939651SAndreas.Sandberg@ARM.com 5949651SAndreas.Sandberg@ARM.comvoid 5959651SAndreas.Sandberg@ARM.comBaseKvmCPU::setOneReg(uint64_t id, const void *addr) 5969651SAndreas.Sandberg@ARM.com{ 5979651SAndreas.Sandberg@ARM.com#ifdef KVM_SET_ONE_REG 5989651SAndreas.Sandberg@ARM.com struct kvm_one_reg reg; 5999651SAndreas.Sandberg@ARM.com reg.id = id; 6009651SAndreas.Sandberg@ARM.com reg.addr = (uint64_t)addr; 6019651SAndreas.Sandberg@ARM.com 6029651SAndreas.Sandberg@ARM.com if (ioctl(KVM_SET_ONE_REG, ®) == -1) { 6039651SAndreas.Sandberg@ARM.com panic("KVM: Failed to set register (0x%x) value (errno: %i)\n", 6049651SAndreas.Sandberg@ARM.com id, errno); 6059651SAndreas.Sandberg@ARM.com } 6069651SAndreas.Sandberg@ARM.com#else 6079651SAndreas.Sandberg@ARM.com panic("KVM_SET_ONE_REG is unsupported on this platform.\n"); 6089651SAndreas.Sandberg@ARM.com#endif 6099651SAndreas.Sandberg@ARM.com} 6109651SAndreas.Sandberg@ARM.com 6119651SAndreas.Sandberg@ARM.comvoid 6129651SAndreas.Sandberg@ARM.comBaseKvmCPU::getOneReg(uint64_t id, void *addr) const 6139651SAndreas.Sandberg@ARM.com{ 6149651SAndreas.Sandberg@ARM.com#ifdef KVM_GET_ONE_REG 6159651SAndreas.Sandberg@ARM.com struct kvm_one_reg reg; 6169651SAndreas.Sandberg@ARM.com reg.id = id; 6179651SAndreas.Sandberg@ARM.com reg.addr = (uint64_t)addr; 6189651SAndreas.Sandberg@ARM.com 6199651SAndreas.Sandberg@ARM.com if (ioctl(KVM_GET_ONE_REG, ®) == -1) { 6209651SAndreas.Sandberg@ARM.com panic("KVM: Failed to get register (0x%x) value (errno: %i)\n", 6219651SAndreas.Sandberg@ARM.com id, errno); 6229651SAndreas.Sandberg@ARM.com } 6239651SAndreas.Sandberg@ARM.com#else 6249651SAndreas.Sandberg@ARM.com panic("KVM_GET_ONE_REG is unsupported on this platform.\n"); 6259651SAndreas.Sandberg@ARM.com#endif 6269651SAndreas.Sandberg@ARM.com} 6279651SAndreas.Sandberg@ARM.com 6289651SAndreas.Sandberg@ARM.comstd::string 6299651SAndreas.Sandberg@ARM.comBaseKvmCPU::getAndFormatOneReg(uint64_t id) const 6309651SAndreas.Sandberg@ARM.com{ 6319651SAndreas.Sandberg@ARM.com#ifdef KVM_GET_ONE_REG 6329651SAndreas.Sandberg@ARM.com std::ostringstream ss; 6339651SAndreas.Sandberg@ARM.com 6349651SAndreas.Sandberg@ARM.com ss.setf(std::ios::hex, std::ios::basefield); 6359651SAndreas.Sandberg@ARM.com ss.setf(std::ios::showbase); 6369651SAndreas.Sandberg@ARM.com#define HANDLE_INTTYPE(len) \ 6379651SAndreas.Sandberg@ARM.com case KVM_REG_SIZE_U ## len: { \ 6389651SAndreas.Sandberg@ARM.com uint ## len ## _t value; \ 6399651SAndreas.Sandberg@ARM.com getOneReg(id, &value); \ 6409651SAndreas.Sandberg@ARM.com ss << value; \ 6419651SAndreas.Sandberg@ARM.com } break 6429651SAndreas.Sandberg@ARM.com 6439651SAndreas.Sandberg@ARM.com#define HANDLE_ARRAY(len) \ 6449651SAndreas.Sandberg@ARM.com case KVM_REG_SIZE_U ## len: { \ 6459651SAndreas.Sandberg@ARM.com uint8_t value[len / 8]; \ 6469651SAndreas.Sandberg@ARM.com getOneReg(id, value); \ 6479651SAndreas.Sandberg@ARM.com ss << "[" << value[0]; \ 6489651SAndreas.Sandberg@ARM.com for (int i = 1; i < len / 8; ++i) \ 6499651SAndreas.Sandberg@ARM.com ss << ", " << value[i]; \ 6509651SAndreas.Sandberg@ARM.com ss << "]"; \ 6519651SAndreas.Sandberg@ARM.com } break 6529651SAndreas.Sandberg@ARM.com 6539651SAndreas.Sandberg@ARM.com switch (id & KVM_REG_SIZE_MASK) { 6549651SAndreas.Sandberg@ARM.com HANDLE_INTTYPE(8); 6559651SAndreas.Sandberg@ARM.com HANDLE_INTTYPE(16); 6569651SAndreas.Sandberg@ARM.com HANDLE_INTTYPE(32); 6579651SAndreas.Sandberg@ARM.com HANDLE_INTTYPE(64); 6589651SAndreas.Sandberg@ARM.com HANDLE_ARRAY(128); 6599651SAndreas.Sandberg@ARM.com HANDLE_ARRAY(256); 6609651SAndreas.Sandberg@ARM.com HANDLE_ARRAY(512); 6619651SAndreas.Sandberg@ARM.com HANDLE_ARRAY(1024); 6629651SAndreas.Sandberg@ARM.com default: 6639651SAndreas.Sandberg@ARM.com ss << "??"; 6649651SAndreas.Sandberg@ARM.com } 6659651SAndreas.Sandberg@ARM.com 6669651SAndreas.Sandberg@ARM.com#undef HANDLE_INTTYPE 6679651SAndreas.Sandberg@ARM.com#undef HANDLE_ARRAY 6689651SAndreas.Sandberg@ARM.com 6699651SAndreas.Sandberg@ARM.com return ss.str(); 6709651SAndreas.Sandberg@ARM.com#else 6719651SAndreas.Sandberg@ARM.com panic("KVM_GET_ONE_REG is unsupported on this platform.\n"); 6729651SAndreas.Sandberg@ARM.com#endif 6739651SAndreas.Sandberg@ARM.com} 6749651SAndreas.Sandberg@ARM.com 6759652SAndreas.Sandberg@ARM.comvoid 6769652SAndreas.Sandberg@ARM.comBaseKvmCPU::syncThreadContext() 6779652SAndreas.Sandberg@ARM.com{ 6789652SAndreas.Sandberg@ARM.com if (!kvmStateDirty) 6799652SAndreas.Sandberg@ARM.com return; 6809652SAndreas.Sandberg@ARM.com 6819652SAndreas.Sandberg@ARM.com assert(!threadContextDirty); 6829652SAndreas.Sandberg@ARM.com 6839652SAndreas.Sandberg@ARM.com updateThreadContext(); 6849652SAndreas.Sandberg@ARM.com kvmStateDirty = false; 6859652SAndreas.Sandberg@ARM.com} 6869652SAndreas.Sandberg@ARM.com 6879652SAndreas.Sandberg@ARM.comvoid 6889652SAndreas.Sandberg@ARM.comBaseKvmCPU::syncKvmState() 6899652SAndreas.Sandberg@ARM.com{ 6909652SAndreas.Sandberg@ARM.com if (!threadContextDirty) 6919652SAndreas.Sandberg@ARM.com return; 6929652SAndreas.Sandberg@ARM.com 6939652SAndreas.Sandberg@ARM.com assert(!kvmStateDirty); 6949652SAndreas.Sandberg@ARM.com 6959652SAndreas.Sandberg@ARM.com updateKvmState(); 6969652SAndreas.Sandberg@ARM.com threadContextDirty = false; 6979652SAndreas.Sandberg@ARM.com} 6989652SAndreas.Sandberg@ARM.com 6999651SAndreas.Sandberg@ARM.comTick 7009651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExit() 7019651SAndreas.Sandberg@ARM.com{ 7029651SAndreas.Sandberg@ARM.com DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason); 7039651SAndreas.Sandberg@ARM.com 7049651SAndreas.Sandberg@ARM.com switch (_kvmRun->exit_reason) { 7059651SAndreas.Sandberg@ARM.com case KVM_EXIT_UNKNOWN: 7069651SAndreas.Sandberg@ARM.com return handleKvmExitUnknown(); 7079651SAndreas.Sandberg@ARM.com 7089651SAndreas.Sandberg@ARM.com case KVM_EXIT_EXCEPTION: 7099651SAndreas.Sandberg@ARM.com return handleKvmExitException(); 7109651SAndreas.Sandberg@ARM.com 7119651SAndreas.Sandberg@ARM.com case KVM_EXIT_IO: 7129651SAndreas.Sandberg@ARM.com ++numIO; 7139651SAndreas.Sandberg@ARM.com return handleKvmExitIO(); 7149651SAndreas.Sandberg@ARM.com 7159651SAndreas.Sandberg@ARM.com case KVM_EXIT_HYPERCALL: 7169651SAndreas.Sandberg@ARM.com ++numHypercalls; 7179651SAndreas.Sandberg@ARM.com return handleKvmExitHypercall(); 7189651SAndreas.Sandberg@ARM.com 7199651SAndreas.Sandberg@ARM.com case KVM_EXIT_HLT: 7209651SAndreas.Sandberg@ARM.com /* The guest has halted and is waiting for interrupts */ 7219651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "handleKvmExitHalt\n"); 7229651SAndreas.Sandberg@ARM.com ++numHalt; 7239651SAndreas.Sandberg@ARM.com 7249651SAndreas.Sandberg@ARM.com // Suspend the thread until the next interrupt arrives 7259651SAndreas.Sandberg@ARM.com thread->suspend(); 7269651SAndreas.Sandberg@ARM.com 7279651SAndreas.Sandberg@ARM.com // This is actually ignored since the thread is suspended. 7289651SAndreas.Sandberg@ARM.com return 0; 7299651SAndreas.Sandberg@ARM.com 7309651SAndreas.Sandberg@ARM.com case KVM_EXIT_MMIO: 7319651SAndreas.Sandberg@ARM.com /* Service memory mapped IO requests */ 7329651SAndreas.Sandberg@ARM.com DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n", 7339651SAndreas.Sandberg@ARM.com _kvmRun->mmio.is_write, 7349651SAndreas.Sandberg@ARM.com _kvmRun->mmio.phys_addr, _kvmRun->mmio.len); 7359651SAndreas.Sandberg@ARM.com 7369651SAndreas.Sandberg@ARM.com ++numMMIO; 7379651SAndreas.Sandberg@ARM.com return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data, 7389651SAndreas.Sandberg@ARM.com _kvmRun->mmio.len, _kvmRun->mmio.is_write); 7399651SAndreas.Sandberg@ARM.com 7409651SAndreas.Sandberg@ARM.com case KVM_EXIT_IRQ_WINDOW_OPEN: 7419651SAndreas.Sandberg@ARM.com return handleKvmExitIRQWindowOpen(); 7429651SAndreas.Sandberg@ARM.com 7439651SAndreas.Sandberg@ARM.com case KVM_EXIT_FAIL_ENTRY: 7449651SAndreas.Sandberg@ARM.com return handleKvmExitFailEntry(); 7459651SAndreas.Sandberg@ARM.com 7469651SAndreas.Sandberg@ARM.com case KVM_EXIT_INTR: 7479651SAndreas.Sandberg@ARM.com /* KVM was interrupted by a signal, restart it in the next 7489651SAndreas.Sandberg@ARM.com * tick. */ 7499651SAndreas.Sandberg@ARM.com return 0; 7509651SAndreas.Sandberg@ARM.com 7519651SAndreas.Sandberg@ARM.com case KVM_EXIT_INTERNAL_ERROR: 7529651SAndreas.Sandberg@ARM.com panic("KVM: Internal error (suberror: %u)\n", 7539651SAndreas.Sandberg@ARM.com _kvmRun->internal.suberror); 7549651SAndreas.Sandberg@ARM.com 7559651SAndreas.Sandberg@ARM.com default: 7569689Sandreas@sandberg.pp.se dump(); 7579651SAndreas.Sandberg@ARM.com panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason); 7589651SAndreas.Sandberg@ARM.com } 7599651SAndreas.Sandberg@ARM.com} 7609651SAndreas.Sandberg@ARM.com 7619651SAndreas.Sandberg@ARM.comTick 7629651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitIO() 7639651SAndreas.Sandberg@ARM.com{ 7649651SAndreas.Sandberg@ARM.com panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n", 7659651SAndreas.Sandberg@ARM.com _kvmRun->io.direction, _kvmRun->io.size, 7669651SAndreas.Sandberg@ARM.com _kvmRun->io.port, _kvmRun->io.count); 7679651SAndreas.Sandberg@ARM.com} 7689651SAndreas.Sandberg@ARM.com 7699651SAndreas.Sandberg@ARM.comTick 7709651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitHypercall() 7719651SAndreas.Sandberg@ARM.com{ 7729651SAndreas.Sandberg@ARM.com panic("KVM: Unhandled hypercall\n"); 7739651SAndreas.Sandberg@ARM.com} 7749651SAndreas.Sandberg@ARM.com 7759651SAndreas.Sandberg@ARM.comTick 7769651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitIRQWindowOpen() 7779651SAndreas.Sandberg@ARM.com{ 7789651SAndreas.Sandberg@ARM.com warn("KVM: Unhandled IRQ window.\n"); 7799651SAndreas.Sandberg@ARM.com return 0; 7809651SAndreas.Sandberg@ARM.com} 7819651SAndreas.Sandberg@ARM.com 7829651SAndreas.Sandberg@ARM.com 7839651SAndreas.Sandberg@ARM.comTick 7849651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitUnknown() 7859651SAndreas.Sandberg@ARM.com{ 7869689Sandreas@sandberg.pp.se dump(); 7879651SAndreas.Sandberg@ARM.com panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n", 7889651SAndreas.Sandberg@ARM.com _kvmRun->hw.hardware_exit_reason); 7899651SAndreas.Sandberg@ARM.com} 7909651SAndreas.Sandberg@ARM.com 7919651SAndreas.Sandberg@ARM.comTick 7929651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitException() 7939651SAndreas.Sandberg@ARM.com{ 7949689Sandreas@sandberg.pp.se dump(); 7959651SAndreas.Sandberg@ARM.com panic("KVM: Got exception when starting vCPU " 7969651SAndreas.Sandberg@ARM.com "(exception: %u, error_code: %u)\n", 7979651SAndreas.Sandberg@ARM.com _kvmRun->ex.exception, _kvmRun->ex.error_code); 7989651SAndreas.Sandberg@ARM.com} 7999651SAndreas.Sandberg@ARM.com 8009651SAndreas.Sandberg@ARM.comTick 8019651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitFailEntry() 8029651SAndreas.Sandberg@ARM.com{ 8039689Sandreas@sandberg.pp.se dump(); 8049651SAndreas.Sandberg@ARM.com panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n", 8059651SAndreas.Sandberg@ARM.com _kvmRun->fail_entry.hardware_entry_failure_reason); 8069651SAndreas.Sandberg@ARM.com} 8079651SAndreas.Sandberg@ARM.com 8089651SAndreas.Sandberg@ARM.comTick 8099651SAndreas.Sandberg@ARM.comBaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write) 8109651SAndreas.Sandberg@ARM.com{ 8119682Sandreas@sandberg.pp.se mmio_req.setPhys(paddr, size, Request::UNCACHEABLE, dataMasterId()); 8129651SAndreas.Sandberg@ARM.com 8139651SAndreas.Sandberg@ARM.com const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq); 8149651SAndreas.Sandberg@ARM.com Packet pkt(&mmio_req, cmd); 8159651SAndreas.Sandberg@ARM.com pkt.dataStatic(data); 8169651SAndreas.Sandberg@ARM.com return dataPort.sendAtomic(&pkt); 8179651SAndreas.Sandberg@ARM.com} 8189651SAndreas.Sandberg@ARM.com 8199651SAndreas.Sandberg@ARM.comint 8209651SAndreas.Sandberg@ARM.comBaseKvmCPU::ioctl(int request, long p1) const 8219651SAndreas.Sandberg@ARM.com{ 8229651SAndreas.Sandberg@ARM.com if (vcpuFD == -1) 8239651SAndreas.Sandberg@ARM.com panic("KVM: CPU ioctl called before initialization\n"); 8249651SAndreas.Sandberg@ARM.com 8259651SAndreas.Sandberg@ARM.com return ::ioctl(vcpuFD, request, p1); 8269651SAndreas.Sandberg@ARM.com} 8279651SAndreas.Sandberg@ARM.com 8289651SAndreas.Sandberg@ARM.comTick 8299651SAndreas.Sandberg@ARM.comBaseKvmCPU::flushCoalescedMMIO() 8309651SAndreas.Sandberg@ARM.com{ 8319651SAndreas.Sandberg@ARM.com if (!mmioRing) 8329651SAndreas.Sandberg@ARM.com return 0; 8339651SAndreas.Sandberg@ARM.com 8349651SAndreas.Sandberg@ARM.com DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n"); 8359651SAndreas.Sandberg@ARM.com 8369651SAndreas.Sandberg@ARM.com // TODO: We might need to do synchronization when we start to 8379651SAndreas.Sandberg@ARM.com // support multiple CPUs 8389651SAndreas.Sandberg@ARM.com Tick ticks(0); 8399651SAndreas.Sandberg@ARM.com while (mmioRing->first != mmioRing->last) { 8409651SAndreas.Sandberg@ARM.com struct kvm_coalesced_mmio &ent( 8419651SAndreas.Sandberg@ARM.com mmioRing->coalesced_mmio[mmioRing->first]); 8429651SAndreas.Sandberg@ARM.com 8439651SAndreas.Sandberg@ARM.com DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n", 8449651SAndreas.Sandberg@ARM.com ent.phys_addr, ent.len); 8459651SAndreas.Sandberg@ARM.com 8469651SAndreas.Sandberg@ARM.com ++numCoalescedMMIO; 8479651SAndreas.Sandberg@ARM.com ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true); 8489651SAndreas.Sandberg@ARM.com 8499651SAndreas.Sandberg@ARM.com mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX; 8509651SAndreas.Sandberg@ARM.com } 8519651SAndreas.Sandberg@ARM.com 8529651SAndreas.Sandberg@ARM.com return ticks; 8539651SAndreas.Sandberg@ARM.com} 8549651SAndreas.Sandberg@ARM.com 8559651SAndreas.Sandberg@ARM.comvoid 8569651SAndreas.Sandberg@ARM.comBaseKvmCPU::setupSignalHandler() 8579651SAndreas.Sandberg@ARM.com{ 8589651SAndreas.Sandberg@ARM.com struct sigaction sa; 8599651SAndreas.Sandberg@ARM.com 8609651SAndreas.Sandberg@ARM.com memset(&sa, 0, sizeof(sa)); 8619651SAndreas.Sandberg@ARM.com sa.sa_sigaction = onTimerOverflow; 8629651SAndreas.Sandberg@ARM.com sa.sa_flags = SA_SIGINFO | SA_RESTART; 8639651SAndreas.Sandberg@ARM.com if (sigaction(KVM_TIMER_SIGNAL, &sa, NULL) == -1) 8649651SAndreas.Sandberg@ARM.com panic("KVM: Failed to setup vCPU signal handler\n"); 8659651SAndreas.Sandberg@ARM.com} 8669651SAndreas.Sandberg@ARM.com 8679651SAndreas.Sandberg@ARM.comvoid 8689651SAndreas.Sandberg@ARM.comBaseKvmCPU::setupCounters() 8699651SAndreas.Sandberg@ARM.com{ 8709651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "Attaching cycle counter...\n"); 8719651SAndreas.Sandberg@ARM.com PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE, 8729651SAndreas.Sandberg@ARM.com PERF_COUNT_HW_CPU_CYCLES); 8739651SAndreas.Sandberg@ARM.com cfgCycles.disabled(true) 8749651SAndreas.Sandberg@ARM.com .pinned(true); 8759655SAndreas.Sandberg@ARM.com 8769655SAndreas.Sandberg@ARM.com if (perfControlledByTimer) { 8779655SAndreas.Sandberg@ARM.com // We need to configure the cycles counter to send overflows 8789655SAndreas.Sandberg@ARM.com // since we are going to use it to trigger timer signals that 8799655SAndreas.Sandberg@ARM.com // trap back into m5 from KVM. In practice, this means that we 8809655SAndreas.Sandberg@ARM.com // need to set some non-zero sample period that gets 8819655SAndreas.Sandberg@ARM.com // overridden when the timer is armed. 8829655SAndreas.Sandberg@ARM.com cfgCycles.wakeupEvents(1) 8839655SAndreas.Sandberg@ARM.com .samplePeriod(42); 8849655SAndreas.Sandberg@ARM.com } 8859655SAndreas.Sandberg@ARM.com 8869651SAndreas.Sandberg@ARM.com hwCycles.attach(cfgCycles, 8879651SAndreas.Sandberg@ARM.com 0); // TID (0 => currentThread) 8889651SAndreas.Sandberg@ARM.com 8899651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "Attaching instruction counter...\n"); 8909651SAndreas.Sandberg@ARM.com PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE, 8919651SAndreas.Sandberg@ARM.com PERF_COUNT_HW_INSTRUCTIONS); 8929651SAndreas.Sandberg@ARM.com hwInstructions.attach(cfgInstructions, 8939651SAndreas.Sandberg@ARM.com 0, // TID (0 => currentThread) 8949651SAndreas.Sandberg@ARM.com hwCycles); 8959651SAndreas.Sandberg@ARM.com} 896