Statistics
| Branch: | Revision:

root / vl.c @ b4a3d965

History | View | Annotate | Download (87.6 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 "trace.h"
162
#include "qemu-queue.h"
163
#include "cpus.h"
164
#include "arch_init.h"
165

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

    
168
//#define DEBUG_NET
169
//#define DEBUG_SLIRP
170

    
171
#define DEFAULT_RAM_SIZE 128
172

    
173
#define MAX_VIRTIO_CONSOLES 1
174

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

    
232
typedef struct FWBootEntry FWBootEntry;
233

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

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

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

    
247
static QEMUTimer *nographic_timer;
248

    
249
uint8_t qemu_uuid[16];
250

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

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

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

    
260
int kvm_allowed = 0;
261
uint32_t xen_domid;
262
enum xen_mode xen_mode = XEN_EMULATE;
263

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
510
    return 0;
511
}
512

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

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

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

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

    
535
    hci_table[nb_hcis++] = hci;
536

    
537
    return 0;
538
}
539

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

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

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

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

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

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

    
569
    vlan = qemu_find_bt_vlan(vlan_id);
570

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
722
    qemu_boot_set(standard_boot_devices);
723

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

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

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

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

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

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

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

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

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

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

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

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

    
802
    *size = total;
803

    
804
    return list;
805
}
806

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

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

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

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

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

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

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

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

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

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

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

    
932
done:
933
    return 0;
934
}
935

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1025
/***********************************************************/
1026
/* I/O handling */
1027

    
1028
typedef struct IOHandlerRecord {
1029
    int fd;
1030
    IOCanReadHandler *fd_read_poll;
1031
    IOHandler *fd_read;
1032
    IOHandler *fd_write;
1033
    int deleted;
1034
    void *opaque;
1035
    /* temporary data */
1036
    struct pollfd *ufd;
1037
    QLIST_ENTRY(IOHandlerRecord) next;
1038
} IOHandlerRecord;
1039

    
1040
static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1041
    QLIST_HEAD_INITIALIZER(io_handlers);
1042

    
1043

    
1044
/* XXX: fd_read_poll should be suppressed, but an API change is
1045
   necessary in the character devices to suppress fd_can_read(). */
1046
int qemu_set_fd_handler2(int fd,
1047
                         IOCanReadHandler *fd_read_poll,
1048
                         IOHandler *fd_read,
1049
                         IOHandler *fd_write,
1050
                         void *opaque)
1051
{
1052
    IOHandlerRecord *ioh;
1053

    
1054
    if (!fd_read && !fd_write) {
1055
        QLIST_FOREACH(ioh, &io_handlers, next) {
1056
            if (ioh->fd == fd) {
1057
                ioh->deleted = 1;
1058
                break;
1059
            }
1060
        }
1061
    } else {
1062
        QLIST_FOREACH(ioh, &io_handlers, next) {
1063
            if (ioh->fd == fd)
1064
                goto found;
1065
        }
1066
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1067
        QLIST_INSERT_HEAD(&io_handlers, ioh, next);
1068
    found:
1069
        ioh->fd = fd;
1070
        ioh->fd_read_poll = fd_read_poll;
1071
        ioh->fd_read = fd_read;
1072
        ioh->fd_write = fd_write;
1073
        ioh->opaque = opaque;
1074
        ioh->deleted = 0;
1075
    }
1076
    return 0;
1077
}
1078

    
1079
int qemu_set_fd_handler(int fd,
1080
                        IOHandler *fd_read,
1081
                        IOHandler *fd_write,
1082
                        void *opaque)
1083
{
1084
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
1085
}
1086

    
1087
/***********************************************************/
1088
/* machine registration */
1089

    
1090
static QEMUMachine *first_machine = NULL;
1091
QEMUMachine *current_machine = NULL;
1092

    
1093
int qemu_register_machine(QEMUMachine *m)
1094
{
1095
    QEMUMachine **pm;
1096
    pm = &first_machine;
1097
    while (*pm != NULL)
1098
        pm = &(*pm)->next;
1099
    m->next = NULL;
1100
    *pm = m;
1101
    return 0;
1102
}
1103

    
1104
static QEMUMachine *find_machine(const char *name)
1105
{
1106
    QEMUMachine *m;
1107

    
1108
    for(m = first_machine; m != NULL; m = m->next) {
1109
        if (!strcmp(m->name, name))
1110
            return m;
1111
        if (m->alias && !strcmp(m->alias, name))
1112
            return m;
1113
    }
1114
    return NULL;
1115
}
1116

    
1117
static QEMUMachine *find_default_machine(void)
1118
{
1119
    QEMUMachine *m;
1120

    
1121
    for(m = first_machine; m != NULL; m = m->next) {
1122
        if (m->is_default) {
1123
            return m;
1124
        }
1125
    }
1126
    return NULL;
1127
}
1128

    
1129
/***********************************************************/
1130
/* main execution loop */
1131

    
1132
static void gui_update(void *opaque)
1133
{
1134
    uint64_t interval = GUI_REFRESH_INTERVAL;
1135
    DisplayState *ds = opaque;
1136
    DisplayChangeListener *dcl = ds->listeners;
1137

    
1138
    qemu_flush_coalesced_mmio_buffer();
1139
    dpy_refresh(ds);
1140

    
1141
    while (dcl != NULL) {
1142
        if (dcl->gui_timer_interval &&
1143
            dcl->gui_timer_interval < interval)
1144
            interval = dcl->gui_timer_interval;
1145
        dcl = dcl->next;
1146
    }
1147
    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
1148
}
1149

    
1150
static void nographic_update(void *opaque)
1151
{
1152
    uint64_t interval = GUI_REFRESH_INTERVAL;
1153

    
1154
    qemu_flush_coalesced_mmio_buffer();
1155
    qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1156
}
1157

    
1158
struct vm_change_state_entry {
1159
    VMChangeStateHandler *cb;
1160
    void *opaque;
1161
    QLIST_ENTRY (vm_change_state_entry) entries;
1162
};
1163

    
1164
static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1165

    
1166
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1167
                                                     void *opaque)
1168
{
1169
    VMChangeStateEntry *e;
1170

    
1171
    e = qemu_mallocz(sizeof (*e));
1172

    
1173
    e->cb = cb;
1174
    e->opaque = opaque;
1175
    QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1176
    return e;
1177
}
1178

    
1179
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1180
{
1181
    QLIST_REMOVE (e, entries);
1182
    qemu_free (e);
1183
}
1184

    
1185
void vm_state_notify(int running, int reason)
1186
{
1187
    VMChangeStateEntry *e;
1188

    
1189
    trace_vm_state_notify(running, reason);
1190

    
1191
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1192
        e->cb(e->opaque, running, reason);
1193
    }
