Statistics
| Branch: | Revision:

root / vl.c @ ce798cf2

History | View | Annotate | Download (94.9 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_BSD
51
#include <sys/stat.h>
52
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
53
#include <libutil.h>
54
#include <sys/sysctl.h>
55
#else
56
#include <util.h>
57
#endif
58
#else
59
#ifdef __linux__
60
#include <pty.h>
61
#include <malloc.h>
62

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

    
86
#if defined(__OpenBSD__)
87
#include <util.h>
88
#endif
89

    
90
#if defined(CONFIG_VDE)
91
#include <libvdeplug.h>
92
#endif
93

    
94
#ifdef _WIN32
95
#include <windows.h>
96
#endif
97

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

    
111
#ifdef CONFIG_COCOA
112
#undef main
113
#define main qemu_main
114
#endif /* CONFIG_COCOA */
115

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

    
154
#include "disas.h"
155

    
156
#include "qemu_socket.h"
157

    
158
#include "slirp/libslirp.h"
159

    
160
#include "qemu-queue.h"
161
#include "cpus.h"
162
#include "arch_init.h"
163

    
164
//#define DEBUG_NET
165
//#define DEBUG_SLIRP
166

    
167
#define DEFAULT_RAM_SIZE 128
168

    
169
#define MAX_VIRTIO_CONSOLES 1
170

    
171
static const char *data_dir;
172
const char *bios_name = NULL;
173
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
174
   to store the VM snapshots */
175
struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
176
struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
177
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
178
DisplayType display_type = DT_DEFAULT;
179
const char* keyboard_layout = NULL;
180
ram_addr_t ram_size;
181
const char *mem_path = NULL;
182
#ifdef MAP_POPULATE
183
int mem_prealloc = 0; /* force preallocation of physical target memory */
184
#endif
185
int nb_nics;
186
NICInfo nd_table[MAX_NICS];
187
int vm_running;
188
int autostart;
189
static int rtc_utc = 1;
190
static int rtc_date_offset = -1; /* -1 means no change */
191
QEMUClock *rtc_clock;
192
int vga_interface_type = VGA_NONE;
193
static int full_screen = 0;
194
#ifdef CONFIG_SDL
195
static int no_frame = 0;
196
#endif
197
int no_quit = 0;
198
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
199
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
200
CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
201
int win2k_install_hack = 0;
202
int rtc_td_hack = 0;
203
int usb_enabled = 0;
204
int singlestep = 0;
205
int smp_cpus = 1;
206
int max_cpus = 0;
207
int smp_cores = 1;
208
int smp_threads = 1;
209
const char *vnc_display;
210
int acpi_enabled = 1;
211
int no_hpet = 0;
212
int fd_bootchk = 1;
213
int no_reboot = 0;
214
int no_shutdown = 0;
215
int cursor_hide = 1;
216
int graphic_rotate = 0;
217
uint8_t irq0override = 1;
218
const char *watchdog;
219
const char *option_rom[MAX_OPTION_ROMS];
220
int nb_option_roms;
221
int semihosting_enabled = 0;
222
int old_param = 0;
223
const char *qemu_name;
224
int alt_grab = 0;
225
int ctrl_grab = 0;
226
unsigned int nb_prom_envs = 0;
227
const char *prom_envs[MAX_PROM_ENVS];
228
int boot_menu;
229

    
230
int nb_numa_nodes;
231
uint64_t node_mem[MAX_NODES];
232
uint64_t node_cpumask[MAX_NODES];
233

    
234
static QEMUTimer *nographic_timer;
235

    
236
uint8_t qemu_uuid[16];
237

    
238
static QEMUBootSetHandler *boot_set_handler;
239
static void *boot_set_opaque;
240

    
241
int kvm_allowed = 0;
242
uint32_t xen_domid;
243
enum xen_mode xen_mode = XEN_EMULATE;
244

    
245
static int default_serial = 1;
246
static int default_parallel = 1;
247
static int default_virtcon = 1;
248
static int default_monitor = 1;
249
static int default_vga = 1;
250
static int default_floppy = 1;
251
static int default_cdrom = 1;
252
static int default_sdcard = 1;
253

    
254
static struct {
255
    const char *driver;
256
    int *flag;
257
} default_list[] = {
258
    { .driver = "isa-serial",           .flag = &default_serial    },
259
    { .driver = "isa-parallel",         .flag = &default_parallel  },
260
    { .driver = "isa-fdc",              .flag = &default_floppy    },
261
    { .driver = "ide-drive",            .flag = &default_cdrom     },
262
    { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
263
    { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
264
    { .driver = "virtio-serial",        .flag = &default_virtcon   },
265
    { .driver = "VGA",                  .flag = &default_vga       },
266
    { .driver = "cirrus-vga",           .flag = &default_vga       },
267
    { .driver = "vmware-svga",          .flag = &default_vga       },
268
};
269

    
270
static int default_driver_check(QemuOpts *opts, void *opaque)
271
{
272
    const char *driver = qemu_opt_get(opts, "driver");
273
    int i;
274

    
275
    if (!driver)
276
        return 0;
277
    for (i = 0; i < ARRAY_SIZE(default_list); i++) {
278
        if (strcmp(default_list[i].driver, driver) != 0)
279
            continue;
280
        *(default_list[i].flag) = 0;
281
    }
282
    return 0;
283
}
284

    
285
/***********************************************************/
286
/* real time host monotonic timer */
287

    
288
/* compute with 96 bit intermediate result: (a*b)/c */
289
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
290
{
291
    union {
292
        uint64_t ll;
293
        struct {
294
#ifdef HOST_WORDS_BIGENDIAN
295
            uint32_t high, low;
296
#else
297
            uint32_t low, high;
298
#endif
299
        } l;
300
    } u, res;
301
    uint64_t rl, rh;
302

    
303
    u.ll = a;
304
    rl = (uint64_t)u.l.low * (uint64_t)b;
305
    rh = (uint64_t)u.l.high * (uint64_t)b;
306
    rh += (rl >> 32);
307
    res.l.high = rh / c;
308
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
309
    return res.ll;
310
}
311

    
312
/***********************************************************/
313
/* host time/date access */
314
void qemu_get_timedate(struct tm *tm, int offset)
315
{
316
    time_t ti;
317
    struct tm *ret;
318

    
319
    time(&ti);
320
    ti += offset;
321
    if (rtc_date_offset == -1) {
322
        if (rtc_utc)
323
            ret = gmtime(&ti);
324
        else
325
            ret = localtime(&ti);
326
    } else {
327
        ti -= rtc_date_offset;
328
        ret = gmtime(&ti);
329
    }
330

    
331
    memcpy(tm, ret, sizeof(struct tm));
332
}
333

    
334
int qemu_timedate_diff(struct tm *tm)
335
{
336
    time_t seconds;
337

    
338
    if (rtc_date_offset == -1)
339
        if (rtc_utc)
340
            seconds = mktimegm(tm);
341
        else
342
            seconds = mktime(tm);
343
    else
344
        seconds = mktimegm(tm) + rtc_date_offset;
345

    
346
    return seconds - time(NULL);
347
}
348

    
349
void rtc_change_mon_event(struct tm *tm)
350
{
351
    QObject *data;
352

    
353
    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
354
    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
355
    qobject_decref(data);
356
}
357

    
358
static void configure_rtc_date_offset(const char *startdate, int legacy)
359
{
360
    time_t rtc_start_date;
361
    struct tm tm;
362

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

    
397
static void configure_rtc(QemuOpts *opts)
398
{
399
    const char *value;
400

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

    
435
/***********************************************************/
436
/* Bluetooth support */
437
static int nb_hcis;
438
static int cur_hci;
439
static struct HCIInfo *hci_table[MAX_NICS];
440

    
441
static struct bt_vlan_s {
442
    struct bt_scatternet_s net;
443
    int id;
444
    struct bt_vlan_s *next;
445
} *first_bt_vlan;
446

    
447
/* find or alloc a new bluetooth "VLAN" */
448
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
449
{
450
    struct bt_vlan_s **pvlan, *vlan;
451
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
452
        if (vlan->id == id)
453
            return &vlan->net;
454
    }
455
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
456
    vlan->id = id;
457
    pvlan = &first_bt_vlan;
458
    while (*pvlan != NULL)
459
        pvlan = &(*pvlan)->next;
460
    *pvlan = vlan;
461
    return &vlan->net;
462
}
463

    
464
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
465
{
466
}
467

    
468
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
469
{
470
    return -ENOTSUP;
471
}
472

    
473
static struct HCIInfo null_hci = {
474
    .cmd_send = null_hci_send,
475
    .sco_send = null_hci_send,
476
    .acl_send = null_hci_send,
477
    .bdaddr_set = null_hci_addr_set,
478
};
479

    
480
struct HCIInfo *qemu_next_hci(void)
481
{
482
    if (cur_hci == nb_hcis)
483
        return &null_hci;
484

    
485
    return hci_table[cur_hci++];
486
}
487

    
488
static struct HCIInfo *hci_init(const char *str)
489
{
490
    char *endp;
491
    struct bt_scatternet_s *vlan = 0;
492

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

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

    
515
    return 0;
516
}
517

    
518
static int bt_hci_parse(const char *str)
519
{
520
    struct HCIInfo *hci;
521
    bdaddr_t bdaddr;
522

    
523
    if (nb_hcis >= MAX_NICS) {
524
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
525
        return -1;
526
    }
527

    
528
    hci = hci_init(str);
529
    if (!hci)
530
        return -1;
531

    
532
    bdaddr.b[0] = 0x52;
533
    bdaddr.b[1] = 0x54;
534
    bdaddr.b[2] = 0x00;
535
    bdaddr.b[3] = 0x12;
536
    bdaddr.b[4] = 0x34;
537
    bdaddr.b[5] = 0x56 + nb_hcis;
538
    hci->bdaddr_set(hci, bdaddr.b);
539

    
540
    hci_table[nb_hcis++] = hci;
541

    
542
    return 0;
543
}
544

    
545
static void bt_vhci_add(int vlan_id)
546
{
547
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
548

    
549
    if (!vlan->slave)
550
        fprintf(stderr, "qemu: warning: adding a VHCI to "
551
                        "an empty scatternet %i\n", vlan_id);
552

    
553
    bt_vhci_init(bt_new_hci(vlan));
554
}
555

    
556
static struct bt_device_s *bt_device_add(const char *opt)
557
{
558
    struct bt_scatternet_s *vlan;
559
    int vlan_id = 0;
560
    char *endp = strstr(opt, ",vlan=");
561
    int len = (endp ? endp - opt : strlen(opt)) + 1;
562
    char devname[10];
563

    
564
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
565

    
566
    if (endp) {
567
        vlan_id = strtol(endp + 6, &endp, 0);
568
        if (*endp) {
569
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
570
            return 0;
571
        }
572
    }
573

    
574
    vlan = qemu_find_bt_vlan(vlan_id);
575

    
576
    if (!vlan->slave)
577
        fprintf(stderr, "qemu: warning: adding a slave device to "
578
                        "an empty scatternet %i\n", vlan_id);
579

    
580
    if (!strcmp(devname, "keyboard"))
581
        return bt_keyboard_init(vlan);
582

    
583
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
584
    return 0;
585
}
586

    
587
static int bt_parse(const char *opt)
588
{
589
    const char *endp, *p;
590
    int vlan;
591

    
592
    if (strstart(opt, "hci", &endp)) {
593
        if (!*endp || *endp == ',') {
594
            if (*endp)
595
                if (!strstart(endp, ",vlan=", 0))
596
                    opt = endp + 1;
597

    
598
            return bt_hci_parse(opt);
599
       }
600
    } else if (strstart(opt, "vhci", &endp)) {
601
        if (!*endp || *endp == ',') {
602
            if (*endp) {
603
                if (strstart(endp, ",vlan=", &p)) {
604
                    vlan = strtol(p, (char **) &endp, 0);
605
                    if (*endp) {
606
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
607
                        return 1;
608
                    }
609
                } else {
610
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
611
                    return 1;
612
                }
613
            } else
614
                vlan = 0;
615

    
616
            bt_vhci_add(vlan);
617
            return 0;
618
        }
619
    } else if (strstart(opt, "device:", &endp))
620
        return !bt_device_add(endp);
621

    
622
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
623
    return 1;
624
}
625

    
626
/***********************************************************/
627
/* QEMU Block devices */
628

    
629
#define HD_ALIAS "index=%d,media=disk"
630
#define CDROM_ALIAS "index=2,media=cdrom"
631
#define FD_ALIAS "index=%d,if=floppy"
632
#define PFLASH_ALIAS "if=pflash"
633
#define MTD_ALIAS "if=mtd"
634
#define SD_ALIAS "index=0,if=sd"
635

    
636
QemuOpts *drive_add(const char *file, const char *fmt, ...)
637
{
638
    va_list ap;
639
    char optstr[1024];
640
    QemuOpts *opts;
641

    
642
    va_start(ap, fmt);
643
    vsnprintf(optstr, sizeof(optstr), fmt, ap);
644
    va_end(ap);
645

    
646
    opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
647
    if (!opts) {
648
        return NULL;
649
    }
650
    if (file)
651
        qemu_opt_set(opts, "file", file);
652
    return opts;
653
}
654

    
655
DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
656
{
657
    DriveInfo *dinfo;
658

    
659
    /* seek interface, bus and unit */
660

    
661
    QTAILQ_FOREACH(dinfo, &drives, next) {
662
        if (dinfo->type == type &&
663
            dinfo->bus == bus &&
664
            dinfo->unit == unit)
665
            return dinfo;
666
    }
667

    
668
    return NULL;
669
}
670

    
671
DriveInfo *drive_get_by_id(const char *id)
672
{
673
    DriveInfo *dinfo;
674

    
675
    QTAILQ_FOREACH(dinfo, &drives, next) {
676
        if (strcmp(id, dinfo->id))
677
            continue;
678
        return dinfo;
679
    }
680
    return NULL;
681
}
682

    
683
int drive_get_max_bus(BlockInterfaceType type)
684
{
685
    int max_bus;
686
    DriveInfo *dinfo;
687

    
688
    max_bus = -1;
689
    QTAILQ_FOREACH(dinfo, &drives, next) {
690
        if(dinfo->type == type &&
691
           dinfo->bus > max_bus)
692
            max_bus = dinfo->bus;
693
    }
694
    return max_bus;
695
}
696

    
697
const char *drive_get_serial(BlockDriverState *bdrv)
698
{
699
    DriveInfo *dinfo;
700

    
701
    QTAILQ_FOREACH(dinfo, &drives, next) {
702
        if (dinfo->bdrv == bdrv)
703
            return dinfo->serial;
704
    }
705

    
706
    return "\0";
707
}
708

    
709
BlockInterfaceErrorAction drive_get_on_error(
710
    BlockDriverState *bdrv, int is_read)
711
{
712
    DriveInfo *dinfo;
713

    
714
    QTAILQ_FOREACH(dinfo, &drives, next) {
715
        if (dinfo->bdrv == bdrv)
716
            return is_read ? dinfo->on_read_error : dinfo->on_write_error;
717
    }
718

    
719
    return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
720
}
721

    
722
static void bdrv_format_print(void *opaque, const char *name)
723
{
724
    fprintf(stderr, " %s", name);
725
}
726

    
727
void drive_uninit(DriveInfo *dinfo)
728
{
729
    qemu_opts_del(dinfo->opts);
730
    bdrv_delete(dinfo->bdrv);
731
    QTAILQ_REMOVE(&drives, dinfo, next);
732
    qemu_free(dinfo);
733
}
734

    
735
static int parse_block_error_action(const char *buf, int is_read)
736
{
737
    if (!strcmp(buf, "ignore")) {
738
        return BLOCK_ERR_IGNORE;
739
    } else if (!is_read && !strcmp(buf, "enospc")) {
740
        return BLOCK_ERR_STOP_ENOSPC;
741
    } else if (!strcmp(buf, "stop")) {
742
        return BLOCK_ERR_STOP_ANY;
743
    } else if (!strcmp(buf, "report")) {
744
        return BLOCK_ERR_REPORT;
745
    } else {
746
        fprintf(stderr, "qemu: '%s' invalid %s error action\n",
747
            buf, is_read ? "read" : "write");
748
        return -1;
749
    }
750
}
751

    
752
DriveInfo *drive_init(QemuOpts *opts, void *opaque,
753
                      int *fatal_error)
754
{
755
    const char *buf;
756
    const char *file = NULL;
757
    char devname[128];
758
    const char *serial;
759
    const char *mediastr = "";
760
    BlockInterfaceType type;
761
    enum { MEDIA_DISK, MEDIA_CDROM } media;
762
    int bus_id, unit_id;
763
    int cyls, heads, secs, translation;
764
    BlockDriver *drv = NULL;
765
    QEMUMachine *machine = opaque;
766
    int max_devs;
767
    int index;
768
    int ro = 0;
769
    int bdrv_flags = 0;
770
    int on_read_error, on_write_error;
771
    const char *devaddr;
772
    DriveInfo *dinfo;
773
    int snapshot = 0;
774

    
775
    *fatal_error = 1;
776

    
777
    translation = BIOS_ATA_TRANSLATION_AUTO;
778

    
779
    if (machine && machine->use_scsi) {
780
        type = IF_SCSI;
781
        max_devs = MAX_SCSI_DEVS;
782
        pstrcpy(devname, sizeof(devname), "scsi");
783
    } else {
784
        type = IF_IDE;
785
        max_devs = MAX_IDE_DEVS;
786
        pstrcpy(devname, sizeof(devname), "ide");
787
    }
788
    media = MEDIA_DISK;
789

    
790
    /* extract parameters */
791
    bus_id  = qemu_opt_get_number(opts, "bus", 0);
792
    unit_id = qemu_opt_get_number(opts, "unit", -1);
793
    index   = qemu_opt_get_number(opts, "index", -1);
794

    
795
    cyls  = qemu_opt_get_number(opts, "cyls", 0);
796
    heads = qemu_opt_get_number(opts, "heads", 0);
797
    secs  = qemu_opt_get_number(opts, "secs", 0);
798

    
799
    snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
800
    ro = qemu_opt_get_bool(opts, "readonly", 0);
801

    
802
    file = qemu_opt_get(opts, "file");
803
    serial = qemu_opt_get(opts, "serial");
804

    
805
    if ((buf = qemu_opt_get(opts, "if")) != NULL) {
806
        pstrcpy(devname, sizeof(devname), buf);
807
        if (!strcmp(buf, "ide")) {
808
            type = IF_IDE;
809
            max_devs = MAX_IDE_DEVS;
810
        } else if (!strcmp(buf, "scsi")) {
811
            type = IF_SCSI;
812
            max_devs = MAX_SCSI_DEVS;
813
        } else if (!strcmp(buf, "floppy")) {
814
            type = IF_FLOPPY;
815
            max_devs = 0;
816
        } else if (!strcmp(buf, "pflash")) {
817
            type = IF_PFLASH;
818
            max_devs = 0;
819
        } else if (!strcmp(buf, "mtd")) {
820
            type = IF_MTD;
821
            max_devs = 0;
822
        } else if (!strcmp(buf, "sd")) {
823
            type = IF_SD;
824
            max_devs = 0;
825
        } else if (!strcmp(buf, "virtio")) {
826
            type = IF_VIRTIO;
827
            max_devs = 0;
828
        } else if (!strcmp(buf, "xen")) {
829
            type = IF_XEN;
830
            max_devs = 0;
831
        } else if (!strcmp(buf, "none")) {
832
            type = IF_NONE;
833
            max_devs = 0;
834
        } else {
835
            fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
836
            return NULL;
837
        }
838
    }
839

    
840
    if (cyls || heads || secs) {
841
        if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
842
            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
843
            return NULL;
844
        }
845
        if (heads < 1 || (type == IF_IDE && heads > 16)) {
846
            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
847
            return NULL;
848
        }
849
        if (secs < 1 || (type == IF_IDE && secs > 63)) {
850
            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
851
            return NULL;
852
        }
853
    }
854

    
855
    if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
856
        if (!cyls) {
857
            fprintf(stderr,
858
                    "qemu: '%s' trans must be used with cyls,heads and secs\n",
859
                    buf);
860
            return NULL;
861
        }
862
        if (!strcmp(buf, "none"))
863
            translation = BIOS_ATA_TRANSLATION_NONE;
864
        else if (!strcmp(buf, "lba"))
865
            translation = BIOS_ATA_TRANSLATION_LBA;
866
        else if (!strcmp(buf, "auto"))
867
            translation = BIOS_ATA_TRANSLATION_AUTO;
868
        else {
869
            fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
870
            return NULL;
871
        }
872
    }
873

    
874
    if ((buf = qemu_opt_get(opts, "media")) != NULL) {
875
        if (!strcmp(buf, "disk")) {
876
            media = MEDIA_DISK;
877
        } else if (!strcmp(buf, "cdrom")) {
878
            if (cyls || secs || heads) {
879
                fprintf(stderr,
880
                        "qemu: '%s' invalid physical CHS format\n", buf);
881
                return NULL;
882
            }
883
            media = MEDIA_CDROM;
884
        } else {
885
            fprintf(stderr, "qemu: '%s' invalid media\n", buf);
886
            return NULL;
887
        }
888
    }
889

    
890
    if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
891
        if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
892
            bdrv_flags |= BDRV_O_NOCACHE;
893
        } else if (!strcmp(buf, "writeback")) {
894
            bdrv_flags |= BDRV_O_CACHE_WB;
895
        } else if (!strcmp(buf, "unsafe")) {
896
            bdrv_flags |= BDRV_O_CACHE_WB;
897
            bdrv_flags |= BDRV_O_NO_FLUSH;
898
        } else if (!strcmp(buf, "writethrough")) {
899
            /* this is the default */
900
        } else {
901
           fprintf(stderr, "qemu: invalid cache option\n");
902
           return NULL;
903
        }
904
    }
905

    
906
#ifdef CONFIG_LINUX_AIO
907
    if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
908
        if (!strcmp(buf, "native")) {
909
            bdrv_flags |= BDRV_O_NATIVE_AIO;
910
        } else if (!strcmp(buf, "threads")) {
911
            /* this is the default */
912
        } else {
913
           fprintf(stderr, "qemu: invalid aio option\n");
914
           return NULL;
915
        }
916
    }
