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

Generated on Wed May 4 2022 12:13:52 for gem5 by doxygen 1.8.17