bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1 | /* |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 2 | * QEMU System Emulator |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 68d0f70 | 2008-01-06 17:21:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 1df912c | 2003-06-25 16:20:35 +0000 | [diff] [blame] | 6 | * 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. |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 23 | */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | #include <signal.h> |
| 27 | #include <time.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 28 | #include <errno.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 29 | #include <sys/time.h> |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 30 | #include <zlib.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 31 | |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 32 | /* Needed early for CONFIG_BSD etc. */ |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 33 | #include "config-host.h" |
Blue Swirl | 96555a9 | 2009-07-17 11:01:45 +0000 | [diff] [blame] | 34 | /* Needed early to override system queue definitions on BSD */ |
| 35 | #include "sys-queue.h" |
blueswir1 | d40cdb1 | 2009-03-07 16:52:02 +0000 | [diff] [blame] | 36 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 37 | #ifndef _WIN32 |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 38 | #include <libgen.h> |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 39 | #include <pwd.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 40 | #include <sys/times.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 41 | #include <sys/wait.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 42 | #include <termios.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 43 | #include <sys/mman.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 44 | #include <sys/ioctl.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 45 | #include <sys/resource.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 46 | #include <sys/socket.h> |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 47 | #include <netinet/in.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 48 | #include <net/if.h> |
| 49 | #if defined(__NetBSD__) |
| 50 | #include <net/if_tap.h> |
| 51 | #endif |
| 52 | #ifdef __linux__ |
| 53 | #include <linux/if_tun.h> |
| 54 | #endif |
| 55 | #include <arpa/inet.h> |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 56 | #include <dirent.h> |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 57 | #include <netdb.h> |
ths | cb4b976 | 2007-09-13 12:39:35 +0000 | [diff] [blame] | 58 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 59 | #ifdef CONFIG_BSD |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 60 | #include <sys/stat.h> |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 61 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 62 | #include <libutil.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 63 | #else |
| 64 | #include <util.h> |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 65 | #endif |
ths | 5c40d2b | 2007-06-23 16:03:36 +0000 | [diff] [blame] | 66 | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__) |
| 67 | #include <freebsd/stdlib.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 68 | #else |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 69 | #ifdef __linux__ |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 70 | #include <pty.h> |
| 71 | #include <malloc.h> |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 72 | #include <linux/rtc.h> |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 73 | #include <sys/prctl.h> |
ths | bd494f4 | 2007-09-16 20:03:23 +0000 | [diff] [blame] | 74 | |
| 75 | /* For the benefit of older linux systems which don't supply it, |
| 76 | we use a local copy of hpet.h. */ |
| 77 | /* #include <linux/hpet.h> */ |
| 78 | #include "hpet.h" |
| 79 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 80 | #include <linux/ppdev.h> |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 81 | #include <linux/parport.h> |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 82 | #endif |
| 83 | #ifdef __sun__ |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 84 | #include <sys/stat.h> |
| 85 | #include <sys/ethernet.h> |
| 86 | #include <sys/sockio.h> |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 87 | #include <netinet/arp.h> |
| 88 | #include <netinet/in.h> |
| 89 | #include <netinet/in_systm.h> |
| 90 | #include <netinet/ip.h> |
| 91 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 92 | #include <netinet/udp.h> |
| 93 | #include <netinet/tcp.h> |
| 94 | #include <net/if.h> |
| 95 | #include <syslog.h> |
| 96 | #include <stropts.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 97 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 98 | #endif |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 99 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 100 | |
blueswir1 | 9892fbf | 2008-08-24 10:34:20 +0000 | [diff] [blame] | 101 | #if defined(__OpenBSD__) |
| 102 | #include <util.h> |
| 103 | #endif |
| 104 | |
ths | 8a16d27 | 2008-07-19 09:56:24 +0000 | [diff] [blame] | 105 | #if defined(CONFIG_VDE) |
| 106 | #include <libvdeplug.h> |
| 107 | #endif |
| 108 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 109 | #ifdef _WIN32 |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 110 | #include <windows.h> |
ths | 4fddf62 | 2007-12-17 04:42:29 +0000 | [diff] [blame] | 111 | #include <mmsystem.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 112 | #endif |
| 113 | |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 114 | #ifdef CONFIG_SDL |
Stefan Weil | 59a36a2 | 2009-06-18 20:11:03 +0200 | [diff] [blame] | 115 | #if defined(__APPLE__) || defined(main) |
Stefan Weil | 6693665 | 2009-06-13 13:19:11 +0200 | [diff] [blame] | 116 | #include <SDL.h> |
malc | 880fec5 | 2009-02-15 20:18:41 +0000 | [diff] [blame] | 117 | int qemu_main(int argc, char **argv, char **envp); |
| 118 | int main(int argc, char **argv) |
| 119 | { |
Stefan Weil | 59a36a2 | 2009-06-18 20:11:03 +0200 | [diff] [blame] | 120 | return qemu_main(argc, argv, NULL); |
malc | 880fec5 | 2009-02-15 20:18:41 +0000 | [diff] [blame] | 121 | } |
| 122 | #undef main |
| 123 | #define main qemu_main |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 124 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 125 | #endif /* CONFIG_SDL */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 126 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 127 | #ifdef CONFIG_COCOA |
| 128 | #undef main |
| 129 | #define main qemu_main |
| 130 | #endif /* CONFIG_COCOA */ |
| 131 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 132 | #include "hw/hw.h" |
| 133 | #include "hw/boards.h" |
| 134 | #include "hw/usb.h" |
| 135 | #include "hw/pcmcia.h" |
| 136 | #include "hw/pc.h" |
| 137 | #include "hw/audiodev.h" |
| 138 | #include "hw/isa.h" |
| 139 | #include "hw/baum.h" |
| 140 | #include "hw/bt.h" |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 141 | #include "hw/watchdog.h" |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 142 | #include "hw/smbios.h" |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 143 | #include "hw/xen.h" |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 144 | #include "hw/qdev.h" |
aurel32 | 5ef4efa | 2009-03-10 21:43:35 +0000 | [diff] [blame] | 145 | #include "bt-host.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 146 | #include "net.h" |
| 147 | #include "monitor.h" |
| 148 | #include "console.h" |
| 149 | #include "sysemu.h" |
| 150 | #include "gdbstub.h" |
| 151 | #include "qemu-timer.h" |
| 152 | #include "qemu-char.h" |
| 153 | #include "cache-utils.h" |
| 154 | #include "block.h" |
blueswir1 | a718ace | 2009-03-28 08:24:44 +0000 | [diff] [blame] | 155 | #include "dma.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 156 | #include "audio/audio.h" |
| 157 | #include "migration.h" |
| 158 | #include "kvm.h" |
| 159 | #include "balloon.h" |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 160 | #include "qemu-option.h" |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 161 | #include "qemu-config.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 162 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 163 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 164 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 165 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 166 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 167 | #include "qemu_socket.h" |
| 168 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 169 | #include "slirp/libslirp.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 170 | |
blueswir1 | 9dc63a1 | 2008-10-04 07:25:46 +0000 | [diff] [blame] | 171 | //#define DEBUG_NET |
| 172 | //#define DEBUG_SLIRP |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 173 | |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 174 | #define DEFAULT_RAM_SIZE 128 |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 175 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 176 | static const char *data_dir; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 177 | const char *bios_name = NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 178 | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 179 | to store the VM snapshots */ |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 180 | struct drivelist drives = TAILQ_HEAD_INITIALIZER(drives); |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 181 | struct driveoptlist driveopts = TAILQ_HEAD_INITIALIZER(driveopts); |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 182 | enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 183 | static DisplayState *display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 184 | DisplayType display_type = DT_DEFAULT; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 185 | const char* keyboard_layout = NULL; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 186 | int64_t ticks_per_sec; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 187 | ram_addr_t ram_size; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 188 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 189 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 190 | int vm_running; |
Paolo Bonzini | d399f67 | 2009-07-27 23:17:51 +0200 | [diff] [blame] | 191 | int autostart; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 192 | static int rtc_utc = 1; |
| 193 | static int rtc_date_offset = -1; /* -1 means no change */ |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 194 | int vga_interface_type = VGA_CIRRUS; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 195 | #ifdef TARGET_SPARC |
| 196 | int graphic_width = 1024; |
| 197 | int graphic_height = 768; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 198 | int graphic_depth = 8; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 199 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 200 | int graphic_width = 800; |
| 201 | int graphic_height = 600; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 202 | int graphic_depth = 15; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 203 | #endif |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 204 | static int full_screen = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 205 | #ifdef CONFIG_SDL |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 206 | static int no_frame = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 207 | #endif |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 208 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 209 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 210 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 211 | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 212 | #ifdef TARGET_I386 |
| 213 | int win2k_install_hack = 0; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 214 | int rtc_td_hack = 0; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 215 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 216 | int usb_enabled = 0; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 217 | int singlestep = 0; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 218 | int smp_cpus = 1; |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 219 | int max_cpus = 0; |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 220 | int smp_cores = 1; |
| 221 | int smp_threads = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 222 | const char *vnc_display; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 223 | int acpi_enabled = 1; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 224 | int no_hpet = 0; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 225 | int virtio_balloon = 1; |
| 226 | const char *virtio_balloon_devaddr; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 227 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 228 | int no_reboot = 0; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 229 | int no_shutdown = 0; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 230 | int cursor_hide = 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 231 | int graphic_rotate = 0; |
Jes Sorensen | 6b35e7b | 2009-08-06 16:25:50 +0200 | [diff] [blame] | 232 | uint8_t irq0override = 1; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 233 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 234 | int daemonize = 0; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 235 | #endif |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame^] | 236 | const char *watchdog; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 237 | const char *option_rom[MAX_OPTION_ROMS]; |
| 238 | int nb_option_roms; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 239 | int semihosting_enabled = 0; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 240 | #ifdef TARGET_ARM |
| 241 | int old_param = 0; |
| 242 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 243 | const char *qemu_name; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 244 | int alt_grab = 0; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 245 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 246 | unsigned int nb_prom_envs = 0; |
| 247 | const char *prom_envs[MAX_PROM_ENVS]; |
| 248 | #endif |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 249 | int boot_menu; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 250 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 251 | int nb_numa_nodes; |
| 252 | uint64_t node_mem[MAX_NODES]; |
| 253 | uint64_t node_cpumask[MAX_NODES]; |
| 254 | |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 255 | static CPUState *cur_cpu; |
| 256 | static CPUState *next_cpu; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 257 | static int timer_alarm_pending = 1; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 258 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 259 | static int icount_time_shift; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 260 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 261 | #define MAX_ICOUNT_SHIFT 10 |
| 262 | /* Compensate for varying guest execution speed. */ |
| 263 | static int64_t qemu_icount_bias; |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 264 | static QEMUTimer *icount_rt_timer; |
| 265 | static QEMUTimer *icount_vm_timer; |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 266 | static QEMUTimer *nographic_timer; |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 267 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 268 | uint8_t qemu_uuid[16]; |
| 269 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 270 | static QEMUBootSetHandler *boot_set_handler; |
| 271 | static void *boot_set_opaque; |
| 272 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 273 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 274 | /* x86 ISA bus support */ |
| 275 | |
| 276 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 277 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 278 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 279 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 280 | void hw_error(const char *fmt, ...) |
| 281 | { |
| 282 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 283 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 284 | |
| 285 | va_start(ap, fmt); |
| 286 | fprintf(stderr, "qemu: hardware error: "); |
| 287 | vfprintf(stderr, fmt, ap); |
| 288 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 289 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 290 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 291 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 292 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 293 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 294 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 295 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 296 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 297 | va_end(ap); |
| 298 | abort(); |
| 299 | } |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 300 | |
| 301 | static void set_proc_name(const char *s) |
| 302 | { |
Nathan Froyd | 6ca8d0f | 2009-08-03 07:32:12 -0700 | [diff] [blame] | 303 | #if defined(__linux__) && defined(PR_SET_NAME) |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 304 | char name[16]; |
| 305 | if (!s) |
| 306 | return; |
| 307 | name[sizeof(name) - 1] = 0; |
| 308 | strncpy(name, s, sizeof(name)); |
| 309 | /* Could rewrite argv[0] too, but that's a bit more complicated. |
| 310 | This simple way is enough for `top'. */ |
| 311 | prctl(PR_SET_NAME, name); |
| 312 | #endif |
| 313 | } |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 314 | |
| 315 | /***************/ |
| 316 | /* ballooning */ |
| 317 | |
| 318 | static QEMUBalloonEvent *qemu_balloon_event; |
| 319 | void *qemu_balloon_event_opaque; |
| 320 | |
| 321 | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) |
| 322 | { |
| 323 | qemu_balloon_event = func; |
| 324 | qemu_balloon_event_opaque = opaque; |
| 325 | } |
| 326 | |
| 327 | void qemu_balloon(ram_addr_t target) |
| 328 | { |
| 329 | if (qemu_balloon_event) |
| 330 | qemu_balloon_event(qemu_balloon_event_opaque, target); |
| 331 | } |
| 332 | |
| 333 | ram_addr_t qemu_balloon_status(void) |
| 334 | { |
| 335 | if (qemu_balloon_event) |
| 336 | return qemu_balloon_event(qemu_balloon_event_opaque, 0); |
| 337 | return 0; |
| 338 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 339 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 340 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 341 | /* keyboard/mouse */ |
| 342 | |
| 343 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 344 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 345 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 346 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 347 | |
| 348 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 349 | { |
| 350 | qemu_put_kbd_event_opaque = opaque; |
| 351 | qemu_put_kbd_event = func; |
| 352 | } |
| 353 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 354 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 355 | void *opaque, int absolute, |
| 356 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 357 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 358 | QEMUPutMouseEntry *s, *cursor; |
| 359 | |
| 360 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 361 | |
| 362 | s->qemu_put_mouse_event = func; |
| 363 | s->qemu_put_mouse_event_opaque = opaque; |
| 364 | s->qemu_put_mouse_event_absolute = absolute; |
| 365 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 366 | s->next = NULL; |
| 367 | |
| 368 | if (!qemu_put_mouse_event_head) { |
| 369 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 370 | return s; |
| 371 | } |
| 372 | |
| 373 | cursor = qemu_put_mouse_event_head; |
| 374 | while (cursor->next != NULL) |
| 375 | cursor = cursor->next; |
| 376 | |
| 377 | cursor->next = s; |
| 378 | qemu_put_mouse_event_current = s; |
| 379 | |
| 380 | return s; |
| 381 | } |
| 382 | |
| 383 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 384 | { |
| 385 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 386 | |
| 387 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 388 | return; |
| 389 | |
| 390 | cursor = qemu_put_mouse_event_head; |
| 391 | while (cursor != NULL && cursor != entry) { |
| 392 | prev = cursor; |
| 393 | cursor = cursor->next; |
| 394 | } |
| 395 | |
| 396 | if (cursor == NULL) // does not exist or list empty |
| 397 | return; |
| 398 | else if (prev == NULL) { // entry is head |
| 399 | qemu_put_mouse_event_head = cursor->next; |
| 400 | if (qemu_put_mouse_event_current == entry) |
| 401 | qemu_put_mouse_event_current = cursor->next; |
| 402 | qemu_free(entry->qemu_put_mouse_event_name); |
| 403 | qemu_free(entry); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | prev->next = entry->next; |
| 408 | |
| 409 | if (qemu_put_mouse_event_current == entry) |
| 410 | qemu_put_mouse_event_current = prev; |
| 411 | |
| 412 | qemu_free(entry->qemu_put_mouse_event_name); |
| 413 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void kbd_put_keycode(int keycode) |
| 417 | { |
| 418 | if (qemu_put_kbd_event) { |
| 419 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 424 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 425 | QEMUPutMouseEvent *mouse_event; |
| 426 | void *mouse_event_opaque; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 427 | int width; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 428 | |
| 429 | if (!qemu_put_mouse_event_current) { |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | mouse_event = |
| 434 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 435 | mouse_event_opaque = |
| 436 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 437 | |
| 438 | if (mouse_event) { |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 439 | if (graphic_rotate) { |
| 440 | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute) |
| 441 | width = 0x7fff; |
| 442 | else |
aurel32 | b94ed57 | 2008-03-10 19:34:27 +0000 | [diff] [blame] | 443 | width = graphic_width - 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 444 | mouse_event(mouse_event_opaque, |
| 445 | width - dy, dx, dz, buttons_state); |
| 446 | } else |
| 447 | mouse_event(mouse_event_opaque, |
| 448 | dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 452 | int kbd_mouse_is_absolute(void) |
| 453 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 454 | if (!qemu_put_mouse_event_current) |
| 455 | return 0; |
| 456 | |
| 457 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 458 | } |
| 459 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 460 | void do_info_mice(Monitor *mon) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 461 | { |
| 462 | QEMUPutMouseEntry *cursor; |
| 463 | int index = 0; |
| 464 | |
| 465 | if (!qemu_put_mouse_event_head) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 466 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 467 | return; |
| 468 | } |
| 469 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 470 | monitor_printf(mon, "Mouse devices available:\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 471 | cursor = qemu_put_mouse_event_head; |
| 472 | while (cursor != NULL) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 473 | monitor_printf(mon, "%c Mouse #%d: %s\n", |
| 474 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
| 475 | index, cursor->qemu_put_mouse_event_name); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 476 | index++; |
| 477 | cursor = cursor->next; |
| 478 | } |
| 479 | } |
| 480 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 481 | void do_mouse_set(Monitor *mon, int index) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 482 | { |
| 483 | QEMUPutMouseEntry *cursor; |
| 484 | int i = 0; |
| 485 | |
| 486 | if (!qemu_put_mouse_event_head) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 487 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
| 491 | cursor = qemu_put_mouse_event_head; |
| 492 | while (cursor != NULL && index != i) { |
| 493 | i++; |
| 494 | cursor = cursor->next; |
| 495 | } |
| 496 | |
| 497 | if (cursor != NULL) |
| 498 | qemu_put_mouse_event_current = cursor; |
| 499 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 500 | monitor_printf(mon, "Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 501 | } |
| 502 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 503 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 504 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 505 | { |
| 506 | union { |
| 507 | uint64_t ll; |
| 508 | struct { |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 509 | #ifdef HOST_WORDS_BIGENDIAN |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 510 | uint32_t high, low; |
| 511 | #else |
| 512 | uint32_t low, high; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 513 | #endif |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 514 | } l; |
| 515 | } u, res; |
| 516 | uint64_t rl, rh; |
| 517 | |
| 518 | u.ll = a; |
| 519 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 520 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 521 | rh += (rl >> 32); |
| 522 | res.l.high = rh / c; |
| 523 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 524 | return res.ll; |
| 525 | } |
| 526 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 527 | /***********************************************************/ |
| 528 | /* real time host monotonic timer */ |
| 529 | |
| 530 | #define QEMU_TIMER_BASE 1000000000LL |
| 531 | |
| 532 | #ifdef WIN32 |
| 533 | |
| 534 | static int64_t clock_freq; |
| 535 | |
| 536 | static void init_get_clock(void) |
| 537 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 538 | LARGE_INTEGER freq; |
| 539 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 540 | ret = QueryPerformanceFrequency(&freq); |
| 541 | if (ret == 0) { |
| 542 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 543 | exit(1); |
| 544 | } |
| 545 | clock_freq = freq.QuadPart; |
| 546 | } |
| 547 | |
| 548 | static int64_t get_clock(void) |
| 549 | { |
| 550 | LARGE_INTEGER ti; |
| 551 | QueryPerformanceCounter(&ti); |
| 552 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); |
| 553 | } |
| 554 | |
| 555 | #else |
| 556 | |
| 557 | static int use_rt_clock; |
| 558 | |
| 559 | static void init_get_clock(void) |
| 560 | { |
| 561 | use_rt_clock = 0; |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 562 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
| 563 | || defined(__DragonFly__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 564 | { |
| 565 | struct timespec ts; |
| 566 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 567 | use_rt_clock = 1; |
| 568 | } |
| 569 | } |
| 570 | #endif |
| 571 | } |
| 572 | |
| 573 | static int64_t get_clock(void) |
| 574 | { |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 575 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
| 576 | || defined(__DragonFly__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 577 | if (use_rt_clock) { |
| 578 | struct timespec ts; |
| 579 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 580 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 581 | } else |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 582 | #endif |
| 583 | { |
| 584 | /* XXX: using gettimeofday leads to problems if the date |
| 585 | changes, so it should be avoided. */ |
| 586 | struct timeval tv; |
| 587 | gettimeofday(&tv, NULL); |
| 588 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 589 | } |
| 590 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 591 | #endif |
| 592 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 593 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 594 | static int64_t cpu_get_icount(void) |
| 595 | { |
| 596 | int64_t icount; |
| 597 | CPUState *env = cpu_single_env;; |
| 598 | icount = qemu_icount; |
| 599 | if (env) { |
| 600 | if (!can_do_io(env)) |
| 601 | fprintf(stderr, "Bad clock read\n"); |
| 602 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 603 | } |
| 604 | return qemu_icount_bias + (icount << icount_time_shift); |
| 605 | } |
| 606 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 607 | /***********************************************************/ |
| 608 | /* guest cycle counter */ |
| 609 | |
| 610 | static int64_t cpu_ticks_prev; |
| 611 | static int64_t cpu_ticks_offset; |
| 612 | static int64_t cpu_clock_offset; |
| 613 | static int cpu_ticks_enabled; |
| 614 | |
| 615 | /* return the host CPU cycle counter and handle stop/restart */ |
| 616 | int64_t cpu_get_ticks(void) |
| 617 | { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 618 | if (use_icount) { |
| 619 | return cpu_get_icount(); |
| 620 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 621 | if (!cpu_ticks_enabled) { |
| 622 | return cpu_ticks_offset; |
| 623 | } else { |
| 624 | int64_t ticks; |
| 625 | ticks = cpu_get_real_ticks(); |
| 626 | if (cpu_ticks_prev > ticks) { |
| 627 | /* Note: non increasing ticks may happen if the host uses |
| 628 | software suspend */ |
| 629 | cpu_ticks_offset += cpu_ticks_prev - ticks; |
| 630 | } |
| 631 | cpu_ticks_prev = ticks; |
| 632 | return ticks + cpu_ticks_offset; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 637 | static int64_t cpu_get_clock(void) |
| 638 | { |
| 639 | int64_t ti; |
| 640 | if (!cpu_ticks_enabled) { |
| 641 | return cpu_clock_offset; |
| 642 | } else { |
| 643 | ti = get_clock(); |
| 644 | return ti + cpu_clock_offset; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /* enable cpu_get_ticks() */ |
| 649 | void cpu_enable_ticks(void) |
| 650 | { |
| 651 | if (!cpu_ticks_enabled) { |
| 652 | cpu_ticks_offset -= cpu_get_real_ticks(); |
| 653 | cpu_clock_offset -= get_clock(); |
| 654 | cpu_ticks_enabled = 1; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 659 | cpu_get_ticks() after that. */ |
| 660 | void cpu_disable_ticks(void) |
| 661 | { |
| 662 | if (cpu_ticks_enabled) { |
| 663 | cpu_ticks_offset = cpu_get_ticks(); |
| 664 | cpu_clock_offset = cpu_get_clock(); |
| 665 | cpu_ticks_enabled = 0; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /***********************************************************/ |
| 670 | /* timers */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 671 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 672 | #define QEMU_TIMER_REALTIME 0 |
| 673 | #define QEMU_TIMER_VIRTUAL 1 |
| 674 | |
| 675 | struct QEMUClock { |
| 676 | int type; |
| 677 | /* XXX: add frequency */ |
| 678 | }; |
| 679 | |
| 680 | struct QEMUTimer { |
| 681 | QEMUClock *clock; |
| 682 | int64_t expire_time; |
| 683 | QEMUTimerCB *cb; |
| 684 | void *opaque; |
| 685 | struct QEMUTimer *next; |
| 686 | }; |
| 687 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 688 | struct qemu_alarm_timer { |
| 689 | char const *name; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 690 | unsigned int flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 691 | |
| 692 | int (*start)(struct qemu_alarm_timer *t); |
| 693 | void (*stop)(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 694 | void (*rearm)(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 695 | void *priv; |
| 696 | }; |
| 697 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 698 | #define ALARM_FLAG_DYNTICKS 0x1 |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 699 | #define ALARM_FLAG_EXPIRED 0x2 |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 700 | |
| 701 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
| 702 | { |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 703 | return t && (t->flags & ALARM_FLAG_DYNTICKS); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
| 707 | { |
| 708 | if (!alarm_has_dynticks(t)) |
| 709 | return; |
| 710 | |
| 711 | t->rearm(t); |
| 712 | } |
| 713 | |
| 714 | /* TODO: MIN_TIMER_REARM_US should be optimized */ |
| 715 | #define MIN_TIMER_REARM_US 250 |
| 716 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 717 | static struct qemu_alarm_timer *alarm_timer; |
| 718 | |
| 719 | #ifdef _WIN32 |
| 720 | |
| 721 | struct qemu_alarm_win32 { |
| 722 | MMRESULT timerId; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 723 | unsigned int period; |
Blue Swirl | ef28c4b | 2009-04-25 12:56:37 +0000 | [diff] [blame] | 724 | } alarm_win32_data = {0, -1}; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 725 | |
| 726 | static int win32_start_timer(struct qemu_alarm_timer *t); |
| 727 | static void win32_stop_timer(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 728 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 729 | |
| 730 | #else |
| 731 | |
| 732 | static int unix_start_timer(struct qemu_alarm_timer *t); |
| 733 | static void unix_stop_timer(struct qemu_alarm_timer *t); |
| 734 | |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 735 | #ifdef __linux__ |
| 736 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 737 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
| 738 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
| 739 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
| 740 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 741 | static int hpet_start_timer(struct qemu_alarm_timer *t); |
| 742 | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
| 743 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 744 | static int rtc_start_timer(struct qemu_alarm_timer *t); |
| 745 | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
| 746 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 747 | #endif /* __linux__ */ |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 748 | |
| 749 | #endif /* _WIN32 */ |
| 750 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 751 | /* Correlation between real and virtual time is always going to be |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 752 | fairly approximate, so ignore small variation. |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 753 | When the guest is idle real and virtual time will be aligned in |
| 754 | the IO wait loop. */ |
| 755 | #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) |
| 756 | |
| 757 | static void icount_adjust(void) |
| 758 | { |
| 759 | int64_t cur_time; |
| 760 | int64_t cur_icount; |
| 761 | int64_t delta; |
| 762 | static int64_t last_delta; |
| 763 | /* If the VM is not running, then do nothing. */ |
| 764 | if (!vm_running) |
| 765 | return; |
| 766 | |
| 767 | cur_time = cpu_get_clock(); |
| 768 | cur_icount = qemu_get_clock(vm_clock); |
| 769 | delta = cur_icount - cur_time; |
| 770 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 771 | if (delta > 0 |
| 772 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 773 | && icount_time_shift > 0) { |
| 774 | /* The guest is getting too far ahead. Slow time down. */ |
| 775 | icount_time_shift--; |
| 776 | } |
| 777 | if (delta < 0 |
| 778 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 779 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 780 | /* The guest is getting too far behind. Speed time up. */ |
| 781 | icount_time_shift++; |
| 782 | } |
| 783 | last_delta = delta; |
| 784 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
| 785 | } |
| 786 | |
| 787 | static void icount_adjust_rt(void * opaque) |
| 788 | { |
| 789 | qemu_mod_timer(icount_rt_timer, |
| 790 | qemu_get_clock(rt_clock) + 1000); |
| 791 | icount_adjust(); |
| 792 | } |
| 793 | |
| 794 | static void icount_adjust_vm(void * opaque) |
| 795 | { |
| 796 | qemu_mod_timer(icount_vm_timer, |
| 797 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); |
| 798 | icount_adjust(); |
| 799 | } |
| 800 | |
| 801 | static void init_icount_adjust(void) |
| 802 | { |
| 803 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 804 | The realtime trigger catches emulated time passing too slowly, |
| 805 | the virtual time trigger catches emulated time passing too fast. |
| 806 | Realtime triggers occur even when idle, so use them less frequently |
| 807 | than VM triggers. */ |
| 808 | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL); |
| 809 | qemu_mod_timer(icount_rt_timer, |
| 810 | qemu_get_clock(rt_clock) + 1000); |
| 811 | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL); |
| 812 | qemu_mod_timer(icount_vm_timer, |
| 813 | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10); |
| 814 | } |
| 815 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 816 | static struct qemu_alarm_timer alarm_timers[] = { |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 817 | #ifndef _WIN32 |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 818 | #ifdef __linux__ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 819 | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer, |
| 820 | dynticks_stop_timer, dynticks_rearm_timer, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 821 | /* HPET - if available - is preferred */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 822 | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 823 | /* ...otherwise try RTC */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 824 | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 825 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 826 | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 827 | #else |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 828 | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer, |
| 829 | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
| 830 | {"win32", 0, win32_start_timer, |
| 831 | win32_stop_timer, NULL, &alarm_win32_data}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 832 | #endif |
| 833 | {NULL, } |
| 834 | }; |
| 835 | |
blueswir1 | 3f47aa8 | 2008-03-09 06:59:01 +0000 | [diff] [blame] | 836 | static void show_available_alarms(void) |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 837 | { |
| 838 | int i; |
| 839 | |
| 840 | printf("Available alarm timers, in order of precedence:\n"); |
| 841 | for (i = 0; alarm_timers[i].name; i++) |
| 842 | printf("%s\n", alarm_timers[i].name); |
| 843 | } |
| 844 | |
| 845 | static void configure_alarms(char const *opt) |
| 846 | { |
| 847 | int i; |
| 848 | int cur = 0; |
malc | b1503cd | 2008-12-22 20:33:55 +0000 | [diff] [blame] | 849 | int count = ARRAY_SIZE(alarm_timers) - 1; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 850 | char *arg; |
| 851 | char *name; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 852 | struct qemu_alarm_timer tmp; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 853 | |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 854 | if (!strcmp(opt, "?")) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 855 | show_available_alarms(); |
| 856 | exit(0); |
| 857 | } |
| 858 | |
| 859 | arg = strdup(opt); |
| 860 | |
| 861 | /* Reorder the array */ |
| 862 | name = strtok(arg, ","); |
| 863 | while (name) { |
balrog | e2b577e | 2007-09-17 21:25:20 +0000 | [diff] [blame] | 864 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 865 | if (!strcmp(alarm_timers[i].name, name)) |
| 866 | break; |
| 867 | } |
| 868 | |
| 869 | if (i == count) { |
| 870 | fprintf(stderr, "Unknown clock %s\n", name); |
| 871 | goto next; |
| 872 | } |
| 873 | |
| 874 | if (i < cur) |
| 875 | /* Ignore */ |
| 876 | goto next; |
| 877 | |
| 878 | /* Swap */ |
| 879 | tmp = alarm_timers[i]; |
| 880 | alarm_timers[i] = alarm_timers[cur]; |
| 881 | alarm_timers[cur] = tmp; |
| 882 | |
| 883 | cur++; |
| 884 | next: |
| 885 | name = strtok(NULL, ","); |
| 886 | } |
| 887 | |
| 888 | free(arg); |
| 889 | |
| 890 | if (cur) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 891 | /* Disable remaining timers */ |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 892 | for (i = cur; i < count; i++) |
| 893 | alarm_timers[i].name = NULL; |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 894 | } else { |
| 895 | show_available_alarms(); |
| 896 | exit(1); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 897 | } |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 898 | } |
| 899 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 900 | QEMUClock *rt_clock; |
| 901 | QEMUClock *vm_clock; |
| 902 | |
| 903 | static QEMUTimer *active_timers[2]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 904 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 905 | static QEMUClock *qemu_new_clock(int type) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 906 | { |
| 907 | QEMUClock *clock; |
| 908 | clock = qemu_mallocz(sizeof(QEMUClock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 909 | clock->type = type; |
| 910 | return clock; |
| 911 | } |
| 912 | |
| 913 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 914 | { |
| 915 | QEMUTimer *ts; |
| 916 | |
| 917 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 918 | ts->clock = clock; |
| 919 | ts->cb = cb; |
| 920 | ts->opaque = opaque; |
| 921 | return ts; |
| 922 | } |
| 923 | |
| 924 | void qemu_free_timer(QEMUTimer *ts) |
| 925 | { |
| 926 | qemu_free(ts); |
| 927 | } |
| 928 | |
| 929 | /* stop a timer, but do not dealloc it */ |
| 930 | void qemu_del_timer(QEMUTimer *ts) |
| 931 | { |
| 932 | QEMUTimer **pt, *t; |
| 933 | |
| 934 | /* NOTE: this code must be signal safe because |
| 935 | qemu_timer_expired() can be called from a signal. */ |
| 936 | pt = &active_timers[ts->clock->type]; |
| 937 | for(;;) { |
| 938 | t = *pt; |
| 939 | if (!t) |
| 940 | break; |
| 941 | if (t == ts) { |
| 942 | *pt = t->next; |
| 943 | break; |
| 944 | } |
| 945 | pt = &t->next; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /* modify the current timer so that it will be fired when current_time |
| 950 | >= expire_time. The corresponding callback will be called. */ |
| 951 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 952 | { |
| 953 | QEMUTimer **pt, *t; |
| 954 | |
| 955 | qemu_del_timer(ts); |
| 956 | |
| 957 | /* add the timer in the sorted list */ |
| 958 | /* NOTE: this code must be signal safe because |
| 959 | qemu_timer_expired() can be called from a signal. */ |
| 960 | pt = &active_timers[ts->clock->type]; |
| 961 | for(;;) { |
| 962 | t = *pt; |
| 963 | if (!t) |
| 964 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 965 | if (t->expire_time > expire_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 966 | break; |
| 967 | pt = &t->next; |
| 968 | } |
| 969 | ts->expire_time = expire_time; |
| 970 | ts->next = *pt; |
| 971 | *pt = ts; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 972 | |
| 973 | /* Rearm if necessary */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 974 | if (pt == &active_timers[ts->clock->type]) { |
| 975 | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
| 976 | qemu_rearm_alarm_timer(alarm_timer); |
| 977 | } |
| 978 | /* Interrupt execution to force deadline recalculation. */ |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 979 | if (use_icount) |
| 980 | qemu_notify_event(); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 981 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | int qemu_timer_pending(QEMUTimer *ts) |
| 985 | { |
| 986 | QEMUTimer *t; |
| 987 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 988 | if (t == ts) |
| 989 | return 1; |
| 990 | } |
| 991 | return 0; |
| 992 | } |
| 993 | |
Stefano Stabellini | 2430ffe | 2009-08-03 10:56:01 +0100 | [diff] [blame] | 994 | int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 995 | { |
| 996 | if (!timer_head) |
| 997 | return 0; |
| 998 | return (timer_head->expire_time <= current_time); |
| 999 | } |
| 1000 | |
| 1001 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 1002 | { |
| 1003 | QEMUTimer *ts; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1004 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1005 | for(;;) { |
| 1006 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 1007 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1008 | break; |
| 1009 | /* remove timer from the list before calling the callback */ |
| 1010 | *ptimer_head = ts->next; |
| 1011 | ts->next = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1012 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1013 | /* run the callback (the timer list can be modified) */ |
| 1014 | ts->cb(ts->opaque); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | int64_t qemu_get_clock(QEMUClock *clock) |
| 1019 | { |
| 1020 | switch(clock->type) { |
| 1021 | case QEMU_TIMER_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1022 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1023 | default: |
| 1024 | case QEMU_TIMER_VIRTUAL: |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1025 | if (use_icount) { |
| 1026 | return cpu_get_icount(); |
| 1027 | } else { |
| 1028 | return cpu_get_clock(); |
| 1029 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1033 | static void init_timers(void) |
| 1034 | { |
| 1035 | init_get_clock(); |
| 1036 | ticks_per_sec = QEMU_TIMER_BASE; |
| 1037 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
| 1038 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
| 1039 | } |
| 1040 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1041 | /* save a timer */ |
| 1042 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 1043 | { |
| 1044 | uint64_t expire_time; |
| 1045 | |
| 1046 | if (qemu_timer_pending(ts)) { |
| 1047 | expire_time = ts->expire_time; |
| 1048 | } else { |
| 1049 | expire_time = -1; |
| 1050 | } |
| 1051 | qemu_put_be64(f, expire_time); |
| 1052 | } |
| 1053 | |
| 1054 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 1055 | { |
| 1056 | uint64_t expire_time; |
| 1057 | |
| 1058 | expire_time = qemu_get_be64(f); |
| 1059 | if (expire_time != -1) { |
| 1060 | qemu_mod_timer(ts, expire_time); |
| 1061 | } else { |
| 1062 | qemu_del_timer(ts); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | static void timer_save(QEMUFile *f, void *opaque) |
| 1067 | { |
| 1068 | if (cpu_ticks_enabled) { |
| 1069 | hw_error("cannot save state if virtual timers are running"); |
| 1070 | } |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1071 | qemu_put_be64(f, cpu_ticks_offset); |
| 1072 | qemu_put_be64(f, ticks_per_sec); |
| 1073 | qemu_put_be64(f, cpu_clock_offset); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
| 1077 | { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1078 | if (version_id != 1 && version_id != 2) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1079 | return -EINVAL; |
| 1080 | if (cpu_ticks_enabled) { |
| 1081 | return -EINVAL; |
| 1082 | } |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1083 | cpu_ticks_offset=qemu_get_be64(f); |
| 1084 | ticks_per_sec=qemu_get_be64(f); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1085 | if (version_id == 2) { |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1086 | cpu_clock_offset=qemu_get_be64(f); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1087 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1088 | return 0; |
| 1089 | } |
| 1090 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1091 | static void qemu_event_increment(void); |
| 1092 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1093 | #ifdef _WIN32 |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 1094 | static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 1095 | DWORD_PTR dwUser, DWORD_PTR dw1, |
| 1096 | DWORD_PTR dw2) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1097 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1098 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1099 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1100 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1101 | #if 0 |
| 1102 | #define DISP_FREQ 1000 |
| 1103 | { |
| 1104 | static int64_t delta_min = INT64_MAX; |
| 1105 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 1106 | static int count; |
| 1107 | ti = qemu_get_clock(vm_clock); |
| 1108 | if (last_clock != 0) { |
| 1109 | delta = ti - last_clock; |
| 1110 | if (delta < delta_min) |
| 1111 | delta_min = delta; |
| 1112 | if (delta > delta_max) |
| 1113 | delta_max = delta; |
| 1114 | delta_cum += delta; |
| 1115 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 1116 | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n", |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1117 | muldiv64(delta_min, 1000000, ticks_per_sec), |
| 1118 | muldiv64(delta_max, 1000000, ticks_per_sec), |
| 1119 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), |
| 1120 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); |
| 1121 | count = 0; |
| 1122 | delta_min = INT64_MAX; |
| 1123 | delta_max = 0; |
| 1124 | delta_cum = 0; |
| 1125 | } |
| 1126 | } |
| 1127 | last_clock = ti; |
| 1128 | } |
| 1129 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1130 | if (alarm_has_dynticks(alarm_timer) || |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1131 | (!use_icount && |
| 1132 | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
| 1133 | qemu_get_clock(vm_clock))) || |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1134 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
| 1135 | qemu_get_clock(rt_clock))) { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1136 | qemu_event_increment(); |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 1137 | if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1138 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1139 | #ifndef CONFIG_IOTHREAD |
| 1140 | if (next_cpu) { |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1141 | /* stop the currently executing cpu because a timer occured */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1142 | cpu_exit(next_cpu); |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1143 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1144 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 1145 | timer_alarm_pending = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 1146 | qemu_notify_event(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1150 | static int64_t qemu_next_deadline(void) |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1151 | { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1152 | int64_t delta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1153 | |
| 1154 | if (active_timers[QEMU_TIMER_VIRTUAL]) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1155 | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
| 1156 | qemu_get_clock(vm_clock); |
| 1157 | } else { |
| 1158 | /* To avoid problems with overflow limit this to 2^32. */ |
| 1159 | delta = INT32_MAX; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1162 | if (delta < 0) |
| 1163 | delta = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1164 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1165 | return delta; |
| 1166 | } |
| 1167 | |
blueswir1 | 8632fb9 | 2008-09-14 13:59:34 +0000 | [diff] [blame] | 1168 | #if defined(__linux__) || defined(_WIN32) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1169 | static uint64_t qemu_next_deadline_dyntick(void) |
| 1170 | { |
| 1171 | int64_t delta; |
| 1172 | int64_t rtdelta; |
| 1173 | |
| 1174 | if (use_icount) |
| 1175 | delta = INT32_MAX; |
| 1176 | else |
| 1177 | delta = (qemu_next_deadline() + 999) / 1000; |
| 1178 | |
| 1179 | if (active_timers[QEMU_TIMER_REALTIME]) { |
| 1180 | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
| 1181 | qemu_get_clock(rt_clock))*1000; |
| 1182 | if (rtdelta < delta) |
| 1183 | delta = rtdelta; |
| 1184 | } |
| 1185 | |
| 1186 | if (delta < MIN_TIMER_REARM_US) |
| 1187 | delta = MIN_TIMER_REARM_US; |
| 1188 | |
| 1189 | return delta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1190 | } |
blueswir1 | 8632fb9 | 2008-09-14 13:59:34 +0000 | [diff] [blame] | 1191 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1192 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1193 | #ifndef _WIN32 |
| 1194 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1195 | /* Sets a specific flag */ |
| 1196 | static int fcntl_setfl(int fd, int flag) |
| 1197 | { |
| 1198 | int flags; |
| 1199 | |
| 1200 | flags = fcntl(fd, F_GETFL); |
| 1201 | if (flags == -1) |
| 1202 | return -errno; |
| 1203 | |
| 1204 | if (fcntl(fd, F_SETFL, flags | flag) == -1) |
| 1205 | return -errno; |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1210 | #if defined(__linux__) |
| 1211 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1212 | #define RTC_FREQ 1024 |
| 1213 | |
aurel32 | de9a95f | 2008-11-11 13:41:01 +0000 | [diff] [blame] | 1214 | static void enable_sigio_timer(int fd) |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1215 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1216 | struct sigaction act; |
| 1217 | |
| 1218 | /* timer signal */ |
| 1219 | sigfillset(&act.sa_mask); |
| 1220 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1221 | act.sa_handler = host_alarm_handler; |
| 1222 | |
| 1223 | sigaction(SIGIO, &act, NULL); |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1224 | fcntl_setfl(fd, O_ASYNC); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1225 | fcntl(fd, F_SETOWN, getpid()); |
| 1226 | } |
| 1227 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1228 | static int hpet_start_timer(struct qemu_alarm_timer *t) |
| 1229 | { |
| 1230 | struct hpet_info info; |
| 1231 | int r, fd; |
| 1232 | |
| 1233 | fd = open("/dev/hpet", O_RDONLY); |
| 1234 | if (fd < 0) |
| 1235 | return -1; |
| 1236 | |
| 1237 | /* Set frequency */ |
| 1238 | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
| 1239 | if (r < 0) { |
| 1240 | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n" |
| 1241 | "error, but for better emulation accuracy type:\n" |
| 1242 | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n"); |
| 1243 | goto fail; |
| 1244 | } |
| 1245 | |
| 1246 | /* Check capabilities */ |
| 1247 | r = ioctl(fd, HPET_INFO, &info); |
| 1248 | if (r < 0) |
| 1249 | goto fail; |
| 1250 | |
| 1251 | /* Enable periodic mode */ |
| 1252 | r = ioctl(fd, HPET_EPI, 0); |
| 1253 | if (info.hi_flags && (r < 0)) |
| 1254 | goto fail; |
| 1255 | |
| 1256 | /* Enable interrupt */ |
| 1257 | r = ioctl(fd, HPET_IE_ON, 0); |
| 1258 | if (r < 0) |
| 1259 | goto fail; |
| 1260 | |
| 1261 | enable_sigio_timer(fd); |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1262 | t->priv = (void *)(long)fd; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1263 | |
| 1264 | return 0; |
| 1265 | fail: |
| 1266 | close(fd); |
| 1267 | return -1; |
| 1268 | } |
| 1269 | |
| 1270 | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
| 1271 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1272 | int fd = (long)t->priv; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1273 | |
| 1274 | close(fd); |
| 1275 | } |
| 1276 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1277 | static int rtc_start_timer(struct qemu_alarm_timer *t) |
| 1278 | { |
| 1279 | int rtc_fd; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1280 | unsigned long current_rtc_freq = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1281 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 1282 | TFR(rtc_fd = open("/dev/rtc", O_RDONLY)); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1283 | if (rtc_fd < 0) |
| 1284 | return -1; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1285 | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
| 1286 | if (current_rtc_freq != RTC_FREQ && |
| 1287 | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1288 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1289 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1290 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1291 | goto fail; |
| 1292 | } |
| 1293 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1294 | fail: |
| 1295 | close(rtc_fd); |
| 1296 | return -1; |
| 1297 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1298 | |
| 1299 | enable_sigio_timer(rtc_fd); |
| 1300 | |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1301 | t->priv = (void *)(long)rtc_fd; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1302 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1303 | return 0; |
| 1304 | } |
| 1305 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1306 | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1307 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1308 | int rtc_fd = (long)t->priv; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1309 | |
| 1310 | close(rtc_fd); |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1313 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
| 1314 | { |
| 1315 | struct sigevent ev; |
| 1316 | timer_t host_timer; |
| 1317 | struct sigaction act; |
| 1318 | |
| 1319 | sigfillset(&act.sa_mask); |
| 1320 | act.sa_flags = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1321 | act.sa_handler = host_alarm_handler; |
| 1322 | |
| 1323 | sigaction(SIGALRM, &act, NULL); |
| 1324 | |
Jean-Christophe Dubois | 9ed415b | 2009-05-17 18:41:16 +0200 | [diff] [blame] | 1325 | /* |
| 1326 | * Initialize ev struct to 0 to avoid valgrind complaining |
| 1327 | * about uninitialized data in timer_create call |
| 1328 | */ |
| 1329 | memset(&ev, 0, sizeof(ev)); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1330 | ev.sigev_value.sival_int = 0; |
| 1331 | ev.sigev_notify = SIGEV_SIGNAL; |
| 1332 | ev.sigev_signo = SIGALRM; |
| 1333 | |
| 1334 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { |
| 1335 | perror("timer_create"); |
| 1336 | |
| 1337 | /* disable dynticks */ |
| 1338 | fprintf(stderr, "Dynamic Ticks disabled\n"); |
| 1339 | |
| 1340 | return -1; |
| 1341 | } |
| 1342 | |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1343 | t->priv = (void *)(long)host_timer; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1344 | |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
| 1348 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
| 1349 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1350 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1351 | |
| 1352 | timer_delete(host_timer); |
| 1353 | } |
| 1354 | |
| 1355 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
| 1356 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1357 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1358 | struct itimerspec timeout; |
| 1359 | int64_t nearest_delta_us = INT64_MAX; |
| 1360 | int64_t current_us; |
| 1361 | |
| 1362 | if (!active_timers[QEMU_TIMER_REALTIME] && |
| 1363 | !active_timers[QEMU_TIMER_VIRTUAL]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1364 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1365 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1366 | nearest_delta_us = qemu_next_deadline_dyntick(); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1367 | |
| 1368 | /* check whether a timer is already running */ |
| 1369 | if (timer_gettime(host_timer, &timeout)) { |
| 1370 | perror("gettime"); |
| 1371 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1372 | exit(1); |
| 1373 | } |
| 1374 | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
| 1375 | if (current_us && current_us <= nearest_delta_us) |
| 1376 | return; |
| 1377 | |
| 1378 | timeout.it_interval.tv_sec = 0; |
| 1379 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
| 1380 | timeout.it_value.tv_sec = nearest_delta_us / 1000000; |
| 1381 | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
| 1382 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
| 1383 | perror("settime"); |
| 1384 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1385 | exit(1); |
| 1386 | } |
| 1387 | } |
| 1388 | |
ths | 70744b3 | 2007-08-26 17:31:30 +0000 | [diff] [blame] | 1389 | #endif /* defined(__linux__) */ |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 1390 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1391 | static int unix_start_timer(struct qemu_alarm_timer *t) |
| 1392 | { |
| 1393 | struct sigaction act; |
| 1394 | struct itimerval itv; |
| 1395 | int err; |
| 1396 | |
| 1397 | /* timer signal */ |
| 1398 | sigfillset(&act.sa_mask); |
| 1399 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1400 | act.sa_handler = host_alarm_handler; |
| 1401 | |
| 1402 | sigaction(SIGALRM, &act, NULL); |
| 1403 | |
| 1404 | itv.it_interval.tv_sec = 0; |
| 1405 | /* for i386 kernel 2.6 to get 1 ms */ |
| 1406 | itv.it_interval.tv_usec = 999; |
| 1407 | itv.it_value.tv_sec = 0; |
| 1408 | itv.it_value.tv_usec = 10 * 1000; |
| 1409 | |
| 1410 | err = setitimer(ITIMER_REAL, &itv, NULL); |
| 1411 | if (err) |
| 1412 | return -1; |
| 1413 | |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
| 1417 | static void unix_stop_timer(struct qemu_alarm_timer *t) |
| 1418 | { |
| 1419 | struct itimerval itv; |
| 1420 | |
| 1421 | memset(&itv, 0, sizeof(itv)); |
| 1422 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1423 | } |
| 1424 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1425 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1426 | |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1427 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1428 | #ifdef _WIN32 |
| 1429 | |
| 1430 | static int win32_start_timer(struct qemu_alarm_timer *t) |
| 1431 | { |
| 1432 | TIMECAPS tc; |
| 1433 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1434 | UINT flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1435 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1436 | memset(&tc, 0, sizeof(tc)); |
| 1437 | timeGetDevCaps(&tc, sizeof(tc)); |
| 1438 | |
| 1439 | if (data->period < tc.wPeriodMin) |
| 1440 | data->period = tc.wPeriodMin; |
| 1441 | |
| 1442 | timeBeginPeriod(data->period); |
| 1443 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1444 | flags = TIME_CALLBACK_FUNCTION; |
| 1445 | if (alarm_has_dynticks(t)) |
| 1446 | flags |= TIME_ONESHOT; |
| 1447 | else |
| 1448 | flags |= TIME_PERIODIC; |
| 1449 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1450 | data->timerId = timeSetEvent(1, // interval (ms) |
| 1451 | data->period, // resolution |
| 1452 | host_alarm_handler, // function |
| 1453 | (DWORD)t, // parameter |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1454 | flags); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1455 | |
| 1456 | if (!data->timerId) { |
| 1457 | perror("Failed to initialize win32 alarm timer"); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1458 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1459 | return -1; |
| 1460 | } |
| 1461 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | static void win32_stop_timer(struct qemu_alarm_timer *t) |
| 1466 | { |
| 1467 | struct qemu_alarm_win32 *data = t->priv; |
| 1468 | |
| 1469 | timeKillEvent(data->timerId); |
| 1470 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1473 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
| 1474 | { |
| 1475 | struct qemu_alarm_win32 *data = t->priv; |
| 1476 | uint64_t nearest_delta_us; |
| 1477 | |
| 1478 | if (!active_timers[QEMU_TIMER_REALTIME] && |
| 1479 | !active_timers[QEMU_TIMER_VIRTUAL]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1480 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1481 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1482 | nearest_delta_us = qemu_next_deadline_dyntick(); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1483 | nearest_delta_us /= 1000; |
| 1484 | |
| 1485 | timeKillEvent(data->timerId); |
| 1486 | |
| 1487 | data->timerId = timeSetEvent(1, |
| 1488 | data->period, |
| 1489 | host_alarm_handler, |
| 1490 | (DWORD)t, |
| 1491 | TIME_ONESHOT | TIME_PERIODIC); |
| 1492 | |
| 1493 | if (!data->timerId) { |
| 1494 | perror("Failed to re-arm win32 alarm timer"); |
| 1495 | |
| 1496 | timeEndPeriod(data->period); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1497 | exit(1); |
| 1498 | } |
| 1499 | } |
| 1500 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1501 | #endif /* _WIN32 */ |
| 1502 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1503 | static int init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1504 | { |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 1505 | struct qemu_alarm_timer *t = NULL; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1506 | int i, err = -1; |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1507 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1508 | for (i = 0; alarm_timers[i].name; i++) { |
| 1509 | t = &alarm_timers[i]; |
| 1510 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1511 | err = t->start(t); |
| 1512 | if (!err) |
| 1513 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1514 | } |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1515 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1516 | if (err) { |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1517 | err = -ENOENT; |
| 1518 | goto fail; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1519 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1520 | |
| 1521 | alarm_timer = t; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1522 | |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 1523 | return 0; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1524 | |
| 1525 | fail: |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1526 | return err; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1529 | static void quit_timers(void) |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1530 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1531 | alarm_timer->stop(alarm_timer); |
| 1532 | alarm_timer = NULL; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1535 | /***********************************************************/ |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 1536 | /* host time/date access */ |
| 1537 | void qemu_get_timedate(struct tm *tm, int offset) |
| 1538 | { |
| 1539 | time_t ti; |
| 1540 | struct tm *ret; |
| 1541 | |
| 1542 | time(&ti); |
| 1543 | ti += offset; |
| 1544 | if (rtc_date_offset == -1) { |
| 1545 | if (rtc_utc) |
| 1546 | ret = gmtime(&ti); |
| 1547 | else |
| 1548 | ret = localtime(&ti); |
| 1549 | } else { |
| 1550 | ti -= rtc_date_offset; |
| 1551 | ret = gmtime(&ti); |
| 1552 | } |
| 1553 | |
| 1554 | memcpy(tm, ret, sizeof(struct tm)); |
| 1555 | } |
| 1556 | |
| 1557 | int qemu_timedate_diff(struct tm *tm) |
| 1558 | { |
| 1559 | time_t seconds; |
| 1560 | |
| 1561 | if (rtc_date_offset == -1) |
| 1562 | if (rtc_utc) |
| 1563 | seconds = mktimegm(tm); |
| 1564 | else |
| 1565 | seconds = mktime(tm); |
| 1566 | else |
| 1567 | seconds = mktimegm(tm) + rtc_date_offset; |
| 1568 | |
| 1569 | return seconds - time(NULL); |
| 1570 | } |
| 1571 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1572 | #ifdef _WIN32 |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1573 | static void socket_cleanup(void) |
| 1574 | { |
| 1575 | WSACleanup(); |
| 1576 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1577 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1578 | static int socket_init(void) |
| 1579 | { |
| 1580 | WSADATA Data; |
| 1581 | int ret, err; |
| 1582 | |
| 1583 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1584 | if (ret != 0) { |
| 1585 | err = WSAGetLastError(); |
| 1586 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1587 | return -1; |
| 1588 | } |
| 1589 | atexit(socket_cleanup); |
| 1590 | return 0; |
| 1591 | } |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 1592 | #endif |
| 1593 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1594 | /***********************************************************/ |
| 1595 | /* Bluetooth support */ |
| 1596 | static int nb_hcis; |
| 1597 | static int cur_hci; |
| 1598 | static struct HCIInfo *hci_table[MAX_NICS]; |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1599 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1600 | static struct bt_vlan_s { |
| 1601 | struct bt_scatternet_s net; |
| 1602 | int id; |
| 1603 | struct bt_vlan_s *next; |
| 1604 | } *first_bt_vlan; |
| 1605 | |
| 1606 | /* find or alloc a new bluetooth "VLAN" */ |
blueswir1 | 674bb26 | 2008-09-30 18:18:27 +0000 | [diff] [blame] | 1607 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1608 | { |
| 1609 | struct bt_vlan_s **pvlan, *vlan; |
| 1610 | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { |
| 1611 | if (vlan->id == id) |
| 1612 | return &vlan->net; |
| 1613 | } |
| 1614 | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); |
| 1615 | vlan->id = id; |
| 1616 | pvlan = &first_bt_vlan; |
| 1617 | while (*pvlan != NULL) |
| 1618 | pvlan = &(*pvlan)->next; |
| 1619 | *pvlan = vlan; |
| 1620 | return &vlan->net; |
| 1621 | } |
| 1622 | |
| 1623 | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) |
| 1624 | { |
| 1625 | } |
| 1626 | |
| 1627 | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
| 1628 | { |
| 1629 | return -ENOTSUP; |
| 1630 | } |
| 1631 | |
| 1632 | static struct HCIInfo null_hci = { |
| 1633 | .cmd_send = null_hci_send, |
| 1634 | .sco_send = null_hci_send, |
| 1635 | .acl_send = null_hci_send, |
| 1636 | .bdaddr_set = null_hci_addr_set, |
| 1637 | }; |
| 1638 | |
| 1639 | struct HCIInfo *qemu_next_hci(void) |
| 1640 | { |
| 1641 | if (cur_hci == nb_hcis) |
| 1642 | return &null_hci; |
| 1643 | |
| 1644 | return hci_table[cur_hci++]; |
| 1645 | } |
| 1646 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1647 | static struct HCIInfo *hci_init(const char *str) |
| 1648 | { |
| 1649 | char *endp; |
| 1650 | struct bt_scatternet_s *vlan = 0; |
| 1651 | |
| 1652 | if (!strcmp(str, "null")) |
| 1653 | /* null */ |
| 1654 | return &null_hci; |
| 1655 | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) |
| 1656 | /* host[:hciN] */ |
| 1657 | return bt_host_hci(str[4] ? str + 5 : "hci0"); |
| 1658 | else if (!strncmp(str, "hci", 3)) { |
| 1659 | /* hci[,vlan=n] */ |
| 1660 | if (str[3]) { |
| 1661 | if (!strncmp(str + 3, ",vlan=", 6)) { |
| 1662 | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); |
| 1663 | if (*endp) |
| 1664 | vlan = 0; |
| 1665 | } |
| 1666 | } else |
| 1667 | vlan = qemu_find_bt_vlan(0); |
| 1668 | if (vlan) |
| 1669 | return bt_new_hci(vlan); |
| 1670 | } |
| 1671 | |
| 1672 | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str); |
| 1673 | |
| 1674 | return 0; |
| 1675 | } |
| 1676 | |
| 1677 | static int bt_hci_parse(const char *str) |
| 1678 | { |
| 1679 | struct HCIInfo *hci; |
| 1680 | bdaddr_t bdaddr; |
| 1681 | |
| 1682 | if (nb_hcis >= MAX_NICS) { |
| 1683 | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS); |
| 1684 | return -1; |
| 1685 | } |
| 1686 | |
| 1687 | hci = hci_init(str); |
| 1688 | if (!hci) |
| 1689 | return -1; |
| 1690 | |
| 1691 | bdaddr.b[0] = 0x52; |
| 1692 | bdaddr.b[1] = 0x54; |
| 1693 | bdaddr.b[2] = 0x00; |
| 1694 | bdaddr.b[3] = 0x12; |
| 1695 | bdaddr.b[4] = 0x34; |
| 1696 | bdaddr.b[5] = 0x56 + nb_hcis; |
| 1697 | hci->bdaddr_set(hci, bdaddr.b); |
| 1698 | |
| 1699 | hci_table[nb_hcis++] = hci; |
| 1700 | |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
| 1704 | static void bt_vhci_add(int vlan_id) |
| 1705 | { |
| 1706 | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id); |
| 1707 | |
| 1708 | if (!vlan->slave) |
| 1709 | fprintf(stderr, "qemu: warning: adding a VHCI to " |
| 1710 | "an empty scatternet %i\n", vlan_id); |
| 1711 | |
| 1712 | bt_vhci_init(bt_new_hci(vlan)); |
| 1713 | } |
| 1714 | |
| 1715 | static struct bt_device_s *bt_device_add(const char *opt) |
| 1716 | { |
| 1717 | struct bt_scatternet_s *vlan; |
| 1718 | int vlan_id = 0; |
| 1719 | char *endp = strstr(opt, ",vlan="); |
| 1720 | int len = (endp ? endp - opt : strlen(opt)) + 1; |
| 1721 | char devname[10]; |
| 1722 | |
| 1723 | pstrcpy(devname, MIN(sizeof(devname), len), opt); |
| 1724 | |
| 1725 | if (endp) { |
| 1726 | vlan_id = strtol(endp + 6, &endp, 0); |
| 1727 | if (*endp) { |
| 1728 | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n"); |
| 1729 | return 0; |
| 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | vlan = qemu_find_bt_vlan(vlan_id); |
| 1734 | |
| 1735 | if (!vlan->slave) |
| 1736 | fprintf(stderr, "qemu: warning: adding a slave device to " |
| 1737 | "an empty scatternet %i\n", vlan_id); |
| 1738 | |
| 1739 | if (!strcmp(devname, "keyboard")) |
| 1740 | return bt_keyboard_init(vlan); |
| 1741 | |
| 1742 | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname); |
| 1743 | return 0; |
| 1744 | } |
| 1745 | |
| 1746 | static int bt_parse(const char *opt) |
| 1747 | { |
| 1748 | const char *endp, *p; |
| 1749 | int vlan; |
| 1750 | |
| 1751 | if (strstart(opt, "hci", &endp)) { |
| 1752 | if (!*endp || *endp == ',') { |
| 1753 | if (*endp) |
| 1754 | if (!strstart(endp, ",vlan=", 0)) |
| 1755 | opt = endp + 1; |
| 1756 | |
| 1757 | return bt_hci_parse(opt); |
| 1758 | } |
| 1759 | } else if (strstart(opt, "vhci", &endp)) { |
| 1760 | if (!*endp || *endp == ',') { |
| 1761 | if (*endp) { |
| 1762 | if (strstart(endp, ",vlan=", &p)) { |
| 1763 | vlan = strtol(p, (char **) &endp, 0); |
| 1764 | if (*endp) { |
| 1765 | fprintf(stderr, "qemu: bad scatternet '%s'\n", p); |
| 1766 | return 1; |
| 1767 | } |
| 1768 | } else { |
| 1769 | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); |
| 1770 | return 1; |
| 1771 | } |
| 1772 | } else |
| 1773 | vlan = 0; |
| 1774 | |
| 1775 | bt_vhci_add(vlan); |
| 1776 | return 0; |
| 1777 | } |
| 1778 | } else if (strstart(opt, "device:", &endp)) |
| 1779 | return !bt_device_add(endp); |
| 1780 | |
| 1781 | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt); |
| 1782 | return 1; |
| 1783 | } |
| 1784 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1785 | /***********************************************************/ |
| 1786 | /* QEMU Block devices */ |
| 1787 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1788 | #define HD_ALIAS "index=%d,media=disk" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1789 | #define CDROM_ALIAS "index=2,media=cdrom" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1790 | #define FD_ALIAS "index=%d,if=floppy" |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1791 | #define PFLASH_ALIAS "if=pflash" |
| 1792 | #define MTD_ALIAS "if=mtd" |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 1793 | #define SD_ALIAS "index=0,if=sd" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1794 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1795 | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1796 | { |
| 1797 | va_list ap; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1798 | char optstr[1024]; |
| 1799 | QemuOpts *opts; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1800 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1801 | va_start(ap, fmt); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1802 | vsnprintf(optstr, sizeof(optstr), fmt, ap); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1803 | va_end(ap); |
| 1804 | |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 1805 | opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1806 | if (!opts) { |
| 1807 | fprintf(stderr, "%s: huh? duplicate? (%s)\n", |
| 1808 | __FUNCTION__, optstr); |
| 1809 | return NULL; |
| 1810 | } |
| 1811 | if (file) |
| 1812 | qemu_opt_set(opts, "file", file); |
| 1813 | return opts; |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1816 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1817 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1818 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1819 | |
| 1820 | /* seek interface, bus and unit */ |
| 1821 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1822 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1823 | if (dinfo->type == type && |
| 1824 | dinfo->bus == bus && |
| 1825 | dinfo->unit == unit) |
| 1826 | return dinfo; |
| 1827 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1828 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1829 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Gerd Hoffmann | 2e810b3 | 2009-07-31 12:25:38 +0200 | [diff] [blame] | 1832 | DriveInfo *drive_get_by_id(const char *id) |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 1833 | { |
| 1834 | DriveInfo *dinfo; |
| 1835 | |
| 1836 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1837 | if (strcmp(id, dinfo->id)) |
| 1838 | continue; |
| 1839 | return dinfo; |
| 1840 | } |
| 1841 | return NULL; |
| 1842 | } |
| 1843 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1844 | int drive_get_max_bus(BlockInterfaceType type) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1845 | { |
| 1846 | int max_bus; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1847 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1848 | |
| 1849 | max_bus = -1; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1850 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1851 | if(dinfo->type == type && |
| 1852 | dinfo->bus > max_bus) |
| 1853 | max_bus = dinfo->bus; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1854 | } |
| 1855 | return max_bus; |
| 1856 | } |
| 1857 | |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1858 | const char *drive_get_serial(BlockDriverState *bdrv) |
| 1859 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1860 | DriveInfo *dinfo; |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1861 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1862 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1863 | if (dinfo->bdrv == bdrv) |
| 1864 | return dinfo->serial; |
| 1865 | } |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1866 | |
| 1867 | return "\0"; |
| 1868 | } |
| 1869 | |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1870 | BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv) |
| 1871 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1872 | DriveInfo *dinfo; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1873 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1874 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1875 | if (dinfo->bdrv == bdrv) |
| 1876 | return dinfo->onerror; |
| 1877 | } |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1878 | |
aliguori | cdad4bd | 2009-02-28 16:51:01 +0000 | [diff] [blame] | 1879 | return BLOCK_ERR_STOP_ENOSPC; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 1882 | static void bdrv_format_print(void *opaque, const char *name) |
| 1883 | { |
| 1884 | fprintf(stderr, " %s", name); |
| 1885 | } |
| 1886 | |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1887 | void drive_uninit(BlockDriverState *bdrv) |
| 1888 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1889 | DriveInfo *dinfo; |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1890 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1891 | TAILQ_FOREACH(dinfo, &drives, next) { |
| 1892 | if (dinfo->bdrv != bdrv) |
| 1893 | continue; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1894 | qemu_opts_del(dinfo->opts); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1895 | TAILQ_REMOVE(&drives, dinfo, next); |
| 1896 | qemu_free(dinfo); |
| 1897 | break; |
| 1898 | } |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1901 | DriveInfo *drive_init(QemuOpts *opts, void *opaque, |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1902 | int *fatal_error) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1903 | { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1904 | const char *buf; |
| 1905 | const char *file = NULL; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 1906 | char devname[128]; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1907 | const char *serial; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 1908 | const char *mediastr = ""; |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1909 | BlockInterfaceType type; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1910 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
| 1911 | int bus_id, unit_id; |
| 1912 | int cyls, heads, secs, translation; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 1913 | BlockDriver *drv = NULL; |
aliguori | 4d73cd3 | 2009-02-11 15:20:46 +0000 | [diff] [blame] | 1914 | QEMUMachine *machine = opaque; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1915 | int max_devs; |
| 1916 | int index; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 1917 | int cache; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 1918 | int aio = 0; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1919 | int bdrv_flags, onerror; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 1920 | const char *devaddr; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1921 | DriveInfo *dinfo; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1922 | int snapshot = 0; |
| 1923 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1924 | *fatal_error = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1925 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1926 | translation = BIOS_ATA_TRANSLATION_AUTO; |
Kevin Wolf | 0aa217e | 2009-06-30 13:06:04 +0200 | [diff] [blame] | 1927 | cache = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1928 | |
blueswir1 | c9b1ae2 | 2008-09-28 18:55:17 +0000 | [diff] [blame] | 1929 | if (machine->use_scsi) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1930 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1931 | max_devs = MAX_SCSI_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1932 | pstrcpy(devname, sizeof(devname), "scsi"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1933 | } else { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1934 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1935 | max_devs = MAX_IDE_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 1936 | pstrcpy(devname, sizeof(devname), "ide"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1937 | } |
| 1938 | media = MEDIA_DISK; |
| 1939 | |
| 1940 | /* extract parameters */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1941 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
| 1942 | unit_id = qemu_opt_get_number(opts, "unit", -1); |
| 1943 | index = qemu_opt_get_number(opts, "index", -1); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1944 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1945 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
| 1946 | heads = qemu_opt_get_number(opts, "heads", 0); |
| 1947 | secs = qemu_opt_get_number(opts, "secs", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1948 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1949 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1950 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1951 | file = qemu_opt_get(opts, "file"); |
| 1952 | serial = qemu_opt_get(opts, "serial"); |
| 1953 | |
| 1954 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
bellard | ae45d36 | 2008-06-11 09:44:44 +0000 | [diff] [blame] | 1955 | pstrcpy(devname, sizeof(devname), buf); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1956 | if (!strcmp(buf, "ide")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1957 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1958 | max_devs = MAX_IDE_DEVS; |
| 1959 | } else if (!strcmp(buf, "scsi")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1960 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1961 | max_devs = MAX_SCSI_DEVS; |
| 1962 | } else if (!strcmp(buf, "floppy")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1963 | type = IF_FLOPPY; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1964 | max_devs = 0; |
| 1965 | } else if (!strcmp(buf, "pflash")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1966 | type = IF_PFLASH; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1967 | max_devs = 0; |
| 1968 | } else if (!strcmp(buf, "mtd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1969 | type = IF_MTD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1970 | max_devs = 0; |
| 1971 | } else if (!strcmp(buf, "sd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1972 | type = IF_SD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1973 | max_devs = 0; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 1974 | } else if (!strcmp(buf, "virtio")) { |
| 1975 | type = IF_VIRTIO; |
| 1976 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 1977 | } else if (!strcmp(buf, "xen")) { |
| 1978 | type = IF_XEN; |
| 1979 | max_devs = 0; |
Gerd Hoffmann | a8659e9 | 2009-07-31 12:25:39 +0200 | [diff] [blame] | 1980 | } else if (!strcmp(buf, "none")) { |
| 1981 | type = IF_NONE; |
| 1982 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 1983 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1984 | fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1985 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1986 | } |
| 1987 | } |
| 1988 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1989 | if (cyls || heads || secs) { |
| 1990 | if (cyls < 1 || cyls > 16383) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1991 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1992 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1993 | } |
| 1994 | if (heads < 1 || heads > 16) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1995 | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1996 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1997 | } |
| 1998 | if (secs < 1 || secs > 63) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1999 | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2000 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2004 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2005 | if (!cyls) { |
| 2006 | fprintf(stderr, |
| 2007 | "qemu: '%s' trans must be used with cyls,heads and secs\n", |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2008 | buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2009 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2010 | } |
| 2011 | if (!strcmp(buf, "none")) |
| 2012 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 2013 | else if (!strcmp(buf, "lba")) |
| 2014 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 2015 | else if (!strcmp(buf, "auto")) |
| 2016 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 2017 | else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2018 | fprintf(stderr, "qemu: '%s' invalid translation type\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2019 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2023 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2024 | if (!strcmp(buf, "disk")) { |
| 2025 | media = MEDIA_DISK; |
| 2026 | } else if (!strcmp(buf, "cdrom")) { |
| 2027 | if (cyls || secs || heads) { |
| 2028 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2029 | "qemu: '%s' invalid physical CHS format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2030 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2031 | } |
| 2032 | media = MEDIA_CDROM; |
| 2033 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2034 | fprintf(stderr, "qemu: '%s' invalid media\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2035 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2039 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2040 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2041 | cache = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2042 | else if (!strcmp(buf, "writethrough")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2043 | cache = 1; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2044 | else if (!strcmp(buf, "writeback")) |
| 2045 | cache = 2; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2046 | else { |
| 2047 | fprintf(stderr, "qemu: invalid cache option\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2048 | return NULL; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2049 | } |
| 2050 | } |
| 2051 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2052 | #ifdef CONFIG_LINUX_AIO |
| 2053 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
| 2054 | if (!strcmp(buf, "threads")) |
| 2055 | aio = 0; |
| 2056 | else if (!strcmp(buf, "native")) |
| 2057 | aio = 1; |
| 2058 | else { |
| 2059 | fprintf(stderr, "qemu: invalid aio option\n"); |
| 2060 | return NULL; |
| 2061 | } |
| 2062 | } |
| 2063 | #endif |
| 2064 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2065 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2066 | if (strcmp(buf, "?") == 0) { |
| 2067 | fprintf(stderr, "qemu: Supported formats:"); |
| 2068 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 2069 | fprintf(stderr, "\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2070 | return NULL; |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2071 | } |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2072 | drv = bdrv_find_format(buf); |
| 2073 | if (!drv) { |
| 2074 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2075 | return NULL; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | |
aliguori | cdad4bd | 2009-02-28 16:51:01 +0000 | [diff] [blame] | 2079 | onerror = BLOCK_ERR_STOP_ENOSPC; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2080 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 2081 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 2082 | fprintf(stderr, "werror is no supported by this format\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2083 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2084 | } |
| 2085 | if (!strcmp(buf, "ignore")) |
| 2086 | onerror = BLOCK_ERR_IGNORE; |
| 2087 | else if (!strcmp(buf, "enospc")) |
| 2088 | onerror = BLOCK_ERR_STOP_ENOSPC; |
| 2089 | else if (!strcmp(buf, "stop")) |
| 2090 | onerror = BLOCK_ERR_STOP_ANY; |
| 2091 | else if (!strcmp(buf, "report")) |
| 2092 | onerror = BLOCK_ERR_REPORT; |
| 2093 | else { |
| 2094 | fprintf(stderr, "qemu: '%s' invalid write error action\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2095 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2099 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2100 | if (type != IF_VIRTIO) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2101 | fprintf(stderr, "addr is not supported\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2102 | return NULL; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2103 | } |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2104 | } |
| 2105 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2106 | /* compute bus and unit according index */ |
| 2107 | |
| 2108 | if (index != -1) { |
| 2109 | if (bus_id != 0 || unit_id != -1) { |
| 2110 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2111 | "qemu: index cannot be used with bus and unit\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2112 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2113 | } |
| 2114 | if (max_devs == 0) |
| 2115 | { |
| 2116 | unit_id = index; |
| 2117 | bus_id = 0; |
| 2118 | } else { |
| 2119 | unit_id = index % max_devs; |
| 2120 | bus_id = index / max_devs; |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | /* if user doesn't specify a unit_id, |
| 2125 | * try to find the first free |
| 2126 | */ |
| 2127 | |
| 2128 | if (unit_id == -1) { |
| 2129 | unit_id = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2130 | while (drive_get(type, bus_id, unit_id) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2131 | unit_id++; |
| 2132 | if (max_devs && unit_id >= max_devs) { |
| 2133 | unit_id -= max_devs; |
| 2134 | bus_id++; |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | /* check unit id */ |
| 2140 | |
| 2141 | if (max_devs && unit_id >= max_devs) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2142 | fprintf(stderr, "qemu: unit %d too big (max is %d)\n", |
| 2143 | unit_id, max_devs - 1); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2144 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /* |
| 2148 | * ignore multiple definitions |
| 2149 | */ |
| 2150 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2151 | if (drive_get(type, bus_id, unit_id) != NULL) { |
| 2152 | *fatal_error = 0; |
| 2153 | return NULL; |
| 2154 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2155 | |
| 2156 | /* init */ |
| 2157 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2158 | dinfo = qemu_mallocz(sizeof(*dinfo)); |
Gerd Hoffmann | e23d9c4 | 2009-07-31 12:25:34 +0200 | [diff] [blame] | 2159 | if ((buf = qemu_opts_id(opts)) != NULL) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2160 | dinfo->id = qemu_strdup(buf); |
| 2161 | } else { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2162 | /* no id supplied -> create one */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2163 | dinfo->id = qemu_mallocz(32); |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2164 | if (type == IF_IDE || type == IF_SCSI) |
| 2165 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
| 2166 | if (max_devs) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2167 | snprintf(dinfo->id, 32, "%s%i%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2168 | devname, bus_id, mediastr, unit_id); |
| 2169 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2170 | snprintf(dinfo->id, 32, "%s%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2171 | devname, mediastr, unit_id); |
| 2172 | } |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2173 | dinfo->bdrv = bdrv_new(dinfo->id); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2174 | dinfo->devaddr = devaddr; |
| 2175 | dinfo->type = type; |
| 2176 | dinfo->bus = bus_id; |
| 2177 | dinfo->unit = unit_id; |
| 2178 | dinfo->onerror = onerror; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2179 | dinfo->opts = opts; |
| 2180 | if (serial) |
| 2181 | strncpy(dinfo->serial, serial, sizeof(serial)); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2182 | TAILQ_INSERT_TAIL(&drives, dinfo, next); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2183 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2184 | switch(type) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2185 | case IF_IDE: |
| 2186 | case IF_SCSI: |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 2187 | case IF_XEN: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2188 | switch(media) { |
| 2189 | case MEDIA_DISK: |
| 2190 | if (cyls != 0) { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2191 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
| 2192 | bdrv_set_translation_hint(dinfo->bdrv, translation); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2193 | } |
| 2194 | break; |
| 2195 | case MEDIA_CDROM: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2196 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2197 | break; |
| 2198 | } |
| 2199 | break; |
| 2200 | case IF_SD: |
| 2201 | /* FIXME: This isn't really a floppy, but it's a reasonable |
| 2202 | approximation. */ |
| 2203 | case IF_FLOPPY: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2204 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2205 | break; |
| 2206 | case IF_PFLASH: |
| 2207 | case IF_MTD: |
Gerd Hoffmann | a8659e9 | 2009-07-31 12:25:39 +0200 | [diff] [blame] | 2208 | case IF_NONE: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2209 | break; |
Gerd Hoffmann | d176c49 | 2009-07-31 12:25:41 +0200 | [diff] [blame] | 2210 | case IF_VIRTIO: |
| 2211 | /* add virtio block device */ |
| 2212 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 2213 | qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
| 2214 | qemu_opt_set(opts, "drive", dinfo->id); |
| 2215 | if (devaddr) |
| 2216 | qemu_opt_set(opts, "addr", devaddr); |
| 2217 | break; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 2218 | case IF_COUNT: |
| 2219 | abort(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2220 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2221 | if (!file) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2222 | *fatal_error = 0; |
| 2223 | return NULL; |
| 2224 | } |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2225 | bdrv_flags = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2226 | if (snapshot) { |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2227 | bdrv_flags |= BDRV_O_SNAPSHOT; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2228 | cache = 2; /* always use write-back with snapshot */ |
| 2229 | } |
| 2230 | if (cache == 0) /* no caching */ |
| 2231 | bdrv_flags |= BDRV_O_NOCACHE; |
| 2232 | else if (cache == 2) /* write-back */ |
| 2233 | bdrv_flags |= BDRV_O_CACHE_WB; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2234 | |
| 2235 | if (aio == 1) { |
| 2236 | bdrv_flags |= BDRV_O_NATIVE_AIO; |
| 2237 | } else { |
| 2238 | bdrv_flags &= ~BDRV_O_NATIVE_AIO; |
| 2239 | } |
| 2240 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2241 | if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2242 | fprintf(stderr, "qemu: could not open disk image %s\n", |
| 2243 | file); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2244 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2245 | } |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2246 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2247 | if (bdrv_key_required(dinfo->bdrv)) |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2248 | autostart = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2249 | *fatal_error = 0; |
| 2250 | return dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2253 | static int drive_init_func(QemuOpts *opts, void *opaque) |
| 2254 | { |
| 2255 | QEMUMachine *machine = opaque; |
| 2256 | int fatal_error = 0; |
| 2257 | |
| 2258 | if (drive_init(opts, machine, &fatal_error) == NULL) { |
| 2259 | if (fatal_error) |
| 2260 | return 1; |
| 2261 | } |
| 2262 | return 0; |
| 2263 | } |
| 2264 | |
| 2265 | static int drive_enable_snapshot(QemuOpts *opts, void *opaque) |
| 2266 | { |
| 2267 | if (NULL == qemu_opt_get(opts, "snapshot")) { |
| 2268 | qemu_opt_set(opts, "snapshot", "on"); |
| 2269 | } |
| 2270 | return 0; |
| 2271 | } |
| 2272 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2273 | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
| 2274 | { |
| 2275 | boot_set_handler = func; |
| 2276 | boot_set_opaque = opaque; |
| 2277 | } |
| 2278 | |
| 2279 | int qemu_boot_set(const char *boot_devices) |
| 2280 | { |
| 2281 | if (!boot_set_handler) { |
| 2282 | return -EINVAL; |
| 2283 | } |
| 2284 | return boot_set_handler(boot_set_opaque, boot_devices); |
| 2285 | } |
| 2286 | |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2287 | static int parse_bootdevices(char *devices) |
| 2288 | { |
| 2289 | /* We just do some generic consistency checks */ |
| 2290 | const char *p; |
| 2291 | int bitmap = 0; |
| 2292 | |
| 2293 | for (p = devices; *p != '\0'; p++) { |
| 2294 | /* Allowed boot devices are: |
| 2295 | * a-b: floppy disk drives |
| 2296 | * c-f: IDE disk drives |
| 2297 | * g-m: machine implementation dependant drives |
| 2298 | * n-p: network devices |
| 2299 | * It's up to each machine implementation to check if the given boot |
| 2300 | * devices match the actual hardware implementation and firmware |
| 2301 | * features. |
| 2302 | */ |
| 2303 | if (*p < 'a' || *p > 'p') { |
| 2304 | fprintf(stderr, "Invalid boot device '%c'\n", *p); |
| 2305 | exit(1); |
| 2306 | } |
| 2307 | if (bitmap & (1 << (*p - 'a'))) { |
| 2308 | fprintf(stderr, "Boot device '%c' was given twice\n", *p); |
| 2309 | exit(1); |
| 2310 | } |
| 2311 | bitmap |= 1 << (*p - 'a'); |
| 2312 | } |
| 2313 | return bitmap; |
| 2314 | } |
| 2315 | |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2316 | static void restore_boot_devices(void *opaque) |
| 2317 | { |
| 2318 | char *standard_boot_devices = opaque; |
| 2319 | |
| 2320 | qemu_boot_set(standard_boot_devices); |
| 2321 | |
| 2322 | qemu_unregister_reset(restore_boot_devices, standard_boot_devices); |
| 2323 | qemu_free(standard_boot_devices); |
| 2324 | } |
| 2325 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 2326 | static void numa_add(const char *optarg) |
| 2327 | { |
| 2328 | char option[128]; |
| 2329 | char *endptr; |
| 2330 | unsigned long long value, endvalue; |
| 2331 | int nodenr; |
| 2332 | |
| 2333 | optarg = get_opt_name(option, 128, optarg, ',') + 1; |
| 2334 | if (!strcmp(option, "node")) { |
| 2335 | if (get_param_value(option, 128, "nodeid", optarg) == 0) { |
| 2336 | nodenr = nb_numa_nodes; |
| 2337 | } else { |
| 2338 | nodenr = strtoull(option, NULL, 10); |
| 2339 | } |
| 2340 | |
| 2341 | if (get_param_value(option, 128, "mem", optarg) == 0) { |
| 2342 | node_mem[nodenr] = 0; |
| 2343 | } else { |
| 2344 | value = strtoull(option, &endptr, 0); |
| 2345 | switch (*endptr) { |
| 2346 | case 0: case 'M': case 'm': |
| 2347 | value <<= 20; |
| 2348 | break; |
| 2349 | case 'G': case 'g': |
| 2350 | value <<= 30; |
| 2351 | break; |
| 2352 | } |
| 2353 | node_mem[nodenr] = value; |
| 2354 | } |
| 2355 | if (get_param_value(option, 128, "cpus", optarg) == 0) { |
| 2356 | node_cpumask[nodenr] = 0; |
| 2357 | } else { |
| 2358 | value = strtoull(option, &endptr, 10); |
| 2359 | if (value >= 64) { |
| 2360 | value = 63; |
| 2361 | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n"); |
| 2362 | } else { |
| 2363 | if (*endptr == '-') { |
| 2364 | endvalue = strtoull(endptr+1, &endptr, 10); |
| 2365 | if (endvalue >= 63) { |
| 2366 | endvalue = 62; |
| 2367 | fprintf(stderr, |
| 2368 | "only 63 CPUs in NUMA mode supported.\n"); |
| 2369 | } |
| 2370 | value = (1 << (endvalue + 1)) - (1 << value); |
| 2371 | } else { |
| 2372 | value = 1 << value; |
| 2373 | } |
| 2374 | } |
| 2375 | node_cpumask[nodenr] = value; |
| 2376 | } |
| 2377 | nb_numa_nodes++; |
| 2378 | } |
| 2379 | return; |
| 2380 | } |
| 2381 | |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 2382 | static void smp_parse(const char *optarg) |
| 2383 | { |
| 2384 | int smp, sockets = 0, threads = 0, cores = 0; |
| 2385 | char *endptr; |
| 2386 | char option[128]; |
| 2387 | |
| 2388 | smp = strtoul(optarg, &endptr, 10); |
| 2389 | if (endptr != optarg) { |
| 2390 | if (*endptr == ',') { |
| 2391 | endptr++; |
| 2392 | } |
| 2393 | } |
| 2394 | if (get_param_value(option, 128, "sockets", endptr) != 0) |
| 2395 | sockets = strtoull(option, NULL, 10); |
| 2396 | if (get_param_value(option, 128, "cores", endptr) != 0) |
| 2397 | cores = strtoull(option, NULL, 10); |
| 2398 | if (get_param_value(option, 128, "threads", endptr) != 0) |
| 2399 | threads = strtoull(option, NULL, 10); |
| 2400 | if (get_param_value(option, 128, "maxcpus", endptr) != 0) |
| 2401 | max_cpus = strtoull(option, NULL, 10); |
| 2402 | |
| 2403 | /* compute missing values, prefer sockets over cores over threads */ |
| 2404 | if (smp == 0 || sockets == 0) { |
| 2405 | sockets = sockets > 0 ? sockets : 1; |
| 2406 | cores = cores > 0 ? cores : 1; |
| 2407 | threads = threads > 0 ? threads : 1; |
| 2408 | if (smp == 0) { |
| 2409 | smp = cores * threads * sockets; |
| 2410 | } else { |
| 2411 | sockets = smp / (cores * threads); |
| 2412 | } |
| 2413 | } else { |
| 2414 | if (cores == 0) { |
| 2415 | threads = threads > 0 ? threads : 1; |
| 2416 | cores = smp / (sockets * threads); |
| 2417 | } else { |
| 2418 | if (sockets == 0) { |
| 2419 | sockets = smp / (cores * threads); |
| 2420 | } else { |
| 2421 | threads = smp / (cores * sockets); |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | smp_cpus = smp; |
| 2426 | smp_cores = cores > 0 ? cores : 1; |
| 2427 | smp_threads = threads > 0 ? threads : 1; |
| 2428 | if (max_cpus == 0) |
| 2429 | max_cpus = smp_cpus; |
| 2430 | } |
| 2431 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 2432 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2433 | /* USB devices */ |
| 2434 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2435 | static USBPort *used_usb_ports; |
| 2436 | static USBPort *free_usb_ports; |
| 2437 | |
| 2438 | /* ??? Maybe change this to register a hub to keep track of the topology. */ |
| 2439 | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
| 2440 | usb_attachfn attach) |
| 2441 | { |
| 2442 | port->opaque = opaque; |
| 2443 | port->index = index; |
| 2444 | port->attach = attach; |
| 2445 | port->next = free_usb_ports; |
| 2446 | free_usb_ports = port; |
| 2447 | } |
| 2448 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame] | 2449 | int usb_device_add_dev(USBDevice *dev) |
| 2450 | { |
| 2451 | USBPort *port; |
| 2452 | |
| 2453 | /* Find a USB port to add the device to. */ |
| 2454 | port = free_usb_ports; |
| 2455 | if (!port->next) { |
| 2456 | USBDevice *hub; |
| 2457 | |
| 2458 | /* Create a new hub and chain it on. */ |
| 2459 | free_usb_ports = NULL; |
| 2460 | port->next = used_usb_ports; |
| 2461 | used_usb_ports = port; |
| 2462 | |
| 2463 | hub = usb_hub_init(VM_USB_HUB_SIZE); |
| 2464 | usb_attach(port, hub); |
| 2465 | port = free_usb_ports; |
| 2466 | } |
| 2467 | |
| 2468 | free_usb_ports = port->next; |
| 2469 | port->next = used_usb_ports; |
| 2470 | used_usb_ports = port; |
| 2471 | usb_attach(port, dev); |
| 2472 | return 0; |
| 2473 | } |
| 2474 | |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2475 | static void usb_msd_password_cb(void *opaque, int err) |
| 2476 | { |
| 2477 | USBDevice *dev = opaque; |
| 2478 | |
| 2479 | if (!err) |
| 2480 | usb_device_add_dev(dev); |
| 2481 | else |
| 2482 | dev->handle_destroy(dev); |
| 2483 | } |
| 2484 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2485 | static int usb_device_add(const char *devname, int is_hotplug) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2486 | { |
| 2487 | const char *p; |
| 2488 | USBDevice *dev; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2489 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2490 | if (!free_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2491 | return -1; |
| 2492 | |
| 2493 | if (strstart(devname, "host:", &p)) { |
| 2494 | dev = usb_host_device_open(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2495 | } else if (!strcmp(devname, "mouse")) { |
| 2496 | dev = usb_mouse_init(); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 2497 | } else if (!strcmp(devname, "tablet")) { |
balrog | 47b2d33 | 2007-06-22 08:16:00 +0000 | [diff] [blame] | 2498 | dev = usb_tablet_init(); |
| 2499 | } else if (!strcmp(devname, "keyboard")) { |
| 2500 | dev = usb_keyboard_init(); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 2501 | } else if (strstart(devname, "disk:", &p)) { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2502 | BlockDriverState *bs; |
| 2503 | |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2504 | dev = usb_msd_init(p); |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2505 | if (!dev) |
| 2506 | return -1; |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2507 | bs = usb_msd_get_bdrv(dev); |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2508 | if (bdrv_key_required(bs)) { |
| 2509 | autostart = 0; |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2510 | if (is_hotplug) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2511 | monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, |
| 2512 | dev); |
aliguori | bb5fc20 | 2009-03-05 23:01:15 +0000 | [diff] [blame] | 2513 | return 0; |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2514 | } |
| 2515 | } |
balrog | f6d2a31 | 2007-06-10 19:21:04 +0000 | [diff] [blame] | 2516 | } else if (!strcmp(devname, "wacom-tablet")) { |
| 2517 | dev = usb_wacom_init(); |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2518 | } else if (strstart(devname, "serial:", &p)) { |
| 2519 | dev = usb_serial_init(p); |
aurel32 | 2e4d9fb | 2008-04-08 06:01:02 +0000 | [diff] [blame] | 2520 | #ifdef CONFIG_BRLAPI |
| 2521 | } else if (!strcmp(devname, "braille")) { |
| 2522 | dev = usb_baum_init(); |
| 2523 | #endif |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2524 | } else if (strstart(devname, "net:", &p)) { |
balrog | 9ad97e6 | 2008-07-29 13:16:31 +0000 | [diff] [blame] | 2525 | int nic = nb_nics; |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2526 | |
Jan Kiszka | 10ae5a7 | 2009-05-08 12:34:18 +0200 | [diff] [blame] | 2527 | if (net_client_init(NULL, "nic", p) < 0) |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2528 | return -1; |
balrog | 9ad97e6 | 2008-07-29 13:16:31 +0000 | [diff] [blame] | 2529 | nd_table[nic].model = "usb"; |
| 2530 | dev = usb_net_init(&nd_table[nic]); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 2531 | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
| 2532 | dev = usb_bt_init(devname[2] ? hci_init(p) : |
| 2533 | bt_new_hci(qemu_find_bt_vlan(0))); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2534 | } else { |
| 2535 | return -1; |
| 2536 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2537 | if (!dev) |
| 2538 | return -1; |
| 2539 | |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame] | 2540 | return usb_device_add_dev(dev); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2543 | int usb_device_del_addr(int bus_num, int addr) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2544 | { |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2545 | USBPort *port; |
| 2546 | USBPort **lastp; |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 2547 | USBDevice *dev; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2548 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2549 | if (!used_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2550 | return -1; |
| 2551 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2552 | if (bus_num != 0) |
| 2553 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2554 | |
| 2555 | lastp = &used_usb_ports; |
| 2556 | port = used_usb_ports; |
| 2557 | while (port && port->dev->addr != addr) { |
| 2558 | lastp = &port->next; |
| 2559 | port = port->next; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2560 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2561 | |
| 2562 | if (!port) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2563 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2564 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 2565 | dev = port->dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2566 | *lastp = port->next; |
| 2567 | usb_attach(port, NULL); |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 2568 | dev->handle_destroy(dev); |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2569 | port->next = free_usb_ports; |
| 2570 | free_usb_ports = port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2571 | return 0; |
| 2572 | } |
| 2573 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2574 | static int usb_device_del(const char *devname) |
| 2575 | { |
| 2576 | int bus_num, addr; |
| 2577 | const char *p; |
| 2578 | |
aliguori | 5d0c575 | 2008-09-14 01:07:41 +0000 | [diff] [blame] | 2579 | if (strstart(devname, "host:", &p)) |
| 2580 | return usb_host_device_close(p); |
| 2581 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2582 | if (!used_usb_ports) |
| 2583 | return -1; |
| 2584 | |
| 2585 | p = strchr(devname, '.'); |
| 2586 | if (!p) |
| 2587 | return -1; |
| 2588 | bus_num = strtoul(devname, NULL, 0); |
| 2589 | addr = strtoul(p + 1, NULL, 0); |
| 2590 | |
| 2591 | return usb_device_del_addr(bus_num, addr); |
| 2592 | } |
| 2593 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 2594 | static int usb_parse(const char *cmdline) |
| 2595 | { |
| 2596 | return usb_device_add(cmdline, 0); |
| 2597 | } |
| 2598 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2599 | void do_usb_add(Monitor *mon, const char *devname) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2600 | { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2601 | usb_device_add(devname, 1); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2604 | void do_usb_del(Monitor *mon, const char *devname) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2605 | { |
aliguori | 4b096fc | 2008-08-21 19:28:55 +0000 | [diff] [blame] | 2606 | usb_device_del(devname); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2609 | void usb_info(Monitor *mon) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2610 | { |
| 2611 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2612 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2613 | const char *speed_str; |
| 2614 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2615 | if (!usb_enabled) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2616 | monitor_printf(mon, "USB support not enabled\n"); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2617 | return; |
| 2618 | } |
| 2619 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2620 | for (port = used_usb_ports; port; port = port->next) { |
| 2621 | dev = port->dev; |
| 2622 | if (!dev) |
| 2623 | continue; |
| 2624 | switch(dev->speed) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2625 | case USB_SPEED_LOW: |
| 2626 | speed_str = "1.5"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2627 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2628 | case USB_SPEED_FULL: |
| 2629 | speed_str = "12"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2630 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2631 | case USB_SPEED_HIGH: |
| 2632 | speed_str = "480"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2633 | break; |
| 2634 | default: |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2635 | speed_str = "?"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2636 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2637 | } |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2638 | monitor_printf(mon, " Device %d.%d, Speed %s Mb/s, Product %s\n", |
| 2639 | 0, dev->addr, speed_str, dev->devname); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2640 | } |
| 2641 | } |
| 2642 | |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 2643 | /***********************************************************/ |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2644 | /* PCMCIA/Cardbus */ |
| 2645 | |
| 2646 | static struct pcmcia_socket_entry_s { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2647 | PCMCIASocket *socket; |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2648 | struct pcmcia_socket_entry_s *next; |
| 2649 | } *pcmcia_sockets = 0; |
| 2650 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2651 | void pcmcia_socket_register(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2652 | { |
| 2653 | struct pcmcia_socket_entry_s *entry; |
| 2654 | |
| 2655 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
| 2656 | entry->socket = socket; |
| 2657 | entry->next = pcmcia_sockets; |
| 2658 | pcmcia_sockets = entry; |
| 2659 | } |
| 2660 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2661 | void pcmcia_socket_unregister(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2662 | { |
| 2663 | struct pcmcia_socket_entry_s *entry, **ptr; |
| 2664 | |
| 2665 | ptr = &pcmcia_sockets; |
| 2666 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) |
| 2667 | if (entry->socket == socket) { |
| 2668 | *ptr = entry->next; |
| 2669 | qemu_free(entry); |
| 2670 | } |
| 2671 | } |
| 2672 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2673 | void pcmcia_info(Monitor *mon) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2674 | { |
| 2675 | struct pcmcia_socket_entry_s *iter; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2676 | |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2677 | if (!pcmcia_sockets) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2678 | monitor_printf(mon, "No PCMCIA sockets\n"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2679 | |
| 2680 | for (iter = pcmcia_sockets; iter; iter = iter->next) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2681 | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string, |
| 2682 | iter->socket->attached ? iter->socket->card_string : |
| 2683 | "Empty"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | /***********************************************************/ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2687 | /* register display */ |
| 2688 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2689 | struct DisplayAllocator default_allocator = { |
| 2690 | defaultallocator_create_displaysurface, |
| 2691 | defaultallocator_resize_displaysurface, |
| 2692 | defaultallocator_free_displaysurface |
| 2693 | }; |
| 2694 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2695 | void register_displaystate(DisplayState *ds) |
| 2696 | { |
| 2697 | DisplayState **s; |
| 2698 | s = &display_state; |
| 2699 | while (*s != NULL) |
| 2700 | s = &(*s)->next; |
| 2701 | ds->next = NULL; |
| 2702 | *s = ds; |
| 2703 | } |
| 2704 | |
| 2705 | DisplayState *get_displaystate(void) |
| 2706 | { |
| 2707 | return display_state; |
| 2708 | } |
| 2709 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2710 | DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
| 2711 | { |
| 2712 | if(ds->allocator == &default_allocator) ds->allocator = da; |
| 2713 | return ds->allocator; |
| 2714 | } |
| 2715 | |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2716 | /* dumb display */ |
| 2717 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2718 | static void dumb_display_init(void) |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2719 | { |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2720 | DisplayState *ds = qemu_mallocz(sizeof(DisplayState)); |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2721 | ds->allocator = &default_allocator; |
| 2722 | ds->surface = qemu_create_displaysurface(ds, 640, 480); |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2723 | register_displaystate(ds); |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2727 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 2728 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2729 | typedef struct IOHandlerRecord { |
| 2730 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2731 | IOCanRWHandler *fd_read_poll; |
| 2732 | IOHandler *fd_read; |
| 2733 | IOHandler *fd_write; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2734 | int deleted; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2735 | void *opaque; |
| 2736 | /* temporary data */ |
| 2737 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2738 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2739 | } IOHandlerRecord; |
| 2740 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2741 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2742 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2743 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 2744 | necessary in the character devices to suppress fd_can_read(). */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2745 | int qemu_set_fd_handler2(int fd, |
| 2746 | IOCanRWHandler *fd_read_poll, |
| 2747 | IOHandler *fd_read, |
| 2748 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2749 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2750 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2751 | IOHandlerRecord **pioh, *ioh; |
| 2752 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2753 | if (!fd_read && !fd_write) { |
| 2754 | pioh = &first_io_handler; |
| 2755 | for(;;) { |
| 2756 | ioh = *pioh; |
| 2757 | if (ioh == NULL) |
| 2758 | break; |
| 2759 | if (ioh->fd == fd) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2760 | ioh->deleted = 1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2761 | break; |
| 2762 | } |
| 2763 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2764 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2765 | } else { |
| 2766 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 2767 | if (ioh->fd == fd) |
| 2768 | goto found; |
| 2769 | } |
| 2770 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2771 | ioh->next = first_io_handler; |
| 2772 | first_io_handler = ioh; |
| 2773 | found: |
| 2774 | ioh->fd = fd; |
| 2775 | ioh->fd_read_poll = fd_read_poll; |
| 2776 | ioh->fd_read = fd_read; |
| 2777 | ioh->fd_write = fd_write; |
| 2778 | ioh->opaque = opaque; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2779 | ioh->deleted = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2780 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2781 | return 0; |
| 2782 | } |
| 2783 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2784 | int qemu_set_fd_handler(int fd, |
| 2785 | IOHandler *fd_read, |
| 2786 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2787 | void *opaque) |
| 2788 | { |
| 2789 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 2792 | #ifdef _WIN32 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2793 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2794 | /* Polling handling */ |
| 2795 | |
| 2796 | typedef struct PollingEntry { |
| 2797 | PollingFunc *func; |
| 2798 | void *opaque; |
| 2799 | struct PollingEntry *next; |
| 2800 | } PollingEntry; |
| 2801 | |
| 2802 | static PollingEntry *first_polling_entry; |
| 2803 | |
| 2804 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 2805 | { |
| 2806 | PollingEntry **ppe, *pe; |
| 2807 | pe = qemu_mallocz(sizeof(PollingEntry)); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2808 | pe->func = func; |
| 2809 | pe->opaque = opaque; |
| 2810 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 2811 | *ppe = pe; |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
| 2815 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 2816 | { |
| 2817 | PollingEntry **ppe, *pe; |
| 2818 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 2819 | pe = *ppe; |
| 2820 | if (pe->func == func && pe->opaque == opaque) { |
| 2821 | *ppe = pe->next; |
| 2822 | qemu_free(pe); |
| 2823 | break; |
| 2824 | } |
| 2825 | } |
| 2826 | } |
| 2827 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2828 | /***********************************************************/ |
| 2829 | /* Wait objects support */ |
| 2830 | typedef struct WaitObjects { |
| 2831 | int num; |
| 2832 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2833 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2834 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2835 | } WaitObjects; |
| 2836 | |
| 2837 | static WaitObjects wait_objects = {0}; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2838 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2839 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2840 | { |
| 2841 | WaitObjects *w = &wait_objects; |
| 2842 | |
| 2843 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 2844 | return -1; |
| 2845 | w->events[w->num] = handle; |
| 2846 | w->func[w->num] = func; |
| 2847 | w->opaque[w->num] = opaque; |
| 2848 | w->num++; |
| 2849 | return 0; |
| 2850 | } |
| 2851 | |
| 2852 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2853 | { |
| 2854 | int i, found; |
| 2855 | WaitObjects *w = &wait_objects; |
| 2856 | |
| 2857 | found = 0; |
| 2858 | for (i = 0; i < w->num; i++) { |
| 2859 | if (w->events[i] == handle) |
| 2860 | found = 1; |
| 2861 | if (found) { |
| 2862 | w->events[i] = w->events[i + 1]; |
| 2863 | w->func[i] = w->func[i + 1]; |
| 2864 | w->opaque[i] = w->opaque[i + 1]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2865 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2866 | } |
| 2867 | if (found) |
| 2868 | w->num--; |
| 2869 | } |
| 2870 | #endif |
| 2871 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2872 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2873 | /* ram save/restore */ |
| 2874 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2875 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
| 2876 | { |
| 2877 | int v; |
| 2878 | |
| 2879 | v = qemu_get_byte(f); |
| 2880 | switch(v) { |
| 2881 | case 0: |
| 2882 | if (qemu_get_buffer(f, buf, len) != len) |
| 2883 | return -EIO; |
| 2884 | break; |
| 2885 | case 1: |
| 2886 | v = qemu_get_byte(f); |
| 2887 | memset(buf, v, len); |
| 2888 | break; |
| 2889 | default: |
| 2890 | return -EINVAL; |
| 2891 | } |
aliguori | 871d2f0 | 2008-10-13 03:07:56 +0000 | [diff] [blame] | 2892 | |
| 2893 | if (qemu_file_has_error(f)) |
| 2894 | return -EIO; |
| 2895 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2896 | return 0; |
| 2897 | } |
| 2898 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2899 | static int ram_load_v1(QEMUFile *f, void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2900 | { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 2901 | int ret; |
| 2902 | ram_addr_t i; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2903 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2904 | if (qemu_get_be32(f) != last_ram_offset) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2905 | return -EINVAL; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2906 | for(i = 0; i < last_ram_offset; i+= TARGET_PAGE_SIZE) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2907 | ret = ram_get_page(f, qemu_get_ram_ptr(i), TARGET_PAGE_SIZE); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2908 | if (ret) |
| 2909 | return ret; |
| 2910 | } |
| 2911 | return 0; |
| 2912 | } |
| 2913 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2914 | #define BDRV_HASH_BLOCK_SIZE 1024 |
| 2915 | #define IOBUF_SIZE 4096 |
| 2916 | #define RAM_CBLOCK_MAGIC 0xfabe |
| 2917 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2918 | typedef struct RamDecompressState { |
| 2919 | z_stream zstream; |
| 2920 | QEMUFile *f; |
| 2921 | uint8_t buf[IOBUF_SIZE]; |
| 2922 | } RamDecompressState; |
| 2923 | |
| 2924 | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
| 2925 | { |
| 2926 | int ret; |
| 2927 | memset(s, 0, sizeof(*s)); |
| 2928 | s->f = f; |
| 2929 | ret = inflateInit(&s->zstream); |
| 2930 | if (ret != Z_OK) |
| 2931 | return -1; |
| 2932 | return 0; |
| 2933 | } |
| 2934 | |
| 2935 | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
| 2936 | { |
| 2937 | int ret, clen; |
| 2938 | |
| 2939 | s->zstream.avail_out = len; |
| 2940 | s->zstream.next_out = buf; |
| 2941 | while (s->zstream.avail_out > 0) { |
| 2942 | if (s->zstream.avail_in == 0) { |
| 2943 | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC) |
| 2944 | return -1; |
| 2945 | clen = qemu_get_be16(s->f); |
| 2946 | if (clen > IOBUF_SIZE) |
| 2947 | return -1; |
| 2948 | qemu_get_buffer(s->f, s->buf, clen); |
| 2949 | s->zstream.avail_in = clen; |
| 2950 | s->zstream.next_in = s->buf; |
| 2951 | } |
| 2952 | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
| 2953 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 2954 | return -1; |
| 2955 | } |
| 2956 | } |
| 2957 | return 0; |
| 2958 | } |
| 2959 | |
| 2960 | static void ram_decompress_close(RamDecompressState *s) |
| 2961 | { |
| 2962 | inflateEnd(&s->zstream); |
| 2963 | } |
| 2964 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2965 | #define RAM_SAVE_FLAG_FULL 0x01 |
| 2966 | #define RAM_SAVE_FLAG_COMPRESS 0x02 |
| 2967 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
| 2968 | #define RAM_SAVE_FLAG_PAGE 0x08 |
| 2969 | #define RAM_SAVE_FLAG_EOS 0x10 |
| 2970 | |
| 2971 | static int is_dup_page(uint8_t *page, uint8_t ch) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2972 | { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2973 | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
| 2974 | uint32_t *array = (uint32_t *)page; |
| 2975 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2976 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2977 | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
| 2978 | if (array[i] != val) |
| 2979 | return 0; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2980 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2981 | |
| 2982 | return 1; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2985 | static int ram_save_block(QEMUFile *f) |
| 2986 | { |
| 2987 | static ram_addr_t current_addr = 0; |
| 2988 | ram_addr_t saved_addr = current_addr; |
| 2989 | ram_addr_t addr = 0; |
| 2990 | int found = 0; |
| 2991 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2992 | while (addr < last_ram_offset) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2993 | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2994 | uint8_t *p; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2995 | |
| 2996 | cpu_physical_memory_reset_dirty(current_addr, |
| 2997 | current_addr + TARGET_PAGE_SIZE, |
| 2998 | MIGRATION_DIRTY_FLAG); |
| 2999 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3000 | p = qemu_get_ram_ptr(current_addr); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3001 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3002 | if (is_dup_page(p, *p)) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3003 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3004 | qemu_put_byte(f, *p); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3005 | } else { |
| 3006 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3007 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | found = 1; |
| 3011 | break; |
| 3012 | } |
| 3013 | addr += TARGET_PAGE_SIZE; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3014 | current_addr = (saved_addr + addr) % last_ram_offset; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | return found; |
| 3018 | } |
| 3019 | |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3020 | static uint64_t bytes_transferred = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3021 | |
| 3022 | static ram_addr_t ram_save_remaining(void) |
| 3023 | { |
| 3024 | ram_addr_t addr; |
| 3025 | ram_addr_t count = 0; |
| 3026 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3027 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3028 | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 3029 | count++; |
| 3030 | } |
| 3031 | |
| 3032 | return count; |
| 3033 | } |
| 3034 | |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3035 | uint64_t ram_bytes_remaining(void) |
| 3036 | { |
| 3037 | return ram_save_remaining() * TARGET_PAGE_SIZE; |
| 3038 | } |
| 3039 | |
| 3040 | uint64_t ram_bytes_transferred(void) |
| 3041 | { |
| 3042 | return bytes_transferred; |
| 3043 | } |
| 3044 | |
| 3045 | uint64_t ram_bytes_total(void) |
| 3046 | { |
| 3047 | return last_ram_offset; |
| 3048 | } |
| 3049 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3050 | static int ram_save_live(QEMUFile *f, int stage, void *opaque) |
| 3051 | { |
| 3052 | ram_addr_t addr; |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3053 | uint64_t bytes_transferred_last; |
| 3054 | double bwidth = 0; |
| 3055 | uint64_t expected_time = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3056 | |
Jan Kiszka | 9fa0638 | 2009-05-22 23:51:45 +0200 | [diff] [blame] | 3057 | if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 3058 | qemu_file_set_error(f); |
| 3059 | return 0; |
| 3060 | } |
| 3061 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3062 | if (stage == 1) { |
| 3063 | /* Make sure all dirty bits are set */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3064 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3065 | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 3066 | cpu_physical_memory_set_dirty(addr); |
| 3067 | } |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 3068 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3069 | /* Enable dirty memory tracking */ |
| 3070 | cpu_physical_memory_set_dirty_tracking(1); |
| 3071 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3072 | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3075 | bytes_transferred_last = bytes_transferred; |
| 3076 | bwidth = get_clock(); |
| 3077 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3078 | while (!qemu_file_rate_limit(f)) { |
| 3079 | int ret; |
| 3080 | |
| 3081 | ret = ram_save_block(f); |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3082 | bytes_transferred += ret * TARGET_PAGE_SIZE; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3083 | if (ret == 0) /* no more blocks */ |
| 3084 | break; |
| 3085 | } |
| 3086 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3087 | bwidth = get_clock() - bwidth; |
| 3088 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; |
| 3089 | |
| 3090 | /* if we haven't transferred anything this round, force expected_time to a |
| 3091 | * a very high value, but without crashing */ |
| 3092 | if (bwidth == 0) |
| 3093 | bwidth = 0.000001; |
| 3094 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3095 | /* try transferring iterative blocks of memory */ |
| 3096 | |
| 3097 | if (stage == 3) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3098 | |
| 3099 | /* flush all remaining blocks regardless of rate limiting */ |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3100 | while (ram_save_block(f) != 0) { |
| 3101 | bytes_transferred += TARGET_PAGE_SIZE; |
| 3102 | } |
aliguori | 8215e91 | 2009-04-05 19:30:55 +0000 | [diff] [blame] | 3103 | cpu_physical_memory_set_dirty_tracking(0); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 3107 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3108 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
| 3109 | |
| 3110 | return (stage == 2) && (expected_time <= migrate_max_downtime()); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | static int ram_load_dead(QEMUFile *f, void *opaque) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3114 | { |
| 3115 | RamDecompressState s1, *s = &s1; |
| 3116 | uint8_t buf[10]; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 3117 | ram_addr_t i; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3118 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3119 | if (ram_decompress_open(s, f) < 0) |
| 3120 | return -EINVAL; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3121 | for(i = 0; i < last_ram_offset; i+= BDRV_HASH_BLOCK_SIZE) { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3122 | if (ram_decompress_buf(s, buf, 1) < 0) { |
| 3123 | fprintf(stderr, "Error while reading ram block header\n"); |
| 3124 | goto error; |
| 3125 | } |
| 3126 | if (buf[0] == 0) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3127 | if (ram_decompress_buf(s, qemu_get_ram_ptr(i), |
| 3128 | BDRV_HASH_BLOCK_SIZE) < 0) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 3129 | fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3130 | goto error; |
| 3131 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3132 | } else { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3133 | error: |
| 3134 | printf("Error block header\n"); |
| 3135 | return -EINVAL; |
| 3136 | } |
| 3137 | } |
| 3138 | ram_decompress_close(s); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3139 | |
| 3140 | return 0; |
| 3141 | } |
| 3142 | |
| 3143 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 3144 | { |
| 3145 | ram_addr_t addr; |
| 3146 | int flags; |
| 3147 | |
| 3148 | if (version_id == 1) |
| 3149 | return ram_load_v1(f, opaque); |
| 3150 | |
| 3151 | if (version_id == 2) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3152 | if (qemu_get_be32(f) != last_ram_offset) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3153 | return -EINVAL; |
| 3154 | return ram_load_dead(f, opaque); |
| 3155 | } |
| 3156 | |
| 3157 | if (version_id != 3) |
| 3158 | return -EINVAL; |
| 3159 | |
| 3160 | do { |
| 3161 | addr = qemu_get_be64(f); |
| 3162 | |
| 3163 | flags = addr & ~TARGET_PAGE_MASK; |
| 3164 | addr &= TARGET_PAGE_MASK; |
| 3165 | |
| 3166 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3167 | if (addr != last_ram_offset) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3168 | return -EINVAL; |
| 3169 | } |
| 3170 | |
| 3171 | if (flags & RAM_SAVE_FLAG_FULL) { |
| 3172 | if (ram_load_dead(f, opaque) < 0) |
| 3173 | return -EINVAL; |
| 3174 | } |
| 3175 | |
| 3176 | if (flags & RAM_SAVE_FLAG_COMPRESS) { |
| 3177 | uint8_t ch = qemu_get_byte(f); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3178 | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
| 3179 | #ifndef _WIN32 |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3180 | if (ch == 0 && |
| 3181 | (!kvm_enabled() || kvm_has_sync_mmu())) { |
| 3182 | madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3183 | } |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3184 | #endif |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3185 | } else if (flags & RAM_SAVE_FLAG_PAGE) |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3186 | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3187 | } while (!(flags & RAM_SAVE_FLAG_EOS)); |
| 3188 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3189 | return 0; |
| 3190 | } |
| 3191 | |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3192 | void qemu_service_io(void) |
| 3193 | { |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3194 | qemu_notify_event(); |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3197 | /***********************************************************/ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3198 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 3199 | |
| 3200 | struct QEMUBH { |
| 3201 | QEMUBHFunc *cb; |
| 3202 | void *opaque; |
| 3203 | int scheduled; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3204 | int idle; |
| 3205 | int deleted; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3206 | QEMUBH *next; |
| 3207 | }; |
| 3208 | |
| 3209 | static QEMUBH *first_bh = NULL; |
| 3210 | |
| 3211 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) |
| 3212 | { |
| 3213 | QEMUBH *bh; |
| 3214 | bh = qemu_mallocz(sizeof(QEMUBH)); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3215 | bh->cb = cb; |
| 3216 | bh->opaque = opaque; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3217 | bh->next = first_bh; |
| 3218 | first_bh = bh; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3219 | return bh; |
| 3220 | } |
| 3221 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3222 | int qemu_bh_poll(void) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3223 | { |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3224 | QEMUBH *bh, **bhp; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3225 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3226 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3227 | ret = 0; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3228 | for (bh = first_bh; bh; bh = bh->next) { |
| 3229 | if (!bh->deleted && bh->scheduled) { |
| 3230 | bh->scheduled = 0; |
| 3231 | if (!bh->idle) |
| 3232 | ret = 1; |
| 3233 | bh->idle = 0; |
| 3234 | bh->cb(bh->opaque); |
| 3235 | } |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3236 | } |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3237 | |
| 3238 | /* remove deleted bhs */ |
| 3239 | bhp = &first_bh; |
| 3240 | while (*bhp) { |
| 3241 | bh = *bhp; |
| 3242 | if (bh->deleted) { |
| 3243 | *bhp = bh->next; |
| 3244 | qemu_free(bh); |
| 3245 | } else |
| 3246 | bhp = &bh->next; |
| 3247 | } |
| 3248 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 3249 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3252 | void qemu_bh_schedule_idle(QEMUBH *bh) |
| 3253 | { |
| 3254 | if (bh->scheduled) |
| 3255 | return; |
| 3256 | bh->scheduled = 1; |
| 3257 | bh->idle = 1; |
| 3258 | } |
| 3259 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3260 | void qemu_bh_schedule(QEMUBH *bh) |
| 3261 | { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3262 | if (bh->scheduled) |
| 3263 | return; |
| 3264 | bh->scheduled = 1; |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3265 | bh->idle = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3266 | /* stop the currently executing CPU to execute the BH ASAP */ |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3267 | qemu_notify_event(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | void qemu_bh_cancel(QEMUBH *bh) |
| 3271 | { |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3272 | bh->scheduled = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | void qemu_bh_delete(QEMUBH *bh) |
| 3276 | { |
aliguori | 1b435b1 | 2008-10-31 17:24:21 +0000 | [diff] [blame] | 3277 | bh->scheduled = 0; |
| 3278 | bh->deleted = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3281 | static void qemu_bh_update_timeout(int *timeout) |
| 3282 | { |
| 3283 | QEMUBH *bh; |
| 3284 | |
| 3285 | for (bh = first_bh; bh; bh = bh->next) { |
| 3286 | if (!bh->deleted && bh->scheduled) { |
| 3287 | if (bh->idle) { |
| 3288 | /* idle bottom halves will be polled at least |
| 3289 | * every 10ms */ |
| 3290 | *timeout = MIN(10, *timeout); |
| 3291 | } else { |
| 3292 | /* non-idle bottom halves will be executed |
| 3293 | * immediately */ |
| 3294 | *timeout = 0; |
| 3295 | break; |
| 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 3301 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3302 | /* machine registration */ |
| 3303 | |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 3304 | static QEMUMachine *first_machine = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 3305 | QEMUMachine *current_machine = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3306 | |
| 3307 | int qemu_register_machine(QEMUMachine *m) |
| 3308 | { |
| 3309 | QEMUMachine **pm; |
| 3310 | pm = &first_machine; |
| 3311 | while (*pm != NULL) |
| 3312 | pm = &(*pm)->next; |
| 3313 | m->next = NULL; |
| 3314 | *pm = m; |
| 3315 | return 0; |
| 3316 | } |
| 3317 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3318 | static QEMUMachine *find_machine(const char *name) |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3319 | { |
| 3320 | QEMUMachine *m; |
| 3321 | |
| 3322 | for(m = first_machine; m != NULL; m = m->next) { |
| 3323 | if (!strcmp(m->name, name)) |
| 3324 | return m; |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 3325 | if (m->alias && !strcmp(m->alias, name)) |
| 3326 | return m; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3327 | } |
| 3328 | return NULL; |
| 3329 | } |
| 3330 | |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 3331 | static QEMUMachine *find_default_machine(void) |
| 3332 | { |
| 3333 | QEMUMachine *m; |
| 3334 | |
| 3335 | for(m = first_machine; m != NULL; m = m->next) { |
| 3336 | if (m->is_default) { |
| 3337 | return m; |
| 3338 | } |
| 3339 | } |
| 3340 | return NULL; |
| 3341 | } |
| 3342 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3343 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3344 | /* main execution loop */ |
| 3345 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3346 | static void gui_update(void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3347 | { |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3348 | uint64_t interval = GUI_REFRESH_INTERVAL; |
ths | 740733b | 2007-06-08 01:57:56 +0000 | [diff] [blame] | 3349 | DisplayState *ds = opaque; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3350 | DisplayChangeListener *dcl = ds->listeners; |
| 3351 | |
| 3352 | dpy_refresh(ds); |
| 3353 | |
| 3354 | while (dcl != NULL) { |
| 3355 | if (dcl->gui_timer_interval && |
| 3356 | dcl->gui_timer_interval < interval) |
| 3357 | interval = dcl->gui_timer_interval; |
| 3358 | dcl = dcl->next; |
| 3359 | } |
| 3360 | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 3363 | static void nographic_update(void *opaque) |
| 3364 | { |
| 3365 | uint64_t interval = GUI_REFRESH_INTERVAL; |
| 3366 | |
| 3367 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); |
| 3368 | } |
| 3369 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3370 | struct vm_change_state_entry { |
| 3371 | VMChangeStateHandler *cb; |
| 3372 | void *opaque; |
| 3373 | LIST_ENTRY (vm_change_state_entry) entries; |
| 3374 | }; |
| 3375 | |
| 3376 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
| 3377 | |
| 3378 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 3379 | void *opaque) |
| 3380 | { |
| 3381 | VMChangeStateEntry *e; |
| 3382 | |
| 3383 | e = qemu_mallocz(sizeof (*e)); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3384 | |
| 3385 | e->cb = cb; |
| 3386 | e->opaque = opaque; |
| 3387 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
| 3388 | return e; |
| 3389 | } |
| 3390 | |
| 3391 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 3392 | { |
| 3393 | LIST_REMOVE (e, entries); |
| 3394 | qemu_free (e); |
| 3395 | } |
| 3396 | |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3397 | static void vm_state_notify(int running, int reason) |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3398 | { |
| 3399 | VMChangeStateEntry *e; |
| 3400 | |
| 3401 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3402 | e->cb(e->opaque, running, reason); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3403 | } |
| 3404 | } |
| 3405 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3406 | static void resume_all_vcpus(void); |
| 3407 | static void pause_all_vcpus(void); |
| 3408 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3409 | void vm_start(void) |
| 3410 | { |
| 3411 | if (!vm_running) { |
| 3412 | cpu_enable_ticks(); |
| 3413 | vm_running = 1; |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3414 | vm_state_notify(1, 0); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 3415 | qemu_rearm_alarm_timer(alarm_timer); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3416 | resume_all_vcpus(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3417 | } |
| 3418 | } |
| 3419 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3420 | /* reset/shutdown handler */ |
| 3421 | |
| 3422 | typedef struct QEMUResetEntry { |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3423 | TAILQ_ENTRY(QEMUResetEntry) entry; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3424 | QEMUResetHandler *func; |
| 3425 | void *opaque; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3426 | } QEMUResetEntry; |
| 3427 | |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3428 | static TAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers = |
| 3429 | TAILQ_HEAD_INITIALIZER(reset_handlers); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3430 | static int reset_requested; |
| 3431 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3432 | static int powerdown_requested; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3433 | static int debug_requested; |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3434 | static int vmstop_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3435 | |
aurel32 | cf7a2fe | 2008-03-18 06:53:05 +0000 | [diff] [blame] | 3436 | int qemu_shutdown_requested(void) |
| 3437 | { |
| 3438 | int r = shutdown_requested; |
| 3439 | shutdown_requested = 0; |
| 3440 | return r; |
| 3441 | } |
| 3442 | |
| 3443 | int qemu_reset_requested(void) |
| 3444 | { |
| 3445 | int r = reset_requested; |
| 3446 | reset_requested = 0; |
| 3447 | return r; |
| 3448 | } |
| 3449 | |
| 3450 | int qemu_powerdown_requested(void) |
| 3451 | { |
| 3452 | int r = powerdown_requested; |
| 3453 | powerdown_requested = 0; |
| 3454 | return r; |
| 3455 | } |
| 3456 | |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3457 | static int qemu_debug_requested(void) |
| 3458 | { |
| 3459 | int r = debug_requested; |
| 3460 | debug_requested = 0; |
| 3461 | return r; |
| 3462 | } |
| 3463 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3464 | static int qemu_vmstop_requested(void) |
| 3465 | { |
| 3466 | int r = vmstop_requested; |
| 3467 | vmstop_requested = 0; |
| 3468 | return r; |
| 3469 | } |
| 3470 | |
| 3471 | static void do_vm_stop(int reason) |
| 3472 | { |
| 3473 | if (vm_running) { |
| 3474 | cpu_disable_ticks(); |
| 3475 | vm_running = 0; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3476 | pause_all_vcpus(); |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3477 | vm_state_notify(0, reason); |
| 3478 | } |
| 3479 | } |
| 3480 | |
Jan Kiszka | a08d436 | 2009-06-27 09:25:07 +0200 | [diff] [blame] | 3481 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3482 | { |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3483 | QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry)); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3484 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3485 | re->func = func; |
| 3486 | re->opaque = opaque; |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3487 | TAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3490 | void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3491 | { |
| 3492 | QEMUResetEntry *re; |
| 3493 | |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3494 | TAILQ_FOREACH(re, &reset_handlers, entry) { |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3495 | if (re->func == func && re->opaque == opaque) { |
| 3496 | TAILQ_REMOVE(&reset_handlers, re, entry); |
| 3497 | qemu_free(re); |
| 3498 | return; |
| 3499 | } |
| 3500 | } |
| 3501 | } |
| 3502 | |
| 3503 | void qemu_system_reset(void) |
| 3504 | { |
| 3505 | QEMUResetEntry *re, *nre; |
| 3506 | |
| 3507 | /* reset all devices */ |
| 3508 | TAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3509 | re->func(re->opaque); |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | void qemu_system_reset_request(void) |
| 3514 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 3515 | if (no_reboot) { |
| 3516 | shutdown_requested = 1; |
| 3517 | } else { |
| 3518 | reset_requested = 1; |
| 3519 | } |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3520 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | void qemu_system_shutdown_request(void) |
| 3524 | { |
| 3525 | shutdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3526 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3529 | void qemu_system_powerdown_request(void) |
| 3530 | { |
| 3531 | powerdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3532 | qemu_notify_event(); |
| 3533 | } |
| 3534 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3535 | #ifdef CONFIG_IOTHREAD |
| 3536 | static void qemu_system_vmstop_request(int reason) |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3537 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3538 | vmstop_requested = reason; |
| 3539 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3540 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3541 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3542 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3543 | #ifndef _WIN32 |
| 3544 | static int io_thread_fd = -1; |
| 3545 | |
| 3546 | static void qemu_event_increment(void) |
| 3547 | { |
| 3548 | static const char byte = 0; |
| 3549 | |
| 3550 | if (io_thread_fd == -1) |
| 3551 | return; |
| 3552 | |
| 3553 | write(io_thread_fd, &byte, sizeof(byte)); |
| 3554 | } |
| 3555 | |
| 3556 | static void qemu_event_read(void *opaque) |
| 3557 | { |
| 3558 | int fd = (unsigned long)opaque; |
| 3559 | ssize_t len; |
| 3560 | |
| 3561 | /* Drain the notify pipe */ |
| 3562 | do { |
| 3563 | char buffer[512]; |
| 3564 | len = read(fd, buffer, sizeof(buffer)); |
| 3565 | } while ((len == -1 && errno == EINTR) || len > 0); |
| 3566 | } |
| 3567 | |
| 3568 | static int qemu_event_init(void) |
| 3569 | { |
| 3570 | int err; |
| 3571 | int fds[2]; |
| 3572 | |
| 3573 | err = pipe(fds); |
| 3574 | if (err == -1) |
| 3575 | return -errno; |
| 3576 | |
| 3577 | err = fcntl_setfl(fds[0], O_NONBLOCK); |
| 3578 | if (err < 0) |
| 3579 | goto fail; |
| 3580 | |
| 3581 | err = fcntl_setfl(fds[1], O_NONBLOCK); |
| 3582 | if (err < 0) |
| 3583 | goto fail; |
| 3584 | |
| 3585 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
| 3586 | (void *)(unsigned long)fds[0]); |
| 3587 | |
| 3588 | io_thread_fd = fds[1]; |
Jan Kiszka | a7e2121 | 2009-04-29 18:38:28 +0000 | [diff] [blame] | 3589 | return 0; |
| 3590 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3591 | fail: |
| 3592 | close(fds[0]); |
| 3593 | close(fds[1]); |
| 3594 | return err; |
| 3595 | } |
| 3596 | #else |
| 3597 | HANDLE qemu_event_handle; |
| 3598 | |
| 3599 | static void dummy_event_handler(void *opaque) |
| 3600 | { |
| 3601 | } |
| 3602 | |
| 3603 | static int qemu_event_init(void) |
| 3604 | { |
| 3605 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 3606 | if (!qemu_event_handle) { |
| 3607 | perror("Failed CreateEvent"); |
| 3608 | return -1; |
| 3609 | } |
| 3610 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
| 3611 | return 0; |
| 3612 | } |
| 3613 | |
| 3614 | static void qemu_event_increment(void) |
| 3615 | { |
| 3616 | SetEvent(qemu_event_handle); |
| 3617 | } |
| 3618 | #endif |
| 3619 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3620 | static int cpu_can_run(CPUState *env) |
| 3621 | { |
| 3622 | if (env->stop) |
| 3623 | return 0; |
| 3624 | if (env->stopped) |
| 3625 | return 0; |
| 3626 | return 1; |
| 3627 | } |
| 3628 | |
| 3629 | #ifndef CONFIG_IOTHREAD |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3630 | static int qemu_init_main_loop(void) |
| 3631 | { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3632 | return qemu_event_init(); |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3635 | void qemu_init_vcpu(void *_env) |
| 3636 | { |
| 3637 | CPUState *env = _env; |
| 3638 | |
| 3639 | if (kvm_enabled()) |
| 3640 | kvm_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3641 | env->nr_cores = smp_cores; |
| 3642 | env->nr_threads = smp_threads; |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3643 | return; |
| 3644 | } |
| 3645 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3646 | int qemu_cpu_self(void *env) |
| 3647 | { |
| 3648 | return 1; |
| 3649 | } |
| 3650 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3651 | static void resume_all_vcpus(void) |
| 3652 | { |
| 3653 | } |
| 3654 | |
| 3655 | static void pause_all_vcpus(void) |
| 3656 | { |
| 3657 | } |
| 3658 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3659 | void qemu_cpu_kick(void *env) |
| 3660 | { |
| 3661 | return; |
| 3662 | } |
| 3663 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3664 | void qemu_notify_event(void) |
| 3665 | { |
| 3666 | CPUState *env = cpu_single_env; |
| 3667 | |
| 3668 | if (env) { |
| 3669 | cpu_exit(env); |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 3670 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3673 | #define qemu_mutex_lock_iothread() do { } while (0) |
| 3674 | #define qemu_mutex_unlock_iothread() do { } while (0) |
| 3675 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3676 | void vm_stop(int reason) |
| 3677 | { |
| 3678 | do_vm_stop(reason); |
| 3679 | } |
| 3680 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3681 | #else /* CONFIG_IOTHREAD */ |
| 3682 | |
| 3683 | #include "qemu-thread.h" |
| 3684 | |
| 3685 | QemuMutex qemu_global_mutex; |
| 3686 | static QemuMutex qemu_fair_mutex; |
| 3687 | |
| 3688 | static QemuThread io_thread; |
| 3689 | |
| 3690 | static QemuThread *tcg_cpu_thread; |
| 3691 | static QemuCond *tcg_halt_cond; |
| 3692 | |
| 3693 | static int qemu_system_ready; |
| 3694 | /* cpu creation */ |
| 3695 | static QemuCond qemu_cpu_cond; |
| 3696 | /* system init */ |
| 3697 | static QemuCond qemu_system_cond; |
| 3698 | static QemuCond qemu_pause_cond; |
| 3699 | |
| 3700 | static void block_io_signals(void); |
| 3701 | static void unblock_io_signals(void); |
| 3702 | static int tcg_has_work(void); |
| 3703 | |
| 3704 | static int qemu_init_main_loop(void) |
| 3705 | { |
| 3706 | int ret; |
| 3707 | |
| 3708 | ret = qemu_event_init(); |
| 3709 | if (ret) |
| 3710 | return ret; |
| 3711 | |
| 3712 | qemu_cond_init(&qemu_pause_cond); |
| 3713 | qemu_mutex_init(&qemu_fair_mutex); |
| 3714 | qemu_mutex_init(&qemu_global_mutex); |
| 3715 | qemu_mutex_lock(&qemu_global_mutex); |
| 3716 | |
| 3717 | unblock_io_signals(); |
| 3718 | qemu_thread_self(&io_thread); |
| 3719 | |
| 3720 | return 0; |
| 3721 | } |
| 3722 | |
| 3723 | static void qemu_wait_io_event(CPUState *env) |
| 3724 | { |
| 3725 | while (!tcg_has_work()) |
| 3726 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); |
| 3727 | |
| 3728 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3729 | |
| 3730 | /* |
| 3731 | * Users of qemu_global_mutex can be starved, having no chance |
| 3732 | * to acquire it since this path will get to it first. |
| 3733 | * So use another lock to provide fairness. |
| 3734 | */ |
| 3735 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3736 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3737 | |
| 3738 | qemu_mutex_lock(&qemu_global_mutex); |
| 3739 | if (env->stop) { |
| 3740 | env->stop = 0; |
| 3741 | env->stopped = 1; |
| 3742 | qemu_cond_signal(&qemu_pause_cond); |
| 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | static int qemu_cpu_exec(CPUState *env); |
| 3747 | |
| 3748 | static void *kvm_cpu_thread_fn(void *arg) |
| 3749 | { |
| 3750 | CPUState *env = arg; |
| 3751 | |
| 3752 | block_io_signals(); |
| 3753 | qemu_thread_self(env->thread); |
| 3754 | |
| 3755 | /* signal CPU creation */ |
| 3756 | qemu_mutex_lock(&qemu_global_mutex); |
| 3757 | env->created = 1; |
| 3758 | qemu_cond_signal(&qemu_cpu_cond); |
| 3759 | |
| 3760 | /* and wait for machine initialization */ |
| 3761 | while (!qemu_system_ready) |
| 3762 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3763 | |
| 3764 | while (1) { |
| 3765 | if (cpu_can_run(env)) |
| 3766 | qemu_cpu_exec(env); |
| 3767 | qemu_wait_io_event(env); |
| 3768 | } |
| 3769 | |
| 3770 | return NULL; |
| 3771 | } |
| 3772 | |
| 3773 | static void tcg_cpu_exec(void); |
| 3774 | |
| 3775 | static void *tcg_cpu_thread_fn(void *arg) |
| 3776 | { |
| 3777 | CPUState *env = arg; |
| 3778 | |
| 3779 | block_io_signals(); |
| 3780 | qemu_thread_self(env->thread); |
| 3781 | |
| 3782 | /* signal CPU creation */ |
| 3783 | qemu_mutex_lock(&qemu_global_mutex); |
| 3784 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 3785 | env->created = 1; |
| 3786 | qemu_cond_signal(&qemu_cpu_cond); |
| 3787 | |
| 3788 | /* and wait for machine initialization */ |
| 3789 | while (!qemu_system_ready) |
| 3790 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3791 | |
| 3792 | while (1) { |
| 3793 | tcg_cpu_exec(); |
| 3794 | qemu_wait_io_event(cur_cpu); |
| 3795 | } |
| 3796 | |
| 3797 | return NULL; |
| 3798 | } |
| 3799 | |
| 3800 | void qemu_cpu_kick(void *_env) |
| 3801 | { |
| 3802 | CPUState *env = _env; |
| 3803 | qemu_cond_broadcast(env->halt_cond); |
| 3804 | if (kvm_enabled()) |
| 3805 | qemu_thread_signal(env->thread, SIGUSR1); |
| 3806 | } |
| 3807 | |
| 3808 | int qemu_cpu_self(void *env) |
| 3809 | { |
| 3810 | return (cpu_single_env != NULL); |
| 3811 | } |
| 3812 | |
| 3813 | static void cpu_signal(int sig) |
| 3814 | { |
| 3815 | if (cpu_single_env) |
| 3816 | cpu_exit(cpu_single_env); |
| 3817 | } |
| 3818 | |
| 3819 | static void block_io_signals(void) |
| 3820 | { |
| 3821 | sigset_t set; |
| 3822 | struct sigaction sigact; |
| 3823 | |
| 3824 | sigemptyset(&set); |
| 3825 | sigaddset(&set, SIGUSR2); |
| 3826 | sigaddset(&set, SIGIO); |
| 3827 | sigaddset(&set, SIGALRM); |
| 3828 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3829 | |
| 3830 | sigemptyset(&set); |
| 3831 | sigaddset(&set, SIGUSR1); |
| 3832 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3833 | |
| 3834 | memset(&sigact, 0, sizeof(sigact)); |
| 3835 | sigact.sa_handler = cpu_signal; |
| 3836 | sigaction(SIGUSR1, &sigact, NULL); |
| 3837 | } |
| 3838 | |
| 3839 | static void unblock_io_signals(void) |
| 3840 | { |
| 3841 | sigset_t set; |
| 3842 | |
| 3843 | sigemptyset(&set); |
| 3844 | sigaddset(&set, SIGUSR2); |
| 3845 | sigaddset(&set, SIGIO); |
| 3846 | sigaddset(&set, SIGALRM); |
| 3847 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3848 | |
| 3849 | sigemptyset(&set); |
| 3850 | sigaddset(&set, SIGUSR1); |
| 3851 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3852 | } |
| 3853 | |
| 3854 | static void qemu_signal_lock(unsigned int msecs) |
| 3855 | { |
| 3856 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3857 | |
| 3858 | while (qemu_mutex_trylock(&qemu_global_mutex)) { |
| 3859 | qemu_thread_signal(tcg_cpu_thread, SIGUSR1); |
| 3860 | if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs)) |
| 3861 | break; |
| 3862 | } |
| 3863 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3864 | } |
| 3865 | |
| 3866 | static void qemu_mutex_lock_iothread(void) |
| 3867 | { |
| 3868 | if (kvm_enabled()) { |
| 3869 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3870 | qemu_mutex_lock(&qemu_global_mutex); |
| 3871 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3872 | } else |
| 3873 | qemu_signal_lock(100); |
| 3874 | } |
| 3875 | |
| 3876 | static void qemu_mutex_unlock_iothread(void) |
| 3877 | { |
| 3878 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3879 | } |
| 3880 | |
| 3881 | static int all_vcpus_paused(void) |
| 3882 | { |
| 3883 | CPUState *penv = first_cpu; |
| 3884 | |
| 3885 | while (penv) { |
| 3886 | if (!penv->stopped) |
| 3887 | return 0; |
| 3888 | penv = (CPUState *)penv->next_cpu; |
| 3889 | } |
| 3890 | |
| 3891 | return 1; |
| 3892 | } |
| 3893 | |
| 3894 | static void pause_all_vcpus(void) |
| 3895 | { |
| 3896 | CPUState *penv = first_cpu; |
| 3897 | |
| 3898 | while (penv) { |
| 3899 | penv->stop = 1; |
| 3900 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3901 | qemu_cpu_kick(penv); |
| 3902 | penv = (CPUState *)penv->next_cpu; |
| 3903 | } |
| 3904 | |
| 3905 | while (!all_vcpus_paused()) { |
| 3906 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); |
| 3907 | penv = first_cpu; |
| 3908 | while (penv) { |
| 3909 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3910 | penv = (CPUState *)penv->next_cpu; |
| 3911 | } |
| 3912 | } |
| 3913 | } |
| 3914 | |
| 3915 | static void resume_all_vcpus(void) |
| 3916 | { |
| 3917 | CPUState *penv = first_cpu; |
| 3918 | |
| 3919 | while (penv) { |
| 3920 | penv->stop = 0; |
| 3921 | penv->stopped = 0; |
| 3922 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3923 | qemu_cpu_kick(penv); |
| 3924 | penv = (CPUState *)penv->next_cpu; |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | static void tcg_init_vcpu(void *_env) |
| 3929 | { |
| 3930 | CPUState *env = _env; |
| 3931 | /* share a single thread for all cpus with TCG */ |
| 3932 | if (!tcg_cpu_thread) { |
| 3933 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3934 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3935 | qemu_cond_init(env->halt_cond); |
| 3936 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
| 3937 | while (env->created == 0) |
| 3938 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3939 | tcg_cpu_thread = env->thread; |
| 3940 | tcg_halt_cond = env->halt_cond; |
| 3941 | } else { |
| 3942 | env->thread = tcg_cpu_thread; |
| 3943 | env->halt_cond = tcg_halt_cond; |
| 3944 | } |
| 3945 | } |
| 3946 | |
| 3947 | static void kvm_start_vcpu(CPUState *env) |
| 3948 | { |
| 3949 | kvm_init_vcpu(env); |
| 3950 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3951 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3952 | qemu_cond_init(env->halt_cond); |
| 3953 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
| 3954 | while (env->created == 0) |
| 3955 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3956 | } |
| 3957 | |
| 3958 | void qemu_init_vcpu(void *_env) |
| 3959 | { |
| 3960 | CPUState *env = _env; |
| 3961 | |
| 3962 | if (kvm_enabled()) |
| 3963 | kvm_start_vcpu(env); |
| 3964 | else |
| 3965 | tcg_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3966 | env->nr_cores = smp_cores; |
| 3967 | env->nr_threads = smp_threads; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
| 3970 | void qemu_notify_event(void) |
| 3971 | { |
| 3972 | qemu_event_increment(); |
| 3973 | } |
| 3974 | |
| 3975 | void vm_stop(int reason) |
| 3976 | { |
| 3977 | QemuThread me; |
| 3978 | qemu_thread_self(&me); |
| 3979 | |
| 3980 | if (!qemu_thread_equal(&me, &io_thread)) { |
| 3981 | qemu_system_vmstop_request(reason); |
| 3982 | /* |
| 3983 | * FIXME: should not return to device code in case |
| 3984 | * vm_stop() has been requested. |
| 3985 | */ |
| 3986 | if (cpu_single_env) { |
| 3987 | cpu_exit(cpu_single_env); |
| 3988 | cpu_single_env->stop = 1; |
| 3989 | } |
| 3990 | return; |
| 3991 | } |
| 3992 | do_vm_stop(reason); |
| 3993 | } |
| 3994 | |
| 3995 | #endif |
| 3996 | |
| 3997 | |
ths | 877cf88 | 2007-04-18 18:11:47 +0000 | [diff] [blame] | 3998 | #ifdef _WIN32 |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3999 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4000 | { |
| 4001 | int ret, ret2, i; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4002 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4003 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4004 | |
| 4005 | /* XXX: need to suppress polling by better using win32 events */ |
| 4006 | ret = 0; |
| 4007 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 4008 | ret |= pe->func(pe->opaque); |
| 4009 | } |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 4010 | if (ret == 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4011 | int err; |
| 4012 | WaitObjects *w = &wait_objects; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4013 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4014 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4015 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 4016 | if (w->func[ret - WAIT_OBJECT_0]) |
| 4017 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4018 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4019 | /* Check for additional signaled events */ |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 4020 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4021 | |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 4022 | /* Check if event is signaled */ |
| 4023 | ret2 = WaitForSingleObject(w->events[i], 0); |
| 4024 | if(ret2 == WAIT_OBJECT_0) { |
| 4025 | if (w->func[i]) |
| 4026 | w->func[i](w->opaque[i]); |
| 4027 | } else if (ret2 == WAIT_TIMEOUT) { |
| 4028 | } else { |
| 4029 | err = GetLastError(); |
| 4030 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4031 | } |
| 4032 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4033 | } else if (ret == WAIT_TIMEOUT) { |
| 4034 | } else { |
| 4035 | err = GetLastError(); |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 4036 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4037 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4038 | } |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4039 | |
| 4040 | *timeout = 0; |
| 4041 | } |
| 4042 | #else |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 4043 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4044 | { |
| 4045 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4046 | #endif |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4047 | |
| 4048 | void main_loop_wait(int timeout) |
| 4049 | { |
| 4050 | IOHandlerRecord *ioh; |
| 4051 | fd_set rfds, wfds, xfds; |
| 4052 | int ret, nfds; |
| 4053 | struct timeval tv; |
| 4054 | |
| 4055 | qemu_bh_update_timeout(&timeout); |
| 4056 | |
| 4057 | host_main_loop_wait(&timeout); |
| 4058 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4059 | /* poll any events */ |
| 4060 | /* XXX: separate device handlers from system ones */ |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 4061 | nfds = -1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4062 | FD_ZERO(&rfds); |
| 4063 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 4064 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4065 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4066 | if (ioh->deleted) |
| 4067 | continue; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4068 | if (ioh->fd_read && |
| 4069 | (!ioh->fd_read_poll || |
| 4070 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 4071 | FD_SET(ioh->fd, &rfds); |
| 4072 | if (ioh->fd > nfds) |
| 4073 | nfds = ioh->fd; |
| 4074 | } |
| 4075 | if (ioh->fd_write) { |
| 4076 | FD_SET(ioh->fd, &wfds); |
| 4077 | if (ioh->fd > nfds) |
| 4078 | nfds = ioh->fd; |
| 4079 | } |
| 4080 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4081 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 4082 | tv.tv_sec = timeout / 1000; |
| 4083 | tv.tv_usec = (timeout % 1000) * 1000; |
| 4084 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 4085 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 4086 | |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 4087 | qemu_mutex_unlock_iothread(); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 4088 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 4089 | qemu_mutex_lock_iothread(); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4090 | if (ret > 0) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4091 | IOHandlerRecord **pioh; |
| 4092 | |
| 4093 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 4094 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4095 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4096 | } |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 4097 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4098 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4099 | } |
| 4100 | } |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4101 | |
| 4102 | /* remove deleted IO handlers */ |
| 4103 | pioh = &first_io_handler; |
| 4104 | while (*pioh) { |
| 4105 | ioh = *pioh; |
| 4106 | if (ioh->deleted) { |
| 4107 | *pioh = ioh->next; |
| 4108 | qemu_free(ioh); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4109 | } else |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 4110 | pioh = &ioh->next; |
| 4111 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4112 | } |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 4113 | |
| 4114 | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4115 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 4116 | /* rearm timer, if not periodic */ |
| 4117 | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) { |
| 4118 | alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; |
| 4119 | qemu_rearm_alarm_timer(alarm_timer); |
| 4120 | } |
| 4121 | |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 4122 | /* vm time timers */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4123 | if (vm_running) { |
| 4124 | if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER))) |
| 4125 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
| 4126 | qemu_get_clock(vm_clock)); |
| 4127 | } |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 4128 | |
| 4129 | /* real time timers */ |
| 4130 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
| 4131 | qemu_get_clock(rt_clock)); |
| 4132 | |
pbrook | 423f074 | 2007-05-23 00:06:54 +0000 | [diff] [blame] | 4133 | /* Check bottom-halves last in case any of the earlier events triggered |
| 4134 | them. */ |
| 4135 | qemu_bh_poll(); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4136 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4139 | static int qemu_cpu_exec(CPUState *env) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4140 | { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4141 | int ret; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 4142 | #ifdef CONFIG_PROFILER |
| 4143 | int64_t ti; |
| 4144 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4145 | |
| 4146 | #ifdef CONFIG_PROFILER |
| 4147 | ti = profile_getclock(); |
| 4148 | #endif |
| 4149 | if (use_icount) { |
| 4150 | int64_t count; |
| 4151 | int decr; |
| 4152 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 4153 | env->icount_decr.u16.low = 0; |
| 4154 | env->icount_extra = 0; |
| 4155 | count = qemu_next_deadline(); |
| 4156 | count = (count + (1 << icount_time_shift) - 1) |
| 4157 | >> icount_time_shift; |
| 4158 | qemu_icount += count; |
| 4159 | decr = (count > 0xffff) ? 0xffff : count; |
| 4160 | count -= decr; |
| 4161 | env->icount_decr.u16.low = decr; |
| 4162 | env->icount_extra = count; |
| 4163 | } |
| 4164 | ret = cpu_exec(env); |
| 4165 | #ifdef CONFIG_PROFILER |
| 4166 | qemu_time += profile_getclock() - ti; |
| 4167 | #endif |
| 4168 | if (use_icount) { |
| 4169 | /* Fold pending instructions back into the |
| 4170 | instruction counter, and clear the interrupt flag. */ |
| 4171 | qemu_icount -= (env->icount_decr.u16.low |
| 4172 | + env->icount_extra); |
| 4173 | env->icount_decr.u32 = 0; |
| 4174 | env->icount_extra = 0; |
| 4175 | } |
| 4176 | return ret; |
| 4177 | } |
| 4178 | |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4179 | static void tcg_cpu_exec(void) |
| 4180 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4181 | int ret = 0; |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4182 | |
| 4183 | if (next_cpu == NULL) |
| 4184 | next_cpu = first_cpu; |
| 4185 | for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { |
| 4186 | CPUState *env = cur_cpu = next_cpu; |
| 4187 | |
| 4188 | if (!vm_running) |
| 4189 | break; |
| 4190 | if (timer_alarm_pending) { |
| 4191 | timer_alarm_pending = 0; |
| 4192 | break; |
| 4193 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4194 | if (cpu_can_run(env)) |
| 4195 | ret = qemu_cpu_exec(env); |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4196 | if (ret == EXCP_DEBUG) { |
| 4197 | gdb_set_stop_cpu(env); |
| 4198 | debug_requested = 1; |
| 4199 | break; |
| 4200 | } |
| 4201 | } |
| 4202 | } |
| 4203 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4204 | static int cpu_has_work(CPUState *env) |
| 4205 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4206 | if (env->stop) |
| 4207 | return 1; |
| 4208 | if (env->stopped) |
| 4209 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4210 | if (!env->halted) |
| 4211 | return 1; |
| 4212 | if (qemu_cpu_has_work(env)) |
| 4213 | return 1; |
| 4214 | return 0; |
| 4215 | } |
| 4216 | |
| 4217 | static int tcg_has_work(void) |
| 4218 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 4219 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4220 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4221 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 4222 | if (cpu_has_work(env)) |
| 4223 | return 1; |
| 4224 | return 0; |
| 4225 | } |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4226 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4227 | static int qemu_calculate_timeout(void) |
| 4228 | { |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4229 | #ifndef CONFIG_IOTHREAD |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4230 | int timeout; |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4231 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4232 | if (!vm_running) |
| 4233 | timeout = 5000; |
| 4234 | else if (tcg_has_work()) |
| 4235 | timeout = 0; |
| 4236 | else if (!use_icount) |
| 4237 | timeout = 5000; |
| 4238 | else { |
| 4239 | /* XXX: use timeout computed from timers */ |
| 4240 | int64_t add; |
| 4241 | int64_t delta; |
| 4242 | /* Advance virtual time to the next event. */ |
| 4243 | if (use_icount == 1) { |
| 4244 | /* When not using an adaptive execution frequency |
| 4245 | we tend to get badly out of sync with real time, |
| 4246 | so just delay for a reasonable amount of time. */ |
| 4247 | delta = 0; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4248 | } else { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4249 | delta = cpu_get_icount() - cpu_get_clock(); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4250 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4251 | if (delta > 0) { |
| 4252 | /* If virtual time is ahead of real time then just |
| 4253 | wait for IO. */ |
| 4254 | timeout = (delta / 1000000) + 1; |
| 4255 | } else { |
| 4256 | /* Wait for either IO to occur or the next |
| 4257 | timer event. */ |
| 4258 | add = qemu_next_deadline(); |
| 4259 | /* We advance the timer before checking for IO. |
| 4260 | Limit the amount we advance so that early IO |
| 4261 | activity won't get the guest too far ahead. */ |
| 4262 | if (add > 10000000) |
| 4263 | add = 10000000; |
| 4264 | delta += add; |
| 4265 | add = (add + (1 << icount_time_shift) - 1) |
| 4266 | >> icount_time_shift; |
| 4267 | qemu_icount += add; |
| 4268 | timeout = delta / 1000000; |
| 4269 | if (timeout < 0) |
| 4270 | timeout = 0; |
| 4271 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4272 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4273 | |
| 4274 | return timeout; |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4275 | #else /* CONFIG_IOTHREAD */ |
| 4276 | return 1000; |
| 4277 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | static int vm_can_run(void) |
| 4281 | { |
| 4282 | if (powerdown_requested) |
| 4283 | return 0; |
| 4284 | if (reset_requested) |
| 4285 | return 0; |
| 4286 | if (shutdown_requested) |
| 4287 | return 0; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4288 | if (debug_requested) |
| 4289 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4290 | return 1; |
| 4291 | } |
| 4292 | |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4293 | qemu_irq qemu_system_powerdown; |
| 4294 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4295 | static void main_loop(void) |
| 4296 | { |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4297 | int r; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4298 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4299 | #ifdef CONFIG_IOTHREAD |
| 4300 | qemu_system_ready = 1; |
| 4301 | qemu_cond_broadcast(&qemu_system_cond); |
| 4302 | #endif |
| 4303 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4304 | for (;;) { |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4305 | do { |
| 4306 | #ifdef CONFIG_PROFILER |
| 4307 | int64_t ti; |
| 4308 | #endif |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4309 | #ifndef CONFIG_IOTHREAD |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4310 | tcg_cpu_exec(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4311 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4312 | #ifdef CONFIG_PROFILER |
| 4313 | ti = profile_getclock(); |
| 4314 | #endif |
| 4315 | main_loop_wait(qemu_calculate_timeout()); |
| 4316 | #ifdef CONFIG_PROFILER |
| 4317 | dev_time += profile_getclock() - ti; |
| 4318 | #endif |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4319 | } while (vm_can_run()); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4320 | |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4321 | if (qemu_debug_requested()) |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4322 | vm_stop(EXCP_DEBUG); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4323 | if (qemu_shutdown_requested()) { |
| 4324 | if (no_shutdown) { |
| 4325 | vm_stop(0); |
| 4326 | no_shutdown = 0; |
| 4327 | } else |
| 4328 | break; |
| 4329 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4330 | if (qemu_reset_requested()) { |
| 4331 | pause_all_vcpus(); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4332 | qemu_system_reset(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4333 | resume_all_vcpus(); |
| 4334 | } |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4335 | if (qemu_powerdown_requested()) { |
| 4336 | qemu_irq_raise(qemu_system_powerdown); |
| 4337 | } |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4338 | if ((r = qemu_vmstop_requested())) |
| 4339 | vm_stop(r); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4340 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4341 | pause_all_vcpus(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4344 | static void version(void) |
| 4345 | { |
pbrook | 4a19f1e | 2009-04-07 23:17:49 +0000 | [diff] [blame] | 4346 | printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"); |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4349 | static void help(int exitcode) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4350 | { |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4351 | version(); |
| 4352 | printf("usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4353 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4354 | "'disk_image' is a raw hard image image for IDE hard disk 0\n" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4355 | "\n" |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4356 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4357 | opt_help |
| 4358 | #define DEFHEADING(text) stringify(text) "\n" |
| 4359 | #include "qemu-options.h" |
| 4360 | #undef DEF |
| 4361 | #undef DEFHEADING |
| 4362 | #undef GEN_DOCS |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4363 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4364 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 4365 | "ctrl-alt-f toggle full screen\n" |
| 4366 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 4367 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4368 | "\n" |
| 4369 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 4370 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 4371 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4372 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4373 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4374 | DEFAULT_NETWORK_SCRIPT, |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4375 | DEFAULT_NETWORK_DOWN_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4376 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 4377 | DEFAULT_GDBSTUB_PORT, |
bellard | bce6184 | 2008-02-01 22:18:51 +0000 | [diff] [blame] | 4378 | "/tmp/qemu.log"); |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4379 | exit(exitcode); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4382 | #define HAS_ARG 0x0001 |
| 4383 | |
| 4384 | enum { |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4385 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4386 | opt_enum, |
| 4387 | #define DEFHEADING(text) |
| 4388 | #include "qemu-options.h" |
| 4389 | #undef DEF |
| 4390 | #undef DEFHEADING |
| 4391 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4392 | }; |
| 4393 | |
| 4394 | typedef struct QEMUOption { |
| 4395 | const char *name; |
| 4396 | int flags; |
| 4397 | int index; |
| 4398 | } QEMUOption; |
| 4399 | |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 4400 | static const QEMUOption qemu_options[] = { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4401 | { "h", 0, QEMU_OPTION_h }, |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4402 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4403 | { option, opt_arg, opt_enum }, |
| 4404 | #define DEFHEADING(text) |
| 4405 | #include "qemu-options.h" |
| 4406 | #undef DEF |
| 4407 | #undef DEFHEADING |
| 4408 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4409 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4410 | }; |
| 4411 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4412 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4413 | struct soundhw soundhw[] = { |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4414 | #ifdef HAS_AUDIO_CHOICE |
aurel32 | 4ce7ff6 | 2008-04-07 19:47:14 +0000 | [diff] [blame] | 4415 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 4416 | { |
| 4417 | "pcspk", |
| 4418 | "PC speaker", |
| 4419 | 0, |
| 4420 | 1, |
| 4421 | { .init_isa = pcspk_audio_init } |
| 4422 | }, |
| 4423 | #endif |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4424 | |
| 4425 | #ifdef CONFIG_SB16 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4426 | { |
| 4427 | "sb16", |
| 4428 | "Creative Sound Blaster 16", |
| 4429 | 0, |
| 4430 | 1, |
| 4431 | { .init_isa = SB16_init } |
| 4432 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4433 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4434 | |
malc | cc53d26 | 2008-06-13 10:48:22 +0000 | [diff] [blame] | 4435 | #ifdef CONFIG_CS4231A |
| 4436 | { |
| 4437 | "cs4231a", |
| 4438 | "CS4231A", |
| 4439 | 0, |
| 4440 | 1, |
| 4441 | { .init_isa = cs4231a_init } |
| 4442 | }, |
| 4443 | #endif |
| 4444 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4445 | #ifdef CONFIG_ADLIB |
| 4446 | { |
| 4447 | "adlib", |
| 4448 | #ifdef HAS_YMF262 |
| 4449 | "Yamaha YMF262 (OPL3)", |
| 4450 | #else |
| 4451 | "Yamaha YM3812 (OPL2)", |
| 4452 | #endif |
| 4453 | 0, |
| 4454 | 1, |
| 4455 | { .init_isa = Adlib_init } |
| 4456 | }, |
| 4457 | #endif |
| 4458 | |
| 4459 | #ifdef CONFIG_GUS |
| 4460 | { |
| 4461 | "gus", |
| 4462 | "Gravis Ultrasound GF1", |
| 4463 | 0, |
| 4464 | 1, |
| 4465 | { .init_isa = GUS_init } |
| 4466 | }, |
| 4467 | #endif |
| 4468 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4469 | #ifdef CONFIG_AC97 |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4470 | { |
| 4471 | "ac97", |
| 4472 | "Intel 82801AA AC97 Audio", |
| 4473 | 0, |
| 4474 | 0, |
| 4475 | { .init_pci = ac97_init } |
| 4476 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4477 | #endif |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4478 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4479 | #ifdef CONFIG_ES1370 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4480 | { |
| 4481 | "es1370", |
| 4482 | "ENSONIQ AudioPCI ES1370", |
| 4483 | 0, |
| 4484 | 0, |
| 4485 | { .init_pci = es1370_init } |
| 4486 | }, |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4487 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4488 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4489 | #endif /* HAS_AUDIO_CHOICE */ |
| 4490 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4491 | { NULL, NULL, 0, 0, { NULL } } |
| 4492 | }; |
| 4493 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4494 | static void select_soundhw (const char *optarg) |
| 4495 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4496 | struct soundhw *c; |
| 4497 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4498 | if (*optarg == '?') { |
| 4499 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4500 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4501 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4502 | for (c = soundhw; c->name; ++c) { |
| 4503 | printf ("%-11s %s\n", c->name, c->descr); |
| 4504 | } |
| 4505 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4506 | exit (*optarg != '?'); |
| 4507 | } |
| 4508 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4509 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4510 | const char *p; |
| 4511 | char *e; |
| 4512 | int bad_card = 0; |
| 4513 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4514 | if (!strcmp (optarg, "all")) { |
| 4515 | for (c = soundhw; c->name; ++c) { |
| 4516 | c->enabled = 1; |
| 4517 | } |
| 4518 | return; |
| 4519 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4520 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4521 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4522 | while (*p) { |
| 4523 | e = strchr (p, ','); |
| 4524 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4525 | |
| 4526 | for (c = soundhw; c->name; ++c) { |
| 4527 | if (!strncmp (c->name, p, l)) { |
| 4528 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4529 | break; |
| 4530 | } |
| 4531 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4532 | |
| 4533 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4534 | if (l > 80) { |
| 4535 | fprintf (stderr, |
| 4536 | "Unknown sound card name (too big to show)\n"); |
| 4537 | } |
| 4538 | else { |
| 4539 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 4540 | (int) l, p); |
| 4541 | } |
| 4542 | bad_card = 1; |
| 4543 | } |
| 4544 | p += l + (e != NULL); |
| 4545 | } |
| 4546 | |
| 4547 | if (bad_card) |
| 4548 | goto show_valid_cards; |
| 4549 | } |
| 4550 | } |
| 4551 | #endif |
| 4552 | |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4553 | static void select_vgahw (const char *p) |
| 4554 | { |
| 4555 | const char *opts; |
| 4556 | |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4557 | vga_interface_type = VGA_NONE; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4558 | if (strstart(p, "std", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4559 | vga_interface_type = VGA_STD; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4560 | } else if (strstart(p, "cirrus", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4561 | vga_interface_type = VGA_CIRRUS; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4562 | } else if (strstart(p, "vmware", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4563 | vga_interface_type = VGA_VMWARE; |
aliguori | 94909d9 | 2009-04-22 15:19:53 +0000 | [diff] [blame] | 4564 | } else if (strstart(p, "xenfb", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4565 | vga_interface_type = VGA_XENFB; |
aliguori | 28b85ed | 2009-04-22 15:19:48 +0000 | [diff] [blame] | 4566 | } else if (!strstart(p, "none", &opts)) { |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4567 | invalid_vga: |
| 4568 | fprintf(stderr, "Unknown vga type: %s\n", p); |
| 4569 | exit(1); |
| 4570 | } |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 4571 | while (*opts) { |
| 4572 | const char *nextopt; |
| 4573 | |
| 4574 | if (strstart(opts, ",retrace=", &nextopt)) { |
| 4575 | opts = nextopt; |
| 4576 | if (strstart(opts, "dumb", &nextopt)) |
| 4577 | vga_retrace_method = VGA_RETRACE_DUMB; |
| 4578 | else if (strstart(opts, "precise", &nextopt)) |
| 4579 | vga_retrace_method = VGA_RETRACE_PRECISE; |
| 4580 | else goto invalid_vga; |
| 4581 | } else goto invalid_vga; |
| 4582 | opts = nextopt; |
| 4583 | } |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4586 | #ifdef TARGET_I386 |
| 4587 | static int balloon_parse(const char *arg) |
| 4588 | { |
| 4589 | char buf[128]; |
| 4590 | const char *p; |
| 4591 | |
| 4592 | if (!strcmp(arg, "none")) { |
| 4593 | virtio_balloon = 0; |
| 4594 | } else if (!strncmp(arg, "virtio", 6)) { |
| 4595 | virtio_balloon = 1; |
| 4596 | if (arg[6] == ',') { |
| 4597 | p = arg + 7; |
| 4598 | if (get_param_value(buf, sizeof(buf), "addr", p)) { |
| 4599 | virtio_balloon_devaddr = strdup(buf); |
| 4600 | } |
| 4601 | } |
| 4602 | } else { |
| 4603 | return -1; |
| 4604 | } |
| 4605 | return 0; |
| 4606 | } |
| 4607 | #endif |
| 4608 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4609 | #ifdef _WIN32 |
| 4610 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 4611 | { |
| 4612 | exit(STATUS_CONTROL_C_EXIT); |
| 4613 | return TRUE; |
| 4614 | } |
| 4615 | #endif |
| 4616 | |
aliguori | c4be29f | 2009-04-17 18:58:14 +0000 | [diff] [blame] | 4617 | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4618 | { |
| 4619 | int ret; |
| 4620 | |
| 4621 | if(strlen(str) != 36) |
| 4622 | return -1; |
| 4623 | |
| 4624 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], |
| 4625 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], |
| 4626 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); |
| 4627 | |
| 4628 | if(ret != 16) |
| 4629 | return -1; |
| 4630 | |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 4631 | #ifdef TARGET_I386 |
| 4632 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); |
| 4633 | #endif |
| 4634 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4635 | return 0; |
| 4636 | } |
| 4637 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4638 | #define MAX_NET_CLIENTS 32 |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4639 | |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4640 | #ifndef _WIN32 |
| 4641 | |
| 4642 | static void termsig_handler(int signal) |
| 4643 | { |
| 4644 | qemu_system_shutdown_request(); |
| 4645 | } |
| 4646 | |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4647 | static void sigchld_handler(int signal) |
| 4648 | { |
| 4649 | waitpid(-1, NULL, WNOHANG); |
| 4650 | } |
| 4651 | |
| 4652 | static void sighandler_setup(void) |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4653 | { |
| 4654 | struct sigaction act; |
| 4655 | |
| 4656 | memset(&act, 0, sizeof(act)); |
| 4657 | act.sa_handler = termsig_handler; |
| 4658 | sigaction(SIGINT, &act, NULL); |
| 4659 | sigaction(SIGHUP, &act, NULL); |
| 4660 | sigaction(SIGTERM, &act, NULL); |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4661 | |
| 4662 | act.sa_handler = sigchld_handler; |
| 4663 | act.sa_flags = SA_NOCLDSTOP; |
| 4664 | sigaction(SIGCHLD, &act, NULL); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | #endif |
| 4668 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4669 | #ifdef _WIN32 |
| 4670 | /* Look for support files in the same directory as the executable. */ |
| 4671 | static char *find_datadir(const char *argv0) |
| 4672 | { |
| 4673 | char *p; |
| 4674 | char buf[MAX_PATH]; |
| 4675 | DWORD len; |
| 4676 | |
| 4677 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
| 4678 | if (len == 0) { |
Blue Swirl | c594780 | 2009-06-09 20:51:21 +0300 | [diff] [blame] | 4679 | return NULL; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | buf[len] = 0; |
| 4683 | p = buf + len - 1; |
| 4684 | while (p != buf && *p != '\\') |
| 4685 | p--; |
| 4686 | *p = 0; |
| 4687 | if (access(buf, R_OK) == 0) { |
| 4688 | return qemu_strdup(buf); |
| 4689 | } |
| 4690 | return NULL; |
| 4691 | } |
| 4692 | #else /* !_WIN32 */ |
| 4693 | |
| 4694 | /* Find a likely location for support files using the location of the binary. |
| 4695 | For installed binaries this will be "$bindir/../share/qemu". When |
| 4696 | running from the build tree this will be "$bindir/../pc-bios". */ |
| 4697 | #define SHARE_SUFFIX "/share/qemu" |
| 4698 | #define BUILD_SUFFIX "/pc-bios" |
| 4699 | static char *find_datadir(const char *argv0) |
| 4700 | { |
| 4701 | char *dir; |
| 4702 | char *p = NULL; |
| 4703 | char *res; |
| 4704 | #ifdef PATH_MAX |
| 4705 | char buf[PATH_MAX]; |
| 4706 | #endif |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4707 | size_t max_len; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4708 | |
| 4709 | #if defined(__linux__) |
| 4710 | { |
| 4711 | int len; |
| 4712 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| 4713 | if (len > 0) { |
| 4714 | buf[len] = 0; |
| 4715 | p = buf; |
| 4716 | } |
| 4717 | } |
| 4718 | #elif defined(__FreeBSD__) |
| 4719 | { |
| 4720 | int len; |
| 4721 | len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); |
| 4722 | if (len > 0) { |
| 4723 | buf[len] = 0; |
| 4724 | p = buf; |
| 4725 | } |
| 4726 | } |
| 4727 | #endif |
| 4728 | /* If we don't have any way of figuring out the actual executable |
| 4729 | location then try argv[0]. */ |
| 4730 | if (!p) { |
| 4731 | #ifdef PATH_MAX |
| 4732 | p = buf; |
| 4733 | #endif |
| 4734 | p = realpath(argv0, p); |
| 4735 | if (!p) { |
| 4736 | return NULL; |
| 4737 | } |
| 4738 | } |
| 4739 | dir = dirname(p); |
| 4740 | dir = dirname(dir); |
| 4741 | |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4742 | max_len = strlen(dir) + |
| 4743 | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; |
| 4744 | res = qemu_mallocz(max_len); |
| 4745 | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4746 | if (access(res, R_OK)) { |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4747 | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4748 | if (access(res, R_OK)) { |
| 4749 | qemu_free(res); |
| 4750 | res = NULL; |
| 4751 | } |
| 4752 | } |
| 4753 | #ifndef PATH_MAX |
| 4754 | free(p); |
| 4755 | #endif |
| 4756 | return res; |
| 4757 | } |
| 4758 | #undef SHARE_SUFFIX |
| 4759 | #undef BUILD_SUFFIX |
| 4760 | #endif |
| 4761 | |
| 4762 | char *qemu_find_file(int type, const char *name) |
| 4763 | { |
| 4764 | int len; |
| 4765 | const char *subdir; |
| 4766 | char *buf; |
| 4767 | |
| 4768 | /* If name contains path separators then try it as a straight path. */ |
| 4769 | if ((strchr(name, '/') || strchr(name, '\\')) |
| 4770 | && access(name, R_OK) == 0) { |
| 4771 | return strdup(name); |
| 4772 | } |
| 4773 | switch (type) { |
| 4774 | case QEMU_FILE_TYPE_BIOS: |
| 4775 | subdir = ""; |
| 4776 | break; |
| 4777 | case QEMU_FILE_TYPE_KEYMAP: |
| 4778 | subdir = "keymaps/"; |
| 4779 | break; |
| 4780 | default: |
| 4781 | abort(); |
| 4782 | } |
| 4783 | len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2; |
| 4784 | buf = qemu_mallocz(len); |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4785 | snprintf(buf, len, "%s/%s%s", data_dir, subdir, name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4786 | if (access(buf, R_OK)) { |
| 4787 | qemu_free(buf); |
| 4788 | return NULL; |
| 4789 | } |
| 4790 | return buf; |
| 4791 | } |
| 4792 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4793 | static int device_init_func(QemuOpts *opts, void *opaque) |
| 4794 | { |
| 4795 | DeviceState *dev; |
| 4796 | |
| 4797 | dev = qdev_device_add(opts); |
| 4798 | if (!dev) |
| 4799 | return -1; |
| 4800 | return 0; |
| 4801 | } |
| 4802 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4803 | struct device_config { |
| 4804 | enum { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4805 | DEV_USB, /* -usbdevice */ |
| 4806 | DEV_BT, /* -bt */ |
| 4807 | } type; |
| 4808 | const char *cmdline; |
| 4809 | TAILQ_ENTRY(device_config) next; |
| 4810 | }; |
| 4811 | TAILQ_HEAD(, device_config) device_configs = TAILQ_HEAD_INITIALIZER(device_configs); |
| 4812 | |
| 4813 | static void add_device_config(int type, const char *cmdline) |
| 4814 | { |
| 4815 | struct device_config *conf; |
| 4816 | |
| 4817 | conf = qemu_mallocz(sizeof(*conf)); |
| 4818 | conf->type = type; |
| 4819 | conf->cmdline = cmdline; |
| 4820 | TAILQ_INSERT_TAIL(&device_configs, conf, next); |
| 4821 | } |
| 4822 | |
| 4823 | static int foreach_device_config(int type, int (*func)(const char *cmdline)) |
| 4824 | { |
| 4825 | struct device_config *conf; |
| 4826 | int rc; |
| 4827 | |
| 4828 | TAILQ_FOREACH(conf, &device_configs, next) { |
| 4829 | if (conf->type != type) |
| 4830 | continue; |
| 4831 | rc = func(conf->cmdline); |
| 4832 | if (0 != rc) |
| 4833 | return rc; |
| 4834 | } |
| 4835 | return 0; |
| 4836 | } |
| 4837 | |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4838 | int main(int argc, char **argv, char **envp) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4839 | { |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 4840 | const char *gdbstub_dev = NULL; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4841 | uint32_t boot_devices_bitmap = 0; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4842 | int i; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4843 | int snapshot, linux_boot, net_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 4844 | const char *initrd_filename; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4845 | const char *kernel_filename, *kernel_cmdline; |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 4846 | char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 4847 | DisplayState *ds; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 4848 | DisplayChangeListener *dcl; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4849 | int cyls, heads, secs, translation; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4850 | const char *net_clients[MAX_NET_CLIENTS]; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4851 | int nb_net_clients; |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4852 | QemuOpts *hda_opts = NULL, *opts; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4853 | int optind; |
| 4854 | const char *r, *optarg; |
aliguori | 4c62180 | 2009-01-16 21:48:20 +0000 | [diff] [blame] | 4855 | CharDriverState *monitor_hd = NULL; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4856 | const char *monitor_device; |
| 4857 | const char *serial_devices[MAX_SERIAL_PORTS]; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 4858 | int serial_device_index; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4859 | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 4860 | int parallel_device_index; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 4861 | const char *virtio_consoles[MAX_VIRTIO_CONSOLES]; |
| 4862 | int virtio_console_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 4863 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4864 | QEMUMachine *machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4865 | const char *cpu_model; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4866 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 4867 | int fds[2]; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4868 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4869 | int tb_size; |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 4870 | const char *pid_file = NULL; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 4871 | const char *incoming = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4872 | #ifndef _WIN32 |
aliguori | 54042bc | 2009-02-27 22:16:47 +0000 | [diff] [blame] | 4873 | int fd = 0; |
| 4874 | struct passwd *pwd = NULL; |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 4875 | const char *chroot_dir = NULL; |
| 4876 | const char *run_as = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4877 | #endif |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4878 | CPUState *env; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 4879 | int show_vnc_port = 0; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 4880 | |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4881 | qemu_cache_utils_init(envp); |
| 4882 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 4883 | LIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4884 | #ifndef _WIN32 |
| 4885 | { |
| 4886 | struct sigaction act; |
| 4887 | sigfillset(&act.sa_mask); |
| 4888 | act.sa_flags = 0; |
| 4889 | act.sa_handler = SIG_IGN; |
| 4890 | sigaction(SIGPIPE, &act, NULL); |
| 4891 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4892 | #else |
| 4893 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 4894 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 4895 | QEMU to run on a single CPU */ |
| 4896 | { |
| 4897 | HANDLE h; |
| 4898 | DWORD mask, smask; |
| 4899 | int i; |
| 4900 | h = GetCurrentProcess(); |
| 4901 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 4902 | for(i = 0; i < 32; i++) { |
| 4903 | if (mask & (1 << i)) |
| 4904 | break; |
| 4905 | } |
| 4906 | if (i != 32) { |
| 4907 | mask = 1 << i; |
| 4908 | SetProcessAffinityMask(h, mask); |
| 4909 | } |
| 4910 | } |
| 4911 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4912 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4913 | |
Anthony Liguori | f80f9ec | 2009-05-20 18:38:09 -0500 | [diff] [blame] | 4914 | module_call_init(MODULE_INIT_MACHINE); |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4915 | machine = find_default_machine(); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4916 | cpu_model = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4917 | initrd_filename = NULL; |
aurel32 | 4fc5d07 | 2008-04-27 21:39:40 +0000 | [diff] [blame] | 4918 | ram_size = 0; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 4919 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4920 | kernel_filename = NULL; |
| 4921 | kernel_cmdline = ""; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4922 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4923 | translation = BIOS_ATA_TRANSLATION_AUTO; |
aliguori | d47d13b | 2009-03-05 23:00:53 +0000 | [diff] [blame] | 4924 | monitor_device = "vc:80Cx24C"; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4925 | |
aurel32 | c75a823 | 2008-05-04 00:50:34 +0000 | [diff] [blame] | 4926 | serial_devices[0] = "vc:80Cx24C"; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 4927 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4928 | serial_devices[i] = NULL; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 4929 | serial_device_index = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4930 | |
aliguori | 8290edd | 2009-02-27 20:14:29 +0000 | [diff] [blame] | 4931 | parallel_devices[0] = "vc:80Cx24C"; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 4932 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 4933 | parallel_devices[i] = NULL; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 4934 | parallel_device_index = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4935 | |
aliguori | 1b8fc81 | 2009-02-27 20:01:39 +0000 | [diff] [blame] | 4936 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 4937 | virtio_consoles[i] = NULL; |
| 4938 | virtio_console_index = 0; |
| 4939 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4940 | for (i = 0; i < MAX_NODES; i++) { |
| 4941 | node_mem[i] = 0; |
| 4942 | node_cpumask[i] = 0; |
| 4943 | } |
| 4944 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4945 | nb_net_clients = 0; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4946 | nb_numa_nodes = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4947 | nb_nics = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4948 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4949 | tb_size = 0; |
blueswir1 | 41bd639 | 2008-10-05 09:56:21 +0000 | [diff] [blame] | 4950 | autostart= 1; |
| 4951 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4952 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4953 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4954 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4955 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4956 | r = argv[optind]; |
| 4957 | if (r[0] != '-') { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4958 | hda_opts = drive_add(argv[optind++], HD_ALIAS, 0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4959 | } else { |
| 4960 | const QEMUOption *popt; |
| 4961 | |
| 4962 | optind++; |
pbrook | dff5efc | 2007-01-27 17:19:39 +0000 | [diff] [blame] | 4963 | /* Treat --foo the same as -foo. */ |
| 4964 | if (r[1] == '-') |
| 4965 | r++; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4966 | popt = qemu_options; |
| 4967 | for(;;) { |
| 4968 | if (!popt->name) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4969 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4970 | argv[0], r); |
| 4971 | exit(1); |
| 4972 | } |
| 4973 | if (!strcmp(popt->name, r + 1)) |
| 4974 | break; |
| 4975 | popt++; |
| 4976 | } |
| 4977 | if (popt->flags & HAS_ARG) { |
| 4978 | if (optind >= argc) { |
| 4979 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 4980 | argv[0], r); |
| 4981 | exit(1); |
| 4982 | } |
| 4983 | optarg = argv[optind++]; |
| 4984 | } else { |
| 4985 | optarg = NULL; |
| 4986 | } |
| 4987 | |
| 4988 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4989 | case QEMU_OPTION_M: |
| 4990 | machine = find_machine(optarg); |
| 4991 | if (!machine) { |
| 4992 | QEMUMachine *m; |
| 4993 | printf("Supported machines are:\n"); |
| 4994 | for(m = first_machine; m != NULL; m = m->next) { |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 4995 | if (m->alias) |
| 4996 | printf("%-10s %s (alias of %s)\n", |
| 4997 | m->alias, m->desc, m->name); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4998 | printf("%-10s %s%s\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4999 | m->name, m->desc, |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 5000 | m->is_default ? " (default)" : ""); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5001 | } |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5002 | exit(*optarg != '?'); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5003 | } |
| 5004 | break; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 5005 | case QEMU_OPTION_cpu: |
| 5006 | /* hw initialization will check this */ |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5007 | if (*optarg == '?') { |
j_mayer | c732abe | 2007-10-12 06:47:46 +0000 | [diff] [blame] | 5008 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 5009 | #if defined(cpu_list) |
| 5010 | cpu_list(stdout, &fprintf); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 5011 | #endif |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5012 | exit(0); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 5013 | } else { |
| 5014 | cpu_model = optarg; |
| 5015 | } |
| 5016 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5017 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 5018 | initrd_filename = optarg; |
| 5019 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5020 | case QEMU_OPTION_hda: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5021 | if (cyls == 0) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5022 | hda_opts = drive_add(optarg, HD_ALIAS, 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5023 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5024 | hda_opts = drive_add(optarg, HD_ALIAS |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5025 | ",cyls=%d,heads=%d,secs=%d%s", |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5026 | 0, cyls, heads, secs, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5027 | translation == BIOS_ATA_TRANSLATION_LBA ? |
| 5028 | ",trans=lba" : |
| 5029 | translation == BIOS_ATA_TRANSLATION_NONE ? |
| 5030 | ",trans=none" : ""); |
| 5031 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5032 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5033 | case QEMU_OPTION_hdc: |
| 5034 | case QEMU_OPTION_hdd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5035 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 5036 | break; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5037 | case QEMU_OPTION_drive: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5038 | drive_add(NULL, "%s", optarg); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5039 | break; |
Gerd Hoffmann | d058fe0 | 2009-07-31 12:25:36 +0200 | [diff] [blame] | 5040 | case QEMU_OPTION_set: |
| 5041 | if (qemu_set_option(optarg) != 0) |
| 5042 | exit(1); |
| 5043 | break; |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5044 | case QEMU_OPTION_mtdblock: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5045 | drive_add(optarg, MTD_ALIAS); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5046 | break; |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 5047 | case QEMU_OPTION_sd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5048 | drive_add(optarg, SD_ALIAS); |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 5049 | break; |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 5050 | case QEMU_OPTION_pflash: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5051 | drive_add(optarg, PFLASH_ALIAS); |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 5052 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5053 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 5054 | snapshot = 1; |
| 5055 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5056 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5057 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5058 | const char *p; |
| 5059 | p = optarg; |
| 5060 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5061 | if (cyls < 1 || cyls > 16383) |
| 5062 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5063 | if (*p != ',') |
| 5064 | goto chs_fail; |
| 5065 | p++; |
| 5066 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5067 | if (heads < 1 || heads > 16) |
| 5068 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5069 | if (*p != ',') |
| 5070 | goto chs_fail; |
| 5071 | p++; |
| 5072 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5073 | if (secs < 1 || secs > 63) |
| 5074 | goto chs_fail; |
| 5075 | if (*p == ',') { |
| 5076 | p++; |
| 5077 | if (!strcmp(p, "none")) |
| 5078 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 5079 | else if (!strcmp(p, "lba")) |
| 5080 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 5081 | else if (!strcmp(p, "auto")) |
| 5082 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 5083 | else |
| 5084 | goto chs_fail; |
| 5085 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5086 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5087 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 5088 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5089 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5090 | if (hda_opts != NULL) { |
| 5091 | char num[16]; |
| 5092 | snprintf(num, sizeof(num), "%d", cyls); |
| 5093 | qemu_opt_set(hda_opts, "cyls", num); |
| 5094 | snprintf(num, sizeof(num), "%d", heads); |
| 5095 | qemu_opt_set(hda_opts, "heads", num); |
| 5096 | snprintf(num, sizeof(num), "%d", secs); |
| 5097 | qemu_opt_set(hda_opts, "secs", num); |
| 5098 | if (translation == BIOS_ATA_TRANSLATION_LBA) |
| 5099 | qemu_opt_set(hda_opts, "trans", "lba"); |
| 5100 | if (translation == BIOS_ATA_TRANSLATION_NONE) |
| 5101 | qemu_opt_set(hda_opts, "trans", "none"); |
| 5102 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5103 | } |
| 5104 | break; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5105 | case QEMU_OPTION_numa: |
| 5106 | if (nb_numa_nodes >= MAX_NODES) { |
| 5107 | fprintf(stderr, "qemu: too many NUMA nodes\n"); |
| 5108 | exit(1); |
| 5109 | } |
| 5110 | numa_add(optarg); |
| 5111 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5112 | case QEMU_OPTION_nographic: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5113 | display_type = DT_NOGRAPHIC; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5114 | break; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5115 | #ifdef CONFIG_CURSES |
| 5116 | case QEMU_OPTION_curses: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5117 | display_type = DT_CURSES; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5118 | break; |
| 5119 | #endif |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 5120 | case QEMU_OPTION_portrait: |
| 5121 | graphic_rotate = 1; |
| 5122 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5123 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5124 | kernel_filename = optarg; |
| 5125 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5126 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5127 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5128 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5129 | case QEMU_OPTION_cdrom: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5130 | drive_add(optarg, CDROM_ALIAS); |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5131 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5132 | case QEMU_OPTION_boot: |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5133 | { |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5134 | static const char * const params[] = { |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5135 | "order", "once", "menu", NULL |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5136 | }; |
| 5137 | char buf[sizeof(boot_devices)]; |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5138 | char *standard_boot_devices; |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5139 | int legacy = 0; |
| 5140 | |
| 5141 | if (!strchr(optarg, '=')) { |
| 5142 | legacy = 1; |
| 5143 | pstrcpy(buf, sizeof(buf), optarg); |
| 5144 | } else if (check_params(buf, sizeof(buf), params, optarg) < 0) { |
| 5145 | fprintf(stderr, |
| 5146 | "qemu: unknown boot parameter '%s' in '%s'\n", |
| 5147 | buf, optarg); |
| 5148 | exit(1); |
| 5149 | } |
| 5150 | |
| 5151 | if (legacy || |
| 5152 | get_param_value(buf, sizeof(buf), "order", optarg)) { |
| 5153 | boot_devices_bitmap = parse_bootdevices(buf); |
| 5154 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5155 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5156 | if (!legacy) { |
| 5157 | if (get_param_value(buf, sizeof(buf), |
| 5158 | "once", optarg)) { |
| 5159 | boot_devices_bitmap |= parse_bootdevices(buf); |
| 5160 | standard_boot_devices = qemu_strdup(boot_devices); |
| 5161 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
| 5162 | qemu_register_reset(restore_boot_devices, |
| 5163 | standard_boot_devices); |
| 5164 | } |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5165 | if (get_param_value(buf, sizeof(buf), |
| 5166 | "menu", optarg)) { |
| 5167 | if (!strcmp(buf, "on")) { |
| 5168 | boot_menu = 1; |
| 5169 | } else if (!strcmp(buf, "off")) { |
| 5170 | boot_menu = 0; |
| 5171 | } else { |
| 5172 | fprintf(stderr, |
| 5173 | "qemu: invalid option value '%s'\n", |
| 5174 | buf); |
| 5175 | exit(1); |
| 5176 | } |
| 5177 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5178 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5179 | } |
| 5180 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5181 | case QEMU_OPTION_fda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5182 | case QEMU_OPTION_fdb: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5183 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 5184 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 5185 | #ifdef TARGET_I386 |
| 5186 | case QEMU_OPTION_no_fd_bootchk: |
| 5187 | fd_bootchk = 0; |
| 5188 | break; |
| 5189 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5190 | case QEMU_OPTION_net: |
| 5191 | if (nb_net_clients >= MAX_NET_CLIENTS) { |
| 5192 | fprintf(stderr, "qemu: too many network clients\n"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5193 | exit(1); |
| 5194 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5195 | net_clients[nb_net_clients] = optarg; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5196 | nb_net_clients++; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5197 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5198 | #ifdef CONFIG_SLIRP |
| 5199 | case QEMU_OPTION_tftp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5200 | legacy_tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5201 | break; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5202 | case QEMU_OPTION_bootp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5203 | legacy_bootp_filename = optarg; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5204 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5205 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5206 | case QEMU_OPTION_smb: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5207 | net_slirp_smb(optarg); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5208 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5209 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5210 | case QEMU_OPTION_redir: |
Jan Kiszka | f3546de | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5211 | net_slirp_redir(optarg); |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5212 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5213 | #endif |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5214 | case QEMU_OPTION_bt: |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5215 | add_device_config(DEV_BT, optarg); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5216 | break; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5217 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5218 | case QEMU_OPTION_audio_help: |
| 5219 | AUD_help (); |
| 5220 | exit (0); |
| 5221 | break; |
| 5222 | case QEMU_OPTION_soundhw: |
| 5223 | select_soundhw (optarg); |
| 5224 | break; |
| 5225 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5226 | case QEMU_OPTION_h: |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5227 | help(0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5228 | break; |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 5229 | case QEMU_OPTION_version: |
| 5230 | version(); |
| 5231 | exit(0); |
| 5232 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5233 | case QEMU_OPTION_m: { |
| 5234 | uint64_t value; |
| 5235 | char *ptr; |
| 5236 | |
| 5237 | value = strtoul(optarg, &ptr, 10); |
| 5238 | switch (*ptr) { |
| 5239 | case 0: case 'M': case 'm': |
| 5240 | value <<= 20; |
| 5241 | break; |
| 5242 | case 'G': case 'g': |
| 5243 | value <<= 30; |
| 5244 | break; |
| 5245 | default: |
| 5246 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5247 | exit(1); |
| 5248 | } |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5249 | |
| 5250 | /* On 32-bit hosts, QEMU is limited by virtual address space */ |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 5251 | if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5252 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); |
| 5253 | exit(1); |
| 5254 | } |
| 5255 | if (value != (uint64_t)(ram_addr_t)value) { |
| 5256 | fprintf(stderr, "qemu: ram size too large\n"); |
| 5257 | exit(1); |
| 5258 | } |
| 5259 | ram_size = value; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5260 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5261 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5262 | case QEMU_OPTION_d: |
| 5263 | { |
| 5264 | int mask; |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 5265 | const CPULogItem *item; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5266 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5267 | mask = cpu_str_to_log_mask(optarg); |
| 5268 | if (!mask) { |
| 5269 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5270 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 5271 | printf("%-10s %s\n", item->name, item->help); |
| 5272 | } |
| 5273 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5274 | } |
| 5275 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5276 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5277 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5278 | case QEMU_OPTION_s: |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5279 | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5280 | break; |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5281 | case QEMU_OPTION_gdb: |
| 5282 | gdbstub_dev = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5283 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5284 | case QEMU_OPTION_L: |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5285 | data_dir = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5286 | break; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 5287 | case QEMU_OPTION_bios: |
| 5288 | bios_name = optarg; |
| 5289 | break; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 5290 | case QEMU_OPTION_singlestep: |
| 5291 | singlestep = 1; |
| 5292 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5293 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 5294 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5295 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5296 | #ifndef _WIN32 |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 5297 | case QEMU_OPTION_k: |
| 5298 | keyboard_layout = optarg; |
| 5299 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5300 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 5301 | case QEMU_OPTION_localtime: |
| 5302 | rtc_utc = 0; |
| 5303 | break; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 5304 | case QEMU_OPTION_vga: |
| 5305 | select_vgahw (optarg); |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 5306 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5307 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5308 | case QEMU_OPTION_g: |
| 5309 | { |
| 5310 | const char *p; |
| 5311 | int w, h, depth; |
| 5312 | p = optarg; |
| 5313 | w = strtol(p, (char **)&p, 10); |
| 5314 | if (w <= 0) { |
| 5315 | graphic_error: |
| 5316 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 5317 | exit(1); |
| 5318 | } |
| 5319 | if (*p != 'x') |
| 5320 | goto graphic_error; |
| 5321 | p++; |
| 5322 | h = strtol(p, (char **)&p, 10); |
| 5323 | if (h <= 0) |
| 5324 | goto graphic_error; |
| 5325 | if (*p == 'x') { |
| 5326 | p++; |
| 5327 | depth = strtol(p, (char **)&p, 10); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5328 | if (depth != 8 && depth != 15 && depth != 16 && |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5329 | depth != 24 && depth != 32) |
| 5330 | goto graphic_error; |
| 5331 | } else if (*p == '\0') { |
| 5332 | depth = graphic_depth; |
| 5333 | } else { |
| 5334 | goto graphic_error; |
| 5335 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5336 | |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5337 | graphic_width = w; |
| 5338 | graphic_height = h; |
| 5339 | graphic_depth = depth; |
| 5340 | } |
| 5341 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5342 | #endif |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 5343 | case QEMU_OPTION_echr: |
| 5344 | { |
| 5345 | char *r; |
| 5346 | term_escape_char = strtol(optarg, &r, 0); |
| 5347 | if (r == optarg) |
| 5348 | printf("Bad argument to echr\n"); |
| 5349 | break; |
| 5350 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5351 | case QEMU_OPTION_monitor: |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5352 | monitor_device = optarg; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5353 | break; |
| 5354 | case QEMU_OPTION_serial: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 5355 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
| 5356 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 5357 | exit(1); |
| 5358 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5359 | serial_devices[serial_device_index] = optarg; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 5360 | serial_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5361 | break; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5362 | case QEMU_OPTION_watchdog: |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame^] | 5363 | if (watchdog) { |
| 5364 | fprintf(stderr, |
| 5365 | "qemu: only one watchdog option may be given\n"); |
| 5366 | return 1; |
| 5367 | } |
| 5368 | watchdog = optarg; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5369 | break; |
| 5370 | case QEMU_OPTION_watchdog_action: |
| 5371 | if (select_watchdog_action(optarg) == -1) { |
| 5372 | fprintf(stderr, "Unknown -watchdog-action parameter\n"); |
| 5373 | exit(1); |
| 5374 | } |
| 5375 | break; |
aliguori | 51ecf13 | 2009-01-15 20:06:40 +0000 | [diff] [blame] | 5376 | case QEMU_OPTION_virtiocon: |
| 5377 | if (virtio_console_index >= MAX_VIRTIO_CONSOLES) { |
| 5378 | fprintf(stderr, "qemu: too many virtio consoles\n"); |
| 5379 | exit(1); |
| 5380 | } |
| 5381 | virtio_consoles[virtio_console_index] = optarg; |
| 5382 | virtio_console_index++; |
| 5383 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5384 | case QEMU_OPTION_parallel: |
| 5385 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { |
| 5386 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 5387 | exit(1); |
| 5388 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 5389 | parallel_devices[parallel_device_index] = optarg; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5390 | parallel_device_index++; |
| 5391 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 5392 | case QEMU_OPTION_loadvm: |
| 5393 | loadvm = optarg; |
| 5394 | break; |
| 5395 | case QEMU_OPTION_full_screen: |
| 5396 | full_screen = 1; |
| 5397 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5398 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 5399 | case QEMU_OPTION_no_frame: |
| 5400 | no_frame = 1; |
| 5401 | break; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 5402 | case QEMU_OPTION_alt_grab: |
| 5403 | alt_grab = 1; |
| 5404 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5405 | case QEMU_OPTION_no_quit: |
| 5406 | no_quit = 1; |
| 5407 | break; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5408 | case QEMU_OPTION_sdl: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5409 | display_type = DT_SDL; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5410 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5411 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5412 | case QEMU_OPTION_pidfile: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5413 | pid_file = optarg; |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5414 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5415 | #ifdef TARGET_I386 |
| 5416 | case QEMU_OPTION_win2k_hack: |
| 5417 | win2k_install_hack = 1; |
| 5418 | break; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 5419 | case QEMU_OPTION_rtc_td_hack: |
| 5420 | rtc_td_hack = 1; |
| 5421 | break; |
aliguori | 8a92ea2 | 2009-02-27 20:12:36 +0000 | [diff] [blame] | 5422 | case QEMU_OPTION_acpitable: |
| 5423 | if(acpi_table_add(optarg) < 0) { |
| 5424 | fprintf(stderr, "Wrong acpi table provided\n"); |
| 5425 | exit(1); |
| 5426 | } |
| 5427 | break; |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 5428 | case QEMU_OPTION_smbios: |
| 5429 | if(smbios_entry_add(optarg) < 0) { |
| 5430 | fprintf(stderr, "Wrong smbios provided\n"); |
| 5431 | exit(1); |
| 5432 | } |
| 5433 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5434 | #endif |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5435 | #ifdef CONFIG_KVM |
| 5436 | case QEMU_OPTION_enable_kvm: |
| 5437 | kvm_allowed = 1; |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5438 | break; |
| 5439 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 5440 | case QEMU_OPTION_usb: |
| 5441 | usb_enabled = 1; |
| 5442 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5443 | case QEMU_OPTION_usbdevice: |
| 5444 | usb_enabled = 1; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5445 | add_device_config(DEV_USB, optarg); |
| 5446 | break; |
| 5447 | case QEMU_OPTION_device: |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5448 | opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver"); |
| 5449 | if (!opts) { |
| 5450 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5451 | exit(1); |
| 5452 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5453 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5454 | case QEMU_OPTION_smp: |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 5455 | smp_parse(optarg); |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5456 | if (smp_cpus < 1) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5457 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 5458 | exit(1); |
| 5459 | } |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5460 | if (max_cpus < smp_cpus) { |
| 5461 | fprintf(stderr, "maxcpus must be equal to or greater than " |
| 5462 | "smp\n"); |
| 5463 | exit(1); |
| 5464 | } |
| 5465 | if (max_cpus > 255) { |
| 5466 | fprintf(stderr, "Unsupported number of maxcpus\n"); |
| 5467 | exit(1); |
| 5468 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5469 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5470 | case QEMU_OPTION_vnc: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5471 | display_type = DT_VNC; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 5472 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5473 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5474 | #ifdef TARGET_I386 |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 5475 | case QEMU_OPTION_no_acpi: |
| 5476 | acpi_enabled = 0; |
| 5477 | break; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 5478 | case QEMU_OPTION_no_hpet: |
| 5479 | no_hpet = 1; |
| 5480 | break; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 5481 | case QEMU_OPTION_balloon: |
| 5482 | if (balloon_parse(optarg) < 0) { |
| 5483 | fprintf(stderr, "Unknown -balloon argument %s\n", optarg); |
| 5484 | exit(1); |
| 5485 | } |
Eduardo Habkost | df97b92 | 2009-06-10 16:34:08 -0300 | [diff] [blame] | 5486 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5487 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 5488 | case QEMU_OPTION_no_reboot: |
| 5489 | no_reboot = 1; |
| 5490 | break; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 5491 | case QEMU_OPTION_no_shutdown: |
| 5492 | no_shutdown = 1; |
| 5493 | break; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 5494 | case QEMU_OPTION_show_cursor: |
| 5495 | cursor_hide = 0; |
| 5496 | break; |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 5497 | case QEMU_OPTION_uuid: |
| 5498 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { |
| 5499 | fprintf(stderr, "Fail to parse UUID string." |
| 5500 | " Wrong format.\n"); |
| 5501 | exit(1); |
| 5502 | } |
| 5503 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5504 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5505 | case QEMU_OPTION_daemonize: |
| 5506 | daemonize = 1; |
| 5507 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5508 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 5509 | case QEMU_OPTION_option_rom: |
| 5510 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 5511 | fprintf(stderr, "Too many option ROMs\n"); |
| 5512 | exit(1); |
| 5513 | } |
| 5514 | option_rom[nb_option_roms] = optarg; |
| 5515 | nb_option_roms++; |
| 5516 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5517 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 5518 | case QEMU_OPTION_semihosting: |
| 5519 | semihosting_enabled = 1; |
| 5520 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5521 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5522 | case QEMU_OPTION_name: |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 5523 | qemu_name = qemu_strdup(optarg); |
| 5524 | { |
| 5525 | char *p = strchr(qemu_name, ','); |
| 5526 | if (p != NULL) { |
| 5527 | *p++ = 0; |
| 5528 | if (strncmp(p, "process=", 8)) { |
| 5529 | fprintf(stderr, "Unknown subargument %s to -name", p); |
| 5530 | exit(1); |
| 5531 | } |
| 5532 | p += 8; |
| 5533 | set_proc_name(p); |
| 5534 | } |
| 5535 | } |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5536 | break; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 5537 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 5538 | case QEMU_OPTION_prom_env: |
| 5539 | if (nb_prom_envs >= MAX_PROM_ENVS) { |
| 5540 | fprintf(stderr, "Too many prom variables\n"); |
| 5541 | exit(1); |
| 5542 | } |
| 5543 | prom_envs[nb_prom_envs] = optarg; |
| 5544 | nb_prom_envs++; |
| 5545 | break; |
| 5546 | #endif |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5547 | #ifdef TARGET_ARM |
| 5548 | case QEMU_OPTION_old_param: |
| 5549 | old_param = 1; |
ths | 05ebd53 | 2008-01-08 19:32:16 +0000 | [diff] [blame] | 5550 | break; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5551 | #endif |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 5552 | case QEMU_OPTION_clock: |
| 5553 | configure_alarms(optarg); |
| 5554 | break; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5555 | case QEMU_OPTION_startdate: |
| 5556 | { |
| 5557 | struct tm tm; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 5558 | time_t rtc_start_date; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5559 | if (!strcmp(optarg, "now")) { |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 5560 | rtc_date_offset = -1; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5561 | } else { |
| 5562 | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
| 5563 | &tm.tm_year, |
| 5564 | &tm.tm_mon, |
| 5565 | &tm.tm_mday, |
| 5566 | &tm.tm_hour, |
| 5567 | &tm.tm_min, |
| 5568 | &tm.tm_sec) == 6) { |
| 5569 | /* OK */ |
| 5570 | } else if (sscanf(optarg, "%d-%d-%d", |
| 5571 | &tm.tm_year, |
| 5572 | &tm.tm_mon, |
| 5573 | &tm.tm_mday) == 3) { |
| 5574 | tm.tm_hour = 0; |
| 5575 | tm.tm_min = 0; |
| 5576 | tm.tm_sec = 0; |
| 5577 | } else { |
| 5578 | goto date_fail; |
| 5579 | } |
| 5580 | tm.tm_year -= 1900; |
| 5581 | tm.tm_mon--; |
bellard | 3c6b208 | 2007-11-10 19:36:39 +0000 | [diff] [blame] | 5582 | rtc_start_date = mktimegm(&tm); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5583 | if (rtc_start_date == -1) { |
| 5584 | date_fail: |
| 5585 | fprintf(stderr, "Invalid date format. Valid format are:\n" |
| 5586 | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n"); |
| 5587 | exit(1); |
| 5588 | } |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 5589 | rtc_date_offset = time(NULL) - rtc_start_date; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5590 | } |
| 5591 | } |
| 5592 | break; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5593 | case QEMU_OPTION_tb_size: |
| 5594 | tb_size = strtol(optarg, NULL, 0); |
| 5595 | if (tb_size < 0) |
| 5596 | tb_size = 0; |
| 5597 | break; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5598 | case QEMU_OPTION_icount: |
| 5599 | use_icount = 1; |
| 5600 | if (strcmp(optarg, "auto") == 0) { |
| 5601 | icount_time_shift = -1; |
| 5602 | } else { |
| 5603 | icount_time_shift = strtol(optarg, NULL, 0); |
| 5604 | } |
| 5605 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 5606 | case QEMU_OPTION_incoming: |
| 5607 | incoming = optarg; |
| 5608 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5609 | #ifndef _WIN32 |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5610 | case QEMU_OPTION_chroot: |
| 5611 | chroot_dir = optarg; |
| 5612 | break; |
| 5613 | case QEMU_OPTION_runas: |
| 5614 | run_as = optarg; |
| 5615 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5616 | #endif |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 5617 | #ifdef CONFIG_XEN |
| 5618 | case QEMU_OPTION_xen_domid: |
| 5619 | xen_domid = atoi(optarg); |
| 5620 | break; |
| 5621 | case QEMU_OPTION_xen_create: |
| 5622 | xen_mode = XEN_CREATE; |
| 5623 | break; |
| 5624 | case QEMU_OPTION_xen_attach: |
| 5625 | xen_mode = XEN_ATTACH; |
| 5626 | break; |
| 5627 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5628 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5629 | } |
| 5630 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5631 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5632 | /* If no data_dir is specified then try to find it relative to the |
| 5633 | executable path. */ |
| 5634 | if (!data_dir) { |
| 5635 | data_dir = find_datadir(argv[0]); |
| 5636 | } |
| 5637 | /* If all else fails use the install patch specified when building. */ |
| 5638 | if (!data_dir) { |
| 5639 | data_dir = CONFIG_QEMU_SHAREDIR; |
| 5640 | } |
| 5641 | |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5642 | /* |
| 5643 | * Default to max_cpus = smp_cpus, in case the user doesn't |
| 5644 | * specify a max_cpus value. |
| 5645 | */ |
| 5646 | if (!max_cpus) |
| 5647 | max_cpus = smp_cpus; |
| 5648 | |
balrog | 3d878ca | 2008-10-28 10:59:59 +0000 | [diff] [blame] | 5649 | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5650 | if (smp_cpus > machine->max_cpus) { |
| 5651 | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus " |
| 5652 | "supported by machine `%s' (%d)\n", smp_cpus, machine->name, |
| 5653 | machine->max_cpus); |
| 5654 | exit(1); |
| 5655 | } |
| 5656 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5657 | if (display_type == DT_NOGRAPHIC) { |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5658 | if (serial_device_index == 0) |
| 5659 | serial_devices[0] = "stdio"; |
| 5660 | if (parallel_device_index == 0) |
| 5661 | parallel_devices[0] = "null"; |
| 5662 | if (strncmp(monitor_device, "vc", 2) == 0) |
| 5663 | monitor_device = "stdio"; |
| 5664 | } |
| 5665 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5666 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5667 | if (daemonize) { |
| 5668 | pid_t pid; |
| 5669 | |
| 5670 | if (pipe(fds) == -1) |
| 5671 | exit(1); |
| 5672 | |
| 5673 | pid = fork(); |
| 5674 | if (pid > 0) { |
| 5675 | uint8_t status; |
| 5676 | ssize_t len; |
| 5677 | |
| 5678 | close(fds[1]); |
| 5679 | |
| 5680 | again: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5681 | len = read(fds[0], &status, 1); |
| 5682 | if (len == -1 && (errno == EINTR)) |
| 5683 | goto again; |
| 5684 | |
| 5685 | if (len != 1) |
| 5686 | exit(1); |
| 5687 | else if (status == 1) { |
| 5688 | fprintf(stderr, "Could not acquire pidfile\n"); |
| 5689 | exit(1); |
| 5690 | } else |
| 5691 | exit(0); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5692 | } else if (pid < 0) |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5693 | exit(1); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5694 | |
| 5695 | setsid(); |
| 5696 | |
| 5697 | pid = fork(); |
| 5698 | if (pid > 0) |
| 5699 | exit(0); |
| 5700 | else if (pid < 0) |
| 5701 | exit(1); |
| 5702 | |
| 5703 | umask(027); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5704 | |
| 5705 | signal(SIGTSTP, SIG_IGN); |
| 5706 | signal(SIGTTOU, SIG_IGN); |
| 5707 | signal(SIGTTIN, SIG_IGN); |
| 5708 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5709 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 5710 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5711 | if (daemonize) { |
| 5712 | uint8_t status = 1; |
| 5713 | write(fds[1], &status, 1); |
| 5714 | } else |
| 5715 | fprintf(stderr, "Could not acquire pid file\n"); |
| 5716 | exit(1); |
| 5717 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 5718 | #endif |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5719 | |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 5720 | if (qemu_init_main_loop()) { |
| 5721 | fprintf(stderr, "qemu_init_main_loop failed\n"); |
| 5722 | exit(1); |
| 5723 | } |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5724 | linux_boot = (kernel_filename != NULL); |
balrog | 6c41b27 | 2007-11-17 12:12:29 +0000 | [diff] [blame] | 5725 | |
ths | f8d39c0 | 2008-07-03 10:01:15 +0000 | [diff] [blame] | 5726 | if (!linux_boot && *kernel_cmdline != '\0') { |
| 5727 | fprintf(stderr, "-append only allowed with -kernel option\n"); |
| 5728 | exit(1); |
| 5729 | } |
| 5730 | |
| 5731 | if (!linux_boot && initrd_filename != NULL) { |
| 5732 | fprintf(stderr, "-initrd only allowed with -kernel option\n"); |
| 5733 | exit(1); |
| 5734 | } |
| 5735 | |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5736 | #ifndef _WIN32 |
| 5737 | /* Win32 doesn't support line-buffering and requires size >= 2 */ |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 5738 | setvbuf(stdout, NULL, _IOLBF, 0); |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5739 | #endif |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5740 | |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 5741 | init_timers(); |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 5742 | if (init_timer_alarm() < 0) { |
| 5743 | fprintf(stderr, "could not initialize alarm timer\n"); |
| 5744 | exit(1); |
| 5745 | } |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5746 | if (use_icount && icount_time_shift < 0) { |
| 5747 | use_icount = 2; |
| 5748 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 5749 | It will be corrected fairly quickly anyway. */ |
| 5750 | icount_time_shift = 3; |
| 5751 | init_icount_adjust(); |
| 5752 | } |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 5753 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5754 | #ifdef _WIN32 |
| 5755 | socket_init(); |
| 5756 | #endif |
| 5757 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5758 | /* init network clients */ |
| 5759 | if (nb_net_clients == 0) { |
| 5760 | /* if no clients, we use a default config */ |
aliguori | f441b28 | 2008-08-28 20:05:14 +0000 | [diff] [blame] | 5761 | net_clients[nb_net_clients++] = "nic"; |
| 5762 | #ifdef CONFIG_SLIRP |
| 5763 | net_clients[nb_net_clients++] = "user"; |
| 5764 | #endif |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5767 | for(i = 0;i < nb_net_clients; i++) { |
balrog | 9ad97e6 | 2008-07-29 13:16:31 +0000 | [diff] [blame] | 5768 | if (net_client_parse(net_clients[i]) < 0) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5769 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5770 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 5771 | |
Glauber Costa | 406c8df | 2009-06-17 09:05:30 -0400 | [diff] [blame] | 5772 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
| 5773 | net_set_boot_mask(net_boot); |
| 5774 | |
| 5775 | net_client_check(); |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 5776 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5777 | /* init the bluetooth world */ |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5778 | if (foreach_device_config(DEV_BT, bt_parse)) |
| 5779 | exit(1); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5780 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5781 | /* init the memory */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 5782 | if (ram_size == 0) |
| 5783 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 5784 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5785 | /* init the dynamic translator */ |
| 5786 | cpu_exec_init_all(tb_size * 1024 * 1024); |
| 5787 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 5788 | bdrv_init(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5789 | |
| 5790 | /* we always create the cdrom drive, even if no disk is there */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5791 | drive_add(NULL, CDROM_ALIAS); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5792 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5793 | /* we always create at least one floppy */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5794 | drive_add(NULL, FD_ALIAS, 0); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5795 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5796 | /* we always create one sd slot, even if no card is in it */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5797 | drive_add(NULL, SD_ALIAS); |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5798 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 5799 | /* open the virtual block devices */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5800 | if (snapshot) |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 5801 | qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
| 5802 | if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5803 | exit(1); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5804 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5805 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 5806 | register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5807 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5808 | #ifndef _WIN32 |
| 5809 | /* must be after terminal init, SDL library changes signal handlers */ |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 5810 | sighandler_setup(); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5811 | #endif |
| 5812 | |
| 5813 | /* Maintain compatibility with multiple stdio monitors */ |
| 5814 | if (!strcmp(monitor_device,"stdio")) { |
| 5815 | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 5816 | const char *devname = serial_devices[i]; |
| 5817 | if (devname && !strcmp(devname,"mon:stdio")) { |
| 5818 | monitor_device = NULL; |
| 5819 | break; |
| 5820 | } else if (devname && !strcmp(devname,"stdio")) { |
| 5821 | monitor_device = NULL; |
| 5822 | serial_devices[i] = "mon:stdio"; |
| 5823 | break; |
| 5824 | } |
| 5825 | } |
| 5826 | } |
| 5827 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5828 | if (nb_numa_nodes > 0) { |
| 5829 | int i; |
| 5830 | |
| 5831 | if (nb_numa_nodes > smp_cpus) { |
| 5832 | nb_numa_nodes = smp_cpus; |
| 5833 | } |
| 5834 | |
| 5835 | /* If no memory size if given for any node, assume the default case |
| 5836 | * and distribute the available memory equally across all nodes |
| 5837 | */ |
| 5838 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5839 | if (node_mem[i] != 0) |
| 5840 | break; |
| 5841 | } |
| 5842 | if (i == nb_numa_nodes) { |
| 5843 | uint64_t usedmem = 0; |
| 5844 | |
| 5845 | /* On Linux, the each node's border has to be 8MB aligned, |
| 5846 | * the final node gets the rest. |
| 5847 | */ |
| 5848 | for (i = 0; i < nb_numa_nodes - 1; i++) { |
| 5849 | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); |
| 5850 | usedmem += node_mem[i]; |
| 5851 | } |
| 5852 | node_mem[i] = ram_size - usedmem; |
| 5853 | } |
| 5854 | |
| 5855 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5856 | if (node_cpumask[i] != 0) |
| 5857 | break; |
| 5858 | } |
| 5859 | /* assigning the VCPUs round-robin is easier to implement, guest OSes |
| 5860 | * must cope with this anyway, because there are BIOSes out there in |
| 5861 | * real machines which also use this scheme. |
| 5862 | */ |
| 5863 | if (i == nb_numa_nodes) { |
| 5864 | for (i = 0; i < smp_cpus; i++) { |
| 5865 | node_cpumask[i % nb_numa_nodes] |= 1 << i; |
| 5866 | } |
| 5867 | } |
| 5868 | } |
| 5869 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5870 | if (kvm_enabled()) { |
| 5871 | int ret; |
| 5872 | |
| 5873 | ret = kvm_init(smp_cpus); |
| 5874 | if (ret < 0) { |
| 5875 | fprintf(stderr, "failed to initialize KVM\n"); |
| 5876 | exit(1); |
| 5877 | } |
| 5878 | } |
| 5879 | |
aliguori | 4c62180 | 2009-01-16 21:48:20 +0000 | [diff] [blame] | 5880 | if (monitor_device) { |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5881 | monitor_hd = qemu_chr_open("monitor", monitor_device, NULL); |
aliguori | 4c62180 | 2009-01-16 21:48:20 +0000 | [diff] [blame] | 5882 | if (!monitor_hd) { |
| 5883 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); |
| 5884 | exit(1); |
| 5885 | } |
| 5886 | } |
| 5887 | |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5888 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 5889 | const char *devname = serial_devices[i]; |
| 5890 | if (devname && strcmp(devname, "none")) { |
| 5891 | char label[32]; |
| 5892 | snprintf(label, sizeof(label), "serial%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5893 | serial_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5894 | if (!serial_hds[i]) { |
| 5895 | fprintf(stderr, "qemu: could not open serial device '%s'\n", |
| 5896 | devname); |
| 5897 | exit(1); |
| 5898 | } |
| 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
| 5903 | const char *devname = parallel_devices[i]; |
| 5904 | if (devname && strcmp(devname, "none")) { |
| 5905 | char label[32]; |
| 5906 | snprintf(label, sizeof(label), "parallel%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5907 | parallel_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5908 | if (!parallel_hds[i]) { |
| 5909 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", |
| 5910 | devname); |
| 5911 | exit(1); |
| 5912 | } |
| 5913 | } |
| 5914 | } |
| 5915 | |
| 5916 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
| 5917 | const char *devname = virtio_consoles[i]; |
| 5918 | if (devname && strcmp(devname, "none")) { |
| 5919 | char label[32]; |
| 5920 | snprintf(label, sizeof(label), "virtcon%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5921 | virtcon_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5922 | if (!virtcon_hds[i]) { |
| 5923 | fprintf(stderr, "qemu: could not open virtio console '%s'\n", |
| 5924 | devname); |
| 5925 | exit(1); |
| 5926 | } |
| 5927 | } |
| 5928 | } |
| 5929 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 5930 | module_call_init(MODULE_INIT_DEVICE); |
| 5931 | |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame^] | 5932 | if (watchdog) { |
| 5933 | i = select_watchdog(watchdog); |
| 5934 | if (i > 0) |
| 5935 | exit (i == 1 ? 1 : 0); |
| 5936 | } |
| 5937 | |
Gerd Hoffmann | b6b6114 | 2009-07-15 13:48:21 +0200 | [diff] [blame] | 5938 | if (machine->compat_props) { |
| 5939 | qdev_prop_register_compat(machine->compat_props); |
| 5940 | } |
Paul Brook | fbe1b59 | 2009-05-13 17:56:25 +0100 | [diff] [blame] | 5941 | machine->init(ram_size, boot_devices, |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5942 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
| 5943 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5944 | |
| 5945 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 5946 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5947 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 5948 | env->numa_node = i; |
| 5949 | } |
| 5950 | } |
| 5951 | } |
| 5952 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 5953 | current_machine = machine; |
| 5954 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5955 | /* init USB devices */ |
| 5956 | if (usb_enabled) { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5957 | foreach_device_config(DEV_USB, usb_parse); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5958 | } |
| 5959 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5960 | /* init generic devices */ |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5961 | if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0) |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5962 | exit(1); |
| 5963 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 5964 | if (!display_state) |
| 5965 | dumb_display_init(); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5966 | /* just use the first displaystate for the moment */ |
| 5967 | ds = display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5968 | |
| 5969 | if (display_type == DT_DEFAULT) { |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5970 | #if defined(CONFIG_SDL) || defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5971 | display_type = DT_SDL; |
| 5972 | #else |
| 5973 | display_type = DT_VNC; |
| 5974 | vnc_display = "localhost:0,to=99"; |
| 5975 | show_vnc_port = 1; |
| 5976 | #endif |
| 5977 | } |
| 5978 | |
| 5979 | |
| 5980 | switch (display_type) { |
| 5981 | case DT_NOGRAPHIC: |
| 5982 | break; |
| 5983 | #if defined(CONFIG_CURSES) |
| 5984 | case DT_CURSES: |
| 5985 | curses_display_init(ds, full_screen); |
| 5986 | break; |
| 5987 | #endif |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5988 | #if defined(CONFIG_SDL) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5989 | case DT_SDL: |
| 5990 | sdl_display_init(ds, full_screen, no_frame); |
| 5991 | break; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5992 | #elif defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5993 | case DT_SDL: |
| 5994 | cocoa_display_init(ds, full_screen); |
| 5995 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5996 | #endif |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5997 | case DT_VNC: |
| 5998 | vnc_display_init(ds); |
| 5999 | if (vnc_display_open(ds, vnc_display) < 0) |
| 6000 | exit(1); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 6001 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6002 | if (show_vnc_port) { |
| 6003 | printf("VNC server running on `%s'\n", vnc_display_local_addr(ds)); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 6004 | } |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6005 | break; |
| 6006 | default: |
| 6007 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6008 | } |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 6009 | dpy_resize(ds); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 6010 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 6011 | dcl = ds->listeners; |
| 6012 | while (dcl != NULL) { |
| 6013 | if (dcl->dpy_refresh != NULL) { |
| 6014 | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); |
| 6015 | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 6016 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 6017 | dcl = dcl->next; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6018 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 6019 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6020 | if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) { |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 6021 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL); |
| 6022 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); |
| 6023 | } |
| 6024 | |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 6025 | text_consoles_set_display(display_state); |
aliguori | 2970a6c | 2009-03-05 22:59:58 +0000 | [diff] [blame] | 6026 | qemu_chr_initial_reset(); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 6027 | |
aliguori | 4c62180 | 2009-01-16 21:48:20 +0000 | [diff] [blame] | 6028 | if (monitor_device && monitor_hd) |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 6029 | monitor_init(monitor_hd, MONITOR_USE_READLINE | MONITOR_IS_DEFAULT); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6030 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6031 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 6032 | const char *devname = serial_devices[i]; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 6033 | if (devname && strcmp(devname, "none")) { |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 6034 | if (strstart(devname, "vc", 0)) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 6035 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6036 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6037 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6038 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6039 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 6040 | const char *devname = parallel_devices[i]; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 6041 | if (devname && strcmp(devname, "none")) { |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 6042 | if (strstart(devname, "vc", 0)) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 6043 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6044 | } |
| 6045 | } |
| 6046 | |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 6047 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
| 6048 | const char *devname = virtio_consoles[i]; |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 6049 | if (virtcon_hds[i] && devname) { |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 6050 | if (strstart(devname, "vc", 0)) |
| 6051 | qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i); |
| 6052 | } |
| 6053 | } |
| 6054 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 6055 | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
| 6056 | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n", |
| 6057 | gdbstub_dev); |
| 6058 | exit(1); |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 6059 | } |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 6060 | |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 6061 | if (loadvm) { |
| 6062 | if (load_vmstate(cur_mon, loadvm) < 0) { |
| 6063 | autostart = 0; |
| 6064 | } |
| 6065 | } |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6066 | |
Glauber Costa | 2bb8c10 | 2009-07-24 16:20:23 -0400 | [diff] [blame] | 6067 | if (incoming) { |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 6068 | qemu_start_incoming_migration(incoming); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 6069 | } else if (autostart) { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 6070 | vm_start(); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 6071 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 6072 | |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 6073 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6074 | if (daemonize) { |
| 6075 | uint8_t status = 0; |
| 6076 | ssize_t len; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6077 | |
| 6078 | again1: |
| 6079 | len = write(fds[1], &status, 1); |
| 6080 | if (len == -1 && (errno == EINTR)) |
| 6081 | goto again1; |
| 6082 | |
| 6083 | if (len != 1) |
| 6084 | exit(1); |
| 6085 | |
aliguori | bd54b86 | 2008-07-23 00:58:33 +0000 | [diff] [blame] | 6086 | chdir("/"); |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 6087 | TFR(fd = open("/dev/null", O_RDWR)); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6088 | if (fd == -1) |
| 6089 | exit(1); |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6090 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6091 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6092 | if (run_as) { |
| 6093 | pwd = getpwnam(run_as); |
| 6094 | if (!pwd) { |
| 6095 | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as); |
| 6096 | exit(1); |
| 6097 | } |
| 6098 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6099 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6100 | if (chroot_dir) { |
| 6101 | if (chroot(chroot_dir) < 0) { |
| 6102 | fprintf(stderr, "chroot failed\n"); |
| 6103 | exit(1); |
| 6104 | } |
| 6105 | chdir("/"); |
| 6106 | } |
| 6107 | |
| 6108 | if (run_as) { |
| 6109 | if (setgid(pwd->pw_gid) < 0) { |
| 6110 | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid); |
| 6111 | exit(1); |
| 6112 | } |
| 6113 | if (setuid(pwd->pw_uid) < 0) { |
| 6114 | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid); |
| 6115 | exit(1); |
| 6116 | } |
| 6117 | if (setuid(0) != -1) { |
| 6118 | fprintf(stderr, "Dropping privileges failed\n"); |
| 6119 | exit(1); |
| 6120 | } |
| 6121 | } |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6122 | |
| 6123 | if (daemonize) { |
| 6124 | dup2(fd, 0); |
| 6125 | dup2(fd, 1); |
| 6126 | dup2(fd, 2); |
| 6127 | |
| 6128 | close(fd); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6129 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 6130 | #endif |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6131 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6132 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 6133 | quit_timers(); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 6134 | net_cleanup(); |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 6135 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6136 | return 0; |
| 6137 | } |