Statistics
| Branch: | Revision:

root / vl.c @ 29b0040b

History | View | Annotate | Download (84.4 kB)

1
/*
2
 * QEMU System Emulator
3
 *
4
 * Copyright (c) 2003-2008 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
#include <unistd.h>
25
#include <fcntl.h>
26
#include <signal.h>
27
#include <time.h>
28
#include <errno.h>
29
#include <sys/time.h>
30
#include <zlib.h>
31

    
32
/* Needed early for CONFIG_BSD etc. */
33
#include "config-host.h"
34

    
35
#ifndef _WIN32
36
#include <libgen.h>
37
#include <sys/times.h>
38
#include <sys/wait.h>
39
#include <termios.h>
40
#include <sys/mman.h>
41
#include <sys/ioctl.h>
42
#include <sys/resource.h>
43
#include <sys/socket.h>
44
#include <netinet/in.h>
45
#include <net/if.h>
46
#include <arpa/inet.h>
47
#include <dirent.h>
48
#include <netdb.h>
49
#include <sys/select.h>
50
#ifdef CONFIG_SIMPLE_TRACE
51
#include "trace.h"
52
#endif
53

    
54
#ifdef CONFIG_BSD
55
#include <sys/stat.h>
56
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
57
#include <libutil.h>
58
#include <sys/sysctl.h>
59
#else
60
#include <util.h>
61
#endif
62
#else
63
#ifdef __linux__
64
#include <pty.h>
65
#include <malloc.h>
66

    
67
#include <linux/ppdev.h>
68
#include <linux/parport.h>
69
#endif
70
#ifdef __sun__
71
#include <sys/stat.h>
72
#include <sys/ethernet.h>
73
#include <sys/sockio.h>
74
#include <netinet/arp.h>
75
#include <netinet/in_systm.h>
76
#include <netinet/ip.h>
77
#include <netinet/ip_icmp.h> // must come after ip.h
78
#include <netinet/udp.h>
79
#include <netinet/tcp.h>
80
#include <net/if.h>
81
#include <syslog.h>
82
#include <stropts.h>
83
/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
84
   discussion about Solaris header problems */
85
extern int madvise(caddr_t, size_t, int);
86
#endif
87
#endif
88
#endif
89

    
90
#if defined(__OpenBSD__)
91
#include <util.h>
92
#endif
93

    
94
#if defined(CONFIG_VDE)
95
#include <libvdeplug.h>
96
#endif
97

    
98
#ifdef _WIN32
99
#include <windows.h>
100
#endif
101

    
102
#ifdef CONFIG_SDL
103
#if defined(__APPLE__) || defined(main)
104
#include <SDL.h>
105
int qemu_main(int argc, char **argv, char **envp);
106
int main(int argc, char **argv)
107
{
108
    return qemu_main(argc, argv, NULL);
109
}
110
#undef main
111
#define main qemu_main
112
#endif
113
#endif /* CONFIG_SDL */
114

    
115
#ifdef CONFIG_COCOA
116
#undef main
117
#define main qemu_main
118
#endif /* CONFIG_COCOA */
119

    
120
#include "hw/hw.h"
121
#include "hw/boards.h"
122
#include "hw/usb.h"
123
#include "hw/pcmcia.h"
124
#include "hw/pc.h"
125
#include "hw/isa.h"
126
#include "hw/baum.h"
127
#include "hw/bt.h"
128
#include "hw/watchdog.h"
129
#include "hw/smbios.h"
130
#include "hw/xen.h"
131
#include "hw/qdev.h"
132
#include "hw/loader.h"
133
#include "bt-host.h"
134
#include "net.h"
135
#include "net/slirp.h"
136
#include "monitor.h"
137
#include "console.h"
138
#include "sysemu.h"
139
#include "gdbstub.h"
140
#include "qemu-timer.h"
141
#include "qemu-char.h"
142
#include "cache-utils.h"
143
#include "block.h"
144
#include "blockdev.h"
145
#include "block-migration.h"
146
#include "dma.h"
147
#include "audio/audio.h"
148
#include "migration.h"
149
#include "kvm.h"
150
#include "qemu-option.h"
151
#include "qemu-config.h"
152
#include "qemu-objects.h"
153
#include "qemu-options.h"
154
#ifdef CONFIG_VIRTFS
155
#include "fsdev/qemu-fsdev.h"
156
#endif
157

    
158
#include "disas.h"
159

    
160
#include "qemu_socket.h"
161

    
162
#include "slirp/libslirp.h"
163

    
164
#include "qemu-queue.h"
165
#include "cpus.h"
166
#include "arch_init.h"
167

    
168
#include "ui/qemu-spice.h"
169

    
170
//#define DEBUG_NET
171
//#define DEBUG_SLIRP
172

    
173
#define DEFAULT_RAM_SIZE 128
174

    
175
#define MAX_VIRTIO_CONSOLES 1
176

    
177
static const char *data_dir;
178
const char *bios_name = NULL;
179
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
180
DisplayType display_type = DT_DEFAULT;
181
int display_remote = 0;
182
const char* keyboard_layout = NULL;
183
ram_addr_t ram_size;
184
const char *mem_path = NULL;
185
#ifdef MAP_POPULATE
186
int mem_prealloc = 0; /* force preallocation of physical target memory */
187
#endif
188
int nb_nics;
189
NICInfo nd_table[MAX_NICS];
190
int vm_running;
191
int autostart;
192
int incoming_expected; /* Started with -incoming and waiting for incoming */
193
static int rtc_utc = 1;
194
static int rtc_date_offset = -1; /* -1 means no change */
195
QEMUClock *rtc_clock;
196
int vga_interface_type = VGA_NONE;
197
static int full_screen = 0;
198
#ifdef CONFIG_SDL
199
static int no_frame = 0;
200
#endif
201
int no_quit = 0;
202
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
203
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
204
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
205
int win2k_install_hack = 0;
206
int rtc_td_hack = 0;
207
int usb_enabled = 0;
208
int singlestep = 0;
209
int smp_cpus = 1;
210
int max_cpus = 0;
211
int smp_cores = 1;
212
int smp_threads = 1;
213
const char *vnc_display;
214
int acpi_enabled = 1;
215
int no_hpet = 0;
216
int fd_bootchk = 1;
217
int no_reboot = 0;
218
int no_shutdown = 0;
219
int cursor_hide = 1;
220
int graphic_rotate = 0;
221
uint8_t irq0override = 1;
222
const char *watchdog;
223
const char *option_rom[MAX_OPTION_ROMS];
224
int nb_option_roms;
225
int semihosting_enabled = 0;
226
int old_param = 0;
227
const char *qemu_name;
228
int alt_grab = 0;
229
int ctrl_grab = 0;
230
unsigned int nb_prom_envs = 0;
231
const char *prom_envs[MAX_PROM_ENVS];
232
int boot_menu;
233

    
234
int nb_numa_nodes;
235
uint64_t node_mem[MAX_NODES];
236
uint64_t node_cpumask[MAX_NODES];
237

    
238
static QEMUTimer *nographic_timer;
239

    
240
uint8_t qemu_uuid[16];
241

    
242
static QEMUBootSetHandler *boot_set_handler;
243
static void *boot_set_opaque;
244

    
245
static NotifierList exit_notifiers =
246
    NOTIFIER_LIST_INITIALIZER(exit_notifiers);
247

    
248
int kvm_allowed = 0;
249
uint32_t xen_domid;
250
enum xen_mode xen_mode = XEN_EMULATE;
251

    
252
static int default_serial = 1;
253
static int default_parallel = 1;
254
static int default_virtcon = 1;
255
static int default_monitor = 1;
256
static int default_vga = 1;
257
static int default_floppy = 1;
258
static int default_cdrom = 1;
259
static int default_sdcard = 1;
260

    
261
static struct {
262
    const char *driver;
263
    int *flag;
264
} default_list[] = {
265
    { .driver = "isa-serial",           .flag = &default_serial    },
266
    { .driver = "isa-parallel",         .flag = &default_parallel  },
267
    { .driver = "isa-fdc",              .flag = &default_floppy    },
268
    { .driver = "ide-drive",            .flag = &default_cdrom     },
269
    { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
270
    { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
271
    { .driver = "virtio-serial",        .flag = &default_virtcon   },
272
    { .driver = "VGA",                  .flag = &default_vga       },
273
    { .driver = "cirrus-vga",           .flag = &default_vga       },
274
    { .driver = "vmware-svga",          .flag = &default_vga       },
275
};
276

    
277
static int default_driver_check(QemuOpts *opts, void *opaque)
278
{
279
    const char *driver = qemu_opt_get(opts, "driver");
280
    int i;
281

    
282
    if (!driver)
283
        return 0;
284
    for (i = 0; i < ARRAY_SIZE(default_list); i++) {
285
        if (strcmp(default_list[i].driver, driver) != 0)
286
            continue;
287
        *(default_list[i].flag) = 0;
288
    }
289
    return 0;
290
}
291

    
292
/***********************************************************/
293
/* real time host monotonic timer */
294

    
295
/* compute with 96 bit intermediate result: (a*b)/c */
296
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
297
{
298
    union {
299
        uint64_t ll;
300
        struct {
301
#ifdef HOST_WORDS_BIGENDIAN
302
            uint32_t high, low;
303
#else
304
            uint32_t low, high;
305
#endif
306
        } l;
307
    } u, res;
308
    uint64_t rl, rh;
309

    
310
    u.ll = a;
311
    rl = (uint64_t)u.l.low * (uint64_t)b;
312
    rh = (uint64_t)u.l.high * (uint64_t)b;
313
    rh += (rl >> 32);
314
    res.l.high = rh / c;
315
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
316
    return res.ll;
317
}
318

    
319
/***********************************************************/
320
/* host time/date access */
321
void qemu_get_timedate(struct tm *tm, int offset)
322
{
323
    time_t ti;
324
    struct tm *ret;
325

    
326
    time(&ti);
327
    ti += offset;
328
    if (rtc_date_offset == -1) {
329
        if (rtc_utc)
330
            ret = gmtime(&ti);
331
        else
332
            ret = localtime(&ti);
333
    } else {
334
        ti -= rtc_date_offset;
335
        ret = gmtime(&ti);
336
    }
337

    
338
    memcpy(tm, ret, sizeof(struct tm));
339
}
340

    
341
int qemu_timedate_diff(struct tm *tm)
342
{
343
    time_t seconds;
344

    
345
    if (rtc_date_offset == -1)
346
        if (rtc_utc)
347
            seconds = mktimegm(tm);
348
        else
349
            seconds = mktime(tm);
350
    else
351
        seconds = mktimegm(tm) + rtc_date_offset;
352

    
353
    return seconds - time(NULL);
354
}
355

    
356
void rtc_change_mon_event(struct tm *tm)
357
{
358
    QObject *data;
359

    
360
    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
361
    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
362
    qobject_decref(data);
363
}
364

    
365
static void configure_rtc_date_offset(const char *startdate, int legacy)
366
{
367
    time_t rtc_start_date;
368
    struct tm tm;
369

    
370
    if (!strcmp(startdate, "now") && legacy) {
371
        rtc_date_offset = -1;
372
    } else {
373
        if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
374
                   &tm.tm_year,
375
                   &tm.tm_mon,
376
                   &tm.tm_mday,
377
                   &tm.tm_hour,
378
                   &tm.tm_min,
379
                   &tm.tm_sec) == 6) {
380
            /* OK */
381
        } else if (sscanf(startdate, "%d-%d-%d",
382
                          &tm.tm_year,
383
                          &tm.tm_mon,
384
                          &tm.tm_mday) == 3) {
385
            tm.tm_hour = 0;
386
            tm.tm_min = 0;
387
            tm.tm_sec = 0;
388
        } else {
389
            goto date_fail;
390
        }
391
        tm.tm_year -= 1900;
392
        tm.tm_mon--;
393
        rtc_start_date = mktimegm(&tm);
394
        if (rtc_start_date == -1) {
395
        date_fail:
396
            fprintf(stderr, "Invalid date format. Valid formats are:\n"
397
                            "'2006-06-17T16:01:21' or '2006-06-17'\n");
398
            exit(1);
399
        }
400
        rtc_date_offset = time(NULL) - rtc_start_date;
401
    }
402
}
403

    
404
static void configure_rtc(QemuOpts *opts)
405
{
406
    const char *value;
407

    
408
    value = qemu_opt_get(opts, "base");
409
    if (value) {
410
        if (!strcmp(value, "utc")) {
411
            rtc_utc = 1;
412
        } else if (!strcmp(value, "localtime")) {
413
            rtc_utc = 0;
414
        } else {
415
            configure_rtc_date_offset(value, 0);
416
        }
417
    }
418
    value = qemu_opt_get(opts, "clock");
419
    if (value) {
420
        if (!strcmp(value, "host")) {
421
            rtc_clock = host_clock;
422
        } else if (!strcmp(value, "vm")) {
423
            rtc_clock = vm_clock;
424
        } else {
425
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
426
            exit(1);
427
        }
428
    }
429
    value = qemu_opt_get(opts, "driftfix");
430
    if (value) {
431
        if (!strcmp(value, "slew")) {
432
            rtc_td_hack = 1;
433
        } else if (!strcmp(value, "none")) {
434
            rtc_td_hack = 0;
435
        } else {
436
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
437
            exit(1);
438
        }
439
    }
440
}
441

    
442
/***********************************************************/
443
/* Bluetooth support */
444
static int nb_hcis;
445
static int cur_hci;
446
static struct HCIInfo *hci_table[MAX_NICS];
447

    
448
static struct bt_vlan_s {
449
    struct bt_scatternet_s net;
450
    int id;
451
    struct bt_vlan_s *next;
452
} *first_bt_vlan;
453

    
454
/* find or alloc a new bluetooth "VLAN" */
455
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
456
{
457
    struct bt_vlan_s **pvlan, *vlan;
458
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
459
        if (vlan->id == id)
460
            return &vlan->net;
461
    }
