[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v10 02/22] softmmu: split off vl.c:main() into main.c
From: |
Alexander Bulekov |
Subject: |
[PATCH v10 02/22] softmmu: split off vl.c:main() into main.c |
Date: |
Wed, 19 Feb 2020 23:10:58 -0500 |
A program might rely on functions implemented in vl.c, but implement its
own main(). By placing main into a separate source file, there are no
complaints about duplicate main()s when linking against vl.o. For
example, the virtual-device fuzzer uses a main() provided by libfuzzer,
and needs to perform some initialization before running the softmmu
initialization. Now, main simply calls three vl.c functions which
handle the guest initialization, main loop and cleanup.
Signed-off-by: Alexander Bulekov <address@hidden>
Reviewed-by: Stefan Hajnoczi <address@hidden>
Reviewed-by: Darren Kenny <address@hidden>
---
MAINTAINERS | 1 +
Makefile.target | 2 +-
include/sysemu/sysemu.h | 4 ++++
softmmu/Makefile.objs | 1 +
softmmu/main.c | 53 +++++++++++++++++++++++++++++++++++++++++
softmmu/vl.c | 36 +++++++---------------------
6 files changed, 69 insertions(+), 28 deletions(-)
create mode 100644 softmmu/main.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 98cbeaab97..a8e2a5f8c7 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2024,6 +2024,7 @@ F: include/sysemu/runstate.h
F: util/main-loop.c
F: util/qemu-timer.c
F: softmmu/vl.c
+F: softmmu/main.c
F: qapi/run-state.json
Human Monitor (HMP)
diff --git a/Makefile.target b/Makefile.target
index 06c36d1161..6f4dd72022 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -203,7 +203,7 @@ endif
COMMON_LDADDS = ../libqemuutil.a
# build either PROG or PROGW
-$(QEMU_PROG_BUILD): $(all-obj-y) $(COMMON_LDADDS)
+$(QEMU_PROG_BUILD): $(all-obj-y) $(COMMON_LDADDS) $(softmmu-main-y)
$(call LINK, $(filter-out %.mak, $^))
ifdef CONFIG_DARWIN
$(call quiet-command,Rez -append $(SRC_PATH)/pc-bios/qemu.rsrc -o
$@,"REZ","$(TARGET_DIR)$@")
diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
index 6358a324a7..3e81a1a79c 100644
--- a/include/sysemu/sysemu.h
+++ b/include/sysemu/sysemu.h
@@ -116,6 +116,10 @@ QemuOpts *qemu_get_machine_opts(void);
bool defaults_enabled(void);
+void qemu_init(int argc, char **argv, char **envp);
+void qemu_main_loop(void);
+void qemu_cleanup(void);
+
extern QemuOptsList qemu_legacy_drive_opts;
extern QemuOptsList qemu_common_drive_opts;
extern QemuOptsList qemu_drive_opts;
diff --git a/softmmu/Makefile.objs b/softmmu/Makefile.objs
index d80a5ffe5a..dd15c24346 100644
--- a/softmmu/Makefile.objs
+++ b/softmmu/Makefile.objs
@@ -1,2 +1,3 @@
+softmmu-main-y = softmmu/main.o
obj-y += vl.o
vl.o-cflags := $(GPROF_CFLAGS) $(SDL_CFLAGS)
diff --git a/softmmu/main.c b/softmmu/main.c
new file mode 100644
index 0000000000..7adc530c73
--- /dev/null
+++ b/softmmu/main.c
@@ -0,0 +1,53 @@
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2003-2020 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "sysemu/sysemu.h"
+
+#ifdef CONFIG_SDL
+#if defined(__APPLE__) || defined(main)
+#include <SDL.h>
+int main(int argc, char **argv)
+{
+ return qemu_main(argc, argv, NULL);
+}
+#undef main
+#define main qemu_main
+#endif
+#endif /* CONFIG_SDL */
+
+#ifdef CONFIG_COCOA
+#undef main
+#define main qemu_main
+#endif /* CONFIG_COCOA */
+
+int main(int argc, char **argv, char **envp)
+{
+ qemu_init(argc, argv, envp);
+ qemu_main_loop();
+ qemu_cleanup();
+
+ return 0;
+}
diff --git a/softmmu/vl.c b/softmmu/vl.c
index 7dcb0879c4..46a48d09df 100644
--- a/softmmu/vl.c
+++ b/softmmu/vl.c
@@ -36,25 +36,6 @@
#include "sysemu/seccomp.h"
#include "sysemu/tcg.h"
-#ifdef CONFIG_SDL
-#if defined(__APPLE__) || defined(main)
-#include <SDL.h>
-int qemu_main(int argc, char **argv, char **envp);
-int main(int argc, char **argv)
-{
- return qemu_main(argc, argv, NULL);
-}
-#undef main
-#define main qemu_main
-#endif
-#endif /* CONFIG_SDL */
-
-#ifdef CONFIG_COCOA
-#undef main
-#define main qemu_main
-#endif /* CONFIG_COCOA */
-
-
#include "qemu/error-report.h"
#include "qemu/sockets.h"
#include "sysemu/accel.h"
@@ -1671,7 +1652,7 @@ static bool main_loop_should_exit(void)
return false;
}
-static void main_loop(void)
+void qemu_main_loop(void)
{
#ifdef CONFIG_PROFILER
int64_t ti;
@@ -2839,7 +2820,7 @@ static void configure_accelerators(const char *progname)
}
}
-int main(int argc, char **argv, char **envp)
+void qemu_init(int argc, char **argv, char **envp)
{
int i;
int snapshot, linux_boot;
@@ -3391,7 +3372,7 @@ int main(int argc, char **argv, char **envp)
case QEMU_OPTION_watchdog:
if (watchdog) {
error_report("only one watchdog option may be given");
- return 1;
+ exit(1);
}
watchdog = optarg;
break;
@@ -4300,7 +4281,7 @@ int main(int argc, char **argv, char **envp)
parse_numa_opts(current_machine);
/* do monitor/qmp handling at preconfig state if requested */
- main_loop();
+ qemu_main_loop();
audio_init_audiodevs();
@@ -4418,7 +4399,7 @@ int main(int argc, char **argv, char **envp)
if (vmstate_dump_file) {
/* dump and exit */
dump_vmstate_json_to_file(vmstate_dump_file);
- return 0;
+ exit(0);
}
if (incoming) {
@@ -4435,8 +4416,11 @@ int main(int argc, char **argv, char **envp)
accel_setup_post(current_machine);
os_setup_post();
- main_loop();
+ return;
+}
+void qemu_cleanup(void)
+{
gdbserver_cleanup();
/*
@@ -4473,6 +4457,4 @@ int main(int argc, char **argv, char **envp)
qemu_chr_cleanup();
user_creatable_cleanup();
/* TODO: unref root container, check all devices are ok */
-
- return 0;
}
--
2.25.0
- [PATCH v10 00/22] Add virtual device fuzzing support, Alexander Bulekov, 2020/02/19
- [PATCH v10 01/22] softmmu: move vl.c to softmmu/, Alexander Bulekov, 2020/02/19
- [PATCH v10 03/22] module: check module wasn't already initialized, Alexander Bulekov, 2020/02/19
- [PATCH v10 02/22] softmmu: split off vl.c:main() into main.c,
Alexander Bulekov <=
- [PATCH v10 04/22] fuzz: add FUZZ_TARGET module type, Alexander Bulekov, 2020/02/19
- [PATCH v10 06/22] libqtest: add a layer of abstraction to send/recv, Alexander Bulekov, 2020/02/19
- [PATCH v10 07/22] libqtest: make bufwrite rely on the TransportOps, Alexander Bulekov, 2020/02/19
- [PATCH v10 08/22] qtest: add in-process incoming command handler, Alexander Bulekov, 2020/02/19
- [PATCH v10 05/22] qtest: add qtest_server_send abstraction, Alexander Bulekov, 2020/02/19
- [PATCH v10 09/22] libqos: rename i2c_send and i2c_recv, Alexander Bulekov, 2020/02/19
- [PATCH v10 10/22] libqos: split qos-test and libqos makefile vars, Alexander Bulekov, 2020/02/19
- [PATCH v10 11/22] libqos: move useful qos-test funcs to qos_external, Alexander Bulekov, 2020/02/19
- [PATCH v10 13/22] exec: keep ram block across fork when using qtest, Alexander Bulekov, 2020/02/19
- [PATCH v10 12/22] fuzz: add fuzzer skeleton, Alexander Bulekov, 2020/02/19