Statistics
| Branch: | Revision:

root / vl.c @ e447fc63

History | View | Annotate | Download (92.5 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

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

    
64
#include <linux/ppdev.h>
65
#include <linux/parport.h>
66
#endif
67
#ifdef __sun__
68
#include <sys/stat.h>
69
#include <sys/ethernet.h>
70
#include <sys/sockio.h>
71
#include <netinet/arp.h>
72
#include <netinet/in_systm.h>
73
#include <netinet/ip.h>
74
#include <netinet/ip_icmp.h> // must come after ip.h
75
#include <netinet/udp.h>
76
#include <netinet/tcp.h>
77
#include <net/if.h>
78
#include <syslog.h>
79
#include <stropts.h>
80
#endif
81
#endif
82
#endif
83

    
84
#if defined(__OpenBSD__)
85
#include <util.h>
86
#endif
87

    
88
#if defined(CONFIG_VDE)
89
#include <libvdeplug.h>
90
#endif
91

    
92
#ifdef _WIN32
93
#include <windows.h>
94
#endif
95

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

    
109
#ifdef CONFIG_COCOA
110
#undef main
111
#define main qemu_main
112
#endif /* CONFIG_COCOA */
113

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

    
152
#include "disas.h"
153

    
154
#include "qemu_socket.h"
155

    
156
#include "slirp/libslirp.h"
157

    
158
#include "trace.h"
159
#include "simpletrace.h"
160
#include "qemu-queue.h"
161
#include "cpus.h"
162
#include "arch_init.h"
163

    
164
#include "ui/qemu-spice.h"
165

    
166
//#define DEBUG_NET
167
//#define DEBUG_SLIRP
168

    
169
#define DEFAULT_RAM_SIZE 128
170

    
171
#define MAX_VIRTIO_CONSOLES 1
172

    
173
static const char *data_dir;
174
const char *bios_name = NULL;
175
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
176
DisplayType display_type = DT_DEFAULT;
177
int display_remote = 0;
178
const char* keyboard_layout = NULL;
179
ram_addr_t ram_size;
180
const char *mem_path = NULL;
181
#ifdef MAP_POPULATE
182
int mem_prealloc = 0; /* force preallocation of physical target memory */
183
#endif
184
int nb_nics;
185
NICInfo nd_table[MAX_NICS];
186
int vm_running;
187
int autostart;
188
int incoming_expected; /* Started with -incoming and waiting for incoming */
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
#ifdef CONFIG_VNC
210
const char *vnc_display;
211
#endif
212
int acpi_enabled = 1;
213
int no_hpet = 0;
214
int fd_bootchk = 1;
215
int no_reboot = 0;
216
int no_shutdown = 0;
217
int cursor_hide = 1;
218
int graphic_rotate = 0;
219
uint8_t irq0override = 1;
220
const char *watchdog;
221
QEMUOptionRom option_rom[MAX_OPTION_ROMS];
222
int nb_option_roms;
223
int semihosting_enabled = 0;
224
int old_param = 0;
225
const char *qemu_name;
226
int alt_grab = 0;
227
int ctrl_grab = 0;
228
unsigned int nb_prom_envs = 0;
229
const char *prom_envs[MAX_PROM_ENVS];
230
int boot_menu;
231

    
232
typedef struct FWBootEntry FWBootEntry;
233

    
234
struct FWBootEntry {
235
    QTAILQ_ENTRY(FWBootEntry) link;
236
    int32_t bootindex;
237
    DeviceState *dev;
238
    char *suffix;
239
};
240

    
241
QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
242

    
243
int nb_numa_nodes;
244
uint64_t node_mem[MAX_NODES];
245
uint64_t node_cpumask[MAX_NODES];
246

    
247
static QEMUTimer *nographic_timer;
248

    
249
uint8_t qemu_uuid[16];
250

    
251
static QEMUBootSetHandler *boot_set_handler;
252
static void *boot_set_opaque;
253

    
254
static NotifierList exit_notifiers =
255
    NOTIFIER_LIST_INITIALIZER(exit_notifiers);
256

    
257
static NotifierList machine_init_done_notifiers =
258
    NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
259

    
260
static int tcg_allowed = 1;
261
int kvm_allowed = 0;
262
int xen_allowed = 0;
263
uint32_t xen_domid;
264
enum xen_mode xen_mode = XEN_EMULATE;
265

    
266
static int default_serial = 1;
267
static int default_parallel = 1;
268
static int default_virtcon = 1;
269
static int default_monitor = 1;
270
static int default_vga = 1;
271
static int default_floppy = 1;
272
static int default_cdrom = 1;
273
static int default_sdcard = 1;
274

    
275
static struct {
276
    const char *driver;
277
    int *flag;
278
} default_list[] = {
279
    { .driver = "isa-serial",           .flag = &default_serial    },
280
    { .driver = "isa-parallel",         .flag = &default_parallel  },
281
    { .driver = "isa-fdc",              .flag = &default_floppy    },
282
    { .driver = "ide-cd",               .flag = &default_cdrom     },
283
    { .driver = "ide-hd",               .flag = &default_cdrom     },
284
    { .driver = "ide-drive",            .flag = &default_cdrom     },
285
    { .driver = "scsi-cd",              .flag = &default_cdrom     },
286
    { .driver = "virtio-serial-pci",    .flag = &default_virtcon   },
287
    { .driver = "virtio-serial-s390",   .flag = &default_virtcon   },
288
    { .driver = "virtio-serial",        .flag = &default_virtcon   },
289
    { .driver = "VGA",                  .flag = &default_vga       },
290
    { .driver = "cirrus-vga",           .flag = &default_vga       },
291
    { .driver = "vmware-svga",          .flag = &default_vga       },
292
    { .driver = "isa-vga",              .flag = &default_vga       },
293
    { .driver = "qxl-vga",              .flag = &default_vga       },
294
};
295

    
296
static int default_driver_check(QemuOpts *opts, void *opaque)
297
{
298
    const char *driver = qemu_opt_get(opts, "driver");
299
    int i;
300

    
301
    if (!driver)
302
        return 0;
303
    for (i = 0; i < ARRAY_SIZE(default_list); i++) {
304
        if (strcmp(default_list[i].driver, driver) != 0)
305
            continue;
306
        *(default_list[i].flag) = 0;
307
    }
308
    return 0;
309
}
310

    
311
/***********************************************************/
312
/* real time host monotonic timer */
313

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

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

    
333
    memcpy(tm, ret, sizeof(struct tm));
334
}
335

    
336
int qemu_timedate_diff(struct tm *tm)
337
{
338
    time_t seconds;
339

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

    
348
    return seconds - time(NULL);
349
}
350

    
351
void rtc_change_mon_event(struct tm *tm)
352
{
353
    QObject *data;
354

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

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

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

    
399
static void configure_rtc(QemuOpts *opts)
400
{
401
    const char *value;
402

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

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

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

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

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

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

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

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

    
487
    return hci_table[cur_hci++];
488
}
489

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

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

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

    
517
    return 0;
518
}
519

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

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

    
530
    hci = hci_init(str);
531
    if (!hci)
532
        return -1;
533

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

    
542
    hci_table[nb_hcis++] = hci;
543

    
544
    return 0;
545
}
546

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

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

    
555
    bt_vhci_init(bt_new_hci(vlan));
556
}
557

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

    
566
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
567

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

    
576
    vlan = qemu_find_bt_vlan(vlan_id);
577

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

    
582
    if (!strcmp(devname, "keyboard"))
583
        return bt_keyboard_init(vlan);
584

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

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

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

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

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

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

    
628
/***********************************************************/
629
/* QEMU Block devices */
630

    
631
#define HD_OPTS "media=disk"
632
#define CDROM_OPTS "media=cdrom"
633
#define FD_OPTS ""
634
#define PFLASH_OPTS ""
635
#define MTD_OPTS ""
636
#define SD_OPTS ""
637

    
638
static int drive_init_func(QemuOpts *opts, void *opaque)
639
{
640
    int *use_scsi = opaque;
641

    
642
    return drive_init(opts, *use_scsi) == NULL;
643
}
644

    
645
static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
646
{
647
    if (NULL == qemu_opt_get(opts, "snapshot")) {
648
        qemu_opt_set(opts, "snapshot", "on");
649
    }
650
    return 0;
651
}
652

    
653
static void default_drive(int enable, int snapshot, int use_scsi,
654
                          BlockInterfaceType type, int index,
655
                          const char *optstr)
656
{
657
    QemuOpts *opts;
658

    
659
    if (type == IF_DEFAULT) {
660
        type = use_scsi ? IF_SCSI : IF_IDE;
661
    }
662

    
663
    if (!enable || drive_get_by_index(type, index)) {
664
        return;
665
    }
666

    
667
    opts = drive_add(type, index, NULL, optstr);
668
    if (snapshot) {
669
        drive_enable_snapshot(opts, NULL);
670
    }
671
    if (!drive_init(opts, use_scsi)) {
672
        exit(1);
673
    }
674
}
675

    
676
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
677
{
678
    boot_set_handler = func;
679
    boot_set_opaque = opaque;
680
}
681

    
682
int qemu_boot_set(const char *boot_devices)
683
{
684
    if (!boot_set_handler) {
685
        return -EINVAL;
686
    }
687
    return boot_set_handler(boot_set_opaque, boot_devices);
688
}
689

    
690
static void validate_bootdevices(char *devices)
691
{
692
    /* We just do some generic consistency checks */
693
    const char *p;
694
    int bitmap = 0;
695

    
696
    for (p = devices; *p != '\0'; p++) {
697
        /* Allowed boot devices are:
698
         * a-b: floppy disk drives
699
         * c-f: IDE disk drives
700
         * g-m: machine implementation dependant drives
701
         * n-p: network devices
702
         * It's up to each machine implementation to check if the given boot
703
         * devices match the actual hardware implementation and firmware
704
         * features.
705
         */
706
        if (*p < 'a' || *p > 'p') {
707
            fprintf(stderr, "Invalid boot device '%c'\n", *p);
708
            exit(1);
709
        }
710
        if (bitmap & (1 << (*p - 'a'))) {
711
            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
712
            exit(1);
713
        }
714
        bitmap |= 1 << (*p - 'a');
715
    }
716
}
717

    
718
static void restore_boot_devices(void *opaque)
719
{
720
    char *standard_boot_devices = opaque;
721
    static int first = 1;
722

    
723
    /* Restore boot order and remove ourselves after the first boot */
724
    if (first) {
725
        first = 0;
726
        return;
727
    }
728

    
729
    qemu_boot_set(standard_boot_devices);
730

    
731
    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
732
    qemu_free(standard_boot_devices);
733
}
734

    
735
void add_boot_device_path(int32_t bootindex, DeviceState *dev,
736
                          const char *suffix)
737
{
738
    FWBootEntry *node, *i;
739

    
740
    if (bootindex < 0) {
741
        return;
742
    }
743

    
744
    assert(dev != NULL || suffix != NULL);
745

    
746
    node = qemu_mallocz(sizeof(FWBootEntry));
747
    node->bootindex = bootindex;
748
    node->suffix = suffix ? qemu_strdup(suffix) : NULL;
749
    node->dev = dev;
750

    
751
    QTAILQ_FOREACH(i, &fw_boot_order, link) {
752
        if (i->bootindex == bootindex) {
753
            fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
754
            exit(1);
755
        } else if (i->bootindex < bootindex) {
756
            continue;
757
        }
758
        QTAILQ_INSERT_BEFORE(i, node, link);
759
        return;
760
    }
761
    QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
762
}
763

    
764
/*
765
 * This function returns null terminated string that consist of new line
766
 * separated device paths.
767
 *
768
 * memory pointed by "size" is assigned total length of the array in bytes
769
 *
770
 */
