2 * Copyright (c) 2012 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Authors: Andreas Sandberg
40 #include <linux/kvm.h>
41 #include <sys/ioctl.h>
43 #include <sys/types.h>
50 #include "cpu/kvm/vm.hh"
51 #include "debug/Kvm.hh"
52 #include "params/KvmVM.hh"
53 #include "sim/system.hh"
55 #define EXPECTED_KVM_API_VERSION 12
57 #if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
58 #error Unsupported KVM version
61 Kvm
*Kvm::instance
= NULL
;
64 : kvmFD(-1), apiVersion(-1), vcpuMMapSize(0)
66 kvmFD
= ::open("/dev/kvm", O_RDWR
);
68 fatal("KVM: Failed to open /dev/kvm\n");
70 apiVersion
= ioctl(KVM_GET_API_VERSION
);
71 if (apiVersion
!= EXPECTED_KVM_API_VERSION
)
72 fatal("KVM: Incompatible API version\n");
74 vcpuMMapSize
= ioctl(KVM_GET_VCPU_MMAP_SIZE
);
75 if (vcpuMMapSize
== -1)
76 panic("KVM: Failed to get virtual CPU MMAP size\n");
94 Kvm::capUserMemory() const
96 return checkExtension(KVM_CAP_USER_MEMORY
) != 0;
100 Kvm::capSetTSSAddress() const
102 return checkExtension(KVM_CAP_SET_TSS_ADDR
) != 0;
106 Kvm::capExtendedCPUID() const
108 return checkExtension(KVM_CAP_EXT_CPUID
) != 0;
112 Kvm::capUserNMI() const
114 #ifdef KVM_CAP_USER_NMI
115 return checkExtension(KVM_CAP_USER_NMI
) != 0;
122 Kvm::capCoalescedMMIO() const
124 return checkExtension(KVM_CAP_COALESCED_MMIO
);
128 Kvm::capOneReg() const
130 #ifdef KVM_CAP_ONE_REG
131 return checkExtension(KVM_CAP_ONE_REG
) != 0;
138 Kvm::capIRQChip() const
140 return checkExtension(KVM_CAP_IRQCHIP
) != 0;
144 Kvm::capVCPUEvents() const
146 #ifdef KVM_CAP_VCPU_EVENTS
147 return checkExtension(KVM_CAP_VCPU_EVENTS
) != 0;
154 Kvm::capDebugRegs() const
156 #ifdef KVM_CAP_DEBUGREGS
157 return checkExtension(KVM_CAP_DEBUGREGS
) != 0;
167 return checkExtension(KVM_CAP_XCRS
) != 0;
174 Kvm::capXSave() const
177 return checkExtension(KVM_CAP_XSAVE
) != 0;
184 Kvm::getSupportedCPUID(struct kvm_cpuid2
&cpuid
) const
186 #if defined(__i386__) || defined(__x86_64__)
187 if (ioctl(KVM_GET_SUPPORTED_CPUID
, (void *)&cpuid
) == -1) {
191 panic("KVM: Failed to get supported CPUID (errno: %i)\n", errno
);
195 panic("KVM: getSupportedCPUID is unsupported on this platform.\n");
199 const Kvm::CPUIDVector
&
200 Kvm::getSupportedCPUID() const
202 if (supportedCPUIDCache
.empty()) {
203 std::unique_ptr
<struct kvm_cpuid2
> cpuid
;
206 cpuid
.reset((struct kvm_cpuid2
*)operator new(
207 sizeof(kvm_cpuid2
) + i
* sizeof(kvm_cpuid_entry2
)));
211 } while (!getSupportedCPUID(*cpuid
));
212 supportedCPUIDCache
.assign(cpuid
->entries
,
213 cpuid
->entries
+ cpuid
->nent
);
216 return supportedCPUIDCache
;
220 Kvm::getSupportedMSRs(struct kvm_msr_list
&msrs
) const
222 #if defined(__i386__) || defined(__x86_64__)
223 if (ioctl(KVM_GET_MSR_INDEX_LIST
, (void *)&msrs
) == -1) {
227 panic("KVM: Failed to get supported CPUID (errno: %i)\n", errno
);
231 panic("KVM: getSupportedCPUID is unsupported on this platform.\n");
235 const Kvm::MSRIndexVector
&
236 Kvm::getSupportedMSRs() const
238 if (supportedMSRCache
.empty()) {
239 std::unique_ptr
<struct kvm_msr_list
> msrs
;
242 msrs
.reset((struct kvm_msr_list
*)operator new(
243 sizeof(kvm_msr_list
) + i
* sizeof(uint32_t)));
247 } while (!getSupportedMSRs(*msrs
));
248 supportedMSRCache
.assign(msrs
->indices
, msrs
->indices
+ msrs
->nmsrs
);
251 return supportedMSRCache
;
255 Kvm::checkExtension(int extension
) const
257 int ret
= ioctl(KVM_CHECK_EXTENSION
, extension
);
259 panic("KVM: ioctl failed when checking for extension\n");
264 Kvm::ioctl(int request
, long p1
) const
268 return ::ioctl(kvmFD
, request
, p1
);
276 vmFD
= ioctl(KVM_CREATE_VM
);
278 panic("Failed to create KVM VM\n");
284 KvmVM::KvmVM(KvmVMParams
*params
)
286 kvm(), system(params
->system
),
287 vmFD(kvm
.createVM()),
291 /* Setup the coalesced MMIO regions */
292 for (int i
= 0; i
< params
->coalescedMMIO
.size(); ++i
)
293 coalesceMMIO(params
->coalescedMMIO
[i
]);
312 KvmVM::delayedStartup()
314 const std::vector
<std::pair
<AddrRange
, uint8_t*> >&memories(
315 system
->getPhysMem().getBackingStore());
317 DPRINTF(Kvm
, "Mapping %i memory region(s)\n", memories
.size());
318 for (int slot(0); slot
< memories
.size(); ++slot
) {
319 const AddrRange
&range(memories
[slot
].first
);
320 void *pmem(memories
[slot
].second
);
323 DPRINTF(Kvm
, "Mapping region: 0x%p -> 0x%llx [size: 0x%llx]\n",
324 pmem
, range
.start(), range
.size());
326 setUserMemoryRegion(slot
, pmem
, range
, 0 /* flags */);
328 DPRINTF(Kvm
, "Zero-region not mapped: [0x%llx]\n", range
.start());
329 hack("KVM: Zero memory handled as IO\n");
335 KvmVM::setUserMemoryRegion(uint32_t slot
,
336 void *host_addr
, AddrRange guest_range
,
339 if (guest_range
.interleaved())
340 panic("Tried to map an interleaved memory range into a KVM VM.\n");
342 setUserMemoryRegion(slot
, host_addr
,
343 guest_range
.start(), guest_range
.size(),
348 KvmVM::setUserMemoryRegion(uint32_t slot
,
349 void *host_addr
, Addr guest_addr
,
350 uint64_t len
, uint32_t flags
)
352 struct kvm_userspace_memory_region m
;
354 memset(&m
, 0, sizeof(m
));
357 m
.guest_phys_addr
= (uint64_t)guest_addr
;
359 m
.userspace_addr
= (__u64
)host_addr
;
361 if (ioctl(KVM_SET_USER_MEMORY_REGION
, (void *)&m
) == -1) {
362 panic("Failed to setup KVM memory region:\n"
363 "\tHost Address: 0x%p\n"
364 "\tGuest Address: 0x%llx\n",
367 m
.userspace_addr
, m
.guest_phys_addr
,
368 m
.memory_size
, m
.flags
);
373 KvmVM::coalesceMMIO(const AddrRange
&range
)
375 coalesceMMIO(range
.start(), range
.size());
379 KvmVM::coalesceMMIO(Addr start
, int size
)
381 struct kvm_coalesced_mmio_zone zone
;
387 DPRINTF(Kvm
, "KVM: Registering coalesced MMIO region [0x%x, 0x%x]\n",
388 zone
.addr
, zone
.addr
+ zone
.size
- 1);
389 if (ioctl(KVM_REGISTER_COALESCED_MMIO
, (void *)&zone
) == -1)
390 panic("KVM: Failed to register coalesced MMIO region (%i)\n",
395 KvmVM::setTSSAddress(Addr tss_address
)
397 if (ioctl(KVM_SET_TSS_ADDR
, (unsigned long)tss_address
) == -1)
398 panic("KVM: Failed to set VM TSS address\n");
402 KvmVM::createIRQChip()
404 if (_hasKernelIRQChip
)
405 panic("KvmVM::createIRQChip called twice.\n");
407 if (ioctl(KVM_CREATE_IRQCHIP
) != -1) {
408 _hasKernelIRQChip
= true;
410 warn("KVM: Failed to create in-kernel IRQ chip (errno: %i)\n",
412 _hasKernelIRQChip
= false;
417 KvmVM::setIRQLine(uint32_t irq
, bool high
)
419 struct kvm_irq_level kvm_level
;
422 kvm_level
.level
= high
? 1 : 0;
424 if (ioctl(KVM_IRQ_LINE
, &kvm_level
) == -1)
425 panic("KVM: Failed to set IRQ line level (errno: %i)\n",
430 KvmVM::createVCPU(long vcpuID
)
434 fd
= ioctl(KVM_CREATE_VCPU
, vcpuID
);
436 panic("KVM: Failed to create virtual CPU");
448 KvmVM::ioctl(int request
, long p1
) const
452 return ::ioctl(vmFD
, request
, p1
);
457 KvmVMParams::create()
459 static bool created
= false;
461 warn_once("Use of multiple KvmVMs is currently untested!\n");
465 return new KvmVM(this);