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