summaryrefslogtreecommitdiff
path: root/tizen/src/emul_state.c
blob: eff41e8ee5ca02348147396ff7fb687d6b000832 (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
 * Emulator State Information
 *
 * Copyright (C) 2011 - 2013 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>
 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
 * HyunJun Son
 *
 * 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 "emul_state.h"

#include <libgen.h>
#include "emulator_common.h"

#if defined(CONFIG_LINUX)
#include <X11/XKBlib.h>
extern bool kvm_allowed;
#elif defined(CONFIG_WIN32)
#include <windows.h>
extern bool hax_allowed;
#elif defined(CONFIG_DARWIN)
#include <fcntl.h>
extern bool hax_allowed;
#endif

#include "qemu/osdep.h"
#include "ui/console.h"
#include "sysemu/sysemu.h"
#include "block/block_int.h"
#include "sysemu/block-backend.h"
#include "net/net.h"
#include "qmp-commands.h"
#ifdef CONFIG_VIRTFS
#include "fsdev/qemu-fsdev.h"
#endif

#include "emulator.h"
#include "emulator_options.h"
#include "hw/virtio/maru_virtio_input.h"
#include "hw/virtio/maru_virtio_evdi.h"
#include "util/net_helper.h"
#include "util/new_debug_ch.h"

DECLARE_DEBUG_CHANNEL(emul_state);

#define MAXLEN  512

static EmulatorConfigInfo _emul_info;
static EmulatorConfigState _emul_state;

int get_max_touch_point(void)
{
    return virtio_touchscreen_get_max_touch_point();
}

/* current emulator condition */
int get_emulator_condition(void)
{
    return _emul_state.emulator_condition;
}

void set_emulator_condition(int state)
{
    if (state == BOOT_COMPLETED) {
        LOG_INFO("boot completed!\n");
        // TODO:
    } else if (state == RESET) {
        LOG_INFO("reset emulator!\n");
    }

    _emul_state.emulator_condition = state;
}

void set_emuld_connection(bool connected)
{
    _emul_state.emuld_connection = connected;
}

bool get_emuld_connection(void)
{
    return _emul_state.emuld_connection;
}

void set_sdb_connection(bool connected)
{
    _emul_state.sdb_connection = connected;
}

bool get_sdb_connection(void)
{
    return _emul_state.sdb_connection;
}

/* emualtor skin path */
const char* get_emul_skin_path(void)
{
    if (!_emul_info.skin_path) {
        _emul_info.skin_path = get_variable("skin_path");
    }

    return _emul_info.skin_path;
}

/* CPU virtualization */
bool get_emul_cpu_accel(void)
{
#ifdef CONFIG_LINUX
    return kvm_allowed;
#else
    return hax_allowed;
#endif

    return false;
}

//
// cleaned-up
//

// launch_conf_path
// only set by emulator.c when start up
const char *launch_conf_file = NULL;

// bin path
// only set by osutil-{OS}.c when start up
const char *bin_path = NULL;

const char *get_bin_path(void)
{
    if (bin_path) {
        return bin_path;
    }

    return "";
}

// emulator kernel log file
static const char *kernel_log_redirect_file = NULL;

const char *get_kernel_log_redirect_file(void)
{
    // TODO: kernel log path should be aquired from char device.
    if (kernel_log_redirect_file) {
        return kernel_log_redirect_file;
    }

    const char *vms_path = get_variable("vms_path");
    const char *vm_name = get_variable("vm_name");
    if (!vms_path || !vm_name) {
        // we can not specify log path.
        // emulator may not be launched from emulator-manager.
        kernel_log_redirect_file = g_strdup("");
    }
    else {
        kernel_log_redirect_file =
            g_strdup_printf("%s/%s/logs/emulator.klog", vms_path, vm_name);
    }

    return kernel_log_redirect_file;
}

// emulator log file
static const char *log_redirect_file = NULL;

#ifdef CONFIG_WIN32
// for checking Windows version
extern OSVERSIONINFO osvi;
#endif

const char *get_log_redirect_file(void)
{
    int result = -1;
    char log_filename[PATH_MAX];

    // should we compare stdout(fd/1) and stderr(fd/2) ??
#if defined(CONFIG_LINUX)
    result = readlink("/proc/self/fd/1", log_filename, PATH_MAX);
    if (result >= 0) {
        log_filename[result] = '\0';
    }
#elif defined(CONFIG_DARWIN)
    result = fcntl(STDOUT_FILENO, F_GETPATH, log_filename);
#elif defined(CONFIG_WIN32)
# if WINVER >= 0x600
    // works only vista or newer...
    if (osvi.dwMajorVersion >= 6) {
        char win32_log_filename_normalized[PATH_MAX];
        HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
        DWORD ret = GetFinalPathNameByHandle(handle_stdout,
                win32_log_filename_normalized,
                PATH_MAX, FILE_NAME_NORMALIZED);
        // strip "\\?\"
        if (ret > 0) {
            g_stpcpy(log_filename, win32_log_filename_normalized + 4);
            result = 0;
        } else {
            result = -1;
        }
    }
# endif
#endif
    if (log_redirect_file) {
        g_free((void *)log_redirect_file);
    }

    if (result >= 0) {
        log_redirect_file = g_strdup(log_filename);
    } else {
    // fail safe
        LOG_WARNING("Can not identify log redirection path, "
                "We should assume it !!!\n");

        const char *vms_path = get_variable("vms_path");
        const char *vm_name = get_variable("vm_name");
        if (!vms_path || !vm_name) {
            // we can not specify log path.
            // emulator may not be launched from emulator-manager.
            log_redirect_file = g_strdup("");
        }
        else {
            log_redirect_file =
                g_strdup_printf("%s/%s/logs/emulator.log", vms_path, vm_name);
        }
    }

    return log_redirect_file;
}

// drive image file
static const char *drive_image_file = NULL;

const char *get_drive_image_file(void)
{
    if (drive_image_file) {
        return drive_image_file;
    }

    // we should parse from "drive" parameter.
    // so qemu_main() had to be called before.
    BlockBackend *bb = blk_by_name("drive");
    if (bb) {
        BlockDriverState *bs = blk_bs(bb);
        drive_image_file = g_strdup(bs->filename);

        return drive_image_file;
    }

    // called before device initialized
    // or very weired situation
    LOG_WARNING("Can not identify main drive image file !!!\n");

    return "";
}

const char *get_drive_image_ver(void)
{
    return get_variable("image_ver");
}

// swap image file
static const char *swap_image_file = NULL;

const char *get_swap_image_file(void)
{
    if (swap_image_file) {
        return swap_image_file;
    }

    // we should parse from "swap" parameter.
    // so qemu_main() had to be called before.
    BlockBackend *bb = blk_by_name("swap");
    if (bb) {
        BlockDriverState *bs = blk_bs(bb);
        swap_image_file = g_strdup(bs->filename);

        return swap_image_file;
    }

    // called before device initialized
    // or very weired situation
    LOG_WARNING("Can not identify swap image file !!!\n");

    return "";
}

// kernel file
const char *get_kernel_file(void)
{
    return qemu_opt_get(qemu_get_machine_opts(), "kernel");
}

// https proxy
static const char *https_proxy_addr = NULL;

const char *get_https_proxy_addr(void)
{
    if (https_proxy_addr) {
        return https_proxy_addr;
    }

    const char *kernel_cmdline =
        qemu_opt_get(qemu_get_machine_opts(), "append");

    // kernel cmdline always contains proxy information
    char *buf = g_strstr_len(kernel_cmdline, -1, "https_proxy=");
    if (buf) {
        char **token = g_strsplit_set(buf, "= ", 3);
        if (token[0] && token[1] && g_strcmp0(token[1], "")) {
            https_proxy_addr = g_strdup(token[1]);
            g_strfreev(token);

            LOG_INFO("HTTPS proxy address: %s\n", https_proxy_addr);

            return https_proxy_addr;
        }
        g_strfreev(token);
    }

    https_proxy_addr = g_strdup("");

    LOG_INFO("HTTPS proxy address is not set.\n");

    return https_proxy_addr;
}

// vm_name
static const char *vm_name = NULL;

const char *get_vm_name(void)
{
    if (vm_name) {
        return vm_name;
    }

    // check variable
    const char *var_vm_name = get_variable("vm_name");

    // check drive image name
    char *drive_file_vm_name = g_path_get_basename(get_drive_image_file());

    // remove file extension
    char *last_dot = g_strrstr(drive_file_vm_name, ".");
    if (last_dot) {
        *last_dot = '\0';
    }

    // if it has "emulimg-" prefix - it may be made by emulator-manager
    // we should remove it
    int start_index = 0;
    if (!strncmp("emulimg-", drive_file_vm_name, 8)) {
        start_index = 8;
    }

    if (g_strcmp0(var_vm_name, drive_file_vm_name + start_index)) {
        // when drive_file_vm_name != var_vm_name
        // we should warn to users
        LOG_WARNING("vm_name and image_file_name is not matched\n");
    }
    // we choose drive_file_vm_name when var_vm_name is not provided
    if (!var_vm_name || strlen(var_vm_name) == 0) {
        vm_name = g_strdup(drive_file_vm_name + start_index);
    } else {
        vm_name = g_strdup(var_vm_name);
    }

    g_free(drive_file_vm_name);

    LOG_INFO("VM name: %s\n", vm_name);

    return vm_name;
}

const char *get_profile_name(void)
{
    return get_variable("profile");
}

const char *get_platform_version(void)
{
    return get_variable("platform_version");
}

#ifdef CONFIG_VIRTFS
// host directory sharing path
const char* get_host_directory_sharing_path(void)
{
    FsDriverEntry *sharing_entry = get_fsdev_fsentry((char*)DEFAULT_STATIC_HDS_ID);
    const char *sharing_path = (sharing_entry != NULL ? sharing_entry->path : NULL);

    return sharing_path;
}
#else
const char* get_host_directory_sharing_path(void)
{
    return NULL;
}
#endif

// GPU virtualization
static bool gpu_accel_enabled;

bool is_gpu_accel_enabled(void)
{
    static bool is_done;
    PciInfoList *info_list, *info;
    Error *err = NULL;

    if (is_done) {
        return gpu_accel_enabled;
    }

    info_list = qmp_query_pci(&err);
    if (err) {
        LOG_WARNING("PCI devices not supported\n");
        error_free(err);
        is_done = true;
        return false;
    }

    for (info = info_list; info; info = info->next) {
        PciDeviceInfoList *dev;

        for (dev = info->value->devices; dev; dev = dev->next) {
            /* TODO: use defines in the pci_regs.h
                     instead of the hard coding */
            if (dev->value->id->vendor == 0x19B1
                && dev->value->id->device == 0x1010) {
                gpu_accel_enabled = true;
            }
        }
    }

    qapi_free_PciInfoList(info_list);
    is_done = true;

    return gpu_accel_enabled;
}

// ram size
uint64_t get_ram_size(void)
{
    return ram_size;
}

// VM base port
// only set by net_helper.c when start up
int vm_base_port = -1;

int get_vm_base_port(void)
{
    // should not be called before base port is prepared
    g_assert(vm_base_port != 0);

    return vm_base_port;
}

int get_vm_spice_port(void)
{
    // should not be called before base port is prepared
    g_assert(vm_base_port != 0);

    return vm_base_port + SPICE_TCP_INDEX;
}

int get_vm_device_serial_number(void)
{
    // should not be called before base port is prepared
    g_assert(vm_base_port != 0);

    if (is_netclient_tap_attached()) {
        return (START_VM_BASE_PORT + SDB_TCP_INDEX);
    }

    return vm_base_port + SDB_TCP_INDEX;
}

// look-up PCI devices
bool is_pci_device_enabled(int device_id)
{
    bool is_enabled = false;
    PciInfoList *info_list, *info;
    Error *err = NULL;

    info_list = qmp_query_pci(&err);
    if (err) {
        LOG_WARNING("PCI devices not supported\n");
        error_free(err);
        return false;
    }

    for (info = info_list; info; info = info->next) {
        PciDeviceInfoList *dev;
        for (dev = info->value->devices; dev; dev = dev->next) {
            if (dev->value->id->device == device_id) {
                is_enabled = true;
                break;
            }
        }
    }

    qapi_free_PciInfoList(info_list);

    return is_enabled;
}

// mouse input
bool is_mouse_enabled(void)
{
    /* TODO: If the maru mouse device is added,
     * the device id should to be changed for it.
    */
    return !is_pci_device_enabled(PCI_DEVICE_ID_VIRTIO_MARU_TOUCHSCREEN);
}

// touchscreen input
bool is_touchscreen_enabled(void)
{
    return is_pci_device_enabled(PCI_DEVICE_ID_VIRTIO_MARU_TOUCHSCREEN);
}

// tap attached
bool is_netclient_tap_attached(void)
{
    NetClientState *ncs[MAX_QUEUE_NUM];
    int queues, i;

    queues = qemu_find_net_clients_except(NULL, ncs,
                                          NET_CLIENT_DRIVER_NIC,
                                          MAX_QUEUE_NUM);
    for (i = 0; i < queues; ++i) {
        if (ncs[i]->info->type == NET_CLIENT_DRIVER_TAP) {
            return true;
        }
    }

    return false;
}

// vm_data_path
static const char *vm_data_path = NULL;

const char* get_vm_data_path(void)
{
    if (vm_data_path) {
        return vm_data_path;
    }

    // check drive image file path
    char *drive_image_path = g_path_get_dirname(get_drive_image_file());

    // check launch conf file path
    char *conf_file_path = g_path_get_dirname(launch_conf_file);

    if (g_strcmp0(drive_image_path, conf_file_path)) {
        // when drive_image_path != launch_conf_path
        // we should warn to users
        LOG_WARNING("drive_image_path and launch_conf_path is not matched\n");
    }

    vm_data_path = drive_image_path;

    g_free(conf_file_path);

    LOG_INFO("VM data path: %s\n", vm_data_path);

    return vm_data_path;
}

const char *get_sdcard_image_path(void)
{
    const char *vm_data_path = get_vm_data_path();
#ifndef CONFIG_WIN32
    const gchar* sdcard_rel_path = g_strdup_printf("%s/../../sdcard/", vm_data_path);
#else
    const gchar* sdcard_rel_path = g_strdup_printf("%s\\..\\..\\sdcard\\", vm_data_path);
#endif
    char sdcard_abs_path[PATH_MAX + 1];

    char *ptr = realpath(sdcard_rel_path, sdcard_abs_path);
    if (!ptr) {
        LOG_WARNING("Fail to get absolute path !!!\n");
        return sdcard_rel_path;
    }
#ifndef CONFIG_WIN32
    const char *sdcard_path = g_strdup_printf("%s/", sdcard_abs_path);
#else
    const char *sdcard_path;
    int path_length = strnlen(sdcard_abs_path, sizeof(sdcard_abs_path));

    if (sdcard_abs_path[path_length - 1] != '\\') {
        sdcard_path = g_strdup_printf("%s\\", sdcard_abs_path);
    } else {
        sdcard_path = g_strdup(sdcard_abs_path);
    }
#endif
    g_free((void*)sdcard_rel_path);

    LOG_INFO("SD card path: %s\n", sdcard_path);
    return sdcard_path;
}

// guest IP
static const char *guest_ip = NULL;

void set_guest_ip(const char *ip)
{
    if (guest_ip) {
        LOG_WARNING("Guest IP is aleady set !!!\n");

        return;
    }
    guest_ip = g_strdup(ip);
}

const char* get_guest_ip(void)
{
    if (!guest_ip) {
        LOG_WARNING("Guest IP is NULL !!!\n");
    }

    return guest_ip;
}

// host IP
static const char *host_ip = NULL;

const char* get_host_ip(void)
{
    if (host_ip) {
        return host_ip;
    }

    // try to get from conf
    const char *host_ip_var = get_variable("host_ip");
    if (host_ip_var) {
        host_ip = g_strdup(host_ip_var);
        return host_ip;
    }

    // failsafe for legacy
    // try to parse from kernel commandline
    const char *kernel_cmdline =
        qemu_opt_get(qemu_get_machine_opts(), "append");
    char *buf = g_strstr_len(kernel_cmdline, -1, "host_ip=");

    char **splitted;
    if (buf && (splitted = g_strsplit(buf, " ", 2))) {
        if (splitted[0]) {
            host_ip = g_strdup(splitted[0]);
        }
        g_strfreev(splitted);

        return host_ip;
    }

    g_assert(host_ip != NULL);

    return NULL;
}

// display resolution
// only set by vl.c when start up
int initial_resolution_width = -1;
int initial_resolution_height = -1;

void set_initial_display_resolution(int width, int height)
{
    LOG_INFO("initial display resolution: %dx%d\n", width, height);

    initial_resolution_width = width;
    initial_resolution_height = height;
}

int get_display_resolution_width(void)
{
    return qemu_console_get_width(NULL, initial_resolution_width);
}

int get_display_resolution_height(void)
{
    return qemu_console_get_height(NULL, initial_resolution_height);
}

// guest default home
static const char *platform_default_home = NULL;
const char* get_platform_default_home(void)
{
    return platform_default_home;
}

void set_platform_default_home(const char *path)
{
    if (!platform_default_home) {
        platform_default_home = g_strdup(path);
    } else {
        LOG_INFO("platform home path is already set : %s\n",
                platform_default_home);
    }
}

static void emul_state_notify_exit(Notifier *notifier, void *data)
{
    g_free((void *)launch_conf_file);
    launch_conf_file = NULL;

    g_free((void *)bin_path);
    bin_path = NULL;

    g_free((void *)kernel_log_redirect_file);
    kernel_log_redirect_file = NULL;

    g_free((void *)log_redirect_file);
    log_redirect_file = NULL;

    g_free((void *)drive_image_file);
    drive_image_file = NULL;

    g_free((void *)swap_image_file);
    swap_image_file = NULL;

    g_free((void *)https_proxy_addr);
    https_proxy_addr = NULL;

    g_free((void *)vm_name);
    vm_name = NULL;

    g_free((void *)vm_data_path);
    vm_data_path = NULL;

    g_free((void *)guest_ip);
    guest_ip = NULL;

    g_free((void *)host_ip);
    host_ip = NULL;

    g_free((void *)platform_default_home);
    platform_default_home = NULL;
}


static Notifier emul_state_exit = { .notify = emul_state_notify_exit };

void init_emul_state(void)
{
    emulator_add_exit_notifier(&emul_state_exit);
}