917
#endif
918

    
919
    if ((buf = qemu_opt_get(opts, "format")) != NULL) {
920
       if (strcmp(buf, "?") == 0) {
921
            fprintf(stderr, "qemu: Supported formats:");
922
            bdrv_iterate_format(bdrv_format_print, NULL);
923
            fprintf(stderr, "\n");
924
            return NULL;
925
        }
926
        drv = bdrv_find_whitelisted_format(buf);
927
        if (!drv) {
928
            fprintf(stderr, "qemu: '%s' invalid format\n", buf);
929
            return NULL;
930
        }
931
    }
932

    
933
    on_write_error = BLOCK_ERR_STOP_ENOSPC;
934
    if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
935
        if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
936
            fprintf(stderr, "werror is no supported by this format\n");
937
            return NULL;
938
        }
939

    
940
        on_write_error = parse_block_error_action(buf, 0);
941
        if (on_write_error < 0) {
942
            return NULL;
943
        }
944
    }
945

    
946
    on_read_error = BLOCK_ERR_REPORT;
947
    if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
948
        if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
949
            fprintf(stderr, "rerror is no supported by this format\n");
950
            return NULL;
951
        }
952

    
953
        on_read_error = parse_block_error_action(buf, 1);
954
        if (on_read_error < 0) {
955
            return NULL;
956
        }
957
    }
958

    
959
    if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
960
        if (type != IF_VIRTIO) {
961
            fprintf(stderr, "addr is not supported\n");
962
            return NULL;
963
        }
964
    }
965

    
966
    /* compute bus and unit according index */
967

    
968
    if (index != -1) {
969
        if (bus_id != 0 || unit_id != -1) {
970
            fprintf(stderr,
971
                    "qemu: index cannot be used with bus and unit\n");
972
            return NULL;
973
        }
974
        if (max_devs == 0)
975
        {
976
            unit_id = index;
977
            bus_id = 0;
978
        } else {
979
            unit_id = index % max_devs;
980
            bus_id = index / max_devs;
981
        }
982
    }
983

    
984
    /* if user doesn't specify a unit_id,
985
     * try to find the first free
986
     */
987

    
988
    if (unit_id == -1) {
989
       unit_id = 0;
990
       while (drive_get(type, bus_id, unit_id) != NULL) {
991
           unit_id++;
992
           if (max_devs && unit_id >= max_devs) {
993
               unit_id -= max_devs;
994
               bus_id++;
995
           }
996
       }
997
    }
998

    
999
    /* check unit id */
1000

    
1001
    if (max_devs && unit_id >= max_devs) {
1002
        fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1003
                unit_id, max_devs - 1);
1004
        return NULL;
1005
    }
1006

    
1007
    /*
1008
     * ignore multiple definitions
1009
     */
1010

    
1011
    if (drive_get(type, bus_id, unit_id) != NULL) {
1012
        *fatal_error = 0;
1013
        return NULL;
1014
    }
1015

    
1016
    /* init */
1017

    
1018
    dinfo = qemu_mallocz(sizeof(*dinfo));
1019
    if ((buf = qemu_opts_id(opts)) != NULL) {
1020
        dinfo->id = qemu_strdup(buf);
1021
    } else {
1022
        /* no id supplied -> create one */
1023
        dinfo->id = qemu_mallocz(32);
1024
        if (type == IF_IDE || type == IF_SCSI)
1025
            mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1026
        if (max_devs)
1027
            snprintf(dinfo->id, 32, "%s%i%s%i",
1028
                     devname, bus_id, mediastr, unit_id);
1029
        else
1030
            snprintf(dinfo->id, 32, "%s%s%i",
1031
                     devname, mediastr, unit_id);
1032
    }
