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