462
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
463
    vlan->id = id;
464
    pvlan = &first_bt_vlan;
465
    while (*pvlan != NULL)
466
        pvlan = &(*pvlan)->next;
467
    *pvlan = vlan;
468
    return &vlan->net;
469
}
470

    
471
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
472
{
473
}
474

    
475
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
476
{
477
    return -ENOTSUP;
478
}
479

    
480
static struct HCIInfo null_hci = {
481
    .cmd_send = null_hci_send,
482
    .sco_send = null_hci_send,
483
    .acl_send = null_hci_send,
484
    .bdaddr_set = null_hci_addr_set,
485
};
486

    
487
struct HCIInfo *qemu_next_hci(void)
488
{
489
    if (cur_hci == nb_hcis)
490
        return &null_hci;
491

    
492
    return hci_table[cur_hci++];
493
}
494

    
495
static struct HCIInfo *hci_init(const char *str)
496
{
497
    char *endp;
498
    struct bt_scatternet_s *vlan = 0;
499

    
500
    if (!strcmp(str, "null"))
501
        /* null */
502
        return &null_hci;
503
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
504
        /* host[:hciN] */
505
        return bt_host_hci(str[4] ? str + 5 : "hci0");
506
    else if (!strncmp(str, "hci", 3)) {
507
        /* hci[,vlan=n] */
508
        if (str[3]) {
509
            if (!strncmp(str + 3, ",vlan=", 6)) {
510
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
511
                if (*endp)
512
                    vlan = 0;
513
            }
514
        } else
515
            vlan = qemu_find_bt_vlan(0);
516
        if (vlan)
517
           return bt_new_hci(vlan);
518
    }
519

    
520
    fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
521

    
522
    return 0;
523
}
524

    
525
static int bt_hci_parse(const char *str)
526
{
527
    struct HCIInfo *hci;
528
    bdaddr_t bdaddr;
529

    
530
    if (nb_hcis >= MAX_NICS) {
531
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
532
        return -1;
533
    }
534

    
535
    hci = hci_init(str);
536
    if (!hci)
537
        return -1;
538

    
539
    bdaddr.b[0] = 0x52;
540
    bdaddr.b[1] = 0x54;
541
    bdaddr.b[2] = 0x00;
542
    bdaddr.b[3] = 0x12;
543
    bdaddr.b[4] = 0x34;
544
    bdaddr.b[5] = 0x56 + nb_hcis;
545
    hci->bdaddr_set(hci, bdaddr.b);
546

    
547
    hci_table[nb_hcis++] = hci;
548

    
549
    return 0;
550
}
551

    
552
static void bt_vhci_add(int vlan_id)
553
{
554
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
555

    
556
    if (!vlan->slave)
557
        fprintf(stderr, "qemu: warning: adding a VHCI to "
558
                        "an empty scatternet %i\n", vlan_id);
559

    
560
    bt_vhci_init(bt_new_hci(vlan));
561
}
562

    
563
static struct bt_device_s *bt_device_add(const char *opt)
564
{
565
    struct bt_scatternet_s *vlan;
566
    int vlan_id = 0;
567
    char *endp = strstr(opt, ",vlan=");
568
    int len = (endp ? endp - opt : strlen(opt)) + 1;
569
    char devname[10];
570

    
571
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
572

    
573
    if (endp) {
574
        vlan_id = strtol(endp + 6, &endp, 0);
575
        if (*endp) {
576
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
577
            return 0;
578
        }
579
    }
580

    
581
    vlan = qemu_find_bt_vlan(vlan_id);
582

    
583
    if (!vlan->slave)
584
        fprintf(stderr, "qemu: warning: adding a slave device to "
585
                        "an empty scatternet %i\n", vlan_id);
586

    
587
    if (!strcmp(devname, "keyboard"))
588
        return bt_keyboard_init(vlan);
589

    
590
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
591
    return 0;
592
}
593

    
594
static int bt_parse(const char *opt)
595
{
596
    const char *endp, *p;
597
    int vlan;
598

    
599
    if (strstart(opt, "hci", &endp)) {
600
        if (!*endp || *endp == ',') {
601
            if (*endp)
602
                if (!strstart(endp, ",vlan=", 0))
603
                    opt = endp + 1;
604

    
605
            return bt_hci_parse(opt);
606
       }
607
    } else if (strstart(opt, "vhci", &endp)) {
608
        if (!*endp || *endp == ',') {
609
            if (*endp) {
610
                if (strstart(endp, ",vlan=", &p)) {
611
                    vlan = strtol(p, (char **) &endp, 0);
612
                    if (*endp) {
613
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
614
                        return 1;
615
                    }
616
                } else {
617
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
618
                    return 1;
619
                }
620
            } else
621
                vlan = 0;
622

    
623
            bt_vhci_add(vlan);
624
            return 0;
625
        }
626
    } else if (strstart(opt, "device:", &endp))
627
        return !bt_device_add(endp);
628

    
629
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
630
    return 1;
631
}
632

    
633
/***********************************************************/
634
/* QEMU Block devices */
635

    
636
#define HD_ALIAS "index=%d,media=disk"
637
#define CDROM_ALIAS "index=2,media=cdrom"
638
#define FD_ALIAS "index=%d,if=floppy"
639
#define PFLASH_ALIAS "if=pflash"
640
#define MTD_ALIAS "if=mtd"
641
#define SD_ALIAS "index=0,if=sd"
642

    
643
static int drive_init_func(QemuOpts *opts, void *opaque)
644
{
645
    int *use_scsi = opaque;
646
    int fatal_error = 0;
647

    
648
    if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
649
        if (fatal_error)
650
            return 1;
651
    }
652
    return 0;
653
}
654

    
655
static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
656
{
657
    if (NULL == qemu_opt_get(opts, "snapshot")) {
658
        qemu_opt_set(opts, "snapshot", "on");
659
    }
660
    return 0;
661
}
662

    
663
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
664
{
665
    boot_set_handler = func;
666
    boot_set_opaque = opaque;
667
}
668

    
669
int qemu_boot_set(const char *boot_devices)
670
{
671
    if (!boot_set_handler) {
672
        return -EINVAL;
673
    }
674
    return boot_set_handler(boot_set_opaque, boot_devices);
675
}
676

    
677
static void validate_bootdevices(char *devices)
678
{
679
    /* We just do some generic consistency checks */
680
    const char *p;
681
    int bitmap = 0;
682

    
683
    for (p = devices; *p != '\0'; p++) {
684
        /* Allowed boot devices are:
685
         * a-b: floppy disk drives
686
         * c-f: IDE disk drives
687
         * g-m: machine implementation dependant drives
688
         * n-p: network devices
689
         * It's up to each machine implementation to check if the given boot
690
         * devices match the actual hardware implementation and firmware
691
         * features.
692
         */
693
        if (*p < 'a' || *p > 'p') {
694
            fprintf(stderr, "Invalid boot device '%c'\n", *p);
695
            exit(1);
696
        }
697
        if (bitmap & (1 << (*p - 'a'))) {
698
            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
699
            exit(1);
700
        }
701
        bitmap |= 1 << (*p - 'a');
702
    }
703
}
704

    
705
static void restore_boot_devices(void *opaque)
706
{
707
    char *standard_boot_devices = opaque;
708
    static int first = 1;
709

    
710
    /* Restore boot order and remove ourselves after the first boot */
711
    if (first) {
712
        first = 0;
713
        return;
714
    }
715

    
716
    qemu_boot_set(standard_boot_devices);
717

    
718
    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
719
    qemu_free(standard_boot_devices);
720
}
721

    
722
static void numa_add(const char *optarg)
723
{
724
    char option[128];
725
    char *endptr;
726
    unsigned long long value, endvalue;
727
    int nodenr;
728

    
729
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
730
    if (!strcmp(option, "node")) {
731
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
732
            nodenr = nb_numa_nodes;
733
        } else {
734
            nodenr = strtoull(option, NULL, 10);
735
        }
736

    
737
        if (get_param_value(option, 128, "mem", optarg) == 0) {
738
            node_mem[nodenr] = 0;
739
        } else {
740
            value = strtoull(option, &endptr, 0);
741
            switch (*endptr) {
742
            case 0: case 'M': case 'm':
743
                value <<= 20;
744
                break;
745
            case 'G': case 'g':
746
                value <<= 30;
747
                break;
748
            }
749
            node_mem[nodenr] = value;
750
        }
751
        if (get_param_value(option, 128, "cpus", optarg) == 0) {
752
            node_cpumask[nodenr] = 0;
753
        } else {
754
            value = strtoull(option, &endptr, 10);
755
            if (value >= 64) {
756
                value = 63;
757
                fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
758
            } else {
759
                if (*endptr == '-') {
760
                    endvalue = strtoull(endptr+1, &endptr, 10);
761
                    if (endvalue >= 63) {
762
                        endvalue = 62;
763
                        fprintf(stderr,
764
                            "only 63 CPUs in NUMA mode supported.\n");
765
                    }
766
                    value = (2ULL << endvalue) - (1ULL << value);
767
                } else {
768
                    value = 1ULL << value;
769
                }
770
            }
771
            node_cpumask[nodenr] = value;
772
        }
773
        nb_numa_nodes++;
774
    }
775
    return;