771
char *get_boot_devices_list(uint32_t *size)
772
{
773
    FWBootEntry *i;
774
    uint32_t total = 0;
775
    char *list = NULL;
776

    
777
    QTAILQ_FOREACH(i, &fw_boot_order, link) {
778
        char *devpath = NULL, *bootpath;
779
        int len;
780

    
781
        if (i->dev) {
782
            devpath = qdev_get_fw_dev_path(i->dev);
783
            assert(devpath);
784
        }
785

    
786
        if (i->suffix && devpath) {
787
            size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
788

    
789
            bootpath = qemu_malloc(bootpathlen);
790
            snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
791
            qemu_free(devpath);
792
        } else if (devpath) {
793
            bootpath = devpath;
794
        } else {
795
            bootpath = qemu_strdup(i->suffix);
796
            assert(bootpath);
797
        }
798

    
799
        if (total) {
800
            list[total-1] = '\n';
801
        }
802
        len = strlen(bootpath) + 1;
803
        list = qemu_realloc(list, total + len);
804
        memcpy(&list[total], bootpath, len);
805
        total += len;
806
        qemu_free(bootpath);
807
    }
808

    
809
    *size = total;
810

    
811
    return list;
812
}
813

    
814
static void numa_add(const char *optarg)
815
{
816
    char option[128];
817
    char *endptr;
818
    unsigned long long value, endvalue;
819
    int nodenr;
820

    
821
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
822
    if (!strcmp(option, "node")) {
823
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
824
            nodenr = nb_numa_nodes;
825
        } else {
826
            nodenr = strtoull(option, NULL, 10);
827
        }
828

    
829
        if (get_param_value(option, 128, "mem", optarg) == 0) {
830
            node_mem[nodenr] = 0;
831
        } else {
832
            int64_t sval;
833
            sval = strtosz(option, NULL);
834
            if (sval < 0) {
835
                fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
836
                exit(1);
837
            }
838
            node_mem[nodenr] = sval;
839
        }
840
        if (get_param_value(option, 128, "cpus", optarg) == 0) {
841
            node_cpumask[nodenr] = 0;
842
        } else {
843
            value = strtoull(option, &endptr, 10);
844
            if (value >= 64) {
845
                value = 63;
846
                fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
847
            } else {
848
                if (*endptr == '-') {
849
                    endvalue = strtoull(endptr+1, &endptr, 10);
850
                    if (endvalue >= 63) {
851
                        endvalue = 62;
852
                        fprintf(stderr,
853
                            "only 63 CPUs in NUMA mode supported.\n");
854
                    }
855
                    value = (2ULL << endvalue) - (1ULL << value);
856
                } else {
857
                    value = 1ULL << value;
858
                }
859
            }
860
            node_cpumask[nodenr] = value;
861
        }
862
        nb_numa_nodes++;
863
    }
864
    return;
865
}
866

    
867
static void smp_parse(const char *optarg)
868
{
869
    int smp, sockets = 0, threads = 0, cores = 0;
870
    char *endptr;
871
    char option[128];
872

    
873
    smp = strtoul(optarg, &endptr, 10);
874
    if (endptr != optarg) {
875
        if (*endptr == ',') {
876
            endptr++;
877
        }
878
    }
879
    if (get_param_value(option, 128, "sockets", endptr) != 0)
880
        sockets = strtoull(option, NULL, 10);
881
    if (get_param_value(option, 128, "cores", endptr) != 0)
882
        cores = strtoull(option, NULL, 10);
883
    if (get_param_value(option, 128, "threads", endptr) != 0)
884
        threads = strtoull(option, NULL, 10);
885
    if (get_param_value(option, 128, "maxcpus", endptr) != 0)
886
        max_cpus = strtoull(option, NULL, 10);
887

    
888
    /* compute missing values, prefer sockets over cores over threads */
889
    if (smp == 0 || sockets == 0) {
890
        sockets = sockets > 0 ? sockets : 1;
891
        cores = cores > 0 ? cores : 1;
892
        threads = threads > 0 ? threads : 1;
893
        if (smp == 0) {
894
            smp = cores * threads * sockets;
895
        }
896
    } else {
897
        if (cores == 0) {
898
            threads = threads > 0 ? threads : 1;
899
            cores = smp / (sockets * threads);
900
        } else {
901
            threads = smp / (cores * sockets);
902
        }
903
    }
904
    smp_cpus = smp;
905
    smp_cores = cores > 0 ? cores : 1;
906
    smp_threads = threads > 0 ? threads : 1;
907
    if (max_cpus == 0)
908
        max_cpus = smp_cpus;
909
}
910

    
911
/***********************************************************/
912
/* USB devices */
913

    
914
static int usb_device_add(const char *devname)
915
{
916
    const char *p;
917
    USBDevice *dev = NULL;
918

    
919
    if (!usb_enabled)
920
        return -1;
921

    
922
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
923
    dev = usbdevice_create(devname);
924
    if (dev)
925
        goto done;
926

    
927
    /* the other ones */
928
#ifndef CONFIG_LINUX
929
    /* only the linux version is qdev-ified, usb-bsd still needs this */
930
    if (strstart(devname, "host:", &p)) {
931
        dev = usb_host_device_open(p);
932
    } else
933
#endif
934
    if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
935
        dev = usb_bt_init(devname[2] ? hci_init(p) :
936
                        bt_new_hci(qemu_find_bt_vlan(0)));
937
    } else {
938
        return -1;
939
    }
940
    if (!dev)
941
        return -1;
942

    
943
done:
944
    return 0;
945
}
946

    
947
static int usb_device_del(const char *devname)
948
{
949
    int bus_num, addr;
950
    const char *p;
951

    
952
    if (strstart(devname, "host:", &p))
953
        return usb_host_device_close(p);
954

    
955
    if (!usb_enabled)
956
        return -1;
957

    
958
    p = strchr(devname, '.');
959
    if (!p)
960
        return -1;
961
    bus_num = strtoul(devname, NULL, 0);
962
    addr = strtoul(p + 1, NULL, 0);
963

    
964
    return usb_device_delete_addr(bus_num, addr);
965
}
966

    
967
static int usb_parse(const char *cmdline)
968
{
969
    int r;
970
    r = usb_device_add(cmdline);
971
    if (r < 0) {
972
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
973
    }
974
    return r;
975
}
976

    
977
void do_usb_add(Monitor *mon, const QDict *qdict)
978
{
979
    const char *devname = qdict_get_str(qdict, "devname");
980
    if (usb_device_add(devname) < 0) {
981
        error_report("could not add USB device '%s'", devname);
982
    }
983
}
984

    
985
void do_usb_del(Monitor *mon, const QDict *qdict)
986
{
987
    const char *devname = qdict_get_str(qdict, "devname");
988
    if (usb_device_del(devname) < 0) {
989
        error_report("could not delete USB device '%s'", devname);
990
    }
991
}
992

    
993
/***********************************************************/
994
/* PCMCIA/Cardbus */
995

    
996
static struct pcmcia_socket_entry_s {
997
    PCMCIASocket *socket;
998
    struct pcmcia_socket_entry_s *next;
999
} *pcmcia_sockets = 0;
1000

    
1001
void pcmcia_socket_register(PCMCIASocket *socket)
1002
{
1003
    struct pcmcia_socket_entry_s *entry;
1004

    
1005
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1006
    entry->socket = socket;
1007
    entry->next = pcmcia_sockets;
1008
    pcmcia_sockets = entry;
1009
}
1010

    
1011
void pcmcia_socket_unregister(PCMCIASocket *socket)
1012
{
1013
    struct pcmcia_socket_entry_s *entry, **ptr;
1014

    
1015
    ptr = &pcmcia_sockets;
1016
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1017
        if (entry->socket == socket) {
1018
            *ptr = entry->next;
1019
            qemu_free(entry);
1020
        }
1021
}
1022

    
1023
void pcmcia_info(Monitor *mon)
1024
{
1025
    struct pcmcia_socket_entry_s *iter;
1026

    
1027
    if (!pcmcia_sockets)
1028
        monitor_printf(mon, "No PCMCIA sockets\n");
1029

    
1030
    for (iter = pcmcia_sockets; iter; iter = iter->next)
1031
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1032
                       iter->socket->attached ? iter->socket->card_string :
1033
                       "Empty");
1034
}
1035

    
1036
/***********************************************************/
1037
/* machine registration */
1038

    
1039
static QEMUMachine *first_machine = NULL;
1040
QEMUMachine *current_machine = NULL;
1041

    
1042
int qemu_register_machine(QEMUMachine *m)
1043
{
1044
    QEMUMachine **pm;
1045
    pm = &first_machine;
1046
    while (*pm != NULL)
1047
        pm = &(*pm)->next;
1048
    m->next = NULL;
1049
    *pm = m;
1050
    return 0;
1051
}
1052

    
1053
static QEMUMachine *find_machine(const char *name)
1054
{
1055
    QEMUMachine *m;
1056

    
1057
    for(m = first_machine; m != NULL; m = m->next) {
1058
        if (!strcmp(m->name, name))
1059
            return m;
1060
        if (m->alias && !strcmp(m->alias, name))
1061
            return m;
1062
    }
1063
    return NULL;
1064
}
1065

    
1066
static QEMUMachine *find_default_machine(void)
1067
{
1068
    QEMUMachine *m;
1069

    
1070
    for(m = first_machine; m != NULL; m = m->next) {
1071
        if (m->is_default) {
1072
            return m;
1073
        }
1074
    }
1075
    return NULL;
1076
}
1077

    
1078
/***********************************************************/
1079
/* main execution loop */
1080

    
1081
static void gui_update(void *opaque)
1082
{
1083
    uint64_t interval = GUI_REFRESH_INTERVAL;
1084
    DisplayState *ds = opaque;
1085
    DisplayChangeListener *dcl = ds->listeners;
1086

    
1087
    qemu_flush_coalesced_mmio_buffer();
1088
    dpy_refresh(ds);
1089

    
1090
    while (dcl != NULL) {
1091
        if (dcl->gui_timer_interval &&
1092
            dcl->gui_timer_interval < interval)
1093
            interval = dcl->gui_timer_interval;
1094
        dcl = dcl->next;
1095
    }
1096
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1097
}
1098

    
1099
static void nographic_update(void *opaque)
1100
{
1101
    uint64_t interval = GUI_REFRESH_INTERVAL;
1102

    
1103
    qemu_flush_coalesced_mmio_buffer();
1104
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1105
}
1106

    
1107
struct vm_change_state_entry {
1108
    VMChangeStateHandler *cb;
1109
    void *opaque;
1110
    QLIST_ENTRY (vm_change_state_entry) entries;
1111
};
1112

    
1113
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1114

    
1115
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1116
                                                     void *opaque)
1117
{
1118
    VMChangeStateEntry *e;
1119

    
1120
    e = qemu_mallocz(sizeof (*e));
1121

    
1122
    e->cb = cb;
1123
    e->opaque = opaque;
1124
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1125
    return e;
1126
}
1127

    
1128
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1129
{
1130
    QLIST_REMOVE (e, entries);
1131
    qemu_free (e);
1132
}
1133

    
1134
void vm_state_notify(int running, int reason)
1135
{
1136
    VMChangeStateEntry *e;
1137

    
1138
    trace_vm_state_notify(running, reason);
1139

    
1140
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1141
        e->cb(e->opaque, running, reason);
1142
    }
