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