gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Andreas Sandberg
39  */
40 
41 #ifndef __CPU_KVM_KVMVM_HH__
42 #define __CPU_KVM_KVMVM_HH__
43 
44 #include <vector>
45 
46 #include "base/addr_range.hh"
47 #include "sim/sim_object.hh"
48 
49 // forward declarations
50 struct KvmVMParams;
51 class BaseKvmCPU;
52 class System;
53 
74 class Kvm
75 {
76  friend class KvmVM;
77 
78  public:
79  virtual ~Kvm();
80 
81  Kvm *create();
82 
84  int getAPIVersion() const { return apiVersion; }
90  int getVCPUMMapSize() const { return vcpuMMapSize; }
91 
94  bool capUserMemory() const;
96  bool capSetTSSAddress() const;
98  bool capExtendedCPUID() const;
100  bool capUserNMI() const;
101 
109  int capCoalescedMMIO() const;
110 
115  int capNumMemSlots() const;
116 
122  bool capOneReg() const;
123 
129  bool capIRQChip() const;
130 
132  bool capVCPUEvents() const;
133 
135  bool capDebugRegs() const;
136 
138  bool capXCRs() const;
139 
141  bool capXSave() const;
144 #if defined(__i386__) || defined(__x86_64__)
145  public: // x86-specific
151  typedef std::vector<struct kvm_cpuid_entry2> CPUIDVector;
152  typedef std::vector<uint32_t> MSRIndexVector;
153 
161  bool getSupportedCPUID(struct kvm_cpuid2 &cpuid) const;
162 
173  const CPUIDVector &getSupportedCPUID() const;
174 
180  bool getSupportedMSRs(struct kvm_msr_list &msrs) const;
181 
190  const MSRIndexVector &getSupportedMSRs() const;
191 
192  private: // x86-specific
194  mutable CPUIDVector supportedCPUIDCache;
195 
197  mutable MSRIndexVector supportedMSRCache;
198 
199 
201 #endif
202 
203  protected:
214  int checkExtension(int extension) const;
215 
229  int ioctl(int request, long p1) const;
230  int ioctl(int request, void *p1) const {
231  return ioctl(request, (long)p1);
232  }
233  int ioctl(int request) const {
234  return ioctl(request, 0L);
235  }
238  private:
239  // This object is a singleton, so prevent instantiation.
240  Kvm();
241 
242  // Prevent copying
243  Kvm(const Kvm &kvm);
244  // Prevent assignment
245  Kvm &operator=(const Kvm &kvm);
246 
252  int createVM();
253 
255  int kvmFD;
260 
262  static Kvm *instance;
263 };
264 
291 class KvmVM : public SimObject
292 {
293  friend class BaseKvmCPU;
294 
295  public:
296  KvmVM(KvmVMParams *params);
297  virtual ~KvmVM();
298 
299  void notifyFork();
300 
307  void setTSSAddress(Addr tss_address);
308 
316  void coalesceMMIO(Addr start, int size);
317 
323  void coalesceMMIO(const AddrRange &range);
335  void createIRQChip();
336 
348  void setIRQLine(uint32_t irq, bool high);
349 
353  bool hasKernelIRQChip() const { return _hasKernelIRQChip; }
354 
362  void enableKernelIRQChip() { _hasKernelIRQChip = true; }
365  struct MemSlot
366  {
367  MemSlot(uint32_t _num) : num(_num)
368  {}
369  MemSlot() : num(-1)
370  {}
371 
372  int32_t num;
373  };
374 
378  const MemSlot allocMemSlot(uint64_t size);
379 
388  void setupMemSlot(const MemSlot slot, void *host_addr, Addr guest_addr,
389  uint32_t flags);
390 
394  void disableMemSlot(const MemSlot slot);
395 
399  void freeMemSlot(const MemSlot slot);
400 
408  int createDevice(uint32_t type, uint32_t flags = 0);
409 
412 
416  void setSystem(System *s);
417 
421  long contextIdToVCpuId(ContextID ctx) const;
422 
423 #if defined(__aarch64__)
424  public: // ARM-specific
436  void kvmArmPreferredTarget(struct kvm_vcpu_init &target) const;
437 
438 #endif
439 
440  protected:
449  void cpuStartup();
450 
462  void delayedStartup();
463 
464 
475  void setUserMemoryRegion(uint32_t slot,
476  void *host_addr, Addr guest_addr,
477  uint64_t len, uint32_t flags);
486  int createVCPU(long vcpuID);
487 
496  long allocVCPUID();
497 
511  int ioctl(int request, long p1) const;
512  int ioctl(int request, void *p1) const {
513  return ioctl(request, (long)p1);
514  }
515  int ioctl(int request) const {
516  return ioctl(request, 0L);
517  }
520  private:
521  // Prevent copying
522  KvmVM(const KvmVM &vm);
523  // Prevent assignment
524  KvmVM &operator=(const KvmVM &vm);
525 
527 
529  int vmFD;
530 
532  bool started;
533 
536 
539 
544  {
545  public:
546  uint64_t size;
547  uint32_t slot;
548  bool active;
549  };
551  uint32_t maxMemorySlot;
552 };
553 
554 #endif
int kvmFD
KVM VM file descriptor.
Definition: vm.hh:255
bool capExtendedCPUID() const
Support for BaseKvmCPU::setCPUID2 and getSupportedCPUID().
Definition: vm.cc:109
int ioctl(int request, void *p1) const
Definition: vm.hh:512
uint32_t maxMemorySlot
Definition: vm.hh:551
int vcpuMMapSize
Size of the MMAPed vCPU parameter area.
Definition: vm.hh:259
Bitfield< 7, 0 > L
Definition: int.hh:59
void enableKernelIRQChip()
Tell the VM and VCPUs to use an in-kernel IRQ chip for interrupt delivery.
Definition: vm.hh:362
void notifyFork() override
Notify a child process of a fork.
Definition: base.cc:443
int apiVersion
KVM API version.
Definition: vm.hh:257
bool hasKernelIRQChip() const
Is in-kernel IRQ chip emulation enabled?
Definition: vm.hh:353
friend class KvmVM
Definition: vm.hh:76
uint64_t size
Definition: vm.hh:546
std::vector< MemorySlot > memorySlots
Definition: vm.hh:550
int capNumMemSlots() const
Attempt to determine how many memory slots are available.
Definition: vm.cc:131
static Kvm * instance
Singleton instance.
Definition: vm.hh:262
Definition: system.hh:77
int getAPIVersion() const
Get the version of the KVM API implemented by the kernel.
Definition: vm.hh:84
Bitfield< 28, 21 > cpuid
Definition: dt_constants.hh:94
long nextVCPUID
Next unallocated vCPU ID.
Definition: vm.hh:538
Base class for KVM based CPU models.
Definition: base.hh:79
bool capVCPUEvents() const
Support for getting and setting the kvm_vcpu_events structure.
Definition: vm.cc:157
int ioctl(int request, long p1) const
Main VM ioctl interface.
Definition: vm.cc:274
bool capOneReg() const
Support for reading and writing single registers.
Definition: vm.cc:141
bool _hasKernelIRQChip
Do we have in-kernel IRQ-chip emulation enabled?
Definition: vm.hh:535
STL vector class.
Definition: stl.hh:40
System * system
Definition: vm.hh:526
bool capUserNMI() const
Support for BaseKvmCPU::kvmNonMaskableInterrupt().
Definition: vm.cc:115
MemSlot(uint32_t _num)
Definition: vm.hh:367
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:72
uint8_t type
Definition: inet.hh:333
Bitfield< 4 > s
bool started
Has delayedStartup() already been called?
Definition: vm.hh:532
uint32_t slot
Definition: vm.hh:547
int checkExtension(int extension) const
Check for the presence of an extension to the KVM API.
Definition: vm.cc:265
int ioctl(int request) const
Definition: vm.hh:515
Bitfield< 18, 16 > len
Kvm * create()
Definition: vm.cc:88
bool active
Definition: vm.hh:548
int createVM()
Create a KVM Virtual Machine.
Definition: vm.cc:282
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
Structures tracking memory slots.
Definition: vm.hh:543
Kvm()
Definition: vm.cc:66
int32_t num
Definition: vm.hh:372
int vmFD
KVM VM file descriptor.
Definition: vm.hh:529
KVM VM container.
Definition: vm.hh:291
Bitfield< 1 > irq
bool capSetTSSAddress() const
Support for KvmVM::setTSSAddress()
Definition: vm.cc:103
Kvm & operator=(const Kvm &kvm)
bool capUserMemory() const
Support for KvmVM::setUserMemoryRegion()
Definition: vm.cc:97
bool capXCRs() const
Support for getting and setting the x86 XCRs.
Definition: vm.cc:177
bool capXSave() const
Support for getting and setting the kvm_xsave structure.
Definition: vm.cc:187
int capCoalescedMMIO() const
Check if coalesced MMIO is supported and which page in the MMAP&#39;ed structure it stores requests in...
Definition: vm.cc:125
bool capIRQChip() const
Support for creating an in-kernel IRQ chip model.
Definition: vm.cc:151
Kvm * kvm
Global KVM interface.
Definition: vm.hh:411
bool capDebugRegs() const
Support for getting and setting the kvm_debugregs structure.
Definition: vm.cc:167
Bitfield< 0 > vm
KVM parent interface.
Definition: vm.hh:74
int getVCPUMMapSize() const
Get the size of the MMAPed parameter area used to communicate vCPU parameters between the kernel and ...
Definition: vm.hh:90
int ioctl(int request, void *p1) const
Definition: vm.hh:230
const Params * params() const
Definition: base.hh:311
Abstract superclass for simulation objects.
Definition: sim_object.hh:96
int ContextID
Globally unique thread context ID.
Definition: types.hh:231
virtual ~Kvm()
Definition: vm.cc:82
int ioctl(int request) const
Definition: vm.hh:233

Generated on Fri Feb 28 2020 16:26:59 for gem5 by doxygen 1.8.13