1143
}
1144

    
1145
void vm_start(void)
1146
{
1147
    if (!vm_running) {
1148
        cpu_enable_ticks();
1149
        vm_running = 1;
1150
        vm_state_notify(1, 0);
1151
        resume_all_vcpus();
1152
        monitor_protocol_event(QEVENT_RESUME, NULL);
1153
    }
1154
}
1155

    
1156
/* reset/shutdown handler */
1157

    
1158
typedef struct QEMUResetEntry {
1159
    QTAILQ_ENTRY(QEMUResetEntry) entry;
1160
    QEMUResetHandler *func;
1161
    void *opaque;
1162
} QEMUResetEntry;
1163

    
1164
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1165
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
1166
static int reset_requested;
1167
static int shutdown_requested, shutdown_signal = -1;
1168
static pid_t shutdown_pid;
1169
static int powerdown_requested;
1170
static int debug_requested;
1171
static int vmstop_requested;
1172

    
1173
int qemu_shutdown_requested_get(void)
1174
{
1175
    return shutdown_requested;
1176
}
1177

    
1178
int qemu_reset_requested_get(void)
1179
{
1180
    return reset_requested;
1181
}
1182

    
1183
int qemu_shutdown_requested(void)
1184
{
1185
    int r = shutdown_requested;
1186
    shutdown_requested = 0;
1187
    return r;
1188
}
1189

    
1190
void qemu_kill_report(void)
1191
{
1192
    if (shutdown_signal != -1) {
1193
        fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1194
        if (shutdown_pid == 0) {
1195
            /* This happens for eg ^C at the terminal, so it's worth
1196
             * avoiding printing an odd message in that case.
1197
             */
1198
            fputc('\n', stderr);
1199
        } else {
1200
            fprintf(stderr, " from pid %d\n", shutdown_pid);
1201
        }
1202
        shutdown_signal = -1;
1203
    }
1204
}
1205

    
1206
int qemu_reset_requested(void)
1207
{
1208
    int r = reset_requested;
1209
    reset_requested = 0;
1210
    return r;
1211
}
1212

    
1213
int qemu_powerdown_requested(void)
1214
{
1215
    int r = powerdown_requested;
1216
    powerdown_requested = 0;
1217
    return r;
1218
}
1219

    
1220
static int qemu_debug_requested(void)
1221
{
1222
    int r = debug_requested;
1223
    debug_requested = 0;
1224
    return r;
1225
}
1226

    
1227
static int qemu_vmstop_requested(void)
1228
{
1229
    int r = vmstop_requested;
1230
    vmstop_requested = 0;
1231
    return r;
1232
}
1233

    
1234
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1235
{
1236
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1237

    
1238
    re->func = func;
1239
    re->opaque = opaque;
1240
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1241
}
1242

    
1243
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1244
{
1245
    QEMUResetEntry *re;
1246

    
1247
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
1248
        if (re->func == func && re->opaque == opaque) {
1249
            QTAILQ_REMOVE(&reset_handlers, re, entry);
1250
            qemu_free(re);
1251
            return;
1252
        }
1253
    }
1254
}
1255

    
1256
void qemu_system_reset(void)
1257
{
1258
    QEMUResetEntry *re, *nre;
1259

    
1260
    /* reset all devices */
1261
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1262
        re->func(re->opaque);
1263
    }
1264
    monitor_protocol_event(QEVENT_RESET, NULL);
1265
    cpu_synchronize_all_post_reset();
1266
}
1267

    
1268
void qemu_system_reset_request(void)
1269
{
1270
    if (no_reboot) {
1271
        shutdown_requested = 1;
1272
    } else {
1273
        reset_requested = 1;
1274
    }
1275
    cpu_stop_current();
1276
    qemu_notify_event();
1277
}
1278

    
1279
void qemu_system_killed(int signal, pid_t pid)
1280
{
1281
    shutdown_signal = signal;
1282
    shutdown_pid = pid;
1283
    qemu_system_shutdown_request();
1284
}
1285

    
1286
void qemu_system_shutdown_request(void)
1287
{
1288
    shutdown_requested = 1;
1289
    qemu_notify_event();
1290
}
1291

    
1292
void qemu_system_powerdown_request(void)
1293
{
1294
    powerdown_requested = 1;
1295
    qemu_notify_event();
1296
}
1297

    
1298
void qemu_system_debug_request(void)
1299
{
1300
    debug_requested = 1;
1301
    qemu_notify_event();
1302
}
1303

    
1304
void qemu_system_vmstop_request(int reason)
1305
{
1306
    vmstop_requested = reason;
1307
    qemu_notify_event();
1308
}
1309

    
1310
void main_loop_wait(int nonblocking)
1311
{
1312
    fd_set rfds, wfds, xfds;
1313
    int ret, nfds;
1314
    struct timeval tv;
1315
    int timeout;
1316

    
1317
    if (nonblocking)
1318
        timeout = 0;
1319
    else {
1320
        timeout = qemu_calculate_timeout();
1321
        qemu_bh_update_timeout(&timeout);
1322
    }
1323

    
1324
    os_host_main_loop_wait(&timeout);
1325

    
1326
    tv.tv_sec = timeout / 1000;
1327
    tv.tv_usec = (timeout % 1000) * 1000;
1328

    
1329
    /* poll any events */
1330
    /* XXX: separate device handlers from system ones */
1331
    nfds = -1;
1332
    FD_ZERO(&rfds);
1333
    FD_ZERO(&wfds);
1334
    FD_ZERO(&xfds);
1335
    qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1336
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1337

    
1338
    qemu_mutex_unlock_iothread();
1339
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1340
    qemu_mutex_lock_iothread();
1341

    
1342
    qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1343
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1344

    
1345
    qemu_run_all_timers();
1346

    
1347
    /* Check bottom-halves last in case any of the earlier events triggered
1348
       them.  */
1349
    qemu_bh_poll();
1350

    
1351
}
1352

    
1353
#ifndef CONFIG_IOTHREAD
1354
static int vm_request_pending(void)
1355
{
1356
    return powerdown_requested ||
1357
           reset_requested ||
1358
           shutdown_requested ||
1359
           debug_requested ||
1360
           vmstop_requested;
1361
}
1362
#endif
1363

    
1364
qemu_irq qemu_system_powerdown;
1365

    
1366
static void main_loop(void)
1367
{
1368
    bool nonblocking = false;
1369
#ifdef CONFIG_PROFILER
1370
    int64_t ti;
1371
#endif
1372
    int r;
1373

    
1374
    qemu_main_loop_start();
1375

    
1376
    for (;;) {
1377
#ifndef CONFIG_IOTHREAD
1378
        nonblocking = cpu_exec_all();
1379
        if (vm_request_pending()) {
1380
            nonblocking = true;
1381
        }
1382
#endif
1383
#ifdef CONFIG_PROFILER
1384
        ti = profile_getclock();
1385
#endif
1386
        main_loop_wait(nonblocking);
1387
#ifdef CONFIG_PROFILER
1388
        dev_time += profile_getclock() - ti;
1389
#endif
1390

    
1391
        if (qemu_debug_requested()) {
1392
            vm_stop(VMSTOP_DEBUG);
1393
        }
1394
        if (qemu_shutdown_requested()) {
1395
            qemu_kill_report();
1396
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1397
            if (no_shutdown) {
1398
                vm_stop(VMSTOP_SHUTDOWN);
1399
                no_shutdown = 0;
1400
            } else
1401
                break;
1402
        }
1403
        if (qemu_reset_requested()) {
1404
            pause_all_vcpus();
1405
            cpu_synchronize_all_states();
1406
            qemu_system_reset();
1407
            resume_all_vcpus();
1408
        }
1409
        if (qemu_powerdown_requested()) {
1410
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1411
            qemu_irq_raise(qemu_system_powerdown);
1412
        }
1413
        if ((r = qemu_vmstop_requested())) {
1414
            vm_stop(r);
1415
        }
1416
    }
1417
    bdrv_close_all();
1418
    pause_all_vcpus();
1419
}
1420

    
1421
static void version(void)
1422
{
1423
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1424
}
1425

    
1426
static void help(int exitcode)
1427
{
1428
    const char *options_help =
1429
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1430
        opt_help
1431
#define DEFHEADING(text) stringify(text) "\n"
1432
#include "qemu-options.def"
1433
#undef DEF
1434
#undef DEFHEADING
1435
#undef GEN_DOCS
1436
        ;
1437
    version();
1438
    printf("usage: %s [options] [disk_image]\n"
1439
           "\n"
1440
           "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1441
           "\n"
1442
           "%s\n"
1443
           "During emulation, the following keys are useful:\n"
1444
           "ctrl-alt-f      toggle full screen\n"
1445
           "ctrl-alt-n      switch to virtual console 'n'\n"
1446
           "ctrl-alt        toggle mouse and keyboard grab\n"
1447
           "\n"
1448
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
1449
           "qemu",
1450
           options_help);
1451
    exit(exitcode);
1452
}
1453

    
1454
#define HAS_ARG 0x0001
1455

    
1456
typedef struct QEMUOption {
1457
    const char *name;
1458
    int flags;
1459
    int index;
1460
    uint32_t arch_mask;
1461
} QEMUOption;
1462

    
1463
static const QEMUOption qemu_options[] = {
1464
    { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1465
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1466
    { option, opt_arg, opt_enum, arch_mask },
1467
#define DEFHEADING(text)
1468
#include "qemu-options.def"
1469
#undef DEF
1470
#undef DEFHEADING
1471
#undef GEN_DOCS
1472
    { NULL },
1473
};
1474
static void select_vgahw (const char *p)
1475
{
1476
    const char *opts;
1477

    
1478
    default_vga = 0;
1479
    vga_interface_type = VGA_NONE;
1480
    if (strstart(p, "std", &opts)) {
1481
        vga_interface_type = VGA_STD;
1482
    } else if (strstart(p, "cirrus", &opts)) {
1483
        vga_interface_type = VGA_CIRRUS;
1484
    } else if (strstart(p, "vmware", &opts)) {
1485
        vga_interface_type = VGA_VMWARE;
1486
    } else if (strstart(p, "xenfb", &opts)) {
1487
        vga_interface_type = VGA_XENFB;
1488
    } else if (strstart(p, "qxl", &opts)) {
1489
        vga_interface_type = VGA_QXL;
1490
    } else if (!strstart(p, "none", &opts)) {
1491
    invalid_vga:
1492
        fprintf(stderr, "Unknown vga type: %s\n", p);
1493
        exit(1);
1494
    }
1495
    while (*opts) {
1496
        const char *nextopt;
1497

    
1498
        if (strstart(opts, ",retrace=", &nextopt)) {
1499
            opts = nextopt;
1500
            if (strstart(opts, "dumb", &nextopt))
1501
                vga_retrace_method = VGA_RETRACE_DUMB;
1502
            else if (strstart(opts, "precise", &nextopt))
1503
                vga_retrace_method = VGA_RETRACE_PRECISE;
1504
            else goto invalid_vga;
1505
        } else goto invalid_vga;
1506
        opts = nextopt;
1507
    }
1508
}
1509

    
1510
static DisplayType select_display(const char *p)
1511
{
1512
    const char *opts;
1513
    DisplayType display = DT_DEFAULT;
1514

    
1515
    if (strstart(p, "sdl", &opts)) {
1516
#ifdef CONFIG_SDL
1517
        display = DT_SDL;
1518
        while (*opts) {
1519
            const char *nextopt;
1520

    
1521
            if (strstart(opts, ",frame=", &nextopt)) {
1522
                opts = nextopt;
1523
                if (strstart(opts, "on", &nextopt)) {
1524
                    no_frame = 0;
1525
                } else if (strstart(opts, "off", &nextopt)) {
1526
                    no_frame = 1;
1527
                } else {
1528
                    goto invalid_sdl_args;
1529
                }
1530
            } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1531
                opts = nextopt;
1532
                if (strstart(opts, "on", &nextopt)) {
1533
                    alt_grab = 1;
1534
                } else if (strstart(opts, "off", &nextopt)) {
1535
                    alt_grab = 0;
1536
                } else {
1537
                    goto invalid_sdl_args;
1538
                }
1539
            } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1540
                opts = nextopt;
1541
                if (strstart(opts, "on", &nextopt)) {
1542
                    ctrl_grab = 1;
1543
                } else if (strstart(opts, "off", &nextopt)) {
1544
                    ctrl_grab = 0;
1545
                } else {
1546
                    goto invalid_sdl_args;
1547
                }
1548
            } else if (strstart(opts, ",window_close=", &nextopt)) {
1549
                opts = nextopt;
1550
                if (strstart(opts, "on", &nextopt)) {
1551
                    no_quit = 0;
1552
                } else if (strstart(opts, "off", &nextopt)) {
1553
                    no_quit = 1;
1554
                } else {
1555
                    goto invalid_sdl_args;
1556
                }
1557
            } else {
1558
            invalid_sdl_args:
1559
                fprintf(stderr, "Invalid SDL option string: %s\n", p);
1560
                exit(1);
1561
            }
1562
            opts = nextopt;
1563
        }
1564
#else
1565
        fprintf(stderr, "SDL support is disabled\n");
1566
        exit(1);
1567
#endif
1568
    } else if (strstart(p, "vnc", &opts)) {
1569
#ifdef CONFIG_VNC
1570
        display_remote++;
1571

    
1572
        if (*opts) {
1573
            const char *nextopt;
1574

    
1575
            if (strstart(opts, "=", &nextopt)) {
1576
                vnc_display = nextopt;
1577
            }
1578
        }
1579
        if (!vnc_display) {
1580
            fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1581
            exit(1);
1582
        }
1583
#else
1584
        fprintf(stderr, "VNC support is disabled\n");
1585
        exit(1);
1586
#endif
1587
    } else if (strstart(p, "curses", &opts)) {
1588
#ifdef CONFIG_CURSES
1589
        display = DT_CURSES;
1590
#else
1591
        fprintf(stderr, "Curses support is disabled\n");
1592
        exit(1);
1593
#endif
1594
    } else if (strstart(p, "none", &opts)) {
1595
        display = DT_NONE;
1596
    } else {
1597
        fprintf(stderr, "Unknown display type: %s\n", p);
1598
        exit(1);
1599
    }
