xref: /qemu/system/main.c (revision 12d1a768bdfea6e27a3a829228840d72507613a1)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2020 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 #include "qemu/osdep.h"
26 #include "qemu-main.h"
27 #include "qemu/main-loop.h"
28 #include "system/replay.h"
29 #include "system/system.h"
30 
31 #ifdef CONFIG_SDL
32 /*
33  * SDL insists on wrapping the main() function with its own implementation on
34  * some platforms; it does so via a macro that renames our main function, so
35  * <SDL.h> must be #included here even with no SDL code called from this file.
36  */
37 #include <SDL.h>
38 #endif
39 
40 #ifdef CONFIG_DARWIN
41 #include <CoreFoundation/CoreFoundation.h>
42 #endif
43 
44 static void *qemu_default_main(void *opaque)
45 {
46     int status;
47 
48     replay_mutex_lock();
49     bql_lock();
50     status = qemu_main_loop();
51     qemu_cleanup(status);
52     bql_unlock();
53     replay_mutex_unlock();
54 
55     exit(status);
56 }
57 
58 int (*qemu_main)(void);
59 
60 #ifdef CONFIG_DARWIN
61 static int os_darwin_cfrunloop_main(void)
62 {
63     CFRunLoopRun();
64     g_assert_not_reached();
65 }
66 int (*qemu_main)(void) = os_darwin_cfrunloop_main;
67 #endif
68 
69 int main(int argc, char **argv)
70 {
71     qemu_init(argc, argv);
72     bql_unlock();
73     replay_mutex_unlock();
74     if (qemu_main) {
75         QemuThread main_loop_thread;
76         qemu_thread_create(&main_loop_thread, "qemu_main",
77                            qemu_default_main, NULL, QEMU_THREAD_DETACHED);
78         return qemu_main();
79     } else {
80         qemu_default_main(NULL);
81         g_assert_not_reached();
82     }
83 }
84