]> Git Repo - qemu.git/blob - cpus.c
Refactor signal setup functions in cpus.c
[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 #ifdef CONFIG_IOTHREAD
227 static void dummy_signal(int sig)
228 {
229 }
230 #endif
231
232 #else /* _WIN32 */
233
234 HANDLE qemu_event_handle;
235
236 static void dummy_event_handler(void *opaque)
237 {
238 }
239
240 static int qemu_event_init(void)
241 {
242     qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
243     if (!qemu_event_handle) {
244         fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
245         return -1;
246     }
247     qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
248     return 0;
249 }
250
251 static void qemu_event_increment(void)
252 {
253     if (!SetEvent(qemu_event_handle)) {
254         fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
255                 GetLastError());
256         exit (1);
257     }
258 }
259 #endif /* _WIN32 */
260
261 #ifndef CONFIG_IOTHREAD
262 int qemu_init_main_loop(void)
263 {
264     cpu_set_debug_excp_handler(cpu_debug_handler);
265
266     return qemu_event_init();
267 }
268
269 void qemu_main_loop_start(void)
270 {
271 }
272
273 void qemu_init_vcpu(void *_env)
274 {
275     CPUState *env = _env;
276     int r;
277
278     env->nr_cores = smp_cores;
279     env->nr_threads = smp_threads;
280
281     if (kvm_enabled()) {
282         r = kvm_init_vcpu(env);
283         if (r < 0) {
284             fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
285             exit(1);
286         }
287     }
288 }
289
290 int qemu_cpu_self(void *env)
291 {
292     return 1;
293 }
294
295 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
296 {
297     func(data);
298 }
299
300 void resume_all_vcpus(void)
301 {
302 }
303
304 void pause_all_vcpus(void)
305 {
306 }
307
308 void qemu_cpu_kick(void *env)
309 {
310     return;
311 }
312
313 void qemu_notify_event(void)
314 {
315     CPUState *env = cpu_single_env;
316
317     qemu_event_increment ();
318     if (env) {
319         cpu_exit(env);
320     }
321     if (next_cpu && env != next_cpu) {
322         cpu_exit(next_cpu);
323     }
324     exit_request = 1;
325 }
326
327 void qemu_mutex_lock_iothread(void) {}
328 void qemu_mutex_unlock_iothread(void) {}
329
330 void cpu_stop_current(void)
331 {
332 }
333
334 void vm_stop(int reason)
335 {
336     do_vm_stop(reason);
337 }
338
339 #else /* CONFIG_IOTHREAD */
340
341 #include "qemu-thread.h"
342
343 QemuMutex qemu_global_mutex;
344 static QemuMutex qemu_fair_mutex;
345
346 static QemuThread io_thread;
347
348 static QemuThread *tcg_cpu_thread;
349 static QemuCond *tcg_halt_cond;
350
351 static int qemu_system_ready;
352 /* cpu creation */
353 static QemuCond qemu_cpu_cond;
354 /* system init */
355 static QemuCond qemu_system_cond;
356 static QemuCond qemu_pause_cond;
357 static QemuCond qemu_work_cond;
358
359 /* If we have signalfd, we mask out the signals we want to handle and then
360  * use signalfd to listen for them.  We rely on whatever the current signal
361  * handler is to dispatch the signals when we receive them.
362  */
363 static void sigfd_handler(void *opaque)
364 {
365     int fd = (unsigned long) opaque;
366     struct qemu_signalfd_siginfo info;
367     struct sigaction action;
368     ssize_t len;
369
370     while (1) {
371         do {
372             len = read(fd, &info, sizeof(info));
373         } while (len == -1 && errno == EINTR);
374
375         if (len == -1 && errno == EAGAIN) {
376             break;
377         }
378
379         if (len != sizeof(info)) {
380             printf("read from sigfd returned %zd: %m\n", len);
381             return;
382         }
383
384         sigaction(info.ssi_signo, NULL, &action);
385         if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
386             action.sa_sigaction(info.ssi_signo,
387                                 (siginfo_t *)&info, NULL);
388         } else if (action.sa_handler) {
389             action.sa_handler(info.ssi_signo);
390         }
391     }
392 }
393
394 static void cpu_signal(int sig)
395 {
396     if (cpu_single_env) {
397         cpu_exit(cpu_single_env);
398     }
399     exit_request = 1;
400 }
401
402 static void qemu_kvm_init_cpu_signals(CPUState *env)
403 {
404     int r;
405     sigset_t set;
406     struct sigaction sigact;
407
408     memset(&sigact, 0, sizeof(sigact));
409     sigact.sa_handler = dummy_signal;
410     sigaction(SIG_IPI, &sigact, NULL);
411
412     pthread_sigmask(SIG_BLOCK, NULL, &set);
413     sigdelset(&set, SIG_IPI);
414     sigdelset(&set, SIGBUS);
415     r = kvm_set_signal_mask(env, &set);
416     if (r) {
417         fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
418         exit(1);
419     }
420 }
421
422 static void qemu_tcg_init_cpu_signals(void)
423 {
424     sigset_t set;
425     struct sigaction sigact;
426
427     memset(&sigact, 0, sizeof(sigact));
428     sigact.sa_handler = cpu_signal;
429     sigaction(SIG_IPI, &sigact, NULL);
430
431     sigemptyset(&set);
432     sigaddset(&set, SIG_IPI);
433     pthread_sigmask(SIG_UNBLOCK, &set, NULL);
434 }
435
436 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
437                            void *ctx);
438
439 static sigset_t block_io_signals(void)
440 {
441     sigset_t set;
442     struct sigaction action;
443
444     /* SIGUSR2 used by posix-aio-compat.c */
445     sigemptyset(&set);
446     sigaddset(&set, SIGUSR2);
447     pthread_sigmask(SIG_UNBLOCK, &set, NULL);
448
449     sigemptyset(&set);
450     sigaddset(&set, SIGIO);
451     sigaddset(&set, SIGALRM);
452     sigaddset(&set, SIG_IPI);
453     sigaddset(&set, SIGBUS);
454     pthread_sigmask(SIG_BLOCK, &set, NULL);
455
456     memset(&action, 0, sizeof(action));
457     action.sa_flags = SA_SIGINFO;
458     action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
459     sigaction(SIGBUS, &action, NULL);
460     prctl(PR_MCE_KILL, 1, 1, 0, 0);
461
462     return set;
463 }
464
465 static int qemu_signalfd_init(sigset_t mask)
466 {
467     int sigfd;
468
469     sigfd = qemu_signalfd(&mask);
470     if (sigfd == -1) {
471         fprintf(stderr, "failed to create signalfd\n");
472         return -errno;
473     }
474
475     fcntl_setfl(sigfd, O_NONBLOCK);
476
477     qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
478                          (void *)(unsigned long) sigfd);
479
480     return 0;
481 }
482
483 int qemu_init_main_loop(void)
484 {
485     int ret;
486     sigset_t blocked_signals;
487
488     cpu_set_debug_excp_handler(cpu_debug_handler);
489
490     blocked_signals = block_io_signals();
491
492     ret = qemu_signalfd_init(blocked_signals);
493     if (ret)
494         return ret;
495
496     /* Note eventfd must be drained before signalfd handlers run */
497     ret = qemu_event_init();
498     if (ret)
499         return ret;
500
501     qemu_cond_init(&qemu_pause_cond);
502     qemu_cond_init(&qemu_system_cond);
503     qemu_mutex_init(&qemu_fair_mutex);
504     qemu_mutex_init(&qemu_global_mutex);
505     qemu_mutex_lock(&qemu_global_mutex);
506
507     qemu_thread_self(&io_thread);
508
509     return 0;
510 }
511
512 void qemu_main_loop_start(void)
513 {
514     qemu_system_ready = 1;
515     qemu_cond_broadcast(&qemu_system_cond);
516 }
517
518 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
519 {
520     struct qemu_work_item wi;
521
522     if (qemu_cpu_self(env)) {
523         func(data);
524         return;
525     }
526
527     wi.func = func;
528     wi.data = data;
529     if (!env->queued_work_first)
530         env->queued_work_first = &wi;
531     else
532         env->queued_work_last->next = &wi;
533     env->queued_work_last = &wi;
534     wi.next = NULL;
535     wi.done = false;
536
537     qemu_cpu_kick(env);
538     while (!wi.done) {
539         CPUState *self_env = cpu_single_env;
540
541         qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
542         cpu_single_env = self_env;
543     }
544 }
545
546 static void flush_queued_work(CPUState *env)
547 {
548     struct qemu_work_item *wi;
549
550     if (!env->queued_work_first)
551         return;
552
553     while ((wi = env->queued_work_first)) {
554         env->queued_work_first = wi->next;
555         wi->func(wi->data);
556         wi->done = true;
557     }
558     env->queued_work_last = NULL;
559     qemu_cond_broadcast(&qemu_work_cond);
560 }
561
562 static void qemu_wait_io_event_common(CPUState *env)
563 {
564     if (env->stop) {
565         env->stop = 0;
566         env->stopped = 1;
567         qemu_cond_signal(&qemu_pause_cond);
568     }
569     flush_queued_work(env);
570     env->thread_kicked = false;
571 }
572
573 static void qemu_tcg_wait_io_event(void)
574 {
575     CPUState *env;
576
577     while (!any_cpu_has_work())
578         qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
579
580     qemu_mutex_unlock(&qemu_global_mutex);
581
582     /*
583      * Users of qemu_global_mutex can be starved, having no chance
584      * to acquire it since this path will get to it first.
585      * So use another lock to provide fairness.
586      */
587     qemu_mutex_lock(&qemu_fair_mutex);
588     qemu_mutex_unlock(&qemu_fair_mutex);
589
590     qemu_mutex_lock(&qemu_global_mutex);
591
592     for (env = first_cpu; env != NULL; env = env->next_cpu) {
593         qemu_wait_io_event_common(env);
594     }
595 }
596
597 static void sigbus_reraise(void)
598 {
599     sigset_t set;
600     struct sigaction action;
601
602     memset(&action, 0, sizeof(action));
603     action.sa_handler = SIG_DFL;
604     if (!sigaction(SIGBUS, &action, NULL)) {
605         raise(SIGBUS);
606         sigemptyset(&set);
607         sigaddset(&set, SIGBUS);
608         sigprocmask(SIG_UNBLOCK, &set, NULL);
609     }
610     perror("Failed to re-raise SIGBUS!\n");
611     abort();
612 }
613
614 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
615                            void *ctx)
616 {
617     if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr)) {
618         sigbus_reraise();
619     }
620 }
621
622 static void qemu_kvm_eat_signal(CPUState *env, int timeout)
623 {
624     struct timespec ts;
625     int r, e;
626     siginfo_t siginfo;
627     sigset_t waitset;
628     sigset_t chkset;
629
630     ts.tv_sec = timeout / 1000;
631     ts.tv_nsec = (timeout % 1000) * 1000000;
632
633     sigemptyset(&waitset);
634     sigaddset(&waitset, SIG_IPI);
635     sigaddset(&waitset, SIGBUS);
636
637     do {
638         qemu_mutex_unlock(&qemu_global_mutex);
639
640         r = sigtimedwait(&waitset, &siginfo, &ts);
641         e = errno;
642
643         qemu_mutex_lock(&qemu_global_mutex);
644
645         if (r == -1 && !(e == EAGAIN || e == EINTR)) {
646             fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
647             exit(1);
648         }
649
650         switch (r) {
651         case SIGBUS:
652             if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
653                 sigbus_reraise();
654             }
655             break;
656         default:
657             break;
658         }
659
660         r = sigpending(&chkset);
661         if (r == -1) {
662             fprintf(stderr, "sigpending: %s\n", strerror(e));
663             exit(1);
664         }
665     } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
666 }
667
668 static void qemu_kvm_wait_io_event(CPUState *env)
669 {
670     while (!cpu_has_work(env))
671         qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
672
673     qemu_kvm_eat_signal(env, 0);
674     qemu_wait_io_event_common(env);
675 }
676
677 static int qemu_cpu_exec(CPUState *env);
678
679 static void *kvm_cpu_thread_fn(void *arg)
680 {
681     CPUState *env = arg;
682     int r;
683
684     qemu_mutex_lock(&qemu_global_mutex);
685     qemu_thread_self(env->thread);
686
687     r = kvm_init_vcpu(env);
688     if (r < 0) {
689         fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
690         exit(1);
691     }
692
693     qemu_kvm_init_cpu_signals(env);
694
695     /* signal CPU creation */
696     env->created = 1;
697     qemu_cond_signal(&qemu_cpu_cond);
698
699     /* and wait for machine initialization */
700     while (!qemu_system_ready)
701         qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
702
703     while (1) {
704         if (cpu_can_run(env))
705             qemu_cpu_exec(env);
706         qemu_kvm_wait_io_event(env);
707     }
708
709     return NULL;
710 }
711
712 static void *tcg_cpu_thread_fn(void *arg)
713 {
714     CPUState *env = arg;
715
716     qemu_tcg_init_cpu_signals();
717     qemu_thread_self(env->thread);
718
719     /* signal CPU creation */
720     qemu_mutex_lock(&qemu_global_mutex);
721     for (env = first_cpu; env != NULL; env = env->next_cpu)
722         env->created = 1;
723     qemu_cond_signal(&qemu_cpu_cond);
724
725     /* and wait for machine initialization */
726     while (!qemu_system_ready)
727         qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
728
729     while (1) {
730         cpu_exec_all();
731         qemu_tcg_wait_io_event();
732     }
733
734     return NULL;
735 }
736
737 void qemu_cpu_kick(void *_env)
738 {
739     CPUState *env = _env;
740     qemu_cond_broadcast(env->halt_cond);
741     if (!env->thread_kicked) {
742         qemu_thread_signal(env->thread, SIG_IPI);
743         env->thread_kicked = true;
744     }
745 }
746
747 int qemu_cpu_self(void *_env)
748 {
749     CPUState *env = _env;
750     QemuThread this;
751
752     qemu_thread_self(&this);
753
754     return qemu_thread_equal(&this, env->thread);
755 }
756
757 void qemu_mutex_lock_iothread(void)
758 {
759     if (kvm_enabled()) {
760         qemu_mutex_lock(&qemu_global_mutex);
761     } else {
762         qemu_mutex_lock(&qemu_fair_mutex);
763         if (qemu_mutex_trylock(&qemu_global_mutex)) {
764             qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
765             qemu_mutex_lock(&qemu_global_mutex);
766         }
767         qemu_mutex_unlock(&qemu_fair_mutex);
768     }
769 }
770
771 void qemu_mutex_unlock_iothread(void)
772 {
773     qemu_mutex_unlock(&qemu_global_mutex);
774 }
775
776 static int all_vcpus_paused(void)
777 {
778     CPUState *penv = first_cpu;
779
780     while (penv) {
781         if (!penv->stopped)
782             return 0;
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_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
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 tcg_init_vcpu(void *_env)
822 {
823     CPUState *env = _env;
824     /* share a single thread for all cpus with TCG */
825     if (!tcg_cpu_thread) {
826         env->thread = qemu_mallocz(sizeof(QemuThread));
827         env->halt_cond = qemu_mallocz(sizeof(QemuCond));
828         qemu_cond_init(env->halt_cond);
829         qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
830         while (env->created == 0)
831             qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
832         tcg_cpu_thread = env->thread;
833         tcg_halt_cond = env->halt_cond;
834     } else {
835         env->thread = tcg_cpu_thread;
836         env->halt_cond = tcg_halt_cond;
837     }
838 }
839
840 static void kvm_start_vcpu(CPUState *env)
841 {
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, kvm_cpu_thread_fn, env);
846     while (env->created == 0)
847         qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
848 }
849
850 void qemu_init_vcpu(void *_env)
851 {
852     CPUState *env = _env;
853
854     env->nr_cores = smp_cores;
855     env->nr_threads = smp_threads;
856     if (kvm_enabled())
857         kvm_start_vcpu(env);
858     else
859         tcg_init_vcpu(env);
860 }
861
862 void qemu_notify_event(void)
863 {
864     qemu_event_increment();
865 }
866
867 static void qemu_system_vmstop_request(int reason)
868 {
869     vmstop_requested = reason;
870     qemu_notify_event();
871 }
872
873 void cpu_stop_current(void)
874 {
875     if (cpu_single_env) {
876         cpu_single_env->stopped = 1;
877         cpu_exit(cpu_single_env);
878     }
879 }
880
881 void vm_stop(int reason)
882 {
883     QemuThread me;
884     qemu_thread_self(&me);
885
886     if (!qemu_thread_equal(&me, &io_thread)) {
887         qemu_system_vmstop_request(reason);
888         /*
889          * FIXME: should not return to device code in case
890          * vm_stop() has been requested.
891          */
892         cpu_stop_current();
893         return;
894     }
895     do_vm_stop(reason);
896 }
897
898 #endif
899
900 static int qemu_cpu_exec(CPUState *env)
901 {
902     int ret;
903 #ifdef CONFIG_PROFILER
904     int64_t ti;
905 #endif
906
907 #ifdef CONFIG_PROFILER
908     ti = profile_getclock();
909 #endif
910     if (use_icount) {
911         int64_t count;
912         int decr;
913         qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
914         env->icount_decr.u16.low = 0;
915         env->icount_extra = 0;
916         count = qemu_icount_round (qemu_next_deadline());
917         qemu_icount += count;
918         decr = (count > 0xffff) ? 0xffff : count;
919         count -= decr;
920         env->icount_decr.u16.low = decr;
921         env->icount_extra = count;
922     }
923     ret = cpu_exec(env);
924 #ifdef CONFIG_PROFILER
925     qemu_time += profile_getclock() - ti;
926 #endif
927     if (use_icount) {
928         /* Fold pending instructions back into the
929            instruction counter, and clear the interrupt flag.  */
930         qemu_icount -= (env->icount_decr.u16.low
931                         + env->icount_extra);
932         env->icount_decr.u32 = 0;
933         env->icount_extra = 0;
934     }
935     return ret;
936 }
937
938 bool cpu_exec_all(void)
939 {
940     if (next_cpu == NULL)
941         next_cpu = first_cpu;
942     for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
943         CPUState *env = next_cpu;
944
945         qemu_clock_enable(vm_clock,
946                           (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
947
948         if (qemu_alarm_pending())
949             break;
950         if (cpu_can_run(env)) {
951             if (qemu_cpu_exec(env) == EXCP_DEBUG) {
952                 break;
953             }
954         } else if (env->stop) {
955             break;
956         }
957     }
958     exit_request = 0;
959     return any_cpu_has_work();
960 }
961
962 void set_numa_modes(void)
963 {
964     CPUState *env;
965     int i;
966
967     for (env = first_cpu; env != NULL; env = env->next_cpu) {
968         for (i = 0; i < nb_numa_nodes; i++) {
969             if (node_cpumask[i] & (1 << env->cpu_index)) {
970                 env->numa_node = i;
971             }
972         }
973     }
974 }
975
976 void set_cpu_log(const char *optarg)
977 {
978     int mask;
979     const CPULogItem *item;
980
981     mask = cpu_str_to_log_mask(optarg);
982     if (!mask) {
983         printf("Log items (comma separated):\n");
984         for (item = cpu_log_items; item->mask != 0; item++) {
985             printf("%-10s %s\n", item->name, item->help);
986         }
987         exit(1);
988     }
989     cpu_set_log(mask);
990 }
991
992 /* Return the virtual CPU time, based on the instruction counter.  */
993 int64_t cpu_get_icount(void)
994 {
995     int64_t icount;
996     CPUState *env = cpu_single_env;;
997
998     icount = qemu_icount;
999     if (env) {
1000         if (!can_do_io(env)) {
1001             fprintf(stderr, "Bad clock read\n");
1002         }
1003         icount -= (env->icount_decr.u16.low + env->icount_extra);
1004     }
1005     return qemu_icount_bias + (icount << icount_time_shift);
1006 }
1007
1008 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1009 {
1010     /* XXX: implement xxx_cpu_list for targets that still miss it */
1011 #if defined(cpu_list_id)
1012     cpu_list_id(f, cpu_fprintf, optarg);
1013 #elif defined(cpu_list)
1014     cpu_list(f, cpu_fprintf); /* deprecated */
1015 #endif
1016 }
This page took 0.075415 seconds and 4 git commands to generate.