Statistics
| Branch: | Revision:

root / vl.c @ 42138043

History | View | Annotate | Download (92.3 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 = "qxl-vga",              .flag = &default_vga       },
293
};
294

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
516
    return 0;
517
}
518

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

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

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

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

    
541
    hci_table[nb_hcis++] = hci;
542

    
543
    return 0;
544
}
545

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

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

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

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

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

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

    
575
    vlan = qemu_find_bt_vlan(vlan_id);
576

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

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

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

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

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

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

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

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

    
627
/***********************************************************/
628
/* QEMU Block devices */
629

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
728
    qemu_boot_set(standard_boot_devices);
729

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

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

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

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

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

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

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

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

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

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

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

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

    
808
    *size = total;
809

    
810
    return list;
811
}
812

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

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

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

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

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

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

    
910
/***********************************************************/
911
/* USB devices */
912

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

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

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

    
926
    /* the other ones */
927
    if (strstart(devname, "host:", &p)) {
928
        dev = usb_host_device_open(p);
929
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
930
        dev = usb_bt_init(devname[2] ? hci_init(p) :
931
                        bt_new_hci(qemu_find_bt_vlan(0)));
932
    } else {
933
        return -1;
934
    }
935
    if (!dev)
936
        return -1;
937

    
938
done:
939
    return 0;
940
}
941

    
942
static int usb_device_del(const char *devname)
943
{
944
    int bus_num, addr;
945
    const char *p;
946

    
947
    if (strstart(devname, "host:", &p))
948
        return usb_host_device_close(p);
949

    
950
    if (!usb_enabled)
951
        return -1;
952

    
953
    p = strchr(devname, '.');
954
    if (!p)
955
        return -1;
956
    bus_num = strtoul(devname, NULL, 0);
957
    addr = strtoul(p + 1, NULL, 0);
958

    
959
    return usb_device_delete_addr(bus_num, addr);
960
}
961

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

    
972
void do_usb_add(Monitor *mon, const QDict *qdict)
973
{
974
    const char *devname = qdict_get_str(qdict, "devname");
975
    if (usb_device_add(devname) < 0) {
976
        error_report("could not add USB device '%s'", devname);
977
    }
978
}
979

    
980
void do_usb_del(Monitor *mon, const QDict *qdict)
981
{
982
    const char *devname = qdict_get_str(qdict, "devname");
983
    if (usb_device_del(devname) < 0) {
984
        error_report("could not delete USB device '%s'", devname);
985
    }
986
}
987

    
988
/***********************************************************/
989
/* PCMCIA/Cardbus */
990

    
991
static struct pcmcia_socket_entry_s {
992
    PCMCIASocket *socket;
993
    struct pcmcia_socket_entry_s *next;
994
} *pcmcia_sockets = 0;
995

    
996
void pcmcia_socket_register(PCMCIASocket *socket)
997
{
998
    struct pcmcia_socket_entry_s *entry;
999

    
1000
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1001
    entry->socket = socket;
1002
    entry->next = pcmcia_sockets;
1003
    pcmcia_sockets = entry;
1004
}
1005

    
1006
void pcmcia_socket_unregister(PCMCIASocket *socket)
1007
{
1008
    struct pcmcia_socket_entry_s *entry, **ptr;
1009

    
1010
    ptr = &pcmcia_sockets;
1011
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1012
        if (entry->socket == socket) {
1013
            *ptr = entry->next;
1014
            qemu_free(entry);
1015
        }
1016
}
1017

    
1018
void pcmcia_info(Monitor *mon)
1019
{
1020
    struct pcmcia_socket_entry_s *iter;
1021

    
1022
    if (!pcmcia_sockets)
1023
        monitor_printf(mon, "No PCMCIA sockets\n");
1024

    
1025
    for (iter = pcmcia_sockets; iter; iter = iter->next)
1026
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1027
                       iter->socket->attached ? iter->socket->card_string :
1028
                       "Empty");
