Statistics
| Branch: | Revision:

root / vl.c @ c57c846a

History | View | Annotate | Download (83.9 kB)

1
/*
2
 * QEMU System Emulator
3
 *
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include <unistd.h>
25
#include <fcntl.h>
26
#include <signal.h>
27
#include <time.h>
28
#include <errno.h>
29
#include <sys/time.h>
30
#include <zlib.h>
31

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

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

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

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

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

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

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

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

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

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

    
155
#include "disas.h"
156

    
157
#include "qemu_socket.h"
158

    
159
#include "slirp/libslirp.h"
160

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

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

    
167
//#define DEBUG_NET
168
//#define DEBUG_SLIRP
169

    
170
#define DEFAULT_RAM_SIZE 128
171

    
172
#define MAX_VIRTIO_CONSOLES 1
173

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

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

    
235
static QEMUTimer *nographic_timer;
236

    
237
uint8_t qemu_uuid[16];
238

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

    
242
static NotifierList exit_notifiers =
243
    NOTIFIER_LIST_INITIALIZER(exit_notifiers);
244

    
245
int kvm_allowed = 0;
246
uint32_t xen_domid;
247
enum xen_mode xen_mode = XEN_EMULATE;
248

    
249
static int default_serial = 1;
250
static int default_parallel = 1;
251
static int default_virtcon = 1;
252
static int default_monitor = 1;
253
static int default_vga = 1;
254
static int default_floppy = 1;
255
static int default_cdrom = 1;
256
static int default_sdcard = 1;
257

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

    
274
static int default_driver_check(QemuOpts *opts, void *opaque)
275
{
276
    const char *driver = qemu_opt_get(opts, "driver");
277
    int i;
278

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

    
289
/***********************************************************/
290
/* real time host monotonic timer */
291

    
292
/***********************************************************/
293
/* host time/date access */
294
void qemu_get_timedate(struct tm *tm, int offset)
295
{
296
    time_t ti;
297
    struct tm *ret;
298

    
299
    time(&ti);
300
    ti += offset;
301
    if (rtc_date_offset == -1) {
302
        if (rtc_utc)
303
            ret = gmtime(&ti);
304
        else
305
            ret = localtime(&ti);
306
    } else {
307
        ti -= rtc_date_offset;
308
        ret = gmtime(&ti);
309
    }
310

    
311
    memcpy(tm, ret, sizeof(struct tm));
312
}
313

    
314
int qemu_timedate_diff(struct tm *tm)
315
{
316
    time_t seconds;
317

    
318
    if (rtc_date_offset == -1)
319
        if (rtc_utc)
320
            seconds = mktimegm(tm);
321
        else
322
            seconds = mktime(tm);
323
    else
324
        seconds = mktimegm(tm) + rtc_date_offset;
325

    
326
    return seconds - time(NULL);
327
}
328

    
329
void rtc_change_mon_event(struct tm *tm)
330
{
331
    QObject *data;
332

    
333
    data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
334
    monitor_protocol_event(QEVENT_RTC_CHANGE, data);
335
    qobject_decref(data);
336
}
337

    
338
static void configure_rtc_date_offset(const char *startdate, int legacy)
339
{
340
    time_t rtc_start_date;
341
    struct tm tm;
342

    
343
    if (!strcmp(startdate, "now") && legacy) {
344
        rtc_date_offset = -1;
345
    } else {
346
        if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
347
                   &tm.tm_year,
348
                   &tm.tm_mon,
349
                   &tm.tm_mday,
350
                   &tm.tm_hour,
351
                   &tm.tm_min,
352
                   &tm.tm_sec) == 6) {
353
            /* OK */
354
        } else if (sscanf(startdate, "%d-%d-%d",
355
                          &tm.tm_year,
356
                          &tm.tm_mon,
357
                          &tm.tm_mday) == 3) {
358
            tm.tm_hour = 0;
359
            tm.tm_min = 0;
360
            tm.tm_sec = 0;
361
        } else {
362
            goto date_fail;
363
        }
364
        tm.tm_year -= 1900;
365
        tm.tm_mon--;
366
        rtc_start_date = mktimegm(&tm);
367
        if (rtc_start_date == -1) {
368
        date_fail:
369
            fprintf(stderr, "Invalid date format. Valid formats are:\n"
370
                            "'2006-06-17T16:01:21' or '2006-06-17'\n");
371
            exit(1);
372
        }
373
        rtc_date_offset = time(NULL) - rtc_start_date;
374
    }
375
}
376

    
377
static void configure_rtc(QemuOpts *opts)
378
{
379
    const char *value;
380

    
381
    value = qemu_opt_get(opts, "base");
382
    if (value) {
383
        if (!strcmp(value, "utc")) {
384
            rtc_utc = 1;
385
        } else if (!strcmp(value, "localtime")) {
386
            rtc_utc = 0;
387
        } else {
388
            configure_rtc_date_offset(value, 0);
389
        }
390
    }
391
    value = qemu_opt_get(opts, "clock");
392
    if (value) {
393
        if (!strcmp(value, "host")) {
394
            rtc_clock = host_clock;
395
        } else if (!strcmp(value, "vm")) {
396
            rtc_clock = vm_clock;
397
        } else {
398
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
399
            exit(1);
400
        }
401
    }
402
    value = qemu_opt_get(opts, "driftfix");
403
    if (value) {
404
        if (!strcmp(value, "slew")) {
405
            rtc_td_hack = 1;
406
        } else if (!strcmp(value, "none")) {
407
            rtc_td_hack = 0;
408
        } else {
409
            fprintf(stderr, "qemu: invalid option value '%s'\n", value);
410
            exit(1);
411
        }
412
    }
413
}
414

    
415
/***********************************************************/
416
/* Bluetooth support */
417
static int nb_hcis;
418
static int cur_hci;
419
static struct HCIInfo *hci_table[MAX_NICS];
420

    
421
static struct bt_vlan_s {
422
    struct bt_scatternet_s net;
423
    int id;
424
    struct bt_vlan_s *next;
425
} *first_bt_vlan;
426

    
427
/* find or alloc a new bluetooth "VLAN" */
428
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
429
{
430
    struct bt_vlan_s **pvlan, *vlan;
431
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
432
        if (vlan->id == id)
433
            return &vlan->net;
434
    }
435
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
436
    vlan->id = id;
437
    pvlan = &first_bt_vlan;
438
    while (*pvlan != NULL)
439
        pvlan = &(*pvlan)->next;
440
    *pvlan = vlan;
441
    return &vlan->net;
