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