Statistics
| Branch: | Revision:

root / vl.c @ 02981419

History | View | Annotate | Download (89.2 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
int kvm_allowed = 0;
261
uint32_t xen_domid;
262
enum xen_mode xen_mode = XEN_EMULATE;
263

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

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

    
289
static int default_driver_check(QemuOpts *opts, void *opaque)
290
{
291
    const char *driver = qemu_opt_get(opts, "driver");
292
    int i;
293

    
294
    if (!driver)
295
        return 0;
296
    for (i = 0; i < ARRAY_SIZE(default_list); i++) {
297
        if (strcmp(default_list[i].driver, driver) != 0)
298
            continue;
299
        *(default_list[i].flag) = 0;
300
    }
301
    return 0;
302
}
303

    
304
/***********************************************************/
305
/* real time host monotonic timer */
306

    
307
/***********************************************************/
308
/* host time/date access */
309
void qemu_get_timedate(struct tm *tm, int offset)
310
{
311
    time_t ti;
312
    struct tm *ret;
313

    
314
    time(&ti);
315
    ti += offset;
316
    if (rtc_date_offset == -1) {
317
        if (rtc_utc)
318
            ret = gmtime(&ti);
319
        else
320
            ret = localtime(&ti);
321
    } else {
322
        ti -= rtc_date_offset;
323
        ret = gmtime(&ti);
324
    }
325

    
326
    memcpy(tm, ret, sizeof(struct tm));
327
}
328

    
329
int qemu_timedate_diff(struct tm *tm)
330
{
331
    time_t seconds;
332

    
333
    if (rtc_date_offset == -1)
334
        if (rtc_utc)
335
            seconds = mktimegm(tm);
336
        else
337
            seconds = mktime(tm);
338
    else
339
        seconds = mktimegm(tm) + rtc_date_offset;
340

    
341
    return seconds - time(NULL);
342
}
343

    
344
void rtc_change_mon_event(struct tm *tm)
345
{
346
    QObject *data;
347

    
348
    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
349
    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
350
    qobject_decref(data);
351
}
352

    
353
static void configure_rtc_date_offset(const char *startdate, int legacy)
354
{
355
    time_t rtc_start_date;
356
    struct tm tm;
357

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

    
392
static void configure_rtc(QemuOpts *opts)
393
{
394
    const char *value;
395

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

    
430
/***********************************************************/
431
/* Bluetooth support */
432
static int nb_hcis;
433
static int cur_hci;
434
static struct HCIInfo *hci_table[MAX_NICS];
435

    
436
static struct bt_vlan_s {
437
    struct bt_scatternet_s net;
438
    int id;
439
    struct bt_vlan_s *next;
440
} *first_bt_vlan;
441

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

    
459
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
460
{
461
}
462

    
463
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
464
{
465
    return -ENOTSUP;
466
}
467

    
468
static struct HCIInfo null_hci = {
469
    .cmd_send = null_hci_send,
470
    .sco_send = null_hci_send,
471
    .acl_send = null_hci_send,
472
    .bdaddr_set = null_hci_addr_set,
473
};
474

    
475
struct HCIInfo *qemu_next_hci(void)
476
{
477
    if (cur_hci == nb_hcis)
478
        return &null_hci;
479

    
480
    return hci_table[cur_hci++];
481
}
482

    
483
static struct HCIInfo *hci_init(const char *str)
484
{
485
    char *endp;
486
    struct bt_scatternet_s *vlan = 0;
487

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

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

    
510
    return 0;
511
}
512

    
513
static int bt_hci_parse(const char *str)
514
{
515
    struct HCIInfo *hci;
516
    bdaddr_t bdaddr;
517

    
518
    if (nb_hcis >= MAX_NICS) {
519
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
520
        return -1;
521
    }
522

    
523
    hci = hci_init(str);
524
    if (!hci)
525
        return -1;
526

    
527
    bdaddr.b[0] = 0x52;
528
    bdaddr.b[1] = 0x54;
529
    bdaddr.b[2] = 0x00;
530
    bdaddr.b[3] = 0x12;
531
    bdaddr.b[4] = 0x34;
532
    bdaddr.b[5] = 0x56 + nb_hcis;
533
    hci->bdaddr_set(hci, bdaddr.b);
534

    
535
    hci_table[nb_hcis++] = hci;
536

    
537
    return 0;
538
}
539

    
540
static void bt_vhci_add(int vlan_id)
541
{
542
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
543

    
544
    if (!vlan->slave)
545
        fprintf(stderr, "qemu: warning: adding a VHCI to "
546
                        "an empty scatternet %i\n", vlan_id);
547

    
548
    bt_vhci_init(bt_new_hci(vlan));
549
}
550

    
551
static struct bt_device_s *bt_device_add(const char *opt)
552
{
553
    struct bt_scatternet_s *vlan;
554
    int vlan_id = 0;
555
    char *endp = strstr(opt, ",vlan=");
556
    int len = (endp ? endp - opt : strlen(opt)) + 1;
557
    char devname[10];
558

    
559
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
560

    
561
    if (endp) {
562
        vlan_id = strtol(endp + 6, &endp, 0);
563
        if (*endp) {
564
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
565
            return 0;
566
        }
567
    }
568

    
569
    vlan = qemu_find_bt_vlan(vlan_id);
570

    
571
    if (!vlan->slave)
572
        fprintf(stderr, "qemu: warning: adding a slave device to "
573
                        "an empty scatternet %i\n", vlan_id);
574

    
575
    if (!strcmp(devname, "keyboard"))
576
        return bt_keyboard_init(vlan);
577

    
578
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
579
    return 0;
580
}
581

    
582
static int bt_parse(const char *opt)
583
{
584
    const char *endp, *p;
585
    int vlan;
586

    
587
    if (strstart(opt, "hci", &endp)) {
588
        if (!*endp || *endp == ',') {
589
            if (*endp)
590
                if (!strstart(endp, ",vlan=", 0))
591
                    opt = endp + 1;
592

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

    
611
            bt_vhci_add(vlan);
612
            return 0;
613
        }
614
    } else if (strstart(opt, "device:", &endp))
615
        return !bt_device_add(endp);
616

    
617
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
618
    return 1;
619
}
620

    
621
/***********************************************************/
622
/* QEMU Block devices */
623

    
624
#define HD_OPTS "media=disk"
625
#define CDROM_OPTS "media=cdrom"
626
#define FD_OPTS ""
627
#define PFLASH_OPTS ""
628
#define MTD_OPTS ""
629
#define SD_OPTS ""
630

    
631
static int drive_init_func(QemuOpts *opts, void *opaque)
632
{
633
    int *use_scsi = opaque;
634

    
635
    return drive_init(opts, *use_scsi) == NULL;
636
}
637

    
638
static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
639
{
640
    if (NULL == qemu_opt_get(opts, "snapshot")) {
641
        qemu_opt_set(opts, "snapshot", "on");
642
    }
643
    return 0;
644
}
645

    
646
static void default_drive(int enable, int snapshot, int use_scsi,
647
                          BlockInterfaceType type, int index,
648
                          const char *optstr)
649
{
650
    QemuOpts *opts;
651

    
652
    if (type == IF_DEFAULT) {
653
        type = use_scsi ? IF_SCSI : IF_IDE;
654
    }
655

    
656
    if (!enable || drive_get_by_index(type, index)) {
657
        return;
658
    }
659

    
660
    opts = drive_add(type, index, NULL, optstr);
661
    if (snapshot) {
662
        drive_enable_snapshot(opts, NULL);
663
    }
664
    if (!drive_init(opts, use_scsi)) {
665
        exit(1);
666
    }
667
}
668

    
669
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
670
{
671
    boot_set_handler = func;
672
    boot_set_opaque = opaque;
673
}
674

    
675
int qemu_boot_set(const char *boot_devices)
676
{
677
    if (!boot_set_handler) {
678
        return -EINVAL;
679
    }
680
    return boot_set_handler(boot_set_opaque, boot_devices);
681
}
682

    
683
static void validate_bootdevices(char *devices)
684
{
685
    /* We just do some generic consistency checks */
686
    const char *p;
687
    int bitmap = 0;
688

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

    
711
static void restore_boot_devices(void *opaque)
712
{
713
    char *standard_boot_devices = opaque;
714
    static int first = 1;
715

    
716
    /* Restore boot order and remove ourselves after the first boot */
717
    if (first) {
718
        first = 0;
719
        return;
720
    }
721

    
722
    qemu_boot_set(standard_boot_devices);
723

    
724
    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
725
    qemu_free(standard_boot_devices);
726
}
727

    
728
void add_boot_device_path(int32_t bootindex, DeviceState *dev,
729
                          const char *suffix)
730
{
731
    FWBootEntry *node, *i;
732

    
733
    if (bootindex < 0) {
734
        return;
735
    }
736

    
737
    assert(dev != NULL || suffix != NULL);
738

    
739
    node = qemu_mallocz(sizeof(FWBootEntry));
740
    node->bootindex = bootindex;
741
    node->suffix = suffix ? qemu_strdup(suffix) : NULL;
742
    node->dev = dev;
743

    
744
    QTAILQ_FOREACH(i, &fw_boot_order, link) {
745
        if (i->bootindex == bootindex) {
746
            fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
747
            exit(1);
748
        } else if (i->bootindex < bootindex) {
749
            continue;
750
        }
751
        QTAILQ_INSERT_BEFORE(i, node, link);
752
        return;
753
    }
754
    QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
755
}
756

    
757
/*
758
 * This function returns null terminated string that consist of new line
759
 * separated device pathes.
760
 *
761
 * memory pointed by "size" is assigned total length of the array in bytes
762
 *
763
 */
764
char *get_boot_devices_list(uint32_t *size)
765
{
766
    FWBootEntry *i;
767
    uint32_t total = 0;
768
    char *list = NULL;
769

    
770
    QTAILQ_FOREACH(i, &fw_boot_order, link) {
771
        char *devpath = NULL, *bootpath;
772
        int len;
773

    
774
        if (i->dev) {
775
            devpath = qdev_get_fw_dev_path(i->dev);
776
            assert(devpath);
777
        }
778

    
779
        if (i->suffix && devpath) {
780
            size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
781

    
782
            bootpath = qemu_malloc(bootpathlen);
783
            snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
784
            qemu_free(devpath);
785
        } else if (devpath) {
786
            bootpath = devpath;
787
        } else {
788
            bootpath = qemu_strdup(i->suffix);
789
            assert(bootpath);
790
        }
791

    
792
        if (total) {
793
            list[total-1] = '\n';
794
        }
795
        len = strlen(bootpath) + 1;
796
        list = qemu_realloc(list, total + len);
797
        memcpy(&list[total], bootpath, len);
798
        total += len;
799
        qemu_free(bootpath);
800
    }
801

    
802
    *size = total;
803

    
804
    return list;
805
}
806

    
807
static void numa_add(const char *optarg)
808
{
809
    char option[128];
810
    char *endptr;
811
    unsigned long long value, endvalue;
812
    int nodenr;
813

    
814
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
815
    if (!strcmp(option, "node")) {
816
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
817
            nodenr = nb_numa_nodes;
818
        } else {
819
            nodenr = strtoull(option, NULL, 10);
820
        }
821

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

    
860
static void smp_parse(const char *optarg)
861
{
862
    int smp, sockets = 0, threads = 0, cores = 0;
863
    char *endptr;
864
    char option[128];
865

    
866
    smp = strtoul(optarg, &endptr, 10);
867
    if (endptr != optarg) {
868
        if (*endptr == ',') {
869
            endptr++;
870
        }
871
    }
872
    if (get_param_value(option, 128, "sockets", endptr) != 0)
873
        sockets = strtoull(option, NULL, 10);
874
    if (get_param_value(option, 128, "cores", endptr) != 0)
875
        cores = strtoull(option, NULL, 10);
876
    if (get_param_value(option, 128, "threads", endptr) != 0)
877
        threads = strtoull(option, NULL, 10);
878
    if (get_param_value(option, 128, "maxcpus", endptr) != 0)
879
        max_cpus = strtoull(option, NULL, 10);
880

    
881
    /* compute missing values, prefer sockets over cores over threads */
882
    if (smp == 0 || sockets == 0) {
883
        sockets = sockets > 0 ? sockets : 1;
884
        cores = cores > 0 ? cores : 1;
885
        threads = threads > 0 ? threads : 1;
886
        if (smp == 0) {
887
            smp = cores * threads * sockets;
888
        }
889
    } else {
890
        if (cores == 0) {
891
            threads = threads > 0 ? threads : 1;
892
            cores = smp / (sockets * threads);
893
        } else {
894
            threads = smp / (cores * sockets);
895
        }
896
    }
897
    smp_cpus = smp;
898
    smp_cores = cores > 0 ? cores : 1;
899
    smp_threads = threads > 0 ? threads : 1;
900
    if (max_cpus == 0)
901
        max_cpus = smp_cpus;
902
}
903

    
904
/***********************************************************/
905
/* USB devices */
906

    
907
static int usb_device_add(const char *devname)
908
{
909
    const char *p;
910
    USBDevice *dev = NULL;
911

    
912
    if (!usb_enabled)
913
        return -1;
914

    
915
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
916
    dev = usbdevice_create(devname);
917
    if (dev)
918
        goto done;
919

    
920
    /* the other ones */
921
    if (strstart(devname, "host:", &p)) {
922
        dev = usb_host_device_open(p);
923
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
924
        dev = usb_bt_init(devname[2] ? hci_init(p) :
925
                        bt_new_hci(qemu_find_bt_vlan(0)));
926
    } else {
927
        return -1;
928
    }
929
    if (!dev)
930
        return -1;
931

    
932
done:
933
    return 0;
934
}
935

    
936
static int usb_device_del(const char *devname)
937
{
938
    int bus_num, addr;
939
    const char *p;
940

    
941
    if (strstart(devname, "host:", &p))
942
        return usb_host_device_close(p);
943

    
944
    if (!usb_enabled)
945
        return -1;
946

    
947
    p = strchr(devname, '.');
948
    if (!p)
949
        return -1;
950
    bus_num = strtoul(devname, NULL, 0);
951
    addr = strtoul(p + 1, NULL, 0);
952

    
953
    return usb_device_delete_addr(bus_num, addr);
954
}
955

    
956
static int usb_parse(const char *cmdline)
957
{
958
    int r;
959
    r = usb_device_add(cmdline);
960
    if (r < 0) {
961
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
962
    }
963
    return r;
964
}
965

    
966
void do_usb_add(Monitor *mon, const QDict *qdict)
967
{
968
    const char *devname = qdict_get_str(qdict, "devname");
969
    if (usb_device_add(devname) < 0) {
970
        error_report("could not add USB device '%s'", devname);
971
    }
972
}
973

    
974
void do_usb_del(Monitor *mon, const QDict *qdict)
975
{
976
    const char *devname = qdict_get_str(qdict, "devname");
977
    if (usb_device_del(devname) < 0) {
978
        error_report("could not delete USB device '%s'", devname);
979
    }
980
}
981

    
982
/***********************************************************/
983
/* PCMCIA/Cardbus */
984

    
985
static struct pcmcia_socket_entry_s {
986
    PCMCIASocket *socket;
987
    struct pcmcia_socket_entry_s *next;
988
} *pcmcia_sockets = 0;
989

    
990
void pcmcia_socket_register(PCMCIASocket *socket)
991
{
992
    struct pcmcia_socket_entry_s *entry;
993

    
994
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
995
    entry->socket = socket;
996
    entry->next = pcmcia_sockets;
997
    pcmcia_sockets = entry;
998
}
999

    
1000
void pcmcia_socket_unregister(PCMCIASocket *socket)
1001
{
1002
    struct pcmcia_socket_entry_s *entry, **ptr;
1003

    
1004
    ptr = &pcmcia_sockets;
1005
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1006
        if (entry->socket == socket) {
1007
            *ptr = entry->next;
1008
            qemu_free(entry);
1009
        }
1010
}
1011

    
1012
void pcmcia_info(Monitor *mon)
1013
{
1014
    struct pcmcia_socket_entry_s *iter;
1015

    
1016
    if (!pcmcia_sockets)
1017
        monitor_printf(mon, "No PCMCIA sockets\n");
1018

    
1019
    for (iter = pcmcia_sockets; iter; iter = iter->next)
1020
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1021
                       iter->socket->attached ? iter->socket->card_string :
1022
                       "Empty");
1023
}
1024

    
1025
/***********************************************************/
1026
/* machine registration */
1027

    
1028
static QEMUMachine *first_machine = NULL;
1029
QEMUMachine *current_machine = NULL;
1030

    
1031
int qemu_register_machine(QEMUMachine *m)
1032
{
1033
    QEMUMachine **pm;
1034
    pm = &first_machine;
1035
    while (*pm != NULL)
1036
        pm = &(*pm)->next;
1037
    m->next = NULL;
1038
    *pm = m;
1039
    return 0;
1040
}
1041

    
1042
static QEMUMachine *find_machine(const char *name)
1043
{
1044
    QEMUMachine *m;
1045

    
1046
    for(m = first_machine; m != NULL; m = m->next) {
1047
        if (!strcmp(m->name, name))
1048
            return m;
1049
        if (m->alias && !strcmp(m->alias, name))
1050
            return m;
1051
    }
1052
    return NULL;
1053
}
1054

    
1055
static QEMUMachine *find_default_machine(void)
1056
{
1057
    QEMUMachine *m;
1058

    
1059
    for(m = first_machine; m != NULL; m = m->next) {
1060
        if (m->is_default) {
1061
            return m;
1062
        }
1063
    }
1064
    return NULL;
1065
}
1066

    
1067
/***********************************************************/
1068
/* main execution loop */
1069

    
1070
static void gui_update(void *opaque)
1071
{
1072
    uint64_t interval = GUI_REFRESH_INTERVAL;
1073
    DisplayState *ds = opaque;
1074
    DisplayChangeListener *dcl = ds->listeners;
1075

    
1076
    qemu_flush_coalesced_mmio_buffer();
1077
    dpy_refresh(ds);
1078

    
1079
    while (dcl != NULL) {
1080
        if (dcl->gui_timer_interval &&
1081
            dcl->gui_timer_interval < interval)
1082
            interval = dcl->gui_timer_interval;
1083
        dcl = dcl->next;
1084
    }
1085
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1086
}
1087

    
1088
static void nographic_update(void *opaque)
1089
{
1090
    uint64_t interval = GUI_REFRESH_INTERVAL;
1091

    
1092
    qemu_flush_coalesced_mmio_buffer();
1093
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
1094
}
1095

    
1096
struct vm_change_state_entry {
1097
    VMChangeStateHandler *cb;
1098
    void *opaque;
1099
    QLIST_ENTRY (vm_change_state_entry) entries;
1100
};
1101

    
1102
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1103

    
1104
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1105
                                                     void *opaque)
1106
{
1107
    VMChangeStateEntry *e;
1108

    
1109
    e = qemu_mallocz(sizeof (*e));
1110

    
1111
    e->cb = cb;
1112
    e->opaque = opaque;
1113
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1114
    return e;
1115
}
1116

    
1117
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1118
{
1119
    QLIST_REMOVE (e, entries);
1120
    qemu_free (e);
1121
}
1122

    
1123
void vm_state_notify(int running, int reason)
1124
{
1125
    VMChangeStateEntry *e;
1126

    
1127
    trace_vm_state_notify(running, reason);
1128

    
1129
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1130
        e->cb(e->opaque, running, reason);
1131
    }
1132
}
1133

    
1134
void vm_start(void)
1135
{
1136
    if (!vm_running) {
1137
        cpu_enable_ticks();
1138
        vm_running = 1;
1139
        vm_state_notify(1, 0);
1140
        resume_all_vcpus();
1141
        monitor_protocol_event(QEVENT_RESUME, NULL);
1142
    }
1143
}
1144

    
1145
/* reset/shutdown handler */
1146

    
1147
typedef struct QEMUResetEntry {
1148
    QTAILQ_ENTRY(QEMUResetEntry) entry;
1149
    QEMUResetHandler *func;
1150
    void *opaque;
1151
} QEMUResetEntry;
1152

    
1153
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1154
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
1155
static int reset_requested;
1156
static int shutdown_requested, shutdown_signal = -1;
1157
static pid_t shutdown_pid;
1158
static int powerdown_requested;
1159
static int debug_requested;
1160
static int vmstop_requested;
1161

    
1162
int qemu_shutdown_requested(void)
1163
{
1164
    int r = shutdown_requested;
1165
    shutdown_requested = 0;
1166
    return r;
1167
}
1168

    
1169
void qemu_kill_report(void)
1170
{
1171
    if (shutdown_signal != -1) {
1172
        fprintf(stderr, "Got signal %d from pid %d\n",
1173
                         shutdown_signal, shutdown_pid);
1174
        shutdown_signal = -1;
1175
    }
1176
}
1177

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

    
1185
int qemu_powerdown_requested(void)
1186
{
1187
    int r = powerdown_requested;
1188
    powerdown_requested = 0;
1189
    return r;
1190
}
1191

    
1192
static int qemu_debug_requested(void)
1193
{
1194
    int r = debug_requested;
1195
    debug_requested = 0;
1196
    return r;
1197
}
1198

    
1199
static int qemu_vmstop_requested(void)
1200
{
1201
    int r = vmstop_requested;
1202
    vmstop_requested = 0;
1203
    return r;
1204
}
1205

    
1206
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1207
{
1208
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1209

    
1210
    re->func = func;
1211
    re->opaque = opaque;
1212
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1213
}
1214

    
1215
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1216
{
1217
    QEMUResetEntry *re;
1218

    
1219
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
1220
        if (re->func == func && re->opaque == opaque) {
1221
            QTAILQ_REMOVE(&reset_handlers, re, entry);
1222
            qemu_free(re);
1223
            return;
1224
        }
1225
    }
1226
}
1227

    
1228
void qemu_system_reset(void)
1229
{
1230
    QEMUResetEntry *re, *nre;
1231

    
1232
    /* reset all devices */
1233
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1234
        re->func(re->opaque);
1235
    }
1236
    monitor_protocol_event(QEVENT_RESET, NULL);
1237
    cpu_synchronize_all_post_reset();
1238
}
1239

    
1240
void qemu_system_reset_request(void)
1241
{
1242
    if (no_reboot) {
1243
        shutdown_requested = 1;
1244
    } else {
1245
        reset_requested = 1;
1246
    }
1247
    cpu_stop_current();
1248
    qemu_notify_event();
1249
}
1250

    
1251
void qemu_system_killed(int signal, pid_t pid)
1252
{
1253
    shutdown_signal = signal;
1254
    shutdown_pid = pid;
1255
    qemu_system_shutdown_request();
1256
}
1257

    
1258
void qemu_system_shutdown_request(void)
1259
{
1260
    shutdown_requested = 1;
1261
    qemu_notify_event();
1262
}
1263

    
1264
void qemu_system_powerdown_request(void)
1265
{
1266
    powerdown_requested = 1;
1267
    qemu_notify_event();
1268
}
1269

    
1270
void qemu_system_debug_request(void)
1271
{
1272
    debug_requested = 1;
1273
    qemu_notify_event();
1274
}
1275

    
1276
void qemu_system_vmstop_request(int reason)
1277
{
1278
    vmstop_requested = reason;
1279
    qemu_notify_event();
1280
}
1281

    
1282
void main_loop_wait(int nonblocking)
1283
{
1284
    fd_set rfds, wfds, xfds;
1285
    int ret, nfds;
1286
    struct timeval tv;
1287
    int timeout;
1288

    
1289
    if (nonblocking)
1290
        timeout = 0;
1291
    else {
1292
        timeout = qemu_calculate_timeout();
1293
        qemu_bh_update_timeout(&timeout);
1294
    }
1295

    
1296
    os_host_main_loop_wait(&timeout);
1297

    
1298
    tv.tv_sec = timeout / 1000;
1299
    tv.tv_usec = (timeout % 1000) * 1000;
1300

    
1301
    /* poll any events */
1302
    /* XXX: separate device handlers from system ones */
1303
    nfds = -1;
1304
    FD_ZERO(&rfds);
1305
    FD_ZERO(&wfds);
1306
    FD_ZERO(&xfds);
1307
    qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
1308
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1309

    
1310
    qemu_mutex_unlock_iothread();
1311
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1312
    qemu_mutex_lock_iothread();
1313

    
1314
    qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
1315
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1316

    
1317
    qemu_run_all_timers();
1318

    
1319
    /* Check bottom-halves last in case any of the earlier events triggered
1320
       them.  */
1321
    qemu_bh_poll();
1322

    
1323
}
1324

    
1325
#ifndef CONFIG_IOTHREAD
1326
static int vm_request_pending(void)
1327
{
1328
    return powerdown_requested ||
1329
           reset_requested ||
1330
           shutdown_requested ||
1331
           debug_requested ||
1332
           vmstop_requested;
1333
}
1334
#endif
1335

    
1336
qemu_irq qemu_system_powerdown;
1337

    
1338
static void main_loop(void)
1339
{
1340
    bool nonblocking = false;
1341
#ifdef CONFIG_PROFILER
1342
    int64_t ti;
1343
#endif
1344
    int r;
1345

    
1346
    qemu_main_loop_start();
1347

    
1348
    for (;;) {
1349
#ifndef CONFIG_IOTHREAD
1350
        nonblocking = cpu_exec_all();
1351
        if (vm_request_pending()) {
1352
            nonblocking = true;
1353
        }
1354
#endif
1355
#ifdef CONFIG_PROFILER
1356
        ti = profile_getclock();
1357
#endif
1358
        main_loop_wait(nonblocking);
1359
#ifdef CONFIG_PROFILER
1360
        dev_time += profile_getclock() - ti;
1361
#endif
1362

    
1363
        if (qemu_debug_requested()) {
1364
            vm_stop(VMSTOP_DEBUG);
1365
        }
1366
        if (qemu_shutdown_requested()) {
1367
            qemu_kill_report();
1368
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1369
            if (no_shutdown) {
1370
                vm_stop(VMSTOP_SHUTDOWN);
1371
                no_shutdown = 0;
1372
            } else
1373
                break;
1374
        }
1375
        if (qemu_reset_requested()) {
1376
            pause_all_vcpus();
1377
            cpu_synchronize_all_states();
1378
            qemu_system_reset();
1379
            resume_all_vcpus();
1380
        }
1381
        if (qemu_powerdown_requested()) {
1382
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1383
            qemu_irq_raise(qemu_system_powerdown);
1384
        }
1385
        if ((r = qemu_vmstop_requested())) {
1386
            vm_stop(r);
1387
        }
1388
    }
1389
    bdrv_close_all();
1390
    pause_all_vcpus();
1391
}
1392

    
1393
static void version(void)
1394
{
1395
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1396
}
1397

    
1398
static void help(int exitcode)
1399
{
1400
    const char *options_help =
1401
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1402
        opt_help
1403
#define DEFHEADING(text) stringify(text) "\n"
1404
#include "qemu-options.def"
1405
#undef DEF
1406
#undef DEFHEADING
1407
#undef GEN_DOCS
1408
        ;
1409
    version();
1410
    printf("usage: %s [options] [disk_image]\n"
1411
           "\n"
1412
           "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1413
           "\n"
1414
           "%s\n"
1415
           "During emulation, the following keys are useful:\n"
1416
           "ctrl-alt-f      toggle full screen\n"
1417
           "ctrl-alt-n      switch to virtual console 'n'\n"
1418
           "ctrl-alt        toggle mouse and keyboard grab\n"
1419
           "\n"
1420
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
1421
           "qemu",
1422
           options_help);
1423
    exit(exitcode);
1424
}
1425

    
1426
#define HAS_ARG 0x0001
1427

    
1428
typedef struct QEMUOption {
1429
    const char *name;
1430
    int flags;
1431
    int index;
1432
    uint32_t arch_mask;
1433
} QEMUOption;
1434

    
1435
static const QEMUOption qemu_options[] = {
1436
    { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1437
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1438
    { option, opt_arg, opt_enum, arch_mask },
1439
#define DEFHEADING(text)
1440
#include "qemu-options.def"
1441
#undef DEF
1442
#undef DEFHEADING
1443
#undef GEN_DOCS
1444
    { NULL },
1445
};
1446
static void select_vgahw (const char *p)
1447
{
1448
    const char *opts;
1449

    
1450
    default_vga = 0;
1451
    vga_interface_type = VGA_NONE;
1452
    if (strstart(p, "std", &opts)) {
1453
        vga_interface_type = VGA_STD;
1454
    } else if (strstart(p, "cirrus", &opts)) {
1455
        vga_interface_type = VGA_CIRRUS;
1456
    } else if (strstart(p, "vmware", &opts)) {
1457
        vga_interface_type = VGA_VMWARE;
1458
    } else if (strstart(p, "xenfb", &opts)) {
1459
        vga_interface_type = VGA_XENFB;
1460
    } else if (strstart(p, "qxl", &opts)) {
1461
        vga_interface_type = VGA_QXL;
1462
    } else if (!strstart(p, "none", &opts)) {
1463
    invalid_vga:
1464
        fprintf(stderr, "Unknown vga type: %s\n", p);
1465
        exit(1);
1466
    }
1467
    while (*opts) {
1468
        const char *nextopt;
1469

    
1470
        if (strstart(opts, ",retrace=", &nextopt)) {
1471
            opts = nextopt;
1472
            if (strstart(opts, "dumb", &nextopt))
1473
                vga_retrace_method = VGA_RETRACE_DUMB;
1474
            else if (strstart(opts, "precise", &nextopt))
1475
                vga_retrace_method = VGA_RETRACE_PRECISE;
1476
            else goto invalid_vga;
1477
        } else goto invalid_vga;
1478
        opts = nextopt;
1479
    }
1480
}
1481

    
1482
static DisplayType select_display(const char *p)
1483
{
1484
    const char *opts;
1485
    DisplayType display = DT_DEFAULT;
1486

    
1487
    if (strstart(p, "sdl", &opts)) {
1488
#ifdef CONFIG_SDL
1489
        display = DT_SDL;
1490
        while (*opts) {
1491
            const char *nextopt;
1492

    
1493
            if (strstart(opts, ",frame=", &nextopt)) {
1494
                opts = nextopt;
1495
                if (strstart(opts, "on", &nextopt)) {
1496
                    no_frame = 0;
1497
                } else if (strstart(opts, "off", &nextopt)) {
1498
                    no_frame = 1;
1499
                } else {
1500
                    goto invalid_sdl_args;
1501
                }
1502
            } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1503
                opts = nextopt;
1504
                if (strstart(opts, "on", &nextopt)) {
1505
                    alt_grab = 1;
1506
                } else if (strstart(opts, "off", &nextopt)) {
1507
                    alt_grab = 0;
1508
                } else {
1509
                    goto invalid_sdl_args;
1510
                }
1511
            } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1512
                opts = nextopt;
1513
                if (strstart(opts, "on", &nextopt)) {
1514
                    ctrl_grab = 1;
1515
                } else if (strstart(opts, "off", &nextopt)) {
1516
                    ctrl_grab = 0;
1517
                } else {
1518
                    goto invalid_sdl_args;
1519
                }
1520
            } else if (strstart(opts, ",window_close=", &nextopt)) {
1521
                opts = nextopt;
1522
                if (strstart(opts, "on", &nextopt)) {
1523
                    no_quit = 0;
1524
                } else if (strstart(opts, "off", &nextopt)) {
1525
                    no_quit = 1;
1526
                } else {
1527
                    goto invalid_sdl_args;
1528
                }
1529
            } else {
1530
            invalid_sdl_args:
1531
                fprintf(stderr, "Invalid SDL option string: %s\n", p);
1532
                exit(1);
1533
            }
1534
            opts = nextopt;
1535
        }
1536
#else
1537
        fprintf(stderr, "SDL support is disabled\n");
1538
        exit(1);
1539
#endif
1540
    } else if (strstart(p, "vnc", &opts)) {
1541
#ifdef CONFIG_VNC
1542
        display_remote++;
1543

    
1544
        if (*opts) {
1545
            const char *nextopt;
1546

    
1547
            if (strstart(opts, "=", &nextopt)) {
1548
                vnc_display = nextopt;
1549
            }
1550
        }
1551
        if (!vnc_display) {
1552
            fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1553
            exit(1);
1554
        }
1555
#else
1556
        fprintf(stderr, "VNC support is disabled\n");
1557
        exit(1);
1558
#endif
1559
    } else if (strstart(p, "curses", &opts)) {
1560
#ifdef CONFIG_CURSES
1561
        display = DT_CURSES;
1562
#else
1563
        fprintf(stderr, "Curses support is disabled\n");
1564
        exit(1);
1565
#endif
1566
    } else if (strstart(p, "none", &opts)) {
1567
        display = DT_NONE;
1568
    } else {
1569
        fprintf(stderr, "Unknown display type: %s\n", p);
1570
        exit(1);
1571
    }