776
}
777

    
778
static void smp_parse(const char *optarg)
779
{
780
    int smp, sockets = 0, threads = 0, cores = 0;
781
    char *endptr;
782
    char option[128];
783

    
784
    smp = strtoul(optarg, &endptr, 10);
785
    if (endptr != optarg) {
786
        if (*endptr == ',') {
787
            endptr++;
788
        }
789
    }
790
    if (get_param_value(option, 128, "sockets", endptr) != 0)
791
        sockets = strtoull(option, NULL, 10);
792
    if (get_param_value(option, 128, "cores", endptr) != 0)
793
        cores = strtoull(option, NULL, 10);
794
    if (get_param_value(option, 128, "threads", endptr) != 0)
795
        threads = strtoull(option, NULL, 10);
796
    if (get_param_value(option, 128, "maxcpus", endptr) != 0)
797
        max_cpus = strtoull(option, NULL, 10);
798

    
799
    /* compute missing values, prefer sockets over cores over threads */
800
    if (smp == 0 || sockets == 0) {
801
        sockets = sockets > 0 ? sockets : 1;
802
        cores = cores > 0 ? cores : 1;
803
        threads = threads > 0 ? threads : 1;
804
        if (smp == 0) {
805
            smp = cores * threads * sockets;
806
        }
807
    } else {
808
        if (cores == 0) {
809
            threads = threads > 0 ? threads : 1;
810
            cores = smp / (sockets * threads);
811
        } else {
812
            threads = smp / (cores * sockets);
813
        }
814
    }
815
    smp_cpus = smp;
816
    smp_cores = cores > 0 ? cores : 1;
817
    smp_threads = threads > 0 ? threads : 1;
818
    if (max_cpus == 0)
819
        max_cpus = smp_cpus;
820
}
821

    
822
/***********************************************************/
823
/* USB devices */
824

    
825
static int usb_device_add(const char *devname)
826
{
827
    const char *p;
828
    USBDevice *dev = NULL;
829

    
830
    if (!usb_enabled)
831
        return -1;
832

    
833
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
834
    dev = usbdevice_create(devname);
835
    if (dev)
836
        goto done;
837

    
838
    /* the other ones */
839
    if (strstart(devname, "host:", &p)) {
840
        dev = usb_host_device_open(p);
841
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
842
        dev = usb_bt_init(devname[2] ? hci_init(p) :
843
                        bt_new_hci(qemu_find_bt_vlan(0)));
844
    } else {
845
        return -1;
846
    }
847
    if (!dev)
848
        return -1;
849

    
850
done:
851
    return 0;
852
}
853

    
854
static int usb_device_del(const char *devname)
855
{
856
    int bus_num, addr;
857
    const char *p;
858

    
859
    if (strstart(devname, "host:", &p))
860
        return usb_host_device_close(p);
861

    
862
    if (!usb_enabled)
863
        return -1;
864

    
865
    p = strchr(devname, '.');
866
    if (!p)
867
        return -1;
868
    bus_num = strtoul(devname, NULL, 0);
869
    addr = strtoul(p + 1, NULL, 0);
870

    
871
    return usb_device_delete_addr(bus_num, addr);
872
}
873

    
874
static int usb_parse(const char *cmdline)
875
{
876
    int r;
877
    r = usb_device_add(cmdline);
878
    if (r < 0) {
879
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
880
    }
881
    return r;
882
}
883

    
884
void do_usb_add(Monitor *mon, const QDict *qdict)
885
{
886
    const char *devname = qdict_get_str(qdict, "devname");
887
    if (usb_device_add(devname) < 0) {
888
        error_report("could not add USB device '%s'", devname);
889
    }
890
}
891

    
892
void do_usb_del(Monitor *mon, const QDict *qdict)
893
{
894
    const char *devname = qdict_get_str(qdict, "devname");
895
    if (usb_device_del(devname) < 0) {
896
        error_report("could not delete USB device '%s'", devname);
897
    }
898
}
899

    
900
/***********************************************************/
901
/* PCMCIA/Cardbus */
902

    
903
static struct pcmcia_socket_entry_s {
904
    PCMCIASocket *socket;
905
    struct pcmcia_socket_entry_s *next;
906
} *pcmcia_sockets = 0;
907

    
908
void pcmcia_socket_register(PCMCIASocket *socket)
909
{
910
    struct pcmcia_socket_entry_s *entry;
911

    
912
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
913
    entry->socket = socket;
914
    entry->next = pcmcia_sockets;
915
    pcmcia_sockets = entry;
916
}
917

    
918
void pcmcia_socket_unregister(PCMCIASocket *socket)
919
{
920
    struct pcmcia_socket_entry_s *entry, **ptr;
921

    
922
    ptr = &pcmcia_sockets;
923
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
924
        if (entry->socket == socket) {
925
            *ptr = entry->next;
926
            qemu_free(entry);
927
        }
928
}
929

    
930
void pcmcia_info(Monitor *mon)
931
{
932
    struct pcmcia_socket_entry_s *iter;
933

    
934
    if (!pcmcia_sockets)
935
        monitor_printf(mon, "No PCMCIA sockets\n");
936

    
937
    for (iter = pcmcia_sockets; iter; iter = iter->next)
938
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
939
                       iter->socket->attached ? iter->socket->card_string :
940
                       "Empty");
941
}
942

    
943
/***********************************************************/
944
/* I/O handling */
945

    
946
typedef struct IOHandlerRecord {
947
    int fd;
948
    IOCanReadHandler *fd_read_poll;
949
    IOHandler *fd_read;
950
    IOHandler *fd_write;
951
    int deleted;
952
    void *opaque;
953
    /* temporary data */
954
    struct pollfd *ufd;
955
    QLIST_ENTRY(IOHandlerRecord) next;
956
} IOHandlerRecord;
957

    
958
static QLIST_HEAD(, IOHandlerRecord) io_handlers =
959
    QLIST_HEAD_INITIALIZER(io_handlers);
960

    
961

    
962
/* XXX: fd_read_poll should be suppressed, but an API change is
963
   necessary in the character devices to suppress fd_can_read(). */
964
int qemu_set_fd_handler2(int fd,
965
                         IOCanReadHandler *fd_read_poll,
966
                         IOHandler *fd_read,
967
                         IOHandler *fd_write,
968
                         void *opaque)
969
{
970
    IOHandlerRecord *ioh;
971

    
972
    if (!fd_read && !fd_write) {
973
        QLIST_FOREACH(ioh, &io_handlers, next) {
974
            if (ioh->fd == fd) {
975
                ioh->deleted = 1;
976
                break;
977
            }
978
        }
979
    } else {
980
        QLIST_FOREACH(ioh, &io_handlers, next) {
981
            if (ioh->fd == fd)
982
                goto found;
983
        }
984
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
985
        QLIST_INSERT_HEAD(&io_handlers, ioh, next);
986
    found:
987
        ioh->fd = fd;
988
        ioh->fd_read_poll = fd_read_poll;
989
        ioh->fd_read = fd_read;
990
        ioh->fd_write = fd_write;
991
        ioh->opaque = opaque;
992
        ioh->deleted = 0;
993
    }
994
    return 0;
995
}
996

    
997
int qemu_set_fd_handler(int fd,
998
                        IOHandler *fd_read,
999
                        IOHandler *fd_write,
1000
                        void *opaque)
1001
{
1002
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1003
}
1004

    
1005
/***********************************************************/
1006
/* machine registration */
1007

    
1008
static QEMUMachine *first_machine = NULL;
1009
QEMUMachine *current_machine = NULL;
1010

    
1011
int qemu_register_machine(QEMUMachine *m)
1012
{
1013
    QEMUMachine **pm;
1014
    pm = &first_machine;
1015
    while (*pm != NULL)
1016
        pm = &(*pm)->next;
1017
    m->next = NULL;
1018
    *pm = m;
1019
    return 0;
1020
}
1021

    
1022
static QEMUMachine *find_machine(const char *name)
1023
{
1024
    QEMUMachine *m;
1025

    
1026
    for(m = first_machine; m != NULL; m = m->next) {
1027
        if (!strcmp(m->name, name))
1028
            return m;
1029
        if (m->alias && !strcmp(m->alias, name))
1030
            return m;
1031
    }
1032
    return NULL;
1033
}
1034

    
1035
static QEMUMachine *find_default_machine(void)
1036
{
1037
    QEMUMachine *m;
1038

    
1039
    for(m = first_machine; m != NULL; m = m->next) {
1040
        if (m->is_default) {
1041
            return m;
1042
        }
1043
    }
1044
    return NULL;
1045
}
1046

    
1047
/***********************************************************/
1048
/* main execution loop */
1049

    
1050
static void gui_update(void *opaque)
1051
{
1052
    uint64_t interval = GUI_REFRESH_INTERVAL;
1053
    DisplayState *ds = opaque;
1054
    DisplayChangeListener *dcl = ds->listeners;
1055

    
1056
    qemu_flush_coalesced_mmio_buffer();
1057
    dpy_refresh(ds);
1058

    
1059
    while (dcl != NULL) {
1060
        if (dcl->gui_timer_interval &&
1061
            dcl->gui_timer_interval < interval)
1062
            interval = dcl->gui_timer_interval;
1063
        dcl = dcl->next;
1064
    }
1065
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1066
}
1067

    
1068
static void nographic_update(void *opaque)
1069
{
1070
    uint64_t interval = GUI_REFRESH_INTERVAL;
1071

    
1072
    qemu_flush_coalesced_mmio_buffer();
1073
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1074
}
1075

    
1076
struct vm_change_state_entry {
1077
    VMChangeStateHandler *cb;
1078
    void *opaque;
1079
    QLIST_ENTRY (vm_change_state_entry) entries;
1080
};
1081

    
1082
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1083

    
1084
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1085
                                                     void *opaque)
1086
{
1087
    VMChangeStateEntry *e;
1088

    
1089
    e = qemu_mallocz(sizeof (*e));
1090

    
1091
    e->cb = cb;
1092
    e->opaque = opaque;
1093
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1094
    return e;
1095
}
1096

    
1097
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1098
{
1099
    QLIST_REMOVE (e, entries);
1100
    qemu_free (e);
1101
}
1102

    
1103
void vm_state_notify(int running, int reason)
1104
{
1105
    VMChangeStateEntry *e;
1106

    
1107
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1108
        e->cb(e->opaque, running, reason);
1109
    }
1110
}
1111

    
1112
void vm_start(void)
1113
{
1114
    if (!vm_running) {
1115
        cpu_enable_ticks();
1116
        vm_running = 1;
1117
        vm_state_notify(1, 0);
1118
        resume_all_vcpus();
1119
        monitor_protocol_event(QEVENT_RESUME, NULL);
1120
    }
1121
}
1122

    
1123
/* reset/shutdown handler */
1124

    
1125
typedef struct QEMUResetEntry {
1126
    QTAILQ_ENTRY(QEMUResetEntry) entry;
1127
    QEMUResetHandler *func;
1128
    void *opaque;
1129
} QEMUResetEntry;
1130

    
1131
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1132
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
1133
static int reset_requested;
1134
static int shutdown_requested;
1135
static int powerdown_requested;
1136
int debug_requested;
1137
int vmstop_requested;
1138

    
1139
int qemu_shutdown_requested(void)
1140
{
1141
    int r = shutdown_requested;
1142
    shutdown_requested = 0;
1143
    return r;
1144
}
1145

    
1146
int qemu_reset_requested(void)
1147
{
1148
    int r = reset_requested;
1149
    reset_requested = 0;
1150
    return r;
1151
}
1152

    
1153
int qemu_powerdown_requested(void)
1154
{
1155
    int r = powerdown_requested;
1156
    powerdown_requested = 0;
1157
    return r;
1158
}
1159

    
1160
static int qemu_debug_requested(void)
1161
{
1162
    int r = debug_requested;
1163
    debug_requested = 0;
1164
    return r;
1165
}
1166

    
1167
static int qemu_vmstop_requested(void)
1168
{
1169
    int r = vmstop_requested;
1170
    vmstop_requested = 0;
1171
    return r;
1172
}
1173

    
1174
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1175
{
1176
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1177

    
1178
    re->func = func;
1179
    re->opaque = opaque;
1180
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1181
}
1182

    
1183
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1184
{
1185
    QEMUResetEntry *re;
1186

    
1187
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
1188
        if (re->func == func && re->opaque == opaque) {
1189
            QTAILQ_REMOVE(&reset_handlers, re, entry);
1190
            qemu_free(re);
1191
            return;
1192
        }
1193
    }
1194
}
1195

    
1196
void qemu_system_reset(void)
1197
{
1198
    QEMUResetEntry *re, *nre;
1199

    
1200
    /* reset all devices */
1201
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1202
        re->func(re->opaque);
1203
    }
1204
    monitor_protocol_event(QEVENT_RESET, NULL);
1205
    cpu_synchronize_all_post_reset();