1033
    dinfo->bdrv = bdrv_new(dinfo->id);
1034
    dinfo->devaddr = devaddr;
1035
    dinfo->type = type;
1036
    dinfo->bus = bus_id;
1037
    dinfo->unit = unit_id;
1038
    dinfo->on_read_error = on_read_error;
1039
    dinfo->on_write_error = on_write_error;
1040
    dinfo->opts = opts;
1041
    if (serial)
1042
        strncpy(dinfo->serial, serial, sizeof(serial));
1043
    QTAILQ_INSERT_TAIL(&drives, dinfo, next);
1044

    
1045
    switch(type) {
1046
    case IF_IDE:
1047
    case IF_SCSI:
1048
    case IF_XEN:
1049
    case IF_NONE:
1050
        switch(media) {
1051
        case MEDIA_DISK:
1052
            if (cyls != 0) {
1053
                bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1054
                bdrv_set_translation_hint(dinfo->bdrv, translation);
1055
            }
1056
            break;
1057
        case MEDIA_CDROM:
1058
            bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
1059
            break;
1060
        }
1061
        break;
1062
    case IF_SD:
1063
        /* FIXME: This isn't really a floppy, but it's a reasonable
1064
           approximation.  */
1065
    case IF_FLOPPY:
1066
        bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
1067
        break;
1068
    case IF_PFLASH:
1069
    case IF_MTD:
1070
        break;
1071
    case IF_VIRTIO:
1072
        /* add virtio block device */
1073
        opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1074
        qemu_opt_set(opts, "driver", "virtio-blk-pci");
1075
        qemu_opt_set(opts, "drive", dinfo->id);
1076
        if (devaddr)
1077
            qemu_opt_set(opts, "addr", devaddr);
1078
        break;
1079
    case IF_COUNT:
1080
        abort();
1081
    }
1082
    if (!file) {
1083
        *fatal_error = 0;
1084
        return NULL;
1085
    }
1086
    if (snapshot) {
1087
        /* always use cache=unsafe with snapshot */
1088
        bdrv_flags &= ~BDRV_O_CACHE_MASK;
1089
        bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
1090
    }
1091

    
1092
    if (media == MEDIA_CDROM) {
1093
        /* CDROM is fine for any interface, don't check.  */
1094
        ro = 1;
1095
    } else if (ro == 1) {
1096
        if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
1097
            fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
1098
            return NULL;
1099
        }
1100
    }
1101

    
1102
    bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
1103

    
1104
    if (bdrv_open(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
1105
        fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1106
                        file, strerror(errno));
1107
        return NULL;
1108
    }
1109

    
1110
    if (bdrv_key_required(dinfo->bdrv))
1111
        autostart = 0;
1112
    *fatal_error = 0;
1113
    return dinfo;
1114
}
1115

    
1116
static int drive_init_func(QemuOpts *opts, void *opaque)
1117
{
1118
    QEMUMachine *machine = opaque;
1119
    int fatal_error = 0;
1120

    
1121
    if (drive_init(opts, machine, &fatal_error) == NULL) {
1122
        if (fatal_error)
1123
            return 1;
1124
    }
1125
    return 0;
1126
}
1127

    
1128
static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1129
{
1130
    if (NULL == qemu_opt_get(opts, "snapshot")) {
1131
        qemu_opt_set(opts, "snapshot", "on");
1132
    }
1133
    return 0;
1134
}
1135

    
1136
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1137
{
1138
    boot_set_handler = func;
1139
    boot_set_opaque = opaque;
1140
}
1141

    
1142
int qemu_boot_set(const char *boot_devices)
1143
{
1144
    if (!boot_set_handler) {
1145
        return -EINVAL;
1146
    }
1147
    return boot_set_handler(boot_set_opaque, boot_devices);
1148
}
1149

    
1150
static void validate_bootdevices(char *devices)
1151
{
1152
    /* We just do some generic consistency checks */
1153
    const char *p;
1154
    int bitmap = 0;
1155

    
1156
    for (p = devices; *p != '\0'; p++) {
1157
        /* Allowed boot devices are:
1158
         * a-b: floppy disk drives
1159
         * c-f: IDE disk drives
1160
         * g-m: machine implementation dependant drives
1161
         * n-p: network devices
1162
         * It's up to each machine implementation to check if the given boot
1163
         * devices match the actual hardware implementation and firmware
1164
         * features.
1165
         */
1166
        if (*p < 'a' || *p > 'p') {
1167
            fprintf(stderr, "Invalid boot device '%c'\n", *p);
1168
            exit(1);
1169
        }
1170
        if (bitmap & (1 << (*p - 'a'))) {
1171
            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1172
            exit(1);
1173
        }
1174
        bitmap |= 1 << (*p - 'a');
1175
    }
1176
}
1177

    
1178
static void restore_boot_devices(void *opaque)
1179
{
1180
    char *standard_boot_devices = opaque;
1181
    static int first = 1;
1182

    
1183
    /* Restore boot order and remove ourselves after the first boot */
1184
    if (first) {
1185
        first = 0;
1186
        return;
1187
    }
1188

    
1189
    qemu_boot_set(standard_boot_devices);
1190

    
1191
    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1192
    qemu_free(standard_boot_devices);
1193
}
1194

    
1195
static void numa_add(const char *optarg)
1196
{
1197
    char option[128];
1198
    char *endptr;
1199
    unsigned long long value, endvalue;
1200
    int nodenr;
1201

    
1202
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
1203
    if (!strcmp(option, "node")) {
1204
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1205
            nodenr = nb_numa_nodes;
1206
        } else {
1207
            nodenr = strtoull(option, NULL, 10);
1208
        }
1209

    
1210
        if (get_param_value(option, 128, "mem", optarg) == 0) {
1211
            node_mem[nodenr] = 0;
1212
        } else {
1213
            value = strtoull(option, &endptr, 0);
1214
            switch (*endptr) {
1215
            case 0: case 'M': case 'm':
1216
                value <<= 20;
1217
                break;
1218
            case 'G': case 'g':
1219
                value <<= 30;
1220
                break;
1221
            }
1222
            node_mem[nodenr] = value;
1223
        }
1224
        if (get_param_value(option, 128, "cpus", optarg) == 0) {
1225
            node_cpumask[nodenr] = 0;
1226
        } else {
1227
            value = strtoull(option, &endptr, 10);
1228
            if (value >= 64) {
1229
                value = 63;
1230
                fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1231
            } else {
1232
                if (*endptr == '-') {
1233
                    endvalue = strtoull(endptr+1, &endptr, 10);
1234
                    if (endvalue >= 63) {
1235
                        endvalue = 62;
1236
                        fprintf(stderr,
1237
                            "only 63 CPUs in NUMA mode supported.\n");
1238
                    }
1239
                    value = (2ULL << endvalue) - (1ULL << value);
1240
                } else {
1241
                    value = 1ULL << value;
1242
                }
1243
            }
1244
            node_cpumask[nodenr] = value;
1245
        }
1246
        nb_numa_nodes++;
1247
    }
1248
    return;
1249
}
1250

    
1251
static void smp_parse(const char *optarg)
1252
{
1253
    int smp, sockets = 0, threads = 0, cores = 0;
1254
    char *endptr;
1255
    char option[128];
1256

    
1257
    smp = strtoul(optarg, &endptr, 10);
1258
    if (endptr != optarg) {
1259
        if (*endptr == ',') {
1260
            endptr++;
1261
        }
1262
    }
1263
    if (get_param_value(option, 128, "sockets", endptr) != 0)
1264
        sockets = strtoull(option, NULL, 10);
1265
    if (get_param_value(option, 128, "cores", endptr) != 0)
1266
        cores = strtoull(option, NULL, 10);
1267
    if (get_param_value(option, 128, "threads", endptr) != 0)
1268
        threads = strtoull(option, NULL, 10);
1269
    if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1270
        max_cpus = strtoull(option, NULL, 10);
1271

    
1272
    /* compute missing values, prefer sockets over cores over threads */
1273
    if (smp == 0 || sockets == 0) {
1274
        sockets = sockets > 0 ? sockets : 1;
1275
        cores = cores > 0 ? cores : 1;
1276
        threads = threads > 0 ? threads : 1;
1277
        if (smp == 0) {
1278
            smp = cores * threads * sockets;
1279
        }
1280
    } else {
1281
        if (cores == 0) {
1282
            threads = threads > 0 ? threads : 1;
1283
            cores = smp / (sockets * threads);
1284
        } else {
1285
            if (sockets) {
1286
                threads = smp / (cores * sockets);
1287
            }
1288
        }
1289
    }
1290
    smp_cpus = smp;
1291
    smp_cores = cores > 0 ? cores : 1;
1292
    smp_threads = threads > 0 ? threads : 1;
1293
    if (max_cpus == 0)
1294
        max_cpus = smp_cpus;
1295
}
1296

    
1297
/***********************************************************/
1298
/* USB devices */
1299

    
1300
static int usb_device_add(const char *devname, int is_hotplug)
1301
{
1302
    const char *p;
1303
    USBDevice *dev = NULL;
1304

    
1305
    if (!usb_enabled)
1306
        return -1;
1307

    
1308
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
1309
    dev = usbdevice_create(devname);
1310
    if (dev)
1311
        goto done;
1312

    
1313
    /* the other ones */
1314
    if (strstart(devname, "host:", &p)) {
1315
        dev = usb_host_device_open(p);
1316
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1317
        dev = usb_bt_init(devname[2] ? hci_init(p) :
1318
                        bt_new_hci(qemu_find_bt_vlan(0)));
1319
    } else {
1320
        return -1;
1321
    }
1322
    if (!dev)
1323
        return -1;
1324

    
1325
done:
1326
    return 0;
1327
}
1328

    
1329
static int usb_device_del(const char *devname)
1330
{
1331
    int bus_num, addr;
1332
    const char *p;
1333

    
1334
    if (strstart(devname, "host:", &p))
1335
        return usb_host_device_close(p);
1336

    
1337
    if (!usb_enabled)
1338
        return -1;
1339

    
1340
    p = strchr(devname, '.');
1341
    if (!p)
1342
        return -1;
1343
    bus_num = strtoul(devname, NULL, 0);
1344
    addr = strtoul(p + 1, NULL, 0);
1345

    
1346
    return usb_device_delete_addr(bus_num, addr);
1347
}
1348

    
1349
static int usb_parse(const char *cmdline)
1350
{
1351
    int r;
1352
    r = usb_device_add(cmdline, 0);
1353
    if (r < 0) {
1354
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1355
    }
1356
    return r;
1357
}
1358

    
1359
void do_usb_add(Monitor *mon, const QDict *qdict)
1360
{
1361
    const char *devname = qdict_get_str(qdict, "devname");
1362
    if (usb_device_add(devname, 1) < 0) {
1363
        error_report("could not add USB device '%s'", devname);
1364
    }
1365
}
1366

    
1367
void do_usb_del(Monitor *mon, const QDict *qdict)
1368
{
1369
    const char *devname = qdict_get_str(qdict, "devname");
1370
    if (usb_device_del(devname) < 0) {
1371
        error_report("could not delete USB device '%s'", devname);
1372
    }
1373
}
1374

    
1375
/***********************************************************/
1376
/* PCMCIA/Cardbus */
1377

    
1378
static struct pcmcia_socket_entry_s {
1379
    PCMCIASocket *socket;
1380
    struct pcmcia_socket_entry_s *next;
1381
} *pcmcia_sockets = 0;
1382

    
1383
void pcmcia_socket_register(PCMCIASocket *socket)
1384
{
1385
    struct pcmcia_socket_entry_s *entry;
1386

    
1387
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1388
    entry->socket = socket;
1389
    entry->next = pcmcia_sockets;
1390
    pcmcia_sockets = entry;
1391
}
1392

    
1393
void pcmcia_socket_unregister(PCMCIASocket *socket)
1394
{
1395
    struct pcmcia_socket_entry_s *entry, **ptr;
1396

    
1397
    ptr = &pcmcia_sockets;
1398
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1399
        if (entry->socket == socket) {
1400
            *ptr = entry->next;
1401
            qemu_free(entry);
1402
        }
1403
}
1404

    
1405
void pcmcia_info(Monitor *mon)
1406
{
1407
    struct pcmcia_socket_entry_s *iter;
1408

    
1409
    if (!pcmcia_sockets)
1410
        monitor_printf(mon, "No PCMCIA sockets\n");
1411

    
1412
    for (iter = pcmcia_sockets; iter; iter = iter->next)
1413
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1414
                       iter->socket->attached ? iter->socket->card_string :
1415
                       "Empty");
1416
}
1417

    
1418
/***********************************************************/
1419
/* I/O handling */
1420

    
1421
typedef struct IOHandlerRecord {
1422
    int fd;
1423
    IOCanReadHandler *fd_read_poll;
1424
    IOHandler *fd_read;
1425
    IOHandler *fd_write;
1426
    int deleted;
1427
    void *opaque;
1428
    /* temporary data */
1429
    struct pollfd *ufd;
1430
    QLIST_ENTRY(IOHandlerRecord) next;
1431
} IOHandlerRecord;
1432

    
1433
static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1434
    QLIST_HEAD_INITIALIZER(io_handlers);