1194
}
1195

    
1196
void vm_start(void)
1197
{
1198
    if (!vm_running) {
1199
        cpu_enable_ticks();
1200
        vm_running = 1;
1201
        vm_state_notify(1, 0);
1202
        resume_all_vcpus();
1203
        monitor_protocol_event(QEVENT_RESUME, NULL);
1204
    }
1205
}
1206

    
1207
/* reset/shutdown handler */
1208

    
1209
typedef struct QEMUResetEntry {
1210
    QTAILQ_ENTRY(QEMUResetEntry) entry;
1211
    QEMUResetHandler *func;
1212
    void *opaque;
1213
} QEMUResetEntry;
1214

    
1215
static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1216
    QTAILQ_HEAD_INITIALIZER(reset_handlers);
1217
static int reset_requested;
1218
static int shutdown_requested;
1219
static int powerdown_requested;
1220
int debug_requested;
1221
int vmstop_requested;
1222

    
1223
int qemu_shutdown_requested(void)
1224
{
1225
    int r = shutdown_requested;
1226
    shutdown_requested = 0;
1227
    return r;
1228
}
1229

    
1230
int qemu_reset_requested(void)
1231
{
1232
    int r = reset_requested;
1233
    reset_requested = 0;
1234
    return r;
1235
}
1236

    
1237
int qemu_powerdown_requested(void)
1238
{
1239
    int r = powerdown_requested;
1240
    powerdown_requested = 0;
1241
    return r;
1242
}
1243

    
1244
static int qemu_debug_requested(void)
1245
{
1246
    int r = debug_requested;
1247
    debug_requested = 0;
1248
    return r;
1249
}
1250

    
1251
static int qemu_vmstop_requested(void)
1252
{
1253
    int r = vmstop_requested;
1254
    vmstop_requested = 0;
1255
    return r;
1256
}
1257

    
1258
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1259
{
1260
    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
1261

    
1262
    re->func = func;
1263
    re->opaque = opaque;
1264
    QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1265
}
1266

    
1267
void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1268
{
1269
    QEMUResetEntry *re;
1270

    
1271
    QTAILQ_FOREACH(re, &reset_handlers, entry) {
1272
        if (re->func == func && re->opaque == opaque) {
1273
            QTAILQ_REMOVE(&reset_handlers, re, entry);
1274
            qemu_free(re);
1275
            return;
1276
        }
1277
    }
1278
}
1279

    
1280
void qemu_system_reset(void)
1281
{
1282
    QEMUResetEntry *re, *nre;
1283

    
1284
    /* reset all devices */
1285
    QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1286
        re->func(re->opaque);
1287
    }
1288
    monitor_protocol_event(QEVENT_RESET, NULL);
1289
    cpu_synchronize_all_post_reset();
1290
}
1291

    
1292
void qemu_system_reset_request(void)
1293
{
1294
    if (no_reboot) {
1295
        shutdown_requested = 1;
1296
    } else {
1297
        reset_requested = 1;
1298
    }
1299
    cpu_stop_current();
1300
    qemu_notify_event();
1301
}
1302

    
1303
void qemu_system_shutdown_request(void)
1304
{
1305
    shutdown_requested = 1;
1306
    qemu_notify_event();
1307
}
1308

    
1309
void qemu_system_powerdown_request(void)
1310
{
1311
    powerdown_requested = 1;
1312
    qemu_notify_event();
1313
}
1314

    
1315
void main_loop_wait(int nonblocking)
1316
{
1317
    IOHandlerRecord *ioh;
1318
    fd_set rfds, wfds, xfds;
1319
    int ret, nfds;
1320
    struct timeval tv;
1321
    int timeout;
1322

    
1323
    if (nonblocking)
1324
        timeout = 0;
1325
    else {
1326
        timeout = qemu_calculate_timeout();
1327
        qemu_bh_update_timeout(&timeout);
1328
    }
1329

    
1330
    os_host_main_loop_wait(&timeout);
1331

    
1332
    /* poll any events */
1333
    /* XXX: separate device handlers from system ones */
1334
    nfds = -1;
1335
    FD_ZERO(&rfds);
1336
    FD_ZERO(&wfds);
1337
    FD_ZERO(&xfds);
1338
    QLIST_FOREACH(ioh, &io_handlers, next) {
1339
        if (ioh->deleted)
1340
            continue;
1341
        if (ioh->fd_read &&
1342
            (!ioh->fd_read_poll ||
1343
             ioh->fd_read_poll(ioh->opaque) != 0)) {
1344
            FD_SET(ioh->fd, &rfds);
1345
            if (ioh->fd > nfds)
1346
                nfds = ioh->fd;
1347
        }
1348
        if (ioh->fd_write) {
1349
            FD_SET(ioh->fd, &wfds);
1350
            if (ioh->fd > nfds)
1351
                nfds = ioh->fd;
1352
        }
1353
    }
1354

    
1355
    tv.tv_sec = timeout / 1000;
1356
    tv.tv_usec = (timeout % 1000) * 1000;
1357

    
1358
    slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1359

    
1360
    qemu_mutex_unlock_iothread();
1361
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1362
    qemu_mutex_lock_iothread();
1363
    if (ret > 0) {
1364
        IOHandlerRecord *pioh;
1365

    
1366
        QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
1367
            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1368
                ioh->fd_read(ioh->opaque);
1369
            }
1370
            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1371
                ioh->fd_write(ioh->opaque);
1372
            }
1373

    
1374
            /* Do this last in case read/write handlers marked it for deletion */
1375
            if (ioh->deleted) {
1376
                QLIST_REMOVE(ioh, next);
1377
                qemu_free(ioh);
1378
            }
1379
        }
1380
    }
1381

    
1382
    slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
1383

    
1384
    qemu_run_all_timers();
1385

    
1386
    /* Check bottom-halves last in case any of the earlier events triggered
1387
       them.  */
1388
    qemu_bh_poll();