1206
}
1207

    
1208
void qemu_system_reset_request(void)
1209
{
1210
    if (no_reboot) {
1211
        shutdown_requested = 1;
1212
    } else {
1213
        reset_requested = 1;
1214
    }
1215
    qemu_notify_event();
1216
}
1217

    
1218
void qemu_system_shutdown_request(void)
1219
{
1220
    shutdown_requested = 1;
1221
    qemu_notify_event();
1222
}
1223

    
1224
void qemu_system_powerdown_request(void)
1225
{
1226
    powerdown_requested = 1;
1227
    qemu_notify_event();
1228
}
1229

    
1230
void main_loop_wait(int nonblocking)
1231
{
1232
    IOHandlerRecord *ioh;
1233
    fd_set rfds, wfds, xfds;
1234
    int ret, nfds;
1235
    struct timeval tv;
1236
    int timeout;
1237

    
1238
    if (nonblocking)
1239
        timeout = 0;
1240
    else {
1241
        timeout = qemu_calculate_timeout();
1242
        qemu_bh_update_timeout(&timeout);
1243
    }
1244

    
1245
    os_host_main_loop_wait(&timeout);
1246

    
1247
    /* poll any events */
1248
    /* XXX: separate device handlers from system ones */
1249
    nfds = -1;
1250
    FD_ZERO(&rfds);
1251
    FD_ZERO(&wfds);
1252
    FD_ZERO(&xfds);
1253
    QLIST_FOREACH(ioh, &io_handlers, next) {
1254
        if (ioh->deleted)
1255
            continue;
1256
        if (ioh->fd_read &&
1257
            (!ioh->fd_read_poll ||
1258
             ioh->fd_read_poll(ioh->opaque) != 0)) {
1259
            FD_SET(ioh->fd, &rfds);
1260
            if (ioh->fd > nfds)
1261
                nfds = ioh->fd;
1262
        }
1263
        if (ioh->fd_write) {
1264
            FD_SET(ioh->fd, &wfds);
1265
            if (ioh->fd > nfds)
1266
                nfds = ioh->fd;
1267
        }
1268
    }
1269

    
1270
    tv.tv_sec = timeout / 1000;
1271
    tv.tv_usec = (timeout % 1000) * 1000;
1272

    
1273
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1274

    
1275
    qemu_mutex_unlock_iothread();
1276
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1277
    qemu_mutex_lock_iothread();
1278
    if (ret > 0) {
1279
        IOHandlerRecord *pioh;
1280

    
1281
        QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1282
            if (ioh->deleted) {
1283
                QLIST_REMOVE(ioh, next);
1284
                qemu_free(ioh);
1285
                continue;
1286
            }
1287
            if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1288
                ioh->fd_read(ioh->opaque);
1289
            }
1290
            if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1291
                ioh->fd_write(ioh->opaque);
1292
            }
1293
        }
1294
    }
1295

    
1296
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1297

    
1298
    qemu_run_all_timers();
1299

    
1300
    /* Check bottom-halves last in case any of the earlier events triggered
1301
       them.  */
1302
    qemu_bh_poll();
1303

    
1304
}
1305

    
1306
static int vm_can_run(void)
1307
{
1308
    if (powerdown_requested)
1309
        return 0;
1310
    if (reset_requested)
1311
        return 0;
1312
    if (shutdown_requested)
1313
        return 0;
1314
    if (debug_requested)
1315
        return 0;
1316
    return 1;
1317
}
1318

    
1319
qemu_irq qemu_system_powerdown;
1320

    
1321
static void main_loop(void)
1322
{
1323
    int r;
1324

    
1325
    qemu_main_loop_start();
1326

    
1327
    for (;;) {
1328
        do {
1329
            bool nonblocking = false;
1330
#ifdef CONFIG_PROFILER
1331
            int64_t ti;
1332
#endif
1333
#ifndef CONFIG_IOTHREAD
1334
            nonblocking = cpu_exec_all();
1335
#endif
1336
#ifdef CONFIG_PROFILER
1337
            ti = profile_getclock();
1338
#endif
1339
            main_loop_wait(nonblocking);
1340
#ifdef CONFIG_PROFILER
1341
            dev_time += profile_getclock() - ti;
1342
#endif
1343
        } while (vm_can_run());
1344

    
1345
        if ((r = qemu_debug_requested())) {
1346
            vm_stop(r);
1347
        }
1348
        if (qemu_shutdown_requested()) {
1349
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1350
            if (no_shutdown) {
1351
                vm_stop(0);
1352
                no_shutdown = 0;
1353
            } else
1354
                break;
1355
        }
1356
        if (qemu_reset_requested()) {
1357
            pause_all_vcpus();
1358
            qemu_system_reset();
1359
            resume_all_vcpus();
1360
        }
1361
        if (qemu_powerdown_requested()) {
1362
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1363
            qemu_irq_raise(qemu_system_powerdown);
1364
        }
1365
        if ((r = qemu_vmstop_requested())) {
1366
            vm_stop(r);
1367
        }
1368
    }
1369
    bdrv_close_all();
1370
    pause_all_vcpus();
1371
}
1372

    
1373
static void version(void)
1374
{
1375
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1376
}
1377

    
1378
static void help(int exitcode)
1379
{
1380
    const char *options_help =
1381
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1382
        opt_help
1383
#define DEFHEADING(text) stringify(text) "\n"
1384
#include "qemu-options.def"
1385
#undef DEF
1386
#undef DEFHEADING
1387
#undef GEN_DOCS
1388
        ;
1389
    version();
1390
    printf("usage: %s [options] [disk_image]\n"
1391
           "\n"
1392
           "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1393
           "\n"
1394
           "%s\n"
1395
           "During emulation, the following keys are useful:\n"
1396
           "ctrl-alt-f      toggle full screen\n"
1397
           "ctrl-alt-n      switch to virtual console 'n'\n"
1398
           "ctrl-alt        toggle mouse and keyboard grab\n"
1399
           "\n"
1400
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
1401
           "qemu",
1402
           options_help);
1403
    exit(exitcode);
1404
}
1405

    
1406
#define HAS_ARG 0x0001
1407

    
1408
typedef struct QEMUOption {
1409
    const char *name;
1410
    int flags;
1411
    int index;
1412
    uint32_t arch_mask;
1413
} QEMUOption;
1414

    
1415
static const QEMUOption qemu_options[] = {
1416
    { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1417
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1418
    { option, opt_arg, opt_enum, arch_mask },
1419
#define DEFHEADING(text)
1420
#include "qemu-options.def"
1421
#undef DEF
1422
#undef DEFHEADING
1423
#undef GEN_DOCS
1424
    { NULL },
1425
};
1426
static void select_vgahw (const char *p)
1427
{
1428
    const char *opts;
1429

    
1430
    default_vga = 0;
1431
    vga_interface_type = VGA_NONE;
1432
    if (strstart(p, "std", &opts)) {
1433
        vga_interface_type = VGA_STD;
1434
    } else if (strstart(p, "cirrus", &opts)) {
1435
        vga_interface_type = VGA_CIRRUS;
1436
    } else if (strstart(p, "vmware", &opts)) {
1437
        vga_interface_type = VGA_VMWARE;
1438
    } else if (strstart(p, "xenfb", &opts)) {
1439
        vga_interface_type = VGA_XENFB;
1440
    } else if (!strstart(p, "none", &opts)) {
1441
    invalid_vga:
1442
        fprintf(stderr, "Unknown vga type: %s\n", p);
1443
        exit(1);
1444
    }
1445
    while (*opts) {
1446
        const char *nextopt;
1447

    
1448
        if (strstart(opts, ",retrace=", &nextopt)) {
1449
            opts = nextopt;
1450
            if (strstart(opts, "dumb", &nextopt))
1451
                vga_retrace_method = VGA_RETRACE_DUMB;
1452
            else if (strstart(opts, "precise", &nextopt))
1453
                vga_retrace_method = VGA_RETRACE_PRECISE;
1454
            else goto invalid_vga;
1455
        } else goto invalid_vga;
1456
        opts = nextopt;
1457
    }
1458
}
1459

    
1460
static int balloon_parse(const char *arg)
1461
{
1462
    QemuOpts *opts;
1463

    
1464
    if (strcmp(arg, "none") == 0) {
1465
        return 0;
1466
    }
1467

    
1468
    if (!strncmp(arg, "virtio", 6)) {
1469
        if (arg[6] == ',') {
1470
            /* have params -> parse them */
1471
            opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1472
            if (!opts)
1473
                return  -1;
1474
        } else {
1475
            /* create empty opts */
1476
            opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1477
        }
1478
        qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1479
        return 0;
1480
    }
1481

    
1482
    return -1;
1483
}
1484

    
1485
char *qemu_find_file(int type, const char *name)
1486
{
1487
    int len;
1488
    const char *subdir;
1489
    char *buf;
1490

    
1491
    /* If name contains path separators then try it as a straight path.  */
1492
    if ((strchr(name, '/') || strchr(name, '\\'))
1493
        && access(name, R_OK) == 0) {
1494
        return qemu_strdup(name);
1495
    }
1496
    switch (type) {
1497
    case QEMU_FILE_TYPE_BIOS:
1498
        subdir = "";
1499
        break;
1500
    case QEMU_FILE_TYPE_KEYMAP:
1501
        subdir = "keymaps/";
1502
        break;
1503
    default:
1504
        abort();
1505
    }
1506
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1507
    buf = qemu_mallocz(len);
1508
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1509
    if (access(buf, R_OK)) {
1510
        qemu_free(buf);
1511
        return NULL;
1512
    }
1513
    return buf;
1514
}
1515

    
1516
static int device_help_func(QemuOpts *opts, void *opaque)
1517
{
1518
    return qdev_device_help(opts);
1519
}
1520

    
1521
static int device_init_func(QemuOpts *opts, void *opaque)
1522
{
1523
    DeviceState *dev;
1524

    
1525
    dev = qdev_device_add(opts);
1526
    if (!dev)
1527
        return -1;
1528
    return 0;
1529
}
1530

    
1531
static int chardev_init_func(QemuOpts *opts, void *opaque)
1532
{
1533
    CharDriverState *chr;
1534

    
1535
    chr = qemu_chr_open_opts(opts, NULL);
1536
    if (!chr)
1537
        return -1;
1538
    return 0;
1539
}
1540

    
1541
#ifdef CONFIG_VIRTFS
1542
static int fsdev_init_func(QemuOpts *opts, void *opaque)
1543
{
1544
    int ret;
1545
    ret = qemu_fsdev_add(opts);
1546

    
1547
    return ret;
1548
}
1549
#endif
1550

    
1551
static int mon_init_func(QemuOpts *opts, void *opaque)
1552
{
1553
    CharDriverState *chr;
1554
    const char *chardev;
1555
    const char *mode;
1556
    int flags;
1557

    
1558
    mode = qemu_opt_get(opts, "mode");
1559
    if (mode == NULL) {
1560
        mode = "readline";
1561
    }
1562
    if (strcmp(mode, "readline") == 0) {
1563
        flags = MONITOR_USE_READLINE;
1564
    } else if (strcmp(mode, "control") == 0) {
1565
        flags = MONITOR_USE_CONTROL;
1566
    } else {
1567
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1568
        exit(1);
1569
    }
1570

    
1571
    if (qemu_opt_get_bool(opts, "default", 0))
1572
        flags |= MONITOR_IS_DEFAULT;
1573

    
1574
    chardev = qemu_opt_get(opts, "chardev");
1575
    chr = qemu_chr_find(chardev);
1576
    if (chr == NULL) {
1577
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1578
        exit(1);
1579
    }
1580

    
1581
    monitor_init(chr, flags);
1582
    return 0;
1583
}
1584

    
1585
static void monitor_parse(const char *optarg, const char *mode)
1586
{
1587
    static int monitor_device_index = 0;
1588
    QemuOpts *opts;
1589
    const char *p;
1590
    char label[32];
1591
    int def = 0;
1592

    
1593
    if (strstart(optarg, "chardev:", &p)) {
1594
        snprintf(label, sizeof(label), "%s", p);
1595
    } else {
1596
        snprintf(label, sizeof(label), "compat_monitor%d",
1597
                 monitor_device_index);
1598
        if (monitor_device_index == 0) {
1599
            def = 1;
1600
        }
1601
        opts = qemu_chr_parse_compat(label, optarg);
1602
        if (!opts) {
1603
            fprintf(stderr, "parse error: %s\n", optarg);
1604
            exit(1);
1605
        }
1606
    }
1607

    
1608
    opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1609
    if (!opts) {
1610
        fprintf(stderr, "duplicate chardev: %s\n", label);
1611
        exit(1);
1612
    }
1613
    qemu_opt_set(opts, "mode", mode);
1614
    qemu_opt_set(opts, "chardev", label);
1615
    if (def)
1616
        qemu_opt_set(opts, "default", "on");
1617
    monitor_device_index++;
1618
}
1619

    
1620
struct device_config {
1621
    enum {
1622
        DEV_USB,       /* -usbdevice     */
1623
        DEV_BT,        /* -bt            */
1624
        DEV_SERIAL,    /* -serial        */
1625
        DEV_PARALLEL,  /* -parallel      */
1626
        DEV_VIRTCON,   /* -virtioconsole */
1627
        DEV_DEBUGCON,  /* -debugcon */
1628
    } type;
1629
    const char *cmdline;
1630
    QTAILQ_ENTRY(device_config) next;
1631
};
1632
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1633

    
1634
static void add_device_config(int type, const char *cmdline)
1635
{
1636
    struct device_config *conf;
1637

    
1638
    conf = qemu_mallocz(sizeof(*conf));
1639
    conf->type = type;
1640
    conf->cmdline = cmdline;
1641
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1642
}
1643

    
1644
static int foreach_device_config(int type, int (*func)(const char *cmdline))
1645
{
1646
    struct device_config *conf;
1647
    int rc;
1648

    
1649
    QTAILQ_FOREACH(conf, &device_configs, next) {
1650
        if (conf->type != type)
1651
            continue;
1652
        rc = func(conf->cmdline);
1653
        if (0 != rc)
1654
            return rc;
1655
    }
1656
    return 0;
1657
}
1658

    
1659
static int serial_parse(const char *devname)
1660
{
1661
    static int index = 0;
1662
    char label[32];
1663

    
1664
    if (strcmp(devname, "none") == 0)
1665
        return 0;
1666
    if (index == MAX_SERIAL_PORTS) {
1667
        fprintf(stderr, "qemu: too many serial ports\n");
1668
        exit(1);
1669
    }
1670
    snprintf(label, sizeof(label), "serial%d", index);
1671
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
1672
    if (!serial_hds[index]) {
1673
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1674
                devname, strerror(errno));
1675
        return -1;
1676
    }
1677
    index++;
1678
    return 0;
1679
}
1680

    
1681
static int parallel_parse(const char *devname)
1682
{
1683
    static int index = 0;
1684
    char label[32];
1685

    
1686
    if (strcmp(devname, "none") == 0)
1687
        return 0;
1688
    if (index == MAX_PARALLEL_PORTS) {
1689
        fprintf(stderr, "qemu: too many parallel ports\n");
1690
        exit(1);
1691
    }
1692
    snprintf(label, sizeof(label), "parallel%d", index);
1693
    parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1694
    if (!parallel_hds[index]) {
1695
        fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1696
                devname, strerror(errno));
1697
        return -1;
1698
    }
