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