Skip to content

Commit d3b12f5

Browse files
committed
main-loop: create main-loop.c
Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 44a9b35 commit d3b12f5

11 files changed

+523
-454
lines changed

Makefile.objs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ common-obj-y += $(oslib-obj-y)
8181
common-obj-$(CONFIG_WIN32) += os-win32.o
8282
common-obj-$(CONFIG_POSIX) += os-posix.o
8383

84-
common-obj-y += tcg-runtime.o host-utils.o
84+
common-obj-y += tcg-runtime.o host-utils.o main-loop.o
8585
common-obj-y += irq.o input.o
8686
common-obj-$(CONFIG_PTIMER) += ptimer.o
8787
common-obj-$(CONFIG_MAX7310) += max7310.o

cpus.c

+1-188
Original file line numberDiff line numberDiff line change
@@ -542,143 +542,10 @@ static void qemu_kvm_eat_signals(CPUState *env)
542542
#endif /* !CONFIG_LINUX */
543543

544544
#ifndef _WIN32
545-
static int io_thread_fd = -1;
546-
547-
static void qemu_event_increment(void)
548-
{
549-
/* Write 8 bytes to be compatible with eventfd. */
550-
static const uint64_t val = 1;
551-
ssize_t ret;
552-
553-
if (io_thread_fd == -1) {
554-
return;
555-
}
556-
do {
557-
ret = write(io_thread_fd, &val, sizeof(val));
558-
} while (ret < 0 && errno == EINTR);
559-
560-
/* EAGAIN is fine, a read must be pending. */
561-
if (ret < 0 && errno != EAGAIN) {
562-
fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
563-
strerror(errno));
564-
exit (1);
565-
}
566-
}
567-
568-
static void qemu_event_read(void *opaque)
569-
{
570-
int fd = (intptr_t)opaque;
571-
ssize_t len;
572-
char buffer[512];
573-
574-
/* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
575-
do {
576-
len = read(fd, buffer, sizeof(buffer));
577-
} while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
578-
}
579-
580-
static int qemu_event_init(void)
581-
{
582-
int err;
583-
int fds[2];
584-
585-
err = qemu_eventfd(fds);
586-
if (err == -1) {
587-
return -errno;
588-
}
589-
err = fcntl_setfl(fds[0], O_NONBLOCK);
590-
if (err < 0) {
591-
goto fail;
592-
}
593-
err = fcntl_setfl(fds[1], O_NONBLOCK);
594-
if (err < 0) {
595-
goto fail;
596-
}
597-
qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
598-
(void *)(intptr_t)fds[0]);
599-
600-
io_thread_fd = fds[1];
601-
return 0;
602-
603-
fail:
604-
close(fds[0]);
605-
close(fds[1]);
606-
return err;
607-
}
608-
609545
static void dummy_signal(int sig)
610546
{
611547
}
612548

613-
/* If we have signalfd, we mask out the signals we want to handle and then
614-
* use signalfd to listen for them. We rely on whatever the current signal
615-
* handler is to dispatch the signals when we receive them.
616-
*/
617-
static void sigfd_handler(void *opaque)
618-
{
619-
int fd = (intptr_t)opaque;
620-
struct qemu_signalfd_siginfo info;
621-
struct sigaction action;
622-
ssize_t len;
623-
624-
while (1) {
625-
do {
626-
len = read(fd, &info, sizeof(info));
627-
} while (len == -1 && errno == EINTR);
628-
629-
if (len == -1 && errno == EAGAIN) {
630-
break;
631-
}
632-
633-
if (len != sizeof(info)) {
634-
printf("read from sigfd returned %zd: %m\n", len);
635-
return;
636-
}
637-
638-
sigaction(info.ssi_signo, NULL, &action);
639-
if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
640-
action.sa_sigaction(info.ssi_signo,
641-
(siginfo_t *)&info, NULL);
642-
} else if (action.sa_handler) {
643-
action.sa_handler(info.ssi_signo);
644-
}
645-
}
646-
}
647-
648-
static int qemu_signal_init(void)
649-
{
650-
int sigfd;
651-
sigset_t set;
652-
653-
/*
654-
* SIG_IPI must be blocked in the main thread and must not be caught
655-
* by sigwait() in the signal thread. Otherwise, the cpu thread will
656-
* not catch it reliably.
657-
*/
658-
sigemptyset(&set);
659-
sigaddset(&set, SIG_IPI);
660-
pthread_sigmask(SIG_BLOCK, &set, NULL);
661-
662-
sigemptyset(&set);
663-
sigaddset(&set, SIGIO);
664-
sigaddset(&set, SIGALRM);
665-
sigaddset(&set, SIGBUS);
666-
pthread_sigmask(SIG_BLOCK, &set, NULL);
667-
668-
sigfd = qemu_signalfd(&set);
669-
if (sigfd == -1) {
670-
fprintf(stderr, "failed to create signalfd\n");
671-
return -errno;
672-
}
673-
674-
fcntl_setfl(sigfd, O_NONBLOCK);
675-
676-
qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
677-
(void *)(intptr_t)sigfd);
678-
679-
return 0;
680-
}
681-
682549
static void qemu_kvm_init_cpu_signals(CPUState *env)
683550
{
684551
int r;
@@ -722,38 +589,6 @@ static void qemu_tcg_init_cpu_signals(void)
722589
}
723590

