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