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