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