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