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