]> git.proxmox.com Git - qemu.git/blame - vl.c
do_snapshot_blkdev() error on missing snapshot_file argument
[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;
a19cbfb3
GH
1507 } else if (strstart(p, "qxl", &opts)) {
1508 vga_interface_type = VGA_QXL;
28b85ed8 1509 } else if (!strstart(p, "none", &opts)) {
3893c124 1510 invalid_vga:
1511 fprintf(stderr, "Unknown vga type: %s\n", p);
1512 exit(1);
1513 }
cb5a7aa8 1514 while (*opts) {
1515 const char *nextopt;
1516
1517 if (strstart(opts, ",retrace=", &nextopt)) {
1518 opts = nextopt;
1519 if (strstart(opts, "dumb", &nextopt))
1520 vga_retrace_method = VGA_RETRACE_DUMB;
1521 else if (strstart(opts, "precise", &nextopt))
1522 vga_retrace_method = VGA_RETRACE_PRECISE;
1523 else goto invalid_vga;
1524 } else goto invalid_vga;
1525 opts = nextopt;
1526 }
3893c124 1527}
1528
7d4c3d53
MA
1529static int balloon_parse(const char *arg)
1530{
382f0743 1531 QemuOpts *opts;
7d4c3d53 1532
382f0743
GH
1533 if (strcmp(arg, "none") == 0) {
1534 return 0;
1535 }
1536
1537 if (!strncmp(arg, "virtio", 6)) {
1538 if (arg[6] == ',') {
1539 /* have params -> parse them */
3329f07b 1540 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
382f0743
GH
1541 if (!opts)
1542 return -1;
1543 } else {
1544 /* create empty opts */
3329f07b 1545 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
7d4c3d53 1546 }
382f0743
GH
1547 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1548 return 0;
7d4c3d53 1549 }
382f0743
GH
1550
1551 return -1;
7d4c3d53 1552}
7d4c3d53 1553
5cea8590
PB
1554char *qemu_find_file(int type, const char *name)
1555{
1556 int len;
1557 const char *subdir;
1558 char *buf;
1559
1560 /* If name contains path separators then try it as a straight path. */
1561 if ((strchr(name, '/') || strchr(name, '\\'))
1562 && access(name, R_OK) == 0) {
73ffc805 1563 return qemu_strdup(name);
5cea8590
PB
1564 }
1565 switch (type) {
1566 case QEMU_FILE_TYPE_BIOS:
1567 subdir = "";
1568 break;
1569 case QEMU_FILE_TYPE_KEYMAP:
1570 subdir = "keymaps/";
1571 break;
1572 default:
1573 abort();
1574 }
1575 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1576 buf = qemu_mallocz(len);
3a41759d 1577 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
5cea8590
PB
1578 if (access(buf, R_OK)) {
1579 qemu_free(buf);
1580 return NULL;
1581 }
1582 return buf;
1583}
1584
ff952ba2
MA
1585static int device_help_func(QemuOpts *opts, void *opaque)
1586{
1587 return qdev_device_help(opts);
1588}
1589
f31d07d1
GH
1590static int device_init_func(QemuOpts *opts, void *opaque)
1591{
1592 DeviceState *dev;
1593
1594 dev = qdev_device_add(opts);
1595 if (!dev)
1596 return -1;
1597 return 0;
1598}
1599
1a688d3b
GH
1600static int chardev_init_func(QemuOpts *opts, void *opaque)
1601{
1602 CharDriverState *chr;
1603
1604 chr = qemu_chr_open_opts(opts, NULL);
1605 if (!chr)
1606 return -1;
1607 return 0;
1608}
1609
758e8e38 1610#ifdef CONFIG_VIRTFS
74db920c
GS
1611static int fsdev_init_func(QemuOpts *opts, void *opaque)
1612{
1613 int ret;
1614 ret = qemu_fsdev_add(opts);
1615
1616 return ret;
1617}
1618#endif
1619
88589343
GH
1620static int mon_init_func(QemuOpts *opts, void *opaque)
1621{
1622 CharDriverState *chr;
1623 const char *chardev;
1624 const char *mode;
1625 int flags;
1626
1627 mode = qemu_opt_get(opts, "mode");
1628 if (mode == NULL) {
1629 mode = "readline";
1630 }
1631 if (strcmp(mode, "readline") == 0) {
1632 flags = MONITOR_USE_READLINE;
1633 } else if (strcmp(mode, "control") == 0) {
1634 flags = MONITOR_USE_CONTROL;
1635 } else {
1636 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1637 exit(1);
1638 }
1639
39eaab9a
DB
1640 if (qemu_opt_get_bool(opts, "pretty", 0))
1641 flags |= MONITOR_USE_PRETTY;
1642
88589343
GH
1643 if (qemu_opt_get_bool(opts, "default", 0))
1644 flags |= MONITOR_IS_DEFAULT;
1645
1646 chardev = qemu_opt_get(opts, "chardev");
1647 chr = qemu_chr_find(chardev);
1648 if (chr == NULL) {
1649 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1650 exit(1);
1651 }
1652
1653 monitor_init(chr, flags);
1654 return 0;
1655}
1656
6ca5582d 1657static void monitor_parse(const char *optarg, const char *mode)
88589343
GH
1658{
1659 static int monitor_device_index = 0;
1660 QemuOpts *opts;
1661 const char *p;
1662 char label[32];
1663 int def = 0;
1664
1665 if (strstart(optarg, "chardev:", &p)) {
1666 snprintf(label, sizeof(label), "%s", p);
1667 } else {
140e065d
JK
1668 snprintf(label, sizeof(label), "compat_monitor%d",
1669 monitor_device_index);
1670 if (monitor_device_index == 0) {
88589343
GH
1671 def = 1;
1672 }
1673 opts = qemu_chr_parse_compat(label, optarg);
1674 if (!opts) {
1675 fprintf(stderr, "parse error: %s\n", optarg);
1676 exit(1);
1677 }
1678 }
1679
3329f07b 1680 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
88589343
GH
1681 if (!opts) {
1682 fprintf(stderr, "duplicate chardev: %s\n", label);
1683 exit(1);
1684 }
6ca5582d 1685 qemu_opt_set(opts, "mode", mode);
88589343
GH
1686 qemu_opt_set(opts, "chardev", label);
1687 if (def)
1688 qemu_opt_set(opts, "default", "on");
1689 monitor_device_index++;
1690}
1691
bd3c948d
GH
1692struct device_config {
1693 enum {
aee1b935
GH
1694 DEV_USB, /* -usbdevice */
1695 DEV_BT, /* -bt */
1696 DEV_SERIAL, /* -serial */
1697 DEV_PARALLEL, /* -parallel */
1698 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 1699 DEV_DEBUGCON, /* -debugcon */
bd3c948d
GH
1700 } type;
1701 const char *cmdline;
72cf2d4f 1702 QTAILQ_ENTRY(device_config) next;
bd3c948d 1703};
72cf2d4f 1704QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
1705
1706static void add_device_config(int type, const char *cmdline)
1707{
1708 struct device_config *conf;
1709
1710 conf = qemu_mallocz(sizeof(*conf));
1711 conf->type = type;
1712 conf->cmdline = cmdline;
72cf2d4f 1713 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
1714}
1715
1716static int foreach_device_config(int type, int (*func)(const char *cmdline))
1717{
1718 struct device_config *conf;
1719 int rc;
1720
72cf2d4f 1721 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
1722 if (conf->type != type)
1723 continue;
1724 rc = func(conf->cmdline);
1725 if (0 != rc)
1726 return rc;
1727 }
1728 return 0;
1729}
1730
998bbd74
GH
1731static int serial_parse(const char *devname)
1732{
1733 static int index = 0;
1734 char label[32];
1735
1736 if (strcmp(devname, "none") == 0)
1737 return 0;
1738 if (index == MAX_SERIAL_PORTS) {
1739 fprintf(stderr, "qemu: too many serial ports\n");
1740 exit(1);
1741 }
1742 snprintf(label, sizeof(label), "serial%d", index);
1743 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1744 if (!serial_hds[index]) {
1745 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1746 devname, strerror(errno));
1747 return -1;
1748 }
1749 index++;
1750 return 0;
1751}
1752
6a5e8b0e
GH
1753static int parallel_parse(const char *devname)
1754{
1755 static int index = 0;
1756 char label[32];
1757
1758 if (strcmp(devname, "none") == 0)
1759 return 0;
1760 if (index == MAX_PARALLEL_PORTS) {
1761 fprintf(stderr, "qemu: too many parallel ports\n");
1762 exit(1);
1763 }
1764 snprintf(label, sizeof(label), "parallel%d", index);
1765 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1766 if (!parallel_hds[index]) {
1767 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1768 devname, strerror(errno));
1769 return -1;
1770 }
1771 index++;
1772 return 0;
1773}
1774
aee1b935
GH
1775static int virtcon_parse(const char *devname)
1776{
3329f07b 1777 QemuOptsList *device = qemu_find_opts("device");
aee1b935
GH
1778 static int index = 0;
1779 char label[32];
392ecf54 1780 QemuOpts *bus_opts, *dev_opts;
aee1b935
GH
1781
1782 if (strcmp(devname, "none") == 0)
1783 return 0;
1784 if (index == MAX_VIRTIO_CONSOLES) {
1785 fprintf(stderr, "qemu: too many virtio consoles\n");
1786 exit(1);
1787 }
392ecf54 1788
3329f07b 1789 bus_opts = qemu_opts_create(device, NULL, 0);
392ecf54
AS
1790 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1791
3329f07b 1792 dev_opts = qemu_opts_create(device, NULL, 0);
392ecf54
AS
1793 qemu_opt_set(dev_opts, "driver", "virtconsole");
1794
aee1b935
GH
1795 snprintf(label, sizeof(label), "virtcon%d", index);
1796 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1797 if (!virtcon_hds[index]) {
1798 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1799 devname, strerror(errno));
1800 return -1;
1801 }
392ecf54
AS
1802 qemu_opt_set(dev_opts, "chardev", label);
1803
aee1b935
GH
1804 index++;
1805 return 0;
1806}
1807
c9f398e5
PA
1808static int debugcon_parse(const char *devname)
1809{
1810 QemuOpts *opts;
1811
1812 if (!qemu_chr_open("debugcon", devname, NULL)) {
1813 exit(1);
1814 }
3329f07b 1815 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
c9f398e5
PA
1816 if (!opts) {
1817 fprintf(stderr, "qemu: already have a debugcon device\n");
1818 exit(1);
1819 }
1820 qemu_opt_set(opts, "driver", "isa-debugcon");
1821 qemu_opt_set(opts, "chardev", "debugcon");
1822 return 0;
1823}
1824
fd42deeb
GH
1825void qemu_add_exit_notifier(Notifier *notify)
1826{
1827 notifier_list_add(&exit_notifiers, notify);
1828}
1829
1830void qemu_remove_exit_notifier(Notifier *notify)
1831{
1832 notifier_list_remove(&exit_notifiers, notify);
1833}
1834
1835static void qemu_run_exit_notifiers(void)
1836{
1837 notifier_list_notify(&exit_notifiers);
1838}
1839
4cab946a
GN
1840void qemu_add_machine_init_done_notifier(Notifier *notify)
1841{
1842 notifier_list_add(&machine_init_done_notifiers, notify);
1843}
1844
1845static void qemu_run_machine_init_done_notifiers(void)
1846{
1847 notifier_list_notify(&machine_init_done_notifiers);
1848}
1849
6530a97b
AL
1850static const QEMUOption *lookup_opt(int argc, char **argv,
1851 const char **poptarg, int *poptind)
1852{
1853 const QEMUOption *popt;
1854 int optind = *poptind;
1855 char *r = argv[optind];
1856 const char *optarg;
1857
0f0bc3f1 1858 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
1859 optind++;
1860 /* Treat --foo the same as -foo. */
1861 if (r[1] == '-')
1862 r++;
1863 popt = qemu_options;
1864 for(;;) {
1865 if (!popt->name) {
0f0bc3f1 1866 error_report("invalid option");
6530a97b
AL
1867 exit(1);
1868 }
1869 if (!strcmp(popt->name, r + 1))
1870 break;
1871 popt++;
1872 }
1873 if (popt->flags & HAS_ARG) {
1874 if (optind >= argc) {
0f0bc3f1 1875 error_report("requires an argument");
6530a97b
AL
1876 exit(1);
1877 }
1878 optarg = argv[optind++];
0f0bc3f1 1879 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
1880 } else {
1881 optarg = NULL;
1882 }
1883
1884 *poptarg = optarg;
1885 *poptind = optind;
1886
1887 return popt;
1888}
1889
902b3d5c 1890int main(int argc, char **argv, char **envp)
0824d6fc 1891{
59030a8c 1892 const char *gdbstub_dev = NULL;
e4bcb14c 1893 int i;
da1fcfda 1894 int snapshot, linux_boot;
4e3de9e9 1895 const char *icount_option = NULL;
7f7f9873 1896 const char *initrd_filename;
a20dd508 1897 const char *kernel_filename, *kernel_cmdline;
195325a4 1898 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
3023f332 1899 DisplayState *ds;
7d957bd8 1900 DisplayChangeListener *dcl;
46d4767d 1901 int cyls, heads, secs, translation;
f31d07d1 1902 QemuOpts *hda_opts = NULL, *opts;
03b0ba70 1903 QemuOptsList *olist;
cd6f1169 1904 int optind;
6530a97b 1905 const char *optarg;
d63d307f 1906 const char *loadvm = NULL;
cc1daa40 1907 QEMUMachine *machine;
94fc95cd 1908 const char *cpu_model;
26a5f13b 1909 int tb_size;
93815bc2 1910 const char *pid_file = NULL;
5bb7910a 1911 const char *incoming = NULL;
993fbfdb 1912 int show_vnc_port = 0;
292444cb 1913 int defconfig = 1;
0bd48850 1914
ab6540d5
PS
1915#ifdef CONFIG_SIMPLE_TRACE
1916 const char *trace_file = NULL;
1917#endif
fd42deeb 1918 atexit(qemu_run_exit_notifiers);
65abca0a
MA
1919 error_set_progname(argv[0]);
1920
6875204c
JK
1921 init_clocks();
1922
902b3d5c 1923 qemu_cache_utils_init(envp);
1924
72cf2d4f 1925 QLIST_INIT (&vm_change_state_head);
fe98ac14 1926 os_setup_early_signal_handling();
be995c27 1927
f80f9ec9 1928 module_call_init(MODULE_INIT_MACHINE);
0c257437 1929 machine = find_default_machine();
94fc95cd 1930 cpu_model = NULL;
fc01f7e7 1931 initrd_filename = NULL;
4fc5d071 1932 ram_size = 0;
33e3963e 1933 snapshot = 0;
a20dd508
FB
1934 kernel_filename = NULL;
1935 kernel_cmdline = "";
c4b1fcc0 1936 cyls = heads = secs = 0;
46d4767d 1937 translation = BIOS_ATA_TRANSLATION_AUTO;
c4b1fcc0 1938
268a362c
AL
1939 for (i = 0; i < MAX_NODES; i++) {
1940 node_mem[i] = 0;
1941 node_cpumask[i] = 0;
1942 }
1943
268a362c 1944 nb_numa_nodes = 0;
7c9d8e07 1945 nb_nics = 0;
3b46e624 1946
26a5f13b 1947 tb_size = 0;
41bd639b
BS
1948 autostart= 1;
1949
292444cb
AL
1950 /* first pass of option parsing */
1951 optind = 1;
1952 while (optind < argc) {
1953 if (argv[optind][0] != '-') {
1954 /* disk image */
28e68d68 1955 optind++;
292444cb
AL
1956 continue;
1957 } else {
1958 const QEMUOption *popt;
1959
1960 popt = lookup_opt(argc, argv, &optarg, &optind);
1961 switch (popt->index) {
1962 case QEMU_OPTION_nodefconfig:
1963 defconfig=0;
1964 break;
1965 }
1966 }
1967 }
1968
1969 if (defconfig) {
dcfb0939 1970 int ret;
cf5a65aa 1971
dcfb0939 1972 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
019e78ba 1973 if (ret < 0 && ret != -ENOENT) {
dcfb0939 1974 exit(1);
292444cb
AL
1975 }
1976
dcfb0939 1977 ret = qemu_read_config_file(arch_config_name);
019e78ba 1978 if (ret < 0 && ret != -ENOENT) {
dcfb0939 1979 exit(1);
292444cb
AL
1980 }
1981 }
de06f8d1 1982 cpudef_init();
292444cb
AL
1983
1984 /* second pass of option parsing */
cd6f1169 1985 optind = 1;
0824d6fc 1986 for(;;) {
cd6f1169 1987 if (optind >= argc)
0824d6fc 1988 break;
6530a97b 1989 if (argv[optind][0] != '-') {
9dfd7c7a 1990 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
cd6f1169
FB
1991 } else {
1992 const QEMUOption *popt;
1993
6530a97b 1994 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a
BS
1995 if (!(popt->arch_mask & arch_type)) {
1996 printf("Option %s not supported for this target\n", popt->name);
1997 exit(1);
1998 }
cd6f1169 1999 switch(popt->index) {
cc1daa40
FB
2000 case QEMU_OPTION_M:
2001 machine = find_machine(optarg);
2002 if (!machine) {
2003 QEMUMachine *m;
2004 printf("Supported machines are:\n");
2005 for(m = first_machine; m != NULL; m = m->next) {
3f6599e6
MM
2006 if (m->alias)
2007 printf("%-10s %s (alias of %s)\n",
2008 m->alias, m->desc, m->name);
cc1daa40 2009 printf("%-10s %s%s\n",
5fafdf24 2010 m->name, m->desc,
0c257437 2011 m->is_default ? " (default)" : "");
cc1daa40 2012 }
15f82208 2013 exit(*optarg != '?');
cc1daa40
FB
2014 }
2015 break;
94fc95cd
JM
2016 case QEMU_OPTION_cpu:
2017 /* hw initialization will check this */
15f82208 2018 if (*optarg == '?') {
262353cb 2019 list_cpus(stdout, &fprintf, optarg);
15f82208 2020 exit(0);
94fc95cd
JM
2021 } else {
2022 cpu_model = optarg;
2023 }
2024 break;
cd6f1169 2025 case QEMU_OPTION_initrd:
fc01f7e7
FB
2026 initrd_filename = optarg;
2027 break;
cd6f1169 2028 case QEMU_OPTION_hda:
e4bcb14c 2029 if (cyls == 0)
9dfd7c7a 2030 hda_opts = drive_add(optarg, HD_ALIAS, 0);
e4bcb14c 2031 else
9dfd7c7a 2032 hda_opts = drive_add(optarg, HD_ALIAS
e4bcb14c 2033 ",cyls=%d,heads=%d,secs=%d%s",
609497ab 2034 0, cyls, heads, secs,
e4bcb14c
TS
2035 translation == BIOS_ATA_TRANSLATION_LBA ?
2036 ",trans=lba" :
2037 translation == BIOS_ATA_TRANSLATION_NONE ?
2038 ",trans=none" : "");
2039 break;
cd6f1169 2040 case QEMU_OPTION_hdb:
cc1daa40
FB
2041 case QEMU_OPTION_hdc:
2042 case QEMU_OPTION_hdd:
609497ab 2043 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
fc01f7e7 2044 break;
e4bcb14c 2045 case QEMU_OPTION_drive:
609497ab 2046 drive_add(NULL, "%s", optarg);
e4bcb14c 2047 break;
d058fe03
GH
2048 case QEMU_OPTION_set:
2049 if (qemu_set_option(optarg) != 0)
2050 exit(1);
2051 break;
d0fef6fb
GH
2052 case QEMU_OPTION_global:
2053 if (qemu_global_option(optarg) != 0)
2054 exit(1);
2055 break;
3e3d5815 2056 case QEMU_OPTION_mtdblock:
609497ab 2057 drive_add(optarg, MTD_ALIAS);
3e3d5815 2058 break;
a1bb27b1 2059 case QEMU_OPTION_sd:
609497ab 2060 drive_add(optarg, SD_ALIAS);
a1bb27b1 2061 break;
86f55663 2062 case QEMU_OPTION_pflash:
609497ab 2063 drive_add(optarg, PFLASH_ALIAS);
86f55663 2064 break;
cd6f1169 2065 case QEMU_OPTION_snapshot:
33e3963e
FB
2066 snapshot = 1;
2067 break;
cd6f1169 2068 case QEMU_OPTION_hdachs:
330d0414 2069 {
330d0414
FB
2070 const char *p;
2071 p = optarg;
2072 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
2073 if (cyls < 1 || cyls > 16383)
2074 goto chs_fail;
330d0414
FB
2075 if (*p != ',')
2076 goto chs_fail;
2077 p++;
2078 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
2079 if (heads < 1 || heads > 16)
2080 goto chs_fail;
330d0414
FB
2081 if (*p != ',')
2082 goto chs_fail;
2083 p++;
2084 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
2085 if (secs < 1 || secs > 63)
2086 goto chs_fail;
2087 if (*p == ',') {
2088 p++;
2089 if (!strcmp(p, "none"))
2090 translation = BIOS_ATA_TRANSLATION_NONE;
2091 else if (!strcmp(p, "lba"))
2092 translation = BIOS_ATA_TRANSLATION_LBA;
2093 else if (!strcmp(p, "auto"))
2094 translation = BIOS_ATA_TRANSLATION_AUTO;
2095 else
2096 goto chs_fail;
2097 } else if (*p != '\0') {
c4b1fcc0 2098 chs_fail:
46d4767d
FB
2099 fprintf(stderr, "qemu: invalid physical CHS format\n");
2100 exit(1);
c4b1fcc0 2101 }
9dfd7c7a
GH
2102 if (hda_opts != NULL) {
2103 char num[16];
2104 snprintf(num, sizeof(num), "%d", cyls);
2105 qemu_opt_set(hda_opts, "cyls", num);
2106 snprintf(num, sizeof(num), "%d", heads);
2107 qemu_opt_set(hda_opts, "heads", num);
2108 snprintf(num, sizeof(num), "%d", secs);
2109 qemu_opt_set(hda_opts, "secs", num);
2110 if (translation == BIOS_ATA_TRANSLATION_LBA)
2111 qemu_opt_set(hda_opts, "trans", "lba");
2112 if (translation == BIOS_ATA_TRANSLATION_NONE)
2113 qemu_opt_set(hda_opts, "trans", "none");
2114 }
330d0414
FB
2115 }
2116 break;
268a362c
AL
2117 case QEMU_OPTION_numa:
2118 if (nb_numa_nodes >= MAX_NODES) {
2119 fprintf(stderr, "qemu: too many NUMA nodes\n");
2120 exit(1);
2121 }
2122 numa_add(optarg);
2123 break;
cd6f1169 2124 case QEMU_OPTION_nographic:
993fbfdb 2125 display_type = DT_NOGRAPHIC;
a20dd508 2126 break;
4d3b6f6e
AZ
2127#ifdef CONFIG_CURSES
2128 case QEMU_OPTION_curses:
993fbfdb 2129 display_type = DT_CURSES;
4d3b6f6e
AZ
2130 break;
2131#endif
a171fe39
AZ
2132 case QEMU_OPTION_portrait:
2133 graphic_rotate = 1;
2134 break;
cd6f1169 2135 case QEMU_OPTION_kernel:
a20dd508
FB
2136 kernel_filename = optarg;
2137 break;
cd6f1169 2138 case QEMU_OPTION_append:
a20dd508 2139 kernel_cmdline = optarg;
313aa567 2140 break;
cd6f1169 2141 case QEMU_OPTION_cdrom:
609497ab 2142 drive_add(optarg, CDROM_ALIAS);
36b486bb 2143 break;
cd6f1169 2144 case QEMU_OPTION_boot:
28c5af54 2145 {
ef3adf68 2146 static const char * const params[] = {
95387491 2147 "order", "once", "menu", NULL
ef3adf68
JK
2148 };
2149 char buf[sizeof(boot_devices)];
e0f084bf 2150 char *standard_boot_devices;
ef3adf68
JK
2151 int legacy = 0;
2152
2153 if (!strchr(optarg, '=')) {
2154 legacy = 1;
2155 pstrcpy(buf, sizeof(buf), optarg);
2156 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2157 fprintf(stderr,
2158 "qemu: unknown boot parameter '%s' in '%s'\n",
2159 buf, optarg);
2160 exit(1);
2161 }
2162
2163 if (legacy ||
2164 get_param_value(buf, sizeof(buf), "order", optarg)) {
4e9e9d6e 2165 validate_bootdevices(buf);
ef3adf68 2166 pstrcpy(boot_devices, sizeof(boot_devices), buf);
28c5af54 2167 }
e0f084bf
JK
2168 if (!legacy) {
2169 if (get_param_value(buf, sizeof(buf),
2170 "once", optarg)) {
4e9e9d6e 2171 validate_bootdevices(buf);
e0f084bf
JK
2172 standard_boot_devices = qemu_strdup(boot_devices);
2173 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2174 qemu_register_reset(restore_boot_devices,
2175 standard_boot_devices);
2176 }
95387491
JK
2177 if (get_param_value(buf, sizeof(buf),
2178 "menu", optarg)) {
2179 if (!strcmp(buf, "on")) {
2180 boot_menu = 1;
2181 } else if (!strcmp(buf, "off")) {
2182 boot_menu = 0;
2183 } else {
2184 fprintf(stderr,
2185 "qemu: invalid option value '%s'\n",
2186 buf);
2187 exit(1);
2188 }
2189 }
e0f084bf 2190 }
36b486bb
FB
2191 }
2192 break;
cd6f1169 2193 case QEMU_OPTION_fda:
cd6f1169 2194 case QEMU_OPTION_fdb:
609497ab 2195 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
c45886db 2196 break;
52ca8d6a
FB
2197 case QEMU_OPTION_no_fd_bootchk:
2198 fd_bootchk = 0;
2199 break;
a1ea458f 2200 case QEMU_OPTION_netdev:
3329f07b 2201 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
a1ea458f
MM
2202 exit(1);
2203 }
2204 break;
7c9d8e07 2205 case QEMU_OPTION_net:
3329f07b 2206 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
c4b1fcc0
FB
2207 exit(1);
2208 }
702c651c 2209 break;
c7f74643
FB
2210#ifdef CONFIG_SLIRP
2211 case QEMU_OPTION_tftp:
ad196a9d 2212 legacy_tftp_prefix = optarg;
9bf05444 2213 break;
47d5d01a 2214 case QEMU_OPTION_bootp:
ad196a9d 2215 legacy_bootp_filename = optarg;
47d5d01a 2216 break;
9bf05444 2217 case QEMU_OPTION_redir:
0752706d
MA
2218 if (net_slirp_redir(optarg) < 0)
2219 exit(1);
9bf05444 2220 break;
c7f74643 2221#endif
dc72ac14 2222 case QEMU_OPTION_bt:
bd3c948d 2223 add_device_config(DEV_BT, optarg);
dc72ac14 2224 break;
1d14ffa9 2225 case QEMU_OPTION_audio_help:
ad96090a
BS
2226 if (!(audio_available())) {
2227 printf("Option %s not supported for this target\n", popt->name);
2228 exit(1);
2229 }
1d14ffa9
FB
2230 AUD_help ();
2231 exit (0);
2232 break;
2233 case QEMU_OPTION_soundhw:
ad96090a
BS
2234 if (!(audio_available())) {
2235 printf("Option %s not supported for this target\n", popt->name);
2236 exit(1);
2237 }
1d14ffa9
FB
2238 select_soundhw (optarg);
2239 break;
cd6f1169 2240 case QEMU_OPTION_h:
15f82208 2241 help(0);
cd6f1169 2242 break;
9bd7e6d9
PB
2243 case QEMU_OPTION_version:
2244 version();
2245 exit(0);
2246 break;
00f82b8a 2247 case QEMU_OPTION_m: {
9f9b17a4
JS
2248 ssize_t value;
2249
2250 value = strtosz(optarg, NULL);
2251 if (value < 0) {
00f82b8a 2252 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
2253 exit(1);
2254 }
00f82b8a
AJ
2255
2256 /* On 32-bit hosts, QEMU is limited by virtual address space */
4a1418e0 2257 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
00f82b8a
AJ
2258 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2259 exit(1);
2260 }
c227f099 2261 if (value != (uint64_t)(ram_addr_t)value) {
00f82b8a
AJ
2262 fprintf(stderr, "qemu: ram size too large\n");
2263 exit(1);
2264 }
2265 ram_size = value;
cd6f1169 2266 break;
00f82b8a 2267 }
c902760f
MT
2268 case QEMU_OPTION_mempath:
2269 mem_path = optarg;
2270 break;
2271#ifdef MAP_POPULATE
2272 case QEMU_OPTION_mem_prealloc:
2273 mem_prealloc = 1;
2274 break;
2275#endif
cd6f1169 2276 case QEMU_OPTION_d:
de06f8d1 2277 set_cpu_log(optarg);
cd6f1169 2278 break;
cd6f1169 2279 case QEMU_OPTION_s:
59030a8c 2280 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
cd6f1169 2281 break;
59030a8c
AL
2282 case QEMU_OPTION_gdb:
2283 gdbstub_dev = optarg;
cd6f1169 2284 break;
cd6f1169 2285 case QEMU_OPTION_L:
5cea8590 2286 data_dir = optarg;
cd6f1169 2287 break;
1192dad8
JM
2288 case QEMU_OPTION_bios:
2289 bios_name = optarg;
2290 break;
1b530a6d
AJ
2291 case QEMU_OPTION_singlestep:
2292 singlestep = 1;
2293 break;
cd6f1169 2294 case QEMU_OPTION_S:
3c07f8e8 2295 autostart = 0;
cd6f1169 2296 break;
3d11d0eb
FB
2297 case QEMU_OPTION_k:
2298 keyboard_layout = optarg;
2299 break;
ee22c2f7
FB
2300 case QEMU_OPTION_localtime:
2301 rtc_utc = 0;
2302 break;
3893c124 2303 case QEMU_OPTION_vga:
2304 select_vgahw (optarg);
1bfe856e 2305 break;
e9b137c2
FB
2306 case QEMU_OPTION_g:
2307 {
2308 const char *p;
2309 int w, h, depth;
2310 p = optarg;
2311 w = strtol(p, (char **)&p, 10);
2312 if (w <= 0) {
2313 graphic_error:
2314 fprintf(stderr, "qemu: invalid resolution or depth\n");
2315 exit(1);
2316 }
2317 if (*p != 'x')
2318 goto graphic_error;
2319 p++;
2320 h = strtol(p, (char **)&p, 10);
2321 if (h <= 0)
2322 goto graphic_error;
2323 if (*p == 'x') {
2324 p++;
2325 depth = strtol(p, (char **)&p, 10);
5fafdf24 2326 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
2327 depth != 24 && depth != 32)
2328 goto graphic_error;
2329 } else if (*p == '\0') {
2330 depth = graphic_depth;
2331 } else {
2332 goto graphic_error;
2333 }
3b46e624 2334
e9b137c2
FB
2335 graphic_width = w;
2336 graphic_height = h;
2337 graphic_depth = depth;
2338 }
2339 break;
20d8a3ed
TS
2340 case QEMU_OPTION_echr:
2341 {
2342 char *r;
2343 term_escape_char = strtol(optarg, &r, 0);
2344 if (r == optarg)
2345 printf("Bad argument to echr\n");
2346 break;
2347 }
82c643ff 2348 case QEMU_OPTION_monitor:
6ca5582d
GH
2349 monitor_parse(optarg, "readline");
2350 default_monitor = 0;
2351 break;
2352 case QEMU_OPTION_qmp:
2353 monitor_parse(optarg, "control");
2d114dc1 2354 default_monitor = 0;
82c643ff 2355 break;
22a0e04b 2356 case QEMU_OPTION_mon:
3329f07b 2357 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
22a0e04b 2358 if (!opts) {
22a0e04b
GH
2359 exit(1);
2360 }
2d114dc1 2361 default_monitor = 0;
22a0e04b 2362 break;
191bc01b 2363 case QEMU_OPTION_chardev:
3329f07b 2364 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
191bc01b 2365 if (!opts) {
191bc01b
GH
2366 exit(1);
2367 }
191bc01b 2368 break;
74db920c 2369 case QEMU_OPTION_fsdev:
03b0ba70
GH
2370 olist = qemu_find_opts("fsdev");
2371 if (!olist) {
2372 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2373 exit(1);
2374 }
2375 opts = qemu_opts_parse(olist, optarg, 1);
74db920c
GS
2376 if (!opts) {
2377 fprintf(stderr, "parse error: %s\n", optarg);
2378 exit(1);
2379 }
2380 break;
3d54abc7
GS
2381 case QEMU_OPTION_virtfs: {
2382 char *arg_fsdev = NULL;
2383 char *arg_9p = NULL;
2384 int len = 0;
2385
03b0ba70
GH
2386 olist = qemu_find_opts("virtfs");
2387 if (!olist) {
2388 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2389 exit(1);
2390 }
2391 opts = qemu_opts_parse(olist, optarg, 1);
3d54abc7
GS
2392 if (!opts) {
2393 fprintf(stderr, "parse error: %s\n", optarg);
2394 exit(1);
2395 }
2396
9ce56db6
VJJ
2397 if (qemu_opt_get(opts, "fstype") == NULL ||
2398 qemu_opt_get(opts, "mount_tag") == NULL ||
2399 qemu_opt_get(opts, "path") == NULL ||
2400 qemu_opt_get(opts, "security_model") == NULL) {
2401 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
12848bfc 2402 "security_model=[mapped|passthrough|none],"
9ce56db6
VJJ
2403 "mnt_tag=tag.\n");
2404 exit(1);
2405 }
2406
2407 len = strlen(",id=,path=,security_model=");
3d54abc7
GS
2408 len += strlen(qemu_opt_get(opts, "fstype"));
2409 len += strlen(qemu_opt_get(opts, "mount_tag"));
2410 len += strlen(qemu_opt_get(opts, "path"));
9ce56db6 2411 len += strlen(qemu_opt_get(opts, "security_model"));
3d54abc7
GS
2412 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2413
c93031e5 2414 snprintf(arg_fsdev, (len + 1) * sizeof(*arg_fsdev),
cb93bbdd
BS
2415 "%s,id=%s,path=%s,security_model=%s",
2416 qemu_opt_get(opts, "fstype"),
2417 qemu_opt_get(opts, "mount_tag"),
2418 qemu_opt_get(opts, "path"),
2419 qemu_opt_get(opts, "security_model"));
3d54abc7
GS
2420
2421 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2422 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2423 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2424
c93031e5 2425 snprintf(arg_9p, (len + 1) * sizeof(*arg_9p),
cb93bbdd
BS
2426 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2427 qemu_opt_get(opts, "mount_tag"),
2428 qemu_opt_get(opts, "mount_tag"));
3d54abc7 2429
3329f07b 2430 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev, 1)) {
3d54abc7
GS
2431 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2432 exit(1);
2433 }
2434
3329f07b 2435 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p, 1)) {
3d54abc7
GS
2436 fprintf(stderr, "parse error [device]: %s\n", optarg);
2437 exit(1);
2438 }
2439
2440 qemu_free(arg_fsdev);
2441 qemu_free(arg_9p);
2442 break;
2443 }
82c643ff 2444 case QEMU_OPTION_serial:
998bbd74
GH
2445 add_device_config(DEV_SERIAL, optarg);
2446 default_serial = 0;
18141ed6
JK
2447 if (strncmp(optarg, "mon:", 4) == 0) {
2448 default_monitor = 0;
2449 }
82c643ff 2450 break;
9dd986cc 2451 case QEMU_OPTION_watchdog:
09aaa160
MA
2452 if (watchdog) {
2453 fprintf(stderr,
2454 "qemu: only one watchdog option may be given\n");
2455 return 1;
2456 }
2457 watchdog = optarg;
9dd986cc
RJ
2458 break;
2459 case QEMU_OPTION_watchdog_action:
2460 if (select_watchdog_action(optarg) == -1) {
2461 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2462 exit(1);
2463 }
2464 break;
51ecf136 2465 case QEMU_OPTION_virtiocon:
aee1b935
GH
2466 add_device_config(DEV_VIRTCON, optarg);
2467 default_virtcon = 0;
18141ed6
JK
2468 if (strncmp(optarg, "mon:", 4) == 0) {
2469 default_monitor = 0;
2470 }
51ecf136 2471 break;
6508fe59 2472 case QEMU_OPTION_parallel:
6a5e8b0e
GH
2473 add_device_config(DEV_PARALLEL, optarg);
2474 default_parallel = 0;
18141ed6
JK
2475 if (strncmp(optarg, "mon:", 4) == 0) {
2476 default_monitor = 0;
2477 }
6508fe59 2478 break;
c9f398e5
PA
2479 case QEMU_OPTION_debugcon:
2480 add_device_config(DEV_DEBUGCON, optarg);
2481 break;
d63d307f
FB
2482 case QEMU_OPTION_loadvm:
2483 loadvm = optarg;
2484 break;
2485 case QEMU_OPTION_full_screen:
2486 full_screen = 1;
2487 break;
667accab 2488#ifdef CONFIG_SDL
43523e93
TS
2489 case QEMU_OPTION_no_frame:
2490 no_frame = 1;
2491 break;
3780e197
TS
2492 case QEMU_OPTION_alt_grab:
2493 alt_grab = 1;
2494 break;
0ca9f8a4
DK
2495 case QEMU_OPTION_ctrl_grab:
2496 ctrl_grab = 1;
2497 break;
667accab
TS
2498 case QEMU_OPTION_no_quit:
2499 no_quit = 1;
2500 break;
7d957bd8 2501 case QEMU_OPTION_sdl:
993fbfdb 2502 display_type = DT_SDL;
7d957bd8 2503 break;
667accab 2504#endif
f7cce898 2505 case QEMU_OPTION_pidfile:
93815bc2 2506 pid_file = optarg;
f7cce898 2507 break;
a09db21f
FB
2508 case QEMU_OPTION_win2k_hack:
2509 win2k_install_hack = 1;
2510 break;
73822ec8
AL
2511 case QEMU_OPTION_rtc_td_hack:
2512 rtc_td_hack = 1;
2513 break;
8a92ea2f 2514 case QEMU_OPTION_acpitable:
de06f8d1 2515 do_acpitable_option(optarg);
8a92ea2f 2516 break;
b6f6e3d3 2517 case QEMU_OPTION_smbios:
de06f8d1 2518 do_smbios_option(optarg);
b6f6e3d3 2519 break;
7ba1e619
AL
2520 case QEMU_OPTION_enable_kvm:
2521 kvm_allowed = 1;
7ba1e619 2522 break;
bb36d470
FB
2523 case QEMU_OPTION_usb:
2524 usb_enabled = 1;
2525 break;
a594cfbf
FB
2526 case QEMU_OPTION_usbdevice:
2527 usb_enabled = 1;
bd3c948d
GH
2528 add_device_config(DEV_USB, optarg);
2529 break;
2530 case QEMU_OPTION_device:
3329f07b 2531 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
f31d07d1
GH
2532 exit(1);
2533 }
a594cfbf 2534 break;
6a00d601 2535 case QEMU_OPTION_smp:
dc6b1c09 2536 smp_parse(optarg);
b2097003 2537 if (smp_cpus < 1) {
6a00d601
FB
2538 fprintf(stderr, "Invalid number of CPUs\n");
2539 exit(1);
2540 }
6be68d7e
JS
2541 if (max_cpus < smp_cpus) {
2542 fprintf(stderr, "maxcpus must be equal to or greater than "
2543 "smp\n");
2544 exit(1);
2545 }
2546 if (max_cpus > 255) {
2547 fprintf(stderr, "Unsupported number of maxcpus\n");
2548 exit(1);
2549 }
6a00d601 2550 break;
24236869 2551 case QEMU_OPTION_vnc:
6b62dc2d 2552 display_remote++;
73fc9742 2553 vnc_display = optarg;
24236869 2554 break;
6515b203
FB
2555 case QEMU_OPTION_no_acpi:
2556 acpi_enabled = 0;
2557 break;
16b29ae1
AL
2558 case QEMU_OPTION_no_hpet:
2559 no_hpet = 1;
2560 break;
7d4c3d53
MA
2561 case QEMU_OPTION_balloon:
2562 if (balloon_parse(optarg) < 0) {
2563 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2564 exit(1);
2565 }
df97b920 2566 break;
d1beab82
FB
2567 case QEMU_OPTION_no_reboot:
2568 no_reboot = 1;
2569 break;
b2f76161
AJ
2570 case QEMU_OPTION_no_shutdown:
2571 no_shutdown = 1;
2572 break;
9467cd46
AZ
2573 case QEMU_OPTION_show_cursor:
2574 cursor_hide = 0;
2575 break;
8fcb1b90
BS
2576 case QEMU_OPTION_uuid:
2577 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2578 fprintf(stderr, "Fail to parse UUID string."
2579 " Wrong format.\n");
2580 exit(1);
2581 }
2582 break;
9ae02555
TS
2583 case QEMU_OPTION_option_rom:
2584 if (nb_option_roms >= MAX_OPTION_ROMS) {
2585 fprintf(stderr, "Too many option ROMs\n");
2586 exit(1);
2587 }
2e55e842
GN
2588 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2589 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2590 option_rom[nb_option_roms].bootindex =
2591 qemu_opt_get_number(opts, "bootindex", -1);
2592 if (!option_rom[nb_option_roms].name) {
2593 fprintf(stderr, "Option ROM file is not specified\n");
2594 exit(1);
2595 }
9ae02555
TS
2596 nb_option_roms++;
2597 break;
8e71621f
PB
2598 case QEMU_OPTION_semihosting:
2599 semihosting_enabled = 1;
2600 break;
c35734b2 2601 case QEMU_OPTION_name:
1889465a
AK
2602 qemu_name = qemu_strdup(optarg);
2603 {
2604 char *p = strchr(qemu_name, ',');
2605 if (p != NULL) {
2606 *p++ = 0;
2607 if (strncmp(p, "process=", 8)) {
5697f6ae 2608 fprintf(stderr, "Unknown subargument %s to -name\n", p);
1889465a
AK
2609 exit(1);
2610 }
2611 p += 8;
ce798cf2 2612 os_set_proc_name(p);
1889465a
AK
2613 }
2614 }
c35734b2 2615 break;
66508601
BS
2616 case QEMU_OPTION_prom_env:
2617 if (nb_prom_envs >= MAX_PROM_ENVS) {
2618 fprintf(stderr, "Too many prom variables\n");
2619 exit(1);
2620 }
2621 prom_envs[nb_prom_envs] = optarg;
2622 nb_prom_envs++;
2623 break;
2b8f2d41
AZ
2624 case QEMU_OPTION_old_param:
2625 old_param = 1;
05ebd537 2626 break;
f3dcfada
TS
2627 case QEMU_OPTION_clock:
2628 configure_alarms(optarg);
2629 break;
7e0af5d0 2630 case QEMU_OPTION_startdate:
1ed2fc1f
JK
2631 configure_rtc_date_offset(optarg, 1);
2632 break;
2633 case QEMU_OPTION_rtc:
3329f07b 2634 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
1ed2fc1f 2635 if (!opts) {
1ed2fc1f 2636 exit(1);
7e0af5d0 2637 }
1ed2fc1f 2638 configure_rtc(opts);
7e0af5d0 2639 break;
26a5f13b
FB
2640 case QEMU_OPTION_tb_size:
2641 tb_size = strtol(optarg, NULL, 0);
2642 if (tb_size < 0)
2643 tb_size = 0;
2644 break;
2e70f6ef 2645 case QEMU_OPTION_icount:
4e3de9e9 2646 icount_option = optarg;
2e70f6ef 2647 break;
5bb7910a
AL
2648 case QEMU_OPTION_incoming:
2649 incoming = optarg;
8e84865e 2650 incoming_expected = true;
5bb7910a 2651 break;
d8c208dd
GH
2652 case QEMU_OPTION_nodefaults:
2653 default_serial = 0;
2654 default_parallel = 0;
aee1b935 2655 default_virtcon = 0;
d8c208dd
GH
2656 default_monitor = 0;
2657 default_vga = 0;
cb4522cc 2658 default_net = 0;
ac33f8fa
GH
2659 default_floppy = 0;
2660 default_cdrom = 0;
2661 default_sdcard = 0;
d8c208dd 2662 break;
e37630ca 2663 case QEMU_OPTION_xen_domid:
ad96090a
BS
2664 if (!(xen_available())) {
2665 printf("Option %s not supported for this target\n", popt->name);
2666 exit(1);
2667 }
e37630ca
AL
2668 xen_domid = atoi(optarg);
2669 break;
2670 case QEMU_OPTION_xen_create:
ad96090a
BS
2671 if (!(xen_available())) {
2672 printf("Option %s not supported for this target\n", popt->name);
2673 exit(1);
2674 }
e37630ca
AL
2675 xen_mode = XEN_CREATE;
2676 break;
2677 case QEMU_OPTION_xen_attach:
ad96090a
BS
2678 if (!(xen_available())) {
2679 printf("Option %s not supported for this target\n", popt->name);
2680 exit(1);
2681 }
e37630ca
AL
2682 xen_mode = XEN_ATTACH;
2683 break;
ab6540d5
PS
2684#ifdef CONFIG_SIMPLE_TRACE
2685 case QEMU_OPTION_trace:
2686 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2687 if (opts) {
2688 trace_file = qemu_opt_get(opts, "file");
2689 }
2690 break;
2691#endif
715a664a
GH
2692 case QEMU_OPTION_readconfig:
2693 {
dcfb0939
KW
2694 int ret = qemu_read_config_file(optarg);
2695 if (ret < 0) {
2696 fprintf(stderr, "read config %s: %s\n", optarg,
2697 strerror(-ret));
715a664a
GH
2698 exit(1);
2699 }
715a664a
GH
2700 break;
2701 }
29b0040b
GH
2702 case QEMU_OPTION_spice:
2703 olist = qemu_find_opts("spice");
2704 if (!olist) {
2705 fprintf(stderr, "spice is not supported by this qemu build.\n");
2706 exit(1);
2707 }
2708 opts = qemu_opts_parse(olist, optarg, 0);
2709 if (!opts) {
2710 fprintf(stderr, "parse error: %s\n", optarg);
2711 exit(1);
2712 }
2713 break;
715a664a
GH
2714 case QEMU_OPTION_writeconfig:
2715 {
2716 FILE *fp;
2717 if (strcmp(optarg, "-") == 0) {
2718 fp = stdout;
2719 } else {
2720 fp = fopen(optarg, "w");
2721 if (fp == NULL) {
2722 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2723 exit(1);
2724 }
2725 }
2726 qemu_config_write(fp);
2727 fclose(fp);
2728 break;
2729 }
59a5264b
JS
2730 default:
2731 os_parse_cmd_args(popt->index, optarg);
cd6f1169 2732 }
0824d6fc
FB
2733 }
2734 }
0f0bc3f1 2735 loc_set_none();
330d0414 2736
5cea8590
PB
2737 /* If no data_dir is specified then try to find it relative to the
2738 executable path. */
2739 if (!data_dir) {
6170540b 2740 data_dir = os_find_datadir(argv[0]);
5cea8590
PB
2741 }
2742 /* If all else fails use the install patch specified when building. */
2743 if (!data_dir) {
1dabe05c 2744 data_dir = CONFIG_QEMU_DATADIR;
5cea8590
PB
2745 }
2746
ab6540d5
PS
2747#ifdef CONFIG_SIMPLE_TRACE
2748 /*
2749 * Set the trace file name, if specified.
2750 */
2751 st_set_trace_file(trace_file);
2752#endif
6be68d7e
JS
2753 /*
2754 * Default to max_cpus = smp_cpus, in case the user doesn't
2755 * specify a max_cpus value.
2756 */
2757 if (!max_cpus)
2758 max_cpus = smp_cpus;
2759
3d878caa 2760 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
2761 if (smp_cpus > machine->max_cpus) {
2762 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2763 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2764 machine->max_cpus);
2765 exit(1);
2766 }
2767
3329f07b
GH
2768 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2769 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
998bbd74 2770
986c5f78
GH
2771 if (machine->no_serial) {
2772 default_serial = 0;
2773 }
2774 if (machine->no_parallel) {
2775 default_parallel = 0;
2776 }
2777 if (!machine->use_virtcon) {
2778 default_virtcon = 0;
2779 }
2780 if (machine->no_vga) {
2781 default_vga = 0;
2782 }
ac33f8fa
GH
2783 if (machine->no_floppy) {
2784 default_floppy = 0;
2785 }
2786 if (machine->no_cdrom) {
2787 default_cdrom = 0;
2788 }
2789 if (machine->no_sdcard) {
2790 default_sdcard = 0;
2791 }
986c5f78 2792
993fbfdb 2793 if (display_type == DT_NOGRAPHIC) {
6a5e8b0e
GH
2794 if (default_parallel)
2795 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
2796 if (default_serial && default_monitor) {
2797 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
2798 } else if (default_virtcon && default_monitor) {
2799 add_device_config(DEV_VIRTCON, "mon:stdio");
e1c09175
GH
2800 } else {
2801 if (default_serial)
2802 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
2803 if (default_virtcon)
2804 add_device_config(DEV_VIRTCON, "stdio");
e1c09175 2805 if (default_monitor)
6ca5582d 2806 monitor_parse("stdio", "readline");
e1c09175 2807 }
998bbd74
GH
2808 } else {
2809 if (default_serial)
2810 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
2811 if (default_parallel)
2812 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 2813 if (default_monitor)
6ca5582d 2814 monitor_parse("vc:80Cx24C", "readline");
38536da1
AG
2815 if (default_virtcon)
2816 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
bc0129d9 2817 }
64465297
GH
2818 if (default_vga)
2819 vga_interface_type = VGA_CIRRUS;
bc0129d9 2820
a5829fd9
T
2821 socket_init();
2822
3329f07b 2823 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
1a688d3b 2824 exit(1);
758e8e38 2825#ifdef CONFIG_VIRTFS
3329f07b 2826 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
74db920c
GS
2827 exit(1);
2828 }
2829#endif
1a688d3b 2830
eb505be1 2831 os_daemonize();
71e3ceb8 2832
aa26bb2d 2833 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
eb505be1 2834 os_pidfile_error();
93815bc2
TS
2835 exit(1);
2836 }
2837
98c8573e
PB
2838 if (kvm_allowed) {
2839 int ret = kvm_init(smp_cpus);
214910a7 2840 if (ret < 0) {
98c8573e
PB
2841 if (!kvm_available()) {
2842 printf("KVM not supported for this target\n");
2843 } else {
2844 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
2845 }
214910a7
MT
2846 exit(1);
2847 }
2848 }
2849
3fcf7b6b
AL
2850 if (qemu_init_main_loop()) {
2851 fprintf(stderr, "qemu_init_main_loop failed\n");
2852 exit(1);
2853 }
a20dd508 2854 linux_boot = (kernel_filename != NULL);
6c41b272 2855
f8d39c01
TS
2856 if (!linux_boot && *kernel_cmdline != '\0') {
2857 fprintf(stderr, "-append only allowed with -kernel option\n");
2858 exit(1);
2859 }
2860
2861 if (!linux_boot && initrd_filename != NULL) {
2862 fprintf(stderr, "-initrd only allowed with -kernel option\n");
2863 exit(1);
2864 }
2865
9156d763 2866 os_set_line_buffering();
3b46e624 2867
7183b4b4
AL
2868 if (init_timer_alarm() < 0) {
2869 fprintf(stderr, "could not initialize alarm timer\n");
2870 exit(1);
2871 }
4e3de9e9 2872 configure_icount(icount_option);
634fce96 2873
dc1c9fe8
MM
2874 if (net_init_clients() < 0) {
2875 exit(1);
702c651c 2876 }
f1510b2c 2877
dc72ac14 2878 /* init the bluetooth world */
bd3c948d
GH
2879 if (foreach_device_config(DEV_BT, bt_parse))
2880 exit(1);
dc72ac14 2881
0824d6fc 2882 /* init the memory */
94a6b54f
PB
2883 if (ram_size == 0)
2884 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
9ae02555 2885
26a5f13b
FB
2886 /* init the dynamic translator */
2887 cpu_exec_init_all(tb_size * 1024 * 1024);
2888
eb852011 2889 bdrv_init_with_whitelist();
c4b1fcc0 2890
c163b5ca 2891 blk_mig_init();
2892
ac33f8fa 2893 if (default_cdrom) {
aa40fc9c
GH
2894 /* we always create the cdrom drive, even if no disk is there */
2895 drive_add(NULL, CDROM_ALIAS);
ac33f8fa 2896 }
c4b1fcc0 2897
ac33f8fa 2898 if (default_floppy) {
aa40fc9c
GH
2899 /* we always create at least one floppy */
2900 drive_add(NULL, FD_ALIAS, 0);
ac33f8fa 2901 }
86f55663 2902
ac33f8fa 2903 if (default_sdcard) {
aa40fc9c
GH
2904 /* we always create one sd slot, even if no card is in it */
2905 drive_add(NULL, SD_ALIAS);
2906 }
9d413d1d 2907
e4bcb14c 2908 /* open the virtual block devices */
9dfd7c7a 2909 if (snapshot)
3329f07b
GH
2910 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
2911 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
9dfd7c7a 2912 exit(1);
3e3d5815 2913
97ab12d4 2914 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
c163b5ca 2915 ram_load, NULL);
8a7ddc38 2916
268a362c
AL
2917 if (nb_numa_nodes > 0) {
2918 int i;
2919
2920 if (nb_numa_nodes > smp_cpus) {
2921 nb_numa_nodes = smp_cpus;
2922 }
2923
2924 /* If no memory size if given for any node, assume the default case
2925 * and distribute the available memory equally across all nodes
2926 */
2927 for (i = 0; i < nb_numa_nodes; i++) {
2928 if (node_mem[i] != 0)
2929 break;
2930 }
2931 if (i == nb_numa_nodes) {
2932 uint64_t usedmem = 0;
2933
2934 /* On Linux, the each node's border has to be 8MB aligned,
2935 * the final node gets the rest.
2936 */
2937 for (i = 0; i < nb_numa_nodes - 1; i++) {
2938 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
2939 usedmem += node_mem[i];
2940 }
2941 node_mem[i] = ram_size - usedmem;
2942 }
2943
2944 for (i = 0; i < nb_numa_nodes; i++) {
2945 if (node_cpumask[i] != 0)
2946 break;
2947 }
2948 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2949 * must cope with this anyway, because there are BIOSes out there in
2950 * real machines which also use this scheme.
2951 */
2952 if (i == nb_numa_nodes) {
2953 for (i = 0; i < smp_cpus; i++) {
2954 node_cpumask[i % nb_numa_nodes] |= 1 << i;
2955 }
2956 }
2957 }
2958
3329f07b 2959 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
157b9319
JK
2960 exit(1);
2961 }
2962
998bbd74
GH
2963 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
2964 exit(1);
6a5e8b0e
GH
2965 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2966 exit(1);
aee1b935
GH
2967 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
2968 exit(1);
c9f398e5
PA
2969 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2970 exit(1);
2796dae0 2971
aae9460e
PB
2972 module_call_init(MODULE_INIT_DEVICE);
2973
3329f07b 2974 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
ff952ba2
MA
2975 exit(0);
2976
09aaa160
MA
2977 if (watchdog) {
2978 i = select_watchdog(watchdog);
2979 if (i > 0)
2980 exit (i == 1 ? 1 : 0);
2981 }
2982
b6b61144 2983 if (machine->compat_props) {
458fb679 2984 qdev_prop_register_global_list(machine->compat_props);
b6b61144 2985 }
d0fef6fb
GH
2986 qemu_add_globals();
2987
fbe1b595 2988 machine->init(ram_size, boot_devices,
3023f332
AL
2989 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2990
ea375f9a 2991 cpu_synchronize_all_post_init();
268a362c 2992
67b3b71d 2993 /* must be after terminal init, SDL library changes signal handlers */
8d963e6a 2994 os_setup_signal_handling();
67b3b71d 2995
87d0a28e 2996 set_numa_modes();
268a362c 2997
6f338c34
AL
2998 current_machine = machine;
2999
3023f332
AL
3000 /* init USB devices */
3001 if (usb_enabled) {
0752706d
MA
3002 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3003 exit(1);
3023f332
AL
3004 }
3005
bd3c948d 3006 /* init generic devices */
3329f07b 3007 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
bd3c948d
GH
3008 exit(1);
3009
668680f7
MA
3010 net_check_clients();
3011
3023f332 3012 /* just use the first displaystate for the moment */
b473df6e 3013 ds = get_displaystate();
993fbfdb 3014
a3e22260
GH
3015 if (using_spice)
3016 display_remote++;
6b62dc2d 3017 if (display_type == DT_DEFAULT && !display_remote) {
993fbfdb
AL
3018#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3019 display_type = DT_SDL;
3020#else
993fbfdb
AL
3021 vnc_display = "localhost:0,to=99";
3022 show_vnc_port = 1;
3023#endif
3024 }
3025
3026
6b62dc2d 3027 /* init local displays */
993fbfdb
AL
3028 switch (display_type) {
3029 case DT_NOGRAPHIC:
3030 break;
4d3b6f6e 3031#if defined(CONFIG_CURSES)
993fbfdb
AL
3032 case DT_CURSES:
3033 curses_display_init(ds, full_screen);
3034 break;
4d3b6f6e 3035#endif
5b0753e0 3036#if defined(CONFIG_SDL)
993fbfdb
AL
3037 case DT_SDL:
3038 sdl_display_init(ds, full_screen, no_frame);
3039 break;
5b0753e0 3040#elif defined(CONFIG_COCOA)
993fbfdb
AL
3041 case DT_SDL:
3042 cocoa_display_init(ds, full_screen);
3043 break;
313aa567 3044#endif
6b62dc2d
GH
3045 default:
3046 break;
3047 }
3048
3049 /* init remote displays */
3050 if (vnc_display) {
993fbfdb
AL
3051 vnc_display_init(ds);
3052 if (vnc_display_open(ds, vnc_display) < 0)
3053 exit(1);
f92f8afe 3054
993fbfdb
AL
3055 if (show_vnc_port) {
3056 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
f92f8afe 3057 }
313aa567 3058 }
a3e22260 3059#ifdef CONFIG_SPICE
a19cbfb3 3060 if (using_spice && !qxl_enabled) {
a3e22260
GH
3061 qemu_spice_display_init(ds);
3062 }
3063#endif
5b08fc10 3064
6b62dc2d
GH
3065 /* display setup */
3066 dpy_resize(ds);
3023f332
AL
3067 dcl = ds->listeners;
3068 while (dcl != NULL) {
3069 if (dcl->dpy_refresh != NULL) {
3070 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3071 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
0f2ad63f 3072 break;
20d8a3ed 3073 }
3023f332 3074 dcl = dcl->next;
20d8a3ed 3075 }
6b62dc2d 3076 if (ds->gui_timer == NULL) {
9043b62d
BS
3077 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3078 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3079 }
b473df6e 3080 text_consoles_set_display(ds);
2796dae0 3081
59030a8c
AL
3082 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3083 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3084 gdbstub_dev);
3085 exit(1);
45669e00 3086 }
45669e00 3087
3418bd25
GH
3088 qdev_machine_creation_done();
3089
15ff7705
GH
3090 if (rom_load_all() != 0) {
3091 fprintf(stderr, "rom loading failed\n");
3092 exit(1);
3093 }
45a50b16 3094
80376c3f
IY
3095 /* TODO: once all bus devices are qdevified, this should be done
3096 * when bus is created by qdev.c */
3097 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4cab946a
GN
3098 qemu_run_machine_init_done_notifiers();
3099
504c2948 3100 qemu_system_reset();
05f2401e 3101 if (loadvm) {
03cd4655 3102 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
3103 autostart = 0;
3104 }
3105 }
d63d307f 3106
2bb8c10c 3107 if (incoming) {
8ca5e801
JQ
3108 int ret = qemu_start_incoming_migration(incoming);
3109 if (ret < 0) {
3110 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3111 incoming, ret);
3112 exit(ret);
3113 }
6b99dadc 3114 } else if (autostart) {
c0f4ce77 3115 vm_start();
6b99dadc 3116 }
ffd843bc 3117
eb505be1 3118 os_setup_post();
71e3ceb8 3119
8a7ddc38 3120 main_loop();
40c3bac3 3121 quit_timers();
63a01ef8 3122 net_cleanup();
b46a8906 3123
0824d6fc
FB
3124 return 0;
3125}