1572

    
1573
    return display;
1574
}
1575

    
1576
static int balloon_parse(const char *arg)
1577
{
1578
    QemuOpts *opts;
1579

    
1580
    if (strcmp(arg, "none") == 0) {
1581
        return 0;
1582
    }
1583

    
1584
    if (!strncmp(arg, "virtio", 6)) {
1585
        if (arg[6] == ',') {
1586
            /* have params -> parse them */
1587
            opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1588
            if (!opts)
1589
                return  -1;
1590
        } else {
1591
            /* create empty opts */
1592
            opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1593
        }
1594
        qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1595
        return 0;
1596
    }
1597

    
1598
    return -1;
1599
}
1600

    
1601
char *qemu_find_file(int type, const char *name)
1602
{
1603
    int len;
1604
    const char *subdir;
1605
    char *buf;
1606

    
1607
    /* If name contains path separators then try it as a straight path.  */
1608
    if ((strchr(name, '/') || strchr(name, '\\'))
1609
        && access(name, R_OK) == 0) {
1610
        return qemu_strdup(name);
1611
    }
1612
    switch (type) {
1613
    case QEMU_FILE_TYPE_BIOS:
1614
        subdir = "";
1615
        break;
1616
    case QEMU_FILE_TYPE_KEYMAP:
1617
        subdir = "keymaps/";
1618
        break;
1619
    default:
1620
        abort();
1621
    }
1622
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1623
    buf = qemu_mallocz(len);
1624
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1625
    if (access(buf, R_OK)) {
1626
        qemu_free(buf);
1627
        return NULL;
1628
    }
1629
    return buf;
1630
}
1631

    
1632
static int device_help_func(QemuOpts *opts, void *opaque)
1633
{
1634
    return qdev_device_help(opts);
1635
}
1636

    
1637
static int device_init_func(QemuOpts *opts, void *opaque)
1638
{
1639
    DeviceState *dev;
1640

    
1641
    dev = qdev_device_add(opts);
1642
    if (!dev)
1643
        return -1;
1644
    return 0;
1645
}
1646

    
1647
static int chardev_init_func(QemuOpts *opts, void *opaque)
1648
{
1649
    CharDriverState *chr;
1650

    
1651
    chr = qemu_chr_open_opts(opts, NULL);
1652
    if (!chr)
1653
        return -1;
1654
    return 0;
1655
}
1656

    
1657
#ifdef CONFIG_VIRTFS
1658
static int fsdev_init_func(QemuOpts *opts, void *opaque)
1659
{
1660
    int ret;
1661
    ret = qemu_fsdev_add(opts);
1662

    
1663
    return ret;
1664
}
1665
#endif
1666

    
1667
static int mon_init_func(QemuOpts *opts, void *opaque)
1668
{
1669
    CharDriverState *chr;
1670
    const char *chardev;
1671
    const char *mode;
1672
    int flags;
1673

    
1674
    mode = qemu_opt_get(opts, "mode");
1675
    if (mode == NULL) {
1676
        mode = "readline";
1677
    }
1678
    if (strcmp(mode, "readline") == 0) {
1679
        flags = MONITOR_USE_READLINE;
1680
    } else if (strcmp(mode, "control") == 0) {
1681
        flags = MONITOR_USE_CONTROL;
1682
    } else {
1683
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1684
        exit(1);
1685
    }
1686

    
1687
    if (qemu_opt_get_bool(opts, "pretty", 0))
1688
        flags |= MONITOR_USE_PRETTY;
1689

    
1690
    if (qemu_opt_get_bool(opts, "default", 0))
1691
        flags |= MONITOR_IS_DEFAULT;
1692

    
1693
    chardev = qemu_opt_get(opts, "chardev");
1694
    chr = qemu_chr_find(chardev);
1695
    if (chr == NULL) {
1696
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1697
        exit(1);
1698
    }
1699

    
1700
    monitor_init(chr, flags);
1701
    return 0;
1702
}
1703

    
1704
static void monitor_parse(const char *optarg, const char *mode)
1705
{
1706
    static int monitor_device_index = 0;
1707
    QemuOpts *opts;
1708
    const char *p;
1709
    char label[32];
1710
    int def = 0;
1711

    
1712
    if (strstart(optarg, "chardev:", &p)) {
1713
        snprintf(label, sizeof(label), "%s", p);
1714
    } else {
1715
        snprintf(label, sizeof(label), "compat_monitor%d",
1716
                 monitor_device_index);
1717
        if (monitor_device_index == 0) {
1718
            def = 1;
1719
        }
1720
        opts = qemu_chr_parse_compat(label, optarg);
1721
        if (!opts) {
1722
            fprintf(stderr, "parse error: %s\n", optarg);
1723
            exit(1);
1724
        }
1725
    }
1726

    
1727
    opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1728
    if (!opts) {
1729
        fprintf(stderr, "duplicate chardev: %s\n", label);
1730
        exit(1);
1731
    }
1732
    qemu_opt_set(opts, "mode", mode);
1733
    qemu_opt_set(opts, "chardev", label);
1734
    if (def)
1735
        qemu_opt_set(opts, "default", "on");
1736
    monitor_device_index++;
1737
}
1738

    
1739
struct device_config {
1740
    enum {
1741
        DEV_USB,       /* -usbdevice     */
1742
        DEV_BT,        /* -bt            */
1743
        DEV_SERIAL,    /* -serial        */
1744
        DEV_PARALLEL,  /* -parallel      */
1745
        DEV_VIRTCON,   /* -virtioconsole */
1746
        DEV_DEBUGCON,  /* -debugcon */
1747
    } type;
1748
    const char *cmdline;
1749
    QTAILQ_ENTRY(device_config) next;
1750
};
1751
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1752

    
1753
static void add_device_config(int type, const char *cmdline)
1754
{
1755
    struct device_config *conf;
1756

    
1757
    conf = qemu_mallocz(sizeof(*conf));
1758
    conf->type = type;
1759
    conf->cmdline = cmdline;
1760
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1761
}
1762

    
1763
static int foreach_device_config(int type, int (*func)(const char *cmdline))
1764
{
1765
    struct device_config *conf;
1766
    int rc;
1767

    
1768
    QTAILQ_FOREACH(conf, &device_configs, next) {
1769
        if (conf->type != type)
1770
            continue;
1771
        rc = func(conf->cmdline);
1772
        if (0 != rc)
1773
            return rc;
1774
    }
1775
    return 0;
1776
}
1777

    
1778
static int serial_parse(const char *devname)
1779
{
1780
    static int index = 0;
1781
    char label[32];
1782

    
1783
    if (strcmp(devname, "none") == 0)
1784
        return 0;
1785
    if (index == MAX_SERIAL_PORTS) {
1786
        fprintf(stderr, "qemu: too many serial ports\n");
1787
        exit(1);
1788
    }
1789
    snprintf(label, sizeof(label), "serial%d", index);
1790
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
1791
    if (!serial_hds[index]) {
1792
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1793
                devname, strerror(errno));
1794
        return -1;
1795
    }