442
}
443

    
444
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
445
{
446
}
447

    
448
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
449
{
450
    return -ENOTSUP;
451
}
452

    
453
static struct HCIInfo null_hci = {
454
    .cmd_send = null_hci_send,
455
    .sco_send = null_hci_send,
456
    .acl_send = null_hci_send,
457
    .bdaddr_set = null_hci_addr_set,
458
};
459

    
460
struct HCIInfo *qemu_next_hci(void)
461
{
462
    if (cur_hci == nb_hcis)
463
        return &null_hci;
464

    
465
    return hci_table[cur_hci++];
466
}
467

    
468
static struct HCIInfo *hci_init(const char *str)
469
{
470
    char *endp;
471
    struct bt_scatternet_s *vlan = 0;
472

    
473
    if (!strcmp(str, "null"))
474
        /* null */
475
        return &null_hci;
476
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
477
        /* host[:hciN] */
478
        return bt_host_hci(str[4] ? str + 5 : "hci0");
479
    else if (!strncmp(str, "hci", 3)) {
480
        /* hci[,vlan=n] */
481
        if (str[3]) {
482
            if (!strncmp(str + 3, ",vlan=", 6)) {
483
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
484
                if (*endp)
485
                    vlan = 0;
486
            }
487
        } else
488
            vlan = qemu_find_bt_vlan(0);
489
        if (vlan)
490
           return bt_new_hci(vlan);
491
    }
492

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

    
495
    return 0;
496
}
497

    
498
static int bt_hci_parse(const char *str)
499
{
500
    struct HCIInfo *hci;
501
    bdaddr_t bdaddr;
502

    
503
    if (nb_hcis >= MAX_NICS) {
504
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
505
        return -1;
506
    }
507

    
508
    hci = hci_init(str);
509
    if (!hci)
510
        return -1;
511

    
512
    bdaddr.b[0] = 0x52;
513
    bdaddr.b[1] = 0x54;
514
    bdaddr.b[2] = 0x00;
515
    bdaddr.b[3] = 0x12;
516
    bdaddr.b[4] = 0x34;
517
    bdaddr.b[5] = 0x56 + nb_hcis;
518
    hci->bdaddr_set(hci, bdaddr.b);
519

    
520
    hci_table[nb_hcis++] = hci;
521

    
522
    return 0;
523
}
524

    
525
static void bt_vhci_add(int vlan_id)
526
{
527
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
528

    
529
    if (!vlan->slave)
530
        fprintf(stderr, "qemu: warning: adding a VHCI to "
531
                        "an empty scatternet %i\n", vlan_id);
532

    
533
    bt_vhci_init(bt_new_hci(vlan));
534
}
535

    
536
static struct bt_device_s *bt_device_add(const char *opt)
537
{
538
    struct bt_scatternet_s *vlan;
539
    int vlan_id = 0;
540
    char *endp = strstr(opt, ",vlan=");
541
    int len = (endp ? endp - opt : strlen(opt)) + 1;
542
    char devname[10];
543

    
544
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
545

    
546
    if (endp) {
547
        vlan_id = strtol(endp + 6, &endp, 0);
548
        if (*endp) {
549
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
550
            return 0;
551
        }
552
    }
553

    
554
    vlan = qemu_find_bt_vlan(vlan_id);
555

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

    
560
    if (!strcmp(devname, "keyboard"))
561
        return bt_keyboard_init(vlan);
562

    
563
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
564
    return 0;
565
}
566

    
567
static int bt_parse(const char *opt)
568
{
569
    const char *endp, *p;
570
    int vlan;
571

    
572
    if (strstart(opt, "hci", &endp)) {
573
        if (!*endp || *endp == ',') {
574
            if (*endp)
575
                if (!strstart(endp, ",vlan=", 0))
576
                    opt = endp + 1;
577

    
578
            return bt_hci_parse(opt);
579
       }
580
    } else if (strstart(opt, "vhci", &endp)) {
581
        if (!*endp || *endp == ',') {
582
            if (*endp) {
583
                if (strstart(endp, ",vlan=", &p)) {
584
                    vlan = strtol(p, (char **) &endp, 0);
585
                    if (*endp) {
586
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
587
                        return 1;
588
                    }
589
                } else {
590
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
591
                    return 1;
592
                }
593
            } else
594
                vlan = 0;
595

    
596
            bt_vhci_add(vlan);
597
            return 0;
598
        }
599
    } else if (strstart(opt, "device:", &endp))
600
        return !bt_device_add(endp);
601

    
602
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
603
    return 1;
604
}
605

    
606
/***********************************************************/
607
/* QEMU Block devices */
608

    
609
#define HD_ALIAS "index=%d,media=disk"
610
#define CDROM_ALIAS "index=2,media=cdrom"
611
#define FD_ALIAS "index=%d,if=floppy"
612
#define PFLASH_ALIAS "if=pflash"
613
#define MTD_ALIAS "if=mtd"
614
#define SD_ALIAS "index=0,if=sd"
615

    
616
static int drive_init_func(QemuOpts *opts, void *opaque)
617
{
618
    int *use_scsi = opaque;
619
    int fatal_error = 0;
620

    
621
    if (drive_init(opts, *use_scsi, &fatal_error) == NULL) {
622
        if (fatal_error)
623
            return 1;
624
    }
625
    return 0;
626
}
627

    
628
static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
629
{
630
    if (NULL == qemu_opt_get(opts, "snapshot")) {
631
        qemu_opt_set(opts, "snapshot", "on");
632
    }
633
    return 0;
634
}
635

    
636
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
637
{
638
    boot_set_handler = func;
639
    boot_set_opaque = opaque;
640
}
641

    
642
int qemu_boot_set(const char *boot_devices)
643
{
644
    if (!boot_set_handler) {
645
        return -EINVAL;
646
    }
647
    return boot_set_handler(boot_set_opaque, boot_devices);
648
}
649

    
650
static void validate_bootdevices(char *devices)
651
{
652
    /* We just do some generic consistency checks */
653
    const char *p;
654
    int bitmap = 0;
655

    
656
    for (p = devices; *p != '\0'; p++) {
657
        /* Allowed boot devices are:
658
         * a-b: floppy disk drives
659
         * c-f: IDE disk drives
660
         * g-m: machine implementation dependant drives
661
         * n-p: network devices
662
         * It's up to each machine implementation to check if the given boot
663
         * devices match the actual hardware implementation and firmware
664
         * features.
665
         */
666
        if (*p < 'a' || *p > 'p') {
667
            fprintf(stderr, "Invalid boot device '%c'\n", *p);
668
            exit(1);
669
        }
670
        if (bitmap & (1 << (*p - 'a'))) {
671
            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
672
            exit(1);
673
        }
674
        bitmap |= 1 << (*p - 'a');
675
    }
676
}
677

    
678
static void restore_boot_devices(void *opaque)
679
{
680
    char *standard_boot_devices = opaque;
681
    static int first = 1;
682

    
683
    /* Restore boot order and remove ourselves after the first boot */
684
    if (first) {
685
        first = 0;
686
        return;
687
    }
688

    
689
    qemu_boot_set(standard_boot_devices);
690

    
691
    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
692
    qemu_free(standard_boot_devices);
693
}
694

    
695
static void numa_add(const char *optarg)
696
{
697
    char option[128];
698
    char *endptr;
699
    unsigned long long value, endvalue;
700
    int nodenr;
701

    
702
    optarg = get_opt_name(option, 128, optarg, ',') + 1;
703
    if (!strcmp(option, "node")) {
704
        if (get_param_value(option, 128, "nodeid", optarg) == 0) {
705
            nodenr = nb_numa_nodes;
706
        } else {
707
            nodenr = strtoull(option, NULL, 10);
708
        }
709

    
710
        if (get_param_value(option, 128, "mem", optarg) == 0) {
711
            node_mem[nodenr] = 0;
712
        } else {
713
            value = strtoull(option, &endptr, 0);
714
            switch (*endptr) {
715
            case 0: case 'M': case 'm':
716
                value <<= 20;
717
                break;
718
            case 'G': case 'g':
719
                value <<= 30;
720
                break;
721
            }
722
            node_mem[nodenr] = value;
723
        }
724
        if (get_param_value(option, 128, "cpus", optarg) == 0) {
725
            node_cpumask[nodenr] = 0;
726
        } else {
727
            value = strtoull(option, &endptr, 10);
728
            if (value >= 64) {
729
                value = 63;
730
                fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
731
            } else {
732
                if (*endptr == '-') {
733
                    endvalue = strtoull(endptr+1, &endptr, 10);
734
                    if (endvalue >= 63) {
735
                        endvalue = 62;
736
                        fprintf(stderr,
737
                            "only 63 CPUs in NUMA mode supported.\n");
738
                    }
739
                    value = (2ULL << endvalue) - (1ULL << value);
740
                } else {
741
                    value = 1ULL << value;
742
                }
743
            }
744
            node_cpumask[nodenr] = value;
745
        }
746
        nb_numa_nodes++;
747
    }
748
    return;
749
}
750

    
751
static void smp_parse(const char *optarg)
752
{
753
    int smp, sockets = 0, threads = 0, cores = 0;
754
    char *endptr;
755
    char option[128];
756

    
757
    smp = strtoul(optarg, &endptr, 10);
758
    if (endptr != optarg) {
759
        if (*endptr == ',') {
760
            endptr++;
761
        }
762
    }
763
    if (get_param_value(option, 128, "sockets", endptr) != 0)
764
        sockets = strtoull(option, NULL, 10);
765
    if (get_param_value(option, 128, "cores", endptr) != 0)
766
        cores = strtoull(option, NULL, 10);
767
    if (get_param_value(option, 128, "threads", endptr) != 0)
768
        threads = strtoull(option, NULL, 10);
769
    if (get_param_value(option, 128, "maxcpus", endptr) != 0)
770
        max_cpus = strtoull(option, NULL, 10);
771

    
772
    /* compute missing values, prefer sockets over cores over threads */
773
    if (smp == 0 || sockets == 0) {
774
        sockets = sockets > 0 ? sockets : 1;
775
        cores = cores > 0 ? cores : 1;
776
        threads = threads > 0 ? threads : 1;
777
        if (smp == 0) {
778
            smp = cores * threads * sockets;
779
        }
780
    } else {
781
        if (cores == 0) {
782
            threads = threads > 0 ? threads : 1;
783
            cores = smp / (sockets * threads);
784
        } else {
785
            threads = smp / (cores * sockets);
786
        }
787
    }
788
    smp_cpus = smp;
789
    smp_cores = cores > 0 ? cores : 1;
790
    smp_threads = threads > 0 ? threads : 1;
791
    if (max_cpus == 0)
792
        max_cpus = smp_cpus;
793
}
794

    
795
/***********************************************************/
796
/* USB devices */
797

    
798
static int usb_device_add(const char *devname)
799
{
800
    const char *p;
801
    USBDevice *dev = NULL;
802

    
803
    if (!usb_enabled)
804
        return -1;
805

    
806
    /* drivers with .usbdevice_name entry in USBDeviceInfo */
807
    dev = usbdevice_create(devname);
808
    if (dev)
809
        goto done;
810

    
811
    /* the other ones */
812
    if (strstart(devname, "host:", &p)) {
813
        dev = usb_host_device_open(p);
814
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
815
        dev = usb_bt_init(devname[2] ? hci_init(p) :
816
                        bt_new_hci(qemu_find_bt_vlan(0)));
817
    } else {
818
        return -1;
819
    }
820
    if (!dev)
821
        return -1;
822

    
823
done:
824
    return 0;
825
}
826

    
827
static int usb_device_del(const char *devname)
828
{
829
    int bus_num, addr;
830
    const char *p;
831

    
832
    if (strstart(devname, "host:", &p))
833
        return usb_host_device_close(p);
834

    
835
    if (!usb_enabled)
836
        return -1;
837

    
838
    p = strchr(devname, '.');
839
    if (!p)
840
        return -1;
841
    bus_num = strtoul(devname, NULL, 0);
842
    addr = strtoul(p + 1, NULL, 0);
843

    
844
    return usb_device_delete_addr(bus_num, addr);
845
}
846

    
847
static int usb_parse(const char *cmdline)
848
{
849
    int r;
850
    r = usb_device_add(cmdline);
851
    if (r < 0) {
852
        fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
853
    }
854
    return r;
855
}
856

    
857
void do_usb_add(Monitor *mon, const QDict *qdict)
858
{
859
    const char *devname = qdict_get_str(qdict, "devname");
860
    if (usb_device_add(devname) < 0) {
861
        error_report("could not add USB device '%s'", devname);
862
    }
863
}
864

    
865
void do_usb_del(Monitor *mon, const QDict *qdict)
866
{
867
    const char *devname = qdict_get_str(qdict, "devname");
868
    if (usb_device_del(devname) < 0) {
869
        error_report("could not delete USB device '%s'", devname);
870
    }
871
}
872

    
873
/***********************************************************/
874
/* PCMCIA/Cardbus */
875

    
876
static struct pcmcia_socket_entry_s {
877
    PCMCIASocket *socket;
878
    struct pcmcia_socket_entry_s *next;
879
} *pcmcia_sockets = 0;
880

    
881
void pcmcia_socket_register(PCMCIASocket *socket)
882
{
883
    struct pcmcia_socket_entry_s *entry;
884

    
885
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
886
    entry->socket = socket;
887
    entry->next = pcmcia_sockets;
888
    pcmcia_sockets = entry;
889
}
890

    
891
void pcmcia_socket_unregister(PCMCIASocket *socket)
892
{
893
    struct pcmcia_socket_entry_s *entry, **ptr;
894

    
895
    ptr = &pcmcia_sockets;
896
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
897
        if (entry->socket == socket) {
898
            *ptr = entry->next;
899
            qemu_free(entry);
900
        }
901
}
902

    
903
void pcmcia_info(Monitor *mon)
904
{
905
    struct pcmcia_socket_entry_s *iter;
906

    
907
    if (!pcmcia_sockets)
908
        monitor_printf(mon, "No PCMCIA sockets\n");
909

    
910
    for (iter = pcmcia_sockets; iter; iter = iter->next)
911
        monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
912
                       iter->socket->attached ? iter->socket->card_string :
913
                       "Empty");
