blob: 0d3f5e6687934a94e8586a13b2f5b7432065c430 [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"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
aliguori08585322009-02-27 22:09:45 +000037#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000047#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000048#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000049#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000050#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000055#else
56#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000057#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010058#else
blueswir1223f0d72008-09-30 18:12:18 +000059#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000060#include <pty.h>
61#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000062#include <linux/rtc.h>
Andi Kleen18894652009-07-02 09:34:17 +020063#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000064
65/* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67/* #include <linux/hpet.h> */
68#include "hpet.h"
69
bellarde57a8c02005-11-10 23:58:52 +000070#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000071#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000072#endif
73#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000074#include <sys/stat.h>
75#include <sys/ethernet.h>
76#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000077#include <netinet/arp.h>
78#include <netinet/in.h>
79#include <netinet/in_systm.h>
80#include <netinet/ip.h>
81#include <netinet/ip_icmp.h> // must come after ip.h
82#include <netinet/udp.h>
83#include <netinet/tcp.h>
84#include <net/if.h>
85#include <syslog.h>
86#include <stropts.h>
Blue Swirl8d32cf02009-10-02 19:32:12 +000087/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000090#endif
bellard7d3505c2004-05-12 19:32:15 +000091#endif
bellardec530c82006-04-25 22:36:06 +000092#endif
bellard67b915a2004-03-31 23:37:16 +000093
blueswir19892fbf2008-08-24 10:34:20 +000094#if defined(__OpenBSD__)
95#include <util.h>
96#endif
97
ths8a16d272008-07-19 09:56:24 +000098#if defined(CONFIG_VDE)
99#include <libvdeplug.h>
100#endif
101
bellard67b915a2004-03-31 23:37:16 +0000102#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000103#include <windows.h>
ths4fddf622007-12-17 04:42:29 +0000104#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000105#endif
106
bellard73332e52004-04-04 20:22:28 +0000107#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200108#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200109#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000110int qemu_main(int argc, char **argv, char **envp);
111int main(int argc, char **argv)
112{
Stefan Weil59a36a22009-06-18 20:11:03 +0200113 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000114}
115#undef main
116#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000117#endif
bellard73332e52004-04-04 20:22:28 +0000118#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000119
bellard5b0753e2005-03-01 21:37:28 +0000120#ifdef CONFIG_COCOA
121#undef main
122#define main qemu_main
123#endif /* CONFIG_COCOA */
124
blueswir1511d2b12009-03-07 15:32:56 +0000125#include "hw/hw.h"
126#include "hw/boards.h"
127#include "hw/usb.h"
128#include "hw/pcmcia.h"
129#include "hw/pc.h"
130#include "hw/audiodev.h"
131#include "hw/isa.h"
132#include "hw/baum.h"
133#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100134#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000135#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000136#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200137#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200138#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000139#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000140#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000141#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000142#include "monitor.h"
143#include "console.h"
144#include "sysemu.h"
145#include "gdbstub.h"
146#include "qemu-timer.h"
147#include "qemu-char.h"
148#include "cache-utils.h"
149#include "block.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200150#include "block_int.h"
151#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000152#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000153#include "audio/audio.h"
154#include "migration.h"
155#include "kvm.h"
156#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200157#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200158#include "qemu-config.h"
blueswir1511d2b12009-03-07 15:32:56 +0000159
bellard0824d6f2003-06-24 13:42:40 +0000160#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000161
bellard8a7ddc32004-03-31 19:00:16 +0000162#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000163
blueswir1511d2b12009-03-07 15:32:56 +0000164#include "qemu_socket.h"
165
Jan Kiszkad918f232009-06-24 14:42:30 +0200166#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000167
Blue Swirl72cf2d42009-09-12 07:36:22 +0000168#include "qemu-queue.h"
169
blueswir19dc63a12008-10-04 07:25:46 +0000170//#define DEBUG_NET
171//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000172
bellard1bfe8562004-07-08 21:17:50 +0000173#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000174
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +0200175/* Maximum number of monitor devices */
176#define MAX_MONITOR_DEVICES 10
177
Paul Brook5cea8592009-05-30 00:52:44 +0100178static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000179const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000180/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000181 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000182struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
183struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000184enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000185static DisplayState *display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500186DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000187const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500188ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000189int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000190NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000191int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200192int autostart;
balrogf6503052008-02-17 11:42:19 +0000193static int rtc_utc = 1;
194static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200195QEMUClock *rtc_clock;
Zachary Amsden86176752009-07-30 00:15:02 -1000196int vga_interface_type = VGA_CIRRUS;
bellardd8272202005-04-06 20:32:23 +0000197#ifdef TARGET_SPARC
198int graphic_width = 1024;
199int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000200int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000201#else
bellard1bfe8562004-07-08 21:17:50 +0000202int graphic_width = 800;
203int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000204int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000205#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000206static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000207#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000208static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000209#endif
ths667acca2006-12-11 02:08:05 +0000210int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000211CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000212CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000213CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100214CharDriverState *monitor_hds[MAX_MONITOR_DEVICES];
bellarda09db212005-04-30 16:10:35 +0000215#ifdef TARGET_I386
216int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000217int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000218#endif
bellardbb36d472005-11-05 14:22:28 +0000219int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000220int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000221int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200222int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200223int smp_cores = 1;
224int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000225const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000226int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000227int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000228int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000229int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000230int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000231int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000232int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200233uint8_t irq0override = 1;
blueswir1b9e82a52009-04-05 18:03:31 +0000234#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000235int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000236#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200237const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000238const char *option_rom[MAX_OPTION_ROMS];
239int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000240int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000241#ifdef TARGET_ARM
242int old_param = 0;
243#endif
thsc35734b2007-03-19 15:17:08 +0000244const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000245int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500246int ctrl_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000247#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000248unsigned int nb_prom_envs = 0;
249const char *prom_envs[MAX_PROM_ENVS];
250#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200251int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000252
aliguori268a3622009-04-21 22:30:27 +0000253int nb_numa_nodes;
254uint64_t node_mem[MAX_NODES];
255uint64_t node_cpumask[MAX_NODES];
256
balrogee5605e2007-12-03 03:01:40 +0000257static CPUState *cur_cpu;
258static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000259static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000260/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000261static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000262/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000263#define MAX_ICOUNT_SHIFT 10
264/* Compensate for varying guest execution speed. */
265static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000266static QEMUTimer *icount_rt_timer;
267static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000268static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000269
blueswir18fcb1b92008-09-18 18:29:08 +0000270uint8_t qemu_uuid[16];
271
Jan Kiszka76e30d02009-07-02 00:19:02 +0200272static QEMUBootSetHandler *boot_set_handler;
273static void *boot_set_opaque;
274
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100275static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100276static int default_parallel = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100277static int default_monitor = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100278
279static struct {
280 const char *driver;
281 int *flag;
282} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100283 { .driver = "isa-serial", .flag = &default_serial },
284 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100285};
286
287static int default_driver_check(QemuOpts *opts, void *opaque)
288{
289 const char *driver = qemu_opt_get(opts, "driver");
290 int i;
291
292 if (!driver)
293 return 0;
294 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
295 if (strcmp(default_list[i].driver, driver) != 0)
296 continue;
297 *(default_list[i].flag) = 0;
298 }
299 return 0;
300}
301
bellard0824d6f2003-06-24 13:42:40 +0000302/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000303/* x86 ISA bus support */
304
Anthony Liguoric227f092009-10-01 16:12:16 -0500305target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000306PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000307
bellard0824d6f2003-06-24 13:42:40 +0000308/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000309void hw_error(const char *fmt, ...)
310{
311 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000312 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000313
314 va_start(ap, fmt);
315 fprintf(stderr, "qemu: hardware error: ");
316 vfprintf(stderr, fmt, ap);
317 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000318 for(env = first_cpu; env != NULL; env = env->next_cpu) {
319 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000320#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000321 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000322#else
bellard6a00d602005-11-21 23:25:50 +0000323 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000324#endif
bellard6a00d602005-11-21 23:25:50 +0000325 }
bellard0824d6f2003-06-24 13:42:40 +0000326 va_end(ap);
327 abort();
328}
Andi Kleen18894652009-07-02 09:34:17 +0200329
330static void set_proc_name(const char *s)
331{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700332#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200333 char name[16];
334 if (!s)
335 return;
336 name[sizeof(name) - 1] = 0;
337 strncpy(name, s, sizeof(name));
338 /* Could rewrite argv[0] too, but that's a bit more complicated.
339 This simple way is enough for `top'. */
340 prctl(PR_SET_NAME, name);
341#endif
342}
aliguoridf751fa2008-12-04 20:19:35 +0000343
344/***************/
345/* ballooning */
346
347static QEMUBalloonEvent *qemu_balloon_event;
348void *qemu_balloon_event_opaque;
349
350void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
351{
352 qemu_balloon_event = func;
353 qemu_balloon_event_opaque = opaque;
354}
355
Anthony Liguoric227f092009-10-01 16:12:16 -0500356void qemu_balloon(ram_addr_t target)
aliguoridf751fa2008-12-04 20:19:35 +0000357{
358 if (qemu_balloon_event)
359 qemu_balloon_event(qemu_balloon_event_opaque, target);
360}
361
Anthony Liguoric227f092009-10-01 16:12:16 -0500362ram_addr_t qemu_balloon_status(void)
aliguoridf751fa2008-12-04 20:19:35 +0000363{
364 if (qemu_balloon_event)
365 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
366 return 0;
367}
bellard0824d6f2003-06-24 13:42:40 +0000368
bellard8a7ddc32004-03-31 19:00:16 +0000369/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000370/* keyboard/mouse */
371
372static QEMUPutKBDEvent *qemu_put_kbd_event;
373static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000374static QEMUPutMouseEntry *qemu_put_mouse_event_head;
375static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000376
377void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
378{
379 qemu_put_kbd_event_opaque = opaque;
380 qemu_put_kbd_event = func;
381}
382
ths455204e2007-01-05 16:42:13 +0000383QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
384 void *opaque, int absolute,
385 const char *name)
bellard63066f42004-06-03 18:45:02 +0000386{
ths455204e2007-01-05 16:42:13 +0000387 QEMUPutMouseEntry *s, *cursor;
388
389 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000390
391 s->qemu_put_mouse_event = func;
392 s->qemu_put_mouse_event_opaque = opaque;
393 s->qemu_put_mouse_event_absolute = absolute;
394 s->qemu_put_mouse_event_name = qemu_strdup(name);
395 s->next = NULL;
396
397 if (!qemu_put_mouse_event_head) {
398 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
399 return s;
400 }
401
402 cursor = qemu_put_mouse_event_head;
403 while (cursor->next != NULL)
404 cursor = cursor->next;
405
406 cursor->next = s;
407 qemu_put_mouse_event_current = s;
408
409 return s;
410}
411
412void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
413{
414 QEMUPutMouseEntry *prev = NULL, *cursor;
415
416 if (!qemu_put_mouse_event_head || entry == NULL)
417 return;
418
419 cursor = qemu_put_mouse_event_head;
420 while (cursor != NULL && cursor != entry) {
421 prev = cursor;
422 cursor = cursor->next;
423 }
424
425 if (cursor == NULL) // does not exist or list empty
426 return;
427 else if (prev == NULL) { // entry is head
428 qemu_put_mouse_event_head = cursor->next;
429 if (qemu_put_mouse_event_current == entry)
430 qemu_put_mouse_event_current = cursor->next;
431 qemu_free(entry->qemu_put_mouse_event_name);
432 qemu_free(entry);
433 return;
434 }
435
436 prev->next = entry->next;
437
438 if (qemu_put_mouse_event_current == entry)
439 qemu_put_mouse_event_current = prev;
440
441 qemu_free(entry->qemu_put_mouse_event_name);
442 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000443}
444
445void kbd_put_keycode(int keycode)
446{
447 if (qemu_put_kbd_event) {
448 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
449 }
450}
451
452void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
453{
ths455204e2007-01-05 16:42:13 +0000454 QEMUPutMouseEvent *mouse_event;
455 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000456 int width;
ths455204e2007-01-05 16:42:13 +0000457
458 if (!qemu_put_mouse_event_current) {
459 return;
460 }
461
462 mouse_event =
463 qemu_put_mouse_event_current->qemu_put_mouse_event;
464 mouse_event_opaque =
465 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
466
467 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000468 if (graphic_rotate) {
469 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
470 width = 0x7fff;
471 else
aurel32b94ed572008-03-10 19:34:27 +0000472 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000473 mouse_event(mouse_event_opaque,
474 width - dy, dx, dz, buttons_state);
475 } else
476 mouse_event(mouse_event_opaque,
477 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000478 }
479}
480
bellard09b26c52006-04-12 21:09:08 +0000481int kbd_mouse_is_absolute(void)
482{
ths455204e2007-01-05 16:42:13 +0000483 if (!qemu_put_mouse_event_current)
484 return 0;
485
486 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
487}
488
aliguori376253e2009-03-05 23:01:23 +0000489void do_info_mice(Monitor *mon)
ths455204e2007-01-05 16:42:13 +0000490{
491 QEMUPutMouseEntry *cursor;
492 int index = 0;
493
494 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000495 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000496 return;
497 }
498
aliguori376253e2009-03-05 23:01:23 +0000499 monitor_printf(mon, "Mouse devices available:\n");
ths455204e2007-01-05 16:42:13 +0000500 cursor = qemu_put_mouse_event_head;
501 while (cursor != NULL) {
aliguori376253e2009-03-05 23:01:23 +0000502 monitor_printf(mon, "%c Mouse #%d: %s\n",
503 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
504 index, cursor->qemu_put_mouse_event_name);
ths455204e2007-01-05 16:42:13 +0000505 index++;
506 cursor = cursor->next;
507 }
508}
509
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300510void do_mouse_set(Monitor *mon, const QDict *qdict)
ths455204e2007-01-05 16:42:13 +0000511{
512 QEMUPutMouseEntry *cursor;
513 int i = 0;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300514 int index = qdict_get_int(qdict, "index");
ths455204e2007-01-05 16:42:13 +0000515
516 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000517 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000518 return;
519 }
520
521 cursor = qemu_put_mouse_event_head;
522 while (cursor != NULL && index != i) {
523 i++;
524 cursor = cursor->next;
525 }
526
527 if (cursor != NULL)
528 qemu_put_mouse_event_current = cursor;
529 else
aliguori376253e2009-03-05 23:01:23 +0000530 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000531}
532
bellard87858c82003-06-27 12:01:39 +0000533/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000534uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000535{
536 union {
537 uint64_t ll;
538 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200539#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000540 uint32_t high, low;
541#else
542 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000543#endif
bellard87858c82003-06-27 12:01:39 +0000544 } l;
545 } u, res;
546 uint64_t rl, rh;
547
548 u.ll = a;
549 rl = (uint64_t)u.l.low * (uint64_t)b;
550 rh = (uint64_t)u.l.high * (uint64_t)b;
551 rh += (rl >> 32);
552 res.l.high = rh / c;
553 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
554 return res.ll;
555}
556
bellard1dce7c32006-07-13 23:20:22 +0000557/***********************************************************/
558/* real time host monotonic timer */
559
Jan Kiszka21d5d122009-09-15 13:36:04 +0200560static int64_t get_clock_realtime(void)
561{
562 struct timeval tv;
563
564 gettimeofday(&tv, NULL);
565 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
566}
567
bellard1dce7c32006-07-13 23:20:22 +0000568#ifdef WIN32
569
570static int64_t clock_freq;
571
572static void init_get_clock(void)
573{
bellarda8e5ac32006-07-14 09:36:13 +0000574 LARGE_INTEGER freq;
575 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000576 ret = QueryPerformanceFrequency(&freq);
577 if (ret == 0) {
578 fprintf(stderr, "Could not calibrate ticks\n");
579 exit(1);
580 }
581 clock_freq = freq.QuadPart;
582}
583
584static int64_t get_clock(void)
585{
586 LARGE_INTEGER ti;
587 QueryPerformanceCounter(&ti);
Anthony Liguori274dfed2009-09-11 10:28:26 -0500588 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
bellard1dce7c32006-07-13 23:20:22 +0000589}
590
591#else
592
593static int use_rt_clock;
594
595static void init_get_clock(void)
596{
597 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000598#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100599 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000600 {
601 struct timespec ts;
602 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
603 use_rt_clock = 1;
604 }
605 }
606#endif
607}
608
609static int64_t get_clock(void)
610{
blueswir1c5e97232009-03-07 20:06:23 +0000611#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100612 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000613 if (use_rt_clock) {
614 struct timespec ts;
615 clock_gettime(CLOCK_MONOTONIC, &ts);
616 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000617 } else
bellard1dce7c32006-07-13 23:20:22 +0000618#endif
619 {
620 /* XXX: using gettimeofday leads to problems if the date
621 changes, so it should be avoided. */
Jan Kiszka21d5d122009-09-15 13:36:04 +0200622 return get_clock_realtime();
bellard1dce7c32006-07-13 23:20:22 +0000623 }
624}
bellard1dce7c32006-07-13 23:20:22 +0000625#endif
626
pbrook2e70f6e2008-06-29 01:03:05 +0000627/* Return the virtual CPU time, based on the instruction counter. */
628static int64_t cpu_get_icount(void)
629{
630 int64_t icount;
631 CPUState *env = cpu_single_env;;
632 icount = qemu_icount;
633 if (env) {
634 if (!can_do_io(env))
635 fprintf(stderr, "Bad clock read\n");
636 icount -= (env->icount_decr.u16.low + env->icount_extra);
637 }
638 return qemu_icount_bias + (icount << icount_time_shift);
639}
640
bellard1dce7c32006-07-13 23:20:22 +0000641/***********************************************************/
642/* guest cycle counter */
643
Juan Quintela6f68e332009-09-10 03:04:27 +0200644typedef struct TimersState {
645 int64_t cpu_ticks_prev;
646 int64_t cpu_ticks_offset;
647 int64_t cpu_clock_offset;
648 int32_t cpu_ticks_enabled;
Anthony Liguori274dfed2009-09-11 10:28:26 -0500649 int64_t dummy;
Juan Quintela6f68e332009-09-10 03:04:27 +0200650} TimersState;
651
652TimersState timers_state;
bellard1dce7c32006-07-13 23:20:22 +0000653
654/* return the host CPU cycle counter and handle stop/restart */
655int64_t cpu_get_ticks(void)
656{
pbrook2e70f6e2008-06-29 01:03:05 +0000657 if (use_icount) {
658 return cpu_get_icount();
659 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200660 if (!timers_state.cpu_ticks_enabled) {
661 return timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000662 } else {
663 int64_t ticks;
664 ticks = cpu_get_real_ticks();
Juan Quintela6f68e332009-09-10 03:04:27 +0200665 if (timers_state.cpu_ticks_prev > ticks) {
bellard1dce7c32006-07-13 23:20:22 +0000666 /* Note: non increasing ticks may happen if the host uses
667 software suspend */
Juan Quintela6f68e332009-09-10 03:04:27 +0200668 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
bellard1dce7c32006-07-13 23:20:22 +0000669 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200670 timers_state.cpu_ticks_prev = ticks;
671 return ticks + timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000672 }
673}
674
675/* return the host CPU monotonic timer and handle stop/restart */
676static int64_t cpu_get_clock(void)
677{
678 int64_t ti;
Juan Quintela6f68e332009-09-10 03:04:27 +0200679 if (!timers_state.cpu_ticks_enabled) {
680 return timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000681 } else {
682 ti = get_clock();
Juan Quintela6f68e332009-09-10 03:04:27 +0200683 return ti + timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000684 }
685}
686
687/* enable cpu_get_ticks() */
688void cpu_enable_ticks(void)
689{
Juan Quintela6f68e332009-09-10 03:04:27 +0200690 if (!timers_state.cpu_ticks_enabled) {
691 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
692 timers_state.cpu_clock_offset -= get_clock();
693 timers_state.cpu_ticks_enabled = 1;
bellard1dce7c32006-07-13 23:20:22 +0000694 }
695}
696
697/* disable cpu_get_ticks() : the clock is stopped. You must not call
698 cpu_get_ticks() after that. */
699void cpu_disable_ticks(void)
700{
Juan Quintela6f68e332009-09-10 03:04:27 +0200701 if (timers_state.cpu_ticks_enabled) {
702 timers_state.cpu_ticks_offset = cpu_get_ticks();
703 timers_state.cpu_clock_offset = cpu_get_clock();
704 timers_state.cpu_ticks_enabled = 0;
bellard1dce7c32006-07-13 23:20:22 +0000705 }
706}
707
708/***********************************************************/
709/* timers */
ths5fafdf22007-09-16 21:08:06 +0000710
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200711#define QEMU_CLOCK_REALTIME 0
712#define QEMU_CLOCK_VIRTUAL 1
Jan Kiszka21d5d122009-09-15 13:36:04 +0200713#define QEMU_CLOCK_HOST 2
bellard8a7ddc32004-03-31 19:00:16 +0000714
715struct QEMUClock {
716 int type;
717 /* XXX: add frequency */
718};
719
720struct QEMUTimer {
721 QEMUClock *clock;
722 int64_t expire_time;
723 QEMUTimerCB *cb;
724 void *opaque;
725 struct QEMUTimer *next;
726};
727
thsc8994012007-08-19 21:56:03 +0000728struct qemu_alarm_timer {
729 char const *name;
thsefe75412007-08-24 01:36:32 +0000730 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000731
732 int (*start)(struct qemu_alarm_timer *t);
733 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000734 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000735 void *priv;
736};
737
thsefe75412007-08-24 01:36:32 +0000738#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000739#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000740
741static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
742{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200743 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000744}
745
746static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
747{
748 if (!alarm_has_dynticks(t))
749 return;
750
751 t->rearm(t);
752}
753
754/* TODO: MIN_TIMER_REARM_US should be optimized */
755#define MIN_TIMER_REARM_US 250
756
thsc8994012007-08-19 21:56:03 +0000757static struct qemu_alarm_timer *alarm_timer;
758
759#ifdef _WIN32
760
761struct qemu_alarm_win32 {
762 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000763 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000764} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000765
766static int win32_start_timer(struct qemu_alarm_timer *t);
767static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000768static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000769
770#else
771
772static int unix_start_timer(struct qemu_alarm_timer *t);
773static void unix_stop_timer(struct qemu_alarm_timer *t);
774
ths231c6582007-08-26 17:29:15 +0000775#ifdef __linux__
776
thsefe75412007-08-24 01:36:32 +0000777static int dynticks_start_timer(struct qemu_alarm_timer *t);
778static void dynticks_stop_timer(struct qemu_alarm_timer *t);
779static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
780
thsc40ec5a2007-08-19 22:09:40 +0000781static int hpet_start_timer(struct qemu_alarm_timer *t);
782static void hpet_stop_timer(struct qemu_alarm_timer *t);
783
thsc8994012007-08-19 21:56:03 +0000784static int rtc_start_timer(struct qemu_alarm_timer *t);
785static void rtc_stop_timer(struct qemu_alarm_timer *t);
786
thsefe75412007-08-24 01:36:32 +0000787#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000788
789#endif /* _WIN32 */
790
pbrook2e70f6e2008-06-29 01:03:05 +0000791/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000792 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000793 When the guest is idle real and virtual time will be aligned in
794 the IO wait loop. */
Anthony Liguori274dfed2009-09-11 10:28:26 -0500795#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
pbrook2e70f6e2008-06-29 01:03:05 +0000796
797static void icount_adjust(void)
798{
799 int64_t cur_time;
800 int64_t cur_icount;
801 int64_t delta;
802 static int64_t last_delta;
803 /* If the VM is not running, then do nothing. */
804 if (!vm_running)
805 return;
806
807 cur_time = cpu_get_clock();
808 cur_icount = qemu_get_clock(vm_clock);
809 delta = cur_icount - cur_time;
810 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
811 if (delta > 0
812 && last_delta + ICOUNT_WOBBLE < delta * 2
813 && icount_time_shift > 0) {
814 /* The guest is getting too far ahead. Slow time down. */
815 icount_time_shift--;
816 }
817 if (delta < 0
818 && last_delta - ICOUNT_WOBBLE > delta * 2
819 && icount_time_shift < MAX_ICOUNT_SHIFT) {
820 /* The guest is getting too far behind. Speed time up. */
821 icount_time_shift++;
822 }
823 last_delta = delta;
824 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
825}
826
827static void icount_adjust_rt(void * opaque)
828{
829 qemu_mod_timer(icount_rt_timer,
830 qemu_get_clock(rt_clock) + 1000);
831 icount_adjust();
832}
833
834static void icount_adjust_vm(void * opaque)
835{
836 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500837 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000838 icount_adjust();
839}
840
841static void init_icount_adjust(void)
842{
843 /* Have both realtime and virtual time triggers for speed adjustment.
844 The realtime trigger catches emulated time passing too slowly,
845 the virtual time trigger catches emulated time passing too fast.
846 Realtime triggers occur even when idle, so use them less frequently
847 than VM triggers. */
848 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
849 qemu_mod_timer(icount_rt_timer,
850 qemu_get_clock(rt_clock) + 1000);
851 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
852 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500853 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000854}
855
thsc8994012007-08-19 21:56:03 +0000856static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000857#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000858#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000859 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
860 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000861 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000862 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000863 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000864 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000865#endif
thsefe75412007-08-24 01:36:32 +0000866 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000867#else
thsefe75412007-08-24 01:36:32 +0000868 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
869 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
870 {"win32", 0, win32_start_timer,
871 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000872#endif
873 {NULL, }
874};
875
blueswir13f47aa82008-03-09 06:59:01 +0000876static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000877{
878 int i;
879
880 printf("Available alarm timers, in order of precedence:\n");
881 for (i = 0; alarm_timers[i].name; i++)
882 printf("%s\n", alarm_timers[i].name);
883}
884
885static void configure_alarms(char const *opt)
886{
887 int i;
888 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000889 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000890 char *arg;
891 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000892 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000893
aurel323adda042008-03-09 23:43:49 +0000894 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000895 show_available_alarms();
896 exit(0);
897 }
898
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200899 arg = qemu_strdup(opt);
thsf3dcfad2007-08-24 01:26:02 +0000900
901 /* Reorder the array */
902 name = strtok(arg, ",");
903 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000904 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000905 if (!strcmp(alarm_timers[i].name, name))
906 break;
907 }
908
909 if (i == count) {
910 fprintf(stderr, "Unknown clock %s\n", name);
911 goto next;
912 }
913
914 if (i < cur)
915 /* Ignore */
916 goto next;
917
918 /* Swap */
919 tmp = alarm_timers[i];
920 alarm_timers[i] = alarm_timers[cur];
921 alarm_timers[cur] = tmp;
922
923 cur++;
924next:
925 name = strtok(NULL, ",");
926 }
927
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200928 qemu_free(arg);
thsf3dcfad2007-08-24 01:26:02 +0000929
930 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000931 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000932 for (i = cur; i < count; i++)
933 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000934 } else {
935 show_available_alarms();
936 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000937 }
thsf3dcfad2007-08-24 01:26:02 +0000938}
939
Jan Kiszka21d5d122009-09-15 13:36:04 +0200940#define QEMU_NUM_CLOCKS 3
941
bellard8a7ddc32004-03-31 19:00:16 +0000942QEMUClock *rt_clock;
943QEMUClock *vm_clock;
Jan Kiszka21d5d122009-09-15 13:36:04 +0200944QEMUClock *host_clock;
bellard8a7ddc32004-03-31 19:00:16 +0000945
Jan Kiszka21d5d122009-09-15 13:36:04 +0200946static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
bellard8a7ddc32004-03-31 19:00:16 +0000947
pbrook9596ebb2007-11-18 01:44:38 +0000948static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000949{
950 QEMUClock *clock;
951 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +0000952 clock->type = type;
953 return clock;
954}
955
956QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
957{
958 QEMUTimer *ts;
959
960 ts = qemu_mallocz(sizeof(QEMUTimer));
961 ts->clock = clock;
962 ts->cb = cb;
963 ts->opaque = opaque;
964 return ts;
965}
966
967void qemu_free_timer(QEMUTimer *ts)
968{
969 qemu_free(ts);
970}
971
972/* stop a timer, but do not dealloc it */
973void qemu_del_timer(QEMUTimer *ts)
974{
975 QEMUTimer **pt, *t;
976
977 /* NOTE: this code must be signal safe because
978 qemu_timer_expired() can be called from a signal. */
979 pt = &active_timers[ts->clock->type];
980 for(;;) {
981 t = *pt;
982 if (!t)
983 break;
984 if (t == ts) {
985 *pt = t->next;
986 break;
987 }
988 pt = &t->next;
989 }
990}
991
992/* modify the current timer so that it will be fired when current_time
993 >= expire_time. The corresponding callback will be called. */
994void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
995{
996 QEMUTimer **pt, *t;
997
998 qemu_del_timer(ts);
999
1000 /* add the timer in the sorted list */
1001 /* NOTE: this code must be signal safe because
1002 qemu_timer_expired() can be called from a signal. */
1003 pt = &active_timers[ts->clock->type];
1004 for(;;) {
1005 t = *pt;
1006 if (!t)
1007 break;
ths5fafdf22007-09-16 21:08:06 +00001008 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001009 break;
1010 pt = &t->next;
1011 }
1012 ts->expire_time = expire_time;
1013 ts->next = *pt;
1014 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001015
1016 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001017 if (pt == &active_timers[ts->clock->type]) {
1018 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1019 qemu_rearm_alarm_timer(alarm_timer);
1020 }
1021 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +00001022 if (use_icount)
1023 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +00001024 }
bellard8a7ddc32004-03-31 19:00:16 +00001025}
1026
1027int qemu_timer_pending(QEMUTimer *ts)
1028{
1029 QEMUTimer *t;
1030 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1031 if (t == ts)
1032 return 1;
1033 }
1034 return 0;
1035}
1036
Stefano Stabellini2430ffe2009-08-03 10:56:01 +01001037int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001038{
1039 if (!timer_head)
1040 return 0;
1041 return (timer_head->expire_time <= current_time);
1042}
1043
1044static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1045{
1046 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001047
bellard8a7ddc32004-03-31 19:00:16 +00001048 for(;;) {
1049 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001050 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001051 break;
1052 /* remove timer from the list before calling the callback */
1053 *ptimer_head = ts->next;
1054 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001055
bellard8a7ddc32004-03-31 19:00:16 +00001056 /* run the callback (the timer list can be modified) */
1057 ts->cb(ts->opaque);
1058 }
1059}
1060
1061int64_t qemu_get_clock(QEMUClock *clock)
1062{
1063 switch(clock->type) {
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001064 case QEMU_CLOCK_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001065 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001066 default:
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001067 case QEMU_CLOCK_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001068 if (use_icount) {
1069 return cpu_get_icount();
1070 } else {
1071 return cpu_get_clock();
1072 }
Jan Kiszka21d5d122009-09-15 13:36:04 +02001073 case QEMU_CLOCK_HOST:
1074 return get_clock_realtime();
bellard8a7ddc32004-03-31 19:00:16 +00001075 }
1076}
1077
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001078static void init_clocks(void)
bellard1dce7c32006-07-13 23:20:22 +00001079{
1080 init_get_clock();
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001081 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
1082 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001083 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Jan Kiszka68752042009-09-15 13:36:04 +02001084
1085 rtc_clock = host_clock;
bellard1dce7c32006-07-13 23:20:22 +00001086}
1087
bellard8a7ddc32004-03-31 19:00:16 +00001088/* save a timer */
1089void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1090{
1091 uint64_t expire_time;
1092
1093 if (qemu_timer_pending(ts)) {
1094 expire_time = ts->expire_time;
1095 } else {
1096 expire_time = -1;
1097 }
1098 qemu_put_be64(f, expire_time);
1099}
1100
1101void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1102{
1103 uint64_t expire_time;
1104
1105 expire_time = qemu_get_be64(f);
1106 if (expire_time != -1) {
1107 qemu_mod_timer(ts, expire_time);
1108 } else {
1109 qemu_del_timer(ts);
1110 }
1111}
1112
Juan Quintela2faf58c2009-09-10 03:04:28 +02001113static const VMStateDescription vmstate_timers = {
1114 .name = "timer",
1115 .version_id = 2,
1116 .minimum_version_id = 1,
1117 .minimum_version_id_old = 1,
1118 .fields = (VMStateField []) {
1119 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Anthony Liguori274dfed2009-09-11 10:28:26 -05001120 VMSTATE_INT64(dummy, TimersState),
Juan Quintela2faf58c2009-09-10 03:04:28 +02001121 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
1122 VMSTATE_END_OF_LIST()
bellardc88676f2006-08-06 13:36:11 +00001123 }
Juan Quintela2faf58c2009-09-10 03:04:28 +02001124};
bellard8a7ddc32004-03-31 19:00:16 +00001125
aliguori50317c72009-04-24 18:03:29 +00001126static void qemu_event_increment(void);
1127
bellard67b915a2004-03-31 23:37:16 +00001128#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001129static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1130 DWORD_PTR dwUser, DWORD_PTR dw1,
1131 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001132#else
bellard8a7ddc32004-03-31 19:00:16 +00001133static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001134#endif
bellard8a7ddc32004-03-31 19:00:16 +00001135{
bellard02ba45c2004-06-25 14:46:23 +00001136#if 0
1137#define DISP_FREQ 1000
1138 {
1139 static int64_t delta_min = INT64_MAX;
1140 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1141 static int count;
1142 ti = qemu_get_clock(vm_clock);
1143 if (last_clock != 0) {
1144 delta = ti - last_clock;
1145 if (delta < delta_min)
1146 delta_min = delta;
1147 if (delta > delta_max)
1148 delta_max = delta;
1149 delta_cum += delta;
1150 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001151 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001152 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
1153 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
1154 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
1155 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
bellard02ba45c2004-06-25 14:46:23 +00001156 count = 0;
1157 delta_min = INT64_MAX;
1158 delta_max = 0;
1159 delta_cum = 0;
1160 }
1161 }
1162 last_clock = ti;
1163 }
1164#endif
thsefe75412007-08-24 01:36:32 +00001165 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001166 (!use_icount &&
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001167 qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
pbrook2e70f6e2008-06-29 01:03:05 +00001168 qemu_get_clock(vm_clock))) ||
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001169 qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
Jan Kiszka21d5d122009-09-15 13:36:04 +02001170 qemu_get_clock(rt_clock)) ||
1171 qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
1172 qemu_get_clock(host_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001173 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001174 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001175
aliguorid6dc3d42009-04-24 18:04:07 +00001176#ifndef CONFIG_IOTHREAD
1177 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001178 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001179 cpu_exit(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001180 }
aliguorid6dc3d42009-04-24 18:04:07 +00001181#endif
aliguori43b96852009-04-24 18:03:33 +00001182 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001183 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001184 }
1185}
1186
pbrook2e70f6e2008-06-29 01:03:05 +00001187static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001188{
Jan Kiszka21d5d122009-09-15 13:36:04 +02001189 /* To avoid problems with overflow limit this to 2^32. */
1190 int64_t delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001191
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001192 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
1193 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001194 qemu_get_clock(vm_clock);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001195 }
1196 if (active_timers[QEMU_CLOCK_HOST]) {
1197 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
1198 qemu_get_clock(host_clock);
1199 if (hdelta < delta)
1200 delta = hdelta;
thsefe75412007-08-24 01:36:32 +00001201 }
1202
pbrook2e70f6e2008-06-29 01:03:05 +00001203 if (delta < 0)
1204 delta = 0;
thsefe75412007-08-24 01:36:32 +00001205
pbrook2e70f6e2008-06-29 01:03:05 +00001206 return delta;
1207}
1208
Jan Kiszkaf64382b2009-09-15 13:36:04 +02001209#if defined(__linux__)
pbrook2e70f6e2008-06-29 01:03:05 +00001210static uint64_t qemu_next_deadline_dyntick(void)
1211{
1212 int64_t delta;
1213 int64_t rtdelta;
1214
1215 if (use_icount)
1216 delta = INT32_MAX;
1217 else
1218 delta = (qemu_next_deadline() + 999) / 1000;
1219
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001220 if (active_timers[QEMU_CLOCK_REALTIME]) {
1221 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001222 qemu_get_clock(rt_clock))*1000;
1223 if (rtdelta < delta)
1224 delta = rtdelta;
1225 }
1226
1227 if (delta < MIN_TIMER_REARM_US)
1228 delta = MIN_TIMER_REARM_US;
1229
1230 return delta;
thsefe75412007-08-24 01:36:32 +00001231}
blueswir18632fb92008-09-14 13:59:34 +00001232#endif
thsefe75412007-08-24 01:36:32 +00001233
bellardfd872592004-05-12 19:11:15 +00001234#ifndef _WIN32
1235
aliguori7183b4b2008-11-05 20:40:18 +00001236/* Sets a specific flag */
1237static int fcntl_setfl(int fd, int flag)
1238{
1239 int flags;
1240
1241 flags = fcntl(fd, F_GETFL);
1242 if (flags == -1)
1243 return -errno;
1244
1245 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1246 return -errno;
1247
1248 return 0;
1249}
1250
bellard829309c2004-05-20 13:20:12 +00001251#if defined(__linux__)
1252
bellardfd872592004-05-12 19:11:15 +00001253#define RTC_FREQ 1024
1254
aurel32de9a95f2008-11-11 13:41:01 +00001255static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001256{
thsc8994012007-08-19 21:56:03 +00001257 struct sigaction act;
1258
1259 /* timer signal */
1260 sigfillset(&act.sa_mask);
1261 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001262 act.sa_handler = host_alarm_handler;
1263
1264 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001265 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001266 fcntl(fd, F_SETOWN, getpid());
1267}
1268
thsc40ec5a2007-08-19 22:09:40 +00001269static int hpet_start_timer(struct qemu_alarm_timer *t)
1270{
1271 struct hpet_info info;
1272 int r, fd;
1273
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001274 fd = qemu_open("/dev/hpet", O_RDONLY);
thsc40ec5a2007-08-19 22:09:40 +00001275 if (fd < 0)
1276 return -1;
1277
1278 /* Set frequency */
1279 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1280 if (r < 0) {
1281 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1282 "error, but for better emulation accuracy type:\n"
1283 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1284 goto fail;
1285 }
1286
1287 /* Check capabilities */
1288 r = ioctl(fd, HPET_INFO, &info);
1289 if (r < 0)
1290 goto fail;
1291
1292 /* Enable periodic mode */
1293 r = ioctl(fd, HPET_EPI, 0);
1294 if (info.hi_flags && (r < 0))
1295 goto fail;
1296
1297 /* Enable interrupt */
1298 r = ioctl(fd, HPET_IE_ON, 0);
1299 if (r < 0)
1300 goto fail;
1301
1302 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001303 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001304
1305 return 0;
1306fail:
1307 close(fd);
1308 return -1;
1309}
1310
1311static void hpet_stop_timer(struct qemu_alarm_timer *t)
1312{
pbrookfcdc2122007-08-23 20:22:22 +00001313 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001314
1315 close(fd);
1316}
1317
thsc8994012007-08-19 21:56:03 +00001318static int rtc_start_timer(struct qemu_alarm_timer *t)
1319{
1320 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001321 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001322
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001323 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001324 if (rtc_fd < 0)
1325 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001326 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1327 if (current_rtc_freq != RTC_FREQ &&
1328 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001329 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1330 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1331 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1332 goto fail;
1333 }
1334 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1335 fail:
1336 close(rtc_fd);
1337 return -1;
1338 }
thsc8994012007-08-19 21:56:03 +00001339
1340 enable_sigio_timer(rtc_fd);
1341
pbrookfcdc2122007-08-23 20:22:22 +00001342 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001343
bellardfd872592004-05-12 19:11:15 +00001344 return 0;
1345}
1346
thsc8994012007-08-19 21:56:03 +00001347static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001348{
pbrookfcdc2122007-08-23 20:22:22 +00001349 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001350
1351 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001352}
1353
thsefe75412007-08-24 01:36:32 +00001354static int dynticks_start_timer(struct qemu_alarm_timer *t)
1355{
1356 struct sigevent ev;
1357 timer_t host_timer;
1358 struct sigaction act;
1359
1360 sigfillset(&act.sa_mask);
1361 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001362 act.sa_handler = host_alarm_handler;
1363
1364 sigaction(SIGALRM, &act, NULL);
1365
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001366 /*
1367 * Initialize ev struct to 0 to avoid valgrind complaining
1368 * about uninitialized data in timer_create call
1369 */
1370 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001371 ev.sigev_value.sival_int = 0;
1372 ev.sigev_notify = SIGEV_SIGNAL;
1373 ev.sigev_signo = SIGALRM;
1374
1375 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1376 perror("timer_create");
1377
1378 /* disable dynticks */
1379 fprintf(stderr, "Dynamic Ticks disabled\n");
1380
1381 return -1;
1382 }
1383
blueswir10399bfe2008-11-16 11:37:18 +00001384 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001385
1386 return 0;
1387}
1388
1389static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1390{
blueswir10399bfe2008-11-16 11:37:18 +00001391 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001392
1393 timer_delete(host_timer);
1394}
1395
1396static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1397{
blueswir10399bfe2008-11-16 11:37:18 +00001398 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001399 struct itimerspec timeout;
1400 int64_t nearest_delta_us = INT64_MAX;
1401 int64_t current_us;
1402
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001403 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001404 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1405 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001406 return;
thsefe75412007-08-24 01:36:32 +00001407
pbrook2e70f6e2008-06-29 01:03:05 +00001408 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001409
1410 /* check whether a timer is already running */
1411 if (timer_gettime(host_timer, &timeout)) {
1412 perror("gettime");
1413 fprintf(stderr, "Internal timer error: aborting\n");
1414 exit(1);
1415 }
1416 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1417 if (current_us && current_us <= nearest_delta_us)
1418 return;
1419
1420 timeout.it_interval.tv_sec = 0;
1421 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1422 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1423 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1424 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1425 perror("settime");
1426 fprintf(stderr, "Internal timer error: aborting\n");
1427 exit(1);
1428 }
1429}
1430
ths70744b32007-08-26 17:31:30 +00001431#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001432
thsc8994012007-08-19 21:56:03 +00001433static int unix_start_timer(struct qemu_alarm_timer *t)
1434{
1435 struct sigaction act;
1436 struct itimerval itv;
1437 int err;
1438
1439 /* timer signal */
1440 sigfillset(&act.sa_mask);
1441 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001442 act.sa_handler = host_alarm_handler;
1443
1444 sigaction(SIGALRM, &act, NULL);
1445
1446 itv.it_interval.tv_sec = 0;
1447 /* for i386 kernel 2.6 to get 1 ms */
1448 itv.it_interval.tv_usec = 999;
1449 itv.it_value.tv_sec = 0;
1450 itv.it_value.tv_usec = 10 * 1000;
1451
1452 err = setitimer(ITIMER_REAL, &itv, NULL);
1453 if (err)
1454 return -1;
1455
1456 return 0;
1457}
1458
1459static void unix_stop_timer(struct qemu_alarm_timer *t)
1460{
1461 struct itimerval itv;
1462
1463 memset(&itv, 0, sizeof(itv));
1464 setitimer(ITIMER_REAL, &itv, NULL);
1465}
1466
bellard829309c2004-05-20 13:20:12 +00001467#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001468
aliguorif49e58d2008-11-05 21:22:34 +00001469
thsc8994012007-08-19 21:56:03 +00001470#ifdef _WIN32
1471
1472static int win32_start_timer(struct qemu_alarm_timer *t)
1473{
1474 TIMECAPS tc;
1475 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001476 UINT flags;
thsc8994012007-08-19 21:56:03 +00001477
thsc8994012007-08-19 21:56:03 +00001478 memset(&tc, 0, sizeof(tc));
1479 timeGetDevCaps(&tc, sizeof(tc));
1480
1481 if (data->period < tc.wPeriodMin)
1482 data->period = tc.wPeriodMin;
1483
1484 timeBeginPeriod(data->period);
1485
thsefe75412007-08-24 01:36:32 +00001486 flags = TIME_CALLBACK_FUNCTION;
1487 if (alarm_has_dynticks(t))
1488 flags |= TIME_ONESHOT;
1489 else
1490 flags |= TIME_PERIODIC;
1491
thsc8994012007-08-19 21:56:03 +00001492 data->timerId = timeSetEvent(1, // interval (ms)
1493 data->period, // resolution
1494 host_alarm_handler, // function
1495 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001496 flags);
thsc8994012007-08-19 21:56:03 +00001497
1498 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001499 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
malc705e83f2009-09-27 14:30:33 +04001500 GetLastError());
thsc8994012007-08-19 21:56:03 +00001501 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001502 return -1;
1503 }
1504
thsc8994012007-08-19 21:56:03 +00001505 return 0;
1506}
1507
1508static void win32_stop_timer(struct qemu_alarm_timer *t)
1509{
1510 struct qemu_alarm_win32 *data = t->priv;
1511
1512 timeKillEvent(data->timerId);
1513 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001514}
1515
thsefe75412007-08-24 01:36:32 +00001516static void win32_rearm_timer(struct qemu_alarm_timer *t)
1517{
1518 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001519
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001520 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001521 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1522 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001523 return;
thsefe75412007-08-24 01:36:32 +00001524
thsefe75412007-08-24 01:36:32 +00001525 timeKillEvent(data->timerId);
1526
1527 data->timerId = timeSetEvent(1,
1528 data->period,
1529 host_alarm_handler,
1530 (DWORD)t,
1531 TIME_ONESHOT | TIME_PERIODIC);
1532
1533 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001534 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
malc705e83f2009-09-27 14:30:33 +04001535 GetLastError());
thsefe75412007-08-24 01:36:32 +00001536
1537 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001538 exit(1);
1539 }
1540}
1541
thsc8994012007-08-19 21:56:03 +00001542#endif /* _WIN32 */
1543
aliguori7183b4b2008-11-05 20:40:18 +00001544static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001545{
blueswir1223f0d72008-09-30 18:12:18 +00001546 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001547 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001548
thsc8994012007-08-19 21:56:03 +00001549 for (i = 0; alarm_timers[i].name; i++) {
1550 t = &alarm_timers[i];
1551
thsc8994012007-08-19 21:56:03 +00001552 err = t->start(t);
1553 if (!err)
1554 break;
bellard67b915a2004-03-31 23:37:16 +00001555 }
bellardfd872592004-05-12 19:11:15 +00001556
thsc8994012007-08-19 21:56:03 +00001557 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001558 err = -ENOENT;
1559 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001560 }
thsc8994012007-08-19 21:56:03 +00001561
1562 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001563
aliguori6abfbd72008-11-05 20:49:37 +00001564 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001565
1566fail:
aliguori7183b4b2008-11-05 20:40:18 +00001567 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001568}
1569
pbrook9596ebb2007-11-18 01:44:38 +00001570static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001571{
thsc8994012007-08-19 21:56:03 +00001572 alarm_timer->stop(alarm_timer);
1573 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001574}
1575
bellardc4b1fcc2004-03-14 21:44:30 +00001576/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001577/* host time/date access */
1578void qemu_get_timedate(struct tm *tm, int offset)
1579{
1580 time_t ti;
1581 struct tm *ret;
1582
1583 time(&ti);
1584 ti += offset;
1585 if (rtc_date_offset == -1) {
1586 if (rtc_utc)
1587 ret = gmtime(&ti);
1588 else
1589 ret = localtime(&ti);
1590 } else {
1591 ti -= rtc_date_offset;
1592 ret = gmtime(&ti);
1593 }
1594
1595 memcpy(tm, ret, sizeof(struct tm));
1596}
1597
1598int qemu_timedate_diff(struct tm *tm)
1599{
1600 time_t seconds;
1601
1602 if (rtc_date_offset == -1)
1603 if (rtc_utc)
1604 seconds = mktimegm(tm);
1605 else
1606 seconds = mktime(tm);
1607 else
1608 seconds = mktimegm(tm) + rtc_date_offset;
1609
1610 return seconds - time(NULL);
1611}
1612
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001613static void configure_rtc_date_offset(const char *startdate, int legacy)
1614{
1615 time_t rtc_start_date;
1616 struct tm tm;
1617
1618 if (!strcmp(startdate, "now") && legacy) {
1619 rtc_date_offset = -1;
1620 } else {
1621 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
1622 &tm.tm_year,
1623 &tm.tm_mon,
1624 &tm.tm_mday,
1625 &tm.tm_hour,
1626 &tm.tm_min,
1627 &tm.tm_sec) == 6) {
1628 /* OK */
1629 } else if (sscanf(startdate, "%d-%d-%d",
1630 &tm.tm_year,
1631 &tm.tm_mon,
1632 &tm.tm_mday) == 3) {
1633 tm.tm_hour = 0;
1634 tm.tm_min = 0;
1635 tm.tm_sec = 0;
1636 } else {
1637 goto date_fail;
1638 }
1639 tm.tm_year -= 1900;
1640 tm.tm_mon--;
1641 rtc_start_date = mktimegm(&tm);
1642 if (rtc_start_date == -1) {
1643 date_fail:
1644 fprintf(stderr, "Invalid date format. Valid formats are:\n"
1645 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1646 exit(1);
1647 }
1648 rtc_date_offset = time(NULL) - rtc_start_date;
1649 }
1650}
1651
1652static void configure_rtc(QemuOpts *opts)
1653{
1654 const char *value;
1655
1656 value = qemu_opt_get(opts, "base");
1657 if (value) {
1658 if (!strcmp(value, "utc")) {
1659 rtc_utc = 1;
1660 } else if (!strcmp(value, "localtime")) {
1661 rtc_utc = 0;
1662 } else {
1663 configure_rtc_date_offset(value, 0);
1664 }
1665 }
Jan Kiszka68752042009-09-15 13:36:04 +02001666 value = qemu_opt_get(opts, "clock");
1667 if (value) {
1668 if (!strcmp(value, "host")) {
1669 rtc_clock = host_clock;
1670 } else if (!strcmp(value, "vm")) {
1671 rtc_clock = vm_clock;
1672 } else {
1673 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1674 exit(1);
1675 }
1676 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001677#ifdef CONFIG_TARGET_I386
1678 value = qemu_opt_get(opts, "driftfix");
1679 if (value) {
1680 if (!strcmp(buf, "slew")) {
1681 rtc_td_hack = 1;
1682 } else if (!strcmp(buf, "none")) {
1683 rtc_td_hack = 0;
1684 } else {
1685 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1686 exit(1);
1687 }
1688 }
1689#endif
1690}
1691
bellardfd1dff42006-02-01 21:29:26 +00001692#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001693static void socket_cleanup(void)
1694{
1695 WSACleanup();
1696}
bellard82c643f2004-07-14 17:28:13 +00001697
bellardfd1dff42006-02-01 21:29:26 +00001698static int socket_init(void)
1699{
1700 WSADATA Data;
1701 int ret, err;
1702
1703 ret = WSAStartup(MAKEWORD(2,2), &Data);
1704 if (ret != 0) {
1705 err = WSAGetLastError();
1706 fprintf(stderr, "WSAStartup: %d\n", err);
1707 return -1;
1708 }
1709 atexit(socket_cleanup);
1710 return 0;
1711}
aurel3264b7b732008-05-05 10:05:31 +00001712#endif
1713
balrog1ae26a12008-09-28 23:19:47 +00001714/***********************************************************/
1715/* Bluetooth support */
1716static int nb_hcis;
1717static int cur_hci;
1718static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001719
balrog1ae26a12008-09-28 23:19:47 +00001720static struct bt_vlan_s {
1721 struct bt_scatternet_s net;
1722 int id;
1723 struct bt_vlan_s *next;
1724} *first_bt_vlan;
1725
1726/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001727static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001728{
1729 struct bt_vlan_s **pvlan, *vlan;
1730 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1731 if (vlan->id == id)
1732 return &vlan->net;
1733 }
1734 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1735 vlan->id = id;
1736 pvlan = &first_bt_vlan;
1737 while (*pvlan != NULL)
1738 pvlan = &(*pvlan)->next;
1739 *pvlan = vlan;
1740 return &vlan->net;
1741}
1742
1743static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1744{
1745}
1746
1747static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1748{
1749 return -ENOTSUP;
1750}
1751
1752static struct HCIInfo null_hci = {
1753 .cmd_send = null_hci_send,
1754 .sco_send = null_hci_send,
1755 .acl_send = null_hci_send,
1756 .bdaddr_set = null_hci_addr_set,
1757};
1758
1759struct HCIInfo *qemu_next_hci(void)
1760{
1761 if (cur_hci == nb_hcis)
1762 return &null_hci;
1763
1764 return hci_table[cur_hci++];
1765}
1766
balrogdc72ac12008-11-09 00:04:26 +00001767static struct HCIInfo *hci_init(const char *str)
1768{
1769 char *endp;
1770 struct bt_scatternet_s *vlan = 0;
1771
1772 if (!strcmp(str, "null"))
1773 /* null */
1774 return &null_hci;
1775 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1776 /* host[:hciN] */
1777 return bt_host_hci(str[4] ? str + 5 : "hci0");
1778 else if (!strncmp(str, "hci", 3)) {
1779 /* hci[,vlan=n] */
1780 if (str[3]) {
1781 if (!strncmp(str + 3, ",vlan=", 6)) {
1782 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1783 if (*endp)
1784 vlan = 0;
1785 }
1786 } else
1787 vlan = qemu_find_bt_vlan(0);
1788 if (vlan)
1789 return bt_new_hci(vlan);
1790 }
1791
1792 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1793
1794 return 0;
1795}
1796
1797static int bt_hci_parse(const char *str)
1798{
1799 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -05001800 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +00001801
1802 if (nb_hcis >= MAX_NICS) {
1803 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1804 return -1;
1805 }
1806
1807 hci = hci_init(str);
1808 if (!hci)
1809 return -1;
1810
1811 bdaddr.b[0] = 0x52;
1812 bdaddr.b[1] = 0x54;
1813 bdaddr.b[2] = 0x00;
1814 bdaddr.b[3] = 0x12;
1815 bdaddr.b[4] = 0x34;
1816 bdaddr.b[5] = 0x56 + nb_hcis;
1817 hci->bdaddr_set(hci, bdaddr.b);
1818
1819 hci_table[nb_hcis++] = hci;
1820
1821 return 0;
1822}
1823
1824static void bt_vhci_add(int vlan_id)
1825{
1826 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1827
1828 if (!vlan->slave)
1829 fprintf(stderr, "qemu: warning: adding a VHCI to "
1830 "an empty scatternet %i\n", vlan_id);
1831
1832 bt_vhci_init(bt_new_hci(vlan));
1833}
1834
1835static struct bt_device_s *bt_device_add(const char *opt)
1836{
1837 struct bt_scatternet_s *vlan;
1838 int vlan_id = 0;
1839 char *endp = strstr(opt, ",vlan=");
1840 int len = (endp ? endp - opt : strlen(opt)) + 1;
1841 char devname[10];
1842
1843 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1844
1845 if (endp) {
1846 vlan_id = strtol(endp + 6, &endp, 0);
1847 if (*endp) {
1848 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1849 return 0;
1850 }
1851 }
1852
1853 vlan = qemu_find_bt_vlan(vlan_id);
1854
1855 if (!vlan->slave)
1856 fprintf(stderr, "qemu: warning: adding a slave device to "
1857 "an empty scatternet %i\n", vlan_id);
1858
1859 if (!strcmp(devname, "keyboard"))
1860 return bt_keyboard_init(vlan);
1861
1862 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1863 return 0;
1864}
1865
1866static int bt_parse(const char *opt)
1867{
1868 const char *endp, *p;
1869 int vlan;
1870
1871 if (strstart(opt, "hci", &endp)) {
1872 if (!*endp || *endp == ',') {
1873 if (*endp)
1874 if (!strstart(endp, ",vlan=", 0))
1875 opt = endp + 1;
1876
1877 return bt_hci_parse(opt);
1878 }
1879 } else if (strstart(opt, "vhci", &endp)) {
1880 if (!*endp || *endp == ',') {
1881 if (*endp) {
1882 if (strstart(endp, ",vlan=", &p)) {
1883 vlan = strtol(p, (char **) &endp, 0);
1884 if (*endp) {
1885 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1886 return 1;
1887 }
1888 } else {
1889 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1890 return 1;
1891 }
1892 } else
1893 vlan = 0;
1894
1895 bt_vhci_add(vlan);
1896 return 0;
1897 }
1898 } else if (strstart(opt, "device:", &endp))
1899 return !bt_device_add(endp);
1900
1901 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1902 return 1;
1903}
1904
balrog1ae26a12008-09-28 23:19:47 +00001905/***********************************************************/
1906/* QEMU Block devices */
1907
balrog609497a2008-01-14 02:56:53 +00001908#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001909#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001910#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001911#define PFLASH_ALIAS "if=pflash"
1912#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001913#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001914
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001915QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001916{
1917 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001918 char optstr[1024];
1919 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +00001920
thse4bcb142007-12-02 04:51:10 +00001921 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001922 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001923 va_end(ap);
1924
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001925 opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001926 if (!opts) {
1927 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1928 __FUNCTION__, optstr);
1929 return NULL;
1930 }
1931 if (file)
1932 qemu_opt_set(opts, "file", file);
1933 return opts;
aliguorib01b1112009-02-11 15:20:20 +00001934}
1935
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001936DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001937{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001938 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001939
1940 /* seek interface, bus and unit */
1941
Blue Swirl72cf2d42009-09-12 07:36:22 +00001942 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001943 if (dinfo->type == type &&
1944 dinfo->bus == bus &&
1945 dinfo->unit == unit)
1946 return dinfo;
1947 }
thse4bcb142007-12-02 04:51:10 +00001948
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001949 return NULL;
thse4bcb142007-12-02 04:51:10 +00001950}
1951
Gerd Hoffmann2e810b32009-07-31 12:25:38 +02001952DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001953{
1954 DriveInfo *dinfo;
1955
Blue Swirl72cf2d42009-09-12 07:36:22 +00001956 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001957 if (strcmp(id, dinfo->id))
1958 continue;
1959 return dinfo;
1960 }
1961 return NULL;
1962}
1963
thsf60d39b2007-12-17 03:55:57 +00001964int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001965{
1966 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001967 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001968
1969 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001970 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001971 if(dinfo->type == type &&
1972 dinfo->bus > max_bus)
1973 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +00001974 }
1975 return max_bus;
1976}
1977
aliguorifa879c62009-01-07 17:32:33 +00001978const char *drive_get_serial(BlockDriverState *bdrv)
1979{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001980 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +00001981
Blue Swirl72cf2d42009-09-12 07:36:22 +00001982 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001983 if (dinfo->bdrv == bdrv)
1984 return dinfo->serial;
1985 }
aliguorifa879c62009-01-07 17:32:33 +00001986
1987 return "\0";
1988}
1989
Kevin Wolff7850092009-11-27 13:25:36 +01001990BlockInterfaceErrorAction drive_get_on_error(
1991 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +00001992{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001993 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +00001994
Kevin Wolffc072ec2009-11-27 13:25:36 +01001995 if (is_read) {
1996 return BLOCK_ERR_REPORT;
1997 }
1998
Blue Swirl72cf2d42009-09-12 07:36:22 +00001999 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002000 if (dinfo->bdrv == bdrv)
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002001 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002002 }
aliguori428c5702009-01-21 18:59:04 +00002003
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002004 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002005}
2006
aurel32a1620fa2008-04-29 05:58:01 +00002007static void bdrv_format_print(void *opaque, const char *name)
2008{
2009 fprintf(stderr, " %s", name);
2010}
2011
Gerd Hoffmann56a14932009-09-25 21:42:46 +02002012void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +00002013{
Gerd Hoffmann56a14932009-09-25 21:42:46 +02002014 qemu_opts_del(dinfo->opts);
2015 bdrv_delete(dinfo->bdrv);
2016 QTAILQ_REMOVE(&drives, dinfo, next);
2017 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +00002018}
2019
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002020static int parse_block_error_action(const char *buf, int is_read)
2021{
2022 if (!strcmp(buf, "ignore")) {
2023 return BLOCK_ERR_IGNORE;
2024 } else if (!is_read && !strcmp(buf, "enospc")) {
2025 return BLOCK_ERR_STOP_ENOSPC;
2026 } else if (!strcmp(buf, "stop")) {
2027 return BLOCK_ERR_STOP_ANY;
2028 } else if (!strcmp(buf, "report")) {
2029 return BLOCK_ERR_REPORT;
2030 } else {
2031 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
2032 buf, is_read ? "read" : "write");
2033 return -1;
2034 }
2035}
2036
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002037DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002038 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +00002039{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002040 const char *buf;
2041 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +00002042 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002043 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +00002044 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002045 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002046 enum { MEDIA_DISK, MEDIA_CDROM } media;
2047 int bus_id, unit_id;
2048 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +00002049 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002050 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002051 int max_devs;
2052 int index;
balrog33f00272007-12-24 14:33:24 +00002053 int cache;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002054 int aio = 0;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002055 int ro = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002056 int bdrv_flags;
2057 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002058 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002059 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002060 int snapshot = 0;
2061
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002062 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +00002063
thse4bcb142007-12-02 04:51:10 +00002064 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02002065 cache = 1;
thse4bcb142007-12-02 04:51:10 +00002066
Gerd Hoffmann4d007812009-08-31 14:23:57 +02002067 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002068 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002069 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002070 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002071 } else {
thsf60d39b2007-12-17 03:55:57 +00002072 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002073 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002074 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002075 }
2076 media = MEDIA_DISK;
2077
2078 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002079 bus_id = qemu_opt_get_number(opts, "bus", 0);
2080 unit_id = qemu_opt_get_number(opts, "unit", -1);
2081 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +00002082
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002083 cyls = qemu_opt_get_number(opts, "cyls", 0);
2084 heads = qemu_opt_get_number(opts, "heads", 0);
2085 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +00002086
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002087 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +02002088 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +00002089
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002090 file = qemu_opt_get(opts, "file");
2091 serial = qemu_opt_get(opts, "serial");
2092
2093 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +00002094 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002095 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002096 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002097 max_devs = MAX_IDE_DEVS;
2098 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002099 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002100 max_devs = MAX_SCSI_DEVS;
2101 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002102 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002103 max_devs = 0;
2104 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002105 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002106 max_devs = 0;
2107 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002108 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002109 max_devs = 0;
2110 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002111 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002112 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002113 } else if (!strcmp(buf, "virtio")) {
2114 type = IF_VIRTIO;
2115 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002116 } else if (!strcmp(buf, "xen")) {
2117 type = IF_XEN;
2118 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +02002119 } else if (!strcmp(buf, "none")) {
2120 type = IF_NONE;
2121 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002122 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002123 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002124 return NULL;
thse4bcb142007-12-02 04:51:10 +00002125 }
2126 }
2127
thse4bcb142007-12-02 04:51:10 +00002128 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +00002129 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002130 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002131 return NULL;
thse4bcb142007-12-02 04:51:10 +00002132 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002133 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002134 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002135 return NULL;
thse4bcb142007-12-02 04:51:10 +00002136 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002137 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002138 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002139 return NULL;
thse4bcb142007-12-02 04:51:10 +00002140 }
2141 }
2142
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002143 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002144 if (!cyls) {
2145 fprintf(stderr,
2146 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002147 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002148 return NULL;
thse4bcb142007-12-02 04:51:10 +00002149 }
2150 if (!strcmp(buf, "none"))
2151 translation = BIOS_ATA_TRANSLATION_NONE;
2152 else if (!strcmp(buf, "lba"))
2153 translation = BIOS_ATA_TRANSLATION_LBA;
2154 else if (!strcmp(buf, "auto"))
2155 translation = BIOS_ATA_TRANSLATION_AUTO;
2156 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002157 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002158 return NULL;
thse4bcb142007-12-02 04:51:10 +00002159 }
2160 }
2161
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002162 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002163 if (!strcmp(buf, "disk")) {
2164 media = MEDIA_DISK;
2165 } else if (!strcmp(buf, "cdrom")) {
2166 if (cyls || secs || heads) {
2167 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002168 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002169 return NULL;
thse4bcb142007-12-02 04:51:10 +00002170 }
2171 media = MEDIA_CDROM;
2172 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002173 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002174 return NULL;
thse4bcb142007-12-02 04:51:10 +00002175 }
2176 }
2177
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002178 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00002179 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002180 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002181 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002182 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002183 else if (!strcmp(buf, "writeback"))
2184 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002185 else {
2186 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002187 return NULL;
balrog33f00272007-12-24 14:33:24 +00002188 }
2189 }
2190
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002191#ifdef CONFIG_LINUX_AIO
2192 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
2193 if (!strcmp(buf, "threads"))
2194 aio = 0;
2195 else if (!strcmp(buf, "native"))
2196 aio = 1;
2197 else {
2198 fprintf(stderr, "qemu: invalid aio option\n");
2199 return NULL;
2200 }
2201 }
2202#endif
2203
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002204 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00002205 if (strcmp(buf, "?") == 0) {
2206 fprintf(stderr, "qemu: Supported formats:");
2207 bdrv_iterate_format(bdrv_format_print, NULL);
2208 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002209 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00002210 }
Markus Armbrustereb852012009-10-27 18:41:44 +01002211 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +00002212 if (!drv) {
2213 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002214 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00002215 }
2216 }
2217
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002218 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002219 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00002220 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002221 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002222 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002223 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002224
2225 on_write_error = parse_block_error_action(buf, 0);
2226 if (on_write_error < 0) {
2227 return NULL;
2228 }
2229 }
2230
2231 on_read_error = BLOCK_ERR_REPORT;
2232 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolff35d68f2009-11-27 13:25:39 +01002233 if (type != IF_IDE && type != IF_VIRTIO) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002234 fprintf(stderr, "rerror is no supported by this format\n");
2235 return NULL;
2236 }
2237
2238 on_read_error = parse_block_error_action(buf, 1);
2239 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002240 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002241 }
2242 }
2243
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002244 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002245 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002246 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002247 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002248 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002249 }
2250
thse4bcb142007-12-02 04:51:10 +00002251 /* compute bus and unit according index */
2252
2253 if (index != -1) {
2254 if (bus_id != 0 || unit_id != -1) {
2255 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002256 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002257 return NULL;
thse4bcb142007-12-02 04:51:10 +00002258 }
2259 if (max_devs == 0)
2260 {
2261 unit_id = index;
2262 bus_id = 0;
2263 } else {
2264 unit_id = index % max_devs;
2265 bus_id = index / max_devs;
2266 }
2267 }
2268
2269 /* if user doesn't specify a unit_id,
2270 * try to find the first free
2271 */
2272
2273 if (unit_id == -1) {
2274 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002275 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002276 unit_id++;
2277 if (max_devs && unit_id >= max_devs) {
2278 unit_id -= max_devs;
2279 bus_id++;
2280 }
2281 }
2282 }
2283
2284 /* check unit id */
2285
2286 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002287 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2288 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002289 return NULL;
thse4bcb142007-12-02 04:51:10 +00002290 }
2291
2292 /*
2293 * ignore multiple definitions
2294 */
2295
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002296 if (drive_get(type, bus_id, unit_id) != NULL) {
2297 *fatal_error = 0;
2298 return NULL;
2299 }
thse4bcb142007-12-02 04:51:10 +00002300
2301 /* init */
2302
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002303 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02002304 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002305 dinfo->id = qemu_strdup(buf);
2306 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002307 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002308 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002309 if (type == IF_IDE || type == IF_SCSI)
2310 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2311 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002312 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002313 devname, bus_id, mediastr, unit_id);
2314 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002315 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002316 devname, mediastr, unit_id);
2317 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002318 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002319 dinfo->devaddr = devaddr;
2320 dinfo->type = type;
2321 dinfo->bus = bus_id;
2322 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002323 dinfo->on_read_error = on_read_error;
2324 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002325 dinfo->opts = opts;
2326 if (serial)
2327 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00002328 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00002329
thsf60d39b2007-12-17 03:55:57 +00002330 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002331 case IF_IDE:
2332 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002333 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00002334 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00002335 switch(media) {
2336 case MEDIA_DISK:
2337 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002338 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2339 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00002340 }
2341 break;
2342 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002343 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00002344 break;
2345 }
2346 break;
2347 case IF_SD:
2348 /* FIXME: This isn't really a floppy, but it's a reasonable
2349 approximation. */
2350 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002351 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00002352 break;
2353 case IF_PFLASH:
2354 case IF_MTD:
2355 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02002356 case IF_VIRTIO:
2357 /* add virtio block device */
2358 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2359 qemu_opt_set(opts, "driver", "virtio-blk-pci");
2360 qemu_opt_set(opts, "drive", dinfo->id);
2361 if (devaddr)
2362 qemu_opt_set(opts, "addr", devaddr);
2363 break;
Paul Brookaae94602009-05-14 22:35:06 +01002364 case IF_COUNT:
2365 abort();
thse4bcb142007-12-02 04:51:10 +00002366 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002367 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002368 *fatal_error = 0;
2369 return NULL;
2370 }
balrog33f00272007-12-24 14:33:24 +00002371 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002372 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002373 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002374 cache = 2; /* always use write-back with snapshot */
2375 }
2376 if (cache == 0) /* no caching */
2377 bdrv_flags |= BDRV_O_NOCACHE;
2378 else if (cache == 2) /* write-back */
2379 bdrv_flags |= BDRV_O_CACHE_WB;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002380
2381 if (aio == 1) {
2382 bdrv_flags |= BDRV_O_NATIVE_AIO;
2383 } else {
2384 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
2385 }
2386
Naphtali Sprei59f26892009-10-26 16:25:16 +02002387 if (ro == 1) {
2388 if (type == IF_IDE) {
2389 fprintf(stderr, "qemu: readonly flag not supported for drive with ide interface\n");
2390 return NULL;
2391 }
2392 (void)bdrv_set_read_only(dinfo->bdrv, 1);
2393 }
2394
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002395 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05002396 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
2397 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002398 return NULL;
thse4bcb142007-12-02 04:51:10 +00002399 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002400
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002401 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00002402 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002403 *fatal_error = 0;
2404 return dinfo;
thse4bcb142007-12-02 04:51:10 +00002405}
2406
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002407static int drive_init_func(QemuOpts *opts, void *opaque)
2408{
2409 QEMUMachine *machine = opaque;
2410 int fatal_error = 0;
2411
2412 if (drive_init(opts, machine, &fatal_error) == NULL) {
2413 if (fatal_error)
2414 return 1;
2415 }
2416 return 0;
2417}
2418
2419static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2420{
2421 if (NULL == qemu_opt_get(opts, "snapshot")) {
2422 qemu_opt_set(opts, "snapshot", "on");
2423 }
2424 return 0;
2425}
2426
Jan Kiszka76e30d02009-07-02 00:19:02 +02002427void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2428{
2429 boot_set_handler = func;
2430 boot_set_opaque = opaque;
2431}
2432
2433int qemu_boot_set(const char *boot_devices)
2434{
2435 if (!boot_set_handler) {
2436 return -EINVAL;
2437 }
2438 return boot_set_handler(boot_set_opaque, boot_devices);
2439}
2440
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002441static int parse_bootdevices(char *devices)
2442{
2443 /* We just do some generic consistency checks */
2444 const char *p;
2445 int bitmap = 0;
2446
2447 for (p = devices; *p != '\0'; p++) {
2448 /* Allowed boot devices are:
2449 * a-b: floppy disk drives
2450 * c-f: IDE disk drives
2451 * g-m: machine implementation dependant drives
2452 * n-p: network devices
2453 * It's up to each machine implementation to check if the given boot
2454 * devices match the actual hardware implementation and firmware
2455 * features.
2456 */
2457 if (*p < 'a' || *p > 'p') {
2458 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2459 exit(1);
2460 }
2461 if (bitmap & (1 << (*p - 'a'))) {
2462 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2463 exit(1);
2464 }
2465 bitmap |= 1 << (*p - 'a');
2466 }
2467 return bitmap;
2468}
2469
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002470static void restore_boot_devices(void *opaque)
2471{
2472 char *standard_boot_devices = opaque;
2473
2474 qemu_boot_set(standard_boot_devices);
2475
2476 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2477 qemu_free(standard_boot_devices);
2478}
2479
aliguori268a3622009-04-21 22:30:27 +00002480static void numa_add(const char *optarg)
2481{
2482 char option[128];
2483 char *endptr;
2484 unsigned long long value, endvalue;
2485 int nodenr;
2486
2487 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2488 if (!strcmp(option, "node")) {
2489 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2490 nodenr = nb_numa_nodes;
2491 } else {
2492 nodenr = strtoull(option, NULL, 10);
2493 }
2494
2495 if (get_param_value(option, 128, "mem", optarg) == 0) {
2496 node_mem[nodenr] = 0;
2497 } else {
2498 value = strtoull(option, &endptr, 0);
2499 switch (*endptr) {
2500 case 0: case 'M': case 'm':
2501 value <<= 20;
2502 break;
2503 case 'G': case 'g':
2504 value <<= 30;
2505 break;
2506 }
2507 node_mem[nodenr] = value;
2508 }
2509 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2510 node_cpumask[nodenr] = 0;
2511 } else {
2512 value = strtoull(option, &endptr, 10);
2513 if (value >= 64) {
2514 value = 63;
2515 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2516 } else {
2517 if (*endptr == '-') {
2518 endvalue = strtoull(endptr+1, &endptr, 10);
2519 if (endvalue >= 63) {
2520 endvalue = 62;
2521 fprintf(stderr,
2522 "only 63 CPUs in NUMA mode supported.\n");
2523 }
2524 value = (1 << (endvalue + 1)) - (1 << value);
2525 } else {
2526 value = 1 << value;
2527 }
2528 }
2529 node_cpumask[nodenr] = value;
2530 }
2531 nb_numa_nodes++;
2532 }
2533 return;
2534}
2535
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002536static void smp_parse(const char *optarg)
2537{
2538 int smp, sockets = 0, threads = 0, cores = 0;
2539 char *endptr;
2540 char option[128];
2541
2542 smp = strtoul(optarg, &endptr, 10);
2543 if (endptr != optarg) {
2544 if (*endptr == ',') {
2545 endptr++;
2546 }
2547 }
2548 if (get_param_value(option, 128, "sockets", endptr) != 0)
2549 sockets = strtoull(option, NULL, 10);
2550 if (get_param_value(option, 128, "cores", endptr) != 0)
2551 cores = strtoull(option, NULL, 10);
2552 if (get_param_value(option, 128, "threads", endptr) != 0)
2553 threads = strtoull(option, NULL, 10);
2554 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
2555 max_cpus = strtoull(option, NULL, 10);
2556
2557 /* compute missing values, prefer sockets over cores over threads */
2558 if (smp == 0 || sockets == 0) {
2559 sockets = sockets > 0 ? sockets : 1;
2560 cores = cores > 0 ? cores : 1;
2561 threads = threads > 0 ? threads : 1;
2562 if (smp == 0) {
2563 smp = cores * threads * sockets;
2564 } else {
2565 sockets = smp / (cores * threads);
2566 }
2567 } else {
2568 if (cores == 0) {
2569 threads = threads > 0 ? threads : 1;
2570 cores = smp / (sockets * threads);
2571 } else {
2572 if (sockets == 0) {
2573 sockets = smp / (cores * threads);
2574 } else {
2575 threads = smp / (cores * sockets);
2576 }
2577 }
2578 }
2579 smp_cpus = smp;
2580 smp_cores = cores > 0 ? cores : 1;
2581 smp_threads = threads > 0 ? threads : 1;
2582 if (max_cpus == 0)
2583 max_cpus = smp_cpus;
2584}
2585
bellard330d0412003-07-26 18:11:40 +00002586/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002587/* USB devices */
2588
aliguoric0f4ce72009-03-05 23:01:01 +00002589static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002590{
2591 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002592 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00002593
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002594 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00002595 return -1;
2596
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01002597 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2598 dev = usbdevice_create(devname);
2599 if (dev)
2600 goto done;
2601
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002602 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00002603 if (strstart(devname, "host:", &p)) {
2604 dev = usb_host_device_open(p);
balrog6c9f8862008-07-17 20:47:13 +00002605 } else if (strstart(devname, "net:", &p)) {
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002606 QemuOpts *opts;
2607 int idx;
balrog6c9f8862008-07-17 20:47:13 +00002608
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002609 opts = qemu_opts_parse(&qemu_net_opts, p, NULL);
2610 if (!opts) {
balrog6c9f8862008-07-17 20:47:13 +00002611 return -1;
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002612 }
2613
2614 qemu_opt_set(opts, "type", "nic");
2615 qemu_opt_set(opts, "model", "usb");
2616
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01002617 idx = net_client_init(NULL, opts, 0);
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01002618 if (idx == -1) {
2619 return -1;
2620 }
2621
2622 dev = usb_net_init(&nd_table[idx]);
balrogdc72ac12008-11-09 00:04:26 +00002623 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2624 dev = usb_bt_init(devname[2] ? hci_init(p) :
2625 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002626 } else {
2627 return -1;
2628 }
pbrook0d92ed32006-05-21 16:30:15 +00002629 if (!dev)
2630 return -1;
2631
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002632done:
bellarda594cfb2005-11-06 16:13:29 +00002633 return 0;
2634}
2635
aliguori1f3870a2008-08-21 19:27:48 +00002636static int usb_device_del(const char *devname)
2637{
2638 int bus_num, addr;
2639 const char *p;
2640
aliguori5d0c5752008-09-14 01:07:41 +00002641 if (strstart(devname, "host:", &p))
2642 return usb_host_device_close(p);
2643
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002644 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00002645 return -1;
2646
2647 p = strchr(devname, '.');
2648 if (!p)
2649 return -1;
2650 bus_num = strtoul(devname, NULL, 0);
2651 addr = strtoul(p + 1, NULL, 0);
2652
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002653 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00002654}
2655
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002656static int usb_parse(const char *cmdline)
2657{
2658 return usb_device_add(cmdline, 0);
2659}
2660
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002661void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002662{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002663 usb_device_add(qdict_get_str(qdict, "devname"), 1);
bellarda594cfb2005-11-06 16:13:29 +00002664}
2665
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002666void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002667{
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002668 usb_device_del(qdict_get_str(qdict, "devname"));
bellarda594cfb2005-11-06 16:13:29 +00002669}
2670
bellardf7cce892004-12-08 22:21:25 +00002671/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002672/* PCMCIA/Cardbus */
2673
2674static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002675 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002676 struct pcmcia_socket_entry_s *next;
2677} *pcmcia_sockets = 0;
2678
Paul Brookbc24a222009-05-10 01:44:56 +01002679void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002680{
2681 struct pcmcia_socket_entry_s *entry;
2682
2683 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2684 entry->socket = socket;
2685 entry->next = pcmcia_sockets;
2686 pcmcia_sockets = entry;
2687}
2688
Paul Brookbc24a222009-05-10 01:44:56 +01002689void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002690{
2691 struct pcmcia_socket_entry_s *entry, **ptr;
2692
2693 ptr = &pcmcia_sockets;
2694 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2695 if (entry->socket == socket) {
2696 *ptr = entry->next;
2697 qemu_free(entry);
2698 }
2699}
2700
aliguori376253e2009-03-05 23:01:23 +00002701void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002702{
2703 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002704
balrog201a51f2007-04-30 00:51:09 +00002705 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002706 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002707
2708 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002709 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2710 iter->socket->attached ? iter->socket->card_string :
2711 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002712}
2713
2714/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002715/* register display */
2716
aliguori7b5d76d2009-03-13 15:02:13 +00002717struct DisplayAllocator default_allocator = {
2718 defaultallocator_create_displaysurface,
2719 defaultallocator_resize_displaysurface,
2720 defaultallocator_free_displaysurface
2721};
2722
aliguori3023f332009-01-16 19:04:14 +00002723void register_displaystate(DisplayState *ds)
2724{
2725 DisplayState **s;
2726 s = &display_state;
2727 while (*s != NULL)
2728 s = &(*s)->next;
2729 ds->next = NULL;
2730 *s = ds;
2731}
2732
2733DisplayState *get_displaystate(void)
2734{
2735 return display_state;
2736}
2737
aliguori7b5d76d2009-03-13 15:02:13 +00002738DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2739{
2740 if(ds->allocator == &default_allocator) ds->allocator = da;
2741 return ds->allocator;
2742}
2743
ths2ff89792007-06-21 23:34:19 +00002744/* dumb display */
2745
aliguori8f391ab2009-01-19 16:34:10 +00002746static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002747{
aliguori8f391ab2009-01-19 16:34:10 +00002748 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002749 ds->allocator = &default_allocator;
2750 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002751 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002752}
2753
2754/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002755/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002756
bellardc4b1fcc2004-03-14 21:44:30 +00002757typedef struct IOHandlerRecord {
2758 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002759 IOCanRWHandler *fd_read_poll;
2760 IOHandler *fd_read;
2761 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002762 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002763 void *opaque;
2764 /* temporary data */
2765 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002766 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002767} IOHandlerRecord;
2768
bellard8a7ddc32004-03-31 19:00:16 +00002769static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002770
bellard7c9d8e02005-11-15 22:16:05 +00002771/* XXX: fd_read_poll should be suppressed, but an API change is
2772 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002773int qemu_set_fd_handler2(int fd,
2774 IOCanRWHandler *fd_read_poll,
2775 IOHandler *fd_read,
2776 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002777 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002778{
bellard8a7ddc32004-03-31 19:00:16 +00002779 IOHandlerRecord **pioh, *ioh;
2780
bellard7c9d8e02005-11-15 22:16:05 +00002781 if (!fd_read && !fd_write) {
2782 pioh = &first_io_handler;
2783 for(;;) {
2784 ioh = *pioh;
2785 if (ioh == NULL)
2786 break;
2787 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002788 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002789 break;
2790 }
2791 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002792 }
bellard7c9d8e02005-11-15 22:16:05 +00002793 } else {
2794 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2795 if (ioh->fd == fd)
2796 goto found;
2797 }
2798 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002799 ioh->next = first_io_handler;
2800 first_io_handler = ioh;
2801 found:
2802 ioh->fd = fd;
2803 ioh->fd_read_poll = fd_read_poll;
2804 ioh->fd_read = fd_read;
2805 ioh->fd_write = fd_write;
2806 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002807 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002808 }
bellard7c9d8e02005-11-15 22:16:05 +00002809 return 0;
2810}
2811
ths5fafdf22007-09-16 21:08:06 +00002812int qemu_set_fd_handler(int fd,
2813 IOHandler *fd_read,
2814 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002815 void *opaque)
2816{
2817 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002818}
2819
aliguori56f3a5d2008-10-31 18:07:17 +00002820#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002821/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002822/* Polling handling */
2823
2824typedef struct PollingEntry {
2825 PollingFunc *func;
2826 void *opaque;
2827 struct PollingEntry *next;
2828} PollingEntry;
2829
2830static PollingEntry *first_polling_entry;
2831
2832int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2833{
2834 PollingEntry **ppe, *pe;
2835 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002836 pe->func = func;
2837 pe->opaque = opaque;
2838 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2839 *ppe = pe;
2840 return 0;
2841}
2842
2843void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2844{
2845 PollingEntry **ppe, *pe;
2846 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2847 pe = *ppe;
2848 if (pe->func == func && pe->opaque == opaque) {
2849 *ppe = pe->next;
2850 qemu_free(pe);
2851 break;
2852 }
2853 }
2854}
2855
bellarda18e5242006-06-25 17:18:27 +00002856/***********************************************************/
2857/* Wait objects support */
2858typedef struct WaitObjects {
2859 int num;
2860 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2861 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2862 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2863} WaitObjects;
2864
2865static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002866
bellarda18e5242006-06-25 17:18:27 +00002867int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2868{
2869 WaitObjects *w = &wait_objects;
2870
2871 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2872 return -1;
2873 w->events[w->num] = handle;
2874 w->func[w->num] = func;
2875 w->opaque[w->num] = opaque;
2876 w->num++;
2877 return 0;
2878}
2879
2880void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2881{
2882 int i, found;
2883 WaitObjects *w = &wait_objects;
2884
2885 found = 0;
2886 for (i = 0; i < w->num; i++) {
2887 if (w->events[i] == handle)
2888 found = 1;
2889 if (found) {
2890 w->events[i] = w->events[i + 1];
2891 w->func[i] = w->func[i + 1];
2892 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002893 }
bellarda18e5242006-06-25 17:18:27 +00002894 }
2895 if (found)
2896 w->num--;
2897}
2898#endif
2899
bellard8a7ddc32004-03-31 19:00:16 +00002900/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002901/* ram save/restore */
2902
Juan Quintela94fb0902009-09-10 03:04:23 +02002903#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
aliguori475e4272008-10-06 20:21:51 +00002904#define RAM_SAVE_FLAG_COMPRESS 0x02
2905#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2906#define RAM_SAVE_FLAG_PAGE 0x08
2907#define RAM_SAVE_FLAG_EOS 0x10
2908
2909static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002910{
aliguori475e4272008-10-06 20:21:51 +00002911 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2912 uint32_t *array = (uint32_t *)page;
2913 int i;
ths3b46e622007-09-17 08:09:54 +00002914
aliguori475e4272008-10-06 20:21:51 +00002915 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2916 if (array[i] != val)
2917 return 0;
bellardc88676f2006-08-06 13:36:11 +00002918 }
aliguori475e4272008-10-06 20:21:51 +00002919
2920 return 1;
bellardc88676f2006-08-06 13:36:11 +00002921}
2922
aliguori475e4272008-10-06 20:21:51 +00002923static int ram_save_block(QEMUFile *f)
2924{
Anthony Liguoric227f092009-10-01 16:12:16 -05002925 static ram_addr_t current_addr = 0;
2926 ram_addr_t saved_addr = current_addr;
2927 ram_addr_t addr = 0;
aliguori475e4272008-10-06 20:21:51 +00002928 int found = 0;
2929
pbrook94a6b542009-04-11 17:15:54 +00002930 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002931 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002932 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002933
2934 cpu_physical_memory_reset_dirty(current_addr,
2935 current_addr + TARGET_PAGE_SIZE,
2936 MIGRATION_DIRTY_FLAG);
2937
pbrook5579c7f2009-04-11 14:47:08 +00002938 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002939
pbrook5579c7f2009-04-11 14:47:08 +00002940 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002941 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002942 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00002943 } else {
2944 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00002945 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002946 }
2947
2948 found = 1;
2949 break;
2950 }
2951 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00002952 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00002953 }
2954
2955 return found;
2956}
2957
Jan Kiszka84307932009-12-02 11:29:38 +01002958static uint64_t bytes_transferred;
aliguori475e4272008-10-06 20:21:51 +00002959
Anthony Liguoric227f092009-10-01 16:12:16 -05002960static ram_addr_t ram_save_remaining(void)
aliguori475e4272008-10-06 20:21:51 +00002961{
Anthony Liguoric227f092009-10-01 16:12:16 -05002962 ram_addr_t addr;
2963 ram_addr_t count = 0;
aliguori475e4272008-10-06 20:21:51 +00002964
pbrook94a6b542009-04-11 17:15:54 +00002965 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002966 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2967 count++;
2968 }
2969
2970 return count;
2971}
2972
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002973uint64_t ram_bytes_remaining(void)
2974{
2975 return ram_save_remaining() * TARGET_PAGE_SIZE;
2976}
2977
2978uint64_t ram_bytes_transferred(void)
2979{
2980 return bytes_transferred;
2981}
2982
2983uint64_t ram_bytes_total(void)
2984{
2985 return last_ram_offset;
2986}
2987
Jan Kiszkaf327aa02009-11-30 18:21:21 +01002988static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
aliguori475e4272008-10-06 20:21:51 +00002989{
Anthony Liguoric227f092009-10-01 16:12:16 -05002990 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002991 uint64_t bytes_transferred_last;
2992 double bwidth = 0;
2993 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00002994
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01002995 if (stage < 0) {
2996 cpu_physical_memory_set_dirty_tracking(0);
2997 return 0;
2998 }
2999
Jan Kiszka9fa06382009-05-22 23:51:45 +02003000 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003001 qemu_file_set_error(f);
3002 return 0;
3003 }
3004
aliguori475e4272008-10-06 20:21:51 +00003005 if (stage == 1) {
Jan Kiszka84307932009-12-02 11:29:38 +01003006 bytes_transferred = 0;
3007
aliguori475e4272008-10-06 20:21:51 +00003008 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003009 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003010 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3011 cpu_physical_memory_set_dirty(addr);
3012 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003013
aliguori475e4272008-10-06 20:21:51 +00003014 /* Enable dirty memory tracking */
3015 cpu_physical_memory_set_dirty_tracking(1);
3016
pbrook94a6b542009-04-11 17:15:54 +00003017 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003018 }
3019
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003020 bytes_transferred_last = bytes_transferred;
3021 bwidth = get_clock();
3022
aliguori475e4272008-10-06 20:21:51 +00003023 while (!qemu_file_rate_limit(f)) {
3024 int ret;
3025
3026 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003027 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003028 if (ret == 0) /* no more blocks */
3029 break;
3030 }
3031
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003032 bwidth = get_clock() - bwidth;
3033 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3034
3035 /* if we haven't transferred anything this round, force expected_time to a
3036 * a very high value, but without crashing */
3037 if (bwidth == 0)
3038 bwidth = 0.000001;
3039
aliguori475e4272008-10-06 20:21:51 +00003040 /* try transferring iterative blocks of memory */
aliguori475e4272008-10-06 20:21:51 +00003041 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003042 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003043 while (ram_save_block(f) != 0) {
3044 bytes_transferred += TARGET_PAGE_SIZE;
3045 }
aliguori8215e912009-04-05 19:30:55 +00003046 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003047 }
3048
3049 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3050
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003051 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3052
3053 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003054}
3055
aliguori475e4272008-10-06 20:21:51 +00003056static int ram_load(QEMUFile *f, void *opaque, int version_id)
3057{
Anthony Liguoric227f092009-10-01 16:12:16 -05003058 ram_addr_t addr;
aliguori475e4272008-10-06 20:21:51 +00003059 int flags;
3060
aliguori475e4272008-10-06 20:21:51 +00003061 if (version_id != 3)
3062 return -EINVAL;
3063
3064 do {
3065 addr = qemu_get_be64(f);
3066
3067 flags = addr & ~TARGET_PAGE_MASK;
3068 addr &= TARGET_PAGE_MASK;
3069
3070 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003071 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003072 return -EINVAL;
3073 }
3074
aliguori475e4272008-10-06 20:21:51 +00003075 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3076 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003077 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3078#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003079 if (ch == 0 &&
3080 (!kvm_enabled() || kvm_has_sync_mmu())) {
3081 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003082 }
Anthony Liguori30868442009-06-17 16:46:12 -05003083#endif
Jan Kiszka9a743e52009-11-30 18:21:21 +01003084 } else if (flags & RAM_SAVE_FLAG_PAGE) {
pbrook5579c7f2009-04-11 14:47:08 +00003085 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
Jan Kiszka9a743e52009-11-30 18:21:21 +01003086 }
3087 if (qemu_file_has_error(f)) {
3088 return -EIO;
3089 }
aliguori475e4272008-10-06 20:21:51 +00003090 } while (!(flags & RAM_SAVE_FLAG_EOS));
3091
bellardc88676f2006-08-06 13:36:11 +00003092 return 0;
3093}
3094
aliguori9e472e12008-10-08 19:50:24 +00003095void qemu_service_io(void)
3096{
aliguorid9f75a42009-04-24 18:03:11 +00003097 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003098}
3099
bellard8a7ddc32004-03-31 19:00:16 +00003100/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003101/* machine registration */
3102
blueswir1bdaf78e2008-10-04 07:24:27 +00003103static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003104QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003105
3106int qemu_register_machine(QEMUMachine *m)
3107{
3108 QEMUMachine **pm;
3109 pm = &first_machine;
3110 while (*pm != NULL)
3111 pm = &(*pm)->next;
3112 m->next = NULL;
3113 *pm = m;
3114 return 0;
3115}
3116
pbrook9596ebb2007-11-18 01:44:38 +00003117static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003118{
3119 QEMUMachine *m;
3120
3121 for(m = first_machine; m != NULL; m = m->next) {
3122 if (!strcmp(m->name, name))
3123 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003124 if (m->alias && !strcmp(m->alias, name))
3125 return m;
bellardcc1daa42005-06-05 14:49:17 +00003126 }
3127 return NULL;
3128}
3129
Anthony Liguori0c257432009-05-21 20:41:01 -05003130static QEMUMachine *find_default_machine(void)
3131{
3132 QEMUMachine *m;
3133
3134 for(m = first_machine; m != NULL; m = m->next) {
3135 if (m->is_default) {
3136 return m;
3137 }
3138 }
3139 return NULL;
3140}
3141
bellardcc1daa42005-06-05 14:49:17 +00003142/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003143/* main execution loop */
3144
pbrook9596ebb2007-11-18 01:44:38 +00003145static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003146{
aliguori7d957bd2009-01-15 22:14:11 +00003147 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003148 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003149 DisplayChangeListener *dcl = ds->listeners;
3150
3151 dpy_refresh(ds);
3152
3153 while (dcl != NULL) {
3154 if (dcl->gui_timer_interval &&
3155 dcl->gui_timer_interval < interval)
3156 interval = dcl->gui_timer_interval;
3157 dcl = dcl->next;
3158 }
3159 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003160}
3161
blueswir19043b622009-01-21 19:28:13 +00003162static void nographic_update(void *opaque)
3163{
3164 uint64_t interval = GUI_REFRESH_INTERVAL;
3165
3166 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3167}
3168
bellard0bd48852005-11-11 00:00:47 +00003169struct vm_change_state_entry {
3170 VMChangeStateHandler *cb;
3171 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003172 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00003173};
3174
Blue Swirl72cf2d42009-09-12 07:36:22 +00003175static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00003176
3177VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3178 void *opaque)
3179{
3180 VMChangeStateEntry *e;
3181
3182 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003183
3184 e->cb = cb;
3185 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003186 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00003187 return e;
3188}
3189
3190void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3191{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003192 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00003193 qemu_free (e);
3194}
3195
aliguori9781e042009-01-22 17:15:29 +00003196static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003197{
3198 VMChangeStateEntry *e;
3199
3200 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003201 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003202 }
3203}
3204
aliguorid6dc3d42009-04-24 18:04:07 +00003205static void resume_all_vcpus(void);
3206static void pause_all_vcpus(void);
3207
bellard8a7ddc32004-03-31 19:00:16 +00003208void vm_start(void)
3209{
3210 if (!vm_running) {
3211 cpu_enable_ticks();
3212 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003213 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003214 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003215 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003216 }
3217}
3218
bellardbb0c6722004-06-20 12:37:32 +00003219/* reset/shutdown handler */
3220
3221typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003222 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003223 QEMUResetHandler *func;
3224 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003225} QEMUResetEntry;
3226
Blue Swirl72cf2d42009-09-12 07:36:22 +00003227static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3228 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003229static int reset_requested;
3230static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003231static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003232static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003233static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003234
aurel32cf7a2fe2008-03-18 06:53:05 +00003235int qemu_shutdown_requested(void)
3236{
3237 int r = shutdown_requested;
3238 shutdown_requested = 0;
3239 return r;
3240}
3241
3242int qemu_reset_requested(void)
3243{
3244 int r = reset_requested;
3245 reset_requested = 0;
3246 return r;
3247}
3248
3249int qemu_powerdown_requested(void)
3250{
3251 int r = powerdown_requested;
3252 powerdown_requested = 0;
3253 return r;
3254}
3255
aliguorie5689022009-04-24 18:03:54 +00003256static int qemu_debug_requested(void)
3257{
3258 int r = debug_requested;
3259 debug_requested = 0;
3260 return r;
3261}
3262
aliguori6e29f5d2009-04-24 18:04:02 +00003263static int qemu_vmstop_requested(void)
3264{
3265 int r = vmstop_requested;
3266 vmstop_requested = 0;
3267 return r;
3268}
3269
3270static void do_vm_stop(int reason)
3271{
3272 if (vm_running) {
3273 cpu_disable_ticks();
3274 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003275 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003276 vm_state_notify(0, reason);
3277 }
3278}
3279
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003280void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003281{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003282 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003283
bellardbb0c6722004-06-20 12:37:32 +00003284 re->func = func;
3285 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003286 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003287}
3288
Jan Kiszkadda9b292009-07-02 00:19:02 +02003289void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003290{
3291 QEMUResetEntry *re;
3292
Blue Swirl72cf2d42009-09-12 07:36:22 +00003293 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003294 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003295 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02003296 qemu_free(re);
3297 return;
3298 }
3299 }
3300}
3301
3302void qemu_system_reset(void)
3303{
3304 QEMUResetEntry *re, *nre;
3305
3306 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00003307 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003308 re->func(re->opaque);
3309 }
3310}
3311
3312void qemu_system_reset_request(void)
3313{
bellardd1beab82006-10-02 19:44:22 +00003314 if (no_reboot) {
3315 shutdown_requested = 1;
3316 } else {
3317 reset_requested = 1;
3318 }
aliguorid9f75a42009-04-24 18:03:11 +00003319 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003320}
3321
3322void qemu_system_shutdown_request(void)
3323{
3324 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003325 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003326}
3327
bellard34751872005-07-02 14:31:34 +00003328void qemu_system_powerdown_request(void)
3329{
3330 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003331 qemu_notify_event();
3332}
3333
aliguorid6dc3d42009-04-24 18:04:07 +00003334#ifdef CONFIG_IOTHREAD
3335static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003336{
aliguorid6dc3d42009-04-24 18:04:07 +00003337 vmstop_requested = reason;
3338 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003339}
aliguorid6dc3d42009-04-24 18:04:07 +00003340#endif
bellardbb0c6722004-06-20 12:37:32 +00003341
aliguori50317c72009-04-24 18:03:29 +00003342#ifndef _WIN32
3343static int io_thread_fd = -1;
3344
3345static void qemu_event_increment(void)
3346{
3347 static const char byte = 0;
3348
3349 if (io_thread_fd == -1)
3350 return;
3351
3352 write(io_thread_fd, &byte, sizeof(byte));
3353}
3354
3355static void qemu_event_read(void *opaque)
3356{
3357 int fd = (unsigned long)opaque;
3358 ssize_t len;
3359
3360 /* Drain the notify pipe */
3361 do {
3362 char buffer[512];
3363 len = read(fd, buffer, sizeof(buffer));
3364 } while ((len == -1 && errno == EINTR) || len > 0);
3365}
3366
3367static int qemu_event_init(void)
3368{
3369 int err;
3370 int fds[2];
3371
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003372 err = qemu_pipe(fds);
aliguori50317c72009-04-24 18:03:29 +00003373 if (err == -1)
3374 return -errno;
3375
3376 err = fcntl_setfl(fds[0], O_NONBLOCK);
3377 if (err < 0)
3378 goto fail;
3379
3380 err = fcntl_setfl(fds[1], O_NONBLOCK);
3381 if (err < 0)
3382 goto fail;
3383
3384 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3385 (void *)(unsigned long)fds[0]);
3386
3387 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003388 return 0;
3389
aliguori50317c72009-04-24 18:03:29 +00003390fail:
3391 close(fds[0]);
3392 close(fds[1]);
3393 return err;
3394}
3395#else
3396HANDLE qemu_event_handle;
3397
3398static void dummy_event_handler(void *opaque)
3399{
3400}
3401
3402static int qemu_event_init(void)
3403{
3404 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3405 if (!qemu_event_handle) {
Blue Swirl20889d42009-09-27 20:03:56 +00003406 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
aliguori50317c72009-04-24 18:03:29 +00003407 return -1;
3408 }
3409 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3410 return 0;
3411}
3412
3413static void qemu_event_increment(void)
3414{
malcde1c90c2009-09-27 14:38:18 +04003415 if (!SetEvent(qemu_event_handle)) {
Blue Swirl20889d42009-09-27 20:03:56 +00003416 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
malcde1c90c2009-09-27 14:38:18 +04003417 GetLastError());
3418 exit (1);
3419 }
aliguori50317c72009-04-24 18:03:29 +00003420}
3421#endif
3422
aliguorid6dc3d42009-04-24 18:04:07 +00003423static int cpu_can_run(CPUState *env)
3424{
3425 if (env->stop)
3426 return 0;
3427 if (env->stopped)
3428 return 0;
3429 return 1;
3430}
3431
3432#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003433static int qemu_init_main_loop(void)
3434{
aliguori50317c72009-04-24 18:03:29 +00003435 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003436}
3437
aliguori0bf46a42009-04-24 18:03:41 +00003438void qemu_init_vcpu(void *_env)
3439{
3440 CPUState *env = _env;
3441
3442 if (kvm_enabled())
3443 kvm_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003444 env->nr_cores = smp_cores;
3445 env->nr_threads = smp_threads;
aliguori0bf46a42009-04-24 18:03:41 +00003446 return;
3447}
3448
aliguori8edac962009-04-24 18:03:45 +00003449int qemu_cpu_self(void *env)
3450{
3451 return 1;
3452}
3453
aliguorid6dc3d42009-04-24 18:04:07 +00003454static void resume_all_vcpus(void)
3455{
3456}
3457
3458static void pause_all_vcpus(void)
3459{
3460}
3461
aliguori8edac962009-04-24 18:03:45 +00003462void qemu_cpu_kick(void *env)
3463{
3464 return;
3465}
3466
aliguorid6dc3d42009-04-24 18:04:07 +00003467void qemu_notify_event(void)
3468{
3469 CPUState *env = cpu_single_env;
3470
3471 if (env) {
3472 cpu_exit(env);
Anthony Liguori4a1418e2009-08-10 17:07:24 -05003473 }
aliguorid6dc3d42009-04-24 18:04:07 +00003474}
3475
Glauber Costad549db52009-10-07 16:38:03 -03003476void qemu_mutex_lock_iothread(void) {}
3477void qemu_mutex_unlock_iothread(void) {}
aliguori48708522009-04-24 18:03:49 +00003478
aliguori6e29f5d2009-04-24 18:04:02 +00003479void vm_stop(int reason)
3480{
3481 do_vm_stop(reason);
3482}
3483
aliguorid6dc3d42009-04-24 18:04:07 +00003484#else /* CONFIG_IOTHREAD */
3485
3486#include "qemu-thread.h"
3487
3488QemuMutex qemu_global_mutex;
3489static QemuMutex qemu_fair_mutex;
3490
3491static QemuThread io_thread;
3492
3493static QemuThread *tcg_cpu_thread;
3494static QemuCond *tcg_halt_cond;
3495
3496static int qemu_system_ready;
3497/* cpu creation */
3498static QemuCond qemu_cpu_cond;
3499/* system init */
3500static QemuCond qemu_system_cond;
3501static QemuCond qemu_pause_cond;
3502
3503static void block_io_signals(void);
3504static void unblock_io_signals(void);
3505static int tcg_has_work(void);
3506
3507static int qemu_init_main_loop(void)
3508{
3509 int ret;
3510
3511 ret = qemu_event_init();
3512 if (ret)
3513 return ret;
3514
3515 qemu_cond_init(&qemu_pause_cond);
3516 qemu_mutex_init(&qemu_fair_mutex);
3517 qemu_mutex_init(&qemu_global_mutex);
3518 qemu_mutex_lock(&qemu_global_mutex);
3519
3520 unblock_io_signals();
3521 qemu_thread_self(&io_thread);
3522
3523 return 0;
3524}
3525
3526static void qemu_wait_io_event(CPUState *env)
3527{
3528 while (!tcg_has_work())
3529 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3530
3531 qemu_mutex_unlock(&qemu_global_mutex);
3532
3533 /*
3534 * Users of qemu_global_mutex can be starved, having no chance
3535 * to acquire it since this path will get to it first.
3536 * So use another lock to provide fairness.
3537 */
3538 qemu_mutex_lock(&qemu_fair_mutex);
3539 qemu_mutex_unlock(&qemu_fair_mutex);
3540
3541 qemu_mutex_lock(&qemu_global_mutex);
3542 if (env->stop) {
3543 env->stop = 0;
3544 env->stopped = 1;
3545 qemu_cond_signal(&qemu_pause_cond);
3546 }
3547}
3548
3549static int qemu_cpu_exec(CPUState *env);
3550
3551static void *kvm_cpu_thread_fn(void *arg)
3552{
3553 CPUState *env = arg;
3554
3555 block_io_signals();
3556 qemu_thread_self(env->thread);
Jean-Christophe DUBOIS321c1cb2009-09-02 23:59:04 +02003557 if (kvm_enabled())
3558 kvm_init_vcpu(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003559
3560 /* signal CPU creation */
3561 qemu_mutex_lock(&qemu_global_mutex);
3562 env->created = 1;
3563 qemu_cond_signal(&qemu_cpu_cond);
3564
3565 /* and wait for machine initialization */
3566 while (!qemu_system_ready)
3567 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3568
3569 while (1) {
3570 if (cpu_can_run(env))
3571 qemu_cpu_exec(env);
Anthony Liguori1c3173b2009-09-10 08:45:43 -05003572 qemu_wait_io_event(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003573 }
3574
3575 return NULL;
3576}
3577
3578static void tcg_cpu_exec(void);
3579
3580static void *tcg_cpu_thread_fn(void *arg)
3581{
3582 CPUState *env = arg;
3583
3584 block_io_signals();
3585 qemu_thread_self(env->thread);
3586
3587 /* signal CPU creation */
3588 qemu_mutex_lock(&qemu_global_mutex);
3589 for (env = first_cpu; env != NULL; env = env->next_cpu)
3590 env->created = 1;
3591 qemu_cond_signal(&qemu_cpu_cond);
3592
3593 /* and wait for machine initialization */
3594 while (!qemu_system_ready)
3595 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3596
3597 while (1) {
3598 tcg_cpu_exec();
3599 qemu_wait_io_event(cur_cpu);
3600 }
3601
3602 return NULL;
3603}
3604
3605void qemu_cpu_kick(void *_env)
3606{
3607 CPUState *env = _env;
3608 qemu_cond_broadcast(env->halt_cond);
3609 if (kvm_enabled())
3610 qemu_thread_signal(env->thread, SIGUSR1);
3611}
3612
Glauber Costae5bc2012009-09-28 15:27:44 -03003613int qemu_cpu_self(void *_env)
aliguorid6dc3d42009-04-24 18:04:07 +00003614{
Glauber Costae5bc2012009-09-28 15:27:44 -03003615 CPUState *env = _env;
3616 QemuThread this;
3617
3618 qemu_thread_self(&this);
3619
3620 return qemu_thread_equal(&this, env->thread);
aliguorid6dc3d42009-04-24 18:04:07 +00003621}
3622
3623static void cpu_signal(int sig)
3624{
3625 if (cpu_single_env)
3626 cpu_exit(cpu_single_env);
3627}
3628
3629static void block_io_signals(void)
3630{
3631 sigset_t set;
3632 struct sigaction sigact;
3633
3634 sigemptyset(&set);
3635 sigaddset(&set, SIGUSR2);
3636 sigaddset(&set, SIGIO);
3637 sigaddset(&set, SIGALRM);
3638 pthread_sigmask(SIG_BLOCK, &set, NULL);
3639
3640 sigemptyset(&set);
3641 sigaddset(&set, SIGUSR1);
3642 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3643
3644 memset(&sigact, 0, sizeof(sigact));
3645 sigact.sa_handler = cpu_signal;
3646 sigaction(SIGUSR1, &sigact, NULL);
3647}
3648
3649static void unblock_io_signals(void)
3650{
3651 sigset_t set;
3652
3653 sigemptyset(&set);
3654 sigaddset(&set, SIGUSR2);
3655 sigaddset(&set, SIGIO);
3656 sigaddset(&set, SIGALRM);
3657 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3658
3659 sigemptyset(&set);
3660 sigaddset(&set, SIGUSR1);
3661 pthread_sigmask(SIG_BLOCK, &set, NULL);
3662}
3663
3664static void qemu_signal_lock(unsigned int msecs)
3665{
3666 qemu_mutex_lock(&qemu_fair_mutex);
3667
3668 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3669 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3670 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3671 break;
3672 }
3673 qemu_mutex_unlock(&qemu_fair_mutex);
3674}
3675
Glauber Costad549db52009-10-07 16:38:03 -03003676void qemu_mutex_lock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003677{
3678 if (kvm_enabled()) {
3679 qemu_mutex_lock(&qemu_fair_mutex);
3680 qemu_mutex_lock(&qemu_global_mutex);
3681 qemu_mutex_unlock(&qemu_fair_mutex);
3682 } else
3683 qemu_signal_lock(100);
3684}
3685
Glauber Costad549db52009-10-07 16:38:03 -03003686void qemu_mutex_unlock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003687{
3688 qemu_mutex_unlock(&qemu_global_mutex);
3689}
3690
3691static int all_vcpus_paused(void)
3692{
3693 CPUState *penv = first_cpu;
3694
3695 while (penv) {
3696 if (!penv->stopped)
3697 return 0;
3698 penv = (CPUState *)penv->next_cpu;
3699 }
3700
3701 return 1;
3702}
3703
3704static void pause_all_vcpus(void)
3705{
3706 CPUState *penv = first_cpu;
3707
3708 while (penv) {
3709 penv->stop = 1;
3710 qemu_thread_signal(penv->thread, SIGUSR1);
3711 qemu_cpu_kick(penv);
3712 penv = (CPUState *)penv->next_cpu;
3713 }
3714
3715 while (!all_vcpus_paused()) {
3716 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3717 penv = first_cpu;
3718 while (penv) {
3719 qemu_thread_signal(penv->thread, SIGUSR1);
3720 penv = (CPUState *)penv->next_cpu;
3721 }
3722 }
3723}
3724
3725static void resume_all_vcpus(void)
3726{
3727 CPUState *penv = first_cpu;
3728
3729 while (penv) {
3730 penv->stop = 0;
3731 penv->stopped = 0;
3732 qemu_thread_signal(penv->thread, SIGUSR1);
3733 qemu_cpu_kick(penv);
3734 penv = (CPUState *)penv->next_cpu;
3735 }
3736}
3737
3738static void tcg_init_vcpu(void *_env)
3739{
3740 CPUState *env = _env;
3741 /* share a single thread for all cpus with TCG */
3742 if (!tcg_cpu_thread) {
3743 env->thread = qemu_mallocz(sizeof(QemuThread));
3744 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3745 qemu_cond_init(env->halt_cond);
3746 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3747 while (env->created == 0)
3748 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3749 tcg_cpu_thread = env->thread;
3750 tcg_halt_cond = env->halt_cond;
3751 } else {
3752 env->thread = tcg_cpu_thread;
3753 env->halt_cond = tcg_halt_cond;
3754 }
3755}
3756
3757static void kvm_start_vcpu(CPUState *env)
3758{
aliguorid6dc3d42009-04-24 18:04:07 +00003759 env->thread = qemu_mallocz(sizeof(QemuThread));
3760 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3761 qemu_cond_init(env->halt_cond);
3762 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3763 while (env->created == 0)
3764 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3765}
3766
3767void qemu_init_vcpu(void *_env)
3768{
3769 CPUState *env = _env;
3770
3771 if (kvm_enabled())
3772 kvm_start_vcpu(env);
3773 else
3774 tcg_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003775 env->nr_cores = smp_cores;
3776 env->nr_threads = smp_threads;
aliguorid6dc3d42009-04-24 18:04:07 +00003777}
3778
3779void qemu_notify_event(void)
3780{
3781 qemu_event_increment();
3782}
3783
3784void vm_stop(int reason)
3785{
3786 QemuThread me;
3787 qemu_thread_self(&me);
3788
3789 if (!qemu_thread_equal(&me, &io_thread)) {
3790 qemu_system_vmstop_request(reason);
3791 /*
3792 * FIXME: should not return to device code in case
3793 * vm_stop() has been requested.
3794 */
3795 if (cpu_single_env) {
3796 cpu_exit(cpu_single_env);
3797 cpu_single_env->stop = 1;
3798 }
3799 return;
3800 }
3801 do_vm_stop(reason);
3802}
3803
3804#endif
3805
3806
ths877cf882007-04-18 18:11:47 +00003807#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003808static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003809{
3810 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003811 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003812
bellardf3311102006-04-12 20:21:17 +00003813
3814 /* XXX: need to suppress polling by better using win32 events */
3815 ret = 0;
3816 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3817 ret |= pe->func(pe->opaque);
3818 }
thse6b1e552007-04-18 17:56:02 +00003819 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003820 int err;
3821 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003822
aliguori56f3a5d2008-10-31 18:07:17 +00003823 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003824 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3825 if (w->func[ret - WAIT_OBJECT_0])
3826 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003827
ths5fafdf22007-09-16 21:08:06 +00003828 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003829 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003830
thse6b1e552007-04-18 17:56:02 +00003831 /* Check if event is signaled */
3832 ret2 = WaitForSingleObject(w->events[i], 0);
3833 if(ret2 == WAIT_OBJECT_0) {
3834 if (w->func[i])
3835 w->func[i](w->opaque[i]);
3836 } else if (ret2 == WAIT_TIMEOUT) {
3837 } else {
3838 err = GetLastError();
3839 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003840 }
3841 }
bellarda18e5242006-06-25 17:18:27 +00003842 } else if (ret == WAIT_TIMEOUT) {
3843 } else {
3844 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003845 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003846 }
bellardf3311102006-04-12 20:21:17 +00003847 }
aliguori56f3a5d2008-10-31 18:07:17 +00003848
3849 *timeout = 0;
3850}
3851#else
blueswir169d64512008-12-07 19:30:18 +00003852static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003853{
3854}
bellardfd1dff42006-02-01 21:29:26 +00003855#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003856
3857void main_loop_wait(int timeout)
3858{
3859 IOHandlerRecord *ioh;
3860 fd_set rfds, wfds, xfds;
3861 int ret, nfds;
3862 struct timeval tv;
3863
3864 qemu_bh_update_timeout(&timeout);
3865
3866 host_main_loop_wait(&timeout);
3867
bellardfd1dff42006-02-01 21:29:26 +00003868 /* poll any events */
3869 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003870 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003871 FD_ZERO(&rfds);
3872 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003873 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003874 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003875 if (ioh->deleted)
3876 continue;
bellardfd1dff42006-02-01 21:29:26 +00003877 if (ioh->fd_read &&
3878 (!ioh->fd_read_poll ||
3879 ioh->fd_read_poll(ioh->opaque) != 0)) {
3880 FD_SET(ioh->fd, &rfds);
3881 if (ioh->fd > nfds)
3882 nfds = ioh->fd;
3883 }
3884 if (ioh->fd_write) {
3885 FD_SET(ioh->fd, &wfds);
3886 if (ioh->fd > nfds)
3887 nfds = ioh->fd;
3888 }
3889 }
ths3b46e622007-09-17 08:09:54 +00003890
aliguori56f3a5d2008-10-31 18:07:17 +00003891 tv.tv_sec = timeout / 1000;
3892 tv.tv_usec = (timeout % 1000) * 1000;
3893
Jan Kiszkad918f232009-06-24 14:42:30 +02003894 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3895
aliguori48708522009-04-24 18:03:49 +00003896 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00003897 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00003898 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00003899 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003900 IOHandlerRecord **pioh;
3901
3902 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003903 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003904 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003905 }
ths6ab43fd2007-08-25 01:34:19 +00003906 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003907 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003908 }
3909 }
thscafffd42007-02-28 21:59:44 +00003910
3911 /* remove deleted IO handlers */
3912 pioh = &first_io_handler;
3913 while (*pioh) {
3914 ioh = *pioh;
3915 if (ioh->deleted) {
3916 *pioh = ioh->next;
3917 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003918 } else
thscafffd42007-02-28 21:59:44 +00003919 pioh = &ioh->next;
3920 }
bellardfd1dff42006-02-01 21:29:26 +00003921 }
Jan Kiszkad918f232009-06-24 14:42:30 +02003922
3923 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00003924
aliguori50317c72009-04-24 18:03:29 +00003925 /* rearm timer, if not periodic */
3926 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
3927 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
3928 qemu_rearm_alarm_timer(alarm_timer);
3929 }
3930
aliguori357c6922008-11-25 17:26:09 +00003931 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00003932 if (vm_running) {
3933 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003934 qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL],
3935 qemu_get_clock(vm_clock));
aliguorid6dc3d42009-04-24 18:04:07 +00003936 }
aliguori357c6922008-11-25 17:26:09 +00003937
3938 /* real time timers */
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003939 qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME],
aliguori357c6922008-11-25 17:26:09 +00003940 qemu_get_clock(rt_clock));
3941
Jan Kiszka21d5d122009-09-15 13:36:04 +02003942 qemu_run_timers(&active_timers[QEMU_CLOCK_HOST],
3943 qemu_get_clock(host_clock));
3944
pbrook423f0742007-05-23 00:06:54 +00003945 /* Check bottom-halves last in case any of the earlier events triggered
3946 them. */
3947 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003948
bellard5905b2e2004-08-01 21:53:26 +00003949}
3950
aliguori43b96852009-04-24 18:03:33 +00003951static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00003952{
aliguori43b96852009-04-24 18:03:33 +00003953 int ret;
bellard89bfc102006-02-08 22:46:31 +00003954#ifdef CONFIG_PROFILER
3955 int64_t ti;
3956#endif
aliguori43b96852009-04-24 18:03:33 +00003957
3958#ifdef CONFIG_PROFILER
3959 ti = profile_getclock();
3960#endif
3961 if (use_icount) {
3962 int64_t count;
3963 int decr;
3964 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3965 env->icount_decr.u16.low = 0;
3966 env->icount_extra = 0;
3967 count = qemu_next_deadline();
3968 count = (count + (1 << icount_time_shift) - 1)
3969 >> icount_time_shift;
3970 qemu_icount += count;
3971 decr = (count > 0xffff) ? 0xffff : count;
3972 count -= decr;
3973 env->icount_decr.u16.low = decr;
3974 env->icount_extra = count;
3975 }
3976 ret = cpu_exec(env);
3977#ifdef CONFIG_PROFILER
3978 qemu_time += profile_getclock() - ti;
3979#endif
3980 if (use_icount) {
3981 /* Fold pending instructions back into the
3982 instruction counter, and clear the interrupt flag. */
3983 qemu_icount -= (env->icount_decr.u16.low
3984 + env->icount_extra);
3985 env->icount_decr.u32 = 0;
3986 env->icount_extra = 0;
3987 }
3988 return ret;
3989}
3990
aliguorie6e35b12009-04-24 18:03:57 +00003991static void tcg_cpu_exec(void)
3992{
aliguorid6dc3d42009-04-24 18:04:07 +00003993 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00003994
3995 if (next_cpu == NULL)
3996 next_cpu = first_cpu;
3997 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3998 CPUState *env = cur_cpu = next_cpu;
3999
4000 if (!vm_running)
4001 break;
4002 if (timer_alarm_pending) {
4003 timer_alarm_pending = 0;
4004 break;
4005 }
aliguorid6dc3d42009-04-24 18:04:07 +00004006 if (cpu_can_run(env))
4007 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004008 if (ret == EXCP_DEBUG) {
4009 gdb_set_stop_cpu(env);
4010 debug_requested = 1;
4011 break;
4012 }
4013 }
4014}
4015
aliguori43b96852009-04-24 18:03:33 +00004016static int cpu_has_work(CPUState *env)
4017{
aliguorid6dc3d42009-04-24 18:04:07 +00004018 if (env->stop)
4019 return 1;
4020 if (env->stopped)
4021 return 0;
aliguori43b96852009-04-24 18:03:33 +00004022 if (!env->halted)
4023 return 1;
4024 if (qemu_cpu_has_work(env))
4025 return 1;
4026 return 0;
4027}
4028
4029static int tcg_has_work(void)
4030{
bellard6a00d602005-11-21 23:25:50 +00004031 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004032
aliguori43b96852009-04-24 18:03:33 +00004033 for (env = first_cpu; env != NULL; env = env->next_cpu)
4034 if (cpu_has_work(env))
4035 return 1;
4036 return 0;
4037}
bellard15a76442005-11-23 21:01:03 +00004038
aliguori43b96852009-04-24 18:03:33 +00004039static int qemu_calculate_timeout(void)
4040{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004041#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004042 int timeout;
bellard15a76442005-11-23 21:01:03 +00004043
aliguori43b96852009-04-24 18:03:33 +00004044 if (!vm_running)
4045 timeout = 5000;
4046 else if (tcg_has_work())
4047 timeout = 0;
4048 else if (!use_icount)
4049 timeout = 5000;
4050 else {
4051 /* XXX: use timeout computed from timers */
4052 int64_t add;
4053 int64_t delta;
4054 /* Advance virtual time to the next event. */
4055 if (use_icount == 1) {
4056 /* When not using an adaptive execution frequency
4057 we tend to get badly out of sync with real time,
4058 so just delay for a reasonable amount of time. */
4059 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004060 } else {
aliguori43b96852009-04-24 18:03:33 +00004061 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004062 }
aliguori43b96852009-04-24 18:03:33 +00004063 if (delta > 0) {
4064 /* If virtual time is ahead of real time then just
4065 wait for IO. */
4066 timeout = (delta / 1000000) + 1;
4067 } else {
4068 /* Wait for either IO to occur or the next
4069 timer event. */
4070 add = qemu_next_deadline();
4071 /* We advance the timer before checking for IO.
4072 Limit the amount we advance so that early IO
4073 activity won't get the guest too far ahead. */
4074 if (add > 10000000)
4075 add = 10000000;
4076 delta += add;
4077 add = (add + (1 << icount_time_shift) - 1)
4078 >> icount_time_shift;
4079 qemu_icount += add;
4080 timeout = delta / 1000000;
4081 if (timeout < 0)
4082 timeout = 0;
4083 }
bellardb4608c02003-06-27 17:34:32 +00004084 }
aliguori43b96852009-04-24 18:03:33 +00004085
4086 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004087#else /* CONFIG_IOTHREAD */
4088 return 1000;
4089#endif
aliguori43b96852009-04-24 18:03:33 +00004090}
4091
4092static int vm_can_run(void)
4093{
4094 if (powerdown_requested)
4095 return 0;
4096 if (reset_requested)
4097 return 0;
4098 if (shutdown_requested)
4099 return 0;
aliguorie5689022009-04-24 18:03:54 +00004100 if (debug_requested)
4101 return 0;
aliguori43b96852009-04-24 18:03:33 +00004102 return 1;
4103}
4104
Blue Swirld9c32312009-08-09 08:42:19 +00004105qemu_irq qemu_system_powerdown;
4106
aliguori43b96852009-04-24 18:03:33 +00004107static void main_loop(void)
4108{
aliguori6e29f5d2009-04-24 18:04:02 +00004109 int r;
aliguori43b96852009-04-24 18:03:33 +00004110
aliguorid6dc3d42009-04-24 18:04:07 +00004111#ifdef CONFIG_IOTHREAD
4112 qemu_system_ready = 1;
4113 qemu_cond_broadcast(&qemu_system_cond);
4114#endif
4115
aliguori6e29f5d2009-04-24 18:04:02 +00004116 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004117 do {
4118#ifdef CONFIG_PROFILER
4119 int64_t ti;
4120#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004121#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004122 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004123#endif
aliguori43b96852009-04-24 18:03:33 +00004124#ifdef CONFIG_PROFILER
4125 ti = profile_getclock();
4126#endif
4127 main_loop_wait(qemu_calculate_timeout());
4128#ifdef CONFIG_PROFILER
4129 dev_time += profile_getclock() - ti;
4130#endif
aliguorie5689022009-04-24 18:03:54 +00004131 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004132
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004133 if (qemu_debug_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004134 monitor_protocol_event(QEVENT_DEBUG, NULL);
aliguori43b96852009-04-24 18:03:33 +00004135 vm_stop(EXCP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004136 }
aliguori43b96852009-04-24 18:03:33 +00004137 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004138 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00004139 if (no_shutdown) {
4140 vm_stop(0);
4141 no_shutdown = 0;
4142 } else
4143 break;
4144 }
aliguorid6dc3d42009-04-24 18:04:07 +00004145 if (qemu_reset_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004146 monitor_protocol_event(QEVENT_RESET, NULL);
aliguorid6dc3d42009-04-24 18:04:07 +00004147 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004148 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004149 resume_all_vcpus();
4150 }
Blue Swirld9c32312009-08-09 08:42:19 +00004151 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004152 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00004153 qemu_irq_raise(qemu_system_powerdown);
4154 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004155 if ((r = qemu_vmstop_requested())) {
Blue Swirl242cd002009-12-04 18:05:45 +00004156 monitor_protocol_event(QEVENT_STOP, NULL);
aliguori6e29f5d2009-04-24 18:04:02 +00004157 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004158 }
aliguori43b96852009-04-24 18:03:33 +00004159 }
aliguorid6dc3d42009-04-24 18:04:07 +00004160 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004161}
4162
pbrook9bd7e6d2009-04-07 22:58:45 +00004163static void version(void)
4164{
pbrook4a19f1e2009-04-07 23:17:49 +00004165 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004166}
4167
ths15f82202007-06-29 23:26:08 +00004168static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004169{
pbrook9bd7e6d2009-04-07 22:58:45 +00004170 version();
4171 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004172 "\n"
bellarda20dd502003-09-30 21:07:02 +00004173 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004174 "\n"
blueswir15824d652009-03-28 06:44:27 +00004175#define DEF(option, opt_arg, opt_enum, opt_help) \
4176 opt_help
4177#define DEFHEADING(text) stringify(text) "\n"
4178#include "qemu-options.h"
4179#undef DEF
4180#undef DEFHEADING
4181#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004182 "\n"
bellard82c643f2004-07-14 17:28:13 +00004183 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004184 "ctrl-alt-f toggle full screen\n"
4185 "ctrl-alt-n switch to virtual console 'n'\n"
4186 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004187 "\n"
4188 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4189 ,
bellard0db63472003-10-27 21:37:46 +00004190 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004191 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004192#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004193 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004194 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004195#endif
bellard6e44ba72004-01-18 21:56:49 +00004196 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004197 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004198 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004199}
4200
bellardcd6f1162004-05-13 22:02:20 +00004201#define HAS_ARG 0x0001
4202
4203enum {
blueswir15824d652009-03-28 06:44:27 +00004204#define DEF(option, opt_arg, opt_enum, opt_help) \
4205 opt_enum,
4206#define DEFHEADING(text)
4207#include "qemu-options.h"
4208#undef DEF
4209#undef DEFHEADING
4210#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004211};
4212
4213typedef struct QEMUOption {
4214 const char *name;
4215 int flags;
4216 int index;
4217} QEMUOption;
4218
blueswir1dbed7e42008-10-01 19:38:09 +00004219static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004220 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004221#define DEF(option, opt_arg, opt_enum, opt_help) \
4222 { option, opt_arg, opt_enum },
4223#define DEFHEADING(text)
4224#include "qemu-options.h"
4225#undef DEF
4226#undef DEFHEADING
4227#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004228 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004229};
4230
bellard1d14ffa2005-10-30 18:58:22 +00004231#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004232struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004233#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004234#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004235 {
4236 "pcspk",
4237 "PC speaker",
4238 0,
4239 1,
4240 { .init_isa = pcspk_audio_init }
4241 },
4242#endif
malc4c9b53e2009-01-09 10:46:34 +00004243
4244#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004245 {
4246 "sb16",
4247 "Creative Sound Blaster 16",
4248 0,
4249 1,
4250 { .init_isa = SB16_init }
4251 },
malc4c9b53e2009-01-09 10:46:34 +00004252#endif
bellard6a36d842005-12-18 20:34:32 +00004253
malccc53d262008-06-13 10:48:22 +00004254#ifdef CONFIG_CS4231A
4255 {
4256 "cs4231a",
4257 "CS4231A",
4258 0,
4259 1,
4260 { .init_isa = cs4231a_init }
4261 },
4262#endif
4263
bellard6a36d842005-12-18 20:34:32 +00004264#ifdef CONFIG_ADLIB
4265 {
4266 "adlib",
4267#ifdef HAS_YMF262
4268 "Yamaha YMF262 (OPL3)",
4269#else
4270 "Yamaha YM3812 (OPL2)",
4271#endif
4272 0,
4273 1,
4274 { .init_isa = Adlib_init }
4275 },
4276#endif
4277
4278#ifdef CONFIG_GUS
4279 {
4280 "gus",
4281 "Gravis Ultrasound GF1",
4282 0,
4283 1,
4284 { .init_isa = GUS_init }
4285 },
4286#endif
4287
malc4c9b53e2009-01-09 10:46:34 +00004288#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004289 {
4290 "ac97",
4291 "Intel 82801AA AC97 Audio",
4292 0,
4293 0,
4294 { .init_pci = ac97_init }
4295 },
malc4c9b53e2009-01-09 10:46:34 +00004296#endif
balroge5c9a132008-01-14 04:27:55 +00004297
malc4c9b53e2009-01-09 10:46:34 +00004298#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004299 {
4300 "es1370",
4301 "ENSONIQ AudioPCI ES1370",
4302 0,
4303 0,
4304 { .init_pci = es1370_init }
4305 },
balrogb00052e2007-04-30 02:22:06 +00004306#endif
bellard6a36d842005-12-18 20:34:32 +00004307
malc4c9b53e2009-01-09 10:46:34 +00004308#endif /* HAS_AUDIO_CHOICE */
4309
bellard6a36d842005-12-18 20:34:32 +00004310 { NULL, NULL, 0, 0, { NULL } }
4311};
4312
bellard1d14ffa2005-10-30 18:58:22 +00004313static void select_soundhw (const char *optarg)
4314{
bellard6a36d842005-12-18 20:34:32 +00004315 struct soundhw *c;
4316
bellard1d14ffa2005-10-30 18:58:22 +00004317 if (*optarg == '?') {
4318 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004319
bellard1d14ffa2005-10-30 18:58:22 +00004320 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004321 for (c = soundhw; c->name; ++c) {
4322 printf ("%-11s %s\n", c->name, c->descr);
4323 }
4324 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004325 exit (*optarg != '?');
4326 }
4327 else {
bellard6a36d842005-12-18 20:34:32 +00004328 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004329 const char *p;
4330 char *e;
4331 int bad_card = 0;
4332
bellard6a36d842005-12-18 20:34:32 +00004333 if (!strcmp (optarg, "all")) {
4334 for (c = soundhw; c->name; ++c) {
4335 c->enabled = 1;
4336 }
4337 return;
4338 }
bellard1d14ffa2005-10-30 18:58:22 +00004339
bellard6a36d842005-12-18 20:34:32 +00004340 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004341 while (*p) {
4342 e = strchr (p, ',');
4343 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004344
4345 for (c = soundhw; c->name; ++c) {
malcb3d6fb42009-09-06 06:49:03 +04004346 if (!strncmp (c->name, p, l) && !c->name[l]) {
bellard6a36d842005-12-18 20:34:32 +00004347 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004348 break;
4349 }
4350 }
bellard6a36d842005-12-18 20:34:32 +00004351
4352 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004353 if (l > 80) {
4354 fprintf (stderr,
4355 "Unknown sound card name (too big to show)\n");
4356 }
4357 else {
4358 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4359 (int) l, p);
4360 }
4361 bad_card = 1;
4362 }
4363 p += l + (e != NULL);
4364 }
4365
4366 if (bad_card)
4367 goto show_valid_cards;
4368 }
4369}
4370#endif
4371
malc3893c122008-09-28 00:42:05 +00004372static void select_vgahw (const char *p)
4373{
4374 const char *opts;
4375
Zachary Amsden86176752009-07-30 00:15:02 -10004376 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00004377 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004378 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00004379 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004380 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00004381 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004382 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00004383 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004384 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00004385 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004386 invalid_vga:
4387 fprintf(stderr, "Unknown vga type: %s\n", p);
4388 exit(1);
4389 }
malccb5a7aa2008-09-28 00:42:12 +00004390 while (*opts) {
4391 const char *nextopt;
4392
4393 if (strstart(opts, ",retrace=", &nextopt)) {
4394 opts = nextopt;
4395 if (strstart(opts, "dumb", &nextopt))
4396 vga_retrace_method = VGA_RETRACE_DUMB;
4397 else if (strstart(opts, "precise", &nextopt))
4398 vga_retrace_method = VGA_RETRACE_PRECISE;
4399 else goto invalid_vga;
4400 } else goto invalid_vga;
4401 opts = nextopt;
4402 }
malc3893c122008-09-28 00:42:05 +00004403}
4404
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004405#ifdef TARGET_I386
4406static int balloon_parse(const char *arg)
4407{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004408 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004409
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004410 if (strcmp(arg, "none") == 0) {
4411 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004412 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004413
4414 if (!strncmp(arg, "virtio", 6)) {
4415 if (arg[6] == ',') {
4416 /* have params -> parse them */
4417 opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL);
4418 if (!opts)
4419 return -1;
4420 } else {
4421 /* create empty opts */
4422 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4423 }
4424 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
4425 return 0;
4426 }
4427
4428 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004429}
4430#endif
4431
bellard3587d7e2006-06-26 20:03:44 +00004432#ifdef _WIN32
4433static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4434{
4435 exit(STATUS_CONTROL_C_EXIT);
4436 return TRUE;
4437}
4438#endif
4439
aliguoric4be29f2009-04-17 18:58:14 +00004440int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004441{
4442 int ret;
4443
4444 if(strlen(str) != 36)
4445 return -1;
4446
4447 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4448 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4449 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4450
4451 if(ret != 16)
4452 return -1;
4453
aliguorib6f6e3d2009-04-17 18:59:56 +00004454#ifdef TARGET_I386
4455 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4456#endif
4457
blueswir18fcb1b92008-09-18 18:29:08 +00004458 return 0;
4459}
4460
aliguori5b08fc12008-08-21 20:08:03 +00004461#ifndef _WIN32
4462
4463static void termsig_handler(int signal)
4464{
4465 qemu_system_shutdown_request();
4466}
4467
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004468static void sigchld_handler(int signal)
4469{
4470 waitpid(-1, NULL, WNOHANG);
4471}
4472
4473static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004474{
4475 struct sigaction act;
4476
4477 memset(&act, 0, sizeof(act));
4478 act.sa_handler = termsig_handler;
4479 sigaction(SIGINT, &act, NULL);
4480 sigaction(SIGHUP, &act, NULL);
4481 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004482
4483 act.sa_handler = sigchld_handler;
4484 act.sa_flags = SA_NOCLDSTOP;
4485 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004486}
4487
4488#endif
4489
Paul Brook5cea8592009-05-30 00:52:44 +01004490#ifdef _WIN32
4491/* Look for support files in the same directory as the executable. */
4492static char *find_datadir(const char *argv0)
4493{
4494 char *p;
4495 char buf[MAX_PATH];
4496 DWORD len;
4497
4498 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4499 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004500 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004501 }
4502
4503 buf[len] = 0;
4504 p = buf + len - 1;
4505 while (p != buf && *p != '\\')
4506 p--;
4507 *p = 0;
4508 if (access(buf, R_OK) == 0) {
4509 return qemu_strdup(buf);
4510 }
4511 return NULL;
4512}
4513#else /* !_WIN32 */
4514
4515/* Find a likely location for support files using the location of the binary.
4516 For installed binaries this will be "$bindir/../share/qemu". When
4517 running from the build tree this will be "$bindir/../pc-bios". */
4518#define SHARE_SUFFIX "/share/qemu"
4519#define BUILD_SUFFIX "/pc-bios"
4520static char *find_datadir(const char *argv0)
4521{
4522 char *dir;
4523 char *p = NULL;
4524 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01004525 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00004526 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004527
4528#if defined(__linux__)
4529 {
4530 int len;
4531 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4532 if (len > 0) {
4533 buf[len] = 0;
4534 p = buf;
4535 }
4536 }
4537#elif defined(__FreeBSD__)
4538 {
4539 int len;
4540 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4541 if (len > 0) {
4542 buf[len] = 0;
4543 p = buf;
4544 }
4545 }
4546#endif
4547 /* If we don't have any way of figuring out the actual executable
4548 location then try argv[0]. */
4549 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004550 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01004551 if (!p) {
4552 return NULL;
4553 }
4554 }
4555 dir = dirname(p);
4556 dir = dirname(dir);
4557
Blue Swirl3a417592009-06-09 19:12:21 +00004558 max_len = strlen(dir) +
4559 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4560 res = qemu_mallocz(max_len);
4561 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004562 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004563 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004564 if (access(res, R_OK)) {
4565 qemu_free(res);
4566 res = NULL;
4567 }
4568 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004569
Paul Brook5cea8592009-05-30 00:52:44 +01004570 return res;
4571}
4572#undef SHARE_SUFFIX
4573#undef BUILD_SUFFIX
4574#endif
4575
4576char *qemu_find_file(int type, const char *name)
4577{
4578 int len;
4579 const char *subdir;
4580 char *buf;
4581
4582 /* If name contains path separators then try it as a straight path. */
4583 if ((strchr(name, '/') || strchr(name, '\\'))
4584 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02004585 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01004586 }
4587 switch (type) {
4588 case QEMU_FILE_TYPE_BIOS:
4589 subdir = "";
4590 break;
4591 case QEMU_FILE_TYPE_KEYMAP:
4592 subdir = "keymaps/";
4593 break;
4594 default:
4595 abort();
4596 }
4597 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4598 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004599 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004600 if (access(buf, R_OK)) {
4601 qemu_free(buf);
4602 return NULL;
4603 }
4604 return buf;
4605}
4606
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004607static int device_init_func(QemuOpts *opts, void *opaque)
4608{
4609 DeviceState *dev;
4610
4611 dev = qdev_device_add(opts);
4612 if (!dev)
4613 return -1;
4614 return 0;
4615}
4616
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004617static int chardev_init_func(QemuOpts *opts, void *opaque)
4618{
4619 CharDriverState *chr;
4620
4621 chr = qemu_chr_open_opts(opts, NULL);
4622 if (!chr)
4623 return -1;
4624 return 0;
4625}
4626
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004627struct device_config {
4628 enum {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004629 DEV_USB, /* -usbdevice */
4630 DEV_BT, /* -bt */
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004631 DEV_SERIAL, /* -serial */
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004632 DEV_PARALLEL, /* -parallel */
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004633 DEV_MONITOR, /* -monitor */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004634 } type;
4635 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004636 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004637};
Blue Swirl72cf2d42009-09-12 07:36:22 +00004638QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004639
4640static void add_device_config(int type, const char *cmdline)
4641{
4642 struct device_config *conf;
4643
4644 conf = qemu_mallocz(sizeof(*conf));
4645 conf->type = type;
4646 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004647 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004648}
4649
4650static int foreach_device_config(int type, int (*func)(const char *cmdline))
4651{
4652 struct device_config *conf;
4653 int rc;
4654
Blue Swirl72cf2d42009-09-12 07:36:22 +00004655 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004656 if (conf->type != type)
4657 continue;
4658 rc = func(conf->cmdline);
4659 if (0 != rc)
4660 return rc;
4661 }
4662 return 0;
4663}
4664
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004665static void serial_monitor_mux(void)
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004666{
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004667 struct device_config *mon0, *serial;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004668 const char *devname;
4669
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004670 QTAILQ_FOREACH(mon0, &device_configs, next) {
4671 if (mon0->type != DEV_MONITOR)
4672 continue;
4673 if (strcmp(mon0->cmdline,"stdio") != 0)
4674 return;
4675 break;
4676 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004677 QTAILQ_FOREACH(serial, &device_configs, next) {
4678 if (serial->type != DEV_SERIAL)
4679 continue;
4680 devname = serial->cmdline;
4681 if (devname && !strcmp(devname,"mon:stdio")) {
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004682 QTAILQ_REMOVE(&device_configs, mon0, next);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004683 break;
4684 } else if (devname && !strcmp(devname,"stdio")) {
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004685 QTAILQ_REMOVE(&device_configs, mon0, next);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004686 serial->cmdline = "mon:stdio";
4687 break;
4688 }
4689 }
4690}
4691
4692static int serial_parse(const char *devname)
4693{
4694 static int index = 0;
4695 char label[32];
4696
4697 if (strcmp(devname, "none") == 0)
4698 return 0;
4699 if (index == MAX_SERIAL_PORTS) {
4700 fprintf(stderr, "qemu: too many serial ports\n");
4701 exit(1);
4702 }
4703 snprintf(label, sizeof(label), "serial%d", index);
4704 serial_hds[index] = qemu_chr_open(label, devname, NULL);
4705 if (!serial_hds[index]) {
4706 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
4707 devname, strerror(errno));
4708 return -1;
4709 }
4710 index++;
4711 return 0;
4712}
4713
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004714static int parallel_parse(const char *devname)
4715{
4716 static int index = 0;
4717 char label[32];
4718
4719 if (strcmp(devname, "none") == 0)
4720 return 0;
4721 if (index == MAX_PARALLEL_PORTS) {
4722 fprintf(stderr, "qemu: too many parallel ports\n");
4723 exit(1);
4724 }
4725 snprintf(label, sizeof(label), "parallel%d", index);
4726 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
4727 if (!parallel_hds[index]) {
4728 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
4729 devname, strerror(errno));
4730 return -1;
4731 }
4732 index++;
4733 return 0;
4734}
4735
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01004736static int monitor_parse(const char *devname)
4737{
4738 static int index = 0;
4739 char label[32];
4740
4741 if (strcmp(devname, "none") == 0)
4742 return 0;
4743 if (index == MAX_MONITOR_DEVICES) {
4744 fprintf(stderr, "qemu: too many monitor devices\n");
4745 exit(1);
4746 }
4747 if (index == 0) {
4748 snprintf(label, sizeof(label), "monitor");
4749 } else {
4750 snprintf(label, sizeof(label), "monitor%d", index);
4751 }
4752 monitor_hds[index] = qemu_chr_open(label, devname, NULL);
4753 if (!monitor_hds[index]) {
4754 fprintf(stderr, "qemu: could not open monitor device '%s'\n",
4755 devname);
4756 return -1;
4757 }
4758 index++;
4759 return 0;
4760}
4761
malc902b3d52008-12-10 19:18:40 +00004762int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004763{
aliguori59030a82009-04-05 18:43:41 +00004764 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004765 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004766 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004767 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004768 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004769 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05004770 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004771 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004772 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004773 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004774 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00004775 int optind;
4776 const char *r, *optarg;
aliguori9ede2fd2009-01-15 20:05:25 +00004777 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4778 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004779 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004780 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004781 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004782#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004783 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004784#endif
bellard26a5f132008-05-28 12:30:31 +00004785 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004786 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004787 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004788#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004789 int fd = 0;
4790 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004791 const char *chroot_dir = NULL;
4792 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004793#endif
aliguori268a3622009-04-21 22:30:27 +00004794 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004795 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004796
Jan Kiszka68752042009-09-15 13:36:04 +02004797 init_clocks();
4798
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004799 qemu_errors_to_file(stderr);
malc902b3d52008-12-10 19:18:40 +00004800 qemu_cache_utils_init(envp);
4801
Blue Swirl72cf2d42009-09-12 07:36:22 +00004802 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004803#ifndef _WIN32
4804 {
4805 struct sigaction act;
4806 sigfillset(&act.sa_mask);
4807 act.sa_flags = 0;
4808 act.sa_handler = SIG_IGN;
4809 sigaction(SIGPIPE, &act, NULL);
4810 }
bellard3587d7e2006-06-26 20:03:44 +00004811#else
4812 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004813 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4814 QEMU to run on a single CPU */
4815 {
4816 HANDLE h;
4817 DWORD mask, smask;
4818 int i;
4819 h = GetCurrentProcess();
4820 if (GetProcessAffinityMask(h, &mask, &smask)) {
4821 for(i = 0; i < 32; i++) {
4822 if (mask & (1 << i))
4823 break;
4824 }
4825 if (i != 32) {
4826 mask = 1 << i;
4827 SetProcessAffinityMask(h, mask);
4828 }
4829 }
4830 }
bellard67b915a2004-03-31 23:37:16 +00004831#endif
bellardbe995c22006-06-25 16:25:21 +00004832
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004833 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004834 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004835 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004836 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004837 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004838 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004839 kernel_filename = NULL;
4840 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004841 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004842 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00004843
aliguori1b8fc812009-02-27 20:01:39 +00004844 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
aliguori9ede2fd2009-01-15 20:05:25 +00004845 virtio_consoles[i] = NULL;
4846 virtio_console_index = 0;
4847
aliguori268a3622009-04-21 22:30:27 +00004848 for (i = 0; i < MAX_NODES; i++) {
4849 node_mem[i] = 0;
4850 node_cpumask[i] = 0;
4851 }
4852
aliguori268a3622009-04-21 22:30:27 +00004853 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004854 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004855
bellard26a5f132008-05-28 12:30:31 +00004856 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004857 autostart= 1;
4858
bellardcd6f1162004-05-13 22:02:20 +00004859 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004860 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004861 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004862 break;
bellardcd6f1162004-05-13 22:02:20 +00004863 r = argv[optind];
4864 if (r[0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004865 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004866 } else {
4867 const QEMUOption *popt;
4868
4869 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004870 /* Treat --foo the same as -foo. */
4871 if (r[1] == '-')
4872 r++;
bellardcd6f1162004-05-13 22:02:20 +00004873 popt = qemu_options;
4874 for(;;) {
4875 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004876 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004877 argv[0], r);
4878 exit(1);
4879 }
4880 if (!strcmp(popt->name, r + 1))
4881 break;
4882 popt++;
4883 }
4884 if (popt->flags & HAS_ARG) {
4885 if (optind >= argc) {
4886 fprintf(stderr, "%s: option '%s' requires an argument\n",
4887 argv[0], r);
4888 exit(1);
4889 }
4890 optarg = argv[optind++];
4891 } else {
4892 optarg = NULL;
4893 }
4894
4895 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004896 case QEMU_OPTION_M:
4897 machine = find_machine(optarg);
4898 if (!machine) {
4899 QEMUMachine *m;
4900 printf("Supported machines are:\n");
4901 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004902 if (m->alias)
4903 printf("%-10s %s (alias of %s)\n",
4904 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004905 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004906 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004907 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004908 }
ths15f82202007-06-29 23:26:08 +00004909 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004910 }
4911 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004912 case QEMU_OPTION_cpu:
4913 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004914 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004915/* XXX: implement xxx_cpu_list for targets that still miss it */
4916#if defined(cpu_list)
4917 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004918#endif
ths15f82202007-06-29 23:26:08 +00004919 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004920 } else {
4921 cpu_model = optarg;
4922 }
4923 break;
bellardcd6f1162004-05-13 22:02:20 +00004924 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004925 initrd_filename = optarg;
4926 break;
bellardcd6f1162004-05-13 22:02:20 +00004927 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004928 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004929 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004930 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004931 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004932 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004933 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004934 translation == BIOS_ATA_TRANSLATION_LBA ?
4935 ",trans=lba" :
4936 translation == BIOS_ATA_TRANSLATION_NONE ?
4937 ",trans=none" : "");
4938 break;
bellardcd6f1162004-05-13 22:02:20 +00004939 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004940 case QEMU_OPTION_hdc:
4941 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004942 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004943 break;
thse4bcb142007-12-02 04:51:10 +00004944 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004945 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004946 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02004947 case QEMU_OPTION_set:
4948 if (qemu_set_option(optarg) != 0)
4949 exit(1);
4950 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01004951 case QEMU_OPTION_global:
4952 if (qemu_global_option(optarg) != 0)
4953 exit(1);
4954 break;
balrog3e3d5812007-04-30 02:09:25 +00004955 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004956 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004957 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004958 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004959 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004960 break;
j_mayer86f55662007-04-24 06:52:59 +00004961 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004962 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004963 break;
bellardcd6f1162004-05-13 22:02:20 +00004964 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004965 snapshot = 1;
4966 break;
bellardcd6f1162004-05-13 22:02:20 +00004967 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004968 {
bellard330d0412003-07-26 18:11:40 +00004969 const char *p;
4970 p = optarg;
4971 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004972 if (cyls < 1 || cyls > 16383)
4973 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004974 if (*p != ',')
4975 goto chs_fail;
4976 p++;
4977 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004978 if (heads < 1 || heads > 16)
4979 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004980 if (*p != ',')
4981 goto chs_fail;
4982 p++;
4983 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004984 if (secs < 1 || secs > 63)
4985 goto chs_fail;
4986 if (*p == ',') {
4987 p++;
4988 if (!strcmp(p, "none"))
4989 translation = BIOS_ATA_TRANSLATION_NONE;
4990 else if (!strcmp(p, "lba"))
4991 translation = BIOS_ATA_TRANSLATION_LBA;
4992 else if (!strcmp(p, "auto"))
4993 translation = BIOS_ATA_TRANSLATION_AUTO;
4994 else
4995 goto chs_fail;
4996 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004997 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004998 fprintf(stderr, "qemu: invalid physical CHS format\n");
4999 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00005000 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005001 if (hda_opts != NULL) {
5002 char num[16];
5003 snprintf(num, sizeof(num), "%d", cyls);
5004 qemu_opt_set(hda_opts, "cyls", num);
5005 snprintf(num, sizeof(num), "%d", heads);
5006 qemu_opt_set(hda_opts, "heads", num);
5007 snprintf(num, sizeof(num), "%d", secs);
5008 qemu_opt_set(hda_opts, "secs", num);
5009 if (translation == BIOS_ATA_TRANSLATION_LBA)
5010 qemu_opt_set(hda_opts, "trans", "lba");
5011 if (translation == BIOS_ATA_TRANSLATION_NONE)
5012 qemu_opt_set(hda_opts, "trans", "none");
5013 }
bellard330d0412003-07-26 18:11:40 +00005014 }
5015 break;
aliguori268a3622009-04-21 22:30:27 +00005016 case QEMU_OPTION_numa:
5017 if (nb_numa_nodes >= MAX_NODES) {
5018 fprintf(stderr, "qemu: too many NUMA nodes\n");
5019 exit(1);
5020 }
5021 numa_add(optarg);
5022 break;
bellardcd6f1162004-05-13 22:02:20 +00005023 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005024 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005025 break;
balrog4d3b6f62008-02-10 16:33:14 +00005026#ifdef CONFIG_CURSES
5027 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005028 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005029 break;
5030#endif
balroga171fe32007-04-30 01:48:07 +00005031 case QEMU_OPTION_portrait:
5032 graphic_rotate = 1;
5033 break;
bellardcd6f1162004-05-13 22:02:20 +00005034 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005035 kernel_filename = optarg;
5036 break;
bellardcd6f1162004-05-13 22:02:20 +00005037 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005038 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005039 break;
bellardcd6f1162004-05-13 22:02:20 +00005040 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005041 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005042 break;
bellardcd6f1162004-05-13 22:02:20 +00005043 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005044 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005045 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02005046 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005047 };
5048 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005049 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005050 int legacy = 0;
5051
5052 if (!strchr(optarg, '=')) {
5053 legacy = 1;
5054 pstrcpy(buf, sizeof(buf), optarg);
5055 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5056 fprintf(stderr,
5057 "qemu: unknown boot parameter '%s' in '%s'\n",
5058 buf, optarg);
5059 exit(1);
5060 }
5061
5062 if (legacy ||
5063 get_param_value(buf, sizeof(buf), "order", optarg)) {
5064 boot_devices_bitmap = parse_bootdevices(buf);
5065 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005066 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005067 if (!legacy) {
5068 if (get_param_value(buf, sizeof(buf),
5069 "once", optarg)) {
5070 boot_devices_bitmap |= parse_bootdevices(buf);
5071 standard_boot_devices = qemu_strdup(boot_devices);
5072 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5073 qemu_register_reset(restore_boot_devices,
5074 standard_boot_devices);
5075 }
Jan Kiszka95387492009-07-02 00:19:02 +02005076 if (get_param_value(buf, sizeof(buf),
5077 "menu", optarg)) {
5078 if (!strcmp(buf, "on")) {
5079 boot_menu = 1;
5080 } else if (!strcmp(buf, "off")) {
5081 boot_menu = 0;
5082 } else {
5083 fprintf(stderr,
5084 "qemu: invalid option value '%s'\n",
5085 buf);
5086 exit(1);
5087 }
5088 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005089 }
bellard36b486b2003-11-11 13:36:08 +00005090 }
5091 break;
bellardcd6f1162004-05-13 22:02:20 +00005092 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005093 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005094 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005095 break;
bellard52ca8d62006-06-14 16:03:05 +00005096#ifdef TARGET_I386
5097 case QEMU_OPTION_no_fd_bootchk:
5098 fd_bootchk = 0;
5099 break;
5100#endif
Mark McLoughlina1ea4582009-10-08 19:58:26 +01005101 case QEMU_OPTION_netdev:
5102 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
5103 exit(1);
5104 }
5105 break;
bellard7c9d8e02005-11-15 22:16:05 +00005106 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01005107 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00005108 exit(1);
5109 }
bellard702c6512004-04-02 21:21:32 +00005110 break;
bellardc7f74642004-08-24 21:57:12 +00005111#ifdef CONFIG_SLIRP
5112 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005113 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005114 break;
ths47d5d012007-02-20 00:05:08 +00005115 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005116 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005117 break;
bellardc94c8d62004-09-13 21:37:34 +00005118#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005119 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01005120 if (net_slirp_smb(optarg) < 0)
5121 exit(1);
bellard9d728e82004-09-05 23:09:03 +00005122 break;
bellardc94c8d62004-09-13 21:37:34 +00005123#endif
bellard9bf05442004-08-25 22:12:49 +00005124 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01005125 if (net_slirp_redir(optarg) < 0)
5126 exit(1);
bellard9bf05442004-08-25 22:12:49 +00005127 break;
bellardc7f74642004-08-24 21:57:12 +00005128#endif
balrogdc72ac12008-11-09 00:04:26 +00005129 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005130 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00005131 break;
bellard1d14ffa2005-10-30 18:58:22 +00005132#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005133 case QEMU_OPTION_audio_help:
5134 AUD_help ();
5135 exit (0);
5136 break;
5137 case QEMU_OPTION_soundhw:
5138 select_soundhw (optarg);
5139 break;
5140#endif
bellardcd6f1162004-05-13 22:02:20 +00005141 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005142 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005143 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005144 case QEMU_OPTION_version:
5145 version();
5146 exit(0);
5147 break;
aurel3200f82b82008-04-27 21:12:55 +00005148 case QEMU_OPTION_m: {
5149 uint64_t value;
5150 char *ptr;
5151
5152 value = strtoul(optarg, &ptr, 10);
5153 switch (*ptr) {
5154 case 0: case 'M': case 'm':
5155 value <<= 20;
5156 break;
5157 case 'G': case 'g':
5158 value <<= 30;
5159 break;
5160 default:
5161 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005162 exit(1);
5163 }
aurel3200f82b82008-04-27 21:12:55 +00005164
5165 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05005166 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00005167 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5168 exit(1);
5169 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005170 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00005171 fprintf(stderr, "qemu: ram size too large\n");
5172 exit(1);
5173 }
5174 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005175 break;
aurel3200f82b82008-04-27 21:12:55 +00005176 }
bellardcd6f1162004-05-13 22:02:20 +00005177 case QEMU_OPTION_d:
5178 {
5179 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005180 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005181
bellardcd6f1162004-05-13 22:02:20 +00005182 mask = cpu_str_to_log_mask(optarg);
5183 if (!mask) {
5184 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005185 for(item = cpu_log_items; item->mask != 0; item++) {
5186 printf("%-10s %s\n", item->name, item->help);
5187 }
5188 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005189 }
5190 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005191 }
bellardcd6f1162004-05-13 22:02:20 +00005192 break;
bellardcd6f1162004-05-13 22:02:20 +00005193 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005194 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005195 break;
aliguori59030a82009-04-05 18:43:41 +00005196 case QEMU_OPTION_gdb:
5197 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005198 break;
bellardcd6f1162004-05-13 22:02:20 +00005199 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005200 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005201 break;
j_mayer1192dad2007-10-05 13:08:35 +00005202 case QEMU_OPTION_bios:
5203 bios_name = optarg;
5204 break;
aurel321b530a62009-04-05 20:08:59 +00005205 case QEMU_OPTION_singlestep:
5206 singlestep = 1;
5207 break;
bellardcd6f1162004-05-13 22:02:20 +00005208 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005209 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005210 break;
bellard3d11d0e2004-12-12 16:56:30 +00005211 case QEMU_OPTION_k:
5212 keyboard_layout = optarg;
5213 break;
bellardee22c2f2004-06-03 12:49:50 +00005214 case QEMU_OPTION_localtime:
5215 rtc_utc = 0;
5216 break;
malc3893c122008-09-28 00:42:05 +00005217 case QEMU_OPTION_vga:
5218 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005219 break;
blueswir15824d652009-03-28 06:44:27 +00005220#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005221 case QEMU_OPTION_g:
5222 {
5223 const char *p;
5224 int w, h, depth;
5225 p = optarg;
5226 w = strtol(p, (char **)&p, 10);
5227 if (w <= 0) {
5228 graphic_error:
5229 fprintf(stderr, "qemu: invalid resolution or depth\n");
5230 exit(1);
5231 }
5232 if (*p != 'x')
5233 goto graphic_error;
5234 p++;
5235 h = strtol(p, (char **)&p, 10);
5236 if (h <= 0)
5237 goto graphic_error;
5238 if (*p == 'x') {
5239 p++;
5240 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005241 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005242 depth != 24 && depth != 32)
5243 goto graphic_error;
5244 } else if (*p == '\0') {
5245 depth = graphic_depth;
5246 } else {
5247 goto graphic_error;
5248 }
ths3b46e622007-09-17 08:09:54 +00005249
bellarde9b137c2004-06-21 16:46:10 +00005250 graphic_width = w;
5251 graphic_height = h;
5252 graphic_depth = depth;
5253 }
5254 break;
blueswir15824d652009-03-28 06:44:27 +00005255#endif
ths20d8a3e2007-02-18 17:04:49 +00005256 case QEMU_OPTION_echr:
5257 {
5258 char *r;
5259 term_escape_char = strtol(optarg, &r, 0);
5260 if (r == optarg)
5261 printf("Bad argument to echr\n");
5262 break;
5263 }
bellard82c643f2004-07-14 17:28:13 +00005264 case QEMU_OPTION_monitor:
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005265 add_device_config(DEV_MONITOR, optarg);
5266 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00005267 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005268 case QEMU_OPTION_chardev:
5269 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
5270 if (!opts) {
5271 fprintf(stderr, "parse error: %s\n", optarg);
5272 exit(1);
5273 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005274 break;
bellard82c643f2004-07-14 17:28:13 +00005275 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005276 add_device_config(DEV_SERIAL, optarg);
5277 default_serial = 0;
bellard82c643f2004-07-14 17:28:13 +00005278 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005279 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02005280 if (watchdog) {
5281 fprintf(stderr,
5282 "qemu: only one watchdog option may be given\n");
5283 return 1;
5284 }
5285 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005286 break;
5287 case QEMU_OPTION_watchdog_action:
5288 if (select_watchdog_action(optarg) == -1) {
5289 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5290 exit(1);
5291 }
5292 break;
aliguori51ecf132009-01-15 20:06:40 +00005293 case QEMU_OPTION_virtiocon:
5294 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5295 fprintf(stderr, "qemu: too many virtio consoles\n");
5296 exit(1);
5297 }
5298 virtio_consoles[virtio_console_index] = optarg;
5299 virtio_console_index++;
5300 break;
bellard6508fe52005-01-15 12:02:56 +00005301 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005302 add_device_config(DEV_PARALLEL, optarg);
5303 default_parallel = 0;
bellard6508fe52005-01-15 12:02:56 +00005304 break;
bellardd63d3072004-10-03 13:29:03 +00005305 case QEMU_OPTION_loadvm:
5306 loadvm = optarg;
5307 break;
5308 case QEMU_OPTION_full_screen:
5309 full_screen = 1;
5310 break;
ths667acca2006-12-11 02:08:05 +00005311#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005312 case QEMU_OPTION_no_frame:
5313 no_frame = 1;
5314 break;
ths3780e192007-06-21 21:08:02 +00005315 case QEMU_OPTION_alt_grab:
5316 alt_grab = 1;
5317 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05005318 case QEMU_OPTION_ctrl_grab:
5319 ctrl_grab = 1;
5320 break;
ths667acca2006-12-11 02:08:05 +00005321 case QEMU_OPTION_no_quit:
5322 no_quit = 1;
5323 break;
aliguori7d957bd2009-01-15 22:14:11 +00005324 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005325 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005326 break;
ths667acca2006-12-11 02:08:05 +00005327#endif
bellardf7cce892004-12-08 22:21:25 +00005328 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005329 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005330 break;
bellarda09db212005-04-30 16:10:35 +00005331#ifdef TARGET_I386
5332 case QEMU_OPTION_win2k_hack:
5333 win2k_install_hack = 1;
5334 break;
aliguori73822ec2009-01-15 20:11:34 +00005335 case QEMU_OPTION_rtc_td_hack:
5336 rtc_td_hack = 1;
5337 break;
aliguori8a92ea22009-02-27 20:12:36 +00005338 case QEMU_OPTION_acpitable:
5339 if(acpi_table_add(optarg) < 0) {
5340 fprintf(stderr, "Wrong acpi table provided\n");
5341 exit(1);
5342 }
5343 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005344 case QEMU_OPTION_smbios:
5345 if(smbios_entry_add(optarg) < 0) {
5346 fprintf(stderr, "Wrong smbios provided\n");
5347 exit(1);
5348 }
5349 break;
bellarda09db212005-04-30 16:10:35 +00005350#endif
aliguori7ba1e612008-11-05 16:04:33 +00005351#ifdef CONFIG_KVM
5352 case QEMU_OPTION_enable_kvm:
5353 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00005354 break;
5355#endif
bellardbb36d472005-11-05 14:22:28 +00005356 case QEMU_OPTION_usb:
5357 usb_enabled = 1;
5358 break;
bellarda594cfb2005-11-06 16:13:29 +00005359 case QEMU_OPTION_usbdevice:
5360 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005361 add_device_config(DEV_USB, optarg);
5362 break;
5363 case QEMU_OPTION_device:
Mark McLoughlinb386bec2009-10-06 12:17:01 +01005364 if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005365 exit(1);
5366 }
bellarda594cfb2005-11-06 16:13:29 +00005367 break;
bellard6a00d602005-11-21 23:25:50 +00005368 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02005369 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00005370 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005371 fprintf(stderr, "Invalid number of CPUs\n");
5372 exit(1);
5373 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005374 if (max_cpus < smp_cpus) {
5375 fprintf(stderr, "maxcpus must be equal to or greater than "
5376 "smp\n");
5377 exit(1);
5378 }
5379 if (max_cpus > 255) {
5380 fprintf(stderr, "Unsupported number of maxcpus\n");
5381 exit(1);
5382 }
bellard6a00d602005-11-21 23:25:50 +00005383 break;
bellard24236862006-04-30 21:28:36 +00005384 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005385 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005386 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005387 break;
blueswir15824d652009-03-28 06:44:27 +00005388#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005389 case QEMU_OPTION_no_acpi:
5390 acpi_enabled = 0;
5391 break;
aliguori16b29ae2008-12-17 23:28:44 +00005392 case QEMU_OPTION_no_hpet:
5393 no_hpet = 1;
5394 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005395 case QEMU_OPTION_balloon:
5396 if (balloon_parse(optarg) < 0) {
5397 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5398 exit(1);
5399 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005400 break;
blueswir15824d652009-03-28 06:44:27 +00005401#endif
bellardd1beab82006-10-02 19:44:22 +00005402 case QEMU_OPTION_no_reboot:
5403 no_reboot = 1;
5404 break;
aurel32b2f76162008-04-11 21:35:52 +00005405 case QEMU_OPTION_no_shutdown:
5406 no_shutdown = 1;
5407 break;
balrog9467cd42007-05-01 01:34:14 +00005408 case QEMU_OPTION_show_cursor:
5409 cursor_hide = 0;
5410 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005411 case QEMU_OPTION_uuid:
5412 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5413 fprintf(stderr, "Fail to parse UUID string."
5414 " Wrong format.\n");
5415 exit(1);
5416 }
5417 break;
blueswir15824d652009-03-28 06:44:27 +00005418#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005419 case QEMU_OPTION_daemonize:
5420 daemonize = 1;
5421 break;
blueswir15824d652009-03-28 06:44:27 +00005422#endif
ths9ae02552007-01-05 17:39:04 +00005423 case QEMU_OPTION_option_rom:
5424 if (nb_option_roms >= MAX_OPTION_ROMS) {
5425 fprintf(stderr, "Too many option ROMs\n");
5426 exit(1);
5427 }
5428 option_rom[nb_option_roms] = optarg;
5429 nb_option_roms++;
5430 break;
blueswir15824d652009-03-28 06:44:27 +00005431#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005432 case QEMU_OPTION_semihosting:
5433 semihosting_enabled = 1;
5434 break;
blueswir15824d652009-03-28 06:44:27 +00005435#endif
thsc35734b2007-03-19 15:17:08 +00005436 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005437 qemu_name = qemu_strdup(optarg);
5438 {
5439 char *p = strchr(qemu_name, ',');
5440 if (p != NULL) {
5441 *p++ = 0;
5442 if (strncmp(p, "process=", 8)) {
5443 fprintf(stderr, "Unknown subargument %s to -name", p);
5444 exit(1);
5445 }
5446 p += 8;
5447 set_proc_name(p);
5448 }
5449 }
thsc35734b2007-03-19 15:17:08 +00005450 break;
blueswir195efd112008-12-24 20:26:14 +00005451#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005452 case QEMU_OPTION_prom_env:
5453 if (nb_prom_envs >= MAX_PROM_ENVS) {
5454 fprintf(stderr, "Too many prom variables\n");
5455 exit(1);
5456 }
5457 prom_envs[nb_prom_envs] = optarg;
5458 nb_prom_envs++;
5459 break;
5460#endif
balrog2b8f2d42007-07-27 22:08:46 +00005461#ifdef TARGET_ARM
5462 case QEMU_OPTION_old_param:
5463 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005464 break;
balrog2b8f2d42007-07-27 22:08:46 +00005465#endif
thsf3dcfad2007-08-24 01:26:02 +00005466 case QEMU_OPTION_clock:
5467 configure_alarms(optarg);
5468 break;
bellard7e0af5d02007-11-07 16:24:33 +00005469 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005470 configure_rtc_date_offset(optarg, 1);
5471 break;
5472 case QEMU_OPTION_rtc:
5473 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL);
5474 if (!opts) {
5475 fprintf(stderr, "parse error: %s\n", optarg);
5476 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00005477 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005478 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00005479 break;
bellard26a5f132008-05-28 12:30:31 +00005480 case QEMU_OPTION_tb_size:
5481 tb_size = strtol(optarg, NULL, 0);
5482 if (tb_size < 0)
5483 tb_size = 0;
5484 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005485 case QEMU_OPTION_icount:
5486 use_icount = 1;
5487 if (strcmp(optarg, "auto") == 0) {
5488 icount_time_shift = -1;
5489 } else {
5490 icount_time_shift = strtol(optarg, NULL, 0);
5491 }
5492 break;
aliguori5bb79102008-10-13 03:12:02 +00005493 case QEMU_OPTION_incoming:
5494 incoming = optarg;
5495 break;
blueswir15824d652009-03-28 06:44:27 +00005496#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005497 case QEMU_OPTION_chroot:
5498 chroot_dir = optarg;
5499 break;
5500 case QEMU_OPTION_runas:
5501 run_as = optarg;
5502 break;
blueswir15824d652009-03-28 06:44:27 +00005503#endif
aliguorie37630c2009-04-22 15:19:10 +00005504#ifdef CONFIG_XEN
5505 case QEMU_OPTION_xen_domid:
5506 xen_domid = atoi(optarg);
5507 break;
5508 case QEMU_OPTION_xen_create:
5509 xen_mode = XEN_CREATE;
5510 break;
5511 case QEMU_OPTION_xen_attach:
5512 xen_mode = XEN_ATTACH;
5513 break;
5514#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02005515 case QEMU_OPTION_readconfig:
5516 {
5517 FILE *fp;
5518 fp = fopen(optarg, "r");
5519 if (fp == NULL) {
5520 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5521 exit(1);
5522 }
5523 if (qemu_config_parse(fp) != 0) {
5524 exit(1);
5525 }
5526 fclose(fp);
5527 break;
5528 }
5529 case QEMU_OPTION_writeconfig:
5530 {
5531 FILE *fp;
5532 if (strcmp(optarg, "-") == 0) {
5533 fp = stdout;
5534 } else {
5535 fp = fopen(optarg, "w");
5536 if (fp == NULL) {
5537 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5538 exit(1);
5539 }
5540 }
5541 qemu_config_write(fp);
5542 fclose(fp);
5543 break;
5544 }
bellardcd6f1162004-05-13 22:02:20 +00005545 }
bellard0824d6f2003-06-24 13:42:40 +00005546 }
5547 }
bellard330d0412003-07-26 18:11:40 +00005548
Paul Brook5cea8592009-05-30 00:52:44 +01005549 /* If no data_dir is specified then try to find it relative to the
5550 executable path. */
5551 if (!data_dir) {
5552 data_dir = find_datadir(argv[0]);
5553 }
5554 /* If all else fails use the install patch specified when building. */
5555 if (!data_dir) {
5556 data_dir = CONFIG_QEMU_SHAREDIR;
5557 }
5558
Jes Sorensen6be68d72009-07-23 17:03:42 +02005559 /*
5560 * Default to max_cpus = smp_cpus, in case the user doesn't
5561 * specify a max_cpus value.
5562 */
5563 if (!max_cpus)
5564 max_cpus = smp_cpus;
5565
balrog3d878ca2008-10-28 10:59:59 +00005566 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005567 if (smp_cpus > machine->max_cpus) {
5568 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5569 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5570 machine->max_cpus);
5571 exit(1);
5572 }
5573
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005574 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
5575
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005576 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005577 if (default_serial)
5578 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005579 if (default_parallel)
5580 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005581 if (default_monitor)
5582 add_device_config(DEV_MONITOR, "stdio");
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005583 } else {
5584 if (default_serial)
5585 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005586 if (default_parallel)
5587 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005588 if (default_monitor)
5589 add_device_config(DEV_MONITOR, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00005590 }
5591
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01005592 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
5593 exit(1);
5594
ths71e3ceb2006-12-22 02:11:31 +00005595#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005596 if (daemonize) {
5597 pid_t pid;
5598
5599 if (pipe(fds) == -1)
5600 exit(1);
5601
5602 pid = fork();
5603 if (pid > 0) {
5604 uint8_t status;
5605 ssize_t len;
5606
5607 close(fds[1]);
5608
5609 again:
ths93815bc2007-03-19 15:58:31 +00005610 len = read(fds[0], &status, 1);
5611 if (len == -1 && (errno == EINTR))
5612 goto again;
5613
5614 if (len != 1)
5615 exit(1);
5616 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005617 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005618 exit(1);
5619 } else
5620 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005621 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005622 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005623
Kevin Wolf40ff6d72009-12-02 12:24:42 +01005624 close(fds[0]);
5625 qemu_set_cloexec(fds[1]);
5626
ths71e3ceb2006-12-22 02:11:31 +00005627 setsid();
5628
5629 pid = fork();
5630 if (pid > 0)
5631 exit(0);
5632 else if (pid < 0)
5633 exit(1);
5634
5635 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005636
5637 signal(SIGTSTP, SIG_IGN);
5638 signal(SIGTTOU, SIG_IGN);
5639 signal(SIGTTIN, SIG_IGN);
5640 }
ths71e3ceb2006-12-22 02:11:31 +00005641
thsaa26bb22007-03-25 21:33:06 +00005642 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005643 if (daemonize) {
5644 uint8_t status = 1;
5645 write(fds[1], &status, 1);
5646 } else
Justin M. Forbes850810d2009-10-01 09:42:56 -05005647 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005648 exit(1);
5649 }
blueswir1b9e82a52009-04-05 18:03:31 +00005650#endif
ths93815bc2007-03-19 15:58:31 +00005651
Marcelo Tosatti214910a2009-09-18 02:41:23 -03005652 if (kvm_enabled()) {
5653 int ret;
5654
5655 ret = kvm_init(smp_cpus);
5656 if (ret < 0) {
5657 fprintf(stderr, "failed to initialize KVM\n");
5658 exit(1);
5659 }
5660 }
5661
aliguori3fcf7b62009-04-24 18:03:25 +00005662 if (qemu_init_main_loop()) {
5663 fprintf(stderr, "qemu_init_main_loop failed\n");
5664 exit(1);
5665 }
bellarda20dd502003-09-30 21:07:02 +00005666 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005667
thsf8d39c02008-07-03 10:01:15 +00005668 if (!linux_boot && *kernel_cmdline != '\0') {
5669 fprintf(stderr, "-append only allowed with -kernel option\n");
5670 exit(1);
5671 }
5672
5673 if (!linux_boot && initrd_filename != NULL) {
5674 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5675 exit(1);
5676 }
5677
Filip Navarabf65f532009-07-27 10:02:04 -05005678#ifndef _WIN32
5679 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00005680 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05005681#endif
ths3b46e622007-09-17 08:09:54 +00005682
aliguori7183b4b2008-11-05 20:40:18 +00005683 if (init_timer_alarm() < 0) {
5684 fprintf(stderr, "could not initialize alarm timer\n");
5685 exit(1);
5686 }
pbrook2e70f6e2008-06-29 01:03:05 +00005687 if (use_icount && icount_time_shift < 0) {
5688 use_icount = 2;
5689 /* 125MIPS seems a reasonable initial guess at the guest speed.
5690 It will be corrected fairly quickly anyway. */
5691 icount_time_shift = 3;
5692 init_icount_adjust();
5693 }
pbrook634fce92006-07-15 17:40:09 +00005694
bellardfd1dff42006-02-01 21:29:26 +00005695#ifdef _WIN32
5696 socket_init();
5697#endif
5698
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01005699 if (net_init_clients() < 0) {
5700 exit(1);
bellard702c6512004-04-02 21:21:32 +00005701 }
bellardf1510b22003-06-25 00:07:40 +00005702
Glauber Costa406c8df2009-06-17 09:05:30 -04005703 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5704 net_set_boot_mask(net_boot);
5705
balrogdc72ac12008-11-09 00:04:26 +00005706 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005707 if (foreach_device_config(DEV_BT, bt_parse))
5708 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005709
bellard0824d6f2003-06-24 13:42:40 +00005710 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005711 if (ram_size == 0)
5712 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005713
bellard26a5f132008-05-28 12:30:31 +00005714 /* init the dynamic translator */
5715 cpu_exec_init_all(tb_size * 1024 * 1024);
5716
Markus Armbrustereb852012009-10-27 18:41:44 +01005717 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00005718
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005719 blk_mig_init();
5720
thse4bcb142007-12-02 04:51:10 +00005721 /* we always create the cdrom drive, even if no disk is there */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005722 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005723
balrog9d413d12007-12-04 00:10:34 +00005724 /* we always create at least one floppy */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005725 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005726
balrog9d413d12007-12-04 00:10:34 +00005727 /* we always create one sd slot, even if no card is in it */
Gerd Hoffmann3b0ba922009-07-22 16:42:59 +02005728 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005729
ths96d30e42007-01-07 20:42:14 +00005730 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005731 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02005732 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5733 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005734 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005735
Juan Quintela2faf58c2009-09-10 03:04:28 +02005736 vmstate_register(0, &vmstate_timers ,&timers_state);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005737 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
5738 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005739
aliguori3023f332009-01-16 19:04:14 +00005740 /* Maintain compatibility with multiple stdio monitors */
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005741 serial_monitor_mux();
aliguori3023f332009-01-16 19:04:14 +00005742
aliguori268a3622009-04-21 22:30:27 +00005743 if (nb_numa_nodes > 0) {
5744 int i;
5745
5746 if (nb_numa_nodes > smp_cpus) {
5747 nb_numa_nodes = smp_cpus;
5748 }
5749
5750 /* If no memory size if given for any node, assume the default case
5751 * and distribute the available memory equally across all nodes
5752 */
5753 for (i = 0; i < nb_numa_nodes; i++) {
5754 if (node_mem[i] != 0)
5755 break;
5756 }
5757 if (i == nb_numa_nodes) {
5758 uint64_t usedmem = 0;
5759
5760 /* On Linux, the each node's border has to be 8MB aligned,
5761 * the final node gets the rest.
5762 */
5763 for (i = 0; i < nb_numa_nodes - 1; i++) {
5764 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5765 usedmem += node_mem[i];
5766 }
5767 node_mem[i] = ram_size - usedmem;
5768 }
5769
5770 for (i = 0; i < nb_numa_nodes; i++) {
5771 if (node_cpumask[i] != 0)
5772 break;
5773 }
5774 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5775 * must cope with this anyway, because there are BIOSes out there in
5776 * real machines which also use this scheme.
5777 */
5778 if (i == nb_numa_nodes) {
5779 for (i = 0; i < smp_cpus; i++) {
5780 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5781 }
5782 }
5783 }
5784
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005785 if (foreach_device_config(DEV_MONITOR, monitor_parse) < 0)
5786 exit(1);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005787 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
5788 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005789 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
5790 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00005791
5792 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5793 const char *devname = virtio_consoles[i];
5794 if (devname && strcmp(devname, "none")) {
5795 char label[32];
5796 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005797 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005798 if (!virtcon_hds[i]) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005799 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
5800 devname, strerror(errno));
aliguori2796dae2009-01-16 20:23:27 +00005801 exit(1);
5802 }
5803 }
5804 }
5805
Paul Brookaae94602009-05-14 22:35:06 +01005806 module_call_init(MODULE_INIT_DEVICE);
5807
Markus Armbruster09aaa162009-08-21 10:31:34 +02005808 if (watchdog) {
5809 i = select_watchdog(watchdog);
5810 if (i > 0)
5811 exit (i == 1 ? 1 : 0);
5812 }
5813
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005814 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01005815 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005816 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01005817 qemu_add_globals();
5818
Paul Brookfbe1b592009-05-13 17:56:25 +01005819 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005820 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5821
aliguori268a3622009-04-21 22:30:27 +00005822
Juan Quintela67b3b712009-08-28 19:25:15 +02005823#ifndef _WIN32
5824 /* must be after terminal init, SDL library changes signal handlers */
5825 sighandler_setup();
5826#endif
5827
aliguori268a3622009-04-21 22:30:27 +00005828 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5829 for (i = 0; i < nb_numa_nodes; i++) {
5830 if (node_cpumask[i] & (1 << env->cpu_index)) {
5831 env->numa_node = i;
5832 }
5833 }
5834 }
5835
aliguori6f338c32009-02-11 15:21:54 +00005836 current_machine = machine;
5837
aliguori3023f332009-01-16 19:04:14 +00005838 /* init USB devices */
5839 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01005840 if (foreach_device_config(DEV_USB, usb_parse) < 0)
5841 exit(1);
aliguori3023f332009-01-16 19:04:14 +00005842 }
5843
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005844 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005845 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005846 exit(1);
5847
aliguori8f391ab2009-01-19 16:34:10 +00005848 if (!display_state)
5849 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005850 /* just use the first displaystate for the moment */
5851 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005852
5853 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005854#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005855 display_type = DT_SDL;
5856#else
5857 display_type = DT_VNC;
5858 vnc_display = "localhost:0,to=99";
5859 show_vnc_port = 1;
5860#endif
5861 }
5862
5863
5864 switch (display_type) {
5865 case DT_NOGRAPHIC:
5866 break;
5867#if defined(CONFIG_CURSES)
5868 case DT_CURSES:
5869 curses_display_init(ds, full_screen);
5870 break;
5871#endif
bellard5b0753e2005-03-01 21:37:28 +00005872#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005873 case DT_SDL:
5874 sdl_display_init(ds, full_screen, no_frame);
5875 break;
bellard5b0753e2005-03-01 21:37:28 +00005876#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005877 case DT_SDL:
5878 cocoa_display_init(ds, full_screen);
5879 break;
bellard313aa562003-08-10 21:52:11 +00005880#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005881 case DT_VNC:
5882 vnc_display_init(ds);
5883 if (vnc_display_open(ds, vnc_display) < 0)
5884 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005885
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005886 if (show_vnc_port) {
5887 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005888 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005889 break;
5890 default:
5891 break;
bellard313aa562003-08-10 21:52:11 +00005892 }
aliguori7d957bd2009-01-15 22:14:11 +00005893 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005894
aliguori3023f332009-01-16 19:04:14 +00005895 dcl = ds->listeners;
5896 while (dcl != NULL) {
5897 if (dcl->dpy_refresh != NULL) {
5898 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5899 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005900 }
aliguori3023f332009-01-16 19:04:14 +00005901 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005902 }
aliguori3023f332009-01-16 19:04:14 +00005903
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005904 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00005905 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5906 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5907 }
5908
aliguori2796dae2009-01-16 20:23:27 +00005909 text_consoles_set_display(display_state);
5910
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005911 for (i = 0; i < MAX_MONITOR_DEVICES; i++) {
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005912 if (monitor_hds[i]) {
Gerd Hoffmann4e307fc2009-12-08 13:11:37 +01005913 monitor_init(monitor_hds[i],
5914 MONITOR_USE_READLINE |
5915 ((i == 0) ? MONITOR_IS_DEFAULT : 0));
Jan Kiszkaddd9bbd2009-08-27 19:51:16 +02005916 }
5917 }
bellard82c643f2004-07-14 17:28:13 +00005918
aliguori59030a82009-04-05 18:43:41 +00005919 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
5920 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
5921 gdbstub_dev);
5922 exit(1);
balrog45669e02007-07-02 13:20:17 +00005923 }
balrog45669e02007-07-02 13:20:17 +00005924
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02005925 qdev_machine_creation_done();
5926
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02005927 rom_load_all();
5928
Juan Quintela504c2942009-11-12 00:39:13 +01005929 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02005930 if (loadvm) {
5931 if (load_vmstate(cur_mon, loadvm) < 0) {
5932 autostart = 0;
5933 }
5934 }
bellardd63d3072004-10-03 13:29:03 +00005935
Glauber Costa2bb8c102009-07-24 16:20:23 -04005936 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00005937 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03005938 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00005939 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03005940 }
thsffd843b2006-12-21 19:46:43 +00005941
blueswir1b9e82a52009-04-05 18:03:31 +00005942#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005943 if (daemonize) {
5944 uint8_t status = 0;
5945 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00005946
5947 again1:
5948 len = write(fds[1], &status, 1);
5949 if (len == -1 && (errno == EINTR))
5950 goto again1;
5951
5952 if (len != 1)
5953 exit(1);
5954
aliguoribd54b862008-07-23 00:58:33 +00005955 chdir("/");
Kevin Wolf40ff6d72009-12-02 12:24:42 +01005956 TFR(fd = qemu_open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005957 if (fd == -1)
5958 exit(1);
aliguori08585322009-02-27 22:09:45 +00005959 }
ths71e3ceb2006-12-22 02:11:31 +00005960
aliguori08585322009-02-27 22:09:45 +00005961 if (run_as) {
5962 pwd = getpwnam(run_as);
5963 if (!pwd) {
5964 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
5965 exit(1);
5966 }
5967 }
ths71e3ceb2006-12-22 02:11:31 +00005968
aliguori08585322009-02-27 22:09:45 +00005969 if (chroot_dir) {
5970 if (chroot(chroot_dir) < 0) {
5971 fprintf(stderr, "chroot failed\n");
5972 exit(1);
5973 }
5974 chdir("/");
5975 }
5976
5977 if (run_as) {
5978 if (setgid(pwd->pw_gid) < 0) {
5979 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
5980 exit(1);
5981 }
5982 if (setuid(pwd->pw_uid) < 0) {
5983 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
5984 exit(1);
5985 }
5986 if (setuid(0) != -1) {
5987 fprintf(stderr, "Dropping privileges failed\n");
5988 exit(1);
5989 }
5990 }
aliguori08585322009-02-27 22:09:45 +00005991
5992 if (daemonize) {
5993 dup2(fd, 0);
5994 dup2(fd, 1);
5995 dup2(fd, 2);
5996
5997 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00005998 }
blueswir1b9e82a52009-04-05 18:03:31 +00005999#endif
ths71e3ceb2006-12-22 02:11:31 +00006000
bellard8a7ddc32004-03-31 19:00:16 +00006001 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006002 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006003 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006004
bellard0824d6f2003-06-24 13:42:40 +00006005 return 0;
6006}