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