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