]> git.proxmox.com Git - mirror_qemu.git/blame - vl.c
Refactor CPUState handling out of vl.c
[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>
0858532e 37#include <pwd.h>
67b915a5 38#include <sys/times.h>
f1510b2c 39#include <sys/wait.h>
67b915a5 40#include <termios.h>
67b915a5 41#include <sys/mman.h>
f1510b2c 42#include <sys/ioctl.h>
24646c7e 43#include <sys/resource.h>
f1510b2c 44#include <sys/socket.h>
c94c8d64 45#include <netinet/in.h>
24646c7e 46#include <net/if.h>
24646c7e 47#include <arpa/inet.h>
9d728e8c 48#include <dirent.h>
7c9d8e07 49#include <netdb.h>
cb4b976b 50#include <sys/select.h>
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>
24646c7e
BS
55#else
56#include <util.h>
128ab2ff 57#endif
bbe813a2 58#else
223f0d72 59#ifdef __linux__
7d3505c5
FB
60#include <pty.h>
61#include <malloc.h>
1889465a 62#include <sys/prctl.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
TS
71#include <netinet/arp.h>
72#include <netinet/in.h>
73#include <netinet/in_systm.h>
74#include <netinet/ip.h>
75#include <netinet/ip_icmp.h> // must come after ip.h
76#include <netinet/udp.h>
77#include <netinet/tcp.h>
78#include <net/if.h>
79#include <syslog.h>
80#include <stropts.h>
8d32cf0e
BS
81/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
82 discussion about Solaris header problems */
83extern int madvise(caddr_t, size_t, int);
67b915a5 84#endif
7d3505c5 85#endif
ec530c81 86#endif
67b915a5 87
9892fbfb
BS
88#if defined(__OpenBSD__)
89#include <util.h>
90#endif
91
8a16d273
TS
92#if defined(CONFIG_VDE)
93#include <libvdeplug.h>
94#endif
95
67b915a5 96#ifdef _WIN32
49dc768d 97#include <windows.h>
67b915a5
FB
98#endif
99
73332e5c 100#ifdef CONFIG_SDL
59a36a2f 101#if defined(__APPLE__) || defined(main)
6693665a 102#include <SDL.h>
880fec5d 103int qemu_main(int argc, char **argv, char **envp);
104int main(int argc, char **argv)
105{
59a36a2f 106 return qemu_main(argc, argv, NULL);
880fec5d 107}
108#undef main
109#define main qemu_main
96bcd4f8 110#endif
73332e5c 111#endif /* CONFIG_SDL */
0824d6fc 112
5b0753e0
FB
113#ifdef CONFIG_COCOA
114#undef main
115#define main qemu_main
116#endif /* CONFIG_COCOA */
117
511d2b14
BS
118#include "hw/hw.h"
119#include "hw/boards.h"
120#include "hw/usb.h"
121#include "hw/pcmcia.h"
122#include "hw/pc.h"
123#include "hw/audiodev.h"
124#include "hw/isa.h"
125#include "hw/baum.h"
126#include "hw/bt.h"
9dd986cc 127#include "hw/watchdog.h"
b6f6e3d3 128#include "hw/smbios.h"
e37630ca 129#include "hw/xen.h"
bd3c948d 130#include "hw/qdev.h"
45a50b16 131#include "hw/loader.h"
5ef4efa4 132#include "bt-host.h"
511d2b14 133#include "net.h"
68ac40d2 134#include "net/slirp.h"
511d2b14
BS
135#include "monitor.h"
136#include "console.h"
137#include "sysemu.h"
138#include "gdbstub.h"
139#include "qemu-timer.h"
140#include "qemu-char.h"
141#include "cache-utils.h"
142#include "block.h"
c163b5ca
LS
143#include "block_int.h"
144#include "block-migration.h"
a718acec 145#include "dma.h"
511d2b14
BS
146#include "audio/audio.h"
147#include "migration.h"
148#include "kvm.h"
149#include "balloon.h"
d3f24367 150#include "qemu-option.h"
7282a033 151#include "qemu-config.h"
e78c48ec 152#include "qemu-objects.h"
511d2b14 153
0824d6fc 154#include "disas.h"
fc01f7e7 155
8a7ddc38 156#include "exec-all.h"
0824d6fc 157
511d2b14
BS
158#include "qemu_socket.h"
159
d918f23e 160#include "slirp/libslirp.h"
511d2b14 161
72cf2d4f 162#include "qemu-queue.h"
296af7c9 163#include "cpus.h"
72cf2d4f 164
9dc63a1e
BS
165//#define DEBUG_NET
166//#define DEBUG_SLIRP
330d0414 167
1bfe856e 168#define DEFAULT_RAM_SIZE 128
313aa567 169
98b19252
AS
170#define MAX_VIRTIO_CONSOLES 1
171
5cea8590 172static const char *data_dir;
1192dad8 173const char *bios_name = NULL;
e4bcb14c 174/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
faea38e7 175 to store the VM snapshots */
72cf2d4f
BS
176struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
177struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
cb5a7aa8 178enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
993fbfdb 179DisplayType display_type = DT_DEFAULT;
3d11d0eb 180const char* keyboard_layout = NULL;
c227f099 181ram_addr_t ram_size;
c902760f
MT
182const char *mem_path = NULL;
183#ifdef MAP_POPULATE
184int mem_prealloc = 0; /* force preallocation of physical target memory */
185#endif
c4b1fcc0 186int nb_nics;
7c9d8e07 187NICInfo nd_table[MAX_NICS];
8a7ddc38 188int vm_running;
d399f677 189int autostart;
f6503059
AZ
190static int rtc_utc = 1;
191static int rtc_date_offset = -1; /* -1 means no change */
6875204c 192QEMUClock *rtc_clock;
64465297 193int vga_interface_type = VGA_NONE;
d827220b
FB
194#ifdef TARGET_SPARC
195int graphic_width = 1024;
196int graphic_height = 768;
eee0b836 197int graphic_depth = 8;
d827220b 198#else
1bfe856e
FB
199int graphic_width = 800;
200int graphic_height = 600;
e9b137c2 201int graphic_depth = 15;
eee0b836 202#endif
dbed7e40 203static int full_screen = 0;
634a21f6 204#ifdef CONFIG_SDL
dbed7e40 205static int no_frame = 0;
634a21f6 206#endif
667accab 207int no_quit = 0;
8d11df9e 208CharDriverState *serial_hds[MAX_SERIAL_PORTS];
6508fe59 209CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
9ede2fde 210CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
a09db21f 211int win2k_install_hack = 0;
f7736b91 212#ifdef TARGET_I386
73822ec8 213int rtc_td_hack = 0;
a09db21f 214#endif
bb36d470 215int usb_enabled = 0;
1b530a6d 216int singlestep = 0;
6a00d601 217int smp_cpus = 1;
6be68d7e 218int max_cpus = 0;
dc6b1c09
AP
219int smp_cores = 1;
220int smp_threads = 1;
73fc9742 221const char *vnc_display;
6515b203 222int acpi_enabled = 1;
16b29ae1 223int no_hpet = 0;
52ca8d6a 224int fd_bootchk = 1;
d1beab82 225int no_reboot = 0;
b2f76161 226int no_shutdown = 0;
9467cd46 227int cursor_hide = 1;
a171fe39 228int graphic_rotate = 0;
6b35e7bf 229uint8_t irq0override = 1;
b9e82a59 230#ifndef _WIN32
71e3ceb8 231int daemonize = 0;
b9e82a59 232#endif
09aaa160 233const char *watchdog;
9ae02555
TS
234const char *option_rom[MAX_OPTION_ROMS];
235int nb_option_roms;
8e71621f 236int semihosting_enabled = 0;
2b8f2d41
AZ
237#ifdef TARGET_ARM
238int old_param = 0;
239#endif
c35734b2 240const char *qemu_name;
3780e197 241int alt_grab = 0;
0ca9f8a4 242int ctrl_grab = 0;
95efd11c 243#if defined(TARGET_SPARC) || defined(TARGET_PPC)
66508601
BS
244unsigned int nb_prom_envs = 0;
245const char *prom_envs[MAX_PROM_ENVS];
246#endif
95387491 247int boot_menu;
0824d6fc 248
268a362c
AL
249int nb_numa_nodes;
250uint64_t node_mem[MAX_NODES];
251uint64_t node_cpumask[MAX_NODES];
252
9043b62d 253static QEMUTimer *nographic_timer;
ee5605e5 254
8fcb1b90
BS
255uint8_t qemu_uuid[16];
256
76e30d0f
JK
257static QEMUBootSetHandler *boot_set_handler;
258static void *boot_set_opaque;
259
d745bef8
BS
260int kvm_allowed = 0;
261uint32_t xen_domid;
262enum xen_mode xen_mode = XEN_EMULATE;
263
cc84de95
MT
264#ifdef SIGRTMIN
265#define SIG_IPI (SIGRTMIN+4)
266#else
267#define SIG_IPI SIGUSR1
268#endif
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
GH
285 { .driver = "isa-fdc", .flag = &default_floppy },
286 { .driver = "ide-drive", .flag = &default_cdrom },
392ecf54
AS
287 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
288 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
289 { .driver = "virtio-serial", .flag = &default_virtcon },
64465297 290 { .driver = "VGA", .flag = &default_vga },
69fd02ee
GH
291 { .driver = "cirrus-vga", .flag = &default_vga },
292 { .driver = "vmware-svga", .flag = &default_vga },
998bbd74
GH
293};
294
295static int default_driver_check(QemuOpts *opts, void *opaque)
296{
297 const char *driver = qemu_opt_get(opts, "driver");
298 int i;
299
300 if (!driver)
301 return 0;
302 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
303 if (strcmp(default_list[i].driver, driver) != 0)
304 continue;
305 *(default_list[i].flag) = 0;
306 }
307 return 0;
308}
309
0824d6fc 310/***********************************************************/
26aa7d72
FB
311/* x86 ISA bus support */
312
c227f099 313target_phys_addr_t isa_mem_base = 0;
3de388f6 314PicState2 *isa_pic;
0824d6fc 315
1889465a
AK
316static void set_proc_name(const char *s)
317{
6ca8d0fd 318#if defined(__linux__) && defined(PR_SET_NAME)
1889465a
AK
319 char name[16];
320 if (!s)
321 return;
322 name[sizeof(name) - 1] = 0;
323 strncpy(name, s, sizeof(name));
324 /* Could rewrite argv[0] too, but that's a bit more complicated.
325 This simple way is enough for `top'. */
326 prctl(PR_SET_NAME, name);
327#endif
328}
df751fa8
AL
329
330/***************/
331/* ballooning */
332
333static QEMUBalloonEvent *qemu_balloon_event;
334void *qemu_balloon_event_opaque;
335
336void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
337{
338 qemu_balloon_event = func;
339 qemu_balloon_event_opaque = opaque;
340}
341
625a5bef 342int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
df751fa8 343{
625a5bef
AL
344 if (qemu_balloon_event) {
345 qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque);
346 return 1;
347 } else {
348 return 0;
349 }
df751fa8
AL
350}
351
625a5bef 352int qemu_balloon_status(MonitorCompletion cb, void *opaque)
df751fa8 353{
625a5bef
AL
354 if (qemu_balloon_event) {
355 qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque);
356 return 1;
357 } else {
358 return 0;
359 }
df751fa8 360}
0824d6fc 361
455204eb 362
8f0056b7
PB
363/***********************************************************/
364/* real time host monotonic timer */
09b26c5e 365
1dce7c3c
FB
366/* compute with 96 bit intermediate result: (a*b)/c */
367uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
0824d6fc 368{
1dce7c3c
FB
369 union {
370 uint64_t ll;
371 struct {
e2542fe2 372#ifdef HOST_WORDS_BIGENDIAN
1dce7c3c
FB
373 uint32_t high, low;
374#else
375 uint32_t low, high;
3b46e624 376#endif
1dce7c3c
FB
377 } l;
378 } u, res;
379 uint64_t rl, rh;
0824d6fc 380
1dce7c3c
FB
381 u.ll = a;
382 rl = (uint64_t)u.l.low * (uint64_t)b;
383 rh = (uint64_t)u.l.high * (uint64_t)b;
384 rh += (rl >> 32);
385 res.l.high = rh / c;
386 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
387 return res.ll;
34865134
FB
388}
389
f6503059
AZ
390/***********************************************************/
391/* host time/date access */
392void qemu_get_timedate(struct tm *tm, int offset)
393{
394 time_t ti;
395 struct tm *ret;
396
397 time(&ti);
398 ti += offset;
399 if (rtc_date_offset == -1) {
400 if (rtc_utc)
401 ret = gmtime(&ti);
402 else
403 ret = localtime(&ti);
404 } else {
405 ti -= rtc_date_offset;
406 ret = gmtime(&ti);
407 }
408
409 memcpy(tm, ret, sizeof(struct tm));
410}
411
412int qemu_timedate_diff(struct tm *tm)
413{
414 time_t seconds;
415
416 if (rtc_date_offset == -1)
417 if (rtc_utc)
418 seconds = mktimegm(tm);
419 else
420 seconds = mktime(tm);
421 else
422 seconds = mktimegm(tm) + rtc_date_offset;
423
424 return seconds - time(NULL);
425}
426
80cd3478
LC
427void rtc_change_mon_event(struct tm *tm)
428{
429 QObject *data;
430
431 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
432 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
433 qobject_decref(data);
434}
435
1ed2fc1f
JK
436static void configure_rtc_date_offset(const char *startdate, int legacy)
437{
438 time_t rtc_start_date;
439 struct tm tm;
440
441 if (!strcmp(startdate, "now") && legacy) {
442 rtc_date_offset = -1;
443 } else {
444 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
445 &tm.tm_year,
446 &tm.tm_mon,
447 &tm.tm_mday,
448 &tm.tm_hour,
449 &tm.tm_min,
450 &tm.tm_sec) == 6) {
451 /* OK */
452 } else if (sscanf(startdate, "%d-%d-%d",
453 &tm.tm_year,
454 &tm.tm_mon,
455 &tm.tm_mday) == 3) {
456 tm.tm_hour = 0;
457 tm.tm_min = 0;
458 tm.tm_sec = 0;
459 } else {
460 goto date_fail;
461 }
462 tm.tm_year -= 1900;
463 tm.tm_mon--;
464 rtc_start_date = mktimegm(&tm);
465 if (rtc_start_date == -1) {
466 date_fail:
467 fprintf(stderr, "Invalid date format. Valid formats are:\n"
468 "'2006-06-17T16:01:21' or '2006-06-17'\n");
469 exit(1);
470 }
471 rtc_date_offset = time(NULL) - rtc_start_date;
472 }
473}
474
475static void configure_rtc(QemuOpts *opts)
476{
477 const char *value;
478
479 value = qemu_opt_get(opts, "base");
480 if (value) {
481 if (!strcmp(value, "utc")) {
482 rtc_utc = 1;
483 } else if (!strcmp(value, "localtime")) {
484 rtc_utc = 0;
485 } else {
486 configure_rtc_date_offset(value, 0);
487 }
488 }
6875204c
JK
489 value = qemu_opt_get(opts, "clock");
490 if (value) {
491 if (!strcmp(value, "host")) {
492 rtc_clock = host_clock;
493 } else if (!strcmp(value, "vm")) {
494 rtc_clock = vm_clock;
495 } else {
496 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
497 exit(1);
498 }
499 }
7e4c0336 500#ifdef TARGET_I386
1ed2fc1f
JK
501 value = qemu_opt_get(opts, "driftfix");
502 if (value) {
7e4c0336 503 if (!strcmp(value, "slew")) {
1ed2fc1f 504 rtc_td_hack = 1;
7e4c0336 505 } else if (!strcmp(value, "none")) {
1ed2fc1f
JK
506 rtc_td_hack = 0;
507 } else {
508 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
509 exit(1);
510 }
511 }
512#endif
513}
514
fd1dff4b 515#ifdef _WIN32
28695489 516static void socket_cleanup(void)
fd1dff4b
FB
517{
518 WSACleanup();
519}
82c643ff 520
fd1dff4b
FB
521static int socket_init(void)
522{
523 WSADATA Data;
524 int ret, err;
525
526 ret = WSAStartup(MAKEWORD(2,2), &Data);
527 if (ret != 0) {
528 err = WSAGetLastError();
529 fprintf(stderr, "WSAStartup: %d\n", err);
530 return -1;
531 }
28695489 532 atexit(socket_cleanup);
fd1dff4b
FB
533 return 0;
534}
64b7b733
AJ
535#endif
536
1ae26a18
AZ
537/***********************************************************/
538/* Bluetooth support */
539static int nb_hcis;
540static int cur_hci;
541static struct HCIInfo *hci_table[MAX_NICS];
dc72ac14 542
1ae26a18
AZ
543static struct bt_vlan_s {
544 struct bt_scatternet_s net;
545 int id;
546 struct bt_vlan_s *next;
547} *first_bt_vlan;
548
549/* find or alloc a new bluetooth "VLAN" */
674bb261 550static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1ae26a18
AZ
551{
552 struct bt_vlan_s **pvlan, *vlan;
553 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
554 if (vlan->id == id)
555 return &vlan->net;
556 }
557 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
558 vlan->id = id;
559 pvlan = &first_bt_vlan;
560 while (*pvlan != NULL)
561 pvlan = &(*pvlan)->next;
562 *pvlan = vlan;
563 return &vlan->net;
564}
565
566static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
567{
568}
569
570static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
571{
572 return -ENOTSUP;
573}
574
575static struct HCIInfo null_hci = {
576 .cmd_send = null_hci_send,
577 .sco_send = null_hci_send,
578 .acl_send = null_hci_send,
579 .bdaddr_set = null_hci_addr_set,
580};
581
582struct HCIInfo *qemu_next_hci(void)
583{
584 if (cur_hci == nb_hcis)
585 return &null_hci;
586
587 return hci_table[cur_hci++];
588}
589
dc72ac14
AZ
590static struct HCIInfo *hci_init(const char *str)
591{
592 char *endp;
593 struct bt_scatternet_s *vlan = 0;
594
595 if (!strcmp(str, "null"))
596 /* null */
597 return &null_hci;
598 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
599 /* host[:hciN] */
600 return bt_host_hci(str[4] ? str + 5 : "hci0");
601 else if (!strncmp(str, "hci", 3)) {
602 /* hci[,vlan=n] */
603 if (str[3]) {
604 if (!strncmp(str + 3, ",vlan=", 6)) {
605 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
606 if (*endp)
607 vlan = 0;
608 }
609 } else
610 vlan = qemu_find_bt_vlan(0);
611 if (vlan)
612 return bt_new_hci(vlan);
613 }
614
615 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
616
617 return 0;
618}
619
620static int bt_hci_parse(const char *str)
621{
622 struct HCIInfo *hci;
c227f099 623 bdaddr_t bdaddr;
dc72ac14
AZ
624
625 if (nb_hcis >= MAX_NICS) {
626 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
627 return -1;
628 }
629
630 hci = hci_init(str);
631 if (!hci)
632 return -1;
633
634 bdaddr.b[0] = 0x52;
635 bdaddr.b[1] = 0x54;
636 bdaddr.b[2] = 0x00;
637 bdaddr.b[3] = 0x12;
638 bdaddr.b[4] = 0x34;
639 bdaddr.b[5] = 0x56 + nb_hcis;
640 hci->bdaddr_set(hci, bdaddr.b);
641
642 hci_table[nb_hcis++] = hci;
643
644 return 0;
645}
646
647static void bt_vhci_add(int vlan_id)
648{
649 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
650
651 if (!vlan->slave)
652 fprintf(stderr, "qemu: warning: adding a VHCI to "
653 "an empty scatternet %i\n", vlan_id);
654
655 bt_vhci_init(bt_new_hci(vlan));
656}
657
658static struct bt_device_s *bt_device_add(const char *opt)
659{
660 struct bt_scatternet_s *vlan;
661 int vlan_id = 0;
662 char *endp = strstr(opt, ",vlan=");
663 int len = (endp ? endp - opt : strlen(opt)) + 1;
664 char devname[10];
665
666 pstrcpy(devname, MIN(sizeof(devname), len), opt);
667
668 if (endp) {
669 vlan_id = strtol(endp + 6, &endp, 0);
670 if (*endp) {
671 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
672 return 0;
673 }
674 }
675
676 vlan = qemu_find_bt_vlan(vlan_id);
677
678 if (!vlan->slave)
679 fprintf(stderr, "qemu: warning: adding a slave device to "
680 "an empty scatternet %i\n", vlan_id);
681
682 if (!strcmp(devname, "keyboard"))
683 return bt_keyboard_init(vlan);
684
685 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
686 return 0;
687}
688
689static int bt_parse(const char *opt)
690{
691 const char *endp, *p;
692 int vlan;
693
694 if (strstart(opt, "hci", &endp)) {
695 if (!*endp || *endp == ',') {
696 if (*endp)
697 if (!strstart(endp, ",vlan=", 0))
698 opt = endp + 1;
699
700 return bt_hci_parse(opt);
701 }
702 } else if (strstart(opt, "vhci", &endp)) {
703 if (!*endp || *endp == ',') {
704 if (*endp) {
705 if (strstart(endp, ",vlan=", &p)) {
706 vlan = strtol(p, (char **) &endp, 0);
707 if (*endp) {
708 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
709 return 1;
710 }
711 } else {
712 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
713 return 1;
714 }
715 } else
716 vlan = 0;
717
718 bt_vhci_add(vlan);
719 return 0;
720 }
721 } else if (strstart(opt, "device:", &endp))
722 return !bt_device_add(endp);
723
724 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
725 return 1;
726}
727
1ae26a18
AZ
728/***********************************************************/
729/* QEMU Block devices */
730
609497ab 731#define HD_ALIAS "index=%d,media=disk"
e4bcb14c 732#define CDROM_ALIAS "index=2,media=cdrom"
e4bcb14c 733#define FD_ALIAS "index=%d,if=floppy"
609497ab
AZ
734#define PFLASH_ALIAS "if=pflash"
735#define MTD_ALIAS "if=mtd"
9d413d1d 736#define SD_ALIAS "index=0,if=sd"
e4bcb14c 737
9dfd7c7a 738QemuOpts *drive_add(const char *file, const char *fmt, ...)
e4bcb14c
TS
739{
740 va_list ap;
9dfd7c7a
GH
741 char optstr[1024];
742 QemuOpts *opts;
e4bcb14c 743
e4bcb14c 744 va_start(ap, fmt);
9dfd7c7a 745 vsnprintf(optstr, sizeof(optstr), fmt, ap);
e4bcb14c
TS
746 va_end(ap);
747
8212c64f 748 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
9dfd7c7a
GH
749 if (!opts) {
750 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
751 __FUNCTION__, optstr);
752 return NULL;
753 }
754 if (file)
755 qemu_opt_set(opts, "file", file);
756 return opts;
b01b1111
AL
757}
758
751c6a17 759DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
e4bcb14c 760{
751c6a17 761 DriveInfo *dinfo;
e4bcb14c
TS
762
763 /* seek interface, bus and unit */
764
72cf2d4f 765 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17
GH
766 if (dinfo->type == type &&
767 dinfo->bus == bus &&
768 dinfo->unit == unit)
769 return dinfo;
770 }
e4bcb14c 771
751c6a17 772 return NULL;
e4bcb14c
TS
773}
774
2e810b36 775DriveInfo *drive_get_by_id(const char *id)
1dae12e6
GH
776{
777 DriveInfo *dinfo;
778
72cf2d4f 779 QTAILQ_FOREACH(dinfo, &drives, next) {
1dae12e6
GH
780 if (strcmp(id, dinfo->id))
781 continue;
782 return dinfo;
783 }
784 return NULL;
785}
786
f60d39bc 787int drive_get_max_bus(BlockInterfaceType type)
e4bcb14c
TS
788{
789 int max_bus;
751c6a17 790 DriveInfo *dinfo;
e4bcb14c
TS
791
792 max_bus = -1;
72cf2d4f 793 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17
GH
794 if(dinfo->type == type &&
795 dinfo->bus > max_bus)
796 max_bus = dinfo->bus;
e4bcb14c
TS
797 }
798 return max_bus;
799}
800
fa879c64
AL
801const char *drive_get_serial(BlockDriverState *bdrv)
802{
751c6a17 803 DriveInfo *dinfo;
fa879c64 804
72cf2d4f 805 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17
GH
806 if (dinfo->bdrv == bdrv)
807 return dinfo->serial;
808 }
fa879c64
AL
809
810 return "\0";
811}
812
f7850099
KW
813BlockInterfaceErrorAction drive_get_on_error(
814 BlockDriverState *bdrv, int is_read)
428c5705 815{
751c6a17 816 DriveInfo *dinfo;
428c5705 817
72cf2d4f 818 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17 819 if (dinfo->bdrv == bdrv)
e9b2e818 820 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
751c6a17 821 }
428c5705 822
e9b2e818 823 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
428c5705
AL
824}
825
a1620fac
AJ
826static void bdrv_format_print(void *opaque, const char *name)
827{
828 fprintf(stderr, " %s", name);
829}
830
56a14938 831void drive_uninit(DriveInfo *dinfo)
b01b1111 832{
56a14938
GH
833 qemu_opts_del(dinfo->opts);
834 bdrv_delete(dinfo->bdrv);
835 QTAILQ_REMOVE(&drives, dinfo, next);
836 qemu_free(dinfo);
b01b1111
AL
837}
838
e9b2e818
KW
839static int parse_block_error_action(const char *buf, int is_read)
840{
841 if (!strcmp(buf, "ignore")) {
842 return BLOCK_ERR_IGNORE;
843 } else if (!is_read && !strcmp(buf, "enospc")) {
844 return BLOCK_ERR_STOP_ENOSPC;
845 } else if (!strcmp(buf, "stop")) {
846 return BLOCK_ERR_STOP_ANY;
847 } else if (!strcmp(buf, "report")) {
848 return BLOCK_ERR_REPORT;
849 } else {
850 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
851 buf, is_read ? "read" : "write");
852 return -1;
853 }
854}
855
9dfd7c7a 856DriveInfo *drive_init(QemuOpts *opts, void *opaque,
751c6a17 857 int *fatal_error)
e4bcb14c 858{
9dfd7c7a
GH
859 const char *buf;
860 const char *file = NULL;
c8522bdf 861 char devname[128];
9dfd7c7a 862 const char *serial;
c8522bdf 863 const char *mediastr = "";
f60d39bc 864 BlockInterfaceType type;
e4bcb14c
TS
865 enum { MEDIA_DISK, MEDIA_CDROM } media;
866 int bus_id, unit_id;
867 int cyls, heads, secs, translation;
1e72d3b7 868 BlockDriver *drv = NULL;
4d73cd3b 869 QEMUMachine *machine = opaque;
e4bcb14c
TS
870 int max_devs;
871 int index;
33f00271 872 int cache;
5c6c3a6c 873 int aio = 0;
59f2689d 874 int ro = 0;
e9b2e818
KW
875 int bdrv_flags;
876 int on_read_error, on_write_error;
c2cc47a4 877 const char *devaddr;
751c6a17 878 DriveInfo *dinfo;
9dfd7c7a 879 int snapshot = 0;
e4bcb14c 880
9dfd7c7a 881 *fatal_error = 1;
e4bcb14c 882
e4bcb14c 883 translation = BIOS_ATA_TRANSLATION_AUTO;
0aa217e4 884 cache = 1;
e4bcb14c 885
4d007814 886 if (machine && machine->use_scsi) {
f60d39bc 887 type = IF_SCSI;
e4bcb14c 888 max_devs = MAX_SCSI_DEVS;
363a37d5 889 pstrcpy(devname, sizeof(devname), "scsi");
e4bcb14c 890 } else {
f60d39bc 891 type = IF_IDE;
e4bcb14c 892 max_devs = MAX_IDE_DEVS;
363a37d5 893 pstrcpy(devname, sizeof(devname), "ide");
e4bcb14c
TS
894 }
895 media = MEDIA_DISK;
896
897 /* extract parameters */
9dfd7c7a
GH
898 bus_id = qemu_opt_get_number(opts, "bus", 0);
899 unit_id = qemu_opt_get_number(opts, "unit", -1);
900 index = qemu_opt_get_number(opts, "index", -1);
e4bcb14c 901
9dfd7c7a
GH
902 cyls = qemu_opt_get_number(opts, "cyls", 0);
903 heads = qemu_opt_get_number(opts, "heads", 0);
904 secs = qemu_opt_get_number(opts, "secs", 0);
e4bcb14c 905
9dfd7c7a 906 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
59f2689d 907 ro = qemu_opt_get_bool(opts, "readonly", 0);
e4bcb14c 908
9dfd7c7a
GH
909 file = qemu_opt_get(opts, "file");
910 serial = qemu_opt_get(opts, "serial");
911
912 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
ae45d369 913 pstrcpy(devname, sizeof(devname), buf);
e4bcb14c 914 if (!strcmp(buf, "ide")) {
f60d39bc 915 type = IF_IDE;
e4bcb14c
TS
916 max_devs = MAX_IDE_DEVS;
917 } else if (!strcmp(buf, "scsi")) {
f60d39bc 918 type = IF_SCSI;
e4bcb14c
TS
919 max_devs = MAX_SCSI_DEVS;
920 } else if (!strcmp(buf, "floppy")) {
f60d39bc 921 type = IF_FLOPPY;
e4bcb14c
TS
922 max_devs = 0;
923 } else if (!strcmp(buf, "pflash")) {
f60d39bc 924 type = IF_PFLASH;
e4bcb14c
TS
925 max_devs = 0;
926 } else if (!strcmp(buf, "mtd")) {
f60d39bc 927 type = IF_MTD;
e4bcb14c
TS
928 max_devs = 0;
929 } else if (!strcmp(buf, "sd")) {
f60d39bc 930 type = IF_SD;
e4bcb14c 931 max_devs = 0;
6e02c38d
AL
932 } else if (!strcmp(buf, "virtio")) {
933 type = IF_VIRTIO;
934 max_devs = 0;
62d23efa
AL
935 } else if (!strcmp(buf, "xen")) {
936 type = IF_XEN;
937 max_devs = 0;
a8659e90
GH
938 } else if (!strcmp(buf, "none")) {
939 type = IF_NONE;
940 max_devs = 0;
62d23efa 941 } else {
9dfd7c7a 942 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
751c6a17 943 return NULL;
e4bcb14c
TS
944 }
945 }
946
e4bcb14c 947 if (cyls || heads || secs) {
5afe3f04 948 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
9dfd7c7a 949 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
751c6a17 950 return NULL;
e4bcb14c 951 }
5afe3f04 952 if (heads < 1 || (type == IF_IDE && heads > 16)) {
9dfd7c7a 953 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
751c6a17 954 return NULL;
e4bcb14c 955 }
5afe3f04 956 if (secs < 1 || (type == IF_IDE && secs > 63)) {
9dfd7c7a 957 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
751c6a17 958 return NULL;
e4bcb14c
TS
959 }
960 }
961
9dfd7c7a 962 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
e4bcb14c
TS
963 if (!cyls) {
964 fprintf(stderr,
965 "qemu: '%s' trans must be used with cyls,heads and secs\n",
9dfd7c7a 966 buf);
751c6a17 967 return NULL;
e4bcb14c
TS
968 }
969 if (!strcmp(buf, "none"))
970 translation = BIOS_ATA_TRANSLATION_NONE;
971 else if (!strcmp(buf, "lba"))
972 translation = BIOS_ATA_TRANSLATION_LBA;
973 else if (!strcmp(buf, "auto"))
974 translation = BIOS_ATA_TRANSLATION_AUTO;
975 else {
9dfd7c7a 976 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
751c6a17 977 return NULL;
e4bcb14c
TS
978 }
979 }
980
9dfd7c7a 981 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
e4bcb14c
TS
982 if (!strcmp(buf, "disk")) {
983 media = MEDIA_DISK;
984 } else if (!strcmp(buf, "cdrom")) {
985 if (cyls || secs || heads) {
986 fprintf(stderr,
9dfd7c7a 987 "qemu: '%s' invalid physical CHS format\n", buf);
751c6a17 988 return NULL;
e4bcb14c
TS
989 }
990 media = MEDIA_CDROM;
991 } else {
9dfd7c7a 992 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
751c6a17 993 return NULL;
e4bcb14c
TS
994 }
995 }
996
9dfd7c7a 997 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
9f7965c7 998 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
33f00271 999 cache = 0;
9f7965c7 1000 else if (!strcmp(buf, "writethrough"))
33f00271 1001 cache = 1;
9f7965c7
AL
1002 else if (!strcmp(buf, "writeback"))
1003 cache = 2;
33f00271
AZ
1004 else {
1005 fprintf(stderr, "qemu: invalid cache option\n");
751c6a17 1006 return NULL;
33f00271
AZ
1007 }
1008 }
1009
5c6c3a6c
CH
1010#ifdef CONFIG_LINUX_AIO
1011 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
1012 if (!strcmp(buf, "threads"))
1013 aio = 0;
1014 else if (!strcmp(buf, "native"))
1015 aio = 1;
1016 else {
1017 fprintf(stderr, "qemu: invalid aio option\n");
1018 return NULL;
1019 }
1020 }
1021#endif
1022
9dfd7c7a 1023 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
a1620fac
AJ
1024 if (strcmp(buf, "?") == 0) {
1025 fprintf(stderr, "qemu: Supported formats:");
1026 bdrv_iterate_format(bdrv_format_print, NULL);
1027 fprintf(stderr, "\n");
751c6a17 1028 return NULL;
a1620fac 1029 }
eb852011 1030 drv = bdrv_find_whitelisted_format(buf);
1e72d3b7
AJ
1031 if (!drv) {
1032 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
751c6a17 1033 return NULL;
1e72d3b7
AJ
1034 }
1035 }
1036
e9b2e818 1037 on_write_error = BLOCK_ERR_STOP_ENOSPC;
9dfd7c7a 1038 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
869a5c6d 1039 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
ea8a5d7f 1040 fprintf(stderr, "werror is no supported by this format\n");
751c6a17 1041 return NULL;
428c5705 1042 }
e9b2e818
KW
1043
1044 on_write_error = parse_block_error_action(buf, 0);
1045 if (on_write_error < 0) {
1046 return NULL;
1047 }
1048 }
1049
1050 on_read_error = BLOCK_ERR_REPORT;
1051 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
f35d68f0 1052 if (type != IF_IDE && type != IF_VIRTIO) {
e9b2e818
KW
1053 fprintf(stderr, "rerror is no supported by this format\n");
1054 return NULL;
1055 }
1056
1057 on_read_error = parse_block_error_action(buf, 1);
1058 if (on_read_error < 0) {
751c6a17 1059 return NULL;
428c5705
AL
1060 }
1061 }
1062
9dfd7c7a 1063 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
c2cc47a4 1064 if (type != IF_VIRTIO) {
9dfd7c7a 1065 fprintf(stderr, "addr is not supported\n");
751c6a17 1066 return NULL;
c2cc47a4 1067 }
c2cc47a4
MA
1068 }
1069
e4bcb14c
TS
1070 /* compute bus and unit according index */
1071
1072 if (index != -1) {
1073 if (bus_id != 0 || unit_id != -1) {
1074 fprintf(stderr,
9dfd7c7a 1075 "qemu: index cannot be used with bus and unit\n");
751c6a17 1076 return NULL;
e4bcb14c
TS
1077 }
1078 if (max_devs == 0)
1079 {
1080 unit_id = index;
1081 bus_id = 0;
1082 } else {
1083 unit_id = index % max_devs;
1084 bus_id = index / max_devs;
1085 }
1086 }
1087
1088 /* if user doesn't specify a unit_id,
1089 * try to find the first free
1090 */
1091
1092 if (unit_id == -1) {
1093 unit_id = 0;
751c6a17 1094 while (drive_get(type, bus_id, unit_id) != NULL) {
e4bcb14c
TS
1095 unit_id++;
1096 if (max_devs && unit_id >= max_devs) {
1097 unit_id -= max_devs;
1098 bus_id++;
1099 }
1100 }
1101 }
1102
1103 /* check unit id */
1104
1105 if (max_devs && unit_id >= max_devs) {
9dfd7c7a
GH
1106 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1107 unit_id, max_devs - 1);
751c6a17 1108 return NULL;
e4bcb14c
TS
1109 }
1110
1111 /*
1112 * ignore multiple definitions
1113 */
1114
751c6a17
GH
1115 if (drive_get(type, bus_id, unit_id) != NULL) {
1116 *fatal_error = 0;
1117 return NULL;
1118 }
e4bcb14c
TS
1119
1120 /* init */
1121
751c6a17 1122 dinfo = qemu_mallocz(sizeof(*dinfo));
e23d9c4d 1123 if ((buf = qemu_opts_id(opts)) != NULL) {
9dfd7c7a
GH
1124 dinfo->id = qemu_strdup(buf);
1125 } else {
1dae12e6 1126 /* no id supplied -> create one */
9dfd7c7a 1127 dinfo->id = qemu_mallocz(32);
1dae12e6
GH
1128 if (type == IF_IDE || type == IF_SCSI)
1129 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1130 if (max_devs)
9dfd7c7a 1131 snprintf(dinfo->id, 32, "%s%i%s%i",
1dae12e6
GH
1132 devname, bus_id, mediastr, unit_id);
1133 else
9dfd7c7a 1134 snprintf(dinfo->id, 32, "%s%s%i",
1dae12e6
GH
1135 devname, mediastr, unit_id);
1136 }
1dae12e6 1137 dinfo->bdrv = bdrv_new(dinfo->id);
751c6a17
GH
1138 dinfo->devaddr = devaddr;
1139 dinfo->type = type;
1140 dinfo->bus = bus_id;
1141 dinfo->unit = unit_id;
e9b2e818
KW
1142 dinfo->on_read_error = on_read_error;
1143 dinfo->on_write_error = on_write_error;
9dfd7c7a
GH
1144 dinfo->opts = opts;
1145 if (serial)
1146 strncpy(dinfo->serial, serial, sizeof(serial));
72cf2d4f 1147 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
e4bcb14c 1148
f60d39bc 1149 switch(type) {
e4bcb14c
TS
1150 case IF_IDE:
1151 case IF_SCSI:
62d23efa 1152 case IF_XEN:
c219331e 1153 case IF_NONE:
e4bcb14c
TS
1154 switch(media) {
1155 case MEDIA_DISK:
1156 if (cyls != 0) {
1dae12e6
GH
1157 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1158 bdrv_set_translation_hint(dinfo->bdrv, translation);
e4bcb14c
TS
1159 }
1160 break;
1161 case MEDIA_CDROM:
1dae12e6 1162 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
e4bcb14c
TS
1163 break;
1164 }
1165 break;
1166 case IF_SD:
1167 /* FIXME: This isn't really a floppy, but it's a reasonable
1168 approximation. */
1169 case IF_FLOPPY:
1dae12e6 1170 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
e4bcb14c
TS
1171 break;
1172 case IF_PFLASH:
1173 case IF_MTD:
1174 break;
d176c495
GH
1175 case IF_VIRTIO:
1176 /* add virtio block device */
1177 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1178 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1179 qemu_opt_set(opts, "drive", dinfo->id);
1180 if (devaddr)
1181 qemu_opt_set(opts, "addr", devaddr);
1182 break;
aae9460e
PB
1183 case IF_COUNT:
1184 abort();
e4bcb14c 1185 }
9dfd7c7a 1186 if (!file) {
751c6a17
GH
1187 *fatal_error = 0;
1188 return NULL;
1189 }
33f00271 1190 bdrv_flags = 0;
9f7965c7 1191 if (snapshot) {
33f00271 1192 bdrv_flags |= BDRV_O_SNAPSHOT;
9f7965c7
AL
1193 cache = 2; /* always use write-back with snapshot */
1194 }
1195 if (cache == 0) /* no caching */
1196 bdrv_flags |= BDRV_O_NOCACHE;
1197 else if (cache == 2) /* write-back */
1198 bdrv_flags |= BDRV_O_CACHE_WB;
5c6c3a6c
CH
1199
1200 if (aio == 1) {
1201 bdrv_flags |= BDRV_O_NATIVE_AIO;
1202 } else {
1203 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
1204 }
1205
59f2689d 1206 if (ro == 1) {
f5edb014
NS
1207 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) {
1208 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
59f2689d
NS
1209 return NULL;
1210 }
b196b153
NS
1211 }
1212 /*
1213 * cdrom is read-only. Set it now, after above interface checking
1214 * since readonly attribute not explicitly required, so no error.
1215 */
1216 if (media == MEDIA_CDROM) {
f5edb014 1217 ro = 1;
59f2689d 1218 }
f5edb014 1219 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
59f2689d 1220
1dae12e6 1221 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
850810d0
JF
1222 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1223 file, strerror(errno));
751c6a17 1224 return NULL;
e4bcb14c 1225 }
5c6c3a6c 1226
1dae12e6 1227 if (bdrv_key_required(dinfo->bdrv))
c0f4ce77 1228 autostart = 0;
751c6a17
GH
1229 *fatal_error = 0;
1230 return dinfo;
e4bcb14c
TS
1231}
1232
9dfd7c7a
GH
1233static int drive_init_func(QemuOpts *opts, void *opaque)
1234{
1235 QEMUMachine *machine = opaque;
1236 int fatal_error = 0;
1237
1238 if (drive_init(opts, machine, &fatal_error) == NULL) {
1239 if (fatal_error)
1240 return 1;
1241 }
1242 return 0;
1243}
1244
1245static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1246{
1247 if (NULL == qemu_opt_get(opts, "snapshot")) {
1248 qemu_opt_set(opts, "snapshot", "on");
1249 }
1250 return 0;
1251}
1252
76e30d0f
JK
1253void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1254{
1255 boot_set_handler = func;
1256 boot_set_opaque = opaque;
1257}
1258
1259int qemu_boot_set(const char *boot_devices)
1260{
1261 if (!boot_set_handler) {
1262 return -EINVAL;
1263 }
1264 return boot_set_handler(boot_set_opaque, boot_devices);
1265}
1266
ef3adf68
JK
1267static int parse_bootdevices(char *devices)
1268{
1269 /* We just do some generic consistency checks */
1270 const char *p;
1271 int bitmap = 0;
1272
1273 for (p = devices; *p != '\0'; p++) {
1274 /* Allowed boot devices are:
1275 * a-b: floppy disk drives
1276 * c-f: IDE disk drives
1277 * g-m: machine implementation dependant drives
1278 * n-p: network devices
1279 * It's up to each machine implementation to check if the given boot
1280 * devices match the actual hardware implementation and firmware
1281 * features.
1282 */
1283 if (*p < 'a' || *p > 'p') {
1284 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1285 exit(1);
1286 }
1287 if (bitmap & (1 << (*p - 'a'))) {
1288 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1289 exit(1);
1290 }
1291 bitmap |= 1 << (*p - 'a');
1292 }
1293 return bitmap;
1294}
1295
e0f084bf
JK
1296static void restore_boot_devices(void *opaque)
1297{
1298 char *standard_boot_devices = opaque;
1299
1300 qemu_boot_set(standard_boot_devices);
1301
1302 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1303 qemu_free(standard_boot_devices);
1304}
1305
268a362c
AL
1306static void numa_add(const char *optarg)
1307{
1308 char option[128];
1309 char *endptr;
1310 unsigned long long value, endvalue;
1311 int nodenr;
1312
1313 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1314 if (!strcmp(option, "node")) {
1315 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1316 nodenr = nb_numa_nodes;
1317 } else {
1318 nodenr = strtoull(option, NULL, 10);
1319 }
1320
1321 if (get_param_value(option, 128, "mem", optarg) == 0) {
1322 node_mem[nodenr] = 0;
1323 } else {
1324 value = strtoull(option, &endptr, 0);
1325 switch (*endptr) {
1326 case 0: case 'M': case 'm':
1327 value <<= 20;
1328 break;
1329 case 'G': case 'g':
1330 value <<= 30;
1331 break;
1332 }
1333 node_mem[nodenr] = value;
1334 }
1335 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1336 node_cpumask[nodenr] = 0;
1337 } else {
1338 value = strtoull(option, &endptr, 10);
1339 if (value >= 64) {
1340 value = 63;
1341 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1342 } else {
1343 if (*endptr == '-') {
1344 endvalue = strtoull(endptr+1, &endptr, 10);
1345 if (endvalue >= 63) {
1346 endvalue = 62;
1347 fprintf(stderr,
1348 "only 63 CPUs in NUMA mode supported.\n");
1349 }
0dfbd514 1350 value = (2ULL << endvalue) - (1ULL << value);
268a362c 1351 } else {
0dfbd514 1352 value = 1ULL << value;
268a362c
AL
1353 }
1354 }
1355 node_cpumask[nodenr] = value;
1356 }
1357 nb_numa_nodes++;
1358 }
1359 return;
1360}
1361
dc6b1c09
AP
1362static void smp_parse(const char *optarg)
1363{
1364 int smp, sockets = 0, threads = 0, cores = 0;
1365 char *endptr;
1366 char option[128];
1367
1368 smp = strtoul(optarg, &endptr, 10);
1369 if (endptr != optarg) {
1370 if (*endptr == ',') {
1371 endptr++;
1372 }
1373 }
1374 if (get_param_value(option, 128, "sockets", endptr) != 0)
1375 sockets = strtoull(option, NULL, 10);
1376 if (get_param_value(option, 128, "cores", endptr) != 0)
1377 cores = strtoull(option, NULL, 10);
1378 if (get_param_value(option, 128, "threads", endptr) != 0)
1379 threads = strtoull(option, NULL, 10);
1380 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1381 max_cpus = strtoull(option, NULL, 10);
1382
1383 /* compute missing values, prefer sockets over cores over threads */
1384 if (smp == 0 || sockets == 0) {
1385 sockets = sockets > 0 ? sockets : 1;
1386 cores = cores > 0 ? cores : 1;
1387 threads = threads > 0 ? threads : 1;
1388 if (smp == 0) {
1389 smp = cores * threads * sockets;
dc6b1c09
AP
1390 }
1391 } else {
1392 if (cores == 0) {
1393 threads = threads > 0 ? threads : 1;
1394 cores = smp / (sockets * threads);
1395 } else {
5cdc9b76 1396 if (sockets) {
dc6b1c09
AP
1397 threads = smp / (cores * sockets);
1398 }
1399 }
1400 }
1401 smp_cpus = smp;
1402 smp_cores = cores > 0 ? cores : 1;
1403 smp_threads = threads > 0 ? threads : 1;
1404 if (max_cpus == 0)
1405 max_cpus = smp_cpus;
1406}
1407
a594cfbf
FB
1408/***********************************************************/
1409/* USB devices */
1410
c0f4ce77 1411static int usb_device_add(const char *devname, int is_hotplug)
a594cfbf
FB
1412{
1413 const char *p;
a5d2f727 1414 USBDevice *dev = NULL;
a594cfbf 1415
a5d2f727 1416 if (!usb_enabled)
a594cfbf
FB
1417 return -1;
1418
0958b4cc
GH
1419 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1420 dev = usbdevice_create(devname);
1421 if (dev)
1422 goto done;
1423
a5d2f727 1424 /* the other ones */
a594cfbf
FB
1425 if (strstart(devname, "host:", &p)) {
1426 dev = usb_host_device_open(p);
dc72ac14
AZ
1427 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1428 dev = usb_bt_init(devname[2] ? hci_init(p) :
1429 bt_new_hci(qemu_find_bt_vlan(0)));
a594cfbf
FB
1430 } else {
1431 return -1;
1432 }
0d92ed30
PB
1433 if (!dev)
1434 return -1;
1435
a5d2f727 1436done:
a594cfbf
FB
1437 return 0;
1438}
1439
1f3870ab
AL
1440static int usb_device_del(const char *devname)
1441{
1442 int bus_num, addr;
1443 const char *p;
1444
5d0c5750
AL
1445 if (strstart(devname, "host:", &p))
1446 return usb_host_device_close(p);
1447
a5d2f727 1448 if (!usb_enabled)
1f3870ab
AL
1449 return -1;
1450
1451 p = strchr(devname, '.');
1452 if (!p)
1453 return -1;
1454 bus_num = strtoul(devname, NULL, 0);
1455 addr = strtoul(p + 1, NULL, 0);
1456
a5d2f727 1457 return usb_device_delete_addr(bus_num, addr);
1f3870ab
AL
1458}
1459
bd3c948d
GH
1460static int usb_parse(const char *cmdline)
1461{
59d1c1c2
ST
1462 int r;
1463 r = usb_device_add(cmdline, 0);
1464 if (r < 0) {
1465 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1466 }
1467 return r;
bd3c948d
GH
1468}
1469
d54908a5 1470void do_usb_add(Monitor *mon, const QDict *qdict)
a594cfbf 1471{
59d1c1c2
ST
1472 const char *devname = qdict_get_str(qdict, "devname");
1473 if (usb_device_add(devname, 1) < 0) {
1ecda02b 1474 error_report("could not add USB device '%s'", devname);
59d1c1c2 1475 }
a594cfbf
FB
1476}
1477
d54908a5 1478void do_usb_del(Monitor *mon, const QDict *qdict)
a594cfbf 1479{
59d1c1c2
ST
1480 const char *devname = qdict_get_str(qdict, "devname");
1481 if (usb_device_del(devname) < 0) {
1ecda02b 1482 error_report("could not delete USB device '%s'", devname);
59d1c1c2 1483 }
a594cfbf
FB
1484}
1485
201a51fc
AZ
1486/***********************************************************/
1487/* PCMCIA/Cardbus */
1488
1489static struct pcmcia_socket_entry_s {
bc24a225 1490 PCMCIASocket *socket;
201a51fc
AZ
1491 struct pcmcia_socket_entry_s *next;
1492} *pcmcia_sockets = 0;
1493
bc24a225 1494void pcmcia_socket_register(PCMCIASocket *socket)
201a51fc
AZ
1495{
1496 struct pcmcia_socket_entry_s *entry;
1497
1498 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1499 entry->socket = socket;
1500 entry->next = pcmcia_sockets;
1501 pcmcia_sockets = entry;
1502}
1503
bc24a225 1504void pcmcia_socket_unregister(PCMCIASocket *socket)
201a51fc
AZ
1505{
1506 struct pcmcia_socket_entry_s *entry, **ptr;
1507
1508 ptr = &pcmcia_sockets;
1509 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1510 if (entry->socket == socket) {
1511 *ptr = entry->next;
1512 qemu_free(entry);
1513 }
1514}
1515
376253ec 1516void pcmcia_info(Monitor *mon)
201a51fc
AZ
1517{
1518 struct pcmcia_socket_entry_s *iter;
376253ec 1519
201a51fc 1520 if (!pcmcia_sockets)
376253ec 1521 monitor_printf(mon, "No PCMCIA sockets\n");
201a51fc
AZ
1522
1523 for (iter = pcmcia_sockets; iter; iter = iter->next)
376253ec
AL
1524 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1525 iter->socket->attached ? iter->socket->card_string :
1526 "Empty");
201a51fc
AZ
1527}
1528
2ff89790 1529/***********************************************************/
8a7ddc38 1530/* I/O handling */
0824d6fc 1531
c4b1fcc0
FB
1532typedef struct IOHandlerRecord {
1533 int fd;
7b27a769 1534 IOCanReadHandler *fd_read_poll;
7c9d8e07
FB
1535 IOHandler *fd_read;
1536 IOHandler *fd_write;
cafffd40 1537 int deleted;
c4b1fcc0
FB
1538 void *opaque;
1539 /* temporary data */
1540 struct pollfd *ufd;
31d4ee6c 1541 QLIST_ENTRY(IOHandlerRecord) next;
c4b1fcc0
FB
1542} IOHandlerRecord;
1543
31d4ee6c
JQ
1544static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1545 QLIST_HEAD_INITIALIZER(io_handlers);
1546
c4b1fcc0 1547
7c9d8e07
FB
1548/* XXX: fd_read_poll should be suppressed, but an API change is
1549 necessary in the character devices to suppress fd_can_read(). */
5fafdf24 1550int qemu_set_fd_handler2(int fd,
7b27a769 1551 IOCanReadHandler *fd_read_poll,
5fafdf24
TS
1552 IOHandler *fd_read,
1553 IOHandler *fd_write,
7c9d8e07 1554 void *opaque)
c4b1fcc0 1555{
31d4ee6c 1556 IOHandlerRecord *ioh;
c4b1fcc0 1557
7c9d8e07 1558 if (!fd_read && !fd_write) {
31d4ee6c 1559 QLIST_FOREACH(ioh, &io_handlers, next) {
7c9d8e07 1560 if (ioh->fd == fd) {
cafffd40 1561 ioh->deleted = 1;
7c9d8e07
FB
1562 break;
1563 }
7c9d8e07
FB
1564 }
1565 } else {
31d4ee6c 1566 QLIST_FOREACH(ioh, &io_handlers, next) {
7c9d8e07
FB
1567 if (ioh->fd == fd)
1568 goto found;
1569 }
1570 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
31d4ee6c 1571 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
7c9d8e07
FB
1572 found:
1573 ioh->fd = fd;
1574 ioh->fd_read_poll = fd_read_poll;
1575 ioh->fd_read = fd_read;
1576 ioh->fd_write = fd_write;
1577 ioh->opaque = opaque;
cafffd40 1578 ioh->deleted = 0;
7c9d8e07 1579 }
c4b1fcc0
FB
1580 return 0;
1581}
1582
5fafdf24
TS
1583int qemu_set_fd_handler(int fd,
1584 IOHandler *fd_read,
1585 IOHandler *fd_write,
7c9d8e07 1586 void *opaque)
8a7ddc38 1587{
7c9d8e07 1588 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
8a7ddc38
FB
1589}
1590
56f3a5d0 1591#ifdef _WIN32
f331110f
FB
1592/***********************************************************/
1593/* Polling handling */
1594
1595typedef struct PollingEntry {
1596 PollingFunc *func;
1597 void *opaque;
1598 struct PollingEntry *next;
1599} PollingEntry;
1600
1601static PollingEntry *first_polling_entry;
1602
1603int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1604{
1605 PollingEntry **ppe, *pe;
1606 pe = qemu_mallocz(sizeof(PollingEntry));
f331110f
FB
1607 pe->func = func;
1608 pe->opaque = opaque;
1609 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1610 *ppe = pe;
1611 return 0;
1612}
1613
1614void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1615{
1616 PollingEntry **ppe, *pe;
1617 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1618 pe = *ppe;
1619 if (pe->func == func && pe->opaque == opaque) {
1620 *ppe = pe->next;
1621 qemu_free(pe);
1622 break;
1623 }
1624 }
1625}
1626
a18e524a
FB
1627/***********************************************************/
1628/* Wait objects support */
1629typedef struct WaitObjects {
1630 int num;
1631 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1632 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1633 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1634} WaitObjects;
1635
1636static WaitObjects wait_objects = {0};
3b46e624 1637
a18e524a
FB
1638int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1639{
1640 WaitObjects *w = &wait_objects;
1641
1642 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1643 return -1;
1644 w->events[w->num] = handle;
1645 w->func[w->num] = func;
1646 w->opaque[w->num] = opaque;
1647 w->num++;
1648 return 0;
1649}
1650
1651void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1652{
1653 int i, found;
1654 WaitObjects *w = &wait_objects;
1655
1656 found = 0;
1657 for (i = 0; i < w->num; i++) {
1658 if (w->events[i] == handle)
1659 found = 1;
1660 if (found) {
1661 w->events[i] = w->events[i + 1];
1662 w->func[i] = w->func[i + 1];
1663 w->opaque[i] = w->opaque[i + 1];
3b46e624 1664 }
a18e524a
FB
1665 }
1666 if (found)
1667 w->num--;
1668}
1669#endif
1670
8a7ddc38
FB
1671/***********************************************************/
1672/* ram save/restore */
1673
94fb0909 1674#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
475e4277
AL
1675#define RAM_SAVE_FLAG_COMPRESS 0x02
1676#define RAM_SAVE_FLAG_MEM_SIZE 0x04
1677#define RAM_SAVE_FLAG_PAGE 0x08
1678#define RAM_SAVE_FLAG_EOS 0x10
1679
1680static int is_dup_page(uint8_t *page, uint8_t ch)
8a7ddc38 1681{
475e4277
AL
1682 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
1683 uint32_t *array = (uint32_t *)page;
1684 int i;
3b46e624 1685
475e4277
AL
1686 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
1687 if (array[i] != val)
1688 return 0;
1689 }
1690
1691 return 1;
1692}
1693
1694static int ram_save_block(QEMUFile *f)
1695{
c227f099
AL
1696 static ram_addr_t current_addr = 0;
1697 ram_addr_t saved_addr = current_addr;
1698 ram_addr_t addr = 0;
475e4277
AL
1699 int found = 0;
1700
94a6b54f 1701 while (addr < last_ram_offset) {
475e4277 1702 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
5579c7f3 1703 uint8_t *p;
475e4277
AL
1704
1705 cpu_physical_memory_reset_dirty(current_addr,
1706 current_addr + TARGET_PAGE_SIZE,
1707 MIGRATION_DIRTY_FLAG);
1708
5579c7f3 1709 p = qemu_get_ram_ptr(current_addr);
475e4277 1710
5579c7f3 1711 if (is_dup_page(p, *p)) {
475e4277 1712 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
5579c7f3 1713 qemu_put_byte(f, *p);
475e4277
AL
1714 } else {
1715 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
5579c7f3 1716 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
c88676f8 1717 }
475e4277
AL
1718
1719 found = 1;
1720 break;
c88676f8 1721 }
475e4277 1722 addr += TARGET_PAGE_SIZE;
94a6b54f 1723 current_addr = (saved_addr + addr) % last_ram_offset;
8a7ddc38 1724 }
475e4277
AL
1725
1726 return found;
8a7ddc38
FB
1727}
1728
84307938 1729static uint64_t bytes_transferred;
475e4277 1730
c227f099 1731static ram_addr_t ram_save_remaining(void)
475e4277 1732{
c227f099
AL
1733 ram_addr_t addr;
1734 ram_addr_t count = 0;
475e4277 1735
94a6b54f 1736 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
475e4277
AL
1737 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1738 count++;
1739 }
1740
1741 return count;
1742}
1743
9f9e28cd
GC
1744uint64_t ram_bytes_remaining(void)
1745{
1746 return ram_save_remaining() * TARGET_PAGE_SIZE;
1747}
1748
1749uint64_t ram_bytes_transferred(void)
1750{
1751 return bytes_transferred;
1752}
1753
1754uint64_t ram_bytes_total(void)
1755{
1756 return last_ram_offset;
1757}
1758
f327aa0c 1759static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
475e4277 1760{
c227f099 1761 ram_addr_t addr;
a0a3fd60
GC
1762 uint64_t bytes_transferred_last;
1763 double bwidth = 0;
1764 uint64_t expected_time = 0;
475e4277 1765
4ec7fcc7
JK
1766 if (stage < 0) {
1767 cpu_physical_memory_set_dirty_tracking(0);
1768 return 0;
1769 }
1770
9fa06385 1771 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
b0a46a33
JK
1772 qemu_file_set_error(f);
1773 return 0;
1774 }
1775
475e4277 1776 if (stage == 1) {
84307938
JK
1777 bytes_transferred = 0;
1778
475e4277 1779 /* Make sure all dirty bits are set */
94a6b54f 1780 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
475e4277
AL
1781 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
1782 cpu_physical_memory_set_dirty(addr);
1783 }
b0a46a33 1784
475e4277
AL
1785 /* Enable dirty memory tracking */
1786 cpu_physical_memory_set_dirty_tracking(1);
1787
94a6b54f 1788 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
475e4277
AL
1789 }
1790
a0a3fd60 1791 bytes_transferred_last = bytes_transferred;
41c872b6 1792 bwidth = qemu_get_clock_ns(rt_clock);
a0a3fd60 1793
475e4277
AL
1794 while (!qemu_file_rate_limit(f)) {
1795 int ret;
1796
1797 ret = ram_save_block(f);
9f9e28cd 1798 bytes_transferred += ret * TARGET_PAGE_SIZE;
475e4277
AL
1799 if (ret == 0) /* no more blocks */
1800 break;
1801 }
1802
41c872b6 1803 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
a0a3fd60
GC
1804 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
1805
1806 /* if we haven't transferred anything this round, force expected_time to a
1807 * a very high value, but without crashing */
1808 if (bwidth == 0)
1809 bwidth = 0.000001;
1810
475e4277 1811 /* try transferring iterative blocks of memory */
475e4277 1812 if (stage == 3) {
475e4277 1813 /* flush all remaining blocks regardless of rate limiting */
9f9e28cd
GC
1814 while (ram_save_block(f) != 0) {
1815 bytes_transferred += TARGET_PAGE_SIZE;
1816 }
8215e914 1817 cpu_physical_memory_set_dirty_tracking(0);
475e4277
AL
1818 }
1819
1820 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1821
a0a3fd60
GC
1822 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
1823
1824 return (stage == 2) && (expected_time <= migrate_max_downtime());
475e4277
AL
1825}
1826
475e4277
AL
1827static int ram_load(QEMUFile *f, void *opaque, int version_id)
1828{
c227f099 1829 ram_addr_t addr;
475e4277
AL
1830 int flags;
1831
475e4277
AL
1832 if (version_id != 3)
1833 return -EINVAL;
1834
1835 do {
1836 addr = qemu_get_be64(f);
1837
1838 flags = addr & ~TARGET_PAGE_MASK;
1839 addr &= TARGET_PAGE_MASK;
1840
1841 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
94a6b54f 1842 if (addr != last_ram_offset)
475e4277
AL
1843 return -EINVAL;
1844 }
1845
475e4277
AL
1846 if (flags & RAM_SAVE_FLAG_COMPRESS) {
1847 uint8_t ch = qemu_get_byte(f);
779c6bef
AL
1848 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
1849#ifndef _WIN32
30868442
AL
1850 if (ch == 0 &&
1851 (!kvm_enabled() || kvm_has_sync_mmu())) {
1852 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
779c6bef 1853 }
30868442 1854#endif
9a743e5b 1855 } else if (flags & RAM_SAVE_FLAG_PAGE) {
5579c7f3 1856 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
9a743e5b
JK
1857 }
1858 if (qemu_file_has_error(f)) {
1859 return -EIO;
1860 }
475e4277
AL
1861 } while (!(flags & RAM_SAVE_FLAG_EOS));
1862
8a7ddc38
FB
1863 return 0;
1864}
1865
9e472e10
AL
1866void qemu_service_io(void)
1867{
d9f75a4e 1868 qemu_notify_event();
9e472e10
AL
1869}
1870
cc1daa40
FB
1871/***********************************************************/
1872/* machine registration */
1873
bdaf78e0 1874static QEMUMachine *first_machine = NULL;
6f338c34 1875QEMUMachine *current_machine = NULL;
cc1daa40
FB
1876
1877int qemu_register_machine(QEMUMachine *m)
1878{
1879 QEMUMachine **pm;
1880 pm = &first_machine;
1881 while (*pm != NULL)
1882 pm = &(*pm)->next;
1883 m->next = NULL;
1884 *pm = m;
1885 return 0;
1886}
1887
9596ebb7 1888static QEMUMachine *find_machine(const char *name)
cc1daa40
FB
1889{
1890 QEMUMachine *m;
1891
1892 for(m = first_machine; m != NULL; m = m->next) {
1893 if (!strcmp(m->name, name))
1894 return m;
3f6599e6
MM
1895 if (m->alias && !strcmp(m->alias, name))
1896 return m;
cc1daa40
FB
1897 }
1898 return NULL;
1899}
1900
0c257437
AL
1901static QEMUMachine *find_default_machine(void)
1902{
1903 QEMUMachine *m;
1904
1905 for(m = first_machine; m != NULL; m = m->next) {
1906 if (m->is_default) {
1907 return m;
1908 }
1909 }
1910 return NULL;
1911}
1912
8a7ddc38
FB
1913/***********************************************************/
1914/* main execution loop */
1915
9596ebb7 1916static void gui_update(void *opaque)
8a7ddc38 1917{
7d957bd8 1918 uint64_t interval = GUI_REFRESH_INTERVAL;
740733bb 1919 DisplayState *ds = opaque;
7d957bd8
AL
1920 DisplayChangeListener *dcl = ds->listeners;
1921
62a2744c 1922 qemu_flush_coalesced_mmio_buffer();
7d957bd8
AL
1923 dpy_refresh(ds);
1924
1925 while (dcl != NULL) {
1926 if (dcl->gui_timer_interval &&
1927 dcl->gui_timer_interval < interval)
1928 interval = dcl->gui_timer_interval;
1929 dcl = dcl->next;
1930 }
1931 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
8a7ddc38
FB
1932}
1933
9043b62d
BS
1934static void nographic_update(void *opaque)
1935{
1936 uint64_t interval = GUI_REFRESH_INTERVAL;
1937
62a2744c 1938 qemu_flush_coalesced_mmio_buffer();
9043b62d
BS
1939 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1940}
1941
0bd48850
FB
1942struct vm_change_state_entry {
1943 VMChangeStateHandler *cb;
1944 void *opaque;
72cf2d4f 1945 QLIST_ENTRY (vm_change_state_entry) entries;
0bd48850
FB
1946};
1947
72cf2d4f 1948static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
0bd48850
FB
1949
1950VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1951 void *opaque)
1952{
1953 VMChangeStateEntry *e;
1954
1955 e = qemu_mallocz(sizeof (*e));
0bd48850
FB
1956
1957 e->cb = cb;
1958 e->opaque = opaque;
72cf2d4f 1959 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
0bd48850
FB
1960 return e;
1961}
1962
1963void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1964{
72cf2d4f 1965 QLIST_REMOVE (e, entries);
0bd48850
FB
1966 qemu_free (e);
1967}
1968
296af7c9 1969void vm_state_notify(int running, int reason)
0bd48850
FB
1970{
1971 VMChangeStateEntry *e;
1972
1973 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
9781e040 1974 e->cb(e->opaque, running, reason);
0bd48850
FB
1975 }
1976}
1977
8a7ddc38
FB
1978void vm_start(void)
1979{
1980 if (!vm_running) {
1981 cpu_enable_ticks();
1982 vm_running = 1;
9781e040 1983 vm_state_notify(1, 0);
d6dc3d42 1984 resume_all_vcpus();
8a7ddc38
FB
1985 }
1986}
1987
bb0c6722
FB
1988/* reset/shutdown handler */
1989
1990typedef struct QEMUResetEntry {
72cf2d4f 1991 QTAILQ_ENTRY(QEMUResetEntry) entry;
bb0c6722
FB
1992 QEMUResetHandler *func;
1993 void *opaque;
bb0c6722
FB
1994} QEMUResetEntry;
1995
72cf2d4f
BS
1996static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1997 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bb0c6722
FB
1998static int reset_requested;
1999static int shutdown_requested;
3475187d 2000static int powerdown_requested;
296af7c9 2001int debug_requested;
6e29f5da 2002static int vmstop_requested;
bb0c6722 2003
cf7a2fe2
AJ
2004int qemu_shutdown_requested(void)
2005{
2006 int r = shutdown_requested;
2007 shutdown_requested = 0;
2008 return r;
2009}
2010
2011int qemu_reset_requested(void)
2012{
2013 int r = reset_requested;
2014 reset_requested = 0;
2015 return r;
2016}
2017
2018int qemu_powerdown_requested(void)
2019{
2020 int r = powerdown_requested;
2021 powerdown_requested = 0;
2022 return r;
2023}
2024
e568902a
AL
2025static int qemu_debug_requested(void)
2026{
2027 int r = debug_requested;
2028 debug_requested = 0;
2029 return r;
2030}
2031
6e29f5da
AL
2032static int qemu_vmstop_requested(void)
2033{
2034 int r = vmstop_requested;
2035 vmstop_requested = 0;
2036 return r;
2037}
2038
a08d4367 2039void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bb0c6722 2040{
55ddfe8e 2041 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bb0c6722 2042
bb0c6722
FB
2043 re->func = func;
2044 re->opaque = opaque;
72cf2d4f 2045 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bb0c6722
FB
2046}
2047
dda9b29f 2048void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bb0c6722
FB
2049{
2050 QEMUResetEntry *re;
2051
72cf2d4f 2052 QTAILQ_FOREACH(re, &reset_handlers, entry) {
dda9b29f 2053 if (re->func == func && re->opaque == opaque) {
72cf2d4f 2054 QTAILQ_REMOVE(&reset_handlers, re, entry);
dda9b29f
JK
2055 qemu_free(re);
2056 return;
2057 }
2058 }
2059}
2060
2061void qemu_system_reset(void)
2062{
2063 QEMUResetEntry *re, *nre;
2064
2065 /* reset all devices */
72cf2d4f 2066 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bb0c6722
FB
2067 re->func(re->opaque);
2068 }
81d9b784 2069 monitor_protocol_event(QEVENT_RESET, NULL);
ea375f9a 2070 cpu_synchronize_all_post_reset();
bb0c6722
FB
2071}
2072
2073void qemu_system_reset_request(void)
2074{
d1beab82
FB
2075 if (no_reboot) {
2076 shutdown_requested = 1;
2077 } else {
2078 reset_requested = 1;
2079 }
d9f75a4e 2080 qemu_notify_event();
bb0c6722
FB
2081}
2082
2083void qemu_system_shutdown_request(void)
2084{
2085 shutdown_requested = 1;
d9f75a4e 2086 qemu_notify_event();
bb0c6722
FB
2087}
2088
3475187d
FB
2089void qemu_system_powerdown_request(void)
2090{
2091 powerdown_requested = 1;
d9f75a4e
AL
2092 qemu_notify_event();
2093}
2094
877cf882 2095#ifdef _WIN32
69d6451c 2096static void host_main_loop_wait(int *timeout)
56f3a5d0
AL
2097{
2098 int ret, ret2, i;
f331110f
FB
2099 PollingEntry *pe;
2100
c4b1fcc0 2101
f331110f
FB
2102 /* XXX: need to suppress polling by better using win32 events */
2103 ret = 0;
2104 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
2105 ret |= pe->func(pe->opaque);
2106 }
e6b1e558 2107 if (ret == 0) {
a18e524a
FB
2108 int err;
2109 WaitObjects *w = &wait_objects;
3b46e624 2110
56f3a5d0 2111 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
a18e524a
FB
2112 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
2113 if (w->func[ret - WAIT_OBJECT_0])
2114 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
3b46e624 2115
5fafdf24 2116 /* Check for additional signaled events */
e6b1e558 2117 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
3b46e624 2118
e6b1e558
TS
2119 /* Check if event is signaled */
2120 ret2 = WaitForSingleObject(w->events[i], 0);
2121 if(ret2 == WAIT_OBJECT_0) {
2122 if (w->func[i])
2123 w->func[i](w->opaque[i]);
2124 } else if (ret2 == WAIT_TIMEOUT) {
2125 } else {
2126 err = GetLastError();
2127 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
3b46e624
TS
2128 }
2129 }
a18e524a
FB
2130 } else if (ret == WAIT_TIMEOUT) {
2131 } else {
2132 err = GetLastError();
e6b1e558 2133 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
a18e524a 2134 }
f331110f 2135 }
56f3a5d0
AL
2136
2137 *timeout = 0;
2138}
2139#else
69d6451c 2140static void host_main_loop_wait(int *timeout)
56f3a5d0
AL
2141{
2142}
fd1dff4b 2143#endif
56f3a5d0 2144
d6f4ade2 2145void main_loop_wait(int nonblocking)
56f3a5d0
AL
2146{
2147 IOHandlerRecord *ioh;
2148 fd_set rfds, wfds, xfds;
2149 int ret, nfds;
2150 struct timeval tv;
d6f4ade2 2151 int timeout;
56f3a5d0 2152
d6f4ade2
PB
2153 if (nonblocking)
2154 timeout = 0;
2155 else {
2156 timeout = qemu_calculate_timeout();
2157 qemu_bh_update_timeout(&timeout);
2158 }
56f3a5d0
AL
2159
2160 host_main_loop_wait(&timeout);
2161
fd1dff4b
FB
2162 /* poll any events */
2163 /* XXX: separate device handlers from system ones */
6abfbd79 2164 nfds = -1;
fd1dff4b
FB
2165 FD_ZERO(&rfds);
2166 FD_ZERO(&wfds);
e035649e 2167 FD_ZERO(&xfds);
31d4ee6c 2168 QLIST_FOREACH(ioh, &io_handlers, next) {
cafffd40
TS
2169 if (ioh->deleted)
2170 continue;
fd1dff4b
FB
2171 if (ioh->fd_read &&
2172 (!ioh->fd_read_poll ||
2173 ioh->fd_read_poll(ioh->opaque) != 0)) {
2174 FD_SET(ioh->fd, &rfds);
2175 if (ioh->fd > nfds)
2176 nfds = ioh->fd;
2177 }
2178 if (ioh->fd_write) {
2179 FD_SET(ioh->fd, &wfds);
2180 if (ioh->fd > nfds)
2181 nfds = ioh->fd;
2182 }
2183 }
3b46e624 2184
56f3a5d0
AL
2185 tv.tv_sec = timeout / 1000;
2186 tv.tv_usec = (timeout % 1000) * 1000;
2187
d918f23e
JK
2188 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2189
4870852c 2190 qemu_mutex_unlock_iothread();
e035649e 2191 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4870852c 2192 qemu_mutex_lock_iothread();
fd1dff4b 2193 if (ret > 0) {
31d4ee6c 2194 IOHandlerRecord *pioh;
cafffd40 2195
31d4ee6c 2196 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
cafffd40 2197 if (ioh->deleted) {
31d4ee6c 2198 QLIST_REMOVE(ioh, next);
cafffd40 2199 qemu_free(ioh);
4bed9837
JQ
2200 continue;
2201 }
2202 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
2203 ioh->fd_read(ioh->opaque);
2204 }
2205 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
2206 ioh->fd_write(ioh->opaque);
31d4ee6c 2207 }
cafffd40 2208 }
fd1dff4b 2209 }
d918f23e
JK
2210
2211 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
b4608c04 2212
b6964827 2213 qemu_run_all_timers();
21d5d12b 2214
423f0742
PB
2215 /* Check bottom-halves last in case any of the earlier events triggered
2216 them. */
2217 qemu_bh_poll();
3b46e624 2218
5905b2e5
FB
2219}
2220
43b96858
AL
2221static int vm_can_run(void)
2222{
2223 if (powerdown_requested)
2224 return 0;
2225 if (reset_requested)
2226 return 0;
2227 if (shutdown_requested)
2228 return 0;
e568902a
AL
2229 if (debug_requested)
2230 return 0;
43b96858
AL
2231 return 1;
2232}
2233
d9c32310
BS
2234qemu_irq qemu_system_powerdown;
2235
43b96858
AL
2236static void main_loop(void)
2237{
6e29f5da 2238 int r;
e6e35b1e 2239
d6dc3d42
AL
2240#ifdef CONFIG_IOTHREAD
2241 qemu_system_ready = 1;
2242 qemu_cond_broadcast(&qemu_system_cond);
2243#endif
2244
6e29f5da 2245 for (;;) {
43b96858 2246 do {
d6f4ade2 2247 bool nonblocking = false;
e6e35b1e
AL
2248#ifdef CONFIG_PROFILER
2249 int64_t ti;
2250#endif
d6dc3d42 2251#ifndef CONFIG_IOTHREAD
d6f4ade2 2252 nonblocking = tcg_cpu_exec();
d6dc3d42 2253#endif
89bfc105 2254#ifdef CONFIG_PROFILER
43b96858 2255 ti = profile_getclock();
89bfc105 2256#endif
d6f4ade2 2257 main_loop_wait(nonblocking);
89bfc105 2258#ifdef CONFIG_PROFILER
43b96858 2259 dev_time += profile_getclock() - ti;
89bfc105 2260#endif
e568902a 2261 } while (vm_can_run());
43b96858 2262
54fc6ea9
BS
2263 if ((r = qemu_debug_requested())) {
2264 vm_stop(r);
b1a15e7e 2265 }
43b96858 2266 if (qemu_shutdown_requested()) {
242cd003 2267 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
43b96858
AL
2268 if (no_shutdown) {
2269 vm_stop(0);
2270 no_shutdown = 0;
2271 } else
2272 break;
2273 }
d6dc3d42
AL
2274 if (qemu_reset_requested()) {
2275 pause_all_vcpus();
43b96858 2276 qemu_system_reset();
d6dc3d42
AL
2277 resume_all_vcpus();
2278 }
d9c32310 2279 if (qemu_powerdown_requested()) {
242cd003 2280 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
d9c32310
BS
2281 qemu_irq_raise(qemu_system_powerdown);
2282 }
b1a15e7e 2283 if ((r = qemu_vmstop_requested())) {
6e29f5da 2284 vm_stop(r);
b1a15e7e 2285 }
b4608c04 2286 }
d6dc3d42 2287 pause_all_vcpus();
b4608c04
FB
2288}
2289
9bd7e6d9
PB
2290static void version(void)
2291{
4a19f1ec 2292 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
9bd7e6d9
PB
2293}
2294
15f82208 2295static void help(int exitcode)
0824d6fc 2296{
e8105ebb 2297 const char *options_help =
5824d651
BS
2298#define DEF(option, opt_arg, opt_enum, opt_help) \
2299 opt_help
2300#define DEFHEADING(text) stringify(text) "\n"
2301#include "qemu-options.h"
2302#undef DEF
2303#undef DEFHEADING
2304#undef GEN_DOCS
e8105ebb
PB
2305 ;
2306 version();
2307 printf("usage: %s [options] [disk_image]\n"
2308 "\n"
2309 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3f020d70 2310 "\n"
e8105ebb 2311 "%s\n"
3f020d70 2312 "During emulation, the following keys are useful:\n"
2313 "ctrl-alt-f toggle full screen\n"
2314 "ctrl-alt-n switch to virtual console 'n'\n"
2315 "ctrl-alt toggle mouse and keyboard grab\n"
2316 "\n"
e8105ebb
PB
2317 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2318 "qemu",
2319 options_help);
15f82208 2320 exit(exitcode);
0824d6fc
FB
2321}
2322
cd6f1169
FB
2323#define HAS_ARG 0x0001
2324
2325enum {
5824d651
BS
2326#define DEF(option, opt_arg, opt_enum, opt_help) \
2327 opt_enum,
2328#define DEFHEADING(text)
2329#include "qemu-options.h"
2330#undef DEF
2331#undef DEFHEADING
2332#undef GEN_DOCS
cd6f1169
FB
2333};
2334
2335typedef struct QEMUOption {
2336 const char *name;
2337 int flags;
2338 int index;
2339} QEMUOption;
2340
dbed7e40 2341static const QEMUOption qemu_options[] = {
cd6f1169 2342 { "h", 0, QEMU_OPTION_h },
5824d651
BS
2343#define DEF(option, opt_arg, opt_enum, opt_help) \
2344 { option, opt_arg, opt_enum },
2345#define DEFHEADING(text)
2346#include "qemu-options.h"
2347#undef DEF
2348#undef DEFHEADING
2349#undef GEN_DOCS
cd6f1169 2350 { NULL },
fc01f7e7
FB
2351};
2352
1d14ffa9 2353#ifdef HAS_AUDIO
6a36d84e 2354struct soundhw soundhw[] = {
b00052e4 2355#ifdef HAS_AUDIO_CHOICE
4ce7ff6e 2356#if defined(TARGET_I386) || defined(TARGET_MIPS)
fd06c375
FB
2357 {
2358 "pcspk",
2359 "PC speaker",
2360 0,
2361 1,
2362 { .init_isa = pcspk_audio_init }
2363 },
2364#endif
4c9b53e3 2365
2366#ifdef CONFIG_SB16
6a36d84e
FB
2367 {
2368 "sb16",
2369 "Creative Sound Blaster 16",
2370 0,
2371 1,
2372 { .init_isa = SB16_init }
2373 },
4c9b53e3 2374#endif
6a36d84e 2375
cc53d26d 2376#ifdef CONFIG_CS4231A
2377 {
2378 "cs4231a",
2379 "CS4231A",
2380 0,
2381 1,
2382 { .init_isa = cs4231a_init }
2383 },
2384#endif
2385
1d14ffa9 2386#ifdef CONFIG_ADLIB
6a36d84e
FB
2387 {
2388 "adlib",
1d14ffa9 2389#ifdef HAS_YMF262
6a36d84e 2390 "Yamaha YMF262 (OPL3)",
1d14ffa9 2391#else
6a36d84e 2392 "Yamaha YM3812 (OPL2)",
1d14ffa9 2393#endif
6a36d84e
FB
2394 0,
2395 1,
2396 { .init_isa = Adlib_init }
2397 },
1d14ffa9 2398#endif
6a36d84e 2399
1d14ffa9 2400#ifdef CONFIG_GUS
6a36d84e
FB
2401 {
2402 "gus",
2403 "Gravis Ultrasound GF1",
2404 0,
2405 1,
2406 { .init_isa = GUS_init }
2407 },
1d14ffa9 2408#endif
6a36d84e 2409
4c9b53e3 2410#ifdef CONFIG_AC97
e5c9a13e
AZ
2411 {
2412 "ac97",
2413 "Intel 82801AA AC97 Audio",
2414 0,
2415 0,
2416 { .init_pci = ac97_init }
2417 },
4c9b53e3 2418#endif
e5c9a13e 2419
4c9b53e3 2420#ifdef CONFIG_ES1370
6a36d84e
FB
2421 {
2422 "es1370",
2423 "ENSONIQ AudioPCI ES1370",
2424 0,
2425 0,
2426 { .init_pci = es1370_init }
2427 },
b00052e4 2428#endif
6a36d84e 2429
4c9b53e3 2430#endif /* HAS_AUDIO_CHOICE */
2431
6a36d84e
FB
2432 { NULL, NULL, 0, 0, { NULL } }
2433};
2434
2435static void select_soundhw (const char *optarg)
2436{
2437 struct soundhw *c;
2438
2439 if (*optarg == '?') {
2440 show_valid_cards:
2441
2442 printf ("Valid sound card names (comma separated):\n");
2443 for (c = soundhw; c->name; ++c) {
2444 printf ("%-11s %s\n", c->name, c->descr);
2445 }
2446 printf ("\n-soundhw all will enable all of the above\n");
1d14ffa9
FB
2447 exit (*optarg != '?');
2448 }
2449 else {
6a36d84e 2450 size_t l;
1d14ffa9
FB
2451 const char *p;
2452 char *e;
2453 int bad_card = 0;
2454
6a36d84e
FB
2455 if (!strcmp (optarg, "all")) {
2456 for (c = soundhw; c->name; ++c) {
2457 c->enabled = 1;
2458 }
2459 return;
2460 }
1d14ffa9 2461
6a36d84e 2462 p = optarg;
1d14ffa9
FB
2463 while (*p) {
2464 e = strchr (p, ',');
2465 l = !e ? strlen (p) : (size_t) (e - p);
6a36d84e
FB
2466
2467 for (c = soundhw; c->name; ++c) {
b3d6fb4a 2468 if (!strncmp (c->name, p, l) && !c->name[l]) {
6a36d84e 2469 c->enabled = 1;
1d14ffa9
FB
2470 break;
2471 }
2472 }
6a36d84e
FB
2473
2474 if (!c->name) {
1d14ffa9
FB
2475 if (l > 80) {
2476 fprintf (stderr,
2477 "Unknown sound card name (too big to show)\n");
2478 }
2479 else {
2480 fprintf (stderr, "Unknown sound card name `%.*s'\n",
2481 (int) l, p);
2482 }
2483 bad_card = 1;
2484 }
2485 p += l + (e != NULL);
2486 }
2487
2488 if (bad_card)
2489 goto show_valid_cards;
2490 }
2491}
2492#endif
2493
3893c124 2494static void select_vgahw (const char *p)
2495{
2496 const char *opts;
2497
64465297 2498 default_vga = 0;
86176759 2499 vga_interface_type = VGA_NONE;
3893c124 2500 if (strstart(p, "std", &opts)) {
86176759 2501 vga_interface_type = VGA_STD;
3893c124 2502 } else if (strstart(p, "cirrus", &opts)) {
86176759 2503 vga_interface_type = VGA_CIRRUS;
3893c124 2504 } else if (strstart(p, "vmware", &opts)) {
86176759 2505 vga_interface_type = VGA_VMWARE;
94909d9f 2506 } else if (strstart(p, "xenfb", &opts)) {
86176759 2507 vga_interface_type = VGA_XENFB;
28b85ed8 2508 } else if (!strstart(p, "none", &opts)) {
3893c124 2509 invalid_vga:
2510 fprintf(stderr, "Unknown vga type: %s\n", p);
2511 exit(1);
2512 }
cb5a7aa8 2513 while (*opts) {
2514 const char *nextopt;
2515
2516 if (strstart(opts, ",retrace=", &nextopt)) {
2517 opts = nextopt;
2518 if (strstart(opts, "dumb", &nextopt))
2519 vga_retrace_method = VGA_RETRACE_DUMB;
2520 else if (strstart(opts, "precise", &nextopt))
2521 vga_retrace_method = VGA_RETRACE_PRECISE;
2522 else goto invalid_vga;
2523 } else goto invalid_vga;
2524 opts = nextopt;
2525 }
3893c124 2526}
2527
7d4c3d53
MA
2528#ifdef TARGET_I386
2529static int balloon_parse(const char *arg)
2530{
382f0743 2531 QemuOpts *opts;
7d4c3d53 2532
382f0743
GH
2533 if (strcmp(arg, "none") == 0) {
2534 return 0;
2535 }
2536
2537 if (!strncmp(arg, "virtio", 6)) {
2538 if (arg[6] == ',') {
2539 /* have params -> parse them */
8212c64f 2540 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
382f0743
GH
2541 if (!opts)
2542 return -1;
2543 } else {
2544 /* create empty opts */
2545 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
7d4c3d53 2546 }
382f0743
GH
2547 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
2548 return 0;
7d4c3d53 2549 }
382f0743
GH
2550
2551 return -1;
7d4c3d53
MA
2552}
2553#endif
2554
3587d7e6
FB
2555#ifdef _WIN32
2556static BOOL WINAPI qemu_ctrl_handler(DWORD type)
2557{
2558 exit(STATUS_CONTROL_C_EXIT);
2559 return TRUE;
2560}
2561#endif
2562
c4be29ff 2563int qemu_uuid_parse(const char *str, uint8_t *uuid)
8fcb1b90
BS
2564{
2565 int ret;
2566
2567 if(strlen(str) != 36)
2568 return -1;
2569
2570 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
2571 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
2572 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
2573
2574 if(ret != 16)
2575 return -1;
2576
b6f6e3d3
AL
2577#ifdef TARGET_I386
2578 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
2579#endif
2580
8fcb1b90
BS
2581 return 0;
2582}
2583
de06f8d1
BS
2584#ifdef TARGET_I386
2585static void do_acpitable_option(const char *optarg)
2586{
2587 if (acpi_table_add(optarg) < 0) {
2588 fprintf(stderr, "Wrong acpi table provided\n");
2589 exit(1);
2590 }
2591}
2592#endif
2593
2594#ifdef TARGET_I386
2595static void do_smbios_option(const char *optarg)
2596{
2597 if (smbios_entry_add(optarg) < 0) {
2598 fprintf(stderr, "Wrong smbios provided\n");
2599 exit(1);
2600 }
2601}
2602#endif
2603
2604static void cpudef_init(void)
2605{
2606#if defined(cpudef_setup)
2607 cpudef_setup(); /* parse cpu definitions in target config file */
2608#endif
2609}
2610
5b08fc10
AL
2611#ifndef _WIN32
2612
2613static void termsig_handler(int signal)
2614{
2615 qemu_system_shutdown_request();
2616}
2617
7c3370d4
JK
2618static void sigchld_handler(int signal)
2619{
2620 waitpid(-1, NULL, WNOHANG);
2621}
2622
2623static void sighandler_setup(void)
5b08fc10
AL
2624{
2625 struct sigaction act;
2626
2627 memset(&act, 0, sizeof(act));
2628 act.sa_handler = termsig_handler;
2629 sigaction(SIGINT, &act, NULL);
2630 sigaction(SIGHUP, &act, NULL);
2631 sigaction(SIGTERM, &act, NULL);
7c3370d4
JK
2632
2633 act.sa_handler = sigchld_handler;
2634 act.sa_flags = SA_NOCLDSTOP;
2635 sigaction(SIGCHLD, &act, NULL);
5b08fc10
AL
2636}
2637
2638#endif
2639
5cea8590
PB
2640#ifdef _WIN32
2641/* Look for support files in the same directory as the executable. */
2642static char *find_datadir(const char *argv0)
2643{
2644 char *p;
2645 char buf[MAX_PATH];
2646 DWORD len;
2647
2648 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2649 if (len == 0) {
c5947808 2650 return NULL;
5cea8590
PB
2651 }
2652
2653 buf[len] = 0;
2654 p = buf + len - 1;
2655 while (p != buf && *p != '\\')
2656 p--;
2657 *p = 0;
2658 if (access(buf, R_OK) == 0) {
2659 return qemu_strdup(buf);
2660 }
2661 return NULL;
2662}
2663#else /* !_WIN32 */
2664
2665/* Find a likely location for support files using the location of the binary.
2666 For installed binaries this will be "$bindir/../share/qemu". When
2667 running from the build tree this will be "$bindir/../pc-bios". */
2668#define SHARE_SUFFIX "/share/qemu"
2669#define BUILD_SUFFIX "/pc-bios"
2670static char *find_datadir(const char *argv0)
2671{
2672 char *dir;
2673 char *p = NULL;
2674 char *res;
5cea8590 2675 char buf[PATH_MAX];
3a41759d 2676 size_t max_len;
5cea8590
PB
2677
2678#if defined(__linux__)
2679 {
2680 int len;
2681 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2682 if (len > 0) {
2683 buf[len] = 0;
2684 p = buf;
2685 }
2686 }
2687#elif defined(__FreeBSD__)
2688 {
2689 int len;
2690 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
2691 if (len > 0) {
2692 buf[len] = 0;
2693 p = buf;
2694 }
2695 }
2696#endif
2697 /* If we don't have any way of figuring out the actual executable
2698 location then try argv[0]. */
2699 if (!p) {
4d224196 2700 p = realpath(argv0, buf);
5cea8590
PB
2701 if (!p) {
2702 return NULL;
2703 }
2704 }
2705 dir = dirname(p);
2706 dir = dirname(dir);
2707
3a41759d
BS
2708 max_len = strlen(dir) +
2709 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
2710 res = qemu_mallocz(max_len);
2711 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
5cea8590 2712 if (access(res, R_OK)) {
3a41759d 2713 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
5cea8590
PB
2714 if (access(res, R_OK)) {
2715 qemu_free(res);
2716 res = NULL;
2717 }
2718 }
4d224196 2719
5cea8590
PB
2720 return res;
2721}
2722#undef SHARE_SUFFIX
2723#undef BUILD_SUFFIX
2724#endif
2725
2726char *qemu_find_file(int type, const char *name)
2727{
2728 int len;
2729 const char *subdir;
2730 char *buf;
2731
2732 /* If name contains path separators then try it as a straight path. */
2733 if ((strchr(name, '/') || strchr(name, '\\'))
2734 && access(name, R_OK) == 0) {
73ffc805 2735 return qemu_strdup(name);
5cea8590
PB
2736 }
2737 switch (type) {
2738 case QEMU_FILE_TYPE_BIOS:
2739 subdir = "";
2740 break;
2741 case QEMU_FILE_TYPE_KEYMAP:
2742 subdir = "keymaps/";
2743 break;
2744 default:
2745 abort();
2746 }
2747 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2748 buf = qemu_mallocz(len);
3a41759d 2749 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
5cea8590
PB
2750 if (access(buf, R_OK)) {
2751 qemu_free(buf);
2752 return NULL;
2753 }
2754 return buf;
2755}
2756
ff952ba2
MA
2757static int device_help_func(QemuOpts *opts, void *opaque)
2758{
2759 return qdev_device_help(opts);
2760}
2761
f31d07d1
GH
2762static int device_init_func(QemuOpts *opts, void *opaque)
2763{
2764 DeviceState *dev;
2765
2766 dev = qdev_device_add(opts);
2767 if (!dev)
2768 return -1;
2769 return 0;
2770}
2771
1a688d3b
GH
2772static int chardev_init_func(QemuOpts *opts, void *opaque)
2773{
2774 CharDriverState *chr;
2775
2776 chr = qemu_chr_open_opts(opts, NULL);
2777 if (!chr)
2778 return -1;
2779 return 0;
2780}
2781
88589343
GH
2782static int mon_init_func(QemuOpts *opts, void *opaque)
2783{
2784 CharDriverState *chr;
2785 const char *chardev;
2786 const char *mode;
2787 int flags;
2788
2789 mode = qemu_opt_get(opts, "mode");
2790 if (mode == NULL) {
2791 mode = "readline";
2792 }
2793 if (strcmp(mode, "readline") == 0) {
2794 flags = MONITOR_USE_READLINE;
2795 } else if (strcmp(mode, "control") == 0) {
2796 flags = MONITOR_USE_CONTROL;
2797 } else {
2798 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2799 exit(1);
2800 }
2801
2802 if (qemu_opt_get_bool(opts, "default", 0))
2803 flags |= MONITOR_IS_DEFAULT;
2804
2805 chardev = qemu_opt_get(opts, "chardev");
2806 chr = qemu_chr_find(chardev);
2807 if (chr == NULL) {
2808 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2809 exit(1);
2810 }
2811
2812 monitor_init(chr, flags);
2813 return 0;
2814}
2815
6ca5582d 2816static void monitor_parse(const char *optarg, const char *mode)
88589343
GH
2817{
2818 static int monitor_device_index = 0;
2819 QemuOpts *opts;
2820 const char *p;
2821 char label[32];
2822 int def = 0;
2823
2824 if (strstart(optarg, "chardev:", &p)) {
2825 snprintf(label, sizeof(label), "%s", p);
2826 } else {
2827 if (monitor_device_index) {
2828 snprintf(label, sizeof(label), "monitor%d",
2829 monitor_device_index);
2830 } else {
2831 snprintf(label, sizeof(label), "monitor");
2832 def = 1;
2833 }
2834 opts = qemu_chr_parse_compat(label, optarg);
2835 if (!opts) {
2836 fprintf(stderr, "parse error: %s\n", optarg);
2837 exit(1);
2838 }
2839 }
2840
2841 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2842 if (!opts) {
2843 fprintf(stderr, "duplicate chardev: %s\n", label);
2844 exit(1);
2845 }
6ca5582d 2846 qemu_opt_set(opts, "mode", mode);
88589343
GH
2847 qemu_opt_set(opts, "chardev", label);
2848 if (def)
2849 qemu_opt_set(opts, "default", "on");
2850 monitor_device_index++;
2851}
2852
bd3c948d
GH
2853struct device_config {
2854 enum {
aee1b935
GH
2855 DEV_USB, /* -usbdevice */
2856 DEV_BT, /* -bt */
2857 DEV_SERIAL, /* -serial */
2858 DEV_PARALLEL, /* -parallel */
2859 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 2860 DEV_DEBUGCON, /* -debugcon */
bd3c948d
GH
2861 } type;
2862 const char *cmdline;
72cf2d4f 2863 QTAILQ_ENTRY(device_config) next;
bd3c948d 2864};
72cf2d4f 2865QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
2866
2867static void add_device_config(int type, const char *cmdline)
2868{
2869 struct device_config *conf;
2870
2871 conf = qemu_mallocz(sizeof(*conf));
2872 conf->type = type;
2873 conf->cmdline = cmdline;
72cf2d4f 2874 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
2875}
2876
2877static int foreach_device_config(int type, int (*func)(const char *cmdline))
2878{
2879 struct device_config *conf;
2880 int rc;
2881
72cf2d4f 2882 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
2883 if (conf->type != type)
2884 continue;
2885 rc = func(conf->cmdline);
2886 if (0 != rc)
2887 return rc;
2888 }
2889 return 0;
2890}
2891
998bbd74
GH
2892static int serial_parse(const char *devname)
2893{
2894 static int index = 0;
2895 char label[32];
2896
2897 if (strcmp(devname, "none") == 0)
2898 return 0;
2899 if (index == MAX_SERIAL_PORTS) {
2900 fprintf(stderr, "qemu: too many serial ports\n");
2901 exit(1);
2902 }
2903 snprintf(label, sizeof(label), "serial%d", index);
2904 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2905 if (!serial_hds[index]) {
2906 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2907 devname, strerror(errno));
2908 return -1;
2909 }
2910 index++;
2911 return 0;
2912}
2913
6a5e8b0e
GH
2914static int parallel_parse(const char *devname)
2915{
2916 static int index = 0;
2917 char label[32];
2918
2919 if (strcmp(devname, "none") == 0)
2920 return 0;
2921 if (index == MAX_PARALLEL_PORTS) {
2922 fprintf(stderr, "qemu: too many parallel ports\n");
2923 exit(1);
2924 }
2925 snprintf(label, sizeof(label), "parallel%d", index);
2926 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2927 if (!parallel_hds[index]) {
2928 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2929 devname, strerror(errno));
2930 return -1;
2931 }
2932 index++;
2933 return 0;
2934}
2935
aee1b935
GH
2936static int virtcon_parse(const char *devname)
2937{
2938 static int index = 0;
2939 char label[32];
392ecf54 2940 QemuOpts *bus_opts, *dev_opts;
aee1b935
GH
2941
2942 if (strcmp(devname, "none") == 0)
2943 return 0;
2944 if (index == MAX_VIRTIO_CONSOLES) {
2945 fprintf(stderr, "qemu: too many virtio consoles\n");
2946 exit(1);
2947 }
392ecf54
AS
2948
2949 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2950 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2951
2952 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2953 qemu_opt_set(dev_opts, "driver", "virtconsole");
2954
aee1b935
GH
2955 snprintf(label, sizeof(label), "virtcon%d", index);
2956 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2957 if (!virtcon_hds[index]) {
2958 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2959 devname, strerror(errno));
2960 return -1;
2961 }
392ecf54
AS
2962 qemu_opt_set(dev_opts, "chardev", label);
2963
aee1b935
GH
2964 index++;
2965 return 0;
2966}
2967
c9f398e5
PA
2968static int debugcon_parse(const char *devname)
2969{
2970 QemuOpts *opts;
2971
2972 if (!qemu_chr_open("debugcon", devname, NULL)) {
2973 exit(1);
2974 }
2975 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2976 if (!opts) {
2977 fprintf(stderr, "qemu: already have a debugcon device\n");
2978 exit(1);
2979 }
2980 qemu_opt_set(opts, "driver", "isa-debugcon");
2981 qemu_opt_set(opts, "chardev", "debugcon");
2982 return 0;
2983}
2984
6530a97b
AL
2985static const QEMUOption *lookup_opt(int argc, char **argv,
2986 const char **poptarg, int *poptind)
2987{
2988 const QEMUOption *popt;
2989 int optind = *poptind;
2990 char *r = argv[optind];
2991 const char *optarg;
2992
0f0bc3f1 2993 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
2994 optind++;
2995 /* Treat --foo the same as -foo. */
2996 if (r[1] == '-')
2997 r++;
2998 popt = qemu_options;
2999 for(;;) {
3000 if (!popt->name) {
0f0bc3f1 3001 error_report("invalid option");
6530a97b
AL
3002 exit(1);
3003 }
3004 if (!strcmp(popt->name, r + 1))
3005 break;
3006 popt++;
3007 }
3008 if (popt->flags & HAS_ARG) {
3009 if (optind >= argc) {
0f0bc3f1 3010 error_report("requires an argument");
6530a97b
AL
3011 exit(1);
3012 }
3013 optarg = argv[optind++];
0f0bc3f1 3014 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
3015 } else {
3016 optarg = NULL;
3017 }
3018
3019 *poptarg = optarg;
3020 *poptind = optind;
3021
3022 return popt;
3023}
3024
902b3d5c 3025int main(int argc, char **argv, char **envp)
0824d6fc 3026{
59030a8c 3027 const char *gdbstub_dev = NULL;
28c5af54 3028 uint32_t boot_devices_bitmap = 0;
e4bcb14c 3029 int i;
28c5af54 3030 int snapshot, linux_boot, net_boot;
4e3de9e9 3031 const char *icount_option = NULL;
7f7f9873 3032 const char *initrd_filename;
a20dd508 3033 const char *kernel_filename, *kernel_cmdline;
195325a4 3034 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
3023f332 3035 DisplayState *ds;
7d957bd8 3036 DisplayChangeListener *dcl;
46d4767d 3037 int cyls, heads, secs, translation;
f31d07d1 3038 QemuOpts *hda_opts = NULL, *opts;
cd6f1169 3039 int optind;
6530a97b 3040 const char *optarg;
d63d307f 3041 const char *loadvm = NULL;
cc1daa40 3042 QEMUMachine *machine;
94fc95cd 3043 const char *cpu_model;
b9e82a59 3044#ifndef _WIN32
71e3ceb8 3045 int fds[2];
b9e82a59 3046#endif
26a5f13b 3047 int tb_size;
93815bc2 3048 const char *pid_file = NULL;
5bb7910a 3049 const char *incoming = NULL;
b9e82a59 3050#ifndef _WIN32
54042bcf
AL
3051 int fd = 0;
3052 struct passwd *pwd = NULL;
0858532e
AL
3053 const char *chroot_dir = NULL;
3054 const char *run_as = NULL;
b9e82a59 3055#endif
993fbfdb 3056 int show_vnc_port = 0;
292444cb 3057 int defconfig = 1;
0bd48850 3058
65abca0a
MA
3059 error_set_progname(argv[0]);
3060
6875204c
JK
3061 init_clocks();
3062
902b3d5c 3063 qemu_cache_utils_init(envp);
3064
72cf2d4f 3065 QLIST_INIT (&vm_change_state_head);
be995c27
FB
3066#ifndef _WIN32
3067 {
3068 struct sigaction act;
3069 sigfillset(&act.sa_mask);
3070 act.sa_flags = 0;
3071 act.sa_handler = SIG_IGN;
3072 sigaction(SIGPIPE, &act, NULL);
3073 }
3587d7e6
FB
3074#else
3075 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
a8e5ac33
FB
3076 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3077 QEMU to run on a single CPU */
3078 {
3079 HANDLE h;
3080 DWORD mask, smask;
3081 int i;
3082 h = GetCurrentProcess();
3083 if (GetProcessAffinityMask(h, &mask, &smask)) {
3084 for(i = 0; i < 32; i++) {
3085 if (mask & (1 << i))
3086 break;
3087 }
3088 if (i != 32) {
3089 mask = 1 << i;
3090 SetProcessAffinityMask(h, mask);
3091 }
3092 }
3093 }
67b915a5 3094#endif
be995c27 3095
f80f9ec9 3096 module_call_init(MODULE_INIT_MACHINE);
0c257437 3097 machine = find_default_machine();
94fc95cd 3098 cpu_model = NULL;
fc01f7e7 3099 initrd_filename = NULL;
4fc5d071 3100 ram_size = 0;
33e3963e 3101 snapshot = 0;
a20dd508
FB
3102 kernel_filename = NULL;
3103 kernel_cmdline = "";
c4b1fcc0 3104 cyls = heads = secs = 0;
46d4767d 3105 translation = BIOS_ATA_TRANSLATION_AUTO;
c4b1fcc0 3106
268a362c
AL
3107 for (i = 0; i < MAX_NODES; i++) {
3108 node_mem[i] = 0;
3109 node_cpumask[i] = 0;
3110 }
3111
268a362c 3112 nb_numa_nodes = 0;
7c9d8e07 3113 nb_nics = 0;
3b46e624 3114
26a5f13b 3115 tb_size = 0;
41bd639b
BS
3116 autostart= 1;
3117
292444cb
AL
3118 /* first pass of option parsing */
3119 optind = 1;
3120 while (optind < argc) {
3121 if (argv[optind][0] != '-') {
3122 /* disk image */
28e68d68 3123 optind++;
292444cb
AL
3124 continue;
3125 } else {
3126 const QEMUOption *popt;
3127
3128 popt = lookup_opt(argc, argv, &optarg, &optind);
3129 switch (popt->index) {
3130 case QEMU_OPTION_nodefconfig:
3131 defconfig=0;
3132 break;
3133 }
3134 }
3135 }
3136
3137 if (defconfig) {
cf5a65aa 3138 const char *fname;
292444cb 3139 FILE *fp;
cf5a65aa
MA
3140
3141 fname = CONFIG_QEMU_CONFDIR "/qemu.conf";
3142 fp = fopen(fname, "r");
292444cb 3143 if (fp) {
cf5a65aa 3144 if (qemu_config_parse(fp, fname) != 0) {
292444cb
AL
3145 exit(1);
3146 }
3147 fclose(fp);
3148 }
3149
cf5a65aa
MA
3150 fname = CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf";
3151 fp = fopen(fname, "r");
292444cb 3152 if (fp) {
cf5a65aa 3153 if (qemu_config_parse(fp, fname) != 0) {
292444cb
AL
3154 exit(1);
3155 }
3156 fclose(fp);
3157 }
3158 }
de06f8d1 3159 cpudef_init();
292444cb
AL
3160
3161 /* second pass of option parsing */
cd6f1169 3162 optind = 1;
0824d6fc 3163 for(;;) {
cd6f1169 3164 if (optind >= argc)
0824d6fc 3165 break;
6530a97b 3166 if (argv[optind][0] != '-') {
9dfd7c7a 3167 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
cd6f1169
FB
3168 } else {
3169 const QEMUOption *popt;
3170
6530a97b 3171 popt = lookup_opt(argc, argv, &optarg, &optind);
cd6f1169 3172 switch(popt->index) {
cc1daa40
FB
3173 case QEMU_OPTION_M:
3174 machine = find_machine(optarg);
3175 if (!machine) {
3176 QEMUMachine *m;
3177 printf("Supported machines are:\n");
3178 for(m = first_machine; m != NULL; m = m->next) {
3f6599e6
MM
3179 if (m->alias)
3180 printf("%-10s %s (alias of %s)\n",
3181 m->alias, m->desc, m->name);
cc1daa40 3182 printf("%-10s %s%s\n",
5fafdf24 3183 m->name, m->desc,
0c257437 3184 m->is_default ? " (default)" : "");
cc1daa40 3185 }
15f82208 3186 exit(*optarg != '?');
cc1daa40
FB
3187 }
3188 break;
94fc95cd
JM
3189 case QEMU_OPTION_cpu:
3190 /* hw initialization will check this */
15f82208 3191 if (*optarg == '?') {
c732abe2 3192/* XXX: implement xxx_cpu_list for targets that still miss it */
b5ec5ce0 3193#if defined(cpu_list_id)
3194 cpu_list_id(stdout, &fprintf, optarg);
3195#elif defined(cpu_list)
3196 cpu_list(stdout, &fprintf); /* deprecated */
94fc95cd 3197#endif
15f82208 3198 exit(0);
94fc95cd
JM
3199 } else {
3200 cpu_model = optarg;
3201 }
3202 break;
cd6f1169 3203 case QEMU_OPTION_initrd:
fc01f7e7
FB
3204 initrd_filename = optarg;
3205 break;
cd6f1169 3206 case QEMU_OPTION_hda:
e4bcb14c 3207 if (cyls == 0)
9dfd7c7a 3208 hda_opts = drive_add(optarg, HD_ALIAS, 0);
e4bcb14c 3209 else
9dfd7c7a 3210 hda_opts = drive_add(optarg, HD_ALIAS
e4bcb14c 3211 ",cyls=%d,heads=%d,secs=%d%s",
609497ab 3212 0, cyls, heads, secs,
e4bcb14c
TS
3213 translation == BIOS_ATA_TRANSLATION_LBA ?
3214 ",trans=lba" :
3215 translation == BIOS_ATA_TRANSLATION_NONE ?
3216 ",trans=none" : "");
3217 break;
cd6f1169 3218 case QEMU_OPTION_hdb:
cc1daa40
FB
3219 case QEMU_OPTION_hdc:
3220 case QEMU_OPTION_hdd:
609497ab 3221 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
fc01f7e7 3222 break;
e4bcb14c 3223 case QEMU_OPTION_drive:
609497ab 3224 drive_add(NULL, "%s", optarg);
e4bcb14c 3225 break;
d058fe03
GH
3226 case QEMU_OPTION_set:
3227 if (qemu_set_option(optarg) != 0)
3228 exit(1);
3229 break;
d0fef6fb
GH
3230 case QEMU_OPTION_global:
3231 if (qemu_global_option(optarg) != 0)
3232 exit(1);
3233 break;
3e3d5815 3234 case QEMU_OPTION_mtdblock:
609497ab 3235 drive_add(optarg, MTD_ALIAS);
3e3d5815 3236 break;
a1bb27b1 3237 case QEMU_OPTION_sd:
609497ab 3238 drive_add(optarg, SD_ALIAS);
a1bb27b1 3239 break;
86f55663 3240 case QEMU_OPTION_pflash:
609497ab 3241 drive_add(optarg, PFLASH_ALIAS);
86f55663 3242 break;
cd6f1169 3243 case QEMU_OPTION_snapshot:
33e3963e
FB
3244 snapshot = 1;
3245 break;
cd6f1169 3246 case QEMU_OPTION_hdachs:
330d0414 3247 {
330d0414
FB
3248 const char *p;
3249 p = optarg;
3250 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
3251 if (cyls < 1 || cyls > 16383)
3252 goto chs_fail;
330d0414
FB
3253 if (*p != ',')
3254 goto chs_fail;
3255 p++;
3256 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
3257 if (heads < 1 || heads > 16)
3258 goto chs_fail;
330d0414
FB
3259 if (*p != ',')
3260 goto chs_fail;
3261 p++;
3262 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
3263 if (secs < 1 || secs > 63)
3264 goto chs_fail;
3265 if (*p == ',') {
3266 p++;
3267 if (!strcmp(p, "none"))
3268 translation = BIOS_ATA_TRANSLATION_NONE;
3269 else if (!strcmp(p, "lba"))
3270 translation = BIOS_ATA_TRANSLATION_LBA;
3271 else if (!strcmp(p, "auto"))
3272 translation = BIOS_ATA_TRANSLATION_AUTO;
3273 else
3274 goto chs_fail;
3275 } else if (*p != '\0') {
c4b1fcc0 3276 chs_fail:
46d4767d
FB
3277 fprintf(stderr, "qemu: invalid physical CHS format\n");
3278 exit(1);
c4b1fcc0 3279 }
9dfd7c7a
GH
3280 if (hda_opts != NULL) {
3281 char num[16];
3282 snprintf(num, sizeof(num), "%d", cyls);
3283 qemu_opt_set(hda_opts, "cyls", num);
3284 snprintf(num, sizeof(num), "%d", heads);
3285 qemu_opt_set(hda_opts, "heads", num);
3286 snprintf(num, sizeof(num), "%d", secs);
3287 qemu_opt_set(hda_opts, "secs", num);
3288 if (translation == BIOS_ATA_TRANSLATION_LBA)
3289 qemu_opt_set(hda_opts, "trans", "lba");
3290 if (translation == BIOS_ATA_TRANSLATION_NONE)
3291 qemu_opt_set(hda_opts, "trans", "none");
3292 }
330d0414
FB
3293 }
3294 break;
268a362c
AL
3295 case QEMU_OPTION_numa:
3296 if (nb_numa_nodes >= MAX_NODES) {
3297 fprintf(stderr, "qemu: too many NUMA nodes\n");
3298 exit(1);
3299 }
3300 numa_add(optarg);
3301 break;
cd6f1169 3302 case QEMU_OPTION_nographic:
993fbfdb 3303 display_type = DT_NOGRAPHIC;
a20dd508 3304 break;
4d3b6f6e
AZ
3305#ifdef CONFIG_CURSES
3306 case QEMU_OPTION_curses:
993fbfdb 3307 display_type = DT_CURSES;
4d3b6f6e
AZ
3308 break;
3309#endif
a171fe39
AZ
3310 case QEMU_OPTION_portrait:
3311 graphic_rotate = 1;
3312 break;
cd6f1169 3313 case QEMU_OPTION_kernel:
a20dd508
FB
3314 kernel_filename = optarg;
3315 break;
cd6f1169 3316 case QEMU_OPTION_append:
a20dd508 3317 kernel_cmdline = optarg;
313aa567 3318 break;
cd6f1169 3319 case QEMU_OPTION_cdrom:
609497ab 3320 drive_add(optarg, CDROM_ALIAS);
36b486bb 3321 break;
cd6f1169 3322 case QEMU_OPTION_boot:
28c5af54 3323 {
ef3adf68 3324 static const char * const params[] = {
95387491 3325 "order", "once", "menu", NULL
ef3adf68
JK
3326 };
3327 char buf[sizeof(boot_devices)];
e0f084bf 3328 char *standard_boot_devices;
ef3adf68
JK
3329 int legacy = 0;
3330
3331 if (!strchr(optarg, '=')) {
3332 legacy = 1;
3333 pstrcpy(buf, sizeof(buf), optarg);
3334 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
3335 fprintf(stderr,
3336 "qemu: unknown boot parameter '%s' in '%s'\n",
3337 buf, optarg);
3338 exit(1);
3339 }
3340
3341 if (legacy ||
3342 get_param_value(buf, sizeof(buf), "order", optarg)) {
3343 boot_devices_bitmap = parse_bootdevices(buf);
3344 pstrcpy(boot_devices, sizeof(boot_devices), buf);
28c5af54 3345 }
e0f084bf
JK
3346 if (!legacy) {
3347 if (get_param_value(buf, sizeof(buf),
3348 "once", optarg)) {
3349 boot_devices_bitmap |= parse_bootdevices(buf);
3350 standard_boot_devices = qemu_strdup(boot_devices);
3351 pstrcpy(boot_devices, sizeof(boot_devices), buf);
3352 qemu_register_reset(restore_boot_devices,
3353 standard_boot_devices);
3354 }
95387491
JK
3355 if (get_param_value(buf, sizeof(buf),
3356 "menu", optarg)) {
3357 if (!strcmp(buf, "on")) {
3358 boot_menu = 1;
3359 } else if (!strcmp(buf, "off")) {
3360 boot_menu = 0;
3361 } else {
3362 fprintf(stderr,
3363 "qemu: invalid option value '%s'\n",
3364 buf);
3365 exit(1);
3366 }
3367 }
e0f084bf 3368 }
36b486bb
FB
3369 }
3370 break;
cd6f1169 3371 case QEMU_OPTION_fda:
cd6f1169 3372 case QEMU_OPTION_fdb:
609497ab 3373 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
c45886db 3374 break;
52ca8d6a
FB
3375#ifdef TARGET_I386
3376 case QEMU_OPTION_no_fd_bootchk:
3377 fd_bootchk = 0;
3378 break;
3379#endif
a1ea458f
MM
3380 case QEMU_OPTION_netdev:
3381 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
3382 exit(1);
3383 }
3384 break;
7c9d8e07 3385 case QEMU_OPTION_net:
7f161aae 3386 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
c4b1fcc0
FB
3387 exit(1);
3388 }
702c651c 3389 break;
c7f74643
FB
3390#ifdef CONFIG_SLIRP
3391 case QEMU_OPTION_tftp:
ad196a9d 3392 legacy_tftp_prefix = optarg;
9bf05444 3393 break;
47d5d01a 3394 case QEMU_OPTION_bootp:
ad196a9d 3395 legacy_bootp_filename = optarg;
47d5d01a 3396 break;
c94c8d64 3397#ifndef _WIN32
9d728e8c 3398 case QEMU_OPTION_smb:
0752706d
MA
3399 if (net_slirp_smb(optarg) < 0)
3400 exit(1);
9d728e8c 3401 break;
c94c8d64 3402#endif
9bf05444 3403 case QEMU_OPTION_redir:
0752706d
MA
3404 if (net_slirp_redir(optarg) < 0)
3405 exit(1);
9bf05444 3406 break;
c7f74643 3407#endif
dc72ac14 3408 case QEMU_OPTION_bt:
bd3c948d 3409 add_device_config(DEV_BT, optarg);
dc72ac14 3410 break;
1d14ffa9 3411#ifdef HAS_AUDIO
1d14ffa9
FB
3412 case QEMU_OPTION_audio_help:
3413 AUD_help ();
3414 exit (0);
3415 break;
3416 case QEMU_OPTION_soundhw:
3417 select_soundhw (optarg);
3418 break;
3419#endif
cd6f1169 3420 case QEMU_OPTION_h:
15f82208 3421 help(0);
cd6f1169 3422 break;
9bd7e6d9
PB
3423 case QEMU_OPTION_version:
3424 version();
3425 exit(0);
3426 break;
00f82b8a
AJ
3427 case QEMU_OPTION_m: {
3428 uint64_t value;
3429 char *ptr;
3430
3431 value = strtoul(optarg, &ptr, 10);
3432 switch (*ptr) {
3433 case 0: case 'M': case 'm':
3434 value <<= 20;
3435 break;
3436 case 'G': case 'g':
3437 value <<= 30;
3438 break;
3439 default:
3440 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
3441 exit(1);
3442 }
00f82b8a
AJ
3443
3444 /* On 32-bit hosts, QEMU is limited by virtual address space */
4a1418e0 3445 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
00f82b8a
AJ
3446 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3447 exit(1);
3448 }
c227f099 3449 if (value != (uint64_t)(ram_addr_t)value) {
00f82b8a
AJ
3450 fprintf(stderr, "qemu: ram size too large\n");
3451 exit(1);
3452 }
3453 ram_size = value;
cd6f1169 3454 break;
00f82b8a 3455 }
c902760f
MT
3456 case QEMU_OPTION_mempath:
3457 mem_path = optarg;
3458 break;
3459#ifdef MAP_POPULATE
3460 case QEMU_OPTION_mem_prealloc:
3461 mem_prealloc = 1;
3462 break;
3463#endif
cd6f1169 3464 case QEMU_OPTION_d:
de06f8d1 3465 set_cpu_log(optarg);
cd6f1169 3466 break;
cd6f1169 3467 case QEMU_OPTION_s:
59030a8c 3468 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
cd6f1169 3469 break;
59030a8c
AL
3470 case QEMU_OPTION_gdb:
3471 gdbstub_dev = optarg;
cd6f1169 3472 break;
cd6f1169 3473 case QEMU_OPTION_L:
5cea8590 3474 data_dir = optarg;
cd6f1169 3475 break;
1192dad8
JM
3476 case QEMU_OPTION_bios:
3477 bios_name = optarg;
3478 break;
1b530a6d
AJ
3479 case QEMU_OPTION_singlestep:
3480 singlestep = 1;
3481 break;
cd6f1169 3482 case QEMU_OPTION_S:
3c07f8e8 3483 autostart = 0;
cd6f1169 3484 break;
3d11d0eb
FB
3485 case QEMU_OPTION_k:
3486 keyboard_layout = optarg;
3487 break;
ee22c2f7
FB
3488 case QEMU_OPTION_localtime:
3489 rtc_utc = 0;
3490 break;
3893c124 3491 case QEMU_OPTION_vga:
3492 select_vgahw (optarg);
1bfe856e 3493 break;
5824d651 3494#if defined(TARGET_PPC) || defined(TARGET_SPARC)
e9b137c2
FB
3495 case QEMU_OPTION_g:
3496 {
3497 const char *p;
3498 int w, h, depth;
3499 p = optarg;
3500 w = strtol(p, (char **)&p, 10);
3501 if (w <= 0) {
3502 graphic_error:
3503 fprintf(stderr, "qemu: invalid resolution or depth\n");
3504 exit(1);
3505 }
3506 if (*p != 'x')
3507 goto graphic_error;
3508 p++;
3509 h = strtol(p, (char **)&p, 10);
3510 if (h <= 0)
3511 goto graphic_error;
3512 if (*p == 'x') {
3513 p++;
3514 depth = strtol(p, (char **)&p, 10);
5fafdf24 3515 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
3516 depth != 24 && depth != 32)
3517 goto graphic_error;
3518 } else if (*p == '\0') {
3519 depth = graphic_depth;
3520 } else {
3521 goto graphic_error;
3522 }
3b46e624 3523
e9b137c2
FB
3524 graphic_width = w;
3525 graphic_height = h;
3526 graphic_depth = depth;
3527 }
3528 break;
5824d651 3529#endif
20d8a3ed
TS
3530 case QEMU_OPTION_echr:
3531 {
3532 char *r;
3533 term_escape_char = strtol(optarg, &r, 0);
3534 if (r == optarg)
3535 printf("Bad argument to echr\n");
3536 break;
3537 }
82c643ff 3538 case QEMU_OPTION_monitor:
6ca5582d
GH
3539 monitor_parse(optarg, "readline");
3540 default_monitor = 0;
3541 break;
3542 case QEMU_OPTION_qmp:
3543 monitor_parse(optarg, "control");
2d114dc1 3544 default_monitor = 0;
82c643ff 3545 break;
22a0e04b 3546 case QEMU_OPTION_mon:
8212c64f 3547 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
22a0e04b
GH
3548 if (!opts) {
3549 fprintf(stderr, "parse error: %s\n", optarg);
3550 exit(1);
3551 }
2d114dc1 3552 default_monitor = 0;
22a0e04b 3553 break;
191bc01b 3554 case QEMU_OPTION_chardev:
8212c64f 3555 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
191bc01b
GH
3556 if (!opts) {
3557 fprintf(stderr, "parse error: %s\n", optarg);
3558 exit(1);
3559 }
191bc01b 3560 break;
82c643ff 3561 case QEMU_OPTION_serial:
998bbd74
GH
3562 add_device_config(DEV_SERIAL, optarg);
3563 default_serial = 0;
18141ed6
JK
3564 if (strncmp(optarg, "mon:", 4) == 0) {
3565 default_monitor = 0;
3566 }
82c643ff 3567 break;
9dd986cc 3568 case QEMU_OPTION_watchdog:
09aaa160
MA
3569 if (watchdog) {
3570 fprintf(stderr,
3571 "qemu: only one watchdog option may be given\n");
3572 return 1;
3573 }
3574 watchdog = optarg;
9dd986cc
RJ
3575 break;
3576 case QEMU_OPTION_watchdog_action:
3577 if (select_watchdog_action(optarg) == -1) {
3578 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3579 exit(1);
3580 }
3581 break;
51ecf136 3582 case QEMU_OPTION_virtiocon:
aee1b935
GH
3583 add_device_config(DEV_VIRTCON, optarg);
3584 default_virtcon = 0;
18141ed6
JK
3585 if (strncmp(optarg, "mon:", 4) == 0) {
3586 default_monitor = 0;
3587 }
51ecf136 3588 break;
6508fe59 3589 case QEMU_OPTION_parallel:
6a5e8b0e
GH
3590 add_device_config(DEV_PARALLEL, optarg);
3591 default_parallel = 0;
18141ed6
JK
3592 if (strncmp(optarg, "mon:", 4) == 0) {
3593 default_monitor = 0;
3594 }
6508fe59 3595 break;
c9f398e5
PA
3596 case QEMU_OPTION_debugcon:
3597 add_device_config(DEV_DEBUGCON, optarg);
3598 break;
d63d307f
FB
3599 case QEMU_OPTION_loadvm:
3600 loadvm = optarg;
3601 break;
3602 case QEMU_OPTION_full_screen:
3603 full_screen = 1;
3604 break;
667accab 3605#ifdef CONFIG_SDL
43523e93
TS
3606 case QEMU_OPTION_no_frame:
3607 no_frame = 1;
3608 break;
3780e197
TS
3609 case QEMU_OPTION_alt_grab:
3610 alt_grab = 1;
3611 break;
0ca9f8a4
DK
3612 case QEMU_OPTION_ctrl_grab:
3613 ctrl_grab = 1;
3614 break;
667accab
TS
3615 case QEMU_OPTION_no_quit:
3616 no_quit = 1;
3617 break;
7d957bd8 3618 case QEMU_OPTION_sdl:
993fbfdb 3619 display_type = DT_SDL;
7d957bd8 3620 break;
667accab 3621#endif
f7cce898 3622 case QEMU_OPTION_pidfile:
93815bc2 3623 pid_file = optarg;
f7cce898 3624 break;
a09db21f
FB
3625#ifdef TARGET_I386
3626 case QEMU_OPTION_win2k_hack:
3627 win2k_install_hack = 1;
3628 break;
73822ec8
AL
3629 case QEMU_OPTION_rtc_td_hack:
3630 rtc_td_hack = 1;
3631 break;
8a92ea2f 3632 case QEMU_OPTION_acpitable:
de06f8d1 3633 do_acpitable_option(optarg);
8a92ea2f 3634 break;
b6f6e3d3 3635 case QEMU_OPTION_smbios:
de06f8d1 3636 do_smbios_option(optarg);
b6f6e3d3 3637 break;
a09db21f 3638#endif
7ba1e619
AL
3639#ifdef CONFIG_KVM
3640 case QEMU_OPTION_enable_kvm:
3641 kvm_allowed = 1;
7ba1e619 3642 break;
d993e026 3643#endif
bb36d470
FB
3644 case QEMU_OPTION_usb:
3645 usb_enabled = 1;
3646 break;
a594cfbf
FB
3647 case QEMU_OPTION_usbdevice:
3648 usb_enabled = 1;
bd3c948d
GH
3649 add_device_config(DEV_USB, optarg);
3650 break;
3651 case QEMU_OPTION_device:
8212c64f 3652 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
f31d07d1
GH
3653 exit(1);
3654 }
a594cfbf 3655 break;
6a00d601 3656 case QEMU_OPTION_smp:
dc6b1c09 3657 smp_parse(optarg);
b2097003 3658 if (smp_cpus < 1) {
6a00d601
FB
3659 fprintf(stderr, "Invalid number of CPUs\n");
3660 exit(1);
3661 }
6be68d7e
JS
3662 if (max_cpus < smp_cpus) {
3663 fprintf(stderr, "maxcpus must be equal to or greater than "
3664 "smp\n");
3665 exit(1);
3666 }
3667 if (max_cpus > 255) {
3668 fprintf(stderr, "Unsupported number of maxcpus\n");
3669 exit(1);
3670 }
6a00d601 3671 break;
24236869 3672 case QEMU_OPTION_vnc:
993fbfdb 3673 display_type = DT_VNC;
73fc9742 3674 vnc_display = optarg;
24236869 3675 break;
5824d651 3676#ifdef TARGET_I386
6515b203
FB
3677 case QEMU_OPTION_no_acpi:
3678 acpi_enabled = 0;
3679 break;
16b29ae1
AL
3680 case QEMU_OPTION_no_hpet:
3681 no_hpet = 1;
3682 break;
7d4c3d53
MA
3683 case QEMU_OPTION_balloon:
3684 if (balloon_parse(optarg) < 0) {
3685 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3686 exit(1);
3687 }
df97b920 3688 break;
5824d651 3689#endif
d1beab82
FB
3690 case QEMU_OPTION_no_reboot:
3691 no_reboot = 1;
3692 break;
b2f76161
AJ
3693 case QEMU_OPTION_no_shutdown:
3694 no_shutdown = 1;
3695 break;
9467cd46
AZ
3696 case QEMU_OPTION_show_cursor:
3697 cursor_hide = 0;
3698 break;
8fcb1b90
BS
3699 case QEMU_OPTION_uuid:
3700 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3701 fprintf(stderr, "Fail to parse UUID string."
3702 " Wrong format.\n");
3703 exit(1);
3704 }
3705 break;
5824d651 3706#ifndef _WIN32
71e3ceb8
TS
3707 case QEMU_OPTION_daemonize:
3708 daemonize = 1;
3709 break;
5824d651 3710#endif
9ae02555
TS
3711 case QEMU_OPTION_option_rom:
3712 if (nb_option_roms >= MAX_OPTION_ROMS) {
3713 fprintf(stderr, "Too many option ROMs\n");
3714 exit(1);
3715 }
3716 option_rom[nb_option_roms] = optarg;
3717 nb_option_roms++;
3718 break;
5824d651 3719#if defined(TARGET_ARM) || defined(TARGET_M68K)
8e71621f
PB
3720 case QEMU_OPTION_semihosting:
3721 semihosting_enabled = 1;
3722 break;
5824d651 3723#endif
c35734b2 3724 case QEMU_OPTION_name:
1889465a
AK
3725 qemu_name = qemu_strdup(optarg);
3726 {
3727 char *p = strchr(qemu_name, ',');
3728 if (p != NULL) {
3729 *p++ = 0;
3730 if (strncmp(p, "process=", 8)) {
3731 fprintf(stderr, "Unknown subargument %s to -name", p);
3732 exit(1);
3733 }
3734 p += 8;
3735 set_proc_name(p);
3736 }
3737 }
c35734b2 3738 break;
95efd11c 3739#if defined(TARGET_SPARC) || defined(TARGET_PPC)
66508601
BS
3740 case QEMU_OPTION_prom_env:
3741 if (nb_prom_envs >= MAX_PROM_ENVS) {
3742 fprintf(stderr, "Too many prom variables\n");
3743 exit(1);
3744 }
3745 prom_envs[nb_prom_envs] = optarg;
3746 nb_prom_envs++;
3747 break;
2b8f2d41
AZ
3748#endif
3749#ifdef TARGET_ARM
3750 case QEMU_OPTION_old_param:
3751 old_param = 1;
05ebd537 3752 break;
66508601 3753#endif
f3dcfada
TS
3754 case QEMU_OPTION_clock:
3755 configure_alarms(optarg);
3756 break;
7e0af5d0 3757 case QEMU_OPTION_startdate:
1ed2fc1f
JK
3758 configure_rtc_date_offset(optarg, 1);
3759 break;
3760 case QEMU_OPTION_rtc:
8212c64f 3761 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
1ed2fc1f
JK
3762 if (!opts) {
3763 fprintf(stderr, "parse error: %s\n", optarg);
3764 exit(1);
7e0af5d0 3765 }
1ed2fc1f 3766 configure_rtc(opts);
7e0af5d0 3767 break;
26a5f13b
FB
3768 case QEMU_OPTION_tb_size:
3769 tb_size = strtol(optarg, NULL, 0);
3770 if (tb_size < 0)
3771 tb_size = 0;
3772 break;
2e70f6ef 3773 case QEMU_OPTION_icount:
4e3de9e9 3774 icount_option = optarg;
2e70f6ef 3775 break;
5bb7910a
AL
3776 case QEMU_OPTION_incoming:
3777 incoming = optarg;
3778 break;
d8c208dd
GH
3779 case QEMU_OPTION_nodefaults:
3780 default_serial = 0;
3781 default_parallel = 0;
aee1b935 3782 default_virtcon = 0;
d8c208dd
GH
3783 default_monitor = 0;
3784 default_vga = 0;
cb4522cc 3785 default_net = 0;
ac33f8fa
GH
3786 default_floppy = 0;
3787 default_cdrom = 0;
3788 default_sdcard = 0;
d8c208dd 3789 break;
5824d651 3790#ifndef _WIN32
0858532e
AL
3791 case QEMU_OPTION_chroot:
3792 chroot_dir = optarg;
3793 break;
3794 case QEMU_OPTION_runas:
3795 run_as = optarg;
3796 break;
e37630ca
AL
3797#endif
3798#ifdef CONFIG_XEN
3799 case QEMU_OPTION_xen_domid:
3800 xen_domid = atoi(optarg);
3801 break;
3802 case QEMU_OPTION_xen_create:
3803 xen_mode = XEN_CREATE;
3804 break;
3805 case QEMU_OPTION_xen_attach:
3806 xen_mode = XEN_ATTACH;
3807 break;
5824d651 3808#endif
715a664a
GH
3809 case QEMU_OPTION_readconfig:
3810 {
3811 FILE *fp;
3812 fp = fopen(optarg, "r");
3813 if (fp == NULL) {
3814 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3815 exit(1);
3816 }
cf5a65aa 3817 if (qemu_config_parse(fp, optarg) != 0) {
715a664a
GH
3818 exit(1);
3819 }
3820 fclose(fp);
3821 break;
3822 }
3823 case QEMU_OPTION_writeconfig:
3824 {
3825 FILE *fp;
3826 if (strcmp(optarg, "-") == 0) {
3827 fp = stdout;
3828 } else {
3829 fp = fopen(optarg, "w");
3830 if (fp == NULL) {
3831 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3832 exit(1);
3833 }
3834 }
3835 qemu_config_write(fp);
3836 fclose(fp);
3837 break;
3838 }
cd6f1169 3839 }
0824d6fc
FB
3840 }
3841 }
0f0bc3f1 3842 loc_set_none();
330d0414 3843
5cea8590
PB
3844 /* If no data_dir is specified then try to find it relative to the
3845 executable path. */
3846 if (!data_dir) {
3847 data_dir = find_datadir(argv[0]);
3848 }
3849 /* If all else fails use the install patch specified when building. */
3850 if (!data_dir) {
3851 data_dir = CONFIG_QEMU_SHAREDIR;
3852 }
3853
6be68d7e
JS
3854 /*
3855 * Default to max_cpus = smp_cpus, in case the user doesn't
3856 * specify a max_cpus value.
3857 */
3858 if (!max_cpus)
3859 max_cpus = smp_cpus;
3860
3d878caa 3861 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
3862 if (smp_cpus > machine->max_cpus) {
3863 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3864 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3865 machine->max_cpus);
3866 exit(1);
3867 }
3868
998bbd74 3869 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
d8bcbabf 3870 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
998bbd74 3871
986c5f78
GH
3872 if (machine->no_serial) {
3873 default_serial = 0;
3874 }
3875 if (machine->no_parallel) {
3876 default_parallel = 0;
3877 }
3878 if (!machine->use_virtcon) {
3879 default_virtcon = 0;
3880 }
3881 if (machine->no_vga) {
3882 default_vga = 0;
3883 }
ac33f8fa
GH
3884 if (machine->no_floppy) {
3885 default_floppy = 0;
3886 }
3887 if (machine->no_cdrom) {
3888 default_cdrom = 0;
3889 }
3890 if (machine->no_sdcard) {
3891 default_sdcard = 0;
3892 }
986c5f78 3893
993fbfdb 3894 if (display_type == DT_NOGRAPHIC) {
6a5e8b0e
GH
3895 if (default_parallel)
3896 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
3897 if (default_serial && default_monitor) {
3898 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
3899 } else if (default_virtcon && default_monitor) {
3900 add_device_config(DEV_VIRTCON, "mon:stdio");
e1c09175
GH
3901 } else {
3902 if (default_serial)
3903 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
3904 if (default_virtcon)
3905 add_device_config(DEV_VIRTCON, "stdio");
e1c09175 3906 if (default_monitor)
6ca5582d 3907 monitor_parse("stdio", "readline");
e1c09175 3908 }
998bbd74
GH
3909 } else {
3910 if (default_serial)
3911 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
3912 if (default_parallel)
3913 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 3914 if (default_monitor)
6ca5582d 3915 monitor_parse("vc:80Cx24C", "readline");
38536da1
AG
3916 if (default_virtcon)
3917 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
bc0129d9 3918 }
64465297
GH
3919 if (default_vga)
3920 vga_interface_type = VGA_CIRRUS;
bc0129d9 3921
1a688d3b
GH
3922 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3923 exit(1);
3924
71e3ceb8 3925#ifndef _WIN32
71e3ceb8
TS
3926 if (daemonize) {
3927 pid_t pid;
3928
3929 if (pipe(fds) == -1)
3930 exit(1);
3931
3932 pid = fork();
3933 if (pid > 0) {
3934 uint8_t status;
3935 ssize_t len;
3936
3937 close(fds[1]);
3938
3939 again:
93815bc2
TS
3940 len = read(fds[0], &status, 1);
3941 if (len == -1 && (errno == EINTR))
3942 goto again;
3943
3944 if (len != 1)
3945 exit(1);
3946 else if (status == 1) {
850810d0 3947 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
93815bc2
TS
3948 exit(1);
3949 } else
3950 exit(0);
71e3ceb8 3951 } else if (pid < 0)
93815bc2 3952 exit(1);
71e3ceb8 3953
40ff6d7e
KW
3954 close(fds[0]);
3955 qemu_set_cloexec(fds[1]);
3956
71e3ceb8
TS
3957 setsid();
3958
3959 pid = fork();
3960 if (pid > 0)
3961 exit(0);
3962 else if (pid < 0)
3963 exit(1);
3964
3965 umask(027);
71e3ceb8
TS
3966
3967 signal(SIGTSTP, SIG_IGN);
3968 signal(SIGTTOU, SIG_IGN);
3969 signal(SIGTTIN, SIG_IGN);
3970 }
099fe236 3971#endif
71e3ceb8 3972
aa26bb2d 3973 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
099fe236 3974#ifndef _WIN32
93815bc2
TS
3975 if (daemonize) {
3976 uint8_t status = 1;
dc330e28
KS
3977 if (write(fds[1], &status, 1) != 1) {
3978 perror("daemonize. Writing to pipe\n");
3979 }
93815bc2 3980 } else
099fe236 3981#endif
850810d0 3982 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
93815bc2
TS
3983 exit(1);
3984 }
3985
214910a7
MT
3986 if (kvm_enabled()) {
3987 int ret;
3988
3989 ret = kvm_init(smp_cpus);
3990 if (ret < 0) {
3991 fprintf(stderr, "failed to initialize KVM\n");
3992 exit(1);
3993 }
3994 }
3995
3fcf7b6b
AL
3996 if (qemu_init_main_loop()) {
3997 fprintf(stderr, "qemu_init_main_loop failed\n");
3998 exit(1);
3999 }
a20dd508 4000 linux_boot = (kernel_filename != NULL);
6c41b272 4001
f8d39c01
TS
4002 if (!linux_boot && *kernel_cmdline != '\0') {
4003 fprintf(stderr, "-append only allowed with -kernel option\n");
4004 exit(1);
4005 }
4006
4007 if (!linux_boot && initrd_filename != NULL) {
4008 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4009 exit(1);
4010 }
4011
bf65f53f
FN
4012#ifndef _WIN32
4013 /* Win32 doesn't support line-buffering and requires size >= 2 */
b118d61e 4014 setvbuf(stdout, NULL, _IOLBF, 0);
bf65f53f 4015#endif
3b46e624 4016
7183b4b4
AL
4017 if (init_timer_alarm() < 0) {
4018 fprintf(stderr, "could not initialize alarm timer\n");
4019 exit(1);
4020 }
4e3de9e9 4021 configure_icount(icount_option);
634fce96 4022
fd1dff4b
FB
4023#ifdef _WIN32
4024 socket_init();
4025#endif
4026
dc1c9fe8
MM
4027 if (net_init_clients() < 0) {
4028 exit(1);
702c651c 4029 }
f1510b2c 4030
406c8df3
GC
4031 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
4032 net_set_boot_mask(net_boot);
4033
dc72ac14 4034 /* init the bluetooth world */
bd3c948d
GH
4035 if (foreach_device_config(DEV_BT, bt_parse))
4036 exit(1);
dc72ac14 4037
0824d6fc 4038 /* init the memory */
94a6b54f
PB
4039 if (ram_size == 0)
4040 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
9ae02555 4041
26a5f13b
FB
4042 /* init the dynamic translator */
4043 cpu_exec_init_all(tb_size * 1024 * 1024);
4044
eb852011 4045 bdrv_init_with_whitelist();
c4b1fcc0 4046
c163b5ca
LS
4047 blk_mig_init();
4048
ac33f8fa 4049 if (default_cdrom) {
aa40fc9c
GH
4050 /* we always create the cdrom drive, even if no disk is there */
4051 drive_add(NULL, CDROM_ALIAS);
ac33f8fa 4052 }
c4b1fcc0 4053
ac33f8fa 4054 if (default_floppy) {
aa40fc9c
GH
4055 /* we always create at least one floppy */
4056 drive_add(NULL, FD_ALIAS, 0);
ac33f8fa 4057 }
86f55663 4058
ac33f8fa 4059 if (default_sdcard) {
aa40fc9c
GH
4060 /* we always create one sd slot, even if no card is in it */
4061 drive_add(NULL, SD_ALIAS);
4062 }
9d413d1d 4063
e4bcb14c 4064 /* open the virtual block devices */
9dfd7c7a 4065 if (snapshot)
7282a033
GH
4066 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
4067 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
9dfd7c7a 4068 exit(1);
3e3d5815 4069
c163b5ca
LS
4070 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
4071 ram_load, NULL);
8a7ddc38 4072
268a362c
AL
4073 if (nb_numa_nodes > 0) {
4074 int i;
4075
4076 if (nb_numa_nodes > smp_cpus) {
4077 nb_numa_nodes = smp_cpus;
4078 }
4079
4080 /* If no memory size if given for any node, assume the default case
4081 * and distribute the available memory equally across all nodes
4082 */
4083 for (i = 0; i < nb_numa_nodes; i++) {
4084 if (node_mem[i] != 0)
4085 break;
4086 }
4087 if (i == nb_numa_nodes) {
4088 uint64_t usedmem = 0;
4089
4090 /* On Linux, the each node's border has to be 8MB aligned,
4091 * the final node gets the rest.
4092 */
4093 for (i = 0; i < nb_numa_nodes - 1; i++) {
4094 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4095 usedmem += node_mem[i];
4096 }
4097 node_mem[i] = ram_size - usedmem;
4098 }
4099
4100 for (i = 0; i < nb_numa_nodes; i++) {
4101 if (node_cpumask[i] != 0)
4102 break;
4103 }
4104 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4105 * must cope with this anyway, because there are BIOSes out there in
4106 * real machines which also use this scheme.
4107 */
4108 if (i == nb_numa_nodes) {
4109 for (i = 0; i < smp_cpus; i++) {
4110 node_cpumask[i % nb_numa_nodes] |= 1 << i;
4111 }
4112 }
4113 }
4114
998bbd74
GH
4115 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4116 exit(1);
6a5e8b0e
GH
4117 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4118 exit(1);
aee1b935
GH
4119 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4120 exit(1);
c9f398e5
PA
4121 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4122 exit(1);
2796dae0 4123
aae9460e
PB
4124 module_call_init(MODULE_INIT_DEVICE);
4125
ff952ba2
MA
4126 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
4127 exit(0);
4128
09aaa160
MA
4129 if (watchdog) {
4130 i = select_watchdog(watchdog);
4131 if (i > 0)
4132 exit (i == 1 ? 1 : 0);
4133 }
4134
b6b61144 4135 if (machine->compat_props) {
458fb679 4136 qdev_prop_register_global_list(machine->compat_props);
b6b61144 4137 }
d0fef6fb
GH
4138 qemu_add_globals();
4139
fbe1b595 4140 machine->init(ram_size, boot_devices,
3023f332
AL
4141 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
4142
ea375f9a 4143 cpu_synchronize_all_post_init();
268a362c 4144
67b3b71d
JQ
4145#ifndef _WIN32
4146 /* must be after terminal init, SDL library changes signal handlers */
4147 sighandler_setup();
4148#endif
4149
87d0a28e 4150 set_numa_modes();
268a362c 4151
6f338c34
AL
4152 current_machine = machine;
4153
3023f332
AL
4154 /* init USB devices */
4155 if (usb_enabled) {
0752706d
MA
4156 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4157 exit(1);
3023f332
AL
4158 }
4159
bd3c948d 4160 /* init generic devices */
f31d07d1 4161 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
bd3c948d
GH
4162 exit(1);
4163
668680f7
MA
4164 net_check_clients();
4165
3023f332 4166 /* just use the first displaystate for the moment */
b473df6e 4167 ds = get_displaystate();
993fbfdb
AL
4168
4169 if (display_type == DT_DEFAULT) {
4170#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4171 display_type = DT_SDL;
4172#else
4173 display_type = DT_VNC;
4174 vnc_display = "localhost:0,to=99";
4175 show_vnc_port = 1;
4176#endif
4177 }
4178
4179
4180 switch (display_type) {
4181 case DT_NOGRAPHIC:
4182 break;
4d3b6f6e 4183#if defined(CONFIG_CURSES)
993fbfdb
AL
4184 case DT_CURSES:
4185 curses_display_init(ds, full_screen);
4186 break;
4d3b6f6e 4187#endif
5b0753e0 4188#if defined(CONFIG_SDL)
993fbfdb
AL
4189 case DT_SDL:
4190 sdl_display_init(ds, full_screen, no_frame);
4191 break;
5b0753e0 4192#elif defined(CONFIG_COCOA)
993fbfdb
AL
4193 case DT_SDL:
4194 cocoa_display_init(ds, full_screen);
4195 break;
313aa567 4196#endif
993fbfdb
AL
4197 case DT_VNC:
4198 vnc_display_init(ds);
4199 if (vnc_display_open(ds, vnc_display) < 0)
4200 exit(1);
f92f8afe 4201
993fbfdb
AL
4202 if (show_vnc_port) {
4203 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
f92f8afe 4204 }
993fbfdb
AL
4205 break;
4206 default:
4207 break;
313aa567 4208 }
7d957bd8 4209 dpy_resize(ds);
5b08fc10 4210
3023f332
AL
4211 dcl = ds->listeners;
4212 while (dcl != NULL) {
4213 if (dcl->dpy_refresh != NULL) {
4214 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
4215 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
20d8a3ed 4216 }
3023f332 4217 dcl = dcl->next;
20d8a3ed 4218 }
3023f332 4219
993fbfdb 4220 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
9043b62d
BS
4221 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
4222 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
4223 }
4224
b473df6e 4225 text_consoles_set_display(ds);
2796dae0 4226
88589343
GH
4227 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0)
4228 exit(1);
82c643ff 4229
59030a8c
AL
4230 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
4231 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
4232 gdbstub_dev);
4233 exit(1);
45669e00 4234 }
45669e00 4235
3418bd25
GH
4236 qdev_machine_creation_done();
4237
15ff7705
GH
4238 if (rom_load_all() != 0) {
4239 fprintf(stderr, "rom loading failed\n");
4240 exit(1);
4241 }
45a50b16 4242
504c2948 4243 qemu_system_reset();
05f2401e 4244 if (loadvm) {
03cd4655 4245 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
4246 autostart = 0;
4247 }
4248 }
d63d307f 4249
2bb8c10c 4250 if (incoming) {
5bb7910a 4251 qemu_start_incoming_migration(incoming);
6b99dadc 4252 } else if (autostart) {
c0f4ce77 4253 vm_start();
6b99dadc 4254 }
ffd843bc 4255
b9e82a59 4256#ifndef _WIN32
71e3ceb8
TS
4257 if (daemonize) {
4258 uint8_t status = 0;
4259 ssize_t len;
71e3ceb8
TS
4260
4261 again1:
4262 len = write(fds[1], &status, 1);
4263 if (len == -1 && (errno == EINTR))
4264 goto again1;
4265
4266 if (len != 1)
4267 exit(1);
4268
dc330e28
KS
4269 if (chdir("/")) {
4270 perror("not able to chdir to /");
4271 exit(1);
4272 }
40ff6d7e 4273 TFR(fd = qemu_open("/dev/null", O_RDWR));
71e3ceb8
TS
4274 if (fd == -1)
4275 exit(1);
0858532e 4276 }
71e3ceb8 4277
0858532e
AL
4278 if (run_as) {
4279 pwd = getpwnam(run_as);
4280 if (!pwd) {
4281 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
4282 exit(1);
4283 }
4284 }
4285
4286 if (chroot_dir) {
4287 if (chroot(chroot_dir) < 0) {
4288 fprintf(stderr, "chroot failed\n");
4289 exit(1);
4290 }
dc330e28
KS
4291 if (chdir("/")) {
4292 perror("not able to chdir to /");
4293 exit(1);
4294 }
0858532e
AL
4295 }
4296
4297 if (run_as) {
4298 if (setgid(pwd->pw_gid) < 0) {
4299 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
4300 exit(1);
4301 }
4302 if (setuid(pwd->pw_uid) < 0) {
4303 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
4304 exit(1);
4305 }
4306 if (setuid(0) != -1) {
4307 fprintf(stderr, "Dropping privileges failed\n");
4308 exit(1);
4309 }
4310 }
0858532e
AL
4311
4312 if (daemonize) {
4313 dup2(fd, 0);
4314 dup2(fd, 1);
4315 dup2(fd, 2);
71e3ceb8 4316
0858532e 4317 close(fd);
71e3ceb8 4318 }
b9e82a59 4319#endif
71e3ceb8 4320
8a7ddc38 4321 main_loop();
40c3bac3 4322 quit_timers();
63a01ef8 4323 net_cleanup();
b46a8906 4324
0824d6fc
FB
4325 return 0;
4326}