]> git.proxmox.com Git - qemu.git/blame - vl.c
Fix RTC initial date computation
[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 */
87ecb68b
PB
24#include "hw/hw.h"
25#include "hw/boards.h"
26#include "hw/usb.h"
27#include "hw/pcmcia.h"
28#include "hw/pc.h"
87ecb68b
PB
29#include "hw/audiodev.h"
30#include "hw/isa.h"
2e4d9fb1 31#include "hw/baum.h"
1ae26a18 32#include "hw/bt.h"
87ecb68b
PB
33#include "net.h"
34#include "console.h"
35#include "sysemu.h"
36#include "gdbstub.h"
37#include "qemu-timer.h"
38#include "qemu-char.h"
39#include "block.h"
40#include "audio/audio.h"
5bb7910a 41#include "migration.h"
7ba1e619 42#include "kvm.h"
df751fa8 43#include "balloon.h"
67b915a5 44
0824d6fc 45#include <unistd.h>
0824d6fc
FB
46#include <fcntl.h>
47#include <signal.h>
48#include <time.h>
0824d6fc 49#include <errno.h>
67b915a5 50#include <sys/time.h>
c88676f8 51#include <zlib.h>
67b915a5
FB
52
53#ifndef _WIN32
54#include <sys/times.h>
f1510b2c 55#include <sys/wait.h>
67b915a5 56#include <termios.h>
67b915a5 57#include <sys/mman.h>
f1510b2c 58#include <sys/ioctl.h>
24646c7e 59#include <sys/resource.h>
f1510b2c 60#include <sys/socket.h>
c94c8d64 61#include <netinet/in.h>
24646c7e
BS
62#include <net/if.h>
63#if defined(__NetBSD__)
64#include <net/if_tap.h>
65#endif
66#ifdef __linux__
67#include <linux/if_tun.h>
68#endif
69#include <arpa/inet.h>
9d728e8c 70#include <dirent.h>
7c9d8e07 71#include <netdb.h>
cb4b976b 72#include <sys/select.h>
7d3505c5
FB
73#ifdef _BSD
74#include <sys/stat.h>
24646c7e 75#ifdef __FreeBSD__
7d3505c5 76#include <libutil.h>
24646c7e
BS
77#else
78#include <util.h>
128ab2ff 79#endif
5c40d2bd
TS
80#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
81#include <freebsd/stdlib.h>
7d3505c5 82#else
223f0d72 83#ifdef __linux__
7d3505c5
FB
84#include <pty.h>
85#include <malloc.h>
fd872598 86#include <linux/rtc.h>
bd494f4c
TS
87
88/* For the benefit of older linux systems which don't supply it,
89 we use a local copy of hpet.h. */
90/* #include <linux/hpet.h> */
91#include "hpet.h"
92
e57a8c0e 93#include <linux/ppdev.h>
5867c88a 94#include <linux/parport.h>
223f0d72
BS
95#endif
96#ifdef __sun__
d5d10bc3
TS
97#include <sys/stat.h>
98#include <sys/ethernet.h>
99#include <sys/sockio.h>
d5d10bc3
TS
100#include <netinet/arp.h>
101#include <netinet/in.h>
102#include <netinet/in_systm.h>
103#include <netinet/ip.h>
104#include <netinet/ip_icmp.h> // must come after ip.h
105#include <netinet/udp.h>
106#include <netinet/tcp.h>
107#include <net/if.h>
108#include <syslog.h>
109#include <stropts.h>
67b915a5 110#endif
7d3505c5 111#endif
ec530c81 112#endif
67b915a5 113
03ff3ca3
AL
114#include "qemu_socket.h"
115
c20709aa
FB
116#if defined(CONFIG_SLIRP)
117#include "libslirp.h"
118#endif
119
9892fbfb
BS
120#if defined(__OpenBSD__)
121#include <util.h>
122#endif
123
8a16d273
TS
124#if defined(CONFIG_VDE)
125#include <libvdeplug.h>
126#endif
127
67b915a5 128#ifdef _WIN32
7d3505c5 129#include <malloc.h>
67b915a5 130#include <sys/timeb.h>
4fddf62a 131#include <mmsystem.h>
67b915a5
FB
132#define getopt_long_only getopt_long
133#define memalign(align, size) malloc(size)
134#endif
135
73332e5c 136#ifdef CONFIG_SDL
96bcd4f8 137#ifdef __APPLE__
83fb7adf 138#include <SDL/SDL.h>
96bcd4f8 139#endif
73332e5c 140#endif /* CONFIG_SDL */
0824d6fc 141
5b0753e0
FB
142#ifdef CONFIG_COCOA
143#undef main
144#define main qemu_main
145#endif /* CONFIG_COCOA */
146
0824d6fc 147#include "disas.h"
fc01f7e7 148
8a7ddc38 149#include "exec-all.h"
0824d6fc 150
5a67135a 151#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
b46a8906 152#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
a14d6c8c
PB
153#ifdef __sun__
154#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
155#else
156#define SMBD_COMMAND "/usr/sbin/smbd"
157#endif
f1510b2c 158
0824d6fc 159//#define DEBUG_UNUSED_IOPORT
fd872598 160//#define DEBUG_IOPORT
9dc63a1e
BS
161//#define DEBUG_NET
162//#define DEBUG_SLIRP
330d0414 163
77d4bc34
FB
164#ifdef TARGET_PPC
165#define DEFAULT_RAM_SIZE 144
166#else
1bfe856e 167#define DEFAULT_RAM_SIZE 128
77d4bc34 168#endif
313aa567 169
0d92ed30
PB
170/* Max number of USB devices that can be specified on the commandline. */
171#define MAX_USB_CMDLINE 8
172
dc72ac14
AZ
173/* Max number of bluetooth switches on the commandline. */
174#define MAX_BT_CMDLINE 10
175
7dea1da4
FB
176/* XXX: use a two level table to limit memory usage */
177#define MAX_IOPORTS 65536
0824d6fc 178
80cabfad 179const char *bios_dir = CONFIG_QEMU_SHAREDIR;
1192dad8 180const char *bios_name = NULL;
dbed7e40
BS
181static void *ioport_opaque[MAX_IOPORTS];
182static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
183static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
e4bcb14c 184/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
faea38e7 185 to store the VM snapshots */
e4bcb14c
TS
186DriveInfo drives_table[MAX_DRIVES+1];
187int nb_drives;
dbed7e40 188static int vga_ram_size;
cb5a7aa8 189enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
0e82f34d 190DisplayState display_state;
a20dd508 191int nographic;
dbed7e40 192static int curses;
3d11d0eb 193const char* keyboard_layout = NULL;
313aa567 194int64_t ticks_per_sec;
00f82b8a 195ram_addr_t ram_size;
c4b1fcc0 196int nb_nics;
7c9d8e07 197NICInfo nd_table[MAX_NICS];
8a7ddc38 198int vm_running;
f6503059
AZ
199static int rtc_utc = 1;
200static int rtc_date_offset = -1; /* -1 means no change */
1bfe856e 201int cirrus_vga_enabled = 1;
d34cab9f 202int vmsvga_enabled = 0;
d827220b
FB
203#ifdef TARGET_SPARC
204int graphic_width = 1024;
205int graphic_height = 768;
eee0b836 206int graphic_depth = 8;
d827220b 207#else
1bfe856e
FB
208int graphic_width = 800;
209int graphic_height = 600;
e9b137c2 210int graphic_depth = 15;
eee0b836 211#endif
dbed7e40 212static int full_screen = 0;
634a21f6 213#ifdef CONFIG_SDL
dbed7e40 214static int no_frame = 0;
634a21f6 215#endif
667accab 216int no_quit = 0;
8d11df9e 217CharDriverState *serial_hds[MAX_SERIAL_PORTS];
6508fe59 218CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
a09db21f
FB
219#ifdef TARGET_I386
220int win2k_install_hack = 0;
221#endif
bb36d470 222int usb_enabled = 0;
6a00d601 223int smp_cpus = 1;
73fc9742 224const char *vnc_display;
6515b203 225int acpi_enabled = 1;
52ca8d6a 226int fd_bootchk = 1;
d1beab82 227int no_reboot = 0;
b2f76161 228int no_shutdown = 0;
9467cd46 229int cursor_hide = 1;
a171fe39 230int graphic_rotate = 0;
71e3ceb8 231int daemonize = 0;
9ae02555
TS
232const char *option_rom[MAX_OPTION_ROMS];
233int nb_option_roms;
8e71621f 234int semihosting_enabled = 0;
2b8f2d41
AZ
235#ifdef TARGET_ARM
236int old_param = 0;
237#endif
c35734b2 238const char *qemu_name;
3780e197 239int alt_grab = 0;
66508601
BS
240#ifdef TARGET_SPARC
241unsigned int nb_prom_envs = 0;
242const char *prom_envs[MAX_PROM_ENVS];
243#endif
dbed7e40
BS
244static int nb_drives_opt;
245static struct drive_opt {
609497ab
AZ
246 const char *file;
247 char opt[1024];
248} drives_opt[MAX_DRIVES];
0824d6fc 249
ee5605e5
AZ
250static CPUState *cur_cpu;
251static CPUState *next_cpu;
76ea08f9 252static int event_pending = 1;
bf20dc07 253/* Conversion factor from emulated instructions to virtual clock ticks. */
2e70f6ef 254static int icount_time_shift;
bf20dc07 255/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
2e70f6ef
PB
256#define MAX_ICOUNT_SHIFT 10
257/* Compensate for varying guest execution speed. */
258static int64_t qemu_icount_bias;
dbed7e40
BS
259static QEMUTimer *icount_rt_timer;
260static QEMUTimer *icount_vm_timer;
ee5605e5 261
8fcb1b90
BS
262uint8_t qemu_uuid[16];
263
0824d6fc 264/***********************************************************/
26aa7d72
FB
265/* x86 ISA bus support */
266
267target_phys_addr_t isa_mem_base = 0;
3de388f6 268PicState2 *isa_pic;
0824d6fc 269
477e3edf
AL
270static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
271static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
272
273static uint32_t ioport_read(int index, uint32_t address)
274{
275 static IOPortReadFunc *default_func[3] = {
276 default_ioport_readb,
277 default_ioport_readw,
278 default_ioport_readl
279 };
280 IOPortReadFunc *func = ioport_read_table[index][address];
281 if (!func)
282 func = default_func[index];
283 return func(ioport_opaque[address], address);
284}
285
286static void ioport_write(int index, uint32_t address, uint32_t data)
287{
288 static IOPortWriteFunc *default_func[3] = {
289 default_ioport_writeb,
290 default_ioport_writew,
291 default_ioport_writel
292 };
293 IOPortWriteFunc *func = ioport_write_table[index][address];
294 if (!func)
295 func = default_func[index];
296 func(ioport_opaque[address], address, data);
297}
298
9596ebb7 299static uint32_t default_ioport_readb(void *opaque, uint32_t address)
0824d6fc
FB
300{
301#ifdef DEBUG_UNUSED_IOPORT
1196be37 302 fprintf(stderr, "unused inb: port=0x%04x\n", address);
0824d6fc 303#endif
fc01f7e7 304 return 0xff;
0824d6fc
FB
305}
306
9596ebb7 307static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
0824d6fc
FB
308{
309#ifdef DEBUG_UNUSED_IOPORT
1196be37 310 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
0824d6fc
FB
311#endif
312}
313
314/* default is to make two byte accesses */
9596ebb7 315static uint32_t default_ioport_readw(void *opaque, uint32_t address)
0824d6fc
FB
316{
317 uint32_t data;
477e3edf 318 data = ioport_read(0, address);
db45c29a 319 address = (address + 1) & (MAX_IOPORTS - 1);
477e3edf 320 data |= ioport_read(0, address) << 8;
0824d6fc
FB
321 return data;
322}
323
9596ebb7 324static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
0824d6fc 325{
477e3edf 326 ioport_write(0, address, data & 0xff);
db45c29a 327 address = (address + 1) & (MAX_IOPORTS - 1);
477e3edf 328 ioport_write(0, address, (data >> 8) & 0xff);
0824d6fc
FB
329}
330
9596ebb7 331static uint32_t default_ioport_readl(void *opaque, uint32_t address)
0824d6fc 332{
fc01f7e7 333#ifdef DEBUG_UNUSED_IOPORT
1196be37 334 fprintf(stderr, "unused inl: port=0x%04x\n", address);
fc01f7e7
FB
335#endif
336 return 0xffffffff;
0824d6fc
FB
337}
338
9596ebb7 339static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
0824d6fc 340{
fc01f7e7 341#ifdef DEBUG_UNUSED_IOPORT
1196be37 342 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
fc01f7e7 343#endif
0824d6fc
FB
344}
345
fc01f7e7 346/* size is the word size in byte */
5fafdf24 347int register_ioport_read(int start, int length, int size,
c4b1fcc0 348 IOPortReadFunc *func, void *opaque)
f1510b2c 349{
fc01f7e7 350 int i, bsize;
f1510b2c 351
c4b1fcc0 352 if (size == 1) {
fc01f7e7 353 bsize = 0;
c4b1fcc0 354 } else if (size == 2) {
fc01f7e7 355 bsize = 1;
c4b1fcc0 356 } else if (size == 4) {
fc01f7e7 357 bsize = 2;
c4b1fcc0 358 } else {
88fdf56f 359 hw_error("register_ioport_read: invalid size");
fc01f7e7 360 return -1;
c4b1fcc0
FB
361 }
362 for(i = start; i < start + length; i += size) {
fc01f7e7 363 ioport_read_table[bsize][i] = func;
c4b1fcc0 364 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
88fdf56f 365 hw_error("register_ioport_read: invalid opaque");
c4b1fcc0
FB
366 ioport_opaque[i] = opaque;
367 }
f1510b2c
FB
368 return 0;
369}
370
fc01f7e7 371/* size is the word size in byte */
5fafdf24 372int register_ioport_write(int start, int length, int size,
c4b1fcc0 373 IOPortWriteFunc *func, void *opaque)
f1510b2c 374{
fc01f7e7 375 int i, bsize;
f1510b2c 376
c4b1fcc0 377 if (size == 1) {
fc01f7e7 378 bsize = 0;
c4b1fcc0 379 } else if (size == 2) {
fc01f7e7 380 bsize = 1;
c4b1fcc0 381 } else if (size == 4) {
fc01f7e7 382 bsize = 2;
c4b1fcc0 383 } else {
88fdf56f 384 hw_error("register_ioport_write: invalid size");
fc01f7e7 385 return -1;
c4b1fcc0
FB
386 }
387 for(i = start; i < start + length; i += size) {
fc01f7e7 388 ioport_write_table[bsize][i] = func;
88fdf56f
AZ
389 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
390 hw_error("register_ioport_write: invalid opaque");
c4b1fcc0
FB
391 ioport_opaque[i] = opaque;
392 }
f1510b2c
FB
393 return 0;
394}
395
69b91039
FB
396void isa_unassign_ioport(int start, int length)
397{
398 int i;
399
400 for(i = start; i < start + length; i++) {
401 ioport_read_table[0][i] = default_ioport_readb;
402 ioport_read_table[1][i] = default_ioport_readw;
403 ioport_read_table[2][i] = default_ioport_readl;
404
405 ioport_write_table[0][i] = default_ioport_writeb;
406 ioport_write_table[1][i] = default_ioport_writew;
407 ioport_write_table[2][i] = default_ioport_writel;
408 }
409}
410
20f32282
FB
411/***********************************************************/
412
c45886db 413void cpu_outb(CPUState *env, int addr, int val)
0824d6fc 414{
fd872598
FB
415#ifdef DEBUG_IOPORT
416 if (loglevel & CPU_LOG_IOPORT)
417 fprintf(logfile, "outb: %04x %02x\n", addr, val);
3b46e624 418#endif
477e3edf 419 ioport_write(0, addr, val);
89bfc105
FB
420#ifdef USE_KQEMU
421 if (env)
422 env->last_io_time = cpu_get_time_fast();
423#endif
0824d6fc
FB
424}
425
c45886db 426void cpu_outw(CPUState *env, int addr, int val)
0824d6fc 427{
fd872598
FB
428#ifdef DEBUG_IOPORT
429 if (loglevel & CPU_LOG_IOPORT)
430 fprintf(logfile, "outw: %04x %04x\n", addr, val);
3b46e624 431#endif
477e3edf 432 ioport_write(1, addr, val);
89bfc105
FB
433#ifdef USE_KQEMU
434 if (env)
435 env->last_io_time = cpu_get_time_fast();
436#endif
0824d6fc
FB
437}
438
c45886db 439void cpu_outl(CPUState *env, int addr, int val)
0824d6fc 440{
fd872598
FB
441#ifdef DEBUG_IOPORT
442 if (loglevel & CPU_LOG_IOPORT)
443 fprintf(logfile, "outl: %04x %08x\n", addr, val);
444#endif
477e3edf 445 ioport_write(2, addr, val);
89bfc105
FB
446#ifdef USE_KQEMU
447 if (env)
448 env->last_io_time = cpu_get_time_fast();
449#endif
0824d6fc
FB
450}
451
c45886db 452int cpu_inb(CPUState *env, int addr)
0824d6fc 453{
fd872598 454 int val;
477e3edf 455 val = ioport_read(0, addr);
fd872598
FB
456#ifdef DEBUG_IOPORT
457 if (loglevel & CPU_LOG_IOPORT)
458 fprintf(logfile, "inb : %04x %02x\n", addr, val);
89bfc105
FB
459#endif
460#ifdef USE_KQEMU
461 if (env)
462 env->last_io_time = cpu_get_time_fast();
fd872598
FB
463#endif
464 return val;
0824d6fc
FB
465}
466
c45886db 467int cpu_inw(CPUState *env, int addr)
0824d6fc 468{
fd872598 469 int val;
477e3edf 470 val = ioport_read(1, addr);
fd872598
FB
471#ifdef DEBUG_IOPORT
472 if (loglevel & CPU_LOG_IOPORT)
473 fprintf(logfile, "inw : %04x %04x\n", addr, val);
89bfc105
FB
474#endif
475#ifdef USE_KQEMU
476 if (env)
477 env->last_io_time = cpu_get_time_fast();
fd872598
FB
478#endif
479 return val;
0824d6fc
FB
480}
481
c45886db 482int cpu_inl(CPUState *env, int addr)
0824d6fc 483{
fd872598 484 int val;
477e3edf 485 val = ioport_read(2, addr);
fd872598
FB
486#ifdef DEBUG_IOPORT
487 if (loglevel & CPU_LOG_IOPORT)
488 fprintf(logfile, "inl : %04x %08x\n", addr, val);
89bfc105
FB
489#endif
490#ifdef USE_KQEMU
491 if (env)
492 env->last_io_time = cpu_get_time_fast();
fd872598
FB
493#endif
494 return val;
0824d6fc
FB
495}
496
497/***********************************************************/
0824d6fc
FB
498void hw_error(const char *fmt, ...)
499{
500 va_list ap;
6a00d601 501 CPUState *env;
0824d6fc
FB
502
503 va_start(ap, fmt);
504 fprintf(stderr, "qemu: hardware error: ");
505 vfprintf(stderr, fmt, ap);
506 fprintf(stderr, "\n");
6a00d601
FB
507 for(env = first_cpu; env != NULL; env = env->next_cpu) {
508 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
0824d6fc 509#ifdef TARGET_I386
6a00d601 510 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
c45886db 511#else
6a00d601 512 cpu_dump_state(env, stderr, fprintf, 0);
0824d6fc 513#endif
6a00d601 514 }
0824d6fc
FB
515 va_end(ap);
516 abort();
517}
df751fa8
AL
518
519/***************/
520/* ballooning */
521
522static QEMUBalloonEvent *qemu_balloon_event;
523void *qemu_balloon_event_opaque;
524
525void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
526{
527 qemu_balloon_event = func;
528 qemu_balloon_event_opaque = opaque;
529}
530
531void qemu_balloon(ram_addr_t target)
532{
533 if (qemu_balloon_event)
534 qemu_balloon_event(qemu_balloon_event_opaque, target);
535}
536
537ram_addr_t qemu_balloon_status(void)
538{
539 if (qemu_balloon_event)
540 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
541 return 0;
542}
0824d6fc 543
63066f4f
FB
544/***********************************************************/
545/* keyboard/mouse */
546
547static QEMUPutKBDEvent *qemu_put_kbd_event;
548static void *qemu_put_kbd_event_opaque;
455204eb
TS
549static QEMUPutMouseEntry *qemu_put_mouse_event_head;
550static QEMUPutMouseEntry *qemu_put_mouse_event_current;
63066f4f
FB
551
552void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
553{
554 qemu_put_kbd_event_opaque = opaque;
555 qemu_put_kbd_event = func;
556}
557
455204eb
TS
558QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
559 void *opaque, int absolute,
560 const char *name)
63066f4f 561{
455204eb
TS
562 QEMUPutMouseEntry *s, *cursor;
563
564 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
565 if (!s)
566 return NULL;
567
568 s->qemu_put_mouse_event = func;
569 s->qemu_put_mouse_event_opaque = opaque;
570 s->qemu_put_mouse_event_absolute = absolute;
571 s->qemu_put_mouse_event_name = qemu_strdup(name);
572 s->next = NULL;
573
574 if (!qemu_put_mouse_event_head) {
575 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
576 return s;
577 }
578
579 cursor = qemu_put_mouse_event_head;
580 while (cursor->next != NULL)
581 cursor = cursor->next;
582
583 cursor->next = s;
584 qemu_put_mouse_event_current = s;
585
586 return s;
587}
588
589void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
590{
591 QEMUPutMouseEntry *prev = NULL, *cursor;
592
593 if (!qemu_put_mouse_event_head || entry == NULL)
594 return;
595
596 cursor = qemu_put_mouse_event_head;
597 while (cursor != NULL && cursor != entry) {
598 prev = cursor;
599 cursor = cursor->next;
600 }
601
602 if (cursor == NULL) // does not exist or list empty
603 return;
604 else if (prev == NULL) { // entry is head
605 qemu_put_mouse_event_head = cursor->next;
606 if (qemu_put_mouse_event_current == entry)
607 qemu_put_mouse_event_current = cursor->next;
608 qemu_free(entry->qemu_put_mouse_event_name);
609 qemu_free(entry);
610 return;
611 }
612
613 prev->next = entry->next;
614
615 if (qemu_put_mouse_event_current == entry)
616 qemu_put_mouse_event_current = prev;
617
618 qemu_free(entry->qemu_put_mouse_event_name);
619 qemu_free(entry);
63066f4f
FB
620}
621
622void kbd_put_keycode(int keycode)
623{
624 if (qemu_put_kbd_event) {
625 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
626 }
627}
628
629void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
630{
455204eb
TS
631 QEMUPutMouseEvent *mouse_event;
632 void *mouse_event_opaque;
a171fe39 633 int width;
455204eb
TS
634
635 if (!qemu_put_mouse_event_current) {
636 return;
637 }
638
639 mouse_event =
640 qemu_put_mouse_event_current->qemu_put_mouse_event;
641 mouse_event_opaque =
642 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
643
644 if (mouse_event) {
a171fe39
AZ
645 if (graphic_rotate) {
646 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
647 width = 0x7fff;
648 else
b94ed577 649 width = graphic_width - 1;
a171fe39
AZ
650 mouse_event(mouse_event_opaque,
651 width - dy, dx, dz, buttons_state);
652 } else
653 mouse_event(mouse_event_opaque,
654 dx, dy, dz, buttons_state);
63066f4f
FB
655 }
656}
657
09b26c5e
FB
658int kbd_mouse_is_absolute(void)
659{
455204eb
TS
660 if (!qemu_put_mouse_event_current)
661 return 0;
662
663 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
664}
665
666void do_info_mice(void)
667{
668 QEMUPutMouseEntry *cursor;
669 int index = 0;
670
671 if (!qemu_put_mouse_event_head) {
672 term_printf("No mouse devices connected\n");
673 return;
674 }
675
676 term_printf("Mouse devices available:\n");
677 cursor = qemu_put_mouse_event_head;
678 while (cursor != NULL) {
679 term_printf("%c Mouse #%d: %s\n",
680 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
681 index, cursor->qemu_put_mouse_event_name);
682 index++;
683 cursor = cursor->next;
684 }
685}
686
687void do_mouse_set(int index)
688{
689 QEMUPutMouseEntry *cursor;
690 int i = 0;
691
692 if (!qemu_put_mouse_event_head) {
693 term_printf("No mouse devices connected\n");
694 return;
695 }
696
697 cursor = qemu_put_mouse_event_head;
698 while (cursor != NULL && index != i) {
699 i++;
700 cursor = cursor->next;
701 }
702
703 if (cursor != NULL)
704 qemu_put_mouse_event_current = cursor;
705 else
706 term_printf("Mouse at given index not found\n");
09b26c5e
FB
707}
708
1dce7c3c
FB
709/* compute with 96 bit intermediate result: (a*b)/c */
710uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
0824d6fc 711{
1dce7c3c
FB
712 union {
713 uint64_t ll;
714 struct {
715#ifdef WORDS_BIGENDIAN
716 uint32_t high, low;
717#else
718 uint32_t low, high;
3b46e624 719#endif
1dce7c3c
FB
720 } l;
721 } u, res;
722 uint64_t rl, rh;
0824d6fc 723
1dce7c3c
FB
724 u.ll = a;
725 rl = (uint64_t)u.l.low * (uint64_t)b;
726 rh = (uint64_t)u.l.high * (uint64_t)b;
727 rh += (rl >> 32);
728 res.l.high = rh / c;
729 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
730 return res.ll;
34865134
FB
731}
732
1dce7c3c
FB
733/***********************************************************/
734/* real time host monotonic timer */
34865134 735
1dce7c3c 736#define QEMU_TIMER_BASE 1000000000LL
34865134 737
1dce7c3c 738#ifdef WIN32
0824d6fc 739
1dce7c3c 740static int64_t clock_freq;
1115dde7 741
1dce7c3c 742static void init_get_clock(void)
1115dde7 743{
a8e5ac33
FB
744 LARGE_INTEGER freq;
745 int ret;
1dce7c3c
FB
746 ret = QueryPerformanceFrequency(&freq);
747 if (ret == 0) {
748 fprintf(stderr, "Could not calibrate ticks\n");
749 exit(1);
750 }
751 clock_freq = freq.QuadPart;
1115dde7
FB
752}
753
1dce7c3c 754static int64_t get_clock(void)
b8076a74 755{
1dce7c3c
FB
756 LARGE_INTEGER ti;
757 QueryPerformanceCounter(&ti);
758 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
b8076a74
FB
759}
760
1dce7c3c 761#else
90cb9493 762
1dce7c3c
FB
763static int use_rt_clock;
764
765static void init_get_clock(void)
90cb9493 766{
1dce7c3c 767 use_rt_clock = 0;
60759371 768#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
1dce7c3c
FB
769 {
770 struct timespec ts;
771 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
772 use_rt_clock = 1;
773 }
774 }
775#endif
90cb9493
FB
776}
777
1dce7c3c 778static int64_t get_clock(void)
fdbb4691 779{
60759371 780#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
1dce7c3c
FB
781 if (use_rt_clock) {
782 struct timespec ts;
783 clock_gettime(CLOCK_MONOTONIC, &ts);
784 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
5fafdf24 785 } else
fdbb4691 786#endif
1dce7c3c
FB
787 {
788 /* XXX: using gettimeofday leads to problems if the date
789 changes, so it should be avoided. */
790 struct timeval tv;
791 gettimeofday(&tv, NULL);
792 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
793 }
fdbb4691 794}
34865134
FB
795#endif
796
2e70f6ef
PB
797/* Return the virtual CPU time, based on the instruction counter. */
798static int64_t cpu_get_icount(void)
799{
800 int64_t icount;
801 CPUState *env = cpu_single_env;;
802 icount = qemu_icount;
803 if (env) {
804 if (!can_do_io(env))
805 fprintf(stderr, "Bad clock read\n");
806 icount -= (env->icount_decr.u16.low + env->icount_extra);
807 }
808 return qemu_icount_bias + (icount << icount_time_shift);
809}
810
1dce7c3c
FB
811/***********************************************************/
812/* guest cycle counter */
813
eade0f19 814static int64_t cpu_ticks_prev;
34865134 815static int64_t cpu_ticks_offset;
1dce7c3c 816static int64_t cpu_clock_offset;
8a7ddc38 817static int cpu_ticks_enabled;
34865134 818
1dce7c3c
FB
819/* return the host CPU cycle counter and handle stop/restart */
820int64_t cpu_get_ticks(void)
34865134 821{
2e70f6ef
PB
822 if (use_icount) {
823 return cpu_get_icount();
824 }
8a7ddc38
FB
825 if (!cpu_ticks_enabled) {
826 return cpu_ticks_offset;
827 } else {
eade0f19
FB
828 int64_t ticks;
829 ticks = cpu_get_real_ticks();
830 if (cpu_ticks_prev > ticks) {
831 /* Note: non increasing ticks may happen if the host uses
832 software suspend */
833 cpu_ticks_offset += cpu_ticks_prev - ticks;
834 }
835 cpu_ticks_prev = ticks;
836 return ticks + cpu_ticks_offset;
8a7ddc38 837 }
34865134
FB
838}
839
1dce7c3c
FB
840/* return the host CPU monotonic timer and handle stop/restart */
841static int64_t cpu_get_clock(void)
842{
843 int64_t ti;
844 if (!cpu_ticks_enabled) {
845 return cpu_clock_offset;
846 } else {
847 ti = get_clock();
848 return ti + cpu_clock_offset;
849 }
850}
851
34865134
FB
852/* enable cpu_get_ticks() */
853void cpu_enable_ticks(void)
854{
8a7ddc38
FB
855 if (!cpu_ticks_enabled) {
856 cpu_ticks_offset -= cpu_get_real_ticks();
1dce7c3c 857 cpu_clock_offset -= get_clock();
8a7ddc38
FB
858 cpu_ticks_enabled = 1;
859 }
34865134
FB
860}
861
862/* disable cpu_get_ticks() : the clock is stopped. You must not call
863 cpu_get_ticks() after that. */
864void cpu_disable_ticks(void)
865{
8a7ddc38
FB
866 if (cpu_ticks_enabled) {
867 cpu_ticks_offset = cpu_get_ticks();
1dce7c3c 868 cpu_clock_offset = cpu_get_clock();
8a7ddc38
FB
869 cpu_ticks_enabled = 0;
870 }
34865134
FB
871}
872
1dce7c3c
FB
873/***********************************************************/
874/* timers */
5fafdf24 875
8a7ddc38
FB
876#define QEMU_TIMER_REALTIME 0
877#define QEMU_TIMER_VIRTUAL 1
878
879struct QEMUClock {
880 int type;
881 /* XXX: add frequency */
882};
883
884struct QEMUTimer {
885 QEMUClock *clock;
886 int64_t expire_time;
887 QEMUTimerCB *cb;
888 void *opaque;
889 struct QEMUTimer *next;
890};
891
c8994013
TS
892struct qemu_alarm_timer {
893 char const *name;
efe75411 894 unsigned int flags;
c8994013
TS
895
896 int (*start)(struct qemu_alarm_timer *t);
897 void (*stop)(struct qemu_alarm_timer *t);
efe75411 898 void (*rearm)(struct qemu_alarm_timer *t);
c8994013
TS
899 void *priv;
900};
901
efe75411 902#define ALARM_FLAG_DYNTICKS 0x1
d5d08334 903#define ALARM_FLAG_EXPIRED 0x2
efe75411
TS
904
905static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
906{
907 return t->flags & ALARM_FLAG_DYNTICKS;
908}
909
910static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
911{
912 if (!alarm_has_dynticks(t))
913 return;
914
915 t->rearm(t);
916}
917
918/* TODO: MIN_TIMER_REARM_US should be optimized */
919#define MIN_TIMER_REARM_US 250
920
c8994013 921static struct qemu_alarm_timer *alarm_timer;
f49e58dc 922#ifndef _WIN32
c96f1a48 923static int alarm_timer_rfd, alarm_timer_wfd;
f49e58dc 924#endif
8a7ddc38 925
40c3bac3 926#ifdef _WIN32
c8994013
TS
927
928struct qemu_alarm_win32 {
929 MMRESULT timerId;
930 HANDLE host_alarm;
931 unsigned int period;
932} alarm_win32_data = {0, NULL, -1};
933
934static int win32_start_timer(struct qemu_alarm_timer *t);
935static void win32_stop_timer(struct qemu_alarm_timer *t);
efe75411 936static void win32_rearm_timer(struct qemu_alarm_timer *t);
c8994013 937
40c3bac3 938#else
c8994013
TS
939
940static int unix_start_timer(struct qemu_alarm_timer *t);
941static void unix_stop_timer(struct qemu_alarm_timer *t);
942
231c6586
TS
943#ifdef __linux__
944
efe75411
TS
945static int dynticks_start_timer(struct qemu_alarm_timer *t);
946static void dynticks_stop_timer(struct qemu_alarm_timer *t);
947static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
948
c40ec5a9
TS
949static int hpet_start_timer(struct qemu_alarm_timer *t);
950static void hpet_stop_timer(struct qemu_alarm_timer *t);
951
c8994013
TS
952static int rtc_start_timer(struct qemu_alarm_timer *t);
953static void rtc_stop_timer(struct qemu_alarm_timer *t);
954
efe75411 955#endif /* __linux__ */
8a7ddc38 956
c8994013
TS
957#endif /* _WIN32 */
958
2e70f6ef 959/* Correlation between real and virtual time is always going to be
bf20dc07 960 fairly approximate, so ignore small variation.
2e70f6ef
PB
961 When the guest is idle real and virtual time will be aligned in
962 the IO wait loop. */
963#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
964
965static void icount_adjust(void)
966{
967 int64_t cur_time;
968 int64_t cur_icount;
969 int64_t delta;
970 static int64_t last_delta;
971 /* If the VM is not running, then do nothing. */
972 if (!vm_running)
973 return;
974
975 cur_time = cpu_get_clock();
976 cur_icount = qemu_get_clock(vm_clock);
977 delta = cur_icount - cur_time;
978 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
979 if (delta > 0
980 && last_delta + ICOUNT_WOBBLE < delta * 2
981 && icount_time_shift > 0) {
982 /* The guest is getting too far ahead. Slow time down. */
983 icount_time_shift--;
984 }
985 if (delta < 0
986 && last_delta - ICOUNT_WOBBLE > delta * 2
987 && icount_time_shift < MAX_ICOUNT_SHIFT) {
988 /* The guest is getting too far behind. Speed time up. */
989 icount_time_shift++;
990 }
991 last_delta = delta;
992 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
993}
994
995static void icount_adjust_rt(void * opaque)
996{
997 qemu_mod_timer(icount_rt_timer,
998 qemu_get_clock(rt_clock) + 1000);
999 icount_adjust();
1000}
1001
1002static void icount_adjust_vm(void * opaque)
1003{
1004 qemu_mod_timer(icount_vm_timer,
1005 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1006 icount_adjust();
1007}
1008
1009static void init_icount_adjust(void)
1010{
1011 /* Have both realtime and virtual time triggers for speed adjustment.
1012 The realtime trigger catches emulated time passing too slowly,
1013 the virtual time trigger catches emulated time passing too fast.
1014 Realtime triggers occur even when idle, so use them less frequently
1015 than VM triggers. */
1016 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
1017 qemu_mod_timer(icount_rt_timer,
1018 qemu_get_clock(rt_clock) + 1000);
1019 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1020 qemu_mod_timer(icount_vm_timer,
1021 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1022}
1023
c8994013 1024static struct qemu_alarm_timer alarm_timers[] = {
efe75411 1025#ifndef _WIN32
231c6586 1026#ifdef __linux__
efe75411
TS
1027 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1028 dynticks_stop_timer, dynticks_rearm_timer, NULL},
c40ec5a9 1029 /* HPET - if available - is preferred */
efe75411 1030 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
c40ec5a9 1031 /* ...otherwise try RTC */
efe75411 1032 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
c8994013 1033#endif
efe75411 1034 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
c8994013 1035#else
efe75411
TS
1036 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1037 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1038 {"win32", 0, win32_start_timer,
1039 win32_stop_timer, NULL, &alarm_win32_data},
c8994013
TS
1040#endif
1041 {NULL, }
1042};
1043
3f47aa8c 1044static void show_available_alarms(void)
f3dcfada
TS
1045{
1046 int i;
1047
1048 printf("Available alarm timers, in order of precedence:\n");
1049 for (i = 0; alarm_timers[i].name; i++)
1050 printf("%s\n", alarm_timers[i].name);
1051}
1052
1053static void configure_alarms(char const *opt)
1054{
1055 int i;
1056 int cur = 0;
1057 int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
1058 char *arg;
1059 char *name;
2e70f6ef 1060 struct qemu_alarm_timer tmp;
f3dcfada 1061
3adda04c 1062 if (!strcmp(opt, "?")) {
f3dcfada
TS
1063 show_available_alarms();
1064 exit(0);
1065 }
1066
1067 arg = strdup(opt);
1068
1069 /* Reorder the array */
1070 name = strtok(arg, ",");
1071 while (name) {
e2b577e5 1072 for (i = 0; i < count && alarm_timers[i].name; i++) {
f3dcfada
TS
1073 if (!strcmp(alarm_timers[i].name, name))
1074 break;
1075 }
1076
1077 if (i == count) {
1078 fprintf(stderr, "Unknown clock %s\n", name);
1079 goto next;
1080 }
1081
1082 if (i < cur)
1083 /* Ignore */
1084 goto next;
1085
1086 /* Swap */
1087 tmp = alarm_timers[i];
1088 alarm_timers[i] = alarm_timers[cur];
1089 alarm_timers[cur] = tmp;
1090
1091 cur++;
1092next:
1093 name = strtok(NULL, ",");
1094 }
1095
1096 free(arg);
1097
1098 if (cur) {
2e70f6ef 1099 /* Disable remaining timers */
f3dcfada
TS
1100 for (i = cur; i < count; i++)
1101 alarm_timers[i].name = NULL;
3adda04c
AJ
1102 } else {
1103 show_available_alarms();
1104 exit(1);
f3dcfada 1105 }
f3dcfada
TS
1106}
1107
c8994013
TS
1108QEMUClock *rt_clock;
1109QEMUClock *vm_clock;
1110
1111static QEMUTimer *active_timers[2];
1112
9596ebb7 1113static QEMUClock *qemu_new_clock(int type)
8a7ddc38
FB
1114{
1115 QEMUClock *clock;
1116 clock = qemu_mallocz(sizeof(QEMUClock));
1117 if (!clock)
1118 return NULL;
1119 clock->type = type;
1120 return clock;
1121}
1122
1123QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1124{
1125 QEMUTimer *ts;
1126
1127 ts = qemu_mallocz(sizeof(QEMUTimer));
1128 ts->clock = clock;
1129 ts->cb = cb;
1130 ts->opaque = opaque;
1131 return ts;
1132}
1133
1134void qemu_free_timer(QEMUTimer *ts)
1135{
1136 qemu_free(ts);
1137}
1138
1139/* stop a timer, but do not dealloc it */
1140void qemu_del_timer(QEMUTimer *ts)
1141{
1142 QEMUTimer **pt, *t;
1143
1144 /* NOTE: this code must be signal safe because
1145 qemu_timer_expired() can be called from a signal. */
1146 pt = &active_timers[ts->clock->type];
1147 for(;;) {
1148 t = *pt;
1149 if (!t)
1150 break;
1151 if (t == ts) {
1152 *pt = t->next;
1153 break;
1154 }
1155 pt = &t->next;
1156 }
1157}
1158
1159/* modify the current timer so that it will be fired when current_time
1160 >= expire_time. The corresponding callback will be called. */
1161void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1162{
1163 QEMUTimer **pt, *t;
1164
1165 qemu_del_timer(ts);
1166
1167 /* add the timer in the sorted list */
1168 /* NOTE: this code must be signal safe because
1169 qemu_timer_expired() can be called from a signal. */
1170 pt = &active_timers[ts->clock->type];
1171 for(;;) {
1172 t = *pt;
1173 if (!t)
1174 break;
5fafdf24 1175 if (t->expire_time > expire_time)
8a7ddc38
FB
1176 break;
1177 pt = &t->next;
1178 }
1179 ts->expire_time = expire_time;
1180 ts->next = *pt;
1181 *pt = ts;
d5d08334
AZ
1182
1183 /* Rearm if necessary */
2e70f6ef
PB
1184 if (pt == &active_timers[ts->clock->type]) {
1185 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1186 qemu_rearm_alarm_timer(alarm_timer);
1187 }
1188 /* Interrupt execution to force deadline recalculation. */
1189 if (use_icount && cpu_single_env) {
1190 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1191 }
1192 }
8a7ddc38
FB
1193}
1194
1195int qemu_timer_pending(QEMUTimer *ts)
1196{
1197 QEMUTimer *t;
1198 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1199 if (t == ts)
1200 return 1;
1201 }
1202 return 0;
1203}
1204
1205static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1206{
1207 if (!timer_head)
1208 return 0;
1209 return (timer_head->expire_time <= current_time);
1210}
1211
1212static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1213{
1214 QEMUTimer *ts;
3b46e624 1215
8a7ddc38
FB
1216 for(;;) {
1217 ts = *ptimer_head;
e95c8d51 1218 if (!ts || ts->expire_time > current_time)
8a7ddc38
FB
1219 break;
1220 /* remove timer from the list before calling the callback */
1221 *ptimer_head = ts->next;
1222 ts->next = NULL;
3b46e624 1223
8a7ddc38
FB
1224 /* run the callback (the timer list can be modified) */
1225 ts->cb(ts->opaque);
1226 }
1227}
1228
1229int64_t qemu_get_clock(QEMUClock *clock)
1230{
1231 switch(clock->type) {
1232 case QEMU_TIMER_REALTIME:
1dce7c3c 1233 return get_clock() / 1000000;
8a7ddc38
FB
1234 default:
1235 case QEMU_TIMER_VIRTUAL:
2e70f6ef
PB
1236 if (use_icount) {
1237 return cpu_get_icount();
1238 } else {
1239 return cpu_get_clock();
1240 }
8a7ddc38
FB
1241 }
1242}
1243
1dce7c3c
FB
1244static void init_timers(void)
1245{
1246 init_get_clock();
1247 ticks_per_sec = QEMU_TIMER_BASE;
1248 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1249 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1250}
1251
8a7ddc38
FB
1252/* save a timer */
1253void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1254{
1255 uint64_t expire_time;
1256
1257 if (qemu_timer_pending(ts)) {
1258 expire_time = ts->expire_time;
1259 } else {
1260 expire_time = -1;
1261 }
1262 qemu_put_be64(f, expire_time);
1263}
1264
1265void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1266{
1267 uint64_t expire_time;
1268
1269 expire_time = qemu_get_be64(f);
1270 if (expire_time != -1) {
1271 qemu_mod_timer(ts, expire_time);
1272 } else {
1273 qemu_del_timer(ts);
1274 }
1275}
1276
1277static void timer_save(QEMUFile *f, void *opaque)
1278{
1279 if (cpu_ticks_enabled) {
1280 hw_error("cannot save state if virtual timers are running");
1281 }
bee8d684
TS
1282 qemu_put_be64(f, cpu_ticks_offset);
1283 qemu_put_be64(f, ticks_per_sec);
1284 qemu_put_be64(f, cpu_clock_offset);
8a7ddc38
FB
1285}
1286
1287static int timer_load(QEMUFile *f, void *opaque, int version_id)
1288{
c88676f8 1289 if (version_id != 1 && version_id != 2)
8a7ddc38
FB
1290 return -EINVAL;
1291 if (cpu_ticks_enabled) {
1292 return -EINVAL;
1293 }
bee8d684
TS
1294 cpu_ticks_offset=qemu_get_be64(f);
1295 ticks_per_sec=qemu_get_be64(f);
c88676f8 1296 if (version_id == 2) {
bee8d684 1297 cpu_clock_offset=qemu_get_be64(f);
c88676f8 1298 }
8a7ddc38
FB
1299 return 0;
1300}
1301
67b915a5 1302#ifdef _WIN32
5fafdf24 1303void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
67b915a5
FB
1304 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1305#else
8a7ddc38 1306static void host_alarm_handler(int host_signum)
67b915a5 1307#endif
8a7ddc38 1308{
02ba45c5
FB
1309#if 0
1310#define DISP_FREQ 1000
1311 {
1312 static int64_t delta_min = INT64_MAX;
1313 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1314 static int count;
1315 ti = qemu_get_clock(vm_clock);
1316 if (last_clock != 0) {
1317 delta = ti - last_clock;
1318 if (delta < delta_min)
1319 delta_min = delta;
1320 if (delta > delta_max)
1321 delta_max = delta;
1322 delta_cum += delta;
1323 if (++count == DISP_FREQ) {
26a76461 1324 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
02ba45c5
FB
1325 muldiv64(delta_min, 1000000, ticks_per_sec),
1326 muldiv64(delta_max, 1000000, ticks_per_sec),
1327 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1328 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1329 count = 0;
1330 delta_min = INT64_MAX;
1331 delta_max = 0;
1332 delta_cum = 0;
1333 }
1334 }
1335 last_clock = ti;
1336 }
1337#endif
efe75411 1338 if (alarm_has_dynticks(alarm_timer) ||
2e70f6ef
PB
1339 (!use_icount &&
1340 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1341 qemu_get_clock(vm_clock))) ||
8a7ddc38
FB
1342 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1343 qemu_get_clock(rt_clock))) {
c96f1a48 1344 CPUState *env = next_cpu;
c96f1a48 1345
06d9f2f7 1346#ifdef _WIN32
c8994013
TS
1347 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1348 SetEvent(data->host_alarm);
f49e58dc
AL
1349#else
1350 static const char byte = 0;
c96f1a48 1351 write(alarm_timer_wfd, &byte, sizeof(byte));
f49e58dc 1352#endif
d5d08334
AZ
1353 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1354
4f8eb8da
AZ
1355 if (env) {
1356 /* stop the currently executing cpu because a timer occured */
1357 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
a332e112 1358#ifdef USE_KQEMU
4f8eb8da
AZ
1359 if (env->kqemu_enabled) {
1360 kqemu_cpu_interrupt(env);
1361 }
ee5605e5 1362#endif
4f8eb8da 1363 }
ee5605e5 1364 event_pending = 1;
8a7ddc38
FB
1365 }
1366}
1367
2e70f6ef 1368static int64_t qemu_next_deadline(void)
efe75411 1369{
2e70f6ef 1370 int64_t delta;
efe75411
TS
1371
1372 if (active_timers[QEMU_TIMER_VIRTUAL]) {
2e70f6ef
PB
1373 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1374 qemu_get_clock(vm_clock);
1375 } else {
1376 /* To avoid problems with overflow limit this to 2^32. */
1377 delta = INT32_MAX;
efe75411
TS
1378 }
1379
2e70f6ef
PB
1380 if (delta < 0)
1381 delta = 0;
efe75411 1382
2e70f6ef
PB
1383 return delta;
1384}
1385
8632fb9a 1386#if defined(__linux__) || defined(_WIN32)
2e70f6ef
PB
1387static uint64_t qemu_next_deadline_dyntick(void)
1388{
1389 int64_t delta;
1390 int64_t rtdelta;
1391
1392 if (use_icount)
1393 delta = INT32_MAX;
1394 else
1395 delta = (qemu_next_deadline() + 999) / 1000;
1396
1397 if (active_timers[QEMU_TIMER_REALTIME]) {
1398 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1399 qemu_get_clock(rt_clock))*1000;
1400 if (rtdelta < delta)
1401 delta = rtdelta;
1402 }
1403
1404 if (delta < MIN_TIMER_REARM_US)
1405 delta = MIN_TIMER_REARM_US;
1406
1407 return delta;
efe75411 1408}
8632fb9a 1409#endif
efe75411 1410
fd872598
FB
1411#ifndef _WIN32
1412
7183b4b4
AL
1413/* Sets a specific flag */
1414static int fcntl_setfl(int fd, int flag)
1415{
1416 int flags;
1417
1418 flags = fcntl(fd, F_GETFL);
1419 if (flags == -1)
1420 return -errno;
1421
1422 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1423 return -errno;
1424
1425 return 0;
1426}
1427
829309c7
FB
1428#if defined(__linux__)
1429
fd872598
FB
1430#define RTC_FREQ 1024
1431
de9a95f0 1432static void enable_sigio_timer(int fd)
c8994013
TS
1433{
1434 struct sigaction act;
1435
1436 /* timer signal */
1437 sigfillset(&act.sa_mask);
1438 act.sa_flags = 0;
c8994013
TS
1439 act.sa_handler = host_alarm_handler;
1440
1441 sigaction(SIGIO, &act, NULL);
7183b4b4 1442 fcntl_setfl(fd, O_ASYNC);
c8994013
TS
1443 fcntl(fd, F_SETOWN, getpid());
1444}
829309c7 1445
c40ec5a9
TS
1446static int hpet_start_timer(struct qemu_alarm_timer *t)
1447{
1448 struct hpet_info info;
1449 int r, fd;
1450
1451 fd = open("/dev/hpet", O_RDONLY);
1452 if (fd < 0)
1453 return -1;
1454
1455 /* Set frequency */
1456 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1457 if (r < 0) {
1458 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1459 "error, but for better emulation accuracy type:\n"
1460 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1461 goto fail;
1462 }
1463
1464 /* Check capabilities */
1465 r = ioctl(fd, HPET_INFO, &info);
1466 if (r < 0)
1467 goto fail;
1468
1469 /* Enable periodic mode */
1470 r = ioctl(fd, HPET_EPI, 0);
1471 if (info.hi_flags && (r < 0))
1472 goto fail;
1473
1474 /* Enable interrupt */
1475 r = ioctl(fd, HPET_IE_ON, 0);
1476 if (r < 0)
1477 goto fail;
1478
1479 enable_sigio_timer(fd);
fcdc2129 1480 t->priv = (void *)(long)fd;
c40ec5a9
TS
1481
1482 return 0;
1483fail:
1484 close(fd);
1485 return -1;
1486}
1487
1488static void hpet_stop_timer(struct qemu_alarm_timer *t)
1489{
fcdc2129 1490 int fd = (long)t->priv;
c40ec5a9
TS
1491
1492 close(fd);
1493}
1494
c8994013 1495static int rtc_start_timer(struct qemu_alarm_timer *t)
fd872598 1496{
c8994013 1497 int rtc_fd;
b5a23ad4 1498 unsigned long current_rtc_freq = 0;
c8994013 1499
aeb30be6 1500 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
fd872598
FB
1501 if (rtc_fd < 0)
1502 return -1;
b5a23ad4
AZ
1503 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1504 if (current_rtc_freq != RTC_FREQ &&
1505 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
fd872598
FB
1506 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1507 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1508 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1509 goto fail;
1510 }
1511 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1512 fail:
1513 close(rtc_fd);
1514 return -1;
1515 }
c8994013
TS
1516
1517 enable_sigio_timer(rtc_fd);
1518
fcdc2129 1519 t->priv = (void *)(long)rtc_fd;
c8994013 1520
fd872598
FB
1521 return 0;
1522}
1523
c8994013 1524static void rtc_stop_timer(struct qemu_alarm_timer *t)
829309c7 1525{
fcdc2129 1526 int rtc_fd = (long)t->priv;
c8994013
TS
1527
1528 close(rtc_fd);
829309c7
FB
1529}
1530
efe75411
TS
1531static int dynticks_start_timer(struct qemu_alarm_timer *t)
1532{
1533 struct sigevent ev;
1534 timer_t host_timer;
1535 struct sigaction act;
1536
1537 sigfillset(&act.sa_mask);
1538 act.sa_flags = 0;
efe75411
TS
1539 act.sa_handler = host_alarm_handler;
1540
1541 sigaction(SIGALRM, &act, NULL);
1542
1543 ev.sigev_value.sival_int = 0;
1544 ev.sigev_notify = SIGEV_SIGNAL;
1545 ev.sigev_signo = SIGALRM;
1546
1547 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1548 perror("timer_create");
1549
1550 /* disable dynticks */
1551 fprintf(stderr, "Dynamic Ticks disabled\n");
1552
1553 return -1;
1554 }
1555
0399bfe0 1556 t->priv = (void *)(long)host_timer;
efe75411
TS
1557
1558 return 0;
1559}
1560
1561static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1562{
0399bfe0 1563 timer_t host_timer = (timer_t)(long)t->priv;
efe75411
TS
1564
1565 timer_delete(host_timer);
1566}
1567
1568static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1569{
0399bfe0 1570 timer_t host_timer = (timer_t)(long)t->priv;
efe75411
TS
1571 struct itimerspec timeout;
1572 int64_t nearest_delta_us = INT64_MAX;
1573 int64_t current_us;
1574
1575 if (!active_timers[QEMU_TIMER_REALTIME] &&
1576 !active_timers[QEMU_TIMER_VIRTUAL])
d5d08334 1577 return;
efe75411 1578
2e70f6ef 1579 nearest_delta_us = qemu_next_deadline_dyntick();
efe75411
TS
1580
1581 /* check whether a timer is already running */
1582 if (timer_gettime(host_timer, &timeout)) {
1583 perror("gettime");
1584 fprintf(stderr, "Internal timer error: aborting\n");
1585 exit(1);
1586 }
1587 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1588 if (current_us && current_us <= nearest_delta_us)
1589 return;
1590
1591 timeout.it_interval.tv_sec = 0;
1592 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1593 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1594 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1595 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1596 perror("settime");
1597 fprintf(stderr, "Internal timer error: aborting\n");
1598 exit(1);
1599 }
1600}
1601
70744b3a 1602#endif /* defined(__linux__) */
231c6586 1603
c8994013
TS
1604static int unix_start_timer(struct qemu_alarm_timer *t)
1605{
1606 struct sigaction act;
1607 struct itimerval itv;
1608 int err;
1609
1610 /* timer signal */
1611 sigfillset(&act.sa_mask);
1612 act.sa_flags = 0;
c8994013
TS
1613 act.sa_handler = host_alarm_handler;
1614
1615 sigaction(SIGALRM, &act, NULL);
1616
1617 itv.it_interval.tv_sec = 0;
1618 /* for i386 kernel 2.6 to get 1 ms */
1619 itv.it_interval.tv_usec = 999;
1620 itv.it_value.tv_sec = 0;
1621 itv.it_value.tv_usec = 10 * 1000;
1622
1623 err = setitimer(ITIMER_REAL, &itv, NULL);
1624 if (err)
1625 return -1;
1626
1627 return 0;
1628}
1629
1630static void unix_stop_timer(struct qemu_alarm_timer *t)
1631{
1632 struct itimerval itv;
1633
1634 memset(&itv, 0, sizeof(itv));
1635 setitimer(ITIMER_REAL, &itv, NULL);
1636}
1637
829309c7 1638#endif /* !defined(_WIN32) */
fd872598 1639
f49e58dc
AL
1640static void try_to_rearm_timer(void *opaque)
1641{
1642 struct qemu_alarm_timer *t = opaque;
1643#ifndef _WIN32
1644 ssize_t len;
1645
1646 /* Drain the notify pipe */
1647 do {
1648 char buffer[512];
1649 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1650 } while ((len == -1 && errno == EINTR) || len > 0);
1651#endif
1652
f49e58dc
AL
1653 if (t->flags & ALARM_FLAG_EXPIRED) {
1654 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1655 qemu_rearm_alarm_timer(alarm_timer);
1656 }
1657}
1658
c8994013
TS
1659#ifdef _WIN32
1660
1661static int win32_start_timer(struct qemu_alarm_timer *t)
1662{
1663 TIMECAPS tc;
1664 struct qemu_alarm_win32 *data = t->priv;
efe75411 1665 UINT flags;
c8994013
TS
1666
1667 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1668 if (!data->host_alarm) {
1669 perror("Failed CreateEvent");
c396a7f0 1670 return -1;
c8994013
TS
1671 }
1672
1673 memset(&tc, 0, sizeof(tc));
1674 timeGetDevCaps(&tc, sizeof(tc));
1675
1676 if (data->period < tc.wPeriodMin)
1677 data->period = tc.wPeriodMin;
1678
1679 timeBeginPeriod(data->period);
1680
efe75411
TS
1681 flags = TIME_CALLBACK_FUNCTION;
1682 if (alarm_has_dynticks(t))
1683 flags |= TIME_ONESHOT;
1684 else
1685 flags |= TIME_PERIODIC;
1686
c8994013
TS
1687 data->timerId = timeSetEvent(1, // interval (ms)
1688 data->period, // resolution
1689 host_alarm_handler, // function
1690 (DWORD)t, // parameter
efe75411 1691 flags);
c8994013
TS
1692
1693 if (!data->timerId) {
1694 perror("Failed to initialize win32 alarm timer");
1695
1696 timeEndPeriod(data->period);
1697 CloseHandle(data->host_alarm);
1698 return -1;
1699 }
1700
f49e58dc 1701 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
c8994013
TS
1702
1703 return 0;
1704}
1705
1706static void win32_stop_timer(struct qemu_alarm_timer *t)
1707{
1708 struct qemu_alarm_win32 *data = t->priv;
1709
1710 timeKillEvent(data->timerId);
1711 timeEndPeriod(data->period);
1712
1713 CloseHandle(data->host_alarm);
1714}
1715
efe75411
TS
1716static void win32_rearm_timer(struct qemu_alarm_timer *t)
1717{
1718 struct qemu_alarm_win32 *data = t->priv;
1719 uint64_t nearest_delta_us;
1720
1721 if (!active_timers[QEMU_TIMER_REALTIME] &&
1722 !active_timers[QEMU_TIMER_VIRTUAL])
d5d08334 1723 return;
efe75411 1724
2e70f6ef 1725 nearest_delta_us = qemu_next_deadline_dyntick();
efe75411
TS
1726 nearest_delta_us /= 1000;
1727
1728 timeKillEvent(data->timerId);
1729
1730 data->timerId = timeSetEvent(1,
1731 data->period,
1732 host_alarm_handler,
1733 (DWORD)t,
1734 TIME_ONESHOT | TIME_PERIODIC);
1735
1736 if (!data->timerId) {
1737 perror("Failed to re-arm win32 alarm timer");
1738
1739 timeEndPeriod(data->period);
1740 CloseHandle(data->host_alarm);
1741 exit(1);
1742 }
1743}
1744
c8994013
TS
1745#endif /* _WIN32 */
1746
7183b4b4 1747static int init_timer_alarm(void)
8a7ddc38 1748{
223f0d72 1749 struct qemu_alarm_timer *t = NULL;
c8994013 1750 int i, err = -1;
f49e58dc
AL
1751
1752#ifndef _WIN32
c96f1a48
AL
1753 int fds[2];
1754
7183b4b4
AL
1755 err = pipe(fds);
1756 if (err == -1)
1757 return -errno;
1758
1759 err = fcntl_setfl(fds[0], O_NONBLOCK);
1760 if (err < 0)
1761 goto fail;
1762
1763 err = fcntl_setfl(fds[1], O_NONBLOCK);
1764 if (err < 0)
1765 goto fail;
1766
c96f1a48
AL
1767 alarm_timer_rfd = fds[0];
1768 alarm_timer_wfd = fds[1];
f49e58dc 1769#endif
c8994013
TS
1770
1771 for (i = 0; alarm_timers[i].name; i++) {
1772 t = &alarm_timers[i];
1773
c8994013
TS
1774 err = t->start(t);
1775 if (!err)
1776 break;
67b915a5 1777 }
fd872598 1778
c8994013 1779 if (err) {
7183b4b4
AL
1780 err = -ENOENT;
1781 goto fail;
67b915a5 1782 }
c8994013 1783
f49e58dc 1784#ifndef _WIN32
6abfbd79
AL
1785 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1786 try_to_rearm_timer, NULL, t);
f49e58dc 1787#endif
6abfbd79 1788
c8994013 1789 alarm_timer = t;
7183b4b4 1790
6abfbd79 1791 return 0;
7183b4b4
AL
1792
1793fail:
f49e58dc 1794#ifndef _WIN32
7183b4b4
AL
1795 close(fds[0]);
1796 close(fds[1]);
f49e58dc 1797#endif
7183b4b4 1798 return err;
8a7ddc38
FB
1799}
1800
9596ebb7 1801static void quit_timers(void)
40c3bac3 1802{
c8994013
TS
1803 alarm_timer->stop(alarm_timer);
1804 alarm_timer = NULL;
40c3bac3
FB
1805}
1806
f6503059
AZ
1807/***********************************************************/
1808/* host time/date access */
1809void qemu_get_timedate(struct tm *tm, int offset)
1810{
1811 time_t ti;
1812 struct tm *ret;
1813
1814 time(&ti);
1815 ti += offset;
1816 if (rtc_date_offset == -1) {
1817 if (rtc_utc)
1818 ret = gmtime(&ti);
1819 else
1820 ret = localtime(&ti);
1821 } else {
1822 ti -= rtc_date_offset;
1823 ret = gmtime(&ti);
1824 }
1825
1826 memcpy(tm, ret, sizeof(struct tm));
1827}
1828
1829int qemu_timedate_diff(struct tm *tm)
1830{
1831 time_t seconds;
1832
1833 if (rtc_date_offset == -1)
1834 if (rtc_utc)
1835 seconds = mktimegm(tm);
1836 else
1837 seconds = mktime(tm);
1838 else
1839 seconds = mktimegm(tm) + rtc_date_offset;
1840
1841 return seconds - time(NULL);
1842}
1843
fd1dff4b 1844#ifdef _WIN32
fd1dff4b
FB
1845static void socket_cleanup(void)
1846{
1847 WSACleanup();
1848}
82c643ff 1849
fd1dff4b
FB
1850static int socket_init(void)
1851{
1852 WSADATA Data;
1853 int ret, err;
1854
1855 ret = WSAStartup(MAKEWORD(2,2), &Data);
1856 if (ret != 0) {
1857 err = WSAGetLastError();
1858 fprintf(stderr, "WSAStartup: %d\n", err);
1859 return -1;
1860 }
1861 atexit(socket_cleanup);
1862 return 0;
1863}
64b7b733
AJ
1864#endif
1865
63a01ef8 1866const char *get_opt_name(char *buf, int buf_size, const char *p)
609497ab
AZ
1867{
1868 char *q;
1869
1870 q = buf;
1871 while (*p != '\0' && *p != '=') {
1872 if (q && (q - buf) < buf_size - 1)
1873 *q++ = *p;
1874 p++;
1875 }
1876 if (q)
1877 *q = '\0';
1878
1879 return p;
1880}
1881
63a01ef8 1882const char *get_opt_value(char *buf, int buf_size, const char *p)
e4bcb14c
TS
1883{
1884 char *q;
e4bcb14c 1885
e4bcb14c
TS
1886 q = buf;
1887 while (*p != '\0') {
609497ab
AZ
1888 if (*p == ',') {
1889 if (*(p + 1) != ',')
e4bcb14c 1890 break;
e4bcb14c 1891 p++;
609497ab 1892 }
e4bcb14c
TS
1893 if (q && (q - buf) < buf_size - 1)
1894 *q++ = *p;
1895 p++;
1896 }
1897 if (q)
1898 *q = '\0';
1899
1900 return p;
1901}
1902
63a01ef8
AL
1903int get_param_value(char *buf, int buf_size,
1904 const char *tag, const char *str)
7c9d8e07
FB
1905{
1906 const char *p;
7c9d8e07
FB
1907 char option[128];
1908
1909 p = str;
1910 for(;;) {
609497ab 1911 p = get_opt_name(option, sizeof(option), p);
7c9d8e07
FB
1912 if (*p != '=')
1913 break;
1914 p++;
1915 if (!strcmp(tag, option)) {
609497ab 1916 (void)get_opt_value(buf, buf_size, p);
e4bcb14c 1917 return strlen(buf);
7c9d8e07 1918 } else {
609497ab 1919 p = get_opt_value(NULL, 0, p);
7c9d8e07
FB
1920 }
1921 if (*p != ',')
1922 break;
1923 p++;
1924 }
1925 return 0;
1926}
1927
63a01ef8
AL
1928int check_params(char *buf, int buf_size,
1929 const char * const *params, const char *str)
e4bcb14c
TS
1930{
1931 const char *p;
1932 int i;
1933
1934 p = str;
1935 for(;;) {
609497ab 1936 p = get_opt_name(buf, buf_size, p);
e4bcb14c
TS
1937 if (*p != '=')
1938 return -1;
1939 p++;
1940 for(i = 0; params[i] != NULL; i++)
1941 if (!strcmp(params[i], buf))
1942 break;
1943 if (params[i] == NULL)
1944 return -1;
609497ab 1945 p = get_opt_value(NULL, 0, p);
e4bcb14c
TS
1946 if (*p != ',')
1947 break;
1948 p++;
1949 }
1950 return 0;
1951}
1952
1ae26a18
AZ
1953/***********************************************************/
1954/* Bluetooth support */
1955static int nb_hcis;
1956static int cur_hci;
1957static struct HCIInfo *hci_table[MAX_NICS];
dc72ac14 1958
1ae26a18
AZ
1959static struct bt_vlan_s {
1960 struct bt_scatternet_s net;
1961 int id;
1962 struct bt_vlan_s *next;
1963} *first_bt_vlan;
1964
1965/* find or alloc a new bluetooth "VLAN" */
674bb261 1966static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1ae26a18
AZ
1967{
1968 struct bt_vlan_s **pvlan, *vlan;
1969 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1970 if (vlan->id == id)
1971 return &vlan->net;
1972 }
1973 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1974 vlan->id = id;
1975 pvlan = &first_bt_vlan;
1976 while (*pvlan != NULL)
1977 pvlan = &(*pvlan)->next;
1978 *pvlan = vlan;
1979 return &vlan->net;
1980}
1981
1982static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1983{
1984}
1985
1986static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1987{
1988 return -ENOTSUP;
1989}
1990
1991static struct HCIInfo null_hci = {
1992 .cmd_send = null_hci_send,
1993 .sco_send = null_hci_send,
1994 .acl_send = null_hci_send,
1995 .bdaddr_set = null_hci_addr_set,
1996};
1997
1998struct HCIInfo *qemu_next_hci(void)
1999{
2000 if (cur_hci == nb_hcis)
2001 return &null_hci;
2002
2003 return hci_table[cur_hci++];
2004}
2005
dc72ac14
AZ
2006static struct HCIInfo *hci_init(const char *str)
2007{
2008 char *endp;
2009 struct bt_scatternet_s *vlan = 0;
2010
2011 if (!strcmp(str, "null"))
2012 /* null */
2013 return &null_hci;
2014 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
2015 /* host[:hciN] */
2016 return bt_host_hci(str[4] ? str + 5 : "hci0");
2017 else if (!strncmp(str, "hci", 3)) {
2018 /* hci[,vlan=n] */
2019 if (str[3]) {
2020 if (!strncmp(str + 3, ",vlan=", 6)) {
2021 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2022 if (*endp)
2023 vlan = 0;
2024 }
2025 } else
2026 vlan = qemu_find_bt_vlan(0);
2027 if (vlan)
2028 return bt_new_hci(vlan);
2029 }
2030
2031 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2032
2033 return 0;
2034}
2035
2036static int bt_hci_parse(const char *str)
2037{
2038 struct HCIInfo *hci;
2039 bdaddr_t bdaddr;
2040
2041 if (nb_hcis >= MAX_NICS) {
2042 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2043 return -1;
2044 }
2045
2046 hci = hci_init(str);
2047 if (!hci)
2048 return -1;
2049
2050 bdaddr.b[0] = 0x52;
2051 bdaddr.b[1] = 0x54;
2052 bdaddr.b[2] = 0x00;
2053 bdaddr.b[3] = 0x12;
2054 bdaddr.b[4] = 0x34;
2055 bdaddr.b[5] = 0x56 + nb_hcis;
2056 hci->bdaddr_set(hci, bdaddr.b);
2057
2058 hci_table[nb_hcis++] = hci;
2059
2060 return 0;
2061}
2062
2063static void bt_vhci_add(int vlan_id)
2064{
2065 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2066
2067 if (!vlan->slave)
2068 fprintf(stderr, "qemu: warning: adding a VHCI to "
2069 "an empty scatternet %i\n", vlan_id);
2070
2071 bt_vhci_init(bt_new_hci(vlan));
2072}
2073
2074static struct bt_device_s *bt_device_add(const char *opt)
2075{
2076 struct bt_scatternet_s *vlan;
2077 int vlan_id = 0;
2078 char *endp = strstr(opt, ",vlan=");
2079 int len = (endp ? endp - opt : strlen(opt)) + 1;
2080 char devname[10];
2081
2082 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2083
2084 if (endp) {
2085 vlan_id = strtol(endp + 6, &endp, 0);
2086 if (*endp) {
2087 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2088 return 0;
2089 }
2090 }
2091
2092 vlan = qemu_find_bt_vlan(vlan_id);
2093
2094 if (!vlan->slave)
2095 fprintf(stderr, "qemu: warning: adding a slave device to "
2096 "an empty scatternet %i\n", vlan_id);
2097
2098 if (!strcmp(devname, "keyboard"))
2099 return bt_keyboard_init(vlan);
2100
2101 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2102 return 0;
2103}
2104
2105static int bt_parse(const char *opt)
2106{
2107 const char *endp, *p;
2108 int vlan;
2109
2110 if (strstart(opt, "hci", &endp)) {
2111 if (!*endp || *endp == ',') {
2112 if (*endp)
2113 if (!strstart(endp, ",vlan=", 0))
2114 opt = endp + 1;
2115
2116 return bt_hci_parse(opt);
2117 }
2118 } else if (strstart(opt, "vhci", &endp)) {
2119 if (!*endp || *endp == ',') {
2120 if (*endp) {
2121 if (strstart(endp, ",vlan=", &p)) {
2122 vlan = strtol(p, (char **) &endp, 0);
2123 if (*endp) {
2124 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2125 return 1;
2126 }
2127 } else {
2128 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2129 return 1;
2130 }
2131 } else
2132 vlan = 0;
2133
2134 bt_vhci_add(vlan);
2135 return 0;
2136 }
2137 } else if (strstart(opt, "device:", &endp))
2138 return !bt_device_add(endp);
2139
2140 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2141 return 1;
2142}
2143
1ae26a18
AZ
2144/***********************************************************/
2145/* QEMU Block devices */
2146
609497ab 2147#define HD_ALIAS "index=%d,media=disk"
e4bcb14c
TS
2148#ifdef TARGET_PPC
2149#define CDROM_ALIAS "index=1,media=cdrom"
2150#else
2151#define CDROM_ALIAS "index=2,media=cdrom"
2152#endif
2153#define FD_ALIAS "index=%d,if=floppy"
609497ab
AZ
2154#define PFLASH_ALIAS "if=pflash"
2155#define MTD_ALIAS "if=mtd"
9d413d1d 2156#define SD_ALIAS "index=0,if=sd"
e4bcb14c 2157
609497ab 2158static int drive_add(const char *file, const char *fmt, ...)
e4bcb14c
TS
2159{
2160 va_list ap;
2161
2162 if (nb_drives_opt >= MAX_DRIVES) {
2163 fprintf(stderr, "qemu: too many drives\n");
2164 exit(1);
2165 }
2166
609497ab 2167 drives_opt[nb_drives_opt].file = file;
e4bcb14c 2168 va_start(ap, fmt);
609497ab
AZ
2169 vsnprintf(drives_opt[nb_drives_opt].opt,
2170 sizeof(drives_opt[0].opt), fmt, ap);
e4bcb14c
TS
2171 va_end(ap);
2172
2173 return nb_drives_opt++;
2174}
2175
f60d39bc 2176int drive_get_index(BlockInterfaceType type, int bus, int unit)
e4bcb14c
TS
2177{
2178 int index;
2179
2180 /* seek interface, bus and unit */
2181
2182 for (index = 0; index < nb_drives; index++)
f60d39bc 2183 if (drives_table[index].type == type &&
e4bcb14c
TS
2184 drives_table[index].bus == bus &&
2185 drives_table[index].unit == unit)
2186 return index;
2187
2188 return -1;
2189}
2190
f60d39bc 2191int drive_get_max_bus(BlockInterfaceType type)
e4bcb14c
TS
2192{
2193 int max_bus;
2194 int index;
2195
2196 max_bus = -1;
2197 for (index = 0; index < nb_drives; index++) {
f60d39bc 2198 if(drives_table[index].type == type &&
e4bcb14c
TS
2199 drives_table[index].bus > max_bus)
2200 max_bus = drives_table[index].bus;
2201 }
2202 return max_bus;
2203}
2204
a1620fac
AJ
2205static void bdrv_format_print(void *opaque, const char *name)
2206{
2207 fprintf(stderr, " %s", name);
2208}
2209
609497ab
AZ
2210static int drive_init(struct drive_opt *arg, int snapshot,
2211 QEMUMachine *machine)
e4bcb14c
TS
2212{
2213 char buf[128];
2214 char file[1024];
c8522bdf
AZ
2215 char devname[128];
2216 const char *mediastr = "";
f60d39bc 2217 BlockInterfaceType type;
e4bcb14c
TS
2218 enum { MEDIA_DISK, MEDIA_CDROM } media;
2219 int bus_id, unit_id;
2220 int cyls, heads, secs, translation;
2221 BlockDriverState *bdrv;
1e72d3b7 2222 BlockDriver *drv = NULL;
e4bcb14c
TS
2223 int max_devs;
2224 int index;
33f00271
AZ
2225 int cache;
2226 int bdrv_flags;
609497ab 2227 char *str = arg->opt;
7ccfb2eb
BS
2228 static const char * const params[] = { "bus", "unit", "if", "index",
2229 "cyls", "heads", "secs", "trans",
2230 "media", "snapshot", "file",
2231 "cache", "format", NULL };
e4bcb14c
TS
2232
2233 if (check_params(buf, sizeof(buf), params, str) < 0) {
ff993638 2234 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
e4bcb14c
TS
2235 buf, str);
2236 return -1;
2237 }
2238
2239 file[0] = 0;
2240 cyls = heads = secs = 0;
2241 bus_id = 0;
2242 unit_id = -1;
2243 translation = BIOS_ATA_TRANSLATION_AUTO;
2244 index = -1;
33f00271 2245 cache = 1;
e4bcb14c 2246
c9b1ae2c 2247 if (machine->use_scsi) {
f60d39bc 2248 type = IF_SCSI;
e4bcb14c 2249 max_devs = MAX_SCSI_DEVS;
363a37d5 2250 pstrcpy(devname, sizeof(devname), "scsi");
e4bcb14c 2251 } else {
f60d39bc 2252 type = IF_IDE;
e4bcb14c 2253 max_devs = MAX_IDE_DEVS;
363a37d5 2254 pstrcpy(devname, sizeof(devname), "ide");
e4bcb14c
TS
2255 }
2256 media = MEDIA_DISK;
2257
2258 /* extract parameters */
2259
2260 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2261 bus_id = strtol(buf, NULL, 0);
2262 if (bus_id < 0) {
2263 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2264 return -1;
2265 }
2266 }
2267
2268 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2269 unit_id = strtol(buf, NULL, 0);
2270 if (unit_id < 0) {
2271 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2272 return -1;
2273 }
2274 }
2275
2276 if (get_param_value(buf, sizeof(buf), "if", str)) {
ae45d369 2277 pstrcpy(devname, sizeof(devname), buf);
e4bcb14c 2278 if (!strcmp(buf, "ide")) {
f60d39bc 2279 type = IF_IDE;
e4bcb14c
TS
2280 max_devs = MAX_IDE_DEVS;
2281 } else if (!strcmp(buf, "scsi")) {
f60d39bc 2282 type = IF_SCSI;
e4bcb14c
TS
2283 max_devs = MAX_SCSI_DEVS;
2284 } else if (!strcmp(buf, "floppy")) {
f60d39bc 2285 type = IF_FLOPPY;
e4bcb14c
TS
2286 max_devs = 0;
2287 } else if (!strcmp(buf, "pflash")) {
f60d39bc 2288 type = IF_PFLASH;
e4bcb14c
TS
2289 max_devs = 0;
2290 } else if (!strcmp(buf, "mtd")) {
f60d39bc 2291 type = IF_MTD;
e4bcb14c
TS
2292 max_devs = 0;
2293 } else if (!strcmp(buf, "sd")) {
f60d39bc 2294 type = IF_SD;
e4bcb14c 2295 max_devs = 0;
6e02c38d
AL
2296 } else if (!strcmp(buf, "virtio")) {
2297 type = IF_VIRTIO;
2298 max_devs = 0;
2299 } else {
e4bcb14c
TS
2300 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2301 return -1;
2302 }
2303 }
2304
2305 if (get_param_value(buf, sizeof(buf), "index", str)) {
2306 index = strtol(buf, NULL, 0);
2307 if (index < 0) {
2308 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2309 return -1;
2310 }
2311 }
2312
2313 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2314 cyls = strtol(buf, NULL, 0);
2315 }
2316
2317 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2318 heads = strtol(buf, NULL, 0);
2319 }
2320
2321 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2322 secs = strtol(buf, NULL, 0);
2323 }
2324
2325 if (cyls || heads || secs) {
2326 if (cyls < 1 || cyls > 16383) {
2327 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2328 return -1;
2329 }
2330 if (heads < 1 || heads > 16) {
2331 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2332 return -1;
2333 }
2334 if (secs < 1 || secs > 63) {
2335 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2336 return -1;
2337 }
2338 }
2339
2340 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2341 if (!cyls) {
2342 fprintf(stderr,
2343 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2344 str);
2345 return -1;
2346 }
2347 if (!strcmp(buf, "none"))
2348 translation = BIOS_ATA_TRANSLATION_NONE;
2349 else if (!strcmp(buf, "lba"))
2350 translation = BIOS_ATA_TRANSLATION_LBA;
2351 else if (!strcmp(buf, "auto"))
2352 translation = BIOS_ATA_TRANSLATION_AUTO;
2353 else {
2354 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2355 return -1;
2356 }
2357 }
2358
2359 if (get_param_value(buf, sizeof(buf), "media", str)) {
2360 if (!strcmp(buf, "disk")) {
2361 media = MEDIA_DISK;
2362 } else if (!strcmp(buf, "cdrom")) {
2363 if (cyls || secs || heads) {
2364 fprintf(stderr,
2365 "qemu: '%s' invalid physical CHS format\n", str);
2366 return -1;
2367 }
2368 media = MEDIA_CDROM;
2369 } else {
2370 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2371 return -1;
2372 }
2373 }
2374
2375 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2376 if (!strcmp(buf, "on"))
2377 snapshot = 1;
2378 else if (!strcmp(buf, "off"))
2379 snapshot = 0;
2380 else {
2381 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2382 return -1;
2383 }
2384 }
2385
33f00271 2386 if (get_param_value(buf, sizeof(buf), "cache", str)) {
9f7965c7 2387 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
33f00271 2388 cache = 0;
9f7965c7 2389 else if (!strcmp(buf, "writethrough"))
33f00271 2390 cache = 1;
9f7965c7
AL
2391 else if (!strcmp(buf, "writeback"))
2392 cache = 2;
33f00271
AZ
2393 else {
2394 fprintf(stderr, "qemu: invalid cache option\n");
2395 return -1;
2396 }
2397 }
2398
1e72d3b7 2399 if (get_param_value(buf, sizeof(buf), "format", str)) {
a1620fac
AJ
2400 if (strcmp(buf, "?") == 0) {
2401 fprintf(stderr, "qemu: Supported formats:");
2402 bdrv_iterate_format(bdrv_format_print, NULL);
2403 fprintf(stderr, "\n");
2404 return -1;
2405 }
1e72d3b7
AJ
2406 drv = bdrv_find_format(buf);
2407 if (!drv) {
2408 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2409 return -1;
2410 }
2411 }
2412
609497ab
AZ
2413 if (arg->file == NULL)
2414 get_param_value(file, sizeof(file), "file", str);
2415 else
2416 pstrcpy(file, sizeof(file), arg->file);
e4bcb14c
TS
2417
2418 /* compute bus and unit according index */
2419
2420 if (index != -1) {
2421 if (bus_id != 0 || unit_id != -1) {
2422 fprintf(stderr,
2423 "qemu: '%s' index cannot be used with bus and unit\n", str);
2424 return -1;
2425 }
2426 if (max_devs == 0)
2427 {
2428 unit_id = index;
2429 bus_id = 0;
2430 } else {
2431 unit_id = index % max_devs;
2432 bus_id = index / max_devs;
2433 }
2434 }
2435
2436 /* if user doesn't specify a unit_id,
2437 * try to find the first free
2438 */
2439
2440 if (unit_id == -1) {
2441 unit_id = 0;
f60d39bc 2442 while (drive_get_index(type, bus_id, unit_id) != -1) {
e4bcb14c
TS
2443 unit_id++;
2444 if (max_devs && unit_id >= max_devs) {
2445 unit_id -= max_devs;
2446 bus_id++;
2447 }
2448 }
2449 }
2450
2451 /* check unit id */
2452
2453 if (max_devs && unit_id >= max_devs) {
2454 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2455 str, unit_id, max_devs - 1);
2456 return -1;
2457 }
2458
2459 /*
2460 * ignore multiple definitions
2461 */
2462
f60d39bc 2463 if (drive_get_index(type, bus_id, unit_id) != -1)
e4bcb14c
TS
2464 return 0;
2465
2466 /* init */
2467
f60d39bc 2468 if (type == IF_IDE || type == IF_SCSI)
c8522bdf 2469 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
e6198a70
AZ
2470 if (max_devs)
2471 snprintf(buf, sizeof(buf), "%s%i%s%i",
2472 devname, bus_id, mediastr, unit_id);
2473 else
2474 snprintf(buf, sizeof(buf), "%s%s%i",
2475 devname, mediastr, unit_id);
e4bcb14c
TS
2476 bdrv = bdrv_new(buf);
2477 drives_table[nb_drives].bdrv = bdrv;
f60d39bc 2478 drives_table[nb_drives].type = type;
e4bcb14c
TS
2479 drives_table[nb_drives].bus = bus_id;
2480 drives_table[nb_drives].unit = unit_id;
2481 nb_drives++;
2482
f60d39bc 2483 switch(type) {
e4bcb14c
TS
2484 case IF_IDE:
2485 case IF_SCSI:
2486 switch(media) {
2487 case MEDIA_DISK:
2488 if (cyls != 0) {
2489 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2490 bdrv_set_translation_hint(bdrv, translation);
2491 }
2492 break;
2493 case MEDIA_CDROM:
2494 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2495 break;
2496 }
2497 break;
2498 case IF_SD:
2499 /* FIXME: This isn't really a floppy, but it's a reasonable
2500 approximation. */
2501 case IF_FLOPPY:
2502 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2503 break;
2504 case IF_PFLASH:
2505 case IF_MTD:
6e02c38d 2506 case IF_VIRTIO:
e4bcb14c
TS
2507 break;
2508 }
2509 if (!file[0])
2510 return 0;
33f00271 2511 bdrv_flags = 0;
9f7965c7 2512 if (snapshot) {
33f00271 2513 bdrv_flags |= BDRV_O_SNAPSHOT;
9f7965c7
AL
2514 cache = 2; /* always use write-back with snapshot */
2515 }
2516 if (cache == 0) /* no caching */
2517 bdrv_flags |= BDRV_O_NOCACHE;
2518 else if (cache == 2) /* write-back */
2519 bdrv_flags |= BDRV_O_CACHE_WB;
83ab7950 2520 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
e4bcb14c
TS
2521 fprintf(stderr, "qemu: could not open disk image %s\n",
2522 file);
2523 return -1;
2524 }
2525 return 0;
2526}
2527
a594cfbf
FB
2528/***********************************************************/
2529/* USB devices */
2530
0d92ed30
PB
2531static USBPort *used_usb_ports;
2532static USBPort *free_usb_ports;
2533
2534/* ??? Maybe change this to register a hub to keep track of the topology. */
2535void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2536 usb_attachfn attach)
2537{
2538 port->opaque = opaque;
2539 port->index = index;
2540 port->attach = attach;
2541 port->next = free_usb_ports;
2542 free_usb_ports = port;
2543}
2544
4b096fc9
AL
2545int usb_device_add_dev(USBDevice *dev)
2546{
2547 USBPort *port;
2548
2549 /* Find a USB port to add the device to. */
2550 port = free_usb_ports;
2551 if (!port->next) {
2552 USBDevice *hub;
2553
2554 /* Create a new hub and chain it on. */
2555 free_usb_ports = NULL;
2556 port->next = used_usb_ports;
2557 used_usb_ports = port;
2558
2559 hub = usb_hub_init(VM_USB_HUB_SIZE);
2560 usb_attach(port, hub);
2561 port = free_usb_ports;
2562 }
2563
2564 free_usb_ports = port->next;
2565 port->next = used_usb_ports;
2566 used_usb_ports = port;
2567 usb_attach(port, dev);
2568 return 0;
2569}
2570
a594cfbf
FB
2571static int usb_device_add(const char *devname)
2572{
2573 const char *p;
2574 USBDevice *dev;
a594cfbf 2575
0d92ed30 2576 if (!free_usb_ports)
a594cfbf
FB
2577 return -1;
2578
2579 if (strstart(devname, "host:", &p)) {
2580 dev = usb_host_device_open(p);
a594cfbf
FB
2581 } else if (!strcmp(devname, "mouse")) {
2582 dev = usb_mouse_init();
09b26c5e 2583 } else if (!strcmp(devname, "tablet")) {
47b2d338
AZ
2584 dev = usb_tablet_init();
2585 } else if (!strcmp(devname, "keyboard")) {
2586 dev = usb_keyboard_init();
2e5d83bb
PB
2587 } else if (strstart(devname, "disk:", &p)) {
2588 dev = usb_msd_init(p);
f6d2a316
AZ
2589 } else if (!strcmp(devname, "wacom-tablet")) {
2590 dev = usb_wacom_init();
a7954218
AZ
2591 } else if (strstart(devname, "serial:", &p)) {
2592 dev = usb_serial_init(p);
2e4d9fb1
AJ
2593#ifdef CONFIG_BRLAPI
2594 } else if (!strcmp(devname, "braille")) {
2595 dev = usb_baum_init();
2596#endif
6c9f886c 2597 } else if (strstart(devname, "net:", &p)) {
9ad97e65 2598 int nic = nb_nics;
6c9f886c 2599
9ad97e65 2600 if (net_client_init("nic", p) < 0)
6c9f886c 2601 return -1;
9ad97e65
AZ
2602 nd_table[nic].model = "usb";
2603 dev = usb_net_init(&nd_table[nic]);
dc72ac14
AZ
2604 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2605 dev = usb_bt_init(devname[2] ? hci_init(p) :
2606 bt_new_hci(qemu_find_bt_vlan(0)));
a594cfbf
FB
2607 } else {
2608 return -1;
2609 }
0d92ed30
PB
2610 if (!dev)
2611 return -1;
2612
4b096fc9 2613 return usb_device_add_dev(dev);
a594cfbf
FB
2614}
2615
1f3870ab 2616int usb_device_del_addr(int bus_num, int addr)
a594cfbf 2617{
0d92ed30
PB
2618 USBPort *port;
2619 USBPort **lastp;
059809e4 2620 USBDevice *dev;
a594cfbf 2621
0d92ed30 2622 if (!used_usb_ports)
a594cfbf
FB
2623 return -1;
2624
a594cfbf
FB
2625 if (bus_num != 0)
2626 return -1;
0d92ed30
PB
2627
2628 lastp = &used_usb_ports;
2629 port = used_usb_ports;
2630 while (port && port->dev->addr != addr) {
2631 lastp = &port->next;
2632 port = port->next;
a594cfbf 2633 }
0d92ed30
PB
2634
2635 if (!port)
a594cfbf 2636 return -1;
0d92ed30 2637
059809e4 2638 dev = port->dev;
0d92ed30
PB
2639 *lastp = port->next;
2640 usb_attach(port, NULL);
059809e4 2641 dev->handle_destroy(dev);
0d92ed30
PB
2642 port->next = free_usb_ports;
2643 free_usb_ports = port;
a594cfbf
FB
2644 return 0;
2645}
2646
1f3870ab
AL
2647static int usb_device_del(const char *devname)
2648{
2649 int bus_num, addr;
2650 const char *p;
2651
5d0c5750
AL
2652 if (strstart(devname, "host:", &p))
2653 return usb_host_device_close(p);
2654
1f3870ab
AL
2655 if (!used_usb_ports)
2656 return -1;
2657
2658 p = strchr(devname, '.');
2659 if (!p)
2660 return -1;
2661 bus_num = strtoul(devname, NULL, 0);
2662 addr = strtoul(p + 1, NULL, 0);
2663
2664 return usb_device_del_addr(bus_num, addr);
2665}
2666
a594cfbf
FB
2667void do_usb_add(const char *devname)
2668{
4b096fc9 2669 usb_device_add(devname);
a594cfbf
FB
2670}
2671
2672void do_usb_del(const char *devname)
2673{
4b096fc9 2674 usb_device_del(devname);
a594cfbf
FB
2675}
2676
2677void usb_info(void)
2678{
2679 USBDevice *dev;
0d92ed30 2680 USBPort *port;
a594cfbf
FB
2681 const char *speed_str;
2682
0d92ed30 2683 if (!usb_enabled) {
a594cfbf
FB
2684 term_printf("USB support not enabled\n");
2685 return;
2686 }
2687
0d92ed30
PB
2688 for (port = used_usb_ports; port; port = port->next) {
2689 dev = port->dev;
2690 if (!dev)
2691 continue;
2692 switch(dev->speed) {
5fafdf24
TS
2693 case USB_SPEED_LOW:
2694 speed_str = "1.5";
0d92ed30 2695 break;
5fafdf24
TS
2696 case USB_SPEED_FULL:
2697 speed_str = "12";
0d92ed30 2698 break;
5fafdf24
TS
2699 case USB_SPEED_HIGH:
2700 speed_str = "480";
0d92ed30
PB
2701 break;
2702 default:
5fafdf24 2703 speed_str = "?";
0d92ed30 2704 break;
a594cfbf 2705 }
5fafdf24 2706 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
1f6e24e7 2707 0, dev->addr, speed_str, dev->devname);
a594cfbf
FB
2708 }
2709}
2710
201a51fc
AZ
2711/***********************************************************/
2712/* PCMCIA/Cardbus */
2713
2714static struct pcmcia_socket_entry_s {
2715 struct pcmcia_socket_s *socket;
2716 struct pcmcia_socket_entry_s *next;
2717} *pcmcia_sockets = 0;
2718
2719void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2720{
2721 struct pcmcia_socket_entry_s *entry;
2722
2723 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2724 entry->socket = socket;
2725 entry->next = pcmcia_sockets;
2726 pcmcia_sockets = entry;
2727}
2728
2729void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2730{
2731 struct pcmcia_socket_entry_s *entry, **ptr;
2732
2733 ptr = &pcmcia_sockets;
2734 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2735 if (entry->socket == socket) {
2736 *ptr = entry->next;
2737 qemu_free(entry);
2738 }
2739}
2740
2741void pcmcia_info(void)
2742{
2743 struct pcmcia_socket_entry_s *iter;
2744 if (!pcmcia_sockets)
2745 term_printf("No PCMCIA sockets\n");
2746
2747 for (iter = pcmcia_sockets; iter; iter = iter->next)
2748 term_printf("%s: %s\n", iter->socket->slot_string,
2749 iter->socket->attached ? iter->socket->card_string :
2750 "Empty");
2751}
2752
2ff89790
TS
2753/***********************************************************/
2754/* dumb display */
2755
2756static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
2757{
2758}
2759
2760static void dumb_resize(DisplayState *ds, int w, int h)
2761{
2762}
2763
2ff89790
TS
2764static void dumb_display_init(DisplayState *ds)
2765{
2766 ds->data = NULL;
2767 ds->linesize = 0;
2768 ds->depth = 0;
2769 ds->dpy_update = dumb_update;
2770 ds->dpy_resize = dumb_resize;
2ad1a437
AL
2771 ds->dpy_refresh = NULL;
2772 ds->gui_timer_interval = 0;
bcfad70f 2773 ds->idle = 1;
2ff89790
TS
2774}
2775
8a7ddc38
FB
2776/***********************************************************/
2777/* I/O handling */
0824d6fc 2778
c4b1fcc0
FB
2779#define MAX_IO_HANDLERS 64
2780
2781typedef struct IOHandlerRecord {
2782 int fd;
7c9d8e07
FB
2783 IOCanRWHandler *fd_read_poll;
2784 IOHandler *fd_read;
2785 IOHandler *fd_write;
cafffd40 2786 int deleted;
c4b1fcc0
FB
2787 void *opaque;
2788 /* temporary data */
2789 struct pollfd *ufd;
8a7ddc38 2790 struct IOHandlerRecord *next;
c4b1fcc0
FB
2791} IOHandlerRecord;
2792
8a7ddc38 2793static IOHandlerRecord *first_io_handler;
c4b1fcc0 2794
7c9d8e07
FB
2795/* XXX: fd_read_poll should be suppressed, but an API change is
2796 necessary in the character devices to suppress fd_can_read(). */
5fafdf24
TS
2797int qemu_set_fd_handler2(int fd,
2798 IOCanRWHandler *fd_read_poll,
2799 IOHandler *fd_read,
2800 IOHandler *fd_write,
7c9d8e07 2801 void *opaque)
c4b1fcc0 2802{
7c9d8e07 2803 IOHandlerRecord **pioh, *ioh;
c4b1fcc0 2804
7c9d8e07
FB
2805 if (!fd_read && !fd_write) {
2806 pioh = &first_io_handler;
2807 for(;;) {
2808 ioh = *pioh;
2809 if (ioh == NULL)
2810 break;
2811 if (ioh->fd == fd) {
cafffd40 2812 ioh->deleted = 1;
7c9d8e07
FB
2813 break;
2814 }
2815 pioh = &ioh->next;
2816 }
2817 } else {
2818 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2819 if (ioh->fd == fd)
2820 goto found;
2821 }
2822 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2823 if (!ioh)
2824 return -1;
2825 ioh->next = first_io_handler;
2826 first_io_handler = ioh;
2827 found:
2828 ioh->fd = fd;
2829 ioh->fd_read_poll = fd_read_poll;
2830 ioh->fd_read = fd_read;
2831 ioh->fd_write = fd_write;
2832 ioh->opaque = opaque;
cafffd40 2833 ioh->deleted = 0;
7c9d8e07 2834 }
c4b1fcc0
FB
2835 return 0;
2836}
2837
5fafdf24
TS
2838int qemu_set_fd_handler(int fd,
2839 IOHandler *fd_read,
2840 IOHandler *fd_write,
7c9d8e07 2841 void *opaque)
8a7ddc38 2842{
7c9d8e07 2843 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
8a7ddc38
FB
2844}
2845
56f3a5d0 2846#ifdef _WIN32
f331110f
FB
2847/***********************************************************/
2848/* Polling handling */
2849
2850typedef struct PollingEntry {
2851 PollingFunc *func;
2852 void *opaque;
2853 struct PollingEntry *next;
2854} PollingEntry;
2855
2856static PollingEntry *first_polling_entry;
2857
2858int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2859{
2860 PollingEntry **ppe, *pe;
2861 pe = qemu_mallocz(sizeof(PollingEntry));
2862 if (!pe)
2863 return -1;
2864 pe->func = func;
2865 pe->opaque = opaque;
2866 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2867 *ppe = pe;
2868 return 0;
2869}
2870
2871void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2872{
2873 PollingEntry **ppe, *pe;
2874 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2875 pe = *ppe;
2876 if (pe->func == func && pe->opaque == opaque) {
2877 *ppe = pe->next;
2878 qemu_free(pe);
2879 break;
2880 }
2881 }
2882}
2883
a18e524a
FB
2884/***********************************************************/
2885/* Wait objects support */
2886typedef struct WaitObjects {
2887 int num;
2888 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2889 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2890 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2891} WaitObjects;
2892
2893static WaitObjects wait_objects = {0};
3b46e624 2894
a18e524a
FB
2895int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2896{
2897 WaitObjects *w = &wait_objects;
2898
2899 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2900 return -1;
2901 w->events[w->num] = handle;
2902 w->func[w->num] = func;
2903 w->opaque[w->num] = opaque;
2904 w->num++;
2905 return 0;
2906}
2907
2908void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2909{
2910 int i, found;
2911 WaitObjects *w = &wait_objects;
2912
2913 found = 0;
2914 for (i = 0; i < w->num; i++) {
2915 if (w->events[i] == handle)
2916 found = 1;
2917 if (found) {
2918 w->events[i] = w->events[i + 1];
2919 w->func[i] = w->func[i + 1];
2920 w->opaque[i] = w->opaque[i + 1];
3b46e624 2921 }
a18e524a
FB
2922 }
2923 if (found)
2924 w->num--;
2925}
2926#endif
2927
8a7ddc38
FB
2928/***********************************************************/
2929/* ram save/restore */
2930
8a7ddc38
FB
2931static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2932{
2933 int v;
2934
2935 v = qemu_get_byte(f);
2936 switch(v) {
2937 case 0:
2938 if (qemu_get_buffer(f, buf, len) != len)
2939 return -EIO;
2940 break;
2941 case 1:
2942 v = qemu_get_byte(f);
2943 memset(buf, v, len);
2944 break;
2945 default:
2946 return -EINVAL;
2947 }
871d2f07
AL
2948
2949 if (qemu_file_has_error(f))
2950 return -EIO;
2951
8a7ddc38
FB
2952 return 0;
2953}
2954
c88676f8
FB
2955static int ram_load_v1(QEMUFile *f, void *opaque)
2956{
00f82b8a
AJ
2957 int ret;
2958 ram_addr_t i;
c88676f8
FB
2959
2960 if (qemu_get_be32(f) != phys_ram_size)
2961 return -EINVAL;
2962 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
2963 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
2964 if (ret)
2965 return ret;
2966 }
2967 return 0;
2968}
2969
2970#define BDRV_HASH_BLOCK_SIZE 1024
2971#define IOBUF_SIZE 4096
2972#define RAM_CBLOCK_MAGIC 0xfabe
2973
c88676f8
FB
2974typedef struct RamDecompressState {
2975 z_stream zstream;
2976 QEMUFile *f;
2977 uint8_t buf[IOBUF_SIZE];
2978} RamDecompressState;
2979
2980static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
2981{
2982 int ret;
2983 memset(s, 0, sizeof(*s));
2984 s->f = f;
2985 ret = inflateInit(&s->zstream);
2986 if (ret != Z_OK)
2987 return -1;
2988 return 0;
2989}
2990
2991static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
2992{
2993 int ret, clen;
2994
2995 s->zstream.avail_out = len;
2996 s->zstream.next_out = buf;
2997 while (s->zstream.avail_out > 0) {
2998 if (s->zstream.avail_in == 0) {
2999 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3000 return -1;
3001 clen = qemu_get_be16(s->f);
3002 if (clen > IOBUF_SIZE)
3003 return -1;
3004 qemu_get_buffer(s->f, s->buf, clen);
3005 s->zstream.avail_in = clen;
3006 s->zstream.next_in = s->buf;
3007 }
3008 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3009 if (ret != Z_OK && ret != Z_STREAM_END) {
3010 return -1;
3011 }
3012 }
3013 return 0;
3014}
3015
3016static void ram_decompress_close(RamDecompressState *s)
3017{
3018 inflateEnd(&s->zstream);
3019}
3020
475e4277
AL
3021#define RAM_SAVE_FLAG_FULL 0x01
3022#define RAM_SAVE_FLAG_COMPRESS 0x02
3023#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3024#define RAM_SAVE_FLAG_PAGE 0x08
3025#define RAM_SAVE_FLAG_EOS 0x10
3026
3027static int is_dup_page(uint8_t *page, uint8_t ch)
8a7ddc38 3028{
475e4277
AL
3029 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3030 uint32_t *array = (uint32_t *)page;
3031 int i;
3b46e624 3032
475e4277
AL
3033 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3034 if (array[i] != val)
3035 return 0;
3036 }
3037
3038 return 1;
3039}
3040
3041static int ram_save_block(QEMUFile *f)
3042{
3043 static ram_addr_t current_addr = 0;
3044 ram_addr_t saved_addr = current_addr;
3045 ram_addr_t addr = 0;
3046 int found = 0;
3047
3048 while (addr < phys_ram_size) {
3049 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3050 uint8_t ch;
3051
3052 cpu_physical_memory_reset_dirty(current_addr,
3053 current_addr + TARGET_PAGE_SIZE,
3054 MIGRATION_DIRTY_FLAG);
3055
3056 ch = *(phys_ram_base + current_addr);
3057
3058 if (is_dup_page(phys_ram_base + current_addr, ch)) {
3059 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3060 qemu_put_byte(f, ch);
3061 } else {
3062 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3063 qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
c88676f8 3064 }
475e4277
AL
3065
3066 found = 1;
3067 break;
c88676f8 3068 }
475e4277
AL
3069 addr += TARGET_PAGE_SIZE;
3070 current_addr = (saved_addr + addr) % phys_ram_size;
8a7ddc38 3071 }
475e4277
AL
3072
3073 return found;
8a7ddc38
FB
3074}
3075
475e4277
AL
3076static ram_addr_t ram_save_threshold = 10;
3077
3078static ram_addr_t ram_save_remaining(void)
3079{
3080 ram_addr_t addr;
3081 ram_addr_t count = 0;
3082
3083 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3084 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3085 count++;
3086 }
3087
3088 return count;
3089}
3090
3091static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3092{
3093 ram_addr_t addr;
3094
3095 if (stage == 1) {
3096 /* Make sure all dirty bits are set */
3097 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3098 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3099 cpu_physical_memory_set_dirty(addr);
3100 }
3101
3102 /* Enable dirty memory tracking */
3103 cpu_physical_memory_set_dirty_tracking(1);
3104
3105 qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
3106 }
3107
3108 while (!qemu_file_rate_limit(f)) {
3109 int ret;
3110
3111 ret = ram_save_block(f);
3112 if (ret == 0) /* no more blocks */
3113 break;
3114 }
3115
3116 /* try transferring iterative blocks of memory */
3117
3118 if (stage == 3) {
3119 cpu_physical_memory_set_dirty_tracking(0);
3120
3121 /* flush all remaining blocks regardless of rate limiting */
3122 while (ram_save_block(f) != 0);
3123 }
3124
3125 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3126
3127 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3128}
3129
3130static int ram_load_dead(QEMUFile *f, void *opaque)
8a7ddc38 3131{
c88676f8
FB
3132 RamDecompressState s1, *s = &s1;
3133 uint8_t buf[10];
00f82b8a 3134 ram_addr_t i;
8a7ddc38 3135
c88676f8
FB
3136 if (ram_decompress_open(s, f) < 0)
3137 return -EINVAL;
3138 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
3139 if (ram_decompress_buf(s, buf, 1) < 0) {
3140 fprintf(stderr, "Error while reading ram block header\n");
3141 goto error;
3142 }
3143 if (buf[0] == 0) {
3144 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
00f82b8a 3145 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
c88676f8
FB
3146 goto error;
3147 }
475e4277 3148 } else {
c88676f8
FB
3149 error:
3150 printf("Error block header\n");
3151 return -EINVAL;
3152 }
8a7ddc38 3153 }
c88676f8 3154 ram_decompress_close(s);
475e4277
AL
3155
3156 return 0;
3157}
3158
3159static int ram_load(QEMUFile *f, void *opaque, int version_id)
3160{
3161 ram_addr_t addr;
3162 int flags;
3163
3164 if (version_id == 1)
3165 return ram_load_v1(f, opaque);
3166
3167 if (version_id == 2) {
3168 if (qemu_get_be32(f) != phys_ram_size)
3169 return -EINVAL;
3170 return ram_load_dead(f, opaque);
3171 }
3172
3173 if (version_id != 3)
3174 return -EINVAL;
3175
3176 do {
3177 addr = qemu_get_be64(f);
3178
3179 flags = addr & ~TARGET_PAGE_MASK;
3180 addr &= TARGET_PAGE_MASK;
3181
3182 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3183 if (addr != phys_ram_size)
3184 return -EINVAL;
3185 }
3186
3187 if (flags & RAM_SAVE_FLAG_FULL) {
3188 if (ram_load_dead(f, opaque) < 0)
3189 return -EINVAL;
3190 }
3191
3192 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3193 uint8_t ch = qemu_get_byte(f);
3194 memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
3195 } else if (flags & RAM_SAVE_FLAG_PAGE)
3196 qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
3197 } while (!(flags & RAM_SAVE_FLAG_EOS));
3198
8a7ddc38
FB
3199 return 0;
3200}
3201
9e472e10
AL
3202void qemu_service_io(void)
3203{
3204 CPUState *env = cpu_single_env;
3205 if (env) {
3206 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3207#ifdef USE_KQEMU
3208 if (env->kqemu_enabled) {
3209 kqemu_cpu_interrupt(env);
3210 }
3211#endif
3212 }
3213}
3214
83f64091
FB
3215/***********************************************************/
3216/* bottom halves (can be seen as timers which expire ASAP) */
3217
3218struct QEMUBH {
3219 QEMUBHFunc *cb;
3220 void *opaque;
3221 int scheduled;
1b435b10
AL
3222 int idle;
3223 int deleted;
83f64091
FB
3224 QEMUBH *next;
3225};
3226
3227static QEMUBH *first_bh = NULL;
3228
3229QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3230{
3231 QEMUBH *bh;
3232 bh = qemu_mallocz(sizeof(QEMUBH));
3233 if (!bh)
3234 return NULL;
3235 bh->cb = cb;
3236 bh->opaque = opaque;
1b435b10
AL
3237 bh->next = first_bh;
3238 first_bh = bh;
83f64091
FB
3239 return bh;
3240}
3241
6eb5733a 3242int qemu_bh_poll(void)
83f64091 3243{
1b435b10 3244 QEMUBH *bh, **bhp;
6eb5733a 3245 int ret;
83f64091 3246
6eb5733a 3247 ret = 0;
1b435b10
AL
3248 for (bh = first_bh; bh; bh = bh->next) {
3249 if (!bh->deleted && bh->scheduled) {
3250 bh->scheduled = 0;
3251 if (!bh->idle)
3252 ret = 1;
3253 bh->idle = 0;
3254 bh->cb(bh->opaque);
3255 }
83f64091 3256 }
1b435b10
AL
3257
3258 /* remove deleted bhs */
3259 bhp = &first_bh;
3260 while (*bhp) {
3261 bh = *bhp;
3262 if (bh->deleted) {
3263 *bhp = bh->next;
3264 qemu_free(bh);
3265 } else
3266 bhp = &bh->next;
3267 }
3268
6eb5733a 3269 return ret;
83f64091
FB
3270}
3271
1b435b10
AL
3272void qemu_bh_schedule_idle(QEMUBH *bh)
3273{
3274 if (bh->scheduled)
3275 return;
3276 bh->scheduled = 1;
3277 bh->idle = 1;
3278}
3279
83f64091
FB
3280void qemu_bh_schedule(QEMUBH *bh)
3281{
3282 CPUState *env = cpu_single_env;
3283 if (bh->scheduled)
3284 return;
3285 bh->scheduled = 1;
1b435b10 3286 bh->idle = 0;
83f64091
FB
3287 /* stop the currently executing CPU to execute the BH ASAP */
3288 if (env) {
3289 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3290 }
3291}
3292
3293void qemu_bh_cancel(QEMUBH *bh)
3294{
1b435b10 3295 bh->scheduled = 0;
83f64091
FB
3296}
3297
3298void qemu_bh_delete(QEMUBH *bh)
3299{
1b435b10
AL
3300 bh->scheduled = 0;
3301 bh->deleted = 1;
83f64091
FB
3302}
3303
56f3a5d0
AL
3304static void qemu_bh_update_timeout(int *timeout)
3305{
3306 QEMUBH *bh;
3307
3308 for (bh = first_bh; bh; bh = bh->next) {
3309 if (!bh->deleted && bh->scheduled) {
3310 if (bh->idle) {
3311 /* idle bottom halves will be polled at least
3312 * every 10ms */
3313 *timeout = MIN(10, *timeout);
3314 } else {
3315 /* non-idle bottom halves will be executed
3316 * immediately */
3317 *timeout = 0;
3318 break;
3319 }
3320 }
3321 }
3322}
3323
cc1daa40
FB
3324/***********************************************************/
3325/* machine registration */
3326
bdaf78e0 3327static QEMUMachine *first_machine = NULL;
cc1daa40
FB
3328
3329int qemu_register_machine(QEMUMachine *m)
3330{
3331 QEMUMachine **pm;
3332 pm = &first_machine;
3333 while (*pm != NULL)
3334 pm = &(*pm)->next;
3335 m->next = NULL;
3336 *pm = m;
3337 return 0;
3338}
3339
9596ebb7 3340static QEMUMachine *find_machine(const char *name)
cc1daa40
FB
3341{
3342 QEMUMachine *m;
3343
3344 for(m = first_machine; m != NULL; m = m->next) {
3345 if (!strcmp(m->name, name))
3346 return m;
3347 }
3348 return NULL;
3349}
3350
8a7ddc38
FB
3351/***********************************************************/
3352/* main execution loop */
3353
9596ebb7 3354static void gui_update(void *opaque)
8a7ddc38 3355{
740733bb
TS
3356 DisplayState *ds = opaque;
3357 ds->dpy_refresh(ds);
f442e08b
AJ
3358 qemu_mod_timer(ds->gui_timer,
3359 (ds->gui_timer_interval ?
3360 ds->gui_timer_interval :
3361 GUI_REFRESH_INTERVAL)
3362 + qemu_get_clock(rt_clock));
8a7ddc38
FB
3363}
3364
0bd48850
FB
3365struct vm_change_state_entry {
3366 VMChangeStateHandler *cb;
3367 void *opaque;
3368 LIST_ENTRY (vm_change_state_entry) entries;
3369};
3370
3371static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3372
3373VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3374 void *opaque)
3375{
3376 VMChangeStateEntry *e;
3377
3378 e = qemu_mallocz(sizeof (*e));
3379 if (!e)
3380 return NULL;
3381
3382 e->cb = cb;
3383 e->opaque = opaque;
3384 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3385 return e;
3386}
3387
3388void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3389{
3390 LIST_REMOVE (e, entries);
3391 qemu_free (e);
3392}
3393
3394static void vm_state_notify(int running)
3395{
3396 VMChangeStateEntry *e;
3397
3398 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
3399 e->cb(e->opaque, running);
3400 }
3401}
3402
8a7ddc38 3403/* XXX: support several handlers */
0bd48850
FB
3404static VMStopHandler *vm_stop_cb;
3405static void *vm_stop_opaque;
8a7ddc38
FB
3406
3407int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
3408{
3409 vm_stop_cb = cb;
3410 vm_stop_opaque = opaque;
3411 return 0;
3412}
3413
3414void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
3415{
3416 vm_stop_cb = NULL;
3417}
3418
3419void vm_start(void)
3420{
3421 if (!vm_running) {
3422 cpu_enable_ticks();
3423 vm_running = 1;
0bd48850 3424 vm_state_notify(1);
efe75411 3425 qemu_rearm_alarm_timer(alarm_timer);
8a7ddc38
FB
3426 }
3427}
3428
5fafdf24 3429void vm_stop(int reason)
8a7ddc38
FB
3430{
3431 if (vm_running) {
3432 cpu_disable_ticks();
3433 vm_running = 0;
3434 if (reason != 0) {
3435 if (vm_stop_cb) {
3436 vm_stop_cb(vm_stop_opaque, reason);
3437 }
34865134 3438 }
0bd48850 3439 vm_state_notify(0);
8a7ddc38
FB
3440 }
3441}
3442
bb0c6722
FB
3443/* reset/shutdown handler */
3444
3445typedef struct QEMUResetEntry {
3446 QEMUResetHandler *func;
3447 void *opaque;
3448 struct QEMUResetEntry *next;
3449} QEMUResetEntry;
3450
3451static QEMUResetEntry *first_reset_entry;
3452static int reset_requested;
3453static int shutdown_requested;
3475187d 3454static int powerdown_requested;
bb0c6722 3455
cf7a2fe2
AJ
3456int qemu_shutdown_requested(void)
3457{
3458 int r = shutdown_requested;
3459 shutdown_requested = 0;
3460 return r;
3461}
3462
3463int qemu_reset_requested(void)
3464{
3465 int r = reset_requested;
3466 reset_requested = 0;
3467 return r;
3468}
3469
3470int qemu_powerdown_requested(void)
3471{
3472 int r = powerdown_requested;
3473 powerdown_requested = 0;
3474 return r;
3475}
3476
bb0c6722
FB
3477void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3478{
3479 QEMUResetEntry **pre, *re;
3480
3481 pre = &first_reset_entry;
3482 while (*pre != NULL)
3483 pre = &(*pre)->next;
3484 re = qemu_mallocz(sizeof(QEMUResetEntry));
3485 re->func = func;
3486 re->opaque = opaque;
3487 re->next = NULL;
3488 *pre = re;
3489}
3490
cf7a2fe2 3491void qemu_system_reset(void)
bb0c6722
FB
3492{
3493 QEMUResetEntry *re;
3494
3495 /* reset all devices */
3496 for(re = first_reset_entry; re != NULL; re = re->next) {
3497 re->func(re->opaque);
3498 }
3499}
3500
3501void qemu_system_reset_request(void)
3502{
d1beab82
FB
3503 if (no_reboot) {
3504 shutdown_requested = 1;
3505 } else {
3506 reset_requested = 1;
3507 }
6a00d601
FB
3508 if (cpu_single_env)
3509 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bb0c6722
FB
3510}
3511
3512void qemu_system_shutdown_request(void)
3513{
3514 shutdown_requested = 1;
6a00d601
FB
3515 if (cpu_single_env)
3516 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bb0c6722
FB
3517}
3518
3475187d
FB
3519void qemu_system_powerdown_request(void)
3520{
3521 powerdown_requested = 1;
6a00d601
FB
3522 if (cpu_single_env)
3523 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bb0c6722
FB
3524}
3525
877cf882 3526#ifdef _WIN32
56f3a5d0
AL
3527void host_main_loop_wait(int *timeout)
3528{
3529 int ret, ret2, i;
f331110f
FB
3530 PollingEntry *pe;
3531
c4b1fcc0 3532
f331110f
FB
3533 /* XXX: need to suppress polling by better using win32 events */
3534 ret = 0;
3535 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3536 ret |= pe->func(pe->opaque);
3537 }
e6b1e558 3538 if (ret == 0) {
a18e524a
FB
3539 int err;
3540 WaitObjects *w = &wait_objects;
3b46e624 3541
56f3a5d0 3542 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
a18e524a
FB
3543 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3544 if (w->func[ret - WAIT_OBJECT_0])
3545 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
3b46e624 3546
5fafdf24 3547 /* Check for additional signaled events */
e6b1e558 3548 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
3b46e624 3549
e6b1e558
TS
3550 /* Check if event is signaled */
3551 ret2 = WaitForSingleObject(w->events[i], 0);
3552 if(ret2 == WAIT_OBJECT_0) {
3553 if (w->func[i])
3554 w->func[i](w->opaque[i]);
3555 } else if (ret2 == WAIT_TIMEOUT) {
3556 } else {
3557 err = GetLastError();
3558 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
3b46e624
TS
3559 }
3560 }
a18e524a
FB
3561 } else if (ret == WAIT_TIMEOUT) {
3562 } else {
3563 err = GetLastError();
e6b1e558 3564 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
a18e524a 3565 }
f331110f 3566 }
56f3a5d0
AL
3567
3568 *timeout = 0;
3569}
3570#else
3571void host_main_loop_wait(int *timeout)
3572{
3573}
fd1dff4b 3574#endif
56f3a5d0
AL
3575
3576void main_loop_wait(int timeout)
3577{
3578 IOHandlerRecord *ioh;
3579 fd_set rfds, wfds, xfds;
3580 int ret, nfds;
3581 struct timeval tv;
3582
3583 qemu_bh_update_timeout(&timeout);
3584
3585 host_main_loop_wait(&timeout);
3586
fd1dff4b
FB
3587 /* poll any events */
3588 /* XXX: separate device handlers from system ones */
6abfbd79 3589 nfds = -1;
fd1dff4b
FB
3590 FD_ZERO(&rfds);
3591 FD_ZERO(&wfds);
e035649e 3592 FD_ZERO(&xfds);
fd1dff4b 3593 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
cafffd40
TS
3594 if (ioh->deleted)
3595 continue;
fd1dff4b
FB
3596 if (ioh->fd_read &&
3597 (!ioh->fd_read_poll ||
3598 ioh->fd_read_poll(ioh->opaque) != 0)) {
3599 FD_SET(ioh->fd, &rfds);
3600 if (ioh->fd > nfds)
3601 nfds = ioh->fd;
3602 }
3603 if (ioh->fd_write) {
3604 FD_SET(ioh->fd, &wfds);
3605 if (ioh->fd > nfds)
3606 nfds = ioh->fd;
3607 }
3608 }
3b46e624 3609
56f3a5d0
AL
3610 tv.tv_sec = timeout / 1000;
3611 tv.tv_usec = (timeout % 1000) * 1000;
3612
e035649e 3613#if defined(CONFIG_SLIRP)
63a01ef8 3614 if (slirp_is_inited()) {
e035649e
FB
3615 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3616 }
3617#endif
3618 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
fd1dff4b 3619 if (ret > 0) {
cafffd40
TS
3620 IOHandlerRecord **pioh;
3621
3622 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
6ab43fdc 3623 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
fd1dff4b 3624 ioh->fd_read(ioh->opaque);
7c9d8e07 3625 }
6ab43fdc 3626 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
fd1dff4b 3627 ioh->fd_write(ioh->opaque);
c4b1fcc0 3628 }
b4608c04 3629 }
cafffd40
TS
3630
3631 /* remove deleted IO handlers */
3632 pioh = &first_io_handler;
3633 while (*pioh) {
3634 ioh = *pioh;
3635 if (ioh->deleted) {
3636 *pioh = ioh->next;
3637 qemu_free(ioh);
5fafdf24 3638 } else
cafffd40
TS
3639 pioh = &ioh->next;
3640 }
fd1dff4b 3641 }
c20709aa 3642#if defined(CONFIG_SLIRP)
63a01ef8 3643 if (slirp_is_inited()) {
e035649e
FB
3644 if (ret < 0) {
3645 FD_ZERO(&rfds);
3646 FD_ZERO(&wfds);
3647 FD_ZERO(&xfds);
c20709aa 3648 }
e035649e 3649 slirp_select_poll(&rfds, &wfds, &xfds);
fd1dff4b 3650 }
e035649e 3651#endif
b4608c04 3652
357c692c
AL
3653 /* vm time timers */
3654 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3655 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3656 qemu_get_clock(vm_clock));
3657
3658 /* real time timers */
3659 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3660 qemu_get_clock(rt_clock));
3661
423f0742
PB
3662 /* Check bottom-halves last in case any of the earlier events triggered
3663 them. */
3664 qemu_bh_poll();
3b46e624 3665
5905b2e5
FB
3666}
3667
9596ebb7 3668static int main_loop(void)
5905b2e5
FB
3669{
3670 int ret, timeout;
89bfc105
FB
3671#ifdef CONFIG_PROFILER
3672 int64_t ti;
3673#endif
6a00d601 3674 CPUState *env;
5905b2e5 3675
6a00d601 3676 cur_cpu = first_cpu;
ee5605e5 3677 next_cpu = cur_cpu->next_cpu ?: first_cpu;
5905b2e5
FB
3678 for(;;) {
3679 if (vm_running) {
15a76449 3680
15a76449
FB
3681 for(;;) {
3682 /* get next cpu */
ee5605e5 3683 env = next_cpu;
89bfc105
FB
3684#ifdef CONFIG_PROFILER
3685 ti = profile_getclock();
3686#endif
2e70f6ef
PB
3687 if (use_icount) {
3688 int64_t count;
3689 int decr;
3690 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3691 env->icount_decr.u16.low = 0;
3692 env->icount_extra = 0;
3693 count = qemu_next_deadline();
3694 count = (count + (1 << icount_time_shift) - 1)
3695 >> icount_time_shift;
3696 qemu_icount += count;
3697 decr = (count > 0xffff) ? 0xffff : count;
3698 count -= decr;
3699 env->icount_decr.u16.low = decr;
3700 env->icount_extra = count;
3701 }
6a00d601 3702 ret = cpu_exec(env);
89bfc105
FB
3703#ifdef CONFIG_PROFILER
3704 qemu_time += profile_getclock() - ti;
3705#endif
2e70f6ef
PB
3706 if (use_icount) {
3707 /* Fold pending instructions back into the
3708 instruction counter, and clear the interrupt flag. */
3709 qemu_icount -= (env->icount_decr.u16.low
3710 + env->icount_extra);
3711 env->icount_decr.u32 = 0;
3712 env->icount_extra = 0;
3713 }
ee5605e5 3714 next_cpu = env->next_cpu ?: first_cpu;
95b01009 3715 if (event_pending && likely(ret != EXCP_DEBUG)) {
ee5605e5
AZ
3716 ret = EXCP_INTERRUPT;
3717 event_pending = 0;
3718 break;
3719 }
bd967e05
PB
3720 if (ret == EXCP_HLT) {
3721 /* Give the next CPU a chance to run. */
3722 cur_cpu = env;
3723 continue;
3724 }
15a76449
FB
3725 if (ret != EXCP_HALTED)
3726 break;
3727 /* all CPUs are halted ? */
bd967e05 3728 if (env == cur_cpu)
15a76449 3729 break;
15a76449
FB
3730 }
3731 cur_cpu = env;
3732
5905b2e5 3733 if (shutdown_requested) {
3475187d 3734 ret = EXCP_INTERRUPT;
b2f76161
AJ
3735 if (no_shutdown) {
3736 vm_stop(0);
3737 no_shutdown = 0;
3738 }
3739 else
3740 break;
5905b2e5
FB
3741 }
3742 if (reset_requested) {
3743 reset_requested = 0;
3744 qemu_system_reset();
3475187d
FB
3745 ret = EXCP_INTERRUPT;
3746 }
3747 if (powerdown_requested) {
3748 powerdown_requested = 0;
3749 qemu_system_powerdown();
3750 ret = EXCP_INTERRUPT;
5905b2e5 3751 }
95b01009 3752 if (unlikely(ret == EXCP_DEBUG)) {
880a7578 3753 gdb_set_stop_cpu(cur_cpu);
5905b2e5
FB
3754 vm_stop(EXCP_DEBUG);
3755 }
bd967e05 3756 /* If all cpus are halted then wait until the next IRQ */
5905b2e5 3757 /* XXX: use timeout computed from timers */
2e70f6ef
PB
3758 if (ret == EXCP_HALTED) {
3759 if (use_icount) {
3760 int64_t add;
3761 int64_t delta;
3762 /* Advance virtual time to the next event. */
3763 if (use_icount == 1) {
3764 /* When not using an adaptive execution frequency
3765 we tend to get badly out of sync with real time,
bf20dc07 3766 so just delay for a reasonable amount of time. */
2e70f6ef
PB
3767 delta = 0;
3768 } else {
3769 delta = cpu_get_icount() - cpu_get_clock();
3770 }
3771 if (delta > 0) {
3772 /* If virtual time is ahead of real time then just
3773 wait for IO. */
3774 timeout = (delta / 1000000) + 1;
3775 } else {
3776 /* Wait for either IO to occur or the next
3777 timer event. */
3778 add = qemu_next_deadline();
3779 /* We advance the timer before checking for IO.
3780 Limit the amount we advance so that early IO
3781 activity won't get the guest too far ahead. */
3782 if (add > 10000000)
3783 add = 10000000;
3784 delta += add;
3785 add = (add + (1 << icount_time_shift) - 1)
3786 >> icount_time_shift;
3787 qemu_icount += add;
3788 timeout = delta / 1000000;
3789 if (timeout < 0)
3790 timeout = 0;
3791 }
3792 } else {
0a1af395 3793 timeout = 5000;
2e70f6ef
PB
3794 }
3795 } else {
5905b2e5 3796 timeout = 0;
2e70f6ef 3797 }
5905b2e5 3798 } else {
98448f58
BS
3799 if (shutdown_requested) {
3800 ret = EXCP_INTERRUPT;
5b08fc10 3801 break;
98448f58 3802 }
0a1af395 3803 timeout = 5000;
5905b2e5 3804 }
89bfc105
FB
3805#ifdef CONFIG_PROFILER
3806 ti = profile_getclock();
3807#endif
5905b2e5 3808 main_loop_wait(timeout);
89bfc105
FB
3809#ifdef CONFIG_PROFILER
3810 dev_time += profile_getclock() - ti;
3811#endif
b4608c04 3812 }
34865134
FB
3813 cpu_disable_ticks();
3814 return ret;
b4608c04
FB
3815}
3816
15f82208 3817static void help(int exitcode)
0824d6fc 3818{
68d0f70e 3819 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
0db63474 3820 "usage: %s [options] [disk_image]\n"
0824d6fc 3821 "\n"
a20dd508 3822 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
fc01f7e7 3823 "\n"
a20dd508 3824 "Standard options:\n"
cc1daa40 3825 "-M machine select emulated machine (-M ? for list)\n"
5adb4839 3826 "-cpu cpu select CPU (-cpu ? for list)\n"
c45886db 3827 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
36b486bb
FB
3828 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3829 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
c4b1fcc0 3830 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
a1620fac
AJ
3831 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3832 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
9f7965c7 3833 " [,cache=writethrough|writeback|none][,format=f]\n"
e4bcb14c 3834 " use 'file' as a drive image\n"
3e3d5815 3835 "-mtdblock file use 'file' as on-board Flash memory image\n"
a1bb27b1 3836 "-sd file use 'file' as SecureDigital card image\n"
86f55663 3837 "-pflash file use 'file' as a parallel flash image\n"
eec85c2a 3838 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
667accab
TS
3839 "-snapshot write to temporary files instead of disk image files\n"
3840#ifdef CONFIG_SDL
43523e93 3841 "-no-frame open SDL window without a frame and window decorations\n"
3780e197 3842 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
667accab
TS
3843 "-no-quit disable SDL window close capability\n"
3844#endif
52ca8d6a
FB
3845#ifdef TARGET_I386
3846 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3847#endif
a00bad7e 3848 "-m megs set virtual RAM size to megs MB [default=%d]\n"
91fc2119 3849 "-smp n set the number of CPUs to 'n' [default=1]\n"
c4b1fcc0 3850 "-nographic disable graphical output and redirect serial I/Os to console\n"
a171fe39 3851 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
4ca0074c 3852#ifndef _WIN32
667accab 3853 "-k language use keyboard layout (for example \"fr\" for French)\n"
4ca0074c 3854#endif
1d14ffa9 3855#ifdef HAS_AUDIO
1d14ffa9 3856 "-audio-help print list of audio drivers and their options\n"
c0fe3827
FB
3857 "-soundhw c1,... enable audio support\n"
3858 " and only specified sound cards (comma separated list)\n"
3859 " use -soundhw ? to get the list of supported cards\n"
6a36d84e 3860 " use -soundhw all to enable all of them\n"
1d14ffa9 3861#endif
3893c124 3862 "-vga [std|cirrus|vmware]\n"
3863 " select video card type\n"
89980284 3864 "-localtime set the real time clock to local time [default=utc]\n"
d63d307f 3865 "-full-screen start in full screen\n"
a09db21f
FB
3866#ifdef TARGET_I386
3867 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3868#endif
b389dbfb
FB
3869 "-usb enable the USB driver (will be the default soon)\n"
3870 "-usbdevice name add the host or guest USB device 'name'\n"
6f7e9aec
FB
3871#if defined(TARGET_PPC) || defined(TARGET_SPARC)
3872 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bb0c6722 3873#endif
c35734b2 3874 "-name string set the name of the guest\n"
8fcb1b90 3875 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
c4b1fcc0
FB
3876 "\n"
3877 "Network options:\n"
a41b2ff2 3878 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7c9d8e07 3879 " create a new Network Interface Card and connect it to VLAN 'n'\n"
c20709aa 3880#ifdef CONFIG_SLIRP
115defd1
PB
3881 "-net user[,vlan=n][,hostname=host]\n"
3882 " connect the user mode network stack to VLAN 'n' and send\n"
3883 " hostname 'host' to DHCP clients\n"
7c9d8e07 3884#endif
7fb843f8
FB
3885#ifdef _WIN32
3886 "-net tap[,vlan=n],ifname=name\n"
3887 " connect the host TAP network interface to VLAN 'n'\n"
3888#else
b46a8906
TS
3889 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3890 " connect the host TAP network interface to VLAN 'n' and use the\n"
3891 " network scripts 'file' (default=%s)\n"
3892 " and 'dfile' (default=%s);\n"
3893 " use '[down]script=no' to disable script execution;\n"
7c9d8e07 3894 " use 'fd=h' to connect to an already opened TAP interface\n"
7fb843f8 3895#endif
6a00d601 3896 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7c9d8e07 3897 " connect the vlan 'n' to another VLAN using a socket connection\n"
3d830459
FB
3898 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
3899 " connect the vlan 'n' to multicast maddr and port\n"
8a16d273
TS
3900#ifdef CONFIG_VDE
3901 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3902 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3903 " on host and listening for incoming connections on 'socketpath'.\n"
3904 " Use group 'groupname' and mode 'octalmode' to change default\n"
3905 " ownership and permissions for communication port.\n"
3906#endif
7c9d8e07
FB
3907 "-net none use it alone to have zero network devices; if no -net option\n"
3908 " is provided, the default is '-net nic -net user'\n"
3909 "\n"
dc72ac14
AZ
3910 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3911 "-bt hci,host[:id]\n"
3912 " Use host's HCI with the given name\n"
3913 "-bt hci[,vlan=n]\n"
3914 " Emulate a standard HCI in virtual scatternet 'n'\n"
3915 "-bt vhci[,vlan=n]\n"
3916 " Add host computer to virtual scatternet 'n' using VHCI\n"
3917 "-bt device:dev[,vlan=n]\n"
3918 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3919 "\n"
7c9d8e07 3920#ifdef CONFIG_SLIRP
0db1137d 3921 "-tftp dir allow tftp access to files in dir [-net user]\n"
47d5d01a 3922 "-bootp file advertise file in BOOTP replies\n"
7c9d8e07
FB
3923#ifndef _WIN32
3924 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
c94c8d64 3925#endif
9bf05444 3926 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7c9d8e07 3927 " redirect TCP or UDP connections from host to guest [-net user]\n"
c20709aa 3928#endif
a20dd508 3929 "\n"
c4b1fcc0 3930 "Linux boot specific:\n"
a20dd508
FB
3931 "-kernel bzImage use 'bzImage' as kernel image\n"
3932 "-append cmdline use 'cmdline' as kernel command line\n"
3933 "-initrd file use 'file' as initial ram disk\n"
fc01f7e7 3934 "\n"
330d0414 3935 "Debug/Expert options:\n"
82c643ff
FB
3936 "-monitor dev redirect the monitor to char device 'dev'\n"
3937 "-serial dev redirect the serial port to char device 'dev'\n"
6508fe59 3938 "-parallel dev redirect the parallel port to char device 'dev'\n"
f7cce898 3939 "-pidfile file Write PID to 'file'\n"
cd6f1169 3940 "-S freeze CPU at startup (use 'c' to start execution)\n"
cfc3475a
PB
3941 "-s wait gdb connection to port\n"
3942 "-p port set gdb connection port [default=%s]\n"
f193c797 3943 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
46d4767d
FB
3944 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3945 " translation (t=none or lba) (usually qemu can guess them)\n"
87b47350 3946 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
d993e026 3947#ifdef USE_KQEMU
6515b203 3948 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
d993e026
FB
3949 "-no-kqemu disable KQEMU kernel module usage\n"
3950#endif
7ba1e619
AL
3951#ifdef CONFIG_KVM
3952 "-enable-kvm enable KVM full virtualization support\n"
3953#endif
bb0c6722 3954#ifdef TARGET_I386
6515b203 3955 "-no-acpi disable ACPI\n"
4d3b6f6e
AZ
3956#endif
3957#ifdef CONFIG_CURSES
3958 "-curses use a curses/ncurses interface instead of SDL\n"
bb0c6722 3959#endif
d1beab82 3960 "-no-reboot exit instead of rebooting\n"
b2f76161 3961 "-no-shutdown stop before shutdown\n"
a8080006 3962 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
24236869 3963 "-vnc display start a VNC server on display\n"
71e3ceb8
TS
3964#ifndef _WIN32
3965 "-daemonize daemonize QEMU after initializing\n"
3966#endif
9ae02555 3967 "-option-rom rom load a file, rom, into the option ROM space\n"
66508601
BS
3968#ifdef TARGET_SPARC
3969 "-prom-env variable=value set OpenBIOS nvram variables\n"
3970#endif
f3dcfada 3971 "-clock force the use of the given methods for timer alarm.\n"
3adda04c 3972 " To see what timers are available use -clock ?\n"
bce61846 3973 "-startdate select initial date of the clock\n"
2e70f6ef 3974 "-icount [N|auto]\n"
dd5d6fe9 3975 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
0824d6fc 3976 "\n"
82c643ff 3977 "During emulation, the following keys are useful:\n"
032a8c9e
FB
3978 "ctrl-alt-f toggle full screen\n"
3979 "ctrl-alt-n switch to virtual console 'n'\n"
3980 "ctrl-alt toggle mouse and keyboard grab\n"
82c643ff
FB
3981 "\n"
3982 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3983 ,
0db63474 3984 "qemu",
a00bad7e 3985 DEFAULT_RAM_SIZE,
7c9d8e07 3986#ifndef _WIN32
a00bad7e 3987 DEFAULT_NETWORK_SCRIPT,
b46a8906 3988 DEFAULT_NETWORK_DOWN_SCRIPT,
7c9d8e07 3989#endif
6e44ba7f 3990 DEFAULT_GDBSTUB_PORT,
bce61846 3991 "/tmp/qemu.log");
15f82208 3992 exit(exitcode);
0824d6fc
FB
3993}
3994
cd6f1169
FB
3995#define HAS_ARG 0x0001
3996
3997enum {
3998 QEMU_OPTION_h,
3999
cc1daa40 4000 QEMU_OPTION_M,
94fc95cd 4001 QEMU_OPTION_cpu,
cd6f1169
FB
4002 QEMU_OPTION_fda,
4003 QEMU_OPTION_fdb,
4004 QEMU_OPTION_hda,
4005 QEMU_OPTION_hdb,
4006 QEMU_OPTION_hdc,
4007 QEMU_OPTION_hdd,
e4bcb14c 4008 QEMU_OPTION_drive,
cd6f1169 4009 QEMU_OPTION_cdrom,
3e3d5815 4010 QEMU_OPTION_mtdblock,
a1bb27b1 4011 QEMU_OPTION_sd,
86f55663 4012 QEMU_OPTION_pflash,
cd6f1169
FB
4013 QEMU_OPTION_boot,
4014 QEMU_OPTION_snapshot,
52ca8d6a
FB
4015#ifdef TARGET_I386
4016 QEMU_OPTION_no_fd_bootchk,
4017#endif
cd6f1169
FB
4018 QEMU_OPTION_m,
4019 QEMU_OPTION_nographic,
a171fe39 4020 QEMU_OPTION_portrait,
1d14ffa9 4021#ifdef HAS_AUDIO
1d14ffa9
FB
4022 QEMU_OPTION_audio_help,
4023 QEMU_OPTION_soundhw,
4024#endif
cd6f1169 4025
7c9d8e07 4026 QEMU_OPTION_net,
c7f74643 4027 QEMU_OPTION_tftp,
47d5d01a 4028 QEMU_OPTION_bootp,
9d728e8c 4029 QEMU_OPTION_smb,
9bf05444 4030 QEMU_OPTION_redir,
dc72ac14 4031 QEMU_OPTION_bt,
cd6f1169
FB
4032
4033 QEMU_OPTION_kernel,
4034 QEMU_OPTION_append,
4035 QEMU_OPTION_initrd,
4036
4037 QEMU_OPTION_S,
4038 QEMU_OPTION_s,
4039 QEMU_OPTION_p,
4040 QEMU_OPTION_d,
4041 QEMU_OPTION_hdachs,
4042 QEMU_OPTION_L,
1192dad8 4043 QEMU_OPTION_bios,
3d11d0eb 4044 QEMU_OPTION_k,
ee22c2f7 4045 QEMU_OPTION_localtime,
e9b137c2 4046 QEMU_OPTION_g,
3893c124 4047 QEMU_OPTION_vga,
20d8a3ed 4048 QEMU_OPTION_echr,
82c643ff
FB
4049 QEMU_OPTION_monitor,
4050 QEMU_OPTION_serial,
6508fe59 4051 QEMU_OPTION_parallel,
d63d307f
FB
4052 QEMU_OPTION_loadvm,
4053 QEMU_OPTION_full_screen,
43523e93 4054 QEMU_OPTION_no_frame,
3780e197 4055 QEMU_OPTION_alt_grab,
667accab 4056 QEMU_OPTION_no_quit,
f7cce898 4057 QEMU_OPTION_pidfile,
d993e026 4058 QEMU_OPTION_no_kqemu,
89bfc105 4059 QEMU_OPTION_kernel_kqemu,
7ba1e619 4060 QEMU_OPTION_enable_kvm,
a09db21f 4061 QEMU_OPTION_win2k_hack,
bb36d470 4062 QEMU_OPTION_usb,
a594cfbf 4063 QEMU_OPTION_usbdevice,
6a00d601 4064 QEMU_OPTION_smp,
24236869 4065 QEMU_OPTION_vnc,
6515b203 4066 QEMU_OPTION_no_acpi,
4d3b6f6e 4067 QEMU_OPTION_curses,
d1beab82 4068 QEMU_OPTION_no_reboot,
b2f76161 4069 QEMU_OPTION_no_shutdown,
9467cd46 4070 QEMU_OPTION_show_cursor,
71e3ceb8 4071 QEMU_OPTION_daemonize,
9ae02555 4072 QEMU_OPTION_option_rom,
c35734b2
TS
4073 QEMU_OPTION_semihosting,
4074 QEMU_OPTION_name,
66508601 4075 QEMU_OPTION_prom_env,
2b8f2d41 4076 QEMU_OPTION_old_param,
f3dcfada 4077 QEMU_OPTION_clock,
7e0af5d0 4078 QEMU_OPTION_startdate,
26a5f13b 4079 QEMU_OPTION_tb_size,
2e70f6ef 4080 QEMU_OPTION_icount,
8fcb1b90 4081 QEMU_OPTION_uuid,
5bb7910a 4082 QEMU_OPTION_incoming,
cd6f1169
FB
4083};
4084
4085typedef struct QEMUOption {
4086 const char *name;
4087 int flags;
4088 int index;
4089} QEMUOption;
4090
dbed7e40 4091static const QEMUOption qemu_options[] = {
cd6f1169 4092 { "h", 0, QEMU_OPTION_h },
64423fb2 4093 { "help", 0, QEMU_OPTION_h },
cd6f1169 4094
cc1daa40 4095 { "M", HAS_ARG, QEMU_OPTION_M },
94fc95cd 4096 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
cd6f1169
FB
4097 { "fda", HAS_ARG, QEMU_OPTION_fda },
4098 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
4099 { "hda", HAS_ARG, QEMU_OPTION_hda },
4100 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
4101 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
4102 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
e4bcb14c 4103 { "drive", HAS_ARG, QEMU_OPTION_drive },
cd6f1169 4104 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
3e3d5815 4105 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
a1bb27b1 4106 { "sd", HAS_ARG, QEMU_OPTION_sd },
86f55663 4107 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
cd6f1169
FB
4108 { "boot", HAS_ARG, QEMU_OPTION_boot },
4109 { "snapshot", 0, QEMU_OPTION_snapshot },
52ca8d6a
FB
4110#ifdef TARGET_I386
4111 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
4112#endif
cd6f1169
FB
4113 { "m", HAS_ARG, QEMU_OPTION_m },
4114 { "nographic", 0, QEMU_OPTION_nographic },
a171fe39 4115 { "portrait", 0, QEMU_OPTION_portrait },
3d11d0eb 4116 { "k", HAS_ARG, QEMU_OPTION_k },
1d14ffa9 4117#ifdef HAS_AUDIO
1d14ffa9
FB
4118 { "audio-help", 0, QEMU_OPTION_audio_help },
4119 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
4120#endif
cd6f1169 4121
7c9d8e07 4122 { "net", HAS_ARG, QEMU_OPTION_net},
158156d1 4123#ifdef CONFIG_SLIRP
c7f74643 4124 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
47d5d01a 4125 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
c94c8d64 4126#ifndef _WIN32
9d728e8c 4127 { "smb", HAS_ARG, QEMU_OPTION_smb },
c94c8d64 4128#endif
9bf05444 4129 { "redir", HAS_ARG, QEMU_OPTION_redir },
158156d1 4130#endif
dc72ac14 4131 { "bt", HAS_ARG, QEMU_OPTION_bt },
cd6f1169
FB
4132
4133 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
4134 { "append", HAS_ARG, QEMU_OPTION_append },
4135 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
4136
4137 { "S", 0, QEMU_OPTION_S },
4138 { "s", 0, QEMU_OPTION_s },
4139 { "p", HAS_ARG, QEMU_OPTION_p },
4140 { "d", HAS_ARG, QEMU_OPTION_d },
4141 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
4142 { "L", HAS_ARG, QEMU_OPTION_L },
1192dad8 4143 { "bios", HAS_ARG, QEMU_OPTION_bios },
d993e026
FB
4144#ifdef USE_KQEMU
4145 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
89bfc105 4146 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
d993e026 4147#endif
7ba1e619
AL
4148#ifdef CONFIG_KVM
4149 { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
4150#endif
6f7e9aec 4151#if defined(TARGET_PPC) || defined(TARGET_SPARC)
e9b137c2 4152 { "g", 1, QEMU_OPTION_g },
77d4bc34 4153#endif
ee22c2f7 4154 { "localtime", 0, QEMU_OPTION_localtime },
3893c124 4155 { "vga", HAS_ARG, QEMU_OPTION_vga },
8b6e0729
AZ
4156 { "echr", HAS_ARG, QEMU_OPTION_echr },
4157 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
4158 { "serial", HAS_ARG, QEMU_OPTION_serial },
4159 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
d63d307f
FB
4160 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
4161 { "full-screen", 0, QEMU_OPTION_full_screen },
667accab 4162#ifdef CONFIG_SDL
43523e93 4163 { "no-frame", 0, QEMU_OPTION_no_frame },
3780e197 4164 { "alt-grab", 0, QEMU_OPTION_alt_grab },
667accab
TS
4165 { "no-quit", 0, QEMU_OPTION_no_quit },
4166#endif
f7cce898 4167 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
a09db21f 4168 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
a594cfbf 4169 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
6a00d601 4170 { "smp", HAS_ARG, QEMU_OPTION_smp },
24236869 4171 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
4d3b6f6e
AZ
4172#ifdef CONFIG_CURSES
4173 { "curses", 0, QEMU_OPTION_curses },
4174#endif
8fcb1b90 4175 { "uuid", HAS_ARG, QEMU_OPTION_uuid },
96d30e48 4176
1f04275e 4177 /* temporary options */
a594cfbf 4178 { "usb", 0, QEMU_OPTION_usb },
6515b203 4179 { "no-acpi", 0, QEMU_OPTION_no_acpi },
d1beab82 4180 { "no-reboot", 0, QEMU_OPTION_no_reboot },
b2f76161 4181 { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
9467cd46 4182 { "show-cursor", 0, QEMU_OPTION_show_cursor },
71e3ceb8 4183 { "daemonize", 0, QEMU_OPTION_daemonize },
9ae02555 4184 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
a87295e8 4185#if defined(TARGET_ARM) || defined(TARGET_M68K)
8e71621f
PB
4186 { "semihosting", 0, QEMU_OPTION_semihosting },
4187#endif
c35734b2 4188 { "name", HAS_ARG, QEMU_OPTION_name },
66508601
BS
4189#if defined(TARGET_SPARC)
4190 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
2b8f2d41
AZ
4191#endif
4192#if defined(TARGET_ARM)
4193 { "old-param", 0, QEMU_OPTION_old_param },
66508601 4194#endif
f3dcfada 4195 { "clock", HAS_ARG, QEMU_OPTION_clock },
7e0af5d0 4196 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
26a5f13b 4197 { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
2e70f6ef 4198 { "icount", HAS_ARG, QEMU_OPTION_icount },
5bb7910a 4199 { "incoming", HAS_ARG, QEMU_OPTION_incoming },
cd6f1169 4200 { NULL },
fc01f7e7
FB
4201};
4202
5905b2e5
FB
4203/* password input */
4204
2bac6019
AZ
4205int qemu_key_check(BlockDriverState *bs, const char *name)
4206{
4207 char password[256];
4208 int i;
4209
4210 if (!bdrv_is_encrypted(bs))
4211 return 0;
4212
4213 term_printf("%s is encrypted.\n", name);
4214 for(i = 0; i < 3; i++) {
4215 monitor_readline("Password: ", 1, password, sizeof(password));
4216 if (bdrv_set_key(bs, password) == 0)
4217 return 0;
4218 term_printf("invalid password\n");
4219 }
4220 return -EPERM;
4221}
4222
83ab7950
AL
4223static BlockDriverState *get_bdrv(int index)
4224{
4225 if (index > nb_drives)
4226 return NULL;
4227 return drives_table[index].bdrv;
4228}
4229
5905b2e5
FB
4230static void read_passwords(void)
4231{
4232 BlockDriverState *bs;
2bac6019 4233 int i;
5905b2e5 4234
83ab7950
AL
4235 for(i = 0; i < 6; i++) {
4236 bs = get_bdrv(i);
4237 if (bs)
4238 qemu_key_check(bs, bdrv_get_device_name(bs));
5905b2e5
FB
4239 }
4240}
4241
1d14ffa9 4242#ifdef HAS_AUDIO
6a36d84e 4243struct soundhw soundhw[] = {
b00052e4 4244#ifdef HAS_AUDIO_CHOICE
4ce7ff6e 4245#if defined(TARGET_I386) || defined(TARGET_MIPS)
fd06c375
FB
4246 {
4247 "pcspk",
4248 "PC speaker",
4249 0,
4250 1,
4251 { .init_isa = pcspk_audio_init }
4252 },
4253#endif
6a36d84e
FB
4254 {
4255 "sb16",
4256 "Creative Sound Blaster 16",
4257 0,
4258 1,
4259 { .init_isa = SB16_init }
4260 },
4261
cc53d26d 4262#ifdef CONFIG_CS4231A
4263 {
4264 "cs4231a",
4265 "CS4231A",
4266 0,
4267 1,
4268 { .init_isa = cs4231a_init }
4269 },
4270#endif
4271
1d14ffa9 4272#ifdef CONFIG_ADLIB
6a36d84e
FB
4273 {
4274 "adlib",
1d14ffa9 4275#ifdef HAS_YMF262
6a36d84e 4276 "Yamaha YMF262 (OPL3)",
1d14ffa9 4277#else
6a36d84e 4278 "Yamaha YM3812 (OPL2)",
1d14ffa9 4279#endif
6a36d84e
FB
4280 0,
4281 1,
4282 { .init_isa = Adlib_init }
4283 },
1d14ffa9 4284#endif
6a36d84e 4285
1d14ffa9 4286#ifdef CONFIG_GUS
6a36d84e
FB
4287 {
4288 "gus",
4289 "Gravis Ultrasound GF1",
4290 0,
4291 1,
4292 { .init_isa = GUS_init }
4293 },
1d14ffa9 4294#endif
6a36d84e 4295
e5c9a13e
AZ
4296#ifdef CONFIG_AC97
4297 {
4298 "ac97",
4299 "Intel 82801AA AC97 Audio",
4300 0,
4301 0,
4302 { .init_pci = ac97_init }
4303 },
4304#endif
4305
6a36d84e
FB
4306 {
4307 "es1370",
4308 "ENSONIQ AudioPCI ES1370",
4309 0,
4310 0,
4311 { .init_pci = es1370_init }
4312 },
b00052e4 4313#endif
6a36d84e
FB
4314
4315 { NULL, NULL, 0, 0, { NULL } }
4316};
4317
4318static void select_soundhw (const char *optarg)
4319{
4320 struct soundhw *c;
4321
4322 if (*optarg == '?') {
4323 show_valid_cards:
4324
4325 printf ("Valid sound card names (comma separated):\n");
4326 for (c = soundhw; c->name; ++c) {
4327 printf ("%-11s %s\n", c->name, c->descr);
4328 }
4329 printf ("\n-soundhw all will enable all of the above\n");
1d14ffa9
FB
4330 exit (*optarg != '?');
4331 }
4332 else {
6a36d84e 4333 size_t l;
1d14ffa9
FB
4334 const char *p;
4335 char *e;
4336 int bad_card = 0;
4337
6a36d84e
FB
4338 if (!strcmp (optarg, "all")) {
4339 for (c = soundhw; c->name; ++c) {
4340 c->enabled = 1;
4341 }
4342 return;
4343 }
1d14ffa9 4344
6a36d84e 4345 p = optarg;
1d14ffa9
FB
4346 while (*p) {
4347 e = strchr (p, ',');
4348 l = !e ? strlen (p) : (size_t) (e - p);
6a36d84e
FB
4349
4350 for (c = soundhw; c->name; ++c) {
4351 if (!strncmp (c->name, p, l)) {
4352 c->enabled = 1;
1d14ffa9
FB
4353 break;
4354 }
4355 }
6a36d84e
FB
4356
4357 if (!c->name) {
1d14ffa9
FB
4358 if (l > 80) {
4359 fprintf (stderr,
4360 "Unknown sound card name (too big to show)\n");
4361 }
4362 else {
4363 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4364 (int) l, p);
4365 }
4366 bad_card = 1;
4367 }
4368 p += l + (e != NULL);
4369 }
4370
4371 if (bad_card)
4372 goto show_valid_cards;
4373 }
4374}
4375#endif
4376
3893c124 4377static void select_vgahw (const char *p)
4378{
4379 const char *opts;
4380
4381 if (strstart(p, "std", &opts)) {
4382 cirrus_vga_enabled = 0;
4383 vmsvga_enabled = 0;
4384 } else if (strstart(p, "cirrus", &opts)) {
4385 cirrus_vga_enabled = 1;
4386 vmsvga_enabled = 0;
4387 } else if (strstart(p, "vmware", &opts)) {
4388 cirrus_vga_enabled = 0;
4389 vmsvga_enabled = 1;
4390 } else {
4391 invalid_vga:
4392 fprintf(stderr, "Unknown vga type: %s\n", p);
4393 exit(1);
4394 }
cb5a7aa8 4395 while (*opts) {
4396 const char *nextopt;
4397
4398 if (strstart(opts, ",retrace=", &nextopt)) {
4399 opts = nextopt;
4400 if (strstart(opts, "dumb", &nextopt))
4401 vga_retrace_method = VGA_RETRACE_DUMB;
4402 else if (strstart(opts, "precise", &nextopt))
4403 vga_retrace_method = VGA_RETRACE_PRECISE;
4404 else goto invalid_vga;
4405 } else goto invalid_vga;
4406 opts = nextopt;
4407 }
3893c124 4408}
4409
3587d7e6
FB
4410#ifdef _WIN32
4411static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4412{
4413 exit(STATUS_CONTROL_C_EXIT);
4414 return TRUE;
4415}
4416#endif
4417
8fcb1b90
BS
4418static int qemu_uuid_parse(const char *str, uint8_t *uuid)
4419{
4420 int ret;
4421
4422 if(strlen(str) != 36)
4423 return -1;
4424
4425 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4426 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4427 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4428
4429 if(ret != 16)
4430 return -1;
4431
4432 return 0;
4433}
4434
7c9d8e07 4435#define MAX_NET_CLIENTS 32
c20709aa 4436
5b08fc10
AL
4437#ifndef _WIN32
4438
4439static void termsig_handler(int signal)
4440{
4441 qemu_system_shutdown_request();
4442}
4443
6f9e3801 4444static void termsig_setup(void)
5b08fc10
AL
4445{
4446 struct sigaction act;
4447
4448 memset(&act, 0, sizeof(act));
4449 act.sa_handler = termsig_handler;
4450 sigaction(SIGINT, &act, NULL);
4451 sigaction(SIGHUP, &act, NULL);
4452 sigaction(SIGTERM, &act, NULL);
4453}
4454
4455#endif
4456
0824d6fc
FB
4457int main(int argc, char **argv)
4458{
67b915a5 4459#ifdef CONFIG_GDBSTUB
cfc3475a
PB
4460 int use_gdbstub;
4461 const char *gdbstub_port;
67b915a5 4462#endif
28c5af54 4463 uint32_t boot_devices_bitmap = 0;
e4bcb14c 4464 int i;
28c5af54 4465 int snapshot, linux_boot, net_boot;
7f7f9873 4466 const char *initrd_filename;
a20dd508 4467 const char *kernel_filename, *kernel_cmdline;
28c5af54 4468 const char *boot_devices = "";
313aa567 4469 DisplayState *ds = &display_state;
46d4767d 4470 int cyls, heads, secs, translation;
fd5f393a 4471 const char *net_clients[MAX_NET_CLIENTS];
7c9d8e07 4472 int nb_net_clients;
dc72ac14
AZ
4473 const char *bt_opts[MAX_BT_CMDLINE];
4474 int nb_bt_opts;
e4bcb14c 4475 int hda_index;
cd6f1169
FB
4476 int optind;
4477 const char *r, *optarg;
82c643ff 4478 CharDriverState *monitor_hd;
fd5f393a
PB
4479 const char *monitor_device;
4480 const char *serial_devices[MAX_SERIAL_PORTS];
8d11df9e 4481 int serial_device_index;
fd5f393a 4482 const char *parallel_devices[MAX_PARALLEL_PORTS];
6508fe59 4483 int parallel_device_index;
d63d307f 4484 const char *loadvm = NULL;
cc1daa40 4485 QEMUMachine *machine;
94fc95cd 4486 const char *cpu_model;
fd5f393a 4487 const char *usb_devices[MAX_USB_CMDLINE];
a594cfbf 4488 int usb_devices_index;
71e3ceb8 4489 int fds[2];
26a5f13b 4490 int tb_size;
93815bc2 4491 const char *pid_file = NULL;
41bd639b 4492 int autostart;
5bb7910a 4493 const char *incoming = NULL;
0bd48850
FB
4494
4495 LIST_INIT (&vm_change_state_head);
be995c27
FB
4496#ifndef _WIN32
4497 {
4498 struct sigaction act;
4499 sigfillset(&act.sa_mask);
4500 act.sa_flags = 0;
4501 act.sa_handler = SIG_IGN;
4502 sigaction(SIGPIPE, &act, NULL);
4503 }
3587d7e6
FB
4504#else
4505 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
a8e5ac33
FB
4506 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4507 QEMU to run on a single CPU */
4508 {
4509 HANDLE h;
4510 DWORD mask, smask;
4511 int i;
4512 h = GetCurrentProcess();
4513 if (GetProcessAffinityMask(h, &mask, &smask)) {
4514 for(i = 0; i < 32; i++) {
4515 if (mask & (1 << i))
4516 break;
4517 }
4518 if (i != 32) {
4519 mask = 1 << i;
4520 SetProcessAffinityMask(h, mask);
4521 }
4522 }
4523 }
67b915a5 4524#endif
be995c27 4525
cc1daa40
FB
4526 register_machines();
4527 machine = first_machine;
94fc95cd 4528 cpu_model = NULL;
fc01f7e7 4529 initrd_filename = NULL;
4fc5d071 4530 ram_size = 0;
313aa567 4531 vga_ram_size = VGA_RAM_SIZE;
67b915a5 4532#ifdef CONFIG_GDBSTUB
b4608c04 4533 use_gdbstub = 0;
c636bb66 4534 gdbstub_port = DEFAULT_GDBSTUB_PORT;
67b915a5 4535#endif
33e3963e 4536 snapshot = 0;
a20dd508 4537 nographic = 0;
4d3b6f6e 4538 curses = 0;
a20dd508
FB
4539 kernel_filename = NULL;
4540 kernel_cmdline = "";
c4b1fcc0 4541 cyls = heads = secs = 0;
46d4767d 4542 translation = BIOS_ATA_TRANSLATION_AUTO;
c60e08d9 4543 monitor_device = "vc";
c4b1fcc0 4544
c75a823c 4545 serial_devices[0] = "vc:80Cx24C";
8d11df9e 4546 for(i = 1; i < MAX_SERIAL_PORTS; i++)
fd5f393a 4547 serial_devices[i] = NULL;
8d11df9e 4548 serial_device_index = 0;
3b46e624 4549
c75a823c 4550 parallel_devices[0] = "vc:640x480";
6508fe59 4551 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
fd5f393a 4552 parallel_devices[i] = NULL;
6508fe59 4553 parallel_device_index = 0;
3b46e624 4554
a594cfbf 4555 usb_devices_index = 0;
3b46e624 4556
7c9d8e07 4557 nb_net_clients = 0;
dc72ac14 4558 nb_bt_opts = 0;
e4bcb14c
TS
4559 nb_drives = 0;
4560 nb_drives_opt = 0;
4561 hda_index = -1;
7c9d8e07
FB
4562
4563 nb_nics = 0;
3b46e624 4564
26a5f13b 4565 tb_size = 0;
41bd639b
BS
4566 autostart= 1;
4567
cd6f1169 4568 optind = 1;
0824d6fc 4569 for(;;) {
cd6f1169 4570 if (optind >= argc)
0824d6fc 4571 break;
cd6f1169
FB
4572 r = argv[optind];
4573 if (r[0] != '-') {
609497ab 4574 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
cd6f1169
FB
4575 } else {
4576 const QEMUOption *popt;
4577
4578 optind++;
dff5efc8
PB
4579 /* Treat --foo the same as -foo. */
4580 if (r[1] == '-')
4581 r++;
cd6f1169
FB
4582 popt = qemu_options;
4583 for(;;) {
4584 if (!popt->name) {
5fafdf24 4585 fprintf(stderr, "%s: invalid option -- '%s'\n",
cd6f1169
FB
4586 argv[0], r);
4587 exit(1);
4588 }
4589 if (!strcmp(popt->name, r + 1))
4590 break;
4591 popt++;
4592 }
4593 if (popt->flags & HAS_ARG) {
4594 if (optind >= argc) {
4595 fprintf(stderr, "%s: option '%s' requires an argument\n",
4596 argv[0], r);
4597 exit(1);
4598 }
4599 optarg = argv[optind++];
4600 } else {
4601 optarg = NULL;
4602 }
4603
4604 switch(popt->index) {
cc1daa40
FB
4605 case QEMU_OPTION_M:
4606 machine = find_machine(optarg);
4607 if (!machine) {
4608 QEMUMachine *m;
4609 printf("Supported machines are:\n");
4610 for(m = first_machine; m != NULL; m = m->next) {
4611 printf("%-10s %s%s\n",
5fafdf24 4612 m->name, m->desc,
cc1daa40
FB
4613 m == first_machine ? " (default)" : "");
4614 }
15f82208 4615 exit(*optarg != '?');
cc1daa40
FB
4616 }
4617 break;
94fc95cd
JM
4618 case QEMU_OPTION_cpu:
4619 /* hw initialization will check this */
15f82208 4620 if (*optarg == '?') {
c732abe2
JM
4621/* XXX: implement xxx_cpu_list for targets that still miss it */
4622#if defined(cpu_list)
4623 cpu_list(stdout, &fprintf);
94fc95cd 4624#endif
15f82208 4625 exit(0);
94fc95cd
JM
4626 } else {
4627 cpu_model = optarg;
4628 }
4629 break;
cd6f1169 4630 case QEMU_OPTION_initrd:
fc01f7e7
FB
4631 initrd_filename = optarg;
4632 break;
cd6f1169 4633 case QEMU_OPTION_hda:
e4bcb14c 4634 if (cyls == 0)
609497ab 4635 hda_index = drive_add(optarg, HD_ALIAS, 0);
e4bcb14c 4636 else
609497ab 4637 hda_index = drive_add(optarg, HD_ALIAS
e4bcb14c 4638 ",cyls=%d,heads=%d,secs=%d%s",
609497ab 4639 0, cyls, heads, secs,
e4bcb14c
TS
4640 translation == BIOS_ATA_TRANSLATION_LBA ?
4641 ",trans=lba" :
4642 translation == BIOS_ATA_TRANSLATION_NONE ?
4643 ",trans=none" : "");
4644 break;
cd6f1169 4645 case QEMU_OPTION_hdb:
cc1daa40
FB
4646 case QEMU_OPTION_hdc:
4647 case QEMU_OPTION_hdd:
609497ab 4648 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
fc01f7e7 4649 break;
e4bcb14c 4650 case QEMU_OPTION_drive:
609497ab 4651 drive_add(NULL, "%s", optarg);
e4bcb14c 4652 break;
3e3d5815 4653 case QEMU_OPTION_mtdblock:
609497ab 4654 drive_add(optarg, MTD_ALIAS);
3e3d5815 4655 break;
a1bb27b1 4656 case QEMU_OPTION_sd:
609497ab 4657 drive_add(optarg, SD_ALIAS);
a1bb27b1 4658 break;
86f55663 4659 case QEMU_OPTION_pflash:
609497ab 4660 drive_add(optarg, PFLASH_ALIAS);
86f55663 4661 break;
cd6f1169 4662 case QEMU_OPTION_snapshot:
33e3963e
FB
4663 snapshot = 1;
4664 break;
cd6f1169 4665 case QEMU_OPTION_hdachs:
330d0414 4666 {
330d0414
FB
4667 const char *p;
4668 p = optarg;
4669 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
4670 if (cyls < 1 || cyls > 16383)
4671 goto chs_fail;
330d0414
FB
4672 if (*p != ',')
4673 goto chs_fail;
4674 p++;
4675 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
4676 if (heads < 1 || heads > 16)
4677 goto chs_fail;
330d0414
FB
4678 if (*p != ',')
4679 goto chs_fail;
4680 p++;
4681 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
4682 if (secs < 1 || secs > 63)
4683 goto chs_fail;
4684 if (*p == ',') {
4685 p++;
4686 if (!strcmp(p, "none"))
4687 translation = BIOS_ATA_TRANSLATION_NONE;
4688 else if (!strcmp(p, "lba"))
4689 translation = BIOS_ATA_TRANSLATION_LBA;
4690 else if (!strcmp(p, "auto"))
4691 translation = BIOS_ATA_TRANSLATION_AUTO;
4692 else
4693 goto chs_fail;
4694 } else if (*p != '\0') {
c4b1fcc0 4695 chs_fail:
46d4767d
FB
4696 fprintf(stderr, "qemu: invalid physical CHS format\n");
4697 exit(1);
c4b1fcc0 4698 }
e4bcb14c 4699 if (hda_index != -1)
609497ab
AZ
4700 snprintf(drives_opt[hda_index].opt,
4701 sizeof(drives_opt[hda_index].opt),
4702 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4703 0, cyls, heads, secs,
e4bcb14c
TS
4704 translation == BIOS_ATA_TRANSLATION_LBA ?
4705 ",trans=lba" :
4706 translation == BIOS_ATA_TRANSLATION_NONE ?
4707 ",trans=none" : "");
330d0414
FB
4708 }
4709 break;
cd6f1169 4710 case QEMU_OPTION_nographic:
a20dd508
FB
4711 nographic = 1;
4712 break;
4d3b6f6e
AZ
4713#ifdef CONFIG_CURSES
4714 case QEMU_OPTION_curses:
4715 curses = 1;
4716 break;
4717#endif
a171fe39
AZ
4718 case QEMU_OPTION_portrait:
4719 graphic_rotate = 1;
4720 break;
cd6f1169 4721 case QEMU_OPTION_kernel:
a20dd508
FB
4722 kernel_filename = optarg;
4723 break;
cd6f1169 4724 case QEMU_OPTION_append:
a20dd508 4725 kernel_cmdline = optarg;
313aa567 4726 break;
cd6f1169 4727 case QEMU_OPTION_cdrom:
609497ab 4728 drive_add(optarg, CDROM_ALIAS);
36b486bb 4729 break;
cd6f1169 4730 case QEMU_OPTION_boot:
28c5af54
JM
4731 boot_devices = optarg;
4732 /* We just do some generic consistency checks */
4733 {
4734 /* Could easily be extended to 64 devices if needed */
60fe76f3 4735 const char *p;
28c5af54
JM
4736
4737 boot_devices_bitmap = 0;
4738 for (p = boot_devices; *p != '\0'; p++) {
4739 /* Allowed boot devices are:
4740 * a b : floppy disk drives
4741 * c ... f : IDE disk drives
4742 * g ... m : machine implementation dependant drives
4743 * n ... p : network devices
4744 * It's up to each machine implementation to check
4745 * if the given boot devices match the actual hardware
4746 * implementation and firmware features.
4747 */
4748 if (*p < 'a' || *p > 'q') {
4749 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4750 exit(1);
4751 }
4752 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4753 fprintf(stderr,
4754 "Boot device '%c' was given twice\n",*p);
4755 exit(1);
4756 }
4757 boot_devices_bitmap |= 1 << (*p - 'a');
4758 }
36b486bb
FB
4759 }
4760 break;
cd6f1169 4761 case QEMU_OPTION_fda:
cd6f1169 4762 case QEMU_OPTION_fdb:
609497ab 4763 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
c45886db 4764 break;
52ca8d6a
FB
4765#ifdef TARGET_I386
4766 case QEMU_OPTION_no_fd_bootchk:
4767 fd_bootchk = 0;
4768 break;
4769#endif
7c9d8e07
FB
4770 case QEMU_OPTION_net:
4771 if (nb_net_clients >= MAX_NET_CLIENTS) {
4772 fprintf(stderr, "qemu: too many network clients\n");
c4b1fcc0
FB
4773 exit(1);
4774 }
fd5f393a 4775 net_clients[nb_net_clients] = optarg;
7c9d8e07 4776 nb_net_clients++;
702c651c 4777 break;
c7f74643
FB
4778#ifdef CONFIG_SLIRP
4779 case QEMU_OPTION_tftp:
c7f74643 4780 tftp_prefix = optarg;
9bf05444 4781 break;
47d5d01a
TS
4782 case QEMU_OPTION_bootp:
4783 bootp_filename = optarg;
4784 break;
c94c8d64 4785#ifndef _WIN32
9d728e8c
FB
4786 case QEMU_OPTION_smb:
4787 net_slirp_smb(optarg);
4788 break;
c94c8d64 4789#endif
9bf05444 4790 case QEMU_OPTION_redir:
3b46e624 4791 net_slirp_redir(optarg);
9bf05444 4792 break;
c7f74643 4793#endif
dc72ac14
AZ
4794 case QEMU_OPTION_bt:
4795 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4796 fprintf(stderr, "qemu: too many bluetooth options\n");
4797 exit(1);
4798 }
4799 bt_opts[nb_bt_opts++] = optarg;
4800 break;
1d14ffa9 4801#ifdef HAS_AUDIO
1d14ffa9
FB
4802 case QEMU_OPTION_audio_help:
4803 AUD_help ();
4804 exit (0);
4805 break;
4806 case QEMU_OPTION_soundhw:
4807 select_soundhw (optarg);
4808 break;
4809#endif
cd6f1169 4810 case QEMU_OPTION_h:
15f82208 4811 help(0);
cd6f1169 4812 break;
00f82b8a
AJ
4813 case QEMU_OPTION_m: {
4814 uint64_t value;
4815 char *ptr;
4816
4817 value = strtoul(optarg, &ptr, 10);
4818 switch (*ptr) {
4819 case 0: case 'M': case 'm':
4820 value <<= 20;
4821 break;
4822 case 'G': case 'g':
4823 value <<= 30;
4824 break;
4825 default:
4826 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
cd6f1169
FB
4827 exit(1);
4828 }
00f82b8a
AJ
4829
4830 /* On 32-bit hosts, QEMU is limited by virtual address space */
4831 if (value > (2047 << 20)
4832#ifndef USE_KQEMU
4833 && HOST_LONG_BITS == 32
4834#endif
4835 ) {
4836 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4837 exit(1);
4838 }
4839 if (value != (uint64_t)(ram_addr_t)value) {
4840 fprintf(stderr, "qemu: ram size too large\n");
4841 exit(1);
4842 }
4843 ram_size = value;
cd6f1169 4844 break;
00f82b8a 4845 }
cd6f1169
FB
4846 case QEMU_OPTION_d:
4847 {
4848 int mask;
c7cd6a37 4849 const CPULogItem *item;
3b46e624 4850
cd6f1169
FB
4851 mask = cpu_str_to_log_mask(optarg);
4852 if (!mask) {
4853 printf("Log items (comma separated):\n");
f193c797
FB
4854 for(item = cpu_log_items; item->mask != 0; item++) {
4855 printf("%-10s %s\n", item->name, item->help);
4856 }
4857 exit(1);
cd6f1169
FB
4858 }
4859 cpu_set_log(mask);
f193c797 4860 }
cd6f1169 4861 break;
67b915a5 4862#ifdef CONFIG_GDBSTUB
cd6f1169
FB
4863 case QEMU_OPTION_s:
4864 use_gdbstub = 1;
4865 break;
4866 case QEMU_OPTION_p:
cfc3475a 4867 gdbstub_port = optarg;
cd6f1169 4868 break;
67b915a5 4869#endif
cd6f1169
FB
4870 case QEMU_OPTION_L:
4871 bios_dir = optarg;
4872 break;
1192dad8
JM
4873 case QEMU_OPTION_bios:
4874 bios_name = optarg;
4875 break;
cd6f1169 4876 case QEMU_OPTION_S:
3c07f8e8 4877 autostart = 0;
cd6f1169 4878 break;
3d11d0eb
FB
4879 case QEMU_OPTION_k:
4880 keyboard_layout = optarg;
4881 break;
ee22c2f7
FB
4882 case QEMU_OPTION_localtime:
4883 rtc_utc = 0;
4884 break;
3893c124 4885 case QEMU_OPTION_vga:
4886 select_vgahw (optarg);
1bfe856e 4887 break;
e9b137c2
FB
4888 case QEMU_OPTION_g:
4889 {
4890 const char *p;
4891 int w, h, depth;
4892 p = optarg;
4893 w = strtol(p, (char **)&p, 10);
4894 if (w <= 0) {
4895 graphic_error:
4896 fprintf(stderr, "qemu: invalid resolution or depth\n");
4897 exit(1);
4898 }
4899 if (*p != 'x')
4900 goto graphic_error;
4901 p++;
4902 h = strtol(p, (char **)&p, 10);
4903 if (h <= 0)
4904 goto graphic_error;
4905 if (*p == 'x') {
4906 p++;
4907 depth = strtol(p, (char **)&p, 10);
5fafdf24 4908 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
4909 depth != 24 && depth != 32)
4910 goto graphic_error;
4911 } else if (*p == '\0') {
4912 depth = graphic_depth;
4913 } else {
4914 goto graphic_error;
4915 }
3b46e624 4916
e9b137c2
FB
4917 graphic_width = w;
4918 graphic_height = h;
4919 graphic_depth = depth;
4920 }
4921 break;
20d8a3ed
TS
4922 case QEMU_OPTION_echr:
4923 {
4924 char *r;
4925 term_escape_char = strtol(optarg, &r, 0);
4926 if (r == optarg)
4927 printf("Bad argument to echr\n");
4928 break;
4929 }
82c643ff 4930 case QEMU_OPTION_monitor:
fd5f393a 4931 monitor_device = optarg;
82c643ff
FB
4932 break;
4933 case QEMU_OPTION_serial:
8d11df9e
FB
4934 if (serial_device_index >= MAX_SERIAL_PORTS) {
4935 fprintf(stderr, "qemu: too many serial ports\n");
4936 exit(1);
4937 }
fd5f393a 4938 serial_devices[serial_device_index] = optarg;
8d11df9e 4939 serial_device_index++;
82c643ff 4940 break;
6508fe59
FB
4941 case QEMU_OPTION_parallel:
4942 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
4943 fprintf(stderr, "qemu: too many parallel ports\n");
4944 exit(1);
4945 }
fd5f393a 4946 parallel_devices[parallel_device_index] = optarg;
6508fe59
FB
4947 parallel_device_index++;
4948 break;
d63d307f
FB
4949 case QEMU_OPTION_loadvm:
4950 loadvm = optarg;
4951 break;
4952 case QEMU_OPTION_full_screen:
4953 full_screen = 1;
4954 break;
667accab 4955#ifdef CONFIG_SDL
43523e93
TS
4956 case QEMU_OPTION_no_frame:
4957 no_frame = 1;
4958 break;
3780e197
TS
4959 case QEMU_OPTION_alt_grab:
4960 alt_grab = 1;
4961 break;
667accab
TS
4962 case QEMU_OPTION_no_quit:
4963 no_quit = 1;
4964 break;
4965#endif
f7cce898 4966 case QEMU_OPTION_pidfile:
93815bc2 4967 pid_file = optarg;
f7cce898 4968 break;
a09db21f
FB
4969#ifdef TARGET_I386
4970 case QEMU_OPTION_win2k_hack:
4971 win2k_install_hack = 1;
4972 break;
4973#endif
d993e026
FB
4974#ifdef USE_KQEMU
4975 case QEMU_OPTION_no_kqemu:
4976 kqemu_allowed = 0;
4977 break;
89bfc105
FB
4978 case QEMU_OPTION_kernel_kqemu:
4979 kqemu_allowed = 2;
4980 break;
7ba1e619
AL
4981#endif
4982#ifdef CONFIG_KVM
4983 case QEMU_OPTION_enable_kvm:
4984 kvm_allowed = 1;
4985#ifdef USE_KQEMU
4986 kqemu_allowed = 0;
4987#endif
4988 break;
d993e026 4989#endif
bb36d470
FB
4990 case QEMU_OPTION_usb:
4991 usb_enabled = 1;
4992 break;
a594cfbf
FB
4993 case QEMU_OPTION_usbdevice:
4994 usb_enabled = 1;
0d92ed30 4995 if (usb_devices_index >= MAX_USB_CMDLINE) {
a594cfbf
FB
4996 fprintf(stderr, "Too many USB devices\n");
4997 exit(1);
4998 }
fd5f393a 4999 usb_devices[usb_devices_index] = optarg;
a594cfbf
FB
5000 usb_devices_index++;
5001 break;
6a00d601
FB
5002 case QEMU_OPTION_smp:
5003 smp_cpus = atoi(optarg);
b2097003 5004 if (smp_cpus < 1) {
6a00d601
FB
5005 fprintf(stderr, "Invalid number of CPUs\n");
5006 exit(1);
5007 }
5008 break;
24236869 5009 case QEMU_OPTION_vnc:
73fc9742 5010 vnc_display = optarg;
24236869 5011 break;
6515b203
FB
5012 case QEMU_OPTION_no_acpi:
5013 acpi_enabled = 0;
5014 break;
d1beab82
FB
5015 case QEMU_OPTION_no_reboot:
5016 no_reboot = 1;
5017 break;
b2f76161
AJ
5018 case QEMU_OPTION_no_shutdown:
5019 no_shutdown = 1;
5020 break;
9467cd46
AZ
5021 case QEMU_OPTION_show_cursor:
5022 cursor_hide = 0;
5023 break;
8fcb1b90
BS
5024 case QEMU_OPTION_uuid:
5025 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5026 fprintf(stderr, "Fail to parse UUID string."
5027 " Wrong format.\n");
5028 exit(1);
5029 }
5030 break;
71e3ceb8
TS
5031 case QEMU_OPTION_daemonize:
5032 daemonize = 1;
5033 break;
9ae02555
TS
5034 case QEMU_OPTION_option_rom:
5035 if (nb_option_roms >= MAX_OPTION_ROMS) {
5036 fprintf(stderr, "Too many option ROMs\n");
5037 exit(1);
5038 }
5039 option_rom[nb_option_roms] = optarg;
5040 nb_option_roms++;
5041 break;
8e71621f
PB
5042 case QEMU_OPTION_semihosting:
5043 semihosting_enabled = 1;
5044 break;
c35734b2
TS
5045 case QEMU_OPTION_name:
5046 qemu_name = optarg;
5047 break;
66508601
BS
5048#ifdef TARGET_SPARC
5049 case QEMU_OPTION_prom_env:
5050 if (nb_prom_envs >= MAX_PROM_ENVS) {
5051 fprintf(stderr, "Too many prom variables\n");
5052 exit(1);
5053 }
5054 prom_envs[nb_prom_envs] = optarg;
5055 nb_prom_envs++;
5056 break;
2b8f2d41
AZ
5057#endif
5058#ifdef TARGET_ARM
5059 case QEMU_OPTION_old_param:
5060 old_param = 1;
05ebd537 5061 break;
66508601 5062#endif
f3dcfada
TS
5063 case QEMU_OPTION_clock:
5064 configure_alarms(optarg);
5065 break;
7e0af5d0
FB
5066 case QEMU_OPTION_startdate:
5067 {
5068 struct tm tm;
f6503059 5069 time_t rtc_start_date;
7e0af5d0 5070 if (!strcmp(optarg, "now")) {
f6503059 5071 rtc_date_offset = -1;
7e0af5d0
FB
5072 } else {
5073 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5074 &tm.tm_year,
5075 &tm.tm_mon,
5076 &tm.tm_mday,
5077 &tm.tm_hour,
5078 &tm.tm_min,
5079 &tm.tm_sec) == 6) {
5080 /* OK */
5081 } else if (sscanf(optarg, "%d-%d-%d",
5082 &tm.tm_year,
5083 &tm.tm_mon,
5084 &tm.tm_mday) == 3) {
5085 tm.tm_hour = 0;
5086 tm.tm_min = 0;
5087 tm.tm_sec = 0;
5088 } else {
5089 goto date_fail;
5090 }
5091 tm.tm_year -= 1900;
5092 tm.tm_mon--;
3c6b2088 5093 rtc_start_date = mktimegm(&tm);
7e0af5d0
FB
5094 if (rtc_start_date == -1) {
5095 date_fail:
5096 fprintf(stderr, "Invalid date format. Valid format are:\n"
5097 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5098 exit(1);
5099 }
f6503059 5100 rtc_date_offset = time(NULL) - rtc_start_date;
7e0af5d0
FB
5101 }
5102 }
5103 break;
26a5f13b
FB
5104 case QEMU_OPTION_tb_size:
5105 tb_size = strtol(optarg, NULL, 0);
5106 if (tb_size < 0)
5107 tb_size = 0;
5108 break;
2e70f6ef
PB
5109 case QEMU_OPTION_icount:
5110 use_icount = 1;
5111 if (strcmp(optarg, "auto") == 0) {
5112 icount_time_shift = -1;
5113 } else {
5114 icount_time_shift = strtol(optarg, NULL, 0);
5115 }
5116 break;
5bb7910a
AL
5117 case QEMU_OPTION_incoming:
5118 incoming = optarg;
5119 break;
cd6f1169 5120 }
0824d6fc
FB
5121 }
5122 }
330d0414 5123
7ba1e619
AL
5124#if defined(CONFIG_KVM) && defined(USE_KQEMU)
5125 if (kvm_allowed && kqemu_allowed) {
5126 fprintf(stderr,
5127 "You can not enable both KVM and kqemu at the same time\n");
5128 exit(1);
5129 }
5130#endif
5131
3d878caa 5132 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
b2097003
AL
5133 if (smp_cpus > machine->max_cpus) {
5134 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5135 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5136 machine->max_cpus);
5137 exit(1);
5138 }
5139
bc0129d9
AL
5140 if (nographic) {
5141 if (serial_device_index == 0)
5142 serial_devices[0] = "stdio";
5143 if (parallel_device_index == 0)
5144 parallel_devices[0] = "null";
5145 if (strncmp(monitor_device, "vc", 2) == 0)
5146 monitor_device = "stdio";
5147 }
5148
71e3ceb8 5149#ifndef _WIN32
71e3ceb8
TS
5150 if (daemonize) {
5151 pid_t pid;
5152
5153 if (pipe(fds) == -1)
5154 exit(1);
5155
5156 pid = fork();
5157 if (pid > 0) {
5158 uint8_t status;
5159 ssize_t len;
5160
5161 close(fds[1]);
5162
5163 again:
93815bc2
TS
5164 len = read(fds[0], &status, 1);
5165 if (len == -1 && (errno == EINTR))
5166 goto again;
5167
5168 if (len != 1)
5169 exit(1);
5170 else if (status == 1) {
5171 fprintf(stderr, "Could not acquire pidfile\n");
5172 exit(1);
5173 } else
5174 exit(0);
71e3ceb8 5175 } else if (pid < 0)
93815bc2 5176 exit(1);
71e3ceb8
TS
5177
5178 setsid();
5179
5180 pid = fork();
5181 if (pid > 0)
5182 exit(0);
5183 else if (pid < 0)
5184 exit(1);
5185
5186 umask(027);
71e3ceb8
TS
5187
5188 signal(SIGTSTP, SIG_IGN);
5189 signal(SIGTTOU, SIG_IGN);
5190 signal(SIGTTIN, SIG_IGN);
5191 }
5192#endif
5193
aa26bb2d 5194 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
93815bc2
TS
5195 if (daemonize) {
5196 uint8_t status = 1;
5197 write(fds[1], &status, 1);
5198 } else
5199 fprintf(stderr, "Could not acquire pid file\n");
5200 exit(1);
5201 }
5202
ff3fbb30
FB
5203#ifdef USE_KQEMU
5204 if (smp_cpus > 1)
5205 kqemu_allowed = 0;
5206#endif
a20dd508 5207 linux_boot = (kernel_filename != NULL);
7317b8ca 5208 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
6c41b272 5209
28c5af54 5210 if (!linux_boot && net_boot == 0 &&
f88e4b91 5211 !machine->nodisk_ok && nb_drives_opt == 0)
15f82208 5212 help(1);
0824d6fc 5213
f8d39c01
TS
5214 if (!linux_boot && *kernel_cmdline != '\0') {
5215 fprintf(stderr, "-append only allowed with -kernel option\n");
5216 exit(1);
5217 }
5218
5219 if (!linux_boot && initrd_filename != NULL) {
5220 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5221 exit(1);
5222 }
5223
96d30e48 5224 /* boot to floppy or the default cd if no hard disk defined yet */
28c5af54 5225 if (!boot_devices[0]) {
e4bcb14c 5226 boot_devices = "cad";
96d30e48 5227 }
b118d61e 5228 setvbuf(stdout, NULL, _IOLBF, 0);
3b46e624 5229
634fce96 5230 init_timers();
7183b4b4
AL
5231 if (init_timer_alarm() < 0) {
5232 fprintf(stderr, "could not initialize alarm timer\n");
5233 exit(1);
5234 }
2e70f6ef
PB
5235 if (use_icount && icount_time_shift < 0) {
5236 use_icount = 2;
5237 /* 125MIPS seems a reasonable initial guess at the guest speed.
5238 It will be corrected fairly quickly anyway. */
5239 icount_time_shift = 3;
5240 init_icount_adjust();
5241 }
634fce96 5242
fd1dff4b
FB
5243#ifdef _WIN32
5244 socket_init();
5245#endif
5246
7c9d8e07
FB
5247 /* init network clients */
5248 if (nb_net_clients == 0) {
5249 /* if no clients, we use a default config */
f441b28b
AL
5250 net_clients[nb_net_clients++] = "nic";
5251#ifdef CONFIG_SLIRP
5252 net_clients[nb_net_clients++] = "user";
5253#endif
c20709aa
FB
5254 }
5255
7c9d8e07 5256 for(i = 0;i < nb_net_clients; i++) {
9ad97e65 5257 if (net_client_parse(net_clients[i]) < 0)
7c9d8e07 5258 exit(1);
702c651c 5259 }
63a01ef8 5260 net_client_check();
f1510b2c 5261
eec85c2a 5262#ifdef TARGET_I386
ed494d87 5263 /* XXX: this should be moved in the PC machine instantiation code */
28c5af54
JM
5264 if (net_boot != 0) {
5265 int netroms = 0;
5266 for (i = 0; i < nb_nics && i < 4; i++) {
eec85c2a
TS
5267 const char *model = nd_table[i].model;
5268 char buf[1024];
28c5af54
JM
5269 if (net_boot & (1 << i)) {
5270 if (model == NULL)
5271 model = "ne2k_pci";
5272 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5273 if (get_image_size(buf) > 0) {
5274 if (nb_option_roms >= MAX_OPTION_ROMS) {
5275 fprintf(stderr, "Too many option ROMs\n");
5276 exit(1);
5277 }
5278 option_rom[nb_option_roms] = strdup(buf);
5279 nb_option_roms++;
5280 netroms++;
5281 }
5282 }
eec85c2a 5283 }
28c5af54 5284 if (netroms == 0) {
eec85c2a
TS
5285 fprintf(stderr, "No valid PXE rom found for network device\n");
5286 exit(1);
5287 }
eec85c2a
TS
5288 }
5289#endif
5290
dc72ac14
AZ
5291 /* init the bluetooth world */
5292 for (i = 0; i < nb_bt_opts; i++)
5293 if (bt_parse(bt_opts[i]))
5294 exit(1);
5295
0824d6fc 5296 /* init the memory */
7fb4fdcf
AZ
5297 phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
5298
5299 if (machine->ram_require & RAMSIZE_FIXED) {
5300 if (ram_size > 0) {
5301 if (ram_size < phys_ram_size) {
cd940061
AJ
5302 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
5303 machine->name, (unsigned long long) phys_ram_size);
7fb4fdcf
AZ
5304 exit(-1);
5305 }
5306
5307 phys_ram_size = ram_size;
5308 } else
5309 ram_size = phys_ram_size;
5310 } else {
4fc5d071 5311 if (ram_size == 0)
7fb4fdcf
AZ
5312 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5313
5314 phys_ram_size += ram_size;
5315 }
9ae02555 5316
d993e026 5317 phys_ram_base = qemu_vmalloc(phys_ram_size);
7f7f9873
FB
5318 if (!phys_ram_base) {
5319 fprintf(stderr, "Could not allocate physical memory\n");
0824d6fc
FB
5320 exit(1);
5321 }
5322
26a5f13b
FB
5323 /* init the dynamic translator */
5324 cpu_exec_init_all(tb_size * 1024 * 1024);
5325
5905b2e5 5326 bdrv_init();
c4b1fcc0 5327
e4bcb14c 5328 /* we always create the cdrom drive, even if no disk is there */
c4b1fcc0 5329
e4bcb14c 5330 if (nb_drives_opt < MAX_DRIVES)
609497ab 5331 drive_add(NULL, CDROM_ALIAS);
c4b1fcc0 5332
9d413d1d 5333 /* we always create at least one floppy */
33e3963e 5334
e4bcb14c 5335 if (nb_drives_opt < MAX_DRIVES)
609497ab 5336 drive_add(NULL, FD_ALIAS, 0);
86f55663 5337
9d413d1d
AZ
5338 /* we always create one sd slot, even if no card is in it */
5339
5340 if (nb_drives_opt < MAX_DRIVES)
609497ab 5341 drive_add(NULL, SD_ALIAS);
9d413d1d 5342
e4bcb14c
TS
5343 /* open the virtual block devices */
5344
5345 for(i = 0; i < nb_drives_opt; i++)
609497ab 5346 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
e4bcb14c 5347 exit(1);
3e3d5815 5348
c88676f8 5349 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
475e4277 5350 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
8a7ddc38 5351
313aa567 5352 /* terminal init */
740733bb 5353 memset(&display_state, 0, sizeof(display_state));
a20dd508 5354 if (nographic) {
4d3b6f6e
AZ
5355 if (curses) {
5356 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5357 exit(1);
5358 }
2ff89790
TS
5359 /* nearly nothing to do */
5360 dumb_display_init(ds);
73fc9742 5361 } else if (vnc_display != NULL) {
71cab5ca
TS
5362 vnc_display_init(ds);
5363 if (vnc_display_open(ds, vnc_display) < 0)
5364 exit(1);
4d3b6f6e
AZ
5365 } else
5366#if defined(CONFIG_CURSES)
5367 if (curses) {
5368 curses_display_init(ds, full_screen);
5369 } else
5370#endif
5371 {
5b0753e0 5372#if defined(CONFIG_SDL)
43523e93 5373 sdl_display_init(ds, full_screen, no_frame);
5b0753e0
FB
5374#elif defined(CONFIG_COCOA)
5375 cocoa_display_init(ds, full_screen);
67276f53
PB
5376#else
5377 dumb_display_init(ds);
313aa567
FB
5378#endif
5379 }
0824d6fc 5380
5b08fc10
AL
5381#ifndef _WIN32
5382 /* must be after terminal init, SDL library changes signal handlers */
5383 termsig_setup();
5384#endif
5385
20d8a3ed
TS
5386 /* Maintain compatibility with multiple stdio monitors */
5387 if (!strcmp(monitor_device,"stdio")) {
5388 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
fd5f393a
PB
5389 const char *devname = serial_devices[i];
5390 if (devname && !strcmp(devname,"mon:stdio")) {
5391 monitor_device = NULL;
20d8a3ed 5392 break;
fd5f393a
PB
5393 } else if (devname && !strcmp(devname,"stdio")) {
5394 monitor_device = NULL;
5395 serial_devices[i] = "mon:stdio";
20d8a3ed
TS
5396 break;
5397 }
5398 }
5399 }
fd5f393a 5400 if (monitor_device) {
5ccfae10 5401 monitor_hd = qemu_chr_open("monitor", monitor_device);
20d8a3ed
TS
5402 if (!monitor_hd) {
5403 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5404 exit(1);
5405 }
5406 monitor_init(monitor_hd, !nographic);
82c643ff 5407 }
82c643ff 5408
8d11df9e 5409 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
c03b0f0f 5410 const char *devname = serial_devices[i];
fd5f393a 5411 if (devname && strcmp(devname, "none")) {
5ccfae10
AL
5412 char label[32];
5413 snprintf(label, sizeof(label), "serial%d", i);
5414 serial_hds[i] = qemu_chr_open(label, devname);
8d11df9e 5415 if (!serial_hds[i]) {
5fafdf24 5416 fprintf(stderr, "qemu: could not open serial device '%s'\n",
c03b0f0f 5417 devname);
8d11df9e
FB
5418 exit(1);
5419 }
af3a9031 5420 if (strstart(devname, "vc", 0))
7ba1260a 5421 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
8d11df9e 5422 }
82c643ff 5423 }
82c643ff 5424
6508fe59 5425 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
c03b0f0f 5426 const char *devname = parallel_devices[i];
fd5f393a 5427 if (devname && strcmp(devname, "none")) {
5ccfae10
AL
5428 char label[32];
5429 snprintf(label, sizeof(label), "parallel%d", i);
5430 parallel_hds[i] = qemu_chr_open(label, devname);
6508fe59 5431 if (!parallel_hds[i]) {
5fafdf24 5432 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
c03b0f0f 5433 devname);
6508fe59
FB
5434 exit(1);
5435 }
af3a9031 5436 if (strstart(devname, "vc", 0))
7ba1260a 5437 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
6508fe59
FB
5438 }
5439 }
5440
7ba1e619
AL
5441 if (kvm_enabled()) {
5442 int ret;
5443
5444 ret = kvm_init(smp_cpus);
5445 if (ret < 0) {
5446 fprintf(stderr, "failed to initialize KVM\n");
5447 exit(1);
5448 }
5449 }
5450
b881c2c6 5451 machine->init(ram_size, vga_ram_size, boot_devices, ds,
94fc95cd 5452 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
73332e5c 5453
0d92ed30
PB
5454 /* init USB devices */
5455 if (usb_enabled) {
5456 for(i = 0; i < usb_devices_index; i++) {
5457 if (usb_device_add(usb_devices[i]) < 0) {
5458 fprintf(stderr, "Warning: could not add USB device %s\n",
5459 usb_devices[i]);
5460 }
5461 }
5462 }
5463
740733bb
TS
5464 if (display_state.dpy_refresh) {
5465 display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
5466 qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
5467 }
7f7f9873 5468
67b915a5 5469#ifdef CONFIG_GDBSTUB
b4608c04 5470 if (use_gdbstub) {
c636bb66
FB
5471 /* XXX: use standard host:port notation and modify options
5472 accordingly. */
cfc3475a
PB
5473 if (gdbserver_start(gdbstub_port) < 0) {
5474 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
c636bb66 5475 gdbstub_port);
8a7ddc38 5476 exit(1);
8a7ddc38 5477 }
45669e00 5478 }
67b915a5 5479#endif
45669e00 5480
d63d307f 5481 if (loadvm)
faea38e7 5482 do_loadvm(loadvm);
d63d307f 5483
5bb7910a
AL
5484 if (incoming) {
5485 autostart = 0; /* fixme how to deal with -daemonize */
5486 qemu_start_incoming_migration(incoming);
5487 }
5488
67b915a5 5489 {
5905b2e5 5490 /* XXX: simplify init */
83ab7950 5491 read_passwords();
3c07f8e8 5492 if (autostart) {
5905b2e5
FB
5493 vm_start();
5494 }
0824d6fc 5495 }
ffd843bc 5496
71e3ceb8
TS
5497 if (daemonize) {
5498 uint8_t status = 0;
5499 ssize_t len;
5500 int fd;
5501
5502 again1:
5503 len = write(fds[1], &status, 1);
5504 if (len == -1 && (errno == EINTR))
5505 goto again1;
5506
5507 if (len != 1)
5508 exit(1);
5509
bd54b863 5510 chdir("/");
aeb30be6 5511 TFR(fd = open("/dev/null", O_RDWR));
71e3ceb8
TS
5512 if (fd == -1)
5513 exit(1);
5514
5515 dup2(fd, 0);
5516 dup2(fd, 1);
5517 dup2(fd, 2);
5518
5519 close(fd);
5520 }
5521
8a7ddc38 5522 main_loop();
40c3bac3 5523 quit_timers();
63a01ef8 5524 net_cleanup();
b46a8906 5525
0824d6fc
FB
5526 return 0;
5527}