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