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