diff mbox series

[v8,01/21] softmmu: split off vl.c:main() into main.c

Message ID 20200129053357.27454-2-alxndr@bu.edu
State New
Headers show
Series [v8,01/21] softmmu: split off vl.c:main() into main.c | expand

Commit Message

Alexander Bulekov Jan. 29, 2020, 5:34 a.m. UTC
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 <alxndr@bu.edu>
---
 Makefile                |  1 +
 Makefile.objs           |  2 ++
 Makefile.target         |  2 +-
 include/sysemu/sysemu.h |  4 ++++
 main.c                  | 53 +++++++++++++++++++++++++++++++++++++++++
 vl.c                    | 36 +++++++---------------------
 6 files changed, 70 insertions(+), 28 deletions(-)
 create mode 100644 main.c

Comments

Stefan Hajnoczi Jan. 30, 2020, 2:39 p.m. UTC | #1
On Wed, Jan 29, 2020 at 05:34:11AM +0000, Bulekov, Alexander wrote:
> 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 <alxndr@bu.edu>
> ---
>  Makefile                |  1 +
>  Makefile.objs           |  2 ++
>  Makefile.target         |  2 +-
>  include/sysemu/sysemu.h |  4 ++++
>  main.c                  | 53 +++++++++++++++++++++++++++++++++++++++++
>  vl.c                    | 36 +++++++---------------------
>  6 files changed, 70 insertions(+), 28 deletions(-)
>  create mode 100644 main.c

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Alex Bennée Jan. 30, 2020, 3:06 p.m. UTC | #2
Bulekov, Alexander <alxndr@bu.edu> writes:

