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