1389

    
1390
}
1391

    
1392
static int vm_can_run(void)
1393
{
1394
    if (powerdown_requested)
1395
        return 0;
1396
    if (reset_requested)
1397
        return 0;
1398
    if (shutdown_requested)
1399
        return 0;
1400
    if (debug_requested)
1401
        return 0;
1402
    return 1;
1403
}
1404

    
1405
qemu_irq qemu_system_powerdown;
1406

    
1407
static void main_loop(void)
1408
{
1409
    int r;
1410

    
1411
    qemu_main_loop_start();
1412

    
1413
    for (;;) {
1414
        do {
1415
            bool nonblocking = false;
1416
#ifdef CONFIG_PROFILER
1417
            int64_t ti;
1418
#endif
1419
#ifndef CONFIG_IOTHREAD
1420
            nonblocking = cpu_exec_all();
1421
#endif
1422
#ifdef CONFIG_PROFILER
1423
            ti = profile_getclock();
1424
#endif
1425
            main_loop_wait(nonblocking);
1426
#ifdef CONFIG_PROFILER
1427
            dev_time += profile_getclock() - ti;
1428
#endif
1429
        } while (vm_can_run());
1430

    
1431
        if ((r = qemu_debug_requested())) {
1432
            vm_stop(r);
1433
        }
1434
        if (qemu_shutdown_requested()) {
1435
            monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1436
            if (no_shutdown) {
1437
                vm_stop(0);
1438
                no_shutdown = 0;
1439
            } else
1440
                break;
1441
        }
1442
        if (qemu_reset_requested()) {
1443
            pause_all_vcpus();
1444
            qemu_system_reset();
1445
            resume_all_vcpus();
1446
        }
1447
        if (qemu_powerdown_requested()) {
1448
            monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1449
            qemu_irq_raise(qemu_system_powerdown);
1450
        }
1451
        if ((r = qemu_vmstop_requested())) {
1452
            vm_stop(r);
1453
        }
1454
    }
1455
    bdrv_close_all();
1456
    pause_all_vcpus();
1457
}
1458

    
1459
static void version(void)
1460
{
1461
    printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1462
}
1463

    
1464
static void help(int exitcode)
1465
{
1466
    const char *options_help =
1467
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1468
        opt_help
1469
#define DEFHEADING(text) stringify(text) "\n"
1470
#include "qemu-options.def"
1471
#undef DEF
1472
#undef DEFHEADING
1473
#undef GEN_DOCS
1474
        ;
1475
    version();
1476
    printf("usage: %s [options] [disk_image]\n"
1477
           "\n"
1478
           "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1479
           "\n"
1480
           "%s\n"
1481
           "During emulation, the following keys are useful:\n"
1482
           "ctrl-alt-f      toggle full screen\n"
1483
           "ctrl-alt-n      switch to virtual console 'n'\n"
1484
           "ctrl-alt        toggle mouse and keyboard grab\n"
1485
           "\n"
1486
           "When using -nographic, press 'ctrl-a h' to get some help.\n",
1487
           "qemu",
1488
           options_help);
1489
    exit(exitcode);
1490
}
1491

    
1492
#define HAS_ARG 0x0001
1493

    
1494
typedef struct QEMUOption {
1495
    const char *name;
1496
    int flags;
1497
    int index;
1498
    uint32_t arch_mask;
1499
} QEMUOption;
1500

    
1501
static const QEMUOption qemu_options[] = {
1502
    { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1503
#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
1504
    { option, opt_arg, opt_enum, arch_mask },
1505
#define DEFHEADING(text)
1506
#include "qemu-options.def"
1507
#undef DEF
1508
#undef DEFHEADING
1509
#undef GEN_DOCS
1510
    { NULL },
1511
};
1512
static void select_vgahw (const char *p)
1513
{
1514
    const char *opts;
1515

    
1516
    default_vga = 0;
1517
    vga_interface_type = VGA_NONE;
1518
    if (strstart(p, "std", &opts)) {
1519
        vga_interface_type = VGA_STD;
1520
    } else if (strstart(p, "cirrus", &opts)) {
1521
        vga_interface_type = VGA_CIRRUS;
1522
    } else if (strstart(p, "vmware", &opts)) {
1523
        vga_interface_type = VGA_VMWARE;
1524
    } else if (strstart(p, "xenfb", &opts)) {
1525
        vga_interface_type = VGA_XENFB;
1526
    } else if (strstart(p, "qxl", &opts)) {
1527
        vga_interface_type = VGA_QXL;
1528
    } else if (!strstart(p, "none", &opts)) {
1529
    invalid_vga:
1530
        fprintf(stderr, "Unknown vga type: %s\n", p);
1531
        exit(1);
1532
    }
1533
    while (*opts) {
1534
        const char *nextopt;
1535

    
1536
        if (strstart(opts, ",retrace=", &nextopt)) {
1537
            opts = nextopt;
1538
            if (strstart(opts, "dumb", &nextopt))
1539
                vga_retrace_method = VGA_RETRACE_DUMB;
1540
            else if (strstart(opts, "precise", &nextopt))
1541
                vga_retrace_method = VGA_RETRACE_PRECISE;
1542
            else goto invalid_vga;
1543
        } else goto invalid_vga;
1544
        opts = nextopt;
1545
    }
1546
}
1547

    
1548
static int balloon_parse(const char *arg)
1549
{
1550
    QemuOpts *opts;
1551

    
1552
    if (strcmp(arg, "none") == 0) {
1553
        return 0;
1554
    }
1555

    
1556
    if (!strncmp(arg, "virtio", 6)) {
1557
        if (arg[6] == ',') {
1558
            /* have params -> parse them */
1559
            opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
1560
            if (!opts)
1561
                return  -1;
1562
        } else {
1563
            /* create empty opts */
1564
            opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
1565
        }
1566
        qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1567
        return 0;
1568
    }
1569

    
1570
    return -1;
1571
}
1572

    
1573
char *qemu_find_file(int type, const char *name)
1574
{
1575
    int len;
1576
    const char *subdir;
1577
    char *buf;
1578

    
1579
    /* If name contains path separators then try it as a straight path.  */
1580
    if ((strchr(name, '/') || strchr(name, '\\'))
1581
        && access(name, R_OK) == 0) {
1582
        return qemu_strdup(name);
1583
    }
1584
    switch (type) {
1585
    case QEMU_FILE_TYPE_BIOS:
1586
        subdir = "";
1587
        break;
1588
    case QEMU_FILE_TYPE_KEYMAP:
1589
        subdir = "keymaps/";
1590
        break;
1591
    default:
1592
        abort();
1593
    }
1594
    len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1595
    buf = qemu_mallocz(len);
1596
    snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
1597
    if (access(buf, R_OK)) {
1598
        qemu_free(buf);
1599
        return NULL;
1600
    }
1601
    return buf;
1602
}
1603

    
1604
static int device_help_func(QemuOpts *opts, void *opaque)
1605
{
1606
    return qdev_device_help(opts);
1607
}
1608

    
1609
static int device_init_func(QemuOpts *opts, void *opaque)
1610
{
1611
    DeviceState *dev;
1612

    
1613
    dev = qdev_device_add(opts);
1614
    if (!dev)
1615
        return -1;
1616
    return 0;
1617
}
1618

    
1619
static int chardev_init_func(QemuOpts *opts, void *opaque)
1620
{
1621
    CharDriverState *chr;
1622

    
1623
    chr = qemu_chr_open_opts(opts, NULL);
1624
    if (!chr)
1625
        return -1;
1626
    return 0;
1627
}
1628

    
1629
#ifdef CONFIG_VIRTFS
1630
static int fsdev_init_func(QemuOpts *opts, void *opaque)
1631
{
1632
    int ret;
1633
    ret = qemu_fsdev_add(opts);
1634

    
1635
    return ret;
1636
}
1637
#endif
1638

    
1639
static int mon_init_func(QemuOpts *opts, void *opaque)
1640
{
1641
    CharDriverState *chr;
1642
    const char *chardev;
1643
    const char *mode;
1644
    int flags;
1645

    
1646
    mode = qemu_opt_get(opts, "mode");
1647
    if (mode == NULL) {
1648
        mode = "readline";
1649
    }
1650
    if (strcmp(mode, "readline") == 0) {
1651
        flags = MONITOR_USE_READLINE;
1652
    } else if (strcmp(mode, "control") == 0) {
1653
        flags = MONITOR_USE_CONTROL;
1654
    } else {
1655
        fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1656
        exit(1);
1657
    }
1658

    
1659
    if (qemu_opt_get_bool(opts, "pretty", 0))
1660
        flags |= MONITOR_USE_PRETTY;
1661

    
1662
    if (qemu_opt_get_bool(opts, "default", 0))
1663
        flags |= MONITOR_IS_DEFAULT;
1664

    
1665
    chardev = qemu_opt_get(opts, "chardev");
1666
    chr = qemu_chr_find(chardev);
1667
    if (chr == NULL) {
1668
        fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1669
        exit(1);
1670
    }
1671

    
1672
    monitor_init(chr, flags);
1673
    return 0;
1674
}
1675

    
1676
static void monitor_parse(const char *optarg, const char *mode)
1677
{
1678
    static int monitor_device_index = 0;
1679
    QemuOpts *opts;
1680
    const char *p;
1681
    char label[32];
1682
    int def = 0;
1683

    
1684
    if (strstart(optarg, "chardev:", &p)) {
1685
        snprintf(label, sizeof(label), "%s", p);
1686
    } else {
1687
        snprintf(label, sizeof(label), "compat_monitor%d",
1688
                 monitor_device_index);
1689
        if (monitor_device_index == 0) {
1690
            def = 1;
1691
        }
1692
        opts = qemu_chr_parse_compat(label, optarg);
1693
        if (!opts) {
1694
            fprintf(stderr, "parse error: %s\n", optarg);
1695
            exit(1);
1696
        }
1697
    }
1698

    
1699
    opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
1700
    if (!opts) {
1701
        fprintf(stderr, "duplicate chardev: %s\n", label);
1702
        exit(1);
1703
    }
1704
    qemu_opt_set(opts, "mode", mode);
1705
    qemu_opt_set(opts, "chardev", label);
1706
    if (def)
1707
        qemu_opt_set(opts, "default", "on");
1708
    monitor_device_index++;
1709
}
1710

    
1711
struct device_config {
1712
    enum {
1713
        DEV_USB,       /* -usbdevice     */
1714
        DEV_BT,        /* -bt            */
1715
        DEV_SERIAL,    /* -serial        */
1716
        DEV_PARALLEL,  /* -parallel      */
1717
        DEV_VIRTCON,   /* -virtioconsole */
1718
        DEV_DEBUGCON,  /* -debugcon */
1719
    } type;
1720
    const char *cmdline;
1721
    QTAILQ_ENTRY(device_config) next;
1722
};
1723
QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
1724

    
1725
static void add_device_config(int type, const char *cmdline)
1726
{
1727
    struct device_config *conf;
1728

    
1729
    conf = qemu_mallocz(sizeof(*conf));
1730
    conf->type = type;
1731
    conf->cmdline = cmdline;
1732
    QTAILQ_INSERT_TAIL(&device_configs, conf, next);
1733
}
1734

    
1735
static int foreach_device_config(int type, int (*func)(const char *cmdline))
1736
{
1737
    struct device_config *conf;
1738
    int rc;
1739

    
1740
    QTAILQ_FOREACH(conf, &device_configs, next) {
1741
        if (conf->type != type)
1742
            continue;
1743
        rc = func(conf->cmdline);
1744
        if (0 != rc)
1745
            return rc;
1746
    }
1747
    return 0;
1748
}
1749

    
1750
static int serial_parse(const char *devname)
1751
{
1752
    static int index = 0;
1753
    char label[32];
1754

    
1755
    if (strcmp(devname, "none") == 0)
1756
        return 0;
1757
    if (index == MAX_SERIAL_PORTS) {
1758
        fprintf(stderr, "qemu: too many serial ports\n");
1759
        exit(1);
1760
    }
1761
    snprintf(label, sizeof(label), "serial%d", index);
1762
    serial_hds[index] = qemu_chr_open(label, devname, NULL);
1763
    if (!serial_hds[index]) {
1764
        fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1765
                devname, strerror(errno));
1766
        return -1;
1767
    }
1768
    index++;
1769
    return 0;
1770
}
1771

    
1772
static int parallel_parse(const char *devname)
1773
{
1774
    static int index = 0;
1775
    char label[32];
1776

    
1777
    if (strcmp(devname, "none") == 0)
1778
        return 0;
1779
    if (index == MAX_PARALLEL_PORTS) {
1780
        fprintf(stderr, "qemu: too many parallel ports\n");
1781
        exit(1);
1782
    }
1783
    snprintf(label, sizeof(label), "parallel%d", index);
1784
    parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1785
    if (!parallel_hds[index]) {
1786
        fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1787
                devname, strerror(errno));
1788
        return -1;
1789
    }
