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