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