master
c 683 lines 14.6 KB
Raw
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 #include "qemu/osdep.h"
26 #include "qemu/coroutine-tls.h"
27 #include "qapi/error.h"
28 #include "exec/gdbstub.h"
29 #include "accel/accel-cpu-ops.h"
30 #include "system/hw_accel.h"
31 #include "exec/cpu-common.h"
32 #include "qemu/thread.h"
33 #include "qemu/main-loop.h"
34 #include "qemu/plugin.h"
35 #include "system/cpus.h"
36 #include "qemu/guest-random.h"
37 #include "system/physmem.h"
38 #include "system/replay.h"
39 #include "system/runstate.h"
40 #include "system/cpu-timers.h"
41 #include "system/whpx.h"
42 #include "hw/core/boards.h"
43 #include "hw/core/hw-error.h"
44 #include "trace.h"
45
46 #ifdef CONFIG_LINUX
47
48 #include <sys/prctl.h>
49
50 #ifndef PR_MCE_KILL
51 #define PR_MCE_KILL 33
52 #endif
53
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
64 /* The Big QEMU Lock (BQL) */
65 static QemuMutex bql;
66
67 /*
68 * The chosen accelerator is supposed to register this.
69 */
70 static const AccelOpsClass *cpus_accel;
71
72 bool cpu_is_stopped(CPUState *cpu)
73 {
74 return cpu->stopped || !runstate_is_running();
75 }
76
77 bool cpu_work_list_empty(CPUState *cpu)
78 {
79 return QSIMPLEQ_EMPTY_ATOMIC(&cpu->work_list);
80 }
81
82 bool cpu_thread_is_idle(CPUState *cpu)
83 {
84 if (cpu->stop || !cpu_work_list_empty(cpu)) {
85 return false;
86 }
87 if (cpu_is_stopped(cpu)) {
88 return true;
89 }
90 if (!cpu->halted || cpu_has_work(cpu)) {
91 return false;
92 }
93 if (cpus_accel->cpu_thread_is_idle) {
94 return cpus_accel->cpu_thread_is_idle(cpu);
95 }
96 return true;
97 }
98
99 bool all_cpu_threads_idle(void)
100 {
101 CPUState *cpu;
102
103 CPU_FOREACH(cpu) {
104 if (!cpu_thread_is_idle(cpu)) {
105 return false;
106 }
107 }
108 return true;
109 }
110
111 /***********************************************************/
112 void hw_error(const char *fmt, ...)
113 {
114 va_list ap;
115 CPUState *cpu;
116
117 va_start(ap, fmt);
118 fprintf(stderr, "qemu: hardware error: ");
119 vfprintf(stderr, fmt, ap);
120 fprintf(stderr, "\n");
121 CPU_FOREACH(cpu) {
122 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
123 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
124 }
125 va_end(ap);
126 abort();
127 }
128
129 void cpu_synchronize_all_states(void)
130 {
131 CPUState *cpu;
132
133 CPU_FOREACH(cpu) {
134 cpu_synchronize_state(cpu);
135 }
136 }
137
138 void cpu_synchronize_all_post_reset(void)
139 {
140 CPUState *cpu;
141
142 CPU_FOREACH(cpu) {
143 cpu_synchronize_post_reset(cpu);
144 }
145 }
146
147 void cpu_synchronize_all_post_init(void)
148 {
149 CPUState *cpu;
150
151 CPU_FOREACH(cpu) {
152 cpu_synchronize_post_init(cpu);
153 }
154 }
155
156 void cpu_synchronize_all_pre_loadvm(void)
157 {
158 CPUState *cpu;
159
160 CPU_FOREACH(cpu) {
161 cpu_synchronize_pre_loadvm(cpu);
162 }
163 }
164
165 void cpu_synchronize_state(CPUState *cpu)
166 {
167 if (cpus_accel->synchronize_state) {
168 cpus_accel->synchronize_state(cpu);
169 }
170 }
171
172 void cpu_synchronize_post_reset(CPUState *cpu)
173 {
174 if (cpus_accel->synchronize_post_reset) {
175 cpus_accel->synchronize_post_reset(cpu);
176 }
177 }
178
179 void cpu_synchronize_post_init(CPUState *cpu)
180 {
181 if (cpus_accel->synchronize_post_init) {
182 cpus_accel->synchronize_post_init(cpu);
183 }
184 }
185
186 void cpu_synchronize_pre_loadvm(CPUState *cpu)
187 {
188 if (cpus_accel->synchronize_pre_loadvm) {
189 cpus_accel->synchronize_pre_loadvm(cpu);
190 }
191 }
192
193 bool cpus_are_resettable(void)
194 {
195 if (cpus_accel->cpus_are_resettable) {
196 return cpus_accel->cpus_are_resettable();
197 }
198 return true;
199 }
200
201 void cpu_exec_reset_hold(CPUState *cpu)
202 {
203 if (cpus_accel->cpu_reset_hold) {
204 cpus_accel->cpu_reset_hold(cpu);
205 }
206 }
207
208 int64_t cpus_get_virtual_clock(void)
209 {
210 /*
211 * XXX
212 *
213 * need to check that cpus_accel is not NULL, because qcow2 calls
214 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and
215 * with ticks disabled in some io-tests:
216 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267
217 *
218 * is this expected?
219 *
220 * XXX
221 */
222 if (cpus_accel && cpus_accel->get_virtual_clock) {
223 return cpus_accel->get_virtual_clock();
224 }
225 return cpu_get_clock();
226 }
227
228 /*
229 * Signal the new virtual time to the accelerator. This is only needed
230 * by accelerators that need to track the changes as we warp time.
231 */
232 void cpus_set_virtual_clock(int64_t new_time)
233 {
234 if (cpus_accel && cpus_accel->set_virtual_clock) {
235 cpus_accel->set_virtual_clock(new_time);
236 }
237 }
238
239 /*
240 * return the time elapsed in VM between vm_start and vm_stop. Unless
241 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle
242 * counter.
243 */
244 int64_t cpus_get_elapsed_ticks(void)
245 {
246 if (cpus_accel->get_elapsed_ticks) {
247 return cpus_accel->get_elapsed_ticks();
248 }
249 return cpu_get_ticks();
250 }
251
252 void cpu_set_interrupt(CPUState *cpu, int mask)
253 {
254 /* Pairs with cpu_test_interrupt(). */
255 qatomic_or(&cpu->interrupt_request, mask);
256 }
257
258 void generic_handle_interrupt(CPUState *cpu, int mask)
259 {
260 cpu_set_interrupt(cpu, mask);
261
262 if (!qemu_cpu_is_self(cpu)) {
263 qemu_cpu_kick(cpu);
264 }
265 }
266
267 void cpu_interrupt(CPUState *cpu, int mask)
268 {
269 g_assert(bql_locked());
270
271 cpus_accel->handle_interrupt(cpu, mask);
272 }
273
274 bool cpu_can_run(CPUState *cpu)
275 {
276 if (cpu->stop) {
277 return false;
278 }
279 if (cpu_is_stopped(cpu)) {
280 return false;
281 }
282 return true;
283 }
284
285 void cpu_handle_guest_debug(CPUState *cpu)
286 {
287 if (replay_running_debug()) {
288 if (!cpu_single_stepping(cpu)) {
289 /*
290 * Report about the breakpoint and
291 * make a single step to skip it
292 */
293 replay_breakpoint();
294 cpu_single_step(cpu, SSTEP_ENABLE);
295 } else {
296 cpu_single_step(cpu, 0);
297 }
298 } else {
299 gdb_set_stop_cpu(cpu);
300 qemu_system_debug_request();
301 cpu->stopped = true;
302 }
303 }
304
305 #ifdef CONFIG_LINUX
306 static void sigbus_reraise(void)
307 {
308 sigset_t set;
309 struct sigaction action;
310
311 memset(&action, 0, sizeof(action));
312 action.sa_handler = SIG_DFL;
313 if (!sigaction(SIGBUS, &action, NULL)) {
314 raise(SIGBUS);
315 sigemptyset(&set);
316 sigaddset(&set, SIGBUS);
317 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
318 }
319 perror("Failed to re-raise SIGBUS!");
320 abort();
321 }
322
323 static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
324 {
325 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
326 sigbus_reraise();
327 }
328
329 if (current_cpu) {
330 /* Called asynchronously in VCPU thread. */
331 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
332 sigbus_reraise();
333 }
334 } else {
335 /* Called synchronously (via signalfd) in main thread. */
336 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
337 sigbus_reraise();
338 }
339 }
340 }
341
342 static void qemu_init_sigbus(void)
343 {
344 struct sigaction action;
345
346 /*
347 * ALERT: when modifying this, take care that SIGBUS forwarding in
348 * qemu_prealloc_mem() will continue working as expected.
349 */
350 memset(&action, 0, sizeof(action));
351 action.sa_flags = SA_SIGINFO;
352 action.sa_sigaction = sigbus_handler;
353 sigaction(SIGBUS, &action, NULL);
354
355 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
356 }
357 #else /* !CONFIG_LINUX */
358 static void qemu_init_sigbus(void)
359 {
360 }
361 #endif /* !CONFIG_LINUX */
362
363 static QemuThread io_thread;
364
365 /* cpu creation */
366 static QemuCond qemu_cpu_cond;
367 /* system init */
368 static QemuCond qemu_pause_cond;
369
370 void qemu_init_cpu_loop(void)
371 {
372 qemu_init_sigbus();
373 qemu_cond_init(&qemu_cpu_cond);
374 qemu_cond_init(&qemu_pause_cond);
375 qemu_mutex_init(&bql);
376
377 qemu_thread_get_self(&io_thread);
378 }
379
380 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
381 {
382 do_run_on_cpu(cpu, func, data, &bql);
383 }
384
385 static void qemu_cpu_stop(CPUState *cpu, bool exit)
386 {
387 g_assert(qemu_cpu_is_self(cpu));
388 cpu->stop = false;
389 cpu->stopped = true;
390 if (exit) {
391 cpu_exit(cpu);
392 }
393 qemu_cond_broadcast(&qemu_pause_cond);
394 }
395
396 void qemu_process_cpu_events_common(CPUState *cpu)
397 {
398 qatomic_set_mb(&cpu->thread_kicked, false);
399 if (cpu->stop) {
400 qemu_cpu_stop(cpu, false);
401 }
402 process_queued_cpu_work(cpu);
403 }
404
405 void qemu_process_cpu_events(CPUState *cpu)
406 {
407 bool slept = false;
408
409 qatomic_set(&cpu->exit_request, false);
410 while (cpu_thread_is_idle(cpu)) {
411 if (!slept) {
412 slept = true;
413 qemu_plugin_vcpu_idle_cb(cpu);
414 }
415 qemu_cond_wait(cpu->halt_cond, &bql);
416 }
417 if (slept) {
418 qemu_plugin_vcpu_resume_cb(cpu);
419 }
420
421 qemu_process_cpu_events_common(cpu);
422 }
423
424 void cpus_kick_thread(CPUState *cpu)
425 {
426 if (qatomic_read(&cpu->thread_kicked)) {
427 return;
428 }
429 qatomic_set(&cpu->thread_kicked, true);
430
431 #ifndef _WIN32
432 int err = pthread_kill(cpu->thread->thread, SIG_IPI);
433 if (err && err != ESRCH) {
434 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
435 exit(1);
436 }
437 #else
438 qemu_sem_post(&cpu->sem);
439 #endif
440 }
441
442 void qemu_cpu_kick(CPUState *cpu)
443 {
444 qemu_cond_broadcast(cpu->halt_cond);
445 if (cpus_accel->kick_vcpu_thread) {
446 cpus_accel->kick_vcpu_thread(cpu);
447 } else { /* default */
448 cpus_kick_thread(cpu);
449 }
450 }
451
452 void qemu_cpu_kick_self(void)
453 {
454 assert(current_cpu);
455 cpus_kick_thread(current_cpu);
456 }
457
458 bool qemu_cpu_is_self(CPUState *cpu)
459 {
460 return qemu_thread_is_self(cpu->thread);
461 }
462
463 bool qemu_in_vcpu_thread(void)
464 {
465 return current_cpu && qemu_cpu_is_self(current_cpu);
466 }
467
468 QEMU_DEFINE_STATIC_CO_TLS(bool, bql_locked)
469
470 bool mutex_is_bql(QemuMutex *mutex)
471 {
472 return mutex == &bql;
473 }
474
475 void bql_update_status(bool locked)
476 {
477 /* This function should only be used when an update happened.. */
478 assert(bql_locked() != locked);
479 set_bql_locked(locked);
480 }
481
482 static uint32_t bql_unlock_blocked;
483
484 void bql_block_unlock(bool increase)
485 {
486 uint32_t new_value;
487
488 assert(bql_locked());
489
490 /* check for overflow! */
491 new_value = bql_unlock_blocked + increase - !increase;
492 assert((new_value > bql_unlock_blocked) == increase);
493 bql_unlock_blocked = new_value;
494 }
495
496 bool bql_locked(void)
497 {
498 return get_bql_locked();
499 }
500
501 bool qemu_in_main_thread(void)
502 {
503 return bql_locked();
504 }
505
506 void rust_bql_mock_lock(void)
507 {
508 error_report("This function should be used only from tests");
509 abort();
510 }
511
512 /*
513 * The BQL is taken from so many places that it is worth profiling the
514 * callers directly, instead of funneling them all through a single function.
515 */
516 void bql_lock_impl(const char *file, int line)
517 {
518 QemuMutexLockFunc bql_lock_fn = qatomic_read(&bql_mutex_lock_func);
519
520 g_assert(!bql_locked());
521 bql_lock_fn(&bql, file, line);
522 }
523
524 void bql_unlock(void)
525 {
526 g_assert(bql_locked());
527 g_assert(!bql_unlock_blocked);
528 qemu_mutex_unlock(&bql);
529 }
530
531 void qemu_cond_wait_bql(QemuCond *cond)
532 {
533 qemu_cond_wait(cond, &bql);
534 }
535
536 void qemu_cond_timedwait_bql(QemuCond *cond, int ms)
537 {
538 qemu_cond_timedwait(cond, &bql, ms);
539 }
540
541 /* signal CPU creation */
542 void cpu_thread_signal_created(CPUState *cpu)
543 {
544 cpu->created = true;
545 qemu_cond_signal(&qemu_cpu_cond);
546 }
547
548 /* signal CPU destruction */
549 void cpu_thread_signal_destroyed(CPUState *cpu)
550 {
551 cpu->created = false;
552 qemu_cond_signal(&qemu_cpu_cond);
553 }
554
555 void cpu_pause(CPUState *cpu)
556 {
557 if (qemu_cpu_is_self(cpu)) {
558 qemu_cpu_stop(cpu, true);
559 } else {
560 cpu->stop = true;
561 cpu_exit(cpu);
562 }
563 }
564
565 void cpu_resume(CPUState *cpu)
566 {
567 cpu->exception_index = -1;
568 cpu->stop = false;
569 cpu->stopped = false;
570 qemu_cpu_kick(cpu);
571 }
572
573 static bool all_vcpus_paused(void)
574 {
575 CPUState *cpu;
576
577 CPU_FOREACH(cpu) {
578 if (!cpu->stopped) {
579 return false;
580 }
581 }
582
583 return true;
584 }
585
586 void pause_all_vcpus(void)
587 {
588 CPUState *cpu;
589
590 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
591 CPU_FOREACH(cpu) {
592 cpu_pause(cpu);
593 }
594
595 /* We need to drop the replay_lock so any vCPU threads woken up
596 * can finish their replay tasks
597 */
598 replay_mutex_unlock();
599
600 while (!all_vcpus_paused()) {
601 qemu_cond_wait(&qemu_pause_cond, &bql);
602 /* FIXME: is this needed? */
603 CPU_FOREACH(cpu) {
604 qemu_cpu_kick(cpu);
605 }
606 }
607
608 bql_unlock();
609 replay_mutex_lock();
610 bql_lock();
611 }
612
613 void resume_all_vcpus(void)
614 {
615 CPUState *cpu;
616
617 if (!runstate_is_running()) {
618 return;
619 }
620
621 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
622 CPU_FOREACH(cpu) {
623 cpu_resume(cpu);
624 }
625 }
626
627 void cpu_remove_sync(CPUState *cpu)
628 {
629 cpu->stop = true;
630 cpu->unplug = true;
631 cpu_exit(cpu);
632 bql_unlock();
633 qemu_thread_join(cpu->thread);
634 bql_lock();
635 }
636
637 void cpus_register_accel(const AccelOpsClass *ops)
638 {
639 assert(ops != NULL);
640 assert(ops->create_vcpu_thread != NULL); /* mandatory */
641 assert(ops->handle_interrupt);
642
643 cpus_accel = ops;
644 }
645
646 const AccelOpsClass *cpus_get_accel(void)
647 {
648 /* broken if we call this early */
649 assert(cpus_accel);
650 return cpus_accel;
651 }
652
653 void qemu_init_vcpu(CPUState *cpu)
654 {
655 MachineState *ms = MACHINE(qdev_get_machine());
656
657 cpu->nr_threads = ms->smp.threads;
658 cpu->stopped = true;
659 cpu->random_seed = qemu_guest_random_seed_thread_part1();
660
661 if (!cpu->as) {
662 /* If the target cpu hasn't set up any address spaces itself,
663 * give it the default one.
664 */
665 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
666 }
667
668 /* accelerators all implement the AccelOpsClass */
669 g_assert(cpus_accel != NULL && cpus_accel->create_vcpu_thread != NULL);
670 cpus_accel->create_vcpu_thread(cpu);
671
672 while (!cpu->created) {
673 qemu_cond_wait(&qemu_cpu_cond, &bql);
674 }
675 }
676
677 void cpu_stop_current(void)
678 {
679 if (current_cpu) {
680 current_cpu->stop = true;
681 cpu_exit(current_cpu);
682 }
683 }