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 --- 35 unchanged lines hidden (view full) --- 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/Checkpoint.hh" |
52#include "debug/Drain.hh" |
53#include "debug/Kvm.hh" 54#include "debug/KvmIO.hh" 55#include "debug/KvmRun.hh" 56#include "params/BaseKvmCPU.hh" 57#include "sim/process.hh" 58#include "sim/system.hh" 59 |
60#include <signal.h> 61 |
62/* Used by some KVM macros */ 63#define PAGE_SIZE pageSize 64 65volatile bool timerOverflowed = false; 66 67static void 68onTimerOverflow(int signo, siginfo_t *si, void *data) 69{ --- 9 unchanged lines hidden (view full) --- 79 threadContextDirty(true), 80 kvmStateDirty(false), 81 vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0), 82 _kvmRun(NULL), mmioRing(NULL), 83 pageSize(sysconf(_SC_PAGE_SIZE)), 84 tickEvent(*this), 85 perfControlledByTimer(params->usePerfOverflow), 86 hostFactor(params->hostFactor), |
87 drainManager(NULL), |
88 ctrInsts(0) 89{ 90 if (pageSize == -1) 91 panic("KVM: Failed to determine host page size (%i)\n", 92 errno); 93 94 thread = new SimpleThread(this, 0, params->system, 95 params->itb, params->dtb, params->isa[0]); 96 thread->setStatus(ThreadContext::Halted); 97 tc = thread->getTC(); 98 threadContexts.push_back(tc); 99 100 setupCounters(); |
101 102 if (params->usePerfOverflow) 103 runTimer.reset(new PerfKvmTimer(hwCycles, 104 KVM_TIMER_SIGNAL, 105 params->hostFactor, 106 params->clock)); 107 else 108 runTimer.reset(new PosixKvmTimer(KVM_TIMER_SIGNAL, CLOCK_MONOTONIC, --- 40 unchanged lines hidden (view full) --- 149 // Tell the VM that a CPU is about to start. 150 vm.cpuStartup(); 151 152 // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are 153 // not guaranteed that the parent KVM VM has initialized at that 154 // point. Initialize virtual CPUs here instead. 155 vcpuFD = vm.createVCPU(vcpuID); 156 |
157 // Setup signal handlers. This has to be done after the vCPU is 158 // created since it manipulates the vCPU signal mask. 159 setupSignalHandler(); 160 |
161 // Map the KVM run structure */ 162 vcpuMMapSize = kvm.getVCPUMMapSize(); 163 _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize, 164 PROT_READ | PROT_WRITE, MAP_SHARED, 165 vcpuFD, 0); 166 if (_kvmRun == MAP_FAILED) 167 panic("KVM: Failed to map run data structure\n"); 168 --- 65 unchanged lines hidden (view full) --- 234void 235BaseKvmCPU::serializeThread(std::ostream &os, ThreadID tid) 236{ 237 if (DTRACE(Checkpoint)) { 238 DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid); 239 dump(); 240 } 241 |
242 assert(tid == 0); 243 assert(_status == Idle); 244 thread->serialize(os); 245} 246 247void 248BaseKvmCPU::unserializeThread(Checkpoint *cp, const std::string §ion, 249 ThreadID tid) --- 7 unchanged lines hidden (view full) --- 257} 258 259unsigned int 260BaseKvmCPU::drain(DrainManager *dm) 261{ 262 if (switchedOut()) 263 return 0; 264 |
265 DPRINTF(Drain, "BaseKvmCPU::drain\n"); 266 switch (_status) { 267 case Running: 268 // The base KVM code is normally ready when it is in the 269 // Running state, but the architecture specific code might be 270 // of a different opinion. This may happen when the CPU been 271 // notified of an event that hasn't been accepted by the vCPU 272 // yet. 273 if (!archIsDrained()) { 274 drainManager = dm; 275 return 1; 276 } |
277 |
278 // The state of the CPU is consistent, so we don't need to do 279 // anything special to drain it. We simply de-schedule the 280 // tick event and enter the Idle state to prevent nasty things 281 // like MMIOs from happening. 282 if (tickEvent.scheduled()) 283 deschedule(tickEvent); 284 _status = Idle; |
285 |
286 /** FALLTHROUGH */ 287 case Idle: 288 // Idle, no need to drain 289 assert(!tickEvent.scheduled()); 290 291 // Sync the thread context here since we'll need it when we 292 // switch CPUs or checkpoint the CPU. 293 syncThreadContext(); 294 295 return 0; 296 297 case RunningServiceCompletion: 298 // The CPU has just requested a service that was handled in 299 // the RunningService state, but the results have still not 300 // been reported to the CPU. Now, we /could/ probably just 301 // update the register state ourselves instead of letting KVM 302 // handle it, but that would be tricky. Instead, we enter KVM 303 // and let it do its stuff. 304 drainManager = dm; 305 306 DPRINTF(Drain, "KVM CPU is waiting for service completion, " 307 "requesting drain.\n"); 308 return 1; 309 310 case RunningService: 311 // We need to drain since the CPU is waiting for service (e.g., MMIOs) 312 drainManager = dm; 313 314 DPRINTF(Drain, "KVM CPU is waiting for service, requesting drain.\n"); 315 return 1; 316 317 default: 318 panic("KVM: Unhandled CPU state in drain()\n"); 319 return 0; 320 } |
321} 322 323void 324BaseKvmCPU::drainResume() 325{ 326 assert(!tickEvent.scheduled()); 327 328 // We might have been switched out. In that case, we don't need to --- 14 unchanged lines hidden (view full) --- 343 } 344} 345 346void 347BaseKvmCPU::switchOut() 348{ 349 DPRINTF(Kvm, "switchOut\n"); 350 |
351 BaseCPU::switchOut(); 352 353 // We should have drained prior to executing a switchOut, which 354 // means that the tick event shouldn't be scheduled and the CPU is 355 // idle. 356 assert(!tickEvent.scheduled()); 357 assert(_status == Idle); 358} --- 7 unchanged lines hidden (view full) --- 366 367 // We should have drained prior to executing a switchOut, which 368 // means that the tick event shouldn't be scheduled and the CPU is 369 // idle. 370 assert(!tickEvent.scheduled()); 371 assert(_status == Idle); 372 assert(threadContexts.size() == 1); 373 |
374 // Force an update of the KVM state here instead of flagging the 375 // TC as dirty. This is not ideal from a performance point of 376 // view, but it makes debugging easier as it allows meaningful KVM 377 // state to be dumped before and after a takeover. 378 updateKvmState(); 379 threadContextDirty = false; |
380} 381 382void 383BaseKvmCPU::verifyMemoryMode() const 384{ 385 if (!(system->isAtomicMode() && system->bypassCaches())) { 386 fatal("The KVM-based CPUs requires the memory system to be in the " 387 "'atomic_noncaching' mode.\n"); --- 93 unchanged lines hidden (view full) --- 481BaseKvmCPU::dump() 482{ 483 inform("State dumping not implemented."); 484} 485 486void 487BaseKvmCPU::tick() 488{ |
489 Tick delay(0); 490 assert(_status != Idle); |
491 |
492 switch (_status) { 493 case RunningService: 494 // handleKvmExit() will determine the next state of the CPU 495 delay = handleKvmExit(); |
496 |
497 if (tryDrain()) 498 _status = Idle; 499 break; |
500 |
501 case RunningServiceCompletion: 502 case Running: { 503 Tick ticksToExecute(mainEventQueue.nextTick() - curTick()); |
504 |
505 // We might need to update the KVM state. 506 syncKvmState(); |
507 |
508 DPRINTF(KvmRun, "Entering KVM...\n"); 509 if (drainManager) { 510 // Force an immediate exit from KVM after completing 511 // pending operations. The architecture-specific code 512 // takes care to run until it is in a state where it can 513 // safely be drained. 514 delay = kvmRunDrain(); 515 } else { 516 delay = kvmRun(ticksToExecute); 517 } 518 519 // Entering into KVM implies that we'll have to reload the thread 520 // context from KVM if we want to access it. Flag the KVM state as 521 // dirty with respect to the cached thread context. 522 kvmStateDirty = true; 523 524 // Enter into the RunningService state unless the 525 // simulation was stopped by a timer. 526 if (_kvmRun->exit_reason != KVM_EXIT_INTR) 527 _status = RunningService; 528 else 529 _status = Running; 530 531 if (tryDrain()) 532 _status = Idle; 533 } break; 534 |
535 default: |
536 panic("BaseKvmCPU entered tick() in an illegal state (%i)\n", 537 _status); |
538 } |
539 540 // Schedule a new tick if we are still running 541 if (_status != Idle) 542 schedule(tickEvent, clockEdge(ticksToCycles(delay))); |
543} 544 |
545Tick 546BaseKvmCPU::kvmRunDrain() 547{ 548 // By default, the only thing we need to drain is a pending IO 549 // operation which assumes that we are in the 550 // RunningServiceCompletion state. 551 assert(_status == RunningServiceCompletion); 552 553 // Deliver the data from the pending IO operation and immediately 554 // exit. 555 return kvmRun(0); 556} 557 |
558uint64_t 559BaseKvmCPU::getHostCycles() const 560{ 561 return hwCycles.read(); 562} 563 564Tick 565BaseKvmCPU::kvmRun(Tick ticks) 566{ |
567 Tick ticksExecuted; |
568 DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks); 569 timerOverflowed = false; 570 |
571 if (ticks == 0) { 572 // Settings ticks == 0 is a special case which causes an entry 573 // into KVM that finishes pending operations (e.g., IO) and 574 // then immediately exits. 575 DPRINTF(KvmRun, "KVM: Delivering IO without full guest entry\n"); |
576 |
577 // This signal is always masked while we are executing in gem5 578 // and gets unmasked temporarily as soon as we enter into 579 // KVM. See setSignalMask() and setupSignalHandler(). 580 raise(KVM_TIMER_SIGNAL); |
581 |
582 // Enter into KVM. KVM will check for signals after completing 583 // pending operations (IO). Since the KVM_TIMER_SIGNAL is 584 // pending, this forces an immediate exit into gem5 again. We 585 // don't bother to setup timers since this shouldn't actually 586 // execute any code in the guest. 587 ioctlRun(); |
588 |
589 // We always execute at least one cycle to prevent the 590 // BaseKvmCPU::tick() to be rescheduled on the same tick 591 // twice. 592 ticksExecuted = clockPeriod(); 593 } else { 594 if (ticks < runTimer->resolution()) { 595 DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n", 596 ticks, runTimer->resolution()); 597 ticks = runTimer->resolution(); 598 } |
599 |
600 // Get hardware statistics after synchronizing contexts. The KVM 601 // state update might affect guest cycle counters. 602 uint64_t baseCycles(getHostCycles()); 603 uint64_t baseInstrs(hwInstructions.read()); |
604 |
605 // Arm the run timer and start the cycle timer if it isn't 606 // controlled by the overflow timer. Starting/stopping the cycle 607 // timer automatically starts the other perf timers as they are in 608 // the same counter group. 609 runTimer->arm(ticks); 610 if (!perfControlledByTimer) 611 hwCycles.start(); |
612 |
613 ioctlRun(); 614 615 runTimer->disarm(); 616 if (!perfControlledByTimer) 617 hwCycles.stop(); 618 619 // The timer signal may have been delivered after we exited 620 // from KVM. It will be pending in that case since it is 621 // masked when we aren't executing in KVM. Discard it to make 622 // sure we don't deliver it immediately next time we try to 623 // enter into KVM. 624 discardPendingSignal(KVM_TIMER_SIGNAL); 625 626 const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles); 627 const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor); 628 const uint64_t instsExecuted(hwInstructions.read() - baseInstrs); 629 ticksExecuted = runTimer->ticksFromHostCycles(hostCyclesExecuted); 630 631 if (ticksExecuted < ticks && 632 timerOverflowed && 633 _kvmRun->exit_reason == KVM_EXIT_INTR) { 634 // TODO: We should probably do something clever here... 635 warn("KVM: Early timer event, requested %i ticks but got %i ticks.\n", 636 ticks, ticksExecuted); 637 } 638 639 /* Update statistics */ 640 numCycles += simCyclesExecuted;; 641 numInsts += instsExecuted; 642 ctrInsts += instsExecuted; 643 system->totalNumInsts += instsExecuted; 644 645 DPRINTF(KvmRun, 646 "KVM: Executed %i instructions in %i cycles " 647 "(%i ticks, sim cycles: %i).\n", 648 instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted); |
649 } 650 |
651 ++numVMExits; |
652 |
653 return ticksExecuted + flushCoalescedMMIO(); 654} 655 656void 657BaseKvmCPU::kvmNonMaskableInterrupt() 658{ 659 ++numInterrupts; 660 if (ioctl(KVM_NMI) == -1) --- 155 unchanged lines hidden (view full) --- 816 updateKvmState(); 817 threadContextDirty = false; 818} 819 820Tick 821BaseKvmCPU::handleKvmExit() 822{ 823 DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason); |
824 assert(_status == RunningService); |
825 |
826 // Switch into the running state by default. Individual handlers 827 // can override this. 828 _status = Running; |
829 switch (_kvmRun->exit_reason) { 830 case KVM_EXIT_UNKNOWN: 831 return handleKvmExitUnknown(); 832 833 case KVM_EXIT_EXCEPTION: 834 return handleKvmExitException(); 835 836 case KVM_EXIT_IO: |
837 _status = RunningServiceCompletion; |
838 ++numIO; 839 return handleKvmExitIO(); 840 841 case KVM_EXIT_HYPERCALL: 842 ++numHypercalls; 843 return handleKvmExitHypercall(); 844 845 case KVM_EXIT_HLT: 846 /* The guest has halted and is waiting for interrupts */ 847 DPRINTF(Kvm, "handleKvmExitHalt\n"); 848 ++numHalt; 849 850 // Suspend the thread until the next interrupt arrives 851 thread->suspend(); 852 853 // This is actually ignored since the thread is suspended. 854 return 0; 855 856 case KVM_EXIT_MMIO: |
857 _status = RunningServiceCompletion; |
858 /* Service memory mapped IO requests */ 859 DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n", 860 _kvmRun->mmio.is_write, 861 _kvmRun->mmio.phys_addr, _kvmRun->mmio.len); 862 863 ++numMMIO; 864 return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data, 865 _kvmRun->mmio.len, _kvmRun->mmio.is_write); --- 72 unchanged lines hidden (view full) --- 938 mmio_req.setPhys(paddr, size, Request::UNCACHEABLE, dataMasterId()); 939 940 const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq); 941 Packet pkt(&mmio_req, cmd); 942 pkt.dataStatic(data); 943 return dataPort.sendAtomic(&pkt); 944} 945 |
946void 947BaseKvmCPU::setSignalMask(const sigset_t *mask) 948{ 949 std::unique_ptr<struct kvm_signal_mask> kvm_mask; 950 951 if (mask) { 952 kvm_mask.reset((struct kvm_signal_mask *)operator new( 953 sizeof(struct kvm_signal_mask) + sizeof(*mask))); 954 // The kernel and the user-space headers have different ideas 955 // about the size of sigset_t. This seems like a massive hack, 956 // but is actually what qemu does. 957 assert(sizeof(*mask) >= 8); 958 kvm_mask->len = 8; 959 memcpy(kvm_mask->sigset, mask, kvm_mask->len); 960 } 961 962 if (ioctl(KVM_SET_SIGNAL_MASK, (void *)kvm_mask.get()) == -1) 963 panic("KVM: Failed to set vCPU signal mask (errno: %i)\n", 964 errno); 965} 966 |
967int 968BaseKvmCPU::ioctl(int request, long p1) const 969{ 970 if (vcpuFD == -1) 971 panic("KVM: CPU ioctl called before initialization\n"); 972 973 return ::ioctl(vcpuFD, request, p1); 974} --- 30 unchanged lines hidden (view full) --- 1005{ 1006 struct sigaction sa; 1007 1008 memset(&sa, 0, sizeof(sa)); 1009 sa.sa_sigaction = onTimerOverflow; 1010 sa.sa_flags = SA_SIGINFO | SA_RESTART; 1011 if (sigaction(KVM_TIMER_SIGNAL, &sa, NULL) == -1) 1012 panic("KVM: Failed to setup vCPU signal handler\n"); |
1013 1014 sigset_t sigset; 1015 if (sigprocmask(SIG_BLOCK, NULL, &sigset) == -1) 1016 panic("KVM: Failed get signal mask\n"); 1017 1018 // Request KVM to setup the same signal mask as we're currently 1019 // running with. We'll sometimes need to mask the KVM_TIMER_SIGNAL 1020 // to cause immediate exits from KVM after servicing IO 1021 // requests. See kvmRun(). 1022 setSignalMask(&sigset); 1023 1024 // Mask the KVM_TIMER_SIGNAL so it isn't delivered unless we're 1025 // actually executing inside KVM. 1026 sigaddset(&sigset, KVM_TIMER_SIGNAL); 1027 if (sigprocmask(SIG_SETMASK, &sigset, NULL) == -1) 1028 panic("KVM: Failed mask the KVM timer signal\n"); |
1029} 1030 |
1031bool 1032BaseKvmCPU::discardPendingSignal(int signum) const 1033{ 1034 int discardedSignal; 1035 1036 // Setting the timeout to zero causes sigtimedwait to return 1037 // immediately. 1038 struct timespec timeout; 1039 timeout.tv_sec = 0; 1040 timeout.tv_nsec = 0; 1041 1042 sigset_t sigset; 1043 sigemptyset(&sigset); 1044 sigaddset(&sigset, signum); 1045 1046 do { 1047 discardedSignal = sigtimedwait(&sigset, NULL, &timeout); 1048 } while (discardedSignal == -1 && errno == EINTR); 1049 1050 if (discardedSignal == signum) 1051 return true; 1052 else if (discardedSignal == -1 && errno == EAGAIN) 1053 return false; 1054 else 1055 panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n", 1056 discardedSignal, errno); 1057} 1058 |
1059void 1060BaseKvmCPU::setupCounters() 1061{ 1062 DPRINTF(Kvm, "Attaching cycle counter...\n"); 1063 PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE, 1064 PERF_COUNT_HW_CPU_CYCLES); 1065 cfgCycles.disabled(true) 1066 .pinned(true); --- 13 unchanged lines hidden (view full) --- 1080 1081 DPRINTF(Kvm, "Attaching instruction counter...\n"); 1082 PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE, 1083 PERF_COUNT_HW_INSTRUCTIONS); 1084 hwInstructions.attach(cfgInstructions, 1085 0, // TID (0 => currentThread) 1086 hwCycles); 1087} |
1088 1089bool 1090BaseKvmCPU::tryDrain() 1091{ 1092 if (!drainManager) 1093 return false; 1094 1095 if (!archIsDrained()) { 1096 DPRINTF(Drain, "tryDrain: Architecture code is not ready.\n"); 1097 return false; 1098 } 1099 1100 if (_status == Idle || _status == Running) { 1101 DPRINTF(Drain, 1102 "tryDrain: CPU transitioned into the Idle state, drain done\n"); 1103 drainManager->signalDrainDone(); 1104 drainManager = NULL; 1105 return true; 1106 } else { 1107 DPRINTF(Drain, "tryDrain: CPU not ready.\n"); 1108 return false; 1109 } 1110} 1111 1112void 1113BaseKvmCPU::ioctlRun() 1114{ 1115 if (ioctl(KVM_RUN) == -1) { 1116 if (errno != EINTR) 1117 panic("KVM: Failed to start virtual CPU (errno: %i)\n", 1118 errno); 1119 } 1120} |