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