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