gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  * Authors: Andreas Sandberg
38  */
39 
40 #ifndef __CPU_KVM_BASE_HH__
41 #define __CPU_KVM_BASE_HH__
42 
43 #include <pthread.h>
44 
45 #include <csignal>
46 #include <memory>
47 #include <queue>
48 
49 #include "base/statistics.hh"
50 #include "cpu/kvm/perfevent.hh"
51 #include "cpu/kvm/timer.hh"
52 #include "cpu/kvm/vm.hh"
53 #include "cpu/base.hh"
54 #include "cpu/simple_thread.hh"
55 
57 #define KVM_KICK_SIGNAL SIGRTMIN
58 
59 // forward declarations
60 class ThreadContext;
61 struct BaseKvmCPUParams;
62 
78 class BaseKvmCPU : public BaseCPU
79 {
80  public:
81  BaseKvmCPU(BaseKvmCPUParams *params);
82  virtual ~BaseKvmCPU();
83 
84  void init() override;
85  void startup() override;
86  void regStats() override;
87 
88  void serializeThread(CheckpointOut &cp, ThreadID tid) const override;
89  void unserializeThread(CheckpointIn &cp, ThreadID tid) override;
90 
91  DrainState drain() override;
92  void drainResume() override;
93  void notifyFork() override;
94 
95  void switchOut() override;
96  void takeOverFrom(BaseCPU *cpu) override;
97 
98  void verifyMemoryMode() const override;
99 
100  MasterPort &getDataPort() override { return dataPort; }
101  MasterPort &getInstPort() override { return instPort; }
102 
103  void wakeup(ThreadID tid = 0) override;
104  void activateContext(ThreadID thread_num) override;
105  void suspendContext(ThreadID thread_num) override;
106  void deallocateContext(ThreadID thread_num);
107  void haltContext(ThreadID thread_num) override;
108 
109  long getVCpuID() const { return vcpuID; }
110  ThreadContext *getContext(int tn) override;
111 
112  Counter totalInsts() const override;
113  Counter totalOps() const override;
114 
119  void finishMMIOPending();
120 
122  virtual void dump() const;
123 
130  void kick() const { pthread_kill(vcpuThread, KVM_KICK_SIGNAL); }
131 
146 
151 
153 
154  protected:
180  enum Status {
228  };
229 
232 
237  void tick();
238 
252  virtual uint64_t getHostCycles() const;
253 
280  virtual Tick kvmRun(Tick ticks);
281 
293  virtual Tick kvmRunDrain();
294 
299  struct kvm_run *getKvmRunState() { return _kvmRun; };
300 
309  uint8_t *getGuestData(uint64_t offset) const {
310  return (uint8_t *)_kvmRun + offset;
311  };
312 
323 
334  void kvmInterrupt(const struct kvm_interrupt &interrupt);
335 
348  void getRegisters(struct kvm_regs &regs) const;
349  void setRegisters(const struct kvm_regs &regs);
350  void getSpecialRegisters(struct kvm_sregs &regs) const;
351  void setSpecialRegisters(const struct kvm_sregs &regs);
358  void getFPUState(struct kvm_fpu &state) const;
359  void setFPUState(const struct kvm_fpu &state);
368  void setOneReg(uint64_t id, const void *addr);
369  void setOneReg(uint64_t id, uint64_t value) { setOneReg(id, &value); }
370  void setOneReg(uint64_t id, uint32_t value) { setOneReg(id, &value); }
371  void getOneReg(uint64_t id, void *addr) const;
372  uint64_t getOneRegU64(uint64_t id) const {
373  uint64_t value;
374  getOneReg(id, &value);
375  return value;
376  }
377  uint32_t getOneRegU32(uint64_t id) const {
378  uint32_t value;
379  getOneReg(id, &value);
380  return value;
381  }
392  std::string getAndFormatOneReg(uint64_t id) const;
393 
403  virtual void updateKvmState() = 0;
404 
413  virtual void updateThreadContext() = 0;
414 
419  void syncThreadContext();
420 
424  void syncKvmState();
434  virtual Tick handleKvmExit();
435 
441  virtual Tick handleKvmExitIO();
442 
448  virtual Tick handleKvmExitHypercall();
449 
460 
473  virtual Tick handleKvmExitUnknown();
474 
484  virtual Tick handleKvmExitException();
485 
494  virtual Tick handleKvmExitFailEntry();
513  virtual bool archIsDrained() const { return true; }
514 
524  Tick doMMIOAccess(Addr paddr, void *data, int size, bool write);
525 
539  void setSignalMask(const sigset_t *mask);
555  int ioctl(int request, long p1) const;
556  int ioctl(int request, void *p1) const {
557  return ioctl(request, (long)p1);
558  }
559  int ioctl(int request) const {
560  return ioctl(request, 0L);
561  }
569  class KVMCpuPort : public MasterPort
570  {
571 
572  public:
573  KVMCpuPort(const std::string &_name, BaseKvmCPU *_cpu)
574  : MasterPort(_name, _cpu), cpu(_cpu), activeMMIOReqs(0)
575  { }
581  Tick submitIO(PacketPtr pkt);
582 
584  Status nextIOState() const;
585 
586  protected:
589 
591  std::queue<PacketPtr> pendingMMIOPkts;
592 
594  unsigned int activeMMIOReqs;
595 
596  bool recvTimingResp(PacketPtr pkt) override;
597 
598  void recvReqRetry() override;
599 
600  };
601 
604 
607 
612  const bool alwaysSyncTC;
613 
619 
625 
627  const long vcpuID;
628 
630  pthread_t vcpuThread;
631 
632  private:
633  struct TickEvent : public Event
634  {
636 
638  : Event(CPU_Tick_Pri), cpu(c) {}
639 
640  void process() { cpu.tick(); }
641 
642  const char *description() const {
643  return "BaseKvmCPU tick";
644  }
645  };
646 
655 
660  void setupSignalHandler();
661 
668  bool discardPendingSignal(int signum) const;
669 
682  void startupThread();
683 
685  bool tryDrain();
686 
688  void ioctlRun();
689 
691  int vcpuFD;
702  struct kvm_run *_kvmRun;
707  struct kvm_coalesced_mmio_ring *mmioRing;
709  const long pageSize;
710 
712 
723  void setupInstStop();
724 
727  void setupCounters();
728 
739  void setupInstCounter(uint64_t period = 0);
740 
743 
753 
766 
783  std::unique_ptr<BaseKvmTimer> runTimer;
784 
786  float hostFactor;
787 
788  public:
789  /* @{ */
800  /* @} */
801 
804 };
805 
806 #endif
A MasterPort is a specialisation of a BaseMasterPort, which implements the default protocol for the t...
Definition: port.hh:167
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:611
void setupCounters()
Setup hardware performance counters.
Definition: base.cc:1282
void finishMMIOPending()
Callback from KvmCPUPort to transition the CPU out of RunningMMIOPending when all timing requests hav...
Definition: base.cc:233
Counter ctrInsts
Number of instructions executed by the CPU.
Definition: base.hh:803
Bitfield< 7, 0 > L
Definition: int.hh:59
uint64_t activeInstPeriod
Currently active instruction count breakpoint.
Definition: base.hh:742
Timing MMIO request in flight or stalled.
Definition: base.hh:218
virtual void updateThreadContext()=0
Update the current thread context with the KVM state.
BaseKvmCPU * cpu
KVM cpu pointer for finishMMIOPending() callback.
Definition: base.hh:588
DrainState
Object drain/handover states.
Definition: drain.hh:71
virtual Tick handleKvmExitIO()
The guest performed a legacy IO request (out/inp on x86)
Definition: base.cc:1062
bool recvTimingResp(PacketPtr pkt) override
Receive a timing response from the slave port.
Definition: base.cc:199
Tick submitIO(PacketPtr pkt)
Interface to send Atomic or Timing IO request.
Definition: base.cc:180
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:618
bool perfControlledByTimer
Does the runTimer control the performance counters?
Definition: base.hh:774
static const Priority CPU_Tick_Pri
CPU ticks must come after other associated CPU events (such as writebacks).
Definition: eventq.hh:161
virtual ~BaseKvmCPU()
Definition: base.cc:100
void notifyFork() override
Definition: base.cc:442
virtual Tick kvmRun(Tick ticks)
Request KVM to run the guest for a given number of ticks.
Definition: base.cc:723
ip6_addr_t addr
Definition: inet.hh:335
std::unique_ptr< BaseKvmTimer > runTimer
Timer used to force execution into the monitor after a specified number of simulation tick equivalent...
Definition: base.hh:783
ThreadContext * tc
ThreadContext object, provides an interface for external objects to modify this thread's state...
Definition: base.hh:150
void kvmNonMaskableInterrupt()
Send a non-maskable interrupt to the guest.
Definition: base.cc:821
Stats::Scalar numHalt
Definition: base.hh:797
virtual Tick handleKvmExitIRQWindowOpen()
The guest exited because an interrupt window was requested.
Definition: base.cc:1076
KVMCpuPort instPort
Unused dummy port for the instruction interface.
Definition: base.hh:606
void verifyMemoryMode() const override
Definition: base.cc:501
void wakeup(ThreadID tid=0) override
Definition: base.cc:510
The SimpleThread object provides a combination of the ThreadState object and the ThreadContext interf...
Bitfield< 23, 0 > offset
Definition: types.hh:149
float hostFactor
Host factor as specified in the configuration.
Definition: base.hh:786
long getVCpuID() const
Definition: base.hh:109
bool tryDrain()
Try to drain the CPU if a drain is pending.
Definition: base.cc:1313
void setOneReg(uint64_t id, uint32_t value)
Definition: base.hh:370
Counter totalInsts() const override
Definition: base.cc:592
Stats::Scalar numExitSignal
Definition: base.hh:793
virtual Tick kvmRunDrain()
Request the CPU to run until draining completes.
Definition: base.cc:703
Base class for KVM based CPU models.
Definition: base.hh:78
Tick flushCoalescedMMIO()
Service MMIO requests in the mmioRing.
Definition: base.cc:1183
TickEvent(BaseKvmCPU &c)
Definition: base.hh:637
TickEvent tickEvent
Definition: base.hh:711
MasterPort & getInstPort() override
Definition: base.hh:101
DrainState drain() override
Definition: base.cc:355
void init() override
Definition: base.cc:108
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:2475
const long vcpuID
KVM internal ID of the vCPU.
Definition: base.hh:627
void recvReqRetry() override
Called by the slave port if sendTimingReq was called on this master port (causing recvTimingReq to be...
Definition: base.cc:217
const char data[]
Definition: circlebuf.cc:43
virtual void updateKvmState()=0
Update the KVM state from the current thread context.
void regStats() override
Definition: base.cc:273
Stats::Scalar numInsts
Definition: base.hh:790
void activateContext(ThreadID thread_num) override
Definition: base.cc:528
Stats::Scalar numInterrupts
Definition: base.hh:798
#define KVM_KICK_SIGNAL
Signal to use to trigger exits from KVM.
Definition: base.hh:57
BaseKvmCPU & cpu
Definition: base.hh:635
bool discardPendingSignal(int signum) const
Discard a (potentially) pending signal.
Definition: base.cc:1254
void setSpecialRegisters(const struct kvm_sregs &regs)
Definition: base.cc:858
void serializeThread(CheckpointOut &cp, ThreadID tid) const override
Definition: base.cc:331
Stats::Scalar numIO
Definition: base.hh:796
void setupSignalHandler()
Setup a signal handler to catch the timer signal used to switch back to the monitor.
Definition: base.cc:1225
int vcpuMMapSize
Size of MMAPed kvm_run area.
Definition: base.hh:693
virtual Tick handleKvmExitFailEntry()
KVM failed to start the virtualized CPU.
Definition: base.cc:1101
void drainResume() override
Definition: base.cc:419
struct kvm_run * _kvmRun
Pointer to the kvm_run structure used to communicate parameters with KVM.
Definition: base.hh:702
void syncKvmState()
Update the KVM if the thread context is dirty.
Definition: base.cc:973
KvmVM & vm
Definition: base.hh:152
Stats::Scalar numVMExits
Definition: base.hh:791
ThreadContext * getContext(int tn) override
Definition: base.cc:583
Stats::Scalar numMMIO
Definition: base.hh:794
void setOneReg(uint64_t id, const void *addr)
Get/Set single register using the KVM_(SET|GET)_ONE_REG API.
Definition: base.cc:880
uint64_t Tick
Tick count type.
Definition: types.hh:63
uint64_t getOneRegU64(uint64_t id) const
Definition: base.hh:372
const bool alwaysSyncTC
Be conservative and always synchronize the thread context on KVM entry/exit.
Definition: base.hh:612
void deallocateContext(ThreadID thread_num)
Definition: base.cc:569
void takeOverFrom(BaseCPU *cpu) override
Definition: base.cc:479
struct kvm_coalesced_mmio_ring * mmioRing
Coalesced MMIO ring buffer.
Definition: base.hh:707
const char * description() const
Return a C string describing the event.
Definition: base.hh:642
pthread_t vcpuThread
ID of the vCPU thread.
Definition: base.hh:630
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:624
void setOneReg(uint64_t id, uint64_t value)
Definition: base.hh:369
PerfKvmCounter hwInstructions
Guest instruction counter.
Definition: base.hh:765
Running normally.
Definition: base.hh:196
uint32_t getOneRegU32(uint64_t id) const
Definition: base.hh:377
PerfKvmCounter hwCycles
Guest cycle counter.
Definition: base.hh:752
Service completion in progress.
Definition: base.hh:227
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:309
An instance of a performance counter.
Definition: perfevent.hh:170
Requiring service at the beginning of the next cycle.
Definition: base.hh:210
virtual Tick handleKvmExitException()
An unhandled virtualization exception occured.
Definition: base.cc:1092
void setSignalMask(const sigset_t *mask)
Set the signal mask used in kvmRun()
Definition: base.cc:1153
Stats::Scalar numVMHalfEntries
Definition: base.hh:792
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
virtual bool archIsDrained() const
Is the architecture specific code in a state that prevents draining?
Definition: base.hh:513
int64_t Counter
Statistics counter type.
Definition: types.hh:58
void setupInstStop()
Setup an instruction break if there is one pending.
Definition: base.cc:1345
A Packet is used to encapsulate a transfer between two objects in the memory system (e...
Definition: packet.hh:245
Status _status
CPU run state.
Definition: base.hh:231
void kvmInterrupt(const struct kvm_interrupt &interrupt)
Send a normal interrupt to the guest.
Definition: base.cc:829
Stats::Scalar numHypercalls
Definition: base.hh:799
BaseKvmCPU(BaseKvmCPUParams *params)
Definition: base.cc:65
void setFPUState(const struct kvm_fpu &state)
Definition: base.cc:872
std::queue< PacketPtr > pendingMMIOPkts
Pending MMIO packets.
Definition: base.hh:591
cbk_int func interrupt
Definition: gpu_nomali.cc:94
int16_t ThreadID
Thread index/ID type.
Definition: types.hh:171
const long pageSize
Cached page size of the host.
Definition: base.hh:709
KVM VM container.
Definition: vm.hh:291
int size()
Definition: pagetable.hh:146
Bitfield< 29 > c
Definition: miscregs.hh:1365
KVMCpuPort dataPort
Port for data requests.
Definition: base.hh:603
void ioctlRun()
Execute the KVM_RUN ioctl.
Definition: base.cc:1335
int ioctl(int request) const
Definition: base.hh:559
std::ostream CheckpointOut
Definition: serialize.hh:67
void kick() const
Force an exit from KVM.
Definition: base.hh:130
int vcpuFD
KVM vCPU file descriptor.
Definition: base.hh:691
MasterPort & getDataPort() override
Definition: base.hh:100
Definition: eventq.hh:185
void setRegisters(const struct kvm_regs &regs)
Definition: base.cc:844
void getOneReg(uint64_t id, void *addr) const
Definition: base.cc:897
std::string getAndFormatOneReg(uint64_t id) const
Get and format one register for printout.
Definition: base.cc:914
void setupInstCounter(uint64_t period=0)
Setup the guest instruction counter.
Definition: base.cc:1358
virtual void dump() const
Dump the internal state to the terminal.
Definition: base.cc:605
void suspendContext(ThreadID thread_num) override
Definition: base.cc:546
virtual Tick handleKvmExitHypercall()
The guest requested a monitor service using a hypercall.
Definition: base.cc:1070
void unserializeThread(CheckpointIn &cp, ThreadID tid) override
Definition: base.cc:344
virtual uint64_t getHostCycles() const
Get the value of the hardware cycle counter in the guest.
Definition: base.cc:717
int ioctl(int request, void *p1) const
Definition: base.hh:556
Bitfield< 3, 0 > mask
Definition: types.hh:64
Status nextIOState() const
Returns next valid state after one or more IO accesses.
Definition: base.cc:173
Context not scheduled in KVM.
Definition: base.hh:190
void startup() override
Definition: base.cc:123
Counter totalOps() const override
Definition: base.cc:598
virtual Tick handleKvmExitUnknown()
An unknown architecture dependent error occurred when starting the vCPU.
Definition: base.cc:1084
SimpleThread * thread
A cached copy of a thread's state in the form of a SimpleThread object.
Definition: base.hh:145
void getFPUState(struct kvm_fpu &state) const
Get/Set the guest FPU/vector state.
Definition: base.cc:865
KVM memory port.
Definition: base.hh:569
void switchOut() override
Definition: base.cc:465
KVMCpuPort(const std::string &_name, BaseKvmCPU *_cpu)
Definition: base.hh:573
void getRegisters(struct kvm_regs &regs) const
Get/Set the register state of the guest vCPU.
Definition: base.cc:837
virtual Tick handleKvmExit()
Main kvmRun exit handler, calls the relevant handleKvmExit* depending on exit type.
Definition: base.cc:985
unsigned int activeMMIOReqs
Number of MMIO requests in flight.
Definition: base.hh:594
Tick doMMIOAccess(Addr paddr, void *data, int size, bool write)
Inject a memory mapped IO request into gem5.
Definition: base.cc:1109
Stats::Scalar numCoalescedMMIO
Definition: base.hh:795
void startupThread()
Thread-specific initialization.
Definition: base.cc:243
void syncThreadContext()
Update a thread context if the KVM state is dirty with respect to the cached thread context...
Definition: base.cc:961
void haltContext(ThreadID thread_num) override
Definition: base.cc:576
void getSpecialRegisters(struct kvm_sregs &regs) const
Definition: base.cc:851
int ioctl(int request, long p1) const
vCPU ioctl interface.
Definition: base.cc:1174
struct kvm_run * getKvmRunState()
Get a pointer to the kvm_run structure containing all the input and output parameters from kvmRun()...
Definition: base.hh:299

Generated on Fri Jun 9 2017 13:03:41 for gem5 by doxygen 1.8.6