914
}
915

    
916
/***********************************************************/
917
/* I/O handling */
918

    
919
typedef struct IOHandlerRecord {
920
    int fd;
921
    IOCanReadHandler *fd_read_poll;
922
    IOHandler *fd_read;
923
    IOHandler *fd_write;
924
    int deleted;
925
    void *opaque;
926
    /* temporary data */
927
    struct pollfd *ufd;
928
    QLIST_ENTRY(IOHandlerRecord) next;
929
} IOHandlerRecord;
930

    
931
static QLIST_HEAD(, IOHandlerRecord) io_handlers =
932
    QLIST_HEAD_INITIALIZER(io_handlers);
933

    
934

    
935
/* XXX: fd_read_poll should be suppressed, but an API change is
936
   necessary in the character devices to suppress fd_can_read(). */
937
int qemu_set_fd_handler2(int fd,
938
                         IOCanReadHandler *fd_read_poll,
939
                         IOHandler *fd_read,
940
                         IOHandler *fd_write,
941
                         void *opaque)
942
{
943
    IOHandlerRecord *ioh;
944

    
945
    if (!fd_read && !fd_write) {
946
        QLIST_FOREACH(ioh, &io_handlers, next) {
947
            if (ioh->fd == fd) {
948
                ioh->deleted = 1;
949
                break;
950
            }
951
        }
952
    } else {
953
        QLIST_FOREACH(ioh, &io_handlers, next) {
954
            if (ioh->fd == fd)
955
                goto found;
956
        }
957
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
958
        QLIST_INSERT_HEAD(&io_handlers, ioh, next);
959
    found:
960
        ioh->fd = fd;
961
        ioh->fd_read_poll = fd_read_poll;
962
        ioh->fd_read = fd_read;
963
        ioh->fd_write = fd_write;
964
        ioh->opaque = opaque;
965
        ioh->deleted = 0;
966
    }
967
    return 0;
968
}
969

    
970
int qemu_set_fd_handler(int fd,
971
                        IOHandler *fd_read,
972
                        IOHandler *fd_write,
973
                        void *opaque)
974
{
975
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
976
}
977

    
978
/***********************************************************/
979
/* machine registration */
980

    
981
static QEMUMachine *first_machine = NULL;
982
QEMUMachine *current_machine = NULL;
983

    
984
int qemu_register_machine(QEMUMachine *m)
985
{
986
    QEMUMachine **pm;
987
    pm = &first_machine;
988
    while (*pm != NULL)
989
        pm = &(*pm)->next;
990
    m->next = NULL;
991
    *pm = m;
992
    return 0;
993
}
994

    
995
static QEMUMachine *find_machine(const char *name)
996
{
997
    QEMUMachine *m;
998

    
999
    for(m = first_machine; m != NULL; m = m->next) {
1000
        if (!strcmp(m->name, name))
1001
            return m;
1002
        if (m->alias && !strcmp(m->alias, name))
1003
            return m;
1004
    }
1005
    return NULL;
1006
}
1007

    
1008
static QEMUMachine *find_default_machine(void)
1009
{
1010
    QEMUMachine *m;
1011

    
1012
    for(m = first_machine; m != NULL; m = m->next) {
1013
        if (m->is_default) {
1014
            return m;
1015
        }
1016
    }
1017
    return NULL;
1018
}
1019

    
1020
/***********************************************************/
1021
/* main execution loop */
1022

    
1023
static void gui_update(void *opaque)
1024
{
1025
    uint64_t interval = GUI_REFRESH_INTERVAL;
1026
    DisplayState *ds = opaque;
1027
    DisplayChangeListener *dcl = ds->listeners;
1028

    
1029
    qemu_flush_coalesced_mmio_buffer();
1030
    dpy_refresh(ds);
1031

    
1032
    while (dcl != NULL) {
1033
        if (dcl->gui_timer_interval &&
1034
            dcl->gui_timer_interval < interval)
1035
            interval = dcl->gui_timer_interval;
1036
        dcl = dcl->next;
1037
    }
1038
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1039
}
1040

    
1041
static void nographic_update(void *opaque)
1042
{
1043
    uint64_t interval = GUI_REFRESH_INTERVAL;
1044

    
1045
    qemu_flush_coalesced_mmio_buffer();
1046
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1047
}
1048

    
1049
struct vm_change_state_entry {
1050
    VMChangeStateHandler *cb;
1051
    void *opaque;
1052
    QLIST_ENTRY (vm_change_state_entry) entries;
1053
};
1054

    
1055
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1056

    
1057
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1058
                                                     void *opaque)
1059
{
1060
    VMChangeStateEntry *e;
1061

    
1062
    e = qemu_mallocz(sizeof (*e));
1063

    
1064
    e->cb = cb;
1065
    e->opaque = opaque;
1066
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1067
    return e;
1068
}
1069

    
1070
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1071
{
1072
    QLIST_REMOVE (e, entries);
1073
    qemu_free (e);
1074
}
1075

    
1076
void vm_state_notify(int running, int reason)
1077
{
1078
    VMChangeStateEntry *e;
1079

    
1080
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1081
        e->cb(e->opaque, running, reason);
1082
    }
1083
}
1084

    
1085
void vm_start(void)
1086
{
1087
    if (!vm_running) {
1088
        cpu_enable_ticks();
1089
        vm_running = 1;
1090
        vm_state_notify(1, 0);
1091
        resume_all_vcpus();
1092
        monitor_protocol_event(QEVENT_RESUME, NULL);
1093
    }
1094
}
1095

    
1096
/* reset/shutdown handler */
1097

    
1098
typedef struct QEMUResetEntry {
1099
    QTAILQ_ENTRY(QEMUResetEntry) entry;
1100
    QEMUResetHandler *func;
1101
    void *opaque;
1102
} QEMUResetEntry;
1103

    
1104
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1105
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
1106
static int reset_requested;
1107
static int shutdown_requested;
1108
static int powerdown_requested;
1109
int debug_requested;
1110
int vmstop_requested;
1111

    
1112
int qemu_shutdown_requested(void)
1113
{
1114
    int r = shutdown_requested;
1115
    shutdown_requested = 0;
1116
    return r;
1117
}
1118

    
1119
int qemu_reset_requested(void)
1120
{
1121
    int r = reset_requested;
1122
    reset_requested = 0;
1123
    return r;
1124
}
1125

    
1126
int qemu_powerdown_requested(void)
1127
{
1128
    int r = powerdown_requested;
1129
    powerdown_requested = 0;
1130
    return r;
1131
}
1132

    
1133
static int qemu_debug_requested(void)
1134
{
1135
    int r = debug_requested;
1136
    debug_requested = 0;
1137
    return r;
1138
}
1139

    
1140
static int qemu_vmstop_requested(void)
1141
{
1142
    int r = vmstop_requested;
1143
    vmstop_requested = 0;
1144
    return r;
1145
}
1146

    
1147
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1148
{
1149
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1150

    
1151
    re->func = func;
1152
    re->opaque = opaque;
1153
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1154
}
1155

    
1156
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1157
{
1158
    QEMUResetEntry *re;
1159

    
1160
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
1161
        if (re->func == func && re->opaque == opaque) {
1162
            QTAILQ_REMOVE(&reset_handlers, re, entry);
1163
            qemu_free(re);
1164
            return;
1165
        }
1166
    }
