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