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