1796
    index++;
1797
    return 0;
1798
}
1799

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

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

    
1822
static int virtcon_parse(const char *devname)
1823
{
1824
    QemuOptsList *device = qemu_find_opts("device");
1825
    static int index = 0;
1826
    char label[32];
1827
    QemuOpts *bus_opts, *dev_opts;
1828

    
1829
    if (strcmp(devname, "none") == 0)
1830
        return 0;
1831
    if (index == MAX_VIRTIO_CONSOLES) {
1832
        fprintf(stderr, "qemu: too many virtio consoles\n");
1833
        exit(1);
1834
    }
1835

    
1836
    bus_opts = qemu_opts_create(device, NULL, 0);
1837
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
1838

    
1839
    dev_opts = qemu_opts_create(device, NULL, 0);
1840
    qemu_opt_set(dev_opts, "driver", "virtconsole");
1841

    
1842
    snprintf(label, sizeof(label), "virtcon%d", index);
1843
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1844
    if (!virtcon_hds[index]) {
1845
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1846
                devname, strerror(errno));
1847
        return -1;
1848
    }
1849
    qemu_opt_set(dev_opts, "chardev", label);
1850

    
1851
    index++;
1852
    return 0;
1853
}
1854

    
1855
static int debugcon_parse(const char *devname)
1856
{   
1857
    QemuOpts *opts;
1858

    
1859
    if (!qemu_chr_open("debugcon", devname, NULL)) {
1860
        exit(1);
1861
    }
1862
    opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1863
    if (!opts) {
1864
        fprintf(stderr, "qemu: already have a debugcon device\n");
1865
        exit(1);
1866
    }
1867
    qemu_opt_set(opts, "driver", "isa-debugcon");
1868
    qemu_opt_set(opts, "chardev", "debugcon");
1869
    return 0;
1870
}
1871

    
1872
void qemu_add_exit_notifier(Notifier *notify)
1873
{
1874
    notifier_list_add(&exit_notifiers, notify);
1875
}
1876

    
1877
void qemu_remove_exit_notifier(Notifier *notify)
1878
{
1879
    notifier_list_remove(&exit_notifiers, notify);
1880
}
1881

    
1882
static void qemu_run_exit_notifiers(void)
1883
{
1884
    notifier_list_notify(&exit_notifiers);
1885
}
1886

    
1887
void qemu_add_machine_init_done_notifier(Notifier *notify)
1888
{
1889
    notifier_list_add(&machine_init_done_notifiers, notify);
1890
}
1891

    
1892
static void qemu_run_machine_init_done_notifiers(void)
1893
{
1894
    notifier_list_notify(&machine_init_done_notifiers);
1895
}
1896

    
1897
static const QEMUOption *lookup_opt(int argc, char **argv,
1898
                                    const char **poptarg, int *poptind)
