gem5  v20.1.0.0
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 // forward declarations
48 struct KvmVMParams;
49 class BaseKvmCPU;
50 class System;
51 
72 class Kvm
73 {
74  friend class KvmVM;
75 
76  public:
77  virtual ~Kvm();
78 
79  Kvm *create();
80 
82  int getAPIVersion() const { return apiVersion; }
88  int getVCPUMMapSize() const { return vcpuMMapSize; }
89 
92  bool capUserMemory() const;
94  bool capSetTSSAddress() const;
96  bool capExtendedCPUID() const;
98  bool capUserNMI() const;
99 
107  int capCoalescedMMIO() const;
108 
113  int capNumMemSlots() const;
114 
120  bool capOneReg() const;
121 
127  bool capIRQChip() const;
128 
130  bool capVCPUEvents() const;
131 
133  bool capDebugRegs() const;
134 
136  bool capXCRs() const;
137 
139  bool capXSave() const;
142 #if defined(__i386__) || defined(__x86_64__)
143  public: // x86-specific
149  typedef std::vector<struct kvm_cpuid_entry2> CPUIDVector;
150  typedef std::vector<uint32_t> MSRIndexVector;
151 
159  bool getSupportedCPUID(struct kvm_cpuid2 &cpuid) const;
160 
171  const CPUIDVector &getSupportedCPUID() const;
172 
178  bool getSupportedMSRs(struct kvm_msr_list &msrs) const;
179 
188  const MSRIndexVector &getSupportedMSRs() const;
189 
190  private: // x86-specific
192  mutable CPUIDVector supportedCPUIDCache;
193 
195  mutable MSRIndexVector supportedMSRCache;
196 
197 
199 #endif
200 
201  protected:
212  int checkExtension(int extension) const;
213 
227  int ioctl(int request, long p1) const;
228  int ioctl(int request, void *p1) const {
229  return ioctl(request, (long)p1);
230  }
231  int ioctl(int request) const {
232  return ioctl(request, 0L);
233  }
236  private:
237  // This object is a singleton, so prevent instantiation.
238  Kvm();
239 
240  // Prevent copying
241  Kvm(const Kvm &kvm);
242  // Prevent assignment
243  Kvm &operator=(const Kvm &kvm);
244 
250  int createVM();
251 
253  int kvmFD;
258 
260  static Kvm *instance;
261 };
262 
289 class KvmVM : public SimObject
290 {
291  friend class BaseKvmCPU;
292 
293  public:
294  KvmVM(KvmVMParams *params);
295  virtual ~KvmVM();
296 
297  void notifyFork();
298 
305  void setTSSAddress(Addr tss_address);
306 
314  void coalesceMMIO(Addr start, int size);
315 
321  void coalesceMMIO(const AddrRange &range);
333  void createIRQChip();
334 
346  void setIRQLine(uint32_t irq, bool high);
347 
351  bool hasKernelIRQChip() const { return _hasKernelIRQChip; }
352 
363  struct MemSlot
364  {
365  MemSlot(uint32_t _num) : num(_num)
366  {}
367  MemSlot() : num(-1)
368  {}
369 
370  int32_t num;
371  };
372 
376  const MemSlot allocMemSlot(uint64_t size);
377 
386  void setupMemSlot(const MemSlot slot, void *host_addr, Addr guest_addr,
387  uint32_t flags);
388 
392  void disableMemSlot(const MemSlot slot);
393 
397  void freeMemSlot(const MemSlot slot);
398 
406  int createDevice(uint32_t type, uint32_t flags = 0);
407 
410 
414  void setSystem(System *s);
415 
419  long contextIdToVCpuId(ContextID ctx) const;
420 
421 #if defined(__aarch64__)
422  public: // ARM-specific
434  void kvmArmPreferredTarget(struct kvm_vcpu_init &target) const;
435 
436 #endif
437 
438  protected:
447  void cpuStartup();
448 
460  void delayedStartup();
461 
462 
473  void setUserMemoryRegion(uint32_t slot,
474  void *host_addr, Addr guest_addr,
475  uint64_t len, uint32_t flags);
484  int createVCPU(long vcpuID);
485 
494  long allocVCPUID();
495 
509  int ioctl(int request, long p1) const;
510  int ioctl(int request, void *p1) const {
511  return ioctl(request, (long)p1);
512  }
513  int ioctl(int request) const {
514  return ioctl(request, 0L);
515  }
518  private:
519  // Prevent copying
520  KvmVM(const KvmVM &vm);
521  // Prevent assignment
522  KvmVM &operator=(const KvmVM &vm);
523 
525 
527  int vmFD;
528 
530  bool started;
531 
534 
537 
542  {
543  public:
544  uint64_t size;
545  uint32_t slot;
546  bool active;
547  };
549  uint32_t maxMemorySlot;
550 };
551 
552 #endif
KvmVM::kvm
Kvm * kvm
Global KVM interface.
Definition: vm.hh:409
KvmVM
KVM VM container.
Definition: vm.hh:289
Kvm::instance
static Kvm * instance
Singleton instance.
Definition: vm.hh:260
X86ISA::L
Bitfield< 7, 0 > L
Definition: int.hh:57
Kvm
KVM parent interface.
Definition: vm.hh:72
Kvm::capUserMemory
bool capUserMemory() const
Support for KvmVM::setUserMemoryRegion()
Definition: vm.cc:95
Kvm::capXCRs
bool capXCRs() const
Support for getting and setting the x86 XCRs.
Definition: vm.cc:175
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:88
Kvm::ioctl
int ioctl(int request, void *p1) const
Definition: vm.hh:228
MipsISA::cpuid
Bitfield< 28, 21 > cpuid
Definition: dt_constants.hh:92
KvmVM::MemSlot
Definition: vm.hh:363
KvmVM::MemorySlot::size
uint64_t size
Definition: vm.hh:544
Kvm::capNumMemSlots
int capNumMemSlots() const
Attempt to determine how many memory slots are available.
Definition: vm.cc:129
Kvm::ioctl
int ioctl(int request) const
Definition: vm.hh:231
KvmVM::_hasKernelIRQChip
bool _hasKernelIRQChip
Do we have in-kernel IRQ-chip emulation enabled?
Definition: vm.hh:533
Kvm::operator=
Kvm & operator=(const Kvm &kvm)
ContextID
int ContextID
Globally unique thread context ID.
Definition: types.hh:231
KvmVM::freeMemSlot
void freeMemSlot(const MemSlot slot)
Free a previously allocated memory slot.
Definition: vm.cc:424
KvmVM::system
System * system
Definition: vm.hh:524
KvmVM::ioctl
int ioctl(int request, void *p1) const
Definition: vm.hh:510
type
uint8_t type
Definition: inet.hh:421
KvmVM::maxMemorySlot
uint32_t maxMemorySlot
Definition: vm.hh:549
KvmVM::allocMemSlot
const MemSlot allocMemSlot(uint64_t size)
Allocate a memory slot within the VM.
Definition: vm.cc:378
Kvm::capDebugRegs
bool capDebugRegs() const
Support for getting and setting the kvm_debugregs structure.
Definition: vm.cc:165
KvmVM::delayedStartup
void delayedStartup()
Delayed initialization, executed once before the first CPU starts.
Definition: vm.cc:343
KvmVM::~KvmVM
virtual ~KvmVM()
Definition: vm.cc:309
std::vector
STL vector class.
Definition: stl.hh:37
Kvm::checkExtension
int checkExtension(int extension) const
Check for the presence of an extension to the KVM API.
Definition: vm.cc:263
KvmVM::enableKernelIRQChip
void enableKernelIRQChip()
Tell the VM and VCPUs to use an in-kernel IRQ chip for interrupt delivery.
Definition: vm.hh:360
KvmVM::contextIdToVCpuId
long contextIdToVCpuId(ContextID ctx) const
Get the VCPUID for a given context.
Definition: vm.cc:539
KvmVM::MemSlot::MemSlot
MemSlot(uint32_t _num)
Definition: vm.hh:365
KvmVM::cpuStartup
void cpuStartup()
VM CPU initialization code.
Definition: vm.cc:333
Kvm::capSetTSSAddress
bool capSetTSSAddress() const
Support for KvmVM::setTSSAddress()
Definition: vm.cc:101
KvmVM::MemorySlot
Structures tracking memory slots.
Definition: vm.hh:541
KvmVM::MemorySlot::slot
uint32_t slot
Definition: vm.hh:545
KvmVM::KvmVM
KvmVM(KvmVMParams *params)
Definition: vm.cc:292
Kvm::vcpuMMapSize
int vcpuMMapSize
Size of the MMAPed vCPU parameter area.
Definition: vm.hh:257
Kvm::Kvm
Kvm()
Definition: vm.cc:64
KvmVM::nextVCPUID
long nextVCPUID
Next unallocated vCPU ID.
Definition: vm.hh:536
Kvm::~Kvm
virtual ~Kvm()
Definition: vm.cc:80
KvmVM::createVCPU
int createVCPU(long vcpuID)
Create a new vCPU within a VM.
Definition: vm.cc:547
KvmVM::coalesceMMIO
void coalesceMMIO(Addr start, int size)
Request coalescing MMIO for a memory range.
Definition: vm.cc:463
BaseKvmCPU
Base class for KVM based CPU models.
Definition: base.hh:77
KvmVM::ioctl
int ioctl(int request) const
Definition: vm.hh:513
Kvm::ioctl
int ioctl(int request, long p1) const
Main VM ioctl interface.
Definition: vm.cc:272
Kvm::capOneReg
bool capOneReg() const
Support for reading and writing single registers.
Definition: vm.cc:139
AddrRange
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:68
sim_object.hh
System
Definition: system.hh:73
KvmVM::MemorySlot::active
bool active
Definition: vm.hh:546
Kvm::kvmFD
int kvmFD
KVM VM file descriptor.
Definition: vm.hh:253
KvmVM::MemSlot::num
int32_t num
Definition: vm.hh:370
KvmVM::setSystem
void setSystem(System *s)
Initialize system pointer.
Definition: vm.cc:531
KvmVM::createDevice
int createDevice(uint32_t type, uint32_t flags=0)
Create an in-kernel device model.
Definition: vm.cc:514
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:432
Kvm::apiVersion
int apiVersion
KVM API version.
Definition: vm.hh:255
Kvm::getAPIVersion
int getAPIVersion() const
Get the version of the KVM API implemented by the kernel.
Definition: vm.hh:82
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:123
Kvm::create
Kvm * create()
Definition: vm.cc:86
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
SimObject::params
const Params * params() const
Definition: sim_object.hh:119
Kvm::createVM
int createVM()
Create a KVM Virtual Machine.
Definition: vm.cc:280
addr_range.hh
KvmVM::createIRQChip
void createIRQChip()
Create an in-kernel interrupt controller.
Definition: vm.cc:486
ArmISA::irq
Bitfield< 1 > irq
Definition: miscregs_types.hh:327
KvmVM::started
bool started
Has delayedStartup() already been called?
Definition: vm.hh:530
Kvm::capUserNMI
bool capUserNMI() const
Support for BaseKvmCPU::kvmNonMaskableInterrupt().
Definition: vm.cc:113
KvmVM::disableMemSlot
void disableMemSlot(const MemSlot slot)
Disable a memory slot.
Definition: vm.cc:415
KvmVM::setIRQLine
void setIRQLine(uint32_t irq, bool high)
Set the status of an IRQ line using KVM_IRQ_LINE.
Definition: vm.cc:501
ArmISA::len
Bitfield< 18, 16 > len
Definition: miscregs_types.hh:439
KvmVM::vmFD
int vmFD
KVM VM file descriptor.
Definition: vm.hh:527
KvmVM::setTSSAddress
void setTSSAddress(Addr tss_address)
Setup a shared three-page memory region used by the internals of KVM.
Definition: vm.cc:479
KvmVM::hasKernelIRQChip
bool hasKernelIRQChip() const
Is in-kernel IRQ chip emulation enabled?
Definition: vm.hh:351
KvmVM::ioctl
int ioctl(int request, long p1) const
KVM VM ioctl interface.
Definition: vm.cc:576
ArmISA::vm
Bitfield< 0 > vm
Definition: miscregs_types.hh:281
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:406
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
Kvm::capXSave
bool capXSave() const
Support for getting and setting the kvm_xsave structure.
Definition: vm.cc:185
Kvm::capExtendedCPUID
bool capExtendedCPUID() const
Support for BaseKvmCPU::setCPUID2 and getSupportedCPUID().
Definition: vm.cc:107
KvmVM::operator=
KvmVM & operator=(const KvmVM &vm)
KvmVM::MemSlot::MemSlot
MemSlot()
Definition: vm.hh:367
KvmVM::allocVCPUID
long allocVCPUID()
Allocate a new vCPU ID within the VM.
Definition: vm.cc:559
KvmVM::memorySlots
std::vector< MemorySlot > memorySlots
Definition: vm.hh:548
KvmVM::notifyFork
void notifyFork()
Notify a child process of a fork.
Definition: vm.cc:319
Kvm::capIRQChip
bool capIRQChip() const
Support for creating an in-kernel IRQ chip model.
Definition: vm.cc:149
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:92
Kvm::capVCPUEvents
bool capVCPUEvents() const
Support for getting and setting the kvm_vcpu_events structure.
Definition: vm.cc:155

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17