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