1699
    index++;
1700
    return 0;
1701
}
1702

    
1703
static int virtcon_parse(const char *devname)
1704
{
1705
    QemuOptsList *device = qemu_find_opts("device");
1706
    static int index = 0;
1707
    char label[32];
1708
    QemuOpts *bus_opts, *dev_opts;
1709

    
1710
    if (strcmp(devname, "none") == 0)
1711
        return 0;
1712
    if (index == MAX_VIRTIO_CONSOLES) {
1713
        fprintf(stderr, "qemu: too many virtio consoles\n");
1714
        exit(1);
1715
    }
1716

    
1717
    bus_opts = qemu_opts_create(device, NULL, 0);
1718
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
1719

    
1720
    dev_opts = qemu_opts_create(device, NULL, 0);
1721
    qemu_opt_set(dev_opts, "driver", "virtconsole");
1722

    
1723
    snprintf(label, sizeof(label), "virtcon%d", index);
1724
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1725
    if (!virtcon_hds[index]) {
1726
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1727
                devname, strerror(errno));
1728
        return -1;
1729
    }
1730
    qemu_opt_set(dev_opts, "chardev", label);
1731

    
1732
    index++;
1733
    return 0;
1734
}
1735

    
1736
static int debugcon_parse(const char *devname)
1737
{   
1738
    QemuOpts *opts;
1739

    
1740
    if (!qemu_chr_open("debugcon", devname, NULL)) {
1741
        exit(1);
1742
    }
1743
    opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1744
    if (!opts) {
1745
        fprintf(stderr, "qemu: already have a debugcon device\n");
1746
        exit(1);
1747
    }
1748
    qemu_opt_set(opts, "driver", "isa-debugcon");
1749
    qemu_opt_set(opts, "chardev", "debugcon");
1750
    return 0;
1751
}
1752

    
1753
void qemu_add_exit_notifier(Notifier *notify)
1754
{
1755
    notifier_list_add(&exit_notifiers, notify);
1756
}
1757

    
1758
void qemu_remove_exit_notifier(Notifier *notify)
1759
{
1760
    notifier_list_remove(&exit_notifiers, notify);
1761
}
1762

    
1763
static void qemu_run_exit_notifiers(void)
1764
{
1765
    notifier_list_notify(&exit_notifiers);
1766
}
1767

    
1768
static const QEMUOption *lookup_opt(int argc, char **argv,
1769
                                    const char **poptarg, int *poptind)
