| 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 | |
| 73 | /* |
| 74 | * qemu_init acquires the BQL and replay mutex lock. BQL is acquired when |
| 75 | * initializing cpus, to block associated threads until initialization is |
| 76 | * complete. Replay_mutex lock is acquired on initialization, because it |
| 77 | * must be held when configuring icount_mode. |
| 78 | * |
| 79 | * On MacOS, qemu main event loop runs in a background thread, as main |
| 80 | * thread must be reserved for UI. Thus, we need to transfer lock ownership, |
| 81 | * and the simplest way to do that is to release them, and reacquire them |
| 82 | * from qemu_default_main. |
| 83 | */ |
| 84 | bql_unlock(); |
| 85 | replay_mutex_unlock(); |
| 86 | |
| 87 | if (qemu_main) { |
| 88 | QemuThread main_loop_thread; |
| 89 | qemu_thread_create(&main_loop_thread, "qemu_main", |
| 90 | qemu_default_main, NULL, QEMU_THREAD_DETACHED); |
| 91 | return qemu_main(); |
| 92 | } else { |
| 93 | qemu_default_main(NULL); |
| 94 | g_assert_not_reached(); |
| 95 | } |
| 96 | } |