]> git.proxmox.com Git - mirror_qemu.git/blob - cpus.c
RunState: Add additional states
[mirror_qemu.git] / cpus.c
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
28 #include "monitor.h"
29 #include "sysemu.h"
30 #include "gdbstub.h"
31 #include "dma.h"
32 #include "kvm.h"
33
34 #include "qemu-thread.h"
35 #include "cpus.h"
36
37 #ifndef _WIN32
38 #include "compatfd.h"
39 #endif
40
41 #ifdef SIGRTMIN
42 #define SIG_IPI (SIGRTMIN+4)
43 #else
44 #define SIG_IPI SIGUSR1
45 #endif
46
47 #ifdef CONFIG_LINUX
48
49 #include <sys/prctl.h>
50
51 #ifndef PR_MCE_KILL
52 #define PR_MCE_KILL 33
53 #endif
54
55 #ifndef PR_MCE_KILL_SET
56 #define PR_MCE_KILL_SET 1
57 #endif
58
59 #ifndef PR_MCE_KILL_EARLY
60 #define PR_MCE_KILL_EARLY 1
61 #endif
62
63 #endif /* CONFIG_LINUX */
64
65 static CPUState *next_cpu;
66
67 /***********************************************************/
68 void hw_error(const char *fmt, ...)
69 {
70 va_list ap;
71 CPUState *env;
72
73 va_start(ap, fmt);
74 fprintf(stderr, "qemu: hardware error: ");
75 vfprintf(stderr, fmt, ap);
76 fprintf(stderr, "\n");
77 for(env = first_cpu; env != NULL; env = env->next_cpu) {
78 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
79 #ifdef TARGET_I386
80 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
81 #else
82 cpu_dump_state(env, stderr, fprintf, 0);
83 #endif
84 }
85 va_end(ap);
86 abort();
87 }
88
89 void cpu_synchronize_all_states(void)
90 {
91 CPUState *cpu;
92
93 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
94 cpu_synchronize_state(cpu);
95 }
96 }
97
98 void cpu_synchronize_all_post_reset(void)
99 {
100 CPUState *cpu;
101
102 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
103 cpu_synchronize_post_reset(cpu);
104 }
105 }
106
107 void cpu_synchronize_all_post_init(void)
108 {
109 CPUState *cpu;
110
111 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
112 cpu_synchronize_post_init(cpu);
113 }
114 }
115
116 int cpu_is_stopped(CPUState *env)
117 {
118 return !vm_running || env->stopped;
119 }
120
121 static void do_vm_stop(RunState state)
122 {
123 if (vm_running) {
124 cpu_disable_ticks();
125 vm_running = 0;
126 pause_all_vcpus();
127 runstate_set(state);
128 vm_state_notify(0, state);
129 qemu_aio_flush();
130 bdrv_flush_all();
131 monitor_protocol_event(QEVENT_STOP, NULL);
132 }
133 }
134
135 static int cpu_can_run(CPUState *env)
136 {
137 if (env->stop) {
138 return 0;
139 }
140 if (env->stopped || !vm_running) {
141 return 0;
142 }
143 return 1;
144 }
145
146 static bool cpu_thread_is_idle(CPUState *env)
147 {
148 if (env->stop || env->queued_work_first) {
149 return false;
150 }
151 if (env->stopped || !vm_running) {
152 return true;
153 }
154 if (!env->halted || qemu_cpu_has_work(env) ||
155 (kvm_enabled() && kvm_irqchip_in_kernel())) {
156 return false;
157 }
158 return true;
159 }
160
161 bool all_cpu_threads_idle(void)
162 {
163 CPUState *env;
164
165 for (env = first_cpu; env != NULL; env = env->next_cpu) {
166 if (!cpu_thread_is_idle(env)) {
167 return false;
168 }
169 }
170 return true;
171 }
172
173 static void cpu_handle_guest_debug(CPUState *env)
174 {
175 gdb_set_stop_cpu(env);
176 qemu_system_debug_request();
177 env->stopped = 1;
178 }
179
180 static void cpu_signal(int sig)
181 {
182 if (cpu_single_env) {
183 cpu_exit(cpu_single_env);
184 }
185 exit_request = 1;
186 }
187
188 #ifdef CONFIG_LINUX
189 static void sigbus_reraise(void)
190 {
191 sigset_t set;
192 struct sigaction action;
193
194 memset(&action, 0, sizeof(action));
195 action.sa_handler = SIG_DFL;
196 if (!sigaction(SIGBUS, &action, NULL)) {
197 raise(SIGBUS);
198 sigemptyset(&set);
199 sigaddset(&set, SIGBUS);
200 sigprocmask(SIG_UNBLOCK, &set, NULL);
201 }
202 perror("Failed to re-raise SIGBUS!\n");
203 abort();
204 }
205
206 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
207 void *ctx)
208 {
209 if (kvm_on_sigbus(siginfo->ssi_code,
210 (void *)(intptr_t)siginfo->ssi_addr)) {
211 sigbus_reraise();
212 }
213 }
214
215 static void qemu_init_sigbus(void)
216 {
217 struct sigaction action;
218
219 memset(&action, 0, sizeof(action));
220 action.sa_flags = SA_SIGINFO;
221 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
222 sigaction(SIGBUS, &action, NULL);
223
224 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
225 }
226
227 static void qemu_kvm_eat_signals(CPUState *env)
228 {
229 struct timespec ts = { 0, 0 };
230 siginfo_t siginfo;
231 sigset_t waitset;
232 sigset_t chkset;
233 int r;
234
235 sigemptyset(&waitset);
236 sigaddset(&waitset, SIG_IPI);
237 sigaddset(&waitset, SIGBUS);
238
239 do {
240 r = sigtimedwait(&waitset, &siginfo, &ts);
241 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
242 perror("sigtimedwait");
243 exit(1);
244 }
245
246 switch (r) {
247 case SIGBUS:
248 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
249 sigbus_reraise();
250 }
251 break;
252 default:
253 break;
254 }
255
256 r = sigpending(&chkset);
257 if (r == -1) {
258 perror("sigpending");
259 exit(1);
260 }
261 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
262 }
263
264 #else /* !CONFIG_LINUX */
265
266 static void qemu_init_sigbus(void)
267 {
268 }
269
270 static void qemu_kvm_eat_signals(CPUState *env)
271 {
272 }
273 #endif /* !CONFIG_LINUX */
274
275 #ifndef _WIN32
276 static int io_thread_fd = -1;
277
278 static void qemu_event_increment(void)
279 {
280 /* Write 8 bytes to be compatible with eventfd. */
281 static const uint64_t val = 1;
282 ssize_t ret;
283
284 if (io_thread_fd == -1) {
285 return;
286 }
287 do {
288 ret = write(io_thread_fd, &val, sizeof(val));
289 } while (ret < 0 && errno == EINTR);
290
291 /* EAGAIN is fine, a read must be pending. */
292 if (ret < 0 && errno != EAGAIN) {
293 fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
294 strerror(errno));
295 exit (1);
296 }
297 }
298
299 static void qemu_event_read(void *opaque)
300 {
301 int fd = (intptr_t)opaque;
302 ssize_t len;
303 char buffer[512];
304
305 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
306 do {
307 len = read(fd, buffer, sizeof(buffer));
308 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
309 }
310
311 static int qemu_event_init(void)
312 {
313 int err;
314 int fds[2];
315
316 err = qemu_eventfd(fds);
317 if (err == -1) {
318 return -errno;
319 }
320 err = fcntl_setfl(fds[0], O_NONBLOCK);
321 if (err < 0) {
322 goto fail;
323 }
324 err = fcntl_setfl(fds[1], O_NONBLOCK);
325 if (err < 0) {
326 goto fail;
327 }
328 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
329 (void *)(intptr_t)fds[0]);
330
331 io_thread_fd = fds[1];
332 return 0;
333
334 fail:
335 close(fds[0]);
336 close(fds[1]);
337 return err;
338 }
339
340 static void dummy_signal(int sig)
341 {
342 }
343
344 /* If we have signalfd, we mask out the signals we want to handle and then
345 * use signalfd to listen for them. We rely on whatever the current signal
346 * handler is to dispatch the signals when we receive them.
347 */
348 static void sigfd_handler(void *opaque)
349 {
350 int fd = (intptr_t)opaque;
351 struct qemu_signalfd_siginfo info;
352 struct sigaction action;
353 ssize_t len;
354
355 while (1) {
356 do {
357 len = read(fd, &info, sizeof(info));
358 } while (len == -1 && errno == EINTR);
359
360 if (len == -1 && errno == EAGAIN) {
361 break;
362 }
363
364 if (len != sizeof(info)) {
365 printf("read from sigfd returned %zd: %m\n", len);
366 return;
367 }
368
369 sigaction(info.ssi_signo, NULL, &action);
370 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
371 action.sa_sigaction(info.ssi_signo,
372 (siginfo_t *)&info, NULL);
373 } else if (action.sa_handler) {
374 action.sa_handler(info.ssi_signo);
375 }
376 }
377 }
378
379 static int qemu_signal_init(void)
380 {
381 int sigfd;
382 sigset_t set;
383
384 /* SIGUSR2 used by posix-aio-compat.c */
385 sigemptyset(&set);
386 sigaddset(&set, SIGUSR2);
387 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
388
389 /*
390 * SIG_IPI must be blocked in the main thread and must not be caught
391 * by sigwait() in the signal thread. Otherwise, the cpu thread will
392 * not catch it reliably.
393 */
394 sigemptyset(&set);
395 sigaddset(&set, SIG_IPI);
396 pthread_sigmask(SIG_BLOCK, &set, NULL);
397
398 sigemptyset(&set);
399 sigaddset(&set, SIGIO);
400 sigaddset(&set, SIGALRM);
401 sigaddset(&set, SIGBUS);
402 pthread_sigmask(SIG_BLOCK, &set, NULL);
403
404 sigfd = qemu_signalfd(&set);
405 if (sigfd == -1) {
406 fprintf(stderr, "failed to create signalfd\n");
407 return -errno;
408 }
409
410 fcntl_setfl(sigfd, O_NONBLOCK);
411
412 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
413 (void *)(intptr_t)sigfd);
414
415 return 0;
416 }
417
418 static void qemu_kvm_init_cpu_signals(CPUState *env)
419 {
420 int r;
421 sigset_t set;
422 struct sigaction sigact;
423
424 memset(&sigact, 0, sizeof(sigact));
425 sigact.sa_handler = dummy_signal;
426 sigaction(SIG_IPI, &sigact, NULL);
427
428 pthread_sigmask(SIG_BLOCK, NULL, &set);
429 sigdelset(&set, SIG_IPI);
430 sigdelset(&set, SIGBUS);
431 r = kvm_set_signal_mask(env, &set);
432 if (r) {
433 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
434 exit(1);
435 }
436
437 sigdelset(&set, SIG_IPI);
438 sigdelset(&set, SIGBUS);
439 r = kvm_set_signal_mask(env, &set);
440 if (r) {
441 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
442 exit(1);
443 }
444 }
445
446 static void qemu_tcg_init_cpu_signals(void)
447 {
448 sigset_t set;
449 struct sigaction sigact;
450
451 memset(&sigact, 0, sizeof(sigact));
452 sigact.sa_handler = cpu_signal;
453 sigaction(SIG_IPI, &sigact, NULL);
454
455 sigemptyset(&set);
456 sigaddset(&set, SIG_IPI);
457 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
458 }
459
460 #else /* _WIN32 */
461
462 HANDLE qemu_event_handle;
463
464 static void dummy_event_handler(void *opaque)
465 {
466 }
467
468 static int qemu_event_init(void)
469 {
470 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
471 if (!qemu_event_handle) {
472 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
473 return -1;
474 }
475 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
476 return 0;
477 }
478
479 static void qemu_event_increment(void)
480 {
481 if (!SetEvent(qemu_event_handle)) {
482 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
483 GetLastError());
484 exit (1);
485 }
486 }
487
488 static int qemu_signal_init(void)
489 {
490 return 0;
491 }
492
493 static void qemu_kvm_init_cpu_signals(CPUState *env)
494 {
495 abort();
496 }
497
498 static void qemu_tcg_init_cpu_signals(void)
499 {
500 }
501 #endif /* _WIN32 */
502
503 QemuMutex qemu_global_mutex;
504 static QemuCond qemu_io_proceeded_cond;
505 static bool iothread_requesting_mutex;
506
507 static QemuThread io_thread;
508
509 static QemuThread *tcg_cpu_thread;
510 static QemuCond *tcg_halt_cond;
511
512 /* cpu creation */
513 static QemuCond qemu_cpu_cond;
514 /* system init */
515 static QemuCond qemu_pause_cond;
516 static QemuCond qemu_work_cond;
517
518 int qemu_init_main_loop(void)
519 {
520 int ret;
521
522 qemu_init_sigbus();
523
524 ret = qemu_signal_init();
525 if (ret) {
526 return ret;
527 }
528
529 /* Note eventfd must be drained before signalfd handlers run */
530 ret = qemu_event_init();
531 if (ret) {
532 return ret;
533 }
534
535 qemu_cond_init(&qemu_cpu_cond);
536 qemu_cond_init(&qemu_pause_cond);
537 qemu_cond_init(&qemu_work_cond);
538 qemu_cond_init(&qemu_io_proceeded_cond);
539 qemu_mutex_init(&qemu_global_mutex);
540 qemu_mutex_lock(&qemu_global_mutex);
541
542 qemu_thread_get_self(&io_thread);
543
544 return 0;
545 }
546
547 void qemu_main_loop_start(void)
548 {
549 resume_all_vcpus();
550 }
551
552 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
553 {
554 struct qemu_work_item wi;
555
556 if (qemu_cpu_is_self(env)) {
557 func(data);
558 return;
559 }
560
561 wi.func = func;
562 wi.data = data;
563 if (!env->queued_work_first) {
564 env->queued_work_first = &wi;
565 } else {
566 env->queued_work_last->next = &wi;
567 }
568 env->queued_work_last = &wi;
569 wi.next = NULL;
570 wi.done = false;
571
572 qemu_cpu_kick(env);
573 while (!wi.done) {
574 CPUState *self_env = cpu_single_env;
575
576 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
577 cpu_single_env = self_env;
578 }
579 }
580
581 static void flush_queued_work(CPUState *env)
582 {
583 struct qemu_work_item *wi;
584
585 if (!env->queued_work_first) {
586 return;
587 }
588
589 while ((wi = env->queued_work_first)) {
590 env->queued_work_first = wi->next;
591 wi->func(wi->data);
592 wi->done = true;
593 }
594 env->queued_work_last = NULL;
595 qemu_cond_broadcast(&qemu_work_cond);
596 }
597
598 static void qemu_wait_io_event_common(CPUState *env)
599 {
600 if (env->stop) {
601 env->stop = 0;
602 env->stopped = 1;
603 qemu_cond_signal(&qemu_pause_cond);
604 }
605 flush_queued_work(env);
606 env->thread_kicked = false;
607 }
608
609 static void qemu_tcg_wait_io_event(void)
610 {
611 CPUState *env;
612
613 while (all_cpu_threads_idle()) {
614 /* Start accounting real time to the virtual clock if the CPUs
615 are idle. */
616 qemu_clock_warp(vm_clock);
617 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
618 }
619
620 while (iothread_requesting_mutex) {
621 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
622 }
623
624 for (env = first_cpu; env != NULL; env = env->next_cpu) {
625 qemu_wait_io_event_common(env);
626 }
627 }
628
629 static void qemu_kvm_wait_io_event(CPUState *env)
630 {
631 while (cpu_thread_is_idle(env)) {
632 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
633 }
634
635 qemu_kvm_eat_signals(env);
636 qemu_wait_io_event_common(env);
637 }
638
639 static void *qemu_kvm_cpu_thread_fn(void *arg)
640 {
641 CPUState *env = arg;
642 int r;
643
644 qemu_mutex_lock(&qemu_global_mutex);
645 qemu_thread_get_self(env->thread);
646 env->thread_id = qemu_get_thread_id();
647
648 r = kvm_init_vcpu(env);
649 if (r < 0) {
650 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
651 exit(1);
652 }
653
654 qemu_kvm_init_cpu_signals(env);
655
656 /* signal CPU creation */
657 env->created = 1;
658 qemu_cond_signal(&qemu_cpu_cond);
659
660 while (1) {
661 if (cpu_can_run(env)) {
662 r = kvm_cpu_exec(env);
663 if (r == EXCP_DEBUG) {
664 cpu_handle_guest_debug(env);
665 }
666 }
667 qemu_kvm_wait_io_event(env);
668 }
669
670 return NULL;
671 }
672
673 static void *qemu_tcg_cpu_thread_fn(void *arg)
674 {
675 CPUState *env = arg;
676
677 qemu_tcg_init_cpu_signals();
678 qemu_thread_get_self(env->thread);
679
680 /* signal CPU creation */
681 qemu_mutex_lock(&qemu_global_mutex);
682 for (env = first_cpu; env != NULL; env = env->next_cpu) {
683 env->thread_id = qemu_get_thread_id();
684 env->created = 1;
685 }
686 qemu_cond_signal(&qemu_cpu_cond);
687
688 /* wait for initial kick-off after machine start */
689 while (first_cpu->stopped) {
690 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
691 }
692
693 while (1) {
694 cpu_exec_all();
695 if (use_icount && qemu_next_icount_deadline() <= 0) {
696 qemu_notify_event();
697 }
698 qemu_tcg_wait_io_event();
699 }
700
701 return NULL;
702 }
703
704 static void qemu_cpu_kick_thread(CPUState *env)
705 {
706 #ifndef _WIN32
707 int err;
708
709 err = pthread_kill(env->thread->thread, SIG_IPI);
710 if (err) {
711 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
712 exit(1);
713 }
714 #else /* _WIN32 */
715 if (!qemu_cpu_is_self(env)) {
716 SuspendThread(env->thread->thread);
717 cpu_signal(0);
718 ResumeThread(env->thread->thread);
719 }
720 #endif
721 }
722
723 void qemu_cpu_kick(void *_env)
724 {
725 CPUState *env = _env;
726
727 qemu_cond_broadcast(env->halt_cond);
728 if (kvm_enabled() && !env->thread_kicked) {
729 qemu_cpu_kick_thread(env);
730 env->thread_kicked = true;
731 }
732 }
733
734 void qemu_cpu_kick_self(void)
735 {
736 #ifndef _WIN32
737 assert(cpu_single_env);
738
739 if (!cpu_single_env->thread_kicked) {
740 qemu_cpu_kick_thread(cpu_single_env);
741 cpu_single_env->thread_kicked = true;
742 }
743 #else
744 abort();
745 #endif
746 }
747
748 int qemu_cpu_is_self(void *_env)
749 {
750 CPUState *env = _env;
751
752 return qemu_thread_is_self(env->thread);
753 }
754
755 void qemu_mutex_lock_iothread(void)
756 {
757 if (kvm_enabled()) {
758 qemu_mutex_lock(&qemu_global_mutex);
759 } else {
760 iothread_requesting_mutex = true;
761 if (qemu_mutex_trylock(&qemu_global_mutex)) {
762 qemu_cpu_kick_thread(first_cpu);
763 qemu_mutex_lock(&qemu_global_mutex);
764 }
765 iothread_requesting_mutex = false;
766 qemu_cond_broadcast(&qemu_io_proceeded_cond);
767 }
768 }
769
770 void qemu_mutex_unlock_iothread(void)
771 {
772 qemu_mutex_unlock(&qemu_global_mutex);
773 }
774
775 static int all_vcpus_paused(void)
776 {
777 CPUState *penv = first_cpu;
778
779 while (penv) {
780 if (!penv->stopped) {
781 return 0;
782 }
783 penv = (CPUState *)penv->next_cpu;
784 }
785
786 return 1;
787 }
788
789 void pause_all_vcpus(void)
790 {
791 CPUState *penv = first_cpu;
792
793 while (penv) {
794 penv->stop = 1;
795 qemu_cpu_kick(penv);
796 penv = (CPUState *)penv->next_cpu;
797 }
798
799 while (!all_vcpus_paused()) {
800 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
801 penv = first_cpu;
802 while (penv) {
803 qemu_cpu_kick(penv);
804 penv = (CPUState *)penv->next_cpu;
805 }
806 }
807 }
808
809 void resume_all_vcpus(void)
810 {
811 CPUState *penv = first_cpu;
812
813 while (penv) {
814 penv->stop = 0;
815 penv->stopped = 0;
816 qemu_cpu_kick(penv);
817 penv = (CPUState *)penv->next_cpu;
818 }
819 }
820
821 static void qemu_tcg_init_vcpu(void *_env)
822 {
823 CPUState *env = _env;
824
825 /* share a single thread for all cpus with TCG */
826 if (!tcg_cpu_thread) {
827 env->thread = g_malloc0(sizeof(QemuThread));
828 env->halt_cond = g_malloc0(sizeof(QemuCond));
829 qemu_cond_init(env->halt_cond);
830 tcg_halt_cond = env->halt_cond;
831 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
832 while (env->created == 0) {
833 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
834 }
835 tcg_cpu_thread = env->thread;
836 } else {
837 env->thread = tcg_cpu_thread;
838 env->halt_cond = tcg_halt_cond;
839 }
840 }
841
842 static void qemu_kvm_start_vcpu(CPUState *env)
843 {
844 env->thread = g_malloc0(sizeof(QemuThread));
845 env->halt_cond = g_malloc0(sizeof(QemuCond));
846 qemu_cond_init(env->halt_cond);
847 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
848 while (env->created == 0) {
849 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
850 }
851 }
852
853 void qemu_init_vcpu(void *_env)
854 {
855 CPUState *env = _env;
856
857 env->nr_cores = smp_cores;
858 env->nr_threads = smp_threads;
859 env->stopped = 1;
860 if (kvm_enabled()) {
861 qemu_kvm_start_vcpu(env);
862 } else {
863 qemu_tcg_init_vcpu(env);
864 }
865 }
866
867 void qemu_notify_event(void)
868 {
869 qemu_event_increment();
870 }
871
872 void cpu_stop_current(void)
873 {
874 if (cpu_single_env) {
875 cpu_single_env->stop = 0;
876 cpu_single_env->stopped = 1;
877 cpu_exit(cpu_single_env);
878 qemu_cond_signal(&qemu_pause_cond);
879 }
880 }
881
882 void vm_stop(RunState state)
883 {
884 if (!qemu_thread_is_self(&io_thread)) {
885 qemu_system_vmstop_request(state);
886 /*
887 * FIXME: should not return to device code in case
888 * vm_stop() has been requested.
889 */
890 cpu_stop_current();
891 return;
892 }
893 do_vm_stop(state);
894 }
895
896 static int tcg_cpu_exec(CPUState *env)
897 {
898 int ret;
899 #ifdef CONFIG_PROFILER
900 int64_t ti;
901 #endif
902
903 #ifdef CONFIG_PROFILER
904 ti = profile_getclock();
905 #endif
906 if (use_icount) {
907 int64_t count;
908 int decr;
909 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
910 env->icount_decr.u16.low = 0;
911 env->icount_extra = 0;
912 count = qemu_icount_round(qemu_next_icount_deadline());
913 qemu_icount += count;
914 decr = (count > 0xffff) ? 0xffff : count;
915 count -= decr;
916 env->icount_decr.u16.low = decr;
917 env->icount_extra = count;
918 }
919 ret = cpu_exec(env);
920 #ifdef CONFIG_PROFILER
921 qemu_time += profile_getclock() - ti;
922 #endif
923 if (use_icount) {
924 /* Fold pending instructions back into the
925 instruction counter, and clear the interrupt flag. */
926 qemu_icount -= (env->icount_decr.u16.low
927 + env->icount_extra);
928 env->icount_decr.u32 = 0;
929 env->icount_extra = 0;
930 }
931 return ret;
932 }
933
934 bool cpu_exec_all(void)
935 {
936 int r;
937
938 /* Account partial waits to the vm_clock. */
939 qemu_clock_warp(vm_clock);
940
941 if (next_cpu == NULL) {
942 next_cpu = first_cpu;
943 }
944 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
945 CPUState *env = next_cpu;
946
947 qemu_clock_enable(vm_clock,
948 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
949
950 if (cpu_can_run(env)) {
951 if (kvm_enabled()) {
952 r = kvm_cpu_exec(env);
953 qemu_kvm_eat_signals(env);
954 } else {
955 r = tcg_cpu_exec(env);
956 }
957 if (r == EXCP_DEBUG) {
958 cpu_handle_guest_debug(env);
959 break;
960 }
961 } else if (env->stop || env->stopped) {
962 break;
963 }
964 }
965 exit_request = 0;
966 return !all_cpu_threads_idle();
967 }
968
969 void set_numa_modes(void)
970 {
971 CPUState *env;
972 int i;
973
974 for (env = first_cpu; env != NULL; env = env->next_cpu) {
975 for (i = 0; i < nb_numa_nodes; i++) {
976 if (node_cpumask[i] & (1 << env->cpu_index)) {
977 env->numa_node = i;
978 }
979 }
980 }
981 }
982
983 void set_cpu_log(const char *optarg)
984 {
985 int mask;
986 const CPULogItem *item;
987
988 mask = cpu_str_to_log_mask(optarg);
989 if (!mask) {
990 printf("Log items (comma separated):\n");
991 for (item = cpu_log_items; item->mask != 0; item++) {
992 printf("%-10s %s\n", item->name, item->help);
993 }
994 exit(1);
995 }
996 cpu_set_log(mask);
997 }
998
999 void set_cpu_log_filename(const char *optarg)
1000 {
1001 cpu_set_log_filename(optarg);
1002 }
1003
1004 /* Return the virtual CPU time, based on the instruction counter. */
1005 int64_t cpu_get_icount(void)
1006 {
1007 int64_t icount;
1008 CPUState *env = cpu_single_env;;
1009
1010 icount = qemu_icount;
1011 if (env) {
1012 if (!can_do_io(env)) {
1013 fprintf(stderr, "Bad clock read\n");
1014 }
1015 icount -= (env->icount_decr.u16.low + env->icount_extra);
1016 }
1017 return qemu_icount_bias + (icount << icount_time_shift);
1018 }
1019
1020 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1021 {
1022 /* XXX: implement xxx_cpu_list for targets that still miss it */
1023 #if defined(cpu_list_id)
1024 cpu_list_id(f, cpu_fprintf, optarg);
1025 #elif defined(cpu_list)
1026 cpu_list(f, cpu_fprintf); /* deprecated */
1027 #endif
1028 }