1029
}
1030

    
1031
/***********************************************************/
1032
/* machine registration */
1033

    
1034
static QEMUMachine *first_machine = NULL;
1035
QEMUMachine *current_machine = NULL;
1036

    
1037
int qemu_register_machine(QEMUMachine *m)
1038
{
1039
    QEMUMachine **pm;
1040
    pm = &first_machine;
1041
    while (*pm != NULL)
1042
        pm = &(*pm)->next;
1043
    m->next = NULL;
1044
    *pm = m;
1045
    return 0;
1046
}
1047

    
1048
static QEMUMachine *find_machine(const char *name)
1049
{
1050
    QEMUMachine *m;
1051

    
1052
    for(m = first_machine; m != NULL; m = m->next) {
1053
        if (!strcmp(m->name, name))
1054
            return m;
1055
        if (m->alias && !strcmp(m->alias, name))
1056
            return m;
1057
    }
1058
    return NULL;
1059
}
1060

    
1061
static QEMUMachine *find_default_machine(void)
1062
{
1063
    QEMUMachine *m;
1064

    
1065
    for(m = first_machine; m != NULL; m = m->next) {
1066
        if (m->is_default) {
1067
            return m;
1068
        }
1069
    }
1070
    return NULL;
1071
}
1072

    
1073
/***********************************************************/
1074
/* main execution loop */
1075

    
1076
static void gui_update(void *opaque)
1077
{
1078
    uint64_t interval = GUI_REFRESH_INTERVAL;
1079
    DisplayState *ds = opaque;
1080
    DisplayChangeListener *dcl = ds->listeners;
1081

    
1082
    qemu_flush_coalesced_mmio_buffer();
1083
    dpy_refresh(ds);
1084

    
1085
    while (dcl != NULL) {
1086
        if (dcl->gui_timer_interval &&
1087
            dcl->gui_timer_interval < interval)
1088
            interval = dcl->gui_timer_interval;
1089
        dcl = dcl->next;
1090
    }
1091
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1092
}
1093

    
1094
static void nographic_update(void *opaque)
1095
{
1096
    uint64_t interval = GUI_REFRESH_INTERVAL;
1097

    
1098
    qemu_flush_coalesced_mmio_buffer();
1099
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1100
}
1101

    
1102
struct vm_change_state_entry {
1103
    VMChangeStateHandler *cb;
1104
    void *opaque;
1105
    QLIST_ENTRY (vm_change_state_entry) entries;
1106
};
1107

    
1108
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1109

    
1110
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1111
                                                     void *opaque)
1112
{
1113
    VMChangeStateEntry *e;
1114

    
1115
    e = qemu_mallocz(sizeof (*e));
1116

    
1117
    e->cb = cb;
1118
    e->opaque = opaque;
1119
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1120
    return e;
1121
}
1122

    
1123
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1124
{
1125
    QLIST_REMOVE (e, entries);
1126
    qemu_free (e);
1127
}
1128

    
1129
void vm_state_notify(int running, int reason)
1130
{
1131
    VMChangeStateEntry *e;
1132

    
1133
    trace_vm_state_notify(running, reason);
1134

    
1135
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1136
        e->cb(e->opaque, running, reason);
1137
    }
1138
}
1139

    
1140
void vm_start(void)
1141
{
1142
    if (!vm_running) {
1143
        cpu_enable_ticks();
1144
        vm_running = 1;
1145
        vm_state_notify(1, 0);
1146
        resume_all_vcpus();
1147
        monitor_protocol_event(QEVENT_RESUME, NULL);
1148
    }
1149
}
1150

    
1151
/* reset/shutdown handler */
1152

    
1153
typedef struct QEMUResetEntry {
1154
    QTAILQ_ENTRY(QEMUResetEntry) entry;
1155
    QEMUResetHandler *func;
1156
    void *opaque;
1157
} QEMUResetEntry;
1158

    
1159
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1160
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
1161
static int reset_requested;
1162
static int shutdown_requested, shutdown_signal = -1;
1163
static pid_t shutdown_pid;
1164
static int powerdown_requested;
1165
static int debug_requested;
1166
static int vmstop_requested;
1167

    
1168
int qemu_shutdown_requested_get(void)
1169
{
1170
    return shutdown_requested;
1171
}
1172

    
1173
int qemu_reset_requested_get(void)
1174
{
1175
    return reset_requested;
1176
}
1177

    
1178
int qemu_shutdown_requested(void)
1179
{
1180
    int r = shutdown_requested;
1181
    shutdown_requested = 0;
1182
    return r;
1183
}
1184

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

    
1201
int qemu_reset_requested(void)
1202
{
1203
    int r = reset_requested;
1204
    reset_requested = 0;
1205
    return r;
1206
}
1207

    
1208
int qemu_powerdown_requested(void)
1209
{
1210
    int r = powerdown_requested;
1211
    powerdown_requested = 0;
1212
    return r;
1213
}
1214

    
1215
static int qemu_debug_requested(void)
1216
{
1217
    int r = debug_requested;
1218
    debug_requested = 0;
1219
    return r;
1220
}
1221

    
1222
static int qemu_vmstop_requested(void)
1223
{
1224
    int r = vmstop_requested;
1225
    vmstop_requested = 0;
1226
    return r;
1227
}
1228

    
1229
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1230
{
1231
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1232

    
1233
    re->func = func;
1234
    re->opaque = opaque;
1235
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1236
}
1237

    
1238
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1239
{
1240
    QEMUResetEntry *re;
1241

    
1242
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
1243
        if (re->func == func && re->opaque == opaque) {
1244
            QTAILQ_REMOVE(&reset_handlers, re, entry);
1245
            qemu_free(re);
1246
            return;
1247
        }
1248
    }
