gem5  v22.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 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
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
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:82
Base class for KVM based CPU models.
Definition: base.hh:88
Structures tracking memory slots.
Definition: vm.hh:552
KVM VM container.
Definition: vm.hh:302
virtual ~KvmVM()
Definition: vm.cc:334
long contextIdToVCpuId(ContextID ctx) const
Get the VCPUID for a given context.
Definition: vm.cc:566
const MemSlot allocMemSlot(uint64_t size)
Allocate a memory slot within the VM.
Definition: vm.cc:402
long nextVCPUID
Next unallocated vCPU ID.
Definition: vm.hh:546
void notifyFork()
Notify a child process of a fork.
Definition: vm.cc:344
bool validEnvironment() const
Verify gem5 configuration will support KVM emulation.
Definition: vm.cc:555
uint32_t maxMemorySlot
Definition: vm.hh:559
int createVCPU(long vcpuID)
Create a new vCPU within a VM.
Definition: vm.cc:573
KvmVM(const KvmVMParams &params)
Definition: vm.cc:316
bool started
Has delayedStartup() already been called?
Definition: vm.hh:540
std::vector< MemorySlot > memorySlots
Definition: vm.hh:558
System * system
Definition: vm.hh:534
KvmVM(const KvmVM &vm)
Kvm * kvm
Global KVM interface.
Definition: vm.hh:421
int vmFD
KVM VM file descriptor.
Definition: vm.hh:537
void freeMemSlot(const MemSlot slot)
Free a previously allocated memory slot.
Definition: vm.cc:448
long allocVCPUID()
Allocate a new vCPU ID within the VM.
Definition: vm.cc:585
KvmVM & operator=(const KvmVM &vm)
void cpuStartup()
VM CPU initialization code.
Definition: vm.cc:358
void setTSSAddress(Addr tss_address)
Setup a shared three-page memory region used by the internals of KVM.
Definition: vm.cc:503
void disableMemSlot(const MemSlot slot)
Disable a memory slot.
Definition: vm.cc:439
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
bool _hasKernelIRQChip
Do we have in-kernel IRQ-chip emulation enabled?
Definition: vm.hh:543
void delayedStartup()
Delayed initialization, executed once before the first CPU starts.
Definition: vm.cc:368
int createDevice(uint32_t type, uint32_t flags=0)
Create an in-kernel device model.
Definition: vm.cc:538
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
void coalesceMMIO(Addr start, int size)
Request coalescing MMIO for a memory range.
Definition: vm.cc:487
KVM parent interface.
Definition: vm.hh:81
virtual ~Kvm()
Definition: vm.cc:93
int apiVersion
KVM API version.
Definition: vm.hh:267
int checkExtension(int extension) const
Check for the presence of an extension to the KVM API.
Definition: vm.cc:287
int capNumMemSlots() const
Attempt to determine how many memory slots are available.
Definition: vm.cc:142
bool capIRQLineLayout2() const
Support for ARM IRQ line layout 2.
Definition: vm.cc:208
bool capXCRs() const
Support for getting and setting the x86 XCRs.
Definition: vm.cc:188
Kvm * create()
Definition: vm.cc:99
bool capIRQChip() const
Support for creating an in-kernel IRQ chip model.
Definition: vm.cc:162
bool capExtendedCPUID() const
Support for BaseKvmCPU::setCPUID2 and getSupportedCPUID().
Definition: vm.cc:120
bool capSetTSSAddress() const
Support for KvmVM::setTSSAddress()
Definition: vm.cc:114
int createVM()
Create a KVM Virtual Machine.
Definition: vm.cc:304
bool capUserNMI() const
Support for BaseKvmCPU::kvmNonMaskableInterrupt().
Definition: vm.cc:126
bool capDebugRegs() const
Support for getting and setting the kvm_debugregs structure.
Definition: vm.cc:178
Kvm & operator=(const Kvm &kvm)
int getVCPUMMapSize() const
Get the size of the MMAPed parameter area used to communicate vCPU parameters between the kernel and ...
Definition: vm.hh:96
int getAPIVersion() const
Get the version of the KVM API implemented by the kernel.
Definition: vm.hh:90
int vcpuMMapSize
Size of the MMAPed vCPU parameter area.
Definition: vm.hh:269
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
bool capXSave() const
Support for getting and setting the kvm_xsave structure.
Definition: vm.cc:198
Kvm(const Kvm &kvm)
Kvm()
Definition: vm.cc:71
bool capUserMemory() const
Support for KvmVM::setUserMemoryRegion()
Definition: vm.cc:108
bool capVCPUEvents() const
Support for getting and setting the kvm_vcpu_events structure.
Definition: vm.cc:168
bool capOneReg() const
Support for reading and writing single registers.
Definition: vm.cc:152
static Kvm * instance
Singleton instance.
Definition: vm.hh:272
int kvmFD
KVM VM file descriptor.
Definition: vm.hh:265
Abstract superclass for simulation objects.
Definition: sim_object.hh:148
STL vector class.
Definition: stl.hh:37
bool hasKernelIRQChip() const
Is in-kernel IRQ chip emulation enabled?
Definition: vm.hh:363
void setIRQLine(uint32_t irq, bool high)
Set the status of an IRQ line using KVM_IRQ_LINE.
Definition: vm.cc:525
void enableKernelIRQChip()
Tell the VM and VCPUs to use an in-kernel IRQ chip for interrupt delivery.
Definition: vm.hh:372
void createIRQChip()
Create an in-kernel interrupt controller.
Definition: vm.cc:510
int ioctl(int request, void *p1) const
Definition: vm.hh:520
int ioctl(int request, long p1) const
Main VM ioctl interface.
Definition: vm.cc:296
int ioctl(int request, void *p1) const
Definition: vm.hh:240
int ioctl(int request, long p1) const
KVM VM ioctl interface.
Definition: vm.cc:602
int ioctl(int request) const
Definition: vm.hh:243
int ioctl(int request) const
Definition: vm.hh:523
const Params & params() const
Definition: sim_object.hh:176
uint16_t len
Definition: helpers.cc:62
uint8_t flags
Definition: helpers.cc:66
Bitfield< 0 > vm
Definition: misc_types.hh:291
Bitfield< 1 > irq
Definition: misc_types.hh:337
Bitfield< 28, 21 > cpuid
Definition: dt_constants.hh:95
Bitfield< 7, 0 > L
Definition: int.hh:62
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
high
Definition: intmath.hh:176
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
int ContextID
Globally unique thread context ID.
Definition: types.hh:239
MemSlot(uint32_t _num)
Definition: vm.hh:377
int32_t num
Definition: vm.hh:382

Generated on Wed Dec 21 2022 10:22:30 for gem5 by doxygen 1.9.1