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