1899
{
1900
    const QEMUOption *popt;
1901
    int optind = *poptind;
1902
    char *r = argv[optind];
1903
    const char *optarg;
1904

    
1905
    loc_set_cmdline(argv, optind, 1);
1906
    optind++;
1907
    /* Treat --foo the same as -foo.  */
1908
    if (r[1] == '-')
1909
        r++;
1910
    popt = qemu_options;
1911
    for(;;) {
1912
        if (!popt->name) {
1913
            error_report("invalid option");
1914
            exit(1);
1915
        }
1916
        if (!strcmp(popt->name, r + 1))
1917
            break;
1918
        popt++;
1919
    }
1920
    if (popt->flags & HAS_ARG) {
1921
        if (optind >= argc) {
1922
            error_report("requires an argument");
1923
            exit(1);
1924
        }
1925
        optarg = argv[optind++];
1926
        loc_set_cmdline(argv, optind - 2, 2);
1927
    } else {
1928
        optarg = NULL;
1929
    }
1930

    
1931
    *poptarg = optarg;
1932
    *poptind = optind;
1933

    
1934
    return popt;
1935
}
1936

    
1937
int main(int argc, char **argv, char **envp)
1938
{
1939
    const char *gdbstub_dev = NULL;
1940
    int i;
1941
    int snapshot, linux_boot;
1942
    const char *icount_option = NULL;
1943
    const char *initrd_filename;
1944
    const char *kernel_filename, *kernel_cmdline;
1945
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1946
    DisplayState *ds;
1947
    DisplayChangeListener *dcl;
1948
    int cyls, heads, secs, translation;
1949
    QemuOpts *hda_opts = NULL, *opts;
1950
    QemuOptsList *olist;
1951
    int optind;
1952
    const char *optarg;
1953
    const char *loadvm = NULL;
1954
    QEMUMachine *machine;
1955
    const char *cpu_model;
1956
    int tb_size;
1957
    const char *pid_file = NULL;
1958
    const char *incoming = NULL;
1959
#ifdef CONFIG_VNC
1960
    int show_vnc_port = 0;
1961
#endif
1962
    int defconfig = 1;
1963
    const char *trace_file = NULL;
1964

    
1965
    atexit(qemu_run_exit_notifiers);
1966
    error_set_progname(argv[0]);
1967

    
1968
    init_clocks();
1969

    
1970
    qemu_cache_utils_init(envp);
1971

    
1972
    QLIST_INIT (&vm_change_state_head);
1973
    os_setup_early_signal_handling();
1974

    
1975
    module_call_init(MODULE_INIT_MACHINE);
1976
    machine = find_default_machine();
1977
    cpu_model = NULL;
1978
    initrd_filename = NULL;
1979
    ram_size = 0;
1980
    snapshot = 0;
1981
    kernel_filename = NULL;
1982
    kernel_cmdline = "";
1983
    cyls = heads = secs = 0;
1984
    translation = BIOS_ATA_TRANSLATION_AUTO;
1985

    
1986
    for (i = 0; i < MAX_NODES; i++) {
1987
        node_mem[i] = 0;
1988
        node_cpumask[i] = 0;
1989
    }
1990

    
1991
    nb_numa_nodes = 0;
1992
    nb_nics = 0;
1993

    
1994
    tb_size = 0;
1995
    autostart= 1;
1996

    
1997
    /* first pass of option parsing */
1998
    optind = 1;
1999
    while (optind < argc) {
2000
        if (argv[optind][0] != '-') {
2001
            /* disk image */
2002
            optind++;
2003
            continue;
2004
        } else {
2005
            const QEMUOption *popt;
2006

    
2007
            popt = lookup_opt(argc, argv, &optarg, &optind);
2008
            switch (popt->index) {
2009
            case QEMU_OPTION_nodefconfig:
2010
                defconfig=0;
2011
                break;
2012
            }
2013
        }
2014
    }
2015

    
2016
    if (defconfig) {
2017
        int ret;
2018

    
2019
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2020
        if (ret < 0 && ret != -ENOENT) {
2021
            exit(1);
2022
        }
2023

    
2024
        ret = qemu_read_config_file(arch_config_name);
2025
        if (ret < 0 && ret != -ENOENT) {
2026
            exit(1);
2027
        }
2028
    }
2029
    cpudef_init();
2030

    
2031
    /* second pass of option parsing */
2032
    optind = 1;
2033
    for(;;) {
2034
        if (optind >= argc)
2035
            break;
2036
        if (argv[optind][0] != '-') {
2037
            hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2038
        } else {
2039
            const QEMUOption *popt;
2040

    
2041
            popt = lookup_opt(argc, argv, &optarg, &optind);
2042
            if (!(popt->arch_mask & arch_type)) {
2043
                printf("Option %s not supported for this target\n", popt->name);
2044
                exit(1);
2045
            }
2046
            switch(popt->index) {
2047
            case QEMU_OPTION_M:
2048
                machine = find_machine(optarg);
2049
                if (!machine) {
2050
                    QEMUMachine *m;
2051
                    printf("Supported machines are:\n");
2052
                    for(m = first_machine; m != NULL; m = m->next) {
2053
                        if (m->alias)
2054
                            printf("%-10s %s (alias of %s)\n",
2055
                                   m->alias, m->desc, m->name);
2056
                        printf("%-10s %s%s\n",
2057
                               m->name, m->desc,
2058
                               m->is_default ? " (default)" : "");
2059
                    }
2060
                    exit(*optarg != '?');
2061
                }
2062
                break;
2063
            case QEMU_OPTION_cpu:
2064
                /* hw initialization will check this */
2065
                if (*optarg == '?') {
2066
                    list_cpus(stdout, &fprintf, optarg);
2067
                    exit(0);
2068
                } else {
2069
                    cpu_model = optarg;
2070
                }
2071
                break;
2072
            case QEMU_OPTION_initrd:
2073
                initrd_filename = optarg;
2074
                break;
2075
            case QEMU_OPTION_hda:
2076
                {
2077
                    char buf[256];
2078
                    if (cyls == 0)
2079
                        snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2080
                    else
2081
                        snprintf(buf, sizeof(buf),
2082
                                 "%s,cyls=%d,heads=%d,secs=%d%s",
2083
                                 HD_OPTS , cyls, heads, secs,
2084
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
2085
                                 ",trans=lba" :
2086
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
2087
                                 ",trans=none" : "");
2088
                    drive_add(IF_DEFAULT, 0, optarg, buf);
2089
                    break;
2090
                }
2091
            case QEMU_OPTION_hdb:
2092
            case QEMU_OPTION_hdc:
2093
            case QEMU_OPTION_hdd:
2094
                drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2095
                          HD_OPTS);
2096
                break;
2097
            case QEMU_OPTION_drive:
2098
                drive_def(optarg);
2099
                break;
2100
            case QEMU_OPTION_set:
2101
                if (qemu_set_option(optarg) != 0)
2102
                    exit(1);
2103
                break;
2104
            case QEMU_OPTION_global:
2105
                if (qemu_global_option(optarg) != 0)
2106
                    exit(1);
2107
                break;
2108
            case QEMU_OPTION_mtdblock:
2109
                drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2110
                break;
2111
            case QEMU_OPTION_sd:
2112
                drive_add(IF_SD, 0, optarg, SD_OPTS);
2113
                break;
2114
            case QEMU_OPTION_pflash:
2115
                drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2116
                break;
2117
            case QEMU_OPTION_snapshot:
2118
                snapshot = 1;
2119
                break;
2120
            case QEMU_OPTION_hdachs:
2121
                {
2122
                    const char *p;
2123
                    p = optarg;
2124
                    cyls = strtol(p, (char **)&p, 0);
2125
                    if (cyls < 1 || cyls > 16383)
2126
                        goto chs_fail;
2127
                    if (*p != ',')
2128
                        goto chs_fail;
2129
                    p++;
2130
                    heads = strtol(p, (char **)&p, 0);
2131
                    if (heads < 1 || heads > 16)
2132
                        goto chs_fail;
2133
                    if (*p != ',')
2134
                        goto chs_fail;
2135
                    p++;
2136
                    secs = strtol(p, (char **)&p, 0);
2137
                    if (secs < 1 || secs > 63)
2138
                        goto chs_fail;
2139
                    if (*p == ',') {
2140
                        p++;
2141
                        if (!strcmp(p, "none"))
2142
                            translation = BIOS_ATA_TRANSLATION_NONE;
2143
                        else if (!strcmp(p, "lba"))
2144
                            translation = BIOS_ATA_TRANSLATION_LBA;
2145
                        else if (!strcmp(p, "auto"))
2146
                            translation = BIOS_ATA_TRANSLATION_AUTO;
2147
                        else
2148
                            goto chs_fail;
2149
                    } else if (*p != '\0') {
2150
                    chs_fail:
2151
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
2152
                        exit(1);
2153
                    }
2154
                    if (hda_opts != NULL) {
2155
                        char num[16];
2156
                        snprintf(num, sizeof(num), "%d", cyls);
2157
                        qemu_opt_set(hda_opts, "cyls", num);
2158
                        snprintf(num, sizeof(num), "%d", heads);
2159
                        qemu_opt_set(hda_opts, "heads", num);
2160
                        snprintf(num, sizeof(num), "%d", secs);
2161
                        qemu_opt_set(hda_opts, "secs", num);
2162
                        if (translation == BIOS_ATA_TRANSLATION_LBA)
2163
                            qemu_opt_set(hda_opts, "trans", "lba");
2164
                        if (translation == BIOS_ATA_TRANSLATION_NONE)
2165
                            qemu_opt_set(hda_opts, "trans", "none");
2166
                    }
2167
                }
2168
                break;
2169
            case QEMU_OPTION_numa:
2170
                if (nb_numa_nodes >= MAX_NODES) {
2171
                    fprintf(stderr, "qemu: too many NUMA nodes\n");
2172
                    exit(1);
2173
                }
2174
                numa_add(optarg);
2175
                break;
2176
            case QEMU_OPTION_display:
2177
                display_type = select_display(optarg);
2178
                break;
2179
            case QEMU_OPTION_nographic:
2180
                display_type = DT_NOGRAPHIC;
2181
                break;
2182
            case QEMU_OPTION_curses:
2183
#ifdef CONFIG_CURSES
2184
                display_type = DT_CURSES;
2185
#else
2186
                fprintf(stderr, "Curses support is disabled\n");
2187
                exit(1);
2188
#endif
2189
                break;
2190
            case QEMU_OPTION_portrait:
2191
                graphic_rotate = 1;
2192
                break;
2193
            case QEMU_OPTION_kernel:
2194
                kernel_filename = optarg;
2195
                break;
2196
            case QEMU_OPTION_append:
2197
                kernel_cmdline = optarg;
2198
                break;
2199
            case QEMU_OPTION_cdrom:
2200
                drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
2201
                break;
2202
            case QEMU_OPTION_boot:
2203
                {
2204
                    static const char * const params[] = {
2205
                        "order", "once", "menu", NULL
2206
                    };
2207
                    char buf[sizeof(boot_devices)];
2208
                    char *standard_boot_devices;
2209
                    int legacy = 0;
2210

    
2211
                    if (!strchr(optarg, '=')) {
2212
                        legacy = 1;
2213
                        pstrcpy(buf, sizeof(buf), optarg);
2214
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2215
                        fprintf(stderr,
2216
                                "qemu: unknown boot parameter '%s' in '%s'\n",
2217
                                buf, optarg);
2218
                        exit(1);
2219
                    }
2220

    
2221
                    if (legacy ||
2222
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
2223
                        validate_bootdevices(buf);
2224
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
2225
                    }
2226
                    if (!legacy) {
2227
                        if (get_param_value(buf, sizeof(buf),
2228
                                            "once", optarg)) {
2229
                            validate_bootdevices(buf);
2230
                            standard_boot_devices = qemu_strdup(boot_devices);
2231
                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
2232
                            qemu_register_reset(restore_boot_devices,
2233
                                                standard_boot_devices);
2234
                        }
2235
                        if (get_param_value(buf, sizeof(buf),
2236
                                            "menu", optarg)) {
2237
                            if (!strcmp(buf, "on")) {
2238
                                boot_menu = 1;
2239
                            } else if (!strcmp(buf, "off")) {
2240
                                boot_menu = 0;
2241
                            } else {
2242
                                fprintf(stderr,
2243
                                        "qemu: invalid option value '%s'\n",
2244
                                        buf);
2245
                                exit(1);
2246
                            }
2247
                        }
2248
                    }
2249
                }
2250
                break;
2251
            case QEMU_OPTION_fda:
2252
            case QEMU_OPTION_fdb:
2253
                drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2254
                          optarg, FD_OPTS);
2255
                break;
2256
            case QEMU_OPTION_no_fd_bootchk:
2257
                fd_bootchk = 0;
2258
                break;
2259
            case QEMU_OPTION_netdev:
2260
                if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2261
                    exit(1);
2262
                }
2263
                break;
2264
            case QEMU_OPTION_net:
2265
                if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2266
                    exit(1);
2267
                }
