base.hh (9752:a152d7f114b8) base.hh (9753:b9a742cdd75a)
1/*
2 * Copyright (c) 2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andreas Sandberg
38 */
39
40#ifndef __CPU_KVM_BASE_HH__
41#define __CPU_KVM_BASE_HH__
42
43#include <memory>
1/*
2 * Copyright (c) 2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andreas Sandberg
38 */
39
40#ifndef __CPU_KVM_BASE_HH__
41#define __CPU_KVM_BASE_HH__
42
43#include <memory>
44#include <csignal>
44
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"
51
52/** Signal to use to trigger time-based exits from KVM */
53#define KVM_TIMER_SIGNAL SIGRTMIN
54
55// forward declarations
56class ThreadContext;
57struct BaseKvmCPUParams;
58
59/**
60 * Base class for KVM based CPU models
61 *
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.
66 *
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.
73 */
74class BaseKvmCPU : public BaseCPU
75{
76 public:
77 BaseKvmCPU(BaseKvmCPUParams *params);
78 virtual ~BaseKvmCPU();
79
80 void init();
81 void startup();
82 void regStats();
83
84 void serializeThread(std::ostream &os, ThreadID tid);
85 void unserializeThread(Checkpoint *cp, const std::string &section,
86 ThreadID tid);
87
88 unsigned int drain(DrainManager *dm);
89 void drainResume();
90
91 void switchOut();
92 void takeOverFrom(BaseCPU *cpu);
93
94 void verifyMemoryMode() const;
95
96 MasterPort &getDataPort() { return dataPort; }
97 MasterPort &getInstPort() { return instPort; }
98
99 void wakeup();
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);
104
105 ThreadContext *getContext(int tn);
106
107 Counter totalInsts() const;
108 Counter totalOps() const;
109
110 /** Dump the internal state to the terminal. */
111 virtual void dump();
112
113 /**
114 * A cached copy of a thread's state in the form of a SimpleThread
115 * object.
116 *
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
124 * update).
125 */
126 SimpleThread *thread;
127
128 /** ThreadContext object, provides an interface for external
129 * objects to modify this thread's state.
130 */
131 ThreadContext *tc;
132
133 KvmVM &vm;
134
135 protected:
45
46#include "base/statistics.hh"
47#include "cpu/kvm/perfevent.hh"
48#include "cpu/kvm/timer.hh"
49#include "cpu/kvm/vm.hh"
50#include "cpu/base.hh"
51#include "cpu/simple_thread.hh"
52
53/** Signal to use to trigger time-based exits from KVM */
54#define KVM_TIMER_SIGNAL SIGRTMIN
55
56// forward declarations
57class ThreadContext;
58struct BaseKvmCPUParams;
59
60/**
61 * Base class for KVM based CPU models
62 *
63 * All architecture specific KVM implementation should inherit from
64 * this class. The most basic CPU models only need to override the
65 * updateKvmState() and updateThreadContext() methods to implement
66 * state synchronization between gem5 and KVM.
67 *
68 * The architecture specific implementation is also responsible for
69 * delivering interrupts into the VM. This is typically done by
70 * overriding tick() and checking the thread context before entering
71 * into the VM. In order to deliver an interrupt, the implementation
72 * then calls KvmVM::setIRQLine() or BaseKvmCPU::kvmInterrupt()
73 * depending on the specifics of the underlying hardware/drivers.
74 */
75class BaseKvmCPU : public BaseCPU
76{
77 public:
78 BaseKvmCPU(BaseKvmCPUParams *params);
79 virtual ~BaseKvmCPU();
80
81 void init();
82 void startup();
83 void regStats();
84
85 void serializeThread(std::ostream &os, ThreadID tid);
86 void unserializeThread(Checkpoint *cp, const std::string &section,
87 ThreadID tid);
88
89 unsigned int drain(DrainManager *dm);
90 void drainResume();
91
92 void switchOut();
93 void takeOverFrom(BaseCPU *cpu);
94
95 void verifyMemoryMode() const;
96
97 MasterPort &getDataPort() { return dataPort; }
98 MasterPort &getInstPort() { return instPort; }
99
100 void wakeup();
101 void activateContext(ThreadID thread_num, Cycles delay);
102 void suspendContext(ThreadID thread_num);
103 void deallocateContext(ThreadID thread_num);
104 void haltContext(ThreadID thread_num);
105
106 ThreadContext *getContext(int tn);
107
108 Counter totalInsts() const;
109 Counter totalOps() const;
110
111 /** Dump the internal state to the terminal. */
112 virtual void dump();
113
114 /**
115 * A cached copy of a thread's state in the form of a SimpleThread
116 * object.
117 *
118 * Normally the actual thread state is stored in the KVM vCPU. If KVM has
119 * been running this copy is will be out of date. If we recently handled
120 * some events within gem5 that required state to be updated this could be
121 * the most up-to-date copy. When getContext() or updateThreadContext() is
122 * called this copy gets updated. The method syncThreadContext can
123 * be used within a KVM CPU to update the thread context if the
124 * KVM state is dirty (i.e., the vCPU has been run since the last
125 * update).
126 */
127 SimpleThread *thread;
128
129 /** ThreadContext object, provides an interface for external
130 * objects to modify this thread's state.
131 */
132 ThreadContext *tc;
133
134 KvmVM &vm;
135
136 protected:
137 /**
138 *
139 * @dot
140 * digraph {
141 * Idle;
142 * Running;
143 * RunningService;
144 * RunningServiceCompletion;
145 *
146 * Idle -> Idle;
147 * Idle -> Running [label="activateContext()", URL="\ref activateContext"];
148 * Running -> Running [label="tick()", URL="\ref tick"];
149 * Running -> RunningService [label="tick()", URL="\ref tick"];
150 * Running -> Idle [label="suspendContext()", URL="\ref suspendContext"];
151 * Running -> Idle [label="drain()", URL="\ref drain"];
152 * Idle -> Running [label="drainResume()", URL="\ref drainResume"];
153 * RunningService -> RunningServiceCompletion [label="handleKvmExit()", URL="\ref handleKvmExit"];
154 * RunningServiceCompletion -> Running [label="tick()", URL="\ref tick"];
155 * RunningServiceCompletion -> RunningService [label="tick()", URL="\ref tick"];
156 * }
157 * @enddot
158 */
136 enum Status {
159 enum Status {
137 /** Context not scheduled in KVM */
160 /** Context not scheduled in KVM.
161 *
162 * The CPU generally enters this state when the guest execute
163 * an instruction that halts the CPU (e.g., WFI on ARM or HLT
164 * on X86) if KVM traps this instruction. Ticks are not
165 * scheduled in this state.
166 *
167 * @see suspendContext()
168 */
138 Idle,
169 Idle,
139 /** Running normally */
170 /** Running normally.
171 *
172 * This is the normal run state of the CPU. KVM will be
173 * entered next time tick() is called.
174 */
140 Running,
175 Running,
176 /** Requiring service at the beginning of the next cycle.
177 *
178 * The virtual machine has exited and requires service, tick()
179 * will call handleKvmExit() on the next cycle. The next state
180 * after running service is determined in handleKvmExit() and
181 * depends on what kind of service the guest requested:
182 * <ul>
183 * <li>IO/MMIO: RunningServiceCompletion
184 * <li>Halt: Idle
185 * <li>Others: Running
186 * </ul>
187 */
188 RunningService,
189 /** Service completion in progress.
190 *
191 * The VM has requested service that requires KVM to be
192 * entered once in order to get to a consistent state. This
193 * happens in handleKvmExit() or one of its friends after IO
194 * exits. After executing tick(), the CPU will transition into
195 * the Running or RunningService state.
196 */
197 RunningServiceCompletion,
141 };
142
143 /** CPU run state */
144 Status _status;
145
146 /**
147 * Execute the CPU until the next event in the main event queue or
148 * until the guest needs service from gem5.
198 };
199
200 /** CPU run state */
201 Status _status;
202
203 /**
204 * Execute the CPU until the next event in the main event queue or
205 * until the guest needs service from gem5.
149 *
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.
153 */
206 */
154 virtual void tick();
207 void tick();
155
156 /**
157 * Get the value of the hardware cycle counter in the guest.
158 *
159 * This method is supposed to return the total number of cycles
160 * executed in hardware mode relative to some arbitrary point in
161 * the past. It's mainly used when estimating the number of cycles
162 * actually executed by the CPU in kvmRun(). The default behavior
163 * of this method is to use the cycles performance counter, but
164 * some architectures may want to use internal registers instead.
165 *
166 * @return Number of host cycles executed relative to an undefined
167 * point in the past.
168 */
169 virtual uint64_t getHostCycles() const;
170
171 /**
172 * Request KVM to run the guest for a given number of ticks. The
173 * method returns the approximate number of ticks executed.
174 *
175 * @note The returned number of ticks can be both larger or
176 * smaller than the requested number of ticks. A smaller number
177 * can, for example, occur when the guest executes MMIO. A larger
178 * number is typically due to performance counter inaccuracies.
179 *
208
209 /**
210 * Get the value of the hardware cycle counter in the guest.
211 *
212 * This method is supposed to return the total number of cycles
213 * executed in hardware mode relative to some arbitrary point in
214 * the past. It's mainly used when estimating the number of cycles
215 * actually executed by the CPU in kvmRun(). The default behavior
216 * of this method is to use the cycles performance counter, but
217 * some architectures may want to use internal registers instead.
218 *
219 * @return Number of host cycles executed relative to an undefined
220 * point in the past.
221 */
222 virtual uint64_t getHostCycles() const;
223
224 /**
225 * Request KVM to run the guest for a given number of ticks. The
226 * method returns the approximate number of ticks executed.
227 *
228 * @note The returned number of ticks can be both larger or
229 * smaller than the requested number of ticks. A smaller number
230 * can, for example, occur when the guest executes MMIO. A larger
231 * number is typically due to performance counter inaccuracies.
232 *
180 * @param ticks Number of ticks to execute
233 * @note This method is virtual in order to allow implementations
234 * to check for architecture specific events (e.g., interrupts)
235 * before entering the VM.
236 *
237 * @note It is the response of the caller (normally tick()) to
238 * make sure that the KVM state is synchronized and that the TC is
239 * invalidated after entering KVM.
240 *
241 * @param ticks Number of ticks to execute, set to 0 to exit
242 * immediately after finishing pending operations.
181 * @return Number of ticks executed (see note)
182 */
243 * @return Number of ticks executed (see note)
244 */
183 Tick kvmRun(Tick ticks);
245 virtual Tick kvmRun(Tick ticks);
184
185 /**
246
247 /**
248 * Request the CPU to run until draining completes.
249 *
250 * This function normally calls kvmRun(0) to make KVM finish
251 * pending MMIO operations. Architecures implementing
252 * archIsDrained() must override this method.
253 *
254 * @see BaseKvmCPU::archIsDrained()
255 *
256 * @return Number of ticks executed
257 */
258 virtual Tick kvmRunDrain();
259
260 /**
186 * Get a pointer to the kvm_run structure containing all the input
187 * and output parameters from kvmRun().
188 */
189 struct kvm_run *getKvmRunState() { return _kvmRun; };
190
191 /**
192 * Retrieve a pointer to guest data stored at the end of the
193 * kvm_run structure. This is mainly used for PIO operations
194 * (KVM_EXIT_IO).
195 *
196 * @param offset Offset as specified by the kvm_run structure
197 * @return Pointer to guest data
198 */
199 uint8_t *getGuestData(uint64_t offset) const {
200 return (uint8_t *)_kvmRun + offset;
201 };
202
203 /**
204 * @addtogroup KvmInterrupts
205 * @{
206 */
207 /**
208 * Send a non-maskable interrupt to the guest
209 *
210 * @note The presence of this call depends on Kvm::capUserNMI().
211 */
212 void kvmNonMaskableInterrupt();
213
214 /**
215 * Send a normal interrupt to the guest
216 *
217 * @note Make sure that ready_for_interrupt_injection in kvm_run
218 * is set prior to calling this function. If not, an interrupt
219 * window must be requested by setting request_interrupt_window in
220 * kvm_run to 1 and restarting the guest.
221 *
222 * @param interrupt Structure describing the interrupt to send
223 */
224 void kvmInterrupt(const struct kvm_interrupt &interrupt);
225
226 /** @} */
227
228 /** @{ */
229 /**
230 * Get/Set the register state of the guest vCPU
231 *
232 * KVM has two different interfaces for accessing the state of the
233 * guest CPU. One interface updates 'normal' registers and one
234 * updates 'special' registers. The distinction between special
235 * and normal registers isn't very clear and is architecture
236 * dependent.
237 */
238 void getRegisters(struct kvm_regs &regs) const;
239 void setRegisters(const struct kvm_regs &regs);
240 void getSpecialRegisters(struct kvm_sregs &regs) const;
241 void setSpecialRegisters(const struct kvm_sregs &regs);
242 /** @} */
243
244 /** @{ */
245 /**
246 * Get/Set the guest FPU/vector state
247 */
248 void getFPUState(struct kvm_fpu &state) const;
249 void setFPUState(const struct kvm_fpu &state);
250 /** @} */
251
252 /** @{ */
253 /**
254 * Get/Set single register using the KVM_(SET|GET)_ONE_REG API.
255 *
256 * @note The presence of this call depends on Kvm::capOneReg().
257 */
258 void setOneReg(uint64_t id, const void *addr);
259 void setOneReg(uint64_t id, uint64_t value) { setOneReg(id, &value); }
260 void setOneReg(uint64_t id, uint32_t value) { setOneReg(id, &value); }
261 void getOneReg(uint64_t id, void *addr) const;
262 uint64_t getOneRegU64(uint64_t id) const {
263 uint64_t value;
264 getOneReg(id, &value);
265 return value;
266 }
267 uint32_t getOneRegU32(uint64_t id) const {
268 uint32_t value;
269 getOneReg(id, &value);
270 return value;
271 }
272 /** @} */
273
274 /**
275 * Get and format one register for printout.
276 *
277 * This function call getOneReg() to retrieve the contents of one
278 * register and automatically formats it for printing.
279 *
280 * @note The presence of this call depends on Kvm::capOneReg().
281 */
282 std::string getAndFormatOneReg(uint64_t id) const;
283
284 /** @{ */
285 /**
286 * Update the KVM state from the current thread context
287 *
288 * The base CPU calls this method before starting the guest CPU
289 * when the contextDirty flag is set. The architecture dependent
290 * CPU implementation is expected to update all guest state
291 * (registers, special registers, and FPU state).
292 */
293 virtual void updateKvmState() = 0;
294
295 /**
296 * Update the current thread context with the KVM state
297 *
298 * The base CPU after the guest updates any of the KVM state. In
299 * practice, this happens after kvmRun is called. The architecture
300 * dependent code is expected to read the state of the guest CPU
301 * and update gem5's thread state.
302 */
303 virtual void updateThreadContext() = 0;
304
305 /**
306 * Update a thread context if the KVM state is dirty with respect
307 * to the cached thread context.
308 */
309 void syncThreadContext();
310
311 /**
312 * Update the KVM if the thread context is dirty.
313 */
314 void syncKvmState();
315 /** @} */
316
317 /** @{ */
318 /**
319 * Main kvmRun exit handler, calls the relevant handleKvmExit*
320 * depending on exit type.
321 *
322 * @return Number of ticks spent servicing the exit request
323 */
324 virtual Tick handleKvmExit();
325
326 /**
327 * The guest performed a legacy IO request (out/inp on x86)
328 *
329 * @return Number of ticks spent servicing the IO request
330 */
331 virtual Tick handleKvmExitIO();
332
333 /**
334 * The guest requested a monitor service using a hypercall
335 *
336 * @return Number of ticks spent servicing the hypercall
337 */
338 virtual Tick handleKvmExitHypercall();
339
340 /**
341 * The guest exited because an interrupt window was requested
342 *
343 * The guest exited because an interrupt window was requested
344 * (request_interrupt_window in the kvm_run structure was set to 1
345 * before calling kvmRun) and it is now ready to receive
346 *
347 * @return Number of ticks spent servicing the IRQ
348 */
349 virtual Tick handleKvmExitIRQWindowOpen();
350
351 /**
352 * An unknown architecture dependent error occurred when starting
353 * the vCPU
354 *
355 * The kvm_run data structure contains the hardware error
356 * code. The defaults behavior of this method just prints the HW
357 * error code and panics. Architecture dependent implementations
358 * may want to override this method to provide better,
359 * hardware-aware, error messages.
360 *
361 * @return Number of ticks delay the next CPU tick
362 */
363 virtual Tick handleKvmExitUnknown();
364
365 /**
366 * An unhandled virtualization exception occured
367 *
368 * Some KVM virtualization drivers return unhandled exceptions to
369 * the user-space monitor. This interface is currently only used
370 * by the Intel VMX KVM driver.
371 *
372 * @return Number of ticks delay the next CPU tick
373 */
374 virtual Tick handleKvmExitException();
375
376 /**
377 * KVM failed to start the virtualized CPU
378 *
379 * The kvm_run data structure contains the hardware-specific error
380 * code.
381 *
382 * @return Number of ticks delay the next CPU tick
383 */
384 virtual Tick handleKvmExitFailEntry();
385 /** @} */
386
387 /**
261 * Get a pointer to the kvm_run structure containing all the input
262 * and output parameters from kvmRun().
263 */
264 struct kvm_run *getKvmRunState() { return _kvmRun; };
265
266 /**
267 * Retrieve a pointer to guest data stored at the end of the
268 * kvm_run structure. This is mainly used for PIO operations
269 * (KVM_EXIT_IO).
270 *
271 * @param offset Offset as specified by the kvm_run structure
272 * @return Pointer to guest data
273 */
274 uint8_t *getGuestData(uint64_t offset) const {
275 return (uint8_t *)_kvmRun + offset;
276 };
277
278 /**
279 * @addtogroup KvmInterrupts
280 * @{
281 */
282 /**
283 * Send a non-maskable interrupt to the guest
284 *
285 * @note The presence of this call depends on Kvm::capUserNMI().
286 */
287 void kvmNonMaskableInterrupt();
288
289 /**
290 * Send a normal interrupt to the guest
291 *
292 * @note Make sure that ready_for_interrupt_injection in kvm_run
293 * is set prior to calling this function. If not, an interrupt
294 * window must be requested by setting request_interrupt_window in
295 * kvm_run to 1 and restarting the guest.
296 *
297 * @param interrupt Structure describing the interrupt to send
298 */
299 void kvmInterrupt(const struct kvm_interrupt &interrupt);
300
301 /** @} */
302
303 /** @{ */
304 /**
305 * Get/Set the register state of the guest vCPU
306 *
307 * KVM has two different interfaces for accessing the state of the
308 * guest CPU. One interface updates 'normal' registers and one
309 * updates 'special' registers. The distinction between special
310 * and normal registers isn't very clear and is architecture
311 * dependent.
312 */
313 void getRegisters(struct kvm_regs &regs) const;
314 void setRegisters(const struct kvm_regs &regs);
315 void getSpecialRegisters(struct kvm_sregs &regs) const;
316 void setSpecialRegisters(const struct kvm_sregs &regs);
317 /** @} */
318
319 /** @{ */
320 /**
321 * Get/Set the guest FPU/vector state
322 */
323 void getFPUState(struct kvm_fpu &state) const;
324 void setFPUState(const struct kvm_fpu &state);
325 /** @} */
326
327 /** @{ */
328 /**
329 * Get/Set single register using the KVM_(SET|GET)_ONE_REG API.
330 *
331 * @note The presence of this call depends on Kvm::capOneReg().
332 */
333 void setOneReg(uint64_t id, const void *addr);
334 void setOneReg(uint64_t id, uint64_t value) { setOneReg(id, &value); }
335 void setOneReg(uint64_t id, uint32_t value) { setOneReg(id, &value); }
336 void getOneReg(uint64_t id, void *addr) const;
337 uint64_t getOneRegU64(uint64_t id) const {
338 uint64_t value;
339 getOneReg(id, &value);
340 return value;
341 }
342 uint32_t getOneRegU32(uint64_t id) const {
343 uint32_t value;
344 getOneReg(id, &value);
345 return value;
346 }
347 /** @} */
348
349 /**
350 * Get and format one register for printout.
351 *
352 * This function call getOneReg() to retrieve the contents of one
353 * register and automatically formats it for printing.
354 *
355 * @note The presence of this call depends on Kvm::capOneReg().
356 */
357 std::string getAndFormatOneReg(uint64_t id) const;
358
359 /** @{ */
360 /**
361 * Update the KVM state from the current thread context
362 *
363 * The base CPU calls this method before starting the guest CPU
364 * when the contextDirty flag is set. The architecture dependent
365 * CPU implementation is expected to update all guest state
366 * (registers, special registers, and FPU state).
367 */
368 virtual void updateKvmState() = 0;
369
370 /**
371 * Update the current thread context with the KVM state
372 *
373 * The base CPU after the guest updates any of the KVM state. In
374 * practice, this happens after kvmRun is called. The architecture
375 * dependent code is expected to read the state of the guest CPU
376 * and update gem5's thread state.
377 */
378 virtual void updateThreadContext() = 0;
379
380 /**
381 * Update a thread context if the KVM state is dirty with respect
382 * to the cached thread context.
383 */
384 void syncThreadContext();
385
386 /**
387 * Update the KVM if the thread context is dirty.
388 */
389 void syncKvmState();
390 /** @} */
391
392 /** @{ */
393 /**
394 * Main kvmRun exit handler, calls the relevant handleKvmExit*
395 * depending on exit type.
396 *
397 * @return Number of ticks spent servicing the exit request
398 */
399 virtual Tick handleKvmExit();
400
401 /**
402 * The guest performed a legacy IO request (out/inp on x86)
403 *
404 * @return Number of ticks spent servicing the IO request
405 */
406 virtual Tick handleKvmExitIO();
407
408 /**
409 * The guest requested a monitor service using a hypercall
410 *
411 * @return Number of ticks spent servicing the hypercall
412 */
413 virtual Tick handleKvmExitHypercall();
414
415 /**
416 * The guest exited because an interrupt window was requested
417 *
418 * The guest exited because an interrupt window was requested
419 * (request_interrupt_window in the kvm_run structure was set to 1
420 * before calling kvmRun) and it is now ready to receive
421 *
422 * @return Number of ticks spent servicing the IRQ
423 */
424 virtual Tick handleKvmExitIRQWindowOpen();
425
426 /**
427 * An unknown architecture dependent error occurred when starting
428 * the vCPU
429 *
430 * The kvm_run data structure contains the hardware error
431 * code. The defaults behavior of this method just prints the HW
432 * error code and panics. Architecture dependent implementations
433 * may want to override this method to provide better,
434 * hardware-aware, error messages.
435 *
436 * @return Number of ticks delay the next CPU tick
437 */
438 virtual Tick handleKvmExitUnknown();
439
440 /**
441 * An unhandled virtualization exception occured
442 *
443 * Some KVM virtualization drivers return unhandled exceptions to
444 * the user-space monitor. This interface is currently only used
445 * by the Intel VMX KVM driver.
446 *
447 * @return Number of ticks delay the next CPU tick
448 */
449 virtual Tick handleKvmExitException();
450
451 /**
452 * KVM failed to start the virtualized CPU
453 *
454 * The kvm_run data structure contains the hardware-specific error
455 * code.
456 *
457 * @return Number of ticks delay the next CPU tick
458 */
459 virtual Tick handleKvmExitFailEntry();
460 /** @} */
461
462 /**
463 * Is the architecture specific code in a state that prevents
464 * draining?
465 *
466 * This method should return false if there are any pending events
467 * in the guest vCPU that won't be carried over to the gem5 state
468 * and thus will prevent correct checkpointing or CPU handover. It
469 * might, for example, check for pending interrupts that have been
470 * passed to the vCPU but not acknowledged by the OS. Architecures
471 * implementing this method <i>must</i> override
472 * kvmRunDrain().
473 *
474 * @see BaseKvmCPU::kvmRunDrain()
475 *
476 * @return true if the vCPU is drained, false otherwise.
477 */
478 virtual bool archIsDrained() const { return true; }
479
480 /**
388 * Inject a memory mapped IO request into gem5
389 *
390 * @param paddr Physical address
391 * @param data Pointer to the source/destination buffer
392 * @param size Memory access size
393 * @param write True if write, False if read
394 * @return Number of ticks spent servicing the memory access
395 */
396 Tick doMMIOAccess(Addr paddr, void *data, int size, bool write);
397
481 * Inject a memory mapped IO request into gem5
482 *
483 * @param paddr Physical address
484 * @param data Pointer to the source/destination buffer
485 * @param size Memory access size
486 * @param write True if write, False if read
487 * @return Number of ticks spent servicing the memory access
488 */
489 Tick doMMIOAccess(Addr paddr, void *data, int size, bool write);
490
491 /** @{ */
492 /**
493 * Set the signal mask used in kvmRun()
494 *
495 * This method allows the signal mask of the thread executing
496 * kvmRun() to be overridden inside the actual system call. This
497 * allows us to mask timer signals used to force KVM exits while
498 * in gem5.
499 *
500 * The signal mask can be disabled by setting it to NULL.
501 *
502 * @param mask Signals to mask
503 */
504 void setSignalMask(const sigset_t *mask);
505 /** @} */
398
399 /**
400 * @addtogroup KvmIoctl
401 * @{
402 */
403 /**
404 * vCPU ioctl interface.
405 *
406 * @param request KVM vCPU request
407 * @param p1 Optional request parameter
408 *
409 * @return -1 on error (error number in errno), ioctl dependent
410 * value otherwise.
411 */
412 int ioctl(int request, long p1) const;
413 int ioctl(int request, void *p1) const {
414 return ioctl(request, (long)p1);
415 }
416 int ioctl(int request) const {
417 return ioctl(request, 0L);
418 }
419 /** @} */
420
421
422 /**
423 * KVM memory port. Uses the default MasterPort behavior, but
424 * panics on timing accesses.
425 */
426 class KVMCpuPort : public MasterPort
427 {
428
429 public:
430 KVMCpuPort(const std::string &_name, BaseKvmCPU *_cpu)
431 : MasterPort(_name, _cpu)
432 { }
433
434 protected:
435 bool recvTimingResp(PacketPtr pkt)
436 {
437 panic("The KVM CPU doesn't expect recvTimingResp!\n");
438 return true;
439 }
440
441 void recvRetry()
442 {
443 panic("The KVM CPU doesn't expect recvRetry!\n");
444 }
445
446 };
447
448 /** Port for data requests */
449 KVMCpuPort dataPort;
450
451 /** Unused dummy port for the instruction interface */
452 KVMCpuPort instPort;
453
454 /** Pre-allocated MMIO memory request */
455 Request mmio_req;
456
457 /**
458 * Is the gem5 context dirty? Set to true to force an update of
459 * the KVM vCPU state upon the next call to kvmRun().
460 */
461 bool threadContextDirty;
462
463 /**
464 * Is the KVM state dirty? Set to true to force an update of
465 * the KVM vCPU state upon the next call to kvmRun().
466 */
467 bool kvmStateDirty;
468
469 /** KVM internal ID of the vCPU */
470 const long vcpuID;
471
472 private:
473 struct TickEvent : public Event
474 {
475 BaseKvmCPU &cpu;
476
477 TickEvent(BaseKvmCPU &c)
478 : Event(CPU_Tick_Pri), cpu(c) {}
479
480 void process() { cpu.tick(); }
481
482 const char *description() const {
483 return "BaseKvmCPU tick";
484 }
485 };
486
487 /**
488 * Service MMIO requests in the mmioRing.
489 *
490 *
491 * @return Number of ticks spent servicing the MMIO requests in
492 * the MMIO ring buffer
493 */
494 Tick flushCoalescedMMIO();
495
496 /**
497 * Setup a signal handler to catch the timer signal used to
498 * switch back to the monitor.
499 */
500 void setupSignalHandler();
501
506
507 /**
508 * @addtogroup KvmIoctl
509 * @{
510 */
511 /**
512 * vCPU ioctl interface.
513 *
514 * @param request KVM vCPU request
515 * @param p1 Optional request parameter
516 *
517 * @return -1 on error (error number in errno), ioctl dependent
518 * value otherwise.
519 */
520 int ioctl(int request, long p1) const;
521 int ioctl(int request, void *p1) const {
522 return ioctl(request, (long)p1);
523 }
524 int ioctl(int request) const {
525 return ioctl(request, 0L);
526 }
527 /** @} */
528
529
530 /**
531 * KVM memory port. Uses the default MasterPort behavior, but
532 * panics on timing accesses.
533 */
534 class KVMCpuPort : public MasterPort
535 {
536
537 public:
538 KVMCpuPort(const std::string &_name, BaseKvmCPU *_cpu)
539 : MasterPort(_name, _cpu)
540 { }
541
542 protected:
543 bool recvTimingResp(PacketPtr pkt)
544 {
545 panic("The KVM CPU doesn't expect recvTimingResp!\n");
546 return true;
547 }
548
549 void recvRetry()
550 {
551 panic("The KVM CPU doesn't expect recvRetry!\n");
552 }
553
554 };
555
556 /** Port for data requests */
557 KVMCpuPort dataPort;
558
559 /** Unused dummy port for the instruction interface */
560 KVMCpuPort instPort;
561
562 /** Pre-allocated MMIO memory request */
563 Request mmio_req;
564
565 /**
566 * Is the gem5 context dirty? Set to true to force an update of
567 * the KVM vCPU state upon the next call to kvmRun().
568 */
569 bool threadContextDirty;
570
571 /**
572 * Is the KVM state dirty? Set to true to force an update of
573 * the KVM vCPU state upon the next call to kvmRun().
574 */
575 bool kvmStateDirty;
576
577 /** KVM internal ID of the vCPU */
578 const long vcpuID;
579
580 private:
581 struct TickEvent : public Event
582 {
583 BaseKvmCPU &cpu;
584
585 TickEvent(BaseKvmCPU &c)
586 : Event(CPU_Tick_Pri), cpu(c) {}
587
588 void process() { cpu.tick(); }
589
590 const char *description() const {
591 return "BaseKvmCPU tick";
592 }
593 };
594
595 /**
596 * Service MMIO requests in the mmioRing.
597 *
598 *
599 * @return Number of ticks spent servicing the MMIO requests in
600 * the MMIO ring buffer
601 */
602 Tick flushCoalescedMMIO();
603
604 /**
605 * Setup a signal handler to catch the timer signal used to
606 * switch back to the monitor.
607 */
608 void setupSignalHandler();
609
610 /**
611 * Discard a (potentially) pending signal.
612 *
613 * @param signum Signal to discard
614 * @return true if the signal was pending, false otherwise.
615 */
616 bool discardPendingSignal(int signum) const;
617
502 /** Setup hardware performance counters */
503 void setupCounters();
504
618 /** Setup hardware performance counters */
619 void setupCounters();
620
621 /** Try to drain the CPU if a drain is pending */
622 bool tryDrain();
623
624 /** Execute the KVM_RUN ioctl */
625 void ioctlRun();
626
505 /** KVM vCPU file descriptor */
506 int vcpuFD;
507 /** Size of MMAPed kvm_run area */
508 int vcpuMMapSize;
509 /**
510 * Pointer to the kvm_run structure used to communicate parameters
511 * with KVM.
512 *
513 * @note This is the base pointer of the MMAPed KVM region. The
514 * first page contains the kvm_run structure. Subsequent pages may
515 * contain other data such as the MMIO ring buffer.
516 */
517 struct kvm_run *_kvmRun;
518 /**
519 * Coalesced MMIO ring buffer. NULL if coalesced MMIO is not
520 * supported.
521 */
522 struct kvm_coalesced_mmio_ring *mmioRing;
523 /** Cached page size of the host */
524 const long pageSize;
525
526 TickEvent tickEvent;
527
528 /** @{ */
529 /** Guest performance counters */
530 PerfKvmCounter hwCycles;
531 PerfKvmCounter hwInstructions;
532 /** @} */
533
534 /**
535 * Does the runTimer control the performance counters?
536 *
537 * The run timer will automatically enable and disable performance
538 * counters if a PerfEvent-based timer is used to control KVM
539 * exits.
540 */
541 bool perfControlledByTimer;
542
543 /**
544 * Timer used to force execution into the monitor after a
545 * specified number of simulation tick equivalents have executed
546 * in the guest. This counter generates the signal specified by
547 * KVM_TIMER_SIGNAL.
548 */
549 std::unique_ptr<BaseKvmTimer> runTimer;
550
551 float hostFactor;
552
627 /** KVM vCPU file descriptor */
628 int vcpuFD;
629 /** Size of MMAPed kvm_run area */
630 int vcpuMMapSize;
631 /**
632 * Pointer to the kvm_run structure used to communicate parameters
633 * with KVM.
634 *
635 * @note This is the base pointer of the MMAPed KVM region. The
636 * first page contains the kvm_run structure. Subsequent pages may
637 * contain other data such as the MMIO ring buffer.
638 */
639 struct kvm_run *_kvmRun;
640 /**
641 * Coalesced MMIO ring buffer. NULL if coalesced MMIO is not
642 * supported.
643 */
644 struct kvm_coalesced_mmio_ring *mmioRing;
645 /** Cached page size of the host */
646 const long pageSize;
647
648 TickEvent tickEvent;
649
650 /** @{ */
651 /** Guest performance counters */
652 PerfKvmCounter hwCycles;
653 PerfKvmCounter hwInstructions;
654 /** @} */
655
656 /**
657 * Does the runTimer control the performance counters?
658 *
659 * The run timer will automatically enable and disable performance
660 * counters if a PerfEvent-based timer is used to control KVM
661 * exits.
662 */
663 bool perfControlledByTimer;
664
665 /**
666 * Timer used to force execution into the monitor after a
667 * specified number of simulation tick equivalents have executed
668 * in the guest. This counter generates the signal specified by
669 * KVM_TIMER_SIGNAL.
670 */
671 std::unique_ptr<BaseKvmTimer> runTimer;
672
673 float hostFactor;
674
675 /**
676 * Drain manager to use when signaling drain completion
677 *
678 * This pointer is non-NULL when draining and NULL otherwise.
679 */
680 DrainManager *drainManager;
681
553 public:
554 /* @{ */
555 Stats::Scalar numInsts;
556 Stats::Scalar numVMExits;
557 Stats::Scalar numMMIO;
558 Stats::Scalar numCoalescedMMIO;
559 Stats::Scalar numIO;
560 Stats::Scalar numHalt;
561 Stats::Scalar numInterrupts;
562 Stats::Scalar numHypercalls;
563 /* @} */
564
565 /** Number of instructions executed by the CPU */
566 Counter ctrInsts;
567};
568
569#endif
682 public:
683 /* @{ */
684 Stats::Scalar numInsts;
685 Stats::Scalar numVMExits;
686 Stats::Scalar numMMIO;
687 Stats::Scalar numCoalescedMMIO;
688 Stats::Scalar numIO;
689 Stats::Scalar numHalt;
690 Stats::Scalar numInterrupts;
691 Stats::Scalar numHypercalls;
692 /* @} */
693
694 /** Number of instructions executed by the CPU */
695 Counter ctrInsts;
696};
697
698#endif