base.cc revision 10905
19651SAndreas.Sandberg@ARM.com/* 210858Sandreas.sandberg@arm.com * Copyright (c) 2012, 2015 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.comBaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params) 679651SAndreas.Sandberg@ARM.com : BaseCPU(params), 689651SAndreas.Sandberg@ARM.com vm(*params->kvmVM), 699651SAndreas.Sandberg@ARM.com _status(Idle), 709651SAndreas.Sandberg@ARM.com dataPort(name() + ".dcache_port", this), 719651SAndreas.Sandberg@ARM.com instPort(name() + ".icache_port", this), 729652SAndreas.Sandberg@ARM.com threadContextDirty(true), 739652SAndreas.Sandberg@ARM.com kvmStateDirty(false), 749651SAndreas.Sandberg@ARM.com vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0), 759651SAndreas.Sandberg@ARM.com _kvmRun(NULL), mmioRing(NULL), 769651SAndreas.Sandberg@ARM.com pageSize(sysconf(_SC_PAGE_SIZE)), 779651SAndreas.Sandberg@ARM.com tickEvent(*this), 789892Sandreas@sandberg.pp.se activeInstPeriod(0), 799655SAndreas.Sandberg@ARM.com perfControlledByTimer(params->usePerfOverflow), 809752Sandreas@sandberg.pp.se hostFactor(params->hostFactor), 819753Sandreas@sandberg.pp.se drainManager(NULL), 829752Sandreas@sandberg.pp.se ctrInsts(0) 839651SAndreas.Sandberg@ARM.com{ 849651SAndreas.Sandberg@ARM.com if (pageSize == -1) 859651SAndreas.Sandberg@ARM.com panic("KVM: Failed to determine host page size (%i)\n", 869651SAndreas.Sandberg@ARM.com errno); 879651SAndreas.Sandberg@ARM.com 8810553Salexandru.dutu@amd.com if (FullSystem) 8910553Salexandru.dutu@amd.com thread = new SimpleThread(this, 0, params->system, params->itb, params->dtb, 9010553Salexandru.dutu@amd.com params->isa[0]); 9110553Salexandru.dutu@amd.com else 9210553Salexandru.dutu@amd.com thread = new SimpleThread(this, /* thread_num */ 0, params->system, 9310553Salexandru.dutu@amd.com params->workload[0], params->itb, 9410553Salexandru.dutu@amd.com params->dtb, params->isa[0]); 9510553Salexandru.dutu@amd.com 969651SAndreas.Sandberg@ARM.com thread->setStatus(ThreadContext::Halted); 979651SAndreas.Sandberg@ARM.com tc = thread->getTC(); 989651SAndreas.Sandberg@ARM.com threadContexts.push_back(tc); 999651SAndreas.Sandberg@ARM.com} 1009651SAndreas.Sandberg@ARM.com 1019651SAndreas.Sandberg@ARM.comBaseKvmCPU::~BaseKvmCPU() 1029651SAndreas.Sandberg@ARM.com{ 1039651SAndreas.Sandberg@ARM.com if (_kvmRun) 1049651SAndreas.Sandberg@ARM.com munmap(_kvmRun, vcpuMMapSize); 1059651SAndreas.Sandberg@ARM.com close(vcpuFD); 1069651SAndreas.Sandberg@ARM.com} 1079651SAndreas.Sandberg@ARM.com 1089651SAndreas.Sandberg@ARM.comvoid 1099651SAndreas.Sandberg@ARM.comBaseKvmCPU::init() 1109651SAndreas.Sandberg@ARM.com{ 1119651SAndreas.Sandberg@ARM.com BaseCPU::init(); 1129651SAndreas.Sandberg@ARM.com 1139651SAndreas.Sandberg@ARM.com if (numThreads != 1) 1149651SAndreas.Sandberg@ARM.com fatal("KVM: Multithreading not supported"); 1159651SAndreas.Sandberg@ARM.com 1169651SAndreas.Sandberg@ARM.com tc->initMemProxies(tc); 1179651SAndreas.Sandberg@ARM.com 1189651SAndreas.Sandberg@ARM.com // initialize CPU, including PC 1199651SAndreas.Sandberg@ARM.com if (FullSystem && !switchedOut()) 1209651SAndreas.Sandberg@ARM.com TheISA::initCPU(tc, tc->contextId()); 1219651SAndreas.Sandberg@ARM.com} 1229651SAndreas.Sandberg@ARM.com 1239651SAndreas.Sandberg@ARM.comvoid 1249651SAndreas.Sandberg@ARM.comBaseKvmCPU::startup() 1259651SAndreas.Sandberg@ARM.com{ 1269690Sandreas@sandberg.pp.se const BaseKvmCPUParams * const p( 1279690Sandreas@sandberg.pp.se dynamic_cast<const BaseKvmCPUParams *>(params())); 1289690Sandreas@sandberg.pp.se 1299651SAndreas.Sandberg@ARM.com Kvm &kvm(vm.kvm); 1309651SAndreas.Sandberg@ARM.com 1319651SAndreas.Sandberg@ARM.com BaseCPU::startup(); 1329651SAndreas.Sandberg@ARM.com 1339651SAndreas.Sandberg@ARM.com assert(vcpuFD == -1); 1349651SAndreas.Sandberg@ARM.com 1359651SAndreas.Sandberg@ARM.com // Tell the VM that a CPU is about to start. 1369651SAndreas.Sandberg@ARM.com vm.cpuStartup(); 1379651SAndreas.Sandberg@ARM.com 1389651SAndreas.Sandberg@ARM.com // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are 1399651SAndreas.Sandberg@ARM.com // not guaranteed that the parent KVM VM has initialized at that 1409651SAndreas.Sandberg@ARM.com // point. Initialize virtual CPUs here instead. 1419651SAndreas.Sandberg@ARM.com vcpuFD = vm.createVCPU(vcpuID); 1429651SAndreas.Sandberg@ARM.com 1439651SAndreas.Sandberg@ARM.com // Map the KVM run structure */ 1449651SAndreas.Sandberg@ARM.com vcpuMMapSize = kvm.getVCPUMMapSize(); 1459651SAndreas.Sandberg@ARM.com _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize, 1469651SAndreas.Sandberg@ARM.com PROT_READ | PROT_WRITE, MAP_SHARED, 1479651SAndreas.Sandberg@ARM.com vcpuFD, 0); 1489651SAndreas.Sandberg@ARM.com if (_kvmRun == MAP_FAILED) 1499651SAndreas.Sandberg@ARM.com panic("KVM: Failed to map run data structure\n"); 1509651SAndreas.Sandberg@ARM.com 1519651SAndreas.Sandberg@ARM.com // Setup a pointer to the MMIO ring buffer if coalesced MMIO is 1529651SAndreas.Sandberg@ARM.com // available. The offset into the KVM's communication page is 1539651SAndreas.Sandberg@ARM.com // provided by the coalesced MMIO capability. 1549651SAndreas.Sandberg@ARM.com int mmioOffset(kvm.capCoalescedMMIO()); 1559690Sandreas@sandberg.pp.se if (!p->useCoalescedMMIO) { 1569690Sandreas@sandberg.pp.se inform("KVM: Coalesced MMIO disabled by config.\n"); 1579690Sandreas@sandberg.pp.se } else if (mmioOffset) { 1589651SAndreas.Sandberg@ARM.com inform("KVM: Coalesced IO available\n"); 1599651SAndreas.Sandberg@ARM.com mmioRing = (struct kvm_coalesced_mmio_ring *)( 1609651SAndreas.Sandberg@ARM.com (char *)_kvmRun + (mmioOffset * pageSize)); 1619651SAndreas.Sandberg@ARM.com } else { 1629651SAndreas.Sandberg@ARM.com inform("KVM: Coalesced not supported by host OS\n"); 1639651SAndreas.Sandberg@ARM.com } 1649732Sandreas@sandberg.pp.se 1659732Sandreas@sandberg.pp.se thread->startup(); 16610073Sandreas@sandberg.pp.se 16710073Sandreas@sandberg.pp.se Event *startupEvent( 16810073Sandreas@sandberg.pp.se new EventWrapper<BaseKvmCPU, 16910073Sandreas@sandberg.pp.se &BaseKvmCPU::startupThread>(this, true)); 17010073Sandreas@sandberg.pp.se schedule(startupEvent, curTick()); 17110073Sandreas@sandberg.pp.se} 17210073Sandreas@sandberg.pp.se 17310073Sandreas@sandberg.pp.sevoid 17410073Sandreas@sandberg.pp.seBaseKvmCPU::startupThread() 17510073Sandreas@sandberg.pp.se{ 17610073Sandreas@sandberg.pp.se // Do thread-specific initialization. We need to setup signal 17710073Sandreas@sandberg.pp.se // delivery for counters and timers from within the thread that 17810073Sandreas@sandberg.pp.se // will execute the event queue to ensure that signals are 17910073Sandreas@sandberg.pp.se // delivered to the right threads. 18010073Sandreas@sandberg.pp.se const BaseKvmCPUParams * const p( 18110073Sandreas@sandberg.pp.se dynamic_cast<const BaseKvmCPUParams *>(params())); 18210073Sandreas@sandberg.pp.se 18310114Sandreas@sandberg.pp.se vcpuThread = pthread_self(); 18410114Sandreas@sandberg.pp.se 18510098Sandreas@sandberg.pp.se // Setup signal handlers. This has to be done after the vCPU is 18610098Sandreas@sandberg.pp.se // created since it manipulates the vCPU signal mask. 18710098Sandreas@sandberg.pp.se setupSignalHandler(); 18810098Sandreas@sandberg.pp.se 18910073Sandreas@sandberg.pp.se setupCounters(); 19010073Sandreas@sandberg.pp.se 19110073Sandreas@sandberg.pp.se if (p->usePerfOverflow) 19210073Sandreas@sandberg.pp.se runTimer.reset(new PerfKvmTimer(hwCycles, 19310114Sandreas@sandberg.pp.se KVM_KICK_SIGNAL, 19410073Sandreas@sandberg.pp.se p->hostFactor, 19510073Sandreas@sandberg.pp.se p->hostFreq)); 19610073Sandreas@sandberg.pp.se else 19710114Sandreas@sandberg.pp.se runTimer.reset(new PosixKvmTimer(KVM_KICK_SIGNAL, CLOCK_MONOTONIC, 19810073Sandreas@sandberg.pp.se p->hostFactor, 19910073Sandreas@sandberg.pp.se p->hostFreq)); 20010073Sandreas@sandberg.pp.se 2019651SAndreas.Sandberg@ARM.com} 2029651SAndreas.Sandberg@ARM.com 2039651SAndreas.Sandberg@ARM.comvoid 2049651SAndreas.Sandberg@ARM.comBaseKvmCPU::regStats() 2059651SAndreas.Sandberg@ARM.com{ 2069651SAndreas.Sandberg@ARM.com using namespace Stats; 2079651SAndreas.Sandberg@ARM.com 2089651SAndreas.Sandberg@ARM.com BaseCPU::regStats(); 2099651SAndreas.Sandberg@ARM.com 2109684Sandreas@sandberg.pp.se numInsts 2119684Sandreas@sandberg.pp.se .name(name() + ".committedInsts") 2129684Sandreas@sandberg.pp.se .desc("Number of instructions committed") 2139684Sandreas@sandberg.pp.se ; 2149684Sandreas@sandberg.pp.se 2159651SAndreas.Sandberg@ARM.com numVMExits 2169651SAndreas.Sandberg@ARM.com .name(name() + ".numVMExits") 2179651SAndreas.Sandberg@ARM.com .desc("total number of KVM exits") 2189651SAndreas.Sandberg@ARM.com ; 2199651SAndreas.Sandberg@ARM.com 2209755Sandreas@sandberg.pp.se numVMHalfEntries 2219755Sandreas@sandberg.pp.se .name(name() + ".numVMHalfEntries") 2229755Sandreas@sandberg.pp.se .desc("number of KVM entries to finalize pending operations") 2239755Sandreas@sandberg.pp.se ; 2249755Sandreas@sandberg.pp.se 2259755Sandreas@sandberg.pp.se numExitSignal 2269755Sandreas@sandberg.pp.se .name(name() + ".numExitSignal") 2279755Sandreas@sandberg.pp.se .desc("exits due to signal delivery") 2289755Sandreas@sandberg.pp.se ; 2299755Sandreas@sandberg.pp.se 2309651SAndreas.Sandberg@ARM.com numMMIO 2319651SAndreas.Sandberg@ARM.com .name(name() + ".numMMIO") 2329651SAndreas.Sandberg@ARM.com .desc("number of VM exits due to memory mapped IO") 2339651SAndreas.Sandberg@ARM.com ; 2349651SAndreas.Sandberg@ARM.com 2359651SAndreas.Sandberg@ARM.com numCoalescedMMIO 2369651SAndreas.Sandberg@ARM.com .name(name() + ".numCoalescedMMIO") 2379651SAndreas.Sandberg@ARM.com .desc("number of coalesced memory mapped IO requests") 2389651SAndreas.Sandberg@ARM.com ; 2399651SAndreas.Sandberg@ARM.com 2409651SAndreas.Sandberg@ARM.com numIO 2419651SAndreas.Sandberg@ARM.com .name(name() + ".numIO") 2429651SAndreas.Sandberg@ARM.com .desc("number of VM exits due to legacy IO") 2439651SAndreas.Sandberg@ARM.com ; 2449651SAndreas.Sandberg@ARM.com 2459651SAndreas.Sandberg@ARM.com numHalt 2469651SAndreas.Sandberg@ARM.com .name(name() + ".numHalt") 2479651SAndreas.Sandberg@ARM.com .desc("number of VM exits due to wait for interrupt instructions") 2489651SAndreas.Sandberg@ARM.com ; 2499651SAndreas.Sandberg@ARM.com 2509651SAndreas.Sandberg@ARM.com numInterrupts 2519651SAndreas.Sandberg@ARM.com .name(name() + ".numInterrupts") 2529651SAndreas.Sandberg@ARM.com .desc("number of interrupts delivered") 2539651SAndreas.Sandberg@ARM.com ; 2549651SAndreas.Sandberg@ARM.com 2559651SAndreas.Sandberg@ARM.com numHypercalls 2569651SAndreas.Sandberg@ARM.com .name(name() + ".numHypercalls") 2579651SAndreas.Sandberg@ARM.com .desc("number of hypercalls") 2589651SAndreas.Sandberg@ARM.com ; 2599651SAndreas.Sandberg@ARM.com} 2609651SAndreas.Sandberg@ARM.com 2619651SAndreas.Sandberg@ARM.comvoid 26210905Sandreas.sandberg@arm.comBaseKvmCPU::serializeThread(CheckpointOut &cp, ThreadID tid) const 2639651SAndreas.Sandberg@ARM.com{ 2649683Sandreas@sandberg.pp.se if (DTRACE(Checkpoint)) { 2659683Sandreas@sandberg.pp.se DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid); 2669683Sandreas@sandberg.pp.se dump(); 2679683Sandreas@sandberg.pp.se } 2689683Sandreas@sandberg.pp.se 2699651SAndreas.Sandberg@ARM.com assert(tid == 0); 2709651SAndreas.Sandberg@ARM.com assert(_status == Idle); 27110905Sandreas.sandberg@arm.com thread->serialize(cp); 2729651SAndreas.Sandberg@ARM.com} 2739651SAndreas.Sandberg@ARM.com 2749651SAndreas.Sandberg@ARM.comvoid 27510905Sandreas.sandberg@arm.comBaseKvmCPU::unserializeThread(CheckpointIn &cp, ThreadID tid) 2769651SAndreas.Sandberg@ARM.com{ 2779683Sandreas@sandberg.pp.se DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid); 2789683Sandreas@sandberg.pp.se 2799651SAndreas.Sandberg@ARM.com assert(tid == 0); 2809651SAndreas.Sandberg@ARM.com assert(_status == Idle); 28110905Sandreas.sandberg@arm.com thread->unserialize(cp); 2829652SAndreas.Sandberg@ARM.com threadContextDirty = true; 2839651SAndreas.Sandberg@ARM.com} 2849651SAndreas.Sandberg@ARM.com 2859651SAndreas.Sandberg@ARM.comunsigned int 2869651SAndreas.Sandberg@ARM.comBaseKvmCPU::drain(DrainManager *dm) 2879651SAndreas.Sandberg@ARM.com{ 2889651SAndreas.Sandberg@ARM.com if (switchedOut()) 2899651SAndreas.Sandberg@ARM.com return 0; 2909651SAndreas.Sandberg@ARM.com 2919753Sandreas@sandberg.pp.se DPRINTF(Drain, "BaseKvmCPU::drain\n"); 2929753Sandreas@sandberg.pp.se switch (_status) { 2939753Sandreas@sandberg.pp.se case Running: 2949753Sandreas@sandberg.pp.se // The base KVM code is normally ready when it is in the 2959753Sandreas@sandberg.pp.se // Running state, but the architecture specific code might be 2969753Sandreas@sandberg.pp.se // of a different opinion. This may happen when the CPU been 2979753Sandreas@sandberg.pp.se // notified of an event that hasn't been accepted by the vCPU 2989753Sandreas@sandberg.pp.se // yet. 2999753Sandreas@sandberg.pp.se if (!archIsDrained()) { 3009753Sandreas@sandberg.pp.se drainManager = dm; 3019753Sandreas@sandberg.pp.se return 1; 3029753Sandreas@sandberg.pp.se } 3039651SAndreas.Sandberg@ARM.com 3049753Sandreas@sandberg.pp.se // The state of the CPU is consistent, so we don't need to do 3059753Sandreas@sandberg.pp.se // anything special to drain it. We simply de-schedule the 3069753Sandreas@sandberg.pp.se // tick event and enter the Idle state to prevent nasty things 3079753Sandreas@sandberg.pp.se // like MMIOs from happening. 3089753Sandreas@sandberg.pp.se if (tickEvent.scheduled()) 3099753Sandreas@sandberg.pp.se deschedule(tickEvent); 3109753Sandreas@sandberg.pp.se _status = Idle; 3119651SAndreas.Sandberg@ARM.com 3129753Sandreas@sandberg.pp.se /** FALLTHROUGH */ 3139753Sandreas@sandberg.pp.se case Idle: 3149753Sandreas@sandberg.pp.se // Idle, no need to drain 3159753Sandreas@sandberg.pp.se assert(!tickEvent.scheduled()); 3169753Sandreas@sandberg.pp.se 3179753Sandreas@sandberg.pp.se // Sync the thread context here since we'll need it when we 3189753Sandreas@sandberg.pp.se // switch CPUs or checkpoint the CPU. 3199753Sandreas@sandberg.pp.se syncThreadContext(); 3209753Sandreas@sandberg.pp.se 3219753Sandreas@sandberg.pp.se return 0; 3229753Sandreas@sandberg.pp.se 3239753Sandreas@sandberg.pp.se case RunningServiceCompletion: 3249753Sandreas@sandberg.pp.se // The CPU has just requested a service that was handled in 3259753Sandreas@sandberg.pp.se // the RunningService state, but the results have still not 3269753Sandreas@sandberg.pp.se // been reported to the CPU. Now, we /could/ probably just 3279753Sandreas@sandberg.pp.se // update the register state ourselves instead of letting KVM 3289753Sandreas@sandberg.pp.se // handle it, but that would be tricky. Instead, we enter KVM 3299753Sandreas@sandberg.pp.se // and let it do its stuff. 3309753Sandreas@sandberg.pp.se drainManager = dm; 3319753Sandreas@sandberg.pp.se 3329753Sandreas@sandberg.pp.se DPRINTF(Drain, "KVM CPU is waiting for service completion, " 3339753Sandreas@sandberg.pp.se "requesting drain.\n"); 3349753Sandreas@sandberg.pp.se return 1; 3359753Sandreas@sandberg.pp.se 3369753Sandreas@sandberg.pp.se case RunningService: 3379753Sandreas@sandberg.pp.se // We need to drain since the CPU is waiting for service (e.g., MMIOs) 3389753Sandreas@sandberg.pp.se drainManager = dm; 3399753Sandreas@sandberg.pp.se 3409753Sandreas@sandberg.pp.se DPRINTF(Drain, "KVM CPU is waiting for service, requesting drain.\n"); 3419753Sandreas@sandberg.pp.se return 1; 3429753Sandreas@sandberg.pp.se 3439753Sandreas@sandberg.pp.se default: 3449753Sandreas@sandberg.pp.se panic("KVM: Unhandled CPU state in drain()\n"); 3459753Sandreas@sandberg.pp.se return 0; 3469753Sandreas@sandberg.pp.se } 3479651SAndreas.Sandberg@ARM.com} 3489651SAndreas.Sandberg@ARM.com 3499651SAndreas.Sandberg@ARM.comvoid 3509651SAndreas.Sandberg@ARM.comBaseKvmCPU::drainResume() 3519651SAndreas.Sandberg@ARM.com{ 3529651SAndreas.Sandberg@ARM.com assert(!tickEvent.scheduled()); 3539651SAndreas.Sandberg@ARM.com 3549651SAndreas.Sandberg@ARM.com // We might have been switched out. In that case, we don't need to 3559651SAndreas.Sandberg@ARM.com // do anything. 3569651SAndreas.Sandberg@ARM.com if (switchedOut()) 3579651SAndreas.Sandberg@ARM.com return; 3589651SAndreas.Sandberg@ARM.com 3599651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "drainResume\n"); 3609651SAndreas.Sandberg@ARM.com verifyMemoryMode(); 3619651SAndreas.Sandberg@ARM.com 3629651SAndreas.Sandberg@ARM.com // The tick event is de-scheduled as a part of the draining 3639651SAndreas.Sandberg@ARM.com // process. Re-schedule it if the thread context is active. 3649651SAndreas.Sandberg@ARM.com if (tc->status() == ThreadContext::Active) { 3659651SAndreas.Sandberg@ARM.com schedule(tickEvent, nextCycle()); 3669651SAndreas.Sandberg@ARM.com _status = Running; 3679651SAndreas.Sandberg@ARM.com } else { 3689651SAndreas.Sandberg@ARM.com _status = Idle; 3699651SAndreas.Sandberg@ARM.com } 3709651SAndreas.Sandberg@ARM.com} 3719651SAndreas.Sandberg@ARM.com 3729651SAndreas.Sandberg@ARM.comvoid 3739651SAndreas.Sandberg@ARM.comBaseKvmCPU::switchOut() 3749651SAndreas.Sandberg@ARM.com{ 3759652SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "switchOut\n"); 3769652SAndreas.Sandberg@ARM.com 3779651SAndreas.Sandberg@ARM.com BaseCPU::switchOut(); 3789651SAndreas.Sandberg@ARM.com 3799651SAndreas.Sandberg@ARM.com // We should have drained prior to executing a switchOut, which 3809651SAndreas.Sandberg@ARM.com // means that the tick event shouldn't be scheduled and the CPU is 3819651SAndreas.Sandberg@ARM.com // idle. 3829651SAndreas.Sandberg@ARM.com assert(!tickEvent.scheduled()); 3839651SAndreas.Sandberg@ARM.com assert(_status == Idle); 3849651SAndreas.Sandberg@ARM.com} 3859651SAndreas.Sandberg@ARM.com 3869651SAndreas.Sandberg@ARM.comvoid 3879651SAndreas.Sandberg@ARM.comBaseKvmCPU::takeOverFrom(BaseCPU *cpu) 3889651SAndreas.Sandberg@ARM.com{ 3899651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "takeOverFrom\n"); 3909651SAndreas.Sandberg@ARM.com 3919651SAndreas.Sandberg@ARM.com BaseCPU::takeOverFrom(cpu); 3929651SAndreas.Sandberg@ARM.com 3939651SAndreas.Sandberg@ARM.com // We should have drained prior to executing a switchOut, which 3949651SAndreas.Sandberg@ARM.com // means that the tick event shouldn't be scheduled and the CPU is 3959651SAndreas.Sandberg@ARM.com // idle. 3969651SAndreas.Sandberg@ARM.com assert(!tickEvent.scheduled()); 3979651SAndreas.Sandberg@ARM.com assert(_status == Idle); 3989651SAndreas.Sandberg@ARM.com assert(threadContexts.size() == 1); 3999651SAndreas.Sandberg@ARM.com 4009753Sandreas@sandberg.pp.se // Force an update of the KVM state here instead of flagging the 4019753Sandreas@sandberg.pp.se // TC as dirty. This is not ideal from a performance point of 4029753Sandreas@sandberg.pp.se // view, but it makes debugging easier as it allows meaningful KVM 4039753Sandreas@sandberg.pp.se // state to be dumped before and after a takeover. 4049753Sandreas@sandberg.pp.se updateKvmState(); 4059753Sandreas@sandberg.pp.se threadContextDirty = false; 4069651SAndreas.Sandberg@ARM.com} 4079651SAndreas.Sandberg@ARM.com 4089651SAndreas.Sandberg@ARM.comvoid 4099651SAndreas.Sandberg@ARM.comBaseKvmCPU::verifyMemoryMode() const 4109651SAndreas.Sandberg@ARM.com{ 4119651SAndreas.Sandberg@ARM.com if (!(system->isAtomicMode() && system->bypassCaches())) { 4129651SAndreas.Sandberg@ARM.com fatal("The KVM-based CPUs requires the memory system to be in the " 4139651SAndreas.Sandberg@ARM.com "'atomic_noncaching' mode.\n"); 4149651SAndreas.Sandberg@ARM.com } 4159651SAndreas.Sandberg@ARM.com} 4169651SAndreas.Sandberg@ARM.com 4179651SAndreas.Sandberg@ARM.comvoid 4189651SAndreas.Sandberg@ARM.comBaseKvmCPU::wakeup() 4199651SAndreas.Sandberg@ARM.com{ 4209651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "wakeup()\n"); 42110157Sandreas@sandberg.pp.se // This method might have been called from another 42210157Sandreas@sandberg.pp.se // context. Migrate to this SimObject's event queue when 42310157Sandreas@sandberg.pp.se // delivering the wakeup signal. 42410157Sandreas@sandberg.pp.se EventQueue::ScopedMigration migrate(eventQueue()); 42510157Sandreas@sandberg.pp.se 42610157Sandreas@sandberg.pp.se // Kick the vCPU to get it to come out of KVM. 42710157Sandreas@sandberg.pp.se kick(); 4289651SAndreas.Sandberg@ARM.com 4299651SAndreas.Sandberg@ARM.com if (thread->status() != ThreadContext::Suspended) 4309651SAndreas.Sandberg@ARM.com return; 4319651SAndreas.Sandberg@ARM.com 4329651SAndreas.Sandberg@ARM.com thread->activate(); 4339651SAndreas.Sandberg@ARM.com} 4349651SAndreas.Sandberg@ARM.com 4359651SAndreas.Sandberg@ARM.comvoid 43610407Smitch.hayenga@arm.comBaseKvmCPU::activateContext(ThreadID thread_num) 4379651SAndreas.Sandberg@ARM.com{ 43810407Smitch.hayenga@arm.com DPRINTF(Kvm, "ActivateContext %d\n", thread_num); 4399651SAndreas.Sandberg@ARM.com 4409651SAndreas.Sandberg@ARM.com assert(thread_num == 0); 4419651SAndreas.Sandberg@ARM.com assert(thread); 4429651SAndreas.Sandberg@ARM.com 4439651SAndreas.Sandberg@ARM.com assert(_status == Idle); 4449651SAndreas.Sandberg@ARM.com assert(!tickEvent.scheduled()); 4459651SAndreas.Sandberg@ARM.com 4469754Sandreas@sandberg.pp.se numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend); 4479651SAndreas.Sandberg@ARM.com 44810407Smitch.hayenga@arm.com schedule(tickEvent, clockEdge(Cycles(0))); 4499651SAndreas.Sandberg@ARM.com _status = Running; 4509651SAndreas.Sandberg@ARM.com} 4519651SAndreas.Sandberg@ARM.com 4529651SAndreas.Sandberg@ARM.com 4539651SAndreas.Sandberg@ARM.comvoid 4549651SAndreas.Sandberg@ARM.comBaseKvmCPU::suspendContext(ThreadID thread_num) 4559651SAndreas.Sandberg@ARM.com{ 4569651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "SuspendContext %d\n", thread_num); 4579651SAndreas.Sandberg@ARM.com 4589651SAndreas.Sandberg@ARM.com assert(thread_num == 0); 4599651SAndreas.Sandberg@ARM.com assert(thread); 4609651SAndreas.Sandberg@ARM.com 4619651SAndreas.Sandberg@ARM.com if (_status == Idle) 4629651SAndreas.Sandberg@ARM.com return; 4639651SAndreas.Sandberg@ARM.com 46410553Salexandru.dutu@amd.com assert(_status == Running || _status == RunningServiceCompletion); 4659651SAndreas.Sandberg@ARM.com 4669651SAndreas.Sandberg@ARM.com // The tick event may no be scheduled if the quest has requested 4679651SAndreas.Sandberg@ARM.com // the monitor to wait for interrupts. The normal CPU models can 4689651SAndreas.Sandberg@ARM.com // get their tick events descheduled by quiesce instructions, but 4699651SAndreas.Sandberg@ARM.com // that can't happen here. 4709651SAndreas.Sandberg@ARM.com if (tickEvent.scheduled()) 4719651SAndreas.Sandberg@ARM.com deschedule(tickEvent); 4729651SAndreas.Sandberg@ARM.com 4739651SAndreas.Sandberg@ARM.com _status = Idle; 4749651SAndreas.Sandberg@ARM.com} 4759651SAndreas.Sandberg@ARM.com 4769651SAndreas.Sandberg@ARM.comvoid 4779651SAndreas.Sandberg@ARM.comBaseKvmCPU::deallocateContext(ThreadID thread_num) 4789651SAndreas.Sandberg@ARM.com{ 4799651SAndreas.Sandberg@ARM.com // for now, these are equivalent 4809651SAndreas.Sandberg@ARM.com suspendContext(thread_num); 4819651SAndreas.Sandberg@ARM.com} 4829651SAndreas.Sandberg@ARM.com 4839651SAndreas.Sandberg@ARM.comvoid 4849651SAndreas.Sandberg@ARM.comBaseKvmCPU::haltContext(ThreadID thread_num) 4859651SAndreas.Sandberg@ARM.com{ 4869651SAndreas.Sandberg@ARM.com // for now, these are equivalent 4879651SAndreas.Sandberg@ARM.com suspendContext(thread_num); 4889651SAndreas.Sandberg@ARM.com} 4899651SAndreas.Sandberg@ARM.com 4909652SAndreas.Sandberg@ARM.comThreadContext * 4919652SAndreas.Sandberg@ARM.comBaseKvmCPU::getContext(int tn) 4929652SAndreas.Sandberg@ARM.com{ 4939652SAndreas.Sandberg@ARM.com assert(tn == 0); 4949652SAndreas.Sandberg@ARM.com syncThreadContext(); 4959652SAndreas.Sandberg@ARM.com return tc; 4969652SAndreas.Sandberg@ARM.com} 4979652SAndreas.Sandberg@ARM.com 4989652SAndreas.Sandberg@ARM.com 4999651SAndreas.Sandberg@ARM.comCounter 5009651SAndreas.Sandberg@ARM.comBaseKvmCPU::totalInsts() const 5019651SAndreas.Sandberg@ARM.com{ 5029752Sandreas@sandberg.pp.se return ctrInsts; 5039651SAndreas.Sandberg@ARM.com} 5049651SAndreas.Sandberg@ARM.com 5059651SAndreas.Sandberg@ARM.comCounter 5069651SAndreas.Sandberg@ARM.comBaseKvmCPU::totalOps() const 5079651SAndreas.Sandberg@ARM.com{ 5089651SAndreas.Sandberg@ARM.com hack_once("Pretending totalOps is equivalent to totalInsts()\n"); 5099752Sandreas@sandberg.pp.se return ctrInsts; 5109651SAndreas.Sandberg@ARM.com} 5119651SAndreas.Sandberg@ARM.com 5129651SAndreas.Sandberg@ARM.comvoid 51310905Sandreas.sandberg@arm.comBaseKvmCPU::dump() const 5149651SAndreas.Sandberg@ARM.com{ 5159651SAndreas.Sandberg@ARM.com inform("State dumping not implemented."); 5169651SAndreas.Sandberg@ARM.com} 5179651SAndreas.Sandberg@ARM.com 5189651SAndreas.Sandberg@ARM.comvoid 5199651SAndreas.Sandberg@ARM.comBaseKvmCPU::tick() 5209651SAndreas.Sandberg@ARM.com{ 5219753Sandreas@sandberg.pp.se Tick delay(0); 5229753Sandreas@sandberg.pp.se assert(_status != Idle); 5239651SAndreas.Sandberg@ARM.com 5249651SAndreas.Sandberg@ARM.com switch (_status) { 5259753Sandreas@sandberg.pp.se case RunningService: 5269753Sandreas@sandberg.pp.se // handleKvmExit() will determine the next state of the CPU 5279753Sandreas@sandberg.pp.se delay = handleKvmExit(); 5289753Sandreas@sandberg.pp.se 5299753Sandreas@sandberg.pp.se if (tryDrain()) 5309753Sandreas@sandberg.pp.se _status = Idle; 5319651SAndreas.Sandberg@ARM.com break; 5329651SAndreas.Sandberg@ARM.com 5339753Sandreas@sandberg.pp.se case RunningServiceCompletion: 5349753Sandreas@sandberg.pp.se case Running: { 53510858Sandreas.sandberg@arm.com const uint64_t nextInstEvent( 53610858Sandreas.sandberg@arm.com !comInstEventQueue[0]->empty() ? 53710858Sandreas.sandberg@arm.com comInstEventQueue[0]->nextTick() : UINT64_MAX); 53810858Sandreas.sandberg@arm.com // Enter into KVM and complete pending IO instructions if we 53910858Sandreas.sandberg@arm.com // have an instruction event pending. 54010858Sandreas.sandberg@arm.com const Tick ticksToExecute( 54110858Sandreas.sandberg@arm.com nextInstEvent > ctrInsts ? 54210858Sandreas.sandberg@arm.com curEventQueue()->nextTick() - curTick() : 0); 5439753Sandreas@sandberg.pp.se 5449753Sandreas@sandberg.pp.se // We might need to update the KVM state. 5459753Sandreas@sandberg.pp.se syncKvmState(); 5469753Sandreas@sandberg.pp.se 5479892Sandreas@sandberg.pp.se // Setup any pending instruction count breakpoints using 54810858Sandreas.sandberg@arm.com // PerfEvent if we are going to execute more than just an IO 54910858Sandreas.sandberg@arm.com // completion. 55010858Sandreas.sandberg@arm.com if (ticksToExecute > 0) 55110858Sandreas.sandberg@arm.com setupInstStop(); 5529892Sandreas@sandberg.pp.se 5539753Sandreas@sandberg.pp.se DPRINTF(KvmRun, "Entering KVM...\n"); 5549753Sandreas@sandberg.pp.se if (drainManager) { 5559753Sandreas@sandberg.pp.se // Force an immediate exit from KVM after completing 5569753Sandreas@sandberg.pp.se // pending operations. The architecture-specific code 5579753Sandreas@sandberg.pp.se // takes care to run until it is in a state where it can 5589753Sandreas@sandberg.pp.se // safely be drained. 5599753Sandreas@sandberg.pp.se delay = kvmRunDrain(); 5609753Sandreas@sandberg.pp.se } else { 5619753Sandreas@sandberg.pp.se delay = kvmRun(ticksToExecute); 5629753Sandreas@sandberg.pp.se } 5639753Sandreas@sandberg.pp.se 56410112Sandreas@sandberg.pp.se // The CPU might have been suspended before entering into 56510112Sandreas@sandberg.pp.se // KVM. Assume that the CPU was suspended /before/ entering 56610112Sandreas@sandberg.pp.se // into KVM and skip the exit handling. 56710112Sandreas@sandberg.pp.se if (_status == Idle) 56810112Sandreas@sandberg.pp.se break; 56910112Sandreas@sandberg.pp.se 5709753Sandreas@sandberg.pp.se // Entering into KVM implies that we'll have to reload the thread 5719753Sandreas@sandberg.pp.se // context from KVM if we want to access it. Flag the KVM state as 5729753Sandreas@sandberg.pp.se // dirty with respect to the cached thread context. 5739753Sandreas@sandberg.pp.se kvmStateDirty = true; 5749753Sandreas@sandberg.pp.se 5759753Sandreas@sandberg.pp.se // Enter into the RunningService state unless the 5769753Sandreas@sandberg.pp.se // simulation was stopped by a timer. 5779755Sandreas@sandberg.pp.se if (_kvmRun->exit_reason != KVM_EXIT_INTR) { 5789753Sandreas@sandberg.pp.se _status = RunningService; 5799755Sandreas@sandberg.pp.se } else { 5809755Sandreas@sandberg.pp.se ++numExitSignal; 5819753Sandreas@sandberg.pp.se _status = Running; 5829755Sandreas@sandberg.pp.se } 5839753Sandreas@sandberg.pp.se 5849904Sandreas@sandberg.pp.se // Service any pending instruction events. The vCPU should 5859904Sandreas@sandberg.pp.se // have exited in time for the event using the instruction 5869904Sandreas@sandberg.pp.se // counter configured by setupInstStop(). 5879904Sandreas@sandberg.pp.se comInstEventQueue[0]->serviceEvents(ctrInsts); 5889904Sandreas@sandberg.pp.se system->instEventQueue.serviceEvents(system->totalNumInsts); 5899904Sandreas@sandberg.pp.se 5909753Sandreas@sandberg.pp.se if (tryDrain()) 5919753Sandreas@sandberg.pp.se _status = Idle; 5929753Sandreas@sandberg.pp.se } break; 5939753Sandreas@sandberg.pp.se 5949651SAndreas.Sandberg@ARM.com default: 5959753Sandreas@sandberg.pp.se panic("BaseKvmCPU entered tick() in an illegal state (%i)\n", 5969753Sandreas@sandberg.pp.se _status); 5979651SAndreas.Sandberg@ARM.com } 5989753Sandreas@sandberg.pp.se 5999753Sandreas@sandberg.pp.se // Schedule a new tick if we are still running 6009753Sandreas@sandberg.pp.se if (_status != Idle) 6019753Sandreas@sandberg.pp.se schedule(tickEvent, clockEdge(ticksToCycles(delay))); 6029753Sandreas@sandberg.pp.se} 6039753Sandreas@sandberg.pp.se 6049753Sandreas@sandberg.pp.seTick 6059753Sandreas@sandberg.pp.seBaseKvmCPU::kvmRunDrain() 6069753Sandreas@sandberg.pp.se{ 6079753Sandreas@sandberg.pp.se // By default, the only thing we need to drain is a pending IO 6089753Sandreas@sandberg.pp.se // operation which assumes that we are in the 6099753Sandreas@sandberg.pp.se // RunningServiceCompletion state. 6109753Sandreas@sandberg.pp.se assert(_status == RunningServiceCompletion); 6119753Sandreas@sandberg.pp.se 6129753Sandreas@sandberg.pp.se // Deliver the data from the pending IO operation and immediately 6139753Sandreas@sandberg.pp.se // exit. 6149753Sandreas@sandberg.pp.se return kvmRun(0); 6159651SAndreas.Sandberg@ARM.com} 6169651SAndreas.Sandberg@ARM.com 6179735Sandreas@sandberg.pp.seuint64_t 6189735Sandreas@sandberg.pp.seBaseKvmCPU::getHostCycles() const 6199735Sandreas@sandberg.pp.se{ 6209735Sandreas@sandberg.pp.se return hwCycles.read(); 6219735Sandreas@sandberg.pp.se} 6229735Sandreas@sandberg.pp.se 6239651SAndreas.Sandberg@ARM.comTick 6249651SAndreas.Sandberg@ARM.comBaseKvmCPU::kvmRun(Tick ticks) 6259651SAndreas.Sandberg@ARM.com{ 6269753Sandreas@sandberg.pp.se Tick ticksExecuted; 6279651SAndreas.Sandberg@ARM.com DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks); 6289655SAndreas.Sandberg@ARM.com 6299753Sandreas@sandberg.pp.se if (ticks == 0) { 6309753Sandreas@sandberg.pp.se // Settings ticks == 0 is a special case which causes an entry 6319753Sandreas@sandberg.pp.se // into KVM that finishes pending operations (e.g., IO) and 6329753Sandreas@sandberg.pp.se // then immediately exits. 6339753Sandreas@sandberg.pp.se DPRINTF(KvmRun, "KVM: Delivering IO without full guest entry\n"); 6349735Sandreas@sandberg.pp.se 6359755Sandreas@sandberg.pp.se ++numVMHalfEntries; 6369755Sandreas@sandberg.pp.se 63710114Sandreas@sandberg.pp.se // Send a KVM_KICK_SIGNAL to the vCPU thread (i.e., this 63810114Sandreas@sandberg.pp.se // thread). The KVM control signal is masked while executing 63910114Sandreas@sandberg.pp.se // in gem5 and gets unmasked temporarily as when entering 6409753Sandreas@sandberg.pp.se // KVM. See setSignalMask() and setupSignalHandler(). 64110114Sandreas@sandberg.pp.se kick(); 6429655SAndreas.Sandberg@ARM.com 64310114Sandreas@sandberg.pp.se // Start the vCPU. KVM will check for signals after completing 64410114Sandreas@sandberg.pp.se // pending operations (IO). Since the KVM_KICK_SIGNAL is 64510114Sandreas@sandberg.pp.se // pending, this forces an immediate exit to gem5 again. We 6469753Sandreas@sandberg.pp.se // don't bother to setup timers since this shouldn't actually 64710114Sandreas@sandberg.pp.se // execute any code (other than completing half-executed IO 64810114Sandreas@sandberg.pp.se // instructions) in the guest. 6499753Sandreas@sandberg.pp.se ioctlRun(); 6509753Sandreas@sandberg.pp.se 6519753Sandreas@sandberg.pp.se // We always execute at least one cycle to prevent the 6529753Sandreas@sandberg.pp.se // BaseKvmCPU::tick() to be rescheduled on the same tick 6539753Sandreas@sandberg.pp.se // twice. 6549753Sandreas@sandberg.pp.se ticksExecuted = clockPeriod(); 6559753Sandreas@sandberg.pp.se } else { 65610157Sandreas@sandberg.pp.se // This method is executed as a result of a tick event. That 65710157Sandreas@sandberg.pp.se // means that the event queue will be locked when entering the 65810157Sandreas@sandberg.pp.se // method. We temporarily unlock the event queue to allow 65910157Sandreas@sandberg.pp.se // other threads to steal control of this thread to inject 66010157Sandreas@sandberg.pp.se // interrupts. They will typically lock the queue and then 66110157Sandreas@sandberg.pp.se // force an exit from KVM by kicking the vCPU. 66210157Sandreas@sandberg.pp.se EventQueue::ScopedRelease release(curEventQueue()); 66310157Sandreas@sandberg.pp.se 6649753Sandreas@sandberg.pp.se if (ticks < runTimer->resolution()) { 6659753Sandreas@sandberg.pp.se DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n", 6669753Sandreas@sandberg.pp.se ticks, runTimer->resolution()); 6679753Sandreas@sandberg.pp.se ticks = runTimer->resolution(); 6689753Sandreas@sandberg.pp.se } 6699753Sandreas@sandberg.pp.se 6709753Sandreas@sandberg.pp.se // Get hardware statistics after synchronizing contexts. The KVM 6719753Sandreas@sandberg.pp.se // state update might affect guest cycle counters. 6729753Sandreas@sandberg.pp.se uint64_t baseCycles(getHostCycles()); 6739753Sandreas@sandberg.pp.se uint64_t baseInstrs(hwInstructions.read()); 6749753Sandreas@sandberg.pp.se 6759753Sandreas@sandberg.pp.se // Arm the run timer and start the cycle timer if it isn't 6769753Sandreas@sandberg.pp.se // controlled by the overflow timer. Starting/stopping the cycle 6779753Sandreas@sandberg.pp.se // timer automatically starts the other perf timers as they are in 6789753Sandreas@sandberg.pp.se // the same counter group. 6799753Sandreas@sandberg.pp.se runTimer->arm(ticks); 6809753Sandreas@sandberg.pp.se if (!perfControlledByTimer) 6819753Sandreas@sandberg.pp.se hwCycles.start(); 6829753Sandreas@sandberg.pp.se 6839753Sandreas@sandberg.pp.se ioctlRun(); 6849753Sandreas@sandberg.pp.se 6859753Sandreas@sandberg.pp.se runTimer->disarm(); 6869753Sandreas@sandberg.pp.se if (!perfControlledByTimer) 6879753Sandreas@sandberg.pp.se hwCycles.stop(); 6889753Sandreas@sandberg.pp.se 68910114Sandreas@sandberg.pp.se // The control signal may have been delivered after we exited 6909753Sandreas@sandberg.pp.se // from KVM. It will be pending in that case since it is 6919753Sandreas@sandberg.pp.se // masked when we aren't executing in KVM. Discard it to make 6929753Sandreas@sandberg.pp.se // sure we don't deliver it immediately next time we try to 6939753Sandreas@sandberg.pp.se // enter into KVM. 69410114Sandreas@sandberg.pp.se discardPendingSignal(KVM_KICK_SIGNAL); 6959753Sandreas@sandberg.pp.se 6969753Sandreas@sandberg.pp.se const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles); 6979753Sandreas@sandberg.pp.se const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor); 6989753Sandreas@sandberg.pp.se const uint64_t instsExecuted(hwInstructions.read() - baseInstrs); 6999753Sandreas@sandberg.pp.se ticksExecuted = runTimer->ticksFromHostCycles(hostCyclesExecuted); 7009753Sandreas@sandberg.pp.se 7019753Sandreas@sandberg.pp.se /* Update statistics */ 7029753Sandreas@sandberg.pp.se numCycles += simCyclesExecuted;; 7039753Sandreas@sandberg.pp.se numInsts += instsExecuted; 7049753Sandreas@sandberg.pp.se ctrInsts += instsExecuted; 7059753Sandreas@sandberg.pp.se system->totalNumInsts += instsExecuted; 7069753Sandreas@sandberg.pp.se 7079753Sandreas@sandberg.pp.se DPRINTF(KvmRun, 7089753Sandreas@sandberg.pp.se "KVM: Executed %i instructions in %i cycles " 7099753Sandreas@sandberg.pp.se "(%i ticks, sim cycles: %i).\n", 7109753Sandreas@sandberg.pp.se instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted); 7119651SAndreas.Sandberg@ARM.com } 7129655SAndreas.Sandberg@ARM.com 7139651SAndreas.Sandberg@ARM.com ++numVMExits; 7149651SAndreas.Sandberg@ARM.com 7159651SAndreas.Sandberg@ARM.com return ticksExecuted + flushCoalescedMMIO(); 7169651SAndreas.Sandberg@ARM.com} 7179651SAndreas.Sandberg@ARM.com 7189651SAndreas.Sandberg@ARM.comvoid 7199651SAndreas.Sandberg@ARM.comBaseKvmCPU::kvmNonMaskableInterrupt() 7209651SAndreas.Sandberg@ARM.com{ 7219651SAndreas.Sandberg@ARM.com ++numInterrupts; 7229651SAndreas.Sandberg@ARM.com if (ioctl(KVM_NMI) == -1) 7239651SAndreas.Sandberg@ARM.com panic("KVM: Failed to deliver NMI to virtual CPU\n"); 7249651SAndreas.Sandberg@ARM.com} 7259651SAndreas.Sandberg@ARM.com 7269651SAndreas.Sandberg@ARM.comvoid 7279651SAndreas.Sandberg@ARM.comBaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt) 7289651SAndreas.Sandberg@ARM.com{ 7299651SAndreas.Sandberg@ARM.com ++numInterrupts; 7309651SAndreas.Sandberg@ARM.com if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1) 7319651SAndreas.Sandberg@ARM.com panic("KVM: Failed to deliver interrupt to virtual CPU\n"); 7329651SAndreas.Sandberg@ARM.com} 7339651SAndreas.Sandberg@ARM.com 7349651SAndreas.Sandberg@ARM.comvoid 7359651SAndreas.Sandberg@ARM.comBaseKvmCPU::getRegisters(struct kvm_regs ®s) const 7369651SAndreas.Sandberg@ARM.com{ 7379651SAndreas.Sandberg@ARM.com if (ioctl(KVM_GET_REGS, ®s) == -1) 7389651SAndreas.Sandberg@ARM.com panic("KVM: Failed to get guest registers\n"); 7399651SAndreas.Sandberg@ARM.com} 7409651SAndreas.Sandberg@ARM.com 7419651SAndreas.Sandberg@ARM.comvoid 7429651SAndreas.Sandberg@ARM.comBaseKvmCPU::setRegisters(const struct kvm_regs ®s) 7439651SAndreas.Sandberg@ARM.com{ 7449651SAndreas.Sandberg@ARM.com if (ioctl(KVM_SET_REGS, (void *)®s) == -1) 7459651SAndreas.Sandberg@ARM.com panic("KVM: Failed to set guest registers\n"); 7469651SAndreas.Sandberg@ARM.com} 7479651SAndreas.Sandberg@ARM.com 7489651SAndreas.Sandberg@ARM.comvoid 7499651SAndreas.Sandberg@ARM.comBaseKvmCPU::getSpecialRegisters(struct kvm_sregs ®s) const 7509651SAndreas.Sandberg@ARM.com{ 7519651SAndreas.Sandberg@ARM.com if (ioctl(KVM_GET_SREGS, ®s) == -1) 7529651SAndreas.Sandberg@ARM.com panic("KVM: Failed to get guest special registers\n"); 7539651SAndreas.Sandberg@ARM.com} 7549651SAndreas.Sandberg@ARM.com 7559651SAndreas.Sandberg@ARM.comvoid 7569651SAndreas.Sandberg@ARM.comBaseKvmCPU::setSpecialRegisters(const struct kvm_sregs ®s) 7579651SAndreas.Sandberg@ARM.com{ 7589651SAndreas.Sandberg@ARM.com if (ioctl(KVM_SET_SREGS, (void *)®s) == -1) 7599651SAndreas.Sandberg@ARM.com panic("KVM: Failed to set guest special registers\n"); 7609651SAndreas.Sandberg@ARM.com} 7619651SAndreas.Sandberg@ARM.com 7629651SAndreas.Sandberg@ARM.comvoid 7639651SAndreas.Sandberg@ARM.comBaseKvmCPU::getFPUState(struct kvm_fpu &state) const 7649651SAndreas.Sandberg@ARM.com{ 7659651SAndreas.Sandberg@ARM.com if (ioctl(KVM_GET_FPU, &state) == -1) 7669651SAndreas.Sandberg@ARM.com panic("KVM: Failed to get guest FPU state\n"); 7679651SAndreas.Sandberg@ARM.com} 7689651SAndreas.Sandberg@ARM.com 7699651SAndreas.Sandberg@ARM.comvoid 7709651SAndreas.Sandberg@ARM.comBaseKvmCPU::setFPUState(const struct kvm_fpu &state) 7719651SAndreas.Sandberg@ARM.com{ 7729651SAndreas.Sandberg@ARM.com if (ioctl(KVM_SET_FPU, (void *)&state) == -1) 7739651SAndreas.Sandberg@ARM.com panic("KVM: Failed to set guest FPU state\n"); 7749651SAndreas.Sandberg@ARM.com} 7759651SAndreas.Sandberg@ARM.com 7769651SAndreas.Sandberg@ARM.com 7779651SAndreas.Sandberg@ARM.comvoid 7789651SAndreas.Sandberg@ARM.comBaseKvmCPU::setOneReg(uint64_t id, const void *addr) 7799651SAndreas.Sandberg@ARM.com{ 7809651SAndreas.Sandberg@ARM.com#ifdef KVM_SET_ONE_REG 7819651SAndreas.Sandberg@ARM.com struct kvm_one_reg reg; 7829651SAndreas.Sandberg@ARM.com reg.id = id; 7839651SAndreas.Sandberg@ARM.com reg.addr = (uint64_t)addr; 7849651SAndreas.Sandberg@ARM.com 7859651SAndreas.Sandberg@ARM.com if (ioctl(KVM_SET_ONE_REG, ®) == -1) { 7869651SAndreas.Sandberg@ARM.com panic("KVM: Failed to set register (0x%x) value (errno: %i)\n", 7879651SAndreas.Sandberg@ARM.com id, errno); 7889651SAndreas.Sandberg@ARM.com } 7899651SAndreas.Sandberg@ARM.com#else 7909651SAndreas.Sandberg@ARM.com panic("KVM_SET_ONE_REG is unsupported on this platform.\n"); 7919651SAndreas.Sandberg@ARM.com#endif 7929651SAndreas.Sandberg@ARM.com} 7939651SAndreas.Sandberg@ARM.com 7949651SAndreas.Sandberg@ARM.comvoid 7959651SAndreas.Sandberg@ARM.comBaseKvmCPU::getOneReg(uint64_t id, void *addr) const 7969651SAndreas.Sandberg@ARM.com{ 7979651SAndreas.Sandberg@ARM.com#ifdef KVM_GET_ONE_REG 7989651SAndreas.Sandberg@ARM.com struct kvm_one_reg reg; 7999651SAndreas.Sandberg@ARM.com reg.id = id; 8009651SAndreas.Sandberg@ARM.com reg.addr = (uint64_t)addr; 8019651SAndreas.Sandberg@ARM.com 8029651SAndreas.Sandberg@ARM.com if (ioctl(KVM_GET_ONE_REG, ®) == -1) { 8039651SAndreas.Sandberg@ARM.com panic("KVM: Failed to get register (0x%x) value (errno: %i)\n", 8049651SAndreas.Sandberg@ARM.com id, errno); 8059651SAndreas.Sandberg@ARM.com } 8069651SAndreas.Sandberg@ARM.com#else 8079651SAndreas.Sandberg@ARM.com panic("KVM_GET_ONE_REG is unsupported on this platform.\n"); 8089651SAndreas.Sandberg@ARM.com#endif 8099651SAndreas.Sandberg@ARM.com} 8109651SAndreas.Sandberg@ARM.com 8119651SAndreas.Sandberg@ARM.comstd::string 8129651SAndreas.Sandberg@ARM.comBaseKvmCPU::getAndFormatOneReg(uint64_t id) const 8139651SAndreas.Sandberg@ARM.com{ 8149651SAndreas.Sandberg@ARM.com#ifdef KVM_GET_ONE_REG 8159651SAndreas.Sandberg@ARM.com std::ostringstream ss; 8169651SAndreas.Sandberg@ARM.com 8179651SAndreas.Sandberg@ARM.com ss.setf(std::ios::hex, std::ios::basefield); 8189651SAndreas.Sandberg@ARM.com ss.setf(std::ios::showbase); 8199651SAndreas.Sandberg@ARM.com#define HANDLE_INTTYPE(len) \ 8209651SAndreas.Sandberg@ARM.com case KVM_REG_SIZE_U ## len: { \ 8219651SAndreas.Sandberg@ARM.com uint ## len ## _t value; \ 8229651SAndreas.Sandberg@ARM.com getOneReg(id, &value); \ 8239651SAndreas.Sandberg@ARM.com ss << value; \ 8249651SAndreas.Sandberg@ARM.com } break 8259651SAndreas.Sandberg@ARM.com 82610843Sandreas.sandberg@arm.com#define HANDLE_ARRAY(len) \ 82710843Sandreas.sandberg@arm.com case KVM_REG_SIZE_U ## len: { \ 82810843Sandreas.sandberg@arm.com uint8_t value[len / 8]; \ 82910843Sandreas.sandberg@arm.com getOneReg(id, value); \ 83010843Sandreas.sandberg@arm.com ccprintf(ss, "[0x%x", value[0]); \ 83110843Sandreas.sandberg@arm.com for (int i = 1; i < len / 8; ++i) \ 83210843Sandreas.sandberg@arm.com ccprintf(ss, ", 0x%x", value[i]); \ 83310843Sandreas.sandberg@arm.com ccprintf(ss, "]"); \ 8349651SAndreas.Sandberg@ARM.com } break 8359651SAndreas.Sandberg@ARM.com 8369651SAndreas.Sandberg@ARM.com switch (id & KVM_REG_SIZE_MASK) { 8379651SAndreas.Sandberg@ARM.com HANDLE_INTTYPE(8); 8389651SAndreas.Sandberg@ARM.com HANDLE_INTTYPE(16); 8399651SAndreas.Sandberg@ARM.com HANDLE_INTTYPE(32); 8409651SAndreas.Sandberg@ARM.com HANDLE_INTTYPE(64); 8419651SAndreas.Sandberg@ARM.com HANDLE_ARRAY(128); 8429651SAndreas.Sandberg@ARM.com HANDLE_ARRAY(256); 8439651SAndreas.Sandberg@ARM.com HANDLE_ARRAY(512); 8449651SAndreas.Sandberg@ARM.com HANDLE_ARRAY(1024); 8459651SAndreas.Sandberg@ARM.com default: 8469651SAndreas.Sandberg@ARM.com ss << "??"; 8479651SAndreas.Sandberg@ARM.com } 8489651SAndreas.Sandberg@ARM.com 8499651SAndreas.Sandberg@ARM.com#undef HANDLE_INTTYPE 8509651SAndreas.Sandberg@ARM.com#undef HANDLE_ARRAY 8519651SAndreas.Sandberg@ARM.com 8529651SAndreas.Sandberg@ARM.com return ss.str(); 8539651SAndreas.Sandberg@ARM.com#else 8549651SAndreas.Sandberg@ARM.com panic("KVM_GET_ONE_REG is unsupported on this platform.\n"); 8559651SAndreas.Sandberg@ARM.com#endif 8569651SAndreas.Sandberg@ARM.com} 8579651SAndreas.Sandberg@ARM.com 8589652SAndreas.Sandberg@ARM.comvoid 8599652SAndreas.Sandberg@ARM.comBaseKvmCPU::syncThreadContext() 8609652SAndreas.Sandberg@ARM.com{ 8619652SAndreas.Sandberg@ARM.com if (!kvmStateDirty) 8629652SAndreas.Sandberg@ARM.com return; 8639652SAndreas.Sandberg@ARM.com 8649652SAndreas.Sandberg@ARM.com assert(!threadContextDirty); 8659652SAndreas.Sandberg@ARM.com 8669652SAndreas.Sandberg@ARM.com updateThreadContext(); 8679652SAndreas.Sandberg@ARM.com kvmStateDirty = false; 8689652SAndreas.Sandberg@ARM.com} 8699652SAndreas.Sandberg@ARM.com 8709652SAndreas.Sandberg@ARM.comvoid 8719652SAndreas.Sandberg@ARM.comBaseKvmCPU::syncKvmState() 8729652SAndreas.Sandberg@ARM.com{ 8739652SAndreas.Sandberg@ARM.com if (!threadContextDirty) 8749652SAndreas.Sandberg@ARM.com return; 8759652SAndreas.Sandberg@ARM.com 8769652SAndreas.Sandberg@ARM.com assert(!kvmStateDirty); 8779652SAndreas.Sandberg@ARM.com 8789652SAndreas.Sandberg@ARM.com updateKvmState(); 8799652SAndreas.Sandberg@ARM.com threadContextDirty = false; 8809652SAndreas.Sandberg@ARM.com} 8819652SAndreas.Sandberg@ARM.com 8829651SAndreas.Sandberg@ARM.comTick 8839651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExit() 8849651SAndreas.Sandberg@ARM.com{ 8859651SAndreas.Sandberg@ARM.com DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason); 8869753Sandreas@sandberg.pp.se assert(_status == RunningService); 8879651SAndreas.Sandberg@ARM.com 8889753Sandreas@sandberg.pp.se // Switch into the running state by default. Individual handlers 8899753Sandreas@sandberg.pp.se // can override this. 8909753Sandreas@sandberg.pp.se _status = Running; 8919651SAndreas.Sandberg@ARM.com switch (_kvmRun->exit_reason) { 8929651SAndreas.Sandberg@ARM.com case KVM_EXIT_UNKNOWN: 8939651SAndreas.Sandberg@ARM.com return handleKvmExitUnknown(); 8949651SAndreas.Sandberg@ARM.com 8959651SAndreas.Sandberg@ARM.com case KVM_EXIT_EXCEPTION: 8969651SAndreas.Sandberg@ARM.com return handleKvmExitException(); 8979651SAndreas.Sandberg@ARM.com 8989651SAndreas.Sandberg@ARM.com case KVM_EXIT_IO: 8999753Sandreas@sandberg.pp.se _status = RunningServiceCompletion; 9009651SAndreas.Sandberg@ARM.com ++numIO; 9019651SAndreas.Sandberg@ARM.com return handleKvmExitIO(); 9029651SAndreas.Sandberg@ARM.com 9039651SAndreas.Sandberg@ARM.com case KVM_EXIT_HYPERCALL: 9049651SAndreas.Sandberg@ARM.com ++numHypercalls; 9059651SAndreas.Sandberg@ARM.com return handleKvmExitHypercall(); 9069651SAndreas.Sandberg@ARM.com 9079651SAndreas.Sandberg@ARM.com case KVM_EXIT_HLT: 9089651SAndreas.Sandberg@ARM.com /* The guest has halted and is waiting for interrupts */ 9099651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "handleKvmExitHalt\n"); 9109651SAndreas.Sandberg@ARM.com ++numHalt; 9119651SAndreas.Sandberg@ARM.com 9129651SAndreas.Sandberg@ARM.com // Suspend the thread until the next interrupt arrives 9139651SAndreas.Sandberg@ARM.com thread->suspend(); 9149651SAndreas.Sandberg@ARM.com 9159651SAndreas.Sandberg@ARM.com // This is actually ignored since the thread is suspended. 9169651SAndreas.Sandberg@ARM.com return 0; 9179651SAndreas.Sandberg@ARM.com 9189651SAndreas.Sandberg@ARM.com case KVM_EXIT_MMIO: 9199753Sandreas@sandberg.pp.se _status = RunningServiceCompletion; 9209651SAndreas.Sandberg@ARM.com /* Service memory mapped IO requests */ 9219651SAndreas.Sandberg@ARM.com DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n", 9229651SAndreas.Sandberg@ARM.com _kvmRun->mmio.is_write, 9239651SAndreas.Sandberg@ARM.com _kvmRun->mmio.phys_addr, _kvmRun->mmio.len); 9249651SAndreas.Sandberg@ARM.com 9259651SAndreas.Sandberg@ARM.com ++numMMIO; 9269651SAndreas.Sandberg@ARM.com return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data, 9279651SAndreas.Sandberg@ARM.com _kvmRun->mmio.len, _kvmRun->mmio.is_write); 9289651SAndreas.Sandberg@ARM.com 9299651SAndreas.Sandberg@ARM.com case KVM_EXIT_IRQ_WINDOW_OPEN: 9309651SAndreas.Sandberg@ARM.com return handleKvmExitIRQWindowOpen(); 9319651SAndreas.Sandberg@ARM.com 9329651SAndreas.Sandberg@ARM.com case KVM_EXIT_FAIL_ENTRY: 9339651SAndreas.Sandberg@ARM.com return handleKvmExitFailEntry(); 9349651SAndreas.Sandberg@ARM.com 9359651SAndreas.Sandberg@ARM.com case KVM_EXIT_INTR: 9369651SAndreas.Sandberg@ARM.com /* KVM was interrupted by a signal, restart it in the next 9379651SAndreas.Sandberg@ARM.com * tick. */ 9389651SAndreas.Sandberg@ARM.com return 0; 9399651SAndreas.Sandberg@ARM.com 9409651SAndreas.Sandberg@ARM.com case KVM_EXIT_INTERNAL_ERROR: 9419651SAndreas.Sandberg@ARM.com panic("KVM: Internal error (suberror: %u)\n", 9429651SAndreas.Sandberg@ARM.com _kvmRun->internal.suberror); 9439651SAndreas.Sandberg@ARM.com 9449651SAndreas.Sandberg@ARM.com default: 9459689Sandreas@sandberg.pp.se dump(); 9469651SAndreas.Sandberg@ARM.com panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason); 9479651SAndreas.Sandberg@ARM.com } 9489651SAndreas.Sandberg@ARM.com} 9499651SAndreas.Sandberg@ARM.com 9509651SAndreas.Sandberg@ARM.comTick 9519651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitIO() 9529651SAndreas.Sandberg@ARM.com{ 9539651SAndreas.Sandberg@ARM.com panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n", 9549651SAndreas.Sandberg@ARM.com _kvmRun->io.direction, _kvmRun->io.size, 9559651SAndreas.Sandberg@ARM.com _kvmRun->io.port, _kvmRun->io.count); 9569651SAndreas.Sandberg@ARM.com} 9579651SAndreas.Sandberg@ARM.com 9589651SAndreas.Sandberg@ARM.comTick 9599651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitHypercall() 9609651SAndreas.Sandberg@ARM.com{ 9619651SAndreas.Sandberg@ARM.com panic("KVM: Unhandled hypercall\n"); 9629651SAndreas.Sandberg@ARM.com} 9639651SAndreas.Sandberg@ARM.com 9649651SAndreas.Sandberg@ARM.comTick 9659651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitIRQWindowOpen() 9669651SAndreas.Sandberg@ARM.com{ 9679651SAndreas.Sandberg@ARM.com warn("KVM: Unhandled IRQ window.\n"); 9689651SAndreas.Sandberg@ARM.com return 0; 9699651SAndreas.Sandberg@ARM.com} 9709651SAndreas.Sandberg@ARM.com 9719651SAndreas.Sandberg@ARM.com 9729651SAndreas.Sandberg@ARM.comTick 9739651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitUnknown() 9749651SAndreas.Sandberg@ARM.com{ 9759689Sandreas@sandberg.pp.se dump(); 9769651SAndreas.Sandberg@ARM.com panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n", 9779651SAndreas.Sandberg@ARM.com _kvmRun->hw.hardware_exit_reason); 9789651SAndreas.Sandberg@ARM.com} 9799651SAndreas.Sandberg@ARM.com 9809651SAndreas.Sandberg@ARM.comTick 9819651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitException() 9829651SAndreas.Sandberg@ARM.com{ 9839689Sandreas@sandberg.pp.se dump(); 9849651SAndreas.Sandberg@ARM.com panic("KVM: Got exception when starting vCPU " 9859651SAndreas.Sandberg@ARM.com "(exception: %u, error_code: %u)\n", 9869651SAndreas.Sandberg@ARM.com _kvmRun->ex.exception, _kvmRun->ex.error_code); 9879651SAndreas.Sandberg@ARM.com} 9889651SAndreas.Sandberg@ARM.com 9899651SAndreas.Sandberg@ARM.comTick 9909651SAndreas.Sandberg@ARM.comBaseKvmCPU::handleKvmExitFailEntry() 9919651SAndreas.Sandberg@ARM.com{ 9929689Sandreas@sandberg.pp.se dump(); 9939651SAndreas.Sandberg@ARM.com panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n", 9949651SAndreas.Sandberg@ARM.com _kvmRun->fail_entry.hardware_entry_failure_reason); 9959651SAndreas.Sandberg@ARM.com} 9969651SAndreas.Sandberg@ARM.com 9979651SAndreas.Sandberg@ARM.comTick 9989651SAndreas.Sandberg@ARM.comBaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write) 9999651SAndreas.Sandberg@ARM.com{ 10009760Sandreas@sandberg.pp.se ThreadContext *tc(thread->getTC()); 10019760Sandreas@sandberg.pp.se syncThreadContext(); 10029760Sandreas@sandberg.pp.se 100310653Sandreas.hansson@arm.com Request mmio_req(paddr, size, Request::UNCACHEABLE, dataMasterId()); 100410653Sandreas.hansson@arm.com mmio_req.setThreadContext(tc->contextId(), 0); 10059760Sandreas@sandberg.pp.se // Some architectures do need to massage physical addresses a bit 10069760Sandreas@sandberg.pp.se // before they are inserted into the memory system. This enables 10079760Sandreas@sandberg.pp.se // APIC accesses on x86 and m5ops where supported through a MMIO 10089760Sandreas@sandberg.pp.se // interface. 10099760Sandreas@sandberg.pp.se BaseTLB::Mode tlb_mode(write ? BaseTLB::Write : BaseTLB::Read); 10109760Sandreas@sandberg.pp.se Fault fault(tc->getDTBPtr()->finalizePhysical(&mmio_req, tc, tlb_mode)); 10119760Sandreas@sandberg.pp.se if (fault != NoFault) 10129760Sandreas@sandberg.pp.se warn("Finalization of MMIO address failed: %s\n", fault->name()); 10139760Sandreas@sandberg.pp.se 10149651SAndreas.Sandberg@ARM.com 10159651SAndreas.Sandberg@ARM.com const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq); 10169651SAndreas.Sandberg@ARM.com Packet pkt(&mmio_req, cmd); 10179651SAndreas.Sandberg@ARM.com pkt.dataStatic(data); 10189760Sandreas@sandberg.pp.se 10199760Sandreas@sandberg.pp.se if (mmio_req.isMmappedIpr()) { 102010157Sandreas@sandberg.pp.se // We currently assume that there is no need to migrate to a 102110157Sandreas@sandberg.pp.se // different event queue when doing IPRs. Currently, IPRs are 102210157Sandreas@sandberg.pp.se // only used for m5ops, so it should be a valid assumption. 10239882Sandreas@sandberg.pp.se const Cycles ipr_delay(write ? 10249882Sandreas@sandberg.pp.se TheISA::handleIprWrite(tc, &pkt) : 10259882Sandreas@sandberg.pp.se TheISA::handleIprRead(tc, &pkt)); 102610553Salexandru.dutu@amd.com threadContextDirty = true; 10279925Sandreas@sandberg.pp.se return clockPeriod() * ipr_delay; 10289760Sandreas@sandberg.pp.se } else { 102910157Sandreas@sandberg.pp.se // Temporarily lock and migrate to the event queue of the 103010157Sandreas@sandberg.pp.se // VM. This queue is assumed to "own" all devices we need to 103110157Sandreas@sandberg.pp.se // access if running in multi-core mode. 103210157Sandreas@sandberg.pp.se EventQueue::ScopedMigration migrate(vm.eventQueue()); 103310157Sandreas@sandberg.pp.se 10349760Sandreas@sandberg.pp.se return dataPort.sendAtomic(&pkt); 10359760Sandreas@sandberg.pp.se } 10369651SAndreas.Sandberg@ARM.com} 10379651SAndreas.Sandberg@ARM.com 10389753Sandreas@sandberg.pp.sevoid 10399753Sandreas@sandberg.pp.seBaseKvmCPU::setSignalMask(const sigset_t *mask) 10409753Sandreas@sandberg.pp.se{ 10419753Sandreas@sandberg.pp.se std::unique_ptr<struct kvm_signal_mask> kvm_mask; 10429753Sandreas@sandberg.pp.se 10439753Sandreas@sandberg.pp.se if (mask) { 10449753Sandreas@sandberg.pp.se kvm_mask.reset((struct kvm_signal_mask *)operator new( 10459753Sandreas@sandberg.pp.se sizeof(struct kvm_signal_mask) + sizeof(*mask))); 10469753Sandreas@sandberg.pp.se // The kernel and the user-space headers have different ideas 10479753Sandreas@sandberg.pp.se // about the size of sigset_t. This seems like a massive hack, 10489753Sandreas@sandberg.pp.se // but is actually what qemu does. 10499753Sandreas@sandberg.pp.se assert(sizeof(*mask) >= 8); 10509753Sandreas@sandberg.pp.se kvm_mask->len = 8; 10519753Sandreas@sandberg.pp.se memcpy(kvm_mask->sigset, mask, kvm_mask->len); 10529753Sandreas@sandberg.pp.se } 10539753Sandreas@sandberg.pp.se 10549753Sandreas@sandberg.pp.se if (ioctl(KVM_SET_SIGNAL_MASK, (void *)kvm_mask.get()) == -1) 10559753Sandreas@sandberg.pp.se panic("KVM: Failed to set vCPU signal mask (errno: %i)\n", 10569753Sandreas@sandberg.pp.se errno); 10579753Sandreas@sandberg.pp.se} 10589753Sandreas@sandberg.pp.se 10599651SAndreas.Sandberg@ARM.comint 10609651SAndreas.Sandberg@ARM.comBaseKvmCPU::ioctl(int request, long p1) const 10619651SAndreas.Sandberg@ARM.com{ 10629651SAndreas.Sandberg@ARM.com if (vcpuFD == -1) 10639651SAndreas.Sandberg@ARM.com panic("KVM: CPU ioctl called before initialization\n"); 10649651SAndreas.Sandberg@ARM.com 10659651SAndreas.Sandberg@ARM.com return ::ioctl(vcpuFD, request, p1); 10669651SAndreas.Sandberg@ARM.com} 10679651SAndreas.Sandberg@ARM.com 10689651SAndreas.Sandberg@ARM.comTick 10699651SAndreas.Sandberg@ARM.comBaseKvmCPU::flushCoalescedMMIO() 10709651SAndreas.Sandberg@ARM.com{ 10719651SAndreas.Sandberg@ARM.com if (!mmioRing) 10729651SAndreas.Sandberg@ARM.com return 0; 10739651SAndreas.Sandberg@ARM.com 10749651SAndreas.Sandberg@ARM.com DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n"); 10759651SAndreas.Sandberg@ARM.com 10769651SAndreas.Sandberg@ARM.com // TODO: We might need to do synchronization when we start to 10779651SAndreas.Sandberg@ARM.com // support multiple CPUs 10789651SAndreas.Sandberg@ARM.com Tick ticks(0); 10799651SAndreas.Sandberg@ARM.com while (mmioRing->first != mmioRing->last) { 10809651SAndreas.Sandberg@ARM.com struct kvm_coalesced_mmio &ent( 10819651SAndreas.Sandberg@ARM.com mmioRing->coalesced_mmio[mmioRing->first]); 10829651SAndreas.Sandberg@ARM.com 10839651SAndreas.Sandberg@ARM.com DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n", 10849651SAndreas.Sandberg@ARM.com ent.phys_addr, ent.len); 10859651SAndreas.Sandberg@ARM.com 10869651SAndreas.Sandberg@ARM.com ++numCoalescedMMIO; 10879651SAndreas.Sandberg@ARM.com ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true); 10889651SAndreas.Sandberg@ARM.com 10899651SAndreas.Sandberg@ARM.com mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX; 10909651SAndreas.Sandberg@ARM.com } 10919651SAndreas.Sandberg@ARM.com 10929651SAndreas.Sandberg@ARM.com return ticks; 10939651SAndreas.Sandberg@ARM.com} 10949651SAndreas.Sandberg@ARM.com 10959892Sandreas@sandberg.pp.se/** 109610114Sandreas@sandberg.pp.se * Dummy handler for KVM kick signals. 109710073Sandreas@sandberg.pp.se * 109810114Sandreas@sandberg.pp.se * @note This function is usually not called since the kernel doesn't 109910073Sandreas@sandberg.pp.se * seem to deliver signals when the signal is only unmasked when 110010073Sandreas@sandberg.pp.se * running in KVM. This doesn't matter though since we are only 110110073Sandreas@sandberg.pp.se * interested in getting KVM to exit, which happens as expected. See 110210073Sandreas@sandberg.pp.se * setupSignalHandler() and kvmRun() for details about KVM signal 110310073Sandreas@sandberg.pp.se * handling. 11049892Sandreas@sandberg.pp.se */ 11059892Sandreas@sandberg.pp.sestatic void 110610114Sandreas@sandberg.pp.seonKickSignal(int signo, siginfo_t *si, void *data) 11079892Sandreas@sandberg.pp.se{ 11089892Sandreas@sandberg.pp.se} 11099892Sandreas@sandberg.pp.se 11109651SAndreas.Sandberg@ARM.comvoid 11119651SAndreas.Sandberg@ARM.comBaseKvmCPU::setupSignalHandler() 11129651SAndreas.Sandberg@ARM.com{ 11139651SAndreas.Sandberg@ARM.com struct sigaction sa; 11149651SAndreas.Sandberg@ARM.com 11159651SAndreas.Sandberg@ARM.com memset(&sa, 0, sizeof(sa)); 111610114Sandreas@sandberg.pp.se sa.sa_sigaction = onKickSignal; 11179651SAndreas.Sandberg@ARM.com sa.sa_flags = SA_SIGINFO | SA_RESTART; 111810114Sandreas@sandberg.pp.se if (sigaction(KVM_KICK_SIGNAL, &sa, NULL) == -1) 11199892Sandreas@sandberg.pp.se panic("KVM: Failed to setup vCPU timer signal handler\n"); 11209892Sandreas@sandberg.pp.se 11219753Sandreas@sandberg.pp.se sigset_t sigset; 112210073Sandreas@sandberg.pp.se if (pthread_sigmask(SIG_BLOCK, NULL, &sigset) == -1) 11239753Sandreas@sandberg.pp.se panic("KVM: Failed get signal mask\n"); 11249753Sandreas@sandberg.pp.se 11259753Sandreas@sandberg.pp.se // Request KVM to setup the same signal mask as we're currently 112610114Sandreas@sandberg.pp.se // running with except for the KVM control signal. We'll sometimes 112710114Sandreas@sandberg.pp.se // need to raise the KVM_KICK_SIGNAL to cause immediate exits from 112810114Sandreas@sandberg.pp.se // KVM after servicing IO requests. See kvmRun(). 112910114Sandreas@sandberg.pp.se sigdelset(&sigset, KVM_KICK_SIGNAL); 11309753Sandreas@sandberg.pp.se setSignalMask(&sigset); 11319753Sandreas@sandberg.pp.se 11329892Sandreas@sandberg.pp.se // Mask our control signals so they aren't delivered unless we're 11339753Sandreas@sandberg.pp.se // actually executing inside KVM. 113410114Sandreas@sandberg.pp.se sigaddset(&sigset, KVM_KICK_SIGNAL); 113510073Sandreas@sandberg.pp.se if (pthread_sigmask(SIG_SETMASK, &sigset, NULL) == -1) 11369892Sandreas@sandberg.pp.se panic("KVM: Failed mask the KVM control signals\n"); 11379753Sandreas@sandberg.pp.se} 11389753Sandreas@sandberg.pp.se 11399753Sandreas@sandberg.pp.sebool 11409753Sandreas@sandberg.pp.seBaseKvmCPU::discardPendingSignal(int signum) const 11419753Sandreas@sandberg.pp.se{ 11429753Sandreas@sandberg.pp.se int discardedSignal; 11439753Sandreas@sandberg.pp.se 11449753Sandreas@sandberg.pp.se // Setting the timeout to zero causes sigtimedwait to return 11459753Sandreas@sandberg.pp.se // immediately. 11469753Sandreas@sandberg.pp.se struct timespec timeout; 11479753Sandreas@sandberg.pp.se timeout.tv_sec = 0; 11489753Sandreas@sandberg.pp.se timeout.tv_nsec = 0; 11499753Sandreas@sandberg.pp.se 11509753Sandreas@sandberg.pp.se sigset_t sigset; 11519753Sandreas@sandberg.pp.se sigemptyset(&sigset); 11529753Sandreas@sandberg.pp.se sigaddset(&sigset, signum); 11539753Sandreas@sandberg.pp.se 11549753Sandreas@sandberg.pp.se do { 11559753Sandreas@sandberg.pp.se discardedSignal = sigtimedwait(&sigset, NULL, &timeout); 11569753Sandreas@sandberg.pp.se } while (discardedSignal == -1 && errno == EINTR); 11579753Sandreas@sandberg.pp.se 11589753Sandreas@sandberg.pp.se if (discardedSignal == signum) 11599753Sandreas@sandberg.pp.se return true; 11609753Sandreas@sandberg.pp.se else if (discardedSignal == -1 && errno == EAGAIN) 11619753Sandreas@sandberg.pp.se return false; 11629753Sandreas@sandberg.pp.se else 11639753Sandreas@sandberg.pp.se panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n", 11649753Sandreas@sandberg.pp.se discardedSignal, errno); 11659651SAndreas.Sandberg@ARM.com} 11669651SAndreas.Sandberg@ARM.com 11679651SAndreas.Sandberg@ARM.comvoid 11689651SAndreas.Sandberg@ARM.comBaseKvmCPU::setupCounters() 11699651SAndreas.Sandberg@ARM.com{ 11709651SAndreas.Sandberg@ARM.com DPRINTF(Kvm, "Attaching cycle counter...\n"); 11719651SAndreas.Sandberg@ARM.com PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE, 11729651SAndreas.Sandberg@ARM.com PERF_COUNT_HW_CPU_CYCLES); 11739651SAndreas.Sandberg@ARM.com cfgCycles.disabled(true) 11749651SAndreas.Sandberg@ARM.com .pinned(true); 11759655SAndreas.Sandberg@ARM.com 11769986Sandreas@sandberg.pp.se // Try to exclude the host. We set both exclude_hv and 11779986Sandreas@sandberg.pp.se // exclude_host since different architectures use slightly 11789986Sandreas@sandberg.pp.se // different APIs in the kernel. 11799986Sandreas@sandberg.pp.se cfgCycles.exclude_hv(true) 11809986Sandreas@sandberg.pp.se .exclude_host(true); 11819986Sandreas@sandberg.pp.se 11829655SAndreas.Sandberg@ARM.com if (perfControlledByTimer) { 11839655SAndreas.Sandberg@ARM.com // We need to configure the cycles counter to send overflows 11849655SAndreas.Sandberg@ARM.com // since we are going to use it to trigger timer signals that 11859655SAndreas.Sandberg@ARM.com // trap back into m5 from KVM. In practice, this means that we 11869655SAndreas.Sandberg@ARM.com // need to set some non-zero sample period that gets 11879655SAndreas.Sandberg@ARM.com // overridden when the timer is armed. 11889655SAndreas.Sandberg@ARM.com cfgCycles.wakeupEvents(1) 11899655SAndreas.Sandberg@ARM.com .samplePeriod(42); 11909655SAndreas.Sandberg@ARM.com } 11919655SAndreas.Sandberg@ARM.com 11929651SAndreas.Sandberg@ARM.com hwCycles.attach(cfgCycles, 11939651SAndreas.Sandberg@ARM.com 0); // TID (0 => currentThread) 11949651SAndreas.Sandberg@ARM.com 11959892Sandreas@sandberg.pp.se setupInstCounter(); 11969651SAndreas.Sandberg@ARM.com} 11979753Sandreas@sandberg.pp.se 11989753Sandreas@sandberg.pp.sebool 11999753Sandreas@sandberg.pp.seBaseKvmCPU::tryDrain() 12009753Sandreas@sandberg.pp.se{ 12019753Sandreas@sandberg.pp.se if (!drainManager) 12029753Sandreas@sandberg.pp.se return false; 12039753Sandreas@sandberg.pp.se 12049753Sandreas@sandberg.pp.se if (!archIsDrained()) { 12059753Sandreas@sandberg.pp.se DPRINTF(Drain, "tryDrain: Architecture code is not ready.\n"); 12069753Sandreas@sandberg.pp.se return false; 12079753Sandreas@sandberg.pp.se } 12089753Sandreas@sandberg.pp.se 12099753Sandreas@sandberg.pp.se if (_status == Idle || _status == Running) { 12109753Sandreas@sandberg.pp.se DPRINTF(Drain, 12119753Sandreas@sandberg.pp.se "tryDrain: CPU transitioned into the Idle state, drain done\n"); 12129753Sandreas@sandberg.pp.se drainManager->signalDrainDone(); 12139753Sandreas@sandberg.pp.se drainManager = NULL; 12149753Sandreas@sandberg.pp.se return true; 12159753Sandreas@sandberg.pp.se } else { 12169753Sandreas@sandberg.pp.se DPRINTF(Drain, "tryDrain: CPU not ready.\n"); 12179753Sandreas@sandberg.pp.se return false; 12189753Sandreas@sandberg.pp.se } 12199753Sandreas@sandberg.pp.se} 12209753Sandreas@sandberg.pp.se 12219753Sandreas@sandberg.pp.sevoid 12229753Sandreas@sandberg.pp.seBaseKvmCPU::ioctlRun() 12239753Sandreas@sandberg.pp.se{ 12249753Sandreas@sandberg.pp.se if (ioctl(KVM_RUN) == -1) { 12259753Sandreas@sandberg.pp.se if (errno != EINTR) 12269753Sandreas@sandberg.pp.se panic("KVM: Failed to start virtual CPU (errno: %i)\n", 12279753Sandreas@sandberg.pp.se errno); 12289753Sandreas@sandberg.pp.se } 12299753Sandreas@sandberg.pp.se} 12309892Sandreas@sandberg.pp.se 12319892Sandreas@sandberg.pp.sevoid 12329892Sandreas@sandberg.pp.seBaseKvmCPU::setupInstStop() 12339892Sandreas@sandberg.pp.se{ 12349892Sandreas@sandberg.pp.se if (comInstEventQueue[0]->empty()) { 12359892Sandreas@sandberg.pp.se setupInstCounter(0); 12369892Sandreas@sandberg.pp.se } else { 12379892Sandreas@sandberg.pp.se const uint64_t next(comInstEventQueue[0]->nextTick()); 12389892Sandreas@sandberg.pp.se 12399892Sandreas@sandberg.pp.se assert(next > ctrInsts); 12409892Sandreas@sandberg.pp.se setupInstCounter(next - ctrInsts); 12419892Sandreas@sandberg.pp.se } 12429892Sandreas@sandberg.pp.se} 12439892Sandreas@sandberg.pp.se 12449892Sandreas@sandberg.pp.sevoid 12459892Sandreas@sandberg.pp.seBaseKvmCPU::setupInstCounter(uint64_t period) 12469892Sandreas@sandberg.pp.se{ 12479892Sandreas@sandberg.pp.se // No need to do anything if we aren't attaching for the first 12489892Sandreas@sandberg.pp.se // time or the period isn't changing. 12499892Sandreas@sandberg.pp.se if (period == activeInstPeriod && hwInstructions.attached()) 12509892Sandreas@sandberg.pp.se return; 12519892Sandreas@sandberg.pp.se 12529892Sandreas@sandberg.pp.se PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE, 12539892Sandreas@sandberg.pp.se PERF_COUNT_HW_INSTRUCTIONS); 12549892Sandreas@sandberg.pp.se 12559986Sandreas@sandberg.pp.se // Try to exclude the host. We set both exclude_hv and 12569986Sandreas@sandberg.pp.se // exclude_host since different architectures use slightly 12579986Sandreas@sandberg.pp.se // different APIs in the kernel. 12589986Sandreas@sandberg.pp.se cfgInstructions.exclude_hv(true) 12599986Sandreas@sandberg.pp.se .exclude_host(true); 12609986Sandreas@sandberg.pp.se 12619892Sandreas@sandberg.pp.se if (period) { 12629892Sandreas@sandberg.pp.se // Setup a sampling counter if that has been requested. 12639892Sandreas@sandberg.pp.se cfgInstructions.wakeupEvents(1) 12649892Sandreas@sandberg.pp.se .samplePeriod(period); 12659892Sandreas@sandberg.pp.se } 12669892Sandreas@sandberg.pp.se 12679892Sandreas@sandberg.pp.se // We need to detach and re-attach the counter to reliably change 12689892Sandreas@sandberg.pp.se // sampling settings. See PerfKvmCounter::period() for details. 12699892Sandreas@sandberg.pp.se if (hwInstructions.attached()) 12709892Sandreas@sandberg.pp.se hwInstructions.detach(); 12719892Sandreas@sandberg.pp.se assert(hwCycles.attached()); 12729892Sandreas@sandberg.pp.se hwInstructions.attach(cfgInstructions, 12739892Sandreas@sandberg.pp.se 0, // TID (0 => currentThread) 12749892Sandreas@sandberg.pp.se hwCycles); 12759892Sandreas@sandberg.pp.se 12769892Sandreas@sandberg.pp.se if (period) 127710114Sandreas@sandberg.pp.se hwInstructions.enableSignals(KVM_KICK_SIGNAL); 12789892Sandreas@sandberg.pp.se 12799892Sandreas@sandberg.pp.se activeInstPeriod = period; 12809892Sandreas@sandberg.pp.se} 1281