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