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