1167
}
1168

    
1169
void qemu_system_reset(void)
1170
{
1171
    QEMUResetEntry *re, *nre;
1172

    
1173
    /* reset all devices */
1174
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1175
        re->func(re->opaque);
1176
    }
1177
    monitor_protocol_event(QEVENT_RESET, NULL);
1178
    cpu_synchronize_all_post_reset();
1179
}
1180

    
1181
void qemu_system_reset_request(void)
1182
{
1183
    if (no_reboot) {
1184
        shutdown_requested = 1;
1185
    } else {
1186
        reset_requested = 1;
1187
    }
1188
    qemu_notify_event();
1189
}
1190

    
1191
void qemu_system_shutdown_request(void)
1192
{
1193
    shutdown_requested = 1;
1194
    qemu_notify_event();
1195
}
1196

    
1197
void qemu_system_powerdown_request(void)
1198
{
1199
    powerdown_requested = 1;
1200
    qemu_notify_event();
1201
}
1202

    
1203
void main_loop_wait(int nonblocking)
1204
{
1205
    IOHandlerRecord *ioh;
1206
    fd_set rfds, wfds, xfds;
1207
    int ret, nfds;
1208
    struct timeval tv;
1209
    int timeout;
1210

    
1211
    if (nonblocking)
1212
        timeout = 0;
1213
    else {
1214
        timeout = qemu_calculate_timeout();
1215
        qemu_bh_update_timeout(&timeout);
1216
    }
1217

    
1218
    os_host_main_loop_wait(&timeout);
1219

    
1220
    /* poll any events */
1221
    /* XXX: separate device handlers from system ones */
1222
    nfds = -1;
1223
    FD_ZERO(&rfds);
1224
    FD_ZERO(&wfds);
1225
    FD_ZERO(&xfds);
1226
    QLIST_FOREACH(ioh, &io_handlers, next) {
1227
        if (ioh->deleted)
1228
            continue;
1229
        if (ioh->fd_read &&
1230
            (!ioh->fd_read_poll ||
1231
             ioh->fd_read_poll(ioh->opaque) != 0)) {
1232
            FD_SET(ioh->fd, &rfds);
1233
            if (ioh->fd > nfds)
1234
                nfds = ioh->fd;
1235
        }
1236
        if (ioh->fd_write) {
1237
            FD_SET(ioh->fd, &wfds);
1238
            if (ioh->fd > nfds)
1239
                nfds = ioh->fd;
1240
        }
1241
    }
1242

    
1243
    tv.tv_sec = timeout / 1000;
1244
    tv.tv_usec = (timeout % 1000) * 1000;
1245

    
1246
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1247

    
1248
    qemu_mutex_unlock_iothread();
1249
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1250
    qemu_mutex_lock_iothread();
1251
    if (ret > 0) {
1252
        IOHandlerRecord *pioh;
1253

    
1254
        QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1255
            if (ioh->deleted) {
1256
                QLIST_REMOVE(ioh, next);
1257
                qemu_free(ioh);
1258
                continue;
1259
            }
1260
            if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1261
                ioh->fd_read(ioh->opaque);
1262
            }
1263
            if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1264
                ioh->fd_write(ioh->opaque);
1265
            }
1266
        }
1267
    }
1268

    
1269
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1270

    
1271
    qemu_run_all_timers();
1272

    
1273
    /* Check bottom-halves last in case any of the earlier events triggered
1274
       them.  */
1275
    qemu_bh_poll();