1435

    
1436

    
1437
/* XXX: fd_read_poll should be suppressed, but an API change is
1438
   necessary in the character devices to suppress fd_can_read(). */
1439
int qemu_set_fd_handler2(int fd,
1440
                         IOCanReadHandler *fd_read_poll,
1441
                         IOHandler *fd_read,
1442
                         IOHandler *fd_write,
1443
                         void *opaque)
1444
{
1445
    IOHandlerRecord *ioh;
1446

    
1447
    if (!fd_read && !fd_write) {
1448
        QLIST_FOREACH(ioh, &io_handlers, next) {
1449
            if (ioh->fd == fd) {
1450
                ioh->deleted = 1;
1451
                break;
1452
            }
1453
        }
1454
    } else {
1455
        QLIST_FOREACH(ioh, &io_handlers, next) {
1456
            if (ioh->fd == fd)
1457
                goto found;
1458
        }
1459
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1460
        QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1461
    found:
1462
        ioh->fd = fd;
1463
        ioh->fd_read_poll = fd_read_poll;
1464
        ioh->fd_read = fd_read;
1465
        ioh->fd_write = fd_write;
1466
        ioh->opaque = opaque;
1467
        ioh->deleted = 0;
1468
    }
1469
    return 0;
1470
}
1471

    
1472
int qemu_set_fd_handler(int fd,
1473
                        IOHandler *fd_read,
1474
                        IOHandler *fd_write,
1475
                        void *opaque)
1476
{
1477
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1478
}
1479

    
1480
/***********************************************************/
1481
/* machine registration */
1482

    
1483
static QEMUMachine *first_machine = NULL;
1484
QEMUMachine *current_machine = NULL;
1485

    
1486
int qemu_register_machine(QEMUMachine *m)
1487
{
1488
    QEMUMachine **pm;
1489
    pm = &first_machine;
1490
    while (*pm != NULL)
1491
        pm = &(*pm)->next;
1492
    m->next = NULL;
1493
    *pm = m;
1494
    return 0;
1495
}
1496

    
1497
static QEMUMachine *find_machine(const char *name)
1498
{
1499
    QEMUMachine *m;
1500

    
1501
    for(m = first_machine; m != NULL; m = m->next) {
1502
        if (!strcmp(m->name, name))
1503
            return m;
1504
        if (m->alias && !strcmp(m->alias, name))
1505
            return m;
1506
    }
1507
    return NULL;
1508
}
1509

    
1510
static QEMUMachine *find_default_machine(void)
1511
{
1512
    QEMUMachine *m;
1513

    
1514
    for(m = first_machine; m != NULL; m = m->next) {
1515
        if (m->is_default) {
1516
            return m;
1517
        }
1518
    }
1519
    return NULL;
1520
}
1521

    
1522
/***********************************************************/
1523
/* main execution loop */
1524

    
1525
static void gui_update(void *opaque)
1526
{
1527
    uint64_t interval = GUI_REFRESH_INTERVAL;
1528
    DisplayState *ds = opaque;
1529
    DisplayChangeListener *dcl = ds->listeners;
1530

    
1531
    qemu_flush_coalesced_mmio_buffer();
1532
    dpy_refresh(ds);
1533

    
1534
    while (dcl != NULL) {
1535
        if (dcl->gui_timer_interval &&
1536
            dcl->gui_timer_interval < interval)
1537
            interval = dcl->gui_timer_interval;
1538
        dcl = dcl->next;
1539
    }
1540
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1541
}
1542

    
1543
static void nographic_update(void *opaque)
1544
{
1545
    uint64_t interval = GUI_REFRESH_INTERVAL;
1546

    
1547
    qemu_flush_coalesced_mmio_buffer();
1548
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1549
}
1550

    
1551
struct vm_change_state_entry {
1552
    VMChangeStateHandler *cb;
1553
    void *opaque;
1554
    QLIST_ENTRY (vm_change_state_entry) entries;
1555
};
1556

    
1557
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1558

    
1559
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1560
                                                     void *opaque)
1561
{
1562
    VMChangeStateEntry *e;
1563

    
1564
    e = qemu_mallocz(sizeof (*e));
1565

    
1566
    e->cb = cb;
1567
    e->opaque = opaque;
1568
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1569
    return e;
1570
}
1571

    
1572
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1573
{
1574
    QLIST_REMOVE (e, entries);
1575
    qemu_free (e);
1576
}
1577

    
1578
void vm_state_notify(int running, int reason)
1579
{
1580
    VMChangeStateEntry *e;
1581

    
1582
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1583
        e->cb(e->opaque, running, reason);
1584
    }
1585
}
1586

    
1587
void vm_start(void)
1588
{
1589
    if (!vm_running) {
1590
        cpu_enable_ticks();
1591
        vm_running = 1;
1592
        vm_state_notify(1, 0);
1593
        resume_all_vcpus();
1594
        monitor_protocol_event(QEVENT_RESUME, NULL);
1595
    }
1596
}
1597

    
1598
/* reset/shutdown handler */
1599

    
1600
typedef struct QEMUResetEntry {
1601
    QTAILQ_ENTRY(QEMUResetEntry) entry;
1602
    QEMUResetHandler *func;
1603
    void *opaque;
1604
} QEMUResetEntry;
1605

    
1606
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1607
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
1608
static int reset_requested;
1609
static int shutdown_requested;
1610
static int powerdown_requested;
1611
int debug_requested;
1612
int vmstop_requested;
1613

    
1614
int qemu_shutdown_requested(void)
1615
{
1616
    int r = shutdown_requested;
1617
    shutdown_requested = 0;
1618
    return r;
1619
}
1620

    
1621
int qemu_reset_requested(void)
1622
{
1623
    int r = reset_requested;
1624
    reset_requested = 0;
1625
    return r;
1626
}
1627

    
1628
int qemu_powerdown_requested(void)
1629
{
1630
    int r = powerdown_requested;
1631
    powerdown_requested = 0;
1632
    return r;
1633
}
1634

    
1635
static int qemu_debug_requested(void)
1636
{
1637
    int r = debug_requested;
1638
    debug_requested = 0;
1639
    return r;
1640
}
1641

    
1642
static int qemu_vmstop_requested(void)
1643
{
1644
    int r = vmstop_requested;
1645
    vmstop_requested = 0;
1646
    return r;
1647
}
1648

    
1649
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1650
{
1651
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1652

    
1653
    re->func = func;
1654
    re->opaque = opaque;
1655
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1656
}
1657

    
1658
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1659
{
1660
    QEMUResetEntry *re;
1661

    
1662
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
1663
        if (re->func == func && re->opaque == opaque) {
1664
            QTAILQ_REMOVE(&reset_handlers, re, entry);
1665
            qemu_free(re);
1666
            return;
1667
        }
1668
    }
1669
}
1670

    
1671
void qemu_system_reset(void)
1672
{
1673
    QEMUResetEntry *re, *nre;
1674

    
1675
    /* reset all devices */
1676
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1677
        re->func(re->opaque);
1678
    }
1679
    monitor_protocol_event(QEVENT_RESET, NULL);
1680
    cpu_synchronize_all_post_reset();
1681
}
1682

    
1683
void qemu_system_reset_request(void)
1684
{
1685
    if (no_reboot) {
1686
        shutdown_requested = 1;
1687
    } else {
1688
        reset_requested = 1;
1689
    }
1690
    qemu_notify_event();
1691
}
1692

    
1693
void qemu_system_shutdown_request(void)
1694
{
1695
    shutdown_requested = 1;
1696
    qemu_notify_event();
1697
}
1698

    
1699
void qemu_system_powerdown_request(void)
1700
{
1701
    powerdown_requested = 1;
1702
    qemu_notify_event();
1703
}
1704

    
1705
void main_loop_wait(int nonblocking)
1706
{
1707
    IOHandlerRecord *ioh;
1708
    fd_set rfds, wfds, xfds;
1709
    int ret, nfds;
1710
    struct timeval tv;
1711
    int timeout;
1712

    
1713
    if (nonblocking)
1714
        timeout = 0;
1715
    else {
1716
        timeout = qemu_calculate_timeout();
1717
        qemu_bh_update_timeout(&timeout);
1718
    }
1719

    
1720
    os_host_main_loop_wait(&timeout);
1721

    
1722
    /* poll any events */
1723
    /* XXX: separate device handlers from system ones */
1724
    nfds = -1;
1725
    FD_ZERO(&rfds);
1726
    FD_ZERO(&wfds);
1727
    FD_ZERO(&xfds);
1728
    QLIST_FOREACH(ioh, &io_handlers, next) {
1729
        if (ioh->deleted)
1730
            continue;
1731
        if (ioh->fd_read &&
1732
            (!ioh->fd_read_poll ||
1733
             ioh->fd_read_poll(ioh->opaque) != 0)) {
1734
            FD_SET(ioh->fd, &rfds);
1735
            if (ioh->fd > nfds)
1736
                nfds = ioh->fd;
1737
        }
1738
        if (ioh->fd_write) {
1739
            FD_SET(ioh->fd, &wfds);
1740
            if (ioh->fd > nfds)
1741
                nfds = ioh->fd;
1742
        }
1743
    }
1744

    
1745
    tv.tv_sec = timeout / 1000;
1746
    tv.tv_usec = (timeout % 1000) * 1000;
1747

    
1748
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1749

    
1750
    qemu_mutex_unlock_iothread();
1751
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1752
    qemu_mutex_lock_iothread();
1753
    if (ret > 0) {
1754
        IOHandlerRecord *pioh;
1755

    
1756
        QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1757
            if (ioh->deleted) {
1758
                QLIST_REMOVE(ioh, next);
1759
                qemu_free(ioh);
1760
                continue;
1761
            }
1762
            if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1763
                ioh->fd_read(ioh->opaque);
1764
            }
1765
            if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1766
                ioh->fd_write(ioh->opaque);
1767
            }
1768
        }
1769
    }
1770

    
1771
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1772

    
1773
    qemu_run_all_timers();
1774

    
1775
    /* Check bottom-halves last in case any of the earlier events triggered
1776
       them.  */
1777
    qemu_bh_poll();