1790
    index++;
1791
    return 0;
1792
}
1793

    
1794
static int virtcon_parse(const char *devname)
1795
{
1796
    QemuOptsList *device = qemu_find_opts("device");
1797
    static int index = 0;
1798
    char label[32];
1799
    QemuOpts *bus_opts, *dev_opts;
1800

    
1801
    if (strcmp(devname, "none") == 0)
1802
        return 0;
1803
    if (index == MAX_VIRTIO_CONSOLES) {
1804
        fprintf(stderr, "qemu: too many virtio consoles\n");
1805
        exit(1);
1806
    }
1807

    
1808
    bus_opts = qemu_opts_create(device, NULL, 0);
1809
    qemu_opt_set(bus_opts, "driver", "virtio-serial");
1810

    
1811
    dev_opts = qemu_opts_create(device, NULL, 0);
1812
    qemu_opt_set(dev_opts, "driver", "virtconsole");
1813

    
1814
    snprintf(label, sizeof(label), "virtcon%d", index);
1815
    virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1816
    if (!virtcon_hds[index]) {
1817
        fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1818
                devname, strerror(errno));
1819
        return -1;
1820
    }
1821
    qemu_opt_set(dev_opts, "chardev", label);
1822

    
1823
    index++;
1824
    return 0;
