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