1778

    
1779
}
1780

    
1781
static int vm_can_run(void)
1782
{
1783
    if (powerdown_requested)
1784
        return 0;
1785
    if (reset_requested)
1786
        return 0;
1787
    if (shutdown_requested)
1788
        return 0;
1789
    if (debug_requested)
1790
        return 0;
1791
    return 1;
1792
}
1793

    
1794
qemu_irq qemu_system_powerdown;
1795

    
1796
static void main_loop(void)
1797
{
1798
    int r;
1799

    
1800
    qemu_main_loop_start();
1801

    
1802
    for (;;) {
1803
        do {
1804
            bool nonblocking = false;
1805
#ifdef CONFIG_PROFILER
1806
            int64_t ti;
1807
#endif
1808
#ifndef CONFIG_IOTHREAD
1809
            nonblocking = tcg_cpu_exec();
1810
#endif
1811
#ifdef CONFIG_PROFILER
1812
            ti = profile_getclock();
1813
#endif
1814
            main_loop_wait(nonblocking);
1815
#ifdef CONFIG_PROFILER
1816
            dev_time += profile_getclock() - ti;
1817
#endif
1818
        } while (vm_can_run());
1819

    
1820
        if ((r = qemu_debug_requested())) {
1821
            vm_stop(r);
1822
        }
1823
        if (qemu_shutdown_requested()) {
1824
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1825
            if (no_shutdown) {
1826
                vm_stop(0);
1827
                no_shutdown = 0;
1828
            } else
1829
                break;
1830
        }
1831
        if (qemu_reset_requested()) {
1832
            pause_all_vcpus();
1833
            qemu_system_reset();
1834
            resume_all_vcpus();
1835
        }
1836
        if (qemu_powerdown_requested()) {
1837
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1838
            qemu_irq_raise(qemu_system_powerdown);
1839
        }
1840
        if ((r = qemu_vmstop_requested())) {
1841
            vm_stop(r);
1842
        }
1843
    }
1844
    pause_all_vcpus();
1845
}
1846

    
1847
static void version(void)
1848
{
1849
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1850
}
1851

    
1852
static void help(int exitcode)
1853
{
1854
    const char *options_help =
1855
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1856
        opt_help
1857
#define DEFHEADING(text) stringify(text) "\n"
1858
#include "qemu-options.def"
1859
#undef DEF
1860
#undef DEFHEADING
1861
#undef GEN_DOCS
1862
        ;
1863
    version();
1864
    printf("usage: %s [options] [disk_image]\n"
1865
           "\n"
1866
           "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1867
           "\n"
1868
           "%s\n"
1869
           "During emulation, the following keys are useful:\n"
1870
           "ctrl-alt-f      toggle full screen\n"
1871
           "ctrl-alt-n      switch to virtual console 'n'\n"
1872
           "ctrl-alt        toggle mouse and keyboard grab\n"
1873
           "\n"
1874
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
1875
           "qemu",
1876
           options_help);
1877
    exit(exitcode);
1878
}
1879

    
1880
#define HAS_ARG 0x0001
1881

    
1882
typedef struct QEMUOption {
1883
    const char *name;
1884
    int flags;
1885
    int index;
1886
    uint32_t arch_mask;
1887
} QEMUOption;
1888

    
1889
static const QEMUOption qemu_options[] = {
1890
    { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1891
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1892
    { option, opt_arg, opt_enum, arch_mask },
1893
#define DEFHEADING(text)
1894
#include "qemu-options.def"
1895
#undef DEF
1896
#undef DEFHEADING
1897
#undef GEN_DOCS
1898
    { NULL },
1899
};
1900
static void select_vgahw (const char *p)
1901
{
1902
    const char *opts;
1903

    
1904
    default_vga = 0;
1905
    vga_interface_type = VGA_NONE;
1906
    if (strstart(p, "std", &opts)) {
1907
        vga_interface_type = VGA_STD;
1908
    } else if (strstart(p, "cirrus", &opts)) {
1909
        vga_interface_type = VGA_CIRRUS;
1910
    } else if (strstart(p, "vmware", &opts)) {
1911
        vga_interface_type = VGA_VMWARE;
1912
    } else if (strstart(p, "xenfb", &opts)) {
1913
        vga_interface_type = VGA_XENFB;
1914
    } else if (!strstart(p, "none", &opts)) {
1915
    invalid_vga:
1916
        fprintf(stderr, "Unknown vga type: %s\n", p);
1917
        exit(1);
1918
    }
1919
    while (*opts) {
1920
        const char *nextopt;
1921

    
1922
        if (strstart(opts, ",retrace=", &nextopt)) {
1923
            opts = nextopt;
1924
            if (strstart(opts, "dumb", &nextopt))
1925
                vga_retrace_method = VGA_RETRACE_DUMB;
1926
            else if (strstart(opts, "precise", &nextopt))
1927
                vga_retrace_method = VGA_RETRACE_PRECISE;
1928
            else goto invalid_vga;
1929
        } else goto invalid_vga;
1930
        opts = nextopt;
1931
    }
1932
}
1933

    
1934
static int balloon_parse(const char *arg)
1935
{
1936
    QemuOpts *opts;
1937

    
1938
    if (strcmp(arg, "none") == 0) {
1939
        return 0;
1940
    }
1941

    
1942
    if (!strncmp(arg, "virtio", 6)) {
1943
        if (arg[6] == ',') {
1944
            /* have params -> parse them */
1945
            opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
1946
            if (!opts)
1947
                return  -1;
1948
        } else {
1949
            /* create empty opts */
1950
            opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1951
        }
1952
        qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1953
        return 0;
1954
    }
1955

    
1956
    return -1;
1957
}
1958

    
1959
char *qemu_find_file(int type, const char *name)
1960
{
1961
    int len;
1962
    const char *subdir;
1963
    char *buf;
1964

    
1965
    /* If name contains path separators then try it as a straight path.  */
1966
    if ((strchr(name, '/') || strchr(name, '\\'))
1967
        && access(name, R_OK) == 0) {
1968
        return qemu_strdup(name);
1969
    }
1970
    switch (type) {
1971
    case QEMU_FILE_TYPE_BIOS:
1972
        subdir = "";
1973
        break;
1974
    case QEMU_FILE_TYPE_KEYMAP:
1975
        subdir = "keymaps/";
1976
        break;
1977
    default:
1978
        abort();
1979
    }
1980
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1981
    buf = qemu_mallocz(len);
1982
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1983
    if (access(buf, R_OK)) {
1984
        qemu_free(buf);
1985
        return NULL;
1986
    }
1987
    return buf;
1988
}
1989

    
1990
static int device_help_func(QemuOpts *opts, void *opaque)
1991
{
1992
    return qdev_device_help(opts);
1993
}
1994

    
1995
static int device_init_func(QemuOpts *opts, void *opaque)
1996
{
1997
    DeviceState *dev;
1998

    
1999
    dev = qdev_device_add(opts);
2000
    if (!dev)
2001
        return -1;
2002
    return 0;
2003
}
2004

    
2005
static int chardev_init_func(QemuOpts *opts, void *opaque)
2006
{
2007
    CharDriverState *chr;
2008

    
2009
    chr = qemu_chr_open_opts(opts, NULL);
2010
    if (!chr)
2011
        return -1;
2012
    return 0;
2013
}
2014

    
2015
#ifdef CONFIG_LINUX
2016
static int fsdev_init_func(QemuOpts *opts, void *opaque)
2017
{
2018
    int ret;
2019
    ret = qemu_fsdev_add(opts);
2020

    
2021
    return ret;
2022
}
2023
#endif
2024

    
2025
static int mon_init_func(QemuOpts *opts, void *opaque)
2026
{
2027
    CharDriverState *chr;
2028
    const char *chardev;
2029
    const char *mode;
2030
    int flags;
2031

    
2032
    mode = qemu_opt_get(opts, "mode");
2033
    if (mode == NULL) {
2034
        mode = "readline";
2035
    }
2036
    if (strcmp(mode, "readline") == 0) {
2037
        flags = MONITOR_USE_READLINE;
2038
    } else if (strcmp(mode, "control") == 0) {
2039
        flags = MONITOR_USE_CONTROL;
2040
    } else {
2041
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2042
        exit(1);
2043
    }
2044

    
2045
    if (qemu_opt_get_bool(opts, "default", 0))
2046
        flags |= MONITOR_IS_DEFAULT;
2047

    
2048
    chardev = qemu_opt_get(opts, "chardev");
2049
    chr = qemu_chr_find(chardev);
2050
    if (chr == NULL) {
2051
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2052
        exit(1);
2053
    }
2054

    
2055
    monitor_init(chr, flags);
2056
    return 0;
2057
}
2058

    
2059
static void monitor_parse(const char *optarg, const char *mode)
2060
{
2061
    static int monitor_device_index = 0;
2062
    QemuOpts *opts;
2063
    const char *p;
2064
    char label[32];
2065
    int def = 0;
2066

    
2067
    if (strstart(optarg, "chardev:", &p)) {
2068
        snprintf(label, sizeof(label), "%s", p);
2069
    } else {
2070
        snprintf(label, sizeof(label), "compat_monitor%d",
2071
                 monitor_device_index);
2072
        if (monitor_device_index == 0) {
2073
            def = 1;
2074
        }
2075
        opts = qemu_chr_parse_compat(label, optarg);
2076
        if (!opts) {
2077
            fprintf(stderr, "parse error: %s\n", optarg);
2078
            exit(1);
2079
        }
2080
    }
2081

    
2082
    opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2083
    if (!opts) {
2084
        fprintf(stderr, "duplicate chardev: %s\n", label);
2085
        exit(1);
2086
    }
2087
    qemu_opt_set(opts, "mode", mode);
2088
    qemu_opt_set(opts, "chardev", label);
2089
    if (def)
2090
        qemu_opt_set(opts, "default", "on");
2091
    monitor_device_index++;
2092
}
2093

    
2094
struct device_config {
2095
    enum {
2096
        DEV_USB,       /* -usbdevice     */
2097
        DEV_BT,        /* -bt            */
2098
        DEV_SERIAL,    /* -serial        */
2099
        DEV_PARALLEL,  /* -parallel      */
2100
        DEV_VIRTCON,   /* -virtioconsole */
2101
        DEV_DEBUGCON,  /* -debugcon */
2102
    } type;
2103
    const char *cmdline;
2104
    QTAILQ_ENTRY(device_config) next;
2105
};
2106
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
2107

    
2108
static void add_device_config(int type, const char *cmdline)
2109
{
2110
    struct device_config *conf;
2111

    
2112
    conf = qemu_mallocz(sizeof(*conf));
2113
    conf->type = type;
2114
    conf->cmdline = cmdline;
2115
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2116
}
2117

    
2118
static int foreach_device_config(int type, int (*func)(const char *cmdline))
2119
{
2120
    struct device_config *conf;
2121
    int rc;
2122

    
2123
    QTAILQ_FOREACH(conf, &device_configs, next) {
2124
        if (conf->type != type)
2125
            continue;
2126
        rc = func(conf->cmdline);
2127
        if (0 != rc)
2128
            return rc;
2129
    }
2130
    return 0;
2131
}
2132

    
2133
static int serial_parse(const char *devname)
2134
{
2135
    static int index = 0;
2136
    char label[32];
2137

    
2138
    if (strcmp(devname, "none") == 0)
2139
        return 0;
2140
    if (index == MAX_SERIAL_PORTS) {
2141
        fprintf(stderr, "qemu: too many serial ports\n");
2142
        exit(1);
2143
    }
2144
    snprintf(label, sizeof(label), "serial%d", index);
2145
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
2146
    if (!serial_hds[index]) {
2147
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2148
                devname, strerror(errno));
2149
        return -1;
2150
    }
2151
    index++;
2152
    return 0;
2153
}
2154

    
2155
static int parallel_parse(const char *devname)
2156
{
2157
    static int index = 0;
2158
    char label[32];
2159

    
2160
    if (strcmp(devname, "none") == 0)
2161
        return 0;
2162
    if (index == MAX_PARALLEL_PORTS) {
2163
        fprintf(stderr, "qemu: too many parallel ports\n");
2164
        exit(1);
2165
    }
2166
    snprintf(label, sizeof(label), "parallel%d", index);
2167
    parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2168
    if (!parallel_hds[index]) {
2169
        fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2170
                devname, strerror(errno));
2171
        return -1;
2172
    }
2173
    index++;
2174
    return 0;
2175
}
2176

    
2177
static int virtcon_parse(const char *devname)
2178
{
2179
    static int index = 0;
2180
    char label[32];
2181
    QemuOpts *bus_opts, *dev_opts;
2182

    
2183
    if (strcmp(devname, "none") == 0)
2184
        return 0;
2185
    if (index == MAX_VIRTIO_CONSOLES) {
2186
        fprintf(stderr, "qemu: too many virtio consoles\n");
2187
        exit(1);
2188
    }
2189

    
2190
    bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2191
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
2192

    
2193
    dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2194
    qemu_opt_set(dev_opts, "driver", "virtconsole");
2195

    
2196
    snprintf(label, sizeof(label), "virtcon%d", index);
2197
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2198
    if (!virtcon_hds[index]) {
2199
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2200
                devname, strerror(errno));
2201
        return -1;
2202
    }
2203
    qemu_opt_set(dev_opts, "chardev", label);
2204

    
2205
    index++;
2206
    return 0;
2207
}
2208

    
2209
static int debugcon_parse(const char *devname)
2210
{   
2211
    QemuOpts *opts;
2212

    
2213
    if (!qemu_chr_open("debugcon", devname, NULL)) {
2214
        exit(1);
2215
    }
2216
    opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2217
    if (!opts) {
2218
        fprintf(stderr, "qemu: already have a debugcon device\n");
2219
        exit(1);
2220
    }
2221
    qemu_opt_set(opts, "driver", "isa-debugcon");
2222
    qemu_opt_set(opts, "chardev", "debugcon");
2223
    return 0;
2224
}
2225

    
2226
static const QEMUOption *lookup_opt(int argc, char **argv,
2227
                                    const char **poptarg, int *poptind)