1825
}
1826

    
1827
static int debugcon_parse(const char *devname)
1828
{   
1829
    QemuOpts *opts;
1830

    
1831
    if (!qemu_chr_open("debugcon", devname, NULL)) {
1832
        exit(1);
1833
    }
1834
    opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1835
    if (!opts) {
1836
        fprintf(stderr, "qemu: already have a debugcon device\n");
1837
        exit(1);
1838
    }
1839
    qemu_opt_set(opts, "driver", "isa-debugcon");
1840
    qemu_opt_set(opts, "chardev", "debugcon");
1841
    return 0;
1842
}
1843

    
1844
void qemu_add_exit_notifier(Notifier *notify)
1845
{
1846
    notifier_list_add(&exit_notifiers, notify);
1847
}
1848

    
1849
void qemu_remove_exit_notifier(Notifier *notify)
1850
{
1851
    notifier_list_remove(&exit_notifiers, notify);
1852
}
1853

    
1854
static void qemu_run_exit_notifiers(void)
1855
{
1856
    notifier_list_notify(&exit_notifiers);
1857
}
1858

    
1859
void qemu_add_machine_init_done_notifier(Notifier *notify)
1860
{
1861
    notifier_list_add(&machine_init_done_notifiers, notify);
1862
}
1863

    
1864
static void qemu_run_machine_init_done_notifiers(void)
1865
{
1866
    notifier_list_notify(&machine_init_done_notifiers);
1867
}
1868

    
1869
static const QEMUOption *lookup_opt(int argc, char **argv,
1870
                                    const char **poptarg, int *poptind)
1871
{
1872
    const QEMUOption *popt;
1873
    int optind = *poptind;
1874
    char *r = argv[optind];
1875
    const char *optarg;
1876

    
1877
    loc_set_cmdline(argv, optind, 1);
1878
    optind++;
1879
    /* Treat --foo the same as -foo.  */
1880
    if (r[1] == '-')
1881
        r++;
1882
    popt = qemu_options;
1883
    for(;;) {
1884
        if (!popt->name) {
1885
            error_report("invalid option");
1886
            exit(1);
1887
        }
1888
        if (!strcmp(popt->name, r + 1))
1889
            break;
1890
        popt++;
1891
    }
1892
    if (popt->flags & HAS_ARG) {
1893
        if (optind >= argc) {
1894
            error_report("requires an argument");
1895
            exit(1);
1896
        }
1897
        optarg = argv[optind++];
1898
        loc_set_cmdline(argv, optind - 2, 2);
1899
    } else {
1900
        optarg = NULL;
1901
    }
1902

    
1903
    *poptarg = optarg;
1904
    *poptind = optind;
1905

    
1906
    return popt;
1907
}
1908

    
1909
int main(int argc, char **argv, char **envp)
1910
{
1911
    const char *gdbstub_dev = NULL;
1912
    int i;
1913
    int snapshot, linux_boot;
1914
    const char *icount_option = NULL;
1915
    const char *initrd_filename;
1916
    const char *kernel_filename, *kernel_cmdline;
1917
    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
1918
    DisplayState *ds;
1919
    DisplayChangeListener *dcl;
1920
    int cyls, heads, secs, translation;
1921
    QemuOpts *hda_opts = NULL, *opts;
1922
    QemuOptsList *olist;
1923
    int optind;
1924
    const char *optarg;
1925
    const char *loadvm = NULL;
1926
    QEMUMachine *machine;
1927
    const char *cpu_model;
1928
    int tb_size;
1929
    const char *pid_file = NULL;
1930
    const char *incoming = NULL;
1931
    int show_vnc_port = 0;
1932
    int defconfig = 1;
1933

    
1934
#ifdef CONFIG_SIMPLE_TRACE
1935
    const char *trace_file = NULL;
1936
#endif
1937
    atexit(qemu_run_exit_notifiers);
1938
    error_set_progname(argv[0]);
1939

    
1940
    init_clocks();
1941

    
1942
    qemu_cache_utils_init(envp);
1943

    
1944
    QLIST_INIT (&vm_change_state_head);
1945
    os_setup_early_signal_handling();
1946

    
1947
    module_call_init(MODULE_INIT_MACHINE);
1948
    machine = find_default_machine();
1949
    cpu_model = NULL;
1950
    initrd_filename = NULL;
1951
    ram_size = 0;
1952
    snapshot = 0;
1953
    kernel_filename = NULL;
1954
    kernel_cmdline = "";
1955
    cyls = heads = secs = 0;
1956
    translation = BIOS_ATA_TRANSLATION_AUTO;
1957

    
1958
    for (i = 0; i < MAX_NODES; i++) {
1959
        node_mem[i] = 0;
1960
        node_cpumask[i] = 0;
1961
    }
1962

    
1963
    nb_numa_nodes = 0;
1964
    nb_nics = 0;
1965

    
1966
    tb_size = 0;
1967
    autostart= 1;
1968

    
1969
    /* first pass of option parsing */
1970
    optind = 1;
1971
    while (optind < argc) {
1972
        if (argv[optind][0] != '-') {
1973
            /* disk image */
1974
            optind++;
1975
            continue;
1976
        } else {
1977
            const QEMUOption *popt;
1978

    
1979
            popt = lookup_opt(argc, argv, &optarg, &optind);
1980
            switch (popt->index) {
1981
            case QEMU_OPTION_nodefconfig:
1982
                defconfig=0;
1983
                break;
1984
            }
1985
        }
1986
    }
1987

    
1988
    if (defconfig) {
1989
        int ret;
1990

    
1991
        ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
1992
        if (ret < 0 && ret != -ENOENT) {
1993
            exit(1);
1994
        }
1995

    
1996
        ret = qemu_read_config_file(arch_config_name);
1997
        if (ret < 0 && ret != -ENOENT) {
1998
            exit(1);
1999
        }
2000
    }
2001
    cpudef_init();
2002

    
2003
    /* second pass of option parsing */
2004
    optind = 1;
2005
    for(;;) {
2006
        if (optind >= argc)
2007
            break;
2008
        if (argv[optind][0] != '-') {
2009
            hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2010
        } else {
2011
            const QEMUOption *popt;
2012

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

    
2177
                    if (!strchr(optarg, '=')) {
2178
                        legacy = 1;
2179
                        pstrcpy(buf, sizeof(buf), optarg);
2180
                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2181
                        fprintf(stderr,
2182
                                "qemu: unknown boot parameter '%s' in '%s'\n",
2183
                                buf, optarg);
2184
                        exit(1);
2185
                    }
2186

    
2187
                    if (legacy ||
2188
                        get_param_value(buf, sizeof(buf), "order", optarg)) {
2189
                        validate_bootdevices(buf);
2190
                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
2191
                    }
2192
                    if (!legacy) {
2193
                        if (get_param_value(buf, sizeof(buf),
2194
                                            "once", optarg)) {
2195
                            validate_bootdevices(buf);
2196
                            standard_boot_devices = qemu_strdup(boot_devices);
2197
                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
2198
                            qemu_register_reset(restore_boot_devices,
2199
                                                standard_boot_devices);
2200
                        }
2201
                        if (get_param_value(buf, sizeof(buf),
2202
                                            "menu", optarg)) {
2203
                            if (!strcmp(buf, "on")) {
2204
                                boot_menu = 1;
2205
                            } else if (!strcmp(buf, "off")) {
2206
                                boot_menu = 0;
2207
                            } else {
2208
                                fprintf(stderr,
2209
                                        "qemu: invalid option value '%s'\n",
2210
                                        buf);
2211
                                exit(1);
2212
                            }
2213
                        }
2214
                    }
2215
                }
2216
                break;
2217
            case QEMU_OPTION_fda:
2218
            case QEMU_OPTION_fdb:
2219
                drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2220
                          optarg, FD_OPTS);
2221
                break;
2222
            case QEMU_OPTION_no_fd_bootchk:
2223
                fd_bootchk = 0;
2224
                break;
2225
            case QEMU_OPTION_netdev:
2226
                if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
2227
                    exit(1);
2228
                }
