summaryrefslogtreecommitdiff
path: root/tizen/src/emulator.c
blob: b63d020518e3f750f931e02125b2e05aaa8d1c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Emulator
 *
 * Copyright (C) 2014 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact:
 * SeokYeon Hwang <syeon.hwang@samsung.com>
 * MunKyu Im <munkyu.im@samsung.com>
 * GiWoong Kim <giwoong.kim@samsung.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

#include <stdlib.h>
#include <getopt.h>
#include <libgen.h>

#include "qemu/osdep.h"
#include "qemu/config-file.h"
#include "qemu/sockets.h"
#include "qemu/error-report.h"

#include "build_info.h"
#include "emulator.h"
#include "emul_state.h"
#include "emulator_options.h"
#include "util/error_handler.h"
#include "util/osutil.h"
#include "util/net_helper.h"
#include "ecs/ecs.h"
#include "util/device_hotplug.h"
#include "util/exported_strings.h"
#ifdef CONFIG_WIN32
#include <windows.h>
#include <sddl.h>
#endif
#ifdef CONFIG_QT
#include <qt5_supplement.h>
#endif

#ifdef CONFIG_SDL
#include <SDL.h>
#endif

#ifdef CONFIG_DARWIN
#include "ns_event.h"
int thread_running = 1; /* Check if we need exit main */
#endif

#include "util/new_debug_ch.h"
DECLARE_DEBUG_CHANNEL(main);

#define ARGS_LIMIT  128
char *maru_kernel_cmdline;

char tizen_target_path[PATH_MAX];

int _qemu_argc;
char **_qemu_argv;

void emulator_add_exit_notifier(Notifier *notify)
{
    qemu_add_exit_notifier(notify);
}

static void emulator_notify_exit(Notifier *notifier, void *data)
{
    int i;
    for (i = 0; i < _qemu_argc; ++i) {
        g_free(_qemu_argv[i]);
    }
    reset_variables();

    LOG_INFO("Exit emulator...\n");
}

static Notifier emulator_exit = { .notify = emulator_notify_exit };

static void* run_timed_shutdown_thread(void* args)
{
    send_shutdown_request(ECS_SYSTEM_ACTION_FORCE_CLOSE);

    const int sleep_interval_time = 1000; /* milli-seconds */
    const int timeout_for_shutdown = (uintptr_t)args;

    int i;
    for (i = 0; i < timeout_for_shutdown; i++) {
#ifdef CONFIG_WIN32
        Sleep(sleep_interval_time);
#else
        usleep(sleep_interval_time * 1000);
#endif
        /* do not use logger to help user see log in console */
        fprintf(stdout, "Wait for shutdown qemu...%d\n", (i + 1));
    }

    LOG_INFO("Shutdown qemu !!!\n");

    qemu_system_shutdown_request();

    return NULL;
}

void qemu_system_graceful_shutdown_request(unsigned int sec)
{
    static bool requested_shutdown_qemu_gracefully = false;

    if (!requested_shutdown_qemu_gracefully) {
        requested_shutdown_qemu_gracefully = true;

        LOG_INFO("shutdown_qemu_gracefully was called\n");
        QemuThread thread_id;
        qemu_thread_create(&thread_id, "shutdown_thread",
            run_timed_shutdown_thread, (void *)(uintptr_t)sec,
            QEMU_THREAD_DETACHED);
    } else {
        LOG_INFO("shutdown_qemu_gracefully was called twice\n");

        qemu_system_shutdown_request();

        // FIXME
#if 0
        /* Cannot call qemu_system_shutdown_request directly because
        * we are in a signal handler.
        */
        shutdown_requested = 1;
        qemu_notify_event();
#endif
    }
}

void print_system_info(void)
{
    static bool is_printed = false;

    if (is_printed) {
        return;
    }

    LOG_INFO("* Board name : %s\n", build_version);
    LOG_INFO("* Package %s\n", pkginfo_version);
    LOG_INFO("* Package %s\n", pkginfo_maintainer);
    LOG_INFO("* Git Head : %s\n", pkginfo_githead);
    LOG_INFO("* %s\n", latest_gittag);
    LOG_INFO("* User name : %s\n", g_get_real_name());
    LOG_INFO("* Host name : %s\n", g_get_host_name());

    /* time stamp */
    LOG_INFO("* Build date : %s\n", build_date);

    qemu_timeval tval = { 0, };
    if (qemu_gettimeofday(&tval) == 0) {
        char timeinfo[64] = {0, };

        time_t ti = tval.tv_sec;
        struct tm *tm_time = localtime(&ti);
        strftime(timeinfo, sizeof(timeinfo), "%Y-%m-%d %H:%M:%S", tm_time);
        LOG_INFO("* Current time : %s\n", timeinfo);
    }

#ifdef CONFIG_QT
    LOG_INFO("* Host Qt version : %s\n", qt5_get_version());
#endif

#ifdef CONFIG_SDL
    /* Gets the version of the dynamically linked SDL library */
    const SDL_version *sdl_linked;
# if (SDL_MAJOR_VERSION == 2)
    SDL_version linked;
    SDL_GetVersion(&linked);
    sdl_linked = &linked;
# else
    sdl_linked = SDL_Linked_Version();
# endif
    LOG_INFO("* Host SDL version : %d.%d.%d\n",
            sdl_linked->major,
            sdl_linked->minor,
            sdl_linked->patch);
#endif

    print_system_info_os();

    is_printed = true;
}

