gem5  v22.0.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;
148 
150  bool capIRQLineLayout2() const;
151 
154 #if defined(__i386__) || defined(__x86_64__)
155  public: // x86-specific
161  typedef std::vector<struct kvm_cpuid_entry2> CPUIDVector;
162  typedef std::vector<uint32_t> MSRIndexVector;
163 
171  bool getSupportedCPUID(struct kvm_cpuid2 &cpuid) const;
172 
183  const CPUIDVector &getSupportedCPUID() const;
184 
190  bool getSupportedMSRs(struct kvm_msr_list &msrs) const;
191 
200  const MSRIndexVector &getSupportedMSRs() const;
201 
202  private: // x86-specific
204  mutable CPUIDVector supportedCPUIDCache;
205 
207  mutable MSRIndexVector supportedMSRCache;
208 
209 
211 #endif
212 
213  protected:
224  int checkExtension(int extension) const;
225 
239  int ioctl(int request, long p1) const;
240  int ioctl(int request, void *p1) const {
241  return ioctl(request, (long)p1);
242  }
243  int ioctl(int request) const {
244  return ioctl(request, 0L);
245  }
248  private:
249  // This object is a singleton, so prevent instantiation.
250  Kvm();
251 
252  // Prevent copying
253  Kvm(const Kvm &kvm);
254  // Prevent assignment
255  Kvm &operator=(const Kvm &kvm);
256 
262  int createVM();
263 
265  int kvmFD;
270 
272  static Kvm *instance;
273 };
274 
301 class KvmVM : public SimObject
302 {
303  friend class BaseKvmCPU;
304 
305  public:
306  KvmVM(const KvmVMParams &params);
307  virtual ~KvmVM();
308 
309  void notifyFork();
310 
317  void setTSSAddress(Addr tss_address);
318 
326  void coalesceMMIO(Addr start, int size);
327 
333  void coalesceMMIO(const AddrRange &range);
345  void createIRQChip();
346 
358  void setIRQLine(uint32_t irq, bool high);
359 
363  bool hasKernelIRQChip() const { return _hasKernelIRQChip; }
364 
375  struct MemSlot
376  {
377  MemSlot(uint32_t _num) : num(_num)
378  {}
379  MemSlot() : num(-1)
380  {}
381 
382  int32_t num;
383  };
384 
388  const MemSlot allocMemSlot(uint64_t size);
389 
398  void setupMemSlot(const MemSlot slot, void *host_addr, Addr guest_addr,
399  uint32_t flags);
400 
404  void disableMemSlot(const MemSlot slot);
405 
409  void freeMemSlot(const MemSlot slot);
410 
418  int createDevice(uint32_t type, uint32_t flags = 0);
419 
422 
424  bool validEnvironment() const;
425 
429  long contextIdToVCpuId(ContextID ctx) const;
430 
431 #if defined(__aarch64__)
432  public: // ARM-specific
444  void kvmArmPreferredTarget(struct kvm_vcpu_init &target) const;
445 
446 #endif
447 
448  protected:
457  void cpuStartup();
458 
470  void delayedStartup();
471 
472 
483  void setUserMemoryRegion(uint32_t slot,
484  void *host_addr, Addr guest_addr,
485  uint64_t len, uint32_t flags);
494  int createVCPU(long vcpuID);
495 
504  long allocVCPUID();
505 
519  int ioctl(int request, long p1) const;
520  int ioctl(int request, void *p1) const {
521  return ioctl(request, (long)p1);
522  }
523  int ioctl(int request) const {
524  return ioctl(request, 0L);
525  }
528  private:
529  // Prevent copying
530  KvmVM(const KvmVM &vm);
531  // Prevent assignment
532  KvmVM &operator=(const KvmVM &vm);
533 
535 
537  int vmFD;
538 
540  bool started;
541 
544 
547 
552  {
553  public:
554  uint64_t size;
555  uint32_t slot;
556  bool active;
557  };
559  uint32_t maxMemorySlot;
560 };
561 
562 } // namespace gem5
563 
564 #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:537
gem5::KvmVM::MemorySlot::slot
uint32_t slot
Definition: vm.hh:555
gem5::KvmVM::createVCPU
int createVCPU(long vcpuID)
Create a new vCPU within a VM.
Definition: vm.cc:573
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:448
gem5::Kvm::capUserNMI
bool capUserNMI() const
Support for BaseKvmCPU::kvmNonMaskableInterrupt().
Definition: vm.cc:126
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:456
gem5::Kvm::instance
static Kvm * instance
Singleton instance.
Definition: vm.hh:272
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:510
gem5::X86ISA::L
Bitfield< 7, 0 > L
Definition: int.hh:61
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:503
gem5::KvmVM::ioctl
int ioctl(int request, void *p1) const
Definition: vm.hh:520
gem5::KvmVM::ioctl
int ioctl(int request) const
Definition: vm.hh:523
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:585
gem5::KvmVM::disableMemSlot
void disableMemSlot(const MemSlot slot)
Disable a memory slot.
Definition: vm.cc:439
gem5::KvmVM::maxMemorySlot
uint32_t maxMemorySlot
Definition: vm.hh:559
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:602
gem5::KvmVM::coalesceMMIO
void coalesceMMIO(Addr start, int size)
Request coalescing MMIO for a memory range.
Definition: vm.cc:487
gem5::Kvm::capUserMemory
bool capUserMemory() const
Support for KvmVM::setUserMemoryRegion()
Definition: vm.cc:108
gem5::KvmVM::KvmVM
KvmVM(const KvmVMParams &params)
Definition: vm.cc:316
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:301
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:331
gem5::Kvm::checkExtension
int checkExtension(int extension) const
Check for the presence of an extension to the KVM API.
Definition: vm.cc:287
gem5::high
high
Definition: intmath.hh:176
gem5::System
Definition: system.hh:75
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:269
gem5::KvmVM::system
System * system
Definition: vm.hh:534
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:543
gem5::X86ISA::type
type
Definition: misc.hh:727
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::Kvm::ioctl
int ioctl(int request, void *p1) const
Definition: vm.hh:240
len
uint16_t len
Definition: helpers.cc:62
gem5::Kvm::kvmFD
int kvmFD
KVM VM file descriptor.
Definition: vm.hh:265
gem5::KvmVM::started
bool started
Has delayedStartup() already been called?
Definition: vm.hh:540
gem5::KvmVM::setIRQLine
void setIRQLine(uint32_t irq, bool high)
Set the status of an IRQ line using KVM_IRQ_LINE.
Definition: vm.cc:525
gem5::KvmVM::enableKernelIRQChip
void enableKernelIRQChip()
Tell the VM and VCPUs to use an in-kernel IRQ chip for interrupt delivery.
Definition: vm.hh:372
gem5::KvmVM::delayedStartup
void delayedStartup()
Delayed initialization, executed once before the first CPU starts.
Definition: vm.cc:368
gem5::Kvm::ioctl
int ioctl(int request, long p1) const
Main VM ioctl interface.
Definition: vm.cc:296
flags
uint8_t flags
Definition: helpers.cc:66
gem5::SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:146
gem5::KvmVM::MemSlot
Definition: vm.hh:375
gem5::KvmVM::contextIdToVCpuId
long contextIdToVCpuId(ContextID ctx) const
Get the VCPUID for a given context.
Definition: vm.cc:566
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:285
gem5::KvmVM::kvm
Kvm * kvm
Global KVM interface.
Definition: vm.hh:421
addr_range.hh
gem5::Kvm::ioctl
int ioctl(int request) const
Definition: vm.hh:243
gem5::KvmVM::operator=
KvmVM & operator=(const KvmVM &vm)
gem5::KvmVM::cpuStartup
void cpuStartup()
VM CPU initialization code.
Definition: vm.cc:358
gem5::KvmVM::MemorySlot
Structures tracking memory slots.
Definition: vm.hh:551
gem5::KvmVM::allocMemSlot
const MemSlot allocMemSlot(uint64_t size)
Allocate a memory slot within the VM.
Definition: vm.cc:402
gem5::KvmVM::MemSlot::MemSlot
MemSlot()
Definition: vm.hh:379
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:546
gem5::ContextID
int ContextID
Globally unique thread context ID.
Definition: types.hh:239
gem5::KvmVM::validEnvironment
bool validEnvironment() const
Verify gem5 configuration will support KVM emulation.
Definition: vm.cc:555
gem5::Kvm
KVM parent interface.
Definition: vm.hh:80
gem5::KvmVM::MemorySlot::active
bool active
Definition: vm.hh:556
gem5::KvmVM::MemorySlot::size
uint64_t size
Definition: vm.hh:554
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:81
gem5::Kvm::apiVersion
int apiVersion
KVM API version.
Definition: vm.hh:267
gem5::KvmVM::MemSlot::num
int32_t num
Definition: vm.hh:382
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::KvmVM::MemSlot::MemSlot
MemSlot(uint32_t _num)
Definition: vm.hh:377
gem5::KvmVM::notifyFork
void notifyFork()
Notify a child process of a fork.
Definition: vm.cc:344
gem5::KvmVM::memorySlots
std::vector< MemorySlot > memorySlots
Definition: vm.hh:558
gem5::KvmVM::~KvmVM
virtual ~KvmVM()
Definition: vm.cc:334
gem5::Kvm::capOneReg
bool capOneReg() const
Support for reading and writing single registers.
Definition: vm.cc:152
gem5::Kvm::capIRQLineLayout2
bool capIRQLineLayout2() const
Support for ARM IRQ line layout 2.
Definition: vm.cc:208
gem5::KvmVM::hasKernelIRQChip
bool hasKernelIRQChip() const
Is in-kernel IRQ chip emulation enabled?
Definition: vm.hh:363
gem5::Kvm::createVM
int createVM()
Create a KVM Virtual Machine.
Definition: vm.cc:304
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:430
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:538

Generated on Thu Jul 28 2022 13:32:28 for gem5 by doxygen 1.8.17