2229
                break;
2230
            case QEMU_OPTION_net:
2231
                if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
2232
                    exit(1);
2233
                }
2234
                break;
2235
#ifdef CONFIG_SLIRP
2236
            case QEMU_OPTION_tftp:
2237
                legacy_tftp_prefix = optarg;
2238
                break;
2239
            case QEMU_OPTION_bootp:
2240
                legacy_bootp_filename = optarg;
2241
                break;
2242
            case QEMU_OPTION_redir:
2243
                if (net_slirp_redir(optarg) < 0)
2244
                    exit(1);
2245
                break;
2246
#endif
2247
            case QEMU_OPTION_bt:
2248
                add_device_config(DEV_BT, optarg);
2249
                break;
2250
            case QEMU_OPTION_audio_help:
2251
                if (!(audio_available())) {
2252
                    printf("Option %s not supported for this target\n", popt->name);
2253
                    exit(1);
2254
                }
2255
                AUD_help ();
2256
                exit (0);
2257
                break;
2258
            case QEMU_OPTION_soundhw:
2259
                if (!(audio_available())) {
2260
                    printf("Option %s not supported for this target\n", popt->name);
2261
                    exit(1);
2262
                }
2263
                select_soundhw (optarg);
2264
                break;
2265
            case QEMU_OPTION_h:
2266
                help(0);
2267
                break;
2268
            case QEMU_OPTION_version:
2269
                version();
2270
                exit(0);
2271
                break;
2272
            case QEMU_OPTION_m: {
2273
                int64_t value;
2274

    
2275
                value = strtosz(optarg, NULL);
2276
                if (value < 0) {
2277
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
2278
                    exit(1);
2279
                }
2280

    
2281
                /* On 32-bit hosts, QEMU is limited by virtual address space */
2282
                if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
2283
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2284
                    exit(1);
2285
                }
2286
                if (value != (uint64_t)(ram_addr_t)value) {
2287
                    fprintf(stderr, "qemu: ram size too large\n");
2288
                    exit(1);
2289
                }
2290
                ram_size = value;
2291
                break;
2292
            }
2293
            case QEMU_OPTION_mempath:
2294
                mem_path = optarg;
2295
                break;
2296
#ifdef MAP_POPULATE
2297
            case QEMU_OPTION_mem_prealloc:
2298
                mem_prealloc = 1;
2299
                break;
2300
#endif
2301
            case QEMU_OPTION_d:
2302
                set_cpu_log(optarg);
2303
                break;
2304
            case QEMU_OPTION_s:
2305
                gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
2306
                break;
2307
            case QEMU_OPTION_gdb:
2308
                gdbstub_dev = optarg;
2309
                break;
2310
            case QEMU_OPTION_L:
2311
                data_dir = optarg;
2312
                break;
2313
            case QEMU_OPTION_bios:
2314
                bios_name = optarg;
2315
                break;
2316
            case QEMU_OPTION_singlestep:
2317
                singlestep = 1;
2318
                break;
2319
            case QEMU_OPTION_S:
2320
                autostart = 0;
2321
                break;
2322
            case QEMU_OPTION_k:
2323
                keyboard_layout = optarg;
2324
                break;
2325
            case QEMU_OPTION_localtime:
2326
                rtc_utc = 0;
2327
                break;
2328
            case QEMU_OPTION_vga:
2329
                select_vgahw (optarg);
2330
                break;
2331
            case QEMU_OPTION_g:
2332
                {
2333
                    const char *p;
2334
                    int w, h, depth;
2335
                    p = optarg;
2336
                    w = strtol(p, (char **)&p, 10);
2337
                    if (w <= 0) {
2338
                    graphic_error:
2339
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
2340
                        exit(1);
2341
                    }
2342
                    if (*p != 'x')
2343
                        goto graphic_error;
2344
                    p++;
2345
                    h = strtol(p, (char **)&p, 10);
2346
                    if (h <= 0)
2347
                        goto graphic_error;
2348
                    if (*p == 'x') {
2349
                        p++;
2350
                        depth = strtol(p, (char **)&p, 10);
2351
                        if (depth != 8 && depth != 15 && depth != 16 &&
2352
                            depth != 24 && depth != 32)
2353
                            goto graphic_error;
2354
                    } else if (*p == '\0') {
2355
                        depth = graphic_depth;
2356
                    } else {
2357
                        goto graphic_error;
2358
                    }
2359

    
2360
                    graphic_width = w;
2361
                    graphic_height = h;
2362
                    graphic_depth = depth;
2363
                }
2364
                break;
2365
            case QEMU_OPTION_echr:
2366
                {
2367
                    char *r;
2368
                    term_escape_char = strtol(optarg, &r, 0);
2369
                    if (r == optarg)
2370
                        printf("Bad argument to echr\n");
2371
                    break;
2372
                }
2373
            case QEMU_OPTION_monitor:
2374
                monitor_parse(optarg, "readline");
2375
                default_monitor = 0;
2376
                break;
2377
            case QEMU_OPTION_qmp:
2378
                monitor_parse(optarg, "control");
2379
                default_monitor = 0;
2380
                break;
2381
            case QEMU_OPTION_mon:
2382
                opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
2383
                if (!opts) {
2384
                    exit(1);
2385
                }
2386
                default_monitor = 0;
2387
                break;
2388
            case QEMU_OPTION_chardev:
2389
                opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
2390
                if (!opts) {
2391
                    exit(1);
2392
                }
2393
                break;
2394
            case QEMU_OPTION_fsdev:
2395
                olist = qemu_find_opts("fsdev");
2396
                if (!olist) {
2397
                    fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2398
                    exit(1);
2399
                }
2400
                opts = qemu_opts_parse(olist, optarg, 1);
2401
                if (!opts) {
2402
                    fprintf(stderr, "parse error: %s\n", optarg);
2403
                    exit(1);
2404
                }
2405
                break;
