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