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