blob: 0bcc0c971a25016fd6fd1531fd5b2a5929321041 [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
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
Blue Swirl96555a92009-07-17 11:01:45 +000034/* Needed early to override system queue definitions on BSD */
35#include "sys-queue.h"
blueswir1d40cdb12009-03-07 16:52:02 +000036
bellard67b915a2004-03-31 23:37:16 +000037#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010038#include <libgen.h>
aliguori08585322009-02-27 22:09:45 +000039#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000042#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000043#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000046#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000047#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000048#include <net/if.h>
49#if defined(__NetBSD__)
50#include <net/if_tap.h>
51#endif
52#ifdef __linux__
53#include <linux/if_tun.h>
54#endif
55#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000056#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000057#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000058#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020059#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000060#include <sys/stat.h>
blueswir1c5e97232009-03-07 20:06:23 +000061#if defined(__FreeBSD__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000062#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000063#else
64#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000065#endif
ths5c40d2b2007-06-23 16:03:36 +000066#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
67#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000068#else
blueswir1223f0d72008-09-30 18:12:18 +000069#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000070#include <pty.h>
71#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000072#include <linux/rtc.h>
Andi Kleen18894652009-07-02 09:34:17 +020073#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000074
75/* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77/* #include <linux/hpet.h> */
78#include "hpet.h"
79
bellarde57a8c02005-11-10 23:58:52 +000080#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000081#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000082#endif
83#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000084#include <sys/stat.h>
85#include <sys/ethernet.h>
86#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000087#include <netinet/arp.h>
88#include <netinet/in.h>
89#include <netinet/in_systm.h>
90#include <netinet/ip.h>
91#include <netinet/ip_icmp.h> // must come after ip.h
92#include <netinet/udp.h>
93#include <netinet/tcp.h>
94#include <net/if.h>
95#include <syslog.h>
96#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000097#endif
bellard7d3505c2004-05-12 19:32:15 +000098#endif
bellardec530c82006-04-25 22:36:06 +000099#endif
bellard67b915a2004-03-31 23:37:16 +0000100
blueswir19892fbf2008-08-24 10:34:20 +0000101#if defined(__OpenBSD__)
102#include <util.h>
103#endif
104
ths8a16d272008-07-19 09:56:24 +0000105#if defined(CONFIG_VDE)
106#include <libvdeplug.h>
107#endif
108
bellard67b915a2004-03-31 23:37:16 +0000109#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000110#include <windows.h>
ths4fddf622007-12-17 04:42:29 +0000111#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000112#endif
113
bellard73332e52004-04-04 20:22:28 +0000114#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200115#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200116#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000117int qemu_main(int argc, char **argv, char **envp);
118int main(int argc, char **argv)
119{
Stefan Weil59a36a22009-06-18 20:11:03 +0200120 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000121}
122#undef main
123#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000124#endif
bellard73332e52004-04-04 20:22:28 +0000125#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000126
bellard5b0753e2005-03-01 21:37:28 +0000127#ifdef CONFIG_COCOA
128#undef main
129#define main qemu_main
130#endif /* CONFIG_COCOA */
131
blueswir1511d2b12009-03-07 15:32:56 +0000132#include "hw/hw.h"
133#include "hw/boards.h"
134#include "hw/usb.h"
135#include "hw/pcmcia.h"
136#include "hw/pc.h"
137#include "hw/audiodev.h"
138#include "hw/isa.h"
139#include "hw/baum.h"
140#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100141#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000142#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000143#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200144#include "hw/qdev.h"
aurel325ef4efa2009-03-10 21:43:35 +0000145#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000146#include "net.h"
147#include "monitor.h"
148#include "console.h"
149#include "sysemu.h"
150#include "gdbstub.h"
151#include "qemu-timer.h"
152#include "qemu-char.h"
153#include "cache-utils.h"
154#include "block.h"
blueswir1a718ace2009-03-28 08:24:44 +0000155#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000156#include "audio/audio.h"
157#include "migration.h"
158#include "kvm.h"
159#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200160#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200161#include "qemu-config.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
Paul Brook5cea8592009-05-30 00:52:44 +0100176static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000177const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000178/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000179 to store the VM snapshots */
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200180struct drivelist drives = TAILQ_HEAD_INITIALIZER(drives);
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +0200181struct driveoptlist driveopts = TAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000182enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000183static DisplayState *display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500184DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000185const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000186int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000187ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000188int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000189NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000190int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200191int autostart;
balrogf6503052008-02-17 11:42:19 +0000192static int rtc_utc = 1;
193static int rtc_date_offset = -1; /* -1 means no change */
Zachary Amsden86176752009-07-30 00:15:02 -1000194int vga_interface_type = VGA_CIRRUS;
bellardd8272202005-04-06 20:32:23 +0000195#ifdef TARGET_SPARC
196int graphic_width = 1024;
197int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000198int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000199#else
bellard1bfe8562004-07-08 21:17:50 +0000200int graphic_width = 800;
201int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000202int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000203#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000204static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000205#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000206static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000207#endif
ths667acca2006-12-11 02:08:05 +0000208int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000209CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000210CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000211CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000212#ifdef TARGET_I386
213int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000214int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000215#endif
bellardbb36d472005-11-05 14:22:28 +0000216int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000217int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000218int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200219int max_cpus = 0;
ths73fc9742006-12-22 02:09:07 +0000220const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000221int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000222int no_hpet = 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +0200223int virtio_balloon = 1;
224const char *virtio_balloon_devaddr;
bellard52ca8d62006-06-14 16:03:05 +0000225int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000226int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000227int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000228int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000229int graphic_rotate = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000230#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000231int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000232#endif
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100233WatchdogTimerModel *watchdog = NULL;
234int watchdog_action = WDT_RESET;
ths9ae02552007-01-05 17:39:04 +0000235const char *option_rom[MAX_OPTION_ROMS];
236int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000237int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000238#ifdef TARGET_ARM
239int old_param = 0;
240#endif
thsc35734b2007-03-19 15:17:08 +0000241const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000242int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000243#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000244unsigned int nb_prom_envs = 0;
245const char *prom_envs[MAX_PROM_ENVS];
246#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200247int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000248
aliguori268a3622009-04-21 22:30:27 +0000249int nb_numa_nodes;
250uint64_t node_mem[MAX_NODES];
251uint64_t node_cpumask[MAX_NODES];
252
balrogee5605e2007-12-03 03:01:40 +0000253static CPUState *cur_cpu;
254static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000255static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000256/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000257static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000258/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000259#define MAX_ICOUNT_SHIFT 10
260/* Compensate for varying guest execution speed. */
261static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000262static QEMUTimer *icount_rt_timer;
263static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000264static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000265
blueswir18fcb1b92008-09-18 18:29:08 +0000266uint8_t qemu_uuid[16];
267
Jan Kiszka76e30d02009-07-02 00:19:02 +0200268static QEMUBootSetHandler *boot_set_handler;
269static void *boot_set_opaque;
270
bellard0824d6f2003-06-24 13:42:40 +0000271/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000272/* x86 ISA bus support */
273
274target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000275PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000276
bellard0824d6f2003-06-24 13:42:40 +0000277/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000278void hw_error(const char *fmt, ...)
279{
280 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000281 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000282
283 va_start(ap, fmt);
284 fprintf(stderr, "qemu: hardware error: ");
285 vfprintf(stderr, fmt, ap);
286 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000287 for(env = first_cpu; env != NULL; env = env->next_cpu) {
288 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000289#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000290 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000291#else
bellard6a00d602005-11-21 23:25:50 +0000292 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000293#endif
bellard6a00d602005-11-21 23:25:50 +0000294 }
bellard0824d6f2003-06-24 13:42:40 +0000295 va_end(ap);
296 abort();
297}
Andi Kleen18894652009-07-02 09:34:17 +0200298
299static void set_proc_name(const char *s)
300{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700301#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200302 char name[16];
303 if (!s)
304 return;
305 name[sizeof(name) - 1] = 0;
306 strncpy(name, s, sizeof(name));
307 /* Could rewrite argv[0] too, but that's a bit more complicated.
308 This simple way is enough for `top'. */
309 prctl(PR_SET_NAME, name);
310#endif
311}
aliguoridf751fa2008-12-04 20:19:35 +0000312
313/***************/
314/* ballooning */
315
316static QEMUBalloonEvent *qemu_balloon_event;
317void *qemu_balloon_event_opaque;
318
319void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
320{
321 qemu_balloon_event = func;
322 qemu_balloon_event_opaque = opaque;
323}
324
325void qemu_balloon(ram_addr_t target)
326{
327 if (qemu_balloon_event)
328 qemu_balloon_event(qemu_balloon_event_opaque, target);
329}
330
331ram_addr_t qemu_balloon_status(void)
332{
333 if (qemu_balloon_event)
334 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
335 return 0;
336}
bellard0824d6f2003-06-24 13:42:40 +0000337
bellard8a7ddc32004-03-31 19:00:16 +0000338/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000339/* keyboard/mouse */
340
341static QEMUPutKBDEvent *qemu_put_kbd_event;
342static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000343static QEMUPutMouseEntry *qemu_put_mouse_event_head;
344static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000345
346void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
347{
348 qemu_put_kbd_event_opaque = opaque;
349 qemu_put_kbd_event = func;
350}
351
ths455204e2007-01-05 16:42:13 +0000352QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
353 void *opaque, int absolute,
354 const char *name)
bellard63066f42004-06-03 18:45:02 +0000355{
ths455204e2007-01-05 16:42:13 +0000356 QEMUPutMouseEntry *s, *cursor;
357
358 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000359
360 s->qemu_put_mouse_event = func;
361 s->qemu_put_mouse_event_opaque = opaque;
362 s->qemu_put_mouse_event_absolute = absolute;
363 s->qemu_put_mouse_event_name = qemu_strdup(name);
364 s->next = NULL;
365
366 if (!qemu_put_mouse_event_head) {
367 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
368 return s;
369 }
370
371 cursor = qemu_put_mouse_event_head;
372 while (cursor->next != NULL)
373 cursor = cursor->next;
374
375 cursor->next = s;
376 qemu_put_mouse_event_current = s;
377
378 return s;
379}
380
381void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
382{
383 QEMUPutMouseEntry *prev = NULL, *cursor;
384
385 if (!qemu_put_mouse_event_head || entry == NULL)
386 return;
387
388 cursor = qemu_put_mouse_event_head;
389 while (cursor != NULL && cursor != entry) {
390 prev = cursor;
391 cursor = cursor->next;
392 }
393
394 if (cursor == NULL) // does not exist or list empty
395 return;
396 else if (prev == NULL) { // entry is head
397 qemu_put_mouse_event_head = cursor->next;
398 if (qemu_put_mouse_event_current == entry)
399 qemu_put_mouse_event_current = cursor->next;
400 qemu_free(entry->qemu_put_mouse_event_name);
401 qemu_free(entry);
402 return;
403 }
404
405 prev->next = entry->next;
406
407 if (qemu_put_mouse_event_current == entry)
408 qemu_put_mouse_event_current = prev;
409
410 qemu_free(entry->qemu_put_mouse_event_name);
411 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000412}
413
414void kbd_put_keycode(int keycode)
415{
416 if (qemu_put_kbd_event) {
417 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
418 }
419}
420
421void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
422{
ths455204e2007-01-05 16:42:13 +0000423 QEMUPutMouseEvent *mouse_event;
424 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000425 int width;
ths455204e2007-01-05 16:42:13 +0000426
427 if (!qemu_put_mouse_event_current) {
428 return;
429 }
430
431 mouse_event =
432 qemu_put_mouse_event_current->qemu_put_mouse_event;
433 mouse_event_opaque =
434 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
435
436 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000437 if (graphic_rotate) {
438 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
439 width = 0x7fff;
440 else
aurel32b94ed572008-03-10 19:34:27 +0000441 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000442 mouse_event(mouse_event_opaque,
443 width - dy, dx, dz, buttons_state);
444 } else
445 mouse_event(mouse_event_opaque,
446 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000447 }
448}
449
bellard09b26c52006-04-12 21:09:08 +0000450int kbd_mouse_is_absolute(void)
451{
ths455204e2007-01-05 16:42:13 +0000452 if (!qemu_put_mouse_event_current)
453 return 0;
454
455 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
456}
457
aliguori376253e2009-03-05 23:01:23 +0000458void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000459{
460 QEMUPutMouseEntry *cursor;
461 int index = 0;
462
463 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000464 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000465 return;
466 }
467
aliguori376253e2009-03-05 23:01:23 +0000468 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000469 cursor = qemu_put_mouse_event_head;
470 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000471 monitor_printf(mon, "%c Mouse #%d: %s\n",
472 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
473 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000474 index++;
475 cursor = cursor->next;
476 }
477}
478
aliguori376253e2009-03-05 23:01:23 +0000479void do_mouse_set(Monitor *mon, int index)
ths455204e2007-01-05 16:42:13 +0000480{
481 QEMUPutMouseEntry *cursor;
482 int i = 0;
483
484 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000485 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000486 return;
487 }
488
489 cursor = qemu_put_mouse_event_head;
490 while (cursor != NULL && index != i) {
491 i++;
492 cursor = cursor->next;
493 }
494
495 if (cursor != NULL)
496 qemu_put_mouse_event_current = cursor;
497 else
aliguori376253e2009-03-05 23:01:23 +0000498 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000499}
500
bellard87858c82003-06-27 12:01:39 +0000501/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000502uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000503{
504 union {
505 uint64_t ll;
506 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200507#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000508 uint32_t high, low;
509#else
510 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000511#endif
bellard87858c82003-06-27 12:01:39 +0000512 } l;
513 } u, res;
514 uint64_t rl, rh;
515
516 u.ll = a;
517 rl = (uint64_t)u.l.low * (uint64_t)b;
518 rh = (uint64_t)u.l.high * (uint64_t)b;
519 rh += (rl >> 32);
520 res.l.high = rh / c;
521 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
522 return res.ll;
523}
524
bellard1dce7c32006-07-13 23:20:22 +0000525/***********************************************************/
526/* real time host monotonic timer */
527
528#define QEMU_TIMER_BASE 1000000000LL
529
530#ifdef WIN32
531
532static int64_t clock_freq;
533
534static void init_get_clock(void)
535{
bellarda8e5ac32006-07-14 09:36:13 +0000536 LARGE_INTEGER freq;
537 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000538 ret = QueryPerformanceFrequency(&freq);
539 if (ret == 0) {
540 fprintf(stderr, "Could not calibrate ticks\n");
541 exit(1);
542 }
543 clock_freq = freq.QuadPart;
544}
545
546static int64_t get_clock(void)
547{
548 LARGE_INTEGER ti;
549 QueryPerformanceCounter(&ti);
550 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
551}
552
553#else
554
555static int use_rt_clock;
556
557static void init_get_clock(void)
558{
559 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000560#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
561 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000562 {
563 struct timespec ts;
564 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
565 use_rt_clock = 1;
566 }
567 }
568#endif
569}
570
571static int64_t get_clock(void)
572{
blueswir1c5e97232009-03-07 20:06:23 +0000573#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
574 || defined(__DragonFly__)
bellard1dce7c32006-07-13 23:20:22 +0000575 if (use_rt_clock) {
576 struct timespec ts;
577 clock_gettime(CLOCK_MONOTONIC, &ts);
578 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000579 } else
bellard1dce7c32006-07-13 23:20:22 +0000580#endif
581 {
582 /* XXX: using gettimeofday leads to problems if the date
583 changes, so it should be avoided. */
584 struct timeval tv;
585 gettimeofday(&tv, NULL);
586 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
587 }
588}
bellard1dce7c32006-07-13 23:20:22 +0000589#endif
590
pbrook2e70f6e2008-06-29 01:03:05 +0000591/* Return the virtual CPU time, based on the instruction counter. */
592static int64_t cpu_get_icount(void)
593{
594 int64_t icount;
595 CPUState *env = cpu_single_env;;
596 icount = qemu_icount;
597 if (env) {
598 if (!can_do_io(env))
599 fprintf(stderr, "Bad clock read\n");
600 icount -= (env->icount_decr.u16.low + env->icount_extra);
601 }
602 return qemu_icount_bias + (icount << icount_time_shift);
603}
604
bellard1dce7c32006-07-13 23:20:22 +0000605/***********************************************************/
606/* guest cycle counter */
607
608static int64_t cpu_ticks_prev;
609static int64_t cpu_ticks_offset;
610static int64_t cpu_clock_offset;
611static int cpu_ticks_enabled;
612
613/* return the host CPU cycle counter and handle stop/restart */
614int64_t cpu_get_ticks(void)
615{
pbrook2e70f6e2008-06-29 01:03:05 +0000616 if (use_icount) {
617 return cpu_get_icount();
618 }
bellard1dce7c32006-07-13 23:20:22 +0000619 if (!cpu_ticks_enabled) {
620 return cpu_ticks_offset;
621 } else {
622 int64_t ticks;
623 ticks = cpu_get_real_ticks();
624 if (cpu_ticks_prev > ticks) {
625 /* Note: non increasing ticks may happen if the host uses
626 software suspend */
627 cpu_ticks_offset += cpu_ticks_prev - ticks;
628 }
629 cpu_ticks_prev = ticks;
630 return ticks + cpu_ticks_offset;
631 }
632}
633
634/* return the host CPU monotonic timer and handle stop/restart */
635static int64_t cpu_get_clock(void)
636{
637 int64_t ti;
638 if (!cpu_ticks_enabled) {
639 return cpu_clock_offset;
640 } else {
641 ti = get_clock();
642 return ti + cpu_clock_offset;
643 }
644}
645
646/* enable cpu_get_ticks() */
647void cpu_enable_ticks(void)
648{
649 if (!cpu_ticks_enabled) {
650 cpu_ticks_offset -= cpu_get_real_ticks();
651 cpu_clock_offset -= get_clock();
652 cpu_ticks_enabled = 1;
653 }
654}
655
656/* disable cpu_get_ticks() : the clock is stopped. You must not call
657 cpu_get_ticks() after that. */
658void cpu_disable_ticks(void)
659{
660 if (cpu_ticks_enabled) {
661 cpu_ticks_offset = cpu_get_ticks();
662 cpu_clock_offset = cpu_get_clock();
663 cpu_ticks_enabled = 0;
664 }
665}
666
667/***********************************************************/
668/* timers */
ths5fafdf22007-09-16 21:08:06 +0000669
bellard8a7ddc32004-03-31 19:00:16 +0000670#define QEMU_TIMER_REALTIME 0
671#define QEMU_TIMER_VIRTUAL 1
672
673struct QEMUClock {
674 int type;
675 /* XXX: add frequency */
676};
677
678struct QEMUTimer {
679 QEMUClock *clock;
680 int64_t expire_time;
681 QEMUTimerCB *cb;
682 void *opaque;
683 struct QEMUTimer *next;
684};
685
thsc8994012007-08-19 21:56:03 +0000686struct qemu_alarm_timer {
687 char const *name;
thsefe75412007-08-24 01:36:32 +0000688 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000689
690 int (*start)(struct qemu_alarm_timer *t);
691 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000692 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000693 void *priv;
694};
695
thsefe75412007-08-24 01:36:32 +0000696#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000697#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000698
699static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
700{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200701 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000702}
703
704static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
705{
706 if (!alarm_has_dynticks(t))
707 return;
708
709 t->rearm(t);
710}
711
712/* TODO: MIN_TIMER_REARM_US should be optimized */
713#define MIN_TIMER_REARM_US 250
714
thsc8994012007-08-19 21:56:03 +0000715static struct qemu_alarm_timer *alarm_timer;
716
717#ifdef _WIN32
718
719struct qemu_alarm_win32 {
720 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000721 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000722} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000723
724static int win32_start_timer(struct qemu_alarm_timer *t);
725static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000726static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000727
728#else
729
730static int unix_start_timer(struct qemu_alarm_timer *t);
731static void unix_stop_timer(struct qemu_alarm_timer *t);
732
ths231c6582007-08-26 17:29:15 +0000733#ifdef __linux__
734
thsefe75412007-08-24 01:36:32 +0000735static int dynticks_start_timer(struct qemu_alarm_timer *t);
736static void dynticks_stop_timer(struct qemu_alarm_timer *t);
737static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
738
thsc40ec5a2007-08-19 22:09:40 +0000739static int hpet_start_timer(struct qemu_alarm_timer *t);
740static void hpet_stop_timer(struct qemu_alarm_timer *t);
741
thsc8994012007-08-19 21:56:03 +0000742static int rtc_start_timer(struct qemu_alarm_timer *t);
743static void rtc_stop_timer(struct qemu_alarm_timer *t);
744
thsefe75412007-08-24 01:36:32 +0000745#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000746
747#endif /* _WIN32 */
748
pbrook2e70f6e2008-06-29 01:03:05 +0000749/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000750 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000751 When the guest is idle real and virtual time will be aligned in
752 the IO wait loop. */
753#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
754
755static void icount_adjust(void)
756{
757 int64_t cur_time;
758 int64_t cur_icount;
759 int64_t delta;
760 static int64_t last_delta;
761 /* If the VM is not running, then do nothing. */
762 if (!vm_running)
763 return;
764
765 cur_time = cpu_get_clock();
766 cur_icount = qemu_get_clock(vm_clock);
767 delta = cur_icount - cur_time;
768 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
769 if (delta > 0
770 && last_delta + ICOUNT_WOBBLE < delta * 2
771 && icount_time_shift > 0) {
772 /* The guest is getting too far ahead. Slow time down. */
773 icount_time_shift--;
774 }
775 if (delta < 0
776 && last_delta - ICOUNT_WOBBLE > delta * 2
777 && icount_time_shift < MAX_ICOUNT_SHIFT) {
778 /* The guest is getting too far behind. Speed time up. */
779 icount_time_shift++;
780 }
781 last_delta = delta;
782 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
783}
784
785static void icount_adjust_rt(void * opaque)
786{
787 qemu_mod_timer(icount_rt_timer,
788 qemu_get_clock(rt_clock) + 1000);
789 icount_adjust();
790}
791
792static void icount_adjust_vm(void * opaque)
793{
794 qemu_mod_timer(icount_vm_timer,
795 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
796 icount_adjust();
797}
798
799static void init_icount_adjust(void)
800{
801 /* Have both realtime and virtual time triggers for speed adjustment.
802 The realtime trigger catches emulated time passing too slowly,
803 the virtual time trigger catches emulated time passing too fast.
804 Realtime triggers occur even when idle, so use them less frequently
805 than VM triggers. */
806 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
807 qemu_mod_timer(icount_rt_timer,
808 qemu_get_clock(rt_clock) + 1000);
809 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
810 qemu_mod_timer(icount_vm_timer,
811 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
812}
813
thsc8994012007-08-19 21:56:03 +0000814static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000815#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000816#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000817 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
818 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000819 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000820 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000821 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000822 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000823#endif
thsefe75412007-08-24 01:36:32 +0000824 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000825#else
thsefe75412007-08-24 01:36:32 +0000826 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
827 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
828 {"win32", 0, win32_start_timer,
829 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000830#endif
831 {NULL, }
832};
833
blueswir13f47aa82008-03-09 06:59:01 +0000834static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000835{
836 int i;
837
838 printf("Available alarm timers, in order of precedence:\n");
839 for (i = 0; alarm_timers[i].name; i++)
840 printf("%s\n", alarm_timers[i].name);
841}
842
843static void configure_alarms(char const *opt)
844{
845 int i;
846 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000847 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000848 char *arg;
849 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000850 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000851
aurel323adda042008-03-09 23:43:49 +0000852 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000853 show_available_alarms();
854 exit(0);
855 }
856
857 arg = strdup(opt);
858
859 /* Reorder the array */
860 name = strtok(arg, ",");
861 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000862 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000863 if (!strcmp(alarm_timers[i].name, name))
864 break;
865 }
866
867 if (i == count) {
868 fprintf(stderr, "Unknown clock %s\n", name);
869 goto next;
870 }
871
872 if (i < cur)
873 /* Ignore */
874 goto next;
875
876 /* Swap */
877 tmp = alarm_timers[i];
878 alarm_timers[i] = alarm_timers[cur];
879 alarm_timers[cur] = tmp;
880
881 cur++;
882next:
883 name = strtok(NULL, ",");
884 }
885
886 free(arg);
887
888 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000889 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000890 for (i = cur; i < count; i++)
891 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000892 } else {
893 show_available_alarms();
894 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000895 }
thsf3dcfad2007-08-24 01:26:02 +0000896}
897
bellard8a7ddc32004-03-31 19:00:16 +0000898QEMUClock *rt_clock;
899QEMUClock *vm_clock;
900
901static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +0000902
pbrook9596ebb2007-11-18 01:44:38 +0000903static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000904{
905 QEMUClock *clock;
906 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +0000907 clock->type = type;
908 return clock;
909}
910
911QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
912{
913 QEMUTimer *ts;
914
915 ts = qemu_mallocz(sizeof(QEMUTimer));
916 ts->clock = clock;
917 ts->cb = cb;
918 ts->opaque = opaque;
919 return ts;
920}
921
922void qemu_free_timer(QEMUTimer *ts)
923{
924 qemu_free(ts);
925}
926
927/* stop a timer, but do not dealloc it */
928void qemu_del_timer(QEMUTimer *ts)
929{
930 QEMUTimer **pt, *t;
931
932 /* NOTE: this code must be signal safe because
933 qemu_timer_expired() can be called from a signal. */
934 pt = &active_timers[ts->clock->type];
935 for(;;) {
936 t = *pt;
937 if (!t)
938 break;
939 if (t == ts) {
940 *pt = t->next;
941 break;
942 }
943 pt = &t->next;
944 }
945}
946
947/* modify the current timer so that it will be fired when current_time
948 >= expire_time. The corresponding callback will be called. */
949void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
950{
951 QEMUTimer **pt, *t;
952
953 qemu_del_timer(ts);
954
955 /* add the timer in the sorted list */
956 /* NOTE: this code must be signal safe because
957 qemu_timer_expired() can be called from a signal. */
958 pt = &active_timers[ts->clock->type];
959 for(;;) {
960 t = *pt;
961 if (!t)
962 break;
ths5fafdf22007-09-16 21:08:06 +0000963 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +0000964 break;
965 pt = &t->next;
966 }
967 ts->expire_time = expire_time;
968 ts->next = *pt;
969 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +0000970
971 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +0000972 if (pt == &active_timers[ts->clock->type]) {
973 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
974 qemu_rearm_alarm_timer(alarm_timer);
975 }
976 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +0000977 if (use_icount)
978 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +0000979 }
bellard8a7ddc32004-03-31 19:00:16 +0000980}
981
982int qemu_timer_pending(QEMUTimer *ts)
983{
984 QEMUTimer *t;
985 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
986 if (t == ts)
987 return 1;
988 }
989 return 0;
990}
991
Stefano Stabellini2430ffe2009-08-03 10:56:01 +0100992int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
bellard8a7ddc32004-03-31 19:00:16 +0000993{
994 if (!timer_head)
995 return 0;
996 return (timer_head->expire_time <= current_time);
997}
998
999static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1000{
1001 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001002
bellard8a7ddc32004-03-31 19:00:16 +00001003 for(;;) {
1004 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001005 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001006 break;
1007 /* remove timer from the list before calling the callback */
1008 *ptimer_head = ts->next;
1009 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001010
bellard8a7ddc32004-03-31 19:00:16 +00001011 /* run the callback (the timer list can be modified) */
1012 ts->cb(ts->opaque);
1013 }
1014}
1015
1016int64_t qemu_get_clock(QEMUClock *clock)
1017{
1018 switch(clock->type) {
1019 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001020 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001021 default:
1022 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001023 if (use_icount) {
1024 return cpu_get_icount();
1025 } else {
1026 return cpu_get_clock();
1027 }
bellard8a7ddc32004-03-31 19:00:16 +00001028 }
1029}
1030
bellard1dce7c32006-07-13 23:20:22 +00001031static void init_timers(void)
1032{
1033 init_get_clock();
1034 ticks_per_sec = QEMU_TIMER_BASE;
1035 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1036 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1037}
1038
bellard8a7ddc32004-03-31 19:00:16 +00001039/* save a timer */
1040void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1041{
1042 uint64_t expire_time;
1043
1044 if (qemu_timer_pending(ts)) {
1045 expire_time = ts->expire_time;
1046 } else {
1047 expire_time = -1;
1048 }
1049 qemu_put_be64(f, expire_time);
1050}
1051
1052void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1053{
1054 uint64_t expire_time;
1055
1056 expire_time = qemu_get_be64(f);
1057 if (expire_time != -1) {
1058 qemu_mod_timer(ts, expire_time);
1059 } else {
1060 qemu_del_timer(ts);
1061 }
1062}
1063
1064static void timer_save(QEMUFile *f, void *opaque)
1065{
1066 if (cpu_ticks_enabled) {
1067 hw_error("cannot save state if virtual timers are running");
1068 }
thsbee8d682007-12-16 23:41:11 +00001069 qemu_put_be64(f, cpu_ticks_offset);
1070 qemu_put_be64(f, ticks_per_sec);
1071 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001072}
1073
1074static int timer_load(QEMUFile *f, void *opaque, int version_id)
1075{
bellardc88676f2006-08-06 13:36:11 +00001076 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001077 return -EINVAL;
1078 if (cpu_ticks_enabled) {
1079 return -EINVAL;
1080 }
thsbee8d682007-12-16 23:41:11 +00001081 cpu_ticks_offset=qemu_get_be64(f);
1082 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001083 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001084 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001085 }
bellard8a7ddc32004-03-31 19:00:16 +00001086 return 0;
1087}
1088
aliguori50317c72009-04-24 18:03:29 +00001089static void qemu_event_increment(void);
1090
bellard67b915a2004-03-31 23:37:16 +00001091#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001092static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1093 DWORD_PTR dwUser, DWORD_PTR dw1,
1094 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001095#else
bellard8a7ddc32004-03-31 19:00:16 +00001096static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001097#endif
bellard8a7ddc32004-03-31 19:00:16 +00001098{
bellard02ba45c2004-06-25 14:46:23 +00001099#if 0
1100#define DISP_FREQ 1000
1101 {
1102 static int64_t delta_min = INT64_MAX;
1103 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1104 static int count;
1105 ti = qemu_get_clock(vm_clock);
1106 if (last_clock != 0) {
1107 delta = ti - last_clock;
1108 if (delta < delta_min)
1109 delta_min = delta;
1110 if (delta > delta_max)
1111 delta_max = delta;
1112 delta_cum += delta;
1113 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001114 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001115 muldiv64(delta_min, 1000000, ticks_per_sec),
1116 muldiv64(delta_max, 1000000, ticks_per_sec),
1117 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1118 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1119 count = 0;
1120 delta_min = INT64_MAX;
1121 delta_max = 0;
1122 delta_cum = 0;
1123 }
1124 }
1125 last_clock = ti;
1126 }
1127#endif
thsefe75412007-08-24 01:36:32 +00001128 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001129 (!use_icount &&
1130 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1131 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001132 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1133 qemu_get_clock(rt_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001134 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001135 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001136
aliguorid6dc3d42009-04-24 18:04:07 +00001137#ifndef CONFIG_IOTHREAD
1138 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001139 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001140 cpu_exit(next_cpu);
blueswir1640f42e2009-04-19 10:18:01 +00001141#ifdef CONFIG_KQEMU
aliguorid6dc3d42009-04-24 18:04:07 +00001142 if (next_cpu->kqemu_enabled) {
1143 kqemu_cpu_interrupt(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001144 }
balrogee5605e2007-12-03 03:01:40 +00001145#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001146 }
aliguorid6dc3d42009-04-24 18:04:07 +00001147#endif
aliguori43b96852009-04-24 18:03:33 +00001148 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001149 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001150 }
1151}
1152
pbrook2e70f6e2008-06-29 01:03:05 +00001153static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001154{
pbrook2e70f6e2008-06-29 01:03:05 +00001155 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001156
1157 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001158 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1159 qemu_get_clock(vm_clock);
1160 } else {
1161 /* To avoid problems with overflow limit this to 2^32. */
1162 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001163 }
1164
pbrook2e70f6e2008-06-29 01:03:05 +00001165 if (delta < 0)
1166 delta = 0;
thsefe75412007-08-24 01:36:32 +00001167
pbrook2e70f6e2008-06-29 01:03:05 +00001168 return delta;
1169}
1170
blueswir18632fb92008-09-14 13:59:34 +00001171#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001172static uint64_t qemu_next_deadline_dyntick(void)
1173{
1174 int64_t delta;
1175 int64_t rtdelta;
1176
1177 if (use_icount)
1178 delta = INT32_MAX;
1179 else
1180 delta = (qemu_next_deadline() + 999) / 1000;
1181
1182 if (active_timers[QEMU_TIMER_REALTIME]) {
1183 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1184 qemu_get_clock(rt_clock))*1000;
1185 if (rtdelta < delta)
1186 delta = rtdelta;
1187 }
1188
1189 if (delta < MIN_TIMER_REARM_US)
1190 delta = MIN_TIMER_REARM_US;
1191
1192 return delta;
thsefe75412007-08-24 01:36:32 +00001193}
blueswir18632fb92008-09-14 13:59:34 +00001194#endif
thsefe75412007-08-24 01:36:32 +00001195
bellardfd872592004-05-12 19:11:15 +00001196#ifndef _WIN32
1197
aliguori7183b4b2008-11-05 20:40:18 +00001198/* Sets a specific flag */
1199static int fcntl_setfl(int fd, int flag)
1200{
1201 int flags;
1202
1203 flags = fcntl(fd, F_GETFL);
1204 if (flags == -1)
1205 return -errno;
1206
1207 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1208 return -errno;
1209
1210 return 0;
1211}
1212
bellard829309c2004-05-20 13:20:12 +00001213#if defined(__linux__)
1214
bellardfd872592004-05-12 19:11:15 +00001215#define RTC_FREQ 1024
1216
aurel32de9a95f2008-11-11 13:41:01 +00001217static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001218{
thsc8994012007-08-19 21:56:03 +00001219 struct sigaction act;
1220
1221 /* timer signal */
1222 sigfillset(&act.sa_mask);
1223 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001224 act.sa_handler = host_alarm_handler;
1225
1226 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001227 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001228 fcntl(fd, F_SETOWN, getpid());
1229}
1230
thsc40ec5a2007-08-19 22:09:40 +00001231static int hpet_start_timer(struct qemu_alarm_timer *t)
1232{
1233 struct hpet_info info;
1234 int r, fd;
1235
1236 fd = open("/dev/hpet", O_RDONLY);
1237 if (fd < 0)
1238 return -1;
1239
1240 /* Set frequency */
1241 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1242 if (r < 0) {
1243 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1244 "error, but for better emulation accuracy type:\n"
1245 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1246 goto fail;
1247 }
1248
1249 /* Check capabilities */
1250 r = ioctl(fd, HPET_INFO, &info);
1251 if (r < 0)
1252 goto fail;
1253
1254 /* Enable periodic mode */
1255 r = ioctl(fd, HPET_EPI, 0);
1256 if (info.hi_flags && (r < 0))
1257 goto fail;
1258
1259 /* Enable interrupt */
1260 r = ioctl(fd, HPET_IE_ON, 0);
1261 if (r < 0)
1262 goto fail;
1263
1264 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001265 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001266
1267 return 0;
1268fail:
1269 close(fd);
1270 return -1;
1271}
1272
1273static void hpet_stop_timer(struct qemu_alarm_timer *t)
1274{
pbrookfcdc2122007-08-23 20:22:22 +00001275 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001276
1277 close(fd);
1278}
1279
thsc8994012007-08-19 21:56:03 +00001280static int rtc_start_timer(struct qemu_alarm_timer *t)
1281{
1282 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001283 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001284
balrogaeb30be2007-07-02 15:03:13 +00001285 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001286 if (rtc_fd < 0)
1287 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001288 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1289 if (current_rtc_freq != RTC_FREQ &&
1290 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001291 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1292 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1293 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1294 goto fail;
1295 }
1296 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1297 fail:
1298 close(rtc_fd);
1299 return -1;
1300 }
thsc8994012007-08-19 21:56:03 +00001301
1302 enable_sigio_timer(rtc_fd);
1303
pbrookfcdc2122007-08-23 20:22:22 +00001304 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001305
bellardfd872592004-05-12 19:11:15 +00001306 return 0;
1307}
1308
thsc8994012007-08-19 21:56:03 +00001309static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001310{
pbrookfcdc2122007-08-23 20:22:22 +00001311 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001312
1313 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001314}
1315
thsefe75412007-08-24 01:36:32 +00001316static int dynticks_start_timer(struct qemu_alarm_timer *t)
1317{
1318 struct sigevent ev;
1319 timer_t host_timer;
1320 struct sigaction act;
1321
1322 sigfillset(&act.sa_mask);
1323 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001324 act.sa_handler = host_alarm_handler;
1325
1326 sigaction(SIGALRM, &act, NULL);
1327
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001328 /*
1329 * Initialize ev struct to 0 to avoid valgrind complaining
1330 * about uninitialized data in timer_create call
1331 */
1332 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001333 ev.sigev_value.sival_int = 0;
1334 ev.sigev_notify = SIGEV_SIGNAL;
1335 ev.sigev_signo = SIGALRM;
1336
1337 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1338 perror("timer_create");
1339
1340 /* disable dynticks */
1341 fprintf(stderr, "Dynamic Ticks disabled\n");
1342
1343 return -1;
1344 }
1345
blueswir10399bfe2008-11-16 11:37:18 +00001346 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001347
1348 return 0;
1349}
1350
1351static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1352{
blueswir10399bfe2008-11-16 11:37:18 +00001353 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001354
1355 timer_delete(host_timer);
1356}
1357
1358static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1359{
blueswir10399bfe2008-11-16 11:37:18 +00001360 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001361 struct itimerspec timeout;
1362 int64_t nearest_delta_us = INT64_MAX;
1363 int64_t current_us;
1364
1365 if (!active_timers[QEMU_TIMER_REALTIME] &&
1366 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001367 return;
thsefe75412007-08-24 01:36:32 +00001368
pbrook2e70f6e2008-06-29 01:03:05 +00001369 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001370
1371 /* check whether a timer is already running */
1372 if (timer_gettime(host_timer, &timeout)) {
1373 perror("gettime");
1374 fprintf(stderr, "Internal timer error: aborting\n");
1375 exit(1);
1376 }
1377 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1378 if (current_us && current_us <= nearest_delta_us)
1379 return;
1380
1381 timeout.it_interval.tv_sec = 0;
1382 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1383 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1384 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1385 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1386 perror("settime");
1387 fprintf(stderr, "Internal timer error: aborting\n");
1388 exit(1);
1389 }
1390}
1391
ths70744b32007-08-26 17:31:30 +00001392#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001393
thsc8994012007-08-19 21:56:03 +00001394static int unix_start_timer(struct qemu_alarm_timer *t)
1395{
1396 struct sigaction act;
1397 struct itimerval itv;
1398 int err;
1399
1400 /* timer signal */
1401 sigfillset(&act.sa_mask);
1402 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001403 act.sa_handler = host_alarm_handler;
1404
1405 sigaction(SIGALRM, &act, NULL);
1406
1407 itv.it_interval.tv_sec = 0;
1408 /* for i386 kernel 2.6 to get 1 ms */
1409 itv.it_interval.tv_usec = 999;
1410 itv.it_value.tv_sec = 0;
1411 itv.it_value.tv_usec = 10 * 1000;
1412
1413 err = setitimer(ITIMER_REAL, &itv, NULL);
1414 if (err)
1415 return -1;
1416
1417 return 0;
1418}
1419
1420static void unix_stop_timer(struct qemu_alarm_timer *t)
1421{
1422 struct itimerval itv;
1423
1424 memset(&itv, 0, sizeof(itv));
1425 setitimer(ITIMER_REAL, &itv, NULL);
1426}
1427
bellard829309c2004-05-20 13:20:12 +00001428#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001429
aliguorif49e58d2008-11-05 21:22:34 +00001430
thsc8994012007-08-19 21:56:03 +00001431#ifdef _WIN32
1432
1433static int win32_start_timer(struct qemu_alarm_timer *t)
1434{
1435 TIMECAPS tc;
1436 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001437 UINT flags;
thsc8994012007-08-19 21:56:03 +00001438
thsc8994012007-08-19 21:56:03 +00001439 memset(&tc, 0, sizeof(tc));
1440 timeGetDevCaps(&tc, sizeof(tc));
1441
1442 if (data->period < tc.wPeriodMin)
1443 data->period = tc.wPeriodMin;
1444
1445 timeBeginPeriod(data->period);
1446
thsefe75412007-08-24 01:36:32 +00001447 flags = TIME_CALLBACK_FUNCTION;
1448 if (alarm_has_dynticks(t))
1449 flags |= TIME_ONESHOT;
1450 else
1451 flags |= TIME_PERIODIC;
1452
thsc8994012007-08-19 21:56:03 +00001453 data->timerId = timeSetEvent(1, // interval (ms)
1454 data->period, // resolution
1455 host_alarm_handler, // function
1456 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001457 flags);
thsc8994012007-08-19 21:56:03 +00001458
1459 if (!data->timerId) {
1460 perror("Failed to initialize win32 alarm timer");
thsc8994012007-08-19 21:56:03 +00001461 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001462 return -1;
1463 }
1464
thsc8994012007-08-19 21:56:03 +00001465 return 0;
1466}
1467
1468static void win32_stop_timer(struct qemu_alarm_timer *t)
1469{
1470 struct qemu_alarm_win32 *data = t->priv;
1471
1472 timeKillEvent(data->timerId);
1473 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001474}
1475
thsefe75412007-08-24 01:36:32 +00001476static void win32_rearm_timer(struct qemu_alarm_timer *t)
1477{
1478 struct qemu_alarm_win32 *data = t->priv;
1479 uint64_t nearest_delta_us;
1480
1481 if (!active_timers[QEMU_TIMER_REALTIME] &&
1482 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001483 return;
thsefe75412007-08-24 01:36:32 +00001484
pbrook2e70f6e2008-06-29 01:03:05 +00001485 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001486 nearest_delta_us /= 1000;
1487
1488 timeKillEvent(data->timerId);
1489
1490 data->timerId = timeSetEvent(1,
1491 data->period,
1492 host_alarm_handler,
1493 (DWORD)t,
1494 TIME_ONESHOT | TIME_PERIODIC);
1495
1496 if (!data->timerId) {
1497 perror("Failed to re-arm win32 alarm timer");
1498
1499 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001500 exit(1);
1501 }
1502}
1503
thsc8994012007-08-19 21:56:03 +00001504#endif /* _WIN32 */
1505
aliguori7183b4b2008-11-05 20:40:18 +00001506static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001507{
blueswir1223f0d72008-09-30 18:12:18 +00001508 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001509 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001510
thsc8994012007-08-19 21:56:03 +00001511 for (i = 0; alarm_timers[i].name; i++) {
1512 t = &alarm_timers[i];
1513
thsc8994012007-08-19 21:56:03 +00001514 err = t->start(t);
1515 if (!err)
1516 break;
bellard67b915a2004-03-31 23:37:16 +00001517 }
bellardfd872592004-05-12 19:11:15 +00001518
thsc8994012007-08-19 21:56:03 +00001519 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001520 err = -ENOENT;
1521 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001522 }
thsc8994012007-08-19 21:56:03 +00001523
1524 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001525
aliguori6abfbd72008-11-05 20:49:37 +00001526 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001527
1528fail:
aliguori7183b4b2008-11-05 20:40:18 +00001529 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001530}
1531
pbrook9596ebb2007-11-18 01:44:38 +00001532static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001533{
thsc8994012007-08-19 21:56:03 +00001534 alarm_timer->stop(alarm_timer);
1535 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001536}
1537
bellardc4b1fcc2004-03-14 21:44:30 +00001538/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001539/* host time/date access */
1540void qemu_get_timedate(struct tm *tm, int offset)
1541{
1542 time_t ti;
1543 struct tm *ret;
1544
1545 time(&ti);
1546 ti += offset;
1547 if (rtc_date_offset == -1) {
1548 if (rtc_utc)
1549 ret = gmtime(&ti);
1550 else
1551 ret = localtime(&ti);
1552 } else {
1553 ti -= rtc_date_offset;
1554 ret = gmtime(&ti);
1555 }
1556
1557 memcpy(tm, ret, sizeof(struct tm));
1558}
1559
1560int qemu_timedate_diff(struct tm *tm)
1561{
1562 time_t seconds;
1563
1564 if (rtc_date_offset == -1)
1565 if (rtc_utc)
1566 seconds = mktimegm(tm);
1567 else
1568 seconds = mktime(tm);
1569 else
1570 seconds = mktimegm(tm) + rtc_date_offset;
1571
1572 return seconds - time(NULL);
1573}
1574
bellardfd1dff42006-02-01 21:29:26 +00001575#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001576static void socket_cleanup(void)
1577{
1578 WSACleanup();
1579}
bellard82c643f2004-07-14 17:28:13 +00001580
bellardfd1dff42006-02-01 21:29:26 +00001581static int socket_init(void)
1582{
1583 WSADATA Data;
1584 int ret, err;
1585
1586 ret = WSAStartup(MAKEWORD(2,2), &Data);
1587 if (ret != 0) {
1588 err = WSAGetLastError();
1589 fprintf(stderr, "WSAStartup: %d\n", err);
1590 return -1;
1591 }
1592 atexit(socket_cleanup);
1593 return 0;
1594}
aurel3264b7b732008-05-05 10:05:31 +00001595#endif
1596
balrog1ae26a12008-09-28 23:19:47 +00001597/***********************************************************/
1598/* Bluetooth support */
1599static int nb_hcis;
1600static int cur_hci;
1601static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001602
balrog1ae26a12008-09-28 23:19:47 +00001603static struct bt_vlan_s {
1604 struct bt_scatternet_s net;
1605 int id;
1606 struct bt_vlan_s *next;
1607} *first_bt_vlan;
1608
1609/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001610static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001611{
1612 struct bt_vlan_s **pvlan, *vlan;
1613 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1614 if (vlan->id == id)
1615 return &vlan->net;
1616 }
1617 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1618 vlan->id = id;
1619 pvlan = &first_bt_vlan;
1620 while (*pvlan != NULL)
1621 pvlan = &(*pvlan)->next;
1622 *pvlan = vlan;
1623 return &vlan->net;
1624}
1625
1626static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1627{
1628}
1629
1630static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1631{
1632 return -ENOTSUP;
1633}
1634
1635static struct HCIInfo null_hci = {
1636 .cmd_send = null_hci_send,
1637 .sco_send = null_hci_send,
1638 .acl_send = null_hci_send,
1639 .bdaddr_set = null_hci_addr_set,
1640};
1641
1642struct HCIInfo *qemu_next_hci(void)
1643{
1644 if (cur_hci == nb_hcis)
1645 return &null_hci;
1646
1647 return hci_table[cur_hci++];
1648}
1649
balrogdc72ac12008-11-09 00:04:26 +00001650static struct HCIInfo *hci_init(const char *str)
1651{
1652 char *endp;
1653 struct bt_scatternet_s *vlan = 0;
1654
1655 if (!strcmp(str, "null"))
1656 /* null */
1657 return &null_hci;
1658 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1659 /* host[:hciN] */
1660 return bt_host_hci(str[4] ? str + 5 : "hci0");
1661 else if (!strncmp(str, "hci", 3)) {
1662 /* hci[,vlan=n] */
1663 if (str[3]) {
1664 if (!strncmp(str + 3, ",vlan=", 6)) {
1665 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1666 if (*endp)
1667 vlan = 0;
1668 }
1669 } else
1670 vlan = qemu_find_bt_vlan(0);
1671 if (vlan)
1672 return bt_new_hci(vlan);
1673 }
1674
1675 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1676
1677 return 0;
1678}
1679
1680static int bt_hci_parse(const char *str)
1681{
1682 struct HCIInfo *hci;
1683 bdaddr_t bdaddr;
1684
1685 if (nb_hcis >= MAX_NICS) {
1686 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1687 return -1;
1688 }
1689
1690 hci = hci_init(str);
1691 if (!hci)
1692 return -1;
1693
1694 bdaddr.b[0] = 0x52;
1695 bdaddr.b[1] = 0x54;
1696 bdaddr.b[2] = 0x00;
1697 bdaddr.b[3] = 0x12;
1698 bdaddr.b[4] = 0x34;
1699 bdaddr.b[5] = 0x56 + nb_hcis;
1700 hci->bdaddr_set(hci, bdaddr.b);
1701
1702 hci_table[nb_hcis++] = hci;
1703
1704 return 0;
1705}
1706
1707static void bt_vhci_add(int vlan_id)
1708{
1709 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1710
1711 if (!vlan->slave)
1712 fprintf(stderr, "qemu: warning: adding a VHCI to "
1713 "an empty scatternet %i\n", vlan_id);
1714
1715 bt_vhci_init(bt_new_hci(vlan));
1716}
1717
1718static struct bt_device_s *bt_device_add(const char *opt)
1719{
1720 struct bt_scatternet_s *vlan;
1721 int vlan_id = 0;
1722 char *endp = strstr(opt, ",vlan=");
1723 int len = (endp ? endp - opt : strlen(opt)) + 1;
1724 char devname[10];
1725
1726 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1727
1728 if (endp) {
1729 vlan_id = strtol(endp + 6, &endp, 0);
1730 if (*endp) {
1731 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1732 return 0;
1733 }
1734 }
1735
1736 vlan = qemu_find_bt_vlan(vlan_id);
1737
1738 if (!vlan->slave)
1739 fprintf(stderr, "qemu: warning: adding a slave device to "
1740 "an empty scatternet %i\n", vlan_id);
1741
1742 if (!strcmp(devname, "keyboard"))
1743 return bt_keyboard_init(vlan);
1744
1745 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1746 return 0;
1747}
1748
1749static int bt_parse(const char *opt)
1750{
1751 const char *endp, *p;
1752 int vlan;
1753
1754 if (strstart(opt, "hci", &endp)) {
1755 if (!*endp || *endp == ',') {
1756 if (*endp)
1757 if (!strstart(endp, ",vlan=", 0))
1758 opt = endp + 1;
1759
1760 return bt_hci_parse(opt);
1761 }
1762 } else if (strstart(opt, "vhci", &endp)) {
1763 if (!*endp || *endp == ',') {
1764 if (*endp) {
1765 if (strstart(endp, ",vlan=", &p)) {
1766 vlan = strtol(p, (char **) &endp, 0);
1767 if (*endp) {
1768 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1769 return 1;
1770 }
1771 } else {
1772 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1773 return 1;
1774 }
1775 } else
1776 vlan = 0;
1777
1778 bt_vhci_add(vlan);
1779 return 0;
1780 }
1781 } else if (strstart(opt, "device:", &endp))
1782 return !bt_device_add(endp);
1783
1784 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1785 return 1;
1786}
1787
balrog1ae26a12008-09-28 23:19:47 +00001788/***********************************************************/
1789/* QEMU Block devices */
1790
balrog609497a2008-01-14 02:56:53 +00001791#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001792#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001793#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001794#define PFLASH_ALIAS "if=pflash"
1795#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001796#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001797
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001798QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001799{
1800 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001801 char optstr[1024];
1802 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +00001803
thse4bcb142007-12-02 04:51:10 +00001804 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001805 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001806 va_end(ap);
1807
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001808 opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001809 if (!opts) {
1810 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1811 __FUNCTION__, optstr);
1812 return NULL;
1813 }
1814 if (file)
1815 qemu_opt_set(opts, "file", file);
1816 return opts;
aliguorib01b1112009-02-11 15:20:20 +00001817}
1818
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001819DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001820{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001821 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001822
1823 /* seek interface, bus and unit */
1824
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001825 TAILQ_FOREACH(dinfo, &drives, next) {
1826 if (dinfo->type == type &&
1827 dinfo->bus == bus &&
1828 dinfo->unit == unit)
1829 return dinfo;
1830 }
thse4bcb142007-12-02 04:51:10 +00001831
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001832 return NULL;
thse4bcb142007-12-02 04:51:10 +00001833}
1834
Gerd Hoffmann2e810b32009-07-31 12:25:38 +02001835DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001836{
1837 DriveInfo *dinfo;
1838
1839 TAILQ_FOREACH(dinfo, &drives, next) {
1840 if (strcmp(id, dinfo->id))
1841 continue;
1842 return dinfo;
1843 }
1844 return NULL;
1845}
1846
thsf60d39b2007-12-17 03:55:57 +00001847int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001848{
1849 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001850 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001851
1852 max_bus = -1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001853 TAILQ_FOREACH(dinfo, &drives, next) {
1854 if(dinfo->type == type &&
1855 dinfo->bus > max_bus)
1856 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +00001857 }
1858 return max_bus;
1859}
1860
aliguorifa879c62009-01-07 17:32:33 +00001861const char *drive_get_serial(BlockDriverState *bdrv)
1862{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001863 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +00001864
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001865 TAILQ_FOREACH(dinfo, &drives, next) {
1866 if (dinfo->bdrv == bdrv)
1867 return dinfo->serial;
1868 }
aliguorifa879c62009-01-07 17:32:33 +00001869
1870 return "\0";
1871}
1872
aliguori428c5702009-01-21 18:59:04 +00001873BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1874{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001875 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +00001876
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001877 TAILQ_FOREACH(dinfo, &drives, next) {
1878 if (dinfo->bdrv == bdrv)
1879 return dinfo->onerror;
1880 }
aliguori428c5702009-01-21 18:59:04 +00001881
aliguoricdad4bd2009-02-28 16:51:01 +00001882 return BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00001883}
1884
aurel32a1620fa2008-04-29 05:58:01 +00001885static void bdrv_format_print(void *opaque, const char *name)
1886{
1887 fprintf(stderr, " %s", name);
1888}
1889
aliguorib01b1112009-02-11 15:20:20 +00001890void drive_uninit(BlockDriverState *bdrv)
1891{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001892 DriveInfo *dinfo;
aliguorib01b1112009-02-11 15:20:20 +00001893
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001894 TAILQ_FOREACH(dinfo, &drives, next) {
1895 if (dinfo->bdrv != bdrv)
1896 continue;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001897 qemu_opts_del(dinfo->opts);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001898 TAILQ_REMOVE(&drives, dinfo, next);
1899 qemu_free(dinfo);
1900 break;
1901 }
aliguorib01b1112009-02-11 15:20:20 +00001902}
1903
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001904DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001905 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +00001906{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001907 const char *buf;
1908 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +00001909 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001910 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +00001911 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00001912 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00001913 enum { MEDIA_DISK, MEDIA_CDROM } media;
1914 int bus_id, unit_id;
1915 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +00001916 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00001917 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00001918 int max_devs;
1919 int index;
balrog33f00272007-12-24 14:33:24 +00001920 int cache;
aliguori428c5702009-01-21 18:59:04 +00001921 int bdrv_flags, onerror;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001922 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001923 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001924 int snapshot = 0;
1925
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001926 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +00001927
thse4bcb142007-12-02 04:51:10 +00001928 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02001929 cache = 1;
thse4bcb142007-12-02 04:51:10 +00001930
blueswir1c9b1ae22008-09-28 18:55:17 +00001931 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00001932 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00001933 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00001934 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00001935 } else {
thsf60d39b2007-12-17 03:55:57 +00001936 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00001937 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00001938 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00001939 }
1940 media = MEDIA_DISK;
1941
1942 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001943 bus_id = qemu_opt_get_number(opts, "bus", 0);
1944 unit_id = qemu_opt_get_number(opts, "unit", -1);
1945 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +00001946
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001947 cyls = qemu_opt_get_number(opts, "cyls", 0);
1948 heads = qemu_opt_get_number(opts, "heads", 0);
1949 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +00001950
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001951 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
thse4bcb142007-12-02 04:51:10 +00001952
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001953 file = qemu_opt_get(opts, "file");
1954 serial = qemu_opt_get(opts, "serial");
1955
1956 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +00001957 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00001958 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00001959 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00001960 max_devs = MAX_IDE_DEVS;
1961 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00001962 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00001963 max_devs = MAX_SCSI_DEVS;
1964 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00001965 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00001966 max_devs = 0;
1967 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00001968 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00001969 max_devs = 0;
1970 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00001971 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00001972 max_devs = 0;
1973 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00001974 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00001975 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00001976 } else if (!strcmp(buf, "virtio")) {
1977 type = IF_VIRTIO;
1978 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00001979 } else if (!strcmp(buf, "xen")) {
1980 type = IF_XEN;
1981 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +02001982 } else if (!strcmp(buf, "none")) {
1983 type = IF_NONE;
1984 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00001985 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001986 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001987 return NULL;
thse4bcb142007-12-02 04:51:10 +00001988 }
1989 }
1990
thse4bcb142007-12-02 04:51:10 +00001991 if (cyls || heads || secs) {
1992 if (cyls < 1 || cyls > 16383) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001993 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001994 return NULL;
thse4bcb142007-12-02 04:51:10 +00001995 }
1996 if (heads < 1 || heads > 16) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001997 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001998 return NULL;
thse4bcb142007-12-02 04:51:10 +00001999 }
2000 if (secs < 1 || secs > 63) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002001 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002002 return NULL;
thse4bcb142007-12-02 04:51:10 +00002003 }
2004 }
2005
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002006 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002007 if (!cyls) {
2008 fprintf(stderr,
2009 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002010 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002011 return NULL;
thse4bcb142007-12-02 04:51:10 +00002012 }
2013 if (!strcmp(buf, "none"))
2014 translation = BIOS_ATA_TRANSLATION_NONE;
2015 else if (!strcmp(buf, "lba"))
2016 translation = BIOS_ATA_TRANSLATION_LBA;
2017 else if (!strcmp(buf, "auto"))
2018 translation = BIOS_ATA_TRANSLATION_AUTO;
2019 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002020 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002021 return NULL;
thse4bcb142007-12-02 04:51:10 +00002022 }
2023 }
2024
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002025 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002026 if (!strcmp(buf, "disk")) {
2027 media = MEDIA_DISK;
2028 } else if (!strcmp(buf, "cdrom")) {
2029 if (cyls || secs || heads) {
2030 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002031 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002032 return NULL;
thse4bcb142007-12-02 04:51:10 +00002033 }
2034 media = MEDIA_CDROM;
2035 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002036 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002037 return NULL;
thse4bcb142007-12-02 04:51:10 +00002038 }
2039 }
2040
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002041 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00002042 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002043 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002044 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002045 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002046 else if (!strcmp(buf, "writeback"))
2047 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002048 else {
2049 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002050 return NULL;
balrog33f00272007-12-24 14:33:24 +00002051 }
2052 }
2053
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002054 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00002055 if (strcmp(buf, "?") == 0) {
2056 fprintf(stderr, "qemu: Supported formats:");
2057 bdrv_iterate_format(bdrv_format_print, NULL);
2058 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002059 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00002060 }
aurel321e72d3b2008-04-28 20:26:45 +00002061 drv = bdrv_find_format(buf);
2062 if (!drv) {
2063 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002064 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00002065 }
2066 }
2067
aliguoricdad4bd2009-02-28 16:51:01 +00002068 onerror = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002069 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00002070 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002071 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002072 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002073 }
2074 if (!strcmp(buf, "ignore"))
2075 onerror = BLOCK_ERR_IGNORE;
2076 else if (!strcmp(buf, "enospc"))
2077 onerror = BLOCK_ERR_STOP_ENOSPC;
2078 else if (!strcmp(buf, "stop"))
2079 onerror = BLOCK_ERR_STOP_ANY;
2080 else if (!strcmp(buf, "report"))
2081 onerror = BLOCK_ERR_REPORT;
2082 else {
2083 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002084 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002085 }
2086 }
2087
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002088 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002089 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002090 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002091 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002092 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002093 }
2094
thse4bcb142007-12-02 04:51:10 +00002095 /* compute bus and unit according index */
2096
2097 if (index != -1) {
2098 if (bus_id != 0 || unit_id != -1) {
2099 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002100 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002101 return NULL;
thse4bcb142007-12-02 04:51:10 +00002102 }
2103 if (max_devs == 0)
2104 {
2105 unit_id = index;
2106 bus_id = 0;
2107 } else {
2108 unit_id = index % max_devs;
2109 bus_id = index / max_devs;
2110 }
2111 }
2112
2113 /* if user doesn't specify a unit_id,
2114 * try to find the first free
2115 */
2116
2117 if (unit_id == -1) {
2118 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002119 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002120 unit_id++;
2121 if (max_devs && unit_id >= max_devs) {
2122 unit_id -= max_devs;
2123 bus_id++;
2124 }
2125 }
2126 }
2127
2128 /* check unit id */
2129
2130 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002131 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2132 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002133 return NULL;
thse4bcb142007-12-02 04:51:10 +00002134 }
2135
2136 /*
2137 * ignore multiple definitions
2138 */
2139
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002140 if (drive_get(type, bus_id, unit_id) != NULL) {
2141 *fatal_error = 0;
2142 return NULL;
2143 }
thse4bcb142007-12-02 04:51:10 +00002144
2145 /* init */
2146
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002147 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02002148 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002149 dinfo->id = qemu_strdup(buf);
2150 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002151 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002152 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002153 if (type == IF_IDE || type == IF_SCSI)
2154 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2155 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002156 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002157 devname, bus_id, mediastr, unit_id);
2158 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002159 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002160 devname, mediastr, unit_id);
2161 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002162 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002163 dinfo->devaddr = devaddr;
2164 dinfo->type = type;
2165 dinfo->bus = bus_id;
2166 dinfo->unit = unit_id;
2167 dinfo->onerror = onerror;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002168 dinfo->opts = opts;
2169 if (serial)
2170 strncpy(dinfo->serial, serial, sizeof(serial));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002171 TAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00002172
thsf60d39b2007-12-17 03:55:57 +00002173 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002174 case IF_IDE:
2175 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002176 case IF_XEN:
thse4bcb142007-12-02 04:51:10 +00002177 switch(media) {
2178 case MEDIA_DISK:
2179 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002180 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2181 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00002182 }
2183 break;
2184 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002185 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00002186 break;
2187 }
2188 break;
2189 case IF_SD:
2190 /* FIXME: This isn't really a floppy, but it's a reasonable
2191 approximation. */
2192 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002193 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00002194 break;
2195 case IF_PFLASH:
2196 case IF_MTD:
Gerd Hoffmanna8659e92009-07-31 12:25:39 +02002197 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00002198 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02002199 case IF_VIRTIO:
2200 /* add virtio block device */
2201 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2202 qemu_opt_set(opts, "driver", "virtio-blk-pci");
2203 qemu_opt_set(opts, "drive", dinfo->id);
2204 if (devaddr)
2205 qemu_opt_set(opts, "addr", devaddr);
2206 break;
Paul Brookaae94602009-05-14 22:35:06 +01002207 case IF_COUNT:
2208 abort();
thse4bcb142007-12-02 04:51:10 +00002209 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002210 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002211 *fatal_error = 0;
2212 return NULL;
2213 }
balrog33f00272007-12-24 14:33:24 +00002214 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002215 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002216 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002217 cache = 2; /* always use write-back with snapshot */
2218 }
2219 if (cache == 0) /* no caching */
2220 bdrv_flags |= BDRV_O_NOCACHE;
2221 else if (cache == 2) /* write-back */
2222 bdrv_flags |= BDRV_O_CACHE_WB;
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002223 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
thse4bcb142007-12-02 04:51:10 +00002224 fprintf(stderr, "qemu: could not open disk image %s\n",
2225 file);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002226 return NULL;
thse4bcb142007-12-02 04:51:10 +00002227 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002228 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00002229 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002230 *fatal_error = 0;
2231 return dinfo;
thse4bcb142007-12-02 04:51:10 +00002232}
2233
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002234static int drive_init_func(QemuOpts *opts, void *opaque)
2235{
2236 QEMUMachine *machine = opaque;
2237 int fatal_error = 0;
2238
2239 if (drive_init(opts, machine, &fatal_error) == NULL) {
2240 if (fatal_error)
2241 return 1;
2242 }
2243 return 0;
2244}
2245
2246static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2247{
2248 if (NULL == qemu_opt_get(opts, "snapshot")) {
2249 qemu_opt_set(opts, "snapshot", "on");
2250 }
2251 return 0;
2252}
2253
Jan Kiszka76e30d02009-07-02 00:19:02 +02002254void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2255{
2256 boot_set_handler = func;
2257 boot_set_opaque = opaque;
2258}
2259
2260int qemu_boot_set(const char *boot_devices)
2261{
2262 if (!boot_set_handler) {
2263 return -EINVAL;
2264 }
2265 return boot_set_handler(boot_set_opaque, boot_devices);
2266}
2267
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002268static int parse_bootdevices(char *devices)
2269{
2270 /* We just do some generic consistency checks */
2271 const char *p;
2272 int bitmap = 0;
2273
2274 for (p = devices; *p != '\0'; p++) {
2275 /* Allowed boot devices are:
2276 * a-b: floppy disk drives
2277 * c-f: IDE disk drives
2278 * g-m: machine implementation dependant drives
2279 * n-p: network devices
2280 * It's up to each machine implementation to check if the given boot
2281 * devices match the actual hardware implementation and firmware
2282 * features.
2283 */
2284 if (*p < 'a' || *p > 'p') {
2285 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2286 exit(1);
2287 }
2288 if (bitmap & (1 << (*p - 'a'))) {
2289 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2290 exit(1);
2291 }
2292 bitmap |= 1 << (*p - 'a');
2293 }
2294 return bitmap;
2295}
2296
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002297static void restore_boot_devices(void *opaque)
2298{
2299 char *standard_boot_devices = opaque;
2300
2301 qemu_boot_set(standard_boot_devices);
2302
2303 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2304 qemu_free(standard_boot_devices);
2305}
2306
aliguori268a3622009-04-21 22:30:27 +00002307static void numa_add(const char *optarg)
2308{
2309 char option[128];
2310 char *endptr;
2311 unsigned long long value, endvalue;
2312 int nodenr;
2313
2314 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2315 if (!strcmp(option, "node")) {
2316 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2317 nodenr = nb_numa_nodes;
2318 } else {
2319 nodenr = strtoull(option, NULL, 10);
2320 }
2321
2322 if (get_param_value(option, 128, "mem", optarg) == 0) {
2323 node_mem[nodenr] = 0;
2324 } else {
2325 value = strtoull(option, &endptr, 0);
2326 switch (*endptr) {
2327 case 0: case 'M': case 'm':
2328 value <<= 20;
2329 break;
2330 case 'G': case 'g':
2331 value <<= 30;
2332 break;
2333 }
2334 node_mem[nodenr] = value;
2335 }
2336 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2337 node_cpumask[nodenr] = 0;
2338 } else {
2339 value = strtoull(option, &endptr, 10);
2340 if (value >= 64) {
2341 value = 63;
2342 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2343 } else {
2344 if (*endptr == '-') {
2345 endvalue = strtoull(endptr+1, &endptr, 10);
2346 if (endvalue >= 63) {
2347 endvalue = 62;
2348 fprintf(stderr,
2349 "only 63 CPUs in NUMA mode supported.\n");
2350 }
2351 value = (1 << (endvalue + 1)) - (1 << value);
2352 } else {
2353 value = 1 << value;
2354 }
2355 }
2356 node_cpumask[nodenr] = value;
2357 }
2358 nb_numa_nodes++;
2359 }
2360 return;
2361}
2362
bellard330d0412003-07-26 18:11:40 +00002363/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002364/* USB devices */
2365
pbrook0d92ed32006-05-21 16:30:15 +00002366static USBPort *used_usb_ports;
2367static USBPort *free_usb_ports;
2368
2369/* ??? Maybe change this to register a hub to keep track of the topology. */
2370void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2371 usb_attachfn attach)
2372{
2373 port->opaque = opaque;
2374 port->index = index;
2375 port->attach = attach;
2376 port->next = free_usb_ports;
2377 free_usb_ports = port;
2378}
2379
aliguori4b096fc2008-08-21 19:28:55 +00002380int usb_device_add_dev(USBDevice *dev)
2381{
2382 USBPort *port;
2383
2384 /* Find a USB port to add the device to. */
2385 port = free_usb_ports;
2386 if (!port->next) {
2387 USBDevice *hub;
2388
2389 /* Create a new hub and chain it on. */
2390 free_usb_ports = NULL;
2391 port->next = used_usb_ports;
2392 used_usb_ports = port;
2393
2394 hub = usb_hub_init(VM_USB_HUB_SIZE);
2395 usb_attach(port, hub);
2396 port = free_usb_ports;
2397 }
2398
2399 free_usb_ports = port->next;
2400 port->next = used_usb_ports;
2401 used_usb_ports = port;
2402 usb_attach(port, dev);
2403 return 0;
2404}
2405
aliguoribb5fc202009-03-05 23:01:15 +00002406static void usb_msd_password_cb(void *opaque, int err)
2407{
2408 USBDevice *dev = opaque;
2409
2410 if (!err)
2411 usb_device_add_dev(dev);
2412 else
2413 dev->handle_destroy(dev);
2414}
2415
aliguoric0f4ce72009-03-05 23:01:01 +00002416static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002417{
2418 const char *p;
2419 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002420
pbrook0d92ed32006-05-21 16:30:15 +00002421 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002422 return -1;
2423
2424 if (strstart(devname, "host:", &p)) {
2425 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002426 } else if (!strcmp(devname, "mouse")) {
2427 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002428 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002429 dev = usb_tablet_init();
2430 } else if (!strcmp(devname, "keyboard")) {
2431 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002432 } else if (strstart(devname, "disk:", &p)) {
aliguoric0f4ce72009-03-05 23:01:01 +00002433 BlockDriverState *bs;
2434
aliguoribb5fc202009-03-05 23:01:15 +00002435 dev = usb_msd_init(p);
aliguoric0f4ce72009-03-05 23:01:01 +00002436 if (!dev)
2437 return -1;
aliguoribb5fc202009-03-05 23:01:15 +00002438 bs = usb_msd_get_bdrv(dev);
aliguoric0f4ce72009-03-05 23:01:01 +00002439 if (bdrv_key_required(bs)) {
2440 autostart = 0;
aliguoribb5fc202009-03-05 23:01:15 +00002441 if (is_hotplug) {
aliguori376253e2009-03-05 23:01:23 +00002442 monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb,
2443 dev);
aliguoribb5fc202009-03-05 23:01:15 +00002444 return 0;
aliguoric0f4ce72009-03-05 23:01:01 +00002445 }
2446 }
balrogf6d2a312007-06-10 19:21:04 +00002447 } else if (!strcmp(devname, "wacom-tablet")) {
2448 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002449 } else if (strstart(devname, "serial:", &p)) {
2450 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002451#ifdef CONFIG_BRLAPI
2452 } else if (!strcmp(devname, "braille")) {
2453 dev = usb_baum_init();
2454#endif
balrog6c9f8862008-07-17 20:47:13 +00002455 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002456 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002457
Jan Kiszka10ae5a72009-05-08 12:34:18 +02002458 if (net_client_init(NULL, "nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002459 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002460 nd_table[nic].model = "usb";
2461 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002462 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2463 dev = usb_bt_init(devname[2] ? hci_init(p) :
2464 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002465 } else {
2466 return -1;
2467 }
pbrook0d92ed32006-05-21 16:30:15 +00002468 if (!dev)
2469 return -1;
2470
aliguori4b096fc2008-08-21 19:28:55 +00002471 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002472}
2473
aliguori1f3870a2008-08-21 19:27:48 +00002474int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002475{
pbrook0d92ed32006-05-21 16:30:15 +00002476 USBPort *port;
2477 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002478 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002479
pbrook0d92ed32006-05-21 16:30:15 +00002480 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002481 return -1;
2482
bellarda594cfb2005-11-06 16:13:29 +00002483 if (bus_num != 0)
2484 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002485
2486 lastp = &used_usb_ports;
2487 port = used_usb_ports;
2488 while (port && port->dev->addr != addr) {
2489 lastp = &port->next;
2490 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002491 }
pbrook0d92ed32006-05-21 16:30:15 +00002492
2493 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002494 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002495
bellard059809e2006-07-19 18:06:15 +00002496 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002497 *lastp = port->next;
2498 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002499 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002500 port->next = free_usb_ports;
2501 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002502 return 0;
2503}
2504
aliguori1f3870a2008-08-21 19:27:48 +00002505static int usb_device_del(const char *devname)
2506{
2507 int bus_num, addr;
2508 const char *p;
2509
aliguori5d0c5752008-09-14 01:07:41 +00002510 if (strstart(devname, "host:", &p))
2511 return usb_host_device_close(p);
2512
aliguori1f3870a2008-08-21 19:27:48 +00002513 if (!used_usb_ports)
2514 return -1;
2515
2516 p = strchr(devname, '.');
2517 if (!p)
2518 return -1;
2519 bus_num = strtoul(devname, NULL, 0);
2520 addr = strtoul(p + 1, NULL, 0);
2521
2522 return usb_device_del_addr(bus_num, addr);
2523}
2524
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002525static int usb_parse(const char *cmdline)
2526{
2527 return usb_device_add(cmdline, 0);
2528}
2529
aliguori376253e2009-03-05 23:01:23 +00002530void do_usb_add(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002531{
aliguoric0f4ce72009-03-05 23:01:01 +00002532 usb_device_add(devname, 1);
bellarda594cfb2005-11-06 16:13:29 +00002533}
2534
aliguori376253e2009-03-05 23:01:23 +00002535void do_usb_del(Monitor *mon, const char *devname)
bellarda594cfb2005-11-06 16:13:29 +00002536{
aliguori4b096fc2008-08-21 19:28:55 +00002537 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002538}
2539
aliguori376253e2009-03-05 23:01:23 +00002540void usb_info(Monitor *mon)
bellarda594cfb2005-11-06 16:13:29 +00002541{
2542 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002543 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002544 const char *speed_str;
2545
pbrook0d92ed32006-05-21 16:30:15 +00002546 if (!usb_enabled) {
aliguori376253e2009-03-05 23:01:23 +00002547 monitor_printf(mon, "USB support not enabled\n");
bellarda594cfb2005-11-06 16:13:29 +00002548 return;
2549 }
2550
pbrook0d92ed32006-05-21 16:30:15 +00002551 for (port = used_usb_ports; port; port = port->next) {
2552 dev = port->dev;
2553 if (!dev)
2554 continue;
2555 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002556 case USB_SPEED_LOW:
2557 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002558 break;
ths5fafdf22007-09-16 21:08:06 +00002559 case USB_SPEED_FULL:
2560 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002561 break;
ths5fafdf22007-09-16 21:08:06 +00002562 case USB_SPEED_HIGH:
2563 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002564 break;
2565 default:
ths5fafdf22007-09-16 21:08:06 +00002566 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002567 break;
bellarda594cfb2005-11-06 16:13:29 +00002568 }
aliguori376253e2009-03-05 23:01:23 +00002569 monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2570 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002571 }
2572}
2573
bellardf7cce892004-12-08 22:21:25 +00002574/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002575/* PCMCIA/Cardbus */
2576
2577static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002578 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002579 struct pcmcia_socket_entry_s *next;
2580} *pcmcia_sockets = 0;
2581
Paul Brookbc24a222009-05-10 01:44:56 +01002582void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002583{
2584 struct pcmcia_socket_entry_s *entry;
2585
2586 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2587 entry->socket = socket;
2588 entry->next = pcmcia_sockets;
2589 pcmcia_sockets = entry;
2590}
2591
Paul Brookbc24a222009-05-10 01:44:56 +01002592void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002593{
2594 struct pcmcia_socket_entry_s *entry, **ptr;
2595
2596 ptr = &pcmcia_sockets;
2597 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2598 if (entry->socket == socket) {
2599 *ptr = entry->next;
2600 qemu_free(entry);
2601 }
2602}
2603
aliguori376253e2009-03-05 23:01:23 +00002604void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002605{
2606 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002607
balrog201a51f2007-04-30 00:51:09 +00002608 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002609 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002610
2611 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002612 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2613 iter->socket->attached ? iter->socket->card_string :
2614 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002615}
2616
2617/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002618/* register display */
2619
aliguori7b5d76d2009-03-13 15:02:13 +00002620struct DisplayAllocator default_allocator = {
2621 defaultallocator_create_displaysurface,
2622 defaultallocator_resize_displaysurface,
2623 defaultallocator_free_displaysurface
2624};
2625
aliguori3023f332009-01-16 19:04:14 +00002626void register_displaystate(DisplayState *ds)
2627{
2628 DisplayState **s;
2629 s = &display_state;
2630 while (*s != NULL)
2631 s = &(*s)->next;
2632 ds->next = NULL;
2633 *s = ds;
2634}
2635
2636DisplayState *get_displaystate(void)
2637{
2638 return display_state;
2639}
2640
aliguori7b5d76d2009-03-13 15:02:13 +00002641DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2642{
2643 if(ds->allocator == &default_allocator) ds->allocator = da;
2644 return ds->allocator;
2645}
2646
ths2ff89792007-06-21 23:34:19 +00002647/* dumb display */
2648
aliguori8f391ab2009-01-19 16:34:10 +00002649static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002650{
aliguori8f391ab2009-01-19 16:34:10 +00002651 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002652 ds->allocator = &default_allocator;
2653 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002654 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002655}
2656
2657/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002658/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002659
bellardc4b1fcc2004-03-14 21:44:30 +00002660typedef struct IOHandlerRecord {
2661 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002662 IOCanRWHandler *fd_read_poll;
2663 IOHandler *fd_read;
2664 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002665 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002666 void *opaque;
2667 /* temporary data */
2668 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002669 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002670} IOHandlerRecord;
2671
bellard8a7ddc32004-03-31 19:00:16 +00002672static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002673
bellard7c9d8e02005-11-15 22:16:05 +00002674/* XXX: fd_read_poll should be suppressed, but an API change is
2675 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002676int qemu_set_fd_handler2(int fd,
2677 IOCanRWHandler *fd_read_poll,
2678 IOHandler *fd_read,
2679 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002680 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002681{
bellard8a7ddc32004-03-31 19:00:16 +00002682 IOHandlerRecord **pioh, *ioh;
2683
bellard7c9d8e02005-11-15 22:16:05 +00002684 if (!fd_read && !fd_write) {
2685 pioh = &first_io_handler;
2686 for(;;) {
2687 ioh = *pioh;
2688 if (ioh == NULL)
2689 break;
2690 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002691 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002692 break;
2693 }
2694 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002695 }
bellard7c9d8e02005-11-15 22:16:05 +00002696 } else {
2697 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2698 if (ioh->fd == fd)
2699 goto found;
2700 }
2701 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002702 ioh->next = first_io_handler;
2703 first_io_handler = ioh;
2704 found:
2705 ioh->fd = fd;
2706 ioh->fd_read_poll = fd_read_poll;
2707 ioh->fd_read = fd_read;
2708 ioh->fd_write = fd_write;
2709 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002710 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002711 }
bellard7c9d8e02005-11-15 22:16:05 +00002712 return 0;
2713}
2714
ths5fafdf22007-09-16 21:08:06 +00002715int qemu_set_fd_handler(int fd,
2716 IOHandler *fd_read,
2717 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002718 void *opaque)
2719{
2720 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002721}
2722
aliguori56f3a5d2008-10-31 18:07:17 +00002723#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002724/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002725/* Polling handling */
2726
2727typedef struct PollingEntry {
2728 PollingFunc *func;
2729 void *opaque;
2730 struct PollingEntry *next;
2731} PollingEntry;
2732
2733static PollingEntry *first_polling_entry;
2734
2735int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2736{
2737 PollingEntry **ppe, *pe;
2738 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002739 pe->func = func;
2740 pe->opaque = opaque;
2741 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2742 *ppe = pe;
2743 return 0;
2744}
2745
2746void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2747{
2748 PollingEntry **ppe, *pe;
2749 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2750 pe = *ppe;
2751 if (pe->func == func && pe->opaque == opaque) {
2752 *ppe = pe->next;
2753 qemu_free(pe);
2754 break;
2755 }
2756 }
2757}
2758
bellarda18e5242006-06-25 17:18:27 +00002759/***********************************************************/
2760/* Wait objects support */
2761typedef struct WaitObjects {
2762 int num;
2763 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2764 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2765 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2766} WaitObjects;
2767
2768static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002769
bellarda18e5242006-06-25 17:18:27 +00002770int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2771{
2772 WaitObjects *w = &wait_objects;
2773
2774 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2775 return -1;
2776 w->events[w->num] = handle;
2777 w->func[w->num] = func;
2778 w->opaque[w->num] = opaque;
2779 w->num++;
2780 return 0;
2781}
2782
2783void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2784{
2785 int i, found;
2786 WaitObjects *w = &wait_objects;
2787
2788 found = 0;
2789 for (i = 0; i < w->num; i++) {
2790 if (w->events[i] == handle)
2791 found = 1;
2792 if (found) {
2793 w->events[i] = w->events[i + 1];
2794 w->func[i] = w->func[i + 1];
2795 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002796 }
bellarda18e5242006-06-25 17:18:27 +00002797 }
2798 if (found)
2799 w->num--;
2800}
2801#endif
2802
bellard8a7ddc32004-03-31 19:00:16 +00002803/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002804/* ram save/restore */
2805
bellard8a7ddc32004-03-31 19:00:16 +00002806static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
2807{
2808 int v;
2809
2810 v = qemu_get_byte(f);
2811 switch(v) {
2812 case 0:
2813 if (qemu_get_buffer(f, buf, len) != len)
2814 return -EIO;
2815 break;
2816 case 1:
2817 v = qemu_get_byte(f);
2818 memset(buf, v, len);
2819 break;
2820 default:
2821 return -EINVAL;
2822 }
aliguori871d2f02008-10-13 03:07:56 +00002823
2824 if (qemu_file_has_error(f))
2825 return -EIO;
2826
bellard8a7ddc32004-03-31 19:00:16 +00002827 return 0;
2828}
2829
bellardc88676f2006-08-06 13:36:11 +00002830static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00002831{
aurel3200f82b82008-04-27 21:12:55 +00002832 int ret;
2833 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00002834
pbrook94a6b542009-04-11 17:15:54 +00002835 if (qemu_get_be32(f) != last_ram_offset)
bellard8a7ddc32004-03-31 19:00:16 +00002836 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00002837 for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) {
pbrook5579c7f2009-04-11 14:47:08 +00002838 ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE);
bellard8a7ddc32004-03-31 19:00:16 +00002839 if (ret)
2840 return ret;
2841 }
2842 return 0;
2843}
2844
bellardc88676f2006-08-06 13:36:11 +00002845#define BDRV_HASH_BLOCK_SIZE 1024
2846#define IOBUF_SIZE 4096
2847#define RAM_CBLOCK_MAGIC 0xfabe
2848
bellardc88676f2006-08-06 13:36:11 +00002849typedef struct RamDecompressState {
2850 z_stream zstream;
2851 QEMUFile *f;
2852 uint8_t buf[IOBUF_SIZE];
2853} RamDecompressState;
2854
2855static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
2856{
2857 int ret;
2858 memset(s, 0, sizeof(*s));
2859 s->f = f;
2860 ret = inflateInit(&s->zstream);
2861 if (ret != Z_OK)
2862 return -1;
2863 return 0;
2864}
2865
2866static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
2867{
2868 int ret, clen;
2869
2870 s->zstream.avail_out = len;
2871 s->zstream.next_out = buf;
2872 while (s->zstream.avail_out > 0) {
2873 if (s->zstream.avail_in == 0) {
2874 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
2875 return -1;
2876 clen = qemu_get_be16(s->f);
2877 if (clen > IOBUF_SIZE)
2878 return -1;
2879 qemu_get_buffer(s->f, s->buf, clen);
2880 s->zstream.avail_in = clen;
2881 s->zstream.next_in = s->buf;
2882 }
2883 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
2884 if (ret != Z_OK && ret != Z_STREAM_END) {
2885 return -1;
2886 }
2887 }
2888 return 0;
2889}
2890
2891static void ram_decompress_close(RamDecompressState *s)
2892{
2893 inflateEnd(&s->zstream);
2894}
2895
aliguori475e4272008-10-06 20:21:51 +00002896#define RAM_SAVE_FLAG_FULL 0x01
2897#define RAM_SAVE_FLAG_COMPRESS 0x02
2898#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2899#define RAM_SAVE_FLAG_PAGE 0x08
2900#define RAM_SAVE_FLAG_EOS 0x10
2901
2902static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002903{
aliguori475e4272008-10-06 20:21:51 +00002904 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2905 uint32_t *array = (uint32_t *)page;
2906 int i;
ths3b46e622007-09-17 08:09:54 +00002907
aliguori475e4272008-10-06 20:21:51 +00002908 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2909 if (array[i] != val)
2910 return 0;
bellardc88676f2006-08-06 13:36:11 +00002911 }
aliguori475e4272008-10-06 20:21:51 +00002912
2913 return 1;
bellardc88676f2006-08-06 13:36:11 +00002914}
2915
aliguori475e4272008-10-06 20:21:51 +00002916static int ram_save_block(QEMUFile *f)
2917{
2918 static ram_addr_t current_addr = 0;
2919 ram_addr_t saved_addr = current_addr;
2920 ram_addr_t addr = 0;
2921 int found = 0;
2922
pbrook94a6b542009-04-11 17:15:54 +00002923 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002924 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002925 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002926
2927 cpu_physical_memory_reset_dirty(current_addr,
2928 current_addr + TARGET_PAGE_SIZE,
2929 MIGRATION_DIRTY_FLAG);
2930
pbrook5579c7f2009-04-11 14:47:08 +00002931 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002932
pbrook5579c7f2009-04-11 14:47:08 +00002933 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002934 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002935 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00002936 } else {
2937 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00002938 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002939 }
2940
2941 found = 1;
2942 break;
2943 }
2944 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00002945 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00002946 }
2947
2948 return found;
2949}
2950
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002951static uint64_t bytes_transferred = 0;
aliguori475e4272008-10-06 20:21:51 +00002952
2953static ram_addr_t ram_save_remaining(void)
2954{
2955 ram_addr_t addr;
2956 ram_addr_t count = 0;
2957
pbrook94a6b542009-04-11 17:15:54 +00002958 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002959 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2960 count++;
2961 }
2962
2963 return count;
2964}
2965
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002966uint64_t ram_bytes_remaining(void)
2967{
2968 return ram_save_remaining() * TARGET_PAGE_SIZE;
2969}
2970
2971uint64_t ram_bytes_transferred(void)
2972{
2973 return bytes_transferred;
2974}
2975
2976uint64_t ram_bytes_total(void)
2977{
2978 return last_ram_offset;
2979}
2980
aliguori475e4272008-10-06 20:21:51 +00002981static int ram_save_live(QEMUFile *f, int stage, void *opaque)
2982{
2983 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002984 uint64_t bytes_transferred_last;
2985 double bwidth = 0;
2986 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00002987
Jan Kiszka9fa06382009-05-22 23:51:45 +02002988 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002989 qemu_file_set_error(f);
2990 return 0;
2991 }
2992
aliguori475e4272008-10-06 20:21:51 +00002993 if (stage == 1) {
2994 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00002995 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002996 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2997 cpu_physical_memory_set_dirty(addr);
2998 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002999
aliguori475e4272008-10-06 20:21:51 +00003000 /* Enable dirty memory tracking */
3001 cpu_physical_memory_set_dirty_tracking(1);
3002
pbrook94a6b542009-04-11 17:15:54 +00003003 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003004 }
3005
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003006 bytes_transferred_last = bytes_transferred;
3007 bwidth = get_clock();
3008
aliguori475e4272008-10-06 20:21:51 +00003009 while (!qemu_file_rate_limit(f)) {
3010 int ret;
3011
3012 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003013 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003014 if (ret == 0) /* no more blocks */
3015 break;
3016 }
3017
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003018 bwidth = get_clock() - bwidth;
3019 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3020
3021 /* if we haven't transferred anything this round, force expected_time to a
3022 * a very high value, but without crashing */
3023 if (bwidth == 0)
3024 bwidth = 0.000001;
3025
aliguori475e4272008-10-06 20:21:51 +00003026 /* try transferring iterative blocks of memory */
3027
3028 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003029
3030 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003031 while (ram_save_block(f) != 0) {
3032 bytes_transferred += TARGET_PAGE_SIZE;
3033 }
aliguori8215e912009-04-05 19:30:55 +00003034 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003035 }
3036
3037 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3038
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003039 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3040
3041 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003042}
3043
3044static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003045{
3046 RamDecompressState s1, *s = &s1;
3047 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003048 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003049
bellardc88676f2006-08-06 13:36:11 +00003050 if (ram_decompress_open(s, f) < 0)
3051 return -EINVAL;
pbrook94a6b542009-04-11 17:15:54 +00003052 for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) {
bellardc88676f2006-08-06 13:36:11 +00003053 if (ram_decompress_buf(s, buf, 1) < 0) {
3054 fprintf(stderr, "Error while reading ram block header\n");
3055 goto error;
3056 }
3057 if (buf[0] == 0) {
pbrook5579c7f2009-04-11 14:47:08 +00003058 if (ram_decompress_buf(s, qemu_get_ram_ptr(i),
3059 BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003060 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003061 goto error;
3062 }
aliguori475e4272008-10-06 20:21:51 +00003063 } else {
bellardc88676f2006-08-06 13:36:11 +00003064 error:
3065 printf("Error block header\n");
3066 return -EINVAL;
3067 }
3068 }
3069 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003070
3071 return 0;
3072}
3073
3074static int ram_load(QEMUFile *f, void *opaque, int version_id)
3075{
3076 ram_addr_t addr;
3077 int flags;
3078
3079 if (version_id == 1)
3080 return ram_load_v1(f, opaque);
3081
3082 if (version_id == 2) {
pbrook94a6b542009-04-11 17:15:54 +00003083 if (qemu_get_be32(f) != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003084 return -EINVAL;
3085 return ram_load_dead(f, opaque);
3086 }
3087
3088 if (version_id != 3)
3089 return -EINVAL;
3090
3091 do {
3092 addr = qemu_get_be64(f);
3093
3094 flags = addr & ~TARGET_PAGE_MASK;
3095 addr &= TARGET_PAGE_MASK;
3096
3097 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003098 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003099 return -EINVAL;
3100 }
3101
3102 if (flags & RAM_SAVE_FLAG_FULL) {
3103 if (ram_load_dead(f, opaque) < 0)
3104 return -EINVAL;
3105 }
3106
3107 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3108 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003109 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3110#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003111 if (ch == 0 &&
3112 (!kvm_enabled() || kvm_has_sync_mmu())) {
3113 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003114 }
Anthony Liguori30868442009-06-17 16:46:12 -05003115#endif
aliguori475e4272008-10-06 20:21:51 +00003116 } else if (flags & RAM_SAVE_FLAG_PAGE)
pbrook5579c7f2009-04-11 14:47:08 +00003117 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003118 } while (!(flags & RAM_SAVE_FLAG_EOS));
3119
bellardc88676f2006-08-06 13:36:11 +00003120 return 0;
3121}
3122
aliguori9e472e12008-10-08 19:50:24 +00003123void qemu_service_io(void)
3124{
aliguorid9f75a42009-04-24 18:03:11 +00003125 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003126}
3127
bellard8a7ddc32004-03-31 19:00:16 +00003128/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003129/* bottom halves (can be seen as timers which expire ASAP) */
3130
3131struct QEMUBH {
3132 QEMUBHFunc *cb;
3133 void *opaque;
3134 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003135 int idle;
3136 int deleted;
bellard83f64092006-08-01 16:21:11 +00003137 QEMUBH *next;
3138};
3139
3140static QEMUBH *first_bh = NULL;
3141
3142QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3143{
3144 QEMUBH *bh;
3145 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003146 bh->cb = cb;
3147 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003148 bh->next = first_bh;
3149 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003150 return bh;
3151}
3152
bellard6eb57332006-08-06 09:51:25 +00003153int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003154{
aliguori1b435b12008-10-31 17:24:21 +00003155 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003156 int ret;
bellard83f64092006-08-01 16:21:11 +00003157
bellard6eb57332006-08-06 09:51:25 +00003158 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003159 for (bh = first_bh; bh; bh = bh->next) {
3160 if (!bh->deleted && bh->scheduled) {
3161 bh->scheduled = 0;
3162 if (!bh->idle)
3163 ret = 1;
3164 bh->idle = 0;
3165 bh->cb(bh->opaque);
3166 }
bellard83f64092006-08-01 16:21:11 +00003167 }
aliguori1b435b12008-10-31 17:24:21 +00003168
3169 /* remove deleted bhs */
3170 bhp = &first_bh;
3171 while (*bhp) {
3172 bh = *bhp;
3173 if (bh->deleted) {
3174 *bhp = bh->next;
3175 qemu_free(bh);
3176 } else
3177 bhp = &bh->next;
3178 }
3179
bellard6eb57332006-08-06 09:51:25 +00003180 return ret;
bellard83f64092006-08-01 16:21:11 +00003181}
3182
aliguori1b435b12008-10-31 17:24:21 +00003183void qemu_bh_schedule_idle(QEMUBH *bh)
3184{
3185 if (bh->scheduled)
3186 return;
3187 bh->scheduled = 1;
3188 bh->idle = 1;
3189}
3190
bellard83f64092006-08-01 16:21:11 +00003191void qemu_bh_schedule(QEMUBH *bh)
3192{
bellard83f64092006-08-01 16:21:11 +00003193 if (bh->scheduled)
3194 return;
3195 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003196 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003197 /* stop the currently executing CPU to execute the BH ASAP */
aliguorid9f75a42009-04-24 18:03:11 +00003198 qemu_notify_event();
bellard83f64092006-08-01 16:21:11 +00003199}
3200
3201void qemu_bh_cancel(QEMUBH *bh)
3202{
aliguori1b435b12008-10-31 17:24:21 +00003203 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003204}
3205
3206void qemu_bh_delete(QEMUBH *bh)
3207{
aliguori1b435b12008-10-31 17:24:21 +00003208 bh->scheduled = 0;
3209 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003210}
3211
aliguori56f3a5d2008-10-31 18:07:17 +00003212static void qemu_bh_update_timeout(int *timeout)
3213{
3214 QEMUBH *bh;
3215
3216 for (bh = first_bh; bh; bh = bh->next) {
3217 if (!bh->deleted && bh->scheduled) {
3218 if (bh->idle) {
3219 /* idle bottom halves will be polled at least
3220 * every 10ms */
3221 *timeout = MIN(10, *timeout);
3222 } else {
3223 /* non-idle bottom halves will be executed
3224 * immediately */
3225 *timeout = 0;
3226 break;
3227 }
3228 }
3229 }
3230}
3231
bellard83f64092006-08-01 16:21:11 +00003232/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003233/* machine registration */
3234
blueswir1bdaf78e2008-10-04 07:24:27 +00003235static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003236QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003237
3238int qemu_register_machine(QEMUMachine *m)
3239{
3240 QEMUMachine **pm;
3241 pm = &first_machine;
3242 while (*pm != NULL)
3243 pm = &(*pm)->next;
3244 m->next = NULL;
3245 *pm = m;
3246 return 0;
3247}
3248
pbrook9596ebb2007-11-18 01:44:38 +00003249static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003250{
3251 QEMUMachine *m;
3252
3253 for(m = first_machine; m != NULL; m = m->next) {
3254 if (!strcmp(m->name, name))
3255 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003256 if (m->alias && !strcmp(m->alias, name))
3257 return m;
bellardcc1daa42005-06-05 14:49:17 +00003258 }
3259 return NULL;
3260}
3261
Anthony Liguori0c257432009-05-21 20:41:01 -05003262static QEMUMachine *find_default_machine(void)
3263{
3264 QEMUMachine *m;
3265
3266 for(m = first_machine; m != NULL; m = m->next) {
3267 if (m->is_default) {
3268 return m;
3269 }
3270 }
3271 return NULL;
3272}
3273
bellardcc1daa42005-06-05 14:49:17 +00003274/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003275/* main execution loop */
3276
pbrook9596ebb2007-11-18 01:44:38 +00003277static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003278{
aliguori7d957bd2009-01-15 22:14:11 +00003279 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003280 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003281 DisplayChangeListener *dcl = ds->listeners;
3282
3283 dpy_refresh(ds);
3284
3285 while (dcl != NULL) {
3286 if (dcl->gui_timer_interval &&
3287 dcl->gui_timer_interval < interval)
3288 interval = dcl->gui_timer_interval;
3289 dcl = dcl->next;
3290 }
3291 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003292}
3293
blueswir19043b622009-01-21 19:28:13 +00003294static void nographic_update(void *opaque)
3295{
3296 uint64_t interval = GUI_REFRESH_INTERVAL;
3297
3298 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3299}
3300
bellard0bd48852005-11-11 00:00:47 +00003301struct vm_change_state_entry {
3302 VMChangeStateHandler *cb;
3303 void *opaque;
3304 LIST_ENTRY (vm_change_state_entry) entries;
3305};
3306
3307static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3308
3309VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3310 void *opaque)
3311{
3312 VMChangeStateEntry *e;
3313
3314 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003315
3316 e->cb = cb;
3317 e->opaque = opaque;
3318 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3319 return e;
3320}
3321
3322void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3323{
3324 LIST_REMOVE (e, entries);
3325 qemu_free (e);
3326}
3327
aliguori9781e042009-01-22 17:15:29 +00003328static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003329{
3330 VMChangeStateEntry *e;
3331
3332 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003333 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003334 }
3335}
3336
aliguorid6dc3d42009-04-24 18:04:07 +00003337static void resume_all_vcpus(void);
3338static void pause_all_vcpus(void);
3339
bellard8a7ddc32004-03-31 19:00:16 +00003340void vm_start(void)
3341{
3342 if (!vm_running) {
3343 cpu_enable_ticks();
3344 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003345 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003346 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003347 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003348 }
3349}
3350
bellardbb0c6722004-06-20 12:37:32 +00003351/* reset/shutdown handler */
3352
3353typedef struct QEMUResetEntry {
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003354 TAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003355 QEMUResetHandler *func;
3356 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003357} QEMUResetEntry;
3358
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003359static TAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3360 TAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003361static int reset_requested;
3362static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003363static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003364static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003365static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003366
aurel32cf7a2fe2008-03-18 06:53:05 +00003367int qemu_shutdown_requested(void)
3368{
3369 int r = shutdown_requested;
3370 shutdown_requested = 0;
3371 return r;
3372}
3373
3374int qemu_reset_requested(void)
3375{
3376 int r = reset_requested;
3377 reset_requested = 0;
3378 return r;
3379}
3380
3381int qemu_powerdown_requested(void)
3382{
3383 int r = powerdown_requested;
3384 powerdown_requested = 0;
3385 return r;
3386}
3387
aliguorie5689022009-04-24 18:03:54 +00003388static int qemu_debug_requested(void)
3389{
3390 int r = debug_requested;
3391 debug_requested = 0;
3392 return r;
3393}
3394
aliguori6e29f5d2009-04-24 18:04:02 +00003395static int qemu_vmstop_requested(void)
3396{
3397 int r = vmstop_requested;
3398 vmstop_requested = 0;
3399 return r;
3400}
3401
3402static void do_vm_stop(int reason)
3403{
3404 if (vm_running) {
3405 cpu_disable_ticks();
3406 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003407 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003408 vm_state_notify(0, reason);
3409 }
3410}
3411
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003412void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003413{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003414 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003415
bellardbb0c6722004-06-20 12:37:32 +00003416 re->func = func;
3417 re->opaque = opaque;
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003418 TAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003419}
3420
Jan Kiszkadda9b292009-07-02 00:19:02 +02003421void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003422{
3423 QEMUResetEntry *re;
3424
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003425 TAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003426 if (re->func == func && re->opaque == opaque) {
3427 TAILQ_REMOVE(&reset_handlers, re, entry);
3428 qemu_free(re);
3429 return;
3430 }
3431 }
3432}
3433
3434void qemu_system_reset(void)
3435{
3436 QEMUResetEntry *re, *nre;
3437
3438 /* reset all devices */
3439 TAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003440 re->func(re->opaque);
3441 }
3442}
3443
3444void qemu_system_reset_request(void)
3445{
bellardd1beab82006-10-02 19:44:22 +00003446 if (no_reboot) {
3447 shutdown_requested = 1;
3448 } else {
3449 reset_requested = 1;
3450 }
aliguorid9f75a42009-04-24 18:03:11 +00003451 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003452}
3453
3454void qemu_system_shutdown_request(void)
3455{
3456 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003457 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003458}
3459
bellard34751872005-07-02 14:31:34 +00003460void qemu_system_powerdown_request(void)
3461{
3462 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003463 qemu_notify_event();
3464}
3465
aliguorid6dc3d42009-04-24 18:04:07 +00003466#ifdef CONFIG_IOTHREAD
3467static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003468{
aliguorid6dc3d42009-04-24 18:04:07 +00003469 vmstop_requested = reason;
3470 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003471}
aliguorid6dc3d42009-04-24 18:04:07 +00003472#endif
bellardbb0c6722004-06-20 12:37:32 +00003473
aliguori50317c72009-04-24 18:03:29 +00003474#ifndef _WIN32
3475static int io_thread_fd = -1;
3476
3477static void qemu_event_increment(void)
3478{
3479 static const char byte = 0;
3480
3481 if (io_thread_fd == -1)
3482 return;
3483
3484 write(io_thread_fd, &byte, sizeof(byte));
3485}
3486
3487static void qemu_event_read(void *opaque)
3488{
3489 int fd = (unsigned long)opaque;
3490 ssize_t len;
3491
3492 /* Drain the notify pipe */
3493 do {
3494 char buffer[512];
3495 len = read(fd, buffer, sizeof(buffer));
3496 } while ((len == -1 && errno == EINTR) || len > 0);
3497}
3498
3499static int qemu_event_init(void)
3500{
3501 int err;
3502 int fds[2];
3503
3504 err = pipe(fds);
3505 if (err == -1)
3506 return -errno;
3507
3508 err = fcntl_setfl(fds[0], O_NONBLOCK);
3509 if (err < 0)
3510 goto fail;
3511
3512 err = fcntl_setfl(fds[1], O_NONBLOCK);
3513 if (err < 0)
3514 goto fail;
3515
3516 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3517 (void *)(unsigned long)fds[0]);
3518
3519 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003520 return 0;
3521
aliguori50317c72009-04-24 18:03:29 +00003522fail:
3523 close(fds[0]);
3524 close(fds[1]);
3525 return err;
3526}
3527#else
3528HANDLE qemu_event_handle;
3529
3530static void dummy_event_handler(void *opaque)
3531{
3532}
3533
3534static int qemu_event_init(void)
3535{
3536 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3537 if (!qemu_event_handle) {
3538 perror("Failed CreateEvent");
3539 return -1;
3540 }
3541 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3542 return 0;
3543}
3544
3545static void qemu_event_increment(void)
3546{
3547 SetEvent(qemu_event_handle);
3548}
3549#endif
3550
aliguorid6dc3d42009-04-24 18:04:07 +00003551static int cpu_can_run(CPUState *env)
3552{
3553 if (env->stop)
3554 return 0;
3555 if (env->stopped)
3556 return 0;
3557 return 1;
3558}
3559
3560#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003561static int qemu_init_main_loop(void)
3562{
aliguori50317c72009-04-24 18:03:29 +00003563 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003564}
3565
aliguori0bf46a42009-04-24 18:03:41 +00003566void qemu_init_vcpu(void *_env)
3567{
3568 CPUState *env = _env;
3569
3570 if (kvm_enabled())
3571 kvm_init_vcpu(env);
3572 return;
3573}
3574
aliguori8edac962009-04-24 18:03:45 +00003575int qemu_cpu_self(void *env)
3576{
3577 return 1;
3578}
3579
aliguorid6dc3d42009-04-24 18:04:07 +00003580static void resume_all_vcpus(void)
3581{
3582}
3583
3584static void pause_all_vcpus(void)
3585{
3586}
3587
aliguori8edac962009-04-24 18:03:45 +00003588void qemu_cpu_kick(void *env)
3589{
3590 return;
3591}
3592
aliguorid6dc3d42009-04-24 18:04:07 +00003593void qemu_notify_event(void)
3594{
3595 CPUState *env = cpu_single_env;
3596
3597 if (env) {
3598 cpu_exit(env);
3599#ifdef USE_KQEMU
3600 if (env->kqemu_enabled)
3601 kqemu_cpu_interrupt(env);
3602#endif
3603 }
3604}
3605
aliguori48708522009-04-24 18:03:49 +00003606#define qemu_mutex_lock_iothread() do { } while (0)
3607#define qemu_mutex_unlock_iothread() do { } while (0)
3608
aliguori6e29f5d2009-04-24 18:04:02 +00003609void vm_stop(int reason)
3610{
3611 do_vm_stop(reason);
3612}
3613
aliguorid6dc3d42009-04-24 18:04:07 +00003614#else /* CONFIG_IOTHREAD */
3615
3616#include "qemu-thread.h"
3617
3618QemuMutex qemu_global_mutex;
3619static QemuMutex qemu_fair_mutex;
3620
3621static QemuThread io_thread;
3622
3623static QemuThread *tcg_cpu_thread;
3624static QemuCond *tcg_halt_cond;
3625
3626static int qemu_system_ready;
3627/* cpu creation */
3628static QemuCond qemu_cpu_cond;
3629/* system init */
3630static QemuCond qemu_system_cond;
3631static QemuCond qemu_pause_cond;
3632
3633static void block_io_signals(void);
3634static void unblock_io_signals(void);
3635static int tcg_has_work(void);
3636
3637static int qemu_init_main_loop(void)
3638{
3639 int ret;
3640
3641 ret = qemu_event_init();
3642 if (ret)
3643 return ret;
3644
3645 qemu_cond_init(&qemu_pause_cond);
3646 qemu_mutex_init(&qemu_fair_mutex);
3647 qemu_mutex_init(&qemu_global_mutex);
3648 qemu_mutex_lock(&qemu_global_mutex);
3649
3650 unblock_io_signals();
3651 qemu_thread_self(&io_thread);
3652
3653 return 0;
3654}
3655
3656static void qemu_wait_io_event(CPUState *env)
3657{
3658 while (!tcg_has_work())
3659 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3660
3661 qemu_mutex_unlock(&qemu_global_mutex);
3662
3663 /*
3664 * Users of qemu_global_mutex can be starved, having no chance
3665 * to acquire it since this path will get to it first.
3666 * So use another lock to provide fairness.
3667 */
3668 qemu_mutex_lock(&qemu_fair_mutex);
3669 qemu_mutex_unlock(&qemu_fair_mutex);
3670
3671 qemu_mutex_lock(&qemu_global_mutex);
3672 if (env->stop) {
3673 env->stop = 0;
3674 env->stopped = 1;
3675 qemu_cond_signal(&qemu_pause_cond);
3676 }
3677}
3678
3679static int qemu_cpu_exec(CPUState *env);
3680
3681static void *kvm_cpu_thread_fn(void *arg)
3682{
3683 CPUState *env = arg;
3684
3685 block_io_signals();
3686 qemu_thread_self(env->thread);
3687
3688 /* signal CPU creation */
3689 qemu_mutex_lock(&qemu_global_mutex);
3690 env->created = 1;
3691 qemu_cond_signal(&qemu_cpu_cond);
3692
3693 /* and wait for machine initialization */
3694 while (!qemu_system_ready)
3695 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3696
3697 while (1) {
3698 if (cpu_can_run(env))
3699 qemu_cpu_exec(env);
3700 qemu_wait_io_event(env);
3701 }
3702
3703 return NULL;
3704}
3705
3706static void tcg_cpu_exec(void);
3707
3708static void *tcg_cpu_thread_fn(void *arg)
3709{
3710 CPUState *env = arg;
3711
3712 block_io_signals();
3713 qemu_thread_self(env->thread);
3714
3715 /* signal CPU creation */
3716 qemu_mutex_lock(&qemu_global_mutex);
3717 for (env = first_cpu; env != NULL; env = env->next_cpu)
3718 env->created = 1;
3719 qemu_cond_signal(&qemu_cpu_cond);
3720
3721 /* and wait for machine initialization */
3722 while (!qemu_system_ready)
3723 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3724
3725 while (1) {
3726 tcg_cpu_exec();
3727 qemu_wait_io_event(cur_cpu);
3728 }
3729
3730 return NULL;
3731}
3732
3733void qemu_cpu_kick(void *_env)
3734{
3735 CPUState *env = _env;
3736 qemu_cond_broadcast(env->halt_cond);
3737 if (kvm_enabled())
3738 qemu_thread_signal(env->thread, SIGUSR1);
3739}
3740
3741int qemu_cpu_self(void *env)
3742{
3743 return (cpu_single_env != NULL);
3744}
3745
3746static void cpu_signal(int sig)
3747{
3748 if (cpu_single_env)
3749 cpu_exit(cpu_single_env);
3750}
3751
3752static void block_io_signals(void)
3753{
3754 sigset_t set;
3755 struct sigaction sigact;
3756
3757 sigemptyset(&set);
3758 sigaddset(&set, SIGUSR2);
3759 sigaddset(&set, SIGIO);
3760 sigaddset(&set, SIGALRM);
3761 pthread_sigmask(SIG_BLOCK, &set, NULL);
3762
3763 sigemptyset(&set);
3764 sigaddset(&set, SIGUSR1);
3765 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3766
3767 memset(&sigact, 0, sizeof(sigact));
3768 sigact.sa_handler = cpu_signal;
3769 sigaction(SIGUSR1, &sigact, NULL);
3770}
3771
3772static void unblock_io_signals(void)
3773{
3774 sigset_t set;
3775
3776 sigemptyset(&set);
3777 sigaddset(&set, SIGUSR2);
3778 sigaddset(&set, SIGIO);
3779 sigaddset(&set, SIGALRM);
3780 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3781
3782 sigemptyset(&set);
3783 sigaddset(&set, SIGUSR1);
3784 pthread_sigmask(SIG_BLOCK, &set, NULL);
3785}
3786
3787static void qemu_signal_lock(unsigned int msecs)
3788{
3789 qemu_mutex_lock(&qemu_fair_mutex);
3790
3791 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3792 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3793 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3794 break;
3795 }
3796 qemu_mutex_unlock(&qemu_fair_mutex);
3797}
3798
3799static void qemu_mutex_lock_iothread(void)
3800{
3801 if (kvm_enabled()) {
3802 qemu_mutex_lock(&qemu_fair_mutex);
3803 qemu_mutex_lock(&qemu_global_mutex);
3804 qemu_mutex_unlock(&qemu_fair_mutex);
3805 } else
3806 qemu_signal_lock(100);
3807}
3808
3809static void qemu_mutex_unlock_iothread(void)
3810{
3811 qemu_mutex_unlock(&qemu_global_mutex);
3812}
3813
3814static int all_vcpus_paused(void)
3815{
3816 CPUState *penv = first_cpu;
3817
3818 while (penv) {
3819 if (!penv->stopped)
3820 return 0;
3821 penv = (CPUState *)penv->next_cpu;
3822 }
3823
3824 return 1;
3825}
3826
3827static void pause_all_vcpus(void)
3828{
3829 CPUState *penv = first_cpu;
3830
3831 while (penv) {
3832 penv->stop = 1;
3833 qemu_thread_signal(penv->thread, SIGUSR1);
3834 qemu_cpu_kick(penv);
3835 penv = (CPUState *)penv->next_cpu;
3836 }
3837
3838 while (!all_vcpus_paused()) {
3839 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3840 penv = first_cpu;
3841 while (penv) {
3842 qemu_thread_signal(penv->thread, SIGUSR1);
3843 penv = (CPUState *)penv->next_cpu;
3844 }
3845 }
3846}
3847
3848static void resume_all_vcpus(void)
3849{
3850 CPUState *penv = first_cpu;
3851
3852 while (penv) {
3853 penv->stop = 0;
3854 penv->stopped = 0;
3855 qemu_thread_signal(penv->thread, SIGUSR1);
3856 qemu_cpu_kick(penv);
3857 penv = (CPUState *)penv->next_cpu;
3858 }
3859}
3860
3861static void tcg_init_vcpu(void *_env)
3862{
3863 CPUState *env = _env;
3864 /* share a single thread for all cpus with TCG */
3865 if (!tcg_cpu_thread) {
3866 env->thread = qemu_mallocz(sizeof(QemuThread));
3867 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3868 qemu_cond_init(env->halt_cond);
3869 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3870 while (env->created == 0)
3871 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3872 tcg_cpu_thread = env->thread;
3873 tcg_halt_cond = env->halt_cond;
3874 } else {
3875 env->thread = tcg_cpu_thread;
3876 env->halt_cond = tcg_halt_cond;
3877 }
3878}
3879
3880static void kvm_start_vcpu(CPUState *env)
3881{
3882 kvm_init_vcpu(env);
3883 env->thread = qemu_mallocz(sizeof(QemuThread));
3884 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3885 qemu_cond_init(env->halt_cond);
3886 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3887 while (env->created == 0)
3888 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3889}
3890
3891void qemu_init_vcpu(void *_env)
3892{
3893 CPUState *env = _env;
3894
3895 if (kvm_enabled())
3896 kvm_start_vcpu(env);
3897 else
3898 tcg_init_vcpu(env);
3899}
3900
3901void qemu_notify_event(void)
3902{
3903 qemu_event_increment();
3904}
3905
3906void vm_stop(int reason)
3907{
3908 QemuThread me;
3909 qemu_thread_self(&me);
3910
3911 if (!qemu_thread_equal(&me, &io_thread)) {
3912 qemu_system_vmstop_request(reason);
3913 /*
3914 * FIXME: should not return to device code in case
3915 * vm_stop() has been requested.
3916 */
3917 if (cpu_single_env) {
3918 cpu_exit(cpu_single_env);
3919 cpu_single_env->stop = 1;
3920 }
3921 return;
3922 }
3923 do_vm_stop(reason);
3924}
3925
3926#endif
3927
3928
ths877cf882007-04-18 18:11:47 +00003929#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003930static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003931{
3932 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003933 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003934
bellardf3311102006-04-12 20:21:17 +00003935
3936 /* XXX: need to suppress polling by better using win32 events */
3937 ret = 0;
3938 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3939 ret |= pe->func(pe->opaque);
3940 }
thse6b1e552007-04-18 17:56:02 +00003941 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003942 int err;
3943 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003944
aliguori56f3a5d2008-10-31 18:07:17 +00003945 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003946 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3947 if (w->func[ret - WAIT_OBJECT_0])
3948 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003949
ths5fafdf22007-09-16 21:08:06 +00003950 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003951 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003952
thse6b1e552007-04-18 17:56:02 +00003953 /* Check if event is signaled */
3954 ret2 = WaitForSingleObject(w->events[i], 0);
3955 if(ret2 == WAIT_OBJECT_0) {
3956 if (w->func[i])
3957 w->func[i](w->opaque[i]);
3958 } else if (ret2 == WAIT_TIMEOUT) {
3959 } else {
3960 err = GetLastError();
3961 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003962 }
3963 }
bellarda18e5242006-06-25 17:18:27 +00003964 } else if (ret == WAIT_TIMEOUT) {
3965 } else {
3966 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003967 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003968 }
bellardf3311102006-04-12 20:21:17 +00003969 }
aliguori56f3a5d2008-10-31 18:07:17 +00003970
3971 *timeout = 0;
3972}
3973#else
blueswir169d64512008-12-07 19:30:18 +00003974static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003975{
3976}
bellardfd1dff42006-02-01 21:29:26 +00003977#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003978
3979void main_loop_wait(int timeout)
3980{
3981 IOHandlerRecord *ioh;
3982 fd_set rfds, wfds, xfds;
3983 int ret, nfds;
3984 struct timeval tv;
3985
3986 qemu_bh_update_timeout(&timeout);
3987
3988 host_main_loop_wait(&timeout);
3989
bellardfd1dff42006-02-01 21:29:26 +00003990 /* poll any events */
3991 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003992 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003993 FD_ZERO(&rfds);
3994 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003995 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003996 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003997 if (ioh->deleted)
3998 continue;
bellardfd1dff42006-02-01 21:29:26 +00003999 if (ioh->fd_read &&
4000 (!ioh->fd_read_poll ||
4001 ioh->fd_read_poll(ioh->opaque) != 0)) {
4002 FD_SET(ioh->fd, &rfds);
4003 if (ioh->fd > nfds)
4004 nfds = ioh->fd;
4005 }
4006 if (ioh->fd_write) {
4007 FD_SET(ioh->fd, &wfds);
4008 if (ioh->fd > nfds)
4009 nfds = ioh->fd;
4010 }
4011 }
ths3b46e622007-09-17 08:09:54 +00004012
aliguori56f3a5d2008-10-31 18:07:17 +00004013 tv.tv_sec = timeout / 1000;
4014 tv.tv_usec = (timeout % 1000) * 1000;
4015
Jan Kiszkad918f232009-06-24 14:42:30 +02004016 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4017
aliguori48708522009-04-24 18:03:49 +00004018 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00004019 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00004020 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00004021 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00004022 IOHandlerRecord **pioh;
4023
4024 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00004025 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004026 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00004027 }
ths6ab43fd2007-08-25 01:34:19 +00004028 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004029 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00004030 }
4031 }
thscafffd42007-02-28 21:59:44 +00004032
4033 /* remove deleted IO handlers */
4034 pioh = &first_io_handler;
4035 while (*pioh) {
4036 ioh = *pioh;
4037 if (ioh->deleted) {
4038 *pioh = ioh->next;
4039 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00004040 } else
thscafffd42007-02-28 21:59:44 +00004041 pioh = &ioh->next;
4042 }
bellardfd1dff42006-02-01 21:29:26 +00004043 }
Jan Kiszkad918f232009-06-24 14:42:30 +02004044
4045 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00004046
aliguori50317c72009-04-24 18:03:29 +00004047 /* rearm timer, if not periodic */
4048 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4049 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
4050 qemu_rearm_alarm_timer(alarm_timer);
4051 }
4052
aliguori357c6922008-11-25 17:26:09 +00004053 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00004054 if (vm_running) {
4055 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
4056 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
4057 qemu_get_clock(vm_clock));
4058 }
aliguori357c6922008-11-25 17:26:09 +00004059
4060 /* real time timers */
4061 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
4062 qemu_get_clock(rt_clock));
4063
pbrook423f0742007-05-23 00:06:54 +00004064 /* Check bottom-halves last in case any of the earlier events triggered
4065 them. */
4066 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00004067
bellard5905b2e2004-08-01 21:53:26 +00004068}
4069
aliguori43b96852009-04-24 18:03:33 +00004070static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00004071{
aliguori43b96852009-04-24 18:03:33 +00004072 int ret;
bellard89bfc102006-02-08 22:46:31 +00004073#ifdef CONFIG_PROFILER
4074 int64_t ti;
4075#endif
aliguori43b96852009-04-24 18:03:33 +00004076
4077#ifdef CONFIG_PROFILER
4078 ti = profile_getclock();
4079#endif
4080 if (use_icount) {
4081 int64_t count;
4082 int decr;
4083 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4084 env->icount_decr.u16.low = 0;
4085 env->icount_extra = 0;
4086 count = qemu_next_deadline();
4087 count = (count + (1 << icount_time_shift) - 1)
4088 >> icount_time_shift;
4089 qemu_icount += count;
4090 decr = (count > 0xffff) ? 0xffff : count;
4091 count -= decr;
4092 env->icount_decr.u16.low = decr;
4093 env->icount_extra = count;
4094 }
4095 ret = cpu_exec(env);
4096#ifdef CONFIG_PROFILER
4097 qemu_time += profile_getclock() - ti;
4098#endif
4099 if (use_icount) {
4100 /* Fold pending instructions back into the
4101 instruction counter, and clear the interrupt flag. */
4102 qemu_icount -= (env->icount_decr.u16.low
4103 + env->icount_extra);
4104 env->icount_decr.u32 = 0;
4105 env->icount_extra = 0;
4106 }
4107 return ret;
4108}
4109
aliguorie6e35b12009-04-24 18:03:57 +00004110static void tcg_cpu_exec(void)
4111{
aliguorid6dc3d42009-04-24 18:04:07 +00004112 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00004113
4114 if (next_cpu == NULL)
4115 next_cpu = first_cpu;
4116 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4117 CPUState *env = cur_cpu = next_cpu;
4118
4119 if (!vm_running)
4120 break;
4121 if (timer_alarm_pending) {
4122 timer_alarm_pending = 0;
4123 break;
4124 }
aliguorid6dc3d42009-04-24 18:04:07 +00004125 if (cpu_can_run(env))
4126 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004127 if (ret == EXCP_DEBUG) {
4128 gdb_set_stop_cpu(env);
4129 debug_requested = 1;
4130 break;
4131 }
4132 }
4133}
4134
aliguori43b96852009-04-24 18:03:33 +00004135static int cpu_has_work(CPUState *env)
4136{
aliguorid6dc3d42009-04-24 18:04:07 +00004137 if (env->stop)
4138 return 1;
4139 if (env->stopped)
4140 return 0;
aliguori43b96852009-04-24 18:03:33 +00004141 if (!env->halted)
4142 return 1;
4143 if (qemu_cpu_has_work(env))
4144 return 1;
4145 return 0;
4146}
4147
4148static int tcg_has_work(void)
4149{
bellard6a00d602005-11-21 23:25:50 +00004150 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004151
aliguori43b96852009-04-24 18:03:33 +00004152 for (env = first_cpu; env != NULL; env = env->next_cpu)
4153 if (cpu_has_work(env))
4154 return 1;
4155 return 0;
4156}
bellard15a76442005-11-23 21:01:03 +00004157
aliguori43b96852009-04-24 18:03:33 +00004158static int qemu_calculate_timeout(void)
4159{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004160#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004161 int timeout;
bellard15a76442005-11-23 21:01:03 +00004162
aliguori43b96852009-04-24 18:03:33 +00004163 if (!vm_running)
4164 timeout = 5000;
4165 else if (tcg_has_work())
4166 timeout = 0;
4167 else if (!use_icount)
4168 timeout = 5000;
4169 else {
4170 /* XXX: use timeout computed from timers */
4171 int64_t add;
4172 int64_t delta;
4173 /* Advance virtual time to the next event. */
4174 if (use_icount == 1) {
4175 /* When not using an adaptive execution frequency
4176 we tend to get badly out of sync with real time,
4177 so just delay for a reasonable amount of time. */
4178 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004179 } else {
aliguori43b96852009-04-24 18:03:33 +00004180 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004181 }
aliguori43b96852009-04-24 18:03:33 +00004182 if (delta > 0) {
4183 /* If virtual time is ahead of real time then just
4184 wait for IO. */
4185 timeout = (delta / 1000000) + 1;
4186 } else {
4187 /* Wait for either IO to occur or the next
4188 timer event. */
4189 add = qemu_next_deadline();
4190 /* We advance the timer before checking for IO.
4191 Limit the amount we advance so that early IO
4192 activity won't get the guest too far ahead. */
4193 if (add > 10000000)
4194 add = 10000000;
4195 delta += add;
4196 add = (add + (1 << icount_time_shift) - 1)
4197 >> icount_time_shift;
4198 qemu_icount += add;
4199 timeout = delta / 1000000;
4200 if (timeout < 0)
4201 timeout = 0;
4202 }
bellardb4608c02003-06-27 17:34:32 +00004203 }
aliguori43b96852009-04-24 18:03:33 +00004204
4205 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004206#else /* CONFIG_IOTHREAD */
4207 return 1000;
4208#endif
aliguori43b96852009-04-24 18:03:33 +00004209}
4210
4211static int vm_can_run(void)
4212{
4213 if (powerdown_requested)
4214 return 0;
4215 if (reset_requested)
4216 return 0;
4217 if (shutdown_requested)
4218 return 0;
aliguorie5689022009-04-24 18:03:54 +00004219 if (debug_requested)
4220 return 0;
aliguori43b96852009-04-24 18:03:33 +00004221 return 1;
4222}
4223
Blue Swirld9c32312009-08-09 08:42:19 +00004224qemu_irq qemu_system_powerdown;
4225
aliguori43b96852009-04-24 18:03:33 +00004226static void main_loop(void)
4227{
aliguori6e29f5d2009-04-24 18:04:02 +00004228 int r;
aliguori43b96852009-04-24 18:03:33 +00004229
aliguorid6dc3d42009-04-24 18:04:07 +00004230#ifdef CONFIG_IOTHREAD
4231 qemu_system_ready = 1;
4232 qemu_cond_broadcast(&qemu_system_cond);
4233#endif
4234
aliguori6e29f5d2009-04-24 18:04:02 +00004235 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004236 do {
4237#ifdef CONFIG_PROFILER
4238 int64_t ti;
4239#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004240#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004241 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004242#endif
aliguori43b96852009-04-24 18:03:33 +00004243#ifdef CONFIG_PROFILER
4244 ti = profile_getclock();
4245#endif
4246 main_loop_wait(qemu_calculate_timeout());
4247#ifdef CONFIG_PROFILER
4248 dev_time += profile_getclock() - ti;
4249#endif
aliguorie5689022009-04-24 18:03:54 +00004250 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004251
aliguorie5689022009-04-24 18:03:54 +00004252 if (qemu_debug_requested())
aliguori43b96852009-04-24 18:03:33 +00004253 vm_stop(EXCP_DEBUG);
aliguori43b96852009-04-24 18:03:33 +00004254 if (qemu_shutdown_requested()) {
4255 if (no_shutdown) {
4256 vm_stop(0);
4257 no_shutdown = 0;
4258 } else
4259 break;
4260 }
aliguorid6dc3d42009-04-24 18:04:07 +00004261 if (qemu_reset_requested()) {
4262 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004263 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004264 resume_all_vcpus();
4265 }
Blue Swirld9c32312009-08-09 08:42:19 +00004266 if (qemu_powerdown_requested()) {
4267 qemu_irq_raise(qemu_system_powerdown);
4268 }
aliguori6e29f5d2009-04-24 18:04:02 +00004269 if ((r = qemu_vmstop_requested()))
4270 vm_stop(r);
aliguori43b96852009-04-24 18:03:33 +00004271 }
aliguorid6dc3d42009-04-24 18:04:07 +00004272 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004273}
4274
pbrook9bd7e6d2009-04-07 22:58:45 +00004275static void version(void)
4276{
pbrook4a19f1e2009-04-07 23:17:49 +00004277 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004278}
4279
ths15f82202007-06-29 23:26:08 +00004280static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004281{
pbrook9bd7e6d2009-04-07 22:58:45 +00004282 version();
4283 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004284 "\n"
bellarda20dd502003-09-30 21:07:02 +00004285 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004286 "\n"
blueswir15824d652009-03-28 06:44:27 +00004287#define DEF(option, opt_arg, opt_enum, opt_help) \
4288 opt_help
4289#define DEFHEADING(text) stringify(text) "\n"
4290#include "qemu-options.h"
4291#undef DEF
4292#undef DEFHEADING
4293#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004294 "\n"
bellard82c643f2004-07-14 17:28:13 +00004295 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004296 "ctrl-alt-f toggle full screen\n"
4297 "ctrl-alt-n switch to virtual console 'n'\n"
4298 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004299 "\n"
4300 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4301 ,
bellard0db63472003-10-27 21:37:46 +00004302 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004303 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004304#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004305 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004306 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004307#endif
bellard6e44ba72004-01-18 21:56:49 +00004308 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004309 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004310 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004311}
4312
bellardcd6f1162004-05-13 22:02:20 +00004313#define HAS_ARG 0x0001
4314
4315enum {
blueswir15824d652009-03-28 06:44:27 +00004316#define DEF(option, opt_arg, opt_enum, opt_help) \
4317 opt_enum,
4318#define DEFHEADING(text)
4319#include "qemu-options.h"
4320#undef DEF
4321#undef DEFHEADING
4322#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004323};
4324
4325typedef struct QEMUOption {
4326 const char *name;
4327 int flags;
4328 int index;
4329} QEMUOption;
4330
blueswir1dbed7e42008-10-01 19:38:09 +00004331static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004332 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004333#define DEF(option, opt_arg, opt_enum, opt_help) \
4334 { option, opt_arg, opt_enum },
4335#define DEFHEADING(text)
4336#include "qemu-options.h"
4337#undef DEF
4338#undef DEFHEADING
4339#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004340 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004341};
4342
bellard1d14ffa2005-10-30 18:58:22 +00004343#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004344struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004345#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004346#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004347 {
4348 "pcspk",
4349 "PC speaker",
4350 0,
4351 1,
4352 { .init_isa = pcspk_audio_init }
4353 },
4354#endif
malc4c9b53e2009-01-09 10:46:34 +00004355
4356#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004357 {
4358 "sb16",
4359 "Creative Sound Blaster 16",
4360 0,
4361 1,
4362 { .init_isa = SB16_init }
4363 },
malc4c9b53e2009-01-09 10:46:34 +00004364#endif
bellard6a36d842005-12-18 20:34:32 +00004365
malccc53d262008-06-13 10:48:22 +00004366#ifdef CONFIG_CS4231A
4367 {
4368 "cs4231a",
4369 "CS4231A",
4370 0,
4371 1,
4372 { .init_isa = cs4231a_init }
4373 },
4374#endif
4375
bellard6a36d842005-12-18 20:34:32 +00004376#ifdef CONFIG_ADLIB
4377 {
4378 "adlib",
4379#ifdef HAS_YMF262
4380 "Yamaha YMF262 (OPL3)",
4381#else
4382 "Yamaha YM3812 (OPL2)",
4383#endif
4384 0,
4385 1,
4386 { .init_isa = Adlib_init }
4387 },
4388#endif
4389
4390#ifdef CONFIG_GUS
4391 {
4392 "gus",
4393 "Gravis Ultrasound GF1",
4394 0,
4395 1,
4396 { .init_isa = GUS_init }
4397 },
4398#endif
4399
malc4c9b53e2009-01-09 10:46:34 +00004400#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004401 {
4402 "ac97",
4403 "Intel 82801AA AC97 Audio",
4404 0,
4405 0,
4406 { .init_pci = ac97_init }
4407 },
malc4c9b53e2009-01-09 10:46:34 +00004408#endif
balroge5c9a132008-01-14 04:27:55 +00004409
malc4c9b53e2009-01-09 10:46:34 +00004410#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004411 {
4412 "es1370",
4413 "ENSONIQ AudioPCI ES1370",
4414 0,
4415 0,
4416 { .init_pci = es1370_init }
4417 },
balrogb00052e2007-04-30 02:22:06 +00004418#endif
bellard6a36d842005-12-18 20:34:32 +00004419
malc4c9b53e2009-01-09 10:46:34 +00004420#endif /* HAS_AUDIO_CHOICE */
4421
bellard6a36d842005-12-18 20:34:32 +00004422 { NULL, NULL, 0, 0, { NULL } }
4423};
4424
bellard1d14ffa2005-10-30 18:58:22 +00004425static void select_soundhw (const char *optarg)
4426{
bellard6a36d842005-12-18 20:34:32 +00004427 struct soundhw *c;
4428
bellard1d14ffa2005-10-30 18:58:22 +00004429 if (*optarg == '?') {
4430 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004431
bellard1d14ffa2005-10-30 18:58:22 +00004432 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004433 for (c = soundhw; c->name; ++c) {
4434 printf ("%-11s %s\n", c->name, c->descr);
4435 }
4436 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004437 exit (*optarg != '?');
4438 }
4439 else {
bellard6a36d842005-12-18 20:34:32 +00004440 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004441 const char *p;
4442 char *e;
4443 int bad_card = 0;
4444
bellard6a36d842005-12-18 20:34:32 +00004445 if (!strcmp (optarg, "all")) {
4446 for (c = soundhw; c->name; ++c) {
4447 c->enabled = 1;
4448 }
4449 return;
4450 }
bellard1d14ffa2005-10-30 18:58:22 +00004451
bellard6a36d842005-12-18 20:34:32 +00004452 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004453 while (*p) {
4454 e = strchr (p, ',');
4455 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004456
4457 for (c = soundhw; c->name; ++c) {
4458 if (!strncmp (c->name, p, l)) {
4459 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004460 break;
4461 }
4462 }
bellard6a36d842005-12-18 20:34:32 +00004463
4464 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004465 if (l > 80) {
4466 fprintf (stderr,
4467 "Unknown sound card name (too big to show)\n");
4468 }
4469 else {
4470 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4471 (int) l, p);
4472 }
4473 bad_card = 1;
4474 }
4475 p += l + (e != NULL);
4476 }
4477
4478 if (bad_card)
4479 goto show_valid_cards;
4480 }
4481}
4482#endif
4483
malc3893c122008-09-28 00:42:05 +00004484static void select_vgahw (const char *p)
4485{
4486 const char *opts;
4487
Zachary Amsden86176752009-07-30 00:15:02 -10004488 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00004489 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004490 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00004491 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004492 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00004493 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004494 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00004495 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004496 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00004497 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004498 invalid_vga:
4499 fprintf(stderr, "Unknown vga type: %s\n", p);
4500 exit(1);
4501 }
malccb5a7aa2008-09-28 00:42:12 +00004502 while (*opts) {
4503 const char *nextopt;
4504
4505 if (strstart(opts, ",retrace=", &nextopt)) {
4506 opts = nextopt;
4507 if (strstart(opts, "dumb", &nextopt))
4508 vga_retrace_method = VGA_RETRACE_DUMB;
4509 else if (strstart(opts, "precise", &nextopt))
4510 vga_retrace_method = VGA_RETRACE_PRECISE;
4511 else goto invalid_vga;
4512 } else goto invalid_vga;
4513 opts = nextopt;
4514 }
malc3893c122008-09-28 00:42:05 +00004515}
4516
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004517#ifdef TARGET_I386
4518static int balloon_parse(const char *arg)
4519{
4520 char buf[128];
4521 const char *p;
4522
4523 if (!strcmp(arg, "none")) {
4524 virtio_balloon = 0;
4525 } else if (!strncmp(arg, "virtio", 6)) {
4526 virtio_balloon = 1;
4527 if (arg[6] == ',') {
4528 p = arg + 7;
4529 if (get_param_value(buf, sizeof(buf), "addr", p)) {
4530 virtio_balloon_devaddr = strdup(buf);
4531 }
4532 }
4533 } else {
4534 return -1;
4535 }
4536 return 0;
4537}
4538#endif
4539
bellard3587d7e2006-06-26 20:03:44 +00004540#ifdef _WIN32
4541static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4542{
4543 exit(STATUS_CONTROL_C_EXIT);
4544 return TRUE;
4545}
4546#endif
4547
aliguoric4be29f2009-04-17 18:58:14 +00004548int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004549{
4550 int ret;
4551
4552 if(strlen(str) != 36)
4553 return -1;
4554
4555 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4556 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4557 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4558
4559 if(ret != 16)
4560 return -1;
4561
aliguorib6f6e3d2009-04-17 18:59:56 +00004562#ifdef TARGET_I386
4563 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4564#endif
4565
blueswir18fcb1b92008-09-18 18:29:08 +00004566 return 0;
4567}
4568
bellard7c9d8e02005-11-15 22:16:05 +00004569#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004570
aliguori5b08fc12008-08-21 20:08:03 +00004571#ifndef _WIN32
4572
4573static void termsig_handler(int signal)
4574{
4575 qemu_system_shutdown_request();
4576}
4577
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004578static void sigchld_handler(int signal)
4579{
4580 waitpid(-1, NULL, WNOHANG);
4581}
4582
4583static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004584{
4585 struct sigaction act;
4586
4587 memset(&act, 0, sizeof(act));
4588 act.sa_handler = termsig_handler;
4589 sigaction(SIGINT, &act, NULL);
4590 sigaction(SIGHUP, &act, NULL);
4591 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004592
4593 act.sa_handler = sigchld_handler;
4594 act.sa_flags = SA_NOCLDSTOP;
4595 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004596}
4597
4598#endif
4599
Paul Brook5cea8592009-05-30 00:52:44 +01004600#ifdef _WIN32
4601/* Look for support files in the same directory as the executable. */
4602static char *find_datadir(const char *argv0)
4603{
4604 char *p;
4605 char buf[MAX_PATH];
4606 DWORD len;
4607
4608 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4609 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004610 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004611 }
4612
4613 buf[len] = 0;
4614 p = buf + len - 1;
4615 while (p != buf && *p != '\\')
4616 p--;
4617 *p = 0;
4618 if (access(buf, R_OK) == 0) {
4619 return qemu_strdup(buf);
4620 }
4621 return NULL;
4622}
4623#else /* !_WIN32 */
4624
4625/* Find a likely location for support files using the location of the binary.
4626 For installed binaries this will be "$bindir/../share/qemu". When
4627 running from the build tree this will be "$bindir/../pc-bios". */
4628#define SHARE_SUFFIX "/share/qemu"
4629#define BUILD_SUFFIX "/pc-bios"
4630static char *find_datadir(const char *argv0)
4631{
4632 char *dir;
4633 char *p = NULL;
4634 char *res;
4635#ifdef PATH_MAX
4636 char buf[PATH_MAX];
4637#endif
Blue Swirl3a417592009-06-09 19:12:21 +00004638 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004639
4640#if defined(__linux__)
4641 {
4642 int len;
4643 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4644 if (len > 0) {
4645 buf[len] = 0;
4646 p = buf;
4647 }
4648 }
4649#elif defined(__FreeBSD__)
4650 {
4651 int len;
4652 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4653 if (len > 0) {
4654 buf[len] = 0;
4655 p = buf;
4656 }
4657 }
4658#endif
4659 /* If we don't have any way of figuring out the actual executable
4660 location then try argv[0]. */
4661 if (!p) {
4662#ifdef PATH_MAX
4663 p = buf;
4664#endif
4665 p = realpath(argv0, p);
4666 if (!p) {
4667 return NULL;
4668 }
4669 }
4670 dir = dirname(p);
4671 dir = dirname(dir);
4672
Blue Swirl3a417592009-06-09 19:12:21 +00004673 max_len = strlen(dir) +
4674 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4675 res = qemu_mallocz(max_len);
4676 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004677 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004678 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004679 if (access(res, R_OK)) {
4680 qemu_free(res);
4681 res = NULL;
4682 }
4683 }
4684#ifndef PATH_MAX
4685 free(p);
4686#endif
4687 return res;
4688}
4689#undef SHARE_SUFFIX
4690#undef BUILD_SUFFIX
4691#endif
4692
4693char *qemu_find_file(int type, const char *name)
4694{
4695 int len;
4696 const char *subdir;
4697 char *buf;
4698
4699 /* If name contains path separators then try it as a straight path. */
4700 if ((strchr(name, '/') || strchr(name, '\\'))
4701 && access(name, R_OK) == 0) {
4702 return strdup(name);
4703 }
4704 switch (type) {
4705 case QEMU_FILE_TYPE_BIOS:
4706 subdir = "";
4707 break;
4708 case QEMU_FILE_TYPE_KEYMAP:
4709 subdir = "keymaps/";
4710 break;
4711 default:
4712 abort();
4713 }
4714 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4715 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004716 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004717 if (access(buf, R_OK)) {
4718 qemu_free(buf);
4719 return NULL;
4720 }
4721 return buf;
4722}
4723
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004724static int device_init_func(QemuOpts *opts, void *opaque)
4725{
4726 DeviceState *dev;
4727
4728 dev = qdev_device_add(opts);
4729 if (!dev)
4730 return -1;
4731 return 0;
4732}
4733
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004734struct device_config {
4735 enum {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004736 DEV_USB, /* -usbdevice */
4737 DEV_BT, /* -bt */
4738 } type;
4739 const char *cmdline;
4740 TAILQ_ENTRY(device_config) next;
4741};
4742TAILQ_HEAD(, device_config) device_configs = TAILQ_HEAD_INITIALIZER(device_configs);
4743
4744static void add_device_config(int type, const char *cmdline)
4745{
4746 struct device_config *conf;
4747
4748 conf = qemu_mallocz(sizeof(*conf));
4749 conf->type = type;
4750 conf->cmdline = cmdline;
4751 TAILQ_INSERT_TAIL(&device_configs, conf, next);
4752}
4753
4754static int foreach_device_config(int type, int (*func)(const char *cmdline))
4755{
4756 struct device_config *conf;
4757 int rc;
4758
4759 TAILQ_FOREACH(conf, &device_configs, next) {
4760 if (conf->type != type)
4761 continue;
4762 rc = func(conf->cmdline);
4763 if (0 != rc)
4764 return rc;
4765 }
4766 return 0;
4767}
4768
malc902b3d52008-12-10 19:18:40 +00004769int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004770{
aliguori59030a82009-04-05 18:43:41 +00004771 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004772 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004773 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004774 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004775 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004776 const char *kernel_filename, *kernel_cmdline;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02004777 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004778 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004779 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004780 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004781 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004782 int nb_net_clients;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004783 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00004784 int optind;
4785 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004786 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004787 const char *monitor_device;
4788 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004789 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004790 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004791 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004792 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4793 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004794 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004795 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004796 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004797#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004798 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004799#endif
bellard26a5f132008-05-28 12:30:31 +00004800 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004801 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004802 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004803#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004804 int fd = 0;
4805 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004806 const char *chroot_dir = NULL;
4807 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004808#endif
aliguori268a3622009-04-21 22:30:27 +00004809 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004810 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004811
malc902b3d52008-12-10 19:18:40 +00004812 qemu_cache_utils_init(envp);
4813
bellard0bd48852005-11-11 00:00:47 +00004814 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004815#ifndef _WIN32
4816 {
4817 struct sigaction act;
4818 sigfillset(&act.sa_mask);
4819 act.sa_flags = 0;
4820 act.sa_handler = SIG_IGN;
4821 sigaction(SIGPIPE, &act, NULL);
4822 }
bellard3587d7e2006-06-26 20:03:44 +00004823#else
4824 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004825 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4826 QEMU to run on a single CPU */
4827 {
4828 HANDLE h;
4829 DWORD mask, smask;
4830 int i;
4831 h = GetCurrentProcess();
4832 if (GetProcessAffinityMask(h, &mask, &smask)) {
4833 for(i = 0; i < 32; i++) {
4834 if (mask & (1 << i))
4835 break;
4836 }
4837 if (i != 32) {
4838 mask = 1 << i;
4839 SetProcessAffinityMask(h, mask);
4840 }
4841 }
4842 }
bellard67b915a2004-03-31 23:37:16 +00004843#endif
bellardbe995c22006-06-25 16:25:21 +00004844
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004845 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004846 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004847 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004848 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004849 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004850 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004851 kernel_filename = NULL;
4852 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004853 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004854 translation = BIOS_ATA_TRANSLATION_AUTO;
aliguorid47d13b2009-03-05 23:00:53 +00004855 monitor_device = "vc:80Cx24C";
bellardc4b1fcc2004-03-14 21:44:30 +00004856
aurel32c75a8232008-05-04 00:50:34 +00004857 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004858 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004859 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004860 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004861
aliguori8290edd2009-02-27 20:14:29 +00004862 parallel_devices[0] = "vc:80Cx24C";
bellard6508fe52005-01-15 12:02:56 +00004863 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004864 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004865 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004866
aliguori1b8fc812009-02-27 20:01:39 +00004867 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004868 virtio_consoles[i] = NULL;
4869 virtio_console_index = 0;
4870
aliguori268a3622009-04-21 22:30:27 +00004871 for (i = 0; i < MAX_NODES; i++) {
4872 node_mem[i] = 0;
4873 node_cpumask[i] = 0;
4874 }
4875
bellard7c9d8e02005-11-15 22:16:05 +00004876 nb_net_clients = 0;
aliguori268a3622009-04-21 22:30:27 +00004877 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004878 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004879
bellard26a5f132008-05-28 12:30:31 +00004880 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004881 autostart= 1;
4882
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01004883 register_watchdogs();
4884
bellardcd6f1162004-05-13 22:02:20 +00004885 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004886 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004887 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004888 break;
bellardcd6f1162004-05-13 22:02:20 +00004889 r = argv[optind];
4890 if (r[0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004891 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004892 } else {
4893 const QEMUOption *popt;
4894
4895 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004896 /* Treat --foo the same as -foo. */
4897 if (r[1] == '-')
4898 r++;
bellardcd6f1162004-05-13 22:02:20 +00004899 popt = qemu_options;
4900 for(;;) {
4901 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004902 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004903 argv[0], r);
4904 exit(1);
4905 }
4906 if (!strcmp(popt->name, r + 1))
4907 break;
4908 popt++;
4909 }
4910 if (popt->flags & HAS_ARG) {
4911 if (optind >= argc) {
4912 fprintf(stderr, "%s: option '%s' requires an argument\n",
4913 argv[0], r);
4914 exit(1);
4915 }
4916 optarg = argv[optind++];
4917 } else {
4918 optarg = NULL;
4919 }
4920
4921 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004922 case QEMU_OPTION_M:
4923 machine = find_machine(optarg);
4924 if (!machine) {
4925 QEMUMachine *m;
4926 printf("Supported machines are:\n");
4927 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004928 if (m->alias)
4929 printf("%-10s %s (alias of %s)\n",
4930 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004931 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004932 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004933 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004934 }
ths15f82202007-06-29 23:26:08 +00004935 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004936 }
4937 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004938 case QEMU_OPTION_cpu:
4939 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004940 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004941/* XXX: implement xxx_cpu_list for targets that still miss it */
4942#if defined(cpu_list)
4943 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004944#endif
ths15f82202007-06-29 23:26:08 +00004945 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004946 } else {
4947 cpu_model = optarg;
4948 }
4949 break;
bellardcd6f1162004-05-13 22:02:20 +00004950 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004951 initrd_filename = optarg;
4952 break;
bellardcd6f1162004-05-13 22:02:20 +00004953 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004954 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004955 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004956 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004957 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004958 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004959 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004960 translation == BIOS_ATA_TRANSLATION_LBA ?
4961 ",trans=lba" :
4962 translation == BIOS_ATA_TRANSLATION_NONE ?
4963 ",trans=none" : "");
4964 break;
bellardcd6f1162004-05-13 22:02:20 +00004965 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004966 case QEMU_OPTION_hdc:
4967 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004968 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004969 break;
thse4bcb142007-12-02 04:51:10 +00004970 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004971 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004972 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02004973 case QEMU_OPTION_set:
4974 if (qemu_set_option(optarg) != 0)
4975 exit(1);
4976 break;
balrog3e3d5812007-04-30 02:09:25 +00004977 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004978 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004979 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004980 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004981 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004982 break;
j_mayer86f55662007-04-24 06:52:59 +00004983 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004984 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004985 break;
bellardcd6f1162004-05-13 22:02:20 +00004986 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004987 snapshot = 1;
4988 break;
bellardcd6f1162004-05-13 22:02:20 +00004989 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004990 {
bellard330d0412003-07-26 18:11:40 +00004991 const char *p;
4992 p = optarg;
4993 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004994 if (cyls < 1 || cyls > 16383)
4995 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004996 if (*p != ',')
4997 goto chs_fail;
4998 p++;
4999 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005000 if (heads < 1 || heads > 16)
5001 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005002 if (*p != ',')
5003 goto chs_fail;
5004 p++;
5005 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005006 if (secs < 1 || secs > 63)
5007 goto chs_fail;
5008 if (*p == ',') {
5009 p++;
5010 if (!strcmp(p, "none"))
5011 translation = BIOS_ATA_TRANSLATION_NONE;
5012 else if (!strcmp(p, "lba"))
5013 translation = BIOS_ATA_TRANSLATION_LBA;
5014 else if (!strcmp(p, "auto"))
5015 translation = BIOS_ATA_TRANSLATION_AUTO;
5016 else
5017 goto chs_fail;
5018 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00005019 chs_fail:
bellard46d47672004-11-16 01:45:27 +00005020 fprintf(stderr, "qemu: invalid physical CHS format\n");
5021 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00005022 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005023 if (hda_opts != NULL) {
5024 char num[16];
5025 snprintf(num, sizeof(num), "%d", cyls);
5026 qemu_opt_set(hda_opts, "cyls", num);
5027 snprintf(num, sizeof(num), "%d", heads);
5028 qemu_opt_set(hda_opts, "heads", num);
5029 snprintf(num, sizeof(num), "%d", secs);
5030 qemu_opt_set(hda_opts, "secs", num);
5031 if (translation == BIOS_ATA_TRANSLATION_LBA)
5032 qemu_opt_set(hda_opts, "trans", "lba");
5033 if (translation == BIOS_ATA_TRANSLATION_NONE)
5034 qemu_opt_set(hda_opts, "trans", "none");
5035 }
bellard330d0412003-07-26 18:11:40 +00005036 }
5037 break;
aliguori268a3622009-04-21 22:30:27 +00005038 case QEMU_OPTION_numa:
5039 if (nb_numa_nodes >= MAX_NODES) {
5040 fprintf(stderr, "qemu: too many NUMA nodes\n");
5041 exit(1);
5042 }
5043 numa_add(optarg);
5044 break;
bellardcd6f1162004-05-13 22:02:20 +00005045 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005046 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005047 break;
balrog4d3b6f62008-02-10 16:33:14 +00005048#ifdef CONFIG_CURSES
5049 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005050 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005051 break;
5052#endif
balroga171fe32007-04-30 01:48:07 +00005053 case QEMU_OPTION_portrait:
5054 graphic_rotate = 1;
5055 break;
bellardcd6f1162004-05-13 22:02:20 +00005056 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005057 kernel_filename = optarg;
5058 break;
bellardcd6f1162004-05-13 22:02:20 +00005059 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005060 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005061 break;
bellardcd6f1162004-05-13 22:02:20 +00005062 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005063 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005064 break;
bellardcd6f1162004-05-13 22:02:20 +00005065 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005066 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005067 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02005068 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005069 };
5070 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005071 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005072 int legacy = 0;
5073
5074 if (!strchr(optarg, '=')) {
5075 legacy = 1;
5076 pstrcpy(buf, sizeof(buf), optarg);
5077 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5078 fprintf(stderr,
5079 "qemu: unknown boot parameter '%s' in '%s'\n",
5080 buf, optarg);
5081 exit(1);
5082 }
5083
5084 if (legacy ||
5085 get_param_value(buf, sizeof(buf), "order", optarg)) {
5086 boot_devices_bitmap = parse_bootdevices(buf);
5087 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005088 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005089 if (!legacy) {
5090 if (get_param_value(buf, sizeof(buf),
5091 "once", optarg)) {
5092 boot_devices_bitmap |= parse_bootdevices(buf);
5093 standard_boot_devices = qemu_strdup(boot_devices);
5094 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5095 qemu_register_reset(restore_boot_devices,
5096 standard_boot_devices);
5097 }
Jan Kiszka95387492009-07-02 00:19:02 +02005098 if (get_param_value(buf, sizeof(buf),
5099 "menu", optarg)) {
5100 if (!strcmp(buf, "on")) {
5101 boot_menu = 1;
5102 } else if (!strcmp(buf, "off")) {
5103 boot_menu = 0;
5104 } else {
5105 fprintf(stderr,
5106 "qemu: invalid option value '%s'\n",
5107 buf);
5108 exit(1);
5109 }
5110 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005111 }
bellard36b486b2003-11-11 13:36:08 +00005112 }
5113 break;
bellardcd6f1162004-05-13 22:02:20 +00005114 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005115 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005116 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005117 break;
bellard52ca8d62006-06-14 16:03:05 +00005118#ifdef TARGET_I386
5119 case QEMU_OPTION_no_fd_bootchk:
5120 fd_bootchk = 0;
5121 break;
5122#endif
bellard7c9d8e02005-11-15 22:16:05 +00005123 case QEMU_OPTION_net:
5124 if (nb_net_clients >= MAX_NET_CLIENTS) {
5125 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00005126 exit(1);
5127 }
pbrookfd5f3932008-03-26 20:55:43 +00005128 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00005129 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00005130 break;
bellardc7f74642004-08-24 21:57:12 +00005131#ifdef CONFIG_SLIRP
5132 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005133 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005134 break;
ths47d5d012007-02-20 00:05:08 +00005135 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005136 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005137 break;
bellardc94c8d62004-09-13 21:37:34 +00005138#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005139 case QEMU_OPTION_smb:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005140 net_slirp_smb(optarg);
bellard9d728e82004-09-05 23:09:03 +00005141 break;
bellardc94c8d62004-09-13 21:37:34 +00005142#endif
bellard9bf05442004-08-25 22:12:49 +00005143 case QEMU_OPTION_redir:
Jan Kiszkaf3546de2009-06-24 14:42:28 +02005144 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00005145 break;
bellardc7f74642004-08-24 21:57:12 +00005146#endif
balrogdc72ac12008-11-09 00:04:26 +00005147 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005148 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00005149 break;
bellard1d14ffa2005-10-30 18:58:22 +00005150#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005151 case QEMU_OPTION_audio_help:
5152 AUD_help ();
5153 exit (0);
5154 break;
5155 case QEMU_OPTION_soundhw:
5156 select_soundhw (optarg);
5157 break;
5158#endif
bellardcd6f1162004-05-13 22:02:20 +00005159 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005160 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005161 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005162 case QEMU_OPTION_version:
5163 version();
5164 exit(0);
5165 break;
aurel3200f82b82008-04-27 21:12:55 +00005166 case QEMU_OPTION_m: {
5167 uint64_t value;
5168 char *ptr;
5169
5170 value = strtoul(optarg, &ptr, 10);
5171 switch (*ptr) {
5172 case 0: case 'M': case 'm':
5173 value <<= 20;
5174 break;
5175 case 'G': case 'g':
5176 value <<= 30;
5177 break;
5178 default:
5179 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005180 exit(1);
5181 }
aurel3200f82b82008-04-27 21:12:55 +00005182
5183 /* On 32-bit hosts, QEMU is limited by virtual address space */
5184 if (value > (2047 << 20)
blueswir1640f42e2009-04-19 10:18:01 +00005185#ifndef CONFIG_KQEMU
aurel3200f82b82008-04-27 21:12:55 +00005186 && HOST_LONG_BITS == 32
5187#endif
5188 ) {
5189 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5190 exit(1);
5191 }
5192 if (value != (uint64_t)(ram_addr_t)value) {
5193 fprintf(stderr, "qemu: ram size too large\n");
5194 exit(1);
5195 }
5196 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005197 break;
aurel3200f82b82008-04-27 21:12:55 +00005198 }
bellardcd6f1162004-05-13 22:02:20 +00005199 case QEMU_OPTION_d:
5200 {
5201 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005202 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005203
bellardcd6f1162004-05-13 22:02:20 +00005204 mask = cpu_str_to_log_mask(optarg);
5205 if (!mask) {
5206 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005207 for(item = cpu_log_items; item->mask != 0; item++) {
5208 printf("%-10s %s\n", item->name, item->help);
5209 }
5210 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005211 }
5212 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005213 }
bellardcd6f1162004-05-13 22:02:20 +00005214 break;
bellardcd6f1162004-05-13 22:02:20 +00005215 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005216 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005217 break;
aliguori59030a82009-04-05 18:43:41 +00005218 case QEMU_OPTION_gdb:
5219 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005220 break;
bellardcd6f1162004-05-13 22:02:20 +00005221 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005222 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005223 break;
j_mayer1192dad2007-10-05 13:08:35 +00005224 case QEMU_OPTION_bios:
5225 bios_name = optarg;
5226 break;
aurel321b530a62009-04-05 20:08:59 +00005227 case QEMU_OPTION_singlestep:
5228 singlestep = 1;
5229 break;
bellardcd6f1162004-05-13 22:02:20 +00005230 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005231 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005232 break;
blueswir15824d652009-03-28 06:44:27 +00005233#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00005234 case QEMU_OPTION_k:
5235 keyboard_layout = optarg;
5236 break;
blueswir15824d652009-03-28 06:44:27 +00005237#endif
bellardee22c2f2004-06-03 12:49:50 +00005238 case QEMU_OPTION_localtime:
5239 rtc_utc = 0;
5240 break;
malc3893c122008-09-28 00:42:05 +00005241 case QEMU_OPTION_vga:
5242 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005243 break;
blueswir15824d652009-03-28 06:44:27 +00005244#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005245 case QEMU_OPTION_g:
5246 {
5247 const char *p;
5248 int w, h, depth;
5249 p = optarg;
5250 w = strtol(p, (char **)&p, 10);
5251 if (w <= 0) {
5252 graphic_error:
5253 fprintf(stderr, "qemu: invalid resolution or depth\n");
5254 exit(1);
5255 }
5256 if (*p != 'x')
5257 goto graphic_error;
5258 p++;
5259 h = strtol(p, (char **)&p, 10);
5260 if (h <= 0)
5261 goto graphic_error;
5262 if (*p == 'x') {
5263 p++;
5264 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005265 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005266 depth != 24 && depth != 32)
5267 goto graphic_error;
5268 } else if (*p == '\0') {
5269 depth = graphic_depth;
5270 } else {
5271 goto graphic_error;
5272 }
ths3b46e622007-09-17 08:09:54 +00005273
bellarde9b137c2004-06-21 16:46:10 +00005274 graphic_width = w;
5275 graphic_height = h;
5276 graphic_depth = depth;
5277 }
5278 break;
blueswir15824d652009-03-28 06:44:27 +00005279#endif
ths20d8a3e2007-02-18 17:04:49 +00005280 case QEMU_OPTION_echr:
5281 {
5282 char *r;
5283 term_escape_char = strtol(optarg, &r, 0);
5284 if (r == optarg)
5285 printf("Bad argument to echr\n");
5286 break;
5287 }
bellard82c643f2004-07-14 17:28:13 +00005288 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005289 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005290 break;
5291 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005292 if (serial_device_index >= MAX_SERIAL_PORTS) {
5293 fprintf(stderr, "qemu: too many serial ports\n");
5294 exit(1);
5295 }
pbrookfd5f3932008-03-26 20:55:43 +00005296 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005297 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005298 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005299 case QEMU_OPTION_watchdog:
5300 i = select_watchdog(optarg);
5301 if (i > 0)
5302 exit (i == 1 ? 1 : 0);
5303 break;
5304 case QEMU_OPTION_watchdog_action:
5305 if (select_watchdog_action(optarg) == -1) {
5306 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5307 exit(1);
5308 }
5309 break;
aliguori51ecf132009-01-15 20:06:40 +00005310 case QEMU_OPTION_virtiocon:
5311 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5312 fprintf(stderr, "qemu: too many virtio consoles\n");
5313 exit(1);
5314 }
5315 virtio_consoles[virtio_console_index] = optarg;
5316 virtio_console_index++;
5317 break;
bellard6508fe52005-01-15 12:02:56 +00005318 case QEMU_OPTION_parallel:
5319 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5320 fprintf(stderr, "qemu: too many parallel ports\n");
5321 exit(1);
5322 }
pbrookfd5f3932008-03-26 20:55:43 +00005323 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005324 parallel_device_index++;
5325 break;
bellardd63d3072004-10-03 13:29:03 +00005326 case QEMU_OPTION_loadvm:
5327 loadvm = optarg;
5328 break;
5329 case QEMU_OPTION_full_screen:
5330 full_screen = 1;
5331 break;
ths667acca2006-12-11 02:08:05 +00005332#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005333 case QEMU_OPTION_no_frame:
5334 no_frame = 1;
5335 break;
ths3780e192007-06-21 21:08:02 +00005336 case QEMU_OPTION_alt_grab:
5337 alt_grab = 1;
5338 break;
ths667acca2006-12-11 02:08:05 +00005339 case QEMU_OPTION_no_quit:
5340 no_quit = 1;
5341 break;
aliguori7d957bd2009-01-15 22:14:11 +00005342 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005343 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005344 break;
ths667acca2006-12-11 02:08:05 +00005345#endif
bellardf7cce892004-12-08 22:21:25 +00005346 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005347 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005348 break;
bellarda09db212005-04-30 16:10:35 +00005349#ifdef TARGET_I386
5350 case QEMU_OPTION_win2k_hack:
5351 win2k_install_hack = 1;
5352 break;
aliguori73822ec2009-01-15 20:11:34 +00005353 case QEMU_OPTION_rtc_td_hack:
5354 rtc_td_hack = 1;
5355 break;
aliguori8a92ea22009-02-27 20:12:36 +00005356 case QEMU_OPTION_acpitable:
5357 if(acpi_table_add(optarg) < 0) {
5358 fprintf(stderr, "Wrong acpi table provided\n");
5359 exit(1);
5360 }
5361 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005362 case QEMU_OPTION_smbios:
5363 if(smbios_entry_add(optarg) < 0) {
5364 fprintf(stderr, "Wrong smbios provided\n");
5365 exit(1);
5366 }
5367 break;
bellarda09db212005-04-30 16:10:35 +00005368#endif
blueswir1640f42e2009-04-19 10:18:01 +00005369#ifdef CONFIG_KQEMU
Anthony Liguori52249f02009-07-09 17:28:08 -05005370 case QEMU_OPTION_enable_kqemu:
5371 kqemu_allowed = 1;
bellardd993e022005-02-10 22:00:06 +00005372 break;
bellard89bfc102006-02-08 22:46:31 +00005373 case QEMU_OPTION_kernel_kqemu:
5374 kqemu_allowed = 2;
5375 break;
bellardd993e022005-02-10 22:00:06 +00005376#endif
aliguori7ba1e612008-11-05 16:04:33 +00005377#ifdef CONFIG_KVM
5378 case QEMU_OPTION_enable_kvm:
5379 kvm_allowed = 1;
blueswir1640f42e2009-04-19 10:18:01 +00005380#ifdef CONFIG_KQEMU
aliguori7ba1e612008-11-05 16:04:33 +00005381 kqemu_allowed = 0;
5382#endif
5383 break;
5384#endif
bellardbb36d472005-11-05 14:22:28 +00005385 case QEMU_OPTION_usb:
5386 usb_enabled = 1;
5387 break;
bellarda594cfb2005-11-06 16:13:29 +00005388 case QEMU_OPTION_usbdevice:
5389 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005390 add_device_config(DEV_USB, optarg);
5391 break;
5392 case QEMU_OPTION_device:
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005393 opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver");
5394 if (!opts) {
5395 fprintf(stderr, "parse error: %s\n", optarg);
5396 exit(1);
5397 }
bellarda594cfb2005-11-06 16:13:29 +00005398 break;
bellard6a00d602005-11-21 23:25:50 +00005399 case QEMU_OPTION_smp:
Jes Sorensen6be68d72009-07-23 17:03:42 +02005400 {
5401 char *p;
5402 char option[128];
5403 smp_cpus = strtol(optarg, &p, 10);
aliguorib2097002008-10-07 20:39:39 +00005404 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005405 fprintf(stderr, "Invalid number of CPUs\n");
5406 exit(1);
5407 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005408 if (*p++ != ',')
5409 break;
5410 if (get_param_value(option, 128, "maxcpus", p))
5411 max_cpus = strtol(option, NULL, 0);
5412 if (max_cpus < smp_cpus) {
5413 fprintf(stderr, "maxcpus must be equal to or greater than "
5414 "smp\n");
5415 exit(1);
5416 }
5417 if (max_cpus > 255) {
5418 fprintf(stderr, "Unsupported number of maxcpus\n");
5419 exit(1);
5420 }
bellard6a00d602005-11-21 23:25:50 +00005421 break;
Jes Sorensen6be68d72009-07-23 17:03:42 +02005422 }
bellard24236862006-04-30 21:28:36 +00005423 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005424 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005425 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005426 break;
blueswir15824d652009-03-28 06:44:27 +00005427#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005428 case QEMU_OPTION_no_acpi:
5429 acpi_enabled = 0;
5430 break;
aliguori16b29ae2008-12-17 23:28:44 +00005431 case QEMU_OPTION_no_hpet:
5432 no_hpet = 1;
5433 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005434 case QEMU_OPTION_balloon:
5435 if (balloon_parse(optarg) < 0) {
5436 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5437 exit(1);
5438 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005439 break;
blueswir15824d652009-03-28 06:44:27 +00005440#endif
bellardd1beab82006-10-02 19:44:22 +00005441 case QEMU_OPTION_no_reboot:
5442 no_reboot = 1;
5443 break;
aurel32b2f76162008-04-11 21:35:52 +00005444 case QEMU_OPTION_no_shutdown:
5445 no_shutdown = 1;
5446 break;
balrog9467cd42007-05-01 01:34:14 +00005447 case QEMU_OPTION_show_cursor:
5448 cursor_hide = 0;
5449 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005450 case QEMU_OPTION_uuid:
5451 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5452 fprintf(stderr, "Fail to parse UUID string."
5453 " Wrong format.\n");
5454 exit(1);
5455 }
5456 break;
blueswir15824d652009-03-28 06:44:27 +00005457#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005458 case QEMU_OPTION_daemonize:
5459 daemonize = 1;
5460 break;
blueswir15824d652009-03-28 06:44:27 +00005461#endif
ths9ae02552007-01-05 17:39:04 +00005462 case QEMU_OPTION_option_rom:
5463 if (nb_option_roms >= MAX_OPTION_ROMS) {
5464 fprintf(stderr, "Too many option ROMs\n");
5465 exit(1);
5466 }
5467 option_rom[nb_option_roms] = optarg;
5468 nb_option_roms++;
5469 break;
blueswir15824d652009-03-28 06:44:27 +00005470#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005471 case QEMU_OPTION_semihosting:
5472 semihosting_enabled = 1;
5473 break;
blueswir15824d652009-03-28 06:44:27 +00005474#endif
thsc35734b2007-03-19 15:17:08 +00005475 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005476 qemu_name = qemu_strdup(optarg);
5477 {
5478 char *p = strchr(qemu_name, ',');
5479 if (p != NULL) {
5480 *p++ = 0;
5481 if (strncmp(p, "process=", 8)) {
5482 fprintf(stderr, "Unknown subargument %s to -name", p);
5483 exit(1);
5484 }
5485 p += 8;
5486 set_proc_name(p);
5487 }
5488 }
thsc35734b2007-03-19 15:17:08 +00005489 break;
blueswir195efd112008-12-24 20:26:14 +00005490#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005491 case QEMU_OPTION_prom_env:
5492 if (nb_prom_envs >= MAX_PROM_ENVS) {
5493 fprintf(stderr, "Too many prom variables\n");
5494 exit(1);
5495 }
5496 prom_envs[nb_prom_envs] = optarg;
5497 nb_prom_envs++;
5498 break;
5499#endif
balrog2b8f2d42007-07-27 22:08:46 +00005500#ifdef TARGET_ARM
5501 case QEMU_OPTION_old_param:
5502 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005503 break;
balrog2b8f2d42007-07-27 22:08:46 +00005504#endif
thsf3dcfad2007-08-24 01:26:02 +00005505 case QEMU_OPTION_clock:
5506 configure_alarms(optarg);
5507 break;
bellard7e0af5d02007-11-07 16:24:33 +00005508 case QEMU_OPTION_startdate:
5509 {
5510 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005511 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005512 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005513 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005514 } else {
5515 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5516 &tm.tm_year,
5517 &tm.tm_mon,
5518 &tm.tm_mday,
5519 &tm.tm_hour,
5520 &tm.tm_min,
5521 &tm.tm_sec) == 6) {
5522 /* OK */
5523 } else if (sscanf(optarg, "%d-%d-%d",
5524 &tm.tm_year,
5525 &tm.tm_mon,
5526 &tm.tm_mday) == 3) {
5527 tm.tm_hour = 0;
5528 tm.tm_min = 0;
5529 tm.tm_sec = 0;
5530 } else {
5531 goto date_fail;
5532 }
5533 tm.tm_year -= 1900;
5534 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005535 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005536 if (rtc_start_date == -1) {
5537 date_fail:
5538 fprintf(stderr, "Invalid date format. Valid format are:\n"
5539 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5540 exit(1);
5541 }
balrogf6503052008-02-17 11:42:19 +00005542 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005543 }
5544 }
5545 break;
bellard26a5f132008-05-28 12:30:31 +00005546 case QEMU_OPTION_tb_size:
5547 tb_size = strtol(optarg, NULL, 0);
5548 if (tb_size < 0)
5549 tb_size = 0;
5550 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005551 case QEMU_OPTION_icount:
5552 use_icount = 1;
5553 if (strcmp(optarg, "auto") == 0) {
5554 icount_time_shift = -1;
5555 } else {
5556 icount_time_shift = strtol(optarg, NULL, 0);
5557 }
5558 break;
aliguori5bb79102008-10-13 03:12:02 +00005559 case QEMU_OPTION_incoming:
5560 incoming = optarg;
5561 break;
blueswir15824d652009-03-28 06:44:27 +00005562#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005563 case QEMU_OPTION_chroot:
5564 chroot_dir = optarg;
5565 break;
5566 case QEMU_OPTION_runas:
5567 run_as = optarg;
5568 break;
blueswir15824d652009-03-28 06:44:27 +00005569#endif
aliguorie37630c2009-04-22 15:19:10 +00005570#ifdef CONFIG_XEN
5571 case QEMU_OPTION_xen_domid:
5572 xen_domid = atoi(optarg);
5573 break;
5574 case QEMU_OPTION_xen_create:
5575 xen_mode = XEN_CREATE;
5576 break;
5577 case QEMU_OPTION_xen_attach:
5578 xen_mode = XEN_ATTACH;
5579 break;
5580#endif
bellardcd6f1162004-05-13 22:02:20 +00005581 }
bellard0824d6f2003-06-24 13:42:40 +00005582 }
5583 }
bellard330d0412003-07-26 18:11:40 +00005584
Paul Brook5cea8592009-05-30 00:52:44 +01005585 /* If no data_dir is specified then try to find it relative to the
5586 executable path. */
5587 if (!data_dir) {
5588 data_dir = find_datadir(argv[0]);
5589 }
5590 /* If all else fails use the install patch specified when building. */
5591 if (!data_dir) {
5592 data_dir = CONFIG_QEMU_SHAREDIR;
5593 }
5594
blueswir1640f42e2009-04-19 10:18:01 +00005595#if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
aliguori7ba1e612008-11-05 16:04:33 +00005596 if (kvm_allowed && kqemu_allowed) {
5597 fprintf(stderr,
5598 "You can not enable both KVM and kqemu at the same time\n");
5599 exit(1);
5600 }
5601#endif
5602
Jes Sorensen6be68d72009-07-23 17:03:42 +02005603 /*
5604 * Default to max_cpus = smp_cpus, in case the user doesn't
5605 * specify a max_cpus value.
5606 */
5607 if (!max_cpus)
5608 max_cpus = smp_cpus;
5609
balrog3d878ca2008-10-28 10:59:59 +00005610 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005611 if (smp_cpus > machine->max_cpus) {
5612 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5613 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5614 machine->max_cpus);
5615 exit(1);
5616 }
5617
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005618 if (display_type == DT_NOGRAPHIC) {
aliguoribc0129d2008-08-01 15:12:34 +00005619 if (serial_device_index == 0)
5620 serial_devices[0] = "stdio";
5621 if (parallel_device_index == 0)
5622 parallel_devices[0] = "null";
5623 if (strncmp(monitor_device, "vc", 2) == 0)
5624 monitor_device = "stdio";
5625 }
5626
ths71e3ceb2006-12-22 02:11:31 +00005627#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005628 if (daemonize) {
5629 pid_t pid;
5630
5631 if (pipe(fds) == -1)
5632 exit(1);
5633
5634 pid = fork();
5635 if (pid > 0) {
5636 uint8_t status;
5637 ssize_t len;
5638
5639 close(fds[1]);
5640
5641 again:
ths93815bc2007-03-19 15:58:31 +00005642 len = read(fds[0], &status, 1);
5643 if (len == -1 && (errno == EINTR))
5644 goto again;
5645
5646 if (len != 1)
5647 exit(1);
5648 else if (status == 1) {
5649 fprintf(stderr, "Could not acquire pidfile\n");
5650 exit(1);
5651 } else
5652 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005653 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005654 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005655
5656 setsid();
5657
5658 pid = fork();
5659 if (pid > 0)
5660 exit(0);
5661 else if (pid < 0)
5662 exit(1);
5663
5664 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005665
5666 signal(SIGTSTP, SIG_IGN);
5667 signal(SIGTTOU, SIG_IGN);
5668 signal(SIGTTIN, SIG_IGN);
5669 }
ths71e3ceb2006-12-22 02:11:31 +00005670
thsaa26bb22007-03-25 21:33:06 +00005671 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005672 if (daemonize) {
5673 uint8_t status = 1;
5674 write(fds[1], &status, 1);
5675 } else
5676 fprintf(stderr, "Could not acquire pid file\n");
5677 exit(1);
5678 }
blueswir1b9e82a52009-04-05 18:03:31 +00005679#endif
ths93815bc2007-03-19 15:58:31 +00005680
blueswir1640f42e2009-04-19 10:18:01 +00005681#ifdef CONFIG_KQEMU
bellardff3fbb32006-01-08 10:53:14 +00005682 if (smp_cpus > 1)
5683 kqemu_allowed = 0;
5684#endif
aliguori3fcf7b62009-04-24 18:03:25 +00005685 if (qemu_init_main_loop()) {
5686 fprintf(stderr, "qemu_init_main_loop failed\n");
5687 exit(1);
5688 }
bellarda20dd502003-09-30 21:07:02 +00005689 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005690
thsf8d39c02008-07-03 10:01:15 +00005691 if (!linux_boot && *kernel_cmdline != '\0') {
5692 fprintf(stderr, "-append only allowed with -kernel option\n");
5693 exit(1);
5694 }
5695
5696 if (!linux_boot && initrd_filename != NULL) {
5697 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5698 exit(1);
5699 }
5700
Filip Navarabf65f532009-07-27 10:02:04 -05005701#ifndef _WIN32
5702 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00005703 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05005704#endif
ths3b46e622007-09-17 08:09:54 +00005705
pbrook634fce92006-07-15 17:40:09 +00005706 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005707 if (init_timer_alarm() < 0) {
5708 fprintf(stderr, "could not initialize alarm timer\n");
5709 exit(1);
5710 }
pbrook2e70f6e2008-06-29 01:03:05 +00005711 if (use_icount && icount_time_shift < 0) {
5712 use_icount = 2;
5713 /* 125MIPS seems a reasonable initial guess at the guest speed.
5714 It will be corrected fairly quickly anyway. */
5715 icount_time_shift = 3;
5716 init_icount_adjust();
5717 }
pbrook634fce92006-07-15 17:40:09 +00005718
bellardfd1dff42006-02-01 21:29:26 +00005719#ifdef _WIN32
5720 socket_init();
5721#endif
5722
bellard7c9d8e02005-11-15 22:16:05 +00005723 /* init network clients */
5724 if (nb_net_clients == 0) {
5725 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005726 net_clients[nb_net_clients++] = "nic";
5727#ifdef CONFIG_SLIRP
5728 net_clients[nb_net_clients++] = "user";
5729#endif
bellardc20709a2004-04-21 23:27:19 +00005730 }
5731
bellard7c9d8e02005-11-15 22:16:05 +00005732 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005733 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005734 exit(1);
bellard702c6512004-04-02 21:21:32 +00005735 }
bellardf1510b22003-06-25 00:07:40 +00005736
Glauber Costa406c8df2009-06-17 09:05:30 -04005737 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5738 net_set_boot_mask(net_boot);
5739
5740 net_client_check();
thseec85c22007-01-05 17:41:07 +00005741
balrogdc72ac12008-11-09 00:04:26 +00005742 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005743 if (foreach_device_config(DEV_BT, bt_parse))
5744 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005745
bellard0824d6f2003-06-24 13:42:40 +00005746 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005747 if (ram_size == 0)
5748 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005749
blueswir1640f42e2009-04-19 10:18:01 +00005750#ifdef CONFIG_KQEMU
pbrook94a6b542009-04-11 17:15:54 +00005751 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5752 guest ram allocation. It needs to go away. */
5753 if (kqemu_allowed) {
Paul Brook4cfce482009-05-13 18:08:17 +01005754 kqemu_phys_ram_size = ram_size + 8 * 1024 * 1024 + 4 * 1024 * 1024;
pbrook94a6b542009-04-11 17:15:54 +00005755 kqemu_phys_ram_base = qemu_vmalloc(kqemu_phys_ram_size);
5756 if (!kqemu_phys_ram_base) {
5757 fprintf(stderr, "Could not allocate physical memory\n");
5758 exit(1);
5759 }
balrog7fb4fdc2008-04-24 17:59:27 +00005760 }
pbrook94a6b542009-04-11 17:15:54 +00005761#endif
bellard0824d6f2003-06-24 13:42:40 +00005762
bellard26a5f132008-05-28 12:30:31 +00005763 /* init the dynamic translator */
5764 cpu_exec_init_all(tb_size * 1024 * 1024);
5765
bellard5905b2e2004-08-01 21:53:26 +00005766 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005767
5768 /* we always create the cdrom drive, even if no disk is there */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005769 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005770
balrog9d413d12007-12-04 00:10:34 +00005771 /* we always create at least one floppy */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005772 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005773
balrog9d413d12007-12-04 00:10:34 +00005774 /* we always create one sd slot, even if no card is in it */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005775 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005776
ths96d30e42007-01-07 20:42:14 +00005777 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005778 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02005779 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5780 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005781 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005782
bellardc88676f2006-08-06 13:36:11 +00005783 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005784 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005785
aliguori3023f332009-01-16 19:04:14 +00005786#ifndef _WIN32
5787 /* must be after terminal init, SDL library changes signal handlers */
Jan Kiszka7c3370d2009-05-08 12:34:17 +02005788 sighandler_setup();
aliguori3023f332009-01-16 19:04:14 +00005789#endif
5790
5791 /* Maintain compatibility with multiple stdio monitors */
5792 if (!strcmp(monitor_device,"stdio")) {
5793 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5794 const char *devname = serial_devices[i];
5795 if (devname && !strcmp(devname,"mon:stdio")) {
5796 monitor_device = NULL;
5797 break;
5798 } else if (devname && !strcmp(devname,"stdio")) {
5799 monitor_device = NULL;
5800 serial_devices[i] = "mon:stdio";
5801 break;
5802 }
5803 }
5804 }
5805
aliguori268a3622009-04-21 22:30:27 +00005806 if (nb_numa_nodes > 0) {
5807 int i;
5808
5809 if (nb_numa_nodes > smp_cpus) {
5810 nb_numa_nodes = smp_cpus;
5811 }
5812
5813 /* If no memory size if given for any node, assume the default case
5814 * and distribute the available memory equally across all nodes
5815 */
5816 for (i = 0; i < nb_numa_nodes; i++) {
5817 if (node_mem[i] != 0)
5818 break;
5819 }
5820 if (i == nb_numa_nodes) {
5821 uint64_t usedmem = 0;
5822
5823 /* On Linux, the each node's border has to be 8MB aligned,
5824 * the final node gets the rest.
5825 */
5826 for (i = 0; i < nb_numa_nodes - 1; i++) {
5827 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5828 usedmem += node_mem[i];
5829 }
5830 node_mem[i] = ram_size - usedmem;
5831 }
5832
5833 for (i = 0; i < nb_numa_nodes; i++) {
5834 if (node_cpumask[i] != 0)
5835 break;
5836 }
5837 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5838 * must cope with this anyway, because there are BIOSes out there in
5839 * real machines which also use this scheme.
5840 */
5841 if (i == nb_numa_nodes) {
5842 for (i = 0; i < smp_cpus; i++) {
5843 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5844 }
5845 }
5846 }
5847
aliguori3023f332009-01-16 19:04:14 +00005848 if (kvm_enabled()) {
5849 int ret;
5850
5851 ret = kvm_init(smp_cpus);
5852 if (ret < 0) {
5853 fprintf(stderr, "failed to initialize KVM\n");
5854 exit(1);
5855 }
5856 }
5857
aliguori4c621802009-01-16 21:48:20 +00005858 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005859 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005860 if (!monitor_hd) {
5861 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5862 exit(1);
5863 }
5864 }
5865
aliguori2796dae2009-01-16 20:23:27 +00005866 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5867 const char *devname = serial_devices[i];
5868 if (devname && strcmp(devname, "none")) {
5869 char label[32];
5870 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005871 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005872 if (!serial_hds[i]) {
5873 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5874 devname);
5875 exit(1);
5876 }
5877 }
5878 }
5879
5880 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5881 const char *devname = parallel_devices[i];
5882 if (devname && strcmp(devname, "none")) {
5883 char label[32];
5884 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005885 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005886 if (!parallel_hds[i]) {
5887 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5888 devname);
5889 exit(1);
5890 }
5891 }
5892 }
5893
5894 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5895 const char *devname = virtio_consoles[i];
5896 if (devname && strcmp(devname, "none")) {
5897 char label[32];
5898 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005899 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005900 if (!virtcon_hds[i]) {
5901 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5902 devname);
5903 exit(1);
5904 }
5905 }
5906 }
5907
Paul Brookaae94602009-05-14 22:35:06 +01005908 module_call_init(MODULE_INIT_DEVICE);
5909
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005910 if (machine->compat_props) {
5911 qdev_prop_register_compat(machine->compat_props);
5912 }
Paul Brookfbe1b592009-05-13 17:56:25 +01005913 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005914 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5915
aliguori268a3622009-04-21 22:30:27 +00005916
5917 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5918 for (i = 0; i < nb_numa_nodes; i++) {
5919 if (node_cpumask[i] & (1 << env->cpu_index)) {
5920 env->numa_node = i;
5921 }
5922 }
5923 }
5924
aliguori6f338c32009-02-11 15:21:54 +00005925 current_machine = machine;
5926
aliguori3023f332009-01-16 19:04:14 +00005927 /* init USB devices */
5928 if (usb_enabled) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005929 foreach_device_config(DEV_USB, usb_parse);
aliguori3023f332009-01-16 19:04:14 +00005930 }
5931
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005932 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005933 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005934 exit(1);
5935
aliguori8f391ab2009-01-19 16:34:10 +00005936 if (!display_state)
5937 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005938 /* just use the first displaystate for the moment */
5939 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005940
5941 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005942#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005943 display_type = DT_SDL;
5944#else
5945 display_type = DT_VNC;
5946 vnc_display = "localhost:0,to=99";
5947 show_vnc_port = 1;
5948#endif
5949 }
5950
5951
5952 switch (display_type) {
5953 case DT_NOGRAPHIC:
5954 break;
5955#if defined(CONFIG_CURSES)
5956 case DT_CURSES:
5957 curses_display_init(ds, full_screen);
5958 break;
5959#endif
bellard5b0753e2005-03-01 21:37:28 +00005960#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005961 case DT_SDL:
5962 sdl_display_init(ds, full_screen, no_frame);
5963 break;
bellard5b0753e2005-03-01 21:37:28 +00005964#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005965 case DT_SDL:
5966 cocoa_display_init(ds, full_screen);
5967 break;
bellard313aa562003-08-10 21:52:11 +00005968#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005969 case DT_VNC:
5970 vnc_display_init(ds);
5971 if (vnc_display_open(ds, vnc_display) < 0)
5972 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005973
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005974 if (show_vnc_port) {
5975 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005976 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005977 break;
5978 default:
5979 break;
bellard313aa562003-08-10 21:52:11 +00005980 }
aliguori7d957bd2009-01-15 22:14:11 +00005981 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005982
aliguori3023f332009-01-16 19:04:14 +00005983 dcl = ds->listeners;
5984 while (dcl != NULL) {
5985 if (dcl->dpy_refresh != NULL) {
5986 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5987 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005988 }
aliguori3023f332009-01-16 19:04:14 +00005989 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005990 }
aliguori3023f332009-01-16 19:04:14 +00005991
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005992 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00005993 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5994 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5995 }
5996
aliguori2796dae2009-01-16 20:23:27 +00005997 text_consoles_set_display(display_state);
aliguori2970a6c2009-03-05 22:59:58 +00005998 qemu_chr_initial_reset();
aliguori2796dae2009-01-16 20:23:27 +00005999
aliguori4c621802009-01-16 21:48:20 +00006000 if (monitor_device && monitor_hd)
aliguoricde76ee2009-03-05 23:01:51 +00006001 monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT);
bellard82c643f2004-07-14 17:28:13 +00006002
bellard8d11df92004-08-24 21:13:40 +00006003 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006004 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006005 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006006 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006007 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00006008 }
bellard82c643f2004-07-14 17:28:13 +00006009 }
bellard82c643f2004-07-14 17:28:13 +00006010
bellard6508fe52005-01-15 12:02:56 +00006011 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006012 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006013 if (devname && strcmp(devname, "none")) {
thsaf3a9032007-07-11 23:14:59 +00006014 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006015 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00006016 }
6017 }
6018
aliguori9ede2fd2009-01-15 20:05:25 +00006019 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
6020 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00006021 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00006022 if (strstart(devname, "vc", 0))
6023 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
6024 }
6025 }
6026
aliguori59030a82009-04-05 18:43:41 +00006027 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6028 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6029 gdbstub_dev);
6030 exit(1);
balrog45669e02007-07-02 13:20:17 +00006031 }
balrog45669e02007-07-02 13:20:17 +00006032
bellardd63d3072004-10-03 13:29:03 +00006033 if (loadvm)
aliguori376253e2009-03-05 23:01:23 +00006034 do_loadvm(cur_mon, loadvm);
bellardd63d3072004-10-03 13:29:03 +00006035
Glauber Costa2bb8c102009-07-24 16:20:23 -04006036 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00006037 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03006038 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00006039 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03006040 }
thsffd843b2006-12-21 19:46:43 +00006041
blueswir1b9e82a52009-04-05 18:03:31 +00006042#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00006043 if (daemonize) {
6044 uint8_t status = 0;
6045 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00006046
6047 again1:
6048 len = write(fds[1], &status, 1);
6049 if (len == -1 && (errno == EINTR))
6050 goto again1;
6051
6052 if (len != 1)
6053 exit(1);
6054
aliguoribd54b862008-07-23 00:58:33 +00006055 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00006056 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00006057 if (fd == -1)
6058 exit(1);
aliguori08585322009-02-27 22:09:45 +00006059 }
ths71e3ceb2006-12-22 02:11:31 +00006060
aliguori08585322009-02-27 22:09:45 +00006061 if (run_as) {
6062 pwd = getpwnam(run_as);
6063 if (!pwd) {
6064 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6065 exit(1);
6066 }
6067 }
ths71e3ceb2006-12-22 02:11:31 +00006068
aliguori08585322009-02-27 22:09:45 +00006069 if (chroot_dir) {
6070 if (chroot(chroot_dir) < 0) {
6071 fprintf(stderr, "chroot failed\n");
6072 exit(1);
6073 }
6074 chdir("/");
6075 }
6076
6077 if (run_as) {
6078 if (setgid(pwd->pw_gid) < 0) {
6079 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6080 exit(1);
6081 }
6082 if (setuid(pwd->pw_uid) < 0) {
6083 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6084 exit(1);
6085 }
6086 if (setuid(0) != -1) {
6087 fprintf(stderr, "Dropping privileges failed\n");
6088 exit(1);
6089 }
6090 }
aliguori08585322009-02-27 22:09:45 +00006091
6092 if (daemonize) {
6093 dup2(fd, 0);
6094 dup2(fd, 1);
6095 dup2(fd, 2);
6096
6097 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006098 }
blueswir1b9e82a52009-04-05 18:03:31 +00006099#endif
ths71e3ceb2006-12-22 02:11:31 +00006100
bellard8a7ddc32004-03-31 19:00:16 +00006101 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006102 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006103 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006104
bellard0824d6f2003-06-24 13:42:40 +00006105 return 0;
6106}