gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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; }
356  struct MemSlot
357  {
358  MemSlot(uint32_t _num) : num(_num)
359  {}
360  MemSlot() : num(-1)
361  {}
362 
363  int32_t num;
364  };
365 
369  const MemSlot allocMemSlot(uint64_t size);
370 
379  void setupMemSlot(const MemSlot slot, void *host_addr, Addr guest_addr,
380  uint32_t flags);
381 
385  void disableMemSlot(const MemSlot slot);
386 
390  void freeMemSlot(const MemSlot slot);
391 
399  int createDevice(uint32_t type, uint32_t flags = 0);
400 
403 
407  void setSystem(System *s);
408 
412  long contextIdToVCpuId(ContextID ctx) const;
413 
414 #if defined(__aarch64__)
415  public: // ARM-specific
427  void kvmArmPreferredTarget(struct kvm_vcpu_init &target) const;
428 
429 #endif
430 
431  protected:
440  void cpuStartup();
441 
453  void delayedStartup();
454 
455 
466  void setUserMemoryRegion(uint32_t slot,
467  void *host_addr, Addr guest_addr,
468  uint64_t len, uint32_t flags);
477  int createVCPU(long vcpuID);
478 
487  long allocVCPUID();
488 
502  int ioctl(int request, long p1) const;
503  int ioctl(int request, void *p1) const {
504  return ioctl(request, (long)p1);
505  }
506  int ioctl(int request) const {
507  return ioctl(request, 0L);
508  }
511  private:
512  // Prevent copying
513  KvmVM(const KvmVM &vm);
514  // Prevent assignment
515  KvmVM &operator=(const KvmVM &vm);
516 
518 
520  int vmFD;
521 
523  bool started;
524 
527 
530 
535  {
536  public:
537  uint64_t size;
538  uint32_t slot;
539  bool active;
540  };
542  uint32_t maxMemorySlot;
543 };
544 
545 #endif
bool capDebugRegs() const
Support for getting and setting the kvm_debugregs structure.
Definition: vm.cc:167
int kvmFD
KVM VM file descriptor.
Definition: vm.hh:255
bool capUserMemory() const
Support for KvmVM::setUserMemoryRegion()
Definition: vm.cc:97
uint32_t maxMemorySlot
Definition: vm.hh:542
int vcpuMMapSize
Size of the MMAPed vCPU parameter area.
Definition: vm.hh:259
Bitfield< 7, 0 > L
Definition: int.hh:59
void createIRQChip()
Create an in-kernel interrupt controller.
Definition: vm.cc:487
void cpuStartup()
VM CPU initialization code.
Definition: vm.cc:334
int apiVersion
KVM API version.
Definition: vm.hh:257
long allocVCPUID()
Allocate a new vCPU ID within the VM.
Definition: vm.cc:560
long contextIdToVCpuId(ContextID ctx) const
Get the VCPUID for a given context.
Definition: vm.cc:540
const Params * params() const
Definition: sim_object.hh:111
uint64_t size
Definition: vm.hh:537
std::vector< MemorySlot > memorySlots
Definition: vm.hh:541
static Kvm * instance
Singleton instance.
Definition: vm.hh:262
int ioctl(int request) const
Definition: vm.hh:506
Definition: system.hh:83
Bitfield< 28, 21 > cpuid
Definition: dt_constants.hh:94
long nextVCPUID
Next unallocated vCPU ID.
Definition: vm.hh:529
int createVCPU(long vcpuID)
Create a new vCPU within a VM.
Definition: vm.cc:548
Base class for KVM based CPU models.
Definition: base.hh:78
int ioctl(int request, void *p1) const
Definition: vm.hh:503
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:433
bool _hasKernelIRQChip
Do we have in-kernel IRQ-chip emulation enabled?
Definition: vm.hh:526
bool capXCRs() const
Support for getting and setting the x86 XCRs.
Definition: vm.cc:177
bool hasKernelIRQChip() const
Is in-kernel IRQ chip emulation enabled?
Definition: vm.hh:353
bool capOneReg() const
Support for reading and writing single registers.
Definition: vm.cc:141
void setIRQLine(uint32_t irq, bool high)
Set the status of an IRQ line using KVM_IRQ_LINE.
Definition: vm.cc:502
STL vector class.
Definition: stl.hh:40
int checkExtension(int extension) const
Check for the presence of an extension to the KVM API.
Definition: vm.cc:265
System * system
Definition: vm.hh:517
int capNumMemSlots() const
Attempt to determine how many memory slots are available.
Definition: vm.cc:131
void setSystem(System *s)
Initialize system pointer.
Definition: vm.cc:532
MemSlot(uint32_t _num)
Definition: vm.hh:358
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:72
void freeMemSlot(const MemSlot slot)
Free a previously allocated memory slot.
Definition: vm.cc:425
Bitfield< 4 > s
Definition: miscregs.hh:1738
bool started
Has delayedStartup() already been called?
Definition: vm.hh:523
uint32_t slot
Definition: vm.hh:538
void delayedStartup()
Delayed initialization, executed once before the first CPU starts.
Definition: vm.cc:344
virtual ~KvmVM()
Definition: vm.cc:310
bool capSetTSSAddress() const
Support for KvmVM::setTSSAddress()
Definition: vm.cc:103
Kvm * create()
Definition: vm.cc:88
bool active
Definition: vm.hh:539
void coalesceMMIO(Addr start, int size)
Request coalescing MMIO for a memory range.
Definition: vm.cc:464
void setTSSAddress(Addr tss_address)
Setup a shared three-page memory region used by the internals of KVM.
Definition: vm.cc:480
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:534
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:407
Kvm()
Definition: vm.cc:66
int32_t num
Definition: vm.hh:363
KvmVM(KvmVMParams *params)
Definition: vm.cc:294
int getAPIVersion() const
Get the version of the KVM API implemented by the kernel.
Definition: vm.hh:84
int ioctl(int request, void *p1) const
Definition: vm.hh:230
int vmFD
KVM VM file descriptor.
Definition: vm.hh:520
KvmVM & operator=(const KvmVM &vm)
int createDevice(uint32_t type, uint32_t flags=0)
Create an in-kernel device model.
Definition: vm.cc:515
bool capUserNMI() const
Support for BaseKvmCPU::kvmNonMaskableInterrupt().
Definition: vm.cc:115
type
Definition: misc.hh:728
bool capVCPUEvents() const
Support for getting and setting the kvm_vcpu_events structure.
Definition: vm.cc:157
KVM VM container.
Definition: vm.hh:291
int size()
Definition: pagetable.hh:146
Bitfield< 1 > irq
Definition: miscregs.hh:1520
bool capXSave() const
Support for getting and setting the kvm_xsave structure.
Definition: vm.cc:187
Kvm & operator=(const Kvm &kvm)
int capCoalescedMMIO() const
Check if coalesced MMIO is supported and which page in the MMAP'ed structure it stores requests in...
Definition: vm.cc:125
int ioctl(int request, long p1) const
KVM VM ioctl interface.
Definition: vm.cc:577
Bitfield< 18, 16 > len
Definition: miscregs.hh:1626
void notifyFork()
Notify a child process of a fork.
Definition: vm.cc:320
Kvm * kvm
Global KVM interface.
Definition: vm.hh:402
bool capIRQChip() const
Support for creating an in-kernel IRQ chip model.
Definition: vm.cc:151
int ioctl(int request, long p1) const
Main VM ioctl interface.
Definition: vm.cc:274
int getVCPUMMapSize() const
Get the size of the MMAPed parameter area used to communicate vCPU parameters between the kernel and ...
Definition: vm.hh:90
Bitfield< 0 > vm
Definition: miscregs.hh:1482
KVM parent interface.
Definition: vm.hh:74
void disableMemSlot(const MemSlot slot)
Disable a memory slot.
Definition: vm.cc:416
const MemSlot allocMemSlot(uint64_t size)
Allocate a memory slot within the VM.
Definition: vm.cc:379
Abstract superclass for simulation objects.
Definition: sim_object.hh:94
int ContextID
Globally unique thread context ID.
Definition: types.hh:175
bool capExtendedCPUID() const
Support for BaseKvmCPU::setCPUID2 and getSupportedCPUID().
Definition: vm.cc:109
virtual ~Kvm()
Definition: vm.cc:82
int ioctl(int request) const
Definition: vm.hh:233

Generated on Fri Jun 9 2017 13:03:42 for gem5 by doxygen 1.8.6