]>
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 | ||
25 | /* Needed early for CONFIG_BSD etc. */ | |
26 | #include "config-host.h" | |
27 | ||
83c9089e | 28 | #include "monitor/monitor.h" |
9c17d615 | 29 | #include "sysemu/sysemu.h" |
022c62cb | 30 | #include "exec/gdbstub.h" |
9c17d615 PB |
31 | #include "sysemu/dma.h" |
32 | #include "sysemu/kvm.h" | |
de0b36b6 | 33 | #include "qmp-commands.h" |
296af7c9 | 34 | |
1de7afc9 | 35 | #include "qemu/thread.h" |
9c17d615 PB |
36 | #include "sysemu/cpus.h" |
37 | #include "sysemu/qtest.h" | |
1de7afc9 PB |
38 | #include "qemu/main-loop.h" |
39 | #include "qemu/bitmap.h" | |
cb365646 | 40 | #include "qemu/seqlock.h" |
0ff0fc19 JK |
41 | |
42 | #ifndef _WIN32 | |
1de7afc9 | 43 | #include "qemu/compatfd.h" |
0ff0fc19 | 44 | #endif |
296af7c9 | 45 | |
6d9cb73c JK |
46 | #ifdef CONFIG_LINUX |
47 | ||
48 | #include <sys/prctl.h> | |
49 | ||
c0532a76 MT |
50 | #ifndef PR_MCE_KILL |
51 | #define PR_MCE_KILL 33 | |
52 | #endif | |
53 | ||
6d9cb73c JK |
54 | #ifndef PR_MCE_KILL_SET |
55 | #define PR_MCE_KILL_SET 1 | |
56 | #endif | |
57 | ||
58 | #ifndef PR_MCE_KILL_EARLY | |
59 | #define PR_MCE_KILL_EARLY 1 | |
60 | #endif | |
61 | ||
62 | #endif /* CONFIG_LINUX */ | |
63 | ||
182735ef | 64 | static CPUState *next_cpu; |
296af7c9 | 65 | |
321bc0b2 TC |
66 | bool cpu_is_stopped(CPUState *cpu) |
67 | { | |
68 | return cpu->stopped || !runstate_is_running(); | |
69 | } | |
70 | ||
a98ae1d8 | 71 | static bool cpu_thread_is_idle(CPUState *cpu) |
ac873f1e | 72 | { |
c64ca814 | 73 | if (cpu->stop || cpu->queued_work_first) { |
ac873f1e PM |
74 | return false; |
75 | } | |
321bc0b2 | 76 | if (cpu_is_stopped(cpu)) { |
ac873f1e PM |
77 | return true; |
78 | } | |
259186a7 | 79 | if (!cpu->halted || qemu_cpu_has_work(cpu) || |
215e79c0 | 80 | kvm_halt_in_kernel()) { |
ac873f1e PM |
81 | return false; |
82 | } | |
83 | return true; | |
84 | } | |
85 | ||
86 | static bool all_cpu_threads_idle(void) | |
87 | { | |
182735ef | 88 | CPUState *cpu; |
ac873f1e | 89 | |
bdc44640 | 90 | CPU_FOREACH(cpu) { |
182735ef | 91 | if (!cpu_thread_is_idle(cpu)) { |
ac873f1e PM |
92 | return false; |
93 | } | |
94 | } | |
95 | return true; | |
96 | } | |
97 | ||
946fb27c PB |
98 | /***********************************************************/ |
99 | /* guest cycle counter */ | |
100 | ||
101 | /* Conversion factor from emulated instructions to virtual clock ticks. */ | |
102 | static int icount_time_shift; | |
103 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ | |
104 | #define MAX_ICOUNT_SHIFT 10 | |
105 | /* Compensate for varying guest execution speed. */ | |
106 | static int64_t qemu_icount_bias; | |
107 | static QEMUTimer *icount_rt_timer; | |
108 | static QEMUTimer *icount_vm_timer; | |
109 | static QEMUTimer *icount_warp_timer; | |
110 | static int64_t vm_clock_warp_start; | |
111 | static int64_t qemu_icount; | |
112 | ||
113 | typedef struct TimersState { | |
cb365646 | 114 | /* Protected by BQL. */ |
946fb27c PB |
115 | int64_t cpu_ticks_prev; |
116 | int64_t cpu_ticks_offset; | |
cb365646 LPF |
117 | |
118 | /* cpu_clock_offset can be read out of BQL, so protect it with | |
119 | * this lock. | |
120 | */ | |
121 | QemuSeqLock vm_clock_seqlock; | |
946fb27c PB |
122 | int64_t cpu_clock_offset; |
123 | int32_t cpu_ticks_enabled; | |
124 | int64_t dummy; | |
125 | } TimersState; | |
126 | ||
d9cd4007 | 127 | static TimersState timers_state; |
946fb27c PB |
128 | |
129 | /* Return the virtual CPU time, based on the instruction counter. */ | |
130 | int64_t cpu_get_icount(void) | |
131 | { | |
132 | int64_t icount; | |
4917cf44 | 133 | CPUState *cpu = current_cpu; |
946fb27c PB |
134 | |
135 | icount = qemu_icount; | |
4917cf44 AF |
136 | if (cpu) { |
137 | CPUArchState *env = cpu->env_ptr; | |
946fb27c PB |
138 | if (!can_do_io(env)) { |
139 | fprintf(stderr, "Bad clock read\n"); | |
140 | } | |
141 | icount -= (env->icount_decr.u16.low + env->icount_extra); | |
142 | } | |
143 | return qemu_icount_bias + (icount << icount_time_shift); | |
144 | } | |
145 | ||
146 | /* return the host CPU cycle counter and handle stop/restart */ | |
cb365646 | 147 | /* Caller must hold the BQL */ |
946fb27c PB |
148 | int64_t cpu_get_ticks(void) |
149 | { | |
150 | if (use_icount) { | |
151 | return cpu_get_icount(); | |
152 | } | |
153 | if (!timers_state.cpu_ticks_enabled) { | |
154 | return timers_state.cpu_ticks_offset; | |
155 | } else { | |
156 | int64_t ticks; | |
157 | ticks = cpu_get_real_ticks(); | |
158 | if (timers_state.cpu_ticks_prev > ticks) { | |
159 | /* Note: non increasing ticks may happen if the host uses | |
160 | software suspend */ | |
161 | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; | |
162 | } | |
163 | timers_state.cpu_ticks_prev = ticks; | |
164 | return ticks + timers_state.cpu_ticks_offset; | |
165 | } | |
166 | } | |
167 | ||
cb365646 | 168 | static int64_t cpu_get_clock_locked(void) |
946fb27c PB |
169 | { |
170 | int64_t ti; | |
cb365646 | 171 | |
946fb27c | 172 | if (!timers_state.cpu_ticks_enabled) { |
cb365646 | 173 | ti = timers_state.cpu_clock_offset; |
946fb27c PB |
174 | } else { |
175 | ti = get_clock(); | |
cb365646 | 176 | ti += timers_state.cpu_clock_offset; |
946fb27c | 177 | } |
cb365646 LPF |
178 | |
179 | return ti; | |
180 | } | |
181 | ||
182 | /* return the host CPU monotonic timer and handle stop/restart */ | |
183 | int64_t cpu_get_clock(void) | |
184 | { | |
185 | int64_t ti; | |
186 | unsigned start; | |
187 | ||
188 | do { | |
189 | start = seqlock_read_begin(&timers_state.vm_clock_seqlock); | |
190 | ti = cpu_get_clock_locked(); | |
191 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start)); | |
192 | ||
193 | return ti; | |
946fb27c PB |
194 | } |
195 | ||
cb365646 LPF |
196 | /* enable cpu_get_ticks() |
197 | * Caller must hold BQL which server as mutex for vm_clock_seqlock. | |
198 | */ | |
946fb27c PB |
199 | void cpu_enable_ticks(void) |
200 | { | |
cb365646 LPF |
201 | /* Here, the really thing protected by seqlock is cpu_clock_offset. */ |
202 | seqlock_write_lock(&timers_state.vm_clock_seqlock); | |
946fb27c PB |
203 | if (!timers_state.cpu_ticks_enabled) { |
204 | timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); | |
205 | timers_state.cpu_clock_offset -= get_clock(); | |
206 | timers_state.cpu_ticks_enabled = 1; | |
207 | } | |
cb365646 | 208 | seqlock_write_unlock(&timers_state.vm_clock_seqlock); |
946fb27c PB |
209 | } |
210 | ||
211 | /* disable cpu_get_ticks() : the clock is stopped. You must not call | |
cb365646 LPF |
212 | * cpu_get_ticks() after that. |
213 | * Caller must hold BQL which server as mutex for vm_clock_seqlock. | |
214 | */ | |
946fb27c PB |
215 | void cpu_disable_ticks(void) |
216 | { | |
cb365646 LPF |
217 | /* Here, the really thing protected by seqlock is cpu_clock_offset. */ |
218 | seqlock_write_lock(&timers_state.vm_clock_seqlock); | |
946fb27c PB |
219 | if (timers_state.cpu_ticks_enabled) { |
220 | timers_state.cpu_ticks_offset = cpu_get_ticks(); | |
cb365646 | 221 | timers_state.cpu_clock_offset = cpu_get_clock_locked(); |
946fb27c PB |
222 | timers_state.cpu_ticks_enabled = 0; |
223 | } | |
cb365646 | 224 | seqlock_write_unlock(&timers_state.vm_clock_seqlock); |
946fb27c PB |
225 | } |
226 | ||
227 | /* Correlation between real and virtual time is always going to be | |
228 | fairly approximate, so ignore small variation. | |
229 | When the guest is idle real and virtual time will be aligned in | |
230 | the IO wait loop. */ | |
231 | #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10) | |
232 | ||
233 | static void icount_adjust(void) | |
234 | { | |
235 | int64_t cur_time; | |
236 | int64_t cur_icount; | |
237 | int64_t delta; | |
238 | static int64_t last_delta; | |
468cc7cf | 239 | |
946fb27c PB |
240 | /* If the VM is not running, then do nothing. */ |
241 | if (!runstate_is_running()) { | |
242 | return; | |
243 | } | |
468cc7cf | 244 | |
946fb27c | 245 | cur_time = cpu_get_clock(); |
468cc7cf PB |
246 | cur_icount = cpu_get_icount(); |
247 | ||
946fb27c PB |
248 | delta = cur_icount - cur_time; |
249 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ | |
250 | if (delta > 0 | |
251 | && last_delta + ICOUNT_WOBBLE < delta * 2 | |
252 | && icount_time_shift > 0) { | |
253 | /* The guest is getting too far ahead. Slow time down. */ | |
254 | icount_time_shift--; | |
255 | } | |
256 | if (delta < 0 | |
257 | && last_delta - ICOUNT_WOBBLE > delta * 2 | |
258 | && icount_time_shift < MAX_ICOUNT_SHIFT) { | |
259 | /* The guest is getting too far behind. Speed time up. */ | |
260 | icount_time_shift++; | |
261 | } | |
262 | last_delta = delta; | |
263 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); | |
264 | } | |
265 | ||
266 | static void icount_adjust_rt(void *opaque) | |
267 | { | |
40daca54 AB |
268 | timer_mod(icount_rt_timer, |
269 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000); | |
946fb27c PB |
270 | icount_adjust(); |
271 | } | |
272 | ||
273 | static void icount_adjust_vm(void *opaque) | |
274 | { | |
40daca54 AB |
275 | timer_mod(icount_vm_timer, |
276 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + | |
277 | get_ticks_per_sec() / 10); | |
946fb27c PB |
278 | icount_adjust(); |
279 | } | |
280 | ||
281 | static int64_t qemu_icount_round(int64_t count) | |
282 | { | |
283 | return (count + (1 << icount_time_shift) - 1) >> icount_time_shift; | |
284 | } | |
285 | ||
286 | static void icount_warp_rt(void *opaque) | |
287 | { | |
288 | if (vm_clock_warp_start == -1) { | |
289 | return; | |
290 | } | |
291 | ||
292 | if (runstate_is_running()) { | |
40daca54 | 293 | int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
946fb27c PB |
294 | int64_t warp_delta = clock - vm_clock_warp_start; |
295 | if (use_icount == 1) { | |
296 | qemu_icount_bias += warp_delta; | |
297 | } else { | |
298 | /* | |
40daca54 | 299 | * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too |
946fb27c PB |
300 | * far ahead of real time. |
301 | */ | |
302 | int64_t cur_time = cpu_get_clock(); | |
468cc7cf | 303 | int64_t cur_icount = cpu_get_icount(); |
946fb27c PB |
304 | int64_t delta = cur_time - cur_icount; |
305 | qemu_icount_bias += MIN(warp_delta, delta); | |
306 | } | |
40daca54 AB |
307 | if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) { |
308 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); | |
946fb27c PB |
309 | } |
310 | } | |
311 | vm_clock_warp_start = -1; | |
312 | } | |
313 | ||
8156be56 PB |
314 | void qtest_clock_warp(int64_t dest) |
315 | { | |
40daca54 | 316 | int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
8156be56 PB |
317 | assert(qtest_enabled()); |
318 | while (clock < dest) { | |
40daca54 | 319 | int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
8156be56 PB |
320 | int64_t warp = MIN(dest - clock, deadline); |
321 | qemu_icount_bias += warp; | |
40daca54 AB |
322 | qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); |
323 | clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
8156be56 | 324 | } |
40daca54 | 325 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
8156be56 PB |
326 | } |
327 | ||
40daca54 | 328 | void qemu_clock_warp(QEMUClockType type) |
946fb27c PB |
329 | { |
330 | int64_t deadline; | |
331 | ||
332 | /* | |
333 | * There are too many global variables to make the "warp" behavior | |
334 | * applicable to other clocks. But a clock argument removes the | |
335 | * need for if statements all over the place. | |
336 | */ | |
40daca54 | 337 | if (type != QEMU_CLOCK_VIRTUAL || !use_icount) { |
946fb27c PB |
338 | return; |
339 | } | |
340 | ||
341 | /* | |
40daca54 AB |
342 | * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now. |
343 | * This ensures that the deadline for the timer is computed correctly below. | |
946fb27c PB |
344 | * This also makes sure that the insn counter is synchronized before the |
345 | * CPU starts running, in case the CPU is woken by an event other than | |
40daca54 | 346 | * the earliest QEMU_CLOCK_VIRTUAL timer. |
946fb27c PB |
347 | */ |
348 | icount_warp_rt(NULL); | |
40daca54 AB |
349 | if (!all_cpu_threads_idle() || !qemu_clock_has_timers(QEMU_CLOCK_VIRTUAL)) { |
350 | timer_del(icount_warp_timer); | |
946fb27c PB |
351 | return; |
352 | } | |
353 | ||
8156be56 PB |
354 | if (qtest_enabled()) { |
355 | /* When testing, qtest commands advance icount. */ | |
356 | return; | |
357 | } | |
358 | ||
40daca54 | 359 | vm_clock_warp_start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
ac70aafc | 360 | /* We want to use the earliest deadline from ALL vm_clocks */ |
40daca54 | 361 | deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
ac70aafc AB |
362 | |
363 | /* Maintain prior (possibly buggy) behaviour where if no deadline | |
40daca54 | 364 | * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than |
ac70aafc AB |
365 | * INT32_MAX nanoseconds ahead, we still use INT32_MAX |
366 | * nanoseconds. | |
367 | */ | |
368 | if ((deadline < 0) || (deadline > INT32_MAX)) { | |
369 | deadline = INT32_MAX; | |
370 | } | |
371 | ||
946fb27c PB |
372 | if (deadline > 0) { |
373 | /* | |
40daca54 | 374 | * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to |
946fb27c PB |
375 | * sleep. Otherwise, the CPU might be waiting for a future timer |
376 | * interrupt to wake it up, but the interrupt never comes because | |
377 | * the vCPU isn't running any insns and thus doesn't advance the | |
40daca54 | 378 | * QEMU_CLOCK_VIRTUAL. |
946fb27c PB |
379 | * |
380 | * An extreme solution for this problem would be to never let VCPUs | |
40daca54 AB |
381 | * sleep in icount mode if there is a pending QEMU_CLOCK_VIRTUAL |
382 | * timer; rather time could just advance to the next QEMU_CLOCK_VIRTUAL | |
383 | * event. Instead, we do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL | |
384 | * after some e"real" time, (related to the time left until the next | |
385 | * event) has passed. The QEMU_CLOCK_REALTIME timer will do this. | |
386 | * This avoids that the warps are visible externally; for example, | |
387 | * you will not be sending network packets continuously instead of | |
388 | * every 100ms. | |
946fb27c | 389 | */ |
40daca54 | 390 | timer_mod(icount_warp_timer, vm_clock_warp_start + deadline); |
ac70aafc | 391 | } else if (deadline == 0) { |
40daca54 | 392 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
946fb27c PB |
393 | } |
394 | } | |
395 | ||
396 | static const VMStateDescription vmstate_timers = { | |
397 | .name = "timer", | |
398 | .version_id = 2, | |
399 | .minimum_version_id = 1, | |
400 | .minimum_version_id_old = 1, | |
401 | .fields = (VMStateField[]) { | |
402 | VMSTATE_INT64(cpu_ticks_offset, TimersState), | |
403 | VMSTATE_INT64(dummy, TimersState), | |
404 | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2), | |
405 | VMSTATE_END_OF_LIST() | |
406 | } | |
407 | }; | |
408 | ||
409 | void configure_icount(const char *option) | |
410 | { | |
cb365646 | 411 | seqlock_init(&timers_state.vm_clock_seqlock, NULL); |
946fb27c PB |
412 | vmstate_register(NULL, 0, &vmstate_timers, &timers_state); |
413 | if (!option) { | |
414 | return; | |
415 | } | |
416 | ||
40daca54 AB |
417 | icount_warp_timer = timer_new_ns(QEMU_CLOCK_REALTIME, |
418 | icount_warp_rt, NULL); | |
946fb27c PB |
419 | if (strcmp(option, "auto") != 0) { |
420 | icount_time_shift = strtol(option, NULL, 0); | |
421 | use_icount = 1; | |
422 | return; | |
423 | } | |
424 | ||
425 | use_icount = 2; | |
426 | ||
427 | /* 125MIPS seems a reasonable initial guess at the guest speed. | |
428 | It will be corrected fairly quickly anyway. */ | |
429 | icount_time_shift = 3; | |
430 | ||
431 | /* Have both realtime and virtual time triggers for speed adjustment. | |
432 | The realtime trigger catches emulated time passing too slowly, | |
433 | the virtual time trigger catches emulated time passing too fast. | |
434 | Realtime triggers occur even when idle, so use them less frequently | |
435 | than VM triggers. */ | |
40daca54 AB |
436 | icount_rt_timer = timer_new_ms(QEMU_CLOCK_REALTIME, |
437 | icount_adjust_rt, NULL); | |
438 | timer_mod(icount_rt_timer, | |
439 | qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000); | |
440 | icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, | |
441 | icount_adjust_vm, NULL); | |
442 | timer_mod(icount_vm_timer, | |
443 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + | |
444 | get_ticks_per_sec() / 10); | |
946fb27c PB |
445 | } |
446 | ||
296af7c9 BS |
447 | /***********************************************************/ |
448 | void hw_error(const char *fmt, ...) | |
449 | { | |
450 | va_list ap; | |
55e5c285 | 451 | CPUState *cpu; |
296af7c9 BS |
452 | |
453 | va_start(ap, fmt); | |
454 | fprintf(stderr, "qemu: hardware error: "); | |
455 | vfprintf(stderr, fmt, ap); | |
456 | fprintf(stderr, "\n"); | |
bdc44640 | 457 | CPU_FOREACH(cpu) { |
55e5c285 | 458 | fprintf(stderr, "CPU #%d:\n", cpu->cpu_index); |
878096ee | 459 | cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU); |
296af7c9 BS |
460 | } |
461 | va_end(ap); | |
462 | abort(); | |
463 | } | |
464 | ||
465 | void cpu_synchronize_all_states(void) | |
466 | { | |
182735ef | 467 | CPUState *cpu; |
296af7c9 | 468 | |
bdc44640 | 469 | CPU_FOREACH(cpu) { |
182735ef | 470 | cpu_synchronize_state(cpu); |
296af7c9 BS |
471 | } |
472 | } | |
473 | ||
474 | void cpu_synchronize_all_post_reset(void) | |
475 | { | |
182735ef | 476 | CPUState *cpu; |
296af7c9 | 477 | |
bdc44640 | 478 | CPU_FOREACH(cpu) { |
182735ef | 479 | cpu_synchronize_post_reset(cpu); |
296af7c9 BS |
480 | } |
481 | } | |
482 | ||
483 | void cpu_synchronize_all_post_init(void) | |
484 | { | |
182735ef | 485 | CPUState *cpu; |
296af7c9 | 486 | |
bdc44640 | 487 | CPU_FOREACH(cpu) { |
182735ef | 488 | cpu_synchronize_post_init(cpu); |
296af7c9 BS |
489 | } |
490 | } | |
491 | ||
56983463 | 492 | static int do_vm_stop(RunState state) |
296af7c9 | 493 | { |
56983463 KW |
494 | int ret = 0; |
495 | ||
1354869c | 496 | if (runstate_is_running()) { |
296af7c9 | 497 | cpu_disable_ticks(); |
296af7c9 | 498 | pause_all_vcpus(); |
f5bbfba1 | 499 | runstate_set(state); |
1dfb4dd9 | 500 | vm_state_notify(0, state); |
296af7c9 BS |
501 | monitor_protocol_event(QEVENT_STOP, NULL); |
502 | } | |
56983463 | 503 | |
594a45ce KW |
504 | bdrv_drain_all(); |
505 | ret = bdrv_flush_all(); | |
506 | ||
56983463 | 507 | return ret; |
296af7c9 BS |
508 | } |
509 | ||
a1fcaa73 | 510 | static bool cpu_can_run(CPUState *cpu) |
296af7c9 | 511 | { |
4fdeee7c | 512 | if (cpu->stop) { |
a1fcaa73 | 513 | return false; |
0ab07c62 | 514 | } |
321bc0b2 | 515 | if (cpu_is_stopped(cpu)) { |
a1fcaa73 | 516 | return false; |
0ab07c62 | 517 | } |
a1fcaa73 | 518 | return true; |
296af7c9 BS |
519 | } |
520 | ||
91325046 | 521 | static void cpu_handle_guest_debug(CPUState *cpu) |
83f338f7 | 522 | { |
64f6b346 | 523 | gdb_set_stop_cpu(cpu); |
8cf71710 | 524 | qemu_system_debug_request(); |
f324e766 | 525 | cpu->stopped = true; |
3c638d06 JK |
526 | } |
527 | ||
714bd040 PB |
528 | static void cpu_signal(int sig) |
529 | { | |
4917cf44 AF |
530 | if (current_cpu) { |
531 | cpu_exit(current_cpu); | |
714bd040 PB |
532 | } |
533 | exit_request = 1; | |
534 | } | |
714bd040 | 535 | |
6d9cb73c JK |
536 | #ifdef CONFIG_LINUX |
537 | static void sigbus_reraise(void) | |
538 | { | |
539 | sigset_t set; | |
540 | struct sigaction action; | |
541 | ||
542 | memset(&action, 0, sizeof(action)); | |
543 | action.sa_handler = SIG_DFL; | |
544 | if (!sigaction(SIGBUS, &action, NULL)) { | |
545 | raise(SIGBUS); | |
546 | sigemptyset(&set); | |
547 | sigaddset(&set, SIGBUS); | |
548 | sigprocmask(SIG_UNBLOCK, &set, NULL); | |
549 | } | |
550 | perror("Failed to re-raise SIGBUS!\n"); | |
551 | abort(); | |
552 | } | |
553 | ||
554 | static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo, | |
555 | void *ctx) | |
556 | { | |
557 | if (kvm_on_sigbus(siginfo->ssi_code, | |
558 | (void *)(intptr_t)siginfo->ssi_addr)) { | |
559 | sigbus_reraise(); | |
560 | } | |
561 | } | |
562 | ||
563 | static void qemu_init_sigbus(void) | |
564 | { | |
565 | struct sigaction action; | |
566 | ||
567 | memset(&action, 0, sizeof(action)); | |
568 | action.sa_flags = SA_SIGINFO; | |
569 | action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler; | |
570 | sigaction(SIGBUS, &action, NULL); | |
571 | ||
572 | prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); | |
573 | } | |
574 | ||
290adf38 | 575 | static void qemu_kvm_eat_signals(CPUState *cpu) |
1ab3c6c0 JK |
576 | { |
577 | struct timespec ts = { 0, 0 }; | |
578 | siginfo_t siginfo; | |
579 | sigset_t waitset; | |
580 | sigset_t chkset; | |
581 | int r; | |
582 | ||
583 | sigemptyset(&waitset); | |
584 | sigaddset(&waitset, SIG_IPI); | |
585 | sigaddset(&waitset, SIGBUS); | |
586 | ||
587 | do { | |
588 | r = sigtimedwait(&waitset, &siginfo, &ts); | |
589 | if (r == -1 && !(errno == EAGAIN || errno == EINTR)) { | |
590 | perror("sigtimedwait"); | |
591 | exit(1); | |
592 | } | |
593 | ||
594 | switch (r) { | |
595 | case SIGBUS: | |
290adf38 | 596 | if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) { |
1ab3c6c0 JK |
597 | sigbus_reraise(); |
598 | } | |
599 | break; | |
600 | default: | |
601 | break; | |
602 | } | |
603 | ||
604 | r = sigpending(&chkset); | |
605 | if (r == -1) { | |
606 | perror("sigpending"); | |
607 | exit(1); | |
608 | } | |
609 | } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS)); | |
1ab3c6c0 JK |
610 | } |
611 | ||
6d9cb73c JK |
612 | #else /* !CONFIG_LINUX */ |
613 | ||
614 | static void qemu_init_sigbus(void) | |
615 | { | |
616 | } | |
1ab3c6c0 | 617 | |
290adf38 | 618 | static void qemu_kvm_eat_signals(CPUState *cpu) |
1ab3c6c0 JK |
619 | { |
620 | } | |
6d9cb73c JK |
621 | #endif /* !CONFIG_LINUX */ |
622 | ||
296af7c9 | 623 | #ifndef _WIN32 |
55f8d6ac JK |
624 | static void dummy_signal(int sig) |
625 | { | |
626 | } | |
55f8d6ac | 627 | |
13618e05 | 628 | static void qemu_kvm_init_cpu_signals(CPUState *cpu) |
714bd040 PB |
629 | { |
630 | int r; | |
631 | sigset_t set; | |
632 | struct sigaction sigact; | |
633 | ||
634 | memset(&sigact, 0, sizeof(sigact)); | |
635 | sigact.sa_handler = dummy_signal; | |
636 | sigaction(SIG_IPI, &sigact, NULL); | |
637 | ||
714bd040 PB |
638 | pthread_sigmask(SIG_BLOCK, NULL, &set); |
639 | sigdelset(&set, SIG_IPI); | |
714bd040 | 640 | sigdelset(&set, SIGBUS); |
491d6e80 | 641 | r = kvm_set_signal_mask(cpu, &set); |
714bd040 PB |
642 | if (r) { |
643 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); | |
644 | exit(1); | |
645 | } | |
646 | } | |
647 | ||
648 | static void qemu_tcg_init_cpu_signals(void) | |
649 | { | |
714bd040 PB |
650 | sigset_t set; |
651 | struct sigaction sigact; | |
652 | ||
653 | memset(&sigact, 0, sizeof(sigact)); | |
654 | sigact.sa_handler = cpu_signal; | |
655 | sigaction(SIG_IPI, &sigact, NULL); | |
656 | ||
657 | sigemptyset(&set); | |
658 | sigaddset(&set, SIG_IPI); | |
659 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
714bd040 PB |
660 | } |
661 | ||
55f8d6ac | 662 | #else /* _WIN32 */ |
13618e05 | 663 | static void qemu_kvm_init_cpu_signals(CPUState *cpu) |
ff48eb5f | 664 | { |
714bd040 PB |
665 | abort(); |
666 | } | |
ff48eb5f | 667 | |
714bd040 PB |
668 | static void qemu_tcg_init_cpu_signals(void) |
669 | { | |
ff48eb5f | 670 | } |
714bd040 | 671 | #endif /* _WIN32 */ |
ff48eb5f | 672 | |
b2532d88 | 673 | static QemuMutex qemu_global_mutex; |
46daff13 PB |
674 | static QemuCond qemu_io_proceeded_cond; |
675 | static bool iothread_requesting_mutex; | |
296af7c9 BS |
676 | |
677 | static QemuThread io_thread; | |
678 | ||
679 | static QemuThread *tcg_cpu_thread; | |
680 | static QemuCond *tcg_halt_cond; | |
681 | ||
296af7c9 BS |
682 | /* cpu creation */ |
683 | static QemuCond qemu_cpu_cond; | |
684 | /* system init */ | |
296af7c9 | 685 | static QemuCond qemu_pause_cond; |
e82bcec2 | 686 | static QemuCond qemu_work_cond; |
296af7c9 | 687 | |
d3b12f5d | 688 | void qemu_init_cpu_loop(void) |
296af7c9 | 689 | { |
6d9cb73c | 690 | qemu_init_sigbus(); |
ed94592b | 691 | qemu_cond_init(&qemu_cpu_cond); |
ed94592b AL |
692 | qemu_cond_init(&qemu_pause_cond); |
693 | qemu_cond_init(&qemu_work_cond); | |
46daff13 | 694 | qemu_cond_init(&qemu_io_proceeded_cond); |
296af7c9 | 695 | qemu_mutex_init(&qemu_global_mutex); |
296af7c9 | 696 | |
b7680cb6 | 697 | qemu_thread_get_self(&io_thread); |
296af7c9 BS |
698 | } |
699 | ||
f100f0b3 | 700 | void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data) |
e82bcec2 MT |
701 | { |
702 | struct qemu_work_item wi; | |
703 | ||
60e82579 | 704 | if (qemu_cpu_is_self(cpu)) { |
e82bcec2 MT |
705 | func(data); |
706 | return; | |
707 | } | |
708 | ||
709 | wi.func = func; | |
710 | wi.data = data; | |
3c02270d | 711 | wi.free = false; |
c64ca814 AF |
712 | if (cpu->queued_work_first == NULL) { |
713 | cpu->queued_work_first = &wi; | |
0ab07c62 | 714 | } else { |
c64ca814 | 715 | cpu->queued_work_last->next = &wi; |
0ab07c62 | 716 | } |
c64ca814 | 717 | cpu->queued_work_last = &wi; |
e82bcec2 MT |
718 | wi.next = NULL; |
719 | wi.done = false; | |
720 | ||
c08d7424 | 721 | qemu_cpu_kick(cpu); |
e82bcec2 | 722 | while (!wi.done) { |
4917cf44 | 723 | CPUState *self_cpu = current_cpu; |
e82bcec2 MT |
724 | |
725 | qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex); | |
4917cf44 | 726 | current_cpu = self_cpu; |
e82bcec2 MT |
727 | } |
728 | } | |
729 | ||
3c02270d CV |
730 | void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data) |
731 | { | |
732 | struct qemu_work_item *wi; | |
733 | ||
734 | if (qemu_cpu_is_self(cpu)) { | |
735 | func(data); | |
736 | return; | |
737 | } | |
738 | ||
739 | wi = g_malloc0(sizeof(struct qemu_work_item)); | |
740 | wi->func = func; | |
741 | wi->data = data; | |
742 | wi->free = true; | |
743 | if (cpu->queued_work_first == NULL) { | |
744 | cpu->queued_work_first = wi; | |
745 | } else { | |
746 | cpu->queued_work_last->next = wi; | |
747 | } | |
748 | cpu->queued_work_last = wi; | |
749 | wi->next = NULL; | |
750 | wi->done = false; | |
751 | ||
752 | qemu_cpu_kick(cpu); | |
753 | } | |
754 | ||
6d45b109 | 755 | static void flush_queued_work(CPUState *cpu) |
e82bcec2 MT |
756 | { |
757 | struct qemu_work_item *wi; | |
758 | ||
c64ca814 | 759 | if (cpu->queued_work_first == NULL) { |
e82bcec2 | 760 | return; |
0ab07c62 | 761 | } |
e82bcec2 | 762 | |
c64ca814 AF |
763 | while ((wi = cpu->queued_work_first)) { |
764 | cpu->queued_work_first = wi->next; | |
e82bcec2 MT |
765 | wi->func(wi->data); |
766 | wi->done = true; | |
3c02270d CV |
767 | if (wi->free) { |
768 | g_free(wi); | |
769 | } | |
e82bcec2 | 770 | } |
c64ca814 | 771 | cpu->queued_work_last = NULL; |
e82bcec2 MT |
772 | qemu_cond_broadcast(&qemu_work_cond); |
773 | } | |
774 | ||
509a0d78 | 775 | static void qemu_wait_io_event_common(CPUState *cpu) |
296af7c9 | 776 | { |
4fdeee7c AF |
777 | if (cpu->stop) { |
778 | cpu->stop = false; | |
f324e766 | 779 | cpu->stopped = true; |
296af7c9 BS |
780 | qemu_cond_signal(&qemu_pause_cond); |
781 | } | |
6d45b109 | 782 | flush_queued_work(cpu); |
216fc9a4 | 783 | cpu->thread_kicked = false; |
296af7c9 BS |
784 | } |
785 | ||
6cabe1f3 | 786 | static void qemu_tcg_wait_io_event(void) |
296af7c9 | 787 | { |
182735ef | 788 | CPUState *cpu; |
6cabe1f3 | 789 | |
16400322 | 790 | while (all_cpu_threads_idle()) { |
ab33fcda PB |
791 | /* Start accounting real time to the virtual clock if the CPUs |
792 | are idle. */ | |
40daca54 | 793 | qemu_clock_warp(QEMU_CLOCK_VIRTUAL); |
9705fbb5 | 794 | qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex); |
16400322 | 795 | } |
296af7c9 | 796 | |
46daff13 PB |
797 | while (iothread_requesting_mutex) { |
798 | qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex); | |
799 | } | |
6cabe1f3 | 800 | |
bdc44640 | 801 | CPU_FOREACH(cpu) { |
182735ef | 802 | qemu_wait_io_event_common(cpu); |
6cabe1f3 | 803 | } |
296af7c9 BS |
804 | } |
805 | ||
fd529e8f | 806 | static void qemu_kvm_wait_io_event(CPUState *cpu) |
296af7c9 | 807 | { |
a98ae1d8 | 808 | while (cpu_thread_is_idle(cpu)) { |
f5c121b8 | 809 | qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); |
16400322 | 810 | } |
296af7c9 | 811 | |
290adf38 | 812 | qemu_kvm_eat_signals(cpu); |
509a0d78 | 813 | qemu_wait_io_event_common(cpu); |
296af7c9 BS |
814 | } |
815 | ||
7e97cd88 | 816 | static void *qemu_kvm_cpu_thread_fn(void *arg) |
296af7c9 | 817 | { |
48a106bd | 818 | CPUState *cpu = arg; |
84b4915d | 819 | int r; |
296af7c9 | 820 | |
6164e6d6 | 821 | qemu_mutex_lock(&qemu_global_mutex); |
814e612e | 822 | qemu_thread_get_self(cpu->thread); |
9f09e18a | 823 | cpu->thread_id = qemu_get_thread_id(); |
4917cf44 | 824 | current_cpu = cpu; |
296af7c9 | 825 | |
504134d2 | 826 | r = kvm_init_vcpu(cpu); |
84b4915d JK |
827 | if (r < 0) { |
828 | fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r)); | |
829 | exit(1); | |
830 | } | |
296af7c9 | 831 | |
13618e05 | 832 | qemu_kvm_init_cpu_signals(cpu); |
296af7c9 BS |
833 | |
834 | /* signal CPU creation */ | |
61a46217 | 835 | cpu->created = true; |
296af7c9 BS |
836 | qemu_cond_signal(&qemu_cpu_cond); |
837 | ||
296af7c9 | 838 | while (1) { |
a1fcaa73 | 839 | if (cpu_can_run(cpu)) { |
1458c363 | 840 | r = kvm_cpu_exec(cpu); |
83f338f7 | 841 | if (r == EXCP_DEBUG) { |
91325046 | 842 | cpu_handle_guest_debug(cpu); |
83f338f7 | 843 | } |
0ab07c62 | 844 | } |
fd529e8f | 845 | qemu_kvm_wait_io_event(cpu); |
296af7c9 BS |
846 | } |
847 | ||
848 | return NULL; | |
849 | } | |
850 | ||
c7f0f3b1 AL |
851 | static void *qemu_dummy_cpu_thread_fn(void *arg) |
852 | { | |
853 | #ifdef _WIN32 | |
854 | fprintf(stderr, "qtest is not supported under Windows\n"); | |
855 | exit(1); | |
856 | #else | |
10a9021d | 857 | CPUState *cpu = arg; |
c7f0f3b1 AL |
858 | sigset_t waitset; |
859 | int r; | |
860 | ||
861 | qemu_mutex_lock_iothread(); | |
814e612e | 862 | qemu_thread_get_self(cpu->thread); |
9f09e18a | 863 | cpu->thread_id = qemu_get_thread_id(); |
c7f0f3b1 AL |
864 | |
865 | sigemptyset(&waitset); | |
866 | sigaddset(&waitset, SIG_IPI); | |
867 | ||
868 | /* signal CPU creation */ | |
61a46217 | 869 | cpu->created = true; |
c7f0f3b1 AL |
870 | qemu_cond_signal(&qemu_cpu_cond); |
871 | ||
4917cf44 | 872 | current_cpu = cpu; |
c7f0f3b1 | 873 | while (1) { |
4917cf44 | 874 | current_cpu = NULL; |
c7f0f3b1 AL |
875 | qemu_mutex_unlock_iothread(); |
876 | do { | |
877 | int sig; | |
878 | r = sigwait(&waitset, &sig); | |
879 | } while (r == -1 && (errno == EAGAIN || errno == EINTR)); | |
880 | if (r == -1) { | |
881 | perror("sigwait"); | |
882 | exit(1); | |
883 | } | |
884 | qemu_mutex_lock_iothread(); | |
4917cf44 | 885 | current_cpu = cpu; |
509a0d78 | 886 | qemu_wait_io_event_common(cpu); |
c7f0f3b1 AL |
887 | } |
888 | ||
889 | return NULL; | |
890 | #endif | |
891 | } | |
892 | ||
bdb7ca67 JK |
893 | static void tcg_exec_all(void); |
894 | ||
7e97cd88 | 895 | static void *qemu_tcg_cpu_thread_fn(void *arg) |
296af7c9 | 896 | { |
c3586ba7 | 897 | CPUState *cpu = arg; |
296af7c9 | 898 | |
55f8d6ac | 899 | qemu_tcg_init_cpu_signals(); |
814e612e | 900 | qemu_thread_get_self(cpu->thread); |
296af7c9 | 901 | |
296af7c9 | 902 | qemu_mutex_lock(&qemu_global_mutex); |
38fcbd3f AF |
903 | CPU_FOREACH(cpu) { |
904 | cpu->thread_id = qemu_get_thread_id(); | |
905 | cpu->created = true; | |
906 | } | |
296af7c9 BS |
907 | qemu_cond_signal(&qemu_cpu_cond); |
908 | ||
fa7d1867 | 909 | /* wait for initial kick-off after machine start */ |
bdc44640 | 910 | while (QTAILQ_FIRST(&cpus)->stopped) { |
fa7d1867 | 911 | qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex); |
8e564b4e JK |
912 | |
913 | /* process any pending work */ | |
bdc44640 | 914 | CPU_FOREACH(cpu) { |
182735ef | 915 | qemu_wait_io_event_common(cpu); |
8e564b4e | 916 | } |
0ab07c62 | 917 | } |
296af7c9 BS |
918 | |
919 | while (1) { | |
bdb7ca67 | 920 | tcg_exec_all(); |
ac70aafc AB |
921 | |
922 | if (use_icount) { | |
40daca54 | 923 | int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
ac70aafc AB |
924 | |
925 | if (deadline == 0) { | |
40daca54 | 926 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
ac70aafc | 927 | } |
3b2319a3 | 928 | } |
6cabe1f3 | 929 | qemu_tcg_wait_io_event(); |
296af7c9 BS |
930 | } |
931 | ||
932 | return NULL; | |
933 | } | |
934 | ||
2ff09a40 | 935 | static void qemu_cpu_kick_thread(CPUState *cpu) |
cc015e9a PB |
936 | { |
937 | #ifndef _WIN32 | |
938 | int err; | |
939 | ||
814e612e | 940 | err = pthread_kill(cpu->thread->thread, SIG_IPI); |
cc015e9a PB |
941 | if (err) { |
942 | fprintf(stderr, "qemu:%s: %s", __func__, strerror(err)); | |
943 | exit(1); | |
944 | } | |
945 | #else /* _WIN32 */ | |
60e82579 | 946 | if (!qemu_cpu_is_self(cpu)) { |
ed9164a3 OH |
947 | CONTEXT tcgContext; |
948 | ||
949 | if (SuspendThread(cpu->hThread) == (DWORD)-1) { | |
7f1721df | 950 | fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__, |
ed9164a3 OH |
951 | GetLastError()); |
952 | exit(1); | |
953 | } | |
954 | ||
955 | /* On multi-core systems, we are not sure that the thread is actually | |
956 | * suspended until we can get the context. | |
957 | */ | |
958 | tcgContext.ContextFlags = CONTEXT_CONTROL; | |
959 | while (GetThreadContext(cpu->hThread, &tcgContext) != 0) { | |
960 | continue; | |
961 | } | |
962 | ||
cc015e9a | 963 | cpu_signal(0); |
ed9164a3 OH |
964 | |
965 | if (ResumeThread(cpu->hThread) == (DWORD)-1) { | |
7f1721df | 966 | fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__, |
ed9164a3 OH |
967 | GetLastError()); |
968 | exit(1); | |
969 | } | |
cc015e9a PB |
970 | } |
971 | #endif | |
972 | } | |
973 | ||
c08d7424 | 974 | void qemu_cpu_kick(CPUState *cpu) |
296af7c9 | 975 | { |
f5c121b8 | 976 | qemu_cond_broadcast(cpu->halt_cond); |
216fc9a4 | 977 | if (!tcg_enabled() && !cpu->thread_kicked) { |
2ff09a40 | 978 | qemu_cpu_kick_thread(cpu); |
216fc9a4 | 979 | cpu->thread_kicked = true; |
aa2c364b | 980 | } |
296af7c9 BS |
981 | } |
982 | ||
46d62fac | 983 | void qemu_cpu_kick_self(void) |
296af7c9 | 984 | { |
b55c22c6 | 985 | #ifndef _WIN32 |
4917cf44 | 986 | assert(current_cpu); |
296af7c9 | 987 | |
4917cf44 AF |
988 | if (!current_cpu->thread_kicked) { |
989 | qemu_cpu_kick_thread(current_cpu); | |
990 | current_cpu->thread_kicked = true; | |
296af7c9 | 991 | } |
b55c22c6 PB |
992 | #else |
993 | abort(); | |
994 | #endif | |
296af7c9 BS |
995 | } |
996 | ||
60e82579 | 997 | bool qemu_cpu_is_self(CPUState *cpu) |
296af7c9 | 998 | { |
814e612e | 999 | return qemu_thread_is_self(cpu->thread); |
296af7c9 BS |
1000 | } |
1001 | ||
aa723c23 JQ |
1002 | static bool qemu_in_vcpu_thread(void) |
1003 | { | |
4917cf44 | 1004 | return current_cpu && qemu_cpu_is_self(current_cpu); |
aa723c23 JQ |
1005 | } |
1006 | ||
296af7c9 BS |
1007 | void qemu_mutex_lock_iothread(void) |
1008 | { | |
c7f0f3b1 | 1009 | if (!tcg_enabled()) { |
296af7c9 | 1010 | qemu_mutex_lock(&qemu_global_mutex); |
1a28cac3 | 1011 | } else { |
46daff13 | 1012 | iothread_requesting_mutex = true; |
1a28cac3 | 1013 | if (qemu_mutex_trylock(&qemu_global_mutex)) { |
182735ef | 1014 | qemu_cpu_kick_thread(first_cpu); |
1a28cac3 MT |
1015 | qemu_mutex_lock(&qemu_global_mutex); |
1016 | } | |
46daff13 PB |
1017 | iothread_requesting_mutex = false; |
1018 | qemu_cond_broadcast(&qemu_io_proceeded_cond); | |
1a28cac3 | 1019 | } |
296af7c9 BS |
1020 | } |
1021 | ||
1022 | void qemu_mutex_unlock_iothread(void) | |
1023 | { | |
1024 | qemu_mutex_unlock(&qemu_global_mutex); | |
1025 | } | |
1026 | ||
1027 | static int all_vcpus_paused(void) | |
1028 | { | |
bdc44640 | 1029 | CPUState *cpu; |
296af7c9 | 1030 | |
bdc44640 | 1031 | CPU_FOREACH(cpu) { |
182735ef | 1032 | if (!cpu->stopped) { |
296af7c9 | 1033 | return 0; |
0ab07c62 | 1034 | } |
296af7c9 BS |
1035 | } |
1036 | ||
1037 | return 1; | |
1038 | } | |
1039 | ||
1040 | void pause_all_vcpus(void) | |
1041 | { | |
bdc44640 | 1042 | CPUState *cpu; |
296af7c9 | 1043 | |
40daca54 | 1044 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false); |
bdc44640 | 1045 | CPU_FOREACH(cpu) { |
182735ef AF |
1046 | cpu->stop = true; |
1047 | qemu_cpu_kick(cpu); | |
296af7c9 BS |
1048 | } |
1049 | ||
aa723c23 | 1050 | if (qemu_in_vcpu_thread()) { |
d798e974 JK |
1051 | cpu_stop_current(); |
1052 | if (!kvm_enabled()) { | |
bdc44640 | 1053 | CPU_FOREACH(cpu) { |
182735ef AF |
1054 | cpu->stop = false; |
1055 | cpu->stopped = true; | |
d798e974 JK |
1056 | } |
1057 | return; | |
1058 | } | |
1059 | } | |
1060 | ||
296af7c9 | 1061 | while (!all_vcpus_paused()) { |
be7d6c57 | 1062 | qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex); |
bdc44640 | 1063 | CPU_FOREACH(cpu) { |
182735ef | 1064 | qemu_cpu_kick(cpu); |
296af7c9 BS |
1065 | } |
1066 | } | |
1067 | } | |
1068 | ||
2993683b IM |
1069 | void cpu_resume(CPUState *cpu) |
1070 | { | |
1071 | cpu->stop = false; | |
1072 | cpu->stopped = false; | |
1073 | qemu_cpu_kick(cpu); | |
1074 | } | |
1075 | ||
296af7c9 BS |
1076 | void resume_all_vcpus(void) |
1077 | { | |
bdc44640 | 1078 | CPUState *cpu; |
296af7c9 | 1079 | |
40daca54 | 1080 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); |
bdc44640 | 1081 | CPU_FOREACH(cpu) { |
182735ef | 1082 | cpu_resume(cpu); |
296af7c9 BS |
1083 | } |
1084 | } | |
1085 | ||
e5ab30a2 | 1086 | static void qemu_tcg_init_vcpu(CPUState *cpu) |
296af7c9 | 1087 | { |
296af7c9 BS |
1088 | /* share a single thread for all cpus with TCG */ |
1089 | if (!tcg_cpu_thread) { | |
814e612e | 1090 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
f5c121b8 AF |
1091 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
1092 | qemu_cond_init(cpu->halt_cond); | |
1093 | tcg_halt_cond = cpu->halt_cond; | |
c3586ba7 | 1094 | qemu_thread_create(cpu->thread, qemu_tcg_cpu_thread_fn, cpu, |
1ecf47bf PB |
1095 | QEMU_THREAD_JOINABLE); |
1096 | #ifdef _WIN32 | |
814e612e | 1097 | cpu->hThread = qemu_thread_get_handle(cpu->thread); |
1ecf47bf | 1098 | #endif |
61a46217 | 1099 | while (!cpu->created) { |
18a85728 | 1100 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
0ab07c62 | 1101 | } |
814e612e | 1102 | tcg_cpu_thread = cpu->thread; |
296af7c9 | 1103 | } else { |
814e612e | 1104 | cpu->thread = tcg_cpu_thread; |
f5c121b8 | 1105 | cpu->halt_cond = tcg_halt_cond; |
296af7c9 BS |
1106 | } |
1107 | } | |
1108 | ||
48a106bd | 1109 | static void qemu_kvm_start_vcpu(CPUState *cpu) |
296af7c9 | 1110 | { |
814e612e | 1111 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
f5c121b8 AF |
1112 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
1113 | qemu_cond_init(cpu->halt_cond); | |
48a106bd | 1114 | qemu_thread_create(cpu->thread, qemu_kvm_cpu_thread_fn, cpu, |
1ecf47bf | 1115 | QEMU_THREAD_JOINABLE); |
61a46217 | 1116 | while (!cpu->created) { |
18a85728 | 1117 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
0ab07c62 | 1118 | } |
296af7c9 BS |
1119 | } |
1120 | ||
10a9021d | 1121 | static void qemu_dummy_start_vcpu(CPUState *cpu) |
c7f0f3b1 | 1122 | { |
814e612e | 1123 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
f5c121b8 AF |
1124 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
1125 | qemu_cond_init(cpu->halt_cond); | |
10a9021d | 1126 | qemu_thread_create(cpu->thread, qemu_dummy_cpu_thread_fn, cpu, |
c7f0f3b1 | 1127 | QEMU_THREAD_JOINABLE); |
61a46217 | 1128 | while (!cpu->created) { |
c7f0f3b1 AL |
1129 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
1130 | } | |
1131 | } | |
1132 | ||
c643bed9 | 1133 | void qemu_init_vcpu(CPUState *cpu) |
296af7c9 | 1134 | { |
ce3960eb AF |
1135 | cpu->nr_cores = smp_cores; |
1136 | cpu->nr_threads = smp_threads; | |
f324e766 | 1137 | cpu->stopped = true; |
0ab07c62 | 1138 | if (kvm_enabled()) { |
48a106bd | 1139 | qemu_kvm_start_vcpu(cpu); |
c7f0f3b1 | 1140 | } else if (tcg_enabled()) { |
e5ab30a2 | 1141 | qemu_tcg_init_vcpu(cpu); |
c7f0f3b1 | 1142 | } else { |
10a9021d | 1143 | qemu_dummy_start_vcpu(cpu); |
0ab07c62 | 1144 | } |
296af7c9 BS |
1145 | } |
1146 | ||
b4a3d965 | 1147 | void cpu_stop_current(void) |
296af7c9 | 1148 | { |
4917cf44 AF |
1149 | if (current_cpu) { |
1150 | current_cpu->stop = false; | |
1151 | current_cpu->stopped = true; | |
1152 | cpu_exit(current_cpu); | |
67bb172f | 1153 | qemu_cond_signal(&qemu_pause_cond); |
b4a3d965 | 1154 | } |
296af7c9 BS |
1155 | } |
1156 | ||
56983463 | 1157 | int vm_stop(RunState state) |
296af7c9 | 1158 | { |
aa723c23 | 1159 | if (qemu_in_vcpu_thread()) { |
1dfb4dd9 | 1160 | qemu_system_vmstop_request(state); |
296af7c9 BS |
1161 | /* |
1162 | * FIXME: should not return to device code in case | |
1163 | * vm_stop() has been requested. | |
1164 | */ | |
b4a3d965 | 1165 | cpu_stop_current(); |
56983463 | 1166 | return 0; |
296af7c9 | 1167 | } |
56983463 KW |
1168 | |
1169 | return do_vm_stop(state); | |
296af7c9 BS |
1170 | } |
1171 | ||
8a9236f1 LC |
1172 | /* does a state transition even if the VM is already stopped, |
1173 | current state is forgotten forever */ | |
56983463 | 1174 | int vm_stop_force_state(RunState state) |
8a9236f1 LC |
1175 | { |
1176 | if (runstate_is_running()) { | |
56983463 | 1177 | return vm_stop(state); |
8a9236f1 LC |
1178 | } else { |
1179 | runstate_set(state); | |
594a45ce KW |
1180 | /* Make sure to return an error if the flush in a previous vm_stop() |
1181 | * failed. */ | |
1182 | return bdrv_flush_all(); | |
8a9236f1 LC |
1183 | } |
1184 | } | |
1185 | ||
9349b4f9 | 1186 | static int tcg_cpu_exec(CPUArchState *env) |
296af7c9 BS |
1187 | { |
1188 | int ret; | |
1189 | #ifdef CONFIG_PROFILER | |
1190 | int64_t ti; | |
1191 | #endif | |
1192 | ||
1193 | #ifdef CONFIG_PROFILER | |
1194 | ti = profile_getclock(); | |
1195 | #endif | |
1196 | if (use_icount) { | |
1197 | int64_t count; | |
ac70aafc | 1198 | int64_t deadline; |
296af7c9 BS |
1199 | int decr; |
1200 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); | |
1201 | env->icount_decr.u16.low = 0; | |
1202 | env->icount_extra = 0; | |
40daca54 | 1203 | deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
ac70aafc AB |
1204 | |
1205 | /* Maintain prior (possibly buggy) behaviour where if no deadline | |
40daca54 | 1206 | * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than |
ac70aafc AB |
1207 | * INT32_MAX nanoseconds ahead, we still use INT32_MAX |
1208 | * nanoseconds. | |
1209 | */ | |
1210 | if ((deadline < 0) || (deadline > INT32_MAX)) { | |
1211 | deadline = INT32_MAX; | |
1212 | } | |
1213 | ||
1214 | count = qemu_icount_round(deadline); | |
296af7c9 BS |
1215 | qemu_icount += count; |
1216 | decr = (count > 0xffff) ? 0xffff : count; | |
1217 | count -= decr; | |
1218 | env->icount_decr.u16.low = decr; | |
1219 | env->icount_extra = count; | |
1220 | } | |
1221 | ret = cpu_exec(env); | |
1222 | #ifdef CONFIG_PROFILER | |
1223 | qemu_time += profile_getclock() - ti; | |
1224 | #endif | |
1225 | if (use_icount) { | |
1226 | /* Fold pending instructions back into the | |
1227 | instruction counter, and clear the interrupt flag. */ | |
1228 | qemu_icount -= (env->icount_decr.u16.low | |
1229 | + env->icount_extra); | |
1230 | env->icount_decr.u32 = 0; | |
1231 | env->icount_extra = 0; | |
1232 | } | |
1233 | return ret; | |
1234 | } | |
1235 | ||
bdb7ca67 | 1236 | static void tcg_exec_all(void) |
296af7c9 | 1237 | { |
9a36085b JK |
1238 | int r; |
1239 | ||
40daca54 AB |
1240 | /* Account partial waits to QEMU_CLOCK_VIRTUAL. */ |
1241 | qemu_clock_warp(QEMU_CLOCK_VIRTUAL); | |
ab33fcda | 1242 | |
0ab07c62 | 1243 | if (next_cpu == NULL) { |
296af7c9 | 1244 | next_cpu = first_cpu; |
0ab07c62 | 1245 | } |
bdc44640 | 1246 | for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) { |
182735ef AF |
1247 | CPUState *cpu = next_cpu; |
1248 | CPUArchState *env = cpu->env_ptr; | |
296af7c9 | 1249 | |
40daca54 | 1250 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, |
ed2803da | 1251 | (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0); |
296af7c9 | 1252 | |
a1fcaa73 | 1253 | if (cpu_can_run(cpu)) { |
bdb7ca67 | 1254 | r = tcg_cpu_exec(env); |
9a36085b | 1255 | if (r == EXCP_DEBUG) { |
91325046 | 1256 | cpu_handle_guest_debug(cpu); |
3c638d06 JK |
1257 | break; |
1258 | } | |
f324e766 | 1259 | } else if (cpu->stop || cpu->stopped) { |
296af7c9 BS |
1260 | break; |
1261 | } | |
1262 | } | |
c629a4bc | 1263 | exit_request = 0; |
296af7c9 BS |
1264 | } |
1265 | ||
1266 | void set_numa_modes(void) | |
1267 | { | |
1b1ed8dc | 1268 | CPUState *cpu; |
296af7c9 BS |
1269 | int i; |
1270 | ||
bdc44640 | 1271 | CPU_FOREACH(cpu) { |
296af7c9 | 1272 | for (i = 0; i < nb_numa_nodes; i++) { |
55e5c285 | 1273 | if (test_bit(cpu->cpu_index, node_cpumask[i])) { |
1b1ed8dc | 1274 | cpu->numa_node = i; |
296af7c9 BS |
1275 | } |
1276 | } | |
1277 | } | |
1278 | } | |
1279 | ||
9a78eead | 1280 | void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg) |
262353cb BS |
1281 | { |
1282 | /* XXX: implement xxx_cpu_list for targets that still miss it */ | |
e916cbf8 PM |
1283 | #if defined(cpu_list) |
1284 | cpu_list(f, cpu_fprintf); | |
262353cb BS |
1285 | #endif |
1286 | } | |
de0b36b6 LC |
1287 | |
1288 | CpuInfoList *qmp_query_cpus(Error **errp) | |
1289 | { | |
1290 | CpuInfoList *head = NULL, *cur_item = NULL; | |
182735ef | 1291 | CPUState *cpu; |
de0b36b6 | 1292 | |
bdc44640 | 1293 | CPU_FOREACH(cpu) { |
de0b36b6 | 1294 | CpuInfoList *info; |
182735ef AF |
1295 | #if defined(TARGET_I386) |
1296 | X86CPU *x86_cpu = X86_CPU(cpu); | |
1297 | CPUX86State *env = &x86_cpu->env; | |
1298 | #elif defined(TARGET_PPC) | |
1299 | PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu); | |
1300 | CPUPPCState *env = &ppc_cpu->env; | |
1301 | #elif defined(TARGET_SPARC) | |
1302 | SPARCCPU *sparc_cpu = SPARC_CPU(cpu); | |
1303 | CPUSPARCState *env = &sparc_cpu->env; | |
1304 | #elif defined(TARGET_MIPS) | |
1305 | MIPSCPU *mips_cpu = MIPS_CPU(cpu); | |
1306 | CPUMIPSState *env = &mips_cpu->env; | |
1307 | #endif | |
de0b36b6 | 1308 | |
cb446eca | 1309 | cpu_synchronize_state(cpu); |
de0b36b6 LC |
1310 | |
1311 | info = g_malloc0(sizeof(*info)); | |
1312 | info->value = g_malloc0(sizeof(*info->value)); | |
55e5c285 | 1313 | info->value->CPU = cpu->cpu_index; |
182735ef | 1314 | info->value->current = (cpu == first_cpu); |
259186a7 | 1315 | info->value->halted = cpu->halted; |
9f09e18a | 1316 | info->value->thread_id = cpu->thread_id; |
de0b36b6 LC |
1317 | #if defined(TARGET_I386) |
1318 | info->value->has_pc = true; | |
1319 | info->value->pc = env->eip + env->segs[R_CS].base; | |
1320 | #elif defined(TARGET_PPC) | |
1321 | info->value->has_nip = true; | |
1322 | info->value->nip = env->nip; | |
1323 | #elif defined(TARGET_SPARC) | |
1324 | info->value->has_pc = true; | |
1325 | info->value->pc = env->pc; | |
1326 | info->value->has_npc = true; | |
1327 | info->value->npc = env->npc; | |
1328 | #elif defined(TARGET_MIPS) | |
1329 | info->value->has_PC = true; | |
1330 | info->value->PC = env->active_tc.PC; | |
1331 | #endif | |
1332 | ||
1333 | /* XXX: waiting for the qapi to support GSList */ | |
1334 | if (!cur_item) { | |
1335 | head = cur_item = info; | |
1336 | } else { | |
1337 | cur_item->next = info; | |
1338 | cur_item = info; | |
1339 | } | |
1340 | } | |
1341 | ||
1342 | return head; | |
1343 | } | |
0cfd6a9a LC |
1344 | |
1345 | void qmp_memsave(int64_t addr, int64_t size, const char *filename, | |
1346 | bool has_cpu, int64_t cpu_index, Error **errp) | |
1347 | { | |
1348 | FILE *f; | |
1349 | uint32_t l; | |
55e5c285 | 1350 | CPUState *cpu; |
0cfd6a9a LC |
1351 | uint8_t buf[1024]; |
1352 | ||
1353 | if (!has_cpu) { | |
1354 | cpu_index = 0; | |
1355 | } | |
1356 | ||
151d1322 AF |
1357 | cpu = qemu_get_cpu(cpu_index); |
1358 | if (cpu == NULL) { | |
0cfd6a9a LC |
1359 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
1360 | "a CPU number"); | |
1361 | return; | |
1362 | } | |
1363 | ||
1364 | f = fopen(filename, "wb"); | |
1365 | if (!f) { | |
618da851 | 1366 | error_setg_file_open(errp, errno, filename); |
0cfd6a9a LC |
1367 | return; |
1368 | } | |
1369 | ||
1370 | while (size != 0) { | |
1371 | l = sizeof(buf); | |
1372 | if (l > size) | |
1373 | l = size; | |
f17ec444 | 1374 | cpu_memory_rw_debug(cpu, addr, buf, l, 0); |
0cfd6a9a LC |
1375 | if (fwrite(buf, 1, l, f) != l) { |
1376 | error_set(errp, QERR_IO_ERROR); | |
1377 | goto exit; | |
1378 | } | |
1379 | addr += l; | |
1380 | size -= l; | |
1381 | } | |
1382 | ||
1383 | exit: | |
1384 | fclose(f); | |
1385 | } | |
6d3962bf LC |
1386 | |
1387 | void qmp_pmemsave(int64_t addr, int64_t size, const char *filename, | |
1388 | Error **errp) | |
1389 | { | |
1390 | FILE *f; | |
1391 | uint32_t l; | |
1392 | uint8_t buf[1024]; | |
1393 | ||
1394 | f = fopen(filename, "wb"); | |
1395 | if (!f) { | |
618da851 | 1396 | error_setg_file_open(errp, errno, filename); |
6d3962bf LC |
1397 | return; |
1398 | } | |
1399 | ||
1400 | while (size != 0) { | |
1401 | l = sizeof(buf); | |
1402 | if (l > size) | |
1403 | l = size; | |
1404 | cpu_physical_memory_rw(addr, buf, l, 0); | |
1405 | if (fwrite(buf, 1, l, f) != l) { | |
1406 | error_set(errp, QERR_IO_ERROR); | |
1407 | goto exit; | |
1408 | } | |
1409 | addr += l; | |
1410 | size -= l; | |
1411 | } | |
1412 | ||
1413 | exit: | |
1414 | fclose(f); | |
1415 | } | |
ab49ab5c LC |
1416 | |
1417 | void qmp_inject_nmi(Error **errp) | |
1418 | { | |
1419 | #if defined(TARGET_I386) | |
182735ef AF |
1420 | CPUState *cs; |
1421 | ||
bdc44640 | 1422 | CPU_FOREACH(cs) { |
182735ef AF |
1423 | X86CPU *cpu = X86_CPU(cs); |
1424 | CPUX86State *env = &cpu->env; | |
ab49ab5c | 1425 | |
02c09195 | 1426 | if (!env->apic_state) { |
182735ef | 1427 | cpu_interrupt(cs, CPU_INTERRUPT_NMI); |
02c09195 JK |
1428 | } else { |
1429 | apic_deliver_nmi(env->apic_state); | |
1430 | } | |
ab49ab5c | 1431 | } |
7f7f9752 ED |
1432 | #elif defined(TARGET_S390X) |
1433 | CPUState *cs; | |
1434 | S390CPU *cpu; | |
1435 | ||
bdc44640 | 1436 | CPU_FOREACH(cs) { |
7f7f9752 ED |
1437 | cpu = S390_CPU(cs); |
1438 | if (cpu->env.cpu_num == monitor_get_cpu_index()) { | |
1439 | if (s390_cpu_restart(S390_CPU(cs)) == -1) { | |
1440 | error_set(errp, QERR_UNSUPPORTED); | |
1441 | return; | |
1442 | } | |
1443 | break; | |
1444 | } | |
1445 | } | |
ab49ab5c LC |
1446 | #else |
1447 | error_set(errp, QERR_UNSUPPORTED); | |
1448 | #endif | |
1449 | } |