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