blob: 7360d4f0212d1d5bef97802139013dca272a08ff [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];
bellard0824d6f2003-06-24 13:42:40 +0000257
aliguori268a3622009-04-21 22:30:27 +0000258int nb_numa_nodes;
259uint64_t node_mem[MAX_NODES];
260uint64_t node_cpumask[MAX_NODES];
261
balrogee5605e2007-12-03 03:01:40 +0000262static CPUState *cur_cpu;
263static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000264static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000265/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000266static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000267/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000268#define MAX_ICOUNT_SHIFT 10
269/* Compensate for varying guest execution speed. */
270static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000271static QEMUTimer *icount_rt_timer;
272static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000273static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000274
blueswir18fcb1b92008-09-18 18:29:08 +0000275uint8_t qemu_uuid[16];
276
Jan Kiszka76e30d02009-07-02 00:19:02 +0200277static QEMUBootSetHandler *boot_set_handler;
278static void *boot_set_opaque;
279
bellard0824d6f2003-06-24 13:42:40 +0000280/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000281/* x86 ISA bus support */
282
283target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000284PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000285
bellard0824d6f2003-06-24 13:42:40 +0000286/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000287void hw_error(const char *fmt, ...)
288{
289 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000290 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000291
292 va_start(ap, fmt);
293 fprintf(stderr, "qemu: hardware error: ");
294 vfprintf(stderr, fmt, ap);
295 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000296 for(env = first_cpu; env != NULL; env = env->next_cpu) {
297 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000298#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000299 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000300#else
bellard6a00d602005-11-21 23:25:50 +0000301 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000302#endif
bellard6a00d602005-11-21 23:25:50 +0000303 }
bellard0824d6f2003-06-24 13:42:40 +0000304 va_end(ap);
305 abort();
306}
Andi Kleen18894652009-07-02 09:34:17 +0200307
308static void set_proc_name(const char *s)
309{
310#ifdef __linux__
311 char name[16];
312 if (!s)
313 return;
314 name[sizeof(name) - 1] = 0;
315 strncpy(name, s, sizeof(name));
316 /* Could rewrite argv[0] too, but that's a bit more complicated.
317 This simple way is enough for `top'. */
318 prctl(PR_SET_NAME, name);
319#endif
320}
aliguoridf751fa2008-12-04 20:19:35 +0000321
322/***************/
323/* ballooning */
324
325static QEMUBalloonEvent *qemu_balloon_event;
326void *qemu_balloon_event_opaque;
327
328void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
329{
330 qemu_balloon_event = func;
331 qemu_balloon_event_opaque = opaque;
332}
333
334void qemu_balloon(ram_addr_t target)
335{
336 if (qemu_balloon_event)
337 qemu_balloon_event(qemu_balloon_event_opaque, target);
338}
339
340ram_addr_t qemu_balloon_status(void)
341{
342 if (qemu_balloon_event)
343 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
344 return 0;
345}
bellard0824d6f2003-06-24 13:42:40 +0000346
bellard8a7ddc32004-03-31 19:00:16 +0000347/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000348/* keyboard/mouse */
349
350static QEMUPutKBDEvent *qemu_put_kbd_event;
351static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000352static QEMUPutMouseEntry *qemu_put_mouse_event_head;
353static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000354
355void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
356{
357 qemu_put_kbd_event_opaque = opaque;
358 qemu_put_kbd_event = func;
359}
360
ths455204e2007-01-05 16:42:13 +0000361QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
362 void *opaque, int absolute,
363 const char *name)
bellard63066f42004-06-03 18:45:02 +0000364{
ths455204e2007-01-05 16:42:13 +0000365 QEMUPutMouseEntry *s, *cursor;
366
367 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000368
369 s->qemu_put_mouse_event = func;
370 s->qemu_put_mouse_event_opaque = opaque;
371 s->qemu_put_mouse_event_absolute = absolute;
372 s->qemu_put_mouse_event_name = qemu_strdup(name);
373 s->next = NULL;
374
375 if (!qemu_put_mouse_event_head) {
376 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
377 return s;
378 }
379
380 cursor = qemu_put_mouse_event_head;
381 while (cursor->next != NULL)
382 cursor = cursor->next;
383
384 cursor->next = s;
385 qemu_put_mouse_event_current = s;
386
387 return s;
388}
389
390void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
391{
392 QEMUPutMouseEntry *prev = NULL, *cursor;
393
394 if (!qemu_put_mouse_event_head || entry == NULL)
395 return;
396
397 cursor = qemu_put_mouse_event_head;
398 while (cursor != NULL && cursor != entry) {
399 prev = cursor;
400 cursor = cursor->next;
401 }
402
403 if (cursor == NULL) // does not exist or list empty
404 return;
405 else if (prev == NULL) { // entry is head
406 qemu_put_mouse_event_head = cursor->next;
407 if (qemu_put_mouse_event_current == entry)
408 qemu_put_mouse_event_current = cursor->next;
409 qemu_free(entry->qemu_put_mouse_event_name);
410 qemu_free(entry);
411 return;
412 }
413
414 prev->next = entry->next;
415
416 if (qemu_put_mouse_event_current == entry)
417 qemu_put_mouse_event_current = prev;
418
419 qemu_free(entry->qemu_put_mouse_event_name);
420 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000421}
422
423void kbd_put_keycode(int keycode)
424{
425 if (qemu_put_kbd_event) {
426 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
427 }
428}
429
430void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
431{
ths455204e2007-01-05 16:42:13 +0000432 QEMUPutMouseEvent *mouse_event;
433 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000434 int width;
ths455204e2007-01-05 16:42:13 +0000435
436 if (!qemu_put_mouse_event_current) {
437 return;
438 }
439
440 mouse_event =
441 qemu_put_mouse_event_current->qemu_put_mouse_event;
442 mouse_event_opaque =
443 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
444
445 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000446 if (graphic_rotate) {
447 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
448 width = 0x7fff;
449 else
aurel32b94ed572008-03-10 19:34:27 +0000450 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000451 mouse_event(mouse_event_opaque,
452 width - dy, dx, dz, buttons_state);
453 } else
454 mouse_event(mouse_event_opaque,
455 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000456 }
457}
458
bellard09b26c52006-04-12 21:09:08 +0000459int kbd_mouse_is_absolute(void)
460{
ths455204e2007-01-05 16:42:13 +0000461 if (!qemu_put_mouse_event_current)
462 return 0;
463
464 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
465}
466
aliguori376253e2009-03-05 23:01:23 +0000467void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000468{
469 QEMUPutMouseEntry *cursor;
470 int index = 0;
471
472 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000473 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000474 return;
475 }
476
aliguori376253e2009-03-05 23:01:23 +0000477 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000478 cursor = qemu_put_mouse_event_head;
479 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000480 monitor_printf(mon, "%c Mouse #%d: %s\n",
481 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
482 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000483 index++;
484 cursor = cursor->next;
485 }
486}
487
aliguori376253e2009-03-05 23:01:23 +0000488void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000489{
490 QEMUPutMouseEntry *cursor;
491 int i = 0;
492
493 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000494 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000495 return;
496 }
497
498 cursor = qemu_put_mouse_event_head;
499 while (cursor != NULL && index != i) {
500 i++;
501 cursor = cursor->next;
502 }
503
504 if (cursor != NULL)
505 qemu_put_mouse_event_current = cursor;
506 else
aliguori376253e2009-03-05 23:01:23 +0000507 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000508}
509
bellard87858c82003-06-27 12:01:39 +0000510/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000511uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000512{
513 union {
514 uint64_t ll;
515 struct {
516#ifdef WORDS_BIGENDIAN
517 uint32_t high, low;
518#else
519 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000520#endif
bellard87858c82003-06-27 12:01:39 +0000521 } l;
522 } u, res;
523 uint64_t rl, rh;
524
525 u.ll = a;
526 rl = (uint64_t)u.l.low * (uint64_t)b;
527 rh = (uint64_t)u.l.high * (uint64_t)b;
528 rh += (rl >> 32);
529 res.l.high = rh / c;
530 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
531 return res.ll;
532}
533
bellard1dce7c32006-07-13 23:20:22 +0000534/***********************************************************/
535/* real time host monotonic timer */
536
537#define QEMU_TIMER_BASE 1000000000LL
538
539#ifdef WIN32
540
541static int64_t clock_freq;
542
543static void init_get_clock(void)
544{
bellarda8e5ac32006-07-14 09:36:13 +0000545 LARGE_INTEGER freq;
546 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000547 ret = QueryPerformanceFrequency(&freq);
548 if (ret == 0) {
549 fprintf(stderr, "Could not calibrate ticks\n");
550 exit(1);
551 }
552 clock_freq = freq.QuadPart;
553}
554
555static int64_t get_clock(void)
556{
557 LARGE_INTEGER ti;
558 QueryPerformanceCounter(&ti);
559 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
560}
561
562#else
563
564static int use_rt_clock;
565
566static void init_get_clock(void)
567{
568 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000569#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
570 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000571 {
572 struct timespec ts;
573 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
574 use_rt_clock = 1;
575 }
576 }
577#endif
578}
579
580static int64_t get_clock(void)
581{
blueswir1c5e97232009-03-07 20:06:23 +0000582#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
583 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000584 if (use_rt_clock) {
585 struct timespec ts;
586 clock_gettime(CLOCK_MONOTONIC, &ts);
587 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000588 } else
bellard1dce7c32006-07-13 23:20:22 +0000589#endif
590 {
591 /* XXX: using gettimeofday leads to problems if the date
592 changes, so it should be avoided. */
593 struct timeval tv;
594 gettimeofday(&tv, NULL);
595 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
596 }
597}
bellard1dce7c32006-07-13 23:20:22 +0000598#endif
599
pbrook2e70f6e2008-06-29 01:03:05 +0000600/* Return the virtual CPU time, based on the instruction counter. */
601static int64_t cpu_get_icount(void)
602{
603 int64_t icount;
604 CPUState *env = cpu_single_env;;
605 icount = qemu_icount;
606 if (env) {
607 if (!can_do_io(env))
608 fprintf(stderr, "Bad clock read\n");
609 icount -= (env->icount_decr.u16.low + env->icount_extra);
610 }
611 return qemu_icount_bias + (icount << icount_time_shift);
612}
613
bellard1dce7c32006-07-13 23:20:22 +0000614/***********************************************************/
615/* guest cycle counter */
616
617static int64_t cpu_ticks_prev;
618static int64_t cpu_ticks_offset;
619static int64_t cpu_clock_offset;
620static int cpu_ticks_enabled;
621
622/* return the host CPU cycle counter and handle stop/restart */
623int64_t cpu_get_ticks(void)
624{
pbrook2e70f6e2008-06-29 01:03:05 +0000625 if (use_icount) {
626 return cpu_get_icount();
627 }
bellard1dce7c32006-07-13 23:20:22 +0000628 if (!cpu_ticks_enabled) {
629 return cpu_ticks_offset;
630 } else {
631 int64_t ticks;
632 ticks = cpu_get_real_ticks();
633 if (cpu_ticks_prev > ticks) {
634 /* Note: non increasing ticks may happen if the host uses
635 software suspend */
636 cpu_ticks_offset += cpu_ticks_prev - ticks;
637 }
638 cpu_ticks_prev = ticks;
639 return ticks + cpu_ticks_offset;
640 }
641}
642
643/* return the host CPU monotonic timer and handle stop/restart */
644static int64_t cpu_get_clock(void)
645{
646 int64_t ti;
647 if (!cpu_ticks_enabled) {
648 return cpu_clock_offset;
649 } else {
650 ti = get_clock();
651 return ti + cpu_clock_offset;
652 }
653}
654
655/* enable cpu_get_ticks() */
656void cpu_enable_ticks(void)
657{
658 if (!cpu_ticks_enabled) {
659 cpu_ticks_offset -= cpu_get_real_ticks();
660 cpu_clock_offset -= get_clock();
661 cpu_ticks_enabled = 1;
662 }
663}
664
665/* disable cpu_get_ticks() : the clock is stopped. You must not call
666 cpu_get_ticks() after that. */
667void cpu_disable_ticks(void)
668{
669 if (cpu_ticks_enabled) {
670 cpu_ticks_offset = cpu_get_ticks();
671 cpu_clock_offset = cpu_get_clock();
672 cpu_ticks_enabled = 0;
673 }
674}
675
676/***********************************************************/
677/* timers */
ths5fafdf22007-09-16 21:08:06 +0000678
bellard8a7ddc32004-03-31 19:00:16 +0000679#define QEMU_TIMER_REALTIME 0
680#define QEMU_TIMER_VIRTUAL 1
681
682struct QEMUClock {
683 int type;
684 /* XXX: add frequency */
685};
686
687struct QEMUTimer {
688 QEMUClock *clock;
689 int64_t expire_time;
690 QEMUTimerCB *cb;
691 void *opaque;
692 struct QEMUTimer *next;
693};
694
thsc8994012007-08-19 21:56:03 +0000695struct qemu_alarm_timer {
696 char const *name;
thsefe75412007-08-24 01:36:32 +0000697 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000698
699 int (*start)(struct qemu_alarm_timer *t);
700 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000701 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000702 void *priv;
703};
704
thsefe75412007-08-24 01:36:32 +0000705#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000706#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000707
708static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
709{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200710 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000711}
712
713static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
714{
715 if (!alarm_has_dynticks(t))
716 return;
717
718 t->rearm(t);
719}
720
721/* TODO: MIN_TIMER_REARM_US should be optimized */
722#define MIN_TIMER_REARM_US 250
723
thsc8994012007-08-19 21:56:03 +0000724static struct qemu_alarm_timer *alarm_timer;
725
726#ifdef _WIN32
727
728struct qemu_alarm_win32 {
729 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000730 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000731} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000732
733static int win32_start_timer(struct qemu_alarm_timer *t);
734static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000735static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000736
737#else
738
739static int unix_start_timer(struct qemu_alarm_timer *t);
740static void unix_stop_timer(struct qemu_alarm_timer *t);
741
ths231c6582007-08-26 17:29:15 +0000742#ifdef __linux__
743
thsefe75412007-08-24 01:36:32 +0000744static int dynticks_start_timer(struct qemu_alarm_timer *t);
745static void dynticks_stop_timer(struct qemu_alarm_timer *t);
746static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
747
thsc40ec5a2007-08-19 22:09:40 +0000748static int hpet_start_timer(struct qemu_alarm_timer *t);
749static void hpet_stop_timer(struct qemu_alarm_timer *t);
750
thsc8994012007-08-19 21:56:03 +0000751static int rtc_start_timer(struct qemu_alarm_timer *t);
752static void rtc_stop_timer(struct qemu_alarm_timer *t);
753
thsefe75412007-08-24 01:36:32 +0000754#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000755
756#endif /* _WIN32 */
757
pbrook2e70f6e2008-06-29 01:03:05 +0000758/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000759 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000760 When the guest is idle real and virtual time will be aligned in
761 the IO wait loop. */
762#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
763
764static void icount_adjust(void)
765{
766 int64_t cur_time;
767 int64_t cur_icount;
768 int64_t delta;
769 static int64_t last_delta;
770 /* If the VM is not running, then do nothing. */
771 if (!vm_running)
772 return;
773
774 cur_time = cpu_get_clock();
775 cur_icount = qemu_get_clock(vm_clock);
776 delta = cur_icount - cur_time;
777 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
778 if (delta > 0
779 && last_delta + ICOUNT_WOBBLE < delta * 2
780 && icount_time_shift > 0) {
781 /* The guest is getting too far ahead. Slow time down. */
782 icount_time_shift--;
783 }
784 if (delta < 0
785 && last_delta - ICOUNT_WOBBLE > delta * 2
786 && icount_time_shift < MAX_ICOUNT_SHIFT) {
787 /* The guest is getting too far behind. Speed time up. */
788 icount_time_shift++;
789 }
790 last_delta = delta;
791 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
792}
793
794static void icount_adjust_rt(void * opaque)
795{
796 qemu_mod_timer(icount_rt_timer,
797 qemu_get_clock(rt_clock) + 1000);
798 icount_adjust();
799}
800
801static void icount_adjust_vm(void * opaque)
802{
803 qemu_mod_timer(icount_vm_timer,
804 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
805 icount_adjust();
806}
807
808static void init_icount_adjust(void)
809{
810 /* Have both realtime and virtual time triggers for speed adjustment.
811 The realtime trigger catches emulated time passing too slowly,
812 the virtual time trigger catches emulated time passing too fast.
813 Realtime triggers occur even when idle, so use them less frequently
814 than VM triggers. */
815 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
816 qemu_mod_timer(icount_rt_timer,
817 qemu_get_clock(rt_clock) + 1000);
818 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
819 qemu_mod_timer(icount_vm_timer,
820 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
821}
822
thsc8994012007-08-19 21:56:03 +0000823static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000824#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000825#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000826 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
827 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000828 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000829 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000830 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000831 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000832#endif
thsefe75412007-08-24 01:36:32 +0000833 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000834#else
thsefe75412007-08-24 01:36:32 +0000835 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
836 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
837 {"win32", 0, win32_start_timer,
838 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000839#endif
840 {NULL, }
841};
842
blueswir13f47aa82008-03-09 06:59:01 +0000843static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000844{
845 int i;
846
847 printf("Available alarm timers, in order of precedence:\n");
848 for (i = 0; alarm_timers[i].name; i++)
849 printf("%s\n", alarm_timers[i].name);
850}
851
852static void configure_alarms(char const *opt)
853{
854 int i;
855 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000856 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000857 char *arg;
858 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000859 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000860
aurel323adda042008-03-09 23:43:49 +0000861 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000862 show_available_alarms();
863 exit(0);
864 }
865
866 arg = strdup(opt);
867
868 /* Reorder the array */
869 name = strtok(arg, ",");
870 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000871 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000872 if (!strcmp(alarm_timers[i].name, name))
873 break;
874 }
875
876 if (i == count) {
877 fprintf(stderr, "Unknown clock %s\n", name);
878 goto next;
879 }
880
881 if (i < cur)
882 /* Ignore */
883 goto next;
884
885 /* Swap */
886 tmp = alarm_timers[i];
887 alarm_timers[i] = alarm_timers[cur];
888 alarm_timers[cur] = tmp;
889
890 cur++;
891next:
892 name = strtok(NULL, ",");
893 }
894
895 free(arg);
896
897 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000898 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000899 for (i = cur; i < count; i++)
900 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000901 } else {
902 show_available_alarms();
903 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000904 }
thsf3dcfad2007-08-24 01:26:02 +0000905}
906
bellard8a7ddc32004-03-31 19:00:16 +0000907QEMUClock *rt_clock;
908QEMUClock *vm_clock;
909
910static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +0000911
pbrook9596ebb2007-11-18 01:44:38 +0000912static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000913{
914 QEMUClock *clock;
915 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +0000916 clock->type = type;
917 return clock;
918}
919
920QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
921{
922 QEMUTimer *ts;
923
924 ts = qemu_mallocz(sizeof(QEMUTimer));
925 ts->clock = clock;
926 ts->cb = cb;
927 ts->opaque = opaque;
928 return ts;
929}
930
931void qemu_free_timer(QEMUTimer *ts)
932{
933 qemu_free(ts);
934}
935
936/* stop a timer, but do not dealloc it */
937void qemu_del_timer(QEMUTimer *ts)
938{
939 QEMUTimer **pt, *t;
940
941 /* NOTE: this code must be signal safe because
942 qemu_timer_expired() can be called from a signal. */
943 pt = &active_timers[ts->clock->type];
944 for(;;) {
945 t = *pt;
946 if (!t)
947 break;
948 if (t == ts) {
949 *pt = t->next;
950 break;
951 }
952 pt = &t->next;
953 }
954}
955
956/* modify the current timer so that it will be fired when current_time
957 >= expire_time. The corresponding callback will be called. */
958void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
959{
960 QEMUTimer **pt, *t;
961
962 qemu_del_timer(ts);
963
964 /* add the timer in the sorted list */
965 /* NOTE: this code must be signal safe because
966 qemu_timer_expired() can be called from a signal. */
967 pt = &active_timers[ts->clock->type];
968 for(;;) {
969 t = *pt;
970 if (!t)
971 break;
ths5fafdf22007-09-16 21:08:06 +0000972 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +0000973 break;
974 pt = &t->next;
975 }
976 ts->expire_time = expire_time;
977 ts->next = *pt;
978 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +0000979
980 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +0000981 if (pt == &active_timers[ts->clock->type]) {
982 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
983 qemu_rearm_alarm_timer(alarm_timer);
984 }
985 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +0000986 if (use_icount)
987 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +0000988 }
bellard8a7ddc32004-03-31 19:00:16 +0000989}
990
991int qemu_timer_pending(QEMUTimer *ts)
992{
993 QEMUTimer *t;
994 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
995 if (t == ts)
996 return 1;
997 }
998 return 0;
999}
1000
1001static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1002{
1003 if (!timer_head)
1004 return 0;
1005 return (timer_head->expire_time <= current_time);
1006}
1007
1008static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1009{
1010 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001011
bellard8a7ddc32004-03-31 19:00:16 +00001012 for(;;) {
1013 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001014 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001015 break;
1016 /* remove timer from the list before calling the callback */
1017 *ptimer_head = ts->next;
1018 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001019
bellard8a7ddc32004-03-31 19:00:16 +00001020 /* run the callback (the timer list can be modified) */
1021 ts->cb(ts->opaque);
1022 }
1023}
1024
1025int64_t qemu_get_clock(QEMUClock *clock)
1026{
1027 switch(clock->type) {
1028 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001029 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001030 default:
1031 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001032 if (use_icount) {
1033 return cpu_get_icount();
1034 } else {
1035 return cpu_get_clock();
1036 }
bellard8a7ddc32004-03-31 19:00:16 +00001037 }
1038}
1039
bellard1dce7c32006-07-13 23:20:22 +00001040static void init_timers(void)
1041{
1042 init_get_clock();
1043 ticks_per_sec = QEMU_TIMER_BASE;
1044 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1045 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1046}
1047
bellard8a7ddc32004-03-31 19:00:16 +00001048/* save a timer */
1049void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1050{
1051 uint64_t expire_time;
1052
1053 if (qemu_timer_pending(ts)) {
1054 expire_time = ts->expire_time;
1055 } else {
1056 expire_time = -1;
1057 }
1058 qemu_put_be64(f, expire_time);
1059}
1060
1061void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1062{
1063 uint64_t expire_time;
1064
1065 expire_time = qemu_get_be64(f);
1066 if (expire_time != -1) {
1067 qemu_mod_timer(ts, expire_time);
1068 } else {
1069 qemu_del_timer(ts);
1070 }
1071}
1072
1073static void timer_save(QEMUFile *f, void *opaque)
1074{
1075 if (cpu_ticks_enabled) {
1076 hw_error("cannot save state if virtual timers are running");
1077 }
thsbee8d682007-12-16 23:41:11 +00001078 qemu_put_be64(f, cpu_ticks_offset);
1079 qemu_put_be64(f, ticks_per_sec);
1080 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001081}
1082
1083static int timer_load(QEMUFile *f, void *opaque, int version_id)
1084{
bellardc88676f2006-08-06 13:36:11 +00001085 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001086 return -EINVAL;
1087 if (cpu_ticks_enabled) {
1088 return -EINVAL;
1089 }
thsbee8d682007-12-16 23:41:11 +00001090 cpu_ticks_offset=qemu_get_be64(f);
1091 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001092 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001093 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001094 }
bellard8a7ddc32004-03-31 19:00:16 +00001095 return 0;
1096}
1097
aliguori50317c72009-04-24 18:03:29 +00001098static void qemu_event_increment(void);
1099
bellard67b915a2004-03-31 23:37:16 +00001100#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001101static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1102 DWORD_PTR dwUser, DWORD_PTR dw1,
1103 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001104#else
bellard8a7ddc32004-03-31 19:00:16 +00001105static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001106#endif
bellard8a7ddc32004-03-31 19:00:16 +00001107{
bellard02ba45c2004-06-25 14:46:23 +00001108#if 0
1109#define DISP_FREQ 1000
1110 {
1111 static int64_t delta_min = INT64_MAX;
1112 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1113 static int count;
1114 ti = qemu_get_clock(vm_clock);
1115 if (last_clock != 0) {
1116 delta = ti - last_clock;
1117 if (delta < delta_min)
1118 delta_min = delta;
1119 if (delta > delta_max)
1120 delta_max = delta;
1121 delta_cum += delta;
1122 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001123 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001124 muldiv64(delta_min, 1000000, ticks_per_sec),
1125 muldiv64(delta_max, 1000000, ticks_per_sec),
1126 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1127 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1128 count = 0;
1129 delta_min = INT64_MAX;
1130 delta_max = 0;
1131 delta_cum = 0;
1132 }
1133 }
1134 last_clock = ti;
1135 }
1136#endif
thsefe75412007-08-24 01:36:32 +00001137 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001138 (!use_icount &&
1139 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1140 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001141 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1142 qemu_get_clock(rt_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001143 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001144 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001145
aliguorid6dc3d42009-04-24 18:04:07 +00001146#ifndef CONFIG_IOTHREAD
1147 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001148 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001149 cpu_exit(next_cpu);
blueswir1640f42e2009-04-19 10:18:01 +00001150#ifdef CONFIG_KQEMU
aliguorid6dc3d42009-04-24 18:04:07 +00001151 if (next_cpu->kqemu_enabled) {
1152 kqemu_cpu_interrupt(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001153 }
balrogee5605e2007-12-03 03:01:40 +00001154#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001155 }
aliguorid6dc3d42009-04-24 18:04:07 +00001156#endif
aliguori43b96852009-04-24 18:03:33 +00001157 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001158 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001159 }
1160}
1161
pbrook2e70f6e2008-06-29 01:03:05 +00001162static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001163{
pbrook2e70f6e2008-06-29 01:03:05 +00001164 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001165
1166 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001167 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1168 qemu_get_clock(vm_clock);
1169 } else {
1170 /* To avoid problems with overflow limit this to 2^32. */
1171 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001172 }
1173
pbrook2e70f6e2008-06-29 01:03:05 +00001174 if (delta < 0)
1175 delta = 0;
thsefe75412007-08-24 01:36:32 +00001176
pbrook2e70f6e2008-06-29 01:03:05 +00001177 return delta;
1178}
1179
blueswir18632fb92008-09-14 13:59:34 +00001180#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001181static uint64_t qemu_next_deadline_dyntick(void)
1182{
1183 int64_t delta;
1184 int64_t rtdelta;
1185
1186 if (use_icount)
1187 delta = INT32_MAX;
1188 else
1189 delta = (qemu_next_deadline() + 999) / 1000;
1190
1191 if (active_timers[QEMU_TIMER_REALTIME]) {
1192 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1193 qemu_get_clock(rt_clock))*1000;
1194 if (rtdelta < delta)
1195 delta = rtdelta;
1196 }
1197
1198 if (delta < MIN_TIMER_REARM_US)
1199 delta = MIN_TIMER_REARM_US;
1200
1201 return delta;
thsefe75412007-08-24 01:36:32 +00001202}
blueswir18632fb92008-09-14 13:59:34 +00001203#endif
thsefe75412007-08-24 01:36:32 +00001204
bellardfd872592004-05-12 19:11:15 +00001205#ifndef _WIN32
1206
aliguori7183b4b2008-11-05 20:40:18 +00001207/* Sets a specific flag */
1208static int fcntl_setfl(int fd, int flag)
1209{
1210 int flags;
1211
1212 flags = fcntl(fd, F_GETFL);
1213 if (flags == -1)
1214 return -errno;
1215
1216 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1217 return -errno;
1218
1219 return 0;
1220}
1221
bellard829309c2004-05-20 13:20:12 +00001222#if defined(__linux__)
1223
bellardfd872592004-05-12 19:11:15 +00001224#define RTC_FREQ 1024
1225
aurel32de9a95f2008-11-11 13:41:01 +00001226static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001227{
thsc8994012007-08-19 21:56:03 +00001228 struct sigaction act;
1229
1230 /* timer signal */
1231 sigfillset(&act.sa_mask);
1232 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001233 act.sa_handler = host_alarm_handler;
1234
1235 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001236 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001237 fcntl(fd, F_SETOWN, getpid());
1238}
1239
thsc40ec5a2007-08-19 22:09:40 +00001240static int hpet_start_timer(struct qemu_alarm_timer *t)
1241{
1242 struct hpet_info info;
1243 int r, fd;
1244
1245 fd = open("/dev/hpet", O_RDONLY);
1246 if (fd < 0)
1247 return -1;
1248
1249 /* Set frequency */
1250 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1251 if (r < 0) {
1252 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1253 "error, but for better emulation accuracy type:\n"
1254 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1255 goto fail;
1256 }
1257
1258 /* Check capabilities */
1259 r = ioctl(fd, HPET_INFO, &info);
1260 if (r < 0)
1261 goto fail;
1262
1263 /* Enable periodic mode */
1264 r = ioctl(fd, HPET_EPI, 0);
1265 if (info.hi_flags && (r < 0))
1266 goto fail;
1267
1268 /* Enable interrupt */
1269 r = ioctl(fd, HPET_IE_ON, 0);
1270 if (r < 0)
1271 goto fail;
1272
1273 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001274 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001275
1276 return 0;
1277fail:
1278 close(fd);
1279 return -1;
1280}
1281
1282static void hpet_stop_timer(struct qemu_alarm_timer *t)
1283{
pbrookfcdc2122007-08-23 20:22:22 +00001284 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001285
1286 close(fd);
1287}
1288
thsc8994012007-08-19 21:56:03 +00001289static int rtc_start_timer(struct qemu_alarm_timer *t)
1290{
1291 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001292 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001293
balrogaeb30be2007-07-02 15:03:13 +00001294 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001295 if (rtc_fd < 0)
1296 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001297 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1298 if (current_rtc_freq != RTC_FREQ &&
1299 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001300 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1301 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1302 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1303 goto fail;
1304 }
1305 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1306 fail:
1307 close(rtc_fd);
1308 return -1;
1309 }
thsc8994012007-08-19 21:56:03 +00001310
1311 enable_sigio_timer(rtc_fd);
1312
pbrookfcdc2122007-08-23 20:22:22 +00001313 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001314
bellardfd872592004-05-12 19:11:15 +00001315 return 0;
1316}
1317
thsc8994012007-08-19 21:56:03 +00001318static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001319{
pbrookfcdc2122007-08-23 20:22:22 +00001320 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001321
1322 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001323}
1324
thsefe75412007-08-24 01:36:32 +00001325static int dynticks_start_timer(struct qemu_alarm_timer *t)
1326{
1327 struct sigevent ev;
1328 timer_t host_timer;
1329 struct sigaction act;
1330
1331 sigfillset(&act.sa_mask);
1332 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001333 act.sa_handler = host_alarm_handler;
1334
1335 sigaction(SIGALRM, &act, NULL);
1336
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001337 /*
1338 * Initialize ev struct to 0 to avoid valgrind complaining
1339 * about uninitialized data in timer_create call
1340 */
1341 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001342 ev.sigev_value.sival_int = 0;
1343 ev.sigev_notify = SIGEV_SIGNAL;
1344 ev.sigev_signo = SIGALRM;
1345
1346 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1347 perror("timer_create");
1348
1349 /* disable dynticks */
1350 fprintf(stderr, "Dynamic Ticks disabled\n");
1351
1352 return -1;
1353 }
1354
blueswir10399bfe2008-11-16 11:37:18 +00001355 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001356
1357 return 0;
1358}
1359
1360static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1361{
blueswir10399bfe2008-11-16 11:37:18 +00001362 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001363
1364 timer_delete(host_timer);
1365}
1366
1367static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1368{
blueswir10399bfe2008-11-16 11:37:18 +00001369 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001370 struct itimerspec timeout;
1371 int64_t nearest_delta_us = INT64_MAX;
1372 int64_t current_us;
1373
1374 if (!active_timers[QEMU_TIMER_REALTIME] &&
1375 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001376 return;
thsefe75412007-08-24 01:36:32 +00001377
pbrook2e70f6e2008-06-29 01:03:05 +00001378 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001379
1380 /* check whether a timer is already running */
1381 if (timer_gettime(host_timer, &timeout)) {
1382 perror("gettime");
1383 fprintf(stderr, "Internal timer error: aborting\n");
1384 exit(1);
1385 }
1386 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1387 if (current_us && current_us <= nearest_delta_us)
1388 return;
1389
1390 timeout.it_interval.tv_sec = 0;
1391 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1392 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1393 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1394 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1395 perror("settime");
1396 fprintf(stderr, "Internal timer error: aborting\n");
1397 exit(1);
1398 }
1399}
1400
ths70744b32007-08-26 17:31:30 +00001401#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001402
thsc8994012007-08-19 21:56:03 +00001403static int unix_start_timer(struct qemu_alarm_timer *t)
1404{
1405 struct sigaction act;
1406 struct itimerval itv;
1407 int err;
1408
1409 /* timer signal */
1410 sigfillset(&act.sa_mask);
1411 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001412 act.sa_handler = host_alarm_handler;
1413
1414 sigaction(SIGALRM, &act, NULL);
1415
1416 itv.it_interval.tv_sec = 0;
1417 /* for i386 kernel 2.6 to get 1 ms */
1418 itv.it_interval.tv_usec = 999;
1419 itv.it_value.tv_sec = 0;
1420 itv.it_value.tv_usec = 10 * 1000;
1421
1422 err = setitimer(ITIMER_REAL, &itv, NULL);
1423 if (err)
1424 return -1;
1425
1426 return 0;
1427}
1428
1429static void unix_stop_timer(struct qemu_alarm_timer *t)
1430{
1431 struct itimerval itv;
1432
1433 memset(&itv, 0, sizeof(itv));
1434 setitimer(ITIMER_REAL, &itv, NULL);
1435}
1436
bellard829309c2004-05-20 13:20:12 +00001437#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001438
aliguorif49e58d2008-11-05 21:22:34 +00001439
thsc8994012007-08-19 21:56:03 +00001440#ifdef _WIN32
1441
1442static int win32_start_timer(struct qemu_alarm_timer *t)
1443{
1444 TIMECAPS tc;
1445 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001446 UINT flags;
thsc8994012007-08-19 21:56:03 +00001447
thsc8994012007-08-19 21:56:03 +00001448 memset(&tc, 0, sizeof(tc));
1449 timeGetDevCaps(&tc, sizeof(tc));
1450
1451 if (data->period < tc.wPeriodMin)
1452 data->period = tc.wPeriodMin;
1453
1454 timeBeginPeriod(data->period);
1455
thsefe75412007-08-24 01:36:32 +00001456 flags = TIME_CALLBACK_FUNCTION;
1457 if (alarm_has_dynticks(t))
1458 flags |= TIME_ONESHOT;
1459 else
1460 flags |= TIME_PERIODIC;
1461
thsc8994012007-08-19 21:56:03 +00001462 data->timerId = timeSetEvent(1, // interval (ms)
1463 data->period, // resolution
1464 host_alarm_handler, // function
1465 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001466 flags);
thsc8994012007-08-19 21:56:03 +00001467
1468 if (!data->timerId) {
1469 perror("Failed to initialize win32 alarm timer");
thsc8994012007-08-19 21:56:03 +00001470 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001471 return -1;
1472 }
1473
thsc8994012007-08-19 21:56:03 +00001474 return 0;
1475}
1476
1477static void win32_stop_timer(struct qemu_alarm_timer *t)
1478{
1479 struct qemu_alarm_win32 *data = t->priv;
1480
1481 timeKillEvent(data->timerId);
1482 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001483}
1484
thsefe75412007-08-24 01:36:32 +00001485static void win32_rearm_timer(struct qemu_alarm_timer *t)
1486{
1487 struct qemu_alarm_win32 *data = t->priv;
1488 uint64_t nearest_delta_us;
1489
1490 if (!active_timers[QEMU_TIMER_REALTIME] &&
1491 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001492 return;
thsefe75412007-08-24 01:36:32 +00001493
pbrook2e70f6e2008-06-29 01:03:05 +00001494 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001495 nearest_delta_us /= 1000;
1496
1497 timeKillEvent(data->timerId);
1498
1499 data->timerId = timeSetEvent(1,
1500 data->period,
1501 host_alarm_handler,
1502 (DWORD)t,
1503 TIME_ONESHOT | TIME_PERIODIC);
1504
1505 if (!data->timerId) {
1506 perror("Failed to re-arm win32 alarm timer");
1507
1508 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001509 exit(1);
1510 }
1511}
1512
thsc8994012007-08-19 21:56:03 +00001513#endif /* _WIN32 */
1514
aliguori7183b4b2008-11-05 20:40:18 +00001515static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001516{
blueswir1223f0d72008-09-30 18:12:18 +00001517 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001518 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001519
thsc8994012007-08-19 21:56:03 +00001520 for (i = 0; alarm_timers[i].name; i++) {
1521 t = &alarm_timers[i];
1522
thsc8994012007-08-19 21:56:03 +00001523 err = t->start(t);
1524 if (!err)
1525 break;
bellard67b915a2004-03-31 23:37:16 +00001526 }
bellardfd872592004-05-12 19:11:15 +00001527
thsc8994012007-08-19 21:56:03 +00001528 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001529 err = -ENOENT;
1530 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001531 }
thsc8994012007-08-19 21:56:03 +00001532
1533 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001534
aliguori6abfbd72008-11-05 20:49:37 +00001535 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001536
1537fail:
aliguori7183b4b2008-11-05 20:40:18 +00001538 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001539}
1540
pbrook9596ebb2007-11-18 01:44:38 +00001541static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001542{
thsc8994012007-08-19 21:56:03 +00001543 alarm_timer->stop(alarm_timer);
1544 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001545}
1546
bellardc4b1fcc2004-03-14 21:44:30 +00001547/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001548/* host time/date access */
1549void qemu_get_timedate(struct tm *tm, int offset)
1550{
1551 time_t ti;
1552 struct tm *ret;
1553
1554 time(&ti);
1555 ti += offset;
1556 if (rtc_date_offset == -1) {
1557 if (rtc_utc)
1558 ret = gmtime(&ti);
1559 else
1560 ret = localtime(&ti);
1561 } else {
1562 ti -= rtc_date_offset;
1563 ret = gmtime(&ti);
1564 }
1565
1566 memcpy(tm, ret, sizeof(struct tm));
1567}
1568
1569int qemu_timedate_diff(struct tm *tm)
1570{
1571 time_t seconds;
1572
1573 if (rtc_date_offset == -1)
1574 if (rtc_utc)
1575 seconds = mktimegm(tm);
1576 else
1577 seconds = mktime(tm);
1578 else
1579 seconds = mktimegm(tm) + rtc_date_offset;
1580
1581 return seconds - time(NULL);
1582}
1583
bellardfd1dff42006-02-01 21:29:26 +00001584#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001585static void socket_cleanup(void)
1586{
1587 WSACleanup();
1588}
bellard82c643f2004-07-14 17:28:13 +00001589
bellardfd1dff42006-02-01 21:29:26 +00001590static int socket_init(void)
1591{
1592 WSADATA Data;
1593 int ret, err;
1594
1595 ret = WSAStartup(MAKEWORD(2,2), &Data);
1596 if (ret != 0) {
1597 err = WSAGetLastError();
1598 fprintf(stderr, "WSAStartup: %d\n", err);
1599 return -1;
1600 }
1601 atexit(socket_cleanup);
1602 return 0;
1603}
aurel3264b7b732008-05-05 10:05:31 +00001604#endif
1605
Jan Kiszka5db4af82009-06-24 14:42:28 +02001606int get_next_param_value(char *buf, int buf_size,
1607 const char *tag, const char **pstr)
bellard7c9d8e02005-11-15 22:16:05 +00001608{
1609 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001610 char option[128];
1611
Jan Kiszka5db4af82009-06-24 14:42:28 +02001612 p = *pstr;
bellard7c9d8e02005-11-15 22:16:05 +00001613 for(;;) {
aliguori268a3622009-04-21 22:30:27 +00001614 p = get_opt_name(option, sizeof(option), p, '=');
bellard7c9d8e02005-11-15 22:16:05 +00001615 if (*p != '=')
1616 break;
1617 p++;
1618 if (!strcmp(tag, option)) {
Jan Kiszka5db4af82009-06-24 14:42:28 +02001619 *pstr = get_opt_value(buf, buf_size, p);
1620 if (**pstr == ',') {
1621 (*pstr)++;
1622 }
thse4bcb142007-12-02 04:51:10 +00001623 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001624 } else {
balrog609497a2008-01-14 02:56:53 +00001625 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001626 }
1627 if (*p != ',')
1628 break;
1629 p++;
1630 }
1631 return 0;
1632}
1633
Jan Kiszka5db4af82009-06-24 14:42:28 +02001634int get_param_value(char *buf, int buf_size,
1635 const char *tag, const char *str)
1636{
1637 return get_next_param_value(buf, buf_size, tag, &str);
1638}
1639
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001640int check_params(char *buf, int buf_size,
1641 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001642{
1643 const char *p;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001644 int i;
thse4bcb142007-12-02 04:51:10 +00001645
1646 p = str;
aliguori10300212009-04-21 19:56:23 +00001647 while (*p != '\0') {
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001648 p = get_opt_name(buf, buf_size, p, '=');
Jan Kiszkaffad4112009-04-26 18:53:42 +02001649 if (*p != '=') {
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001650 return -1;
Jan Kiszkaffad4112009-04-26 18:53:42 +02001651 }
thse4bcb142007-12-02 04:51:10 +00001652 p++;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001653 for (i = 0; params[i] != NULL; i++) {
1654 if (!strcmp(params[i], buf)) {
thse4bcb142007-12-02 04:51:10 +00001655 break;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001656 }
1657 }
Jan Kiszkaffad4112009-04-26 18:53:42 +02001658 if (params[i] == NULL) {
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001659 return -1;
Jan Kiszkaffad4112009-04-26 18:53:42 +02001660 }
balrog609497a2008-01-14 02:56:53 +00001661 p = get_opt_value(NULL, 0, p);
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001662 if (*p != ',') {
thse4bcb142007-12-02 04:51:10 +00001663 break;
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001664 }
thse4bcb142007-12-02 04:51:10 +00001665 p++;
1666 }
Jan Kiszka0aa7a202009-05-08 12:34:17 +02001667 return 0;
thse4bcb142007-12-02 04:51:10 +00001668}
1669
balrog1ae26a12008-09-28 23:19:47 +00001670/***********************************************************/
1671/* Bluetooth support */
1672static int nb_hcis;
1673static int cur_hci;
1674static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001675
balrog1ae26a12008-09-28 23:19:47 +00001676static struct bt_vlan_s {
1677 struct bt_scatternet_s net;
1678 int id;
1679 struct bt_vlan_s *next;
1680} *first_bt_vlan;
1681
1682/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001683static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001684{
1685 struct bt_vlan_s **pvlan, *vlan;
1686 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1687 if (vlan->id == id)
1688 return &vlan->net;
1689 }
1690 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1691 vlan->id = id;
1692 pvlan = &first_bt_vlan;
1693 while (*pvlan != NULL)
1694 pvlan = &(*pvlan)->next;
1695 *pvlan = vlan;
1696 return &vlan->net;
1697}
1698
1699static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1700{
1701}
1702
1703static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1704{
1705 return -ENOTSUP;
1706}
1707
1708static struct HCIInfo null_hci = {
1709 .cmd_send = null_hci_send,
1710 .sco_send = null_hci_send,
1711 .acl_send = null_hci_send,
1712 .bdaddr_set = null_hci_addr_set,
1713};
1714
1715struct HCIInfo *qemu_next_hci(void)
1716{
1717 if (cur_hci == nb_hcis)
1718 return &null_hci;
1719
1720 return hci_table[cur_hci++];
1721}
1722
balrogdc72ac12008-11-09 00:04:26 +00001723static struct HCIInfo *hci_init(const char *str)
1724{
1725 char *endp;
1726 struct bt_scatternet_s *vlan = 0;
1727
1728 if (!strcmp(str, "null"))
1729 /* null */
1730 return &null_hci;
1731 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1732 /* host[:hciN] */
1733 return bt_host_hci(str[4] ? str + 5 : "hci0");
1734 else if (!strncmp(str, "hci", 3)) {
1735 /* hci[,vlan=n] */
1736 if (str[3]) {
1737 if (!strncmp(str + 3, ",vlan=", 6)) {
1738 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1739 if (*endp)
1740 vlan = 0;
1741 }
1742 } else
1743 vlan = qemu_find_bt_vlan(0);
1744 if (vlan)
1745 return bt_new_hci(vlan);
1746 }
1747
1748 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1749
1750 return 0;
1751}
1752
1753static int bt_hci_parse(const char *str)
1754{
1755 struct HCIInfo *hci;
1756 bdaddr_t bdaddr;
1757
1758 if (nb_hcis >= MAX_NICS) {
1759 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1760 return -1;
1761 }
1762
1763 hci = hci_init(str);
1764 if (!hci)
1765 return -1;
1766
1767 bdaddr.b[0] = 0x52;
1768 bdaddr.b[1] = 0x54;
1769 bdaddr.b[2] = 0x00;
1770 bdaddr.b[3] = 0x12;
1771 bdaddr.b[4] = 0x34;
1772 bdaddr.b[5] = 0x56 + nb_hcis;
1773 hci->bdaddr_set(hci, bdaddr.b);
1774
1775 hci_table[nb_hcis++] = hci;
1776
1777 return 0;
1778}
1779
1780static void bt_vhci_add(int vlan_id)
1781{
1782 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1783
1784 if (!vlan->slave)
1785 fprintf(stderr, "qemu: warning: adding a VHCI to "
1786 "an empty scatternet %i\n", vlan_id);
1787
1788 bt_vhci_init(bt_new_hci(vlan));
1789}
1790
1791static struct bt_device_s *bt_device_add(const char *opt)
1792{
1793 struct bt_scatternet_s *vlan;
1794 int vlan_id = 0;
1795 char *endp = strstr(opt, ",vlan=");
1796 int len = (endp ? endp - opt : strlen(opt)) + 1;
1797 char devname[10];
1798
1799 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1800
1801 if (endp) {
1802 vlan_id = strtol(endp + 6, &endp, 0);
1803 if (*endp) {
1804 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1805 return 0;
1806 }
1807 }
1808
1809 vlan = qemu_find_bt_vlan(vlan_id);
1810
1811 if (!vlan->slave)
1812 fprintf(stderr, "qemu: warning: adding a slave device to "
1813 "an empty scatternet %i\n", vlan_id);
1814
1815 if (!strcmp(devname, "keyboard"))
1816 return bt_keyboard_init(vlan);
1817
1818 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1819 return 0;
1820}
1821
1822static int bt_parse(const char *opt)
1823{
1824 const char *endp, *p;
1825 int vlan;
1826
1827 if (strstart(opt, "hci", &endp)) {
1828 if (!*endp || *endp == ',') {
1829 if (*endp)
1830 if (!strstart(endp, ",vlan=", 0))
1831 opt = endp + 1;
1832
1833 return bt_hci_parse(opt);
1834 }
1835 } else if (strstart(opt, "vhci", &endp)) {
1836 if (!*endp || *endp == ',') {
1837 if (*endp) {
1838 if (strstart(endp, ",vlan=", &p)) {
1839 vlan = strtol(p, (char **) &endp, 0);
1840 if (*endp) {
1841 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1842 return 1;
1843 }
1844 } else {
1845 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1846 return 1;
1847 }
1848 } else
1849 vlan = 0;
1850
1851 bt_vhci_add(vlan);
1852 return 0;
1853 }
1854 } else if (strstart(opt, "device:", &endp))
1855 return !bt_device_add(endp);
1856
1857 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1858 return 1;
1859}
1860
balrog1ae26a12008-09-28 23:19:47 +00001861/***********************************************************/
1862/* QEMU Block devices */
1863
balrog609497a2008-01-14 02:56:53 +00001864#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001865#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001866#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001867#define PFLASH_ALIAS "if=pflash"
1868#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001869#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001870
aliguori7d5aca92009-02-11 15:19:58 +00001871static int drive_opt_get_free_idx(void)
1872{
1873 int index;
1874
1875 for (index = 0; index < MAX_DRIVES; index++)
1876 if (!drives_opt[index].used) {
1877 drives_opt[index].used = 1;
1878 return index;
1879 }
1880
1881 return -1;
1882}
1883
1884static int drive_get_free_idx(void)
1885{
1886 int index;
1887
1888 for (index = 0; index < MAX_DRIVES; index++)
1889 if (!drives_table[index].used) {
1890 drives_table[index].used = 1;
1891 return index;
1892 }
1893
1894 return -1;
1895}
1896
aliguori4d73cd32009-02-11 15:20:46 +00001897int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001898{
1899 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00001900 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00001901
aliguori7d5aca92009-02-11 15:19:58 +00001902 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00001903 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00001904 return -1;
thse4bcb142007-12-02 04:51:10 +00001905 }
1906
aliguori7d5aca92009-02-11 15:19:58 +00001907 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00001908 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00001909 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00001910 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001911 va_end(ap);
1912
aliguori7d5aca92009-02-11 15:19:58 +00001913 nb_drives_opt++;
1914 return index;
thse4bcb142007-12-02 04:51:10 +00001915}
1916
aliguorib01b1112009-02-11 15:20:20 +00001917void drive_remove(int index)
1918{
1919 drives_opt[index].used = 0;
1920 nb_drives_opt--;
1921}
1922
thsf60d39b2007-12-17 03:55:57 +00001923int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001924{
1925 int index;
1926
1927 /* seek interface, bus and unit */
1928
aliguori7d5aca92009-02-11 15:19:58 +00001929 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00001930 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00001931 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00001932 drives_table[index].unit == unit &&
1933 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00001934 return index;
1935
1936 return -1;
1937}
1938
thsf60d39b2007-12-17 03:55:57 +00001939int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001940{
1941 int max_bus;
1942 int index;
1943
1944 max_bus = -1;
1945 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00001946 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00001947 drives_table[index].bus > max_bus)
1948 max_bus = drives_table[index].bus;
1949 }
1950 return max_bus;
1951}
1952
aliguorifa879c62009-01-07 17:32:33 +00001953const char *drive_get_serial(BlockDriverState *bdrv)
1954{
1955 int index;
1956
1957 for (index = 0; index < nb_drives; index++)
1958 if (drives_table[index].bdrv == bdrv)
1959 return drives_table[index].serial;
1960
1961 return "\0";
1962}
1963
aliguori428c5702009-01-21 18:59:04 +00001964BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1965{
1966 int index;
1967
1968 for (index = 0; index < nb_drives; index++)
1969 if (drives_table[index].bdrv == bdrv)
1970 return drives_table[index].onerror;
1971
aliguoricdad4bd2009-02-28 16:51:01 +00001972 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00001973}
1974
aurel32a1620fa2008-04-29 05:58:01 +00001975static void bdrv_format_print(void *opaque, const char *name)
1976{
1977 fprintf(stderr, " %s", name);
1978}
1979
aliguorib01b1112009-02-11 15:20:20 +00001980void drive_uninit(BlockDriverState *bdrv)
1981{
1982 int i;
1983
1984 for (i = 0; i < MAX_DRIVES; i++)
1985 if (drives_table[i].bdrv == bdrv) {
1986 drives_table[i].bdrv = NULL;
1987 drives_table[i].used = 0;
1988 drive_remove(drives_table[i].drive_opt_idx);
1989 nb_drives--;
1990 break;
1991 }
1992}
1993
aliguori4d73cd32009-02-11 15:20:46 +00001994int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00001995{
1996 char buf[128];
1997 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00001998 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00001999 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002000 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002001 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002002 enum { MEDIA_DISK, MEDIA_CDROM } media;
2003 int bus_id, unit_id;
2004 int cyls, heads, secs, translation;
2005 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002006 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002007 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002008 int max_devs;
2009 int index;
balrog33f00272007-12-24 14:33:24 +00002010 int cache;
aliguori428c5702009-01-21 18:59:04 +00002011 int bdrv_flags, onerror;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002012 const char *devaddr;
aliguori7d5aca92009-02-11 15:19:58 +00002013 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002014 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002015 static const char * const params[] = { "bus", "unit", "if", "index",
2016 "cyls", "heads", "secs", "trans",
2017 "media", "snapshot", "file",
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002018 "cache", "format", "serial",
2019 "werror", "addr",
aliguori428c5702009-01-21 18:59:04 +00002020 NULL };
thse4bcb142007-12-02 04:51:10 +00002021
Jan Kiszka0aa7a202009-05-08 12:34:17 +02002022 if (check_params(buf, sizeof(buf), params, str) < 0) {
Mark McLoughlincda94b22009-05-28 14:07:31 +01002023 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
2024 buf, str);
thse4bcb142007-12-02 04:51:10 +00002025 return -1;
2026 }
2027
2028 file[0] = 0;
2029 cyls = heads = secs = 0;
2030 bus_id = 0;
2031 unit_id = -1;
2032 translation = BIOS_ATA_TRANSLATION_AUTO;
2033 index = -1;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02002034 cache = 1;
thse4bcb142007-12-02 04:51:10 +00002035
blueswir1c9b1ae22008-09-28 18:55:17 +00002036 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002037 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002038 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002039 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002040 } else {
thsf60d39b2007-12-17 03:55:57 +00002041 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002042 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002043 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002044 }
2045 media = MEDIA_DISK;
2046
2047 /* extract parameters */
2048
2049 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2050 bus_id = strtol(buf, NULL, 0);
2051 if (bus_id < 0) {
2052 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2053 return -1;
2054 }
2055 }
2056
2057 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2058 unit_id = strtol(buf, NULL, 0);
2059 if (unit_id < 0) {
2060 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2061 return -1;
2062 }
2063 }
2064
2065 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002066 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002067 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002068 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002069 max_devs = MAX_IDE_DEVS;
2070 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002071 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002072 max_devs = MAX_SCSI_DEVS;
2073 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002074 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002075 max_devs = 0;
2076 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002077 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002078 max_devs = 0;
2079 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002080 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002081 max_devs = 0;
2082 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002083 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002084 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002085 } else if (!strcmp(buf, "virtio")) {
2086 type = IF_VIRTIO;
2087 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002088 } else if (!strcmp(buf, "xen")) {
2089 type = IF_XEN;
2090 max_devs = 0;
2091 } else {
thse4bcb142007-12-02 04:51:10 +00002092 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2093 return -1;
2094 }
2095 }
2096
2097 if (get_param_value(buf, sizeof(buf), "index", str)) {
2098 index = strtol(buf, NULL, 0);
2099 if (index < 0) {
2100 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2101 return -1;
2102 }
2103 }
2104
2105 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2106 cyls = strtol(buf, NULL, 0);
2107 }
2108
2109 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2110 heads = strtol(buf, NULL, 0);
2111 }
2112
2113 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2114 secs = strtol(buf, NULL, 0);
2115 }
2116
2117 if (cyls || heads || secs) {
2118 if (cyls < 1 || cyls > 16383) {
2119 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2120 return -1;
2121 }
2122 if (heads < 1 || heads > 16) {
2123 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2124 return -1;
2125 }
2126 if (secs < 1 || secs > 63) {
2127 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2128 return -1;
2129 }
2130 }
2131
2132 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2133 if (!cyls) {
2134 fprintf(stderr,
2135 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2136 str);
2137 return -1;
2138 }
2139 if (!strcmp(buf, "none"))
2140 translation = BIOS_ATA_TRANSLATION_NONE;
2141 else if (!strcmp(buf, "lba"))
2142 translation = BIOS_ATA_TRANSLATION_LBA;
2143 else if (!strcmp(buf, "auto"))
2144 translation = BIOS_ATA_TRANSLATION_AUTO;
2145 else {
2146 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2147 return -1;
2148 }
2149 }
2150
2151 if (get_param_value(buf, sizeof(buf), "media", str)) {
2152 if (!strcmp(buf, "disk")) {
2153 media = MEDIA_DISK;
2154 } else if (!strcmp(buf, "cdrom")) {
2155 if (cyls || secs || heads) {
2156 fprintf(stderr,
2157 "qemu: '%s' invalid physical CHS format\n", str);
2158 return -1;
2159 }
2160 media = MEDIA_CDROM;
2161 } else {
2162 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2163 return -1;
2164 }
2165 }
2166
2167 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2168 if (!strcmp(buf, "on"))
2169 snapshot = 1;
2170 else if (!strcmp(buf, "off"))
2171 snapshot = 0;
2172 else {
2173 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2174 return -1;
2175 }
2176 }
2177
balrog33f00272007-12-24 14:33:24 +00002178 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002179 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002180 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002181 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002182 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002183 else if (!strcmp(buf, "writeback"))
2184 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002185 else {
2186 fprintf(stderr, "qemu: invalid cache option\n");
2187 return -1;
2188 }
2189 }
2190
aurel321e72d3b2008-04-28 20:26:45 +00002191 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002192 if (strcmp(buf, "?") == 0) {
2193 fprintf(stderr, "qemu: Supported formats:");
2194 bdrv_iterate_format(bdrv_format_print, NULL);
2195 fprintf(stderr, "\n");
2196 return -1;
2197 }
aurel321e72d3b2008-04-28 20:26:45 +00002198 drv = bdrv_find_format(buf);
2199 if (!drv) {
2200 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2201 return -1;
2202 }
2203 }
2204
balrog609497a2008-01-14 02:56:53 +00002205 if (arg->file == NULL)
2206 get_param_value(file, sizeof(file), "file", str);
2207 else
2208 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002209
aliguorifa879c62009-01-07 17:32:33 +00002210 if (!get_param_value(serial, sizeof(serial), "serial", str))
2211 memset(serial, 0, sizeof(serial));
2212
aliguoricdad4bd2009-02-28 16:51:01 +00002213 onerror = BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002214 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002215 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002216 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002217 return -1;
2218 }
2219 if (!strcmp(buf, "ignore"))
2220 onerror = BLOCK_ERR_IGNORE;
2221 else if (!strcmp(buf, "enospc"))
2222 onerror = BLOCK_ERR_STOP_ENOSPC;
2223 else if (!strcmp(buf, "stop"))
2224 onerror = BLOCK_ERR_STOP_ANY;
2225 else if (!strcmp(buf, "report"))
2226 onerror = BLOCK_ERR_REPORT;
2227 else {
2228 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2229 return -1;
2230 }
2231 }
2232
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002233 devaddr = NULL;
2234 if (get_param_value(buf, sizeof(buf), "addr", str)) {
2235 if (type != IF_VIRTIO) {
2236 fprintf(stderr, "addr is not supported by in '%s'\n", str);
2237 return -1;
2238 }
2239 devaddr = strdup(buf);
2240 }
2241
thse4bcb142007-12-02 04:51:10 +00002242 /* compute bus and unit according index */
2243
2244 if (index != -1) {
2245 if (bus_id != 0 || unit_id != -1) {
2246 fprintf(stderr,
2247 "qemu: '%s' index cannot be used with bus and unit\n", str);
2248 return -1;
2249 }
2250 if (max_devs == 0)
2251 {
2252 unit_id = index;
2253 bus_id = 0;
2254 } else {
2255 unit_id = index % max_devs;
2256 bus_id = index / max_devs;
2257 }
2258 }
2259
2260 /* if user doesn't specify a unit_id,
2261 * try to find the first free
2262 */
2263
2264 if (unit_id == -1) {
2265 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002266 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002267 unit_id++;
2268 if (max_devs && unit_id >= max_devs) {
2269 unit_id -= max_devs;
2270 bus_id++;
2271 }
2272 }
2273 }
2274
2275 /* check unit id */
2276
2277 if (max_devs && unit_id >= max_devs) {
2278 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2279 str, unit_id, max_devs - 1);
2280 return -1;
2281 }
2282
2283 /*
2284 * ignore multiple definitions
2285 */
2286
thsf60d39b2007-12-17 03:55:57 +00002287 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002288 return -2;
thse4bcb142007-12-02 04:51:10 +00002289
2290 /* init */
2291
thsf60d39b2007-12-17 03:55:57 +00002292 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002293 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002294 if (max_devs)
2295 snprintf(buf, sizeof(buf), "%s%i%s%i",
2296 devname, bus_id, mediastr, unit_id);
2297 else
2298 snprintf(buf, sizeof(buf), "%s%s%i",
2299 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002300 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002301 drives_table_idx = drive_get_free_idx();
2302 drives_table[drives_table_idx].bdrv = bdrv;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002303 drives_table[drives_table_idx].devaddr = devaddr;
aliguori7d5aca92009-02-11 15:19:58 +00002304 drives_table[drives_table_idx].type = type;
2305 drives_table[drives_table_idx].bus = bus_id;
2306 drives_table[drives_table_idx].unit = unit_id;
2307 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002308 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
Anthony Liguorie6a6dfe2009-05-01 09:26:16 -05002309 strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002310 nb_drives++;
2311
thsf60d39b2007-12-17 03:55:57 +00002312 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002313 case IF_IDE:
2314 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002315 case IF_XEN:
thse4bcb142007-12-02 04:51:10 +00002316 switch(media) {
2317 case MEDIA_DISK:
2318 if (cyls != 0) {
2319 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2320 bdrv_set_translation_hint(bdrv, translation);
2321 }
2322 break;
2323 case MEDIA_CDROM:
2324 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2325 break;
2326 }
2327 break;
2328 case IF_SD:
2329 /* FIXME: This isn't really a floppy, but it's a reasonable
2330 approximation. */
2331 case IF_FLOPPY:
2332 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2333 break;
2334 case IF_PFLASH:
2335 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002336 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002337 break;
Paul Brookaae94602009-05-14 22:35:06 +01002338 case IF_COUNT:
2339 abort();
thse4bcb142007-12-02 04:51:10 +00002340 }
2341 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002342 return -2;
balrog33f00272007-12-24 14:33:24 +00002343 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002344 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002345 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002346 cache = 2; /* always use write-back with snapshot */
2347 }
2348 if (cache == 0) /* no caching */
2349 bdrv_flags |= BDRV_O_NOCACHE;
2350 else if (cache == 2) /* write-back */
2351 bdrv_flags |= BDRV_O_CACHE_WB;
aliguoric0f4ce72009-03-05 23:01:01 +00002352 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002353 fprintf(stderr, "qemu: could not open disk image %s\n",
2354 file);
2355 return -1;
2356 }
aliguoric0f4ce72009-03-05 23:01:01 +00002357 if (bdrv_key_required(bdrv))
2358 autostart = 0;
aliguori4d73cd32009-02-11 15:20:46 +00002359 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002360}
2361
Jan Kiszka76e30d02009-07-02 00:19:02 +02002362void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2363{
2364 boot_set_handler = func;
2365 boot_set_opaque = opaque;
2366}
2367
2368int qemu_boot_set(const char *boot_devices)
2369{
2370 if (!boot_set_handler) {
2371 return -EINVAL;
2372 }
2373 return boot_set_handler(boot_set_opaque, boot_devices);
2374}
2375
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002376static int parse_bootdevices(char *devices)
2377{
2378 /* We just do some generic consistency checks */
2379 const char *p;
2380 int bitmap = 0;
2381
2382 for (p = devices; *p != '\0'; p++) {
2383 /* Allowed boot devices are:
2384 * a-b: floppy disk drives
2385 * c-f: IDE disk drives
2386 * g-m: machine implementation dependant drives
2387 * n-p: network devices
2388 * It's up to each machine implementation to check if the given boot
2389 * devices match the actual hardware implementation and firmware
2390 * features.
2391 */
2392 if (*p < 'a' || *p > 'p') {
2393 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2394 exit(1);
2395 }
2396 if (bitmap & (1 << (*p - 'a'))) {
2397 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2398 exit(1);
2399 }
2400 bitmap |= 1 << (*p - 'a');
2401 }
2402 return bitmap;
2403}
2404
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002405static void restore_boot_devices(void *opaque)
2406{
2407 char *standard_boot_devices = opaque;
2408
2409 qemu_boot_set(standard_boot_devices);
2410
2411 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2412 qemu_free(standard_boot_devices);
2413}
2414
aliguori268a3622009-04-21 22:30:27 +00002415static void numa_add(const char *optarg)
2416{
2417 char option[128];
2418 char *endptr;
2419 unsigned long long value, endvalue;
2420 int nodenr;
2421
2422 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2423 if (!strcmp(option, "node")) {
2424 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2425 nodenr = nb_numa_nodes;
2426 } else {
2427 nodenr = strtoull(option, NULL, 10);
2428 }
2429
2430 if (get_param_value(option, 128, "mem", optarg) == 0) {
2431 node_mem[nodenr] = 0;
2432 } else {
2433 value = strtoull(option, &endptr, 0);
2434 switch (*endptr) {
2435 case 0: case 'M': case 'm':
2436 value <<= 20;
2437 break;
2438 case 'G': case 'g':
2439 value <<= 30;
2440 break;
2441 }
2442 node_mem[nodenr] = value;
2443 }
2444 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2445 node_cpumask[nodenr] = 0;
2446 } else {
2447 value = strtoull(option, &endptr, 10);
2448 if (value >= 64) {
2449 value = 63;
2450 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2451 } else {
2452 if (*endptr == '-') {
2453 endvalue = strtoull(endptr+1, &endptr, 10);
2454 if (endvalue >= 63) {
2455 endvalue = 62;
2456 fprintf(stderr,
2457 "only 63 CPUs in NUMA mode supported.\n");
2458 }
2459 value = (1 << (endvalue + 1)) - (1 << value);
2460 } else {
2461 value = 1 << value;
2462 }
2463 }
2464 node_cpumask[nodenr] = value;
2465 }
2466 nb_numa_nodes++;
2467 }
2468 return;
2469}
2470
bellard330d0412003-07-26 18:11:40 +00002471/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002472/* USB devices */
2473
pbrook0d92ed32006-05-21 16:30:15 +00002474static USBPort *used_usb_ports;
2475static USBPort *free_usb_ports;
2476
2477/* ??? Maybe change this to register a hub to keep track of the topology. */
2478void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2479 usb_attachfn attach)
2480{
2481 port->opaque = opaque;
2482 port->index = index;
2483 port->attach = attach;
2484 port->next = free_usb_ports;
2485 free_usb_ports = port;
2486}
2487
aliguori4b096fc2008-08-21 19:28:55 +00002488int usb_device_add_dev(USBDevice *dev)
2489{
2490 USBPort *port;
2491
2492 /* Find a USB port to add the device to. */
2493 port = free_usb_ports;
2494 if (!port->next) {
2495 USBDevice *hub;
2496
2497 /* Create a new hub and chain it on. */
2498 free_usb_ports = NULL;
2499 port->next = used_usb_ports;
2500 used_usb_ports = port;
2501
2502 hub = usb_hub_init(VM_USB_HUB_SIZE);
2503 usb_attach(port, hub);
2504 port = free_usb_ports;
2505 }
2506
2507 free_usb_ports = port->next;
2508 port->next = used_usb_ports;
2509 used_usb_ports = port;
2510 usb_attach(port, dev);
2511 return 0;
2512}
2513
aliguoribb5fc202009-03-05 23:01:15 +00002514static void usb_msd_password_cb(void *opaque, int err)
2515{
2516 USBDevice *dev = opaque;
2517
2518 if (!err)
2519 usb_device_add_dev(dev);
2520 else
2521 dev->handle_destroy(dev);
2522}
2523
aliguoric0f4ce72009-03-05 23:01:01 +00002524static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002525{
2526 const char *p;
2527 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002528
pbrook0d92ed32006-05-21 16:30:15 +00002529 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002530 return -1;
2531
2532 if (strstart(devname, "host:", &p)) {
2533 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002534 } else if (!strcmp(devname, "mouse")) {
2535 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002536 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002537 dev = usb_tablet_init();
2538 } else if (!strcmp(devname, "keyboard")) {
2539 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002540 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002541 BlockDriverState *bs;
2542
aliguoribb5fc202009-03-05 23:01:15 +00002543 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002544 if (!dev)
2545 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002546 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002547 if (bdrv_key_required(bs)) {
2548 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002549 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002550 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2551 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002552 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002553 }
2554 }
balrogf6d2a312007-06-10 19:21:04 +00002555 } else if (!strcmp(devname, "wacom-tablet")) {
2556 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002557 } else if (strstart(devname, "serial:", &p)) {
2558 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002559#ifdef CONFIG_BRLAPI
2560 } else if (!strcmp(devname, "braille")) {
2561 dev = usb_baum_init();
2562#endif
balrog6c9f8862008-07-17 20:47:13 +00002563 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002564 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002565
Jan Kiszka10ae5a72009-05-08 12:34:18 +02002566 if (net_client_init(NULL, "nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002567 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002568 nd_table[nic].model = "usb";
2569 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002570 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2571 dev = usb_bt_init(devname[2] ? hci_init(p) :
2572 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002573 } else {
2574 return -1;
2575 }
pbrook0d92ed32006-05-21 16:30:15 +00002576 if (!dev)
2577 return -1;
2578
aliguori4b096fc2008-08-21 19:28:55 +00002579 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002580}
2581
aliguori1f3870a2008-08-21 19:27:48 +00002582int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002583{
pbrook0d92ed32006-05-21 16:30:15 +00002584 USBPort *port;
2585 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002586 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002587
pbrook0d92ed32006-05-21 16:30:15 +00002588 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002589 return -1;
2590
bellarda594cfb2005-11-06 16:13:29 +00002591 if (bus_num != 0)
2592 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002593
2594 lastp = &used_usb_ports;
2595 port = used_usb_ports;
2596 while (port && port->dev->addr != addr) {
2597 lastp = &port->next;
2598 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002599 }
pbrook0d92ed32006-05-21 16:30:15 +00002600
2601 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002602 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002603
bellard059809e2006-07-19 18:06:15 +00002604 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002605 *lastp = port->next;
2606 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002607 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002608 port->next = free_usb_ports;
2609 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002610 return 0;
2611}
2612
aliguori1f3870a2008-08-21 19:27:48 +00002613static int usb_device_del(const char *devname)
2614{
2615 int bus_num, addr;
2616 const char *p;
2617
aliguori5d0c5752008-09-14 01:07:41 +00002618 if (strstart(devname, "host:", &p))
2619 return usb_host_device_close(p);
2620
aliguori1f3870a2008-08-21 19:27:48 +00002621 if (!used_usb_ports)
2622 return -1;
2623
2624 p = strchr(devname, '.');
2625 if (!p)
2626 return -1;
2627 bus_num = strtoul(devname, NULL, 0);
2628 addr = strtoul(p + 1, NULL, 0);
2629
2630 return usb_device_del_addr(bus_num, addr);
2631}
2632
aliguori376253e2009-03-05 23:01:23 +00002633void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002634{
aliguoric0f4ce72009-03-05 23:01:01 +00002635 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002636}
2637
aliguori376253e2009-03-05 23:01:23 +00002638void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002639{
aliguori4b096fc2008-08-21 19:28:55 +00002640 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002641}
2642
aliguori376253e2009-03-05 23:01:23 +00002643void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002644{
2645 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002646 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002647 const char *speed_str;
2648
pbrook0d92ed32006-05-21 16:30:15 +00002649 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002650 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002651 return;
2652 }
2653
pbrook0d92ed32006-05-21 16:30:15 +00002654 for (port = used_usb_ports; port; port = port->next) {
2655 dev = port->dev;
2656 if (!dev)
2657 continue;
2658 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002659 case USB_SPEED_LOW:
2660 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002661 break;
ths5fafdf22007-09-16 21:08:06 +00002662 case USB_SPEED_FULL:
2663 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002664 break;
ths5fafdf22007-09-16 21:08:06 +00002665 case USB_SPEED_HIGH:
2666 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002667 break;
2668 default:
ths5fafdf22007-09-16 21:08:06 +00002669 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002670 break;
bellarda594cfb2005-11-06 16:13:29 +00002671 }
aliguori376253e2009-03-05 23:01:23 +00002672 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2673 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002674 }
2675}
2676
bellardf7cce892004-12-08 22:21:25 +00002677/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002678/* PCMCIA/Cardbus */
2679
2680static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002681 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002682 struct pcmcia_socket_entry_s *next;
2683} *pcmcia_sockets = 0;
2684
Paul Brookbc24a222009-05-10 01:44:56 +01002685void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002686{
2687 struct pcmcia_socket_entry_s *entry;
2688
2689 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2690 entry->socket = socket;
2691 entry->next = pcmcia_sockets;
2692 pcmcia_sockets = entry;
2693}
2694
Paul Brookbc24a222009-05-10 01:44:56 +01002695void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002696{
2697 struct pcmcia_socket_entry_s *entry, **ptr;
2698
2699 ptr = &pcmcia_sockets;
2700 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2701 if (entry->socket == socket) {
2702 *ptr = entry->next;
2703 qemu_free(entry);
2704 }
2705}
2706
aliguori376253e2009-03-05 23:01:23 +00002707void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002708{
2709 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002710
balrog201a51f2007-04-30 00:51:09 +00002711 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002712 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002713
2714 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002715 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2716 iter->socket->attached ? iter->socket->card_string :
2717 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002718}
2719
2720/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002721/* register display */
2722
aliguori7b5d76d2009-03-13 15:02:13 +00002723struct DisplayAllocator default_allocator = {
2724 defaultallocator_create_displaysurface,
2725 defaultallocator_resize_displaysurface,
2726 defaultallocator_free_displaysurface
2727};
2728
aliguori3023f332009-01-16 19:04:14 +00002729void register_displaystate(DisplayState *ds)
2730{
2731 DisplayState **s;
2732 s = &display_state;
2733 while (*s != NULL)
2734 s = &(*s)->next;
2735 ds->next = NULL;
2736 *s = ds;
2737}
2738
2739DisplayState *get_displaystate(void)
2740{
2741 return display_state;
2742}
2743
aliguori7b5d76d2009-03-13 15:02:13 +00002744DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2745{
2746 if(ds->allocator == &default_allocator) ds->allocator = da;
2747 return ds->allocator;
2748}
2749
ths2ff89792007-06-21 23:34:19 +00002750/* dumb display */
2751
aliguori8f391ab2009-01-19 16:34:10 +00002752static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002753{
aliguori8f391ab2009-01-19 16:34:10 +00002754 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002755 ds->allocator = &default_allocator;
2756 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002757 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002758}
2759
2760/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002761/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002762
bellardc4b1fcc2004-03-14 21:44:30 +00002763typedef struct IOHandlerRecord {
2764 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002765 IOCanRWHandler *fd_read_poll;
2766 IOHandler *fd_read;
2767 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002768 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002769 void *opaque;
2770 /* temporary data */
2771 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002772 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002773} IOHandlerRecord;
2774
bellard8a7ddc32004-03-31 19:00:16 +00002775static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002776
bellard7c9d8e02005-11-15 22:16:05 +00002777/* XXX: fd_read_poll should be suppressed, but an API change is
2778 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002779int qemu_set_fd_handler2(int fd,
2780 IOCanRWHandler *fd_read_poll,
2781 IOHandler *fd_read,
2782 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002783 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002784{
bellard8a7ddc32004-03-31 19:00:16 +00002785 IOHandlerRecord **pioh, *ioh;
2786
bellard7c9d8e02005-11-15 22:16:05 +00002787 if (!fd_read && !fd_write) {
2788 pioh = &first_io_handler;
2789 for(;;) {
2790 ioh = *pioh;
2791 if (ioh == NULL)
2792 break;
2793 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002794 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002795 break;
2796 }
2797 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002798 }
bellard7c9d8e02005-11-15 22:16:05 +00002799 } else {
2800 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2801 if (ioh->fd == fd)
2802 goto found;
2803 }
2804 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002805 ioh->next = first_io_handler;
2806 first_io_handler = ioh;
2807 found:
2808 ioh->fd = fd;
2809 ioh->fd_read_poll = fd_read_poll;
2810 ioh->fd_read = fd_read;
2811 ioh->fd_write = fd_write;
2812 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002813 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002814 }
bellard7c9d8e02005-11-15 22:16:05 +00002815 return 0;
2816}
2817
ths5fafdf22007-09-16 21:08:06 +00002818int qemu_set_fd_handler(int fd,
2819 IOHandler *fd_read,
2820 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002821 void *opaque)
2822{
2823 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002824}
2825
aliguori56f3a5d2008-10-31 18:07:17 +00002826#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002827/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002828/* Polling handling */
2829
2830typedef struct PollingEntry {
2831 PollingFunc *func;
2832 void *opaque;
2833 struct PollingEntry *next;
2834} PollingEntry;
2835
2836static PollingEntry *first_polling_entry;
2837
2838int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2839{
2840 PollingEntry **ppe, *pe;
2841 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002842 pe->func = func;
2843 pe->opaque = opaque;
2844 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2845 *ppe = pe;
2846 return 0;
2847}
2848
2849void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2850{
2851 PollingEntry **ppe, *pe;
2852 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2853 pe = *ppe;
2854 if (pe->func == func && pe->opaque == opaque) {
2855 *ppe = pe->next;
2856 qemu_free(pe);
2857 break;
2858 }
2859 }
2860}
2861
bellarda18e5242006-06-25 17:18:27 +00002862/***********************************************************/
2863/* Wait objects support */
2864typedef struct WaitObjects {
2865 int num;
2866 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2867 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2868 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2869} WaitObjects;
2870
2871static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002872
bellarda18e5242006-06-25 17:18:27 +00002873int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2874{
2875 WaitObjects *w = &wait_objects;
2876
2877 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2878 return -1;
2879 w->events[w->num] = handle;
2880 w->func[w->num] = func;
2881 w->opaque[w->num] = opaque;
2882 w->num++;
2883 return 0;
2884}
2885
2886void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2887{
2888 int i, found;
2889 WaitObjects *w = &wait_objects;
2890
2891 found = 0;
2892 for (i = 0; i < w->num; i++) {
2893 if (w->events[i] == handle)
2894 found = 1;
2895 if (found) {
2896 w->events[i] = w->events[i + 1];
2897 w->func[i] = w->func[i + 1];
2898 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002899 }
bellarda18e5242006-06-25 17:18:27 +00002900 }
2901 if (found)
2902 w->num--;
2903}
2904#endif
2905
bellard8a7ddc32004-03-31 19:00:16 +00002906/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002907/* ram save/restore */
2908
bellard8a7ddc32004-03-31 19:00:16 +00002909static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2910{
2911 int v;
2912
2913 v = qemu_get_byte(f);
2914 switch(v) {
2915 case 0:
2916 if (qemu_get_buffer(f, buf, len) != len)
2917 return -EIO;
2918 break;
2919 case 1:
2920 v = qemu_get_byte(f);
2921 memset(buf, v, len);
2922 break;
2923 default:
2924 return -EINVAL;
2925 }
aliguori871d2f02008-10-13 03:07:56 +00002926
2927 if (qemu_file_has_error(f))
2928 return -EIO;
2929
bellard8a7ddc32004-03-31 19:00:16 +00002930 return 0;
2931}
2932
bellardc88676f2006-08-06 13:36:11 +00002933static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00002934{
aurel3200f82b82008-04-27 21:12:55 +00002935 int ret;
2936 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00002937
pbrook94a6b542009-04-11 17:15:54 +00002938 if (qemu_get_be32(f) != last_ram_offset)
bellard8a7ddc32004-03-31 19:00:16 +00002939 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00002940 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
pbrook5579c7f2009-04-11 14:47:08 +00002941 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
bellard8a7ddc32004-03-31 19:00:16 +00002942 if (ret)
2943 return ret;
2944 }
2945 return 0;
2946}
2947
bellardc88676f2006-08-06 13:36:11 +00002948#define BDRV_HASH_BLOCK_SIZE 1024
2949#define IOBUF_SIZE 4096
2950#define RAM_CBLOCK_MAGIC 0xfabe
2951
bellardc88676f2006-08-06 13:36:11 +00002952typedef struct RamDecompressState {
2953 z_stream zstream;
2954 QEMUFile *f;
2955 uint8_t buf[IOBUF_SIZE];
2956} RamDecompressState;
2957
2958static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
2959{
2960 int ret;
2961 memset(s, 0, sizeof(*s));
2962 s->f = f;
2963 ret = inflateInit(&s->zstream);
2964 if (ret != Z_OK)
2965 return -1;
2966 return 0;
2967}
2968
2969static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
2970{
2971 int ret, clen;
2972
2973 s->zstream.avail_out = len;
2974 s->zstream.next_out = buf;
2975 while (s->zstream.avail_out > 0) {
2976 if (s->zstream.avail_in == 0) {
2977 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
2978 return -1;
2979 clen = qemu_get_be16(s->f);
2980 if (clen > IOBUF_SIZE)
2981 return -1;
2982 qemu_get_buffer(s->f, s->buf, clen);
2983 s->zstream.avail_in = clen;
2984 s->zstream.next_in = s->buf;
2985 }
2986 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
2987 if (ret != Z_OK && ret != Z_STREAM_END) {
2988 return -1;
2989 }
2990 }
2991 return 0;
2992}
2993
2994static void ram_decompress_close(RamDecompressState *s)
2995{
2996 inflateEnd(&s->zstream);
2997}
2998
aliguori475e4272008-10-06 20:21:51 +00002999#define RAM_SAVE_FLAG_FULL 0x01
3000#define RAM_SAVE_FLAG_COMPRESS 0x02
3001#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3002#define RAM_SAVE_FLAG_PAGE 0x08
3003#define RAM_SAVE_FLAG_EOS 0x10
3004
3005static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003006{
aliguori475e4272008-10-06 20:21:51 +00003007 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3008 uint32_t *array = (uint32_t *)page;
3009 int i;
ths3b46e622007-09-17 08:09:54 +00003010
aliguori475e4272008-10-06 20:21:51 +00003011 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3012 if (array[i] != val)
3013 return 0;
bellardc88676f2006-08-06 13:36:11 +00003014 }
aliguori475e4272008-10-06 20:21:51 +00003015
3016 return 1;
bellardc88676f2006-08-06 13:36:11 +00003017}
3018
aliguori475e4272008-10-06 20:21:51 +00003019static int ram_save_block(QEMUFile *f)
3020{
3021 static ram_addr_t current_addr = 0;
3022 ram_addr_t saved_addr = current_addr;
3023 ram_addr_t addr = 0;
3024 int found = 0;
3025
pbrook94a6b542009-04-11 17:15:54 +00003026 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00003027 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00003028 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00003029
3030 cpu_physical_memory_reset_dirty(current_addr,
3031 current_addr + TARGET_PAGE_SIZE,
3032 MIGRATION_DIRTY_FLAG);
3033
pbrook5579c7f2009-04-11 14:47:08 +00003034 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00003035
pbrook5579c7f2009-04-11 14:47:08 +00003036 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00003037 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00003038 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00003039 } else {
3040 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00003041 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003042 }
3043
3044 found = 1;
3045 break;
3046 }
3047 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00003048 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00003049 }
3050
3051 return found;
3052}
3053
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003054static uint64_t bytes_transferred = 0;
aliguori475e4272008-10-06 20:21:51 +00003055
3056static ram_addr_t ram_save_remaining(void)
3057{
3058 ram_addr_t addr;
3059 ram_addr_t count = 0;
3060
pbrook94a6b542009-04-11 17:15:54 +00003061 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003062 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3063 count++;
3064 }
3065
3066 return count;
3067}
3068
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003069uint64_t ram_bytes_remaining(void)
3070{
3071 return ram_save_remaining() * TARGET_PAGE_SIZE;
3072}
3073
3074uint64_t ram_bytes_transferred(void)
3075{
3076 return bytes_transferred;
3077}
3078
3079uint64_t ram_bytes_total(void)
3080{
3081 return last_ram_offset;
3082}
3083
aliguori475e4272008-10-06 20:21:51 +00003084static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3085{
3086 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003087 uint64_t bytes_transferred_last;
3088 double bwidth = 0;
3089 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00003090
Jan Kiszka9fa06382009-05-22 23:51:45 +02003091 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003092 qemu_file_set_error(f);
3093 return 0;
3094 }
3095
aliguori475e4272008-10-06 20:21:51 +00003096 if (stage == 1) {
3097 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003098 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003099 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3100 cpu_physical_memory_set_dirty(addr);
3101 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003102
aliguori475e4272008-10-06 20:21:51 +00003103 /* Enable dirty memory tracking */
3104 cpu_physical_memory_set_dirty_tracking(1);
3105
pbrook94a6b542009-04-11 17:15:54 +00003106 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003107 }
3108
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003109 bytes_transferred_last = bytes_transferred;
3110 bwidth = get_clock();
3111
aliguori475e4272008-10-06 20:21:51 +00003112 while (!qemu_file_rate_limit(f)) {
3113 int ret;
3114
3115 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003116 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003117 if (ret == 0) /* no more blocks */
3118 break;
3119 }
3120
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003121 bwidth = get_clock() - bwidth;
3122 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3123
3124 /* if we haven't transferred anything this round, force expected_time to a
3125 * a very high value, but without crashing */
3126 if (bwidth == 0)
3127 bwidth = 0.000001;
3128
aliguori475e4272008-10-06 20:21:51 +00003129 /* try transferring iterative blocks of memory */
3130
3131 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003132
3133 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003134 while (ram_save_block(f) != 0) {
3135 bytes_transferred += TARGET_PAGE_SIZE;
3136 }
aliguori8215e912009-04-05 19:30:55 +00003137 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003138 }
3139
3140 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3141
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003142 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3143
3144 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003145}
3146
3147static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003148{
3149 RamDecompressState s1, *s = &s1;
3150 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003151 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003152
bellardc88676f2006-08-06 13:36:11 +00003153 if (ram_decompress_open(s, f) < 0)
3154 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003155 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
bellardc88676f2006-08-06 13:36:11 +00003156 if (ram_decompress_buf(s, buf, 1) < 0) {
3157 fprintf(stderr, "Error while reading ram block header\n");
3158 goto error;
3159 }
3160 if (buf[0] == 0) {
pbrook5579c7f2009-04-11 14:47:08 +00003161 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3162 BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003163 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003164 goto error;
3165 }
aliguori475e4272008-10-06 20:21:51 +00003166 } else {
bellardc88676f2006-08-06 13:36:11 +00003167 error:
3168 printf("Error block header\n");
3169 return -EINVAL;
3170 }
3171 }
3172 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003173
3174 return 0;
3175}
3176
3177static int ram_load(QEMUFile *f, void *opaque, int version_id)
3178{
3179 ram_addr_t addr;
3180 int flags;
3181
3182 if (version_id == 1)
3183 return ram_load_v1(f, opaque);
3184
3185 if (version_id == 2) {
pbrook94a6b542009-04-11 17:15:54 +00003186 if (qemu_get_be32(f) != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003187 return -EINVAL;
3188 return ram_load_dead(f, opaque);
3189 }
3190
3191 if (version_id != 3)
3192 return -EINVAL;
3193
3194 do {
3195 addr = qemu_get_be64(f);
3196
3197 flags = addr & ~TARGET_PAGE_MASK;
3198 addr &= TARGET_PAGE_MASK;
3199
3200 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003201 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003202 return -EINVAL;
3203 }
3204
3205 if (flags & RAM_SAVE_FLAG_FULL) {
3206 if (ram_load_dead(f, opaque) < 0)
3207 return -EINVAL;
3208 }
3209
3210 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3211 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003212 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3213#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003214 if (ch == 0 &&
3215 (!kvm_enabled() || kvm_has_sync_mmu())) {
3216 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003217 }
Anthony Liguori30868442009-06-17 16:46:12 -05003218#endif
aliguori475e4272008-10-06 20:21:51 +00003219 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003220 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003221 } while (!(flags & RAM_SAVE_FLAG_EOS));
3222
bellardc88676f2006-08-06 13:36:11 +00003223 return 0;
3224}
3225
aliguori9e472e12008-10-08 19:50:24 +00003226void qemu_service_io(void)
3227{
aliguorid9f75a42009-04-24 18:03:11 +00003228 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003229}
3230
bellard8a7ddc32004-03-31 19:00:16 +00003231/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003232/* bottom halves (can be seen as timers which expire ASAP) */
3233
3234struct QEMUBH {
3235 QEMUBHFunc *cb;
3236 void *opaque;
3237 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003238 int idle;
3239 int deleted;
bellard83f64092006-08-01 16:21:11 +00003240 QEMUBH *next;
3241};
3242
3243static QEMUBH *first_bh = NULL;
3244
3245QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3246{
3247 QEMUBH *bh;
3248 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003249 bh->cb = cb;
3250 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003251 bh->next = first_bh;
3252 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003253 return bh;
3254}
3255
bellard6eb57332006-08-06 09:51:25 +00003256int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003257{
aliguori1b435b12008-10-31 17:24:21 +00003258 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003259 int ret;
bellard83f64092006-08-01 16:21:11 +00003260
bellard6eb57332006-08-06 09:51:25 +00003261 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003262 for (bh = first_bh; bh; bh = bh->next) {
3263 if (!bh->deleted && bh->scheduled) {
3264 bh->scheduled = 0;
3265 if (!bh->idle)
3266 ret = 1;
3267 bh->idle = 0;
3268 bh->cb(bh->opaque);
3269 }
bellard83f64092006-08-01 16:21:11 +00003270 }
aliguori1b435b12008-10-31 17:24:21 +00003271
3272 /* remove deleted bhs */
3273 bhp = &first_bh;
3274 while (*bhp) {
3275 bh = *bhp;
3276 if (bh->deleted) {
3277 *bhp = bh->next;
3278 qemu_free(bh);
3279 } else
3280 bhp = &bh->next;
3281 }
3282
bellard6eb57332006-08-06 09:51:25 +00003283 return ret;
bellard83f64092006-08-01 16:21:11 +00003284}
3285
aliguori1b435b12008-10-31 17:24:21 +00003286void qemu_bh_schedule_idle(QEMUBH *bh)
3287{
3288 if (bh->scheduled)
3289 return;
3290 bh->scheduled = 1;
3291 bh->idle = 1;
3292}
3293
bellard83f64092006-08-01 16:21:11 +00003294void qemu_bh_schedule(QEMUBH *bh)
3295{
bellard83f64092006-08-01 16:21:11 +00003296 if (bh->scheduled)
3297 return;
3298 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003299 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003300 /* stop the currently executing CPU to execute the BH ASAP */
aliguorid9f75a42009-04-24 18:03:11 +00003301 qemu_notify_event();
bellard83f64092006-08-01 16:21:11 +00003302}
3303
3304void qemu_bh_cancel(QEMUBH *bh)
3305{
aliguori1b435b12008-10-31 17:24:21 +00003306 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003307}
3308
3309void qemu_bh_delete(QEMUBH *bh)
3310{
aliguori1b435b12008-10-31 17:24:21 +00003311 bh->scheduled = 0;
3312 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003313}
3314
aliguori56f3a5d2008-10-31 18:07:17 +00003315static void qemu_bh_update_timeout(int *timeout)
3316{
3317 QEMUBH *bh;
3318
3319 for (bh = first_bh; bh; bh = bh->next) {
3320 if (!bh->deleted && bh->scheduled) {
3321 if (bh->idle) {
3322 /* idle bottom halves will be polled at least
3323 * every 10ms */
3324 *timeout = MIN(10, *timeout);
3325 } else {
3326 /* non-idle bottom halves will be executed
3327 * immediately */
3328 *timeout = 0;
3329 break;
3330 }
3331 }
3332 }
3333}
3334
bellard83f64092006-08-01 16:21:11 +00003335/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003336/* machine registration */
3337
blueswir1bdaf78e2008-10-04 07:24:27 +00003338static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003339QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003340
3341int qemu_register_machine(QEMUMachine *m)
3342{
3343 QEMUMachine **pm;
3344 pm = &first_machine;
3345 while (*pm != NULL)
3346 pm = &(*pm)->next;
3347 m->next = NULL;
3348 *pm = m;
3349 return 0;
3350}
3351
pbrook9596ebb2007-11-18 01:44:38 +00003352static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003353{
3354 QEMUMachine *m;
3355
3356 for(m = first_machine; m != NULL; m = m->next) {
3357 if (!strcmp(m->name, name))
3358 return m;
3359 }
3360 return NULL;
3361}
3362
Anthony Liguori0c257432009-05-21 20:41:01 -05003363static QEMUMachine *find_default_machine(void)
3364{
3365 QEMUMachine *m;
3366
3367 for(m = first_machine; m != NULL; m = m->next) {
3368 if (m->is_default) {
3369 return m;
3370 }
3371 }
3372 return NULL;
3373}
3374
bellardcc1daa42005-06-05 14:49:17 +00003375/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003376/* main execution loop */
3377
pbrook9596ebb2007-11-18 01:44:38 +00003378static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003379{
aliguori7d957bd2009-01-15 22:14:11 +00003380 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003381 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003382 DisplayChangeListener *dcl = ds->listeners;
3383
3384 dpy_refresh(ds);
3385
3386 while (dcl != NULL) {
3387 if (dcl->gui_timer_interval &&
3388 dcl->gui_timer_interval < interval)
3389 interval = dcl->gui_timer_interval;
3390 dcl = dcl->next;
3391 }
3392 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003393}
3394
blueswir19043b622009-01-21 19:28:13 +00003395static void nographic_update(void *opaque)
3396{
3397 uint64_t interval = GUI_REFRESH_INTERVAL;
3398
3399 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3400}
3401
bellard0bd48852005-11-11 00:00:47 +00003402struct vm_change_state_entry {
3403 VMChangeStateHandler *cb;
3404 void *opaque;
3405 LIST_ENTRY (vm_change_state_entry) entries;
3406};
3407
3408static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3409
3410VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3411 void *opaque)
3412{
3413 VMChangeStateEntry *e;
3414
3415 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003416
3417 e->cb = cb;
3418 e->opaque = opaque;
3419 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3420 return e;
3421}
3422
3423void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3424{
3425 LIST_REMOVE (e, entries);
3426 qemu_free (e);
3427}
3428
aliguori9781e042009-01-22 17:15:29 +00003429static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003430{
3431 VMChangeStateEntry *e;
3432
3433 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003434 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003435 }
3436}
3437
aliguorid6dc3d42009-04-24 18:04:07 +00003438static void resume_all_vcpus(void);
3439static void pause_all_vcpus(void);
3440
bellard8a7ddc32004-03-31 19:00:16 +00003441void vm_start(void)
3442{
3443 if (!vm_running) {
3444 cpu_enable_ticks();
3445 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003446 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003447 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003448 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003449 }
3450}
3451
bellardbb0c6722004-06-20 12:37:32 +00003452/* reset/shutdown handler */
3453
3454typedef struct QEMUResetEntry {
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003455 TAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003456 QEMUResetHandler *func;
3457 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003458} QEMUResetEntry;
3459
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003460static TAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3461 TAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003462static int reset_requested;
3463static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003464static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003465static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003466static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003467
aurel32cf7a2fe2008-03-18 06:53:05 +00003468int qemu_shutdown_requested(void)
3469{
3470 int r = shutdown_requested;
3471 shutdown_requested = 0;
3472 return r;
3473}
3474
3475int qemu_reset_requested(void)
3476{
3477 int r = reset_requested;
3478 reset_requested = 0;
3479 return r;
3480}
3481
3482int qemu_powerdown_requested(void)
3483{
3484 int r = powerdown_requested;
3485 powerdown_requested = 0;
3486 return r;
3487}
3488
aliguorie5689022009-04-24 18:03:54 +00003489static int qemu_debug_requested(void)
3490{
3491 int r = debug_requested;
3492 debug_requested = 0;
3493 return r;
3494}
3495
aliguori6e29f5d2009-04-24 18:04:02 +00003496static int qemu_vmstop_requested(void)
3497{
3498 int r = vmstop_requested;
3499 vmstop_requested = 0;
3500 return r;
3501}
3502
3503static void do_vm_stop(int reason)
3504{
3505 if (vm_running) {
3506 cpu_disable_ticks();
3507 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003508 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003509 vm_state_notify(0, reason);
3510 }
3511}
3512
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003513void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003514{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003515 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003516
bellardbb0c6722004-06-20 12:37:32 +00003517 re->func = func;
3518 re->opaque = opaque;
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003519 TAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003520}
3521
Jan Kiszkadda9b292009-07-02 00:19:02 +02003522void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003523{
3524 QEMUResetEntry *re;
3525
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003526 TAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003527 if (re->func == func && re->opaque == opaque) {
3528 TAILQ_REMOVE(&reset_handlers, re, entry);
3529 qemu_free(re);
3530 return;
3531 }
3532 }
3533}
3534
3535void qemu_system_reset(void)
3536{
3537 QEMUResetEntry *re, *nre;
3538
3539 /* reset all devices */
3540 TAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003541 re->func(re->opaque);
3542 }
3543}
3544
3545void qemu_system_reset_request(void)
3546{
bellardd1beab82006-10-02 19:44:22 +00003547 if (no_reboot) {
3548 shutdown_requested = 1;
3549 } else {
3550 reset_requested = 1;
3551 }
aliguorid9f75a42009-04-24 18:03:11 +00003552 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003553}
3554
3555void qemu_system_shutdown_request(void)
3556{
3557 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003558 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003559}
3560
bellard34751872005-07-02 14:31:34 +00003561void qemu_system_powerdown_request(void)
3562{
3563 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003564 qemu_notify_event();
3565}
3566
aliguorid6dc3d42009-04-24 18:04:07 +00003567#ifdef CONFIG_IOTHREAD
3568static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003569{
aliguorid6dc3d42009-04-24 18:04:07 +00003570 vmstop_requested = reason;
3571 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003572}
aliguorid6dc3d42009-04-24 18:04:07 +00003573#endif
bellardbb0c6722004-06-20 12:37:32 +00003574
aliguori50317c72009-04-24 18:03:29 +00003575#ifndef _WIN32
3576static int io_thread_fd = -1;
3577
3578static void qemu_event_increment(void)
3579{
3580 static const char byte = 0;
3581
3582 if (io_thread_fd == -1)
3583 return;
3584
3585 write(io_thread_fd, &byte, sizeof(byte));
3586}
3587
3588static void qemu_event_read(void *opaque)
3589{
3590 int fd = (unsigned long)opaque;
3591 ssize_t len;
3592
3593 /* Drain the notify pipe */
3594 do {
3595 char buffer[512];
3596 len = read(fd, buffer, sizeof(buffer));
3597 } while ((len == -1 && errno == EINTR) || len > 0);
3598}
3599
3600static int qemu_event_init(void)
3601{
3602 int err;
3603 int fds[2];
3604
3605 err = pipe(fds);
3606 if (err == -1)
3607 return -errno;
3608
3609 err = fcntl_setfl(fds[0], O_NONBLOCK);
3610 if (err < 0)
3611 goto fail;
3612
3613 err = fcntl_setfl(fds[1], O_NONBLOCK);
3614 if (err < 0)
3615 goto fail;
3616
3617 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3618 (void *)(unsigned long)fds[0]);
3619
3620 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003621 return 0;
3622
aliguori50317c72009-04-24 18:03:29 +00003623fail:
3624 close(fds[0]);
3625 close(fds[1]);
3626 return err;
3627}
3628#else
3629HANDLE qemu_event_handle;
3630
3631static void dummy_event_handler(void *opaque)
3632{
3633}
3634
3635static int qemu_event_init(void)
3636{
3637 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3638 if (!qemu_event_handle) {
3639 perror("Failed CreateEvent");
3640 return -1;
3641 }
3642 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3643 return 0;
3644}
3645
3646static void qemu_event_increment(void)
3647{
3648 SetEvent(qemu_event_handle);
3649}
3650#endif
3651
aliguorid6dc3d42009-04-24 18:04:07 +00003652static int cpu_can_run(CPUState *env)
3653{
3654 if (env->stop)
3655 return 0;
3656 if (env->stopped)
3657 return 0;
3658 return 1;
3659}
3660
3661#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003662static int qemu_init_main_loop(void)
3663{
aliguori50317c72009-04-24 18:03:29 +00003664 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003665}
3666
aliguori0bf46a42009-04-24 18:03:41 +00003667void qemu_init_vcpu(void *_env)
3668{
3669 CPUState *env = _env;
3670
3671 if (kvm_enabled())
3672 kvm_init_vcpu(env);
3673 return;
3674}
3675
aliguori8edac962009-04-24 18:03:45 +00003676int qemu_cpu_self(void *env)
3677{
3678 return 1;
3679}
3680
aliguorid6dc3d42009-04-24 18:04:07 +00003681static void resume_all_vcpus(void)
3682{
3683}
3684
3685static void pause_all_vcpus(void)
3686{
3687}
3688
aliguori8edac962009-04-24 18:03:45 +00003689void qemu_cpu_kick(void *env)
3690{
3691 return;
3692}
3693
aliguorid6dc3d42009-04-24 18:04:07 +00003694void qemu_notify_event(void)
3695{
3696 CPUState *env = cpu_single_env;
3697
3698 if (env) {
3699 cpu_exit(env);
3700#ifdef USE_KQEMU
3701 if (env->kqemu_enabled)
3702 kqemu_cpu_interrupt(env);
3703#endif
3704 }
3705}
3706
aliguori48708522009-04-24 18:03:49 +00003707#define qemu_mutex_lock_iothread() do { } while (0)
3708#define qemu_mutex_unlock_iothread() do { } while (0)
3709
aliguori6e29f5d2009-04-24 18:04:02 +00003710void vm_stop(int reason)
3711{
3712 do_vm_stop(reason);
3713}
3714
aliguorid6dc3d42009-04-24 18:04:07 +00003715#else /* CONFIG_IOTHREAD */
3716
3717#include "qemu-thread.h"
3718
3719QemuMutex qemu_global_mutex;
3720static QemuMutex qemu_fair_mutex;
3721
3722static QemuThread io_thread;
3723
3724static QemuThread *tcg_cpu_thread;
3725static QemuCond *tcg_halt_cond;
3726
3727static int qemu_system_ready;
3728/* cpu creation */
3729static QemuCond qemu_cpu_cond;
3730/* system init */
3731static QemuCond qemu_system_cond;
3732static QemuCond qemu_pause_cond;
3733
3734static void block_io_signals(void);
3735static void unblock_io_signals(void);
3736static int tcg_has_work(void);
3737
3738static int qemu_init_main_loop(void)
3739{
3740 int ret;
3741
3742 ret = qemu_event_init();
3743 if (ret)
3744 return ret;
3745
3746 qemu_cond_init(&qemu_pause_cond);
3747 qemu_mutex_init(&qemu_fair_mutex);
3748 qemu_mutex_init(&qemu_global_mutex);
3749 qemu_mutex_lock(&qemu_global_mutex);
3750
3751 unblock_io_signals();
3752 qemu_thread_self(&io_thread);
3753
3754 return 0;
3755}
3756
3757static void qemu_wait_io_event(CPUState *env)
3758{
3759 while (!tcg_has_work())
3760 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3761
3762 qemu_mutex_unlock(&qemu_global_mutex);
3763
3764 /*
3765 * Users of qemu_global_mutex can be starved, having no chance
3766 * to acquire it since this path will get to it first.
3767 * So use another lock to provide fairness.
3768 */
3769 qemu_mutex_lock(&qemu_fair_mutex);
3770 qemu_mutex_unlock(&qemu_fair_mutex);
3771
3772 qemu_mutex_lock(&qemu_global_mutex);
3773 if (env->stop) {
3774 env->stop = 0;
3775 env->stopped = 1;
3776 qemu_cond_signal(&qemu_pause_cond);
3777 }
3778}
3779
3780static int qemu_cpu_exec(CPUState *env);
3781
3782static void *kvm_cpu_thread_fn(void *arg)
3783{
3784 CPUState *env = arg;
3785
3786 block_io_signals();
3787 qemu_thread_self(env->thread);
3788
3789 /* signal CPU creation */
3790 qemu_mutex_lock(&qemu_global_mutex);
3791 env->created = 1;
3792 qemu_cond_signal(&qemu_cpu_cond);
3793
3794 /* and wait for machine initialization */
3795 while (!qemu_system_ready)
3796 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3797
3798 while (1) {
3799 if (cpu_can_run(env))
3800 qemu_cpu_exec(env);
3801 qemu_wait_io_event(env);
3802 }
3803
3804 return NULL;
3805}
3806
3807static void tcg_cpu_exec(void);
3808
3809static void *tcg_cpu_thread_fn(void *arg)
3810{
3811 CPUState *env = arg;
3812
3813 block_io_signals();
3814 qemu_thread_self(env->thread);
3815
3816 /* signal CPU creation */
3817 qemu_mutex_lock(&qemu_global_mutex);
3818 for (env = first_cpu; env != NULL; env = env->next_cpu)
3819 env->created = 1;
3820 qemu_cond_signal(&qemu_cpu_cond);
3821
3822 /* and wait for machine initialization */
3823 while (!qemu_system_ready)
3824 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3825
3826 while (1) {
3827 tcg_cpu_exec();
3828 qemu_wait_io_event(cur_cpu);
3829 }
3830
3831 return NULL;
3832}
3833
3834void qemu_cpu_kick(void *_env)
3835{
3836 CPUState *env = _env;
3837 qemu_cond_broadcast(env->halt_cond);
3838 if (kvm_enabled())
3839 qemu_thread_signal(env->thread, SIGUSR1);
3840}
3841
3842int qemu_cpu_self(void *env)
3843{
3844 return (cpu_single_env != NULL);
3845}
3846
3847static void cpu_signal(int sig)
3848{
3849 if (cpu_single_env)
3850 cpu_exit(cpu_single_env);
3851}
3852
3853static void block_io_signals(void)
3854{
3855 sigset_t set;
3856 struct sigaction sigact;
3857
3858 sigemptyset(&set);
3859 sigaddset(&set, SIGUSR2);
3860 sigaddset(&set, SIGIO);
3861 sigaddset(&set, SIGALRM);
3862 pthread_sigmask(SIG_BLOCK, &set, NULL);
3863
3864 sigemptyset(&set);
3865 sigaddset(&set, SIGUSR1);
3866 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3867
3868 memset(&sigact, 0, sizeof(sigact));
3869 sigact.sa_handler = cpu_signal;
3870 sigaction(SIGUSR1, &sigact, NULL);
3871}
3872
3873static void unblock_io_signals(void)
3874{
3875 sigset_t set;
3876
3877 sigemptyset(&set);
3878 sigaddset(&set, SIGUSR2);
3879 sigaddset(&set, SIGIO);
3880 sigaddset(&set, SIGALRM);
3881 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3882
3883 sigemptyset(&set);
3884 sigaddset(&set, SIGUSR1);
3885 pthread_sigmask(SIG_BLOCK, &set, NULL);
3886}
3887
3888static void qemu_signal_lock(unsigned int msecs)
3889{
3890 qemu_mutex_lock(&qemu_fair_mutex);
3891
3892 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3893 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3894 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3895 break;
3896 }
3897 qemu_mutex_unlock(&qemu_fair_mutex);
3898}
3899
3900static void qemu_mutex_lock_iothread(void)
3901{
3902 if (kvm_enabled()) {
3903 qemu_mutex_lock(&qemu_fair_mutex);
3904 qemu_mutex_lock(&qemu_global_mutex);
3905 qemu_mutex_unlock(&qemu_fair_mutex);
3906 } else
3907 qemu_signal_lock(100);
3908}
3909
3910static void qemu_mutex_unlock_iothread(void)
3911{
3912 qemu_mutex_unlock(&qemu_global_mutex);
3913}
3914
3915static int all_vcpus_paused(void)
3916{
3917 CPUState *penv = first_cpu;
3918
3919 while (penv) {
3920 if (!penv->stopped)
3921 return 0;
3922 penv = (CPUState *)penv->next_cpu;
3923 }
3924
3925 return 1;
3926}
3927
3928static void pause_all_vcpus(void)
3929{
3930 CPUState *penv = first_cpu;
3931
3932 while (penv) {
3933 penv->stop = 1;
3934 qemu_thread_signal(penv->thread, SIGUSR1);
3935 qemu_cpu_kick(penv);
3936 penv = (CPUState *)penv->next_cpu;
3937 }
3938
3939 while (!all_vcpus_paused()) {
3940 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3941 penv = first_cpu;
3942 while (penv) {
3943 qemu_thread_signal(penv->thread, SIGUSR1);
3944 penv = (CPUState *)penv->next_cpu;
3945 }
3946 }
3947}
3948
3949static void resume_all_vcpus(void)
3950{
3951 CPUState *penv = first_cpu;
3952
3953 while (penv) {
3954 penv->stop = 0;
3955 penv->stopped = 0;
3956 qemu_thread_signal(penv->thread, SIGUSR1);
3957 qemu_cpu_kick(penv);
3958 penv = (CPUState *)penv->next_cpu;
3959 }
3960}
3961
3962static void tcg_init_vcpu(void *_env)
3963{
3964 CPUState *env = _env;
3965 /* share a single thread for all cpus with TCG */
3966 if (!tcg_cpu_thread) {
3967 env->thread = qemu_mallocz(sizeof(QemuThread));
3968 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3969 qemu_cond_init(env->halt_cond);
3970 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3971 while (env->created == 0)
3972 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3973 tcg_cpu_thread = env->thread;
3974 tcg_halt_cond = env->halt_cond;
3975 } else {
3976 env->thread = tcg_cpu_thread;
3977 env->halt_cond = tcg_halt_cond;
3978 }
3979}
3980
3981static void kvm_start_vcpu(CPUState *env)
3982{
3983 kvm_init_vcpu(env);
3984 env->thread = qemu_mallocz(sizeof(QemuThread));
3985 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3986 qemu_cond_init(env->halt_cond);
3987 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3988 while (env->created == 0)
3989 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3990}
3991
3992void qemu_init_vcpu(void *_env)
3993{
3994 CPUState *env = _env;
3995
3996 if (kvm_enabled())
3997 kvm_start_vcpu(env);
3998 else
3999 tcg_init_vcpu(env);
4000}
4001
4002void qemu_notify_event(void)
4003{
4004 qemu_event_increment();
4005}
4006
4007void vm_stop(int reason)
4008{
4009 QemuThread me;
4010 qemu_thread_self(&me);
4011
4012 if (!qemu_thread_equal(&me, &io_thread)) {
4013 qemu_system_vmstop_request(reason);
4014 /*
4015 * FIXME: should not return to device code in case
4016 * vm_stop() has been requested.
4017 */
4018 if (cpu_single_env) {
4019 cpu_exit(cpu_single_env);
4020 cpu_single_env->stop = 1;
4021 }
4022 return;
4023 }
4024 do_vm_stop(reason);
4025}
4026
4027#endif
4028
4029
ths877cf882007-04-18 18:11:47 +00004030#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00004031static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00004032{
4033 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00004034 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00004035
bellardf3311102006-04-12 20:21:17 +00004036
4037 /* XXX: need to suppress polling by better using win32 events */
4038 ret = 0;
4039 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4040 ret |= pe->func(pe->opaque);
4041 }
thse6b1e552007-04-18 17:56:02 +00004042 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00004043 int err;
4044 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00004045
aliguori56f3a5d2008-10-31 18:07:17 +00004046 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00004047 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4048 if (w->func[ret - WAIT_OBJECT_0])
4049 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00004050
ths5fafdf22007-09-16 21:08:06 +00004051 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00004052 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00004053
thse6b1e552007-04-18 17:56:02 +00004054 /* Check if event is signaled */
4055 ret2 = WaitForSingleObject(w->events[i], 0);
4056 if(ret2 == WAIT_OBJECT_0) {
4057 if (w->func[i])
4058 w->func[i](w->opaque[i]);
4059 } else if (ret2 == WAIT_TIMEOUT) {
4060 } else {
4061 err = GetLastError();
4062 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00004063 }
4064 }
bellarda18e5242006-06-25 17:18:27 +00004065 } else if (ret == WAIT_TIMEOUT) {
4066 } else {
4067 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00004068 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00004069 }
bellardf3311102006-04-12 20:21:17 +00004070 }
aliguori56f3a5d2008-10-31 18:07:17 +00004071
4072 *timeout = 0;
4073}
4074#else
blueswir169d64512008-12-07 19:30:18 +00004075static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00004076{
4077}
bellardfd1dff42006-02-01 21:29:26 +00004078#endif
aliguori56f3a5d2008-10-31 18:07:17 +00004079
4080void main_loop_wait(int timeout)
4081{
4082 IOHandlerRecord *ioh;
4083 fd_set rfds, wfds, xfds;
4084 int ret, nfds;
4085 struct timeval tv;
4086
4087 qemu_bh_update_timeout(&timeout);
4088
4089 host_main_loop_wait(&timeout);
4090
bellardfd1dff42006-02-01 21:29:26 +00004091 /* poll any events */
4092 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00004093 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00004094 FD_ZERO(&rfds);
4095 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00004096 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00004097 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00004098 if (ioh->deleted)
4099 continue;
bellardfd1dff42006-02-01 21:29:26 +00004100 if (ioh->fd_read &&
4101 (!ioh->fd_read_poll ||
4102 ioh->fd_read_poll(ioh->opaque) != 0)) {
4103 FD_SET(ioh->fd, &rfds);
4104 if (ioh->fd > nfds)
4105 nfds = ioh->fd;
4106 }
4107 if (ioh->fd_write) {
4108 FD_SET(ioh->fd, &wfds);
4109 if (ioh->fd > nfds)
4110 nfds = ioh->fd;
4111 }
4112 }
ths3b46e622007-09-17 08:09:54 +00004113
aliguori56f3a5d2008-10-31 18:07:17 +00004114 tv.tv_sec = timeout / 1000;
4115 tv.tv_usec = (timeout % 1000) * 1000;
4116
Jan Kiszkad918f232009-06-24 14:42:30 +02004117 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4118
aliguori48708522009-04-24 18:03:49 +00004119 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00004120 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00004121 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00004122 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00004123 IOHandlerRecord **pioh;
4124
4125 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00004126 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004127 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00004128 }
ths6ab43fd2007-08-25 01:34:19 +00004129 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004130 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00004131 }
4132 }
thscafffd42007-02-28 21:59:44 +00004133
4134 /* remove deleted IO handlers */
4135 pioh = &first_io_handler;
4136 while (*pioh) {
4137 ioh = *pioh;
4138 if (ioh->deleted) {
4139 *pioh = ioh->next;
4140 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00004141 } else
thscafffd42007-02-28 21:59:44 +00004142 pioh = &ioh->next;
4143 }
bellardfd1dff42006-02-01 21:29:26 +00004144 }
Jan Kiszkad918f232009-06-24 14:42:30 +02004145
4146 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00004147
aliguori50317c72009-04-24 18:03:29 +00004148 /* rearm timer, if not periodic */
4149 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4150 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4151 qemu_rearm_alarm_timer(alarm_timer);
4152 }
4153
aliguori357c6922008-11-25 17:26:09 +00004154 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00004155 if (vm_running) {
4156 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
4157 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
4158 qemu_get_clock(vm_clock));
4159 }
aliguori357c6922008-11-25 17:26:09 +00004160
4161 /* real time timers */
4162 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
4163 qemu_get_clock(rt_clock));
4164
pbrook423f0742007-05-23 00:06:54 +00004165 /* Check bottom-halves last in case any of the earlier events triggered
4166 them. */
4167 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00004168
bellard5905b2e2004-08-01 21:53:26 +00004169}
4170
aliguori43b96852009-04-24 18:03:33 +00004171static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00004172{
aliguori43b96852009-04-24 18:03:33 +00004173 int ret;
bellard89bfc102006-02-08 22:46:31 +00004174#ifdef CONFIG_PROFILER
4175 int64_t ti;
4176#endif
aliguori43b96852009-04-24 18:03:33 +00004177
4178#ifdef CONFIG_PROFILER
4179 ti = profile_getclock();
4180#endif
4181 if (use_icount) {
4182 int64_t count;
4183 int decr;
4184 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4185 env->icount_decr.u16.low = 0;
4186 env->icount_extra = 0;
4187 count = qemu_next_deadline();
4188 count = (count + (1 << icount_time_shift) - 1)
4189 >> icount_time_shift;
4190 qemu_icount += count;
4191 decr = (count > 0xffff) ? 0xffff : count;
4192 count -= decr;
4193 env->icount_decr.u16.low = decr;
4194 env->icount_extra = count;
4195 }
4196 ret = cpu_exec(env);
4197#ifdef CONFIG_PROFILER
4198 qemu_time += profile_getclock() - ti;
4199#endif
4200 if (use_icount) {
4201 /* Fold pending instructions back into the
4202 instruction counter, and clear the interrupt flag. */
4203 qemu_icount -= (env->icount_decr.u16.low
4204 + env->icount_extra);
4205 env->icount_decr.u32 = 0;
4206 env->icount_extra = 0;
4207 }
4208 return ret;
4209}
4210
aliguorie6e35b12009-04-24 18:03:57 +00004211static void tcg_cpu_exec(void)
4212{
aliguorid6dc3d42009-04-24 18:04:07 +00004213 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00004214
4215 if (next_cpu == NULL)
4216 next_cpu = first_cpu;
4217 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4218 CPUState *env = cur_cpu = next_cpu;
4219
4220 if (!vm_running)
4221 break;
4222 if (timer_alarm_pending) {
4223 timer_alarm_pending = 0;
4224 break;
4225 }
aliguorid6dc3d42009-04-24 18:04:07 +00004226 if (cpu_can_run(env))
4227 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004228 if (ret == EXCP_DEBUG) {
4229 gdb_set_stop_cpu(env);
4230 debug_requested = 1;
4231 break;
4232 }
4233 }
4234}
4235
aliguori43b96852009-04-24 18:03:33 +00004236static int cpu_has_work(CPUState *env)
4237{
aliguorid6dc3d42009-04-24 18:04:07 +00004238 if (env->stop)
4239 return 1;
4240 if (env->stopped)
4241 return 0;
aliguori43b96852009-04-24 18:03:33 +00004242 if (!env->halted)
4243 return 1;
4244 if (qemu_cpu_has_work(env))
4245 return 1;
4246 return 0;
4247}
4248
4249static int tcg_has_work(void)
4250{
bellard6a00d602005-11-21 23:25:50 +00004251 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004252
aliguori43b96852009-04-24 18:03:33 +00004253 for (env = first_cpu; env != NULL; env = env->next_cpu)
4254 if (cpu_has_work(env))
4255 return 1;
4256 return 0;
4257}
bellard15a76442005-11-23 21:01:03 +00004258
aliguori43b96852009-04-24 18:03:33 +00004259static int qemu_calculate_timeout(void)
4260{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004261#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004262 int timeout;
bellard15a76442005-11-23 21:01:03 +00004263
aliguori43b96852009-04-24 18:03:33 +00004264 if (!vm_running)
4265 timeout = 5000;
4266 else if (tcg_has_work())
4267 timeout = 0;
4268 else if (!use_icount)
4269 timeout = 5000;
4270 else {
4271 /* XXX: use timeout computed from timers */
4272 int64_t add;
4273 int64_t delta;
4274 /* Advance virtual time to the next event. */
4275 if (use_icount == 1) {
4276 /* When not using an adaptive execution frequency
4277 we tend to get badly out of sync with real time,
4278 so just delay for a reasonable amount of time. */
4279 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004280 } else {
aliguori43b96852009-04-24 18:03:33 +00004281 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004282 }
aliguori43b96852009-04-24 18:03:33 +00004283 if (delta > 0) {
4284 /* If virtual time is ahead of real time then just
4285 wait for IO. */
4286 timeout = (delta / 1000000) + 1;
4287 } else {
4288 /* Wait for either IO to occur or the next
4289 timer event. */
4290 add = qemu_next_deadline();
4291 /* We advance the timer before checking for IO.
4292 Limit the amount we advance so that early IO
4293 activity won't get the guest too far ahead. */
4294 if (add > 10000000)
4295 add = 10000000;
4296 delta += add;
4297 add = (add + (1 << icount_time_shift) - 1)
4298 >> icount_time_shift;
4299 qemu_icount += add;
4300 timeout = delta / 1000000;
4301 if (timeout < 0)
4302 timeout = 0;
4303 }
bellardb4608c02003-06-27 17:34:32 +00004304 }
aliguori43b96852009-04-24 18:03:33 +00004305
4306 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004307#else /* CONFIG_IOTHREAD */
4308 return 1000;
4309#endif
aliguori43b96852009-04-24 18:03:33 +00004310}
4311
4312static int vm_can_run(void)
4313{
4314 if (powerdown_requested)
4315 return 0;
4316 if (reset_requested)
4317 return 0;
4318 if (shutdown_requested)
4319 return 0;
aliguorie5689022009-04-24 18:03:54 +00004320 if (debug_requested)
4321 return 0;
aliguori43b96852009-04-24 18:03:33 +00004322 return 1;
4323}
4324
4325static void main_loop(void)
4326{
aliguori6e29f5d2009-04-24 18:04:02 +00004327 int r;
aliguori43b96852009-04-24 18:03:33 +00004328
aliguorid6dc3d42009-04-24 18:04:07 +00004329#ifdef CONFIG_IOTHREAD
4330 qemu_system_ready = 1;
4331 qemu_cond_broadcast(&qemu_system_cond);
4332#endif
4333
aliguori6e29f5d2009-04-24 18:04:02 +00004334 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004335 do {
4336#ifdef CONFIG_PROFILER
4337 int64_t ti;
4338#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004339#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004340 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004341#endif
aliguori43b96852009-04-24 18:03:33 +00004342#ifdef CONFIG_PROFILER
4343 ti = profile_getclock();
4344#endif
4345 main_loop_wait(qemu_calculate_timeout());
4346#ifdef CONFIG_PROFILER
4347 dev_time += profile_getclock() - ti;
4348#endif
aliguorie5689022009-04-24 18:03:54 +00004349 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004350
aliguorie5689022009-04-24 18:03:54 +00004351 if (qemu_debug_requested())
aliguori43b96852009-04-24 18:03:33 +00004352 vm_stop(EXCP_DEBUG);
aliguori43b96852009-04-24 18:03:33 +00004353 if (qemu_shutdown_requested()) {
4354 if (no_shutdown) {
4355 vm_stop(0);
4356 no_shutdown = 0;
4357 } else
4358 break;
4359 }
aliguorid6dc3d42009-04-24 18:04:07 +00004360 if (qemu_reset_requested()) {
4361 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004362 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004363 resume_all_vcpus();
4364 }
aliguori43b96852009-04-24 18:03:33 +00004365 if (qemu_powerdown_requested())
4366 qemu_system_powerdown();
aliguori6e29f5d2009-04-24 18:04:02 +00004367 if ((r = qemu_vmstop_requested()))
4368 vm_stop(r);
aliguori43b96852009-04-24 18:03:33 +00004369 }
aliguorid6dc3d42009-04-24 18:04:07 +00004370 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004371}
4372
pbrook9bd7e6d2009-04-07 22:58:45 +00004373static void version(void)
4374{
pbrook4a19f1e2009-04-07 23:17:49 +00004375 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004376}
4377
ths15f82202007-06-29 23:26:08 +00004378static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004379{
pbrook9bd7e6d2009-04-07 22:58:45 +00004380 version();
4381 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004382 "\n"
bellarda20dd502003-09-30 21:07:02 +00004383 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004384 "\n"
blueswir15824d652009-03-28 06:44:27 +00004385#define DEF(option, opt_arg, opt_enum, opt_help) \
4386 opt_help
4387#define DEFHEADING(text) stringify(text) "\n"
4388#include "qemu-options.h"
4389#undef DEF
4390#undef DEFHEADING
4391#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004392 "\n"
bellard82c643f2004-07-14 17:28:13 +00004393 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004394 "ctrl-alt-f toggle full screen\n"
4395 "ctrl-alt-n switch to virtual console 'n'\n"
4396 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004397 "\n"
4398 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4399 ,
bellard0db63472003-10-27 21:37:46 +00004400 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004401 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004402#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004403 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004404 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004405#endif
bellard6e44ba72004-01-18 21:56:49 +00004406 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004407 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004408 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004409}
4410
bellardcd6f1162004-05-13 22:02:20 +00004411#define HAS_ARG 0x0001
4412
4413enum {
blueswir15824d652009-03-28 06:44:27 +00004414#define DEF(option, opt_arg, opt_enum, opt_help) \
4415 opt_enum,
4416#define DEFHEADING(text)
4417#include "qemu-options.h"
4418#undef DEF
4419#undef DEFHEADING
4420#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004421};
4422
4423typedef struct QEMUOption {
4424 const char *name;
4425 int flags;
4426 int index;
4427} QEMUOption;
4428
blueswir1dbed7e42008-10-01 19:38:09 +00004429static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004430 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004431#define DEF(option, opt_arg, opt_enum, opt_help) \
4432 { option, opt_arg, opt_enum },
4433#define DEFHEADING(text)
4434#include "qemu-options.h"
4435#undef DEF
4436#undef DEFHEADING
4437#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004438 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004439};
4440
bellard1d14ffa2005-10-30 18:58:22 +00004441#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004442struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004443#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004444#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004445 {
4446 "pcspk",
4447 "PC speaker",
4448 0,
4449 1,
4450 { .init_isa = pcspk_audio_init }
4451 },
4452#endif
malc4c9b53e2009-01-09 10:46:34 +00004453
4454#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004455 {
4456 "sb16",
4457 "Creative Sound Blaster 16",
4458 0,
4459 1,
4460 { .init_isa = SB16_init }
4461 },
malc4c9b53e2009-01-09 10:46:34 +00004462#endif
bellard6a36d842005-12-18 20:34:32 +00004463
malccc53d262008-06-13 10:48:22 +00004464#ifdef CONFIG_CS4231A
4465 {
4466 "cs4231a",
4467 "CS4231A",
4468 0,
4469 1,
4470 { .init_isa = cs4231a_init }
4471 },
4472#endif
4473
bellard6a36d842005-12-18 20:34:32 +00004474#ifdef CONFIG_ADLIB
4475 {
4476 "adlib",
4477#ifdef HAS_YMF262
4478 "Yamaha YMF262 (OPL3)",
4479#else
4480 "Yamaha YM3812 (OPL2)",
4481#endif
4482 0,
4483 1,
4484 { .init_isa = Adlib_init }
4485 },
4486#endif
4487
4488#ifdef CONFIG_GUS
4489 {
4490 "gus",
4491 "Gravis Ultrasound GF1",
4492 0,
4493 1,
4494 { .init_isa = GUS_init }
4495 },
4496#endif
4497
malc4c9b53e2009-01-09 10:46:34 +00004498#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004499 {
4500 "ac97",
4501 "Intel 82801AA AC97 Audio",
4502 0,
4503 0,
4504 { .init_pci = ac97_init }
4505 },
malc4c9b53e2009-01-09 10:46:34 +00004506#endif
balroge5c9a132008-01-14 04:27:55 +00004507
malc4c9b53e2009-01-09 10:46:34 +00004508#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004509 {
4510 "es1370",
4511 "ENSONIQ AudioPCI ES1370",
4512 0,
4513 0,
4514 { .init_pci = es1370_init }
4515 },
balrogb00052e2007-04-30 02:22:06 +00004516#endif
bellard6a36d842005-12-18 20:34:32 +00004517
malc4c9b53e2009-01-09 10:46:34 +00004518#endif /* HAS_AUDIO_CHOICE */
4519
bellard6a36d842005-12-18 20:34:32 +00004520 { NULL, NULL, 0, 0, { NULL } }
4521};
4522
bellard1d14ffa2005-10-30 18:58:22 +00004523static void select_soundhw (const char *optarg)
4524{
bellard6a36d842005-12-18 20:34:32 +00004525 struct soundhw *c;
4526
bellard1d14ffa2005-10-30 18:58:22 +00004527 if (*optarg == '?') {
4528 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004529
bellard1d14ffa2005-10-30 18:58:22 +00004530 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004531 for (c = soundhw; c->name; ++c) {
4532 printf ("%-11s %s\n", c->name, c->descr);
4533 }
4534 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004535 exit (*optarg != '?');
4536 }
4537 else {
bellard6a36d842005-12-18 20:34:32 +00004538 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004539 const char *p;
4540 char *e;
4541 int bad_card = 0;
4542
bellard6a36d842005-12-18 20:34:32 +00004543 if (!strcmp (optarg, "all")) {
4544 for (c = soundhw; c->name; ++c) {
4545 c->enabled = 1;
4546 }
4547 return;
4548 }
bellard1d14ffa2005-10-30 18:58:22 +00004549
bellard6a36d842005-12-18 20:34:32 +00004550 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004551 while (*p) {
4552 e = strchr (p, ',');
4553 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004554
4555 for (c = soundhw; c->name; ++c) {
4556 if (!strncmp (c->name, p, l)) {
4557 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004558 break;
4559 }
4560 }
bellard6a36d842005-12-18 20:34:32 +00004561
4562 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004563 if (l > 80) {
4564 fprintf (stderr,
4565 "Unknown sound card name (too big to show)\n");
4566 }
4567 else {
4568 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4569 (int) l, p);
4570 }
4571 bad_card = 1;
4572 }
4573 p += l + (e != NULL);
4574 }
4575
4576 if (bad_card)
4577 goto show_valid_cards;
4578 }
4579}
4580#endif
4581
malc3893c122008-09-28 00:42:05 +00004582static void select_vgahw (const char *p)
4583{
4584 const char *opts;
4585
aliguori28b85ed2009-04-22 15:19:48 +00004586 cirrus_vga_enabled = 0;
4587 std_vga_enabled = 0;
4588 vmsvga_enabled = 0;
aliguori94909d92009-04-22 15:19:53 +00004589 xenfb_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004590 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004591 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004592 } else if (strstart(p, "cirrus", &opts)) {
4593 cirrus_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004594 } else if (strstart(p, "vmware", &opts)) {
malc3893c122008-09-28 00:42:05 +00004595 vmsvga_enabled = 1;
aliguori94909d92009-04-22 15:19:53 +00004596 } else if (strstart(p, "xenfb", &opts)) {
4597 xenfb_enabled = 1;
aliguori28b85ed2009-04-22 15:19:48 +00004598 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004599 invalid_vga:
4600 fprintf(stderr, "Unknown vga type: %s\n", p);
4601 exit(1);
4602 }
malccb5a7aa2008-09-28 00:42:12 +00004603 while (*opts) {
4604 const char *nextopt;
4605
4606 if (strstart(opts, ",retrace=", &nextopt)) {
4607 opts = nextopt;
4608 if (strstart(opts, "dumb", &nextopt))
4609 vga_retrace_method = VGA_RETRACE_DUMB;
4610 else if (strstart(opts, "precise", &nextopt))
4611 vga_retrace_method = VGA_RETRACE_PRECISE;
4612 else goto invalid_vga;
4613 } else goto invalid_vga;
4614 opts = nextopt;
4615 }
malc3893c122008-09-28 00:42:05 +00004616}
4617
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004618#ifdef TARGET_I386
4619static int balloon_parse(const char *arg)
4620{
4621 char buf[128];
4622 const char *p;
4623
4624 if (!strcmp(arg, "none")) {
4625 virtio_balloon = 0;
4626 } else if (!strncmp(arg, "virtio", 6)) {
4627 virtio_balloon = 1;
4628 if (arg[6] == ',') {
4629 p = arg + 7;
4630 if (get_param_value(buf, sizeof(buf), "addr", p)) {
4631 virtio_balloon_devaddr = strdup(buf);
4632 }
4633 }
4634 } else {
4635 return -1;
4636 }
4637 return 0;
4638}
4639#endif
4640
bellard3587d7e2006-06-26 20:03:44 +00004641#ifdef _WIN32
4642static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4643{
4644 exit(STATUS_CONTROL_C_EXIT);
4645 return TRUE;
4646}
4647#endif
4648
aliguoric4be29f2009-04-17 18:58:14 +00004649int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004650{
4651 int ret;
4652
4653 if(strlen(str) != 36)
4654 return -1;
4655
4656 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4657 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4658 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4659
4660 if(ret != 16)
4661 return -1;
4662
aliguorib6f6e3d2009-04-17 18:59:56 +00004663#ifdef TARGET_I386
4664 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4665#endif
4666
blueswir18fcb1b92008-09-18 18:29:08 +00004667 return 0;
4668}
4669
bellard7c9d8e02005-11-15 22:16:05 +00004670#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004671
aliguori5b08fc12008-08-21 20:08:03 +00004672#ifndef _WIN32
4673
4674static void termsig_handler(int signal)
4675{
4676 qemu_system_shutdown_request();
4677}
4678
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004679static void sigchld_handler(int signal)
4680{
4681 waitpid(-1, NULL, WNOHANG);
4682}
4683
4684static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004685{
4686 struct sigaction act;
4687
4688 memset(&act, 0, sizeof(act));
4689 act.sa_handler = termsig_handler;
4690 sigaction(SIGINT, &act, NULL);
4691 sigaction(SIGHUP, &act, NULL);
4692 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004693
4694 act.sa_handler = sigchld_handler;
4695 act.sa_flags = SA_NOCLDSTOP;
4696 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004697}
4698
4699#endif
4700
Paul Brook5cea8592009-05-30 00:52:44 +01004701#ifdef _WIN32
4702/* Look for support files in the same directory as the executable. */
4703static char *find_datadir(const char *argv0)
4704{
4705 char *p;
4706 char buf[MAX_PATH];
4707 DWORD len;
4708
4709 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4710 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004711 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004712 }
4713
4714 buf[len] = 0;
4715 p = buf + len - 1;
4716 while (p != buf && *p != '\\')
4717 p--;
4718 *p = 0;
4719 if (access(buf, R_OK) == 0) {
4720 return qemu_strdup(buf);
4721 }
4722 return NULL;
4723}
4724#else /* !_WIN32 */
4725
4726/* Find a likely location for support files using the location of the binary.
4727 For installed binaries this will be "$bindir/../share/qemu". When
4728 running from the build tree this will be "$bindir/../pc-bios". */
4729#define SHARE_SUFFIX "/share/qemu"
4730#define BUILD_SUFFIX "/pc-bios"
4731static char *find_datadir(const char *argv0)
4732{
4733 char *dir;
4734 char *p = NULL;
4735 char *res;
4736#ifdef PATH_MAX
4737 char buf[PATH_MAX];
4738#endif
Blue Swirl3a417592009-06-09 19:12:21 +00004739 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004740
4741#if defined(__linux__)
4742 {
4743 int len;
4744 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4745 if (len > 0) {
4746 buf[len] = 0;
4747 p = buf;
4748 }
4749 }
4750#elif defined(__FreeBSD__)
4751 {
4752 int len;
4753 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4754 if (len > 0) {
4755 buf[len] = 0;
4756 p = buf;
4757 }
4758 }
4759#endif
4760 /* If we don't have any way of figuring out the actual executable
4761 location then try argv[0]. */
4762 if (!p) {
4763#ifdef PATH_MAX
4764 p = buf;
4765#endif
4766 p = realpath(argv0, p);
4767 if (!p) {
4768 return NULL;
4769 }
4770 }
4771 dir = dirname(p);
4772 dir = dirname(dir);
4773
Blue Swirl3a417592009-06-09 19:12:21 +00004774 max_len = strlen(dir) +
4775 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4776 res = qemu_mallocz(max_len);
4777 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004778 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004779 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004780 if (access(res, R_OK)) {
4781 qemu_free(res);
4782 res = NULL;
4783 }
4784 }
4785#ifndef PATH_MAX
4786 free(p);
4787#endif
4788 return res;
4789}
4790#undef SHARE_SUFFIX
4791#undef BUILD_SUFFIX
4792#endif
4793
4794char *qemu_find_file(int type, const char *name)
4795{
4796 int len;
4797 const char *subdir;
4798 char *buf;
4799
4800 /* If name contains path separators then try it as a straight path. */
4801 if ((strchr(name, '/') || strchr(name, '\\'))
4802 && access(name, R_OK) == 0) {
4803 return strdup(name);
4804 }
4805 switch (type) {
4806 case QEMU_FILE_TYPE_BIOS:
4807 subdir = "";
4808 break;
4809 case QEMU_FILE_TYPE_KEYMAP:
4810 subdir = "keymaps/";
4811 break;
4812 default:
4813 abort();
4814 }
4815 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4816 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004817 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004818 if (access(buf, R_OK)) {
4819 qemu_free(buf);
4820 return NULL;
4821 }
4822 return buf;
4823}
4824
malc902b3d52008-12-10 19:18:40 +00004825int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004826{
aliguori59030a82009-04-05 18:43:41 +00004827 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004828 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004829 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004830 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004831 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004832 const char *kernel_filename, *kernel_cmdline;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004833 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004834 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004835 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004836 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004837 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004838 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004839 const char *bt_opts[MAX_BT_CMDLINE];
4840 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004841 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004842 int optind;
4843 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004844 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004845 const char *monitor_device;
4846 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004847 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004848 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004849 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004850 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4851 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004852 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004853 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004854 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004855 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004856 int usb_devices_index;
blueswir1b9e82a52009-04-05 18:03:31 +00004857#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004858 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004859#endif
bellard26a5f132008-05-28 12:30:31 +00004860 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004861 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004862 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004863#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004864 int fd = 0;
4865 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004866 const char *chroot_dir = NULL;
4867 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004868#endif
aliguori268a3622009-04-21 22:30:27 +00004869 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004870 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004871
malc902b3d52008-12-10 19:18:40 +00004872 qemu_cache_utils_init(envp);
4873
bellard0bd48852005-11-11 00:00:47 +00004874 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004875#ifndef _WIN32
4876 {
4877 struct sigaction act;
4878 sigfillset(&act.sa_mask);
4879 act.sa_flags = 0;
4880 act.sa_handler = SIG_IGN;
4881 sigaction(SIGPIPE, &act, NULL);
4882 }
bellard3587d7e2006-06-26 20:03:44 +00004883#else
4884 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004885 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4886 QEMU to run on a single CPU */
4887 {
4888 HANDLE h;
4889 DWORD mask, smask;
4890 int i;
4891 h = GetCurrentProcess();
4892 if (GetProcessAffinityMask(h, &mask, &smask)) {
4893 for(i = 0; i < 32; i++) {
4894 if (mask & (1 << i))
4895 break;
4896 }
4897 if (i != 32) {
4898 mask = 1 << i;
4899 SetProcessAffinityMask(h, mask);
4900 }
4901 }
4902 }
bellard67b915a2004-03-31 23:37:16 +00004903#endif
bellardbe995c22006-06-25 16:25:21 +00004904
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004905 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004906 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004907 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004908 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004909 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004910 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004911 kernel_filename = NULL;
4912 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004913 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004914 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004915 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004916
aurel32c75a8232008-05-04 00:50:34 +00004917 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004918 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004919 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004920 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004921
aliguori8290edd2009-02-27 20:14:29 +00004922 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004923 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004924 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004925 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004926
aliguori1b8fc812009-02-27 20:01:39 +00004927 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004928 virtio_consoles[i] = NULL;
4929 virtio_console_index = 0;
4930
aliguori268a3622009-04-21 22:30:27 +00004931 for (i = 0; i < MAX_NODES; i++) {
4932 node_mem[i] = 0;
4933 node_cpumask[i] = 0;
4934 }
4935
bellarda594cfb2005-11-06 16:13:29 +00004936 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004937
bellard7c9d8e02005-11-15 22:16:05 +00004938 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004939 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004940 nb_drives = 0;
4941 nb_drives_opt = 0;
aliguori268a3622009-04-21 22:30:27 +00004942 nb_numa_nodes = 0;
thse4bcb142007-12-02 04:51:10 +00004943 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004944
4945 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004946
bellard26a5f132008-05-28 12:30:31 +00004947 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004948 autostart= 1;
4949
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01004950 register_watchdogs();
4951
bellardcd6f1162004-05-13 22:02:20 +00004952 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004953 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004954 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004955 break;
bellardcd6f1162004-05-13 22:02:20 +00004956 r = argv[optind];
4957 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004958 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004959 } else {
4960 const QEMUOption *popt;
4961
4962 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004963 /* Treat --foo the same as -foo. */
4964 if (r[1] == '-')
4965 r++;
bellardcd6f1162004-05-13 22:02:20 +00004966 popt = qemu_options;
4967 for(;;) {
4968 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004969 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004970 argv[0], r);
4971 exit(1);
4972 }
4973 if (!strcmp(popt->name, r + 1))
4974 break;
4975 popt++;
4976 }
4977 if (popt->flags & HAS_ARG) {
4978 if (optind >= argc) {
4979 fprintf(stderr, "%s: option '%s' requires an argument\n",
4980 argv[0], r);
4981 exit(1);
4982 }
4983 optarg = argv[optind++];
4984 } else {
4985 optarg = NULL;
4986 }
4987
4988 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004989 case QEMU_OPTION_M:
4990 machine = find_machine(optarg);
4991 if (!machine) {
4992 QEMUMachine *m;
4993 printf("Supported machines are:\n");
4994 for(m = first_machine; m != NULL; m = m->next) {
4995 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004996 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004997 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004998 }
ths15f82202007-06-29 23:26:08 +00004999 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00005000 }
5001 break;
j_mayer94fc95c2007-03-05 19:44:02 +00005002 case QEMU_OPTION_cpu:
5003 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00005004 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00005005/* XXX: implement xxx_cpu_list for targets that still miss it */
5006#if defined(cpu_list)
5007 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00005008#endif
ths15f82202007-06-29 23:26:08 +00005009 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00005010 } else {
5011 cpu_model = optarg;
5012 }
5013 break;
bellardcd6f1162004-05-13 22:02:20 +00005014 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00005015 initrd_filename = optarg;
5016 break;
bellardcd6f1162004-05-13 22:02:20 +00005017 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00005018 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00005019 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00005020 else
balrog609497a2008-01-14 02:56:53 +00005021 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00005022 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00005023 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005024 translation == BIOS_ATA_TRANSLATION_LBA ?
5025 ",trans=lba" :
5026 translation == BIOS_ATA_TRANSLATION_NONE ?
5027 ",trans=none" : "");
5028 break;
bellardcd6f1162004-05-13 22:02:20 +00005029 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00005030 case QEMU_OPTION_hdc:
5031 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00005032 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00005033 break;
thse4bcb142007-12-02 04:51:10 +00005034 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00005035 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00005036 break;
balrog3e3d5812007-04-30 02:09:25 +00005037 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00005038 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00005039 break;
pbrooka1bb27b2007-04-06 16:49:48 +00005040 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00005041 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00005042 break;
j_mayer86f55662007-04-24 06:52:59 +00005043 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00005044 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00005045 break;
bellardcd6f1162004-05-13 22:02:20 +00005046 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00005047 snapshot = 1;
5048 break;
bellardcd6f1162004-05-13 22:02:20 +00005049 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00005050 {
bellard330d0412003-07-26 18:11:40 +00005051 const char *p;
5052 p = optarg;
5053 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005054 if (cyls < 1 || cyls > 16383)
5055 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005056 if (*p != ',')
5057 goto chs_fail;
5058 p++;
5059 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005060 if (heads < 1 || heads > 16)
5061 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005062 if (*p != ',')
5063 goto chs_fail;
5064 p++;
5065 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005066 if (secs < 1 || secs > 63)
5067 goto chs_fail;
5068 if (*p == ',') {
5069 p++;
5070 if (!strcmp(p, "none"))
5071 translation = BIOS_ATA_TRANSLATION_NONE;
5072 else if (!strcmp(p, "lba"))
5073 translation = BIOS_ATA_TRANSLATION_LBA;
5074 else if (!strcmp(p, "auto"))
5075 translation = BIOS_ATA_TRANSLATION_AUTO;
5076 else
5077 goto chs_fail;
5078 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00005079 chs_fail:
bellard46d47672004-11-16 01:45:27 +00005080 fprintf(stderr, "qemu: invalid physical CHS format\n");
5081 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00005082 }
thse4bcb142007-12-02 04:51:10 +00005083 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00005084 snprintf(drives_opt[hda_index].opt,
5085 sizeof(drives_opt[hda_index].opt),
5086 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
5087 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005088 translation == BIOS_ATA_TRANSLATION_LBA ?
5089 ",trans=lba" :
5090 translation == BIOS_ATA_TRANSLATION_NONE ?
5091 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00005092 }
5093 break;
aliguori268a3622009-04-21 22:30:27 +00005094 case QEMU_OPTION_numa:
5095 if (nb_numa_nodes >= MAX_NODES) {
5096 fprintf(stderr, "qemu: too many NUMA nodes\n");
5097 exit(1);
5098 }
5099 numa_add(optarg);
5100 break;
bellardcd6f1162004-05-13 22:02:20 +00005101 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005102 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005103 break;
balrog4d3b6f62008-02-10 16:33:14 +00005104#ifdef CONFIG_CURSES
5105 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005106 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005107 break;
5108#endif
balroga171fe32007-04-30 01:48:07 +00005109 case QEMU_OPTION_portrait:
5110 graphic_rotate = 1;
5111 break;
bellardcd6f1162004-05-13 22:02:20 +00005112 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005113 kernel_filename = optarg;
5114 break;
bellardcd6f1162004-05-13 22:02:20 +00005115 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005116 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005117 break;
bellardcd6f1162004-05-13 22:02:20 +00005118 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005119 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005120 break;
bellardcd6f1162004-05-13 22:02:20 +00005121 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005122 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005123 static const char * const params[] = {
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005124 "order", "once", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005125 };
5126 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005127 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005128 int legacy = 0;
5129
5130 if (!strchr(optarg, '=')) {
5131 legacy = 1;
5132 pstrcpy(buf, sizeof(buf), optarg);
5133 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5134 fprintf(stderr,
5135 "qemu: unknown boot parameter '%s' in '%s'\n",
5136 buf, optarg);
5137 exit(1);
5138 }
5139
5140 if (legacy ||
5141 get_param_value(buf, sizeof(buf), "order", optarg)) {
5142 boot_devices_bitmap = parse_bootdevices(buf);
5143 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005144 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005145 if (!legacy) {
5146 if (get_param_value(buf, sizeof(buf),
5147 "once", optarg)) {
5148 boot_devices_bitmap |= parse_bootdevices(buf);
5149 standard_boot_devices = qemu_strdup(boot_devices);
5150 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5151 qemu_register_reset(restore_boot_devices,
5152 standard_boot_devices);
5153 }
5154 }
bellard36b486b2003-11-11 13:36:08 +00005155 }
5156 break;
bellardcd6f1162004-05-13 22:02:20 +00005157 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005158 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005159 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005160 break;
bellard52ca8d62006-06-14 16:03:05 +00005161#ifdef TARGET_I386
5162 case QEMU_OPTION_no_fd_bootchk:
5163 fd_bootchk = 0;
5164 break;
5165#endif
bellard7c9d8e02005-11-15 22:16:05 +00005166 case QEMU_OPTION_net:
5167 if (nb_net_clients >= MAX_NET_CLIENTS) {
5168 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00005169 exit(1);
5170 }
pbrookfd5f3932008-03-26 20:55:43 +00005171 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00005172 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00005173 break;
bellardc7f74642004-08-24 21:57:12 +00005174#ifdef CONFIG_SLIRP
5175 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005176 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005177 break;
ths47d5d012007-02-20 00:05:08 +00005178 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005179 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005180 break;
bellardc94c8d62004-09-13 21:37:34 +00005181#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005182 case QEMU_OPTION_smb:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005183 net_slirp_smb(optarg);
bellard9d728e82004-09-05 23:09:03 +00005184 break;
bellardc94c8d62004-09-13 21:37:34 +00005185#endif
bellard9bf05442004-08-25 22:12:49 +00005186 case QEMU_OPTION_redir:
Jan Kiszkaf3546de2009-06-24 14:42:28 +02005187 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00005188 break;
bellardc7f74642004-08-24 21:57:12 +00005189#endif
balrogdc72ac12008-11-09 00:04:26 +00005190 case QEMU_OPTION_bt:
5191 if (nb_bt_opts >= MAX_BT_CMDLINE) {
5192 fprintf(stderr, "qemu: too many bluetooth options\n");
5193 exit(1);
5194 }
5195 bt_opts[nb_bt_opts++] = optarg;
5196 break;
bellard1d14ffa2005-10-30 18:58:22 +00005197#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005198 case QEMU_OPTION_audio_help:
5199 AUD_help ();
5200 exit (0);
5201 break;
5202 case QEMU_OPTION_soundhw:
5203 select_soundhw (optarg);
5204 break;
5205#endif
bellardcd6f1162004-05-13 22:02:20 +00005206 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005207 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005208 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005209 case QEMU_OPTION_version:
5210 version();
5211 exit(0);
5212 break;
aurel3200f82b82008-04-27 21:12:55 +00005213 case QEMU_OPTION_m: {
5214 uint64_t value;
5215 char *ptr;
5216
5217 value = strtoul(optarg, &ptr, 10);
5218 switch (*ptr) {
5219 case 0: case 'M': case 'm':
5220 value <<= 20;
5221 break;
5222 case 'G': case 'g':
5223 value <<= 30;
5224 break;
5225 default:
5226 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005227 exit(1);
5228 }
aurel3200f82b82008-04-27 21:12:55 +00005229
5230 /* On 32-bit hosts, QEMU is limited by virtual address space */
5231 if (value > (2047 << 20)
blueswir1640f42e2009-04-19 10:18:01 +00005232#ifndef CONFIG_KQEMU
aurel3200f82b82008-04-27 21:12:55 +00005233 && HOST_LONG_BITS == 32
5234#endif
5235 ) {
5236 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5237 exit(1);
5238 }
5239 if (value != (uint64_t)(ram_addr_t)value) {
5240 fprintf(stderr, "qemu: ram size too large\n");
5241 exit(1);
5242 }
5243 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005244 break;
aurel3200f82b82008-04-27 21:12:55 +00005245 }
bellardcd6f1162004-05-13 22:02:20 +00005246 case QEMU_OPTION_d:
5247 {
5248 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005249 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005250
bellardcd6f1162004-05-13 22:02:20 +00005251 mask = cpu_str_to_log_mask(optarg);
5252 if (!mask) {
5253 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005254 for(item = cpu_log_items; item->mask != 0; item++) {
5255 printf("%-10s %s\n", item->name, item->help);
5256 }
5257 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005258 }
5259 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005260 }
bellardcd6f1162004-05-13 22:02:20 +00005261 break;
bellardcd6f1162004-05-13 22:02:20 +00005262 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005263 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005264 break;
aliguori59030a82009-04-05 18:43:41 +00005265 case QEMU_OPTION_gdb:
5266 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005267 break;
bellardcd6f1162004-05-13 22:02:20 +00005268 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005269 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005270 break;
j_mayer1192dad2007-10-05 13:08:35 +00005271 case QEMU_OPTION_bios:
5272 bios_name = optarg;
5273 break;
aurel321b530a62009-04-05 20:08:59 +00005274 case QEMU_OPTION_singlestep:
5275 singlestep = 1;
5276 break;
bellardcd6f1162004-05-13 22:02:20 +00005277 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005278 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005279 break;
blueswir15824d652009-03-28 06:44:27 +00005280#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00005281 case QEMU_OPTION_k:
5282 keyboard_layout = optarg;
5283 break;
blueswir15824d652009-03-28 06:44:27 +00005284#endif
bellardee22c2f2004-06-03 12:49:50 +00005285 case QEMU_OPTION_localtime:
5286 rtc_utc = 0;
5287 break;
malc3893c122008-09-28 00:42:05 +00005288 case QEMU_OPTION_vga:
5289 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005290 break;
blueswir15824d652009-03-28 06:44:27 +00005291#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005292 case QEMU_OPTION_g:
5293 {
5294 const char *p;
5295 int w, h, depth;
5296 p = optarg;
5297 w = strtol(p, (char **)&p, 10);
5298 if (w <= 0) {
5299 graphic_error:
5300 fprintf(stderr, "qemu: invalid resolution or depth\n");
5301 exit(1);
5302 }
5303 if (*p != 'x')
5304 goto graphic_error;
5305 p++;
5306 h = strtol(p, (char **)&p, 10);
5307 if (h <= 0)
5308 goto graphic_error;
5309 if (*p == 'x') {
5310 p++;
5311 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005312 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005313 depth != 24 && depth != 32)
5314 goto graphic_error;
5315 } else if (*p == '\0') {
5316 depth = graphic_depth;
5317 } else {
5318 goto graphic_error;
5319 }
ths3b46e622007-09-17 08:09:54 +00005320
bellarde9b137c2004-06-21 16:46:10 +00005321 graphic_width = w;
5322 graphic_height = h;
5323 graphic_depth = depth;
5324 }
5325 break;
blueswir15824d652009-03-28 06:44:27 +00005326#endif
ths20d8a3e2007-02-18 17:04:49 +00005327 case QEMU_OPTION_echr:
5328 {
5329 char *r;
5330 term_escape_char = strtol(optarg, &r, 0);
5331 if (r == optarg)
5332 printf("Bad argument to echr\n");
5333 break;
5334 }
bellard82c643f2004-07-14 17:28:13 +00005335 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005336 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005337 break;
5338 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005339 if (serial_device_index >= MAX_SERIAL_PORTS) {
5340 fprintf(stderr, "qemu: too many serial ports\n");
5341 exit(1);
5342 }
pbrookfd5f3932008-03-26 20:55:43 +00005343 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005344 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005345 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005346 case QEMU_OPTION_watchdog:
5347 i = select_watchdog(optarg);
5348 if (i > 0)
5349 exit (i == 1 ? 1 : 0);
5350 break;
5351 case QEMU_OPTION_watchdog_action:
5352 if (select_watchdog_action(optarg) == -1) {
5353 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5354 exit(1);
5355 }
5356 break;
aliguori51ecf132009-01-15 20:06:40 +00005357 case QEMU_OPTION_virtiocon:
5358 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5359 fprintf(stderr, "qemu: too many virtio consoles\n");
5360 exit(1);
5361 }
5362 virtio_consoles[virtio_console_index] = optarg;
5363 virtio_console_index++;
5364 break;
bellard6508fe52005-01-15 12:02:56 +00005365 case QEMU_OPTION_parallel:
5366 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5367 fprintf(stderr, "qemu: too many parallel ports\n");
5368 exit(1);
5369 }
pbrookfd5f3932008-03-26 20:55:43 +00005370 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005371 parallel_device_index++;
5372 break;
bellardd63d3072004-10-03 13:29:03 +00005373 case QEMU_OPTION_loadvm:
5374 loadvm = optarg;
5375 break;
5376 case QEMU_OPTION_full_screen:
5377 full_screen = 1;
5378 break;
ths667acca2006-12-11 02:08:05 +00005379#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005380 case QEMU_OPTION_no_frame:
5381 no_frame = 1;
5382 break;
ths3780e192007-06-21 21:08:02 +00005383 case QEMU_OPTION_alt_grab:
5384 alt_grab = 1;
5385 break;
ths667acca2006-12-11 02:08:05 +00005386 case QEMU_OPTION_no_quit:
5387 no_quit = 1;
5388 break;
aliguori7d957bd2009-01-15 22:14:11 +00005389 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005390 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005391 break;
ths667acca2006-12-11 02:08:05 +00005392#endif
bellardf7cce892004-12-08 22:21:25 +00005393 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005394 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005395 break;
bellarda09db212005-04-30 16:10:35 +00005396#ifdef TARGET_I386
5397 case QEMU_OPTION_win2k_hack:
5398 win2k_install_hack = 1;
5399 break;
aliguori73822ec2009-01-15 20:11:34 +00005400 case QEMU_OPTION_rtc_td_hack:
5401 rtc_td_hack = 1;
5402 break;
aliguori8a92ea22009-02-27 20:12:36 +00005403 case QEMU_OPTION_acpitable:
5404 if(acpi_table_add(optarg) < 0) {
5405 fprintf(stderr, "Wrong acpi table provided\n");
5406 exit(1);
5407 }
5408 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005409 case QEMU_OPTION_smbios:
5410 if(smbios_entry_add(optarg) < 0) {
5411 fprintf(stderr, "Wrong smbios provided\n");
5412 exit(1);
5413 }
5414 break;
bellarda09db212005-04-30 16:10:35 +00005415#endif
blueswir1640f42e2009-04-19 10:18:01 +00005416#ifdef CONFIG_KQEMU
Anthony Liguori52249f02009-07-09 17:28:08 -05005417 case QEMU_OPTION_enable_kqemu:
5418 kqemu_allowed = 1;
bellardd993e022005-02-10 22:00:06 +00005419 break;
bellard89bfc102006-02-08 22:46:31 +00005420 case QEMU_OPTION_kernel_kqemu:
5421 kqemu_allowed = 2;
5422 break;
bellardd993e022005-02-10 22:00:06 +00005423#endif
aliguori7ba1e612008-11-05 16:04:33 +00005424#ifdef CONFIG_KVM
5425 case QEMU_OPTION_enable_kvm:
5426 kvm_allowed = 1;
blueswir1640f42e2009-04-19 10:18:01 +00005427#ifdef CONFIG_KQEMU
aliguori7ba1e612008-11-05 16:04:33 +00005428 kqemu_allowed = 0;
5429#endif
5430 break;
5431#endif
bellardbb36d472005-11-05 14:22:28 +00005432 case QEMU_OPTION_usb:
5433 usb_enabled = 1;
5434 break;
bellarda594cfb2005-11-06 16:13:29 +00005435 case QEMU_OPTION_usbdevice:
5436 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005437 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005438 fprintf(stderr, "Too many USB devices\n");
5439 exit(1);
5440 }
pbrookfd5f3932008-03-26 20:55:43 +00005441 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005442 usb_devices_index++;
5443 break;
bellard6a00d602005-11-21 23:25:50 +00005444 case QEMU_OPTION_smp:
5445 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005446 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005447 fprintf(stderr, "Invalid number of CPUs\n");
5448 exit(1);
5449 }
5450 break;
bellard24236862006-04-30 21:28:36 +00005451 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005452 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005453 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005454 break;
blueswir15824d652009-03-28 06:44:27 +00005455#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005456 case QEMU_OPTION_no_acpi:
5457 acpi_enabled = 0;
5458 break;
aliguori16b29ae2008-12-17 23:28:44 +00005459 case QEMU_OPTION_no_hpet:
5460 no_hpet = 1;
5461 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005462 case QEMU_OPTION_balloon:
5463 if (balloon_parse(optarg) < 0) {
5464 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5465 exit(1);
5466 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005467 break;
blueswir15824d652009-03-28 06:44:27 +00005468#endif
bellardd1beab82006-10-02 19:44:22 +00005469 case QEMU_OPTION_no_reboot:
5470 no_reboot = 1;
5471 break;
aurel32b2f76162008-04-11 21:35:52 +00005472 case QEMU_OPTION_no_shutdown:
5473 no_shutdown = 1;
5474 break;
balrog9467cd42007-05-01 01:34:14 +00005475 case QEMU_OPTION_show_cursor:
5476 cursor_hide = 0;
5477 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005478 case QEMU_OPTION_uuid:
5479 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5480 fprintf(stderr, "Fail to parse UUID string."
5481 " Wrong format.\n");
5482 exit(1);
5483 }
5484 break;
blueswir15824d652009-03-28 06:44:27 +00005485#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005486 case QEMU_OPTION_daemonize:
5487 daemonize = 1;
5488 break;
blueswir15824d652009-03-28 06:44:27 +00005489#endif
ths9ae02552007-01-05 17:39:04 +00005490 case QEMU_OPTION_option_rom:
5491 if (nb_option_roms >= MAX_OPTION_ROMS) {
5492 fprintf(stderr, "Too many option ROMs\n");
5493 exit(1);
5494 }
5495 option_rom[nb_option_roms] = optarg;
5496 nb_option_roms++;
5497 break;
blueswir15824d652009-03-28 06:44:27 +00005498#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005499 case QEMU_OPTION_semihosting:
5500 semihosting_enabled = 1;
5501 break;
blueswir15824d652009-03-28 06:44:27 +00005502#endif
thsc35734b2007-03-19 15:17:08 +00005503 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005504 qemu_name = qemu_strdup(optarg);
5505 {
5506 char *p = strchr(qemu_name, ',');
5507 if (p != NULL) {
5508 *p++ = 0;
5509 if (strncmp(p, "process=", 8)) {
5510 fprintf(stderr, "Unknown subargument %s to -name", p);
5511 exit(1);
5512 }
5513 p += 8;
5514 set_proc_name(p);
5515 }
5516 }
thsc35734b2007-03-19 15:17:08 +00005517 break;
blueswir195efd112008-12-24 20:26:14 +00005518#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005519 case QEMU_OPTION_prom_env:
5520 if (nb_prom_envs >= MAX_PROM_ENVS) {
5521 fprintf(stderr, "Too many prom variables\n");
5522 exit(1);
5523 }
5524 prom_envs[nb_prom_envs] = optarg;
5525 nb_prom_envs++;
5526 break;
5527#endif
balrog2b8f2d42007-07-27 22:08:46 +00005528#ifdef TARGET_ARM
5529 case QEMU_OPTION_old_param:
5530 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005531 break;
balrog2b8f2d42007-07-27 22:08:46 +00005532#endif
thsf3dcfad2007-08-24 01:26:02 +00005533 case QEMU_OPTION_clock:
5534 configure_alarms(optarg);
5535 break;
bellard7e0af5d02007-11-07 16:24:33 +00005536 case QEMU_OPTION_startdate:
5537 {
5538 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005539 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005540 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005541 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005542 } else {
5543 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5544 &tm.tm_year,
5545 &tm.tm_mon,
5546 &tm.tm_mday,
5547 &tm.tm_hour,
5548 &tm.tm_min,
5549 &tm.tm_sec) == 6) {
5550 /* OK */
5551 } else if (sscanf(optarg, "%d-%d-%d",
5552 &tm.tm_year,
5553 &tm.tm_mon,
5554 &tm.tm_mday) == 3) {
5555 tm.tm_hour = 0;
5556 tm.tm_min = 0;
5557 tm.tm_sec = 0;
5558 } else {
5559 goto date_fail;
5560 }
5561 tm.tm_year -= 1900;
5562 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005563 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005564 if (rtc_start_date == -1) {
5565 date_fail:
5566 fprintf(stderr, "Invalid date format. Valid format are:\n"
5567 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5568 exit(1);
5569 }
balrogf6503052008-02-17 11:42:19 +00005570 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005571 }
5572 }
5573 break;
bellard26a5f132008-05-28 12:30:31 +00005574 case QEMU_OPTION_tb_size:
5575 tb_size = strtol(optarg, NULL, 0);
5576 if (tb_size < 0)
5577 tb_size = 0;
5578 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005579 case QEMU_OPTION_icount:
5580 use_icount = 1;
5581 if (strcmp(optarg, "auto") == 0) {
5582 icount_time_shift = -1;
5583 } else {
5584 icount_time_shift = strtol(optarg, NULL, 0);
5585 }
5586 break;
aliguori5bb79102008-10-13 03:12:02 +00005587 case QEMU_OPTION_incoming:
5588 incoming = optarg;
5589 break;
blueswir15824d652009-03-28 06:44:27 +00005590#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005591 case QEMU_OPTION_chroot:
5592 chroot_dir = optarg;
5593 break;
5594 case QEMU_OPTION_runas:
5595 run_as = optarg;
5596 break;
blueswir15824d652009-03-28 06:44:27 +00005597#endif
aliguorie37630c2009-04-22 15:19:10 +00005598#ifdef CONFIG_XEN
5599 case QEMU_OPTION_xen_domid:
5600 xen_domid = atoi(optarg);
5601 break;
5602 case QEMU_OPTION_xen_create:
5603 xen_mode = XEN_CREATE;
5604 break;
5605 case QEMU_OPTION_xen_attach:
5606 xen_mode = XEN_ATTACH;
5607 break;
5608#endif
bellardcd6f1162004-05-13 22:02:20 +00005609 }
bellard0824d6f2003-06-24 13:42:40 +00005610 }
5611 }
bellard330d0412003-07-26 18:11:40 +00005612
Paul Brook5cea8592009-05-30 00:52:44 +01005613 /* If no data_dir is specified then try to find it relative to the
5614 executable path. */
5615 if (!data_dir) {
5616 data_dir = find_datadir(argv[0]);
5617 }
5618 /* If all else fails use the install patch specified when building. */
5619 if (!data_dir) {
5620 data_dir = CONFIG_QEMU_SHAREDIR;
5621 }
5622
blueswir1640f42e2009-04-19 10:18:01 +00005623#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
aliguori7ba1e612008-11-05 16:04:33 +00005624 if (kvm_allowed && kqemu_allowed) {
5625 fprintf(stderr,
5626 "You can not enable both KVM and kqemu at the same time\n");
5627 exit(1);
5628 }
5629#endif
5630
balrog3d878ca2008-10-28 10:59:59 +00005631 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005632 if (smp_cpus > machine->max_cpus) {
5633 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5634 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5635 machine->max_cpus);
5636 exit(1);
5637 }
5638
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005639 if (display_type == DT_NOGRAPHIC) {
aliguoribc0129d2008-08-01 15:12:34 +00005640 if (serial_device_index == 0)
5641 serial_devices[0] = "stdio";
5642 if (parallel_device_index == 0)
5643 parallel_devices[0] = "null";
5644 if (strncmp(monitor_device, "vc", 2) == 0)
5645 monitor_device = "stdio";
5646 }
5647
ths71e3ceb2006-12-22 02:11:31 +00005648#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005649 if (daemonize) {
5650 pid_t pid;
5651
5652 if (pipe(fds) == -1)
5653 exit(1);
5654
5655 pid = fork();
5656 if (pid > 0) {
5657 uint8_t status;
5658 ssize_t len;
5659
5660 close(fds[1]);
5661
5662 again:
ths93815bc2007-03-19 15:58:31 +00005663 len = read(fds[0], &status, 1);
5664 if (len == -1 && (errno == EINTR))
5665 goto again;
5666
5667 if (len != 1)
5668 exit(1);
5669 else if (status == 1) {
5670 fprintf(stderr, "Could not acquire pidfile\n");
5671 exit(1);
5672 } else
5673 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005674 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005675 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005676
5677 setsid();
5678
5679 pid = fork();
5680 if (pid > 0)
5681 exit(0);
5682 else if (pid < 0)
5683 exit(1);
5684
5685 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005686
5687 signal(SIGTSTP, SIG_IGN);
5688 signal(SIGTTOU, SIG_IGN);
5689 signal(SIGTTIN, SIG_IGN);
5690 }
ths71e3ceb2006-12-22 02:11:31 +00005691
thsaa26bb22007-03-25 21:33:06 +00005692 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005693 if (daemonize) {
5694 uint8_t status = 1;
5695 write(fds[1], &status, 1);
5696 } else
5697 fprintf(stderr, "Could not acquire pid file\n");
5698 exit(1);
5699 }
blueswir1b9e82a52009-04-05 18:03:31 +00005700#endif
ths93815bc2007-03-19 15:58:31 +00005701
blueswir1640f42e2009-04-19 10:18:01 +00005702#ifdef CONFIG_KQEMU
bellardff3fbb32006-01-08 10:53:14 +00005703 if (smp_cpus > 1)
5704 kqemu_allowed = 0;
5705#endif
aliguori3fcf7b62009-04-24 18:03:25 +00005706 if (qemu_init_main_loop()) {
5707 fprintf(stderr, "qemu_init_main_loop failed\n");
5708 exit(1);
5709 }
bellarda20dd502003-09-30 21:07:02 +00005710 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005711
thsf8d39c02008-07-03 10:01:15 +00005712 if (!linux_boot && *kernel_cmdline != '\0') {
5713 fprintf(stderr, "-append only allowed with -kernel option\n");
5714 exit(1);
5715 }
5716
5717 if (!linux_boot && initrd_filename != NULL) {
5718 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5719 exit(1);
5720 }
5721
bellardb118d612003-06-30 23:36:21 +00005722 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005723
pbrook634fce92006-07-15 17:40:09 +00005724 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005725 if (init_timer_alarm() < 0) {
5726 fprintf(stderr, "could not initialize alarm timer\n");
5727 exit(1);
5728 }
pbrook2e70f6e2008-06-29 01:03:05 +00005729 if (use_icount && icount_time_shift < 0) {
5730 use_icount = 2;
5731 /* 125MIPS seems a reasonable initial guess at the guest speed.
5732 It will be corrected fairly quickly anyway. */
5733 icount_time_shift = 3;
5734 init_icount_adjust();
5735 }
pbrook634fce92006-07-15 17:40:09 +00005736
bellardfd1dff42006-02-01 21:29:26 +00005737#ifdef _WIN32
5738 socket_init();
5739#endif
5740
bellard7c9d8e02005-11-15 22:16:05 +00005741 /* init network clients */
5742 if (nb_net_clients == 0) {
5743 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005744 net_clients[nb_net_clients++] = "nic";
5745#ifdef CONFIG_SLIRP
5746 net_clients[nb_net_clients++] = "user";
5747#endif
bellardc20709a2004-04-21 23:27:19 +00005748 }
5749
bellard7c9d8e02005-11-15 22:16:05 +00005750 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005751 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005752 exit(1);
bellard702c6512004-04-02 21:21:32 +00005753 }
bellardf1510b22003-06-25 00:07:40 +00005754
Glauber Costa406c8df2009-06-17 09:05:30 -04005755 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5756 net_set_boot_mask(net_boot);
5757
5758 net_client_check();
thseec85c22007-01-05 17:41:07 +00005759
balrogdc72ac12008-11-09 00:04:26 +00005760 /* init the bluetooth world */
5761 for (i = 0; i < nb_bt_opts; i++)
5762 if (bt_parse(bt_opts[i]))
5763 exit(1);
5764
bellard0824d6f2003-06-24 13:42:40 +00005765 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005766 if (ram_size == 0)
5767 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005768
blueswir1640f42e2009-04-19 10:18:01 +00005769#ifdef CONFIG_KQEMU
pbrook94a6b542009-04-11 17:15:54 +00005770 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5771 guest ram allocation. It needs to go away. */
5772 if (kqemu_allowed) {
Paul Brook4cfce482009-05-13 18:08:17 +01005773 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
pbrook94a6b542009-04-11 17:15:54 +00005774 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5775 if (!kqemu_phys_ram_base) {
5776 fprintf(stderr, "Could not allocate physical memory\n");
5777 exit(1);
5778 }
balrog7fb4fdc2008-04-24 17:59:27 +00005779 }
pbrook94a6b542009-04-11 17:15:54 +00005780#endif
bellard0824d6f2003-06-24 13:42:40 +00005781
bellard26a5f132008-05-28 12:30:31 +00005782 /* init the dynamic translator */
5783 cpu_exec_init_all(tb_size * 1024 * 1024);
5784
bellard5905b2e2004-08-01 21:53:26 +00005785 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005786
5787 /* we always create the cdrom drive, even if no disk is there */
5788
5789 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005790 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005791
balrog9d413d12007-12-04 00:10:34 +00005792 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005793
5794 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005795 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005796
balrog9d413d12007-12-04 00:10:34 +00005797 /* we always create one sd slot, even if no card is in it */
5798
5799 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005800 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005801
ths96d30e42007-01-07 20:42:14 +00005802 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005803
thse4bcb142007-12-02 04:51:10 +00005804 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005805 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005806 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005807
bellardc88676f2006-08-06 13:36:11 +00005808 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005809 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005810
aliguori3023f332009-01-16 19:04:14 +00005811#ifndef _WIN32
5812 /* must be after terminal init, SDL library changes signal handlers */
Jan Kiszka7c3370d2009-05-08 12:34:17 +02005813 sighandler_setup();
aliguori3023f332009-01-16 19:04:14 +00005814#endif
5815
5816 /* Maintain compatibility with multiple stdio monitors */
5817 if (!strcmp(monitor_device,"stdio")) {
5818 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5819 const char *devname = serial_devices[i];
5820 if (devname && !strcmp(devname,"mon:stdio")) {
5821 monitor_device = NULL;
5822 break;
5823 } else if (devname && !strcmp(devname,"stdio")) {
5824 monitor_device = NULL;
5825 serial_devices[i] = "mon:stdio";
5826 break;
5827 }
5828 }
5829 }
5830
aliguori268a3622009-04-21 22:30:27 +00005831 if (nb_numa_nodes > 0) {
5832 int i;
5833
5834 if (nb_numa_nodes > smp_cpus) {
5835 nb_numa_nodes = smp_cpus;
5836 }
5837
5838 /* If no memory size if given for any node, assume the default case
5839 * and distribute the available memory equally across all nodes
5840 */
5841 for (i = 0; i < nb_numa_nodes; i++) {
5842 if (node_mem[i] != 0)
5843 break;
5844 }
5845 if (i == nb_numa_nodes) {
5846 uint64_t usedmem = 0;
5847
5848 /* On Linux, the each node's border has to be 8MB aligned,
5849 * the final node gets the rest.
5850 */
5851 for (i = 0; i < nb_numa_nodes - 1; i++) {
5852 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5853 usedmem += node_mem[i];
5854 }
5855 node_mem[i] = ram_size - usedmem;
5856 }
5857
5858 for (i = 0; i < nb_numa_nodes; i++) {
5859 if (node_cpumask[i] != 0)
5860 break;
5861 }
5862 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5863 * must cope with this anyway, because there are BIOSes out there in
5864 * real machines which also use this scheme.
5865 */
5866 if (i == nb_numa_nodes) {
5867 for (i = 0; i < smp_cpus; i++) {
5868 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5869 }
5870 }
5871 }
5872
aliguori3023f332009-01-16 19:04:14 +00005873 if (kvm_enabled()) {
5874 int ret;
5875
5876 ret = kvm_init(smp_cpus);
5877 if (ret < 0) {
5878 fprintf(stderr, "failed to initialize KVM\n");
5879 exit(1);
5880 }
5881 }
5882
aliguori4c621802009-01-16 21:48:20 +00005883 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005884 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005885 if (!monitor_hd) {
5886 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5887 exit(1);
5888 }
5889 }
5890
aliguori2796dae2009-01-16 20:23:27 +00005891 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5892 const char *devname = serial_devices[i];
5893 if (devname && strcmp(devname, "none")) {
5894 char label[32];
5895 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005896 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005897 if (!serial_hds[i]) {
5898 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5899 devname);
5900 exit(1);
5901 }
5902 }
5903 }
5904
5905 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5906 const char *devname = parallel_devices[i];
5907 if (devname && strcmp(devname, "none")) {
5908 char label[32];
5909 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005910 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005911 if (!parallel_hds[i]) {
5912 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5913 devname);
5914 exit(1);
5915 }
5916 }
5917 }
5918
5919 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5920 const char *devname = virtio_consoles[i];
5921 if (devname && strcmp(devname, "none")) {
5922 char label[32];
5923 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005924 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005925 if (!virtcon_hds[i]) {
5926 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5927 devname);
5928 exit(1);
5929 }
5930 }
5931 }
5932
Paul Brookaae94602009-05-14 22:35:06 +01005933 module_call_init(MODULE_INIT_DEVICE);
5934
Paul Brookfbe1b592009-05-13 17:56:25 +01005935 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005936 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5937
aliguori268a3622009-04-21 22:30:27 +00005938
5939 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5940 for (i = 0; i < nb_numa_nodes; i++) {
5941 if (node_cpumask[i] & (1 << env->cpu_index)) {
5942 env->numa_node = i;
5943 }
5944 }
5945 }
5946
aliguori6f338c32009-02-11 15:21:54 +00005947 current_machine = machine;
5948
aliguori3023f332009-01-16 19:04:14 +00005949 /* init USB devices */
5950 if (usb_enabled) {
5951 for(i = 0; i < usb_devices_index; i++) {
aliguoric0f4ce72009-03-05 23:01:01 +00005952 if (usb_device_add(usb_devices[i], 0) < 0) {
aliguori3023f332009-01-16 19:04:14 +00005953 fprintf(stderr, "Warning: could not add USB device %s\n",
5954 usb_devices[i]);
5955 }
5956 }
5957 }
5958
aliguori8f391ab2009-01-19 16:34:10 +00005959 if (!display_state)
5960 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005961 /* just use the first displaystate for the moment */
5962 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005963
5964 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005965#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005966 display_type = DT_SDL;
5967#else
5968 display_type = DT_VNC;
5969 vnc_display = "localhost:0,to=99";
5970 show_vnc_port = 1;
5971#endif
5972 }
5973
5974
5975 switch (display_type) {
5976 case DT_NOGRAPHIC:
5977 break;
5978#if defined(CONFIG_CURSES)
5979 case DT_CURSES:
5980 curses_display_init(ds, full_screen);
5981 break;
5982#endif
bellard5b0753e2005-03-01 21:37:28 +00005983#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005984 case DT_SDL:
5985 sdl_display_init(ds, full_screen, no_frame);
5986 break;
bellard5b0753e2005-03-01 21:37:28 +00005987#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005988 case DT_SDL:
5989 cocoa_display_init(ds, full_screen);
5990 break;
bellard313aa562003-08-10 21:52:11 +00005991#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005992 case DT_VNC:
5993 vnc_display_init(ds);
5994 if (vnc_display_open(ds, vnc_display) < 0)
5995 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005996
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005997 if (show_vnc_port) {
5998 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005999 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006000 break;
6001 default:
6002 break;
bellard313aa562003-08-10 21:52:11 +00006003 }
aliguori7d957bd2009-01-15 22:14:11 +00006004 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00006005
aliguori3023f332009-01-16 19:04:14 +00006006 dcl = ds->listeners;
6007 while (dcl != NULL) {
6008 if (dcl->dpy_refresh != NULL) {
6009 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6010 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00006011 }
aliguori3023f332009-01-16 19:04:14 +00006012 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00006013 }
aliguori3023f332009-01-16 19:04:14 +00006014
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006015 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00006016 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6017 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6018 }
6019
aliguori2796dae2009-01-16 20:23:27 +00006020 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00006021 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00006022
aliguori4c621802009-01-16 21:48:20 +00006023 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00006024 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00006025
bellard8d11df92004-08-24 21:13:40 +00006026 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006027 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006028 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006029 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006030 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00006031 }
bellard82c643f2004-07-14 17:28:13 +00006032 }
bellard82c643f2004-07-14 17:28:13 +00006033
bellard6508fe52005-01-15 12:02:56 +00006034 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006035 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006036 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006037 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006038 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00006039 }
6040 }
6041
aliguori9ede2fd2009-01-15 20:05:25 +00006042 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6043 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00006044 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00006045 if (strstart(devname, "vc", 0))
6046 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
6047 }
6048 }
6049
aliguori59030a82009-04-05 18:43:41 +00006050 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6051 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6052 gdbstub_dev);
6053 exit(1);
balrog45669e02007-07-02 13:20:17 +00006054 }
balrog45669e02007-07-02 13:20:17 +00006055
bellardd63d3072004-10-03 13:29:03 +00006056 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00006057 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00006058
aliguori5bb79102008-10-13 03:12:02 +00006059 if (incoming) {
6060 autostart = 0; /* fixme how to deal with -daemonize */
6061 qemu_start_incoming_migration(incoming);
6062 }
6063
aliguoric0f4ce72009-03-05 23:01:01 +00006064 if (autostart)
6065 vm_start();
thsffd843b2006-12-21 19:46:43 +00006066
blueswir1b9e82a52009-04-05 18:03:31 +00006067#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00006068 if (daemonize) {
6069 uint8_t status = 0;
6070 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00006071
6072 again1:
6073 len = write(fds[1], &status, 1);
6074 if (len == -1 && (errno == EINTR))
6075 goto again1;
6076
6077 if (len != 1)
6078 exit(1);
6079
aliguoribd54b862008-07-23 00:58:33 +00006080 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00006081 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00006082 if (fd == -1)
6083 exit(1);
aliguori08585322009-02-27 22:09:45 +00006084 }
ths71e3ceb2006-12-22 02:11:31 +00006085
aliguori08585322009-02-27 22:09:45 +00006086 if (run_as) {
6087 pwd = getpwnam(run_as);
6088 if (!pwd) {
6089 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6090 exit(1);
6091 }
6092 }
ths71e3ceb2006-12-22 02:11:31 +00006093
aliguori08585322009-02-27 22:09:45 +00006094 if (chroot_dir) {
6095 if (chroot(chroot_dir) < 0) {
6096 fprintf(stderr, "chroot failed\n");
6097 exit(1);
6098 }
6099 chdir("/");
6100 }
6101
6102 if (run_as) {
6103 if (setgid(pwd->pw_gid) < 0) {
6104 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6105 exit(1);
6106 }
6107 if (setuid(pwd->pw_uid) < 0) {
6108 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6109 exit(1);
6110 }
6111 if (setuid(0) != -1) {
6112 fprintf(stderr, "Dropping privileges failed\n");
6113 exit(1);
6114 }
6115 }
aliguori08585322009-02-27 22:09:45 +00006116
6117 if (daemonize) {
6118 dup2(fd, 0);
6119 dup2(fd, 1);
6120 dup2(fd, 2);
6121
6122 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006123 }
blueswir1b9e82a52009-04-05 18:03:31 +00006124#endif
ths71e3ceb2006-12-22 02:11:31 +00006125
bellard8a7ddc32004-03-31 19:00:16 +00006126 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006127 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006128 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006129
bellard0824d6f2003-06-24 13:42:40 +00006130 return 0;
6131}