1276

    
1277
}
1278

    
1279
static int vm_can_run(void)
1280
{
1281
    if (powerdown_requested)
1282
        return 0;
1283
    if (reset_requested)
1284
        return 0;
1285
    if (shutdown_requested)
1286
        return 0;
1287
    if (debug_requested)
1288
        return 0;
1289
    return 1;
1290
}
1291

    
1292
qemu_irq qemu_system_powerdown;
1293

    
1294
static void main_loop(void)
1295
{
1296
    int r;
1297

    
1298
    qemu_main_loop_start();
1299

    
1300
    for (;;) {
1301
        do {
1302
            bool nonblocking = false;
1303
#ifdef CONFIG_PROFILER
1304
            int64_t ti;
1305
#endif
1306
#ifndef CONFIG_IOTHREAD
1307
            nonblocking = cpu_exec_all();
1308
#endif
1309
#ifdef CONFIG_PROFILER
1310
            ti = profile_getclock();
1311
#endif
1312
            main_loop_wait(nonblocking);
1313
#ifdef CONFIG_PROFILER
1314
            dev_time += profile_getclock() - ti;
1315
#endif
1316
        } while (vm_can_run());
1317

    
1318
        if ((r = qemu_debug_requested())) {
1319
            vm_stop(r);
1320
        }
1321
        if (qemu_shutdown_requested()) {
1322
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1323
            if (no_shutdown) {
1324
                vm_stop(0);
1325
                no_shutdown = 0;
1326
            } else
1327
                break;
1328
        }
1329
        if (qemu_reset_requested()) {
1330
            pause_all_vcpus();
1331
            qemu_system_reset();
1332
            resume_all_vcpus();
1333
        }
1334
        if (qemu_powerdown_requested()) {
1335
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1336
            qemu_irq_raise(qemu_system_powerdown);
1337
        }
1338
        if ((r = qemu_vmstop_requested())) {
1339
            vm_stop(r);
1340
        }
1341
    }
1342
    bdrv_close_all();
1343
    pause_all_vcpus();
1344
}
1345

    
1346
static void version(void)
1347
{
1348
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1349
}
1350

    
1351
static void help(int exitcode)
1352
{
1353
    const char *options_help =
1354
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1355
        opt_help
1356
#define DEFHEADING(text) stringify(text) "\n"
1357
#include "qemu-options.def"
1358
#undef DEF
1359
#undef DEFHEADING
1360
#undef GEN_DOCS
1361
        ;
1362
    version();
1363
    printf("usage: %s [options] [disk_image]\n"
1364
           "\n"
1365
           "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1366
           "\n"
1367
           "%s\n"
1368
           "During emulation, the following keys are useful:\n"
1369
           "ctrl-alt-f      toggle full screen\n"
1370
           "ctrl-alt-n      switch to virtual console 'n'\n"
1371
           "ctrl-alt        toggle mouse and keyboard grab\n"
1372
           "\n"
1373
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
1374
           "qemu",
1375
           options_help);
1376
    exit(exitcode);
1377
}
1378

    
1379
#define HAS_ARG 0x0001
1380

    
1381
typedef struct QEMUOption {
1382
    const char *name;
1383
    int flags;
1384
    int index;
1385
    uint32_t arch_mask;
1386
} QEMUOption;
1387

    
1388
static const QEMUOption qemu_options[] = {
1389
    { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1390
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1391
    { option, opt_arg, opt_enum, arch_mask },
1392
#define DEFHEADING(text)
1393
#include "qemu-options.def"
1394
#undef DEF
1395
#undef DEFHEADING
1396
#undef GEN_DOCS
1397
    { NULL },
1398
};
1399
static void select_vgahw (const char *p)
1400
{
1401
    const char *opts;
1402

    
1403
    default_vga = 0;
1404
    vga_interface_type = VGA_NONE;
1405
    if (strstart(p, "std", &opts)) {
1406
        vga_interface_type = VGA_STD;
1407
    } else if (strstart(p, "cirrus", &opts)) {
1408
        vga_interface_type = VGA_CIRRUS;
1409
    } else if (strstart(p, "vmware", &opts)) {
1410
        vga_interface_type = VGA_VMWARE;
1411
    } else if (strstart(p, "xenfb", &opts)) {
1412
        vga_interface_type = VGA_XENFB;
1413
    } else if (!strstart(p, "none", &opts)) {
1414
    invalid_vga:
1415
        fprintf(stderr, "Unknown vga type: %s\n", p);
1416
        exit(1);
1417
    }
1418
    while (*opts) {
1419
        const char *nextopt;
1420

    
1421
        if (strstart(opts, ",retrace=", &nextopt)) {
1422
            opts = nextopt;
1423
            if (strstart(opts, "dumb", &nextopt))
1424
                vga_retrace_method = VGA_RETRACE_DUMB;
1425
            else if (strstart(opts, "precise", &nextopt))
1426
                vga_retrace_method = VGA_RETRACE_PRECISE;
1427
            else goto invalid_vga;
1428
        } else goto invalid_vga;
1429
        opts = nextopt;
1430
    }
1431
}
1432

    
1433
static int balloon_parse(const char *arg)
1434
{
1435
    QemuOpts *opts;
1436

    
1437
    if (strcmp(arg, "none") == 0) {
1438
        return 0;
1439
    }
1440

    
1441
    if (!strncmp(arg, "virtio", 6)) {
1442
        if (arg[6] == ',') {
1443
            /* have params -> parse them */
1444
            opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1445
            if (!opts)
1446
                return  -1;
1447
        } else {
1448
            /* create empty opts */
1449
            opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1450
        }
1451
        qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1452
        return 0;
1453
    }
1454

    
1455
    return -1;
1456
}
1457

    
1458
char *qemu_find_file(int type, const char *name)
1459
{
1460
    int len;
1461
    const char *subdir;
1462
    char *buf;
1463

    
1464
    /* If name contains path separators then try it as a straight path.  */
1465
    if ((strchr(name, '/') || strchr(name, '\\'))
1466
        && access(name, R_OK) == 0) {
1467
        return qemu_strdup(name);
1468
    }
1469
    switch (type) {
1470
    case QEMU_FILE_TYPE_BIOS:
1471
        subdir = "";
1472
        break;
1473
    case QEMU_FILE_TYPE_KEYMAP:
1474
        subdir = "keymaps/";
1475
        break;
1476
    default:
1477
        abort();
1478
    }
1479
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1480
    buf = qemu_mallocz(len);
1481
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1482
    if (access(buf, R_OK)) {
1483
        qemu_free(buf);
1484
        return NULL;
1485
    }
1486
    return buf;
1487
}
1488

    
1489
static int device_help_func(QemuOpts *opts, void *opaque)
1490
{
1491
    return qdev_device_help(opts);
1492
}
1493

    
1494
static int device_init_func(QemuOpts *opts, void *opaque)
1495
{
1496
    DeviceState *dev;
1497

    
1498
    dev = qdev_device_add(opts);
1499
    if (!dev)
1500
        return -1;
1501
    return 0;
1502
}
1503

    
1504
static int chardev_init_func(QemuOpts *opts, void *opaque)
1505
{
1506
    CharDriverState *chr;
1507

    
1508
    chr = qemu_chr_open_opts(opts, NULL);
1509
    if (!chr)
1510
        return -1;
1511
    return 0;
1512
}
1513

    
1514
#ifdef CONFIG_VIRTFS
1515
static int fsdev_init_func(QemuOpts *opts, void *opaque)
1516
{
1517
    int ret;
1518
    ret = qemu_fsdev_add(opts);
1519

    
1520
    return ret;
1521
}
1522
#endif
1523

    
1524
static int mon_init_func(QemuOpts *opts, void *opaque)
1525
{
1526
    CharDriverState *chr;
1527
    const char *chardev;
1528
    const char *mode;
1529
    int flags;
1530

    
1531
    mode = qemu_opt_get(opts, "mode");
1532
    if (mode == NULL) {
1533
        mode = "readline";
1534
    }
1535
    if (strcmp(mode, "readline") == 0) {
1536
        flags = MONITOR_USE_READLINE;
1537
    } else if (strcmp(mode, "control") == 0) {
1538
        flags = MONITOR_USE_CONTROL;
1539
    } else {
1540
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1541
        exit(1);
1542
    }
1543

    
1544
    if (qemu_opt_get_bool(opts, "pretty", 0))
1545
        flags |= MONITOR_USE_PRETTY;
1546

    
1547
    if (qemu_opt_get_bool(opts, "default", 0))
1548
        flags |= MONITOR_IS_DEFAULT;
1549

    
1550
    chardev = qemu_opt_get(opts, "chardev");
1551
    chr = qemu_chr_find(chardev);
1552
    if (chr == NULL) {
1553
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1554
        exit(1);
1555
    }
1556

    
1557
    monitor_init(chr, flags);
1558
    return 0;
1559
}
1560

    
1561
static void monitor_parse(const char *optarg, const char *mode)
1562
{
1563
    static int monitor_device_index = 0;
1564
    QemuOpts *opts;
1565
    const char *p;
1566
    char label[32];
1567
    int def = 0;
1568

    
1569
    if (strstart(optarg, "chardev:", &p)) {
1570
        snprintf(label, sizeof(label), "%s", p);
1571
    } else {
1572
        snprintf(label, sizeof(label), "compat_monitor%d",
1573
                 monitor_device_index);
1574
        if (monitor_device_index == 0) {
1575
            def = 1;
1576
        }
1577
        opts = qemu_chr_parse_compat(label, optarg);
1578
        if (!opts) {
1579
            fprintf(stderr, "parse error: %s\n", optarg);
1580
            exit(1);
1581
        }
1582
    }
1583

    
1584
    opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1585
    if (!opts) {
1586
        fprintf(stderr, "duplicate chardev: %s\n", label);
1587
        exit(1);
1588
    }
1589
    qemu_opt_set(opts, "mode", mode);
1590
    qemu_opt_set(opts, "chardev", label);
1591
    if (def)
1592
        qemu_opt_set(opts, "default", "on");
1593
    monitor_device_index++;
1594
}
1595

    
1596
struct device_config {
1597
    enum {
1598
        DEV_USB,       /* -usbdevice     */
1599
        DEV_BT,        /* -bt            */
1600
        DEV_SERIAL,    /* -serial        */
1601
        DEV_PARALLEL,  /* -parallel      */
1602
        DEV_VIRTCON,   /* -virtioconsole */
1603
        DEV_DEBUGCON,  /* -debugcon */
1604
    } type;
1605
    const char *cmdline;
1606
    QTAILQ_ENTRY(device_config) next;
1607
};
1608
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1609

    
1610
static void add_device_config(int type, const char *cmdline)
1611
{
1612
    struct device_config *conf;
1613

    
1614
    conf = qemu_mallocz(sizeof(*conf));
1615
    conf->type = type;
1616
    conf->cmdline = cmdline;
1617
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1618
}
1619

    
1620
static int foreach_device_config(int type, int (*func)(const char *cmdline))
1621
{
1622
    struct device_config *conf;
1623
    int rc;
1624

    
1625
    QTAILQ_FOREACH(conf, &device_configs, next) {
1626
        if (conf->type != type)
1627
            continue;
1628
        rc = func(conf->cmdline);
1629
        if (0 != rc)
1630
            return rc;
1631
    }
1632
    return 0;
1633
}
1634

    
1635
static int serial_parse(const char *devname)
1636
{
1637
    static int index = 0;
1638
    char label[32];
1639

    
1640
    if (strcmp(devname, "none") == 0)
1641
        return 0;
1642
    if (index == MAX_SERIAL_PORTS) {
1643
        fprintf(stderr, "qemu: too many serial ports\n");
1644
        exit(1);
1645
    }
1646
    snprintf(label, sizeof(label), "serial%d", index);
1647
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
1648
    if (!serial_hds[index]) {
1649
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1650
                devname, strerror(errno));
1651
        return -1;
1652
    }
1653
    index++;
1654
    return 0;
1655
}
1656

    
1657
static int parallel_parse(const char *devname)
1658
{
1659
    static int index = 0;
1660
    char label[32];
1661

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

    
1679
static int virtcon_parse(const char *devname)
1680
{
1681
    QemuOptsList *device = qemu_find_opts("device");
1682
    static int index = 0;
1683
    char label[32];
1684
    QemuOpts *bus_opts, *dev_opts;
1685

    
1686
    if (strcmp(devname, "none") == 0)
1687
        return 0;
1688
    if (index == MAX_VIRTIO_CONSOLES) {
1689
        fprintf(stderr, "qemu: too many virtio consoles\n");
1690
        exit(1);
1691
    }
1692

    
1693
    bus_opts = qemu_opts_create(device, NULL, 0);
1694
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
1695

    
1696
    dev_opts = qemu_opts_create(device, NULL, 0);
1697
    qemu_opt_set(dev_opts, "driver", "virtconsole");
1698

    
1699
    snprintf(label, sizeof(label), "virtcon%d", index);
1700
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1701
    if (!virtcon_hds[index]) {
1702
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1703
                devname, strerror(errno));
1704
        return -1;
1705
    }
1706
    qemu_opt_set(dev_opts, "chardev", label);
1707

    
1708
    index++;
1709
    return 0;
1710
}
1711

    
1712
static int debugcon_parse(const char *devname)
1713
{   
1714
    QemuOpts *opts;
1715

    
1716
    if (!qemu_chr_open("debugcon", devname, NULL)) {
1717
        exit(1);
1718
    }
1719
    opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1720
    if (!opts) {
1721
        fprintf(stderr, "qemu: already have a debugcon device\n");
1722
        exit(1);
1723
    }
1724
    qemu_opt_set(opts, "driver", "isa-debugcon");
1725
    qemu_opt_set(opts, "chardev", "debugcon");
1726
    return 0;
1727
}
1728

    
1729
void qemu_add_exit_notifier(Notifier *notify)
1730
{
1731
    notifier_list_add(&exit_notifiers, notify);
1732
}
1733

    
1734
void qemu_remove_exit_notifier(Notifier *notify)
1735
{
1736
    notifier_list_remove(&exit_notifiers, notify);
1737
}
1738

    
1739
static void qemu_run_exit_notifiers(void)
1740
{
1741
    notifier_list_notify(&exit_notifiers);
1742
}
1743

    
1744
static const QEMUOption *lookup_opt(int argc, char **argv,
1745
                                    const char **poptarg, int *poptind)
