blob: 2fbea375ab3b222a1f1e2dc54de07074812afad8 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * 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.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
blueswir1179a2c12009-03-08 08:23:32 +000032/* Needed early for HOST_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
aliguori08585322009-02-27 22:09:45 +000037#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
47#if defined(__NetBSD__)
48#include <net/if_tap.h>
49#endif
50#ifdef __linux__
51#include <linux/if_tun.h>
52#endif
53#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000054#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000055#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000056#include <sys/select.h>
blueswir1179a2c12009-03-08 08:23:32 +000057#ifdef HOST_BSD
bellard7d3505c2004-05-12 19:32:15 +000058#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000059#if defined(__FreeBSD__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000060#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000061#else
62#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000063#endif
ths5c40d2b2007-06-23 16:03:36 +000064#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
65#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000066#else
blueswir1223f0d72008-09-30 18:12:18 +000067#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000068#include <pty.h>
69#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000070#include <linux/rtc.h>
Andi Kleen18894652009-07-02 09:34:17 +020071#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000072
73/* For the benefit of older linux systems which don't supply it,
74 we use a local copy of hpet.h. */
75/* #include <linux/hpet.h> */
76#include "hpet.h"
77
bellarde57a8c02005-11-10 23:58:52 +000078#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000079#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000080#endif
81#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000082#include <sys/stat.h>
83#include <sys/ethernet.h>
84#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000085#include <netinet/arp.h>
86#include <netinet/in.h>
87#include <netinet/in_systm.h>
88#include <netinet/ip.h>
89#include <netinet/ip_icmp.h> // must come after ip.h
90#include <netinet/udp.h>
91#include <netinet/tcp.h>
92#include <net/if.h>
93#include <syslog.h>
94#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000095#endif
bellard7d3505c2004-05-12 19:32:15 +000096#endif
bellardec530c82006-04-25 22:36:06 +000097#endif
bellard67b915a2004-03-31 23:37:16 +000098
blueswir19892fbf2008-08-24 10:34:20 +000099#if defined(__OpenBSD__)
100#include <util.h>
101#endif
102
ths8a16d272008-07-19 09:56:24 +0000103#if defined(CONFIG_VDE)
104#include <libvdeplug.h>
105#endif
106
bellard67b915a2004-03-31 23:37:16 +0000107#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000108#include <windows.h>
bellard7d3505c2004-05-12 19:32:15 +0000109#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000110#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000111#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000112#define getopt_long_only getopt_long
113#define memalign(align, size) malloc(size)
114#endif
115
bellard73332e52004-04-04 20:22:28 +0000116#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200117#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200118#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000119int qemu_main(int argc, char **argv, char **envp);
120int main(int argc, char **argv)
121{
Stefan Weil59a36a22009-06-18 20:11:03 +0200122 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000123}
124#undef main
125#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000126#endif
bellard73332e52004-04-04 20:22:28 +0000127#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000128
bellard5b0753e2005-03-01 21:37:28 +0000129#ifdef CONFIG_COCOA
130#undef main
131#define main qemu_main
132#endif /* CONFIG_COCOA */
133
blueswir1511d2b12009-03-07 15:32:56 +0000134#include "hw/hw.h"
135#include "hw/boards.h"
136#include "hw/usb.h"
137#include "hw/pcmcia.h"
138#include "hw/pc.h"
139#include "hw/audiodev.h"
140#include "hw/isa.h"
141#include "hw/baum.h"
142#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100143#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000144#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000145#include "hw/xen.h"
aurel325ef4efa2009-03-10 21:43:35 +0000146#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000147#include "net.h"
148#include "monitor.h"
149#include "console.h"
150#include "sysemu.h"
151#include "gdbstub.h"
152#include "qemu-timer.h"
153#include "qemu-char.h"
154#include "cache-utils.h"
155#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000156#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000157#include "audio/audio.h"
158#include "migration.h"
159#include "kvm.h"
160#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200161#include "qemu-option.h"
blueswir1511d2b12009-03-07 15:32:56 +0000162
bellard0824d6f2003-06-24 13:42:40 +0000163#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000164
bellard8a7ddc32004-03-31 19:00:16 +0000165#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000166
blueswir1511d2b12009-03-07 15:32:56 +0000167#include "qemu_socket.h"
168
Jan Kiszkad918f232009-06-24 14:42:30 +0200169#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000170
blueswir19dc63a12008-10-04 07:25:46 +0000171//#define DEBUG_NET
172//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000173
bellard1bfe8562004-07-08 21:17:50 +0000174#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000175
pbrook0d92ed32006-05-21 16:30:15 +0000176/* Max number of USB devices that can be specified on the commandline. */
177#define MAX_USB_CMDLINE 8
178
balrogdc72ac12008-11-09 00:04:26 +0000179/* Max number of bluetooth switches on the commandline. */
180#define MAX_BT_CMDLINE 10
181
Paul Brook5cea8592009-05-30 00:52:44 +0100182static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000183const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000184/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000185 to store the VM snapshots */
thse4bcb142007-12-02 04:51:10 +0000186DriveInfo drives_table[MAX_DRIVES+1];
187int nb_drives;
malccb5a7aa2008-09-28 00:42:12 +0000188enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000189static DisplayState *display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500190DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000191const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000192int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000193ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000194int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000195NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000196int vm_running;
aliguoric0f4ce72009-03-05 23:01:01 +0000197static int autostart;
balrogf6503052008-02-17 11:42:19 +0000198static int rtc_utc = 1;
199static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000200int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000201int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000202int vmsvga_enabled = 0;
aliguori94909d92009-04-22 15:19:53 +0000203int xenfb_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000204#ifdef TARGET_SPARC
205int graphic_width = 1024;
206int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000207int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000208#else
bellard1bfe8562004-07-08 21:17:50 +0000209int graphic_width = 800;
210int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000211int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000212#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000213static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000214#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000215static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000216#endif
ths667acca2006-12-11 02:08:05 +0000217int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000218CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000219CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000220CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000221#ifdef TARGET_I386
222int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000223int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000224#endif
bellardbb36d472005-11-05 14:22:28 +0000225int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000226int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000227int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000228const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000229int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000230int no_hpet = 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +0200231int virtio_balloon = 1;
232const char *virtio_balloon_devaddr;
bellard52ca8d62006-06-14 16:03:05 +0000233int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000234int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000235int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000236int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000237int graphic_rotate = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000238#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000239int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000240#endif
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100241WatchdogTimerModel *watchdog = NULL;
242int watchdog_action = WDT_RESET;
ths9ae02552007-01-05 17:39:04 +0000243const char *option_rom[MAX_OPTION_ROMS];
244int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000245int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000246#ifdef TARGET_ARM
247int old_param = 0;
248#endif
thsc35734b2007-03-19 15:17:08 +0000249const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000250int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000251#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000252unsigned int nb_prom_envs = 0;
253const char *prom_envs[MAX_PROM_ENVS];
254#endif
aliguoriec691c82009-02-11 15:20:37 +0000255int nb_drives_opt;
256struct drive_opt drives_opt[MAX_DRIVES];
Jan Kiszka95387492009-07-02 00:19:02 +0200257int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000258
aliguori268a3622009-04-21 22:30:27 +0000259int nb_numa_nodes;
260uint64_t node_mem[MAX_NODES];
261uint64_t node_cpumask[MAX_NODES];
262
balrogee5605e2007-12-03 03:01:40 +0000263static CPUState *cur_cpu;
264static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000265static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000266/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000267static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000268/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000269#define MAX_ICOUNT_SHIFT 10
270/* Compensate for varying guest execution speed. */
271static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000272static QEMUTimer *icount_rt_timer;
273static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000274static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000275
blueswir18fcb1b92008-09-18 18:29:08 +0000276uint8_t qemu_uuid[16];
277
Jan Kiszka76e30d02009-07-02 00:19:02 +0200278static QEMUBootSetHandler *boot_set_handler;
279static void *boot_set_opaque;
280
bellard0824d6f2003-06-24 13:42:40 +0000281/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000282/* x86 ISA bus support */
283
284target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000285PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000286
bellard0824d6f2003-06-24 13:42:40 +0000287/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000288void hw_error(const char *fmt, ...)
289{
290 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000291 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000292
293 va_start(ap, fmt);
294 fprintf(stderr, "qemu: hardware error: ");
295 vfprintf(stderr, fmt, ap);
296 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000297 for(env = first_cpu; env != NULL; env = env->next_cpu) {
298 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000299#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000300 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000301#else
bellard6a00d602005-11-21 23:25:50 +0000302 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000303#endif
bellard6a00d602005-11-21 23:25:50 +0000304 }
bellard0824d6f2003-06-24 13:42:40 +0000305 va_end(ap);
306 abort();
307}
Andi Kleen18894652009-07-02 09:34:17 +0200308
309static void set_proc_name(const char *s)
310{
311#ifdef __linux__
312 char name[16];
313 if (!s)
314 return;
315 name[sizeof(name) - 1] = 0;
316 strncpy(name, s, sizeof(name));
317 /* Could rewrite argv[0] too, but that's a bit more complicated.
318 This simple way is enough for `top'. */
319 prctl(PR_SET_NAME, name);
320#endif
321}
aliguoridf751fa2008-12-04 20:19:35 +0000322
323/***************/
324/* ballooning */
325
326static QEMUBalloonEvent *qemu_balloon_event;
327void *qemu_balloon_event_opaque;
328
329void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
330{
331 qemu_balloon_event = func;
332 qemu_balloon_event_opaque = opaque;
333}
334
335void qemu_balloon(ram_addr_t target)
336{
337 if (qemu_balloon_event)
338 qemu_balloon_event(qemu_balloon_event_opaque, target);
339}
340
341ram_addr_t qemu_balloon_status(void)
342{
343 if (qemu_balloon_event)
344 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
345 return 0;
346}
bellard0824d6f2003-06-24 13:42:40 +0000347
bellard8a7ddc32004-03-31 19:00:16 +0000348/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000349/* keyboard/mouse */
350
351static QEMUPutKBDEvent *qemu_put_kbd_event;
352static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000353static QEMUPutMouseEntry *qemu_put_mouse_event_head;
354static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000355
356void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
357{
358 qemu_put_kbd_event_opaque = opaque;
359 qemu_put_kbd_event = func;
360}
361
ths455204e2007-01-05 16:42:13 +0000362QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
363 void *opaque, int absolute,
364 const char *name)
bellard63066f42004-06-03 18:45:02 +0000365{
ths455204e2007-01-05 16:42:13 +0000366 QEMUPutMouseEntry *s, *cursor;
367
368 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000369
370 s->qemu_put_mouse_event = func;
371 s->qemu_put_mouse_event_opaque = opaque;
372 s->qemu_put_mouse_event_absolute = absolute;
373 s->qemu_put_mouse_event_name = qemu_strdup(name);
374 s->next = NULL;
375
376 if (!qemu_put_mouse_event_head) {
377 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
378 return s;
379 }
380
381 cursor = qemu_put_mouse_event_head;
382 while (cursor->next != NULL)
383 cursor = cursor->next;
384
385 cursor->next = s;
386 qemu_put_mouse_event_current = s;
387
388 return s;
389}
390
391void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
392{
393 QEMUPutMouseEntry *prev = NULL, *cursor;
394
395 if (!qemu_put_mouse_event_head || entry == NULL)
396 return;
397
398 cursor = qemu_put_mouse_event_head;
399 while (cursor != NULL && cursor != entry) {
400 prev = cursor;
401 cursor = cursor->next;
402 }
403
404 if (cursor == NULL) // does not exist or list empty
405 return;
406 else if (prev == NULL) { // entry is head
407 qemu_put_mouse_event_head = cursor->next;
408 if (qemu_put_mouse_event_current == entry)
409 qemu_put_mouse_event_current = cursor->next;
410 qemu_free(entry->qemu_put_mouse_event_name);
411 qemu_free(entry);
412 return;
413 }
414
415 prev->next = entry->next;
416
417 if (qemu_put_mouse_event_current == entry)
418 qemu_put_mouse_event_current = prev;
419
420 qemu_free(entry->qemu_put_mouse_event_name);
421 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000422}
423
424void kbd_put_keycode(int keycode)
425{
426 if (qemu_put_kbd_event) {
427 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
428 }
429}
430
431void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
432{
ths455204e2007-01-05 16:42:13 +0000433 QEMUPutMouseEvent *mouse_event;
434 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000435 int width;
ths455204e2007-01-05 16:42:13 +0000436
437 if (!qemu_put_mouse_event_current) {
438 return;
439 }
440
441 mouse_event =
442 qemu_put_mouse_event_current->qemu_put_mouse_event;
443 mouse_event_opaque =
444 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
445
446 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000447 if (graphic_rotate) {
448 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
449 width = 0x7fff;
450 else
aurel32b94ed572008-03-10 19:34:27 +0000451 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000452 mouse_event(mouse_event_opaque,
453 width - dy, dx, dz, buttons_state);
454 } else
455 mouse_event(mouse_event_opaque,
456 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000457 }
458}
459
bellard09b26c52006-04-12 21:09:08 +0000460int kbd_mouse_is_absolute(void)
461{
ths455204e2007-01-05 16:42:13 +0000462 if (!qemu_put_mouse_event_current)
463 return 0;
464
465 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
466}
467
aliguori376253e2009-03-05 23:01:23 +0000468void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000469{
470 QEMUPutMouseEntry *cursor;
471 int index = 0;
472
473 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000474 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000475 return;
476 }
477
aliguori376253e2009-03-05 23:01:23 +0000478 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000479 cursor = qemu_put_mouse_event_head;
480 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000481 monitor_printf(mon, "%c Mouse #%d: %s\n",
482 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
483 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000484 index++;
485 cursor = cursor->next;
486 }
487}
488
aliguori376253e2009-03-05 23:01:23 +0000489void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000490{
491 QEMUPutMouseEntry *cursor;
492 int i = 0;
493
494 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000495 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000496 return;
497 }
498
499 cursor = qemu_put_mouse_event_head;
500 while (cursor != NULL && index != i) {
501 i++;
502 cursor = cursor->next;
503 }
504
505 if (cursor != NULL)
506 qemu_put_mouse_event_current = cursor;
507 else
aliguori376253e2009-03-05 23:01:23 +0000508 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000509}
510
bellard87858c82003-06-27 12:01:39 +0000511/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000512uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000513{
514 union {
515 uint64_t ll;
516 struct {
517#ifdef WORDS_BIGENDIAN
518 uint32_t high, low;
519#else
520 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000521#endif
bellard87858c82003-06-27 12:01:39 +0000522 } l;
523 } u, res;
524 uint64_t rl, rh;
525
526 u.ll = a;
527 rl = (uint64_t)u.l.low * (uint64_t)b;
528 rh = (uint64_t)u.l.high * (uint64_t)b;
529 rh += (rl >> 32);
530 res.l.high = rh / c;
531 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
532 return res.ll;
533}
534
bellard1dce7c32006-07-13 23:20:22 +0000535/***********************************************************/
536/* real time host monotonic timer */
537
538#define QEMU_TIMER_BASE 1000000000LL
539
540#ifdef WIN32
541
542static int64_t clock_freq;
543
544static void init_get_clock(void)
545{
bellarda8e5ac32006-07-14 09:36:13 +0000546 LARGE_INTEGER freq;
547 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000548 ret = QueryPerformanceFrequency(&freq);
549 if (ret == 0) {
550 fprintf(stderr, "Could not calibrate ticks\n");
551 exit(1);
552 }
553 clock_freq = freq.QuadPart;
554}
555
556static int64_t get_clock(void)
557{
558 LARGE_INTEGER ti;
559 QueryPerformanceCounter(&ti);
560 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
561}
562
563#else
564
565static int use_rt_clock;
566
567static void init_get_clock(void)
568{
569 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000570#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
571 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000572 {
573 struct timespec ts;
574 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
575 use_rt_clock = 1;
576 }
577 }
578#endif
579}
580
581static int64_t get_clock(void)
582{
blueswir1c5e97232009-03-07 20:06:23 +0000583#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
584 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000585 if (use_rt_clock) {
586 struct timespec ts;
587 clock_gettime(CLOCK_MONOTONIC, &ts);
588 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000589 } else
bellard1dce7c32006-07-13 23:20:22 +0000590#endif
591 {
592 /* XXX: using gettimeofday leads to problems if the date
593 changes, so it should be avoided. */
594 struct timeval tv;
595 gettimeofday(&tv, NULL);
596 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
597 }
598}
bellard1dce7c32006-07-13 23:20:22 +0000599#endif
600
pbrook2e70f6e2008-06-29 01:03:05 +0000601/* Return the virtual CPU time, based on the instruction counter. */
602static int64_t cpu_get_icount(void)
603{
604 int64_t icount;
605 CPUState *env = cpu_single_env;;
606 icount = qemu_icount;
607 if (env) {
608 if (!can_do_io(env))
609 fprintf(stderr, "Bad clock read\n");
610 icount -= (env->icount_decr.u16.low + env->icount_extra);
611 }
612 return qemu_icount_bias + (icount << icount_time_shift);
613}
614
bellard1dce7c32006-07-13 23:20:22 +0000615/***********************************************************/
616/* guest cycle counter */
617
618static int64_t cpu_ticks_prev;
619static int64_t cpu_ticks_offset;
620static int64_t cpu_clock_offset;
621static int cpu_ticks_enabled;
622
623/* return the host CPU cycle counter and handle stop/restart */
624int64_t cpu_get_ticks(void)
625{
pbrook2e70f6e2008-06-29 01:03:05 +0000626 if (use_icount) {
627 return cpu_get_icount();
628 }
bellard1dce7c32006-07-13 23:20:22 +0000629 if (!cpu_ticks_enabled) {
630 return cpu_ticks_offset;
631 } else {
632 int64_t ticks;
633 ticks = cpu_get_real_ticks();
634 if (cpu_ticks_prev > ticks) {
635 /* Note: non increasing ticks may happen if the host uses
636 software suspend */
637 cpu_ticks_offset += cpu_ticks_prev - ticks;
638 }
639 cpu_ticks_prev = ticks;
640 return ticks + cpu_ticks_offset;
641 }
642}
643
644/* return the host CPU monotonic timer and handle stop/restart */
645static int64_t cpu_get_clock(void)
646{
647 int64_t ti;
648 if (!cpu_ticks_enabled) {
649 return cpu_clock_offset;
650 } else {
651 ti = get_clock();
652 return ti + cpu_clock_offset;
653 }
654}
655
656/* enable cpu_get_ticks() */
657void cpu_enable_ticks(void)
658{
659 if (!cpu_ticks_enabled) {
660 cpu_ticks_offset -= cpu_get_real_ticks();
661 cpu_clock_offset -= get_clock();
662 cpu_ticks_enabled = 1;
663 }
664}
665
666/* disable cpu_get_ticks() : the clock is stopped. You must not call
667 cpu_get_ticks() after that. */
668void cpu_disable_ticks(void)
669{
670 if (cpu_ticks_enabled) {
671 cpu_ticks_offset = cpu_get_ticks();
672 cpu_clock_offset = cpu_get_clock();
673 cpu_ticks_enabled = 0;
674 }
675}
676
677/***********************************************************/
678/* timers */
ths5fafdf22007-09-16 21:08:06 +0000679
bellard8a7ddc32004-03-31 19:00:16 +0000680#define QEMU_TIMER_REALTIME 0
681#define QEMU_TIMER_VIRTUAL 1
682
683struct QEMUClock {
684 int type;
685 /* XXX: add frequency */
686};
687
688struct QEMUTimer {
689 QEMUClock *clock;
690 int64_t expire_time;
691 QEMUTimerCB *cb;
692 void *opaque;
693 struct QEMUTimer *next;
694};
695
thsc8994012007-08-19 21:56:03 +0000696struct qemu_alarm_timer {
697 char const *name;
thsefe75412007-08-24 01:36:32 +0000698 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000699
700 int (*start)(struct qemu_alarm_timer *t);
701 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000702 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000703 void *priv;
704};
705
thsefe75412007-08-24 01:36:32 +0000706#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000707#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000708
709static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
710{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200711 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000712}
713
714static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
715{
716 if (!alarm_has_dynticks(t))
717 return;
718
719 t->rearm(t);
720}
721
722/* TODO: MIN_TIMER_REARM_US should be optimized */
723#define MIN_TIMER_REARM_US 250
724
thsc8994012007-08-19 21:56:03 +0000725static struct qemu_alarm_timer *alarm_timer;
726
727#ifdef _WIN32
728
729struct qemu_alarm_win32 {
730 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000731 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000732} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000733
734static int win32_start_timer(struct qemu_alarm_timer *t);
735static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000736static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000737
738#else
739
740static int unix_start_timer(struct qemu_alarm_timer *t);
741static void unix_stop_timer(struct qemu_alarm_timer *t);
742
ths231c6582007-08-26 17:29:15 +0000743#ifdef __linux__
744
thsefe75412007-08-24 01:36:32 +0000745static int dynticks_start_timer(struct qemu_alarm_timer *t);
746static void dynticks_stop_timer(struct qemu_alarm_timer *t);
747static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
748
thsc40ec5a2007-08-19 22:09:40 +0000749static int hpet_start_timer(struct qemu_alarm_timer *t);
750static void hpet_stop_timer(struct qemu_alarm_timer *t);
751
thsc8994012007-08-19 21:56:03 +0000752static int rtc_start_timer(struct qemu_alarm_timer *t);
753static void rtc_stop_timer(struct qemu_alarm_timer *t);
754
thsefe75412007-08-24 01:36:32 +0000755#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000756
757#endif /* _WIN32 */
758
pbrook2e70f6e2008-06-29 01:03:05 +0000759/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000760 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000761 When the guest is idle real and virtual time will be aligned in
762 the IO wait loop. */
763#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
764
765static void icount_adjust(void)
766{
767 int64_t cur_time;
768 int64_t cur_icount;
769 int64_t delta;
770 static int64_t last_delta;
771 /* If the VM is not running, then do nothing. */
772 if (!vm_running)
773 return;
774
775 cur_time = cpu_get_clock();
776 cur_icount = qemu_get_clock(vm_clock);
777 delta = cur_icount - cur_time;
778 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
779 if (delta > 0
780 && last_delta + ICOUNT_WOBBLE < delta * 2
781 && icount_time_shift > 0) {
782 /* The guest is getting too far ahead. Slow time down. */
783 icount_time_shift--;
784 }
785 if (delta < 0
786 && last_delta - ICOUNT_WOBBLE > delta * 2
787 && icount_time_shift < MAX_ICOUNT_SHIFT) {
788 /* The guest is getting too far behind. Speed time up. */
789 icount_time_shift++;
790 }
791 last_delta = delta;
792 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
793}
794
795static void icount_adjust_rt(void * opaque)
796{
797 qemu_mod_timer(icount_rt_timer,
798 qemu_get_clock(rt_clock) + 1000);
799 icount_adjust();
800}
801
802static void icount_adjust_vm(void * opaque)
803{
804 qemu_mod_timer(icount_vm_timer,
805 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
806 icount_adjust();
807}
808
809static void init_icount_adjust(void)
810{
811 /* Have both realtime and virtual time triggers for speed adjustment.
812 The realtime trigger catches emulated time passing too slowly,
813 the virtual time trigger catches emulated time passing too fast.
814 Realtime triggers occur even when idle, so use them less frequently
815 than VM triggers. */
816 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
817 qemu_mod_timer(icount_rt_timer,
818 qemu_get_clock(rt_clock) + 1000);
819 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
820 qemu_mod_timer(icount_vm_timer,
821 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
822}
823
thsc8994012007-08-19 21:56:03 +0000824static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000825#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000826#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000827 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
828 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000829 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000830 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000831 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000832 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000833#endif
thsefe75412007-08-24 01:36:32 +0000834 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000835#else
thsefe75412007-08-24 01:36:32 +0000836 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
837 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
838 {"win32", 0, win32_start_timer,
839 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000840#endif
841 {NULL, }
842};
843
blueswir13f47aa82008-03-09 06:59:01 +0000844static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000845{
846 int i;
847
848 printf("Available alarm timers, in order of precedence:\n");
849 for (i = 0; alarm_timers[i].name; i++)
850 printf("%s\n", alarm_timers[i].name);
851}
852
853static void configure_alarms(char const *opt)
854{
855 int i;
856 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000857 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000858 char *arg;
859 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000860 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000861
aurel323adda042008-03-09 23:43:49 +0000862 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000863 show_available_alarms();
864 exit(0);
865 }
866
867 arg = strdup(opt);
868
869 /* Reorder the array */
870 name = strtok(arg, ",");
871 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000872 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000873 if (!strcmp(alarm_timers[i].name, name))
874 break;
875 }
876
877 if (i == count) {
878 fprintf(stderr, "Unknown clock %s\n", name);
879 goto next;
880 }
881
882 if (i < cur)
883 /* Ignore */
884 goto next;
885
886 /* Swap */
887 tmp = alarm_timers[i];
888 alarm_timers[i] = alarm_timers[cur];
889 alarm_timers[cur] = tmp;
890
891 cur++;
892next:
893 name = strtok(NULL, ",");
894 }
895
896 free(arg);
897
898 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000899 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000900 for (i = cur; i < count; i++)
901 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000902 } else {
903 show_available_alarms();
904 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000905 }
thsf3dcfad2007-08-24 01:26:02 +0000906}
907
bellard8a7ddc32004-03-31 19:00:16 +0000908QEMUClock *rt_clock;
909QEMUClock *vm_clock;
910
911static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +0000912
pbrook9596ebb2007-11-18 01:44:38 +0000913static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000914{
915 QEMUClock *clock;
916 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +0000917 clock->type = type;
918 return clock;
919}
920
921QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
922{
923 QEMUTimer *ts;
924
925 ts = qemu_mallocz(sizeof(QEMUTimer));
926 ts->clock = clock;
927 ts->cb = cb;
928 ts->opaque = opaque;
929 return ts;
930}
931
932void qemu_free_timer(QEMUTimer *ts)
933{
934 qemu_free(ts);
935}
936
937/* stop a timer, but do not dealloc it */
938void qemu_del_timer(QEMUTimer *ts)
939{
940 QEMUTimer **pt, *t;
941
942 /* NOTE: this code must be signal safe because
943 qemu_timer_expired() can be called from a signal. */
944 pt = &active_timers[ts->clock->type];
945 for(;;) {
946 t = *pt;
947 if (!t)
948 break;
949 if (t == ts) {
950 *pt = t->next;
951 break;
952 }
953 pt = &t->next;
954 }
955}
956
957/* modify the current timer so that it will be fired when current_time
958 >= expire_time. The corresponding callback will be called. */
959void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
960{
961 QEMUTimer **pt, *t;
962
963 qemu_del_timer(ts);
964
965 /* add the timer in the sorted list */
966 /* NOTE: this code must be signal safe because
967 qemu_timer_expired() can be called from a signal. */
968 pt = &active_timers[ts->clock->type];
969 for(;;) {
970 t = *pt;
971 if (!t)
972 break;
ths5fafdf22007-09-16 21:08:06 +0000973 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +0000974 break;
975 pt = &t->next;
976 }
977 ts->expire_time = expire_time;
978 ts->next = *pt;
979 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +0000980
981 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +0000982 if (pt == &active_timers[ts->clock->type]) {
983 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
984 qemu_rearm_alarm_timer(alarm_timer);
985 }
986 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +0000987 if (use_icount)
988 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +0000989 }
bellard8a7ddc32004-03-31 19:00:16 +0000990}
991
992int qemu_timer_pending(QEMUTimer *ts)
993{
994 QEMUTimer *t;
995 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
996 if (t == ts)
997 return 1;
998 }
999 return 0;
1000}
1001
1002static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1003{
1004 if (!timer_head)
1005 return 0;
1006 return (timer_head->expire_time <= current_time);
1007}
1008
1009static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1010{
1011 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001012
bellard8a7ddc32004-03-31 19:00:16 +00001013 for(;;) {
1014 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001015 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001016 break;
1017 /* remove timer from the list before calling the callback */
1018 *ptimer_head = ts->next;
1019 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001020
bellard8a7ddc32004-03-31 19:00:16 +00001021 /* run the callback (the timer list can be modified) */
1022 ts->cb(ts->opaque);
1023 }
1024}
1025
1026int64_t qemu_get_clock(QEMUClock *clock)
1027{
1028 switch(clock->type) {
1029 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001030 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001031 default:
1032 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001033 if (use_icount) {
1034 return cpu_get_icount();
1035 } else {
1036 return cpu_get_clock();
1037 }
bellard8a7ddc32004-03-31 19:00:16 +00001038 }
1039}
1040
bellard1dce7c32006-07-13 23:20:22 +00001041static void init_timers(void)
1042{
1043 init_get_clock();
1044 ticks_per_sec = QEMU_TIMER_BASE;
1045 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1046 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1047}
1048
bellard8a7ddc32004-03-31 19:00:16 +00001049/* save a timer */
1050void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1051{
1052 uint64_t expire_time;
1053
1054 if (qemu_timer_pending(ts)) {
1055 expire_time = ts->expire_time;
1056 } else {
1057 expire_time = -1;
1058 }
1059 qemu_put_be64(f, expire_time);
1060}
1061
1062void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1063{
1064 uint64_t expire_time;
1065
1066 expire_time = qemu_get_be64(f);
1067 if (expire_time != -1) {
1068 qemu_mod_timer(ts, expire_time);
1069 } else {
1070 qemu_del_timer(ts);
1071 }
1072}
1073
1074static void timer_save(QEMUFile *f, void *opaque)
1075{
1076 if (cpu_ticks_enabled) {
1077 hw_error("cannot save state if virtual timers are running");
1078 }
thsbee8d682007-12-16 23:41:11 +00001079 qemu_put_be64(f, cpu_ticks_offset);
1080 qemu_put_be64(f, ticks_per_sec);
1081 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001082}
1083
1084static int timer_load(QEMUFile *f, void *opaque, int version_id)
1085{
bellardc88676f2006-08-06 13:36:11 +00001086 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001087 return -EINVAL;
1088 if (cpu_ticks_enabled) {
1089 return -EINVAL;
1090 }
thsbee8d682007-12-16 23:41:11 +00001091 cpu_ticks_offset=qemu_get_be64(f);
1092 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001093 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001094 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001095 }
bellard8a7ddc32004-03-31 19:00:16 +00001096 return 0;
1097}
1098
aliguori50317c72009-04-24 18:03:29 +00001099static void qemu_event_increment(void);
1100
bellard67b915a2004-03-31 23:37:16 +00001101#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001102static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1103 DWORD_PTR dwUser, DWORD_PTR dw1,
1104 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001105#else
bellard8a7ddc32004-03-31 19:00:16 +00001106static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001107#endif
bellard8a7ddc32004-03-31 19:00:16 +00001108{
bellard02ba45c2004-06-25 14:46:23 +00001109#if 0
1110#define DISP_FREQ 1000
1111 {
1112 static int64_t delta_min = INT64_MAX;
1113 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1114 static int count;
1115 ti = qemu_get_clock(vm_clock);
1116 if (last_clock != 0) {
1117 delta = ti - last_clock;
1118 if (delta < delta_min)
1119 delta_min = delta;
1120 if (delta > delta_max)
1121 delta_max = delta;
1122 delta_cum += delta;
1123 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001124 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001125 muldiv64(delta_min, 1000000, ticks_per_sec),
1126 muldiv64(delta_max, 1000000, ticks_per_sec),
1127 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1128 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1129 count = 0;
1130 delta_min = INT64_MAX;
1131 delta_max = 0;
1132 delta_cum = 0;
1133 }
1134 }
1135 last_clock = ti;
1136 }
1137#endif
thsefe75412007-08-24 01:36:32 +00001138 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001139 (!use_icount &&
1140 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1141 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001142 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1143 qemu_get_clock(rt_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001144 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001145 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001146
aliguorid6dc3d42009-04-24 18:04:07 +00001147#ifndef CONFIG_IOTHREAD
1148 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001149 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001150 cpu_exit(next_cpu);
blueswir1640f42e2009-04-19 10:18:01 +00001151#ifdef CONFIG_KQEMU
aliguorid6dc3d42009-04-24 18:04:07 +00001152 if (next_cpu->kqemu_enabled) {
1153 kqemu_cpu_interrupt(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001154 }
balrogee5605e2007-12-03 03:01:40 +00001155#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001156 }
aliguorid6dc3d42009-04-24 18:04:07 +00001157#endif
aliguori43b96852009-04-24 18:03:33 +00001158 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001159 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001160 }
1161}
1162
pbrook2e70f6e2008-06-29 01:03:05 +00001163static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001164{
pbrook2e70f6e2008-06-29 01:03:05 +00001165 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001166
1167 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001168 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1169 qemu_get_clock(vm_clock);
1170 } else {
1171 /* To avoid problems with overflow limit this to 2^32. */
1172 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001173 }
1174
pbrook2e70f6e2008-06-29 01:03:05 +00001175 if (delta < 0)
1176 delta = 0;
thsefe75412007-08-24 01:36:32 +00001177
pbrook2e70f6e2008-06-29 01:03:05 +00001178 return delta;
1179}
1180
blueswir18632fb92008-09-14 13:59:34 +00001181#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001182static uint64_t qemu_next_deadline_dyntick(void)
1183{
1184 int64_t delta;
1185 int64_t rtdelta;
1186
1187 if (use_icount)
1188 delta = INT32_MAX;
1189 else
1190 delta = (qemu_next_deadline() + 999) / 1000;
1191
1192 if (active_timers[QEMU_TIMER_REALTIME]) {
1193 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1194 qemu_get_clock(rt_clock))*1000;
1195 if (rtdelta < delta)
1196 delta = rtdelta;
1197 }
1198
1199 if (delta < MIN_TIMER_REARM_US)
1200 delta = MIN_TIMER_REARM_US;
1201
1202 return delta;
thsefe75412007-08-24 01:36:32 +00001203}
blueswir18632fb92008-09-14 13:59:34 +00001204#endif
thsefe75412007-08-24 01:36:32 +00001205
bellardfd872592004-05-12 19:11:15 +00001206#ifndef _WIN32
1207
aliguori7183b4b2008-11-05 20:40:18 +00001208/* Sets a specific flag */
1209static int fcntl_setfl(int fd, int flag)
1210{
1211 int flags;
1212
1213 flags = fcntl(fd, F_GETFL);
1214 if (flags == -1)
1215 return -errno;
1216
1217 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1218 return -errno;
1219
1220 return 0;
1221}
1222
bellard829309c2004-05-20 13:20:12 +00001223#if defined(__linux__)
1224
bellardfd872592004-05-12 19:11:15 +00001225#define RTC_FREQ 1024
1226
aurel32de9a95f2008-11-11 13:41:01 +00001227static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001228{
thsc8994012007-08-19 21:56:03 +00001229 struct sigaction act;
1230
1231 /* timer signal */
1232 sigfillset(&act.sa_mask);
1233 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001234 act.sa_handler = host_alarm_handler;
1235
1236 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001237 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001238 fcntl(fd, F_SETOWN, getpid());
1239}
1240
thsc40ec5a2007-08-19 22:09:40 +00001241static int hpet_start_timer(struct qemu_alarm_timer *t)
1242{
1243 struct hpet_info info;
1244 int r, fd;
1245
1246 fd = open("/dev/hpet", O_RDONLY);
1247 if (fd < 0)
1248 return -1;
1249
1250 /* Set frequency */
1251 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1252 if (r < 0) {
1253 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1254 "error, but for better emulation accuracy type:\n"
1255 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1256 goto fail;
1257 }
1258
1259 /* Check capabilities */
1260 r = ioctl(fd, HPET_INFO, &info);
1261 if (r < 0)
1262 goto fail;
1263
1264 /* Enable periodic mode */
1265 r = ioctl(fd, HPET_EPI, 0);
1266 if (info.hi_flags && (r < 0))
1267 goto fail;
1268
1269 /* Enable interrupt */
1270 r = ioctl(fd, HPET_IE_ON, 0);
1271 if (r < 0)
1272 goto fail;
1273
1274 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001275 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001276
1277 return 0;
1278fail:
1279 close(fd);
1280 return -1;
1281}
1282
1283static void hpet_stop_timer(struct qemu_alarm_timer *t)
1284{
pbrookfcdc2122007-08-23 20:22:22 +00001285 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001286
1287 close(fd);
1288}
1289
thsc8994012007-08-19 21:56:03 +00001290static int rtc_start_timer(struct qemu_alarm_timer *t)
1291{
1292 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001293 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001294
balrogaeb30be2007-07-02 15:03:13 +00001295 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001296 if (rtc_fd < 0)
1297 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001298 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1299 if (current_rtc_freq != RTC_FREQ &&
1300 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001301 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1302 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1303 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1304 goto fail;
1305 }
1306 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1307 fail:
1308 close(rtc_fd);
1309 return -1;
1310 }
thsc8994012007-08-19 21:56:03 +00001311
1312 enable_sigio_timer(rtc_fd);
1313
pbrookfcdc2122007-08-23 20:22:22 +00001314 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001315
bellardfd872592004-05-12 19:11:15 +00001316 return 0;
1317}
1318
thsc8994012007-08-19 21:56:03 +00001319static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001320{
pbrookfcdc2122007-08-23 20:22:22 +00001321 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001322
1323 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001324}
1325
thsefe75412007-08-24 01:36:32 +00001326static int dynticks_start_timer(struct qemu_alarm_timer *t)
1327{
1328 struct sigevent ev;
1329 timer_t host_timer;
1330 struct sigaction act;
1331
1332 sigfillset(&act.sa_mask);
1333 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001334 act.sa_handler = host_alarm_handler;
1335
1336 sigaction(SIGALRM, &act, NULL);
1337
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001338 /*
1339 * Initialize ev struct to 0 to avoid valgrind complaining
1340 * about uninitialized data in timer_create call
1341 */
1342 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001343 ev.sigev_value.sival_int = 0;
1344 ev.sigev_notify = SIGEV_SIGNAL;
1345 ev.sigev_signo = SIGALRM;
1346
1347 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1348 perror("timer_create");
1349
1350 /* disable dynticks */
1351 fprintf(stderr, "Dynamic Ticks disabled\n");
1352
1353 return -1;
1354 }
1355
blueswir10399bfe2008-11-16 11:37:18 +00001356 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001357
1358 return 0;
1359}
1360
1361static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1362{
blueswir10399bfe2008-11-16 11:37:18 +00001363 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001364
1365 timer_delete(host_timer);
1366}
1367
1368static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1369{
blueswir10399bfe2008-11-16 11:37:18 +00001370 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001371 struct itimerspec timeout;
1372 int64_t nearest_delta_us = INT64_MAX;
1373 int64_t current_us;
1374
1375 if (!active_timers[QEMU_TIMER_REALTIME] &&
1376 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001377 return;
thsefe75412007-08-24 01:36:32 +00001378
pbrook2e70f6e2008-06-29 01:03:05 +00001379 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001380
1381 /* check whether a timer is already running */
1382 if (timer_gettime(host_timer, &timeout)) {
1383 perror("gettime");
1384 fprintf(stderr, "Internal timer error: aborting\n");
1385 exit(1);
1386 }
1387 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1388 if (current_us && current_us <= nearest_delta_us)
1389 return;
1390
1391 timeout.it_interval.tv_sec = 0;
1392 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1393 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1394 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1395 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1396 perror("settime");
1397 fprintf(stderr, "Internal timer error: aborting\n");
1398 exit(1);
1399 }
1400}
1401
ths70744b32007-08-26 17:31:30 +00001402#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001403
thsc8994012007-08-19 21:56:03 +00001404static int unix_start_timer(struct qemu_alarm_timer *t)
1405{
1406 struct sigaction act;
1407 struct itimerval itv;
1408 int err;
1409
1410 /* timer signal */
1411 sigfillset(&act.sa_mask);
1412 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001413 act.sa_handler = host_alarm_handler;
1414
1415 sigaction(SIGALRM, &act, NULL);
1416
1417 itv.it_interval.tv_sec = 0;
1418 /* for i386 kernel 2.6 to get 1 ms */
1419 itv.it_interval.tv_usec = 999;
1420 itv.it_value.tv_sec = 0;
1421 itv.it_value.tv_usec = 10 * 1000;
1422
1423 err = setitimer(ITIMER_REAL, &itv, NULL);
1424 if (err)
1425 return -1;
1426
1427 return 0;
1428}
1429
1430static void unix_stop_timer(struct qemu_alarm_timer *t)
1431{
1432 struct itimerval itv;
1433
1434 memset(&itv, 0, sizeof(itv));
1435 setitimer(ITIMER_REAL, &itv, NULL);
1436}
1437
bellard829309c2004-05-20 13:20:12 +00001438#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001439
aliguorif49e58d2008-11-05 21:22:34 +00001440
thsc8994012007-08-19 21:56:03 +00001441#ifdef _WIN32
1442
1443static int win32_start_timer(struct qemu_alarm_timer *t)
1444{
1445 TIMECAPS tc;
1446 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001447 UINT flags;
thsc8994012007-08-19 21:56:03 +00001448
thsc8994012007-08-19 21:56:03 +00001449 memset(&tc, 0, sizeof(tc));
1450 timeGetDevCaps(&tc, sizeof(tc));
1451
1452 if (data->period < tc.wPeriodMin)
1453 data->period = tc.wPeriodMin;
1454
1455 timeBeginPeriod(data->period);
1456
thsefe75412007-08-24 01:36:32 +00001457 flags = TIME_CALLBACK_FUNCTION;
1458 if (alarm_has_dynticks(t))
1459 flags |= TIME_ONESHOT;
1460 else
1461 flags |= TIME_PERIODIC;
1462
thsc8994012007-08-19 21:56:03 +00001463 data->timerId = timeSetEvent(1, // interval (ms)
1464 data->period, // resolution
1465 host_alarm_handler, // function
1466 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001467 flags);
thsc8994012007-08-19 21:56:03 +00001468
1469 if (!data->timerId) {
1470 perror("Failed to initialize win32 alarm timer");
thsc8994012007-08-19 21:56:03 +00001471 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001472 return -1;
1473 }
1474
thsc8994012007-08-19 21:56:03 +00001475 return 0;
1476}
1477
1478static void win32_stop_timer(struct qemu_alarm_timer *t)
1479{
1480 struct qemu_alarm_win32 *data = t->priv;
1481
1482 timeKillEvent(data->timerId);
1483 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001484}
1485
thsefe75412007-08-24 01:36:32 +00001486static void win32_rearm_timer(struct qemu_alarm_timer *t)
1487{
1488 struct qemu_alarm_win32 *data = t->priv;
1489 uint64_t nearest_delta_us;
1490
1491 if (!active_timers[QEMU_TIMER_REALTIME] &&
1492 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001493 return;
thsefe75412007-08-24 01:36:32 +00001494
pbrook2e70f6e2008-06-29 01:03:05 +00001495 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001496 nearest_delta_us /= 1000;
1497
1498 timeKillEvent(data->timerId);
1499
1500 data->timerId = timeSetEvent(1,
1501 data->period,
1502 host_alarm_handler,
1503 (DWORD)t,
1504 TIME_ONESHOT | TIME_PERIODIC);
1505
1506 if (!data->timerId) {
1507 perror("Failed to re-arm win32 alarm timer");
1508
1509 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001510 exit(1);
1511 }
1512}
1513
thsc8994012007-08-19 21:56:03 +00001514#endif /* _WIN32 */
1515
aliguori7183b4b2008-11-05 20:40:18 +00001516static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001517{
blueswir1223f0d72008-09-30 18:12:18 +00001518 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001519 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001520
thsc8994012007-08-19 21:56:03 +00001521 for (i = 0; alarm_timers[i].name; i++) {
1522 t = &alarm_timers[i];
1523
thsc8994012007-08-19 21:56:03 +00001524 err = t->start(t);
1525 if (!err)
1526 break;
bellard67b915a2004-03-31 23:37:16 +00001527 }
bellardfd872592004-05-12 19:11:15 +00001528
thsc8994012007-08-19 21:56:03 +00001529 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001530 err = -ENOENT;
1531 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001532 }
thsc8994012007-08-19 21:56:03 +00001533
1534 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001535
aliguori6abfbd72008-11-05 20:49:37 +00001536 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001537
1538fail:
aliguori7183b4b2008-11-05 20:40:18 +00001539 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001540}
1541
pbrook9596ebb2007-11-18 01:44:38 +00001542static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001543{
thsc8994012007-08-19 21:56:03 +00001544 alarm_timer->stop(alarm_timer);
1545 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001546}
1547
bellardc4b1fcc2004-03-14 21:44:30 +00001548/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001549/* host time/date access */
1550void qemu_get_timedate(struct tm *tm, int offset)
1551{
1552 time_t ti;
1553 struct tm *ret;
1554
1555 time(&ti);
1556 ti += offset;
1557 if (rtc_date_offset == -1) {
1558 if (rtc_utc)
1559 ret = gmtime(&ti);
1560 else
1561 ret = localtime(&ti);
1562 } else {
1563 ti -= rtc_date_offset;
1564 ret = gmtime(&ti);
1565 }
1566
1567 memcpy(tm, ret, sizeof(struct tm));
1568}
1569
1570int qemu_timedate_diff(struct tm *tm)
1571{
1572 time_t seconds;
1573
1574 if (rtc_date_offset == -1)
1575 if (rtc_utc)
1576 seconds = mktimegm(tm);
1577 else
1578 seconds = mktime(tm);
1579 else
1580 seconds = mktimegm(tm) + rtc_date_offset;
1581
1582 return seconds - time(NULL);
1583}
1584
bellardfd1dff42006-02-01 21:29:26 +00001585#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001586static void socket_cleanup(void)
1587{
1588 WSACleanup();
1589}
bellard82c643f2004-07-14 17:28:13 +00001590
bellardfd1dff42006-02-01 21:29:26 +00001591static int socket_init(void)
1592{
1593 WSADATA Data;
1594 int ret, err;
1595
1596 ret = WSAStartup(MAKEWORD(2,2), &Data);
1597 if (ret != 0) {
1598 err = WSAGetLastError();
1599 fprintf(stderr, "WSAStartup: %d\n", err);
1600 return -1;
1601 }
1602 atexit(socket_cleanup);
1603 return 0;
1604}
aurel3264b7b732008-05-05 10:05:31 +00001605#endif
1606
Jan Kiszka5db4af82009-06-24 14:42:28 +02001607int get_next_param_value(char *buf, int buf_size,
1608 const char *tag, const char **pstr)
bellard7c9d8e02005-11-15 22:16:05 +00001609{
1610 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001611 char option[128];
1612
Jan Kiszka5db4af82009-06-24 14:42:28 +02001613 p = *pstr;
bellard7c9d8e02005-11-15 22:16:05 +00001614 for(;;) {
aliguori268a3622009-04-21 22:30:27 +00001615 p = get_opt_name(option, sizeof(option), p, '=');
bellard7c9d8e02005-11-15 22:16:05 +00001616 if (*p != '=')
1617 break;
1618 p++;
1619 if (!strcmp(tag, option)) {
Jan Kiszka5db4af82009-06-24 14:42:28 +02001620 *pstr = get_opt_value(buf, buf_size, p);
1621 if (**pstr == ',') {
1622 (*pstr)++;
1623 }
thse4bcb142007-12-02 04:51:10 +00001624 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001625 } else {
balrog609497a2008-01-14 02:56:53 +00001626 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001627 }
1628 if (*p != ',')
1629 break;
1630 p++;
1631 }
1632 return 0;
1633}
1634
Jan Kiszka5db4af82009-06-24 14:42:28 +02001635int get_param_value(char *buf, int buf_size,
1636 const char *tag, const char *str)
1637{
1638 return get_next_param_value(buf, buf_size, tag, &str);
1639}
1640
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001641int check_params(char *buf, int buf_size,
1642 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001643{
1644 const char *p;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001645 int i;
thse4bcb142007-12-02 04:51:10 +00001646
1647 p = str;
aliguori10300212009-04-21 19:56:23 +00001648 while (*p != '\0') {
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001649 p = get_opt_name(buf, buf_size, p, '=');
Jan Kiszkaffad4112009-04-26 18:53:42 +02001650 if (*p != '=') {
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001651 return -1;
Jan Kiszkaffad4112009-04-26 18:53:42 +02001652 }
thse4bcb142007-12-02 04:51:10 +00001653 p++;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001654 for (i = 0; params[i] != NULL; i++) {
1655 if (!strcmp(params[i], buf)) {
thse4bcb142007-12-02 04:51:10 +00001656 break;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001657 }
1658 }
Jan Kiszkaffad4112009-04-26 18:53:42 +02001659 if (params[i] == NULL) {
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001660 return -1;
Jan Kiszkaffad4112009-04-26 18:53:42 +02001661 }
balrog609497a2008-01-14 02:56:53 +00001662 p = get_opt_value(NULL, 0, p);
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001663 if (*p != ',') {
thse4bcb142007-12-02 04:51:10 +00001664 break;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001665 }
thse4bcb142007-12-02 04:51:10 +00001666 p++;
1667 }
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001668 return 0;
thse4bcb142007-12-02 04:51:10 +00001669}
1670
balrog1ae26a12008-09-28 23:19:47 +00001671/***********************************************************/
1672/* Bluetooth support */
1673static int nb_hcis;
1674static int cur_hci;
1675static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001676
balrog1ae26a12008-09-28 23:19:47 +00001677static struct bt_vlan_s {
1678 struct bt_scatternet_s net;
1679 int id;
1680 struct bt_vlan_s *next;
1681} *first_bt_vlan;
1682
1683/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001684static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001685{
1686 struct bt_vlan_s **pvlan, *vlan;
1687 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1688 if (vlan->id == id)
1689 return &vlan->net;
1690 }
1691 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1692 vlan->id = id;
1693 pvlan = &first_bt_vlan;
1694 while (*pvlan != NULL)
1695 pvlan = &(*pvlan)->next;
1696 *pvlan = vlan;
1697 return &vlan->net;
1698}
1699
1700static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1701{
1702}
1703
1704static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1705{
1706 return -ENOTSUP;
1707}
1708
1709static struct HCIInfo null_hci = {
1710 .cmd_send = null_hci_send,
1711 .sco_send = null_hci_send,
1712 .acl_send = null_hci_send,
1713 .bdaddr_set = null_hci_addr_set,
1714};
1715
1716struct HCIInfo *qemu_next_hci(void)
1717{
1718 if (cur_hci == nb_hcis)
1719 return &null_hci;
1720
1721 return hci_table[cur_hci++];
1722}
1723
balrogdc72ac12008-11-09 00:04:26 +00001724static struct HCIInfo *hci_init(const char *str)
1725{
1726 char *endp;
1727 struct bt_scatternet_s *vlan = 0;
1728
1729 if (!strcmp(str, "null"))
1730 /* null */
1731 return &null_hci;
1732 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1733 /* host[:hciN] */
1734 return bt_host_hci(str[4] ? str + 5 : "hci0");
1735 else if (!strncmp(str, "hci", 3)) {
1736 /* hci[,vlan=n] */
1737 if (str[3]) {
1738 if (!strncmp(str + 3, ",vlan=", 6)) {
1739 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1740 if (*endp)
1741 vlan = 0;
1742 }
1743 } else
1744 vlan = qemu_find_bt_vlan(0);
1745 if (vlan)
1746 return bt_new_hci(vlan);
1747 }
1748
1749 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1750
1751 return 0;
1752}
1753
1754static int bt_hci_parse(const char *str)
1755{
1756 struct HCIInfo *hci;
1757 bdaddr_t bdaddr;
1758
1759 if (nb_hcis >= MAX_NICS) {
1760 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1761 return -1;
1762 }
1763
1764 hci = hci_init(str);
1765 if (!hci)
1766 return -1;
1767
1768 bdaddr.b[0] = 0x52;
1769 bdaddr.b[1] = 0x54;
1770 bdaddr.b[2] = 0x00;
1771 bdaddr.b[3] = 0x12;
1772 bdaddr.b[4] = 0x34;
1773 bdaddr.b[5] = 0x56 + nb_hcis;
1774 hci->bdaddr_set(hci, bdaddr.b);
1775
1776 hci_table[nb_hcis++] = hci;
1777
1778 return 0;
1779}
1780
1781static void bt_vhci_add(int vlan_id)
1782{
1783 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1784
1785 if (!vlan->slave)
1786 fprintf(stderr, "qemu: warning: adding a VHCI to "
1787 "an empty scatternet %i\n", vlan_id);
1788
1789 bt_vhci_init(bt_new_hci(vlan));
1790}
1791
1792static struct bt_device_s *bt_device_add(const char *opt)
1793{
1794 struct bt_scatternet_s *vlan;
1795 int vlan_id = 0;
1796 char *endp = strstr(opt, ",vlan=");
1797 int len = (endp ? endp - opt : strlen(opt)) + 1;
1798 char devname[10];
1799
1800 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1801
1802 if (endp) {
1803 vlan_id = strtol(endp + 6, &endp, 0);
1804 if (*endp) {
1805 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1806 return 0;
1807 }
1808 }
1809
1810 vlan = qemu_find_bt_vlan(vlan_id);
1811
1812 if (!vlan->slave)
1813 fprintf(stderr, "qemu: warning: adding a slave device to "
1814 "an empty scatternet %i\n", vlan_id);
1815
1816 if (!strcmp(devname, "keyboard"))
1817 return bt_keyboard_init(vlan);
1818
1819 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1820 return 0;
1821}
1822
1823static int bt_parse(const char *opt)
1824{
1825 const char *endp, *p;
1826 int vlan;
1827
1828 if (strstart(opt, "hci", &endp)) {
1829 if (!*endp || *endp == ',') {
1830 if (*endp)
1831 if (!strstart(endp, ",vlan=", 0))
1832 opt = endp + 1;
1833
1834 return bt_hci_parse(opt);
1835 }
1836 } else if (strstart(opt, "vhci", &endp)) {
1837 if (!*endp || *endp == ',') {
1838 if (*endp) {
1839 if (strstart(endp, ",vlan=", &p)) {
1840 vlan = strtol(p, (char **) &endp, 0);
1841 if (*endp) {
1842 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1843 return 1;
1844 }
1845 } else {
1846 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1847 return 1;
1848 }
1849 } else
1850 vlan = 0;
1851
1852 bt_vhci_add(vlan);
1853 return 0;
1854 }
1855 } else if (strstart(opt, "device:", &endp))
1856 return !bt_device_add(endp);
1857
1858 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1859 return 1;
1860}
1861
balrog1ae26a12008-09-28 23:19:47 +00001862/***********************************************************/
1863/* QEMU Block devices */
1864
balrog609497a2008-01-14 02:56:53 +00001865#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001866#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001867#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001868#define PFLASH_ALIAS "if=pflash"
1869#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001870#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001871
aliguori7d5aca92009-02-11 15:19:58 +00001872static int drive_opt_get_free_idx(void)
1873{
1874 int index;
1875
1876 for (index = 0; index < MAX_DRIVES; index++)
1877 if (!drives_opt[index].used) {
1878 drives_opt[index].used = 1;
1879 return index;
1880 }
1881
1882 return -1;
1883}
1884
1885static int drive_get_free_idx(void)
1886{
1887 int index;
1888
1889 for (index = 0; index < MAX_DRIVES; index++)
1890 if (!drives_table[index].used) {
1891 drives_table[index].used = 1;
1892 return index;
1893 }
1894
1895 return -1;
1896}
1897
aliguori4d73cd32009-02-11 15:20:46 +00001898int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001899{
1900 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00001901 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00001902
aliguori7d5aca92009-02-11 15:19:58 +00001903 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00001904 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00001905 return -1;
thse4bcb142007-12-02 04:51:10 +00001906 }
1907
aliguori7d5aca92009-02-11 15:19:58 +00001908 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00001909 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00001910 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00001911 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001912 va_end(ap);
1913
aliguori7d5aca92009-02-11 15:19:58 +00001914 nb_drives_opt++;
1915 return index;
thse4bcb142007-12-02 04:51:10 +00001916}
1917
aliguorib01b1112009-02-11 15:20:20 +00001918void drive_remove(int index)
1919{
1920 drives_opt[index].used = 0;
1921 nb_drives_opt--;
1922}
1923
thsf60d39b2007-12-17 03:55:57 +00001924int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001925{
1926 int index;
1927
1928 /* seek interface, bus and unit */
1929
aliguori7d5aca92009-02-11 15:19:58 +00001930 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00001931 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00001932 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00001933 drives_table[index].unit == unit &&
1934 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00001935 return index;
1936
1937 return -1;
1938}
1939
thsf60d39b2007-12-17 03:55:57 +00001940int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001941{
1942 int max_bus;
1943 int index;
1944
1945 max_bus = -1;
1946 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00001947 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00001948 drives_table[index].bus > max_bus)
1949 max_bus = drives_table[index].bus;
1950 }
1951 return max_bus;
1952}
1953
aliguorifa879c62009-01-07 17:32:33 +00001954const char *drive_get_serial(BlockDriverState *bdrv)
1955{
1956 int index;
1957
1958 for (index = 0; index < nb_drives; index++)
1959 if (drives_table[index].bdrv == bdrv)
1960 return drives_table[index].serial;
1961
1962 return "\0";
1963}
1964
aliguori428c5702009-01-21 18:59:04 +00001965BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1966{
1967 int index;
1968
1969 for (index = 0; index < nb_drives; index++)
1970 if (drives_table[index].bdrv == bdrv)
1971 return drives_table[index].onerror;
1972
aliguoricdad4bd2009-02-28 16:51:01 +00001973 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00001974}
1975
aurel32a1620fa2008-04-29 05:58:01 +00001976static void bdrv_format_print(void *opaque, const char *name)
1977{
1978 fprintf(stderr, " %s", name);
1979}
1980
aliguorib01b1112009-02-11 15:20:20 +00001981void drive_uninit(BlockDriverState *bdrv)
1982{
1983 int i;
1984
1985 for (i = 0; i < MAX_DRIVES; i++)
1986 if (drives_table[i].bdrv == bdrv) {
1987 drives_table[i].bdrv = NULL;
1988 drives_table[i].used = 0;
1989 drive_remove(drives_table[i].drive_opt_idx);
1990 nb_drives--;
1991 break;
1992 }
1993}
1994
aliguori4d73cd32009-02-11 15:20:46 +00001995int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00001996{
1997 char buf[128];
1998 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00001999 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002000 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002001 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002002 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002003 enum { MEDIA_DISK, MEDIA_CDROM } media;
2004 int bus_id, unit_id;
2005 int cyls, heads, secs, translation;
2006 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002007 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002008 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002009 int max_devs;
2010 int index;
balrog33f00272007-12-24 14:33:24 +00002011 int cache;
aliguori428c5702009-01-21 18:59:04 +00002012 int bdrv_flags, onerror;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002013 const char *devaddr;
aliguori7d5aca92009-02-11 15:19:58 +00002014 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002015 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002016 static const char * const params[] = { "bus", "unit", "if", "index",
2017 "cyls", "heads", "secs", "trans",
2018 "media", "snapshot", "file",
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002019 "cache", "format", "serial",
2020 "werror", "addr",
aliguori428c5702009-01-21 18:59:04 +00002021 NULL };
thse4bcb142007-12-02 04:51:10 +00002022
Jan Kiszka0aa7a202009-05-08 12:34:17 +02002023 if (check_params(buf, sizeof(buf), params, str) < 0) {
Mark McLoughlincda94b22009-05-28 14:07:31 +01002024 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
2025 buf, str);
thse4bcb142007-12-02 04:51:10 +00002026 return -1;
2027 }
2028
2029 file[0] = 0;
2030 cyls = heads = secs = 0;
2031 bus_id = 0;
2032 unit_id = -1;
2033 translation = BIOS_ATA_TRANSLATION_AUTO;
2034 index = -1;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02002035 cache = 1;
thse4bcb142007-12-02 04:51:10 +00002036
blueswir1c9b1ae22008-09-28 18:55:17 +00002037 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002038 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002039 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002040 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002041 } else {
thsf60d39b2007-12-17 03:55:57 +00002042 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002043 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002044 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002045 }
2046 media = MEDIA_DISK;
2047
2048 /* extract parameters */
2049
2050 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2051 bus_id = strtol(buf, NULL, 0);
2052 if (bus_id < 0) {
2053 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2054 return -1;
2055 }
2056 }
2057
2058 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2059 unit_id = strtol(buf, NULL, 0);
2060 if (unit_id < 0) {
2061 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2062 return -1;
2063 }
2064 }
2065
2066 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002067 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002068 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002069 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002070 max_devs = MAX_IDE_DEVS;
2071 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002072 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002073 max_devs = MAX_SCSI_DEVS;
2074 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002075 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002076 max_devs = 0;
2077 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002078 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002079 max_devs = 0;
2080 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002081 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002082 max_devs = 0;
2083 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002084 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002085 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002086 } else if (!strcmp(buf, "virtio")) {
2087 type = IF_VIRTIO;
2088 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002089 } else if (!strcmp(buf, "xen")) {
2090 type = IF_XEN;
2091 max_devs = 0;
2092 } else {
thse4bcb142007-12-02 04:51:10 +00002093 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2094 return -1;
2095 }
2096 }
2097
2098 if (get_param_value(buf, sizeof(buf), "index", str)) {
2099 index = strtol(buf, NULL, 0);
2100 if (index < 0) {
2101 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2102 return -1;
2103 }
2104 }
2105
2106 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2107 cyls = strtol(buf, NULL, 0);
2108 }
2109
2110 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2111 heads = strtol(buf, NULL, 0);
2112 }
2113
2114 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2115 secs = strtol(buf, NULL, 0);
2116 }
2117
2118 if (cyls || heads || secs) {
2119 if (cyls < 1 || cyls > 16383) {
2120 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2121 return -1;
2122 }
2123 if (heads < 1 || heads > 16) {
2124 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2125 return -1;
2126 }
2127 if (secs < 1 || secs > 63) {
2128 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2129 return -1;
2130 }
2131 }
2132
2133 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2134 if (!cyls) {
2135 fprintf(stderr,
2136 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2137 str);
2138 return -1;
2139 }
2140 if (!strcmp(buf, "none"))
2141 translation = BIOS_ATA_TRANSLATION_NONE;
2142 else if (!strcmp(buf, "lba"))
2143 translation = BIOS_ATA_TRANSLATION_LBA;
2144 else if (!strcmp(buf, "auto"))
2145 translation = BIOS_ATA_TRANSLATION_AUTO;
2146 else {
2147 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2148 return -1;
2149 }
2150 }
2151
2152 if (get_param_value(buf, sizeof(buf), "media", str)) {
2153 if (!strcmp(buf, "disk")) {
2154 media = MEDIA_DISK;
2155 } else if (!strcmp(buf, "cdrom")) {
2156 if (cyls || secs || heads) {
2157 fprintf(stderr,
2158 "qemu: '%s' invalid physical CHS format\n", str);
2159 return -1;
2160 }
2161 media = MEDIA_CDROM;
2162 } else {
2163 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2164 return -1;
2165 }
2166 }
2167
2168 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2169 if (!strcmp(buf, "on"))
2170 snapshot = 1;
2171 else if (!strcmp(buf, "off"))
2172 snapshot = 0;
2173 else {
2174 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2175 return -1;
2176 }
2177 }
2178
balrog33f00272007-12-24 14:33:24 +00002179 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002180 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002181 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002182 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002183 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002184 else if (!strcmp(buf, "writeback"))
2185 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002186 else {
2187 fprintf(stderr, "qemu: invalid cache option\n");
2188 return -1;
2189 }
2190 }
2191
aurel321e72d3b2008-04-28 20:26:45 +00002192 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002193 if (strcmp(buf, "?") == 0) {
2194 fprintf(stderr, "qemu: Supported formats:");
2195 bdrv_iterate_format(bdrv_format_print, NULL);
2196 fprintf(stderr, "\n");
2197 return -1;
2198 }
aurel321e72d3b2008-04-28 20:26:45 +00002199 drv = bdrv_find_format(buf);
2200 if (!drv) {
2201 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2202 return -1;
2203 }
2204 }
2205
balrog609497a2008-01-14 02:56:53 +00002206 if (arg->file == NULL)
2207 get_param_value(file, sizeof(file), "file", str);
2208 else
2209 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002210
aliguorifa879c62009-01-07 17:32:33 +00002211 if (!get_param_value(serial, sizeof(serial), "serial", str))
2212 memset(serial, 0, sizeof(serial));
2213
aliguoricdad4bd2009-02-28 16:51:01 +00002214 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002215 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002216 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002217 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002218 return -1;
2219 }
2220 if (!strcmp(buf, "ignore"))
2221 onerror = BLOCK_ERR_IGNORE;
2222 else if (!strcmp(buf, "enospc"))
2223 onerror = BLOCK_ERR_STOP_ENOSPC;
2224 else if (!strcmp(buf, "stop"))
2225 onerror = BLOCK_ERR_STOP_ANY;
2226 else if (!strcmp(buf, "report"))
2227 onerror = BLOCK_ERR_REPORT;
2228 else {
2229 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2230 return -1;
2231 }
2232 }
2233
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002234 devaddr = NULL;
2235 if (get_param_value(buf, sizeof(buf), "addr", str)) {
2236 if (type != IF_VIRTIO) {
2237 fprintf(stderr, "addr is not supported by in '%s'\n", str);
2238 return -1;
2239 }
2240 devaddr = strdup(buf);
2241 }
2242
thse4bcb142007-12-02 04:51:10 +00002243 /* compute bus and unit according index */
2244
2245 if (index != -1) {
2246 if (bus_id != 0 || unit_id != -1) {
2247 fprintf(stderr,
2248 "qemu: '%s' index cannot be used with bus and unit\n", str);
2249 return -1;
2250 }
2251 if (max_devs == 0)
2252 {
2253 unit_id = index;
2254 bus_id = 0;
2255 } else {
2256 unit_id = index % max_devs;
2257 bus_id = index / max_devs;
2258 }
2259 }
2260
2261 /* if user doesn't specify a unit_id,
2262 * try to find the first free
2263 */
2264
2265 if (unit_id == -1) {
2266 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002267 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002268 unit_id++;
2269 if (max_devs && unit_id >= max_devs) {
2270 unit_id -= max_devs;
2271 bus_id++;
2272 }
2273 }
2274 }
2275
2276 /* check unit id */
2277
2278 if (max_devs && unit_id >= max_devs) {
2279 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2280 str, unit_id, max_devs - 1);
2281 return -1;
2282 }
2283
2284 /*
2285 * ignore multiple definitions
2286 */
2287
thsf60d39b2007-12-17 03:55:57 +00002288 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002289 return -2;
thse4bcb142007-12-02 04:51:10 +00002290
2291 /* init */
2292
thsf60d39b2007-12-17 03:55:57 +00002293 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002294 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002295 if (max_devs)
2296 snprintf(buf, sizeof(buf), "%s%i%s%i",
2297 devname, bus_id, mediastr, unit_id);
2298 else
2299 snprintf(buf, sizeof(buf), "%s%s%i",
2300 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002301 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002302 drives_table_idx = drive_get_free_idx();
2303 drives_table[drives_table_idx].bdrv = bdrv;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002304 drives_table[drives_table_idx].devaddr = devaddr;
aliguori7d5aca92009-02-11 15:19:58 +00002305 drives_table[drives_table_idx].type = type;
2306 drives_table[drives_table_idx].bus = bus_id;
2307 drives_table[drives_table_idx].unit = unit_id;
2308 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002309 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
Anthony Liguorie6a6dfe2009-05-01 09:26:16 -05002310 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002311 nb_drives++;
2312
thsf60d39b2007-12-17 03:55:57 +00002313 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002314 case IF_IDE:
2315 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002316 case IF_XEN:
thse4bcb142007-12-02 04:51:10 +00002317 switch(media) {
2318 case MEDIA_DISK:
2319 if (cyls != 0) {
2320 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2321 bdrv_set_translation_hint(bdrv, translation);
2322 }
2323 break;
2324 case MEDIA_CDROM:
2325 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2326 break;
2327 }
2328 break;
2329 case IF_SD:
2330 /* FIXME: This isn't really a floppy, but it's a reasonable
2331 approximation. */
2332 case IF_FLOPPY:
2333 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2334 break;
2335 case IF_PFLASH:
2336 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002337 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002338 break;
Paul Brookaae94602009-05-14 22:35:06 +01002339 case IF_COUNT:
2340 abort();
thse4bcb142007-12-02 04:51:10 +00002341 }
2342 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002343 return -2;
balrog33f00272007-12-24 14:33:24 +00002344 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002345 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002346 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002347 cache = 2; /* always use write-back with snapshot */
2348 }
2349 if (cache == 0) /* no caching */
2350 bdrv_flags |= BDRV_O_NOCACHE;
2351 else if (cache == 2) /* write-back */
2352 bdrv_flags |= BDRV_O_CACHE_WB;
aliguoric0f4ce72009-03-05 23:01:01 +00002353 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002354 fprintf(stderr, "qemu: could not open disk image %s\n",
2355 file);
2356 return -1;
2357 }
aliguoric0f4ce72009-03-05 23:01:01 +00002358 if (bdrv_key_required(bdrv))
2359 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002360 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002361}
2362
Jan Kiszka76e30d02009-07-02 00:19:02 +02002363void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2364{
2365 boot_set_handler = func;
2366 boot_set_opaque = opaque;
2367}
2368
2369int qemu_boot_set(const char *boot_devices)
2370{
2371 if (!boot_set_handler) {
2372 return -EINVAL;
2373 }
2374 return boot_set_handler(boot_set_opaque, boot_devices);
2375}
2376
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002377static int parse_bootdevices(char *devices)
2378{
2379 /* We just do some generic consistency checks */
2380 const char *p;
2381 int bitmap = 0;
2382
2383 for (p = devices; *p != '\0'; p++) {
2384 /* Allowed boot devices are:
2385 * a-b: floppy disk drives
2386 * c-f: IDE disk drives
2387 * g-m: machine implementation dependant drives
2388 * n-p: network devices
2389 * It's up to each machine implementation to check if the given boot
2390 * devices match the actual hardware implementation and firmware
2391 * features.
2392 */
2393 if (*p < 'a' || *p > 'p') {
2394 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2395 exit(1);
2396 }
2397 if (bitmap & (1 << (*p - 'a'))) {
2398 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2399 exit(1);
2400 }
2401 bitmap |= 1 << (*p - 'a');
2402 }
2403 return bitmap;
2404}
2405
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002406static void restore_boot_devices(void *opaque)
2407{
2408 char *standard_boot_devices = opaque;
2409
2410 qemu_boot_set(standard_boot_devices);
2411
2412 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2413 qemu_free(standard_boot_devices);
2414}
2415
aliguori268a3622009-04-21 22:30:27 +00002416static void numa_add(const char *optarg)
2417{
2418 char option[128];
2419 char *endptr;
2420 unsigned long long value, endvalue;
2421 int nodenr;
2422
2423 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2424 if (!strcmp(option, "node")) {
2425 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2426 nodenr = nb_numa_nodes;
2427 } else {
2428 nodenr = strtoull(option, NULL, 10);
2429 }
2430
2431 if (get_param_value(option, 128, "mem", optarg) == 0) {
2432 node_mem[nodenr] = 0;
2433 } else {
2434 value = strtoull(option, &endptr, 0);
2435 switch (*endptr) {
2436 case 0: case 'M': case 'm':
2437 value <<= 20;
2438 break;
2439 case 'G': case 'g':
2440 value <<= 30;
2441 break;
2442 }
2443 node_mem[nodenr] = value;
2444 }
2445 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2446 node_cpumask[nodenr] = 0;
2447 } else {
2448 value = strtoull(option, &endptr, 10);
2449 if (value >= 64) {
2450 value = 63;
2451 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2452 } else {
2453 if (*endptr == '-') {
2454 endvalue = strtoull(endptr+1, &endptr, 10);
2455 if (endvalue >= 63) {
2456 endvalue = 62;
2457 fprintf(stderr,
2458 "only 63 CPUs in NUMA mode supported.\n");
2459 }
2460 value = (1 << (endvalue + 1)) - (1 << value);
2461 } else {
2462 value = 1 << value;
2463 }
2464 }
2465 node_cpumask[nodenr] = value;
2466 }
2467 nb_numa_nodes++;
2468 }
2469 return;
2470}
2471
bellard330d0412003-07-26 18:11:40 +00002472/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002473/* USB devices */
2474
pbrook0d92ed32006-05-21 16:30:15 +00002475static USBPort *used_usb_ports;
2476static USBPort *free_usb_ports;
2477
2478/* ??? Maybe change this to register a hub to keep track of the topology. */
2479void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2480 usb_attachfn attach)
2481{
2482 port->opaque = opaque;
2483 port->index = index;
2484 port->attach = attach;
2485 port->next = free_usb_ports;
2486 free_usb_ports = port;
2487}
2488
aliguori4b096fc2008-08-21 19:28:55 +00002489int usb_device_add_dev(USBDevice *dev)
2490{
2491 USBPort *port;
2492
2493 /* Find a USB port to add the device to. */
2494 port = free_usb_ports;
2495 if (!port->next) {
2496 USBDevice *hub;
2497
2498 /* Create a new hub and chain it on. */
2499 free_usb_ports = NULL;
2500 port->next = used_usb_ports;
2501 used_usb_ports = port;
2502
2503 hub = usb_hub_init(VM_USB_HUB_SIZE);
2504 usb_attach(port, hub);
2505 port = free_usb_ports;
2506 }
2507
2508 free_usb_ports = port->next;
2509 port->next = used_usb_ports;
2510 used_usb_ports = port;
2511 usb_attach(port, dev);
2512 return 0;
2513}
2514
aliguoribb5fc202009-03-05 23:01:15 +00002515static void usb_msd_password_cb(void *opaque, int err)
2516{
2517 USBDevice *dev = opaque;
2518
2519 if (!err)
2520 usb_device_add_dev(dev);
2521 else
2522 dev->handle_destroy(dev);
2523}
2524
aliguoric0f4ce72009-03-05 23:01:01 +00002525static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002526{
2527 const char *p;
2528 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002529
pbrook0d92ed32006-05-21 16:30:15 +00002530 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002531 return -1;
2532
2533 if (strstart(devname, "host:", &p)) {
2534 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002535 } else if (!strcmp(devname, "mouse")) {
2536 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002537 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002538 dev = usb_tablet_init();
2539 } else if (!strcmp(devname, "keyboard")) {
2540 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002541 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002542 BlockDriverState *bs;
2543
aliguoribb5fc202009-03-05 23:01:15 +00002544 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002545 if (!dev)
2546 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002547 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002548 if (bdrv_key_required(bs)) {
2549 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002550 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002551 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2552 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002553 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002554 }
2555 }
balrogf6d2a312007-06-10 19:21:04 +00002556 } else if (!strcmp(devname, "wacom-tablet")) {
2557 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002558 } else if (strstart(devname, "serial:", &p)) {
2559 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002560#ifdef CONFIG_BRLAPI
2561 } else if (!strcmp(devname, "braille")) {
2562 dev = usb_baum_init();
2563#endif
balrog6c9f8862008-07-17 20:47:13 +00002564 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002565 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002566
Jan Kiszka10ae5a72009-05-08 12:34:18 +02002567 if (net_client_init(NULL, "nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002568 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002569 nd_table[nic].model = "usb";
2570 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002571 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2572 dev = usb_bt_init(devname[2] ? hci_init(p) :
2573 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002574 } else {
2575 return -1;
2576 }
pbrook0d92ed32006-05-21 16:30:15 +00002577 if (!dev)
2578 return -1;
2579
aliguori4b096fc2008-08-21 19:28:55 +00002580 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002581}
2582
aliguori1f3870a2008-08-21 19:27:48 +00002583int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002584{
pbrook0d92ed32006-05-21 16:30:15 +00002585 USBPort *port;
2586 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002587 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002588
pbrook0d92ed32006-05-21 16:30:15 +00002589 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002590 return -1;
2591
bellarda594cfb2005-11-06 16:13:29 +00002592 if (bus_num != 0)
2593 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002594
2595 lastp = &used_usb_ports;
2596 port = used_usb_ports;
2597 while (port && port->dev->addr != addr) {
2598 lastp = &port->next;
2599 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002600 }
pbrook0d92ed32006-05-21 16:30:15 +00002601
2602 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002603 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002604
bellard059809e2006-07-19 18:06:15 +00002605 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002606 *lastp = port->next;
2607 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002608 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002609 port->next = free_usb_ports;
2610 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002611 return 0;
2612}
2613
aliguori1f3870a2008-08-21 19:27:48 +00002614static int usb_device_del(const char *devname)
2615{
2616 int bus_num, addr;
2617 const char *p;
2618
aliguori5d0c5752008-09-14 01:07:41 +00002619 if (strstart(devname, "host:", &p))
2620 return usb_host_device_close(p);
2621
aliguori1f3870a2008-08-21 19:27:48 +00002622 if (!used_usb_ports)
2623 return -1;
2624
2625 p = strchr(devname, '.');
2626 if (!p)
2627 return -1;
2628 bus_num = strtoul(devname, NULL, 0);
2629 addr = strtoul(p + 1, NULL, 0);
2630
2631 return usb_device_del_addr(bus_num, addr);
2632}
2633
aliguori376253e2009-03-05 23:01:23 +00002634void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002635{
aliguoric0f4ce72009-03-05 23:01:01 +00002636 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002637}
2638
aliguori376253e2009-03-05 23:01:23 +00002639void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002640{
aliguori4b096fc2008-08-21 19:28:55 +00002641 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002642}
2643
aliguori376253e2009-03-05 23:01:23 +00002644void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002645{
2646 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002647 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002648 const char *speed_str;
2649
pbrook0d92ed32006-05-21 16:30:15 +00002650 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002651 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002652 return;
2653 }
2654
pbrook0d92ed32006-05-21 16:30:15 +00002655 for (port = used_usb_ports; port; port = port->next) {
2656 dev = port->dev;
2657 if (!dev)
2658 continue;
2659 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002660 case USB_SPEED_LOW:
2661 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002662 break;
ths5fafdf22007-09-16 21:08:06 +00002663 case USB_SPEED_FULL:
2664 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002665 break;
ths5fafdf22007-09-16 21:08:06 +00002666 case USB_SPEED_HIGH:
2667 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002668 break;
2669 default:
ths5fafdf22007-09-16 21:08:06 +00002670 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002671 break;
bellarda594cfb2005-11-06 16:13:29 +00002672 }
aliguori376253e2009-03-05 23:01:23 +00002673 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2674 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002675 }
2676}
2677
bellardf7cce892004-12-08 22:21:25 +00002678/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002679/* PCMCIA/Cardbus */
2680
2681static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002682 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002683 struct pcmcia_socket_entry_s *next;
2684} *pcmcia_sockets = 0;
2685
Paul Brookbc24a222009-05-10 01:44:56 +01002686void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002687{
2688 struct pcmcia_socket_entry_s *entry;
2689
2690 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2691 entry->socket = socket;
2692 entry->next = pcmcia_sockets;
2693 pcmcia_sockets = entry;
2694}
2695
Paul Brookbc24a222009-05-10 01:44:56 +01002696void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002697{
2698 struct pcmcia_socket_entry_s *entry, **ptr;
2699
2700 ptr = &pcmcia_sockets;
2701 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2702 if (entry->socket == socket) {
2703 *ptr = entry->next;
2704 qemu_free(entry);
2705 }
2706}
2707
aliguori376253e2009-03-05 23:01:23 +00002708void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002709{
2710 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002711
balrog201a51f2007-04-30 00:51:09 +00002712 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002713 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002714
2715 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002716 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2717 iter->socket->attached ? iter->socket->card_string :
2718 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002719}
2720
2721/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002722/* register display */
2723
aliguori7b5d76d2009-03-13 15:02:13 +00002724struct DisplayAllocator default_allocator = {
2725 defaultallocator_create_displaysurface,
2726 defaultallocator_resize_displaysurface,
2727 defaultallocator_free_displaysurface
2728};
2729
aliguori3023f332009-01-16 19:04:14 +00002730void register_displaystate(DisplayState *ds)
2731{
2732 DisplayState **s;
2733 s = &display_state;
2734 while (*s != NULL)
2735 s = &(*s)->next;
2736 ds->next = NULL;
2737 *s = ds;
2738}
2739
2740DisplayState *get_displaystate(void)
2741{
2742 return display_state;
2743}
2744
aliguori7b5d76d2009-03-13 15:02:13 +00002745DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2746{
2747 if(ds->allocator == &default_allocator) ds->allocator = da;
2748 return ds->allocator;
2749}
2750
ths2ff89792007-06-21 23:34:19 +00002751/* dumb display */
2752
aliguori8f391ab2009-01-19 16:34:10 +00002753static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002754{
aliguori8f391ab2009-01-19 16:34:10 +00002755 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002756 ds->allocator = &default_allocator;
2757 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002758 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002759}
2760
2761/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002762/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002763
bellardc4b1fcc2004-03-14 21:44:30 +00002764typedef struct IOHandlerRecord {
2765 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002766 IOCanRWHandler *fd_read_poll;
2767 IOHandler *fd_read;
2768 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002769 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002770 void *opaque;
2771 /* temporary data */
2772 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002773 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002774} IOHandlerRecord;
2775
bellard8a7ddc32004-03-31 19:00:16 +00002776static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002777
bellard7c9d8e02005-11-15 22:16:05 +00002778/* XXX: fd_read_poll should be suppressed, but an API change is
2779 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002780int qemu_set_fd_handler2(int fd,
2781 IOCanRWHandler *fd_read_poll,
2782 IOHandler *fd_read,
2783 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002784 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002785{
bellard8a7ddc32004-03-31 19:00:16 +00002786 IOHandlerRecord **pioh, *ioh;
2787
bellard7c9d8e02005-11-15 22:16:05 +00002788 if (!fd_read && !fd_write) {
2789 pioh = &first_io_handler;
2790 for(;;) {
2791 ioh = *pioh;
2792 if (ioh == NULL)
2793 break;
2794 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002795 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002796 break;
2797 }
2798 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002799 }
bellard7c9d8e02005-11-15 22:16:05 +00002800 } else {
2801 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2802 if (ioh->fd == fd)
2803 goto found;
2804 }
2805 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002806 ioh->next = first_io_handler;
2807 first_io_handler = ioh;
2808 found:
2809 ioh->fd = fd;
2810 ioh->fd_read_poll = fd_read_poll;
2811 ioh->fd_read = fd_read;
2812 ioh->fd_write = fd_write;
2813 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002814 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002815 }
bellard7c9d8e02005-11-15 22:16:05 +00002816 return 0;
2817}
2818
ths5fafdf22007-09-16 21:08:06 +00002819int qemu_set_fd_handler(int fd,
2820 IOHandler *fd_read,
2821 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002822 void *opaque)
2823{
2824 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002825}
2826
aliguori56f3a5d2008-10-31 18:07:17 +00002827#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002828/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002829/* Polling handling */
2830
2831typedef struct PollingEntry {
2832 PollingFunc *func;
2833 void *opaque;
2834 struct PollingEntry *next;
2835} PollingEntry;
2836
2837static PollingEntry *first_polling_entry;
2838
2839int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2840{
2841 PollingEntry **ppe, *pe;
2842 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002843 pe->func = func;
2844 pe->opaque = opaque;
2845 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2846 *ppe = pe;
2847 return 0;
2848}
2849
2850void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2851{
2852 PollingEntry **ppe, *pe;
2853 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2854 pe = *ppe;
2855 if (pe->func == func && pe->opaque == opaque) {
2856 *ppe = pe->next;
2857 qemu_free(pe);
2858 break;
2859 }
2860 }
2861}
2862
bellarda18e5242006-06-25 17:18:27 +00002863/***********************************************************/
2864/* Wait objects support */
2865typedef struct WaitObjects {
2866 int num;
2867 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2868 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2869 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2870} WaitObjects;
2871
2872static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002873
bellarda18e5242006-06-25 17:18:27 +00002874int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2875{
2876 WaitObjects *w = &wait_objects;
2877
2878 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2879 return -1;
2880 w->events[w->num] = handle;
2881 w->func[w->num] = func;
2882 w->opaque[w->num] = opaque;
2883 w->num++;
2884 return 0;
2885}
2886
2887void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2888{
2889 int i, found;
2890 WaitObjects *w = &wait_objects;
2891
2892 found = 0;
2893 for (i = 0; i < w->num; i++) {
2894 if (w->events[i] == handle)
2895 found = 1;
2896 if (found) {
2897 w->events[i] = w->events[i + 1];
2898 w->func[i] = w->func[i + 1];
2899 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002900 }
bellarda18e5242006-06-25 17:18:27 +00002901 }
2902 if (found)
2903 w->num--;
2904}
2905#endif
2906
bellard8a7ddc32004-03-31 19:00:16 +00002907/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002908/* ram save/restore */
2909
bellard8a7ddc32004-03-31 19:00:16 +00002910static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2911{
2912 int v;
2913
2914 v = qemu_get_byte(f);
2915 switch(v) {
2916 case 0:
2917 if (qemu_get_buffer(f, buf, len) != len)
2918 return -EIO;
2919 break;
2920 case 1:
2921 v = qemu_get_byte(f);
2922 memset(buf, v, len);
2923 break;
2924 default:
2925 return -EINVAL;
2926 }
aliguori871d2f02008-10-13 03:07:56 +00002927
2928 if (qemu_file_has_error(f))
2929 return -EIO;
2930
bellard8a7ddc32004-03-31 19:00:16 +00002931 return 0;
2932}
2933
bellardc88676f2006-08-06 13:36:11 +00002934static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00002935{
aurel3200f82b82008-04-27 21:12:55 +00002936 int ret;
2937 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00002938
pbrook94a6b542009-04-11 17:15:54 +00002939 if (qemu_get_be32(f) != last_ram_offset)
bellard8a7ddc32004-03-31 19:00:16 +00002940 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00002941 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
pbrook5579c7f2009-04-11 14:47:08 +00002942 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
bellard8a7ddc32004-03-31 19:00:16 +00002943 if (ret)
2944 return ret;
2945 }
2946 return 0;
2947}
2948
bellardc88676f2006-08-06 13:36:11 +00002949#define BDRV_HASH_BLOCK_SIZE 1024
2950#define IOBUF_SIZE 4096
2951#define RAM_CBLOCK_MAGIC 0xfabe
2952
bellardc88676f2006-08-06 13:36:11 +00002953typedef struct RamDecompressState {
2954 z_stream zstream;
2955 QEMUFile *f;
2956 uint8_t buf[IOBUF_SIZE];
2957} RamDecompressState;
2958
2959static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
2960{
2961 int ret;
2962 memset(s, 0, sizeof(*s));
2963 s->f = f;
2964 ret = inflateInit(&s->zstream);
2965 if (ret != Z_OK)
2966 return -1;
2967 return 0;
2968}
2969
2970static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
2971{
2972 int ret, clen;
2973
2974 s->zstream.avail_out = len;
2975 s->zstream.next_out = buf;
2976 while (s->zstream.avail_out > 0) {
2977 if (s->zstream.avail_in == 0) {
2978 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
2979 return -1;
2980 clen = qemu_get_be16(s->f);
2981 if (clen > IOBUF_SIZE)
2982 return -1;
2983 qemu_get_buffer(s->f, s->buf, clen);
2984 s->zstream.avail_in = clen;
2985 s->zstream.next_in = s->buf;
2986 }
2987 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
2988 if (ret != Z_OK && ret != Z_STREAM_END) {
2989 return -1;
2990 }
2991 }
2992 return 0;
2993}
2994
2995static void ram_decompress_close(RamDecompressState *s)
2996{
2997 inflateEnd(&s->zstream);
2998}
2999
aliguori475e4272008-10-06 20:21:51 +00003000#define RAM_SAVE_FLAG_FULL 0x01
3001#define RAM_SAVE_FLAG_COMPRESS 0x02
3002#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3003#define RAM_SAVE_FLAG_PAGE 0x08
3004#define RAM_SAVE_FLAG_EOS 0x10
3005
3006static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003007{
aliguori475e4272008-10-06 20:21:51 +00003008 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3009 uint32_t *array = (uint32_t *)page;
3010 int i;
ths3b46e622007-09-17 08:09:54 +00003011
aliguori475e4272008-10-06 20:21:51 +00003012 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3013 if (array[i] != val)
3014 return 0;
bellardc88676f2006-08-06 13:36:11 +00003015 }
aliguori475e4272008-10-06 20:21:51 +00003016
3017 return 1;
bellardc88676f2006-08-06 13:36:11 +00003018}
3019
aliguori475e4272008-10-06 20:21:51 +00003020static int ram_save_block(QEMUFile *f)
3021{
3022 static ram_addr_t current_addr = 0;
3023 ram_addr_t saved_addr = current_addr;
3024 ram_addr_t addr = 0;
3025 int found = 0;
3026
pbrook94a6b542009-04-11 17:15:54 +00003027 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00003028 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00003029 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00003030
3031 cpu_physical_memory_reset_dirty(current_addr,
3032 current_addr + TARGET_PAGE_SIZE,
3033 MIGRATION_DIRTY_FLAG);
3034
pbrook5579c7f2009-04-11 14:47:08 +00003035 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00003036
pbrook5579c7f2009-04-11 14:47:08 +00003037 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00003038 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00003039 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00003040 } else {
3041 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00003042 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003043 }
3044
3045 found = 1;
3046 break;
3047 }
3048 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00003049 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00003050 }
3051
3052 return found;
3053}
3054
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003055static uint64_t bytes_transferred = 0;
aliguori475e4272008-10-06 20:21:51 +00003056
3057static ram_addr_t ram_save_remaining(void)
3058{
3059 ram_addr_t addr;
3060 ram_addr_t count = 0;
3061
pbrook94a6b542009-04-11 17:15:54 +00003062 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003063 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3064 count++;
3065 }
3066
3067 return count;
3068}
3069
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003070uint64_t ram_bytes_remaining(void)
3071{
3072 return ram_save_remaining() * TARGET_PAGE_SIZE;
3073}
3074
3075uint64_t ram_bytes_transferred(void)
3076{
3077 return bytes_transferred;
3078}
3079
3080uint64_t ram_bytes_total(void)
3081{
3082 return last_ram_offset;
3083}
3084
aliguori475e4272008-10-06 20:21:51 +00003085static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3086{
3087 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003088 uint64_t bytes_transferred_last;
3089 double bwidth = 0;
3090 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00003091
Jan Kiszka9fa06382009-05-22 23:51:45 +02003092 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003093 qemu_file_set_error(f);
3094 return 0;
3095 }
3096
aliguori475e4272008-10-06 20:21:51 +00003097 if (stage == 1) {
3098 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003099 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003100 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3101 cpu_physical_memory_set_dirty(addr);
3102 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003103
aliguori475e4272008-10-06 20:21:51 +00003104 /* Enable dirty memory tracking */
3105 cpu_physical_memory_set_dirty_tracking(1);
3106
pbrook94a6b542009-04-11 17:15:54 +00003107 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003108 }
3109
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003110 bytes_transferred_last = bytes_transferred;
3111 bwidth = get_clock();
3112
aliguori475e4272008-10-06 20:21:51 +00003113 while (!qemu_file_rate_limit(f)) {
3114 int ret;
3115
3116 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003117 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003118 if (ret == 0) /* no more blocks */
3119 break;
3120 }
3121
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003122 bwidth = get_clock() - bwidth;
3123 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3124
3125 /* if we haven't transferred anything this round, force expected_time to a
3126 * a very high value, but without crashing */
3127 if (bwidth == 0)
3128 bwidth = 0.000001;
3129
aliguori475e4272008-10-06 20:21:51 +00003130 /* try transferring iterative blocks of memory */
3131
3132 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003133
3134 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003135 while (ram_save_block(f) != 0) {
3136 bytes_transferred += TARGET_PAGE_SIZE;
3137 }
aliguori8215e912009-04-05 19:30:55 +00003138 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003139 }
3140
3141 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3142
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003143 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3144
3145 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003146}
3147
3148static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003149{
3150 RamDecompressState s1, *s = &s1;
3151 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003152 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003153
bellardc88676f2006-08-06 13:36:11 +00003154 if (ram_decompress_open(s, f) < 0)
3155 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003156 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
bellardc88676f2006-08-06 13:36:11 +00003157 if (ram_decompress_buf(s, buf, 1) < 0) {
3158 fprintf(stderr, "Error while reading ram block header\n");
3159 goto error;
3160 }
3161 if (buf[0] == 0) {
pbrook5579c7f2009-04-11 14:47:08 +00003162 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3163 BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003164 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003165 goto error;
3166 }
aliguori475e4272008-10-06 20:21:51 +00003167 } else {
bellardc88676f2006-08-06 13:36:11 +00003168 error:
3169 printf("Error block header\n");
3170 return -EINVAL;
3171 }
3172 }
3173 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003174
3175 return 0;
3176}
3177
3178static int ram_load(QEMUFile *f, void *opaque, int version_id)
3179{
3180 ram_addr_t addr;
3181 int flags;
3182
3183 if (version_id == 1)
3184 return ram_load_v1(f, opaque);
3185
3186 if (version_id == 2) {
pbrook94a6b542009-04-11 17:15:54 +00003187 if (qemu_get_be32(f) != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003188 return -EINVAL;
3189 return ram_load_dead(f, opaque);
3190 }
3191
3192 if (version_id != 3)
3193 return -EINVAL;
3194
3195 do {
3196 addr = qemu_get_be64(f);
3197
3198 flags = addr & ~TARGET_PAGE_MASK;
3199 addr &= TARGET_PAGE_MASK;
3200
3201 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003202 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003203 return -EINVAL;
3204 }
3205
3206 if (flags & RAM_SAVE_FLAG_FULL) {
3207 if (ram_load_dead(f, opaque) < 0)
3208 return -EINVAL;
3209 }
3210
3211 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3212 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003213 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3214#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003215 if (ch == 0 &&
3216 (!kvm_enabled() || kvm_has_sync_mmu())) {
3217 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003218 }
Anthony Liguori30868442009-06-17 16:46:12 -05003219#endif
aliguori475e4272008-10-06 20:21:51 +00003220 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003221 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003222 } while (!(flags & RAM_SAVE_FLAG_EOS));
3223
bellardc88676f2006-08-06 13:36:11 +00003224 return 0;
3225}
3226
aliguori9e472e12008-10-08 19:50:24 +00003227void qemu_service_io(void)
3228{
aliguorid9f75a42009-04-24 18:03:11 +00003229 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003230}
3231
bellard8a7ddc32004-03-31 19:00:16 +00003232/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003233/* bottom halves (can be seen as timers which expire ASAP) */
3234
3235struct QEMUBH {
3236 QEMUBHFunc *cb;
3237 void *opaque;
3238 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003239 int idle;
3240 int deleted;
bellard83f64092006-08-01 16:21:11 +00003241 QEMUBH *next;
3242};
3243
3244static QEMUBH *first_bh = NULL;
3245
3246QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3247{
3248 QEMUBH *bh;
3249 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003250 bh->cb = cb;
3251 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003252 bh->next = first_bh;
3253 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003254 return bh;
3255}
3256
bellard6eb57332006-08-06 09:51:25 +00003257int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003258{
aliguori1b435b12008-10-31 17:24:21 +00003259 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003260 int ret;
bellard83f64092006-08-01 16:21:11 +00003261
bellard6eb57332006-08-06 09:51:25 +00003262 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003263 for (bh = first_bh; bh; bh = bh->next) {
3264 if (!bh->deleted && bh->scheduled) {
3265 bh->scheduled = 0;
3266 if (!bh->idle)
3267 ret = 1;
3268 bh->idle = 0;
3269 bh->cb(bh->opaque);
3270 }
bellard83f64092006-08-01 16:21:11 +00003271 }
aliguori1b435b12008-10-31 17:24:21 +00003272
3273 /* remove deleted bhs */
3274 bhp = &first_bh;
3275 while (*bhp) {
3276 bh = *bhp;
3277 if (bh->deleted) {
3278 *bhp = bh->next;
3279 qemu_free(bh);
3280 } else
3281 bhp = &bh->next;
3282 }
3283
bellard6eb57332006-08-06 09:51:25 +00003284 return ret;
bellard83f64092006-08-01 16:21:11 +00003285}
3286
aliguori1b435b12008-10-31 17:24:21 +00003287void qemu_bh_schedule_idle(QEMUBH *bh)
3288{
3289 if (bh->scheduled)
3290 return;
3291 bh->scheduled = 1;
3292 bh->idle = 1;
3293}
3294
bellard83f64092006-08-01 16:21:11 +00003295void qemu_bh_schedule(QEMUBH *bh)
3296{
bellard83f64092006-08-01 16:21:11 +00003297 if (bh->scheduled)
3298 return;
3299 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003300 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003301 /* stop the currently executing CPU to execute the BH ASAP */
aliguorid9f75a42009-04-24 18:03:11 +00003302 qemu_notify_event();
bellard83f64092006-08-01 16:21:11 +00003303}
3304
3305void qemu_bh_cancel(QEMUBH *bh)
3306{
aliguori1b435b12008-10-31 17:24:21 +00003307 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003308}
3309
3310void qemu_bh_delete(QEMUBH *bh)
3311{
aliguori1b435b12008-10-31 17:24:21 +00003312 bh->scheduled = 0;
3313 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003314}
3315
aliguori56f3a5d2008-10-31 18:07:17 +00003316static void qemu_bh_update_timeout(int *timeout)
3317{
3318 QEMUBH *bh;
3319
3320 for (bh = first_bh; bh; bh = bh->next) {
3321 if (!bh->deleted && bh->scheduled) {
3322 if (bh->idle) {
3323 /* idle bottom halves will be polled at least
3324 * every 10ms */
3325 *timeout = MIN(10, *timeout);
3326 } else {
3327 /* non-idle bottom halves will be executed
3328 * immediately */
3329 *timeout = 0;
3330 break;
3331 }
3332 }
3333 }
3334}
3335
bellard83f64092006-08-01 16:21:11 +00003336/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003337/* machine registration */
3338
blueswir1bdaf78e2008-10-04 07:24:27 +00003339static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003340QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003341
3342int qemu_register_machine(QEMUMachine *m)
3343{
3344 QEMUMachine **pm;
3345 pm = &first_machine;
3346 while (*pm != NULL)
3347 pm = &(*pm)->next;
3348 m->next = NULL;
3349 *pm = m;
3350 return 0;
3351}
3352
pbrook9596ebb2007-11-18 01:44:38 +00003353static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003354{
3355 QEMUMachine *m;
3356
3357 for(m = first_machine; m != NULL; m = m->next) {
3358 if (!strcmp(m->name, name))
3359 return m;
3360 }
3361 return NULL;
3362}
3363
Anthony Liguori0c257432009-05-21 20:41:01 -05003364static QEMUMachine *find_default_machine(void)
3365{
3366 QEMUMachine *m;
3367
3368 for(m = first_machine; m != NULL; m = m->next) {
3369 if (m->is_default) {
3370 return m;
3371 }
3372 }
3373 return NULL;
3374}
3375
bellardcc1daa42005-06-05 14:49:17 +00003376/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003377/* main execution loop */
3378
pbrook9596ebb2007-11-18 01:44:38 +00003379static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003380{
aliguori7d957bd2009-01-15 22:14:11 +00003381 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003382 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003383 DisplayChangeListener *dcl = ds->listeners;
3384
3385 dpy_refresh(ds);
3386
3387 while (dcl != NULL) {
3388 if (dcl->gui_timer_interval &&
3389 dcl->gui_timer_interval < interval)
3390 interval = dcl->gui_timer_interval;
3391 dcl = dcl->next;
3392 }
3393 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003394}
3395
blueswir19043b622009-01-21 19:28:13 +00003396static void nographic_update(void *opaque)
3397{
3398 uint64_t interval = GUI_REFRESH_INTERVAL;
3399
3400 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3401}
3402
bellard0bd48852005-11-11 00:00:47 +00003403struct vm_change_state_entry {
3404 VMChangeStateHandler *cb;
3405 void *opaque;
3406 LIST_ENTRY (vm_change_state_entry) entries;
3407};
3408
3409static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3410
3411VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3412 void *opaque)
3413{
3414 VMChangeStateEntry *e;
3415
3416 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003417
3418 e->cb = cb;
3419 e->opaque = opaque;
3420 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3421 return e;
3422}
3423
3424void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3425{
3426 LIST_REMOVE (e, entries);
3427 qemu_free (e);
3428}
3429
aliguori9781e042009-01-22 17:15:29 +00003430static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003431{
3432 VMChangeStateEntry *e;
3433
3434 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003435 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003436 }
3437}
3438
aliguorid6dc3d42009-04-24 18:04:07 +00003439static void resume_all_vcpus(void);
3440static void pause_all_vcpus(void);
3441
bellard8a7ddc32004-03-31 19:00:16 +00003442void vm_start(void)
3443{
3444 if (!vm_running) {
3445 cpu_enable_ticks();
3446 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003447 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003448 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003449 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003450 }
3451}
3452
bellardbb0c6722004-06-20 12:37:32 +00003453/* reset/shutdown handler */
3454
3455typedef struct QEMUResetEntry {
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003456 TAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003457 QEMUResetHandler *func;
3458 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003459} QEMUResetEntry;
3460
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003461static TAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3462 TAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003463static int reset_requested;
3464static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003465static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003466static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003467static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003468
aurel32cf7a2fe2008-03-18 06:53:05 +00003469int qemu_shutdown_requested(void)
3470{
3471 int r = shutdown_requested;
3472 shutdown_requested = 0;
3473 return r;
3474}
3475
3476int qemu_reset_requested(void)
3477{
3478 int r = reset_requested;
3479 reset_requested = 0;
3480 return r;
3481}
3482
3483int qemu_powerdown_requested(void)
3484{
3485 int r = powerdown_requested;
3486 powerdown_requested = 0;
3487 return r;
3488}
3489
aliguorie5689022009-04-24 18:03:54 +00003490static int qemu_debug_requested(void)
3491{
3492 int r = debug_requested;
3493 debug_requested = 0;
3494 return r;
3495}
3496
aliguori6e29f5d2009-04-24 18:04:02 +00003497static int qemu_vmstop_requested(void)
3498{
3499 int r = vmstop_requested;
3500 vmstop_requested = 0;
3501 return r;
3502}
3503
3504static void do_vm_stop(int reason)
3505{
3506 if (vm_running) {
3507 cpu_disable_ticks();
3508 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003509 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003510 vm_state_notify(0, reason);
3511 }
3512}
3513
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003514void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003515{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003516 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003517
bellardbb0c6722004-06-20 12:37:32 +00003518 re->func = func;
3519 re->opaque = opaque;
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003520 TAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003521}
3522
Jan Kiszkadda9b292009-07-02 00:19:02 +02003523void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003524{
3525 QEMUResetEntry *re;
3526
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003527 TAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003528 if (re->func == func && re->opaque == opaque) {
3529 TAILQ_REMOVE(&reset_handlers, re, entry);
3530 qemu_free(re);
3531 return;
3532 }
3533 }
3534}
3535
3536void qemu_system_reset(void)
3537{
3538 QEMUResetEntry *re, *nre;
3539
3540 /* reset all devices */
3541 TAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003542 re->func(re->opaque);
3543 }
3544}
3545
3546void qemu_system_reset_request(void)
3547{
bellardd1beab82006-10-02 19:44:22 +00003548 if (no_reboot) {
3549 shutdown_requested = 1;
3550 } else {
3551 reset_requested = 1;
3552 }
aliguorid9f75a42009-04-24 18:03:11 +00003553 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003554}
3555
3556void qemu_system_shutdown_request(void)
3557{
3558 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003559 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003560}
3561
bellard34751872005-07-02 14:31:34 +00003562void qemu_system_powerdown_request(void)
3563{
3564 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003565 qemu_notify_event();
3566}
3567
aliguorid6dc3d42009-04-24 18:04:07 +00003568#ifdef CONFIG_IOTHREAD
3569static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003570{
aliguorid6dc3d42009-04-24 18:04:07 +00003571 vmstop_requested = reason;
3572 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003573}
aliguorid6dc3d42009-04-24 18:04:07 +00003574#endif
bellardbb0c6722004-06-20 12:37:32 +00003575
aliguori50317c72009-04-24 18:03:29 +00003576#ifndef _WIN32
3577static int io_thread_fd = -1;
3578
3579static void qemu_event_increment(void)
3580{
3581 static const char byte = 0;
3582
3583 if (io_thread_fd == -1)
3584 return;
3585
3586 write(io_thread_fd, &byte, sizeof(byte));
3587}
3588
3589static void qemu_event_read(void *opaque)
3590{
3591 int fd = (unsigned long)opaque;
3592 ssize_t len;
3593
3594 /* Drain the notify pipe */
3595 do {
3596 char buffer[512];
3597 len = read(fd, buffer, sizeof(buffer));
3598 } while ((len == -1 && errno == EINTR) || len > 0);
3599}
3600
3601static int qemu_event_init(void)
3602{
3603 int err;
3604 int fds[2];
3605
3606 err = pipe(fds);
3607 if (err == -1)
3608 return -errno;
3609
3610 err = fcntl_setfl(fds[0], O_NONBLOCK);
3611 if (err < 0)
3612 goto fail;
3613
3614 err = fcntl_setfl(fds[1], O_NONBLOCK);
3615 if (err < 0)
3616 goto fail;
3617
3618 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3619 (void *)(unsigned long)fds[0]);
3620
3621 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003622 return 0;
3623
aliguori50317c72009-04-24 18:03:29 +00003624fail:
3625 close(fds[0]);
3626 close(fds[1]);
3627 return err;
3628}
3629#else
3630HANDLE qemu_event_handle;
3631
3632static void dummy_event_handler(void *opaque)
3633{
3634}
3635
3636static int qemu_event_init(void)
3637{
3638 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3639 if (!qemu_event_handle) {
3640 perror("Failed CreateEvent");
3641 return -1;
3642 }
3643 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3644 return 0;
3645}
3646
3647static void qemu_event_increment(void)
3648{
3649 SetEvent(qemu_event_handle);
3650}
3651#endif
3652
aliguorid6dc3d42009-04-24 18:04:07 +00003653static int cpu_can_run(CPUState *env)
3654{
3655 if (env->stop)
3656 return 0;
3657 if (env->stopped)
3658 return 0;
3659 return 1;
3660}
3661
3662#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003663static int qemu_init_main_loop(void)
3664{
aliguori50317c72009-04-24 18:03:29 +00003665 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003666}
3667
aliguori0bf46a42009-04-24 18:03:41 +00003668void qemu_init_vcpu(void *_env)
3669{
3670 CPUState *env = _env;
3671
3672 if (kvm_enabled())
3673 kvm_init_vcpu(env);
3674 return;
3675}
3676
aliguori8edac962009-04-24 18:03:45 +00003677int qemu_cpu_self(void *env)
3678{
3679 return 1;
3680}
3681
aliguorid6dc3d42009-04-24 18:04:07 +00003682static void resume_all_vcpus(void)
3683{
3684}
3685
3686static void pause_all_vcpus(void)
3687{
3688}
3689
aliguori8edac962009-04-24 18:03:45 +00003690void qemu_cpu_kick(void *env)
3691{
3692 return;
3693}
3694
aliguorid6dc3d42009-04-24 18:04:07 +00003695void qemu_notify_event(void)
3696{
3697 CPUState *env = cpu_single_env;
3698
3699 if (env) {
3700 cpu_exit(env);
3701#ifdef USE_KQEMU
3702 if (env->kqemu_enabled)
3703 kqemu_cpu_interrupt(env);
3704#endif
3705 }
3706}
3707
aliguori48708522009-04-24 18:03:49 +00003708#define qemu_mutex_lock_iothread() do { } while (0)
3709#define qemu_mutex_unlock_iothread() do { } while (0)
3710
aliguori6e29f5d2009-04-24 18:04:02 +00003711void vm_stop(int reason)
3712{
3713 do_vm_stop(reason);
3714}
3715
aliguorid6dc3d42009-04-24 18:04:07 +00003716#else /* CONFIG_IOTHREAD */
3717
3718#include "qemu-thread.h"
3719
3720QemuMutex qemu_global_mutex;
3721static QemuMutex qemu_fair_mutex;
3722
3723static QemuThread io_thread;
3724
3725static QemuThread *tcg_cpu_thread;
3726static QemuCond *tcg_halt_cond;
3727
3728static int qemu_system_ready;
3729/* cpu creation */
3730static QemuCond qemu_cpu_cond;
3731/* system init */
3732static QemuCond qemu_system_cond;
3733static QemuCond qemu_pause_cond;
3734
3735static void block_io_signals(void);
3736static void unblock_io_signals(void);
3737static int tcg_has_work(void);
3738
3739static int qemu_init_main_loop(void)
3740{
3741 int ret;
3742
3743 ret = qemu_event_init();
3744 if (ret)
3745 return ret;
3746
3747 qemu_cond_init(&qemu_pause_cond);
3748 qemu_mutex_init(&qemu_fair_mutex);
3749 qemu_mutex_init(&qemu_global_mutex);
3750 qemu_mutex_lock(&qemu_global_mutex);
3751
3752 unblock_io_signals();
3753 qemu_thread_self(&io_thread);
3754
3755 return 0;
3756}
3757
3758static void qemu_wait_io_event(CPUState *env)
3759{
3760 while (!tcg_has_work())
3761 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3762
3763 qemu_mutex_unlock(&qemu_global_mutex);
3764
3765 /*
3766 * Users of qemu_global_mutex can be starved, having no chance
3767 * to acquire it since this path will get to it first.
3768 * So use another lock to provide fairness.
3769 */
3770 qemu_mutex_lock(&qemu_fair_mutex);
3771 qemu_mutex_unlock(&qemu_fair_mutex);
3772
3773 qemu_mutex_lock(&qemu_global_mutex);
3774 if (env->stop) {
3775 env->stop = 0;
3776 env->stopped = 1;
3777 qemu_cond_signal(&qemu_pause_cond);
3778 }
3779}
3780
3781static int qemu_cpu_exec(CPUState *env);
3782
3783static void *kvm_cpu_thread_fn(void *arg)
3784{
3785 CPUState *env = arg;
3786
3787 block_io_signals();
3788 qemu_thread_self(env->thread);
3789
3790 /* signal CPU creation */
3791 qemu_mutex_lock(&qemu_global_mutex);
3792 env->created = 1;
3793 qemu_cond_signal(&qemu_cpu_cond);
3794
3795 /* and wait for machine initialization */
3796 while (!qemu_system_ready)
3797 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3798
3799 while (1) {
3800 if (cpu_can_run(env))
3801 qemu_cpu_exec(env);
3802 qemu_wait_io_event(env);
3803 }
3804
3805 return NULL;
3806}
3807
3808static void tcg_cpu_exec(void);
3809
3810static void *tcg_cpu_thread_fn(void *arg)
3811{
3812 CPUState *env = arg;
3813
3814 block_io_signals();
3815 qemu_thread_self(env->thread);
3816
3817 /* signal CPU creation */
3818 qemu_mutex_lock(&qemu_global_mutex);
3819 for (env = first_cpu; env != NULL; env = env->next_cpu)
3820 env->created = 1;
3821 qemu_cond_signal(&qemu_cpu_cond);
3822
3823 /* and wait for machine initialization */
3824 while (!qemu_system_ready)
3825 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3826
3827 while (1) {
3828 tcg_cpu_exec();
3829 qemu_wait_io_event(cur_cpu);
3830 }
3831
3832 return NULL;
3833}
3834
3835void qemu_cpu_kick(void *_env)
3836{
3837 CPUState *env = _env;
3838 qemu_cond_broadcast(env->halt_cond);
3839 if (kvm_enabled())
3840 qemu_thread_signal(env->thread, SIGUSR1);
3841}
3842
3843int qemu_cpu_self(void *env)
3844{
3845 return (cpu_single_env != NULL);
3846}
3847
3848static void cpu_signal(int sig)
3849{
3850 if (cpu_single_env)
3851 cpu_exit(cpu_single_env);
3852}
3853
3854static void block_io_signals(void)
3855{
3856 sigset_t set;
3857 struct sigaction sigact;
3858
3859 sigemptyset(&set);
3860 sigaddset(&set, SIGUSR2);
3861 sigaddset(&set, SIGIO);
3862 sigaddset(&set, SIGALRM);
3863 pthread_sigmask(SIG_BLOCK, &set, NULL);
3864
3865 sigemptyset(&set);
3866 sigaddset(&set, SIGUSR1);
3867 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3868
3869 memset(&sigact, 0, sizeof(sigact));
3870 sigact.sa_handler = cpu_signal;
3871 sigaction(SIGUSR1, &sigact, NULL);
3872}
3873
3874static void unblock_io_signals(void)
3875{
3876 sigset_t set;
3877
3878 sigemptyset(&set);
3879 sigaddset(&set, SIGUSR2);
3880 sigaddset(&set, SIGIO);
3881 sigaddset(&set, SIGALRM);
3882 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3883
3884 sigemptyset(&set);
3885 sigaddset(&set, SIGUSR1);
3886 pthread_sigmask(SIG_BLOCK, &set, NULL);
3887}
3888
3889static void qemu_signal_lock(unsigned int msecs)
3890{
3891 qemu_mutex_lock(&qemu_fair_mutex);
3892
3893 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3894 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3895 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3896 break;
3897 }
3898 qemu_mutex_unlock(&qemu_fair_mutex);
3899}
3900
3901static void qemu_mutex_lock_iothread(void)
3902{
3903 if (kvm_enabled()) {
3904 qemu_mutex_lock(&qemu_fair_mutex);
3905 qemu_mutex_lock(&qemu_global_mutex);
3906 qemu_mutex_unlock(&qemu_fair_mutex);
3907 } else
3908 qemu_signal_lock(100);
3909}
3910
3911static void qemu_mutex_unlock_iothread(void)
3912{
3913 qemu_mutex_unlock(&qemu_global_mutex);
3914}
3915
3916static int all_vcpus_paused(void)
3917{
3918 CPUState *penv = first_cpu;
3919
3920 while (penv) {
3921 if (!penv->stopped)
3922 return 0;
3923 penv = (CPUState *)penv->next_cpu;
3924 }
3925
3926 return 1;
3927}
3928
3929static void pause_all_vcpus(void)
3930{
3931 CPUState *penv = first_cpu;
3932
3933 while (penv) {
3934 penv->stop = 1;
3935 qemu_thread_signal(penv->thread, SIGUSR1);
3936 qemu_cpu_kick(penv);
3937 penv = (CPUState *)penv->next_cpu;
3938 }
3939
3940 while (!all_vcpus_paused()) {
3941 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3942 penv = first_cpu;
3943 while (penv) {
3944 qemu_thread_signal(penv->thread, SIGUSR1);
3945 penv = (CPUState *)penv->next_cpu;
3946 }
3947 }
3948}
3949
3950static void resume_all_vcpus(void)
3951{
3952 CPUState *penv = first_cpu;
3953
3954 while (penv) {
3955 penv->stop = 0;
3956 penv->stopped = 0;
3957 qemu_thread_signal(penv->thread, SIGUSR1);
3958 qemu_cpu_kick(penv);
3959 penv = (CPUState *)penv->next_cpu;
3960 }
3961}
3962
3963static void tcg_init_vcpu(void *_env)
3964{
3965 CPUState *env = _env;
3966 /* share a single thread for all cpus with TCG */
3967 if (!tcg_cpu_thread) {
3968 env->thread = qemu_mallocz(sizeof(QemuThread));
3969 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3970 qemu_cond_init(env->halt_cond);
3971 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3972 while (env->created == 0)
3973 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3974 tcg_cpu_thread = env->thread;
3975 tcg_halt_cond = env->halt_cond;
3976 } else {
3977 env->thread = tcg_cpu_thread;
3978 env->halt_cond = tcg_halt_cond;
3979 }
3980}
3981
3982static void kvm_start_vcpu(CPUState *env)
3983{
3984 kvm_init_vcpu(env);
3985 env->thread = qemu_mallocz(sizeof(QemuThread));
3986 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3987 qemu_cond_init(env->halt_cond);
3988 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3989 while (env->created == 0)
3990 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3991}
3992
3993void qemu_init_vcpu(void *_env)
3994{
3995 CPUState *env = _env;
3996
3997 if (kvm_enabled())
3998 kvm_start_vcpu(env);
3999 else
4000 tcg_init_vcpu(env);
4001}
4002
4003void qemu_notify_event(void)
4004{
4005 qemu_event_increment();
4006}
4007
4008void vm_stop(int reason)
4009{
4010 QemuThread me;
4011 qemu_thread_self(&me);
4012
4013 if (!qemu_thread_equal(&me, &io_thread)) {
4014 qemu_system_vmstop_request(reason);
4015 /*
4016 * FIXME: should not return to device code in case
4017 * vm_stop() has been requested.
4018 */
4019 if (cpu_single_env) {
4020 cpu_exit(cpu_single_env);
4021 cpu_single_env->stop = 1;
4022 }
4023 return;
4024 }
4025 do_vm_stop(reason);
4026}
4027
4028#endif
4029
4030
ths877cf882007-04-18 18:11:47 +00004031#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00004032static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00004033{
4034 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00004035 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00004036
bellardf3311102006-04-12 20:21:17 +00004037
4038 /* XXX: need to suppress polling by better using win32 events */
4039 ret = 0;
4040 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4041 ret |= pe->func(pe->opaque);
4042 }
thse6b1e552007-04-18 17:56:02 +00004043 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00004044 int err;
4045 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00004046
aliguori56f3a5d2008-10-31 18:07:17 +00004047 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00004048 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4049 if (w->func[ret - WAIT_OBJECT_0])
4050 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00004051
ths5fafdf22007-09-16 21:08:06 +00004052 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00004053 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00004054
thse6b1e552007-04-18 17:56:02 +00004055 /* Check if event is signaled */
4056 ret2 = WaitForSingleObject(w->events[i], 0);
4057 if(ret2 == WAIT_OBJECT_0) {
4058 if (w->func[i])
4059 w->func[i](w->opaque[i]);
4060 } else if (ret2 == WAIT_TIMEOUT) {
4061 } else {
4062 err = GetLastError();
4063 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00004064 }
4065 }
bellarda18e5242006-06-25 17:18:27 +00004066 } else if (ret == WAIT_TIMEOUT) {
4067 } else {
4068 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00004069 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00004070 }
bellardf3311102006-04-12 20:21:17 +00004071 }
aliguori56f3a5d2008-10-31 18:07:17 +00004072
4073 *timeout = 0;
4074}
4075#else
blueswir169d64512008-12-07 19:30:18 +00004076static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00004077{
4078}
bellardfd1dff42006-02-01 21:29:26 +00004079#endif
aliguori56f3a5d2008-10-31 18:07:17 +00004080
4081void main_loop_wait(int timeout)
4082{
4083 IOHandlerRecord *ioh;
4084 fd_set rfds, wfds, xfds;
4085 int ret, nfds;
4086 struct timeval tv;
4087
4088 qemu_bh_update_timeout(&timeout);
4089
4090 host_main_loop_wait(&timeout);
4091
bellardfd1dff42006-02-01 21:29:26 +00004092 /* poll any events */
4093 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00004094 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00004095 FD_ZERO(&rfds);
4096 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00004097 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00004098 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00004099 if (ioh->deleted)
4100 continue;
bellardfd1dff42006-02-01 21:29:26 +00004101 if (ioh->fd_read &&
4102 (!ioh->fd_read_poll ||
4103 ioh->fd_read_poll(ioh->opaque) != 0)) {
4104 FD_SET(ioh->fd, &rfds);
4105 if (ioh->fd > nfds)
4106 nfds = ioh->fd;
4107 }
4108 if (ioh->fd_write) {
4109 FD_SET(ioh->fd, &wfds);
4110 if (ioh->fd > nfds)
4111 nfds = ioh->fd;
4112 }
4113 }
ths3b46e622007-09-17 08:09:54 +00004114
aliguori56f3a5d2008-10-31 18:07:17 +00004115 tv.tv_sec = timeout / 1000;
4116 tv.tv_usec = (timeout % 1000) * 1000;
4117
Jan Kiszkad918f232009-06-24 14:42:30 +02004118 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4119
aliguori48708522009-04-24 18:03:49 +00004120 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00004121 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00004122 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00004123 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00004124 IOHandlerRecord **pioh;
4125
4126 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00004127 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004128 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00004129 }
ths6ab43fd2007-08-25 01:34:19 +00004130 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004131 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00004132 }
4133 }
thscafffd42007-02-28 21:59:44 +00004134
4135 /* remove deleted IO handlers */
4136 pioh = &first_io_handler;
4137 while (*pioh) {
4138 ioh = *pioh;
4139 if (ioh->deleted) {
4140 *pioh = ioh->next;
4141 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00004142 } else
thscafffd42007-02-28 21:59:44 +00004143 pioh = &ioh->next;
4144 }
bellardfd1dff42006-02-01 21:29:26 +00004145 }
Jan Kiszkad918f232009-06-24 14:42:30 +02004146
4147 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00004148
aliguori50317c72009-04-24 18:03:29 +00004149 /* rearm timer, if not periodic */
4150 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4151 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4152 qemu_rearm_alarm_timer(alarm_timer);
4153 }
4154
aliguori357c6922008-11-25 17:26:09 +00004155 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00004156 if (vm_running) {
4157 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
4158 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
4159 qemu_get_clock(vm_clock));
4160 }
aliguori357c6922008-11-25 17:26:09 +00004161
4162 /* real time timers */
4163 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
4164 qemu_get_clock(rt_clock));
4165
pbrook423f0742007-05-23 00:06:54 +00004166 /* Check bottom-halves last in case any of the earlier events triggered
4167 them. */
4168 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00004169
bellard5905b2e2004-08-01 21:53:26 +00004170}
4171
aliguori43b96852009-04-24 18:03:33 +00004172static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00004173{
aliguori43b96852009-04-24 18:03:33 +00004174 int ret;
bellard89bfc102006-02-08 22:46:31 +00004175#ifdef CONFIG_PROFILER
4176 int64_t ti;
4177#endif
aliguori43b96852009-04-24 18:03:33 +00004178
4179#ifdef CONFIG_PROFILER
4180 ti = profile_getclock();
4181#endif
4182 if (use_icount) {
4183 int64_t count;
4184 int decr;
4185 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4186 env->icount_decr.u16.low = 0;
4187 env->icount_extra = 0;
4188 count = qemu_next_deadline();
4189 count = (count + (1 << icount_time_shift) - 1)
4190 >> icount_time_shift;
4191 qemu_icount += count;
4192 decr = (count > 0xffff) ? 0xffff : count;
4193 count -= decr;
4194 env->icount_decr.u16.low = decr;
4195 env->icount_extra = count;
4196 }
4197 ret = cpu_exec(env);
4198#ifdef CONFIG_PROFILER
4199 qemu_time += profile_getclock() - ti;
4200#endif
4201 if (use_icount) {
4202 /* Fold pending instructions back into the
4203 instruction counter, and clear the interrupt flag. */
4204 qemu_icount -= (env->icount_decr.u16.low
4205 + env->icount_extra);
4206 env->icount_decr.u32 = 0;
4207 env->icount_extra = 0;
4208 }
4209 return ret;
4210}
4211
aliguorie6e35b12009-04-24 18:03:57 +00004212static void tcg_cpu_exec(void)
4213{
aliguorid6dc3d42009-04-24 18:04:07 +00004214 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00004215
4216 if (next_cpu == NULL)
4217 next_cpu = first_cpu;
4218 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4219 CPUState *env = cur_cpu = next_cpu;
4220
4221 if (!vm_running)
4222 break;
4223 if (timer_alarm_pending) {
4224 timer_alarm_pending = 0;
4225 break;
4226 }
aliguorid6dc3d42009-04-24 18:04:07 +00004227 if (cpu_can_run(env))
4228 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004229 if (ret == EXCP_DEBUG) {
4230 gdb_set_stop_cpu(env);
4231 debug_requested = 1;
4232 break;
4233 }
4234 }
4235}
4236
aliguori43b96852009-04-24 18:03:33 +00004237static int cpu_has_work(CPUState *env)
4238{
aliguorid6dc3d42009-04-24 18:04:07 +00004239 if (env->stop)
4240 return 1;
4241 if (env->stopped)
4242 return 0;
aliguori43b96852009-04-24 18:03:33 +00004243 if (!env->halted)
4244 return 1;
4245 if (qemu_cpu_has_work(env))
4246 return 1;
4247 return 0;
4248}
4249
4250static int tcg_has_work(void)
4251{
bellard6a00d602005-11-21 23:25:50 +00004252 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004253
aliguori43b96852009-04-24 18:03:33 +00004254 for (env = first_cpu; env != NULL; env = env->next_cpu)
4255 if (cpu_has_work(env))
4256 return 1;
4257 return 0;
4258}
bellard15a76442005-11-23 21:01:03 +00004259
aliguori43b96852009-04-24 18:03:33 +00004260static int qemu_calculate_timeout(void)
4261{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004262#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004263 int timeout;
bellard15a76442005-11-23 21:01:03 +00004264
aliguori43b96852009-04-24 18:03:33 +00004265 if (!vm_running)
4266 timeout = 5000;
4267 else if (tcg_has_work())
4268 timeout = 0;
4269 else if (!use_icount)
4270 timeout = 5000;
4271 else {
4272 /* XXX: use timeout computed from timers */
4273 int64_t add;
4274 int64_t delta;
4275 /* Advance virtual time to the next event. */
4276 if (use_icount == 1) {
4277 /* When not using an adaptive execution frequency
4278 we tend to get badly out of sync with real time,
4279 so just delay for a reasonable amount of time. */
4280 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004281 } else {
aliguori43b96852009-04-24 18:03:33 +00004282 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004283 }
aliguori43b96852009-04-24 18:03:33 +00004284 if (delta > 0) {
4285 /* If virtual time is ahead of real time then just
4286 wait for IO. */
4287 timeout = (delta / 1000000) + 1;
4288 } else {
4289 /* Wait for either IO to occur or the next
4290 timer event. */
4291 add = qemu_next_deadline();
4292 /* We advance the timer before checking for IO.
4293 Limit the amount we advance so that early IO
4294 activity won't get the guest too far ahead. */
4295 if (add > 10000000)
4296 add = 10000000;
4297 delta += add;
4298 add = (add + (1 << icount_time_shift) - 1)
4299 >> icount_time_shift;
4300 qemu_icount += add;
4301 timeout = delta / 1000000;
4302 if (timeout < 0)
4303 timeout = 0;
4304 }
bellardb4608c02003-06-27 17:34:32 +00004305 }
aliguori43b96852009-04-24 18:03:33 +00004306
4307 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004308#else /* CONFIG_IOTHREAD */
4309 return 1000;
4310#endif
aliguori43b96852009-04-24 18:03:33 +00004311}
4312
4313static int vm_can_run(void)
4314{
4315 if (powerdown_requested)
4316 return 0;
4317 if (reset_requested)
4318 return 0;
4319 if (shutdown_requested)
4320 return 0;
aliguorie5689022009-04-24 18:03:54 +00004321 if (debug_requested)
4322 return 0;
aliguori43b96852009-04-24 18:03:33 +00004323 return 1;
4324}
4325
4326static void main_loop(void)
4327{
aliguori6e29f5d2009-04-24 18:04:02 +00004328 int r;
aliguori43b96852009-04-24 18:03:33 +00004329
aliguorid6dc3d42009-04-24 18:04:07 +00004330#ifdef CONFIG_IOTHREAD
4331 qemu_system_ready = 1;
4332 qemu_cond_broadcast(&qemu_system_cond);
4333#endif
4334
aliguori6e29f5d2009-04-24 18:04:02 +00004335 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004336 do {
4337#ifdef CONFIG_PROFILER
4338 int64_t ti;
4339#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004340#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004341 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004342#endif
aliguori43b96852009-04-24 18:03:33 +00004343#ifdef CONFIG_PROFILER
4344 ti = profile_getclock();
4345#endif
4346 main_loop_wait(qemu_calculate_timeout());
4347#ifdef CONFIG_PROFILER
4348 dev_time += profile_getclock() - ti;
4349#endif
aliguorie5689022009-04-24 18:03:54 +00004350 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004351
aliguorie5689022009-04-24 18:03:54 +00004352 if (qemu_debug_requested())
aliguori43b96852009-04-24 18:03:33 +00004353 vm_stop(EXCP_DEBUG);
aliguori43b96852009-04-24 18:03:33 +00004354 if (qemu_shutdown_requested()) {
4355 if (no_shutdown) {
4356 vm_stop(0);
4357 no_shutdown = 0;
4358 } else
4359 break;
4360 }
aliguorid6dc3d42009-04-24 18:04:07 +00004361 if (qemu_reset_requested()) {
4362 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004363 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004364 resume_all_vcpus();
4365 }
aliguori43b96852009-04-24 18:03:33 +00004366 if (qemu_powerdown_requested())
4367 qemu_system_powerdown();
aliguori6e29f5d2009-04-24 18:04:02 +00004368 if ((r = qemu_vmstop_requested()))
4369 vm_stop(r);
aliguori43b96852009-04-24 18:03:33 +00004370 }
aliguorid6dc3d42009-04-24 18:04:07 +00004371 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004372}
4373
pbrook9bd7e6d2009-04-07 22:58:45 +00004374static void version(void)
4375{
pbrook4a19f1e2009-04-07 23:17:49 +00004376 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004377}
4378
ths15f82202007-06-29 23:26:08 +00004379static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004380{
pbrook9bd7e6d2009-04-07 22:58:45 +00004381 version();
4382 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004383 "\n"
bellarda20dd502003-09-30 21:07:02 +00004384 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004385 "\n"
blueswir15824d652009-03-28 06:44:27 +00004386#define DEF(option, opt_arg, opt_enum, opt_help) \
4387 opt_help
4388#define DEFHEADING(text) stringify(text) "\n"
4389#include "qemu-options.h"
4390#undef DEF
4391#undef DEFHEADING
4392#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004393 "\n"
bellard82c643f2004-07-14 17:28:13 +00004394 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004395 "ctrl-alt-f toggle full screen\n"
4396 "ctrl-alt-n switch to virtual console 'n'\n"
4397 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004398 "\n"
4399 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4400 ,
bellard0db63472003-10-27 21:37:46 +00004401 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004402 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004403#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004404 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004405 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004406#endif
bellard6e44ba72004-01-18 21:56:49 +00004407 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004408 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004409 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004410}
4411
bellardcd6f1162004-05-13 22:02:20 +00004412#define HAS_ARG 0x0001
4413
4414enum {
blueswir15824d652009-03-28 06:44:27 +00004415#define DEF(option, opt_arg, opt_enum, opt_help) \
4416 opt_enum,
4417#define DEFHEADING(text)
4418#include "qemu-options.h"
4419#undef DEF
4420#undef DEFHEADING
4421#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004422};
4423
4424typedef struct QEMUOption {
4425 const char *name;
4426 int flags;
4427 int index;
4428} QEMUOption;
4429
blueswir1dbed7e42008-10-01 19:38:09 +00004430static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004431 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004432#define DEF(option, opt_arg, opt_enum, opt_help) \
4433 { option, opt_arg, opt_enum },
4434#define DEFHEADING(text)
4435#include "qemu-options.h"
4436#undef DEF
4437#undef DEFHEADING
4438#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004439 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004440};
4441
bellard1d14ffa2005-10-30 18:58:22 +00004442#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004443struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004444#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004445#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004446 {
4447 "pcspk",
4448 "PC speaker",
4449 0,
4450 1,
4451 { .init_isa = pcspk_audio_init }
4452 },
4453#endif
malc4c9b53e2009-01-09 10:46:34 +00004454
4455#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004456 {
4457 "sb16",
4458 "Creative Sound Blaster 16",
4459 0,
4460 1,
4461 { .init_isa = SB16_init }
4462 },
malc4c9b53e2009-01-09 10:46:34 +00004463#endif
bellard6a36d842005-12-18 20:34:32 +00004464
malccc53d262008-06-13 10:48:22 +00004465#ifdef CONFIG_CS4231A
4466 {
4467 "cs4231a",
4468 "CS4231A",
4469 0,
4470 1,
4471 { .init_isa = cs4231a_init }
4472 },
4473#endif
4474
bellard6a36d842005-12-18 20:34:32 +00004475#ifdef CONFIG_ADLIB
4476 {
4477 "adlib",
4478#ifdef HAS_YMF262
4479 "Yamaha YMF262 (OPL3)",
4480#else
4481 "Yamaha YM3812 (OPL2)",
4482#endif
4483 0,
4484 1,
4485 { .init_isa = Adlib_init }
4486 },
4487#endif
4488
4489#ifdef CONFIG_GUS
4490 {
4491 "gus",
4492 "Gravis Ultrasound GF1",
4493 0,
4494 1,
4495 { .init_isa = GUS_init }
4496 },
4497#endif
4498
malc4c9b53e2009-01-09 10:46:34 +00004499#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004500 {
4501 "ac97",
4502 "Intel 82801AA AC97 Audio",
4503 0,
4504 0,
4505 { .init_pci = ac97_init }
4506 },
malc4c9b53e2009-01-09 10:46:34 +00004507#endif
balroge5c9a132008-01-14 04:27:55 +00004508
malc4c9b53e2009-01-09 10:46:34 +00004509#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004510 {
4511 "es1370",
4512 "ENSONIQ AudioPCI ES1370",
4513 0,
4514 0,
4515 { .init_pci = es1370_init }
4516 },
balrogb00052e2007-04-30 02:22:06 +00004517#endif
bellard6a36d842005-12-18 20:34:32 +00004518
malc4c9b53e2009-01-09 10:46:34 +00004519#endif /* HAS_AUDIO_CHOICE */
4520
bellard6a36d842005-12-18 20:34:32 +00004521 { NULL, NULL, 0, 0, { NULL } }
4522};
4523
bellard1d14ffa2005-10-30 18:58:22 +00004524static void select_soundhw (const char *optarg)
4525{
bellard6a36d842005-12-18 20:34:32 +00004526 struct soundhw *c;
4527
bellard1d14ffa2005-10-30 18:58:22 +00004528 if (*optarg == '?') {
4529 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004530
bellard1d14ffa2005-10-30 18:58:22 +00004531 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004532 for (c = soundhw; c->name; ++c) {
4533 printf ("%-11s %s\n", c->name, c->descr);
4534 }
4535 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004536 exit (*optarg != '?');
4537 }
4538 else {
bellard6a36d842005-12-18 20:34:32 +00004539 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004540 const char *p;
4541 char *e;
4542 int bad_card = 0;
4543
bellard6a36d842005-12-18 20:34:32 +00004544 if (!strcmp (optarg, "all")) {
4545 for (c = soundhw; c->name; ++c) {
4546 c->enabled = 1;
4547 }
4548 return;
4549 }
bellard1d14ffa2005-10-30 18:58:22 +00004550
bellard6a36d842005-12-18 20:34:32 +00004551 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004552 while (*p) {
4553 e = strchr (p, ',');
4554 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004555
4556 for (c = soundhw; c->name; ++c) {
4557 if (!strncmp (c->name, p, l)) {
4558 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004559 break;
4560 }
4561 }
bellard6a36d842005-12-18 20:34:32 +00004562
4563 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004564 if (l > 80) {
4565 fprintf (stderr,
4566 "Unknown sound card name (too big to show)\n");
4567 }
4568 else {
4569 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4570 (int) l, p);
4571 }
4572 bad_card = 1;
4573 }
4574 p += l + (e != NULL);
4575 }
4576
4577 if (bad_card)
4578 goto show_valid_cards;
4579 }
4580}
4581#endif
4582
malc3893c122008-09-28 00:42:05 +00004583static void select_vgahw (const char *p)
4584{
4585 const char *opts;
4586
aliguori28b85ed2009-04-22 15:19:48 +00004587 cirrus_vga_enabled = 0;
4588 std_vga_enabled = 0;
4589 vmsvga_enabled = 0;
aliguori94909d92009-04-22 15:19:53 +00004590 xenfb_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004591 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004592 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004593 } else if (strstart(p, "cirrus", &opts)) {
4594 cirrus_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004595 } else if (strstart(p, "vmware", &opts)) {
malc3893c122008-09-28 00:42:05 +00004596 vmsvga_enabled = 1;
aliguori94909d92009-04-22 15:19:53 +00004597 } else if (strstart(p, "xenfb", &opts)) {
4598 xenfb_enabled = 1;
aliguori28b85ed2009-04-22 15:19:48 +00004599 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004600 invalid_vga:
4601 fprintf(stderr, "Unknown vga type: %s\n", p);
4602 exit(1);
4603 }
malccb5a7aa2008-09-28 00:42:12 +00004604 while (*opts) {
4605 const char *nextopt;
4606
4607 if (strstart(opts, ",retrace=", &nextopt)) {
4608 opts = nextopt;
4609 if (strstart(opts, "dumb", &nextopt))
4610 vga_retrace_method = VGA_RETRACE_DUMB;
4611 else if (strstart(opts, "precise", &nextopt))
4612 vga_retrace_method = VGA_RETRACE_PRECISE;
4613 else goto invalid_vga;
4614 } else goto invalid_vga;
4615 opts = nextopt;
4616 }
malc3893c122008-09-28 00:42:05 +00004617}
4618
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004619#ifdef TARGET_I386
4620static int balloon_parse(const char *arg)
4621{
4622 char buf[128];
4623 const char *p;
4624
4625 if (!strcmp(arg, "none")) {
4626 virtio_balloon = 0;
4627 } else if (!strncmp(arg, "virtio", 6)) {
4628 virtio_balloon = 1;
4629 if (arg[6] == ',') {
4630 p = arg + 7;
4631 if (get_param_value(buf, sizeof(buf), "addr", p)) {
4632 virtio_balloon_devaddr = strdup(buf);
4633 }
4634 }
4635 } else {
4636 return -1;
4637 }
4638 return 0;
4639}
4640#endif
4641
bellard3587d7e2006-06-26 20:03:44 +00004642#ifdef _WIN32
4643static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4644{
4645 exit(STATUS_CONTROL_C_EXIT);
4646 return TRUE;
4647}
4648#endif
4649
aliguoric4be29f2009-04-17 18:58:14 +00004650int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004651{
4652 int ret;
4653
4654 if(strlen(str) != 36)
4655 return -1;
4656
4657 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4658 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4659 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4660
4661 if(ret != 16)
4662 return -1;
4663
aliguorib6f6e3d2009-04-17 18:59:56 +00004664#ifdef TARGET_I386
4665 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4666#endif
4667
blueswir18fcb1b92008-09-18 18:29:08 +00004668 return 0;
4669}
4670
bellard7c9d8e02005-11-15 22:16:05 +00004671#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004672
aliguori5b08fc12008-08-21 20:08:03 +00004673#ifndef _WIN32
4674
4675static void termsig_handler(int signal)
4676{
4677 qemu_system_shutdown_request();
4678}
4679
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004680static void sigchld_handler(int signal)
4681{
4682 waitpid(-1, NULL, WNOHANG);
4683}
4684
4685static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004686{
4687 struct sigaction act;
4688
4689 memset(&act, 0, sizeof(act));
4690 act.sa_handler = termsig_handler;
4691 sigaction(SIGINT, &act, NULL);
4692 sigaction(SIGHUP, &act, NULL);
4693 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004694
4695 act.sa_handler = sigchld_handler;
4696 act.sa_flags = SA_NOCLDSTOP;
4697 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004698}
4699
4700#endif
4701
Paul Brook5cea8592009-05-30 00:52:44 +01004702#ifdef _WIN32
4703/* Look for support files in the same directory as the executable. */
4704static char *find_datadir(const char *argv0)
4705{
4706 char *p;
4707 char buf[MAX_PATH];
4708 DWORD len;
4709
4710 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4711 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004712 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004713 }
4714
4715 buf[len] = 0;
4716 p = buf + len - 1;
4717 while (p != buf && *p != '\\')
4718 p--;
4719 *p = 0;
4720 if (access(buf, R_OK) == 0) {
4721 return qemu_strdup(buf);
4722 }
4723 return NULL;
4724}
4725#else /* !_WIN32 */
4726
4727/* Find a likely location for support files using the location of the binary.
4728 For installed binaries this will be "$bindir/../share/qemu". When
4729 running from the build tree this will be "$bindir/../pc-bios". */
4730#define SHARE_SUFFIX "/share/qemu"
4731#define BUILD_SUFFIX "/pc-bios"
4732static char *find_datadir(const char *argv0)
4733{
4734 char *dir;
4735 char *p = NULL;
4736 char *res;
4737#ifdef PATH_MAX
4738 char buf[PATH_MAX];
4739#endif
Blue Swirl3a417592009-06-09 19:12:21 +00004740 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004741
4742#if defined(__linux__)
4743 {
4744 int len;
4745 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4746 if (len > 0) {
4747 buf[len] = 0;
4748 p = buf;
4749 }
4750 }
4751#elif defined(__FreeBSD__)
4752 {
4753 int len;
4754 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4755 if (len > 0) {
4756 buf[len] = 0;
4757 p = buf;
4758 }
4759 }
4760#endif
4761 /* If we don't have any way of figuring out the actual executable
4762 location then try argv[0]. */
4763 if (!p) {
4764#ifdef PATH_MAX
4765 p = buf;
4766#endif
4767 p = realpath(argv0, p);
4768 if (!p) {
4769 return NULL;
4770 }
4771 }
4772 dir = dirname(p);
4773 dir = dirname(dir);
4774
Blue Swirl3a417592009-06-09 19:12:21 +00004775 max_len = strlen(dir) +
4776 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4777 res = qemu_mallocz(max_len);
4778 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004779 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004780 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004781 if (access(res, R_OK)) {
4782 qemu_free(res);
4783 res = NULL;
4784 }
4785 }
4786#ifndef PATH_MAX
4787 free(p);
4788#endif
4789 return res;
4790}
4791#undef SHARE_SUFFIX
4792#undef BUILD_SUFFIX
4793#endif
4794
4795char *qemu_find_file(int type, const char *name)
4796{
4797 int len;
4798 const char *subdir;
4799 char *buf;
4800
4801 /* If name contains path separators then try it as a straight path. */
4802 if ((strchr(name, '/') || strchr(name, '\\'))
4803 && access(name, R_OK) == 0) {
4804 return strdup(name);
4805 }
4806 switch (type) {
4807 case QEMU_FILE_TYPE_BIOS:
4808 subdir = "";
4809 break;
4810 case QEMU_FILE_TYPE_KEYMAP:
4811 subdir = "keymaps/";
4812 break;
4813 default:
4814 abort();
4815 }
4816 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4817 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004818 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004819 if (access(buf, R_OK)) {
4820 qemu_free(buf);
4821 return NULL;
4822 }
4823 return buf;
4824}
4825
malc902b3d52008-12-10 19:18:40 +00004826int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004827{
aliguori59030a82009-04-05 18:43:41 +00004828 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004829 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004830 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004831 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004832 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004833 const char *kernel_filename, *kernel_cmdline;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004834 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004835 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004836 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004837 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004838 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004839 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004840 const char *bt_opts[MAX_BT_CMDLINE];
4841 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004842 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004843 int optind;
4844 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004845 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004846 const char *monitor_device;
4847 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004848 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004849 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004850 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004851 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4852 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004853 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004854 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004855 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004856 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004857 int usb_devices_index;
blueswir1b9e82a52009-04-05 18:03:31 +00004858#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004859 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004860#endif
bellard26a5f132008-05-28 12:30:31 +00004861 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004862 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004863 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004864#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004865 int fd = 0;
4866 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004867 const char *chroot_dir = NULL;
4868 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004869#endif
aliguori268a3622009-04-21 22:30:27 +00004870 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004871 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004872
malc902b3d52008-12-10 19:18:40 +00004873 qemu_cache_utils_init(envp);
4874
bellard0bd48852005-11-11 00:00:47 +00004875 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004876#ifndef _WIN32
4877 {
4878 struct sigaction act;
4879 sigfillset(&act.sa_mask);
4880 act.sa_flags = 0;
4881 act.sa_handler = SIG_IGN;
4882 sigaction(SIGPIPE, &act, NULL);
4883 }
bellard3587d7e2006-06-26 20:03:44 +00004884#else
4885 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004886 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4887 QEMU to run on a single CPU */
4888 {
4889 HANDLE h;
4890 DWORD mask, smask;
4891 int i;
4892 h = GetCurrentProcess();
4893 if (GetProcessAffinityMask(h, &mask, &smask)) {
4894 for(i = 0; i < 32; i++) {
4895 if (mask & (1 << i))
4896 break;
4897 }
4898 if (i != 32) {
4899 mask = 1 << i;
4900 SetProcessAffinityMask(h, mask);
4901 }
4902 }
4903 }
bellard67b915a2004-03-31 23:37:16 +00004904#endif
bellardbe995c22006-06-25 16:25:21 +00004905
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004906 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004907 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004908 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004909 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004910 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004911 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004912 kernel_filename = NULL;
4913 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004914 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004915 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004916 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004917
aurel32c75a8232008-05-04 00:50:34 +00004918 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004919 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004920 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004921 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004922
aliguori8290edd2009-02-27 20:14:29 +00004923 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004924 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004925 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004926 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004927
aliguori1b8fc812009-02-27 20:01:39 +00004928 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004929 virtio_consoles[i] = NULL;
4930 virtio_console_index = 0;
4931
aliguori268a3622009-04-21 22:30:27 +00004932 for (i = 0; i < MAX_NODES; i++) {
4933 node_mem[i] = 0;
4934 node_cpumask[i] = 0;
4935 }
4936
bellarda594cfb2005-11-06 16:13:29 +00004937 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004938
bellard7c9d8e02005-11-15 22:16:05 +00004939 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004940 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004941 nb_drives = 0;
4942 nb_drives_opt = 0;
aliguori268a3622009-04-21 22:30:27 +00004943 nb_numa_nodes = 0;
thse4bcb142007-12-02 04:51:10 +00004944 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004945
4946 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004947
bellard26a5f132008-05-28 12:30:31 +00004948 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004949 autostart= 1;
4950
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01004951 register_watchdogs();
4952
bellardcd6f1162004-05-13 22:02:20 +00004953 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004954 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004955 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004956 break;
bellardcd6f1162004-05-13 22:02:20 +00004957 r = argv[optind];
4958 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004959 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004960 } else {
4961 const QEMUOption *popt;
4962
4963 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004964 /* Treat --foo the same as -foo. */
4965 if (r[1] == '-')
4966 r++;
bellardcd6f1162004-05-13 22:02:20 +00004967 popt = qemu_options;
4968 for(;;) {
4969 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004970 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004971 argv[0], r);
4972 exit(1);
4973 }
4974 if (!strcmp(popt->name, r + 1))
4975 break;
4976 popt++;
4977 }
4978 if (popt->flags & HAS_ARG) {
4979 if (optind >= argc) {
4980 fprintf(stderr, "%s: option '%s' requires an argument\n",
4981 argv[0], r);
4982 exit(1);
4983 }
4984 optarg = argv[optind++];
4985 } else {
4986 optarg = NULL;
4987 }
4988
4989 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004990 case QEMU_OPTION_M:
4991 machine = find_machine(optarg);
4992 if (!machine) {
4993 QEMUMachine *m;
4994 printf("Supported machines are:\n");
4995 for(m = first_machine; m != NULL; m = m->next) {
4996 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004997 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004998 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004999 }
ths15f82202007-06-29 23:26:08 +00005000 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00005001 }
5002 break;
j_mayer94fc95c2007-03-05 19:44:02 +00005003 case QEMU_OPTION_cpu:
5004 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00005005 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00005006/* XXX: implement xxx_cpu_list for targets that still miss it */
5007#if defined(cpu_list)
5008 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00005009#endif
ths15f82202007-06-29 23:26:08 +00005010 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00005011 } else {
5012 cpu_model = optarg;
5013 }
5014 break;
bellardcd6f1162004-05-13 22:02:20 +00005015 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00005016 initrd_filename = optarg;
5017 break;
bellardcd6f1162004-05-13 22:02:20 +00005018 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00005019 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00005020 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00005021 else
balrog609497a2008-01-14 02:56:53 +00005022 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00005023 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00005024 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005025 translation == BIOS_ATA_TRANSLATION_LBA ?
5026 ",trans=lba" :
5027 translation == BIOS_ATA_TRANSLATION_NONE ?
5028 ",trans=none" : "");
5029 break;
bellardcd6f1162004-05-13 22:02:20 +00005030 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00005031 case QEMU_OPTION_hdc:
5032 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00005033 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00005034 break;
thse4bcb142007-12-02 04:51:10 +00005035 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00005036 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00005037 break;
balrog3e3d5812007-04-30 02:09:25 +00005038 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00005039 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00005040 break;
pbrooka1bb27b2007-04-06 16:49:48 +00005041 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00005042 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00005043 break;
j_mayer86f55662007-04-24 06:52:59 +00005044 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00005045 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00005046 break;
bellardcd6f1162004-05-13 22:02:20 +00005047 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00005048 snapshot = 1;
5049 break;
bellardcd6f1162004-05-13 22:02:20 +00005050 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00005051 {
bellard330d0412003-07-26 18:11:40 +00005052 const char *p;
5053 p = optarg;
5054 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005055 if (cyls < 1 || cyls > 16383)
5056 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005057 if (*p != ',')
5058 goto chs_fail;
5059 p++;
5060 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005061 if (heads < 1 || heads > 16)
5062 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005063 if (*p != ',')
5064 goto chs_fail;
5065 p++;
5066 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005067 if (secs < 1 || secs > 63)
5068 goto chs_fail;
5069 if (*p == ',') {
5070 p++;
5071 if (!strcmp(p, "none"))
5072 translation = BIOS_ATA_TRANSLATION_NONE;
5073 else if (!strcmp(p, "lba"))
5074 translation = BIOS_ATA_TRANSLATION_LBA;
5075 else if (!strcmp(p, "auto"))
5076 translation = BIOS_ATA_TRANSLATION_AUTO;
5077 else
5078 goto chs_fail;
5079 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00005080 chs_fail:
bellard46d47672004-11-16 01:45:27 +00005081 fprintf(stderr, "qemu: invalid physical CHS format\n");
5082 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00005083 }
thse4bcb142007-12-02 04:51:10 +00005084 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00005085 snprintf(drives_opt[hda_index].opt,
5086 sizeof(drives_opt[hda_index].opt),
5087 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
5088 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005089 translation == BIOS_ATA_TRANSLATION_LBA ?
5090 ",trans=lba" :
5091 translation == BIOS_ATA_TRANSLATION_NONE ?
5092 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00005093 }
5094 break;
aliguori268a3622009-04-21 22:30:27 +00005095 case QEMU_OPTION_numa:
5096 if (nb_numa_nodes >= MAX_NODES) {
5097 fprintf(stderr, "qemu: too many NUMA nodes\n");
5098 exit(1);
5099 }
5100 numa_add(optarg);
5101 break;
bellardcd6f1162004-05-13 22:02:20 +00005102 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005103 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005104 break;
balrog4d3b6f62008-02-10 16:33:14 +00005105#ifdef CONFIG_CURSES
5106 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005107 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005108 break;
5109#endif
balroga171fe32007-04-30 01:48:07 +00005110 case QEMU_OPTION_portrait:
5111 graphic_rotate = 1;
5112 break;
bellardcd6f1162004-05-13 22:02:20 +00005113 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005114 kernel_filename = optarg;
5115 break;
bellardcd6f1162004-05-13 22:02:20 +00005116 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005117 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005118 break;
bellardcd6f1162004-05-13 22:02:20 +00005119 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005120 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005121 break;
bellardcd6f1162004-05-13 22:02:20 +00005122 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005123 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005124 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02005125 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005126 };
5127 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005128 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005129 int legacy = 0;
5130
5131 if (!strchr(optarg, '=')) {
5132 legacy = 1;
5133 pstrcpy(buf, sizeof(buf), optarg);
5134 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5135 fprintf(stderr,
5136 "qemu: unknown boot parameter '%s' in '%s'\n",
5137 buf, optarg);
5138 exit(1);
5139 }
5140
5141 if (legacy ||
5142 get_param_value(buf, sizeof(buf), "order", optarg)) {
5143 boot_devices_bitmap = parse_bootdevices(buf);
5144 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005145 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005146 if (!legacy) {
5147 if (get_param_value(buf, sizeof(buf),
5148 "once", optarg)) {
5149 boot_devices_bitmap |= parse_bootdevices(buf);
5150 standard_boot_devices = qemu_strdup(boot_devices);
5151 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5152 qemu_register_reset(restore_boot_devices,
5153 standard_boot_devices);
5154 }
Jan Kiszka95387492009-07-02 00:19:02 +02005155 if (get_param_value(buf, sizeof(buf),
5156 "menu", optarg)) {
5157 if (!strcmp(buf, "on")) {
5158 boot_menu = 1;
5159 } else if (!strcmp(buf, "off")) {
5160 boot_menu = 0;
5161 } else {
5162 fprintf(stderr,
5163 "qemu: invalid option value '%s'\n",
5164 buf);
5165 exit(1);
5166 }
5167 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005168 }
bellard36b486b2003-11-11 13:36:08 +00005169 }
5170 break;
bellardcd6f1162004-05-13 22:02:20 +00005171 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005172 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005173 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005174 break;
bellard52ca8d62006-06-14 16:03:05 +00005175#ifdef TARGET_I386
5176 case QEMU_OPTION_no_fd_bootchk:
5177 fd_bootchk = 0;
5178 break;
5179#endif
bellard7c9d8e02005-11-15 22:16:05 +00005180 case QEMU_OPTION_net:
5181 if (nb_net_clients >= MAX_NET_CLIENTS) {
5182 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00005183 exit(1);
5184 }
pbrookfd5f3932008-03-26 20:55:43 +00005185 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00005186 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00005187 break;
bellardc7f74642004-08-24 21:57:12 +00005188#ifdef CONFIG_SLIRP
5189 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005190 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005191 break;
ths47d5d012007-02-20 00:05:08 +00005192 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005193 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005194 break;
bellardc94c8d62004-09-13 21:37:34 +00005195#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005196 case QEMU_OPTION_smb:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005197 net_slirp_smb(optarg);
bellard9d728e82004-09-05 23:09:03 +00005198 break;
bellardc94c8d62004-09-13 21:37:34 +00005199#endif
bellard9bf05442004-08-25 22:12:49 +00005200 case QEMU_OPTION_redir:
Jan Kiszkaf3546de2009-06-24 14:42:28 +02005201 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00005202 break;
bellardc7f74642004-08-24 21:57:12 +00005203#endif
balrogdc72ac12008-11-09 00:04:26 +00005204 case QEMU_OPTION_bt:
5205 if (nb_bt_opts >= MAX_BT_CMDLINE) {
5206 fprintf(stderr, "qemu: too many bluetooth options\n");
5207 exit(1);
5208 }
5209 bt_opts[nb_bt_opts++] = optarg;
5210 break;
bellard1d14ffa2005-10-30 18:58:22 +00005211#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005212 case QEMU_OPTION_audio_help:
5213 AUD_help ();
5214 exit (0);
5215 break;
5216 case QEMU_OPTION_soundhw:
5217 select_soundhw (optarg);
5218 break;
5219#endif
bellardcd6f1162004-05-13 22:02:20 +00005220 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005221 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005222 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005223 case QEMU_OPTION_version:
5224 version();
5225 exit(0);
5226 break;
aurel3200f82b82008-04-27 21:12:55 +00005227 case QEMU_OPTION_m: {
5228 uint64_t value;
5229 char *ptr;
5230
5231 value = strtoul(optarg, &ptr, 10);
5232 switch (*ptr) {
5233 case 0: case 'M': case 'm':
5234 value <<= 20;
5235 break;
5236 case 'G': case 'g':
5237 value <<= 30;
5238 break;
5239 default:
5240 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005241 exit(1);
5242 }
aurel3200f82b82008-04-27 21:12:55 +00005243
5244 /* On 32-bit hosts, QEMU is limited by virtual address space */
5245 if (value > (2047 << 20)
blueswir1640f42e2009-04-19 10:18:01 +00005246#ifndef CONFIG_KQEMU
aurel3200f82b82008-04-27 21:12:55 +00005247 && HOST_LONG_BITS == 32
5248#endif
5249 ) {
5250 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5251 exit(1);
5252 }
5253 if (value != (uint64_t)(ram_addr_t)value) {
5254 fprintf(stderr, "qemu: ram size too large\n");
5255 exit(1);
5256 }
5257 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005258 break;
aurel3200f82b82008-04-27 21:12:55 +00005259 }
bellardcd6f1162004-05-13 22:02:20 +00005260 case QEMU_OPTION_d:
5261 {
5262 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005263 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005264
bellardcd6f1162004-05-13 22:02:20 +00005265 mask = cpu_str_to_log_mask(optarg);
5266 if (!mask) {
5267 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005268 for(item = cpu_log_items; item->mask != 0; item++) {
5269 printf("%-10s %s\n", item->name, item->help);
5270 }
5271 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005272 }
5273 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005274 }
bellardcd6f1162004-05-13 22:02:20 +00005275 break;
bellardcd6f1162004-05-13 22:02:20 +00005276 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005277 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005278 break;
aliguori59030a82009-04-05 18:43:41 +00005279 case QEMU_OPTION_gdb:
5280 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005281 break;
bellardcd6f1162004-05-13 22:02:20 +00005282 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005283 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005284 break;
j_mayer1192dad2007-10-05 13:08:35 +00005285 case QEMU_OPTION_bios:
5286 bios_name = optarg;
5287 break;
aurel321b530a62009-04-05 20:08:59 +00005288 case QEMU_OPTION_singlestep:
5289 singlestep = 1;
5290 break;
bellardcd6f1162004-05-13 22:02:20 +00005291 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005292 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005293 break;
blueswir15824d652009-03-28 06:44:27 +00005294#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00005295 case QEMU_OPTION_k:
5296 keyboard_layout = optarg;
5297 break;
blueswir15824d652009-03-28 06:44:27 +00005298#endif
bellardee22c2f2004-06-03 12:49:50 +00005299 case QEMU_OPTION_localtime:
5300 rtc_utc = 0;
5301 break;
malc3893c122008-09-28 00:42:05 +00005302 case QEMU_OPTION_vga:
5303 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005304 break;
blueswir15824d652009-03-28 06:44:27 +00005305#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005306 case QEMU_OPTION_g:
5307 {
5308 const char *p;
5309 int w, h, depth;
5310 p = optarg;
5311 w = strtol(p, (char **)&p, 10);
5312 if (w <= 0) {
5313 graphic_error:
5314 fprintf(stderr, "qemu: invalid resolution or depth\n");
5315 exit(1);
5316 }
5317 if (*p != 'x')
5318 goto graphic_error;
5319 p++;
5320 h = strtol(p, (char **)&p, 10);
5321 if (h <= 0)
5322 goto graphic_error;
5323 if (*p == 'x') {
5324 p++;
5325 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005326 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005327 depth != 24 && depth != 32)
5328 goto graphic_error;
5329 } else if (*p == '\0') {
5330 depth = graphic_depth;
5331 } else {
5332 goto graphic_error;
5333 }
ths3b46e622007-09-17 08:09:54 +00005334
bellarde9b137c2004-06-21 16:46:10 +00005335 graphic_width = w;
5336 graphic_height = h;
5337 graphic_depth = depth;
5338 }
5339 break;
blueswir15824d652009-03-28 06:44:27 +00005340#endif
ths20d8a3e2007-02-18 17:04:49 +00005341 case QEMU_OPTION_echr:
5342 {
5343 char *r;
5344 term_escape_char = strtol(optarg, &r, 0);
5345 if (r == optarg)
5346 printf("Bad argument to echr\n");
5347 break;
5348 }
bellard82c643f2004-07-14 17:28:13 +00005349 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005350 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005351 break;
5352 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005353 if (serial_device_index >= MAX_SERIAL_PORTS) {
5354 fprintf(stderr, "qemu: too many serial ports\n");
5355 exit(1);
5356 }
pbrookfd5f3932008-03-26 20:55:43 +00005357 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005358 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005359 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005360 case QEMU_OPTION_watchdog:
5361 i = select_watchdog(optarg);
5362 if (i > 0)
5363 exit (i == 1 ? 1 : 0);
5364 break;
5365 case QEMU_OPTION_watchdog_action:
5366 if (select_watchdog_action(optarg) == -1) {
5367 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5368 exit(1);
5369 }
5370 break;
aliguori51ecf132009-01-15 20:06:40 +00005371 case QEMU_OPTION_virtiocon:
5372 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5373 fprintf(stderr, "qemu: too many virtio consoles\n");
5374 exit(1);
5375 }
5376 virtio_consoles[virtio_console_index] = optarg;
5377 virtio_console_index++;
5378 break;
bellard6508fe52005-01-15 12:02:56 +00005379 case QEMU_OPTION_parallel:
5380 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5381 fprintf(stderr, "qemu: too many parallel ports\n");
5382 exit(1);
5383 }
pbrookfd5f3932008-03-26 20:55:43 +00005384 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005385 parallel_device_index++;
5386 break;
bellardd63d3072004-10-03 13:29:03 +00005387 case QEMU_OPTION_loadvm:
5388 loadvm = optarg;
5389 break;
5390 case QEMU_OPTION_full_screen:
5391 full_screen = 1;
5392 break;
ths667acca2006-12-11 02:08:05 +00005393#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005394 case QEMU_OPTION_no_frame:
5395 no_frame = 1;
5396 break;
ths3780e192007-06-21 21:08:02 +00005397 case QEMU_OPTION_alt_grab:
5398 alt_grab = 1;
5399 break;
ths667acca2006-12-11 02:08:05 +00005400 case QEMU_OPTION_no_quit:
5401 no_quit = 1;
5402 break;
aliguori7d957bd2009-01-15 22:14:11 +00005403 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005404 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005405 break;
ths667acca2006-12-11 02:08:05 +00005406#endif
bellardf7cce892004-12-08 22:21:25 +00005407 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005408 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005409 break;
bellarda09db212005-04-30 16:10:35 +00005410#ifdef TARGET_I386
5411 case QEMU_OPTION_win2k_hack:
5412 win2k_install_hack = 1;
5413 break;
aliguori73822ec2009-01-15 20:11:34 +00005414 case QEMU_OPTION_rtc_td_hack:
5415 rtc_td_hack = 1;
5416 break;
aliguori8a92ea22009-02-27 20:12:36 +00005417 case QEMU_OPTION_acpitable:
5418 if(acpi_table_add(optarg) < 0) {
5419 fprintf(stderr, "Wrong acpi table provided\n");
5420 exit(1);
5421 }
5422 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005423 case QEMU_OPTION_smbios:
5424 if(smbios_entry_add(optarg) < 0) {
5425 fprintf(stderr, "Wrong smbios provided\n");
5426 exit(1);
5427 }
5428 break;
bellarda09db212005-04-30 16:10:35 +00005429#endif
blueswir1640f42e2009-04-19 10:18:01 +00005430#ifdef CONFIG_KQEMU
Anthony Liguori52249f02009-07-09 17:28:08 -05005431 case QEMU_OPTION_enable_kqemu:
5432 kqemu_allowed = 1;
bellardd993e022005-02-10 22:00:06 +00005433 break;
bellard89bfc102006-02-08 22:46:31 +00005434 case QEMU_OPTION_kernel_kqemu:
5435 kqemu_allowed = 2;
5436 break;
bellardd993e022005-02-10 22:00:06 +00005437#endif
aliguori7ba1e612008-11-05 16:04:33 +00005438#ifdef CONFIG_KVM
5439 case QEMU_OPTION_enable_kvm:
5440 kvm_allowed = 1;
blueswir1640f42e2009-04-19 10:18:01 +00005441#ifdef CONFIG_KQEMU
aliguori7ba1e612008-11-05 16:04:33 +00005442 kqemu_allowed = 0;
5443#endif
5444 break;
5445#endif
bellardbb36d472005-11-05 14:22:28 +00005446 case QEMU_OPTION_usb:
5447 usb_enabled = 1;
5448 break;
bellarda594cfb2005-11-06 16:13:29 +00005449 case QEMU_OPTION_usbdevice:
5450 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005451 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005452 fprintf(stderr, "Too many USB devices\n");
5453 exit(1);
5454 }
pbrookfd5f3932008-03-26 20:55:43 +00005455 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005456 usb_devices_index++;
5457 break;
bellard6a00d602005-11-21 23:25:50 +00005458 case QEMU_OPTION_smp:
5459 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005460 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005461 fprintf(stderr, "Invalid number of CPUs\n");
5462 exit(1);
5463 }
5464 break;
bellard24236862006-04-30 21:28:36 +00005465 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005466 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005467 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005468 break;
blueswir15824d652009-03-28 06:44:27 +00005469#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005470 case QEMU_OPTION_no_acpi:
5471 acpi_enabled = 0;
5472 break;
aliguori16b29ae2008-12-17 23:28:44 +00005473 case QEMU_OPTION_no_hpet:
5474 no_hpet = 1;
5475 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005476 case QEMU_OPTION_balloon:
5477 if (balloon_parse(optarg) < 0) {
5478 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5479 exit(1);
5480 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005481 break;
blueswir15824d652009-03-28 06:44:27 +00005482#endif
bellardd1beab82006-10-02 19:44:22 +00005483 case QEMU_OPTION_no_reboot:
5484 no_reboot = 1;
5485 break;
aurel32b2f76162008-04-11 21:35:52 +00005486 case QEMU_OPTION_no_shutdown:
5487 no_shutdown = 1;
5488 break;
balrog9467cd42007-05-01 01:34:14 +00005489 case QEMU_OPTION_show_cursor:
5490 cursor_hide = 0;
5491 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005492 case QEMU_OPTION_uuid:
5493 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5494 fprintf(stderr, "Fail to parse UUID string."
5495 " Wrong format.\n");
5496 exit(1);
5497 }
5498 break;
blueswir15824d652009-03-28 06:44:27 +00005499#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005500 case QEMU_OPTION_daemonize:
5501 daemonize = 1;
5502 break;
blueswir15824d652009-03-28 06:44:27 +00005503#endif
ths9ae02552007-01-05 17:39:04 +00005504 case QEMU_OPTION_option_rom:
5505 if (nb_option_roms >= MAX_OPTION_ROMS) {
5506 fprintf(stderr, "Too many option ROMs\n");
5507 exit(1);
5508 }
5509 option_rom[nb_option_roms] = optarg;
5510 nb_option_roms++;
5511 break;
blueswir15824d652009-03-28 06:44:27 +00005512#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005513 case QEMU_OPTION_semihosting:
5514 semihosting_enabled = 1;
5515 break;
blueswir15824d652009-03-28 06:44:27 +00005516#endif
thsc35734b2007-03-19 15:17:08 +00005517 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005518 qemu_name = qemu_strdup(optarg);
5519 {
5520 char *p = strchr(qemu_name, ',');
5521 if (p != NULL) {
5522 *p++ = 0;
5523 if (strncmp(p, "process=", 8)) {
5524 fprintf(stderr, "Unknown subargument %s to -name", p);
5525 exit(1);
5526 }
5527 p += 8;
5528 set_proc_name(p);
5529 }
5530 }
thsc35734b2007-03-19 15:17:08 +00005531 break;
blueswir195efd112008-12-24 20:26:14 +00005532#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005533 case QEMU_OPTION_prom_env:
5534 if (nb_prom_envs >= MAX_PROM_ENVS) {
5535 fprintf(stderr, "Too many prom variables\n");
5536 exit(1);
5537 }
5538 prom_envs[nb_prom_envs] = optarg;
5539 nb_prom_envs++;
5540 break;
5541#endif
balrog2b8f2d42007-07-27 22:08:46 +00005542#ifdef TARGET_ARM
5543 case QEMU_OPTION_old_param:
5544 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005545 break;
balrog2b8f2d42007-07-27 22:08:46 +00005546#endif
thsf3dcfad2007-08-24 01:26:02 +00005547 case QEMU_OPTION_clock:
5548 configure_alarms(optarg);
5549 break;
bellard7e0af5d02007-11-07 16:24:33 +00005550 case QEMU_OPTION_startdate:
5551 {
5552 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005553 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005554 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005555 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005556 } else {
5557 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5558 &tm.tm_year,
5559 &tm.tm_mon,
5560 &tm.tm_mday,
5561 &tm.tm_hour,
5562 &tm.tm_min,
5563 &tm.tm_sec) == 6) {
5564 /* OK */
5565 } else if (sscanf(optarg, "%d-%d-%d",
5566 &tm.tm_year,
5567 &tm.tm_mon,
5568 &tm.tm_mday) == 3) {
5569 tm.tm_hour = 0;
5570 tm.tm_min = 0;
5571 tm.tm_sec = 0;
5572 } else {
5573 goto date_fail;
5574 }
5575 tm.tm_year -= 1900;
5576 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005577 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005578 if (rtc_start_date == -1) {
5579 date_fail:
5580 fprintf(stderr, "Invalid date format. Valid format are:\n"
5581 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5582 exit(1);
5583 }
balrogf6503052008-02-17 11:42:19 +00005584 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005585 }
5586 }
5587 break;
bellard26a5f132008-05-28 12:30:31 +00005588 case QEMU_OPTION_tb_size:
5589 tb_size = strtol(optarg, NULL, 0);
5590 if (tb_size < 0)
5591 tb_size = 0;
5592 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005593 case QEMU_OPTION_icount:
5594 use_icount = 1;
5595 if (strcmp(optarg, "auto") == 0) {
5596 icount_time_shift = -1;
5597 } else {
5598 icount_time_shift = strtol(optarg, NULL, 0);
5599 }
5600 break;
aliguori5bb79102008-10-13 03:12:02 +00005601 case QEMU_OPTION_incoming:
5602 incoming = optarg;
5603 break;
blueswir15824d652009-03-28 06:44:27 +00005604#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005605 case QEMU_OPTION_chroot:
5606 chroot_dir = optarg;
5607 break;
5608 case QEMU_OPTION_runas:
5609 run_as = optarg;
5610 break;
blueswir15824d652009-03-28 06:44:27 +00005611#endif
aliguorie37630c2009-04-22 15:19:10 +00005612#ifdef CONFIG_XEN
5613 case QEMU_OPTION_xen_domid:
5614 xen_domid = atoi(optarg);
5615 break;
5616 case QEMU_OPTION_xen_create:
5617 xen_mode = XEN_CREATE;
5618 break;
5619 case QEMU_OPTION_xen_attach:
5620 xen_mode = XEN_ATTACH;
5621 break;
5622#endif
bellardcd6f1162004-05-13 22:02:20 +00005623 }
bellard0824d6f2003-06-24 13:42:40 +00005624 }
5625 }
bellard330d0412003-07-26 18:11:40 +00005626
Paul Brook5cea8592009-05-30 00:52:44 +01005627 /* If no data_dir is specified then try to find it relative to the
5628 executable path. */
5629 if (!data_dir) {
5630 data_dir = find_datadir(argv[0]);
5631 }
5632 /* If all else fails use the install patch specified when building. */
5633 if (!data_dir) {
5634 data_dir = CONFIG_QEMU_SHAREDIR;
5635 }
5636
blueswir1640f42e2009-04-19 10:18:01 +00005637#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
aliguori7ba1e612008-11-05 16:04:33 +00005638 if (kvm_allowed && kqemu_allowed) {
5639 fprintf(stderr,
5640 "You can not enable both KVM and kqemu at the same time\n");
5641 exit(1);
5642 }
5643#endif
5644
balrog3d878ca2008-10-28 10:59:59 +00005645 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005646 if (smp_cpus > machine->max_cpus) {
5647 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5648 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5649 machine->max_cpus);
5650 exit(1);
5651 }
5652
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005653 if (display_type == DT_NOGRAPHIC) {
aliguoribc0129d2008-08-01 15:12:34 +00005654 if (serial_device_index == 0)
5655 serial_devices[0] = "stdio";
5656 if (parallel_device_index == 0)
5657 parallel_devices[0] = "null";
5658 if (strncmp(monitor_device, "vc", 2) == 0)
5659 monitor_device = "stdio";
5660 }
5661
ths71e3ceb2006-12-22 02:11:31 +00005662#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005663 if (daemonize) {
5664 pid_t pid;
5665
5666 if (pipe(fds) == -1)
5667 exit(1);
5668
5669 pid = fork();
5670 if (pid > 0) {
5671 uint8_t status;
5672 ssize_t len;
5673
5674 close(fds[1]);
5675
5676 again:
ths93815bc2007-03-19 15:58:31 +00005677 len = read(fds[0], &status, 1);
5678 if (len == -1 && (errno == EINTR))
5679 goto again;
5680
5681 if (len != 1)
5682 exit(1);
5683 else if (status == 1) {
5684 fprintf(stderr, "Could not acquire pidfile\n");
5685 exit(1);
5686 } else
5687 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005688 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005689 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005690
5691 setsid();
5692
5693 pid = fork();
5694 if (pid > 0)
5695 exit(0);
5696 else if (pid < 0)
5697 exit(1);
5698
5699 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005700
5701 signal(SIGTSTP, SIG_IGN);
5702 signal(SIGTTOU, SIG_IGN);
5703 signal(SIGTTIN, SIG_IGN);
5704 }
ths71e3ceb2006-12-22 02:11:31 +00005705
thsaa26bb22007-03-25 21:33:06 +00005706 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005707 if (daemonize) {
5708 uint8_t status = 1;
5709 write(fds[1], &status, 1);
5710 } else
5711 fprintf(stderr, "Could not acquire pid file\n");
5712 exit(1);
5713 }
blueswir1b9e82a52009-04-05 18:03:31 +00005714#endif
ths93815bc2007-03-19 15:58:31 +00005715
blueswir1640f42e2009-04-19 10:18:01 +00005716#ifdef CONFIG_KQEMU
bellardff3fbb32006-01-08 10:53:14 +00005717 if (smp_cpus > 1)
5718 kqemu_allowed = 0;
5719#endif
aliguori3fcf7b62009-04-24 18:03:25 +00005720 if (qemu_init_main_loop()) {
5721 fprintf(stderr, "qemu_init_main_loop failed\n");
5722 exit(1);
5723 }
bellarda20dd502003-09-30 21:07:02 +00005724 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005725
thsf8d39c02008-07-03 10:01:15 +00005726 if (!linux_boot && *kernel_cmdline != '\0') {
5727 fprintf(stderr, "-append only allowed with -kernel option\n");
5728 exit(1);
5729 }
5730
5731 if (!linux_boot && initrd_filename != NULL) {
5732 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5733 exit(1);
5734 }
5735
bellardb118d612003-06-30 23:36:21 +00005736 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005737
pbrook634fce92006-07-15 17:40:09 +00005738 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005739 if (init_timer_alarm() < 0) {
5740 fprintf(stderr, "could not initialize alarm timer\n");
5741 exit(1);
5742 }
pbrook2e70f6e2008-06-29 01:03:05 +00005743 if (use_icount && icount_time_shift < 0) {
5744 use_icount = 2;
5745 /* 125MIPS seems a reasonable initial guess at the guest speed.
5746 It will be corrected fairly quickly anyway. */
5747 icount_time_shift = 3;
5748 init_icount_adjust();
5749 }
pbrook634fce92006-07-15 17:40:09 +00005750
bellardfd1dff42006-02-01 21:29:26 +00005751#ifdef _WIN32
5752 socket_init();
5753#endif
5754
bellard7c9d8e02005-11-15 22:16:05 +00005755 /* init network clients */
5756 if (nb_net_clients == 0) {
5757 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005758 net_clients[nb_net_clients++] = "nic";
5759#ifdef CONFIG_SLIRP
5760 net_clients[nb_net_clients++] = "user";
5761#endif
bellardc20709a2004-04-21 23:27:19 +00005762 }
5763
bellard7c9d8e02005-11-15 22:16:05 +00005764 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005765 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005766 exit(1);
bellard702c6512004-04-02 21:21:32 +00005767 }
bellardf1510b22003-06-25 00:07:40 +00005768
Glauber Costa406c8df2009-06-17 09:05:30 -04005769 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5770 net_set_boot_mask(net_boot);
5771
5772 net_client_check();
thseec85c22007-01-05 17:41:07 +00005773
balrogdc72ac12008-11-09 00:04:26 +00005774 /* init the bluetooth world */
5775 for (i = 0; i < nb_bt_opts; i++)
5776 if (bt_parse(bt_opts[i]))
5777 exit(1);
5778
bellard0824d6f2003-06-24 13:42:40 +00005779 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005780 if (ram_size == 0)
5781 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005782
blueswir1640f42e2009-04-19 10:18:01 +00005783#ifdef CONFIG_KQEMU
pbrook94a6b542009-04-11 17:15:54 +00005784 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5785 guest ram allocation. It needs to go away. */
5786 if (kqemu_allowed) {
Paul Brook4cfce482009-05-13 18:08:17 +01005787 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
pbrook94a6b542009-04-11 17:15:54 +00005788 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5789 if (!kqemu_phys_ram_base) {
5790 fprintf(stderr, "Could not allocate physical memory\n");
5791 exit(1);
5792 }
balrog7fb4fdc2008-04-24 17:59:27 +00005793 }
pbrook94a6b542009-04-11 17:15:54 +00005794#endif
bellard0824d6f2003-06-24 13:42:40 +00005795
bellard26a5f132008-05-28 12:30:31 +00005796 /* init the dynamic translator */
5797 cpu_exec_init_all(tb_size * 1024 * 1024);
5798
bellard5905b2e2004-08-01 21:53:26 +00005799 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005800
5801 /* we always create the cdrom drive, even if no disk is there */
5802
5803 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005804 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005805
balrog9d413d12007-12-04 00:10:34 +00005806 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005807
5808 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005809 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005810
balrog9d413d12007-12-04 00:10:34 +00005811 /* we always create one sd slot, even if no card is in it */
5812
5813 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005814 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005815
ths96d30e42007-01-07 20:42:14 +00005816 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005817
thse4bcb142007-12-02 04:51:10 +00005818 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005819 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005820 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005821
bellardc88676f2006-08-06 13:36:11 +00005822 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005823 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005824
aliguori3023f332009-01-16 19:04:14 +00005825#ifndef _WIN32
5826 /* must be after terminal init, SDL library changes signal handlers */
Jan Kiszka7c3370d2009-05-08 12:34:17 +02005827 sighandler_setup();
aliguori3023f332009-01-16 19:04:14 +00005828#endif
5829
5830 /* Maintain compatibility with multiple stdio monitors */
5831 if (!strcmp(monitor_device,"stdio")) {
5832 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5833 const char *devname = serial_devices[i];
5834 if (devname && !strcmp(devname,"mon:stdio")) {
5835 monitor_device = NULL;
5836 break;
5837 } else if (devname && !strcmp(devname,"stdio")) {
5838 monitor_device = NULL;
5839 serial_devices[i] = "mon:stdio";
5840 break;
5841 }
5842 }
5843 }
5844
aliguori268a3622009-04-21 22:30:27 +00005845 if (nb_numa_nodes > 0) {
5846 int i;
5847
5848 if (nb_numa_nodes > smp_cpus) {
5849 nb_numa_nodes = smp_cpus;
5850 }
5851
5852 /* If no memory size if given for any node, assume the default case
5853 * and distribute the available memory equally across all nodes
5854 */
5855 for (i = 0; i < nb_numa_nodes; i++) {
5856 if (node_mem[i] != 0)
5857 break;
5858 }
5859 if (i == nb_numa_nodes) {
5860 uint64_t usedmem = 0;
5861
5862 /* On Linux, the each node's border has to be 8MB aligned,
5863 * the final node gets the rest.
5864 */
5865 for (i = 0; i < nb_numa_nodes - 1; i++) {
5866 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5867 usedmem += node_mem[i];
5868 }
5869 node_mem[i] = ram_size - usedmem;
5870 }
5871
5872 for (i = 0; i < nb_numa_nodes; i++) {
5873 if (node_cpumask[i] != 0)
5874 break;
5875 }
5876 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5877 * must cope with this anyway, because there are BIOSes out there in
5878 * real machines which also use this scheme.
5879 */
5880 if (i == nb_numa_nodes) {
5881 for (i = 0; i < smp_cpus; i++) {
5882 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5883 }
5884 }
5885 }
5886
aliguori3023f332009-01-16 19:04:14 +00005887 if (kvm_enabled()) {
5888 int ret;
5889
5890 ret = kvm_init(smp_cpus);
5891 if (ret < 0) {
5892 fprintf(stderr, "failed to initialize KVM\n");
5893 exit(1);
5894 }
5895 }
5896
aliguori4c621802009-01-16 21:48:20 +00005897 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005898 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005899 if (!monitor_hd) {
5900 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5901 exit(1);
5902 }
5903 }
5904
aliguori2796dae2009-01-16 20:23:27 +00005905 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5906 const char *devname = serial_devices[i];
5907 if (devname && strcmp(devname, "none")) {
5908 char label[32];
5909 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005910 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005911 if (!serial_hds[i]) {
5912 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5913 devname);
5914 exit(1);
5915 }
5916 }
5917 }
5918
5919 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5920 const char *devname = parallel_devices[i];
5921 if (devname && strcmp(devname, "none")) {
5922 char label[32];
5923 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005924 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005925 if (!parallel_hds[i]) {
5926 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5927 devname);
5928 exit(1);
5929 }
5930 }
5931 }
5932
5933 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5934 const char *devname = virtio_consoles[i];
5935 if (devname && strcmp(devname, "none")) {
5936 char label[32];
5937 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005938 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005939 if (!virtcon_hds[i]) {
5940 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5941 devname);
5942 exit(1);
5943 }
5944 }
5945 }
5946
Paul Brookaae94602009-05-14 22:35:06 +01005947 module_call_init(MODULE_INIT_DEVICE);
5948
Paul Brookfbe1b592009-05-13 17:56:25 +01005949 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005950 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5951
aliguori268a3622009-04-21 22:30:27 +00005952
5953 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5954 for (i = 0; i < nb_numa_nodes; i++) {
5955 if (node_cpumask[i] & (1 << env->cpu_index)) {
5956 env->numa_node = i;
5957 }
5958 }
5959 }
5960
aliguori6f338c32009-02-11 15:21:54 +00005961 current_machine = machine;
5962
aliguori3023f332009-01-16 19:04:14 +00005963 /* init USB devices */
5964 if (usb_enabled) {
5965 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00005966 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f332009-01-16 19:04:14 +00005967 fprintf(stderr, "Warning: could not add USB device %s\n",
5968 usb_devices[i]);
5969 }
5970 }
5971 }
5972
aliguori8f391ab2009-01-19 16:34:10 +00005973 if (!display_state)
5974 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005975 /* just use the first displaystate for the moment */
5976 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005977
5978 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005979#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005980 display_type = DT_SDL;
5981#else
5982 display_type = DT_VNC;
5983 vnc_display = "localhost:0,to=99";
5984 show_vnc_port = 1;
5985#endif
5986 }
5987
5988
5989 switch (display_type) {
5990 case DT_NOGRAPHIC:
5991 break;
5992#if defined(CONFIG_CURSES)
5993 case DT_CURSES:
5994 curses_display_init(ds, full_screen);
5995 break;
5996#endif
bellard5b0753e2005-03-01 21:37:28 +00005997#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005998 case DT_SDL:
5999 sdl_display_init(ds, full_screen, no_frame);
6000 break;
bellard5b0753e2005-03-01 21:37:28 +00006001#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006002 case DT_SDL:
6003 cocoa_display_init(ds, full_screen);
6004 break;
bellard313aa562003-08-10 21:52:11 +00006005#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006006 case DT_VNC:
6007 vnc_display_init(ds);
6008 if (vnc_display_open(ds, vnc_display) < 0)
6009 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006010
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006011 if (show_vnc_port) {
6012 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006013 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006014 break;
6015 default:
6016 break;
bellard313aa562003-08-10 21:52:11 +00006017 }
aliguori7d957bd2009-01-15 22:14:11 +00006018 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00006019
aliguori3023f332009-01-16 19:04:14 +00006020 dcl = ds->listeners;
6021 while (dcl != NULL) {
6022 if (dcl->dpy_refresh != NULL) {
6023 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6024 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00006025 }
aliguori3023f332009-01-16 19:04:14 +00006026 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00006027 }
aliguori3023f332009-01-16 19:04:14 +00006028
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006029 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00006030 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6031 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6032 }
6033
aliguori2796dae2009-01-16 20:23:27 +00006034 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00006035 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00006036
aliguori4c621802009-01-16 21:48:20 +00006037 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00006038 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00006039
bellard8d11df92004-08-24 21:13:40 +00006040 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006041 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006042 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006043 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006044 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00006045 }
bellard82c643f2004-07-14 17:28:13 +00006046 }
bellard82c643f2004-07-14 17:28:13 +00006047
bellard6508fe52005-01-15 12:02:56 +00006048 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006049 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006050 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006051 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006052 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00006053 }
6054 }
6055
aliguori9ede2fd2009-01-15 20:05:25 +00006056 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6057 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00006058 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00006059 if (strstart(devname, "vc", 0))
6060 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
6061 }
6062 }
6063
aliguori59030a82009-04-05 18:43:41 +00006064 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6065 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6066 gdbstub_dev);
6067 exit(1);
balrog45669e02007-07-02 13:20:17 +00006068 }
balrog45669e02007-07-02 13:20:17 +00006069
bellardd63d3072004-10-03 13:29:03 +00006070 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00006071 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00006072
aliguori5bb79102008-10-13 03:12:02 +00006073 if (incoming) {
6074 autostart = 0; /* fixme how to deal with -daemonize */
6075 qemu_start_incoming_migration(incoming);
6076 }
6077
aliguoric0f4ce72009-03-05 23:01:01 +00006078 if (autostart)
6079 vm_start();
thsffd843b2006-12-21 19:46:43 +00006080
blueswir1b9e82a52009-04-05 18:03:31 +00006081#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00006082 if (daemonize) {
6083 uint8_t status = 0;
6084 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00006085
6086 again1:
6087 len = write(fds[1], &status, 1);
6088 if (len == -1 && (errno == EINTR))
6089 goto again1;
6090
6091 if (len != 1)
6092 exit(1);
6093
aliguoribd54b862008-07-23 00:58:33 +00006094 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00006095 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00006096 if (fd == -1)
6097 exit(1);
aliguori08585322009-02-27 22:09:45 +00006098 }
ths71e3ceb2006-12-22 02:11:31 +00006099
aliguori08585322009-02-27 22:09:45 +00006100 if (run_as) {
6101 pwd = getpwnam(run_as);
6102 if (!pwd) {
6103 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6104 exit(1);
6105 }
6106 }
ths71e3ceb2006-12-22 02:11:31 +00006107
aliguori08585322009-02-27 22:09:45 +00006108 if (chroot_dir) {
6109 if (chroot(chroot_dir) < 0) {
6110 fprintf(stderr, "chroot failed\n");
6111 exit(1);
6112 }
6113 chdir("/");
6114 }
6115
6116 if (run_as) {
6117 if (setgid(pwd->pw_gid) < 0) {
6118 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6119 exit(1);
6120 }
6121 if (setuid(pwd->pw_uid) < 0) {
6122 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6123 exit(1);
6124 }
6125 if (setuid(0) != -1) {
6126 fprintf(stderr, "Dropping privileges failed\n");
6127 exit(1);
6128 }
6129 }
aliguori08585322009-02-27 22:09:45 +00006130
6131 if (daemonize) {
6132 dup2(fd, 0);
6133 dup2(fd, 1);
6134 dup2(fd, 2);
6135
6136 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006137 }
blueswir1b9e82a52009-04-05 18:03:31 +00006138#endif
ths71e3ceb2006-12-22 02:11:31 +00006139
bellard8a7ddc32004-03-31 19:00:16 +00006140 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006141 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006142 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006143
bellard0824d6f2003-06-24 13:42:40 +00006144 return 0;
6145}