724591
#else /* _WIN32 */
725-
726-
HANDLE qemu_event_handle;
727-
728-
static void dummy_event_handler(void *opaque)
729-
{
730-
}
731-
732-
static int qemu_event_init(void)
733-
{
734-
qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
735-
if (!qemu_event_handle) {
736-
fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
737-
return -1;
738-
}
739-
qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
740-
return 0;
741-
}
742-
743-
static void qemu_event_increment(void)
744-
{
745-
if (!SetEvent(qemu_event_handle)) {
746-
fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
747-
GetLastError());
748-
exit (1);
749-
}
750-
}
751-
752-
static int qemu_signal_init(void)
753-
{
754-
return 0;
755-
}
756-
757592
static void qemu_kvm_init_cpu_signals(CPUState *env)
758593
{
759594
abort();
@@ -779,33 +614,16 @@ static QemuCond qemu_cpu_cond;
779614
static QemuCond qemu_pause_cond;
780615
static QemuCond qemu_work_cond;
781616

782-
int qemu_init_main_loop(void)
617+
void qemu_init_cpu_loop(void)
783618
{
784-
int ret;
785-
786619
qemu_init_sigbus();
787-
788-
ret = qemu_signal_init();
789-
if (ret) {
790-
return ret;
791-
}
792-
793-
/* Note eventfd must be drained before signalfd handlers run */
794-
ret = qemu_event_init();
795-
if (ret) {
796-
return ret;
797-
}
798-
799620
qemu_cond_init(&qemu_cpu_cond);
800621
qemu_cond_init(&qemu_pause_cond);
801622
qemu_cond_init(&qemu_work_cond);
802623
qemu_cond_init(&qemu_io_proceeded_cond);
803624
qemu_mutex_init(&qemu_global_mutex);
804-
qemu_mutex_lock(&qemu_global_mutex);
805625

806626
qemu_thread_get_self(&io_thread);
807-
808-
return 0;
809627
}
810628

811629
void qemu_main_loop_start(void)
@@ -1129,11 +947,6 @@ void qemu_init_vcpu(void *_env)
1129947
}
1130948
}
1131949

1132-
void qemu_notify_event(void)
1133-
{
1134-
qemu_event_increment();
1135-
}
1136-
1137950
void cpu_stop_current(void)
1138951
{
1139952
if (cpu_single_env) {

cpus.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define QEMU_CPUS_H
33

44
/* cpus.c */
5+
void qemu_init_cpu_loop(void);
56
void qemu_main_loop_start(void);
67
void resume_all_vcpus(void);
78
void pause_all_vcpus(void);

0 commit comments

Comments
 (0)