1746
{
1747
    const QEMUOption *popt;
1748
    int optind = *poptind;
1749
    char *r = argv[optind];
1750
    const char *optarg;
1751

    
1752
    loc_set_cmdline(argv, optind, 1);
1753
    optind++;
1754
    /* Treat --foo the same as -foo.  */
1755
    if (r[1] == '-')
1756
        r++;
1757
    popt = qemu_options;
1758
    for(;;) {
1759
        if (!popt->name) {
1760
            error_report("invalid option");
1761
            exit(1);
1762
        }
1763
        if (!strcmp(popt->name, r + 1))
1764
            break;
1765
        popt++;
1766
    }
1767
    if (popt->flags & HAS_ARG) {
1768
        if (optind >= argc) {
1769
            error_report("requires an argument");
1770
            exit(1);
1771
        }
1772
        optarg = argv[optind++];
1773
        loc_set_cmdline(argv, optind - 2, 2);
1774
    } else {
1775
        optarg = NULL;
1776
    }
1777

    
1778
    *poptarg = optarg;
1779
    *poptind = optind;
1780

    
1781
    return popt;
1782
}
1783

    
1784
int main(int argc, char **argv, char **envp)
1785
{
1786
    const char *gdbstub_dev = NULL;
1787
    int i;
1788
    int snapshot, linux_boot;
1789
    const char *icount_option = NULL;
1790
    const char *initrd_filename;
1791
    const char *kernel_filename, *kernel_cmdline;
1792
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1793
    DisplayState *ds;
1794
    DisplayChangeListener *dcl;
1795
    int cyls, heads, secs, translation;
1796
    QemuOpts *hda_opts = NULL, *opts;
1797
    QemuOptsList *olist;
1798
    int optind;
1799
    const char *optarg;
1800
    const char *loadvm = NULL;
1801
    QEMUMachine *machine;
1802
    const char *cpu_model;
1803
    int tb_size;
1804
    const char *pid_file = NULL;
1805
    const char *incoming = NULL;
1806
    int show_vnc_port = 0;
1807
    int defconfig = 1;
1808

    
1809
#ifdef CONFIG_SIMPLE_TRACE
1810
    const char *trace_file = NULL;
1811
#endif
1812
    atexit(qemu_run_exit_notifiers);
1813
    error_set_progname(argv[0]);
1814

    
1815
    init_clocks();
1816

    
1817
    qemu_cache_utils_init(envp);
1818

    
1819
    QLIST_INIT (&vm_change_state_head);
1820
    os_setup_early_signal_handling();
1821

    
1822
    module_call_init(MODULE_INIT_MACHINE);
1823
    machine = find_default_machine();
1824
    cpu_model = NULL;
1825
    initrd_filename = NULL;
1826
    ram_size = 0;
1827
    snapshot = 0;
1828
    kernel_filename = NULL;
1829
    kernel_cmdline = "";
1830
    cyls = heads = secs = 0;
1831
    translation = BIOS_ATA_TRANSLATION_AUTO;
1832

    
1833
    for (i = 0; i < MAX_NODES; i++) {
1834
        node_mem[i] = 0;
1835
        node_cpumask[i] = 0;
1836
    }
1837

    
1838
    nb_numa_nodes = 0;
1839
    nb_nics = 0;
1840

    
1841
    tb_size = 0;
1842
    autostart= 1;
1843

    
1844
    /* first pass of option parsing */
1845
    optind = 1;
1846
    while (optind < argc) {
1847
        if (argv[optind][0] != '-') {
1848
            /* disk image */
1849
            optind++;
1850
            continue;
1851
        } else {
1852
            const QEMUOption *popt;
1853

    
1854
            popt = lookup_opt(argc, argv, &optarg, &optind);
1855
            switch (popt->index) {
1856
            case QEMU_OPTION_nodefconfig:
1857
                defconfig=0;
1858
                break;
1859
            }
1860
        }
1861
    }
1862

    
1863
    if (defconfig) {
1864
        int ret;
1865

    
1866
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1867
        if (ret < 0 && ret != -ENOENT) {
1868
            exit(1);
1869
        }
1870

    
1871
        ret = qemu_read_config_file(arch_config_name);
1872
        if (ret < 0 && ret != -ENOENT) {
1873
            exit(1);
1874
        }
1875
    }
1876
    cpudef_init();
1877

    
1878
    /* second pass of option parsing */
1879
    optind = 1;
1880
    for(;;) {
1881
        if (optind >= argc)
1882
            break;
1883
        if (argv[optind][0] != '-') {
1884
            hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
1885
        } else {
1886
            const QEMUOption *popt;
1887

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

    
2047
                    if (!strchr(optarg, '=')) {
2048
                        legacy = 1;
2049
                        pstrcpy(buf, sizeof(buf), optarg);
2050
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2051
                        fprintf(stderr,
2052
                                "qemu: unknown boot parameter '%s' in '%s'\n",
2053
                                buf, optarg);
2054
                        exit(1);
2055
                    }
2056

    
2057
                    if (legacy ||
2058
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
2059
                        validate_bootdevices(buf);
2060
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
2061
                    }
2062
                    if (!legacy) {
2063
                        if (get_param_value(buf, sizeof(buf),
2064
                                            "once", optarg)) {
2065
                            validate_bootdevices(buf);
2066
                            standard_boot_devices = qemu_strdup(boot_devices);
2067
                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
2068
                            qemu_register_reset(restore_boot_devices,
2069
                                                standard_boot_devices);
2070
                        }
2071
                        if (get_param_value(buf, sizeof(buf),
2072
                                            "menu", optarg)) {
2073
                            if (!strcmp(buf, "on")) {
2074
                                boot_menu = 1;
2075
                            } else if (!strcmp(buf, "off")) {
2076
                                boot_menu = 0;
2077
                            } else {
2078
                                fprintf(stderr,
2079
                                        "qemu: invalid option value '%s'\n",
2080
                                        buf);
2081
                                exit(1);
2082
                            }
2083
                        }
2084
                    }
2085
                }
2086
                break;
2087
            case QEMU_OPTION_fda:
2088
            case QEMU_OPTION_fdb:
2089
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
2090
                break;
2091
            case QEMU_OPTION_no_fd_bootchk:
2092
                fd_bootchk = 0;
2093
                break;
2094
            case QEMU_OPTION_netdev:
2095
                if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2096
                    exit(1);
2097
                }
2098
                break;
2099
            case QEMU_OPTION_net:
2100
                if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2101
                    exit(1);
2102
                }
2103
                break;
2104
#ifdef CONFIG_SLIRP
2105
            case QEMU_OPTION_tftp:
2106
                legacy_tftp_prefix = optarg;
2107
                break;
2108
            case QEMU_OPTION_bootp:
2109
                legacy_bootp_filename = optarg;
2110
                break;
2111
            case QEMU_OPTION_redir:
2112
                if (net_slirp_redir(optarg) < 0)
2113
                    exit(1);
2114
                break;
2115
#endif
2116
            case QEMU_OPTION_bt:
2117
                add_device_config(DEV_BT, optarg);
2118
                break;
2119
            case QEMU_OPTION_audio_help:
2120
                if (!(audio_available())) {
2121
                    printf("Option %s not supported for this target\n", popt->name);
2122
                    exit(1);
2123
                }
2124
                AUD_help ();
2125
                exit (0);
2126
                break;
2127
            case QEMU_OPTION_soundhw:
2128
                if (!(audio_available())) {
2129
                    printf("Option %s not supported for this target\n", popt->name);
2130
                    exit(1);
2131
                }
2132
                select_soundhw (optarg);
2133
                break;
2134
            case QEMU_OPTION_h:
2135
                help(0);
2136
                break;
2137
            case QEMU_OPTION_version:
2138
                version();
2139
                exit(0);
2140
                break;
