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" |
Luiz Capitulino | e78c48e | 2009-12-10 17:16:04 -0200 | [diff] [blame] | 159 | #include "qemu-objects.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 160 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 161 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 162 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 163 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 164 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 165 | #include "qemu_socket.h" |
| 166 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 167 | #include "slirp/libslirp.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 168 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 169 | #include "qemu-queue.h" |
| 170 | |
blueswir1 | 9dc63a1 | 2008-10-04 07:25:46 +0000 | [diff] [blame] | 171 | //#define DEBUG_NET |
| 172 | //#define DEBUG_SLIRP |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 173 | |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 174 | #define DEFAULT_RAM_SIZE 128 |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 175 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 176 | static const char *data_dir; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 177 | const char *bios_name = NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 178 | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 179 | to store the VM snapshots */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 180 | struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives); |
| 181 | struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts); |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 182 | enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB; |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 183 | static DisplayState *display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 184 | DisplayType display_type = DT_DEFAULT; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 185 | const char* keyboard_layout = NULL; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 186 | ram_addr_t ram_size; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 187 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 188 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 189 | int vm_running; |
Paolo Bonzini | d399f67 | 2009-07-27 23:17:51 +0200 | [diff] [blame] | 190 | int autostart; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 191 | static int rtc_utc = 1; |
| 192 | static int rtc_date_offset = -1; /* -1 means no change */ |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 193 | QEMUClock *rtc_clock; |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 194 | int vga_interface_type = VGA_NONE; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 195 | #ifdef TARGET_SPARC |
| 196 | int graphic_width = 1024; |
| 197 | int graphic_height = 768; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 198 | int graphic_depth = 8; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 199 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 200 | int graphic_width = 800; |
| 201 | int graphic_height = 600; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 202 | int graphic_depth = 15; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 203 | #endif |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 204 | static int full_screen = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 205 | #ifdef CONFIG_SDL |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 206 | static int no_frame = 0; |
blueswir1 | 634a21f | 2008-11-16 11:34:07 +0000 | [diff] [blame] | 207 | #endif |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 208 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 209 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 210 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
aliguori | 9ede2fd | 2009-01-15 20:05:25 +0000 | [diff] [blame] | 211 | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 212 | #ifdef TARGET_I386 |
| 213 | int win2k_install_hack = 0; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 214 | int rtc_td_hack = 0; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 215 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 216 | int usb_enabled = 0; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 217 | int singlestep = 0; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 218 | int smp_cpus = 1; |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 219 | int max_cpus = 0; |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 220 | int smp_cores = 1; |
| 221 | int smp_threads = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 222 | const char *vnc_display; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 223 | int acpi_enabled = 1; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 224 | int no_hpet = 0; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 225 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 226 | int no_reboot = 0; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 227 | int no_shutdown = 0; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 228 | int cursor_hide = 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 229 | int graphic_rotate = 0; |
Jes Sorensen | 6b35e7b | 2009-08-06 16:25:50 +0200 | [diff] [blame] | 230 | uint8_t irq0override = 1; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 231 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 232 | int daemonize = 0; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 233 | #endif |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 234 | const char *watchdog; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 235 | const char *option_rom[MAX_OPTION_ROMS]; |
| 236 | int nb_option_roms; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 237 | int semihosting_enabled = 0; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 238 | #ifdef TARGET_ARM |
| 239 | int old_param = 0; |
| 240 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 241 | const char *qemu_name; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 242 | int alt_grab = 0; |
Dustin Kirkland | 0ca9f8a | 2009-09-17 15:48:04 -0500 | [diff] [blame] | 243 | int ctrl_grab = 0; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 244 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 245 | unsigned int nb_prom_envs = 0; |
| 246 | const char *prom_envs[MAX_PROM_ENVS]; |
| 247 | #endif |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 248 | int boot_menu; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 249 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 250 | int nb_numa_nodes; |
| 251 | uint64_t node_mem[MAX_NODES]; |
| 252 | uint64_t node_cpumask[MAX_NODES]; |
| 253 | |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 254 | static CPUState *cur_cpu; |
| 255 | static CPUState *next_cpu; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 256 | static int timer_alarm_pending = 1; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 257 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 258 | static int icount_time_shift; |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 259 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 260 | #define MAX_ICOUNT_SHIFT 10 |
| 261 | /* Compensate for varying guest execution speed. */ |
| 262 | static int64_t qemu_icount_bias; |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 263 | static QEMUTimer *icount_rt_timer; |
| 264 | static QEMUTimer *icount_vm_timer; |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 265 | static QEMUTimer *nographic_timer; |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 266 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 267 | uint8_t qemu_uuid[16]; |
| 268 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 269 | static QEMUBootSetHandler *boot_set_handler; |
| 270 | static void *boot_set_opaque; |
| 271 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 272 | static int default_serial = 1; |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 273 | static int default_parallel = 1; |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 274 | static int default_virtcon = 1; |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 275 | static int default_monitor = 1; |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 276 | static int default_vga = 1; |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 277 | static int default_floppy = 1; |
| 278 | static int default_cdrom = 1; |
| 279 | static int default_sdcard = 1; |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 280 | |
| 281 | static struct { |
| 282 | const char *driver; |
| 283 | int *flag; |
| 284 | } default_list[] = { |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 285 | { .driver = "isa-serial", .flag = &default_serial }, |
| 286 | { .driver = "isa-parallel", .flag = &default_parallel }, |
Gerd Hoffmann | d8bcbab | 2009-12-16 14:25:40 +0100 | [diff] [blame] | 287 | { .driver = "isa-fdc", .flag = &default_floppy }, |
| 288 | { .driver = "ide-drive", .flag = &default_cdrom }, |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 289 | { .driver = "virtio-console-pci", .flag = &default_virtcon }, |
| 290 | { .driver = "virtio-console-s390", .flag = &default_virtcon }, |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 291 | { .driver = "VGA", .flag = &default_vga }, |
| 292 | { .driver = "Cirrus VGA", .flag = &default_vga }, |
| 293 | { .driver = "QEMUware SVGA", .flag = &default_vga }, |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | static int default_driver_check(QemuOpts *opts, void *opaque) |
| 297 | { |
| 298 | const char *driver = qemu_opt_get(opts, "driver"); |
| 299 | int i; |
| 300 | |
| 301 | if (!driver) |
| 302 | return 0; |
| 303 | for (i = 0; i < ARRAY_SIZE(default_list); i++) { |
| 304 | if (strcmp(default_list[i].driver, driver) != 0) |
| 305 | continue; |
| 306 | *(default_list[i].flag) = 0; |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 311 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 312 | /* x86 ISA bus support */ |
| 313 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 314 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 315 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 316 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 317 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 318 | void hw_error(const char *fmt, ...) |
| 319 | { |
| 320 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 321 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 322 | |
| 323 | va_start(ap, fmt); |
| 324 | fprintf(stderr, "qemu: hardware error: "); |
| 325 | vfprintf(stderr, fmt, ap); |
| 326 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 327 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 328 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 329 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 330 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 331 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 332 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 333 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 334 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 335 | va_end(ap); |
| 336 | abort(); |
| 337 | } |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 338 | |
| 339 | static void set_proc_name(const char *s) |
| 340 | { |
Nathan Froyd | 6ca8d0f | 2009-08-03 07:32:12 -0700 | [diff] [blame] | 341 | #if defined(__linux__) && defined(PR_SET_NAME) |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 342 | char name[16]; |
| 343 | if (!s) |
| 344 | return; |
| 345 | name[sizeof(name) - 1] = 0; |
| 346 | strncpy(name, s, sizeof(name)); |
| 347 | /* Could rewrite argv[0] too, but that's a bit more complicated. |
| 348 | This simple way is enough for `top'. */ |
| 349 | prctl(PR_SET_NAME, name); |
| 350 | #endif |
| 351 | } |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 352 | |
| 353 | /***************/ |
| 354 | /* ballooning */ |
| 355 | |
| 356 | static QEMUBalloonEvent *qemu_balloon_event; |
| 357 | void *qemu_balloon_event_opaque; |
| 358 | |
| 359 | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) |
| 360 | { |
| 361 | qemu_balloon_event = func; |
| 362 | qemu_balloon_event_opaque = opaque; |
| 363 | } |
| 364 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 365 | void qemu_balloon(ram_addr_t target) |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 366 | { |
| 367 | if (qemu_balloon_event) |
| 368 | qemu_balloon_event(qemu_balloon_event_opaque, target); |
| 369 | } |
| 370 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 371 | ram_addr_t qemu_balloon_status(void) |
aliguori | df751fa | 2008-12-04 20:19:35 +0000 | [diff] [blame] | 372 | { |
| 373 | if (qemu_balloon_event) |
| 374 | return qemu_balloon_event(qemu_balloon_event_opaque, 0); |
| 375 | return 0; |
| 376 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 377 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 378 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 379 | /* keyboard/mouse */ |
| 380 | |
| 381 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 382 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 383 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 384 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 385 | |
| 386 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 387 | { |
| 388 | qemu_put_kbd_event_opaque = opaque; |
| 389 | qemu_put_kbd_event = func; |
| 390 | } |
| 391 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 392 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 393 | void *opaque, int absolute, |
| 394 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 395 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 396 | QEMUPutMouseEntry *s, *cursor; |
| 397 | |
| 398 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 399 | |
| 400 | s->qemu_put_mouse_event = func; |
| 401 | s->qemu_put_mouse_event_opaque = opaque; |
| 402 | s->qemu_put_mouse_event_absolute = absolute; |
| 403 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 404 | s->next = NULL; |
| 405 | |
| 406 | if (!qemu_put_mouse_event_head) { |
| 407 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 408 | return s; |
| 409 | } |
| 410 | |
| 411 | cursor = qemu_put_mouse_event_head; |
| 412 | while (cursor->next != NULL) |
| 413 | cursor = cursor->next; |
| 414 | |
| 415 | cursor->next = s; |
| 416 | qemu_put_mouse_event_current = s; |
| 417 | |
| 418 | return s; |
| 419 | } |
| 420 | |
| 421 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 422 | { |
| 423 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 424 | |
| 425 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 426 | return; |
| 427 | |
| 428 | cursor = qemu_put_mouse_event_head; |
| 429 | while (cursor != NULL && cursor != entry) { |
| 430 | prev = cursor; |
| 431 | cursor = cursor->next; |
| 432 | } |
| 433 | |
| 434 | if (cursor == NULL) // does not exist or list empty |
| 435 | return; |
| 436 | else if (prev == NULL) { // entry is head |
| 437 | qemu_put_mouse_event_head = cursor->next; |
| 438 | if (qemu_put_mouse_event_current == entry) |
| 439 | qemu_put_mouse_event_current = cursor->next; |
| 440 | qemu_free(entry->qemu_put_mouse_event_name); |
| 441 | qemu_free(entry); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | prev->next = entry->next; |
| 446 | |
| 447 | if (qemu_put_mouse_event_current == entry) |
| 448 | qemu_put_mouse_event_current = prev; |
| 449 | |
| 450 | qemu_free(entry->qemu_put_mouse_event_name); |
| 451 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void kbd_put_keycode(int keycode) |
| 455 | { |
| 456 | if (qemu_put_kbd_event) { |
| 457 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 462 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 463 | QEMUPutMouseEvent *mouse_event; |
| 464 | void *mouse_event_opaque; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 465 | int width; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 466 | |
| 467 | if (!qemu_put_mouse_event_current) { |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | mouse_event = |
| 472 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 473 | mouse_event_opaque = |
| 474 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 475 | |
| 476 | if (mouse_event) { |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 477 | if (graphic_rotate) { |
| 478 | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute) |
| 479 | width = 0x7fff; |
| 480 | else |
aurel32 | b94ed57 | 2008-03-10 19:34:27 +0000 | [diff] [blame] | 481 | width = graphic_width - 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 482 | mouse_event(mouse_event_opaque, |
| 483 | width - dy, dx, dz, buttons_state); |
| 484 | } else |
| 485 | mouse_event(mouse_event_opaque, |
| 486 | dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 490 | int kbd_mouse_is_absolute(void) |
| 491 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 492 | if (!qemu_put_mouse_event_current) |
| 493 | return 0; |
| 494 | |
| 495 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 496 | } |
| 497 | |
Luiz Capitulino | e78c48e | 2009-12-10 17:16:04 -0200 | [diff] [blame] | 498 | static void info_mice_iter(QObject *data, void *opaque) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 499 | { |
Luiz Capitulino | e78c48e | 2009-12-10 17:16:04 -0200 | [diff] [blame] | 500 | QDict *mouse; |
| 501 | Monitor *mon = opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 502 | |
Luiz Capitulino | e78c48e | 2009-12-10 17:16:04 -0200 | [diff] [blame] | 503 | mouse = qobject_to_qdict(data); |
| 504 | monitor_printf(mon, "%c Mouse #%" PRId64 ": %s\n", |
| 505 | (qdict_get_bool(mouse, "current") ? '*' : ' '), |
| 506 | qdict_get_int(mouse, "index"), qdict_get_str(mouse, "name")); |
| 507 | } |
| 508 | |
| 509 | void do_info_mice_print(Monitor *mon, const QObject *data) |
| 510 | { |
| 511 | QList *mice_list; |
| 512 | |
| 513 | mice_list = qobject_to_qlist(data); |
| 514 | if (qlist_empty(mice_list)) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 515 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | |
Luiz Capitulino | e78c48e | 2009-12-10 17:16:04 -0200 | [diff] [blame] | 519 | qlist_iter(mice_list, info_mice_iter, mon); |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * do_info_mice(): Show VM mice information |
| 524 | * |
| 525 | * Each mouse is represented by a QDict, the returned QObject is a QList of |
| 526 | * all mice. |
| 527 | * |
| 528 | * The mouse QDict contains the following: |
| 529 | * |
| 530 | * - "name": mouse's name |
| 531 | * - "index": mouse's index |
| 532 | * - "current": true if this mouse is receiving events, false otherwise |
| 533 | * |
| 534 | * Example: |
| 535 | * |
| 536 | * [ { "name": "QEMU Microsoft Mouse", "index": 0, "current": false }, |
| 537 | * { "name": "QEMU PS/2 Mouse", "index": 1, "current": true } ] |
| 538 | */ |
| 539 | void do_info_mice(Monitor *mon, QObject **ret_data) |
| 540 | { |
| 541 | QEMUPutMouseEntry *cursor; |
| 542 | QList *mice_list; |
| 543 | int index = 0; |
| 544 | |
| 545 | mice_list = qlist_new(); |
| 546 | |
| 547 | if (!qemu_put_mouse_event_head) { |
| 548 | goto out; |
| 549 | } |
| 550 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 551 | cursor = qemu_put_mouse_event_head; |
| 552 | while (cursor != NULL) { |
Luiz Capitulino | e78c48e | 2009-12-10 17:16:04 -0200 | [diff] [blame] | 553 | QObject *obj; |
| 554 | obj = qobject_from_jsonf("{ 'name': %s, 'index': %d, 'current': %i }", |
| 555 | cursor->qemu_put_mouse_event_name, |
| 556 | index, cursor == qemu_put_mouse_event_current); |
| 557 | qlist_append_obj(mice_list, obj); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 558 | index++; |
| 559 | cursor = cursor->next; |
| 560 | } |
Luiz Capitulino | e78c48e | 2009-12-10 17:16:04 -0200 | [diff] [blame] | 561 | |
| 562 | out: |
| 563 | *ret_data = QOBJECT(mice_list); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 566 | void do_mouse_set(Monitor *mon, const QDict *qdict) |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 567 | { |
| 568 | QEMUPutMouseEntry *cursor; |
| 569 | int i = 0; |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 570 | int index = qdict_get_int(qdict, "index"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 571 | |
| 572 | if (!qemu_put_mouse_event_head) { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 573 | monitor_printf(mon, "No mouse devices connected\n"); |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
| 577 | cursor = qemu_put_mouse_event_head; |
| 578 | while (cursor != NULL && index != i) { |
| 579 | i++; |
| 580 | cursor = cursor->next; |
| 581 | } |
| 582 | |
| 583 | if (cursor != NULL) |
| 584 | qemu_put_mouse_event_current = cursor; |
| 585 | else |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 586 | monitor_printf(mon, "Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 587 | } |
| 588 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 589 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 590 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 591 | { |
| 592 | union { |
| 593 | uint64_t ll; |
| 594 | struct { |
Juan Quintela | e2542fe | 2009-07-27 16:13:06 +0200 | [diff] [blame] | 595 | #ifdef HOST_WORDS_BIGENDIAN |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 596 | uint32_t high, low; |
| 597 | #else |
| 598 | uint32_t low, high; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 599 | #endif |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 600 | } l; |
| 601 | } u, res; |
| 602 | uint64_t rl, rh; |
| 603 | |
| 604 | u.ll = a; |
| 605 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 606 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 607 | rh += (rl >> 32); |
| 608 | res.l.high = rh / c; |
| 609 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 610 | return res.ll; |
| 611 | } |
| 612 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 613 | /***********************************************************/ |
| 614 | /* real time host monotonic timer */ |
| 615 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 616 | static int64_t get_clock_realtime(void) |
| 617 | { |
| 618 | struct timeval tv; |
| 619 | |
| 620 | gettimeofday(&tv, NULL); |
| 621 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 622 | } |
| 623 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 624 | #ifdef WIN32 |
| 625 | |
| 626 | static int64_t clock_freq; |
| 627 | |
| 628 | static void init_get_clock(void) |
| 629 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 630 | LARGE_INTEGER freq; |
| 631 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 632 | ret = QueryPerformanceFrequency(&freq); |
| 633 | if (ret == 0) { |
| 634 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 635 | exit(1); |
| 636 | } |
| 637 | clock_freq = freq.QuadPart; |
| 638 | } |
| 639 | |
| 640 | static int64_t get_clock(void) |
| 641 | { |
| 642 | LARGE_INTEGER ti; |
| 643 | QueryPerformanceCounter(&ti); |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 644 | return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq); |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | #else |
| 648 | |
| 649 | static int use_rt_clock; |
| 650 | |
| 651 | static void init_get_clock(void) |
| 652 | { |
| 653 | use_rt_clock = 0; |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 654 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 655 | || defined(__DragonFly__) || defined(__FreeBSD_kernel__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 656 | { |
| 657 | struct timespec ts; |
| 658 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 659 | use_rt_clock = 1; |
| 660 | } |
| 661 | } |
| 662 | #endif |
| 663 | } |
| 664 | |
| 665 | static int64_t get_clock(void) |
| 666 | { |
blueswir1 | c5e9723 | 2009-03-07 20:06:23 +0000 | [diff] [blame] | 667 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
Aurelien Jarno | a167ba5 | 2009-11-29 18:00:41 +0100 | [diff] [blame] | 668 | || defined(__DragonFly__) || defined(__FreeBSD_kernel__) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 669 | if (use_rt_clock) { |
| 670 | struct timespec ts; |
| 671 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 672 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 673 | } else |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 674 | #endif |
| 675 | { |
| 676 | /* XXX: using gettimeofday leads to problems if the date |
| 677 | changes, so it should be avoided. */ |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 678 | return get_clock_realtime(); |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 681 | #endif |
| 682 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 683 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 684 | static int64_t cpu_get_icount(void) |
| 685 | { |
| 686 | int64_t icount; |
| 687 | CPUState *env = cpu_single_env;; |
| 688 | icount = qemu_icount; |
| 689 | if (env) { |
| 690 | if (!can_do_io(env)) |
| 691 | fprintf(stderr, "Bad clock read\n"); |
| 692 | icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 693 | } |
| 694 | return qemu_icount_bias + (icount << icount_time_shift); |
| 695 | } |
| 696 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 697 | /***********************************************************/ |
| 698 | /* guest cycle counter */ |
| 699 | |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 700 | typedef struct TimersState { |
| 701 | int64_t cpu_ticks_prev; |
| 702 | int64_t cpu_ticks_offset; |
| 703 | int64_t cpu_clock_offset; |
| 704 | int32_t cpu_ticks_enabled; |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 705 | int64_t dummy; |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 706 | } TimersState; |
| 707 | |
| 708 | TimersState timers_state; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 709 | |
| 710 | /* return the host CPU cycle counter and handle stop/restart */ |
| 711 | int64_t cpu_get_ticks(void) |
| 712 | { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 713 | if (use_icount) { |
| 714 | return cpu_get_icount(); |
| 715 | } |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 716 | if (!timers_state.cpu_ticks_enabled) { |
| 717 | return timers_state.cpu_ticks_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 718 | } else { |
| 719 | int64_t ticks; |
| 720 | ticks = cpu_get_real_ticks(); |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 721 | if (timers_state.cpu_ticks_prev > ticks) { |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 722 | /* Note: non increasing ticks may happen if the host uses |
| 723 | software suspend */ |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 724 | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 725 | } |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 726 | timers_state.cpu_ticks_prev = ticks; |
| 727 | return ticks + timers_state.cpu_ticks_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
| 731 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 732 | static int64_t cpu_get_clock(void) |
| 733 | { |
| 734 | int64_t ti; |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 735 | if (!timers_state.cpu_ticks_enabled) { |
| 736 | return timers_state.cpu_clock_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 737 | } else { |
| 738 | ti = get_clock(); |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 739 | return ti + timers_state.cpu_clock_offset; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
| 743 | /* enable cpu_get_ticks() */ |
| 744 | void cpu_enable_ticks(void) |
| 745 | { |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 746 | if (!timers_state.cpu_ticks_enabled) { |
| 747 | timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); |
| 748 | timers_state.cpu_clock_offset -= get_clock(); |
| 749 | timers_state.cpu_ticks_enabled = 1; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
| 753 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 754 | cpu_get_ticks() after that. */ |
| 755 | void cpu_disable_ticks(void) |
| 756 | { |
Juan Quintela | 6f68e33 | 2009-09-10 03:04:27 +0200 | [diff] [blame] | 757 | if (timers_state.cpu_ticks_enabled) { |
| 758 | timers_state.cpu_ticks_offset = cpu_get_ticks(); |
| 759 | timers_state.cpu_clock_offset = cpu_get_clock(); |
| 760 | timers_state.cpu_ticks_enabled = 0; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
| 764 | /***********************************************************/ |
| 765 | /* timers */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 766 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 767 | #define QEMU_CLOCK_REALTIME 0 |
| 768 | #define QEMU_CLOCK_VIRTUAL 1 |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 769 | #define QEMU_CLOCK_HOST 2 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 770 | |
| 771 | struct QEMUClock { |
| 772 | int type; |
| 773 | /* XXX: add frequency */ |
| 774 | }; |
| 775 | |
| 776 | struct QEMUTimer { |
| 777 | QEMUClock *clock; |
| 778 | int64_t expire_time; |
| 779 | QEMUTimerCB *cb; |
| 780 | void *opaque; |
| 781 | struct QEMUTimer *next; |
| 782 | }; |
| 783 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 784 | struct qemu_alarm_timer { |
| 785 | char const *name; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 786 | unsigned int flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 787 | |
| 788 | int (*start)(struct qemu_alarm_timer *t); |
| 789 | void (*stop)(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 790 | void (*rearm)(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 791 | void *priv; |
| 792 | }; |
| 793 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 794 | #define ALARM_FLAG_DYNTICKS 0x1 |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 795 | #define ALARM_FLAG_EXPIRED 0x2 |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 796 | |
| 797 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
| 798 | { |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 799 | return t && (t->flags & ALARM_FLAG_DYNTICKS); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
| 803 | { |
| 804 | if (!alarm_has_dynticks(t)) |
| 805 | return; |
| 806 | |
| 807 | t->rearm(t); |
| 808 | } |
| 809 | |
| 810 | /* TODO: MIN_TIMER_REARM_US should be optimized */ |
| 811 | #define MIN_TIMER_REARM_US 250 |
| 812 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 813 | static struct qemu_alarm_timer *alarm_timer; |
| 814 | |
| 815 | #ifdef _WIN32 |
| 816 | |
| 817 | struct qemu_alarm_win32 { |
| 818 | MMRESULT timerId; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 819 | unsigned int period; |
Blue Swirl | ef28c4b | 2009-04-25 12:56:37 +0000 | [diff] [blame] | 820 | } alarm_win32_data = {0, -1}; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 821 | |
| 822 | static int win32_start_timer(struct qemu_alarm_timer *t); |
| 823 | static void win32_stop_timer(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 824 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 825 | |
| 826 | #else |
| 827 | |
| 828 | static int unix_start_timer(struct qemu_alarm_timer *t); |
| 829 | static void unix_stop_timer(struct qemu_alarm_timer *t); |
| 830 | |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 831 | #ifdef __linux__ |
| 832 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 833 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
| 834 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
| 835 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
| 836 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 837 | static int hpet_start_timer(struct qemu_alarm_timer *t); |
| 838 | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
| 839 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 840 | static int rtc_start_timer(struct qemu_alarm_timer *t); |
| 841 | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
| 842 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 843 | #endif /* __linux__ */ |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 844 | |
| 845 | #endif /* _WIN32 */ |
| 846 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 847 | /* Correlation between real and virtual time is always going to be |
ths | bf20dc0 | 2008-06-30 17:22:19 +0000 | [diff] [blame] | 848 | fairly approximate, so ignore small variation. |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 849 | When the guest is idle real and virtual time will be aligned in |
| 850 | the IO wait loop. */ |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 851 | #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 852 | |
| 853 | static void icount_adjust(void) |
| 854 | { |
| 855 | int64_t cur_time; |
| 856 | int64_t cur_icount; |
| 857 | int64_t delta; |
| 858 | static int64_t last_delta; |
| 859 | /* If the VM is not running, then do nothing. */ |
| 860 | if (!vm_running) |
| 861 | return; |
| 862 | |
| 863 | cur_time = cpu_get_clock(); |
| 864 | cur_icount = qemu_get_clock(vm_clock); |
| 865 | delta = cur_icount - cur_time; |
| 866 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 867 | if (delta > 0 |
| 868 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 869 | && icount_time_shift > 0) { |
| 870 | /* The guest is getting too far ahead. Slow time down. */ |
| 871 | icount_time_shift--; |
| 872 | } |
| 873 | if (delta < 0 |
| 874 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 875 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 876 | /* The guest is getting too far behind. Speed time up. */ |
| 877 | icount_time_shift++; |
| 878 | } |
| 879 | last_delta = delta; |
| 880 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
| 881 | } |
| 882 | |
| 883 | static void icount_adjust_rt(void * opaque) |
| 884 | { |
| 885 | qemu_mod_timer(icount_rt_timer, |
| 886 | qemu_get_clock(rt_clock) + 1000); |
| 887 | icount_adjust(); |
| 888 | } |
| 889 | |
| 890 | static void icount_adjust_vm(void * opaque) |
| 891 | { |
| 892 | qemu_mod_timer(icount_vm_timer, |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 893 | qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 894 | icount_adjust(); |
| 895 | } |
| 896 | |
| 897 | static void init_icount_adjust(void) |
| 898 | { |
| 899 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 900 | The realtime trigger catches emulated time passing too slowly, |
| 901 | the virtual time trigger catches emulated time passing too fast. |
| 902 | Realtime triggers occur even when idle, so use them less frequently |
| 903 | than VM triggers. */ |
| 904 | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL); |
| 905 | qemu_mod_timer(icount_rt_timer, |
| 906 | qemu_get_clock(rt_clock) + 1000); |
| 907 | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL); |
| 908 | qemu_mod_timer(icount_vm_timer, |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 909 | qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 910 | } |
| 911 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 912 | static struct qemu_alarm_timer alarm_timers[] = { |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 913 | #ifndef _WIN32 |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 914 | #ifdef __linux__ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 915 | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer, |
| 916 | dynticks_stop_timer, dynticks_rearm_timer, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 917 | /* HPET - if available - is preferred */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 918 | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 919 | /* ...otherwise try RTC */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 920 | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 921 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 922 | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 923 | #else |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 924 | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer, |
| 925 | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
| 926 | {"win32", 0, win32_start_timer, |
| 927 | win32_stop_timer, NULL, &alarm_win32_data}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 928 | #endif |
| 929 | {NULL, } |
| 930 | }; |
| 931 | |
blueswir1 | 3f47aa8 | 2008-03-09 06:59:01 +0000 | [diff] [blame] | 932 | static void show_available_alarms(void) |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 933 | { |
| 934 | int i; |
| 935 | |
| 936 | printf("Available alarm timers, in order of precedence:\n"); |
| 937 | for (i = 0; alarm_timers[i].name; i++) |
| 938 | printf("%s\n", alarm_timers[i].name); |
| 939 | } |
| 940 | |
| 941 | static void configure_alarms(char const *opt) |
| 942 | { |
| 943 | int i; |
| 944 | int cur = 0; |
malc | b1503cd | 2008-12-22 20:33:55 +0000 | [diff] [blame] | 945 | int count = ARRAY_SIZE(alarm_timers) - 1; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 946 | char *arg; |
| 947 | char *name; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 948 | struct qemu_alarm_timer tmp; |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 949 | |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 950 | if (!strcmp(opt, "?")) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 951 | show_available_alarms(); |
| 952 | exit(0); |
| 953 | } |
| 954 | |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 955 | arg = qemu_strdup(opt); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 956 | |
| 957 | /* Reorder the array */ |
| 958 | name = strtok(arg, ","); |
| 959 | while (name) { |
balrog | e2b577e | 2007-09-17 21:25:20 +0000 | [diff] [blame] | 960 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 961 | if (!strcmp(alarm_timers[i].name, name)) |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | if (i == count) { |
| 966 | fprintf(stderr, "Unknown clock %s\n", name); |
| 967 | goto next; |
| 968 | } |
| 969 | |
| 970 | if (i < cur) |
| 971 | /* Ignore */ |
| 972 | goto next; |
| 973 | |
| 974 | /* Swap */ |
| 975 | tmp = alarm_timers[i]; |
| 976 | alarm_timers[i] = alarm_timers[cur]; |
| 977 | alarm_timers[cur] = tmp; |
| 978 | |
| 979 | cur++; |
| 980 | next: |
| 981 | name = strtok(NULL, ","); |
| 982 | } |
| 983 | |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 984 | qemu_free(arg); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 985 | |
| 986 | if (cur) { |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 987 | /* Disable remaining timers */ |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 988 | for (i = cur; i < count; i++) |
| 989 | alarm_timers[i].name = NULL; |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 990 | } else { |
| 991 | show_available_alarms(); |
| 992 | exit(1); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 993 | } |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 996 | #define QEMU_NUM_CLOCKS 3 |
| 997 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 998 | QEMUClock *rt_clock; |
| 999 | QEMUClock *vm_clock; |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1000 | QEMUClock *host_clock; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1001 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1002 | static QEMUTimer *active_timers[QEMU_NUM_CLOCKS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1003 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1004 | static QEMUClock *qemu_new_clock(int type) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1005 | { |
| 1006 | QEMUClock *clock; |
| 1007 | clock = qemu_mallocz(sizeof(QEMUClock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1008 | clock->type = type; |
| 1009 | return clock; |
| 1010 | } |
| 1011 | |
| 1012 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 1013 | { |
| 1014 | QEMUTimer *ts; |
| 1015 | |
| 1016 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 1017 | ts->clock = clock; |
| 1018 | ts->cb = cb; |
| 1019 | ts->opaque = opaque; |
| 1020 | return ts; |
| 1021 | } |
| 1022 | |
| 1023 | void qemu_free_timer(QEMUTimer *ts) |
| 1024 | { |
| 1025 | qemu_free(ts); |
| 1026 | } |
| 1027 | |
| 1028 | /* stop a timer, but do not dealloc it */ |
| 1029 | void qemu_del_timer(QEMUTimer *ts) |
| 1030 | { |
| 1031 | QEMUTimer **pt, *t; |
| 1032 | |
| 1033 | /* NOTE: this code must be signal safe because |
| 1034 | qemu_timer_expired() can be called from a signal. */ |
| 1035 | pt = &active_timers[ts->clock->type]; |
| 1036 | for(;;) { |
| 1037 | t = *pt; |
| 1038 | if (!t) |
| 1039 | break; |
| 1040 | if (t == ts) { |
| 1041 | *pt = t->next; |
| 1042 | break; |
| 1043 | } |
| 1044 | pt = &t->next; |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | /* modify the current timer so that it will be fired when current_time |
| 1049 | >= expire_time. The corresponding callback will be called. */ |
| 1050 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 1051 | { |
| 1052 | QEMUTimer **pt, *t; |
| 1053 | |
| 1054 | qemu_del_timer(ts); |
| 1055 | |
| 1056 | /* add the timer in the sorted list */ |
| 1057 | /* NOTE: this code must be signal safe because |
| 1058 | qemu_timer_expired() can be called from a signal. */ |
| 1059 | pt = &active_timers[ts->clock->type]; |
| 1060 | for(;;) { |
| 1061 | t = *pt; |
| 1062 | if (!t) |
| 1063 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1064 | if (t->expire_time > expire_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | pt = &t->next; |
| 1067 | } |
| 1068 | ts->expire_time = expire_time; |
| 1069 | ts->next = *pt; |
| 1070 | *pt = ts; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1071 | |
| 1072 | /* Rearm if necessary */ |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1073 | if (pt == &active_timers[ts->clock->type]) { |
| 1074 | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
| 1075 | qemu_rearm_alarm_timer(alarm_timer); |
| 1076 | } |
| 1077 | /* Interrupt execution to force deadline recalculation. */ |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 1078 | if (use_icount) |
| 1079 | qemu_notify_event(); |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1080 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | int qemu_timer_pending(QEMUTimer *ts) |
| 1084 | { |
| 1085 | QEMUTimer *t; |
| 1086 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 1087 | if (t == ts) |
| 1088 | return 1; |
| 1089 | } |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
Stefano Stabellini | 2430ffe | 2009-08-03 10:56:01 +0100 | [diff] [blame] | 1093 | int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1094 | { |
| 1095 | if (!timer_head) |
| 1096 | return 0; |
| 1097 | return (timer_head->expire_time <= current_time); |
| 1098 | } |
| 1099 | |
| 1100 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 1101 | { |
| 1102 | QEMUTimer *ts; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1103 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1104 | for(;;) { |
| 1105 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 1106 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1107 | break; |
| 1108 | /* remove timer from the list before calling the callback */ |
| 1109 | *ptimer_head = ts->next; |
| 1110 | ts->next = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1111 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1112 | /* run the callback (the timer list can be modified) */ |
| 1113 | ts->cb(ts->opaque); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | int64_t qemu_get_clock(QEMUClock *clock) |
| 1118 | { |
| 1119 | switch(clock->type) { |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1120 | case QEMU_CLOCK_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1121 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1122 | default: |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1123 | case QEMU_CLOCK_VIRTUAL: |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1124 | if (use_icount) { |
| 1125 | return cpu_get_icount(); |
| 1126 | } else { |
| 1127 | return cpu_get_clock(); |
| 1128 | } |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1129 | case QEMU_CLOCK_HOST: |
| 1130 | return get_clock_realtime(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1134 | static void init_clocks(void) |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1135 | { |
| 1136 | init_get_clock(); |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1137 | rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME); |
| 1138 | vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL); |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1139 | host_clock = qemu_new_clock(QEMU_CLOCK_HOST); |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1140 | |
| 1141 | rtc_clock = host_clock; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1144 | /* save a timer */ |
| 1145 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 1146 | { |
| 1147 | uint64_t expire_time; |
| 1148 | |
| 1149 | if (qemu_timer_pending(ts)) { |
| 1150 | expire_time = ts->expire_time; |
| 1151 | } else { |
| 1152 | expire_time = -1; |
| 1153 | } |
| 1154 | qemu_put_be64(f, expire_time); |
| 1155 | } |
| 1156 | |
| 1157 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 1158 | { |
| 1159 | uint64_t expire_time; |
| 1160 | |
| 1161 | expire_time = qemu_get_be64(f); |
| 1162 | if (expire_time != -1) { |
| 1163 | qemu_mod_timer(ts, expire_time); |
| 1164 | } else { |
| 1165 | qemu_del_timer(ts); |
| 1166 | } |
| 1167 | } |
| 1168 | |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 1169 | static const VMStateDescription vmstate_timers = { |
| 1170 | .name = "timer", |
| 1171 | .version_id = 2, |
| 1172 | .minimum_version_id = 1, |
| 1173 | .minimum_version_id_old = 1, |
| 1174 | .fields = (VMStateField []) { |
| 1175 | VMSTATE_INT64(cpu_ticks_offset, TimersState), |
Anthony Liguori | 274dfed | 2009-09-11 10:28:26 -0500 | [diff] [blame] | 1176 | VMSTATE_INT64(dummy, TimersState), |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 1177 | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2), |
| 1178 | VMSTATE_END_OF_LIST() |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1179 | } |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 1180 | }; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1181 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1182 | static void qemu_event_increment(void); |
| 1183 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1184 | #ifdef _WIN32 |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 1185 | static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 1186 | DWORD_PTR dwUser, DWORD_PTR dw1, |
| 1187 | DWORD_PTR dw2) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1188 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1189 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1190 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1191 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1192 | #if 0 |
| 1193 | #define DISP_FREQ 1000 |
| 1194 | { |
| 1195 | static int64_t delta_min = INT64_MAX; |
| 1196 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 1197 | static int count; |
| 1198 | ti = qemu_get_clock(vm_clock); |
| 1199 | if (last_clock != 0) { |
| 1200 | delta = ti - last_clock; |
| 1201 | if (delta < delta_min) |
| 1202 | delta_min = delta; |
| 1203 | if (delta > delta_max) |
| 1204 | delta_max = delta; |
| 1205 | delta_cum += delta; |
| 1206 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 1207 | 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] | 1208 | muldiv64(delta_min, 1000000, get_ticks_per_sec()), |
| 1209 | muldiv64(delta_max, 1000000, get_ticks_per_sec()), |
| 1210 | muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()), |
| 1211 | (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ)); |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1212 | count = 0; |
| 1213 | delta_min = INT64_MAX; |
| 1214 | delta_max = 0; |
| 1215 | delta_cum = 0; |
| 1216 | } |
| 1217 | } |
| 1218 | last_clock = ti; |
| 1219 | } |
| 1220 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1221 | if (alarm_has_dynticks(alarm_timer) || |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1222 | (!use_icount && |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1223 | qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL], |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1224 | qemu_get_clock(vm_clock))) || |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1225 | qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME], |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1226 | qemu_get_clock(rt_clock)) || |
| 1227 | qemu_timer_expired(active_timers[QEMU_CLOCK_HOST], |
| 1228 | qemu_get_clock(host_clock))) { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 1229 | qemu_event_increment(); |
Jean-Christophe Dubois | e332340 | 2009-05-17 18:38:39 +0200 | [diff] [blame] | 1230 | if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1231 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1232 | #ifndef CONFIG_IOTHREAD |
| 1233 | if (next_cpu) { |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1234 | /* stop the currently executing cpu because a timer occured */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1235 | cpu_exit(next_cpu); |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1236 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 1237 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 1238 | timer_alarm_pending = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 1239 | qemu_notify_event(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1243 | static int64_t qemu_next_deadline(void) |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1244 | { |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1245 | /* To avoid problems with overflow limit this to 2^32. */ |
| 1246 | int64_t delta = INT32_MAX; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1247 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1248 | if (active_timers[QEMU_CLOCK_VIRTUAL]) { |
| 1249 | delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time - |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1250 | qemu_get_clock(vm_clock); |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1251 | } |
| 1252 | if (active_timers[QEMU_CLOCK_HOST]) { |
| 1253 | int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time - |
| 1254 | qemu_get_clock(host_clock); |
| 1255 | if (hdelta < delta) |
| 1256 | delta = hdelta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1259 | if (delta < 0) |
| 1260 | delta = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1261 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1262 | return delta; |
| 1263 | } |
| 1264 | |
Jan Kiszka | f64382b | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1265 | #if defined(__linux__) |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1266 | static uint64_t qemu_next_deadline_dyntick(void) |
| 1267 | { |
| 1268 | int64_t delta; |
| 1269 | int64_t rtdelta; |
| 1270 | |
| 1271 | if (use_icount) |
| 1272 | delta = INT32_MAX; |
| 1273 | else |
| 1274 | delta = (qemu_next_deadline() + 999) / 1000; |
| 1275 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1276 | if (active_timers[QEMU_CLOCK_REALTIME]) { |
| 1277 | rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time - |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1278 | qemu_get_clock(rt_clock))*1000; |
| 1279 | if (rtdelta < delta) |
| 1280 | delta = rtdelta; |
| 1281 | } |
| 1282 | |
| 1283 | if (delta < MIN_TIMER_REARM_US) |
| 1284 | delta = MIN_TIMER_REARM_US; |
| 1285 | |
| 1286 | return delta; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1287 | } |
blueswir1 | 8632fb9 | 2008-09-14 13:59:34 +0000 | [diff] [blame] | 1288 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1289 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1290 | #ifndef _WIN32 |
| 1291 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1292 | /* Sets a specific flag */ |
| 1293 | static int fcntl_setfl(int fd, int flag) |
| 1294 | { |
| 1295 | int flags; |
| 1296 | |
| 1297 | flags = fcntl(fd, F_GETFL); |
| 1298 | if (flags == -1) |
| 1299 | return -errno; |
| 1300 | |
| 1301 | if (fcntl(fd, F_SETFL, flags | flag) == -1) |
| 1302 | return -errno; |
| 1303 | |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1307 | #if defined(__linux__) |
| 1308 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1309 | #define RTC_FREQ 1024 |
| 1310 | |
aurel32 | de9a95f | 2008-11-11 13:41:01 +0000 | [diff] [blame] | 1311 | static void enable_sigio_timer(int fd) |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1312 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1313 | struct sigaction act; |
| 1314 | |
| 1315 | /* timer signal */ |
| 1316 | sigfillset(&act.sa_mask); |
| 1317 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1318 | act.sa_handler = host_alarm_handler; |
| 1319 | |
| 1320 | sigaction(SIGIO, &act, NULL); |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1321 | fcntl_setfl(fd, O_ASYNC); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1322 | fcntl(fd, F_SETOWN, getpid()); |
| 1323 | } |
| 1324 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1325 | static int hpet_start_timer(struct qemu_alarm_timer *t) |
| 1326 | { |
| 1327 | struct hpet_info info; |
| 1328 | int r, fd; |
| 1329 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1330 | fd = qemu_open("/dev/hpet", O_RDONLY); |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1331 | if (fd < 0) |
| 1332 | return -1; |
| 1333 | |
| 1334 | /* Set frequency */ |
| 1335 | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
| 1336 | if (r < 0) { |
| 1337 | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n" |
| 1338 | "error, but for better emulation accuracy type:\n" |
| 1339 | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n"); |
| 1340 | goto fail; |
| 1341 | } |
| 1342 | |
| 1343 | /* Check capabilities */ |
| 1344 | r = ioctl(fd, HPET_INFO, &info); |
| 1345 | if (r < 0) |
| 1346 | goto fail; |
| 1347 | |
| 1348 | /* Enable periodic mode */ |
| 1349 | r = ioctl(fd, HPET_EPI, 0); |
| 1350 | if (info.hi_flags && (r < 0)) |
| 1351 | goto fail; |
| 1352 | |
| 1353 | /* Enable interrupt */ |
| 1354 | r = ioctl(fd, HPET_IE_ON, 0); |
| 1355 | if (r < 0) |
| 1356 | goto fail; |
| 1357 | |
| 1358 | enable_sigio_timer(fd); |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1359 | t->priv = (void *)(long)fd; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1360 | |
| 1361 | return 0; |
| 1362 | fail: |
| 1363 | close(fd); |
| 1364 | return -1; |
| 1365 | } |
| 1366 | |
| 1367 | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
| 1368 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1369 | int fd = (long)t->priv; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1370 | |
| 1371 | close(fd); |
| 1372 | } |
| 1373 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1374 | static int rtc_start_timer(struct qemu_alarm_timer *t) |
| 1375 | { |
| 1376 | int rtc_fd; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1377 | unsigned long current_rtc_freq = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1378 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 1379 | TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY)); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1380 | if (rtc_fd < 0) |
| 1381 | return -1; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1382 | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
| 1383 | if (current_rtc_freq != RTC_FREQ && |
| 1384 | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1385 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1386 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1387 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1388 | goto fail; |
| 1389 | } |
| 1390 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1391 | fail: |
| 1392 | close(rtc_fd); |
| 1393 | return -1; |
| 1394 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1395 | |
| 1396 | enable_sigio_timer(rtc_fd); |
| 1397 | |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1398 | t->priv = (void *)(long)rtc_fd; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1399 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
| 1402 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1403 | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1404 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1405 | int rtc_fd = (long)t->priv; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1406 | |
| 1407 | close(rtc_fd); |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1410 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
| 1411 | { |
| 1412 | struct sigevent ev; |
| 1413 | timer_t host_timer; |
| 1414 | struct sigaction act; |
| 1415 | |
| 1416 | sigfillset(&act.sa_mask); |
| 1417 | act.sa_flags = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1418 | act.sa_handler = host_alarm_handler; |
| 1419 | |
| 1420 | sigaction(SIGALRM, &act, NULL); |
| 1421 | |
Jean-Christophe Dubois | 9ed415b | 2009-05-17 18:41:16 +0200 | [diff] [blame] | 1422 | /* |
| 1423 | * Initialize ev struct to 0 to avoid valgrind complaining |
| 1424 | * about uninitialized data in timer_create call |
| 1425 | */ |
| 1426 | memset(&ev, 0, sizeof(ev)); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1427 | ev.sigev_value.sival_int = 0; |
| 1428 | ev.sigev_notify = SIGEV_SIGNAL; |
| 1429 | ev.sigev_signo = SIGALRM; |
| 1430 | |
| 1431 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { |
| 1432 | perror("timer_create"); |
| 1433 | |
| 1434 | /* disable dynticks */ |
| 1435 | fprintf(stderr, "Dynamic Ticks disabled\n"); |
| 1436 | |
| 1437 | return -1; |
| 1438 | } |
| 1439 | |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1440 | t->priv = (void *)(long)host_timer; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1441 | |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
| 1445 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
| 1446 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1447 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1448 | |
| 1449 | timer_delete(host_timer); |
| 1450 | } |
| 1451 | |
| 1452 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
| 1453 | { |
blueswir1 | 0399bfe | 2008-11-16 11:37:18 +0000 | [diff] [blame] | 1454 | timer_t host_timer = (timer_t)(long)t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1455 | struct itimerspec timeout; |
| 1456 | int64_t nearest_delta_us = INT64_MAX; |
| 1457 | int64_t current_us; |
| 1458 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1459 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1460 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 1461 | !active_timers[QEMU_CLOCK_HOST]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1462 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1463 | |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 1464 | nearest_delta_us = qemu_next_deadline_dyntick(); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1465 | |
| 1466 | /* check whether a timer is already running */ |
| 1467 | if (timer_gettime(host_timer, &timeout)) { |
| 1468 | perror("gettime"); |
| 1469 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1470 | exit(1); |
| 1471 | } |
| 1472 | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
| 1473 | if (current_us && current_us <= nearest_delta_us) |
| 1474 | return; |
| 1475 | |
| 1476 | timeout.it_interval.tv_sec = 0; |
| 1477 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
| 1478 | timeout.it_value.tv_sec = nearest_delta_us / 1000000; |
| 1479 | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
| 1480 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
| 1481 | perror("settime"); |
| 1482 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1483 | exit(1); |
| 1484 | } |
| 1485 | } |
| 1486 | |
ths | 70744b3 | 2007-08-26 17:31:30 +0000 | [diff] [blame] | 1487 | #endif /* defined(__linux__) */ |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 1488 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1489 | static int unix_start_timer(struct qemu_alarm_timer *t) |
| 1490 | { |
| 1491 | struct sigaction act; |
| 1492 | struct itimerval itv; |
| 1493 | int err; |
| 1494 | |
| 1495 | /* timer signal */ |
| 1496 | sigfillset(&act.sa_mask); |
| 1497 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1498 | act.sa_handler = host_alarm_handler; |
| 1499 | |
| 1500 | sigaction(SIGALRM, &act, NULL); |
| 1501 | |
| 1502 | itv.it_interval.tv_sec = 0; |
| 1503 | /* for i386 kernel 2.6 to get 1 ms */ |
| 1504 | itv.it_interval.tv_usec = 999; |
| 1505 | itv.it_value.tv_sec = 0; |
| 1506 | itv.it_value.tv_usec = 10 * 1000; |
| 1507 | |
| 1508 | err = setitimer(ITIMER_REAL, &itv, NULL); |
| 1509 | if (err) |
| 1510 | return -1; |
| 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | static void unix_stop_timer(struct qemu_alarm_timer *t) |
| 1516 | { |
| 1517 | struct itimerval itv; |
| 1518 | |
| 1519 | memset(&itv, 0, sizeof(itv)); |
| 1520 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1521 | } |
| 1522 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1523 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1524 | |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1525 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1526 | #ifdef _WIN32 |
| 1527 | |
| 1528 | static int win32_start_timer(struct qemu_alarm_timer *t) |
| 1529 | { |
| 1530 | TIMECAPS tc; |
| 1531 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1532 | UINT flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1533 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1534 | memset(&tc, 0, sizeof(tc)); |
| 1535 | timeGetDevCaps(&tc, sizeof(tc)); |
| 1536 | |
| 1537 | if (data->period < tc.wPeriodMin) |
| 1538 | data->period = tc.wPeriodMin; |
| 1539 | |
| 1540 | timeBeginPeriod(data->period); |
| 1541 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1542 | flags = TIME_CALLBACK_FUNCTION; |
| 1543 | if (alarm_has_dynticks(t)) |
| 1544 | flags |= TIME_ONESHOT; |
| 1545 | else |
| 1546 | flags |= TIME_PERIODIC; |
| 1547 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1548 | data->timerId = timeSetEvent(1, // interval (ms) |
| 1549 | data->period, // resolution |
| 1550 | host_alarm_handler, // function |
| 1551 | (DWORD)t, // parameter |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1552 | flags); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1553 | |
| 1554 | if (!data->timerId) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 1555 | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n", |
malc | 705e83f | 2009-09-27 14:30:33 +0400 | [diff] [blame] | 1556 | GetLastError()); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1557 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1558 | return -1; |
| 1559 | } |
| 1560 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | static void win32_stop_timer(struct qemu_alarm_timer *t) |
| 1565 | { |
| 1566 | struct qemu_alarm_win32 *data = t->priv; |
| 1567 | |
| 1568 | timeKillEvent(data->timerId); |
| 1569 | timeEndPeriod(data->period); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1572 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
| 1573 | { |
| 1574 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1575 | |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1576 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1577 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 1578 | !active_timers[QEMU_CLOCK_HOST]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1579 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1580 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1581 | timeKillEvent(data->timerId); |
| 1582 | |
| 1583 | data->timerId = timeSetEvent(1, |
| 1584 | data->period, |
| 1585 | host_alarm_handler, |
| 1586 | (DWORD)t, |
| 1587 | TIME_ONESHOT | TIME_PERIODIC); |
| 1588 | |
| 1589 | if (!data->timerId) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 1590 | fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n", |
malc | 705e83f | 2009-09-27 14:30:33 +0400 | [diff] [blame] | 1591 | GetLastError()); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1592 | |
| 1593 | timeEndPeriod(data->period); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1594 | exit(1); |
| 1595 | } |
| 1596 | } |
| 1597 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1598 | #endif /* _WIN32 */ |
| 1599 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1600 | static int init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1601 | { |
blueswir1 | 223f0d7 | 2008-09-30 18:12:18 +0000 | [diff] [blame] | 1602 | struct qemu_alarm_timer *t = NULL; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1603 | int i, err = -1; |
aliguori | f49e58d | 2008-11-05 21:22:34 +0000 | [diff] [blame] | 1604 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1605 | for (i = 0; alarm_timers[i].name; i++) { |
| 1606 | t = &alarm_timers[i]; |
| 1607 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1608 | err = t->start(t); |
| 1609 | if (!err) |
| 1610 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1611 | } |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1612 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1613 | if (err) { |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1614 | err = -ENOENT; |
| 1615 | goto fail; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1616 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1617 | |
| 1618 | alarm_timer = t; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1619 | |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 1620 | return 0; |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1621 | |
| 1622 | fail: |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 1623 | return err; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1626 | static void quit_timers(void) |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1627 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1628 | alarm_timer->stop(alarm_timer); |
| 1629 | alarm_timer = NULL; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1632 | /***********************************************************/ |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 1633 | /* host time/date access */ |
| 1634 | void qemu_get_timedate(struct tm *tm, int offset) |
| 1635 | { |
| 1636 | time_t ti; |
| 1637 | struct tm *ret; |
| 1638 | |
| 1639 | time(&ti); |
| 1640 | ti += offset; |
| 1641 | if (rtc_date_offset == -1) { |
| 1642 | if (rtc_utc) |
| 1643 | ret = gmtime(&ti); |
| 1644 | else |
| 1645 | ret = localtime(&ti); |
| 1646 | } else { |
| 1647 | ti -= rtc_date_offset; |
| 1648 | ret = gmtime(&ti); |
| 1649 | } |
| 1650 | |
| 1651 | memcpy(tm, ret, sizeof(struct tm)); |
| 1652 | } |
| 1653 | |
| 1654 | int qemu_timedate_diff(struct tm *tm) |
| 1655 | { |
| 1656 | time_t seconds; |
| 1657 | |
| 1658 | if (rtc_date_offset == -1) |
| 1659 | if (rtc_utc) |
| 1660 | seconds = mktimegm(tm); |
| 1661 | else |
| 1662 | seconds = mktime(tm); |
| 1663 | else |
| 1664 | seconds = mktimegm(tm) + rtc_date_offset; |
| 1665 | |
| 1666 | return seconds - time(NULL); |
| 1667 | } |
| 1668 | |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1669 | static void configure_rtc_date_offset(const char *startdate, int legacy) |
| 1670 | { |
| 1671 | time_t rtc_start_date; |
| 1672 | struct tm tm; |
| 1673 | |
| 1674 | if (!strcmp(startdate, "now") && legacy) { |
| 1675 | rtc_date_offset = -1; |
| 1676 | } else { |
| 1677 | if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", |
| 1678 | &tm.tm_year, |
| 1679 | &tm.tm_mon, |
| 1680 | &tm.tm_mday, |
| 1681 | &tm.tm_hour, |
| 1682 | &tm.tm_min, |
| 1683 | &tm.tm_sec) == 6) { |
| 1684 | /* OK */ |
| 1685 | } else if (sscanf(startdate, "%d-%d-%d", |
| 1686 | &tm.tm_year, |
| 1687 | &tm.tm_mon, |
| 1688 | &tm.tm_mday) == 3) { |
| 1689 | tm.tm_hour = 0; |
| 1690 | tm.tm_min = 0; |
| 1691 | tm.tm_sec = 0; |
| 1692 | } else { |
| 1693 | goto date_fail; |
| 1694 | } |
| 1695 | tm.tm_year -= 1900; |
| 1696 | tm.tm_mon--; |
| 1697 | rtc_start_date = mktimegm(&tm); |
| 1698 | if (rtc_start_date == -1) { |
| 1699 | date_fail: |
| 1700 | fprintf(stderr, "Invalid date format. Valid formats are:\n" |
| 1701 | "'2006-06-17T16:01:21' or '2006-06-17'\n"); |
| 1702 | exit(1); |
| 1703 | } |
| 1704 | rtc_date_offset = time(NULL) - rtc_start_date; |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | static void configure_rtc(QemuOpts *opts) |
| 1709 | { |
| 1710 | const char *value; |
| 1711 | |
| 1712 | value = qemu_opt_get(opts, "base"); |
| 1713 | if (value) { |
| 1714 | if (!strcmp(value, "utc")) { |
| 1715 | rtc_utc = 1; |
| 1716 | } else if (!strcmp(value, "localtime")) { |
| 1717 | rtc_utc = 0; |
| 1718 | } else { |
| 1719 | configure_rtc_date_offset(value, 0); |
| 1720 | } |
| 1721 | } |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1722 | value = qemu_opt_get(opts, "clock"); |
| 1723 | if (value) { |
| 1724 | if (!strcmp(value, "host")) { |
| 1725 | rtc_clock = host_clock; |
| 1726 | } else if (!strcmp(value, "vm")) { |
| 1727 | rtc_clock = vm_clock; |
| 1728 | } else { |
| 1729 | fprintf(stderr, "qemu: invalid option value '%s'\n", value); |
| 1730 | exit(1); |
| 1731 | } |
| 1732 | } |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 1733 | #ifdef CONFIG_TARGET_I386 |
| 1734 | value = qemu_opt_get(opts, "driftfix"); |
| 1735 | if (value) { |
| 1736 | if (!strcmp(buf, "slew")) { |
| 1737 | rtc_td_hack = 1; |
| 1738 | } else if (!strcmp(buf, "none")) { |
| 1739 | rtc_td_hack = 0; |
| 1740 | } else { |
| 1741 | fprintf(stderr, "qemu: invalid option value '%s'\n", value); |
| 1742 | exit(1); |
| 1743 | } |
| 1744 | } |
| 1745 | #endif |
| 1746 | } |
| 1747 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1748 | #ifdef _WIN32 |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1749 | static void socket_cleanup(void) |
| 1750 | { |
| 1751 | WSACleanup(); |
| 1752 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1753 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1754 | static int socket_init(void) |
| 1755 | { |
| 1756 | WSADATA Data; |
| 1757 | int ret, err; |
| 1758 | |
| 1759 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1760 | if (ret != 0) { |
| 1761 | err = WSAGetLastError(); |
| 1762 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1763 | return -1; |
| 1764 | } |
| 1765 | atexit(socket_cleanup); |
| 1766 | return 0; |
| 1767 | } |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 1768 | #endif |
| 1769 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1770 | /***********************************************************/ |
| 1771 | /* Bluetooth support */ |
| 1772 | static int nb_hcis; |
| 1773 | static int cur_hci; |
| 1774 | static struct HCIInfo *hci_table[MAX_NICS]; |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1775 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1776 | static struct bt_vlan_s { |
| 1777 | struct bt_scatternet_s net; |
| 1778 | int id; |
| 1779 | struct bt_vlan_s *next; |
| 1780 | } *first_bt_vlan; |
| 1781 | |
| 1782 | /* find or alloc a new bluetooth "VLAN" */ |
blueswir1 | 674bb26 | 2008-09-30 18:18:27 +0000 | [diff] [blame] | 1783 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1784 | { |
| 1785 | struct bt_vlan_s **pvlan, *vlan; |
| 1786 | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { |
| 1787 | if (vlan->id == id) |
| 1788 | return &vlan->net; |
| 1789 | } |
| 1790 | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); |
| 1791 | vlan->id = id; |
| 1792 | pvlan = &first_bt_vlan; |
| 1793 | while (*pvlan != NULL) |
| 1794 | pvlan = &(*pvlan)->next; |
| 1795 | *pvlan = vlan; |
| 1796 | return &vlan->net; |
| 1797 | } |
| 1798 | |
| 1799 | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) |
| 1800 | { |
| 1801 | } |
| 1802 | |
| 1803 | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
| 1804 | { |
| 1805 | return -ENOTSUP; |
| 1806 | } |
| 1807 | |
| 1808 | static struct HCIInfo null_hci = { |
| 1809 | .cmd_send = null_hci_send, |
| 1810 | .sco_send = null_hci_send, |
| 1811 | .acl_send = null_hci_send, |
| 1812 | .bdaddr_set = null_hci_addr_set, |
| 1813 | }; |
| 1814 | |
| 1815 | struct HCIInfo *qemu_next_hci(void) |
| 1816 | { |
| 1817 | if (cur_hci == nb_hcis) |
| 1818 | return &null_hci; |
| 1819 | |
| 1820 | return hci_table[cur_hci++]; |
| 1821 | } |
| 1822 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1823 | static struct HCIInfo *hci_init(const char *str) |
| 1824 | { |
| 1825 | char *endp; |
| 1826 | struct bt_scatternet_s *vlan = 0; |
| 1827 | |
| 1828 | if (!strcmp(str, "null")) |
| 1829 | /* null */ |
| 1830 | return &null_hci; |
| 1831 | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) |
| 1832 | /* host[:hciN] */ |
| 1833 | return bt_host_hci(str[4] ? str + 5 : "hci0"); |
| 1834 | else if (!strncmp(str, "hci", 3)) { |
| 1835 | /* hci[,vlan=n] */ |
| 1836 | if (str[3]) { |
| 1837 | if (!strncmp(str + 3, ",vlan=", 6)) { |
| 1838 | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); |
| 1839 | if (*endp) |
| 1840 | vlan = 0; |
| 1841 | } |
| 1842 | } else |
| 1843 | vlan = qemu_find_bt_vlan(0); |
| 1844 | if (vlan) |
| 1845 | return bt_new_hci(vlan); |
| 1846 | } |
| 1847 | |
| 1848 | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str); |
| 1849 | |
| 1850 | return 0; |
| 1851 | } |
| 1852 | |
| 1853 | static int bt_hci_parse(const char *str) |
| 1854 | { |
| 1855 | struct HCIInfo *hci; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1856 | bdaddr_t bdaddr; |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 1857 | |
| 1858 | if (nb_hcis >= MAX_NICS) { |
| 1859 | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS); |
| 1860 | return -1; |
| 1861 | } |
| 1862 | |
| 1863 | hci = hci_init(str); |
| 1864 | if (!hci) |
| 1865 | return -1; |
| 1866 | |
| 1867 | bdaddr.b[0] = 0x52; |
| 1868 | bdaddr.b[1] = 0x54; |
| 1869 | bdaddr.b[2] = 0x00; |
| 1870 | bdaddr.b[3] = 0x12; |
| 1871 | bdaddr.b[4] = 0x34; |
| 1872 | bdaddr.b[5] = 0x56 + nb_hcis; |
| 1873 | hci->bdaddr_set(hci, bdaddr.b); |
| 1874 | |
| 1875 | hci_table[nb_hcis++] = hci; |
| 1876 | |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
| 1880 | static void bt_vhci_add(int vlan_id) |
| 1881 | { |
| 1882 | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id); |
| 1883 | |
| 1884 | if (!vlan->slave) |
| 1885 | fprintf(stderr, "qemu: warning: adding a VHCI to " |
| 1886 | "an empty scatternet %i\n", vlan_id); |
| 1887 | |
| 1888 | bt_vhci_init(bt_new_hci(vlan)); |
| 1889 | } |
| 1890 | |
| 1891 | static struct bt_device_s *bt_device_add(const char *opt) |
| 1892 | { |
| 1893 | struct bt_scatternet_s *vlan; |
| 1894 | int vlan_id = 0; |
| 1895 | char *endp = strstr(opt, ",vlan="); |
| 1896 | int len = (endp ? endp - opt : strlen(opt)) + 1; |
| 1897 | char devname[10]; |
| 1898 | |
| 1899 | pstrcpy(devname, MIN(sizeof(devname), len), opt); |
| 1900 | |
| 1901 | if (endp) { |
| 1902 | vlan_id = strtol(endp + 6, &endp, 0); |
| 1903 | if (*endp) { |
| 1904 | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n"); |
| 1905 | return 0; |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | vlan = qemu_find_bt_vlan(vlan_id); |
| 1910 | |
| 1911 | if (!vlan->slave) |
| 1912 | fprintf(stderr, "qemu: warning: adding a slave device to " |
| 1913 | "an empty scatternet %i\n", vlan_id); |
| 1914 | |
| 1915 | if (!strcmp(devname, "keyboard")) |
| 1916 | return bt_keyboard_init(vlan); |
| 1917 | |
| 1918 | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname); |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
| 1922 | static int bt_parse(const char *opt) |
| 1923 | { |
| 1924 | const char *endp, *p; |
| 1925 | int vlan; |
| 1926 | |
| 1927 | if (strstart(opt, "hci", &endp)) { |
| 1928 | if (!*endp || *endp == ',') { |
| 1929 | if (*endp) |
| 1930 | if (!strstart(endp, ",vlan=", 0)) |
| 1931 | opt = endp + 1; |
| 1932 | |
| 1933 | return bt_hci_parse(opt); |
| 1934 | } |
| 1935 | } else if (strstart(opt, "vhci", &endp)) { |
| 1936 | if (!*endp || *endp == ',') { |
| 1937 | if (*endp) { |
| 1938 | if (strstart(endp, ",vlan=", &p)) { |
| 1939 | vlan = strtol(p, (char **) &endp, 0); |
| 1940 | if (*endp) { |
| 1941 | fprintf(stderr, "qemu: bad scatternet '%s'\n", p); |
| 1942 | return 1; |
| 1943 | } |
| 1944 | } else { |
| 1945 | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); |
| 1946 | return 1; |
| 1947 | } |
| 1948 | } else |
| 1949 | vlan = 0; |
| 1950 | |
| 1951 | bt_vhci_add(vlan); |
| 1952 | return 0; |
| 1953 | } |
| 1954 | } else if (strstart(opt, "device:", &endp)) |
| 1955 | return !bt_device_add(endp); |
| 1956 | |
| 1957 | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt); |
| 1958 | return 1; |
| 1959 | } |
| 1960 | |
balrog | 1ae26a1 | 2008-09-28 23:19:47 +0000 | [diff] [blame] | 1961 | /***********************************************************/ |
| 1962 | /* QEMU Block devices */ |
| 1963 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1964 | #define HD_ALIAS "index=%d,media=disk" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1965 | #define CDROM_ALIAS "index=2,media=cdrom" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1966 | #define FD_ALIAS "index=%d,if=floppy" |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 1967 | #define PFLASH_ALIAS "if=pflash" |
| 1968 | #define MTD_ALIAS "if=mtd" |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 1969 | #define SD_ALIAS "index=0,if=sd" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1970 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1971 | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1972 | { |
| 1973 | va_list ap; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1974 | char optstr[1024]; |
| 1975 | QemuOpts *opts; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1976 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1977 | va_start(ap, fmt); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1978 | vsnprintf(optstr, sizeof(optstr), fmt, ap); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1979 | va_end(ap); |
| 1980 | |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 1981 | opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL); |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 1982 | if (!opts) { |
| 1983 | fprintf(stderr, "%s: huh? duplicate? (%s)\n", |
| 1984 | __FUNCTION__, optstr); |
| 1985 | return NULL; |
| 1986 | } |
| 1987 | if (file) |
| 1988 | qemu_opt_set(opts, "file", file); |
| 1989 | return opts; |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1992 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1993 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1994 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1995 | |
| 1996 | /* seek interface, bus and unit */ |
| 1997 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1998 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 1999 | if (dinfo->type == type && |
| 2000 | dinfo->bus == bus && |
| 2001 | dinfo->unit == unit) |
| 2002 | return dinfo; |
| 2003 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2004 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2005 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Gerd Hoffmann | 2e810b3 | 2009-07-31 12:25:38 +0200 | [diff] [blame] | 2008 | DriveInfo *drive_get_by_id(const char *id) |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2009 | { |
| 2010 | DriveInfo *dinfo; |
| 2011 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2012 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2013 | if (strcmp(id, dinfo->id)) |
| 2014 | continue; |
| 2015 | return dinfo; |
| 2016 | } |
| 2017 | return NULL; |
| 2018 | } |
| 2019 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2020 | int drive_get_max_bus(BlockInterfaceType type) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2021 | { |
| 2022 | int max_bus; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2023 | DriveInfo *dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2024 | |
| 2025 | max_bus = -1; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2026 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2027 | if(dinfo->type == type && |
| 2028 | dinfo->bus > max_bus) |
| 2029 | max_bus = dinfo->bus; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2030 | } |
| 2031 | return max_bus; |
| 2032 | } |
| 2033 | |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 2034 | const char *drive_get_serial(BlockDriverState *bdrv) |
| 2035 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2036 | DriveInfo *dinfo; |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 2037 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2038 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2039 | if (dinfo->bdrv == bdrv) |
| 2040 | return dinfo->serial; |
| 2041 | } |
aliguori | fa879c6 | 2009-01-07 17:32:33 +0000 | [diff] [blame] | 2042 | |
| 2043 | return "\0"; |
| 2044 | } |
| 2045 | |
Kevin Wolf | f785009 | 2009-11-27 13:25:36 +0100 | [diff] [blame] | 2046 | BlockInterfaceErrorAction drive_get_on_error( |
| 2047 | BlockDriverState *bdrv, int is_read) |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2048 | { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2049 | DriveInfo *dinfo; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2050 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2051 | QTAILQ_FOREACH(dinfo, &drives, next) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2052 | if (dinfo->bdrv == bdrv) |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2053 | return is_read ? dinfo->on_read_error : dinfo->on_write_error; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2054 | } |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2055 | |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2056 | return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2059 | static void bdrv_format_print(void *opaque, const char *name) |
| 2060 | { |
| 2061 | fprintf(stderr, " %s", name); |
| 2062 | } |
| 2063 | |
Gerd Hoffmann | 56a1493 | 2009-09-25 21:42:46 +0200 | [diff] [blame] | 2064 | void drive_uninit(DriveInfo *dinfo) |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 2065 | { |
Gerd Hoffmann | 56a1493 | 2009-09-25 21:42:46 +0200 | [diff] [blame] | 2066 | qemu_opts_del(dinfo->opts); |
| 2067 | bdrv_delete(dinfo->bdrv); |
| 2068 | QTAILQ_REMOVE(&drives, dinfo, next); |
| 2069 | qemu_free(dinfo); |
aliguori | b01b111 | 2009-02-11 15:20:20 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2072 | static int parse_block_error_action(const char *buf, int is_read) |
| 2073 | { |
| 2074 | if (!strcmp(buf, "ignore")) { |
| 2075 | return BLOCK_ERR_IGNORE; |
| 2076 | } else if (!is_read && !strcmp(buf, "enospc")) { |
| 2077 | return BLOCK_ERR_STOP_ENOSPC; |
| 2078 | } else if (!strcmp(buf, "stop")) { |
| 2079 | return BLOCK_ERR_STOP_ANY; |
| 2080 | } else if (!strcmp(buf, "report")) { |
| 2081 | return BLOCK_ERR_REPORT; |
| 2082 | } else { |
| 2083 | fprintf(stderr, "qemu: '%s' invalid %s error action\n", |
| 2084 | buf, is_read ? "read" : "write"); |
| 2085 | return -1; |
| 2086 | } |
| 2087 | } |
| 2088 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2089 | DriveInfo *drive_init(QemuOpts *opts, void *opaque, |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2090 | int *fatal_error) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2091 | { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2092 | const char *buf; |
| 2093 | const char *file = NULL; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 2094 | char devname[128]; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2095 | const char *serial; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 2096 | const char *mediastr = ""; |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2097 | BlockInterfaceType type; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2098 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
| 2099 | int bus_id, unit_id; |
| 2100 | int cyls, heads, secs, translation; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2101 | BlockDriver *drv = NULL; |
aliguori | 4d73cd3 | 2009-02-11 15:20:46 +0000 | [diff] [blame] | 2102 | QEMUMachine *machine = opaque; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2103 | int max_devs; |
| 2104 | int index; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2105 | int cache; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2106 | int aio = 0; |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2107 | int ro = 0; |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2108 | int bdrv_flags; |
| 2109 | int on_read_error, on_write_error; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2110 | const char *devaddr; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2111 | DriveInfo *dinfo; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2112 | int snapshot = 0; |
| 2113 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2114 | *fatal_error = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2115 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2116 | translation = BIOS_ATA_TRANSLATION_AUTO; |
Kevin Wolf | 0aa217e | 2009-06-30 13:06:04 +0200 | [diff] [blame] | 2117 | cache = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2118 | |
Gerd Hoffmann | 4d00781 | 2009-08-31 14:23:57 +0200 | [diff] [blame] | 2119 | if (machine && machine->use_scsi) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2120 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2121 | max_devs = MAX_SCSI_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 2122 | pstrcpy(devname, sizeof(devname), "scsi"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2123 | } else { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2124 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2125 | max_devs = MAX_IDE_DEVS; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 2126 | pstrcpy(devname, sizeof(devname), "ide"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2127 | } |
| 2128 | media = MEDIA_DISK; |
| 2129 | |
| 2130 | /* extract parameters */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2131 | bus_id = qemu_opt_get_number(opts, "bus", 0); |
| 2132 | unit_id = qemu_opt_get_number(opts, "unit", -1); |
| 2133 | index = qemu_opt_get_number(opts, "index", -1); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2134 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2135 | cyls = qemu_opt_get_number(opts, "cyls", 0); |
| 2136 | heads = qemu_opt_get_number(opts, "heads", 0); |
| 2137 | secs = qemu_opt_get_number(opts, "secs", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2138 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2139 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2140 | ro = qemu_opt_get_bool(opts, "readonly", 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2141 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2142 | file = qemu_opt_get(opts, "file"); |
| 2143 | serial = qemu_opt_get(opts, "serial"); |
| 2144 | |
| 2145 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
bellard | ae45d36 | 2008-06-11 09:44:44 +0000 | [diff] [blame] | 2146 | pstrcpy(devname, sizeof(devname), buf); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2147 | if (!strcmp(buf, "ide")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2148 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2149 | max_devs = MAX_IDE_DEVS; |
| 2150 | } else if (!strcmp(buf, "scsi")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2151 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2152 | max_devs = MAX_SCSI_DEVS; |
| 2153 | } else if (!strcmp(buf, "floppy")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2154 | type = IF_FLOPPY; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2155 | max_devs = 0; |
| 2156 | } else if (!strcmp(buf, "pflash")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2157 | type = IF_PFLASH; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2158 | max_devs = 0; |
| 2159 | } else if (!strcmp(buf, "mtd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2160 | type = IF_MTD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2161 | max_devs = 0; |
| 2162 | } else if (!strcmp(buf, "sd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2163 | type = IF_SD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2164 | max_devs = 0; |
aliguori | 6e02c38 | 2008-12-04 19:52:44 +0000 | [diff] [blame] | 2165 | } else if (!strcmp(buf, "virtio")) { |
| 2166 | type = IF_VIRTIO; |
| 2167 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 2168 | } else if (!strcmp(buf, "xen")) { |
| 2169 | type = IF_XEN; |
| 2170 | max_devs = 0; |
Gerd Hoffmann | a8659e9 | 2009-07-31 12:25:39 +0200 | [diff] [blame] | 2171 | } else if (!strcmp(buf, "none")) { |
| 2172 | type = IF_NONE; |
| 2173 | max_devs = 0; |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 2174 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2175 | fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2176 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2177 | } |
| 2178 | } |
| 2179 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2180 | if (cyls || heads || secs) { |
Blue Swirl | 5afe3f0 | 2009-10-17 09:08:47 +0000 | [diff] [blame] | 2181 | if (cyls < 1 || (type == IF_IDE && cyls > 16383)) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2182 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2183 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2184 | } |
Blue Swirl | 5afe3f0 | 2009-10-17 09:08:47 +0000 | [diff] [blame] | 2185 | if (heads < 1 || (type == IF_IDE && heads > 16)) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2186 | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2187 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2188 | } |
Blue Swirl | 5afe3f0 | 2009-10-17 09:08:47 +0000 | [diff] [blame] | 2189 | if (secs < 1 || (type == IF_IDE && secs > 63)) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2190 | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2191 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2192 | } |
| 2193 | } |
| 2194 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2195 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2196 | if (!cyls) { |
| 2197 | fprintf(stderr, |
| 2198 | "qemu: '%s' trans must be used with cyls,heads and secs\n", |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2199 | buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2200 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2201 | } |
| 2202 | if (!strcmp(buf, "none")) |
| 2203 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 2204 | else if (!strcmp(buf, "lba")) |
| 2205 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 2206 | else if (!strcmp(buf, "auto")) |
| 2207 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 2208 | else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2209 | fprintf(stderr, "qemu: '%s' invalid translation type\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2210 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2214 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2215 | if (!strcmp(buf, "disk")) { |
| 2216 | media = MEDIA_DISK; |
| 2217 | } else if (!strcmp(buf, "cdrom")) { |
| 2218 | if (cyls || secs || heads) { |
| 2219 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2220 | "qemu: '%s' invalid physical CHS format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2221 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2222 | } |
| 2223 | media = MEDIA_CDROM; |
| 2224 | } else { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2225 | fprintf(stderr, "qemu: '%s' invalid media\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2226 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2230 | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2231 | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2232 | cache = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2233 | else if (!strcmp(buf, "writethrough")) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2234 | cache = 1; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2235 | else if (!strcmp(buf, "writeback")) |
| 2236 | cache = 2; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2237 | else { |
| 2238 | fprintf(stderr, "qemu: invalid cache option\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2239 | return NULL; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2240 | } |
| 2241 | } |
| 2242 | |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2243 | #ifdef CONFIG_LINUX_AIO |
| 2244 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
| 2245 | if (!strcmp(buf, "threads")) |
| 2246 | aio = 0; |
| 2247 | else if (!strcmp(buf, "native")) |
| 2248 | aio = 1; |
| 2249 | else { |
| 2250 | fprintf(stderr, "qemu: invalid aio option\n"); |
| 2251 | return NULL; |
| 2252 | } |
| 2253 | } |
| 2254 | #endif |
| 2255 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2256 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2257 | if (strcmp(buf, "?") == 0) { |
| 2258 | fprintf(stderr, "qemu: Supported formats:"); |
| 2259 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 2260 | fprintf(stderr, "\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2261 | return NULL; |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 2262 | } |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 2263 | drv = bdrv_find_whitelisted_format(buf); |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2264 | if (!drv) { |
| 2265 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2266 | return NULL; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2270 | on_write_error = BLOCK_ERR_STOP_ENOSPC; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2271 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
aliguori | 869a5c6 | 2009-01-22 19:52:25 +0000 | [diff] [blame] | 2272 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) { |
aliguori | ea8a5d7 | 2009-01-22 19:52:21 +0000 | [diff] [blame] | 2273 | fprintf(stderr, "werror is no supported by this format\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2274 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2275 | } |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2276 | |
| 2277 | on_write_error = parse_block_error_action(buf, 0); |
| 2278 | if (on_write_error < 0) { |
| 2279 | return NULL; |
| 2280 | } |
| 2281 | } |
| 2282 | |
| 2283 | on_read_error = BLOCK_ERR_REPORT; |
| 2284 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
Kevin Wolf | f35d68f | 2009-11-27 13:25:39 +0100 | [diff] [blame] | 2285 | if (type != IF_IDE && type != IF_VIRTIO) { |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2286 | fprintf(stderr, "rerror is no supported by this format\n"); |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | |
| 2290 | on_read_error = parse_block_error_action(buf, 1); |
| 2291 | if (on_read_error < 0) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2292 | return NULL; |
aliguori | 428c570 | 2009-01-21 18:59:04 +0000 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2296 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2297 | if (type != IF_VIRTIO) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2298 | fprintf(stderr, "addr is not supported\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2299 | return NULL; |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2300 | } |
Markus Armbruster | c2cc47a | 2009-06-18 15:14:10 +0200 | [diff] [blame] | 2301 | } |
| 2302 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2303 | /* compute bus and unit according index */ |
| 2304 | |
| 2305 | if (index != -1) { |
| 2306 | if (bus_id != 0 || unit_id != -1) { |
| 2307 | fprintf(stderr, |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2308 | "qemu: index cannot be used with bus and unit\n"); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2309 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2310 | } |
| 2311 | if (max_devs == 0) |
| 2312 | { |
| 2313 | unit_id = index; |
| 2314 | bus_id = 0; |
| 2315 | } else { |
| 2316 | unit_id = index % max_devs; |
| 2317 | bus_id = index / max_devs; |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | /* if user doesn't specify a unit_id, |
| 2322 | * try to find the first free |
| 2323 | */ |
| 2324 | |
| 2325 | if (unit_id == -1) { |
| 2326 | unit_id = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2327 | while (drive_get(type, bus_id, unit_id) != NULL) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2328 | unit_id++; |
| 2329 | if (max_devs && unit_id >= max_devs) { |
| 2330 | unit_id -= max_devs; |
| 2331 | bus_id++; |
| 2332 | } |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | /* check unit id */ |
| 2337 | |
| 2338 | if (max_devs && unit_id >= max_devs) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2339 | fprintf(stderr, "qemu: unit %d too big (max is %d)\n", |
| 2340 | unit_id, max_devs - 1); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2341 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | /* |
| 2345 | * ignore multiple definitions |
| 2346 | */ |
| 2347 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2348 | if (drive_get(type, bus_id, unit_id) != NULL) { |
| 2349 | *fatal_error = 0; |
| 2350 | return NULL; |
| 2351 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2352 | |
| 2353 | /* init */ |
| 2354 | |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2355 | dinfo = qemu_mallocz(sizeof(*dinfo)); |
Gerd Hoffmann | e23d9c4 | 2009-07-31 12:25:34 +0200 | [diff] [blame] | 2356 | if ((buf = qemu_opts_id(opts)) != NULL) { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2357 | dinfo->id = qemu_strdup(buf); |
| 2358 | } else { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2359 | /* no id supplied -> create one */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2360 | dinfo->id = qemu_mallocz(32); |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2361 | if (type == IF_IDE || type == IF_SCSI) |
| 2362 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
| 2363 | if (max_devs) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2364 | snprintf(dinfo->id, 32, "%s%i%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2365 | devname, bus_id, mediastr, unit_id); |
| 2366 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2367 | snprintf(dinfo->id, 32, "%s%s%i", |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2368 | devname, mediastr, unit_id); |
| 2369 | } |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2370 | dinfo->bdrv = bdrv_new(dinfo->id); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2371 | dinfo->devaddr = devaddr; |
| 2372 | dinfo->type = type; |
| 2373 | dinfo->bus = bus_id; |
| 2374 | dinfo->unit = unit_id; |
Kevin Wolf | e9b2e81 | 2009-11-27 13:25:37 +0100 | [diff] [blame] | 2375 | dinfo->on_read_error = on_read_error; |
| 2376 | dinfo->on_write_error = on_write_error; |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2377 | dinfo->opts = opts; |
| 2378 | if (serial) |
| 2379 | strncpy(dinfo->serial, serial, sizeof(serial)); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 2380 | QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2381 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 2382 | switch(type) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2383 | case IF_IDE: |
| 2384 | case IF_SCSI: |
aliguori | 62d23ef | 2009-04-22 15:19:30 +0000 | [diff] [blame] | 2385 | case IF_XEN: |
Gerd Hoffmann | c219331 | 2009-09-15 19:23:28 +0000 | [diff] [blame] | 2386 | case IF_NONE: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2387 | switch(media) { |
| 2388 | case MEDIA_DISK: |
| 2389 | if (cyls != 0) { |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2390 | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
| 2391 | bdrv_set_translation_hint(dinfo->bdrv, translation); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2392 | } |
| 2393 | break; |
| 2394 | case MEDIA_CDROM: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2395 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2396 | break; |
| 2397 | } |
| 2398 | break; |
| 2399 | case IF_SD: |
| 2400 | /* FIXME: This isn't really a floppy, but it's a reasonable |
| 2401 | approximation. */ |
| 2402 | case IF_FLOPPY: |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2403 | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2404 | break; |
| 2405 | case IF_PFLASH: |
| 2406 | case IF_MTD: |
| 2407 | break; |
Gerd Hoffmann | d176c49 | 2009-07-31 12:25:41 +0200 | [diff] [blame] | 2408 | case IF_VIRTIO: |
| 2409 | /* add virtio block device */ |
| 2410 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 2411 | qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
| 2412 | qemu_opt_set(opts, "drive", dinfo->id); |
| 2413 | if (devaddr) |
| 2414 | qemu_opt_set(opts, "addr", devaddr); |
| 2415 | break; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 2416 | case IF_COUNT: |
| 2417 | abort(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2418 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2419 | if (!file) { |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2420 | *fatal_error = 0; |
| 2421 | return NULL; |
| 2422 | } |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2423 | bdrv_flags = 0; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2424 | if (snapshot) { |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 2425 | bdrv_flags |= BDRV_O_SNAPSHOT; |
aliguori | 9f7965c | 2008-10-14 14:42:54 +0000 | [diff] [blame] | 2426 | cache = 2; /* always use write-back with snapshot */ |
| 2427 | } |
| 2428 | if (cache == 0) /* no caching */ |
| 2429 | bdrv_flags |= BDRV_O_NOCACHE; |
| 2430 | else if (cache == 2) /* write-back */ |
| 2431 | bdrv_flags |= BDRV_O_CACHE_WB; |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2432 | |
| 2433 | if (aio == 1) { |
| 2434 | bdrv_flags |= BDRV_O_NATIVE_AIO; |
| 2435 | } else { |
| 2436 | bdrv_flags &= ~BDRV_O_NATIVE_AIO; |
| 2437 | } |
| 2438 | |
Naphtali Sprei | 59f2689 | 2009-10-26 16:25:16 +0200 | [diff] [blame] | 2439 | if (ro == 1) { |
| 2440 | if (type == IF_IDE) { |
| 2441 | fprintf(stderr, "qemu: readonly flag not supported for drive with ide interface\n"); |
| 2442 | return NULL; |
| 2443 | } |
| 2444 | (void)bdrv_set_read_only(dinfo->bdrv, 1); |
| 2445 | } |
| 2446 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2447 | if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 2448 | fprintf(stderr, "qemu: could not open disk image %s: %s\n", |
| 2449 | file, strerror(errno)); |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2450 | return NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2451 | } |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 2452 | |
Gerd Hoffmann | 1dae12e | 2009-07-22 16:42:58 +0200 | [diff] [blame] | 2453 | if (bdrv_key_required(dinfo->bdrv)) |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2454 | autostart = 0; |
Gerd Hoffmann | 751c6a1 | 2009-07-22 16:42:57 +0200 | [diff] [blame] | 2455 | *fatal_error = 0; |
| 2456 | return dinfo; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 2459 | static int drive_init_func(QemuOpts *opts, void *opaque) |
| 2460 | { |
| 2461 | QEMUMachine *machine = opaque; |
| 2462 | int fatal_error = 0; |
| 2463 | |
| 2464 | if (drive_init(opts, machine, &fatal_error) == NULL) { |
| 2465 | if (fatal_error) |
| 2466 | return 1; |
| 2467 | } |
| 2468 | return 0; |
| 2469 | } |
| 2470 | |
| 2471 | static int drive_enable_snapshot(QemuOpts *opts, void *opaque) |
| 2472 | { |
| 2473 | if (NULL == qemu_opt_get(opts, "snapshot")) { |
| 2474 | qemu_opt_set(opts, "snapshot", "on"); |
| 2475 | } |
| 2476 | return 0; |
| 2477 | } |
| 2478 | |
Jan Kiszka | 76e30d0 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2479 | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
| 2480 | { |
| 2481 | boot_set_handler = func; |
| 2482 | boot_set_opaque = opaque; |
| 2483 | } |
| 2484 | |
| 2485 | int qemu_boot_set(const char *boot_devices) |
| 2486 | { |
| 2487 | if (!boot_set_handler) { |
| 2488 | return -EINVAL; |
| 2489 | } |
| 2490 | return boot_set_handler(boot_set_opaque, boot_devices); |
| 2491 | } |
| 2492 | |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2493 | static int parse_bootdevices(char *devices) |
| 2494 | { |
| 2495 | /* We just do some generic consistency checks */ |
| 2496 | const char *p; |
| 2497 | int bitmap = 0; |
| 2498 | |
| 2499 | for (p = devices; *p != '\0'; p++) { |
| 2500 | /* Allowed boot devices are: |
| 2501 | * a-b: floppy disk drives |
| 2502 | * c-f: IDE disk drives |
| 2503 | * g-m: machine implementation dependant drives |
| 2504 | * n-p: network devices |
| 2505 | * It's up to each machine implementation to check if the given boot |
| 2506 | * devices match the actual hardware implementation and firmware |
| 2507 | * features. |
| 2508 | */ |
| 2509 | if (*p < 'a' || *p > 'p') { |
| 2510 | fprintf(stderr, "Invalid boot device '%c'\n", *p); |
| 2511 | exit(1); |
| 2512 | } |
| 2513 | if (bitmap & (1 << (*p - 'a'))) { |
| 2514 | fprintf(stderr, "Boot device '%c' was given twice\n", *p); |
| 2515 | exit(1); |
| 2516 | } |
| 2517 | bitmap |= 1 << (*p - 'a'); |
| 2518 | } |
| 2519 | return bitmap; |
| 2520 | } |
| 2521 | |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 2522 | static void restore_boot_devices(void *opaque) |
| 2523 | { |
| 2524 | char *standard_boot_devices = opaque; |
| 2525 | |
| 2526 | qemu_boot_set(standard_boot_devices); |
| 2527 | |
| 2528 | qemu_unregister_reset(restore_boot_devices, standard_boot_devices); |
| 2529 | qemu_free(standard_boot_devices); |
| 2530 | } |
| 2531 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 2532 | static void numa_add(const char *optarg) |
| 2533 | { |
| 2534 | char option[128]; |
| 2535 | char *endptr; |
| 2536 | unsigned long long value, endvalue; |
| 2537 | int nodenr; |
| 2538 | |
| 2539 | optarg = get_opt_name(option, 128, optarg, ',') + 1; |
| 2540 | if (!strcmp(option, "node")) { |
| 2541 | if (get_param_value(option, 128, "nodeid", optarg) == 0) { |
| 2542 | nodenr = nb_numa_nodes; |
| 2543 | } else { |
| 2544 | nodenr = strtoull(option, NULL, 10); |
| 2545 | } |
| 2546 | |
| 2547 | if (get_param_value(option, 128, "mem", optarg) == 0) { |
| 2548 | node_mem[nodenr] = 0; |
| 2549 | } else { |
| 2550 | value = strtoull(option, &endptr, 0); |
| 2551 | switch (*endptr) { |
| 2552 | case 0: case 'M': case 'm': |
| 2553 | value <<= 20; |
| 2554 | break; |
| 2555 | case 'G': case 'g': |
| 2556 | value <<= 30; |
| 2557 | break; |
| 2558 | } |
| 2559 | node_mem[nodenr] = value; |
| 2560 | } |
| 2561 | if (get_param_value(option, 128, "cpus", optarg) == 0) { |
| 2562 | node_cpumask[nodenr] = 0; |
| 2563 | } else { |
| 2564 | value = strtoull(option, &endptr, 10); |
| 2565 | if (value >= 64) { |
| 2566 | value = 63; |
| 2567 | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n"); |
| 2568 | } else { |
| 2569 | if (*endptr == '-') { |
| 2570 | endvalue = strtoull(endptr+1, &endptr, 10); |
| 2571 | if (endvalue >= 63) { |
| 2572 | endvalue = 62; |
| 2573 | fprintf(stderr, |
| 2574 | "only 63 CPUs in NUMA mode supported.\n"); |
| 2575 | } |
| 2576 | value = (1 << (endvalue + 1)) - (1 << value); |
| 2577 | } else { |
| 2578 | value = 1 << value; |
| 2579 | } |
| 2580 | } |
| 2581 | node_cpumask[nodenr] = value; |
| 2582 | } |
| 2583 | nb_numa_nodes++; |
| 2584 | } |
| 2585 | return; |
| 2586 | } |
| 2587 | |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 2588 | static void smp_parse(const char *optarg) |
| 2589 | { |
| 2590 | int smp, sockets = 0, threads = 0, cores = 0; |
| 2591 | char *endptr; |
| 2592 | char option[128]; |
| 2593 | |
| 2594 | smp = strtoul(optarg, &endptr, 10); |
| 2595 | if (endptr != optarg) { |
| 2596 | if (*endptr == ',') { |
| 2597 | endptr++; |
| 2598 | } |
| 2599 | } |
| 2600 | if (get_param_value(option, 128, "sockets", endptr) != 0) |
| 2601 | sockets = strtoull(option, NULL, 10); |
| 2602 | if (get_param_value(option, 128, "cores", endptr) != 0) |
| 2603 | cores = strtoull(option, NULL, 10); |
| 2604 | if (get_param_value(option, 128, "threads", endptr) != 0) |
| 2605 | threads = strtoull(option, NULL, 10); |
| 2606 | if (get_param_value(option, 128, "maxcpus", endptr) != 0) |
| 2607 | max_cpus = strtoull(option, NULL, 10); |
| 2608 | |
| 2609 | /* compute missing values, prefer sockets over cores over threads */ |
| 2610 | if (smp == 0 || sockets == 0) { |
| 2611 | sockets = sockets > 0 ? sockets : 1; |
| 2612 | cores = cores > 0 ? cores : 1; |
| 2613 | threads = threads > 0 ? threads : 1; |
| 2614 | if (smp == 0) { |
| 2615 | smp = cores * threads * sockets; |
| 2616 | } else { |
| 2617 | sockets = smp / (cores * threads); |
| 2618 | } |
| 2619 | } else { |
| 2620 | if (cores == 0) { |
| 2621 | threads = threads > 0 ? threads : 1; |
| 2622 | cores = smp / (sockets * threads); |
| 2623 | } else { |
| 2624 | if (sockets == 0) { |
| 2625 | sockets = smp / (cores * threads); |
| 2626 | } else { |
| 2627 | threads = smp / (cores * sockets); |
| 2628 | } |
| 2629 | } |
| 2630 | } |
| 2631 | smp_cpus = smp; |
| 2632 | smp_cores = cores > 0 ? cores : 1; |
| 2633 | smp_threads = threads > 0 ? threads : 1; |
| 2634 | if (max_cpus == 0) |
| 2635 | max_cpus = smp_cpus; |
| 2636 | } |
| 2637 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 2638 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2639 | /* USB devices */ |
| 2640 | |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 2641 | static int usb_device_add(const char *devname, int is_hotplug) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2642 | { |
| 2643 | const char *p; |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2644 | USBDevice *dev = NULL; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2645 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2646 | if (!usb_enabled) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2647 | return -1; |
| 2648 | |
Gerd Hoffmann | 0958b4c | 2009-10-26 15:56:45 +0100 | [diff] [blame] | 2649 | /* drivers with .usbdevice_name entry in USBDeviceInfo */ |
| 2650 | dev = usbdevice_create(devname); |
| 2651 | if (dev) |
| 2652 | goto done; |
| 2653 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2654 | /* the other ones */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2655 | if (strstart(devname, "host:", &p)) { |
| 2656 | dev = usb_host_device_open(p); |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2657 | } else if (strstart(devname, "net:", &p)) { |
Mark McLoughlin | 13cf8f2 | 2009-10-06 12:17:14 +0100 | [diff] [blame] | 2658 | QemuOpts *opts; |
| 2659 | int idx; |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2660 | |
Mark McLoughlin | 13cf8f2 | 2009-10-06 12:17:14 +0100 | [diff] [blame] | 2661 | opts = qemu_opts_parse(&qemu_net_opts, p, NULL); |
| 2662 | if (!opts) { |
balrog | 6c9f886 | 2008-07-17 20:47:13 +0000 | [diff] [blame] | 2663 | return -1; |
Mark McLoughlin | 13cf8f2 | 2009-10-06 12:17:14 +0100 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | qemu_opt_set(opts, "type", "nic"); |
| 2667 | qemu_opt_set(opts, "model", "usb"); |
| 2668 | |
Mark McLoughlin | f6b134a | 2009-10-08 19:58:27 +0100 | [diff] [blame] | 2669 | idx = net_client_init(NULL, opts, 0); |
Mark McLoughlin | 13cf8f2 | 2009-10-06 12:17:14 +0100 | [diff] [blame] | 2670 | if (idx == -1) { |
| 2671 | return -1; |
| 2672 | } |
| 2673 | |
| 2674 | dev = usb_net_init(&nd_table[idx]); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 2675 | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
| 2676 | dev = usb_bt_init(devname[2] ? hci_init(p) : |
| 2677 | bt_new_hci(qemu_find_bt_vlan(0))); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2678 | } else { |
| 2679 | return -1; |
| 2680 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 2681 | if (!dev) |
| 2682 | return -1; |
| 2683 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2684 | done: |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2685 | return 0; |
| 2686 | } |
| 2687 | |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2688 | static int usb_device_del(const char *devname) |
| 2689 | { |
| 2690 | int bus_num, addr; |
| 2691 | const char *p; |
| 2692 | |
aliguori | 5d0c575 | 2008-09-14 01:07:41 +0000 | [diff] [blame] | 2693 | if (strstart(devname, "host:", &p)) |
| 2694 | return usb_host_device_close(p); |
| 2695 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2696 | if (!usb_enabled) |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2697 | return -1; |
| 2698 | |
| 2699 | p = strchr(devname, '.'); |
| 2700 | if (!p) |
| 2701 | return -1; |
| 2702 | bus_num = strtoul(devname, NULL, 0); |
| 2703 | addr = strtoul(p + 1, NULL, 0); |
| 2704 | |
Gerd Hoffmann | a5d2f72 | 2009-08-31 14:24:00 +0200 | [diff] [blame] | 2705 | return usb_device_delete_addr(bus_num, addr); |
aliguori | 1f3870a | 2008-08-21 19:27:48 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 2708 | static int usb_parse(const char *cmdline) |
| 2709 | { |
| 2710 | return usb_device_add(cmdline, 0); |
| 2711 | } |
| 2712 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2713 | void do_usb_add(Monitor *mon, const QDict *qdict) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2714 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2715 | usb_device_add(qdict_get_str(qdict, "devname"), 1); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2718 | void do_usb_del(Monitor *mon, const QDict *qdict) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2719 | { |
Luiz Capitulino | d54908a | 2009-08-28 15:27:13 -0300 | [diff] [blame] | 2720 | usb_device_del(qdict_get_str(qdict, "devname")); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 2723 | /***********************************************************/ |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2724 | /* PCMCIA/Cardbus */ |
| 2725 | |
| 2726 | static struct pcmcia_socket_entry_s { |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2727 | PCMCIASocket *socket; |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2728 | struct pcmcia_socket_entry_s *next; |
| 2729 | } *pcmcia_sockets = 0; |
| 2730 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2731 | void pcmcia_socket_register(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2732 | { |
| 2733 | struct pcmcia_socket_entry_s *entry; |
| 2734 | |
| 2735 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
| 2736 | entry->socket = socket; |
| 2737 | entry->next = pcmcia_sockets; |
| 2738 | pcmcia_sockets = entry; |
| 2739 | } |
| 2740 | |
Paul Brook | bc24a22 | 2009-05-10 01:44:56 +0100 | [diff] [blame] | 2741 | void pcmcia_socket_unregister(PCMCIASocket *socket) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2742 | { |
| 2743 | struct pcmcia_socket_entry_s *entry, **ptr; |
| 2744 | |
| 2745 | ptr = &pcmcia_sockets; |
| 2746 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) |
| 2747 | if (entry->socket == socket) { |
| 2748 | *ptr = entry->next; |
| 2749 | qemu_free(entry); |
| 2750 | } |
| 2751 | } |
| 2752 | |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2753 | void pcmcia_info(Monitor *mon) |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2754 | { |
| 2755 | struct pcmcia_socket_entry_s *iter; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2756 | |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2757 | if (!pcmcia_sockets) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2758 | monitor_printf(mon, "No PCMCIA sockets\n"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2759 | |
| 2760 | for (iter = pcmcia_sockets; iter; iter = iter->next) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 2761 | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string, |
| 2762 | iter->socket->attached ? iter->socket->card_string : |
| 2763 | "Empty"); |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | /***********************************************************/ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2767 | /* register display */ |
| 2768 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2769 | struct DisplayAllocator default_allocator = { |
| 2770 | defaultallocator_create_displaysurface, |
| 2771 | defaultallocator_resize_displaysurface, |
| 2772 | defaultallocator_free_displaysurface |
| 2773 | }; |
| 2774 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 2775 | void register_displaystate(DisplayState *ds) |
| 2776 | { |
| 2777 | DisplayState **s; |
| 2778 | s = &display_state; |
| 2779 | while (*s != NULL) |
| 2780 | s = &(*s)->next; |
| 2781 | ds->next = NULL; |
| 2782 | *s = ds; |
| 2783 | } |
| 2784 | |
| 2785 | DisplayState *get_displaystate(void) |
| 2786 | { |
| 2787 | return display_state; |
| 2788 | } |
| 2789 | |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2790 | DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
| 2791 | { |
| 2792 | if(ds->allocator == &default_allocator) ds->allocator = da; |
| 2793 | return ds->allocator; |
| 2794 | } |
| 2795 | |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2796 | /* dumb display */ |
| 2797 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2798 | static void dumb_display_init(void) |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2799 | { |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2800 | DisplayState *ds = qemu_mallocz(sizeof(DisplayState)); |
aliguori | 7b5d76d | 2009-03-13 15:02:13 +0000 | [diff] [blame] | 2801 | ds->allocator = &default_allocator; |
| 2802 | ds->surface = qemu_create_displaysurface(ds, 640, 480); |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 2803 | register_displaystate(ds); |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2807 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 2808 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2809 | typedef struct IOHandlerRecord { |
| 2810 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2811 | IOCanRWHandler *fd_read_poll; |
| 2812 | IOHandler *fd_read; |
| 2813 | IOHandler *fd_write; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2814 | int deleted; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2815 | void *opaque; |
| 2816 | /* temporary data */ |
| 2817 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2818 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2819 | } IOHandlerRecord; |
| 2820 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2821 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 2822 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2823 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 2824 | necessary in the character devices to suppress fd_can_read(). */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2825 | int qemu_set_fd_handler2(int fd, |
| 2826 | IOCanRWHandler *fd_read_poll, |
| 2827 | IOHandler *fd_read, |
| 2828 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2829 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2830 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2831 | IOHandlerRecord **pioh, *ioh; |
| 2832 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2833 | if (!fd_read && !fd_write) { |
| 2834 | pioh = &first_io_handler; |
| 2835 | for(;;) { |
| 2836 | ioh = *pioh; |
| 2837 | if (ioh == NULL) |
| 2838 | break; |
| 2839 | if (ioh->fd == fd) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2840 | ioh->deleted = 1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2841 | break; |
| 2842 | } |
| 2843 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2844 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2845 | } else { |
| 2846 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 2847 | if (ioh->fd == fd) |
| 2848 | goto found; |
| 2849 | } |
| 2850 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2851 | ioh->next = first_io_handler; |
| 2852 | first_io_handler = ioh; |
| 2853 | found: |
| 2854 | ioh->fd = fd; |
| 2855 | ioh->fd_read_poll = fd_read_poll; |
| 2856 | ioh->fd_read = fd_read; |
| 2857 | ioh->fd_write = fd_write; |
| 2858 | ioh->opaque = opaque; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 2859 | ioh->deleted = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2860 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2861 | return 0; |
| 2862 | } |
| 2863 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2864 | int qemu_set_fd_handler(int fd, |
| 2865 | IOHandler *fd_read, |
| 2866 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2867 | void *opaque) |
| 2868 | { |
| 2869 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 2872 | #ifdef _WIN32 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2873 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2874 | /* Polling handling */ |
| 2875 | |
| 2876 | typedef struct PollingEntry { |
| 2877 | PollingFunc *func; |
| 2878 | void *opaque; |
| 2879 | struct PollingEntry *next; |
| 2880 | } PollingEntry; |
| 2881 | |
| 2882 | static PollingEntry *first_polling_entry; |
| 2883 | |
| 2884 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 2885 | { |
| 2886 | PollingEntry **ppe, *pe; |
| 2887 | pe = qemu_mallocz(sizeof(PollingEntry)); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2888 | pe->func = func; |
| 2889 | pe->opaque = opaque; |
| 2890 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 2891 | *ppe = pe; |
| 2892 | return 0; |
| 2893 | } |
| 2894 | |
| 2895 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 2896 | { |
| 2897 | PollingEntry **ppe, *pe; |
| 2898 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 2899 | pe = *ppe; |
| 2900 | if (pe->func == func && pe->opaque == opaque) { |
| 2901 | *ppe = pe->next; |
| 2902 | qemu_free(pe); |
| 2903 | break; |
| 2904 | } |
| 2905 | } |
| 2906 | } |
| 2907 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2908 | /***********************************************************/ |
| 2909 | /* Wait objects support */ |
| 2910 | typedef struct WaitObjects { |
| 2911 | int num; |
| 2912 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2913 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2914 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 2915 | } WaitObjects; |
| 2916 | |
| 2917 | static WaitObjects wait_objects = {0}; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2918 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2919 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2920 | { |
| 2921 | WaitObjects *w = &wait_objects; |
| 2922 | |
| 2923 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 2924 | return -1; |
| 2925 | w->events[w->num] = handle; |
| 2926 | w->func[w->num] = func; |
| 2927 | w->opaque[w->num] = opaque; |
| 2928 | w->num++; |
| 2929 | return 0; |
| 2930 | } |
| 2931 | |
| 2932 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 2933 | { |
| 2934 | int i, found; |
| 2935 | WaitObjects *w = &wait_objects; |
| 2936 | |
| 2937 | found = 0; |
| 2938 | for (i = 0; i < w->num; i++) { |
| 2939 | if (w->events[i] == handle) |
| 2940 | found = 1; |
| 2941 | if (found) { |
| 2942 | w->events[i] = w->events[i + 1]; |
| 2943 | w->func[i] = w->func[i + 1]; |
| 2944 | w->opaque[i] = w->opaque[i + 1]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2945 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 2946 | } |
| 2947 | if (found) |
| 2948 | w->num--; |
| 2949 | } |
| 2950 | #endif |
| 2951 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2952 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 2953 | /* ram save/restore */ |
| 2954 | |
Juan Quintela | 94fb090 | 2009-09-10 03:04:23 +0200 | [diff] [blame] | 2955 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2956 | #define RAM_SAVE_FLAG_COMPRESS 0x02 |
| 2957 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
| 2958 | #define RAM_SAVE_FLAG_PAGE 0x08 |
| 2959 | #define RAM_SAVE_FLAG_EOS 0x10 |
| 2960 | |
| 2961 | static int is_dup_page(uint8_t *page, uint8_t ch) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2962 | { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2963 | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
| 2964 | uint32_t *array = (uint32_t *)page; |
| 2965 | int i; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2966 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2967 | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
| 2968 | if (array[i] != val) |
| 2969 | return 0; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2970 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2971 | |
| 2972 | return 1; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2975 | static int ram_save_block(QEMUFile *f) |
| 2976 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2977 | static ram_addr_t current_addr = 0; |
| 2978 | ram_addr_t saved_addr = current_addr; |
| 2979 | ram_addr_t addr = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2980 | int found = 0; |
| 2981 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 2982 | while (addr < last_ram_offset) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2983 | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2984 | uint8_t *p; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2985 | |
| 2986 | cpu_physical_memory_reset_dirty(current_addr, |
| 2987 | current_addr + TARGET_PAGE_SIZE, |
| 2988 | MIGRATION_DIRTY_FLAG); |
| 2989 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2990 | p = qemu_get_ram_ptr(current_addr); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2991 | |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2992 | if (is_dup_page(p, *p)) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2993 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2994 | qemu_put_byte(f, *p); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2995 | } else { |
| 2996 | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 2997 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | found = 1; |
| 3001 | break; |
| 3002 | } |
| 3003 | addr += TARGET_PAGE_SIZE; |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3004 | current_addr = (saved_addr + addr) % last_ram_offset; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | return found; |
| 3008 | } |
| 3009 | |
Jan Kiszka | 8430793 | 2009-12-02 11:29:38 +0100 | [diff] [blame] | 3010 | static uint64_t bytes_transferred; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3011 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3012 | static ram_addr_t ram_save_remaining(void) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3013 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3014 | ram_addr_t addr; |
| 3015 | ram_addr_t count = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3016 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3017 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3018 | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 3019 | count++; |
| 3020 | } |
| 3021 | |
| 3022 | return count; |
| 3023 | } |
| 3024 | |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3025 | uint64_t ram_bytes_remaining(void) |
| 3026 | { |
| 3027 | return ram_save_remaining() * TARGET_PAGE_SIZE; |
| 3028 | } |
| 3029 | |
| 3030 | uint64_t ram_bytes_transferred(void) |
| 3031 | { |
| 3032 | return bytes_transferred; |
| 3033 | } |
| 3034 | |
| 3035 | uint64_t ram_bytes_total(void) |
| 3036 | { |
| 3037 | return last_ram_offset; |
| 3038 | } |
| 3039 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 3040 | static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3041 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3042 | ram_addr_t addr; |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3043 | uint64_t bytes_transferred_last; |
| 3044 | double bwidth = 0; |
| 3045 | uint64_t expected_time = 0; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3046 | |
Jan Kiszka | 4ec7fcc | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 3047 | if (stage < 0) { |
| 3048 | cpu_physical_memory_set_dirty_tracking(0); |
| 3049 | return 0; |
| 3050 | } |
| 3051 | |
Jan Kiszka | 9fa0638 | 2009-05-22 23:51:45 +0200 | [diff] [blame] | 3052 | if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 3053 | qemu_file_set_error(f); |
| 3054 | return 0; |
| 3055 | } |
| 3056 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3057 | if (stage == 1) { |
Jan Kiszka | 8430793 | 2009-12-02 11:29:38 +0100 | [diff] [blame] | 3058 | bytes_transferred = 0; |
| 3059 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3060 | /* Make sure all dirty bits are set */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3061 | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3062 | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) |
| 3063 | cpu_physical_memory_set_dirty(addr); |
| 3064 | } |
Jan Kiszka | b0a46a3 | 2009-05-02 00:22:51 +0200 | [diff] [blame] | 3065 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3066 | /* Enable dirty memory tracking */ |
| 3067 | cpu_physical_memory_set_dirty_tracking(1); |
| 3068 | |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3069 | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3072 | bytes_transferred_last = bytes_transferred; |
| 3073 | bwidth = get_clock(); |
| 3074 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3075 | while (!qemu_file_rate_limit(f)) { |
| 3076 | int ret; |
| 3077 | |
| 3078 | ret = ram_save_block(f); |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3079 | bytes_transferred += ret * TARGET_PAGE_SIZE; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3080 | if (ret == 0) /* no more blocks */ |
| 3081 | break; |
| 3082 | } |
| 3083 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3084 | bwidth = get_clock() - bwidth; |
| 3085 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; |
| 3086 | |
| 3087 | /* if we haven't transferred anything this round, force expected_time to a |
| 3088 | * a very high value, but without crashing */ |
| 3089 | if (bwidth == 0) |
| 3090 | bwidth = 0.000001; |
| 3091 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3092 | /* try transferring iterative blocks of memory */ |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3093 | if (stage == 3) { |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3094 | /* flush all remaining blocks regardless of rate limiting */ |
Glauber Costa | 9f9e28c | 2009-05-21 17:38:01 -0400 | [diff] [blame] | 3095 | while (ram_save_block(f) != 0) { |
| 3096 | bytes_transferred += TARGET_PAGE_SIZE; |
| 3097 | } |
aliguori | 8215e91 | 2009-04-05 19:30:55 +0000 | [diff] [blame] | 3098 | cpu_physical_memory_set_dirty_tracking(0); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 3102 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 3103 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
| 3104 | |
| 3105 | return (stage == 2) && (expected_time <= migrate_max_downtime()); |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3108 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 3109 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3110 | ram_addr_t addr; |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3111 | int flags; |
| 3112 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3113 | if (version_id != 3) |
| 3114 | return -EINVAL; |
| 3115 | |
| 3116 | do { |
| 3117 | addr = qemu_get_be64(f); |
| 3118 | |
| 3119 | flags = addr & ~TARGET_PAGE_MASK; |
| 3120 | addr &= TARGET_PAGE_MASK; |
| 3121 | |
| 3122 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 3123 | if (addr != last_ram_offset) |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3124 | return -EINVAL; |
| 3125 | } |
| 3126 | |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3127 | if (flags & RAM_SAVE_FLAG_COMPRESS) { |
| 3128 | uint8_t ch = qemu_get_byte(f); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3129 | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
| 3130 | #ifndef _WIN32 |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3131 | if (ch == 0 && |
| 3132 | (!kvm_enabled() || kvm_has_sync_mmu())) { |
| 3133 | madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); |
Anthony Liguori | 779c6be | 2009-06-22 12:39:00 -0500 | [diff] [blame] | 3134 | } |
Anthony Liguori | 3086844 | 2009-06-17 16:46:12 -0500 | [diff] [blame] | 3135 | #endif |
Jan Kiszka | 9a743e5 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 3136 | } else if (flags & RAM_SAVE_FLAG_PAGE) { |
pbrook | 5579c7f | 2009-04-11 14:47:08 +0000 | [diff] [blame] | 3137 | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
Jan Kiszka | 9a743e5 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 3138 | } |
| 3139 | if (qemu_file_has_error(f)) { |
| 3140 | return -EIO; |
| 3141 | } |
aliguori | 475e427 | 2008-10-06 20:21:51 +0000 | [diff] [blame] | 3142 | } while (!(flags & RAM_SAVE_FLAG_EOS)); |
| 3143 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 3144 | return 0; |
| 3145 | } |
| 3146 | |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3147 | void qemu_service_io(void) |
| 3148 | { |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3149 | qemu_notify_event(); |
aliguori | 9e472e1 | 2008-10-08 19:50:24 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3152 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3153 | /* machine registration */ |
| 3154 | |
blueswir1 | bdaf78e | 2008-10-04 07:24:27 +0000 | [diff] [blame] | 3155 | static QEMUMachine *first_machine = NULL; |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 3156 | QEMUMachine *current_machine = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3157 | |
| 3158 | int qemu_register_machine(QEMUMachine *m) |
| 3159 | { |
| 3160 | QEMUMachine **pm; |
| 3161 | pm = &first_machine; |
| 3162 | while (*pm != NULL) |
| 3163 | pm = &(*pm)->next; |
| 3164 | m->next = NULL; |
| 3165 | *pm = m; |
| 3166 | return 0; |
| 3167 | } |
| 3168 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3169 | static QEMUMachine *find_machine(const char *name) |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3170 | { |
| 3171 | QEMUMachine *m; |
| 3172 | |
| 3173 | for(m = first_machine; m != NULL; m = m->next) { |
| 3174 | if (!strcmp(m->name, name)) |
| 3175 | return m; |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 3176 | if (m->alias && !strcmp(m->alias, name)) |
| 3177 | return m; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3178 | } |
| 3179 | return NULL; |
| 3180 | } |
| 3181 | |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 3182 | static QEMUMachine *find_default_machine(void) |
| 3183 | { |
| 3184 | QEMUMachine *m; |
| 3185 | |
| 3186 | for(m = first_machine; m != NULL; m = m->next) { |
| 3187 | if (m->is_default) { |
| 3188 | return m; |
| 3189 | } |
| 3190 | } |
| 3191 | return NULL; |
| 3192 | } |
| 3193 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 3194 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3195 | /* main execution loop */ |
| 3196 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3197 | static void gui_update(void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3198 | { |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3199 | uint64_t interval = GUI_REFRESH_INTERVAL; |
ths | 740733b | 2007-06-08 01:57:56 +0000 | [diff] [blame] | 3200 | DisplayState *ds = opaque; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 3201 | DisplayChangeListener *dcl = ds->listeners; |
| 3202 | |
| 3203 | dpy_refresh(ds); |
| 3204 | |
| 3205 | while (dcl != NULL) { |
| 3206 | if (dcl->gui_timer_interval && |
| 3207 | dcl->gui_timer_interval < interval) |
| 3208 | interval = dcl->gui_timer_interval; |
| 3209 | dcl = dcl->next; |
| 3210 | } |
| 3211 | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 3214 | static void nographic_update(void *opaque) |
| 3215 | { |
| 3216 | uint64_t interval = GUI_REFRESH_INTERVAL; |
| 3217 | |
| 3218 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); |
| 3219 | } |
| 3220 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3221 | struct vm_change_state_entry { |
| 3222 | VMChangeStateHandler *cb; |
| 3223 | void *opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3224 | QLIST_ENTRY (vm_change_state_entry) entries; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3225 | }; |
| 3226 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3227 | 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] | 3228 | |
| 3229 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 3230 | void *opaque) |
| 3231 | { |
| 3232 | VMChangeStateEntry *e; |
| 3233 | |
| 3234 | e = qemu_mallocz(sizeof (*e)); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3235 | |
| 3236 | e->cb = cb; |
| 3237 | e->opaque = opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3238 | QLIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3239 | return e; |
| 3240 | } |
| 3241 | |
| 3242 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 3243 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3244 | QLIST_REMOVE (e, entries); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3245 | qemu_free (e); |
| 3246 | } |
| 3247 | |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3248 | static void vm_state_notify(int running, int reason) |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3249 | { |
| 3250 | VMChangeStateEntry *e; |
| 3251 | |
| 3252 | 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] | 3253 | e->cb(e->opaque, running, reason); |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 3254 | } |
| 3255 | } |
| 3256 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3257 | static void resume_all_vcpus(void); |
| 3258 | static void pause_all_vcpus(void); |
| 3259 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3260 | void vm_start(void) |
| 3261 | { |
| 3262 | if (!vm_running) { |
| 3263 | cpu_enable_ticks(); |
| 3264 | vm_running = 1; |
aliguori | 9781e04 | 2009-01-22 17:15:29 +0000 | [diff] [blame] | 3265 | vm_state_notify(1, 0); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 3266 | qemu_rearm_alarm_timer(alarm_timer); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3267 | resume_all_vcpus(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 3268 | } |
| 3269 | } |
| 3270 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3271 | /* reset/shutdown handler */ |
| 3272 | |
| 3273 | typedef struct QEMUResetEntry { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3274 | QTAILQ_ENTRY(QEMUResetEntry) entry; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3275 | QEMUResetHandler *func; |
| 3276 | void *opaque; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3277 | } QEMUResetEntry; |
| 3278 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3279 | static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers = |
| 3280 | QTAILQ_HEAD_INITIALIZER(reset_handlers); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3281 | static int reset_requested; |
| 3282 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3283 | static int powerdown_requested; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3284 | static int debug_requested; |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3285 | static int vmstop_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3286 | |
aurel32 | cf7a2fe | 2008-03-18 06:53:05 +0000 | [diff] [blame] | 3287 | int qemu_shutdown_requested(void) |
| 3288 | { |
| 3289 | int r = shutdown_requested; |
| 3290 | shutdown_requested = 0; |
| 3291 | return r; |
| 3292 | } |
| 3293 | |
| 3294 | int qemu_reset_requested(void) |
| 3295 | { |
| 3296 | int r = reset_requested; |
| 3297 | reset_requested = 0; |
| 3298 | return r; |
| 3299 | } |
| 3300 | |
| 3301 | int qemu_powerdown_requested(void) |
| 3302 | { |
| 3303 | int r = powerdown_requested; |
| 3304 | powerdown_requested = 0; |
| 3305 | return r; |
| 3306 | } |
| 3307 | |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 3308 | static int qemu_debug_requested(void) |
| 3309 | { |
| 3310 | int r = debug_requested; |
| 3311 | debug_requested = 0; |
| 3312 | return r; |
| 3313 | } |
| 3314 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3315 | static int qemu_vmstop_requested(void) |
| 3316 | { |
| 3317 | int r = vmstop_requested; |
| 3318 | vmstop_requested = 0; |
| 3319 | return r; |
| 3320 | } |
| 3321 | |
| 3322 | static void do_vm_stop(int reason) |
| 3323 | { |
| 3324 | if (vm_running) { |
| 3325 | cpu_disable_ticks(); |
| 3326 | vm_running = 0; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3327 | pause_all_vcpus(); |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3328 | vm_state_notify(0, reason); |
| 3329 | } |
| 3330 | } |
| 3331 | |
Jan Kiszka | a08d436 | 2009-06-27 09:25:07 +0200 | [diff] [blame] | 3332 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3333 | { |
Jan Kiszka | 55ddfe8 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3334 | QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry)); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3335 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3336 | re->func = func; |
| 3337 | re->opaque = opaque; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3338 | QTAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3341 | void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3342 | { |
| 3343 | QEMUResetEntry *re; |
| 3344 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3345 | QTAILQ_FOREACH(re, &reset_handlers, entry) { |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3346 | if (re->func == func && re->opaque == opaque) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3347 | QTAILQ_REMOVE(&reset_handlers, re, entry); |
Jan Kiszka | dda9b29 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 3348 | qemu_free(re); |
| 3349 | return; |
| 3350 | } |
| 3351 | } |
| 3352 | } |
| 3353 | |
| 3354 | void qemu_system_reset(void) |
| 3355 | { |
| 3356 | QEMUResetEntry *re, *nre; |
| 3357 | |
| 3358 | /* reset all devices */ |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 3359 | QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3360 | re->func(re->opaque); |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | void qemu_system_reset_request(void) |
| 3365 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 3366 | if (no_reboot) { |
| 3367 | shutdown_requested = 1; |
| 3368 | } else { |
| 3369 | reset_requested = 1; |
| 3370 | } |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3371 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | void qemu_system_shutdown_request(void) |
| 3375 | { |
| 3376 | shutdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3377 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 3380 | void qemu_system_powerdown_request(void) |
| 3381 | { |
| 3382 | powerdown_requested = 1; |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3383 | qemu_notify_event(); |
| 3384 | } |
| 3385 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3386 | #ifdef CONFIG_IOTHREAD |
| 3387 | static void qemu_system_vmstop_request(int reason) |
aliguori | d9f75a4 | 2009-04-24 18:03:11 +0000 | [diff] [blame] | 3388 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3389 | vmstop_requested = reason; |
| 3390 | qemu_notify_event(); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3391 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3392 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 3393 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3394 | #ifndef _WIN32 |
| 3395 | static int io_thread_fd = -1; |
| 3396 | |
| 3397 | static void qemu_event_increment(void) |
| 3398 | { |
| 3399 | static const char byte = 0; |
| 3400 | |
| 3401 | if (io_thread_fd == -1) |
| 3402 | return; |
| 3403 | |
| 3404 | write(io_thread_fd, &byte, sizeof(byte)); |
| 3405 | } |
| 3406 | |
| 3407 | static void qemu_event_read(void *opaque) |
| 3408 | { |
| 3409 | int fd = (unsigned long)opaque; |
| 3410 | ssize_t len; |
| 3411 | |
| 3412 | /* Drain the notify pipe */ |
| 3413 | do { |
| 3414 | char buffer[512]; |
| 3415 | len = read(fd, buffer, sizeof(buffer)); |
| 3416 | } while ((len == -1 && errno == EINTR) || len > 0); |
| 3417 | } |
| 3418 | |
| 3419 | static int qemu_event_init(void) |
| 3420 | { |
| 3421 | int err; |
| 3422 | int fds[2]; |
| 3423 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 3424 | err = qemu_pipe(fds); |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3425 | if (err == -1) |
| 3426 | return -errno; |
| 3427 | |
| 3428 | err = fcntl_setfl(fds[0], O_NONBLOCK); |
| 3429 | if (err < 0) |
| 3430 | goto fail; |
| 3431 | |
| 3432 | err = fcntl_setfl(fds[1], O_NONBLOCK); |
| 3433 | if (err < 0) |
| 3434 | goto fail; |
| 3435 | |
| 3436 | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
| 3437 | (void *)(unsigned long)fds[0]); |
| 3438 | |
| 3439 | io_thread_fd = fds[1]; |
Jan Kiszka | a7e2121 | 2009-04-29 18:38:28 +0000 | [diff] [blame] | 3440 | return 0; |
| 3441 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3442 | fail: |
| 3443 | close(fds[0]); |
| 3444 | close(fds[1]); |
| 3445 | return err; |
| 3446 | } |
| 3447 | #else |
| 3448 | HANDLE qemu_event_handle; |
| 3449 | |
| 3450 | static void dummy_event_handler(void *opaque) |
| 3451 | { |
| 3452 | } |
| 3453 | |
| 3454 | static int qemu_event_init(void) |
| 3455 | { |
| 3456 | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 3457 | if (!qemu_event_handle) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 3458 | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError()); |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3459 | return -1; |
| 3460 | } |
| 3461 | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL); |
| 3462 | return 0; |
| 3463 | } |
| 3464 | |
| 3465 | static void qemu_event_increment(void) |
| 3466 | { |
malc | de1c90c | 2009-09-27 14:38:18 +0400 | [diff] [blame] | 3467 | if (!SetEvent(qemu_event_handle)) { |
Blue Swirl | 20889d4 | 2009-09-27 20:03:56 +0000 | [diff] [blame] | 3468 | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n", |
malc | de1c90c | 2009-09-27 14:38:18 +0400 | [diff] [blame] | 3469 | GetLastError()); |
| 3470 | exit (1); |
| 3471 | } |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3472 | } |
| 3473 | #endif |
| 3474 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3475 | static int cpu_can_run(CPUState *env) |
| 3476 | { |
| 3477 | if (env->stop) |
| 3478 | return 0; |
| 3479 | if (env->stopped) |
| 3480 | return 0; |
| 3481 | return 1; |
| 3482 | } |
| 3483 | |
| 3484 | #ifndef CONFIG_IOTHREAD |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3485 | static int qemu_init_main_loop(void) |
| 3486 | { |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3487 | return qemu_event_init(); |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3490 | void qemu_init_vcpu(void *_env) |
| 3491 | { |
| 3492 | CPUState *env = _env; |
| 3493 | |
| 3494 | if (kvm_enabled()) |
| 3495 | kvm_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3496 | env->nr_cores = smp_cores; |
| 3497 | env->nr_threads = smp_threads; |
aliguori | 0bf46a4 | 2009-04-24 18:03:41 +0000 | [diff] [blame] | 3498 | return; |
| 3499 | } |
| 3500 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3501 | int qemu_cpu_self(void *env) |
| 3502 | { |
| 3503 | return 1; |
| 3504 | } |
| 3505 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3506 | static void resume_all_vcpus(void) |
| 3507 | { |
| 3508 | } |
| 3509 | |
| 3510 | static void pause_all_vcpus(void) |
| 3511 | { |
| 3512 | } |
| 3513 | |
aliguori | 8edac96 | 2009-04-24 18:03:45 +0000 | [diff] [blame] | 3514 | void qemu_cpu_kick(void *env) |
| 3515 | { |
| 3516 | return; |
| 3517 | } |
| 3518 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3519 | void qemu_notify_event(void) |
| 3520 | { |
| 3521 | CPUState *env = cpu_single_env; |
| 3522 | |
| 3523 | if (env) { |
| 3524 | cpu_exit(env); |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 3525 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
Glauber Costa | d549db5 | 2009-10-07 16:38:03 -0300 | [diff] [blame] | 3528 | void qemu_mutex_lock_iothread(void) {} |
| 3529 | void qemu_mutex_unlock_iothread(void) {} |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3530 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 3531 | void vm_stop(int reason) |
| 3532 | { |
| 3533 | do_vm_stop(reason); |
| 3534 | } |
| 3535 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3536 | #else /* CONFIG_IOTHREAD */ |
| 3537 | |
| 3538 | #include "qemu-thread.h" |
| 3539 | |
| 3540 | QemuMutex qemu_global_mutex; |
| 3541 | static QemuMutex qemu_fair_mutex; |
| 3542 | |
| 3543 | static QemuThread io_thread; |
| 3544 | |
| 3545 | static QemuThread *tcg_cpu_thread; |
| 3546 | static QemuCond *tcg_halt_cond; |
| 3547 | |
| 3548 | static int qemu_system_ready; |
| 3549 | /* cpu creation */ |
| 3550 | static QemuCond qemu_cpu_cond; |
| 3551 | /* system init */ |
| 3552 | static QemuCond qemu_system_cond; |
| 3553 | static QemuCond qemu_pause_cond; |
| 3554 | |
| 3555 | static void block_io_signals(void); |
| 3556 | static void unblock_io_signals(void); |
| 3557 | static int tcg_has_work(void); |
| 3558 | |
| 3559 | static int qemu_init_main_loop(void) |
| 3560 | { |
| 3561 | int ret; |
| 3562 | |
| 3563 | ret = qemu_event_init(); |
| 3564 | if (ret) |
| 3565 | return ret; |
| 3566 | |
| 3567 | qemu_cond_init(&qemu_pause_cond); |
| 3568 | qemu_mutex_init(&qemu_fair_mutex); |
| 3569 | qemu_mutex_init(&qemu_global_mutex); |
| 3570 | qemu_mutex_lock(&qemu_global_mutex); |
| 3571 | |
| 3572 | unblock_io_signals(); |
| 3573 | qemu_thread_self(&io_thread); |
| 3574 | |
| 3575 | return 0; |
| 3576 | } |
| 3577 | |
| 3578 | static void qemu_wait_io_event(CPUState *env) |
| 3579 | { |
| 3580 | while (!tcg_has_work()) |
| 3581 | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000); |
| 3582 | |
| 3583 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3584 | |
| 3585 | /* |
| 3586 | * Users of qemu_global_mutex can be starved, having no chance |
| 3587 | * to acquire it since this path will get to it first. |
| 3588 | * So use another lock to provide fairness. |
| 3589 | */ |
| 3590 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3591 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3592 | |
| 3593 | qemu_mutex_lock(&qemu_global_mutex); |
| 3594 | if (env->stop) { |
| 3595 | env->stop = 0; |
| 3596 | env->stopped = 1; |
| 3597 | qemu_cond_signal(&qemu_pause_cond); |
| 3598 | } |
| 3599 | } |
| 3600 | |
| 3601 | static int qemu_cpu_exec(CPUState *env); |
| 3602 | |
| 3603 | static void *kvm_cpu_thread_fn(void *arg) |
| 3604 | { |
| 3605 | CPUState *env = arg; |
| 3606 | |
| 3607 | block_io_signals(); |
| 3608 | qemu_thread_self(env->thread); |
Jean-Christophe DUBOIS | 321c1cb | 2009-09-02 23:59:04 +0200 | [diff] [blame] | 3609 | if (kvm_enabled()) |
| 3610 | kvm_init_vcpu(env); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3611 | |
| 3612 | /* signal CPU creation */ |
| 3613 | qemu_mutex_lock(&qemu_global_mutex); |
| 3614 | env->created = 1; |
| 3615 | qemu_cond_signal(&qemu_cpu_cond); |
| 3616 | |
| 3617 | /* and wait for machine initialization */ |
| 3618 | while (!qemu_system_ready) |
| 3619 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3620 | |
| 3621 | while (1) { |
| 3622 | if (cpu_can_run(env)) |
| 3623 | qemu_cpu_exec(env); |
Anthony Liguori | 1c3173b | 2009-09-10 08:45:43 -0500 | [diff] [blame] | 3624 | qemu_wait_io_event(env); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | return NULL; |
| 3628 | } |
| 3629 | |
| 3630 | static void tcg_cpu_exec(void); |
| 3631 | |
| 3632 | static void *tcg_cpu_thread_fn(void *arg) |
| 3633 | { |
| 3634 | CPUState *env = arg; |
| 3635 | |
| 3636 | block_io_signals(); |
| 3637 | qemu_thread_self(env->thread); |
| 3638 | |
| 3639 | /* signal CPU creation */ |
| 3640 | qemu_mutex_lock(&qemu_global_mutex); |
| 3641 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 3642 | env->created = 1; |
| 3643 | qemu_cond_signal(&qemu_cpu_cond); |
| 3644 | |
| 3645 | /* and wait for machine initialization */ |
| 3646 | while (!qemu_system_ready) |
| 3647 | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100); |
| 3648 | |
| 3649 | while (1) { |
| 3650 | tcg_cpu_exec(); |
| 3651 | qemu_wait_io_event(cur_cpu); |
| 3652 | } |
| 3653 | |
| 3654 | return NULL; |
| 3655 | } |
| 3656 | |
| 3657 | void qemu_cpu_kick(void *_env) |
| 3658 | { |
| 3659 | CPUState *env = _env; |
| 3660 | qemu_cond_broadcast(env->halt_cond); |
| 3661 | if (kvm_enabled()) |
| 3662 | qemu_thread_signal(env->thread, SIGUSR1); |
| 3663 | } |
| 3664 | |
Glauber Costa | e5bc201 | 2009-09-28 15:27:44 -0300 | [diff] [blame] | 3665 | int qemu_cpu_self(void *_env) |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3666 | { |
Glauber Costa | e5bc201 | 2009-09-28 15:27:44 -0300 | [diff] [blame] | 3667 | CPUState *env = _env; |
| 3668 | QemuThread this; |
| 3669 | |
| 3670 | qemu_thread_self(&this); |
| 3671 | |
| 3672 | return qemu_thread_equal(&this, env->thread); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | static void cpu_signal(int sig) |
| 3676 | { |
| 3677 | if (cpu_single_env) |
| 3678 | cpu_exit(cpu_single_env); |
| 3679 | } |
| 3680 | |
| 3681 | static void block_io_signals(void) |
| 3682 | { |
| 3683 | sigset_t set; |
| 3684 | struct sigaction sigact; |
| 3685 | |
| 3686 | sigemptyset(&set); |
| 3687 | sigaddset(&set, SIGUSR2); |
| 3688 | sigaddset(&set, SIGIO); |
| 3689 | sigaddset(&set, SIGALRM); |
| 3690 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3691 | |
| 3692 | sigemptyset(&set); |
| 3693 | sigaddset(&set, SIGUSR1); |
| 3694 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3695 | |
| 3696 | memset(&sigact, 0, sizeof(sigact)); |
| 3697 | sigact.sa_handler = cpu_signal; |
| 3698 | sigaction(SIGUSR1, &sigact, NULL); |
| 3699 | } |
| 3700 | |
| 3701 | static void unblock_io_signals(void) |
| 3702 | { |
| 3703 | sigset_t set; |
| 3704 | |
| 3705 | sigemptyset(&set); |
| 3706 | sigaddset(&set, SIGUSR2); |
| 3707 | sigaddset(&set, SIGIO); |
| 3708 | sigaddset(&set, SIGALRM); |
| 3709 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
| 3710 | |
| 3711 | sigemptyset(&set); |
| 3712 | sigaddset(&set, SIGUSR1); |
| 3713 | pthread_sigmask(SIG_BLOCK, &set, NULL); |
| 3714 | } |
| 3715 | |
| 3716 | static void qemu_signal_lock(unsigned int msecs) |
| 3717 | { |
| 3718 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3719 | |
| 3720 | while (qemu_mutex_trylock(&qemu_global_mutex)) { |
| 3721 | qemu_thread_signal(tcg_cpu_thread, SIGUSR1); |
| 3722 | if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs)) |
| 3723 | break; |
| 3724 | } |
| 3725 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3726 | } |
| 3727 | |
Glauber Costa | d549db5 | 2009-10-07 16:38:03 -0300 | [diff] [blame] | 3728 | void qemu_mutex_lock_iothread(void) |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3729 | { |
| 3730 | if (kvm_enabled()) { |
| 3731 | qemu_mutex_lock(&qemu_fair_mutex); |
| 3732 | qemu_mutex_lock(&qemu_global_mutex); |
| 3733 | qemu_mutex_unlock(&qemu_fair_mutex); |
| 3734 | } else |
| 3735 | qemu_signal_lock(100); |
| 3736 | } |
| 3737 | |
Glauber Costa | d549db5 | 2009-10-07 16:38:03 -0300 | [diff] [blame] | 3738 | void qemu_mutex_unlock_iothread(void) |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3739 | { |
| 3740 | qemu_mutex_unlock(&qemu_global_mutex); |
| 3741 | } |
| 3742 | |
| 3743 | static int all_vcpus_paused(void) |
| 3744 | { |
| 3745 | CPUState *penv = first_cpu; |
| 3746 | |
| 3747 | while (penv) { |
| 3748 | if (!penv->stopped) |
| 3749 | return 0; |
| 3750 | penv = (CPUState *)penv->next_cpu; |
| 3751 | } |
| 3752 | |
| 3753 | return 1; |
| 3754 | } |
| 3755 | |
| 3756 | static void pause_all_vcpus(void) |
| 3757 | { |
| 3758 | CPUState *penv = first_cpu; |
| 3759 | |
| 3760 | while (penv) { |
| 3761 | penv->stop = 1; |
| 3762 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3763 | qemu_cpu_kick(penv); |
| 3764 | penv = (CPUState *)penv->next_cpu; |
| 3765 | } |
| 3766 | |
| 3767 | while (!all_vcpus_paused()) { |
| 3768 | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100); |
| 3769 | penv = first_cpu; |
| 3770 | while (penv) { |
| 3771 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3772 | penv = (CPUState *)penv->next_cpu; |
| 3773 | } |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | static void resume_all_vcpus(void) |
| 3778 | { |
| 3779 | CPUState *penv = first_cpu; |
| 3780 | |
| 3781 | while (penv) { |
| 3782 | penv->stop = 0; |
| 3783 | penv->stopped = 0; |
| 3784 | qemu_thread_signal(penv->thread, SIGUSR1); |
| 3785 | qemu_cpu_kick(penv); |
| 3786 | penv = (CPUState *)penv->next_cpu; |
| 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | static void tcg_init_vcpu(void *_env) |
| 3791 | { |
| 3792 | CPUState *env = _env; |
| 3793 | /* share a single thread for all cpus with TCG */ |
| 3794 | if (!tcg_cpu_thread) { |
| 3795 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3796 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3797 | qemu_cond_init(env->halt_cond); |
| 3798 | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
| 3799 | while (env->created == 0) |
| 3800 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3801 | tcg_cpu_thread = env->thread; |
| 3802 | tcg_halt_cond = env->halt_cond; |
| 3803 | } else { |
| 3804 | env->thread = tcg_cpu_thread; |
| 3805 | env->halt_cond = tcg_halt_cond; |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | static void kvm_start_vcpu(CPUState *env) |
| 3810 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3811 | env->thread = qemu_mallocz(sizeof(QemuThread)); |
| 3812 | env->halt_cond = qemu_mallocz(sizeof(QemuCond)); |
| 3813 | qemu_cond_init(env->halt_cond); |
| 3814 | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
| 3815 | while (env->created == 0) |
| 3816 | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100); |
| 3817 | } |
| 3818 | |
| 3819 | void qemu_init_vcpu(void *_env) |
| 3820 | { |
| 3821 | CPUState *env = _env; |
| 3822 | |
| 3823 | if (kvm_enabled()) |
| 3824 | kvm_start_vcpu(env); |
| 3825 | else |
| 3826 | tcg_init_vcpu(env); |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 3827 | env->nr_cores = smp_cores; |
| 3828 | env->nr_threads = smp_threads; |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | void qemu_notify_event(void) |
| 3832 | { |
| 3833 | qemu_event_increment(); |
| 3834 | } |
| 3835 | |
| 3836 | void vm_stop(int reason) |
| 3837 | { |
| 3838 | QemuThread me; |
| 3839 | qemu_thread_self(&me); |
| 3840 | |
| 3841 | if (!qemu_thread_equal(&me, &io_thread)) { |
| 3842 | qemu_system_vmstop_request(reason); |
| 3843 | /* |
| 3844 | * FIXME: should not return to device code in case |
| 3845 | * vm_stop() has been requested. |
| 3846 | */ |
| 3847 | if (cpu_single_env) { |
| 3848 | cpu_exit(cpu_single_env); |
| 3849 | cpu_single_env->stop = 1; |
| 3850 | } |
| 3851 | return; |
| 3852 | } |
| 3853 | do_vm_stop(reason); |
| 3854 | } |
| 3855 | |
| 3856 | #endif |
| 3857 | |
| 3858 | |
ths | 877cf88 | 2007-04-18 18:11:47 +0000 | [diff] [blame] | 3859 | #ifdef _WIN32 |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3860 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3861 | { |
| 3862 | int ret, ret2, i; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3863 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3864 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3865 | |
| 3866 | /* XXX: need to suppress polling by better using win32 events */ |
| 3867 | ret = 0; |
| 3868 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 3869 | ret |= pe->func(pe->opaque); |
| 3870 | } |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3871 | if (ret == 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3872 | int err; |
| 3873 | WaitObjects *w = &wait_objects; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3874 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3875 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3876 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 3877 | if (w->func[ret - WAIT_OBJECT_0]) |
| 3878 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3879 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3880 | /* Check for additional signaled events */ |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3881 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3882 | |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3883 | /* Check if event is signaled */ |
| 3884 | ret2 = WaitForSingleObject(w->events[i], 0); |
| 3885 | if(ret2 == WAIT_OBJECT_0) { |
| 3886 | if (w->func[i]) |
| 3887 | w->func[i](w->opaque[i]); |
| 3888 | } else if (ret2 == WAIT_TIMEOUT) { |
| 3889 | } else { |
| 3890 | err = GetLastError(); |
| 3891 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3892 | } |
| 3893 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3894 | } else if (ret == WAIT_TIMEOUT) { |
| 3895 | } else { |
| 3896 | err = GetLastError(); |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 3897 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 3898 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3899 | } |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3900 | |
| 3901 | *timeout = 0; |
| 3902 | } |
| 3903 | #else |
blueswir1 | 69d6451 | 2008-12-07 19:30:18 +0000 | [diff] [blame] | 3904 | static void host_main_loop_wait(int *timeout) |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3905 | { |
| 3906 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3907 | #endif |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3908 | |
| 3909 | void main_loop_wait(int timeout) |
| 3910 | { |
| 3911 | IOHandlerRecord *ioh; |
| 3912 | fd_set rfds, wfds, xfds; |
| 3913 | int ret, nfds; |
| 3914 | struct timeval tv; |
| 3915 | |
| 3916 | qemu_bh_update_timeout(&timeout); |
| 3917 | |
| 3918 | host_main_loop_wait(&timeout); |
| 3919 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3920 | /* poll any events */ |
| 3921 | /* XXX: separate device handlers from system ones */ |
aliguori | 6abfbd7 | 2008-11-05 20:49:37 +0000 | [diff] [blame] | 3922 | nfds = -1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3923 | FD_ZERO(&rfds); |
| 3924 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 3925 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3926 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3927 | if (ioh->deleted) |
| 3928 | continue; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3929 | if (ioh->fd_read && |
| 3930 | (!ioh->fd_read_poll || |
| 3931 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 3932 | FD_SET(ioh->fd, &rfds); |
| 3933 | if (ioh->fd > nfds) |
| 3934 | nfds = ioh->fd; |
| 3935 | } |
| 3936 | if (ioh->fd_write) { |
| 3937 | FD_SET(ioh->fd, &wfds); |
| 3938 | if (ioh->fd > nfds) |
| 3939 | nfds = ioh->fd; |
| 3940 | } |
| 3941 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3942 | |
aliguori | 56f3a5d | 2008-10-31 18:07:17 +0000 | [diff] [blame] | 3943 | tv.tv_sec = timeout / 1000; |
| 3944 | tv.tv_usec = (timeout % 1000) * 1000; |
| 3945 | |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 3946 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 3947 | |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3948 | qemu_mutex_unlock_iothread(); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 3949 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
aliguori | 4870852 | 2009-04-24 18:03:49 +0000 | [diff] [blame] | 3950 | qemu_mutex_lock_iothread(); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3951 | if (ret > 0) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3952 | IOHandlerRecord **pioh; |
| 3953 | |
| 3954 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 3955 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3956 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3957 | } |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 3958 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3959 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 3960 | } |
| 3961 | } |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3962 | |
| 3963 | /* remove deleted IO handlers */ |
| 3964 | pioh = &first_io_handler; |
| 3965 | while (*pioh) { |
| 3966 | ioh = *pioh; |
| 3967 | if (ioh->deleted) { |
| 3968 | *pioh = ioh->next; |
| 3969 | qemu_free(ioh); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3970 | } else |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 3971 | pioh = &ioh->next; |
| 3972 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3973 | } |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 3974 | |
| 3975 | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3976 | |
aliguori | 50317c7 | 2009-04-24 18:03:29 +0000 | [diff] [blame] | 3977 | /* rearm timer, if not periodic */ |
| 3978 | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) { |
| 3979 | alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; |
| 3980 | qemu_rearm_alarm_timer(alarm_timer); |
| 3981 | } |
| 3982 | |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 3983 | /* vm time timers */ |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3984 | if (vm_running) { |
| 3985 | if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER))) |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 3986 | qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL], |
| 3987 | qemu_get_clock(vm_clock)); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 3988 | } |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 3989 | |
| 3990 | /* real time timers */ |
Jan Kiszka | 0fdddf8 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 3991 | qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME], |
aliguori | 357c692 | 2008-11-25 17:26:09 +0000 | [diff] [blame] | 3992 | qemu_get_clock(rt_clock)); |
| 3993 | |
Jan Kiszka | 21d5d12 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 3994 | qemu_run_timers(&active_timers[QEMU_CLOCK_HOST], |
| 3995 | qemu_get_clock(host_clock)); |
| 3996 | |
pbrook | 423f074 | 2007-05-23 00:06:54 +0000 | [diff] [blame] | 3997 | /* Check bottom-halves last in case any of the earlier events triggered |
| 3998 | them. */ |
| 3999 | qemu_bh_poll(); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4000 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4003 | static int qemu_cpu_exec(CPUState *env) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4004 | { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4005 | int ret; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 4006 | #ifdef CONFIG_PROFILER |
| 4007 | int64_t ti; |
| 4008 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4009 | |
| 4010 | #ifdef CONFIG_PROFILER |
| 4011 | ti = profile_getclock(); |
| 4012 | #endif |
| 4013 | if (use_icount) { |
| 4014 | int64_t count; |
| 4015 | int decr; |
| 4016 | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
| 4017 | env->icount_decr.u16.low = 0; |
| 4018 | env->icount_extra = 0; |
| 4019 | count = qemu_next_deadline(); |
| 4020 | count = (count + (1 << icount_time_shift) - 1) |
| 4021 | >> icount_time_shift; |
| 4022 | qemu_icount += count; |
| 4023 | decr = (count > 0xffff) ? 0xffff : count; |
| 4024 | count -= decr; |
| 4025 | env->icount_decr.u16.low = decr; |
| 4026 | env->icount_extra = count; |
| 4027 | } |
| 4028 | ret = cpu_exec(env); |
| 4029 | #ifdef CONFIG_PROFILER |
| 4030 | qemu_time += profile_getclock() - ti; |
| 4031 | #endif |
| 4032 | if (use_icount) { |
| 4033 | /* Fold pending instructions back into the |
| 4034 | instruction counter, and clear the interrupt flag. */ |
| 4035 | qemu_icount -= (env->icount_decr.u16.low |
| 4036 | + env->icount_extra); |
| 4037 | env->icount_decr.u32 = 0; |
| 4038 | env->icount_extra = 0; |
| 4039 | } |
| 4040 | return ret; |
| 4041 | } |
| 4042 | |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4043 | static void tcg_cpu_exec(void) |
| 4044 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4045 | int ret = 0; |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4046 | |
| 4047 | if (next_cpu == NULL) |
| 4048 | next_cpu = first_cpu; |
| 4049 | for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { |
| 4050 | CPUState *env = cur_cpu = next_cpu; |
| 4051 | |
| 4052 | if (!vm_running) |
| 4053 | break; |
| 4054 | if (timer_alarm_pending) { |
| 4055 | timer_alarm_pending = 0; |
| 4056 | break; |
| 4057 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4058 | if (cpu_can_run(env)) |
| 4059 | ret = qemu_cpu_exec(env); |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4060 | if (ret == EXCP_DEBUG) { |
| 4061 | gdb_set_stop_cpu(env); |
| 4062 | debug_requested = 1; |
| 4063 | break; |
| 4064 | } |
| 4065 | } |
| 4066 | } |
| 4067 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4068 | static int cpu_has_work(CPUState *env) |
| 4069 | { |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4070 | if (env->stop) |
| 4071 | return 1; |
| 4072 | if (env->stopped) |
| 4073 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4074 | if (!env->halted) |
| 4075 | return 1; |
| 4076 | if (qemu_cpu_has_work(env)) |
| 4077 | return 1; |
| 4078 | return 0; |
| 4079 | } |
| 4080 | |
| 4081 | static int tcg_has_work(void) |
| 4082 | { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 4083 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4084 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4085 | for (env = first_cpu; env != NULL; env = env->next_cpu) |
| 4086 | if (cpu_has_work(env)) |
| 4087 | return 1; |
| 4088 | return 0; |
| 4089 | } |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4090 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4091 | static int qemu_calculate_timeout(void) |
| 4092 | { |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4093 | #ifndef CONFIG_IOTHREAD |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4094 | int timeout; |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 4095 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4096 | if (!vm_running) |
| 4097 | timeout = 5000; |
| 4098 | else if (tcg_has_work()) |
| 4099 | timeout = 0; |
| 4100 | else if (!use_icount) |
| 4101 | timeout = 5000; |
| 4102 | else { |
| 4103 | /* XXX: use timeout computed from timers */ |
| 4104 | int64_t add; |
| 4105 | int64_t delta; |
| 4106 | /* Advance virtual time to the next event. */ |
| 4107 | if (use_icount == 1) { |
| 4108 | /* When not using an adaptive execution frequency |
| 4109 | we tend to get badly out of sync with real time, |
| 4110 | so just delay for a reasonable amount of time. */ |
| 4111 | delta = 0; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4112 | } else { |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4113 | delta = cpu_get_icount() - cpu_get_clock(); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 4114 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4115 | if (delta > 0) { |
| 4116 | /* If virtual time is ahead of real time then just |
| 4117 | wait for IO. */ |
| 4118 | timeout = (delta / 1000000) + 1; |
| 4119 | } else { |
| 4120 | /* Wait for either IO to occur or the next |
| 4121 | timer event. */ |
| 4122 | add = qemu_next_deadline(); |
| 4123 | /* We advance the timer before checking for IO. |
| 4124 | Limit the amount we advance so that early IO |
| 4125 | activity won't get the guest too far ahead. */ |
| 4126 | if (add > 10000000) |
| 4127 | add = 10000000; |
| 4128 | delta += add; |
| 4129 | add = (add + (1 << icount_time_shift) - 1) |
| 4130 | >> icount_time_shift; |
| 4131 | qemu_icount += add; |
| 4132 | timeout = delta / 1000000; |
| 4133 | if (timeout < 0) |
| 4134 | timeout = 0; |
| 4135 | } |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4136 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4137 | |
| 4138 | return timeout; |
Luiz Capitulino | b319820 | 2009-06-09 18:24:57 -0300 | [diff] [blame] | 4139 | #else /* CONFIG_IOTHREAD */ |
| 4140 | return 1000; |
| 4141 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | static int vm_can_run(void) |
| 4145 | { |
| 4146 | if (powerdown_requested) |
| 4147 | return 0; |
| 4148 | if (reset_requested) |
| 4149 | return 0; |
| 4150 | if (shutdown_requested) |
| 4151 | return 0; |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4152 | if (debug_requested) |
| 4153 | return 0; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4154 | return 1; |
| 4155 | } |
| 4156 | |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4157 | qemu_irq qemu_system_powerdown; |
| 4158 | |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4159 | static void main_loop(void) |
| 4160 | { |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4161 | int r; |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4162 | |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4163 | #ifdef CONFIG_IOTHREAD |
| 4164 | qemu_system_ready = 1; |
| 4165 | qemu_cond_broadcast(&qemu_system_cond); |
| 4166 | #endif |
| 4167 | |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4168 | for (;;) { |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4169 | do { |
| 4170 | #ifdef CONFIG_PROFILER |
| 4171 | int64_t ti; |
| 4172 | #endif |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4173 | #ifndef CONFIG_IOTHREAD |
aliguori | e6e35b1 | 2009-04-24 18:03:57 +0000 | [diff] [blame] | 4174 | tcg_cpu_exec(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4175 | #endif |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4176 | #ifdef CONFIG_PROFILER |
| 4177 | ti = profile_getclock(); |
| 4178 | #endif |
| 4179 | main_loop_wait(qemu_calculate_timeout()); |
| 4180 | #ifdef CONFIG_PROFILER |
| 4181 | dev_time += profile_getclock() - ti; |
| 4182 | #endif |
aliguori | e568902 | 2009-04-24 18:03:54 +0000 | [diff] [blame] | 4183 | } while (vm_can_run()); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4184 | |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4185 | if (qemu_debug_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4186 | monitor_protocol_event(QEVENT_DEBUG, NULL); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4187 | vm_stop(EXCP_DEBUG); |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4188 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4189 | if (qemu_shutdown_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4190 | monitor_protocol_event(QEVENT_SHUTDOWN, NULL); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4191 | if (no_shutdown) { |
| 4192 | vm_stop(0); |
| 4193 | no_shutdown = 0; |
| 4194 | } else |
| 4195 | break; |
| 4196 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4197 | if (qemu_reset_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4198 | monitor_protocol_event(QEVENT_RESET, NULL); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4199 | pause_all_vcpus(); |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4200 | qemu_system_reset(); |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4201 | resume_all_vcpus(); |
| 4202 | } |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4203 | if (qemu_powerdown_requested()) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4204 | monitor_protocol_event(QEVENT_POWERDOWN, NULL); |
Blue Swirl | d9c3231 | 2009-08-09 08:42:19 +0000 | [diff] [blame] | 4205 | qemu_irq_raise(qemu_system_powerdown); |
| 4206 | } |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4207 | if ((r = qemu_vmstop_requested())) { |
Blue Swirl | 242cd00 | 2009-12-04 18:05:45 +0000 | [diff] [blame] | 4208 | monitor_protocol_event(QEVENT_STOP, NULL); |
aliguori | 6e29f5d | 2009-04-24 18:04:02 +0000 | [diff] [blame] | 4209 | vm_stop(r); |
Luiz Capitulino | b1a15e7 | 2009-11-26 22:59:04 -0200 | [diff] [blame] | 4210 | } |
aliguori | 43b9685 | 2009-04-24 18:03:33 +0000 | [diff] [blame] | 4211 | } |
aliguori | d6dc3d4 | 2009-04-24 18:04:07 +0000 | [diff] [blame] | 4212 | pause_all_vcpus(); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4215 | static void version(void) |
| 4216 | { |
pbrook | 4a19f1e | 2009-04-07 23:17:49 +0000 | [diff] [blame] | 4217 | 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] | 4218 | } |
| 4219 | |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4220 | static void help(int exitcode) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4221 | { |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 4222 | version(); |
| 4223 | printf("usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4224 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4225 | "'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] | 4226 | "\n" |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4227 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4228 | opt_help |
| 4229 | #define DEFHEADING(text) stringify(text) "\n" |
| 4230 | #include "qemu-options.h" |
| 4231 | #undef DEF |
| 4232 | #undef DEFHEADING |
| 4233 | #undef GEN_DOCS |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4234 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4235 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 4236 | "ctrl-alt-f toggle full screen\n" |
| 4237 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 4238 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 4239 | "\n" |
| 4240 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 4241 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 4242 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4243 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4244 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 4245 | DEFAULT_NETWORK_SCRIPT, |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4246 | DEFAULT_NETWORK_DOWN_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4247 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 4248 | DEFAULT_GDBSTUB_PORT, |
bellard | bce6184 | 2008-02-01 22:18:51 +0000 | [diff] [blame] | 4249 | "/tmp/qemu.log"); |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4250 | exit(exitcode); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4253 | #define HAS_ARG 0x0001 |
| 4254 | |
| 4255 | enum { |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4256 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4257 | opt_enum, |
| 4258 | #define DEFHEADING(text) |
| 4259 | #include "qemu-options.h" |
| 4260 | #undef DEF |
| 4261 | #undef DEFHEADING |
| 4262 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4263 | }; |
| 4264 | |
| 4265 | typedef struct QEMUOption { |
| 4266 | const char *name; |
| 4267 | int flags; |
| 4268 | int index; |
| 4269 | } QEMUOption; |
| 4270 | |
blueswir1 | dbed7e4 | 2008-10-01 19:38:09 +0000 | [diff] [blame] | 4271 | static const QEMUOption qemu_options[] = { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4272 | { "h", 0, QEMU_OPTION_h }, |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 4273 | #define DEF(option, opt_arg, opt_enum, opt_help) \ |
| 4274 | { option, opt_arg, opt_enum }, |
| 4275 | #define DEFHEADING(text) |
| 4276 | #include "qemu-options.h" |
| 4277 | #undef DEF |
| 4278 | #undef DEFHEADING |
| 4279 | #undef GEN_DOCS |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4280 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4281 | }; |
| 4282 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4283 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4284 | struct soundhw soundhw[] = { |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4285 | #ifdef HAS_AUDIO_CHOICE |
aurel32 | 4ce7ff6 | 2008-04-07 19:47:14 +0000 | [diff] [blame] | 4286 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 4287 | { |
| 4288 | "pcspk", |
| 4289 | "PC speaker", |
| 4290 | 0, |
| 4291 | 1, |
| 4292 | { .init_isa = pcspk_audio_init } |
| 4293 | }, |
| 4294 | #endif |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4295 | |
| 4296 | #ifdef CONFIG_SB16 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4297 | { |
| 4298 | "sb16", |
| 4299 | "Creative Sound Blaster 16", |
| 4300 | 0, |
| 4301 | 1, |
| 4302 | { .init_isa = SB16_init } |
| 4303 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4304 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4305 | |
malc | cc53d26 | 2008-06-13 10:48:22 +0000 | [diff] [blame] | 4306 | #ifdef CONFIG_CS4231A |
| 4307 | { |
| 4308 | "cs4231a", |
| 4309 | "CS4231A", |
| 4310 | 0, |
| 4311 | 1, |
| 4312 | { .init_isa = cs4231a_init } |
| 4313 | }, |
| 4314 | #endif |
| 4315 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4316 | #ifdef CONFIG_ADLIB |
| 4317 | { |
| 4318 | "adlib", |
| 4319 | #ifdef HAS_YMF262 |
| 4320 | "Yamaha YMF262 (OPL3)", |
| 4321 | #else |
| 4322 | "Yamaha YM3812 (OPL2)", |
| 4323 | #endif |
| 4324 | 0, |
| 4325 | 1, |
| 4326 | { .init_isa = Adlib_init } |
| 4327 | }, |
| 4328 | #endif |
| 4329 | |
| 4330 | #ifdef CONFIG_GUS |
| 4331 | { |
| 4332 | "gus", |
| 4333 | "Gravis Ultrasound GF1", |
| 4334 | 0, |
| 4335 | 1, |
| 4336 | { .init_isa = GUS_init } |
| 4337 | }, |
| 4338 | #endif |
| 4339 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4340 | #ifdef CONFIG_AC97 |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4341 | { |
| 4342 | "ac97", |
| 4343 | "Intel 82801AA AC97 Audio", |
| 4344 | 0, |
| 4345 | 0, |
| 4346 | { .init_pci = ac97_init } |
| 4347 | }, |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4348 | #endif |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 4349 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4350 | #ifdef CONFIG_ES1370 |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4351 | { |
| 4352 | "es1370", |
| 4353 | "ENSONIQ AudioPCI ES1370", |
| 4354 | 0, |
| 4355 | 0, |
| 4356 | { .init_pci = es1370_init } |
| 4357 | }, |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 4358 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4359 | |
malc | 4c9b53e | 2009-01-09 10:46:34 +0000 | [diff] [blame] | 4360 | #endif /* HAS_AUDIO_CHOICE */ |
| 4361 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4362 | { NULL, NULL, 0, 0, { NULL } } |
| 4363 | }; |
| 4364 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4365 | static void select_soundhw (const char *optarg) |
| 4366 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4367 | struct soundhw *c; |
| 4368 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4369 | if (*optarg == '?') { |
| 4370 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4371 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4372 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4373 | for (c = soundhw; c->name; ++c) { |
| 4374 | printf ("%-11s %s\n", c->name, c->descr); |
| 4375 | } |
| 4376 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4377 | exit (*optarg != '?'); |
| 4378 | } |
| 4379 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4380 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4381 | const char *p; |
| 4382 | char *e; |
| 4383 | int bad_card = 0; |
| 4384 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4385 | if (!strcmp (optarg, "all")) { |
| 4386 | for (c = soundhw; c->name; ++c) { |
| 4387 | c->enabled = 1; |
| 4388 | } |
| 4389 | return; |
| 4390 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4391 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4392 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4393 | while (*p) { |
| 4394 | e = strchr (p, ','); |
| 4395 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4396 | |
| 4397 | for (c = soundhw; c->name; ++c) { |
malc | b3d6fb4 | 2009-09-06 06:49:03 +0400 | [diff] [blame] | 4398 | if (!strncmp (c->name, p, l) && !c->name[l]) { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4399 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4400 | break; |
| 4401 | } |
| 4402 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 4403 | |
| 4404 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 4405 | if (l > 80) { |
| 4406 | fprintf (stderr, |
| 4407 | "Unknown sound card name (too big to show)\n"); |
| 4408 | } |
| 4409 | else { |
| 4410 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 4411 | (int) l, p); |
| 4412 | } |
| 4413 | bad_card = 1; |
| 4414 | } |
| 4415 | p += l + (e != NULL); |
| 4416 | } |
| 4417 | |
| 4418 | if (bad_card) |
| 4419 | goto show_valid_cards; |
| 4420 | } |
| 4421 | } |
| 4422 | #endif |
| 4423 | |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4424 | static void select_vgahw (const char *p) |
| 4425 | { |
| 4426 | const char *opts; |
| 4427 | |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 4428 | default_vga = 0; |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4429 | vga_interface_type = VGA_NONE; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4430 | if (strstart(p, "std", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4431 | vga_interface_type = VGA_STD; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4432 | } else if (strstart(p, "cirrus", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4433 | vga_interface_type = VGA_CIRRUS; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4434 | } else if (strstart(p, "vmware", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4435 | vga_interface_type = VGA_VMWARE; |
aliguori | 94909d9 | 2009-04-22 15:19:53 +0000 | [diff] [blame] | 4436 | } else if (strstart(p, "xenfb", &opts)) { |
Zachary Amsden | 8617675 | 2009-07-30 00:15:02 -1000 | [diff] [blame] | 4437 | vga_interface_type = VGA_XENFB; |
aliguori | 28b85ed | 2009-04-22 15:19:48 +0000 | [diff] [blame] | 4438 | } else if (!strstart(p, "none", &opts)) { |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4439 | invalid_vga: |
| 4440 | fprintf(stderr, "Unknown vga type: %s\n", p); |
| 4441 | exit(1); |
| 4442 | } |
malc | cb5a7aa | 2008-09-28 00:42:12 +0000 | [diff] [blame] | 4443 | while (*opts) { |
| 4444 | const char *nextopt; |
| 4445 | |
| 4446 | if (strstart(opts, ",retrace=", &nextopt)) { |
| 4447 | opts = nextopt; |
| 4448 | if (strstart(opts, "dumb", &nextopt)) |
| 4449 | vga_retrace_method = VGA_RETRACE_DUMB; |
| 4450 | else if (strstart(opts, "precise", &nextopt)) |
| 4451 | vga_retrace_method = VGA_RETRACE_PRECISE; |
| 4452 | else goto invalid_vga; |
| 4453 | } else goto invalid_vga; |
| 4454 | opts = nextopt; |
| 4455 | } |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4458 | #ifdef TARGET_I386 |
| 4459 | static int balloon_parse(const char *arg) |
| 4460 | { |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4461 | QemuOpts *opts; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4462 | |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4463 | if (strcmp(arg, "none") == 0) { |
| 4464 | return 0; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4465 | } |
Gerd Hoffmann | 382f074 | 2009-08-14 10:34:22 +0200 | [diff] [blame] | 4466 | |
| 4467 | if (!strncmp(arg, "virtio", 6)) { |
| 4468 | if (arg[6] == ',') { |
| 4469 | /* have params -> parse them */ |
| 4470 | opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL); |
| 4471 | if (!opts) |
| 4472 | return -1; |
| 4473 | } else { |
| 4474 | /* create empty opts */ |
| 4475 | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
| 4476 | } |
| 4477 | qemu_opt_set(opts, "driver", "virtio-balloon-pci"); |
| 4478 | return 0; |
| 4479 | } |
| 4480 | |
| 4481 | return -1; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 4482 | } |
| 4483 | #endif |
| 4484 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4485 | #ifdef _WIN32 |
| 4486 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 4487 | { |
| 4488 | exit(STATUS_CONTROL_C_EXIT); |
| 4489 | return TRUE; |
| 4490 | } |
| 4491 | #endif |
| 4492 | |
aliguori | c4be29f | 2009-04-17 18:58:14 +0000 | [diff] [blame] | 4493 | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4494 | { |
| 4495 | int ret; |
| 4496 | |
| 4497 | if(strlen(str) != 36) |
| 4498 | return -1; |
| 4499 | |
| 4500 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], |
| 4501 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], |
| 4502 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); |
| 4503 | |
| 4504 | if(ret != 16) |
| 4505 | return -1; |
| 4506 | |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 4507 | #ifdef TARGET_I386 |
| 4508 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); |
| 4509 | #endif |
| 4510 | |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 4511 | return 0; |
| 4512 | } |
| 4513 | |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4514 | #ifndef _WIN32 |
| 4515 | |
| 4516 | static void termsig_handler(int signal) |
| 4517 | { |
| 4518 | qemu_system_shutdown_request(); |
| 4519 | } |
| 4520 | |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4521 | static void sigchld_handler(int signal) |
| 4522 | { |
| 4523 | waitpid(-1, NULL, WNOHANG); |
| 4524 | } |
| 4525 | |
| 4526 | static void sighandler_setup(void) |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4527 | { |
| 4528 | struct sigaction act; |
| 4529 | |
| 4530 | memset(&act, 0, sizeof(act)); |
| 4531 | act.sa_handler = termsig_handler; |
| 4532 | sigaction(SIGINT, &act, NULL); |
| 4533 | sigaction(SIGHUP, &act, NULL); |
| 4534 | sigaction(SIGTERM, &act, NULL); |
Jan Kiszka | 7c3370d | 2009-05-08 12:34:17 +0200 | [diff] [blame] | 4535 | |
| 4536 | act.sa_handler = sigchld_handler; |
| 4537 | act.sa_flags = SA_NOCLDSTOP; |
| 4538 | sigaction(SIGCHLD, &act, NULL); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
| 4541 | #endif |
| 4542 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4543 | #ifdef _WIN32 |
| 4544 | /* Look for support files in the same directory as the executable. */ |
| 4545 | static char *find_datadir(const char *argv0) |
| 4546 | { |
| 4547 | char *p; |
| 4548 | char buf[MAX_PATH]; |
| 4549 | DWORD len; |
| 4550 | |
| 4551 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
| 4552 | if (len == 0) { |
Blue Swirl | c594780 | 2009-06-09 20:51:21 +0300 | [diff] [blame] | 4553 | return NULL; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4554 | } |
| 4555 | |
| 4556 | buf[len] = 0; |
| 4557 | p = buf + len - 1; |
| 4558 | while (p != buf && *p != '\\') |
| 4559 | p--; |
| 4560 | *p = 0; |
| 4561 | if (access(buf, R_OK) == 0) { |
| 4562 | return qemu_strdup(buf); |
| 4563 | } |
| 4564 | return NULL; |
| 4565 | } |
| 4566 | #else /* !_WIN32 */ |
| 4567 | |
| 4568 | /* Find a likely location for support files using the location of the binary. |
| 4569 | For installed binaries this will be "$bindir/../share/qemu". When |
| 4570 | running from the build tree this will be "$bindir/../pc-bios". */ |
| 4571 | #define SHARE_SUFFIX "/share/qemu" |
| 4572 | #define BUILD_SUFFIX "/pc-bios" |
| 4573 | static char *find_datadir(const char *argv0) |
| 4574 | { |
| 4575 | char *dir; |
| 4576 | char *p = NULL; |
| 4577 | char *res; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4578 | char buf[PATH_MAX]; |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4579 | size_t max_len; |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4580 | |
| 4581 | #if defined(__linux__) |
| 4582 | { |
| 4583 | int len; |
| 4584 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| 4585 | if (len > 0) { |
| 4586 | buf[len] = 0; |
| 4587 | p = buf; |
| 4588 | } |
| 4589 | } |
| 4590 | #elif defined(__FreeBSD__) |
| 4591 | { |
| 4592 | int len; |
| 4593 | len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); |
| 4594 | if (len > 0) { |
| 4595 | buf[len] = 0; |
| 4596 | p = buf; |
| 4597 | } |
| 4598 | } |
| 4599 | #endif |
| 4600 | /* If we don't have any way of figuring out the actual executable |
| 4601 | location then try argv[0]. */ |
| 4602 | if (!p) { |
Jean-Christophe DUBOIS | 4d22419 | 2009-09-02 23:59:02 +0200 | [diff] [blame] | 4603 | p = realpath(argv0, buf); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4604 | if (!p) { |
| 4605 | return NULL; |
| 4606 | } |
| 4607 | } |
| 4608 | dir = dirname(p); |
| 4609 | dir = dirname(dir); |
| 4610 | |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4611 | max_len = strlen(dir) + |
| 4612 | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; |
| 4613 | res = qemu_mallocz(max_len); |
| 4614 | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4615 | if (access(res, R_OK)) { |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4616 | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4617 | if (access(res, R_OK)) { |
| 4618 | qemu_free(res); |
| 4619 | res = NULL; |
| 4620 | } |
| 4621 | } |
Jean-Christophe DUBOIS | 4d22419 | 2009-09-02 23:59:02 +0200 | [diff] [blame] | 4622 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4623 | return res; |
| 4624 | } |
| 4625 | #undef SHARE_SUFFIX |
| 4626 | #undef BUILD_SUFFIX |
| 4627 | #endif |
| 4628 | |
| 4629 | char *qemu_find_file(int type, const char *name) |
| 4630 | { |
| 4631 | int len; |
| 4632 | const char *subdir; |
| 4633 | char *buf; |
| 4634 | |
| 4635 | /* If name contains path separators then try it as a straight path. */ |
| 4636 | if ((strchr(name, '/') || strchr(name, '\\')) |
| 4637 | && access(name, R_OK) == 0) { |
Jean-Christophe DUBOIS | 73ffc80 | 2009-09-02 23:59:06 +0200 | [diff] [blame] | 4638 | return qemu_strdup(name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4639 | } |
| 4640 | switch (type) { |
| 4641 | case QEMU_FILE_TYPE_BIOS: |
| 4642 | subdir = ""; |
| 4643 | break; |
| 4644 | case QEMU_FILE_TYPE_KEYMAP: |
| 4645 | subdir = "keymaps/"; |
| 4646 | break; |
| 4647 | default: |
| 4648 | abort(); |
| 4649 | } |
| 4650 | len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2; |
| 4651 | buf = qemu_mallocz(len); |
Blue Swirl | 3a41759 | 2009-06-09 19:12:21 +0000 | [diff] [blame] | 4652 | snprintf(buf, len, "%s/%s%s", data_dir, subdir, name); |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 4653 | if (access(buf, R_OK)) { |
| 4654 | qemu_free(buf); |
| 4655 | return NULL; |
| 4656 | } |
| 4657 | return buf; |
| 4658 | } |
| 4659 | |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4660 | static int device_init_func(QemuOpts *opts, void *opaque) |
| 4661 | { |
| 4662 | DeviceState *dev; |
| 4663 | |
| 4664 | dev = qdev_device_add(opts); |
| 4665 | if (!dev) |
| 4666 | return -1; |
| 4667 | return 0; |
| 4668 | } |
| 4669 | |
Gerd Hoffmann | 1a688d3 | 2009-12-08 13:11:36 +0100 | [diff] [blame] | 4670 | static int chardev_init_func(QemuOpts *opts, void *opaque) |
| 4671 | { |
| 4672 | CharDriverState *chr; |
| 4673 | |
| 4674 | chr = qemu_chr_open_opts(opts, NULL); |
| 4675 | if (!chr) |
| 4676 | return -1; |
| 4677 | return 0; |
| 4678 | } |
| 4679 | |
Gerd Hoffmann | 8858934 | 2009-12-08 13:11:50 +0100 | [diff] [blame] | 4680 | static int mon_init_func(QemuOpts *opts, void *opaque) |
| 4681 | { |
| 4682 | CharDriverState *chr; |
| 4683 | const char *chardev; |
| 4684 | const char *mode; |
| 4685 | int flags; |
| 4686 | |
| 4687 | mode = qemu_opt_get(opts, "mode"); |
| 4688 | if (mode == NULL) { |
| 4689 | mode = "readline"; |
| 4690 | } |
| 4691 | if (strcmp(mode, "readline") == 0) { |
| 4692 | flags = MONITOR_USE_READLINE; |
| 4693 | } else if (strcmp(mode, "control") == 0) { |
| 4694 | flags = MONITOR_USE_CONTROL; |
| 4695 | } else { |
| 4696 | fprintf(stderr, "unknown monitor mode \"%s\"\n", mode); |
| 4697 | exit(1); |
| 4698 | } |
| 4699 | |
| 4700 | if (qemu_opt_get_bool(opts, "default", 0)) |
| 4701 | flags |= MONITOR_IS_DEFAULT; |
| 4702 | |
| 4703 | chardev = qemu_opt_get(opts, "chardev"); |
| 4704 | chr = qemu_chr_find(chardev); |
| 4705 | if (chr == NULL) { |
| 4706 | fprintf(stderr, "chardev \"%s\" not found\n", chardev); |
| 4707 | exit(1); |
| 4708 | } |
| 4709 | |
| 4710 | monitor_init(chr, flags); |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 4714 | static void monitor_parse(const char *optarg, const char *mode) |
Gerd Hoffmann | 8858934 | 2009-12-08 13:11:50 +0100 | [diff] [blame] | 4715 | { |
| 4716 | static int monitor_device_index = 0; |
| 4717 | QemuOpts *opts; |
| 4718 | const char *p; |
| 4719 | char label[32]; |
| 4720 | int def = 0; |
| 4721 | |
| 4722 | if (strstart(optarg, "chardev:", &p)) { |
| 4723 | snprintf(label, sizeof(label), "%s", p); |
| 4724 | } else { |
| 4725 | if (monitor_device_index) { |
| 4726 | snprintf(label, sizeof(label), "monitor%d", |
| 4727 | monitor_device_index); |
| 4728 | } else { |
| 4729 | snprintf(label, sizeof(label), "monitor"); |
| 4730 | def = 1; |
| 4731 | } |
| 4732 | opts = qemu_chr_parse_compat(label, optarg); |
| 4733 | if (!opts) { |
| 4734 | fprintf(stderr, "parse error: %s\n", optarg); |
| 4735 | exit(1); |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | opts = qemu_opts_create(&qemu_mon_opts, label, 1); |
| 4740 | if (!opts) { |
| 4741 | fprintf(stderr, "duplicate chardev: %s\n", label); |
| 4742 | exit(1); |
| 4743 | } |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 4744 | qemu_opt_set(opts, "mode", mode); |
Gerd Hoffmann | 8858934 | 2009-12-08 13:11:50 +0100 | [diff] [blame] | 4745 | qemu_opt_set(opts, "chardev", label); |
| 4746 | if (def) |
| 4747 | qemu_opt_set(opts, "default", "on"); |
| 4748 | monitor_device_index++; |
| 4749 | } |
| 4750 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4751 | struct device_config { |
| 4752 | enum { |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 4753 | DEV_USB, /* -usbdevice */ |
| 4754 | DEV_BT, /* -bt */ |
| 4755 | DEV_SERIAL, /* -serial */ |
| 4756 | DEV_PARALLEL, /* -parallel */ |
| 4757 | DEV_VIRTCON, /* -virtioconsole */ |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4758 | } type; |
| 4759 | const char *cmdline; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4760 | QTAILQ_ENTRY(device_config) next; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4761 | }; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4762 | QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs); |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4763 | |
| 4764 | static void add_device_config(int type, const char *cmdline) |
| 4765 | { |
| 4766 | struct device_config *conf; |
| 4767 | |
| 4768 | conf = qemu_mallocz(sizeof(*conf)); |
| 4769 | conf->type = type; |
| 4770 | conf->cmdline = cmdline; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4771 | QTAILQ_INSERT_TAIL(&device_configs, conf, next); |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | static int foreach_device_config(int type, int (*func)(const char *cmdline)) |
| 4775 | { |
| 4776 | struct device_config *conf; |
| 4777 | int rc; |
| 4778 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4779 | QTAILQ_FOREACH(conf, &device_configs, next) { |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 4780 | if (conf->type != type) |
| 4781 | continue; |
| 4782 | rc = func(conf->cmdline); |
| 4783 | if (0 != rc) |
| 4784 | return rc; |
| 4785 | } |
| 4786 | return 0; |
| 4787 | } |
| 4788 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 4789 | static int serial_parse(const char *devname) |
| 4790 | { |
| 4791 | static int index = 0; |
| 4792 | char label[32]; |
| 4793 | |
| 4794 | if (strcmp(devname, "none") == 0) |
| 4795 | return 0; |
| 4796 | if (index == MAX_SERIAL_PORTS) { |
| 4797 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 4798 | exit(1); |
| 4799 | } |
| 4800 | snprintf(label, sizeof(label), "serial%d", index); |
| 4801 | serial_hds[index] = qemu_chr_open(label, devname, NULL); |
| 4802 | if (!serial_hds[index]) { |
| 4803 | fprintf(stderr, "qemu: could not open serial device '%s': %s\n", |
| 4804 | devname, strerror(errno)); |
| 4805 | return -1; |
| 4806 | } |
| 4807 | index++; |
| 4808 | return 0; |
| 4809 | } |
| 4810 | |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 4811 | static int parallel_parse(const char *devname) |
| 4812 | { |
| 4813 | static int index = 0; |
| 4814 | char label[32]; |
| 4815 | |
| 4816 | if (strcmp(devname, "none") == 0) |
| 4817 | return 0; |
| 4818 | if (index == MAX_PARALLEL_PORTS) { |
| 4819 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 4820 | exit(1); |
| 4821 | } |
| 4822 | snprintf(label, sizeof(label), "parallel%d", index); |
| 4823 | parallel_hds[index] = qemu_chr_open(label, devname, NULL); |
| 4824 | if (!parallel_hds[index]) { |
| 4825 | fprintf(stderr, "qemu: could not open parallel device '%s': %s\n", |
| 4826 | devname, strerror(errno)); |
| 4827 | return -1; |
| 4828 | } |
| 4829 | index++; |
| 4830 | return 0; |
| 4831 | } |
| 4832 | |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 4833 | static int virtcon_parse(const char *devname) |
| 4834 | { |
| 4835 | static int index = 0; |
| 4836 | char label[32]; |
| 4837 | |
| 4838 | if (strcmp(devname, "none") == 0) |
| 4839 | return 0; |
| 4840 | if (index == MAX_VIRTIO_CONSOLES) { |
| 4841 | fprintf(stderr, "qemu: too many virtio consoles\n"); |
| 4842 | exit(1); |
| 4843 | } |
| 4844 | snprintf(label, sizeof(label), "virtcon%d", index); |
| 4845 | virtcon_hds[index] = qemu_chr_open(label, devname, NULL); |
| 4846 | if (!virtcon_hds[index]) { |
| 4847 | fprintf(stderr, "qemu: could not open virtio console '%s': %s\n", |
| 4848 | devname, strerror(errno)); |
| 4849 | return -1; |
| 4850 | } |
| 4851 | index++; |
| 4852 | return 0; |
| 4853 | } |
| 4854 | |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4855 | int main(int argc, char **argv, char **envp) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4856 | { |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 4857 | const char *gdbstub_dev = NULL; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4858 | uint32_t boot_devices_bitmap = 0; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4859 | int i; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 4860 | int snapshot, linux_boot, net_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 4861 | const char *initrd_filename; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4862 | const char *kernel_filename, *kernel_cmdline; |
Anthony Liguori | 195325a | 2009-10-30 12:42:29 -0500 | [diff] [blame] | 4863 | char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 4864 | DisplayState *ds; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 4865 | DisplayChangeListener *dcl; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4866 | int cyls, heads, secs, translation; |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 4867 | QemuOpts *hda_opts = NULL, *opts; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4868 | int optind; |
| 4869 | const char *r, *optarg; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 4870 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4871 | QEMUMachine *machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4872 | const char *cpu_model; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4873 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 4874 | int fds[2]; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4875 | #endif |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4876 | int tb_size; |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 4877 | const char *pid_file = NULL; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 4878 | const char *incoming = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4879 | #ifndef _WIN32 |
aliguori | 54042bc | 2009-02-27 22:16:47 +0000 | [diff] [blame] | 4880 | int fd = 0; |
| 4881 | struct passwd *pwd = NULL; |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 4882 | const char *chroot_dir = NULL; |
| 4883 | const char *run_as = NULL; |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 4884 | #endif |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4885 | CPUState *env; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 4886 | int show_vnc_port = 0; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 4887 | |
Jan Kiszka | 6875204 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 4888 | init_clocks(); |
| 4889 | |
Gerd Hoffmann | ac7531e | 2009-08-14 10:36:06 +0200 | [diff] [blame] | 4890 | qemu_errors_to_file(stderr); |
malc | 902b3d5 | 2008-12-10 19:18:40 +0000 | [diff] [blame] | 4891 | qemu_cache_utils_init(envp); |
| 4892 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 4893 | QLIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4894 | #ifndef _WIN32 |
| 4895 | { |
| 4896 | struct sigaction act; |
| 4897 | sigfillset(&act.sa_mask); |
| 4898 | act.sa_flags = 0; |
| 4899 | act.sa_handler = SIG_IGN; |
| 4900 | sigaction(SIGPIPE, &act, NULL); |
| 4901 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 4902 | #else |
| 4903 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 4904 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 4905 | QEMU to run on a single CPU */ |
| 4906 | { |
| 4907 | HANDLE h; |
| 4908 | DWORD mask, smask; |
| 4909 | int i; |
| 4910 | h = GetCurrentProcess(); |
| 4911 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 4912 | for(i = 0; i < 32; i++) { |
| 4913 | if (mask & (1 << i)) |
| 4914 | break; |
| 4915 | } |
| 4916 | if (i != 32) { |
| 4917 | mask = 1 << i; |
| 4918 | SetProcessAffinityMask(h, mask); |
| 4919 | } |
| 4920 | } |
| 4921 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4922 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 4923 | |
Anthony Liguori | f80f9ec | 2009-05-20 18:38:09 -0500 | [diff] [blame] | 4924 | module_call_init(MODULE_INIT_MACHINE); |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4925 | machine = find_default_machine(); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4926 | cpu_model = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 4927 | initrd_filename = NULL; |
aurel32 | 4fc5d07 | 2008-04-27 21:39:40 +0000 | [diff] [blame] | 4928 | ram_size = 0; |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 4929 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 4930 | kernel_filename = NULL; |
| 4931 | kernel_cmdline = ""; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4932 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 4933 | translation = BIOS_ATA_TRANSLATION_AUTO; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4934 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4935 | for (i = 0; i < MAX_NODES; i++) { |
| 4936 | node_mem[i] = 0; |
| 4937 | node_cpumask[i] = 0; |
| 4938 | } |
| 4939 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 4940 | nb_numa_nodes = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4941 | nb_nics = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4942 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 4943 | tb_size = 0; |
blueswir1 | 41bd639 | 2008-10-05 09:56:21 +0000 | [diff] [blame] | 4944 | autostart= 1; |
| 4945 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4946 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4947 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4948 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4949 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4950 | r = argv[optind]; |
| 4951 | if (r[0] != '-') { |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 4952 | hda_opts = drive_add(argv[optind++], HD_ALIAS, 0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4953 | } else { |
| 4954 | const QEMUOption *popt; |
| 4955 | |
| 4956 | optind++; |
pbrook | dff5efc | 2007-01-27 17:19:39 +0000 | [diff] [blame] | 4957 | /* Treat --foo the same as -foo. */ |
| 4958 | if (r[1] == '-') |
| 4959 | r++; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4960 | popt = qemu_options; |
| 4961 | for(;;) { |
| 4962 | if (!popt->name) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4963 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 4964 | argv[0], r); |
| 4965 | exit(1); |
| 4966 | } |
| 4967 | if (!strcmp(popt->name, r + 1)) |
| 4968 | break; |
| 4969 | popt++; |
| 4970 | } |
| 4971 | if (popt->flags & HAS_ARG) { |
| 4972 | if (optind >= argc) { |
| 4973 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 4974 | argv[0], r); |
| 4975 | exit(1); |
| 4976 | } |
| 4977 | optarg = argv[optind++]; |
| 4978 | } else { |
| 4979 | optarg = NULL; |
| 4980 | } |
| 4981 | |
| 4982 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4983 | case QEMU_OPTION_M: |
| 4984 | machine = find_machine(optarg); |
| 4985 | if (!machine) { |
| 4986 | QEMUMachine *m; |
| 4987 | printf("Supported machines are:\n"); |
| 4988 | for(m = first_machine; m != NULL; m = m->next) { |
Mark McLoughlin | 3f6599e | 2009-07-22 10:02:50 +0100 | [diff] [blame] | 4989 | if (m->alias) |
| 4990 | printf("%-10s %s (alias of %s)\n", |
| 4991 | m->alias, m->desc, m->name); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4992 | printf("%-10s %s%s\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4993 | m->name, m->desc, |
Anthony Liguori | 0c25743 | 2009-05-21 20:41:01 -0500 | [diff] [blame] | 4994 | m->is_default ? " (default)" : ""); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4995 | } |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 4996 | exit(*optarg != '?'); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 4997 | } |
| 4998 | break; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 4999 | case QEMU_OPTION_cpu: |
| 5000 | /* hw initialization will check this */ |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5001 | if (*optarg == '?') { |
j_mayer | c732abe | 2007-10-12 06:47:46 +0000 | [diff] [blame] | 5002 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 5003 | #if defined(cpu_list) |
| 5004 | cpu_list(stdout, &fprintf); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 5005 | #endif |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5006 | exit(0); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 5007 | } else { |
| 5008 | cpu_model = optarg; |
| 5009 | } |
| 5010 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5011 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 5012 | initrd_filename = optarg; |
| 5013 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5014 | case QEMU_OPTION_hda: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5015 | if (cyls == 0) |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5016 | hda_opts = drive_add(optarg, HD_ALIAS, 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5017 | else |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5018 | hda_opts = drive_add(optarg, HD_ALIAS |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5019 | ",cyls=%d,heads=%d,secs=%d%s", |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5020 | 0, cyls, heads, secs, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5021 | translation == BIOS_ATA_TRANSLATION_LBA ? |
| 5022 | ",trans=lba" : |
| 5023 | translation == BIOS_ATA_TRANSLATION_NONE ? |
| 5024 | ",trans=none" : ""); |
| 5025 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5026 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5027 | case QEMU_OPTION_hdc: |
| 5028 | case QEMU_OPTION_hdd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5029 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 5030 | break; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5031 | case QEMU_OPTION_drive: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5032 | drive_add(NULL, "%s", optarg); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5033 | break; |
Gerd Hoffmann | d058fe0 | 2009-07-31 12:25:36 +0200 | [diff] [blame] | 5034 | case QEMU_OPTION_set: |
| 5035 | if (qemu_set_option(optarg) != 0) |
| 5036 | exit(1); |
| 5037 | break; |
Gerd Hoffmann | d0fef6f | 2009-12-08 13:11:34 +0100 | [diff] [blame] | 5038 | case QEMU_OPTION_global: |
| 5039 | if (qemu_global_option(optarg) != 0) |
| 5040 | exit(1); |
| 5041 | break; |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5042 | case QEMU_OPTION_mtdblock: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5043 | drive_add(optarg, MTD_ALIAS); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5044 | break; |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 5045 | case QEMU_OPTION_sd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5046 | drive_add(optarg, SD_ALIAS); |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 5047 | break; |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 5048 | case QEMU_OPTION_pflash: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5049 | drive_add(optarg, PFLASH_ALIAS); |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 5050 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5051 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 5052 | snapshot = 1; |
| 5053 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5054 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5055 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5056 | const char *p; |
| 5057 | p = optarg; |
| 5058 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5059 | if (cyls < 1 || cyls > 16383) |
| 5060 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5061 | if (*p != ',') |
| 5062 | goto chs_fail; |
| 5063 | p++; |
| 5064 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5065 | if (heads < 1 || heads > 16) |
| 5066 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5067 | if (*p != ',') |
| 5068 | goto chs_fail; |
| 5069 | p++; |
| 5070 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5071 | if (secs < 1 || secs > 63) |
| 5072 | goto chs_fail; |
| 5073 | if (*p == ',') { |
| 5074 | p++; |
| 5075 | if (!strcmp(p, "none")) |
| 5076 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 5077 | else if (!strcmp(p, "lba")) |
| 5078 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 5079 | else if (!strcmp(p, "auto")) |
| 5080 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 5081 | else |
| 5082 | goto chs_fail; |
| 5083 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5084 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 5085 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 5086 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5087 | } |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5088 | if (hda_opts != NULL) { |
| 5089 | char num[16]; |
| 5090 | snprintf(num, sizeof(num), "%d", cyls); |
| 5091 | qemu_opt_set(hda_opts, "cyls", num); |
| 5092 | snprintf(num, sizeof(num), "%d", heads); |
| 5093 | qemu_opt_set(hda_opts, "heads", num); |
| 5094 | snprintf(num, sizeof(num), "%d", secs); |
| 5095 | qemu_opt_set(hda_opts, "secs", num); |
| 5096 | if (translation == BIOS_ATA_TRANSLATION_LBA) |
| 5097 | qemu_opt_set(hda_opts, "trans", "lba"); |
| 5098 | if (translation == BIOS_ATA_TRANSLATION_NONE) |
| 5099 | qemu_opt_set(hda_opts, "trans", "none"); |
| 5100 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5101 | } |
| 5102 | break; |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5103 | case QEMU_OPTION_numa: |
| 5104 | if (nb_numa_nodes >= MAX_NODES) { |
| 5105 | fprintf(stderr, "qemu: too many NUMA nodes\n"); |
| 5106 | exit(1); |
| 5107 | } |
| 5108 | numa_add(optarg); |
| 5109 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5110 | case QEMU_OPTION_nographic: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5111 | display_type = DT_NOGRAPHIC; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5112 | break; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5113 | #ifdef CONFIG_CURSES |
| 5114 | case QEMU_OPTION_curses: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5115 | display_type = DT_CURSES; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 5116 | break; |
| 5117 | #endif |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 5118 | case QEMU_OPTION_portrait: |
| 5119 | graphic_rotate = 1; |
| 5120 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5121 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5122 | kernel_filename = optarg; |
| 5123 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5124 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5125 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5126 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5127 | case QEMU_OPTION_cdrom: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5128 | drive_add(optarg, CDROM_ALIAS); |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5129 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5130 | case QEMU_OPTION_boot: |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5131 | { |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5132 | static const char * const params[] = { |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5133 | "order", "once", "menu", NULL |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5134 | }; |
| 5135 | char buf[sizeof(boot_devices)]; |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5136 | char *standard_boot_devices; |
Jan Kiszka | ef3adf6 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5137 | int legacy = 0; |
| 5138 | |
| 5139 | if (!strchr(optarg, '=')) { |
| 5140 | legacy = 1; |
| 5141 | pstrcpy(buf, sizeof(buf), optarg); |
| 5142 | } else if (check_params(buf, sizeof(buf), params, optarg) < 0) { |
| 5143 | fprintf(stderr, |
| 5144 | "qemu: unknown boot parameter '%s' in '%s'\n", |
| 5145 | buf, optarg); |
| 5146 | exit(1); |
| 5147 | } |
| 5148 | |
| 5149 | if (legacy || |
| 5150 | get_param_value(buf, sizeof(buf), "order", optarg)) { |
| 5151 | boot_devices_bitmap = parse_bootdevices(buf); |
| 5152 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 5153 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5154 | if (!legacy) { |
| 5155 | if (get_param_value(buf, sizeof(buf), |
| 5156 | "once", optarg)) { |
| 5157 | boot_devices_bitmap |= parse_bootdevices(buf); |
| 5158 | standard_boot_devices = qemu_strdup(boot_devices); |
| 5159 | pstrcpy(boot_devices, sizeof(boot_devices), buf); |
| 5160 | qemu_register_reset(restore_boot_devices, |
| 5161 | standard_boot_devices); |
| 5162 | } |
Jan Kiszka | 9538749 | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5163 | if (get_param_value(buf, sizeof(buf), |
| 5164 | "menu", optarg)) { |
| 5165 | if (!strcmp(buf, "on")) { |
| 5166 | boot_menu = 1; |
| 5167 | } else if (!strcmp(buf, "off")) { |
| 5168 | boot_menu = 0; |
| 5169 | } else { |
| 5170 | fprintf(stderr, |
| 5171 | "qemu: invalid option value '%s'\n", |
| 5172 | buf); |
| 5173 | exit(1); |
| 5174 | } |
| 5175 | } |
Jan Kiszka | e0f084b | 2009-07-02 00:19:02 +0200 | [diff] [blame] | 5176 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 5177 | } |
| 5178 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5179 | case QEMU_OPTION_fda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5180 | case QEMU_OPTION_fdb: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5181 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 5182 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 5183 | #ifdef TARGET_I386 |
| 5184 | case QEMU_OPTION_no_fd_bootchk: |
| 5185 | fd_bootchk = 0; |
| 5186 | break; |
| 5187 | #endif |
Mark McLoughlin | a1ea458 | 2009-10-08 19:58:26 +0100 | [diff] [blame] | 5188 | case QEMU_OPTION_netdev: |
| 5189 | if (net_client_parse(&qemu_netdev_opts, optarg) == -1) { |
| 5190 | exit(1); |
| 5191 | } |
| 5192 | break; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5193 | case QEMU_OPTION_net: |
Mark McLoughlin | 7f161aa | 2009-10-08 19:58:25 +0100 | [diff] [blame] | 5194 | if (net_client_parse(&qemu_net_opts, optarg) == -1) { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5195 | exit(1); |
| 5196 | } |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5197 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5198 | #ifdef CONFIG_SLIRP |
| 5199 | case QEMU_OPTION_tftp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5200 | legacy_tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5201 | break; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5202 | case QEMU_OPTION_bootp: |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 5203 | legacy_bootp_filename = optarg; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 5204 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5205 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5206 | case QEMU_OPTION_smb: |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 5207 | if (net_slirp_smb(optarg) < 0) |
| 5208 | exit(1); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 5209 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 5210 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5211 | case QEMU_OPTION_redir: |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 5212 | if (net_slirp_redir(optarg) < 0) |
| 5213 | exit(1); |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 5214 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 5215 | #endif |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5216 | case QEMU_OPTION_bt: |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5217 | add_device_config(DEV_BT, optarg); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5218 | break; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5219 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 5220 | case QEMU_OPTION_audio_help: |
| 5221 | AUD_help (); |
| 5222 | exit (0); |
| 5223 | break; |
| 5224 | case QEMU_OPTION_soundhw: |
| 5225 | select_soundhw (optarg); |
| 5226 | break; |
| 5227 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5228 | case QEMU_OPTION_h: |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 5229 | help(0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5230 | break; |
pbrook | 9bd7e6d | 2009-04-07 22:58:45 +0000 | [diff] [blame] | 5231 | case QEMU_OPTION_version: |
| 5232 | version(); |
| 5233 | exit(0); |
| 5234 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5235 | case QEMU_OPTION_m: { |
| 5236 | uint64_t value; |
| 5237 | char *ptr; |
| 5238 | |
| 5239 | value = strtoul(optarg, &ptr, 10); |
| 5240 | switch (*ptr) { |
| 5241 | case 0: case 'M': case 'm': |
| 5242 | value <<= 20; |
| 5243 | break; |
| 5244 | case 'G': case 'g': |
| 5245 | value <<= 30; |
| 5246 | break; |
| 5247 | default: |
| 5248 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5249 | exit(1); |
| 5250 | } |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5251 | |
| 5252 | /* On 32-bit hosts, QEMU is limited by virtual address space */ |
Anthony Liguori | 4a1418e | 2009-08-10 17:07:24 -0500 | [diff] [blame] | 5253 | if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5254 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); |
| 5255 | exit(1); |
| 5256 | } |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5257 | if (value != (uint64_t)(ram_addr_t)value) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5258 | fprintf(stderr, "qemu: ram size too large\n"); |
| 5259 | exit(1); |
| 5260 | } |
| 5261 | ram_size = value; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5262 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 5263 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5264 | case QEMU_OPTION_d: |
| 5265 | { |
| 5266 | int mask; |
blueswir1 | c7cd6a3 | 2008-10-02 18:27:46 +0000 | [diff] [blame] | 5267 | const CPULogItem *item; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5268 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5269 | mask = cpu_str_to_log_mask(optarg); |
| 5270 | if (!mask) { |
| 5271 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5272 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 5273 | printf("%-10s %s\n", item->name, item->help); |
| 5274 | } |
| 5275 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5276 | } |
| 5277 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 5278 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5279 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5280 | case QEMU_OPTION_s: |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5281 | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5282 | break; |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 5283 | case QEMU_OPTION_gdb: |
| 5284 | gdbstub_dev = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5285 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5286 | case QEMU_OPTION_L: |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5287 | data_dir = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5288 | break; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 5289 | case QEMU_OPTION_bios: |
| 5290 | bios_name = optarg; |
| 5291 | break; |
aurel32 | 1b530a6 | 2009-04-05 20:08:59 +0000 | [diff] [blame] | 5292 | case QEMU_OPTION_singlestep: |
| 5293 | singlestep = 1; |
| 5294 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5295 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 5296 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5297 | break; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 5298 | case QEMU_OPTION_k: |
| 5299 | keyboard_layout = optarg; |
| 5300 | break; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 5301 | case QEMU_OPTION_localtime: |
| 5302 | rtc_utc = 0; |
| 5303 | break; |
malc | 3893c12 | 2008-09-28 00:42:05 +0000 | [diff] [blame] | 5304 | case QEMU_OPTION_vga: |
| 5305 | select_vgahw (optarg); |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 5306 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5307 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5308 | case QEMU_OPTION_g: |
| 5309 | { |
| 5310 | const char *p; |
| 5311 | int w, h, depth; |
| 5312 | p = optarg; |
| 5313 | w = strtol(p, (char **)&p, 10); |
| 5314 | if (w <= 0) { |
| 5315 | graphic_error: |
| 5316 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 5317 | exit(1); |
| 5318 | } |
| 5319 | if (*p != 'x') |
| 5320 | goto graphic_error; |
| 5321 | p++; |
| 5322 | h = strtol(p, (char **)&p, 10); |
| 5323 | if (h <= 0) |
| 5324 | goto graphic_error; |
| 5325 | if (*p == 'x') { |
| 5326 | p++; |
| 5327 | depth = strtol(p, (char **)&p, 10); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5328 | if (depth != 8 && depth != 15 && depth != 16 && |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5329 | depth != 24 && depth != 32) |
| 5330 | goto graphic_error; |
| 5331 | } else if (*p == '\0') { |
| 5332 | depth = graphic_depth; |
| 5333 | } else { |
| 5334 | goto graphic_error; |
| 5335 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5336 | |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 5337 | graphic_width = w; |
| 5338 | graphic_height = h; |
| 5339 | graphic_depth = depth; |
| 5340 | } |
| 5341 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5342 | #endif |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 5343 | case QEMU_OPTION_echr: |
| 5344 | { |
| 5345 | char *r; |
| 5346 | term_escape_char = strtol(optarg, &r, 0); |
| 5347 | if (r == optarg) |
| 5348 | printf("Bad argument to echr\n"); |
| 5349 | break; |
| 5350 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5351 | case QEMU_OPTION_monitor: |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 5352 | monitor_parse(optarg, "readline"); |
| 5353 | default_monitor = 0; |
| 5354 | break; |
| 5355 | case QEMU_OPTION_qmp: |
| 5356 | monitor_parse(optarg, "control"); |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 5357 | default_monitor = 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5358 | break; |
Gerd Hoffmann | 22a0e04 | 2009-12-08 13:11:51 +0100 | [diff] [blame] | 5359 | case QEMU_OPTION_mon: |
| 5360 | opts = qemu_opts_parse(&qemu_mon_opts, optarg, "chardev"); |
| 5361 | if (!opts) { |
| 5362 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5363 | exit(1); |
| 5364 | } |
| 5365 | default_monitor = 0; |
| 5366 | break; |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 5367 | case QEMU_OPTION_chardev: |
| 5368 | opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend"); |
| 5369 | if (!opts) { |
| 5370 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5371 | exit(1); |
| 5372 | } |
Gerd Hoffmann | 191bc01 | 2009-09-10 10:58:35 +0200 | [diff] [blame] | 5373 | break; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5374 | case QEMU_OPTION_serial: |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5375 | add_device_config(DEV_SERIAL, optarg); |
| 5376 | default_serial = 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 5377 | break; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5378 | case QEMU_OPTION_watchdog: |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5379 | if (watchdog) { |
| 5380 | fprintf(stderr, |
| 5381 | "qemu: only one watchdog option may be given\n"); |
| 5382 | return 1; |
| 5383 | } |
| 5384 | watchdog = optarg; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 5385 | break; |
| 5386 | case QEMU_OPTION_watchdog_action: |
| 5387 | if (select_watchdog_action(optarg) == -1) { |
| 5388 | fprintf(stderr, "Unknown -watchdog-action parameter\n"); |
| 5389 | exit(1); |
| 5390 | } |
| 5391 | break; |
aliguori | 51ecf13 | 2009-01-15 20:06:40 +0000 | [diff] [blame] | 5392 | case QEMU_OPTION_virtiocon: |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 5393 | add_device_config(DEV_VIRTCON, optarg); |
| 5394 | default_virtcon = 0; |
aliguori | 51ecf13 | 2009-01-15 20:06:40 +0000 | [diff] [blame] | 5395 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5396 | case QEMU_OPTION_parallel: |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5397 | add_device_config(DEV_PARALLEL, optarg); |
| 5398 | default_parallel = 0; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 5399 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 5400 | case QEMU_OPTION_loadvm: |
| 5401 | loadvm = optarg; |
| 5402 | break; |
| 5403 | case QEMU_OPTION_full_screen: |
| 5404 | full_screen = 1; |
| 5405 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5406 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 5407 | case QEMU_OPTION_no_frame: |
| 5408 | no_frame = 1; |
| 5409 | break; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 5410 | case QEMU_OPTION_alt_grab: |
| 5411 | alt_grab = 1; |
| 5412 | break; |
Dustin Kirkland | 0ca9f8a | 2009-09-17 15:48:04 -0500 | [diff] [blame] | 5413 | case QEMU_OPTION_ctrl_grab: |
| 5414 | ctrl_grab = 1; |
| 5415 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5416 | case QEMU_OPTION_no_quit: |
| 5417 | no_quit = 1; |
| 5418 | break; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5419 | case QEMU_OPTION_sdl: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5420 | display_type = DT_SDL; |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 5421 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 5422 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5423 | case QEMU_OPTION_pidfile: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5424 | pid_file = optarg; |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5425 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5426 | #ifdef TARGET_I386 |
| 5427 | case QEMU_OPTION_win2k_hack: |
| 5428 | win2k_install_hack = 1; |
| 5429 | break; |
aliguori | 73822ec | 2009-01-15 20:11:34 +0000 | [diff] [blame] | 5430 | case QEMU_OPTION_rtc_td_hack: |
| 5431 | rtc_td_hack = 1; |
| 5432 | break; |
aliguori | 8a92ea2 | 2009-02-27 20:12:36 +0000 | [diff] [blame] | 5433 | case QEMU_OPTION_acpitable: |
| 5434 | if(acpi_table_add(optarg) < 0) { |
| 5435 | fprintf(stderr, "Wrong acpi table provided\n"); |
| 5436 | exit(1); |
| 5437 | } |
| 5438 | break; |
aliguori | b6f6e3d | 2009-04-17 18:59:56 +0000 | [diff] [blame] | 5439 | case QEMU_OPTION_smbios: |
| 5440 | if(smbios_entry_add(optarg) < 0) { |
| 5441 | fprintf(stderr, "Wrong smbios provided\n"); |
| 5442 | exit(1); |
| 5443 | } |
| 5444 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 5445 | #endif |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5446 | #ifdef CONFIG_KVM |
| 5447 | case QEMU_OPTION_enable_kvm: |
| 5448 | kvm_allowed = 1; |
aliguori | 7ba1e61 | 2008-11-05 16:04:33 +0000 | [diff] [blame] | 5449 | break; |
| 5450 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 5451 | case QEMU_OPTION_usb: |
| 5452 | usb_enabled = 1; |
| 5453 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5454 | case QEMU_OPTION_usbdevice: |
| 5455 | usb_enabled = 1; |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5456 | add_device_config(DEV_USB, optarg); |
| 5457 | break; |
| 5458 | case QEMU_OPTION_device: |
Mark McLoughlin | b386bec | 2009-10-06 12:17:01 +0100 | [diff] [blame] | 5459 | if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) { |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5460 | exit(1); |
| 5461 | } |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5462 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5463 | case QEMU_OPTION_smp: |
Andre Przywara | dc6b1c0 | 2009-08-19 15:42:40 +0200 | [diff] [blame] | 5464 | smp_parse(optarg); |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5465 | if (smp_cpus < 1) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5466 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 5467 | exit(1); |
| 5468 | } |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5469 | if (max_cpus < smp_cpus) { |
| 5470 | fprintf(stderr, "maxcpus must be equal to or greater than " |
| 5471 | "smp\n"); |
| 5472 | exit(1); |
| 5473 | } |
| 5474 | if (max_cpus > 255) { |
| 5475 | fprintf(stderr, "Unsupported number of maxcpus\n"); |
| 5476 | exit(1); |
| 5477 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5478 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5479 | case QEMU_OPTION_vnc: |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5480 | display_type = DT_VNC; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 5481 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 5482 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5483 | #ifdef TARGET_I386 |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 5484 | case QEMU_OPTION_no_acpi: |
| 5485 | acpi_enabled = 0; |
| 5486 | break; |
aliguori | 16b29ae | 2008-12-17 23:28:44 +0000 | [diff] [blame] | 5487 | case QEMU_OPTION_no_hpet: |
| 5488 | no_hpet = 1; |
| 5489 | break; |
Markus Armbruster | 7d4c3d5 | 2009-06-26 19:15:14 +0200 | [diff] [blame] | 5490 | case QEMU_OPTION_balloon: |
| 5491 | if (balloon_parse(optarg) < 0) { |
| 5492 | fprintf(stderr, "Unknown -balloon argument %s\n", optarg); |
| 5493 | exit(1); |
| 5494 | } |
Eduardo Habkost | df97b92 | 2009-06-10 16:34:08 -0300 | [diff] [blame] | 5495 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5496 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 5497 | case QEMU_OPTION_no_reboot: |
| 5498 | no_reboot = 1; |
| 5499 | break; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 5500 | case QEMU_OPTION_no_shutdown: |
| 5501 | no_shutdown = 1; |
| 5502 | break; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 5503 | case QEMU_OPTION_show_cursor: |
| 5504 | cursor_hide = 0; |
| 5505 | break; |
blueswir1 | 8fcb1b9 | 2008-09-18 18:29:08 +0000 | [diff] [blame] | 5506 | case QEMU_OPTION_uuid: |
| 5507 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { |
| 5508 | fprintf(stderr, "Fail to parse UUID string." |
| 5509 | " Wrong format.\n"); |
| 5510 | exit(1); |
| 5511 | } |
| 5512 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5513 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5514 | case QEMU_OPTION_daemonize: |
| 5515 | daemonize = 1; |
| 5516 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5517 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 5518 | case QEMU_OPTION_option_rom: |
| 5519 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 5520 | fprintf(stderr, "Too many option ROMs\n"); |
| 5521 | exit(1); |
| 5522 | } |
| 5523 | option_rom[nb_option_roms] = optarg; |
| 5524 | nb_option_roms++; |
| 5525 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5526 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 5527 | case QEMU_OPTION_semihosting: |
| 5528 | semihosting_enabled = 1; |
| 5529 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5530 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5531 | case QEMU_OPTION_name: |
Andi Kleen | 1889465 | 2009-07-02 09:34:17 +0200 | [diff] [blame] | 5532 | qemu_name = qemu_strdup(optarg); |
| 5533 | { |
| 5534 | char *p = strchr(qemu_name, ','); |
| 5535 | if (p != NULL) { |
| 5536 | *p++ = 0; |
| 5537 | if (strncmp(p, "process=", 8)) { |
| 5538 | fprintf(stderr, "Unknown subargument %s to -name", p); |
| 5539 | exit(1); |
| 5540 | } |
| 5541 | p += 8; |
| 5542 | set_proc_name(p); |
| 5543 | } |
| 5544 | } |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 5545 | break; |
blueswir1 | 95efd11 | 2008-12-24 20:26:14 +0000 | [diff] [blame] | 5546 | #if defined(TARGET_SPARC) || defined(TARGET_PPC) |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 5547 | case QEMU_OPTION_prom_env: |
| 5548 | if (nb_prom_envs >= MAX_PROM_ENVS) { |
| 5549 | fprintf(stderr, "Too many prom variables\n"); |
| 5550 | exit(1); |
| 5551 | } |
| 5552 | prom_envs[nb_prom_envs] = optarg; |
| 5553 | nb_prom_envs++; |
| 5554 | break; |
| 5555 | #endif |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5556 | #ifdef TARGET_ARM |
| 5557 | case QEMU_OPTION_old_param: |
| 5558 | old_param = 1; |
ths | 05ebd53 | 2008-01-08 19:32:16 +0000 | [diff] [blame] | 5559 | break; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 5560 | #endif |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 5561 | case QEMU_OPTION_clock: |
| 5562 | configure_alarms(optarg); |
| 5563 | break; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5564 | case QEMU_OPTION_startdate: |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 5565 | configure_rtc_date_offset(optarg, 1); |
| 5566 | break; |
| 5567 | case QEMU_OPTION_rtc: |
| 5568 | opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL); |
| 5569 | if (!opts) { |
| 5570 | fprintf(stderr, "parse error: %s\n", optarg); |
| 5571 | exit(1); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5572 | } |
Jan Kiszka | 1ed2fc1 | 2009-09-15 13:36:04 +0200 | [diff] [blame] | 5573 | configure_rtc(opts); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 5574 | break; |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5575 | case QEMU_OPTION_tb_size: |
| 5576 | tb_size = strtol(optarg, NULL, 0); |
| 5577 | if (tb_size < 0) |
| 5578 | tb_size = 0; |
| 5579 | break; |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5580 | case QEMU_OPTION_icount: |
| 5581 | use_icount = 1; |
| 5582 | if (strcmp(optarg, "auto") == 0) { |
| 5583 | icount_time_shift = -1; |
| 5584 | } else { |
| 5585 | icount_time_shift = strtol(optarg, NULL, 0); |
| 5586 | } |
| 5587 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 5588 | case QEMU_OPTION_incoming: |
| 5589 | incoming = optarg; |
| 5590 | break; |
Gerd Hoffmann | d8c208d | 2009-12-08 13:11:46 +0100 | [diff] [blame] | 5591 | case QEMU_OPTION_nodefaults: |
| 5592 | default_serial = 0; |
| 5593 | default_parallel = 0; |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 5594 | default_virtcon = 0; |
Gerd Hoffmann | d8c208d | 2009-12-08 13:11:46 +0100 | [diff] [blame] | 5595 | default_monitor = 0; |
| 5596 | default_vga = 0; |
Gerd Hoffmann | cb4522c | 2009-12-08 13:11:47 +0100 | [diff] [blame] | 5597 | default_net = 0; |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5598 | default_floppy = 0; |
| 5599 | default_cdrom = 0; |
| 5600 | default_sdcard = 0; |
Gerd Hoffmann | d8c208d | 2009-12-08 13:11:46 +0100 | [diff] [blame] | 5601 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5602 | #ifndef _WIN32 |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 5603 | case QEMU_OPTION_chroot: |
| 5604 | chroot_dir = optarg; |
| 5605 | break; |
| 5606 | case QEMU_OPTION_runas: |
| 5607 | run_as = optarg; |
| 5608 | break; |
blueswir1 | 5824d65 | 2009-03-28 06:44:27 +0000 | [diff] [blame] | 5609 | #endif |
aliguori | e37630c | 2009-04-22 15:19:10 +0000 | [diff] [blame] | 5610 | #ifdef CONFIG_XEN |
| 5611 | case QEMU_OPTION_xen_domid: |
| 5612 | xen_domid = atoi(optarg); |
| 5613 | break; |
| 5614 | case QEMU_OPTION_xen_create: |
| 5615 | xen_mode = XEN_CREATE; |
| 5616 | break; |
| 5617 | case QEMU_OPTION_xen_attach: |
| 5618 | xen_mode = XEN_ATTACH; |
| 5619 | break; |
| 5620 | #endif |
Gerd Hoffmann | 715a664 | 2009-10-14 10:39:28 +0200 | [diff] [blame] | 5621 | case QEMU_OPTION_readconfig: |
| 5622 | { |
| 5623 | FILE *fp; |
| 5624 | fp = fopen(optarg, "r"); |
| 5625 | if (fp == NULL) { |
| 5626 | fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); |
| 5627 | exit(1); |
| 5628 | } |
| 5629 | if (qemu_config_parse(fp) != 0) { |
| 5630 | exit(1); |
| 5631 | } |
| 5632 | fclose(fp); |
| 5633 | break; |
| 5634 | } |
| 5635 | case QEMU_OPTION_writeconfig: |
| 5636 | { |
| 5637 | FILE *fp; |
| 5638 | if (strcmp(optarg, "-") == 0) { |
| 5639 | fp = stdout; |
| 5640 | } else { |
| 5641 | fp = fopen(optarg, "w"); |
| 5642 | if (fp == NULL) { |
| 5643 | fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); |
| 5644 | exit(1); |
| 5645 | } |
| 5646 | } |
| 5647 | qemu_config_write(fp); |
| 5648 | fclose(fp); |
| 5649 | break; |
| 5650 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 5651 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5652 | } |
| 5653 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5654 | |
Paul Brook | 5cea859 | 2009-05-30 00:52:44 +0100 | [diff] [blame] | 5655 | /* If no data_dir is specified then try to find it relative to the |
| 5656 | executable path. */ |
| 5657 | if (!data_dir) { |
| 5658 | data_dir = find_datadir(argv[0]); |
| 5659 | } |
| 5660 | /* If all else fails use the install patch specified when building. */ |
| 5661 | if (!data_dir) { |
| 5662 | data_dir = CONFIG_QEMU_SHAREDIR; |
| 5663 | } |
| 5664 | |
Jes Sorensen | 6be68d7 | 2009-07-23 17:03:42 +0200 | [diff] [blame] | 5665 | /* |
| 5666 | * Default to max_cpus = smp_cpus, in case the user doesn't |
| 5667 | * specify a max_cpus value. |
| 5668 | */ |
| 5669 | if (!max_cpus) |
| 5670 | max_cpus = smp_cpus; |
| 5671 | |
balrog | 3d878ca | 2008-10-28 10:59:59 +0000 | [diff] [blame] | 5672 | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
aliguori | b209700 | 2008-10-07 20:39:39 +0000 | [diff] [blame] | 5673 | if (smp_cpus > machine->max_cpus) { |
| 5674 | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus " |
| 5675 | "supported by machine `%s' (%d)\n", smp_cpus, machine->name, |
| 5676 | machine->max_cpus); |
| 5677 | exit(1); |
| 5678 | } |
| 5679 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5680 | qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0); |
Gerd Hoffmann | d8bcbab | 2009-12-16 14:25:40 +0100 | [diff] [blame] | 5681 | qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0); |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5682 | |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 5683 | if (machine->no_serial) { |
| 5684 | default_serial = 0; |
| 5685 | } |
| 5686 | if (machine->no_parallel) { |
| 5687 | default_parallel = 0; |
| 5688 | } |
| 5689 | if (!machine->use_virtcon) { |
| 5690 | default_virtcon = 0; |
| 5691 | } |
| 5692 | if (machine->no_vga) { |
| 5693 | default_vga = 0; |
| 5694 | } |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5695 | if (machine->no_floppy) { |
| 5696 | default_floppy = 0; |
| 5697 | } |
| 5698 | if (machine->no_cdrom) { |
| 5699 | default_cdrom = 0; |
| 5700 | } |
| 5701 | if (machine->no_sdcard) { |
| 5702 | default_sdcard = 0; |
| 5703 | } |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 5704 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5705 | if (display_type == DT_NOGRAPHIC) { |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5706 | if (default_parallel) |
| 5707 | add_device_config(DEV_PARALLEL, "null"); |
Gerd Hoffmann | e1c0917 | 2009-12-08 13:11:44 +0100 | [diff] [blame] | 5708 | if (default_serial && default_monitor) { |
| 5709 | add_device_config(DEV_SERIAL, "mon:stdio"); |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 5710 | } else if (default_virtcon && default_monitor) { |
| 5711 | add_device_config(DEV_VIRTCON, "mon:stdio"); |
Gerd Hoffmann | e1c0917 | 2009-12-08 13:11:44 +0100 | [diff] [blame] | 5712 | } else { |
| 5713 | if (default_serial) |
| 5714 | add_device_config(DEV_SERIAL, "stdio"); |
Gerd Hoffmann | 986c5f7 | 2009-12-08 13:11:54 +0100 | [diff] [blame] | 5715 | if (default_virtcon) |
| 5716 | add_device_config(DEV_VIRTCON, "stdio"); |
Gerd Hoffmann | e1c0917 | 2009-12-08 13:11:44 +0100 | [diff] [blame] | 5717 | if (default_monitor) |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 5718 | monitor_parse("stdio", "readline"); |
Gerd Hoffmann | e1c0917 | 2009-12-08 13:11:44 +0100 | [diff] [blame] | 5719 | } |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5720 | } else { |
| 5721 | if (default_serial) |
| 5722 | add_device_config(DEV_SERIAL, "vc:80Cx24C"); |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5723 | if (default_parallel) |
| 5724 | add_device_config(DEV_PARALLEL, "vc:80Cx24C"); |
Gerd Hoffmann | abdeed0 | 2009-12-08 13:11:43 +0100 | [diff] [blame] | 5725 | if (default_monitor) |
Gerd Hoffmann | 6ca5582 | 2009-12-08 13:11:52 +0100 | [diff] [blame] | 5726 | monitor_parse("vc:80Cx24C", "readline"); |
Alexander Graf | 38536da | 2009-12-17 13:06:08 +0100 | [diff] [blame] | 5727 | if (default_virtcon) |
| 5728 | add_device_config(DEV_VIRTCON, "vc:80Cx24C"); |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5729 | } |
Gerd Hoffmann | 6446529 | 2009-12-08 13:11:45 +0100 | [diff] [blame] | 5730 | if (default_vga) |
| 5731 | vga_interface_type = VGA_CIRRUS; |
aliguori | bc0129d | 2008-08-01 15:12:34 +0000 | [diff] [blame] | 5732 | |
Gerd Hoffmann | 1a688d3 | 2009-12-08 13:11:36 +0100 | [diff] [blame] | 5733 | if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0) |
| 5734 | exit(1); |
| 5735 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5736 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5737 | if (daemonize) { |
| 5738 | pid_t pid; |
| 5739 | |
| 5740 | if (pipe(fds) == -1) |
| 5741 | exit(1); |
| 5742 | |
| 5743 | pid = fork(); |
| 5744 | if (pid > 0) { |
| 5745 | uint8_t status; |
| 5746 | ssize_t len; |
| 5747 | |
| 5748 | close(fds[1]); |
| 5749 | |
| 5750 | again: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5751 | len = read(fds[0], &status, 1); |
| 5752 | if (len == -1 && (errno == EINTR)) |
| 5753 | goto again; |
| 5754 | |
| 5755 | if (len != 1) |
| 5756 | exit(1); |
| 5757 | else if (status == 1) { |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 5758 | fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno)); |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5759 | exit(1); |
| 5760 | } else |
| 5761 | exit(0); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5762 | } else if (pid < 0) |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5763 | exit(1); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5764 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 5765 | close(fds[0]); |
| 5766 | qemu_set_cloexec(fds[1]); |
| 5767 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5768 | setsid(); |
| 5769 | |
| 5770 | pid = fork(); |
| 5771 | if (pid > 0) |
| 5772 | exit(0); |
| 5773 | else if (pid < 0) |
| 5774 | exit(1); |
| 5775 | |
| 5776 | umask(027); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5777 | |
| 5778 | signal(SIGTSTP, SIG_IGN); |
| 5779 | signal(SIGTTOU, SIG_IGN); |
| 5780 | signal(SIGTTIN, SIG_IGN); |
| 5781 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 5782 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 5783 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5784 | if (daemonize) { |
| 5785 | uint8_t status = 1; |
| 5786 | write(fds[1], &status, 1); |
| 5787 | } else |
Justin M. Forbes | 850810d | 2009-10-01 09:42:56 -0500 | [diff] [blame] | 5788 | fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno)); |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5789 | exit(1); |
| 5790 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 5791 | #endif |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 5792 | |
Marcelo Tosatti | 214910a | 2009-09-18 02:41:23 -0300 | [diff] [blame] | 5793 | if (kvm_enabled()) { |
| 5794 | int ret; |
| 5795 | |
| 5796 | ret = kvm_init(smp_cpus); |
| 5797 | if (ret < 0) { |
| 5798 | fprintf(stderr, "failed to initialize KVM\n"); |
| 5799 | exit(1); |
| 5800 | } |
| 5801 | } |
| 5802 | |
aliguori | 3fcf7b6 | 2009-04-24 18:03:25 +0000 | [diff] [blame] | 5803 | if (qemu_init_main_loop()) { |
| 5804 | fprintf(stderr, "qemu_init_main_loop failed\n"); |
| 5805 | exit(1); |
| 5806 | } |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 5807 | linux_boot = (kernel_filename != NULL); |
balrog | 6c41b27 | 2007-11-17 12:12:29 +0000 | [diff] [blame] | 5808 | |
ths | f8d39c0 | 2008-07-03 10:01:15 +0000 | [diff] [blame] | 5809 | if (!linux_boot && *kernel_cmdline != '\0') { |
| 5810 | fprintf(stderr, "-append only allowed with -kernel option\n"); |
| 5811 | exit(1); |
| 5812 | } |
| 5813 | |
| 5814 | if (!linux_boot && initrd_filename != NULL) { |
| 5815 | fprintf(stderr, "-initrd only allowed with -kernel option\n"); |
| 5816 | exit(1); |
| 5817 | } |
| 5818 | |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5819 | #ifndef _WIN32 |
| 5820 | /* Win32 doesn't support line-buffering and requires size >= 2 */ |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 5821 | setvbuf(stdout, NULL, _IOLBF, 0); |
Filip Navara | bf65f53 | 2009-07-27 10:02:04 -0500 | [diff] [blame] | 5822 | #endif |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5823 | |
aliguori | 7183b4b | 2008-11-05 20:40:18 +0000 | [diff] [blame] | 5824 | if (init_timer_alarm() < 0) { |
| 5825 | fprintf(stderr, "could not initialize alarm timer\n"); |
| 5826 | exit(1); |
| 5827 | } |
pbrook | 2e70f6e | 2008-06-29 01:03:05 +0000 | [diff] [blame] | 5828 | if (use_icount && icount_time_shift < 0) { |
| 5829 | use_icount = 2; |
| 5830 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 5831 | It will be corrected fairly quickly anyway. */ |
| 5832 | icount_time_shift = 3; |
| 5833 | init_icount_adjust(); |
| 5834 | } |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 5835 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5836 | #ifdef _WIN32 |
| 5837 | socket_init(); |
| 5838 | #endif |
| 5839 | |
Mark McLoughlin | dc1c9fe | 2009-10-06 12:17:16 +0100 | [diff] [blame] | 5840 | if (net_init_clients() < 0) { |
| 5841 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 5842 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 5843 | |
Glauber Costa | 406c8df | 2009-06-17 09:05:30 -0400 | [diff] [blame] | 5844 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
| 5845 | net_set_boot_mask(net_boot); |
| 5846 | |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5847 | /* init the bluetooth world */ |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5848 | if (foreach_device_config(DEV_BT, bt_parse)) |
| 5849 | exit(1); |
balrog | dc72ac1 | 2008-11-09 00:04:26 +0000 | [diff] [blame] | 5850 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5851 | /* init the memory */ |
pbrook | 94a6b54 | 2009-04-11 17:15:54 +0000 | [diff] [blame] | 5852 | if (ram_size == 0) |
| 5853 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 5854 | |
bellard | 26a5f13 | 2008-05-28 12:30:31 +0000 | [diff] [blame] | 5855 | /* init the dynamic translator */ |
| 5856 | cpu_exec_init_all(tb_size * 1024 * 1024); |
| 5857 | |
Markus Armbruster | eb85201 | 2009-10-27 18:41:44 +0100 | [diff] [blame] | 5858 | bdrv_init_with_whitelist(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5859 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 5860 | blk_mig_init(); |
| 5861 | |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5862 | if (default_cdrom) { |
Gerd Hoffmann | aa40fc9 | 2009-12-08 13:11:48 +0100 | [diff] [blame] | 5863 | /* we always create the cdrom drive, even if no disk is there */ |
| 5864 | drive_add(NULL, CDROM_ALIAS); |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5865 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5866 | |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5867 | if (default_floppy) { |
Gerd Hoffmann | aa40fc9 | 2009-12-08 13:11:48 +0100 | [diff] [blame] | 5868 | /* we always create at least one floppy */ |
| 5869 | drive_add(NULL, FD_ALIAS, 0); |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5870 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5871 | |
Gerd Hoffmann | ac33f8f | 2009-12-16 14:25:39 +0100 | [diff] [blame] | 5872 | if (default_sdcard) { |
Gerd Hoffmann | aa40fc9 | 2009-12-08 13:11:48 +0100 | [diff] [blame] | 5873 | /* we always create one sd slot, even if no card is in it */ |
| 5874 | drive_add(NULL, SD_ALIAS); |
| 5875 | } |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 5876 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 5877 | /* open the virtual block devices */ |
Gerd Hoffmann | 9dfd7c7 | 2009-07-22 16:43:04 +0200 | [diff] [blame] | 5878 | if (snapshot) |
Gerd Hoffmann | 7282a03 | 2009-07-31 12:25:35 +0200 | [diff] [blame] | 5879 | qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
| 5880 | 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] | 5881 | exit(1); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 5882 | |
Juan Quintela | 2faf58c | 2009-09-10 03:04:28 +0200 | [diff] [blame] | 5883 | vmstate_register(0, &vmstate_timers ,&timers_state); |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 5884 | register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL, |
| 5885 | ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5886 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5887 | if (nb_numa_nodes > 0) { |
| 5888 | int i; |
| 5889 | |
| 5890 | if (nb_numa_nodes > smp_cpus) { |
| 5891 | nb_numa_nodes = smp_cpus; |
| 5892 | } |
| 5893 | |
| 5894 | /* If no memory size if given for any node, assume the default case |
| 5895 | * and distribute the available memory equally across all nodes |
| 5896 | */ |
| 5897 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5898 | if (node_mem[i] != 0) |
| 5899 | break; |
| 5900 | } |
| 5901 | if (i == nb_numa_nodes) { |
| 5902 | uint64_t usedmem = 0; |
| 5903 | |
| 5904 | /* On Linux, the each node's border has to be 8MB aligned, |
| 5905 | * the final node gets the rest. |
| 5906 | */ |
| 5907 | for (i = 0; i < nb_numa_nodes - 1; i++) { |
| 5908 | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); |
| 5909 | usedmem += node_mem[i]; |
| 5910 | } |
| 5911 | node_mem[i] = ram_size - usedmem; |
| 5912 | } |
| 5913 | |
| 5914 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5915 | if (node_cpumask[i] != 0) |
| 5916 | break; |
| 5917 | } |
| 5918 | /* assigning the VCPUs round-robin is easier to implement, guest OSes |
| 5919 | * must cope with this anyway, because there are BIOSes out there in |
| 5920 | * real machines which also use this scheme. |
| 5921 | */ |
| 5922 | if (i == nb_numa_nodes) { |
| 5923 | for (i = 0; i < smp_cpus; i++) { |
| 5924 | node_cpumask[i % nb_numa_nodes] |= 1 << i; |
| 5925 | } |
| 5926 | } |
| 5927 | } |
| 5928 | |
Gerd Hoffmann | 998bbd7 | 2009-12-08 13:11:41 +0100 | [diff] [blame] | 5929 | if (foreach_device_config(DEV_SERIAL, serial_parse) < 0) |
| 5930 | exit(1); |
Gerd Hoffmann | 6a5e8b0 | 2009-12-08 13:11:42 +0100 | [diff] [blame] | 5931 | if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0) |
| 5932 | exit(1); |
Gerd Hoffmann | aee1b93 | 2009-12-08 13:11:53 +0100 | [diff] [blame] | 5933 | if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0) |
| 5934 | exit(1); |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 5935 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 5936 | module_call_init(MODULE_INIT_DEVICE); |
| 5937 | |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 5938 | if (watchdog) { |
| 5939 | i = select_watchdog(watchdog); |
| 5940 | if (i > 0) |
| 5941 | exit (i == 1 ? 1 : 0); |
| 5942 | } |
| 5943 | |
Gerd Hoffmann | b6b6114 | 2009-07-15 13:48:21 +0200 | [diff] [blame] | 5944 | if (machine->compat_props) { |
Gerd Hoffmann | 458fb67 | 2009-12-08 13:11:33 +0100 | [diff] [blame] | 5945 | qdev_prop_register_global_list(machine->compat_props); |
Gerd Hoffmann | b6b6114 | 2009-07-15 13:48:21 +0200 | [diff] [blame] | 5946 | } |
Gerd Hoffmann | d0fef6f | 2009-12-08 13:11:34 +0100 | [diff] [blame] | 5947 | qemu_add_globals(); |
| 5948 | |
Paul Brook | fbe1b59 | 2009-05-13 17:56:25 +0100 | [diff] [blame] | 5949 | machine->init(ram_size, boot_devices, |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5950 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
| 5951 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5952 | |
Juan Quintela | 67b3b71 | 2009-08-28 19:25:15 +0200 | [diff] [blame] | 5953 | #ifndef _WIN32 |
| 5954 | /* must be after terminal init, SDL library changes signal handlers */ |
| 5955 | sighandler_setup(); |
| 5956 | #endif |
| 5957 | |
aliguori | 268a362 | 2009-04-21 22:30:27 +0000 | [diff] [blame] | 5958 | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
| 5959 | for (i = 0; i < nb_numa_nodes; i++) { |
| 5960 | if (node_cpumask[i] & (1 << env->cpu_index)) { |
| 5961 | env->numa_node = i; |
| 5962 | } |
| 5963 | } |
| 5964 | } |
| 5965 | |
aliguori | 6f338c3 | 2009-02-11 15:21:54 +0000 | [diff] [blame] | 5966 | current_machine = machine; |
| 5967 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5968 | /* init USB devices */ |
| 5969 | if (usb_enabled) { |
Markus Armbruster | 0752706 | 2009-10-06 12:16:57 +0100 | [diff] [blame] | 5970 | if (foreach_device_config(DEV_USB, usb_parse) < 0) |
| 5971 | exit(1); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5972 | } |
| 5973 | |
Gerd Hoffmann | bd3c948 | 2009-07-15 13:59:26 +0200 | [diff] [blame] | 5974 | /* init generic devices */ |
Gerd Hoffmann | f31d07d | 2009-07-31 12:25:37 +0200 | [diff] [blame] | 5975 | 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] | 5976 | exit(1); |
| 5977 | |
aliguori | 8f391ab | 2009-01-19 16:34:10 +0000 | [diff] [blame] | 5978 | if (!display_state) |
| 5979 | dumb_display_init(); |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 5980 | /* just use the first displaystate for the moment */ |
| 5981 | ds = display_state; |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5982 | |
| 5983 | if (display_type == DT_DEFAULT) { |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 5984 | #if defined(CONFIG_SDL) || defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 5985 | display_type = DT_SDL; |
| 5986 | #else |
| 5987 | display_type = DT_VNC; |
| 5988 | vnc_display = "localhost:0,to=99"; |
| 5989 | show_vnc_port = 1; |
| 5990 | #endif |
| 5991 | } |
| 5992 | |
| 5993 | |
| 5994 | switch (display_type) { |
| 5995 | case DT_NOGRAPHIC: |
| 5996 | break; |
| 5997 | #if defined(CONFIG_CURSES) |
| 5998 | case DT_CURSES: |
| 5999 | curses_display_init(ds, full_screen); |
| 6000 | break; |
| 6001 | #endif |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 6002 | #if defined(CONFIG_SDL) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6003 | case DT_SDL: |
| 6004 | sdl_display_init(ds, full_screen, no_frame); |
| 6005 | break; |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 6006 | #elif defined(CONFIG_COCOA) |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6007 | case DT_SDL: |
| 6008 | cocoa_display_init(ds, full_screen); |
| 6009 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6010 | #endif |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6011 | case DT_VNC: |
| 6012 | vnc_display_init(ds); |
| 6013 | if (vnc_display_open(ds, vnc_display) < 0) |
| 6014 | exit(1); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 6015 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6016 | if (show_vnc_port) { |
| 6017 | printf("VNC server running on `%s'\n", vnc_display_local_addr(ds)); |
Anthony Liguori | f92f8af | 2009-05-20 13:01:02 -0500 | [diff] [blame] | 6018 | } |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6019 | break; |
| 6020 | default: |
| 6021 | break; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6022 | } |
aliguori | 7d957bd | 2009-01-15 22:14:11 +0000 | [diff] [blame] | 6023 | dpy_resize(ds); |
aliguori | 5b08fc1 | 2008-08-21 20:08:03 +0000 | [diff] [blame] | 6024 | |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 6025 | dcl = ds->listeners; |
| 6026 | while (dcl != NULL) { |
| 6027 | if (dcl->dpy_refresh != NULL) { |
| 6028 | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); |
| 6029 | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 6030 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 6031 | dcl = dcl->next; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6032 | } |
aliguori | 3023f33 | 2009-01-16 19:04:14 +0000 | [diff] [blame] | 6033 | |
Anthony Liguori | 993fbfd | 2009-05-21 16:54:00 -0500 | [diff] [blame] | 6034 | if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) { |
blueswir1 | 9043b62 | 2009-01-21 19:28:13 +0000 | [diff] [blame] | 6035 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL); |
| 6036 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); |
| 6037 | } |
| 6038 | |
aliguori | 2796dae | 2009-01-16 20:23:27 +0000 | [diff] [blame] | 6039 | text_consoles_set_display(display_state); |
| 6040 | |
Gerd Hoffmann | 8858934 | 2009-12-08 13:11:50 +0100 | [diff] [blame] | 6041 | if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) |
| 6042 | exit(1); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6043 | |
aliguori | 59030a8 | 2009-04-05 18:43:41 +0000 | [diff] [blame] | 6044 | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
| 6045 | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n", |
| 6046 | gdbstub_dev); |
| 6047 | exit(1); |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 6048 | } |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 6049 | |
Gerd Hoffmann | 3418bd2 | 2009-09-25 21:42:41 +0200 | [diff] [blame] | 6050 | qdev_machine_creation_done(); |
| 6051 | |
Gerd Hoffmann | 15ff770 | 2009-12-14 16:07:35 +0100 | [diff] [blame^] | 6052 | if (rom_load_all() != 0) { |
| 6053 | fprintf(stderr, "rom loading failed\n"); |
| 6054 | exit(1); |
| 6055 | } |
Gerd Hoffmann | 45a50b1 | 2009-10-01 16:42:33 +0200 | [diff] [blame] | 6056 | |
Juan Quintela | 504c294 | 2009-11-12 00:39:13 +0100 | [diff] [blame] | 6057 | qemu_system_reset(); |
Juan Quintela | 05f2401 | 2009-08-20 19:42:22 +0200 | [diff] [blame] | 6058 | if (loadvm) { |
| 6059 | if (load_vmstate(cur_mon, loadvm) < 0) { |
| 6060 | autostart = 0; |
| 6061 | } |
| 6062 | } |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6063 | |
Glauber Costa | 2bb8c10 | 2009-07-24 16:20:23 -0400 | [diff] [blame] | 6064 | if (incoming) { |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 6065 | qemu_start_incoming_migration(incoming); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 6066 | } else if (autostart) { |
aliguori | c0f4ce7 | 2009-03-05 23:01:01 +0000 | [diff] [blame] | 6067 | vm_start(); |
Avi Kivity | 6b99dad | 2009-08-09 14:39:20 +0300 | [diff] [blame] | 6068 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 6069 | |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 6070 | #ifndef _WIN32 |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6071 | if (daemonize) { |
| 6072 | uint8_t status = 0; |
| 6073 | ssize_t len; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6074 | |
| 6075 | again1: |
| 6076 | len = write(fds[1], &status, 1); |
| 6077 | if (len == -1 && (errno == EINTR)) |
| 6078 | goto again1; |
| 6079 | |
| 6080 | if (len != 1) |
| 6081 | exit(1); |
| 6082 | |
aliguori | bd54b86 | 2008-07-23 00:58:33 +0000 | [diff] [blame] | 6083 | chdir("/"); |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 6084 | TFR(fd = qemu_open("/dev/null", O_RDWR)); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6085 | if (fd == -1) |
| 6086 | exit(1); |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6087 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6088 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6089 | if (run_as) { |
| 6090 | pwd = getpwnam(run_as); |
| 6091 | if (!pwd) { |
| 6092 | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as); |
| 6093 | exit(1); |
| 6094 | } |
| 6095 | } |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6096 | |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6097 | if (chroot_dir) { |
| 6098 | if (chroot(chroot_dir) < 0) { |
| 6099 | fprintf(stderr, "chroot failed\n"); |
| 6100 | exit(1); |
| 6101 | } |
| 6102 | chdir("/"); |
| 6103 | } |
| 6104 | |
| 6105 | if (run_as) { |
| 6106 | if (setgid(pwd->pw_gid) < 0) { |
| 6107 | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid); |
| 6108 | exit(1); |
| 6109 | } |
| 6110 | if (setuid(pwd->pw_uid) < 0) { |
| 6111 | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid); |
| 6112 | exit(1); |
| 6113 | } |
| 6114 | if (setuid(0) != -1) { |
| 6115 | fprintf(stderr, "Dropping privileges failed\n"); |
| 6116 | exit(1); |
| 6117 | } |
| 6118 | } |
aliguori | 0858532 | 2009-02-27 22:09:45 +0000 | [diff] [blame] | 6119 | |
| 6120 | if (daemonize) { |
| 6121 | dup2(fd, 0); |
| 6122 | dup2(fd, 1); |
| 6123 | dup2(fd, 2); |
| 6124 | |
| 6125 | close(fd); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6126 | } |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 6127 | #endif |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6128 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6129 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 6130 | quit_timers(); |
aliguori | 63a01ef | 2008-10-31 19:10:00 +0000 | [diff] [blame] | 6131 | net_cleanup(); |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 6132 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6133 | return 0; |
| 6134 | } |