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