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