2268
                break;
2269
#ifdef CONFIG_SLIRP
2270
            case QEMU_OPTION_tftp:
2271
                legacy_tftp_prefix = optarg;
2272
                break;
2273
            case QEMU_OPTION_bootp:
2274
                legacy_bootp_filename = optarg;
2275
                break;
2276
            case QEMU_OPTION_redir:
2277
                if (net_slirp_redir(optarg) < 0)
2278
                    exit(1);
2279
                break;
2280
#endif
2281
            case QEMU_OPTION_bt:
2282
                add_device_config(DEV_BT, optarg);
2283
                break;
2284
            case QEMU_OPTION_audio_help:
2285
                if (!(audio_available())) {
2286
                    printf("Option %s not supported for this target\n", popt->name);
2287
                    exit(1);
2288
                }
2289
                AUD_help ();
2290
                exit (0);
2291
                break;
2292
            case QEMU_OPTION_soundhw:
2293
                if (!(audio_available())) {
2294
                    printf("Option %s not supported for this target\n", popt->name);
2295
                    exit(1);
2296
                }
2297
                select_soundhw (optarg);
2298
                break;
2299
            case QEMU_OPTION_h:
2300
                help(0);
2301
                break;
2302
            case QEMU_OPTION_version:
2303
                version();
2304
                exit(0);
