]> git.proxmox.com Git - qemu.git/blame - vl.c
xen: Support new libxc calls from xen unstable.
[qemu.git] / vl.c
CommitLineData
0824d6fc 1/*
80cabfad 2 * QEMU System Emulator
5fafdf24 3 *
68d0f70e 4 * Copyright (c) 2003-2008 Fabrice Bellard
5fafdf24 5 *
1df912cf
FB
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.
0824d6fc 23 */
0824d6fc 24#include <unistd.h>
0824d6fc
FB
25#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
0824d6fc 28#include <errno.h>
67b915a5 29#include <sys/time.h>
c88676f8 30#include <zlib.h>
67b915a5 31
71e72a19 32/* Needed early for CONFIG_BSD etc. */
d40cdb10
BS
33#include "config-host.h"
34
67b915a5 35#ifndef _WIN32
5cea8590 36#include <libgen.h>
67b915a5 37#include <sys/times.h>
f1510b2c 38#include <sys/wait.h>
67b915a5 39#include <termios.h>
67b915a5 40#include <sys/mman.h>
f1510b2c 41#include <sys/ioctl.h>
24646c7e 42#include <sys/resource.h>
f1510b2c 43#include <sys/socket.h>
c94c8d64 44#include <netinet/in.h>
24646c7e 45#include <net/if.h>
24646c7e 46#include <arpa/inet.h>
9d728e8c 47#include <dirent.h>
7c9d8e07 48#include <netdb.h>
cb4b976b 49#include <sys/select.h>
ab6540d5 50
71e72a19 51#ifdef CONFIG_BSD
7d3505c5 52#include <sys/stat.h>
a167ba50 53#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
7d3505c5 54#include <libutil.h>
92c0e657 55#include <sys/sysctl.h>
24646c7e
BS
56#else
57#include <util.h>
128ab2ff 58#endif
bbe813a2 59#else
223f0d72 60#ifdef __linux__
7d3505c5
FB
61#include <pty.h>
62#include <malloc.h>
bd494f4c 63
e57a8c0e 64#include <linux/ppdev.h>
5867c88a 65#include <linux/parport.h>
223f0d72
BS
66#endif
67#ifdef __sun__
d5d10bc3
TS
68#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
d5d10bc3 71#include <netinet/arp.h>
d5d10bc3
TS
72#include <netinet/in_systm.h>
73#include <netinet/ip.h>
74#include <netinet/ip_icmp.h> // must come after ip.h
75#include <netinet/udp.h>
76#include <netinet/tcp.h>
77#include <net/if.h>
78#include <syslog.h>
79#include <stropts.h>
67b915a5 80#endif
7d3505c5 81#endif
ec530c81 82#endif
67b915a5 83
9892fbfb
BS
84#if defined(__OpenBSD__)
85#include <util.h>
86#endif
87
8a16d273
TS
88#if defined(CONFIG_VDE)
89#include <libvdeplug.h>
90#endif
91
67b915a5 92#ifdef _WIN32
49dc768d 93#include <windows.h>
67b915a5
FB
94#endif
95
73332e5c 96#ifdef CONFIG_SDL
59a36a2f 97#if defined(__APPLE__) || defined(main)
6693665a 98#include <SDL.h>
880fec5d 99int qemu_main(int argc, char **argv, char **envp);
100int main(int argc, char **argv)
101{
59a36a2f 102 return qemu_main(argc, argv, NULL);
880fec5d 103}
104#undef main
105#define main qemu_main
96bcd4f8 106#endif
73332e5c 107#endif /* CONFIG_SDL */
0824d6fc 108
5b0753e0
FB
109#ifdef CONFIG_COCOA
110#undef main
111#define main qemu_main
112#endif /* CONFIG_COCOA */
113
511d2b14
BS
114#include "hw/hw.h"
115#include "hw/boards.h"
116#include "hw/usb.h"
117#include "hw/pcmcia.h"
118#include "hw/pc.h"
511d2b14
BS
119#include "hw/isa.h"
120#include "hw/baum.h"
121#include "hw/bt.h"
9dd986cc 122#include "hw/watchdog.h"
b6f6e3d3 123#include "hw/smbios.h"
e37630ca 124#include "hw/xen.h"
bd3c948d 125#include "hw/qdev.h"
45a50b16 126#include "hw/loader.h"
5ef4efa4 127#include "bt-host.h"
511d2b14 128#include "net.h"
68ac40d2 129#include "net/slirp.h"
511d2b14
BS
130#include "monitor.h"
131#include "console.h"
132#include "sysemu.h"
133#include "gdbstub.h"
134#include "qemu-timer.h"
135#include "qemu-char.h"
136#include "cache-utils.h"
137#include "block.h"
666daa68 138#include "blockdev.h"
c163b5ca 139#include "block-migration.h"
a718acec 140#include "dma.h"
511d2b14
BS
141#include "audio/audio.h"
142#include "migration.h"
143#include "kvm.h"
d3f24367 144#include "qemu-option.h"
7282a033 145#include "qemu-config.h"
e78c48ec 146#include "qemu-objects.h"
59a5264b 147#include "qemu-options.h"
758e8e38 148#ifdef CONFIG_VIRTFS
74db920c
GS
149#include "fsdev/qemu-fsdev.h"
150#endif
511d2b14 151
0824d6fc 152#include "disas.h"
fc01f7e7 153
511d2b14
BS
154#include "qemu_socket.h"
155
d918f23e 156#include "slirp/libslirp.h"
511d2b14 157
94b0b5ff 158#include "trace.h"
0b5538c3 159#include "simpletrace.h"
72cf2d4f 160#include "qemu-queue.h"
296af7c9 161#include "cpus.h"
ad96090a 162#include "arch_init.h"
72cf2d4f 163
29b0040b
GH
164#include "ui/qemu-spice.h"
165
9dc63a1e
BS
166//#define DEBUG_NET
167//#define DEBUG_SLIRP
330d0414 168
1bfe856e 169#define DEFAULT_RAM_SIZE 128
313aa567 170
98b19252
AS
171#define MAX_VIRTIO_CONSOLES 1
172
5cea8590 173static const char *data_dir;
1192dad8 174const char *bios_name = NULL;
cb5a7aa8 175enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
993fbfdb 176DisplayType display_type = DT_DEFAULT;
6b62dc2d 177int display_remote = 0;
3d11d0eb 178const char* keyboard_layout = NULL;
c227f099 179ram_addr_t ram_size;
c902760f
MT
180const char *mem_path = NULL;
181#ifdef MAP_POPULATE
182int mem_prealloc = 0; /* force preallocation of physical target memory */
183#endif
c4b1fcc0 184int nb_nics;
7c9d8e07 185NICInfo nd_table[MAX_NICS];
8a7ddc38 186int vm_running;
d399f677 187int autostart;
8e84865e 188int incoming_expected; /* Started with -incoming and waiting for incoming */
f6503059
AZ
189static int rtc_utc = 1;
190static int rtc_date_offset = -1; /* -1 means no change */
6875204c 191QEMUClock *rtc_clock;
64465297 192int vga_interface_type = VGA_NONE;
dbed7e40 193static int full_screen = 0;
634a21f6 194#ifdef CONFIG_SDL
dbed7e40 195static int no_frame = 0;
634a21f6 196#endif
667accab 197int no_quit = 0;
8d11df9e 198CharDriverState *serial_hds[MAX_SERIAL_PORTS];
6508fe59 199CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
9ede2fde 200CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
a09db21f 201int win2k_install_hack = 0;
73822ec8 202int rtc_td_hack = 0;
bb36d470 203int usb_enabled = 0;
1b530a6d 204int singlestep = 0;
6a00d601 205int smp_cpus = 1;
6be68d7e 206int max_cpus = 0;
dc6b1c09
AP
207int smp_cores = 1;
208int smp_threads = 1;
821601ea 209#ifdef CONFIG_VNC
73fc9742 210const char *vnc_display;
821601ea 211#endif
6515b203 212int acpi_enabled = 1;
16b29ae1 213int no_hpet = 0;
52ca8d6a 214int fd_bootchk = 1;
d1beab82 215int no_reboot = 0;
b2f76161 216int no_shutdown = 0;
9467cd46 217int cursor_hide = 1;
a171fe39 218int graphic_rotate = 0;
6b35e7bf 219uint8_t irq0override = 1;
09aaa160 220const char *watchdog;
2e55e842 221QEMUOptionRom option_rom[MAX_OPTION_ROMS];
9ae02555 222int nb_option_roms;
8e71621f 223int semihosting_enabled = 0;
2b8f2d41 224int old_param = 0;
c35734b2 225const char *qemu_name;
3780e197 226int alt_grab = 0;
0ca9f8a4 227int ctrl_grab = 0;
66508601
BS
228unsigned int nb_prom_envs = 0;
229const char *prom_envs[MAX_PROM_ENVS];
95387491 230int boot_menu;
0824d6fc 231
1ca4d09a
GN
232typedef struct FWBootEntry FWBootEntry;
233
234struct FWBootEntry {
235 QTAILQ_ENTRY(FWBootEntry) link;
236 int32_t bootindex;
237 DeviceState *dev;
238 char *suffix;
239};
240
241QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
242
268a362c
AL
243int nb_numa_nodes;
244uint64_t node_mem[MAX_NODES];
245uint64_t node_cpumask[MAX_NODES];
246
9043b62d 247static QEMUTimer *nographic_timer;
ee5605e5 248
8fcb1b90
BS
249uint8_t qemu_uuid[16];
250
76e30d0f
JK
251static QEMUBootSetHandler *boot_set_handler;
252static void *boot_set_opaque;
253
fd42deeb
GH
254static NotifierList exit_notifiers =
255 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
256
4cab946a
GN
257static NotifierList machine_init_done_notifiers =
258 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
259
303d4e86 260static int tcg_allowed = 1;
d745bef8
BS
261int kvm_allowed = 0;
262uint32_t xen_domid;
263enum xen_mode xen_mode = XEN_EMULATE;
264
998bbd74 265static int default_serial = 1;
6a5e8b0e 266static int default_parallel = 1;
986c5f78 267static int default_virtcon = 1;
abdeed06 268static int default_monitor = 1;
64465297 269static int default_vga = 1;
ac33f8fa
GH
270static int default_floppy = 1;
271static int default_cdrom = 1;
272static int default_sdcard = 1;
998bbd74
GH
273
274static struct {
275 const char *driver;
276 int *flag;
277} default_list[] = {
6a5e8b0e
GH
278 { .driver = "isa-serial", .flag = &default_serial },
279 { .driver = "isa-parallel", .flag = &default_parallel },
d8bcbabf
GH
280 { .driver = "isa-fdc", .flag = &default_floppy },
281 { .driver = "ide-drive", .flag = &default_cdrom },
392ecf54
AS
282 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
283 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
284 { .driver = "virtio-serial", .flag = &default_virtcon },
64465297 285 { .driver = "VGA", .flag = &default_vga },
69fd02ee
GH
286 { .driver = "cirrus-vga", .flag = &default_vga },
287 { .driver = "vmware-svga", .flag = &default_vga },
998bbd74
GH
288};
289
290static int default_driver_check(QemuOpts *opts, void *opaque)
291{
292 const char *driver = qemu_opt_get(opts, "driver");
293 int i;
294
295 if (!driver)
296 return 0;
297 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
298 if (strcmp(default_list[i].driver, driver) != 0)
299 continue;
300 *(default_list[i].flag) = 0;
301 }
302 return 0;
303}
304
8f0056b7
PB
305/***********************************************************/
306/* real time host monotonic timer */
09b26c5e 307
f6503059
AZ
308/***********************************************************/
309/* host time/date access */
310void qemu_get_timedate(struct tm *tm, int offset)
311{
312 time_t ti;
313 struct tm *ret;
314
315 time(&ti);
316 ti += offset;
317 if (rtc_date_offset == -1) {
318 if (rtc_utc)
319 ret = gmtime(&ti);
320 else
321 ret = localtime(&ti);
322 } else {
323 ti -= rtc_date_offset;
324 ret = gmtime(&ti);
325 }
326
327 memcpy(tm, ret, sizeof(struct tm));
328}
329
330int qemu_timedate_diff(struct tm *tm)
331{
332 time_t seconds;
333
334 if (rtc_date_offset == -1)
335 if (rtc_utc)
336 seconds = mktimegm(tm);
337 else
338 seconds = mktime(tm);
339 else
340 seconds = mktimegm(tm) + rtc_date_offset;
341
342 return seconds - time(NULL);
343}
344
80cd3478
LC
345void rtc_change_mon_event(struct tm *tm)
346{
347 QObject *data;
348
349 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
350 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
351 qobject_decref(data);
352}
353
1ed2fc1f
JK
354static void configure_rtc_date_offset(const char *startdate, int legacy)
355{
356 time_t rtc_start_date;
357 struct tm tm;
358
359 if (!strcmp(startdate, "now") && legacy) {
360 rtc_date_offset = -1;
361 } else {
362 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
363 &tm.tm_year,
364 &tm.tm_mon,
365 &tm.tm_mday,
366 &tm.tm_hour,
367 &tm.tm_min,
368 &tm.tm_sec) == 6) {
369 /* OK */
370 } else if (sscanf(startdate, "%d-%d-%d",
371 &tm.tm_year,
372 &tm.tm_mon,
373 &tm.tm_mday) == 3) {
374 tm.tm_hour = 0;
375 tm.tm_min = 0;
376 tm.tm_sec = 0;
377 } else {
378 goto date_fail;
379 }
380 tm.tm_year -= 1900;
381 tm.tm_mon--;
382 rtc_start_date = mktimegm(&tm);
383 if (rtc_start_date == -1) {
384 date_fail:
385 fprintf(stderr, "Invalid date format. Valid formats are:\n"
386 "'2006-06-17T16:01:21' or '2006-06-17'\n");
387 exit(1);
388 }
389 rtc_date_offset = time(NULL) - rtc_start_date;
390 }
391}
392
393static void configure_rtc(QemuOpts *opts)
394{
395 const char *value;
396
397 value = qemu_opt_get(opts, "base");
398 if (value) {
399 if (!strcmp(value, "utc")) {
400 rtc_utc = 1;
401 } else if (!strcmp(value, "localtime")) {
402 rtc_utc = 0;
403 } else {
404 configure_rtc_date_offset(value, 0);
405 }
406 }
6875204c
JK
407 value = qemu_opt_get(opts, "clock");
408 if (value) {
409 if (!strcmp(value, "host")) {
410 rtc_clock = host_clock;
411 } else if (!strcmp(value, "vm")) {
412 rtc_clock = vm_clock;
413 } else {
414 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
415 exit(1);
416 }
417 }
1ed2fc1f
JK
418 value = qemu_opt_get(opts, "driftfix");
419 if (value) {
7e4c0336 420 if (!strcmp(value, "slew")) {
1ed2fc1f 421 rtc_td_hack = 1;
7e4c0336 422 } else if (!strcmp(value, "none")) {
1ed2fc1f
JK
423 rtc_td_hack = 0;
424 } else {
425 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
426 exit(1);
427 }
428 }
1ed2fc1f
JK
429}
430
1ae26a18
AZ
431/***********************************************************/
432/* Bluetooth support */
433static int nb_hcis;
434static int cur_hci;
435static struct HCIInfo *hci_table[MAX_NICS];
dc72ac14 436
1ae26a18
AZ
437static struct bt_vlan_s {
438 struct bt_scatternet_s net;
439 int id;
440 struct bt_vlan_s *next;
441} *first_bt_vlan;
442
443/* find or alloc a new bluetooth "VLAN" */
674bb261 444static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1ae26a18
AZ
445{
446 struct bt_vlan_s **pvlan, *vlan;
447 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
448 if (vlan->id == id)
449 return &vlan->net;
450 }
451 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
452 vlan->id = id;
453 pvlan = &first_bt_vlan;
454 while (*pvlan != NULL)
455 pvlan = &(*pvlan)->next;
456 *pvlan = vlan;
457 return &vlan->net;
458}
459
460static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
461{
462}
463
464static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
465{
466 return -ENOTSUP;
467}
468
469static struct HCIInfo null_hci = {
470 .cmd_send = null_hci_send,
471 .sco_send = null_hci_send,
472 .acl_send = null_hci_send,
473 .bdaddr_set = null_hci_addr_set,
474};
475
476struct HCIInfo *qemu_next_hci(void)
477{
478 if (cur_hci == nb_hcis)
479 return &null_hci;
480
481 return hci_table[cur_hci++];
482}
483
dc72ac14
AZ
484static struct HCIInfo *hci_init(const char *str)
485{
486 char *endp;
487 struct bt_scatternet_s *vlan = 0;
488
489 if (!strcmp(str, "null"))
490 /* null */
491 return &null_hci;
492 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
493 /* host[:hciN] */
494 return bt_host_hci(str[4] ? str + 5 : "hci0");
495 else if (!strncmp(str, "hci", 3)) {
496 /* hci[,vlan=n] */
497 if (str[3]) {
498 if (!strncmp(str + 3, ",vlan=", 6)) {
499 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
500 if (*endp)
501 vlan = 0;
502 }
503 } else
504 vlan = qemu_find_bt_vlan(0);
505 if (vlan)
506 return bt_new_hci(vlan);
507 }
508
509 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
510
511 return 0;
512}
513
514static int bt_hci_parse(const char *str)
515{
516 struct HCIInfo *hci;
c227f099 517 bdaddr_t bdaddr;
dc72ac14
AZ
518
519 if (nb_hcis >= MAX_NICS) {
520 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
521 return -1;
522 }
523
524 hci = hci_init(str);
525 if (!hci)
526 return -1;
527
528 bdaddr.b[0] = 0x52;
529 bdaddr.b[1] = 0x54;
530 bdaddr.b[2] = 0x00;
531 bdaddr.b[3] = 0x12;
532 bdaddr.b[4] = 0x34;
533 bdaddr.b[5] = 0x56 + nb_hcis;
534 hci->bdaddr_set(hci, bdaddr.b);
535
536 hci_table[nb_hcis++] = hci;
537
538 return 0;
539}
540
541static void bt_vhci_add(int vlan_id)
542{
543 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
544
545 if (!vlan->slave)
546 fprintf(stderr, "qemu: warning: adding a VHCI to "
547 "an empty scatternet %i\n", vlan_id);
548
549 bt_vhci_init(bt_new_hci(vlan));
550}
551
552static struct bt_device_s *bt_device_add(const char *opt)
553{
554 struct bt_scatternet_s *vlan;
555 int vlan_id = 0;
556 char *endp = strstr(opt, ",vlan=");
557 int len = (endp ? endp - opt : strlen(opt)) + 1;
558 char devname[10];
559
560 pstrcpy(devname, MIN(sizeof(devname), len), opt);
561
562 if (endp) {
563 vlan_id = strtol(endp + 6, &endp, 0);
564 if (*endp) {
565 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
566 return 0;
567 }
568 }
569
570 vlan = qemu_find_bt_vlan(vlan_id);
571
572 if (!vlan->slave)
573 fprintf(stderr, "qemu: warning: adding a slave device to "
574 "an empty scatternet %i\n", vlan_id);
575
576 if (!strcmp(devname, "keyboard"))
577 return bt_keyboard_init(vlan);
578
579 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
580 return 0;
581}
582
583static int bt_parse(const char *opt)
584{
585 const char *endp, *p;
586 int vlan;
587
588 if (strstart(opt, "hci", &endp)) {
589 if (!*endp || *endp == ',') {
590 if (*endp)
591 if (!strstart(endp, ",vlan=", 0))
592 opt = endp + 1;
593
594 return bt_hci_parse(opt);
595 }
596 } else if (strstart(opt, "vhci", &endp)) {
597 if (!*endp || *endp == ',') {
598 if (*endp) {
599 if (strstart(endp, ",vlan=", &p)) {
600 vlan = strtol(p, (char **) &endp, 0);
601 if (*endp) {
602 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
603 return 1;
604 }
605 } else {
606 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
607 return 1;
608 }
609 } else
610 vlan = 0;
611
612 bt_vhci_add(vlan);
613 return 0;
614 }
615 } else if (strstart(opt, "device:", &endp))
616 return !bt_device_add(endp);
617
618 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
619 return 1;
620}
621
1ae26a18
AZ
622/***********************************************************/
623/* QEMU Block devices */
624
2292ddae
MA
625#define HD_OPTS "media=disk"
626#define CDROM_OPTS "media=cdrom"
627#define FD_OPTS ""
628#define PFLASH_OPTS ""
629#define MTD_OPTS ""
630#define SD_OPTS ""
e4bcb14c 631
9dfd7c7a
GH
632static int drive_init_func(QemuOpts *opts, void *opaque)
633{
a803cb8e 634 int *use_scsi = opaque;
9dfd7c7a 635
319ae529 636 return drive_init(opts, *use_scsi) == NULL;
9dfd7c7a
GH
637}
638
639static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
640{
641 if (NULL == qemu_opt_get(opts, "snapshot")) {
642 qemu_opt_set(opts, "snapshot", "on");
643 }
644 return 0;
645}
646
4e5d9b57
MA
647static void default_drive(int enable, int snapshot, int use_scsi,
648 BlockInterfaceType type, int index,
649 const char *optstr)
650{
651 QemuOpts *opts;
652
653 if (type == IF_DEFAULT) {
654 type = use_scsi ? IF_SCSI : IF_IDE;
655 }
656
657 if (!enable || drive_get_by_index(type, index)) {
658 return;
659 }
660
661 opts = drive_add(type, index, NULL, optstr);
662 if (snapshot) {
663 drive_enable_snapshot(opts, NULL);
664 }
319ae529 665 if (!drive_init(opts, use_scsi)) {
4e5d9b57
MA
666 exit(1);
667 }
668}
669
76e30d0f
JK
670void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
671{
672 boot_set_handler = func;
673 boot_set_opaque = opaque;
674}
675
676int qemu_boot_set(const char *boot_devices)
677{
678 if (!boot_set_handler) {
679 return -EINVAL;
680 }
681 return boot_set_handler(boot_set_opaque, boot_devices);
682}
683
4e9e9d6e 684static void validate_bootdevices(char *devices)
ef3adf68
JK
685{
686 /* We just do some generic consistency checks */
687 const char *p;
688 int bitmap = 0;
689
690 for (p = devices; *p != '\0'; p++) {
691 /* Allowed boot devices are:
692 * a-b: floppy disk drives
693 * c-f: IDE disk drives
694 * g-m: machine implementation dependant drives
695 * n-p: network devices
696 * It's up to each machine implementation to check if the given boot
697 * devices match the actual hardware implementation and firmware
698 * features.
699 */
700 if (*p < 'a' || *p > 'p') {
701 fprintf(stderr, "Invalid boot device '%c'\n", *p);
702 exit(1);
703 }
704 if (bitmap & (1 << (*p - 'a'))) {
705 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
706 exit(1);
707 }
708 bitmap |= 1 << (*p - 'a');
709 }
ef3adf68
JK
710}
711
e0f084bf
JK
712static void restore_boot_devices(void *opaque)
713{
714 char *standard_boot_devices = opaque;
37905d6a
AW
715 static int first = 1;
716
717 /* Restore boot order and remove ourselves after the first boot */
718 if (first) {
719 first = 0;
720 return;
721 }
e0f084bf
JK
722
723 qemu_boot_set(standard_boot_devices);
724
725 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
726 qemu_free(standard_boot_devices);
727}
728
1ca4d09a
GN
729void add_boot_device_path(int32_t bootindex, DeviceState *dev,
730 const char *suffix)
731{
732 FWBootEntry *node, *i;
733
734 if (bootindex < 0) {
735 return;
736 }
737
738 assert(dev != NULL || suffix != NULL);
739
740 node = qemu_mallocz(sizeof(FWBootEntry));
741 node->bootindex = bootindex;
4fef930a 742 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
1ca4d09a
GN
743 node->dev = dev;
744
745 QTAILQ_FOREACH(i, &fw_boot_order, link) {
746 if (i->bootindex == bootindex) {
747 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
748 exit(1);
749 } else if (i->bootindex < bootindex) {
750 continue;
751 }
752 QTAILQ_INSERT_BEFORE(i, node, link);
753 return;
754 }
755 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
756}
757
962630f2
GN
758/*
759 * This function returns null terminated string that consist of new line
71785aba 760 * separated device paths.
962630f2
GN
761 *
762 * memory pointed by "size" is assigned total length of the array in bytes
763 *
764 */
765char *get_boot_devices_list(uint32_t *size)
766{
767 FWBootEntry *i;
768 uint32_t total = 0;
769 char *list = NULL;
770
771 QTAILQ_FOREACH(i, &fw_boot_order, link) {
772 char *devpath = NULL, *bootpath;
773 int len;
774
775 if (i->dev) {
776 devpath = qdev_get_fw_dev_path(i->dev);
777 assert(devpath);
778 }
779
780 if (i->suffix && devpath) {
4fd37a98
BS
781 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
782
783 bootpath = qemu_malloc(bootpathlen);
784 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
962630f2
GN
785 qemu_free(devpath);
786 } else if (devpath) {
787 bootpath = devpath;
788 } else {
4fef930a 789 bootpath = qemu_strdup(i->suffix);
962630f2
GN
790 assert(bootpath);
791 }
792
793 if (total) {
794 list[total-1] = '\n';
795 }
796 len = strlen(bootpath) + 1;
797 list = qemu_realloc(list, total + len);
798 memcpy(&list[total], bootpath, len);
799 total += len;
800 qemu_free(bootpath);
801 }
802
803 *size = total;
804
805 return list;
806}
807
268a362c
AL
808static void numa_add(const char *optarg)
809{
810 char option[128];
811 char *endptr;
812 unsigned long long value, endvalue;
813 int nodenr;
814
815 optarg = get_opt_name(option, 128, optarg, ',') + 1;
816 if (!strcmp(option, "node")) {
817 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
818 nodenr = nb_numa_nodes;
819 } else {
820 nodenr = strtoull(option, NULL, 10);
821 }
822
823 if (get_param_value(option, 128, "mem", optarg) == 0) {
824 node_mem[nodenr] = 0;
825 } else {
70b4f4bb 826 int64_t sval;
9f9b17a4
JS
827 sval = strtosz(option, NULL);
828 if (sval < 0) {
829 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
830 exit(1);
268a362c 831 }
9f9b17a4 832 node_mem[nodenr] = sval;
268a362c
AL
833 }
834 if (get_param_value(option, 128, "cpus", optarg) == 0) {
835 node_cpumask[nodenr] = 0;
836 } else {
837 value = strtoull(option, &endptr, 10);
838 if (value >= 64) {
839 value = 63;
840 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
841 } else {
842 if (*endptr == '-') {
843 endvalue = strtoull(endptr+1, &endptr, 10);
844 if (endvalue >= 63) {
845 endvalue = 62;
846 fprintf(stderr,
847 "only 63 CPUs in NUMA mode supported.\n");
848 }
0dfbd514 849 value = (2ULL << endvalue) - (1ULL << value);
268a362c 850 } else {
0dfbd514 851 value = 1ULL << value;
268a362c
AL
852 }
853 }
854 node_cpumask[nodenr] = value;
855 }
856 nb_numa_nodes++;
857 }
858 return;
859}
860
dc6b1c09
AP
861static void smp_parse(const char *optarg)
862{
863 int smp, sockets = 0, threads = 0, cores = 0;
864 char *endptr;
865 char option[128];
866
867 smp = strtoul(optarg, &endptr, 10);
868 if (endptr != optarg) {
869 if (*endptr == ',') {
870 endptr++;
871 }
872 }
873 if (get_param_value(option, 128, "sockets", endptr) != 0)
874 sockets = strtoull(option, NULL, 10);
875 if (get_param_value(option, 128, "cores", endptr) != 0)
876 cores = strtoull(option, NULL, 10);
877 if (get_param_value(option, 128, "threads", endptr) != 0)
878 threads = strtoull(option, NULL, 10);
879 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
880 max_cpus = strtoull(option, NULL, 10);
881
882 /* compute missing values, prefer sockets over cores over threads */
883 if (smp == 0 || sockets == 0) {
884 sockets = sockets > 0 ? sockets : 1;
885 cores = cores > 0 ? cores : 1;
886 threads = threads > 0 ? threads : 1;
887 if (smp == 0) {
888 smp = cores * threads * sockets;
dc6b1c09
AP
889 }
890 } else {
891 if (cores == 0) {
892 threads = threads > 0 ? threads : 1;
893 cores = smp / (sockets * threads);
894 } else {
dca98169 895 threads = smp / (cores * sockets);
dc6b1c09
AP
896 }
897 }
898 smp_cpus = smp;
899 smp_cores = cores > 0 ? cores : 1;
900 smp_threads = threads > 0 ? threads : 1;
901 if (max_cpus == 0)
902 max_cpus = smp_cpus;
903}
904
a594cfbf
FB
905/***********************************************************/
906/* USB devices */
907
fb08000c 908static int usb_device_add(const char *devname)
a594cfbf
FB
909{
910 const char *p;
a5d2f727 911 USBDevice *dev = NULL;
a594cfbf 912
a5d2f727 913 if (!usb_enabled)
a594cfbf
FB
914 return -1;
915
0958b4cc
GH
916 /* drivers with .usbdevice_name entry in USBDeviceInfo */
917 dev = usbdevice_create(devname);
918 if (dev)
919 goto done;
920
a5d2f727 921 /* the other ones */
a594cfbf
FB
922 if (strstart(devname, "host:", &p)) {
923 dev = usb_host_device_open(p);
dc72ac14
AZ
924 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
925 dev = usb_bt_init(devname[2] ? hci_init(p) :
926 bt_new_hci(qemu_find_bt_vlan(0)));
a594cfbf
FB
927 } else {
928 return -1;
929 }
0d92ed30
PB
930 if (!dev)
931 return -1;
932
a5d2f727 933done:
a594cfbf
FB
934 return 0;
935}
936
1f3870ab
AL
937static int usb_device_del(const char *devname)
938{
939 int bus_num, addr;
940 const char *p;
941
5d0c5750
AL
942 if (strstart(devname, "host:", &p))
943 return usb_host_device_close(p);
944
a5d2f727 945 if (!usb_enabled)
1f3870ab
AL
946 return -1;
947
948 p = strchr(devname, '.');
949 if (!p)
950 return -1;
951 bus_num = strtoul(devname, NULL, 0);
952 addr = strtoul(p + 1, NULL, 0);
953
a5d2f727 954 return usb_device_delete_addr(bus_num, addr);
1f3870ab
AL
955}
956
bd3c948d
GH
957static int usb_parse(const char *cmdline)
958{
59d1c1c2 959 int r;
fb08000c 960 r = usb_device_add(cmdline);
59d1c1c2
ST
961 if (r < 0) {
962 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
963 }
964 return r;
bd3c948d
GH
965}
966
d54908a5 967void do_usb_add(Monitor *mon, const QDict *qdict)
a594cfbf 968{
59d1c1c2 969 const char *devname = qdict_get_str(qdict, "devname");
fb08000c 970 if (usb_device_add(devname) < 0) {
1ecda02b 971 error_report("could not add USB device '%s'", devname);
59d1c1c2 972 }
a594cfbf
FB
973}
974
d54908a5 975void do_usb_del(Monitor *mon, const QDict *qdict)
a594cfbf 976{
59d1c1c2
ST
977 const char *devname = qdict_get_str(qdict, "devname");
978 if (usb_device_del(devname) < 0) {
1ecda02b 979 error_report("could not delete USB device '%s'", devname);
59d1c1c2 980 }
a594cfbf
FB
981}
982
201a51fc
AZ
983/***********************************************************/
984/* PCMCIA/Cardbus */
985
986static struct pcmcia_socket_entry_s {
bc24a225 987 PCMCIASocket *socket;
201a51fc
AZ
988 struct pcmcia_socket_entry_s *next;
989} *pcmcia_sockets = 0;
990
bc24a225 991void pcmcia_socket_register(PCMCIASocket *socket)
201a51fc
AZ
992{
993 struct pcmcia_socket_entry_s *entry;
994
995 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
996 entry->socket = socket;
997 entry->next = pcmcia_sockets;
998 pcmcia_sockets = entry;
999}
1000
bc24a225 1001void pcmcia_socket_unregister(PCMCIASocket *socket)
201a51fc
AZ
1002{
1003 struct pcmcia_socket_entry_s *entry, **ptr;
1004
1005 ptr = &pcmcia_sockets;
1006 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1007 if (entry->socket == socket) {
1008 *ptr = entry->next;
1009 qemu_free(entry);
1010 }
1011}
1012
376253ec 1013void pcmcia_info(Monitor *mon)
201a51fc
AZ
1014{
1015 struct pcmcia_socket_entry_s *iter;
376253ec 1016
201a51fc 1017 if (!pcmcia_sockets)
376253ec 1018 monitor_printf(mon, "No PCMCIA sockets\n");
201a51fc
AZ
1019
1020 for (iter = pcmcia_sockets; iter; iter = iter->next)
376253ec
AL
1021 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1022 iter->socket->attached ? iter->socket->card_string :
1023 "Empty");
201a51fc
AZ
1024}
1025
cc1daa40
FB
1026/***********************************************************/
1027/* machine registration */
1028
bdaf78e0 1029static QEMUMachine *first_machine = NULL;
6f338c34 1030QEMUMachine *current_machine = NULL;
cc1daa40
FB
1031
1032int qemu_register_machine(QEMUMachine *m)
1033{
1034 QEMUMachine **pm;
1035 pm = &first_machine;
1036 while (*pm != NULL)
1037 pm = &(*pm)->next;
1038 m->next = NULL;
1039 *pm = m;
1040 return 0;
1041}
1042
9596ebb7 1043static QEMUMachine *find_machine(const char *name)
cc1daa40
FB
1044{
1045 QEMUMachine *m;
1046
1047 for(m = first_machine; m != NULL; m = m->next) {
1048 if (!strcmp(m->name, name))
1049 return m;
3f6599e6
MM
1050 if (m->alias && !strcmp(m->alias, name))
1051 return m;
cc1daa40
FB
1052 }
1053 return NULL;
1054}
1055
0c257437
AL
1056static QEMUMachine *find_default_machine(void)
1057{
1058 QEMUMachine *m;
1059
1060 for(m = first_machine; m != NULL; m = m->next) {
1061 if (m->is_default) {
1062 return m;
1063 }
1064 }
1065 return NULL;
1066}
1067
8a7ddc38
FB
1068/***********************************************************/
1069/* main execution loop */
1070
9596ebb7 1071static void gui_update(void *opaque)
8a7ddc38 1072{
7d957bd8 1073 uint64_t interval = GUI_REFRESH_INTERVAL;
740733bb 1074 DisplayState *ds = opaque;
7d957bd8
AL
1075 DisplayChangeListener *dcl = ds->listeners;
1076
62a2744c 1077 qemu_flush_coalesced_mmio_buffer();
7d957bd8
AL
1078 dpy_refresh(ds);
1079
1080 while (dcl != NULL) {
1081 if (dcl->gui_timer_interval &&
1082 dcl->gui_timer_interval < interval)
1083 interval = dcl->gui_timer_interval;
1084 dcl = dcl->next;
1085 }
7bd427d8 1086 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
8a7ddc38
FB
1087}
1088
9043b62d
BS
1089static void nographic_update(void *opaque)
1090{
1091 uint64_t interval = GUI_REFRESH_INTERVAL;
1092
62a2744c 1093 qemu_flush_coalesced_mmio_buffer();
7bd427d8 1094 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
9043b62d
BS
1095}
1096
0bd48850
FB
1097struct vm_change_state_entry {
1098 VMChangeStateHandler *cb;
1099 void *opaque;
72cf2d4f 1100 QLIST_ENTRY (vm_change_state_entry) entries;
0bd48850
FB
1101};
1102
72cf2d4f 1103static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
0bd48850
FB
1104
1105VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1106 void *opaque)
1107{
1108 VMChangeStateEntry *e;
1109
1110 e = qemu_mallocz(sizeof (*e));
0bd48850
FB
1111
1112 e->cb = cb;
1113 e->opaque = opaque;
72cf2d4f 1114 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
0bd48850
FB
1115 return e;
1116}
1117
1118void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1119{
72cf2d4f 1120 QLIST_REMOVE (e, entries);
0bd48850
FB
1121 qemu_free (e);
1122}
1123
296af7c9 1124void vm_state_notify(int running, int reason)
0bd48850
FB
1125{
1126 VMChangeStateEntry *e;
1127
94b0b5ff
SH
1128 trace_vm_state_notify(running, reason);
1129
0bd48850 1130 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
9781e040 1131 e->cb(e->opaque, running, reason);
0bd48850
FB
1132 }
1133}
1134
8a7ddc38
FB
1135void vm_start(void)
1136{
1137 if (!vm_running) {
1138 cpu_enable_ticks();
1139 vm_running = 1;
9781e040 1140 vm_state_notify(1, 0);
d6dc3d42 1141 resume_all_vcpus();
6ed2c484 1142 monitor_protocol_event(QEVENT_RESUME, NULL);
8a7ddc38
FB
1143 }
1144}
1145
bb0c6722
FB
1146/* reset/shutdown handler */
1147
1148typedef struct QEMUResetEntry {
72cf2d4f 1149 QTAILQ_ENTRY(QEMUResetEntry) entry;
bb0c6722
FB
1150 QEMUResetHandler *func;
1151 void *opaque;
bb0c6722
FB
1152} QEMUResetEntry;
1153
72cf2d4f
BS
1154static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1155 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bb0c6722 1156static int reset_requested;
f64622c4
GN
1157static int shutdown_requested, shutdown_signal = -1;
1158static pid_t shutdown_pid;
3475187d 1159static int powerdown_requested;
8cf71710
JK
1160static int debug_requested;
1161static int vmstop_requested;
bb0c6722 1162
cf7a2fe2
AJ
1163int qemu_shutdown_requested(void)
1164{
1165 int r = shutdown_requested;
1166 shutdown_requested = 0;
1167 return r;
1168}
1169
f64622c4
GN
1170void qemu_kill_report(void)
1171{
1172 if (shutdown_signal != -1) {
f1d3fb04
PM
1173 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1174 if (shutdown_pid == 0) {
1175 /* This happens for eg ^C at the terminal, so it's worth
1176 * avoiding printing an odd message in that case.
1177 */
1178 fputc('\n', stderr);
1179 } else {
1180 fprintf(stderr, " from pid %d\n", shutdown_pid);
1181 }
f64622c4
GN
1182 shutdown_signal = -1;
1183 }
1184}
1185
cf7a2fe2
AJ
1186int qemu_reset_requested(void)
1187{
1188 int r = reset_requested;
1189 reset_requested = 0;
1190 return r;
1191}
1192
1193int qemu_powerdown_requested(void)
1194{
1195 int r = powerdown_requested;
1196 powerdown_requested = 0;
1197 return r;
1198}
1199
e568902a
AL
1200static int qemu_debug_requested(void)
1201{
1202 int r = debug_requested;
1203 debug_requested = 0;
1204 return r;
1205}
1206
6e29f5da
AL
1207static int qemu_vmstop_requested(void)
1208{
1209 int r = vmstop_requested;
1210 vmstop_requested = 0;
1211 return r;
1212}
1213
a08d4367 1214void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bb0c6722 1215{
55ddfe8e 1216 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bb0c6722 1217
bb0c6722
FB
1218 re->func = func;
1219 re->opaque = opaque;
72cf2d4f 1220 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bb0c6722
FB
1221}
1222
dda9b29f 1223void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bb0c6722
FB
1224{
1225 QEMUResetEntry *re;
1226
72cf2d4f 1227 QTAILQ_FOREACH(re, &reset_handlers, entry) {
dda9b29f 1228 if (re->func == func && re->opaque == opaque) {
72cf2d4f 1229 QTAILQ_REMOVE(&reset_handlers, re, entry);
dda9b29f
JK
1230 qemu_free(re);
1231 return;
1232 }
1233 }
1234}
1235
1236void qemu_system_reset(void)
1237{
1238 QEMUResetEntry *re, *nre;
1239
1240 /* reset all devices */
72cf2d4f 1241 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bb0c6722
FB
1242 re->func(re->opaque);
1243 }
81d9b784 1244 monitor_protocol_event(QEVENT_RESET, NULL);
ea375f9a 1245 cpu_synchronize_all_post_reset();
bb0c6722
FB
1246}
1247
1248void qemu_system_reset_request(void)
1249{
d1beab82
FB
1250 if (no_reboot) {
1251 shutdown_requested = 1;
1252 } else {
1253 reset_requested = 1;
1254 }
b4a3d965 1255 cpu_stop_current();
d9f75a4e 1256 qemu_notify_event();
bb0c6722
FB
1257}
1258
f64622c4
GN
1259void qemu_system_killed(int signal, pid_t pid)
1260{
1261 shutdown_signal = signal;
1262 shutdown_pid = pid;
1263 qemu_system_shutdown_request();
1264}
1265
bb0c6722
FB
1266void qemu_system_shutdown_request(void)
1267{
1268 shutdown_requested = 1;
d9f75a4e 1269 qemu_notify_event();
bb0c6722
FB
1270}
1271
3475187d
FB
1272void qemu_system_powerdown_request(void)
1273{
1274 powerdown_requested = 1;
d9f75a4e
AL
1275 qemu_notify_event();
1276}
1277
8cf71710
JK
1278void qemu_system_debug_request(void)
1279{
1280 debug_requested = 1;
83f338f7 1281 qemu_notify_event();
8cf71710
JK
1282}
1283
1284void qemu_system_vmstop_request(int reason)
1285{
1286 vmstop_requested = reason;
1287 qemu_notify_event();
1288}
1289
d6f4ade2 1290void main_loop_wait(int nonblocking)
56f3a5d0 1291{
56f3a5d0
AL
1292 fd_set rfds, wfds, xfds;
1293 int ret, nfds;
1294 struct timeval tv;
d6f4ade2 1295 int timeout;
56f3a5d0 1296
d6f4ade2
PB
1297 if (nonblocking)
1298 timeout = 0;
1299 else {
1300 timeout = qemu_calculate_timeout();
1301 qemu_bh_update_timeout(&timeout);
1302 }
56f3a5d0 1303
0d93ca7c 1304 os_host_main_loop_wait(&timeout);
56f3a5d0 1305
02981419
PB
1306 tv.tv_sec = timeout / 1000;
1307 tv.tv_usec = (timeout % 1000) * 1000;
1308
fd1dff4b
FB
1309 /* poll any events */
1310 /* XXX: separate device handlers from system ones */
6abfbd79 1311 nfds = -1;
fd1dff4b
FB
1312 FD_ZERO(&rfds);
1313 FD_ZERO(&wfds);
e035649e 1314 FD_ZERO(&xfds);
02981419 1315 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
d918f23e
JK
1316 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1317
4870852c 1318 qemu_mutex_unlock_iothread();
e035649e 1319 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4870852c 1320 qemu_mutex_lock_iothread();
d918f23e 1321
02981419 1322 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
d918f23e 1323 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
b4608c04 1324
b6964827 1325 qemu_run_all_timers();
21d5d12b 1326
423f0742
PB
1327 /* Check bottom-halves last in case any of the earlier events triggered
1328 them. */
1329 qemu_bh_poll();
3b46e624 1330
5905b2e5
FB
1331}
1332
46481d39
JK
1333#ifndef CONFIG_IOTHREAD
1334static int vm_request_pending(void)
43b96858 1335{
46481d39
JK
1336 return powerdown_requested ||
1337 reset_requested ||
1338 shutdown_requested ||
1339 debug_requested ||
1340 vmstop_requested;
43b96858 1341}
46481d39 1342#endif
43b96858 1343
d9c32310
BS
1344qemu_irq qemu_system_powerdown;
1345
43b96858
AL
1346static void main_loop(void)
1347{
8e1b90ec
JK
1348 bool nonblocking = false;
1349#ifdef CONFIG_PROFILER
1350 int64_t ti;
1351#endif
6e29f5da 1352 int r;
e6e35b1e 1353
7277e027 1354 qemu_main_loop_start();
d6dc3d42 1355
6e29f5da 1356 for (;;) {
d6dc3d42 1357#ifndef CONFIG_IOTHREAD
46481d39
JK
1358 nonblocking = cpu_exec_all();
1359 if (vm_request_pending()) {
1360 nonblocking = true;
1361 }
d6dc3d42 1362#endif
89bfc105 1363#ifdef CONFIG_PROFILER
46481d39 1364 ti = profile_getclock();
89bfc105 1365#endif
46481d39 1366 main_loop_wait(nonblocking);
89bfc105 1367#ifdef CONFIG_PROFILER
46481d39 1368 dev_time += profile_getclock() - ti;
89bfc105 1369#endif
43b96858 1370
8cf71710
JK
1371 if (qemu_debug_requested()) {
1372 vm_stop(VMSTOP_DEBUG);
b1a15e7e 1373 }
43b96858 1374 if (qemu_shutdown_requested()) {
f64622c4 1375 qemu_kill_report();
242cd003 1376 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
43b96858 1377 if (no_shutdown) {
e07bbac5 1378 vm_stop(VMSTOP_SHUTDOWN);
43b96858
AL
1379 no_shutdown = 0;
1380 } else
1381 break;
1382 }
d6dc3d42
AL
1383 if (qemu_reset_requested()) {
1384 pause_all_vcpus();
a7ada151 1385 cpu_synchronize_all_states();
43b96858 1386 qemu_system_reset();
d6dc3d42
AL
1387 resume_all_vcpus();
1388 }
d9c32310 1389 if (qemu_powerdown_requested()) {
242cd003 1390 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
d9c32310
BS
1391 qemu_irq_raise(qemu_system_powerdown);
1392 }
b1a15e7e 1393 if ((r = qemu_vmstop_requested())) {
6e29f5da 1394 vm_stop(r);
b1a15e7e 1395 }
b4608c04 1396 }
2bc93fed 1397 bdrv_close_all();
d6dc3d42 1398 pause_all_vcpus();
b4608c04
FB
1399}
1400
9bd7e6d9
PB
1401static void version(void)
1402{
f75ca1ae 1403 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
9bd7e6d9
PB
1404}
1405
15f82208 1406static void help(int exitcode)
0824d6fc 1407{
e8105ebb 1408 const char *options_help =
ad96090a
BS
1409#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1410 opt_help
5824d651 1411#define DEFHEADING(text) stringify(text) "\n"
9f16732a 1412#include "qemu-options.def"
5824d651
BS
1413#undef DEF
1414#undef DEFHEADING
1415#undef GEN_DOCS
e8105ebb
PB
1416 ;
1417 version();
1418 printf("usage: %s [options] [disk_image]\n"
1419 "\n"
f75ca1ae 1420 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
3f020d70 1421 "\n"
e8105ebb 1422 "%s\n"
3f020d70 1423 "During emulation, the following keys are useful:\n"
1424 "ctrl-alt-f toggle full screen\n"
1425 "ctrl-alt-n switch to virtual console 'n'\n"
1426 "ctrl-alt toggle mouse and keyboard grab\n"
1427 "\n"
e8105ebb
PB
1428 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1429 "qemu",
1430 options_help);
15f82208 1431 exit(exitcode);
0824d6fc
FB
1432}
1433
cd6f1169
FB
1434#define HAS_ARG 0x0001
1435
cd6f1169
FB
1436typedef struct QEMUOption {
1437 const char *name;
1438 int flags;
1439 int index;
ad96090a 1440 uint32_t arch_mask;
cd6f1169
FB
1441} QEMUOption;
1442
dbed7e40 1443static const QEMUOption qemu_options[] = {
ad96090a
BS
1444 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1445#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1446 { option, opt_arg, opt_enum, arch_mask },
5824d651 1447#define DEFHEADING(text)
9f16732a 1448#include "qemu-options.def"
5824d651
BS
1449#undef DEF
1450#undef DEFHEADING
1451#undef GEN_DOCS
cd6f1169 1452 { NULL },
fc01f7e7 1453};
3893c124 1454static void select_vgahw (const char *p)
1455{
1456 const char *opts;
1457
64465297 1458 default_vga = 0;
86176759 1459 vga_interface_type = VGA_NONE;
3893c124 1460 if (strstart(p, "std", &opts)) {
86176759 1461 vga_interface_type = VGA_STD;
3893c124 1462 } else if (strstart(p, "cirrus", &opts)) {
86176759 1463 vga_interface_type = VGA_CIRRUS;
3893c124 1464 } else if (strstart(p, "vmware", &opts)) {
86176759 1465 vga_interface_type = VGA_VMWARE;
94909d9f 1466 } else if (strstart(p, "xenfb", &opts)) {
86176759 1467 vga_interface_type = VGA_XENFB;
a19cbfb3
GH
1468 } else if (strstart(p, "qxl", &opts)) {
1469 vga_interface_type = VGA_QXL;
28b85ed8 1470 } else if (!strstart(p, "none", &opts)) {
3893c124 1471 invalid_vga:
1472 fprintf(stderr, "Unknown vga type: %s\n", p);
1473 exit(1);
1474 }
cb5a7aa8 1475 while (*opts) {
1476 const char *nextopt;
1477
1478 if (strstart(opts, ",retrace=", &nextopt)) {
1479 opts = nextopt;
1480 if (strstart(opts, "dumb", &nextopt))
1481 vga_retrace_method = VGA_RETRACE_DUMB;
1482 else if (strstart(opts, "precise", &nextopt))
1483 vga_retrace_method = VGA_RETRACE_PRECISE;
1484 else goto invalid_vga;
1485 } else goto invalid_vga;
1486 opts = nextopt;
1487 }
3893c124 1488}
1489
1472a95b
JS
1490static DisplayType select_display(const char *p)
1491{
1492 const char *opts;
1493 DisplayType display = DT_DEFAULT;
1494
1495 if (strstart(p, "sdl", &opts)) {
1496#ifdef CONFIG_SDL
1497 display = DT_SDL;
1498 while (*opts) {
1499 const char *nextopt;
1500
1501 if (strstart(opts, ",frame=", &nextopt)) {
1502 opts = nextopt;
1503 if (strstart(opts, "on", &nextopt)) {
1504 no_frame = 0;
1505 } else if (strstart(opts, "off", &nextopt)) {
1506 no_frame = 1;
1507 } else {
05175535 1508 goto invalid_sdl_args;
1472a95b
JS
1509 }
1510 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1511 opts = nextopt;
1512 if (strstart(opts, "on", &nextopt)) {
1513 alt_grab = 1;
1514 } else if (strstart(opts, "off", &nextopt)) {
1515 alt_grab = 0;
1516 } else {
05175535 1517 goto invalid_sdl_args;
1472a95b
JS
1518 }
1519 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1520 opts = nextopt;
1521 if (strstart(opts, "on", &nextopt)) {
1522 ctrl_grab = 1;
1523 } else if (strstart(opts, "off", &nextopt)) {
1524 ctrl_grab = 0;
1525 } else {
05175535 1526 goto invalid_sdl_args;
1472a95b
JS
1527 }
1528 } else if (strstart(opts, ",window_close=", &nextopt)) {
1529 opts = nextopt;
1530 if (strstart(opts, "on", &nextopt)) {
1531 no_quit = 0;
1532 } else if (strstart(opts, "off", &nextopt)) {
1533 no_quit = 1;
1534 } else {
05175535 1535 goto invalid_sdl_args;
1472a95b
JS
1536 }
1537 } else {
05175535
PM
1538 invalid_sdl_args:
1539 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1540 exit(1);
1472a95b
JS
1541 }
1542 opts = nextopt;
1543 }
1544#else
1545 fprintf(stderr, "SDL support is disabled\n");
1546 exit(1);
1547#endif
3264ff12 1548 } else if (strstart(p, "vnc", &opts)) {
821601ea 1549#ifdef CONFIG_VNC
3264ff12
JS
1550 display_remote++;
1551
1552 if (*opts) {
1553 const char *nextopt;
1554
1555 if (strstart(opts, "=", &nextopt)) {
1556 vnc_display = nextopt;
1557 }
1558 }
1559 if (!vnc_display) {
1560 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1561 exit(1);
1562 }
821601ea
JS
1563#else
1564 fprintf(stderr, "VNC support is disabled\n");
1565 exit(1);
1566#endif
1472a95b
JS
1567 } else if (strstart(p, "curses", &opts)) {
1568#ifdef CONFIG_CURSES
1569 display = DT_CURSES;
1570#else
1571 fprintf(stderr, "Curses support is disabled\n");
1572 exit(1);
1573#endif
4171d32e
JS
1574 } else if (strstart(p, "none", &opts)) {
1575 display = DT_NONE;
1472a95b 1576 } else {
1472a95b
JS
1577 fprintf(stderr, "Unknown display type: %s\n", p);
1578 exit(1);
1579 }
1580
1581 return display;
1582}
1583
7d4c3d53
MA
1584static int balloon_parse(const char *arg)
1585{
382f0743 1586 QemuOpts *opts;
7d4c3d53 1587
382f0743
GH
1588 if (strcmp(arg, "none") == 0) {
1589 return 0;
1590 }
1591
1592 if (!strncmp(arg, "virtio", 6)) {
1593 if (arg[6] == ',') {
1594 /* have params -> parse them */
3329f07b 1595 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
382f0743
GH
1596 if (!opts)
1597 return -1;
1598 } else {
1599 /* create empty opts */
3329f07b 1600 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
7d4c3d53 1601 }
29f82b37 1602 qemu_opt_set(opts, "driver", "virtio-balloon");
382f0743 1603 return 0;
7d4c3d53 1604 }
382f0743
GH
1605
1606 return -1;
7d4c3d53 1607}
7d4c3d53 1608
5cea8590
PB
1609char *qemu_find_file(int type, const char *name)
1610{
1611 int len;
1612 const char *subdir;
1613 char *buf;
1614
1615 /* If name contains path separators then try it as a straight path. */
1616 if ((strchr(name, '/') || strchr(name, '\\'))
1617 && access(name, R_OK) == 0) {
73ffc805 1618 return qemu_strdup(name);
5cea8590
PB
1619 }
1620 switch (type) {
1621 case QEMU_FILE_TYPE_BIOS:
1622 subdir = "";
1623 break;
1624 case QEMU_FILE_TYPE_KEYMAP:
1625 subdir = "keymaps/";
1626 break;
1627 default:
1628 abort();
1629 }
1630 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1631 buf = qemu_mallocz(len);
3a41759d 1632 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
5cea8590
PB
1633 if (access(buf, R_OK)) {
1634 qemu_free(buf);
1635 return NULL;
1636 }
1637 return buf;
1638}
1639
ff952ba2
MA
1640static int device_help_func(QemuOpts *opts, void *opaque)
1641{
1642 return qdev_device_help(opts);
1643}
1644
f31d07d1
GH
1645static int device_init_func(QemuOpts *opts, void *opaque)
1646{
1647 DeviceState *dev;
1648
1649 dev = qdev_device_add(opts);
1650 if (!dev)
1651 return -1;
1652 return 0;
1653}
1654
1a688d3b
GH
1655static int chardev_init_func(QemuOpts *opts, void *opaque)
1656{
1657 CharDriverState *chr;
1658
1659 chr = qemu_chr_open_opts(opts, NULL);
1660 if (!chr)
1661 return -1;
1662 return 0;
1663}
1664
758e8e38 1665#ifdef CONFIG_VIRTFS
74db920c
GS
1666static int fsdev_init_func(QemuOpts *opts, void *opaque)
1667{
1668 int ret;
1669 ret = qemu_fsdev_add(opts);
1670
1671 return ret;
1672}
1673#endif
1674
88589343
GH
1675static int mon_init_func(QemuOpts *opts, void *opaque)
1676{
1677 CharDriverState *chr;
1678 const char *chardev;
1679 const char *mode;
1680 int flags;
1681
1682 mode = qemu_opt_get(opts, "mode");
1683 if (mode == NULL) {
1684 mode = "readline";
1685 }
1686 if (strcmp(mode, "readline") == 0) {
1687 flags = MONITOR_USE_READLINE;
1688 } else if (strcmp(mode, "control") == 0) {
1689 flags = MONITOR_USE_CONTROL;
1690 } else {
1691 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1692 exit(1);
1693 }
1694
39eaab9a
DB
1695 if (qemu_opt_get_bool(opts, "pretty", 0))
1696 flags |= MONITOR_USE_PRETTY;
1697
88589343
GH
1698 if (qemu_opt_get_bool(opts, "default", 0))
1699 flags |= MONITOR_IS_DEFAULT;
1700
1701 chardev = qemu_opt_get(opts, "chardev");
1702 chr = qemu_chr_find(chardev);
1703 if (chr == NULL) {
1704 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1705 exit(1);
1706 }
1707
1708 monitor_init(chr, flags);
1709 return 0;
1710}
1711
6ca5582d 1712static void monitor_parse(const char *optarg, const char *mode)
88589343
GH
1713{
1714 static int monitor_device_index = 0;
1715 QemuOpts *opts;
1716 const char *p;
1717 char label[32];
1718 int def = 0;
1719
1720 if (strstart(optarg, "chardev:", &p)) {
1721 snprintf(label, sizeof(label), "%s", p);
1722 } else {
140e065d
JK
1723 snprintf(label, sizeof(label), "compat_monitor%d",
1724 monitor_device_index);
1725 if (monitor_device_index == 0) {
88589343
GH
1726 def = 1;
1727 }
1728 opts = qemu_chr_parse_compat(label, optarg);
1729 if (!opts) {
1730 fprintf(stderr, "parse error: %s\n", optarg);
1731 exit(1);
1732 }
1733 }
1734
3329f07b 1735 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
88589343
GH
1736 if (!opts) {
1737 fprintf(stderr, "duplicate chardev: %s\n", label);
1738 exit(1);
1739 }
6ca5582d 1740 qemu_opt_set(opts, "mode", mode);
88589343
GH
1741 qemu_opt_set(opts, "chardev", label);
1742 if (def)
1743 qemu_opt_set(opts, "default", "on");
1744 monitor_device_index++;
1745}
1746
bd3c948d
GH
1747struct device_config {
1748 enum {
aee1b935
GH
1749 DEV_USB, /* -usbdevice */
1750 DEV_BT, /* -bt */
1751 DEV_SERIAL, /* -serial */
1752 DEV_PARALLEL, /* -parallel */
1753 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 1754 DEV_DEBUGCON, /* -debugcon */
bd3c948d
GH
1755 } type;
1756 const char *cmdline;
72cf2d4f 1757 QTAILQ_ENTRY(device_config) next;
bd3c948d 1758};
72cf2d4f 1759QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
1760
1761static void add_device_config(int type, const char *cmdline)
1762{
1763 struct device_config *conf;
1764
1765 conf = qemu_mallocz(sizeof(*conf));
1766 conf->type = type;
1767 conf->cmdline = cmdline;
72cf2d4f 1768 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
1769}
1770
1771static int foreach_device_config(int type, int (*func)(const char *cmdline))
1772{
1773 struct device_config *conf;
1774 int rc;
1775
72cf2d4f 1776 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
1777 if (conf->type != type)
1778 continue;
1779 rc = func(conf->cmdline);
1780 if (0 != rc)
1781 return rc;
1782 }
1783 return 0;
1784}
1785
998bbd74
GH
1786static int serial_parse(const char *devname)
1787{
1788 static int index = 0;
1789 char label[32];
1790
1791 if (strcmp(devname, "none") == 0)
1792 return 0;
1793 if (index == MAX_SERIAL_PORTS) {
1794 fprintf(stderr, "qemu: too many serial ports\n");
1795 exit(1);
1796 }
1797 snprintf(label, sizeof(label), "serial%d", index);
1798 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1799 if (!serial_hds[index]) {
1800 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1801 devname, strerror(errno));
1802 return -1;
1803 }
1804 index++;
1805 return 0;
1806}
1807
6a5e8b0e
GH
1808static int parallel_parse(const char *devname)
1809{
1810 static int index = 0;
1811 char label[32];
1812
1813 if (strcmp(devname, "none") == 0)
1814 return 0;
1815 if (index == MAX_PARALLEL_PORTS) {
1816 fprintf(stderr, "qemu: too many parallel ports\n");
1817 exit(1);
1818 }
1819 snprintf(label, sizeof(label), "parallel%d", index);
1820 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1821 if (!parallel_hds[index]) {
1822 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1823 devname, strerror(errno));
1824 return -1;
1825 }
1826 index++;
1827 return 0;
1828}
1829
aee1b935
GH
1830static int virtcon_parse(const char *devname)
1831{
3329f07b 1832 QemuOptsList *device = qemu_find_opts("device");
aee1b935
GH
1833 static int index = 0;
1834 char label[32];
392ecf54 1835 QemuOpts *bus_opts, *dev_opts;
aee1b935
GH
1836
1837 if (strcmp(devname, "none") == 0)
1838 return 0;
1839 if (index == MAX_VIRTIO_CONSOLES) {
1840 fprintf(stderr, "qemu: too many virtio consoles\n");
1841 exit(1);
1842 }
392ecf54 1843
3329f07b 1844 bus_opts = qemu_opts_create(device, NULL, 0);
392ecf54
AS
1845 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1846
3329f07b 1847 dev_opts = qemu_opts_create(device, NULL, 0);
392ecf54
AS
1848 qemu_opt_set(dev_opts, "driver", "virtconsole");
1849
aee1b935
GH
1850 snprintf(label, sizeof(label), "virtcon%d", index);
1851 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1852 if (!virtcon_hds[index]) {
1853 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1854 devname, strerror(errno));
1855 return -1;
1856 }
392ecf54
AS
1857 qemu_opt_set(dev_opts, "chardev", label);
1858
aee1b935
GH
1859 index++;
1860 return 0;
1861}
1862
c9f398e5
PA
1863static int debugcon_parse(const char *devname)
1864{
1865 QemuOpts *opts;
1866
1867 if (!qemu_chr_open("debugcon", devname, NULL)) {
1868 exit(1);
1869 }
3329f07b 1870 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
c9f398e5
PA
1871 if (!opts) {
1872 fprintf(stderr, "qemu: already have a debugcon device\n");
1873 exit(1);
1874 }
1875 qemu_opt_set(opts, "driver", "isa-debugcon");
1876 qemu_opt_set(opts, "chardev", "debugcon");
1877 return 0;
1878}
1879
303d4e86
AP
1880static int tcg_init(void)
1881{
1882 return 0;
1883}
1884
1885static struct {
1886 const char *opt_name;
1887 const char *name;
1888 int (*available)(void);
1889 int (*init)(void);
1890 int *allowed;
1891} accel_list[] = {
1892 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
1893 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1894};
1895
1896static int configure_accelerator(void)
1897{
1898 const char *p = NULL;
1899 char buf[10];
1900 int i, ret;
1901 bool accel_initalised = 0;
1902 bool init_failed = 0;
1903
1904 QemuOptsList *list = qemu_find_opts("machine");
1905 if (!QTAILQ_EMPTY(&list->head)) {
1906 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1907 }
1908
1909 if (p == NULL) {
1910 /* Use the default "accelerator", tcg */
1911 p = "tcg";
1912 }
1913
1914 while (!accel_initalised && *p != '\0') {
1915 if (*p == ':') {
1916 p++;
1917 }
1918 p = get_opt_name(buf, sizeof (buf), p, ':');
1919 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1920 if (strcmp(accel_list[i].opt_name, buf) == 0) {
1921 ret = accel_list[i].init();
1922 if (ret < 0) {
1923 init_failed = 1;
1924 if (!accel_list[i].available()) {
1925 printf("%s not supported for this target\n",
1926 accel_list[i].name);
1927 } else {
1928 fprintf(stderr, "failed to initialize %s: %s\n",
1929 accel_list[i].name,
1930 strerror(-ret));
1931 }
1932 } else {
1933 accel_initalised = 1;
1934 *(accel_list[i].allowed) = 1;
1935 }
1936 break;
1937 }
1938 }
1939 if (i == ARRAY_SIZE(accel_list)) {
1940 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
1941 }
1942 }
1943
1944 if (!accel_initalised) {
1945 fprintf(stderr, "No accelerator found!\n");
1946 exit(1);
1947 }
1948
1949 if (init_failed) {
1950 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
1951 }
1952
1953 return !accel_initalised;
1954}
1955
fd42deeb
GH
1956void qemu_add_exit_notifier(Notifier *notify)
1957{
1958 notifier_list_add(&exit_notifiers, notify);
1959}
1960
1961void qemu_remove_exit_notifier(Notifier *notify)
1962{
1963 notifier_list_remove(&exit_notifiers, notify);
1964}
1965
1966static void qemu_run_exit_notifiers(void)
1967{
1968 notifier_list_notify(&exit_notifiers);
1969}
1970
4cab946a
GN
1971void qemu_add_machine_init_done_notifier(Notifier *notify)
1972{
1973 notifier_list_add(&machine_init_done_notifiers, notify);
1974}
1975
1976static void qemu_run_machine_init_done_notifiers(void)
1977{
1978 notifier_list_notify(&machine_init_done_notifiers);
1979}
1980
6530a97b
AL
1981static const QEMUOption *lookup_opt(int argc, char **argv,
1982 const char **poptarg, int *poptind)
1983{
1984 const QEMUOption *popt;
1985 int optind = *poptind;
1986 char *r = argv[optind];
1987 const char *optarg;
1988
0f0bc3f1 1989 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
1990 optind++;
1991 /* Treat --foo the same as -foo. */
1992 if (r[1] == '-')
1993 r++;
1994 popt = qemu_options;
1995 for(;;) {
1996 if (!popt->name) {
0f0bc3f1 1997 error_report("invalid option");
6530a97b
AL
1998 exit(1);
1999 }
2000 if (!strcmp(popt->name, r + 1))
2001 break;
2002 popt++;
2003 }
2004 if (popt->flags & HAS_ARG) {
2005 if (optind >= argc) {
0f0bc3f1 2006 error_report("requires an argument");
6530a97b
AL
2007 exit(1);
2008 }
2009 optarg = argv[optind++];
0f0bc3f1 2010 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
2011 } else {
2012 optarg = NULL;
2013 }
2014
2015 *poptarg = optarg;
2016 *poptind = optind;
2017
2018 return popt;
2019}
2020
902b3d5c 2021int main(int argc, char **argv, char **envp)
0824d6fc 2022{
59030a8c 2023 const char *gdbstub_dev = NULL;
e4bcb14c 2024 int i;
da1fcfda 2025 int snapshot, linux_boot;
4e3de9e9 2026 const char *icount_option = NULL;
7f7f9873 2027 const char *initrd_filename;
a20dd508 2028 const char *kernel_filename, *kernel_cmdline;
195325a4 2029 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
3023f332 2030 DisplayState *ds;
7d957bd8 2031 DisplayChangeListener *dcl;
46d4767d 2032 int cyls, heads, secs, translation;
f31d07d1 2033 QemuOpts *hda_opts = NULL, *opts;
03b0ba70 2034 QemuOptsList *olist;
cd6f1169 2035 int optind;
6530a97b 2036 const char *optarg;
d63d307f 2037 const char *loadvm = NULL;
cc1daa40 2038 QEMUMachine *machine;
94fc95cd 2039 const char *cpu_model;
26a5f13b 2040 int tb_size;
93815bc2 2041 const char *pid_file = NULL;
5bb7910a 2042 const char *incoming = NULL;
821601ea 2043#ifdef CONFIG_VNC
993fbfdb 2044 int show_vnc_port = 0;
821601ea 2045#endif
292444cb 2046 int defconfig = 1;
ab6540d5 2047 const char *trace_file = NULL;
0b5538c3 2048
fd42deeb 2049 atexit(qemu_run_exit_notifiers);
65abca0a
MA
2050 error_set_progname(argv[0]);
2051
6875204c
JK
2052 init_clocks();
2053
902b3d5c 2054 qemu_cache_utils_init(envp);
2055
72cf2d4f 2056 QLIST_INIT (&vm_change_state_head);
fe98ac14 2057 os_setup_early_signal_handling();
be995c27 2058
f80f9ec9 2059 module_call_init(MODULE_INIT_MACHINE);
0c257437 2060 machine = find_default_machine();
94fc95cd 2061 cpu_model = NULL;
fc01f7e7 2062 initrd_filename = NULL;
4fc5d071 2063 ram_size = 0;
33e3963e 2064 snapshot = 0;
a20dd508
FB
2065 kernel_filename = NULL;
2066 kernel_cmdline = "";
c4b1fcc0 2067 cyls = heads = secs = 0;
46d4767d 2068 translation = BIOS_ATA_TRANSLATION_AUTO;
c4b1fcc0 2069
268a362c
AL
2070 for (i = 0; i < MAX_NODES; i++) {
2071 node_mem[i] = 0;
2072 node_cpumask[i] = 0;
2073 }
2074
268a362c 2075 nb_numa_nodes = 0;
7c9d8e07 2076 nb_nics = 0;
3b46e624 2077
26a5f13b 2078 tb_size = 0;
41bd639b
BS
2079 autostart= 1;
2080
292444cb
AL
2081 /* first pass of option parsing */
2082 optind = 1;
2083 while (optind < argc) {
2084 if (argv[optind][0] != '-') {
2085 /* disk image */
28e68d68 2086 optind++;
292444cb
AL
2087 continue;
2088 } else {
2089 const QEMUOption *popt;
2090
2091 popt = lookup_opt(argc, argv, &optarg, &optind);
2092 switch (popt->index) {
2093 case QEMU_OPTION_nodefconfig:
2094 defconfig=0;
2095 break;
2096 }
2097 }
2098 }
2099
2100 if (defconfig) {
dcfb0939 2101 int ret;
cf5a65aa 2102
dcfb0939 2103 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
019e78ba 2104 if (ret < 0 && ret != -ENOENT) {
dcfb0939 2105 exit(1);
292444cb
AL
2106 }
2107
dcfb0939 2108 ret = qemu_read_config_file(arch_config_name);
019e78ba 2109 if (ret < 0 && ret != -ENOENT) {
dcfb0939 2110 exit(1);
292444cb
AL
2111 }
2112 }
de06f8d1 2113 cpudef_init();
292444cb
AL
2114
2115 /* second pass of option parsing */
cd6f1169 2116 optind = 1;
0824d6fc 2117 for(;;) {
cd6f1169 2118 if (optind >= argc)
0824d6fc 2119 break;
6530a97b 2120 if (argv[optind][0] != '-') {
2292ddae 2121 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
cd6f1169
FB
2122 } else {
2123 const QEMUOption *popt;
2124
6530a97b 2125 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a
BS
2126 if (!(popt->arch_mask & arch_type)) {
2127 printf("Option %s not supported for this target\n", popt->name);
2128 exit(1);
2129 }
cd6f1169 2130 switch(popt->index) {
cc1daa40
FB
2131 case QEMU_OPTION_M:
2132 machine = find_machine(optarg);
2133 if (!machine) {
2134 QEMUMachine *m;
2135 printf("Supported machines are:\n");
2136 for(m = first_machine; m != NULL; m = m->next) {
3f6599e6
MM
2137 if (m->alias)
2138 printf("%-10s %s (alias of %s)\n",
2139 m->alias, m->desc, m->name);
cc1daa40 2140 printf("%-10s %s%s\n",
5fafdf24 2141 m->name, m->desc,
0c257437 2142 m->is_default ? " (default)" : "");
cc1daa40 2143 }
15f82208 2144 exit(*optarg != '?');
cc1daa40
FB
2145 }
2146 break;
94fc95cd
JM
2147 case QEMU_OPTION_cpu:
2148 /* hw initialization will check this */
15f82208 2149 if (*optarg == '?') {
262353cb 2150 list_cpus(stdout, &fprintf, optarg);
15f82208 2151 exit(0);
94fc95cd
JM
2152 } else {
2153 cpu_model = optarg;
2154 }
2155 break;
cd6f1169 2156 case QEMU_OPTION_initrd:
fc01f7e7
FB
2157 initrd_filename = optarg;
2158 break;
cd6f1169 2159 case QEMU_OPTION_hda:
5645b0f4
MA
2160 {
2161 char buf[256];
2162 if (cyls == 0)
2163 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2164 else
2165 snprintf(buf, sizeof(buf),
2166 "%s,cyls=%d,heads=%d,secs=%d%s",
2167 HD_OPTS , cyls, heads, secs,
2168 translation == BIOS_ATA_TRANSLATION_LBA ?
e4bcb14c 2169 ",trans=lba" :
5645b0f4 2170 translation == BIOS_ATA_TRANSLATION_NONE ?
e4bcb14c 2171 ",trans=none" : "");
5645b0f4
MA
2172 drive_add(IF_DEFAULT, 0, optarg, buf);
2173 break;
2174 }
cd6f1169 2175 case QEMU_OPTION_hdb:
cc1daa40
FB
2176 case QEMU_OPTION_hdc:
2177 case QEMU_OPTION_hdd:
2292ddae
MA
2178 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2179 HD_OPTS);
fc01f7e7 2180 break;
e4bcb14c 2181 case QEMU_OPTION_drive:
e2982c3a
MT
2182 if (drive_def(optarg) == NULL) {
2183 exit(1);
2184 }
e4bcb14c 2185 break;
d058fe03
GH
2186 case QEMU_OPTION_set:
2187 if (qemu_set_option(optarg) != 0)
2188 exit(1);
2189 break;
d0fef6fb
GH
2190 case QEMU_OPTION_global:
2191 if (qemu_global_option(optarg) != 0)
2192 exit(1);
2193 break;
3e3d5815 2194 case QEMU_OPTION_mtdblock:
2292ddae 2195 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3e3d5815 2196 break;
a1bb27b1 2197 case QEMU_OPTION_sd:
2292ddae 2198 drive_add(IF_SD, 0, optarg, SD_OPTS);
a1bb27b1 2199 break;
86f55663 2200 case QEMU_OPTION_pflash:
2292ddae 2201 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
86f55663 2202 break;
cd6f1169 2203 case QEMU_OPTION_snapshot:
33e3963e
FB
2204 snapshot = 1;
2205 break;
cd6f1169 2206 case QEMU_OPTION_hdachs:
330d0414 2207 {
330d0414
FB
2208 const char *p;
2209 p = optarg;
2210 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
2211 if (cyls < 1 || cyls > 16383)
2212 goto chs_fail;
330d0414
FB
2213 if (*p != ',')
2214 goto chs_fail;
2215 p++;
2216 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
2217 if (heads < 1 || heads > 16)
2218 goto chs_fail;
330d0414
FB
2219 if (*p != ',')
2220 goto chs_fail;
2221 p++;
2222 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
2223 if (secs < 1 || secs > 63)
2224 goto chs_fail;
2225 if (*p == ',') {
2226 p++;
2227 if (!strcmp(p, "none"))
2228 translation = BIOS_ATA_TRANSLATION_NONE;
2229 else if (!strcmp(p, "lba"))
2230 translation = BIOS_ATA_TRANSLATION_LBA;
2231 else if (!strcmp(p, "auto"))
2232 translation = BIOS_ATA_TRANSLATION_AUTO;
2233 else
2234 goto chs_fail;
2235 } else if (*p != '\0') {
c4b1fcc0 2236 chs_fail:
46d4767d
FB
2237 fprintf(stderr, "qemu: invalid physical CHS format\n");
2238 exit(1);
c4b1fcc0 2239 }
9dfd7c7a
GH
2240 if (hda_opts != NULL) {
2241 char num[16];
2242 snprintf(num, sizeof(num), "%d", cyls);
2243 qemu_opt_set(hda_opts, "cyls", num);
2244 snprintf(num, sizeof(num), "%d", heads);
2245 qemu_opt_set(hda_opts, "heads", num);
2246 snprintf(num, sizeof(num), "%d", secs);
2247 qemu_opt_set(hda_opts, "secs", num);
2248 if (translation == BIOS_ATA_TRANSLATION_LBA)
2249 qemu_opt_set(hda_opts, "trans", "lba");
2250 if (translation == BIOS_ATA_TRANSLATION_NONE)
2251 qemu_opt_set(hda_opts, "trans", "none");
2252 }
330d0414
FB
2253 }
2254 break;
268a362c
AL
2255 case QEMU_OPTION_numa:
2256 if (nb_numa_nodes >= MAX_NODES) {
2257 fprintf(stderr, "qemu: too many NUMA nodes\n");
2258 exit(1);
2259 }
2260 numa_add(optarg);
2261 break;
1472a95b
JS
2262 case QEMU_OPTION_display:
2263 display_type = select_display(optarg);
2264 break;
cd6f1169 2265 case QEMU_OPTION_nographic:
993fbfdb 2266 display_type = DT_NOGRAPHIC;
a20dd508 2267 break;
4d3b6f6e 2268 case QEMU_OPTION_curses:
47b05369 2269#ifdef CONFIG_CURSES
993fbfdb 2270 display_type = DT_CURSES;
47b05369
JS
2271#else
2272 fprintf(stderr, "Curses support is disabled\n");
2273 exit(1);
4d3b6f6e 2274#endif
47b05369 2275 break;
a171fe39
AZ
2276 case QEMU_OPTION_portrait:
2277 graphic_rotate = 1;
2278 break;
cd6f1169 2279 case QEMU_OPTION_kernel:
a20dd508
FB
2280 kernel_filename = optarg;
2281 break;
cd6f1169 2282 case QEMU_OPTION_append:
a20dd508 2283 kernel_cmdline = optarg;
313aa567 2284 break;
cd6f1169 2285 case QEMU_OPTION_cdrom:
2292ddae 2286 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
36b486bb 2287 break;
cd6f1169 2288 case QEMU_OPTION_boot:
28c5af54 2289 {
ef3adf68 2290 static const char * const params[] = {
95387491 2291 "order", "once", "menu", NULL
ef3adf68
JK
2292 };
2293 char buf[sizeof(boot_devices)];
e0f084bf 2294 char *standard_boot_devices;
ef3adf68
JK
2295 int legacy = 0;
2296
2297 if (!strchr(optarg, '=')) {
2298 legacy = 1;
2299 pstrcpy(buf, sizeof(buf), optarg);
2300 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2301 fprintf(stderr,
2302 "qemu: unknown boot parameter '%s' in '%s'\n",
2303 buf, optarg);
2304 exit(1);
2305 }
2306
2307 if (legacy ||
2308 get_param_value(buf, sizeof(buf), "order", optarg)) {
4e9e9d6e 2309 validate_bootdevices(buf);
ef3adf68 2310 pstrcpy(boot_devices, sizeof(boot_devices), buf);
28c5af54 2311 }
e0f084bf
JK
2312 if (!legacy) {
2313 if (get_param_value(buf, sizeof(buf),
2314 "once", optarg)) {
4e9e9d6e 2315 validate_bootdevices(buf);
e0f084bf
JK
2316 standard_boot_devices = qemu_strdup(boot_devices);
2317 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2318 qemu_register_reset(restore_boot_devices,
2319 standard_boot_devices);
2320 }
95387491
JK
2321 if (get_param_value(buf, sizeof(buf),
2322 "menu", optarg)) {
2323 if (!strcmp(buf, "on")) {
2324 boot_menu = 1;
2325 } else if (!strcmp(buf, "off")) {
2326 boot_menu = 0;
2327 } else {
2328 fprintf(stderr,
2329 "qemu: invalid option value '%s'\n",
2330 buf);
2331 exit(1);
2332 }
2333 }
e0f084bf 2334 }
36b486bb
FB
2335 }
2336 break;
cd6f1169 2337 case QEMU_OPTION_fda:
cd6f1169 2338 case QEMU_OPTION_fdb:
2292ddae
MA
2339 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2340 optarg, FD_OPTS);
c45886db 2341 break;
52ca8d6a
FB
2342 case QEMU_OPTION_no_fd_bootchk:
2343 fd_bootchk = 0;
2344 break;
a1ea458f 2345 case QEMU_OPTION_netdev:
3329f07b 2346 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
a1ea458f
MM
2347 exit(1);
2348 }
2349 break;
7c9d8e07 2350 case QEMU_OPTION_net:
3329f07b 2351 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
c4b1fcc0
FB
2352 exit(1);
2353 }
702c651c 2354 break;
c7f74643
FB
2355#ifdef CONFIG_SLIRP
2356 case QEMU_OPTION_tftp:
ad196a9d 2357 legacy_tftp_prefix = optarg;
9bf05444 2358 break;
47d5d01a 2359 case QEMU_OPTION_bootp:
ad196a9d 2360 legacy_bootp_filename = optarg;
47d5d01a 2361 break;
9bf05444 2362 case QEMU_OPTION_redir:
0752706d
MA
2363 if (net_slirp_redir(optarg) < 0)
2364 exit(1);
9bf05444 2365 break;
c7f74643 2366#endif
dc72ac14 2367 case QEMU_OPTION_bt:
bd3c948d 2368 add_device_config(DEV_BT, optarg);
dc72ac14 2369 break;
1d14ffa9 2370 case QEMU_OPTION_audio_help:
ad96090a
BS
2371 if (!(audio_available())) {
2372 printf("Option %s not supported for this target\n", popt->name);
2373 exit(1);
2374 }
1d14ffa9
FB
2375 AUD_help ();
2376 exit (0);
2377 break;
2378 case QEMU_OPTION_soundhw:
ad96090a
BS
2379 if (!(audio_available())) {
2380 printf("Option %s not supported for this target\n", popt->name);
2381 exit(1);
2382 }
1d14ffa9
FB
2383 select_soundhw (optarg);
2384 break;
cd6f1169 2385 case QEMU_OPTION_h:
15f82208 2386 help(0);
cd6f1169 2387 break;
9bd7e6d9
PB
2388 case QEMU_OPTION_version:
2389 version();
2390 exit(0);
2391 break;
00f82b8a 2392 case QEMU_OPTION_m: {
70b4f4bb 2393 int64_t value;
9f9b17a4
JS
2394
2395 value = strtosz(optarg, NULL);
2396 if (value < 0) {
00f82b8a 2397 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
2398 exit(1);
2399 }
00f82b8a
AJ
2400
2401 /* On 32-bit hosts, QEMU is limited by virtual address space */
4a1418e0 2402 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
00f82b8a
AJ
2403 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2404 exit(1);
2405 }
c227f099 2406 if (value != (uint64_t)(ram_addr_t)value) {
00f82b8a
AJ
2407 fprintf(stderr, "qemu: ram size too large\n");
2408 exit(1);
2409 }
2410 ram_size = value;
cd6f1169 2411 break;
00f82b8a 2412 }
c902760f
MT
2413 case QEMU_OPTION_mempath:
2414 mem_path = optarg;
2415 break;
2416#ifdef MAP_POPULATE
2417 case QEMU_OPTION_mem_prealloc:
2418 mem_prealloc = 1;
2419 break;
2420#endif
cd6f1169 2421 case QEMU_OPTION_d:
de06f8d1 2422 set_cpu_log(optarg);
cd6f1169 2423 break;
cd6f1169 2424 case QEMU_OPTION_s:
59030a8c 2425 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
cd6f1169 2426 break;
59030a8c
AL
2427 case QEMU_OPTION_gdb:
2428 gdbstub_dev = optarg;
cd6f1169 2429 break;
cd6f1169 2430 case QEMU_OPTION_L:
5cea8590 2431 data_dir = optarg;
cd6f1169 2432 break;
1192dad8
JM
2433 case QEMU_OPTION_bios:
2434 bios_name = optarg;
2435 break;
1b530a6d
AJ
2436 case QEMU_OPTION_singlestep:
2437 singlestep = 1;
2438 break;
cd6f1169 2439 case QEMU_OPTION_S:
3c07f8e8 2440 autostart = 0;
cd6f1169 2441 break;
3d11d0eb
FB
2442 case QEMU_OPTION_k:
2443 keyboard_layout = optarg;
2444 break;
ee22c2f7
FB
2445 case QEMU_OPTION_localtime:
2446 rtc_utc = 0;
2447 break;
3893c124 2448 case QEMU_OPTION_vga:
2449 select_vgahw (optarg);
1bfe856e 2450 break;
e9b137c2
FB
2451 case QEMU_OPTION_g:
2452 {
2453 const char *p;
2454 int w, h, depth;
2455 p = optarg;
2456 w = strtol(p, (char **)&p, 10);
2457 if (w <= 0) {
2458 graphic_error:
2459 fprintf(stderr, "qemu: invalid resolution or depth\n");
2460 exit(1);
2461 }
2462 if (*p != 'x')
2463 goto graphic_error;
2464 p++;
2465 h = strtol(p, (char **)&p, 10);
2466 if (h <= 0)
2467 goto graphic_error;
2468 if (*p == 'x') {
2469 p++;
2470 depth = strtol(p, (char **)&p, 10);
5fafdf24 2471 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
2472 depth != 24 && depth != 32)
2473 goto graphic_error;
2474 } else if (*p == '\0') {
2475 depth = graphic_depth;
2476 } else {
2477 goto graphic_error;
2478 }
3b46e624 2479
e9b137c2
FB
2480 graphic_width = w;
2481 graphic_height = h;
2482 graphic_depth = depth;
2483 }
2484 break;
20d8a3ed
TS
2485 case QEMU_OPTION_echr:
2486 {
2487 char *r;
2488 term_escape_char = strtol(optarg, &r, 0);
2489 if (r == optarg)
2490 printf("Bad argument to echr\n");
2491 break;
2492 }
82c643ff 2493 case QEMU_OPTION_monitor:
6ca5582d
GH
2494 monitor_parse(optarg, "readline");
2495 default_monitor = 0;
2496 break;
2497 case QEMU_OPTION_qmp:
2498 monitor_parse(optarg, "control");
2d114dc1 2499 default_monitor = 0;
82c643ff 2500 break;
22a0e04b 2501 case QEMU_OPTION_mon:
3329f07b 2502 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
22a0e04b 2503 if (!opts) {
22a0e04b
GH
2504 exit(1);
2505 }
2d114dc1 2506 default_monitor = 0;
22a0e04b 2507 break;
191bc01b 2508 case QEMU_OPTION_chardev:
3329f07b 2509 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
191bc01b 2510 if (!opts) {
191bc01b
GH
2511 exit(1);
2512 }
191bc01b 2513 break;
74db920c 2514 case QEMU_OPTION_fsdev:
03b0ba70
GH
2515 olist = qemu_find_opts("fsdev");
2516 if (!olist) {
2517 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2518 exit(1);
2519 }
2520 opts = qemu_opts_parse(olist, optarg, 1);
74db920c
GS
2521 if (!opts) {
2522 fprintf(stderr, "parse error: %s\n", optarg);
2523 exit(1);
2524 }
2525 break;
3d54abc7 2526 case QEMU_OPTION_virtfs: {
e14ea479
SH
2527 QemuOpts *fsdev;
2528 QemuOpts *device;
3d54abc7 2529
03b0ba70
GH
2530 olist = qemu_find_opts("virtfs");
2531 if (!olist) {
2532 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2533 exit(1);
2534 }
2535 opts = qemu_opts_parse(olist, optarg, 1);
3d54abc7
GS
2536 if (!opts) {
2537 fprintf(stderr, "parse error: %s\n", optarg);
2538 exit(1);
2539 }
2540
9ce56db6
VJJ
2541 if (qemu_opt_get(opts, "fstype") == NULL ||
2542 qemu_opt_get(opts, "mount_tag") == NULL ||
2543 qemu_opt_get(opts, "path") == NULL ||
2544 qemu_opt_get(opts, "security_model") == NULL) {
2545 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
12848bfc 2546 "security_model=[mapped|passthrough|none],"
e14ea479 2547 "mount_tag=tag.\n");
9ce56db6
VJJ
2548 exit(1);
2549 }
2550
e14ea479
SH
2551 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2552 qemu_opt_get(opts, "mount_tag"), 1);
2553 if (!fsdev) {
2554 fprintf(stderr, "duplicate fsdev id: %s\n",
2555 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2556 exit(1);
2557 }
e14ea479
SH
2558 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2559 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2560 qemu_opt_set(fsdev, "security_model",
2561 qemu_opt_get(opts, "security_model"));
2562
2563 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2564 qemu_opt_set(device, "driver", "virtio-9p-pci");
2565 qemu_opt_set(device, "fsdev",
2566 qemu_opt_get(opts, "mount_tag"));
2567 qemu_opt_set(device, "mount_tag",
2568 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2569 break;
2570 }
82c643ff 2571 case QEMU_OPTION_serial:
998bbd74
GH
2572 add_device_config(DEV_SERIAL, optarg);
2573 default_serial = 0;
18141ed6
JK
2574 if (strncmp(optarg, "mon:", 4) == 0) {
2575 default_monitor = 0;
2576 }
82c643ff 2577 break;
9dd986cc 2578 case QEMU_OPTION_watchdog:
09aaa160
MA
2579 if (watchdog) {
2580 fprintf(stderr,
2581 "qemu: only one watchdog option may be given\n");
2582 return 1;
2583 }
2584 watchdog = optarg;
9dd986cc
RJ
2585 break;
2586 case QEMU_OPTION_watchdog_action:
2587 if (select_watchdog_action(optarg) == -1) {
2588 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2589 exit(1);
2590 }
2591 break;
51ecf136 2592 case QEMU_OPTION_virtiocon:
aee1b935
GH
2593 add_device_config(DEV_VIRTCON, optarg);
2594 default_virtcon = 0;
18141ed6
JK
2595 if (strncmp(optarg, "mon:", 4) == 0) {
2596 default_monitor = 0;
2597 }
51ecf136 2598 break;
6508fe59 2599 case QEMU_OPTION_parallel:
6a5e8b0e
GH
2600 add_device_config(DEV_PARALLEL, optarg);
2601 default_parallel = 0;
18141ed6
JK
2602 if (strncmp(optarg, "mon:", 4) == 0) {
2603 default_monitor = 0;
2604 }
6508fe59 2605 break;
c9f398e5
PA
2606 case QEMU_OPTION_debugcon:
2607 add_device_config(DEV_DEBUGCON, optarg);
2608 break;
d63d307f
FB
2609 case QEMU_OPTION_loadvm:
2610 loadvm = optarg;
2611 break;
2612 case QEMU_OPTION_full_screen:
2613 full_screen = 1;
2614 break;
667accab 2615#ifdef CONFIG_SDL
43523e93
TS
2616 case QEMU_OPTION_no_frame:
2617 no_frame = 1;
2618 break;
3780e197
TS
2619 case QEMU_OPTION_alt_grab:
2620 alt_grab = 1;
2621 break;
0ca9f8a4
DK
2622 case QEMU_OPTION_ctrl_grab:
2623 ctrl_grab = 1;
2624 break;
667accab
TS
2625 case QEMU_OPTION_no_quit:
2626 no_quit = 1;
2627 break;
7d957bd8 2628 case QEMU_OPTION_sdl:
993fbfdb 2629 display_type = DT_SDL;
7d957bd8 2630 break;
58fc096c
JS
2631#else
2632 case QEMU_OPTION_no_frame:
2633 case QEMU_OPTION_alt_grab:
2634 case QEMU_OPTION_ctrl_grab:
2635 case QEMU_OPTION_no_quit:
2636 case QEMU_OPTION_sdl:
2637 fprintf(stderr, "SDL support is disabled\n");
2638 exit(1);
667accab 2639#endif
f7cce898 2640 case QEMU_OPTION_pidfile:
93815bc2 2641 pid_file = optarg;
f7cce898 2642 break;
a09db21f
FB
2643 case QEMU_OPTION_win2k_hack:
2644 win2k_install_hack = 1;
2645 break;
73822ec8
AL
2646 case QEMU_OPTION_rtc_td_hack:
2647 rtc_td_hack = 1;
2648 break;
8a92ea2f 2649 case QEMU_OPTION_acpitable:
de06f8d1 2650 do_acpitable_option(optarg);
8a92ea2f 2651 break;
b6f6e3d3 2652 case QEMU_OPTION_smbios:
de06f8d1 2653 do_smbios_option(optarg);
b6f6e3d3 2654 break;
7ba1e619 2655 case QEMU_OPTION_enable_kvm:
303d4e86
AP
2656 olist = qemu_find_opts("machine");
2657 qemu_opts_reset(olist);
2658 qemu_opts_parse(olist, "accel=kvm", 0);
2659 break;
2660 case QEMU_OPTION_machine:
2661 olist = qemu_find_opts("machine");
2662 qemu_opts_reset(olist);
2663 opts = qemu_opts_parse(olist, optarg, 0);
2664 if (!opts) {
2665 fprintf(stderr, "parse error: %s\n", optarg);
2666 exit(1);
2667 }
7ba1e619 2668 break;
bb36d470
FB
2669 case QEMU_OPTION_usb:
2670 usb_enabled = 1;
2671 break;
a594cfbf
FB
2672 case QEMU_OPTION_usbdevice:
2673 usb_enabled = 1;
bd3c948d
GH
2674 add_device_config(DEV_USB, optarg);
2675 break;
2676 case QEMU_OPTION_device:
3329f07b 2677 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
f31d07d1
GH
2678 exit(1);
2679 }
a594cfbf 2680 break;
6a00d601 2681 case QEMU_OPTION_smp:
dc6b1c09 2682 smp_parse(optarg);
b2097003 2683 if (smp_cpus < 1) {
6a00d601
FB
2684 fprintf(stderr, "Invalid number of CPUs\n");
2685 exit(1);
2686 }
6be68d7e
JS
2687 if (max_cpus < smp_cpus) {
2688 fprintf(stderr, "maxcpus must be equal to or greater than "
2689 "smp\n");
2690 exit(1);
2691 }
2692 if (max_cpus > 255) {
2693 fprintf(stderr, "Unsupported number of maxcpus\n");
2694 exit(1);
2695 }
6a00d601 2696 break;
24236869 2697 case QEMU_OPTION_vnc:
821601ea 2698#ifdef CONFIG_VNC
6b62dc2d 2699 display_remote++;
821601ea
JS
2700 vnc_display = optarg;
2701#else
2702 fprintf(stderr, "VNC support is disabled\n");
2703 exit(1);
2704#endif
2705 break;
6515b203
FB
2706 case QEMU_OPTION_no_acpi:
2707 acpi_enabled = 0;
2708 break;
16b29ae1
AL
2709 case QEMU_OPTION_no_hpet:
2710 no_hpet = 1;
2711 break;
7d4c3d53
MA
2712 case QEMU_OPTION_balloon:
2713 if (balloon_parse(optarg) < 0) {
2714 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2715 exit(1);
2716 }
df97b920 2717 break;
d1beab82
FB
2718 case QEMU_OPTION_no_reboot:
2719 no_reboot = 1;
2720 break;
b2f76161
AJ
2721 case QEMU_OPTION_no_shutdown:
2722 no_shutdown = 1;
2723 break;
9467cd46
AZ
2724 case QEMU_OPTION_show_cursor:
2725 cursor_hide = 0;
2726 break;
8fcb1b90
BS
2727 case QEMU_OPTION_uuid:
2728 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2729 fprintf(stderr, "Fail to parse UUID string."
2730 " Wrong format.\n");
2731 exit(1);
2732 }
2733 break;
9ae02555
TS
2734 case QEMU_OPTION_option_rom:
2735 if (nb_option_roms >= MAX_OPTION_ROMS) {
2736 fprintf(stderr, "Too many option ROMs\n");
2737 exit(1);
2738 }
2e55e842
GN
2739 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2740 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2741 option_rom[nb_option_roms].bootindex =
2742 qemu_opt_get_number(opts, "bootindex", -1);
2743 if (!option_rom[nb_option_roms].name) {
2744 fprintf(stderr, "Option ROM file is not specified\n");
2745 exit(1);
2746 }
9ae02555
TS
2747 nb_option_roms++;
2748 break;
8e71621f
PB
2749 case QEMU_OPTION_semihosting:
2750 semihosting_enabled = 1;
2751 break;
c35734b2 2752 case QEMU_OPTION_name:
1889465a
AK
2753 qemu_name = qemu_strdup(optarg);
2754 {
2755 char *p = strchr(qemu_name, ',');
2756 if (p != NULL) {
2757 *p++ = 0;
2758 if (strncmp(p, "process=", 8)) {
5697f6ae 2759 fprintf(stderr, "Unknown subargument %s to -name\n", p);
1889465a
AK
2760 exit(1);
2761 }
2762 p += 8;
ce798cf2 2763 os_set_proc_name(p);
1889465a
AK
2764 }
2765 }
c35734b2 2766 break;
66508601
BS
2767 case QEMU_OPTION_prom_env:
2768 if (nb_prom_envs >= MAX_PROM_ENVS) {
2769 fprintf(stderr, "Too many prom variables\n");
2770 exit(1);
2771 }
2772 prom_envs[nb_prom_envs] = optarg;
2773 nb_prom_envs++;
2774 break;
2b8f2d41
AZ
2775 case QEMU_OPTION_old_param:
2776 old_param = 1;
05ebd537 2777 break;
f3dcfada
TS
2778 case QEMU_OPTION_clock:
2779 configure_alarms(optarg);
2780 break;
7e0af5d0 2781 case QEMU_OPTION_startdate:
1ed2fc1f
JK
2782 configure_rtc_date_offset(optarg, 1);
2783 break;
2784 case QEMU_OPTION_rtc:
3329f07b 2785 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
1ed2fc1f 2786 if (!opts) {
1ed2fc1f 2787 exit(1);
7e0af5d0 2788 }
1ed2fc1f 2789 configure_rtc(opts);
7e0af5d0 2790 break;
26a5f13b
FB
2791 case QEMU_OPTION_tb_size:
2792 tb_size = strtol(optarg, NULL, 0);
2793 if (tb_size < 0)
2794 tb_size = 0;
2795 break;
2e70f6ef 2796 case QEMU_OPTION_icount:
4e3de9e9 2797 icount_option = optarg;
2e70f6ef 2798 break;
5bb7910a
AL
2799 case QEMU_OPTION_incoming:
2800 incoming = optarg;
8e84865e 2801 incoming_expected = true;
5bb7910a 2802 break;
d8c208dd
GH
2803 case QEMU_OPTION_nodefaults:
2804 default_serial = 0;
2805 default_parallel = 0;
aee1b935 2806 default_virtcon = 0;
d8c208dd
GH
2807 default_monitor = 0;
2808 default_vga = 0;
cb4522cc 2809 default_net = 0;
ac33f8fa
GH
2810 default_floppy = 0;
2811 default_cdrom = 0;
2812 default_sdcard = 0;
d8c208dd 2813 break;
e37630ca 2814 case QEMU_OPTION_xen_domid:
ad96090a
BS
2815 if (!(xen_available())) {
2816 printf("Option %s not supported for this target\n", popt->name);
2817 exit(1);
2818 }
e37630ca
AL
2819 xen_domid = atoi(optarg);
2820 break;
2821 case QEMU_OPTION_xen_create:
ad96090a
BS
2822 if (!(xen_available())) {
2823 printf("Option %s not supported for this target\n", popt->name);
2824 exit(1);
2825 }
e37630ca
AL
2826 xen_mode = XEN_CREATE;
2827 break;
2828 case QEMU_OPTION_xen_attach:
ad96090a
BS
2829 if (!(xen_available())) {
2830 printf("Option %s not supported for this target\n", popt->name);
2831 exit(1);
2832 }
e37630ca
AL
2833 xen_mode = XEN_ATTACH;
2834 break;
ab6540d5
PS
2835#ifdef CONFIG_SIMPLE_TRACE
2836 case QEMU_OPTION_trace:
2837 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2838 if (opts) {
2839 trace_file = qemu_opt_get(opts, "file");
2840 }
2841 break;
2842#endif
715a664a
GH
2843 case QEMU_OPTION_readconfig:
2844 {
dcfb0939
KW
2845 int ret = qemu_read_config_file(optarg);
2846 if (ret < 0) {
2847 fprintf(stderr, "read config %s: %s\n", optarg,
2848 strerror(-ret));
715a664a
GH
2849 exit(1);
2850 }
715a664a
GH
2851 break;
2852 }
29b0040b
GH
2853 case QEMU_OPTION_spice:
2854 olist = qemu_find_opts("spice");
2855 if (!olist) {
2856 fprintf(stderr, "spice is not supported by this qemu build.\n");
2857 exit(1);
2858 }
2859 opts = qemu_opts_parse(olist, optarg, 0);
2860 if (!opts) {
2861 fprintf(stderr, "parse error: %s\n", optarg);
2862 exit(1);
2863 }
2864 break;
715a664a
GH
2865 case QEMU_OPTION_writeconfig:
2866 {
2867 FILE *fp;
2868 if (strcmp(optarg, "-") == 0) {
2869 fp = stdout;
2870 } else {
2871 fp = fopen(optarg, "w");
2872 if (fp == NULL) {
2873 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2874 exit(1);
2875 }
2876 }
2877 qemu_config_write(fp);
2878 fclose(fp);
2879 break;
2880 }
59a5264b
JS
2881 default:
2882 os_parse_cmd_args(popt->index, optarg);
cd6f1169 2883 }
0824d6fc
FB
2884 }
2885 }
0f0bc3f1 2886 loc_set_none();
330d0414 2887
31d3c9b8
SH
2888 if (!st_init(trace_file)) {
2889 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2890 }
0b5538c3 2891
5cea8590
PB
2892 /* If no data_dir is specified then try to find it relative to the
2893 executable path. */
2894 if (!data_dir) {
6170540b 2895 data_dir = os_find_datadir(argv[0]);
5cea8590
PB
2896 }
2897 /* If all else fails use the install patch specified when building. */
2898 if (!data_dir) {
1dabe05c 2899 data_dir = CONFIG_QEMU_DATADIR;
5cea8590
PB
2900 }
2901
6be68d7e
JS
2902 /*
2903 * Default to max_cpus = smp_cpus, in case the user doesn't
2904 * specify a max_cpus value.
2905 */
2906 if (!max_cpus)
2907 max_cpus = smp_cpus;
2908
3d878caa 2909 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
2910 if (smp_cpus > machine->max_cpus) {
2911 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2912 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2913 machine->max_cpus);
2914 exit(1);
2915 }
2916
67b724e6
AP
2917 /*
2918 * Get the default machine options from the machine if it is not already
2919 * specified either by the configuration file or by the command line.
2920 */
2921 if (machine->default_machine_opts) {
2922 QemuOptsList *list = qemu_find_opts("machine");
2923 const char *p = NULL;
2924
2925 if (!QTAILQ_EMPTY(&list->head)) {
2926 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2927 }
2928 if (p == NULL) {
2929 opts = qemu_opts_parse(qemu_find_opts("machine"),
2930 machine->default_machine_opts, 0);
2931 if (!opts) {
2932 fprintf(stderr, "parse error for machine %s: %s\n",
2933 machine->name, machine->default_machine_opts);
2934 exit(1);
2935 }
2936 }
2937 }
2938
3329f07b
GH
2939 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2940 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
998bbd74 2941
986c5f78
GH
2942 if (machine->no_serial) {
2943 default_serial = 0;
2944 }
2945 if (machine->no_parallel) {
2946 default_parallel = 0;
2947 }
2948 if (!machine->use_virtcon) {
2949 default_virtcon = 0;
2950 }
2951 if (machine->no_vga) {
2952 default_vga = 0;
2953 }
ac33f8fa
GH
2954 if (machine->no_floppy) {
2955 default_floppy = 0;
2956 }
2957 if (machine->no_cdrom) {
2958 default_cdrom = 0;
2959 }
2960 if (machine->no_sdcard) {
2961 default_sdcard = 0;
2962 }
986c5f78 2963
993fbfdb 2964 if (display_type == DT_NOGRAPHIC) {
6a5e8b0e
GH
2965 if (default_parallel)
2966 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
2967 if (default_serial && default_monitor) {
2968 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
2969 } else if (default_virtcon && default_monitor) {
2970 add_device_config(DEV_VIRTCON, "mon:stdio");
e1c09175
GH
2971 } else {
2972 if (default_serial)
2973 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
2974 if (default_virtcon)
2975 add_device_config(DEV_VIRTCON, "stdio");
e1c09175 2976 if (default_monitor)
6ca5582d 2977 monitor_parse("stdio", "readline");
e1c09175 2978 }
998bbd74
GH
2979 } else {
2980 if (default_serial)
2981 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
2982 if (default_parallel)
2983 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 2984 if (default_monitor)
6ca5582d 2985 monitor_parse("vc:80Cx24C", "readline");
38536da1
AG
2986 if (default_virtcon)
2987 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
bc0129d9 2988 }
64465297
GH
2989 if (default_vga)
2990 vga_interface_type = VGA_CIRRUS;
bc0129d9 2991
a5829fd9
T
2992 socket_init();
2993
3329f07b 2994 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
1a688d3b 2995 exit(1);
758e8e38 2996#ifdef CONFIG_VIRTFS
3329f07b 2997 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
74db920c
GS
2998 exit(1);
2999 }
3000#endif
1a688d3b 3001
eb505be1 3002 os_daemonize();
71e3ceb8 3003
aa26bb2d 3004 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
eb505be1 3005 os_pidfile_error();
93815bc2
TS
3006 exit(1);
3007 }
3008
303d4e86 3009 configure_accelerator();
214910a7 3010
3fcf7b6b
AL
3011 if (qemu_init_main_loop()) {
3012 fprintf(stderr, "qemu_init_main_loop failed\n");
3013 exit(1);
3014 }
a20dd508 3015 linux_boot = (kernel_filename != NULL);
6c41b272 3016
f8d39c01
TS
3017 if (!linux_boot && *kernel_cmdline != '\0') {
3018 fprintf(stderr, "-append only allowed with -kernel option\n");
3019 exit(1);
3020 }
3021
3022 if (!linux_boot && initrd_filename != NULL) {
3023 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3024 exit(1);
3025 }
3026
9156d763 3027 os_set_line_buffering();
3b46e624 3028
7183b4b4
AL
3029 if (init_timer_alarm() < 0) {
3030 fprintf(stderr, "could not initialize alarm timer\n");
3031 exit(1);
3032 }
4e3de9e9 3033 configure_icount(icount_option);
634fce96 3034
dc1c9fe8
MM
3035 if (net_init_clients() < 0) {
3036 exit(1);
702c651c 3037 }
f1510b2c 3038
dc72ac14 3039 /* init the bluetooth world */
bd3c948d
GH
3040 if (foreach_device_config(DEV_BT, bt_parse))
3041 exit(1);
dc72ac14 3042
0824d6fc 3043 /* init the memory */
94a6b54f
PB
3044 if (ram_size == 0)
3045 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
9ae02555 3046
26a5f13b
FB
3047 /* init the dynamic translator */
3048 cpu_exec_init_all(tb_size * 1024 * 1024);
3049
eb852011 3050 bdrv_init_with_whitelist();
c4b1fcc0 3051
c163b5ca 3052 blk_mig_init();
3053
e4bcb14c 3054 /* open the virtual block devices */
9dfd7c7a 3055 if (snapshot)
3329f07b
GH
3056 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3057 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
9dfd7c7a 3058 exit(1);
3e3d5815 3059
4e5d9b57
MA
3060 default_drive(default_cdrom, snapshot, machine->use_scsi,
3061 IF_DEFAULT, 2, CDROM_OPTS);
3062 default_drive(default_floppy, snapshot, machine->use_scsi,
3063 IF_FLOPPY, 0, FD_OPTS);
3064 default_drive(default_sdcard, snapshot, machine->use_scsi,
3065 IF_SD, 0, SD_OPTS);
3066
97ab12d4 3067 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
c163b5ca 3068 ram_load, NULL);
8a7ddc38 3069
268a362c
AL
3070 if (nb_numa_nodes > 0) {
3071 int i;
3072
3073 if (nb_numa_nodes > smp_cpus) {
3074 nb_numa_nodes = smp_cpus;
3075 }
3076
3077 /* If no memory size if given for any node, assume the default case
3078 * and distribute the available memory equally across all nodes
3079 */
3080 for (i = 0; i < nb_numa_nodes; i++) {
3081 if (node_mem[i] != 0)
3082 break;
3083 }
3084 if (i == nb_numa_nodes) {
3085 uint64_t usedmem = 0;
3086
3087 /* On Linux, the each node's border has to be 8MB aligned,
3088 * the final node gets the rest.
3089 */
3090 for (i = 0; i < nb_numa_nodes - 1; i++) {
3091 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3092 usedmem += node_mem[i];
3093 }
3094 node_mem[i] = ram_size - usedmem;
3095 }
3096
3097 for (i = 0; i < nb_numa_nodes; i++) {
3098 if (node_cpumask[i] != 0)
3099 break;
3100 }
3101 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3102 * must cope with this anyway, because there are BIOSes out there in
3103 * real machines which also use this scheme.
3104 */
3105 if (i == nb_numa_nodes) {
3106 for (i = 0; i < smp_cpus; i++) {
3107 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3108 }
3109 }
3110 }
3111
3329f07b 3112 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
157b9319
JK
3113 exit(1);
3114 }
3115
998bbd74
GH
3116 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3117 exit(1);
6a5e8b0e
GH
3118 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3119 exit(1);
aee1b935
GH
3120 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3121 exit(1);
c9f398e5
PA
3122 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3123 exit(1);
2796dae0 3124
aae9460e
PB
3125 module_call_init(MODULE_INIT_DEVICE);
3126
3329f07b 3127 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
ff952ba2
MA
3128 exit(0);
3129
09aaa160
MA
3130 if (watchdog) {
3131 i = select_watchdog(watchdog);
3132 if (i > 0)
3133 exit (i == 1 ? 1 : 0);
3134 }
3135
b6b61144 3136 if (machine->compat_props) {
458fb679 3137 qdev_prop_register_global_list(machine->compat_props);
b6b61144 3138 }
d0fef6fb
GH
3139 qemu_add_globals();
3140
fbe1b595 3141 machine->init(ram_size, boot_devices,
3023f332
AL
3142 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3143
ea375f9a 3144 cpu_synchronize_all_post_init();
268a362c 3145
87d0a28e 3146 set_numa_modes();
268a362c 3147
6f338c34
AL
3148 current_machine = machine;
3149
3023f332
AL
3150 /* init USB devices */
3151 if (usb_enabled) {
0752706d
MA
3152 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3153 exit(1);
3023f332
AL
3154 }
3155
bd3c948d 3156 /* init generic devices */
3329f07b 3157 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
bd3c948d
GH
3158 exit(1);
3159
668680f7
MA
3160 net_check_clients();
3161
3023f332 3162 /* just use the first displaystate for the moment */
b473df6e 3163 ds = get_displaystate();
993fbfdb 3164
a3e22260
GH
3165 if (using_spice)
3166 display_remote++;
6b62dc2d 3167 if (display_type == DT_DEFAULT && !display_remote) {
993fbfdb
AL
3168#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3169 display_type = DT_SDL;
821601ea 3170#elif defined(CONFIG_VNC)
993fbfdb
AL
3171 vnc_display = "localhost:0,to=99";
3172 show_vnc_port = 1;
821601ea
JS
3173#else
3174 display_type = DT_NONE;
993fbfdb
AL
3175#endif
3176 }
821601ea 3177
993fbfdb 3178
6b62dc2d 3179 /* init local displays */
993fbfdb
AL
3180 switch (display_type) {
3181 case DT_NOGRAPHIC:
3182 break;
4d3b6f6e 3183#if defined(CONFIG_CURSES)
993fbfdb
AL
3184 case DT_CURSES:
3185 curses_display_init(ds, full_screen);
3186 break;
4d3b6f6e 3187#endif
5b0753e0 3188#if defined(CONFIG_SDL)
993fbfdb
AL
3189 case DT_SDL:
3190 sdl_display_init(ds, full_screen, no_frame);
3191 break;
5b0753e0 3192#elif defined(CONFIG_COCOA)
993fbfdb
AL
3193 case DT_SDL:
3194 cocoa_display_init(ds, full_screen);
3195 break;
313aa567 3196#endif
6b62dc2d
GH
3197 default:
3198 break;
3199 }
3200
0ce235a7
GN
3201 /* must be after terminal init, SDL library changes signal handlers */
3202 os_setup_signal_handling();
3203
821601ea 3204#ifdef CONFIG_VNC
6b62dc2d
GH
3205 /* init remote displays */
3206 if (vnc_display) {
993fbfdb
AL
3207 vnc_display_init(ds);
3208 if (vnc_display_open(ds, vnc_display) < 0)
3209 exit(1);
f92f8afe 3210
993fbfdb
AL
3211 if (show_vnc_port) {
3212 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
f92f8afe 3213 }
313aa567 3214 }
821601ea 3215#endif
a3e22260 3216#ifdef CONFIG_SPICE
a19cbfb3 3217 if (using_spice && !qxl_enabled) {
a3e22260
GH
3218 qemu_spice_display_init(ds);
3219 }
3220#endif
5b08fc10 3221
6b62dc2d
GH
3222 /* display setup */
3223 dpy_resize(ds);
3023f332
AL
3224 dcl = ds->listeners;
3225 while (dcl != NULL) {
3226 if (dcl->dpy_refresh != NULL) {
7bd427d8
PB
3227 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3228 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
0f2ad63f 3229 break;
20d8a3ed 3230 }
3023f332 3231 dcl = dcl->next;
20d8a3ed 3232 }
6b62dc2d 3233 if (ds->gui_timer == NULL) {
7bd427d8
PB
3234 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3235 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
9043b62d 3236 }
b473df6e 3237 text_consoles_set_display(ds);
2796dae0 3238
59030a8c
AL
3239 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3240 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3241 gdbstub_dev);
3242 exit(1);
45669e00 3243 }
45669e00 3244
3418bd25
GH
3245 qdev_machine_creation_done();
3246
15ff7705
GH
3247 if (rom_load_all() != 0) {
3248 fprintf(stderr, "rom loading failed\n");
3249 exit(1);
3250 }
45a50b16 3251
80376c3f
IY
3252 /* TODO: once all bus devices are qdevified, this should be done
3253 * when bus is created by qdev.c */
3254 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4cab946a
GN
3255 qemu_run_machine_init_done_notifiers();
3256
504c2948 3257 qemu_system_reset();
05f2401e 3258 if (loadvm) {
03cd4655 3259 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
3260 autostart = 0;
3261 }
3262 }
d63d307f 3263
2bb8c10c 3264 if (incoming) {
8ca5e801
JQ
3265 int ret = qemu_start_incoming_migration(incoming);
3266 if (ret < 0) {
3267 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3268 incoming, ret);
3269 exit(ret);
3270 }
6b99dadc 3271 } else if (autostart) {
c0f4ce77 3272 vm_start();
6b99dadc 3273 }
ffd843bc 3274
eb505be1 3275 os_setup_post();
71e3ceb8 3276
8a7ddc38 3277 main_loop();
40c3bac3 3278 quit_timers();
63a01ef8 3279 net_cleanup();
b46a8906 3280
0824d6fc
FB
3281 return 0;
3282}