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