]>
Commit | Line | Data |
---|---|---|
296af7c9 BS |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
7b31bbc2 | 25 | #include "qemu/osdep.h" |
a8d25326 | 26 | #include "qemu-common.h" |
8d4e9146 | 27 | #include "qemu/config-file.h" |
d6454270 | 28 | #include "migration/vmstate.h" |
83c9089e | 29 | #include "monitor/monitor.h" |
e688df6b | 30 | #include "qapi/error.h" |
112ed241 | 31 | #include "qapi/qapi-commands-misc.h" |
9af23989 | 32 | #include "qapi/qapi-events-run-state.h" |
a4e15de9 | 33 | #include "qapi/qmp/qerror.h" |
d49b6836 | 34 | #include "qemu/error-report.h" |
76c86615 | 35 | #include "qemu/qemu-print.h" |
14a48c1d | 36 | #include "sysemu/tcg.h" |
da31d594 | 37 | #include "sysemu/block-backend.h" |
022c62cb | 38 | #include "exec/gdbstub.h" |
9c17d615 | 39 | #include "sysemu/dma.h" |
b3946626 | 40 | #include "sysemu/hw_accel.h" |
9c17d615 | 41 | #include "sysemu/kvm.h" |
b0cb0a66 | 42 | #include "sysemu/hax.h" |
c97d6d2c | 43 | #include "sysemu/hvf.h" |
19306806 | 44 | #include "sysemu/whpx.h" |
63c91552 | 45 | #include "exec/exec-all.h" |
296af7c9 | 46 | |
1de7afc9 | 47 | #include "qemu/thread.h" |
9c17d615 PB |
48 | #include "sysemu/cpus.h" |
49 | #include "sysemu/qtest.h" | |
1de7afc9 | 50 | #include "qemu/main-loop.h" |
922a01a0 | 51 | #include "qemu/option.h" |
1de7afc9 | 52 | #include "qemu/bitmap.h" |
cb365646 | 53 | #include "qemu/seqlock.h" |
9c09a251 | 54 | #include "qemu/guest-random.h" |
8d4e9146 | 55 | #include "tcg.h" |
9cb805fd | 56 | #include "hw/nmi.h" |
8b427044 | 57 | #include "sysemu/replay.h" |
54d31236 | 58 | #include "sysemu/runstate.h" |
5cc8767d | 59 | #include "hw/boards.h" |
650d103d | 60 | #include "hw/hw.h" |
0ff0fc19 | 61 | |
6d9cb73c JK |
62 | #ifdef CONFIG_LINUX |
63 | ||
64 | #include <sys/prctl.h> | |
65 | ||
c0532a76 MT |
66 | #ifndef PR_MCE_KILL |
67 | #define PR_MCE_KILL 33 | |
68 | #endif | |
69 | ||
6d9cb73c JK |
70 | #ifndef PR_MCE_KILL_SET |
71 | #define PR_MCE_KILL_SET 1 | |
72 | #endif | |
73 | ||
74 | #ifndef PR_MCE_KILL_EARLY | |
75 | #define PR_MCE_KILL_EARLY 1 | |
76 | #endif | |
77 | ||
78 | #endif /* CONFIG_LINUX */ | |
79 | ||
bd1f7ff4 YK |
80 | static QemuMutex qemu_global_mutex; |
81 | ||
27498bef ST |
82 | int64_t max_delay; |
83 | int64_t max_advance; | |
296af7c9 | 84 | |
2adcc85d JH |
85 | /* vcpu throttling controls */ |
86 | static QEMUTimer *throttle_timer; | |
87 | static unsigned int throttle_percentage; | |
88 | ||
89 | #define CPU_THROTTLE_PCT_MIN 1 | |
90 | #define CPU_THROTTLE_PCT_MAX 99 | |
91 | #define CPU_THROTTLE_TIMESLICE_NS 10000000 | |
92 | ||
321bc0b2 TC |
93 | bool cpu_is_stopped(CPUState *cpu) |
94 | { | |
95 | return cpu->stopped || !runstate_is_running(); | |
96 | } | |
97 | ||
a98ae1d8 | 98 | static bool cpu_thread_is_idle(CPUState *cpu) |
ac873f1e | 99 | { |
c64ca814 | 100 | if (cpu->stop || cpu->queued_work_first) { |
ac873f1e PM |
101 | return false; |
102 | } | |
321bc0b2 | 103 | if (cpu_is_stopped(cpu)) { |
ac873f1e PM |
104 | return true; |
105 | } | |
8c2e1b00 | 106 | if (!cpu->halted || cpu_has_work(cpu) || |
215e79c0 | 107 | kvm_halt_in_kernel()) { |
ac873f1e PM |
108 | return false; |
109 | } | |
110 | return true; | |
111 | } | |
112 | ||
113 | static bool all_cpu_threads_idle(void) | |
114 | { | |
182735ef | 115 | CPUState *cpu; |
ac873f1e | 116 | |
bdc44640 | 117 | CPU_FOREACH(cpu) { |
182735ef | 118 | if (!cpu_thread_is_idle(cpu)) { |
ac873f1e PM |
119 | return false; |
120 | } | |
121 | } | |
122 | return true; | |
123 | } | |
124 | ||
946fb27c PB |
125 | /***********************************************************/ |
126 | /* guest cycle counter */ | |
127 | ||
a3270e19 PB |
128 | /* Protected by TimersState seqlock */ |
129 | ||
5045e9d9 | 130 | static bool icount_sleep = true; |
946fb27c PB |
131 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
132 | #define MAX_ICOUNT_SHIFT 10 | |
a3270e19 | 133 | |
946fb27c | 134 | typedef struct TimersState { |
cb365646 | 135 | /* Protected by BQL. */ |
946fb27c PB |
136 | int64_t cpu_ticks_prev; |
137 | int64_t cpu_ticks_offset; | |
cb365646 | 138 | |
94377115 PB |
139 | /* Protect fields that can be respectively read outside the |
140 | * BQL, and written from multiple threads. | |
cb365646 LPF |
141 | */ |
142 | QemuSeqLock vm_clock_seqlock; | |
94377115 PB |
143 | QemuSpin vm_clock_lock; |
144 | ||
145 | int16_t cpu_ticks_enabled; | |
c96778bb | 146 | |
c1ff073c | 147 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
94377115 PB |
148 | int16_t icount_time_shift; |
149 | ||
c96778bb FK |
150 | /* Compensate for varying guest execution speed. */ |
151 | int64_t qemu_icount_bias; | |
94377115 PB |
152 | |
153 | int64_t vm_clock_warp_start; | |
154 | int64_t cpu_clock_offset; | |
155 | ||
c96778bb FK |
156 | /* Only written by TCG thread */ |
157 | int64_t qemu_icount; | |
94377115 | 158 | |
b39e3f34 | 159 | /* for adjusting icount */ |
b39e3f34 PD |
160 | QEMUTimer *icount_rt_timer; |
161 | QEMUTimer *icount_vm_timer; | |
162 | QEMUTimer *icount_warp_timer; | |
946fb27c PB |
163 | } TimersState; |
164 | ||
d9cd4007 | 165 | static TimersState timers_state; |
8d4e9146 FK |
166 | bool mttcg_enabled; |
167 | ||
168 | /* | |
169 | * We default to false if we know other options have been enabled | |
170 | * which are currently incompatible with MTTCG. Otherwise when each | |
171 | * guest (target) has been updated to support: | |
172 | * - atomic instructions | |
173 | * - memory ordering primitives (barriers) | |
174 | * they can set the appropriate CONFIG flags in ${target}-softmmu.mak | |
175 | * | |
176 | * Once a guest architecture has been converted to the new primitives | |
177 | * there are two remaining limitations to check. | |
178 | * | |
179 | * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host) | |
180 | * - The host must have a stronger memory order than the guest | |
181 | * | |
182 | * It may be possible in future to support strong guests on weak hosts | |
183 | * but that will require tagging all load/stores in a guest with their | |
184 | * implicit memory order requirements which would likely slow things | |
185 | * down a lot. | |
186 | */ | |
187 | ||
188 | static bool check_tcg_memory_orders_compatible(void) | |
189 | { | |
190 | #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO) | |
191 | return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0; | |
192 | #else | |
193 | return false; | |
194 | #endif | |
195 | } | |
196 | ||
197 | static bool default_mttcg_enabled(void) | |
198 | { | |
83fd9629 | 199 | if (use_icount || TCG_OVERSIZED_GUEST) { |
8d4e9146 FK |
200 | return false; |
201 | } else { | |
202 | #ifdef TARGET_SUPPORTS_MTTCG | |
203 | return check_tcg_memory_orders_compatible(); | |
204 | #else | |
205 | return false; | |
206 | #endif | |
207 | } | |
208 | } | |
209 | ||
210 | void qemu_tcg_configure(QemuOpts *opts, Error **errp) | |
211 | { | |
212 | const char *t = qemu_opt_get(opts, "thread"); | |
213 | if (t) { | |
214 | if (strcmp(t, "multi") == 0) { | |
215 | if (TCG_OVERSIZED_GUEST) { | |
216 | error_setg(errp, "No MTTCG when guest word size > hosts"); | |
83fd9629 AB |
217 | } else if (use_icount) { |
218 | error_setg(errp, "No MTTCG when icount is enabled"); | |
8d4e9146 | 219 | } else { |
86953503 | 220 | #ifndef TARGET_SUPPORTS_MTTCG |
0765691e MA |
221 | warn_report("Guest not yet converted to MTTCG - " |
222 | "you may get unexpected results"); | |
c34c7620 | 223 | #endif |
8d4e9146 | 224 | if (!check_tcg_memory_orders_compatible()) { |
0765691e MA |
225 | warn_report("Guest expects a stronger memory ordering " |
226 | "than the host provides"); | |
8cfef892 | 227 | error_printf("This may cause strange/hard to debug errors\n"); |
8d4e9146 FK |
228 | } |
229 | mttcg_enabled = true; | |
230 | } | |
231 | } else if (strcmp(t, "single") == 0) { | |
232 | mttcg_enabled = false; | |
233 | } else { | |
234 | error_setg(errp, "Invalid 'thread' setting %s", t); | |
235 | } | |
236 | } else { | |
237 | mttcg_enabled = default_mttcg_enabled(); | |
238 | } | |
239 | } | |
946fb27c | 240 | |
e4cd9657 AB |
241 | /* The current number of executed instructions is based on what we |
242 | * originally budgeted minus the current state of the decrementing | |
243 | * icount counters in extra/u16.low. | |
244 | */ | |
245 | static int64_t cpu_get_icount_executed(CPUState *cpu) | |
246 | { | |
5e140196 RH |
247 | return (cpu->icount_budget - |
248 | (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra)); | |
e4cd9657 AB |
249 | } |
250 | ||
512d3c80 AB |
251 | /* |
252 | * Update the global shared timer_state.qemu_icount to take into | |
253 | * account executed instructions. This is done by the TCG vCPU | |
254 | * thread so the main-loop can see time has moved forward. | |
255 | */ | |
9b4e6f49 | 256 | static void cpu_update_icount_locked(CPUState *cpu) |
512d3c80 AB |
257 | { |
258 | int64_t executed = cpu_get_icount_executed(cpu); | |
259 | cpu->icount_budget -= executed; | |
260 | ||
38adcb6e EC |
261 | atomic_set_i64(&timers_state.qemu_icount, |
262 | timers_state.qemu_icount + executed); | |
9b4e6f49 PB |
263 | } |
264 | ||
265 | /* | |
266 | * Update the global shared timer_state.qemu_icount to take into | |
267 | * account executed instructions. This is done by the TCG vCPU | |
268 | * thread so the main-loop can see time has moved forward. | |
269 | */ | |
270 | void cpu_update_icount(CPUState *cpu) | |
271 | { | |
272 | seqlock_write_lock(&timers_state.vm_clock_seqlock, | |
273 | &timers_state.vm_clock_lock); | |
274 | cpu_update_icount_locked(cpu); | |
94377115 PB |
275 | seqlock_write_unlock(&timers_state.vm_clock_seqlock, |
276 | &timers_state.vm_clock_lock); | |
512d3c80 AB |
277 | } |
278 | ||
c1ff073c | 279 | static int64_t cpu_get_icount_raw_locked(void) |
946fb27c | 280 | { |
4917cf44 | 281 | CPUState *cpu = current_cpu; |
946fb27c | 282 | |
243c5f77 | 283 | if (cpu && cpu->running) { |
414b15c9 | 284 | if (!cpu->can_do_io) { |
493d89bf | 285 | error_report("Bad icount read"); |
2a62914b | 286 | exit(1); |
946fb27c | 287 | } |
e4cd9657 | 288 | /* Take into account what has run */ |
9b4e6f49 | 289 | cpu_update_icount_locked(cpu); |
946fb27c | 290 | } |
38adcb6e EC |
291 | /* The read is protected by the seqlock, but needs atomic64 to avoid UB */ |
292 | return atomic_read_i64(&timers_state.qemu_icount); | |
2a62914b PD |
293 | } |
294 | ||
2a62914b PD |
295 | static int64_t cpu_get_icount_locked(void) |
296 | { | |
c1ff073c | 297 | int64_t icount = cpu_get_icount_raw_locked(); |
c97595d1 EC |
298 | return atomic_read_i64(&timers_state.qemu_icount_bias) + |
299 | cpu_icount_to_ns(icount); | |
c1ff073c PB |
300 | } |
301 | ||
302 | int64_t cpu_get_icount_raw(void) | |
303 | { | |
304 | int64_t icount; | |
305 | unsigned start; | |
306 | ||
307 | do { | |
308 | start = seqlock_read_begin(&timers_state.vm_clock_seqlock); | |
309 | icount = cpu_get_icount_raw_locked(); | |
310 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start)); | |
311 | ||
312 | return icount; | |
946fb27c PB |
313 | } |
314 | ||
c1ff073c | 315 | /* Return the virtual CPU time, based on the instruction counter. */ |
17a15f1b PB |
316 | int64_t cpu_get_icount(void) |
317 | { | |
318 | int64_t icount; | |
319 | unsigned start; | |
320 | ||
321 | do { | |
322 | start = seqlock_read_begin(&timers_state.vm_clock_seqlock); | |
323 | icount = cpu_get_icount_locked(); | |
324 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start)); | |
325 | ||
326 | return icount; | |
327 | } | |
328 | ||
3f031313 FK |
329 | int64_t cpu_icount_to_ns(int64_t icount) |
330 | { | |
c1ff073c | 331 | return icount << atomic_read(&timers_state.icount_time_shift); |
3f031313 FK |
332 | } |
333 | ||
f2a4ad6d PB |
334 | static int64_t cpu_get_ticks_locked(void) |
335 | { | |
336 | int64_t ticks = timers_state.cpu_ticks_offset; | |
337 | if (timers_state.cpu_ticks_enabled) { | |
338 | ticks += cpu_get_host_ticks(); | |
339 | } | |
340 | ||
341 | if (timers_state.cpu_ticks_prev > ticks) { | |
342 | /* Non increasing ticks may happen if the host uses software suspend. */ | |
343 | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; | |
344 | ticks = timers_state.cpu_ticks_prev; | |
345 | } | |
346 | ||
347 | timers_state.cpu_ticks_prev = ticks; | |
348 | return ticks; | |
349 | } | |
350 | ||
d90f3cca C |
351 | /* return the time elapsed in VM between vm_start and vm_stop. Unless |
352 | * icount is active, cpu_get_ticks() uses units of the host CPU cycle | |
353 | * counter. | |
d90f3cca | 354 | */ |
946fb27c PB |
355 | int64_t cpu_get_ticks(void) |
356 | { | |
5f3e3101 PB |
357 | int64_t ticks; |
358 | ||
946fb27c PB |
359 | if (use_icount) { |
360 | return cpu_get_icount(); | |
361 | } | |
5f3e3101 | 362 | |
f2a4ad6d PB |
363 | qemu_spin_lock(&timers_state.vm_clock_lock); |
364 | ticks = cpu_get_ticks_locked(); | |
365 | qemu_spin_unlock(&timers_state.vm_clock_lock); | |
5f3e3101 | 366 | return ticks; |
946fb27c PB |
367 | } |
368 | ||
cb365646 | 369 | static int64_t cpu_get_clock_locked(void) |
946fb27c | 370 | { |
1d45cea5 | 371 | int64_t time; |
cb365646 | 372 | |
1d45cea5 | 373 | time = timers_state.cpu_clock_offset; |
5f3e3101 | 374 | if (timers_state.cpu_ticks_enabled) { |
1d45cea5 | 375 | time += get_clock(); |
946fb27c | 376 | } |
cb365646 | 377 | |
1d45cea5 | 378 | return time; |
cb365646 LPF |
379 | } |
380 | ||
d90f3cca | 381 | /* Return the monotonic time elapsed in VM, i.e., |
8212ff86 PM |
382 | * the time between vm_start and vm_stop |
383 | */ | |
cb365646 LPF |
384 | int64_t cpu_get_clock(void) |
385 | { | |
386 | int64_t ti; | |
387 | unsigned start; | |
388 | ||
389 | do { | |
390 | start = seqlock_read_begin(&timers_state.vm_clock_seqlock); | |
391 | ti = cpu_get_clock_locked(); | |
392 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start)); | |
393 | ||
394 | return ti; | |
946fb27c PB |
395 | } |
396 | ||
cb365646 | 397 | /* enable cpu_get_ticks() |
3224e878 | 398 | * Caller must hold BQL which serves as mutex for vm_clock_seqlock. |
cb365646 | 399 | */ |
946fb27c PB |
400 | void cpu_enable_ticks(void) |
401 | { | |
94377115 PB |
402 | seqlock_write_lock(&timers_state.vm_clock_seqlock, |
403 | &timers_state.vm_clock_lock); | |
946fb27c | 404 | if (!timers_state.cpu_ticks_enabled) { |
4a7428c5 | 405 | timers_state.cpu_ticks_offset -= cpu_get_host_ticks(); |
946fb27c PB |
406 | timers_state.cpu_clock_offset -= get_clock(); |
407 | timers_state.cpu_ticks_enabled = 1; | |
408 | } | |
94377115 PB |
409 | seqlock_write_unlock(&timers_state.vm_clock_seqlock, |
410 | &timers_state.vm_clock_lock); | |
946fb27c PB |
411 | } |
412 | ||
413 | /* disable cpu_get_ticks() : the clock is stopped. You must not call | |
cb365646 | 414 | * cpu_get_ticks() after that. |
3224e878 | 415 | * Caller must hold BQL which serves as mutex for vm_clock_seqlock. |
cb365646 | 416 | */ |
946fb27c PB |
417 | void cpu_disable_ticks(void) |
418 | { | |
94377115 PB |
419 | seqlock_write_lock(&timers_state.vm_clock_seqlock, |
420 | &timers_state.vm_clock_lock); | |
946fb27c | 421 | if (timers_state.cpu_ticks_enabled) { |
4a7428c5 | 422 | timers_state.cpu_ticks_offset += cpu_get_host_ticks(); |
cb365646 | 423 | timers_state.cpu_clock_offset = cpu_get_clock_locked(); |
946fb27c PB |
424 | timers_state.cpu_ticks_enabled = 0; |
425 | } | |
94377115 PB |
426 | seqlock_write_unlock(&timers_state.vm_clock_seqlock, |
427 | &timers_state.vm_clock_lock); | |
946fb27c PB |
428 | } |
429 | ||
430 | /* Correlation between real and virtual time is always going to be | |
431 | fairly approximate, so ignore small variation. | |
432 | When the guest is idle real and virtual time will be aligned in | |
433 | the IO wait loop. */ | |
73bcb24d | 434 | #define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10) |
946fb27c PB |
435 | |
436 | static void icount_adjust(void) | |
437 | { | |
438 | int64_t cur_time; | |
439 | int64_t cur_icount; | |
440 | int64_t delta; | |
a3270e19 PB |
441 | |
442 | /* Protected by TimersState mutex. */ | |
946fb27c | 443 | static int64_t last_delta; |
468cc7cf | 444 | |
946fb27c PB |
445 | /* If the VM is not running, then do nothing. */ |
446 | if (!runstate_is_running()) { | |
447 | return; | |
448 | } | |
468cc7cf | 449 | |
94377115 PB |
450 | seqlock_write_lock(&timers_state.vm_clock_seqlock, |
451 | &timers_state.vm_clock_lock); | |
17a15f1b PB |
452 | cur_time = cpu_get_clock_locked(); |
453 | cur_icount = cpu_get_icount_locked(); | |
468cc7cf | 454 | |
946fb27c PB |
455 | delta = cur_icount - cur_time; |
456 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ | |
457 | if (delta > 0 | |
458 | && last_delta + ICOUNT_WOBBLE < delta * 2 | |
c1ff073c | 459 | && timers_state.icount_time_shift > 0) { |
946fb27c | 460 | /* The guest is getting too far ahead. Slow time down. */ |
c1ff073c PB |
461 | atomic_set(&timers_state.icount_time_shift, |
462 | timers_state.icount_time_shift - 1); | |
946fb27c PB |
463 | } |
464 | if (delta < 0 | |
465 | && last_delta - ICOUNT_WOBBLE > delta * 2 | |
c1ff073c | 466 | && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) { |
946fb27c | 467 | /* The guest is getting too far behind. Speed time up. */ |
c1ff073c PB |
468 | atomic_set(&timers_state.icount_time_shift, |
469 | timers_state.icount_time_shift + 1); | |
946fb27c PB |
470 | } |
471 | last_delta = delta; | |
c97595d1 EC |
472 | atomic_set_i64(&timers_state.qemu_icount_bias, |
473 | cur_icount - (timers_state.qemu_icount | |
474 | << timers_state.icount_time_shift)); | |
94377115 PB |
475 | seqlock_write_unlock(&timers_state.vm_clock_seqlock, |
476 | &timers_state.vm_clock_lock); | |
946fb27c PB |
477 | } |
478 | ||
479 | static void icount_adjust_rt(void *opaque) | |
480 | { | |
b39e3f34 | 481 | timer_mod(timers_state.icount_rt_timer, |
1979b908 | 482 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000); |
946fb27c PB |
483 | icount_adjust(); |
484 | } | |
485 | ||
486 | static void icount_adjust_vm(void *opaque) | |
487 | { | |
b39e3f34 | 488 | timer_mod(timers_state.icount_vm_timer, |
40daca54 | 489 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
73bcb24d | 490 | NANOSECONDS_PER_SECOND / 10); |
946fb27c PB |
491 | icount_adjust(); |
492 | } | |
493 | ||
494 | static int64_t qemu_icount_round(int64_t count) | |
495 | { | |
c1ff073c PB |
496 | int shift = atomic_read(&timers_state.icount_time_shift); |
497 | return (count + (1 << shift) - 1) >> shift; | |
946fb27c PB |
498 | } |
499 | ||
efab87cf | 500 | static void icount_warp_rt(void) |
946fb27c | 501 | { |
ccffff48 AB |
502 | unsigned seq; |
503 | int64_t warp_start; | |
504 | ||
17a15f1b PB |
505 | /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start |
506 | * changes from -1 to another value, so the race here is okay. | |
507 | */ | |
ccffff48 AB |
508 | do { |
509 | seq = seqlock_read_begin(&timers_state.vm_clock_seqlock); | |
b39e3f34 | 510 | warp_start = timers_state.vm_clock_warp_start; |
ccffff48 AB |
511 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq)); |
512 | ||
513 | if (warp_start == -1) { | |
946fb27c PB |
514 | return; |
515 | } | |
516 | ||
94377115 PB |
517 | seqlock_write_lock(&timers_state.vm_clock_seqlock, |
518 | &timers_state.vm_clock_lock); | |
946fb27c | 519 | if (runstate_is_running()) { |
74c0b816 PB |
520 | int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT, |
521 | cpu_get_clock_locked()); | |
8ed961d9 PB |
522 | int64_t warp_delta; |
523 | ||
b39e3f34 | 524 | warp_delta = clock - timers_state.vm_clock_warp_start; |
8ed961d9 | 525 | if (use_icount == 2) { |
946fb27c | 526 | /* |
40daca54 | 527 | * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too |
946fb27c PB |
528 | * far ahead of real time. |
529 | */ | |
17a15f1b | 530 | int64_t cur_icount = cpu_get_icount_locked(); |
bf2a7ddb | 531 | int64_t delta = clock - cur_icount; |
8ed961d9 | 532 | warp_delta = MIN(warp_delta, delta); |
946fb27c | 533 | } |
c97595d1 EC |
534 | atomic_set_i64(&timers_state.qemu_icount_bias, |
535 | timers_state.qemu_icount_bias + warp_delta); | |
946fb27c | 536 | } |
b39e3f34 | 537 | timers_state.vm_clock_warp_start = -1; |
94377115 PB |
538 | seqlock_write_unlock(&timers_state.vm_clock_seqlock, |
539 | &timers_state.vm_clock_lock); | |
8ed961d9 PB |
540 | |
541 | if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) { | |
542 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); | |
543 | } | |
946fb27c PB |
544 | } |
545 | ||
e76d1798 | 546 | static void icount_timer_cb(void *opaque) |
efab87cf | 547 | { |
e76d1798 PD |
548 | /* No need for a checkpoint because the timer already synchronizes |
549 | * with CHECKPOINT_CLOCK_VIRTUAL_RT. | |
550 | */ | |
551 | icount_warp_rt(); | |
efab87cf PD |
552 | } |
553 | ||
8156be56 PB |
554 | void qtest_clock_warp(int64_t dest) |
555 | { | |
40daca54 | 556 | int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
efef88b3 | 557 | AioContext *aio_context; |
8156be56 | 558 | assert(qtest_enabled()); |
efef88b3 | 559 | aio_context = qemu_get_aio_context(); |
8156be56 | 560 | while (clock < dest) { |
dcb15780 PD |
561 | int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, |
562 | QEMU_TIMER_ATTR_ALL); | |
c9299e2f | 563 | int64_t warp = qemu_soonest_timeout(dest - clock, deadline); |
efef88b3 | 564 | |
94377115 PB |
565 | seqlock_write_lock(&timers_state.vm_clock_seqlock, |
566 | &timers_state.vm_clock_lock); | |
c97595d1 EC |
567 | atomic_set_i64(&timers_state.qemu_icount_bias, |
568 | timers_state.qemu_icount_bias + warp); | |
94377115 PB |
569 | seqlock_write_unlock(&timers_state.vm_clock_seqlock, |
570 | &timers_state.vm_clock_lock); | |
17a15f1b | 571 | |
40daca54 | 572 | qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); |
efef88b3 | 573 | timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]); |
40daca54 | 574 | clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
8156be56 | 575 | } |
40daca54 | 576 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
8156be56 PB |
577 | } |
578 | ||
e76d1798 | 579 | void qemu_start_warp_timer(void) |
946fb27c | 580 | { |
ce78d18c | 581 | int64_t clock; |
946fb27c PB |
582 | int64_t deadline; |
583 | ||
e76d1798 | 584 | if (!use_icount) { |
946fb27c PB |
585 | return; |
586 | } | |
587 | ||
8bd7f71d PD |
588 | /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers |
589 | * do not fire, so computing the deadline does not make sense. | |
590 | */ | |
591 | if (!runstate_is_running()) { | |
592 | return; | |
593 | } | |
594 | ||
0c08185f PD |
595 | if (replay_mode != REPLAY_MODE_PLAY) { |
596 | if (!all_cpu_threads_idle()) { | |
597 | return; | |
598 | } | |
8bd7f71d | 599 | |
0c08185f PD |
600 | if (qtest_enabled()) { |
601 | /* When testing, qtest commands advance icount. */ | |
602 | return; | |
603 | } | |
946fb27c | 604 | |
0c08185f PD |
605 | replay_checkpoint(CHECKPOINT_CLOCK_WARP_START); |
606 | } else { | |
607 | /* warp clock deterministically in record/replay mode */ | |
608 | if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) { | |
609 | /* vCPU is sleeping and warp can't be started. | |
610 | It is probably a race condition: notification sent | |
611 | to vCPU was processed in advance and vCPU went to sleep. | |
612 | Therefore we have to wake it up for doing someting. */ | |
613 | if (replay_has_checkpoint()) { | |
614 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); | |
615 | } | |
616 | return; | |
617 | } | |
8156be56 PB |
618 | } |
619 | ||
ac70aafc | 620 | /* We want to use the earliest deadline from ALL vm_clocks */ |
bf2a7ddb | 621 | clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT); |
dcb15780 PD |
622 | deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, |
623 | ~QEMU_TIMER_ATTR_EXTERNAL); | |
ce78d18c | 624 | if (deadline < 0) { |
d7a0f71d VC |
625 | static bool notified; |
626 | if (!icount_sleep && !notified) { | |
3dc6f869 | 627 | warn_report("icount sleep disabled and no active timers"); |
d7a0f71d VC |
628 | notified = true; |
629 | } | |
ce78d18c | 630 | return; |
ac70aafc AB |
631 | } |
632 | ||
946fb27c PB |
633 | if (deadline > 0) { |
634 | /* | |
40daca54 | 635 | * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to |
946fb27c PB |
636 | * sleep. Otherwise, the CPU might be waiting for a future timer |
637 | * interrupt to wake it up, but the interrupt never comes because | |
638 | * the vCPU isn't running any insns and thus doesn't advance the | |
40daca54 | 639 | * QEMU_CLOCK_VIRTUAL. |
946fb27c | 640 | */ |
5045e9d9 VC |
641 | if (!icount_sleep) { |
642 | /* | |
643 | * We never let VCPUs sleep in no sleep icount mode. | |
644 | * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance | |
645 | * to the next QEMU_CLOCK_VIRTUAL event and notify it. | |
646 | * It is useful when we want a deterministic execution time, | |
647 | * isolated from host latencies. | |
648 | */ | |
94377115 PB |
649 | seqlock_write_lock(&timers_state.vm_clock_seqlock, |
650 | &timers_state.vm_clock_lock); | |
c97595d1 EC |
651 | atomic_set_i64(&timers_state.qemu_icount_bias, |
652 | timers_state.qemu_icount_bias + deadline); | |
94377115 PB |
653 | seqlock_write_unlock(&timers_state.vm_clock_seqlock, |
654 | &timers_state.vm_clock_lock); | |
5045e9d9 VC |
655 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
656 | } else { | |
657 | /* | |
658 | * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some | |
659 | * "real" time, (related to the time left until the next event) has | |
660 | * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this. | |
661 | * This avoids that the warps are visible externally; for example, | |
662 | * you will not be sending network packets continuously instead of | |
663 | * every 100ms. | |
664 | */ | |
94377115 PB |
665 | seqlock_write_lock(&timers_state.vm_clock_seqlock, |
666 | &timers_state.vm_clock_lock); | |
b39e3f34 PD |
667 | if (timers_state.vm_clock_warp_start == -1 |
668 | || timers_state.vm_clock_warp_start > clock) { | |
669 | timers_state.vm_clock_warp_start = clock; | |
5045e9d9 | 670 | } |
94377115 PB |
671 | seqlock_write_unlock(&timers_state.vm_clock_seqlock, |
672 | &timers_state.vm_clock_lock); | |
b39e3f34 PD |
673 | timer_mod_anticipate(timers_state.icount_warp_timer, |
674 | clock + deadline); | |
ce78d18c | 675 | } |
ac70aafc | 676 | } else if (deadline == 0) { |
40daca54 | 677 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
946fb27c PB |
678 | } |
679 | } | |
680 | ||
e76d1798 PD |
681 | static void qemu_account_warp_timer(void) |
682 | { | |
683 | if (!use_icount || !icount_sleep) { | |
684 | return; | |
685 | } | |
686 | ||
687 | /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers | |
688 | * do not fire, so computing the deadline does not make sense. | |
689 | */ | |
690 | if (!runstate_is_running()) { | |
691 | return; | |
692 | } | |
693 | ||
694 | /* warp clock deterministically in record/replay mode */ | |
695 | if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) { | |
696 | return; | |
697 | } | |
698 | ||
b39e3f34 | 699 | timer_del(timers_state.icount_warp_timer); |
e76d1798 PD |
700 | icount_warp_rt(); |
701 | } | |
702 | ||
d09eae37 FK |
703 | static bool icount_state_needed(void *opaque) |
704 | { | |
705 | return use_icount; | |
706 | } | |
707 | ||
b39e3f34 PD |
708 | static bool warp_timer_state_needed(void *opaque) |
709 | { | |
710 | TimersState *s = opaque; | |
711 | return s->icount_warp_timer != NULL; | |
712 | } | |
713 | ||
714 | static bool adjust_timers_state_needed(void *opaque) | |
715 | { | |
716 | TimersState *s = opaque; | |
717 | return s->icount_rt_timer != NULL; | |
718 | } | |
719 | ||
720 | /* | |
721 | * Subsection for warp timer migration is optional, because may not be created | |
722 | */ | |
723 | static const VMStateDescription icount_vmstate_warp_timer = { | |
724 | .name = "timer/icount/warp_timer", | |
725 | .version_id = 1, | |
726 | .minimum_version_id = 1, | |
727 | .needed = warp_timer_state_needed, | |
728 | .fields = (VMStateField[]) { | |
729 | VMSTATE_INT64(vm_clock_warp_start, TimersState), | |
730 | VMSTATE_TIMER_PTR(icount_warp_timer, TimersState), | |
731 | VMSTATE_END_OF_LIST() | |
732 | } | |
733 | }; | |
734 | ||
735 | static const VMStateDescription icount_vmstate_adjust_timers = { | |
736 | .name = "timer/icount/timers", | |
737 | .version_id = 1, | |
738 | .minimum_version_id = 1, | |
739 | .needed = adjust_timers_state_needed, | |
740 | .fields = (VMStateField[]) { | |
741 | VMSTATE_TIMER_PTR(icount_rt_timer, TimersState), | |
742 | VMSTATE_TIMER_PTR(icount_vm_timer, TimersState), | |
743 | VMSTATE_END_OF_LIST() | |
744 | } | |
745 | }; | |
746 | ||
d09eae37 FK |
747 | /* |
748 | * This is a subsection for icount migration. | |
749 | */ | |
750 | static const VMStateDescription icount_vmstate_timers = { | |
751 | .name = "timer/icount", | |
752 | .version_id = 1, | |
753 | .minimum_version_id = 1, | |
5cd8cada | 754 | .needed = icount_state_needed, |
d09eae37 FK |
755 | .fields = (VMStateField[]) { |
756 | VMSTATE_INT64(qemu_icount_bias, TimersState), | |
757 | VMSTATE_INT64(qemu_icount, TimersState), | |
758 | VMSTATE_END_OF_LIST() | |
b39e3f34 PD |
759 | }, |
760 | .subsections = (const VMStateDescription*[]) { | |
761 | &icount_vmstate_warp_timer, | |
762 | &icount_vmstate_adjust_timers, | |
763 | NULL | |
d09eae37 FK |
764 | } |
765 | }; | |
766 | ||
946fb27c PB |
767 | static const VMStateDescription vmstate_timers = { |
768 | .name = "timer", | |
769 | .version_id = 2, | |
770 | .minimum_version_id = 1, | |
35d08458 | 771 | .fields = (VMStateField[]) { |
946fb27c | 772 | VMSTATE_INT64(cpu_ticks_offset, TimersState), |
c1ff073c | 773 | VMSTATE_UNUSED(8), |
946fb27c PB |
774 | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2), |
775 | VMSTATE_END_OF_LIST() | |
d09eae37 | 776 | }, |
5cd8cada JQ |
777 | .subsections = (const VMStateDescription*[]) { |
778 | &icount_vmstate_timers, | |
779 | NULL | |
946fb27c PB |
780 | } |
781 | }; | |
782 | ||
14e6fe12 | 783 | static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) |
2adcc85d | 784 | { |
2adcc85d JH |
785 | double pct; |
786 | double throttle_ratio; | |
bd1f7ff4 | 787 | int64_t sleeptime_ns, endtime_ns; |
2adcc85d JH |
788 | |
789 | if (!cpu_throttle_get_percentage()) { | |
790 | return; | |
791 | } | |
792 | ||
793 | pct = (double)cpu_throttle_get_percentage()/100; | |
794 | throttle_ratio = pct / (1 - pct); | |
bd1f7ff4 YK |
795 | /* Add 1ns to fix double's rounding error (like 0.9999999...) */ |
796 | sleeptime_ns = (int64_t)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS + 1); | |
797 | endtime_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + sleeptime_ns; | |
798 | while (sleeptime_ns > 0 && !cpu->stop) { | |
799 | if (sleeptime_ns > SCALE_MS) { | |
800 | qemu_cond_timedwait(cpu->halt_cond, &qemu_global_mutex, | |
801 | sleeptime_ns / SCALE_MS); | |
802 | } else { | |
803 | qemu_mutex_unlock_iothread(); | |
804 | g_usleep(sleeptime_ns / SCALE_US); | |
805 | qemu_mutex_lock_iothread(); | |
806 | } | |
807 | sleeptime_ns = endtime_ns - qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
808 | } | |
90bb0c04 | 809 | atomic_set(&cpu->throttle_thread_scheduled, 0); |
2adcc85d JH |
810 | } |
811 | ||
812 | static void cpu_throttle_timer_tick(void *opaque) | |
813 | { | |
814 | CPUState *cpu; | |
815 | double pct; | |
816 | ||
817 | /* Stop the timer if needed */ | |
818 | if (!cpu_throttle_get_percentage()) { | |
819 | return; | |
820 | } | |
821 | CPU_FOREACH(cpu) { | |
822 | if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) { | |
14e6fe12 PB |
823 | async_run_on_cpu(cpu, cpu_throttle_thread, |
824 | RUN_ON_CPU_NULL); | |
2adcc85d JH |
825 | } |
826 | } | |
827 | ||
828 | pct = (double)cpu_throttle_get_percentage()/100; | |
829 | timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) + | |
830 | CPU_THROTTLE_TIMESLICE_NS / (1-pct)); | |
831 | } | |
832 | ||
833 | void cpu_throttle_set(int new_throttle_pct) | |
834 | { | |
835 | /* Ensure throttle percentage is within valid range */ | |
836 | new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX); | |
837 | new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN); | |
838 | ||
839 | atomic_set(&throttle_percentage, new_throttle_pct); | |
840 | ||
841 | timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) + | |
842 | CPU_THROTTLE_TIMESLICE_NS); | |
843 | } | |
844 | ||
845 | void cpu_throttle_stop(void) | |
846 | { | |
847 | atomic_set(&throttle_percentage, 0); | |
848 | } | |
849 | ||
850 | bool cpu_throttle_active(void) | |
851 | { | |
852 | return (cpu_throttle_get_percentage() != 0); | |
853 | } | |
854 | ||
855 | int cpu_throttle_get_percentage(void) | |
856 | { | |
857 | return atomic_read(&throttle_percentage); | |
858 | } | |
859 | ||
4603ea01 PD |
860 | void cpu_ticks_init(void) |
861 | { | |
ccdb3c1f | 862 | seqlock_init(&timers_state.vm_clock_seqlock); |
87a09cdc | 863 | qemu_spin_init(&timers_state.vm_clock_lock); |
4603ea01 | 864 | vmstate_register(NULL, 0, &vmstate_timers, &timers_state); |
2adcc85d JH |
865 | throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT, |
866 | cpu_throttle_timer_tick, NULL); | |
4603ea01 PD |
867 | } |
868 | ||
1ad9580b | 869 | void configure_icount(QemuOpts *opts, Error **errp) |
946fb27c | 870 | { |
1ad9580b | 871 | const char *option; |
a8bfac37 | 872 | char *rem_str = NULL; |
1ad9580b | 873 | |
1ad9580b | 874 | option = qemu_opt_get(opts, "shift"); |
946fb27c | 875 | if (!option) { |
a8bfac37 ST |
876 | if (qemu_opt_get(opts, "align") != NULL) { |
877 | error_setg(errp, "Please specify shift option when using align"); | |
878 | } | |
946fb27c PB |
879 | return; |
880 | } | |
f1f4b57e VC |
881 | |
882 | icount_sleep = qemu_opt_get_bool(opts, "sleep", true); | |
5045e9d9 | 883 | if (icount_sleep) { |
b39e3f34 | 884 | timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT, |
e76d1798 | 885 | icount_timer_cb, NULL); |
5045e9d9 | 886 | } |
f1f4b57e | 887 | |
a8bfac37 | 888 | icount_align_option = qemu_opt_get_bool(opts, "align", false); |
f1f4b57e VC |
889 | |
890 | if (icount_align_option && !icount_sleep) { | |
778d9f9b | 891 | error_setg(errp, "align=on and sleep=off are incompatible"); |
f1f4b57e | 892 | } |
946fb27c | 893 | if (strcmp(option, "auto") != 0) { |
a8bfac37 | 894 | errno = 0; |
c1ff073c | 895 | timers_state.icount_time_shift = strtol(option, &rem_str, 0); |
a8bfac37 ST |
896 | if (errno != 0 || *rem_str != '\0' || !strlen(option)) { |
897 | error_setg(errp, "icount: Invalid shift value"); | |
898 | } | |
946fb27c PB |
899 | use_icount = 1; |
900 | return; | |
a8bfac37 ST |
901 | } else if (icount_align_option) { |
902 | error_setg(errp, "shift=auto and align=on are incompatible"); | |
f1f4b57e | 903 | } else if (!icount_sleep) { |
778d9f9b | 904 | error_setg(errp, "shift=auto and sleep=off are incompatible"); |
946fb27c PB |
905 | } |
906 | ||
907 | use_icount = 2; | |
908 | ||
909 | /* 125MIPS seems a reasonable initial guess at the guest speed. | |
910 | It will be corrected fairly quickly anyway. */ | |
c1ff073c | 911 | timers_state.icount_time_shift = 3; |
946fb27c PB |
912 | |
913 | /* Have both realtime and virtual time triggers for speed adjustment. | |
914 | The realtime trigger catches emulated time passing too slowly, | |
915 | the virtual time trigger catches emulated time passing too fast. | |
916 | Realtime triggers occur even when idle, so use them less frequently | |
917 | than VM triggers. */ | |
b39e3f34 PD |
918 | timers_state.vm_clock_warp_start = -1; |
919 | timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT, | |
bf2a7ddb | 920 | icount_adjust_rt, NULL); |
b39e3f34 | 921 | timer_mod(timers_state.icount_rt_timer, |
bf2a7ddb | 922 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000); |
b39e3f34 | 923 | timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
40daca54 | 924 | icount_adjust_vm, NULL); |
b39e3f34 | 925 | timer_mod(timers_state.icount_vm_timer, |
40daca54 | 926 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
73bcb24d | 927 | NANOSECONDS_PER_SECOND / 10); |
946fb27c PB |
928 | } |
929 | ||
6546706d AB |
930 | /***********************************************************/ |
931 | /* TCG vCPU kick timer | |
932 | * | |
933 | * The kick timer is responsible for moving single threaded vCPU | |
934 | * emulation on to the next vCPU. If more than one vCPU is running a | |
935 | * timer event with force a cpu->exit so the next vCPU can get | |
936 | * scheduled. | |
937 | * | |
938 | * The timer is removed if all vCPUs are idle and restarted again once | |
939 | * idleness is complete. | |
940 | */ | |
941 | ||
942 | static QEMUTimer *tcg_kick_vcpu_timer; | |
791158d9 | 943 | static CPUState *tcg_current_rr_cpu; |
6546706d AB |
944 | |
945 | #define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10) | |
946 | ||
947 | static inline int64_t qemu_tcg_next_kick(void) | |
948 | { | |
949 | return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD; | |
950 | } | |
951 | ||
e8f22f76 AB |
952 | /* Kick the currently round-robin scheduled vCPU to next */ |
953 | static void qemu_cpu_kick_rr_next_cpu(void) | |
791158d9 AB |
954 | { |
955 | CPUState *cpu; | |
791158d9 AB |
956 | do { |
957 | cpu = atomic_mb_read(&tcg_current_rr_cpu); | |
958 | if (cpu) { | |
959 | cpu_exit(cpu); | |
960 | } | |
961 | } while (cpu != atomic_mb_read(&tcg_current_rr_cpu)); | |
962 | } | |
963 | ||
e8f22f76 AB |
964 | /* Kick all RR vCPUs */ |
965 | static void qemu_cpu_kick_rr_cpus(void) | |
966 | { | |
967 | CPUState *cpu; | |
968 | ||
969 | CPU_FOREACH(cpu) { | |
970 | cpu_exit(cpu); | |
971 | }; | |
972 | } | |
973 | ||
6b8f0187 PB |
974 | static void do_nothing(CPUState *cpu, run_on_cpu_data unused) |
975 | { | |
976 | } | |
977 | ||
3f53bc61 PB |
978 | void qemu_timer_notify_cb(void *opaque, QEMUClockType type) |
979 | { | |
6b8f0187 PB |
980 | if (!use_icount || type != QEMU_CLOCK_VIRTUAL) { |
981 | qemu_notify_event(); | |
982 | return; | |
983 | } | |
984 | ||
c52e7132 PM |
985 | if (qemu_in_vcpu_thread()) { |
986 | /* A CPU is currently running; kick it back out to the | |
987 | * tcg_cpu_exec() loop so it will recalculate its | |
988 | * icount deadline immediately. | |
989 | */ | |
990 | qemu_cpu_kick(current_cpu); | |
991 | } else if (first_cpu) { | |
6b8f0187 PB |
992 | /* qemu_cpu_kick is not enough to kick a halted CPU out of |
993 | * qemu_tcg_wait_io_event. async_run_on_cpu, instead, | |
994 | * causes cpu_thread_is_idle to return false. This way, | |
995 | * handle_icount_deadline can run. | |
c52e7132 PM |
996 | * If we have no CPUs at all for some reason, we don't |
997 | * need to do anything. | |
6b8f0187 PB |
998 | */ |
999 | async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL); | |
1000 | } | |
3f53bc61 PB |
1001 | } |
1002 | ||
6546706d AB |
1003 | static void kick_tcg_thread(void *opaque) |
1004 | { | |
1005 | timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick()); | |
e8f22f76 | 1006 | qemu_cpu_kick_rr_next_cpu(); |
6546706d AB |
1007 | } |
1008 | ||
1009 | static void start_tcg_kick_timer(void) | |
1010 | { | |
db08b687 PB |
1011 | assert(!mttcg_enabled); |
1012 | if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) { | |
6546706d AB |
1013 | tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
1014 | kick_tcg_thread, NULL); | |
1926ab27 AB |
1015 | } |
1016 | if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) { | |
6546706d AB |
1017 | timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick()); |
1018 | } | |
1019 | } | |
1020 | ||
1021 | static void stop_tcg_kick_timer(void) | |
1022 | { | |
db08b687 | 1023 | assert(!mttcg_enabled); |
1926ab27 | 1024 | if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) { |
6546706d | 1025 | timer_del(tcg_kick_vcpu_timer); |
6546706d AB |
1026 | } |
1027 | } | |
1028 | ||
296af7c9 BS |
1029 | /***********************************************************/ |
1030 | void hw_error(const char *fmt, ...) | |
1031 | { | |
1032 | va_list ap; | |
55e5c285 | 1033 | CPUState *cpu; |
296af7c9 BS |
1034 | |
1035 | va_start(ap, fmt); | |
1036 | fprintf(stderr, "qemu: hardware error: "); | |
1037 | vfprintf(stderr, fmt, ap); | |
1038 | fprintf(stderr, "\n"); | |
bdc44640 | 1039 | CPU_FOREACH(cpu) { |
55e5c285 | 1040 | fprintf(stderr, "CPU #%d:\n", cpu->cpu_index); |
90c84c56 | 1041 | cpu_dump_state(cpu, stderr, CPU_DUMP_FPU); |
296af7c9 BS |
1042 | } |
1043 | va_end(ap); | |
1044 | abort(); | |
1045 | } | |
1046 | ||
1047 | void cpu_synchronize_all_states(void) | |
1048 | { | |
182735ef | 1049 | CPUState *cpu; |
296af7c9 | 1050 | |
bdc44640 | 1051 | CPU_FOREACH(cpu) { |
182735ef | 1052 | cpu_synchronize_state(cpu); |
c97d6d2c SAGDR |
1053 | /* TODO: move to cpu_synchronize_state() */ |
1054 | if (hvf_enabled()) { | |
1055 | hvf_cpu_synchronize_state(cpu); | |
1056 | } | |
296af7c9 BS |
1057 | } |
1058 | } | |
1059 | ||
1060 | void cpu_synchronize_all_post_reset(void) | |
1061 | { | |
182735ef | 1062 | CPUState *cpu; |
296af7c9 | 1063 | |
bdc44640 | 1064 | CPU_FOREACH(cpu) { |
182735ef | 1065 | cpu_synchronize_post_reset(cpu); |
c97d6d2c SAGDR |
1066 | /* TODO: move to cpu_synchronize_post_reset() */ |
1067 | if (hvf_enabled()) { | |
1068 | hvf_cpu_synchronize_post_reset(cpu); | |
1069 | } | |
296af7c9 BS |
1070 | } |
1071 | } | |
1072 | ||
1073 | void cpu_synchronize_all_post_init(void) | |
1074 | { | |
182735ef | 1075 | CPUState *cpu; |
296af7c9 | 1076 | |
bdc44640 | 1077 | CPU_FOREACH(cpu) { |
182735ef | 1078 | cpu_synchronize_post_init(cpu); |
c97d6d2c SAGDR |
1079 | /* TODO: move to cpu_synchronize_post_init() */ |
1080 | if (hvf_enabled()) { | |
1081 | hvf_cpu_synchronize_post_init(cpu); | |
1082 | } | |
296af7c9 BS |
1083 | } |
1084 | } | |
1085 | ||
75e972da DG |
1086 | void cpu_synchronize_all_pre_loadvm(void) |
1087 | { | |
1088 | CPUState *cpu; | |
1089 | ||
1090 | CPU_FOREACH(cpu) { | |
1091 | cpu_synchronize_pre_loadvm(cpu); | |
1092 | } | |
1093 | } | |
1094 | ||
4486e89c | 1095 | static int do_vm_stop(RunState state, bool send_stop) |
296af7c9 | 1096 | { |
56983463 KW |
1097 | int ret = 0; |
1098 | ||
1354869c | 1099 | if (runstate_is_running()) { |
296af7c9 | 1100 | cpu_disable_ticks(); |
296af7c9 | 1101 | pause_all_vcpus(); |
f5bbfba1 | 1102 | runstate_set(state); |
1dfb4dd9 | 1103 | vm_state_notify(0, state); |
4486e89c | 1104 | if (send_stop) { |
3ab72385 | 1105 | qapi_event_send_stop(); |
4486e89c | 1106 | } |
296af7c9 | 1107 | } |
56983463 | 1108 | |
594a45ce | 1109 | bdrv_drain_all(); |
22af08ea | 1110 | ret = bdrv_flush_all(); |
594a45ce | 1111 | |
56983463 | 1112 | return ret; |
296af7c9 BS |
1113 | } |
1114 | ||
4486e89c SH |
1115 | /* Special vm_stop() variant for terminating the process. Historically clients |
1116 | * did not expect a QMP STOP event and so we need to retain compatibility. | |
1117 | */ | |
1118 | int vm_shutdown(void) | |
1119 | { | |
1120 | return do_vm_stop(RUN_STATE_SHUTDOWN, false); | |
1121 | } | |
1122 | ||
a1fcaa73 | 1123 | static bool cpu_can_run(CPUState *cpu) |
296af7c9 | 1124 | { |
4fdeee7c | 1125 | if (cpu->stop) { |
a1fcaa73 | 1126 | return false; |
0ab07c62 | 1127 | } |
321bc0b2 | 1128 | if (cpu_is_stopped(cpu)) { |
a1fcaa73 | 1129 | return false; |
0ab07c62 | 1130 | } |
a1fcaa73 | 1131 | return true; |
296af7c9 BS |
1132 | } |
1133 | ||
91325046 | 1134 | static void cpu_handle_guest_debug(CPUState *cpu) |
83f338f7 | 1135 | { |
64f6b346 | 1136 | gdb_set_stop_cpu(cpu); |
8cf71710 | 1137 | qemu_system_debug_request(); |
f324e766 | 1138 | cpu->stopped = true; |
3c638d06 JK |
1139 | } |
1140 | ||
6d9cb73c JK |
1141 | #ifdef CONFIG_LINUX |
1142 | static void sigbus_reraise(void) | |
1143 | { | |
1144 | sigset_t set; | |
1145 | struct sigaction action; | |
1146 | ||
1147 | memset(&action, 0, sizeof(action)); | |
1148 | action.sa_handler = SIG_DFL; | |
1149 | if (!sigaction(SIGBUS, &action, NULL)) { | |
1150 | raise(SIGBUS); | |
1151 | sigemptyset(&set); | |
1152 | sigaddset(&set, SIGBUS); | |
a2d1761d | 1153 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
6d9cb73c JK |
1154 | } |
1155 | perror("Failed to re-raise SIGBUS!\n"); | |
1156 | abort(); | |
1157 | } | |
1158 | ||
d98d4072 | 1159 | static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx) |
6d9cb73c | 1160 | { |
a16fc07e PB |
1161 | if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) { |
1162 | sigbus_reraise(); | |
1163 | } | |
1164 | ||
2ae41db2 PB |
1165 | if (current_cpu) { |
1166 | /* Called asynchronously in VCPU thread. */ | |
1167 | if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) { | |
1168 | sigbus_reraise(); | |
1169 | } | |
1170 | } else { | |
1171 | /* Called synchronously (via signalfd) in main thread. */ | |
1172 | if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) { | |
1173 | sigbus_reraise(); | |
1174 | } | |
6d9cb73c JK |
1175 | } |
1176 | } | |
1177 | ||
1178 | static void qemu_init_sigbus(void) | |
1179 | { | |
1180 | struct sigaction action; | |
1181 | ||
1182 | memset(&action, 0, sizeof(action)); | |
1183 | action.sa_flags = SA_SIGINFO; | |
d98d4072 | 1184 | action.sa_sigaction = sigbus_handler; |
6d9cb73c JK |
1185 | sigaction(SIGBUS, &action, NULL); |
1186 | ||
1187 | prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); | |
1188 | } | |
6d9cb73c | 1189 | #else /* !CONFIG_LINUX */ |
6d9cb73c JK |
1190 | static void qemu_init_sigbus(void) |
1191 | { | |
1192 | } | |
a16fc07e | 1193 | #endif /* !CONFIG_LINUX */ |
ff48eb5f | 1194 | |
296af7c9 BS |
1195 | static QemuThread io_thread; |
1196 | ||
296af7c9 BS |
1197 | /* cpu creation */ |
1198 | static QemuCond qemu_cpu_cond; | |
1199 | /* system init */ | |
296af7c9 BS |
1200 | static QemuCond qemu_pause_cond; |
1201 | ||
d3b12f5d | 1202 | void qemu_init_cpu_loop(void) |
296af7c9 | 1203 | { |
6d9cb73c | 1204 | qemu_init_sigbus(); |
ed94592b | 1205 | qemu_cond_init(&qemu_cpu_cond); |
ed94592b | 1206 | qemu_cond_init(&qemu_pause_cond); |
296af7c9 | 1207 | qemu_mutex_init(&qemu_global_mutex); |
296af7c9 | 1208 | |
b7680cb6 | 1209 | qemu_thread_get_self(&io_thread); |
296af7c9 BS |
1210 | } |
1211 | ||
14e6fe12 | 1212 | void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) |
e82bcec2 | 1213 | { |
d148d90e | 1214 | do_run_on_cpu(cpu, func, data, &qemu_global_mutex); |
3c02270d CV |
1215 | } |
1216 | ||
4c055ab5 GZ |
1217 | static void qemu_kvm_destroy_vcpu(CPUState *cpu) |
1218 | { | |
1219 | if (kvm_destroy_vcpu(cpu) < 0) { | |
1220 | error_report("kvm_destroy_vcpu failed"); | |
1221 | exit(EXIT_FAILURE); | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | static void qemu_tcg_destroy_vcpu(CPUState *cpu) | |
1226 | { | |
1227 | } | |
1228 | ||
ebd05fea DH |
1229 | static void qemu_cpu_stop(CPUState *cpu, bool exit) |
1230 | { | |
1231 | g_assert(qemu_cpu_is_self(cpu)); | |
1232 | cpu->stop = false; | |
1233 | cpu->stopped = true; | |
1234 | if (exit) { | |
1235 | cpu_exit(cpu); | |
1236 | } | |
1237 | qemu_cond_broadcast(&qemu_pause_cond); | |
1238 | } | |
1239 | ||
509a0d78 | 1240 | static void qemu_wait_io_event_common(CPUState *cpu) |
296af7c9 | 1241 | { |
37257942 | 1242 | atomic_mb_set(&cpu->thread_kicked, false); |
4fdeee7c | 1243 | if (cpu->stop) { |
ebd05fea | 1244 | qemu_cpu_stop(cpu, false); |
296af7c9 | 1245 | } |
a5403c69 | 1246 | process_queued_cpu_work(cpu); |
37257942 AB |
1247 | } |
1248 | ||
a8efa606 | 1249 | static void qemu_tcg_rr_wait_io_event(void) |
37257942 | 1250 | { |
a8efa606 PB |
1251 | CPUState *cpu; |
1252 | ||
db08b687 | 1253 | while (all_cpu_threads_idle()) { |
6546706d | 1254 | stop_tcg_kick_timer(); |
a8efa606 | 1255 | qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex); |
16400322 | 1256 | } |
296af7c9 | 1257 | |
6546706d AB |
1258 | start_tcg_kick_timer(); |
1259 | ||
a8efa606 PB |
1260 | CPU_FOREACH(cpu) { |
1261 | qemu_wait_io_event_common(cpu); | |
1262 | } | |
296af7c9 BS |
1263 | } |
1264 | ||
db08b687 | 1265 | static void qemu_wait_io_event(CPUState *cpu) |
296af7c9 | 1266 | { |
a98ae1d8 | 1267 | while (cpu_thread_is_idle(cpu)) { |
f5c121b8 | 1268 | qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); |
16400322 | 1269 | } |
296af7c9 | 1270 | |
db08b687 PB |
1271 | #ifdef _WIN32 |
1272 | /* Eat dummy APC queued by qemu_cpu_kick_thread. */ | |
1273 | if (!tcg_enabled()) { | |
1274 | SleepEx(0, TRUE); | |
c97d6d2c | 1275 | } |
db08b687 | 1276 | #endif |
c97d6d2c SAGDR |
1277 | qemu_wait_io_event_common(cpu); |
1278 | } | |
1279 | ||
7e97cd88 | 1280 | static void *qemu_kvm_cpu_thread_fn(void *arg) |
296af7c9 | 1281 | { |
48a106bd | 1282 | CPUState *cpu = arg; |
84b4915d | 1283 | int r; |
296af7c9 | 1284 | |
ab28bd23 PB |
1285 | rcu_register_thread(); |
1286 | ||
2e7f7a3c | 1287 | qemu_mutex_lock_iothread(); |
814e612e | 1288 | qemu_thread_get_self(cpu->thread); |
9f09e18a | 1289 | cpu->thread_id = qemu_get_thread_id(); |
626cf8f4 | 1290 | cpu->can_do_io = 1; |
4917cf44 | 1291 | current_cpu = cpu; |
296af7c9 | 1292 | |
504134d2 | 1293 | r = kvm_init_vcpu(cpu); |
84b4915d | 1294 | if (r < 0) { |
493d89bf | 1295 | error_report("kvm_init_vcpu failed: %s", strerror(-r)); |
84b4915d JK |
1296 | exit(1); |
1297 | } | |
296af7c9 | 1298 | |
18268b60 | 1299 | kvm_init_cpu_signals(cpu); |
296af7c9 BS |
1300 | |
1301 | /* signal CPU creation */ | |
61a46217 | 1302 | cpu->created = true; |
296af7c9 | 1303 | qemu_cond_signal(&qemu_cpu_cond); |
9c09a251 | 1304 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
296af7c9 | 1305 | |
4c055ab5 | 1306 | do { |
a1fcaa73 | 1307 | if (cpu_can_run(cpu)) { |
1458c363 | 1308 | r = kvm_cpu_exec(cpu); |
83f338f7 | 1309 | if (r == EXCP_DEBUG) { |
91325046 | 1310 | cpu_handle_guest_debug(cpu); |
83f338f7 | 1311 | } |
0ab07c62 | 1312 | } |
db08b687 | 1313 | qemu_wait_io_event(cpu); |
4c055ab5 | 1314 | } while (!cpu->unplug || cpu_can_run(cpu)); |
296af7c9 | 1315 | |
4c055ab5 | 1316 | qemu_kvm_destroy_vcpu(cpu); |
2c579042 BR |
1317 | cpu->created = false; |
1318 | qemu_cond_signal(&qemu_cpu_cond); | |
4c055ab5 | 1319 | qemu_mutex_unlock_iothread(); |
57615ed5 | 1320 | rcu_unregister_thread(); |
296af7c9 BS |
1321 | return NULL; |
1322 | } | |
1323 | ||
c7f0f3b1 AL |
1324 | static void *qemu_dummy_cpu_thread_fn(void *arg) |
1325 | { | |
1326 | #ifdef _WIN32 | |
493d89bf | 1327 | error_report("qtest is not supported under Windows"); |
c7f0f3b1 AL |
1328 | exit(1); |
1329 | #else | |
10a9021d | 1330 | CPUState *cpu = arg; |
c7f0f3b1 AL |
1331 | sigset_t waitset; |
1332 | int r; | |
1333 | ||
ab28bd23 PB |
1334 | rcu_register_thread(); |
1335 | ||
c7f0f3b1 | 1336 | qemu_mutex_lock_iothread(); |
814e612e | 1337 | qemu_thread_get_self(cpu->thread); |
9f09e18a | 1338 | cpu->thread_id = qemu_get_thread_id(); |
626cf8f4 | 1339 | cpu->can_do_io = 1; |
37257942 | 1340 | current_cpu = cpu; |
c7f0f3b1 AL |
1341 | |
1342 | sigemptyset(&waitset); | |
1343 | sigaddset(&waitset, SIG_IPI); | |
1344 | ||
1345 | /* signal CPU creation */ | |
61a46217 | 1346 | cpu->created = true; |
c7f0f3b1 | 1347 | qemu_cond_signal(&qemu_cpu_cond); |
9c09a251 | 1348 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
c7f0f3b1 | 1349 | |
d2831ab0 | 1350 | do { |
c7f0f3b1 AL |
1351 | qemu_mutex_unlock_iothread(); |
1352 | do { | |
1353 | int sig; | |
1354 | r = sigwait(&waitset, &sig); | |
1355 | } while (r == -1 && (errno == EAGAIN || errno == EINTR)); | |
1356 | if (r == -1) { | |
1357 | perror("sigwait"); | |
1358 | exit(1); | |
1359 | } | |
1360 | qemu_mutex_lock_iothread(); | |
db08b687 | 1361 | qemu_wait_io_event(cpu); |
d2831ab0 | 1362 | } while (!cpu->unplug); |
c7f0f3b1 | 1363 | |
d40bfcbb | 1364 | qemu_mutex_unlock_iothread(); |
d2831ab0 | 1365 | rcu_unregister_thread(); |
c7f0f3b1 AL |
1366 | return NULL; |
1367 | #endif | |
1368 | } | |
1369 | ||
1be7fcb8 AB |
1370 | static int64_t tcg_get_icount_limit(void) |
1371 | { | |
1372 | int64_t deadline; | |
1373 | ||
1374 | if (replay_mode != REPLAY_MODE_PLAY) { | |
dcb15780 PD |
1375 | /* |
1376 | * Include all the timers, because they may need an attention. | |
1377 | * Too long CPU execution may create unnecessary delay in UI. | |
1378 | */ | |
1379 | deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, | |
1380 | QEMU_TIMER_ATTR_ALL); | |
1be7fcb8 AB |
1381 | |
1382 | /* Maintain prior (possibly buggy) behaviour where if no deadline | |
1383 | * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than | |
1384 | * INT32_MAX nanoseconds ahead, we still use INT32_MAX | |
1385 | * nanoseconds. | |
1386 | */ | |
1387 | if ((deadline < 0) || (deadline > INT32_MAX)) { | |
1388 | deadline = INT32_MAX; | |
1389 | } | |
1390 | ||
1391 | return qemu_icount_round(deadline); | |
1392 | } else { | |
1393 | return replay_get_instructions(); | |
1394 | } | |
1395 | } | |
1396 | ||
12e9700d AB |
1397 | static void handle_icount_deadline(void) |
1398 | { | |
6b8f0187 | 1399 | assert(qemu_in_vcpu_thread()); |
12e9700d | 1400 | if (use_icount) { |
dcb15780 PD |
1401 | int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, |
1402 | QEMU_TIMER_ATTR_ALL); | |
12e9700d AB |
1403 | |
1404 | if (deadline == 0) { | |
6b8f0187 | 1405 | /* Wake up other AioContexts. */ |
12e9700d | 1406 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
6b8f0187 | 1407 | qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); |
12e9700d AB |
1408 | } |
1409 | } | |
1410 | } | |
1411 | ||
05248382 | 1412 | static void prepare_icount_for_run(CPUState *cpu) |
1be7fcb8 | 1413 | { |
1be7fcb8 | 1414 | if (use_icount) { |
eda5f7c6 | 1415 | int insns_left; |
05248382 AB |
1416 | |
1417 | /* These should always be cleared by process_icount_data after | |
1418 | * each vCPU execution. However u16.high can be raised | |
1419 | * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt | |
1420 | */ | |
5e140196 | 1421 | g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0); |
05248382 AB |
1422 | g_assert(cpu->icount_extra == 0); |
1423 | ||
eda5f7c6 AB |
1424 | cpu->icount_budget = tcg_get_icount_limit(); |
1425 | insns_left = MIN(0xffff, cpu->icount_budget); | |
5e140196 | 1426 | cpu_neg(cpu)->icount_decr.u16.low = insns_left; |
eda5f7c6 | 1427 | cpu->icount_extra = cpu->icount_budget - insns_left; |
d759c951 AB |
1428 | |
1429 | replay_mutex_lock(); | |
1be7fcb8 | 1430 | } |
05248382 AB |
1431 | } |
1432 | ||
1433 | static void process_icount_data(CPUState *cpu) | |
1434 | { | |
1be7fcb8 | 1435 | if (use_icount) { |
e4cd9657 | 1436 | /* Account for executed instructions */ |
512d3c80 | 1437 | cpu_update_icount(cpu); |
05248382 AB |
1438 | |
1439 | /* Reset the counters */ | |
5e140196 | 1440 | cpu_neg(cpu)->icount_decr.u16.low = 0; |
1be7fcb8 | 1441 | cpu->icount_extra = 0; |
e4cd9657 AB |
1442 | cpu->icount_budget = 0; |
1443 | ||
1be7fcb8 | 1444 | replay_account_executed_instructions(); |
d759c951 AB |
1445 | |
1446 | replay_mutex_unlock(); | |
1be7fcb8 | 1447 | } |
05248382 AB |
1448 | } |
1449 | ||
1450 | ||
1451 | static int tcg_cpu_exec(CPUState *cpu) | |
1452 | { | |
1453 | int ret; | |
1454 | #ifdef CONFIG_PROFILER | |
1455 | int64_t ti; | |
1456 | #endif | |
1457 | ||
f28d0dfd | 1458 | assert(tcg_enabled()); |
05248382 AB |
1459 | #ifdef CONFIG_PROFILER |
1460 | ti = profile_getclock(); | |
1461 | #endif | |
05248382 AB |
1462 | cpu_exec_start(cpu); |
1463 | ret = cpu_exec(cpu); | |
1464 | cpu_exec_end(cpu); | |
05248382 | 1465 | #ifdef CONFIG_PROFILER |
72fd2efb EC |
1466 | atomic_set(&tcg_ctx->prof.cpu_exec_time, |
1467 | tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti); | |
05248382 | 1468 | #endif |
1be7fcb8 AB |
1469 | return ret; |
1470 | } | |
1471 | ||
c93bbbef AB |
1472 | /* Destroy any remaining vCPUs which have been unplugged and have |
1473 | * finished running | |
1474 | */ | |
1475 | static void deal_with_unplugged_cpus(void) | |
1be7fcb8 | 1476 | { |
c93bbbef | 1477 | CPUState *cpu; |
1be7fcb8 | 1478 | |
c93bbbef AB |
1479 | CPU_FOREACH(cpu) { |
1480 | if (cpu->unplug && !cpu_can_run(cpu)) { | |
1481 | qemu_tcg_destroy_vcpu(cpu); | |
1482 | cpu->created = false; | |
1483 | qemu_cond_signal(&qemu_cpu_cond); | |
1be7fcb8 AB |
1484 | break; |
1485 | } | |
1486 | } | |
1be7fcb8 | 1487 | } |
bdb7ca67 | 1488 | |
6546706d AB |
1489 | /* Single-threaded TCG |
1490 | * | |
1491 | * In the single-threaded case each vCPU is simulated in turn. If | |
1492 | * there is more than a single vCPU we create a simple timer to kick | |
1493 | * the vCPU and ensure we don't get stuck in a tight loop in one vCPU. | |
1494 | * This is done explicitly rather than relying on side-effects | |
1495 | * elsewhere. | |
1496 | */ | |
1497 | ||
37257942 | 1498 | static void *qemu_tcg_rr_cpu_thread_fn(void *arg) |
296af7c9 | 1499 | { |
c3586ba7 | 1500 | CPUState *cpu = arg; |
296af7c9 | 1501 | |
f28d0dfd | 1502 | assert(tcg_enabled()); |
ab28bd23 | 1503 | rcu_register_thread(); |
3468b59e | 1504 | tcg_register_thread(); |
ab28bd23 | 1505 | |
2e7f7a3c | 1506 | qemu_mutex_lock_iothread(); |
814e612e | 1507 | qemu_thread_get_self(cpu->thread); |
296af7c9 | 1508 | |
5a9c973b DH |
1509 | cpu->thread_id = qemu_get_thread_id(); |
1510 | cpu->created = true; | |
1511 | cpu->can_do_io = 1; | |
296af7c9 | 1512 | qemu_cond_signal(&qemu_cpu_cond); |
9c09a251 | 1513 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
296af7c9 | 1514 | |
fa7d1867 | 1515 | /* wait for initial kick-off after machine start */ |
c28e399c | 1516 | while (first_cpu->stopped) { |
d5f8d613 | 1517 | qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex); |
8e564b4e JK |
1518 | |
1519 | /* process any pending work */ | |
bdc44640 | 1520 | CPU_FOREACH(cpu) { |
37257942 | 1521 | current_cpu = cpu; |
182735ef | 1522 | qemu_wait_io_event_common(cpu); |
8e564b4e | 1523 | } |
0ab07c62 | 1524 | } |
296af7c9 | 1525 | |
6546706d AB |
1526 | start_tcg_kick_timer(); |
1527 | ||
c93bbbef AB |
1528 | cpu = first_cpu; |
1529 | ||
e5143e30 AB |
1530 | /* process any pending work */ |
1531 | cpu->exit_request = 1; | |
1532 | ||
296af7c9 | 1533 | while (1) { |
d759c951 AB |
1534 | qemu_mutex_unlock_iothread(); |
1535 | replay_mutex_lock(); | |
1536 | qemu_mutex_lock_iothread(); | |
c93bbbef AB |
1537 | /* Account partial waits to QEMU_CLOCK_VIRTUAL. */ |
1538 | qemu_account_warp_timer(); | |
1539 | ||
6b8f0187 PB |
1540 | /* Run the timers here. This is much more efficient than |
1541 | * waking up the I/O thread and waiting for completion. | |
1542 | */ | |
1543 | handle_icount_deadline(); | |
1544 | ||
d759c951 AB |
1545 | replay_mutex_unlock(); |
1546 | ||
c93bbbef AB |
1547 | if (!cpu) { |
1548 | cpu = first_cpu; | |
1549 | } | |
1550 | ||
e5143e30 AB |
1551 | while (cpu && !cpu->queued_work_first && !cpu->exit_request) { |
1552 | ||
791158d9 | 1553 | atomic_mb_set(&tcg_current_rr_cpu, cpu); |
37257942 | 1554 | current_cpu = cpu; |
c93bbbef AB |
1555 | |
1556 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, | |
1557 | (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0); | |
1558 | ||
1559 | if (cpu_can_run(cpu)) { | |
1560 | int r; | |
05248382 | 1561 | |
d759c951 | 1562 | qemu_mutex_unlock_iothread(); |
05248382 AB |
1563 | prepare_icount_for_run(cpu); |
1564 | ||
c93bbbef | 1565 | r = tcg_cpu_exec(cpu); |
05248382 AB |
1566 | |
1567 | process_icount_data(cpu); | |
d759c951 | 1568 | qemu_mutex_lock_iothread(); |
05248382 | 1569 | |
c93bbbef AB |
1570 | if (r == EXCP_DEBUG) { |
1571 | cpu_handle_guest_debug(cpu); | |
1572 | break; | |
08e73c48 PK |
1573 | } else if (r == EXCP_ATOMIC) { |
1574 | qemu_mutex_unlock_iothread(); | |
1575 | cpu_exec_step_atomic(cpu); | |
1576 | qemu_mutex_lock_iothread(); | |
1577 | break; | |
c93bbbef | 1578 | } |
37257942 | 1579 | } else if (cpu->stop) { |
c93bbbef AB |
1580 | if (cpu->unplug) { |
1581 | cpu = CPU_NEXT(cpu); | |
1582 | } | |
1583 | break; | |
1584 | } | |
1585 | ||
e5143e30 AB |
1586 | cpu = CPU_NEXT(cpu); |
1587 | } /* while (cpu && !cpu->exit_request).. */ | |
1588 | ||
791158d9 AB |
1589 | /* Does not need atomic_mb_set because a spurious wakeup is okay. */ |
1590 | atomic_set(&tcg_current_rr_cpu, NULL); | |
c93bbbef | 1591 | |
e5143e30 AB |
1592 | if (cpu && cpu->exit_request) { |
1593 | atomic_mb_set(&cpu->exit_request, 0); | |
1594 | } | |
ac70aafc | 1595 | |
013aabdc CD |
1596 | if (use_icount && all_cpu_threads_idle()) { |
1597 | /* | |
1598 | * When all cpus are sleeping (e.g in WFI), to avoid a deadlock | |
1599 | * in the main_loop, wake it up in order to start the warp timer. | |
1600 | */ | |
1601 | qemu_notify_event(); | |
1602 | } | |
1603 | ||
a8efa606 | 1604 | qemu_tcg_rr_wait_io_event(); |
c93bbbef | 1605 | deal_with_unplugged_cpus(); |
296af7c9 BS |
1606 | } |
1607 | ||
9b0605f9 | 1608 | rcu_unregister_thread(); |
296af7c9 BS |
1609 | return NULL; |
1610 | } | |
1611 | ||
b0cb0a66 VP |
1612 | static void *qemu_hax_cpu_thread_fn(void *arg) |
1613 | { | |
1614 | CPUState *cpu = arg; | |
1615 | int r; | |
b3d3a426 | 1616 | |
9857c2d2 | 1617 | rcu_register_thread(); |
b3d3a426 | 1618 | qemu_mutex_lock_iothread(); |
b0cb0a66 | 1619 | qemu_thread_get_self(cpu->thread); |
b0cb0a66 VP |
1620 | |
1621 | cpu->thread_id = qemu_get_thread_id(); | |
1622 | cpu->created = true; | |
b0cb0a66 VP |
1623 | current_cpu = cpu; |
1624 | ||
1625 | hax_init_vcpu(cpu); | |
1626 | qemu_cond_signal(&qemu_cpu_cond); | |
9c09a251 | 1627 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
b0cb0a66 | 1628 | |
9857c2d2 | 1629 | do { |
b0cb0a66 VP |
1630 | if (cpu_can_run(cpu)) { |
1631 | r = hax_smp_cpu_exec(cpu); | |
1632 | if (r == EXCP_DEBUG) { | |
1633 | cpu_handle_guest_debug(cpu); | |
1634 | } | |
1635 | } | |
1636 | ||
db08b687 | 1637 | qemu_wait_io_event(cpu); |
9857c2d2 PB |
1638 | } while (!cpu->unplug || cpu_can_run(cpu)); |
1639 | rcu_unregister_thread(); | |
b0cb0a66 VP |
1640 | return NULL; |
1641 | } | |
1642 | ||
c97d6d2c SAGDR |
1643 | /* The HVF-specific vCPU thread function. This one should only run when the host |
1644 | * CPU supports the VMX "unrestricted guest" feature. */ | |
1645 | static void *qemu_hvf_cpu_thread_fn(void *arg) | |
1646 | { | |
1647 | CPUState *cpu = arg; | |
1648 | ||
1649 | int r; | |
1650 | ||
1651 | assert(hvf_enabled()); | |
1652 | ||
1653 | rcu_register_thread(); | |
1654 | ||
1655 | qemu_mutex_lock_iothread(); | |
1656 | qemu_thread_get_self(cpu->thread); | |
1657 | ||
1658 | cpu->thread_id = qemu_get_thread_id(); | |
1659 | cpu->can_do_io = 1; | |
1660 | current_cpu = cpu; | |
1661 | ||
1662 | hvf_init_vcpu(cpu); | |
1663 | ||
1664 | /* signal CPU creation */ | |
1665 | cpu->created = true; | |
1666 | qemu_cond_signal(&qemu_cpu_cond); | |
9c09a251 | 1667 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
c97d6d2c SAGDR |
1668 | |
1669 | do { | |
1670 | if (cpu_can_run(cpu)) { | |
1671 | r = hvf_vcpu_exec(cpu); | |
1672 | if (r == EXCP_DEBUG) { | |
1673 | cpu_handle_guest_debug(cpu); | |
1674 | } | |
1675 | } | |
db08b687 | 1676 | qemu_wait_io_event(cpu); |
c97d6d2c SAGDR |
1677 | } while (!cpu->unplug || cpu_can_run(cpu)); |
1678 | ||
1679 | hvf_vcpu_destroy(cpu); | |
1680 | cpu->created = false; | |
1681 | qemu_cond_signal(&qemu_cpu_cond); | |
1682 | qemu_mutex_unlock_iothread(); | |
8178e637 | 1683 | rcu_unregister_thread(); |
c97d6d2c SAGDR |
1684 | return NULL; |
1685 | } | |
1686 | ||
19306806 JTV |
1687 | static void *qemu_whpx_cpu_thread_fn(void *arg) |
1688 | { | |
1689 | CPUState *cpu = arg; | |
1690 | int r; | |
1691 | ||
1692 | rcu_register_thread(); | |
1693 | ||
1694 | qemu_mutex_lock_iothread(); | |
1695 | qemu_thread_get_self(cpu->thread); | |
1696 | cpu->thread_id = qemu_get_thread_id(); | |
1697 | current_cpu = cpu; | |
1698 | ||
1699 | r = whpx_init_vcpu(cpu); | |
1700 | if (r < 0) { | |
1701 | fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r)); | |
1702 | exit(1); | |
1703 | } | |
1704 | ||
1705 | /* signal CPU creation */ | |
1706 | cpu->created = true; | |
1707 | qemu_cond_signal(&qemu_cpu_cond); | |
9c09a251 | 1708 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
19306806 JTV |
1709 | |
1710 | do { | |
1711 | if (cpu_can_run(cpu)) { | |
1712 | r = whpx_vcpu_exec(cpu); | |
1713 | if (r == EXCP_DEBUG) { | |
1714 | cpu_handle_guest_debug(cpu); | |
1715 | } | |
1716 | } | |
1717 | while (cpu_thread_is_idle(cpu)) { | |
1718 | qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); | |
1719 | } | |
1720 | qemu_wait_io_event_common(cpu); | |
1721 | } while (!cpu->unplug || cpu_can_run(cpu)); | |
1722 | ||
1723 | whpx_destroy_vcpu(cpu); | |
1724 | cpu->created = false; | |
1725 | qemu_cond_signal(&qemu_cpu_cond); | |
1726 | qemu_mutex_unlock_iothread(); | |
1727 | rcu_unregister_thread(); | |
c97d6d2c SAGDR |
1728 | return NULL; |
1729 | } | |
1730 | ||
b0cb0a66 VP |
1731 | #ifdef _WIN32 |
1732 | static void CALLBACK dummy_apc_func(ULONG_PTR unused) | |
1733 | { | |
1734 | } | |
1735 | #endif | |
1736 | ||
37257942 AB |
1737 | /* Multi-threaded TCG |
1738 | * | |
1739 | * In the multi-threaded case each vCPU has its own thread. The TLS | |
1740 | * variable current_cpu can be used deep in the code to find the | |
1741 | * current CPUState for a given thread. | |
1742 | */ | |
1743 | ||
1744 | static void *qemu_tcg_cpu_thread_fn(void *arg) | |
1745 | { | |
1746 | CPUState *cpu = arg; | |
1747 | ||
f28d0dfd | 1748 | assert(tcg_enabled()); |
bf51c720 AB |
1749 | g_assert(!use_icount); |
1750 | ||
37257942 | 1751 | rcu_register_thread(); |
3468b59e | 1752 | tcg_register_thread(); |
37257942 AB |
1753 | |
1754 | qemu_mutex_lock_iothread(); | |
1755 | qemu_thread_get_self(cpu->thread); | |
1756 | ||
1757 | cpu->thread_id = qemu_get_thread_id(); | |
1758 | cpu->created = true; | |
1759 | cpu->can_do_io = 1; | |
1760 | current_cpu = cpu; | |
1761 | qemu_cond_signal(&qemu_cpu_cond); | |
9c09a251 | 1762 | qemu_guest_random_seed_thread_part2(cpu->random_seed); |
37257942 AB |
1763 | |
1764 | /* process any pending work */ | |
1765 | cpu->exit_request = 1; | |
1766 | ||
54961aac | 1767 | do { |
37257942 AB |
1768 | if (cpu_can_run(cpu)) { |
1769 | int r; | |
d759c951 | 1770 | qemu_mutex_unlock_iothread(); |
37257942 | 1771 | r = tcg_cpu_exec(cpu); |
d759c951 | 1772 | qemu_mutex_lock_iothread(); |
37257942 AB |
1773 | switch (r) { |
1774 | case EXCP_DEBUG: | |
1775 | cpu_handle_guest_debug(cpu); | |
1776 | break; | |
1777 | case EXCP_HALTED: | |
1778 | /* during start-up the vCPU is reset and the thread is | |
1779 | * kicked several times. If we don't ensure we go back | |
1780 | * to sleep in the halted state we won't cleanly | |
1781 | * start-up when the vCPU is enabled. | |
1782 | * | |
1783 | * cpu->halted should ensure we sleep in wait_io_event | |
1784 | */ | |
1785 | g_assert(cpu->halted); | |
1786 | break; | |
08e73c48 PK |
1787 | case EXCP_ATOMIC: |
1788 | qemu_mutex_unlock_iothread(); | |
1789 | cpu_exec_step_atomic(cpu); | |
1790 | qemu_mutex_lock_iothread(); | |
37257942 AB |
1791 | default: |
1792 | /* Ignore everything else? */ | |
1793 | break; | |
1794 | } | |
1795 | } | |
1796 | ||
37257942 | 1797 | atomic_mb_set(&cpu->exit_request, 0); |
db08b687 | 1798 | qemu_wait_io_event(cpu); |
9b0605f9 | 1799 | } while (!cpu->unplug || cpu_can_run(cpu)); |
37257942 | 1800 | |
9b0605f9 PB |
1801 | qemu_tcg_destroy_vcpu(cpu); |
1802 | cpu->created = false; | |
1803 | qemu_cond_signal(&qemu_cpu_cond); | |
1804 | qemu_mutex_unlock_iothread(); | |
1805 | rcu_unregister_thread(); | |
37257942 AB |
1806 | return NULL; |
1807 | } | |
1808 | ||
2ff09a40 | 1809 | static void qemu_cpu_kick_thread(CPUState *cpu) |
cc015e9a PB |
1810 | { |
1811 | #ifndef _WIN32 | |
1812 | int err; | |
1813 | ||
e0c38211 PB |
1814 | if (cpu->thread_kicked) { |
1815 | return; | |
9102deda | 1816 | } |
e0c38211 | 1817 | cpu->thread_kicked = true; |
814e612e | 1818 | err = pthread_kill(cpu->thread->thread, SIG_IPI); |
d455ebc4 | 1819 | if (err && err != ESRCH) { |
cc015e9a PB |
1820 | fprintf(stderr, "qemu:%s: %s", __func__, strerror(err)); |
1821 | exit(1); | |
1822 | } | |
1823 | #else /* _WIN32 */ | |
b0cb0a66 | 1824 | if (!qemu_cpu_is_self(cpu)) { |
19306806 JTV |
1825 | if (whpx_enabled()) { |
1826 | whpx_vcpu_kick(cpu); | |
1827 | } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) { | |
b0cb0a66 VP |
1828 | fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n", |
1829 | __func__, GetLastError()); | |
1830 | exit(1); | |
1831 | } | |
1832 | } | |
e0c38211 PB |
1833 | #endif |
1834 | } | |
ed9164a3 | 1835 | |
c08d7424 | 1836 | void qemu_cpu_kick(CPUState *cpu) |
296af7c9 | 1837 | { |
f5c121b8 | 1838 | qemu_cond_broadcast(cpu->halt_cond); |
e0c38211 | 1839 | if (tcg_enabled()) { |
e8f22f76 AB |
1840 | if (qemu_tcg_mttcg_enabled()) { |
1841 | cpu_exit(cpu); | |
1842 | } else { | |
1843 | qemu_cpu_kick_rr_cpus(); | |
1844 | } | |
e0c38211 | 1845 | } else { |
b0cb0a66 VP |
1846 | if (hax_enabled()) { |
1847 | /* | |
1848 | * FIXME: race condition with the exit_request check in | |
1849 | * hax_vcpu_hax_exec | |
1850 | */ | |
1851 | cpu->exit_request = 1; | |
1852 | } | |
e0c38211 PB |
1853 | qemu_cpu_kick_thread(cpu); |
1854 | } | |
296af7c9 BS |
1855 | } |
1856 | ||
46d62fac | 1857 | void qemu_cpu_kick_self(void) |
296af7c9 | 1858 | { |
4917cf44 | 1859 | assert(current_cpu); |
9102deda | 1860 | qemu_cpu_kick_thread(current_cpu); |
296af7c9 BS |
1861 | } |
1862 | ||
60e82579 | 1863 | bool qemu_cpu_is_self(CPUState *cpu) |
296af7c9 | 1864 | { |
814e612e | 1865 | return qemu_thread_is_self(cpu->thread); |
296af7c9 BS |
1866 | } |
1867 | ||
79e2b9ae | 1868 | bool qemu_in_vcpu_thread(void) |
aa723c23 | 1869 | { |
4917cf44 | 1870 | return current_cpu && qemu_cpu_is_self(current_cpu); |
aa723c23 JQ |
1871 | } |
1872 | ||
afbe7053 PB |
1873 | static __thread bool iothread_locked = false; |
1874 | ||
1875 | bool qemu_mutex_iothread_locked(void) | |
1876 | { | |
1877 | return iothread_locked; | |
1878 | } | |
1879 | ||
cb764d06 EC |
1880 | /* |
1881 | * The BQL is taken from so many places that it is worth profiling the | |
1882 | * callers directly, instead of funneling them all through a single function. | |
1883 | */ | |
1884 | void qemu_mutex_lock_iothread_impl(const char *file, int line) | |
296af7c9 | 1885 | { |
cb764d06 EC |
1886 | QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func); |
1887 | ||
8d04fb55 | 1888 | g_assert(!qemu_mutex_iothread_locked()); |
cb764d06 | 1889 | bql_lock(&qemu_global_mutex, file, line); |
afbe7053 | 1890 | iothread_locked = true; |
296af7c9 BS |
1891 | } |
1892 | ||
1893 | void qemu_mutex_unlock_iothread(void) | |
1894 | { | |
8d04fb55 | 1895 | g_assert(qemu_mutex_iothread_locked()); |
afbe7053 | 1896 | iothread_locked = false; |
296af7c9 BS |
1897 | qemu_mutex_unlock(&qemu_global_mutex); |
1898 | } | |
1899 | ||
e8faee06 | 1900 | static bool all_vcpus_paused(void) |
296af7c9 | 1901 | { |
bdc44640 | 1902 | CPUState *cpu; |
296af7c9 | 1903 | |
bdc44640 | 1904 | CPU_FOREACH(cpu) { |
182735ef | 1905 | if (!cpu->stopped) { |
e8faee06 | 1906 | return false; |
0ab07c62 | 1907 | } |
296af7c9 BS |
1908 | } |
1909 | ||
e8faee06 | 1910 | return true; |
296af7c9 BS |
1911 | } |
1912 | ||
1913 | void pause_all_vcpus(void) | |
1914 | { | |
bdc44640 | 1915 | CPUState *cpu; |
296af7c9 | 1916 | |
40daca54 | 1917 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false); |
bdc44640 | 1918 | CPU_FOREACH(cpu) { |
ebd05fea DH |
1919 | if (qemu_cpu_is_self(cpu)) { |
1920 | qemu_cpu_stop(cpu, true); | |
1921 | } else { | |
1922 | cpu->stop = true; | |
1923 | qemu_cpu_kick(cpu); | |
1924 | } | |
d798e974 JK |
1925 | } |
1926 | ||
d759c951 AB |
1927 | /* We need to drop the replay_lock so any vCPU threads woken up |
1928 | * can finish their replay tasks | |
1929 | */ | |
1930 | replay_mutex_unlock(); | |
1931 | ||
296af7c9 | 1932 | while (!all_vcpus_paused()) { |
be7d6c57 | 1933 | qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex); |
bdc44640 | 1934 | CPU_FOREACH(cpu) { |
182735ef | 1935 | qemu_cpu_kick(cpu); |
296af7c9 BS |
1936 | } |
1937 | } | |
d759c951 AB |
1938 | |
1939 | qemu_mutex_unlock_iothread(); | |
1940 | replay_mutex_lock(); | |
1941 | qemu_mutex_lock_iothread(); | |
296af7c9 BS |
1942 | } |
1943 | ||
2993683b IM |
1944 | void cpu_resume(CPUState *cpu) |
1945 | { | |
1946 | cpu->stop = false; | |
1947 | cpu->stopped = false; | |
1948 | qemu_cpu_kick(cpu); | |
1949 | } | |
1950 | ||
296af7c9 BS |
1951 | void resume_all_vcpus(void) |
1952 | { | |
bdc44640 | 1953 | CPUState *cpu; |
296af7c9 | 1954 | |
40daca54 | 1955 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); |
bdc44640 | 1956 | CPU_FOREACH(cpu) { |
182735ef | 1957 | cpu_resume(cpu); |
296af7c9 BS |
1958 | } |
1959 | } | |
1960 | ||
dbadee4f | 1961 | void cpu_remove_sync(CPUState *cpu) |
4c055ab5 GZ |
1962 | { |
1963 | cpu->stop = true; | |
1964 | cpu->unplug = true; | |
1965 | qemu_cpu_kick(cpu); | |
dbadee4f PB |
1966 | qemu_mutex_unlock_iothread(); |
1967 | qemu_thread_join(cpu->thread); | |
1968 | qemu_mutex_lock_iothread(); | |
2c579042 BR |
1969 | } |
1970 | ||
4900116e DDAG |
1971 | /* For temporary buffers for forming a name */ |
1972 | #define VCPU_THREAD_NAME_SIZE 16 | |
1973 | ||
e5ab30a2 | 1974 | static void qemu_tcg_init_vcpu(CPUState *cpu) |
296af7c9 | 1975 | { |
4900116e | 1976 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
37257942 AB |
1977 | static QemuCond *single_tcg_halt_cond; |
1978 | static QemuThread *single_tcg_cpu_thread; | |
e8feb96f EC |
1979 | static int tcg_region_inited; |
1980 | ||
f28d0dfd | 1981 | assert(tcg_enabled()); |
e8feb96f EC |
1982 | /* |
1983 | * Initialize TCG regions--once. Now is a good time, because: | |
1984 | * (1) TCG's init context, prologue and target globals have been set up. | |
1985 | * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the | |
1986 | * -accel flag is processed, so the check doesn't work then). | |
1987 | */ | |
1988 | if (!tcg_region_inited) { | |
1989 | tcg_region_inited = 1; | |
1990 | tcg_region_init(); | |
1991 | } | |
4900116e | 1992 | |
37257942 | 1993 | if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) { |
814e612e | 1994 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
f5c121b8 AF |
1995 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
1996 | qemu_cond_init(cpu->halt_cond); | |
37257942 AB |
1997 | |
1998 | if (qemu_tcg_mttcg_enabled()) { | |
1999 | /* create a thread per vCPU with TCG (MTTCG) */ | |
2000 | parallel_cpus = true; | |
2001 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG", | |
4900116e | 2002 | cpu->cpu_index); |
37257942 AB |
2003 | |
2004 | qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn, | |
2005 | cpu, QEMU_THREAD_JOINABLE); | |
2006 | ||
2007 | } else { | |
2008 | /* share a single thread for all cpus with TCG */ | |
2009 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG"); | |
2010 | qemu_thread_create(cpu->thread, thread_name, | |
2011 | qemu_tcg_rr_cpu_thread_fn, | |
2012 | cpu, QEMU_THREAD_JOINABLE); | |
2013 | ||
2014 | single_tcg_halt_cond = cpu->halt_cond; | |
2015 | single_tcg_cpu_thread = cpu->thread; | |
2016 | } | |
1ecf47bf | 2017 | #ifdef _WIN32 |
814e612e | 2018 | cpu->hThread = qemu_thread_get_handle(cpu->thread); |
1ecf47bf | 2019 | #endif |
296af7c9 | 2020 | } else { |
37257942 AB |
2021 | /* For non-MTTCG cases we share the thread */ |
2022 | cpu->thread = single_tcg_cpu_thread; | |
2023 | cpu->halt_cond = single_tcg_halt_cond; | |
a342173a DH |
2024 | cpu->thread_id = first_cpu->thread_id; |
2025 | cpu->can_do_io = 1; | |
2026 | cpu->created = true; | |
296af7c9 BS |
2027 | } |
2028 | } | |
2029 | ||
b0cb0a66 VP |
2030 | static void qemu_hax_start_vcpu(CPUState *cpu) |
2031 | { | |
2032 | char thread_name[VCPU_THREAD_NAME_SIZE]; | |
2033 | ||
2034 | cpu->thread = g_malloc0(sizeof(QemuThread)); | |
2035 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); | |
2036 | qemu_cond_init(cpu->halt_cond); | |
2037 | ||
2038 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX", | |
2039 | cpu->cpu_index); | |
2040 | qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn, | |
2041 | cpu, QEMU_THREAD_JOINABLE); | |
2042 | #ifdef _WIN32 | |
2043 | cpu->hThread = qemu_thread_get_handle(cpu->thread); | |
2044 | #endif | |
b0cb0a66 VP |
2045 | } |
2046 | ||
48a106bd | 2047 | static void qemu_kvm_start_vcpu(CPUState *cpu) |
296af7c9 | 2048 | { |
4900116e DDAG |
2049 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
2050 | ||
814e612e | 2051 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
f5c121b8 AF |
2052 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
2053 | qemu_cond_init(cpu->halt_cond); | |
4900116e DDAG |
2054 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM", |
2055 | cpu->cpu_index); | |
2056 | qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn, | |
2057 | cpu, QEMU_THREAD_JOINABLE); | |
296af7c9 BS |
2058 | } |
2059 | ||
c97d6d2c SAGDR |
2060 | static void qemu_hvf_start_vcpu(CPUState *cpu) |
2061 | { | |
2062 | char thread_name[VCPU_THREAD_NAME_SIZE]; | |
2063 | ||
2064 | /* HVF currently does not support TCG, and only runs in | |
2065 | * unrestricted-guest mode. */ | |
2066 | assert(hvf_enabled()); | |
2067 | ||
2068 | cpu->thread = g_malloc0(sizeof(QemuThread)); | |
2069 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); | |
2070 | qemu_cond_init(cpu->halt_cond); | |
2071 | ||
2072 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF", | |
2073 | cpu->cpu_index); | |
2074 | qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn, | |
2075 | cpu, QEMU_THREAD_JOINABLE); | |
c97d6d2c SAGDR |
2076 | } |
2077 | ||
19306806 JTV |
2078 | static void qemu_whpx_start_vcpu(CPUState *cpu) |
2079 | { | |
2080 | char thread_name[VCPU_THREAD_NAME_SIZE]; | |
2081 | ||
2082 | cpu->thread = g_malloc0(sizeof(QemuThread)); | |
2083 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); | |
2084 | qemu_cond_init(cpu->halt_cond); | |
2085 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX", | |
2086 | cpu->cpu_index); | |
2087 | qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn, | |
2088 | cpu, QEMU_THREAD_JOINABLE); | |
2089 | #ifdef _WIN32 | |
2090 | cpu->hThread = qemu_thread_get_handle(cpu->thread); | |
2091 | #endif | |
19306806 JTV |
2092 | } |
2093 | ||
10a9021d | 2094 | static void qemu_dummy_start_vcpu(CPUState *cpu) |
c7f0f3b1 | 2095 | { |
4900116e DDAG |
2096 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
2097 | ||
814e612e | 2098 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
f5c121b8 AF |
2099 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
2100 | qemu_cond_init(cpu->halt_cond); | |
4900116e DDAG |
2101 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY", |
2102 | cpu->cpu_index); | |
2103 | qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu, | |
c7f0f3b1 | 2104 | QEMU_THREAD_JOINABLE); |
c7f0f3b1 AL |
2105 | } |
2106 | ||
c643bed9 | 2107 | void qemu_init_vcpu(CPUState *cpu) |
296af7c9 | 2108 | { |
5cc8767d LX |
2109 | MachineState *ms = MACHINE(qdev_get_machine()); |
2110 | ||
2111 | cpu->nr_cores = ms->smp.cores; | |
2112 | cpu->nr_threads = ms->smp.threads; | |
f324e766 | 2113 | cpu->stopped = true; |
9c09a251 | 2114 | cpu->random_seed = qemu_guest_random_seed_thread_part1(); |
56943e8c PM |
2115 | |
2116 | if (!cpu->as) { | |
2117 | /* If the target cpu hasn't set up any address spaces itself, | |
2118 | * give it the default one. | |
2119 | */ | |
12ebc9a7 | 2120 | cpu->num_ases = 1; |
80ceb07a | 2121 | cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory); |
56943e8c PM |
2122 | } |
2123 | ||
0ab07c62 | 2124 | if (kvm_enabled()) { |
48a106bd | 2125 | qemu_kvm_start_vcpu(cpu); |
b0cb0a66 VP |
2126 | } else if (hax_enabled()) { |
2127 | qemu_hax_start_vcpu(cpu); | |
c97d6d2c SAGDR |
2128 | } else if (hvf_enabled()) { |
2129 | qemu_hvf_start_vcpu(cpu); | |
c7f0f3b1 | 2130 | } else if (tcg_enabled()) { |
e5ab30a2 | 2131 | qemu_tcg_init_vcpu(cpu); |
19306806 JTV |
2132 | } else if (whpx_enabled()) { |
2133 | qemu_whpx_start_vcpu(cpu); | |
c7f0f3b1 | 2134 | } else { |
10a9021d | 2135 | qemu_dummy_start_vcpu(cpu); |
0ab07c62 | 2136 | } |
81e96311 DH |
2137 | |
2138 | while (!cpu->created) { | |
2139 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); | |
2140 | } | |
296af7c9 BS |
2141 | } |
2142 | ||
b4a3d965 | 2143 | void cpu_stop_current(void) |
296af7c9 | 2144 | { |
4917cf44 | 2145 | if (current_cpu) { |
0ec7e677 PM |
2146 | current_cpu->stop = true; |
2147 | cpu_exit(current_cpu); | |
b4a3d965 | 2148 | } |
296af7c9 BS |
2149 | } |
2150 | ||
56983463 | 2151 | int vm_stop(RunState state) |
296af7c9 | 2152 | { |
aa723c23 | 2153 | if (qemu_in_vcpu_thread()) { |
74892d24 | 2154 | qemu_system_vmstop_request_prepare(); |
1dfb4dd9 | 2155 | qemu_system_vmstop_request(state); |
296af7c9 BS |
2156 | /* |
2157 | * FIXME: should not return to device code in case | |
2158 | * vm_stop() has been requested. | |
2159 | */ | |
b4a3d965 | 2160 | cpu_stop_current(); |
56983463 | 2161 | return 0; |
296af7c9 | 2162 | } |
56983463 | 2163 | |
4486e89c | 2164 | return do_vm_stop(state, true); |
296af7c9 BS |
2165 | } |
2166 | ||
2d76e823 CI |
2167 | /** |
2168 | * Prepare for (re)starting the VM. | |
2169 | * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already | |
2170 | * running or in case of an error condition), 0 otherwise. | |
2171 | */ | |
2172 | int vm_prepare_start(void) | |
2173 | { | |
2174 | RunState requested; | |
2d76e823 CI |
2175 | |
2176 | qemu_vmstop_requested(&requested); | |
2177 | if (runstate_is_running() && requested == RUN_STATE__MAX) { | |
2178 | return -1; | |
2179 | } | |
2180 | ||
2181 | /* Ensure that a STOP/RESUME pair of events is emitted if a | |
2182 | * vmstop request was pending. The BLOCK_IO_ERROR event, for | |
2183 | * example, according to documentation is always followed by | |
2184 | * the STOP event. | |
2185 | */ | |
2186 | if (runstate_is_running()) { | |
3ab72385 PX |
2187 | qapi_event_send_stop(); |
2188 | qapi_event_send_resume(); | |
f056158d | 2189 | return -1; |
2d76e823 CI |
2190 | } |
2191 | ||
2192 | /* We are sending this now, but the CPUs will be resumed shortly later */ | |
3ab72385 | 2193 | qapi_event_send_resume(); |
f056158d | 2194 | |
f056158d MA |
2195 | cpu_enable_ticks(); |
2196 | runstate_set(RUN_STATE_RUNNING); | |
2197 | vm_state_notify(1, RUN_STATE_RUNNING); | |
2198 | return 0; | |
2d76e823 CI |
2199 | } |
2200 | ||
2201 | void vm_start(void) | |
2202 | { | |
2203 | if (!vm_prepare_start()) { | |
2204 | resume_all_vcpus(); | |
2205 | } | |
2206 | } | |
2207 | ||
8a9236f1 LC |
2208 | /* does a state transition even if the VM is already stopped, |
2209 | current state is forgotten forever */ | |
56983463 | 2210 | int vm_stop_force_state(RunState state) |
8a9236f1 LC |
2211 | { |
2212 | if (runstate_is_running()) { | |
56983463 | 2213 | return vm_stop(state); |
8a9236f1 LC |
2214 | } else { |
2215 | runstate_set(state); | |
b2780d32 WC |
2216 | |
2217 | bdrv_drain_all(); | |
594a45ce KW |
2218 | /* Make sure to return an error if the flush in a previous vm_stop() |
2219 | * failed. */ | |
22af08ea | 2220 | return bdrv_flush_all(); |
8a9236f1 LC |
2221 | } |
2222 | } | |
2223 | ||
0442428a | 2224 | void list_cpus(const char *optarg) |
262353cb BS |
2225 | { |
2226 | /* XXX: implement xxx_cpu_list for targets that still miss it */ | |
e916cbf8 | 2227 | #if defined(cpu_list) |
0442428a | 2228 | cpu_list(); |
262353cb BS |
2229 | #endif |
2230 | } | |
de0b36b6 | 2231 | |
0cfd6a9a LC |
2232 | void qmp_memsave(int64_t addr, int64_t size, const char *filename, |
2233 | bool has_cpu, int64_t cpu_index, Error **errp) | |
2234 | { | |
2235 | FILE *f; | |
2236 | uint32_t l; | |
55e5c285 | 2237 | CPUState *cpu; |
0cfd6a9a | 2238 | uint8_t buf[1024]; |
0dc9daf0 | 2239 | int64_t orig_addr = addr, orig_size = size; |
0cfd6a9a LC |
2240 | |
2241 | if (!has_cpu) { | |
2242 | cpu_index = 0; | |
2243 | } | |
2244 | ||
151d1322 AF |
2245 | cpu = qemu_get_cpu(cpu_index); |
2246 | if (cpu == NULL) { | |
c6bd8c70 MA |
2247 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
2248 | "a CPU number"); | |
0cfd6a9a LC |
2249 | return; |
2250 | } | |
2251 | ||
2252 | f = fopen(filename, "wb"); | |
2253 | if (!f) { | |
618da851 | 2254 | error_setg_file_open(errp, errno, filename); |
0cfd6a9a LC |
2255 | return; |
2256 | } | |
2257 | ||
2258 | while (size != 0) { | |
2259 | l = sizeof(buf); | |
2260 | if (l > size) | |
2261 | l = size; | |
2f4d0f59 | 2262 | if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) { |
0dc9daf0 BP |
2263 | error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64 |
2264 | " specified", orig_addr, orig_size); | |
2f4d0f59 AK |
2265 | goto exit; |
2266 | } | |
0cfd6a9a | 2267 | if (fwrite(buf, 1, l, f) != l) { |
c6bd8c70 | 2268 | error_setg(errp, QERR_IO_ERROR); |
0cfd6a9a LC |
2269 | goto exit; |
2270 | } | |
2271 | addr += l; | |
2272 | size -= l; | |
2273 | } | |
2274 | ||
2275 | exit: | |
2276 | fclose(f); | |
2277 | } | |
6d3962bf LC |
2278 | |
2279 | void qmp_pmemsave(int64_t addr, int64_t size, const char *filename, | |
2280 | Error **errp) | |
2281 | { | |
2282 | FILE *f; | |
2283 | uint32_t l; | |
2284 | uint8_t buf[1024]; | |
2285 | ||
2286 | f = fopen(filename, "wb"); | |
2287 | if (!f) { | |
618da851 | 2288 | error_setg_file_open(errp, errno, filename); |
6d3962bf LC |
2289 | return; |
2290 | } | |
2291 | ||
2292 | while (size != 0) { | |
2293 | l = sizeof(buf); | |
2294 | if (l > size) | |
2295 | l = size; | |
eb6282f2 | 2296 | cpu_physical_memory_read(addr, buf, l); |
6d3962bf | 2297 | if (fwrite(buf, 1, l, f) != l) { |
c6bd8c70 | 2298 | error_setg(errp, QERR_IO_ERROR); |
6d3962bf LC |
2299 | goto exit; |
2300 | } | |
2301 | addr += l; | |
2302 | size -= l; | |
2303 | } | |
2304 | ||
2305 | exit: | |
2306 | fclose(f); | |
2307 | } | |
ab49ab5c LC |
2308 | |
2309 | void qmp_inject_nmi(Error **errp) | |
2310 | { | |
9cb805fd | 2311 | nmi_monitor_handle(monitor_get_cpu_index(), errp); |
ab49ab5c | 2312 | } |
27498bef | 2313 | |
76c86615 | 2314 | void dump_drift_info(void) |
27498bef ST |
2315 | { |
2316 | if (!use_icount) { | |
2317 | return; | |
2318 | } | |
2319 | ||
76c86615 | 2320 | qemu_printf("Host - Guest clock %"PRIi64" ms\n", |
27498bef ST |
2321 | (cpu_get_clock() - cpu_get_icount())/SCALE_MS); |
2322 | if (icount_align_option) { | |
76c86615 MA |
2323 | qemu_printf("Max guest delay %"PRIi64" ms\n", |
2324 | -max_delay / SCALE_MS); | |
2325 | qemu_printf("Max guest advance %"PRIi64" ms\n", | |
2326 | max_advance / SCALE_MS); | |
27498bef | 2327 | } else { |
76c86615 MA |
2328 | qemu_printf("Max guest delay NA\n"); |
2329 | qemu_printf("Max guest advance NA\n"); | |
27498bef ST |
2330 | } |
2331 | } |