1249
}
1250

    
1251
void qemu_system_reset(void)
1252
{
1253
    QEMUResetEntry *re, *nre;
1254

    
1255
    /* reset all devices */
1256
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1257
        re->func(re->opaque);
1258
    }
1259
    monitor_protocol_event(QEVENT_RESET, NULL);
1260
    cpu_synchronize_all_post_reset();
1261
}
1262

    
1263
void qemu_system_reset_request(void)
1264
{
1265
    if (no_reboot) {
1266
        shutdown_requested = 1;
1267
    } else {
1268
        reset_requested = 1;
1269
    }
1270
    cpu_stop_current();
1271
    qemu_notify_event();
1272
}
1273

    
1274
void qemu_system_killed(int signal, pid_t pid)
1275
{
1276
    shutdown_signal = signal;
1277
    shutdown_pid = pid;
1278
    qemu_system_shutdown_request();
1279
}
1280

    
1281
void qemu_system_shutdown_request(void)
1282
{
1283
    shutdown_requested = 1;
1284
    qemu_notify_event();
1285
}
1286

    
1287
void qemu_system_powerdown_request(void)
1288
{
1289
    powerdown_requested = 1;
1290
    qemu_notify_event();
1291
}
1292

    
1293
void qemu_system_debug_request(void)
1294
{
1295
    debug_requested = 1;
1296
    qemu_notify_event();
1297
}
1298

    
1299
void qemu_system_vmstop_request(int reason)
1300
{
1301
    vmstop_requested = reason;
1302
    qemu_notify_event();
1303
}
1304

    
1305
void main_loop_wait(int nonblocking)
1306
{
1307
    fd_set rfds, wfds, xfds;
1308
    int ret, nfds;
1309
    struct timeval tv;
1310
    int timeout;
1311

    
1312
    if (nonblocking)
1313
        timeout = 0;
1314
    else {
1315
        timeout = qemu_calculate_timeout();
1316
        qemu_bh_update_timeout(&timeout);
1317
    }
1318

    
1319
    os_host_main_loop_wait(&timeout);
1320

    
1321
    tv.tv_sec = timeout / 1000;
1322
    tv.tv_usec = (timeout % 1000) * 1000;
1323

    
1324
    /* poll any events */
1325
    /* XXX: separate device handlers from system ones */
1326
    nfds = -1;
1327
    FD_ZERO(&rfds);
1328
    FD_ZERO(&wfds);
1329
    FD_ZERO(&xfds);
1330
    qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1331
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1332

    
1333
    qemu_mutex_unlock_iothread();
1334
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1335
    qemu_mutex_lock_iothread();
1336

    
1337
    qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1338
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1339

    
1340
    qemu_run_all_timers();
1341

    
1342
    /* Check bottom-halves last in case any of the earlier events triggered
1343
       them.  */
1344
    qemu_bh_poll();
1345

    
1346
}
1347

    
1348
#ifndef CONFIG_IOTHREAD
1349
static int vm_request_pending(void)
1350
{
1351
    return powerdown_requested ||
1352
           reset_requested ||
1353
           shutdown_requested ||
1354
           debug_requested ||
1355
           vmstop_requested;
1356
}
1357
#endif
1358

    
1359
qemu_irq qemu_system_powerdown;
1360

    
1361
static void main_loop(void)
1362
{
1363
    bool nonblocking = false;
1364
#ifdef CONFIG_PROFILER
1365
    int64_t ti;
1366
#endif
1367
    int r;
1368

    
1369
    qemu_main_loop_start();
1370

    
1371
    for (;;) {
1372
#ifndef CONFIG_IOTHREAD
1373
        nonblocking = cpu_exec_all();
1374
        if (vm_request_pending()) {
1375
            nonblocking = true;
1376
        }
1377
#endif
1378
#ifdef CONFIG_PROFILER
1379
        ti = profile_getclock();
1380
#endif
1381
        main_loop_wait(nonblocking);
1382
#ifdef CONFIG_PROFILER
1383
        dev_time += profile_getclock() - ti;
1384
#endif
1385

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

    
1416
static void version(void)
1417
{
1418
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1419
}
1420

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

    
1449
#define HAS_ARG 0x0001
1450

    
1451
typedef struct QEMUOption {
1452
    const char *name;
1453
    int flags;
1454
    int index;
1455
    uint32_t arch_mask;
1456
} QEMUOption;
1457

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

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

    
1493
        if (strstart(opts, ",retrace=", &nextopt)) {
1494
            opts = nextopt;
1495
            if (strstart(opts, "dumb", &nextopt))
1496
                vga_retrace_method = VGA_RETRACE_DUMB;
1497
            else if (strstart(opts, "precise", &nextopt))
1498
                vga_retrace_method = VGA_RETRACE_PRECISE;
1499
            else goto invalid_vga;
1500
        } else goto invalid_vga;
1501
        opts = nextopt;
1502
    }
1503
}
1504

    
1505
static DisplayType select_display(const char *p)
1506
{
1507
    const char *opts;
1508
    DisplayType display = DT_DEFAULT;
1509

    
1510
    if (strstart(p, "sdl", &opts)) {
1511
#ifdef CONFIG_SDL
1512
        display = DT_SDL;
1513
        while (*opts) {
1514
            const char *nextopt;
1515

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

    
1567
        if (*opts) {
1568
            const char *nextopt;
1569

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

    
1596
    return display;
1597
}
1598

    
1599
static int balloon_parse(const char *arg)
1600
{
1601
    QemuOpts *opts;
1602

    
1603
    if (strcmp(arg, "none") == 0) {
1604
        return 0;
1605
    }
1606

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

    
1621
    return -1;
1622
}
1623

    
1624
char *qemu_find_file(int type, const char *name)
1625
{
1626
    int len;
1627
    const char *subdir;
1628
    char *buf;
1629

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

    
1655
static int device_help_func(QemuOpts *opts, void *opaque)
1656
{
1657
    return qdev_device_help(opts);
1658
}
1659

    
1660
static int device_init_func(QemuOpts *opts, void *opaque)
1661
{
1662
    DeviceState *dev;
1663

    
1664
    dev = qdev_device_add(opts);
1665
    if (!dev)
1666
        return -1;
1667
    return 0;
1668
}
1669

    
1670
static int chardev_init_func(QemuOpts *opts, void *opaque)
1671
{
1672
    CharDriverState *chr;
1673

    
1674
    chr = qemu_chr_open_opts(opts, NULL);
1675
    if (!chr)
1676
        return -1;
1677
    return 0;
1678
}
1679

    
1680
#ifdef CONFIG_VIRTFS
1681
static int fsdev_init_func(QemuOpts *opts, void *opaque)
1682
{
1683
    int ret;
1684
    ret = qemu_fsdev_add(opts);
1685

    
1686
    return ret;
1687
}
1688
#endif
1689

    
1690
static int mon_init_func(QemuOpts *opts, void *opaque)
1691
{
1692
    CharDriverState *chr;
1693
    const char *chardev;
1694
    const char *mode;
1695
    int flags;
1696

    
1697
    mode = qemu_opt_get(opts, "mode");
1698
    if (mode == NULL) {
1699
        mode = "readline";
1700
    }
1701
    if (strcmp(mode, "readline") == 0) {
1702
        flags = MONITOR_USE_READLINE;
1703
    } else if (strcmp(mode, "control") == 0) {
1704
        flags = MONITOR_USE_CONTROL;
1705
    } else {
1706
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1707
        exit(1);
1708
    }
1709

    
1710
    if (qemu_opt_get_bool(opts, "pretty", 0))
1711
        flags |= MONITOR_USE_PRETTY;
1712

    
1713
    if (qemu_opt_get_bool(opts, "default", 0))
1714
        flags |= MONITOR_IS_DEFAULT;
1715

    
1716
    chardev = qemu_opt_get(opts, "chardev");
1717
    chr = qemu_chr_find(chardev);
1718
    if (chr == NULL) {
1719
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1720
        exit(1);
1721
    }
1722

    
1723
    monitor_init(chr, flags);
1724
    return 0;
1725
}
1726

    
1727
static void monitor_parse(const char *optarg, const char *mode)
1728
{
1729
    static int monitor_device_index = 0;
1730
    QemuOpts *opts;
1731
    const char *p;
1732
    char label[32];
1733
    int def = 0;
1734

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

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

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

    
1776
static void add_device_config(int type, const char *cmdline)
1777
{
1778
    struct device_config *conf;
1779

    
1780
    conf = qemu_mallocz(sizeof(*conf));
1781
    conf->type = type;
1782
    conf->cmdline = cmdline;
1783
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1784
}
1785

    
1786
static int foreach_device_config(int type, int (*func)(const char *cmdline))
1787
{
1788
    struct device_config *conf;
1789
    int rc;
1790

    
1791
    QTAILQ_FOREACH(conf, &device_configs, next) {
1792
        if (conf->type != type)
1793
            continue;
1794
        rc = func(conf->cmdline);
1795
        if (0 != rc)
1796
            return rc;
1797
    }
1798
    return 0;
1799
}
1800

    
1801
static int serial_parse(const char *devname)
1802
{
1803
    static int index = 0;
1804
    char label[32];
1805

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

    
1823
static int parallel_parse(const char *devname)
1824
{
1825
    static int index = 0;
1826
    char label[32];
1827

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

    
1845
static int virtcon_parse(const char *devname)
1846
{
1847
    QemuOptsList *device = qemu_find_opts("device");
1848
    static int index = 0;
1849
    char label[32];
1850
    QemuOpts *bus_opts, *dev_opts;
1851

    
1852
    if (strcmp(devname, "none") == 0)
1853
        return 0;
1854
    if (index == MAX_VIRTIO_CONSOLES) {
1855
        fprintf(stderr, "qemu: too many virtio consoles\n");
1856
        exit(1);
1857
    }
1858

    
1859
    bus_opts = qemu_opts_create(device, NULL, 0);
1860
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
1861

    
1862
    dev_opts = qemu_opts_create(device, NULL, 0);
1863
    qemu_opt_set(dev_opts, "driver", "virtconsole");
1864

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

    
1874
    index++;
1875
    return 0;
1876
}
1877

    
1878
static int debugcon_parse(const char *devname)
1879
{   
1880
    QemuOpts *opts;
1881

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

    
1895
static int tcg_init(void)
1896
{
1897
    return 0;
1898
}
1899

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

    
1912
static int configure_accelerator(void)
1913
{
1914
    const char *p = NULL;
1915
    char buf[10];
1916
    int i, ret;
1917
    bool accel_initalised = 0;
1918
    bool init_failed = 0;
1919

    
1920
    QemuOptsList *list = qemu_find_opts("machine");
1921
    if (!QTAILQ_EMPTY(&list->head)) {
1922
        p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1923
    }
1924

    
1925
    if (p == NULL) {
1926
        /* Use the default "accelerator", tcg */
1927
        p = "tcg";
1928
    }
1929

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

    
1960
    if (!accel_initalised) {
1961
        fprintf(stderr, "No accelerator found!\n");
1962
        exit(1);
1963
    }
1964

    
1965
    if (init_failed) {
1966
        fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
1967
    }
1968

    
1969
    return !accel_initalised;
1970
}
1971

    
1972
void qemu_add_exit_notifier(Notifier *notify)
1973
{
1974
    notifier_list_add(&exit_notifiers, notify);
1975
}
1976

    
1977
void qemu_remove_exit_notifier(Notifier *notify)
1978
{
1979
    notifier_list_remove(&exit_notifiers, notify);
1980
}
1981

    
1982
static void qemu_run_exit_notifiers(void)
1983
{
1984
    notifier_list_notify(&exit_notifiers);
1985
}
1986

    
1987
void qemu_add_machine_init_done_notifier(Notifier *notify)
1988
{
1989
    notifier_list_add(&machine_init_done_notifiers, notify);
1990
}
1991

    
1992
static void qemu_run_machine_init_done_notifiers(void)
1993
{
1994
    notifier_list_notify(&machine_init_done_notifiers);
1995
}
1996

    
1997
static const QEMUOption *lookup_opt(int argc, char **argv,
1998
                                    const char **poptarg, int *poptind)
1999
{
2000
    const QEMUOption *popt;
2001
    int optind = *poptind;
2002
    char *r = argv[optind];
2003
    const char *optarg;
2004

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

    
2031
    *poptarg = optarg;
2032
    *poptind = optind;
2033

    
2034
    return popt;
2035
}
2036

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

    
2065
    atexit(qemu_run_exit_notifiers);
2066
    error_set_progname(argv[0]);
2067

    
2068
    init_clocks();
2069

    
2070
    qemu_cache_utils_init(envp);
2071

    
2072
    QLIST_INIT (&vm_change_state_head);
2073
    os_setup_early_signal_handling();
2074

    
2075
    module_call_init(MODULE_INIT_MACHINE);
2076
    machine = find_default_machine();
2077
    cpu_model = NULL;
2078
    initrd_filename = NULL;
2079
    ram_size = 0;
2080
    snapshot = 0;
2081
    kernel_filename = NULL;
2082
    kernel_cmdline = "";
2083
    cyls = heads = secs = 0;
2084
    translation = BIOS_ATA_TRANSLATION_AUTO;
2085

    
2086
    for (i = 0; i < MAX_NODES; i++) {
2087
        node_mem[i] = 0;
2088
        node_cpumask[i] = 0;
2089
    }
2090

    
2091
    nb_numa_nodes = 0;
2092
    nb_nics = 0;
2093

    
2094
    tb_size = 0;
2095
    autostart= 1;
2096

    
2097
    /* first pass of option parsing */
2098
    optind = 1;
2099
    while (optind < argc) {
2100
        if (argv[optind][0] != '-') {
2101
            /* disk image */
2102
            optind++;
2103
            continue;
2104
        } else {
2105
            const QEMUOption *popt;
2106

    
2107
            popt = lookup_opt(argc, argv, &optarg, &optind);
2108
            switch (popt->index) {
2109
            case QEMU_OPTION_nodefconfig:
2110
                defconfig=0;
2111
                break;
2112
            }
2113
        }
2114
    }
2115

    
2116
    if (defconfig) {
2117
        int ret;
2118

    
2119
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2120
        if (ret < 0 && ret != -ENOENT) {
2121
            exit(1);
2122
        }
2123

    
2124
        ret = qemu_read_config_file(arch_config_name);
2125
        if (ret < 0 && ret != -ENOENT) {
2126
            exit(1);
2127
        }
2128
    }
2129
    cpudef_init();
2130

    
2131
    /* second pass of option parsing */
2132
    optind = 1;
2133
    for(;;) {
2134
        if (optind >= argc)
2135
            break;
2136
        if (argv[optind][0] != '-') {
2137
            hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2138
        } else {
2139
            const QEMUOption *popt;
2140

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

    
2313
                    if (!strchr(optarg, '=')) {
2314
                        legacy = 1;
2315
                        pstrcpy(buf, sizeof(buf), optarg);
2316
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2317
                        fprintf(stderr,
2318
                                "qemu: unknown boot parameter '%s' in '%s'\n",
2319
                                buf, optarg);
2320
                        exit(1);
2321
                    }
2322

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

    
2411
                value = strtosz(optarg, NULL);
2412
                if (value < 0) {
2413
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2414
                    exit(1);
2415
                }
2416

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

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

    
2546
                olist = qemu_find_opts("virtfs");
2547
                if (!olist) {
2548
                    fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2549
                    exit(1);
2550
                }
2551
                opts = qemu_opts_parse(olist, optarg, 1);
2552
                if (!opts) {
2553
                    fprintf(stderr, "parse error: %s\n", optarg);
2554
                    exit(1);
2555
                }
2556

    
2557
                if (qemu_opt_get(opts, "fstype") == NULL ||
2558
                        qemu_opt_get(opts, "mount_tag") == NULL ||
2559
                        qemu_opt_get(opts, "path") == NULL ||
2560
                        qemu_opt_get(opts, "security_model") == NULL) {
2561
                    fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2562
                            "security_model=[mapped|passthrough|none],"
2563
                            "mount_tag=tag.\n");
2564
                    exit(1);
2565
                }
2566

    
2567
                fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2568
                                         qemu_opt_get(opts, "mount_tag"), 1);
2569
                if (!fsdev) {
2570
                    fprintf(stderr, "duplicate fsdev id: %s\n",
2571
                            qemu_opt_get(opts, "mount_tag"));
2572
                    exit(1);
2573
                }
2574
                qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2575
                qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2576
                qemu_opt_set(fsdev, "security_model",
2577
                             qemu_opt_get(opts, "security_model"));
2578

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

    
2904
    if (!st_init(trace_file)) {
2905
        fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2906
    }
2907

    
2908
    /* If no data_dir is specified then try to find it relative to the
2909
       executable path.  */
2910
    if (!data_dir) {
2911
        data_dir = os_find_datadir(argv[0]);
2912
    }
2913
    /* If all else fails use the install patch specified when building.  */
2914
    if (!data_dir) {
2915
        data_dir = CONFIG_QEMU_DATADIR;
2916
    }
2917

    
2918
    /*
2919
     * Default to max_cpus = smp_cpus, in case the user doesn't
2920
     * specify a max_cpus value.
2921
     */
2922
    if (!max_cpus)
2923
        max_cpus = smp_cpus;
2924

    
2925
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2926
    if (smp_cpus > machine->max_cpus) {
2927
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2928
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
2929
                machine->max_cpus);
2930
        exit(1);
2931
    }
2932

    
2933
    /*
2934
     * Get the default machine options from the machine if it is not already
2935
     * specified either by the configuration file or by the command line.
2936
     */
2937
    if (machine->default_machine_opts) {
2938
        QemuOptsList *list = qemu_find_opts("machine");
2939
        const char *p = NULL;
2940

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

    
2955
    qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2956
    qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2957

    
2958
    if (machine->no_serial) {
2959
        default_serial = 0;
2960
    }
2961
    if (machine->no_parallel) {
2962
        default_parallel = 0;
2963
    }
2964
    if (!machine->use_virtcon) {
2965
        default_virtcon = 0;
2966
    }
2967
    if (machine->no_vga) {
2968
        default_vga = 0;
2969
    }
2970
    if (machine->no_floppy) {
2971
        default_floppy = 0;
2972
    }
2973
    if (machine->no_cdrom) {
2974
        default_cdrom = 0;
2975
    }
2976
    if (machine->no_sdcard) {
2977
        default_sdcard = 0;
2978
    }
2979

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

    
3008
    socket_init();
3009

    
3010
    if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
3011
        exit(1);
3012
#ifdef CONFIG_VIRTFS
3013
    if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
3014
        exit(1);
3015
    }
3016
#endif
3017

    
3018
    os_daemonize();
3019

    
3020
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
3021
        os_pidfile_error();
3022
        exit(1);
3023
    }
3024

    
3025
    configure_accelerator();
3026

    
3027
    if (qemu_init_main_loop()) {
3028
        fprintf(stderr, "qemu_init_main_loop failed\n");
3029
        exit(1);
3030
    }
3031
    linux_boot = (kernel_filename != NULL);
3032

    
3033
    if (!linux_boot && *kernel_cmdline != '\0') {
3034
        fprintf(stderr, "-append only allowed with -kernel option\n");
3035
        exit(1);
3036
    }
3037

    
3038
    if (!linux_boot && initrd_filename != NULL) {
3039
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
3040
        exit(1);
3041
    }
3042

    
3043
    os_set_line_buffering();
3044

    
3045
    if (init_timer_alarm() < 0) {
3046
        fprintf(stderr, "could not initialize alarm timer\n");
3047
        exit(1);
3048
    }
3049
    configure_icount(icount_option);
3050

    
3051
    if (net_init_clients() < 0) {
3052
        exit(1);
3053
    }
3054

    
3055
    /* init the bluetooth world */
3056
    if (foreach_device_config(DEV_BT, bt_parse))
3057
        exit(1);
3058

    
3059
    /* init the memory */
3060
    if (ram_size == 0)
3061
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3062

    
3063
    /* init the dynamic translator */
3064
    cpu_exec_init_all(tb_size * 1024 * 1024);
3065

    
3066
    bdrv_init_with_whitelist();
3067

    
3068
    blk_mig_init();
3069

    
3070
    /* open the virtual block devices */
3071
    if (snapshot)
3072
        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3073
    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
3074
        exit(1);
3075

    
3076
    default_drive(default_cdrom, snapshot, machine->use_scsi,
3077
                  IF_DEFAULT, 2, CDROM_OPTS);
3078
    default_drive(default_floppy, snapshot, machine->use_scsi,
3079
                  IF_FLOPPY, 0, FD_OPTS);
3080
    default_drive(default_sdcard, snapshot, machine->use_scsi,
3081
                  IF_SD, 0, SD_OPTS);
3082

    
3083
    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
3084
                         ram_load, NULL);
3085

    
3086
    if (nb_numa_nodes > 0) {
3087
        int i;
3088

    
3089
        if (nb_numa_nodes > smp_cpus) {
3090
            nb_numa_nodes = smp_cpus;
3091
        }
3092

    
3093
        /* If no memory size if given for any node, assume the default case
3094
         * and distribute the available memory equally across all nodes
3095
         */
3096
        for (i = 0; i < nb_numa_nodes; i++) {
3097
            if (node_mem[i] != 0)
3098
                break;
3099
        }
3100
        if (i == nb_numa_nodes) {
3101
            uint64_t usedmem = 0;
3102

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

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

    
3128
    if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3129
        exit(1);
3130
    }
3131

    
3132
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3133
        exit(1);
3134
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3135
        exit(1);
3136
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3137
        exit(1);
3138
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3139
        exit(1);
3140

    
3141
    module_call_init(MODULE_INIT_DEVICE);
3142

    
3143
    if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3144
        exit(0);
3145

    
3146
    if (watchdog) {
3147
        i = select_watchdog(watchdog);
3148
        if (i > 0)
3149
            exit (i == 1 ? 1 : 0);
3150
    }
3151

    
3152
    if (machine->compat_props) {
3153
        qdev_prop_register_global_list(machine->compat_props);
3154
    }
3155
    qemu_add_globals();
3156

    
3157
    machine->init(ram_size, boot_devices,
3158
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3159

    
3160
    cpu_synchronize_all_post_init();
3161

    
3162
    set_numa_modes();
3163

    
3164
    current_machine = machine;
3165

    
3166
    /* init USB devices */
3167
    if (usb_enabled) {
3168
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
3169
            exit(1);
3170
    }
3171

    
3172
    /* init generic devices */
3173
    if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3174
        exit(1);
3175

    
3176
    net_check_clients();
3177

    
3178
    /* just use the first displaystate for the moment */
3179
    ds = get_displaystate();
3180

    
3181
    if (using_spice)
3182
        display_remote++;
3183
    if (display_type == DT_DEFAULT && !display_remote) {
3184
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3185
        display_type = DT_SDL;
3186
#elif defined(CONFIG_VNC)
3187
        vnc_display = "localhost:0,to=99";
3188
        show_vnc_port = 1;
3189
#else
3190
        display_type = DT_NONE;
3191
#endif
3192
    }
3193

    
3194

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

    
3217
    /* must be after terminal init, SDL library changes signal handlers */
3218
    os_setup_signal_handling();
3219

    
3220
#ifdef CONFIG_VNC
3221
    /* init remote displays */
3222
    if (vnc_display) {
3223
        vnc_display_init(ds);
3224
        if (vnc_display_open(ds, vnc_display) < 0)
3225
            exit(1);
3226

    
3227
        if (show_vnc_port) {
3228
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3229
        }
3230
    }
3231
#endif
3232
#ifdef CONFIG_SPICE
3233
    if (using_spice && !qxl_enabled) {
3234
        qemu_spice_display_init(ds);
3235
    }
3236
#endif
3237

    
3238
    /* display setup */
3239
    dpy_resize(ds);
3240
    dcl = ds->listeners;
3241
    while (dcl != NULL) {
3242
        if (dcl->dpy_refresh != NULL) {
3243
            ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3244
            qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3245
            break;
3246
        }
3247
        dcl = dcl->next;
3248
    }
3249
    if (ds->gui_timer == NULL) {
3250
        nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3251
        qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3252
    }
3253
    text_consoles_set_display(ds);
3254

    
3255
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3256
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3257
                gdbstub_dev);
3258
        exit(1);
3259
    }
3260

    
3261
    qdev_machine_creation_done();
3262

    
3263
    if (rom_load_all() != 0) {
3264
        fprintf(stderr, "rom loading failed\n");
3265
        exit(1);
3266
    }
3267

    
3268
    /* TODO: once all bus devices are qdevified, this should be done
3269
     * when bus is created by qdev.c */
3270
    qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3271
    qemu_run_machine_init_done_notifiers();
3272

    
3273
    qemu_system_reset();
3274
    if (loadvm) {
3275
        if (load_vmstate(loadvm) < 0) {
3276
            autostart = 0;
3277
        }
3278
    }
3279

    
3280
    if (incoming) {
3281
        int ret = qemu_start_incoming_migration(incoming);
3282
        if (ret < 0) {
3283
            fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3284
                    incoming, ret);
3285
            exit(ret);
3286
        }
3287
    } else if (autostart) {
3288
        vm_start();
3289
    }
3290

    
3291
    os_setup_post();
3292

    
3293
    main_loop();
3294
    quit_timers();
3295
    net_cleanup();
3296

    
3297
    return 0;
3298
}