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