]> git.proxmox.com Git - mirror_qemu.git/blame - vl.c
suspend: make acpi timer wakeup the guest.
[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;
95b363b5
GH
1286static int suspend_requested;
1287static bool is_suspended;
1288static NotifierList suspend_notifiers =
1289 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1290static NotifierList wakeup_notifiers =
1291 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1292static uint32_t wakeup_reason_mask = ~0;
0461d5a6 1293static RunState vmstop_requested = RUN_STATE_MAX;
bb0c6722 1294
1291eb35
AP
1295int qemu_shutdown_requested_get(void)
1296{
1297 return shutdown_requested;
1298}
1299
1300int qemu_reset_requested_get(void)
1301{
1302 return reset_requested;
1303}
1304
cf7a2fe2
AJ
1305int qemu_shutdown_requested(void)
1306{
1307 int r = shutdown_requested;
1308 shutdown_requested = 0;
1309 return r;
1310}
1311
f64622c4
GN
1312void qemu_kill_report(void)
1313{
1314 if (shutdown_signal != -1) {
f1d3fb04
PM
1315 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1316 if (shutdown_pid == 0) {
1317 /* This happens for eg ^C at the terminal, so it's worth
1318 * avoiding printing an odd message in that case.
1319 */
1320 fputc('\n', stderr);
1321 } else {
953ffe0f 1322 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
f1d3fb04 1323 }
f64622c4
GN
1324 shutdown_signal = -1;
1325 }
1326}
1327
cf7a2fe2
AJ
1328int qemu_reset_requested(void)
1329{
1330 int r = reset_requested;
1331 reset_requested = 0;
1332 return r;
1333}
1334
95b363b5
GH
1335static int qemu_suspend_requested(void)
1336{
1337 int r = suspend_requested;
1338 suspend_requested = 0;
1339 return r;
1340}
1341
cf7a2fe2
AJ
1342int qemu_powerdown_requested(void)
1343{
1344 int r = powerdown_requested;
1345 powerdown_requested = 0;
1346 return r;
1347}
1348
e568902a
AL
1349static int qemu_debug_requested(void)
1350{
1351 int r = debug_requested;
1352 debug_requested = 0;
1353 return r;
1354}
1355
0461d5a6 1356/* We use RUN_STATE_MAX but any invalid value will do */
0d45b702 1357static bool qemu_vmstop_requested(RunState *r)
6e29f5da 1358{
0461d5a6 1359 if (vmstop_requested < RUN_STATE_MAX) {
0d45b702 1360 *r = vmstop_requested;
0461d5a6 1361 vmstop_requested = RUN_STATE_MAX;
0d45b702
LC
1362 return true;
1363 }
1364
1365 return false;
6e29f5da
AL
1366}
1367
a08d4367 1368void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bb0c6722 1369{
7267c094 1370 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bb0c6722 1371
bb0c6722
FB
1372 re->func = func;
1373 re->opaque = opaque;
72cf2d4f 1374 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bb0c6722
FB
1375}
1376
dda9b29f 1377void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bb0c6722
FB
1378{
1379 QEMUResetEntry *re;
1380
72cf2d4f 1381 QTAILQ_FOREACH(re, &reset_handlers, entry) {
dda9b29f 1382 if (re->func == func && re->opaque == opaque) {
72cf2d4f 1383 QTAILQ_REMOVE(&reset_handlers, re, entry);
7267c094 1384 g_free(re);
dda9b29f
JK
1385 return;
1386 }
1387 }
1388}
1389
e063eb1f 1390void qemu_system_reset(bool report)
dda9b29f
JK
1391{
1392 QEMUResetEntry *re, *nre;
1393
1394 /* reset all devices */
72cf2d4f 1395 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bb0c6722
FB
1396 re->func(re->opaque);
1397 }
e063eb1f
JK
1398 if (report) {
1399 monitor_protocol_event(QEVENT_RESET, NULL);
1400 }
ea375f9a 1401 cpu_synchronize_all_post_reset();
bb0c6722
FB
1402}
1403
1404void qemu_system_reset_request(void)
1405{
d1beab82
FB
1406 if (no_reboot) {
1407 shutdown_requested = 1;
1408 } else {
1409 reset_requested = 1;
1410 }
b4a3d965 1411 cpu_stop_current();
d9f75a4e 1412 qemu_notify_event();
bb0c6722
FB
1413}
1414
95b363b5
GH
1415static void qemu_system_suspend(void)
1416{
1417 pause_all_vcpus();
1418 notifier_list_notify(&suspend_notifiers, NULL);
1419 is_suspended = true;
1420}
1421
1422void qemu_system_suspend_request(void)
1423{
1424 if (is_suspended) {
1425 return;
1426 }
1427 suspend_requested = 1;
1428 cpu_stop_current();
1429 qemu_notify_event();
1430}
1431
1432void qemu_register_suspend_notifier(Notifier *notifier)
1433{
1434 notifier_list_add(&suspend_notifiers, notifier);
1435}
1436
1437void qemu_system_wakeup_request(WakeupReason reason)
1438{
1439 if (!is_suspended) {
1440 return;
1441 }
1442 if (!(wakeup_reason_mask & (1 << reason))) {
1443 return;
1444 }
1445 notifier_list_notify(&wakeup_notifiers, &reason);
1446 reset_requested = 1;
1447 qemu_notify_event();
1448 is_suspended = false;
1449}
1450
1451void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1452{
1453 if (enabled) {
1454 wakeup_reason_mask |= (1 << reason);
1455 } else {
1456 wakeup_reason_mask &= ~(1 << reason);
1457 }
1458}
1459
1460void qemu_register_wakeup_notifier(Notifier *notifier)
1461{
1462 notifier_list_add(&wakeup_notifiers, notifier);
1463}
1464
f64622c4
GN
1465void qemu_system_killed(int signal, pid_t pid)
1466{
1467 shutdown_signal = signal;
1468 shutdown_pid = pid;
d9389b96 1469 no_shutdown = 0;
f64622c4
GN
1470 qemu_system_shutdown_request();
1471}
1472
bb0c6722
FB
1473void qemu_system_shutdown_request(void)
1474{
1475 shutdown_requested = 1;
d9f75a4e 1476 qemu_notify_event();
bb0c6722
FB
1477}
1478
3475187d
FB
1479void qemu_system_powerdown_request(void)
1480{
1481 powerdown_requested = 1;
d9f75a4e
AL
1482 qemu_notify_event();
1483}
1484
8cf71710
JK
1485void qemu_system_debug_request(void)
1486{
1487 debug_requested = 1;
83f338f7 1488 qemu_notify_event();
8cf71710
JK
1489}
1490
1dfb4dd9 1491void qemu_system_vmstop_request(RunState state)
8cf71710 1492{
1dfb4dd9 1493 vmstop_requested = state;
8cf71710
JK
1494 qemu_notify_event();
1495}
1496
d9c32310
BS
1497qemu_irq qemu_system_powerdown;
1498
99435906
PB
1499static bool main_loop_should_exit(void)
1500{
1501 RunState r;
1502 if (qemu_debug_requested()) {
1503 vm_stop(RUN_STATE_DEBUG);
1504 }
95b363b5
GH
1505 if (qemu_suspend_requested()) {
1506 qemu_system_suspend();
1507 }
99435906
PB
1508 if (qemu_shutdown_requested()) {
1509 qemu_kill_report();
1510 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1511 if (no_shutdown) {
1512 vm_stop(RUN_STATE_SHUTDOWN);
1513 } else {
1514 return true;
1515 }
1516 }
1517 if (qemu_reset_requested()) {
1518 pause_all_vcpus();
1519 cpu_synchronize_all_states();
1520 qemu_system_reset(VMRESET_REPORT);
1521 resume_all_vcpus();
1522 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1523 runstate_check(RUN_STATE_SHUTDOWN)) {
1524 runstate_set(RUN_STATE_PAUSED);
1525 }
1526 }
1527 if (qemu_powerdown_requested()) {
1528 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1529 qemu_irq_raise(qemu_system_powerdown);
1530 }
1531 if (qemu_vmstop_requested(&r)) {
1532 vm_stop(r);
1533 }
1534 return false;
1535}
1536
43b96858
AL
1537static void main_loop(void)
1538{
c9f711a5 1539 bool nonblocking;
99435906 1540 int last_io = 0;
8e1b90ec
JK
1541#ifdef CONFIG_PROFILER
1542 int64_t ti;
1543#endif
99435906 1544 do {
c9f711a5 1545 nonblocking = !kvm_enabled() && last_io > 0;
89bfc105 1546#ifdef CONFIG_PROFILER
46481d39 1547 ti = profile_getclock();
89bfc105 1548#endif
c9f711a5 1549 last_io = main_loop_wait(nonblocking);
89bfc105 1550#ifdef CONFIG_PROFILER
46481d39 1551 dev_time += profile_getclock() - ti;
89bfc105 1552#endif
99435906 1553 } while (!main_loop_should_exit());
b4608c04
FB
1554}
1555
9bd7e6d9
PB
1556static void version(void)
1557{
f75ca1ae 1558 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
9bd7e6d9
PB
1559}
1560
15f82208 1561static void help(int exitcode)
0824d6fc 1562{
a3adb7ad
ME
1563 version();
1564 printf("usage: %s [options] [disk_image]\n\n"
1565 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1566 error_get_progname());
1567
77bd1119
ME
1568#define QEMU_OPTIONS_GENERATE_HELP
1569#include "qemu-options-wrapper.h"
a3adb7ad
ME
1570
1571 printf("\nDuring emulation, the following keys are useful:\n"
3f020d70 1572 "ctrl-alt-f toggle full screen\n"
1573 "ctrl-alt-n switch to virtual console 'n'\n"
1574 "ctrl-alt toggle mouse and keyboard grab\n"
1575 "\n"
a3adb7ad
ME
1576 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1577
15f82208 1578 exit(exitcode);
0824d6fc
FB
1579}
1580
cd6f1169
FB
1581#define HAS_ARG 0x0001
1582
cd6f1169
FB
1583typedef struct QEMUOption {
1584 const char *name;
1585 int flags;
1586 int index;
ad96090a 1587 uint32_t arch_mask;
cd6f1169
FB
1588} QEMUOption;
1589
dbed7e40 1590static const QEMUOption qemu_options[] = {
ad96090a 1591 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
77bd1119
ME
1592#define QEMU_OPTIONS_GENERATE_OPTIONS
1593#include "qemu-options-wrapper.h"
cd6f1169 1594 { NULL },
fc01f7e7 1595};
a369da5f
BS
1596
1597static bool vga_available(void)
1598{
1599 return qdev_exists("VGA") || qdev_exists("isa-vga");
1600}
1601
1602static bool cirrus_vga_available(void)
1603{
1604 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1605}
1606
1607static bool vmware_vga_available(void)
1608{
1609 return qdev_exists("vmware-svga");
1610}
1611
3893c124 1612static void select_vgahw (const char *p)
1613{
1614 const char *opts;
1615
86176759 1616 vga_interface_type = VGA_NONE;
3893c124 1617 if (strstart(p, "std", &opts)) {
a369da5f
BS
1618 if (vga_available()) {
1619 vga_interface_type = VGA_STD;
1620 } else {
1621 fprintf(stderr, "Error: standard VGA not available\n");
1622 exit(0);
1623 }
3893c124 1624 } else if (strstart(p, "cirrus", &opts)) {
a369da5f
BS
1625 if (cirrus_vga_available()) {
1626 vga_interface_type = VGA_CIRRUS;
1627 } else {
1628 fprintf(stderr, "Error: Cirrus VGA not available\n");
1629 exit(0);
1630 }
3893c124 1631 } else if (strstart(p, "vmware", &opts)) {
a369da5f
BS
1632 if (vmware_vga_available()) {
1633 vga_interface_type = VGA_VMWARE;
1634 } else {
1635 fprintf(stderr, "Error: VMWare SVGA not available\n");
1636 exit(0);
1637 }
94909d9f 1638 } else if (strstart(p, "xenfb", &opts)) {
86176759 1639 vga_interface_type = VGA_XENFB;
a19cbfb3
GH
1640 } else if (strstart(p, "qxl", &opts)) {
1641 vga_interface_type = VGA_QXL;
28b85ed8 1642 } else if (!strstart(p, "none", &opts)) {
3893c124 1643 invalid_vga:
1644 fprintf(stderr, "Unknown vga type: %s\n", p);
1645 exit(1);
1646 }
cb5a7aa8 1647 while (*opts) {
1648 const char *nextopt;
1649
1650 if (strstart(opts, ",retrace=", &nextopt)) {
1651 opts = nextopt;
1652 if (strstart(opts, "dumb", &nextopt))
1653 vga_retrace_method = VGA_RETRACE_DUMB;
1654 else if (strstart(opts, "precise", &nextopt))
1655 vga_retrace_method = VGA_RETRACE_PRECISE;
1656 else goto invalid_vga;
1657 } else goto invalid_vga;
1658 opts = nextopt;
1659 }
3893c124 1660}
1661
1472a95b
JS
1662static DisplayType select_display(const char *p)
1663{
1664 const char *opts;
1665 DisplayType display = DT_DEFAULT;
1666
1667 if (strstart(p, "sdl", &opts)) {
1668#ifdef CONFIG_SDL
1669 display = DT_SDL;
1670 while (*opts) {
1671 const char *nextopt;
1672
1673 if (strstart(opts, ",frame=", &nextopt)) {
1674 opts = nextopt;
1675 if (strstart(opts, "on", &nextopt)) {
1676 no_frame = 0;
1677 } else if (strstart(opts, "off", &nextopt)) {
1678 no_frame = 1;
1679 } else {
05175535 1680 goto invalid_sdl_args;
1472a95b
JS
1681 }
1682 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1683 opts = nextopt;
1684 if (strstart(opts, "on", &nextopt)) {
1685 alt_grab = 1;
1686 } else if (strstart(opts, "off", &nextopt)) {
1687 alt_grab = 0;
1688 } else {
05175535 1689 goto invalid_sdl_args;
1472a95b
JS
1690 }
1691 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1692 opts = nextopt;
1693 if (strstart(opts, "on", &nextopt)) {
1694 ctrl_grab = 1;
1695 } else if (strstart(opts, "off", &nextopt)) {
1696 ctrl_grab = 0;
1697 } else {
05175535 1698 goto invalid_sdl_args;
1472a95b
JS
1699 }
1700 } else if (strstart(opts, ",window_close=", &nextopt)) {
1701 opts = nextopt;
1702 if (strstart(opts, "on", &nextopt)) {
1703 no_quit = 0;
1704 } else if (strstart(opts, "off", &nextopt)) {
1705 no_quit = 1;
1706 } else {
05175535 1707 goto invalid_sdl_args;
1472a95b
JS
1708 }
1709 } else {
05175535
PM
1710 invalid_sdl_args:
1711 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1712 exit(1);
1472a95b
JS
1713 }
1714 opts = nextopt;
1715 }
1716#else
1717 fprintf(stderr, "SDL support is disabled\n");
1718 exit(1);
1719#endif
3264ff12 1720 } else if (strstart(p, "vnc", &opts)) {
821601ea 1721#ifdef CONFIG_VNC
3264ff12
JS
1722 display_remote++;
1723
1724 if (*opts) {
1725 const char *nextopt;
1726
1727 if (strstart(opts, "=", &nextopt)) {
1728 vnc_display = nextopt;
1729 }
1730 }
1731 if (!vnc_display) {
1732 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1733 exit(1);
1734 }
821601ea
JS
1735#else
1736 fprintf(stderr, "VNC support is disabled\n");
1737 exit(1);
1738#endif
1472a95b
JS
1739 } else if (strstart(p, "curses", &opts)) {
1740#ifdef CONFIG_CURSES
1741 display = DT_CURSES;
1742#else
1743 fprintf(stderr, "Curses support is disabled\n");
1744 exit(1);
1745#endif
4171d32e
JS
1746 } else if (strstart(p, "none", &opts)) {
1747 display = DT_NONE;
1472a95b 1748 } else {
1472a95b
JS
1749 fprintf(stderr, "Unknown display type: %s\n", p);
1750 exit(1);
1751 }
1752
1753 return display;
1754}
1755
7d4c3d53
MA
1756static int balloon_parse(const char *arg)
1757{
382f0743 1758 QemuOpts *opts;
7d4c3d53 1759
382f0743
GH
1760 if (strcmp(arg, "none") == 0) {
1761 return 0;
1762 }
1763
1764 if (!strncmp(arg, "virtio", 6)) {
1765 if (arg[6] == ',') {
1766 /* have params -> parse them */
3329f07b 1767 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
382f0743
GH
1768 if (!opts)
1769 return -1;
1770 } else {
1771 /* create empty opts */
3329f07b 1772 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
7d4c3d53 1773 }
29f82b37 1774 qemu_opt_set(opts, "driver", "virtio-balloon");
382f0743 1775 return 0;
7d4c3d53 1776 }
382f0743
GH
1777
1778 return -1;
7d4c3d53 1779}
7d4c3d53 1780
5cea8590
PB
1781char *qemu_find_file(int type, const char *name)
1782{
1783 int len;
1784 const char *subdir;
1785 char *buf;
1786
1787 /* If name contains path separators then try it as a straight path. */
1788 if ((strchr(name, '/') || strchr(name, '\\'))
1789 && access(name, R_OK) == 0) {
7267c094 1790 return g_strdup(name);
5cea8590
PB
1791 }
1792 switch (type) {
1793 case QEMU_FILE_TYPE_BIOS:
1794 subdir = "";
1795 break;
1796 case QEMU_FILE_TYPE_KEYMAP:
1797 subdir = "keymaps/";
1798 break;
1799 default:
1800 abort();
1801 }
1802 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
7267c094 1803 buf = g_malloc0(len);
3a41759d 1804 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
5cea8590 1805 if (access(buf, R_OK)) {
7267c094 1806 g_free(buf);
5cea8590
PB
1807 return NULL;
1808 }
1809 return buf;
1810}
1811
ff952ba2
MA
1812static int device_help_func(QemuOpts *opts, void *opaque)
1813{
1814 return qdev_device_help(opts);
1815}
1816
f31d07d1
GH
1817static int device_init_func(QemuOpts *opts, void *opaque)
1818{
1819 DeviceState *dev;
1820
1821 dev = qdev_device_add(opts);
1822 if (!dev)
1823 return -1;
1824 return 0;
1825}
1826
1a688d3b
GH
1827static int chardev_init_func(QemuOpts *opts, void *opaque)
1828{
1829 CharDriverState *chr;
1830
f69554b9 1831 chr = qemu_chr_new_from_opts(opts, NULL);
1a688d3b
GH
1832 if (!chr)
1833 return -1;
1834 return 0;
1835}
1836
758e8e38 1837#ifdef CONFIG_VIRTFS
74db920c
GS
1838static int fsdev_init_func(QemuOpts *opts, void *opaque)
1839{
1840 int ret;
1841 ret = qemu_fsdev_add(opts);
1842
1843 return ret;
1844}
1845#endif
1846
88589343
GH
1847static int mon_init_func(QemuOpts *opts, void *opaque)
1848{
1849 CharDriverState *chr;
1850 const char *chardev;
1851 const char *mode;
1852 int flags;
1853
1854 mode = qemu_opt_get(opts, "mode");
1855 if (mode == NULL) {
1856 mode = "readline";
1857 }
1858 if (strcmp(mode, "readline") == 0) {
1859 flags = MONITOR_USE_READLINE;
1860 } else if (strcmp(mode, "control") == 0) {
1861 flags = MONITOR_USE_CONTROL;
1862 } else {
1863 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1864 exit(1);
1865 }
1866
39eaab9a
DB
1867 if (qemu_opt_get_bool(opts, "pretty", 0))
1868 flags |= MONITOR_USE_PRETTY;
1869
88589343
GH
1870 if (qemu_opt_get_bool(opts, "default", 0))
1871 flags |= MONITOR_IS_DEFAULT;
1872
1873 chardev = qemu_opt_get(opts, "chardev");
1874 chr = qemu_chr_find(chardev);
1875 if (chr == NULL) {
1876 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1877 exit(1);
1878 }
1879
1880 monitor_init(chr, flags);
1881 return 0;
1882}
1883
6ca5582d 1884static void monitor_parse(const char *optarg, const char *mode)
88589343
GH
1885{
1886 static int monitor_device_index = 0;
1887 QemuOpts *opts;
1888 const char *p;
1889 char label[32];
1890 int def = 0;
1891
1892 if (strstart(optarg, "chardev:", &p)) {
1893 snprintf(label, sizeof(label), "%s", p);
1894 } else {
140e065d
JK
1895 snprintf(label, sizeof(label), "compat_monitor%d",
1896 monitor_device_index);
1897 if (monitor_device_index == 0) {
88589343
GH
1898 def = 1;
1899 }
1900 opts = qemu_chr_parse_compat(label, optarg);
1901 if (!opts) {
1902 fprintf(stderr, "parse error: %s\n", optarg);
1903 exit(1);
1904 }
1905 }
1906
3329f07b 1907 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
88589343
GH
1908 if (!opts) {
1909 fprintf(stderr, "duplicate chardev: %s\n", label);
1910 exit(1);
1911 }
6ca5582d 1912 qemu_opt_set(opts, "mode", mode);
88589343
GH
1913 qemu_opt_set(opts, "chardev", label);
1914 if (def)
1915 qemu_opt_set(opts, "default", "on");
1916 monitor_device_index++;
1917}
1918
bd3c948d
GH
1919struct device_config {
1920 enum {
aee1b935
GH
1921 DEV_USB, /* -usbdevice */
1922 DEV_BT, /* -bt */
1923 DEV_SERIAL, /* -serial */
1924 DEV_PARALLEL, /* -parallel */
1925 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 1926 DEV_DEBUGCON, /* -debugcon */
ef0c4a0d 1927 DEV_GDB, /* -gdb, -s */
bd3c948d
GH
1928 } type;
1929 const char *cmdline;
d9a5954d 1930 Location loc;
72cf2d4f 1931 QTAILQ_ENTRY(device_config) next;
bd3c948d 1932};
72cf2d4f 1933QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
1934
1935static void add_device_config(int type, const char *cmdline)
1936{
1937 struct device_config *conf;
1938
7267c094 1939 conf = g_malloc0(sizeof(*conf));
bd3c948d
GH
1940 conf->type = type;
1941 conf->cmdline = cmdline;
d9a5954d 1942 loc_save(&conf->loc);
72cf2d4f 1943 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
1944}
1945
1946static int foreach_device_config(int type, int (*func)(const char *cmdline))
1947{
1948 struct device_config *conf;
1949 int rc;
1950
72cf2d4f 1951 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
1952 if (conf->type != type)
1953 continue;
d9a5954d 1954 loc_push_restore(&conf->loc);
bd3c948d 1955 rc = func(conf->cmdline);
d9a5954d 1956 loc_pop(&conf->loc);
bd3c948d
GH
1957 if (0 != rc)
1958 return rc;
1959 }
1960 return 0;
1961}
1962
998bbd74
GH
1963static int serial_parse(const char *devname)
1964{
1965 static int index = 0;
1966 char label[32];
1967
1968 if (strcmp(devname, "none") == 0)
1969 return 0;
1970 if (index == MAX_SERIAL_PORTS) {
1971 fprintf(stderr, "qemu: too many serial ports\n");
1972 exit(1);
1973 }
1974 snprintf(label, sizeof(label), "serial%d", index);
27143a44 1975 serial_hds[index] = qemu_chr_new(label, devname, NULL);
998bbd74
GH
1976 if (!serial_hds[index]) {
1977 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1978 devname, strerror(errno));
1979 return -1;
1980 }
1981 index++;
1982 return 0;
1983}
1984
6a5e8b0e
GH
1985static int parallel_parse(const char *devname)
1986{
1987 static int index = 0;
1988 char label[32];
1989
1990 if (strcmp(devname, "none") == 0)
1991 return 0;
1992 if (index == MAX_PARALLEL_PORTS) {
1993 fprintf(stderr, "qemu: too many parallel ports\n");
1994 exit(1);
1995 }
1996 snprintf(label, sizeof(label), "parallel%d", index);
27143a44 1997 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
6a5e8b0e
GH
1998 if (!parallel_hds[index]) {
1999 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2000 devname, strerror(errno));
2001 return -1;
2002 }
2003 index++;
2004 return 0;
2005}
2006
aee1b935
GH
2007static int virtcon_parse(const char *devname)
2008{
3329f07b 2009 QemuOptsList *device = qemu_find_opts("device");
aee1b935
GH
2010 static int index = 0;
2011 char label[32];
392ecf54 2012 QemuOpts *bus_opts, *dev_opts;
aee1b935
GH
2013
2014 if (strcmp(devname, "none") == 0)
2015 return 0;
2016 if (index == MAX_VIRTIO_CONSOLES) {
2017 fprintf(stderr, "qemu: too many virtio consoles\n");
2018 exit(1);
2019 }
392ecf54 2020
3329f07b 2021 bus_opts = qemu_opts_create(device, NULL, 0);
e87f7fc6
AL
2022 if (arch_type == QEMU_ARCH_S390X) {
2023 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2024 } else {
2025 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2026 }
392ecf54 2027
3329f07b 2028 dev_opts = qemu_opts_create(device, NULL, 0);
392ecf54
AS
2029 qemu_opt_set(dev_opts, "driver", "virtconsole");
2030
aee1b935 2031 snprintf(label, sizeof(label), "virtcon%d", index);
27143a44 2032 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
aee1b935
GH
2033 if (!virtcon_hds[index]) {
2034 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2035 devname, strerror(errno));
2036 return -1;
2037 }
392ecf54
AS
2038 qemu_opt_set(dev_opts, "chardev", label);
2039
aee1b935
GH
2040 index++;
2041 return 0;
2042}
2043
c9f398e5
PA
2044static int debugcon_parse(const char *devname)
2045{
2046 QemuOpts *opts;
2047
27143a44 2048 if (!qemu_chr_new("debugcon", devname, NULL)) {
c9f398e5
PA
2049 exit(1);
2050 }
3329f07b 2051 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
c9f398e5
PA
2052 if (!opts) {
2053 fprintf(stderr, "qemu: already have a debugcon device\n");
2054 exit(1);
2055 }
2056 qemu_opt_set(opts, "driver", "isa-debugcon");
2057 qemu_opt_set(opts, "chardev", "debugcon");
2058 return 0;
2059}
2060
9052ea6b
JK
2061static QEMUMachine *machine_parse(const char *name)
2062{
2063 QEMUMachine *m, *machine = NULL;
2064
2065 if (name) {
2066 machine = find_machine(name);
2067 }
2068 if (machine) {
2069 return machine;
2070 }
2071 printf("Supported machines are:\n");
2072 for (m = first_machine; m != NULL; m = m->next) {
2073 if (m->alias) {
3b26486a 2074 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
9052ea6b 2075 }
3b26486a 2076 printf("%-20s %s%s\n", m->name, m->desc,
9052ea6b
JK
2077 m->is_default ? " (default)" : "");
2078 }
2079 exit(!name || *name != '?');
2080}
2081
303d4e86
AP
2082static int tcg_init(void)
2083{
d5ab9713 2084 tcg_exec_init(tcg_tb_size * 1024 * 1024);
303d4e86
AP
2085 return 0;
2086}
2087
2088static struct {
2089 const char *opt_name;
2090 const char *name;
2091 int (*available)(void);
2092 int (*init)(void);
2093 int *allowed;
2094} accel_list[] = {
2095 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
3285cf4f 2096 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
303d4e86
AP
2097 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2098};
2099
2100static int configure_accelerator(void)
2101{
2102 const char *p = NULL;
2103 char buf[10];
2104 int i, ret;
1b785a97 2105 bool accel_initialised = 0;
303d4e86
AP
2106 bool init_failed = 0;
2107
2108 QemuOptsList *list = qemu_find_opts("machine");
2109 if (!QTAILQ_EMPTY(&list->head)) {
2110 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2111 }
2112
2113 if (p == NULL) {
2114 /* Use the default "accelerator", tcg */
2115 p = "tcg";
2116 }
2117
1b785a97 2118 while (!accel_initialised && *p != '\0') {
303d4e86
AP
2119 if (*p == ':') {
2120 p++;
2121 }
2122 p = get_opt_name(buf, sizeof (buf), p, ':');
2123 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2124 if (strcmp(accel_list[i].opt_name, buf) == 0) {
a16c53b1 2125 *(accel_list[i].allowed) = 1;
303d4e86
AP
2126 ret = accel_list[i].init();
2127 if (ret < 0) {
2128 init_failed = 1;
2129 if (!accel_list[i].available()) {
2130 printf("%s not supported for this target\n",
2131 accel_list[i].name);
2132 } else {
2133 fprintf(stderr, "failed to initialize %s: %s\n",
2134 accel_list[i].name,
2135 strerror(-ret));
2136 }
a16c53b1 2137 *(accel_list[i].allowed) = 0;
303d4e86 2138 } else {
1b785a97 2139 accel_initialised = 1;
303d4e86
AP
2140 }
2141 break;
2142 }
2143 }
2144 if (i == ARRAY_SIZE(accel_list)) {
2145 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2146 }
2147 }
2148
1b785a97 2149 if (!accel_initialised) {
303d4e86
AP
2150 fprintf(stderr, "No accelerator found!\n");
2151 exit(1);
2152 }
2153
2154 if (init_failed) {
2155 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2156 }
2157
1b785a97 2158 return !accel_initialised;
303d4e86
AP
2159}
2160
fd42deeb
GH
2161void qemu_add_exit_notifier(Notifier *notify)
2162{
2163 notifier_list_add(&exit_notifiers, notify);
2164}
2165
2166void qemu_remove_exit_notifier(Notifier *notify)
2167{
31552529 2168 notifier_remove(notify);
fd42deeb
GH
2169}
2170
2171static void qemu_run_exit_notifiers(void)
2172{
9e8dd451 2173 notifier_list_notify(&exit_notifiers, NULL);
fd42deeb
GH
2174}
2175
4cab946a
GN
2176void qemu_add_machine_init_done_notifier(Notifier *notify)
2177{
2178 notifier_list_add(&machine_init_done_notifiers, notify);
2179}
2180
2181static void qemu_run_machine_init_done_notifiers(void)
2182{
9e8dd451 2183 notifier_list_notify(&machine_init_done_notifiers, NULL);
4cab946a
GN
2184}
2185
6530a97b
AL
2186static const QEMUOption *lookup_opt(int argc, char **argv,
2187 const char **poptarg, int *poptind)
2188{
2189 const QEMUOption *popt;
2190 int optind = *poptind;
2191 char *r = argv[optind];
2192 const char *optarg;
2193
0f0bc3f1 2194 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
2195 optind++;
2196 /* Treat --foo the same as -foo. */
2197 if (r[1] == '-')
2198 r++;
2199 popt = qemu_options;
2200 for(;;) {
2201 if (!popt->name) {
0f0bc3f1 2202 error_report("invalid option");
6530a97b
AL
2203 exit(1);
2204 }
2205 if (!strcmp(popt->name, r + 1))
2206 break;
2207 popt++;
2208 }
2209 if (popt->flags & HAS_ARG) {
2210 if (optind >= argc) {
0f0bc3f1 2211 error_report("requires an argument");
6530a97b
AL
2212 exit(1);
2213 }
2214 optarg = argv[optind++];
0f0bc3f1 2215 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
2216 } else {
2217 optarg = NULL;
2218 }
2219
2220 *poptarg = optarg;
2221 *poptind = optind;
2222
2223 return popt;
2224}
2225
0750112a
AL
2226static gpointer malloc_and_trace(gsize n_bytes)
2227{
2228 void *ptr = malloc(n_bytes);
a74cd8cc 2229 trace_g_malloc(n_bytes, ptr);
0750112a
AL
2230 return ptr;
2231}
2232
2233static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2234{
2235 void *ptr = realloc(mem, n_bytes);
a74cd8cc 2236 trace_g_realloc(mem, n_bytes, ptr);
0750112a
AL
2237 return ptr;
2238}
2239
2240static void free_and_trace(gpointer mem)
2241{
a74cd8cc 2242 trace_g_free(mem);
0750112a
AL
2243 free(mem);
2244}
2245
d34e8f6e
MR
2246int qemu_init_main_loop(void)
2247{
2248 return main_loop_init();
2249}
2250
902b3d5c 2251int main(int argc, char **argv, char **envp)
0824d6fc 2252{
e4bcb14c 2253 int i;
da1fcfda 2254 int snapshot, linux_boot;
4e3de9e9 2255 const char *icount_option = NULL;
7f7f9873 2256 const char *initrd_filename;
a20dd508 2257 const char *kernel_filename, *kernel_cmdline;
195325a4 2258 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
3023f332 2259 DisplayState *ds;
7d957bd8 2260 DisplayChangeListener *dcl;
46d4767d 2261 int cyls, heads, secs, translation;
f31d07d1 2262 QemuOpts *hda_opts = NULL, *opts;
03b0ba70 2263 QemuOptsList *olist;
cd6f1169 2264 int optind;
6530a97b 2265 const char *optarg;
d63d307f 2266 const char *loadvm = NULL;
cc1daa40 2267 QEMUMachine *machine;
94fc95cd 2268 const char *cpu_model;
a369da5f 2269 const char *vga_model = NULL;
93815bc2 2270 const char *pid_file = NULL;
5bb7910a 2271 const char *incoming = NULL;
821601ea 2272#ifdef CONFIG_VNC
993fbfdb 2273 int show_vnc_port = 0;
821601ea 2274#endif
292444cb 2275 int defconfig = 1;
c235d738
MF
2276 const char *log_mask = NULL;
2277 const char *log_file = NULL;
0750112a
AL
2278 GMemVTable mem_trace = {
2279 .malloc = malloc_and_trace,
2280 .realloc = realloc_and_trace,
2281 .free = free_and_trace,
2282 };
23d15e86 2283 const char *trace_events = NULL;
e4858974 2284 const char *trace_file = NULL;
0b5538c3 2285
fd42deeb 2286 atexit(qemu_run_exit_notifiers);
65abca0a
MA
2287 error_set_progname(argv[0]);
2288
0750112a 2289 g_mem_set_vtable(&mem_trace);
db529aab 2290 if (!g_thread_supported()) {
42ed3727 2291#if !GLIB_CHECK_VERSION(2, 31, 0)
db529aab 2292 g_thread_init(NULL);
42ed3727
AL
2293#else
2294 fprintf(stderr, "glib threading failed to initialize.\n");
2295 exit(1);
2296#endif
db529aab 2297 }
0750112a 2298
5db9d4d1
LC
2299 runstate_init();
2300
6875204c 2301 init_clocks();
2ff68d07 2302 rtc_clock = host_clock;
6875204c 2303
902b3d5c 2304 qemu_cache_utils_init(envp);
2305
72cf2d4f 2306 QLIST_INIT (&vm_change_state_head);
fe98ac14 2307 os_setup_early_signal_handling();
be995c27 2308
f80f9ec9 2309 module_call_init(MODULE_INIT_MACHINE);
0c257437 2310 machine = find_default_machine();
94fc95cd 2311 cpu_model = NULL;
4fc5d071 2312 ram_size = 0;
33e3963e 2313 snapshot = 0;
c4b1fcc0 2314 cyls = heads = secs = 0;
46d4767d 2315 translation = BIOS_ATA_TRANSLATION_AUTO;
c4b1fcc0 2316
268a362c
AL
2317 for (i = 0; i < MAX_NODES; i++) {
2318 node_mem[i] = 0;
2319 node_cpumask[i] = 0;
2320 }
2321
268a362c 2322 nb_numa_nodes = 0;
7c9d8e07 2323 nb_nics = 0;
3b46e624 2324
41bd639b
BS
2325 autostart= 1;
2326
292444cb
AL
2327 /* first pass of option parsing */
2328 optind = 1;
2329 while (optind < argc) {
2330 if (argv[optind][0] != '-') {
2331 /* disk image */
28e68d68 2332 optind++;
292444cb
AL
2333 continue;
2334 } else {
2335 const QEMUOption *popt;
2336
2337 popt = lookup_opt(argc, argv, &optarg, &optind);
2338 switch (popt->index) {
2339 case QEMU_OPTION_nodefconfig:
2340 defconfig=0;
2341 break;
2342 }
2343 }
2344 }
2345
2346 if (defconfig) {
dcfb0939 2347 int ret;
cf5a65aa 2348
dcfb0939 2349 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
019e78ba 2350 if (ret < 0 && ret != -ENOENT) {
dcfb0939 2351 exit(1);
292444cb
AL
2352 }
2353
dcfb0939 2354 ret = qemu_read_config_file(arch_config_name);
019e78ba 2355 if (ret < 0 && ret != -ENOENT) {
dcfb0939 2356 exit(1);
292444cb
AL
2357 }
2358 }
de06f8d1 2359 cpudef_init();
292444cb
AL
2360
2361 /* second pass of option parsing */
cd6f1169 2362 optind = 1;
0824d6fc 2363 for(;;) {
cd6f1169 2364 if (optind >= argc)
0824d6fc 2365 break;
6530a97b 2366 if (argv[optind][0] != '-') {
2292ddae 2367 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
cd6f1169
FB
2368 } else {
2369 const QEMUOption *popt;
2370
6530a97b 2371 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a
BS
2372 if (!(popt->arch_mask & arch_type)) {
2373 printf("Option %s not supported for this target\n", popt->name);
2374 exit(1);
2375 }
cd6f1169 2376 switch(popt->index) {
cc1daa40 2377 case QEMU_OPTION_M:
9052ea6b 2378 machine = machine_parse(optarg);
cc1daa40 2379 break;
94fc95cd
JM
2380 case QEMU_OPTION_cpu:
2381 /* hw initialization will check this */
15f82208 2382 if (*optarg == '?') {
262353cb 2383 list_cpus(stdout, &fprintf, optarg);
15f82208 2384 exit(0);
94fc95cd
JM
2385 } else {
2386 cpu_model = optarg;
2387 }
2388 break;
cd6f1169 2389 case QEMU_OPTION_hda:
5645b0f4
MA
2390 {
2391 char buf[256];
2392 if (cyls == 0)
2393 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2394 else
2395 snprintf(buf, sizeof(buf),
2396 "%s,cyls=%d,heads=%d,secs=%d%s",
2397 HD_OPTS , cyls, heads, secs,
2398 translation == BIOS_ATA_TRANSLATION_LBA ?
e4bcb14c 2399 ",trans=lba" :
5645b0f4 2400 translation == BIOS_ATA_TRANSLATION_NONE ?
e4bcb14c 2401 ",trans=none" : "");
5645b0f4
MA
2402 drive_add(IF_DEFAULT, 0, optarg, buf);
2403 break;
2404 }
cd6f1169 2405 case QEMU_OPTION_hdb:
cc1daa40
FB
2406 case QEMU_OPTION_hdc:
2407 case QEMU_OPTION_hdd:
2292ddae
MA
2408 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2409 HD_OPTS);
fc01f7e7 2410 break;
e4bcb14c 2411 case QEMU_OPTION_drive:
e2982c3a
MT
2412 if (drive_def(optarg) == NULL) {
2413 exit(1);
2414 }
e4bcb14c 2415 break;
d058fe03
GH
2416 case QEMU_OPTION_set:
2417 if (qemu_set_option(optarg) != 0)
2418 exit(1);
2419 break;
d0fef6fb
GH
2420 case QEMU_OPTION_global:
2421 if (qemu_global_option(optarg) != 0)
2422 exit(1);
2423 break;
3e3d5815 2424 case QEMU_OPTION_mtdblock:
2292ddae 2425 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3e3d5815 2426 break;
a1bb27b1 2427 case QEMU_OPTION_sd:
2292ddae 2428 drive_add(IF_SD, 0, optarg, SD_OPTS);
a1bb27b1 2429 break;
86f55663 2430 case QEMU_OPTION_pflash:
2292ddae 2431 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
86f55663 2432 break;
cd6f1169 2433 case QEMU_OPTION_snapshot:
33e3963e
FB
2434 snapshot = 1;
2435 break;
cd6f1169 2436 case QEMU_OPTION_hdachs:
330d0414 2437 {
330d0414
FB
2438 const char *p;
2439 p = optarg;
2440 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
2441 if (cyls < 1 || cyls > 16383)
2442 goto chs_fail;
330d0414
FB
2443 if (*p != ',')
2444 goto chs_fail;
2445 p++;
2446 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
2447 if (heads < 1 || heads > 16)
2448 goto chs_fail;
330d0414
FB
2449 if (*p != ',')
2450 goto chs_fail;
2451 p++;
2452 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
2453 if (secs < 1 || secs > 63)
2454 goto chs_fail;
2455 if (*p == ',') {
2456 p++;
2457 if (!strcmp(p, "none"))
2458 translation = BIOS_ATA_TRANSLATION_NONE;
2459 else if (!strcmp(p, "lba"))
2460 translation = BIOS_ATA_TRANSLATION_LBA;
2461 else if (!strcmp(p, "auto"))
2462 translation = BIOS_ATA_TRANSLATION_AUTO;
2463 else
2464 goto chs_fail;
2465 } else if (*p != '\0') {
c4b1fcc0 2466 chs_fail:
46d4767d
FB
2467 fprintf(stderr, "qemu: invalid physical CHS format\n");
2468 exit(1);
c4b1fcc0 2469 }
9dfd7c7a
GH
2470 if (hda_opts != NULL) {
2471 char num[16];
2472 snprintf(num, sizeof(num), "%d", cyls);
2473 qemu_opt_set(hda_opts, "cyls", num);
2474 snprintf(num, sizeof(num), "%d", heads);
2475 qemu_opt_set(hda_opts, "heads", num);
2476 snprintf(num, sizeof(num), "%d", secs);
2477 qemu_opt_set(hda_opts, "secs", num);
2478 if (translation == BIOS_ATA_TRANSLATION_LBA)
2479 qemu_opt_set(hda_opts, "trans", "lba");
2480 if (translation == BIOS_ATA_TRANSLATION_NONE)
2481 qemu_opt_set(hda_opts, "trans", "none");
2482 }
330d0414
FB
2483 }
2484 break;
268a362c
AL
2485 case QEMU_OPTION_numa:
2486 if (nb_numa_nodes >= MAX_NODES) {
2487 fprintf(stderr, "qemu: too many NUMA nodes\n");
2488 exit(1);
2489 }
2490 numa_add(optarg);
2491 break;
1472a95b
JS
2492 case QEMU_OPTION_display:
2493 display_type = select_display(optarg);
2494 break;
cd6f1169 2495 case QEMU_OPTION_nographic:
993fbfdb 2496 display_type = DT_NOGRAPHIC;
a20dd508 2497 break;
4d3b6f6e 2498 case QEMU_OPTION_curses:
47b05369 2499#ifdef CONFIG_CURSES
993fbfdb 2500 display_type = DT_CURSES;
47b05369
JS
2501#else
2502 fprintf(stderr, "Curses support is disabled\n");
2503 exit(1);
4d3b6f6e 2504#endif
47b05369 2505 break;
a171fe39 2506 case QEMU_OPTION_portrait:
9312805d
VK
2507 graphic_rotate = 90;
2508 break;
2509 case QEMU_OPTION_rotate:
2510 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2511 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2512 graphic_rotate != 180 && graphic_rotate != 270) {
2513 fprintf(stderr,
2514 "qemu: only 90, 180, 270 deg rotation is available\n");
2515 exit(1);
2516 }
a171fe39 2517 break;
cd6f1169 2518 case QEMU_OPTION_kernel:
a0abe474
PM
2519 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
2520 break;
2521 case QEMU_OPTION_initrd:
2522 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
a20dd508 2523 break;
cd6f1169 2524 case QEMU_OPTION_append:
a0abe474 2525 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
313aa567 2526 break;
cd6f1169 2527 case QEMU_OPTION_cdrom:
2292ddae 2528 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
36b486bb 2529 break;
cd6f1169 2530 case QEMU_OPTION_boot:
28c5af54 2531 {
ef3adf68 2532 static const char * const params[] = {
3d3b8303
WX
2533 "order", "once", "menu",
2534 "splash", "splash-time", NULL
ef3adf68
JK
2535 };
2536 char buf[sizeof(boot_devices)];
e0f084bf 2537 char *standard_boot_devices;
ef3adf68
JK
2538 int legacy = 0;
2539
2540 if (!strchr(optarg, '=')) {
2541 legacy = 1;
2542 pstrcpy(buf, sizeof(buf), optarg);
2543 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2544 fprintf(stderr,
2545 "qemu: unknown boot parameter '%s' in '%s'\n",
2546 buf, optarg);
2547 exit(1);
2548 }
2549
2550 if (legacy ||
2551 get_param_value(buf, sizeof(buf), "order", optarg)) {
4e9e9d6e 2552 validate_bootdevices(buf);
ef3adf68 2553 pstrcpy(boot_devices, sizeof(boot_devices), buf);
28c5af54 2554 }
e0f084bf
JK
2555 if (!legacy) {
2556 if (get_param_value(buf, sizeof(buf),
2557 "once", optarg)) {
4e9e9d6e 2558 validate_bootdevices(buf);
7267c094 2559 standard_boot_devices = g_strdup(boot_devices);
e0f084bf
JK
2560 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2561 qemu_register_reset(restore_boot_devices,
2562 standard_boot_devices);
2563 }
95387491
JK
2564 if (get_param_value(buf, sizeof(buf),
2565 "menu", optarg)) {
2566 if (!strcmp(buf, "on")) {
2567 boot_menu = 1;
2568 } else if (!strcmp(buf, "off")) {
2569 boot_menu = 0;
2570 } else {
2571 fprintf(stderr,
2572 "qemu: invalid option value '%s'\n",
2573 buf);
2574 exit(1);
2575 }
2576 }
3d3b8303
WX
2577 qemu_opts_parse(qemu_find_opts("boot-opts"),
2578 optarg, 0);
e0f084bf 2579 }
36b486bb
FB
2580 }
2581 break;
cd6f1169 2582 case QEMU_OPTION_fda:
cd6f1169 2583 case QEMU_OPTION_fdb:
2292ddae
MA
2584 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2585 optarg, FD_OPTS);
c45886db 2586 break;
52ca8d6a
FB
2587 case QEMU_OPTION_no_fd_bootchk:
2588 fd_bootchk = 0;
2589 break;
a1ea458f 2590 case QEMU_OPTION_netdev:
3329f07b 2591 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
a1ea458f
MM
2592 exit(1);
2593 }
2594 break;
7c9d8e07 2595 case QEMU_OPTION_net:
3329f07b 2596 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
c4b1fcc0
FB
2597 exit(1);
2598 }
702c651c 2599 break;
f9dadc98
RS
2600#ifdef CONFIG_LIBISCSI
2601 case QEMU_OPTION_iscsi:
2602 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
2603 if (!opts) {
2604 exit(1);
2605 }
2606 break;
2607#endif
c7f74643
FB
2608#ifdef CONFIG_SLIRP
2609 case QEMU_OPTION_tftp:
ad196a9d 2610 legacy_tftp_prefix = optarg;
9bf05444 2611 break;
47d5d01a 2612 case QEMU_OPTION_bootp:
ad196a9d 2613 legacy_bootp_filename = optarg;
47d5d01a 2614 break;
9bf05444 2615 case QEMU_OPTION_redir:
0752706d
MA
2616 if (net_slirp_redir(optarg) < 0)
2617 exit(1);
9bf05444 2618 break;
c7f74643 2619#endif
dc72ac14 2620 case QEMU_OPTION_bt:
bd3c948d 2621 add_device_config(DEV_BT, optarg);
dc72ac14 2622 break;
1d14ffa9 2623 case QEMU_OPTION_audio_help:
ad96090a
BS
2624 if (!(audio_available())) {
2625 printf("Option %s not supported for this target\n", popt->name);
2626 exit(1);
2627 }
1d14ffa9
FB
2628 AUD_help ();
2629 exit (0);
2630 break;
2631 case QEMU_OPTION_soundhw:
ad96090a
BS
2632 if (!(audio_available())) {
2633 printf("Option %s not supported for this target\n", popt->name);
2634 exit(1);
2635 }
1d14ffa9
FB
2636 select_soundhw (optarg);
2637 break;
cd6f1169 2638 case QEMU_OPTION_h:
15f82208 2639 help(0);
cd6f1169 2640 break;
9bd7e6d9
PB
2641 case QEMU_OPTION_version:
2642 version();
2643 exit(0);
2644 break;
00f82b8a 2645 case QEMU_OPTION_m: {
70b4f4bb 2646 int64_t value;
961b42b9 2647 char *end;
9f9b17a4 2648
961b42b9
MA
2649 value = strtosz(optarg, &end);
2650 if (value < 0 || *end) {
00f82b8a 2651 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
2652 exit(1);
2653 }
00f82b8a 2654
c227f099 2655 if (value != (uint64_t)(ram_addr_t)value) {
00f82b8a
AJ
2656 fprintf(stderr, "qemu: ram size too large\n");
2657 exit(1);
2658 }
2659 ram_size = value;
cd6f1169 2660 break;
00f82b8a 2661 }
c902760f
MT
2662 case QEMU_OPTION_mempath:
2663 mem_path = optarg;
2664 break;
2665#ifdef MAP_POPULATE
2666 case QEMU_OPTION_mem_prealloc:
2667 mem_prealloc = 1;
2668 break;
2669#endif
cd6f1169 2670 case QEMU_OPTION_d:
c235d738
MF
2671 log_mask = optarg;
2672 break;
2673 case QEMU_OPTION_D:
2674 log_file = optarg;
cd6f1169 2675 break;
cd6f1169 2676 case QEMU_OPTION_s:
ef0c4a0d 2677 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
cd6f1169 2678 break;
59030a8c 2679 case QEMU_OPTION_gdb:
ef0c4a0d 2680 add_device_config(DEV_GDB, optarg);
cd6f1169 2681 break;
cd6f1169 2682 case QEMU_OPTION_L:
5cea8590 2683 data_dir = optarg;
cd6f1169 2684 break;
1192dad8
JM
2685 case QEMU_OPTION_bios:
2686 bios_name = optarg;
2687 break;
1b530a6d
AJ
2688 case QEMU_OPTION_singlestep:
2689 singlestep = 1;
2690 break;
cd6f1169 2691 case QEMU_OPTION_S:
3c07f8e8 2692 autostart = 0;
cd6f1169 2693 break;
3d11d0eb
FB
2694 case QEMU_OPTION_k:
2695 keyboard_layout = optarg;
2696 break;
ee22c2f7
FB
2697 case QEMU_OPTION_localtime:
2698 rtc_utc = 0;
2699 break;
3893c124 2700 case QEMU_OPTION_vga:
a369da5f 2701 vga_model = optarg;
1bfe856e 2702 break;
e9b137c2
FB
2703 case QEMU_OPTION_g:
2704 {
2705 const char *p;
2706 int w, h, depth;
2707 p = optarg;
2708 w = strtol(p, (char **)&p, 10);
2709 if (w <= 0) {
2710 graphic_error:
2711 fprintf(stderr, "qemu: invalid resolution or depth\n");
2712 exit(1);
2713 }
2714 if (*p != 'x')
2715 goto graphic_error;
2716 p++;
2717 h = strtol(p, (char **)&p, 10);
2718 if (h <= 0)
2719 goto graphic_error;
2720 if (*p == 'x') {
2721 p++;
2722 depth = strtol(p, (char **)&p, 10);
5fafdf24 2723 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
2724 depth != 24 && depth != 32)
2725 goto graphic_error;
2726 } else if (*p == '\0') {
2727 depth = graphic_depth;
2728 } else {
2729 goto graphic_error;
2730 }
3b46e624 2731
e9b137c2
FB
2732 graphic_width = w;
2733 graphic_height = h;
2734 graphic_depth = depth;
2735 }
2736 break;
20d8a3ed
TS
2737 case QEMU_OPTION_echr:
2738 {
2739 char *r;
2740 term_escape_char = strtol(optarg, &r, 0);
2741 if (r == optarg)
2742 printf("Bad argument to echr\n");
2743 break;
2744 }
82c643ff 2745 case QEMU_OPTION_monitor:
6ca5582d
GH
2746 monitor_parse(optarg, "readline");
2747 default_monitor = 0;
2748 break;
2749 case QEMU_OPTION_qmp:
2750 monitor_parse(optarg, "control");
2d114dc1 2751 default_monitor = 0;
82c643ff 2752 break;
22a0e04b 2753 case QEMU_OPTION_mon:
3329f07b 2754 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
22a0e04b 2755 if (!opts) {
22a0e04b
GH
2756 exit(1);
2757 }
2d114dc1 2758 default_monitor = 0;
22a0e04b 2759 break;
191bc01b 2760 case QEMU_OPTION_chardev:
3329f07b 2761 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
191bc01b 2762 if (!opts) {
191bc01b
GH
2763 exit(1);
2764 }
191bc01b 2765 break;
74db920c 2766 case QEMU_OPTION_fsdev:
03b0ba70
GH
2767 olist = qemu_find_opts("fsdev");
2768 if (!olist) {
2769 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2770 exit(1);
2771 }
2772 opts = qemu_opts_parse(olist, optarg, 1);
74db920c
GS
2773 if (!opts) {
2774 fprintf(stderr, "parse error: %s\n", optarg);
2775 exit(1);
2776 }
2777 break;
3d54abc7 2778 case QEMU_OPTION_virtfs: {
e14ea479
SH
2779 QemuOpts *fsdev;
2780 QemuOpts *device;
84a87cc4 2781 const char *writeout, *sock_fd, *socket;
3d54abc7 2782
03b0ba70
GH
2783 olist = qemu_find_opts("virtfs");
2784 if (!olist) {
2785 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2786 exit(1);
2787 }
2788 opts = qemu_opts_parse(olist, optarg, 1);
3d54abc7
GS
2789 if (!opts) {
2790 fprintf(stderr, "parse error: %s\n", optarg);
2791 exit(1);
2792 }
2793
fbcbf101 2794 if (qemu_opt_get(opts, "fsdriver") == NULL ||
99519f0a
AK
2795 qemu_opt_get(opts, "mount_tag") == NULL) {
2796 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
9ce56db6
VJ
2797 exit(1);
2798 }
e14ea479
SH
2799 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2800 qemu_opt_get(opts, "mount_tag"), 1);
2801 if (!fsdev) {
2802 fprintf(stderr, "duplicate fsdev id: %s\n",
2803 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2804 exit(1);
2805 }
d3ab98e6
AK
2806
2807 writeout = qemu_opt_get(opts, "writeout");
2808 if (writeout) {
2809#ifdef CONFIG_SYNC_FILE_RANGE
2810 qemu_opt_set(fsdev, "writeout", writeout);
2811#else
2812 fprintf(stderr, "writeout=immediate not supported on "
2813 "this platform\n");
2814 exit(1);
2815#endif
2816 }
fbcbf101 2817 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
e14ea479
SH
2818 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2819 qemu_opt_set(fsdev, "security_model",
2820 qemu_opt_get(opts, "security_model"));
84a87cc4
MK
2821 socket = qemu_opt_get(opts, "socket");
2822 if (socket) {
2823 qemu_opt_set(fsdev, "socket", socket);
2824 }
4c793dda
MK
2825 sock_fd = qemu_opt_get(opts, "sock_fd");
2826 if (sock_fd) {
2827 qemu_opt_set(fsdev, "sock_fd", sock_fd);
2828 }
e14ea479 2829
2c74c2cb
MK
2830 qemu_opt_set_bool(fsdev, "readonly",
2831 qemu_opt_get_bool(opts, "readonly", 0));
e14ea479
SH
2832 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2833 qemu_opt_set(device, "driver", "virtio-9p-pci");
2834 qemu_opt_set(device, "fsdev",
2835 qemu_opt_get(opts, "mount_tag"));
2836 qemu_opt_set(device, "mount_tag",
2837 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
2838 break;
2839 }
9db221ae
AK
2840 case QEMU_OPTION_virtfs_synth: {
2841 QemuOpts *fsdev;
2842 QemuOpts *device;
2843
2844 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2845 if (!fsdev) {
2846 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
2847 exit(1);
2848 }
2849 qemu_opt_set(fsdev, "fsdriver", "synth");
9db221ae
AK
2850
2851 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2852 qemu_opt_set(device, "driver", "virtio-9p-pci");
2853 qemu_opt_set(device, "fsdev", "v_synth");
2854 qemu_opt_set(device, "mount_tag", "v_synth");
2855 break;
2856 }
82c643ff 2857 case QEMU_OPTION_serial:
998bbd74
GH
2858 add_device_config(DEV_SERIAL, optarg);
2859 default_serial = 0;
18141ed6
JK
2860 if (strncmp(optarg, "mon:", 4) == 0) {
2861 default_monitor = 0;
2862 }
82c643ff 2863 break;
9dd986cc 2864 case QEMU_OPTION_watchdog:
09aaa160
MA
2865 if (watchdog) {
2866 fprintf(stderr,
2867 "qemu: only one watchdog option may be given\n");
2868 return 1;
2869 }
2870 watchdog = optarg;
9dd986cc
RJ
2871 break;
2872 case QEMU_OPTION_watchdog_action:
2873 if (select_watchdog_action(optarg) == -1) {
2874 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2875 exit(1);
2876 }
2877 break;
51ecf136 2878 case QEMU_OPTION_virtiocon:
aee1b935
GH
2879 add_device_config(DEV_VIRTCON, optarg);
2880 default_virtcon = 0;
18141ed6
JK
2881 if (strncmp(optarg, "mon:", 4) == 0) {
2882 default_monitor = 0;
2883 }
51ecf136 2884 break;
6508fe59 2885 case QEMU_OPTION_parallel:
6a5e8b0e
GH
2886 add_device_config(DEV_PARALLEL, optarg);
2887 default_parallel = 0;
18141ed6
JK
2888 if (strncmp(optarg, "mon:", 4) == 0) {
2889 default_monitor = 0;
2890 }
6508fe59 2891 break;
c9f398e5
PA
2892 case QEMU_OPTION_debugcon:
2893 add_device_config(DEV_DEBUGCON, optarg);
2894 break;
d63d307f
FB
2895 case QEMU_OPTION_loadvm:
2896 loadvm = optarg;
2897 break;
2898 case QEMU_OPTION_full_screen:
2899 full_screen = 1;
2900 break;
667accab 2901#ifdef CONFIG_SDL
43523e93
TS
2902 case QEMU_OPTION_no_frame:
2903 no_frame = 1;
2904 break;
3780e197
TS
2905 case QEMU_OPTION_alt_grab:
2906 alt_grab = 1;
2907 break;
0ca9f8a4
DK
2908 case QEMU_OPTION_ctrl_grab:
2909 ctrl_grab = 1;
2910 break;
667accab
TS
2911 case QEMU_OPTION_no_quit:
2912 no_quit = 1;
2913 break;
7d957bd8 2914 case QEMU_OPTION_sdl:
993fbfdb 2915 display_type = DT_SDL;
7d957bd8 2916 break;
58fc096c
JS
2917#else
2918 case QEMU_OPTION_no_frame:
2919 case QEMU_OPTION_alt_grab:
2920 case QEMU_OPTION_ctrl_grab:
2921 case QEMU_OPTION_no_quit:
2922 case QEMU_OPTION_sdl:
2923 fprintf(stderr, "SDL support is disabled\n");
2924 exit(1);
667accab 2925#endif
f7cce898 2926 case QEMU_OPTION_pidfile:
93815bc2 2927 pid_file = optarg;
f7cce898 2928 break;
a09db21f
FB
2929 case QEMU_OPTION_win2k_hack:
2930 win2k_install_hack = 1;
2931 break;
433acf0d
JK
2932 case QEMU_OPTION_rtc_td_hack: {
2933 static GlobalProperty slew_lost_ticks[] = {
2934 {
2935 .driver = "mc146818rtc",
2936 .property = "lost_tick_policy",
2937 .value = "slew",
2938 },
2939 { /* end of list */ }
2940 };
2941
2942 qdev_prop_register_global_list(slew_lost_ticks);
73822ec8 2943 break;
433acf0d 2944 }
8a92ea2f 2945 case QEMU_OPTION_acpitable:
de06f8d1 2946 do_acpitable_option(optarg);
8a92ea2f 2947 break;
b6f6e3d3 2948 case QEMU_OPTION_smbios:
de06f8d1 2949 do_smbios_option(optarg);
b6f6e3d3 2950 break;
7ba1e619 2951 case QEMU_OPTION_enable_kvm:
303d4e86 2952 olist = qemu_find_opts("machine");
303d4e86
AP
2953 qemu_opts_parse(olist, "accel=kvm", 0);
2954 break;
2955 case QEMU_OPTION_machine:
2956 olist = qemu_find_opts("machine");
9052ea6b 2957 opts = qemu_opts_parse(olist, optarg, 1);
303d4e86
AP
2958 if (!opts) {
2959 fprintf(stderr, "parse error: %s\n", optarg);
2960 exit(1);
2961 }
2645c6dc
JK
2962 optarg = qemu_opt_get(opts, "type");
2963 if (optarg) {
2964 machine = machine_parse(optarg);
2965 }
7ba1e619 2966 break;
bb36d470
FB
2967 case QEMU_OPTION_usb:
2968 usb_enabled = 1;
2969 break;
a594cfbf
FB
2970 case QEMU_OPTION_usbdevice:
2971 usb_enabled = 1;
bd3c948d
GH
2972 add_device_config(DEV_USB, optarg);
2973 break;
2974 case QEMU_OPTION_device:
3329f07b 2975 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
f31d07d1
GH
2976 exit(1);
2977 }
a594cfbf 2978 break;
6a00d601 2979 case QEMU_OPTION_smp:
dc6b1c09 2980 smp_parse(optarg);
b2097003 2981 if (smp_cpus < 1) {
6a00d601
FB
2982 fprintf(stderr, "Invalid number of CPUs\n");
2983 exit(1);
2984 }
6be68d7e
JS
2985 if (max_cpus < smp_cpus) {
2986 fprintf(stderr, "maxcpus must be equal to or greater than "
2987 "smp\n");
2988 exit(1);
2989 }
2990 if (max_cpus > 255) {
2991 fprintf(stderr, "Unsupported number of maxcpus\n");
2992 exit(1);
2993 }
6a00d601 2994 break;
24236869 2995 case QEMU_OPTION_vnc:
821601ea 2996#ifdef CONFIG_VNC
6b62dc2d 2997 display_remote++;
821601ea
JS
2998 vnc_display = optarg;
2999#else
3000 fprintf(stderr, "VNC support is disabled\n");
3001 exit(1);
3002#endif
3003 break;
6515b203
FB
3004 case QEMU_OPTION_no_acpi:
3005 acpi_enabled = 0;
3006 break;
16b29ae1
AL
3007 case QEMU_OPTION_no_hpet:
3008 no_hpet = 1;
3009 break;
7d4c3d53
MA
3010 case QEMU_OPTION_balloon:
3011 if (balloon_parse(optarg) < 0) {
3012 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3013 exit(1);
3014 }
df97b920 3015 break;
d1beab82
FB
3016 case QEMU_OPTION_no_reboot:
3017 no_reboot = 1;
3018 break;
b2f76161
AJ
3019 case QEMU_OPTION_no_shutdown:
3020 no_shutdown = 1;
3021 break;
9467cd46
AZ
3022 case QEMU_OPTION_show_cursor:
3023 cursor_hide = 0;
3024 break;
8fcb1b90
BS
3025 case QEMU_OPTION_uuid:
3026 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3027 fprintf(stderr, "Fail to parse UUID string."
3028 " Wrong format.\n");
3029 exit(1);
3030 }
3031 break;
9ae02555
TS
3032 case QEMU_OPTION_option_rom:
3033 if (nb_option_roms >= MAX_OPTION_ROMS) {
3034 fprintf(stderr, "Too many option ROMs\n");
3035 exit(1);
3036 }
2e55e842
GN
3037 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3038 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3039 option_rom[nb_option_roms].bootindex =
3040 qemu_opt_get_number(opts, "bootindex", -1);
3041 if (!option_rom[nb_option_roms].name) {
3042 fprintf(stderr, "Option ROM file is not specified\n");
3043 exit(1);
3044 }
9ae02555
TS
3045 nb_option_roms++;
3046 break;
8e71621f
PB
3047 case QEMU_OPTION_semihosting:
3048 semihosting_enabled = 1;
3049 break;
c35734b2 3050 case QEMU_OPTION_name:
7267c094 3051 qemu_name = g_strdup(optarg);
1889465a
AK
3052 {
3053 char *p = strchr(qemu_name, ',');
3054 if (p != NULL) {
3055 *p++ = 0;
3056 if (strncmp(p, "process=", 8)) {
5697f6ae 3057 fprintf(stderr, "Unknown subargument %s to -name\n", p);
1889465a
AK
3058 exit(1);
3059 }
3060 p += 8;
ce798cf2 3061 os_set_proc_name(p);
1889465a
AK
3062 }
3063 }
c35734b2 3064 break;
66508601
BS
3065 case QEMU_OPTION_prom_env:
3066 if (nb_prom_envs >= MAX_PROM_ENVS) {
3067 fprintf(stderr, "Too many prom variables\n");
3068 exit(1);
3069 }
3070 prom_envs[nb_prom_envs] = optarg;
3071 nb_prom_envs++;
3072 break;
2b8f2d41
AZ
3073 case QEMU_OPTION_old_param:
3074 old_param = 1;
05ebd537 3075 break;
f3dcfada
TS
3076 case QEMU_OPTION_clock:
3077 configure_alarms(optarg);
3078 break;
7e0af5d0 3079 case QEMU_OPTION_startdate:
1ed2fc1f
JK
3080 configure_rtc_date_offset(optarg, 1);
3081 break;
3082 case QEMU_OPTION_rtc:
3329f07b 3083 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
1ed2fc1f 3084 if (!opts) {
1ed2fc1f 3085 exit(1);
7e0af5d0 3086 }
1ed2fc1f 3087 configure_rtc(opts);
7e0af5d0 3088 break;
26a5f13b 3089 case QEMU_OPTION_tb_size:
d5ab9713
JK
3090 tcg_tb_size = strtol(optarg, NULL, 0);
3091 if (tcg_tb_size < 0) {
3092 tcg_tb_size = 0;
3093 }
26a5f13b 3094 break;
2e70f6ef 3095 case QEMU_OPTION_icount:
4e3de9e9 3096 icount_option = optarg;
2e70f6ef 3097 break;
5bb7910a
AL
3098 case QEMU_OPTION_incoming:
3099 incoming = optarg;
3100 break;
d8c208dd
GH
3101 case QEMU_OPTION_nodefaults:
3102 default_serial = 0;
3103 default_parallel = 0;
aee1b935 3104 default_virtcon = 0;
d8c208dd 3105 default_monitor = 0;
cb4522cc 3106 default_net = 0;
ac33f8fa
GH
3107 default_floppy = 0;
3108 default_cdrom = 0;
3109 default_sdcard = 0;
c5bd4f3d 3110 vga_model = "none";
d8c208dd 3111 break;
e37630ca 3112 case QEMU_OPTION_xen_domid:
ad96090a
BS
3113 if (!(xen_available())) {
3114 printf("Option %s not supported for this target\n", popt->name);
3115 exit(1);
3116 }
e37630ca
AL
3117 xen_domid = atoi(optarg);
3118 break;
3119 case QEMU_OPTION_xen_create:
ad96090a
BS
3120 if (!(xen_available())) {
3121 printf("Option %s not supported for this target\n", popt->name);
3122 exit(1);
3123 }
e37630ca
AL
3124 xen_mode = XEN_CREATE;
3125 break;
3126 case QEMU_OPTION_xen_attach:
ad96090a
BS
3127 if (!(xen_available())) {
3128 printf("Option %s not supported for this target\n", popt->name);
3129 exit(1);
3130 }
e37630ca
AL
3131 xen_mode = XEN_ATTACH;
3132 break;
ab6540d5 3133 case QEMU_OPTION_trace:
e4858974 3134 {
ab6540d5 3135 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
e4858974
LV
3136 if (!opts) {
3137 exit(1);
ab6540d5 3138 }
23d15e86 3139 trace_events = qemu_opt_get(opts, "events");
e4858974 3140 trace_file = qemu_opt_get(opts, "file");
ab6540d5 3141 break;
e4858974 3142 }
715a664a
GH
3143 case QEMU_OPTION_readconfig:
3144 {
dcfb0939
KW
3145 int ret = qemu_read_config_file(optarg);
3146 if (ret < 0) {
3147 fprintf(stderr, "read config %s: %s\n", optarg,
3148 strerror(-ret));
715a664a
GH
3149 exit(1);
3150 }
715a664a
GH
3151 break;
3152 }
29b0040b
GH
3153 case QEMU_OPTION_spice:
3154 olist = qemu_find_opts("spice");
3155 if (!olist) {
3156 fprintf(stderr, "spice is not supported by this qemu build.\n");
3157 exit(1);
3158 }
3159 opts = qemu_opts_parse(olist, optarg, 0);
3160 if (!opts) {
3161 fprintf(stderr, "parse error: %s\n", optarg);
3162 exit(1);
3163 }
3164 break;
715a664a
GH
3165 case QEMU_OPTION_writeconfig:
3166 {
3167 FILE *fp;
3168 if (strcmp(optarg, "-") == 0) {
3169 fp = stdout;
3170 } else {
3171 fp = fopen(optarg, "w");
3172 if (fp == NULL) {
3173 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3174 exit(1);
3175 }
3176 }
3177 qemu_config_write(fp);
3178 fclose(fp);
3179 break;
3180 }
59a5264b
JS
3181 default:
3182 os_parse_cmd_args(popt->index, optarg);
cd6f1169 3183 }
0824d6fc
FB
3184 }
3185 }
0f0bc3f1 3186 loc_set_none();
c235d738
MF
3187
3188 /* Open the logfile at this point, if necessary. We can't open the logfile
3189 * when encountering either of the logging options (-d or -D) because the
3190 * other one may be encountered later on the command line, changing the
3191 * location or level of logging.
3192 */
3193 if (log_mask) {
3194 if (log_file) {
3195 set_cpu_log_filename(log_file);
3196 }
3197 set_cpu_log(log_mask);
3198 }
330d0414 3199
23d15e86 3200 if (!trace_backend_init(trace_events, trace_file)) {
e4858974 3201 exit(1);
31d3c9b8 3202 }
0b5538c3 3203
5cea8590
PB
3204 /* If no data_dir is specified then try to find it relative to the
3205 executable path. */
3206 if (!data_dir) {
6170540b 3207 data_dir = os_find_datadir(argv[0]);
5cea8590 3208 }
60474fb5 3209 /* If all else fails use the install path specified when building. */
5cea8590 3210 if (!data_dir) {
1dabe05c 3211 data_dir = CONFIG_QEMU_DATADIR;
5cea8590
PB
3212 }
3213
d423675c
AF
3214 if (machine == NULL) {
3215 fprintf(stderr, "No machine found.\n");
3216 exit(1);
3217 }
3218
6be68d7e
JS
3219 /*
3220 * Default to max_cpus = smp_cpus, in case the user doesn't
3221 * specify a max_cpus value.
3222 */
3223 if (!max_cpus)
3224 max_cpus = smp_cpus;
3225
3d878caa 3226 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
3227 if (smp_cpus > machine->max_cpus) {
3228 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3229 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3230 machine->max_cpus);
3231 exit(1);
3232 }
3233
67b724e6
AP
3234 /*
3235 * Get the default machine options from the machine if it is not already
3236 * specified either by the configuration file or by the command line.
3237 */
3238 if (machine->default_machine_opts) {
25de5935
JK
3239 qemu_opts_set_defaults(qemu_find_opts("machine"),
3240 machine->default_machine_opts, 0);
67b724e6
AP
3241 }
3242
3329f07b
GH
3243 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3244 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
998bbd74 3245
986c5f78
GH
3246 if (machine->no_serial) {
3247 default_serial = 0;
3248 }
3249 if (machine->no_parallel) {
3250 default_parallel = 0;
3251 }
3252 if (!machine->use_virtcon) {
3253 default_virtcon = 0;
3254 }
ac33f8fa
GH
3255 if (machine->no_floppy) {
3256 default_floppy = 0;
3257 }
3258 if (machine->no_cdrom) {
3259 default_cdrom = 0;
3260 }
3261 if (machine->no_sdcard) {
3262 default_sdcard = 0;
3263 }
986c5f78 3264
993fbfdb 3265 if (display_type == DT_NOGRAPHIC) {
6a5e8b0e
GH
3266 if (default_parallel)
3267 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
3268 if (default_serial && default_monitor) {
3269 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
3270 } else if (default_virtcon && default_monitor) {
3271 add_device_config(DEV_VIRTCON, "mon:stdio");
e1c09175
GH
3272 } else {
3273 if (default_serial)
3274 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
3275 if (default_virtcon)
3276 add_device_config(DEV_VIRTCON, "stdio");
e1c09175 3277 if (default_monitor)
6ca5582d 3278 monitor_parse("stdio", "readline");
e1c09175 3279 }
998bbd74
GH
3280 } else {
3281 if (default_serial)
3282 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
3283 if (default_parallel)
3284 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 3285 if (default_monitor)
6ca5582d 3286 monitor_parse("vc:80Cx24C", "readline");
38536da1
AG
3287 if (default_virtcon)
3288 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
bc0129d9
AL
3289 }
3290
a5829fd9
T
3291 socket_init();
3292
3329f07b 3293 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
1a688d3b 3294 exit(1);
758e8e38 3295#ifdef CONFIG_VIRTFS
3329f07b 3296 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
74db920c
GS
3297 exit(1);
3298 }
3299#endif
1a688d3b 3300
eb505be1 3301 os_daemonize();
71e3ceb8 3302
aa26bb2d 3303 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
eb505be1 3304 os_pidfile_error();
93815bc2
TS
3305 exit(1);
3306 }
3307
0ac543de
JK
3308 /* init the memory */
3309 if (ram_size == 0) {
3310 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3311 }
3312
303d4e86 3313 configure_accelerator();
214910a7 3314
d3b12f5d 3315 qemu_init_cpu_loop();
3fcf7b6b
AL
3316 if (qemu_init_main_loop()) {
3317 fprintf(stderr, "qemu_init_main_loop failed\n");
3318 exit(1);
3319 }
a0abe474
PM
3320
3321 kernel_filename = qemu_opt_get(qemu_opts_find(qemu_find_opts("machine"),
3322 0), "kernel");
3323 initrd_filename = qemu_opt_get(qemu_opts_find(qemu_find_opts("machine"),
3324 0), "initrd");
3325 kernel_cmdline = qemu_opt_get(qemu_opts_find(qemu_find_opts("machine"),
3326 0), "append");
3327 if (!kernel_cmdline) {
3328 kernel_cmdline = "";
3329 }
3330
a20dd508 3331 linux_boot = (kernel_filename != NULL);
6c41b272 3332
f8d39c01
TS
3333 if (!linux_boot && *kernel_cmdline != '\0') {
3334 fprintf(stderr, "-append only allowed with -kernel option\n");
3335 exit(1);
3336 }
3337
3338 if (!linux_boot && initrd_filename != NULL) {
3339 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3340 exit(1);
3341 }
3342
9156d763 3343 os_set_line_buffering();
3b46e624 3344
7183b4b4
AL
3345 if (init_timer_alarm() < 0) {
3346 fprintf(stderr, "could not initialize alarm timer\n");
3347 exit(1);
3348 }
0abe905d
MF
3349
3350 if (icount_option && (kvm_enabled() || xen_enabled())) {
3351 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
3352 exit(1);
3353 }
4e3de9e9 3354 configure_icount(icount_option);
634fce96 3355
dc1c9fe8
MM
3356 if (net_init_clients() < 0) {
3357 exit(1);
702c651c 3358 }
f1510b2c 3359
dc72ac14 3360 /* init the bluetooth world */
bd3c948d
GH
3361 if (foreach_device_config(DEV_BT, bt_parse))
3362 exit(1);
dc72ac14 3363
834e76ea
AP
3364 if (!xen_enabled()) {
3365 /* On 32-bit hosts, QEMU is limited by virtual address space */
3366 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3367 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3368 exit(1);
3369 }
3370 }
9ae02555 3371
d5ab9713 3372 cpu_exec_init_all();
26a5f13b 3373
eb852011 3374 bdrv_init_with_whitelist();
c4b1fcc0 3375
c163b5ca
LS
3376 blk_mig_init();
3377
e4bcb14c 3378 /* open the virtual block devices */
9dfd7c7a 3379 if (snapshot)
3329f07b
GH
3380 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3381 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
9dfd7c7a 3382 exit(1);
3e3d5815 3383
4e5d9b57
MA
3384 default_drive(default_cdrom, snapshot, machine->use_scsi,
3385 IF_DEFAULT, 2, CDROM_OPTS);
3386 default_drive(default_floppy, snapshot, machine->use_scsi,
3387 IF_FLOPPY, 0, FD_OPTS);
3388 default_drive(default_sdcard, snapshot, machine->use_scsi,
3389 IF_SD, 0, SD_OPTS);
3390
97ab12d4 3391 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
c163b5ca 3392 ram_load, NULL);
8a7ddc38 3393
268a362c
AL
3394 if (nb_numa_nodes > 0) {
3395 int i;
3396
ea0e5418
SL
3397 if (nb_numa_nodes > MAX_NODES) {
3398 nb_numa_nodes = MAX_NODES;
268a362c
AL
3399 }
3400
3401 /* If no memory size if given for any node, assume the default case
3402 * and distribute the available memory equally across all nodes
3403 */
3404 for (i = 0; i < nb_numa_nodes; i++) {
3405 if (node_mem[i] != 0)
3406 break;
3407 }
3408 if (i == nb_numa_nodes) {
3409 uint64_t usedmem = 0;
3410
3411 /* On Linux, the each node's border has to be 8MB aligned,
3412 * the final node gets the rest.
3413 */
3414 for (i = 0; i < nb_numa_nodes - 1; i++) {
3415 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3416 usedmem += node_mem[i];
3417 }
3418 node_mem[i] = ram_size - usedmem;
3419 }
3420
3421 for (i = 0; i < nb_numa_nodes; i++) {
3422 if (node_cpumask[i] != 0)
3423 break;
3424 }
3425 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3426 * must cope with this anyway, because there are BIOSes out there in
3427 * real machines which also use this scheme.
3428 */
3429 if (i == nb_numa_nodes) {
991dfefd 3430 for (i = 0; i < max_cpus; i++) {
268a362c
AL
3431 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3432 }
3433 }
3434 }
3435
3329f07b 3436 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
157b9319
JK
3437 exit(1);
3438 }
3439
998bbd74
GH
3440 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3441 exit(1);
6a5e8b0e
GH
3442 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3443 exit(1);
aee1b935
GH
3444 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3445 exit(1);
c9f398e5
PA
3446 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3447 exit(1);
2796dae0 3448
83f7d43a 3449 module_call_init(MODULE_INIT_QOM);
aae9460e 3450
a369da5f
BS
3451 /* must be after qdev registration but before machine init */
3452 if (vga_model) {
3453 select_vgahw(vga_model);
3454 } else if (cirrus_vga_available()) {
3455 select_vgahw("cirrus");
3456 } else {
3457 select_vgahw("none");
3458 }
3459
3329f07b 3460 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
ff952ba2
MA
3461 exit(0);
3462
09aaa160
MA
3463 if (watchdog) {
3464 i = select_watchdog(watchdog);
3465 if (i > 0)
3466 exit (i == 1 ? 1 : 0);
3467 }
3468
b6b61144 3469 if (machine->compat_props) {
458fb679 3470 qdev_prop_register_global_list(machine->compat_props);
b6b61144 3471 }
d0fef6fb
GH
3472 qemu_add_globals();
3473
1de81d28
AL
3474 qdev_machine_init();
3475
fbe1b595 3476 machine->init(ram_size, boot_devices,
3023f332
AL
3477 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3478
ea375f9a 3479 cpu_synchronize_all_post_init();
268a362c 3480
87d0a28e 3481 set_numa_modes();
268a362c 3482
6f338c34
AL
3483 current_machine = machine;
3484
3023f332
AL
3485 /* init USB devices */
3486 if (usb_enabled) {
0752706d
MA
3487 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3488 exit(1);
3023f332
AL
3489 }
3490
bd3c948d 3491 /* init generic devices */
3329f07b 3492 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
bd3c948d
GH
3493 exit(1);
3494
668680f7
MA
3495 net_check_clients();
3496
3023f332 3497 /* just use the first displaystate for the moment */
b473df6e 3498 ds = get_displaystate();
993fbfdb 3499
a3e22260
GH
3500 if (using_spice)
3501 display_remote++;
6b62dc2d 3502 if (display_type == DT_DEFAULT && !display_remote) {
993fbfdb
AL
3503#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3504 display_type = DT_SDL;
821601ea 3505#elif defined(CONFIG_VNC)
993fbfdb
AL
3506 vnc_display = "localhost:0,to=99";
3507 show_vnc_port = 1;
821601ea
JS
3508#else
3509 display_type = DT_NONE;
993fbfdb
AL
3510#endif
3511 }
821601ea 3512
993fbfdb 3513
6b62dc2d 3514 /* init local displays */
993fbfdb
AL
3515 switch (display_type) {
3516 case DT_NOGRAPHIC:
3517 break;
4d3b6f6e 3518#if defined(CONFIG_CURSES)
993fbfdb
AL
3519 case DT_CURSES:
3520 curses_display_init(ds, full_screen);
3521 break;
4d3b6f6e 3522#endif
5b0753e0 3523#if defined(CONFIG_SDL)
993fbfdb
AL
3524 case DT_SDL:
3525 sdl_display_init(ds, full_screen, no_frame);
3526 break;
5b0753e0 3527#elif defined(CONFIG_COCOA)
993fbfdb
AL
3528 case DT_SDL:
3529 cocoa_display_init(ds, full_screen);
3530 break;
313aa567 3531#endif
6b62dc2d
GH
3532 default:
3533 break;
3534 }
3535
0ce235a7
GN
3536 /* must be after terminal init, SDL library changes signal handlers */
3537 os_setup_signal_handling();
3538
821601ea 3539#ifdef CONFIG_VNC
6b62dc2d
GH
3540 /* init remote displays */
3541 if (vnc_display) {
993fbfdb
AL
3542 vnc_display_init(ds);
3543 if (vnc_display_open(ds, vnc_display) < 0)
3544 exit(1);
f92f8afe 3545
993fbfdb
AL
3546 if (show_vnc_port) {
3547 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
f92f8afe 3548 }
313aa567 3549 }
821601ea 3550#endif
a3e22260 3551#ifdef CONFIG_SPICE
a19cbfb3 3552 if (using_spice && !qxl_enabled) {
a3e22260
GH
3553 qemu_spice_display_init(ds);
3554 }
3555#endif
5b08fc10 3556
6b62dc2d
GH
3557 /* display setup */
3558 dpy_resize(ds);
3023f332
AL
3559 dcl = ds->listeners;
3560 while (dcl != NULL) {
3561 if (dcl->dpy_refresh != NULL) {
7bd427d8
PB
3562 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3563 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
0f2ad63f 3564 break;
20d8a3ed 3565 }
3023f332 3566 dcl = dcl->next;
20d8a3ed 3567 }
b473df6e 3568 text_consoles_set_display(ds);
2796dae0 3569
ef0c4a0d 3570 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
59030a8c 3571 exit(1);
45669e00 3572 }
45669e00 3573
3418bd25
GH
3574 qdev_machine_creation_done();
3575
15ff7705
GH
3576 if (rom_load_all() != 0) {
3577 fprintf(stderr, "rom loading failed\n");
3578 exit(1);
3579 }
45a50b16 3580
80376c3f
IY
3581 /* TODO: once all bus devices are qdevified, this should be done
3582 * when bus is created by qdev.c */
3583 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4cab946a
GN
3584 qemu_run_machine_init_done_notifiers();
3585
e063eb1f 3586 qemu_system_reset(VMRESET_SILENT);
05f2401e 3587 if (loadvm) {
03cd4655 3588 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
3589 autostart = 0;
3590 }
3591 }
d63d307f 3592
2bb8c10c 3593 if (incoming) {
0461d5a6 3594 runstate_set(RUN_STATE_INMIGRATE);
8ca5e801
JQ
3595 int ret = qemu_start_incoming_migration(incoming);
3596 if (ret < 0) {
3597 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3598 incoming, ret);
3599 exit(ret);
3600 }
6b99dadc 3601 } else if (autostart) {
c0f4ce77 3602 vm_start();
6b99dadc 3603 }
ffd843bc 3604
eb505be1 3605 os_setup_post();
71e3ceb8 3606
99435906 3607 resume_all_vcpus();
8a7ddc38 3608 main_loop();
99435906
PB
3609 bdrv_close_all();
3610 pause_all_vcpus();
63a01ef8 3611 net_cleanup();
3d3b8303 3612 res_free();
b46a8906 3613
0824d6fc
FB
3614 return 0;
3615}