> 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 <alxndr@bu.edu>
> ---
<snip>
>  main.c                  | 53 +++++++++++++++++++++++++++++++++++++++++
<snip>
> --- a/Makefile.objs
> +++ b/Makefile.objs
> @@ -84,6 +84,8 @@ common-obj-$(CONFIG_FDT) += device_tree.o
>  # qapi
>  
>  common-obj-y += qapi/
> +
> +softmmu-obj-y = main.o
>  endif
>  
<snip>
> diff --git a/main.c b/main.c
> new file mode 100644
> index 0000000000..f10ceda541
> --- /dev/null
> +++ b/main.c
> @@ -0,0 +1,53 @@
> +/*
> + * QEMU System Emulator
> + *
> + * Copyright (c) 2003-2008 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;
> +}
<snip>

Can we put the main in a project appropriate sub-directory so it's on
the same order as linux-user/main.c?

I guess the new directory could be "softmmu" which matches the directory
or "system" which matches the binary name. I'd lean towards the latter
as softmmu is very specifically not this bit.
Alexander Bulekov Jan. 30, 2020, 5:44 p.m. UTC | #3
On 200130 1506, Alex Bennée wrote:
> 
> Bulekov, Alexander <alxndr@bu.edu> writes:
> 
> > 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 <alxndr@bu.edu>
> > ---
> <snip>
> >  main.c                  | 53 +++++++++++++++++++++++++++++++++++++++++
> <snip>
> > --- a/Makefile.objs
> > +++ b/Makefile.objs
> > @@ -84,6 +84,8 @@ common-obj-$(CONFIG_FDT) += device_tree.o
> >  # qapi
> >  
> >  common-obj-y += qapi/
> > +
> > +softmmu-obj-y = main.o
> >  endif
> >  
> <snip>
> > diff --git a/main.c b/main.c
> > new file mode 100644
> > index 0000000000..f10ceda541
> > --- /dev/null
> > +++ b/main.c
> > @@ -0,0 +1,53 @@
> > +/*
> > + * QEMU System Emulator
> > + *
> > + * Copyright (c) 2003-2008 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;
> > +}
> <snip>
> 
> Can we put the main in a project appropriate sub-directory so it's on
> the same order as linux-user/main.c?
> 
> I guess the new directory could be "softmmu" which matches the directory
> or "system" which matches the binary name. I'd lean towards the latter
> as softmmu is very specifically not this bit.
Will do - should vl.c move into this directory, as well?
-Alex

> -- 
> Alex Bennée
Alex Bennée Jan. 30, 2020, 6:41 p.m. UTC | #4
Alexander Bulekov <alxndr@bu.edu> writes:

> On 200130 1506, Alex Bennée wrote:
>> 
>> Bulekov, Alexander <alxndr@bu.edu> writes:
>> 
>> > 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 <alxndr@bu.edu>
>> > ---
>> <snip>
>> >  main.c                  | 53 +++++++++++++++++++++++++++++++++++++++++
>> <snip>
>> > --- a/Makefile.objs
>> > +++ b/Makefile.objs
>> > @@ -84,6 +84,8 @@ common-obj-$(CONFIG_FDT) += device_tree.o
>> >  # qapi
>> >  
>> >  common-obj-y += qapi/
>> > +
>> > +softmmu-obj-y = main.o
>> >  endif
>> >  
>> <snip>
>> > diff --git a/main.c b/main.c
>> > new file mode 100644
>> > index 0000000000..f10ceda541
>> > --- /dev/null
>> > +++ b/main.c
>> > @@ -0,0 +1,53 @@
>> > +/*
>> > + * QEMU System Emulator
>> > + *
>> > + * Copyright (c) 2003-2008 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;
>> > +}
>> <snip>
>> 
>> Can we put the main in a project appropriate sub-directory so it's on
>> the same order as linux-user/main.c?
>> 
>> I guess the new directory could be "softmmu" which matches the directory
>> or "system" which matches the binary name. I'd lean towards the latter
>> as softmmu is very specifically not this bit.
> Will do - should vl.c move into this directory, as well?
> -Alex

Might as well so it doesn't get lonely ;-)

There has been a slow process of moving bits and pieces of the root
directory into a respective sub-directories over the years (decades?) so
this is a good opportunity to clean this bit up.

>
>> -- 
>> Alex Bennée
Darren Kenny Feb. 5, 2020, 2:02 p.m. UTC | #5
On Wed, Jan 29, 2020 at 05:34:11AM +0000, Bulekov, Alexander wrote:
>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 <alxndr@bu.edu>
>---
> Makefile                |  1 +
> Makefile.objs           |  2 ++
> Makefile.target         |  2 +-
> include/sysemu/sysemu.h |  4 ++++
> main.c                  | 53 +++++++++++++++++++++++++++++++++++++++++
> vl.c                    | 36 +++++++---------------------
> 6 files changed, 70 insertions(+), 28 deletions(-)
> create mode 100644 main.c
>
>diff --git a/Makefile b/Makefile
>index 32bd554480..e6de7a47bb 100644
>--- a/Makefile
>+++ b/Makefile
>@@ -473,6 +473,7 @@ $(SOFTMMU_ALL_RULES): $(chardev-obj-y)
> $(SOFTMMU_ALL_RULES): $(crypto-obj-y)
> $(SOFTMMU_ALL_RULES): $(io-obj-y)
> $(SOFTMMU_ALL_RULES): config-all-devices.mak
>+$(SOFTMMU_ALL_RULES): $(softmmu-main-y)
> ifdef DECOMPRESS_EDK2_BLOBS
> $(SOFTMMU_ALL_RULES): $(edk2-decompressed)
> endif
>diff --git a/Makefile.objs b/Makefile.objs
>index 7c1e50f9d6..5ab166fed5 100644
>--- a/Makefile.objs
>+++ b/Makefile.objs
>@@ -84,6 +84,8 @@ common-obj-$(CONFIG_FDT) += device_tree.o
> # qapi
>
> common-obj-y += qapi/
>+
>+softmmu-obj-y = main.o
> endif
>
> #######################################################################
>diff --git a/Makefile.target b/Makefile.target
>index 6e61f607b1..8dcf3dddd8 100644
>--- a/Makefile.target
>+++ b/Makefile.target
>@@ -202,7 +202,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-obj-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 80c57fdc4e..270df5fa34 100644
>--- a/include/sysemu/sysemu.h
>+++ b/include/sysemu/sysemu.h
>@@ -118,6 +118,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/main.c b/main.c
>new file mode 100644
>index 0000000000..f10ceda541
>--- /dev/null
>+++ b/main.c
>@@ -0,0 +1,53 @@
>+/*
>+ * QEMU System Emulator
>+ *
>+ * Copyright (c) 2003-2008 Fabrice Bellard

I don't know the rules but, maybe that should also be extended to
2019/2020 since this is a new file.

Otherwise,

Reviewed-by: Darren Kenny <darren.kenny@oracle.com>

Thanks,

Darren.
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 32bd554480..e6de7a47bb 100644
--- a/Makefile
+++ b/Makefile
@@ -473,6 +473,7 @@  $(SOFTMMU_ALL_RULES): $(chardev-obj-y)
 $(SOFTMMU_ALL_RULES): $(crypto-obj-y)
 $(SOFTMMU_ALL_RULES): $(io-obj-y)
 $(SOFTMMU_ALL_RULES): config-all-devices.mak
+$(SOFTMMU_ALL_RULES): $(softmmu-main-y)
 ifdef DECOMPRESS_EDK2_BLOBS
 $(SOFTMMU_ALL_RULES): $(edk2-decompressed)
 endif
diff --git a/Makefile.objs b/Makefile.objs
index 7c1e50f9d6..5ab166fed5 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -84,6 +84,8 @@  common-obj-$(CONFIG_FDT) += device_tree.o
 # qapi
 
 common-obj-y += qapi/
+
+softmmu-obj-y = main.o
 endif
 
 #######################################################################
diff --git a/Makefile.target b/Makefile.target
index 6e61f607b1..8dcf3dddd8 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -202,7 +202,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-obj-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 80c57fdc4e..270df5fa34 100644
--- a/include/sysemu/sysemu.h
+++ b/include/sysemu/sysemu.h
@@ -118,6 +118,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/main.c b/main.c
new file mode 100644
index 0000000000..f10ceda541
--- /dev/null
+++ b/main.c
@@ -0,0 +1,53 @@ 
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2003-2008 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/vl.c b/vl.c
index 751401214c..bb77935f04 100644
--- a/vl.c
+++ b/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"
@@ -1657,7 +1638,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;
@@ -2820,7 +2801,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;
@@ -3372,7 +3353,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;
@@ -4284,7 +4265,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();
 
@@ -4394,7 +4375,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) {
@@ -4411,8 +4392,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();
 
     /*
@@ -4449,6 +4433,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;
 }