gem5  v20.0.0.3
base.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef __CPU_KVM_BASE_HH__
39 #define __CPU_KVM_BASE_HH__
40 
41 #include <pthread.h>
42 
43 #include <csignal>
44 #include <memory>
45 #include <queue>
46 
47 #include "base/statistics.hh"
48 #include "cpu/kvm/perfevent.hh"
49 #include "cpu/kvm/timer.hh"
50 #include "cpu/kvm/vm.hh"
51 #include "cpu/base.hh"
52 #include "cpu/simple_thread.hh"
53 #include "sim/faults.hh"
54 
56 #define KVM_KICK_SIGNAL SIGRTMIN
57 
58 // forward declarations
59 class ThreadContext;
60 struct BaseKvmCPUParams;
61 
77 class BaseKvmCPU : public BaseCPU
78 {
79  public:
80  BaseKvmCPU(BaseKvmCPUParams *params);
81  virtual ~BaseKvmCPU();
82 
83  void init() override;
84  void startup() override;
85  void regStats() override;
86 
87  void serializeThread(CheckpointOut &cp, ThreadID tid) const override;
88  void unserializeThread(CheckpointIn &cp, ThreadID tid) override;
89 
90  DrainState drain() override;
91  void drainResume() override;
92  void notifyFork() override;
93 
94  void switchOut() override;
95  void takeOverFrom(BaseCPU *cpu) override;
96 
97  void verifyMemoryMode() const override;
98 
99  Port &getDataPort() override { return dataPort; }
100  Port &getInstPort() override { return instPort; }
101 
102  void wakeup(ThreadID tid = 0) override;
103  void activateContext(ThreadID thread_num) override;
104  void suspendContext(ThreadID thread_num) override;
105  void deallocateContext(ThreadID thread_num);
106  void haltContext(ThreadID thread_num) override;
107 
108  long getVCpuID() const { return vcpuID; }
109  ThreadContext *getContext(int tn) override;
110 
111  Counter totalInsts() const override;
112  Counter totalOps() const override;
113 
118  void finishMMIOPending();
119 
121  virtual void dump() const;
122 
129  void kick() const { pthread_kill(vcpuThread, KVM_KICK_SIGNAL); }
130 
145 
150 
152 
153  protected:
179  enum Status {
227  };
228 
231 
236  void tick();
237 
251  virtual uint64_t getHostCycles() const;
252 
279  virtual Tick kvmRun(Tick ticks);
280 
292  virtual Tick kvmRunDrain();
293 
298  struct kvm_run *getKvmRunState() { return _kvmRun; };
299 
308  uint8_t *getGuestData(uint64_t offset) const {
309  return (uint8_t *)_kvmRun + offset;
310  };
311 
322 
333  void kvmInterrupt(const struct kvm_interrupt &interrupt);
334 
347  void getRegisters(struct kvm_regs &regs) const;
348  void setRegisters(const struct kvm_regs &regs);
349  void getSpecialRegisters(struct kvm_sregs &regs) const;
350  void setSpecialRegisters(const struct kvm_sregs &regs);
357  void getFPUState(struct kvm_fpu &state) const;
358  void setFPUState(const struct kvm_fpu &state);
367  void setOneReg(uint64_t id, const void *addr);
368  void setOneReg(uint64_t id, uint64_t value) { setOneReg(id, &value); }
369  void setOneReg(uint64_t id, uint32_t value) { setOneReg(id, &value); }
370  void getOneReg(uint64_t id, void *addr) const;
371  uint64_t getOneRegU64(uint64_t id) const {
372  uint64_t value;
373  getOneReg(id, &value);
374  return value;
375  }
376  uint32_t getOneRegU32(uint64_t id) const {
377  uint32_t value;
378  getOneReg(id, &value);
379  return value;
380  }
391  std::string getAndFormatOneReg(uint64_t id) const;
392 
402  virtual void updateKvmState() = 0;
403 
412  virtual void updateThreadContext() = 0;
413 
418  void syncThreadContext();
419 
429 
433  void syncKvmState();
443  virtual Tick handleKvmExit();
444 
450  virtual Tick handleKvmExitIO();
451 
457  virtual Tick handleKvmExitHypercall();
458 
469 
482  virtual Tick handleKvmExitUnknown();
483 
493  virtual Tick handleKvmExitException();
494 
503  virtual Tick handleKvmExitFailEntry();
522  virtual bool archIsDrained() const { return true; }
523 
533  Tick doMMIOAccess(Addr paddr, void *data, int size, bool write);
534 
548  void setSignalMask(const sigset_t *mask);
564  int ioctl(int request, long p1) const;
565  int ioctl(int request, void *p1) const {
566  return ioctl(request, (long)p1);
567  }
568  int ioctl(int request) const {
569  return ioctl(request, 0L);
570  }
578  class KVMCpuPort : public MasterPort
579  {
580 
581  public:
582  KVMCpuPort(const std::string &_name, BaseKvmCPU *_cpu)
583  : MasterPort(_name, _cpu), cpu(_cpu), activeMMIOReqs(0)
584  { }
590  Tick submitIO(PacketPtr pkt);
591 
593  Status nextIOState() const;
594 
595  protected:
598 
600  std::queue<PacketPtr> pendingMMIOPkts;
601 
603  unsigned int activeMMIOReqs;
604 
605  bool recvTimingResp(PacketPtr pkt) override;
606 
607  void recvReqRetry() override;
608 
609  };
610 
613 
616 
621  const bool alwaysSyncTC;
622 
628 
634 
636  const long vcpuID;
637 
639  pthread_t vcpuThread;
640 
641  private:
650 
655  void setupSignalHandler();
656 
663  bool discardPendingSignal(int signum) const;
664 
677  void startupThread();
678 
680  bool tryDrain();
681 
683  void ioctlRun();
684 
686  int vcpuFD;
697  struct kvm_run *_kvmRun;
702  struct kvm_coalesced_mmio_ring *mmioRing;
704  const long pageSize;
705 
707 
718  void setupInstStop();
719 
722  void setupCounters();
723 
734  void setupInstCounter(uint64_t period = 0);
735 
738 
748 
761 
778  std::unique_ptr<BaseKvmTimer> runTimer;
779 
781  float hostFactor;
782 
783  public:
784  /* @{ */
795  /* @} */
796 
799 };
800 
801 #endif
A MasterPort is a specialisation of a BaseMasterPort, which implements the default protocol for the t...
Definition: port.hh:71
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:610
Ports are used to interface objects to each other.
Definition: port.hh:56
int ioctl(int request, void *p1) const
Definition: base.hh:565
void setupCounters()
Setup hardware performance counters.
Definition: base.cc:1277
EventQueue * deviceEventQueue()
Get a pointer to the event queue owning devices.
Definition: base.hh:428
void finishMMIOPending()
Callback from KvmCPUPort to transition the CPU out of RunningMMIOPending when all timing requests hav...
Definition: base.cc:224
Counter ctrInsts
Number of instructions executed by the CPU.
Definition: base.hh:798
Bitfield< 7, 0 > L
Definition: int.hh:57
uint64_t activeInstPeriod
Currently active instruction count breakpoint.
Definition: base.hh:737
Timing MMIO request in flight or stalled.
Definition: base.hh:217
int ioctl(int request) const
Definition: base.hh:568
virtual void updateThreadContext()=0
Update the current thread context with the KVM state.
BaseKvmCPU * cpu
KVM cpu pointer for finishMMIOPending() callback.
Definition: base.hh:597
virtual Tick handleKvmExitIO()
The guest performed a legacy IO request (out/inp on x86)
Definition: base.cc:1060
bool recvTimingResp(PacketPtr pkt) override
Receive a timing response from the peer.
Definition: base.cc:191
Tick submitIO(PacketPtr pkt)
Interface to send Atomic or Timing IO request.
Definition: base.cc:173
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:627
bool perfControlledByTimer
Does the runTimer control the performance counters?
Definition: base.hh:769
void getSpecialRegisters(struct kvm_sregs &regs) const
Definition: base.cc:849
virtual ~BaseKvmCPU()
Definition: base.cc:98
void notifyFork() override
Notify a child process of a fork.
Definition: base.cc:440
virtual Tick kvmRun(Tick ticks)
Request KVM to run the guest for a given number of ticks.
Definition: base.cc:721
ip6_addr_t addr
Definition: inet.hh:330
std::unique_ptr< BaseKvmTimer > runTimer
Timer used to force execution into the monitor after a specified number of simulation tick equivalent...
Definition: base.hh:778
ThreadContext * tc
ThreadContext object, provides an interface for external objects to modify this thread&#39;s state...
Definition: base.hh:149
void kvmNonMaskableInterrupt()
Send a non-maskable interrupt to the guest.
Definition: base.cc:819
Stats::Scalar numHalt
Definition: base.hh:792
virtual Tick handleKvmExitIRQWindowOpen()
The guest exited because an interrupt window was requested.
Definition: base.cc:1074
KVMCpuPort instPort
Unused dummy port for the instruction interface.
Definition: base.hh:615
void verifyMemoryMode() const override
Verify that the system is in a memory mode supported by the CPU.
Definition: base.cc:499
void wakeup(ThreadID tid=0) override
Definition: base.cc:508
The SimpleThread object provides a combination of the ThreadState object and the ThreadContext interf...
virtual void dump() const
Dump the internal state to the terminal.
Definition: base.cc:604
Bitfield< 23, 0 > offset
Definition: types.hh:152
float hostFactor
Host factor as specified in the configuration.
Definition: base.hh:781
bool tryDrain()
Try to drain the CPU if a drain is pending.
Definition: base.cc:1308
void getFPUState(struct kvm_fpu &state) const
Get/Set the guest FPU/vector state.
Definition: base.cc:863
void setOneReg(uint64_t id, uint32_t value)
Definition: base.hh:369
Counter totalInsts() const override
Definition: base.cc:591
Stats::Scalar numExitSignal
Definition: base.hh:788
virtual Tick kvmRunDrain()
Request the CPU to run until draining completes.
Definition: base.cc:701
Base class for KVM based CPU models.
Definition: base.hh:77
Definition: cprintf.cc:40
Tick flushCoalescedMMIO()
Service MMIO requests in the mmioRing.
Definition: base.cc:1178
DrainState drain() override
Notify an object that it needs to drain its state.
Definition: base.cc:346
void init() override
init() is called after all C++ SimObjects have been created and all ports are connected.
Definition: base.cc:106
ThreadContext is the external interface to all thread state for anything outside of the CPU...
Declaration of Statistics objects.
This is a simple scalar statistic, like a counter.
Definition: statistics.hh:2505
DrainState
Object drain/handover states.
Definition: drain.hh:71
const long vcpuID
KVM internal ID of the vCPU.
Definition: base.hh:636
void recvReqRetry() override
Called by the peer if sendTimingReq was called on this peer (causing recvTimingReq to be called on th...
Definition: base.cc:208
virtual void updateKvmState()=0
Update the KVM state from the current thread context.
void regStats() override
Callback to set stat parameters.
Definition: base.cc:264
Stats::Scalar numInsts
Definition: base.hh:785
void activateContext(ThreadID thread_num) override
Notify the CPU that the indicated context is now active.
Definition: base.cc:526
Stats::Scalar numInterrupts
Definition: base.hh:793
#define KVM_KICK_SIGNAL
Signal to use to trigger exits from KVM.
Definition: base.hh:56
void setSpecialRegisters(const struct kvm_sregs &regs)
Definition: base.cc:856
void serializeThread(CheckpointOut &cp, ThreadID tid) const override
Serialize a single thread.
Definition: base.cc:322
Stats::Scalar numIO
Definition: base.hh:791
void setupSignalHandler()
Setup a signal handler to catch the timer signal used to switch back to the monitor.
Definition: base.cc:1220
int vcpuMMapSize
Size of MMAPed kvm_run area.
Definition: base.hh:688
virtual Tick handleKvmExitFailEntry()
KVM failed to start the virtualized CPU.
Definition: base.cc:1099
void drainResume() override
Resume execution after a successful drain.
Definition: base.cc:417
struct kvm_run * _kvmRun
Pointer to the kvm_run structure used to communicate parameters with KVM.
Definition: base.hh:697
void syncKvmState()
Update the KVM if the thread context is dirty.
Definition: base.cc:971
Queue of events sorted in time order.
Definition: eventq.hh:613
KvmVM & vm
Definition: base.hh:151
Stats::Scalar numVMExits
Definition: base.hh:786
ThreadContext * getContext(int tn) override
Given a thread num get tho thread context for it.
Definition: base.cc:582
Stats::Scalar numMMIO
Definition: base.hh:789
void setOneReg(uint64_t id, const void *addr)
Get/Set single register using the KVM_(SET|GET)_ONE_REG API.
Definition: base.cc:878
uint64_t Tick
Tick count type.
Definition: types.hh:61
virtual bool archIsDrained() const
Is the architecture specific code in a state that prevents draining?
Definition: base.hh:522
const bool alwaysSyncTC
Be conservative and always synchronize the thread context on KVM entry/exit.
Definition: base.hh:621
uint32_t getOneRegU32(uint64_t id) const
Definition: base.hh:376
void deallocateContext(ThreadID thread_num)
Definition: base.cc:567
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:477
struct kvm_coalesced_mmio_ring * mmioRing
Coalesced MMIO ring buffer.
Definition: base.hh:702
std::string getAndFormatOneReg(uint64_t id) const
Get and format one register for printout.
Definition: base.cc:912
void getRegisters(struct kvm_regs &regs) const
Get/Set the register state of the guest vCPU.
Definition: base.cc:835
pthread_t vcpuThread
ID of the vCPU thread.
Definition: base.hh:639
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:633
void setOneReg(uint64_t id, uint64_t value)
Definition: base.hh:368
PerfKvmCounter hwInstructions
Guest instruction counter.
Definition: base.hh:760
Running normally.
Definition: base.hh:195
PerfKvmCounter hwCycles
Guest cycle counter.
Definition: base.hh:747
Service completion in progress.
Definition: base.hh:226
EventFunctionWrapper tickEvent
Definition: base.hh:706
An instance of a performance counter.
Definition: perfevent.hh:168
Requiring service at the beginning of the next cycle.
Definition: base.hh:209
virtual Tick handleKvmExitException()
An unhandled virtualization exception occured.
Definition: base.cc:1090
Port & getInstPort() override
Purely virtual method that returns a reference to the instruction port.
Definition: base.hh:100
void setSignalMask(const sigset_t *mask)
Set the signal mask used in kvmRun()
Definition: base.cc:1148
Stats::Scalar numVMHalfEntries
Definition: base.hh:787
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:140
int64_t Counter
Statistics counter type.
Definition: types.hh:56
void setupInstStop()
Setup an instruction break if there is one pending.
Definition: base.cc:1340
A Packet is used to encapsulate a transfer between two objects in the memory system (e...
Definition: packet.hh:249
Status _status
CPU run state.
Definition: base.hh:230
bool discardPendingSignal(int signum) const
Discard a (potentially) pending signal.
Definition: base.cc:1249
long getVCpuID() const
Definition: base.hh:108
void kvmInterrupt(const struct kvm_interrupt &interrupt)
Send a normal interrupt to the guest.
Definition: base.cc:827
Stats::Scalar numHypercalls
Definition: base.hh:794
BaseKvmCPU(BaseKvmCPUParams *params)
Definition: base.cc:62
uint8_t * getGuestData(uint64_t offset) const
Retrieve a pointer to guest data stored at the end of the kvm_run structure.
Definition: base.hh:308
void setFPUState(const struct kvm_fpu &state)
Definition: base.cc:870
std::queue< PacketPtr > pendingMMIOPkts
Pending MMIO packets.
Definition: base.hh:600
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:225
const long pageSize
Cached page size of the host.
Definition: base.hh:704
KVM VM container.
Definition: vm.hh:289
KVMCpuPort dataPort
Port for data requests.
Definition: base.hh:612
void ioctlRun()
Execute the KVM_RUN ioctl.
Definition: base.cc:1330
std::ostream CheckpointOut
Definition: serialize.hh:63
Status nextIOState() const
Returns next valid state after one or more IO accesses.
Definition: base.cc:166
void getOneReg(uint64_t id, void *addr) const
Definition: base.cc:895
int vcpuFD
KVM vCPU file descriptor.
Definition: base.hh:686
virtual uint64_t getHostCycles() const
Get the value of the hardware cycle counter in the guest.
Definition: base.cc:715
void setRegisters(const struct kvm_regs &regs)
Definition: base.cc:842
EventQueue * eventQueue() const
Definition: eventq.hh:925
void setupInstCounter(uint64_t period=0)
Setup the guest instruction counter.
Definition: base.cc:1352
void suspendContext(ThreadID thread_num) override
Notify the CPU that the indicated context is now suspended.
Definition: base.cc:544
virtual Tick handleKvmExitHypercall()
The guest requested a monitor service using a hypercall.
Definition: base.cc:1068
void unserializeThread(CheckpointIn &cp, ThreadID tid) override
Unserialize one thread.
Definition: base.cc:335
int ioctl(int request, long p1) const
vCPU ioctl interface.
Definition: base.cc:1169
Bitfield< 3, 0 > mask
Definition: types.hh:62
Context not scheduled in KVM.
Definition: base.hh:189
void startup() override
startup() is the final initialization call before simulation.
Definition: base.cc:117
Counter totalOps() const override
Definition: base.cc:597
virtual Tick handleKvmExitUnknown()
An unknown architecture dependent error occurred when starting the vCPU.
Definition: base.cc:1082
SimpleThread * thread
A cached copy of a thread&#39;s state in the form of a SimpleThread object.
Definition: base.hh:144
KVM memory port.
Definition: base.hh:578
void switchOut() override
Prepare for another CPU to take over execution.
Definition: base.cc:463
KVMCpuPort(const std::string &_name, BaseKvmCPU *_cpu)
Definition: base.hh:582
virtual Tick handleKvmExit()
Main kvmRun exit handler, calls the relevant handleKvmExit* depending on exit type.
Definition: base.cc:983
const char data[]
const Params * params() const
Definition: base.hh:307
unsigned int activeMMIOReqs
Number of MMIO requests in flight.
Definition: base.hh:603
Tick doMMIOAccess(Addr paddr, void *data, int size, bool write)
Inject a memory mapped IO request into gem5.
Definition: base.cc:1107
Port & getDataPort() override
Purely virtual method that returns a reference to the data port.
Definition: base.hh:99
Stats::Scalar numCoalescedMMIO
Definition: base.hh:790
uint64_t getOneRegU64(uint64_t id) const
Definition: base.hh:371
void kick() const
Force an exit from KVM.
Definition: base.hh:129
void startupThread()
Thread-specific initialization.
Definition: base.cc:234
void syncThreadContext()
Update a thread context if the KVM state is dirty with respect to the cached thread context...
Definition: base.cc:959
void haltContext(ThreadID thread_num) override
Notify the CPU that the indicated context is now halted.
Definition: base.cc:574
struct kvm_run * getKvmRunState()
Get a pointer to the kvm_run structure containing all the input and output parameters from kvmRun()...
Definition: base.hh:298

Generated on Fri Jul 3 2020 15:52:59 for gem5 by doxygen 1.8.13