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