2 * Copyright (c) 2012 ARM Limited
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.
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.
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.
37 * Authors: Andreas Sandberg
40 #ifndef __CPU_KVM_BASE_HH__
41 #define __CPU_KVM_BASE_HH__
45 #include "base/statistics.hh"
46 #include "cpu/kvm/perfevent.hh"
47 #include "cpu/kvm/timer.hh"
48 #include "cpu/kvm/vm.hh"
49 #include "cpu/base.hh"
50 #include "cpu/simple_thread.hh"
52 /** Signal to use to trigger time-based exits from KVM */
53 #define KVM_TIMER_SIGNAL SIGRTMIN
55 // forward declarations
57 struct BaseKvmCPUParams;
60 * Base class for KVM based CPU models
62 * All architecture specific KVM implementation should inherit from
63 * this class. The most basic CPU models only need to override the
64 * updateKvmState() and updateThreadContext() methods to implement
65 * state synchronization between gem5 and KVM.
67 * The architecture specific implementation is also responsible for
68 * delivering interrupts into the VM. This is typically done by
69 * overriding tick() and checking the thread context before entering
70 * into the VM. In order to deliver an interrupt, the implementation
71 * then calls KvmVM::setIRQLine() or BaseKvmCPU::kvmInterrupt()
72 * depending on the specifics of the underlying hardware/drivers.
74 class BaseKvmCPU : public BaseCPU
77 BaseKvmCPU(BaseKvmCPUParams *params);
78 virtual ~BaseKvmCPU();
84 void serializeThread(std::ostream &os, ThreadID tid);
85 void unserializeThread(Checkpoint *cp, const std::string §ion,
88 unsigned int drain(DrainManager *dm);
92 void takeOverFrom(BaseCPU *cpu);
94 void verifyMemoryMode() const;
96 CpuPort &getDataPort() { return dataPort; }
97 CpuPort &getInstPort() { return instPort; }
100 void activateContext(ThreadID thread_num, Cycles delay);
101 void suspendContext(ThreadID thread_num);
102 void deallocateContext(ThreadID thread_num);
103 void haltContext(ThreadID thread_num);
105 ThreadContext *getContext(int tn);
107 Counter totalInsts() const;
108 Counter totalOps() const;
110 /** Dump the internal state to the terminal. */
114 * A cached copy of a thread's state in the form of a SimpleThread
117 * Normally the actual thread state is stored in the KVM vCPU. If KVM has
118 * been running this copy is will be out of date. If we recently handled
119 * some events within gem5 that required state to be updated this could be
120 * the most up-to-date copy. When getContext() or updateThreadContext() is
121 * called this copy gets updated. The method syncThreadContext can
122 * be used within a KVM CPU to update the thread context if the
123 * KVM state is dirty (i.e., the vCPU has been run since the last
126 SimpleThread *thread;
128 /** ThreadContext object, provides an interface for external
129 * objects to modify this thread's state.
137 /** Context not scheduled in KVM */
139 /** Running normally */
147 * Execute the CPU until the next event in the main event queue or
148 * until the guest needs service from gem5.
150 * @note This method is virtual in order to allow implementations
151 * to check for architecture specific events (e.g., interrupts)
152 * before entering the VM.
157 * Request KVM to run the guest for a given number of ticks. The
158 * method returns the approximate number of ticks executed.
160 * @note The returned number of ticks can be both larger or
161 * smaller than the requested number of ticks. A smaller number
162 * can, for example, occur when the guest executes MMIO. A larger
163 * number is typically due to performance counter inaccuracies.
165 * @param ticks Number of ticks to execute
166 * @return Number of ticks executed (see note)
168 Tick kvmRun(Tick ticks);
171 * Get a pointer to the kvm_run structure containing all the input
172 * and output parameters from kvmRun().
174 struct kvm_run *getKvmRunState() { return _kvmRun; };
177 * Retrieve a pointer to guest data stored at the end of the
178 * kvm_run structure. This is mainly used for PIO operations
181 * @param offset Offset as specified by the kvm_run structure
182 * @return Pointer to guest data
184 uint8_t *getGuestData(uint64_t offset) const {
185 return (uint8_t *)_kvmRun + offset;
189 * @addtogroup KvmInterrupts
193 * Send a non-maskable interrupt to the guest
195 * @note The presence of this call depends on Kvm::capUserNMI().
197 void kvmNonMaskableInterrupt();
200 * Send a normal interrupt to the guest
202 * @note Make sure that ready_for_interrupt_injection in kvm_run
203 * is set prior to calling this function. If not, an interrupt
204 * window must be requested by setting request_interrupt_window in
205 * kvm_run to 1 and restarting the guest.
207 * @param interrupt Structure describing the interrupt to send
209 void kvmInterrupt(const struct kvm_interrupt &interrupt);
215 * Get/Set the register state of the guest vCPU
217 * KVM has two different interfaces for accessing the state of the
218 * guest CPU. One interface updates 'normal' registers and one
219 * updates 'special' registers. The distinction between special
220 * and normal registers isn't very clear and is architecture
223 void getRegisters(struct kvm_regs ®s) const;
224 void setRegisters(const struct kvm_regs ®s);
225 void getSpecialRegisters(struct kvm_sregs ®s) const;
226 void setSpecialRegisters(const struct kvm_sregs ®s);
231 * Get/Set the guest FPU/vector state
233 void getFPUState(struct kvm_fpu &state) const;
234 void setFPUState(const struct kvm_fpu &state);
239 * Get/Set single register using the KVM_(SET|GET)_ONE_REG API.
241 * @note The presence of this call depends on Kvm::capOneReg().
243 void setOneReg(uint64_t id, const void *addr);
244 void setOneReg(uint64_t id, uint64_t value) { setOneReg(id, &value); }
245 void setOneReg(uint64_t id, uint32_t value) { setOneReg(id, &value); }
246 void getOneReg(uint64_t id, void *addr) const;
247 uint64_t getOneRegU64(uint64_t id) const {
249 getOneReg(id, &value);
252 uint32_t getOneRegU32(uint64_t id) const {
254 getOneReg(id, &value);
260 * Get and format one register for printout.
262 * This function call getOneReg() to retrieve the contents of one
263 * register and automatically formats it for printing.
265 * @note The presence of this call depends on Kvm::capOneReg().
267 std::string getAndFormatOneReg(uint64_t id) const;
271 * Update the KVM state from the current thread context
273 * The base CPU calls this method before starting the guest CPU
274 * when the contextDirty flag is set. The architecture dependent
275 * CPU implementation is expected to update all guest state
276 * (registers, special registers, and FPU state).
278 virtual void updateKvmState() = 0;
281 * Update the current thread context with the KVM state
283 * The base CPU after the guest updates any of the KVM state. In
284 * practice, this happens after kvmRun is called. The architecture
285 * dependent code is expected to read the state of the guest CPU
286 * and update gem5's thread state.
288 virtual void updateThreadContext() = 0;
291 * Update a thread context if the KVM state is dirty with respect
292 * to the cached thread context.
294 void syncThreadContext();
297 * Update the KVM if the thread context is dirty.
304 * Main kvmRun exit handler, calls the relevant handleKvmExit*
305 * depending on exit type.
307 * @return Number of ticks spent servicing the exit request
309 virtual Tick handleKvmExit();
312 * The guest performed a legacy IO request (out/inp on x86)
314 * @return Number of ticks spent servicing the IO request
316 virtual Tick handleKvmExitIO();
319 * The guest requested a monitor service using a hypercall
321 * @return Number of ticks spent servicing the hypercall
323 virtual Tick handleKvmExitHypercall();
326 * The guest exited because an interrupt window was requested
328 * The guest exited because an interrupt window was requested
329 * (request_interrupt_window in the kvm_run structure was set to 1
330 * before calling kvmRun) and it is now ready to receive
332 * @return Number of ticks spent servicing the IRQ
334 virtual Tick handleKvmExitIRQWindowOpen();
337 * An unknown architecture dependent error occurred when starting
340 * The kvm_run data structure contains the hardware error
341 * code. The defaults behavior of this method just prints the HW
342 * error code and panics. Architecture dependent implementations
343 * may want to override this method to provide better,
344 * hardware-aware, error messages.
346 * @return Number of ticks delay the next CPU tick
348 virtual Tick handleKvmExitUnknown();
351 * An unhandled virtualization exception occured
353 * Some KVM virtualization drivers return unhandled exceptions to
354 * the user-space monitor. This interface is currently only used
355 * by the Intel VMX KVM driver.
357 * @return Number of ticks delay the next CPU tick
359 virtual Tick handleKvmExitException();
362 * KVM failed to start the virtualized CPU
364 * The kvm_run data structure contains the hardware-specific error
367 * @return Number of ticks delay the next CPU tick
369 virtual Tick handleKvmExitFailEntry();
373 * Inject a memory mapped IO request into gem5
375 * @param paddr Physical address
376 * @param data Pointer to the source/destination buffer
377 * @param size Memory access size
378 * @param write True if write, False if read
379 * @return Number of ticks spent servicing the memory access
381 Tick doMMIOAccess(Addr paddr, void *data, int size, bool write);
385 * @addtogroup KvmIoctl
389 * vCPU ioctl interface.
391 * @param request KVM vCPU request
392 * @param p1 Optional request parameter
394 * @return -1 on error (error number in errno), ioctl dependent
397 int ioctl(int request, long p1) const;
398 int ioctl(int request, void *p1) const {
399 return ioctl(request, (long)p1);
401 int ioctl(int request) const {
402 return ioctl(request, 0L);
406 /** Port for data requests */
409 /** Unused dummy port for the instruction interface */
412 /** Pre-allocated MMIO memory request */
416 * Is the gem5 context dirty? Set to true to force an update of
417 * the KVM vCPU state upon the next call to kvmRun().
419 bool threadContextDirty;
422 * Is the KVM state dirty? Set to true to force an update of
423 * the KVM vCPU state upon the next call to kvmRun().
427 /** KVM internal ID of the vCPU */
431 struct TickEvent : public Event
435 TickEvent(BaseKvmCPU &c)
436 : Event(CPU_Tick_Pri), cpu(c) {}
438 void process() { cpu.tick(); }
440 const char *description() const {
441 return "BaseKvmCPU tick";
446 * Service MMIO requests in the mmioRing.
449 * @return Number of ticks spent servicing the MMIO requests in
450 * the MMIO ring buffer
452 Tick flushCoalescedMMIO();
455 * Setup a signal handler to catch the timer signal used to
456 * switch back to the monitor.
458 void setupSignalHandler();
460 /** Setup hardware performance counters */
461 void setupCounters();
463 /** KVM vCPU file descriptor */
465 /** Size of MMAPed kvm_run area */
468 * Pointer to the kvm_run structure used to communicate parameters
471 * @note This is the base pointer of the MMAPed KVM region. The
472 * first page contains the kvm_run structure. Subsequent pages may
473 * contain other data such as the MMIO ring buffer.
475 struct kvm_run *_kvmRun;
477 * Coalesced MMIO ring buffer. NULL if coalesced MMIO is not
480 struct kvm_coalesced_mmio_ring *mmioRing;
481 /** Cached page size of the host */
487 /** Guest performance counters */
488 PerfKvmCounter hwCycles;
489 PerfKvmCounter hwInstructions;
493 * Does the runTimer control the performance counters?
495 * The run timer will automatically enable and disable performance
496 * counters if a PerfEvent-based timer is used to control KVM
499 bool perfControlledByTimer;
502 * Timer used to force execution into the monitor after a
503 * specified number of simulation tick equivalents have executed
504 * in the guest. This counter generates the signal specified by
507 std::unique_ptr<BaseKvmTimer> runTimer;
513 Stats::Scalar numVMExits;
514 Stats::Scalar numMMIO;
515 Stats::Scalar numCoalescedMMIO;
517 Stats::Scalar numHalt;
518 Stats::Scalar numInterrupts;
519 Stats::Scalar numHypercalls;