1770
{
1771
    const QEMUOption *popt;
1772
    int optind = *poptind;
1773
    char *r = argv[optind];
1774
    const char *optarg;
1775

    
1776
    loc_set_cmdline(argv, optind, 1);
1777
    optind++;
1778
    /* Treat --foo the same as -foo.  */
1779
    if (r[1] == '-')
1780
        r++;
1781
    popt = qemu_options;
1782
    for(;;) {
1783
        if (!popt->name) {
1784
            error_report("invalid option");
1785
            exit(1);
1786
        }
1787
        if (!strcmp(popt->name, r + 1))
1788
            break;
1789
        popt++;
1790
    }
1791
    if (popt->flags & HAS_ARG) {
1792
        if (optind >= argc) {
1793
            error_report("requires an argument");
1794
            exit(1);
1795
        }
1796
        optarg = argv[optind++];
1797
        loc_set_cmdline(argv, optind - 2, 2);
1798
    } else {
1799
        optarg = NULL;
1800
    }
1801

    
1802
    *poptarg = optarg;
1803
    *poptind = optind;
1804

    
1805
    return popt;
1806
}
1807

    
1808
int main(int argc, char **argv, char **envp)
1809
{
1810
    const char *gdbstub_dev = NULL;
1811
    int i;
1812
    int snapshot, linux_boot;
1813
    const char *icount_option = NULL;
1814
    const char *initrd_filename;
1815
    const char *kernel_filename, *kernel_cmdline;
1816
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1817
    DisplayState *ds;
1818
    DisplayChangeListener *dcl;
1819
    int cyls, heads, secs, translation;
1820
    QemuOpts *hda_opts = NULL, *opts;
1821
    QemuOptsList *olist;
1822
    int optind;
1823
    const char *optarg;
1824
    const char *loadvm = NULL;
1825
    QEMUMachine *machine;
1826
    const char *cpu_model;
1827
    int tb_size;
1828
    const char *pid_file = NULL;
1829
    const char *incoming = NULL;
1830
    int show_vnc_port = 0;
1831
    int defconfig = 1;
1832

    
1833
#ifdef CONFIG_SIMPLE_TRACE
1834
    const char *trace_file = NULL;
1835
#endif
1836
    atexit(qemu_run_exit_notifiers);
1837
    error_set_progname(argv[0]);
1838

    
1839
    init_clocks();
1840

    
1841
    qemu_cache_utils_init(envp);
1842

    
1843
    QLIST_INIT (&vm_change_state_head);
1844
    os_setup_early_signal_handling();
1845

    
1846
    module_call_init(MODULE_INIT_MACHINE);
1847
    machine = find_default_machine();
1848
    cpu_model = NULL;
1849
    initrd_filename = NULL;
1850
    ram_size = 0;
1851
    snapshot = 0;
1852
    kernel_filename = NULL;
1853
    kernel_cmdline = "";
1854
    cyls = heads = secs = 0;
1855
    translation = BIOS_ATA_TRANSLATION_AUTO;
1856

    
1857
    for (i = 0; i < MAX_NODES; i++) {
1858
        node_mem[i] = 0;
1859
        node_cpumask[i] = 0;
1860
    }
1861

    
1862
    nb_numa_nodes = 0;
1863
    nb_nics = 0;
1864

    
1865
    tb_size = 0;
1866
    autostart= 1;
1867

    
1868
    /* first pass of option parsing */
1869
    optind = 1;
1870
    while (optind < argc) {
1871
        if (argv[optind][0] != '-') {
1872
            /* disk image */
1873
            optind++;
1874
            continue;
1875
        } else {
1876
            const QEMUOption *popt;
1877

    
1878
            popt = lookup_opt(argc, argv, &optarg, &optind);
1879
            switch (popt->index) {
1880
            case QEMU_OPTION_nodefconfig:
1881
                defconfig=0;
1882
                break;
1883
            }
1884
        }
1885
    }
1886

    
1887
    if (defconfig) {
1888
        int ret;
1889

    
1890
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1891
        if (ret < 0 && ret != -ENOENT) {
1892
            exit(1);
1893
        }
1894

    
1895
        ret = qemu_read_config_file(arch_config_name);
1896
        if (ret < 0 && ret != -ENOENT) {
1897
            exit(1);
1898
        }
1899
    }
1900
    cpudef_init();
1901

    
1902
    /* second pass of option parsing */
1903
    optind = 1;
1904
    for(;;) {
1905
        if (optind >= argc)
1906
            break;
1907
        if (argv[optind][0] != '-') {
1908
            hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1909
        } else {
1910
            const QEMUOption *popt;
1911

    
1912
            popt = lookup_opt(argc, argv, &optarg, &optind);
1913
            if (!(popt->arch_mask & arch_type)) {
1914
                printf("Option %s not supported for this target\n", popt->name);
1915
                exit(1);
1916
            }
1917
            switch(popt->index) {
1918
            case QEMU_OPTION_M:
1919
                machine = find_machine(optarg);
1920
                if (!machine) {
1921
                    QEMUMachine *m;
1922
                    printf("Supported machines are:\n");
1923
                    for(m = first_machine; m != NULL; m = m->next) {
1924
                        if (m->alias)
1925
                            printf("%-10s %s (alias of %s)\n",
1926
                                   m->alias, m->desc, m->name);
1927
                        printf("%-10s %s%s\n",
1928
                               m->name, m->desc,
1929
                               m->is_default ? " (default)" : "");
1930
                    }
1931
                    exit(*optarg != '?');
1932
                }
1933
                break;
1934
            case QEMU_OPTION_cpu:
1935
                /* hw initialization will check this */
1936
                if (*optarg == '?') {
1937
                    list_cpus(stdout, &fprintf, optarg);
1938
                    exit(0);
1939
                } else {
1940
                    cpu_model = optarg;
1941
                }
1942
                break;
1943
            case QEMU_OPTION_initrd:
1944
                initrd_filename = optarg;
1945
                break;
1946
            case QEMU_OPTION_hda:
1947
                if (cyls == 0)
1948
                    hda_opts = drive_add(optarg, HD_ALIAS, 0);
1949
                else
1950
                    hda_opts = drive_add(optarg, HD_ALIAS
1951
                             ",cyls=%d,heads=%d,secs=%d%s",
1952
                             0, cyls, heads, secs,
1953
                             translation == BIOS_ATA_TRANSLATION_LBA ?
1954
                                 ",trans=lba" :
1955
                             translation == BIOS_ATA_TRANSLATION_NONE ?
1956
                                 ",trans=none" : "");
1957
                 break;
1958
            case QEMU_OPTION_hdb:
1959
            case QEMU_OPTION_hdc:
1960
            case QEMU_OPTION_hdd:
1961
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
1962
                break;
1963
            case QEMU_OPTION_drive:
1964
                drive_add(NULL, "%s", optarg);
1965
                break;
1966
            case QEMU_OPTION_set:
1967
                if (qemu_set_option(optarg) != 0)
1968
                    exit(1);
1969
                break;
1970
            case QEMU_OPTION_global:
1971
                if (qemu_global_option(optarg) != 0)
1972
                    exit(1);
1973
                break;
1974
            case QEMU_OPTION_mtdblock:
1975
                drive_add(optarg, MTD_ALIAS);
1976
                break;
1977
            case QEMU_OPTION_sd:
1978
                drive_add(optarg, SD_ALIAS);
1979
                break;
1980
            case QEMU_OPTION_pflash:
1981
                drive_add(optarg, PFLASH_ALIAS);
1982
                break;
1983
            case QEMU_OPTION_snapshot:
1984
                snapshot = 1;
1985
                break;
1986
            case QEMU_OPTION_hdachs:
1987
                {
1988
                    const char *p;
1989
                    p = optarg;
1990
                    cyls = strtol(p, (char **)&p, 0);
1991
                    if (cyls < 1 || cyls > 16383)
1992
                        goto chs_fail;
1993
                    if (*p != ',')
1994
                        goto chs_fail;
1995
                    p++;
1996
                    heads = strtol(p, (char **)&p, 0);
1997
                    if (heads < 1 || heads > 16)
1998
                        goto chs_fail;
1999
                    if (*p != ',')
2000
                        goto chs_fail;
2001
                    p++;
2002
                    secs = strtol(p, (char **)&p, 0);
2003
                    if (secs < 1 || secs > 63)
2004
                        goto chs_fail;
2005
                    if (*p == ',') {
2006
                        p++;
2007
                        if (!strcmp(p, "none"))
2008
                            translation = BIOS_ATA_TRANSLATION_NONE;
2009
                        else if (!strcmp(p, "lba"))
2010
                            translation = BIOS_ATA_TRANSLATION_LBA;
2011
                        else if (!strcmp(p, "auto"))
2012
                            translation = BIOS_ATA_TRANSLATION_AUTO;
2013
                        else
2014
                            goto chs_fail;
2015
                    } else if (*p != '\0') {
2016
                    chs_fail:
2017
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
2018
                        exit(1);
2019
                    }
2020
                    if (hda_opts != NULL) {
2021
                        char num[16];
2022
                        snprintf(num, sizeof(num), "%d", cyls);
2023
                        qemu_opt_set(hda_opts, "cyls", num);
2024
                        snprintf(num, sizeof(num), "%d", heads);
2025
                        qemu_opt_set(hda_opts, "heads", num);
2026
                        snprintf(num, sizeof(num), "%d", secs);
2027
                        qemu_opt_set(hda_opts, "secs", num);
2028
                        if (translation == BIOS_ATA_TRANSLATION_LBA)
2029
                            qemu_opt_set(hda_opts, "trans", "lba");
2030
                        if (translation == BIOS_ATA_TRANSLATION_NONE)
2031
                            qemu_opt_set(hda_opts, "trans", "none");
2032
                    }
2033
                }
2034
                break;
2035
            case QEMU_OPTION_numa:
2036
                if (nb_numa_nodes >= MAX_NODES) {
2037
                    fprintf(stderr, "qemu: too many NUMA nodes\n");
2038
                    exit(1);
2039
                }
2040
                numa_add(optarg);
2041
                break;
2042
            case QEMU_OPTION_nographic:
2043
                display_type = DT_NOGRAPHIC;
2044
                break;
2045
#ifdef CONFIG_CURSES
2046
            case QEMU_OPTION_curses:
2047
                display_type = DT_CURSES;
2048
                break;
2049
#endif
2050
            case QEMU_OPTION_portrait:
2051
                graphic_rotate = 1;
2052
                break;
2053
            case QEMU_OPTION_kernel:
2054
                kernel_filename = optarg;
2055
                break;
2056
            case QEMU_OPTION_append:
2057
                kernel_cmdline = optarg;
2058
                break;
2059
            case QEMU_OPTION_cdrom:
2060
                drive_add(optarg, CDROM_ALIAS);
2061
                break;
2062
            case QEMU_OPTION_boot:
2063
                {
2064
                    static const char * const params[] = {
2065
                        "order", "once", "menu", NULL
2066
                    };
2067
                    char buf[sizeof(boot_devices)];
2068
                    char *standard_boot_devices;
2069
                    int legacy = 0;
2070

    
2071
                    if (!strchr(optarg, '=')) {
2072
                        legacy = 1;
2073
                        pstrcpy(buf, sizeof(buf), optarg);
2074
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2075
                        fprintf(stderr,
2076
                                "qemu: unknown boot parameter '%s' in '%s'\n",
2077
                                buf, optarg);
2078
                        exit(1);
2079
                    }
2080

    
2081
                    if (legacy ||
2082
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
2083
                        validate_bootdevices(buf);
2084
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
2085
                    }
2086
                    if (!legacy) {
2087
                        if (get_param_value(buf, sizeof(buf),
2088
                                            "once", optarg)) {
2089
                            validate_bootdevices(buf);
2090
                            standard_boot_devices = qemu_strdup(boot_devices);
2091
                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
2092
                            qemu_register_reset(restore_boot_devices,
2093
                                                standard_boot_devices);
2094
                        }
2095
                        if (get_param_value(buf, sizeof(buf),
2096
                                            "menu", optarg)) {
2097
                            if (!strcmp(buf, "on")) {
2098
                                boot_menu = 1;
2099
                            } else if (!strcmp(buf, "off")) {
2100
                                boot_menu = 0;
2101
                            } else {
2102
                                fprintf(stderr,
2103
                                        "qemu: invalid option value '%s'\n",
2104
                                        buf);
2105
                                exit(1);
2106
                            }
2107
                        }
2108
                    }
2109
                }
2110
                break;
2111
            case QEMU_OPTION_fda:
2112
            case QEMU_OPTION_fdb:
2113
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2114
                break;
2115
            case QEMU_OPTION_no_fd_bootchk:
2116
                fd_bootchk = 0;
2117
                break;
2118
            case QEMU_OPTION_netdev:
2119
                if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2120
                    exit(1);
2121
                }
2122
                break;
2123
            case QEMU_OPTION_net:
2124
                if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2125
                    exit(1);
2126
                }
2127
                break;
2128
#ifdef CONFIG_SLIRP
2129
            case QEMU_OPTION_tftp:
2130
                legacy_tftp_prefix = optarg;
2131
                break;
2132
            case QEMU_OPTION_bootp:
2133
                legacy_bootp_filename = optarg;
2134
                break;
2135
            case QEMU_OPTION_redir:
2136
                if (net_slirp_redir(optarg) < 0)
2137
                    exit(1);
2138
                break;
2139
#endif
2140
            case QEMU_OPTION_bt:
2141
                add_device_config(DEV_BT, optarg);
2142
                break;
2143
            case QEMU_OPTION_audio_help:
2144
                if (!(audio_available())) {
2145
                    printf("Option %s not supported for this target\n", popt->name);
2146
                    exit(1);
2147
                }
2148
                AUD_help ();
2149
                exit (0);
2150
                break;
2151
            case QEMU_OPTION_soundhw:
2152
                if (!(audio_available())) {
2153
                    printf("Option %s not supported for this target\n", popt->name);
2154
                    exit(1);
2155
                }
2156
                select_soundhw (optarg);
2157
                break;
2158
            case QEMU_OPTION_h:
2159
                help(0);
2160
                break;
2161
            case QEMU_OPTION_version:
2162
                version();
2163
                exit(0);
2164
                break;
2165
            case QEMU_OPTION_m: {
2166
                uint64_t value;
2167
                char *ptr;
2168

    
2169
                value = strtoul(optarg, &ptr, 10);
2170
                switch (*ptr) {
2171
                case 0: case 'M': case 'm':
2172
                    value <<= 20;
2173
                    break;
2174
                case 'G': case 'g':
2175
                    value <<= 30;
2176
                    break;
2177
                default:
2178
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2179
                    exit(1);
2180
                }
2181

    
2182
                /* On 32-bit hosts, QEMU is limited by virtual address space */
2183
                if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2184
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2185
                    exit(1);
2186
                }
2187
                if (value != (uint64_t)(ram_addr_t)value) {
2188
                    fprintf(stderr, "qemu: ram size too large\n");
2189
                    exit(1);
2190
                }
2191
                ram_size = value;
2192
                break;
2193
            }
2194
            case QEMU_OPTION_mempath:
2195
                mem_path = optarg;
2196
                break;
2197
#ifdef MAP_POPULATE
2198
            case QEMU_OPTION_mem_prealloc:
2199
                mem_prealloc = 1;
2200
                break;
2201
#endif
2202
            case QEMU_OPTION_d:
2203
                set_cpu_log(optarg);
2204
                break;
2205
            case QEMU_OPTION_s:
2206
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2207
                break;
2208
            case QEMU_OPTION_gdb:
2209
                gdbstub_dev = optarg;
2210
                break;
2211
            case QEMU_OPTION_L:
2212
                data_dir = optarg;
2213
                break;
2214
            case QEMU_OPTION_bios:
2215
                bios_name = optarg;
2216
                break;
2217
            case QEMU_OPTION_singlestep:
2218
                singlestep = 1;
2219
                break;
2220
            case QEMU_OPTION_S:
2221
                autostart = 0;
2222
                break;
2223
            case QEMU_OPTION_k:
2224
                keyboard_layout = optarg;
2225
                break;
2226
            case QEMU_OPTION_localtime:
2227
                rtc_utc = 0;
2228
                break;
2229
            case QEMU_OPTION_vga:
2230
                select_vgahw (optarg);
2231
                break;
2232
            case QEMU_OPTION_g:
2233
                {
2234
                    const char *p;
2235
                    int w, h, depth;
2236
                    p = optarg;
2237
                    w = strtol(p, (char **)&p, 10);
2238
                    if (w <= 0) {
2239
                    graphic_error:
2240
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
2241
                        exit(1);
2242
                    }
2243
                    if (*p != 'x')
2244
                        goto graphic_error;
2245
                    p++;
2246
                    h = strtol(p, (char **)&p, 10);
2247
                    if (h <= 0)
2248
                        goto graphic_error;
2249
                    if (*p == 'x') {
2250
                        p++;
2251
                        depth = strtol(p, (char **)&p, 10);
2252
                        if (depth != 8 && depth != 15 && depth != 16 &&
2253
                            depth != 24 && depth != 32)
2254
                            goto graphic_error;
2255
                    } else if (*p == '\0') {
2256
                        depth = graphic_depth;
2257
                    } else {
2258
                        goto graphic_error;
2259
                    }
2260

    
2261
                    graphic_width = w;
2262
                    graphic_height = h;
2263
                    graphic_depth = depth;
2264
                }
2265
                break;
2266
            case QEMU_OPTION_echr:
2267
                {
2268
                    char *r;
2269
                    term_escape_char = strtol(optarg, &r, 0);
2270
                    if (r == optarg)
2271
                        printf("Bad argument to echr\n");
2272
                    break;
2273
                }
2274
            case QEMU_OPTION_monitor:
2275
                monitor_parse(optarg, "readline");
2276
                default_monitor = 0;
2277
                break;
2278
            case QEMU_OPTION_qmp:
2279
                monitor_parse(optarg, "control");
2280
                default_monitor = 0;
2281
                break;
2282
            case QEMU_OPTION_mon:
2283
                opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2284
                if (!opts) {
2285
                    exit(1);
2286
                }
2287
                default_monitor = 0;
2288
                break;
2289
            case QEMU_OPTION_chardev:
2290
                opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2291
                if (!opts) {
2292
                    exit(1);
2293
                }
2294
                break;
2295
            case QEMU_OPTION_fsdev:
2296
                olist = qemu_find_opts("fsdev");
2297
                if (!olist) {
2298
                    fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2299
                    exit(1);
2300
                }
2301
                opts = qemu_opts_parse(olist, optarg, 1);
2302
                if (!opts) {
2303
                    fprintf(stderr, "parse error: %s\n", optarg);
2304
                    exit(1);
2305
                }
2306
                break;
2307
            case QEMU_OPTION_virtfs: {
2308
                char *arg_fsdev = NULL;
2309
                char *arg_9p = NULL;
2310
                int len = 0;
2311

    
2312
                olist = qemu_find_opts("virtfs");
2313
                if (!olist) {
2314
                    fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2315
                    exit(1);
2316
                }
2317
                opts = qemu_opts_parse(olist, optarg, 1);
2318
                if (!opts) {
2319
                    fprintf(stderr, "parse error: %s\n", optarg);
2320
                    exit(1);
2321
                }
2322

    
2323
                if (qemu_opt_get(opts, "fstype") == NULL ||
2324
                        qemu_opt_get(opts, "mount_tag") == NULL ||
2325
                        qemu_opt_get(opts, "path") == NULL ||
2326
                        qemu_opt_get(opts, "security_model") == NULL) {
2327
                    fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2328
                            "security_model=[mapped|passthrough|none],"
2329
                            "mnt_tag=tag.\n");
2330
                    exit(1);
2331
                }
2332

    
2333
                len = strlen(",id=,path=,security_model=");
2334
                len += strlen(qemu_opt_get(opts, "fstype"));
2335
                len += strlen(qemu_opt_get(opts, "mount_tag"));
2336
                len += strlen(qemu_opt_get(opts, "path"));
2337
                len += strlen(qemu_opt_get(opts, "security_model"));
2338
                arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2339

    
2340
                snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2341
                         "%s,id=%s,path=%s,security_model=%s",
2342
                         qemu_opt_get(opts, "fstype"),
2343
                         qemu_opt_get(opts, "mount_tag"),
2344
                         qemu_opt_get(opts, "path"),
2345
                         qemu_opt_get(opts, "security_model"));
