gem5  v20.1.0.0
base.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 2015, 2017 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 
38 #include "cpu/kvm/base.hh"
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 "debug/Checkpoint.hh"
51 #include "debug/Drain.hh"
52 #include "debug/Kvm.hh"
53 #include "debug/KvmIO.hh"
54 #include "debug/KvmRun.hh"
55 #include "params/BaseKvmCPU.hh"
56 #include "sim/process.hh"
57 #include "sim/system.hh"
58 
59 /* Used by some KVM macros */
60 #define PAGE_SIZE pageSize
61 
62 BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params)
63  : BaseCPU(params),
64  vm(*params->system->getKvmVM()),
65  _status(Idle),
66  dataPort(name() + ".dcache_port", this),
67  instPort(name() + ".icache_port", this),
68  alwaysSyncTC(params->alwaysSyncTC),
69  threadContextDirty(true),
70  kvmStateDirty(false),
71  vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
72  _kvmRun(NULL), mmioRing(NULL),
73  pageSize(sysconf(_SC_PAGE_SIZE)),
74  tickEvent([this]{ tick(); }, "BaseKvmCPU tick",
75  false, Event::CPU_Tick_Pri),
76  activeInstPeriod(0),
77  perfControlledByTimer(params->usePerfOverflow),
78  hostFactor(params->hostFactor), stats(this),
79  ctrInsts(0)
80 {
81  if (pageSize == -1)
82  panic("KVM: Failed to determine host page size (%i)\n",
83  errno);
84 
85  if (FullSystem)
86  thread = new SimpleThread(this, 0, params->system, params->itb, params->dtb,
87  params->isa[0]);
88  else
89  thread = new SimpleThread(this, /* thread_num */ 0, params->system,
90  params->workload[0], params->itb,
91  params->dtb, params->isa[0]);
92 
93  thread->setStatus(ThreadContext::Halted);
94  tc = thread->getTC();
95  threadContexts.push_back(tc);
96 }
97 
99 {
100  if (_kvmRun)
101  munmap(_kvmRun, vcpuMMapSize);
102  close(vcpuFD);
103 }
104 
105 void
107 {
108  BaseCPU::init();
109 
110  if (numThreads != 1)
111  fatal("KVM: Multithreading not supported");
112 
113  tc->initMemProxies(tc);
114 }
115 
116 void
118 {
119  const BaseKvmCPUParams * const p(
120  dynamic_cast<const BaseKvmCPUParams *>(params()));
121 
122  Kvm &kvm(*vm.kvm);
123 
125 
126  assert(vcpuFD == -1);
127 
128  // Tell the VM that a CPU is about to start.
129  vm.cpuStartup();
130 
131  // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
132  // not guaranteed that the parent KVM VM has initialized at that
133  // point. Initialize virtual CPUs here instead.
135 
136  // Map the KVM run structure */
138  _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize,
139  PROT_READ | PROT_WRITE, MAP_SHARED,
140  vcpuFD, 0);
141  if (_kvmRun == MAP_FAILED)
142  panic("KVM: Failed to map run data structure\n");
143 
144  // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
145  // available. The offset into the KVM's communication page is
146  // provided by the coalesced MMIO capability.
147  int mmioOffset(kvm.capCoalescedMMIO());
148  if (!p->useCoalescedMMIO) {
149  inform("KVM: Coalesced MMIO disabled by config.\n");
150  } else if (mmioOffset) {
151  inform("KVM: Coalesced IO available\n");
152  mmioRing = (struct kvm_coalesced_mmio_ring *)(
153  (char *)_kvmRun + (mmioOffset * pageSize));
154  } else {
155  inform("KVM: Coalesced not supported by host OS\n");
156  }
157 
158  Event *startupEvent(
159  new EventFunctionWrapper([this]{ startupThread(); }, name(), true));
160  schedule(startupEvent, curTick());
161 }
162 
165 {
166  return (activeMMIOReqs || pendingMMIOPkts.size())
168 }
169 
170 Tick
172 {
173  if (cpu->system->isAtomicMode()) {
174  Tick delay = sendAtomic(pkt);
175  delete pkt;
176  return delay;
177  } else {
178  if (pendingMMIOPkts.empty() && sendTimingReq(pkt)) {
179  activeMMIOReqs++;
180  } else {
181  pendingMMIOPkts.push(pkt);
182  }
183  // Return value is irrelevant for timing-mode accesses.
184  return 0;
185  }
186 }
187 
188 bool
190 {
191  DPRINTF(KvmIO, "KVM: Finished timing request\n");
192 
193  delete pkt;
194  activeMMIOReqs--;
195 
196  // We can switch back into KVM when all pending and in-flight MMIO
197  // operations have completed.
198  if (!(activeMMIOReqs || pendingMMIOPkts.size())) {
199  DPRINTF(KvmIO, "KVM: Finished all outstanding timing requests\n");
200  cpu->finishMMIOPending();
201  }
202  return true;
203 }
204 
205 void
207 {
208  DPRINTF(KvmIO, "KVM: Retry for timing request\n");
209 
210  assert(pendingMMIOPkts.size());
211 
212  // Assuming that we can issue infinite requests this cycle is a bit
213  // unrealistic, but it's not worth modeling something more complex in
214  // KVM.
215  while (pendingMMIOPkts.size() && sendTimingReq(pendingMMIOPkts.front())) {
216  pendingMMIOPkts.pop();
217  activeMMIOReqs++;
218  }
219 }
220 
221 void
223 {
224  assert(_status == RunningMMIOPending);
225  assert(!tickEvent.scheduled());
226 
228  schedule(tickEvent, nextCycle());
229 }
230 
231 void
233 {
234  // Do thread-specific initialization. We need to setup signal
235  // delivery for counters and timers from within the thread that
236  // will execute the event queue to ensure that signals are
237  // delivered to the right threads.
238  const BaseKvmCPUParams * const p(
239  dynamic_cast<const BaseKvmCPUParams *>(params()));
240 
241  vcpuThread = pthread_self();
242 
243  // Setup signal handlers. This has to be done after the vCPU is
244  // created since it manipulates the vCPU signal mask.
246 
247  setupCounters();
248 
249  if (p->usePerfOverflow)
250  runTimer.reset(new PerfKvmTimer(hwCycles,
252  p->hostFactor,
253  p->hostFreq));
254  else
255  runTimer.reset(new PosixKvmTimer(KVM_KICK_SIGNAL, CLOCK_MONOTONIC,
256  p->hostFactor,
257  p->hostFreq));
258 
259 }
260 
262  : Stats::Group(parent),
263  ADD_STAT(committedInsts, "Number of instructions committed"),
264  ADD_STAT(numVMExits, "total number of KVM exits"),
265  ADD_STAT(numVMHalfEntries,
266  "number of KVM entries to finalize pending operations"),
267  ADD_STAT(numExitSignal, "exits due to signal delivery"),
268  ADD_STAT(numMMIO, "number of VM exits due to memory mapped IO"),
269  ADD_STAT(numCoalescedMMIO,
270  "number of coalesced memory mapped IO requests"),
271  ADD_STAT(numIO, "number of VM exits due to legacy IO"),
272  ADD_STAT(numHalt,
273  "number of VM exits due to wait for interrupt instructions"),
274  ADD_STAT(numInterrupts, "number of interrupts delivered"),
275  ADD_STAT(numHypercalls, "number of hypercalls")
276 {
277 }
278 
279 void
281 {
282  if (DTRACE(Checkpoint)) {
283  DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid);
284  dump();
285  }
286 
287  assert(tid == 0);
288  assert(_status == Idle);
289  thread->serialize(cp);
290 }
291 
292 void
294 {
295  DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid);
296 
297  assert(tid == 0);
298  assert(_status == Idle);
300  threadContextDirty = true;
301 }
302 
305 {
306  if (switchedOut())
307  return DrainState::Drained;
308 
309  DPRINTF(Drain, "BaseKvmCPU::drain\n");
310 
311  // The event queue won't be locked when calling drain since that's
312  // not done from an event. Lock the event queue here to make sure
313  // that scoped migrations continue to work if we need to
314  // synchronize the thread context.
315  std::lock_guard<EventQueue> lock(*this->eventQueue());
316 
317  switch (_status) {
318  case Running:
319  // The base KVM code is normally ready when it is in the
320  // Running state, but the architecture specific code might be
321  // of a different opinion. This may happen when the CPU been
322  // notified of an event that hasn't been accepted by the vCPU
323  // yet.
324  if (!archIsDrained())
325  return DrainState::Draining;
326 
327  // The state of the CPU is consistent, so we don't need to do
328  // anything special to drain it. We simply de-schedule the
329  // tick event and enter the Idle state to prevent nasty things
330  // like MMIOs from happening.
331  if (tickEvent.scheduled())
332  deschedule(tickEvent);
333  _status = Idle;
334 
336  case Idle:
337  // Idle, no need to drain
338  assert(!tickEvent.scheduled());
339 
340  // Sync the thread context here since we'll need it when we
341  // switch CPUs or checkpoint the CPU.
343 
344  return DrainState::Drained;
345 
347  // The CPU has just requested a service that was handled in
348  // the RunningService state, but the results have still not
349  // been reported to the CPU. Now, we /could/ probably just
350  // update the register state ourselves instead of letting KVM
351  // handle it, but that would be tricky. Instead, we enter KVM
352  // and let it do its stuff.
353  DPRINTF(Drain, "KVM CPU is waiting for service completion, "
354  "requesting drain.\n");
355  return DrainState::Draining;
356 
357  case RunningMMIOPending:
358  // We need to drain since there are in-flight timing accesses
359  DPRINTF(Drain, "KVM CPU is waiting for timing accesses to complete, "
360  "requesting drain.\n");
361  return DrainState::Draining;
362 
363  case RunningService:
364  // We need to drain since the CPU is waiting for service (e.g., MMIOs)
365  DPRINTF(Drain, "KVM CPU is waiting for service, requesting drain.\n");
366  return DrainState::Draining;
367 
368  default:
369  panic("KVM: Unhandled CPU state in drain()\n");
370  return DrainState::Drained;
371  }
372 }
373 
374 void
376 {
377  assert(!tickEvent.scheduled());
378 
379  // We might have been switched out. In that case, we don't need to
380  // do anything.
381  if (switchedOut())
382  return;
383 
384  DPRINTF(Kvm, "drainResume\n");
386 
387  // The tick event is de-scheduled as a part of the draining
388  // process. Re-schedule it if the thread context is active.
389  if (tc->status() == ThreadContext::Active) {
390  schedule(tickEvent, nextCycle());
391  _status = Running;
392  } else {
393  _status = Idle;
394  }
395 }
396 
397 void
399 {
400  // We should have drained prior to forking, which means that the
401  // tick event shouldn't be scheduled and the CPU is idle.
402  assert(!tickEvent.scheduled());
403  assert(_status == Idle);
404 
405  if (vcpuFD != -1) {
406  if (close(vcpuFD) == -1)
407  warn("kvm CPU: notifyFork failed to close vcpuFD\n");
408 
409  if (_kvmRun)
410  munmap(_kvmRun, vcpuMMapSize);
411 
412  vcpuFD = -1;
413  _kvmRun = NULL;
414 
416  hwCycles.detach();
417  }
418 }
419 
420 void
422 {
423  DPRINTF(Kvm, "switchOut\n");
424 
426 
427  // We should have drained prior to executing a switchOut, which
428  // means that the tick event shouldn't be scheduled and the CPU is
429  // idle.
430  assert(!tickEvent.scheduled());
431  assert(_status == Idle);
432 }
433 
434 void
436 {
437  DPRINTF(Kvm, "takeOverFrom\n");
438 
440 
441  // We should have drained prior to executing a switchOut, which
442  // means that the tick event shouldn't be scheduled and the CPU is
443  // idle.
444  assert(!tickEvent.scheduled());
445  assert(_status == Idle);
446  assert(threadContexts.size() == 1);
447 
448  // Force an update of the KVM state here instead of flagging the
449  // TC as dirty. This is not ideal from a performance point of
450  // view, but it makes debugging easier as it allows meaningful KVM
451  // state to be dumped before and after a takeover.
452  updateKvmState();
453  threadContextDirty = false;
454 }
455 
456 void
458 {
459  if (!(system->bypassCaches())) {
460  fatal("The KVM-based CPUs requires the memory system to be in the "
461  "'noncaching' mode.\n");
462  }
463 }
464 
465 void
467 {
468  DPRINTF(Kvm, "wakeup()\n");
469  // This method might have been called from another
470  // context. Migrate to this SimObject's event queue when
471  // delivering the wakeup signal.
472  EventQueue::ScopedMigration migrate(eventQueue());
473 
474  // Kick the vCPU to get it to come out of KVM.
475  kick();
476 
478  return;
479 
480  thread->activate();
481 }
482 
483 void
485 {
486  DPRINTF(Kvm, "ActivateContext %d\n", thread_num);
487 
488  assert(thread_num == 0);
489  assert(thread);
490 
491  assert(_status == Idle);
492  assert(!tickEvent.scheduled());
493 
494  numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend);
495 
496  schedule(tickEvent, clockEdge(Cycles(0)));
497  _status = Running;
498 }
499 
500 
501 void
503 {
504  DPRINTF(Kvm, "SuspendContext %d\n", thread_num);
505 
506  assert(thread_num == 0);
507  assert(thread);
508 
509  if (_status == Idle)
510  return;
511 
513 
514  // The tick event may no be scheduled if the quest has requested
515  // the monitor to wait for interrupts. The normal CPU models can
516  // get their tick events descheduled by quiesce instructions, but
517  // that can't happen here.
518  if (tickEvent.scheduled())
519  deschedule(tickEvent);
520 
521  _status = Idle;
522 }
523 
524 void
526 {
527  // for now, these are equivalent
528  suspendContext(thread_num);
529 }
530 
531 void
533 {
534  // for now, these are equivalent
535  suspendContext(thread_num);
537 }
538 
541 {
542  assert(tn == 0);
544  return tc;
545 }
546 
547 
548 Counter
550 {
551  return ctrInsts;
552 }
553 
554 Counter
556 {
557  hack_once("Pretending totalOps is equivalent to totalInsts()\n");
558  return ctrInsts;
559 }
560 
561 void
563 {
564  inform("State dumping not implemented.");
565 }
566 
567 void
569 {
570  Tick delay(0);
571  assert(_status != Idle && _status != RunningMMIOPending);
572 
573  switch (_status) {
574  case RunningService:
575  // handleKvmExit() will determine the next state of the CPU
576  delay = handleKvmExit();
577 
578  if (tryDrain())
579  _status = Idle;
580  break;
581 
583  case Running: {
584  auto &queue = thread->comInstEventQueue;
585  const uint64_t nextInstEvent(
586  queue.empty() ? MaxTick : queue.nextTick());
587  // Enter into KVM and complete pending IO instructions if we
588  // have an instruction event pending.
589  const Tick ticksToExecute(
590  nextInstEvent > ctrInsts ?
591  curEventQueue()->nextTick() - curTick() : 0);
592 
593  if (alwaysSyncTC)
594  threadContextDirty = true;
595 
596  // We might need to update the KVM state.
597  syncKvmState();
598 
599  // Setup any pending instruction count breakpoints using
600  // PerfEvent if we are going to execute more than just an IO
601  // completion.
602  if (ticksToExecute > 0)
603  setupInstStop();
604 
605  DPRINTF(KvmRun, "Entering KVM...\n");
606  if (drainState() == DrainState::Draining) {
607  // Force an immediate exit from KVM after completing
608  // pending operations. The architecture-specific code
609  // takes care to run until it is in a state where it can
610  // safely be drained.
611  delay = kvmRunDrain();
612  } else {
613  delay = kvmRun(ticksToExecute);
614  }
615 
616  // The CPU might have been suspended before entering into
617  // KVM. Assume that the CPU was suspended /before/ entering
618  // into KVM and skip the exit handling.
619  if (_status == Idle)
620  break;
621 
622  // Entering into KVM implies that we'll have to reload the thread
623  // context from KVM if we want to access it. Flag the KVM state as
624  // dirty with respect to the cached thread context.
625  kvmStateDirty = true;
626 
627  if (alwaysSyncTC)
629 
630  // Enter into the RunningService state unless the
631  // simulation was stopped by a timer.
632  if (_kvmRun->exit_reason != KVM_EXIT_INTR) {
634  } else {
636  _status = Running;
637  }
638 
639  // Service any pending instruction events. The vCPU should
640  // have exited in time for the event using the instruction
641  // counter configured by setupInstStop().
642  queue.serviceEvents(ctrInsts);
643 
644  if (tryDrain())
645  _status = Idle;
646  } break;
647 
648  default:
649  panic("BaseKvmCPU entered tick() in an illegal state (%i)\n",
650  _status);
651  }
652 
653  // Schedule a new tick if we are still running
654  if (_status != Idle && _status != RunningMMIOPending) {
655  if (_kvmRun->exit_reason == KVM_EXIT_INTR && runTimer->expired())
656  schedule(tickEvent, clockEdge(ticksToCycles(
657  curEventQueue()->nextTick() - curTick() + 1)));
658  else
659  schedule(tickEvent, clockEdge(ticksToCycles(delay)));
660  }
661 }
662 
663 Tick
665 {
666  // By default, the only thing we need to drain is a pending IO
667  // operation which assumes that we are in the
668  // RunningServiceCompletion or RunningMMIOPending state.
669  assert(_status == RunningServiceCompletion ||
671 
672  // Deliver the data from the pending IO operation and immediately
673  // exit.
674  return kvmRun(0);
675 }
676 
677 uint64_t
679 {
680  return hwCycles.read();
681 }
682 
683 Tick
685 {
686  Tick ticksExecuted;
687  fatal_if(vcpuFD == -1,
688  "Trying to run a KVM CPU in a forked child process. "
689  "This is not supported.\n");
690  DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks);
691 
692  if (ticks == 0) {
693  // Settings ticks == 0 is a special case which causes an entry
694  // into KVM that finishes pending operations (e.g., IO) and
695  // then immediately exits.
696  DPRINTF(KvmRun, "KVM: Delivering IO without full guest entry\n");
697 
699 
700  // Send a KVM_KICK_SIGNAL to the vCPU thread (i.e., this
701  // thread). The KVM control signal is masked while executing
702  // in gem5 and gets unmasked temporarily as when entering
703  // KVM. See setSignalMask() and setupSignalHandler().
704  kick();
705 
706  // Start the vCPU. KVM will check for signals after completing
707  // pending operations (IO). Since the KVM_KICK_SIGNAL is
708  // pending, this forces an immediate exit to gem5 again. We
709  // don't bother to setup timers since this shouldn't actually
710  // execute any code (other than completing half-executed IO
711  // instructions) in the guest.
712  ioctlRun();
713 
714  // We always execute at least one cycle to prevent the
715  // BaseKvmCPU::tick() to be rescheduled on the same tick
716  // twice.
717  ticksExecuted = clockPeriod();
718  } else {
719  // This method is executed as a result of a tick event. That
720  // means that the event queue will be locked when entering the
721  // method. We temporarily unlock the event queue to allow
722  // other threads to steal control of this thread to inject
723  // interrupts. They will typically lock the queue and then
724  // force an exit from KVM by kicking the vCPU.
726 
727  if (ticks < runTimer->resolution()) {
728  DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
729  ticks, runTimer->resolution());
730  ticks = runTimer->resolution();
731  }
732 
733  // Get hardware statistics after synchronizing contexts. The KVM
734  // state update might affect guest cycle counters.
735  uint64_t baseCycles(getHostCycles());
736  uint64_t baseInstrs(hwInstructions.read());
737 
738  // Arm the run timer and start the cycle timer if it isn't
739  // controlled by the overflow timer. Starting/stopping the cycle
740  // timer automatically starts the other perf timers as they are in
741  // the same counter group.
742  runTimer->arm(ticks);
744  hwCycles.start();
745 
746  ioctlRun();
747 
748  runTimer->disarm();
750  hwCycles.stop();
751 
752  // The control signal may have been delivered after we exited
753  // from KVM. It will be pending in that case since it is
754  // masked when we aren't executing in KVM. Discard it to make
755  // sure we don't deliver it immediately next time we try to
756  // enter into KVM.
758 
759  const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles);
760  const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor);
761  const uint64_t instsExecuted(hwInstructions.read() - baseInstrs);
762  ticksExecuted = runTimer->ticksFromHostCycles(hostCyclesExecuted);
763 
764  /* Update statistics */
765  numCycles += simCyclesExecuted;;
766  stats.committedInsts += instsExecuted;
767  ctrInsts += instsExecuted;
768  system->totalNumInsts += instsExecuted;
769 
770  DPRINTF(KvmRun,
771  "KVM: Executed %i instructions in %i cycles "
772  "(%i ticks, sim cycles: %i).\n",
773  instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted);
774  }
775 
776  ++stats.numVMExits;
777 
778  return ticksExecuted + flushCoalescedMMIO();
779 }
780 
781 void
783 {
785  if (ioctl(KVM_NMI) == -1)
786  panic("KVM: Failed to deliver NMI to virtual CPU\n");
787 }
788 
789 void
790 BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt)
791 {
793  if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1)
794  panic("KVM: Failed to deliver interrupt to virtual CPU\n");
795 }
796 
797 void
798 BaseKvmCPU::getRegisters(struct kvm_regs &regs) const
799 {
800  if (ioctl(KVM_GET_REGS, &regs) == -1)
801  panic("KVM: Failed to get guest registers\n");
802 }
803 
804 void
805 BaseKvmCPU::setRegisters(const struct kvm_regs &regs)
806 {
807  if (ioctl(KVM_SET_REGS, (void *)&regs) == -1)
808  panic("KVM: Failed to set guest registers\n");
809 }
810 
811 void
812 BaseKvmCPU::getSpecialRegisters(struct kvm_sregs &regs) const
813 {
814  if (ioctl(KVM_GET_SREGS, &regs) == -1)
815  panic("KVM: Failed to get guest special registers\n");
816 }
817 
818 void
819 BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs &regs)
820 {
821  if (ioctl(KVM_SET_SREGS, (void *)&regs) == -1)
822  panic("KVM: Failed to set guest special registers\n");
823 }
824 
825 void
826 BaseKvmCPU::getFPUState(struct kvm_fpu &state) const
827 {
828  if (ioctl(KVM_GET_FPU, &state) == -1)
829  panic("KVM: Failed to get guest FPU state\n");
830 }
831 
832 void
833 BaseKvmCPU::setFPUState(const struct kvm_fpu &state)
834 {
835  if (ioctl(KVM_SET_FPU, (void *)&state) == -1)
836  panic("KVM: Failed to set guest FPU state\n");
837 }
838 
839 
840 void
841 BaseKvmCPU::setOneReg(uint64_t id, const void *addr)
842 {
843 #ifdef KVM_SET_ONE_REG
844  struct kvm_one_reg reg;
845  reg.id = id;
846  reg.addr = (uint64_t)addr;
847 
848  if (ioctl(KVM_SET_ONE_REG, &reg) == -1) {
849  panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
850  id, errno);
851  }
852 #else
853  panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
854 #endif
855 }
856 
857 void
858 BaseKvmCPU::getOneReg(uint64_t id, void *addr) const
859 {
860 #ifdef KVM_GET_ONE_REG
861  struct kvm_one_reg reg;
862  reg.id = id;
863  reg.addr = (uint64_t)addr;
864 
865  if (ioctl(KVM_GET_ONE_REG, &reg) == -1) {
866  panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
867  id, errno);
868  }
869 #else
870  panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
871 #endif
872 }
873 
874 std::string
876 {
877 #ifdef KVM_GET_ONE_REG
878  std::ostringstream ss;
879 
880  ss.setf(std::ios::hex, std::ios::basefield);
881  ss.setf(std::ios::showbase);
882 #define HANDLE_INTTYPE(len) \
883  case KVM_REG_SIZE_U ## len: { \
884  uint ## len ## _t value; \
885  getOneReg(id, &value); \
886  ss << value; \
887  } break
888 
889 #define HANDLE_ARRAY(len) \
890  case KVM_REG_SIZE_U ## len: { \
891  uint8_t value[len / 8]; \
892  getOneReg(id, value); \
893  ccprintf(ss, "[0x%x", value[0]); \
894  for (int i = 1; i < len / 8; ++i) \
895  ccprintf(ss, ", 0x%x", value[i]); \
896  ccprintf(ss, "]"); \
897  } break
898 
899  switch (id & KVM_REG_SIZE_MASK) {
900  HANDLE_INTTYPE(8);
901  HANDLE_INTTYPE(16);
902  HANDLE_INTTYPE(32);
903  HANDLE_INTTYPE(64);
904  HANDLE_ARRAY(128);
905  HANDLE_ARRAY(256);
906  HANDLE_ARRAY(512);
907  HANDLE_ARRAY(1024);
908  default:
909  ss << "??";
910  }
911 
912 #undef HANDLE_INTTYPE
913 #undef HANDLE_ARRAY
914 
915  return ss.str();
916 #else
917  panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
918 #endif
919 }
920 
921 void
923 {
924  if (!kvmStateDirty)
925  return;
926 
927  assert(!threadContextDirty);
928 
930  kvmStateDirty = false;
931 }
932 
933 void
935 {
936  if (!threadContextDirty)
937  return;
938 
939  assert(!kvmStateDirty);
940 
941  updateKvmState();
942  threadContextDirty = false;
943 }
944 
945 Tick
947 {
948  DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason);
949  assert(_status == RunningService);
950 
951  // Switch into the running state by default. Individual handlers
952  // can override this.
953  _status = Running;
954  switch (_kvmRun->exit_reason) {
955  case KVM_EXIT_UNKNOWN:
956  return handleKvmExitUnknown();
957 
958  case KVM_EXIT_EXCEPTION:
959  return handleKvmExitException();
960 
961  case KVM_EXIT_IO:
962  {
963  ++stats.numIO;
964  Tick ticks = handleKvmExitIO();
966  return ticks;
967  }
968 
969  case KVM_EXIT_HYPERCALL:
971  return handleKvmExitHypercall();
972 
973  case KVM_EXIT_HLT:
974  /* The guest has halted and is waiting for interrupts */
975  DPRINTF(Kvm, "handleKvmExitHalt\n");
976  ++stats.numHalt;
977 
978  // Suspend the thread until the next interrupt arrives
979  thread->suspend();
980 
981  // This is actually ignored since the thread is suspended.
982  return 0;
983 
984  case KVM_EXIT_MMIO:
985  {
986  /* Service memory mapped IO requests */
987  DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
988  _kvmRun->mmio.is_write,
989  _kvmRun->mmio.phys_addr, _kvmRun->mmio.len);
990 
991  ++stats.numMMIO;
992  Tick ticks = doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data,
993  _kvmRun->mmio.len, _kvmRun->mmio.is_write);
994  // doMMIOAccess could have triggered a suspend, in which case we don't
995  // want to overwrite the _status.
996  if (_status != Idle)
998  return ticks;
999  }
1000 
1001  case KVM_EXIT_IRQ_WINDOW_OPEN:
1002  return handleKvmExitIRQWindowOpen();
1003 
1004  case KVM_EXIT_FAIL_ENTRY:
1005  return handleKvmExitFailEntry();
1006 
1007  case KVM_EXIT_INTR:
1008  /* KVM was interrupted by a signal, restart it in the next
1009  * tick. */
1010  return 0;
1011 
1012  case KVM_EXIT_INTERNAL_ERROR:
1013  panic("KVM: Internal error (suberror: %u)\n",
1014  _kvmRun->internal.suberror);
1015 
1016  default:
1017  dump();
1018  panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason);
1019  }
1020 }
1021 
1022 Tick
1024 {
1025  panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
1026  _kvmRun->io.direction, _kvmRun->io.size,
1027  _kvmRun->io.port, _kvmRun->io.count);
1028 }
1029 
1030 Tick
1032 {
1033  panic("KVM: Unhandled hypercall\n");
1034 }
1035 
1036 Tick
1038 {
1039  warn("KVM: Unhandled IRQ window.\n");
1040  return 0;
1041 }
1042 
1043 
1044 Tick
1046 {
1047  dump();
1048  panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
1049  _kvmRun->hw.hardware_exit_reason);
1050 }
1051 
1052 Tick
1054 {
1055  dump();
1056  panic("KVM: Got exception when starting vCPU "
1057  "(exception: %u, error_code: %u)\n",
1058  _kvmRun->ex.exception, _kvmRun->ex.error_code);
1059 }
1060 
1061 Tick
1063 {
1064  dump();
1065  panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
1066  _kvmRun->fail_entry.hardware_entry_failure_reason);
1067 }
1068 
1069 Tick
1070 BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write)
1071 {
1074 
1075  RequestPtr mmio_req = std::make_shared<Request>(
1076  paddr, size, Request::UNCACHEABLE, dataRequestorId());
1077 
1078  mmio_req->setContext(tc->contextId());
1079  // Some architectures do need to massage physical addresses a bit
1080  // before they are inserted into the memory system. This enables
1081  // APIC accesses on x86 and m5ops where supported through a MMIO
1082  // interface.
1083  BaseTLB::Mode tlb_mode(write ? BaseTLB::Write : BaseTLB::Read);
1084  Fault fault(tc->getDTBPtr()->finalizePhysical(mmio_req, tc, tlb_mode));
1085  if (fault != NoFault)
1086  warn("Finalization of MMIO address failed: %s\n", fault->name());
1087 
1088 
1089  const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq);
1090  PacketPtr pkt = new Packet(mmio_req, cmd);
1091  pkt->dataStatic(data);
1092 
1093  if (mmio_req->isLocalAccess()) {
1094  // We currently assume that there is no need to migrate to a
1095  // different event queue when doing local accesses. Currently, they
1096  // are only used for m5ops, so it should be a valid assumption.
1097  const Cycles ipr_delay = mmio_req->localAccessor(tc, pkt);
1098  threadContextDirty = true;
1099  delete pkt;
1100  return clockPeriod() * ipr_delay;
1101  } else {
1102  // Temporarily lock and migrate to the device event queue to
1103  // prevent races in multi-core mode.
1105 
1106  return dataPort.submitIO(pkt);
1107  }
1108 }
1109 
1110 void
1112 {
1113  std::unique_ptr<struct kvm_signal_mask> kvm_mask;
1114 
1115  if (mask) {
1116  kvm_mask.reset((struct kvm_signal_mask *)operator new(
1117  sizeof(struct kvm_signal_mask) + sizeof(*mask)));
1118  // The kernel and the user-space headers have different ideas
1119  // about the size of sigset_t. This seems like a massive hack,
1120  // but is actually what qemu does.
1121  assert(sizeof(*mask) >= 8);
1122  kvm_mask->len = 8;
1123  memcpy(kvm_mask->sigset, mask, kvm_mask->len);
1124  }
1125 
1126  if (ioctl(KVM_SET_SIGNAL_MASK, (void *)kvm_mask.get()) == -1)
1127  panic("KVM: Failed to set vCPU signal mask (errno: %i)\n",
1128  errno);
1129 }
1130 
1131 int
1132 BaseKvmCPU::ioctl(int request, long p1) const
1133 {
1134  if (vcpuFD == -1)
1135  panic("KVM: CPU ioctl called before initialization\n");
1136 
1137  return ::ioctl(vcpuFD, request, p1);
1138 }
1139 
1140 Tick
1142 {
1143  if (!mmioRing)
1144  return 0;
1145 
1146  DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n");
1147 
1148  // TODO: We might need to do synchronization when we start to
1149  // support multiple CPUs
1150  Tick ticks(0);
1151  while (mmioRing->first != mmioRing->last) {
1152  struct kvm_coalesced_mmio &ent(
1153  mmioRing->coalesced_mmio[mmioRing->first]);
1154 
1155  DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
1156  ent.phys_addr, ent.len);
1157 
1159  ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true);
1160 
1161  mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX;
1162  }
1163 
1164  return ticks;
1165 }
1166 
1177 static void
1178 onKickSignal(int signo, siginfo_t *si, void *data)
1179 {
1180 }
1181 
1182 void
1184 {
1185  struct sigaction sa;
1186 
1187  memset(&sa, 0, sizeof(sa));
1188  sa.sa_sigaction = onKickSignal;
1189  sa.sa_flags = SA_SIGINFO | SA_RESTART;
1190  if (sigaction(KVM_KICK_SIGNAL, &sa, NULL) == -1)
1191  panic("KVM: Failed to setup vCPU timer signal handler\n");
1192 
1193  sigset_t sigset;
1194  if (pthread_sigmask(SIG_BLOCK, NULL, &sigset) == -1)
1195  panic("KVM: Failed get signal mask\n");
1196 
1197  // Request KVM to setup the same signal mask as we're currently
1198  // running with except for the KVM control signal. We'll sometimes
1199  // need to raise the KVM_KICK_SIGNAL to cause immediate exits from
1200  // KVM after servicing IO requests. See kvmRun().
1201  sigdelset(&sigset, KVM_KICK_SIGNAL);
1202  setSignalMask(&sigset);
1203 
1204  // Mask our control signals so they aren't delivered unless we're
1205  // actually executing inside KVM.
1206  sigaddset(&sigset, KVM_KICK_SIGNAL);
1207  if (pthread_sigmask(SIG_SETMASK, &sigset, NULL) == -1)
1208  panic("KVM: Failed mask the KVM control signals\n");
1209 }
1210 
1211 bool
1213 {
1214  int discardedSignal;
1215 
1216  // Setting the timeout to zero causes sigtimedwait to return
1217  // immediately.
1218  struct timespec timeout;
1219  timeout.tv_sec = 0;
1220  timeout.tv_nsec = 0;
1221 
1222  sigset_t sigset;
1223  sigemptyset(&sigset);
1224  sigaddset(&sigset, signum);
1225 
1226  do {
1227  discardedSignal = sigtimedwait(&sigset, NULL, &timeout);
1228  } while (discardedSignal == -1 && errno == EINTR);
1229 
1230  if (discardedSignal == signum)
1231  return true;
1232  else if (discardedSignal == -1 && errno == EAGAIN)
1233  return false;
1234  else
1235  panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n",
1236  discardedSignal, errno);
1237 }
1238 
1239 void
1241 {
1242  DPRINTF(Kvm, "Attaching cycle counter...\n");
1243  PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE,
1244  PERF_COUNT_HW_CPU_CYCLES);
1245  cfgCycles.disabled(true)
1246  .pinned(true);
1247 
1248  // Try to exclude the host. We set both exclude_hv and
1249  // exclude_host since different architectures use slightly
1250  // different APIs in the kernel.
1251  cfgCycles.exclude_hv(true)
1252  .exclude_host(true);
1253 
1254  if (perfControlledByTimer) {
1255  // We need to configure the cycles counter to send overflows
1256  // since we are going to use it to trigger timer signals that
1257  // trap back into m5 from KVM. In practice, this means that we
1258  // need to set some non-zero sample period that gets
1259  // overridden when the timer is armed.
1260  cfgCycles.wakeupEvents(1)
1261  .samplePeriod(42);
1262  }
1263 
1264  hwCycles.attach(cfgCycles,
1265  0); // TID (0 => currentThread)
1266 
1267  setupInstCounter();
1268 }
1269 
1270 bool
1272 {
1273  if (drainState() != DrainState::Draining)
1274  return false;
1275 
1276  if (!archIsDrained()) {
1277  DPRINTF(Drain, "tryDrain: Architecture code is not ready.\n");
1278  return false;
1279  }
1280 
1281  if (_status == Idle || _status == Running) {
1282  DPRINTF(Drain,
1283  "tryDrain: CPU transitioned into the Idle state, drain done\n");
1284  signalDrainDone();
1285  return true;
1286  } else {
1287  DPRINTF(Drain, "tryDrain: CPU not ready.\n");
1288  return false;
1289  }
1290 }
1291 
1292 void
1294 {
1295  if (ioctl(KVM_RUN) == -1) {
1296  if (errno != EINTR)
1297  panic("KVM: Failed to start virtual CPU (errno: %i)\n",
1298  errno);
1299  }
1300 }
1301 
1302 void
1304 {
1305  if (thread->comInstEventQueue.empty()) {
1306  setupInstCounter(0);
1307  } else {
1309  assert(next > ctrInsts);
1310  setupInstCounter(next - ctrInsts);
1311  }
1312 }
1313 
1314 void
1316 {
1317  // No need to do anything if we aren't attaching for the first
1318  // time or the period isn't changing.
1319  if (period == activeInstPeriod && hwInstructions.attached())
1320  return;
1321 
1322  PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE,
1323  PERF_COUNT_HW_INSTRUCTIONS);
1324 
1325  // Try to exclude the host. We set both exclude_hv and
1326  // exclude_host since different architectures use slightly
1327  // different APIs in the kernel.
1328  cfgInstructions.exclude_hv(true)
1329  .exclude_host(true);
1330 
1331  if (period) {
1332  // Setup a sampling counter if that has been requested.
1333  cfgInstructions.wakeupEvents(1)
1334  .samplePeriod(period);
1335  }
1336 
1337  // We need to detach and re-attach the counter to reliably change
1338  // sampling settings. See PerfKvmCounter::period() for details.
1339  if (hwInstructions.attached())
1341  assert(hwCycles.attached());
1342  hwInstructions.attach(cfgInstructions,
1343  0, // TID (0 => currentThread)
1344  hwCycles);
1345 
1346  if (period)
1348 
1349  activeInstPeriod = period;
1350 }
KvmVM::kvm
Kvm * kvm
Global KVM interface.
Definition: vm.hh:409
BaseKvmCPU::tickEvent
EventFunctionWrapper tickEvent
Definition: base.hh:705
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:183
SimpleThread::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: simple_thread.cc:122
BaseKvmCPU::StatGroup::numHalt
Stats::Scalar numHalt
Definition: base.hh:793
BaseKvmCPU::setOneReg
void setOneReg(uint64_t id, const void *addr)
Get/Set single register using the KVM_(SET|GET)_ONE_REG API.
Definition: base.cc:841
BaseKvmCPU::kvmNonMaskableInterrupt
void kvmNonMaskableInterrupt()
Send a non-maskable interrupt to the guest.
Definition: base.cc:782
Event::scheduled
bool scheduled() const
Determine if the current event is scheduled.
Definition: eventq.hh:460
BaseKvmCPU::alwaysSyncTC
const bool alwaysSyncTC
Be conservative and always synchronize the thread context on KVM entry/exit.
Definition: base.hh:620
BaseKvmCPU::Running
@ Running
Running normally.
Definition: base.hh:194
Kvm
KVM parent interface.
Definition: vm.hh:72
BaseKvmCPU::pageSize
const long pageSize
Cached page size of the host.
Definition: base.hh:703
warn
#define warn(...)
Definition: logging.hh:239
BaseKvmCPU::setupSignalHandler
void setupSignalHandler()
Setup a signal handler to catch the timer signal used to switch back to the monitor.
Definition: base.cc:1183
BaseKvmCPU::setupInstStop
void setupInstStop()
Setup an instruction break if there is one pending.
Definition: base.cc:1303
BaseKvmCPU::vcpuThread
pthread_t vcpuThread
ID of the vCPU thread.
Definition: base.hh:638
BaseKvmCPU::kvmStateDirty
bool kvmStateDirty
Is the KVM state dirty? Set to true to force an update of the KVM vCPU state upon the next call to kv...
Definition: base.hh:632
system.hh
Kvm::getVCPUMMapSize
int getVCPUMMapSize() const
Get the size of the MMAPed parameter area used to communicate vCPU parameters between the kernel and ...
Definition: vm.hh:88
BaseKvmCPU::ioctlRun
void ioctlRun()
Execute the KVM_RUN ioctl.
Definition: base.cc:1293
BaseKvmCPU::vm
KvmVM & vm
Definition: base.hh:150
BaseTLB::Read
@ Read
Definition: tlb.hh:57
hack_once
#define hack_once(...)
Definition: logging.hh:245
BaseKvmCPU::dataPort
KVMCpuPort dataPort
Port for data requests.
Definition: base.hh:611
KVM_KICK_SIGNAL
#define KVM_KICK_SIGNAL
Signal to use to trigger exits from KVM.
Definition: base.hh:56
EventBase::CPU_Tick_Pri
static const Priority CPU_Tick_Pri
CPU ticks must come after other associated CPU events (such as writebacks).
Definition: eventq.hh:199
data
const char data[]
Definition: circlebuf.test.cc:42
BaseKvmCPU::StatGroup::numIO
Stats::Scalar numIO
Definition: base.hh:792
BaseKvmCPU::getContext
ThreadContext * getContext(int tn) override
Given a thread num get tho thread context for it.
Definition: base.cc:540
BaseCPU::init
void init() override
Definition: base.cc:267
PerfKvmCounterConfig::exclude_host
PerfKvmCounterConfig & exclude_host(bool val)
Exclude the events from the host (i.e., only include events from the guest system).
Definition: perfevent.hh:141
BaseKvmCPU::StatGroup::numMMIO
Stats::Scalar numMMIO
Definition: base.hh:790
BaseKvmCPU::takeOverFrom
void takeOverFrom(BaseCPU *cpu) override
Load the state of a CPU from the previous CPU object, invoked on all new CPUs that are about to be sw...
Definition: base.cc:435
ThreadID
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:227
ThreadState::lastActivate
Tick lastActivate
Last time activate was called on this thread.
Definition: thread_state.hh:145
BaseKvmCPU::getAndFormatOneReg
std::string getAndFormatOneReg(uint64_t id) const
Get and format one register for printout.
Definition: base.cc:875
ArmISA::si
Bitfield< 6 > si
Definition: miscregs_types.hh:766
PerfKvmCounterConfig::samplePeriod
PerfKvmCounterConfig & samplePeriod(uint64_t period)
Set the initial sample period (overflow count) of an event.
Definition: perfevent.hh:85
BaseKvmCPU::StatGroup::numVMHalfEntries
Stats::Scalar numVMHalfEntries
Definition: base.hh:788
BaseKvmCPU::discardPendingSignal
bool discardPendingSignal(int signum) const
Discard a (potentially) pending signal.
Definition: base.cc:1212
BaseKvmCPU::_status
Status _status
CPU run state.
Definition: base.hh:229
X86ISA::lock
Bitfield< 5 > lock
Definition: types.hh:77
BaseKvmCPU::BaseKvmCPU
BaseKvmCPU(BaseKvmCPUParams *params)
Definition: base.cc:62
BaseTLB::Mode
Mode
Definition: tlb.hh:57
MemCmd::ReadReq
@ ReadReq
Definition: packet.hh:82
PerfKvmCounterConfig::wakeupEvents
PerfKvmCounterConfig & wakeupEvents(uint32_t events)
Set the number of samples that need to be triggered before reporting data as being available on the p...
Definition: perfevent.hh:98
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
BaseKvmCPU::setFPUState
void setFPUState(const struct kvm_fpu &state)
Definition: base.cc:833
BaseKvmCPU::setupInstCounter
void setupInstCounter(uint64_t period=0)
Setup the guest instruction counter.
Definition: base.cc:1315
BaseKvmCPU::setRegisters
void setRegisters(const struct kvm_regs &regs)
Definition: base.cc:805
SimpleThread::suspend
void suspend() override
Set the status to Suspended.
Definition: simple_thread.cc:148
BaseKvmCPU::handleKvmExitException
virtual Tick handleKvmExitException()
An unhandled virtualization exception occured.
Definition: base.cc:1053
BaseKvmCPU::KVMCpuPort::recvReqRetry
void recvReqRetry() override
Called by the peer if sendTimingReq was called on this peer (causing recvTimingReq to be called on th...
Definition: base.cc:206
PerfKvmCounter::detach
void detach()
Detach a counter from PerfEvent.
Definition: perfevent.cc:93
SimpleThread::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: simple_thread.cc:130
RequestPtr
std::shared_ptr< Request > RequestPtr
Definition: request.hh:82
DTRACE
#define DTRACE(x)
Definition: debug.hh:146
BaseKvmCPU::switchOut
void switchOut() override
Prepare for another CPU to take over execution.
Definition: base.cc:421
FullSystem
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition: root.cc:132
SimpleThread::status
Status status() const override
Definition: simple_thread.hh:242
BaseCPU::numCycles
Stats::Scalar numCycles
Definition: base.hh:588
BaseKvmCPU::handleKvmExit
virtual Tick handleKvmExit()
Main kvmRun exit handler, calls the relevant handleKvmExit* depending on exit type.
Definition: base.cc:946
BaseKvmCPU::getOneReg
void getOneReg(uint64_t id, void *addr) const
Definition: base.cc:858
BaseKvmCPU::StatGroup::numVMExits
Stats::Scalar numVMExits
Definition: base.hh:787
PerfKvmCounterConfig
PerfEvent counter configuration.
Definition: perfevent.hh:51
BaseKvmCPU::getHostCycles
virtual uint64_t getHostCycles() const
Get the value of the hardware cycle counter in the guest.
Definition: base.cc:678
X86ISA::reg
Bitfield< 5, 3 > reg
Definition: types.hh:87
BaseKvmCPU::KVMCpuPort::submitIO
Tick submitIO(PacketPtr pkt)
Interface to send Atomic or Timing IO request.
Definition: base.cc:171
KvmVM::cpuStartup
void cpuStartup()
VM CPU initialization code.
Definition: vm.cc:333
BaseKvmCPU::StatGroup::numCoalescedMMIO
Stats::Scalar numCoalescedMMIO
Definition: base.hh:791
BaseKvmCPU::handleKvmExitUnknown
virtual Tick handleKvmExitUnknown()
An unknown architecture dependent error occurred when starting the vCPU.
Definition: base.cc:1045
EventQueue::ScopedRelease
Definition: eventq.hh:709
BaseCPU::updateCycleCounters
void updateCycleCounters(CPUState state)
base method keeping track of cycle progression
Definition: base.hh:517
SimpleThread
The SimpleThread object provides a combination of the ThreadState object and the ThreadContext interf...
Definition: simple_thread.hh:89
EventQueue::nextTick
Tick nextTick() const
Definition: eventq.hh:836
EventFunctionWrapper
Definition: eventq.hh:1101
DrainState::Drained
@ Drained
Buffers drained, ready for serialization/handover.
BaseKvmCPU::stats
BaseKvmCPU::StatGroup stats
MemCmd::WriteReq
@ WriteReq
Definition: packet.hh:85
DrainState
DrainState
Object drain/handover states.
Definition: drain.hh:71
BaseKvmCPU::kvmInterrupt
void kvmInterrupt(const struct kvm_interrupt &interrupt)
Send a normal interrupt to the guest.
Definition: base.cc:790
PerfKvmCounter::enableSignals
void enableSignals(pid_t tid, int signal)
Enable signal delivery to a thread on counter overflow.
Definition: perfevent.cc:144
X86ISA::system
Bitfield< 15 > system
Definition: misc.hh:997
Counter
int64_t Counter
Statistics counter type.
Definition: types.hh:58
BaseKvmCPU::vcpuFD
int vcpuFD
KVM vCPU file descriptor.
Definition: base.hh:685
cp
Definition: cprintf.cc:40
BaseKvmCPU::totalOps
Counter totalOps() const override
Definition: base.cc:555
BaseKvmCPU::syncThreadContext
void syncThreadContext()
Update a thread context if the KVM state is dirty with respect to the cached thread context.
Definition: base.cc:922
KvmVM::createVCPU
int createVCPU(long vcpuID)
Create a new vCPU within a VM.
Definition: vm.cc:547
ThreadContext::initMemProxies
virtual void initMemProxies(ThreadContext *tc)=0
Initialise the physical and virtual port proxies and tie them to the data port of the CPU.
BaseKvmCPU::RunningServiceCompletion
@ RunningServiceCompletion
Service completion in progress.
Definition: base.hh:225
BaseKvmCPU::thread
SimpleThread * thread
A cached copy of a thread's state in the form of a SimpleThread object.
Definition: base.hh:143
ArmISA::ss
Bitfield< 21 > ss
Definition: miscregs_types.hh:56
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
BaseKvmCPU::suspendContext
void suspendContext(ThreadID thread_num) override
Notify the CPU that the indicated context is now suspended.
Definition: base.cc:502
BaseKvmCPU::handleKvmExitHypercall
virtual Tick handleKvmExitHypercall()
The guest requested a monitor service using a hypercall.
Definition: base.cc:1031
BaseKvmCPU::unserializeThread
void unserializeThread(CheckpointIn &cp, ThreadID tid) override
Unserialize one thread.
Definition: base.cc:293
SimpleThread::getTC
ThreadContext * getTC()
Returns the pointer to this SimpleThread's ThreadContext.
Definition: simple_thread.hh:169
Event
Definition: eventq.hh:246
ThreadState::lastSuspend
Tick lastSuspend
Last time suspend was called on this thread.
Definition: thread_state.hh:148
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
ADD_STAT
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:67
BaseKvmCPU::handleKvmExitIO
virtual Tick handleKvmExitIO()
The guest performed a legacy IO request (out/inp on x86)
Definition: base.cc:1023
ThreadContext::Halted
@ Halted
Permanently shut down.
Definition: thread_context.hh:115
BaseKvmCPU::StatGroup::numInterrupts
Stats::Scalar numInterrupts
Definition: base.hh:794
MemCmd
Definition: packet.hh:71
BaseKvmCPU::perfControlledByTimer
bool perfControlledByTimer
Does the runTimer control the performance counters?
Definition: base.hh:768
Fault
std::shared_ptr< FaultBase > Fault
Definition: types.hh:240
PerfKvmCounter::start
void start()
Start counting.
Definition: perfevent.cc:107
ArmISA::sa
Bitfield< 3 > sa
Definition: miscregs_types.hh:386
EventQueue::empty
bool empty() const
Returns true if no events are queued.
Definition: eventq.hh:891
BaseKvmCPU::setupCounters
void setupCounters()
Setup hardware performance counters.
Definition: base.cc:1240
BaseKvmCPU::startup
void startup() override
Definition: base.cc:117
BaseCPU::threadContexts
std::vector< ThreadContext * > threadContexts
Definition: base.hh:252
process.hh
BaseKvmCPU::haltContext
void haltContext(ThreadID thread_num) override
Notify the CPU that the indicated context is now halted.
Definition: base.cc:532
BaseKvmCPU::kvmRun
virtual Tick kvmRun(Tick ticks)
Request KVM to run the guest for a given number of ticks.
Definition: base.cc:684
BaseKvmCPU::StatGroup::StatGroup
StatGroup(Stats::Group *parent)
Definition: base.cc:261
System::bypassCaches
bool bypassCaches() const
Should caches be bypassed?
Definition: system.hh:279
M5_FALLTHROUGH
#define M5_FALLTHROUGH
Definition: compiler.hh:84
ThreadContext::contextId
virtual ContextID contextId() const =0
curEventQueue
EventQueue * curEventQueue()
Definition: eventq.hh:83
BaseKvmCPU::StatGroup::numExitSignal
Stats::Scalar numExitSignal
Definition: base.hh:789
BaseKvmCPU::RunningMMIOPending
@ RunningMMIOPending
Timing MMIO request in flight or stalled.
Definition: base.hh:216
Request::UNCACHEABLE
@ UNCACHEABLE
The request is to an uncacheable address.
Definition: request.hh:114
BaseKvmCPU::doMMIOAccess
Tick doMMIOAccess(Addr paddr, void *data, int size, bool write)
Inject a memory mapped IO request into gem5.
Definition: base.cc:1070
BaseKvmCPU::deviceEventQueue
EventQueue * deviceEventQueue()
Get a pointer to the event queue owning devices.
Definition: base.hh:427
BaseKvmCPU::dump
virtual void dump() const
Dump the internal state to the terminal.
Definition: base.cc:562
BaseKvmCPU::startupThread
void startupThread()
Thread-specific initialization.
Definition: base.cc:232
NoFault
constexpr decltype(nullptr) NoFault
Definition: types.hh:245
ThreadContext::status
virtual Status status() const =0
onKickSignal
static void onKickSignal(int signo, siginfo_t *si, void *data)
Dummy handler for KVM kick signals.
Definition: base.cc:1178
BaseKvmCPU::getFPUState
void getFPUState(struct kvm_fpu &state) const
Get/Set the guest FPU/vector state.
Definition: base.cc:826
BaseKvmCPU::kick
void kick() const
Force an exit from KVM.
Definition: base.hh:128
BaseKvmCPU::notifyFork
void notifyFork() override
Definition: base.cc:398
Kvm::capCoalescedMMIO
int capCoalescedMMIO() const
Check if coalesced MMIO is supported and which page in the MMAP'ed structure it stores requests in.
Definition: vm.cc:123
ProbePoints::Packet
ProbePointArg< PacketInfo > Packet
Packet probe point.
Definition: mem.hh:103
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
BaseKvmCPU::kvmRunDrain
virtual Tick kvmRunDrain()
Request the CPU to run until draining completes.
Definition: base.cc:664
BaseCPU::params
const Params * params() const
Definition: base.hh:296
BaseKvmCPU::drain
DrainState drain() override
Definition: base.cc:304
name
const std::string & name()
Definition: trace.cc:50
base.hh
SimpleThread::activate
void activate() override
Set the status to Active.
Definition: simple_thread.cc:137
BaseKvmCPU::tick
void tick()
Execute the CPU until the next event in the main event queue or until the guest needs service from ge...
Definition: base.cc:568
BaseKvmCPU::ioctl
int ioctl(int request, long p1) const
vCPU ioctl interface.
Definition: base.cc:1132
BaseKvmCPU::threadContextDirty
bool threadContextDirty
Is the gem5 context dirty? Set to true to force an update of the KVM vCPU state upon the next call to...
Definition: base.hh:626
BaseKvmCPU::verifyMemoryMode
void verifyMemoryMode() const override
Verify that the system is in a memory mode supported by the CPU.
Definition: base.cc:457
BaseKvmCPU::vcpuID
const long vcpuID
KVM internal ID of the vCPU.
Definition: base.hh:635
BaseTLB::Write
@ Write
Definition: tlb.hh:57
BaseCPU
Definition: cpu_dummy.hh:43
SimpleThread::comInstEventQueue
EventQueue comInstEventQueue
An instruction-based event queue.
Definition: simple_thread.hh:129
BaseKvmCPU::ctrInsts
Counter ctrInsts
Number of instructions executed by the CPU.
Definition: base.hh:800
BaseKvmCPU::finishMMIOPending
void finishMMIOPending()
Callback from KvmCPUPort to transition the CPU out of RunningMMIOPending when all timing requests hav...
Definition: base.cc:222
BaseKvmCPU::RunningService
@ RunningService
Requiring service at the beginning of the next cycle.
Definition: base.hh:208
BaseKvmCPU::~BaseKvmCPU
virtual ~BaseKvmCPU()
Definition: base.cc:98
EventQueue::ScopedMigration
Definition: eventq.hh:665
ThreadContext::Suspended
@ Suspended
Temporarily inactive.
Definition: thread_context.hh:106
BaseKvmCPU::totalInsts
Counter totalInsts() const override
Definition: base.cc:549
BaseKvmCPU::activeInstPeriod
uint64_t activeInstPeriod
Currently active instruction count breakpoint.
Definition: base.hh:736
inform
#define inform(...)
Definition: logging.hh:240
BaseKvmCPU::updateThreadContext
virtual void updateThreadContext()=0
Update the current thread context with the KVM state.
BaseKvmCPU::Idle
@ Idle
Context not scheduled in KVM.
Definition: base.hh:188
BaseKvmCPU::runTimer
std::unique_ptr< BaseKvmTimer > runTimer
Timer used to force execution into the monitor after a specified number of simulation tick equivalent...
Definition: base.hh:777
BaseKvmCPU::tc
ThreadContext * tc
ThreadContext object, provides an interface for external objects to modify this thread's state.
Definition: base.hh:148
PerfKvmCounter::stop
void stop()
Stop counting.
Definition: perfevent.cc:114
BaseKvmCPU::handleKvmExitIRQWindowOpen
virtual Tick handleKvmExitIRQWindowOpen()
The guest exited because an interrupt window was requested.
Definition: base.cc:1037
BaseCPU::system
System * system
Definition: base.hh:371
Packet::dataStatic
void dataStatic(T *p)
Set the data pointer to the following value that should not be freed.
Definition: packet.hh:1107
PerfKvmCounter::read
uint64_t read() const
Read the current value of a counter.
Definition: perfevent.cc:135
System::totalNumInsts
Counter totalNumInsts
Definition: system.hh:603
BaseCPU::takeOverFrom
virtual void takeOverFrom(BaseCPU *cpu)
Load the state of a CPU from the previous CPU object, invoked on all new CPUs that are about to be sw...
Definition: base.cc:546
Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:257
BaseKvmCPU::getSpecialRegisters
void getSpecialRegisters(struct kvm_sregs &regs) const
Definition: base.cc:812
BaseCPU::switchOut
virtual void switchOut()
Prepare for another CPU to take over execution.
Definition: base.cc:532
Stats::Group
Statistics container.
Definition: group.hh:83
BaseKvmCPU::vcpuMMapSize
int vcpuMMapSize
Size of MMAPed kvm_run area.
Definition: base.hh:687
BaseKvmCPU::StatGroup::committedInsts
Stats::Scalar committedInsts
Definition: base.hh:786
BaseKvmCPU::KVMCpuPort::activeMMIOReqs
unsigned int activeMMIOReqs
Number of MMIO requests in flight.
Definition: base.hh:602
PerfKvmCounterConfig::disabled
PerfKvmCounterConfig & disabled(bool val)
Don't start the performance counter automatically when attaching it.
Definition: perfevent.hh:110
addr
ip6_addr_t addr
Definition: inet.hh:423
BaseKvmCPU::drainResume
void drainResume() override
Definition: base.cc:375
BaseKvmCPU::_kvmRun
struct kvm_run * _kvmRun
Pointer to the kvm_run structure used to communicate parameters with KVM.
Definition: base.hh:696
BaseKvmCPU::syncKvmState
void syncKvmState()
Update the KVM if the thread context is dirty.
Definition: base.cc:934
BaseCPU::numThreads
ThreadID numThreads
Number of threads we're actually simulating (<= SMT_MAX_THREADS).
Definition: base.hh:363
BaseKvmCPU::updateKvmState
virtual void updateKvmState()=0
Update the KVM state from the current thread context.
BaseCPU::dataRequestorId
RequestorID dataRequestorId() const
Reads this CPU's unique data requestor ID.
Definition: base.hh:184
Cycles
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:83
BaseKvmCPU::flushCoalescedMMIO
Tick flushCoalescedMMIO()
Service MMIO requests in the mmioRing.
Definition: base.cc:1141
ThreadContext::Active
@ Active
Running.
Definition: thread_context.hh:102
BaseKvmCPU::archIsDrained
virtual bool archIsDrained() const
Is the architecture specific code in a state that prevents draining?
Definition: base.hh:521
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
BaseCPU::startup
void startup() override
Definition: base.cc:310
Stats
Definition: statistics.cc:61
BaseKvmCPU::StatGroup::numHypercalls
Stats::Scalar numHypercalls
Definition: base.hh:795
BaseKvmCPU::mmioRing
struct kvm_coalesced_mmio_ring * mmioRing
Coalesced MMIO ring buffer.
Definition: base.hh:701
BaseKvmCPU::KVMCpuPort::recvTimingResp
bool recvTimingResp(PacketPtr pkt) override
Receive a timing response from the peer.
Definition: base.cc:189
BaseKvmCPU::setSpecialRegisters
void setSpecialRegisters(const struct kvm_sregs &regs)
Definition: base.cc:819
BaseCPU::CPU_STATE_SLEEP
@ CPU_STATE_SLEEP
Definition: base.hh:509
BaseKvmCPU::wakeup
void wakeup(ThreadID tid=0) override
Definition: base.cc:466
PerfKvmCounter::attached
bool attached() const
Check if a counter is attached.
Definition: perfevent.hh:228
ArmISA::vm
Bitfield< 0 > vm
Definition: miscregs_types.hh:281
PosixKvmTimer
Timer based on standard POSIX timers.
Definition: timer.hh:184
BaseKvmCPU::getRegisters
void getRegisters(struct kvm_regs &regs) const
Get/Set the register state of the guest vCPU.
Definition: base.cc:798
BaseKvmCPU::KVMCpuPort::pendingMMIOPkts
std::queue< PacketPtr > pendingMMIOPkts
Pending MMIO packets.
Definition: base.hh:599
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
BaseKvmCPU::hostFactor
float hostFactor
Host factor as specified in the configuration.
Definition: base.hh:780
BaseKvmCPU::tryDrain
bool tryDrain()
Try to drain the CPU if a drain is pending.
Definition: base.cc:1271
BaseCPU::switchedOut
bool switchedOut() const
Determine if the CPU is switched out.
Definition: base.hh:352
PerfKvmCounter::attach
void attach(PerfKvmCounterConfig &config, pid_t tid)
Attach a counter.
Definition: perfevent.hh:204
PerfKvmCounterConfig::exclude_hv
PerfKvmCounterConfig & exclude_hv(bool val)
Exclude the hyper visor (i.e., only include events from the guest system).
Definition: perfevent.hh:156
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:219
BaseKvmCPU::hwInstructions
PerfKvmCounter hwInstructions
Guest instruction counter.
Definition: base.hh:759
CheckpointIn
Definition: serialize.hh:67
BaseKvmCPU::hwCycles
PerfKvmCounter hwCycles
Guest cycle counter.
Definition: base.hh:746
BaseKvmCPU::activateContext
void activateContext(ThreadID thread_num) override
Notify the CPU that the indicated context is now active.
Definition: base.cc:484
BaseKvmCPU::deallocateContext
void deallocateContext(ThreadID thread_num)
Definition: base.cc:525
BaseKvmCPU::setSignalMask
void setSignalMask(const sigset_t *mask)
Set the signal mask used in kvmRun()
Definition: base.cc:1111
BaseKvmCPU::init
void init() override
Definition: base.cc:106
MaxTick
const Tick MaxTick
Definition: types.hh:65
BaseTLB::finalizePhysical
virtual Fault finalizePhysical(const RequestPtr &req, ThreadContext *tc, Mode mode) const =0
Do post-translation physical address finalization.
BaseKvmCPU::serializeThread
void serializeThread(CheckpointOut &cp, ThreadID tid) const override
Serialize a single thread.
Definition: base.cc:280
ThreadContext::getDTBPtr
virtual BaseTLB * getDTBPtr()=0
DrainState::Draining
@ Draining
Draining buffers pending serialization/handover.
PerfKvmCounterConfig::pinned
PerfKvmCounterConfig & pinned(bool val)
Force the group to be on the active all the time (i.e., disallow multiplexing).
Definition: perfevent.hh:123
ArmISA::mask
Bitfield< 28, 24 > mask
Definition: miscregs_types.hh:711
ArmISA::id
Bitfield< 33 > id
Definition: miscregs_types.hh:247
PerfKvmTimer
PerfEvent based timer using the host's CPU cycle counter.
Definition: timer.hh:218
BaseKvmCPU::handleKvmExitFailEntry
virtual Tick handleKvmExitFailEntry()
KVM failed to start the virtualized CPU.
Definition: base.cc:1062
BaseKvmCPU::Status
Status
Definition: base.hh:178
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
curTick
Tick curTick()
The current simulated tick.
Definition: core.hh:45
BaseKvmCPU::KVMCpuPort::nextIOState
Status nextIOState() const
Returns next valid state after one or more IO accesses.
Definition: base.cc:164

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17