1600

    
1601
    return display;
1602
}
1603

    
1604
static int balloon_parse(const char *arg)
1605
{
1606
    QemuOpts *opts;
1607

    
1608
    if (strcmp(arg, "none") == 0) {
1609
        return 0;
1610
    }
1611

    
1612
    if (!strncmp(arg, "virtio", 6)) {
1613
        if (arg[6] == ',') {
1614
            /* have params -> parse them */
1615
            opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1616
            if (!opts)
1617
                return  -1;
1618
        } else {
1619
            /* create empty opts */
1620
            opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1621
        }
1622
        qemu_opt_set(opts, "driver", "virtio-balloon");
1623
        return 0;
1624
    }
1625

    
1626
    return -1;
1627
}
1628

    
1629
char *qemu_find_file(int type, const char *name)
1630
{
1631
    int len;
1632
    const char *subdir;
1633
    char *buf;
1634

    
1635
    /* If name contains path separators then try it as a straight path.  */
1636
    if ((strchr(name, '/') || strchr(name, '\\'))
1637
        && access(name, R_OK) == 0) {
1638
        return qemu_strdup(name);
1639
    }
1640
    switch (type) {
1641
    case QEMU_FILE_TYPE_BIOS:
1642
        subdir = "";
1643
        break;
1644
    case QEMU_FILE_TYPE_KEYMAP:
1645
        subdir = "keymaps/";
1646
        break;
1647
    default:
1648
        abort();
1649
    }
1650
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1651
    buf = qemu_mallocz(len);
1652
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1653
    if (access(buf, R_OK)) {
1654
        qemu_free(buf);
1655
        return NULL;
1656
    }
1657
    return buf;
1658
}
1659

    
1660
static int device_help_func(QemuOpts *opts, void *opaque)
1661
{
1662
    return qdev_device_help(opts);
1663
}
1664

    
1665
static int device_init_func(QemuOpts *opts, void *opaque)
1666
{
1667
    DeviceState *dev;
1668

    
1669
    dev = qdev_device_add(opts);
1670
    if (!dev)
1671
        return -1;
1672
    return 0;
1673
}
1674

    
1675
static int chardev_init_func(QemuOpts *opts, void *opaque)
1676
{
1677
    CharDriverState *chr;
1678

    
1679
    chr = qemu_chr_open_opts(opts, NULL);
1680
    if (!chr)
1681
        return -1;
1682
    return 0;
1683
}
1684

    
1685
#ifdef CONFIG_VIRTFS
1686
static int fsdev_init_func(QemuOpts *opts, void *opaque)
1687
{
1688
    int ret;
1689
    ret = qemu_fsdev_add(opts);
1690

    
1691
    return ret;
1692
}
1693
#endif
1694

    
1695
static int mon_init_func(QemuOpts *opts, void *opaque)
1696
{
1697
    CharDriverState *chr;
1698
    const char *chardev;
1699
    const char *mode;
1700
    int flags;
1701

    
1702
    mode = qemu_opt_get(opts, "mode");
1703
    if (mode == NULL) {
1704
        mode = "readline";
1705
    }
1706
    if (strcmp(mode, "readline") == 0) {
1707
        flags = MONITOR_USE_READLINE;
1708
    } else if (strcmp(mode, "control") == 0) {
1709
        flags = MONITOR_USE_CONTROL;
1710
    } else {
1711
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1712
        exit(1);
1713
    }
1714

    
1715
    if (qemu_opt_get_bool(opts, "pretty", 0))
1716
        flags |= MONITOR_USE_PRETTY;
1717

    
1718
    if (qemu_opt_get_bool(opts, "default", 0))
1719
        flags |= MONITOR_IS_DEFAULT;
1720

    
1721
    chardev = qemu_opt_get(opts, "chardev");
1722
    chr = qemu_chr_find(chardev);
1723
    if (chr == NULL) {
1724
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1725
        exit(1);
1726
    }
1727

    
1728
    monitor_init(chr, flags);
1729
    return 0;
1730
}
1731

    
1732
static void monitor_parse(const char *optarg, const char *mode)
1733
{
1734
    static int monitor_device_index = 0;
1735
    QemuOpts *opts;
1736
    const char *p;
1737
    char label[32];
1738
    int def = 0;
1739

    
1740
    if (strstart(optarg, "chardev:", &p)) {
1741
        snprintf(label, sizeof(label), "%s", p);
1742
    } else {
1743
        snprintf(label, sizeof(label), "compat_monitor%d",
1744
                 monitor_device_index);
1745
        if (monitor_device_index == 0) {
1746
            def = 1;
1747
        }
1748
        opts = qemu_chr_parse_compat(label, optarg);
1749
        if (!opts) {
1750
            fprintf(stderr, "parse error: %s\n", optarg);
1751
            exit(1);
1752
        }
1753
    }
1754

    
1755
    opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1756
    if (!opts) {
1757
        fprintf(stderr, "duplicate chardev: %s\n", label);
1758
        exit(1);
1759
    }
1760
    qemu_opt_set(opts, "mode", mode);
1761
    qemu_opt_set(opts, "chardev", label);
1762
    if (def)
1763
        qemu_opt_set(opts, "default", "on");
1764
    monitor_device_index++;
1765
}
1766

    
1767
struct device_config {
1768
    enum {
1769
        DEV_USB,       /* -usbdevice     */
1770
        DEV_BT,        /* -bt            */
1771
        DEV_SERIAL,    /* -serial        */
1772
        DEV_PARALLEL,  /* -parallel      */
1773
        DEV_VIRTCON,   /* -virtioconsole */
1774
        DEV_DEBUGCON,  /* -debugcon */
1775
    } type;
1776
    const char *cmdline;
1777
    QTAILQ_ENTRY(device_config) next;
1778
};
1779
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1780

    
1781
static void add_device_config(int type, const char *cmdline)
1782
{
1783
    struct device_config *conf;
1784

    
1785
    conf = qemu_mallocz(sizeof(*conf));
1786
    conf->type = type;
1787
    conf->cmdline = cmdline;
1788
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1789
}
1790

    
1791
static int foreach_device_config(int type, int (*func)(const char *cmdline))
1792
{
1793
    struct device_config *conf;
1794
    int rc;
1795

    
1796
    QTAILQ_FOREACH(conf, &device_configs, next) {
1797
        if (conf->type != type)
1798
            continue;
1799
        rc = func(conf->cmdline);
1800
        if (0 != rc)
1801
            return rc;
1802
    }
1803
    return 0;
1804
}
1805

    
1806
static int serial_parse(const char *devname)
1807
{
1808
    static int index = 0;
1809
    char label[32];
1810

    
1811
    if (strcmp(devname, "none") == 0)
1812
        return 0;
1813
    if (index == MAX_SERIAL_PORTS) {
1814
        fprintf(stderr, "qemu: too many serial ports\n");
1815
        exit(1);
1816
    }
1817
    snprintf(label, sizeof(label), "serial%d", index);
1818
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
1819
    if (!serial_hds[index]) {
1820
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1821
                devname, strerror(errno));
1822
        return -1;
1823
    }
1824
    index++;
1825
    return 0;
1826
}
1827

    
1828
static int parallel_parse(const char *devname)
1829
{
1830
    static int index = 0;
1831
    char label[32];
1832

    
1833
    if (strcmp(devname, "none") == 0)
1834
        return 0;
1835
    if (index == MAX_PARALLEL_PORTS) {
1836
        fprintf(stderr, "qemu: too many parallel ports\n");
1837
        exit(1);
1838
    }
1839
    snprintf(label, sizeof(label), "parallel%d", index);
1840
    parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1841
    if (!parallel_hds[index]) {
1842
        fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1843
                devname, strerror(errno));
1844
        return -1;
1845
    }
1846
    index++;
1847
    return 0;
1848
}
1849

    
1850
static int virtcon_parse(const char *devname)
1851
{
1852
    QemuOptsList *device = qemu_find_opts("device");
1853
    static int index = 0;
1854
    char label[32];
1855
    QemuOpts *bus_opts, *dev_opts;
1856

    
1857
    if (strcmp(devname, "none") == 0)
1858
        return 0;
1859
    if (index == MAX_VIRTIO_CONSOLES) {
1860
        fprintf(stderr, "qemu: too many virtio consoles\n");
1861
        exit(1);
1862
    }
1863

    
1864
    bus_opts = qemu_opts_create(device, NULL, 0);
1865
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
1866

    
1867
    dev_opts = qemu_opts_create(device, NULL, 0);
1868
    qemu_opt_set(dev_opts, "driver", "virtconsole");
1869

    
1870
    snprintf(label, sizeof(label), "virtcon%d", index);
1871
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1872
    if (!virtcon_hds[index]) {
1873
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1874
                devname, strerror(errno));
1875
        return -1;
1876
    }
1877
    qemu_opt_set(dev_opts, "chardev", label);
1878

    
1879
    index++;
1880
    return 0;
