2 * Copyright 2014 Google, Inc.
3 * Copyright (c) 2012, 2015 ARM Limited
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.
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.
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.
39 #include "cpu/kvm/vm.hh"
42 #include <linux/kvm.h>
43 #include <sys/ioctl.h>
45 #include <sys/types.h>
51 #include "cpu/kvm/base.hh"
52 #include "debug/Kvm.hh"
53 #include "params/KvmVM.hh"
54 #include "sim/system.hh"
56 #define EXPECTED_KVM_API_VERSION 12
58 #if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
59 #error Unsupported KVM version
62 Kvm
*Kvm::instance
= NULL
;
65 : kvmFD(-1), apiVersion(-1), vcpuMMapSize(0)
67 static bool created
= false;
69 warn_once("Use of multiple KvmVMs is currently untested!");
73 kvmFD
= ::open("/dev/kvm", O_RDWR
);
75 fatal("KVM: Failed to open /dev/kvm\n");
77 apiVersion
= ioctl(KVM_GET_API_VERSION
);
78 if (apiVersion
!= EXPECTED_KVM_API_VERSION
)
79 fatal("KVM: Incompatible API version\n");
81 vcpuMMapSize
= ioctl(KVM_GET_VCPU_MMAP_SIZE
);
82 if (vcpuMMapSize
== -1)
83 panic("KVM: Failed to get virtual CPU MMAP size\n");
101 Kvm::capUserMemory() const
103 return checkExtension(KVM_CAP_USER_MEMORY
) != 0;
107 Kvm::capSetTSSAddress() const
109 return checkExtension(KVM_CAP_SET_TSS_ADDR
) != 0;
113 Kvm::capExtendedCPUID() const
115 return checkExtension(KVM_CAP_EXT_CPUID
) != 0;
119 Kvm::capUserNMI() const
121 #ifdef KVM_CAP_USER_NMI
122 return checkExtension(KVM_CAP_USER_NMI
) != 0;
129 Kvm::capCoalescedMMIO() const
131 return checkExtension(KVM_CAP_COALESCED_MMIO
);
135 Kvm::capNumMemSlots() const
137 #ifdef KVM_CAP_NR_MEMSLOTS
138 return checkExtension(KVM_CAP_NR_MEMSLOTS
);
145 Kvm::capOneReg() const
147 #ifdef KVM_CAP_ONE_REG
148 return checkExtension(KVM_CAP_ONE_REG
) != 0;
155 Kvm::capIRQChip() const
157 return checkExtension(KVM_CAP_IRQCHIP
) != 0;
161 Kvm::capVCPUEvents() const
163 #ifdef KVM_CAP_VCPU_EVENTS
164 return checkExtension(KVM_CAP_VCPU_EVENTS
) != 0;
171 Kvm::capDebugRegs() const
173 #ifdef KVM_CAP_DEBUGREGS
174 return checkExtension(KVM_CAP_DEBUGREGS
) != 0;
184 return checkExtension(KVM_CAP_XCRS
) != 0;
191 Kvm::capXSave() const
194 return checkExtension(KVM_CAP_XSAVE
) != 0;
201 #if defined(__i386__) || defined(__x86_64__)
203 Kvm::getSupportedCPUID(struct kvm_cpuid2
&cpuid
) const
205 if (ioctl(KVM_GET_SUPPORTED_CPUID
, (void *)&cpuid
) == -1) {
209 panic("KVM: Failed to get supported CPUID (errno: %i)\n", errno
);
214 const Kvm::CPUIDVector
&
215 Kvm::getSupportedCPUID() const
217 if (supportedCPUIDCache
.empty()) {
218 std::unique_ptr
<struct kvm_cpuid2
> cpuid
;
221 cpuid
.reset((struct kvm_cpuid2
*)operator new(
222 sizeof(kvm_cpuid2
) + i
* sizeof(kvm_cpuid_entry2
)));
226 } while (!getSupportedCPUID(*cpuid
));
227 supportedCPUIDCache
.assign(cpuid
->entries
,
228 cpuid
->entries
+ cpuid
->nent
);
231 return supportedCPUIDCache
;
235 Kvm::getSupportedMSRs(struct kvm_msr_list
&msrs
) const
237 if (ioctl(KVM_GET_MSR_INDEX_LIST
, (void *)&msrs
) == -1) {
241 panic("KVM: Failed to get supported CPUID (errno: %i)\n", errno
);
246 const Kvm::MSRIndexVector
&
247 Kvm::getSupportedMSRs() const
249 if (supportedMSRCache
.empty()) {
250 std::unique_ptr
<struct kvm_msr_list
> msrs
;
253 msrs
.reset((struct kvm_msr_list
*)operator new(
254 sizeof(kvm_msr_list
) + i
* sizeof(uint32_t)));
258 } while (!getSupportedMSRs(*msrs
));
259 supportedMSRCache
.assign(msrs
->indices
, msrs
->indices
+ msrs
->nmsrs
);
262 return supportedMSRCache
;
265 #endif // x86-specific
269 Kvm::checkExtension(int extension
) const
271 int ret
= ioctl(KVM_CHECK_EXTENSION
, extension
);
273 panic("KVM: ioctl failed when checking for extension\n");
278 Kvm::ioctl(int request
, long p1
) const
282 return ::ioctl(kvmFD
, request
, p1
);
290 vmFD
= ioctl(KVM_CREATE_VM
);
292 panic("Failed to create KVM VM\n");
298 KvmVM::KvmVM(const KvmVMParams
¶ms
)
300 kvm(new Kvm()), system(nullptr),
301 vmFD(kvm
->createVM()),
303 _hasKernelIRQChip(false),
306 maxMemorySlot
= kvm
->capNumMemSlots();
307 /* If we couldn't determine how memory slots there are, guess 32. */
310 /* Setup the coalesced MMIO regions */
311 for (int i
= 0; i
< params
.coalescedMMIO
.size(); ++i
)
312 coalesceMMIO(params
.coalescedMMIO
[i
]);
328 if (close(vmFD
) == -1)
329 warn("kvm VM: notifyFork failed to close vmFD\n");
349 KvmVM::delayedStartup()
351 assert(system
); // set by the system during its construction
352 const std::vector
<BackingStoreEntry
> &memories(
353 system
->getPhysMem().getBackingStore());
355 DPRINTF(Kvm
, "Mapping %i memory region(s)\n", memories
.size());
356 for (int slot(0); slot
< memories
.size(); ++slot
) {
357 if (!memories
[slot
].kvmMap
) {
358 DPRINTF(Kvm
, "Skipping region marked as not usable by KVM\n");
362 const AddrRange
&range(memories
[slot
].range
);
363 void *pmem(memories
[slot
].pmem
);
366 DPRINTF(Kvm
, "Mapping region: 0x%p -> 0x%llx [size: 0x%llx]\n",
367 pmem
, range
.start(), range
.size());
369 if (range
.interleaved()) {
370 panic("Tried to map an interleaved memory range into "
374 const MemSlot slot
= allocMemSlot(range
.size());
375 setupMemSlot(slot
, pmem
, range
.start(), 0/* flags */);
377 DPRINTF(Kvm
, "Zero-region not mapped: [0x%llx]\n", range
.start());
378 hack("KVM: Zero memory handled as IO\n");
384 KvmVM::allocMemSlot(uint64_t size
)
387 panic("Memory slots must have non-zero size.\n");
389 std::vector
<MemorySlot
>::iterator pos
;
390 for (pos
= memorySlots
.begin(); pos
!= memorySlots
.end(); pos
++) {
398 uint32_t nextSlot
= memorySlots
.size();
399 if (nextSlot
> maxMemorySlot
)
400 panic("Out of memory slots.\n");
404 slot
.slot
= nextSlot
;
407 memorySlots
.push_back(slot
);
408 return MemSlot(slot
.slot
);
412 KvmVM::setupMemSlot(const KvmVM::MemSlot num
, void *host_addr
, Addr guest
,
415 MemorySlot
&slot
= memorySlots
.at(num
.num
);
417 setUserMemoryRegion(num
.num
, host_addr
, guest
, slot
.size
, flags
);
421 KvmVM::disableMemSlot(const KvmVM::MemSlot num
)
423 MemorySlot
&slot
= memorySlots
.at(num
.num
);
425 setUserMemoryRegion(num
.num
, NULL
, 0, 0, 0);
430 KvmVM::freeMemSlot(const KvmVM::MemSlot num
)
432 disableMemSlot(num
.num
);
433 MemorySlot
&slot
= memorySlots
.at(num
.num
);
438 KvmVM::setUserMemoryRegion(uint32_t slot
,
439 void *host_addr
, Addr guest_addr
,
440 uint64_t len
, uint32_t flags
)
442 struct kvm_userspace_memory_region m
;
444 memset(&m
, 0, sizeof(m
));
447 m
.guest_phys_addr
= (uint64_t)guest_addr
;
449 m
.userspace_addr
= (__u64
)host_addr
;
451 if (ioctl(KVM_SET_USER_MEMORY_REGION
, (void *)&m
) == -1) {
452 panic("Failed to setup KVM memory region:\n"
453 "\tHost Address: 0x%p\n"
454 "\tGuest Address: 0x%llx\n",
457 m
.userspace_addr
, m
.guest_phys_addr
,
458 m
.memory_size
, m
.flags
);
463 KvmVM::coalesceMMIO(const AddrRange
&range
)
465 coalesceMMIO(range
.start(), range
.size());
469 KvmVM::coalesceMMIO(Addr start
, int size
)
471 struct kvm_coalesced_mmio_zone zone
;
477 DPRINTF(Kvm
, "KVM: Registering coalesced MMIO region [0x%x, 0x%x]\n",
478 zone
.addr
, zone
.addr
+ zone
.size
- 1);
479 if (ioctl(KVM_REGISTER_COALESCED_MMIO
, (void *)&zone
) == -1)
480 panic("KVM: Failed to register coalesced MMIO region (%i)\n",
485 KvmVM::setTSSAddress(Addr tss_address
)
487 if (ioctl(KVM_SET_TSS_ADDR
, (unsigned long)tss_address
) == -1)
488 panic("KVM: Failed to set VM TSS address\n");
492 KvmVM::createIRQChip()
494 if (_hasKernelIRQChip
)
495 panic("KvmVM::createIRQChip called twice.\n");
497 if (ioctl(KVM_CREATE_IRQCHIP
) != -1) {
498 _hasKernelIRQChip
= true;
500 warn("KVM: Failed to create in-kernel IRQ chip (errno: %i)\n",
502 _hasKernelIRQChip
= false;
507 KvmVM::setIRQLine(uint32_t irq
, bool high
)
509 struct kvm_irq_level kvm_level
;
512 kvm_level
.level
= high
? 1 : 0;
514 if (ioctl(KVM_IRQ_LINE
, &kvm_level
) == -1)
515 panic("KVM: Failed to set IRQ line level (errno: %i)\n",
520 KvmVM::createDevice(uint32_t type
, uint32_t flags
)
522 #if defined(KVM_CREATE_DEVICE)
523 struct kvm_create_device dev
= { type
, 0, flags
};
525 if (ioctl(KVM_CREATE_DEVICE
, &dev
) == -1) {
526 panic("KVM: Failed to create device (errno: %i)\n",
532 panic("Kernel headers don't support KVM_CREATE_DEVICE\n");
537 KvmVM::setSystem(System
*s
)
539 panic_if(system
!= nullptr, "setSystem() can only be called once");
540 panic_if(s
== nullptr, "setSystem() called with null System*");
545 KvmVM::contextIdToVCpuId(ContextID ctx
) const
547 assert(system
!= nullptr);
548 return dynamic_cast<BaseKvmCPU
*>
549 (system
->threads
[ctx
]->getCpuPtr())->getVCpuID();
553 KvmVM::createVCPU(long vcpuID
)
557 fd
= ioctl(KVM_CREATE_VCPU
, vcpuID
);
559 panic("KVM: Failed to create virtual CPU");
570 #if defined(__aarch64__)
572 KvmVM::kvmArmPreferredTarget(struct kvm_vcpu_init
&target
) const
574 if (ioctl(KVM_ARM_PREFERRED_TARGET
, &target
) == -1) {
575 panic("KVM: Failed to get ARM preferred CPU target (errno: %i)\n",
582 KvmVM::ioctl(int request
, long p1
) const
586 return ::ioctl(vmFD
, request
, p1
);