2346

    
2347
                len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2348
                len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2349
                arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2350

    
2351
                snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2352
                         "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2353
                         qemu_opt_get(opts, "mount_tag"),
2354
                         qemu_opt_get(opts, "mount_tag"));
2355

    
2356
                if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2357
                    fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2358
                    exit(1);
2359
                }
2360

    
2361
                if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2362
                    fprintf(stderr, "parse error [device]: %s\n", optarg);
2363
                    exit(1);
2364
                }
2365

    
2366
                qemu_free(arg_fsdev);
2367
                qemu_free(arg_9p);
2368
                break;
2369
            }
2370
            case QEMU_OPTION_serial:
2371
                add_device_config(DEV_SERIAL, optarg);
2372
                default_serial = 0;
2373
                if (strncmp(optarg, "mon:", 4) == 0) {
2374
                    default_monitor = 0;
2375
                }
2376
                break;
2377
            case QEMU_OPTION_watchdog:
2378
                if (watchdog) {
2379
                    fprintf(stderr,
2380
                            "qemu: only one watchdog option may be given\n");
2381
                    return 1;
2382
                }
2383
                watchdog = optarg;
2384
                break;
2385
            case QEMU_OPTION_watchdog_action:
2386
                if (select_watchdog_action(optarg) == -1) {
2387
                    fprintf(stderr, "Unknown -watchdog-action parameter\n");
2388
                    exit(1);
2389
                }
2390
                break;
2391
            case QEMU_OPTION_virtiocon:
2392
                add_device_config(DEV_VIRTCON, optarg);
2393
                default_virtcon = 0;
2394
                if (strncmp(optarg, "mon:", 4) == 0) {
2395
                    default_monitor = 0;
2396
                }
2397
                break;
2398
            case QEMU_OPTION_parallel:
2399
                add_device_config(DEV_PARALLEL, optarg);
2400
                default_parallel = 0;
2401
                if (strncmp(optarg, "mon:", 4) == 0) {
2402
                    default_monitor = 0;
2403
                }
2404
                break;
2405
            case QEMU_OPTION_debugcon:
2406
                add_device_config(DEV_DEBUGCON, optarg);
2407
                break;
2408
            case QEMU_OPTION_loadvm:
2409
                loadvm = optarg;
2410
                break;
2411
            case QEMU_OPTION_full_screen:
2412
                full_screen = 1;
2413
                break;
2414
#ifdef CONFIG_SDL
2415
            case QEMU_OPTION_no_frame:
2416
                no_frame = 1;
2417
                break;
2418
            case QEMU_OPTION_alt_grab:
2419
                alt_grab = 1;
2420
                break;
2421
            case QEMU_OPTION_ctrl_grab:
2422
                ctrl_grab = 1;
2423
                break;
2424
            case QEMU_OPTION_no_quit:
2425
                no_quit = 1;
2426
                break;
2427
            case QEMU_OPTION_sdl:
2428
                display_type = DT_SDL;
2429
                break;
2430
#endif
2431
            case QEMU_OPTION_pidfile:
2432
                pid_file = optarg;
2433
                break;
2434
            case QEMU_OPTION_win2k_hack:
2435
                win2k_install_hack = 1;
2436
                break;
2437
            case QEMU_OPTION_rtc_td_hack:
2438
                rtc_td_hack = 1;
2439
                break;
2440
            case QEMU_OPTION_acpitable:
2441
                do_acpitable_option(optarg);
2442
                break;
2443
            case QEMU_OPTION_smbios:
2444
                do_smbios_option(optarg);
2445
                break;
2446
            case QEMU_OPTION_enable_kvm:
2447
                kvm_allowed = 1;
2448
                break;
2449
            case QEMU_OPTION_usb:
2450
                usb_enabled = 1;
2451
                break;
2452
            case QEMU_OPTION_usbdevice:
2453
                usb_enabled = 1;
2454
                add_device_config(DEV_USB, optarg);
2455
                break;
2456
            case QEMU_OPTION_device:
2457
                if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2458
                    exit(1);
2459
                }
2460
                break;
2461
            case QEMU_OPTION_smp:
2462
                smp_parse(optarg);
2463
                if (smp_cpus < 1) {
2464
                    fprintf(stderr, "Invalid number of CPUs\n");
2465
                    exit(1);
2466
                }
2467
                if (max_cpus < smp_cpus) {
2468
                    fprintf(stderr, "maxcpus must be equal to or greater than "
2469
                            "smp\n");
2470
                    exit(1);
2471
                }
2472
                if (max_cpus > 255) {
2473
                    fprintf(stderr, "Unsupported number of maxcpus\n");
2474
                    exit(1);
2475
                }
2476
                break;
2477
            case QEMU_OPTION_vnc:
2478
                display_remote++;
2479
                vnc_display = optarg;
2480
                break;
2481
            case QEMU_OPTION_no_acpi:
2482
                acpi_enabled = 0;
2483
                break;
2484
            case QEMU_OPTION_no_hpet:
2485
                no_hpet = 1;
2486
                break;
2487
            case QEMU_OPTION_balloon:
2488
                if (balloon_parse(optarg) < 0) {
2489
                    fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2490
                    exit(1);
2491
                }
2492
                break;
2493
            case QEMU_OPTION_no_reboot:
2494
                no_reboot = 1;
2495
                break;
2496
            case QEMU_OPTION_no_shutdown:
2497
                no_shutdown = 1;
2498
                break;
2499
            case QEMU_OPTION_show_cursor:
2500
                cursor_hide = 0;
2501
                break;
2502
            case QEMU_OPTION_uuid:
2503
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2504
                    fprintf(stderr, "Fail to parse UUID string."
2505
                            " Wrong format.\n");
2506
                    exit(1);
2507
                }
2508
                break;
2509
            case QEMU_OPTION_option_rom:
2510
                if (nb_option_roms >= MAX_OPTION_ROMS) {
2511
                    fprintf(stderr, "Too many option ROMs\n");
2512
                    exit(1);
2513
                }
2514
                option_rom[nb_option_roms] = optarg;
2515
                nb_option_roms++;
2516
                break;
2517
            case QEMU_OPTION_semihosting:
2518
                semihosting_enabled = 1;
2519
                break;
2520
            case QEMU_OPTION_name:
2521
                qemu_name = qemu_strdup(optarg);
2522
                 {
2523
                     char *p = strchr(qemu_name, ',');
2524
                     if (p != NULL) {
2525
                        *p++ = 0;
2526
                        if (strncmp(p, "process=", 8)) {
2527
                            fprintf(stderr, "Unknown subargument %s to -name", p);
2528
                            exit(1);
2529
                        }
2530
                        p += 8;
2531
                        os_set_proc_name(p);
2532
                     }        
2533
                 }        
2534
                break;
2535
            case QEMU_OPTION_prom_env:
2536
                if (nb_prom_envs >= MAX_PROM_ENVS) {
2537
                    fprintf(stderr, "Too many prom variables\n");
2538
                    exit(1);
2539
                }
2540
                prom_envs[nb_prom_envs] = optarg;
2541
                nb_prom_envs++;
2542
                break;
2543
            case QEMU_OPTION_old_param:
2544
                old_param = 1;
2545
                break;
2546
            case QEMU_OPTION_clock:
2547
                configure_alarms(optarg);
2548
                break;
2549
            case QEMU_OPTION_startdate:
2550
                configure_rtc_date_offset(optarg, 1);
2551
                break;
2552
            case QEMU_OPTION_rtc:
2553
                opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2554
                if (!opts) {
2555
                    exit(1);
2556
                }
2557
                configure_rtc(opts);
2558
                break;
2559
            case QEMU_OPTION_tb_size:
2560
                tb_size = strtol(optarg, NULL, 0);
2561
                if (tb_size < 0)
2562
                    tb_size = 0;
2563
                break;
2564
            case QEMU_OPTION_icount:
2565
                icount_option = optarg;
2566
                break;
2567
            case QEMU_OPTION_incoming:
2568
                incoming = optarg;
2569
                incoming_expected = true;
2570
                break;
2571
            case QEMU_OPTION_nodefaults:
2572
                default_serial = 0;
2573
                default_parallel = 0;
2574
                default_virtcon = 0;
2575
                default_monitor = 0;
2576
                default_vga = 0;
2577
                default_net = 0;
2578
                default_floppy = 0;
2579
                default_cdrom = 0;
2580
                default_sdcard = 0;
2581
                break;
2582
            case QEMU_OPTION_xen_domid:
2583
                if (!(xen_available())) {
2584
                    printf("Option %s not supported for this target\n", popt->name);
2585
                    exit(1);
2586
                }
2587
                xen_domid = atoi(optarg);
2588
                break;
2589
            case QEMU_OPTION_xen_create:
2590
                if (!(xen_available())) {
2591
                    printf("Option %s not supported for this target\n", popt->name);
2592
                    exit(1);
2593
                }
2594
                xen_mode = XEN_CREATE;
2595
                break;
2596
            case QEMU_OPTION_xen_attach:
2597
                if (!(xen_available())) {
2598
                    printf("Option %s not supported for this target\n", popt->name);
2599
                    exit(1);
2600
                }
2601
                xen_mode = XEN_ATTACH;
2602
                break;
2603
#ifdef CONFIG_SIMPLE_TRACE
2604
            case QEMU_OPTION_trace:
2605
                opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2606
                if (opts) {
2607
                    trace_file = qemu_opt_get(opts, "file");
2608
                }
2609
                break;
2610
#endif
2611
            case QEMU_OPTION_readconfig:
2612
                {
2613
                    int ret = qemu_read_config_file(optarg);
2614
                    if (ret < 0) {
2615
                        fprintf(stderr, "read config %s: %s\n", optarg,
2616
                            strerror(-ret));
2617
                        exit(1);
2618
                    }
2619
                    break;
2620
                }
2621
            case QEMU_OPTION_spice:
2622
                olist = qemu_find_opts("spice");
2623
                if (!olist) {
2624
                    fprintf(stderr, "spice is not supported by this qemu build.\n");
2625
                    exit(1);
2626
                }