1881
}
1882

    
1883
static int debugcon_parse(const char *devname)
1884
{   
1885
    QemuOpts *opts;
1886

    
1887
    if (!qemu_chr_open("debugcon", devname, NULL)) {
1888
        exit(1);
1889
    }
1890
    opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1891
    if (!opts) {
1892
        fprintf(stderr, "qemu: already have a debugcon device\n");
1893
        exit(1);
1894
    }
1895
    qemu_opt_set(opts, "driver", "isa-debugcon");
1896
    qemu_opt_set(opts, "chardev", "debugcon");
1897
    return 0;
1898
}
1899

    
1900
static int tcg_init(void)
1901
{
1902
    return 0;
1903
}
1904

    
1905
static struct {
1906
    const char *opt_name;
1907
    const char *name;
1908
    int (*available)(void);
1909
    int (*init)(void);
1910
    int *allowed;
1911
} accel_list[] = {
1912
    { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
1913
    { "xen", "Xen", xen_available, xen_init, &xen_allowed },
1914
    { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1915
};
1916

    
1917
static int configure_accelerator(void)
1918
{
1919
    const char *p = NULL;
1920
    char buf[10];
1921
    int i, ret;
1922
    bool accel_initalised = 0;
1923
    bool init_failed = 0;
1924

    
1925
    QemuOptsList *list = qemu_find_opts("machine");
1926
    if (!QTAILQ_EMPTY(&list->head)) {
1927
        p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1928
    }
1929

    
1930
    if (p == NULL) {
1931
        /* Use the default "accelerator", tcg */
1932
        p = "tcg";
1933
    }
1934

    
1935
    while (!accel_initalised && *p != '\0') {
1936
        if (*p == ':') {
1937
            p++;
1938
        }
1939
        p = get_opt_name(buf, sizeof (buf), p, ':');
1940
        for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1941
            if (strcmp(accel_list[i].opt_name, buf) == 0) {
1942
                *(accel_list[i].allowed) = 1;
1943
                ret = accel_list[i].init();
1944
                if (ret < 0) {
1945
                    init_failed = 1;
1946
                    if (!accel_list[i].available()) {
1947
                        printf("%s not supported for this target\n",
1948
                               accel_list[i].name);
1949
                    } else {
1950
                        fprintf(stderr, "failed to initialize %s: %s\n",
1951
                                accel_list[i].name,
1952
                                strerror(-ret));
1953
                    }
1954
                    *(accel_list[i].allowed) = 0;
1955
                } else {
1956
                    accel_initalised = 1;
1957
                }
1958
                break;
1959
            }
1960
        }
1961
        if (i == ARRAY_SIZE(accel_list)) {
1962
            fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
1963
        }
1964
    }
1965

    
1966
    if (!accel_initalised) {
1967
        fprintf(stderr, "No accelerator found!\n");
1968
        exit(1);
1969
    }
1970

    
1971
    if (init_failed) {
1972
        fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
1973
    }
1974

    
1975
    return !accel_initalised;
1976
}
1977

    
1978
void qemu_add_exit_notifier(Notifier *notify)
1979
{
1980
    notifier_list_add(&exit_notifiers, notify);
1981
}
1982

    
1983
void qemu_remove_exit_notifier(Notifier *notify)
1984
{
1985
    notifier_list_remove(&exit_notifiers, notify);
1986
}
1987

    
1988
static void qemu_run_exit_notifiers(void)
1989
{
1990
    notifier_list_notify(&exit_notifiers);
1991
}
1992

    
1993
void qemu_add_machine_init_done_notifier(Notifier *notify)
1994
{
1995
    notifier_list_add(&machine_init_done_notifiers, notify);
1996
}
1997

    
1998
static void qemu_run_machine_init_done_notifiers(void)
1999
{
2000
    notifier_list_notify(&machine_init_done_notifiers);
2001
}
2002

    
2003
static const QEMUOption *lookup_opt(int argc, char **argv,
2004
                                    const char **poptarg, int *poptind)
2005
{
2006
    const QEMUOption *popt;
2007
    int optind = *poptind;
2008
    char *r = argv[optind];
2009
    const char *optarg;
2010

    
2011
    loc_set_cmdline(argv, optind, 1);
2012
    optind++;
2013
    /* Treat --foo the same as -foo.  */
2014
    if (r[1] == '-')
2015
        r++;
2016
    popt = qemu_options;
2017
    for(;;) {
2018
        if (!popt->name) {
2019
            error_report("invalid option");
2020
            exit(1);
2021
        }
2022
        if (!strcmp(popt->name, r + 1))
2023
            break;
2024
        popt++;
2025
    }
2026
    if (popt->flags & HAS_ARG) {
2027
        if (optind >= argc) {
2028
            error_report("requires an argument");
2029
            exit(1);
2030
        }
2031
        optarg = argv[optind++];
2032
        loc_set_cmdline(argv, optind - 2, 2);
2033
    } else {
2034
        optarg = NULL;
2035
    }
2036

    
2037
    *poptarg = optarg;
2038
    *poptind = optind;
2039

    
2040
    return popt;
2041
}
2042

    
2043
int main(int argc, char **argv, char **envp)
2044
{
2045
    const char *gdbstub_dev = NULL;
2046
    int i;
2047
    int snapshot, linux_boot;
2048
    const char *icount_option = NULL;
2049
    const char *initrd_filename;
2050
    const char *kernel_filename, *kernel_cmdline;
2051
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
2052
    DisplayState *ds;
2053
    DisplayChangeListener *dcl;
2054
    int cyls, heads, secs, translation;
2055
    QemuOpts *hda_opts = NULL, *opts;
2056
    QemuOptsList *olist;
2057
    int optind;
2058
    const char *optarg;
2059
    const char *loadvm = NULL;
2060
    QEMUMachine *machine;
2061
    const char *cpu_model;
2062
    int tb_size;
2063
    const char *pid_file = NULL;
2064
    const char *incoming = NULL;
2065
#ifdef CONFIG_VNC
2066
    int show_vnc_port = 0;
2067
#endif
2068
    int defconfig = 1;
2069
    const char *trace_file = NULL;
2070

    
2071
    atexit(qemu_run_exit_notifiers);
2072
    error_set_progname(argv[0]);
2073

    
2074
    init_clocks();
2075

    
2076
    qemu_cache_utils_init(envp);
2077

    
2078
    QLIST_INIT (&vm_change_state_head);
2079
    os_setup_early_signal_handling();
2080

    
2081
    module_call_init(MODULE_INIT_MACHINE);
2082
    machine = find_default_machine();
2083
    cpu_model = NULL;
2084
    initrd_filename = NULL;
2085
    ram_size = 0;
2086
    snapshot = 0;
2087
    kernel_filename = NULL;
2088
    kernel_cmdline = "";
2089
    cyls = heads = secs = 0;
2090
    translation = BIOS_ATA_TRANSLATION_AUTO;
2091

    
2092
    for (i = 0; i < MAX_NODES; i++) {
2093
        node_mem[i] = 0;
2094
        node_cpumask[i] = 0;
2095
    }
2096

    
2097
    nb_numa_nodes = 0;
2098
    nb_nics = 0;
2099

    
2100
    tb_size = 0;
2101
    autostart= 1;
2102

    
2103
    /* first pass of option parsing */
2104
    optind = 1;
2105
    while (optind < argc) {
2106
        if (argv[optind][0] != '-') {
2107
            /* disk image */
2108
            optind++;
2109
            continue;
2110
        } else {
2111
            const QEMUOption *popt;
2112

    
2113
            popt = lookup_opt(argc, argv, &optarg, &optind);
2114
            switch (popt->index) {
2115
            case QEMU_OPTION_nodefconfig:
2116
                defconfig=0;
2117
                break;
2118
            }
2119
        }
2120
    }
2121

    
2122
    if (defconfig) {
2123
        int ret;
2124

    
2125
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2126
        if (ret < 0 && ret != -ENOENT) {
2127
            exit(1);
2128
        }
2129

    
2130
        ret = qemu_read_config_file(arch_config_name);
2131
        if (ret < 0 && ret != -ENOENT) {
2132
            exit(1);
2133
        }
2134
    }
2135
    cpudef_init();
2136

    
2137
    /* second pass of option parsing */
2138
    optind = 1;
2139
    for(;;) {
2140
        if (optind >= argc)
2141
            break;
2142
        if (argv[optind][0] != '-') {
2143
            hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2144
        } else {
2145
            const QEMUOption *popt;
2146

    
2147
            popt = lookup_opt(argc, argv, &optarg, &optind);
2148
            if (!(popt->arch_mask & arch_type)) {
2149
                printf("Option %s not supported for this target\n", popt->name);
2150
                exit(1);
2151
            }
2152
            switch(popt->index) {
2153
            case QEMU_OPTION_M:
2154
                machine = find_machine(optarg);
2155
                if (!machine) {
2156
                    QEMUMachine *m;
2157
                    printf("Supported machines are:\n");
2158
                    for(m = first_machine; m != NULL; m = m->next) {
2159
                        if (m->alias)
2160
                            printf("%-10s %s (alias of %s)\n",
2161
                                   m->alias, m->desc, m->name);
2162
                        printf("%-10s %s%s\n",
2163
                               m->name, m->desc,
2164
                               m->is_default ? " (default)" : "");
2165
                    }
2166
                    exit(*optarg != '?');
2167
                }
2168
                break;
2169
            case QEMU_OPTION_cpu:
2170
                /* hw initialization will check this */
2171
                if (*optarg == '?') {
2172
                    list_cpus(stdout, &fprintf, optarg);
2173
                    exit(0);
2174
                } else {
2175
                    cpu_model = optarg;
2176
                }
2177
                break;
2178
            case QEMU_OPTION_initrd:
2179
                initrd_filename = optarg;
2180
                break;
2181
            case QEMU_OPTION_hda:
2182
                {
2183
                    char buf[256];
2184
                    if (cyls == 0)
2185
                        snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2186
                    else
2187
                        snprintf(buf, sizeof(buf),
2188
                                 "%s,cyls=%d,heads=%d,secs=%d%s",
2189
                                 HD_OPTS , cyls, heads, secs,
2190
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
2191
                                 ",trans=lba" :
2192
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
2193
                                 ",trans=none" : "");
2194
                    drive_add(IF_DEFAULT, 0, optarg, buf);
2195
                    break;
2196
                }
2197
            case QEMU_OPTION_hdb:
2198
            case QEMU_OPTION_hdc:
2199
            case QEMU_OPTION_hdd:
2200
                drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2201
                          HD_OPTS);
2202
                break;
2203
            case QEMU_OPTION_drive:
2204
                if (drive_def(optarg) == NULL) {
2205
                    exit(1);
2206
                }
2207
                break;
2208
            case QEMU_OPTION_set:
2209
                if (qemu_set_option(optarg) != 0)
2210
                    exit(1);
2211
                break;
2212
            case QEMU_OPTION_global:
2213
                if (qemu_global_option(optarg) != 0)
2214
                    exit(1);
2215
                break;
2216
            case QEMU_OPTION_mtdblock:
2217
                drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2218
                break;
2219
            case QEMU_OPTION_sd:
2220
                drive_add(IF_SD, 0, optarg, SD_OPTS);
2221
                break;
2222
            case QEMU_OPTION_pflash:
2223
                drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2224
                break;
2225
            case QEMU_OPTION_snapshot:
2226
                snapshot = 1;
2227
                break;
2228
            case QEMU_OPTION_hdachs:
2229
                {
2230
                    const char *p;
2231
                    p = optarg;
2232
                    cyls = strtol(p, (char **)&p, 0);
2233
                    if (cyls < 1 || cyls > 16383)
2234
                        goto chs_fail;
2235
                    if (*p != ',')
2236
                        goto chs_fail;
2237
                    p++;
2238
                    heads = strtol(p, (char **)&p, 0);
2239
                    if (heads < 1 || heads > 16)
2240
                        goto chs_fail;
2241
                    if (*p != ',')
2242
                        goto chs_fail;
2243
                    p++;
2244
                    secs = strtol(p, (char **)&p, 0);
2245
                    if (secs < 1 || secs > 63)
2246
                        goto chs_fail;
2247
                    if (*p == ',') {
2248
                        p++;
2249
                        if (!strcmp(p, "none"))
2250
                            translation = BIOS_ATA_TRANSLATION_NONE;
2251
                        else if (!strcmp(p, "lba"))
2252
                            translation = BIOS_ATA_TRANSLATION_LBA;
2253
                        else if (!strcmp(p, "auto"))
2254
                            translation = BIOS_ATA_TRANSLATION_AUTO;
2255
                        else
2256
                            goto chs_fail;
2257
                    } else if (*p != '\0') {
2258
                    chs_fail:
2259
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
2260
                        exit(1);
2261
                    }
2262
                    if (hda_opts != NULL) {
2263
                        char num[16];
2264
                        snprintf(num, sizeof(num), "%d", cyls);
2265
                        qemu_opt_set(hda_opts, "cyls", num);
2266
                        snprintf(num, sizeof(num), "%d", heads);
2267
                        qemu_opt_set(hda_opts, "heads", num);
2268
                        snprintf(num, sizeof(num), "%d", secs);
2269
                        qemu_opt_set(hda_opts, "secs", num);
2270
                        if (translation == BIOS_ATA_TRANSLATION_LBA)
2271
                            qemu_opt_set(hda_opts, "trans", "lba");
2272
                        if (translation == BIOS_ATA_TRANSLATION_NONE)
2273
                            qemu_opt_set(hda_opts, "trans", "none");
2274
                    }
2275
                }
2276
                break;
2277
            case QEMU_OPTION_numa:
2278
                if (nb_numa_nodes >= MAX_NODES) {
2279
                    fprintf(stderr, "qemu: too many NUMA nodes\n");
2280
                    exit(1);
2281
                }
2282
                numa_add(optarg);
2283
                break;
2284
            case QEMU_OPTION_display:
2285
                display_type = select_display(optarg);
2286
                break;
2287
            case QEMU_OPTION_nographic:
2288
                display_type = DT_NOGRAPHIC;
2289
                break;
2290
            case QEMU_OPTION_curses:
2291
#ifdef CONFIG_CURSES
2292
                display_type = DT_CURSES;
2293
#else
2294
                fprintf(stderr, "Curses support is disabled\n");
2295
                exit(1);
2296
#endif
2297
                break;
2298
            case QEMU_OPTION_portrait:
2299
                graphic_rotate = 1;
2300
                break;
2301
            case QEMU_OPTION_kernel:
2302
                kernel_filename = optarg;
2303
                break;
2304
            case QEMU_OPTION_append:
2305
                kernel_cmdline = optarg;
2306
                break;
2307
            case QEMU_OPTION_cdrom:
2308
                drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2309
                break;
2310
            case QEMU_OPTION_boot:
2311
                {
2312
                    static const char * const params[] = {
2313
                        "order", "once", "menu", NULL
2314
                    };
2315
                    char buf[sizeof(boot_devices)];
2316
                    char *standard_boot_devices;
2317
                    int legacy = 0;
2318

    
2319
                    if (!strchr(optarg, '=')) {
2320
                        legacy = 1;
2321
                        pstrcpy(buf, sizeof(buf), optarg);
2322
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2323
                        fprintf(stderr,
2324
                                "qemu: unknown boot parameter '%s' in '%s'\n",
2325
                                buf, optarg);
2326
                        exit(1);
2327
                    }
2328

    
2329
                    if (legacy ||
2330
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
2331
                        validate_bootdevices(buf);
2332
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
2333
                    }
2334
                    if (!legacy) {
2335
                        if (get_param_value(buf, sizeof(buf),
2336
                                            "once", optarg)) {
2337
                            validate_bootdevices(buf);
2338
                            standard_boot_devices = qemu_strdup(boot_devices);
2339
                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
2340
                            qemu_register_reset(restore_boot_devices,
2341
                                                standard_boot_devices);
2342
                        }
2343
                        if (get_param_value(buf, sizeof(buf),
2344
                                            "menu", optarg)) {
2345
                            if (!strcmp(buf, "on")) {
2346
                                boot_menu = 1;
2347
                            } else if (!strcmp(buf, "off")) {
2348
                                boot_menu = 0;
2349
                            } else {
2350
                                fprintf(stderr,
2351
                                        "qemu: invalid option value '%s'\n",
2352
                                        buf);
2353
                                exit(1);
2354
                            }
2355
                        }
2356
                    }
2357
                }
2358
                break;
2359
            case QEMU_OPTION_fda:
2360
            case QEMU_OPTION_fdb:
2361
                drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2362
                          optarg, FD_OPTS);
2363
                break;
2364
            case QEMU_OPTION_no_fd_bootchk:
2365
                fd_bootchk = 0;
2366
                break;
2367
            case QEMU_OPTION_netdev:
2368
                if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2369
                    exit(1);
2370
                }
2371
                break;
2372
            case QEMU_OPTION_net:
2373
                if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2374
                    exit(1);
2375
                }
2376
                break;
2377
#ifdef CONFIG_SLIRP
2378
            case QEMU_OPTION_tftp:
2379
                legacy_tftp_prefix = optarg;
2380
                break;
2381
            case QEMU_OPTION_bootp:
2382
                legacy_bootp_filename = optarg;
2383
                break;
2384
            case QEMU_OPTION_redir:
2385
                if (net_slirp_redir(optarg) < 0)
2386
                    exit(1);
2387
                break;
2388
#endif
2389
            case QEMU_OPTION_bt:
2390
                add_device_config(DEV_BT, optarg);
2391
                break;
2392
            case QEMU_OPTION_audio_help:
2393
                if (!(audio_available())) {
2394
                    printf("Option %s not supported for this target\n", popt->name);
2395
                    exit(1);
2396
                }
2397
                AUD_help ();
2398
                exit (0);
2399
                break;
2400
            case QEMU_OPTION_soundhw:
2401
                if (!(audio_available())) {
2402
                    printf("Option %s not supported for this target\n", popt->name);
2403
                    exit(1);
2404
                }
2405
                select_soundhw (optarg);
2406
                break;
2407
            case QEMU_OPTION_h:
2408
                help(0);
2409
                break;
2410
            case QEMU_OPTION_version:
2411
                version();
2412
                exit(0);
2413
                break;
2414
            case QEMU_OPTION_m: {
2415
                int64_t value;
2416

    
2417
                value = strtosz(optarg, NULL);
2418
                if (value < 0) {
2419
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2420
                    exit(1);
2421
                }
2422

    
2423
                /* On 32-bit hosts, QEMU is limited by virtual address space */
2424
                if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2425
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2426
                    exit(1);
2427
                }
2428
                if (value != (uint64_t)(ram_addr_t)value) {
2429
                    fprintf(stderr, "qemu: ram size too large\n");
2430
                    exit(1);
2431
                }
2432
                ram_size = value;
2433
                break;
2434
            }