2406
            case QEMU_OPTION_virtfs: {
2407
                char *arg_fsdev = NULL;
2408
                char *arg_9p = NULL;
2409
                int len = 0;
2410

    
2411
                olist = qemu_find_opts("virtfs");
2412
                if (!olist) {
2413
                    fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2414
                    exit(1);
2415
                }
2416
                opts = qemu_opts_parse(olist, optarg, 1);
2417
                if (!opts) {
2418
                    fprintf(stderr, "parse error: %s\n", optarg);
2419
                    exit(1);
2420
                }
2421

    
2422
                if (qemu_opt_get(opts, "fstype") == NULL ||
2423
                        qemu_opt_get(opts, "mount_tag") == NULL ||
2424
                        qemu_opt_get(opts, "path") == NULL ||
2425
                        qemu_opt_get(opts, "security_model") == NULL) {
2426
                    fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
2427
                            "security_model=[mapped|passthrough|none],"
2428
                            "mnt_tag=tag.\n");
2429
                    exit(1);
2430
                }
2431

    
2432
                len = strlen(",id=,path=,security_model=");
2433
                len += strlen(qemu_opt_get(opts, "fstype"));
2434
                len += strlen(qemu_opt_get(opts, "mount_tag"));
2435
                len += strlen(qemu_opt_get(opts, "path"));
2436
                len += strlen(qemu_opt_get(opts, "security_model"));
2437
                arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2438

    
2439
                snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
2440
                         "%s,id=%s,path=%s,security_model=%s",
2441
                         qemu_opt_get(opts, "fstype"),
2442
                         qemu_opt_get(opts, "mount_tag"),
2443
                         qemu_opt_get(opts, "path"),
2444
                         qemu_opt_get(opts, "security_model"));
2445

    
2446
                len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2447
                len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2448
                arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2449

    
2450
                snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
2451
                         "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2452
                         qemu_opt_get(opts, "mount_tag"),
2453
                         qemu_opt_get(opts, "mount_tag"));
2454

    
2455
                if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
2456
                    fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2457
                    exit(1);
2458
                }
2459

    
2460
                if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
2461
                    fprintf(stderr, "parse error [device]: %s\n", optarg);
2462
                    exit(1);
2463
                }
2464

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

    
2762
    /* If no data_dir is specified then try to find it relative to the
2763
       executable path.  */
2764
    if (!data_dir) {
2765
        data_dir = os_find_datadir(argv[0]);
2766
    }
2767
    /* If all else fails use the install patch specified when building.  */
2768
    if (!data_dir) {
2769
        data_dir = CONFIG_QEMU_DATADIR;
2770
    }
2771

    
2772
#ifdef CONFIG_SIMPLE_TRACE
2773
    /*
2774
     * Set the trace file name, if specified.
2775
     */
2776
    st_set_trace_file(trace_file);
2777
#endif
2778
    /*
2779
     * Default to max_cpus = smp_cpus, in case the user doesn't
2780
     * specify a max_cpus value.
2781
     */
2782
    if (!max_cpus)
2783
        max_cpus = smp_cpus;
2784

    
2785
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
2786
    if (smp_cpus > machine->max_cpus) {
2787
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2788
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
2789
                machine->max_cpus);
2790
        exit(1);
2791
    }
2792

    
2793
    qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2794
    qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
2795

    
2796
    if (machine->no_serial) {
2797
        default_serial = 0;
2798
    }
2799
    if (machine->no_parallel) {
2800
        default_parallel = 0;
2801
    }
2802
    if (!machine->use_virtcon) {
2803
        default_virtcon = 0;
2804
    }
2805
    if (machine->no_vga) {
2806
        default_vga = 0;
2807
    }
2808
    if (machine->no_floppy) {
2809
        default_floppy = 0;
2810
    }
2811
    if (machine->no_cdrom) {
2812
        default_cdrom = 0;
2813
    }
2814
    if (machine->no_sdcard) {
2815
        default_sdcard = 0;
2816
    }
2817

    
2818
    if (display_type == DT_NOGRAPHIC) {
2819
        if (default_parallel)
2820
            add_device_config(DEV_PARALLEL, "null");
2821
        if (default_serial && default_monitor) {
2822
            add_device_config(DEV_SERIAL, "mon:stdio");
2823
        } else if (default_virtcon && default_monitor) {
2824
            add_device_config(DEV_VIRTCON, "mon:stdio");
2825
        } else {
2826
            if (default_serial)
2827
                add_device_config(DEV_SERIAL, "stdio");
2828
            if (default_virtcon)
2829
                add_device_config(DEV_VIRTCON, "stdio");
2830
            if (default_monitor)
2831
                monitor_parse("stdio", "readline");
2832
        }
2833
    } else {
2834
        if (default_serial)
2835
            add_device_config(DEV_SERIAL, "vc:80Cx24C");
2836
        if (default_parallel)
2837
            add_device_config(DEV_PARALLEL, "vc:80Cx24C");
2838
        if (default_monitor)
2839
            monitor_parse("vc:80Cx24C", "readline");
2840
        if (default_virtcon)
2841
            add_device_config(DEV_VIRTCON, "vc:80Cx24C");
2842
    }
2843
    if (default_vga)
2844
        vga_interface_type = VGA_CIRRUS;
2845

    
2846
    socket_init();
2847

    
2848
    if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
2849
        exit(1);
2850
#ifdef CONFIG_VIRTFS
2851
    if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
2852
        exit(1);
2853
    }
2854
#endif
2855

    
2856
    os_daemonize();
2857

    
2858
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
2859
        os_pidfile_error();
2860
        exit(1);
2861
    }
2862

    
2863
    if (kvm_allowed) {
2864
        int ret = kvm_init();
2865
        if (ret < 0) {
2866
            if (!kvm_available()) {
2867
                printf("KVM not supported for this target\n");
2868
            } else {
2869
                fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2870
            }
2871
            exit(1);
2872
        }
2873
    }
2874

    
2875
    if (qemu_init_main_loop()) {
2876
        fprintf(stderr, "qemu_init_main_loop failed\n");
2877
        exit(1);
2878
    }
2879
    linux_boot = (kernel_filename != NULL);
2880

    
2881
    if (!linux_boot && *kernel_cmdline != '\0') {
2882
        fprintf(stderr, "-append only allowed with -kernel option\n");
2883
        exit(1);
2884
    }
2885

    
2886
    if (!linux_boot && initrd_filename != NULL) {
2887
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
2888
        exit(1);
2889
    }
2890

    
2891
    os_set_line_buffering();
2892

    
2893
    if (init_timer_alarm() < 0) {
2894
        fprintf(stderr, "could not initialize alarm timer\n");
2895
        exit(1);
2896
    }
2897
    configure_icount(icount_option);
2898

    
2899
    if (net_init_clients() < 0) {
2900
        exit(1);
2901
    }
2902

    
2903
    /* init the bluetooth world */
2904
    if (foreach_device_config(DEV_BT, bt_parse))
2905
        exit(1);
2906

    
2907
    /* init the memory */
2908
    if (ram_size == 0)
2909
        ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2910

    
2911
    /* init the dynamic translator */
2912
    cpu_exec_init_all(tb_size * 1024 * 1024);
2913

    
2914
    bdrv_init_with_whitelist();
2915

    
2916
    blk_mig_init();
2917

    
2918
    /* open the virtual block devices */