2141
            case QEMU_OPTION_m: {
2142
                uint64_t value;
2143
                char *ptr;
2144

    
2145
                value = strtoul(optarg, &ptr, 10);
2146
                switch (*ptr) {
2147
                case 0: case 'M': case 'm':
2148
                    value <<= 20;
2149
                    break;
2150
                case 'G': case 'g':
2151
                    value <<= 30;
2152
                    break;
2153
                default:
2154
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2155
                    exit(1);
2156
                }
2157

    
2158
                /* On 32-bit hosts, QEMU is limited by virtual address space */
2159
                if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2160
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2161
                    exit(1);
2162
                }
2163
                if (value != (uint64_t)(ram_addr_t)value) {
2164
                    fprintf(stderr, "qemu: ram size too large\n");
2165
                    exit(1);
2166
                }
2167
                ram_size = value;
2168
                break;
2169
            }
2170
            case QEMU_OPTION_mempath:
2171
                mem_path = optarg;
2172
                break;
2173
#ifdef MAP_POPULATE
2174
            case QEMU_OPTION_mem_prealloc:
2175
                mem_prealloc = 1;
2176
                break;
2177
#endif
2178
            case QEMU_OPTION_d:
2179
                set_cpu_log(optarg);
2180
                break;
2181
            case QEMU_OPTION_s:
2182
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2183
                break;
2184
            case QEMU_OPTION_gdb:
2185
                gdbstub_dev = optarg;
2186
                break;
2187
            case QEMU_OPTION_L:
2188
                data_dir = optarg;
2189
                break;
2190
            case QEMU_OPTION_bios:
2191
                bios_name = optarg;
2192
                break;
2193
            case QEMU_OPTION_singlestep:
2194
                singlestep = 1;
2195
                break;
2196
            case QEMU_OPTION_S:
2197
                autostart = 0;
2198
                break;
2199
            case QEMU_OPTION_k:
2200
                keyboard_layout = optarg;
2201
                break;
2202
            case QEMU_OPTION_localtime:
2203
                rtc_utc = 0;
2204
                break;
2205
            case QEMU_OPTION_vga:
2206
                select_vgahw (optarg);
2207
                break;
2208
            case QEMU_OPTION_g:
2209
                {
2210
                    const char *p;
2211
                    int w, h, depth;
2212
                    p = optarg;
2213
                    w = strtol(p, (char **)&p, 10);
2214
                    if (w <= 0) {
2215
                    graphic_error:
2216
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
2217
                        exit(1);
2218
                    }
2219
                    if (*p != 'x')
2220
                        goto graphic_error;
2221
                    p++;
2222
                    h = strtol(p, (char **)&p, 10);
2223
                    if (h <= 0)
2224
                        goto graphic_error;
2225
                    if (*p == 'x') {
2226
                        p++;
2227
                        depth = strtol(p, (char **)&p, 10);
2228
                        if (depth != 8 && depth != 15 && depth != 16 &&
2229
                            depth != 24 && depth != 32)
2230
                            goto graphic_error;
2231
                    } else if (*p == '\0') {
2232
                        depth = graphic_depth;
2233
                    } else {
2234
                        goto graphic_error;
2235
                    }
2236

    
2237
                    graphic_width = w;
2238
                    graphic_height = h;
2239
                    graphic_depth = depth;
2240
                }
2241
                break;
2242
            case QEMU_OPTION_echr:
2243
                {
2244
                    char *r;
2245
                    term_escape_char = strtol(optarg, &r, 0);
2246
                    if (r == optarg)
2247
                        printf("Bad argument to echr\n");
2248
                    break;
2249
                }
2250
            case QEMU_OPTION_monitor:
2251
                monitor_parse(optarg, "readline");
2252
                default_monitor = 0;
2253
                break;
2254
            case QEMU_OPTION_qmp:
2255
                monitor_parse(optarg, "control");
2256
                default_monitor = 0;
2257
                break;
2258
            case QEMU_OPTION_mon:
2259
                opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2260
                if (!opts) {
2261
                    exit(1);
2262
                }
2263
                default_monitor = 0;
2264
                break;
2265
            case QEMU_OPTION_chardev:
2266
                opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2267
                if (!opts) {
2268
                    exit(1);
2269
                }
2270
                break;
2271
            case QEMU_OPTION_fsdev:
2272
                olist = qemu_find_opts("fsdev");
2273
                if (!olist) {
2274
                    fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2275
                    exit(1);
2276
                }
2277
                opts = qemu_opts_parse(olist, optarg, 1);
2278
                if (!opts) {
2279
                    fprintf(stderr, "parse error: %s\n", optarg);
2280
                    exit(1);
2281
                }
2282
                break;
2283
            case QEMU_OPTION_virtfs: {
2284
                char *arg_fsdev = NULL;
2285
                char *arg_9p = NULL;
2286
                int len = 0;
2287

    
2288
                olist = qemu_find_opts("virtfs");
2289
                if (!olist) {
2290
                    fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2291
                    exit(1);
2292
                }
2293
                opts = qemu_opts_parse(olist, optarg, 1);
2294
                if (!opts) {
2295
                    fprintf(stderr, "parse error: %s\n", optarg);
2296
                    exit(1);
2297
                }
2298

    
2299
                if (qemu_opt_get(opts, "fstype") == NULL ||
2300
                        qemu_opt_get(opts, "mount_tag") == NULL ||
2301
                        qemu_opt_get(opts, "path") == NULL ||
2302
                        qemu_opt_get(opts, "security_model") == NULL) {
2303
                    fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2304
                            "security_model=[mapped|passthrough|none],"
2305
                            "mnt_tag=tag.\n");
2306
                    exit(1);
2307
                }
2308

    
2309
                len = strlen(",id=,path=,security_model=");
2310
                len += strlen(qemu_opt_get(opts, "fstype"));
2311
                len += strlen(qemu_opt_get(opts, "mount_tag"));
2312
                len += strlen(qemu_opt_get(opts, "path"));
2313
                len += strlen(qemu_opt_get(opts, "security_model"));
2314
                arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2315

    
2316
                snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2317
                         "%s,id=%s,path=%s,security_model=%s",
2318
                         qemu_opt_get(opts, "fstype"),
2319
                         qemu_opt_get(opts, "mount_tag"),
2320
                         qemu_opt_get(opts, "path"),
2321
                         qemu_opt_get(opts, "security_model"));
2322

    
2323
                len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2324
                len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2325
                arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2326

    
2327
                snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2328
                         "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2329
                         qemu_opt_get(opts, "mount_tag"),
2330
                         qemu_opt_get(opts, "mount_tag"));
2331

    
2332
                if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2333
                    fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2334
                    exit(1);
2335
                }
2336

    
2337
                if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2338
                    fprintf(stderr, "parse error [device]: %s\n", optarg);
2339
                    exit(1);
2340
                }
2341

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

    
2632
    /* If no data_dir is specified then try to find it relative to the
2633
       executable path.  */
2634
    if (!data_dir) {
2635
        data_dir = os_find_datadir(argv[0]);
2636
    }
2637
    /* If all else fails use the install patch specified when building.  */
2638
    if (!data_dir) {
2639
        data_dir = CONFIG_QEMU_DATADIR;
2640
    }
2641

    
2642
#ifdef CONFIG_SIMPLE_TRACE
2643
    /*
2644
     * Set the trace file name, if specified.
2645
     */
2646
    st_set_trace_file(trace_file);
2647
#endif
2648
    /*
2649
     * Default to max_cpus = smp_cpus, in case the user doesn't
2650
     * specify a max_cpus value.
2651
     */
2652
    if (!max_cpus)
2653
        max_cpus = smp_cpus;
2654

    
2655
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2656
    if (smp_cpus > machine->max_cpus) {
2657
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2658
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
2659
                machine->max_cpus);
2660
        exit(1);
2661
    }
2662

    
2663
    qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2664
    qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2665

    
2666
    if (machine->no_serial) {
2667
        default_serial = 0;
2668
    }
2669
    if (machine->no_parallel) {
2670
        default_parallel = 0;
2671
    }
2672
    if (!machine->use_virtcon) {
2673
        default_virtcon = 0;
2674
    }
2675
    if (machine->no_vga) {
2676
        default_vga = 0;
2677
    }
2678
    if (machine->no_floppy) {
2679
        default_floppy = 0;
2680
    }
2681
    if (machine->no_cdrom) {
2682
        default_cdrom = 0;
2683
    }
2684
    if (machine->no_sdcard) {
2685
        default_sdcard = 0;
2686
    }
2687

    
2688
    if (display_type == DT_NOGRAPHIC) {
2689
        if (default_parallel)
2690
            add_device_config(DEV_PARALLEL, "null");
2691
        if (default_serial && default_monitor) {
2692
            add_device_config(DEV_SERIAL, "mon:stdio");
2693
        } else if (default_virtcon && default_monitor) {
2694
            add_device_config(DEV_VIRTCON, "mon:stdio");
2695
        } else {
2696
            if (default_serial)
2697
                add_device_config(DEV_SERIAL, "stdio");
2698
            if (default_virtcon)
2699
                add_device_config(DEV_VIRTCON, "stdio");
2700
            if (default_monitor)
2701
                monitor_parse("stdio", "readline");
2702
        }
2703
    } else {
2704
        if (default_serial)
2705
            add_device_config(DEV_SERIAL, "vc:80Cx24C");
2706
        if (default_parallel)
2707
            add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2708
        if (default_monitor)
2709
            monitor_parse("vc:80Cx24C", "readline");
2710
        if (default_virtcon)
2711
            add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2712
    }
