base.cc (9689:a1ea7e67a9d9) base.cc (9690:8055cd04be78)
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/mman.h>
43#include <unistd.h>
44
45#include <cerrno>
46#include <csignal>
47#include <ostream>
48
49#include "arch/utility.hh"
50#include "cpu/kvm/base.hh"
51#include "debug/Checkpoint.hh"
52#include "debug/Kvm.hh"
53#include "debug/KvmIO.hh"
54#include "debug/KvmRun.hh"
55#include "params/BaseKvmCPU.hh"
56#include "sim/process.hh"
57#include "sim/system.hh"
58
59/* Used by some KVM macros */
60#define PAGE_SIZE pageSize
61
62volatile bool timerOverflowed = false;
63
64static void
65onTimerOverflow(int signo, siginfo_t *si, void *data)
66{
67 timerOverflowed = true;
68}
69
70BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params)
71 : BaseCPU(params),
72 vm(*params->kvmVM),
73 _status(Idle),
74 dataPort(name() + ".dcache_port", this),
75 instPort(name() + ".icache_port", this),
76 threadContextDirty(true),
77 kvmStateDirty(false),
78 vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
79 _kvmRun(NULL), mmioRing(NULL),
80 pageSize(sysconf(_SC_PAGE_SIZE)),
81 tickEvent(*this),
82 perfControlledByTimer(params->usePerfOverflow),
83 hostFactor(params->hostFactor)
84{
85 if (pageSize == -1)
86 panic("KVM: Failed to determine host page size (%i)\n",
87 errno);
88
89 thread = new SimpleThread(this, 0, params->system,
90 params->itb, params->dtb, params->isa[0]);
91 thread->setStatus(ThreadContext::Halted);
92 tc = thread->getTC();
93 threadContexts.push_back(tc);
94
95 setupCounters();
96 setupSignalHandler();
97
98 if (params->usePerfOverflow)
99 runTimer.reset(new PerfKvmTimer(hwCycles,
100 KVM_TIMER_SIGNAL,
101 params->hostFactor,
102 params->clock));
103 else
104 runTimer.reset(new PosixKvmTimer(KVM_TIMER_SIGNAL, CLOCK_MONOTONIC,
105 params->hostFactor,
106 params->clock));
107}
108
109BaseKvmCPU::~BaseKvmCPU()
110{
111 if (_kvmRun)
112 munmap(_kvmRun, vcpuMMapSize);
113 close(vcpuFD);
114}
115
116void
117BaseKvmCPU::init()
118{
119 BaseCPU::init();
120
121 if (numThreads != 1)
122 fatal("KVM: Multithreading not supported");
123
124 tc->initMemProxies(tc);
125
126 // initialize CPU, including PC
127 if (FullSystem && !switchedOut())
128 TheISA::initCPU(tc, tc->contextId());
129
130 mmio_req.setThreadContext(tc->contextId(), 0);
131}
132
133void
134BaseKvmCPU::startup()
135{
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/mman.h>
43#include <unistd.h>
44
45#include <cerrno>
46#include <csignal>
47#include <ostream>
48
49#include "arch/utility.hh"
50#include "cpu/kvm/base.hh"
51#include "debug/Checkpoint.hh"
52#include "debug/Kvm.hh"
53#include "debug/KvmIO.hh"
54#include "debug/KvmRun.hh"
55#include "params/BaseKvmCPU.hh"
56#include "sim/process.hh"
57#include "sim/system.hh"
58
59/* Used by some KVM macros */
60#define PAGE_SIZE pageSize
61
62volatile bool timerOverflowed = false;
63
64static void
65onTimerOverflow(int signo, siginfo_t *si, void *data)
66{
67 timerOverflowed = true;
68}
69
70BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params)
71 : BaseCPU(params),
72 vm(*params->kvmVM),
73 _status(Idle),
74 dataPort(name() + ".dcache_port", this),
75 instPort(name() + ".icache_port", this),
76 threadContextDirty(true),
77 kvmStateDirty(false),
78 vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
79 _kvmRun(NULL), mmioRing(NULL),
80 pageSize(sysconf(_SC_PAGE_SIZE)),
81 tickEvent(*this),
82 perfControlledByTimer(params->usePerfOverflow),
83 hostFactor(params->hostFactor)
84{
85 if (pageSize == -1)
86 panic("KVM: Failed to determine host page size (%i)\n",
87 errno);
88
89 thread = new SimpleThread(this, 0, params->system,
90 params->itb, params->dtb, params->isa[0]);
91 thread->setStatus(ThreadContext::Halted);
92 tc = thread->getTC();
93 threadContexts.push_back(tc);
94
95 setupCounters();
96 setupSignalHandler();
97
98 if (params->usePerfOverflow)
99 runTimer.reset(new PerfKvmTimer(hwCycles,
100 KVM_TIMER_SIGNAL,
101 params->hostFactor,
102 params->clock));
103 else
104 runTimer.reset(new PosixKvmTimer(KVM_TIMER_SIGNAL, CLOCK_MONOTONIC,
105 params->hostFactor,
106 params->clock));
107}
108
109BaseKvmCPU::~BaseKvmCPU()
110{
111 if (_kvmRun)
112 munmap(_kvmRun, vcpuMMapSize);
113 close(vcpuFD);
114}
115
116void
117BaseKvmCPU::init()
118{
119 BaseCPU::init();
120
121 if (numThreads != 1)
122 fatal("KVM: Multithreading not supported");
123
124 tc->initMemProxies(tc);
125
126 // initialize CPU, including PC
127 if (FullSystem && !switchedOut())
128 TheISA::initCPU(tc, tc->contextId());
129
130 mmio_req.setThreadContext(tc->contextId(), 0);
131}
132
133void
134BaseKvmCPU::startup()
135{
136 const BaseKvmCPUParams * const p(
137 dynamic_cast<const BaseKvmCPUParams *>(params()));
138
136 Kvm &kvm(vm.kvm);
137
138 BaseCPU::startup();
139
140 assert(vcpuFD == -1);
141
142 // Tell the VM that a CPU is about to start.
143 vm.cpuStartup();
144
145 // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
146 // not guaranteed that the parent KVM VM has initialized at that
147 // point. Initialize virtual CPUs here instead.
148 vcpuFD = vm.createVCPU(vcpuID);
149
150 // Map the KVM run structure */
151 vcpuMMapSize = kvm.getVCPUMMapSize();
152 _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize,
153 PROT_READ | PROT_WRITE, MAP_SHARED,
154 vcpuFD, 0);
155 if (_kvmRun == MAP_FAILED)
156 panic("KVM: Failed to map run data structure\n");
157
158 // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
159 // available. The offset into the KVM's communication page is
160 // provided by the coalesced MMIO capability.
161 int mmioOffset(kvm.capCoalescedMMIO());
139 Kvm &kvm(vm.kvm);
140
141 BaseCPU::startup();
142
143 assert(vcpuFD == -1);
144
145 // Tell the VM that a CPU is about to start.
146 vm.cpuStartup();
147
148 // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
149 // not guaranteed that the parent KVM VM has initialized at that
150 // point. Initialize virtual CPUs here instead.
151 vcpuFD = vm.createVCPU(vcpuID);
152
153 // Map the KVM run structure */
154 vcpuMMapSize = kvm.getVCPUMMapSize();
155 _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize,
156 PROT_READ | PROT_WRITE, MAP_SHARED,
157 vcpuFD, 0);
158 if (_kvmRun == MAP_FAILED)
159 panic("KVM: Failed to map run data structure\n");
160
161 // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
162 // available. The offset into the KVM's communication page is
163 // provided by the coalesced MMIO capability.
164 int mmioOffset(kvm.capCoalescedMMIO());
162 if (mmioOffset) {
165 if (!p->useCoalescedMMIO) {
166 inform("KVM: Coalesced MMIO disabled by config.\n");
167 } else if (mmioOffset) {
163 inform("KVM: Coalesced IO available\n");
164 mmioRing = (struct kvm_coalesced_mmio_ring *)(
165 (char *)_kvmRun + (mmioOffset * pageSize));
166 } else {
167 inform("KVM: Coalesced not supported by host OS\n");
168 }
169}
170
171void
172BaseKvmCPU::regStats()
173{
174 using namespace Stats;
175
176 BaseCPU::regStats();
177
178 numInsts
179 .name(name() + ".committedInsts")
180 .desc("Number of instructions committed")
181 ;
182
183 numVMExits
184 .name(name() + ".numVMExits")
185 .desc("total number of KVM exits")
186 ;
187
188 numMMIO
189 .name(name() + ".numMMIO")
190 .desc("number of VM exits due to memory mapped IO")
191 ;
192
193 numCoalescedMMIO
194 .name(name() + ".numCoalescedMMIO")
195 .desc("number of coalesced memory mapped IO requests")
196 ;
197
198 numIO
199 .name(name() + ".numIO")
200 .desc("number of VM exits due to legacy IO")
201 ;
202
203 numHalt
204 .name(name() + ".numHalt")
205 .desc("number of VM exits due to wait for interrupt instructions")
206 ;
207
208 numInterrupts
209 .name(name() + ".numInterrupts")
210 .desc("number of interrupts delivered")
211 ;
212
213 numHypercalls
214 .name(name() + ".numHypercalls")
215 .desc("number of hypercalls")
216 ;
217}
218
219void
220BaseKvmCPU::serializeThread(std::ostream &os, ThreadID tid)
221{
222 if (DTRACE(Checkpoint)) {
223 DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid);
224 dump();
225 }
226
227 // Update the thread context so we have something to serialize.
228 syncThreadContext();
229
230 assert(tid == 0);
231 assert(_status == Idle);
232 thread->serialize(os);
233}
234
235void
236BaseKvmCPU::unserializeThread(Checkpoint *cp, const std::string &section,
237 ThreadID tid)
238{
239 DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid);
240
241 assert(tid == 0);
242 assert(_status == Idle);
243 thread->unserialize(cp, section);
244 threadContextDirty = true;
245}
246
247unsigned int
248BaseKvmCPU::drain(DrainManager *dm)
249{
250 if (switchedOut())
251 return 0;
252
253 DPRINTF(Kvm, "drain\n");
254
255 // De-schedule the tick event so we don't insert any more MMIOs
256 // into the system while it is draining.
257 if (tickEvent.scheduled())
258 deschedule(tickEvent);
259
260 _status = Idle;
261 return 0;
262}
263
264void
265BaseKvmCPU::drainResume()
266{
267 assert(!tickEvent.scheduled());
268
269 // We might have been switched out. In that case, we don't need to
270 // do anything.
271 if (switchedOut())
272 return;
273
274 DPRINTF(Kvm, "drainResume\n");
275 verifyMemoryMode();
276
277 // The tick event is de-scheduled as a part of the draining
278 // process. Re-schedule it if the thread context is active.
279 if (tc->status() == ThreadContext::Active) {
280 schedule(tickEvent, nextCycle());
281 _status = Running;
282 } else {
283 _status = Idle;
284 }
285}
286
287void
288BaseKvmCPU::switchOut()
289{
290 DPRINTF(Kvm, "switchOut\n");
291
292 // Make sure to update the thread context in case, the new CPU
293 // will need to access it.
294 syncThreadContext();
295
296 BaseCPU::switchOut();
297
298 // We should have drained prior to executing a switchOut, which
299 // means that the tick event shouldn't be scheduled and the CPU is
300 // idle.
301 assert(!tickEvent.scheduled());
302 assert(_status == Idle);
303}
304
305void
306BaseKvmCPU::takeOverFrom(BaseCPU *cpu)
307{
308 DPRINTF(Kvm, "takeOverFrom\n");
309
310 BaseCPU::takeOverFrom(cpu);
311
312 // We should have drained prior to executing a switchOut, which
313 // means that the tick event shouldn't be scheduled and the CPU is
314 // idle.
315 assert(!tickEvent.scheduled());
316 assert(_status == Idle);
317 assert(threadContexts.size() == 1);
318
319 // The BaseCPU updated the thread context, make sure that we
320 // synchronize next time we enter start the CPU.
321 threadContextDirty = true;
322}
323
324void
325BaseKvmCPU::verifyMemoryMode() const
326{
327 if (!(system->isAtomicMode() && system->bypassCaches())) {
328 fatal("The KVM-based CPUs requires the memory system to be in the "
329 "'atomic_noncaching' mode.\n");
330 }
331}
332
333void
334BaseKvmCPU::wakeup()
335{
336 DPRINTF(Kvm, "wakeup()\n");
337
338 if (thread->status() != ThreadContext::Suspended)
339 return;
340
341 thread->activate();
342}
343
344void
345BaseKvmCPU::activateContext(ThreadID thread_num, Cycles delay)
346{
347 DPRINTF(Kvm, "ActivateContext %d (%d cycles)\n", thread_num, delay);
348
349 assert(thread_num == 0);
350 assert(thread);
351
352 assert(_status == Idle);
353 assert(!tickEvent.scheduled());
354
355 numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend)
356 * hostFactor;
357
358 schedule(tickEvent, clockEdge(delay));
359 _status = Running;
360}
361
362
363void
364BaseKvmCPU::suspendContext(ThreadID thread_num)
365{
366 DPRINTF(Kvm, "SuspendContext %d\n", thread_num);
367
368 assert(thread_num == 0);
369 assert(thread);
370
371 if (_status == Idle)
372 return;
373
374 assert(_status == Running);
375
376 // The tick event may no be scheduled if the quest has requested
377 // the monitor to wait for interrupts. The normal CPU models can
378 // get their tick events descheduled by quiesce instructions, but
379 // that can't happen here.
380 if (tickEvent.scheduled())
381 deschedule(tickEvent);
382
383 _status = Idle;
384}
385
386void
387BaseKvmCPU::deallocateContext(ThreadID thread_num)
388{
389 // for now, these are equivalent
390 suspendContext(thread_num);
391}
392
393void
394BaseKvmCPU::haltContext(ThreadID thread_num)
395{
396 // for now, these are equivalent
397 suspendContext(thread_num);
398}
399
400ThreadContext *
401BaseKvmCPU::getContext(int tn)
402{
403 assert(tn == 0);
404 syncThreadContext();
405 return tc;
406}
407
408
409Counter
410BaseKvmCPU::totalInsts() const
411{
412 return hwInstructions.read();
413}
414
415Counter
416BaseKvmCPU::totalOps() const
417{
418 hack_once("Pretending totalOps is equivalent to totalInsts()\n");
419 return hwInstructions.read();
420}
421
422void
423BaseKvmCPU::dump()
424{
425 inform("State dumping not implemented.");
426}
427
428void
429BaseKvmCPU::tick()
430{
431 assert(_status == Running);
432
433 DPRINTF(KvmRun, "Entering KVM...\n");
434
435 Tick ticksToExecute(mainEventQueue.nextTick() - curTick());
436 Tick ticksExecuted(kvmRun(ticksToExecute));
437
438 Tick delay(ticksExecuted + handleKvmExit());
439
440 switch (_status) {
441 case Running:
442 schedule(tickEvent, clockEdge(ticksToCycles(delay)));
443 break;
444
445 default:
446 /* The CPU is halted or waiting for an interrupt from a
447 * device. Don't start it. */
448 break;
449 }
450}
451
452Tick
453BaseKvmCPU::kvmRun(Tick ticks)
454{
455 uint64_t baseCycles(hwCycles.read());
456 uint64_t baseInstrs(hwInstructions.read());
457
458 // We might need to update the KVM state.
459 syncKvmState();
460 // Entering into KVM implies that we'll have to reload the thread
461 // context from KVM if we want to access it. Flag the KVM state as
462 // dirty with respect to the cached thread context.
463 kvmStateDirty = true;
464
465 if (ticks < runTimer->resolution()) {
466 DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
467 ticks, runTimer->resolution());
468 ticks = runTimer->resolution();
469 }
470
471 DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks);
472 timerOverflowed = false;
473
474 // Arm the run timer and start the cycle timer if it isn't
475 // controlled by the overflow timer. Starting/stopping the cycle
476 // timer automatically starts the other perf timers as they are in
477 // the same counter group.
478 runTimer->arm(ticks);
479 if (!perfControlledByTimer)
480 hwCycles.start();
481
482 if (ioctl(KVM_RUN) == -1) {
483 if (errno != EINTR)
484 panic("KVM: Failed to start virtual CPU (errno: %i)\n",
485 errno);
486 }
487
488 runTimer->disarm();
489 if (!perfControlledByTimer)
490 hwCycles.stop();
491
492
493 const uint64_t hostCyclesExecuted(hwCycles.read() - baseCycles);
494 const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor);
495 const uint64_t instsExecuted(hwInstructions.read() - baseInstrs);
496 const Tick ticksExecuted(runTimer->ticksFromHostCycles(hostCyclesExecuted));
497
498 if (ticksExecuted < ticks &&
499 timerOverflowed &&
500 _kvmRun->exit_reason == KVM_EXIT_INTR) {
501 // TODO: We should probably do something clever here...
502 warn("KVM: Early timer event, requested %i ticks but got %i ticks.\n",
503 ticks, ticksExecuted);
504 }
505
506 /* Update statistics */
507 numCycles += simCyclesExecuted;;
508 ++numVMExits;
509 numInsts += instsExecuted;
510
511 DPRINTF(KvmRun, "KVM: Executed %i instructions in %i cycles (%i ticks, sim cycles: %i).\n",
512 instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted);
513
514 return ticksExecuted + flushCoalescedMMIO();
515}
516
517void
518BaseKvmCPU::kvmNonMaskableInterrupt()
519{
520 ++numInterrupts;
521 if (ioctl(KVM_NMI) == -1)
522 panic("KVM: Failed to deliver NMI to virtual CPU\n");
523}
524
525void
526BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt)
527{
528 ++numInterrupts;
529 if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1)
530 panic("KVM: Failed to deliver interrupt to virtual CPU\n");
531}
532
533void
534BaseKvmCPU::getRegisters(struct kvm_regs &regs) const
535{
536 if (ioctl(KVM_GET_REGS, &regs) == -1)
537 panic("KVM: Failed to get guest registers\n");
538}
539
540void
541BaseKvmCPU::setRegisters(const struct kvm_regs &regs)
542{
543 if (ioctl(KVM_SET_REGS, (void *)&regs) == -1)
544 panic("KVM: Failed to set guest registers\n");
545}
546
547void
548BaseKvmCPU::getSpecialRegisters(struct kvm_sregs &regs) const
549{
550 if (ioctl(KVM_GET_SREGS, &regs) == -1)
551 panic("KVM: Failed to get guest special registers\n");
552}
553
554void
555BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs &regs)
556{
557 if (ioctl(KVM_SET_SREGS, (void *)&regs) == -1)
558 panic("KVM: Failed to set guest special registers\n");
559}
560
561void
562BaseKvmCPU::getFPUState(struct kvm_fpu &state) const
563{
564 if (ioctl(KVM_GET_FPU, &state) == -1)
565 panic("KVM: Failed to get guest FPU state\n");
566}
567
568void
569BaseKvmCPU::setFPUState(const struct kvm_fpu &state)
570{
571 if (ioctl(KVM_SET_FPU, (void *)&state) == -1)
572 panic("KVM: Failed to set guest FPU state\n");
573}
574
575
576void
577BaseKvmCPU::setOneReg(uint64_t id, const void *addr)
578{
579#ifdef KVM_SET_ONE_REG
580 struct kvm_one_reg reg;
581 reg.id = id;
582 reg.addr = (uint64_t)addr;
583
584 if (ioctl(KVM_SET_ONE_REG, &reg) == -1) {
585 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
586 id, errno);
587 }
588#else
589 panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
590#endif
591}
592
593void
594BaseKvmCPU::getOneReg(uint64_t id, void *addr) const
595{
596#ifdef KVM_GET_ONE_REG
597 struct kvm_one_reg reg;
598 reg.id = id;
599 reg.addr = (uint64_t)addr;
600
601 if (ioctl(KVM_GET_ONE_REG, &reg) == -1) {
602 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
603 id, errno);
604 }
605#else
606 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
607#endif
608}
609
610std::string
611BaseKvmCPU::getAndFormatOneReg(uint64_t id) const
612{
613#ifdef KVM_GET_ONE_REG
614 std::ostringstream ss;
615
616 ss.setf(std::ios::hex, std::ios::basefield);
617 ss.setf(std::ios::showbase);
618#define HANDLE_INTTYPE(len) \
619 case KVM_REG_SIZE_U ## len: { \
620 uint ## len ## _t value; \
621 getOneReg(id, &value); \
622 ss << value; \
623 } break
624
625#define HANDLE_ARRAY(len) \
626 case KVM_REG_SIZE_U ## len: { \
627 uint8_t value[len / 8]; \
628 getOneReg(id, value); \
629 ss << "[" << value[0]; \
630 for (int i = 1; i < len / 8; ++i) \
631 ss << ", " << value[i]; \
632 ss << "]"; \
633 } break
634
635 switch (id & KVM_REG_SIZE_MASK) {
636 HANDLE_INTTYPE(8);
637 HANDLE_INTTYPE(16);
638 HANDLE_INTTYPE(32);
639 HANDLE_INTTYPE(64);
640 HANDLE_ARRAY(128);
641 HANDLE_ARRAY(256);
642 HANDLE_ARRAY(512);
643 HANDLE_ARRAY(1024);
644 default:
645 ss << "??";
646 }
647
648#undef HANDLE_INTTYPE
649#undef HANDLE_ARRAY
650
651 return ss.str();
652#else
653 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
654#endif
655}
656
657void
658BaseKvmCPU::syncThreadContext()
659{
660 if (!kvmStateDirty)
661 return;
662
663 assert(!threadContextDirty);
664
665 updateThreadContext();
666 kvmStateDirty = false;
667}
668
669void
670BaseKvmCPU::syncKvmState()
671{
672 if (!threadContextDirty)
673 return;
674
675 assert(!kvmStateDirty);
676
677 updateKvmState();
678 threadContextDirty = false;
679}
680
681Tick
682BaseKvmCPU::handleKvmExit()
683{
684 DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason);
685
686 switch (_kvmRun->exit_reason) {
687 case KVM_EXIT_UNKNOWN:
688 return handleKvmExitUnknown();
689
690 case KVM_EXIT_EXCEPTION:
691 return handleKvmExitException();
692
693 case KVM_EXIT_IO:
694 ++numIO;
695 return handleKvmExitIO();
696
697 case KVM_EXIT_HYPERCALL:
698 ++numHypercalls;
699 return handleKvmExitHypercall();
700
701 case KVM_EXIT_HLT:
702 /* The guest has halted and is waiting for interrupts */
703 DPRINTF(Kvm, "handleKvmExitHalt\n");
704 ++numHalt;
705
706 // Suspend the thread until the next interrupt arrives
707 thread->suspend();
708
709 // This is actually ignored since the thread is suspended.
710 return 0;
711
712 case KVM_EXIT_MMIO:
713 /* Service memory mapped IO requests */
714 DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
715 _kvmRun->mmio.is_write,
716 _kvmRun->mmio.phys_addr, _kvmRun->mmio.len);
717
718 ++numMMIO;
719 return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data,
720 _kvmRun->mmio.len, _kvmRun->mmio.is_write);
721
722 case KVM_EXIT_IRQ_WINDOW_OPEN:
723 return handleKvmExitIRQWindowOpen();
724
725 case KVM_EXIT_FAIL_ENTRY:
726 return handleKvmExitFailEntry();
727
728 case KVM_EXIT_INTR:
729 /* KVM was interrupted by a signal, restart it in the next
730 * tick. */
731 return 0;
732
733 case KVM_EXIT_INTERNAL_ERROR:
734 panic("KVM: Internal error (suberror: %u)\n",
735 _kvmRun->internal.suberror);
736
737 default:
738 dump();
739 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason);
740 }
741}
742
743Tick
744BaseKvmCPU::handleKvmExitIO()
745{
746 panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
747 _kvmRun->io.direction, _kvmRun->io.size,
748 _kvmRun->io.port, _kvmRun->io.count);
749}
750
751Tick
752BaseKvmCPU::handleKvmExitHypercall()
753{
754 panic("KVM: Unhandled hypercall\n");
755}
756
757Tick
758BaseKvmCPU::handleKvmExitIRQWindowOpen()
759{
760 warn("KVM: Unhandled IRQ window.\n");
761 return 0;
762}
763
764
765Tick
766BaseKvmCPU::handleKvmExitUnknown()
767{
768 dump();
769 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
770 _kvmRun->hw.hardware_exit_reason);
771}
772
773Tick
774BaseKvmCPU::handleKvmExitException()
775{
776 dump();
777 panic("KVM: Got exception when starting vCPU "
778 "(exception: %u, error_code: %u)\n",
779 _kvmRun->ex.exception, _kvmRun->ex.error_code);
780}
781
782Tick
783BaseKvmCPU::handleKvmExitFailEntry()
784{
785 dump();
786 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
787 _kvmRun->fail_entry.hardware_entry_failure_reason);
788}
789
790Tick
791BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write)
792{
793 mmio_req.setPhys(paddr, size, Request::UNCACHEABLE, dataMasterId());
794
795 const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq);
796 Packet pkt(&mmio_req, cmd);
797 pkt.dataStatic(data);
798 return dataPort.sendAtomic(&pkt);
799}
800
801int
802BaseKvmCPU::ioctl(int request, long p1) const
803{
804 if (vcpuFD == -1)
805 panic("KVM: CPU ioctl called before initialization\n");
806
807 return ::ioctl(vcpuFD, request, p1);
808}
809
810Tick
811BaseKvmCPU::flushCoalescedMMIO()
812{
813 if (!mmioRing)
814 return 0;
815
816 DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n");
817
818 // TODO: We might need to do synchronization when we start to
819 // support multiple CPUs
820 Tick ticks(0);
821 while (mmioRing->first != mmioRing->last) {
822 struct kvm_coalesced_mmio &ent(
823 mmioRing->coalesced_mmio[mmioRing->first]);
824
825 DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
826 ent.phys_addr, ent.len);
827
828 ++numCoalescedMMIO;
829 ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true);
830
831 mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX;
832 }
833
834 return ticks;
835}
836
837void
838BaseKvmCPU::setupSignalHandler()
839{
840 struct sigaction sa;
841
842 memset(&sa, 0, sizeof(sa));
843 sa.sa_sigaction = onTimerOverflow;
844 sa.sa_flags = SA_SIGINFO | SA_RESTART;
845 if (sigaction(KVM_TIMER_SIGNAL, &sa, NULL) == -1)
846 panic("KVM: Failed to setup vCPU signal handler\n");
847}
848
849void
850BaseKvmCPU::setupCounters()
851{
852 DPRINTF(Kvm, "Attaching cycle counter...\n");
853 PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE,
854 PERF_COUNT_HW_CPU_CYCLES);
855 cfgCycles.disabled(true)
856 .pinned(true);
857
858 if (perfControlledByTimer) {
859 // We need to configure the cycles counter to send overflows
860 // since we are going to use it to trigger timer signals that
861 // trap back into m5 from KVM. In practice, this means that we
862 // need to set some non-zero sample period that gets
863 // overridden when the timer is armed.
864 cfgCycles.wakeupEvents(1)
865 .samplePeriod(42);
866 }
867
868 hwCycles.attach(cfgCycles,
869 0); // TID (0 => currentThread)
870
871 DPRINTF(Kvm, "Attaching instruction counter...\n");
872 PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE,
873 PERF_COUNT_HW_INSTRUCTIONS);
874 hwInstructions.attach(cfgInstructions,
875 0, // TID (0 => currentThread)
876 hwCycles);
877}
168 inform("KVM: Coalesced IO available\n");
169 mmioRing = (struct kvm_coalesced_mmio_ring *)(
170 (char *)_kvmRun + (mmioOffset * pageSize));
171 } else {
172 inform("KVM: Coalesced not supported by host OS\n");
173 }
174}
175
176void
177BaseKvmCPU::regStats()
178{
179 using namespace Stats;
180
181 BaseCPU::regStats();
182
183 numInsts
184 .name(name() + ".committedInsts")
185 .desc("Number of instructions committed")
186 ;
187
188 numVMExits
189 .name(name() + ".numVMExits")
190 .desc("total number of KVM exits")
191 ;
192
193 numMMIO
194 .name(name() + ".numMMIO")
195 .desc("number of VM exits due to memory mapped IO")
196 ;
197
198 numCoalescedMMIO
199 .name(name() + ".numCoalescedMMIO")
200 .desc("number of coalesced memory mapped IO requests")
201 ;
202
203 numIO
204 .name(name() + ".numIO")
205 .desc("number of VM exits due to legacy IO")
206 ;
207
208 numHalt
209 .name(name() + ".numHalt")
210 .desc("number of VM exits due to wait for interrupt instructions")
211 ;
212
213 numInterrupts
214 .name(name() + ".numInterrupts")
215 .desc("number of interrupts delivered")
216 ;
217
218 numHypercalls
219 .name(name() + ".numHypercalls")
220 .desc("number of hypercalls")
221 ;
222}
223
224void
225BaseKvmCPU::serializeThread(std::ostream &os, ThreadID tid)
226{
227 if (DTRACE(Checkpoint)) {
228 DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid);
229 dump();
230 }
231
232 // Update the thread context so we have something to serialize.
233 syncThreadContext();
234
235 assert(tid == 0);
236 assert(_status == Idle);
237 thread->serialize(os);
238}
239
240void
241BaseKvmCPU::unserializeThread(Checkpoint *cp, const std::string &section,
242 ThreadID tid)
243{
244 DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid);
245
246 assert(tid == 0);
247 assert(_status == Idle);
248 thread->unserialize(cp, section);
249 threadContextDirty = true;
250}
251
252unsigned int
253BaseKvmCPU::drain(DrainManager *dm)
254{
255 if (switchedOut())
256 return 0;
257
258 DPRINTF(Kvm, "drain\n");
259
260 // De-schedule the tick event so we don't insert any more MMIOs
261 // into the system while it is draining.
262 if (tickEvent.scheduled())
263 deschedule(tickEvent);
264
265 _status = Idle;
266 return 0;
267}
268
269void
270BaseKvmCPU::drainResume()
271{
272 assert(!tickEvent.scheduled());
273
274 // We might have been switched out. In that case, we don't need to
275 // do anything.
276 if (switchedOut())
277 return;
278
279 DPRINTF(Kvm, "drainResume\n");
280 verifyMemoryMode();
281
282 // The tick event is de-scheduled as a part of the draining
283 // process. Re-schedule it if the thread context is active.
284 if (tc->status() == ThreadContext::Active) {
285 schedule(tickEvent, nextCycle());
286 _status = Running;
287 } else {
288 _status = Idle;
289 }
290}
291
292void
293BaseKvmCPU::switchOut()
294{
295 DPRINTF(Kvm, "switchOut\n");
296
297 // Make sure to update the thread context in case, the new CPU
298 // will need to access it.
299 syncThreadContext();
300
301 BaseCPU::switchOut();
302
303 // We should have drained prior to executing a switchOut, which
304 // means that the tick event shouldn't be scheduled and the CPU is
305 // idle.
306 assert(!tickEvent.scheduled());
307 assert(_status == Idle);
308}
309
310void
311BaseKvmCPU::takeOverFrom(BaseCPU *cpu)
312{
313 DPRINTF(Kvm, "takeOverFrom\n");
314
315 BaseCPU::takeOverFrom(cpu);
316
317 // We should have drained prior to executing a switchOut, which
318 // means that the tick event shouldn't be scheduled and the CPU is
319 // idle.
320 assert(!tickEvent.scheduled());
321 assert(_status == Idle);
322 assert(threadContexts.size() == 1);
323
324 // The BaseCPU updated the thread context, make sure that we
325 // synchronize next time we enter start the CPU.
326 threadContextDirty = true;
327}
328
329void
330BaseKvmCPU::verifyMemoryMode() const
331{
332 if (!(system->isAtomicMode() && system->bypassCaches())) {
333 fatal("The KVM-based CPUs requires the memory system to be in the "
334 "'atomic_noncaching' mode.\n");
335 }
336}
337
338void
339BaseKvmCPU::wakeup()
340{
341 DPRINTF(Kvm, "wakeup()\n");
342
343 if (thread->status() != ThreadContext::Suspended)
344 return;
345
346 thread->activate();
347}
348
349void
350BaseKvmCPU::activateContext(ThreadID thread_num, Cycles delay)
351{
352 DPRINTF(Kvm, "ActivateContext %d (%d cycles)\n", thread_num, delay);
353
354 assert(thread_num == 0);
355 assert(thread);
356
357 assert(_status == Idle);
358 assert(!tickEvent.scheduled());
359
360 numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend)
361 * hostFactor;
362
363 schedule(tickEvent, clockEdge(delay));
364 _status = Running;
365}
366
367
368void
369BaseKvmCPU::suspendContext(ThreadID thread_num)
370{
371 DPRINTF(Kvm, "SuspendContext %d\n", thread_num);
372
373 assert(thread_num == 0);
374 assert(thread);
375
376 if (_status == Idle)
377 return;
378
379 assert(_status == Running);
380
381 // The tick event may no be scheduled if the quest has requested
382 // the monitor to wait for interrupts. The normal CPU models can
383 // get their tick events descheduled by quiesce instructions, but
384 // that can't happen here.
385 if (tickEvent.scheduled())
386 deschedule(tickEvent);
387
388 _status = Idle;
389}
390
391void
392BaseKvmCPU::deallocateContext(ThreadID thread_num)
393{
394 // for now, these are equivalent
395 suspendContext(thread_num);
396}
397
398void
399BaseKvmCPU::haltContext(ThreadID thread_num)
400{
401 // for now, these are equivalent
402 suspendContext(thread_num);
403}
404
405ThreadContext *
406BaseKvmCPU::getContext(int tn)
407{
408 assert(tn == 0);
409 syncThreadContext();
410 return tc;
411}
412
413
414Counter
415BaseKvmCPU::totalInsts() const
416{
417 return hwInstructions.read();
418}
419
420Counter
421BaseKvmCPU::totalOps() const
422{
423 hack_once("Pretending totalOps is equivalent to totalInsts()\n");
424 return hwInstructions.read();
425}
426
427void
428BaseKvmCPU::dump()
429{
430 inform("State dumping not implemented.");
431}
432
433void
434BaseKvmCPU::tick()
435{
436 assert(_status == Running);
437
438 DPRINTF(KvmRun, "Entering KVM...\n");
439
440 Tick ticksToExecute(mainEventQueue.nextTick() - curTick());
441 Tick ticksExecuted(kvmRun(ticksToExecute));
442
443 Tick delay(ticksExecuted + handleKvmExit());
444
445 switch (_status) {
446 case Running:
447 schedule(tickEvent, clockEdge(ticksToCycles(delay)));
448 break;
449
450 default:
451 /* The CPU is halted or waiting for an interrupt from a
452 * device. Don't start it. */
453 break;
454 }
455}
456
457Tick
458BaseKvmCPU::kvmRun(Tick ticks)
459{
460 uint64_t baseCycles(hwCycles.read());
461 uint64_t baseInstrs(hwInstructions.read());
462
463 // We might need to update the KVM state.
464 syncKvmState();
465 // Entering into KVM implies that we'll have to reload the thread
466 // context from KVM if we want to access it. Flag the KVM state as
467 // dirty with respect to the cached thread context.
468 kvmStateDirty = true;
469
470 if (ticks < runTimer->resolution()) {
471 DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
472 ticks, runTimer->resolution());
473 ticks = runTimer->resolution();
474 }
475
476 DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks);
477 timerOverflowed = false;
478
479 // Arm the run timer and start the cycle timer if it isn't
480 // controlled by the overflow timer. Starting/stopping the cycle
481 // timer automatically starts the other perf timers as they are in
482 // the same counter group.
483 runTimer->arm(ticks);
484 if (!perfControlledByTimer)
485 hwCycles.start();
486
487 if (ioctl(KVM_RUN) == -1) {
488 if (errno != EINTR)
489 panic("KVM: Failed to start virtual CPU (errno: %i)\n",
490 errno);
491 }
492
493 runTimer->disarm();
494 if (!perfControlledByTimer)
495 hwCycles.stop();
496
497
498 const uint64_t hostCyclesExecuted(hwCycles.read() - baseCycles);
499 const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor);
500 const uint64_t instsExecuted(hwInstructions.read() - baseInstrs);
501 const Tick ticksExecuted(runTimer->ticksFromHostCycles(hostCyclesExecuted));
502
503 if (ticksExecuted < ticks &&
504 timerOverflowed &&
505 _kvmRun->exit_reason == KVM_EXIT_INTR) {
506 // TODO: We should probably do something clever here...
507 warn("KVM: Early timer event, requested %i ticks but got %i ticks.\n",
508 ticks, ticksExecuted);
509 }
510
511 /* Update statistics */
512 numCycles += simCyclesExecuted;;
513 ++numVMExits;
514 numInsts += instsExecuted;
515
516 DPRINTF(KvmRun, "KVM: Executed %i instructions in %i cycles (%i ticks, sim cycles: %i).\n",
517 instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted);
518
519 return ticksExecuted + flushCoalescedMMIO();
520}
521
522void
523BaseKvmCPU::kvmNonMaskableInterrupt()
524{
525 ++numInterrupts;
526 if (ioctl(KVM_NMI) == -1)
527 panic("KVM: Failed to deliver NMI to virtual CPU\n");
528}
529
530void
531BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt)
532{
533 ++numInterrupts;
534 if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1)
535 panic("KVM: Failed to deliver interrupt to virtual CPU\n");
536}
537
538void
539BaseKvmCPU::getRegisters(struct kvm_regs &regs) const
540{
541 if (ioctl(KVM_GET_REGS, &regs) == -1)
542 panic("KVM: Failed to get guest registers\n");
543}
544
545void
546BaseKvmCPU::setRegisters(const struct kvm_regs &regs)
547{
548 if (ioctl(KVM_SET_REGS, (void *)&regs) == -1)
549 panic("KVM: Failed to set guest registers\n");
550}
551
552void
553BaseKvmCPU::getSpecialRegisters(struct kvm_sregs &regs) const
554{
555 if (ioctl(KVM_GET_SREGS, &regs) == -1)
556 panic("KVM: Failed to get guest special registers\n");
557}
558
559void
560BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs &regs)
561{
562 if (ioctl(KVM_SET_SREGS, (void *)&regs) == -1)
563 panic("KVM: Failed to set guest special registers\n");
564}
565
566void
567BaseKvmCPU::getFPUState(struct kvm_fpu &state) const
568{
569 if (ioctl(KVM_GET_FPU, &state) == -1)
570 panic("KVM: Failed to get guest FPU state\n");
571}
572
573void
574BaseKvmCPU::setFPUState(const struct kvm_fpu &state)
575{
576 if (ioctl(KVM_SET_FPU, (void *)&state) == -1)
577 panic("KVM: Failed to set guest FPU state\n");
578}
579
580
581void
582BaseKvmCPU::setOneReg(uint64_t id, const void *addr)
583{
584#ifdef KVM_SET_ONE_REG
585 struct kvm_one_reg reg;
586 reg.id = id;
587 reg.addr = (uint64_t)addr;
588
589 if (ioctl(KVM_SET_ONE_REG, &reg) == -1) {
590 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
591 id, errno);
592 }
593#else
594 panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
595#endif
596}
597
598void
599BaseKvmCPU::getOneReg(uint64_t id, void *addr) const
600{
601#ifdef KVM_GET_ONE_REG
602 struct kvm_one_reg reg;
603 reg.id = id;
604 reg.addr = (uint64_t)addr;
605
606 if (ioctl(KVM_GET_ONE_REG, &reg) == -1) {
607 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
608 id, errno);
609 }
610#else
611 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
612#endif
613}
614
615std::string
616BaseKvmCPU::getAndFormatOneReg(uint64_t id) const
617{
618#ifdef KVM_GET_ONE_REG
619 std::ostringstream ss;
620
621 ss.setf(std::ios::hex, std::ios::basefield);
622 ss.setf(std::ios::showbase);
623#define HANDLE_INTTYPE(len) \
624 case KVM_REG_SIZE_U ## len: { \
625 uint ## len ## _t value; \
626 getOneReg(id, &value); \
627 ss << value; \
628 } break
629
630#define HANDLE_ARRAY(len) \
631 case KVM_REG_SIZE_U ## len: { \
632 uint8_t value[len / 8]; \
633 getOneReg(id, value); \
634 ss << "[" << value[0]; \
635 for (int i = 1; i < len / 8; ++i) \
636 ss << ", " << value[i]; \
637 ss << "]"; \
638 } break
639
640 switch (id & KVM_REG_SIZE_MASK) {
641 HANDLE_INTTYPE(8);
642 HANDLE_INTTYPE(16);
643 HANDLE_INTTYPE(32);
644 HANDLE_INTTYPE(64);
645 HANDLE_ARRAY(128);
646 HANDLE_ARRAY(256);
647 HANDLE_ARRAY(512);
648 HANDLE_ARRAY(1024);
649 default:
650 ss << "??";
651 }
652
653#undef HANDLE_INTTYPE
654#undef HANDLE_ARRAY
655
656 return ss.str();
657#else
658 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
659#endif
660}
661
662void
663BaseKvmCPU::syncThreadContext()
664{
665 if (!kvmStateDirty)
666 return;
667
668 assert(!threadContextDirty);
669
670 updateThreadContext();
671 kvmStateDirty = false;
672}
673
674void
675BaseKvmCPU::syncKvmState()
676{
677 if (!threadContextDirty)
678 return;
679
680 assert(!kvmStateDirty);
681
682 updateKvmState();
683 threadContextDirty = false;
684}
685
686Tick
687BaseKvmCPU::handleKvmExit()
688{
689 DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason);
690
691 switch (_kvmRun->exit_reason) {
692 case KVM_EXIT_UNKNOWN:
693 return handleKvmExitUnknown();
694
695 case KVM_EXIT_EXCEPTION:
696 return handleKvmExitException();
697
698 case KVM_EXIT_IO:
699 ++numIO;
700 return handleKvmExitIO();
701
702 case KVM_EXIT_HYPERCALL:
703 ++numHypercalls;
704 return handleKvmExitHypercall();
705
706 case KVM_EXIT_HLT:
707 /* The guest has halted and is waiting for interrupts */
708 DPRINTF(Kvm, "handleKvmExitHalt\n");
709 ++numHalt;
710
711 // Suspend the thread until the next interrupt arrives
712 thread->suspend();
713
714 // This is actually ignored since the thread is suspended.
715 return 0;
716
717 case KVM_EXIT_MMIO:
718 /* Service memory mapped IO requests */
719 DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
720 _kvmRun->mmio.is_write,
721 _kvmRun->mmio.phys_addr, _kvmRun->mmio.len);
722
723 ++numMMIO;
724 return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data,
725 _kvmRun->mmio.len, _kvmRun->mmio.is_write);
726
727 case KVM_EXIT_IRQ_WINDOW_OPEN:
728 return handleKvmExitIRQWindowOpen();
729
730 case KVM_EXIT_FAIL_ENTRY:
731 return handleKvmExitFailEntry();
732
733 case KVM_EXIT_INTR:
734 /* KVM was interrupted by a signal, restart it in the next
735 * tick. */
736 return 0;
737
738 case KVM_EXIT_INTERNAL_ERROR:
739 panic("KVM: Internal error (suberror: %u)\n",
740 _kvmRun->internal.suberror);
741
742 default:
743 dump();
744 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason);
745 }
746}
747
748Tick
749BaseKvmCPU::handleKvmExitIO()
750{
751 panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
752 _kvmRun->io.direction, _kvmRun->io.size,
753 _kvmRun->io.port, _kvmRun->io.count);
754}
755
756Tick
757BaseKvmCPU::handleKvmExitHypercall()
758{
759 panic("KVM: Unhandled hypercall\n");
760}
761
762Tick
763BaseKvmCPU::handleKvmExitIRQWindowOpen()
764{
765 warn("KVM: Unhandled IRQ window.\n");
766 return 0;
767}
768
769
770Tick
771BaseKvmCPU::handleKvmExitUnknown()
772{
773 dump();
774 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
775 _kvmRun->hw.hardware_exit_reason);
776}
777
778Tick
779BaseKvmCPU::handleKvmExitException()
780{
781 dump();
782 panic("KVM: Got exception when starting vCPU "
783 "(exception: %u, error_code: %u)\n",
784 _kvmRun->ex.exception, _kvmRun->ex.error_code);
785}
786
787Tick
788BaseKvmCPU::handleKvmExitFailEntry()
789{
790 dump();
791 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
792 _kvmRun->fail_entry.hardware_entry_failure_reason);
793}
794
795Tick
796BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write)
797{
798 mmio_req.setPhys(paddr, size, Request::UNCACHEABLE, dataMasterId());
799
800 const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq);
801 Packet pkt(&mmio_req, cmd);
802 pkt.dataStatic(data);
803 return dataPort.sendAtomic(&pkt);
804}
805
806int
807BaseKvmCPU::ioctl(int request, long p1) const
808{
809 if (vcpuFD == -1)
810 panic("KVM: CPU ioctl called before initialization\n");
811
812 return ::ioctl(vcpuFD, request, p1);
813}
814
815Tick
816BaseKvmCPU::flushCoalescedMMIO()
817{
818 if (!mmioRing)
819 return 0;
820
821 DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n");
822
823 // TODO: We might need to do synchronization when we start to
824 // support multiple CPUs
825 Tick ticks(0);
826 while (mmioRing->first != mmioRing->last) {
827 struct kvm_coalesced_mmio &ent(
828 mmioRing->coalesced_mmio[mmioRing->first]);
829
830 DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
831 ent.phys_addr, ent.len);
832
833 ++numCoalescedMMIO;
834 ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true);
835
836 mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX;
837 }
838
839 return ticks;
840}
841
842void
843BaseKvmCPU::setupSignalHandler()
844{
845 struct sigaction sa;
846
847 memset(&sa, 0, sizeof(sa));
848 sa.sa_sigaction = onTimerOverflow;
849 sa.sa_flags = SA_SIGINFO | SA_RESTART;
850 if (sigaction(KVM_TIMER_SIGNAL, &sa, NULL) == -1)
851 panic("KVM: Failed to setup vCPU signal handler\n");
852}
853
854void
855BaseKvmCPU::setupCounters()
856{
857 DPRINTF(Kvm, "Attaching cycle counter...\n");
858 PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE,
859 PERF_COUNT_HW_CPU_CYCLES);
860 cfgCycles.disabled(true)
861 .pinned(true);
862
863 if (perfControlledByTimer) {
864 // We need to configure the cycles counter to send overflows
865 // since we are going to use it to trigger timer signals that
866 // trap back into m5 from KVM. In practice, this means that we
867 // need to set some non-zero sample period that gets
868 // overridden when the timer is armed.
869 cfgCycles.wakeupEvents(1)
870 .samplePeriod(42);
871 }
872
873 hwCycles.attach(cfgCycles,
874 0); // TID (0 => currentThread)
875
876 DPRINTF(Kvm, "Attaching instruction counter...\n");
877 PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE,
878 PERF_COUNT_HW_INSTRUCTIONS);
879 hwInstructions.attach(cfgInstructions,
880 0, // TID (0 => currentThread)
881 hwCycles);
882}