2627
                opts = qemu_opts_parse(olist, optarg, 0);
2628
                if (!opts) {
2629
                    fprintf(stderr, "parse error: %s\n", optarg);
2630
                    exit(1);
2631
                }
2632
                break;
2633
            case QEMU_OPTION_writeconfig:
2634
                {
2635
                    FILE *fp;
2636
                    if (strcmp(optarg, "-") == 0) {
2637
                        fp = stdout;
2638
                    } else {
2639
                        fp = fopen(optarg, "w");
2640
                        if (fp == NULL) {
2641
                            fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2642
                            exit(1);
2643
                        }
2644
                    }
2645
                    qemu_config_write(fp);
2646
                    fclose(fp);
2647
                    break;
2648
                }
2649
            default:
2650
                os_parse_cmd_args(popt->index, optarg);
2651
            }
2652
        }
2653
    }
2654
    loc_set_none();
2655

    
2656
    /* If no data_dir is specified then try to find it relative to the
2657
       executable path.  */
2658
    if (!data_dir) {
2659
        data_dir = os_find_datadir(argv[0]);
2660
    }
2661
    /* If all else fails use the install patch specified when building.  */
2662
    if (!data_dir) {
2663
        data_dir = CONFIG_QEMU_DATADIR;
2664
    }
2665

    
2666
#ifdef CONFIG_SIMPLE_TRACE
2667
    /*
2668
     * Set the trace file name, if specified.
2669
     */
2670
    st_set_trace_file(trace_file);
2671
#endif
2672
    /*
2673
     * Default to max_cpus = smp_cpus, in case the user doesn't
2674
     * specify a max_cpus value.
2675
     */
2676
    if (!max_cpus)
2677
        max_cpus = smp_cpus;
2678

    
2679
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2680
    if (smp_cpus > machine->max_cpus) {
2681
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2682
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
2683
                machine->max_cpus);
2684
        exit(1);
2685
    }
2686

    
2687
    qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2688
    qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2689

    
2690
    if (machine->no_serial) {
2691
        default_serial = 0;
2692
    }
2693
    if (machine->no_parallel) {
2694
        default_parallel = 0;
2695
    }
2696
    if (!machine->use_virtcon) {
2697
        default_virtcon = 0;
2698
    }
2699
    if (machine->no_vga) {
2700
        default_vga = 0;
2701
    }
2702
    if (machine->no_floppy) {
2703
        default_floppy = 0;
2704
    }
2705
    if (machine->no_cdrom) {
2706
        default_cdrom = 0;
2707
    }
2708
    if (machine->no_sdcard) {
2709
        default_sdcard = 0;
2710
    }
2711

    
2712
    if (display_type == DT_NOGRAPHIC) {
2713
        if (default_parallel)
2714
            add_device_config(DEV_PARALLEL, "null");
2715
        if (default_serial && default_monitor) {
2716
            add_device_config(DEV_SERIAL, "mon:stdio");
2717
        } else if (default_virtcon && default_monitor) {
2718
            add_device_config(DEV_VIRTCON, "mon:stdio");
2719
        } else {
2720
            if (default_serial)
2721
                add_device_config(DEV_SERIAL, "stdio");
2722
            if (default_virtcon)
2723
                add_device_config(DEV_VIRTCON, "stdio");
2724
            if (default_monitor)
2725
                monitor_parse("stdio", "readline");
2726
        }
2727
    } else {
2728
        if (default_serial)
2729
            add_device_config(DEV_SERIAL, "vc:80Cx24C");
2730
        if (default_parallel)
2731
            add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2732
        if (default_monitor)
2733
            monitor_parse("vc:80Cx24C", "readline");
2734
        if (default_virtcon)
2735
            add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2736
    }
2737
    if (default_vga)
2738
        vga_interface_type = VGA_CIRRUS;
2739

    
2740
    socket_init();
2741

    
2742
    if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2743
        exit(1);
2744
#ifdef CONFIG_VIRTFS
2745
    if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2746
        exit(1);
2747
    }
2748
#endif
2749

    
2750
    os_daemonize();
2751

    
2752
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2753
        os_pidfile_error();
2754
        exit(1);
2755
    }
2756

    
2757
    if (kvm_allowed) {
2758
        int ret = kvm_init(smp_cpus);
2759
        if (ret < 0) {
2760
            if (!kvm_available()) {
2761
                printf("KVM not supported for this target\n");
2762
            } else {
2763
                fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2764
            }
2765
            exit(1);
2766
        }
2767
    }
2768

    
2769
    if (qemu_init_main_loop()) {
2770
        fprintf(stderr, "qemu_init_main_loop failed\n");
2771
        exit(1);
2772
    }
2773
    linux_boot = (kernel_filename != NULL);
2774

    
2775
    if (!linux_boot && *kernel_cmdline != '\0') {
2776
        fprintf(stderr, "-append only allowed with -kernel option\n");
2777
        exit(1);
2778
    }
2779

    
2780
    if (!linux_boot && initrd_filename != NULL) {
2781
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
2782
        exit(1);
2783
    }
2784

    
2785
    os_set_line_buffering();
2786

    
2787
    if (init_timer_alarm() < 0) {
2788
        fprintf(stderr, "could not initialize alarm timer\n");
2789
        exit(1);
2790
    }
2791
    configure_icount(icount_option);
2792

    
2793
    if (net_init_clients() < 0) {
2794
        exit(1);
2795
    }
2796

    
2797
    /* init the bluetooth world */
2798
    if (foreach_device_config(DEV_BT, bt_parse))
2799
        exit(1);
2800

    
2801
    /* init the memory */
2802
    if (ram_size == 0)
2803
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2804

    
2805
    /* init the dynamic translator */
2806
    cpu_exec_init_all(tb_size * 1024 * 1024);
2807

    
2808
    bdrv_init_with_whitelist();
2809

    
2810
    blk_mig_init();
2811

    
2812
    if (default_cdrom) {
2813
        /* we always create the cdrom drive, even if no disk is there */
2814
        drive_add(NULL, CDROM_ALIAS);
2815
    }
2816

    
2817
    if (default_floppy) {
2818
        /* we always create at least one floppy */
2819
        drive_add(NULL, FD_ALIAS, 0);
2820
    }
2821

    
2822
    if (default_sdcard) {
2823
        /* we always create one sd slot, even if no card is in it */
2824
        drive_add(NULL, SD_ALIAS);
2825
    }
2826

    
2827
    /* open the virtual block devices */
2828
    if (snapshot)
2829
        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2830
    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2831
        exit(1);
2832

    
2833
    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2834
                         ram_load, NULL);
2835

    
2836
    if (nb_numa_nodes > 0) {
2837
        int i;
2838

    
2839
        if (nb_numa_nodes > smp_cpus) {
2840
            nb_numa_nodes = smp_cpus;
2841
        }
2842

    
2843
        /* If no memory size if given for any node, assume the default case
2844
         * and distribute the available memory equally across all nodes
2845
         */
2846
        for (i = 0; i < nb_numa_nodes; i++) {
2847
            if (node_mem[i] != 0)
2848
                break;
2849
        }
2850
        if (i == nb_numa_nodes) {
2851
            uint64_t usedmem = 0;
2852

    
2853
            /* On Linux, the each node's border has to be 8MB aligned,
2854
             * the final node gets the rest.
2855
             */
2856
            for (i = 0; i < nb_numa_nodes - 1; i++) {
2857
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2858
                usedmem += node_mem[i];
2859
            }
2860
            node_mem[i] = ram_size - usedmem;
2861
        }
2862

    
2863
        for (i = 0; i < nb_numa_nodes; i++) {
2864
            if (node_cpumask[i] != 0)
2865
                break;
2866
        }
2867
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
2868
         * must cope with this anyway, because there are BIOSes out there in
2869
         * real machines which also use this scheme.
2870
         */
2871
        if (i == nb_numa_nodes) {
2872
            for (i = 0; i < smp_cpus; i++) {
2873
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
2874
            }
2875
        }
2876
    }
2877

    
2878
    if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2879
        exit(1);
2880
    }
2881

    
2882
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2883
        exit(1);
2884
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2885
        exit(1);
2886
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2887
        exit(1);
2888
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2889
        exit(1);
2890

    
2891
    module_call_init(MODULE_INIT_DEVICE);
2892

    
2893
    if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2894
        exit(0);
2895

    
2896
    if (watchdog) {
2897
        i = select_watchdog(watchdog);
2898
        if (i > 0)
2899
            exit (i == 1 ? 1 : 0);
2900
    }
2901

    
2902
    if (machine->compat_props) {
2903
        qdev_prop_register_global_list(machine->compat_props);
2904
    }
2905
    qemu_add_globals();
2906

    
2907
    machine->init(ram_size, boot_devices,
2908
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2909

    
2910
    cpu_synchronize_all_post_init();
2911

    
2912
    /* must be after terminal init, SDL library changes signal handlers */
2913
    os_setup_signal_handling();
2914

    
2915
    set_numa_modes();
2916

    
2917
    current_machine = machine;
2918

    
2919
    /* init USB devices */
2920
    if (usb_enabled) {
2921
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
2922
            exit(1);
2923
    }
2924

    
2925
    /* init generic devices */
2926
    if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2927
        exit(1);
2928

    
2929
    net_check_clients();
2930

    
2931
    /* just use the first displaystate for the moment */
2932
    ds = get_displaystate();
2933

    
2934
    if (display_type == DT_DEFAULT && !display_remote) {
2935
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2936
        display_type = DT_SDL;
2937
#else
2938
        vnc_display = "localhost:0,to=99";
2939
        show_vnc_port = 1;
2940
#endif
2941
    }
2942
        
2943

    
2944
    /* init local displays */
2945
    switch (display_type) {
2946
    case DT_NOGRAPHIC:
2947
        break;
2948
#if defined(CONFIG_CURSES)
2949
    case DT_CURSES:
2950
        curses_display_init(ds, full_screen);
2951
        break;
2952
#endif
2953
#if defined(CONFIG_SDL)
2954
    case DT_SDL:
2955
        sdl_display_init(ds, full_screen, no_frame);
2956
        break;
2957
#elif defined(CONFIG_COCOA)
2958
    case DT_SDL:
2959
        cocoa_display_init(ds, full_screen);
2960
        break;
2961
#endif
2962
    default:
2963
        break;
2964
    }
2965

    
2966
    /* init remote displays */
2967
    if (vnc_display) {
2968
        vnc_display_init(ds);
2969
        if (vnc_display_open(ds, vnc_display) < 0)
2970
            exit(1);
2971

    
2972
        if (show_vnc_port) {
2973
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2974
        }
2975
    }
2976

    
2977
    /* display setup */
2978
    dpy_resize(ds);
2979
    dcl = ds->listeners;
2980
    while (dcl != NULL) {
2981
        if (dcl->dpy_refresh != NULL) {
2982
            ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2983
            qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2984
            break;
2985
        }
2986
        dcl = dcl->next;
2987
    }
2988
    if (ds->gui_timer == NULL) {
2989
        nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2990
        qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2991
    }
2992
    text_consoles_set_display(ds);
2993

    
2994
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2995
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2996
                gdbstub_dev);
2997
        exit(1);
2998
    }
2999

    
3000
    qdev_machine_creation_done();
3001

    
3002
    if (rom_load_all() != 0) {
3003
        fprintf(stderr, "rom loading failed\n");
3004
        exit(1);
3005
    }
3006

    
3007
    qemu_system_reset();
3008
    if (loadvm) {
3009
        if (load_vmstate(loadvm) < 0) {
3010
            autostart = 0;
3011
        }
3012
    }
3013

    
3014
    if (incoming) {
3015
        int ret = qemu_start_incoming_migration(incoming);
3016
        if (ret < 0) {
3017
            fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3018
                    incoming, ret);
3019
            exit(ret);
3020
        }
3021
    } else if (autostart) {
3022
        vm_start();
3023
    }
3024

    
3025
    os_setup_post();
3026

    
3027
    main_loop();
3028
    quit_timers();
3029
    net_cleanup();
3030

    
3031
    return 0;
3032
}