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