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