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