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