2305
                break;
2306
            case QEMU_OPTION_m: {
2307
                int64_t value;
2308

    
2309
                value = strtosz(optarg, NULL);
2310
                if (value < 0) {
2311
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2312
                    exit(1);
2313
                }
2314

    
2315
                /* On 32-bit hosts, QEMU is limited by virtual address space */
2316
                if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2317
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2318
                    exit(1);
2319
                }
2320
                if (value != (uint64_t)(ram_addr_t)value) {
2321
                    fprintf(stderr, "qemu: ram size too large\n");
2322
                    exit(1);
2323
                }
2324
                ram_size = value;
2325
                break;
2326
            }
2327
            case QEMU_OPTION_mempath:
2328
                mem_path = optarg;
2329
                break;
2330
#ifdef MAP_POPULATE
2331
            case QEMU_OPTION_mem_prealloc:
2332
                mem_prealloc = 1;
2333
                break;
2334
#endif
2335
            case QEMU_OPTION_d:
2336
                set_cpu_log(optarg);
2337
                break;
2338
            case QEMU_OPTION_s:
2339
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2340
                break;
2341
            case QEMU_OPTION_gdb:
2342
                gdbstub_dev = optarg;
2343
                break;
2344
            case QEMU_OPTION_L:
2345
                data_dir = optarg;
2346
                break;
2347
            case QEMU_OPTION_bios:
2348
                bios_name = optarg;
2349
                break;
2350
            case QEMU_OPTION_singlestep:
2351
                singlestep = 1;
2352
                break;
2353
            case QEMU_OPTION_S:
2354
                autostart = 0;
2355
                break;
2356
            case QEMU_OPTION_k:
2357
                keyboard_layout = optarg;
2358
                break;
2359
            case QEMU_OPTION_localtime:
2360
                rtc_utc = 0;
2361
                break;
2362
            case QEMU_OPTION_vga:
2363
                select_vgahw (optarg);
2364
                break;
2365
            case QEMU_OPTION_g:
2366
                {
2367
                    const char *p;
2368
                    int w, h, depth;
2369
                    p = optarg;
2370
                    w = strtol(p, (char **)&p, 10);
2371
                    if (w <= 0) {
2372
                    graphic_error:
2373
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
2374
                        exit(1);
2375
                    }
2376
                    if (*p != 'x')
2377
                        goto graphic_error;
2378
                    p++;
2379
                    h = strtol(p, (char **)&p, 10);
2380
                    if (h <= 0)
2381
                        goto graphic_error;
2382
                    if (*p == 'x') {
2383
                        p++;
2384
                        depth = strtol(p, (char **)&p, 10);
2385
                        if (depth != 8 && depth != 15 && depth != 16 &&
2386
                            depth != 24 && depth != 32)
2387
                            goto graphic_error;
2388
                    } else if (*p == '\0') {
2389
                        depth = graphic_depth;
2390
                    } else {
2391
                        goto graphic_error;
2392
                    }
2393

    
2394
                    graphic_width = w;
2395
                    graphic_height = h;
2396
                    graphic_depth = depth;
2397
                }
2398
                break;
2399
            case QEMU_OPTION_echr:
2400
                {
2401
                    char *r;
2402
                    term_escape_char = strtol(optarg, &r, 0);
2403
                    if (r == optarg)
2404
                        printf("Bad argument to echr\n");
2405
                    break;
2406
                }
2407
            case QEMU_OPTION_monitor:
2408
                monitor_parse(optarg, "readline");
2409
                default_monitor = 0;
2410
                break;
2411
            case QEMU_OPTION_qmp:
2412
                monitor_parse(optarg, "control");
2413
                default_monitor = 0;
2414
                break;
2415
            case QEMU_OPTION_mon:
2416
                opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2417
                if (!opts) {
2418
                    exit(1);
2419
                }
2420
                default_monitor = 0;
2421
                break;
2422
            case QEMU_OPTION_chardev:
2423
                opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2424
                if (!opts) {
2425
                    exit(1);
2426
                }
2427
                break;
2428
            case QEMU_OPTION_fsdev:
2429
                olist = qemu_find_opts("fsdev");
2430
                if (!olist) {
2431
                    fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2432
                    exit(1);
2433
                }
2434
                opts = qemu_opts_parse(olist, optarg, 1);
2435
                if (!opts) {
2436
                    fprintf(stderr, "parse error: %s\n", optarg);
2437
                    exit(1);
2438
                }
2439
                break;
2440
            case QEMU_OPTION_virtfs: {
2441
                char *arg_fsdev = NULL;
2442
                char *arg_9p = NULL;
2443
                int len = 0;
2444

    
2445
                olist = qemu_find_opts("virtfs");
2446
                if (!olist) {
2447
                    fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2448
                    exit(1);
2449
                }
2450
                opts = qemu_opts_parse(olist, optarg, 1);
2451
                if (!opts) {
2452
                    fprintf(stderr, "parse error: %s\n", optarg);
2453
                    exit(1);
2454
                }
2455

    
2456
                if (qemu_opt_get(opts, "fstype") == NULL ||
2457
                        qemu_opt_get(opts, "mount_tag") == NULL ||
2458
                        qemu_opt_get(opts, "path") == NULL ||
2459
                        qemu_opt_get(opts, "security_model") == NULL) {
2460
                    fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2461
                            "security_model=[mapped|passthrough|none],"
2462
                            "mnt_tag=tag.\n");
2463
                    exit(1);
2464
                }
2465

    
2466
                len = strlen(",id=,path=,security_model=");
2467
                len += strlen(qemu_opt_get(opts, "fstype"));
2468
                len += strlen(qemu_opt_get(opts, "mount_tag"));
2469
                len += strlen(qemu_opt_get(opts, "path"));
2470
                len += strlen(qemu_opt_get(opts, "security_model"));
2471
                arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2472

    
2473
                snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2474
                         "%s,id=%s,path=%s,security_model=%s",
2475
                         qemu_opt_get(opts, "fstype"),
2476
                         qemu_opt_get(opts, "mount_tag"),
2477
                         qemu_opt_get(opts, "path"),
2478
                         qemu_opt_get(opts, "security_model"));
2479

    
2480
                len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2481
                len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2482
                arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2483

    
2484
                snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2485
                         "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2486
                         qemu_opt_get(opts, "mount_tag"),
2487
                         qemu_opt_get(opts, "mount_tag"));
2488

    
2489
                if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2490
                    fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2491
                    exit(1);
2492
                }
2493

    
2494
                if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2495
                    fprintf(stderr, "parse error [device]: %s\n", optarg);
2496
                    exit(1);
2497
                }
2498

    
2499
                qemu_free(arg_fsdev);
2500
                qemu_free(arg_9p);
2501
                break;
2502
            }
2503
            case QEMU_OPTION_serial:
2504
                add_device_config(DEV_SERIAL, optarg);
2505
                default_serial = 0;
2506
                if (strncmp(optarg, "mon:", 4) == 0) {
2507
                    default_monitor = 0;
2508
                }
2509
                break;
2510
            case QEMU_OPTION_watchdog:
2511
                if (watchdog) {
2512
                    fprintf(stderr,
2513
                            "qemu: only one watchdog option may be given\n");
2514
                    return 1;
2515
                }
2516
                watchdog = optarg;
2517
                break;
2518
            case QEMU_OPTION_watchdog_action:
2519
                if (select_watchdog_action(optarg) == -1) {
2520
                    fprintf(stderr, "Unknown -watchdog-action parameter\n");
2521
                    exit(1);
2522
                }
2523
                break;
2524
            case QEMU_OPTION_virtiocon:
2525
                add_device_config(DEV_VIRTCON, optarg);
2526
                default_virtcon = 0;
