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