vm.cc (11614:29606f000389) vm.cc (11793:ef606668d247)
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
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#include "cpu/kvm/vm.hh"
42
43#include <fcntl.h>
41#include <linux/kvm.h>
42#include <sys/ioctl.h>
43#include <sys/stat.h>
44#include <sys/types.h>
44#include <linux/kvm.h>
45#include <sys/ioctl.h>
46#include <sys/stat.h>
47#include <sys/types.h>
45#include <fcntl.h>
46#include <unistd.h>
47
48#include <cerrno>
49#include <memory>
50
48#include <unistd.h>
49
50#include <cerrno>
51#include <memory>
52
51#include "cpu/kvm/vm.hh"
52#include "debug/Kvm.hh"
53#include "params/KvmVM.hh"
54#include "sim/system.hh"
55
56#define EXPECTED_KVM_API_VERSION 12
57
58#if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
59#error Unsupported KVM version
60#endif
61
62Kvm *Kvm::instance = NULL;
63
64Kvm::Kvm()
65 : kvmFD(-1), apiVersion(-1), vcpuMMapSize(0)
66{
67 kvmFD = ::open("/dev/kvm", O_RDWR);
68 if (kvmFD == -1)
69 fatal("KVM: Failed to open /dev/kvm\n");
70
71 apiVersion = ioctl(KVM_GET_API_VERSION);
72 if (apiVersion != EXPECTED_KVM_API_VERSION)
73 fatal("KVM: Incompatible API version\n");
74
75 vcpuMMapSize = ioctl(KVM_GET_VCPU_MMAP_SIZE);
76 if (vcpuMMapSize == -1)
77 panic("KVM: Failed to get virtual CPU MMAP size\n");
78}
79
80Kvm::~Kvm()
81{
82 close(kvmFD);
83}
84
85Kvm *
86Kvm::create()
87{
88 if (!instance)
89 instance = new Kvm();
90
91 return instance;
92}
93
94bool
95Kvm::capUserMemory() const
96{
97 return checkExtension(KVM_CAP_USER_MEMORY) != 0;
98}
99
100bool
101Kvm::capSetTSSAddress() const
102{
103 return checkExtension(KVM_CAP_SET_TSS_ADDR) != 0;
104}
105
106bool
107Kvm::capExtendedCPUID() const
108{
109 return checkExtension(KVM_CAP_EXT_CPUID) != 0;
110}
111
112bool
113Kvm::capUserNMI() const
114{
115#ifdef KVM_CAP_USER_NMI
116 return checkExtension(KVM_CAP_USER_NMI) != 0;
117#else
118 return false;
119#endif
120}
121
122int
123Kvm::capCoalescedMMIO() const
124{
125 return checkExtension(KVM_CAP_COALESCED_MMIO);
126}
127
128int
129Kvm::capNumMemSlots() const
130{
131#ifdef KVM_CAP_NR_MEMSLOTS
132 return checkExtension(KVM_CAP_NR_MEMSLOTS);
133#else
134 return 0;
135#endif
136}
137
138bool
139Kvm::capOneReg() const
140{
141#ifdef KVM_CAP_ONE_REG
142 return checkExtension(KVM_CAP_ONE_REG) != 0;
143#else
144 return false;
145#endif
146}
147
148bool
149Kvm::capIRQChip() const
150{
151 return checkExtension(KVM_CAP_IRQCHIP) != 0;
152}
153
154bool
155Kvm::capVCPUEvents() const
156{
157#ifdef KVM_CAP_VCPU_EVENTS
158 return checkExtension(KVM_CAP_VCPU_EVENTS) != 0;
159#else
160 return false;
161#endif
162}
163
164bool
165Kvm::capDebugRegs() const
166{
167#ifdef KVM_CAP_DEBUGREGS
168 return checkExtension(KVM_CAP_DEBUGREGS) != 0;
169#else
170 return false;
171#endif
172}
173
174bool
175Kvm::capXCRs() const
176{
177#ifdef KVM_CAP_XCRS
178 return checkExtension(KVM_CAP_XCRS) != 0;
179#else
180 return false;
181#endif
182}
183
184bool
185Kvm::capXSave() const
186{
187#ifdef KVM_CAP_XSAVE
188 return checkExtension(KVM_CAP_XSAVE) != 0;
189#else
190 return false;
191#endif
192}
193
194
195#if defined(__i386__) || defined(__x86_64__)
196bool
197Kvm::getSupportedCPUID(struct kvm_cpuid2 &cpuid) const
198{
199 if (ioctl(KVM_GET_SUPPORTED_CPUID, (void *)&cpuid) == -1) {
200 if (errno == E2BIG)
201 return false;
202 else
203 panic("KVM: Failed to get supported CPUID (errno: %i)\n", errno);
204 } else
205 return true;
206}
207
208const Kvm::CPUIDVector &
209Kvm::getSupportedCPUID() const
210{
211 if (supportedCPUIDCache.empty()) {
212 std::unique_ptr<struct kvm_cpuid2> cpuid;
213 int i(1);
214 do {
215 cpuid.reset((struct kvm_cpuid2 *)operator new(
216 sizeof(kvm_cpuid2) + i * sizeof(kvm_cpuid_entry2)));
217
218 cpuid->nent = i;
219 ++i;
220 } while (!getSupportedCPUID(*cpuid));
221 supportedCPUIDCache.assign(cpuid->entries,
222 cpuid->entries + cpuid->nent);
223 }
224
225 return supportedCPUIDCache;
226}
227
228bool
229Kvm::getSupportedMSRs(struct kvm_msr_list &msrs) const
230{
231 if (ioctl(KVM_GET_MSR_INDEX_LIST, (void *)&msrs) == -1) {
232 if (errno == E2BIG)
233 return false;
234 else
235 panic("KVM: Failed to get supported CPUID (errno: %i)\n", errno);
236 } else
237 return true;
238}
239
240const Kvm::MSRIndexVector &
241Kvm::getSupportedMSRs() const
242{
243 if (supportedMSRCache.empty()) {
244 std::unique_ptr<struct kvm_msr_list> msrs;
245 int i(0);
246 do {
247 msrs.reset((struct kvm_msr_list *)operator new(
248 sizeof(kvm_msr_list) + i * sizeof(uint32_t)));
249
250 msrs->nmsrs = i;
251 ++i;
252 } while (!getSupportedMSRs(*msrs));
253 supportedMSRCache.assign(msrs->indices, msrs->indices + msrs->nmsrs);
254 }
255
256 return supportedMSRCache;
257}
258
259#endif // x86-specific
260
261
262int
263Kvm::checkExtension(int extension) const
264{
265 int ret = ioctl(KVM_CHECK_EXTENSION, extension);
266 if (ret == -1)
267 panic("KVM: ioctl failed when checking for extension\n");
268 return ret;
269}
270
271int
272Kvm::ioctl(int request, long p1) const
273{
274 assert(kvmFD != -1);
275
276 return ::ioctl(kvmFD, request, p1);
277}
278
279int
280Kvm::createVM()
281{
282 int vmFD;
283
284 vmFD = ioctl(KVM_CREATE_VM);
285 if (vmFD == -1)
286 panic("Failed to create KVM VM\n");
287
288 return vmFD;
289}
290
291
292KvmVM::KvmVM(KvmVMParams *params)
293 : SimObject(params),
294 kvm(new Kvm()), system(params->system),
295 vmFD(kvm->createVM()),
296 started(false),
297 nextVCPUID(0)
298{
299 maxMemorySlot = kvm->capNumMemSlots();
300 /* If we couldn't determine how memory slots there are, guess 32. */
301 if (!maxMemorySlot)
302 maxMemorySlot = 32;
303 /* Setup the coalesced MMIO regions */
304 for (int i = 0; i < params->coalescedMMIO.size(); ++i)
305 coalesceMMIO(params->coalescedMMIO[i]);
306}
307
308KvmVM::~KvmVM()
309{
310 if (vmFD != -1)
311 close(vmFD);
312
313 if (kvm)
314 delete kvm;
315}
316
317void
318KvmVM::notifyFork()
319{
320 if (vmFD != -1) {
321 if (close(vmFD) == -1)
322 warn("kvm VM: notifyFork failed to close vmFD\n");
323
324 vmFD = -1;
325
326 delete kvm;
327 kvm = NULL;
328 }
329}
330
331void
332KvmVM::cpuStartup()
333{
334 if (started)
335 return;
336 started = true;
337
338 delayedStartup();
339}
340
341void
342KvmVM::delayedStartup()
343{
344 const std::vector<BackingStoreEntry> &memories(
345 system->getPhysMem().getBackingStore());
346
347 DPRINTF(Kvm, "Mapping %i memory region(s)\n", memories.size());
348 for (int slot(0); slot < memories.size(); ++slot) {
349 if (!memories[slot].kvmMap) {
350 DPRINTF(Kvm, "Skipping region marked as not usable by KVM\n");
351 continue;
352 }
353
354 const AddrRange &range(memories[slot].range);
355 void *pmem(memories[slot].pmem);
356
357 if (pmem) {
358 DPRINTF(Kvm, "Mapping region: 0x%p -> 0x%llx [size: 0x%llx]\n",
359 pmem, range.start(), range.size());
360
361 if (range.interleaved()) {
362 panic("Tried to map an interleaved memory range into "
363 "a KVM VM.\n");
364 }
365
366 const MemSlot slot = allocMemSlot(range.size());
367 setupMemSlot(slot, pmem, range.start(), 0/* flags */);
368 } else {
369 DPRINTF(Kvm, "Zero-region not mapped: [0x%llx]\n", range.start());
370 hack("KVM: Zero memory handled as IO\n");
371 }
372 }
373}
374
375const KvmVM::MemSlot
376KvmVM::allocMemSlot(uint64_t size)
377{
378 if (!size)
379 panic("Memory slots must have non-zero size.\n");
380
381 std::vector<MemorySlot>::iterator pos;
382 for (pos = memorySlots.begin(); pos != memorySlots.end(); pos++) {
383 if (!pos->size) {
384 pos->size = size;
385 pos->active = false;
386 return pos->slot;
387 }
388 }
389
390 uint32_t nextSlot = memorySlots.size();
391 if (nextSlot > maxMemorySlot)
392 panic("Out of memory slots.\n");
393
394 MemorySlot slot;
395 slot.size = size;
396 slot.slot = nextSlot;
397 slot.active = false;
398
399 memorySlots.push_back(slot);
400 return MemSlot(slot.slot);
401}
402
403void
404KvmVM::setupMemSlot(const KvmVM::MemSlot num, void *host_addr, Addr guest,
405 uint32_t flags)
406{
407 MemorySlot &slot = memorySlots.at(num.num);
408 slot.active = true;
409 setUserMemoryRegion(num.num, host_addr, guest, slot.size, flags);
410}
411
412void
413KvmVM::disableMemSlot(const KvmVM::MemSlot num)
414{
415 MemorySlot &slot = memorySlots.at(num.num);
416 if (slot.active)
417 setUserMemoryRegion(num.num, NULL, 0, 0, 0);
418 slot.active = false;
419}
420
421void
422KvmVM::freeMemSlot(const KvmVM::MemSlot num)
423{
424 disableMemSlot(num.num);
425 MemorySlot &slot = memorySlots.at(num.num);
426 slot.size = 0;
427}
428
429void
430KvmVM::setUserMemoryRegion(uint32_t slot,
431 void *host_addr, Addr guest_addr,
432 uint64_t len, uint32_t flags)
433{
434 struct kvm_userspace_memory_region m;
435
436 memset(&m, 0, sizeof(m));
437 m.slot = slot;
438 m.flags = flags;
439 m.guest_phys_addr = (uint64_t)guest_addr;
440 m.memory_size = len;
441 m.userspace_addr = (__u64)host_addr;
442
443 if (ioctl(KVM_SET_USER_MEMORY_REGION, (void *)&m) == -1) {
444 panic("Failed to setup KVM memory region:\n"
445 "\tHost Address: 0x%p\n"
446 "\tGuest Address: 0x%llx\n",
447 "\tSize: %ll\n",
448 "\tFlags: 0x%x\n",
449 m.userspace_addr, m.guest_phys_addr,
450 m.memory_size, m.flags);
451 }
452}
453
454void
455KvmVM::coalesceMMIO(const AddrRange &range)
456{
457 coalesceMMIO(range.start(), range.size());
458}
459
460void
461KvmVM::coalesceMMIO(Addr start, int size)
462{
463 struct kvm_coalesced_mmio_zone zone;
464
465 zone.addr = start;
466 zone.size = size;
467 zone.pad = 0;
468
469 DPRINTF(Kvm, "KVM: Registering coalesced MMIO region [0x%x, 0x%x]\n",
470 zone.addr, zone.addr + zone.size - 1);
471 if (ioctl(KVM_REGISTER_COALESCED_MMIO, (void *)&zone) == -1)
472 panic("KVM: Failed to register coalesced MMIO region (%i)\n",
473 errno);
474}
475
476void
477KvmVM::setTSSAddress(Addr tss_address)
478{
479 if (ioctl(KVM_SET_TSS_ADDR, (unsigned long)tss_address) == -1)
480 panic("KVM: Failed to set VM TSS address\n");
481}
482
483void
484KvmVM::createIRQChip()
485{
486 if (_hasKernelIRQChip)
487 panic("KvmVM::createIRQChip called twice.\n");
488
489 if (ioctl(KVM_CREATE_IRQCHIP) != -1) {
490 _hasKernelIRQChip = true;
491 } else {
492 warn("KVM: Failed to create in-kernel IRQ chip (errno: %i)\n",
493 errno);
494 _hasKernelIRQChip = false;
495 }
496}
497
498void
499KvmVM::setIRQLine(uint32_t irq, bool high)
500{
501 struct kvm_irq_level kvm_level;
502
503 kvm_level.irq = irq;
504 kvm_level.level = high ? 1 : 0;
505
506 if (ioctl(KVM_IRQ_LINE, &kvm_level) == -1)
507 panic("KVM: Failed to set IRQ line level (errno: %i)\n",
508 errno);
509}
510
511int
512KvmVM::createDevice(uint32_t type, uint32_t flags)
513{
514#if defined(KVM_CREATE_DEVICE)
515 struct kvm_create_device dev = { type, 0, flags };
516
517 if (ioctl(KVM_CREATE_DEVICE, &dev) == -1) {
518 panic("KVM: Failed to create device (errno: %i)\n",
519 errno);
520 }
521
522 return dev.fd;
523#else
524 panic("Kernel headers don't support KVM_CREATE_DEVICE\n");
525#endif
526}
527
528int
529KvmVM::createVCPU(long vcpuID)
530{
531 int fd;
532
533 fd = ioctl(KVM_CREATE_VCPU, vcpuID);
534 if (fd == -1)
535 panic("KVM: Failed to create virtual CPU");
536
537 return fd;
538}
539
540long
541KvmVM::allocVCPUID()
542{
543 return nextVCPUID++;
544}
545
546#if defined(__aarch64__)
547void
548KvmVM::kvmArmPreferredTarget(struct kvm_vcpu_init &target) const
549{
550 if (ioctl(KVM_ARM_PREFERRED_TARGET, &target) == -1) {
551 panic("KVM: Failed to get ARM preferred CPU target (errno: %i)\n",
552 errno);
553 }
554}
555#endif
556
557int
558KvmVM::ioctl(int request, long p1) const
559{
560 assert(vmFD != -1);
561
562 return ::ioctl(vmFD, request, p1);
563}
564
565
566KvmVM *
567KvmVMParams::create()
568{
569 static bool created = false;
570 if (created)
571 warn_once("Use of multiple KvmVMs is currently untested!\n");
572
573 created = true;
574
575 return new KvmVM(this);
576}
53#include "debug/Kvm.hh"
54#include "params/KvmVM.hh"
55#include "sim/system.hh"
56
57#define EXPECTED_KVM_API_VERSION 12
58
59#if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
60#error Unsupported KVM version
61#endif
62
63Kvm *Kvm::instance = NULL;
64
65Kvm::Kvm()
66 : kvmFD(-1), apiVersion(-1), vcpuMMapSize(0)
67{
68 kvmFD = ::open("/dev/kvm", O_RDWR);
69 if (kvmFD == -1)
70 fatal("KVM: Failed to open /dev/kvm\n");
71
72 apiVersion = ioctl(KVM_GET_API_VERSION);
73 if (apiVersion != EXPECTED_KVM_API_VERSION)
74 fatal("KVM: Incompatible API version\n");
75
76 vcpuMMapSize = ioctl(KVM_GET_VCPU_MMAP_SIZE);
77 if (vcpuMMapSize == -1)
78 panic("KVM: Failed to get virtual CPU MMAP size\n");
79}
80
81Kvm::~Kvm()
82{
83 close(kvmFD);
84}
85
86Kvm *
87Kvm::create()
88{
89 if (!instance)
90 instance = new Kvm();
91
92 return instance;
93}
94
95bool
96Kvm::capUserMemory() const
97{
98 return checkExtension(KVM_CAP_USER_MEMORY) != 0;
99}
100
101bool
102Kvm::capSetTSSAddress() const
103{
104 return checkExtension(KVM_CAP_SET_TSS_ADDR) != 0;
105}
106
107bool
108Kvm::capExtendedCPUID() const
109{
110 return checkExtension(KVM_CAP_EXT_CPUID) != 0;
111}
112
113bool
114Kvm::capUserNMI() const
115{
116#ifdef KVM_CAP_USER_NMI
117 return checkExtension(KVM_CAP_USER_NMI) != 0;
118#else
119 return false;
120#endif
121}
122
123int
124Kvm::capCoalescedMMIO() const
125{
126 return checkExtension(KVM_CAP_COALESCED_MMIO);
127}
128
129int
130Kvm::capNumMemSlots() const
131{
132#ifdef KVM_CAP_NR_MEMSLOTS
133 return checkExtension(KVM_CAP_NR_MEMSLOTS);
134#else
135 return 0;
136#endif
137}
138
139bool
140Kvm::capOneReg() const
141{
142#ifdef KVM_CAP_ONE_REG
143 return checkExtension(KVM_CAP_ONE_REG) != 0;
144#else
145 return false;
146#endif
147}
148
149bool
150Kvm::capIRQChip() const
151{
152 return checkExtension(KVM_CAP_IRQCHIP) != 0;
153}
154
155bool
156Kvm::capVCPUEvents() const
157{
158#ifdef KVM_CAP_VCPU_EVENTS
159 return checkExtension(KVM_CAP_VCPU_EVENTS) != 0;
160#else
161 return false;
162#endif
163}
164
165bool
166Kvm::capDebugRegs() const
167{
168#ifdef KVM_CAP_DEBUGREGS
169 return checkExtension(KVM_CAP_DEBUGREGS) != 0;
170#else
171 return false;
172#endif
173}
174
175bool
176Kvm::capXCRs() const
177{
178#ifdef KVM_CAP_XCRS
179 return checkExtension(KVM_CAP_XCRS) != 0;
180#else
181 return false;
182#endif
183}
184
185bool
186Kvm::capXSave() const
187{
188#ifdef KVM_CAP_XSAVE
189 return checkExtension(KVM_CAP_XSAVE) != 0;
190#else
191 return false;
192#endif
193}
194
195
196#if defined(__i386__) || defined(__x86_64__)
197bool
198Kvm::getSupportedCPUID(struct kvm_cpuid2 &cpuid) const
199{
200 if (ioctl(KVM_GET_SUPPORTED_CPUID, (void *)&cpuid) == -1) {
201 if (errno == E2BIG)
202 return false;
203 else
204 panic("KVM: Failed to get supported CPUID (errno: %i)\n", errno);
205 } else
206 return true;
207}
208
209const Kvm::CPUIDVector &
210Kvm::getSupportedCPUID() const
211{
212 if (supportedCPUIDCache.empty()) {
213 std::unique_ptr<struct kvm_cpuid2> cpuid;
214 int i(1);
215 do {
216 cpuid.reset((struct kvm_cpuid2 *)operator new(
217 sizeof(kvm_cpuid2) + i * sizeof(kvm_cpuid_entry2)));
218
219 cpuid->nent = i;
220 ++i;
221 } while (!getSupportedCPUID(*cpuid));
222 supportedCPUIDCache.assign(cpuid->entries,
223 cpuid->entries + cpuid->nent);
224 }
225
226 return supportedCPUIDCache;
227}
228
229bool
230Kvm::getSupportedMSRs(struct kvm_msr_list &msrs) const
231{
232 if (ioctl(KVM_GET_MSR_INDEX_LIST, (void *)&msrs) == -1) {
233 if (errno == E2BIG)
234 return false;
235 else
236 panic("KVM: Failed to get supported CPUID (errno: %i)\n", errno);
237 } else
238 return true;
239}
240
241const Kvm::MSRIndexVector &
242Kvm::getSupportedMSRs() const
243{
244 if (supportedMSRCache.empty()) {
245 std::unique_ptr<struct kvm_msr_list> msrs;
246 int i(0);
247 do {
248 msrs.reset((struct kvm_msr_list *)operator new(
249 sizeof(kvm_msr_list) + i * sizeof(uint32_t)));
250
251 msrs->nmsrs = i;
252 ++i;
253 } while (!getSupportedMSRs(*msrs));
254 supportedMSRCache.assign(msrs->indices, msrs->indices + msrs->nmsrs);
255 }
256
257 return supportedMSRCache;
258}
259
260#endif // x86-specific
261
262
263int
264Kvm::checkExtension(int extension) const
265{
266 int ret = ioctl(KVM_CHECK_EXTENSION, extension);
267 if (ret == -1)
268 panic("KVM: ioctl failed when checking for extension\n");
269 return ret;
270}
271
272int
273Kvm::ioctl(int request, long p1) const
274{
275 assert(kvmFD != -1);
276
277 return ::ioctl(kvmFD, request, p1);
278}
279
280int
281Kvm::createVM()
282{
283 int vmFD;
284
285 vmFD = ioctl(KVM_CREATE_VM);
286 if (vmFD == -1)
287 panic("Failed to create KVM VM\n");
288
289 return vmFD;
290}
291
292
293KvmVM::KvmVM(KvmVMParams *params)
294 : SimObject(params),
295 kvm(new Kvm()), system(params->system),
296 vmFD(kvm->createVM()),
297 started(false),
298 nextVCPUID(0)
299{
300 maxMemorySlot = kvm->capNumMemSlots();
301 /* If we couldn't determine how memory slots there are, guess 32. */
302 if (!maxMemorySlot)
303 maxMemorySlot = 32;
304 /* Setup the coalesced MMIO regions */
305 for (int i = 0; i < params->coalescedMMIO.size(); ++i)
306 coalesceMMIO(params->coalescedMMIO[i]);
307}
308
309KvmVM::~KvmVM()
310{
311 if (vmFD != -1)
312 close(vmFD);
313
314 if (kvm)
315 delete kvm;
316}
317
318void
319KvmVM::notifyFork()
320{
321 if (vmFD != -1) {
322 if (close(vmFD) == -1)
323 warn("kvm VM: notifyFork failed to close vmFD\n");
324
325 vmFD = -1;
326
327 delete kvm;
328 kvm = NULL;
329 }
330}
331
332void
333KvmVM::cpuStartup()
334{
335 if (started)
336 return;
337 started = true;
338
339 delayedStartup();
340}
341
342void
343KvmVM::delayedStartup()
344{
345 const std::vector<BackingStoreEntry> &memories(
346 system->getPhysMem().getBackingStore());
347
348 DPRINTF(Kvm, "Mapping %i memory region(s)\n", memories.size());
349 for (int slot(0); slot < memories.size(); ++slot) {
350 if (!memories[slot].kvmMap) {
351 DPRINTF(Kvm, "Skipping region marked as not usable by KVM\n");
352 continue;
353 }
354
355 const AddrRange &range(memories[slot].range);
356 void *pmem(memories[slot].pmem);
357
358 if (pmem) {
359 DPRINTF(Kvm, "Mapping region: 0x%p -> 0x%llx [size: 0x%llx]\n",
360 pmem, range.start(), range.size());
361
362 if (range.interleaved()) {
363 panic("Tried to map an interleaved memory range into "
364 "a KVM VM.\n");
365 }
366
367 const MemSlot slot = allocMemSlot(range.size());
368 setupMemSlot(slot, pmem, range.start(), 0/* flags */);
369 } else {
370 DPRINTF(Kvm, "Zero-region not mapped: [0x%llx]\n", range.start());
371 hack("KVM: Zero memory handled as IO\n");
372 }
373 }
374}
375
376const KvmVM::MemSlot
377KvmVM::allocMemSlot(uint64_t size)
378{
379 if (!size)
380 panic("Memory slots must have non-zero size.\n");
381
382 std::vector<MemorySlot>::iterator pos;
383 for (pos = memorySlots.begin(); pos != memorySlots.end(); pos++) {
384 if (!pos->size) {
385 pos->size = size;
386 pos->active = false;
387 return pos->slot;
388 }
389 }
390
391 uint32_t nextSlot = memorySlots.size();
392 if (nextSlot > maxMemorySlot)
393 panic("Out of memory slots.\n");
394
395 MemorySlot slot;
396 slot.size = size;
397 slot.slot = nextSlot;
398 slot.active = false;
399
400 memorySlots.push_back(slot);
401 return MemSlot(slot.slot);
402}
403
404void
405KvmVM::setupMemSlot(const KvmVM::MemSlot num, void *host_addr, Addr guest,
406 uint32_t flags)
407{
408 MemorySlot &slot = memorySlots.at(num.num);
409 slot.active = true;
410 setUserMemoryRegion(num.num, host_addr, guest, slot.size, flags);
411}
412
413void
414KvmVM::disableMemSlot(const KvmVM::MemSlot num)
415{
416 MemorySlot &slot = memorySlots.at(num.num);
417 if (slot.active)
418 setUserMemoryRegion(num.num, NULL, 0, 0, 0);
419 slot.active = false;
420}
421
422void
423KvmVM::freeMemSlot(const KvmVM::MemSlot num)
424{
425 disableMemSlot(num.num);
426 MemorySlot &slot = memorySlots.at(num.num);
427 slot.size = 0;
428}
429
430void
431KvmVM::setUserMemoryRegion(uint32_t slot,
432 void *host_addr, Addr guest_addr,
433 uint64_t len, uint32_t flags)
434{
435 struct kvm_userspace_memory_region m;
436
437 memset(&m, 0, sizeof(m));
438 m.slot = slot;
439 m.flags = flags;
440 m.guest_phys_addr = (uint64_t)guest_addr;
441 m.memory_size = len;
442 m.userspace_addr = (__u64)host_addr;
443
444 if (ioctl(KVM_SET_USER_MEMORY_REGION, (void *)&m) == -1) {
445 panic("Failed to setup KVM memory region:\n"
446 "\tHost Address: 0x%p\n"
447 "\tGuest Address: 0x%llx\n",
448 "\tSize: %ll\n",
449 "\tFlags: 0x%x\n",
450 m.userspace_addr, m.guest_phys_addr,
451 m.memory_size, m.flags);
452 }
453}
454
455void
456KvmVM::coalesceMMIO(const AddrRange &range)
457{
458 coalesceMMIO(range.start(), range.size());
459}
460
461void
462KvmVM::coalesceMMIO(Addr start, int size)
463{
464 struct kvm_coalesced_mmio_zone zone;
465
466 zone.addr = start;
467 zone.size = size;
468 zone.pad = 0;
469
470 DPRINTF(Kvm, "KVM: Registering coalesced MMIO region [0x%x, 0x%x]\n",
471 zone.addr, zone.addr + zone.size - 1);
472 if (ioctl(KVM_REGISTER_COALESCED_MMIO, (void *)&zone) == -1)
473 panic("KVM: Failed to register coalesced MMIO region (%i)\n",
474 errno);
475}
476
477void
478KvmVM::setTSSAddress(Addr tss_address)
479{
480 if (ioctl(KVM_SET_TSS_ADDR, (unsigned long)tss_address) == -1)
481 panic("KVM: Failed to set VM TSS address\n");
482}
483
484void
485KvmVM::createIRQChip()
486{
487 if (_hasKernelIRQChip)
488 panic("KvmVM::createIRQChip called twice.\n");
489
490 if (ioctl(KVM_CREATE_IRQCHIP) != -1) {
491 _hasKernelIRQChip = true;
492 } else {
493 warn("KVM: Failed to create in-kernel IRQ chip (errno: %i)\n",
494 errno);
495 _hasKernelIRQChip = false;
496 }
497}
498
499void
500KvmVM::setIRQLine(uint32_t irq, bool high)
501{
502 struct kvm_irq_level kvm_level;
503
504 kvm_level.irq = irq;
505 kvm_level.level = high ? 1 : 0;
506
507 if (ioctl(KVM_IRQ_LINE, &kvm_level) == -1)
508 panic("KVM: Failed to set IRQ line level (errno: %i)\n",
509 errno);
510}
511
512int
513KvmVM::createDevice(uint32_t type, uint32_t flags)
514{
515#if defined(KVM_CREATE_DEVICE)
516 struct kvm_create_device dev = { type, 0, flags };
517
518 if (ioctl(KVM_CREATE_DEVICE, &dev) == -1) {
519 panic("KVM: Failed to create device (errno: %i)\n",
520 errno);
521 }
522
523 return dev.fd;
524#else
525 panic("Kernel headers don't support KVM_CREATE_DEVICE\n");
526#endif
527}
528
529int
530KvmVM::createVCPU(long vcpuID)
531{
532 int fd;
533
534 fd = ioctl(KVM_CREATE_VCPU, vcpuID);
535 if (fd == -1)
536 panic("KVM: Failed to create virtual CPU");
537
538 return fd;
539}
540
541long
542KvmVM::allocVCPUID()
543{
544 return nextVCPUID++;
545}
546
547#if defined(__aarch64__)
548void
549KvmVM::kvmArmPreferredTarget(struct kvm_vcpu_init &target) const
550{
551 if (ioctl(KVM_ARM_PREFERRED_TARGET, &target) == -1) {
552 panic("KVM: Failed to get ARM preferred CPU target (errno: %i)\n",
553 errno);
554 }
555}
556#endif
557
558int
559KvmVM::ioctl(int request, long p1) const
560{
561 assert(vmFD != -1);
562
563 return ::ioctl(vmFD, request, p1);
564}
565
566
567KvmVM *
568KvmVMParams::create()
569{
570 static bool created = false;
571 if (created)
572 warn_once("Use of multiple KvmVMs is currently untested!\n");
573
574 created = true;
575
576 return new KvmVM(this);
577}