2527
                if (strncmp(optarg, "mon:", 4) == 0) {
2528
                    default_monitor = 0;
2529
                }
2530
                break;
2531
            case QEMU_OPTION_parallel:
2532
                add_device_config(DEV_PARALLEL, optarg);
2533
                default_parallel = 0;
2534
                if (strncmp(optarg, "mon:", 4) == 0) {
2535
                    default_monitor = 0;
2536
                }
2537
                break;
2538
            case QEMU_OPTION_debugcon:
2539
                add_device_config(DEV_DEBUGCON, optarg);
2540
                break;
2541
            case QEMU_OPTION_loadvm:
2542
                loadvm = optarg;
2543
                break;
2544
            case QEMU_OPTION_full_screen:
2545
                full_screen = 1;
2546
                break;
2547
#ifdef CONFIG_SDL
2548
            case QEMU_OPTION_no_frame:
2549
                no_frame = 1;
2550
                break;
2551
            case QEMU_OPTION_alt_grab:
2552
                alt_grab = 1;
2553
                break;
2554
            case QEMU_OPTION_ctrl_grab:
2555
                ctrl_grab = 1;
2556
                break;
2557
            case QEMU_OPTION_no_quit:
2558
                no_quit = 1;
2559
                break;
2560
            case QEMU_OPTION_sdl:
2561
                display_type = DT_SDL;
2562
                break;
2563
#else
2564
            case QEMU_OPTION_no_frame:
2565
            case QEMU_OPTION_alt_grab:
2566
            case QEMU_OPTION_ctrl_grab:
2567
            case QEMU_OPTION_no_quit:
2568
            case QEMU_OPTION_sdl:
2569
                fprintf(stderr, "SDL support is disabled\n");
2570
                exit(1);
2571
#endif
2572
            case QEMU_OPTION_pidfile:
2573
                pid_file = optarg;
2574
                break;
2575
            case QEMU_OPTION_win2k_hack:
2576
                win2k_install_hack = 1;
2577
                break;
2578
            case QEMU_OPTION_rtc_td_hack:
2579
                rtc_td_hack = 1;
2580
                break;
2581
            case QEMU_OPTION_acpitable:
2582
                do_acpitable_option(optarg);
2583
                break;
2584
            case QEMU_OPTION_smbios:
2585
                do_smbios_option(optarg);
2586
                break;
2587
            case QEMU_OPTION_enable_kvm:
2588
                kvm_allowed = 1;
2589
                break;
2590
            case QEMU_OPTION_usb:
2591
                usb_enabled = 1;
2592
                break;
2593
            case QEMU_OPTION_usbdevice:
2594
                usb_enabled = 1;
2595
                add_device_config(DEV_USB, optarg);
2596
                break;
2597
            case QEMU_OPTION_device:
2598
                if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
2599
                    exit(1);
2600
                }
2601
                break;
2602
            case QEMU_OPTION_smp:
2603
                smp_parse(optarg);
2604
                if (smp_cpus < 1) {
2605
                    fprintf(stderr, "Invalid number of CPUs\n");
2606
                    exit(1);
2607
                }
2608
                if (max_cpus < smp_cpus) {
2609
                    fprintf(stderr, "maxcpus must be equal to or greater than "
2610
                            "smp\n");
2611
                    exit(1);
2612
                }
2613
                if (max_cpus > 255) {
2614
                    fprintf(stderr, "Unsupported number of maxcpus\n");
2615
                    exit(1);
2616
                }
2617
                break;
2618
            case QEMU_OPTION_vnc:
2619
#ifdef CONFIG_VNC
2620
                display_remote++;
2621
                vnc_display = optarg;
2622
#else
2623
                fprintf(stderr, "VNC support is disabled\n");
2624
                exit(1);
2625
#endif
2626
                break;
2627
            case QEMU_OPTION_no_acpi:
2628
                acpi_enabled = 0;
2629
                break;
2630
            case QEMU_OPTION_no_hpet:
2631
                no_hpet = 1;
2632
                break;
2633
            case QEMU_OPTION_balloon:
2634
                if (balloon_parse(optarg) < 0) {
2635
                    fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2636
                    exit(1);
2637
                }
2638
                break;
2639
            case QEMU_OPTION_no_reboot:
2640
                no_reboot = 1;
2641
                break;
2642
            case QEMU_OPTION_no_shutdown:
2643
                no_shutdown = 1;
2644
                break;
2645
            case QEMU_OPTION_show_cursor:
2646
                cursor_hide = 0;
2647
                break;
2648
            case QEMU_OPTION_uuid:
2649
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2650
                    fprintf(stderr, "Fail to parse UUID string."
2651
                            " Wrong format.\n");
2652
                    exit(1);
2653
                }
2654
                break;
2655
            case QEMU_OPTION_option_rom:
2656
                if (nb_option_roms >= MAX_OPTION_ROMS) {
2657
                    fprintf(stderr, "Too many option ROMs\n");
2658
                    exit(1);
2659
                }
2660
                opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2661
                option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2662
                option_rom[nb_option_roms].bootindex =
2663
                    qemu_opt_get_number(opts, "bootindex", -1);
2664
                if (!option_rom[nb_option_roms].name) {
2665
                    fprintf(stderr, "Option ROM file is not specified\n");
2666
                    exit(1);
2667
                }
2668
                nb_option_roms++;
2669
                break;
2670
            case QEMU_OPTION_semihosting:
2671
                semihosting_enabled = 1;
2672
                break;
2673
            case QEMU_OPTION_name:
2674
                qemu_name = qemu_strdup(optarg);
2675
                 {
2676
                     char *p = strchr(qemu_name, ',');
2677
                     if (p != NULL) {
2678
                        *p++ = 0;
2679
                        if (strncmp(p, "process=", 8)) {
2680
                            fprintf(stderr, "Unknown subargument %s to -name\n", p);
2681
                            exit(1);
2682
                        }
2683
                        p += 8;
2684
                        os_set_proc_name(p);
2685
                     }        
2686
                 }        
2687
                break;
2688
            case QEMU_OPTION_prom_env:
2689
                if (nb_prom_envs >= MAX_PROM_ENVS) {
2690
                    fprintf(stderr, "Too many prom variables\n");
2691
                    exit(1);
2692
                }
2693
                prom_envs[nb_prom_envs] = optarg;
2694
                nb_prom_envs++;
2695
                break;
2696
            case QEMU_OPTION_old_param:
2697
                old_param = 1;
2698
                break;
2699
            case QEMU_OPTION_clock:
2700
                configure_alarms(optarg);
2701
                break;
2702
            case QEMU_OPTION_startdate:
2703
                configure_rtc_date_offset(optarg, 1);
2704
                break;
2705
            case QEMU_OPTION_rtc:
2706
                opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
2707
                if (!opts) {
2708
                    exit(1);
2709
                }
2710
                configure_rtc(opts);
2711
                break;
2712
            case QEMU_OPTION_tb_size:
2713
                tb_size = strtol(optarg, NULL, 0);
2714
                if (tb_size < 0)
2715
                    tb_size = 0;
2716
                break;
2717
            case QEMU_OPTION_icount:
2718
                icount_option = optarg;
2719
                break;
2720
            case QEMU_OPTION_incoming:
2721
                incoming = optarg;
2722
                incoming_expected = true;
2723
                break;
2724
            case QEMU_OPTION_nodefaults:
2725
                default_serial = 0;
2726
                default_parallel = 0;
2727
                default_virtcon = 0;
2728
                default_monitor = 0;
2729
                default_vga = 0;
2730
                default_net = 0;
2731
                default_floppy = 0;
2732
                default_cdrom = 0;
2733
                default_sdcard = 0;
2734
                break;
2735
            case QEMU_OPTION_xen_domid:
2736
                if (!(xen_available())) {
2737
                    printf("Option %s not supported for this target\n", popt->name);
2738
                    exit(1);
2739
                }
2740
                xen_domid = atoi(optarg);
2741
                break;
2742
            case QEMU_OPTION_xen_create:
2743
                if (!(xen_available())) {
2744
                    printf("Option %s not supported for this target\n", popt->name);
2745
                    exit(1);
2746
                }
2747
                xen_mode = XEN_CREATE;
2748
                break;
2749
            case QEMU_OPTION_xen_attach:
2750
                if (!(xen_available())) {
2751
                    printf("Option %s not supported for this target\n", popt->name);
2752
                    exit(1);
2753
                }
2754
                xen_mode = XEN_ATTACH;
2755
                break;
2756
#ifdef CONFIG_SIMPLE_TRACE
2757
            case QEMU_OPTION_trace:
2758
                opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2759
                if (opts) {
2760
                    trace_file = qemu_opt_get(opts, "file");
2761
                }
2762
                break;
2763
#endif
2764
            case QEMU_OPTION_readconfig:
2765
                {
2766
                    int ret = qemu_read_config_file(optarg);
2767
                    if (ret < 0) {
2768
                        fprintf(stderr, "read config %s: %s\n", optarg,
2769
                            strerror(-ret));
2770
                        exit(1);
2771
                    }
2772
                    break;
2773
                }
2774
            case QEMU_OPTION_spice:
2775
                olist = qemu_find_opts("spice");
2776
                if (!olist) {
2777
                    fprintf(stderr, "spice is not supported by this qemu build.\n");
2778
                    exit(1);
2779
                }
2780
                opts = qemu_opts_parse(olist, optarg, 0);
2781
                if (!opts) {
2782
                    fprintf(stderr, "parse error: %s\n", optarg);
2783
                    exit(1);
2784
                }
2785
                break;
2786
            case QEMU_OPTION_writeconfig:
2787
                {
2788
                    FILE *fp;
2789
                    if (strcmp(optarg, "-") == 0) {
2790
                        fp = stdout;
2791
                    } else {
2792
                        fp = fopen(optarg, "w");
2793
                        if (fp == NULL) {
2794
                            fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2795
                            exit(1);
2796
                        }
2797
                    }
2798
                    qemu_config_write(fp);
2799
                    fclose(fp);
2800
                    break;
2801
                }
2802
            default:
2803
                os_parse_cmd_args(popt->index, optarg);
2804
            }
2805
        }
2806
    }
2807
    loc_set_none();
2808

    
2809
    if (!st_init(trace_file)) {
2810
        fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2811
    }
2812

    
2813
    /* If no data_dir is specified then try to find it relative to the
2814
       executable path.  */
2815
    if (!data_dir) {
2816
        data_dir = os_find_datadir(argv[0]);
2817
    }
2818
    /* If all else fails use the install patch specified when building.  */
2819
    if (!data_dir) {
2820
        data_dir = CONFIG_QEMU_DATADIR;
2821
    }
2822

    
2823
    /*
2824
     * Default to max_cpus = smp_cpus, in case the user doesn't
2825
     * specify a max_cpus value.
2826
     */
2827
    if (!max_cpus)
2828
        max_cpus = smp_cpus;
