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