base.cc revision 12154:9a9bc3c1b788
16019Shines@cs.fsu.edu/* 27093Sgblack@eecs.umich.edu * Copyright (c) 2012, 2015 ARM Limited 37093Sgblack@eecs.umich.edu * All rights reserved 47093Sgblack@eecs.umich.edu * 57093Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall 67093Sgblack@eecs.umich.edu * not be construed as granting a license to any other intellectual 77093Sgblack@eecs.umich.edu * property including but not limited to intellectual property relating 87093Sgblack@eecs.umich.edu * to a hardware implementation of the functionality of the software 97093Sgblack@eecs.umich.edu * licensed hereunder. You may use the software subject to the license 107093Sgblack@eecs.umich.edu * terms below provided that you ensure that this notice is replicated 117093Sgblack@eecs.umich.edu * unmodified and in its entirety in all distributions of the software, 127093Sgblack@eecs.umich.edu * modified or unmodified, in source code or in binary form. 137093Sgblack@eecs.umich.edu * 146019Shines@cs.fsu.edu * Redistribution and use in source and binary forms, with or without 156019Shines@cs.fsu.edu * modification, are permitted provided that the following conditions are 166019Shines@cs.fsu.edu * met: redistributions of source code must retain the above copyright 176019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer; 186019Shines@cs.fsu.edu * redistributions in binary form must reproduce the above copyright 196019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer in the 206019Shines@cs.fsu.edu * documentation and/or other materials provided with the distribution; 216019Shines@cs.fsu.edu * neither the name of the copyright holders nor the names of its 226019Shines@cs.fsu.edu * contributors may be used to endorse or promote products derived from 236019Shines@cs.fsu.edu * this software without specific prior written permission. 246019Shines@cs.fsu.edu * 256019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 266019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 276019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 286019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 296019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 306019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 316019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 326019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 336019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 346019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 356019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 366019Shines@cs.fsu.edu * 376019Shines@cs.fsu.edu * Authors: Andreas Sandberg 386019Shines@cs.fsu.edu */ 396019Shines@cs.fsu.edu 406019Shines@cs.fsu.edu#include "cpu/kvm/base.hh" 416735Sgblack@eecs.umich.edu 426735Sgblack@eecs.umich.edu#include <linux/kvm.h> 436019Shines@cs.fsu.edu#include <sys/ioctl.h> 446019Shines@cs.fsu.edu#include <sys/mman.h> 456019Shines@cs.fsu.edu#include <unistd.h> 468229Snate@binkert.org 478229Snate@binkert.org#include <cerrno> 486019Shines@cs.fsu.edu#include <csignal> 498232Snate@binkert.org#include <ostream> 506019Shines@cs.fsu.edu 516019Shines@cs.fsu.edu#include "arch/mmapped_ipr.hh" 526019Shines@cs.fsu.edu#include "arch/utility.hh" 536019Shines@cs.fsu.edu#include "debug/Checkpoint.hh" 547362Sgblack@eecs.umich.edu#include "debug/Drain.hh" 556735Sgblack@eecs.umich.edu#include "debug/Kvm.hh" 566019Shines@cs.fsu.edu#include "debug/KvmIO.hh" 577362Sgblack@eecs.umich.edu#include "debug/KvmRun.hh" 586735Sgblack@eecs.umich.edu#include "params/BaseKvmCPU.hh" 596019Shines@cs.fsu.edu#include "sim/process.hh" 607362Sgblack@eecs.umich.edu#include "sim/system.hh" 616735Sgblack@eecs.umich.edu 626019Shines@cs.fsu.edu/* Used by some KVM macros */ 637362Sgblack@eecs.umich.edu#define PAGE_SIZE pageSize 646735Sgblack@eecs.umich.edu 656019Shines@cs.fsu.eduBaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params) 667362Sgblack@eecs.umich.edu : BaseCPU(params), 676735Sgblack@eecs.umich.edu vm(*params->system->getKvmVM()), 686019Shines@cs.fsu.edu _status(Idle), 697362Sgblack@eecs.umich.edu dataPort(name() + ".dcache_port", this), 706735Sgblack@eecs.umich.edu instPort(name() + ".icache_port", this), 716019Shines@cs.fsu.edu alwaysSyncTC(params->alwaysSyncTC), 727362Sgblack@eecs.umich.edu threadContextDirty(true), 736735Sgblack@eecs.umich.edu kvmStateDirty(false), 746019Shines@cs.fsu.edu vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0), 757652Sminkyu.jeong@arm.com _kvmRun(NULL), mmioRing(NULL), 767652Sminkyu.jeong@arm.com pageSize(sysconf(_SC_PAGE_SIZE)), 777652Sminkyu.jeong@arm.com tickEvent([this]{ tick(); }, "BaseKvmCPU tick", 788202SAli.Saidi@ARM.com false, Event::CPU_Tick_Pri), 798202SAli.Saidi@ARM.com activeInstPeriod(0), 808202SAli.Saidi@ARM.com perfControlledByTimer(params->usePerfOverflow), 816735Sgblack@eecs.umich.edu hostFactor(params->hostFactor), 827362Sgblack@eecs.umich.edu ctrInsts(0) 836735Sgblack@eecs.umich.edu{ 846735Sgblack@eecs.umich.edu if (pageSize == -1) 856019Shines@cs.fsu.edu panic("KVM: Failed to determine host page size (%i)\n", 866735Sgblack@eecs.umich.edu errno); 877400SAli.Saidi@ARM.com 886735Sgblack@eecs.umich.edu if (FullSystem) 896735Sgblack@eecs.umich.edu thread = new SimpleThread(this, 0, params->system, params->itb, params->dtb, 906735Sgblack@eecs.umich.edu params->isa[0]); 917400SAli.Saidi@ARM.com else 926735Sgblack@eecs.umich.edu thread = new SimpleThread(this, /* thread_num */ 0, params->system, 936735Sgblack@eecs.umich.edu params->workload[0], params->itb, 946735Sgblack@eecs.umich.edu params->dtb, params->isa[0]); 956019Shines@cs.fsu.edu 966019Shines@cs.fsu.edu thread->setStatus(ThreadContext::Halted); 976019Shines@cs.fsu.edu tc = thread->getTC(); 986735Sgblack@eecs.umich.edu threadContexts.push_back(tc); 996735Sgblack@eecs.umich.edu} 1006735Sgblack@eecs.umich.edu 1017678Sgblack@eecs.umich.eduBaseKvmCPU::~BaseKvmCPU() 1026019Shines@cs.fsu.edu{ 1036735Sgblack@eecs.umich.edu if (_kvmRun) 1046735Sgblack@eecs.umich.edu munmap(_kvmRun, vcpuMMapSize); 1056735Sgblack@eecs.umich.edu close(vcpuFD); 1066019Shines@cs.fsu.edu} 1076735Sgblack@eecs.umich.edu 1086735Sgblack@eecs.umich.eduvoid 1096735Sgblack@eecs.umich.eduBaseKvmCPU::init() 1106735Sgblack@eecs.umich.edu{ 1117720Sgblack@eecs.umich.edu BaseCPU::init(); 1128205SAli.Saidi@ARM.com 1138205SAli.Saidi@ARM.com if (numThreads != 1) 1148205SAli.Saidi@ARM.com fatal("KVM: Multithreading not supported"); 1156735Sgblack@eecs.umich.edu 1166735Sgblack@eecs.umich.edu tc->initMemProxies(tc); 1176735Sgblack@eecs.umich.edu 1186735Sgblack@eecs.umich.edu // initialize CPU, including PC 1196735Sgblack@eecs.umich.edu if (FullSystem && !switchedOut()) 1207093Sgblack@eecs.umich.edu TheISA::initCPU(tc, tc->contextId()); 1216735Sgblack@eecs.umich.edu} 1226735Sgblack@eecs.umich.edu 1236735Sgblack@eecs.umich.eduvoid 1247302Sgblack@eecs.umich.eduBaseKvmCPU::startup() 1256735Sgblack@eecs.umich.edu{ 1267720Sgblack@eecs.umich.edu const BaseKvmCPUParams * const p( 1276735Sgblack@eecs.umich.edu dynamic_cast<const BaseKvmCPUParams *>(params())); 1286735Sgblack@eecs.umich.edu 1296735Sgblack@eecs.umich.edu Kvm &kvm(*vm.kvm); 1306735Sgblack@eecs.umich.edu 1316735Sgblack@eecs.umich.edu BaseCPU::startup(); 1326735Sgblack@eecs.umich.edu 1336735Sgblack@eecs.umich.edu assert(vcpuFD == -1); 1346735Sgblack@eecs.umich.edu 1356735Sgblack@eecs.umich.edu // Tell the VM that a CPU is about to start. 1366735Sgblack@eecs.umich.edu vm.cpuStartup(); 1376735Sgblack@eecs.umich.edu 1386735Sgblack@eecs.umich.edu // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are 1396735Sgblack@eecs.umich.edu // not guaranteed that the parent KVM VM has initialized at that 1406735Sgblack@eecs.umich.edu // point. Initialize virtual CPUs here instead. 1416735Sgblack@eecs.umich.edu vcpuFD = vm.createVCPU(vcpuID); 1426735Sgblack@eecs.umich.edu 1436735Sgblack@eecs.umich.edu // Map the KVM run structure */ 1446735Sgblack@eecs.umich.edu vcpuMMapSize = kvm.getVCPUMMapSize(); 1456735Sgblack@eecs.umich.edu _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize, 1466735Sgblack@eecs.umich.edu PROT_READ | PROT_WRITE, MAP_SHARED, 1477093Sgblack@eecs.umich.edu vcpuFD, 0); 1487093Sgblack@eecs.umich.edu if (_kvmRun == MAP_FAILED) 1497720Sgblack@eecs.umich.edu panic("KVM: Failed to map run data structure\n"); 1507585SAli.Saidi@arm.com 1517720Sgblack@eecs.umich.edu // Setup a pointer to the MMIO ring buffer if coalesced MMIO is 1527720Sgblack@eecs.umich.edu // available. The offset into the KVM's communication page is 1537720Sgblack@eecs.umich.edu // provided by the coalesced MMIO capability. 1547720Sgblack@eecs.umich.edu int mmioOffset(kvm.capCoalescedMMIO()); 1557720Sgblack@eecs.umich.edu if (!p->useCoalescedMMIO) { 1567720Sgblack@eecs.umich.edu inform("KVM: Coalesced MMIO disabled by config.\n"); 1577720Sgblack@eecs.umich.edu } else if (mmioOffset) { 1586019Shines@cs.fsu.edu inform("KVM: Coalesced IO available\n"); 1597189Sgblack@eecs.umich.edu mmioRing = (struct kvm_coalesced_mmio_ring *)( 1607400SAli.Saidi@ARM.com (char *)_kvmRun + (mmioOffset * pageSize)); 1617678Sgblack@eecs.umich.edu } else { 1627400SAli.Saidi@ARM.com inform("KVM: Coalesced not supported by host OS\n"); 1637400SAli.Saidi@ARM.com } 1647400SAli.Saidi@ARM.com 1658205SAli.Saidi@ARM.com thread->startup(); 1667400SAli.Saidi@ARM.com 1677400SAli.Saidi@ARM.com Event *startupEvent( 1687189Sgblack@eecs.umich.edu new EventFunctionWrapper([this]{ startupThread(); }, name(), true)); 1697189Sgblack@eecs.umich.edu schedule(startupEvent, curTick()); 1707189Sgblack@eecs.umich.edu} 1717678Sgblack@eecs.umich.edu 1727189Sgblack@eecs.umich.eduBaseKvmCPU::Status 1737640Sgblack@eecs.umich.eduBaseKvmCPU::KVMCpuPort::nextIOState() const 1747189Sgblack@eecs.umich.edu{ 1757640Sgblack@eecs.umich.edu return (activeMMIOReqs || pendingMMIOPkts.size()) 1767640Sgblack@eecs.umich.edu ? RunningMMIOPending : RunningServiceCompletion; 1777640Sgblack@eecs.umich.edu} 1787640Sgblack@eecs.umich.edu 1797426Sgblack@eecs.umich.eduTick 1807426Sgblack@eecs.umich.eduBaseKvmCPU::KVMCpuPort::submitIO(PacketPtr pkt) 1817189Sgblack@eecs.umich.edu{ 1827426Sgblack@eecs.umich.edu if (cpu->system->isAtomicMode()) { 1837426Sgblack@eecs.umich.edu Tick delay = sendAtomic(pkt); 1847189Sgblack@eecs.umich.edu delete pkt->req; 1857189Sgblack@eecs.umich.edu delete pkt; 1867189Sgblack@eecs.umich.edu return delay; 1877197Sgblack@eecs.umich.edu } else { 1887678Sgblack@eecs.umich.edu if (pendingMMIOPkts.empty() && sendTimingReq(pkt)) { 1897197Sgblack@eecs.umich.edu activeMMIOReqs++; 1907197Sgblack@eecs.umich.edu } else { 1917197Sgblack@eecs.umich.edu pendingMMIOPkts.push(pkt); 1927197Sgblack@eecs.umich.edu } 1938063SAli.Saidi@ARM.com // Return value is irrelevant for timing-mode accesses. 1947197Sgblack@eecs.umich.edu return 0; 1957197Sgblack@eecs.umich.edu } 1967197Sgblack@eecs.umich.edu} 1977720Sgblack@eecs.umich.edu 1987720Sgblack@eecs.umich.edubool 1997720Sgblack@eecs.umich.eduBaseKvmCPU::KVMCpuPort::recvTimingResp(PacketPtr pkt) 2007720Sgblack@eecs.umich.edu{ 2017197Sgblack@eecs.umich.edu DPRINTF(KvmIO, "KVM: Finished timing request\n"); 2027197Sgblack@eecs.umich.edu 2036019Shines@cs.fsu.edu delete pkt->req; 2046019Shines@cs.fsu.edu delete pkt; 2057362Sgblack@eecs.umich.edu activeMMIOReqs--; 2067362Sgblack@eecs.umich.edu 2077678Sgblack@eecs.umich.edu // We can switch back into KVM when all pending and in-flight MMIO 2087362Sgblack@eecs.umich.edu // operations have completed. 2098205SAli.Saidi@ARM.com if (!(activeMMIOReqs || pendingMMIOPkts.size())) { 2107362Sgblack@eecs.umich.edu DPRINTF(KvmIO, "KVM: Finished all outstanding timing requests\n"); 2117362Sgblack@eecs.umich.edu cpu->finishMMIOPending(); 2127362Sgblack@eecs.umich.edu } 2137362Sgblack@eecs.umich.edu return true; 2147362Sgblack@eecs.umich.edu} 2157362Sgblack@eecs.umich.edu 2167362Sgblack@eecs.umich.eduvoid 2177362Sgblack@eecs.umich.eduBaseKvmCPU::KVMCpuPort::recvReqRetry() 2187362Sgblack@eecs.umich.edu{ 2197362Sgblack@eecs.umich.edu DPRINTF(KvmIO, "KVM: Retry for timing request\n"); 2207652Sminkyu.jeong@arm.com 2217678Sgblack@eecs.umich.edu assert(pendingMMIOPkts.size()); 2227652Sminkyu.jeong@arm.com 2237652Sminkyu.jeong@arm.com // Assuming that we can issue infinite requests this cycle is a bit 2247652Sminkyu.jeong@arm.com // unrealistic, but it's not worth modeling something more complex in 2257652Sminkyu.jeong@arm.com // KVM. 2267652Sminkyu.jeong@arm.com while (pendingMMIOPkts.size() && sendTimingReq(pendingMMIOPkts.front())) { 2277720Sgblack@eecs.umich.edu pendingMMIOPkts.pop(); 2287720Sgblack@eecs.umich.edu activeMMIOReqs++; 2297720Sgblack@eecs.umich.edu } 2307720Sgblack@eecs.umich.edu} 2317652Sminkyu.jeong@arm.com 2327652Sminkyu.jeong@arm.comvoid 2338202SAli.Saidi@ARM.comBaseKvmCPU::finishMMIOPending() 2348202SAli.Saidi@ARM.com{ 2358202SAli.Saidi@ARM.com assert(_status = RunningMMIOPending); 2368202SAli.Saidi@ARM.com assert(!tickEvent.scheduled()); 2378202SAli.Saidi@ARM.com 2388202SAli.Saidi@ARM.com _status = RunningServiceCompletion; 2398202SAli.Saidi@ARM.com schedule(tickEvent, nextCycle()); 2408202SAli.Saidi@ARM.com} 2418202SAli.Saidi@ARM.com 2428202SAli.Saidi@ARM.comvoid 2438202SAli.Saidi@ARM.comBaseKvmCPU::startupThread() 2447678Sgblack@eecs.umich.edu{ 2457678Sgblack@eecs.umich.edu // Do thread-specific initialization. We need to setup signal 2467678Sgblack@eecs.umich.edu // delivery for counters and timers from within the thread that 2477678Sgblack@eecs.umich.edu // will execute the event queue to ensure that signals are 2487362Sgblack@eecs.umich.edu // delivered to the right threads. 2496735Sgblack@eecs.umich.edu const BaseKvmCPUParams * const p( 2506019Shines@cs.fsu.edu dynamic_cast<const BaseKvmCPUParams *>(params())); 2516019Shines@cs.fsu.edu 252 vcpuThread = pthread_self(); 253 254 // Setup signal handlers. This has to be done after the vCPU is 255 // created since it manipulates the vCPU signal mask. 256 setupSignalHandler(); 257 258 setupCounters(); 259 260 if (p->usePerfOverflow) 261 runTimer.reset(new PerfKvmTimer(hwCycles, 262 KVM_KICK_SIGNAL, 263 p->hostFactor, 264 p->hostFreq)); 265 else 266 runTimer.reset(new PosixKvmTimer(KVM_KICK_SIGNAL, CLOCK_MONOTONIC, 267 p->hostFactor, 268 p->hostFreq)); 269 270} 271 272void 273BaseKvmCPU::regStats() 274{ 275 using namespace Stats; 276 277 BaseCPU::regStats(); 278 279 numInsts 280 .name(name() + ".committedInsts") 281 .desc("Number of instructions committed") 282 ; 283 284 numVMExits 285 .name(name() + ".numVMExits") 286 .desc("total number of KVM exits") 287 ; 288 289 numVMHalfEntries 290 .name(name() + ".numVMHalfEntries") 291 .desc("number of KVM entries to finalize pending operations") 292 ; 293 294 numExitSignal 295 .name(name() + ".numExitSignal") 296 .desc("exits due to signal delivery") 297 ; 298 299 numMMIO 300 .name(name() + ".numMMIO") 301 .desc("number of VM exits due to memory mapped IO") 302 ; 303 304 numCoalescedMMIO 305 .name(name() + ".numCoalescedMMIO") 306 .desc("number of coalesced memory mapped IO requests") 307 ; 308 309 numIO 310 .name(name() + ".numIO") 311 .desc("number of VM exits due to legacy IO") 312 ; 313 314 numHalt 315 .name(name() + ".numHalt") 316 .desc("number of VM exits due to wait for interrupt instructions") 317 ; 318 319 numInterrupts 320 .name(name() + ".numInterrupts") 321 .desc("number of interrupts delivered") 322 ; 323 324 numHypercalls 325 .name(name() + ".numHypercalls") 326 .desc("number of hypercalls") 327 ; 328} 329 330void 331BaseKvmCPU::serializeThread(CheckpointOut &cp, ThreadID tid) const 332{ 333 if (DTRACE(Checkpoint)) { 334 DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid); 335 dump(); 336 } 337 338 assert(tid == 0); 339 assert(_status == Idle); 340 thread->serialize(cp); 341} 342 343void 344BaseKvmCPU::unserializeThread(CheckpointIn &cp, ThreadID tid) 345{ 346 DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid); 347 348 assert(tid == 0); 349 assert(_status == Idle); 350 thread->unserialize(cp); 351 threadContextDirty = true; 352} 353 354DrainState 355BaseKvmCPU::drain() 356{ 357 if (switchedOut()) 358 return DrainState::Drained; 359 360 DPRINTF(Drain, "BaseKvmCPU::drain\n"); 361 362 // The event queue won't be locked when calling drain since that's 363 // not done from an event. Lock the event queue here to make sure 364 // that scoped migrations continue to work if we need to 365 // synchronize the thread context. 366 std::lock_guard<EventQueue> lock(*this->eventQueue()); 367 368 switch (_status) { 369 case Running: 370 // The base KVM code is normally ready when it is in the 371 // Running state, but the architecture specific code might be 372 // of a different opinion. This may happen when the CPU been 373 // notified of an event that hasn't been accepted by the vCPU 374 // yet. 375 if (!archIsDrained()) 376 return DrainState::Draining; 377 378 // The state of the CPU is consistent, so we don't need to do 379 // anything special to drain it. We simply de-schedule the 380 // tick event and enter the Idle state to prevent nasty things 381 // like MMIOs from happening. 382 if (tickEvent.scheduled()) 383 deschedule(tickEvent); 384 _status = Idle; 385 386 /** FALLTHROUGH */ 387 case Idle: 388 // Idle, no need to drain 389 assert(!tickEvent.scheduled()); 390 391 // Sync the thread context here since we'll need it when we 392 // switch CPUs or checkpoint the CPU. 393 syncThreadContext(); 394 395 return DrainState::Drained; 396 397 case RunningServiceCompletion: 398 // The CPU has just requested a service that was handled in 399 // the RunningService state, but the results have still not 400 // been reported to the CPU. Now, we /could/ probably just 401 // update the register state ourselves instead of letting KVM 402 // handle it, but that would be tricky. Instead, we enter KVM 403 // and let it do its stuff. 404 DPRINTF(Drain, "KVM CPU is waiting for service completion, " 405 "requesting drain.\n"); 406 return DrainState::Draining; 407 408 case RunningMMIOPending: 409 // We need to drain since there are in-flight timing accesses 410 DPRINTF(Drain, "KVM CPU is waiting for timing accesses to complete, " 411 "requesting drain.\n"); 412 return DrainState::Draining; 413 414 case RunningService: 415 // We need to drain since the CPU is waiting for service (e.g., MMIOs) 416 DPRINTF(Drain, "KVM CPU is waiting for service, requesting drain.\n"); 417 return DrainState::Draining; 418 419 default: 420 panic("KVM: Unhandled CPU state in drain()\n"); 421 return DrainState::Drained; 422 } 423} 424 425void 426BaseKvmCPU::drainResume() 427{ 428 assert(!tickEvent.scheduled()); 429 430 // We might have been switched out. In that case, we don't need to 431 // do anything. 432 if (switchedOut()) 433 return; 434 435 DPRINTF(Kvm, "drainResume\n"); 436 verifyMemoryMode(); 437 438 // The tick event is de-scheduled as a part of the draining 439 // process. Re-schedule it if the thread context is active. 440 if (tc->status() == ThreadContext::Active) { 441 schedule(tickEvent, nextCycle()); 442 _status = Running; 443 } else { 444 _status = Idle; 445 } 446} 447 448void 449BaseKvmCPU::notifyFork() 450{ 451 // We should have drained prior to forking, which means that the 452 // tick event shouldn't be scheduled and the CPU is idle. 453 assert(!tickEvent.scheduled()); 454 assert(_status == Idle); 455 456 if (vcpuFD != -1) { 457 if (close(vcpuFD) == -1) 458 warn("kvm CPU: notifyFork failed to close vcpuFD\n"); 459 460 if (_kvmRun) 461 munmap(_kvmRun, vcpuMMapSize); 462 463 vcpuFD = -1; 464 _kvmRun = NULL; 465 466 hwInstructions.detach(); 467 hwCycles.detach(); 468 } 469} 470 471void 472BaseKvmCPU::switchOut() 473{ 474 DPRINTF(Kvm, "switchOut\n"); 475 476 BaseCPU::switchOut(); 477 478 // We should have drained prior to executing a switchOut, which 479 // means that the tick event shouldn't be scheduled and the CPU is 480 // idle. 481 assert(!tickEvent.scheduled()); 482 assert(_status == Idle); 483} 484 485void 486BaseKvmCPU::takeOverFrom(BaseCPU *cpu) 487{ 488 DPRINTF(Kvm, "takeOverFrom\n"); 489 490 BaseCPU::takeOverFrom(cpu); 491 492 // We should have drained prior to executing a switchOut, which 493 // means that the tick event shouldn't be scheduled and the CPU is 494 // idle. 495 assert(!tickEvent.scheduled()); 496 assert(_status == Idle); 497 assert(threadContexts.size() == 1); 498 499 // Force an update of the KVM state here instead of flagging the 500 // TC as dirty. This is not ideal from a performance point of 501 // view, but it makes debugging easier as it allows meaningful KVM 502 // state to be dumped before and after a takeover. 503 updateKvmState(); 504 threadContextDirty = false; 505} 506 507void 508BaseKvmCPU::verifyMemoryMode() const 509{ 510 if (!(system->bypassCaches())) { 511 fatal("The KVM-based CPUs requires the memory system to be in the " 512 "'noncaching' mode.\n"); 513 } 514} 515 516void 517BaseKvmCPU::wakeup(ThreadID tid) 518{ 519 DPRINTF(Kvm, "wakeup()\n"); 520 // This method might have been called from another 521 // context. Migrate to this SimObject's event queue when 522 // delivering the wakeup signal. 523 EventQueue::ScopedMigration migrate(eventQueue()); 524 525 // Kick the vCPU to get it to come out of KVM. 526 kick(); 527 528 if (thread->status() != ThreadContext::Suspended) 529 return; 530 531 thread->activate(); 532} 533 534void 535BaseKvmCPU::activateContext(ThreadID thread_num) 536{ 537 DPRINTF(Kvm, "ActivateContext %d\n", thread_num); 538 539 assert(thread_num == 0); 540 assert(thread); 541 542 assert(_status == Idle); 543 assert(!tickEvent.scheduled()); 544 545 numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend); 546 547 schedule(tickEvent, clockEdge(Cycles(0))); 548 _status = Running; 549} 550 551 552void 553BaseKvmCPU::suspendContext(ThreadID thread_num) 554{ 555 DPRINTF(Kvm, "SuspendContext %d\n", thread_num); 556 557 assert(thread_num == 0); 558 assert(thread); 559 560 if (_status == Idle) 561 return; 562 563 assert(_status == Running || _status == RunningServiceCompletion); 564 565 // The tick event may no be scheduled if the quest has requested 566 // the monitor to wait for interrupts. The normal CPU models can 567 // get their tick events descheduled by quiesce instructions, but 568 // that can't happen here. 569 if (tickEvent.scheduled()) 570 deschedule(tickEvent); 571 572 _status = Idle; 573} 574 575void 576BaseKvmCPU::deallocateContext(ThreadID thread_num) 577{ 578 // for now, these are equivalent 579 suspendContext(thread_num); 580} 581 582void 583BaseKvmCPU::haltContext(ThreadID thread_num) 584{ 585 // for now, these are equivalent 586 suspendContext(thread_num); 587} 588 589ThreadContext * 590BaseKvmCPU::getContext(int tn) 591{ 592 assert(tn == 0); 593 syncThreadContext(); 594 return tc; 595} 596 597 598Counter 599BaseKvmCPU::totalInsts() const 600{ 601 return ctrInsts; 602} 603 604Counter 605BaseKvmCPU::totalOps() const 606{ 607 hack_once("Pretending totalOps is equivalent to totalInsts()\n"); 608 return ctrInsts; 609} 610 611void 612BaseKvmCPU::dump() const 613{ 614 inform("State dumping not implemented."); 615} 616 617void 618BaseKvmCPU::tick() 619{ 620 Tick delay(0); 621 assert(_status != Idle && _status != RunningMMIOPending); 622 623 switch (_status) { 624 case RunningService: 625 // handleKvmExit() will determine the next state of the CPU 626 delay = handleKvmExit(); 627 628 if (tryDrain()) 629 _status = Idle; 630 break; 631 632 case RunningServiceCompletion: 633 case Running: { 634 const uint64_t nextInstEvent( 635 !comInstEventQueue[0]->empty() ? 636 comInstEventQueue[0]->nextTick() : UINT64_MAX); 637 // Enter into KVM and complete pending IO instructions if we 638 // have an instruction event pending. 639 const Tick ticksToExecute( 640 nextInstEvent > ctrInsts ? 641 curEventQueue()->nextTick() - curTick() : 0); 642 643 if (alwaysSyncTC) 644 threadContextDirty = true; 645 646 // We might need to update the KVM state. 647 syncKvmState(); 648 649 // Setup any pending instruction count breakpoints using 650 // PerfEvent if we are going to execute more than just an IO 651 // completion. 652 if (ticksToExecute > 0) 653 setupInstStop(); 654 655 DPRINTF(KvmRun, "Entering KVM...\n"); 656 if (drainState() == DrainState::Draining) { 657 // Force an immediate exit from KVM after completing 658 // pending operations. The architecture-specific code 659 // takes care to run until it is in a state where it can 660 // safely be drained. 661 delay = kvmRunDrain(); 662 } else { 663 delay = kvmRun(ticksToExecute); 664 } 665 666 // The CPU might have been suspended before entering into 667 // KVM. Assume that the CPU was suspended /before/ entering 668 // into KVM and skip the exit handling. 669 if (_status == Idle) 670 break; 671 672 // Entering into KVM implies that we'll have to reload the thread 673 // context from KVM if we want to access it. Flag the KVM state as 674 // dirty with respect to the cached thread context. 675 kvmStateDirty = true; 676 677 if (alwaysSyncTC) 678 syncThreadContext(); 679 680 // Enter into the RunningService state unless the 681 // simulation was stopped by a timer. 682 if (_kvmRun->exit_reason != KVM_EXIT_INTR) { 683 _status = RunningService; 684 } else { 685 ++numExitSignal; 686 _status = Running; 687 } 688 689 // Service any pending instruction events. The vCPU should 690 // have exited in time for the event using the instruction 691 // counter configured by setupInstStop(). 692 comInstEventQueue[0]->serviceEvents(ctrInsts); 693 system->instEventQueue.serviceEvents(system->totalNumInsts); 694 695 if (tryDrain()) 696 _status = Idle; 697 } break; 698 699 default: 700 panic("BaseKvmCPU entered tick() in an illegal state (%i)\n", 701 _status); 702 } 703 704 // Schedule a new tick if we are still running 705 if (_status != Idle && _status != RunningMMIOPending) 706 schedule(tickEvent, clockEdge(ticksToCycles(delay))); 707} 708 709Tick 710BaseKvmCPU::kvmRunDrain() 711{ 712 // By default, the only thing we need to drain is a pending IO 713 // operation which assumes that we are in the 714 // RunningServiceCompletion or RunningMMIOPending state. 715 assert(_status == RunningServiceCompletion || 716 _status == RunningMMIOPending); 717 718 // Deliver the data from the pending IO operation and immediately 719 // exit. 720 return kvmRun(0); 721} 722 723uint64_t 724BaseKvmCPU::getHostCycles() const 725{ 726 return hwCycles.read(); 727} 728 729Tick 730BaseKvmCPU::kvmRun(Tick ticks) 731{ 732 Tick ticksExecuted; 733 fatal_if(vcpuFD == -1, 734 "Trying to run a KVM CPU in a forked child process. " 735 "This is not supported.\n"); 736 DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks); 737 738 if (ticks == 0) { 739 // Settings ticks == 0 is a special case which causes an entry 740 // into KVM that finishes pending operations (e.g., IO) and 741 // then immediately exits. 742 DPRINTF(KvmRun, "KVM: Delivering IO without full guest entry\n"); 743 744 ++numVMHalfEntries; 745 746 // Send a KVM_KICK_SIGNAL to the vCPU thread (i.e., this 747 // thread). The KVM control signal is masked while executing 748 // in gem5 and gets unmasked temporarily as when entering 749 // KVM. See setSignalMask() and setupSignalHandler(). 750 kick(); 751 752 // Start the vCPU. KVM will check for signals after completing 753 // pending operations (IO). Since the KVM_KICK_SIGNAL is 754 // pending, this forces an immediate exit to gem5 again. We 755 // don't bother to setup timers since this shouldn't actually 756 // execute any code (other than completing half-executed IO 757 // instructions) in the guest. 758 ioctlRun(); 759 760 // We always execute at least one cycle to prevent the 761 // BaseKvmCPU::tick() to be rescheduled on the same tick 762 // twice. 763 ticksExecuted = clockPeriod(); 764 } else { 765 // This method is executed as a result of a tick event. That 766 // means that the event queue will be locked when entering the 767 // method. We temporarily unlock the event queue to allow 768 // other threads to steal control of this thread to inject 769 // interrupts. They will typically lock the queue and then 770 // force an exit from KVM by kicking the vCPU. 771 EventQueue::ScopedRelease release(curEventQueue()); 772 773 if (ticks < runTimer->resolution()) { 774 DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n", 775 ticks, runTimer->resolution()); 776 ticks = runTimer->resolution(); 777 } 778 779 // Get hardware statistics after synchronizing contexts. The KVM 780 // state update might affect guest cycle counters. 781 uint64_t baseCycles(getHostCycles()); 782 uint64_t baseInstrs(hwInstructions.read()); 783 784 // Arm the run timer and start the cycle timer if it isn't 785 // controlled by the overflow timer. Starting/stopping the cycle 786 // timer automatically starts the other perf timers as they are in 787 // the same counter group. 788 runTimer->arm(ticks); 789 if (!perfControlledByTimer) 790 hwCycles.start(); 791 792 ioctlRun(); 793 794 runTimer->disarm(); 795 if (!perfControlledByTimer) 796 hwCycles.stop(); 797 798 // The control signal may have been delivered after we exited 799 // from KVM. It will be pending in that case since it is 800 // masked when we aren't executing in KVM. Discard it to make 801 // sure we don't deliver it immediately next time we try to 802 // enter into KVM. 803 discardPendingSignal(KVM_KICK_SIGNAL); 804 805 const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles); 806 const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor); 807 const uint64_t instsExecuted(hwInstructions.read() - baseInstrs); 808 ticksExecuted = runTimer->ticksFromHostCycles(hostCyclesExecuted); 809 810 /* Update statistics */ 811 numCycles += simCyclesExecuted;; 812 numInsts += instsExecuted; 813 ctrInsts += instsExecuted; 814 system->totalNumInsts += instsExecuted; 815 816 DPRINTF(KvmRun, 817 "KVM: Executed %i instructions in %i cycles " 818 "(%i ticks, sim cycles: %i).\n", 819 instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted); 820 } 821 822 ++numVMExits; 823 824 return ticksExecuted + flushCoalescedMMIO(); 825} 826 827void 828BaseKvmCPU::kvmNonMaskableInterrupt() 829{ 830 ++numInterrupts; 831 if (ioctl(KVM_NMI) == -1) 832 panic("KVM: Failed to deliver NMI to virtual CPU\n"); 833} 834 835void 836BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt) 837{ 838 ++numInterrupts; 839 if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1) 840 panic("KVM: Failed to deliver interrupt to virtual CPU\n"); 841} 842 843void 844BaseKvmCPU::getRegisters(struct kvm_regs ®s) const 845{ 846 if (ioctl(KVM_GET_REGS, ®s) == -1) 847 panic("KVM: Failed to get guest registers\n"); 848} 849 850void 851BaseKvmCPU::setRegisters(const struct kvm_regs ®s) 852{ 853 if (ioctl(KVM_SET_REGS, (void *)®s) == -1) 854 panic("KVM: Failed to set guest registers\n"); 855} 856 857void 858BaseKvmCPU::getSpecialRegisters(struct kvm_sregs ®s) const 859{ 860 if (ioctl(KVM_GET_SREGS, ®s) == -1) 861 panic("KVM: Failed to get guest special registers\n"); 862} 863 864void 865BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs ®s) 866{ 867 if (ioctl(KVM_SET_SREGS, (void *)®s) == -1) 868 panic("KVM: Failed to set guest special registers\n"); 869} 870 871void 872BaseKvmCPU::getFPUState(struct kvm_fpu &state) const 873{ 874 if (ioctl(KVM_GET_FPU, &state) == -1) 875 panic("KVM: Failed to get guest FPU state\n"); 876} 877 878void 879BaseKvmCPU::setFPUState(const struct kvm_fpu &state) 880{ 881 if (ioctl(KVM_SET_FPU, (void *)&state) == -1) 882 panic("KVM: Failed to set guest FPU state\n"); 883} 884 885 886void 887BaseKvmCPU::setOneReg(uint64_t id, const void *addr) 888{ 889#ifdef KVM_SET_ONE_REG 890 struct kvm_one_reg reg; 891 reg.id = id; 892 reg.addr = (uint64_t)addr; 893 894 if (ioctl(KVM_SET_ONE_REG, ®) == -1) { 895 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n", 896 id, errno); 897 } 898#else 899 panic("KVM_SET_ONE_REG is unsupported on this platform.\n"); 900#endif 901} 902 903void 904BaseKvmCPU::getOneReg(uint64_t id, void *addr) const 905{ 906#ifdef KVM_GET_ONE_REG 907 struct kvm_one_reg reg; 908 reg.id = id; 909 reg.addr = (uint64_t)addr; 910 911 if (ioctl(KVM_GET_ONE_REG, ®) == -1) { 912 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n", 913 id, errno); 914 } 915#else 916 panic("KVM_GET_ONE_REG is unsupported on this platform.\n"); 917#endif 918} 919 920std::string 921BaseKvmCPU::getAndFormatOneReg(uint64_t id) const 922{ 923#ifdef KVM_GET_ONE_REG 924 std::ostringstream ss; 925 926 ss.setf(std::ios::hex, std::ios::basefield); 927 ss.setf(std::ios::showbase); 928#define HANDLE_INTTYPE(len) \ 929 case KVM_REG_SIZE_U ## len: { \ 930 uint ## len ## _t value; \ 931 getOneReg(id, &value); \ 932 ss << value; \ 933 } break 934 935#define HANDLE_ARRAY(len) \ 936 case KVM_REG_SIZE_U ## len: { \ 937 uint8_t value[len / 8]; \ 938 getOneReg(id, value); \ 939 ccprintf(ss, "[0x%x", value[0]); \ 940 for (int i = 1; i < len / 8; ++i) \ 941 ccprintf(ss, ", 0x%x", value[i]); \ 942 ccprintf(ss, "]"); \ 943 } break 944 945 switch (id & KVM_REG_SIZE_MASK) { 946 HANDLE_INTTYPE(8); 947 HANDLE_INTTYPE(16); 948 HANDLE_INTTYPE(32); 949 HANDLE_INTTYPE(64); 950 HANDLE_ARRAY(128); 951 HANDLE_ARRAY(256); 952 HANDLE_ARRAY(512); 953 HANDLE_ARRAY(1024); 954 default: 955 ss << "??"; 956 } 957 958#undef HANDLE_INTTYPE 959#undef HANDLE_ARRAY 960 961 return ss.str(); 962#else 963 panic("KVM_GET_ONE_REG is unsupported on this platform.\n"); 964#endif 965} 966 967void 968BaseKvmCPU::syncThreadContext() 969{ 970 if (!kvmStateDirty) 971 return; 972 973 assert(!threadContextDirty); 974 975 updateThreadContext(); 976 kvmStateDirty = false; 977} 978 979void 980BaseKvmCPU::syncKvmState() 981{ 982 if (!threadContextDirty) 983 return; 984 985 assert(!kvmStateDirty); 986 987 updateKvmState(); 988 threadContextDirty = false; 989} 990 991Tick 992BaseKvmCPU::handleKvmExit() 993{ 994 DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason); 995 assert(_status == RunningService); 996 997 // Switch into the running state by default. Individual handlers 998 // can override this. 999 _status = Running; 1000 switch (_kvmRun->exit_reason) { 1001 case KVM_EXIT_UNKNOWN: 1002 return handleKvmExitUnknown(); 1003 1004 case KVM_EXIT_EXCEPTION: 1005 return handleKvmExitException(); 1006 1007 case KVM_EXIT_IO: 1008 { 1009 ++numIO; 1010 Tick ticks = handleKvmExitIO(); 1011 _status = dataPort.nextIOState(); 1012 return ticks; 1013 } 1014 1015 case KVM_EXIT_HYPERCALL: 1016 ++numHypercalls; 1017 return handleKvmExitHypercall(); 1018 1019 case KVM_EXIT_HLT: 1020 /* The guest has halted and is waiting for interrupts */ 1021 DPRINTF(Kvm, "handleKvmExitHalt\n"); 1022 ++numHalt; 1023 1024 // Suspend the thread until the next interrupt arrives 1025 thread->suspend(); 1026 1027 // This is actually ignored since the thread is suspended. 1028 return 0; 1029 1030 case KVM_EXIT_MMIO: 1031 { 1032 /* Service memory mapped IO requests */ 1033 DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n", 1034 _kvmRun->mmio.is_write, 1035 _kvmRun->mmio.phys_addr, _kvmRun->mmio.len); 1036 1037 ++numMMIO; 1038 Tick ticks = doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data, 1039 _kvmRun->mmio.len, _kvmRun->mmio.is_write); 1040 // doMMIOAccess could have triggered a suspend, in which case we don't 1041 // want to overwrite the _status. 1042 if (_status != Idle) 1043 _status = dataPort.nextIOState(); 1044 return ticks; 1045 } 1046 1047 case KVM_EXIT_IRQ_WINDOW_OPEN: 1048 return handleKvmExitIRQWindowOpen(); 1049 1050 case KVM_EXIT_FAIL_ENTRY: 1051 return handleKvmExitFailEntry(); 1052 1053 case KVM_EXIT_INTR: 1054 /* KVM was interrupted by a signal, restart it in the next 1055 * tick. */ 1056 return 0; 1057 1058 case KVM_EXIT_INTERNAL_ERROR: 1059 panic("KVM: Internal error (suberror: %u)\n", 1060 _kvmRun->internal.suberror); 1061 1062 default: 1063 dump(); 1064 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason); 1065 } 1066} 1067 1068Tick 1069BaseKvmCPU::handleKvmExitIO() 1070{ 1071 panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n", 1072 _kvmRun->io.direction, _kvmRun->io.size, 1073 _kvmRun->io.port, _kvmRun->io.count); 1074} 1075 1076Tick 1077BaseKvmCPU::handleKvmExitHypercall() 1078{ 1079 panic("KVM: Unhandled hypercall\n"); 1080} 1081 1082Tick 1083BaseKvmCPU::handleKvmExitIRQWindowOpen() 1084{ 1085 warn("KVM: Unhandled IRQ window.\n"); 1086 return 0; 1087} 1088 1089 1090Tick 1091BaseKvmCPU::handleKvmExitUnknown() 1092{ 1093 dump(); 1094 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n", 1095 _kvmRun->hw.hardware_exit_reason); 1096} 1097 1098Tick 1099BaseKvmCPU::handleKvmExitException() 1100{ 1101 dump(); 1102 panic("KVM: Got exception when starting vCPU " 1103 "(exception: %u, error_code: %u)\n", 1104 _kvmRun->ex.exception, _kvmRun->ex.error_code); 1105} 1106 1107Tick 1108BaseKvmCPU::handleKvmExitFailEntry() 1109{ 1110 dump(); 1111 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n", 1112 _kvmRun->fail_entry.hardware_entry_failure_reason); 1113} 1114 1115Tick 1116BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write) 1117{ 1118 ThreadContext *tc(thread->getTC()); 1119 syncThreadContext(); 1120 1121 RequestPtr mmio_req = new Request(paddr, size, Request::UNCACHEABLE, 1122 dataMasterId()); 1123 mmio_req->setContext(tc->contextId()); 1124 // Some architectures do need to massage physical addresses a bit 1125 // before they are inserted into the memory system. This enables 1126 // APIC accesses on x86 and m5ops where supported through a MMIO 1127 // interface. 1128 BaseTLB::Mode tlb_mode(write ? BaseTLB::Write : BaseTLB::Read); 1129 Fault fault(tc->getDTBPtr()->finalizePhysical(mmio_req, tc, tlb_mode)); 1130 if (fault != NoFault) 1131 warn("Finalization of MMIO address failed: %s\n", fault->name()); 1132 1133 1134 const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq); 1135 PacketPtr pkt = new Packet(mmio_req, cmd); 1136 pkt->dataStatic(data); 1137 1138 if (mmio_req->isMmappedIpr()) { 1139 // We currently assume that there is no need to migrate to a 1140 // different event queue when doing IPRs. Currently, IPRs are 1141 // only used for m5ops, so it should be a valid assumption. 1142 const Cycles ipr_delay(write ? 1143 TheISA::handleIprWrite(tc, pkt) : 1144 TheISA::handleIprRead(tc, pkt)); 1145 threadContextDirty = true; 1146 delete pkt->req; 1147 delete pkt; 1148 return clockPeriod() * ipr_delay; 1149 } else { 1150 // Temporarily lock and migrate to the event queue of the 1151 // VM. This queue is assumed to "own" all devices we need to 1152 // access if running in multi-core mode. 1153 EventQueue::ScopedMigration migrate(vm.eventQueue()); 1154 1155 return dataPort.submitIO(pkt); 1156 } 1157} 1158 1159void 1160BaseKvmCPU::setSignalMask(const sigset_t *mask) 1161{ 1162 std::unique_ptr<struct kvm_signal_mask> kvm_mask; 1163 1164 if (mask) { 1165 kvm_mask.reset((struct kvm_signal_mask *)operator new( 1166 sizeof(struct kvm_signal_mask) + sizeof(*mask))); 1167 // The kernel and the user-space headers have different ideas 1168 // about the size of sigset_t. This seems like a massive hack, 1169 // but is actually what qemu does. 1170 assert(sizeof(*mask) >= 8); 1171 kvm_mask->len = 8; 1172 memcpy(kvm_mask->sigset, mask, kvm_mask->len); 1173 } 1174 1175 if (ioctl(KVM_SET_SIGNAL_MASK, (void *)kvm_mask.get()) == -1) 1176 panic("KVM: Failed to set vCPU signal mask (errno: %i)\n", 1177 errno); 1178} 1179 1180int 1181BaseKvmCPU::ioctl(int request, long p1) const 1182{ 1183 if (vcpuFD == -1) 1184 panic("KVM: CPU ioctl called before initialization\n"); 1185 1186 return ::ioctl(vcpuFD, request, p1); 1187} 1188 1189Tick 1190BaseKvmCPU::flushCoalescedMMIO() 1191{ 1192 if (!mmioRing) 1193 return 0; 1194 1195 DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n"); 1196 1197 // TODO: We might need to do synchronization when we start to 1198 // support multiple CPUs 1199 Tick ticks(0); 1200 while (mmioRing->first != mmioRing->last) { 1201 struct kvm_coalesced_mmio &ent( 1202 mmioRing->coalesced_mmio[mmioRing->first]); 1203 1204 DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n", 1205 ent.phys_addr, ent.len); 1206 1207 ++numCoalescedMMIO; 1208 ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true); 1209 1210 mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX; 1211 } 1212 1213 return ticks; 1214} 1215 1216/** 1217 * Dummy handler for KVM kick signals. 1218 * 1219 * @note This function is usually not called since the kernel doesn't 1220 * seem to deliver signals when the signal is only unmasked when 1221 * running in KVM. This doesn't matter though since we are only 1222 * interested in getting KVM to exit, which happens as expected. See 1223 * setupSignalHandler() and kvmRun() for details about KVM signal 1224 * handling. 1225 */ 1226static void 1227onKickSignal(int signo, siginfo_t *si, void *data) 1228{ 1229} 1230 1231void 1232BaseKvmCPU::setupSignalHandler() 1233{ 1234 struct sigaction sa; 1235 1236 memset(&sa, 0, sizeof(sa)); 1237 sa.sa_sigaction = onKickSignal; 1238 sa.sa_flags = SA_SIGINFO | SA_RESTART; 1239 if (sigaction(KVM_KICK_SIGNAL, &sa, NULL) == -1) 1240 panic("KVM: Failed to setup vCPU timer signal handler\n"); 1241 1242 sigset_t sigset; 1243 if (pthread_sigmask(SIG_BLOCK, NULL, &sigset) == -1) 1244 panic("KVM: Failed get signal mask\n"); 1245 1246 // Request KVM to setup the same signal mask as we're currently 1247 // running with except for the KVM control signal. We'll sometimes 1248 // need to raise the KVM_KICK_SIGNAL to cause immediate exits from 1249 // KVM after servicing IO requests. See kvmRun(). 1250 sigdelset(&sigset, KVM_KICK_SIGNAL); 1251 setSignalMask(&sigset); 1252 1253 // Mask our control signals so they aren't delivered unless we're 1254 // actually executing inside KVM. 1255 sigaddset(&sigset, KVM_KICK_SIGNAL); 1256 if (pthread_sigmask(SIG_SETMASK, &sigset, NULL) == -1) 1257 panic("KVM: Failed mask the KVM control signals\n"); 1258} 1259 1260bool 1261BaseKvmCPU::discardPendingSignal(int signum) const 1262{ 1263 int discardedSignal; 1264 1265 // Setting the timeout to zero causes sigtimedwait to return 1266 // immediately. 1267 struct timespec timeout; 1268 timeout.tv_sec = 0; 1269 timeout.tv_nsec = 0; 1270 1271 sigset_t sigset; 1272 sigemptyset(&sigset); 1273 sigaddset(&sigset, signum); 1274 1275 do { 1276 discardedSignal = sigtimedwait(&sigset, NULL, &timeout); 1277 } while (discardedSignal == -1 && errno == EINTR); 1278 1279 if (discardedSignal == signum) 1280 return true; 1281 else if (discardedSignal == -1 && errno == EAGAIN) 1282 return false; 1283 else 1284 panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n", 1285 discardedSignal, errno); 1286} 1287 1288void 1289BaseKvmCPU::setupCounters() 1290{ 1291 DPRINTF(Kvm, "Attaching cycle counter...\n"); 1292 PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE, 1293 PERF_COUNT_HW_CPU_CYCLES); 1294 cfgCycles.disabled(true) 1295 .pinned(true); 1296 1297 // Try to exclude the host. We set both exclude_hv and 1298 // exclude_host since different architectures use slightly 1299 // different APIs in the kernel. 1300 cfgCycles.exclude_hv(true) 1301 .exclude_host(true); 1302 1303 if (perfControlledByTimer) { 1304 // We need to configure the cycles counter to send overflows 1305 // since we are going to use it to trigger timer signals that 1306 // trap back into m5 from KVM. In practice, this means that we 1307 // need to set some non-zero sample period that gets 1308 // overridden when the timer is armed. 1309 cfgCycles.wakeupEvents(1) 1310 .samplePeriod(42); 1311 } 1312 1313 hwCycles.attach(cfgCycles, 1314 0); // TID (0 => currentThread) 1315 1316 setupInstCounter(); 1317} 1318 1319bool 1320BaseKvmCPU::tryDrain() 1321{ 1322 if (drainState() != DrainState::Draining) 1323 return false; 1324 1325 if (!archIsDrained()) { 1326 DPRINTF(Drain, "tryDrain: Architecture code is not ready.\n"); 1327 return false; 1328 } 1329 1330 if (_status == Idle || _status == Running) { 1331 DPRINTF(Drain, 1332 "tryDrain: CPU transitioned into the Idle state, drain done\n"); 1333 signalDrainDone(); 1334 return true; 1335 } else { 1336 DPRINTF(Drain, "tryDrain: CPU not ready.\n"); 1337 return false; 1338 } 1339} 1340 1341void 1342BaseKvmCPU::ioctlRun() 1343{ 1344 if (ioctl(KVM_RUN) == -1) { 1345 if (errno != EINTR) 1346 panic("KVM: Failed to start virtual CPU (errno: %i)\n", 1347 errno); 1348 } 1349} 1350 1351void 1352BaseKvmCPU::setupInstStop() 1353{ 1354 if (comInstEventQueue[0]->empty()) { 1355 setupInstCounter(0); 1356 } else { 1357 const uint64_t next(comInstEventQueue[0]->nextTick()); 1358 1359 assert(next > ctrInsts); 1360 setupInstCounter(next - ctrInsts); 1361 } 1362} 1363 1364void 1365BaseKvmCPU::setupInstCounter(uint64_t period) 1366{ 1367 // No need to do anything if we aren't attaching for the first 1368 // time or the period isn't changing. 1369 if (period == activeInstPeriod && hwInstructions.attached()) 1370 return; 1371 1372 PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE, 1373 PERF_COUNT_HW_INSTRUCTIONS); 1374 1375 // Try to exclude the host. We set both exclude_hv and 1376 // exclude_host since different architectures use slightly 1377 // different APIs in the kernel. 1378 cfgInstructions.exclude_hv(true) 1379 .exclude_host(true); 1380 1381 if (period) { 1382 // Setup a sampling counter if that has been requested. 1383 cfgInstructions.wakeupEvents(1) 1384 .samplePeriod(period); 1385 } 1386 1387 // We need to detach and re-attach the counter to reliably change 1388 // sampling settings. See PerfKvmCounter::period() for details. 1389 if (hwInstructions.attached()) 1390 hwInstructions.detach(); 1391 assert(hwCycles.attached()); 1392 hwInstructions.attach(cfgInstructions, 1393 0, // TID (0 => currentThread) 1394 hwCycles); 1395 1396 if (period) 1397 hwInstructions.enableSignals(KVM_KICK_SIGNAL); 1398 1399 activeInstPeriod = period; 1400} 1401