2829

    
2830
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2831
    if (smp_cpus > machine->max_cpus) {
2832
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2833
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
2834
                machine->max_cpus);
2835
        exit(1);
2836
    }
2837

    
2838
    qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2839
    qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2840

    
2841
    if (machine->no_serial) {
2842
        default_serial = 0;
2843
    }
2844
    if (machine->no_parallel) {
2845
        default_parallel = 0;
2846
    }
2847
    if (!machine->use_virtcon) {
2848
        default_virtcon = 0;
2849
    }
2850
    if (machine->no_vga) {
2851
        default_vga = 0;
2852
    }
2853
    if (machine->no_floppy) {
2854
        default_floppy = 0;
2855
    }
2856
    if (machine->no_cdrom) {
2857
        default_cdrom = 0;
2858
    }
2859
    if (machine->no_sdcard) {
2860
        default_sdcard = 0;
2861
    }
2862

    
2863
    if (display_type == DT_NOGRAPHIC) {
2864
        if (default_parallel)
2865
            add_device_config(DEV_PARALLEL, "null");
2866
        if (default_serial && default_monitor) {
2867
            add_device_config(DEV_SERIAL, "mon:stdio");
2868
        } else if (default_virtcon && default_monitor) {
2869
            add_device_config(DEV_VIRTCON, "mon:stdio");
2870
        } else {
2871
            if (default_serial)
2872
                add_device_config(DEV_SERIAL, "stdio");
2873
            if (default_virtcon)
2874
                add_device_config(DEV_VIRTCON, "stdio");
2875
            if (default_monitor)
2876
                monitor_parse("stdio", "readline");
2877
        }
2878
    } else {
2879
        if (default_serial)
2880
            add_device_config(DEV_SERIAL, "vc:80Cx24C");
2881
        if (default_parallel)
2882
            add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2883
        if (default_monitor)
2884
            monitor_parse("vc:80Cx24C", "readline");
2885
        if (default_virtcon)
2886
            add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2887
    }
2888
    if (default_vga)
2889
        vga_interface_type = VGA_CIRRUS;
2890

    
2891
    socket_init();
2892

    
2893
    if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2894
        exit(1);
2895
#ifdef CONFIG_VIRTFS
2896
    if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2897
        exit(1);
2898
    }
2899
#endif
2900

    
2901
    os_daemonize();
2902

    
2903
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2904
        os_pidfile_error();
2905
        exit(1);
2906
    }
2907

    
2908
    if (kvm_allowed) {
2909
        int ret = kvm_init();
2910
        if (ret < 0) {
2911
            if (!kvm_available()) {
2912
                printf("KVM not supported for this target\n");
2913
            } else {
2914
                fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2915
            }
2916
            exit(1);
2917
        }
2918
    }
2919

    
2920
    if (qemu_init_main_loop()) {
2921
        fprintf(stderr, "qemu_init_main_loop failed\n");
2922
        exit(1);
2923
    }
2924
    linux_boot = (kernel_filename != NULL);
2925

    
2926
    if (!linux_boot && *kernel_cmdline != '\0') {
2927
        fprintf(stderr, "-append only allowed with -kernel option\n");
2928
        exit(1);
2929
    }
2930

    
2931
    if (!linux_boot && initrd_filename != NULL) {
2932
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
2933
        exit(1);
2934
    }
2935

    
2936
    os_set_line_buffering();
2937

    
2938
    if (init_timer_alarm() < 0) {
2939
        fprintf(stderr, "could not initialize alarm timer\n");
2940
        exit(1);
2941
    }
2942
    configure_icount(icount_option);
2943

    
2944
    if (net_init_clients() < 0) {
2945
        exit(1);
2946
    }
2947

    
2948
    /* init the bluetooth world */
2949
    if (foreach_device_config(DEV_BT, bt_parse))
2950
        exit(1);
2951

    
2952
    /* init the memory */
2953
    if (ram_size == 0)
2954
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2955

    
2956
    /* init the dynamic translator */
2957
    cpu_exec_init_all(tb_size * 1024 * 1024);
2958

    
2959
    bdrv_init_with_whitelist();
2960

    
2961
    blk_mig_init();
2962

    
2963
    /* open the virtual block devices */
2964
    if (snapshot)
2965
        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2966
    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2967
        exit(1);
2968

    
2969
    default_drive(default_cdrom, snapshot, machine->use_scsi,
2970
                  IF_DEFAULT, 2, CDROM_OPTS);
2971
    default_drive(default_floppy, snapshot, machine->use_scsi,
2972
                  IF_FLOPPY, 0, FD_OPTS);
2973
    default_drive(default_sdcard, snapshot, machine->use_scsi,
2974
                  IF_SD, 0, SD_OPTS);
2975

    
2976
    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2977
                         ram_load, NULL);
2978

    
2979
    if (nb_numa_nodes > 0) {
2980
        int i;
2981

    
2982
        if (nb_numa_nodes > smp_cpus) {
2983
            nb_numa_nodes = smp_cpus;
2984
        }
2985

    
2986
        /* If no memory size if given for any node, assume the default case
2987
         * and distribute the available memory equally across all nodes
2988
         */
2989
        for (i = 0; i < nb_numa_nodes; i++) {
2990
            if (node_mem[i] != 0)
2991
                break;
2992
        }
2993
        if (i == nb_numa_nodes) {
2994
            uint64_t usedmem = 0;
2995

    
2996
            /* On Linux, the each node's border has to be 8MB aligned,
2997
             * the final node gets the rest.
2998
             */
2999
            for (i = 0; i < nb_numa_nodes - 1; i++) {
3000
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3001
                usedmem += node_mem[i];
3002
            }
3003
            node_mem[i] = ram_size - usedmem;
3004
        }
3005

    
3006
        for (i = 0; i < nb_numa_nodes; i++) {
3007
            if (node_cpumask[i] != 0)
3008
                break;
3009
        }
3010
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
3011
         * must cope with this anyway, because there are BIOSes out there in
3012
         * real machines which also use this scheme.
3013
         */
3014
        if (i == nb_numa_nodes) {
3015
            for (i = 0; i < smp_cpus; i++) {
3016
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
3017
            }
3018
        }
3019
    }
3020

    
3021
    if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
3022
        exit(1);
3023
    }
3024

    
3025
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3026
        exit(1);
3027
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3028
        exit(1);
3029
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3030
        exit(1);
3031
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3032
        exit(1);
3033

    
3034
    module_call_init(MODULE_INIT_DEVICE);
3035

    
3036
    if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
3037
        exit(0);
3038

    
3039
    if (watchdog) {
3040
        i = select_watchdog(watchdog);
3041
        if (i > 0)
3042
            exit (i == 1 ? 1 : 0);
3043
    }
3044

    
3045
    if (machine->compat_props) {
3046
        qdev_prop_register_global_list(machine->compat_props);
3047
    }
3048
    qemu_add_globals();
3049

    
3050
    machine->init(ram_size, boot_devices,
3051
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3052

    
3053
    cpu_synchronize_all_post_init();
3054

    
3055
    /* must be after terminal init, SDL library changes signal handlers */
3056
    os_setup_signal_handling();
3057

    
3058
    set_numa_modes();
3059

    
3060
    current_machine = machine;
3061

    
3062
    /* init USB devices */
3063
    if (usb_enabled) {
3064
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
3065
            exit(1);
3066
    }
3067

    
3068
    /* init generic devices */
3069
    if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3070
        exit(1);
3071

    
3072
    net_check_clients();
3073

    
3074
    /* just use the first displaystate for the moment */
3075
    ds = get_displaystate();
3076

    
3077
    if (using_spice)
3078
        display_remote++;
3079
    if (display_type == DT_DEFAULT && !display_remote) {
3080
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3081
        display_type = DT_SDL;
3082
#elif defined(CONFIG_VNC)
3083
        vnc_display = "localhost:0,to=99";
3084
        show_vnc_port = 1;
3085
#else
3086
        display_type = DT_NONE;
3087
#endif
3088
    }
3089

    
3090

    
3091
    /* init local displays */
3092
    switch (display_type) {
3093
    case DT_NOGRAPHIC:
3094
        break;
3095
#if defined(CONFIG_CURSES)
3096
    case DT_CURSES:
3097
        curses_display_init(ds, full_screen);
3098
        break;
3099
#endif
3100
#if defined(CONFIG_SDL)
3101
    case DT_SDL:
3102
        sdl_display_init(ds, full_screen, no_frame);
3103
        break;
3104
#elif defined(CONFIG_COCOA)
3105
    case DT_SDL:
3106
        cocoa_display_init(ds, full_screen);
3107
        break;
3108
#endif
3109
    default:
3110
        break;
3111
    }
3112

    
3113
#ifdef CONFIG_VNC
3114
    /* init remote displays */
3115
    if (vnc_display) {
3116
        vnc_display_init(ds);
3117
        if (vnc_display_open(ds, vnc_display) < 0)
3118
            exit(1);
3119

    
3120
        if (show_vnc_port) {
3121
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3122
        }
3123
    }
3124
#endif
3125
#ifdef CONFIG_SPICE
3126
    if (using_spice && !qxl_enabled) {
3127
        qemu_spice_display_init(ds);
3128
    }
3129
#endif
3130

    
3131
    /* display setup */
3132
    dpy_resize(ds);
3133
    dcl = ds->listeners;
3134
    while (dcl != NULL) {
3135
        if (dcl->dpy_refresh != NULL) {
3136
            ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3137
            qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
3138
            break;
3139
        }
3140
        dcl = dcl->next;
3141
    }
3142
    if (ds->gui_timer == NULL) {
3143
        nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3144
        qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
3145
    }
3146
    text_consoles_set_display(ds);
3147

    
3148
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3149
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3150
                gdbstub_dev);
3151
        exit(1);
3152
    }
3153

    
3154
    qdev_machine_creation_done();
3155

    
3156
    if (rom_load_all() != 0) {
3157
        fprintf(stderr, "rom loading failed\n");
3158
        exit(1);
3159
    }
3160

    
3161
    /* TODO: once all bus devices are qdevified, this should be done
3162
     * when bus is created by qdev.c */
3163
    qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3164
    qemu_run_machine_init_done_notifiers();
3165

    
3166
    qemu_system_reset();
3167
    if (loadvm) {
3168
        if (load_vmstate(loadvm) < 0) {
3169
            autostart = 0;
3170
        }
3171
    }
3172

    
3173
    if (incoming) {
3174
        int ret = qemu_start_incoming_migration(incoming);
3175
        if (ret < 0) {
3176
            fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3177
                    incoming, ret);
3178
            exit(ret);
3179
        }
3180
    } else if (autostart) {
3181
        vm_start();
3182
    }
3183

    
3184
    os_setup_post();
3185

    
3186
    main_loop();
3187
    quit_timers();
3188
    net_cleanup();
3189

    
3190
    return 0;
3191
}