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