2713
    if (default_vga)
2714
        vga_interface_type = VGA_CIRRUS;
2715

    
2716
    socket_init();
2717

    
2718
    if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2719
        exit(1);
2720
#ifdef CONFIG_VIRTFS
2721
    if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2722
        exit(1);
2723
    }
2724
#endif
2725

    
2726
    os_daemonize();
2727

    
2728
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2729
        os_pidfile_error();
2730
        exit(1);
2731
    }
2732

    
2733
    if (kvm_allowed) {
2734
        int ret = kvm_init(smp_cpus);
2735
        if (ret < 0) {
2736
            if (!kvm_available()) {
2737
                printf("KVM not supported for this target\n");
2738
            } else {
2739
                fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2740
            }
2741
            exit(1);
2742
        }
2743
    }
2744

    
2745
    if (qemu_init_main_loop()) {
2746
        fprintf(stderr, "qemu_init_main_loop failed\n");
2747
        exit(1);
2748
    }
2749
    linux_boot = (kernel_filename != NULL);
2750

    
2751
    if (!linux_boot && *kernel_cmdline != '\0') {
2752
        fprintf(stderr, "-append only allowed with -kernel option\n");
2753
        exit(1);
2754
    }
2755

    
2756
    if (!linux_boot && initrd_filename != NULL) {
2757
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
2758
        exit(1);
2759
    }
2760

    
2761
    os_set_line_buffering();
2762

    
2763
    if (init_timer_alarm() < 0) {
2764
        fprintf(stderr, "could not initialize alarm timer\n");
2765
        exit(1);
2766
    }
2767
    configure_icount(icount_option);
2768

    
2769
    if (net_init_clients() < 0) {
2770
        exit(1);
2771
    }
2772

    
2773
    /* init the bluetooth world */
2774
    if (foreach_device_config(DEV_BT, bt_parse))
2775
        exit(1);
2776

    
2777
    /* init the memory */
2778
    if (ram_size == 0)
2779
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2780

    
2781
    /* init the dynamic translator */
2782
    cpu_exec_init_all(tb_size * 1024 * 1024);
2783

    
2784
    bdrv_init_with_whitelist();
2785

    
2786
    blk_mig_init();
2787

    
2788
    if (default_cdrom) {
2789
        /* we always create the cdrom drive, even if no disk is there */
2790
        drive_add(NULL, CDROM_ALIAS);
2791
    }
2792

    
2793
    if (default_floppy) {
2794
        /* we always create at least one floppy */
2795
        drive_add(NULL, FD_ALIAS, 0);
2796
    }
2797

    
2798
    if (default_sdcard) {
2799
        /* we always create one sd slot, even if no card is in it */
2800
        drive_add(NULL, SD_ALIAS);
2801
    }
2802

    
2803
    /* open the virtual block devices */
2804
    if (snapshot)
2805
        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2806
    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2807
        exit(1);
2808

    
2809
    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2810
                         ram_load, NULL);
2811

    
2812
    if (nb_numa_nodes > 0) {
2813
        int i;
2814

    
2815
        if (nb_numa_nodes > smp_cpus) {
2816
            nb_numa_nodes = smp_cpus;
2817
        }
2818

    
2819
        /* If no memory size if given for any node, assume the default case
2820
         * and distribute the available memory equally across all nodes
2821
         */
2822
        for (i = 0; i < nb_numa_nodes; i++) {
2823
            if (node_mem[i] != 0)
2824
                break;
2825
        }
2826
        if (i == nb_numa_nodes) {
2827
            uint64_t usedmem = 0;
2828

    
2829
            /* On Linux, the each node's border has to be 8MB aligned,
2830
             * the final node gets the rest.
2831
             */
2832
            for (i = 0; i < nb_numa_nodes - 1; i++) {
2833
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2834
                usedmem += node_mem[i];
2835
            }
2836
            node_mem[i] = ram_size - usedmem;
2837
        }
2838

    
2839
        for (i = 0; i < nb_numa_nodes; i++) {
2840
            if (node_cpumask[i] != 0)
2841
                break;
2842
        }
2843
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
2844
         * must cope with this anyway, because there are BIOSes out there in
2845
         * real machines which also use this scheme.
2846
         */
2847
        if (i == nb_numa_nodes) {
2848
            for (i = 0; i < smp_cpus; i++) {
2849
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
2850
            }
2851
        }
2852
    }
2853

    
2854
    if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2855
        exit(1);
2856
    }
2857

    
2858
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2859
        exit(1);
2860
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2861
        exit(1);
2862
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2863
        exit(1);
2864
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2865
        exit(1);
2866

    
2867
    module_call_init(MODULE_INIT_DEVICE);
2868

    
2869
    if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2870
        exit(0);
2871

    
2872
    if (watchdog) {
2873
        i = select_watchdog(watchdog);
2874
        if (i > 0)
2875
            exit (i == 1 ? 1 : 0);
2876
    }
2877

    
2878
    if (machine->compat_props) {
2879
        qdev_prop_register_global_list(machine->compat_props);
2880
    }
2881
    qemu_add_globals();
2882

    
2883
    machine->init(ram_size, boot_devices,
2884
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2885

    
2886
    cpu_synchronize_all_post_init();
2887

    
2888
    /* must be after terminal init, SDL library changes signal handlers */
2889
    os_setup_signal_handling();
2890

    
2891
    set_numa_modes();
2892

    
2893
    current_machine = machine;
2894

    
2895
    /* init USB devices */
2896
    if (usb_enabled) {
2897
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
2898
            exit(1);
2899
    }
2900

    
2901
    /* init generic devices */
2902
    if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
2903
        exit(1);
2904

    
2905
    net_check_clients();
2906

    
2907
    /* just use the first displaystate for the moment */
2908
    ds = get_displaystate();
2909

    
2910
    if (using_spice)
2911
        display_remote++;
2912
    if (display_type == DT_DEFAULT && !display_remote) {
2913
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2914
        display_type = DT_SDL;
2915
#else
2916
        vnc_display = "localhost:0,to=99";
2917
        show_vnc_port = 1;
2918
#endif
2919
    }
2920
        
2921

    
2922
    /* init local displays */
2923
    switch (display_type) {
2924
    case DT_NOGRAPHIC:
2925
        break;
2926
#if defined(CONFIG_CURSES)
2927
    case DT_CURSES:
2928
        curses_display_init(ds, full_screen);
2929
        break;
2930
#endif
2931
#if defined(CONFIG_SDL)
2932
    case DT_SDL:
2933
        sdl_display_init(ds, full_screen, no_frame);
2934
        break;
2935
#elif defined(CONFIG_COCOA)
2936
    case DT_SDL:
2937
        cocoa_display_init(ds, full_screen);
2938
        break;
2939
#endif
2940
    default:
2941
        break;
2942
    }
2943

    
2944
    /* init remote displays */
2945
    if (vnc_display) {
2946
        vnc_display_init(ds);
2947
        if (vnc_display_open(ds, vnc_display) < 0)
2948
            exit(1);
2949

    
2950
        if (show_vnc_port) {
2951
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
2952
        }
2953
    }
2954
#ifdef CONFIG_SPICE
2955
    if (using_spice) {
2956
        qemu_spice_display_init(ds);
2957
    }
2958
#endif
2959

    
2960
    /* display setup */
2961
    dpy_resize(ds);
2962
    dcl = ds->listeners;
2963
    while (dcl != NULL) {
2964
        if (dcl->dpy_refresh != NULL) {
2965
            ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
2966
            qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
2967
            break;
2968
        }
2969
        dcl = dcl->next;
2970
    }
2971
    if (ds->gui_timer == NULL) {
2972
        nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
2973
        qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
2974
    }
2975
    text_consoles_set_display(ds);
2976

    
2977
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
2978
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
2979
                gdbstub_dev);
2980
        exit(1);
2981
    }
2982

    
2983
    qdev_machine_creation_done();
2984

    
2985
    if (rom_load_all() != 0) {
2986
        fprintf(stderr, "rom loading failed\n");
2987
        exit(1);
2988
    }
2989

    
2990
    qemu_system_reset();
2991
    if (loadvm) {
2992
        if (load_vmstate(loadvm) < 0) {
2993
            autostart = 0;
2994
        }
2995
    }
2996

    
2997
    if (incoming) {
2998
        int ret = qemu_start_incoming_migration(incoming);
2999
        if (ret < 0) {
3000
            fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3001
                    incoming, ret);
3002
            exit(ret);
3003
        }
3004
    } else if (autostart) {
3005
        vm_start();
3006
    }
3007

    
3008
    os_setup_post();
3009

    
3010
    main_loop();
3011
    quit_timers();
3012
    net_cleanup();
3013

    
3014
    return 0;
3015
}