static void print_options_info(void)
{
    int i;

    fprintf(stdout, "qemu args: =========================================\n");
    for (i = 0; i < _qemu_argc; ++i) {
        fprintf(stdout, "\"%s\" ", _qemu_argv[i]);
    }
    fprintf(stdout, "\n====================================================\n");

    fflush(stdout);
}

const char *prepare_maru(const char * const kernel_cmdline)
{
    // FIXME: we needs modified kernel commandline now.
    // So we need independent kernel to use QEMU linux bootloader.
    // TODO: we should remove kernel commandline dependencies.
    const char *kernel_filename =
        qemu_opt_get(qemu_get_machine_opts(), "kernel");
    if (!kernel_filename) {
        error_report("Independent kernel must be specified!");
        exit(1);
    }

    print_system_info();

    init_emul_state();

    init_sdb_and_vm_base_port();

    // Assemble new kernel cmdline
    maru_kernel_cmdline = g_strdup_printf("%s sdb_port=%d, "
            "vm_resolution=%dx%d", kernel_cmdline, get_vm_base_port(),
            get_display_resolution_width(), get_display_resolution_height());

    LOG_INFO("kernel commandline : %s\n", maru_kernel_cmdline);

    return maru_kernel_cmdline;
}

void prepare_maru_after_device_init(void)
{
    init_device_hotplug();
    start_ecs();

    // Only for intent logging of essential information
    get_vm_name();
    get_vm_data_path();
}

#ifdef CONFIG_COCOA
int cocoa_main(int argc, const char * argv[]);
#else
int qemu_main(int argc, char **argv, char **envp);
#endif

static int emulator_main(int argc, char *argv[], char **envp)
{
    int c = 0;
    int qemu_arg_index = 0;

    _qemu_argv = g_malloc(sizeof(char*) * ARGS_LIMIT);

    // parse arguments
    // prevent the error message for undefined options
    opterr = 0;

    while (c != -1) {
        static struct option long_options[] = {
            {"conf",        required_argument,  0,  'c' },
            {"qemu",        required_argument,  0,  'q' },
            {0,             0,                  0,  0   }
        };

        c = getopt_long(argc, argv, "c:q:", long_options, NULL);

        if (c == -1)
            break;

        switch (c) {
        case '?':
            set_variable(argv[optind - 1], argv[optind], true);
            break;
        case 'c':
            if (optind == 3) {
                // "--conf" should be a first argument
                launch_conf_file = g_strdup(optarg);
            }
            break;
        case 'q':
            c = -1;
            qemu_arg_index = optind - 1;
            break;
        default:
            break;
        }
    }

    if (!launch_conf_file && qemu_arg_index == 0) {
        char *executable = basename(argv[0]);
        fprintf(stderr, "Usage: %s {-c|--conf} conf_file [--<key> <value>]...\n"
                        "       %*s [{-q|--qemu} <QEMU_ARGS>...]\n",
                        executable, (int)strlen(executable), "");
        fprintf(stderr, "       %s {-q|--qemu} <QEMU_ARGS>...\n",
                        executable);

        return -1;
    }

    // load configurations
    _qemu_argc = 0;
    _qemu_argv[_qemu_argc++] = g_strdup(argv[0]);

    set_bin_path_os(_qemu_argv[0]);

    if (launch_conf_file) {
        if (!g_file_test(launch_conf_file, G_FILE_TEST_EXISTS)) {
            fprintf(stderr, "conf_file(%s) does not exist.\n", launch_conf_file);
            return -1;
        }

        make_vm_lock_os(g_path_get_dirname(launch_conf_file));
        if (!load_conf(launch_conf_file)) {
            return -1;
        }

        // assemble arguments for qemu
        if (!assemble_emulator_args(&_qemu_argc, _qemu_argv)) {
            return -1;
        }
    }

    // if "--qemu" is specified, we should respect specified args
    if (qemu_arg_index > 0) {
        while (qemu_arg_index < argc) {
            _qemu_argv[_qemu_argc++] = g_strdup(argv[qemu_arg_index++]);
        }
    }

    emulator_add_exit_notifier(&emulator_exit);

    LOG_INFO("Start emulator...\n");
    print_options_info();

    LOG_INFO("qemu main start...\n");
#ifdef CONFIG_COCOA
    cocoa_main(_qemu_argc, (const char **)_qemu_argv);
#else
    qemu_main(_qemu_argc, _qemu_argv, envp);
#endif

    return 0;
}

#if defined (CONFIG_LINUX)
int main(int argc, char *argv[], char **envp)
{
    init_error_handler();
    return emulator_main(argc, argv, envp);
}
#elif defined (CONFIG_WIN32)
int main(int argc, char *argv[])
{
    if (!check_integrity_level_and_respawn()) {
        init_error_handler();
        return emulator_main(argc, argv, NULL);
    }
    return 0;
}
#else
int main(int argc, char *argv[])
{
    init_error_handler();
    return emulator_main(argc, argv, NULL);
}
#endif