base.cc revision 9655
1/* 2 * Copyright (c) 2012 ARM Limited 3 * All rights reserved 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions are 16 * met: redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer; 18 * redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution; 21 * neither the name of the copyright holders nor the names of its 22 * contributors may be used to endorse or promote products derived from 23 * this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 * Authors: Andreas Sandberg 38 */ 39 40#include <linux/kvm.h> 41#include <sys/ioctl.h> 42#include <sys/mman.h> 43#include <unistd.h> 44 45#include <cerrno> 46#include <csignal> 47#include <ostream> 48 49#include "arch/utility.hh" 50#include "cpu/kvm/base.hh" 51#include "debug/Kvm.hh" 52#include "debug/KvmIO.hh" 53#include "debug/KvmRun.hh" 54#include "params/BaseKvmCPU.hh" 55#include "sim/process.hh" 56#include "sim/system.hh" 57 58/* Used by some KVM macros */ 59#define PAGE_SIZE pageSize 60 61volatile bool timerOverflowed = false; 62 63static void 64onTimerOverflow(int signo, siginfo_t *si, void *data) 65{ 66 timerOverflowed = true; 67} 68 69BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params) 70 : BaseCPU(params), 71 vm(*params->kvmVM), 72 _status(Idle), 73 dataPort(name() + ".dcache_port", this), 74 instPort(name() + ".icache_port", this), 75 threadContextDirty(true), 76 kvmStateDirty(false), 77 vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0), 78 _kvmRun(NULL), mmioRing(NULL), 79 pageSize(sysconf(_SC_PAGE_SIZE)), 80 tickEvent(*this), 81 perfControlledByTimer(params->usePerfOverflow), 82 hostFactor(params->hostFactor) 83{ 84 if (pageSize == -1) 85 panic("KVM: Failed to determine host page size (%i)\n", 86 errno); 87 88 thread = new SimpleThread(this, 0, params->system, 89 params->itb, params->dtb, params->isa[0]); 90 thread->setStatus(ThreadContext::Halted); 91 tc = thread->getTC(); 92 threadContexts.push_back(tc); 93 94 setupCounters(); 95 setupSignalHandler(); 96 97 if (params->usePerfOverflow) 98 runTimer.reset(new PerfKvmTimer(hwCycles, 99 KVM_TIMER_SIGNAL, 100 params->hostFactor, 101 params->clock)); 102 else 103 runTimer.reset(new PosixKvmTimer(KVM_TIMER_SIGNAL, CLOCK_MONOTONIC, 104 params->hostFactor, 105 params->clock)); 106} 107 108BaseKvmCPU::~BaseKvmCPU() 109{ 110 if (_kvmRun) 111 munmap(_kvmRun, vcpuMMapSize); 112 close(vcpuFD); 113} 114 115void 116BaseKvmCPU::init() 117{ 118 BaseCPU::init(); 119 120 if (numThreads != 1) 121 fatal("KVM: Multithreading not supported"); 122 123 tc->initMemProxies(tc); 124 125 // initialize CPU, including PC 126 if (FullSystem && !switchedOut()) 127 TheISA::initCPU(tc, tc->contextId()); 128 129 mmio_req.setThreadContext(tc->contextId(), 0); 130} 131 132void 133BaseKvmCPU::startup() 134{ 135 Kvm &kvm(vm.kvm); 136 137 BaseCPU::startup(); 138 139 assert(vcpuFD == -1); 140 141 // Tell the VM that a CPU is about to start. 142 vm.cpuStartup(); 143 144 // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are 145 // not guaranteed that the parent KVM VM has initialized at that 146 // point. Initialize virtual CPUs here instead. 147 vcpuFD = vm.createVCPU(vcpuID); 148 149 // Map the KVM run structure */ 150 vcpuMMapSize = kvm.getVCPUMMapSize(); 151 _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize, 152 PROT_READ | PROT_WRITE, MAP_SHARED, 153 vcpuFD, 0); 154 if (_kvmRun == MAP_FAILED) 155 panic("KVM: Failed to map run data structure\n"); 156 157 // Setup a pointer to the MMIO ring buffer if coalesced MMIO is 158 // available. The offset into the KVM's communication page is 159 // provided by the coalesced MMIO capability. 160 int mmioOffset(kvm.capCoalescedMMIO()); 161 if (mmioOffset) { 162 inform("KVM: Coalesced IO available\n"); 163 mmioRing = (struct kvm_coalesced_mmio_ring *)( 164 (char *)_kvmRun + (mmioOffset * pageSize)); 165 } else { 166 inform("KVM: Coalesced not supported by host OS\n"); 167 } 168} 169 170void 171BaseKvmCPU::regStats() 172{ 173 using namespace Stats; 174 175 BaseCPU::regStats(); 176 177 numVMExits 178 .name(name() + ".numVMExits") 179 .desc("total number of KVM exits") 180 ; 181 182 numMMIO 183 .name(name() + ".numMMIO") 184 .desc("number of VM exits due to memory mapped IO") 185 ; 186 187 numCoalescedMMIO 188 .name(name() + ".numCoalescedMMIO") 189 .desc("number of coalesced memory mapped IO requests") 190 ; 191 192 numIO 193 .name(name() + ".numIO") 194 .desc("number of VM exits due to legacy IO") 195 ; 196 197 numHalt 198 .name(name() + ".numHalt") 199 .desc("number of VM exits due to wait for interrupt instructions") 200 ; 201 202 numInterrupts 203 .name(name() + ".numInterrupts") 204 .desc("number of interrupts delivered") 205 ; 206 207 numHypercalls 208 .name(name() + ".numHypercalls") 209 .desc("number of hypercalls") 210 ; 211} 212 213void 214BaseKvmCPU::serializeThread(std::ostream &os, ThreadID tid) 215{ 216 // Update the thread context so we have something to serialize. 217 syncThreadContext(); 218 219 assert(tid == 0); 220 assert(_status == Idle); 221 thread->serialize(os); 222} 223 224void 225BaseKvmCPU::unserializeThread(Checkpoint *cp, const std::string §ion, 226 ThreadID tid) 227{ 228 assert(tid == 0); 229 assert(_status == Idle); 230 thread->unserialize(cp, section); 231 threadContextDirty = true; 232} 233 234unsigned int 235BaseKvmCPU::drain(DrainManager *dm) 236{ 237 if (switchedOut()) 238 return 0; 239 240 DPRINTF(Kvm, "drain\n"); 241 242 // De-schedule the tick event so we don't insert any more MMIOs 243 // into the system while it is draining. 244 if (tickEvent.scheduled()) 245 deschedule(tickEvent); 246 247 _status = Idle; 248 return 0; 249} 250 251void 252BaseKvmCPU::drainResume() 253{ 254 assert(!tickEvent.scheduled()); 255 256 // We might have been switched out. In that case, we don't need to 257 // do anything. 258 if (switchedOut()) 259 return; 260 261 DPRINTF(Kvm, "drainResume\n"); 262 verifyMemoryMode(); 263 264 // The tick event is de-scheduled as a part of the draining 265 // process. Re-schedule it if the thread context is active. 266 if (tc->status() == ThreadContext::Active) { 267 schedule(tickEvent, nextCycle()); 268 _status = Running; 269 } else { 270 _status = Idle; 271 } 272} 273 274void 275BaseKvmCPU::switchOut() 276{ 277 DPRINTF(Kvm, "switchOut\n"); 278 279 // Make sure to update the thread context in case, the new CPU 280 // will need to access it. 281 syncThreadContext(); 282 283 BaseCPU::switchOut(); 284 285 // We should have drained prior to executing a switchOut, which 286 // means that the tick event shouldn't be scheduled and the CPU is 287 // idle. 288 assert(!tickEvent.scheduled()); 289 assert(_status == Idle); 290} 291 292void 293BaseKvmCPU::takeOverFrom(BaseCPU *cpu) 294{ 295 DPRINTF(Kvm, "takeOverFrom\n"); 296 297 BaseCPU::takeOverFrom(cpu); 298 299 // We should have drained prior to executing a switchOut, which 300 // means that the tick event shouldn't be scheduled and the CPU is 301 // idle. 302 assert(!tickEvent.scheduled()); 303 assert(_status == Idle); 304 assert(threadContexts.size() == 1); 305 306 // The BaseCPU updated the thread context, make sure that we 307 // synchronize next time we enter start the CPU. 308 threadContextDirty = true; 309} 310 311void 312BaseKvmCPU::verifyMemoryMode() const 313{ 314 if (!(system->isAtomicMode() && system->bypassCaches())) { 315 fatal("The KVM-based CPUs requires the memory system to be in the " 316 "'atomic_noncaching' mode.\n"); 317 } 318} 319 320void 321BaseKvmCPU::wakeup() 322{ 323 DPRINTF(Kvm, "wakeup()\n"); 324 325 if (thread->status() != ThreadContext::Suspended) 326 return; 327 328 thread->activate(); 329} 330 331void 332BaseKvmCPU::activateContext(ThreadID thread_num, Cycles delay) 333{ 334 DPRINTF(Kvm, "ActivateContext %d (%d cycles)\n", thread_num, delay); 335 336 assert(thread_num == 0); 337 assert(thread); 338 339 assert(_status == Idle); 340 assert(!tickEvent.scheduled()); 341 342 numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend) 343 * hostFactor; 344 345 schedule(tickEvent, clockEdge(delay)); 346 _status = Running; 347} 348 349 350void 351BaseKvmCPU::suspendContext(ThreadID thread_num) 352{ 353 DPRINTF(Kvm, "SuspendContext %d\n", thread_num); 354 355 assert(thread_num == 0); 356 assert(thread); 357 358 if (_status == Idle) 359 return; 360 361 assert(_status == Running); 362 363 // The tick event may no be scheduled if the quest has requested 364 // the monitor to wait for interrupts. The normal CPU models can 365 // get their tick events descheduled by quiesce instructions, but 366 // that can't happen here. 367 if (tickEvent.scheduled()) 368 deschedule(tickEvent); 369 370 _status = Idle; 371} 372 373void 374BaseKvmCPU::deallocateContext(ThreadID thread_num) 375{ 376 // for now, these are equivalent 377 suspendContext(thread_num); 378} 379 380void 381BaseKvmCPU::haltContext(ThreadID thread_num) 382{ 383 // for now, these are equivalent 384 suspendContext(thread_num); 385} 386 387ThreadContext * 388BaseKvmCPU::getContext(int tn) 389{ 390 assert(tn == 0); 391 syncThreadContext(); 392 return tc; 393} 394 395 396Counter 397BaseKvmCPU::totalInsts() const 398{ 399 return hwInstructions.read(); 400} 401 402Counter 403BaseKvmCPU::totalOps() const 404{ 405 hack_once("Pretending totalOps is equivalent to totalInsts()\n"); 406 return hwInstructions.read(); 407} 408 409void 410BaseKvmCPU::dump() 411{ 412 inform("State dumping not implemented."); 413} 414 415void 416BaseKvmCPU::tick() 417{ 418 assert(_status == Running); 419 420 DPRINTF(KvmRun, "Entering KVM...\n"); 421 422 Tick ticksToExecute(mainEventQueue.nextTick() - curTick()); 423 Tick ticksExecuted(kvmRun(ticksToExecute)); 424 425 Tick delay(ticksExecuted + handleKvmExit()); 426 427 switch (_status) { 428 case Running: 429 schedule(tickEvent, clockEdge(ticksToCycles(delay))); 430 break; 431 432 default: 433 /* The CPU is halted or waiting for an interrupt from a 434 * device. Don't start it. */ 435 break; 436 } 437} 438 439Tick 440BaseKvmCPU::kvmRun(Tick ticks) 441{ 442 uint64_t baseCycles(hwCycles.read()); 443 uint64_t baseInstrs(hwInstructions.read()); 444 445 // We might need to update the KVM state. 446 syncKvmState(); 447 // Entering into KVM implies that we'll have to reload the thread 448 // context from KVM if we want to access it. Flag the KVM state as 449 // dirty with respect to the cached thread context. 450 kvmStateDirty = true; 451 452 if (ticks < runTimer->resolution()) { 453 DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n", 454 ticks, runTimer->resolution()); 455 ticks = runTimer->resolution(); 456 } 457 458 DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks); 459 timerOverflowed = false; 460 461 // Arm the run timer and start the cycle timer if it isn't 462 // controlled by the overflow timer. Starting/stopping the cycle 463 // timer automatically starts the other perf timers as they are in 464 // the same counter group. 465 runTimer->arm(ticks); 466 if (!perfControlledByTimer) 467 hwCycles.start(); 468 469 if (ioctl(KVM_RUN) == -1) { 470 if (errno != EINTR) 471 panic("KVM: Failed to start virtual CPU (errno: %i)\n", 472 errno); 473 } 474 475 runTimer->disarm(); 476 if (!perfControlledByTimer) 477 hwCycles.stop(); 478 479 480 uint64_t cyclesExecuted(hwCycles.read() - baseCycles); 481 Tick ticksExecuted(runTimer->ticksFromHostCycles(cyclesExecuted)); 482 483 if (ticksExecuted < ticks && 484 timerOverflowed && 485 _kvmRun->exit_reason == KVM_EXIT_INTR) { 486 // TODO: We should probably do something clever here... 487 warn("KVM: Early timer event, requested %i ticks but got %i ticks.\n", 488 ticks, ticksExecuted); 489 } 490 491 numCycles += cyclesExecuted * hostFactor; 492 ++numVMExits; 493 494 DPRINTF(KvmRun, "KVM: Executed %i instructions in %i cycles (%i ticks, sim cycles: %i).\n", 495 hwInstructions.read() - baseInstrs, 496 cyclesExecuted, 497 ticksExecuted, 498 cyclesExecuted * hostFactor); 499 500 return ticksExecuted + flushCoalescedMMIO(); 501} 502 503void 504BaseKvmCPU::kvmNonMaskableInterrupt() 505{ 506 ++numInterrupts; 507 if (ioctl(KVM_NMI) == -1) 508 panic("KVM: Failed to deliver NMI to virtual CPU\n"); 509} 510 511void 512BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt) 513{ 514 ++numInterrupts; 515 if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1) 516 panic("KVM: Failed to deliver interrupt to virtual CPU\n"); 517} 518 519void 520BaseKvmCPU::getRegisters(struct kvm_regs ®s) const 521{ 522 if (ioctl(KVM_GET_REGS, ®s) == -1) 523 panic("KVM: Failed to get guest registers\n"); 524} 525 526void 527BaseKvmCPU::setRegisters(const struct kvm_regs ®s) 528{ 529 if (ioctl(KVM_SET_REGS, (void *)®s) == -1) 530 panic("KVM: Failed to set guest registers\n"); 531} 532 533void 534BaseKvmCPU::getSpecialRegisters(struct kvm_sregs ®s) const 535{ 536 if (ioctl(KVM_GET_SREGS, ®s) == -1) 537 panic("KVM: Failed to get guest special registers\n"); 538} 539 540void 541BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs ®s) 542{ 543 if (ioctl(KVM_SET_SREGS, (void *)®s) == -1) 544 panic("KVM: Failed to set guest special registers\n"); 545} 546 547void 548BaseKvmCPU::getFPUState(struct kvm_fpu &state) const 549{ 550 if (ioctl(KVM_GET_FPU, &state) == -1) 551 panic("KVM: Failed to get guest FPU state\n"); 552} 553 554void 555BaseKvmCPU::setFPUState(const struct kvm_fpu &state) 556{ 557 if (ioctl(KVM_SET_FPU, (void *)&state) == -1) 558 panic("KVM: Failed to set guest FPU state\n"); 559} 560 561 562void 563BaseKvmCPU::setOneReg(uint64_t id, const void *addr) 564{ 565#ifdef KVM_SET_ONE_REG 566 struct kvm_one_reg reg; 567 reg.id = id; 568 reg.addr = (uint64_t)addr; 569 570 if (ioctl(KVM_SET_ONE_REG, ®) == -1) { 571 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n", 572 id, errno); 573 } 574#else 575 panic("KVM_SET_ONE_REG is unsupported on this platform.\n"); 576#endif 577} 578 579void 580BaseKvmCPU::getOneReg(uint64_t id, void *addr) const 581{ 582#ifdef KVM_GET_ONE_REG 583 struct kvm_one_reg reg; 584 reg.id = id; 585 reg.addr = (uint64_t)addr; 586 587 if (ioctl(KVM_GET_ONE_REG, ®) == -1) { 588 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n", 589 id, errno); 590 } 591#else 592 panic("KVM_GET_ONE_REG is unsupported on this platform.\n"); 593#endif 594} 595 596std::string 597BaseKvmCPU::getAndFormatOneReg(uint64_t id) const 598{ 599#ifdef KVM_GET_ONE_REG 600 std::ostringstream ss; 601 602 ss.setf(std::ios::hex, std::ios::basefield); 603 ss.setf(std::ios::showbase); 604#define HANDLE_INTTYPE(len) \ 605 case KVM_REG_SIZE_U ## len: { \ 606 uint ## len ## _t value; \ 607 getOneReg(id, &value); \ 608 ss << value; \ 609 } break 610 611#define HANDLE_ARRAY(len) \ 612 case KVM_REG_SIZE_U ## len: { \ 613 uint8_t value[len / 8]; \ 614 getOneReg(id, value); \ 615 ss << "[" << value[0]; \ 616 for (int i = 1; i < len / 8; ++i) \ 617 ss << ", " << value[i]; \ 618 ss << "]"; \ 619 } break 620 621 switch (id & KVM_REG_SIZE_MASK) { 622 HANDLE_INTTYPE(8); 623 HANDLE_INTTYPE(16); 624 HANDLE_INTTYPE(32); 625 HANDLE_INTTYPE(64); 626 HANDLE_ARRAY(128); 627 HANDLE_ARRAY(256); 628 HANDLE_ARRAY(512); 629 HANDLE_ARRAY(1024); 630 default: 631 ss << "??"; 632 } 633 634#undef HANDLE_INTTYPE 635#undef HANDLE_ARRAY 636 637 return ss.str(); 638#else 639 panic("KVM_GET_ONE_REG is unsupported on this platform.\n"); 640#endif 641} 642 643void 644BaseKvmCPU::syncThreadContext() 645{ 646 if (!kvmStateDirty) 647 return; 648 649 assert(!threadContextDirty); 650 651 updateThreadContext(); 652 kvmStateDirty = false; 653} 654 655void 656BaseKvmCPU::syncKvmState() 657{ 658 if (!threadContextDirty) 659 return; 660 661 assert(!kvmStateDirty); 662 663 updateKvmState(); 664 threadContextDirty = false; 665} 666 667Tick 668BaseKvmCPU::handleKvmExit() 669{ 670 DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason); 671 672 switch (_kvmRun->exit_reason) { 673 case KVM_EXIT_UNKNOWN: 674 return handleKvmExitUnknown(); 675 676 case KVM_EXIT_EXCEPTION: 677 return handleKvmExitException(); 678 679 case KVM_EXIT_IO: 680 ++numIO; 681 return handleKvmExitIO(); 682 683 case KVM_EXIT_HYPERCALL: 684 ++numHypercalls; 685 return handleKvmExitHypercall(); 686 687 case KVM_EXIT_HLT: 688 /* The guest has halted and is waiting for interrupts */ 689 DPRINTF(Kvm, "handleKvmExitHalt\n"); 690 ++numHalt; 691 692 // Suspend the thread until the next interrupt arrives 693 thread->suspend(); 694 695 // This is actually ignored since the thread is suspended. 696 return 0; 697 698 case KVM_EXIT_MMIO: 699 /* Service memory mapped IO requests */ 700 DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n", 701 _kvmRun->mmio.is_write, 702 _kvmRun->mmio.phys_addr, _kvmRun->mmio.len); 703 704 ++numMMIO; 705 return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data, 706 _kvmRun->mmio.len, _kvmRun->mmio.is_write); 707 708 case KVM_EXIT_IRQ_WINDOW_OPEN: 709 return handleKvmExitIRQWindowOpen(); 710 711 case KVM_EXIT_FAIL_ENTRY: 712 return handleKvmExitFailEntry(); 713 714 case KVM_EXIT_INTR: 715 /* KVM was interrupted by a signal, restart it in the next 716 * tick. */ 717 return 0; 718 719 case KVM_EXIT_INTERNAL_ERROR: 720 panic("KVM: Internal error (suberror: %u)\n", 721 _kvmRun->internal.suberror); 722 723 default: 724 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason); 725 } 726} 727 728Tick 729BaseKvmCPU::handleKvmExitIO() 730{ 731 panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n", 732 _kvmRun->io.direction, _kvmRun->io.size, 733 _kvmRun->io.port, _kvmRun->io.count); 734} 735 736Tick 737BaseKvmCPU::handleKvmExitHypercall() 738{ 739 panic("KVM: Unhandled hypercall\n"); 740} 741 742Tick 743BaseKvmCPU::handleKvmExitIRQWindowOpen() 744{ 745 warn("KVM: Unhandled IRQ window.\n"); 746 return 0; 747} 748 749 750Tick 751BaseKvmCPU::handleKvmExitUnknown() 752{ 753 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n", 754 _kvmRun->hw.hardware_exit_reason); 755} 756 757Tick 758BaseKvmCPU::handleKvmExitException() 759{ 760 panic("KVM: Got exception when starting vCPU " 761 "(exception: %u, error_code: %u)\n", 762 _kvmRun->ex.exception, _kvmRun->ex.error_code); 763} 764 765Tick 766BaseKvmCPU::handleKvmExitFailEntry() 767{ 768 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n", 769 _kvmRun->fail_entry.hardware_entry_failure_reason); 770} 771 772Tick 773BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write) 774{ 775 mmio_req.setPhys(paddr, size, 776 0, /* flags */ 777 dataMasterId()); 778 779 const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq); 780 Packet pkt(&mmio_req, cmd); 781 pkt.dataStatic(data); 782 return dataPort.sendAtomic(&pkt); 783} 784 785int 786BaseKvmCPU::ioctl(int request, long p1) const 787{ 788 if (vcpuFD == -1) 789 panic("KVM: CPU ioctl called before initialization\n"); 790 791 return ::ioctl(vcpuFD, request, p1); 792} 793 794Tick 795BaseKvmCPU::flushCoalescedMMIO() 796{ 797 if (!mmioRing) 798 return 0; 799 800 DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n"); 801 802 // TODO: We might need to do synchronization when we start to 803 // support multiple CPUs 804 Tick ticks(0); 805 while (mmioRing->first != mmioRing->last) { 806 struct kvm_coalesced_mmio &ent( 807 mmioRing->coalesced_mmio[mmioRing->first]); 808 809 DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n", 810 ent.phys_addr, ent.len); 811 812 ++numCoalescedMMIO; 813 ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true); 814 815 mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX; 816 } 817 818 return ticks; 819} 820 821void 822BaseKvmCPU::setupSignalHandler() 823{ 824 struct sigaction sa; 825 826 memset(&sa, 0, sizeof(sa)); 827 sa.sa_sigaction = onTimerOverflow; 828 sa.sa_flags = SA_SIGINFO | SA_RESTART; 829 if (sigaction(KVM_TIMER_SIGNAL, &sa, NULL) == -1) 830 panic("KVM: Failed to setup vCPU signal handler\n"); 831} 832 833void 834BaseKvmCPU::setupCounters() 835{ 836 DPRINTF(Kvm, "Attaching cycle counter...\n"); 837 PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE, 838 PERF_COUNT_HW_CPU_CYCLES); 839 cfgCycles.disabled(true) 840 .pinned(true); 841 842 if (perfControlledByTimer) { 843 // We need to configure the cycles counter to send overflows 844 // since we are going to use it to trigger timer signals that 845 // trap back into m5 from KVM. In practice, this means that we 846 // need to set some non-zero sample period that gets 847 // overridden when the timer is armed. 848 cfgCycles.wakeupEvents(1) 849 .samplePeriod(42); 850 } 851 852 hwCycles.attach(cfgCycles, 853 0); // TID (0 => currentThread) 854 855 DPRINTF(Kvm, "Attaching instruction counter...\n"); 856 PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE, 857 PERF_COUNT_HW_INSTRUCTIONS); 858 hwInstructions.attach(cfgInstructions, 859 0, // TID (0 => currentThread) 860 hwCycles); 861} 862