2228
{
2229
    const QEMUOption *popt;
2230
    int optind = *poptind;
2231
    char *r = argv[optind];
2232
    const char *optarg;
2233

    
2234
    loc_set_cmdline(argv, optind, 1);
2235
    optind++;
2236
    /* Treat --foo the same as -foo.  */
2237
    if (r[1] == '-')
2238
        r++;
2239
    popt = qemu_options;
2240
    for(;;) {
2241
        if (!popt->name) {
2242
            error_report("invalid option");
2243
            exit(1);
2244
        }
2245
        if (!strcmp(popt->name, r + 1))
2246
            break;
2247
        popt++;
2248
    }
2249
    if (popt->flags & HAS_ARG) {
2250
        if (optind >= argc) {
2251
            error_report("requires an argument");
2252
            exit(1);
2253
        }
2254
        optarg = argv[optind++];
2255
        loc_set_cmdline(argv, optind - 2, 2);
2256
    } else {
2257
        optarg = NULL;
2258
    }
2259

    
2260
    *poptarg = optarg;
2261
    *poptind = optind;
2262

    
2263
    return popt;
2264
}
2265

    
2266
int main(int argc, char **argv, char **envp)
2267
{
2268
    const char *gdbstub_dev = NULL;
2269
    int i;
2270
    int snapshot, linux_boot;
2271
    const char *icount_option = NULL;
2272
    const char *initrd_filename;
2273
    const char *kernel_filename, *kernel_cmdline;
2274
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2275
    DisplayState *ds;
2276
    DisplayChangeListener *dcl;
2277
    int cyls, heads, secs, translation;
2278
    QemuOpts *hda_opts = NULL, *opts;
2279
    int optind;
2280
    const char *optarg;
2281
    const char *loadvm = NULL;
2282
    QEMUMachine *machine;
2283
    const char *cpu_model;
2284
    int tb_size;
2285
    const char *pid_file = NULL;
2286
    const char *incoming = NULL;
2287
    int show_vnc_port = 0;
2288
    int defconfig = 1;
2289

    
2290
    error_set_progname(argv[0]);
2291

    
2292
    init_clocks();
2293

    
2294
    qemu_cache_utils_init(envp);
2295

    
2296
    QLIST_INIT (&vm_change_state_head);
2297
    os_setup_early_signal_handling();
2298

    
2299
    module_call_init(MODULE_INIT_MACHINE);
2300
    machine = find_default_machine();
2301
    cpu_model = NULL;
2302
    initrd_filename = NULL;
2303
    ram_size = 0;
2304
    snapshot = 0;
2305
    kernel_filename = NULL;
2306
    kernel_cmdline = "";
2307
    cyls = heads = secs = 0;
2308
    translation = BIOS_ATA_TRANSLATION_AUTO;
2309

    
2310
    for (i = 0; i < MAX_NODES; i++) {
2311
        node_mem[i] = 0;
2312
        node_cpumask[i] = 0;
2313
    }
2314

    
2315
    nb_numa_nodes = 0;
2316
    nb_nics = 0;
2317

    
2318
    tb_size = 0;
2319
    autostart= 1;
2320

    
2321
    /* first pass of option parsing */
2322
    optind = 1;
2323
    while (optind < argc) {
2324
        if (argv[optind][0] != '-') {
2325
            /* disk image */
2326
            optind++;
2327
            continue;
2328
        } else {
2329
            const QEMUOption *popt;
2330

    
2331
            popt = lookup_opt(argc, argv, &optarg, &optind);
2332
            switch (popt->index) {
2333
            case QEMU_OPTION_nodefconfig:
2334
                defconfig=0;
2335
                break;
2336
            }
2337
        }
2338
    }
2339

    
2340
    if (defconfig) {
2341
        int ret;
2342

    
2343
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2344
        if (ret < 0 && ret != -ENOENT) {
2345
            exit(1);
2346
        }
2347

    
2348
        ret = qemu_read_config_file(arch_config_name);
2349
        if (ret < 0 && ret != -ENOENT) {
2350
            exit(1);
2351
        }
2352
    }
2353
    cpudef_init();
2354

    
2355
    /* second pass of option parsing */
2356
    optind = 1;
2357
    for(;;) {
2358
        if (optind >= argc)
2359
            break;
2360
        if (argv[optind][0] != '-') {
2361
            hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
2362
        } else {
2363
            const QEMUOption *popt;
2364

    
2365
            popt = lookup_opt(argc, argv, &optarg, &optind);
2366
            if (!(popt->arch_mask & arch_type)) {
2367
                printf("Option %s not supported for this target\n", popt->name);
2368
                exit(1);
2369
            }
2370
            switch(popt->index) {
2371
            case QEMU_OPTION_M:
2372
                machine = find_machine(optarg);
2373
                if (!machine) {
2374
                    QEMUMachine *m;
2375
                    printf("Supported machines are:\n");
2376
                    for(m = first_machine; m != NULL; m = m->next) {
2377
                        if (m->alias)
2378
                            printf("%-10s %s (alias of %s)\n",
2379
                                   m->alias, m->desc, m->name);
2380
                        printf("%-10s %s%s\n",
2381
                               m->name, m->desc,
2382
                               m->is_default ? " (default)" : "");
2383
                    }
2384
                    exit(*optarg != '?');
2385
                }
2386
                break;
2387
            case QEMU_OPTION_cpu:
2388
                /* hw initialization will check this */
2389
                if (*optarg == '?') {
2390
                    list_cpus(stdout, &fprintf, optarg);
2391
                    exit(0);
2392
                } else {
2393
                    cpu_model = optarg;
2394
                }
2395
                break;
2396
            case QEMU_OPTION_initrd:
2397
                initrd_filename = optarg;
2398
                break;
2399
            case QEMU_OPTION_hda:
2400
                if (cyls == 0)
2401
                    hda_opts = drive_add(optarg, HD_ALIAS, 0);
2402
                else
2403
                    hda_opts = drive_add(optarg, HD_ALIAS
2404
                             ",cyls=%d,heads=%d,secs=%d%s",
2405
                             0, cyls, heads, secs,
2406
                             translation == BIOS_ATA_TRANSLATION_LBA ?
2407
                                 ",trans=lba" :
2408
                             translation == BIOS_ATA_TRANSLATION_NONE ?
2409
                                 ",trans=none" : "");
2410
                 break;
2411
            case QEMU_OPTION_hdb:
2412
            case QEMU_OPTION_hdc:
2413
            case QEMU_OPTION_hdd:
2414
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
2415
                break;
2416
            case QEMU_OPTION_drive:
2417
                drive_add(NULL, "%s", optarg);
2418
                break;
2419
            case QEMU_OPTION_set:
2420
                if (qemu_set_option(optarg) != 0)
2421
                    exit(1);
2422
                break;
2423
            case QEMU_OPTION_global:
2424
                if (qemu_global_option(optarg) != 0)
2425
                    exit(1);
2426
                break;
2427
            case QEMU_OPTION_mtdblock:
2428
                drive_add(optarg, MTD_ALIAS);
2429
                break;
2430
            case QEMU_OPTION_sd:
2431
                drive_add(optarg, SD_ALIAS);
2432
                break;
2433
            case QEMU_OPTION_pflash:
2434
                drive_add(optarg, PFLASH_ALIAS);
2435
                break;
2436
            case QEMU_OPTION_snapshot:
2437
                snapshot = 1;
2438
                break;
2439
            case QEMU_OPTION_hdachs:
2440
                {
2441
                    const char *p;
2442
                    p = optarg;
2443
                    cyls = strtol(p, (char **)&p, 0);
2444
                    if (cyls < 1 || cyls > 16383)
2445
                        goto chs_fail;
2446
                    if (*p != ',')
2447
                        goto chs_fail;
2448
                    p++;
2449
                    heads = strtol(p, (char **)&p, 0);
2450
                    if (heads < 1 || heads > 16)
2451
                        goto chs_fail;
2452
                    if (*p != ',')
2453
                        goto chs_fail;
2454
                    p++;
2455
                    secs = strtol(p, (char **)&p, 0);
2456
                    if (secs < 1 || secs > 63)
2457
                        goto chs_fail;
2458
                    if (*p == ',') {
2459
                        p++;
2460
                        if (!strcmp(p, "none"))
2461
                            translation = BIOS_ATA_TRANSLATION_NONE;
2462
                        else if (!strcmp(p, "lba"))
2463
                            translation = BIOS_ATA_TRANSLATION_LBA;
2464
                        else if (!strcmp(p, "auto"))
2465
                            translation = BIOS_ATA_TRANSLATION_AUTO;
2466
                        else
2467
                            goto chs_fail;
2468
                    } else if (*p != '\0') {
2469
                    chs_fail:
2470
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
2471
                        exit(1);
2472
                    }
2473
                    if (hda_opts != NULL) {
2474
                        char num[16];
2475
                        snprintf(num, sizeof(num), "%d", cyls);
2476
                        qemu_opt_set(hda_opts, "cyls", num);
2477
                        snprintf(num, sizeof(num), "%d", heads);
2478
                        qemu_opt_set(hda_opts, "heads", num);
2479
                        snprintf(num, sizeof(num), "%d", secs);
2480
                        qemu_opt_set(hda_opts, "secs", num);
2481
                        if (translation == BIOS_ATA_TRANSLATION_LBA)
2482
                            qemu_opt_set(hda_opts, "trans", "lba");
2483
                        if (translation == BIOS_ATA_TRANSLATION_NONE)
2484
                            qemu_opt_set(hda_opts, "trans", "none");
2485
                    }
2486
                }
2487
                break;
2488
            case QEMU_OPTION_numa:
2489
                if (nb_numa_nodes >= MAX_NODES) {
2490
                    fprintf(stderr, "qemu: too many NUMA nodes\n");
2491
                    exit(1);
2492
                }
2493
                numa_add(optarg);
2494
                break;
2495
            case QEMU_OPTION_nographic:
2496
                display_type = DT_NOGRAPHIC;
2497
                break;
2498
#ifdef CONFIG_CURSES
2499
            case QEMU_OPTION_curses:
2500
                display_type = DT_CURSES;
2501
                break;
2502
#endif
2503
            case QEMU_OPTION_portrait:
2504
                graphic_rotate = 1;
2505
                break;
2506
            case QEMU_OPTION_kernel:
2507
                kernel_filename = optarg;
2508
                break;
2509
            case QEMU_OPTION_append:
2510
                kernel_cmdline = optarg;
2511
                break;
2512
            case QEMU_OPTION_cdrom:
2513
                drive_add(optarg, CDROM_ALIAS);
2514
                break;
2515
            case QEMU_OPTION_boot:
2516
                {
2517
                    static const char * const params[] = {
2518
                        "order", "once", "menu", NULL
2519
                    };
2520
                    char buf[sizeof(boot_devices)];
2521
                    char *standard_boot_devices;
2522
                    int legacy = 0;
2523

    
2524
                    if (!strchr(optarg, '=')) {
2525
                        legacy = 1;
2526
                        pstrcpy(buf, sizeof(buf), optarg);
2527
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2528
                        fprintf(stderr,
2529
                                "qemu: unknown boot parameter '%s' in '%s'\n",
2530
                                buf, optarg);
2531
                        exit(1);
2532
                    }
2533

    
2534
                    if (legacy ||
2535
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
2536
                        validate_bootdevices(buf);
2537
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
2538
                    }
2539
                    if (!legacy) {
2540
                        if (get_param_value(buf, sizeof(buf),
2541
                                            "once", optarg)) {
2542
                            validate_bootdevices(buf);
2543
                            standard_boot_devices = qemu_strdup(boot_devices);
2544
                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
2545
                            qemu_register_reset(restore_boot_devices,
2546
                                                standard_boot_devices);
2547
                        }
2548
                        if (get_param_value(buf, sizeof(buf),
2549
                                            "menu", optarg)) {
2550
                            if (!strcmp(buf, "on")) {
2551
                                boot_menu = 1;
2552
                            } else if (!strcmp(buf, "off")) {
2553
                                boot_menu = 0;
2554
                            } else {
2555
                                fprintf(stderr,
2556
                                        "qemu: invalid option value '%s'\n",
2557
                                        buf);
2558
                                exit(1);
2559
                            }
2560
                        }
2561
                    }
2562
                }
2563
                break;
2564
            case QEMU_OPTION_fda:
2565
            case QEMU_OPTION_fdb:
2566
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2567
                break;
2568
            case QEMU_OPTION_no_fd_bootchk:
2569
                fd_bootchk = 0;
2570
                break;
2571
            case QEMU_OPTION_netdev:
2572
                if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2573
                    exit(1);
2574
                }
2575
                break;
2576
            case QEMU_OPTION_net:
2577
                if (net_client_parse(&qemu_net_opts, optarg) == -1) {
2578
                    exit(1);
2579
                }
2580
                break;
2581
#ifdef CONFIG_SLIRP
2582
            case QEMU_OPTION_tftp:
2583
                legacy_tftp_prefix = optarg;
2584
                break;
2585
            case QEMU_OPTION_bootp:
2586
                legacy_bootp_filename = optarg;
2587
                break;
2588
            case QEMU_OPTION_redir:
2589
                if (net_slirp_redir(optarg) < 0)
2590
                    exit(1);
2591
                break;
2592
#endif
2593
            case QEMU_OPTION_bt:
2594
                add_device_config(DEV_BT, optarg);
2595
                break;
2596
            case QEMU_OPTION_audio_help:
2597
                if (!(audio_available())) {
2598
                    printf("Option %s not supported for this target\n", popt->name);
2599
                    exit(1);
2600
                }
2601
                AUD_help ();
2602
                exit (0);
2603
                break;
2604
            case QEMU_OPTION_soundhw:
2605
                if (!(audio_available())) {
2606
                    printf("Option %s not supported for this target\n", popt->name);
2607
                    exit(1);
2608
                }
2609
                select_soundhw (optarg);
2610
                break;
2611
            case QEMU_OPTION_h:
2612
                help(0);
2613
                break;
2614
            case QEMU_OPTION_version:
2615
                version();
2616
                exit(0);
2617
                break;
2618
            case QEMU_OPTION_m: {
2619
                uint64_t value;
2620
                char *ptr;
2621

    
2622
                value = strtoul(optarg, &ptr, 10);
2623
                switch (*ptr) {
2624
                case 0: case 'M': case 'm':
2625
                    value <<= 20;
2626
                    break;
2627
                case 'G': case 'g':
2628
                    value <<= 30;
2629
                    break;
2630
                default:
2631
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2632
                    exit(1);
2633
                }
2634

    
2635
                /* On 32-bit hosts, QEMU is limited by virtual address space */
2636
                if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2637
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2638
                    exit(1);
2639
                }
2640
                if (value != (uint64_t)(ram_addr_t)value) {
2641
                    fprintf(stderr, "qemu: ram size too large\n");
2642
                    exit(1);
2643
                }
2644
                ram_size = value;
2645
                break;
2646
            }
2647
            case QEMU_OPTION_mempath:
2648
                mem_path = optarg;
2649
                break;
2650
#ifdef MAP_POPULATE
2651
            case QEMU_OPTION_mem_prealloc:
2652
                mem_prealloc = 1;
2653
                break;
2654
#endif
2655
            case QEMU_OPTION_d:
2656
                set_cpu_log(optarg);
2657
                break;
2658
            case QEMU_OPTION_s:
2659
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2660
                break;
2661
            case QEMU_OPTION_gdb:
2662
                gdbstub_dev = optarg;
2663
                break;
2664
            case QEMU_OPTION_L:
2665
                data_dir = optarg;
2666
                break;
2667
            case QEMU_OPTION_bios:
2668
                bios_name = optarg;
2669
                break;
2670
            case QEMU_OPTION_singlestep:
2671
                singlestep = 1;
2672
                break;
2673
            case QEMU_OPTION_S:
2674
                autostart = 0;
2675
                break;
2676
            case QEMU_OPTION_k:
2677
                keyboard_layout = optarg;
2678
                break;
2679
            case QEMU_OPTION_localtime:
2680
                rtc_utc = 0;
2681
                break;
2682
            case QEMU_OPTION_vga:
2683
                select_vgahw (optarg);
2684
                break;
2685
            case QEMU_OPTION_g:
2686
                {
2687
                    const char *p;
2688
                    int w, h, depth;
2689
                    p = optarg;
2690
                    w = strtol(p, (char **)&p, 10);
2691
                    if (w <= 0) {
2692
                    graphic_error:
2693
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
2694
                        exit(1);
2695
                    }
2696
                    if (*p != 'x')
2697
                        goto graphic_error;
2698
                    p++;
2699
                    h = strtol(p, (char **)&p, 10);
2700
                    if (h <= 0)
2701
                        goto graphic_error;
2702
                    if (*p == 'x') {
2703
                        p++;
2704
                        depth = strtol(p, (char **)&p, 10);
2705
                        if (depth != 8 && depth != 15 && depth != 16 &&
2706
                            depth != 24 && depth != 32)
2707
                            goto graphic_error;
2708
                    } else if (*p == '\0') {
2709
                        depth = graphic_depth;
2710
                    } else {
2711
                        goto graphic_error;
2712
                    }
2713

    
2714
                    graphic_width = w;
2715
                    graphic_height = h;
2716
                    graphic_depth = depth;
2717
                }
2718
                break;
2719
            case QEMU_OPTION_echr:
2720
                {
2721
                    char *r;
2722
                    term_escape_char = strtol(optarg, &r, 0);
2723
                    if (r == optarg)
2724
                        printf("Bad argument to echr\n");
2725
                    break;
2726
                }
2727
            case QEMU_OPTION_monitor:
2728
                monitor_parse(optarg, "readline");
2729
                default_monitor = 0;
2730
                break;
2731
            case QEMU_OPTION_qmp:
2732
                monitor_parse(optarg, "control");
2733
                default_monitor = 0;
2734
                break;
2735
            case QEMU_OPTION_mon:
2736
                opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
2737
                if (!opts) {
2738
                    exit(1);
2739
                }
2740
                default_monitor = 0;
2741
                break;
2742
            case QEMU_OPTION_chardev:
2743
                opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
2744
                if (!opts) {
2745
                    exit(1);
2746
                }
2747
                break;
2748
#ifdef CONFIG_LINUX
2749
            case QEMU_OPTION_fsdev:
2750
                opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2751
                if (!opts) {
2752
                    fprintf(stderr, "parse error: %s\n", optarg);
2753
                    exit(1);
2754
                }
2755
                break;
2756
            case QEMU_OPTION_virtfs: {
2757
                char *arg_fsdev = NULL;
2758
                char *arg_9p = NULL;
2759
                int len = 0;
2760

    
2761
                opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2762
                if (!opts) {
2763
                    fprintf(stderr, "parse error: %s\n", optarg);
2764
                    exit(1);
2765
                }
2766

    
2767
                len = strlen(",id=,path=");
2768
                len += strlen(qemu_opt_get(opts, "fstype"));
2769
                len += strlen(qemu_opt_get(opts, "mount_tag"));
2770
                len += strlen(qemu_opt_get(opts, "path"));
2771
                arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2772

    
2773
                if (!arg_fsdev) {
2774
                    fprintf(stderr, "No memory to parse -fsdev for %s\n",
2775
                            optarg);
2776
                    exit(1);
2777
                }
2778

    
2779
                sprintf(arg_fsdev, "%s,id=%s,path=%s",
2780
                                qemu_opt_get(opts, "fstype"),
2781
                                qemu_opt_get(opts, "mount_tag"),
2782
                                qemu_opt_get(opts, "path"));
2783

    
2784
                len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2785
                len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2786
                arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2787

    
2788
                if (!arg_9p) {
2789
                    fprintf(stderr, "No memory to parse -device for %s\n",
2790
                            optarg);
2791
                    exit(1);
2792
                }
2793

    
2794
                sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2795
                                qemu_opt_get(opts, "mount_tag"),
2796
                                qemu_opt_get(opts, "mount_tag"));
2797

    
2798
                if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2799
                    fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2800
                    exit(1);
2801
                }
2802

    
2803
                if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2804
                    fprintf(stderr, "parse error [device]: %s\n", optarg);
2805
                    exit(1);
2806
                }