2435
            case QEMU_OPTION_mempath:
2436
                mem_path = optarg;
2437
                break;
2438
#ifdef MAP_POPULATE
2439
            case QEMU_OPTION_mem_prealloc:
2440
                mem_prealloc = 1;
2441
                break;
2442
#endif
2443
            case QEMU_OPTION_d:
2444
                set_cpu_log(optarg);
2445
                break;
2446
            case QEMU_OPTION_s:
2447
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2448
                break;
2449
            case QEMU_OPTION_gdb:
2450
                gdbstub_dev = optarg;
2451
                break;
2452
            case QEMU_OPTION_L:
2453
                data_dir = optarg;
2454
                break;
2455
            case QEMU_OPTION_bios:
2456
                bios_name = optarg;
2457
                break;
2458
            case QEMU_OPTION_singlestep:
2459
                singlestep = 1;
2460
                break;
2461
            case QEMU_OPTION_S:
2462
                autostart = 0;
2463
                break;
2464
            case QEMU_OPTION_k:
2465
                keyboard_layout = optarg;
2466
                break;
2467
            case QEMU_OPTION_localtime:
2468
                rtc_utc = 0;
2469
                break;
2470
            case QEMU_OPTION_vga:
2471
                select_vgahw (optarg);
2472
                break;
2473
            case QEMU_OPTION_g:
2474
                {
2475
                    const char *p;
2476
                    int w, h, depth;
2477
                    p = optarg;
2478
                    w = strtol(p, (char **)&p, 10);
2479
                    if (w <= 0) {
2480
                    graphic_error:
2481
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
2482
                        exit(1);
2483
                    }
2484
                    if (*p != 'x')
2485
                        goto graphic_error;
2486
                    p++;
2487
                    h = strtol(p, (char **)&p, 10);
2488
                    if (h <= 0)
2489
                        goto graphic_error;
2490
                    if (*p == 'x') {
2491
                        p++;
2492
                        depth = strtol(p, (char **)&p, 10);
2493
                        if (depth != 8 && depth != 15 && depth != 16 &&
2494
                            depth != 24 && depth != 32)
2495
                            goto graphic_error;
2496
                    } else if (*p == '\0') {
2497
                        depth = graphic_depth;
2498
                    } else {
2499
                        goto graphic_error;
2500
                    }
2501

    
2502
                    graphic_width = w;
2503
                    graphic_height = h;
2504
                    graphic_depth = depth;
2505
                }
2506
                break;
2507
            case QEMU_OPTION_echr:
2508
                {
2509
                    char *r;
2510
                    term_escape_char = strtol(optarg, &r, 0);
2511
                    if (r == optarg)
2512
                        printf("Bad argument to echr\n");
2513
                    break;
2514
                }
2515
            case QEMU_OPTION_monitor:
2516
                monitor_parse(optarg, "readline");
2517
                default_monitor = 0;
2518
                break;
2519
            case QEMU_OPTION_qmp:
2520
                monitor_parse(optarg, "control");
2521
                default_monitor = 0;
2522
                break;
2523
            case QEMU_OPTION_mon:
2524
                opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2525
                if (!opts) {
2526
                    exit(1);
2527
                }
2528
                default_monitor = 0;
2529
                break;
2530
            case QEMU_OPTION_chardev:
2531
                opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2532
                if (!opts) {
2533
                    exit(1);
2534
                }
2535
                break;
2536
            case QEMU_OPTION_fsdev:
2537
                olist = qemu_find_opts("fsdev");
2538
                if (!olist) {
2539
                    fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2540
                    exit(1);
2541
                }
2542
                opts = qemu_opts_parse(olist, optarg, 1);
2543
                if (!opts) {
2544
                    fprintf(stderr, "parse error: %s\n", optarg);
2545
                    exit(1);
2546
                }
2547
                break;
2548
            case QEMU_OPTION_virtfs: {
2549
                QemuOpts *fsdev;
2550
                QemuOpts *device;
2551

    
2552
                olist = qemu_find_opts("virtfs");
2553
                if (!olist) {
2554
                    fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2555
                    exit(1);
2556
                }
2557
                opts = qemu_opts_parse(olist, optarg, 1);
2558
                if (!opts) {
2559
                    fprintf(stderr, "parse error: %s\n", optarg);
2560
                    exit(1);
2561
                }
2562

    
2563
                if (qemu_opt_get(opts, "fstype") == NULL ||
2564
                        qemu_opt_get(opts, "mount_tag") == NULL ||
2565
                        qemu_opt_get(opts, "path") == NULL ||
2566
                        qemu_opt_get(opts, "security_model") == NULL) {
2567
                    fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2568
                            "security_model=[mapped|passthrough|none],"
2569
                            "mount_tag=tag.\n");
2570
                    exit(1);
2571
                }
2572

    
2573
                fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2574
                                         qemu_opt_get(opts, "mount_tag"), 1);
2575
                if (!fsdev) {
2576
                    fprintf(stderr, "duplicate fsdev id: %s\n",
2577
                            qemu_opt_get(opts, "mount_tag"));
2578
                    exit(1);
2579
                }
2580
                qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2581
                qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2582
                qemu_opt_set(fsdev, "security_model",
2583
                             qemu_opt_get(opts, "security_model"));
2584

    
2585
                device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2586
                qemu_opt_set(device, "driver", "virtio-9p-pci");
2587
                qemu_opt_set(device, "fsdev",
2588
                             qemu_opt_get(opts, "mount_tag"));
2589
                qemu_opt_set(device, "mount_tag",
2590
                             qemu_opt_get(opts, "mount_tag"));
2591
                break;
2592
            }
2593
            case QEMU_OPTION_serial:
