gem5  v21.1.0.2
vm.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Google, Inc.
3  * Copyright (c) 2012, 2015 ARM Limited
4  * All rights reserved
5  *
6  * The license below extends only to copyright in the software and shall
7  * not be construed as granting a license to any other intellectual
8  * property including but not limited to intellectual property relating
9  * to a hardware implementation of the functionality of the software
10  * licensed hereunder. You may use the software subject to the license
11  * terms below provided that you ensure that this notice is replicated
12  * unmodified and in its entirety in all distributions of the software,
13  * modified or unmodified, in source code or in binary form.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are
17  * met: redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer;
19  * redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution;
22  * neither the name of the copyright holders nor the names of its
23  * contributors may be used to endorse or promote products derived from
24  * this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #ifndef __CPU_KVM_KVMVM_HH__
40 #define __CPU_KVM_KVMVM_HH__
41 
42 #include <vector>
43 
44 #include "base/addr_range.hh"
45 #include "sim/sim_object.hh"
46 
47 struct kvm_cpuid_entry2;
48 struct kvm_cpuid2;
49 struct kvm_msr_list;
50 struct kvm_vcpu_init;
51 
52 namespace gem5
53 {
54 
55 // forward declarations
56 struct KvmVMParams;
57 class BaseKvmCPU;
58 class System;
59 
80 class Kvm
81 {
82  friend class KvmVM;
83 
84  public:
85  virtual ~Kvm();
86 
87  Kvm *create();
88 
90  int getAPIVersion() const { return apiVersion; }
96  int getVCPUMMapSize() const { return vcpuMMapSize; }
97 
100  bool capUserMemory() const;
102  bool capSetTSSAddress() const;
104  bool capExtendedCPUID() const;
106  bool capUserNMI() const;
107 
115  int capCoalescedMMIO() const;
116 
121  int capNumMemSlots() const;
122 
128  bool capOneReg() const;
129 
135  bool capIRQChip() const;
136 
138  bool capVCPUEvents() const;
139 
141  bool capDebugRegs() const;
142 
144  bool capXCRs() const;
145 
147  bool capXSave() const;
150 #if defined(__i386__) || defined(__x86_64__)
151  public: // x86-specific
157  typedef std::vector<struct kvm_cpuid_entry2> CPUIDVector;
158  typedef std::vector<uint32_t> MSRIndexVector;
159 
167  bool getSupportedCPUID(struct kvm_cpuid2 &cpuid) const;
168 
179  const CPUIDVector &getSupportedCPUID() const;
180 
186  bool getSupportedMSRs(struct kvm_msr_list &msrs) const;
187 
196  const MSRIndexVector &getSupportedMSRs() const;
197 
198  private: // x86-specific
200  mutable CPUIDVector supportedCPUIDCache;
201 
203  mutable MSRIndexVector supportedMSRCache;
204 
205 
207 #endif
208 
209  protected:
220  int checkExtension(int extension) const;
221 
235  int ioctl(int request, long p1) const;
236  int ioctl(int request, void *p1) const {
237  return ioctl(request, (long)p1);
238  }
239  int ioctl(int request) const {
240  return ioctl(request, 0L);
241  }
244  private:
245  // This object is a singleton, so prevent instantiation.
246  Kvm();
247 
248  // Prevent copying
249  Kvm(const Kvm &kvm);
250  // Prevent assignment
251  Kvm &operator=(const Kvm &kvm);
252 
258  int createVM();
259 
261  int kvmFD;
266 
268  static Kvm *instance;
269 };
270 
297 class KvmVM : public SimObject
298 {
299  friend class BaseKvmCPU;
300 
301  public:
302  KvmVM(const KvmVMParams &params);
303  virtual ~KvmVM();
304 
305  void notifyFork();
306 
313  void setTSSAddress(Addr tss_address);
314 
322  void coalesceMMIO(Addr start, int size);
323 
329  void coalesceMMIO(const AddrRange &range);
341  void createIRQChip();
342 
354  void setIRQLine(uint32_t irq, bool high);
355 
359  bool hasKernelIRQChip() const { return _hasKernelIRQChip; }
360 
371  struct MemSlot
372  {
373  MemSlot(uint32_t _num) : num(_num)
374  {}
375  MemSlot() : num(-1)
376  {}
377 
378  int32_t num;
379  };
380 
384  const MemSlot allocMemSlot(uint64_t size);
385 
394  void setupMemSlot(const MemSlot slot, void *host_addr, Addr guest_addr,
395  uint32_t flags);
396 
400  void disableMemSlot(const MemSlot slot);
401 
405  void freeMemSlot(const MemSlot slot);
406 
414  int createDevice(uint32_t type, uint32_t flags = 0);
415 
418 
422  void setSystem(System *s);
423 
427  long contextIdToVCpuId(ContextID ctx) const;
428 
429 #if defined(__aarch64__)
430  public: // ARM-specific
442  void kvmArmPreferredTarget(struct kvm_vcpu_init &target) const;
443 
444 #endif
445 
446  protected:
455  void cpuStartup();
456 
468  void delayedStartup();
469 
470 
481  void setUserMemoryRegion(uint32_t slot,
482  void *host_addr, Addr guest_addr,
483  uint64_t len, uint32_t flags);
492  int createVCPU(long vcpuID);
493 
502  long allocVCPUID();
503 
517  int ioctl(int request, long p1) const;
518  int ioctl(int request, void *p1) const {
519  return ioctl(request, (long)p1);
520  }
521  int ioctl(int request) const {
522  return ioctl(request, 0L);
523  }
526  private:
527  // Prevent copying
528  KvmVM(const KvmVM &vm);
529  // Prevent assignment
530  KvmVM &operator=(const KvmVM &vm);
531 
533 
535  int vmFD;
536 
538  bool started;
539 
542 
545 
550  {
551  public:
552  uint64_t size;
553  uint32_t slot;
554  bool active;
555  };
557  uint32_t maxMemorySlot;
558 };
559 
560 } // namespace gem5
561 
562 #endif
gem5::Kvm::getAPIVersion
int getAPIVersion() const
Get the version of the KVM API implemented by the kernel.
Definition: vm.hh:90
gem5::KvmVM::vmFD
int vmFD
KVM VM file descriptor.
Definition: vm.hh:535
gem5::KvmVM::MemorySlot::slot
uint32_t slot
Definition: vm.hh:553
gem5::KvmVM::createVCPU
int createVCPU(long vcpuID)
Create a new vCPU within a VM.
Definition: vm.cc:560
gem5::Kvm::capSetTSSAddress
bool capSetTSSAddress() const
Support for KvmVM::setTSSAddress()
Definition: vm.cc:114
gem5::KvmVM::freeMemSlot
void freeMemSlot(const MemSlot slot)
Free a previously allocated memory slot.
Definition: vm.cc:437
gem5::Kvm::capUserNMI
bool capUserNMI() const
Support for BaseKvmCPU::kvmNonMaskableInterrupt().
Definition: vm.cc:126
gem5::ArmISA::len
Bitfield< 18, 16 > len
Definition: misc_types.hh:444
gem5::KvmVM::setUserMemoryRegion
void setUserMemoryRegion(uint32_t slot, void *host_addr, Addr guest_addr, uint64_t len, uint32_t flags)
Setup a region of physical memory in the guest.
Definition: vm.cc:445
gem5::Kvm::instance
static Kvm * instance
Singleton instance.
Definition: vm.hh:268
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
gem5::KvmVM::createIRQChip
void createIRQChip()
Create an in-kernel interrupt controller.
Definition: vm.cc:499
gem5::X86ISA::L
Bitfield< 7, 0 > L
Definition: int.hh:59
gem5::MipsISA::cpuid
Bitfield< 28, 21 > cpuid
Definition: dt_constants.hh:95
gem5::KvmVM::setTSSAddress
void setTSSAddress(Addr tss_address)
Setup a shared three-page memory region used by the internals of KVM.
Definition: vm.cc:492
gem5::KvmVM::ioctl
int ioctl(int request, void *p1) const
Definition: vm.hh:518
gem5::KvmVM::ioctl
int ioctl(int request) const
Definition: vm.hh:521
gem5::Kvm::create
Kvm * create()
Definition: vm.cc:99
gem5::KvmVM::allocVCPUID
long allocVCPUID()
Allocate a new vCPU ID within the VM.
Definition: vm.cc:572
gem5::KvmVM::disableMemSlot
void disableMemSlot(const MemSlot slot)
Disable a memory slot.
Definition: vm.cc:428
gem5::KvmVM::setSystem
void setSystem(System *s)
Initialize system pointer.
Definition: vm.cc:544
gem5::KvmVM::maxMemorySlot
uint32_t maxMemorySlot
Definition: vm.hh:557
gem5::Kvm::capExtendedCPUID
bool capExtendedCPUID() const
Support for BaseKvmCPU::setCPUID2 and getSupportedCPUID().
Definition: vm.cc:120
std::vector
STL vector class.
Definition: stl.hh:37
gem5::KvmVM::ioctl
int ioctl(int request, long p1) const
KVM VM ioctl interface.
Definition: vm.cc:589
gem5::KvmVM::coalesceMMIO
void coalesceMMIO(Addr start, int size)
Request coalescing MMIO for a memory range.
Definition: vm.cc:476
gem5::Kvm::capUserMemory
bool capUserMemory() const
Support for KvmVM::setUserMemoryRegion()
Definition: vm.cc:108
gem5::KvmVM::KvmVM
KvmVM(const KvmVMParams &params)
Definition: vm.cc:305
gem5::Kvm::capXCRs
bool capXCRs() const
Support for getting and setting the x86 XCRs.
Definition: vm.cc:188
gem5::Kvm::capXSave
bool capXSave() const
Support for getting and setting the kvm_xsave structure.
Definition: vm.cc:198
gem5::KvmVM
KVM VM container.
Definition: vm.hh:297
gem5::Kvm::capIRQChip
bool capIRQChip() const
Support for creating an in-kernel IRQ chip model.
Definition: vm.cc:162
gem5::ArmISA::irq
Bitfield< 1 > irq
Definition: misc_types.hh:330
gem5::Kvm::checkExtension
int checkExtension(int extension) const
Check for the presence of an extension to the KVM API.
Definition: vm.cc:276
gem5::high
high
Definition: intmath.hh:176
gem5::System
Definition: system.hh:77
gem5::Kvm::capVCPUEvents
bool capVCPUEvents() const
Support for getting and setting the kvm_vcpu_events structure.
Definition: vm.cc:168
gem5::SimObject::params
const Params & params() const
Definition: sim_object.hh:176
sim_object.hh
gem5::BaseKvmCPU
Base class for KVM based CPU models.
Definition: base.hh:87
gem5::Kvm::vcpuMMapSize
int vcpuMMapSize
Size of the MMAPed vCPU parameter area.
Definition: vm.hh:265
gem5::KvmVM::system
System * system
Definition: vm.hh:532
gem5::Kvm::Kvm
Kvm()
Definition: vm.cc:71
gem5::KvmVM::_hasKernelIRQChip
bool _hasKernelIRQChip
Do we have in-kernel IRQ-chip emulation enabled?
Definition: vm.hh:541
gem5::X86ISA::type
type
Definition: misc.hh:733
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::ArmISA::s
Bitfield< 4 > s
Definition: misc_types.hh:561
gem5::Kvm::ioctl
int ioctl(int request, void *p1) const
Definition: vm.hh:236
gem5::Kvm::kvmFD
int kvmFD
KVM VM file descriptor.
Definition: vm.hh:261
gem5::KvmVM::started
bool started
Has delayedStartup() already been called?
Definition: vm.hh:538
gem5::KvmVM::setIRQLine
void setIRQLine(uint32_t irq, bool high)
Set the status of an IRQ line using KVM_IRQ_LINE.
Definition: vm.cc:514
gem5::KvmVM::enableKernelIRQChip
void enableKernelIRQChip()
Tell the VM and VCPUs to use an in-kernel IRQ chip for interrupt delivery.
Definition: vm.hh:368
gem5::KvmVM::delayedStartup
void delayedStartup()
Delayed initialization, executed once before the first CPU starts.
Definition: vm.cc:356
gem5::Kvm::ioctl
int ioctl(int request, long p1) const
Main VM ioctl interface.
Definition: vm.cc:285
gem5::SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:146
gem5::KvmVM::MemSlot
Definition: vm.hh:371
gem5::KvmVM::contextIdToVCpuId
long contextIdToVCpuId(ContextID ctx) const
Get the VCPUID for a given context.
Definition: vm.cc:552
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::ArmISA::vm
Bitfield< 0 > vm
Definition: misc_types.hh:284
gem5::KvmVM::kvm
Kvm * kvm
Global KVM interface.
Definition: vm.hh:417
addr_range.hh
gem5::Kvm::ioctl
int ioctl(int request) const
Definition: vm.hh:239
gem5::KvmVM::operator=
KvmVM & operator=(const KvmVM &vm)
gem5::KvmVM::cpuStartup
void cpuStartup()
VM CPU initialization code.
Definition: vm.cc:346
gem5::KvmVM::MemorySlot
Structures tracking memory slots.
Definition: vm.hh:549
gem5::KvmVM::allocMemSlot
const MemSlot allocMemSlot(uint64_t size)
Allocate a memory slot within the VM.
Definition: vm.cc:391
gem5::KvmVM::MemSlot::MemSlot
MemSlot()
Definition: vm.hh:375
gem5::Kvm::capDebugRegs
bool capDebugRegs() const
Support for getting and setting the kvm_debugregs structure.
Definition: vm.cc:178
gem5::KvmVM::nextVCPUID
long nextVCPUID
Next unallocated vCPU ID.
Definition: vm.hh:544
gem5::ContextID
int ContextID
Globally unique thread context ID.
Definition: types.hh:246
gem5::Kvm
KVM parent interface.
Definition: vm.hh:80
gem5::KvmVM::MemorySlot::active
bool active
Definition: vm.hh:554
gem5::KvmVM::MemorySlot::size
uint64_t size
Definition: vm.hh:552
gem5::Kvm::capNumMemSlots
int capNumMemSlots() const
Attempt to determine how many memory slots are available.
Definition: vm.cc:142
gem5::Kvm::~Kvm
virtual ~Kvm()
Definition: vm.cc:93
gem5::AddrRange
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:71
gem5::Kvm::apiVersion
int apiVersion
KVM API version.
Definition: vm.hh:263
gem5::KvmVM::MemSlot::num
int32_t num
Definition: vm.hh:378
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::KvmVM::MemSlot::MemSlot
MemSlot(uint32_t _num)
Definition: vm.hh:373
gem5::KvmVM::notifyFork
void notifyFork()
Notify a child process of a fork.
Definition: vm.cc:332
gem5::KvmVM::memorySlots
std::vector< MemorySlot > memorySlots
Definition: vm.hh:556
gem5::KvmVM::~KvmVM
virtual ~KvmVM()
Definition: vm.cc:322
gem5::Kvm::capOneReg
bool capOneReg() const
Support for reading and writing single registers.
Definition: vm.cc:152
gem5::KvmVM::hasKernelIRQChip
bool hasKernelIRQChip() const
Is in-kernel IRQ chip emulation enabled?
Definition: vm.hh:359
gem5::Kvm::createVM
int createVM()
Create a KVM Virtual Machine.
Definition: vm.cc:293
gem5::KvmVM::setupMemSlot
void setupMemSlot(const MemSlot slot, void *host_addr, Addr guest_addr, uint32_t flags)
Setup a region of physical memory in the guest.
Definition: vm.cc:419
gem5::Kvm::operator=
Kvm & operator=(const Kvm &kvm)
gem5::KvmVM::createDevice
int createDevice(uint32_t type, uint32_t flags=0)
Create an in-kernel device model.
Definition: vm.cc:527

Generated on Tue Sep 21 2021 12:25:00 for gem5 by doxygen 1.8.17