2807

    
2808
                qemu_free(arg_fsdev);
2809
                qemu_free(arg_9p);
2810
                break;
2811
            }
2812
#endif
2813
            case QEMU_OPTION_serial:
2814
                add_device_config(DEV_SERIAL, optarg);
2815
                default_serial = 0;
2816
                if (strncmp(optarg, "mon:", 4) == 0) {
2817
                    default_monitor = 0;
2818
                }
2819
                break;
2820
            case QEMU_OPTION_watchdog:
2821
                if (watchdog) {
2822
                    fprintf(stderr,
2823
                            "qemu: only one watchdog option may be given\n");
2824
                    return 1;
2825
                }
2826
                watchdog = optarg;
2827
                break;
2828
            case QEMU_OPTION_watchdog_action:
2829
                if (select_watchdog_action(optarg) == -1) {
2830
                    fprintf(stderr, "Unknown -watchdog-action parameter\n");
2831
                    exit(1);
2832
                }
2833
                break;
2834
            case QEMU_OPTION_virtiocon:
2835
                add_device_config(DEV_VIRTCON, optarg);
2836
                default_virtcon = 0;
2837
                if (strncmp(optarg, "mon:", 4) == 0) {
2838
                    default_monitor = 0;
2839
                }
2840
                break;
2841
            case QEMU_OPTION_parallel:
2842
                add_device_config(DEV_PARALLEL, optarg);
2843
                default_parallel = 0;
2844
                if (strncmp(optarg, "mon:", 4) == 0) {
2845
                    default_monitor = 0;
2846
                }
2847
                break;
2848
            case QEMU_OPTION_debugcon:
2849
                add_device_config(DEV_DEBUGCON, optarg);
2850
                break;
2851
            case QEMU_OPTION_loadvm:
2852
                loadvm = optarg;
2853
                break;
2854
            case QEMU_OPTION_full_screen:
2855
                full_screen = 1;
2856
                break;
2857
#ifdef CONFIG_SDL
2858
            case QEMU_OPTION_no_frame:
2859
                no_frame = 1;
2860
                break;
2861
            case QEMU_OPTION_alt_grab:
2862
                alt_grab = 1;
2863
                break;
2864
            case QEMU_OPTION_ctrl_grab:
2865
                ctrl_grab = 1;
2866
                break;
2867
            case QEMU_OPTION_no_quit:
2868
                no_quit = 1;
2869
                break;
2870
            case QEMU_OPTION_sdl:
2871
                display_type = DT_SDL;
2872
                break;
2873
#endif
2874
            case QEMU_OPTION_pidfile:
2875
                pid_file = optarg;
2876
                break;
2877
            case QEMU_OPTION_win2k_hack:
2878
                win2k_install_hack = 1;
2879
                break;
2880
            case QEMU_OPTION_rtc_td_hack:
2881
                rtc_td_hack = 1;
2882
                break;
2883
            case QEMU_OPTION_acpitable:
2884
                do_acpitable_option(optarg);
2885
                break;
2886
            case QEMU_OPTION_smbios:
2887
                do_smbios_option(optarg);
2888
                break;
2889
            case QEMU_OPTION_enable_kvm:
2890
                kvm_allowed = 1;
2891
                break;
2892
            case QEMU_OPTION_usb:
2893
                usb_enabled = 1;
2894
                break;
2895
            case QEMU_OPTION_usbdevice:
2896
                usb_enabled = 1;
2897
                add_device_config(DEV_USB, optarg);
2898
                break;
2899
            case QEMU_OPTION_device:
2900
                if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
2901
                    exit(1);
2902
                }
2903
                break;
2904
            case QEMU_OPTION_smp:
2905
                smp_parse(optarg);
2906
                if (smp_cpus < 1) {
2907
                    fprintf(stderr, "Invalid number of CPUs\n");
2908
                    exit(1);
2909
                }
2910
                if (max_cpus < smp_cpus) {
2911
                    fprintf(stderr, "maxcpus must be equal to or greater than "
2912
                            "smp\n");
2913
                    exit(1);
2914
                }
2915
                if (max_cpus > 255) {
2916
                    fprintf(stderr, "Unsupported number of maxcpus\n");
2917
                    exit(1);
2918
                }
2919
                break;
2920
            case QEMU_OPTION_vnc:
2921
                display_type = DT_VNC;
2922
                vnc_display = optarg;
2923
                break;
2924
            case QEMU_OPTION_no_acpi:
2925
                acpi_enabled = 0;
2926
                break;
2927
            case QEMU_OPTION_no_hpet:
2928
                no_hpet = 1;
2929
                break;
2930
            case QEMU_OPTION_balloon:
2931
                if (balloon_parse(optarg) < 0) {
2932
                    fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2933
                    exit(1);
2934
                }
2935
                break;
2936
            case QEMU_OPTION_no_reboot:
2937
                no_reboot = 1;
2938
                break;
2939
            case QEMU_OPTION_no_shutdown:
2940
                no_shutdown = 1;
2941
                break;
2942
            case QEMU_OPTION_show_cursor:
2943
                cursor_hide = 0;
2944
                break;
2945
            case QEMU_OPTION_uuid:
2946
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2947
                    fprintf(stderr, "Fail to parse UUID string."
2948
                            " Wrong format.\n");
2949
                    exit(1);
2950
                }
2951
                break;
2952
            case QEMU_OPTION_option_rom:
2953
                if (nb_option_roms >= MAX_OPTION_ROMS) {
2954
                    fprintf(stderr, "Too many option ROMs\n");
2955
                    exit(1);
2956
                }
2957
                option_rom[nb_option_roms] = optarg;
2958
                nb_option_roms++;
2959
                break;
2960
            case QEMU_OPTION_semihosting:
2961
                semihosting_enabled = 1;
2962
                break;
2963
            case QEMU_OPTION_name:
2964
                qemu_name = qemu_strdup(optarg);
2965
                 {
2966
                     char *p = strchr(qemu_name, ',');
2967
                     if (p != NULL) {
2968
                        *p++ = 0;
2969
                        if (strncmp(p, "process=", 8)) {
2970
                            fprintf(stderr, "Unknown subargument %s to -name", p);
2971
                            exit(1);
2972
                        }
2973
                        p += 8;
2974
                        os_set_proc_name(p);
2975
                     }        
2976
                 }        
2977
                break;
2978
            case QEMU_OPTION_prom_env:
2979
                if (nb_prom_envs >= MAX_PROM_ENVS) {
2980
                    fprintf(stderr, "Too many prom variables\n");
2981
                    exit(1);
2982
                }
2983
                prom_envs[nb_prom_envs] = optarg;
2984
                nb_prom_envs++;
2985
                break;
2986
            case QEMU_OPTION_old_param:
2987
                old_param = 1;
2988
                break;
2989
            case QEMU_OPTION_clock:
2990
                configure_alarms(optarg);
2991
                break;
2992
            case QEMU_OPTION_startdate:
2993
                configure_rtc_date_offset(optarg, 1);
2994
                break;
2995
            case QEMU_OPTION_rtc:
2996
                opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
2997
                if (!opts) {
2998
                    exit(1);
2999
                }
3000
                configure_rtc(opts);
3001
                break;
3002
            case QEMU_OPTION_tb_size:
3003
                tb_size = strtol(optarg, NULL, 0);
3004
                if (tb_size < 0)
3005
                    tb_size = 0;
3006
                break;
3007
            case QEMU_OPTION_icount:
3008
                icount_option = optarg;
3009
                break;
3010
            case QEMU_OPTION_incoming:
3011
                incoming = optarg;
3012
                break;
3013
            case QEMU_OPTION_nodefaults:
3014
                default_serial = 0;
3015
                default_parallel = 0;
3016
                default_virtcon = 0;
3017
                default_monitor = 0;
3018
                default_vga = 0;
3019
                default_net = 0;
3020
                default_floppy = 0;
3021
                default_cdrom = 0;
3022
                default_sdcard = 0;
3023
                break;
3024
            case QEMU_OPTION_xen_domid:
3025
                if (!(xen_available())) {
3026
                    printf("Option %s not supported for this target\n", popt->name);
3027
                    exit(1);
3028
                }
3029
                xen_domid = atoi(optarg);
3030
                break;
3031
            case QEMU_OPTION_xen_create:
3032
                if (!(xen_available())) {
3033
                    printf("Option %s not supported for this target\n", popt->name);
3034
                    exit(1);
3035
                }
3036
                xen_mode = XEN_CREATE;
3037
                break;
3038
            case QEMU_OPTION_xen_attach:
3039
                if (!(xen_available())) {
3040
                    printf("Option %s not supported for this target\n", popt->name);
3041
                    exit(1);
3042
                }
3043
                xen_mode = XEN_ATTACH;
3044
                break;
3045
            case QEMU_OPTION_readconfig:
3046
                {
3047
                    int ret = qemu_read_config_file(optarg);
3048
                    if (ret < 0) {
3049
                        fprintf(stderr, "read config %s: %s\n", optarg,
3050
                            strerror(-ret));
3051
                        exit(1);
3052
                    }
3053
                    break;
3054
                }
3055
            case QEMU_OPTION_writeconfig:
3056
                {
3057
                    FILE *fp;
3058
                    if (strcmp(optarg, "-") == 0) {
3059
                        fp = stdout;
3060
                    } else {
3061
                        fp = fopen(optarg, "w");
3062
                        if (fp == NULL) {
3063
                            fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3064
                            exit(1);
3065
                        }
3066
                    }
3067
                    qemu_config_write(fp);
3068
                    fclose(fp);
3069
                    break;
3070
                }
3071
            default:
3072
                os_parse_cmd_args(popt->index, optarg);
3073
            }
3074
        }
3075
    }
3076
    loc_set_none();
3077

    
3078
    /* If no data_dir is specified then try to find it relative to the
3079
       executable path.  */
3080
    if (!data_dir) {
3081
        data_dir = os_find_datadir(argv[0]);
3082
    }
3083
    /* If all else fails use the install patch specified when building.  */
3084
    if (!data_dir) {
3085
        data_dir = CONFIG_QEMU_DATADIR;
3086
    }
3087

    
3088
    /*
3089
     * Default to max_cpus = smp_cpus, in case the user doesn't
3090
     * specify a max_cpus value.
3091
     */
3092
    if (!max_cpus)
3093
        max_cpus = smp_cpus;
3094

    
3095
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3096
    if (smp_cpus > machine->max_cpus) {
3097
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3098
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
3099
                machine->max_cpus);
3100
        exit(1);
3101
    }
3102

    
3103
    qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
3104
    qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
3105

    
3106
    if (machine->no_serial) {
3107
        default_serial = 0;
3108
    }
3109
    if (machine->no_parallel) {
3110
        default_parallel = 0;
3111
    }
3112
    if (!machine->use_virtcon) {
3113
        default_virtcon = 0;
3114
    }
3115
    if (machine->no_vga) {
3116
        default_vga = 0;
3117
    }
3118
    if (machine->no_floppy) {
3119
        default_floppy = 0;
3120
    }
3121
    if (machine->no_cdrom) {
3122
        default_cdrom = 0;
3123
    }
3124
    if (machine->no_sdcard) {
3125
        default_sdcard = 0;
3126
    }
3127

    
3128
    if (display_type == DT_NOGRAPHIC) {
3129
        if (default_parallel)
3130
            add_device_config(DEV_PARALLEL, "null");
3131
        if (default_serial && default_monitor) {
3132
            add_device_config(DEV_SERIAL, "mon:stdio");
3133
        } else if (default_virtcon && default_monitor) {
3134
            add_device_config(DEV_VIRTCON, "mon:stdio");
3135
        } else {
3136
            if (default_serial)
3137
                add_device_config(DEV_SERIAL, "stdio");
3138
            if (default_virtcon)
3139
                add_device_config(DEV_VIRTCON, "stdio");
3140
            if (default_monitor)
3141
                monitor_parse("stdio", "readline");
3142
        }
3143
    } else {
3144
        if (default_serial)
3145
            add_device_config(DEV_SERIAL, "vc:80Cx24C");
3146
        if (default_parallel)
3147
            add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3148
        if (default_monitor)
3149
            monitor_parse("vc:80Cx24C", "readline");
3150
        if (default_virtcon)
3151
            add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3152
    }
3153
    if (default_vga)
3154
        vga_interface_type = VGA_CIRRUS;
3155

    
3156
    socket_init();
3157

    
3158
    if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3159
        exit(1);
3160
#ifdef CONFIG_LINUX
3161
    if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
3162
        exit(1);
3163
    }
3164
#endif
3165

    
3166
    os_daemonize();
3167

    
3168
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3169
        os_pidfile_error();
3170
        exit(1);
3171
    }
3172

    
3173
    if (kvm_allowed) {
3174
        int ret = kvm_init(smp_cpus);
3175
        if (ret < 0) {
3176
            if (!kvm_available()) {
3177
                printf("KVM not supported for this target\n");
3178
            } else {
3179
                fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
3180
            }
3181
            exit(1);
3182
        }
3183
    }
3184

    
3185
    if (qemu_init_main_loop()) {
3186
        fprintf(stderr, "qemu_init_main_loop failed\n");
3187
        exit(1);
3188
    }
3189
    linux_boot = (kernel_filename != NULL);
3190

    
3191
    if (!linux_boot && *kernel_cmdline != '\0') {
3192
        fprintf(stderr, "-append only allowed with -kernel option\n");
3193
        exit(1);
3194
    }
3195

    
3196
    if (!linux_boot && initrd_filename != NULL) {
3197
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
3198
        exit(1);
3199
    }
3200

    
3201
    os_set_line_buffering();
3202

    
3203
    if (init_timer_alarm() < 0) {
3204
        fprintf(stderr, "could not initialize alarm timer\n");
3205
        exit(1);
3206
    }
3207
    configure_icount(icount_option);
3208

    
3209
    if (net_init_clients() < 0) {
3210
        exit(1);
3211
    }
3212

    
3213
    /* init the bluetooth world */
3214
    if (foreach_device_config(DEV_BT, bt_parse))
3215
        exit(1);
3216

    
3217
    /* init the memory */
3218
    if (ram_size == 0)
3219
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3220

    
3221
    /* init the dynamic translator */
3222
    cpu_exec_init_all(tb_size * 1024 * 1024);
3223

    
3224
    bdrv_init_with_whitelist();
3225

    
3226
    blk_mig_init();
3227

    
3228
    if (default_cdrom) {
3229
        /* we always create the cdrom drive, even if no disk is there */
3230
        drive_add(NULL, CDROM_ALIAS);
3231
    }
3232

    
3233
    if (default_floppy) {
3234
        /* we always create at least one floppy */
3235
        drive_add(NULL, FD_ALIAS, 0);
3236
    }
3237

    
3238
    if (default_sdcard) {
3239
        /* we always create one sd slot, even if no card is in it */
3240
        drive_add(NULL, SD_ALIAS);
3241
    }
3242

    
3243
    /* open the virtual block devices */
3244
    if (snapshot)
3245
        qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3246
    if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
3247
        exit(1);
3248

    
3249
    register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL, 
3250
                         ram_load, NULL);
3251

    
3252
    if (nb_numa_nodes > 0) {
3253
        int i;
3254

    
3255
        if (nb_numa_nodes > smp_cpus) {
3256
            nb_numa_nodes = smp_cpus;
3257
        }
3258

    
3259
        /* If no memory size if given for any node, assume the default case
3260
         * and distribute the available memory equally across all nodes
3261
         */
3262
        for (i = 0; i < nb_numa_nodes; i++) {
3263
            if (node_mem[i] != 0)
3264
                break;
3265
        }
3266
        if (i == nb_numa_nodes) {
3267
            uint64_t usedmem = 0;
3268

    
3269
            /* On Linux, the each node's border has to be 8MB aligned,
3270
             * the final node gets the rest.
3271
             */
3272
            for (i = 0; i < nb_numa_nodes - 1; i++) {
3273
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3274
                usedmem += node_mem[i];
3275
            }
3276
            node_mem[i] = ram_size - usedmem;
3277
        }
3278

    
3279
        for (i = 0; i < nb_numa_nodes; i++) {
3280
            if (node_cpumask[i] != 0)
3281
                break;
3282
        }
3283
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
3284
         * must cope with this anyway, because there are BIOSes out there in
3285
         * real machines which also use this scheme.
3286
         */
3287
        if (i == nb_numa_nodes) {
3288
            for (i = 0; i < smp_cpus; i++) {
3289
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
3290
            }
3291
        }
3292
    }
3293

    
3294
    if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
3295
        exit(1);
3296
    }
3297

    
3298
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3299
        exit(1);
3300
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3301
        exit(1);
3302
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3303
        exit(1);
3304
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3305
        exit(1);
3306

    
3307
    module_call_init(MODULE_INIT_DEVICE);
3308

    
3309
    if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3310
        exit(0);
3311

    
3312
    if (watchdog) {
3313
        i = select_watchdog(watchdog);
3314
        if (i > 0)
3315
            exit (i == 1 ? 1 : 0);
3316
    }
3317

    
3318
    if (machine->compat_props) {
3319
        qdev_prop_register_global_list(machine->compat_props);
3320
    }
3321
    qemu_add_globals();
3322

    
3323
    machine->init(ram_size, boot_devices,
3324
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3325

    
3326
    cpu_synchronize_all_post_init();
3327

    
3328
    /* must be after terminal init, SDL library changes signal handlers */
3329
    os_setup_signal_handling();
3330

    
3331
    set_numa_modes();
3332

    
3333
    current_machine = machine;
3334

    
3335
    /* init USB devices */
3336
    if (usb_enabled) {
3337
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
3338
            exit(1);
3339
    }
3340

    
3341
    /* init generic devices */
3342
    if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
3343
        exit(1);
3344

    
3345
    net_check_clients();
3346

    
3347
    /* just use the first displaystate for the moment */
3348
    ds = get_displaystate();
3349

    
3350
    if (display_type == DT_DEFAULT) {
3351
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3352
        display_type = DT_SDL;
3353
#else
3354
        display_type = DT_VNC;
3355
        vnc_display = "localhost:0,to=99";
3356
        show_vnc_port = 1;
3357
#endif
3358
    }
3359
        
3360

    
3361
    switch (display_type) {
3362
    case DT_NOGRAPHIC:
3363
        break;
3364
#if defined(CONFIG_CURSES)
3365
    case DT_CURSES:
3366
        curses_display_init(ds, full_screen);
3367
        break;
3368
#endif
3369
#if defined(CONFIG_SDL)
3370
    case DT_SDL:
3371
        sdl_display_init(ds, full_screen, no_frame);
3372
        break;
3373
#elif defined(CONFIG_COCOA)
3374
    case DT_SDL:
3375
        cocoa_display_init(ds, full_screen);
3376
        break;
3377
#endif
3378
    case DT_VNC:
3379
        vnc_display_init(ds);
3380
        if (vnc_display_open(ds, vnc_display) < 0)
3381
            exit(1);
3382

    
3383
        if (show_vnc_port) {
3384
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3385
        }
3386
        break;
3387
    default:
3388
        break;
3389
    }
3390
    dpy_resize(ds);
3391

    
3392
    dcl = ds->listeners;
3393
    while (dcl != NULL) {
3394
        if (dcl->dpy_refresh != NULL) {
3395
            ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3396
            qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3397
            break;
3398
        }
3399
        dcl = dcl->next;
3400
    }
3401

    
3402
    if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
3403
        nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3404
        qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3405
    }
3406

    
3407
    text_consoles_set_display(ds);
3408

    
3409
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3410
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3411
                gdbstub_dev);
3412
        exit(1);
3413
    }
3414

    
3415
    qdev_machine_creation_done();
3416

    
3417
    if (rom_load_all() != 0) {
3418
        fprintf(stderr, "rom loading failed\n");
3419
        exit(1);
3420
    }
3421

    
3422
    qemu_system_reset();
3423
    if (loadvm) {
3424
        if (load_vmstate(loadvm) < 0) {
3425
            autostart = 0;
3426
        }
3427
    }
3428

    
3429
    if (incoming) {
3430
        qemu_start_incoming_migration(incoming);
3431
    } else if (autostart) {
3432
        vm_start();
3433
    }
3434

    
3435
    os_setup_post();
3436

    
3437
    main_loop();
3438
    quit_timers();
3439
    net_cleanup();
3440

    
3441
    return 0;
3442
}