2594
                add_device_config(DEV_SERIAL, optarg);
2595
                default_serial = 0;
2596
                if (strncmp(optarg, "mon:", 4) == 0) {
2597
                    default_monitor = 0;
2598
                }
2599
                break;
2600
            case QEMU_OPTION_watchdog:
2601
                if (watchdog) {
2602
                    fprintf(stderr,
2603
                            "qemu: only one watchdog option may be given\n");
2604
                    return 1;
2605
                }
2606
                watchdog = optarg;
2607
                break;
2608
            case QEMU_OPTION_watchdog_action:
2609
                if (select_watchdog_action(optarg) == -1) {
2610
                    fprintf(stderr, "Unknown -watchdog-action parameter\n");
2611
                    exit(1);
2612
                }
2613
                break;
2614
            case QEMU_OPTION_virtiocon:
2615
                add_device_config(DEV_VIRTCON, optarg);
2616
                default_virtcon = 0;
2617
                if (strncmp(optarg, "mon:", 4) == 0) {
2618
                    default_monitor = 0;
2619
                }
2620
                break;
2621
            case QEMU_OPTION_parallel:
2622
                add_device_config(DEV_PARALLEL, optarg);
2623
                default_parallel = 0;
2624
                if (strncmp(optarg, "mon:", 4) == 0) {
2625
                    default_monitor = 0;
2626
                }
2627
                break;
2628
            case QEMU_OPTION_debugcon:
2629
                add_device_config(DEV_DEBUGCON, optarg);
2630
                break;
2631
            case QEMU_OPTION_loadvm:
2632
                loadvm = optarg;
2633
                break;
2634
            case QEMU_OPTION_full_screen:
2635
                full_screen = 1;
2636
                break;
2637
#ifdef CONFIG_SDL
2638
            case QEMU_OPTION_no_frame:
2639
                no_frame = 1;
2640
                break;
2641
            case QEMU_OPTION_alt_grab:
2642
                alt_grab = 1;
2643
                break;
2644
            case QEMU_OPTION_ctrl_grab:
2645
                ctrl_grab = 1;
2646
                break;
2647
            case QEMU_OPTION_no_quit:
2648
                no_quit = 1;
2649
                break;
2650
            case QEMU_OPTION_sdl:
2651
                display_type = DT_SDL;
2652
                break;
2653
#else
2654
            case QEMU_OPTION_no_frame:
2655
            case QEMU_OPTION_alt_grab:
2656
            case QEMU_OPTION_ctrl_grab:
2657
            case QEMU_OPTION_no_quit:
2658
            case QEMU_OPTION_sdl:
2659
                fprintf(stderr, "SDL support is disabled\n");
2660
                exit(1);
2661
#endif
2662
            case QEMU_OPTION_pidfile:
2663
                pid_file = optarg;
2664
                break;
2665
            case QEMU_OPTION_win2k_hack:
2666
                win2k_install_hack = 1;
2667
                break;
2668
            case QEMU_OPTION_rtc_td_hack:
2669
                rtc_td_hack = 1;
2670
                break;
2671
            case QEMU_OPTION_acpitable:
2672
                do_acpitable_option(optarg);
2673
                break;
2674
            case QEMU_OPTION_smbios:
2675
                do_smbios_option(optarg);
2676
                break;
2677
            case QEMU_OPTION_enable_kvm:
2678
                olist = qemu_find_opts("machine");
2679
                qemu_opts_reset(olist);
2680
                qemu_opts_parse(olist, "accel=kvm", 0);
2681
                break;
2682
            case QEMU_OPTION_machine:
2683
                olist = qemu_find_opts("machine");
2684
                qemu_opts_reset(olist);
2685
                opts = qemu_opts_parse(olist, optarg, 0);
2686
                if (!opts) {
2687
                    fprintf(stderr, "parse error: %s\n", optarg);
2688
                    exit(1);
2689
                }
2690
                break;
2691
            case QEMU_OPTION_usb:
2692
                usb_enabled = 1;
2693
                break;
2694
            case QEMU_OPTION_usbdevice:
2695
                usb_enabled = 1;
2696
                add_device_config(DEV_USB, optarg);
2697
                break;
2698
            case QEMU_OPTION_device:
2699
                if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2700
                    exit(1);
2701
                }
2702
                break;
2703
            case QEMU_OPTION_smp:
2704
                smp_parse(optarg);
2705
                if (smp_cpus < 1) {
2706
                    fprintf(stderr, "Invalid number of CPUs\n");
2707
                    exit(1);
2708
                }
2709
                if (max_cpus < smp_cpus) {
2710
                    fprintf(stderr, "maxcpus must be equal to or greater than "
2711
                            "smp\n");
2712
                    exit(1);
2713
                }
2714
                if (max_cpus > 255) {
2715
                    fprintf(stderr, "Unsupported number of maxcpus\n");
2716
                    exit(1);
2717
                }
2718
                break;
2719
            case QEMU_OPTION_vnc:
2720
#ifdef CONFIG_VNC
2721
                display_remote++;
2722
                vnc_display = optarg;
2723
#else
2724
                fprintf(stderr, "VNC support is disabled\n");
2725
                exit(1);
2726
#endif
2727
                break;
2728
            case QEMU_OPTION_no_acpi:
2729
                acpi_enabled = 0;
2730
                break;
2731
            case QEMU_OPTION_no_hpet:
2732
                no_hpet = 1;
2733
                break;
2734
            case QEMU_OPTION_balloon:
2735
                if (balloon_parse(optarg) < 0) {
2736
                    fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2737
                    exit(1);
2738
                }
2739
                break;
2740
            case QEMU_OPTION_no_reboot:
2741
                no_reboot = 1;
2742
                break;
2743
            case QEMU_OPTION_no_shutdown:
2744
                no_shutdown = 1;
2745
                break;
2746
            case QEMU_OPTION_show_cursor:
2747
                cursor_hide = 0;
2748
                break;
2749
            case QEMU_OPTION_uuid:
2750
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2751
                    fprintf(stderr, "Fail to parse UUID string."
2752
                            " Wrong format.\n");
2753
                    exit(1);
2754
                }
2755
                break;
2756
            case QEMU_OPTION_option_rom:
2757
                if (nb_option_roms >= MAX_OPTION_ROMS) {
2758
                    fprintf(stderr, "Too many option ROMs\n");
2759
                    exit(1);
2760
                }
2761
                opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2762
                option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2763
                option_rom[nb_option_roms].bootindex =
2764
                    qemu_opt_get_number(opts, "bootindex", -1);
2765
                if (!option_rom[nb_option_roms].name) {
2766
                    fprintf(stderr, "Option ROM file is not specified\n");
2767
                    exit(1);
2768
                }
2769
                nb_option_roms++;
2770
                break;
2771
            case QEMU_OPTION_semihosting:
2772
                semihosting_enabled = 1;
2773
                break;
2774
            case QEMU_OPTION_name:
2775
                qemu_name = qemu_strdup(optarg);
2776
                 {
2777
                     char *p = strchr(qemu_name, ',');
2778
                     if (p != NULL) {
2779
                        *p++ = 0;
2780
                        if (strncmp(p, "process=", 8)) {
2781
                            fprintf(stderr, "Unknown subargument %s to -name\n", p);
2782
                            exit(1);
2783
                        }
2784
                        p += 8;
2785
                        os_set_proc_name(p);
2786
                     }        
2787
                 }        
2788
                break;
2789
            case QEMU_OPTION_prom_env:
2790
                if (nb_prom_envs >= MAX_PROM_ENVS) {
2791
                    fprintf(stderr, "Too many prom variables\n");
2792
                    exit(1);
2793
                }
2794
                prom_envs[nb_prom_envs] = optarg;
2795
                nb_prom_envs++;
2796
                break;
2797
            case QEMU_OPTION_old_param:
2798
                old_param = 1;
2799
                break;
2800
            case QEMU_OPTION_clock:
2801
                configure_alarms(optarg);
2802
                break;
2803
            case QEMU_OPTION_startdate:
2804
                configure_rtc_date_offset(optarg, 1);
2805
                break;
2806
            case QEMU_OPTION_rtc:
2807
                opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2808
                if (!opts) {
2809
                    exit(1);
2810
                }
2811
                configure_rtc(opts);
2812
                break;
2813
            case QEMU_OPTION_tb_size:
2814
                tb_size = strtol(optarg, NULL, 0);
2815
                if (tb_size < 0)
2816
                    tb_size = 0;
2817
                break;
2818
            case QEMU_OPTION_icount:
2819
                icount_option = optarg;
2820
                break;
2821
            case QEMU_OPTION_incoming:
2822
                incoming = optarg;
2823
                incoming_expected = true;
2824
                break;
2825
            case QEMU_OPTION_nodefaults:
2826
                default_serial = 0;
2827
                default_parallel = 0;
2828
                default_virtcon = 0;
2829
                default_monitor = 0;
2830
                default_vga = 0;
2831
                default_net = 0;
2832
                default_floppy = 0;
2833
                default_cdrom = 0;
2834
                default_sdcard = 0;
2835
                break;
2836
            case QEMU_OPTION_xen_domid:
2837
                if (!(xen_available())) {
2838
                    printf("Option %s not supported for this target\n", popt->name);
2839
                    exit(1);
2840
                }
2841
                xen_domid = atoi(optarg);
2842
                break;
2843
            case QEMU_OPTION_xen_create:
2844
                if (!(xen_available())) {
2845
                    printf("Option %s not supported for this target\n", popt->name);
2846
                    exit(1);
2847
                }
2848
                xen_mode = XEN_CREATE;
2849
                break;
2850
            case QEMU_OPTION_xen_attach:
2851
                if (!(xen_available())) {
2852
                    printf("Option %s not supported for this target\n", popt->name);
2853
                    exit(1);
2854
                }
2855
                xen_mode = XEN_ATTACH;
2856
                break;
2857
#ifdef CONFIG_SIMPLE_TRACE
2858
            case QEMU_OPTION_trace:
2859
                opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2860
                if (opts) {
2861
                    trace_file = qemu_opt_get(opts, "file");
2862
                }
2863
                break;
2864
#endif
2865
            case QEMU_OPTION_readconfig:
2866
                {
2867
                    int ret = qemu_read_config_file(optarg);
2868
                    if (ret < 0) {
2869
                        fprintf(stderr, "read config %s: %s\n", optarg,
2870
                            strerror(-ret));
2871
                        exit(1);
2872
                    }
2873
                    break;
2874
                }
2875
            case QEMU_OPTION_spice:
2876
                olist = qemu_find_opts("spice");
2877
                if (!olist) {
2878
                    fprintf(stderr, "spice is not supported by this qemu build.\n");
2879
                    exit(1);
2880
                }
2881
                opts = qemu_opts_parse(olist, optarg, 0);
2882
                if (!opts) {
2883
                    fprintf(stderr, "parse error: %s\n", optarg);
2884
                    exit(1);
2885
                }
2886
                break;
2887
            case QEMU_OPTION_writeconfig:
2888
                {
2889
                    FILE *fp;
2890
                    if (strcmp(optarg, "-") == 0) {
2891
                        fp = stdout;
2892
                    } else {
2893
                        fp = fopen(optarg, "w");
2894
                        if (fp == NULL) {
2895
                            fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2896
                            exit(1);
2897
                        }
2898
                    }
2899
                    qemu_config_write(fp);
2900
                    fclose(fp);
2901
                    break;
2902
                }
2903
            default:
2904
                os_parse_cmd_args(popt->index, optarg);
2905
            }
2906
        }
2907
    }
2908
    loc_set_none();
2909

    
2910
    if (!st_init(trace_file)) {
2911
        fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2912
    }
2913

    
2914
    /* If no data_dir is specified then try to find it relative to the
2915
       executable path.  */
2916
    if (!data_dir) {
2917
        data_dir = os_find_datadir(argv[0]);
2918
    }
