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