vm.cc revision 9651
1/*
2 * Copyright (c) 2012 ARM Limited
3 * All rights reserved
4 *
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.
13 *
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.
24 *
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.
36 *
37 * Authors: Andreas Sandberg
38 */
39
40#include <linux/kvm.h>
41#include <sys/ioctl.h>
42#include <sys/stat.h>
43#include <sys/types.h>
44#include <fcntl.h>
45#include <unistd.h>
46
47#include <cerrno>
48
49#include "cpu/kvm/vm.hh"
50#include "debug/Kvm.hh"
51#include "params/KvmVM.hh"
52#include "sim/system.hh"
53
54#define EXPECTED_KVM_API_VERSION 12
55
56#if EXPECTED_KVM_API_VERSION != KVM_API_VERSION
57#error Unsupported KVM version
58#endif
59
60Kvm *Kvm::instance = NULL;
61
62Kvm::Kvm()
63    : kvmFD(-1), apiVersion(-1), vcpuMMapSize(0)
64{
65    kvmFD = ::open("/dev/kvm", O_RDWR);
66    if (kvmFD == -1)
67        fatal("KVM: Failed to open /dev/kvm\n");
68
69    apiVersion = ioctl(KVM_GET_API_VERSION);
70    if (apiVersion != EXPECTED_KVM_API_VERSION)
71        fatal("KVM: Incompatible API version\n");
72
73    vcpuMMapSize = ioctl(KVM_GET_VCPU_MMAP_SIZE);
74    if (vcpuMMapSize == -1)
75        panic("KVM: Failed to get virtual CPU MMAP size\n");
76}
77
78Kvm::~Kvm()
79{
80    close(kvmFD);
81}
82
83Kvm *
84Kvm::create()
85{
86    if (!instance)
87        instance = new Kvm();
88
89    return instance;
90}
91
92bool
93Kvm::capUserMemory() const
94{
95    return checkExtension(KVM_CAP_USER_MEMORY) != 0;
96}
97
98bool
99Kvm::capSetTSSAddress() const
100{
101    return checkExtension(KVM_CAP_SET_TSS_ADDR) != 0;
102}
103
104bool
105Kvm::capExtendedCPUID() const
106{
107    return checkExtension(KVM_CAP_EXT_CPUID) != 0;
108}
109
110bool
111Kvm::capUserNMI() const
112{
113#ifdef KVM_CAP_USER_NMI
114    return checkExtension(KVM_CAP_USER_NMI) != 0;
115#else
116    return false;
117#endif
118}
119
120int
121Kvm::capCoalescedMMIO() const
122{
123    return checkExtension(KVM_CAP_COALESCED_MMIO);
124}
125
126bool
127Kvm::capOneReg() const
128{
129#ifdef KVM_CAP_ONE_REG
130    return checkExtension(KVM_CAP_ONE_REG) != 0;
131#else
132    return false;
133#endif
134}
135
136bool
137Kvm::capIRQChip() const
138{
139    return checkExtension(KVM_CAP_IRQCHIP) != 0;
140}
141
142bool
143Kvm::getSupportedCPUID(struct kvm_cpuid2 &cpuid) const
144{
145#if defined(__i386__) || defined(__x86_64__)
146    if (ioctl(KVM_GET_SUPPORTED_CPUID, (void *)&cpuid) == -1) {
147        if (errno == E2BIG)
148            return false;
149        else
150            panic("KVM: Failed to get supported CPUID (errno: %i)\n", errno);
151    } else
152        return true;
153#else
154    panic("KVM: getSupportedCPUID is unsupported on this platform.\n");
155#endif
156}
157
158int
159Kvm::checkExtension(int extension) const
160{
161    int ret = ioctl(KVM_CHECK_EXTENSION, extension);
162    if (ret == -1)
163        panic("KVM: ioctl failed when checking for extension\n");
164    return ret;
165}
166
167int
168Kvm::ioctl(int request, long p1) const
169{
170    assert(kvmFD != -1);
171
172    return ::ioctl(kvmFD, request, p1);
173}
174
175int
176Kvm::createVM()
177{
178    int vmFD;
179
180    vmFD = ioctl(KVM_CREATE_VM);
181    if (vmFD == -1)
182        panic("Failed to create KVM VM\n");
183
184    return vmFD;
185}
186
187
188KvmVM::KvmVM(KvmVMParams *params)
189    : SimObject(params),
190      kvm(), system(params->system),
191      vmFD(kvm.createVM()),
192      started(false),
193      nextVCPUID(0)
194{
195    /* Setup the coalesced MMIO regions */
196    for (int i = 0; i < params->coalescedMMIO.size(); ++i)
197        coalesceMMIO(params->coalescedMMIO[i]);
198}
199
200KvmVM::~KvmVM()
201{
202    close(vmFD);
203}
204
205void
206KvmVM::cpuStartup()
207{
208    if (started)
209        return;
210    started = true;
211
212    delayedStartup();
213}
214
215void
216KvmVM::delayedStartup()
217{
218    const std::vector<std::pair<AddrRange, uint8_t*> >&memories(
219        system->getPhysMem().getBackingStore());
220
221    DPRINTF(Kvm, "Mapping %i memory region(s)\n", memories.size());
222    for (int slot(0); slot < memories.size(); ++slot) {
223        const AddrRange &range(memories[slot].first);
224        void *pmem(memories[slot].second);
225
226        if (pmem) {
227            DPRINTF(Kvm, "Mapping region: 0x%p -> 0x%llx [size: 0x%llx]\n",
228                    pmem, range.start(), range.size());
229
230            setUserMemoryRegion(slot, pmem, range, 0 /* flags */);
231        } else {
232            DPRINTF(Kvm, "Zero-region not mapped: [0x%llx]\n", range.start());
233            hack("KVM: Zero memory handled as IO\n");
234        }
235    }
236}
237
238void
239KvmVM::setUserMemoryRegion(uint32_t slot,
240                           void *host_addr, AddrRange guest_range,
241                           uint32_t flags)
242{
243    if (guest_range.interleaved())
244        panic("Tried to map an interleaved memory range into a KVM VM.\n");
245
246    setUserMemoryRegion(slot, host_addr,
247                        guest_range.start(), guest_range.size(),
248                        flags);
249}
250
251void
252KvmVM::setUserMemoryRegion(uint32_t slot,
253                           void *host_addr, Addr guest_addr,
254                           uint64_t len, uint32_t flags)
255{
256    struct kvm_userspace_memory_region m;
257
258    memset(&m, 0, sizeof(m));
259    m.slot = slot;
260    m.flags = flags;
261    m.guest_phys_addr = (uint64_t)guest_addr;
262    m.memory_size = len;
263    m.userspace_addr = (__u64)host_addr;
264
265    if (ioctl(KVM_SET_USER_MEMORY_REGION, (void *)&m) == -1) {
266        panic("Failed to setup KVM memory region:\n"
267              "\tHost Address: 0x%p\n"
268              "\tGuest Address: 0x%llx\n",
269              "\tSize: %ll\n",
270              "\tFlags: 0x%x\n",
271              m.userspace_addr, m.guest_phys_addr,
272              m.memory_size, m.flags);
273    }
274}
275
276void
277KvmVM::coalesceMMIO(const AddrRange &range)
278{
279    coalesceMMIO(range.start(), range.size());
280}
281
282void
283KvmVM::coalesceMMIO(Addr start, int size)
284{
285    struct kvm_coalesced_mmio_zone zone;
286
287    zone.addr = start;
288    zone.size = size;
289    zone.pad = 0;
290
291    DPRINTF(Kvm, "KVM: Registering coalesced MMIO region [0x%x, 0x%x]\n",
292            zone.addr, zone.addr + zone.size - 1);
293    if (ioctl(KVM_REGISTER_COALESCED_MMIO, (void *)&zone) == -1)
294        panic("KVM: Failed to register coalesced MMIO region (%i)\n",
295              errno);
296}
297
298void
299KvmVM::setTSSAddress(Addr tss_address)
300{
301    if (ioctl(KVM_SET_TSS_ADDR, (unsigned long)tss_address) == -1)
302        panic("KVM: Failed to set VM TSS address\n");
303}
304
305void
306KvmVM::createIRQChip()
307{
308    if (_hasKernelIRQChip)
309        panic("KvmVM::createIRQChip called twice.\n");
310
311    if (ioctl(KVM_CREATE_IRQCHIP) != -1) {
312        _hasKernelIRQChip = true;
313    } else {
314        warn("KVM: Failed to create in-kernel IRQ chip (errno: %i)\n",
315             errno);
316        _hasKernelIRQChip = false;
317    }
318}
319
320void
321KvmVM::setIRQLine(uint32_t irq, bool high)
322{
323    struct kvm_irq_level kvm_level;
324
325    kvm_level.irq = irq;
326    kvm_level.level = high ? 1 : 0;
327
328    if (ioctl(KVM_IRQ_LINE, &kvm_level) == -1)
329        panic("KVM: Failed to set IRQ line level (errno: %i)\n",
330              errno);
331}
332
333int
334KvmVM::createVCPU(long vcpuID)
335{
336    int fd;
337
338    fd = ioctl(KVM_CREATE_VCPU, vcpuID);
339    if (fd == -1)
340        panic("KVM: Failed to create virtual CPU");
341
342    return fd;
343}
344
345long
346KvmVM::allocVCPUID()
347{
348    return nextVCPUID++;
349}
350
351int
352KvmVM::ioctl(int request, long p1) const
353{
354    assert(vmFD != -1);
355
356    return ::ioctl(vmFD, request, p1);
357}
358
359
360KvmVM *
361KvmVMParams::create()
362{
363    static bool created = false;
364    if (created)
365        warn_once("Use of multiple KvmVMs is currently untested!\n");
366
367    created = true;
368
369    return new KvmVM(this);
370}
371