2919
    if (snapshot)
2920
        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2921
    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
2922
        exit(1);
2923

    
2924
    default_drive(default_cdrom, snapshot, machine->use_scsi,
2925
                  IF_DEFAULT, 2, CDROM_OPTS);
2926
    default_drive(default_floppy, snapshot, machine->use_scsi,
2927
                  IF_FLOPPY, 0, FD_OPTS);
2928
    default_drive(default_sdcard, snapshot, machine->use_scsi,
2929
                  IF_SD, 0, SD_OPTS);
2930

    
2931
    register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
2932
                         ram_load, NULL);
2933

    
2934
    if (nb_numa_nodes > 0) {
2935
        int i;
2936

    
2937
        if (nb_numa_nodes > smp_cpus) {
2938
            nb_numa_nodes = smp_cpus;
2939
        }
2940

    
2941
        /* If no memory size if given for any node, assume the default case
2942
         * and distribute the available memory equally across all nodes
2943
         */
2944
        for (i = 0; i < nb_numa_nodes; i++) {
2945
            if (node_mem[i] != 0)
2946
                break;
2947
        }
2948
        if (i == nb_numa_nodes) {
2949
            uint64_t usedmem = 0;
2950

    
2951
            /* On Linux, the each node's border has to be 8MB aligned,
2952
             * the final node gets the rest.
2953
             */
2954
            for (i = 0; i < nb_numa_nodes - 1; i++) {
2955
                node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2956
                usedmem += node_mem[i];
2957
            }
2958
            node_mem[i] = ram_size - usedmem;
2959
        }
2960

    
2961
        for (i = 0; i < nb_numa_nodes; i++) {
2962
            if (node_cpumask[i] != 0)
2963
                break;
2964
        }
2965
        /* assigning the VCPUs round-robin is easier to implement, guest OSes
2966
         * must cope with this anyway, because there are BIOSes out there in
2967
         * real machines which also use this scheme.
2968
         */
2969
        if (i == nb_numa_nodes) {
2970
            for (i = 0; i < smp_cpus; i++) {
2971
                node_cpumask[i % nb_numa_nodes] |= 1 << i;
2972
            }
2973
        }
2974
    }
2975

    
2976
    if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
2977
        exit(1);
2978
    }
2979

    
2980
    if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2981
        exit(1);
2982
    if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2983
        exit(1);
2984
    if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2985
        exit(1);
2986
    if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2987
        exit(1);
2988

    
2989
    module_call_init(MODULE_INIT_DEVICE);
2990

    
2991
    if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
2992
        exit(0);
2993

    
2994
    if (watchdog) {
2995
        i = select_watchdog(watchdog);
2996
        if (i > 0)
2997
            exit (i == 1 ? 1 : 0);
2998
    }
2999

    
3000
    if (machine->compat_props) {
3001
        qdev_prop_register_global_list(machine->compat_props);
3002
    }
3003
    qemu_add_globals();
3004

    
3005
    machine->init(ram_size, boot_devices,
3006
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3007

    
3008
    cpu_synchronize_all_post_init();
3009

    
3010
    /* must be after terminal init, SDL library changes signal handlers */
3011
    os_setup_signal_handling();
3012

    
3013
    set_numa_modes();
3014

    
3015
    current_machine = machine;
3016

    
3017
    /* init USB devices */
3018
    if (usb_enabled) {
3019
        if (foreach_device_config(DEV_USB, usb_parse) < 0)
3020
            exit(1);
3021
    }
3022

    
3023
    /* init generic devices */
3024
    if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
3025
        exit(1);
3026

    
3027
    net_check_clients();
3028

    
3029
    /* just use the first displaystate for the moment */
3030
    ds = get_displaystate();
3031

    
3032
    if (using_spice)
3033
        display_remote++;
3034
    if (display_type == DT_DEFAULT && !display_remote) {
3035
#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3036
        display_type = DT_SDL;
3037
#else
3038
        vnc_display = "localhost:0,to=99";
3039
        show_vnc_port = 1;
3040
#endif
3041
    }
3042
        
3043

    
3044
    /* init local displays */
3045
    switch (display_type) {
3046
    case DT_NOGRAPHIC:
3047
        break;
3048
#if defined(CONFIG_CURSES)
3049
    case DT_CURSES:
3050
        curses_display_init(ds, full_screen);
3051
        break;
3052
#endif
3053
#if defined(CONFIG_SDL)
3054
    case DT_SDL:
3055
        sdl_display_init(ds, full_screen, no_frame);
3056
        break;
3057
#elif defined(CONFIG_COCOA)
3058
    case DT_SDL:
3059
        cocoa_display_init(ds, full_screen);
3060
        break;
3061
#endif
3062
    default:
3063
        break;
3064
    }
3065

    
3066
    /* init remote displays */
3067
    if (vnc_display) {
3068
        vnc_display_init(ds);
3069
        if (vnc_display_open(ds, vnc_display) < 0)
3070
            exit(1);
3071

    
3072
        if (show_vnc_port) {
3073
            printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
3074
        }
3075
    }
3076
#ifdef CONFIG_SPICE
3077
    if (using_spice && !qxl_enabled) {
3078
        qemu_spice_display_init(ds);
3079
    }
3080
#endif
3081

    
3082
    /* display setup */
3083
    dpy_resize(ds);
3084
    dcl = ds->listeners;
3085
    while (dcl != NULL) {
3086
        if (dcl->dpy_refresh != NULL) {
3087
            ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3088
            qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
3089
            break;
3090
        }
3091
        dcl = dcl->next;
3092
    }
3093
    if (ds->gui_timer == NULL) {
3094
        nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3095
        qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3096
    }
3097
    text_consoles_set_display(ds);
3098

    
3099
    if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3100
        fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3101
                gdbstub_dev);
3102
        exit(1);
3103
    }
3104

    
3105
    qdev_machine_creation_done();
3106

    
3107
    if (rom_load_all() != 0) {
3108
        fprintf(stderr, "rom loading failed\n");
3109
        exit(1);
3110
    }
3111

    
3112
    /* TODO: once all bus devices are qdevified, this should be done
3113
     * when bus is created by qdev.c */
3114
    qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
3115
    qemu_run_machine_init_done_notifiers();
3116

    
3117
    qemu_system_reset();
3118
    if (loadvm) {
3119
        if (load_vmstate(loadvm) < 0) {
3120
            autostart = 0;
3121
        }
3122
    }
3123

    
3124
    if (incoming) {
3125
        int ret = qemu_start_incoming_migration(incoming);
3126
        if (ret < 0) {
3127
            fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3128
                    incoming, ret);
3129
            exit(ret);
3130
        }
3131
    } else if (autostart) {
3132
        vm_start();
3133
    }
3134

    
3135
    os_setup_post();
3136

    
3137
    main_loop();
3138
    quit_timers();
3139
    net_cleanup();
3140

    
3141
    return 0;
3142
}