blob: e881e456b668975bec1c435daa199903b44620b2 [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"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200159#include "qemu-objects.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
bellard0824d6f2003-06-24 13:42:40 +0000161#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000162
bellard8a7ddc32004-03-31 19:00:16 +0000163#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000164
blueswir1511d2b12009-03-07 15:32:56 +0000165#include "qemu_socket.h"
166
Jan Kiszkad918f232009-06-24 14:42:30 +0200167#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000168
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169#include "qemu-queue.h"
170
blueswir19dc63a12008-10-04 07:25:46 +0000171//#define DEBUG_NET
172//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000173
bellard1bfe8562004-07-08 21:17:50 +0000174#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000175
Paul Brook5cea8592009-05-30 00:52:44 +0100176static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000177const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000178/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000179 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000180struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
181struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000182enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000183static DisplayState *display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500184DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000185const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500186ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000187int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000188NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000189int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200190int autostart;
balrogf6503052008-02-17 11:42:19 +0000191static int rtc_utc = 1;
192static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200193QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100194int vga_interface_type = VGA_NONE;
bellardd8272202005-04-06 20:32:23 +0000195#ifdef TARGET_SPARC
196int graphic_width = 1024;
197int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000198int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000199#else
bellard1bfe8562004-07-08 21:17:50 +0000200int graphic_width = 800;
201int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000202int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000203#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000204static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000205#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000206static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000207#endif
ths667acca2006-12-11 02:08:05 +0000208int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000209CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000210CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000211CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000212#ifdef TARGET_I386
213int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000214int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000215#endif
bellardbb36d472005-11-05 14:22:28 +0000216int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000217int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000218int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200219int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200220int smp_cores = 1;
221int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000222const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000223int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000224int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000225int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000226int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000227int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000228int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000229int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200230uint8_t irq0override = 1;
blueswir1b9e82a52009-04-05 18:03:31 +0000231#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000232int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000233#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200234const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000235const char *option_rom[MAX_OPTION_ROMS];
236int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000237int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000238#ifdef TARGET_ARM
239int old_param = 0;
240#endif
thsc35734b2007-03-19 15:17:08 +0000241const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000242int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500243int ctrl_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000244#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000245unsigned int nb_prom_envs = 0;
246const char *prom_envs[MAX_PROM_ENVS];
247#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200248int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000249
aliguori268a3622009-04-21 22:30:27 +0000250int nb_numa_nodes;
251uint64_t node_mem[MAX_NODES];
252uint64_t node_cpumask[MAX_NODES];
253
balrogee5605e2007-12-03 03:01:40 +0000254static CPUState *cur_cpu;
255static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000256static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000257/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000258static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000259/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000260#define MAX_ICOUNT_SHIFT 10
261/* Compensate for varying guest execution speed. */
262static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000263static QEMUTimer *icount_rt_timer;
264static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000265static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000266
blueswir18fcb1b92008-09-18 18:29:08 +0000267uint8_t qemu_uuid[16];
268
Jan Kiszka76e30d02009-07-02 00:19:02 +0200269static QEMUBootSetHandler *boot_set_handler;
270static void *boot_set_opaque;
271
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100272static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100273static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100274static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100275static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100276static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100277static int default_floppy = 1;
278static int default_cdrom = 1;
279static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100280
281static struct {
282 const char *driver;
283 int *flag;
284} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100285 { .driver = "isa-serial", .flag = &default_serial },
286 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100287 { .driver = "isa-fdc", .flag = &default_floppy },
288 { .driver = "ide-drive", .flag = &default_cdrom },
Gerd Hoffmannaee1b932009-12-08 13:11:53 +0100289 { .driver = "virtio-console-pci", .flag = &default_virtcon },
290 { .driver = "virtio-console-s390", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100291 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100292 { .driver = "cirrus-vga", .flag = &default_vga },
293 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100294};
295
296static int default_driver_check(QemuOpts *opts, void *opaque)
297{
298 const char *driver = qemu_opt_get(opts, "driver");
299 int i;
300
301 if (!driver)
302 return 0;
303 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
304 if (strcmp(default_list[i].driver, driver) != 0)
305 continue;
306 *(default_list[i].flag) = 0;
307 }
308 return 0;
309}
310
bellard0824d6f2003-06-24 13:42:40 +0000311/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000312/* x86 ISA bus support */
313
Anthony Liguoric227f092009-10-01 16:12:16 -0500314target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000315PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000316
bellard0824d6f2003-06-24 13:42:40 +0000317/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000318void hw_error(const char *fmt, ...)
319{
320 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000321 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000322
323 va_start(ap, fmt);
324 fprintf(stderr, "qemu: hardware error: ");
325 vfprintf(stderr, fmt, ap);
326 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000327 for(env = first_cpu; env != NULL; env = env->next_cpu) {
328 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000329#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000330 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000331#else
bellard6a00d602005-11-21 23:25:50 +0000332 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000333#endif
bellard6a00d602005-11-21 23:25:50 +0000334 }
bellard0824d6f2003-06-24 13:42:40 +0000335 va_end(ap);
336 abort();
337}
Andi Kleen18894652009-07-02 09:34:17 +0200338
339static void set_proc_name(const char *s)
340{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700341#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200342 char name[16];
343 if (!s)
344 return;
345 name[sizeof(name) - 1] = 0;
346 strncpy(name, s, sizeof(name));
347 /* Could rewrite argv[0] too, but that's a bit more complicated.
348 This simple way is enough for `top'. */
349 prctl(PR_SET_NAME, name);
350#endif
351}
aliguoridf751fa2008-12-04 20:19:35 +0000352
353/***************/
354/* ballooning */
355
356static QEMUBalloonEvent *qemu_balloon_event;
357void *qemu_balloon_event_opaque;
358
359void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
360{
361 qemu_balloon_event = func;
362 qemu_balloon_event_opaque = opaque;
363}
364
Anthony Liguoric227f092009-10-01 16:12:16 -0500365void qemu_balloon(ram_addr_t target)
aliguoridf751fa2008-12-04 20:19:35 +0000366{
367 if (qemu_balloon_event)
368 qemu_balloon_event(qemu_balloon_event_opaque, target);
369}
370
Anthony Liguoric227f092009-10-01 16:12:16 -0500371ram_addr_t qemu_balloon_status(void)
aliguoridf751fa2008-12-04 20:19:35 +0000372{
373 if (qemu_balloon_event)
374 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
375 return 0;
376}
bellard0824d6f2003-06-24 13:42:40 +0000377
bellard8a7ddc32004-03-31 19:00:16 +0000378/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000379/* keyboard/mouse */
380
381static QEMUPutKBDEvent *qemu_put_kbd_event;
382static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000383static QEMUPutMouseEntry *qemu_put_mouse_event_head;
384static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000385
386void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
387{
388 qemu_put_kbd_event_opaque = opaque;
389 qemu_put_kbd_event = func;
390}
391
ths455204e2007-01-05 16:42:13 +0000392QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
393 void *opaque, int absolute,
394 const char *name)
bellard63066f42004-06-03 18:45:02 +0000395{
ths455204e2007-01-05 16:42:13 +0000396 QEMUPutMouseEntry *s, *cursor;
397
398 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000399
400 s->qemu_put_mouse_event = func;
401 s->qemu_put_mouse_event_opaque = opaque;
402 s->qemu_put_mouse_event_absolute = absolute;
403 s->qemu_put_mouse_event_name = qemu_strdup(name);
404 s->next = NULL;
405
406 if (!qemu_put_mouse_event_head) {
407 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
408 return s;
409 }
410
411 cursor = qemu_put_mouse_event_head;
412 while (cursor->next != NULL)
413 cursor = cursor->next;
414
415 cursor->next = s;
416 qemu_put_mouse_event_current = s;
417
418 return s;
419}
420
421void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
422{
423 QEMUPutMouseEntry *prev = NULL, *cursor;
424
425 if (!qemu_put_mouse_event_head || entry == NULL)
426 return;
427
428 cursor = qemu_put_mouse_event_head;
429 while (cursor != NULL && cursor != entry) {
430 prev = cursor;
431 cursor = cursor->next;
432 }
433
434 if (cursor == NULL) // does not exist or list empty
435 return;
436 else if (prev == NULL) { // entry is head
437 qemu_put_mouse_event_head = cursor->next;
438 if (qemu_put_mouse_event_current == entry)
439 qemu_put_mouse_event_current = cursor->next;
440 qemu_free(entry->qemu_put_mouse_event_name);
441 qemu_free(entry);
442 return;
443 }
444
445 prev->next = entry->next;
446
447 if (qemu_put_mouse_event_current == entry)
448 qemu_put_mouse_event_current = prev;
449
450 qemu_free(entry->qemu_put_mouse_event_name);
451 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000452}
453
454void kbd_put_keycode(int keycode)
455{
456 if (qemu_put_kbd_event) {
457 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
458 }
459}
460
461void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
462{
ths455204e2007-01-05 16:42:13 +0000463 QEMUPutMouseEvent *mouse_event;
464 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000465 int width;
ths455204e2007-01-05 16:42:13 +0000466
467 if (!qemu_put_mouse_event_current) {
468 return;
469 }
470
471 mouse_event =
472 qemu_put_mouse_event_current->qemu_put_mouse_event;
473 mouse_event_opaque =
474 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
475
476 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000477 if (graphic_rotate) {
478 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
479 width = 0x7fff;
480 else
aurel32b94ed572008-03-10 19:34:27 +0000481 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000482 mouse_event(mouse_event_opaque,
483 width - dy, dx, dz, buttons_state);
484 } else
485 mouse_event(mouse_event_opaque,
486 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000487 }
488}
489
bellard09b26c52006-04-12 21:09:08 +0000490int kbd_mouse_is_absolute(void)
491{
ths455204e2007-01-05 16:42:13 +0000492 if (!qemu_put_mouse_event_current)
493 return 0;
494
495 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
496}
497
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200498static void info_mice_iter(QObject *data, void *opaque)
ths455204e2007-01-05 16:42:13 +0000499{
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200500 QDict *mouse;
501 Monitor *mon = opaque;
ths455204e2007-01-05 16:42:13 +0000502
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200503 mouse = qobject_to_qdict(data);
504 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s\n",
505 (qdict_get_bool(mouse, "current") ? '*' : ' '),
506 qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name"));
507}
508
509void do_info_mice_print(Monitor *mon, const QObject *data)
510{
511 QList *mice_list;
512
513 mice_list = qobject_to_qlist(data);
514 if (qlist_empty(mice_list)) {
aliguori376253e2009-03-05 23:01:23 +0000515 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000516 return;
517 }
518
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200519 qlist_iter(mice_list, info_mice_iter, mon);
520}
521
522/**
523 * do_info_mice(): Show VM mice information
524 *
525 * Each mouse is represented by a QDict, the returned QObject is a QList of
526 * all mice.
527 *
528 * The mouse QDict contains the following:
529 *
530 * - "name": mouse's name
531 * - "index": mouse's index
532 * - "current": true if this mouse is receiving events, false otherwise
533 *
534 * Example:
535 *
536 * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false },
537 * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true } ]
538 */
539void do_info_mice(Monitor *mon, QObject **ret_data)
540{
541 QEMUPutMouseEntry *cursor;
542 QList *mice_list;
543 int index = 0;
544
545 mice_list = qlist_new();
546
547 if (!qemu_put_mouse_event_head) {
548 goto out;
549 }
550
ths455204e2007-01-05 16:42:13 +0000551 cursor = qemu_put_mouse_event_head;
552 while (cursor != NULL) {
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200553 QObject *obj;
554 obj = qobject_from_jsonf("{ 'name': %s, 'index': %d, 'current': %i }",
555 cursor->qemu_put_mouse_event_name,
556 index, cursor == qemu_put_mouse_event_current);
557 qlist_append_obj(mice_list, obj);
ths455204e2007-01-05 16:42:13 +0000558 index++;
559 cursor = cursor->next;
560 }
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200561
562out:
563 *ret_data = QOBJECT(mice_list);
ths455204e2007-01-05 16:42:13 +0000564}
565
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300566void do_mouse_set(Monitor *mon, const QDict *qdict)
ths455204e2007-01-05 16:42:13 +0000567{
568 QEMUPutMouseEntry *cursor;
569 int i = 0;
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300570 int index = qdict_get_int(qdict, "index");
ths455204e2007-01-05 16:42:13 +0000571
572 if (!qemu_put_mouse_event_head) {
aliguori376253e2009-03-05 23:01:23 +0000573 monitor_printf(mon, "No mouse devices connected\n");
ths455204e2007-01-05 16:42:13 +0000574 return;
575 }
576
577 cursor = qemu_put_mouse_event_head;
578 while (cursor != NULL && index != i) {
579 i++;
580 cursor = cursor->next;
581 }
582
583 if (cursor != NULL)
584 qemu_put_mouse_event_current = cursor;
585 else
aliguori376253e2009-03-05 23:01:23 +0000586 monitor_printf(mon, "Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000587}
588
bellard87858c82003-06-27 12:01:39 +0000589/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000590uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000591{
592 union {
593 uint64_t ll;
594 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200595#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000596 uint32_t high, low;
597#else
598 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000599#endif
bellard87858c82003-06-27 12:01:39 +0000600 } l;
601 } u, res;
602 uint64_t rl, rh;
603
604 u.ll = a;
605 rl = (uint64_t)u.l.low * (uint64_t)b;
606 rh = (uint64_t)u.l.high * (uint64_t)b;
607 rh += (rl >> 32);
608 res.l.high = rh / c;
609 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
610 return res.ll;
611}
612
bellard1dce7c32006-07-13 23:20:22 +0000613/***********************************************************/
614/* real time host monotonic timer */
615
Jan Kiszka21d5d122009-09-15 13:36:04 +0200616static int64_t get_clock_realtime(void)
617{
618 struct timeval tv;
619
620 gettimeofday(&tv, NULL);
621 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
622}
623
bellard1dce7c32006-07-13 23:20:22 +0000624#ifdef WIN32
625
626static int64_t clock_freq;
627
628static void init_get_clock(void)
629{
bellarda8e5ac32006-07-14 09:36:13 +0000630 LARGE_INTEGER freq;
631 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000632 ret = QueryPerformanceFrequency(&freq);
633 if (ret == 0) {
634 fprintf(stderr, "Could not calibrate ticks\n");
635 exit(1);
636 }
637 clock_freq = freq.QuadPart;
638}
639
640static int64_t get_clock(void)
641{
642 LARGE_INTEGER ti;
643 QueryPerformanceCounter(&ti);
Anthony Liguori274dfed2009-09-11 10:28:26 -0500644 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
bellard1dce7c32006-07-13 23:20:22 +0000645}
646
647#else
648
649static int use_rt_clock;
650
651static void init_get_clock(void)
652{
653 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000654#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100655 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000656 {
657 struct timespec ts;
658 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
659 use_rt_clock = 1;
660 }
661 }
662#endif
663}
664
665static int64_t get_clock(void)
666{
blueswir1c5e97232009-03-07 20:06:23 +0000667#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100668 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000669 if (use_rt_clock) {
670 struct timespec ts;
671 clock_gettime(CLOCK_MONOTONIC, &ts);
672 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000673 } else
bellard1dce7c32006-07-13 23:20:22 +0000674#endif
675 {
676 /* XXX: using gettimeofday leads to problems if the date
677 changes, so it should be avoided. */
Jan Kiszka21d5d122009-09-15 13:36:04 +0200678 return get_clock_realtime();
bellard1dce7c32006-07-13 23:20:22 +0000679 }
680}
bellard1dce7c32006-07-13 23:20:22 +0000681#endif
682
pbrook2e70f6e2008-06-29 01:03:05 +0000683/* Return the virtual CPU time, based on the instruction counter. */
684static int64_t cpu_get_icount(void)
685{
686 int64_t icount;
687 CPUState *env = cpu_single_env;;
688 icount = qemu_icount;
689 if (env) {
690 if (!can_do_io(env))
691 fprintf(stderr, "Bad clock read\n");
692 icount -= (env->icount_decr.u16.low + env->icount_extra);
693 }
694 return qemu_icount_bias + (icount << icount_time_shift);
695}
696
bellard1dce7c32006-07-13 23:20:22 +0000697/***********************************************************/
698/* guest cycle counter */
699
Juan Quintela6f68e332009-09-10 03:04:27 +0200700typedef struct TimersState {
701 int64_t cpu_ticks_prev;
702 int64_t cpu_ticks_offset;
703 int64_t cpu_clock_offset;
704 int32_t cpu_ticks_enabled;
Anthony Liguori274dfed2009-09-11 10:28:26 -0500705 int64_t dummy;
Juan Quintela6f68e332009-09-10 03:04:27 +0200706} TimersState;
707
708TimersState timers_state;
bellard1dce7c32006-07-13 23:20:22 +0000709
710/* return the host CPU cycle counter and handle stop/restart */
711int64_t cpu_get_ticks(void)
712{
pbrook2e70f6e2008-06-29 01:03:05 +0000713 if (use_icount) {
714 return cpu_get_icount();
715 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200716 if (!timers_state.cpu_ticks_enabled) {
717 return timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000718 } else {
719 int64_t ticks;
720 ticks = cpu_get_real_ticks();
Juan Quintela6f68e332009-09-10 03:04:27 +0200721 if (timers_state.cpu_ticks_prev > ticks) {
bellard1dce7c32006-07-13 23:20:22 +0000722 /* Note: non increasing ticks may happen if the host uses
723 software suspend */
Juan Quintela6f68e332009-09-10 03:04:27 +0200724 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
bellard1dce7c32006-07-13 23:20:22 +0000725 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200726 timers_state.cpu_ticks_prev = ticks;
727 return ticks + timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000728 }
729}
730
731/* return the host CPU monotonic timer and handle stop/restart */
732static int64_t cpu_get_clock(void)
733{
734 int64_t ti;
Juan Quintela6f68e332009-09-10 03:04:27 +0200735 if (!timers_state.cpu_ticks_enabled) {
736 return timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000737 } else {
738 ti = get_clock();
Juan Quintela6f68e332009-09-10 03:04:27 +0200739 return ti + timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000740 }
741}
742
743/* enable cpu_get_ticks() */
744void cpu_enable_ticks(void)
745{
Juan Quintela6f68e332009-09-10 03:04:27 +0200746 if (!timers_state.cpu_ticks_enabled) {
747 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
748 timers_state.cpu_clock_offset -= get_clock();
749 timers_state.cpu_ticks_enabled = 1;
bellard1dce7c32006-07-13 23:20:22 +0000750 }
751}
752
753/* disable cpu_get_ticks() : the clock is stopped. You must not call
754 cpu_get_ticks() after that. */
755void cpu_disable_ticks(void)
756{
Juan Quintela6f68e332009-09-10 03:04:27 +0200757 if (timers_state.cpu_ticks_enabled) {
758 timers_state.cpu_ticks_offset = cpu_get_ticks();
759 timers_state.cpu_clock_offset = cpu_get_clock();
760 timers_state.cpu_ticks_enabled = 0;
bellard1dce7c32006-07-13 23:20:22 +0000761 }
762}
763
764/***********************************************************/
765/* timers */
ths5fafdf22007-09-16 21:08:06 +0000766
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200767#define QEMU_CLOCK_REALTIME 0
768#define QEMU_CLOCK_VIRTUAL 1
Jan Kiszka21d5d122009-09-15 13:36:04 +0200769#define QEMU_CLOCK_HOST 2
bellard8a7ddc32004-03-31 19:00:16 +0000770
771struct QEMUClock {
772 int type;
773 /* XXX: add frequency */
774};
775
776struct QEMUTimer {
777 QEMUClock *clock;
778 int64_t expire_time;
779 QEMUTimerCB *cb;
780 void *opaque;
781 struct QEMUTimer *next;
782};
783
thsc8994012007-08-19 21:56:03 +0000784struct qemu_alarm_timer {
785 char const *name;
thsefe75412007-08-24 01:36:32 +0000786 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000787
788 int (*start)(struct qemu_alarm_timer *t);
789 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000790 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000791 void *priv;
792};
793
thsefe75412007-08-24 01:36:32 +0000794#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000795#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000796
797static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
798{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200799 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000800}
801
802static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
803{
804 if (!alarm_has_dynticks(t))
805 return;
806
807 t->rearm(t);
808}
809
810/* TODO: MIN_TIMER_REARM_US should be optimized */
811#define MIN_TIMER_REARM_US 250
812
thsc8994012007-08-19 21:56:03 +0000813static struct qemu_alarm_timer *alarm_timer;
814
815#ifdef _WIN32
816
817struct qemu_alarm_win32 {
818 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000819 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000820} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000821
822static int win32_start_timer(struct qemu_alarm_timer *t);
823static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000824static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000825
826#else
827
828static int unix_start_timer(struct qemu_alarm_timer *t);
829static void unix_stop_timer(struct qemu_alarm_timer *t);
830
ths231c6582007-08-26 17:29:15 +0000831#ifdef __linux__
832
thsefe75412007-08-24 01:36:32 +0000833static int dynticks_start_timer(struct qemu_alarm_timer *t);
834static void dynticks_stop_timer(struct qemu_alarm_timer *t);
835static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
836
thsc40ec5a2007-08-19 22:09:40 +0000837static int hpet_start_timer(struct qemu_alarm_timer *t);
838static void hpet_stop_timer(struct qemu_alarm_timer *t);
839
thsc8994012007-08-19 21:56:03 +0000840static int rtc_start_timer(struct qemu_alarm_timer *t);
841static void rtc_stop_timer(struct qemu_alarm_timer *t);
842
thsefe75412007-08-24 01:36:32 +0000843#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000844
845#endif /* _WIN32 */
846
pbrook2e70f6e2008-06-29 01:03:05 +0000847/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000848 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000849 When the guest is idle real and virtual time will be aligned in
850 the IO wait loop. */
Anthony Liguori274dfed2009-09-11 10:28:26 -0500851#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
pbrook2e70f6e2008-06-29 01:03:05 +0000852
853static void icount_adjust(void)
854{
855 int64_t cur_time;
856 int64_t cur_icount;
857 int64_t delta;
858 static int64_t last_delta;
859 /* If the VM is not running, then do nothing. */
860 if (!vm_running)
861 return;
862
863 cur_time = cpu_get_clock();
864 cur_icount = qemu_get_clock(vm_clock);
865 delta = cur_icount - cur_time;
866 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
867 if (delta > 0
868 && last_delta + ICOUNT_WOBBLE < delta * 2
869 && icount_time_shift > 0) {
870 /* The guest is getting too far ahead. Slow time down. */
871 icount_time_shift--;
872 }
873 if (delta < 0
874 && last_delta - ICOUNT_WOBBLE > delta * 2
875 && icount_time_shift < MAX_ICOUNT_SHIFT) {
876 /* The guest is getting too far behind. Speed time up. */
877 icount_time_shift++;
878 }
879 last_delta = delta;
880 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
881}
882
883static void icount_adjust_rt(void * opaque)
884{
885 qemu_mod_timer(icount_rt_timer,
886 qemu_get_clock(rt_clock) + 1000);
887 icount_adjust();
888}
889
890static void icount_adjust_vm(void * opaque)
891{
892 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500893 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000894 icount_adjust();
895}
896
897static void init_icount_adjust(void)
898{
899 /* Have both realtime and virtual time triggers for speed adjustment.
900 The realtime trigger catches emulated time passing too slowly,
901 the virtual time trigger catches emulated time passing too fast.
902 Realtime triggers occur even when idle, so use them less frequently
903 than VM triggers. */
904 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
905 qemu_mod_timer(icount_rt_timer,
906 qemu_get_clock(rt_clock) + 1000);
907 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
908 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500909 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000910}
911
thsc8994012007-08-19 21:56:03 +0000912static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000913#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000914#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000915 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
916 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000917 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000918 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000919 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000920 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000921#endif
thsefe75412007-08-24 01:36:32 +0000922 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000923#else
thsefe75412007-08-24 01:36:32 +0000924 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
925 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
926 {"win32", 0, win32_start_timer,
927 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000928#endif
929 {NULL, }
930};
931
blueswir13f47aa82008-03-09 06:59:01 +0000932static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000933{
934 int i;
935
936 printf("Available alarm timers, in order of precedence:\n");
937 for (i = 0; alarm_timers[i].name; i++)
938 printf("%s\n", alarm_timers[i].name);
939}
940
941static void configure_alarms(char const *opt)
942{
943 int i;
944 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000945 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000946 char *arg;
947 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000948 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000949
aurel323adda042008-03-09 23:43:49 +0000950 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000951 show_available_alarms();
952 exit(0);
953 }
954
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200955 arg = qemu_strdup(opt);
thsf3dcfad2007-08-24 01:26:02 +0000956
957 /* Reorder the array */
958 name = strtok(arg, ",");
959 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000960 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000961 if (!strcmp(alarm_timers[i].name, name))
962 break;
963 }
964
965 if (i == count) {
966 fprintf(stderr, "Unknown clock %s\n", name);
967 goto next;
968 }
969
970 if (i < cur)
971 /* Ignore */
972 goto next;
973
974 /* Swap */
975 tmp = alarm_timers[i];
976 alarm_timers[i] = alarm_timers[cur];
977 alarm_timers[cur] = tmp;
978
979 cur++;
980next:
981 name = strtok(NULL, ",");
982 }
983
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200984 qemu_free(arg);
thsf3dcfad2007-08-24 01:26:02 +0000985
986 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000987 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000988 for (i = cur; i < count; i++)
989 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000990 } else {
991 show_available_alarms();
992 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000993 }
thsf3dcfad2007-08-24 01:26:02 +0000994}
995
Jan Kiszka21d5d122009-09-15 13:36:04 +0200996#define QEMU_NUM_CLOCKS 3
997
bellard8a7ddc32004-03-31 19:00:16 +0000998QEMUClock *rt_clock;
999QEMUClock *vm_clock;
Jan Kiszka21d5d122009-09-15 13:36:04 +02001000QEMUClock *host_clock;
bellard8a7ddc32004-03-31 19:00:16 +00001001
Jan Kiszka21d5d122009-09-15 13:36:04 +02001002static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
bellard8a7ddc32004-03-31 19:00:16 +00001003
pbrook9596ebb2007-11-18 01:44:38 +00001004static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001005{
1006 QEMUClock *clock;
1007 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +00001008 clock->type = type;
1009 return clock;
1010}
1011
1012QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1013{
1014 QEMUTimer *ts;
1015
1016 ts = qemu_mallocz(sizeof(QEMUTimer));
1017 ts->clock = clock;
1018 ts->cb = cb;
1019 ts->opaque = opaque;
1020 return ts;
1021}
1022
1023void qemu_free_timer(QEMUTimer *ts)
1024{
1025 qemu_free(ts);
1026}
1027
1028/* stop a timer, but do not dealloc it */
1029void qemu_del_timer(QEMUTimer *ts)
1030{
1031 QEMUTimer **pt, *t;
1032
1033 /* NOTE: this code must be signal safe because
1034 qemu_timer_expired() can be called from a signal. */
1035 pt = &active_timers[ts->clock->type];
1036 for(;;) {
1037 t = *pt;
1038 if (!t)
1039 break;
1040 if (t == ts) {
1041 *pt = t->next;
1042 break;
1043 }
1044 pt = &t->next;
1045 }
1046}
1047
1048/* modify the current timer so that it will be fired when current_time
1049 >= expire_time. The corresponding callback will be called. */
1050void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1051{
1052 QEMUTimer **pt, *t;
1053
1054 qemu_del_timer(ts);
1055
1056 /* add the timer in the sorted list */
1057 /* NOTE: this code must be signal safe because
1058 qemu_timer_expired() can be called from a signal. */
1059 pt = &active_timers[ts->clock->type];
1060 for(;;) {
1061 t = *pt;
1062 if (!t)
1063 break;
ths5fafdf22007-09-16 21:08:06 +00001064 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001065 break;
1066 pt = &t->next;
1067 }
1068 ts->expire_time = expire_time;
1069 ts->next = *pt;
1070 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001071
1072 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001073 if (pt == &active_timers[ts->clock->type]) {
1074 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1075 qemu_rearm_alarm_timer(alarm_timer);
1076 }
1077 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +00001078 if (use_icount)
1079 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +00001080 }
bellard8a7ddc32004-03-31 19:00:16 +00001081}
1082
1083int qemu_timer_pending(QEMUTimer *ts)
1084{
1085 QEMUTimer *t;
1086 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1087 if (t == ts)
1088 return 1;
1089 }
1090 return 0;
1091}
1092
Stefano Stabellini2430ffe2009-08-03 10:56:01 +01001093int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001094{
1095 if (!timer_head)
1096 return 0;
1097 return (timer_head->expire_time <= current_time);
1098}
1099
1100static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1101{
1102 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001103
bellard8a7ddc32004-03-31 19:00:16 +00001104 for(;;) {
1105 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001106 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001107 break;
1108 /* remove timer from the list before calling the callback */
1109 *ptimer_head = ts->next;
1110 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001111
bellard8a7ddc32004-03-31 19:00:16 +00001112 /* run the callback (the timer list can be modified) */
1113 ts->cb(ts->opaque);
1114 }
1115}
1116
1117int64_t qemu_get_clock(QEMUClock *clock)
1118{
1119 switch(clock->type) {
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001120 case QEMU_CLOCK_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001121 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001122 default:
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001123 case QEMU_CLOCK_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001124 if (use_icount) {
1125 return cpu_get_icount();
1126 } else {
1127 return cpu_get_clock();
1128 }
Jan Kiszka21d5d122009-09-15 13:36:04 +02001129 case QEMU_CLOCK_HOST:
1130 return get_clock_realtime();
bellard8a7ddc32004-03-31 19:00:16 +00001131 }
1132}
1133
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001134static void init_clocks(void)
bellard1dce7c32006-07-13 23:20:22 +00001135{
1136 init_get_clock();
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001137 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
1138 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001139 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Jan Kiszka68752042009-09-15 13:36:04 +02001140
1141 rtc_clock = host_clock;
bellard1dce7c32006-07-13 23:20:22 +00001142}
1143
bellard8a7ddc32004-03-31 19:00:16 +00001144/* save a timer */
1145void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1146{
1147 uint64_t expire_time;
1148
1149 if (qemu_timer_pending(ts)) {
1150 expire_time = ts->expire_time;
1151 } else {
1152 expire_time = -1;
1153 }
1154 qemu_put_be64(f, expire_time);
1155}
1156
1157void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1158{
1159 uint64_t expire_time;
1160
1161 expire_time = qemu_get_be64(f);
1162 if (expire_time != -1) {
1163 qemu_mod_timer(ts, expire_time);
1164 } else {
1165 qemu_del_timer(ts);
1166 }
1167}
1168
Juan Quintela2faf58c2009-09-10 03:04:28 +02001169static const VMStateDescription vmstate_timers = {
1170 .name = "timer",
1171 .version_id = 2,
1172 .minimum_version_id = 1,
1173 .minimum_version_id_old = 1,
1174 .fields = (VMStateField []) {
1175 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Anthony Liguori274dfed2009-09-11 10:28:26 -05001176 VMSTATE_INT64(dummy, TimersState),
Juan Quintela2faf58c2009-09-10 03:04:28 +02001177 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
1178 VMSTATE_END_OF_LIST()
bellardc88676f2006-08-06 13:36:11 +00001179 }
Juan Quintela2faf58c2009-09-10 03:04:28 +02001180};
bellard8a7ddc32004-03-31 19:00:16 +00001181
aliguori50317c72009-04-24 18:03:29 +00001182static void qemu_event_increment(void);
1183
bellard67b915a2004-03-31 23:37:16 +00001184#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001185static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1186 DWORD_PTR dwUser, DWORD_PTR dw1,
1187 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001188#else
bellard8a7ddc32004-03-31 19:00:16 +00001189static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001190#endif
bellard8a7ddc32004-03-31 19:00:16 +00001191{
bellard02ba45c2004-06-25 14:46:23 +00001192#if 0
1193#define DISP_FREQ 1000
1194 {
1195 static int64_t delta_min = INT64_MAX;
1196 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1197 static int count;
1198 ti = qemu_get_clock(vm_clock);
1199 if (last_clock != 0) {
1200 delta = ti - last_clock;
1201 if (delta < delta_min)
1202 delta_min = delta;
1203 if (delta > delta_max)
1204 delta_max = delta;
1205 delta_cum += delta;
1206 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001207 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001208 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
1209 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
1210 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
1211 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
bellard02ba45c2004-06-25 14:46:23 +00001212 count = 0;
1213 delta_min = INT64_MAX;
1214 delta_max = 0;
1215 delta_cum = 0;
1216 }
1217 }
1218 last_clock = ti;
1219 }
1220#endif
thsefe75412007-08-24 01:36:32 +00001221 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001222 (!use_icount &&
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001223 qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
pbrook2e70f6e2008-06-29 01:03:05 +00001224 qemu_get_clock(vm_clock))) ||
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001225 qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
Jan Kiszka21d5d122009-09-15 13:36:04 +02001226 qemu_get_clock(rt_clock)) ||
1227 qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
1228 qemu_get_clock(host_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001229 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001230 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001231
aliguorid6dc3d42009-04-24 18:04:07 +00001232#ifndef CONFIG_IOTHREAD
1233 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001234 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001235 cpu_exit(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001236 }
aliguorid6dc3d42009-04-24 18:04:07 +00001237#endif
aliguori43b96852009-04-24 18:03:33 +00001238 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001239 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001240 }
1241}
1242
pbrook2e70f6e2008-06-29 01:03:05 +00001243static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001244{
Jan Kiszka21d5d122009-09-15 13:36:04 +02001245 /* To avoid problems with overflow limit this to 2^32. */
1246 int64_t delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001247
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001248 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
1249 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001250 qemu_get_clock(vm_clock);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001251 }
1252 if (active_timers[QEMU_CLOCK_HOST]) {
1253 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
1254 qemu_get_clock(host_clock);
1255 if (hdelta < delta)
1256 delta = hdelta;
thsefe75412007-08-24 01:36:32 +00001257 }
1258
pbrook2e70f6e2008-06-29 01:03:05 +00001259 if (delta < 0)
1260 delta = 0;
thsefe75412007-08-24 01:36:32 +00001261
pbrook2e70f6e2008-06-29 01:03:05 +00001262 return delta;
1263}
1264
Jan Kiszkaf64382b2009-09-15 13:36:04 +02001265#if defined(__linux__)
pbrook2e70f6e2008-06-29 01:03:05 +00001266static uint64_t qemu_next_deadline_dyntick(void)
1267{
1268 int64_t delta;
1269 int64_t rtdelta;
1270
1271 if (use_icount)
1272 delta = INT32_MAX;
1273 else
1274 delta = (qemu_next_deadline() + 999) / 1000;
1275
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001276 if (active_timers[QEMU_CLOCK_REALTIME]) {
1277 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001278 qemu_get_clock(rt_clock))*1000;
1279 if (rtdelta < delta)
1280 delta = rtdelta;
1281 }
1282
1283 if (delta < MIN_TIMER_REARM_US)
1284 delta = MIN_TIMER_REARM_US;
1285
1286 return delta;
thsefe75412007-08-24 01:36:32 +00001287}
blueswir18632fb92008-09-14 13:59:34 +00001288#endif
thsefe75412007-08-24 01:36:32 +00001289
bellardfd872592004-05-12 19:11:15 +00001290#ifndef _WIN32
1291
aliguori7183b4b2008-11-05 20:40:18 +00001292/* Sets a specific flag */
1293static int fcntl_setfl(int fd, int flag)
1294{
1295 int flags;
1296
1297 flags = fcntl(fd, F_GETFL);
1298 if (flags == -1)
1299 return -errno;
1300
1301 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1302 return -errno;
1303
1304 return 0;
1305}
1306
bellard829309c2004-05-20 13:20:12 +00001307#if defined(__linux__)
1308
bellardfd872592004-05-12 19:11:15 +00001309#define RTC_FREQ 1024
1310
aurel32de9a95f2008-11-11 13:41:01 +00001311static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001312{
thsc8994012007-08-19 21:56:03 +00001313 struct sigaction act;
1314
1315 /* timer signal */
1316 sigfillset(&act.sa_mask);
1317 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001318 act.sa_handler = host_alarm_handler;
1319
1320 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001321 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001322 fcntl(fd, F_SETOWN, getpid());
1323}
1324
thsc40ec5a2007-08-19 22:09:40 +00001325static int hpet_start_timer(struct qemu_alarm_timer *t)
1326{
1327 struct hpet_info info;
1328 int r, fd;
1329
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001330 fd = qemu_open("/dev/hpet", O_RDONLY);
thsc40ec5a2007-08-19 22:09:40 +00001331 if (fd < 0)
1332 return -1;
1333
1334 /* Set frequency */
1335 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1336 if (r < 0) {
1337 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1338 "error, but for better emulation accuracy type:\n"
1339 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1340 goto fail;
1341 }
1342
1343 /* Check capabilities */
1344 r = ioctl(fd, HPET_INFO, &info);
1345 if (r < 0)
1346 goto fail;
1347
1348 /* Enable periodic mode */
1349 r = ioctl(fd, HPET_EPI, 0);
1350 if (info.hi_flags && (r < 0))
1351 goto fail;
1352
1353 /* Enable interrupt */
1354 r = ioctl(fd, HPET_IE_ON, 0);
1355 if (r < 0)
1356 goto fail;
1357
1358 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001359 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001360
1361 return 0;
1362fail:
1363 close(fd);
1364 return -1;
1365}
1366
1367static void hpet_stop_timer(struct qemu_alarm_timer *t)
1368{
pbrookfcdc2122007-08-23 20:22:22 +00001369 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001370
1371 close(fd);
1372}
1373
thsc8994012007-08-19 21:56:03 +00001374static int rtc_start_timer(struct qemu_alarm_timer *t)
1375{
1376 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001377 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001378
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001379 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001380 if (rtc_fd < 0)
1381 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001382 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1383 if (current_rtc_freq != RTC_FREQ &&
1384 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001385 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1386 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1387 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1388 goto fail;
1389 }
1390 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1391 fail:
1392 close(rtc_fd);
1393 return -1;
1394 }
thsc8994012007-08-19 21:56:03 +00001395
1396 enable_sigio_timer(rtc_fd);
1397
pbrookfcdc2122007-08-23 20:22:22 +00001398 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001399
bellardfd872592004-05-12 19:11:15 +00001400 return 0;
1401}
1402
thsc8994012007-08-19 21:56:03 +00001403static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001404{
pbrookfcdc2122007-08-23 20:22:22 +00001405 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001406
1407 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001408}
1409
thsefe75412007-08-24 01:36:32 +00001410static int dynticks_start_timer(struct qemu_alarm_timer *t)
1411{
1412 struct sigevent ev;
1413 timer_t host_timer;
1414 struct sigaction act;
1415
1416 sigfillset(&act.sa_mask);
1417 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001418 act.sa_handler = host_alarm_handler;
1419
1420 sigaction(SIGALRM, &act, NULL);
1421
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001422 /*
1423 * Initialize ev struct to 0 to avoid valgrind complaining
1424 * about uninitialized data in timer_create call
1425 */
1426 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001427 ev.sigev_value.sival_int = 0;
1428 ev.sigev_notify = SIGEV_SIGNAL;
1429 ev.sigev_signo = SIGALRM;
1430
1431 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1432 perror("timer_create");
1433
1434 /* disable dynticks */
1435 fprintf(stderr, "Dynamic Ticks disabled\n");
1436
1437 return -1;
1438 }
1439
blueswir10399bfe2008-11-16 11:37:18 +00001440 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001441
1442 return 0;
1443}
1444
1445static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1446{
blueswir10399bfe2008-11-16 11:37:18 +00001447 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001448
1449 timer_delete(host_timer);
1450}
1451
1452static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1453{
blueswir10399bfe2008-11-16 11:37:18 +00001454 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001455 struct itimerspec timeout;
1456 int64_t nearest_delta_us = INT64_MAX;
1457 int64_t current_us;
1458
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001459 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001460 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1461 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001462 return;
thsefe75412007-08-24 01:36:32 +00001463
pbrook2e70f6e2008-06-29 01:03:05 +00001464 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001465
1466 /* check whether a timer is already running */
1467 if (timer_gettime(host_timer, &timeout)) {
1468 perror("gettime");
1469 fprintf(stderr, "Internal timer error: aborting\n");
1470 exit(1);
1471 }
1472 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1473 if (current_us && current_us <= nearest_delta_us)
1474 return;
1475
1476 timeout.it_interval.tv_sec = 0;
1477 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1478 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1479 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1480 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1481 perror("settime");
1482 fprintf(stderr, "Internal timer error: aborting\n");
1483 exit(1);
1484 }
1485}
1486
ths70744b32007-08-26 17:31:30 +00001487#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001488
thsc8994012007-08-19 21:56:03 +00001489static int unix_start_timer(struct qemu_alarm_timer *t)
1490{
1491 struct sigaction act;
1492 struct itimerval itv;
1493 int err;
1494
1495 /* timer signal */
1496 sigfillset(&act.sa_mask);
1497 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001498 act.sa_handler = host_alarm_handler;
1499
1500 sigaction(SIGALRM, &act, NULL);
1501
1502 itv.it_interval.tv_sec = 0;
1503 /* for i386 kernel 2.6 to get 1 ms */
1504 itv.it_interval.tv_usec = 999;
1505 itv.it_value.tv_sec = 0;
1506 itv.it_value.tv_usec = 10 * 1000;
1507
1508 err = setitimer(ITIMER_REAL, &itv, NULL);
1509 if (err)
1510 return -1;
1511
1512 return 0;
1513}
1514
1515static void unix_stop_timer(struct qemu_alarm_timer *t)
1516{
1517 struct itimerval itv;
1518
1519 memset(&itv, 0, sizeof(itv));
1520 setitimer(ITIMER_REAL, &itv, NULL);
1521}
1522
bellard829309c2004-05-20 13:20:12 +00001523#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001524
aliguorif49e58d2008-11-05 21:22:34 +00001525
thsc8994012007-08-19 21:56:03 +00001526#ifdef _WIN32
1527
1528static int win32_start_timer(struct qemu_alarm_timer *t)
1529{
1530 TIMECAPS tc;
1531 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001532 UINT flags;
thsc8994012007-08-19 21:56:03 +00001533
thsc8994012007-08-19 21:56:03 +00001534 memset(&tc, 0, sizeof(tc));
1535 timeGetDevCaps(&tc, sizeof(tc));
1536
1537 if (data->period < tc.wPeriodMin)
1538 data->period = tc.wPeriodMin;
1539
1540 timeBeginPeriod(data->period);
1541
thsefe75412007-08-24 01:36:32 +00001542 flags = TIME_CALLBACK_FUNCTION;
1543 if (alarm_has_dynticks(t))
1544 flags |= TIME_ONESHOT;
1545 else
1546 flags |= TIME_PERIODIC;
1547
thsc8994012007-08-19 21:56:03 +00001548 data->timerId = timeSetEvent(1, // interval (ms)
1549 data->period, // resolution
1550 host_alarm_handler, // function
1551 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001552 flags);
thsc8994012007-08-19 21:56:03 +00001553
1554 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001555 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
malc705e83f2009-09-27 14:30:33 +04001556 GetLastError());
thsc8994012007-08-19 21:56:03 +00001557 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001558 return -1;
1559 }
1560
thsc8994012007-08-19 21:56:03 +00001561 return 0;
1562}
1563
1564static void win32_stop_timer(struct qemu_alarm_timer *t)
1565{
1566 struct qemu_alarm_win32 *data = t->priv;
1567
1568 timeKillEvent(data->timerId);
1569 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001570}
1571
thsefe75412007-08-24 01:36:32 +00001572static void win32_rearm_timer(struct qemu_alarm_timer *t)
1573{
1574 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001575
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001576 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001577 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1578 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001579 return;
thsefe75412007-08-24 01:36:32 +00001580
thsefe75412007-08-24 01:36:32 +00001581 timeKillEvent(data->timerId);
1582
1583 data->timerId = timeSetEvent(1,
1584 data->period,
1585 host_alarm_handler,
1586 (DWORD)t,
1587 TIME_ONESHOT | TIME_PERIODIC);
1588
1589 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001590 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
malc705e83f2009-09-27 14:30:33 +04001591 GetLastError());
thsefe75412007-08-24 01:36:32 +00001592
1593 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001594 exit(1);
1595 }
1596}
1597
thsc8994012007-08-19 21:56:03 +00001598#endif /* _WIN32 */
1599
aliguori7183b4b2008-11-05 20:40:18 +00001600static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001601{
blueswir1223f0d72008-09-30 18:12:18 +00001602 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001603 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001604
thsc8994012007-08-19 21:56:03 +00001605 for (i = 0; alarm_timers[i].name; i++) {
1606 t = &alarm_timers[i];
1607
thsc8994012007-08-19 21:56:03 +00001608 err = t->start(t);
1609 if (!err)
1610 break;
bellard67b915a2004-03-31 23:37:16 +00001611 }
bellardfd872592004-05-12 19:11:15 +00001612
thsc8994012007-08-19 21:56:03 +00001613 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001614 err = -ENOENT;
1615 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001616 }
thsc8994012007-08-19 21:56:03 +00001617
1618 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001619
aliguori6abfbd72008-11-05 20:49:37 +00001620 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001621
1622fail:
aliguori7183b4b2008-11-05 20:40:18 +00001623 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001624}
1625
pbrook9596ebb2007-11-18 01:44:38 +00001626static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001627{
thsc8994012007-08-19 21:56:03 +00001628 alarm_timer->stop(alarm_timer);
1629 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001630}
1631
bellardc4b1fcc2004-03-14 21:44:30 +00001632/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001633/* host time/date access */
1634void qemu_get_timedate(struct tm *tm, int offset)
1635{
1636 time_t ti;
1637 struct tm *ret;
1638
1639 time(&ti);
1640 ti += offset;
1641 if (rtc_date_offset == -1) {
1642 if (rtc_utc)
1643 ret = gmtime(&ti);
1644 else
1645 ret = localtime(&ti);
1646 } else {
1647 ti -= rtc_date_offset;
1648 ret = gmtime(&ti);
1649 }
1650
1651 memcpy(tm, ret, sizeof(struct tm));
1652}
1653
1654int qemu_timedate_diff(struct tm *tm)
1655{
1656 time_t seconds;
1657
1658 if (rtc_date_offset == -1)
1659 if (rtc_utc)
1660 seconds = mktimegm(tm);
1661 else
1662 seconds = mktime(tm);
1663 else
1664 seconds = mktimegm(tm) + rtc_date_offset;
1665
1666 return seconds - time(NULL);
1667}
1668
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001669static void configure_rtc_date_offset(const char *startdate, int legacy)
1670{
1671 time_t rtc_start_date;
1672 struct tm tm;
1673
1674 if (!strcmp(startdate, "now") && legacy) {
1675 rtc_date_offset = -1;
1676 } else {
1677 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
1678 &tm.tm_year,
1679 &tm.tm_mon,
1680 &tm.tm_mday,
1681 &tm.tm_hour,
1682 &tm.tm_min,
1683 &tm.tm_sec) == 6) {
1684 /* OK */
1685 } else if (sscanf(startdate, "%d-%d-%d",
1686 &tm.tm_year,
1687 &tm.tm_mon,
1688 &tm.tm_mday) == 3) {
1689 tm.tm_hour = 0;
1690 tm.tm_min = 0;
1691 tm.tm_sec = 0;
1692 } else {
1693 goto date_fail;
1694 }
1695 tm.tm_year -= 1900;
1696 tm.tm_mon--;
1697 rtc_start_date = mktimegm(&tm);
1698 if (rtc_start_date == -1) {
1699 date_fail:
1700 fprintf(stderr, "Invalid date format. Valid formats are:\n"
1701 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1702 exit(1);
1703 }
1704 rtc_date_offset = time(NULL) - rtc_start_date;
1705 }
1706}
1707
1708static void configure_rtc(QemuOpts *opts)
1709{
1710 const char *value;
1711
1712 value = qemu_opt_get(opts, "base");
1713 if (value) {
1714 if (!strcmp(value, "utc")) {
1715 rtc_utc = 1;
1716 } else if (!strcmp(value, "localtime")) {
1717 rtc_utc = 0;
1718 } else {
1719 configure_rtc_date_offset(value, 0);
1720 }
1721 }
Jan Kiszka68752042009-09-15 13:36:04 +02001722 value = qemu_opt_get(opts, "clock");
1723 if (value) {
1724 if (!strcmp(value, "host")) {
1725 rtc_clock = host_clock;
1726 } else if (!strcmp(value, "vm")) {
1727 rtc_clock = vm_clock;
1728 } else {
1729 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1730 exit(1);
1731 }
1732 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001733#ifdef CONFIG_TARGET_I386
1734 value = qemu_opt_get(opts, "driftfix");
1735 if (value) {
1736 if (!strcmp(buf, "slew")) {
1737 rtc_td_hack = 1;
1738 } else if (!strcmp(buf, "none")) {
1739 rtc_td_hack = 0;
1740 } else {
1741 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1742 exit(1);
1743 }
1744 }
1745#endif
1746}
1747
bellardfd1dff42006-02-01 21:29:26 +00001748#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001749static void socket_cleanup(void)
1750{
1751 WSACleanup();
1752}
bellard82c643f2004-07-14 17:28:13 +00001753
bellardfd1dff42006-02-01 21:29:26 +00001754static int socket_init(void)
1755{
1756 WSADATA Data;
1757 int ret, err;
1758
1759 ret = WSAStartup(MAKEWORD(2,2), &Data);
1760 if (ret != 0) {
1761 err = WSAGetLastError();
1762 fprintf(stderr, "WSAStartup: %d\n", err);
1763 return -1;
1764 }
1765 atexit(socket_cleanup);
1766 return 0;
1767}
aurel3264b7b732008-05-05 10:05:31 +00001768#endif
1769
balrog1ae26a12008-09-28 23:19:47 +00001770/***********************************************************/
1771/* Bluetooth support */
1772static int nb_hcis;
1773static int cur_hci;
1774static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001775
balrog1ae26a12008-09-28 23:19:47 +00001776static struct bt_vlan_s {
1777 struct bt_scatternet_s net;
1778 int id;
1779 struct bt_vlan_s *next;
1780} *first_bt_vlan;
1781
1782/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001783static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001784{
1785 struct bt_vlan_s **pvlan, *vlan;
1786 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1787 if (vlan->id == id)
1788 return &vlan->net;
1789 }
1790 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1791 vlan->id = id;
1792 pvlan = &first_bt_vlan;
1793 while (*pvlan != NULL)
1794 pvlan = &(*pvlan)->next;
1795 *pvlan = vlan;
1796 return &vlan->net;
1797}
1798
1799static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1800{
1801}
1802
1803static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1804{
1805 return -ENOTSUP;
1806}
1807
1808static struct HCIInfo null_hci = {
1809 .cmd_send = null_hci_send,
1810 .sco_send = null_hci_send,
1811 .acl_send = null_hci_send,
1812 .bdaddr_set = null_hci_addr_set,
1813};
1814
1815struct HCIInfo *qemu_next_hci(void)
1816{
1817 if (cur_hci == nb_hcis)
1818 return &null_hci;
1819
1820 return hci_table[cur_hci++];
1821}
1822
balrogdc72ac12008-11-09 00:04:26 +00001823static struct HCIInfo *hci_init(const char *str)
1824{
1825 char *endp;
1826 struct bt_scatternet_s *vlan = 0;
1827
1828 if (!strcmp(str, "null"))
1829 /* null */
1830 return &null_hci;
1831 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1832 /* host[:hciN] */
1833 return bt_host_hci(str[4] ? str + 5 : "hci0");
1834 else if (!strncmp(str, "hci", 3)) {
1835 /* hci[,vlan=n] */
1836 if (str[3]) {
1837 if (!strncmp(str + 3, ",vlan=", 6)) {
1838 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1839 if (*endp)
1840 vlan = 0;
1841 }
1842 } else
1843 vlan = qemu_find_bt_vlan(0);
1844 if (vlan)
1845 return bt_new_hci(vlan);
1846 }
1847
1848 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1849
1850 return 0;
1851}
1852
1853static int bt_hci_parse(const char *str)
1854{
1855 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -05001856 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +00001857
1858 if (nb_hcis >= MAX_NICS) {
1859 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1860 return -1;
1861 }
1862
1863 hci = hci_init(str);
1864 if (!hci)
1865 return -1;
1866
1867 bdaddr.b[0] = 0x52;
1868 bdaddr.b[1] = 0x54;
1869 bdaddr.b[2] = 0x00;
1870 bdaddr.b[3] = 0x12;
1871 bdaddr.b[4] = 0x34;
1872 bdaddr.b[5] = 0x56 + nb_hcis;
1873 hci->bdaddr_set(hci, bdaddr.b);
1874
1875 hci_table[nb_hcis++] = hci;
1876
1877 return 0;
1878}
1879
1880static void bt_vhci_add(int vlan_id)
1881{
1882 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1883
1884 if (!vlan->slave)
1885 fprintf(stderr, "qemu: warning: adding a VHCI to "
1886 "an empty scatternet %i\n", vlan_id);
1887
1888 bt_vhci_init(bt_new_hci(vlan));
1889}
1890
1891static struct bt_device_s *bt_device_add(const char *opt)
1892{
1893 struct bt_scatternet_s *vlan;
1894 int vlan_id = 0;
1895 char *endp = strstr(opt, ",vlan=");
1896 int len = (endp ? endp - opt : strlen(opt)) + 1;
1897 char devname[10];
1898
1899 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1900
1901 if (endp) {
1902 vlan_id = strtol(endp + 6, &endp, 0);
1903 if (*endp) {
1904 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1905 return 0;
1906 }
1907 }
1908
1909 vlan = qemu_find_bt_vlan(vlan_id);
1910
1911 if (!vlan->slave)
1912 fprintf(stderr, "qemu: warning: adding a slave device to "
1913 "an empty scatternet %i\n", vlan_id);
1914
1915 if (!strcmp(devname, "keyboard"))
1916 return bt_keyboard_init(vlan);
1917
1918 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1919 return 0;
1920}
1921
1922static int bt_parse(const char *opt)
1923{
1924 const char *endp, *p;
1925 int vlan;
1926
1927 if (strstart(opt, "hci", &endp)) {
1928 if (!*endp || *endp == ',') {
1929 if (*endp)
1930 if (!strstart(endp, ",vlan=", 0))
1931 opt = endp + 1;
1932
1933 return bt_hci_parse(opt);
1934 }
1935 } else if (strstart(opt, "vhci", &endp)) {
1936 if (!*endp || *endp == ',') {
1937 if (*endp) {
1938 if (strstart(endp, ",vlan=", &p)) {
1939 vlan = strtol(p, (char **) &endp, 0);
1940 if (*endp) {
1941 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1942 return 1;
1943 }
1944 } else {
1945 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1946 return 1;
1947 }
1948 } else
1949 vlan = 0;
1950
1951 bt_vhci_add(vlan);
1952 return 0;
1953 }
1954 } else if (strstart(opt, "device:", &endp))
1955 return !bt_device_add(endp);
1956
1957 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1958 return 1;
1959}
1960
balrog1ae26a12008-09-28 23:19:47 +00001961/***********************************************************/
1962/* QEMU Block devices */
1963
balrog609497a2008-01-14 02:56:53 +00001964#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001965#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001966#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001967#define PFLASH_ALIAS "if=pflash"
1968#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001969#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001970
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001971QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001972{
1973 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001974 char optstr[1024];
1975 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +00001976
thse4bcb142007-12-02 04:51:10 +00001977 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001978 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001979 va_end(ap);
1980
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001981 opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001982 if (!opts) {
1983 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1984 __FUNCTION__, optstr);
1985 return NULL;
1986 }
1987 if (file)
1988 qemu_opt_set(opts, "file", file);
1989 return opts;
aliguorib01b1112009-02-11 15:20:20 +00001990}
1991
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001992DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001993{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001994 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001995
1996 /* seek interface, bus and unit */
1997
Blue Swirl72cf2d42009-09-12 07:36:22 +00001998 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001999 if (dinfo->type == type &&
2000 dinfo->bus == bus &&
2001 dinfo->unit == unit)
2002 return dinfo;
2003 }
thse4bcb142007-12-02 04:51:10 +00002004
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002005 return NULL;
thse4bcb142007-12-02 04:51:10 +00002006}
2007
Gerd Hoffmann2e810b32009-07-31 12:25:38 +02002008DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002009{
2010 DriveInfo *dinfo;
2011
Blue Swirl72cf2d42009-09-12 07:36:22 +00002012 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002013 if (strcmp(id, dinfo->id))
2014 continue;
2015 return dinfo;
2016 }
2017 return NULL;
2018}
2019
thsf60d39b2007-12-17 03:55:57 +00002020int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002021{
2022 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002023 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00002024
2025 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002026 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002027 if(dinfo->type == type &&
2028 dinfo->bus > max_bus)
2029 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +00002030 }
2031 return max_bus;
2032}
2033
aliguorifa879c62009-01-07 17:32:33 +00002034const char *drive_get_serial(BlockDriverState *bdrv)
2035{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002036 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +00002037
Blue Swirl72cf2d42009-09-12 07:36:22 +00002038 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002039 if (dinfo->bdrv == bdrv)
2040 return dinfo->serial;
2041 }
aliguorifa879c62009-01-07 17:32:33 +00002042
2043 return "\0";
2044}
2045
Kevin Wolff7850092009-11-27 13:25:36 +01002046BlockInterfaceErrorAction drive_get_on_error(
2047 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +00002048{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002049 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +00002050
Blue Swirl72cf2d42009-09-12 07:36:22 +00002051 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002052 if (dinfo->bdrv == bdrv)
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002053 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002054 }
aliguori428c5702009-01-21 18:59:04 +00002055
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002056 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00002057}
2058
aurel32a1620fa2008-04-29 05:58:01 +00002059static void bdrv_format_print(void *opaque, const char *name)
2060{
2061 fprintf(stderr, " %s", name);
2062}
2063
Gerd Hoffmann56a14932009-09-25 21:42:46 +02002064void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +00002065{
Gerd Hoffmann56a14932009-09-25 21:42:46 +02002066 qemu_opts_del(dinfo->opts);
2067 bdrv_delete(dinfo->bdrv);
2068 QTAILQ_REMOVE(&drives, dinfo, next);
2069 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +00002070}
2071
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002072static int parse_block_error_action(const char *buf, int is_read)
2073{
2074 if (!strcmp(buf, "ignore")) {
2075 return BLOCK_ERR_IGNORE;
2076 } else if (!is_read && !strcmp(buf, "enospc")) {
2077 return BLOCK_ERR_STOP_ENOSPC;
2078 } else if (!strcmp(buf, "stop")) {
2079 return BLOCK_ERR_STOP_ANY;
2080 } else if (!strcmp(buf, "report")) {
2081 return BLOCK_ERR_REPORT;
2082 } else {
2083 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
2084 buf, is_read ? "read" : "write");
2085 return -1;
2086 }
2087}
2088
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002089DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002090 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +00002091{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002092 const char *buf;
2093 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +00002094 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002095 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +00002096 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002097 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002098 enum { MEDIA_DISK, MEDIA_CDROM } media;
2099 int bus_id, unit_id;
2100 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +00002101 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002102 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002103 int max_devs;
2104 int index;
balrog33f00272007-12-24 14:33:24 +00002105 int cache;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002106 int aio = 0;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002107 int ro = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002108 int bdrv_flags;
2109 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002110 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002111 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002112 int snapshot = 0;
2113
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002114 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +00002115
thse4bcb142007-12-02 04:51:10 +00002116 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02002117 cache = 1;
thse4bcb142007-12-02 04:51:10 +00002118
Gerd Hoffmann4d007812009-08-31 14:23:57 +02002119 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002120 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002121 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002122 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002123 } else {
thsf60d39b2007-12-17 03:55:57 +00002124 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002125 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002126 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002127 }
2128 media = MEDIA_DISK;
2129
2130 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002131 bus_id = qemu_opt_get_number(opts, "bus", 0);
2132 unit_id = qemu_opt_get_number(opts, "unit", -1);
2133 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +00002134
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002135 cyls = qemu_opt_get_number(opts, "cyls", 0);
2136 heads = qemu_opt_get_number(opts, "heads", 0);
2137 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +00002138
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002139 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +02002140 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +00002141
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002142 file = qemu_opt_get(opts, "file");
2143 serial = qemu_opt_get(opts, "serial");
2144
2145 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +00002146 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002147 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002148 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002149 max_devs = MAX_IDE_DEVS;
2150 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002151 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002152 max_devs = MAX_SCSI_DEVS;
2153 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002154 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002155 max_devs = 0;
2156 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002157 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002158 max_devs = 0;
2159 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002160 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002161 max_devs = 0;
2162 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002163 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002164 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002165 } else if (!strcmp(buf, "virtio")) {
2166 type = IF_VIRTIO;
2167 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002168 } else if (!strcmp(buf, "xen")) {
2169 type = IF_XEN;
2170 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +02002171 } else if (!strcmp(buf, "none")) {
2172 type = IF_NONE;
2173 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002174 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002175 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002176 return NULL;
thse4bcb142007-12-02 04:51:10 +00002177 }
2178 }
2179
thse4bcb142007-12-02 04:51:10 +00002180 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +00002181 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002182 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002183 return NULL;
thse4bcb142007-12-02 04:51:10 +00002184 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002185 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002186 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002187 return NULL;
thse4bcb142007-12-02 04:51:10 +00002188 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002189 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002190 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002191 return NULL;
thse4bcb142007-12-02 04:51:10 +00002192 }
2193 }
2194
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002195 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002196 if (!cyls) {
2197 fprintf(stderr,
2198 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002199 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002200 return NULL;
thse4bcb142007-12-02 04:51:10 +00002201 }
2202 if (!strcmp(buf, "none"))
2203 translation = BIOS_ATA_TRANSLATION_NONE;
2204 else if (!strcmp(buf, "lba"))
2205 translation = BIOS_ATA_TRANSLATION_LBA;
2206 else if (!strcmp(buf, "auto"))
2207 translation = BIOS_ATA_TRANSLATION_AUTO;
2208 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002209 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002210 return NULL;
thse4bcb142007-12-02 04:51:10 +00002211 }
2212 }
2213
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002214 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002215 if (!strcmp(buf, "disk")) {
2216 media = MEDIA_DISK;
2217 } else if (!strcmp(buf, "cdrom")) {
2218 if (cyls || secs || heads) {
2219 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002220 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002221 return NULL;
thse4bcb142007-12-02 04:51:10 +00002222 }
2223 media = MEDIA_CDROM;
2224 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002225 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002226 return NULL;
thse4bcb142007-12-02 04:51:10 +00002227 }
2228 }
2229
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002230 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00002231 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002232 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002233 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002234 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002235 else if (!strcmp(buf, "writeback"))
2236 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002237 else {
2238 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002239 return NULL;
balrog33f00272007-12-24 14:33:24 +00002240 }
2241 }
2242
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002243#ifdef CONFIG_LINUX_AIO
2244 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
2245 if (!strcmp(buf, "threads"))
2246 aio = 0;
2247 else if (!strcmp(buf, "native"))
2248 aio = 1;
2249 else {
2250 fprintf(stderr, "qemu: invalid aio option\n");
2251 return NULL;
2252 }
2253 }
2254#endif
2255
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002256 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00002257 if (strcmp(buf, "?") == 0) {
2258 fprintf(stderr, "qemu: Supported formats:");
2259 bdrv_iterate_format(bdrv_format_print, NULL);
2260 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002261 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00002262 }
Markus Armbrustereb852012009-10-27 18:41:44 +01002263 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +00002264 if (!drv) {
2265 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002266 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00002267 }
2268 }
2269
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002270 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002271 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00002272 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002273 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002274 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002275 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002276
2277 on_write_error = parse_block_error_action(buf, 0);
2278 if (on_write_error < 0) {
2279 return NULL;
2280 }
2281 }
2282
2283 on_read_error = BLOCK_ERR_REPORT;
2284 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolff35d68f2009-11-27 13:25:39 +01002285 if (type != IF_IDE && type != IF_VIRTIO) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002286 fprintf(stderr, "rerror is no supported by this format\n");
2287 return NULL;
2288 }
2289
2290 on_read_error = parse_block_error_action(buf, 1);
2291 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002292 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002293 }
2294 }
2295
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002296 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002297 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002298 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002299 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002300 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002301 }
2302
thse4bcb142007-12-02 04:51:10 +00002303 /* compute bus and unit according index */
2304
2305 if (index != -1) {
2306 if (bus_id != 0 || unit_id != -1) {
2307 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002308 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002309 return NULL;
thse4bcb142007-12-02 04:51:10 +00002310 }
2311 if (max_devs == 0)
2312 {
2313 unit_id = index;
2314 bus_id = 0;
2315 } else {
2316 unit_id = index % max_devs;
2317 bus_id = index / max_devs;
2318 }
2319 }
2320
2321 /* if user doesn't specify a unit_id,
2322 * try to find the first free
2323 */
2324
2325 if (unit_id == -1) {
2326 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002327 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002328 unit_id++;
2329 if (max_devs && unit_id >= max_devs) {
2330 unit_id -= max_devs;
2331 bus_id++;
2332 }
2333 }
2334 }
2335
2336 /* check unit id */
2337
2338 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002339 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2340 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002341 return NULL;
thse4bcb142007-12-02 04:51:10 +00002342 }
2343
2344 /*
2345 * ignore multiple definitions
2346 */
2347
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002348 if (drive_get(type, bus_id, unit_id) != NULL) {
2349 *fatal_error = 0;
2350 return NULL;
2351 }
thse4bcb142007-12-02 04:51:10 +00002352
2353 /* init */
2354
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002355 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02002356 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002357 dinfo->id = qemu_strdup(buf);
2358 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002359 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002360 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002361 if (type == IF_IDE || type == IF_SCSI)
2362 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2363 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002364 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002365 devname, bus_id, mediastr, unit_id);
2366 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002367 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002368 devname, mediastr, unit_id);
2369 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002370 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002371 dinfo->devaddr = devaddr;
2372 dinfo->type = type;
2373 dinfo->bus = bus_id;
2374 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002375 dinfo->on_read_error = on_read_error;
2376 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002377 dinfo->opts = opts;
2378 if (serial)
2379 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00002380 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00002381
thsf60d39b2007-12-17 03:55:57 +00002382 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002383 case IF_IDE:
2384 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002385 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00002386 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00002387 switch(media) {
2388 case MEDIA_DISK:
2389 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002390 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2391 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00002392 }
2393 break;
2394 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002395 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00002396 break;
2397 }
2398 break;
2399 case IF_SD:
2400 /* FIXME: This isn't really a floppy, but it's a reasonable
2401 approximation. */
2402 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002403 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00002404 break;
2405 case IF_PFLASH:
2406 case IF_MTD:
2407 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02002408 case IF_VIRTIO:
2409 /* add virtio block device */
2410 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2411 qemu_opt_set(opts, "driver", "virtio-blk-pci");
2412 qemu_opt_set(opts, "drive", dinfo->id);
2413 if (devaddr)
2414 qemu_opt_set(opts, "addr", devaddr);
2415 break;
Paul Brookaae94602009-05-14 22:35:06 +01002416 case IF_COUNT:
2417 abort();
thse4bcb142007-12-02 04:51:10 +00002418 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002419 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002420 *fatal_error = 0;
2421 return NULL;
2422 }
balrog33f00272007-12-24 14:33:24 +00002423 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002424 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002425 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002426 cache = 2; /* always use write-back with snapshot */
2427 }
2428 if (cache == 0) /* no caching */
2429 bdrv_flags |= BDRV_O_NOCACHE;
2430 else if (cache == 2) /* write-back */
2431 bdrv_flags |= BDRV_O_CACHE_WB;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002432
2433 if (aio == 1) {
2434 bdrv_flags |= BDRV_O_NATIVE_AIO;
2435 } else {
2436 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
2437 }
2438
Naphtali Sprei59f26892009-10-26 16:25:16 +02002439 if (ro == 1) {
2440 if (type == IF_IDE) {
2441 fprintf(stderr, "qemu: readonly flag not supported for drive with ide interface\n");
2442 return NULL;
2443 }
2444 (void)bdrv_set_read_only(dinfo->bdrv, 1);
2445 }
2446
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002447 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05002448 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
2449 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002450 return NULL;
thse4bcb142007-12-02 04:51:10 +00002451 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002452
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002453 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00002454 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002455 *fatal_error = 0;
2456 return dinfo;
thse4bcb142007-12-02 04:51:10 +00002457}
2458
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002459static int drive_init_func(QemuOpts *opts, void *opaque)
2460{
2461 QEMUMachine *machine = opaque;
2462 int fatal_error = 0;
2463
2464 if (drive_init(opts, machine, &fatal_error) == NULL) {
2465 if (fatal_error)
2466 return 1;
2467 }
2468 return 0;
2469}
2470
2471static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2472{
2473 if (NULL == qemu_opt_get(opts, "snapshot")) {
2474 qemu_opt_set(opts, "snapshot", "on");
2475 }
2476 return 0;
2477}
2478
Jan Kiszka76e30d02009-07-02 00:19:02 +02002479void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2480{
2481 boot_set_handler = func;
2482 boot_set_opaque = opaque;
2483}
2484
2485int qemu_boot_set(const char *boot_devices)
2486{
2487 if (!boot_set_handler) {
2488 return -EINVAL;
2489 }
2490 return boot_set_handler(boot_set_opaque, boot_devices);
2491}
2492
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002493static int parse_bootdevices(char *devices)
2494{
2495 /* We just do some generic consistency checks */
2496 const char *p;
2497 int bitmap = 0;
2498
2499 for (p = devices; *p != '\0'; p++) {
2500 /* Allowed boot devices are:
2501 * a-b: floppy disk drives
2502 * c-f: IDE disk drives
2503 * g-m: machine implementation dependant drives
2504 * n-p: network devices
2505 * It's up to each machine implementation to check if the given boot
2506 * devices match the actual hardware implementation and firmware
2507 * features.
2508 */
2509 if (*p < 'a' || *p > 'p') {
2510 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2511 exit(1);
2512 }
2513 if (bitmap & (1 << (*p - 'a'))) {
2514 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2515 exit(1);
2516 }
2517 bitmap |= 1 << (*p - 'a');
2518 }
2519 return bitmap;
2520}
2521
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002522static void restore_boot_devices(void *opaque)
2523{
2524 char *standard_boot_devices = opaque;
2525
2526 qemu_boot_set(standard_boot_devices);
2527
2528 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2529 qemu_free(standard_boot_devices);
2530}
2531
aliguori268a3622009-04-21 22:30:27 +00002532static void numa_add(const char *optarg)
2533{
2534 char option[128];
2535 char *endptr;
2536 unsigned long long value, endvalue;
2537 int nodenr;
2538
2539 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2540 if (!strcmp(option, "node")) {
2541 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2542 nodenr = nb_numa_nodes;
2543 } else {
2544 nodenr = strtoull(option, NULL, 10);
2545 }
2546
2547 if (get_param_value(option, 128, "mem", optarg) == 0) {
2548 node_mem[nodenr] = 0;
2549 } else {
2550 value = strtoull(option, &endptr, 0);
2551 switch (*endptr) {
2552 case 0: case 'M': case 'm':
2553 value <<= 20;
2554 break;
2555 case 'G': case 'g':
2556 value <<= 30;
2557 break;
2558 }
2559 node_mem[nodenr] = value;
2560 }
2561 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2562 node_cpumask[nodenr] = 0;
2563 } else {
2564 value = strtoull(option, &endptr, 10);
2565 if (value >= 64) {
2566 value = 63;
2567 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2568 } else {
2569 if (*endptr == '-') {
2570 endvalue = strtoull(endptr+1, &endptr, 10);
2571 if (endvalue >= 63) {
2572 endvalue = 62;
2573 fprintf(stderr,
2574 "only 63 CPUs in NUMA mode supported.\n");
2575 }
2576 value = (1 << (endvalue + 1)) - (1 << value);
2577 } else {
2578 value = 1 << value;
2579 }
2580 }
2581 node_cpumask[nodenr] = value;
2582 }
2583 nb_numa_nodes++;
2584 }
2585 return;
2586}
2587
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002588static void smp_parse(const char *optarg)
2589{
2590 int smp, sockets = 0, threads = 0, cores = 0;
2591 char *endptr;
2592 char option[128];
2593
2594 smp = strtoul(optarg, &endptr, 10);
2595 if (endptr != optarg) {
2596 if (*endptr == ',') {
2597 endptr++;
2598 }
2599 }
2600 if (get_param_value(option, 128, "sockets", endptr) != 0)
2601 sockets = strtoull(option, NULL, 10);
2602 if (get_param_value(option, 128, "cores", endptr) != 0)
2603 cores = strtoull(option, NULL, 10);
2604 if (get_param_value(option, 128, "threads", endptr) != 0)
2605 threads = strtoull(option, NULL, 10);
2606 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
2607 max_cpus = strtoull(option, NULL, 10);
2608
2609 /* compute missing values, prefer sockets over cores over threads */
2610 if (smp == 0 || sockets == 0) {
2611 sockets = sockets > 0 ? sockets : 1;
2612 cores = cores > 0 ? cores : 1;
2613 threads = threads > 0 ? threads : 1;
2614 if (smp == 0) {
2615 smp = cores * threads * sockets;
2616 } else {
2617 sockets = smp / (cores * threads);
2618 }
2619 } else {
2620 if (cores == 0) {
2621 threads = threads > 0 ? threads : 1;
2622 cores = smp / (sockets * threads);
2623 } else {
2624 if (sockets == 0) {
2625 sockets = smp / (cores * threads);
2626 } else {
2627 threads = smp / (cores * sockets);
2628 }
2629 }
2630 }
2631 smp_cpus = smp;
2632 smp_cores = cores > 0 ? cores : 1;
2633 smp_threads = threads > 0 ? threads : 1;
2634 if (max_cpus == 0)
2635 max_cpus = smp_cpus;
2636}
2637
bellard330d0412003-07-26 18:11:40 +00002638/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002639/* USB devices */
2640
aliguoric0f4ce72009-03-05 23:01:01 +00002641static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002642{
2643 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002644 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00002645
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002646 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00002647 return -1;
2648
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01002649 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2650 dev = usbdevice_create(devname);
2651 if (dev)
2652 goto done;
2653
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002654 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00002655 if (strstart(devname, "host:", &p)) {
2656 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +00002657 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2658 dev = usb_bt_init(devname[2] ? hci_init(p) :
2659 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002660 } else {
2661 return -1;
2662 }
pbrook0d92ed32006-05-21 16:30:15 +00002663 if (!dev)
2664 return -1;
2665
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002666done:
bellarda594cfb2005-11-06 16:13:29 +00002667 return 0;
2668}
2669
aliguori1f3870a2008-08-21 19:27:48 +00002670static int usb_device_del(const char *devname)
2671{
2672 int bus_num, addr;
2673 const char *p;
2674
aliguori5d0c5752008-09-14 01:07:41 +00002675 if (strstart(devname, "host:", &p))
2676 return usb_host_device_close(p);
2677
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002678 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00002679 return -1;
2680
2681 p = strchr(devname, '.');
2682 if (!p)
2683 return -1;
2684 bus_num = strtoul(devname, NULL, 0);
2685 addr = strtoul(p + 1, NULL, 0);
2686
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002687 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00002688}
2689
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002690static int usb_parse(const char *cmdline)
2691{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08002692 int r;
2693 r = usb_device_add(cmdline, 0);
2694 if (r < 0) {
2695 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
2696 }
2697 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002698}
2699
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002700void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002701{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08002702 const char *devname = qdict_get_str(qdict, "devname");
2703 if (usb_device_add(devname, 1) < 0) {
2704 qemu_error("could not add USB device '%s'\n", devname);
2705 }
bellarda594cfb2005-11-06 16:13:29 +00002706}
2707
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002708void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002709{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08002710 const char *devname = qdict_get_str(qdict, "devname");
2711 if (usb_device_del(devname) < 0) {
2712 qemu_error("could not delete USB device '%s'\n", devname);
2713 }
bellarda594cfb2005-11-06 16:13:29 +00002714}
2715
bellardf7cce892004-12-08 22:21:25 +00002716/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002717/* PCMCIA/Cardbus */
2718
2719static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002720 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002721 struct pcmcia_socket_entry_s *next;
2722} *pcmcia_sockets = 0;
2723
Paul Brookbc24a222009-05-10 01:44:56 +01002724void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002725{
2726 struct pcmcia_socket_entry_s *entry;
2727
2728 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2729 entry->socket = socket;
2730 entry->next = pcmcia_sockets;
2731 pcmcia_sockets = entry;
2732}
2733
Paul Brookbc24a222009-05-10 01:44:56 +01002734void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002735{
2736 struct pcmcia_socket_entry_s *entry, **ptr;
2737
2738 ptr = &pcmcia_sockets;
2739 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2740 if (entry->socket == socket) {
2741 *ptr = entry->next;
2742 qemu_free(entry);
2743 }
2744}
2745
aliguori376253e2009-03-05 23:01:23 +00002746void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002747{
2748 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002749
balrog201a51f2007-04-30 00:51:09 +00002750 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002751 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002752
2753 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002754 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2755 iter->socket->attached ? iter->socket->card_string :
2756 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002757}
2758
2759/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002760/* register display */
2761
aliguori7b5d76d2009-03-13 15:02:13 +00002762struct DisplayAllocator default_allocator = {
2763 defaultallocator_create_displaysurface,
2764 defaultallocator_resize_displaysurface,
2765 defaultallocator_free_displaysurface
2766};
2767
aliguori3023f332009-01-16 19:04:14 +00002768void register_displaystate(DisplayState *ds)
2769{
2770 DisplayState **s;
2771 s = &display_state;
2772 while (*s != NULL)
2773 s = &(*s)->next;
2774 ds->next = NULL;
2775 *s = ds;
2776}
2777
2778DisplayState *get_displaystate(void)
2779{
2780 return display_state;
2781}
2782
aliguori7b5d76d2009-03-13 15:02:13 +00002783DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da)
2784{
2785 if(ds->allocator == &default_allocator) ds->allocator = da;
2786 return ds->allocator;
2787}
2788
ths2ff89792007-06-21 23:34:19 +00002789/* dumb display */
2790
aliguori8f391ab2009-01-19 16:34:10 +00002791static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002792{
aliguori8f391ab2009-01-19 16:34:10 +00002793 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7b5d76d2009-03-13 15:02:13 +00002794 ds->allocator = &default_allocator;
2795 ds->surface = qemu_create_displaysurface(ds, 640, 480);
aliguori8f391ab2009-01-19 16:34:10 +00002796 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002797}
2798
2799/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002800/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002801
bellardc4b1fcc2004-03-14 21:44:30 +00002802typedef struct IOHandlerRecord {
2803 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002804 IOCanRWHandler *fd_read_poll;
2805 IOHandler *fd_read;
2806 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002807 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002808 void *opaque;
2809 /* temporary data */
2810 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002811 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002812} IOHandlerRecord;
2813
bellard8a7ddc32004-03-31 19:00:16 +00002814static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002815
bellard7c9d8e02005-11-15 22:16:05 +00002816/* XXX: fd_read_poll should be suppressed, but an API change is
2817 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002818int qemu_set_fd_handler2(int fd,
2819 IOCanRWHandler *fd_read_poll,
2820 IOHandler *fd_read,
2821 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002822 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002823{
bellard8a7ddc32004-03-31 19:00:16 +00002824 IOHandlerRecord **pioh, *ioh;
2825
bellard7c9d8e02005-11-15 22:16:05 +00002826 if (!fd_read && !fd_write) {
2827 pioh = &first_io_handler;
2828 for(;;) {
2829 ioh = *pioh;
2830 if (ioh == NULL)
2831 break;
2832 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002833 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002834 break;
2835 }
2836 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002837 }
bellard7c9d8e02005-11-15 22:16:05 +00002838 } else {
2839 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2840 if (ioh->fd == fd)
2841 goto found;
2842 }
2843 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002844 ioh->next = first_io_handler;
2845 first_io_handler = ioh;
2846 found:
2847 ioh->fd = fd;
2848 ioh->fd_read_poll = fd_read_poll;
2849 ioh->fd_read = fd_read;
2850 ioh->fd_write = fd_write;
2851 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002852 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002853 }
bellard7c9d8e02005-11-15 22:16:05 +00002854 return 0;
2855}
2856
ths5fafdf22007-09-16 21:08:06 +00002857int qemu_set_fd_handler(int fd,
2858 IOHandler *fd_read,
2859 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002860 void *opaque)
2861{
2862 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002863}
2864
aliguori56f3a5d2008-10-31 18:07:17 +00002865#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002866/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002867/* Polling handling */
2868
2869typedef struct PollingEntry {
2870 PollingFunc *func;
2871 void *opaque;
2872 struct PollingEntry *next;
2873} PollingEntry;
2874
2875static PollingEntry *first_polling_entry;
2876
2877int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2878{
2879 PollingEntry **ppe, *pe;
2880 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002881 pe->func = func;
2882 pe->opaque = opaque;
2883 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2884 *ppe = pe;
2885 return 0;
2886}
2887
2888void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2889{
2890 PollingEntry **ppe, *pe;
2891 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2892 pe = *ppe;
2893 if (pe->func == func && pe->opaque == opaque) {
2894 *ppe = pe->next;
2895 qemu_free(pe);
2896 break;
2897 }
2898 }
2899}
2900
bellarda18e5242006-06-25 17:18:27 +00002901/***********************************************************/
2902/* Wait objects support */
2903typedef struct WaitObjects {
2904 int num;
2905 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2906 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2907 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2908} WaitObjects;
2909
2910static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002911
bellarda18e5242006-06-25 17:18:27 +00002912int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2913{
2914 WaitObjects *w = &wait_objects;
2915
2916 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2917 return -1;
2918 w->events[w->num] = handle;
2919 w->func[w->num] = func;
2920 w->opaque[w->num] = opaque;
2921 w->num++;
2922 return 0;
2923}
2924
2925void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2926{
2927 int i, found;
2928 WaitObjects *w = &wait_objects;
2929
2930 found = 0;
2931 for (i = 0; i < w->num; i++) {
2932 if (w->events[i] == handle)
2933 found = 1;
2934 if (found) {
2935 w->events[i] = w->events[i + 1];
2936 w->func[i] = w->func[i + 1];
2937 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002938 }
bellarda18e5242006-06-25 17:18:27 +00002939 }
2940 if (found)
2941 w->num--;
2942}
2943#endif
2944
bellard8a7ddc32004-03-31 19:00:16 +00002945/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002946/* ram save/restore */
2947
Juan Quintela94fb0902009-09-10 03:04:23 +02002948#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
aliguori475e4272008-10-06 20:21:51 +00002949#define RAM_SAVE_FLAG_COMPRESS 0x02
2950#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2951#define RAM_SAVE_FLAG_PAGE 0x08
2952#define RAM_SAVE_FLAG_EOS 0x10
2953
2954static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002955{
aliguori475e4272008-10-06 20:21:51 +00002956 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2957 uint32_t *array = (uint32_t *)page;
2958 int i;
ths3b46e622007-09-17 08:09:54 +00002959
aliguori475e4272008-10-06 20:21:51 +00002960 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2961 if (array[i] != val)
2962 return 0;
bellardc88676f2006-08-06 13:36:11 +00002963 }
aliguori475e4272008-10-06 20:21:51 +00002964
2965 return 1;
bellardc88676f2006-08-06 13:36:11 +00002966}
2967
aliguori475e4272008-10-06 20:21:51 +00002968static int ram_save_block(QEMUFile *f)
2969{
Anthony Liguoric227f092009-10-01 16:12:16 -05002970 static ram_addr_t current_addr = 0;
2971 ram_addr_t saved_addr = current_addr;
2972 ram_addr_t addr = 0;
aliguori475e4272008-10-06 20:21:51 +00002973 int found = 0;
2974
pbrook94a6b542009-04-11 17:15:54 +00002975 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002976 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002977 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002978
2979 cpu_physical_memory_reset_dirty(current_addr,
2980 current_addr + TARGET_PAGE_SIZE,
2981 MIGRATION_DIRTY_FLAG);
2982
pbrook5579c7f2009-04-11 14:47:08 +00002983 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002984
pbrook5579c7f2009-04-11 14:47:08 +00002985 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002986 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002987 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00002988 } else {
2989 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00002990 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002991 }
2992
2993 found = 1;
2994 break;
2995 }
2996 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00002997 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00002998 }
2999
3000 return found;
3001}
3002
Jan Kiszka84307932009-12-02 11:29:38 +01003003static uint64_t bytes_transferred;
aliguori475e4272008-10-06 20:21:51 +00003004
Anthony Liguoric227f092009-10-01 16:12:16 -05003005static ram_addr_t ram_save_remaining(void)
aliguori475e4272008-10-06 20:21:51 +00003006{
Anthony Liguoric227f092009-10-01 16:12:16 -05003007 ram_addr_t addr;
3008 ram_addr_t count = 0;
aliguori475e4272008-10-06 20:21:51 +00003009
pbrook94a6b542009-04-11 17:15:54 +00003010 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003011 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3012 count++;
3013 }
3014
3015 return count;
3016}
3017
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003018uint64_t ram_bytes_remaining(void)
3019{
3020 return ram_save_remaining() * TARGET_PAGE_SIZE;
3021}
3022
3023uint64_t ram_bytes_transferred(void)
3024{
3025 return bytes_transferred;
3026}
3027
3028uint64_t ram_bytes_total(void)
3029{
3030 return last_ram_offset;
3031}
3032
Jan Kiszkaf327aa02009-11-30 18:21:21 +01003033static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
aliguori475e4272008-10-06 20:21:51 +00003034{
Anthony Liguoric227f092009-10-01 16:12:16 -05003035 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003036 uint64_t bytes_transferred_last;
3037 double bwidth = 0;
3038 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00003039
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01003040 if (stage < 0) {
3041 cpu_physical_memory_set_dirty_tracking(0);
3042 return 0;
3043 }
3044
Jan Kiszka9fa06382009-05-22 23:51:45 +02003045 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003046 qemu_file_set_error(f);
3047 return 0;
3048 }
3049
aliguori475e4272008-10-06 20:21:51 +00003050 if (stage == 1) {
Jan Kiszka84307932009-12-02 11:29:38 +01003051 bytes_transferred = 0;
3052
aliguori475e4272008-10-06 20:21:51 +00003053 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00003054 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00003055 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3056 cpu_physical_memory_set_dirty(addr);
3057 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02003058
aliguori475e4272008-10-06 20:21:51 +00003059 /* Enable dirty memory tracking */
3060 cpu_physical_memory_set_dirty_tracking(1);
3061
pbrook94a6b542009-04-11 17:15:54 +00003062 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00003063 }
3064
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003065 bytes_transferred_last = bytes_transferred;
3066 bwidth = get_clock();
3067
aliguori475e4272008-10-06 20:21:51 +00003068 while (!qemu_file_rate_limit(f)) {
3069 int ret;
3070
3071 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003072 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00003073 if (ret == 0) /* no more blocks */
3074 break;
3075 }
3076
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003077 bwidth = get_clock() - bwidth;
3078 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
3079
3080 /* if we haven't transferred anything this round, force expected_time to a
3081 * a very high value, but without crashing */
3082 if (bwidth == 0)
3083 bwidth = 0.000001;
3084
aliguori475e4272008-10-06 20:21:51 +00003085 /* try transferring iterative blocks of memory */
aliguori475e4272008-10-06 20:21:51 +00003086 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00003087 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04003088 while (ram_save_block(f) != 0) {
3089 bytes_transferred += TARGET_PAGE_SIZE;
3090 }
aliguori8215e912009-04-05 19:30:55 +00003091 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00003092 }
3093
3094 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3095
Glauber Costaa0a3fd62009-05-28 15:22:57 -04003096 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
3097
3098 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00003099}
3100
aliguori475e4272008-10-06 20:21:51 +00003101static int ram_load(QEMUFile *f, void *opaque, int version_id)
3102{
Anthony Liguoric227f092009-10-01 16:12:16 -05003103 ram_addr_t addr;
aliguori475e4272008-10-06 20:21:51 +00003104 int flags;
3105
aliguori475e4272008-10-06 20:21:51 +00003106 if (version_id != 3)
3107 return -EINVAL;
3108
3109 do {
3110 addr = qemu_get_be64(f);
3111
3112 flags = addr & ~TARGET_PAGE_MASK;
3113 addr &= TARGET_PAGE_MASK;
3114
3115 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00003116 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00003117 return -EINVAL;
3118 }
3119
aliguori475e4272008-10-06 20:21:51 +00003120 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3121 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003122 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
3123#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05003124 if (ch == 0 &&
3125 (!kvm_enabled() || kvm_has_sync_mmu())) {
3126 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05003127 }
Anthony Liguori30868442009-06-17 16:46:12 -05003128#endif
Jan Kiszka9a743e52009-11-30 18:21:21 +01003129 } else if (flags & RAM_SAVE_FLAG_PAGE) {
pbrook5579c7f2009-04-11 14:47:08 +00003130 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
Jan Kiszka9a743e52009-11-30 18:21:21 +01003131 }
3132 if (qemu_file_has_error(f)) {
3133 return -EIO;
3134 }
aliguori475e4272008-10-06 20:21:51 +00003135 } while (!(flags & RAM_SAVE_FLAG_EOS));
3136
bellardc88676f2006-08-06 13:36:11 +00003137 return 0;
3138}
3139
aliguori9e472e12008-10-08 19:50:24 +00003140void qemu_service_io(void)
3141{
aliguorid9f75a42009-04-24 18:03:11 +00003142 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00003143}
3144
bellard8a7ddc32004-03-31 19:00:16 +00003145/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003146/* machine registration */
3147
blueswir1bdaf78e2008-10-04 07:24:27 +00003148static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003149QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003150
3151int qemu_register_machine(QEMUMachine *m)
3152{
3153 QEMUMachine **pm;
3154 pm = &first_machine;
3155 while (*pm != NULL)
3156 pm = &(*pm)->next;
3157 m->next = NULL;
3158 *pm = m;
3159 return 0;
3160}
3161
pbrook9596ebb2007-11-18 01:44:38 +00003162static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003163{
3164 QEMUMachine *m;
3165
3166 for(m = first_machine; m != NULL; m = m->next) {
3167 if (!strcmp(m->name, name))
3168 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01003169 if (m->alias && !strcmp(m->alias, name))
3170 return m;
bellardcc1daa42005-06-05 14:49:17 +00003171 }
3172 return NULL;
3173}
3174
Anthony Liguori0c257432009-05-21 20:41:01 -05003175static QEMUMachine *find_default_machine(void)
3176{
3177 QEMUMachine *m;
3178
3179 for(m = first_machine; m != NULL; m = m->next) {
3180 if (m->is_default) {
3181 return m;
3182 }
3183 }
3184 return NULL;
3185}
3186
bellardcc1daa42005-06-05 14:49:17 +00003187/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003188/* main execution loop */
3189
pbrook9596ebb2007-11-18 01:44:38 +00003190static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003191{
aliguori7d957bd2009-01-15 22:14:11 +00003192 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003193 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003194 DisplayChangeListener *dcl = ds->listeners;
3195
3196 dpy_refresh(ds);
3197
3198 while (dcl != NULL) {
3199 if (dcl->gui_timer_interval &&
3200 dcl->gui_timer_interval < interval)
3201 interval = dcl->gui_timer_interval;
3202 dcl = dcl->next;
3203 }
3204 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003205}
3206
blueswir19043b622009-01-21 19:28:13 +00003207static void nographic_update(void *opaque)
3208{
3209 uint64_t interval = GUI_REFRESH_INTERVAL;
3210
3211 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3212}
3213
bellard0bd48852005-11-11 00:00:47 +00003214struct vm_change_state_entry {
3215 VMChangeStateHandler *cb;
3216 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003217 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00003218};
3219
Blue Swirl72cf2d42009-09-12 07:36:22 +00003220static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00003221
3222VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3223 void *opaque)
3224{
3225 VMChangeStateEntry *e;
3226
3227 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003228
3229 e->cb = cb;
3230 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003231 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00003232 return e;
3233}
3234
3235void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3236{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003237 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00003238 qemu_free (e);
3239}
3240
aliguori9781e042009-01-22 17:15:29 +00003241static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003242{
3243 VMChangeStateEntry *e;
3244
3245 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003246 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003247 }
3248}
3249
aliguorid6dc3d42009-04-24 18:04:07 +00003250static void resume_all_vcpus(void);
3251static void pause_all_vcpus(void);
3252
bellard8a7ddc32004-03-31 19:00:16 +00003253void vm_start(void)
3254{
3255 if (!vm_running) {
3256 cpu_enable_ticks();
3257 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003258 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003259 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003260 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003261 }
3262}
3263
bellardbb0c6722004-06-20 12:37:32 +00003264/* reset/shutdown handler */
3265
3266typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003267 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003268 QEMUResetHandler *func;
3269 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003270} QEMUResetEntry;
3271
Blue Swirl72cf2d42009-09-12 07:36:22 +00003272static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3273 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003274static int reset_requested;
3275static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003276static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003277static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003278static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003279
aurel32cf7a2fe2008-03-18 06:53:05 +00003280int qemu_shutdown_requested(void)
3281{
3282 int r = shutdown_requested;
3283 shutdown_requested = 0;
3284 return r;
3285}
3286
3287int qemu_reset_requested(void)
3288{
3289 int r = reset_requested;
3290 reset_requested = 0;
3291 return r;
3292}
3293
3294int qemu_powerdown_requested(void)
3295{
3296 int r = powerdown_requested;
3297 powerdown_requested = 0;
3298 return r;
3299}
3300
aliguorie5689022009-04-24 18:03:54 +00003301static int qemu_debug_requested(void)
3302{
3303 int r = debug_requested;
3304 debug_requested = 0;
3305 return r;
3306}
3307
aliguori6e29f5d2009-04-24 18:04:02 +00003308static int qemu_vmstop_requested(void)
3309{
3310 int r = vmstop_requested;
3311 vmstop_requested = 0;
3312 return r;
3313}
3314
3315static void do_vm_stop(int reason)
3316{
3317 if (vm_running) {
3318 cpu_disable_ticks();
3319 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003320 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003321 vm_state_notify(0, reason);
3322 }
3323}
3324
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003325void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003326{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003327 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003328
bellardbb0c6722004-06-20 12:37:32 +00003329 re->func = func;
3330 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003331 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003332}
3333
Jan Kiszkadda9b292009-07-02 00:19:02 +02003334void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003335{
3336 QEMUResetEntry *re;
3337
Blue Swirl72cf2d42009-09-12 07:36:22 +00003338 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003339 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003340 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02003341 qemu_free(re);
3342 return;
3343 }
3344 }
3345}
3346
3347void qemu_system_reset(void)
3348{
3349 QEMUResetEntry *re, *nre;
3350
3351 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00003352 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003353 re->func(re->opaque);
3354 }
3355}
3356
3357void qemu_system_reset_request(void)
3358{
bellardd1beab82006-10-02 19:44:22 +00003359 if (no_reboot) {
3360 shutdown_requested = 1;
3361 } else {
3362 reset_requested = 1;
3363 }
aliguorid9f75a42009-04-24 18:03:11 +00003364 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003365}
3366
3367void qemu_system_shutdown_request(void)
3368{
3369 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003370 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003371}
3372
bellard34751872005-07-02 14:31:34 +00003373void qemu_system_powerdown_request(void)
3374{
3375 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003376 qemu_notify_event();
3377}
3378
aliguorid6dc3d42009-04-24 18:04:07 +00003379#ifdef CONFIG_IOTHREAD
3380static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003381{
aliguorid6dc3d42009-04-24 18:04:07 +00003382 vmstop_requested = reason;
3383 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003384}
aliguorid6dc3d42009-04-24 18:04:07 +00003385#endif
bellardbb0c6722004-06-20 12:37:32 +00003386
aliguori50317c72009-04-24 18:03:29 +00003387#ifndef _WIN32
3388static int io_thread_fd = -1;
3389
3390static void qemu_event_increment(void)
3391{
3392 static const char byte = 0;
3393
3394 if (io_thread_fd == -1)
3395 return;
3396
3397 write(io_thread_fd, &byte, sizeof(byte));
3398}
3399
3400static void qemu_event_read(void *opaque)
3401{
3402 int fd = (unsigned long)opaque;
3403 ssize_t len;
3404
3405 /* Drain the notify pipe */
3406 do {
3407 char buffer[512];
3408 len = read(fd, buffer, sizeof(buffer));
3409 } while ((len == -1 && errno == EINTR) || len > 0);
3410}
3411
3412static int qemu_event_init(void)
3413{
3414 int err;
3415 int fds[2];
3416
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003417 err = qemu_pipe(fds);
aliguori50317c72009-04-24 18:03:29 +00003418 if (err == -1)
3419 return -errno;
3420
3421 err = fcntl_setfl(fds[0], O_NONBLOCK);
3422 if (err < 0)
3423 goto fail;
3424
3425 err = fcntl_setfl(fds[1], O_NONBLOCK);
3426 if (err < 0)
3427 goto fail;
3428
3429 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3430 (void *)(unsigned long)fds[0]);
3431
3432 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003433 return 0;
3434
aliguori50317c72009-04-24 18:03:29 +00003435fail:
3436 close(fds[0]);
3437 close(fds[1]);
3438 return err;
3439}
3440#else
3441HANDLE qemu_event_handle;
3442
3443static void dummy_event_handler(void *opaque)
3444{
3445}
3446
3447static int qemu_event_init(void)
3448{
3449 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3450 if (!qemu_event_handle) {
Blue Swirl20889d42009-09-27 20:03:56 +00003451 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
aliguori50317c72009-04-24 18:03:29 +00003452 return -1;
3453 }
3454 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3455 return 0;
3456}
3457
3458static void qemu_event_increment(void)
3459{
malcde1c90c2009-09-27 14:38:18 +04003460 if (!SetEvent(qemu_event_handle)) {
Blue Swirl20889d42009-09-27 20:03:56 +00003461 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
malcde1c90c2009-09-27 14:38:18 +04003462 GetLastError());
3463 exit (1);
3464 }
aliguori50317c72009-04-24 18:03:29 +00003465}
3466#endif
3467
aliguorid6dc3d42009-04-24 18:04:07 +00003468static int cpu_can_run(CPUState *env)
3469{
3470 if (env->stop)
3471 return 0;
3472 if (env->stopped)
3473 return 0;
3474 return 1;
3475}
3476
3477#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003478static int qemu_init_main_loop(void)
3479{
aliguori50317c72009-04-24 18:03:29 +00003480 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003481}
3482
aliguori0bf46a42009-04-24 18:03:41 +00003483void qemu_init_vcpu(void *_env)
3484{
3485 CPUState *env = _env;
3486
3487 if (kvm_enabled())
3488 kvm_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003489 env->nr_cores = smp_cores;
3490 env->nr_threads = smp_threads;
aliguori0bf46a42009-04-24 18:03:41 +00003491 return;
3492}
3493
aliguori8edac962009-04-24 18:03:45 +00003494int qemu_cpu_self(void *env)
3495{
3496 return 1;
3497}
3498
aliguorid6dc3d42009-04-24 18:04:07 +00003499static void resume_all_vcpus(void)
3500{
3501}
3502
3503static void pause_all_vcpus(void)
3504{
3505}
3506
aliguori8edac962009-04-24 18:03:45 +00003507void qemu_cpu_kick(void *env)
3508{
3509 return;
3510}
3511
aliguorid6dc3d42009-04-24 18:04:07 +00003512void qemu_notify_event(void)
3513{
3514 CPUState *env = cpu_single_env;
3515
3516 if (env) {
3517 cpu_exit(env);
Anthony Liguori4a1418e2009-08-10 17:07:24 -05003518 }
aliguorid6dc3d42009-04-24 18:04:07 +00003519}
3520
Glauber Costad549db52009-10-07 16:38:03 -03003521void qemu_mutex_lock_iothread(void) {}
3522void qemu_mutex_unlock_iothread(void) {}
aliguori48708522009-04-24 18:03:49 +00003523
aliguori6e29f5d2009-04-24 18:04:02 +00003524void vm_stop(int reason)
3525{
3526 do_vm_stop(reason);
3527}
3528
aliguorid6dc3d42009-04-24 18:04:07 +00003529#else /* CONFIG_IOTHREAD */
3530
3531#include "qemu-thread.h"
3532
3533QemuMutex qemu_global_mutex;
3534static QemuMutex qemu_fair_mutex;
3535
3536static QemuThread io_thread;
3537
3538static QemuThread *tcg_cpu_thread;
3539static QemuCond *tcg_halt_cond;
3540
3541static int qemu_system_ready;
3542/* cpu creation */
3543static QemuCond qemu_cpu_cond;
3544/* system init */
3545static QemuCond qemu_system_cond;
3546static QemuCond qemu_pause_cond;
3547
3548static void block_io_signals(void);
3549static void unblock_io_signals(void);
3550static int tcg_has_work(void);
3551
3552static int qemu_init_main_loop(void)
3553{
3554 int ret;
3555
3556 ret = qemu_event_init();
3557 if (ret)
3558 return ret;
3559
3560 qemu_cond_init(&qemu_pause_cond);
3561 qemu_mutex_init(&qemu_fair_mutex);
3562 qemu_mutex_init(&qemu_global_mutex);
3563 qemu_mutex_lock(&qemu_global_mutex);
3564
3565 unblock_io_signals();
3566 qemu_thread_self(&io_thread);
3567
3568 return 0;
3569}
3570
3571static void qemu_wait_io_event(CPUState *env)
3572{
3573 while (!tcg_has_work())
3574 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3575
3576 qemu_mutex_unlock(&qemu_global_mutex);
3577
3578 /*
3579 * Users of qemu_global_mutex can be starved, having no chance
3580 * to acquire it since this path will get to it first.
3581 * So use another lock to provide fairness.
3582 */
3583 qemu_mutex_lock(&qemu_fair_mutex);
3584 qemu_mutex_unlock(&qemu_fair_mutex);
3585
3586 qemu_mutex_lock(&qemu_global_mutex);
3587 if (env->stop) {
3588 env->stop = 0;
3589 env->stopped = 1;
3590 qemu_cond_signal(&qemu_pause_cond);
3591 }
3592}
3593
3594static int qemu_cpu_exec(CPUState *env);
3595
3596static void *kvm_cpu_thread_fn(void *arg)
3597{
3598 CPUState *env = arg;
3599
3600 block_io_signals();
3601 qemu_thread_self(env->thread);
Jean-Christophe DUBOIS321c1cb2009-09-02 23:59:04 +02003602 if (kvm_enabled())
3603 kvm_init_vcpu(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003604
3605 /* signal CPU creation */
3606 qemu_mutex_lock(&qemu_global_mutex);
3607 env->created = 1;
3608 qemu_cond_signal(&qemu_cpu_cond);
3609
3610 /* and wait for machine initialization */
3611 while (!qemu_system_ready)
3612 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3613
3614 while (1) {
3615 if (cpu_can_run(env))
3616 qemu_cpu_exec(env);
Anthony Liguori1c3173b2009-09-10 08:45:43 -05003617 qemu_wait_io_event(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003618 }
3619
3620 return NULL;
3621}
3622
3623static void tcg_cpu_exec(void);
3624
3625static void *tcg_cpu_thread_fn(void *arg)
3626{
3627 CPUState *env = arg;
3628
3629 block_io_signals();
3630 qemu_thread_self(env->thread);
3631
3632 /* signal CPU creation */
3633 qemu_mutex_lock(&qemu_global_mutex);
3634 for (env = first_cpu; env != NULL; env = env->next_cpu)
3635 env->created = 1;
3636 qemu_cond_signal(&qemu_cpu_cond);
3637
3638 /* and wait for machine initialization */
3639 while (!qemu_system_ready)
3640 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3641
3642 while (1) {
3643 tcg_cpu_exec();
3644 qemu_wait_io_event(cur_cpu);
3645 }
3646
3647 return NULL;
3648}
3649
3650void qemu_cpu_kick(void *_env)
3651{
3652 CPUState *env = _env;
3653 qemu_cond_broadcast(env->halt_cond);
3654 if (kvm_enabled())
3655 qemu_thread_signal(env->thread, SIGUSR1);
3656}
3657
Glauber Costae5bc2012009-09-28 15:27:44 -03003658int qemu_cpu_self(void *_env)
aliguorid6dc3d42009-04-24 18:04:07 +00003659{
Glauber Costae5bc2012009-09-28 15:27:44 -03003660 CPUState *env = _env;
3661 QemuThread this;
3662
3663 qemu_thread_self(&this);
3664
3665 return qemu_thread_equal(&this, env->thread);
aliguorid6dc3d42009-04-24 18:04:07 +00003666}
3667
3668static void cpu_signal(int sig)
3669{
3670 if (cpu_single_env)
3671 cpu_exit(cpu_single_env);
3672}
3673
3674static void block_io_signals(void)
3675{
3676 sigset_t set;
3677 struct sigaction sigact;
3678
3679 sigemptyset(&set);
3680 sigaddset(&set, SIGUSR2);
3681 sigaddset(&set, SIGIO);
3682 sigaddset(&set, SIGALRM);
3683 pthread_sigmask(SIG_BLOCK, &set, NULL);
3684
3685 sigemptyset(&set);
3686 sigaddset(&set, SIGUSR1);
3687 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3688
3689 memset(&sigact, 0, sizeof(sigact));
3690 sigact.sa_handler = cpu_signal;
3691 sigaction(SIGUSR1, &sigact, NULL);
3692}
3693
3694static void unblock_io_signals(void)
3695{
3696 sigset_t set;
3697
3698 sigemptyset(&set);
3699 sigaddset(&set, SIGUSR2);
3700 sigaddset(&set, SIGIO);
3701 sigaddset(&set, SIGALRM);
3702 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3703
3704 sigemptyset(&set);
3705 sigaddset(&set, SIGUSR1);
3706 pthread_sigmask(SIG_BLOCK, &set, NULL);
3707}
3708
3709static void qemu_signal_lock(unsigned int msecs)
3710{
3711 qemu_mutex_lock(&qemu_fair_mutex);
3712
3713 while (qemu_mutex_trylock(&qemu_global_mutex)) {
3714 qemu_thread_signal(tcg_cpu_thread, SIGUSR1);
3715 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3716 break;
3717 }
3718 qemu_mutex_unlock(&qemu_fair_mutex);
3719}
3720
Glauber Costad549db52009-10-07 16:38:03 -03003721void qemu_mutex_lock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003722{
3723 if (kvm_enabled()) {
3724 qemu_mutex_lock(&qemu_fair_mutex);
3725 qemu_mutex_lock(&qemu_global_mutex);
3726 qemu_mutex_unlock(&qemu_fair_mutex);
3727 } else
3728 qemu_signal_lock(100);
3729}
3730
Glauber Costad549db52009-10-07 16:38:03 -03003731void qemu_mutex_unlock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003732{
3733 qemu_mutex_unlock(&qemu_global_mutex);
3734}
3735
3736static int all_vcpus_paused(void)
3737{
3738 CPUState *penv = first_cpu;
3739
3740 while (penv) {
3741 if (!penv->stopped)
3742 return 0;
3743 penv = (CPUState *)penv->next_cpu;
3744 }
3745
3746 return 1;
3747}
3748
3749static void pause_all_vcpus(void)
3750{
3751 CPUState *penv = first_cpu;
3752
3753 while (penv) {
3754 penv->stop = 1;
3755 qemu_thread_signal(penv->thread, SIGUSR1);
3756 qemu_cpu_kick(penv);
3757 penv = (CPUState *)penv->next_cpu;
3758 }
3759
3760 while (!all_vcpus_paused()) {
3761 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3762 penv = first_cpu;
3763 while (penv) {
3764 qemu_thread_signal(penv->thread, SIGUSR1);
3765 penv = (CPUState *)penv->next_cpu;
3766 }
3767 }
3768}
3769
3770static void resume_all_vcpus(void)
3771{
3772 CPUState *penv = first_cpu;
3773
3774 while (penv) {
3775 penv->stop = 0;
3776 penv->stopped = 0;
3777 qemu_thread_signal(penv->thread, SIGUSR1);
3778 qemu_cpu_kick(penv);
3779 penv = (CPUState *)penv->next_cpu;
3780 }
3781}
3782
3783static void tcg_init_vcpu(void *_env)
3784{
3785 CPUState *env = _env;
3786 /* share a single thread for all cpus with TCG */
3787 if (!tcg_cpu_thread) {
3788 env->thread = qemu_mallocz(sizeof(QemuThread));
3789 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3790 qemu_cond_init(env->halt_cond);
3791 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3792 while (env->created == 0)
3793 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3794 tcg_cpu_thread = env->thread;
3795 tcg_halt_cond = env->halt_cond;
3796 } else {
3797 env->thread = tcg_cpu_thread;
3798 env->halt_cond = tcg_halt_cond;
3799 }
3800}
3801
3802static void kvm_start_vcpu(CPUState *env)
3803{
aliguorid6dc3d42009-04-24 18:04:07 +00003804 env->thread = qemu_mallocz(sizeof(QemuThread));
3805 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3806 qemu_cond_init(env->halt_cond);
3807 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3808 while (env->created == 0)
3809 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3810}
3811
3812void qemu_init_vcpu(void *_env)
3813{
3814 CPUState *env = _env;
3815
3816 if (kvm_enabled())
3817 kvm_start_vcpu(env);
3818 else
3819 tcg_init_vcpu(env);
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003820 env->nr_cores = smp_cores;
3821 env->nr_threads = smp_threads;
aliguorid6dc3d42009-04-24 18:04:07 +00003822}
3823
3824void qemu_notify_event(void)
3825{
3826 qemu_event_increment();
3827}
3828
3829void vm_stop(int reason)
3830{
3831 QemuThread me;
3832 qemu_thread_self(&me);
3833
3834 if (!qemu_thread_equal(&me, &io_thread)) {
3835 qemu_system_vmstop_request(reason);
3836 /*
3837 * FIXME: should not return to device code in case
3838 * vm_stop() has been requested.
3839 */
3840 if (cpu_single_env) {
3841 cpu_exit(cpu_single_env);
3842 cpu_single_env->stop = 1;
3843 }
3844 return;
3845 }
3846 do_vm_stop(reason);
3847}
3848
3849#endif
3850
3851
ths877cf882007-04-18 18:11:47 +00003852#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003853static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003854{
3855 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003856 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003857
bellardf3311102006-04-12 20:21:17 +00003858
3859 /* XXX: need to suppress polling by better using win32 events */
3860 ret = 0;
3861 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3862 ret |= pe->func(pe->opaque);
3863 }
thse6b1e552007-04-18 17:56:02 +00003864 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003865 int err;
3866 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003867
aliguori56f3a5d2008-10-31 18:07:17 +00003868 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003869 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3870 if (w->func[ret - WAIT_OBJECT_0])
3871 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003872
ths5fafdf22007-09-16 21:08:06 +00003873 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003874 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003875
thse6b1e552007-04-18 17:56:02 +00003876 /* Check if event is signaled */
3877 ret2 = WaitForSingleObject(w->events[i], 0);
3878 if(ret2 == WAIT_OBJECT_0) {
3879 if (w->func[i])
3880 w->func[i](w->opaque[i]);
3881 } else if (ret2 == WAIT_TIMEOUT) {
3882 } else {
3883 err = GetLastError();
3884 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003885 }
3886 }
bellarda18e5242006-06-25 17:18:27 +00003887 } else if (ret == WAIT_TIMEOUT) {
3888 } else {
3889 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003890 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003891 }
bellardf3311102006-04-12 20:21:17 +00003892 }
aliguori56f3a5d2008-10-31 18:07:17 +00003893
3894 *timeout = 0;
3895}
3896#else
blueswir169d64512008-12-07 19:30:18 +00003897static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003898{
3899}
bellardfd1dff42006-02-01 21:29:26 +00003900#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003901
3902void main_loop_wait(int timeout)
3903{
3904 IOHandlerRecord *ioh;
3905 fd_set rfds, wfds, xfds;
3906 int ret, nfds;
3907 struct timeval tv;
3908
3909 qemu_bh_update_timeout(&timeout);
3910
3911 host_main_loop_wait(&timeout);
3912
bellardfd1dff42006-02-01 21:29:26 +00003913 /* poll any events */
3914 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003915 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003916 FD_ZERO(&rfds);
3917 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003918 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003919 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003920 if (ioh->deleted)
3921 continue;
bellardfd1dff42006-02-01 21:29:26 +00003922 if (ioh->fd_read &&
3923 (!ioh->fd_read_poll ||
3924 ioh->fd_read_poll(ioh->opaque) != 0)) {
3925 FD_SET(ioh->fd, &rfds);
3926 if (ioh->fd > nfds)
3927 nfds = ioh->fd;
3928 }
3929 if (ioh->fd_write) {
3930 FD_SET(ioh->fd, &wfds);
3931 if (ioh->fd > nfds)
3932 nfds = ioh->fd;
3933 }
3934 }
ths3b46e622007-09-17 08:09:54 +00003935
aliguori56f3a5d2008-10-31 18:07:17 +00003936 tv.tv_sec = timeout / 1000;
3937 tv.tv_usec = (timeout % 1000) * 1000;
3938
Jan Kiszkad918f232009-06-24 14:42:30 +02003939 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3940
aliguori48708522009-04-24 18:03:49 +00003941 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00003942 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00003943 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00003944 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003945 IOHandlerRecord **pioh;
3946
3947 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003948 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003949 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003950 }
ths6ab43fd2007-08-25 01:34:19 +00003951 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003952 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003953 }
3954 }
thscafffd42007-02-28 21:59:44 +00003955
3956 /* remove deleted IO handlers */
3957 pioh = &first_io_handler;
3958 while (*pioh) {
3959 ioh = *pioh;
3960 if (ioh->deleted) {
3961 *pioh = ioh->next;
3962 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003963 } else
thscafffd42007-02-28 21:59:44 +00003964 pioh = &ioh->next;
3965 }
bellardfd1dff42006-02-01 21:29:26 +00003966 }
Jan Kiszkad918f232009-06-24 14:42:30 +02003967
3968 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00003969
aliguori50317c72009-04-24 18:03:29 +00003970 /* rearm timer, if not periodic */
3971 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
3972 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
3973 qemu_rearm_alarm_timer(alarm_timer);
3974 }
3975
aliguori357c6922008-11-25 17:26:09 +00003976 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00003977 if (vm_running) {
3978 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003979 qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL],
3980 qemu_get_clock(vm_clock));
aliguorid6dc3d42009-04-24 18:04:07 +00003981 }
aliguori357c6922008-11-25 17:26:09 +00003982
3983 /* real time timers */
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003984 qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME],
aliguori357c6922008-11-25 17:26:09 +00003985 qemu_get_clock(rt_clock));
3986
Jan Kiszka21d5d122009-09-15 13:36:04 +02003987 qemu_run_timers(&active_timers[QEMU_CLOCK_HOST],
3988 qemu_get_clock(host_clock));
3989
pbrook423f0742007-05-23 00:06:54 +00003990 /* Check bottom-halves last in case any of the earlier events triggered
3991 them. */
3992 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003993
bellard5905b2e2004-08-01 21:53:26 +00003994}
3995
aliguori43b96852009-04-24 18:03:33 +00003996static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00003997{
aliguori43b96852009-04-24 18:03:33 +00003998 int ret;
bellard89bfc102006-02-08 22:46:31 +00003999#ifdef CONFIG_PROFILER
4000 int64_t ti;
4001#endif
aliguori43b96852009-04-24 18:03:33 +00004002
4003#ifdef CONFIG_PROFILER
4004 ti = profile_getclock();
4005#endif
4006 if (use_icount) {
4007 int64_t count;
4008 int decr;
4009 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4010 env->icount_decr.u16.low = 0;
4011 env->icount_extra = 0;
4012 count = qemu_next_deadline();
4013 count = (count + (1 << icount_time_shift) - 1)
4014 >> icount_time_shift;
4015 qemu_icount += count;
4016 decr = (count > 0xffff) ? 0xffff : count;
4017 count -= decr;
4018 env->icount_decr.u16.low = decr;
4019 env->icount_extra = count;
4020 }
4021 ret = cpu_exec(env);
4022#ifdef CONFIG_PROFILER
4023 qemu_time += profile_getclock() - ti;
4024#endif
4025 if (use_icount) {
4026 /* Fold pending instructions back into the
4027 instruction counter, and clear the interrupt flag. */
4028 qemu_icount -= (env->icount_decr.u16.low
4029 + env->icount_extra);
4030 env->icount_decr.u32 = 0;
4031 env->icount_extra = 0;
4032 }
4033 return ret;
4034}
4035
aliguorie6e35b12009-04-24 18:03:57 +00004036static void tcg_cpu_exec(void)
4037{
aliguorid6dc3d42009-04-24 18:04:07 +00004038 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00004039
4040 if (next_cpu == NULL)
4041 next_cpu = first_cpu;
4042 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
4043 CPUState *env = cur_cpu = next_cpu;
4044
4045 if (!vm_running)
4046 break;
4047 if (timer_alarm_pending) {
4048 timer_alarm_pending = 0;
4049 break;
4050 }
aliguorid6dc3d42009-04-24 18:04:07 +00004051 if (cpu_can_run(env))
4052 ret = qemu_cpu_exec(env);
aliguorie6e35b12009-04-24 18:03:57 +00004053 if (ret == EXCP_DEBUG) {
4054 gdb_set_stop_cpu(env);
4055 debug_requested = 1;
4056 break;
4057 }
4058 }
4059}
4060
aliguori43b96852009-04-24 18:03:33 +00004061static int cpu_has_work(CPUState *env)
4062{
aliguorid6dc3d42009-04-24 18:04:07 +00004063 if (env->stop)
4064 return 1;
4065 if (env->stopped)
4066 return 0;
aliguori43b96852009-04-24 18:03:33 +00004067 if (!env->halted)
4068 return 1;
4069 if (qemu_cpu_has_work(env))
4070 return 1;
4071 return 0;
4072}
4073
4074static int tcg_has_work(void)
4075{
bellard6a00d602005-11-21 23:25:50 +00004076 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004077
aliguori43b96852009-04-24 18:03:33 +00004078 for (env = first_cpu; env != NULL; env = env->next_cpu)
4079 if (cpu_has_work(env))
4080 return 1;
4081 return 0;
4082}
bellard15a76442005-11-23 21:01:03 +00004083
aliguori43b96852009-04-24 18:03:33 +00004084static int qemu_calculate_timeout(void)
4085{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004086#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004087 int timeout;
bellard15a76442005-11-23 21:01:03 +00004088
aliguori43b96852009-04-24 18:03:33 +00004089 if (!vm_running)
4090 timeout = 5000;
4091 else if (tcg_has_work())
4092 timeout = 0;
4093 else if (!use_icount)
4094 timeout = 5000;
4095 else {
4096 /* XXX: use timeout computed from timers */
4097 int64_t add;
4098 int64_t delta;
4099 /* Advance virtual time to the next event. */
4100 if (use_icount == 1) {
4101 /* When not using an adaptive execution frequency
4102 we tend to get badly out of sync with real time,
4103 so just delay for a reasonable amount of time. */
4104 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004105 } else {
aliguori43b96852009-04-24 18:03:33 +00004106 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004107 }
aliguori43b96852009-04-24 18:03:33 +00004108 if (delta > 0) {
4109 /* If virtual time is ahead of real time then just
4110 wait for IO. */
4111 timeout = (delta / 1000000) + 1;
4112 } else {
4113 /* Wait for either IO to occur or the next
4114 timer event. */
4115 add = qemu_next_deadline();
4116 /* We advance the timer before checking for IO.
4117 Limit the amount we advance so that early IO
4118 activity won't get the guest too far ahead. */
4119 if (add > 10000000)
4120 add = 10000000;
4121 delta += add;
4122 add = (add + (1 << icount_time_shift) - 1)
4123 >> icount_time_shift;
4124 qemu_icount += add;
4125 timeout = delta / 1000000;
4126 if (timeout < 0)
4127 timeout = 0;
4128 }
bellardb4608c02003-06-27 17:34:32 +00004129 }
aliguori43b96852009-04-24 18:03:33 +00004130
4131 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004132#else /* CONFIG_IOTHREAD */
4133 return 1000;
4134#endif
aliguori43b96852009-04-24 18:03:33 +00004135}
4136
4137static int vm_can_run(void)
4138{
4139 if (powerdown_requested)
4140 return 0;
4141 if (reset_requested)
4142 return 0;
4143 if (shutdown_requested)
4144 return 0;
aliguorie5689022009-04-24 18:03:54 +00004145 if (debug_requested)
4146 return 0;
aliguori43b96852009-04-24 18:03:33 +00004147 return 1;
4148}
4149
Blue Swirld9c32312009-08-09 08:42:19 +00004150qemu_irq qemu_system_powerdown;
4151
aliguori43b96852009-04-24 18:03:33 +00004152static void main_loop(void)
4153{
aliguori6e29f5d2009-04-24 18:04:02 +00004154 int r;
aliguori43b96852009-04-24 18:03:33 +00004155
aliguorid6dc3d42009-04-24 18:04:07 +00004156#ifdef CONFIG_IOTHREAD
4157 qemu_system_ready = 1;
4158 qemu_cond_broadcast(&qemu_system_cond);
4159#endif
4160
aliguori6e29f5d2009-04-24 18:04:02 +00004161 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004162 do {
4163#ifdef CONFIG_PROFILER
4164 int64_t ti;
4165#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004166#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004167 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004168#endif
aliguori43b96852009-04-24 18:03:33 +00004169#ifdef CONFIG_PROFILER
4170 ti = profile_getclock();
4171#endif
4172 main_loop_wait(qemu_calculate_timeout());
4173#ifdef CONFIG_PROFILER
4174 dev_time += profile_getclock() - ti;
4175#endif
aliguorie5689022009-04-24 18:03:54 +00004176 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004177
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004178 if (qemu_debug_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004179 monitor_protocol_event(QEVENT_DEBUG, NULL);
aliguori43b96852009-04-24 18:03:33 +00004180 vm_stop(EXCP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004181 }
aliguori43b96852009-04-24 18:03:33 +00004182 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004183 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00004184 if (no_shutdown) {
4185 vm_stop(0);
4186 no_shutdown = 0;
4187 } else
4188 break;
4189 }
aliguorid6dc3d42009-04-24 18:04:07 +00004190 if (qemu_reset_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004191 monitor_protocol_event(QEVENT_RESET, NULL);
aliguorid6dc3d42009-04-24 18:04:07 +00004192 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004193 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004194 resume_all_vcpus();
4195 }
Blue Swirld9c32312009-08-09 08:42:19 +00004196 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004197 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00004198 qemu_irq_raise(qemu_system_powerdown);
4199 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004200 if ((r = qemu_vmstop_requested())) {
Blue Swirl242cd002009-12-04 18:05:45 +00004201 monitor_protocol_event(QEVENT_STOP, NULL);
aliguori6e29f5d2009-04-24 18:04:02 +00004202 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004203 }
aliguori43b96852009-04-24 18:03:33 +00004204 }
aliguorid6dc3d42009-04-24 18:04:07 +00004205 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004206}
4207
pbrook9bd7e6d2009-04-07 22:58:45 +00004208static void version(void)
4209{
pbrook4a19f1e2009-04-07 23:17:49 +00004210 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004211}
4212
ths15f82202007-06-29 23:26:08 +00004213static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004214{
pbrook9bd7e6d2009-04-07 22:58:45 +00004215 version();
4216 printf("usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004217 "\n"
bellarda20dd502003-09-30 21:07:02 +00004218 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004219 "\n"
blueswir15824d652009-03-28 06:44:27 +00004220#define DEF(option, opt_arg, opt_enum, opt_help) \
4221 opt_help
4222#define DEFHEADING(text) stringify(text) "\n"
4223#include "qemu-options.h"
4224#undef DEF
4225#undef DEFHEADING
4226#undef GEN_DOCS
bellard0824d6f2003-06-24 13:42:40 +00004227 "\n"
bellard82c643f2004-07-14 17:28:13 +00004228 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004229 "ctrl-alt-f toggle full screen\n"
4230 "ctrl-alt-n switch to virtual console 'n'\n"
4231 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004232 "\n"
4233 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4234 ,
bellard0db63472003-10-27 21:37:46 +00004235 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004236 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004237#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004238 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004239 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004240#endif
bellard6e44ba72004-01-18 21:56:49 +00004241 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004242 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004243 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004244}
4245
bellardcd6f1162004-05-13 22:02:20 +00004246#define HAS_ARG 0x0001
4247
4248enum {
blueswir15824d652009-03-28 06:44:27 +00004249#define DEF(option, opt_arg, opt_enum, opt_help) \
4250 opt_enum,
4251#define DEFHEADING(text)
4252#include "qemu-options.h"
4253#undef DEF
4254#undef DEFHEADING
4255#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004256};
4257
4258typedef struct QEMUOption {
4259 const char *name;
4260 int flags;
4261 int index;
4262} QEMUOption;
4263
blueswir1dbed7e42008-10-01 19:38:09 +00004264static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004265 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004266#define DEF(option, opt_arg, opt_enum, opt_help) \
4267 { option, opt_arg, opt_enum },
4268#define DEFHEADING(text)
4269#include "qemu-options.h"
4270#undef DEF
4271#undef DEFHEADING
4272#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004273 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004274};
4275
bellard1d14ffa2005-10-30 18:58:22 +00004276#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004277struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004278#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004279#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004280 {
4281 "pcspk",
4282 "PC speaker",
4283 0,
4284 1,
4285 { .init_isa = pcspk_audio_init }
4286 },
4287#endif
malc4c9b53e2009-01-09 10:46:34 +00004288
4289#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004290 {
4291 "sb16",
4292 "Creative Sound Blaster 16",
4293 0,
4294 1,
4295 { .init_isa = SB16_init }
4296 },
malc4c9b53e2009-01-09 10:46:34 +00004297#endif
bellard6a36d842005-12-18 20:34:32 +00004298
malccc53d262008-06-13 10:48:22 +00004299#ifdef CONFIG_CS4231A
4300 {
4301 "cs4231a",
4302 "CS4231A",
4303 0,
4304 1,
4305 { .init_isa = cs4231a_init }
4306 },
4307#endif
4308
bellard6a36d842005-12-18 20:34:32 +00004309#ifdef CONFIG_ADLIB
4310 {
4311 "adlib",
4312#ifdef HAS_YMF262
4313 "Yamaha YMF262 (OPL3)",
4314#else
4315 "Yamaha YM3812 (OPL2)",
4316#endif
4317 0,
4318 1,
4319 { .init_isa = Adlib_init }
4320 },
4321#endif
4322
4323#ifdef CONFIG_GUS
4324 {
4325 "gus",
4326 "Gravis Ultrasound GF1",
4327 0,
4328 1,
4329 { .init_isa = GUS_init }
4330 },
4331#endif
4332
malc4c9b53e2009-01-09 10:46:34 +00004333#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004334 {
4335 "ac97",
4336 "Intel 82801AA AC97 Audio",
4337 0,
4338 0,
4339 { .init_pci = ac97_init }
4340 },
malc4c9b53e2009-01-09 10:46:34 +00004341#endif
balroge5c9a132008-01-14 04:27:55 +00004342
malc4c9b53e2009-01-09 10:46:34 +00004343#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004344 {
4345 "es1370",
4346 "ENSONIQ AudioPCI ES1370",
4347 0,
4348 0,
4349 { .init_pci = es1370_init }
4350 },
balrogb00052e2007-04-30 02:22:06 +00004351#endif
bellard6a36d842005-12-18 20:34:32 +00004352
malc4c9b53e2009-01-09 10:46:34 +00004353#endif /* HAS_AUDIO_CHOICE */
4354
bellard6a36d842005-12-18 20:34:32 +00004355 { NULL, NULL, 0, 0, { NULL } }
4356};
4357
bellard1d14ffa2005-10-30 18:58:22 +00004358static void select_soundhw (const char *optarg)
4359{
bellard6a36d842005-12-18 20:34:32 +00004360 struct soundhw *c;
4361
bellard1d14ffa2005-10-30 18:58:22 +00004362 if (*optarg == '?') {
4363 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004364
bellard1d14ffa2005-10-30 18:58:22 +00004365 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004366 for (c = soundhw; c->name; ++c) {
4367 printf ("%-11s %s\n", c->name, c->descr);
4368 }
4369 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004370 exit (*optarg != '?');
4371 }
4372 else {
bellard6a36d842005-12-18 20:34:32 +00004373 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004374 const char *p;
4375 char *e;
4376 int bad_card = 0;
4377
bellard6a36d842005-12-18 20:34:32 +00004378 if (!strcmp (optarg, "all")) {
4379 for (c = soundhw; c->name; ++c) {
4380 c->enabled = 1;
4381 }
4382 return;
4383 }
bellard1d14ffa2005-10-30 18:58:22 +00004384
bellard6a36d842005-12-18 20:34:32 +00004385 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004386 while (*p) {
4387 e = strchr (p, ',');
4388 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004389
4390 for (c = soundhw; c->name; ++c) {
malcb3d6fb42009-09-06 06:49:03 +04004391 if (!strncmp (c->name, p, l) && !c->name[l]) {
bellard6a36d842005-12-18 20:34:32 +00004392 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004393 break;
4394 }
4395 }
bellard6a36d842005-12-18 20:34:32 +00004396
4397 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004398 if (l > 80) {
4399 fprintf (stderr,
4400 "Unknown sound card name (too big to show)\n");
4401 }
4402 else {
4403 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4404 (int) l, p);
4405 }
4406 bad_card = 1;
4407 }
4408 p += l + (e != NULL);
4409 }
4410
4411 if (bad_card)
4412 goto show_valid_cards;
4413 }
4414}
4415#endif
4416
malc3893c122008-09-28 00:42:05 +00004417static void select_vgahw (const char *p)
4418{
4419 const char *opts;
4420
Gerd Hoffmann64465292009-12-08 13:11:45 +01004421 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10004422 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00004423 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004424 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00004425 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004426 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00004427 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004428 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00004429 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004430 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00004431 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004432 invalid_vga:
4433 fprintf(stderr, "Unknown vga type: %s\n", p);
4434 exit(1);
4435 }
malccb5a7aa2008-09-28 00:42:12 +00004436 while (*opts) {
4437 const char *nextopt;
4438
4439 if (strstart(opts, ",retrace=", &nextopt)) {
4440 opts = nextopt;
4441 if (strstart(opts, "dumb", &nextopt))
4442 vga_retrace_method = VGA_RETRACE_DUMB;
4443 else if (strstart(opts, "precise", &nextopt))
4444 vga_retrace_method = VGA_RETRACE_PRECISE;
4445 else goto invalid_vga;
4446 } else goto invalid_vga;
4447 opts = nextopt;
4448 }
malc3893c122008-09-28 00:42:05 +00004449}
4450
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004451#ifdef TARGET_I386
4452static int balloon_parse(const char *arg)
4453{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004454 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004455
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004456 if (strcmp(arg, "none") == 0) {
4457 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004458 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004459
4460 if (!strncmp(arg, "virtio", 6)) {
4461 if (arg[6] == ',') {
4462 /* have params -> parse them */
4463 opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL);
4464 if (!opts)
4465 return -1;
4466 } else {
4467 /* create empty opts */
4468 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4469 }
4470 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
4471 return 0;
4472 }
4473
4474 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004475}
4476#endif
4477
bellard3587d7e2006-06-26 20:03:44 +00004478#ifdef _WIN32
4479static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4480{
4481 exit(STATUS_CONTROL_C_EXIT);
4482 return TRUE;
4483}
4484#endif
4485
aliguoric4be29f2009-04-17 18:58:14 +00004486int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004487{
4488 int ret;
4489
4490 if(strlen(str) != 36)
4491 return -1;
4492
4493 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4494 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4495 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4496
4497 if(ret != 16)
4498 return -1;
4499
aliguorib6f6e3d2009-04-17 18:59:56 +00004500#ifdef TARGET_I386
4501 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4502#endif
4503
blueswir18fcb1b92008-09-18 18:29:08 +00004504 return 0;
4505}
4506
aliguori5b08fc12008-08-21 20:08:03 +00004507#ifndef _WIN32
4508
4509static void termsig_handler(int signal)
4510{
4511 qemu_system_shutdown_request();
4512}
4513
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004514static void sigchld_handler(int signal)
4515{
4516 waitpid(-1, NULL, WNOHANG);
4517}
4518
4519static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004520{
4521 struct sigaction act;
4522
4523 memset(&act, 0, sizeof(act));
4524 act.sa_handler = termsig_handler;
4525 sigaction(SIGINT, &act, NULL);
4526 sigaction(SIGHUP, &act, NULL);
4527 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004528
4529 act.sa_handler = sigchld_handler;
4530 act.sa_flags = SA_NOCLDSTOP;
4531 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004532}
4533
4534#endif
4535
Paul Brook5cea8592009-05-30 00:52:44 +01004536#ifdef _WIN32
4537/* Look for support files in the same directory as the executable. */
4538static char *find_datadir(const char *argv0)
4539{
4540 char *p;
4541 char buf[MAX_PATH];
4542 DWORD len;
4543
4544 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4545 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004546 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004547 }
4548
4549 buf[len] = 0;
4550 p = buf + len - 1;
4551 while (p != buf && *p != '\\')
4552 p--;
4553 *p = 0;
4554 if (access(buf, R_OK) == 0) {
4555 return qemu_strdup(buf);
4556 }
4557 return NULL;
4558}
4559#else /* !_WIN32 */
4560
4561/* Find a likely location for support files using the location of the binary.
4562 For installed binaries this will be "$bindir/../share/qemu". When
4563 running from the build tree this will be "$bindir/../pc-bios". */
4564#define SHARE_SUFFIX "/share/qemu"
4565#define BUILD_SUFFIX "/pc-bios"
4566static char *find_datadir(const char *argv0)
4567{
4568 char *dir;
4569 char *p = NULL;
4570 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01004571 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00004572 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004573
4574#if defined(__linux__)
4575 {
4576 int len;
4577 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4578 if (len > 0) {
4579 buf[len] = 0;
4580 p = buf;
4581 }
4582 }
4583#elif defined(__FreeBSD__)
4584 {
4585 int len;
4586 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4587 if (len > 0) {
4588 buf[len] = 0;
4589 p = buf;
4590 }
4591 }
4592#endif
4593 /* If we don't have any way of figuring out the actual executable
4594 location then try argv[0]. */
4595 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004596 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01004597 if (!p) {
4598 return NULL;
4599 }
4600 }
4601 dir = dirname(p);
4602 dir = dirname(dir);
4603
Blue Swirl3a417592009-06-09 19:12:21 +00004604 max_len = strlen(dir) +
4605 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4606 res = qemu_mallocz(max_len);
4607 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004608 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004609 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004610 if (access(res, R_OK)) {
4611 qemu_free(res);
4612 res = NULL;
4613 }
4614 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004615
Paul Brook5cea8592009-05-30 00:52:44 +01004616 return res;
4617}
4618#undef SHARE_SUFFIX
4619#undef BUILD_SUFFIX
4620#endif
4621
4622char *qemu_find_file(int type, const char *name)
4623{
4624 int len;
4625 const char *subdir;
4626 char *buf;
4627
4628 /* If name contains path separators then try it as a straight path. */
4629 if ((strchr(name, '/') || strchr(name, '\\'))
4630 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02004631 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01004632 }
4633 switch (type) {
4634 case QEMU_FILE_TYPE_BIOS:
4635 subdir = "";
4636 break;
4637 case QEMU_FILE_TYPE_KEYMAP:
4638 subdir = "keymaps/";
4639 break;
4640 default:
4641 abort();
4642 }
4643 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4644 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004645 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004646 if (access(buf, R_OK)) {
4647 qemu_free(buf);
4648 return NULL;
4649 }
4650 return buf;
4651}
4652
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004653static int device_init_func(QemuOpts *opts, void *opaque)
4654{
4655 DeviceState *dev;
4656
4657 dev = qdev_device_add(opts);
4658 if (!dev)
4659 return -1;
4660 return 0;
4661}
4662
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004663static int chardev_init_func(QemuOpts *opts, void *opaque)
4664{
4665 CharDriverState *chr;
4666
4667 chr = qemu_chr_open_opts(opts, NULL);
4668 if (!chr)
4669 return -1;
4670 return 0;
4671}
4672
Gerd Hoffmann88589342009-12-08 13:11:50 +01004673static int mon_init_func(QemuOpts *opts, void *opaque)
4674{
4675 CharDriverState *chr;
4676 const char *chardev;
4677 const char *mode;
4678 int flags;
4679
4680 mode = qemu_opt_get(opts, "mode");
4681 if (mode == NULL) {
4682 mode = "readline";
4683 }
4684 if (strcmp(mode, "readline") == 0) {
4685 flags = MONITOR_USE_READLINE;
4686 } else if (strcmp(mode, "control") == 0) {
4687 flags = MONITOR_USE_CONTROL;
4688 } else {
4689 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
4690 exit(1);
4691 }
4692
4693 if (qemu_opt_get_bool(opts, "default", 0))
4694 flags |= MONITOR_IS_DEFAULT;
4695
4696 chardev = qemu_opt_get(opts, "chardev");
4697 chr = qemu_chr_find(chardev);
4698 if (chr == NULL) {
4699 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
4700 exit(1);
4701 }
4702
4703 monitor_init(chr, flags);
4704 return 0;
4705}
4706
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004707static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01004708{
4709 static int monitor_device_index = 0;
4710 QemuOpts *opts;
4711 const char *p;
4712 char label[32];
4713 int def = 0;
4714
4715 if (strstart(optarg, "chardev:", &p)) {
4716 snprintf(label, sizeof(label), "%s", p);
4717 } else {
4718 if (monitor_device_index) {
4719 snprintf(label, sizeof(label), "monitor%d",
4720 monitor_device_index);
4721 } else {
4722 snprintf(label, sizeof(label), "monitor");
4723 def = 1;
4724 }
4725 opts = qemu_chr_parse_compat(label, optarg);
4726 if (!opts) {
4727 fprintf(stderr, "parse error: %s\n", optarg);
4728 exit(1);
4729 }
4730 }
4731
4732 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
4733 if (!opts) {
4734 fprintf(stderr, "duplicate chardev: %s\n", label);
4735 exit(1);
4736 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004737 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01004738 qemu_opt_set(opts, "chardev", label);
4739 if (def)
4740 qemu_opt_set(opts, "default", "on");
4741 monitor_device_index++;
4742}
4743
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004744struct device_config {
4745 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004746 DEV_USB, /* -usbdevice */
4747 DEV_BT, /* -bt */
4748 DEV_SERIAL, /* -serial */
4749 DEV_PARALLEL, /* -parallel */
4750 DEV_VIRTCON, /* -virtioconsole */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004751 } type;
4752 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004753 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004754};
Blue Swirl72cf2d42009-09-12 07:36:22 +00004755QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004756
4757static void add_device_config(int type, const char *cmdline)
4758{
4759 struct device_config *conf;
4760
4761 conf = qemu_mallocz(sizeof(*conf));
4762 conf->type = type;
4763 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004764 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004765}
4766
4767static int foreach_device_config(int type, int (*func)(const char *cmdline))
4768{
4769 struct device_config *conf;
4770 int rc;
4771
Blue Swirl72cf2d42009-09-12 07:36:22 +00004772 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004773 if (conf->type != type)
4774 continue;
4775 rc = func(conf->cmdline);
4776 if (0 != rc)
4777 return rc;
4778 }
4779 return 0;
4780}
4781
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004782static int serial_parse(const char *devname)
4783{
4784 static int index = 0;
4785 char label[32];
4786
4787 if (strcmp(devname, "none") == 0)
4788 return 0;
4789 if (index == MAX_SERIAL_PORTS) {
4790 fprintf(stderr, "qemu: too many serial ports\n");
4791 exit(1);
4792 }
4793 snprintf(label, sizeof(label), "serial%d", index);
4794 serial_hds[index] = qemu_chr_open(label, devname, NULL);
4795 if (!serial_hds[index]) {
4796 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
4797 devname, strerror(errno));
4798 return -1;
4799 }
4800 index++;
4801 return 0;
4802}
4803
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004804static int parallel_parse(const char *devname)
4805{
4806 static int index = 0;
4807 char label[32];
4808
4809 if (strcmp(devname, "none") == 0)
4810 return 0;
4811 if (index == MAX_PARALLEL_PORTS) {
4812 fprintf(stderr, "qemu: too many parallel ports\n");
4813 exit(1);
4814 }
4815 snprintf(label, sizeof(label), "parallel%d", index);
4816 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
4817 if (!parallel_hds[index]) {
4818 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
4819 devname, strerror(errno));
4820 return -1;
4821 }
4822 index++;
4823 return 0;
4824}
4825
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004826static int virtcon_parse(const char *devname)
4827{
4828 static int index = 0;
4829 char label[32];
4830
4831 if (strcmp(devname, "none") == 0)
4832 return 0;
4833 if (index == MAX_VIRTIO_CONSOLES) {
4834 fprintf(stderr, "qemu: too many virtio consoles\n");
4835 exit(1);
4836 }
4837 snprintf(label, sizeof(label), "virtcon%d", index);
4838 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
4839 if (!virtcon_hds[index]) {
4840 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
4841 devname, strerror(errno));
4842 return -1;
4843 }
4844 index++;
4845 return 0;
4846}
4847
malc902b3d52008-12-10 19:18:40 +00004848int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004849{
aliguori59030a82009-04-05 18:43:41 +00004850 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004851 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004852 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004853 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004854 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004855 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05004856 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004857 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004858 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004859 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004860 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00004861 int optind;
4862 const char *r, *optarg;
bellardd63d3072004-10-03 13:29:03 +00004863 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004864 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004865 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004866#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004867 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004868#endif
bellard26a5f132008-05-28 12:30:31 +00004869 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004870 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004871 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004872#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004873 int fd = 0;
4874 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004875 const char *chroot_dir = NULL;
4876 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004877#endif
aliguori268a3622009-04-21 22:30:27 +00004878 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004879 int show_vnc_port = 0;
bellard0bd48852005-11-11 00:00:47 +00004880
Jan Kiszka68752042009-09-15 13:36:04 +02004881 init_clocks();
4882
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004883 qemu_errors_to_file(stderr);
malc902b3d52008-12-10 19:18:40 +00004884 qemu_cache_utils_init(envp);
4885
Blue Swirl72cf2d42009-09-12 07:36:22 +00004886 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004887#ifndef _WIN32
4888 {
4889 struct sigaction act;
4890 sigfillset(&act.sa_mask);
4891 act.sa_flags = 0;
4892 act.sa_handler = SIG_IGN;
4893 sigaction(SIGPIPE, &act, NULL);
4894 }
bellard3587d7e2006-06-26 20:03:44 +00004895#else
4896 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004897 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4898 QEMU to run on a single CPU */
4899 {
4900 HANDLE h;
4901 DWORD mask, smask;
4902 int i;
4903 h = GetCurrentProcess();
4904 if (GetProcessAffinityMask(h, &mask, &smask)) {
4905 for(i = 0; i < 32; i++) {
4906 if (mask & (1 << i))
4907 break;
4908 }
4909 if (i != 32) {
4910 mask = 1 << i;
4911 SetProcessAffinityMask(h, mask);
4912 }
4913 }
4914 }
bellard67b915a2004-03-31 23:37:16 +00004915#endif
bellardbe995c22006-06-25 16:25:21 +00004916
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004917 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004918 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004919 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004920 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004921 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004922 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004923 kernel_filename = NULL;
4924 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004925 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004926 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00004927
aliguori268a3622009-04-21 22:30:27 +00004928 for (i = 0; i < MAX_NODES; i++) {
4929 node_mem[i] = 0;
4930 node_cpumask[i] = 0;
4931 }
4932
aliguori268a3622009-04-21 22:30:27 +00004933 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004934 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004935
bellard26a5f132008-05-28 12:30:31 +00004936 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004937 autostart= 1;
4938
bellardcd6f1162004-05-13 22:02:20 +00004939 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004940 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004941 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004942 break;
bellardcd6f1162004-05-13 22:02:20 +00004943 r = argv[optind];
4944 if (r[0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004945 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004946 } else {
4947 const QEMUOption *popt;
4948
4949 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004950 /* Treat --foo the same as -foo. */
4951 if (r[1] == '-')
4952 r++;
bellardcd6f1162004-05-13 22:02:20 +00004953 popt = qemu_options;
4954 for(;;) {
4955 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004956 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004957 argv[0], r);
4958 exit(1);
4959 }
4960 if (!strcmp(popt->name, r + 1))
4961 break;
4962 popt++;
4963 }
4964 if (popt->flags & HAS_ARG) {
4965 if (optind >= argc) {
4966 fprintf(stderr, "%s: option '%s' requires an argument\n",
4967 argv[0], r);
4968 exit(1);
4969 }
4970 optarg = argv[optind++];
4971 } else {
4972 optarg = NULL;
4973 }
4974
4975 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004976 case QEMU_OPTION_M:
4977 machine = find_machine(optarg);
4978 if (!machine) {
4979 QEMUMachine *m;
4980 printf("Supported machines are:\n");
4981 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004982 if (m->alias)
4983 printf("%-10s %s (alias of %s)\n",
4984 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004985 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004986 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004987 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004988 }
ths15f82202007-06-29 23:26:08 +00004989 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004990 }
4991 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004992 case QEMU_OPTION_cpu:
4993 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004994 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004995/* XXX: implement xxx_cpu_list for targets that still miss it */
4996#if defined(cpu_list)
4997 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004998#endif
ths15f82202007-06-29 23:26:08 +00004999 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00005000 } else {
5001 cpu_model = optarg;
5002 }
5003 break;
bellardcd6f1162004-05-13 22:02:20 +00005004 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00005005 initrd_filename = optarg;
5006 break;
bellardcd6f1162004-05-13 22:02:20 +00005007 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00005008 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005009 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00005010 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005011 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00005012 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00005013 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005014 translation == BIOS_ATA_TRANSLATION_LBA ?
5015 ",trans=lba" :
5016 translation == BIOS_ATA_TRANSLATION_NONE ?
5017 ",trans=none" : "");
5018 break;
bellardcd6f1162004-05-13 22:02:20 +00005019 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00005020 case QEMU_OPTION_hdc:
5021 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00005022 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00005023 break;
thse4bcb142007-12-02 04:51:10 +00005024 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00005025 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00005026 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02005027 case QEMU_OPTION_set:
5028 if (qemu_set_option(optarg) != 0)
5029 exit(1);
5030 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01005031 case QEMU_OPTION_global:
5032 if (qemu_global_option(optarg) != 0)
5033 exit(1);
5034 break;
balrog3e3d5812007-04-30 02:09:25 +00005035 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00005036 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00005037 break;
pbrooka1bb27b2007-04-06 16:49:48 +00005038 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00005039 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00005040 break;
j_mayer86f55662007-04-24 06:52:59 +00005041 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00005042 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00005043 break;
bellardcd6f1162004-05-13 22:02:20 +00005044 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00005045 snapshot = 1;
5046 break;
bellardcd6f1162004-05-13 22:02:20 +00005047 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00005048 {
bellard330d0412003-07-26 18:11:40 +00005049 const char *p;
5050 p = optarg;
5051 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005052 if (cyls < 1 || cyls > 16383)
5053 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005054 if (*p != ',')
5055 goto chs_fail;
5056 p++;
5057 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005058 if (heads < 1 || heads > 16)
5059 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005060 if (*p != ',')
5061 goto chs_fail;
5062 p++;
5063 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005064 if (secs < 1 || secs > 63)
5065 goto chs_fail;
5066 if (*p == ',') {
5067 p++;
5068 if (!strcmp(p, "none"))
5069 translation = BIOS_ATA_TRANSLATION_NONE;
5070 else if (!strcmp(p, "lba"))
5071 translation = BIOS_ATA_TRANSLATION_LBA;
5072 else if (!strcmp(p, "auto"))
5073 translation = BIOS_ATA_TRANSLATION_AUTO;
5074 else
5075 goto chs_fail;
5076 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00005077 chs_fail:
bellard46d47672004-11-16 01:45:27 +00005078 fprintf(stderr, "qemu: invalid physical CHS format\n");
5079 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00005080 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005081 if (hda_opts != NULL) {
5082 char num[16];
5083 snprintf(num, sizeof(num), "%d", cyls);
5084 qemu_opt_set(hda_opts, "cyls", num);
5085 snprintf(num, sizeof(num), "%d", heads);
5086 qemu_opt_set(hda_opts, "heads", num);
5087 snprintf(num, sizeof(num), "%d", secs);
5088 qemu_opt_set(hda_opts, "secs", num);
5089 if (translation == BIOS_ATA_TRANSLATION_LBA)
5090 qemu_opt_set(hda_opts, "trans", "lba");
5091 if (translation == BIOS_ATA_TRANSLATION_NONE)
5092 qemu_opt_set(hda_opts, "trans", "none");
5093 }
bellard330d0412003-07-26 18:11:40 +00005094 }
5095 break;
aliguori268a3622009-04-21 22:30:27 +00005096 case QEMU_OPTION_numa:
5097 if (nb_numa_nodes >= MAX_NODES) {
5098 fprintf(stderr, "qemu: too many NUMA nodes\n");
5099 exit(1);
5100 }
5101 numa_add(optarg);
5102 break;
bellardcd6f1162004-05-13 22:02:20 +00005103 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005104 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005105 break;
balrog4d3b6f62008-02-10 16:33:14 +00005106#ifdef CONFIG_CURSES
5107 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005108 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005109 break;
5110#endif
balroga171fe32007-04-30 01:48:07 +00005111 case QEMU_OPTION_portrait:
5112 graphic_rotate = 1;
5113 break;
bellardcd6f1162004-05-13 22:02:20 +00005114 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005115 kernel_filename = optarg;
5116 break;
bellardcd6f1162004-05-13 22:02:20 +00005117 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005118 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005119 break;
bellardcd6f1162004-05-13 22:02:20 +00005120 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005121 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005122 break;
bellardcd6f1162004-05-13 22:02:20 +00005123 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005124 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005125 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02005126 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005127 };
5128 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005129 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005130 int legacy = 0;
5131
5132 if (!strchr(optarg, '=')) {
5133 legacy = 1;
5134 pstrcpy(buf, sizeof(buf), optarg);
5135 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5136 fprintf(stderr,
5137 "qemu: unknown boot parameter '%s' in '%s'\n",
5138 buf, optarg);
5139 exit(1);
5140 }
5141
5142 if (legacy ||
5143 get_param_value(buf, sizeof(buf), "order", optarg)) {
5144 boot_devices_bitmap = parse_bootdevices(buf);
5145 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005146 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005147 if (!legacy) {
5148 if (get_param_value(buf, sizeof(buf),
5149 "once", optarg)) {
5150 boot_devices_bitmap |= parse_bootdevices(buf);
5151 standard_boot_devices = qemu_strdup(boot_devices);
5152 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5153 qemu_register_reset(restore_boot_devices,
5154 standard_boot_devices);
5155 }
Jan Kiszka95387492009-07-02 00:19:02 +02005156 if (get_param_value(buf, sizeof(buf),
5157 "menu", optarg)) {
5158 if (!strcmp(buf, "on")) {
5159 boot_menu = 1;
5160 } else if (!strcmp(buf, "off")) {
5161 boot_menu = 0;
5162 } else {
5163 fprintf(stderr,
5164 "qemu: invalid option value '%s'\n",
5165 buf);
5166 exit(1);
5167 }
5168 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005169 }
bellard36b486b2003-11-11 13:36:08 +00005170 }
5171 break;
bellardcd6f1162004-05-13 22:02:20 +00005172 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005173 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005174 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005175 break;
bellard52ca8d62006-06-14 16:03:05 +00005176#ifdef TARGET_I386
5177 case QEMU_OPTION_no_fd_bootchk:
5178 fd_bootchk = 0;
5179 break;
5180#endif
Mark McLoughlina1ea4582009-10-08 19:58:26 +01005181 case QEMU_OPTION_netdev:
5182 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
5183 exit(1);
5184 }
5185 break;
bellard7c9d8e02005-11-15 22:16:05 +00005186 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01005187 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00005188 exit(1);
5189 }
bellard702c6512004-04-02 21:21:32 +00005190 break;
bellardc7f74642004-08-24 21:57:12 +00005191#ifdef CONFIG_SLIRP
5192 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005193 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005194 break;
ths47d5d012007-02-20 00:05:08 +00005195 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005196 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005197 break;
bellardc94c8d62004-09-13 21:37:34 +00005198#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005199 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01005200 if (net_slirp_smb(optarg) < 0)
5201 exit(1);
bellard9d728e82004-09-05 23:09:03 +00005202 break;
bellardc94c8d62004-09-13 21:37:34 +00005203#endif
bellard9bf05442004-08-25 22:12:49 +00005204 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01005205 if (net_slirp_redir(optarg) < 0)
5206 exit(1);
bellard9bf05442004-08-25 22:12:49 +00005207 break;
bellardc7f74642004-08-24 21:57:12 +00005208#endif
balrogdc72ac12008-11-09 00:04:26 +00005209 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005210 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00005211 break;
bellard1d14ffa2005-10-30 18:58:22 +00005212#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005213 case QEMU_OPTION_audio_help:
5214 AUD_help ();
5215 exit (0);
5216 break;
5217 case QEMU_OPTION_soundhw:
5218 select_soundhw (optarg);
5219 break;
5220#endif
bellardcd6f1162004-05-13 22:02:20 +00005221 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005222 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005223 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005224 case QEMU_OPTION_version:
5225 version();
5226 exit(0);
5227 break;
aurel3200f82b82008-04-27 21:12:55 +00005228 case QEMU_OPTION_m: {
5229 uint64_t value;
5230 char *ptr;
5231
5232 value = strtoul(optarg, &ptr, 10);
5233 switch (*ptr) {
5234 case 0: case 'M': case 'm':
5235 value <<= 20;
5236 break;
5237 case 'G': case 'g':
5238 value <<= 30;
5239 break;
5240 default:
5241 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005242 exit(1);
5243 }
aurel3200f82b82008-04-27 21:12:55 +00005244
5245 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05005246 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00005247 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5248 exit(1);
5249 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005250 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00005251 fprintf(stderr, "qemu: ram size too large\n");
5252 exit(1);
5253 }
5254 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005255 break;
aurel3200f82b82008-04-27 21:12:55 +00005256 }
bellardcd6f1162004-05-13 22:02:20 +00005257 case QEMU_OPTION_d:
5258 {
5259 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005260 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005261
bellardcd6f1162004-05-13 22:02:20 +00005262 mask = cpu_str_to_log_mask(optarg);
5263 if (!mask) {
5264 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005265 for(item = cpu_log_items; item->mask != 0; item++) {
5266 printf("%-10s %s\n", item->name, item->help);
5267 }
5268 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005269 }
5270 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005271 }
bellardcd6f1162004-05-13 22:02:20 +00005272 break;
bellardcd6f1162004-05-13 22:02:20 +00005273 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005274 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005275 break;
aliguori59030a82009-04-05 18:43:41 +00005276 case QEMU_OPTION_gdb:
5277 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005278 break;
bellardcd6f1162004-05-13 22:02:20 +00005279 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005280 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005281 break;
j_mayer1192dad2007-10-05 13:08:35 +00005282 case QEMU_OPTION_bios:
5283 bios_name = optarg;
5284 break;
aurel321b530a62009-04-05 20:08:59 +00005285 case QEMU_OPTION_singlestep:
5286 singlestep = 1;
5287 break;
bellardcd6f1162004-05-13 22:02:20 +00005288 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005289 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005290 break;
bellard3d11d0e2004-12-12 16:56:30 +00005291 case QEMU_OPTION_k:
5292 keyboard_layout = optarg;
5293 break;
bellardee22c2f2004-06-03 12:49:50 +00005294 case QEMU_OPTION_localtime:
5295 rtc_utc = 0;
5296 break;
malc3893c122008-09-28 00:42:05 +00005297 case QEMU_OPTION_vga:
5298 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005299 break;
blueswir15824d652009-03-28 06:44:27 +00005300#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005301 case QEMU_OPTION_g:
5302 {
5303 const char *p;
5304 int w, h, depth;
5305 p = optarg;
5306 w = strtol(p, (char **)&p, 10);
5307 if (w <= 0) {
5308 graphic_error:
5309 fprintf(stderr, "qemu: invalid resolution or depth\n");
5310 exit(1);
5311 }
5312 if (*p != 'x')
5313 goto graphic_error;
5314 p++;
5315 h = strtol(p, (char **)&p, 10);
5316 if (h <= 0)
5317 goto graphic_error;
5318 if (*p == 'x') {
5319 p++;
5320 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005321 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005322 depth != 24 && depth != 32)
5323 goto graphic_error;
5324 } else if (*p == '\0') {
5325 depth = graphic_depth;
5326 } else {
5327 goto graphic_error;
5328 }
ths3b46e622007-09-17 08:09:54 +00005329
bellarde9b137c2004-06-21 16:46:10 +00005330 graphic_width = w;
5331 graphic_height = h;
5332 graphic_depth = depth;
5333 }
5334 break;
blueswir15824d652009-03-28 06:44:27 +00005335#endif
ths20d8a3e2007-02-18 17:04:49 +00005336 case QEMU_OPTION_echr:
5337 {
5338 char *r;
5339 term_escape_char = strtol(optarg, &r, 0);
5340 if (r == optarg)
5341 printf("Bad argument to echr\n");
5342 break;
5343 }
bellard82c643f2004-07-14 17:28:13 +00005344 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01005345 monitor_parse(optarg, "readline");
5346 default_monitor = 0;
5347 break;
5348 case QEMU_OPTION_qmp:
5349 monitor_parse(optarg, "control");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005350 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00005351 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01005352 case QEMU_OPTION_mon:
5353 opts = qemu_opts_parse(&qemu_mon_opts, optarg, "chardev");
5354 if (!opts) {
5355 fprintf(stderr, "parse error: %s\n", optarg);
5356 exit(1);
5357 }
5358 default_monitor = 0;
5359 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005360 case QEMU_OPTION_chardev:
5361 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
5362 if (!opts) {
5363 fprintf(stderr, "parse error: %s\n", optarg);
5364 exit(1);
5365 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005366 break;
bellard82c643f2004-07-14 17:28:13 +00005367 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005368 add_device_config(DEV_SERIAL, optarg);
5369 default_serial = 0;
bellard82c643f2004-07-14 17:28:13 +00005370 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005371 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02005372 if (watchdog) {
5373 fprintf(stderr,
5374 "qemu: only one watchdog option may be given\n");
5375 return 1;
5376 }
5377 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005378 break;
5379 case QEMU_OPTION_watchdog_action:
5380 if (select_watchdog_action(optarg) == -1) {
5381 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5382 exit(1);
5383 }
5384 break;
aliguori51ecf132009-01-15 20:06:40 +00005385 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01005386 add_device_config(DEV_VIRTCON, optarg);
5387 default_virtcon = 0;
aliguori51ecf132009-01-15 20:06:40 +00005388 break;
bellard6508fe52005-01-15 12:02:56 +00005389 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005390 add_device_config(DEV_PARALLEL, optarg);
5391 default_parallel = 0;
bellard6508fe52005-01-15 12:02:56 +00005392 break;
bellardd63d3072004-10-03 13:29:03 +00005393 case QEMU_OPTION_loadvm:
5394 loadvm = optarg;
5395 break;
5396 case QEMU_OPTION_full_screen:
5397 full_screen = 1;
5398 break;
ths667acca2006-12-11 02:08:05 +00005399#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005400 case QEMU_OPTION_no_frame:
5401 no_frame = 1;
5402 break;
ths3780e192007-06-21 21:08:02 +00005403 case QEMU_OPTION_alt_grab:
5404 alt_grab = 1;
5405 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05005406 case QEMU_OPTION_ctrl_grab:
5407 ctrl_grab = 1;
5408 break;
ths667acca2006-12-11 02:08:05 +00005409 case QEMU_OPTION_no_quit:
5410 no_quit = 1;
5411 break;
aliguori7d957bd2009-01-15 22:14:11 +00005412 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005413 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005414 break;
ths667acca2006-12-11 02:08:05 +00005415#endif
bellardf7cce892004-12-08 22:21:25 +00005416 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005417 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005418 break;
bellarda09db212005-04-30 16:10:35 +00005419#ifdef TARGET_I386
5420 case QEMU_OPTION_win2k_hack:
5421 win2k_install_hack = 1;
5422 break;
aliguori73822ec2009-01-15 20:11:34 +00005423 case QEMU_OPTION_rtc_td_hack:
5424 rtc_td_hack = 1;
5425 break;
aliguori8a92ea22009-02-27 20:12:36 +00005426 case QEMU_OPTION_acpitable:
5427 if(acpi_table_add(optarg) < 0) {
5428 fprintf(stderr, "Wrong acpi table provided\n");
5429 exit(1);
5430 }
5431 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005432 case QEMU_OPTION_smbios:
5433 if(smbios_entry_add(optarg) < 0) {
5434 fprintf(stderr, "Wrong smbios provided\n");
5435 exit(1);
5436 }
5437 break;
bellarda09db212005-04-30 16:10:35 +00005438#endif
aliguori7ba1e612008-11-05 16:04:33 +00005439#ifdef CONFIG_KVM
5440 case QEMU_OPTION_enable_kvm:
5441 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00005442 break;
5443#endif
bellardbb36d472005-11-05 14:22:28 +00005444 case QEMU_OPTION_usb:
5445 usb_enabled = 1;
5446 break;
bellarda594cfb2005-11-06 16:13:29 +00005447 case QEMU_OPTION_usbdevice:
5448 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005449 add_device_config(DEV_USB, optarg);
5450 break;
5451 case QEMU_OPTION_device:
Mark McLoughlinb386bec2009-10-06 12:17:01 +01005452 if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005453 exit(1);
5454 }
bellarda594cfb2005-11-06 16:13:29 +00005455 break;
bellard6a00d602005-11-21 23:25:50 +00005456 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02005457 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00005458 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005459 fprintf(stderr, "Invalid number of CPUs\n");
5460 exit(1);
5461 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005462 if (max_cpus < smp_cpus) {
5463 fprintf(stderr, "maxcpus must be equal to or greater than "
5464 "smp\n");
5465 exit(1);
5466 }
5467 if (max_cpus > 255) {
5468 fprintf(stderr, "Unsupported number of maxcpus\n");
5469 exit(1);
5470 }
bellard6a00d602005-11-21 23:25:50 +00005471 break;
bellard24236862006-04-30 21:28:36 +00005472 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005473 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005474 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005475 break;
blueswir15824d652009-03-28 06:44:27 +00005476#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005477 case QEMU_OPTION_no_acpi:
5478 acpi_enabled = 0;
5479 break;
aliguori16b29ae2008-12-17 23:28:44 +00005480 case QEMU_OPTION_no_hpet:
5481 no_hpet = 1;
5482 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005483 case QEMU_OPTION_balloon:
5484 if (balloon_parse(optarg) < 0) {
5485 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5486 exit(1);
5487 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005488 break;
blueswir15824d652009-03-28 06:44:27 +00005489#endif
bellardd1beab82006-10-02 19:44:22 +00005490 case QEMU_OPTION_no_reboot:
5491 no_reboot = 1;
5492 break;
aurel32b2f76162008-04-11 21:35:52 +00005493 case QEMU_OPTION_no_shutdown:
5494 no_shutdown = 1;
5495 break;
balrog9467cd42007-05-01 01:34:14 +00005496 case QEMU_OPTION_show_cursor:
5497 cursor_hide = 0;
5498 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005499 case QEMU_OPTION_uuid:
5500 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5501 fprintf(stderr, "Fail to parse UUID string."
5502 " Wrong format.\n");
5503 exit(1);
5504 }
5505 break;
blueswir15824d652009-03-28 06:44:27 +00005506#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005507 case QEMU_OPTION_daemonize:
5508 daemonize = 1;
5509 break;
blueswir15824d652009-03-28 06:44:27 +00005510#endif
ths9ae02552007-01-05 17:39:04 +00005511 case QEMU_OPTION_option_rom:
5512 if (nb_option_roms >= MAX_OPTION_ROMS) {
5513 fprintf(stderr, "Too many option ROMs\n");
5514 exit(1);
5515 }
5516 option_rom[nb_option_roms] = optarg;
5517 nb_option_roms++;
5518 break;
blueswir15824d652009-03-28 06:44:27 +00005519#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005520 case QEMU_OPTION_semihosting:
5521 semihosting_enabled = 1;
5522 break;
blueswir15824d652009-03-28 06:44:27 +00005523#endif
thsc35734b2007-03-19 15:17:08 +00005524 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005525 qemu_name = qemu_strdup(optarg);
5526 {
5527 char *p = strchr(qemu_name, ',');
5528 if (p != NULL) {
5529 *p++ = 0;
5530 if (strncmp(p, "process=", 8)) {
5531 fprintf(stderr, "Unknown subargument %s to -name", p);
5532 exit(1);
5533 }
5534 p += 8;
5535 set_proc_name(p);
5536 }
5537 }
thsc35734b2007-03-19 15:17:08 +00005538 break;
blueswir195efd112008-12-24 20:26:14 +00005539#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005540 case QEMU_OPTION_prom_env:
5541 if (nb_prom_envs >= MAX_PROM_ENVS) {
5542 fprintf(stderr, "Too many prom variables\n");
5543 exit(1);
5544 }
5545 prom_envs[nb_prom_envs] = optarg;
5546 nb_prom_envs++;
5547 break;
5548#endif
balrog2b8f2d42007-07-27 22:08:46 +00005549#ifdef TARGET_ARM
5550 case QEMU_OPTION_old_param:
5551 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005552 break;
balrog2b8f2d42007-07-27 22:08:46 +00005553#endif
thsf3dcfad2007-08-24 01:26:02 +00005554 case QEMU_OPTION_clock:
5555 configure_alarms(optarg);
5556 break;
bellard7e0af5d02007-11-07 16:24:33 +00005557 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005558 configure_rtc_date_offset(optarg, 1);
5559 break;
5560 case QEMU_OPTION_rtc:
5561 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL);
5562 if (!opts) {
5563 fprintf(stderr, "parse error: %s\n", optarg);
5564 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00005565 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005566 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00005567 break;
bellard26a5f132008-05-28 12:30:31 +00005568 case QEMU_OPTION_tb_size:
5569 tb_size = strtol(optarg, NULL, 0);
5570 if (tb_size < 0)
5571 tb_size = 0;
5572 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005573 case QEMU_OPTION_icount:
5574 use_icount = 1;
5575 if (strcmp(optarg, "auto") == 0) {
5576 icount_time_shift = -1;
5577 } else {
5578 icount_time_shift = strtol(optarg, NULL, 0);
5579 }
5580 break;
aliguori5bb79102008-10-13 03:12:02 +00005581 case QEMU_OPTION_incoming:
5582 incoming = optarg;
5583 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01005584 case QEMU_OPTION_nodefaults:
5585 default_serial = 0;
5586 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01005587 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01005588 default_monitor = 0;
5589 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01005590 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005591 default_floppy = 0;
5592 default_cdrom = 0;
5593 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01005594 break;
blueswir15824d652009-03-28 06:44:27 +00005595#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005596 case QEMU_OPTION_chroot:
5597 chroot_dir = optarg;
5598 break;
5599 case QEMU_OPTION_runas:
5600 run_as = optarg;
5601 break;
blueswir15824d652009-03-28 06:44:27 +00005602#endif
aliguorie37630c2009-04-22 15:19:10 +00005603#ifdef CONFIG_XEN
5604 case QEMU_OPTION_xen_domid:
5605 xen_domid = atoi(optarg);
5606 break;
5607 case QEMU_OPTION_xen_create:
5608 xen_mode = XEN_CREATE;
5609 break;
5610 case QEMU_OPTION_xen_attach:
5611 xen_mode = XEN_ATTACH;
5612 break;
5613#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02005614 case QEMU_OPTION_readconfig:
5615 {
5616 FILE *fp;
5617 fp = fopen(optarg, "r");
5618 if (fp == NULL) {
5619 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5620 exit(1);
5621 }
5622 if (qemu_config_parse(fp) != 0) {
5623 exit(1);
5624 }
5625 fclose(fp);
5626 break;
5627 }
5628 case QEMU_OPTION_writeconfig:
5629 {
5630 FILE *fp;
5631 if (strcmp(optarg, "-") == 0) {
5632 fp = stdout;
5633 } else {
5634 fp = fopen(optarg, "w");
5635 if (fp == NULL) {
5636 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5637 exit(1);
5638 }
5639 }
5640 qemu_config_write(fp);
5641 fclose(fp);
5642 break;
5643 }
bellardcd6f1162004-05-13 22:02:20 +00005644 }
bellard0824d6f2003-06-24 13:42:40 +00005645 }
5646 }
bellard330d0412003-07-26 18:11:40 +00005647
Paul Brook5cea8592009-05-30 00:52:44 +01005648 /* If no data_dir is specified then try to find it relative to the
5649 executable path. */
5650 if (!data_dir) {
5651 data_dir = find_datadir(argv[0]);
5652 }
5653 /* If all else fails use the install patch specified when building. */
5654 if (!data_dir) {
5655 data_dir = CONFIG_QEMU_SHAREDIR;
5656 }
5657
Jes Sorensen6be68d72009-07-23 17:03:42 +02005658 /*
5659 * Default to max_cpus = smp_cpus, in case the user doesn't
5660 * specify a max_cpus value.
5661 */
5662 if (!max_cpus)
5663 max_cpus = smp_cpus;
5664
balrog3d878ca2008-10-28 10:59:59 +00005665 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005666 if (smp_cpus > machine->max_cpus) {
5667 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5668 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5669 machine->max_cpus);
5670 exit(1);
5671 }
5672
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005673 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +01005674 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005675
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01005676 if (machine->no_serial) {
5677 default_serial = 0;
5678 }
5679 if (machine->no_parallel) {
5680 default_parallel = 0;
5681 }
5682 if (!machine->use_virtcon) {
5683 default_virtcon = 0;
5684 }
5685 if (machine->no_vga) {
5686 default_vga = 0;
5687 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005688 if (machine->no_floppy) {
5689 default_floppy = 0;
5690 }
5691 if (machine->no_cdrom) {
5692 default_cdrom = 0;
5693 }
5694 if (machine->no_sdcard) {
5695 default_sdcard = 0;
5696 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01005697
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005698 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005699 if (default_parallel)
5700 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01005701 if (default_serial && default_monitor) {
5702 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01005703 } else if (default_virtcon && default_monitor) {
5704 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01005705 } else {
5706 if (default_serial)
5707 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01005708 if (default_virtcon)
5709 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01005710 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01005711 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01005712 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005713 } else {
5714 if (default_serial)
5715 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005716 if (default_parallel)
5717 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005718 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01005719 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01005720 if (default_virtcon)
5721 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00005722 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01005723 if (default_vga)
5724 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00005725
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01005726 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
5727 exit(1);
5728
ths71e3ceb2006-12-22 02:11:31 +00005729#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005730 if (daemonize) {
5731 pid_t pid;
5732
5733 if (pipe(fds) == -1)
5734 exit(1);
5735
5736 pid = fork();
5737 if (pid > 0) {
5738 uint8_t status;
5739 ssize_t len;
5740
5741 close(fds[1]);
5742
5743 again:
ths93815bc2007-03-19 15:58:31 +00005744 len = read(fds[0], &status, 1);
5745 if (len == -1 && (errno == EINTR))
5746 goto again;
5747
5748 if (len != 1)
5749 exit(1);
5750 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005751 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005752 exit(1);
5753 } else
5754 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005755 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005756 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005757
Kevin Wolf40ff6d72009-12-02 12:24:42 +01005758 close(fds[0]);
5759 qemu_set_cloexec(fds[1]);
5760
ths71e3ceb2006-12-22 02:11:31 +00005761 setsid();
5762
5763 pid = fork();
5764 if (pid > 0)
5765 exit(0);
5766 else if (pid < 0)
5767 exit(1);
5768
5769 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005770
5771 signal(SIGTSTP, SIG_IGN);
5772 signal(SIGTTOU, SIG_IGN);
5773 signal(SIGTTIN, SIG_IGN);
5774 }
Juha Riihimäki099fe232009-12-03 15:56:03 +02005775#endif
ths71e3ceb2006-12-22 02:11:31 +00005776
thsaa26bb22007-03-25 21:33:06 +00005777 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Juha Riihimäki099fe232009-12-03 15:56:03 +02005778#ifndef _WIN32
ths93815bc2007-03-19 15:58:31 +00005779 if (daemonize) {
5780 uint8_t status = 1;
5781 write(fds[1], &status, 1);
5782 } else
Juha Riihimäki099fe232009-12-03 15:56:03 +02005783#endif
Justin M. Forbes850810d2009-10-01 09:42:56 -05005784 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005785 exit(1);
5786 }
5787
Marcelo Tosatti214910a2009-09-18 02:41:23 -03005788 if (kvm_enabled()) {
5789 int ret;
5790
5791 ret = kvm_init(smp_cpus);
5792 if (ret < 0) {
5793 fprintf(stderr, "failed to initialize KVM\n");
5794 exit(1);
5795 }
5796 }
5797
aliguori3fcf7b62009-04-24 18:03:25 +00005798 if (qemu_init_main_loop()) {
5799 fprintf(stderr, "qemu_init_main_loop failed\n");
5800 exit(1);
5801 }
bellarda20dd502003-09-30 21:07:02 +00005802 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005803
thsf8d39c02008-07-03 10:01:15 +00005804 if (!linux_boot && *kernel_cmdline != '\0') {
5805 fprintf(stderr, "-append only allowed with -kernel option\n");
5806 exit(1);
5807 }
5808
5809 if (!linux_boot && initrd_filename != NULL) {
5810 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5811 exit(1);
5812 }
5813
Filip Navarabf65f532009-07-27 10:02:04 -05005814#ifndef _WIN32
5815 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00005816 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05005817#endif
ths3b46e622007-09-17 08:09:54 +00005818
aliguori7183b4b2008-11-05 20:40:18 +00005819 if (init_timer_alarm() < 0) {
5820 fprintf(stderr, "could not initialize alarm timer\n");
5821 exit(1);
5822 }
pbrook2e70f6e2008-06-29 01:03:05 +00005823 if (use_icount && icount_time_shift < 0) {
5824 use_icount = 2;
5825 /* 125MIPS seems a reasonable initial guess at the guest speed.
5826 It will be corrected fairly quickly anyway. */
5827 icount_time_shift = 3;
5828 init_icount_adjust();
5829 }
pbrook634fce92006-07-15 17:40:09 +00005830
bellardfd1dff42006-02-01 21:29:26 +00005831#ifdef _WIN32
5832 socket_init();
5833#endif
5834
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01005835 if (net_init_clients() < 0) {
5836 exit(1);
bellard702c6512004-04-02 21:21:32 +00005837 }
bellardf1510b22003-06-25 00:07:40 +00005838
Glauber Costa406c8df2009-06-17 09:05:30 -04005839 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5840 net_set_boot_mask(net_boot);
5841
balrogdc72ac12008-11-09 00:04:26 +00005842 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005843 if (foreach_device_config(DEV_BT, bt_parse))
5844 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005845
bellard0824d6f2003-06-24 13:42:40 +00005846 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005847 if (ram_size == 0)
5848 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005849
bellard26a5f132008-05-28 12:30:31 +00005850 /* init the dynamic translator */
5851 cpu_exec_init_all(tb_size * 1024 * 1024);
5852
Markus Armbrustereb852012009-10-27 18:41:44 +01005853 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00005854
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005855 blk_mig_init();
5856
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005857 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01005858 /* we always create the cdrom drive, even if no disk is there */
5859 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005860 }
thse4bcb142007-12-02 04:51:10 +00005861
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005862 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01005863 /* we always create at least one floppy */
5864 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005865 }
bellardc4b1fcc2004-03-14 21:44:30 +00005866
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005867 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01005868 /* we always create one sd slot, even if no card is in it */
5869 drive_add(NULL, SD_ALIAS);
5870 }
balrog9d413d12007-12-04 00:10:34 +00005871
ths96d30e42007-01-07 20:42:14 +00005872 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005873 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02005874 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5875 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005876 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005877
Juan Quintela2faf58c2009-09-10 03:04:28 +02005878 vmstate_register(0, &vmstate_timers ,&timers_state);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005879 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
5880 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005881
aliguori268a3622009-04-21 22:30:27 +00005882 if (nb_numa_nodes > 0) {
5883 int i;
5884
5885 if (nb_numa_nodes > smp_cpus) {
5886 nb_numa_nodes = smp_cpus;
5887 }
5888
5889 /* If no memory size if given for any node, assume the default case
5890 * and distribute the available memory equally across all nodes
5891 */
5892 for (i = 0; i < nb_numa_nodes; i++) {
5893 if (node_mem[i] != 0)
5894 break;
5895 }
5896 if (i == nb_numa_nodes) {
5897 uint64_t usedmem = 0;
5898
5899 /* On Linux, the each node's border has to be 8MB aligned,
5900 * the final node gets the rest.
5901 */
5902 for (i = 0; i < nb_numa_nodes - 1; i++) {
5903 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5904 usedmem += node_mem[i];
5905 }
5906 node_mem[i] = ram_size - usedmem;
5907 }
5908
5909 for (i = 0; i < nb_numa_nodes; i++) {
5910 if (node_cpumask[i] != 0)
5911 break;
5912 }
5913 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5914 * must cope with this anyway, because there are BIOSes out there in
5915 * real machines which also use this scheme.
5916 */
5917 if (i == nb_numa_nodes) {
5918 for (i = 0; i < smp_cpus; i++) {
5919 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5920 }
5921 }
5922 }
5923
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005924 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
5925 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005926 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
5927 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01005928 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
5929 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00005930
Paul Brookaae94602009-05-14 22:35:06 +01005931 module_call_init(MODULE_INIT_DEVICE);
5932
Markus Armbruster09aaa162009-08-21 10:31:34 +02005933 if (watchdog) {
5934 i = select_watchdog(watchdog);
5935 if (i > 0)
5936 exit (i == 1 ? 1 : 0);
5937 }
5938
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005939 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01005940 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005941 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01005942 qemu_add_globals();
5943
Paul Brookfbe1b592009-05-13 17:56:25 +01005944 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005945 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5946
aliguori268a3622009-04-21 22:30:27 +00005947
Juan Quintela67b3b712009-08-28 19:25:15 +02005948#ifndef _WIN32
5949 /* must be after terminal init, SDL library changes signal handlers */
5950 sighandler_setup();
5951#endif
5952
aliguori268a3622009-04-21 22:30:27 +00005953 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5954 for (i = 0; i < nb_numa_nodes; i++) {
5955 if (node_cpumask[i] & (1 << env->cpu_index)) {
5956 env->numa_node = i;
5957 }
5958 }
5959 }
5960
aliguori6f338c32009-02-11 15:21:54 +00005961 current_machine = machine;
5962
aliguori3023f332009-01-16 19:04:14 +00005963 /* init USB devices */
5964 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01005965 if (foreach_device_config(DEV_USB, usb_parse) < 0)
5966 exit(1);
aliguori3023f332009-01-16 19:04:14 +00005967 }
5968
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005969 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005970 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005971 exit(1);
5972
aliguori8f391ab2009-01-19 16:34:10 +00005973 if (!display_state)
5974 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005975 /* just use the first displaystate for the moment */
5976 ds = display_state;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005977
5978 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05005979#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005980 display_type = DT_SDL;
5981#else
5982 display_type = DT_VNC;
5983 vnc_display = "localhost:0,to=99";
5984 show_vnc_port = 1;
5985#endif
5986 }
5987
5988
5989 switch (display_type) {
5990 case DT_NOGRAPHIC:
5991 break;
5992#if defined(CONFIG_CURSES)
5993 case DT_CURSES:
5994 curses_display_init(ds, full_screen);
5995 break;
5996#endif
bellard5b0753e2005-03-01 21:37:28 +00005997#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005998 case DT_SDL:
5999 sdl_display_init(ds, full_screen, no_frame);
6000 break;
bellard5b0753e2005-03-01 21:37:28 +00006001#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006002 case DT_SDL:
6003 cocoa_display_init(ds, full_screen);
6004 break;
bellard313aa562003-08-10 21:52:11 +00006005#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006006 case DT_VNC:
6007 vnc_display_init(ds);
6008 if (vnc_display_open(ds, vnc_display) < 0)
6009 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006010
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006011 if (show_vnc_port) {
6012 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006013 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006014 break;
6015 default:
6016 break;
bellard313aa562003-08-10 21:52:11 +00006017 }
aliguori7d957bd2009-01-15 22:14:11 +00006018 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00006019
aliguori3023f332009-01-16 19:04:14 +00006020 dcl = ds->listeners;
6021 while (dcl != NULL) {
6022 if (dcl->dpy_refresh != NULL) {
6023 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6024 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00006025 }
aliguori3023f332009-01-16 19:04:14 +00006026 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00006027 }
aliguori3023f332009-01-16 19:04:14 +00006028
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006029 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00006030 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6031 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6032 }
6033
aliguori2796dae2009-01-16 20:23:27 +00006034 text_consoles_set_display(display_state);
6035
Gerd Hoffmann88589342009-12-08 13:11:50 +01006036 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0)
6037 exit(1);
bellard82c643f2004-07-14 17:28:13 +00006038
aliguori59030a82009-04-05 18:43:41 +00006039 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6040 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6041 gdbstub_dev);
6042 exit(1);
balrog45669e02007-07-02 13:20:17 +00006043 }
balrog45669e02007-07-02 13:20:17 +00006044
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02006045 qdev_machine_creation_done();
6046
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01006047 if (rom_load_all() != 0) {
6048 fprintf(stderr, "rom loading failed\n");
6049 exit(1);
6050 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02006051
Juan Quintela504c2942009-11-12 00:39:13 +01006052 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02006053 if (loadvm) {
6054 if (load_vmstate(cur_mon, loadvm) < 0) {
6055 autostart = 0;
6056 }
6057 }
bellardd63d3072004-10-03 13:29:03 +00006058
Glauber Costa2bb8c102009-07-24 16:20:23 -04006059 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00006060 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03006061 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00006062 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03006063 }
thsffd843b2006-12-21 19:46:43 +00006064
blueswir1b9e82a52009-04-05 18:03:31 +00006065#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00006066 if (daemonize) {
6067 uint8_t status = 0;
6068 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00006069
6070 again1:
6071 len = write(fds[1], &status, 1);
6072 if (len == -1 && (errno == EINTR))
6073 goto again1;
6074
6075 if (len != 1)
6076 exit(1);
6077
aliguoribd54b862008-07-23 00:58:33 +00006078 chdir("/");
Kevin Wolf40ff6d72009-12-02 12:24:42 +01006079 TFR(fd = qemu_open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00006080 if (fd == -1)
6081 exit(1);
aliguori08585322009-02-27 22:09:45 +00006082 }
ths71e3ceb2006-12-22 02:11:31 +00006083
aliguori08585322009-02-27 22:09:45 +00006084 if (run_as) {
6085 pwd = getpwnam(run_as);
6086 if (!pwd) {
6087 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6088 exit(1);
6089 }
6090 }
ths71e3ceb2006-12-22 02:11:31 +00006091
aliguori08585322009-02-27 22:09:45 +00006092 if (chroot_dir) {
6093 if (chroot(chroot_dir) < 0) {
6094 fprintf(stderr, "chroot failed\n");
6095 exit(1);
6096 }
6097 chdir("/");
6098 }
6099
6100 if (run_as) {
6101 if (setgid(pwd->pw_gid) < 0) {
6102 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6103 exit(1);
6104 }
6105 if (setuid(pwd->pw_uid) < 0) {
6106 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6107 exit(1);
6108 }
6109 if (setuid(0) != -1) {
6110 fprintf(stderr, "Dropping privileges failed\n");
6111 exit(1);
6112 }
6113 }
aliguori08585322009-02-27 22:09:45 +00006114
6115 if (daemonize) {
6116 dup2(fd, 0);
6117 dup2(fd, 1);
6118 dup2(fd, 2);
6119
6120 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006121 }
blueswir1b9e82a52009-04-05 18:03:31 +00006122#endif
ths71e3ceb2006-12-22 02:11:31 +00006123
bellard8a7ddc32004-03-31 19:00:16 +00006124 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006125 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006126 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006127
bellard0824d6f2003-06-24 13:42:40 +00006128 return 0;
6129}