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