2919
    /* If all else fails use the install patch specified when building.  */
2920
    if (!data_dir) {
2921
        data_dir = CONFIG_QEMU_DATADIR;
2922
    }
2923

    
2924
    /*
2925
     * Default to max_cpus = smp_cpus, in case the user doesn't
2926
     * specify a max_cpus value.
2927
     */
2928
    if (!max_cpus)
2929
        max_cpus = smp_cpus;
2930

    
2931
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2932
    if (smp_cpus > machine->max_cpus) {
2933
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2934
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
2935
                machine->max_cpus);
2936
        exit(1);
2937
    }
2938

    
2939
    /*
2940
     * Get the default machine options from the machine if it is not already
2941
     * specified either by the configuration file or by the command line.
2942
     */
2943
    if (machine->default_machine_opts) {
2944
        QemuOptsList *list = qemu_find_opts("machine");
2945
        const char *p = NULL;
2946

    
2947
        if (!QTAILQ_EMPTY(&list->head)) {
2948
            p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2949
        }
2950
        if (p == NULL) {
2951
            opts = qemu_opts_parse(qemu_find_opts("machine"),
2952
                                   machine->default_machine_opts, 0);
2953
            if (!opts) {
2954
                fprintf(stderr, "parse error for machine %s: %s\n",
2955
                        machine->name, machine->default_machine_opts);
2956
                exit(1);
2957
            }
2958
        }
2959
    }
2960

    
2961
    qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2962
    qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2963

    
2964
    if (machine->no_serial) {
2965
        default_serial = 0;
2966
    }
2967
    if (machine->no_parallel) {
2968
        default_parallel = 0;
2969
    }
2970
    if (!machine->use_virtcon) {
2971
        default_virtcon = 0;
2972
    }
2973
    if (machine->no_vga) {
2974
        default_vga = 0;
2975
    }
2976
    if (machine->no_floppy) {
2977
        default_floppy = 0;
2978
    }
2979
    if (machine->no_cdrom) {
2980
        default_cdrom = 0;
2981
    }
2982
    if (machine->no_sdcard) {
2983
        default_sdcard = 0;
2984
    }
2985

    
2986
    if (display_type == DT_NOGRAPHIC) {
2987
        if (default_parallel)
2988
            add_device_config(DEV_PARALLEL, "null");
2989
        if (default_serial && default_monitor) {
2990
            add_device_config(DEV_SERIAL, "mon:stdio");
2991
        } else if (default_virtcon && default_monitor) {
2992
            add_device_config(DEV_VIRTCON, "mon:stdio");
2993
        } else {
2994
            if (default_serial)
2995
                add_device_config(DEV_SERIAL, "stdio");
2996
            if (default_virtcon)
2997
                add_device_config(DEV_VIRTCON, "stdio");
2998
            if (default_monitor)
2999
                monitor_parse("stdio", "readline");
3000
        }
3001
    } else {
3002
        if (default_serial)
3003
            add_device_config(DEV_SERIAL, "vc:80Cx24C");
3004
        if (default_parallel)
3005
            add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3006
        if (default_monitor)
3007
            monitor_parse("vc:80Cx24C", "readline");
3008
        if (default_virtcon)
3009
            add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3010
    }
3011
    if (default_vga)
3012
        vga_interface_type = VGA_CIRRUS;
3013

    
3014
    socket_init();
3015

    
3016
    if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3017
        exit(1);
3018
#ifdef CONFIG_VIRTFS
3019
    if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3020
        exit(1);
3021
    }
3022
#endif
3023

    
3024
    os_daemonize();
3025

    
3026
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3027
        os_pidfile_error();
3028
        exit(1);
3029
    }
3030

    
3031
    configure_accelerator();
3032

    
3033
    if (qemu_init_main_loop()) {
3034
        fprintf(stderr, "qemu_init_main_loop failed\n");
3035
        exit(1);
3036
    }
3037
    linux_boot = (kernel_filename != NULL);
3038

    
3039
    if (!linux_boot && *kernel_cmdline != '\0') {
3040
        fprintf(stderr, "-append only allowed with -kernel option\n");
3041
        exit(1);
3042
    }
3043

    
3044
    if (!linux_boot && initrd_filename != NULL) {
3045
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
3046
        exit(1);
3047
    }
3048

    
3049
    os_set_line_buffering();
3050

    
3051
    if (init_timer_alarm() < 0) {
3052
        fprintf(stderr, "could not initialize alarm timer\n");
3053
        exit(1);
3054
    }
3055
    configure_icount(icount_option);
3056

    
3057
    if (net_init_clients() < 0) {
3058
        exit(1);
3059
    }
3060

    
3061
    /* init the bluetooth world */
3062
    if (foreach_device_config(DEV_BT, bt_parse))
3063
        exit(1);
3064

    
3065
    /* init the memory */
3066
    if (ram_size == 0)
3067
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3068

    
3069
    /* init the dynamic translator */
3070
    cpu_exec_init_all(tb_size * 1024 * 1024);
3071

    
3072
    bdrv_init_with_whitelist();
3073

    
3074
    blk_mig_init();
3075

    
3076
    /* open the virtual block devices */
3077
    if (snapshot)
3078
        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3079
    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3080
        exit(1);
3081

    
3082
    default_drive(default_cdrom, snapshot, machine->use_scsi,
3083
                  IF_DEFAULT, 2, CDROM_OPTS);
3084
    default_drive(default_floppy, snapshot, machine->use_scsi,
3085
                  IF_FLOPPY, 0, FD_OPTS);
3086
    default_drive(default_sdcard, snapshot, machine->use_scsi,
3087
                  IF_SD, 0, SD_OPTS);
3088

    
3089
    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3090
                         ram_load, NULL);
3091

    
3092
    if (nb_numa_nodes > 0) {
3093
        int i;
3094

    
3095
        if (nb_numa_nodes > smp_cpus) {
3096
            nb_numa_nodes = smp_cpus;
3097
        }
3098

    
3099
        /* If no memory size if given for any node, assume the default case
3100
         * and distribute the available memory equally across all nodes
3101
         */
3102
        for (i = 0; i < nb_numa_nodes; i++) {
3103
            if (node_mem[i] != 0)
3104
                break;
3105
        }
3106
        if (i == nb_numa_nodes) {
3107
            uint64_t usedmem = 0;
3108

    
3109
            /* On Linux, the each node's border has to be 8MB aligned,
3110
             * the final node gets the rest.
3111
             */
3112
            for (i = 0; i < nb_numa_nodes - 1; i++) {
3113
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3114
                usedmem += node_mem[i];
3115
            }
3116
            node_mem[i] = ram_size - usedmem;
3117
        }
3118

    
3119
        for (i = 0; i < nb_numa_nodes; i++) {
3120
            if (node_cpumask[i] != 0)
3121
                break;
3122
        }
3123
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
3124
         * must cope with this anyway, because there are BIOSes out there in
3125
         * real machines which also use this scheme.
3126
         */
3127
        if (i == nb_numa_nodes) {
3128
            for (i = 0; i < smp_cpus; i++) {
3129
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
3130
            }
3131
        }
3132
    }
3133

    
3134
    if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3135
        exit(1);
3136
    }
3137

    
3138
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3139
        exit(1);
3140
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3141
        exit(1);
3142
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3143
        exit(1);
3144
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3145
        exit(1);
3146

    
3147
    module_call_init(MODULE_INIT_DEVICE);
3148

    
3149
    if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3150
        exit(0);
3151

    
3152
    if (watchdog) {
3153
        i = select_watchdog(watchdog);
3154
        if (i > 0)
3155
            exit (i == 1 ? 1 : 0);
3156
    }
3157

    
3158
    if (machine->compat_props) {
3159
        qdev_prop_register_global_list(machine->compat_props);
3160
    }
3161
    qemu_add_globals();
3162

    
3163
    machine->init(ram_size, boot_devices,
3164
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3165

    
3166
    cpu_synchronize_all_post_init();
3167

    
3168
    set_numa_modes();
3169

    
3170
    current_machine = machine;
3171

    
3172
    /* init USB devices */
3173
    if (usb_enabled) {
3174
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
3175
            exit(1);
3176
    }
3177

    
3178
    /* init generic devices */
3179
    if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3180
        exit(1);
3181

    
3182
    net_check_clients();
3183

    
3184
    /* just use the first displaystate for the moment */
3185
    ds = get_displaystate();
3186

    
3187
    if (using_spice)
3188
        display_remote++;
3189
    if (display_type == DT_DEFAULT && !display_remote) {
3190
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3191
        display_type = DT_SDL;
3192
#elif defined(CONFIG_VNC)
3193
        vnc_display = "localhost:0,to=99";
3194
        show_vnc_port = 1;
3195
#else
3196
        display_type = DT_NONE;
3197
#endif
3198
    }
3199

    
3200

    
3201
    /* init local displays */
3202
    switch (display_type) {
3203
    case DT_NOGRAPHIC:
3204
        break;
3205
#if defined(CONFIG_CURSES)
3206
    case DT_CURSES:
3207
        curses_display_init(ds, full_screen);
3208
        break;
3209
#endif
3210
#if defined(CONFIG_SDL)
3211
    case DT_SDL:
3212
        sdl_display_init(ds, full_screen, no_frame);
3213
        break;
3214
#elif defined(CONFIG_COCOA)
3215
    case DT_SDL:
3216
        cocoa_display_init(ds, full_screen);
3217
        break;
3218
#endif
3219
    default:
3220
        break;
3221
    }
3222

    
3223
    /* must be after terminal init, SDL library changes signal handlers */
3224
    os_setup_signal_handling();
3225

    
3226
#ifdef CONFIG_VNC
3227
    /* init remote displays */
3228
    if (vnc_display) {
3229
        vnc_display_init(ds);
3230
        if (vnc_display_open(ds, vnc_display) < 0)
3231
            exit(1);
3232

    
3233
        if (show_vnc_port) {
3234
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3235
        }
3236
    }
3237
#endif
3238
#ifdef CONFIG_SPICE
3239
    if (using_spice && !qxl_enabled) {
3240
        qemu_spice_display_init(ds);
3241
    }
3242
#endif
3243

    
3244
    /* display setup */
3245
    dpy_resize(ds);
3246
    dcl = ds->listeners;
3247
    while (dcl != NULL) {
3248
        if (dcl->dpy_refresh != NULL) {
3249
            ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3250
            qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3251
            break;
3252
        }
3253
        dcl = dcl->next;
3254
    }
3255
    if (ds->gui_timer == NULL) {
3256
        nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3257
        qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3258
    }
3259
    text_consoles_set_display(ds);
3260

    
3261
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3262
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3263
                gdbstub_dev);
3264
        exit(1);
3265
    }
3266

    
3267
    qdev_machine_creation_done();
3268

    
3269
    if (rom_load_all() != 0) {
3270
        fprintf(stderr, "rom loading failed\n");
3271
        exit(1);
3272
    }
3273

    
3274
    /* TODO: once all bus devices are qdevified, this should be done
3275
     * when bus is created by qdev.c */
3276
    qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3277
    qemu_run_machine_init_done_notifiers();
3278

    
3279
    qemu_system_reset();
3280
    if (loadvm) {
3281
        if (load_vmstate(loadvm) < 0) {
3282
            autostart = 0;
3283
        }
3284
    }
3285

    
3286
    if (incoming) {
3287
        int ret = qemu_start_incoming_migration(incoming);
3288
        if (ret < 0) {
3289
            fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3290
                    incoming, ret);
3291
            exit(ret);
3292
        }
3293
    } else if (autostart) {
3294
        vm_start();
3295
    }
3296

    
3297
    os_setup_post();
3298

    
3299
    main_loop();
3300
    quit_timers();
3301
    net_cleanup();
3302

    
3303
    return 0;
3304
}