base.cc (10858:6734ec272816) base.cc (10905:a6ca6831e775)
1/*
2 * Copyright (c) 2012, 2015 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/mmapped_ipr.hh"
50#include "arch/utility.hh"
51#include "cpu/kvm/base.hh"
52#include "debug/Checkpoint.hh"
53#include "debug/Drain.hh"
54#include "debug/Kvm.hh"
55#include "debug/KvmIO.hh"
56#include "debug/KvmRun.hh"
57#include "params/BaseKvmCPU.hh"
58#include "sim/process.hh"
59#include "sim/system.hh"
60
61#include <signal.h>
62
63/* Used by some KVM macros */
64#define PAGE_SIZE pageSize
65
66BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params)
67 : BaseCPU(params),
68 vm(*params->kvmVM),
69 _status(Idle),
70 dataPort(name() + ".dcache_port", this),
71 instPort(name() + ".icache_port", this),
72 threadContextDirty(true),
73 kvmStateDirty(false),
74 vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
75 _kvmRun(NULL), mmioRing(NULL),
76 pageSize(sysconf(_SC_PAGE_SIZE)),
77 tickEvent(*this),
78 activeInstPeriod(0),
79 perfControlledByTimer(params->usePerfOverflow),
80 hostFactor(params->hostFactor),
81 drainManager(NULL),
82 ctrInsts(0)
83{
84 if (pageSize == -1)
85 panic("KVM: Failed to determine host page size (%i)\n",
86 errno);
87
88 if (FullSystem)
89 thread = new SimpleThread(this, 0, params->system, params->itb, params->dtb,
90 params->isa[0]);
91 else
92 thread = new SimpleThread(this, /* thread_num */ 0, params->system,
93 params->workload[0], params->itb,
94 params->dtb, params->isa[0]);
95
96 thread->setStatus(ThreadContext::Halted);
97 tc = thread->getTC();
98 threadContexts.push_back(tc);
99}
100
101BaseKvmCPU::~BaseKvmCPU()
102{
103 if (_kvmRun)
104 munmap(_kvmRun, vcpuMMapSize);
105 close(vcpuFD);
106}
107
108void
109BaseKvmCPU::init()
110{
111 BaseCPU::init();
112
113 if (numThreads != 1)
114 fatal("KVM: Multithreading not supported");
115
116 tc->initMemProxies(tc);
117
118 // initialize CPU, including PC
119 if (FullSystem && !switchedOut())
120 TheISA::initCPU(tc, tc->contextId());
121}
122
123void
124BaseKvmCPU::startup()
125{
126 const BaseKvmCPUParams * const p(
127 dynamic_cast<const BaseKvmCPUParams *>(params()));
128
129 Kvm &kvm(vm.kvm);
130
131 BaseCPU::startup();
132
133 assert(vcpuFD == -1);
134
135 // Tell the VM that a CPU is about to start.
136 vm.cpuStartup();
137
138 // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
139 // not guaranteed that the parent KVM VM has initialized at that
140 // point. Initialize virtual CPUs here instead.
141 vcpuFD = vm.createVCPU(vcpuID);
142
143 // Map the KVM run structure */
144 vcpuMMapSize = kvm.getVCPUMMapSize();
145 _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize,
146 PROT_READ | PROT_WRITE, MAP_SHARED,
147 vcpuFD, 0);
148 if (_kvmRun == MAP_FAILED)
149 panic("KVM: Failed to map run data structure\n");
150
151 // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
152 // available. The offset into the KVM's communication page is
153 // provided by the coalesced MMIO capability.
154 int mmioOffset(kvm.capCoalescedMMIO());
155 if (!p->useCoalescedMMIO) {
156 inform("KVM: Coalesced MMIO disabled by config.\n");
157 } else if (mmioOffset) {
158 inform("KVM: Coalesced IO available\n");
159 mmioRing = (struct kvm_coalesced_mmio_ring *)(
160 (char *)_kvmRun + (mmioOffset * pageSize));
161 } else {
162 inform("KVM: Coalesced not supported by host OS\n");
163 }
164
165 thread->startup();
166
167 Event *startupEvent(
168 new EventWrapper<BaseKvmCPU,
169 &BaseKvmCPU::startupThread>(this, true));
170 schedule(startupEvent, curTick());
171}
172
173void
174BaseKvmCPU::startupThread()
175{
176 // Do thread-specific initialization. We need to setup signal
177 // delivery for counters and timers from within the thread that
178 // will execute the event queue to ensure that signals are
179 // delivered to the right threads.
180 const BaseKvmCPUParams * const p(
181 dynamic_cast<const BaseKvmCPUParams *>(params()));
182
183 vcpuThread = pthread_self();
184
185 // Setup signal handlers. This has to be done after the vCPU is
186 // created since it manipulates the vCPU signal mask.
187 setupSignalHandler();
188
189 setupCounters();
190
191 if (p->usePerfOverflow)
192 runTimer.reset(new PerfKvmTimer(hwCycles,
193 KVM_KICK_SIGNAL,
194 p->hostFactor,
195 p->hostFreq));
196 else
197 runTimer.reset(new PosixKvmTimer(KVM_KICK_SIGNAL, CLOCK_MONOTONIC,
198 p->hostFactor,
199 p->hostFreq));
200
201}
202
203void
204BaseKvmCPU::regStats()
205{
206 using namespace Stats;
207
208 BaseCPU::regStats();
209
210 numInsts
211 .name(name() + ".committedInsts")
212 .desc("Number of instructions committed")
213 ;
214
215 numVMExits
216 .name(name() + ".numVMExits")
217 .desc("total number of KVM exits")
218 ;
219
220 numVMHalfEntries
221 .name(name() + ".numVMHalfEntries")
222 .desc("number of KVM entries to finalize pending operations")
223 ;
224
225 numExitSignal
226 .name(name() + ".numExitSignal")
227 .desc("exits due to signal delivery")
228 ;
229
230 numMMIO
231 .name(name() + ".numMMIO")
232 .desc("number of VM exits due to memory mapped IO")
233 ;
234
235 numCoalescedMMIO
236 .name(name() + ".numCoalescedMMIO")
237 .desc("number of coalesced memory mapped IO requests")
238 ;
239
240 numIO
241 .name(name() + ".numIO")
242 .desc("number of VM exits due to legacy IO")
243 ;
244
245 numHalt
246 .name(name() + ".numHalt")
247 .desc("number of VM exits due to wait for interrupt instructions")
248 ;
249
250 numInterrupts
251 .name(name() + ".numInterrupts")
252 .desc("number of interrupts delivered")
253 ;
254
255 numHypercalls
256 .name(name() + ".numHypercalls")
257 .desc("number of hypercalls")
258 ;
259}
260
261void
1/*
2 * Copyright (c) 2012, 2015 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/mmapped_ipr.hh"
50#include "arch/utility.hh"
51#include "cpu/kvm/base.hh"
52#include "debug/Checkpoint.hh"
53#include "debug/Drain.hh"
54#include "debug/Kvm.hh"
55#include "debug/KvmIO.hh"
56#include "debug/KvmRun.hh"
57#include "params/BaseKvmCPU.hh"
58#include "sim/process.hh"
59#include "sim/system.hh"
60
61#include <signal.h>
62
63/* Used by some KVM macros */
64#define PAGE_SIZE pageSize
65
66BaseKvmCPU::BaseKvmCPU(BaseKvmCPUParams *params)
67 : BaseCPU(params),
68 vm(*params->kvmVM),
69 _status(Idle),
70 dataPort(name() + ".dcache_port", this),
71 instPort(name() + ".icache_port", this),
72 threadContextDirty(true),
73 kvmStateDirty(false),
74 vcpuID(vm.allocVCPUID()), vcpuFD(-1), vcpuMMapSize(0),
75 _kvmRun(NULL), mmioRing(NULL),
76 pageSize(sysconf(_SC_PAGE_SIZE)),
77 tickEvent(*this),
78 activeInstPeriod(0),
79 perfControlledByTimer(params->usePerfOverflow),
80 hostFactor(params->hostFactor),
81 drainManager(NULL),
82 ctrInsts(0)
83{
84 if (pageSize == -1)
85 panic("KVM: Failed to determine host page size (%i)\n",
86 errno);
87
88 if (FullSystem)
89 thread = new SimpleThread(this, 0, params->system, params->itb, params->dtb,
90 params->isa[0]);
91 else
92 thread = new SimpleThread(this, /* thread_num */ 0, params->system,
93 params->workload[0], params->itb,
94 params->dtb, params->isa[0]);
95
96 thread->setStatus(ThreadContext::Halted);
97 tc = thread->getTC();
98 threadContexts.push_back(tc);
99}
100
101BaseKvmCPU::~BaseKvmCPU()
102{
103 if (_kvmRun)
104 munmap(_kvmRun, vcpuMMapSize);
105 close(vcpuFD);
106}
107
108void
109BaseKvmCPU::init()
110{
111 BaseCPU::init();
112
113 if (numThreads != 1)
114 fatal("KVM: Multithreading not supported");
115
116 tc->initMemProxies(tc);
117
118 // initialize CPU, including PC
119 if (FullSystem && !switchedOut())
120 TheISA::initCPU(tc, tc->contextId());
121}
122
123void
124BaseKvmCPU::startup()
125{
126 const BaseKvmCPUParams * const p(
127 dynamic_cast<const BaseKvmCPUParams *>(params()));
128
129 Kvm &kvm(vm.kvm);
130
131 BaseCPU::startup();
132
133 assert(vcpuFD == -1);
134
135 // Tell the VM that a CPU is about to start.
136 vm.cpuStartup();
137
138 // We can't initialize KVM CPUs in BaseKvmCPU::init() since we are
139 // not guaranteed that the parent KVM VM has initialized at that
140 // point. Initialize virtual CPUs here instead.
141 vcpuFD = vm.createVCPU(vcpuID);
142
143 // Map the KVM run structure */
144 vcpuMMapSize = kvm.getVCPUMMapSize();
145 _kvmRun = (struct kvm_run *)mmap(0, vcpuMMapSize,
146 PROT_READ | PROT_WRITE, MAP_SHARED,
147 vcpuFD, 0);
148 if (_kvmRun == MAP_FAILED)
149 panic("KVM: Failed to map run data structure\n");
150
151 // Setup a pointer to the MMIO ring buffer if coalesced MMIO is
152 // available. The offset into the KVM's communication page is
153 // provided by the coalesced MMIO capability.
154 int mmioOffset(kvm.capCoalescedMMIO());
155 if (!p->useCoalescedMMIO) {
156 inform("KVM: Coalesced MMIO disabled by config.\n");
157 } else if (mmioOffset) {
158 inform("KVM: Coalesced IO available\n");
159 mmioRing = (struct kvm_coalesced_mmio_ring *)(
160 (char *)_kvmRun + (mmioOffset * pageSize));
161 } else {
162 inform("KVM: Coalesced not supported by host OS\n");
163 }
164
165 thread->startup();
166
167 Event *startupEvent(
168 new EventWrapper<BaseKvmCPU,
169 &BaseKvmCPU::startupThread>(this, true));
170 schedule(startupEvent, curTick());
171}
172
173void
174BaseKvmCPU::startupThread()
175{
176 // Do thread-specific initialization. We need to setup signal
177 // delivery for counters and timers from within the thread that
178 // will execute the event queue to ensure that signals are
179 // delivered to the right threads.
180 const BaseKvmCPUParams * const p(
181 dynamic_cast<const BaseKvmCPUParams *>(params()));
182
183 vcpuThread = pthread_self();
184
185 // Setup signal handlers. This has to be done after the vCPU is
186 // created since it manipulates the vCPU signal mask.
187 setupSignalHandler();
188
189 setupCounters();
190
191 if (p->usePerfOverflow)
192 runTimer.reset(new PerfKvmTimer(hwCycles,
193 KVM_KICK_SIGNAL,
194 p->hostFactor,
195 p->hostFreq));
196 else
197 runTimer.reset(new PosixKvmTimer(KVM_KICK_SIGNAL, CLOCK_MONOTONIC,
198 p->hostFactor,
199 p->hostFreq));
200
201}
202
203void
204BaseKvmCPU::regStats()
205{
206 using namespace Stats;
207
208 BaseCPU::regStats();
209
210 numInsts
211 .name(name() + ".committedInsts")
212 .desc("Number of instructions committed")
213 ;
214
215 numVMExits
216 .name(name() + ".numVMExits")
217 .desc("total number of KVM exits")
218 ;
219
220 numVMHalfEntries
221 .name(name() + ".numVMHalfEntries")
222 .desc("number of KVM entries to finalize pending operations")
223 ;
224
225 numExitSignal
226 .name(name() + ".numExitSignal")
227 .desc("exits due to signal delivery")
228 ;
229
230 numMMIO
231 .name(name() + ".numMMIO")
232 .desc("number of VM exits due to memory mapped IO")
233 ;
234
235 numCoalescedMMIO
236 .name(name() + ".numCoalescedMMIO")
237 .desc("number of coalesced memory mapped IO requests")
238 ;
239
240 numIO
241 .name(name() + ".numIO")
242 .desc("number of VM exits due to legacy IO")
243 ;
244
245 numHalt
246 .name(name() + ".numHalt")
247 .desc("number of VM exits due to wait for interrupt instructions")
248 ;
249
250 numInterrupts
251 .name(name() + ".numInterrupts")
252 .desc("number of interrupts delivered")
253 ;
254
255 numHypercalls
256 .name(name() + ".numHypercalls")
257 .desc("number of hypercalls")
258 ;
259}
260
261void
262BaseKvmCPU::serializeThread(std::ostream &os, ThreadID tid)
262BaseKvmCPU::serializeThread(CheckpointOut &cp, ThreadID tid) const
263{
264 if (DTRACE(Checkpoint)) {
265 DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid);
266 dump();
267 }
268
269 assert(tid == 0);
270 assert(_status == Idle);
263{
264 if (DTRACE(Checkpoint)) {
265 DPRINTF(Checkpoint, "KVM: Serializing thread %i:\n", tid);
266 dump();
267 }
268
269 assert(tid == 0);
270 assert(_status == Idle);
271 thread->serialize(os);
271 thread->serialize(cp);
272}
273
274void
272}
273
274void
275BaseKvmCPU::unserializeThread(Checkpoint *cp, const std::string &section,
276 ThreadID tid)
275BaseKvmCPU::unserializeThread(CheckpointIn &cp, ThreadID tid)
277{
278 DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid);
279
280 assert(tid == 0);
281 assert(_status == Idle);
276{
277 DPRINTF(Checkpoint, "KVM: Unserialize thread %i:\n", tid);
278
279 assert(tid == 0);
280 assert(_status == Idle);
282 thread->unserialize(cp, section);
281 thread->unserialize(cp);
283 threadContextDirty = true;
284}
285
286unsigned int
287BaseKvmCPU::drain(DrainManager *dm)
288{
289 if (switchedOut())
290 return 0;
291
292 DPRINTF(Drain, "BaseKvmCPU::drain\n");
293 switch (_status) {
294 case Running:
295 // The base KVM code is normally ready when it is in the
296 // Running state, but the architecture specific code might be
297 // of a different opinion. This may happen when the CPU been
298 // notified of an event that hasn't been accepted by the vCPU
299 // yet.
300 if (!archIsDrained()) {
301 drainManager = dm;
302 return 1;
303 }
304
305 // The state of the CPU is consistent, so we don't need to do
306 // anything special to drain it. We simply de-schedule the
307 // tick event and enter the Idle state to prevent nasty things
308 // like MMIOs from happening.
309 if (tickEvent.scheduled())
310 deschedule(tickEvent);
311 _status = Idle;
312
313 /** FALLTHROUGH */
314 case Idle:
315 // Idle, no need to drain
316 assert(!tickEvent.scheduled());
317
318 // Sync the thread context here since we'll need it when we
319 // switch CPUs or checkpoint the CPU.
320 syncThreadContext();
321
322 return 0;
323
324 case RunningServiceCompletion:
325 // The CPU has just requested a service that was handled in
326 // the RunningService state, but the results have still not
327 // been reported to the CPU. Now, we /could/ probably just
328 // update the register state ourselves instead of letting KVM
329 // handle it, but that would be tricky. Instead, we enter KVM
330 // and let it do its stuff.
331 drainManager = dm;
332
333 DPRINTF(Drain, "KVM CPU is waiting for service completion, "
334 "requesting drain.\n");
335 return 1;
336
337 case RunningService:
338 // We need to drain since the CPU is waiting for service (e.g., MMIOs)
339 drainManager = dm;
340
341 DPRINTF(Drain, "KVM CPU is waiting for service, requesting drain.\n");
342 return 1;
343
344 default:
345 panic("KVM: Unhandled CPU state in drain()\n");
346 return 0;
347 }
348}
349
350void
351BaseKvmCPU::drainResume()
352{
353 assert(!tickEvent.scheduled());
354
355 // We might have been switched out. In that case, we don't need to
356 // do anything.
357 if (switchedOut())
358 return;
359
360 DPRINTF(Kvm, "drainResume\n");
361 verifyMemoryMode();
362
363 // The tick event is de-scheduled as a part of the draining
364 // process. Re-schedule it if the thread context is active.
365 if (tc->status() == ThreadContext::Active) {
366 schedule(tickEvent, nextCycle());
367 _status = Running;
368 } else {
369 _status = Idle;
370 }
371}
372
373void
374BaseKvmCPU::switchOut()
375{
376 DPRINTF(Kvm, "switchOut\n");
377
378 BaseCPU::switchOut();
379
380 // We should have drained prior to executing a switchOut, which
381 // means that the tick event shouldn't be scheduled and the CPU is
382 // idle.
383 assert(!tickEvent.scheduled());
384 assert(_status == Idle);
385}
386
387void
388BaseKvmCPU::takeOverFrom(BaseCPU *cpu)
389{
390 DPRINTF(Kvm, "takeOverFrom\n");
391
392 BaseCPU::takeOverFrom(cpu);
393
394 // We should have drained prior to executing a switchOut, which
395 // means that the tick event shouldn't be scheduled and the CPU is
396 // idle.
397 assert(!tickEvent.scheduled());
398 assert(_status == Idle);
399 assert(threadContexts.size() == 1);
400
401 // Force an update of the KVM state here instead of flagging the
402 // TC as dirty. This is not ideal from a performance point of
403 // view, but it makes debugging easier as it allows meaningful KVM
404 // state to be dumped before and after a takeover.
405 updateKvmState();
406 threadContextDirty = false;
407}
408
409void
410BaseKvmCPU::verifyMemoryMode() const
411{
412 if (!(system->isAtomicMode() && system->bypassCaches())) {
413 fatal("The KVM-based CPUs requires the memory system to be in the "
414 "'atomic_noncaching' mode.\n");
415 }
416}
417
418void
419BaseKvmCPU::wakeup()
420{
421 DPRINTF(Kvm, "wakeup()\n");
422 // This method might have been called from another
423 // context. Migrate to this SimObject's event queue when
424 // delivering the wakeup signal.
425 EventQueue::ScopedMigration migrate(eventQueue());
426
427 // Kick the vCPU to get it to come out of KVM.
428 kick();
429
430 if (thread->status() != ThreadContext::Suspended)
431 return;
432
433 thread->activate();
434}
435
436void
437BaseKvmCPU::activateContext(ThreadID thread_num)
438{
439 DPRINTF(Kvm, "ActivateContext %d\n", thread_num);
440
441 assert(thread_num == 0);
442 assert(thread);
443
444 assert(_status == Idle);
445 assert(!tickEvent.scheduled());
446
447 numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend);
448
449 schedule(tickEvent, clockEdge(Cycles(0)));
450 _status = Running;
451}
452
453
454void
455BaseKvmCPU::suspendContext(ThreadID thread_num)
456{
457 DPRINTF(Kvm, "SuspendContext %d\n", thread_num);
458
459 assert(thread_num == 0);
460 assert(thread);
461
462 if (_status == Idle)
463 return;
464
465 assert(_status == Running || _status == RunningServiceCompletion);
466
467 // The tick event may no be scheduled if the quest has requested
468 // the monitor to wait for interrupts. The normal CPU models can
469 // get their tick events descheduled by quiesce instructions, but
470 // that can't happen here.
471 if (tickEvent.scheduled())
472 deschedule(tickEvent);
473
474 _status = Idle;
475}
476
477void
478BaseKvmCPU::deallocateContext(ThreadID thread_num)
479{
480 // for now, these are equivalent
481 suspendContext(thread_num);
482}
483
484void
485BaseKvmCPU::haltContext(ThreadID thread_num)
486{
487 // for now, these are equivalent
488 suspendContext(thread_num);
489}
490
491ThreadContext *
492BaseKvmCPU::getContext(int tn)
493{
494 assert(tn == 0);
495 syncThreadContext();
496 return tc;
497}
498
499
500Counter
501BaseKvmCPU::totalInsts() const
502{
503 return ctrInsts;
504}
505
506Counter
507BaseKvmCPU::totalOps() const
508{
509 hack_once("Pretending totalOps is equivalent to totalInsts()\n");
510 return ctrInsts;
511}
512
513void
282 threadContextDirty = true;
283}
284
285unsigned int
286BaseKvmCPU::drain(DrainManager *dm)
287{
288 if (switchedOut())
289 return 0;
290
291 DPRINTF(Drain, "BaseKvmCPU::drain\n");
292 switch (_status) {
293 case Running:
294 // The base KVM code is normally ready when it is in the
295 // Running state, but the architecture specific code might be
296 // of a different opinion. This may happen when the CPU been
297 // notified of an event that hasn't been accepted by the vCPU
298 // yet.
299 if (!archIsDrained()) {
300 drainManager = dm;
301 return 1;
302 }
303
304 // The state of the CPU is consistent, so we don't need to do
305 // anything special to drain it. We simply de-schedule the
306 // tick event and enter the Idle state to prevent nasty things
307 // like MMIOs from happening.
308 if (tickEvent.scheduled())
309 deschedule(tickEvent);
310 _status = Idle;
311
312 /** FALLTHROUGH */
313 case Idle:
314 // Idle, no need to drain
315 assert(!tickEvent.scheduled());
316
317 // Sync the thread context here since we'll need it when we
318 // switch CPUs or checkpoint the CPU.
319 syncThreadContext();
320
321 return 0;
322
323 case RunningServiceCompletion:
324 // The CPU has just requested a service that was handled in
325 // the RunningService state, but the results have still not
326 // been reported to the CPU. Now, we /could/ probably just
327 // update the register state ourselves instead of letting KVM
328 // handle it, but that would be tricky. Instead, we enter KVM
329 // and let it do its stuff.
330 drainManager = dm;
331
332 DPRINTF(Drain, "KVM CPU is waiting for service completion, "
333 "requesting drain.\n");
334 return 1;
335
336 case RunningService:
337 // We need to drain since the CPU is waiting for service (e.g., MMIOs)
338 drainManager = dm;
339
340 DPRINTF(Drain, "KVM CPU is waiting for service, requesting drain.\n");
341 return 1;
342
343 default:
344 panic("KVM: Unhandled CPU state in drain()\n");
345 return 0;
346 }
347}
348
349void
350BaseKvmCPU::drainResume()
351{
352 assert(!tickEvent.scheduled());
353
354 // We might have been switched out. In that case, we don't need to
355 // do anything.
356 if (switchedOut())
357 return;
358
359 DPRINTF(Kvm, "drainResume\n");
360 verifyMemoryMode();
361
362 // The tick event is de-scheduled as a part of the draining
363 // process. Re-schedule it if the thread context is active.
364 if (tc->status() == ThreadContext::Active) {
365 schedule(tickEvent, nextCycle());
366 _status = Running;
367 } else {
368 _status = Idle;
369 }
370}
371
372void
373BaseKvmCPU::switchOut()
374{
375 DPRINTF(Kvm, "switchOut\n");
376
377 BaseCPU::switchOut();
378
379 // We should have drained prior to executing a switchOut, which
380 // means that the tick event shouldn't be scheduled and the CPU is
381 // idle.
382 assert(!tickEvent.scheduled());
383 assert(_status == Idle);
384}
385
386void
387BaseKvmCPU::takeOverFrom(BaseCPU *cpu)
388{
389 DPRINTF(Kvm, "takeOverFrom\n");
390
391 BaseCPU::takeOverFrom(cpu);
392
393 // We should have drained prior to executing a switchOut, which
394 // means that the tick event shouldn't be scheduled and the CPU is
395 // idle.
396 assert(!tickEvent.scheduled());
397 assert(_status == Idle);
398 assert(threadContexts.size() == 1);
399
400 // Force an update of the KVM state here instead of flagging the
401 // TC as dirty. This is not ideal from a performance point of
402 // view, but it makes debugging easier as it allows meaningful KVM
403 // state to be dumped before and after a takeover.
404 updateKvmState();
405 threadContextDirty = false;
406}
407
408void
409BaseKvmCPU::verifyMemoryMode() const
410{
411 if (!(system->isAtomicMode() && system->bypassCaches())) {
412 fatal("The KVM-based CPUs requires the memory system to be in the "
413 "'atomic_noncaching' mode.\n");
414 }
415}
416
417void
418BaseKvmCPU::wakeup()
419{
420 DPRINTF(Kvm, "wakeup()\n");
421 // This method might have been called from another
422 // context. Migrate to this SimObject's event queue when
423 // delivering the wakeup signal.
424 EventQueue::ScopedMigration migrate(eventQueue());
425
426 // Kick the vCPU to get it to come out of KVM.
427 kick();
428
429 if (thread->status() != ThreadContext::Suspended)
430 return;
431
432 thread->activate();
433}
434
435void
436BaseKvmCPU::activateContext(ThreadID thread_num)
437{
438 DPRINTF(Kvm, "ActivateContext %d\n", thread_num);
439
440 assert(thread_num == 0);
441 assert(thread);
442
443 assert(_status == Idle);
444 assert(!tickEvent.scheduled());
445
446 numCycles += ticksToCycles(thread->lastActivate - thread->lastSuspend);
447
448 schedule(tickEvent, clockEdge(Cycles(0)));
449 _status = Running;
450}
451
452
453void
454BaseKvmCPU::suspendContext(ThreadID thread_num)
455{
456 DPRINTF(Kvm, "SuspendContext %d\n", thread_num);
457
458 assert(thread_num == 0);
459 assert(thread);
460
461 if (_status == Idle)
462 return;
463
464 assert(_status == Running || _status == RunningServiceCompletion);
465
466 // The tick event may no be scheduled if the quest has requested
467 // the monitor to wait for interrupts. The normal CPU models can
468 // get their tick events descheduled by quiesce instructions, but
469 // that can't happen here.
470 if (tickEvent.scheduled())
471 deschedule(tickEvent);
472
473 _status = Idle;
474}
475
476void
477BaseKvmCPU::deallocateContext(ThreadID thread_num)
478{
479 // for now, these are equivalent
480 suspendContext(thread_num);
481}
482
483void
484BaseKvmCPU::haltContext(ThreadID thread_num)
485{
486 // for now, these are equivalent
487 suspendContext(thread_num);
488}
489
490ThreadContext *
491BaseKvmCPU::getContext(int tn)
492{
493 assert(tn == 0);
494 syncThreadContext();
495 return tc;
496}
497
498
499Counter
500BaseKvmCPU::totalInsts() const
501{
502 return ctrInsts;
503}
504
505Counter
506BaseKvmCPU::totalOps() const
507{
508 hack_once("Pretending totalOps is equivalent to totalInsts()\n");
509 return ctrInsts;
510}
511
512void
514BaseKvmCPU::dump()
513BaseKvmCPU::dump() const
515{
516 inform("State dumping not implemented.");
517}
518
519void
520BaseKvmCPU::tick()
521{
522 Tick delay(0);
523 assert(_status != Idle);
524
525 switch (_status) {
526 case RunningService:
527 // handleKvmExit() will determine the next state of the CPU
528 delay = handleKvmExit();
529
530 if (tryDrain())
531 _status = Idle;
532 break;
533
534 case RunningServiceCompletion:
535 case Running: {
536 const uint64_t nextInstEvent(
537 !comInstEventQueue[0]->empty() ?
538 comInstEventQueue[0]->nextTick() : UINT64_MAX);
539 // Enter into KVM and complete pending IO instructions if we
540 // have an instruction event pending.
541 const Tick ticksToExecute(
542 nextInstEvent > ctrInsts ?
543 curEventQueue()->nextTick() - curTick() : 0);
544
545 // We might need to update the KVM state.
546 syncKvmState();
547
548 // Setup any pending instruction count breakpoints using
549 // PerfEvent if we are going to execute more than just an IO
550 // completion.
551 if (ticksToExecute > 0)
552 setupInstStop();
553
554 DPRINTF(KvmRun, "Entering KVM...\n");
555 if (drainManager) {
556 // Force an immediate exit from KVM after completing
557 // pending operations. The architecture-specific code
558 // takes care to run until it is in a state where it can
559 // safely be drained.
560 delay = kvmRunDrain();
561 } else {
562 delay = kvmRun(ticksToExecute);
563 }
564
565 // The CPU might have been suspended before entering into
566 // KVM. Assume that the CPU was suspended /before/ entering
567 // into KVM and skip the exit handling.
568 if (_status == Idle)
569 break;
570
571 // Entering into KVM implies that we'll have to reload the thread
572 // context from KVM if we want to access it. Flag the KVM state as
573 // dirty with respect to the cached thread context.
574 kvmStateDirty = true;
575
576 // Enter into the RunningService state unless the
577 // simulation was stopped by a timer.
578 if (_kvmRun->exit_reason != KVM_EXIT_INTR) {
579 _status = RunningService;
580 } else {
581 ++numExitSignal;
582 _status = Running;
583 }
584
585 // Service any pending instruction events. The vCPU should
586 // have exited in time for the event using the instruction
587 // counter configured by setupInstStop().
588 comInstEventQueue[0]->serviceEvents(ctrInsts);
589 system->instEventQueue.serviceEvents(system->totalNumInsts);
590
591 if (tryDrain())
592 _status = Idle;
593 } break;
594
595 default:
596 panic("BaseKvmCPU entered tick() in an illegal state (%i)\n",
597 _status);
598 }
599
600 // Schedule a new tick if we are still running
601 if (_status != Idle)
602 schedule(tickEvent, clockEdge(ticksToCycles(delay)));
603}
604
605Tick
606BaseKvmCPU::kvmRunDrain()
607{
608 // By default, the only thing we need to drain is a pending IO
609 // operation which assumes that we are in the
610 // RunningServiceCompletion state.
611 assert(_status == RunningServiceCompletion);
612
613 // Deliver the data from the pending IO operation and immediately
614 // exit.
615 return kvmRun(0);
616}
617
618uint64_t
619BaseKvmCPU::getHostCycles() const
620{
621 return hwCycles.read();
622}
623
624Tick
625BaseKvmCPU::kvmRun(Tick ticks)
626{
627 Tick ticksExecuted;
628 DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks);
629
630 if (ticks == 0) {
631 // Settings ticks == 0 is a special case which causes an entry
632 // into KVM that finishes pending operations (e.g., IO) and
633 // then immediately exits.
634 DPRINTF(KvmRun, "KVM: Delivering IO without full guest entry\n");
635
636 ++numVMHalfEntries;
637
638 // Send a KVM_KICK_SIGNAL to the vCPU thread (i.e., this
639 // thread). The KVM control signal is masked while executing
640 // in gem5 and gets unmasked temporarily as when entering
641 // KVM. See setSignalMask() and setupSignalHandler().
642 kick();
643
644 // Start the vCPU. KVM will check for signals after completing
645 // pending operations (IO). Since the KVM_KICK_SIGNAL is
646 // pending, this forces an immediate exit to gem5 again. We
647 // don't bother to setup timers since this shouldn't actually
648 // execute any code (other than completing half-executed IO
649 // instructions) in the guest.
650 ioctlRun();
651
652 // We always execute at least one cycle to prevent the
653 // BaseKvmCPU::tick() to be rescheduled on the same tick
654 // twice.
655 ticksExecuted = clockPeriod();
656 } else {
657 // This method is executed as a result of a tick event. That
658 // means that the event queue will be locked when entering the
659 // method. We temporarily unlock the event queue to allow
660 // other threads to steal control of this thread to inject
661 // interrupts. They will typically lock the queue and then
662 // force an exit from KVM by kicking the vCPU.
663 EventQueue::ScopedRelease release(curEventQueue());
664
665 if (ticks < runTimer->resolution()) {
666 DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
667 ticks, runTimer->resolution());
668 ticks = runTimer->resolution();
669 }
670
671 // Get hardware statistics after synchronizing contexts. The KVM
672 // state update might affect guest cycle counters.
673 uint64_t baseCycles(getHostCycles());
674 uint64_t baseInstrs(hwInstructions.read());
675
676 // Arm the run timer and start the cycle timer if it isn't
677 // controlled by the overflow timer. Starting/stopping the cycle
678 // timer automatically starts the other perf timers as they are in
679 // the same counter group.
680 runTimer->arm(ticks);
681 if (!perfControlledByTimer)
682 hwCycles.start();
683
684 ioctlRun();
685
686 runTimer->disarm();
687 if (!perfControlledByTimer)
688 hwCycles.stop();
689
690 // The control signal may have been delivered after we exited
691 // from KVM. It will be pending in that case since it is
692 // masked when we aren't executing in KVM. Discard it to make
693 // sure we don't deliver it immediately next time we try to
694 // enter into KVM.
695 discardPendingSignal(KVM_KICK_SIGNAL);
696
697 const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles);
698 const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor);
699 const uint64_t instsExecuted(hwInstructions.read() - baseInstrs);
700 ticksExecuted = runTimer->ticksFromHostCycles(hostCyclesExecuted);
701
702 /* Update statistics */
703 numCycles += simCyclesExecuted;;
704 numInsts += instsExecuted;
705 ctrInsts += instsExecuted;
706 system->totalNumInsts += instsExecuted;
707
708 DPRINTF(KvmRun,
709 "KVM: Executed %i instructions in %i cycles "
710 "(%i ticks, sim cycles: %i).\n",
711 instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted);
712 }
713
714 ++numVMExits;
715
716 return ticksExecuted + flushCoalescedMMIO();
717}
718
719void
720BaseKvmCPU::kvmNonMaskableInterrupt()
721{
722 ++numInterrupts;
723 if (ioctl(KVM_NMI) == -1)
724 panic("KVM: Failed to deliver NMI to virtual CPU\n");
725}
726
727void
728BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt)
729{
730 ++numInterrupts;
731 if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1)
732 panic("KVM: Failed to deliver interrupt to virtual CPU\n");
733}
734
735void
736BaseKvmCPU::getRegisters(struct kvm_regs &regs) const
737{
738 if (ioctl(KVM_GET_REGS, &regs) == -1)
739 panic("KVM: Failed to get guest registers\n");
740}
741
742void
743BaseKvmCPU::setRegisters(const struct kvm_regs &regs)
744{
745 if (ioctl(KVM_SET_REGS, (void *)&regs) == -1)
746 panic("KVM: Failed to set guest registers\n");
747}
748
749void
750BaseKvmCPU::getSpecialRegisters(struct kvm_sregs &regs) const
751{
752 if (ioctl(KVM_GET_SREGS, &regs) == -1)
753 panic("KVM: Failed to get guest special registers\n");
754}
755
756void
757BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs &regs)
758{
759 if (ioctl(KVM_SET_SREGS, (void *)&regs) == -1)
760 panic("KVM: Failed to set guest special registers\n");
761}
762
763void
764BaseKvmCPU::getFPUState(struct kvm_fpu &state) const
765{
766 if (ioctl(KVM_GET_FPU, &state) == -1)
767 panic("KVM: Failed to get guest FPU state\n");
768}
769
770void
771BaseKvmCPU::setFPUState(const struct kvm_fpu &state)
772{
773 if (ioctl(KVM_SET_FPU, (void *)&state) == -1)
774 panic("KVM: Failed to set guest FPU state\n");
775}
776
777
778void
779BaseKvmCPU::setOneReg(uint64_t id, const void *addr)
780{
781#ifdef KVM_SET_ONE_REG
782 struct kvm_one_reg reg;
783 reg.id = id;
784 reg.addr = (uint64_t)addr;
785
786 if (ioctl(KVM_SET_ONE_REG, &reg) == -1) {
787 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
788 id, errno);
789 }
790#else
791 panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
792#endif
793}
794
795void
796BaseKvmCPU::getOneReg(uint64_t id, void *addr) const
797{
798#ifdef KVM_GET_ONE_REG
799 struct kvm_one_reg reg;
800 reg.id = id;
801 reg.addr = (uint64_t)addr;
802
803 if (ioctl(KVM_GET_ONE_REG, &reg) == -1) {
804 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
805 id, errno);
806 }
807#else
808 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
809#endif
810}
811
812std::string
813BaseKvmCPU::getAndFormatOneReg(uint64_t id) const
814{
815#ifdef KVM_GET_ONE_REG
816 std::ostringstream ss;
817
818 ss.setf(std::ios::hex, std::ios::basefield);
819 ss.setf(std::ios::showbase);
820#define HANDLE_INTTYPE(len) \
821 case KVM_REG_SIZE_U ## len: { \
822 uint ## len ## _t value; \
823 getOneReg(id, &value); \
824 ss << value; \
825 } break
826
827#define HANDLE_ARRAY(len) \
828 case KVM_REG_SIZE_U ## len: { \
829 uint8_t value[len / 8]; \
830 getOneReg(id, value); \
831 ccprintf(ss, "[0x%x", value[0]); \
832 for (int i = 1; i < len / 8; ++i) \
833 ccprintf(ss, ", 0x%x", value[i]); \
834 ccprintf(ss, "]"); \
835 } break
836
837 switch (id & KVM_REG_SIZE_MASK) {
838 HANDLE_INTTYPE(8);
839 HANDLE_INTTYPE(16);
840 HANDLE_INTTYPE(32);
841 HANDLE_INTTYPE(64);
842 HANDLE_ARRAY(128);
843 HANDLE_ARRAY(256);
844 HANDLE_ARRAY(512);
845 HANDLE_ARRAY(1024);
846 default:
847 ss << "??";
848 }
849
850#undef HANDLE_INTTYPE
851#undef HANDLE_ARRAY
852
853 return ss.str();
854#else
855 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
856#endif
857}
858
859void
860BaseKvmCPU::syncThreadContext()
861{
862 if (!kvmStateDirty)
863 return;
864
865 assert(!threadContextDirty);
866
867 updateThreadContext();
868 kvmStateDirty = false;
869}
870
871void
872BaseKvmCPU::syncKvmState()
873{
874 if (!threadContextDirty)
875 return;
876
877 assert(!kvmStateDirty);
878
879 updateKvmState();
880 threadContextDirty = false;
881}
882
883Tick
884BaseKvmCPU::handleKvmExit()
885{
886 DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason);
887 assert(_status == RunningService);
888
889 // Switch into the running state by default. Individual handlers
890 // can override this.
891 _status = Running;
892 switch (_kvmRun->exit_reason) {
893 case KVM_EXIT_UNKNOWN:
894 return handleKvmExitUnknown();
895
896 case KVM_EXIT_EXCEPTION:
897 return handleKvmExitException();
898
899 case KVM_EXIT_IO:
900 _status = RunningServiceCompletion;
901 ++numIO;
902 return handleKvmExitIO();
903
904 case KVM_EXIT_HYPERCALL:
905 ++numHypercalls;
906 return handleKvmExitHypercall();
907
908 case KVM_EXIT_HLT:
909 /* The guest has halted and is waiting for interrupts */
910 DPRINTF(Kvm, "handleKvmExitHalt\n");
911 ++numHalt;
912
913 // Suspend the thread until the next interrupt arrives
914 thread->suspend();
915
916 // This is actually ignored since the thread is suspended.
917 return 0;
918
919 case KVM_EXIT_MMIO:
920 _status = RunningServiceCompletion;
921 /* Service memory mapped IO requests */
922 DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
923 _kvmRun->mmio.is_write,
924 _kvmRun->mmio.phys_addr, _kvmRun->mmio.len);
925
926 ++numMMIO;
927 return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data,
928 _kvmRun->mmio.len, _kvmRun->mmio.is_write);
929
930 case KVM_EXIT_IRQ_WINDOW_OPEN:
931 return handleKvmExitIRQWindowOpen();
932
933 case KVM_EXIT_FAIL_ENTRY:
934 return handleKvmExitFailEntry();
935
936 case KVM_EXIT_INTR:
937 /* KVM was interrupted by a signal, restart it in the next
938 * tick. */
939 return 0;
940
941 case KVM_EXIT_INTERNAL_ERROR:
942 panic("KVM: Internal error (suberror: %u)\n",
943 _kvmRun->internal.suberror);
944
945 default:
946 dump();
947 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason);
948 }
949}
950
951Tick
952BaseKvmCPU::handleKvmExitIO()
953{
954 panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
955 _kvmRun->io.direction, _kvmRun->io.size,
956 _kvmRun->io.port, _kvmRun->io.count);
957}
958
959Tick
960BaseKvmCPU::handleKvmExitHypercall()
961{
962 panic("KVM: Unhandled hypercall\n");
963}
964
965Tick
966BaseKvmCPU::handleKvmExitIRQWindowOpen()
967{
968 warn("KVM: Unhandled IRQ window.\n");
969 return 0;
970}
971
972
973Tick
974BaseKvmCPU::handleKvmExitUnknown()
975{
976 dump();
977 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
978 _kvmRun->hw.hardware_exit_reason);
979}
980
981Tick
982BaseKvmCPU::handleKvmExitException()
983{
984 dump();
985 panic("KVM: Got exception when starting vCPU "
986 "(exception: %u, error_code: %u)\n",
987 _kvmRun->ex.exception, _kvmRun->ex.error_code);
988}
989
990Tick
991BaseKvmCPU::handleKvmExitFailEntry()
992{
993 dump();
994 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
995 _kvmRun->fail_entry.hardware_entry_failure_reason);
996}
997
998Tick
999BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write)
1000{
1001 ThreadContext *tc(thread->getTC());
1002 syncThreadContext();
1003
1004 Request mmio_req(paddr, size, Request::UNCACHEABLE, dataMasterId());
1005 mmio_req.setThreadContext(tc->contextId(), 0);
1006 // Some architectures do need to massage physical addresses a bit
1007 // before they are inserted into the memory system. This enables
1008 // APIC accesses on x86 and m5ops where supported through a MMIO
1009 // interface.
1010 BaseTLB::Mode tlb_mode(write ? BaseTLB::Write : BaseTLB::Read);
1011 Fault fault(tc->getDTBPtr()->finalizePhysical(&mmio_req, tc, tlb_mode));
1012 if (fault != NoFault)
1013 warn("Finalization of MMIO address failed: %s\n", fault->name());
1014
1015
1016 const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq);
1017 Packet pkt(&mmio_req, cmd);
1018 pkt.dataStatic(data);
1019
1020 if (mmio_req.isMmappedIpr()) {
1021 // We currently assume that there is no need to migrate to a
1022 // different event queue when doing IPRs. Currently, IPRs are
1023 // only used for m5ops, so it should be a valid assumption.
1024 const Cycles ipr_delay(write ?
1025 TheISA::handleIprWrite(tc, &pkt) :
1026 TheISA::handleIprRead(tc, &pkt));
1027 threadContextDirty = true;
1028 return clockPeriod() * ipr_delay;
1029 } else {
1030 // Temporarily lock and migrate to the event queue of the
1031 // VM. This queue is assumed to "own" all devices we need to
1032 // access if running in multi-core mode.
1033 EventQueue::ScopedMigration migrate(vm.eventQueue());
1034
1035 return dataPort.sendAtomic(&pkt);
1036 }
1037}
1038
1039void
1040BaseKvmCPU::setSignalMask(const sigset_t *mask)
1041{
1042 std::unique_ptr<struct kvm_signal_mask> kvm_mask;
1043
1044 if (mask) {
1045 kvm_mask.reset((struct kvm_signal_mask *)operator new(
1046 sizeof(struct kvm_signal_mask) + sizeof(*mask)));
1047 // The kernel and the user-space headers have different ideas
1048 // about the size of sigset_t. This seems like a massive hack,
1049 // but is actually what qemu does.
1050 assert(sizeof(*mask) >= 8);
1051 kvm_mask->len = 8;
1052 memcpy(kvm_mask->sigset, mask, kvm_mask->len);
1053 }
1054
1055 if (ioctl(KVM_SET_SIGNAL_MASK, (void *)kvm_mask.get()) == -1)
1056 panic("KVM: Failed to set vCPU signal mask (errno: %i)\n",
1057 errno);
1058}
1059
1060int
1061BaseKvmCPU::ioctl(int request, long p1) const
1062{
1063 if (vcpuFD == -1)
1064 panic("KVM: CPU ioctl called before initialization\n");
1065
1066 return ::ioctl(vcpuFD, request, p1);
1067}
1068
1069Tick
1070BaseKvmCPU::flushCoalescedMMIO()
1071{
1072 if (!mmioRing)
1073 return 0;
1074
1075 DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n");
1076
1077 // TODO: We might need to do synchronization when we start to
1078 // support multiple CPUs
1079 Tick ticks(0);
1080 while (mmioRing->first != mmioRing->last) {
1081 struct kvm_coalesced_mmio &ent(
1082 mmioRing->coalesced_mmio[mmioRing->first]);
1083
1084 DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
1085 ent.phys_addr, ent.len);
1086
1087 ++numCoalescedMMIO;
1088 ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true);
1089
1090 mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX;
1091 }
1092
1093 return ticks;
1094}
1095
1096/**
1097 * Dummy handler for KVM kick signals.
1098 *
1099 * @note This function is usually not called since the kernel doesn't
1100 * seem to deliver signals when the signal is only unmasked when
1101 * running in KVM. This doesn't matter though since we are only
1102 * interested in getting KVM to exit, which happens as expected. See
1103 * setupSignalHandler() and kvmRun() for details about KVM signal
1104 * handling.
1105 */
1106static void
1107onKickSignal(int signo, siginfo_t *si, void *data)
1108{
1109}
1110
1111void
1112BaseKvmCPU::setupSignalHandler()
1113{
1114 struct sigaction sa;
1115
1116 memset(&sa, 0, sizeof(sa));
1117 sa.sa_sigaction = onKickSignal;
1118 sa.sa_flags = SA_SIGINFO | SA_RESTART;
1119 if (sigaction(KVM_KICK_SIGNAL, &sa, NULL) == -1)
1120 panic("KVM: Failed to setup vCPU timer signal handler\n");
1121
1122 sigset_t sigset;
1123 if (pthread_sigmask(SIG_BLOCK, NULL, &sigset) == -1)
1124 panic("KVM: Failed get signal mask\n");
1125
1126 // Request KVM to setup the same signal mask as we're currently
1127 // running with except for the KVM control signal. We'll sometimes
1128 // need to raise the KVM_KICK_SIGNAL to cause immediate exits from
1129 // KVM after servicing IO requests. See kvmRun().
1130 sigdelset(&sigset, KVM_KICK_SIGNAL);
1131 setSignalMask(&sigset);
1132
1133 // Mask our control signals so they aren't delivered unless we're
1134 // actually executing inside KVM.
1135 sigaddset(&sigset, KVM_KICK_SIGNAL);
1136 if (pthread_sigmask(SIG_SETMASK, &sigset, NULL) == -1)
1137 panic("KVM: Failed mask the KVM control signals\n");
1138}
1139
1140bool
1141BaseKvmCPU::discardPendingSignal(int signum) const
1142{
1143 int discardedSignal;
1144
1145 // Setting the timeout to zero causes sigtimedwait to return
1146 // immediately.
1147 struct timespec timeout;
1148 timeout.tv_sec = 0;
1149 timeout.tv_nsec = 0;
1150
1151 sigset_t sigset;
1152 sigemptyset(&sigset);
1153 sigaddset(&sigset, signum);
1154
1155 do {
1156 discardedSignal = sigtimedwait(&sigset, NULL, &timeout);
1157 } while (discardedSignal == -1 && errno == EINTR);
1158
1159 if (discardedSignal == signum)
1160 return true;
1161 else if (discardedSignal == -1 && errno == EAGAIN)
1162 return false;
1163 else
1164 panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n",
1165 discardedSignal, errno);
1166}
1167
1168void
1169BaseKvmCPU::setupCounters()
1170{
1171 DPRINTF(Kvm, "Attaching cycle counter...\n");
1172 PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE,
1173 PERF_COUNT_HW_CPU_CYCLES);
1174 cfgCycles.disabled(true)
1175 .pinned(true);
1176
1177 // Try to exclude the host. We set both exclude_hv and
1178 // exclude_host since different architectures use slightly
1179 // different APIs in the kernel.
1180 cfgCycles.exclude_hv(true)
1181 .exclude_host(true);
1182
1183 if (perfControlledByTimer) {
1184 // We need to configure the cycles counter to send overflows
1185 // since we are going to use it to trigger timer signals that
1186 // trap back into m5 from KVM. In practice, this means that we
1187 // need to set some non-zero sample period that gets
1188 // overridden when the timer is armed.
1189 cfgCycles.wakeupEvents(1)
1190 .samplePeriod(42);
1191 }
1192
1193 hwCycles.attach(cfgCycles,
1194 0); // TID (0 => currentThread)
1195
1196 setupInstCounter();
1197}
1198
1199bool
1200BaseKvmCPU::tryDrain()
1201{
1202 if (!drainManager)
1203 return false;
1204
1205 if (!archIsDrained()) {
1206 DPRINTF(Drain, "tryDrain: Architecture code is not ready.\n");
1207 return false;
1208 }
1209
1210 if (_status == Idle || _status == Running) {
1211 DPRINTF(Drain,
1212 "tryDrain: CPU transitioned into the Idle state, drain done\n");
1213 drainManager->signalDrainDone();
1214 drainManager = NULL;
1215 return true;
1216 } else {
1217 DPRINTF(Drain, "tryDrain: CPU not ready.\n");
1218 return false;
1219 }
1220}
1221
1222void
1223BaseKvmCPU::ioctlRun()
1224{
1225 if (ioctl(KVM_RUN) == -1) {
1226 if (errno != EINTR)
1227 panic("KVM: Failed to start virtual CPU (errno: %i)\n",
1228 errno);
1229 }
1230}
1231
1232void
1233BaseKvmCPU::setupInstStop()
1234{
1235 if (comInstEventQueue[0]->empty()) {
1236 setupInstCounter(0);
1237 } else {
1238 const uint64_t next(comInstEventQueue[0]->nextTick());
1239
1240 assert(next > ctrInsts);
1241 setupInstCounter(next - ctrInsts);
1242 }
1243}
1244
1245void
1246BaseKvmCPU::setupInstCounter(uint64_t period)
1247{
1248 // No need to do anything if we aren't attaching for the first
1249 // time or the period isn't changing.
1250 if (period == activeInstPeriod && hwInstructions.attached())
1251 return;
1252
1253 PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE,
1254 PERF_COUNT_HW_INSTRUCTIONS);
1255
1256 // Try to exclude the host. We set both exclude_hv and
1257 // exclude_host since different architectures use slightly
1258 // different APIs in the kernel.
1259 cfgInstructions.exclude_hv(true)
1260 .exclude_host(true);
1261
1262 if (period) {
1263 // Setup a sampling counter if that has been requested.
1264 cfgInstructions.wakeupEvents(1)
1265 .samplePeriod(period);
1266 }
1267
1268 // We need to detach and re-attach the counter to reliably change
1269 // sampling settings. See PerfKvmCounter::period() for details.
1270 if (hwInstructions.attached())
1271 hwInstructions.detach();
1272 assert(hwCycles.attached());
1273 hwInstructions.attach(cfgInstructions,
1274 0, // TID (0 => currentThread)
1275 hwCycles);
1276
1277 if (period)
1278 hwInstructions.enableSignals(KVM_KICK_SIGNAL);
1279
1280 activeInstPeriod = period;
1281}
514{
515 inform("State dumping not implemented.");
516}
517
518void
519BaseKvmCPU::tick()
520{
521 Tick delay(0);
522 assert(_status != Idle);
523
524 switch (_status) {
525 case RunningService:
526 // handleKvmExit() will determine the next state of the CPU
527 delay = handleKvmExit();
528
529 if (tryDrain())
530 _status = Idle;
531 break;
532
533 case RunningServiceCompletion:
534 case Running: {
535 const uint64_t nextInstEvent(
536 !comInstEventQueue[0]->empty() ?
537 comInstEventQueue[0]->nextTick() : UINT64_MAX);
538 // Enter into KVM and complete pending IO instructions if we
539 // have an instruction event pending.
540 const Tick ticksToExecute(
541 nextInstEvent > ctrInsts ?
542 curEventQueue()->nextTick() - curTick() : 0);
543
544 // We might need to update the KVM state.
545 syncKvmState();
546
547 // Setup any pending instruction count breakpoints using
548 // PerfEvent if we are going to execute more than just an IO
549 // completion.
550 if (ticksToExecute > 0)
551 setupInstStop();
552
553 DPRINTF(KvmRun, "Entering KVM...\n");
554 if (drainManager) {
555 // Force an immediate exit from KVM after completing
556 // pending operations. The architecture-specific code
557 // takes care to run until it is in a state where it can
558 // safely be drained.
559 delay = kvmRunDrain();
560 } else {
561 delay = kvmRun(ticksToExecute);
562 }
563
564 // The CPU might have been suspended before entering into
565 // KVM. Assume that the CPU was suspended /before/ entering
566 // into KVM and skip the exit handling.
567 if (_status == Idle)
568 break;
569
570 // Entering into KVM implies that we'll have to reload the thread
571 // context from KVM if we want to access it. Flag the KVM state as
572 // dirty with respect to the cached thread context.
573 kvmStateDirty = true;
574
575 // Enter into the RunningService state unless the
576 // simulation was stopped by a timer.
577 if (_kvmRun->exit_reason != KVM_EXIT_INTR) {
578 _status = RunningService;
579 } else {
580 ++numExitSignal;
581 _status = Running;
582 }
583
584 // Service any pending instruction events. The vCPU should
585 // have exited in time for the event using the instruction
586 // counter configured by setupInstStop().
587 comInstEventQueue[0]->serviceEvents(ctrInsts);
588 system->instEventQueue.serviceEvents(system->totalNumInsts);
589
590 if (tryDrain())
591 _status = Idle;
592 } break;
593
594 default:
595 panic("BaseKvmCPU entered tick() in an illegal state (%i)\n",
596 _status);
597 }
598
599 // Schedule a new tick if we are still running
600 if (_status != Idle)
601 schedule(tickEvent, clockEdge(ticksToCycles(delay)));
602}
603
604Tick
605BaseKvmCPU::kvmRunDrain()
606{
607 // By default, the only thing we need to drain is a pending IO
608 // operation which assumes that we are in the
609 // RunningServiceCompletion state.
610 assert(_status == RunningServiceCompletion);
611
612 // Deliver the data from the pending IO operation and immediately
613 // exit.
614 return kvmRun(0);
615}
616
617uint64_t
618BaseKvmCPU::getHostCycles() const
619{
620 return hwCycles.read();
621}
622
623Tick
624BaseKvmCPU::kvmRun(Tick ticks)
625{
626 Tick ticksExecuted;
627 DPRINTF(KvmRun, "KVM: Executing for %i ticks\n", ticks);
628
629 if (ticks == 0) {
630 // Settings ticks == 0 is a special case which causes an entry
631 // into KVM that finishes pending operations (e.g., IO) and
632 // then immediately exits.
633 DPRINTF(KvmRun, "KVM: Delivering IO without full guest entry\n");
634
635 ++numVMHalfEntries;
636
637 // Send a KVM_KICK_SIGNAL to the vCPU thread (i.e., this
638 // thread). The KVM control signal is masked while executing
639 // in gem5 and gets unmasked temporarily as when entering
640 // KVM. See setSignalMask() and setupSignalHandler().
641 kick();
642
643 // Start the vCPU. KVM will check for signals after completing
644 // pending operations (IO). Since the KVM_KICK_SIGNAL is
645 // pending, this forces an immediate exit to gem5 again. We
646 // don't bother to setup timers since this shouldn't actually
647 // execute any code (other than completing half-executed IO
648 // instructions) in the guest.
649 ioctlRun();
650
651 // We always execute at least one cycle to prevent the
652 // BaseKvmCPU::tick() to be rescheduled on the same tick
653 // twice.
654 ticksExecuted = clockPeriod();
655 } else {
656 // This method is executed as a result of a tick event. That
657 // means that the event queue will be locked when entering the
658 // method. We temporarily unlock the event queue to allow
659 // other threads to steal control of this thread to inject
660 // interrupts. They will typically lock the queue and then
661 // force an exit from KVM by kicking the vCPU.
662 EventQueue::ScopedRelease release(curEventQueue());
663
664 if (ticks < runTimer->resolution()) {
665 DPRINTF(KvmRun, "KVM: Adjusting tick count (%i -> %i)\n",
666 ticks, runTimer->resolution());
667 ticks = runTimer->resolution();
668 }
669
670 // Get hardware statistics after synchronizing contexts. The KVM
671 // state update might affect guest cycle counters.
672 uint64_t baseCycles(getHostCycles());
673 uint64_t baseInstrs(hwInstructions.read());
674
675 // Arm the run timer and start the cycle timer if it isn't
676 // controlled by the overflow timer. Starting/stopping the cycle
677 // timer automatically starts the other perf timers as they are in
678 // the same counter group.
679 runTimer->arm(ticks);
680 if (!perfControlledByTimer)
681 hwCycles.start();
682
683 ioctlRun();
684
685 runTimer->disarm();
686 if (!perfControlledByTimer)
687 hwCycles.stop();
688
689 // The control signal may have been delivered after we exited
690 // from KVM. It will be pending in that case since it is
691 // masked when we aren't executing in KVM. Discard it to make
692 // sure we don't deliver it immediately next time we try to
693 // enter into KVM.
694 discardPendingSignal(KVM_KICK_SIGNAL);
695
696 const uint64_t hostCyclesExecuted(getHostCycles() - baseCycles);
697 const uint64_t simCyclesExecuted(hostCyclesExecuted * hostFactor);
698 const uint64_t instsExecuted(hwInstructions.read() - baseInstrs);
699 ticksExecuted = runTimer->ticksFromHostCycles(hostCyclesExecuted);
700
701 /* Update statistics */
702 numCycles += simCyclesExecuted;;
703 numInsts += instsExecuted;
704 ctrInsts += instsExecuted;
705 system->totalNumInsts += instsExecuted;
706
707 DPRINTF(KvmRun,
708 "KVM: Executed %i instructions in %i cycles "
709 "(%i ticks, sim cycles: %i).\n",
710 instsExecuted, hostCyclesExecuted, ticksExecuted, simCyclesExecuted);
711 }
712
713 ++numVMExits;
714
715 return ticksExecuted + flushCoalescedMMIO();
716}
717
718void
719BaseKvmCPU::kvmNonMaskableInterrupt()
720{
721 ++numInterrupts;
722 if (ioctl(KVM_NMI) == -1)
723 panic("KVM: Failed to deliver NMI to virtual CPU\n");
724}
725
726void
727BaseKvmCPU::kvmInterrupt(const struct kvm_interrupt &interrupt)
728{
729 ++numInterrupts;
730 if (ioctl(KVM_INTERRUPT, (void *)&interrupt) == -1)
731 panic("KVM: Failed to deliver interrupt to virtual CPU\n");
732}
733
734void
735BaseKvmCPU::getRegisters(struct kvm_regs &regs) const
736{
737 if (ioctl(KVM_GET_REGS, &regs) == -1)
738 panic("KVM: Failed to get guest registers\n");
739}
740
741void
742BaseKvmCPU::setRegisters(const struct kvm_regs &regs)
743{
744 if (ioctl(KVM_SET_REGS, (void *)&regs) == -1)
745 panic("KVM: Failed to set guest registers\n");
746}
747
748void
749BaseKvmCPU::getSpecialRegisters(struct kvm_sregs &regs) const
750{
751 if (ioctl(KVM_GET_SREGS, &regs) == -1)
752 panic("KVM: Failed to get guest special registers\n");
753}
754
755void
756BaseKvmCPU::setSpecialRegisters(const struct kvm_sregs &regs)
757{
758 if (ioctl(KVM_SET_SREGS, (void *)&regs) == -1)
759 panic("KVM: Failed to set guest special registers\n");
760}
761
762void
763BaseKvmCPU::getFPUState(struct kvm_fpu &state) const
764{
765 if (ioctl(KVM_GET_FPU, &state) == -1)
766 panic("KVM: Failed to get guest FPU state\n");
767}
768
769void
770BaseKvmCPU::setFPUState(const struct kvm_fpu &state)
771{
772 if (ioctl(KVM_SET_FPU, (void *)&state) == -1)
773 panic("KVM: Failed to set guest FPU state\n");
774}
775
776
777void
778BaseKvmCPU::setOneReg(uint64_t id, const void *addr)
779{
780#ifdef KVM_SET_ONE_REG
781 struct kvm_one_reg reg;
782 reg.id = id;
783 reg.addr = (uint64_t)addr;
784
785 if (ioctl(KVM_SET_ONE_REG, &reg) == -1) {
786 panic("KVM: Failed to set register (0x%x) value (errno: %i)\n",
787 id, errno);
788 }
789#else
790 panic("KVM_SET_ONE_REG is unsupported on this platform.\n");
791#endif
792}
793
794void
795BaseKvmCPU::getOneReg(uint64_t id, void *addr) const
796{
797#ifdef KVM_GET_ONE_REG
798 struct kvm_one_reg reg;
799 reg.id = id;
800 reg.addr = (uint64_t)addr;
801
802 if (ioctl(KVM_GET_ONE_REG, &reg) == -1) {
803 panic("KVM: Failed to get register (0x%x) value (errno: %i)\n",
804 id, errno);
805 }
806#else
807 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
808#endif
809}
810
811std::string
812BaseKvmCPU::getAndFormatOneReg(uint64_t id) const
813{
814#ifdef KVM_GET_ONE_REG
815 std::ostringstream ss;
816
817 ss.setf(std::ios::hex, std::ios::basefield);
818 ss.setf(std::ios::showbase);
819#define HANDLE_INTTYPE(len) \
820 case KVM_REG_SIZE_U ## len: { \
821 uint ## len ## _t value; \
822 getOneReg(id, &value); \
823 ss << value; \
824 } break
825
826#define HANDLE_ARRAY(len) \
827 case KVM_REG_SIZE_U ## len: { \
828 uint8_t value[len / 8]; \
829 getOneReg(id, value); \
830 ccprintf(ss, "[0x%x", value[0]); \
831 for (int i = 1; i < len / 8; ++i) \
832 ccprintf(ss, ", 0x%x", value[i]); \
833 ccprintf(ss, "]"); \
834 } break
835
836 switch (id & KVM_REG_SIZE_MASK) {
837 HANDLE_INTTYPE(8);
838 HANDLE_INTTYPE(16);
839 HANDLE_INTTYPE(32);
840 HANDLE_INTTYPE(64);
841 HANDLE_ARRAY(128);
842 HANDLE_ARRAY(256);
843 HANDLE_ARRAY(512);
844 HANDLE_ARRAY(1024);
845 default:
846 ss << "??";
847 }
848
849#undef HANDLE_INTTYPE
850#undef HANDLE_ARRAY
851
852 return ss.str();
853#else
854 panic("KVM_GET_ONE_REG is unsupported on this platform.\n");
855#endif
856}
857
858void
859BaseKvmCPU::syncThreadContext()
860{
861 if (!kvmStateDirty)
862 return;
863
864 assert(!threadContextDirty);
865
866 updateThreadContext();
867 kvmStateDirty = false;
868}
869
870void
871BaseKvmCPU::syncKvmState()
872{
873 if (!threadContextDirty)
874 return;
875
876 assert(!kvmStateDirty);
877
878 updateKvmState();
879 threadContextDirty = false;
880}
881
882Tick
883BaseKvmCPU::handleKvmExit()
884{
885 DPRINTF(KvmRun, "handleKvmExit (exit_reason: %i)\n", _kvmRun->exit_reason);
886 assert(_status == RunningService);
887
888 // Switch into the running state by default. Individual handlers
889 // can override this.
890 _status = Running;
891 switch (_kvmRun->exit_reason) {
892 case KVM_EXIT_UNKNOWN:
893 return handleKvmExitUnknown();
894
895 case KVM_EXIT_EXCEPTION:
896 return handleKvmExitException();
897
898 case KVM_EXIT_IO:
899 _status = RunningServiceCompletion;
900 ++numIO;
901 return handleKvmExitIO();
902
903 case KVM_EXIT_HYPERCALL:
904 ++numHypercalls;
905 return handleKvmExitHypercall();
906
907 case KVM_EXIT_HLT:
908 /* The guest has halted and is waiting for interrupts */
909 DPRINTF(Kvm, "handleKvmExitHalt\n");
910 ++numHalt;
911
912 // Suspend the thread until the next interrupt arrives
913 thread->suspend();
914
915 // This is actually ignored since the thread is suspended.
916 return 0;
917
918 case KVM_EXIT_MMIO:
919 _status = RunningServiceCompletion;
920 /* Service memory mapped IO requests */
921 DPRINTF(KvmIO, "KVM: Handling MMIO (w: %u, addr: 0x%x, len: %u)\n",
922 _kvmRun->mmio.is_write,
923 _kvmRun->mmio.phys_addr, _kvmRun->mmio.len);
924
925 ++numMMIO;
926 return doMMIOAccess(_kvmRun->mmio.phys_addr, _kvmRun->mmio.data,
927 _kvmRun->mmio.len, _kvmRun->mmio.is_write);
928
929 case KVM_EXIT_IRQ_WINDOW_OPEN:
930 return handleKvmExitIRQWindowOpen();
931
932 case KVM_EXIT_FAIL_ENTRY:
933 return handleKvmExitFailEntry();
934
935 case KVM_EXIT_INTR:
936 /* KVM was interrupted by a signal, restart it in the next
937 * tick. */
938 return 0;
939
940 case KVM_EXIT_INTERNAL_ERROR:
941 panic("KVM: Internal error (suberror: %u)\n",
942 _kvmRun->internal.suberror);
943
944 default:
945 dump();
946 panic("KVM: Unexpected exit (exit_reason: %u)\n", _kvmRun->exit_reason);
947 }
948}
949
950Tick
951BaseKvmCPU::handleKvmExitIO()
952{
953 panic("KVM: Unhandled guest IO (dir: %i, size: %i, port: 0x%x, count: %i)\n",
954 _kvmRun->io.direction, _kvmRun->io.size,
955 _kvmRun->io.port, _kvmRun->io.count);
956}
957
958Tick
959BaseKvmCPU::handleKvmExitHypercall()
960{
961 panic("KVM: Unhandled hypercall\n");
962}
963
964Tick
965BaseKvmCPU::handleKvmExitIRQWindowOpen()
966{
967 warn("KVM: Unhandled IRQ window.\n");
968 return 0;
969}
970
971
972Tick
973BaseKvmCPU::handleKvmExitUnknown()
974{
975 dump();
976 panic("KVM: Unknown error when starting vCPU (hw reason: 0x%llx)\n",
977 _kvmRun->hw.hardware_exit_reason);
978}
979
980Tick
981BaseKvmCPU::handleKvmExitException()
982{
983 dump();
984 panic("KVM: Got exception when starting vCPU "
985 "(exception: %u, error_code: %u)\n",
986 _kvmRun->ex.exception, _kvmRun->ex.error_code);
987}
988
989Tick
990BaseKvmCPU::handleKvmExitFailEntry()
991{
992 dump();
993 panic("KVM: Failed to enter virtualized mode (hw reason: 0x%llx)\n",
994 _kvmRun->fail_entry.hardware_entry_failure_reason);
995}
996
997Tick
998BaseKvmCPU::doMMIOAccess(Addr paddr, void *data, int size, bool write)
999{
1000 ThreadContext *tc(thread->getTC());
1001 syncThreadContext();
1002
1003 Request mmio_req(paddr, size, Request::UNCACHEABLE, dataMasterId());
1004 mmio_req.setThreadContext(tc->contextId(), 0);
1005 // Some architectures do need to massage physical addresses a bit
1006 // before they are inserted into the memory system. This enables
1007 // APIC accesses on x86 and m5ops where supported through a MMIO
1008 // interface.
1009 BaseTLB::Mode tlb_mode(write ? BaseTLB::Write : BaseTLB::Read);
1010 Fault fault(tc->getDTBPtr()->finalizePhysical(&mmio_req, tc, tlb_mode));
1011 if (fault != NoFault)
1012 warn("Finalization of MMIO address failed: %s\n", fault->name());
1013
1014
1015 const MemCmd cmd(write ? MemCmd::WriteReq : MemCmd::ReadReq);
1016 Packet pkt(&mmio_req, cmd);
1017 pkt.dataStatic(data);
1018
1019 if (mmio_req.isMmappedIpr()) {
1020 // We currently assume that there is no need to migrate to a
1021 // different event queue when doing IPRs. Currently, IPRs are
1022 // only used for m5ops, so it should be a valid assumption.
1023 const Cycles ipr_delay(write ?
1024 TheISA::handleIprWrite(tc, &pkt) :
1025 TheISA::handleIprRead(tc, &pkt));
1026 threadContextDirty = true;
1027 return clockPeriod() * ipr_delay;
1028 } else {
1029 // Temporarily lock and migrate to the event queue of the
1030 // VM. This queue is assumed to "own" all devices we need to
1031 // access if running in multi-core mode.
1032 EventQueue::ScopedMigration migrate(vm.eventQueue());
1033
1034 return dataPort.sendAtomic(&pkt);
1035 }
1036}
1037
1038void
1039BaseKvmCPU::setSignalMask(const sigset_t *mask)
1040{
1041 std::unique_ptr<struct kvm_signal_mask> kvm_mask;
1042
1043 if (mask) {
1044 kvm_mask.reset((struct kvm_signal_mask *)operator new(
1045 sizeof(struct kvm_signal_mask) + sizeof(*mask)));
1046 // The kernel and the user-space headers have different ideas
1047 // about the size of sigset_t. This seems like a massive hack,
1048 // but is actually what qemu does.
1049 assert(sizeof(*mask) >= 8);
1050 kvm_mask->len = 8;
1051 memcpy(kvm_mask->sigset, mask, kvm_mask->len);
1052 }
1053
1054 if (ioctl(KVM_SET_SIGNAL_MASK, (void *)kvm_mask.get()) == -1)
1055 panic("KVM: Failed to set vCPU signal mask (errno: %i)\n",
1056 errno);
1057}
1058
1059int
1060BaseKvmCPU::ioctl(int request, long p1) const
1061{
1062 if (vcpuFD == -1)
1063 panic("KVM: CPU ioctl called before initialization\n");
1064
1065 return ::ioctl(vcpuFD, request, p1);
1066}
1067
1068Tick
1069BaseKvmCPU::flushCoalescedMMIO()
1070{
1071 if (!mmioRing)
1072 return 0;
1073
1074 DPRINTF(KvmIO, "KVM: Flushing the coalesced MMIO ring buffer\n");
1075
1076 // TODO: We might need to do synchronization when we start to
1077 // support multiple CPUs
1078 Tick ticks(0);
1079 while (mmioRing->first != mmioRing->last) {
1080 struct kvm_coalesced_mmio &ent(
1081 mmioRing->coalesced_mmio[mmioRing->first]);
1082
1083 DPRINTF(KvmIO, "KVM: Handling coalesced MMIO (addr: 0x%x, len: %u)\n",
1084 ent.phys_addr, ent.len);
1085
1086 ++numCoalescedMMIO;
1087 ticks += doMMIOAccess(ent.phys_addr, ent.data, ent.len, true);
1088
1089 mmioRing->first = (mmioRing->first + 1) % KVM_COALESCED_MMIO_MAX;
1090 }
1091
1092 return ticks;
1093}
1094
1095/**
1096 * Dummy handler for KVM kick signals.
1097 *
1098 * @note This function is usually not called since the kernel doesn't
1099 * seem to deliver signals when the signal is only unmasked when
1100 * running in KVM. This doesn't matter though since we are only
1101 * interested in getting KVM to exit, which happens as expected. See
1102 * setupSignalHandler() and kvmRun() for details about KVM signal
1103 * handling.
1104 */
1105static void
1106onKickSignal(int signo, siginfo_t *si, void *data)
1107{
1108}
1109
1110void
1111BaseKvmCPU::setupSignalHandler()
1112{
1113 struct sigaction sa;
1114
1115 memset(&sa, 0, sizeof(sa));
1116 sa.sa_sigaction = onKickSignal;
1117 sa.sa_flags = SA_SIGINFO | SA_RESTART;
1118 if (sigaction(KVM_KICK_SIGNAL, &sa, NULL) == -1)
1119 panic("KVM: Failed to setup vCPU timer signal handler\n");
1120
1121 sigset_t sigset;
1122 if (pthread_sigmask(SIG_BLOCK, NULL, &sigset) == -1)
1123 panic("KVM: Failed get signal mask\n");
1124
1125 // Request KVM to setup the same signal mask as we're currently
1126 // running with except for the KVM control signal. We'll sometimes
1127 // need to raise the KVM_KICK_SIGNAL to cause immediate exits from
1128 // KVM after servicing IO requests. See kvmRun().
1129 sigdelset(&sigset, KVM_KICK_SIGNAL);
1130 setSignalMask(&sigset);
1131
1132 // Mask our control signals so they aren't delivered unless we're
1133 // actually executing inside KVM.
1134 sigaddset(&sigset, KVM_KICK_SIGNAL);
1135 if (pthread_sigmask(SIG_SETMASK, &sigset, NULL) == -1)
1136 panic("KVM: Failed mask the KVM control signals\n");
1137}
1138
1139bool
1140BaseKvmCPU::discardPendingSignal(int signum) const
1141{
1142 int discardedSignal;
1143
1144 // Setting the timeout to zero causes sigtimedwait to return
1145 // immediately.
1146 struct timespec timeout;
1147 timeout.tv_sec = 0;
1148 timeout.tv_nsec = 0;
1149
1150 sigset_t sigset;
1151 sigemptyset(&sigset);
1152 sigaddset(&sigset, signum);
1153
1154 do {
1155 discardedSignal = sigtimedwait(&sigset, NULL, &timeout);
1156 } while (discardedSignal == -1 && errno == EINTR);
1157
1158 if (discardedSignal == signum)
1159 return true;
1160 else if (discardedSignal == -1 && errno == EAGAIN)
1161 return false;
1162 else
1163 panic("Unexpected return value from sigtimedwait: %i (errno: %i)\n",
1164 discardedSignal, errno);
1165}
1166
1167void
1168BaseKvmCPU::setupCounters()
1169{
1170 DPRINTF(Kvm, "Attaching cycle counter...\n");
1171 PerfKvmCounterConfig cfgCycles(PERF_TYPE_HARDWARE,
1172 PERF_COUNT_HW_CPU_CYCLES);
1173 cfgCycles.disabled(true)
1174 .pinned(true);
1175
1176 // Try to exclude the host. We set both exclude_hv and
1177 // exclude_host since different architectures use slightly
1178 // different APIs in the kernel.
1179 cfgCycles.exclude_hv(true)
1180 .exclude_host(true);
1181
1182 if (perfControlledByTimer) {
1183 // We need to configure the cycles counter to send overflows
1184 // since we are going to use it to trigger timer signals that
1185 // trap back into m5 from KVM. In practice, this means that we
1186 // need to set some non-zero sample period that gets
1187 // overridden when the timer is armed.
1188 cfgCycles.wakeupEvents(1)
1189 .samplePeriod(42);
1190 }
1191
1192 hwCycles.attach(cfgCycles,
1193 0); // TID (0 => currentThread)
1194
1195 setupInstCounter();
1196}
1197
1198bool
1199BaseKvmCPU::tryDrain()
1200{
1201 if (!drainManager)
1202 return false;
1203
1204 if (!archIsDrained()) {
1205 DPRINTF(Drain, "tryDrain: Architecture code is not ready.\n");
1206 return false;
1207 }
1208
1209 if (_status == Idle || _status == Running) {
1210 DPRINTF(Drain,
1211 "tryDrain: CPU transitioned into the Idle state, drain done\n");
1212 drainManager->signalDrainDone();
1213 drainManager = NULL;
1214 return true;
1215 } else {
1216 DPRINTF(Drain, "tryDrain: CPU not ready.\n");
1217 return false;
1218 }
1219}
1220
1221void
1222BaseKvmCPU::ioctlRun()
1223{
1224 if (ioctl(KVM_RUN) == -1) {
1225 if (errno != EINTR)
1226 panic("KVM: Failed to start virtual CPU (errno: %i)\n",
1227 errno);
1228 }
1229}
1230
1231void
1232BaseKvmCPU::setupInstStop()
1233{
1234 if (comInstEventQueue[0]->empty()) {
1235 setupInstCounter(0);
1236 } else {
1237 const uint64_t next(comInstEventQueue[0]->nextTick());
1238
1239 assert(next > ctrInsts);
1240 setupInstCounter(next - ctrInsts);
1241 }
1242}
1243
1244void
1245BaseKvmCPU::setupInstCounter(uint64_t period)
1246{
1247 // No need to do anything if we aren't attaching for the first
1248 // time or the period isn't changing.
1249 if (period == activeInstPeriod && hwInstructions.attached())
1250 return;
1251
1252 PerfKvmCounterConfig cfgInstructions(PERF_TYPE_HARDWARE,
1253 PERF_COUNT_HW_INSTRUCTIONS);
1254
1255 // Try to exclude the host. We set both exclude_hv and
1256 // exclude_host since different architectures use slightly
1257 // different APIs in the kernel.
1258 cfgInstructions.exclude_hv(true)
1259 .exclude_host(true);
1260
1261 if (period) {
1262 // Setup a sampling counter if that has been requested.
1263 cfgInstructions.wakeupEvents(1)
1264 .samplePeriod(period);
1265 }
1266
1267 // We need to detach and re-attach the counter to reliably change
1268 // sampling settings. See PerfKvmCounter::period() for details.
1269 if (hwInstructions.attached())
1270 hwInstructions.detach();
1271 assert(hwCycles.attached());
1272 hwInstructions.attach(cfgInstructions,
1273 0, // TID (0 => currentThread)
1274 hwCycles);
1275
1276 if (period)
1277 hwInstructions.enableSignals(KVM_KICK_SIGNAL);
1278
1279 activeInstPeriod = period;
1280}