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" |
| 34 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 35 | #ifndef _WIN32 |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 36 | #include <libgen.h> |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 37 | #include <pwd.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 38 | #include <sys/times.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 39 | #include <sys/wait.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 40 | #include <termios.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 41 | #include <sys/mman.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 42 | #include <sys/ioctl.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 43 | #include <sys/resource.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 44 | #include <sys/socket.h> |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 45 | #include <netinet/in.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 46 | #include <net/if.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 47 | #include <arpa/inet.h> |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 48 | #include <dirent.h> |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 49 | #include <netdb.h> |
ths | cb4b976 | 2007-09-13 12:39:35 +0000 | [diff] [blame] | 50 | #include <sys/select.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 51 | #ifdef CONFIG_BSD |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 52 | #include <sys/stat.h> |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 53 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 54 | #include <libutil.h> |
blueswir1 | 24646c7 | 2008-11-07 16:55:48 +0000 | [diff] [blame] | 55 | #else |
| 56 | #include <util.h> |
blueswir1 | 128ab2f | 2008-08-15 18:33:42 +0000 | [diff] [blame] | 57 | #endif |
Aurelien Jarno | bbe813a | 2009-11-30 15:42:59 +0100 | [diff] [blame] | 58 | #else |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 59 | #ifdef __linux__ |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 60 | #include <pty.h> |
| 61 | #include <malloc.h> |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 62 | #include <linux/rtc.h> |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 63 | #include <sys/prctl.h> |
ths | bd494f4 | 2007-09-16 20:03:23 +0000 | [diff] [blame] | 64 | |
| 65 | /* For the benefit of older linux systems which don't supply it, |
| 66 | we use a local copy of hpet.h. */ |
| 67 | /* #include <linux/hpet.h> */ |
| 68 | #include "hpet.h" |
| 69 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 70 | #include <linux/ppdev.h> |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 71 | #include <linux/parport.h> |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 72 | #endif |
| 73 | #ifdef __sun__ |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 74 | #include <sys/stat.h> |
| 75 | #include <sys/ethernet.h> |
| 76 | #include <sys/sockio.h> |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 77 | #include <netinet/arp.h> |
| 78 | #include <netinet/in.h> |
| 79 | #include <netinet/in_systm.h> |
| 80 | #include <netinet/ip.h> |
| 81 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 82 | #include <netinet/udp.h> |
| 83 | #include <netinet/tcp.h> |
| 84 | #include <net/if.h> |
| 85 | #include <syslog.h> |
| 86 | #include <stropts.h> |
Blue Swirl | 8d32cf0 | 2009-10-02 19:32:12 +0000 | [diff] [blame] | 87 | /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for |
| 88 | discussion about Solaris header problems */ |
| 89 | extern int madvise(caddr_t, size_t, int); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 90 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 91 | #endif |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 92 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 93 | |
blueswir1 | 9892fbf | 2008-08-24 10:34:20 +0000 | [diff] [blame] | 94 | #if defined(__OpenBSD__) |
| 95 | #include <util.h> |
| 96 | #endif |
| 97 | |
ths | 8a16d27 | 2008-07-19 09:56:24 +0000 | [diff] [blame] | 98 | #if defined(CONFIG_VDE) |
| 99 | #include <libvdeplug.h> |
| 100 | #endif |
| 101 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 102 | #ifdef _WIN32 |
aliguori | 49dc768 | 2009-03-08 16:26:59 +0000 | [diff] [blame] | 103 | #include <windows.h> |
ths | 4fddf62 | 2007-12-17 04:42:29 +0000 | [diff] [blame] | 104 | #include <mmsystem.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 107 | #ifdef CONFIG_SDL |
Stefan Weil | 59a36a2 | 2009-06-18 20:11:03 +0200 | [diff] [blame] | 108 | #if defined(__APPLE__) || defined(main) |
Stefan Weil | 6693665 | 2009-06-13 13:19:11 +0200 | [diff] [blame] | 109 | #include <SDL.h> |
malc | 880fec5 | 2009-02-15 20:18:41 +0000 | [diff] [blame] | 110 | int qemu_main(int argc, char **argv, char **envp); |
| 111 | int main(int argc, char **argv) |
| 112 | { |
Stefan Weil | 59a36a2 | 2009-06-18 20:11:03 +0200 | [diff] [blame] | 113 | return qemu_main(argc, argv, NULL); |
malc | 880fec5 | 2009-02-15 20:18:41 +0000 | [diff] [blame] | 114 | } |
| 115 | #undef main |
| 116 | #define main qemu_main |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 117 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 118 | #endif /* CONFIG_SDL */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 119 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 120 | #ifdef CONFIG_COCOA |
| 121 | #undef main |
| 122 | #define main qemu_main |
| 123 | #endif /* CONFIG_COCOA */ |
| 124 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 125 | #include "hw/hw.h" |
| 126 | #include "hw/boards.h" |
| 127 | #include "hw/usb.h" |
| 128 | #include "hw/pcmcia.h" |
| 129 | #include "hw/pc.h" |
| 130 | #include "hw/audiodev.h" |
| 131 | #include "hw/isa.h" |
| 132 | #include "hw/baum.h" |
| 133 | #include "hw/bt.h" |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 134 | #include "hw/watchdog.h" |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 135 | #include "hw/smbios.h" |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 136 | #include "hw/xen.h" |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 137 | #include "hw/qdev.h" |
Gerd Hoffmann | 45a50b1 | 2009-10-01 16:42:33 +0200 | [diff] [blame] | 138 | #include "hw/loader.h" |
aurel32 | 5ef4efa | 2009-03-10 21:43:35 +0000 | [diff] [blame] | 139 | #include "bt-host.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 140 | #include "net.h" |
Mark McLoughlin | 68ac40d | 2009-11-25 18:48:54 +0000 | [diff] [blame] | 141 | #include "net/slirp.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 142 | #include "monitor.h" |
| 143 | #include "console.h" |
| 144 | #include "sysemu.h" |
| 145 | #include "gdbstub.h" |
| 146 | #include "qemu-timer.h" |
| 147 | #include "qemu-char.h" |
| 148 | #include "cache-utils.h" |
| 149 | #include "block.h" |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 150 | #include "block_int.h" |
| 151 | #include "block-migration.h" |
blueswir1 | a718ace | 2009-03-28 08:24:44 +0000 | [diff] [blame] | 152 | #include "dma.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 153 | #include "audio/audio.h" |
| 154 | #include "migration.h" |
| 155 | #include "kvm.h" |
| 156 | #include "balloon.h" |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 157 | #include "qemu-option.h" |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 158 | #include "qemu-config.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 159 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 160 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 161 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 162 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 163 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 164 | #include "qemu_socket.h" |
| 165 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 166 | #include "slirp/libslirp.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 167 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 168 | #include "qemu-queue.h" |
| 169 | |
blueswir1 | 9dc63a1 | 2008-10-04 07:25:46 +0000 | [diff] [blame] | 170 | //#define DEBUG_NET |
| 171 | //#define DEBUG_SLIRP |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 172 | |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 173 | #define DEFAULT_RAM_SIZE 128 |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 174 | |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 175 | /* Maximum number of monitor devices */ |
| 176 | #define MAX_MONITOR_DEVICES 10 |
| 177 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 178 | static const char *data_dir; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 179 | const char *bios_name = NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 180 | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 181 | to store the VM snapshots */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 182 | struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives); |
| 183 | struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts); |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 184 | enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 185 | static DisplayState *display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 186 | DisplayType display_type = DT_DEFAULT; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 187 | const char* keyboard_layout = NULL; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 188 | ram_addr_t ram_size; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 189 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 190 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 191 | int vm_running; |
Paolo Bonzini | d399f67 | 2009-07-27 23:17:51 +0200 | [diff] [blame] | 192 | int autostart; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 193 | static int rtc_utc = 1; |
| 194 | static int rtc_date_offset = -1; /* -1 means no change */ |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 195 | QEMUClock *rtc_clock; |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 196 | int vga_interface_type = VGA_NONE; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 197 | #ifdef TARGET_SPARC |
| 198 | int graphic_width = 1024; |
| 199 | int graphic_height = 768; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 200 | int graphic_depth = 8; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 201 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 202 | int graphic_width = 800; |
| 203 | int graphic_height = 600; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 204 | int graphic_depth = 15; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 205 | #endif |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 206 | static int full_screen = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 207 | #ifdef CONFIG_SDL |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 208 | static int no_frame = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 209 | #endif |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 210 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 211 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 212 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 213 | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 214 | CharDriverState *monitor_hds[MAX_MONITOR_DEVICES]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 215 | #ifdef TARGET_I386 |
| 216 | int win2k_install_hack = 0; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 217 | int rtc_td_hack = 0; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 218 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 219 | int usb_enabled = 0; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 220 | int singlestep = 0; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 221 | int smp_cpus = 1; |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 222 | int max_cpus = 0; |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 223 | int smp_cores = 1; |
| 224 | int smp_threads = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 225 | const char *vnc_display; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 226 | int acpi_enabled = 1; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 227 | int no_hpet = 0; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 228 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 229 | int no_reboot = 0; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 230 | int no_shutdown = 0; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 231 | int cursor_hide = 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 232 | int graphic_rotate = 0; |
Jes Sorensen | 6b35e7b | 2009-08-06 16:25:50 +0200 | [diff] [blame] | 233 | uint8_t irq0override = 1; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 234 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 235 | int daemonize = 0; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 236 | #endif |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 237 | const char *watchdog; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 238 | const char *option_rom[MAX_OPTION_ROMS]; |
| 239 | int nb_option_roms; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 240 | int semihosting_enabled = 0; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 241 | #ifdef TARGET_ARM |
| 242 | int old_param = 0; |
| 243 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 244 | const char *qemu_name; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 245 | int alt_grab = 0; |
Dustin Kirkland | 0ca9f8a | 2009-09-17 15:48:04 -0500 | [diff] [blame] | 246 | int ctrl_grab = 0; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 247 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 248 | unsigned int nb_prom_envs = 0; |
| 249 | const char *prom_envs[MAX_PROM_ENVS]; |
| 250 | #endif |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 251 | int boot_menu; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 252 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 253 | int nb_numa_nodes; |
| 254 | uint64_t node_mem[MAX_NODES]; |
| 255 | uint64_t node_cpumask[MAX_NODES]; |
| 256 | |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 257 | static CPUState *cur_cpu; |
| 258 | static CPUState *next_cpu; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 259 | static int timer_alarm_pending = 1; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 260 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 261 | static int icount_time_shift; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 262 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 263 | #define MAX_ICOUNT_SHIFT 10 |
| 264 | /* Compensate for varying guest execution speed. */ |
| 265 | static int64_t qemu_icount_bias; |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 266 | static QEMUTimer *icount_rt_timer; |
| 267 | static QEMUTimer *icount_vm_timer; |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 268 | static QEMUTimer *nographic_timer; |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 269 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 270 | uint8_t qemu_uuid[16]; |
| 271 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 272 | static QEMUBootSetHandler *boot_set_handler; |
| 273 | static void *boot_set_opaque; |
| 274 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 275 | static int default_serial = 1; |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 276 | static int default_parallel = 1; |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 277 | static int default_monitor = 1; |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 278 | static int default_vga = 1; |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 279 | |
| 280 | static struct { |
| 281 | const char *driver; |
| 282 | int *flag; |
| 283 | } default_list[] = { |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 284 | { .driver = "isa-serial", .flag = &default_serial }, |
| 285 | { .driver = "isa-parallel", .flag = &default_parallel }, |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 286 | { .driver = "VGA", .flag = &default_vga }, |
| 287 | { .driver = "Cirrus VGA", .flag = &default_vga }, |
| 288 | { .driver = "QEMUware SVGA", .flag = &default_vga }, |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | static int default_driver_check(QemuOpts *opts, void *opaque) |
| 292 | { |
| 293 | const char *driver = qemu_opt_get(opts, "driver"); |
| 294 | int i; |
| 295 | |
| 296 | if (!driver) |
| 297 | return 0; |
| 298 | for (i = 0; i < ARRAY_SIZE(default_list); i++) { |
| 299 | if (strcmp(default_list[i].driver, driver) != 0) |
| 300 | continue; |
| 301 | *(default_list[i].flag) = 0; |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 306 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 307 | /* x86 ISA bus support */ |
| 308 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 309 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 310 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 311 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 312 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 313 | void hw_error(const char *fmt, ...) |
| 314 | { |
| 315 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 316 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 317 | |
| 318 | va_start(ap, fmt); |
| 319 | fprintf(stderr, "qemu: hardware error: "); |
| 320 | vfprintf(stderr, fmt, ap); |
| 321 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 322 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 323 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 324 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 325 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 326 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 327 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 328 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 329 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 330 | va_end(ap); |
| 331 | abort(); |
| 332 | } |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 333 | |
| 334 | static void set_proc_name(const char *s) |
| 335 | { |
Nathan Froyd | 6ca8d0f | 2009-08-03 07:32:12 -0700 | [diff] [blame] | 336 | #if defined(__linux__) && defined(PR_SET_NAME) |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 337 | char name[16]; |
| 338 | if (!s) |
| 339 | return; |
| 340 | name[sizeof(name) - 1] = 0; |
| 341 | strncpy(name, s, sizeof(name)); |
| 342 | /* Could rewrite argv[0] too, but that's a bit more complicated. |
| 343 | This simple way is enough for `top'. */ |
| 344 | prctl(PR_SET_NAME, name); |
| 345 | #endif |
| 346 | } |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 347 | |
| 348 | /***************/ |
| 349 | /* ballooning */ |
| 350 | |
| 351 | static QEMUBalloonEvent *qemu_balloon_event; |
| 352 | void *qemu_balloon_event_opaque; |
| 353 | |
| 354 | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) |
| 355 | { |
| 356 | qemu_balloon_event = func; |
| 357 | qemu_balloon_event_opaque = opaque; |
| 358 | } |
| 359 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 360 | void qemu_balloon(ram_addr_t target) |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 361 | { |
| 362 | if (qemu_balloon_event) |
| 363 | qemu_balloon_event(qemu_balloon_event_opaque, target); |
| 364 | } |
| 365 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 366 | ram_addr_t qemu_balloon_status(void) |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 367 | { |
| 368 | if (qemu_balloon_event) |
| 369 | return qemu_balloon_event(qemu_balloon_event_opaque, 0); |
| 370 | return 0; |
| 371 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 372 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 373 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 374 | /* keyboard/mouse */ |
| 375 | |
| 376 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 377 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 378 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 379 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 380 | |
| 381 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 382 | { |
| 383 | qemu_put_kbd_event_opaque = opaque; |
| 384 | qemu_put_kbd_event = func; |
| 385 | } |
| 386 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 387 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 388 | void *opaque, int absolute, |
| 389 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 390 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 391 | QEMUPutMouseEntry *s, *cursor; |
| 392 | |
| 393 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 394 | |
| 395 | s->qemu_put_mouse_event = func; |
| 396 | s->qemu_put_mouse_event_opaque = opaque; |
| 397 | s->qemu_put_mouse_event_absolute = absolute; |
| 398 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 399 | s->next = NULL; |
| 400 | |
| 401 | if (!qemu_put_mouse_event_head) { |
| 402 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 403 | return s; |
| 404 | } |
| 405 | |
| 406 | cursor = qemu_put_mouse_event_head; |
| 407 | while (cursor->next != NULL) |
| 408 | cursor = cursor->next; |
| 409 | |
| 410 | cursor->next = s; |
| 411 | qemu_put_mouse_event_current = s; |
| 412 | |
| 413 | return s; |
| 414 | } |
| 415 | |
| 416 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 417 | { |
| 418 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 419 | |
| 420 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 421 | return; |
| 422 | |
| 423 | cursor = qemu_put_mouse_event_head; |
| 424 | while (cursor != NULL && cursor != entry) { |
| 425 | prev = cursor; |
| 426 | cursor = cursor->next; |
| 427 | } |
| 428 | |
| 429 | if (cursor == NULL) // does not exist or list empty |
| 430 | return; |
| 431 | else if (prev == NULL) { // entry is head |
| 432 | qemu_put_mouse_event_head = cursor->next; |
| 433 | if (qemu_put_mouse_event_current == entry) |
| 434 | qemu_put_mouse_event_current = cursor->next; |
| 435 | qemu_free(entry->qemu_put_mouse_event_name); |
| 436 | qemu_free(entry); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | prev->next = entry->next; |
| 441 | |
| 442 | if (qemu_put_mouse_event_current == entry) |
| 443 | qemu_put_mouse_event_current = prev; |
| 444 | |
| 445 | qemu_free(entry->qemu_put_mouse_event_name); |
| 446 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void kbd_put_keycode(int keycode) |
| 450 | { |
| 451 | if (qemu_put_kbd_event) { |
| 452 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 457 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 458 | QEMUPutMouseEvent *mouse_event; |
| 459 | void *mouse_event_opaque; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 460 | int width; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 461 | |
| 462 | if (!qemu_put_mouse_event_current) { |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | mouse_event = |
| 467 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 468 | mouse_event_opaque = |
| 469 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 470 | |
| 471 | if (mouse_event) { |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 472 | if (graphic_rotate) { |
| 473 | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute) |
| 474 | width = 0x7fff; |
| 475 | else |
aurel32 | b94ed57 | 2008-03-10 19:34:27 +0000 | [diff] [blame] | 476 | width = graphic_width - 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 477 | mouse_event(mouse_event_opaque, |
| 478 | width - dy, dx, dz, buttons_state); |
| 479 | } else |
| 480 | mouse_event(mouse_event_opaque, |
| 481 | dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 485 | int kbd_mouse_is_absolute(void) |
| 486 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 487 | if (!qemu_put_mouse_event_current) |
| 488 | return 0; |
| 489 | |
| 490 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 491 | } |
| 492 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 493 | void do_info_mice(Monitor *mon) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 494 | { |
| 495 | QEMUPutMouseEntry *cursor; |
| 496 | int index = 0; |
| 497 | |
| 498 | if (!qemu_put_mouse_event_head) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 499 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 500 | return; |
| 501 | } |
| 502 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 503 | monitor_printf(mon, "Mouse devices available:\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 504 | cursor = qemu_put_mouse_event_head; |
| 505 | while (cursor != NULL) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 506 | monitor_printf(mon, "%c Mouse #%d: %s\n", |
| 507 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
| 508 | index, cursor->qemu_put_mouse_event_name); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 509 | index++; |
| 510 | cursor = cursor->next; |
| 511 | } |
| 512 | } |
| 513 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 514 | void do_mouse_set(Monitor *mon, const QDict *qdict) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 515 | { |
| 516 | QEMUPutMouseEntry *cursor; |
| 517 | int i = 0; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 518 | int index = qdict_get_int(qdict, "index"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 519 | |
| 520 | if (!qemu_put_mouse_event_head) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 521 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | |
| 525 | cursor = qemu_put_mouse_event_head; |
| 526 | while (cursor != NULL && index != i) { |
| 527 | i++; |
| 528 | cursor = cursor->next; |
| 529 | } |
| 530 | |
| 531 | if (cursor != NULL) |
| 532 | qemu_put_mouse_event_current = cursor; |
| 533 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 534 | monitor_printf(mon, "Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 535 | } |
| 536 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 537 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 538 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 539 | { |
| 540 | union { |
| 541 | uint64_t ll; |
| 542 | struct { |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 543 | #ifdef HOST_WORDS_BIGENDIAN |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 544 | uint32_t high, low; |
| 545 | #else |
| 546 | uint32_t low, high; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 547 | #endif |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 548 | } l; |
| 549 | } u, res; |
| 550 | uint64_t rl, rh; |
| 551 | |
| 552 | u.ll = a; |
| 553 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 554 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 555 | rh += (rl >> 32); |
| 556 | res.l.high = rh / c; |
| 557 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 558 | return res.ll; |
| 559 | } |
| 560 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 561 | /***********************************************************/ |
| 562 | /* real time host monotonic timer */ |
| 563 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 564 | static int64_t get_clock_realtime(void) |
| 565 | { |
| 566 | struct timeval tv; |
| 567 | |
| 568 | gettimeofday(&tv, NULL); |
| 569 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 570 | } |
| 571 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 572 | #ifdef WIN32 |
| 573 | |
| 574 | static int64_t clock_freq; |
| 575 | |
| 576 | static void init_get_clock(void) |
| 577 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 578 | LARGE_INTEGER freq; |
| 579 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 580 | ret = QueryPerformanceFrequency(&freq); |
| 581 | if (ret == 0) { |
| 582 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 583 | exit(1); |
| 584 | } |
| 585 | clock_freq = freq.QuadPart; |
| 586 | } |
| 587 | |
| 588 | static int64_t get_clock(void) |
| 589 | { |
| 590 | LARGE_INTEGER ti; |
| 591 | QueryPerformanceCounter(&ti); |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 592 | return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq); |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | #else |
| 596 | |
| 597 | static int use_rt_clock; |
| 598 | |
| 599 | static void init_get_clock(void) |
| 600 | { |
| 601 | use_rt_clock = 0; |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 602 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 603 | || defined(__DragonFly__) || defined(__FreeBSD_kernel__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 604 | { |
| 605 | struct timespec ts; |
| 606 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 607 | use_rt_clock = 1; |
| 608 | } |
| 609 | } |
| 610 | #endif |
| 611 | } |
| 612 | |
| 613 | static int64_t get_clock(void) |
| 614 | { |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 615 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 616 | || defined(__DragonFly__) || defined(__FreeBSD_kernel__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 617 | if (use_rt_clock) { |
| 618 | struct timespec ts; |
| 619 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 620 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 621 | } else |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 622 | #endif |
| 623 | { |
| 624 | /* XXX: using gettimeofday leads to problems if the date |
| 625 | changes, so it should be avoided. */ |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 626 | return get_clock_realtime(); |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 629 | #endif |
| 630 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 631 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 632 | static int64_t cpu_get_icount(void) |
| 633 | { |
| 634 | int64_t icount; |
| 635 | CPUState *env = cpu_single_env;; |
| 636 | icount = qemu_icount; |
| 637 | if (env) { |
| 638 | if (!can_do_io(env)) |
| 639 | fprintf(stderr, "Bad clock read\n"); |
| 640 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 641 | } |
| 642 | return qemu_icount_bias + (icount << icount_time_shift); |
| 643 | } |
| 644 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 645 | /***********************************************************/ |
| 646 | /* guest cycle counter */ |
| 647 | |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 648 | typedef struct TimersState { |
| 649 | int64_t cpu_ticks_prev; |
| 650 | int64_t cpu_ticks_offset; |
| 651 | int64_t cpu_clock_offset; |
| 652 | int32_t cpu_ticks_enabled; |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 653 | int64_t dummy; |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 654 | } TimersState; |
| 655 | |
| 656 | TimersState timers_state; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 657 | |
| 658 | /* return the host CPU cycle counter and handle stop/restart */ |
| 659 | int64_t cpu_get_ticks(void) |
| 660 | { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 661 | if (use_icount) { |
| 662 | return cpu_get_icount(); |
| 663 | } |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 664 | if (!timers_state.cpu_ticks_enabled) { |
| 665 | return timers_state.cpu_ticks_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 666 | } else { |
| 667 | int64_t ticks; |
| 668 | ticks = cpu_get_real_ticks(); |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 669 | if (timers_state.cpu_ticks_prev > ticks) { |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 670 | /* Note: non increasing ticks may happen if the host uses |
| 671 | software suspend */ |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 672 | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 673 | } |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 674 | timers_state.cpu_ticks_prev = ticks; |
| 675 | return ticks + timers_state.cpu_ticks_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 680 | static int64_t cpu_get_clock(void) |
| 681 | { |
| 682 | int64_t ti; |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 683 | if (!timers_state.cpu_ticks_enabled) { |
| 684 | return timers_state.cpu_clock_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 685 | } else { |
| 686 | ti = get_clock(); |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 687 | return ti + timers_state.cpu_clock_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | /* enable cpu_get_ticks() */ |
| 692 | void cpu_enable_ticks(void) |
| 693 | { |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 694 | if (!timers_state.cpu_ticks_enabled) { |
| 695 | timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); |
| 696 | timers_state.cpu_clock_offset -= get_clock(); |
| 697 | timers_state.cpu_ticks_enabled = 1; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
| 701 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 702 | cpu_get_ticks() after that. */ |
| 703 | void cpu_disable_ticks(void) |
| 704 | { |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 705 | if (timers_state.cpu_ticks_enabled) { |
| 706 | timers_state.cpu_ticks_offset = cpu_get_ticks(); |
| 707 | timers_state.cpu_clock_offset = cpu_get_clock(); |
| 708 | timers_state.cpu_ticks_enabled = 0; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | /***********************************************************/ |
| 713 | /* timers */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 714 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 715 | #define QEMU_CLOCK_REALTIME 0 |
| 716 | #define QEMU_CLOCK_VIRTUAL 1 |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 717 | #define QEMU_CLOCK_HOST 2 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 718 | |
| 719 | struct QEMUClock { |
| 720 | int type; |
| 721 | /* XXX: add frequency */ |
| 722 | }; |
| 723 | |
| 724 | struct QEMUTimer { |
| 725 | QEMUClock *clock; |
| 726 | int64_t expire_time; |
| 727 | QEMUTimerCB *cb; |
| 728 | void *opaque; |
| 729 | struct QEMUTimer *next; |
| 730 | }; |
| 731 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 732 | struct qemu_alarm_timer { |
| 733 | char const *name; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 734 | unsigned int flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 735 | |
| 736 | int (*start)(struct qemu_alarm_timer *t); |
| 737 | void (*stop)(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 738 | void (*rearm)(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 739 | void *priv; |
| 740 | }; |
| 741 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 742 | #define ALARM_FLAG_DYNTICKS 0x1 |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 743 | #define ALARM_FLAG_EXPIRED 0x2 |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 744 | |
| 745 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
| 746 | { |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 747 | return t && (t->flags & ALARM_FLAG_DYNTICKS); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
| 751 | { |
| 752 | if (!alarm_has_dynticks(t)) |
| 753 | return; |
| 754 | |
| 755 | t->rearm(t); |
| 756 | } |
| 757 | |
| 758 | /* TODO: MIN_TIMER_REARM_US should be optimized */ |
| 759 | #define MIN_TIMER_REARM_US 250 |
| 760 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 761 | static struct qemu_alarm_timer *alarm_timer; |
| 762 | |
| 763 | #ifdef _WIN32 |
| 764 | |
| 765 | struct qemu_alarm_win32 { |
| 766 | MMRESULT timerId; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 767 | unsigned int period; |
Blue Swirl | ef28c4b | 2009-04-25 12:56:37 +0000 | [diff] [blame] | 768 | } alarm_win32_data = {0, -1}; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 769 | |
| 770 | static int win32_start_timer(struct qemu_alarm_timer *t); |
| 771 | static void win32_stop_timer(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 772 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 773 | |
| 774 | #else |
| 775 | |
| 776 | static int unix_start_timer(struct qemu_alarm_timer *t); |
| 777 | static void unix_stop_timer(struct qemu_alarm_timer *t); |
| 778 | |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 779 | #ifdef __linux__ |
| 780 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 781 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
| 782 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
| 783 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
| 784 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 785 | static int hpet_start_timer(struct qemu_alarm_timer *t); |
| 786 | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
| 787 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 788 | static int rtc_start_timer(struct qemu_alarm_timer *t); |
| 789 | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
| 790 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 791 | #endif /* __linux__ */ |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 792 | |
| 793 | #endif /* _WIN32 */ |
| 794 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 795 | /* Correlation between real and virtual time is always going to be |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 796 | fairly approximate, so ignore small variation. |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 797 | When the guest is idle real and virtual time will be aligned in |
| 798 | the IO wait loop. */ |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 799 | #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 800 | |
| 801 | static void icount_adjust(void) |
| 802 | { |
| 803 | int64_t cur_time; |
| 804 | int64_t cur_icount; |
| 805 | int64_t delta; |
| 806 | static int64_t last_delta; |
| 807 | /* If the VM is not running, then do nothing. */ |
| 808 | if (!vm_running) |
| 809 | return; |
| 810 | |
| 811 | cur_time = cpu_get_clock(); |
| 812 | cur_icount = qemu_get_clock(vm_clock); |
| 813 | delta = cur_icount - cur_time; |
| 814 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 815 | if (delta > 0 |
| 816 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 817 | && icount_time_shift > 0) { |
| 818 | /* The guest is getting too far ahead. Slow time down. */ |
| 819 | icount_time_shift--; |
| 820 | } |
| 821 | if (delta < 0 |
| 822 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 823 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 824 | /* The guest is getting too far behind. Speed time up. */ |
| 825 | icount_time_shift++; |
| 826 | } |
| 827 | last_delta = delta; |
| 828 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
| 829 | } |
| 830 | |
| 831 | static void icount_adjust_rt(void * opaque) |
| 832 | { |
| 833 | qemu_mod_timer(icount_rt_timer, |
| 834 | qemu_get_clock(rt_clock) + 1000); |
| 835 | icount_adjust(); |
| 836 | } |
| 837 | |
| 838 | static void icount_adjust_vm(void * opaque) |
| 839 | { |
| 840 | qemu_mod_timer(icount_vm_timer, |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 841 | qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 842 | icount_adjust(); |
| 843 | } |
| 844 | |
| 845 | static void init_icount_adjust(void) |
| 846 | { |
| 847 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 848 | The realtime trigger catches emulated time passing too slowly, |
| 849 | the virtual time trigger catches emulated time passing too fast. |
| 850 | Realtime triggers occur even when idle, so use them less frequently |
| 851 | than VM triggers. */ |
| 852 | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL); |
| 853 | qemu_mod_timer(icount_rt_timer, |
| 854 | qemu_get_clock(rt_clock) + 1000); |
| 855 | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL); |
| 856 | qemu_mod_timer(icount_vm_timer, |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 857 | qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 858 | } |
| 859 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 860 | static struct qemu_alarm_timer alarm_timers[] = { |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 861 | #ifndef _WIN32 |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 862 | #ifdef __linux__ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 863 | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer, |
| 864 | dynticks_stop_timer, dynticks_rearm_timer, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 865 | /* HPET - if available - is preferred */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 866 | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 867 | /* ...otherwise try RTC */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 868 | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 869 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 870 | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 871 | #else |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 872 | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer, |
| 873 | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
| 874 | {"win32", 0, win32_start_timer, |
| 875 | win32_stop_timer, NULL, &alarm_win32_data}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 876 | #endif |
| 877 | {NULL, } |
| 878 | }; |
| 879 | |
blueswir1 | 3f47aa8 | 2008-03-09 06:59:01 +0000 | [diff] [blame] | 880 | static void show_available_alarms(void) |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 881 | { |
| 882 | int i; |
| 883 | |
| 884 | printf("Available alarm timers, in order of precedence:\n"); |
| 885 | for (i = 0; alarm_timers[i].name; i++) |
| 886 | printf("%s\n", alarm_timers[i].name); |
| 887 | } |
| 888 | |
| 889 | static void configure_alarms(char const *opt) |
| 890 | { |
| 891 | int i; |
| 892 | int cur = 0; |
malc | b1503cd | 2008-12-22 20:33:55 +0000 | [diff] [blame] | 893 | int count = ARRAY_SIZE(alarm_timers) - 1; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 894 | char *arg; |
| 895 | char *name; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 896 | struct qemu_alarm_timer tmp; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 897 | |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 898 | if (!strcmp(opt, "?")) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 899 | show_available_alarms(); |
| 900 | exit(0); |
| 901 | } |
| 902 | |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 903 | arg = qemu_strdup(opt); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 904 | |
| 905 | /* Reorder the array */ |
| 906 | name = strtok(arg, ","); |
| 907 | while (name) { |
balrog | e2b577e | 2007-09-17 21:25:20 +0000 | [diff] [blame] | 908 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 909 | if (!strcmp(alarm_timers[i].name, name)) |
| 910 | break; |
| 911 | } |
| 912 | |
| 913 | if (i == count) { |
| 914 | fprintf(stderr, "Unknown clock %s\n", name); |
| 915 | goto next; |
| 916 | } |
| 917 | |
| 918 | if (i < cur) |
| 919 | /* Ignore */ |
| 920 | goto next; |
| 921 | |
| 922 | /* Swap */ |
| 923 | tmp = alarm_timers[i]; |
| 924 | alarm_timers[i] = alarm_timers[cur]; |
| 925 | alarm_timers[cur] = tmp; |
| 926 | |
| 927 | cur++; |
| 928 | next: |
| 929 | name = strtok(NULL, ","); |
| 930 | } |
| 931 | |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 932 | qemu_free(arg); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 933 | |
| 934 | if (cur) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 935 | /* Disable remaining timers */ |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 936 | for (i = cur; i < count; i++) |
| 937 | alarm_timers[i].name = NULL; |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 938 | } else { |
| 939 | show_available_alarms(); |
| 940 | exit(1); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 941 | } |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 944 | #define QEMU_NUM_CLOCKS 3 |
| 945 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 946 | QEMUClock *rt_clock; |
| 947 | QEMUClock *vm_clock; |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 948 | QEMUClock *host_clock; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 949 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 950 | static QEMUTimer *active_timers[QEMU_NUM_CLOCKS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 951 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 952 | static QEMUClock *qemu_new_clock(int type) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 953 | { |
| 954 | QEMUClock *clock; |
| 955 | clock = qemu_mallocz(sizeof(QEMUClock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 956 | clock->type = type; |
| 957 | return clock; |
| 958 | } |
| 959 | |
| 960 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 961 | { |
| 962 | QEMUTimer *ts; |
| 963 | |
| 964 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 965 | ts->clock = clock; |
| 966 | ts->cb = cb; |
| 967 | ts->opaque = opaque; |
| 968 | return ts; |
| 969 | } |
| 970 | |
| 971 | void qemu_free_timer(QEMUTimer *ts) |
| 972 | { |
| 973 | qemu_free(ts); |
| 974 | } |
| 975 | |
| 976 | /* stop a timer, but do not dealloc it */ |
| 977 | void qemu_del_timer(QEMUTimer *ts) |
| 978 | { |
| 979 | QEMUTimer **pt, *t; |
| 980 | |
| 981 | /* NOTE: this code must be signal safe because |
| 982 | qemu_timer_expired() can be called from a signal. */ |
| 983 | pt = &active_timers[ts->clock->type]; |
| 984 | for(;;) { |
| 985 | t = *pt; |
| 986 | if (!t) |
| 987 | break; |
| 988 | if (t == ts) { |
| 989 | *pt = t->next; |
| 990 | break; |
| 991 | } |
| 992 | pt = &t->next; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /* modify the current timer so that it will be fired when current_time |
| 997 | >= expire_time. The corresponding callback will be called. */ |
| 998 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 999 | { |
| 1000 | QEMUTimer **pt, *t; |
| 1001 | |
| 1002 | qemu_del_timer(ts); |
| 1003 | |
| 1004 | /* add the timer in the sorted list */ |
| 1005 | /* NOTE: this code must be signal safe because |
| 1006 | qemu_timer_expired() can be called from a signal. */ |
| 1007 | pt = &active_timers[ts->clock->type]; |
| 1008 | for(;;) { |
| 1009 | t = *pt; |
| 1010 | if (!t) |
| 1011 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1012 | if (t->expire_time > expire_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1013 | break; |
| 1014 | pt = &t->next; |
| 1015 | } |
| 1016 | ts->expire_time = expire_time; |
| 1017 | ts->next = *pt; |
| 1018 | *pt = ts; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1019 | |
| 1020 | /* Rearm if necessary */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1021 | if (pt == &active_timers[ts->clock->type]) { |
| 1022 | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
| 1023 | qemu_rearm_alarm_timer(alarm_timer); |
| 1024 | } |
| 1025 | /* Interrupt execution to force deadline recalculation. */ |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 1026 | if (use_icount) |
| 1027 | qemu_notify_event(); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1028 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | int qemu_timer_pending(QEMUTimer *ts) |
| 1032 | { |
| 1033 | QEMUTimer *t; |
| 1034 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 1035 | if (t == ts) |
| 1036 | return 1; |
| 1037 | } |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
Stefano Stabellini | 2430ffe | 2009-08-03 10:56:01 +0100 | [diff] [blame] | 1041 | int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1042 | { |
| 1043 | if (!timer_head) |
| 1044 | return 0; |
| 1045 | return (timer_head->expire_time <= current_time); |
| 1046 | } |
| 1047 | |
| 1048 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 1049 | { |
| 1050 | QEMUTimer *ts; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1051 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1052 | for(;;) { |
| 1053 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 1054 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | /* remove timer from the list before calling the callback */ |
| 1057 | *ptimer_head = ts->next; |
| 1058 | ts->next = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1059 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1060 | /* run the callback (the timer list can be modified) */ |
| 1061 | ts->cb(ts->opaque); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | int64_t qemu_get_clock(QEMUClock *clock) |
| 1066 | { |
| 1067 | switch(clock->type) { |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1068 | case QEMU_CLOCK_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1069 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1070 | default: |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1071 | case QEMU_CLOCK_VIRTUAL: |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1072 | if (use_icount) { |
| 1073 | return cpu_get_icount(); |
| 1074 | } else { |
| 1075 | return cpu_get_clock(); |
| 1076 | } |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1077 | case QEMU_CLOCK_HOST: |
| 1078 | return get_clock_realtime(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1082 | static void init_clocks(void) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1083 | { |
| 1084 | init_get_clock(); |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1085 | rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME); |
| 1086 | vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL); |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1087 | host_clock = qemu_new_clock(QEMU_CLOCK_HOST); |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1088 | |
| 1089 | rtc_clock = host_clock; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1092 | /* save a timer */ |
| 1093 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 1094 | { |
| 1095 | uint64_t expire_time; |
| 1096 | |
| 1097 | if (qemu_timer_pending(ts)) { |
| 1098 | expire_time = ts->expire_time; |
| 1099 | } else { |
| 1100 | expire_time = -1; |
| 1101 | } |
| 1102 | qemu_put_be64(f, expire_time); |
| 1103 | } |
| 1104 | |
| 1105 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 1106 | { |
| 1107 | uint64_t expire_time; |
| 1108 | |
| 1109 | expire_time = qemu_get_be64(f); |
| 1110 | if (expire_time != -1) { |
| 1111 | qemu_mod_timer(ts, expire_time); |
| 1112 | } else { |
| 1113 | qemu_del_timer(ts); |
| 1114 | } |
| 1115 | } |
| 1116 | |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 1117 | static const VMStateDescription vmstate_timers = { |
| 1118 | .name = "timer", |
| 1119 | .version_id = 2, |
| 1120 | .minimum_version_id = 1, |
| 1121 | .minimum_version_id_old = 1, |
| 1122 | .fields = (VMStateField []) { |
| 1123 | VMSTATE_INT64(cpu_ticks_offset, TimersState), |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 1124 | VMSTATE_INT64(dummy, TimersState), |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 1125 | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2), |
| 1126 | VMSTATE_END_OF_LIST() |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1127 | } |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 1128 | }; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1129 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1130 | static void qemu_event_increment(void); |
| 1131 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1132 | #ifdef _WIN32 |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 1133 | static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 1134 | DWORD_PTR dwUser, DWORD_PTR dw1, |
| 1135 | DWORD_PTR dw2) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1136 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1137 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1138 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1139 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1140 | #if 0 |
| 1141 | #define DISP_FREQ 1000 |
| 1142 | { |
| 1143 | static int64_t delta_min = INT64_MAX; |
| 1144 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 1145 | static int count; |
| 1146 | ti = qemu_get_clock(vm_clock); |
| 1147 | if (last_clock != 0) { |
| 1148 | delta = ti - last_clock; |
| 1149 | if (delta < delta_min) |
| 1150 | delta_min = delta; |
| 1151 | if (delta > delta_max) |
| 1152 | delta_max = delta; |
| 1153 | delta_cum += delta; |
| 1154 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 1155 | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n", |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 1156 | muldiv64(delta_min, 1000000, get_ticks_per_sec()), |
| 1157 | muldiv64(delta_max, 1000000, get_ticks_per_sec()), |
| 1158 | muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()), |
| 1159 | (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ)); |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1160 | count = 0; |
| 1161 | delta_min = INT64_MAX; |
| 1162 | delta_max = 0; |
| 1163 | delta_cum = 0; |
| 1164 | } |
| 1165 | } |
| 1166 | last_clock = ti; |
| 1167 | } |
| 1168 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1169 | if (alarm_has_dynticks(alarm_timer) || |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1170 | (!use_icount && |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1171 | qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL], |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1172 | qemu_get_clock(vm_clock))) || |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1173 | qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME], |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1174 | qemu_get_clock(rt_clock)) || |
| 1175 | qemu_timer_expired(active_timers[QEMU_CLOCK_HOST], |
| 1176 | qemu_get_clock(host_clock))) { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1177 | qemu_event_increment(); |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 1178 | if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1179 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1180 | #ifndef CONFIG_IOTHREAD |
| 1181 | if (next_cpu) { |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1182 | /* stop the currently executing cpu because a timer occured */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1183 | cpu_exit(next_cpu); |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1184 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1185 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 1186 | timer_alarm_pending = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 1187 | qemu_notify_event(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1191 | static int64_t qemu_next_deadline(void) |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1192 | { |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1193 | /* To avoid problems with overflow limit this to 2^32. */ |
| 1194 | int64_t delta = INT32_MAX; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1195 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1196 | if (active_timers[QEMU_CLOCK_VIRTUAL]) { |
| 1197 | delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time - |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1198 | qemu_get_clock(vm_clock); |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1199 | } |
| 1200 | if (active_timers[QEMU_CLOCK_HOST]) { |
| 1201 | int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time - |
| 1202 | qemu_get_clock(host_clock); |
| 1203 | if (hdelta < delta) |
| 1204 | delta = hdelta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1207 | if (delta < 0) |
| 1208 | delta = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1209 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1210 | return delta; |
| 1211 | } |
| 1212 | |
Jan Kiszka | f64382b | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1213 | #if defined(__linux__) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1214 | static uint64_t qemu_next_deadline_dyntick(void) |
| 1215 | { |
| 1216 | int64_t delta; |
| 1217 | int64_t rtdelta; |
| 1218 | |
| 1219 | if (use_icount) |
| 1220 | delta = INT32_MAX; |
| 1221 | else |
| 1222 | delta = (qemu_next_deadline() + 999) / 1000; |
| 1223 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1224 | if (active_timers[QEMU_CLOCK_REALTIME]) { |
| 1225 | rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time - |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1226 | qemu_get_clock(rt_clock))*1000; |
| 1227 | if (rtdelta < delta) |
| 1228 | delta = rtdelta; |
| 1229 | } |
| 1230 | |
| 1231 | if (delta < MIN_TIMER_REARM_US) |
| 1232 | delta = MIN_TIMER_REARM_US; |
| 1233 | |
| 1234 | return delta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1235 | } |
blueswir1 | 8632fb9 | 2008-09-14 13:59:34 +0000 | [diff] [blame] | 1236 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1237 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1238 | #ifndef _WIN32 |
| 1239 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1240 | /* Sets a specific flag */ |
| 1241 | static int fcntl_setfl(int fd, int flag) |
| 1242 | { |
| 1243 | int flags; |
| 1244 | |
| 1245 | flags = fcntl(fd, F_GETFL); |
| 1246 | if (flags == -1) |
| 1247 | return -errno; |
| 1248 | |
| 1249 | if (fcntl(fd, F_SETFL, flags | flag) == -1) |
| 1250 | return -errno; |
| 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1255 | #if defined(__linux__) |
| 1256 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1257 | #define RTC_FREQ 1024 |
| 1258 | |
aurel32 | de9a95f | 2008-11-11 13:41:01 +0000 | [diff] [blame] | 1259 | static void enable_sigio_timer(int fd) |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1260 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1261 | struct sigaction act; |
| 1262 | |
| 1263 | /* timer signal */ |
| 1264 | sigfillset(&act.sa_mask); |
| 1265 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1266 | act.sa_handler = host_alarm_handler; |
| 1267 | |
| 1268 | sigaction(SIGIO, &act, NULL); |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1269 | fcntl_setfl(fd, O_ASYNC); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1270 | fcntl(fd, F_SETOWN, getpid()); |
| 1271 | } |
| 1272 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1273 | static int hpet_start_timer(struct qemu_alarm_timer *t) |
| 1274 | { |
| 1275 | struct hpet_info info; |
| 1276 | int r, fd; |
| 1277 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1278 | fd = qemu_open("/dev/hpet", O_RDONLY); |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1279 | if (fd < 0) |
| 1280 | return -1; |
| 1281 | |
| 1282 | /* Set frequency */ |
| 1283 | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
| 1284 | if (r < 0) { |
| 1285 | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n" |
| 1286 | "error, but for better emulation accuracy type:\n" |
| 1287 | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n"); |
| 1288 | goto fail; |
| 1289 | } |
| 1290 | |
| 1291 | /* Check capabilities */ |
| 1292 | r = ioctl(fd, HPET_INFO, &info); |
| 1293 | if (r < 0) |
| 1294 | goto fail; |
| 1295 | |
| 1296 | /* Enable periodic mode */ |
| 1297 | r = ioctl(fd, HPET_EPI, 0); |
| 1298 | if (info.hi_flags && (r < 0)) |
| 1299 | goto fail; |
| 1300 | |
| 1301 | /* Enable interrupt */ |
| 1302 | r = ioctl(fd, HPET_IE_ON, 0); |
| 1303 | if (r < 0) |
| 1304 | goto fail; |
| 1305 | |
| 1306 | enable_sigio_timer(fd); |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1307 | t->priv = (void *)(long)fd; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1308 | |
| 1309 | return 0; |
| 1310 | fail: |
| 1311 | close(fd); |
| 1312 | return -1; |
| 1313 | } |
| 1314 | |
| 1315 | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
| 1316 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1317 | int fd = (long)t->priv; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1318 | |
| 1319 | close(fd); |
| 1320 | } |
| 1321 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1322 | static int rtc_start_timer(struct qemu_alarm_timer *t) |
| 1323 | { |
| 1324 | int rtc_fd; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1325 | unsigned long current_rtc_freq = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1326 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1327 | TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY)); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1328 | if (rtc_fd < 0) |
| 1329 | return -1; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1330 | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
| 1331 | if (current_rtc_freq != RTC_FREQ && |
| 1332 | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1333 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1334 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1335 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1336 | goto fail; |
| 1337 | } |
| 1338 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1339 | fail: |
| 1340 | close(rtc_fd); |
| 1341 | return -1; |
| 1342 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1343 | |
| 1344 | enable_sigio_timer(rtc_fd); |
| 1345 | |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1346 | t->priv = (void *)(long)rtc_fd; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1347 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1348 | return 0; |
| 1349 | } |
| 1350 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1351 | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1352 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1353 | int rtc_fd = (long)t->priv; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1354 | |
| 1355 | close(rtc_fd); |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1358 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
| 1359 | { |
| 1360 | struct sigevent ev; |
| 1361 | timer_t host_timer; |
| 1362 | struct sigaction act; |
| 1363 | |
| 1364 | sigfillset(&act.sa_mask); |
| 1365 | act.sa_flags = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1366 | act.sa_handler = host_alarm_handler; |
| 1367 | |
| 1368 | sigaction(SIGALRM, &act, NULL); |
| 1369 | |
Jean-Christophe Dubois | 9ed415b | 2009-05-17 18:41:16 +0200 | [diff] [blame] | 1370 | /* |
| 1371 | * Initialize ev struct to 0 to avoid valgrind complaining |
| 1372 | * about uninitialized data in timer_create call |
| 1373 | */ |
| 1374 | memset(&ev, 0, sizeof(ev)); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1375 | ev.sigev_value.sival_int = 0; |
| 1376 | ev.sigev_notify = SIGEV_SIGNAL; |
| 1377 | ev.sigev_signo = SIGALRM; |
| 1378 | |
| 1379 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { |
| 1380 | perror("timer_create"); |
| 1381 | |
| 1382 | /* disable dynticks */ |
| 1383 | fprintf(stderr, "Dynamic Ticks disabled\n"); |
| 1384 | |
| 1385 | return -1; |
| 1386 | } |
| 1387 | |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1388 | t->priv = (void *)(long)host_timer; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1389 | |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
| 1394 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1395 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1396 | |
| 1397 | timer_delete(host_timer); |
| 1398 | } |
| 1399 | |
| 1400 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
| 1401 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1402 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1403 | struct itimerspec timeout; |
| 1404 | int64_t nearest_delta_us = INT64_MAX; |
| 1405 | int64_t current_us; |
| 1406 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1407 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1408 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 1409 | !active_timers[QEMU_CLOCK_HOST]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1410 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1411 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1412 | nearest_delta_us = qemu_next_deadline_dyntick(); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1413 | |
| 1414 | /* check whether a timer is already running */ |
| 1415 | if (timer_gettime(host_timer, &timeout)) { |
| 1416 | perror("gettime"); |
| 1417 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1418 | exit(1); |
| 1419 | } |
| 1420 | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
| 1421 | if (current_us && current_us <= nearest_delta_us) |
| 1422 | return; |
| 1423 | |
| 1424 | timeout.it_interval.tv_sec = 0; |
| 1425 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
| 1426 | timeout.it_value.tv_sec = nearest_delta_us / 1000000; |
| 1427 | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
| 1428 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
| 1429 | perror("settime"); |
| 1430 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1431 | exit(1); |
| 1432 | } |
| 1433 | } |
| 1434 | |
ths | 70744b3 | 2007-08-26 17:31:30 +0000 | [diff] [blame] | 1435 | #endif /* defined(__linux__) */ |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 1436 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1437 | static int unix_start_timer(struct qemu_alarm_timer *t) |
| 1438 | { |
| 1439 | struct sigaction act; |
| 1440 | struct itimerval itv; |
| 1441 | int err; |
| 1442 | |
| 1443 | /* timer signal */ |
| 1444 | sigfillset(&act.sa_mask); |
| 1445 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1446 | act.sa_handler = host_alarm_handler; |
| 1447 | |
| 1448 | sigaction(SIGALRM, &act, NULL); |
| 1449 | |
| 1450 | itv.it_interval.tv_sec = 0; |
| 1451 | /* for i386 kernel 2.6 to get 1 ms */ |
| 1452 | itv.it_interval.tv_usec = 999; |
| 1453 | itv.it_value.tv_sec = 0; |
| 1454 | itv.it_value.tv_usec = 10 * 1000; |
| 1455 | |
| 1456 | err = setitimer(ITIMER_REAL, &itv, NULL); |
| 1457 | if (err) |
| 1458 | return -1; |
| 1459 | |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | static void unix_stop_timer(struct qemu_alarm_timer *t) |
| 1464 | { |
| 1465 | struct itimerval itv; |
| 1466 | |
| 1467 | memset(&itv, 0, sizeof(itv)); |
| 1468 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1469 | } |
| 1470 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1471 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1472 | |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1473 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1474 | #ifdef _WIN32 |
| 1475 | |
| 1476 | static int win32_start_timer(struct qemu_alarm_timer *t) |
| 1477 | { |
| 1478 | TIMECAPS tc; |
| 1479 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1480 | UINT flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1481 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1482 | memset(&tc, 0, sizeof(tc)); |
| 1483 | timeGetDevCaps(&tc, sizeof(tc)); |
| 1484 | |
| 1485 | if (data->period < tc.wPeriodMin) |
| 1486 | data->period = tc.wPeriodMin; |
| 1487 | |
| 1488 | timeBeginPeriod(data->period); |
| 1489 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1490 | flags = TIME_CALLBACK_FUNCTION; |
| 1491 | if (alarm_has_dynticks(t)) |
| 1492 | flags |= TIME_ONESHOT; |
| 1493 | else |
| 1494 | flags |= TIME_PERIODIC; |
| 1495 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1496 | data->timerId = timeSetEvent(1, // interval (ms) |
| 1497 | data->period, // resolution |
| 1498 | host_alarm_handler, // function |
| 1499 | (DWORD)t, // parameter |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1500 | flags); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1501 | |
| 1502 | if (!data->timerId) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 1503 | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n", |
malc | 705e83f | 2009-09-27 14:30:33 +0400 | [diff] [blame] | 1504 | GetLastError()); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1505 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1506 | return -1; |
| 1507 | } |
| 1508 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1509 | return 0; |
| 1510 | } |
| 1511 | |
| 1512 | static void win32_stop_timer(struct qemu_alarm_timer *t) |
| 1513 | { |
| 1514 | struct qemu_alarm_win32 *data = t->priv; |
| 1515 | |
| 1516 | timeKillEvent(data->timerId); |
| 1517 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1520 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
| 1521 | { |
| 1522 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1523 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1524 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1525 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 1526 | !active_timers[QEMU_CLOCK_HOST]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1527 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1528 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1529 | timeKillEvent(data->timerId); |
| 1530 | |
| 1531 | data->timerId = timeSetEvent(1, |
| 1532 | data->period, |
| 1533 | host_alarm_handler, |
| 1534 | (DWORD)t, |
| 1535 | TIME_ONESHOT | TIME_PERIODIC); |
| 1536 | |
| 1537 | if (!data->timerId) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 1538 | fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n", |
malc | 705e83f | 2009-09-27 14:30:33 +0400 | [diff] [blame] | 1539 | GetLastError()); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1540 | |
| 1541 | timeEndPeriod(data->period); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1542 | exit(1); |
| 1543 | } |
| 1544 | } |
| 1545 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1546 | #endif /* _WIN32 */ |
| 1547 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1548 | static int init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1549 | { |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 1550 | struct qemu_alarm_timer *t = NULL; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1551 | int i, err = -1; |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1552 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1553 | for (i = 0; alarm_timers[i].name; i++) { |
| 1554 | t = &alarm_timers[i]; |
| 1555 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1556 | err = t->start(t); |
| 1557 | if (!err) |
| 1558 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1559 | } |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1560 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1561 | if (err) { |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1562 | err = -ENOENT; |
| 1563 | goto fail; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1564 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1565 | |
| 1566 | alarm_timer = t; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1567 | |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 1568 | return 0; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1569 | |
| 1570 | fail: |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1571 | return err; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1574 | static void quit_timers(void) |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1575 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1576 | alarm_timer->stop(alarm_timer); |
| 1577 | alarm_timer = NULL; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1580 | /***********************************************************/ |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 1581 | /* host time/date access */ |
| 1582 | void qemu_get_timedate(struct tm *tm, int offset) |
| 1583 | { |
| 1584 | time_t ti; |
| 1585 | struct tm *ret; |
| 1586 | |
| 1587 | time(&ti); |
| 1588 | ti += offset; |
| 1589 | if (rtc_date_offset == -1) { |
| 1590 | if (rtc_utc) |
| 1591 | ret = gmtime(&ti); |
| 1592 | else |
| 1593 | ret = localtime(&ti); |
| 1594 | } else { |
| 1595 | ti -= rtc_date_offset; |
| 1596 | ret = gmtime(&ti); |
| 1597 | } |
| 1598 | |
| 1599 | memcpy(tm, ret, sizeof(struct tm)); |
| 1600 | } |
| 1601 | |
| 1602 | int qemu_timedate_diff(struct tm *tm) |
| 1603 | { |
| 1604 | time_t seconds; |
| 1605 | |
| 1606 | if (rtc_date_offset == -1) |
| 1607 | if (rtc_utc) |
| 1608 | seconds = mktimegm(tm); |
| 1609 | else |
| 1610 | seconds = mktime(tm); |
| 1611 | else |
| 1612 | seconds = mktimegm(tm) + rtc_date_offset; |
| 1613 | |
| 1614 | return seconds - time(NULL); |
| 1615 | } |
| 1616 | |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1617 | static void configure_rtc_date_offset(const char *startdate, int legacy) |
| 1618 | { |
| 1619 | time_t rtc_start_date; |
| 1620 | struct tm tm; |
| 1621 | |
| 1622 | if (!strcmp(startdate, "now") && legacy) { |
| 1623 | rtc_date_offset = -1; |
| 1624 | } else { |
| 1625 | if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", |
| 1626 | &tm.tm_year, |
| 1627 | &tm.tm_mon, |
| 1628 | &tm.tm_mday, |
| 1629 | &tm.tm_hour, |
| 1630 | &tm.tm_min, |
| 1631 | &tm.tm_sec) == 6) { |
| 1632 | /* OK */ |
| 1633 | } else if (sscanf(startdate, "%d-%d-%d", |
| 1634 | &tm.tm_year, |
| 1635 | &tm.tm_mon, |
| 1636 | &tm.tm_mday) == 3) { |
| 1637 | tm.tm_hour = 0; |
| 1638 | tm.tm_min = 0; |
| 1639 | tm.tm_sec = 0; |
| 1640 | } else { |
| 1641 | goto date_fail; |
| 1642 | } |
| 1643 | tm.tm_year -= 1900; |
| 1644 | tm.tm_mon--; |
| 1645 | rtc_start_date = mktimegm(&tm); |
| 1646 | if (rtc_start_date == -1) { |
| 1647 | date_fail: |
| 1648 | fprintf(stderr, "Invalid date format. Valid formats are:\n" |
| 1649 | "'2006-06-17T16:01:21' or '2006-06-17'\n"); |
| 1650 | exit(1); |
| 1651 | } |
| 1652 | rtc_date_offset = time(NULL) - rtc_start_date; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | static void configure_rtc(QemuOpts *opts) |
| 1657 | { |
| 1658 | const char *value; |
| 1659 | |
| 1660 | value = qemu_opt_get(opts, "base"); |
| 1661 | if (value) { |
| 1662 | if (!strcmp(value, "utc")) { |
| 1663 | rtc_utc = 1; |
| 1664 | } else if (!strcmp(value, "localtime")) { |
| 1665 | rtc_utc = 0; |
| 1666 | } else { |
| 1667 | configure_rtc_date_offset(value, 0); |
| 1668 | } |
| 1669 | } |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1670 | value = qemu_opt_get(opts, "clock"); |
| 1671 | if (value) { |
| 1672 | if (!strcmp(value, "host")) { |
| 1673 | rtc_clock = host_clock; |
| 1674 | } else if (!strcmp(value, "vm")) { |
| 1675 | rtc_clock = vm_clock; |
| 1676 | } else { |
| 1677 | fprintf(stderr, "qemu: invalid option value '%s'\n", value); |
| 1678 | exit(1); |
| 1679 | } |
| 1680 | } |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1681 | #ifdef CONFIG_TARGET_I386 |
| 1682 | value = qemu_opt_get(opts, "driftfix"); |
| 1683 | if (value) { |
| 1684 | if (!strcmp(buf, "slew")) { |
| 1685 | rtc_td_hack = 1; |
| 1686 | } else if (!strcmp(buf, "none")) { |
| 1687 | rtc_td_hack = 0; |
| 1688 | } else { |
| 1689 | fprintf(stderr, "qemu: invalid option value '%s'\n", value); |
| 1690 | exit(1); |
| 1691 | } |
| 1692 | } |
| 1693 | #endif |
| 1694 | } |
| 1695 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1696 | #ifdef _WIN32 |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1697 | static void socket_cleanup(void) |
| 1698 | { |
| 1699 | WSACleanup(); |
| 1700 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1701 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1702 | static int socket_init(void) |
| 1703 | { |
| 1704 | WSADATA Data; |
| 1705 | int ret, err; |
| 1706 | |
| 1707 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1708 | if (ret != 0) { |
| 1709 | err = WSAGetLastError(); |
| 1710 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1711 | return -1; |
| 1712 | } |
| 1713 | atexit(socket_cleanup); |
| 1714 | return 0; |
| 1715 | } |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 1716 | #endif |
| 1717 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1718 | /***********************************************************/ |
| 1719 | /* Bluetooth support */ |
| 1720 | static int nb_hcis; |
| 1721 | static int cur_hci; |
| 1722 | static struct HCIInfo *hci_table[MAX_NICS]; |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1723 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1724 | static struct bt_vlan_s { |
| 1725 | struct bt_scatternet_s net; |
| 1726 | int id; |
| 1727 | struct bt_vlan_s *next; |
| 1728 | } *first_bt_vlan; |
| 1729 | |
| 1730 | /* find or alloc a new bluetooth "VLAN" */ |
blueswir1 | 674bb26 | 2008-09-30 18:18:27 +0000 | [diff] [blame] | 1731 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1732 | { |
| 1733 | struct bt_vlan_s **pvlan, *vlan; |
| 1734 | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { |
| 1735 | if (vlan->id == id) |
| 1736 | return &vlan->net; |
| 1737 | } |
| 1738 | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); |
| 1739 | vlan->id = id; |
| 1740 | pvlan = &first_bt_vlan; |
| 1741 | while (*pvlan != NULL) |
| 1742 | pvlan = &(*pvlan)->next; |
| 1743 | *pvlan = vlan; |
| 1744 | return &vlan->net; |
| 1745 | } |
| 1746 | |
| 1747 | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) |
| 1748 | { |
| 1749 | } |
| 1750 | |
| 1751 | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
| 1752 | { |
| 1753 | return -ENOTSUP; |
| 1754 | } |
| 1755 | |
| 1756 | static struct HCIInfo null_hci = { |
| 1757 | .cmd_send = null_hci_send, |
| 1758 | .sco_send = null_hci_send, |
| 1759 | .acl_send = null_hci_send, |
| 1760 | .bdaddr_set = null_hci_addr_set, |
| 1761 | }; |
| 1762 | |
| 1763 | struct HCIInfo *qemu_next_hci(void) |
| 1764 | { |
| 1765 | if (cur_hci == nb_hcis) |
| 1766 | return &null_hci; |
| 1767 | |
| 1768 | return hci_table[cur_hci++]; |
| 1769 | } |
| 1770 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1771 | static struct HCIInfo *hci_init(const char *str) |
| 1772 | { |
| 1773 | char *endp; |
| 1774 | struct bt_scatternet_s *vlan = 0; |
| 1775 | |
| 1776 | if (!strcmp(str, "null")) |
| 1777 | /* null */ |
| 1778 | return &null_hci; |
| 1779 | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) |
| 1780 | /* host[:hciN] */ |
| 1781 | return bt_host_hci(str[4] ? str + 5 : "hci0"); |
| 1782 | else if (!strncmp(str, "hci", 3)) { |
| 1783 | /* hci[,vlan=n] */ |
| 1784 | if (str[3]) { |
| 1785 | if (!strncmp(str + 3, ",vlan=", 6)) { |
| 1786 | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); |
| 1787 | if (*endp) |
| 1788 | vlan = 0; |
| 1789 | } |
| 1790 | } else |
| 1791 | vlan = qemu_find_bt_vlan(0); |
| 1792 | if (vlan) |
| 1793 | return bt_new_hci(vlan); |
| 1794 | } |
| 1795 | |
| 1796 | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str); |
| 1797 | |
| 1798 | return 0; |
| 1799 | } |
| 1800 | |
| 1801 | static int bt_hci_parse(const char *str) |
| 1802 | { |
| 1803 | struct HCIInfo *hci; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1804 | bdaddr_t bdaddr; |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1805 | |
| 1806 | if (nb_hcis >= MAX_NICS) { |
| 1807 | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS); |
| 1808 | return -1; |
| 1809 | } |
| 1810 | |
| 1811 | hci = hci_init(str); |
| 1812 | if (!hci) |
| 1813 | return -1; |
| 1814 | |
| 1815 | bdaddr.b[0] = 0x52; |
| 1816 | bdaddr.b[1] = 0x54; |
| 1817 | bdaddr.b[2] = 0x00; |
| 1818 | bdaddr.b[3] = 0x12; |
| 1819 | bdaddr.b[4] = 0x34; |
| 1820 | bdaddr.b[5] = 0x56 + nb_hcis; |
| 1821 | hci->bdaddr_set(hci, bdaddr.b); |
| 1822 | |
| 1823 | hci_table[nb_hcis++] = hci; |
| 1824 | |
| 1825 | return 0; |
| 1826 | } |
| 1827 | |
| 1828 | static void bt_vhci_add(int vlan_id) |
| 1829 | { |
| 1830 | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id); |
| 1831 | |
| 1832 | if (!vlan->slave) |
| 1833 | fprintf(stderr, "qemu: warning: adding a VHCI to " |
| 1834 | "an empty scatternet %i\n", vlan_id); |
| 1835 | |
| 1836 | bt_vhci_init(bt_new_hci(vlan)); |
| 1837 | } |
| 1838 | |
| 1839 | static struct bt_device_s *bt_device_add(const char *opt) |
| 1840 | { |
| 1841 | struct bt_scatternet_s *vlan; |
| 1842 | int vlan_id = 0; |
| 1843 | char *endp = strstr(opt, ",vlan="); |
| 1844 | int len = (endp ? endp - opt : strlen(opt)) + 1; |
| 1845 | char devname[10]; |
| 1846 | |
| 1847 | pstrcpy(devname, MIN(sizeof(devname), len), opt); |
| 1848 | |
| 1849 | if (endp) { |
| 1850 | vlan_id = strtol(endp + 6, &endp, 0); |
| 1851 | if (*endp) { |
| 1852 | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n"); |
| 1853 | return 0; |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | vlan = qemu_find_bt_vlan(vlan_id); |
| 1858 | |
| 1859 | if (!vlan->slave) |
| 1860 | fprintf(stderr, "qemu: warning: adding a slave device to " |
| 1861 | "an empty scatternet %i\n", vlan_id); |
| 1862 | |
| 1863 | if (!strcmp(devname, "keyboard")) |
| 1864 | return bt_keyboard_init(vlan); |
| 1865 | |
| 1866 | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname); |
| 1867 | return 0; |
| 1868 | } |
| 1869 | |
| 1870 | static int bt_parse(const char *opt) |
| 1871 | { |
| 1872 | const char *endp, *p; |
| 1873 | int vlan; |
| 1874 | |
| 1875 | if (strstart(opt, "hci", &endp)) { |
| 1876 | if (!*endp || *endp == ',') { |
| 1877 | if (*endp) |
| 1878 | if (!strstart(endp, ",vlan=", 0)) |
| 1879 | opt = endp + 1; |
| 1880 | |
| 1881 | return bt_hci_parse(opt); |
| 1882 | } |
| 1883 | } else if (strstart(opt, "vhci", &endp)) { |
| 1884 | if (!*endp || *endp == ',') { |
| 1885 | if (*endp) { |
| 1886 | if (strstart(endp, ",vlan=", &p)) { |
| 1887 | vlan = strtol(p, (char **) &endp, 0); |
| 1888 | if (*endp) { |
| 1889 | fprintf(stderr, "qemu: bad scatternet '%s'\n", p); |
| 1890 | return 1; |
| 1891 | } |
| 1892 | } else { |
| 1893 | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); |
| 1894 | return 1; |
| 1895 | } |
| 1896 | } else |
| 1897 | vlan = 0; |
| 1898 | |
| 1899 | bt_vhci_add(vlan); |
| 1900 | return 0; |
| 1901 | } |
| 1902 | } else if (strstart(opt, "device:", &endp)) |
| 1903 | return !bt_device_add(endp); |
| 1904 | |
| 1905 | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt); |
| 1906 | return 1; |
| 1907 | } |
| 1908 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1909 | /***********************************************************/ |
| 1910 | /* QEMU Block devices */ |
| 1911 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1912 | #define HD_ALIAS "index=%d,media=disk" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1913 | #define CDROM_ALIAS "index=2,media=cdrom" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1914 | #define FD_ALIAS "index=%d,if=floppy" |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1915 | #define PFLASH_ALIAS "if=pflash" |
| 1916 | #define MTD_ALIAS "if=mtd" |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 1917 | #define SD_ALIAS "index=0,if=sd" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1918 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1919 | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1920 | { |
| 1921 | va_list ap; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1922 | char optstr[1024]; |
| 1923 | QemuOpts *opts; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1924 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1925 | va_start(ap, fmt); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1926 | vsnprintf(optstr, sizeof(optstr), fmt, ap); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1927 | va_end(ap); |
| 1928 | |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 1929 | opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1930 | if (!opts) { |
| 1931 | fprintf(stderr, "%s: huh? duplicate? (%s)\n", |
| 1932 | __FUNCTION__, optstr); |
| 1933 | return NULL; |
| 1934 | } |
| 1935 | if (file) |
| 1936 | qemu_opt_set(opts, "file", file); |
| 1937 | return opts; |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1940 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1941 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1942 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1943 | |
| 1944 | /* seek interface, bus and unit */ |
| 1945 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1946 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1947 | if (dinfo->type == type && |
| 1948 | dinfo->bus == bus && |
| 1949 | dinfo->unit == unit) |
| 1950 | return dinfo; |
| 1951 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1952 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1953 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Gerd Hoffmann | 2e810b3 | 2009-07-31 12:25:38 +0200 | [diff] [blame] | 1956 | DriveInfo *drive_get_by_id(const char *id) |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 1957 | { |
| 1958 | DriveInfo *dinfo; |
| 1959 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1960 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 1961 | if (strcmp(id, dinfo->id)) |
| 1962 | continue; |
| 1963 | return dinfo; |
| 1964 | } |
| 1965 | return NULL; |
| 1966 | } |
| 1967 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 1968 | int drive_get_max_bus(BlockInterfaceType type) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1969 | { |
| 1970 | int max_bus; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1971 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1972 | |
| 1973 | max_bus = -1; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1974 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1975 | if(dinfo->type == type && |
| 1976 | dinfo->bus > max_bus) |
| 1977 | max_bus = dinfo->bus; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1978 | } |
| 1979 | return max_bus; |
| 1980 | } |
| 1981 | |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1982 | const char *drive_get_serial(BlockDriverState *bdrv) |
| 1983 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1984 | DriveInfo *dinfo; |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1985 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1986 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1987 | if (dinfo->bdrv == bdrv) |
| 1988 | return dinfo->serial; |
| 1989 | } |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 1990 | |
| 1991 | return "\0"; |
| 1992 | } |
| 1993 | |
Kevin Wolf | f785009 | 2009-11-27 13:25:36 +0100 | [diff] [blame] | 1994 | BlockInterfaceErrorAction drive_get_on_error( |
| 1995 | BlockDriverState *bdrv, int is_read) |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1996 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1997 | DriveInfo *dinfo; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 1998 | |
Kevin Wolf | fc072ec | 2009-11-27 13:25:36 +0100 | [diff] [blame] | 1999 | if (is_read) { |
| 2000 | return BLOCK_ERR_REPORT; |
| 2001 | } |
| 2002 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2003 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2004 | if (dinfo->bdrv == bdrv) |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2005 | return is_read ? dinfo->on_read_error : dinfo->on_write_error; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2006 | } |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2007 | |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2008 | return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2011 | static void bdrv_format_print(void *opaque, const char *name) |
| 2012 | { |
| 2013 | fprintf(stderr, " %s", name); |
| 2014 | } |
| 2015 | |
Gerd Hoffmann | 56a1493 | 2009-09-25 21:42:46 +0200 | [diff] [blame] | 2016 | void drive_uninit(DriveInfo *dinfo) |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 2017 | { |
Gerd Hoffmann | 56a1493 | 2009-09-25 21:42:46 +0200 | [diff] [blame] | 2018 | qemu_opts_del(dinfo->opts); |
| 2019 | bdrv_delete(dinfo->bdrv); |
| 2020 | QTAILQ_REMOVE(&drives, dinfo, next); |
| 2021 | qemu_free(dinfo); |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2024 | static int parse_block_error_action(const char *buf, int is_read) |
| 2025 | { |
| 2026 | if (!strcmp(buf, "ignore")) { |
| 2027 | return BLOCK_ERR_IGNORE; |
| 2028 | } else if (!is_read && !strcmp(buf, "enospc")) { |
| 2029 | return BLOCK_ERR_STOP_ENOSPC; |
| 2030 | } else if (!strcmp(buf, "stop")) { |
| 2031 | return BLOCK_ERR_STOP_ANY; |
| 2032 | } else if (!strcmp(buf, "report")) { |
| 2033 | return BLOCK_ERR_REPORT; |
| 2034 | } else { |
| 2035 | fprintf(stderr, "qemu: '%s' invalid %s error action\n", |
| 2036 | buf, is_read ? "read" : "write"); |
| 2037 | return -1; |
| 2038 | } |
| 2039 | } |
| 2040 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2041 | DriveInfo *drive_init(QemuOpts *opts, void *opaque, |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2042 | int *fatal_error) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2043 | { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2044 | const char *buf; |
| 2045 | const char *file = NULL; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 2046 | char devname[128]; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2047 | const char *serial; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 2048 | const char *mediastr = ""; |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2049 | BlockInterfaceType type; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2050 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
| 2051 | int bus_id, unit_id; |
| 2052 | int cyls, heads, secs, translation; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2053 | BlockDriver *drv = NULL; |
aliguori | 4d73cd3 | 2009-02-11 15:20:46 +0000 | [diff] [blame] | 2054 | QEMUMachine *machine = opaque; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2055 | int max_devs; |
| 2056 | int index; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2057 | int cache; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2058 | int aio = 0; |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2059 | int ro = 0; |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2060 | int bdrv_flags; |
| 2061 | int on_read_error, on_write_error; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2062 | const char *devaddr; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2063 | DriveInfo *dinfo; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2064 | int snapshot = 0; |
| 2065 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2066 | *fatal_error = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2067 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2068 | translation = BIOS_ATA_TRANSLATION_AUTO; |
Kevin Wolf | 0aa217e | 2009-06-30 13:06:04 +0200 | [diff] [blame] | 2069 | cache = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2070 | |
Gerd Hoffmann | 4d00781 | 2009-08-31 14:23:57 +0200 | [diff] [blame] | 2071 | if (machine && machine->use_scsi) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2072 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2073 | max_devs = MAX_SCSI_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 2074 | pstrcpy(devname, sizeof(devname), "scsi"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2075 | } else { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2076 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2077 | max_devs = MAX_IDE_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 2078 | pstrcpy(devname, sizeof(devname), "ide"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2079 | } |
| 2080 | media = MEDIA_DISK; |
| 2081 | |
| 2082 | /* extract parameters */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2083 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
| 2084 | unit_id = qemu_opt_get_number(opts, "unit", -1); |
| 2085 | index = qemu_opt_get_number(opts, "index", -1); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2086 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2087 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
| 2088 | heads = qemu_opt_get_number(opts, "heads", 0); |
| 2089 | secs = qemu_opt_get_number(opts, "secs", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2090 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2091 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2092 | ro = qemu_opt_get_bool(opts, "readonly", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2093 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2094 | file = qemu_opt_get(opts, "file"); |
| 2095 | serial = qemu_opt_get(opts, "serial"); |
| 2096 | |
| 2097 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
bellard | ae45d36 | 2008-06-11 09:44:44 +0000 | [diff] [blame] | 2098 | pstrcpy(devname, sizeof(devname), buf); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2099 | if (!strcmp(buf, "ide")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2100 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2101 | max_devs = MAX_IDE_DEVS; |
| 2102 | } else if (!strcmp(buf, "scsi")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2103 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2104 | max_devs = MAX_SCSI_DEVS; |
| 2105 | } else if (!strcmp(buf, "floppy")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2106 | type = IF_FLOPPY; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2107 | max_devs = 0; |
| 2108 | } else if (!strcmp(buf, "pflash")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2109 | type = IF_PFLASH; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2110 | max_devs = 0; |
| 2111 | } else if (!strcmp(buf, "mtd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2112 | type = IF_MTD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2113 | max_devs = 0; |
| 2114 | } else if (!strcmp(buf, "sd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2115 | type = IF_SD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2116 | max_devs = 0; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 2117 | } else if (!strcmp(buf, "virtio")) { |
| 2118 | type = IF_VIRTIO; |
| 2119 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 2120 | } else if (!strcmp(buf, "xen")) { |
| 2121 | type = IF_XEN; |
| 2122 | max_devs = 0; |
Gerd Hoffmann | a8659e9 | 2009-07-31 12:25:39 +0200 | [diff] [blame] | 2123 | } else if (!strcmp(buf, "none")) { |
| 2124 | type = IF_NONE; |
| 2125 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 2126 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2127 | fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2128 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2129 | } |
| 2130 | } |
| 2131 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2132 | if (cyls || heads || secs) { |
Blue Swirl | 5afe3f0 | 2009-10-17 09:08:47 +0000 | [diff] [blame] | 2133 | if (cyls < 1 || (type == IF_IDE && cyls > 16383)) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2134 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2135 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2136 | } |
Blue Swirl | 5afe3f0 | 2009-10-17 09:08:47 +0000 | [diff] [blame] | 2137 | if (heads < 1 || (type == IF_IDE && heads > 16)) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2138 | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2139 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2140 | } |
Blue Swirl | 5afe3f0 | 2009-10-17 09:08:47 +0000 | [diff] [blame] | 2141 | if (secs < 1 || (type == IF_IDE && secs > 63)) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2142 | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2143 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2144 | } |
| 2145 | } |
| 2146 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2147 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2148 | if (!cyls) { |
| 2149 | fprintf(stderr, |
| 2150 | "qemu: '%s' trans must be used with cyls,heads and secs\n", |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2151 | buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2152 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2153 | } |
| 2154 | if (!strcmp(buf, "none")) |
| 2155 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 2156 | else if (!strcmp(buf, "lba")) |
| 2157 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 2158 | else if (!strcmp(buf, "auto")) |
| 2159 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 2160 | else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2161 | fprintf(stderr, "qemu: '%s' invalid translation type\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2162 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2166 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2167 | if (!strcmp(buf, "disk")) { |
| 2168 | media = MEDIA_DISK; |
| 2169 | } else if (!strcmp(buf, "cdrom")) { |
| 2170 | if (cyls || secs || heads) { |
| 2171 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2172 | "qemu: '%s' invalid physical CHS format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2173 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2174 | } |
| 2175 | media = MEDIA_CDROM; |
| 2176 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2177 | fprintf(stderr, "qemu: '%s' invalid media\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2178 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2179 | } |
| 2180 | } |
| 2181 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2182 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2183 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2184 | cache = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2185 | else if (!strcmp(buf, "writethrough")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2186 | cache = 1; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2187 | else if (!strcmp(buf, "writeback")) |
| 2188 | cache = 2; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2189 | else { |
| 2190 | fprintf(stderr, "qemu: invalid cache option\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2191 | return NULL; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2192 | } |
| 2193 | } |
| 2194 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2195 | #ifdef CONFIG_LINUX_AIO |
| 2196 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
| 2197 | if (!strcmp(buf, "threads")) |
| 2198 | aio = 0; |
| 2199 | else if (!strcmp(buf, "native")) |
| 2200 | aio = 1; |
| 2201 | else { |
| 2202 | fprintf(stderr, "qemu: invalid aio option\n"); |
| 2203 | return NULL; |
| 2204 | } |
| 2205 | } |
| 2206 | #endif |
| 2207 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2208 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2209 | if (strcmp(buf, "?") == 0) { |
| 2210 | fprintf(stderr, "qemu: Supported formats:"); |
| 2211 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 2212 | fprintf(stderr, "\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2213 | return NULL; |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2214 | } |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 2215 | drv = bdrv_find_whitelisted_format(buf); |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2216 | if (!drv) { |
| 2217 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2218 | return NULL; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2219 | } |
| 2220 | } |
| 2221 | |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2222 | on_write_error = BLOCK_ERR_STOP_ENOSPC; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2223 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 2224 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 2225 | fprintf(stderr, "werror is no supported by this format\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2226 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2227 | } |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2228 | |
| 2229 | on_write_error = parse_block_error_action(buf, 0); |
| 2230 | if (on_write_error < 0) { |
| 2231 | return NULL; |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | on_read_error = BLOCK_ERR_REPORT; |
| 2236 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
Kevin Wolf | f35d68f | 2009-11-27 13:25:39 +0100 | [diff] [blame] | 2237 | if (type != IF_IDE && type != IF_VIRTIO) { |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2238 | fprintf(stderr, "rerror is no supported by this format\n"); |
| 2239 | return NULL; |
| 2240 | } |
| 2241 | |
| 2242 | on_read_error = parse_block_error_action(buf, 1); |
| 2243 | if (on_read_error < 0) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2244 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2245 | } |
| 2246 | } |
| 2247 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2248 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2249 | if (type != IF_VIRTIO) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2250 | fprintf(stderr, "addr is not supported\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2251 | return NULL; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2252 | } |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2253 | } |
| 2254 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2255 | /* compute bus and unit according index */ |
| 2256 | |
| 2257 | if (index != -1) { |
| 2258 | if (bus_id != 0 || unit_id != -1) { |
| 2259 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2260 | "qemu: index cannot be used with bus and unit\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2261 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2262 | } |
| 2263 | if (max_devs == 0) |
| 2264 | { |
| 2265 | unit_id = index; |
| 2266 | bus_id = 0; |
| 2267 | } else { |
| 2268 | unit_id = index % max_devs; |
| 2269 | bus_id = index / max_devs; |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | /* if user doesn't specify a unit_id, |
| 2274 | * try to find the first free |
| 2275 | */ |
| 2276 | |
| 2277 | if (unit_id == -1) { |
| 2278 | unit_id = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2279 | while (drive_get(type, bus_id, unit_id) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2280 | unit_id++; |
| 2281 | if (max_devs && unit_id >= max_devs) { |
| 2282 | unit_id -= max_devs; |
| 2283 | bus_id++; |
| 2284 | } |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | /* check unit id */ |
| 2289 | |
| 2290 | if (max_devs && unit_id >= max_devs) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2291 | fprintf(stderr, "qemu: unit %d too big (max is %d)\n", |
| 2292 | unit_id, max_devs - 1); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2293 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | /* |
| 2297 | * ignore multiple definitions |
| 2298 | */ |
| 2299 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2300 | if (drive_get(type, bus_id, unit_id) != NULL) { |
| 2301 | *fatal_error = 0; |
| 2302 | return NULL; |
| 2303 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2304 | |
| 2305 | /* init */ |
| 2306 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2307 | dinfo = qemu_mallocz(sizeof(*dinfo)); |
Gerd Hoffmann | e23d9c4 | 2009-07-31 12:25:34 +0200 | [diff] [blame] | 2308 | if ((buf = qemu_opts_id(opts)) != NULL) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2309 | dinfo->id = qemu_strdup(buf); |
| 2310 | } else { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2311 | /* no id supplied -> create one */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2312 | dinfo->id = qemu_mallocz(32); |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2313 | if (type == IF_IDE || type == IF_SCSI) |
| 2314 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
| 2315 | if (max_devs) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2316 | snprintf(dinfo->id, 32, "%s%i%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2317 | devname, bus_id, mediastr, unit_id); |
| 2318 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2319 | snprintf(dinfo->id, 32, "%s%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2320 | devname, mediastr, unit_id); |
| 2321 | } |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2322 | dinfo->bdrv = bdrv_new(dinfo->id); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2323 | dinfo->devaddr = devaddr; |
| 2324 | dinfo->type = type; |
| 2325 | dinfo->bus = bus_id; |
| 2326 | dinfo->unit = unit_id; |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2327 | dinfo->on_read_error = on_read_error; |
| 2328 | dinfo->on_write_error = on_write_error; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2329 | dinfo->opts = opts; |
| 2330 | if (serial) |
| 2331 | strncpy(dinfo->serial, serial, sizeof(serial)); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2332 | QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2333 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2334 | switch(type) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2335 | case IF_IDE: |
| 2336 | case IF_SCSI: |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 2337 | case IF_XEN: |
Gerd Hoffmann | c219331 | 2009-09-15 19:23:28 +0000 | [diff] [blame] | 2338 | case IF_NONE: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2339 | switch(media) { |
| 2340 | case MEDIA_DISK: |
| 2341 | if (cyls != 0) { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2342 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
| 2343 | bdrv_set_translation_hint(dinfo->bdrv, translation); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2344 | } |
| 2345 | break; |
| 2346 | case MEDIA_CDROM: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2347 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2348 | break; |
| 2349 | } |
| 2350 | break; |
| 2351 | case IF_SD: |
| 2352 | /* FIXME: This isn't really a floppy, but it's a reasonable |
| 2353 | approximation. */ |
| 2354 | case IF_FLOPPY: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2355 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2356 | break; |
| 2357 | case IF_PFLASH: |
| 2358 | case IF_MTD: |
| 2359 | break; |
Gerd Hoffmann | d176c49 | 2009-07-31 12:25:41 +0200 | [diff] [blame] | 2360 | case IF_VIRTIO: |
| 2361 | /* add virtio block device */ |
| 2362 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 2363 | qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
| 2364 | qemu_opt_set(opts, "drive", dinfo->id); |
| 2365 | if (devaddr) |
| 2366 | qemu_opt_set(opts, "addr", devaddr); |
| 2367 | break; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 2368 | case IF_COUNT: |
| 2369 | abort(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2370 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2371 | if (!file) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2372 | *fatal_error = 0; |
| 2373 | return NULL; |
| 2374 | } |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2375 | bdrv_flags = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2376 | if (snapshot) { |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2377 | bdrv_flags |= BDRV_O_SNAPSHOT; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2378 | cache = 2; /* always use write-back with snapshot */ |
| 2379 | } |
| 2380 | if (cache == 0) /* no caching */ |
| 2381 | bdrv_flags |= BDRV_O_NOCACHE; |
| 2382 | else if (cache == 2) /* write-back */ |
| 2383 | bdrv_flags |= BDRV_O_CACHE_WB; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2384 | |
| 2385 | if (aio == 1) { |
| 2386 | bdrv_flags |= BDRV_O_NATIVE_AIO; |
| 2387 | } else { |
| 2388 | bdrv_flags &= ~BDRV_O_NATIVE_AIO; |
| 2389 | } |
| 2390 | |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2391 | if (ro == 1) { |
| 2392 | if (type == IF_IDE) { |
| 2393 | fprintf(stderr, "qemu: readonly flag not supported for drive with ide interface\n"); |
| 2394 | return NULL; |
| 2395 | } |
| 2396 | (void)bdrv_set_read_only(dinfo->bdrv, 1); |
| 2397 | } |
| 2398 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2399 | if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 2400 | fprintf(stderr, "qemu: could not open disk image %s: %s\n", |
| 2401 | file, strerror(errno)); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2402 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2403 | } |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2404 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2405 | if (bdrv_key_required(dinfo->bdrv)) |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2406 | autostart = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2407 | *fatal_error = 0; |
| 2408 | return dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2411 | static int drive_init_func(QemuOpts *opts, void *opaque) |
| 2412 | { |
| 2413 | QEMUMachine *machine = opaque; |
| 2414 | int fatal_error = 0; |
| 2415 | |
| 2416 | if (drive_init(opts, machine, &fatal_error) == NULL) { |
| 2417 | if (fatal_error) |
| 2418 | return 1; |
| 2419 | } |
| 2420 | return 0; |
| 2421 | } |
| 2422 | |
| 2423 | static int drive_enable_snapshot(QemuOpts *opts, void *opaque) |
| 2424 | { |
| 2425 | if (NULL == qemu_opt_get(opts, "snapshot")) { |
| 2426 | qemu_opt_set(opts, "snapshot", "on"); |
| 2427 | } |
| 2428 | return 0; |
| 2429 | } |
| 2430 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2431 | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
| 2432 | { |
| 2433 | boot_set_handler = func; |
| 2434 | boot_set_opaque = opaque; |
| 2435 | } |
| 2436 | |
| 2437 | int qemu_boot_set(const char *boot_devices) |
| 2438 | { |
| 2439 | if (!boot_set_handler) { |
| 2440 | return -EINVAL; |
| 2441 | } |
| 2442 | return boot_set_handler(boot_set_opaque, boot_devices); |
| 2443 | } |
| 2444 | |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2445 | static int parse_bootdevices(char *devices) |
| 2446 | { |
| 2447 | /* We just do some generic consistency checks */ |
| 2448 | const char *p; |
| 2449 | int bitmap = 0; |
| 2450 | |
| 2451 | for (p = devices; *p != '\0'; p++) { |
| 2452 | /* Allowed boot devices are: |
| 2453 | * a-b: floppy disk drives |
| 2454 | * c-f: IDE disk drives |
| 2455 | * g-m: machine implementation dependant drives |
| 2456 | * n-p: network devices |
| 2457 | * It's up to each machine implementation to check if the given boot |
| 2458 | * devices match the actual hardware implementation and firmware |
| 2459 | * features. |
| 2460 | */ |
| 2461 | if (*p < 'a' || *p > 'p') { |
| 2462 | fprintf(stderr, "Invalid boot device '%c'\n", *p); |
| 2463 | exit(1); |
| 2464 | } |
| 2465 | if (bitmap & (1 << (*p - 'a'))) { |
| 2466 | fprintf(stderr, "Boot device '%c' was given twice\n", *p); |
| 2467 | exit(1); |
| 2468 | } |
| 2469 | bitmap |= 1 << (*p - 'a'); |
| 2470 | } |
| 2471 | return bitmap; |
| 2472 | } |
| 2473 | |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2474 | static void restore_boot_devices(void *opaque) |
| 2475 | { |
| 2476 | char *standard_boot_devices = opaque; |
| 2477 | |
| 2478 | qemu_boot_set(standard_boot_devices); |
| 2479 | |
| 2480 | qemu_unregister_reset(restore_boot_devices, standard_boot_devices); |
| 2481 | qemu_free(standard_boot_devices); |
| 2482 | } |
| 2483 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 2484 | static void numa_add(const char *optarg) |
| 2485 | { |
| 2486 | char option[128]; |
| 2487 | char *endptr; |
| 2488 | unsigned long long value, endvalue; |
| 2489 | int nodenr; |
| 2490 | |
| 2491 | optarg = get_opt_name(option, 128, optarg, ',') + 1; |
| 2492 | if (!strcmp(option, "node")) { |
| 2493 | if (get_param_value(option, 128, "nodeid", optarg) == 0) { |
| 2494 | nodenr = nb_numa_nodes; |
| 2495 | } else { |
| 2496 | nodenr = strtoull(option, NULL, 10); |
| 2497 | } |
| 2498 | |
| 2499 | if (get_param_value(option, 128, "mem", optarg) == 0) { |
| 2500 | node_mem[nodenr] = 0; |
| 2501 | } else { |
| 2502 | value = strtoull(option, &endptr, 0); |
| 2503 | switch (*endptr) { |
| 2504 | case 0: case 'M': case 'm': |
| 2505 | value <<= 20; |
| 2506 | break; |
| 2507 | case 'G': case 'g': |
| 2508 | value <<= 30; |
| 2509 | break; |
| 2510 | } |
| 2511 | node_mem[nodenr] = value; |
| 2512 | } |
| 2513 | if (get_param_value(option, 128, "cpus", optarg) == 0) { |
| 2514 | node_cpumask[nodenr] = 0; |
| 2515 | } else { |
| 2516 | value = strtoull(option, &endptr, 10); |
| 2517 | if (value >= 64) { |
| 2518 | value = 63; |
| 2519 | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n"); |
| 2520 | } else { |
| 2521 | if (*endptr == '-') { |
| 2522 | endvalue = strtoull(endptr+1, &endptr, 10); |
| 2523 | if (endvalue >= 63) { |
| 2524 | endvalue = 62; |
| 2525 | fprintf(stderr, |
| 2526 | "only 63 CPUs in NUMA mode supported.\n"); |
| 2527 | } |
| 2528 | value = (1 << (endvalue + 1)) - (1 << value); |
| 2529 | } else { |
| 2530 | value = 1 << value; |
| 2531 | } |
| 2532 | } |
| 2533 | node_cpumask[nodenr] = value; |
| 2534 | } |
| 2535 | nb_numa_nodes++; |
| 2536 | } |
| 2537 | return; |
| 2538 | } |
| 2539 | |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 2540 | static void smp_parse(const char *optarg) |
| 2541 | { |
| 2542 | int smp, sockets = 0, threads = 0, cores = 0; |
| 2543 | char *endptr; |
| 2544 | char option[128]; |
| 2545 | |
| 2546 | smp = strtoul(optarg, &endptr, 10); |
| 2547 | if (endptr != optarg) { |
| 2548 | if (*endptr == ',') { |
| 2549 | endptr++; |
| 2550 | } |
| 2551 | } |
| 2552 | if (get_param_value(option, 128, "sockets", endptr) != 0) |
| 2553 | sockets = strtoull(option, NULL, 10); |
| 2554 | if (get_param_value(option, 128, "cores", endptr) != 0) |
| 2555 | cores = strtoull(option, NULL, 10); |
| 2556 | if (get_param_value(option, 128, "threads", endptr) != 0) |
| 2557 | threads = strtoull(option, NULL, 10); |
| 2558 | if (get_param_value(option, 128, "maxcpus", endptr) != 0) |
| 2559 | max_cpus = strtoull(option, NULL, 10); |
| 2560 | |
| 2561 | /* compute missing values, prefer sockets over cores over threads */ |
| 2562 | if (smp == 0 || sockets == 0) { |
| 2563 | sockets = sockets > 0 ? sockets : 1; |
| 2564 | cores = cores > 0 ? cores : 1; |
| 2565 | threads = threads > 0 ? threads : 1; |
| 2566 | if (smp == 0) { |
| 2567 | smp = cores * threads * sockets; |
| 2568 | } else { |
| 2569 | sockets = smp / (cores * threads); |
| 2570 | } |
| 2571 | } else { |
| 2572 | if (cores == 0) { |
| 2573 | threads = threads > 0 ? threads : 1; |
| 2574 | cores = smp / (sockets * threads); |
| 2575 | } else { |
| 2576 | if (sockets == 0) { |
| 2577 | sockets = smp / (cores * threads); |
| 2578 | } else { |
| 2579 | threads = smp / (cores * sockets); |
| 2580 | } |
| 2581 | } |
| 2582 | } |
| 2583 | smp_cpus = smp; |
| 2584 | smp_cores = cores > 0 ? cores : 1; |
| 2585 | smp_threads = threads > 0 ? threads : 1; |
| 2586 | if (max_cpus == 0) |
| 2587 | max_cpus = smp_cpus; |
| 2588 | } |
| 2589 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 2590 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2591 | /* USB devices */ |
| 2592 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2593 | static int usb_device_add(const char *devname, int is_hotplug) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2594 | { |
| 2595 | const char *p; |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2596 | USBDevice *dev = NULL; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2597 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2598 | if (!usb_enabled) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2599 | return -1; |
| 2600 | |
Gerd Hoffmann | 0958b4c | 2009-10-26 15:56:45 +0100 | [diff] [blame] | 2601 | /* drivers with .usbdevice_name entry in USBDeviceInfo */ |
| 2602 | dev = usbdevice_create(devname); |
| 2603 | if (dev) |
| 2604 | goto done; |
| 2605 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2606 | /* the other ones */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2607 | if (strstart(devname, "host:", &p)) { |
| 2608 | dev = usb_host_device_open(p); |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2609 | } else if (strstart(devname, "net:", &p)) { |
Mark McLoughlin | 13cf8f2 | 2009-10-06 12:17:14 +0100 | [diff] [blame] | 2610 | QemuOpts *opts; |
| 2611 | int idx; |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2612 | |
Mark McLoughlin | 13cf8f2 | 2009-10-06 12:17:14 +0100 | [diff] [blame] | 2613 | opts = qemu_opts_parse(&qemu_net_opts, p, NULL); |
| 2614 | if (!opts) { |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2615 | return -1; |
Mark McLoughlin | 13cf8f2 | 2009-10-06 12:17:14 +0100 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | qemu_opt_set(opts, "type", "nic"); |
| 2619 | qemu_opt_set(opts, "model", "usb"); |
| 2620 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2621 | idx = net_client_init(NULL, opts, 0); |
Mark McLoughlin | 13cf8f2 | 2009-10-06 12:17:14 +0100 | [diff] [blame] | 2622 | if (idx == -1) { |
| 2623 | return -1; |
| 2624 | } |
| 2625 | |
| 2626 | dev = usb_net_init(&nd_table[idx]); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 2627 | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
| 2628 | dev = usb_bt_init(devname[2] ? hci_init(p) : |
| 2629 | bt_new_hci(qemu_find_bt_vlan(0))); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2630 | } else { |
| 2631 | return -1; |
| 2632 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2633 | if (!dev) |
| 2634 | return -1; |
| 2635 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2636 | done: |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2637 | return 0; |
| 2638 | } |
| 2639 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2640 | static int usb_device_del(const char *devname) |
| 2641 | { |
| 2642 | int bus_num, addr; |
| 2643 | const char *p; |
| 2644 | |
aliguori | 5d0c575 | 2008-09-14 01:07:41 +0000 | [diff] [blame] | 2645 | if (strstart(devname, "host:", &p)) |
| 2646 | return usb_host_device_close(p); |
| 2647 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2648 | if (!usb_enabled) |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2649 | return -1; |
| 2650 | |
| 2651 | p = strchr(devname, '.'); |
| 2652 | if (!p) |
| 2653 | return -1; |
| 2654 | bus_num = strtoul(devname, NULL, 0); |
| 2655 | addr = strtoul(p + 1, NULL, 0); |
| 2656 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2657 | return usb_device_delete_addr(bus_num, addr); |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 2660 | static int usb_parse(const char *cmdline) |
| 2661 | { |
| 2662 | return usb_device_add(cmdline, 0); |
| 2663 | } |
| 2664 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2665 | void do_usb_add(Monitor *mon, const QDict *qdict) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2666 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2667 | usb_device_add(qdict_get_str(qdict, "devname"), 1); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2670 | void do_usb_del(Monitor *mon, const QDict *qdict) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2671 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2672 | usb_device_del(qdict_get_str(qdict, "devname")); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 2675 | /***********************************************************/ |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2676 | /* PCMCIA/Cardbus */ |
| 2677 | |
| 2678 | static struct pcmcia_socket_entry_s { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2679 | PCMCIASocket *socket; |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2680 | struct pcmcia_socket_entry_s *next; |
| 2681 | } *pcmcia_sockets = 0; |
| 2682 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2683 | void pcmcia_socket_register(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2684 | { |
| 2685 | struct pcmcia_socket_entry_s *entry; |
| 2686 | |
| 2687 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
| 2688 | entry->socket = socket; |
| 2689 | entry->next = pcmcia_sockets; |
| 2690 | pcmcia_sockets = entry; |
| 2691 | } |
| 2692 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2693 | void pcmcia_socket_unregister(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2694 | { |
| 2695 | struct pcmcia_socket_entry_s *entry, **ptr; |
| 2696 | |
| 2697 | ptr = &pcmcia_sockets; |
| 2698 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) |
| 2699 | if (entry->socket == socket) { |
| 2700 | *ptr = entry->next; |
| 2701 | qemu_free(entry); |
| 2702 | } |
| 2703 | } |
| 2704 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2705 | void pcmcia_info(Monitor *mon) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2706 | { |
| 2707 | struct pcmcia_socket_entry_s *iter; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2708 | |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2709 | if (!pcmcia_sockets) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2710 | monitor_printf(mon, "No PCMCIA sockets\n"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2711 | |
| 2712 | for (iter = pcmcia_sockets; iter; iter = iter->next) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2713 | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string, |
| 2714 | iter->socket->attached ? iter->socket->card_string : |
| 2715 | "Empty"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | /***********************************************************/ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2719 | /* register display */ |
| 2720 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2721 | struct DisplayAllocator default_allocator = { |
| 2722 | defaultallocator_create_displaysurface, |
| 2723 | defaultallocator_resize_displaysurface, |
| 2724 | defaultallocator_free_displaysurface |
| 2725 | }; |
| 2726 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2727 | void register_displaystate(DisplayState *ds) |
| 2728 | { |
| 2729 | DisplayState **s; |
| 2730 | s = &display_state; |
| 2731 | while (*s != NULL) |
| 2732 | s = &(*s)->next; |
| 2733 | ds->next = NULL; |
| 2734 | *s = ds; |
| 2735 | } |
| 2736 | |
| 2737 | DisplayState *get_displaystate(void) |
| 2738 | { |
| 2739 | return display_state; |
| 2740 | } |
| 2741 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2742 | DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
| 2743 | { |
| 2744 | if(ds->allocator == &default_allocator) ds->allocator = da; |
| 2745 | return ds->allocator; |
| 2746 | } |
| 2747 | |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2748 | /* dumb display */ |
| 2749 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2750 | static void dumb_display_init(void) |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2751 | { |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2752 | DisplayState *ds = qemu_mallocz(sizeof(DisplayState)); |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2753 | ds->allocator = &default_allocator; |
| 2754 | ds->surface = qemu_create_displaysurface(ds, 640, 480); |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2755 | register_displaystate(ds); |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2759 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 2760 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2761 | typedef struct IOHandlerRecord { |
| 2762 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2763 | IOCanRWHandler *fd_read_poll; |
| 2764 | IOHandler *fd_read; |
| 2765 | IOHandler *fd_write; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2766 | int deleted; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2767 | void *opaque; |
| 2768 | /* temporary data */ |
| 2769 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2770 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2771 | } IOHandlerRecord; |
| 2772 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2773 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2774 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2775 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 2776 | necessary in the character devices to suppress fd_can_read(). */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2777 | int qemu_set_fd_handler2(int fd, |
| 2778 | IOCanRWHandler *fd_read_poll, |
| 2779 | IOHandler *fd_read, |
| 2780 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2781 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2782 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2783 | IOHandlerRecord **pioh, *ioh; |
| 2784 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2785 | if (!fd_read && !fd_write) { |
| 2786 | pioh = &first_io_handler; |
| 2787 | for(;;) { |
| 2788 | ioh = *pioh; |
| 2789 | if (ioh == NULL) |
| 2790 | break; |
| 2791 | if (ioh->fd == fd) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2792 | ioh->deleted = 1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2793 | break; |
| 2794 | } |
| 2795 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2796 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2797 | } else { |
| 2798 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 2799 | if (ioh->fd == fd) |
| 2800 | goto found; |
| 2801 | } |
| 2802 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2803 | ioh->next = first_io_handler; |
| 2804 | first_io_handler = ioh; |
| 2805 | found: |
| 2806 | ioh->fd = fd; |
| 2807 | ioh->fd_read_poll = fd_read_poll; |
| 2808 | ioh->fd_read = fd_read; |
| 2809 | ioh->fd_write = fd_write; |
| 2810 | ioh->opaque = opaque; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2811 | ioh->deleted = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2812 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2813 | return 0; |
| 2814 | } |
| 2815 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2816 | int qemu_set_fd_handler(int fd, |
| 2817 | IOHandler *fd_read, |
| 2818 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2819 | void *opaque) |
| 2820 | { |
| 2821 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 2824 | #ifdef _WIN32 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2825 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2826 | /* Polling handling */ |
| 2827 | |
| 2828 | typedef struct PollingEntry { |
| 2829 | PollingFunc *func; |
| 2830 | void *opaque; |
| 2831 | struct PollingEntry *next; |
| 2832 | } PollingEntry; |
| 2833 | |
| 2834 | static PollingEntry *first_polling_entry; |
| 2835 | |
| 2836 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 2837 | { |
| 2838 | PollingEntry **ppe, *pe; |
| 2839 | pe = qemu_mallocz(sizeof(PollingEntry)); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2840 | pe->func = func; |
| 2841 | pe->opaque = opaque; |
| 2842 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 2843 | *ppe = pe; |
| 2844 | return 0; |
| 2845 | } |
| 2846 | |
| 2847 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 2848 | { |
| 2849 | PollingEntry **ppe, *pe; |
| 2850 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 2851 | pe = *ppe; |
| 2852 | if (pe->func == func && pe->opaque == opaque) { |
| 2853 | *ppe = pe->next; |
| 2854 | qemu_free(pe); |
| 2855 | break; |
| 2856 | } |
| 2857 | } |
| 2858 | } |
| 2859 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2860 | /***********************************************************/ |
| 2861 | /* Wait objects support */ |
| 2862 | typedef struct WaitObjects { |
| 2863 | int num; |
| 2864 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2865 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2866 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2867 | } WaitObjects; |
| 2868 | |
| 2869 | static WaitObjects wait_objects = {0}; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2870 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2871 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2872 | { |
| 2873 | WaitObjects *w = &wait_objects; |
| 2874 | |
| 2875 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 2876 | return -1; |
| 2877 | w->events[w->num] = handle; |
| 2878 | w->func[w->num] = func; |
| 2879 | w->opaque[w->num] = opaque; |
| 2880 | w->num++; |
| 2881 | return 0; |
| 2882 | } |
| 2883 | |
| 2884 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2885 | { |
| 2886 | int i, found; |
| 2887 | WaitObjects *w = &wait_objects; |
| 2888 | |
| 2889 | found = 0; |
| 2890 | for (i = 0; i < w->num; i++) { |
| 2891 | if (w->events[i] == handle) |
| 2892 | found = 1; |
| 2893 | if (found) { |
| 2894 | w->events[i] = w->events[i + 1]; |
| 2895 | w->func[i] = w->func[i + 1]; |
| 2896 | w->opaque[i] = w->opaque[i + 1]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2897 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2898 | } |
| 2899 | if (found) |
| 2900 | w->num--; |
| 2901 | } |
| 2902 | #endif |
| 2903 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2904 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2905 | /* ram save/restore */ |
| 2906 | |
Juan Quintela | 94fb090 | 2009-09-10 03:04:23 +0200 | [diff] [blame] | 2907 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2908 | #define RAM_SAVE_FLAG_COMPRESS 0x02 |
| 2909 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
| 2910 | #define RAM_SAVE_FLAG_PAGE 0x08 |
| 2911 | #define RAM_SAVE_FLAG_EOS 0x10 |
| 2912 | |
| 2913 | static int is_dup_page(uint8_t *page, uint8_t ch) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2914 | { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2915 | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
| 2916 | uint32_t *array = (uint32_t *)page; |
| 2917 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2918 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2919 | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
| 2920 | if (array[i] != val) |
| 2921 | return 0; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2922 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2923 | |
| 2924 | return 1; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2927 | static int ram_save_block(QEMUFile *f) |
| 2928 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2929 | static ram_addr_t current_addr = 0; |
| 2930 | ram_addr_t saved_addr = current_addr; |
| 2931 | ram_addr_t addr = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2932 | int found = 0; |
| 2933 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2934 | while (addr < last_ram_offset) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2935 | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2936 | uint8_t *p; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2937 | |
| 2938 | cpu_physical_memory_reset_dirty(current_addr, |
| 2939 | current_addr + TARGET_PAGE_SIZE, |
| 2940 | MIGRATION_DIRTY_FLAG); |
| 2941 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2942 | p = qemu_get_ram_ptr(current_addr); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2943 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2944 | if (is_dup_page(p, *p)) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2945 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2946 | qemu_put_byte(f, *p); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2947 | } else { |
| 2948 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2949 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | found = 1; |
| 2953 | break; |
| 2954 | } |
| 2955 | addr += TARGET_PAGE_SIZE; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2956 | current_addr = (saved_addr + addr) % last_ram_offset; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | return found; |
| 2960 | } |
| 2961 | |
Jan Kiszka | 8430793 | 2009-12-02 11:29:38 +0100 | [diff] [blame] | 2962 | static uint64_t bytes_transferred; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2963 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2964 | static ram_addr_t ram_save_remaining(void) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2965 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2966 | ram_addr_t addr; |
| 2967 | ram_addr_t count = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2968 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2969 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2970 | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 2971 | count++; |
| 2972 | } |
| 2973 | |
| 2974 | return count; |
| 2975 | } |
| 2976 | |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 2977 | uint64_t ram_bytes_remaining(void) |
| 2978 | { |
| 2979 | return ram_save_remaining() * TARGET_PAGE_SIZE; |
| 2980 | } |
| 2981 | |
| 2982 | uint64_t ram_bytes_transferred(void) |
| 2983 | { |
| 2984 | return bytes_transferred; |
| 2985 | } |
| 2986 | |
| 2987 | uint64_t ram_bytes_total(void) |
| 2988 | { |
| 2989 | return last_ram_offset; |
| 2990 | } |
| 2991 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 2992 | static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2993 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2994 | ram_addr_t addr; |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 2995 | uint64_t bytes_transferred_last; |
| 2996 | double bwidth = 0; |
| 2997 | uint64_t expected_time = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2998 | |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 2999 | if (stage < 0) { |
| 3000 | cpu_physical_memory_set_dirty_tracking(0); |
| 3001 | return 0; |
| 3002 | } |
| 3003 | |
Jan Kiszka | 9fa0638 | 2009-05-22 23:51:45 +0200 | [diff] [blame] | 3004 | if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 3005 | qemu_file_set_error(f); |
| 3006 | return 0; |
| 3007 | } |
| 3008 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3009 | if (stage == 1) { |
Jan Kiszka | 8430793 | 2009-12-02 11:29:38 +0100 | [diff] [blame] | 3010 | bytes_transferred = 0; |
| 3011 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3012 | /* Make sure all dirty bits are set */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3013 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3014 | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 3015 | cpu_physical_memory_set_dirty(addr); |
| 3016 | } |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 3017 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3018 | /* Enable dirty memory tracking */ |
| 3019 | cpu_physical_memory_set_dirty_tracking(1); |
| 3020 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3021 | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3024 | bytes_transferred_last = bytes_transferred; |
| 3025 | bwidth = get_clock(); |
| 3026 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3027 | while (!qemu_file_rate_limit(f)) { |
| 3028 | int ret; |
| 3029 | |
| 3030 | ret = ram_save_block(f); |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3031 | bytes_transferred += ret * TARGET_PAGE_SIZE; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3032 | if (ret == 0) /* no more blocks */ |
| 3033 | break; |
| 3034 | } |
| 3035 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3036 | bwidth = get_clock() - bwidth; |
| 3037 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; |
| 3038 | |
| 3039 | /* if we haven't transferred anything this round, force expected_time to a |
| 3040 | * a very high value, but without crashing */ |
| 3041 | if (bwidth == 0) |
| 3042 | bwidth = 0.000001; |
| 3043 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3044 | /* try transferring iterative blocks of memory */ |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3045 | if (stage == 3) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3046 | /* flush all remaining blocks regardless of rate limiting */ |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3047 | while (ram_save_block(f) != 0) { |
| 3048 | bytes_transferred += TARGET_PAGE_SIZE; |
| 3049 | } |
aliguori | 8215e91 | 2009-04-05 19:30:55 +0000 | [diff] [blame] | 3050 | cpu_physical_memory_set_dirty_tracking(0); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 3054 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3055 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
| 3056 | |
| 3057 | return (stage == 2) && (expected_time <= migrate_max_downtime()); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3060 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 3061 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3062 | ram_addr_t addr; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3063 | int flags; |
| 3064 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3065 | if (version_id != 3) |
| 3066 | return -EINVAL; |
| 3067 | |
| 3068 | do { |
| 3069 | addr = qemu_get_be64(f); |
| 3070 | |
| 3071 | flags = addr & ~TARGET_PAGE_MASK; |
| 3072 | addr &= TARGET_PAGE_MASK; |
| 3073 | |
| 3074 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3075 | if (addr != last_ram_offset) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3076 | return -EINVAL; |
| 3077 | } |
| 3078 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3079 | if (flags & RAM_SAVE_FLAG_COMPRESS) { |
| 3080 | uint8_t ch = qemu_get_byte(f); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3081 | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
| 3082 | #ifndef _WIN32 |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3083 | if (ch == 0 && |
| 3084 | (!kvm_enabled() || kvm_has_sync_mmu())) { |
| 3085 | madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3086 | } |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3087 | #endif |
Jan Kiszka | 9a743e5 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 3088 | } else if (flags & RAM_SAVE_FLAG_PAGE) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3089 | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
Jan Kiszka | 9a743e5 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 3090 | } |
| 3091 | if (qemu_file_has_error(f)) { |
| 3092 | return -EIO; |
| 3093 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3094 | } while (!(flags & RAM_SAVE_FLAG_EOS)); |
| 3095 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3096 | return 0; |
| 3097 | } |
| 3098 | |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3099 | void qemu_service_io(void) |
| 3100 | { |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3101 | qemu_notify_event(); |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3104 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3105 | /* machine registration */ |
| 3106 | |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 3107 | static QEMUMachine *first_machine = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 3108 | QEMUMachine *current_machine = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3109 | |
| 3110 | int qemu_register_machine(QEMUMachine *m) |
| 3111 | { |
| 3112 | QEMUMachine **pm; |
| 3113 | pm = &first_machine; |
| 3114 | while (*pm != NULL) |
| 3115 | pm = &(*pm)->next; |
| 3116 | m->next = NULL; |
| 3117 | *pm = m; |
| 3118 | return 0; |
| 3119 | } |
| 3120 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3121 | static QEMUMachine *find_machine(const char *name) |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3122 | { |
| 3123 | QEMUMachine *m; |
| 3124 | |
| 3125 | for(m = first_machine; m != NULL; m = m->next) { |
| 3126 | if (!strcmp(m->name, name)) |
| 3127 | return m; |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 3128 | if (m->alias && !strcmp(m->alias, name)) |
| 3129 | return m; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3130 | } |
| 3131 | return NULL; |
| 3132 | } |
| 3133 | |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 3134 | static QEMUMachine *find_default_machine(void) |
| 3135 | { |
| 3136 | QEMUMachine *m; |
| 3137 | |
| 3138 | for(m = first_machine; m != NULL; m = m->next) { |
| 3139 | if (m->is_default) { |
| 3140 | return m; |
| 3141 | } |
| 3142 | } |
| 3143 | return NULL; |
| 3144 | } |
| 3145 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3146 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3147 | /* main execution loop */ |
| 3148 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3149 | static void gui_update(void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3150 | { |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3151 | uint64_t interval = GUI_REFRESH_INTERVAL; |
ths | 740733b | 2007-06-08 01:57:56 +0000 | [diff] [blame] | 3152 | DisplayState *ds = opaque; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3153 | DisplayChangeListener *dcl = ds->listeners; |
| 3154 | |
| 3155 | dpy_refresh(ds); |
| 3156 | |
| 3157 | while (dcl != NULL) { |
| 3158 | if (dcl->gui_timer_interval && |
| 3159 | dcl->gui_timer_interval < interval) |
| 3160 | interval = dcl->gui_timer_interval; |
| 3161 | dcl = dcl->next; |
| 3162 | } |
| 3163 | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 3166 | static void nographic_update(void *opaque) |
| 3167 | { |
| 3168 | uint64_t interval = GUI_REFRESH_INTERVAL; |
| 3169 | |
| 3170 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); |
| 3171 | } |
| 3172 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3173 | struct vm_change_state_entry { |
| 3174 | VMChangeStateHandler *cb; |
| 3175 | void *opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3176 | QLIST_ENTRY (vm_change_state_entry) entries; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3177 | }; |
| 3178 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3179 | static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3180 | |
| 3181 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 3182 | void *opaque) |
| 3183 | { |
| 3184 | VMChangeStateEntry *e; |
| 3185 | |
| 3186 | e = qemu_mallocz(sizeof (*e)); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3187 | |
| 3188 | e->cb = cb; |
| 3189 | e->opaque = opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3190 | QLIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3191 | return e; |
| 3192 | } |
| 3193 | |
| 3194 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 3195 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3196 | QLIST_REMOVE (e, entries); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3197 | qemu_free (e); |
| 3198 | } |
| 3199 | |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3200 | static void vm_state_notify(int running, int reason) |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3201 | { |
| 3202 | VMChangeStateEntry *e; |
| 3203 | |
| 3204 | 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] | 3205 | e->cb(e->opaque, running, reason); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3206 | } |
| 3207 | } |
| 3208 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3209 | static void resume_all_vcpus(void); |
| 3210 | static void pause_all_vcpus(void); |
| 3211 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3212 | void vm_start(void) |
| 3213 | { |
| 3214 | if (!vm_running) { |
| 3215 | cpu_enable_ticks(); |
| 3216 | vm_running = 1; |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3217 | vm_state_notify(1, 0); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 3218 | qemu_rearm_alarm_timer(alarm_timer); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3219 | resume_all_vcpus(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3220 | } |
| 3221 | } |
| 3222 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3223 | /* reset/shutdown handler */ |
| 3224 | |
| 3225 | typedef struct QEMUResetEntry { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3226 | QTAILQ_ENTRY(QEMUResetEntry) entry; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3227 | QEMUResetHandler *func; |
| 3228 | void *opaque; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3229 | } QEMUResetEntry; |
| 3230 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3231 | static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers = |
| 3232 | QTAILQ_HEAD_INITIALIZER(reset_handlers); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3233 | static int reset_requested; |
| 3234 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3235 | static int powerdown_requested; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3236 | static int debug_requested; |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3237 | static int vmstop_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3238 | |
aurel32 | cf7a2fe | 2008-03-18 06:53:05 +0000 | [diff] [blame] | 3239 | int qemu_shutdown_requested(void) |
| 3240 | { |
| 3241 | int r = shutdown_requested; |
| 3242 | shutdown_requested = 0; |
| 3243 | return r; |
| 3244 | } |
| 3245 | |
| 3246 | int qemu_reset_requested(void) |
| 3247 | { |
| 3248 | int r = reset_requested; |
| 3249 | reset_requested = 0; |
| 3250 | return r; |
| 3251 | } |
| 3252 | |
| 3253 | int qemu_powerdown_requested(void) |
| 3254 | { |
| 3255 | int r = powerdown_requested; |
| 3256 | powerdown_requested = 0; |
| 3257 | return r; |
| 3258 | } |
| 3259 | |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3260 | static int qemu_debug_requested(void) |
| 3261 | { |
| 3262 | int r = debug_requested; |
| 3263 | debug_requested = 0; |
| 3264 | return r; |
| 3265 | } |
| 3266 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3267 | static int qemu_vmstop_requested(void) |
| 3268 | { |
| 3269 | int r = vmstop_requested; |
| 3270 | vmstop_requested = 0; |
| 3271 | return r; |
| 3272 | } |
| 3273 | |
| 3274 | static void do_vm_stop(int reason) |
| 3275 | { |
| 3276 | if (vm_running) { |
| 3277 | cpu_disable_ticks(); |
| 3278 | vm_running = 0; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3279 | pause_all_vcpus(); |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3280 | vm_state_notify(0, reason); |
| 3281 | } |
| 3282 | } |
| 3283 | |
Jan Kiszka | a08d436 | 2009-06-27 09:25:07 +0200 | [diff] [blame] | 3284 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3285 | { |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3286 | QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry)); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3287 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3288 | re->func = func; |
| 3289 | re->opaque = opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3290 | QTAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3293 | void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3294 | { |
| 3295 | QEMUResetEntry *re; |
| 3296 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3297 | QTAILQ_FOREACH(re, &reset_handlers, entry) { |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3298 | if (re->func == func && re->opaque == opaque) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3299 | QTAILQ_REMOVE(&reset_handlers, re, entry); |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3300 | qemu_free(re); |
| 3301 | return; |
| 3302 | } |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | void qemu_system_reset(void) |
| 3307 | { |
| 3308 | QEMUResetEntry *re, *nre; |
| 3309 | |
| 3310 | /* reset all devices */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3311 | QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3312 | re->func(re->opaque); |
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | void qemu_system_reset_request(void) |
| 3317 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 3318 | if (no_reboot) { |
| 3319 | shutdown_requested = 1; |
| 3320 | } else { |
| 3321 | reset_requested = 1; |
| 3322 | } |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3323 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | void qemu_system_shutdown_request(void) |
| 3327 | { |
| 3328 | shutdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3329 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3332 | void qemu_system_powerdown_request(void) |
| 3333 | { |
| 3334 | powerdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3335 | qemu_notify_event(); |
| 3336 | } |
| 3337 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3338 | #ifdef CONFIG_IOTHREAD |
| 3339 | static void qemu_system_vmstop_request(int reason) |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3340 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3341 | vmstop_requested = reason; |
| 3342 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3343 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3344 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3345 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3346 | #ifndef _WIN32 |
| 3347 | static int io_thread_fd = -1; |
| 3348 | |
| 3349 | static void qemu_event_increment(void) |
| 3350 | { |
| 3351 | static const char byte = 0; |
| 3352 | |
| 3353 | if (io_thread_fd == -1) |
| 3354 | return; |
| 3355 | |
| 3356 | write(io_thread_fd, &byte, sizeof(byte)); |
| 3357 | } |
| 3358 | |
| 3359 | static void qemu_event_read(void *opaque) |
| 3360 | { |
| 3361 | int fd = (unsigned long)opaque; |
| 3362 | ssize_t len; |
| 3363 | |
| 3364 | /* Drain the notify pipe */ |
| 3365 | do { |
| 3366 | char buffer[512]; |
| 3367 | len = read(fd, buffer, sizeof(buffer)); |
| 3368 | } while ((len == -1 && errno == EINTR) || len > 0); |
| 3369 | } |
| 3370 | |
| 3371 | static int qemu_event_init(void) |
| 3372 | { |
| 3373 | int err; |
| 3374 | int fds[2]; |
| 3375 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 3376 | err = qemu_pipe(fds); |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3377 | if (err == -1) |
| 3378 | return -errno; |
| 3379 | |
| 3380 | err = fcntl_setfl(fds[0], O_NONBLOCK); |
| 3381 | if (err < 0) |
| 3382 | goto fail; |
| 3383 | |
| 3384 | err = fcntl_setfl(fds[1], O_NONBLOCK); |
| 3385 | if (err < 0) |
| 3386 | goto fail; |
| 3387 | |
| 3388 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
| 3389 | (void *)(unsigned long)fds[0]); |
| 3390 | |
| 3391 | io_thread_fd = fds[1]; |
Jan Kiszka | a7e2121 | 2009-04-29 18:38:28 +0000 | [diff] [blame] | 3392 | return 0; |
| 3393 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3394 | fail: |
| 3395 | close(fds[0]); |
| 3396 | close(fds[1]); |
| 3397 | return err; |
| 3398 | } |
| 3399 | #else |
| 3400 | HANDLE qemu_event_handle; |
| 3401 | |
| 3402 | static void dummy_event_handler(void *opaque) |
| 3403 | { |
| 3404 | } |
| 3405 | |
| 3406 | static int qemu_event_init(void) |
| 3407 | { |
| 3408 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 3409 | if (!qemu_event_handle) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 3410 | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3411 | return -1; |
| 3412 | } |
| 3413 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
| 3417 | static void qemu_event_increment(void) |
| 3418 | { |
malc | de1c90c | 2009-09-27 14:38:18 +0400 | [diff] [blame] | 3419 | if (!SetEvent(qemu_event_handle)) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 3420 | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n", |
malc | de1c90c | 2009-09-27 14:38:18 +0400 | [diff] [blame] | 3421 | GetLastError()); |
| 3422 | exit (1); |
| 3423 | } |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3424 | } |
| 3425 | #endif |
| 3426 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3427 | static int cpu_can_run(CPUState *env) |
| 3428 | { |
| 3429 | if (env->stop) |
| 3430 | return 0; |
| 3431 | if (env->stopped) |
| 3432 | return 0; |
| 3433 | return 1; |
| 3434 | } |
| 3435 | |
| 3436 | #ifndef CONFIG_IOTHREAD |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3437 | static int qemu_init_main_loop(void) |
| 3438 | { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3439 | return qemu_event_init(); |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3442 | void qemu_init_vcpu(void *_env) |
| 3443 | { |
| 3444 | CPUState *env = _env; |
| 3445 | |
| 3446 | if (kvm_enabled()) |
| 3447 | kvm_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3448 | env->nr_cores = smp_cores; |
| 3449 | env->nr_threads = smp_threads; |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3450 | return; |
| 3451 | } |
| 3452 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3453 | int qemu_cpu_self(void *env) |
| 3454 | { |
| 3455 | return 1; |
| 3456 | } |
| 3457 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3458 | static void resume_all_vcpus(void) |
| 3459 | { |
| 3460 | } |
| 3461 | |
| 3462 | static void pause_all_vcpus(void) |
| 3463 | { |
| 3464 | } |
| 3465 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3466 | void qemu_cpu_kick(void *env) |
| 3467 | { |
| 3468 | return; |
| 3469 | } |
| 3470 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3471 | void qemu_notify_event(void) |
| 3472 | { |
| 3473 | CPUState *env = cpu_single_env; |
| 3474 | |
| 3475 | if (env) { |
| 3476 | cpu_exit(env); |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 3477 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
Glauber Costa | d549db5 | 2009-10-07 16:38:03 -0300 | [diff] [blame] | 3480 | void qemu_mutex_lock_iothread(void) {} |
| 3481 | void qemu_mutex_unlock_iothread(void) {} |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3482 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3483 | void vm_stop(int reason) |
| 3484 | { |
| 3485 | do_vm_stop(reason); |
| 3486 | } |
| 3487 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3488 | #else /* CONFIG_IOTHREAD */ |
| 3489 | |
| 3490 | #include "qemu-thread.h" |
| 3491 | |
| 3492 | QemuMutex qemu_global_mutex; |
| 3493 | static QemuMutex qemu_fair_mutex; |
| 3494 | |
| 3495 | static QemuThread io_thread; |
| 3496 | |
| 3497 | static QemuThread *tcg_cpu_thread; |
| 3498 | static QemuCond *tcg_halt_cond; |
| 3499 | |
| 3500 | static int qemu_system_ready; |
| 3501 | /* cpu creation */ |
| 3502 | static QemuCond qemu_cpu_cond; |
| 3503 | /* system init */ |
| 3504 | static QemuCond qemu_system_cond; |
| 3505 | static QemuCond qemu_pause_cond; |
| 3506 | |
| 3507 | static void block_io_signals(void); |
| 3508 | static void unblock_io_signals(void); |
| 3509 | static int tcg_has_work(void); |
| 3510 | |
| 3511 | static int qemu_init_main_loop(void) |
| 3512 | { |
| 3513 | int ret; |
| 3514 | |
| 3515 | ret = qemu_event_init(); |
| 3516 | if (ret) |
| 3517 | return ret; |
| 3518 | |
| 3519 | qemu_cond_init(&qemu_pause_cond); |
| 3520 | qemu_mutex_init(&qemu_fair_mutex); |
| 3521 | qemu_mutex_init(&qemu_global_mutex); |
| 3522 | qemu_mutex_lock(&qemu_global_mutex); |
| 3523 | |
| 3524 | unblock_io_signals(); |
| 3525 | qemu_thread_self(&io_thread); |
| 3526 | |
| 3527 | return 0; |
| 3528 | } |
| 3529 | |
| 3530 | static void qemu_wait_io_event(CPUState *env) |
| 3531 | { |
| 3532 | while (!tcg_has_work()) |
| 3533 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); |
| 3534 | |
| 3535 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3536 | |
| 3537 | /* |
| 3538 | * Users of qemu_global_mutex can be starved, having no chance |
| 3539 | * to acquire it since this path will get to it first. |
| 3540 | * So use another lock to provide fairness. |
| 3541 | */ |
| 3542 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3543 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3544 | |
| 3545 | qemu_mutex_lock(&qemu_global_mutex); |
| 3546 | if (env->stop) { |
| 3547 | env->stop = 0; |
| 3548 | env->stopped = 1; |
| 3549 | qemu_cond_signal(&qemu_pause_cond); |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | static int qemu_cpu_exec(CPUState *env); |
| 3554 | |
| 3555 | static void *kvm_cpu_thread_fn(void *arg) |
| 3556 | { |
| 3557 | CPUState *env = arg; |
| 3558 | |
| 3559 | block_io_signals(); |
| 3560 | qemu_thread_self(env->thread); |
Jean-Christophe DUBOIS | 321c1cb | 2009-09-02 23:59:04 +0200 | [diff] [blame] | 3561 | if (kvm_enabled()) |
| 3562 | kvm_init_vcpu(env); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3563 | |
| 3564 | /* signal CPU creation */ |
| 3565 | qemu_mutex_lock(&qemu_global_mutex); |
| 3566 | env->created = 1; |
| 3567 | qemu_cond_signal(&qemu_cpu_cond); |
| 3568 | |
| 3569 | /* and wait for machine initialization */ |
| 3570 | while (!qemu_system_ready) |
| 3571 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3572 | |
| 3573 | while (1) { |
| 3574 | if (cpu_can_run(env)) |
| 3575 | qemu_cpu_exec(env); |
Anthony Liguori | 1c3173b | 2009-09-10 08:45:43 -0500 | [diff] [blame] | 3576 | qemu_wait_io_event(env); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | return NULL; |
| 3580 | } |
| 3581 | |
| 3582 | static void tcg_cpu_exec(void); |
| 3583 | |
| 3584 | static void *tcg_cpu_thread_fn(void *arg) |
| 3585 | { |
| 3586 | CPUState *env = arg; |
| 3587 | |
| 3588 | block_io_signals(); |
| 3589 | qemu_thread_self(env->thread); |
| 3590 | |
| 3591 | /* signal CPU creation */ |
| 3592 | qemu_mutex_lock(&qemu_global_mutex); |
| 3593 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 3594 | env->created = 1; |
| 3595 | qemu_cond_signal(&qemu_cpu_cond); |
| 3596 | |
| 3597 | /* and wait for machine initialization */ |
| 3598 | while (!qemu_system_ready) |
| 3599 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3600 | |
| 3601 | while (1) { |
| 3602 | tcg_cpu_exec(); |
| 3603 | qemu_wait_io_event(cur_cpu); |
| 3604 | } |
| 3605 | |
| 3606 | return NULL; |
| 3607 | } |
| 3608 | |
| 3609 | void qemu_cpu_kick(void *_env) |
| 3610 | { |
| 3611 | CPUState *env = _env; |
| 3612 | qemu_cond_broadcast(env->halt_cond); |
| 3613 | if (kvm_enabled()) |
| 3614 | qemu_thread_signal(env->thread, SIGUSR1); |
| 3615 | } |
| 3616 | |
Glauber Costa | e5bc201 | 2009-09-28 15:27:44 -0300 | [diff] [blame] | 3617 | int qemu_cpu_self(void *_env) |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3618 | { |
Glauber Costa | e5bc201 | 2009-09-28 15:27:44 -0300 | [diff] [blame] | 3619 | CPUState *env = _env; |
| 3620 | QemuThread this; |
| 3621 | |
| 3622 | qemu_thread_self(&this); |
| 3623 | |
| 3624 | return qemu_thread_equal(&this, env->thread); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | static void cpu_signal(int sig) |
| 3628 | { |
| 3629 | if (cpu_single_env) |
| 3630 | cpu_exit(cpu_single_env); |
| 3631 | } |
| 3632 | |
| 3633 | static void block_io_signals(void) |
| 3634 | { |
| 3635 | sigset_t set; |
| 3636 | struct sigaction sigact; |
| 3637 | |
| 3638 | sigemptyset(&set); |
| 3639 | sigaddset(&set, SIGUSR2); |
| 3640 | sigaddset(&set, SIGIO); |
| 3641 | sigaddset(&set, SIGALRM); |
| 3642 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3643 | |
| 3644 | sigemptyset(&set); |
| 3645 | sigaddset(&set, SIGUSR1); |
| 3646 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3647 | |
| 3648 | memset(&sigact, 0, sizeof(sigact)); |
| 3649 | sigact.sa_handler = cpu_signal; |
| 3650 | sigaction(SIGUSR1, &sigact, NULL); |
| 3651 | } |
| 3652 | |
| 3653 | static void unblock_io_signals(void) |
| 3654 | { |
| 3655 | sigset_t set; |
| 3656 | |
| 3657 | sigemptyset(&set); |
| 3658 | sigaddset(&set, SIGUSR2); |
| 3659 | sigaddset(&set, SIGIO); |
| 3660 | sigaddset(&set, SIGALRM); |
| 3661 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3662 | |
| 3663 | sigemptyset(&set); |
| 3664 | sigaddset(&set, SIGUSR1); |
| 3665 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3666 | } |
| 3667 | |
| 3668 | static void qemu_signal_lock(unsigned int msecs) |
| 3669 | { |
| 3670 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3671 | |
| 3672 | while (qemu_mutex_trylock(&qemu_global_mutex)) { |
| 3673 | qemu_thread_signal(tcg_cpu_thread, SIGUSR1); |
| 3674 | if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs)) |
| 3675 | break; |
| 3676 | } |
| 3677 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3678 | } |
| 3679 | |
Glauber Costa | d549db5 | 2009-10-07 16:38:03 -0300 | [diff] [blame] | 3680 | void qemu_mutex_lock_iothread(void) |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3681 | { |
| 3682 | if (kvm_enabled()) { |
| 3683 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3684 | qemu_mutex_lock(&qemu_global_mutex); |
| 3685 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3686 | } else |
| 3687 | qemu_signal_lock(100); |
| 3688 | } |
| 3689 | |
Glauber Costa | d549db5 | 2009-10-07 16:38:03 -0300 | [diff] [blame] | 3690 | void qemu_mutex_unlock_iothread(void) |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3691 | { |
| 3692 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3693 | } |
| 3694 | |
| 3695 | static int all_vcpus_paused(void) |
| 3696 | { |
| 3697 | CPUState *penv = first_cpu; |
| 3698 | |
| 3699 | while (penv) { |
| 3700 | if (!penv->stopped) |
| 3701 | return 0; |
| 3702 | penv = (CPUState *)penv->next_cpu; |
| 3703 | } |
| 3704 | |
| 3705 | return 1; |
| 3706 | } |
| 3707 | |
| 3708 | static void pause_all_vcpus(void) |
| 3709 | { |
| 3710 | CPUState *penv = first_cpu; |
| 3711 | |
| 3712 | while (penv) { |
| 3713 | penv->stop = 1; |
| 3714 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3715 | qemu_cpu_kick(penv); |
| 3716 | penv = (CPUState *)penv->next_cpu; |
| 3717 | } |
| 3718 | |
| 3719 | while (!all_vcpus_paused()) { |
| 3720 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); |
| 3721 | penv = first_cpu; |
| 3722 | while (penv) { |
| 3723 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3724 | penv = (CPUState *)penv->next_cpu; |
| 3725 | } |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | static void resume_all_vcpus(void) |
| 3730 | { |
| 3731 | CPUState *penv = first_cpu; |
| 3732 | |
| 3733 | while (penv) { |
| 3734 | penv->stop = 0; |
| 3735 | penv->stopped = 0; |
| 3736 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3737 | qemu_cpu_kick(penv); |
| 3738 | penv = (CPUState *)penv->next_cpu; |
| 3739 | } |
| 3740 | } |
| 3741 | |
| 3742 | static void tcg_init_vcpu(void *_env) |
| 3743 | { |
| 3744 | CPUState *env = _env; |
| 3745 | /* share a single thread for all cpus with TCG */ |
| 3746 | if (!tcg_cpu_thread) { |
| 3747 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3748 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3749 | qemu_cond_init(env->halt_cond); |
| 3750 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
| 3751 | while (env->created == 0) |
| 3752 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3753 | tcg_cpu_thread = env->thread; |
| 3754 | tcg_halt_cond = env->halt_cond; |
| 3755 | } else { |
| 3756 | env->thread = tcg_cpu_thread; |
| 3757 | env->halt_cond = tcg_halt_cond; |
| 3758 | } |
| 3759 | } |
| 3760 | |
| 3761 | static void kvm_start_vcpu(CPUState *env) |
| 3762 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3763 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3764 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3765 | qemu_cond_init(env->halt_cond); |
| 3766 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
| 3767 | while (env->created == 0) |
| 3768 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3769 | } |
| 3770 | |
| 3771 | void qemu_init_vcpu(void *_env) |
| 3772 | { |
| 3773 | CPUState *env = _env; |
| 3774 | |
| 3775 | if (kvm_enabled()) |
| 3776 | kvm_start_vcpu(env); |
| 3777 | else |
| 3778 | tcg_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3779 | env->nr_cores = smp_cores; |
| 3780 | env->nr_threads = smp_threads; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | void qemu_notify_event(void) |
| 3784 | { |
| 3785 | qemu_event_increment(); |
| 3786 | } |
| 3787 | |
| 3788 | void vm_stop(int reason) |
| 3789 | { |
| 3790 | QemuThread me; |
| 3791 | qemu_thread_self(&me); |
| 3792 | |
| 3793 | if (!qemu_thread_equal(&me, &io_thread)) { |
| 3794 | qemu_system_vmstop_request(reason); |
| 3795 | /* |
| 3796 | * FIXME: should not return to device code in case |
| 3797 | * vm_stop() has been requested. |
| 3798 | */ |
| 3799 | if (cpu_single_env) { |
| 3800 | cpu_exit(cpu_single_env); |
| 3801 | cpu_single_env->stop = 1; |
| 3802 | } |
| 3803 | return; |
| 3804 | } |
| 3805 | do_vm_stop(reason); |
| 3806 | } |
| 3807 | |
| 3808 | #endif |
| 3809 | |
| 3810 | |
ths | 877cf88 | 2007-04-18 18:11:47 +0000 | [diff] [blame] | 3811 | #ifdef _WIN32 |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3812 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3813 | { |
| 3814 | int ret, ret2, i; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3815 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3816 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3817 | |
| 3818 | /* XXX: need to suppress polling by better using win32 events */ |
| 3819 | ret = 0; |
| 3820 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 3821 | ret |= pe->func(pe->opaque); |
| 3822 | } |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3823 | if (ret == 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3824 | int err; |
| 3825 | WaitObjects *w = &wait_objects; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3826 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3827 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3828 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 3829 | if (w->func[ret - WAIT_OBJECT_0]) |
| 3830 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3831 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3832 | /* Check for additional signaled events */ |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3833 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3834 | |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3835 | /* Check if event is signaled */ |
| 3836 | ret2 = WaitForSingleObject(w->events[i], 0); |
| 3837 | if(ret2 == WAIT_OBJECT_0) { |
| 3838 | if (w->func[i]) |
| 3839 | w->func[i](w->opaque[i]); |
| 3840 | } else if (ret2 == WAIT_TIMEOUT) { |
| 3841 | } else { |
| 3842 | err = GetLastError(); |
| 3843 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3844 | } |
| 3845 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3846 | } else if (ret == WAIT_TIMEOUT) { |
| 3847 | } else { |
| 3848 | err = GetLastError(); |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3849 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3850 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3851 | } |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3852 | |
| 3853 | *timeout = 0; |
| 3854 | } |
| 3855 | #else |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3856 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3857 | { |
| 3858 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3859 | #endif |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3860 | |
| 3861 | void main_loop_wait(int timeout) |
| 3862 | { |
| 3863 | IOHandlerRecord *ioh; |
| 3864 | fd_set rfds, wfds, xfds; |
| 3865 | int ret, nfds; |
| 3866 | struct timeval tv; |
| 3867 | |
| 3868 | qemu_bh_update_timeout(&timeout); |
| 3869 | |
| 3870 | host_main_loop_wait(&timeout); |
| 3871 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3872 | /* poll any events */ |
| 3873 | /* XXX: separate device handlers from system ones */ |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 3874 | nfds = -1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3875 | FD_ZERO(&rfds); |
| 3876 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 3877 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3878 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3879 | if (ioh->deleted) |
| 3880 | continue; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3881 | if (ioh->fd_read && |
| 3882 | (!ioh->fd_read_poll || |
| 3883 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 3884 | FD_SET(ioh->fd, &rfds); |
| 3885 | if (ioh->fd > nfds) |
| 3886 | nfds = ioh->fd; |
| 3887 | } |
| 3888 | if (ioh->fd_write) { |
| 3889 | FD_SET(ioh->fd, &wfds); |
| 3890 | if (ioh->fd > nfds) |
| 3891 | nfds = ioh->fd; |
| 3892 | } |
| 3893 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3894 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3895 | tv.tv_sec = timeout / 1000; |
| 3896 | tv.tv_usec = (timeout % 1000) * 1000; |
| 3897 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 3898 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 3899 | |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3900 | qemu_mutex_unlock_iothread(); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 3901 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3902 | qemu_mutex_lock_iothread(); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3903 | if (ret > 0) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3904 | IOHandlerRecord **pioh; |
| 3905 | |
| 3906 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 3907 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3908 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3909 | } |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 3910 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3911 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 3912 | } |
| 3913 | } |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3914 | |
| 3915 | /* remove deleted IO handlers */ |
| 3916 | pioh = &first_io_handler; |
| 3917 | while (*pioh) { |
| 3918 | ioh = *pioh; |
| 3919 | if (ioh->deleted) { |
| 3920 | *pioh = ioh->next; |
| 3921 | qemu_free(ioh); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3922 | } else |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3923 | pioh = &ioh->next; |
| 3924 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3925 | } |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 3926 | |
| 3927 | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3928 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3929 | /* rearm timer, if not periodic */ |
| 3930 | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) { |
| 3931 | alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; |
| 3932 | qemu_rearm_alarm_timer(alarm_timer); |
| 3933 | } |
| 3934 | |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 3935 | /* vm time timers */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3936 | if (vm_running) { |
| 3937 | if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER))) |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 3938 | qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL], |
| 3939 | qemu_get_clock(vm_clock)); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3940 | } |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 3941 | |
| 3942 | /* real time timers */ |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 3943 | qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME], |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 3944 | qemu_get_clock(rt_clock)); |
| 3945 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 3946 | qemu_run_timers(&active_timers[QEMU_CLOCK_HOST], |
| 3947 | qemu_get_clock(host_clock)); |
| 3948 | |
pbrook | 423f074 | 2007-05-23 00:06:54 +0000 | [diff] [blame] | 3949 | /* Check bottom-halves last in case any of the earlier events triggered |
| 3950 | them. */ |
| 3951 | qemu_bh_poll(); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3952 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3955 | static int qemu_cpu_exec(CPUState *env) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 3956 | { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3957 | int ret; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 3958 | #ifdef CONFIG_PROFILER |
| 3959 | int64_t ti; |
| 3960 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 3961 | |
| 3962 | #ifdef CONFIG_PROFILER |
| 3963 | ti = profile_getclock(); |
| 3964 | #endif |
| 3965 | if (use_icount) { |
| 3966 | int64_t count; |
| 3967 | int decr; |
| 3968 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 3969 | env->icount_decr.u16.low = 0; |
| 3970 | env->icount_extra = 0; |
| 3971 | count = qemu_next_deadline(); |
| 3972 | count = (count + (1 << icount_time_shift) - 1) |
| 3973 | >> icount_time_shift; |
| 3974 | qemu_icount += count; |
| 3975 | decr = (count > 0xffff) ? 0xffff : count; |
| 3976 | count -= decr; |
| 3977 | env->icount_decr.u16.low = decr; |
| 3978 | env->icount_extra = count; |
| 3979 | } |
| 3980 | ret = cpu_exec(env); |
| 3981 | #ifdef CONFIG_PROFILER |
| 3982 | qemu_time += profile_getclock() - ti; |
| 3983 | #endif |
| 3984 | if (use_icount) { |
| 3985 | /* Fold pending instructions back into the |
| 3986 | instruction counter, and clear the interrupt flag. */ |
| 3987 | qemu_icount -= (env->icount_decr.u16.low |
| 3988 | + env->icount_extra); |
| 3989 | env->icount_decr.u32 = 0; |
| 3990 | env->icount_extra = 0; |
| 3991 | } |
| 3992 | return ret; |
| 3993 | } |
| 3994 | |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 3995 | static void tcg_cpu_exec(void) |
| 3996 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3997 | int ret = 0; |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 3998 | |
| 3999 | if (next_cpu == NULL) |
| 4000 | next_cpu = first_cpu; |
| 4001 | for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { |
| 4002 | CPUState *env = cur_cpu = next_cpu; |
| 4003 | |
| 4004 | if (!vm_running) |
| 4005 | break; |
| 4006 | if (timer_alarm_pending) { |
| 4007 | timer_alarm_pending = 0; |
| 4008 | break; |
| 4009 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4010 | if (cpu_can_run(env)) |
| 4011 | ret = qemu_cpu_exec(env); |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4012 | if (ret == EXCP_DEBUG) { |
| 4013 | gdb_set_stop_cpu(env); |
| 4014 | debug_requested = 1; |
| 4015 | break; |
| 4016 | } |
| 4017 | } |
| 4018 | } |
| 4019 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4020 | static int cpu_has_work(CPUState *env) |
| 4021 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4022 | if (env->stop) |
| 4023 | return 1; |
| 4024 | if (env->stopped) |
| 4025 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4026 | if (!env->halted) |
| 4027 | return 1; |
| 4028 | if (qemu_cpu_has_work(env)) |
| 4029 | return 1; |
| 4030 | return 0; |
| 4031 | } |
| 4032 | |
| 4033 | static int tcg_has_work(void) |
| 4034 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 4035 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4036 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4037 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 4038 | if (cpu_has_work(env)) |
| 4039 | return 1; |
| 4040 | return 0; |
| 4041 | } |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4042 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4043 | static int qemu_calculate_timeout(void) |
| 4044 | { |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4045 | #ifndef CONFIG_IOTHREAD |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4046 | int timeout; |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4047 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4048 | if (!vm_running) |
| 4049 | timeout = 5000; |
| 4050 | else if (tcg_has_work()) |
| 4051 | timeout = 0; |
| 4052 | else if (!use_icount) |
| 4053 | timeout = 5000; |
| 4054 | else { |
| 4055 | /* XXX: use timeout computed from timers */ |
| 4056 | int64_t add; |
| 4057 | int64_t delta; |
| 4058 | /* Advance virtual time to the next event. */ |
| 4059 | if (use_icount == 1) { |
| 4060 | /* When not using an adaptive execution frequency |
| 4061 | we tend to get badly out of sync with real time, |
| 4062 | so just delay for a reasonable amount of time. */ |
| 4063 | delta = 0; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4064 | } else { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4065 | delta = cpu_get_icount() - cpu_get_clock(); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4066 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4067 | if (delta > 0) { |
| 4068 | /* If virtual time is ahead of real time then just |
| 4069 | wait for IO. */ |
| 4070 | timeout = (delta / 1000000) + 1; |
| 4071 | } else { |
| 4072 | /* Wait for either IO to occur or the next |
| 4073 | timer event. */ |
| 4074 | add = qemu_next_deadline(); |
| 4075 | /* We advance the timer before checking for IO. |
| 4076 | Limit the amount we advance so that early IO |
| 4077 | activity won't get the guest too far ahead. */ |
| 4078 | if (add > 10000000) |
| 4079 | add = 10000000; |
| 4080 | delta += add; |
| 4081 | add = (add + (1 << icount_time_shift) - 1) |
| 4082 | >> icount_time_shift; |
| 4083 | qemu_icount += add; |
| 4084 | timeout = delta / 1000000; |
| 4085 | if (timeout < 0) |
| 4086 | timeout = 0; |
| 4087 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4088 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4089 | |
| 4090 | return timeout; |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4091 | #else /* CONFIG_IOTHREAD */ |
| 4092 | return 1000; |
| 4093 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | static int vm_can_run(void) |
| 4097 | { |
| 4098 | if (powerdown_requested) |
| 4099 | return 0; |
| 4100 | if (reset_requested) |
| 4101 | return 0; |
| 4102 | if (shutdown_requested) |
| 4103 | return 0; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4104 | if (debug_requested) |
| 4105 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4106 | return 1; |
| 4107 | } |
| 4108 | |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4109 | qemu_irq qemu_system_powerdown; |
| 4110 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4111 | static void main_loop(void) |
| 4112 | { |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4113 | int r; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4114 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4115 | #ifdef CONFIG_IOTHREAD |
| 4116 | qemu_system_ready = 1; |
| 4117 | qemu_cond_broadcast(&qemu_system_cond); |
| 4118 | #endif |
| 4119 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4120 | for (;;) { |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4121 | do { |
| 4122 | #ifdef CONFIG_PROFILER |
| 4123 | int64_t ti; |
| 4124 | #endif |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4125 | #ifndef CONFIG_IOTHREAD |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4126 | tcg_cpu_exec(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4127 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4128 | #ifdef CONFIG_PROFILER |
| 4129 | ti = profile_getclock(); |
| 4130 | #endif |
| 4131 | main_loop_wait(qemu_calculate_timeout()); |
| 4132 | #ifdef CONFIG_PROFILER |
| 4133 | dev_time += profile_getclock() - ti; |
| 4134 | #endif |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4135 | } while (vm_can_run()); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4136 | |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4137 | if (qemu_debug_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4138 | monitor_protocol_event(QEVENT_DEBUG, NULL); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4139 | vm_stop(EXCP_DEBUG); |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4140 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4141 | if (qemu_shutdown_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4142 | monitor_protocol_event(QEVENT_SHUTDOWN, NULL); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4143 | if (no_shutdown) { |
| 4144 | vm_stop(0); |
| 4145 | no_shutdown = 0; |
| 4146 | } else |
| 4147 | break; |
| 4148 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4149 | if (qemu_reset_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4150 | monitor_protocol_event(QEVENT_RESET, NULL); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4151 | pause_all_vcpus(); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4152 | qemu_system_reset(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4153 | resume_all_vcpus(); |
| 4154 | } |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4155 | if (qemu_powerdown_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4156 | monitor_protocol_event(QEVENT_POWERDOWN, NULL); |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4157 | qemu_irq_raise(qemu_system_powerdown); |
| 4158 | } |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4159 | if ((r = qemu_vmstop_requested())) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4160 | monitor_protocol_event(QEVENT_STOP, NULL); |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4161 | vm_stop(r); |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4162 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4163 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4164 | pause_all_vcpus(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4167 | static void version(void) |
| 4168 | { |
pbrook | 4a19f1e | 2009-04-07 23:17:49 +0000 | [diff] [blame] | 4169 | 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] | 4170 | } |
| 4171 | |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4172 | static void help(int exitcode) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4173 | { |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4174 | version(); |
| 4175 | printf("usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4176 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4177 | "'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] | 4178 | "\n" |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4179 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4180 | opt_help |
| 4181 | #define DEFHEADING(text) stringify(text) "\n" |
| 4182 | #include "qemu-options.h" |
| 4183 | #undef DEF |
| 4184 | #undef DEFHEADING |
| 4185 | #undef GEN_DOCS |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4186 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4187 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 4188 | "ctrl-alt-f toggle full screen\n" |
| 4189 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 4190 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4191 | "\n" |
| 4192 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 4193 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 4194 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4195 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4196 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4197 | DEFAULT_NETWORK_SCRIPT, |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4198 | DEFAULT_NETWORK_DOWN_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4199 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 4200 | DEFAULT_GDBSTUB_PORT, |
bellard | bce6184 | 2008-02-01 22:18:51 +0000 | [diff] [blame] | 4201 | "/tmp/qemu.log"); |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4202 | exit(exitcode); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4205 | #define HAS_ARG 0x0001 |
| 4206 | |
| 4207 | enum { |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4208 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4209 | opt_enum, |
| 4210 | #define DEFHEADING(text) |
| 4211 | #include "qemu-options.h" |
| 4212 | #undef DEF |
| 4213 | #undef DEFHEADING |
| 4214 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4215 | }; |
| 4216 | |
| 4217 | typedef struct QEMUOption { |
| 4218 | const char *name; |
| 4219 | int flags; |
| 4220 | int index; |
| 4221 | } QEMUOption; |
| 4222 | |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 4223 | static const QEMUOption qemu_options[] = { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4224 | { "h", 0, QEMU_OPTION_h }, |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4225 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4226 | { option, opt_arg, opt_enum }, |
| 4227 | #define DEFHEADING(text) |
| 4228 | #include "qemu-options.h" |
| 4229 | #undef DEF |
| 4230 | #undef DEFHEADING |
| 4231 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4232 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4233 | }; |
| 4234 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4235 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4236 | struct soundhw soundhw[] = { |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4237 | #ifdef HAS_AUDIO_CHOICE |
aurel32 | 4ce7ff6 | 2008-04-07 19:47:14 +0000 | [diff] [blame] | 4238 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 4239 | { |
| 4240 | "pcspk", |
| 4241 | "PC speaker", |
| 4242 | 0, |
| 4243 | 1, |
| 4244 | { .init_isa = pcspk_audio_init } |
| 4245 | }, |
| 4246 | #endif |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4247 | |
| 4248 | #ifdef CONFIG_SB16 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4249 | { |
| 4250 | "sb16", |
| 4251 | "Creative Sound Blaster 16", |
| 4252 | 0, |
| 4253 | 1, |
| 4254 | { .init_isa = SB16_init } |
| 4255 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4256 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4257 | |
malc | cc53d26 | 2008-06-13 10:48:22 +0000 | [diff] [blame] | 4258 | #ifdef CONFIG_CS4231A |
| 4259 | { |
| 4260 | "cs4231a", |
| 4261 | "CS4231A", |
| 4262 | 0, |
| 4263 | 1, |
| 4264 | { .init_isa = cs4231a_init } |
| 4265 | }, |
| 4266 | #endif |
| 4267 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4268 | #ifdef CONFIG_ADLIB |
| 4269 | { |
| 4270 | "adlib", |
| 4271 | #ifdef HAS_YMF262 |
| 4272 | "Yamaha YMF262 (OPL3)", |
| 4273 | #else |
| 4274 | "Yamaha YM3812 (OPL2)", |
| 4275 | #endif |
| 4276 | 0, |
| 4277 | 1, |
| 4278 | { .init_isa = Adlib_init } |
| 4279 | }, |
| 4280 | #endif |
| 4281 | |
| 4282 | #ifdef CONFIG_GUS |
| 4283 | { |
| 4284 | "gus", |
| 4285 | "Gravis Ultrasound GF1", |
| 4286 | 0, |
| 4287 | 1, |
| 4288 | { .init_isa = GUS_init } |
| 4289 | }, |
| 4290 | #endif |
| 4291 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4292 | #ifdef CONFIG_AC97 |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4293 | { |
| 4294 | "ac97", |
| 4295 | "Intel 82801AA AC97 Audio", |
| 4296 | 0, |
| 4297 | 0, |
| 4298 | { .init_pci = ac97_init } |
| 4299 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4300 | #endif |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4301 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4302 | #ifdef CONFIG_ES1370 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4303 | { |
| 4304 | "es1370", |
| 4305 | "ENSONIQ AudioPCI ES1370", |
| 4306 | 0, |
| 4307 | 0, |
| 4308 | { .init_pci = es1370_init } |
| 4309 | }, |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4310 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4311 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4312 | #endif /* HAS_AUDIO_CHOICE */ |
| 4313 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4314 | { NULL, NULL, 0, 0, { NULL } } |
| 4315 | }; |
| 4316 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4317 | static void select_soundhw (const char *optarg) |
| 4318 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4319 | struct soundhw *c; |
| 4320 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4321 | if (*optarg == '?') { |
| 4322 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4323 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4324 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4325 | for (c = soundhw; c->name; ++c) { |
| 4326 | printf ("%-11s %s\n", c->name, c->descr); |
| 4327 | } |
| 4328 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4329 | exit (*optarg != '?'); |
| 4330 | } |
| 4331 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4332 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4333 | const char *p; |
| 4334 | char *e; |
| 4335 | int bad_card = 0; |
| 4336 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4337 | if (!strcmp (optarg, "all")) { |
| 4338 | for (c = soundhw; c->name; ++c) { |
| 4339 | c->enabled = 1; |
| 4340 | } |
| 4341 | return; |
| 4342 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4343 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4344 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4345 | while (*p) { |
| 4346 | e = strchr (p, ','); |
| 4347 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4348 | |
| 4349 | for (c = soundhw; c->name; ++c) { |
malc | b3d6fb4 | 2009-09-06 06:49:03 +0400 | [diff] [blame] | 4350 | if (!strncmp (c->name, p, l) && !c->name[l]) { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4351 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4352 | break; |
| 4353 | } |
| 4354 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4355 | |
| 4356 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4357 | if (l > 80) { |
| 4358 | fprintf (stderr, |
| 4359 | "Unknown sound card name (too big to show)\n"); |
| 4360 | } |
| 4361 | else { |
| 4362 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 4363 | (int) l, p); |
| 4364 | } |
| 4365 | bad_card = 1; |
| 4366 | } |
| 4367 | p += l + (e != NULL); |
| 4368 | } |
| 4369 | |
| 4370 | if (bad_card) |
| 4371 | goto show_valid_cards; |
| 4372 | } |
| 4373 | } |
| 4374 | #endif |
| 4375 | |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4376 | static void select_vgahw (const char *p) |
| 4377 | { |
| 4378 | const char *opts; |
| 4379 | |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 4380 | default_vga = 0; |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4381 | vga_interface_type = VGA_NONE; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4382 | if (strstart(p, "std", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4383 | vga_interface_type = VGA_STD; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4384 | } else if (strstart(p, "cirrus", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4385 | vga_interface_type = VGA_CIRRUS; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4386 | } else if (strstart(p, "vmware", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4387 | vga_interface_type = VGA_VMWARE; |
aliguori | 94909d9 | 2009-04-22 15:19:53 +0000 | [diff] [blame] | 4388 | } else if (strstart(p, "xenfb", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4389 | vga_interface_type = VGA_XENFB; |
aliguori | 28b85ed | 2009-04-22 15:19:48 +0000 | [diff] [blame] | 4390 | } else if (!strstart(p, "none", &opts)) { |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4391 | invalid_vga: |
| 4392 | fprintf(stderr, "Unknown vga type: %s\n", p); |
| 4393 | exit(1); |
| 4394 | } |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 4395 | while (*opts) { |
| 4396 | const char *nextopt; |
| 4397 | |
| 4398 | if (strstart(opts, ",retrace=", &nextopt)) { |
| 4399 | opts = nextopt; |
| 4400 | if (strstart(opts, "dumb", &nextopt)) |
| 4401 | vga_retrace_method = VGA_RETRACE_DUMB; |
| 4402 | else if (strstart(opts, "precise", &nextopt)) |
| 4403 | vga_retrace_method = VGA_RETRACE_PRECISE; |
| 4404 | else goto invalid_vga; |
| 4405 | } else goto invalid_vga; |
| 4406 | opts = nextopt; |
| 4407 | } |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4408 | } |
| 4409 | |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4410 | #ifdef TARGET_I386 |
| 4411 | static int balloon_parse(const char *arg) |
| 4412 | { |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4413 | QemuOpts *opts; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4414 | |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4415 | if (strcmp(arg, "none") == 0) { |
| 4416 | return 0; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4417 | } |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4418 | |
| 4419 | if (!strncmp(arg, "virtio", 6)) { |
| 4420 | if (arg[6] == ',') { |
| 4421 | /* have params -> parse them */ |
| 4422 | opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL); |
| 4423 | if (!opts) |
| 4424 | return -1; |
| 4425 | } else { |
| 4426 | /* create empty opts */ |
| 4427 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 4428 | } |
| 4429 | qemu_opt_set(opts, "driver", "virtio-balloon-pci"); |
| 4430 | return 0; |
| 4431 | } |
| 4432 | |
| 4433 | return -1; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4434 | } |
| 4435 | #endif |
| 4436 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4437 | #ifdef _WIN32 |
| 4438 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 4439 | { |
| 4440 | exit(STATUS_CONTROL_C_EXIT); |
| 4441 | return TRUE; |
| 4442 | } |
| 4443 | #endif |
| 4444 | |
aliguori | c4be29f | 2009-04-17 18:58:14 +0000 | [diff] [blame] | 4445 | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4446 | { |
| 4447 | int ret; |
| 4448 | |
| 4449 | if(strlen(str) != 36) |
| 4450 | return -1; |
| 4451 | |
| 4452 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], |
| 4453 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], |
| 4454 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); |
| 4455 | |
| 4456 | if(ret != 16) |
| 4457 | return -1; |
| 4458 | |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 4459 | #ifdef TARGET_I386 |
| 4460 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); |
| 4461 | #endif |
| 4462 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4463 | return 0; |
| 4464 | } |
| 4465 | |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4466 | #ifndef _WIN32 |
| 4467 | |
| 4468 | static void termsig_handler(int signal) |
| 4469 | { |
| 4470 | qemu_system_shutdown_request(); |
| 4471 | } |
| 4472 | |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4473 | static void sigchld_handler(int signal) |
| 4474 | { |
| 4475 | waitpid(-1, NULL, WNOHANG); |
| 4476 | } |
| 4477 | |
| 4478 | static void sighandler_setup(void) |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4479 | { |
| 4480 | struct sigaction act; |
| 4481 | |
| 4482 | memset(&act, 0, sizeof(act)); |
| 4483 | act.sa_handler = termsig_handler; |
| 4484 | sigaction(SIGINT, &act, NULL); |
| 4485 | sigaction(SIGHUP, &act, NULL); |
| 4486 | sigaction(SIGTERM, &act, NULL); |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4487 | |
| 4488 | act.sa_handler = sigchld_handler; |
| 4489 | act.sa_flags = SA_NOCLDSTOP; |
| 4490 | sigaction(SIGCHLD, &act, NULL); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | #endif |
| 4494 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4495 | #ifdef _WIN32 |
| 4496 | /* Look for support files in the same directory as the executable. */ |
| 4497 | static char *find_datadir(const char *argv0) |
| 4498 | { |
| 4499 | char *p; |
| 4500 | char buf[MAX_PATH]; |
| 4501 | DWORD len; |
| 4502 | |
| 4503 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
| 4504 | if (len == 0) { |
Blue Swirl | c594780 | 2009-06-09 20:51:21 +0300 | [diff] [blame] | 4505 | return NULL; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | buf[len] = 0; |
| 4509 | p = buf + len - 1; |
| 4510 | while (p != buf && *p != '\\') |
| 4511 | p--; |
| 4512 | *p = 0; |
| 4513 | if (access(buf, R_OK) == 0) { |
| 4514 | return qemu_strdup(buf); |
| 4515 | } |
| 4516 | return NULL; |
| 4517 | } |
| 4518 | #else /* !_WIN32 */ |
| 4519 | |
| 4520 | /* Find a likely location for support files using the location of the binary. |
| 4521 | For installed binaries this will be "$bindir/../share/qemu". When |
| 4522 | running from the build tree this will be "$bindir/../pc-bios". */ |
| 4523 | #define SHARE_SUFFIX "/share/qemu" |
| 4524 | #define BUILD_SUFFIX "/pc-bios" |
| 4525 | static char *find_datadir(const char *argv0) |
| 4526 | { |
| 4527 | char *dir; |
| 4528 | char *p = NULL; |
| 4529 | char *res; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4530 | char buf[PATH_MAX]; |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4531 | size_t max_len; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4532 | |
| 4533 | #if defined(__linux__) |
| 4534 | { |
| 4535 | int len; |
| 4536 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| 4537 | if (len > 0) { |
| 4538 | buf[len] = 0; |
| 4539 | p = buf; |
| 4540 | } |
| 4541 | } |
| 4542 | #elif defined(__FreeBSD__) |
| 4543 | { |
| 4544 | int len; |
| 4545 | len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); |
| 4546 | if (len > 0) { |
| 4547 | buf[len] = 0; |
| 4548 | p = buf; |
| 4549 | } |
| 4550 | } |
| 4551 | #endif |
| 4552 | /* If we don't have any way of figuring out the actual executable |
| 4553 | location then try argv[0]. */ |
| 4554 | if (!p) { |
Jean-Christophe DUBOIS | 4d22419 | 2009-09-02 23:59:02 +0200 | [diff] [blame] | 4555 | p = realpath(argv0, buf); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4556 | if (!p) { |
| 4557 | return NULL; |
| 4558 | } |
| 4559 | } |
| 4560 | dir = dirname(p); |
| 4561 | dir = dirname(dir); |
| 4562 | |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4563 | max_len = strlen(dir) + |
| 4564 | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; |
| 4565 | res = qemu_mallocz(max_len); |
| 4566 | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4567 | if (access(res, R_OK)) { |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4568 | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4569 | if (access(res, R_OK)) { |
| 4570 | qemu_free(res); |
| 4571 | res = NULL; |
| 4572 | } |
| 4573 | } |
Jean-Christophe DUBOIS | 4d22419 | 2009-09-02 23:59:02 +0200 | [diff] [blame] | 4574 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4575 | return res; |
| 4576 | } |
| 4577 | #undef SHARE_SUFFIX |
| 4578 | #undef BUILD_SUFFIX |
| 4579 | #endif |
| 4580 | |
| 4581 | char *qemu_find_file(int type, const char *name) |
| 4582 | { |
| 4583 | int len; |
| 4584 | const char *subdir; |
| 4585 | char *buf; |
| 4586 | |
| 4587 | /* If name contains path separators then try it as a straight path. */ |
| 4588 | if ((strchr(name, '/') || strchr(name, '\\')) |
| 4589 | && access(name, R_OK) == 0) { |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 4590 | return qemu_strdup(name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4591 | } |
| 4592 | switch (type) { |
| 4593 | case QEMU_FILE_TYPE_BIOS: |
| 4594 | subdir = ""; |
| 4595 | break; |
| 4596 | case QEMU_FILE_TYPE_KEYMAP: |
| 4597 | subdir = "keymaps/"; |
| 4598 | break; |
| 4599 | default: |
| 4600 | abort(); |
| 4601 | } |
| 4602 | len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2; |
| 4603 | buf = qemu_mallocz(len); |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4604 | snprintf(buf, len, "%s/%s%s", data_dir, subdir, name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4605 | if (access(buf, R_OK)) { |
| 4606 | qemu_free(buf); |
| 4607 | return NULL; |
| 4608 | } |
| 4609 | return buf; |
| 4610 | } |
| 4611 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4612 | static int device_init_func(QemuOpts *opts, void *opaque) |
| 4613 | { |
| 4614 | DeviceState *dev; |
| 4615 | |
| 4616 | dev = qdev_device_add(opts); |
| 4617 | if (!dev) |
| 4618 | return -1; |
| 4619 | return 0; |
| 4620 | } |
| 4621 | |
Gerd Hoffmann | 1a688d3 | 2009-12-08 13:11:36 +0100 | [diff] [blame] | 4622 | static int chardev_init_func(QemuOpts *opts, void *opaque) |
| 4623 | { |
| 4624 | CharDriverState *chr; |
| 4625 | |
| 4626 | chr = qemu_chr_open_opts(opts, NULL); |
| 4627 | if (!chr) |
| 4628 | return -1; |
| 4629 | return 0; |
| 4630 | } |
| 4631 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4632 | struct device_config { |
| 4633 | enum { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4634 | DEV_USB, /* -usbdevice */ |
| 4635 | DEV_BT, /* -bt */ |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 4636 | DEV_SERIAL, /* -serial */ |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 4637 | DEV_PARALLEL, /* -parallel */ |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 4638 | DEV_MONITOR, /* -monitor */ |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4639 | } type; |
| 4640 | const char *cmdline; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4641 | QTAILQ_ENTRY(device_config) next; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4642 | }; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4643 | QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs); |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4644 | |
| 4645 | static void add_device_config(int type, const char *cmdline) |
| 4646 | { |
| 4647 | struct device_config *conf; |
| 4648 | |
| 4649 | conf = qemu_mallocz(sizeof(*conf)); |
| 4650 | conf->type = type; |
| 4651 | conf->cmdline = cmdline; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4652 | QTAILQ_INSERT_TAIL(&device_configs, conf, next); |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | static int foreach_device_config(int type, int (*func)(const char *cmdline)) |
| 4656 | { |
| 4657 | struct device_config *conf; |
| 4658 | int rc; |
| 4659 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4660 | QTAILQ_FOREACH(conf, &device_configs, next) { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4661 | if (conf->type != type) |
| 4662 | continue; |
| 4663 | rc = func(conf->cmdline); |
| 4664 | if (0 != rc) |
| 4665 | return rc; |
| 4666 | } |
| 4667 | return 0; |
| 4668 | } |
| 4669 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 4670 | static int serial_parse(const char *devname) |
| 4671 | { |
| 4672 | static int index = 0; |
| 4673 | char label[32]; |
| 4674 | |
| 4675 | if (strcmp(devname, "none") == 0) |
| 4676 | return 0; |
| 4677 | if (index == MAX_SERIAL_PORTS) { |
| 4678 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 4679 | exit(1); |
| 4680 | } |
| 4681 | snprintf(label, sizeof(label), "serial%d", index); |
| 4682 | serial_hds[index] = qemu_chr_open(label, devname, NULL); |
| 4683 | if (!serial_hds[index]) { |
| 4684 | fprintf(stderr, "qemu: could not open serial device '%s': %s\n", |
| 4685 | devname, strerror(errno)); |
| 4686 | return -1; |
| 4687 | } |
| 4688 | index++; |
| 4689 | return 0; |
| 4690 | } |
| 4691 | |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 4692 | static int parallel_parse(const char *devname) |
| 4693 | { |
| 4694 | static int index = 0; |
| 4695 | char label[32]; |
| 4696 | |
| 4697 | if (strcmp(devname, "none") == 0) |
| 4698 | return 0; |
| 4699 | if (index == MAX_PARALLEL_PORTS) { |
| 4700 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 4701 | exit(1); |
| 4702 | } |
| 4703 | snprintf(label, sizeof(label), "parallel%d", index); |
| 4704 | parallel_hds[index] = qemu_chr_open(label, devname, NULL); |
| 4705 | if (!parallel_hds[index]) { |
| 4706 | fprintf(stderr, "qemu: could not open parallel device '%s': %s\n", |
| 4707 | devname, strerror(errno)); |
| 4708 | return -1; |
| 4709 | } |
| 4710 | index++; |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 4714 | static int monitor_parse(const char *devname) |
| 4715 | { |
| 4716 | static int index = 0; |
| 4717 | char label[32]; |
| 4718 | |
| 4719 | if (strcmp(devname, "none") == 0) |
| 4720 | return 0; |
| 4721 | if (index == MAX_MONITOR_DEVICES) { |
| 4722 | fprintf(stderr, "qemu: too many monitor devices\n"); |
| 4723 | exit(1); |
| 4724 | } |
| 4725 | if (index == 0) { |
| 4726 | snprintf(label, sizeof(label), "monitor"); |
| 4727 | } else { |
| 4728 | snprintf(label, sizeof(label), "monitor%d", index); |
| 4729 | } |
| 4730 | monitor_hds[index] = qemu_chr_open(label, devname, NULL); |
| 4731 | if (!monitor_hds[index]) { |
| 4732 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", |
| 4733 | devname); |
| 4734 | return -1; |
| 4735 | } |
| 4736 | index++; |
| 4737 | return 0; |
| 4738 | } |
| 4739 | |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4740 | int main(int argc, char **argv, char **envp) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4741 | { |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 4742 | const char *gdbstub_dev = NULL; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4743 | uint32_t boot_devices_bitmap = 0; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4744 | int i; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4745 | int snapshot, linux_boot, net_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 4746 | const char *initrd_filename; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4747 | const char *kernel_filename, *kernel_cmdline; |
Anthony Liguori | 195325a | 2009-10-30 12:42:29 -0500 | [diff] [blame] | 4748 | char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 4749 | DisplayState *ds; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 4750 | DisplayChangeListener *dcl; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4751 | int cyls, heads, secs, translation; |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4752 | QemuOpts *hda_opts = NULL, *opts; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4753 | int optind; |
| 4754 | const char *r, *optarg; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 4755 | const char *virtio_consoles[MAX_VIRTIO_CONSOLES]; |
| 4756 | int virtio_console_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 4757 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4758 | QEMUMachine *machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4759 | const char *cpu_model; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4760 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 4761 | int fds[2]; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4762 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4763 | int tb_size; |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 4764 | const char *pid_file = NULL; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 4765 | const char *incoming = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4766 | #ifndef _WIN32 |
aliguori | 54042bc | 2009-02-27 22:16:47 +0000 | [diff] [blame] | 4767 | int fd = 0; |
| 4768 | struct passwd *pwd = NULL; |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 4769 | const char *chroot_dir = NULL; |
| 4770 | const char *run_as = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4771 | #endif |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4772 | CPUState *env; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 4773 | int show_vnc_port = 0; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 4774 | |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 4775 | init_clocks(); |
| 4776 | |
Gerd Hoffmann | ac7531e | 2009-08-14 10:36:06 +0200 | [diff] [blame] | 4777 | qemu_errors_to_file(stderr); |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4778 | qemu_cache_utils_init(envp); |
| 4779 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4780 | QLIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4781 | #ifndef _WIN32 |
| 4782 | { |
| 4783 | struct sigaction act; |
| 4784 | sigfillset(&act.sa_mask); |
| 4785 | act.sa_flags = 0; |
| 4786 | act.sa_handler = SIG_IGN; |
| 4787 | sigaction(SIGPIPE, &act, NULL); |
| 4788 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4789 | #else |
| 4790 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 4791 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 4792 | QEMU to run on a single CPU */ |
| 4793 | { |
| 4794 | HANDLE h; |
| 4795 | DWORD mask, smask; |
| 4796 | int i; |
| 4797 | h = GetCurrentProcess(); |
| 4798 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 4799 | for(i = 0; i < 32; i++) { |
| 4800 | if (mask & (1 << i)) |
| 4801 | break; |
| 4802 | } |
| 4803 | if (i != 32) { |
| 4804 | mask = 1 << i; |
| 4805 | SetProcessAffinityMask(h, mask); |
| 4806 | } |
| 4807 | } |
| 4808 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4809 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4810 | |
Anthony Liguori | f80f9ec | 2009-05-20 18:38:09 -0500 | [diff] [blame] | 4811 | module_call_init(MODULE_INIT_MACHINE); |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4812 | machine = find_default_machine(); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4813 | cpu_model = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4814 | initrd_filename = NULL; |
aurel32 | 4fc5d07 | 2008-04-27 21:39:40 +0000 | [diff] [blame] | 4815 | ram_size = 0; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 4816 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4817 | kernel_filename = NULL; |
| 4818 | kernel_cmdline = ""; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4819 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4820 | translation = BIOS_ATA_TRANSLATION_AUTO; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4821 | |
aliguori | 1b8fc81 | 2009-02-27 20:01:39 +0000 | [diff] [blame] | 4822 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 4823 | virtio_consoles[i] = NULL; |
| 4824 | virtio_console_index = 0; |
| 4825 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4826 | for (i = 0; i < MAX_NODES; i++) { |
| 4827 | node_mem[i] = 0; |
| 4828 | node_cpumask[i] = 0; |
| 4829 | } |
| 4830 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4831 | nb_numa_nodes = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4832 | nb_nics = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4833 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4834 | tb_size = 0; |
blueswir1 | 41bd639 | 2008-10-05 09:56:21 +0000 | [diff] [blame] | 4835 | autostart= 1; |
| 4836 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4837 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4838 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4839 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4840 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4841 | r = argv[optind]; |
| 4842 | if (r[0] != '-') { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4843 | hda_opts = drive_add(argv[optind++], HD_ALIAS, 0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4844 | } else { |
| 4845 | const QEMUOption *popt; |
| 4846 | |
| 4847 | optind++; |
pbrook | dff5efc | 2007-01-27 17:19:39 +0000 | [diff] [blame] | 4848 | /* Treat --foo the same as -foo. */ |
| 4849 | if (r[1] == '-') |
| 4850 | r++; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4851 | popt = qemu_options; |
| 4852 | for(;;) { |
| 4853 | if (!popt->name) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4854 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4855 | argv[0], r); |
| 4856 | exit(1); |
| 4857 | } |
| 4858 | if (!strcmp(popt->name, r + 1)) |
| 4859 | break; |
| 4860 | popt++; |
| 4861 | } |
| 4862 | if (popt->flags & HAS_ARG) { |
| 4863 | if (optind >= argc) { |
| 4864 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 4865 | argv[0], r); |
| 4866 | exit(1); |
| 4867 | } |
| 4868 | optarg = argv[optind++]; |
| 4869 | } else { |
| 4870 | optarg = NULL; |
| 4871 | } |
| 4872 | |
| 4873 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4874 | case QEMU_OPTION_M: |
| 4875 | machine = find_machine(optarg); |
| 4876 | if (!machine) { |
| 4877 | QEMUMachine *m; |
| 4878 | printf("Supported machines are:\n"); |
| 4879 | for(m = first_machine; m != NULL; m = m->next) { |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 4880 | if (m->alias) |
| 4881 | printf("%-10s %s (alias of %s)\n", |
| 4882 | m->alias, m->desc, m->name); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4883 | printf("%-10s %s%s\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4884 | m->name, m->desc, |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4885 | m->is_default ? " (default)" : ""); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4886 | } |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4887 | exit(*optarg != '?'); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4888 | } |
| 4889 | break; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4890 | case QEMU_OPTION_cpu: |
| 4891 | /* hw initialization will check this */ |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4892 | if (*optarg == '?') { |
j_mayer | c732abe | 2007-10-12 06:47:46 +0000 | [diff] [blame] | 4893 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 4894 | #if defined(cpu_list) |
| 4895 | cpu_list(stdout, &fprintf); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4896 | #endif |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4897 | exit(0); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4898 | } else { |
| 4899 | cpu_model = optarg; |
| 4900 | } |
| 4901 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4902 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4903 | initrd_filename = optarg; |
| 4904 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4905 | case QEMU_OPTION_hda: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4906 | if (cyls == 0) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4907 | hda_opts = drive_add(optarg, HD_ALIAS, 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4908 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4909 | hda_opts = drive_add(optarg, HD_ALIAS |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4910 | ",cyls=%d,heads=%d,secs=%d%s", |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4911 | 0, cyls, heads, secs, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4912 | translation == BIOS_ATA_TRANSLATION_LBA ? |
| 4913 | ",trans=lba" : |
| 4914 | translation == BIOS_ATA_TRANSLATION_NONE ? |
| 4915 | ",trans=none" : ""); |
| 4916 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4917 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4918 | case QEMU_OPTION_hdc: |
| 4919 | case QEMU_OPTION_hdd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4920 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4921 | break; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4922 | case QEMU_OPTION_drive: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4923 | drive_add(NULL, "%s", optarg); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4924 | break; |
Gerd Hoffmann | d058fe0 | 2009-07-31 12:25:36 +0200 | [diff] [blame] | 4925 | case QEMU_OPTION_set: |
| 4926 | if (qemu_set_option(optarg) != 0) |
| 4927 | exit(1); |
| 4928 | break; |
Gerd Hoffmann | d0fef6f | 2009-12-08 13:11:34 +0100 | [diff] [blame] | 4929 | case QEMU_OPTION_global: |
| 4930 | if (qemu_global_option(optarg) != 0) |
| 4931 | exit(1); |
| 4932 | break; |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 4933 | case QEMU_OPTION_mtdblock: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4934 | drive_add(optarg, MTD_ALIAS); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 4935 | break; |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 4936 | case QEMU_OPTION_sd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4937 | drive_add(optarg, SD_ALIAS); |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 4938 | break; |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 4939 | case QEMU_OPTION_pflash: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4940 | drive_add(optarg, PFLASH_ALIAS); |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 4941 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4942 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 4943 | snapshot = 1; |
| 4944 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4945 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4946 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4947 | const char *p; |
| 4948 | p = optarg; |
| 4949 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4950 | if (cyls < 1 || cyls > 16383) |
| 4951 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4952 | if (*p != ',') |
| 4953 | goto chs_fail; |
| 4954 | p++; |
| 4955 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4956 | if (heads < 1 || heads > 16) |
| 4957 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4958 | if (*p != ',') |
| 4959 | goto chs_fail; |
| 4960 | p++; |
| 4961 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4962 | if (secs < 1 || secs > 63) |
| 4963 | goto chs_fail; |
| 4964 | if (*p == ',') { |
| 4965 | p++; |
| 4966 | if (!strcmp(p, "none")) |
| 4967 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 4968 | else if (!strcmp(p, "lba")) |
| 4969 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 4970 | else if (!strcmp(p, "auto")) |
| 4971 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 4972 | else |
| 4973 | goto chs_fail; |
| 4974 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4975 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4976 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 4977 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4978 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4979 | if (hda_opts != NULL) { |
| 4980 | char num[16]; |
| 4981 | snprintf(num, sizeof(num), "%d", cyls); |
| 4982 | qemu_opt_set(hda_opts, "cyls", num); |
| 4983 | snprintf(num, sizeof(num), "%d", heads); |
| 4984 | qemu_opt_set(hda_opts, "heads", num); |
| 4985 | snprintf(num, sizeof(num), "%d", secs); |
| 4986 | qemu_opt_set(hda_opts, "secs", num); |
| 4987 | if (translation == BIOS_ATA_TRANSLATION_LBA) |
| 4988 | qemu_opt_set(hda_opts, "trans", "lba"); |
| 4989 | if (translation == BIOS_ATA_TRANSLATION_NONE) |
| 4990 | qemu_opt_set(hda_opts, "trans", "none"); |
| 4991 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4992 | } |
| 4993 | break; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4994 | case QEMU_OPTION_numa: |
| 4995 | if (nb_numa_nodes >= MAX_NODES) { |
| 4996 | fprintf(stderr, "qemu: too many NUMA nodes\n"); |
| 4997 | exit(1); |
| 4998 | } |
| 4999 | numa_add(optarg); |
| 5000 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5001 | case QEMU_OPTION_nographic: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5002 | display_type = DT_NOGRAPHIC; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5003 | break; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5004 | #ifdef CONFIG_CURSES |
| 5005 | case QEMU_OPTION_curses: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5006 | display_type = DT_CURSES; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5007 | break; |
| 5008 | #endif |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 5009 | case QEMU_OPTION_portrait: |
| 5010 | graphic_rotate = 1; |
| 5011 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5012 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5013 | kernel_filename = optarg; |
| 5014 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5015 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5016 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5017 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5018 | case QEMU_OPTION_cdrom: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5019 | drive_add(optarg, CDROM_ALIAS); |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5020 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5021 | case QEMU_OPTION_boot: |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5022 | { |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5023 | static const char * const params[] = { |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5024 | "order", "once", "menu", NULL |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5025 | }; |
| 5026 | char buf[sizeof(boot_devices)]; |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5027 | char *standard_boot_devices; |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5028 | int legacy = 0; |
| 5029 | |
| 5030 | if (!strchr(optarg, '=')) { |
| 5031 | legacy = 1; |
| 5032 | pstrcpy(buf, sizeof(buf), optarg); |
| 5033 | } else if (check_params(buf, sizeof(buf), params, optarg) < 0) { |
| 5034 | fprintf(stderr, |
| 5035 | "qemu: unknown boot parameter '%s' in '%s'\n", |
| 5036 | buf, optarg); |
| 5037 | exit(1); |
| 5038 | } |
| 5039 | |
| 5040 | if (legacy || |
| 5041 | get_param_value(buf, sizeof(buf), "order", optarg)) { |
| 5042 | boot_devices_bitmap = parse_bootdevices(buf); |
| 5043 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5044 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5045 | if (!legacy) { |
| 5046 | if (get_param_value(buf, sizeof(buf), |
| 5047 | "once", optarg)) { |
| 5048 | boot_devices_bitmap |= parse_bootdevices(buf); |
| 5049 | standard_boot_devices = qemu_strdup(boot_devices); |
| 5050 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
| 5051 | qemu_register_reset(restore_boot_devices, |
| 5052 | standard_boot_devices); |
| 5053 | } |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5054 | if (get_param_value(buf, sizeof(buf), |
| 5055 | "menu", optarg)) { |
| 5056 | if (!strcmp(buf, "on")) { |
| 5057 | boot_menu = 1; |
| 5058 | } else if (!strcmp(buf, "off")) { |
| 5059 | boot_menu = 0; |
| 5060 | } else { |
| 5061 | fprintf(stderr, |
| 5062 | "qemu: invalid option value '%s'\n", |
| 5063 | buf); |
| 5064 | exit(1); |
| 5065 | } |
| 5066 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5067 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5068 | } |
| 5069 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5070 | case QEMU_OPTION_fda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5071 | case QEMU_OPTION_fdb: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5072 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 5073 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 5074 | #ifdef TARGET_I386 |
| 5075 | case QEMU_OPTION_no_fd_bootchk: |
| 5076 | fd_bootchk = 0; |
| 5077 | break; |
| 5078 | #endif |
Mark McLoughlin | a1ea458 | 2009-10-08 19:58:26 +0100 | [diff] [blame] | 5079 | case QEMU_OPTION_netdev: |
| 5080 | if (net_client_parse(&qemu_netdev_opts, optarg) == -1) { |
| 5081 | exit(1); |
| 5082 | } |
| 5083 | break; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5084 | case QEMU_OPTION_net: |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 5085 | if (net_client_parse(&qemu_net_opts, optarg) == -1) { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5086 | exit(1); |
| 5087 | } |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5088 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5089 | #ifdef CONFIG_SLIRP |
| 5090 | case QEMU_OPTION_tftp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5091 | legacy_tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5092 | break; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5093 | case QEMU_OPTION_bootp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5094 | legacy_bootp_filename = optarg; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5095 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5096 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5097 | case QEMU_OPTION_smb: |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 5098 | if (net_slirp_smb(optarg) < 0) |
| 5099 | exit(1); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5100 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5101 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5102 | case QEMU_OPTION_redir: |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 5103 | if (net_slirp_redir(optarg) < 0) |
| 5104 | exit(1); |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5105 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5106 | #endif |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5107 | case QEMU_OPTION_bt: |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5108 | add_device_config(DEV_BT, optarg); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5109 | break; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5110 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5111 | case QEMU_OPTION_audio_help: |
| 5112 | AUD_help (); |
| 5113 | exit (0); |
| 5114 | break; |
| 5115 | case QEMU_OPTION_soundhw: |
| 5116 | select_soundhw (optarg); |
| 5117 | break; |
| 5118 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5119 | case QEMU_OPTION_h: |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5120 | help(0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5121 | break; |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 5122 | case QEMU_OPTION_version: |
| 5123 | version(); |
| 5124 | exit(0); |
| 5125 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5126 | case QEMU_OPTION_m: { |
| 5127 | uint64_t value; |
| 5128 | char *ptr; |
| 5129 | |
| 5130 | value = strtoul(optarg, &ptr, 10); |
| 5131 | switch (*ptr) { |
| 5132 | case 0: case 'M': case 'm': |
| 5133 | value <<= 20; |
| 5134 | break; |
| 5135 | case 'G': case 'g': |
| 5136 | value <<= 30; |
| 5137 | break; |
| 5138 | default: |
| 5139 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5140 | exit(1); |
| 5141 | } |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5142 | |
| 5143 | /* On 32-bit hosts, QEMU is limited by virtual address space */ |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 5144 | if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5145 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); |
| 5146 | exit(1); |
| 5147 | } |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5148 | if (value != (uint64_t)(ram_addr_t)value) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5149 | fprintf(stderr, "qemu: ram size too large\n"); |
| 5150 | exit(1); |
| 5151 | } |
| 5152 | ram_size = value; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5153 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5154 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5155 | case QEMU_OPTION_d: |
| 5156 | { |
| 5157 | int mask; |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 5158 | const CPULogItem *item; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5159 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5160 | mask = cpu_str_to_log_mask(optarg); |
| 5161 | if (!mask) { |
| 5162 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5163 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 5164 | printf("%-10s %s\n", item->name, item->help); |
| 5165 | } |
| 5166 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5167 | } |
| 5168 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5169 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5170 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5171 | case QEMU_OPTION_s: |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5172 | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5173 | break; |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5174 | case QEMU_OPTION_gdb: |
| 5175 | gdbstub_dev = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5176 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5177 | case QEMU_OPTION_L: |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5178 | data_dir = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5179 | break; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 5180 | case QEMU_OPTION_bios: |
| 5181 | bios_name = optarg; |
| 5182 | break; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 5183 | case QEMU_OPTION_singlestep: |
| 5184 | singlestep = 1; |
| 5185 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5186 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 5187 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5188 | break; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 5189 | case QEMU_OPTION_k: |
| 5190 | keyboard_layout = optarg; |
| 5191 | break; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 5192 | case QEMU_OPTION_localtime: |
| 5193 | rtc_utc = 0; |
| 5194 | break; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 5195 | case QEMU_OPTION_vga: |
| 5196 | select_vgahw (optarg); |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 5197 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5198 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5199 | case QEMU_OPTION_g: |
| 5200 | { |
| 5201 | const char *p; |
| 5202 | int w, h, depth; |
| 5203 | p = optarg; |
| 5204 | w = strtol(p, (char **)&p, 10); |
| 5205 | if (w <= 0) { |
| 5206 | graphic_error: |
| 5207 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 5208 | exit(1); |
| 5209 | } |
| 5210 | if (*p != 'x') |
| 5211 | goto graphic_error; |
| 5212 | p++; |
| 5213 | h = strtol(p, (char **)&p, 10); |
| 5214 | if (h <= 0) |
| 5215 | goto graphic_error; |
| 5216 | if (*p == 'x') { |
| 5217 | p++; |
| 5218 | depth = strtol(p, (char **)&p, 10); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5219 | if (depth != 8 && depth != 15 && depth != 16 && |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5220 | depth != 24 && depth != 32) |
| 5221 | goto graphic_error; |
| 5222 | } else if (*p == '\0') { |
| 5223 | depth = graphic_depth; |
| 5224 | } else { |
| 5225 | goto graphic_error; |
| 5226 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5227 | |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5228 | graphic_width = w; |
| 5229 | graphic_height = h; |
| 5230 | graphic_depth = depth; |
| 5231 | } |
| 5232 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5233 | #endif |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 5234 | case QEMU_OPTION_echr: |
| 5235 | { |
| 5236 | char *r; |
| 5237 | term_escape_char = strtol(optarg, &r, 0); |
| 5238 | if (r == optarg) |
| 5239 | printf("Bad argument to echr\n"); |
| 5240 | break; |
| 5241 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5242 | case QEMU_OPTION_monitor: |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 5243 | add_device_config(DEV_MONITOR, optarg); |
| 5244 | default_monitor = 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5245 | break; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 5246 | case QEMU_OPTION_chardev: |
| 5247 | opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend"); |
| 5248 | if (!opts) { |
| 5249 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5250 | exit(1); |
| 5251 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 5252 | break; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5253 | case QEMU_OPTION_serial: |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5254 | add_device_config(DEV_SERIAL, optarg); |
| 5255 | default_serial = 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5256 | break; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5257 | case QEMU_OPTION_watchdog: |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5258 | if (watchdog) { |
| 5259 | fprintf(stderr, |
| 5260 | "qemu: only one watchdog option may be given\n"); |
| 5261 | return 1; |
| 5262 | } |
| 5263 | watchdog = optarg; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5264 | break; |
| 5265 | case QEMU_OPTION_watchdog_action: |
| 5266 | if (select_watchdog_action(optarg) == -1) { |
| 5267 | fprintf(stderr, "Unknown -watchdog-action parameter\n"); |
| 5268 | exit(1); |
| 5269 | } |
| 5270 | break; |
aliguori | 51ecf13 | 2009-01-15 20:06:40 +0000 | [diff] [blame] | 5271 | case QEMU_OPTION_virtiocon: |
| 5272 | if (virtio_console_index >= MAX_VIRTIO_CONSOLES) { |
| 5273 | fprintf(stderr, "qemu: too many virtio consoles\n"); |
| 5274 | exit(1); |
| 5275 | } |
| 5276 | virtio_consoles[virtio_console_index] = optarg; |
| 5277 | virtio_console_index++; |
| 5278 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5279 | case QEMU_OPTION_parallel: |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5280 | add_device_config(DEV_PARALLEL, optarg); |
| 5281 | default_parallel = 0; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5282 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 5283 | case QEMU_OPTION_loadvm: |
| 5284 | loadvm = optarg; |
| 5285 | break; |
| 5286 | case QEMU_OPTION_full_screen: |
| 5287 | full_screen = 1; |
| 5288 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5289 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 5290 | case QEMU_OPTION_no_frame: |
| 5291 | no_frame = 1; |
| 5292 | break; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 5293 | case QEMU_OPTION_alt_grab: |
| 5294 | alt_grab = 1; |
| 5295 | break; |
Dustin Kirkland | 0ca9f8a | 2009-09-17 15:48:04 -0500 | [diff] [blame] | 5296 | case QEMU_OPTION_ctrl_grab: |
| 5297 | ctrl_grab = 1; |
| 5298 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5299 | case QEMU_OPTION_no_quit: |
| 5300 | no_quit = 1; |
| 5301 | break; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5302 | case QEMU_OPTION_sdl: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5303 | display_type = DT_SDL; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5304 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5305 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5306 | case QEMU_OPTION_pidfile: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5307 | pid_file = optarg; |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5308 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5309 | #ifdef TARGET_I386 |
| 5310 | case QEMU_OPTION_win2k_hack: |
| 5311 | win2k_install_hack = 1; |
| 5312 | break; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 5313 | case QEMU_OPTION_rtc_td_hack: |
| 5314 | rtc_td_hack = 1; |
| 5315 | break; |
aliguori | 8a92ea2 | 2009-02-27 20:12:36 +0000 | [diff] [blame] | 5316 | case QEMU_OPTION_acpitable: |
| 5317 | if(acpi_table_add(optarg) < 0) { |
| 5318 | fprintf(stderr, "Wrong acpi table provided\n"); |
| 5319 | exit(1); |
| 5320 | } |
| 5321 | break; |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 5322 | case QEMU_OPTION_smbios: |
| 5323 | if(smbios_entry_add(optarg) < 0) { |
| 5324 | fprintf(stderr, "Wrong smbios provided\n"); |
| 5325 | exit(1); |
| 5326 | } |
| 5327 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5328 | #endif |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5329 | #ifdef CONFIG_KVM |
| 5330 | case QEMU_OPTION_enable_kvm: |
| 5331 | kvm_allowed = 1; |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5332 | break; |
| 5333 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 5334 | case QEMU_OPTION_usb: |
| 5335 | usb_enabled = 1; |
| 5336 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5337 | case QEMU_OPTION_usbdevice: |
| 5338 | usb_enabled = 1; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5339 | add_device_config(DEV_USB, optarg); |
| 5340 | break; |
| 5341 | case QEMU_OPTION_device: |
Mark McLoughlin | b386bec | 2009-10-06 12:17:01 +0100 | [diff] [blame] | 5342 | if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) { |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5343 | exit(1); |
| 5344 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5345 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5346 | case QEMU_OPTION_smp: |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 5347 | smp_parse(optarg); |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5348 | if (smp_cpus < 1) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5349 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 5350 | exit(1); |
| 5351 | } |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5352 | if (max_cpus < smp_cpus) { |
| 5353 | fprintf(stderr, "maxcpus must be equal to or greater than " |
| 5354 | "smp\n"); |
| 5355 | exit(1); |
| 5356 | } |
| 5357 | if (max_cpus > 255) { |
| 5358 | fprintf(stderr, "Unsupported number of maxcpus\n"); |
| 5359 | exit(1); |
| 5360 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5361 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5362 | case QEMU_OPTION_vnc: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5363 | display_type = DT_VNC; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 5364 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5365 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5366 | #ifdef TARGET_I386 |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 5367 | case QEMU_OPTION_no_acpi: |
| 5368 | acpi_enabled = 0; |
| 5369 | break; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 5370 | case QEMU_OPTION_no_hpet: |
| 5371 | no_hpet = 1; |
| 5372 | break; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 5373 | case QEMU_OPTION_balloon: |
| 5374 | if (balloon_parse(optarg) < 0) { |
| 5375 | fprintf(stderr, "Unknown -balloon argument %s\n", optarg); |
| 5376 | exit(1); |
| 5377 | } |
Eduardo Habkost | df97b92 | 2009-06-10 16:34:08 -0300 | [diff] [blame] | 5378 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5379 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 5380 | case QEMU_OPTION_no_reboot: |
| 5381 | no_reboot = 1; |
| 5382 | break; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 5383 | case QEMU_OPTION_no_shutdown: |
| 5384 | no_shutdown = 1; |
| 5385 | break; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 5386 | case QEMU_OPTION_show_cursor: |
| 5387 | cursor_hide = 0; |
| 5388 | break; |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 5389 | case QEMU_OPTION_uuid: |
| 5390 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { |
| 5391 | fprintf(stderr, "Fail to parse UUID string." |
| 5392 | " Wrong format.\n"); |
| 5393 | exit(1); |
| 5394 | } |
| 5395 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5396 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5397 | case QEMU_OPTION_daemonize: |
| 5398 | daemonize = 1; |
| 5399 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5400 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 5401 | case QEMU_OPTION_option_rom: |
| 5402 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 5403 | fprintf(stderr, "Too many option ROMs\n"); |
| 5404 | exit(1); |
| 5405 | } |
| 5406 | option_rom[nb_option_roms] = optarg; |
| 5407 | nb_option_roms++; |
| 5408 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5409 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 5410 | case QEMU_OPTION_semihosting: |
| 5411 | semihosting_enabled = 1; |
| 5412 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5413 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5414 | case QEMU_OPTION_name: |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 5415 | qemu_name = qemu_strdup(optarg); |
| 5416 | { |
| 5417 | char *p = strchr(qemu_name, ','); |
| 5418 | if (p != NULL) { |
| 5419 | *p++ = 0; |
| 5420 | if (strncmp(p, "process=", 8)) { |
| 5421 | fprintf(stderr, "Unknown subargument %s to -name", p); |
| 5422 | exit(1); |
| 5423 | } |
| 5424 | p += 8; |
| 5425 | set_proc_name(p); |
| 5426 | } |
| 5427 | } |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5428 | break; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 5429 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 5430 | case QEMU_OPTION_prom_env: |
| 5431 | if (nb_prom_envs >= MAX_PROM_ENVS) { |
| 5432 | fprintf(stderr, "Too many prom variables\n"); |
| 5433 | exit(1); |
| 5434 | } |
| 5435 | prom_envs[nb_prom_envs] = optarg; |
| 5436 | nb_prom_envs++; |
| 5437 | break; |
| 5438 | #endif |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5439 | #ifdef TARGET_ARM |
| 5440 | case QEMU_OPTION_old_param: |
| 5441 | old_param = 1; |
ths | 05ebd53 | 2008-01-08 19:32:16 +0000 | [diff] [blame] | 5442 | break; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5443 | #endif |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 5444 | case QEMU_OPTION_clock: |
| 5445 | configure_alarms(optarg); |
| 5446 | break; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5447 | case QEMU_OPTION_startdate: |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 5448 | configure_rtc_date_offset(optarg, 1); |
| 5449 | break; |
| 5450 | case QEMU_OPTION_rtc: |
| 5451 | opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL); |
| 5452 | if (!opts) { |
| 5453 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5454 | exit(1); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5455 | } |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 5456 | configure_rtc(opts); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5457 | break; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5458 | case QEMU_OPTION_tb_size: |
| 5459 | tb_size = strtol(optarg, NULL, 0); |
| 5460 | if (tb_size < 0) |
| 5461 | tb_size = 0; |
| 5462 | break; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5463 | case QEMU_OPTION_icount: |
| 5464 | use_icount = 1; |
| 5465 | if (strcmp(optarg, "auto") == 0) { |
| 5466 | icount_time_shift = -1; |
| 5467 | } else { |
| 5468 | icount_time_shift = strtol(optarg, NULL, 0); |
| 5469 | } |
| 5470 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 5471 | case QEMU_OPTION_incoming: |
| 5472 | incoming = optarg; |
| 5473 | break; |
Gerd Hoffmann | d8c208d | 2009-12-08 13:11:46 +0100 | [diff] [blame^] | 5474 | case QEMU_OPTION_nodefaults: |
| 5475 | default_serial = 0; |
| 5476 | default_parallel = 0; |
| 5477 | default_monitor = 0; |
| 5478 | default_vga = 0; |
| 5479 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5480 | #ifndef _WIN32 |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5481 | case QEMU_OPTION_chroot: |
| 5482 | chroot_dir = optarg; |
| 5483 | break; |
| 5484 | case QEMU_OPTION_runas: |
| 5485 | run_as = optarg; |
| 5486 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5487 | #endif |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 5488 | #ifdef CONFIG_XEN |
| 5489 | case QEMU_OPTION_xen_domid: |
| 5490 | xen_domid = atoi(optarg); |
| 5491 | break; |
| 5492 | case QEMU_OPTION_xen_create: |
| 5493 | xen_mode = XEN_CREATE; |
| 5494 | break; |
| 5495 | case QEMU_OPTION_xen_attach: |
| 5496 | xen_mode = XEN_ATTACH; |
| 5497 | break; |
| 5498 | #endif |
Gerd Hoffmann | 715a664 | 2009-10-14 10:39:28 +0200 | [diff] [blame] | 5499 | case QEMU_OPTION_readconfig: |
| 5500 | { |
| 5501 | FILE *fp; |
| 5502 | fp = fopen(optarg, "r"); |
| 5503 | if (fp == NULL) { |
| 5504 | fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); |
| 5505 | exit(1); |
| 5506 | } |
| 5507 | if (qemu_config_parse(fp) != 0) { |
| 5508 | exit(1); |
| 5509 | } |
| 5510 | fclose(fp); |
| 5511 | break; |
| 5512 | } |
| 5513 | case QEMU_OPTION_writeconfig: |
| 5514 | { |
| 5515 | FILE *fp; |
| 5516 | if (strcmp(optarg, "-") == 0) { |
| 5517 | fp = stdout; |
| 5518 | } else { |
| 5519 | fp = fopen(optarg, "w"); |
| 5520 | if (fp == NULL) { |
| 5521 | fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); |
| 5522 | exit(1); |
| 5523 | } |
| 5524 | } |
| 5525 | qemu_config_write(fp); |
| 5526 | fclose(fp); |
| 5527 | break; |
| 5528 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5529 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5530 | } |
| 5531 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5532 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5533 | /* If no data_dir is specified then try to find it relative to the |
| 5534 | executable path. */ |
| 5535 | if (!data_dir) { |
| 5536 | data_dir = find_datadir(argv[0]); |
| 5537 | } |
| 5538 | /* If all else fails use the install patch specified when building. */ |
| 5539 | if (!data_dir) { |
| 5540 | data_dir = CONFIG_QEMU_SHAREDIR; |
| 5541 | } |
| 5542 | |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5543 | /* |
| 5544 | * Default to max_cpus = smp_cpus, in case the user doesn't |
| 5545 | * specify a max_cpus value. |
| 5546 | */ |
| 5547 | if (!max_cpus) |
| 5548 | max_cpus = smp_cpus; |
| 5549 | |
balrog | 3d878ca | 2008-10-28 10:59:59 +0000 | [diff] [blame] | 5550 | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5551 | if (smp_cpus > machine->max_cpus) { |
| 5552 | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus " |
| 5553 | "supported by machine `%s' (%d)\n", smp_cpus, machine->name, |
| 5554 | machine->max_cpus); |
| 5555 | exit(1); |
| 5556 | } |
| 5557 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5558 | qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0); |
| 5559 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5560 | if (display_type == DT_NOGRAPHIC) { |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5561 | if (default_parallel) |
| 5562 | add_device_config(DEV_PARALLEL, "null"); |
Gerd Hoffmann | e1c0917 | 2009-12-08 13:11:44 +0100 | [diff] [blame] | 5563 | if (default_serial && default_monitor) { |
| 5564 | add_device_config(DEV_SERIAL, "mon:stdio"); |
| 5565 | } else { |
| 5566 | if (default_serial) |
| 5567 | add_device_config(DEV_SERIAL, "stdio"); |
| 5568 | if (default_monitor) |
| 5569 | add_device_config(DEV_MONITOR, "stdio"); |
| 5570 | } |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5571 | } else { |
| 5572 | if (default_serial) |
| 5573 | add_device_config(DEV_SERIAL, "vc:80Cx24C"); |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5574 | if (default_parallel) |
| 5575 | add_device_config(DEV_PARALLEL, "vc:80Cx24C"); |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 5576 | if (default_monitor) |
| 5577 | add_device_config(DEV_MONITOR, "vc:80Cx24C"); |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5578 | } |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 5579 | if (default_vga) |
| 5580 | vga_interface_type = VGA_CIRRUS; |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5581 | |
Gerd Hoffmann | 1a688d3 | 2009-12-08 13:11:36 +0100 | [diff] [blame] | 5582 | if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0) |
| 5583 | exit(1); |
| 5584 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5585 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5586 | if (daemonize) { |
| 5587 | pid_t pid; |
| 5588 | |
| 5589 | if (pipe(fds) == -1) |
| 5590 | exit(1); |
| 5591 | |
| 5592 | pid = fork(); |
| 5593 | if (pid > 0) { |
| 5594 | uint8_t status; |
| 5595 | ssize_t len; |
| 5596 | |
| 5597 | close(fds[1]); |
| 5598 | |
| 5599 | again: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5600 | len = read(fds[0], &status, 1); |
| 5601 | if (len == -1 && (errno == EINTR)) |
| 5602 | goto again; |
| 5603 | |
| 5604 | if (len != 1) |
| 5605 | exit(1); |
| 5606 | else if (status == 1) { |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 5607 | fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno)); |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5608 | exit(1); |
| 5609 | } else |
| 5610 | exit(0); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5611 | } else if (pid < 0) |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5612 | exit(1); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5613 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 5614 | close(fds[0]); |
| 5615 | qemu_set_cloexec(fds[1]); |
| 5616 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5617 | setsid(); |
| 5618 | |
| 5619 | pid = fork(); |
| 5620 | if (pid > 0) |
| 5621 | exit(0); |
| 5622 | else if (pid < 0) |
| 5623 | exit(1); |
| 5624 | |
| 5625 | umask(027); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5626 | |
| 5627 | signal(SIGTSTP, SIG_IGN); |
| 5628 | signal(SIGTTOU, SIG_IGN); |
| 5629 | signal(SIGTTIN, SIG_IGN); |
| 5630 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5631 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 5632 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5633 | if (daemonize) { |
| 5634 | uint8_t status = 1; |
| 5635 | write(fds[1], &status, 1); |
| 5636 | } else |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 5637 | fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno)); |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5638 | exit(1); |
| 5639 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 5640 | #endif |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5641 | |
Marcelo Tosatti | 214910a | 2009-09-18 02:41:23 -0300 | [diff] [blame] | 5642 | if (kvm_enabled()) { |
| 5643 | int ret; |
| 5644 | |
| 5645 | ret = kvm_init(smp_cpus); |
| 5646 | if (ret < 0) { |
| 5647 | fprintf(stderr, "failed to initialize KVM\n"); |
| 5648 | exit(1); |
| 5649 | } |
| 5650 | } |
| 5651 | |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 5652 | if (qemu_init_main_loop()) { |
| 5653 | fprintf(stderr, "qemu_init_main_loop failed\n"); |
| 5654 | exit(1); |
| 5655 | } |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5656 | linux_boot = (kernel_filename != NULL); |
balrog | 6c41b27 | 2007-11-17 12:12:29 +0000 | [diff] [blame] | 5657 | |
ths | f8d39c0 | 2008-07-03 10:01:15 +0000 | [diff] [blame] | 5658 | if (!linux_boot && *kernel_cmdline != '\0') { |
| 5659 | fprintf(stderr, "-append only allowed with -kernel option\n"); |
| 5660 | exit(1); |
| 5661 | } |
| 5662 | |
| 5663 | if (!linux_boot && initrd_filename != NULL) { |
| 5664 | fprintf(stderr, "-initrd only allowed with -kernel option\n"); |
| 5665 | exit(1); |
| 5666 | } |
| 5667 | |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5668 | #ifndef _WIN32 |
| 5669 | /* Win32 doesn't support line-buffering and requires size >= 2 */ |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 5670 | setvbuf(stdout, NULL, _IOLBF, 0); |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5671 | #endif |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5672 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 5673 | if (init_timer_alarm() < 0) { |
| 5674 | fprintf(stderr, "could not initialize alarm timer\n"); |
| 5675 | exit(1); |
| 5676 | } |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5677 | if (use_icount && icount_time_shift < 0) { |
| 5678 | use_icount = 2; |
| 5679 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 5680 | It will be corrected fairly quickly anyway. */ |
| 5681 | icount_time_shift = 3; |
| 5682 | init_icount_adjust(); |
| 5683 | } |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 5684 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5685 | #ifdef _WIN32 |
| 5686 | socket_init(); |
| 5687 | #endif |
| 5688 | |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 5689 | if (net_init_clients() < 0) { |
| 5690 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5691 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 5692 | |
Glauber Costa | 406c8df | 2009-06-17 09:05:30 -0400 | [diff] [blame] | 5693 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
| 5694 | net_set_boot_mask(net_boot); |
| 5695 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5696 | /* init the bluetooth world */ |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5697 | if (foreach_device_config(DEV_BT, bt_parse)) |
| 5698 | exit(1); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5699 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5700 | /* init the memory */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 5701 | if (ram_size == 0) |
| 5702 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 5703 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5704 | /* init the dynamic translator */ |
| 5705 | cpu_exec_init_all(tb_size * 1024 * 1024); |
| 5706 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 5707 | bdrv_init_with_whitelist(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5708 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 5709 | blk_mig_init(); |
| 5710 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5711 | /* we always create the cdrom drive, even if no disk is there */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5712 | drive_add(NULL, CDROM_ALIAS); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5713 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5714 | /* we always create at least one floppy */ |
Gerd Hoffmann | 3b0ba92 | 2009-07-22 16:42:59 +0200 | [diff] [blame] | 5715 | drive_add(NULL, FD_ALIAS, 0); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5716 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5717 | /* 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] | 5718 | drive_add(NULL, SD_ALIAS); |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5719 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 5720 | /* open the virtual block devices */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5721 | if (snapshot) |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 5722 | qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
| 5723 | 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] | 5724 | exit(1); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5725 | |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 5726 | vmstate_register(0, &vmstate_timers ,&timers_state); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 5727 | register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL, |
| 5728 | ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5729 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5730 | if (nb_numa_nodes > 0) { |
| 5731 | int i; |
| 5732 | |
| 5733 | if (nb_numa_nodes > smp_cpus) { |
| 5734 | nb_numa_nodes = smp_cpus; |
| 5735 | } |
| 5736 | |
| 5737 | /* If no memory size if given for any node, assume the default case |
| 5738 | * and distribute the available memory equally across all nodes |
| 5739 | */ |
| 5740 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5741 | if (node_mem[i] != 0) |
| 5742 | break; |
| 5743 | } |
| 5744 | if (i == nb_numa_nodes) { |
| 5745 | uint64_t usedmem = 0; |
| 5746 | |
| 5747 | /* On Linux, the each node's border has to be 8MB aligned, |
| 5748 | * the final node gets the rest. |
| 5749 | */ |
| 5750 | for (i = 0; i < nb_numa_nodes - 1; i++) { |
| 5751 | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); |
| 5752 | usedmem += node_mem[i]; |
| 5753 | } |
| 5754 | node_mem[i] = ram_size - usedmem; |
| 5755 | } |
| 5756 | |
| 5757 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5758 | if (node_cpumask[i] != 0) |
| 5759 | break; |
| 5760 | } |
| 5761 | /* assigning the VCPUs round-robin is easier to implement, guest OSes |
| 5762 | * must cope with this anyway, because there are BIOSes out there in |
| 5763 | * real machines which also use this scheme. |
| 5764 | */ |
| 5765 | if (i == nb_numa_nodes) { |
| 5766 | for (i = 0; i < smp_cpus; i++) { |
| 5767 | node_cpumask[i % nb_numa_nodes] |= 1 << i; |
| 5768 | } |
| 5769 | } |
| 5770 | } |
| 5771 | |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 5772 | if (foreach_device_config(DEV_MONITOR, monitor_parse) < 0) |
| 5773 | exit(1); |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5774 | if (foreach_device_config(DEV_SERIAL, serial_parse) < 0) |
| 5775 | exit(1); |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5776 | if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0) |
| 5777 | exit(1); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5778 | |
| 5779 | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
| 5780 | const char *devname = virtio_consoles[i]; |
| 5781 | if (devname && strcmp(devname, "none")) { |
| 5782 | char label[32]; |
| 5783 | snprintf(label, sizeof(label), "virtcon%d", i); |
aurel32 | ceecf1d | 2009-01-18 14:08:04 +0000 | [diff] [blame] | 5784 | virtcon_hds[i] = qemu_chr_open(label, devname, NULL); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5785 | if (!virtcon_hds[i]) { |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 5786 | fprintf(stderr, "qemu: could not open virtio console '%s': %s\n", |
| 5787 | devname, strerror(errno)); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5788 | exit(1); |
| 5789 | } |
| 5790 | } |
| 5791 | } |
| 5792 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 5793 | module_call_init(MODULE_INIT_DEVICE); |
| 5794 | |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5795 | if (watchdog) { |
| 5796 | i = select_watchdog(watchdog); |
| 5797 | if (i > 0) |
| 5798 | exit (i == 1 ? 1 : 0); |
| 5799 | } |
| 5800 | |
Gerd Hoffmann | b6b6114 | 2009-07-15 13:48:21 +0200 | [diff] [blame] | 5801 | if (machine->compat_props) { |
Gerd Hoffmann | 458fb67 | 2009-12-08 13:11:33 +0100 | [diff] [blame] | 5802 | qdev_prop_register_global_list(machine->compat_props); |
Gerd Hoffmann | b6b6114 | 2009-07-15 13:48:21 +0200 | [diff] [blame] | 5803 | } |
Gerd Hoffmann | d0fef6f | 2009-12-08 13:11:34 +0100 | [diff] [blame] | 5804 | qemu_add_globals(); |
| 5805 | |
Paul Brook | fbe1b59 | 2009-05-13 17:56:25 +0100 | [diff] [blame] | 5806 | machine->init(ram_size, boot_devices, |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5807 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
| 5808 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5809 | |
Juan Quintela | 67b3b71 | 2009-08-28 19:25:15 +0200 | [diff] [blame] | 5810 | #ifndef _WIN32 |
| 5811 | /* must be after terminal init, SDL library changes signal handlers */ |
| 5812 | sighandler_setup(); |
| 5813 | #endif |
| 5814 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5815 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 5816 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5817 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 5818 | env->numa_node = i; |
| 5819 | } |
| 5820 | } |
| 5821 | } |
| 5822 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 5823 | current_machine = machine; |
| 5824 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5825 | /* init USB devices */ |
| 5826 | if (usb_enabled) { |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 5827 | if (foreach_device_config(DEV_USB, usb_parse) < 0) |
| 5828 | exit(1); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5831 | /* init generic devices */ |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5832 | 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] | 5833 | exit(1); |
| 5834 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 5835 | if (!display_state) |
| 5836 | dumb_display_init(); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5837 | /* just use the first displaystate for the moment */ |
| 5838 | ds = display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5839 | |
| 5840 | if (display_type == DT_DEFAULT) { |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5841 | #if defined(CONFIG_SDL) || defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5842 | display_type = DT_SDL; |
| 5843 | #else |
| 5844 | display_type = DT_VNC; |
| 5845 | vnc_display = "localhost:0,to=99"; |
| 5846 | show_vnc_port = 1; |
| 5847 | #endif |
| 5848 | } |
| 5849 | |
| 5850 | |
| 5851 | switch (display_type) { |
| 5852 | case DT_NOGRAPHIC: |
| 5853 | break; |
| 5854 | #if defined(CONFIG_CURSES) |
| 5855 | case DT_CURSES: |
| 5856 | curses_display_init(ds, full_screen); |
| 5857 | break; |
| 5858 | #endif |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5859 | #if defined(CONFIG_SDL) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5860 | case DT_SDL: |
| 5861 | sdl_display_init(ds, full_screen, no_frame); |
| 5862 | break; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 5863 | #elif defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5864 | case DT_SDL: |
| 5865 | cocoa_display_init(ds, full_screen); |
| 5866 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5867 | #endif |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5868 | case DT_VNC: |
| 5869 | vnc_display_init(ds); |
| 5870 | if (vnc_display_open(ds, vnc_display) < 0) |
| 5871 | exit(1); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5872 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5873 | if (show_vnc_port) { |
| 5874 | printf("VNC server running on `%s'\n", vnc_display_local_addr(ds)); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5875 | } |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5876 | break; |
| 5877 | default: |
| 5878 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5879 | } |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5880 | dpy_resize(ds); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 5881 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5882 | dcl = ds->listeners; |
| 5883 | while (dcl != NULL) { |
| 5884 | if (dcl->dpy_refresh != NULL) { |
| 5885 | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); |
| 5886 | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 5887 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5888 | dcl = dcl->next; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5889 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5890 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5891 | if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) { |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 5892 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL); |
| 5893 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); |
| 5894 | } |
| 5895 | |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5896 | text_consoles_set_display(display_state); |
| 5897 | |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 5898 | for (i = 0; i < MAX_MONITOR_DEVICES; i++) { |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 5899 | if (monitor_hds[i]) { |
Gerd Hoffmann | 4e307fc | 2009-12-08 13:11:37 +0100 | [diff] [blame] | 5900 | monitor_init(monitor_hds[i], |
| 5901 | MONITOR_USE_READLINE | |
| 5902 | ((i == 0) ? MONITOR_IS_DEFAULT : 0)); |
Jan Kiszka | ddd9bbd | 2009-08-27 19:51:16 +0200 | [diff] [blame] | 5903 | } |
| 5904 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5905 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5906 | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
| 5907 | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n", |
| 5908 | gdbstub_dev); |
| 5909 | exit(1); |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 5910 | } |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 5911 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 5912 | qdev_machine_creation_done(); |
| 5913 | |
Gerd Hoffmann | 45a50b1 | 2009-10-01 16:42:33 +0200 | [diff] [blame] | 5914 | rom_load_all(); |
| 5915 | |
Juan Quintela | 504c294 | 2009-11-12 00:39:13 +0100 | [diff] [blame] | 5916 | qemu_system_reset(); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 5917 | if (loadvm) { |
| 5918 | if (load_vmstate(cur_mon, loadvm) < 0) { |
| 5919 | autostart = 0; |
| 5920 | } |
| 5921 | } |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 5922 | |
Glauber Costa | 2bb8c10 | 2009-07-24 16:20:23 -0400 | [diff] [blame] | 5923 | if (incoming) { |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 5924 | qemu_start_incoming_migration(incoming); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 5925 | } else if (autostart) { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 5926 | vm_start(); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 5927 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 5928 | |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 5929 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5930 | if (daemonize) { |
| 5931 | uint8_t status = 0; |
| 5932 | ssize_t len; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5933 | |
| 5934 | again1: |
| 5935 | len = write(fds[1], &status, 1); |
| 5936 | if (len == -1 && (errno == EINTR)) |
| 5937 | goto again1; |
| 5938 | |
| 5939 | if (len != 1) |
| 5940 | exit(1); |
| 5941 | |
aliguori | bd54b86 | 2008-07-23 00:58:33 +0000 | [diff] [blame] | 5942 | chdir("/"); |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 5943 | TFR(fd = qemu_open("/dev/null", O_RDWR)); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5944 | if (fd == -1) |
| 5945 | exit(1); |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5946 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5947 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5948 | if (run_as) { |
| 5949 | pwd = getpwnam(run_as); |
| 5950 | if (!pwd) { |
| 5951 | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as); |
| 5952 | exit(1); |
| 5953 | } |
| 5954 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5955 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5956 | if (chroot_dir) { |
| 5957 | if (chroot(chroot_dir) < 0) { |
| 5958 | fprintf(stderr, "chroot failed\n"); |
| 5959 | exit(1); |
| 5960 | } |
| 5961 | chdir("/"); |
| 5962 | } |
| 5963 | |
| 5964 | if (run_as) { |
| 5965 | if (setgid(pwd->pw_gid) < 0) { |
| 5966 | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid); |
| 5967 | exit(1); |
| 5968 | } |
| 5969 | if (setuid(pwd->pw_uid) < 0) { |
| 5970 | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid); |
| 5971 | exit(1); |
| 5972 | } |
| 5973 | if (setuid(0) != -1) { |
| 5974 | fprintf(stderr, "Dropping privileges failed\n"); |
| 5975 | exit(1); |
| 5976 | } |
| 5977 | } |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5978 | |
| 5979 | if (daemonize) { |
| 5980 | dup2(fd, 0); |
| 5981 | dup2(fd, 1); |
| 5982 | dup2(fd, 2); |
| 5983 | |
| 5984 | close(fd); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5985 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 5986 | #endif |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5987 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5988 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 5989 | quit_timers(); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 5990 | net_cleanup(); |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 5991 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5992 | return 0; |
| 5993 | } |