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