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 | */ |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 24 | #include "hw/hw.h" |
| 25 | #include "hw/boards.h" |
| 26 | #include "hw/usb.h" |
| 27 | #include "hw/pcmcia.h" |
| 28 | #include "hw/pc.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 29 | #include "hw/audiodev.h" |
| 30 | #include "hw/isa.h" |
aurel32 | 2e4d9fb | 2008-04-08 06:01:02 +0000 | [diff] [blame] | 31 | #include "hw/baum.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 32 | #include "net.h" |
| 33 | #include "console.h" |
| 34 | #include "sysemu.h" |
| 35 | #include "gdbstub.h" |
| 36 | #include "qemu-timer.h" |
| 37 | #include "qemu-char.h" |
| 38 | #include "block.h" |
| 39 | #include "audio/audio.h" |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 40 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 41 | #include <unistd.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 42 | #include <fcntl.h> |
| 43 | #include <signal.h> |
| 44 | #include <time.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 45 | #include <errno.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 46 | #include <sys/time.h> |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 47 | #include <zlib.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 48 | |
| 49 | #ifndef _WIN32 |
| 50 | #include <sys/times.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 51 | #include <sys/wait.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 52 | #include <termios.h> |
| 53 | #include <sys/poll.h> |
| 54 | #include <sys/mman.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 55 | #include <sys/ioctl.h> |
| 56 | #include <sys/socket.h> |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 57 | #include <netinet/in.h> |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 58 | #include <dirent.h> |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 59 | #include <netdb.h> |
ths | cb4b976 | 2007-09-13 12:39:35 +0000 | [diff] [blame] | 60 | #include <sys/select.h> |
| 61 | #include <arpa/inet.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 62 | #ifdef _BSD |
| 63 | #include <sys/stat.h> |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 64 | #ifndef __APPLE__ |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 65 | #include <libutil.h> |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 66 | #endif |
ths | 5c40d2b | 2007-06-23 16:03:36 +0000 | [diff] [blame] | 67 | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__) |
| 68 | #include <freebsd/stdlib.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 69 | #else |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 70 | #ifndef __sun__ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 71 | #include <linux/if.h> |
| 72 | #include <linux/if_tun.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 73 | #include <pty.h> |
| 74 | #include <malloc.h> |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 75 | #include <linux/rtc.h> |
ths | bd494f4 | 2007-09-16 20:03:23 +0000 | [diff] [blame] | 76 | |
| 77 | /* For the benefit of older linux systems which don't supply it, |
| 78 | we use a local copy of hpet.h. */ |
| 79 | /* #include <linux/hpet.h> */ |
| 80 | #include "hpet.h" |
| 81 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 82 | #include <linux/ppdev.h> |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 83 | #include <linux/parport.h> |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 84 | #else |
| 85 | #include <sys/stat.h> |
| 86 | #include <sys/ethernet.h> |
| 87 | #include <sys/sockio.h> |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 88 | #include <netinet/arp.h> |
| 89 | #include <netinet/in.h> |
| 90 | #include <netinet/in_systm.h> |
| 91 | #include <netinet/ip.h> |
| 92 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 93 | #include <netinet/udp.h> |
| 94 | #include <netinet/tcp.h> |
| 95 | #include <net/if.h> |
| 96 | #include <syslog.h> |
| 97 | #include <stropts.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 98 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 99 | #endif |
ths | cb4b976 | 2007-09-13 12:39:35 +0000 | [diff] [blame] | 100 | #else |
| 101 | #include <winsock2.h> |
| 102 | int inet_aton(const char *cp, struct in_addr *ia); |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 103 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 104 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 105 | #if defined(CONFIG_SLIRP) |
| 106 | #include "libslirp.h" |
| 107 | #endif |
| 108 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 109 | #ifdef _WIN32 |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 110 | #include <malloc.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 111 | #include <sys/timeb.h> |
ths | 4fddf62 | 2007-12-17 04:42:29 +0000 | [diff] [blame] | 112 | #include <mmsystem.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 113 | #define getopt_long_only getopt_long |
| 114 | #define memalign(align, size) malloc(size) |
| 115 | #endif |
| 116 | |
bellard | 6ca957f | 2006-04-30 22:53:25 +0000 | [diff] [blame] | 117 | #include "qemu_socket.h" |
| 118 | |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 119 | #ifdef CONFIG_SDL |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 120 | #ifdef __APPLE__ |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 121 | #include <SDL/SDL.h> |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 122 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 123 | #endif /* CONFIG_SDL */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 124 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 125 | #ifdef CONFIG_COCOA |
| 126 | #undef main |
| 127 | #define main qemu_main |
| 128 | #endif /* CONFIG_COCOA */ |
| 129 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 130 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 131 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 132 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 133 | |
bellard | 5a67135 | 2003-10-01 00:13:48 +0000 | [diff] [blame] | 134 | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 135 | #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" |
pbrook | a14d6c8 | 2006-12-23 15:37:33 +0000 | [diff] [blame] | 136 | #ifdef __sun__ |
| 137 | #define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
| 138 | #else |
| 139 | #define SMBD_COMMAND "/usr/sbin/smbd" |
| 140 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 141 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 142 | //#define DEBUG_UNUSED_IOPORT |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 143 | //#define DEBUG_IOPORT |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 144 | |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 145 | #ifdef TARGET_PPC |
| 146 | #define DEFAULT_RAM_SIZE 144 |
| 147 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 148 | #define DEFAULT_RAM_SIZE 128 |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 149 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 150 | /* in ms */ |
| 151 | #define GUI_REFRESH_INTERVAL 30 |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 152 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 153 | /* Max number of USB devices that can be specified on the commandline. */ |
| 154 | #define MAX_USB_CMDLINE 8 |
| 155 | |
bellard | 7dea1da | 2003-11-16 15:59:30 +0000 | [diff] [blame] | 156 | /* XXX: use a two level table to limit memory usage */ |
| 157 | #define MAX_IOPORTS 65536 |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 158 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 159 | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 160 | const char *bios_name = NULL; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 161 | void *ioport_opaque[MAX_IOPORTS]; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 162 | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
| 163 | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 164 | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 165 | to store the VM snapshots */ |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 166 | DriveInfo drives_table[MAX_DRIVES+1]; |
| 167 | int nb_drives; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 168 | /* point to the block driver where the snapshots are managed */ |
| 169 | BlockDriverState *bs_snapshots; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 170 | int vga_ram_size; |
| 171 | static DisplayState display_state; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 172 | int nographic; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 173 | int curses; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 174 | const char* keyboard_layout = NULL; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 175 | int64_t ticks_per_sec; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 176 | ram_addr_t ram_size; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 177 | int pit_min_timer_count = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 178 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 179 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 180 | int vm_running; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 181 | static int rtc_utc = 1; |
| 182 | static int rtc_date_offset = -1; /* -1 means no change */ |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 183 | int cirrus_vga_enabled = 1; |
ths | d34cab9 | 2007-04-02 01:10:46 +0000 | [diff] [blame] | 184 | int vmsvga_enabled = 0; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 185 | #ifdef TARGET_SPARC |
| 186 | int graphic_width = 1024; |
| 187 | int graphic_height = 768; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 188 | int graphic_depth = 8; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 189 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 190 | int graphic_width = 800; |
| 191 | int graphic_height = 600; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 192 | int graphic_depth = 15; |
blueswir1 | eee0b83 | 2007-04-21 19:45:49 +0000 | [diff] [blame] | 193 | #endif |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 194 | int full_screen = 0; |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 195 | int no_frame = 0; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 196 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 197 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 198 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 199 | #ifdef TARGET_I386 |
| 200 | int win2k_install_hack = 0; |
| 201 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 202 | int usb_enabled = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 203 | static VLANState *first_vlan; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 204 | int smp_cpus = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 205 | const char *vnc_display; |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 206 | #if defined(TARGET_SPARC) |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 207 | #define MAX_CPUS 16 |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 208 | #elif defined(TARGET_I386) |
| 209 | #define MAX_CPUS 255 |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 210 | #else |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 211 | #define MAX_CPUS 1 |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 212 | #endif |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 213 | int acpi_enabled = 1; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 214 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 215 | int no_reboot = 0; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 216 | int no_shutdown = 0; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 217 | int cursor_hide = 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 218 | int graphic_rotate = 0; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 219 | int daemonize = 0; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 220 | const char *option_rom[MAX_OPTION_ROMS]; |
| 221 | int nb_option_roms; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 222 | int semihosting_enabled = 0; |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 223 | int autostart = 1; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 224 | #ifdef TARGET_ARM |
| 225 | int old_param = 0; |
| 226 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 227 | const char *qemu_name; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 228 | int alt_grab = 0; |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 229 | #ifdef TARGET_SPARC |
| 230 | unsigned int nb_prom_envs = 0; |
| 231 | const char *prom_envs[MAX_PROM_ENVS]; |
| 232 | #endif |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 233 | int nb_drives_opt; |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 234 | struct drive_opt { |
| 235 | const char *file; |
| 236 | char opt[1024]; |
| 237 | } drives_opt[MAX_DRIVES]; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 238 | |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 239 | static CPUState *cur_cpu; |
| 240 | static CPUState *next_cpu; |
balrog | 76ea08f | 2007-12-16 11:48:54 +0000 | [diff] [blame] | 241 | static int event_pending = 1; |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 242 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 243 | #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR) |
| 244 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 245 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 246 | /* x86 ISA bus support */ |
| 247 | |
| 248 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 249 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 250 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 251 | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 252 | { |
| 253 | #ifdef DEBUG_UNUSED_IOPORT |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 254 | fprintf(stderr, "unused inb: port=0x%04x\n", address); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 255 | #endif |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 256 | return 0xff; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 257 | } |
| 258 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 259 | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 260 | { |
| 261 | #ifdef DEBUG_UNUSED_IOPORT |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 262 | fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 263 | #endif |
| 264 | } |
| 265 | |
| 266 | /* default is to make two byte accesses */ |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 267 | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 268 | { |
| 269 | uint32_t data; |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 270 | data = ioport_read_table[0][address](ioport_opaque[address], address); |
| 271 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 272 | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 273 | return data; |
| 274 | } |
| 275 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 276 | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 277 | { |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 278 | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
| 279 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 280 | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 281 | } |
| 282 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 283 | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 284 | { |
| 285 | #ifdef DEBUG_UNUSED_IOPORT |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 286 | fprintf(stderr, "unused inl: port=0x%04x\n", address); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 287 | #endif |
| 288 | return 0xffffffff; |
| 289 | } |
| 290 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 291 | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 292 | { |
| 293 | #ifdef DEBUG_UNUSED_IOPORT |
ths | 1196be3 | 2007-03-17 15:17:58 +0000 | [diff] [blame] | 294 | fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 295 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 296 | } |
| 297 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 298 | static void init_ioports(void) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 299 | { |
| 300 | int i; |
| 301 | |
| 302 | for(i = 0; i < MAX_IOPORTS; i++) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 303 | ioport_read_table[0][i] = default_ioport_readb; |
| 304 | ioport_write_table[0][i] = default_ioport_writeb; |
| 305 | ioport_read_table[1][i] = default_ioport_readw; |
| 306 | ioport_write_table[1][i] = default_ioport_writew; |
| 307 | ioport_read_table[2][i] = default_ioport_readl; |
| 308 | ioport_write_table[2][i] = default_ioport_writel; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 312 | /* size is the word size in byte */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 313 | int register_ioport_read(int start, int length, int size, |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 314 | IOPortReadFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 315 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 316 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 317 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 318 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 319 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 320 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 321 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 322 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 323 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 324 | } else { |
balrog | 88fdf56 | 2008-04-26 21:11:22 +0000 | [diff] [blame] | 325 | hw_error("register_ioport_read: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 326 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 327 | } |
| 328 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 329 | ioport_read_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 330 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
balrog | 88fdf56 | 2008-04-26 21:11:22 +0000 | [diff] [blame] | 331 | hw_error("register_ioport_read: invalid opaque"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 332 | ioport_opaque[i] = opaque; |
| 333 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 337 | /* size is the word size in byte */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 338 | int register_ioport_write(int start, int length, int size, |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 339 | IOPortWriteFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 340 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 341 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 342 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 343 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 344 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 345 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 346 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 347 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 348 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 349 | } else { |
balrog | 88fdf56 | 2008-04-26 21:11:22 +0000 | [diff] [blame] | 350 | hw_error("register_ioport_write: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 351 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 352 | } |
| 353 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 354 | ioport_write_table[bsize][i] = func; |
balrog | 88fdf56 | 2008-04-26 21:11:22 +0000 | [diff] [blame] | 355 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
| 356 | hw_error("register_ioport_write: invalid opaque"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 357 | ioport_opaque[i] = opaque; |
| 358 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
bellard | 69b9103 | 2004-05-18 23:05:28 +0000 | [diff] [blame] | 362 | void isa_unassign_ioport(int start, int length) |
| 363 | { |
| 364 | int i; |
| 365 | |
| 366 | for(i = start; i < start + length; i++) { |
| 367 | ioport_read_table[0][i] = default_ioport_readb; |
| 368 | ioport_read_table[1][i] = default_ioport_readw; |
| 369 | ioport_read_table[2][i] = default_ioport_readl; |
| 370 | |
| 371 | ioport_write_table[0][i] = default_ioport_writeb; |
| 372 | ioport_write_table[1][i] = default_ioport_writew; |
| 373 | ioport_write_table[2][i] = default_ioport_writel; |
| 374 | } |
| 375 | } |
| 376 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 377 | /***********************************************************/ |
| 378 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 379 | void cpu_outb(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 380 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 381 | #ifdef DEBUG_IOPORT |
| 382 | if (loglevel & CPU_LOG_IOPORT) |
| 383 | fprintf(logfile, "outb: %04x %02x\n", addr, val); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 384 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 385 | ioport_write_table[0][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 386 | #ifdef USE_KQEMU |
| 387 | if (env) |
| 388 | env->last_io_time = cpu_get_time_fast(); |
| 389 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 390 | } |
| 391 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 392 | void cpu_outw(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 393 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 394 | #ifdef DEBUG_IOPORT |
| 395 | if (loglevel & CPU_LOG_IOPORT) |
| 396 | fprintf(logfile, "outw: %04x %04x\n", addr, val); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 397 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 398 | ioport_write_table[1][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 399 | #ifdef USE_KQEMU |
| 400 | if (env) |
| 401 | env->last_io_time = cpu_get_time_fast(); |
| 402 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 403 | } |
| 404 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 405 | void cpu_outl(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 406 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 407 | #ifdef DEBUG_IOPORT |
| 408 | if (loglevel & CPU_LOG_IOPORT) |
| 409 | fprintf(logfile, "outl: %04x %08x\n", addr, val); |
| 410 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 411 | ioport_write_table[2][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 412 | #ifdef USE_KQEMU |
| 413 | if (env) |
| 414 | env->last_io_time = cpu_get_time_fast(); |
| 415 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 416 | } |
| 417 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 418 | int cpu_inb(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 419 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 420 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 421 | val = ioport_read_table[0][addr](ioport_opaque[addr], addr); |
| 422 | #ifdef DEBUG_IOPORT |
| 423 | if (loglevel & CPU_LOG_IOPORT) |
| 424 | fprintf(logfile, "inb : %04x %02x\n", addr, val); |
| 425 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 426 | #ifdef USE_KQEMU |
| 427 | if (env) |
| 428 | env->last_io_time = cpu_get_time_fast(); |
| 429 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 430 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 431 | } |
| 432 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 433 | int cpu_inw(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 434 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 435 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 436 | val = ioport_read_table[1][addr](ioport_opaque[addr], addr); |
| 437 | #ifdef DEBUG_IOPORT |
| 438 | if (loglevel & CPU_LOG_IOPORT) |
| 439 | fprintf(logfile, "inw : %04x %04x\n", addr, val); |
| 440 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 441 | #ifdef USE_KQEMU |
| 442 | if (env) |
| 443 | env->last_io_time = cpu_get_time_fast(); |
| 444 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 445 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 446 | } |
| 447 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 448 | int cpu_inl(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 449 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 450 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 451 | val = ioport_read_table[2][addr](ioport_opaque[addr], addr); |
| 452 | #ifdef DEBUG_IOPORT |
| 453 | if (loglevel & CPU_LOG_IOPORT) |
| 454 | fprintf(logfile, "inl : %04x %08x\n", addr, val); |
| 455 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 456 | #ifdef USE_KQEMU |
| 457 | if (env) |
| 458 | env->last_io_time = cpu_get_time_fast(); |
| 459 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 460 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 464 | void hw_error(const char *fmt, ...) |
| 465 | { |
| 466 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 467 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 468 | |
| 469 | va_start(ap, fmt); |
| 470 | fprintf(stderr, "qemu: hardware error: "); |
| 471 | vfprintf(stderr, fmt, ap); |
| 472 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 473 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 474 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 475 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 476 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 477 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 478 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 479 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 480 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 481 | va_end(ap); |
| 482 | abort(); |
| 483 | } |
| 484 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 485 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 486 | /* keyboard/mouse */ |
| 487 | |
| 488 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 489 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 490 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 491 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 492 | |
| 493 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 494 | { |
| 495 | qemu_put_kbd_event_opaque = opaque; |
| 496 | qemu_put_kbd_event = func; |
| 497 | } |
| 498 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 499 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 500 | void *opaque, int absolute, |
| 501 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 502 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 503 | QEMUPutMouseEntry *s, *cursor; |
| 504 | |
| 505 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
| 506 | if (!s) |
| 507 | return NULL; |
| 508 | |
| 509 | s->qemu_put_mouse_event = func; |
| 510 | s->qemu_put_mouse_event_opaque = opaque; |
| 511 | s->qemu_put_mouse_event_absolute = absolute; |
| 512 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 513 | s->next = NULL; |
| 514 | |
| 515 | if (!qemu_put_mouse_event_head) { |
| 516 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 517 | return s; |
| 518 | } |
| 519 | |
| 520 | cursor = qemu_put_mouse_event_head; |
| 521 | while (cursor->next != NULL) |
| 522 | cursor = cursor->next; |
| 523 | |
| 524 | cursor->next = s; |
| 525 | qemu_put_mouse_event_current = s; |
| 526 | |
| 527 | return s; |
| 528 | } |
| 529 | |
| 530 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 531 | { |
| 532 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 533 | |
| 534 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 535 | return; |
| 536 | |
| 537 | cursor = qemu_put_mouse_event_head; |
| 538 | while (cursor != NULL && cursor != entry) { |
| 539 | prev = cursor; |
| 540 | cursor = cursor->next; |
| 541 | } |
| 542 | |
| 543 | if (cursor == NULL) // does not exist or list empty |
| 544 | return; |
| 545 | else if (prev == NULL) { // entry is head |
| 546 | qemu_put_mouse_event_head = cursor->next; |
| 547 | if (qemu_put_mouse_event_current == entry) |
| 548 | qemu_put_mouse_event_current = cursor->next; |
| 549 | qemu_free(entry->qemu_put_mouse_event_name); |
| 550 | qemu_free(entry); |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | prev->next = entry->next; |
| 555 | |
| 556 | if (qemu_put_mouse_event_current == entry) |
| 557 | qemu_put_mouse_event_current = prev; |
| 558 | |
| 559 | qemu_free(entry->qemu_put_mouse_event_name); |
| 560 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | void kbd_put_keycode(int keycode) |
| 564 | { |
| 565 | if (qemu_put_kbd_event) { |
| 566 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 571 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 572 | QEMUPutMouseEvent *mouse_event; |
| 573 | void *mouse_event_opaque; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 574 | int width; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 575 | |
| 576 | if (!qemu_put_mouse_event_current) { |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | mouse_event = |
| 581 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 582 | mouse_event_opaque = |
| 583 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 584 | |
| 585 | if (mouse_event) { |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 586 | if (graphic_rotate) { |
| 587 | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute) |
| 588 | width = 0x7fff; |
| 589 | else |
aurel32 | b94ed57 | 2008-03-10 19:34:27 +0000 | [diff] [blame] | 590 | width = graphic_width - 1; |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 591 | mouse_event(mouse_event_opaque, |
| 592 | width - dy, dx, dz, buttons_state); |
| 593 | } else |
| 594 | mouse_event(mouse_event_opaque, |
| 595 | dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 599 | int kbd_mouse_is_absolute(void) |
| 600 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 601 | if (!qemu_put_mouse_event_current) |
| 602 | return 0; |
| 603 | |
| 604 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 605 | } |
| 606 | |
| 607 | void do_info_mice(void) |
| 608 | { |
| 609 | QEMUPutMouseEntry *cursor; |
| 610 | int index = 0; |
| 611 | |
| 612 | if (!qemu_put_mouse_event_head) { |
| 613 | term_printf("No mouse devices connected\n"); |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | term_printf("Mouse devices available:\n"); |
| 618 | cursor = qemu_put_mouse_event_head; |
| 619 | while (cursor != NULL) { |
| 620 | term_printf("%c Mouse #%d: %s\n", |
| 621 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
| 622 | index, cursor->qemu_put_mouse_event_name); |
| 623 | index++; |
| 624 | cursor = cursor->next; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | void do_mouse_set(int index) |
| 629 | { |
| 630 | QEMUPutMouseEntry *cursor; |
| 631 | int i = 0; |
| 632 | |
| 633 | if (!qemu_put_mouse_event_head) { |
| 634 | term_printf("No mouse devices connected\n"); |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | cursor = qemu_put_mouse_event_head; |
| 639 | while (cursor != NULL && index != i) { |
| 640 | i++; |
| 641 | cursor = cursor->next; |
| 642 | } |
| 643 | |
| 644 | if (cursor != NULL) |
| 645 | qemu_put_mouse_event_current = cursor; |
| 646 | else |
| 647 | term_printf("Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 648 | } |
| 649 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 650 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 651 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 652 | { |
| 653 | union { |
| 654 | uint64_t ll; |
| 655 | struct { |
| 656 | #ifdef WORDS_BIGENDIAN |
| 657 | uint32_t high, low; |
| 658 | #else |
| 659 | uint32_t low, high; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 660 | #endif |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 661 | } l; |
| 662 | } u, res; |
| 663 | uint64_t rl, rh; |
| 664 | |
| 665 | u.ll = a; |
| 666 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 667 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 668 | rh += (rl >> 32); |
| 669 | res.l.high = rh / c; |
| 670 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 671 | return res.ll; |
| 672 | } |
| 673 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 674 | /***********************************************************/ |
| 675 | /* real time host monotonic timer */ |
| 676 | |
| 677 | #define QEMU_TIMER_BASE 1000000000LL |
| 678 | |
| 679 | #ifdef WIN32 |
| 680 | |
| 681 | static int64_t clock_freq; |
| 682 | |
| 683 | static void init_get_clock(void) |
| 684 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 685 | LARGE_INTEGER freq; |
| 686 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 687 | ret = QueryPerformanceFrequency(&freq); |
| 688 | if (ret == 0) { |
| 689 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 690 | exit(1); |
| 691 | } |
| 692 | clock_freq = freq.QuadPart; |
| 693 | } |
| 694 | |
| 695 | static int64_t get_clock(void) |
| 696 | { |
| 697 | LARGE_INTEGER ti; |
| 698 | QueryPerformanceCounter(&ti); |
| 699 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); |
| 700 | } |
| 701 | |
| 702 | #else |
| 703 | |
| 704 | static int use_rt_clock; |
| 705 | |
| 706 | static void init_get_clock(void) |
| 707 | { |
| 708 | use_rt_clock = 0; |
| 709 | #if defined(__linux__) |
| 710 | { |
| 711 | struct timespec ts; |
| 712 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 713 | use_rt_clock = 1; |
| 714 | } |
| 715 | } |
| 716 | #endif |
| 717 | } |
| 718 | |
| 719 | static int64_t get_clock(void) |
| 720 | { |
| 721 | #if defined(__linux__) |
| 722 | if (use_rt_clock) { |
| 723 | struct timespec ts; |
| 724 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 725 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 726 | } else |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 727 | #endif |
| 728 | { |
| 729 | /* XXX: using gettimeofday leads to problems if the date |
| 730 | changes, so it should be avoided. */ |
| 731 | struct timeval tv; |
| 732 | gettimeofday(&tv, NULL); |
| 733 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | #endif |
| 738 | |
| 739 | /***********************************************************/ |
| 740 | /* guest cycle counter */ |
| 741 | |
| 742 | static int64_t cpu_ticks_prev; |
| 743 | static int64_t cpu_ticks_offset; |
| 744 | static int64_t cpu_clock_offset; |
| 745 | static int cpu_ticks_enabled; |
| 746 | |
| 747 | /* return the host CPU cycle counter and handle stop/restart */ |
| 748 | int64_t cpu_get_ticks(void) |
| 749 | { |
| 750 | if (!cpu_ticks_enabled) { |
| 751 | return cpu_ticks_offset; |
| 752 | } else { |
| 753 | int64_t ticks; |
| 754 | ticks = cpu_get_real_ticks(); |
| 755 | if (cpu_ticks_prev > ticks) { |
| 756 | /* Note: non increasing ticks may happen if the host uses |
| 757 | software suspend */ |
| 758 | cpu_ticks_offset += cpu_ticks_prev - ticks; |
| 759 | } |
| 760 | cpu_ticks_prev = ticks; |
| 761 | return ticks + cpu_ticks_offset; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 766 | static int64_t cpu_get_clock(void) |
| 767 | { |
| 768 | int64_t ti; |
| 769 | if (!cpu_ticks_enabled) { |
| 770 | return cpu_clock_offset; |
| 771 | } else { |
| 772 | ti = get_clock(); |
| 773 | return ti + cpu_clock_offset; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | /* enable cpu_get_ticks() */ |
| 778 | void cpu_enable_ticks(void) |
| 779 | { |
| 780 | if (!cpu_ticks_enabled) { |
| 781 | cpu_ticks_offset -= cpu_get_real_ticks(); |
| 782 | cpu_clock_offset -= get_clock(); |
| 783 | cpu_ticks_enabled = 1; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 788 | cpu_get_ticks() after that. */ |
| 789 | void cpu_disable_ticks(void) |
| 790 | { |
| 791 | if (cpu_ticks_enabled) { |
| 792 | cpu_ticks_offset = cpu_get_ticks(); |
| 793 | cpu_clock_offset = cpu_get_clock(); |
| 794 | cpu_ticks_enabled = 0; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /***********************************************************/ |
| 799 | /* timers */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 800 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 801 | #define QEMU_TIMER_REALTIME 0 |
| 802 | #define QEMU_TIMER_VIRTUAL 1 |
| 803 | |
| 804 | struct QEMUClock { |
| 805 | int type; |
| 806 | /* XXX: add frequency */ |
| 807 | }; |
| 808 | |
| 809 | struct QEMUTimer { |
| 810 | QEMUClock *clock; |
| 811 | int64_t expire_time; |
| 812 | QEMUTimerCB *cb; |
| 813 | void *opaque; |
| 814 | struct QEMUTimer *next; |
| 815 | }; |
| 816 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 817 | struct qemu_alarm_timer { |
| 818 | char const *name; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 819 | unsigned int flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 820 | |
| 821 | int (*start)(struct qemu_alarm_timer *t); |
| 822 | void (*stop)(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 823 | void (*rearm)(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 824 | void *priv; |
| 825 | }; |
| 826 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 827 | #define ALARM_FLAG_DYNTICKS 0x1 |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 828 | #define ALARM_FLAG_EXPIRED 0x2 |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 829 | |
| 830 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
| 831 | { |
| 832 | return t->flags & ALARM_FLAG_DYNTICKS; |
| 833 | } |
| 834 | |
| 835 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
| 836 | { |
| 837 | if (!alarm_has_dynticks(t)) |
| 838 | return; |
| 839 | |
| 840 | t->rearm(t); |
| 841 | } |
| 842 | |
| 843 | /* TODO: MIN_TIMER_REARM_US should be optimized */ |
| 844 | #define MIN_TIMER_REARM_US 250 |
| 845 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 846 | static struct qemu_alarm_timer *alarm_timer; |
| 847 | |
| 848 | #ifdef _WIN32 |
| 849 | |
| 850 | struct qemu_alarm_win32 { |
| 851 | MMRESULT timerId; |
| 852 | HANDLE host_alarm; |
| 853 | unsigned int period; |
| 854 | } alarm_win32_data = {0, NULL, -1}; |
| 855 | |
| 856 | static int win32_start_timer(struct qemu_alarm_timer *t); |
| 857 | static void win32_stop_timer(struct qemu_alarm_timer *t); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 858 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 859 | |
| 860 | #else |
| 861 | |
| 862 | static int unix_start_timer(struct qemu_alarm_timer *t); |
| 863 | static void unix_stop_timer(struct qemu_alarm_timer *t); |
| 864 | |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 865 | #ifdef __linux__ |
| 866 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 867 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
| 868 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
| 869 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
| 870 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 871 | static int hpet_start_timer(struct qemu_alarm_timer *t); |
| 872 | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
| 873 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 874 | static int rtc_start_timer(struct qemu_alarm_timer *t); |
| 875 | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
| 876 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 877 | #endif /* __linux__ */ |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 878 | |
| 879 | #endif /* _WIN32 */ |
| 880 | |
| 881 | static struct qemu_alarm_timer alarm_timers[] = { |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 882 | #ifndef _WIN32 |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 883 | #ifdef __linux__ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 884 | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer, |
| 885 | dynticks_stop_timer, dynticks_rearm_timer, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 886 | /* HPET - if available - is preferred */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 887 | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 888 | /* ...otherwise try RTC */ |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 889 | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 890 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 891 | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 892 | #else |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 893 | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer, |
| 894 | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
| 895 | {"win32", 0, win32_start_timer, |
| 896 | win32_stop_timer, NULL, &alarm_win32_data}, |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 897 | #endif |
| 898 | {NULL, } |
| 899 | }; |
| 900 | |
blueswir1 | 3f47aa8 | 2008-03-09 06:59:01 +0000 | [diff] [blame] | 901 | static void show_available_alarms(void) |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 902 | { |
| 903 | int i; |
| 904 | |
| 905 | printf("Available alarm timers, in order of precedence:\n"); |
| 906 | for (i = 0; alarm_timers[i].name; i++) |
| 907 | printf("%s\n", alarm_timers[i].name); |
| 908 | } |
| 909 | |
| 910 | static void configure_alarms(char const *opt) |
| 911 | { |
| 912 | int i; |
| 913 | int cur = 0; |
| 914 | int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1; |
| 915 | char *arg; |
| 916 | char *name; |
| 917 | |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 918 | if (!strcmp(opt, "?")) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 919 | show_available_alarms(); |
| 920 | exit(0); |
| 921 | } |
| 922 | |
| 923 | arg = strdup(opt); |
| 924 | |
| 925 | /* Reorder the array */ |
| 926 | name = strtok(arg, ","); |
| 927 | while (name) { |
| 928 | struct qemu_alarm_timer tmp; |
| 929 | |
balrog | e2b577e | 2007-09-17 21:25:20 +0000 | [diff] [blame] | 930 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 931 | if (!strcmp(alarm_timers[i].name, name)) |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | if (i == count) { |
| 936 | fprintf(stderr, "Unknown clock %s\n", name); |
| 937 | goto next; |
| 938 | } |
| 939 | |
| 940 | if (i < cur) |
| 941 | /* Ignore */ |
| 942 | goto next; |
| 943 | |
| 944 | /* Swap */ |
| 945 | tmp = alarm_timers[i]; |
| 946 | alarm_timers[i] = alarm_timers[cur]; |
| 947 | alarm_timers[cur] = tmp; |
| 948 | |
| 949 | cur++; |
| 950 | next: |
| 951 | name = strtok(NULL, ","); |
| 952 | } |
| 953 | |
| 954 | free(arg); |
| 955 | |
| 956 | if (cur) { |
| 957 | /* Disable remaining timers */ |
| 958 | for (i = cur; i < count; i++) |
| 959 | alarm_timers[i].name = NULL; |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 960 | } else { |
| 961 | show_available_alarms(); |
| 962 | exit(1); |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 963 | } |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 964 | } |
| 965 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 966 | QEMUClock *rt_clock; |
| 967 | QEMUClock *vm_clock; |
| 968 | |
| 969 | static QEMUTimer *active_timers[2]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 970 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 971 | static QEMUClock *qemu_new_clock(int type) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 972 | { |
| 973 | QEMUClock *clock; |
| 974 | clock = qemu_mallocz(sizeof(QEMUClock)); |
| 975 | if (!clock) |
| 976 | return NULL; |
| 977 | clock->type = type; |
| 978 | return clock; |
| 979 | } |
| 980 | |
| 981 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 982 | { |
| 983 | QEMUTimer *ts; |
| 984 | |
| 985 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 986 | ts->clock = clock; |
| 987 | ts->cb = cb; |
| 988 | ts->opaque = opaque; |
| 989 | return ts; |
| 990 | } |
| 991 | |
| 992 | void qemu_free_timer(QEMUTimer *ts) |
| 993 | { |
| 994 | qemu_free(ts); |
| 995 | } |
| 996 | |
| 997 | /* stop a timer, but do not dealloc it */ |
| 998 | void qemu_del_timer(QEMUTimer *ts) |
| 999 | { |
| 1000 | QEMUTimer **pt, *t; |
| 1001 | |
| 1002 | /* NOTE: this code must be signal safe because |
| 1003 | qemu_timer_expired() can be called from a signal. */ |
| 1004 | pt = &active_timers[ts->clock->type]; |
| 1005 | for(;;) { |
| 1006 | t = *pt; |
| 1007 | if (!t) |
| 1008 | break; |
| 1009 | if (t == ts) { |
| 1010 | *pt = t->next; |
| 1011 | break; |
| 1012 | } |
| 1013 | pt = &t->next; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /* modify the current timer so that it will be fired when current_time |
| 1018 | >= expire_time. The corresponding callback will be called. */ |
| 1019 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 1020 | { |
| 1021 | QEMUTimer **pt, *t; |
| 1022 | |
| 1023 | qemu_del_timer(ts); |
| 1024 | |
| 1025 | /* add the timer in the sorted list */ |
| 1026 | /* NOTE: this code must be signal safe because |
| 1027 | qemu_timer_expired() can be called from a signal. */ |
| 1028 | pt = &active_timers[ts->clock->type]; |
| 1029 | for(;;) { |
| 1030 | t = *pt; |
| 1031 | if (!t) |
| 1032 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1033 | if (t->expire_time > expire_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1034 | break; |
| 1035 | pt = &t->next; |
| 1036 | } |
| 1037 | ts->expire_time = expire_time; |
| 1038 | ts->next = *pt; |
| 1039 | *pt = ts; |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* Rearm if necessary */ |
| 1042 | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0 && |
| 1043 | pt == &active_timers[ts->clock->type]) |
| 1044 | qemu_rearm_alarm_timer(alarm_timer); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | int qemu_timer_pending(QEMUTimer *ts) |
| 1048 | { |
| 1049 | QEMUTimer *t; |
| 1050 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 1051 | if (t == ts) |
| 1052 | return 1; |
| 1053 | } |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
| 1058 | { |
| 1059 | if (!timer_head) |
| 1060 | return 0; |
| 1061 | return (timer_head->expire_time <= current_time); |
| 1062 | } |
| 1063 | |
| 1064 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 1065 | { |
| 1066 | QEMUTimer *ts; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1067 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1068 | for(;;) { |
| 1069 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 1070 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1071 | break; |
| 1072 | /* remove timer from the list before calling the callback */ |
| 1073 | *ptimer_head = ts->next; |
| 1074 | ts->next = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1075 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1076 | /* run the callback (the timer list can be modified) */ |
| 1077 | ts->cb(ts->opaque); |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | int64_t qemu_get_clock(QEMUClock *clock) |
| 1082 | { |
| 1083 | switch(clock->type) { |
| 1084 | case QEMU_TIMER_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1085 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1086 | default: |
| 1087 | case QEMU_TIMER_VIRTUAL: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1088 | return cpu_get_clock(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1092 | static void init_timers(void) |
| 1093 | { |
| 1094 | init_get_clock(); |
| 1095 | ticks_per_sec = QEMU_TIMER_BASE; |
| 1096 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
| 1097 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
| 1098 | } |
| 1099 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1100 | /* save a timer */ |
| 1101 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 1102 | { |
| 1103 | uint64_t expire_time; |
| 1104 | |
| 1105 | if (qemu_timer_pending(ts)) { |
| 1106 | expire_time = ts->expire_time; |
| 1107 | } else { |
| 1108 | expire_time = -1; |
| 1109 | } |
| 1110 | qemu_put_be64(f, expire_time); |
| 1111 | } |
| 1112 | |
| 1113 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 1114 | { |
| 1115 | uint64_t expire_time; |
| 1116 | |
| 1117 | expire_time = qemu_get_be64(f); |
| 1118 | if (expire_time != -1) { |
| 1119 | qemu_mod_timer(ts, expire_time); |
| 1120 | } else { |
| 1121 | qemu_del_timer(ts); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | static void timer_save(QEMUFile *f, void *opaque) |
| 1126 | { |
| 1127 | if (cpu_ticks_enabled) { |
| 1128 | hw_error("cannot save state if virtual timers are running"); |
| 1129 | } |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1130 | qemu_put_be64(f, cpu_ticks_offset); |
| 1131 | qemu_put_be64(f, ticks_per_sec); |
| 1132 | qemu_put_be64(f, cpu_clock_offset); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
| 1136 | { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1137 | if (version_id != 1 && version_id != 2) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1138 | return -EINVAL; |
| 1139 | if (cpu_ticks_enabled) { |
| 1140 | return -EINVAL; |
| 1141 | } |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1142 | cpu_ticks_offset=qemu_get_be64(f); |
| 1143 | ticks_per_sec=qemu_get_be64(f); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1144 | if (version_id == 2) { |
ths | bee8d68 | 2007-12-16 23:41:11 +0000 | [diff] [blame] | 1145 | cpu_clock_offset=qemu_get_be64(f); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 1146 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1147 | return 0; |
| 1148 | } |
| 1149 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1150 | #ifdef _WIN32 |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1151 | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1152 | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
| 1153 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1154 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1155 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1156 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1157 | #if 0 |
| 1158 | #define DISP_FREQ 1000 |
| 1159 | { |
| 1160 | static int64_t delta_min = INT64_MAX; |
| 1161 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 1162 | static int count; |
| 1163 | ti = qemu_get_clock(vm_clock); |
| 1164 | if (last_clock != 0) { |
| 1165 | delta = ti - last_clock; |
| 1166 | if (delta < delta_min) |
| 1167 | delta_min = delta; |
| 1168 | if (delta > delta_max) |
| 1169 | delta_max = delta; |
| 1170 | delta_cum += delta; |
| 1171 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 1172 | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n", |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 1173 | muldiv64(delta_min, 1000000, ticks_per_sec), |
| 1174 | muldiv64(delta_max, 1000000, ticks_per_sec), |
| 1175 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), |
| 1176 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); |
| 1177 | count = 0; |
| 1178 | delta_min = INT64_MAX; |
| 1179 | delta_max = 0; |
| 1180 | delta_cum = 0; |
| 1181 | } |
| 1182 | } |
| 1183 | last_clock = ti; |
| 1184 | } |
| 1185 | #endif |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1186 | if (alarm_has_dynticks(alarm_timer) || |
| 1187 | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1188 | qemu_get_clock(vm_clock)) || |
| 1189 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
| 1190 | qemu_get_clock(rt_clock))) { |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1191 | #ifdef _WIN32 |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1192 | struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv; |
| 1193 | SetEvent(data->host_alarm); |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1194 | #endif |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 1195 | CPUState *env = next_cpu; |
| 1196 | |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1197 | alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
| 1198 | |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1199 | if (env) { |
| 1200 | /* stop the currently executing cpu because a timer occured */ |
| 1201 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 1202 | #ifdef USE_KQEMU |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1203 | if (env->kqemu_enabled) { |
| 1204 | kqemu_cpu_interrupt(env); |
| 1205 | } |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 1206 | #endif |
balrog | 4f8eb8d | 2007-12-16 12:39:38 +0000 | [diff] [blame] | 1207 | } |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 1208 | event_pending = 1; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1212 | static uint64_t qemu_next_deadline(void) |
| 1213 | { |
balrog | 49a9b72 | 2007-10-04 19:47:09 +0000 | [diff] [blame] | 1214 | int64_t nearest_delta_us = INT64_MAX; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1215 | int64_t vmdelta_us; |
| 1216 | |
| 1217 | if (active_timers[QEMU_TIMER_REALTIME]) |
| 1218 | nearest_delta_us = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
| 1219 | qemu_get_clock(rt_clock))*1000; |
| 1220 | |
| 1221 | if (active_timers[QEMU_TIMER_VIRTUAL]) { |
| 1222 | /* round up */ |
| 1223 | vmdelta_us = (active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
| 1224 | qemu_get_clock(vm_clock)+999)/1000; |
| 1225 | if (vmdelta_us < nearest_delta_us) |
| 1226 | nearest_delta_us = vmdelta_us; |
| 1227 | } |
| 1228 | |
| 1229 | /* Avoid arming the timer to negative, zero, or too low values */ |
| 1230 | if (nearest_delta_us <= MIN_TIMER_REARM_US) |
| 1231 | nearest_delta_us = MIN_TIMER_REARM_US; |
| 1232 | |
| 1233 | return nearest_delta_us; |
| 1234 | } |
| 1235 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1236 | #ifndef _WIN32 |
| 1237 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1238 | #if defined(__linux__) |
| 1239 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1240 | #define RTC_FREQ 1024 |
| 1241 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1242 | static void enable_sigio_timer(int fd) |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1243 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1244 | struct sigaction act; |
| 1245 | |
| 1246 | /* timer signal */ |
| 1247 | sigfillset(&act.sa_mask); |
| 1248 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1249 | act.sa_handler = host_alarm_handler; |
| 1250 | |
| 1251 | sigaction(SIGIO, &act, NULL); |
| 1252 | fcntl(fd, F_SETFL, O_ASYNC); |
| 1253 | fcntl(fd, F_SETOWN, getpid()); |
| 1254 | } |
| 1255 | |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1256 | static int hpet_start_timer(struct qemu_alarm_timer *t) |
| 1257 | { |
| 1258 | struct hpet_info info; |
| 1259 | int r, fd; |
| 1260 | |
| 1261 | fd = open("/dev/hpet", O_RDONLY); |
| 1262 | if (fd < 0) |
| 1263 | return -1; |
| 1264 | |
| 1265 | /* Set frequency */ |
| 1266 | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
| 1267 | if (r < 0) { |
| 1268 | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n" |
| 1269 | "error, but for better emulation accuracy type:\n" |
| 1270 | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n"); |
| 1271 | goto fail; |
| 1272 | } |
| 1273 | |
| 1274 | /* Check capabilities */ |
| 1275 | r = ioctl(fd, HPET_INFO, &info); |
| 1276 | if (r < 0) |
| 1277 | goto fail; |
| 1278 | |
| 1279 | /* Enable periodic mode */ |
| 1280 | r = ioctl(fd, HPET_EPI, 0); |
| 1281 | if (info.hi_flags && (r < 0)) |
| 1282 | goto fail; |
| 1283 | |
| 1284 | /* Enable interrupt */ |
| 1285 | r = ioctl(fd, HPET_IE_ON, 0); |
| 1286 | if (r < 0) |
| 1287 | goto fail; |
| 1288 | |
| 1289 | enable_sigio_timer(fd); |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1290 | t->priv = (void *)(long)fd; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1291 | |
| 1292 | return 0; |
| 1293 | fail: |
| 1294 | close(fd); |
| 1295 | return -1; |
| 1296 | } |
| 1297 | |
| 1298 | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
| 1299 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1300 | int fd = (long)t->priv; |
ths | c40ec5a | 2007-08-19 22:09:40 +0000 | [diff] [blame] | 1301 | |
| 1302 | close(fd); |
| 1303 | } |
| 1304 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1305 | static int rtc_start_timer(struct qemu_alarm_timer *t) |
| 1306 | { |
| 1307 | int rtc_fd; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1308 | unsigned long current_rtc_freq = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1309 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 1310 | TFR(rtc_fd = open("/dev/rtc", O_RDONLY)); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1311 | if (rtc_fd < 0) |
| 1312 | return -1; |
balrog | b5a23ad | 2008-02-03 03:45:47 +0000 | [diff] [blame] | 1313 | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
| 1314 | if (current_rtc_freq != RTC_FREQ && |
| 1315 | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1316 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1317 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1318 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1319 | goto fail; |
| 1320 | } |
| 1321 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1322 | fail: |
| 1323 | close(rtc_fd); |
| 1324 | return -1; |
| 1325 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1326 | |
| 1327 | enable_sigio_timer(rtc_fd); |
| 1328 | |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1329 | t->priv = (void *)(long)rtc_fd; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1330 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1331 | return 0; |
| 1332 | } |
| 1333 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1334 | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1335 | { |
pbrook | fcdc212 | 2007-08-23 20:22:22 +0000 | [diff] [blame] | 1336 | int rtc_fd = (long)t->priv; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1337 | |
| 1338 | close(rtc_fd); |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1341 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
| 1342 | { |
| 1343 | struct sigevent ev; |
| 1344 | timer_t host_timer; |
| 1345 | struct sigaction act; |
| 1346 | |
| 1347 | sigfillset(&act.sa_mask); |
| 1348 | act.sa_flags = 0; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1349 | act.sa_handler = host_alarm_handler; |
| 1350 | |
| 1351 | sigaction(SIGALRM, &act, NULL); |
| 1352 | |
| 1353 | ev.sigev_value.sival_int = 0; |
| 1354 | ev.sigev_notify = SIGEV_SIGNAL; |
| 1355 | ev.sigev_signo = SIGALRM; |
| 1356 | |
| 1357 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { |
| 1358 | perror("timer_create"); |
| 1359 | |
| 1360 | /* disable dynticks */ |
| 1361 | fprintf(stderr, "Dynamic Ticks disabled\n"); |
| 1362 | |
| 1363 | return -1; |
| 1364 | } |
| 1365 | |
| 1366 | t->priv = (void *)host_timer; |
| 1367 | |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
| 1372 | { |
| 1373 | timer_t host_timer = (timer_t)t->priv; |
| 1374 | |
| 1375 | timer_delete(host_timer); |
| 1376 | } |
| 1377 | |
| 1378 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
| 1379 | { |
| 1380 | timer_t host_timer = (timer_t)t->priv; |
| 1381 | struct itimerspec timeout; |
| 1382 | int64_t nearest_delta_us = INT64_MAX; |
| 1383 | int64_t current_us; |
| 1384 | |
| 1385 | if (!active_timers[QEMU_TIMER_REALTIME] && |
| 1386 | !active_timers[QEMU_TIMER_VIRTUAL]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1387 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1388 | |
| 1389 | nearest_delta_us = qemu_next_deadline(); |
| 1390 | |
| 1391 | /* check whether a timer is already running */ |
| 1392 | if (timer_gettime(host_timer, &timeout)) { |
| 1393 | perror("gettime"); |
| 1394 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1395 | exit(1); |
| 1396 | } |
| 1397 | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
| 1398 | if (current_us && current_us <= nearest_delta_us) |
| 1399 | return; |
| 1400 | |
| 1401 | timeout.it_interval.tv_sec = 0; |
| 1402 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
| 1403 | timeout.it_value.tv_sec = nearest_delta_us / 1000000; |
| 1404 | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
| 1405 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
| 1406 | perror("settime"); |
| 1407 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 1408 | exit(1); |
| 1409 | } |
| 1410 | } |
| 1411 | |
ths | 70744b3 | 2007-08-26 17:31:30 +0000 | [diff] [blame] | 1412 | #endif /* defined(__linux__) */ |
ths | 231c658 | 2007-08-26 17:29:15 +0000 | [diff] [blame] | 1413 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1414 | static int unix_start_timer(struct qemu_alarm_timer *t) |
| 1415 | { |
| 1416 | struct sigaction act; |
| 1417 | struct itimerval itv; |
| 1418 | int err; |
| 1419 | |
| 1420 | /* timer signal */ |
| 1421 | sigfillset(&act.sa_mask); |
| 1422 | act.sa_flags = 0; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1423 | act.sa_handler = host_alarm_handler; |
| 1424 | |
| 1425 | sigaction(SIGALRM, &act, NULL); |
| 1426 | |
| 1427 | itv.it_interval.tv_sec = 0; |
| 1428 | /* for i386 kernel 2.6 to get 1 ms */ |
| 1429 | itv.it_interval.tv_usec = 999; |
| 1430 | itv.it_value.tv_sec = 0; |
| 1431 | itv.it_value.tv_usec = 10 * 1000; |
| 1432 | |
| 1433 | err = setitimer(ITIMER_REAL, &itv, NULL); |
| 1434 | if (err) |
| 1435 | return -1; |
| 1436 | |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | static void unix_stop_timer(struct qemu_alarm_timer *t) |
| 1441 | { |
| 1442 | struct itimerval itv; |
| 1443 | |
| 1444 | memset(&itv, 0, sizeof(itv)); |
| 1445 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1446 | } |
| 1447 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1448 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1449 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1450 | #ifdef _WIN32 |
| 1451 | |
| 1452 | static int win32_start_timer(struct qemu_alarm_timer *t) |
| 1453 | { |
| 1454 | TIMECAPS tc; |
| 1455 | struct qemu_alarm_win32 *data = t->priv; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1456 | UINT flags; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1457 | |
| 1458 | data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 1459 | if (!data->host_alarm) { |
| 1460 | perror("Failed CreateEvent"); |
ths | c396a7f | 2007-08-20 15:42:22 +0000 | [diff] [blame] | 1461 | return -1; |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | memset(&tc, 0, sizeof(tc)); |
| 1465 | timeGetDevCaps(&tc, sizeof(tc)); |
| 1466 | |
| 1467 | if (data->period < tc.wPeriodMin) |
| 1468 | data->period = tc.wPeriodMin; |
| 1469 | |
| 1470 | timeBeginPeriod(data->period); |
| 1471 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1472 | flags = TIME_CALLBACK_FUNCTION; |
| 1473 | if (alarm_has_dynticks(t)) |
| 1474 | flags |= TIME_ONESHOT; |
| 1475 | else |
| 1476 | flags |= TIME_PERIODIC; |
| 1477 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1478 | data->timerId = timeSetEvent(1, // interval (ms) |
| 1479 | data->period, // resolution |
| 1480 | host_alarm_handler, // function |
| 1481 | (DWORD)t, // parameter |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1482 | flags); |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1483 | |
| 1484 | if (!data->timerId) { |
| 1485 | perror("Failed to initialize win32 alarm timer"); |
| 1486 | |
| 1487 | timeEndPeriod(data->period); |
| 1488 | CloseHandle(data->host_alarm); |
| 1489 | return -1; |
| 1490 | } |
| 1491 | |
| 1492 | qemu_add_wait_object(data->host_alarm, NULL, NULL); |
| 1493 | |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | static void win32_stop_timer(struct qemu_alarm_timer *t) |
| 1498 | { |
| 1499 | struct qemu_alarm_win32 *data = t->priv; |
| 1500 | |
| 1501 | timeKillEvent(data->timerId); |
| 1502 | timeEndPeriod(data->period); |
| 1503 | |
| 1504 | CloseHandle(data->host_alarm); |
| 1505 | } |
| 1506 | |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1507 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
| 1508 | { |
| 1509 | struct qemu_alarm_win32 *data = t->priv; |
| 1510 | uint64_t nearest_delta_us; |
| 1511 | |
| 1512 | if (!active_timers[QEMU_TIMER_REALTIME] && |
| 1513 | !active_timers[QEMU_TIMER_VIRTUAL]) |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 1514 | return; |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 1515 | |
| 1516 | nearest_delta_us = qemu_next_deadline(); |
| 1517 | nearest_delta_us /= 1000; |
| 1518 | |
| 1519 | timeKillEvent(data->timerId); |
| 1520 | |
| 1521 | data->timerId = timeSetEvent(1, |
| 1522 | data->period, |
| 1523 | host_alarm_handler, |
| 1524 | (DWORD)t, |
| 1525 | TIME_ONESHOT | TIME_PERIODIC); |
| 1526 | |
| 1527 | if (!data->timerId) { |
| 1528 | perror("Failed to re-arm win32 alarm timer"); |
| 1529 | |
| 1530 | timeEndPeriod(data->period); |
| 1531 | CloseHandle(data->host_alarm); |
| 1532 | exit(1); |
| 1533 | } |
| 1534 | } |
| 1535 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1536 | #endif /* _WIN32 */ |
| 1537 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1538 | static void init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1539 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1540 | struct qemu_alarm_timer *t; |
| 1541 | int i, err = -1; |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1542 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1543 | for (i = 0; alarm_timers[i].name; i++) { |
| 1544 | t = &alarm_timers[i]; |
| 1545 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1546 | err = t->start(t); |
| 1547 | if (!err) |
| 1548 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1549 | } |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1550 | |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1551 | if (err) { |
| 1552 | fprintf(stderr, "Unable to find any suitable alarm timer.\n"); |
| 1553 | fprintf(stderr, "Terminating\n"); |
| 1554 | exit(1); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1555 | } |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1556 | |
| 1557 | alarm_timer = t; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1560 | static void quit_timers(void) |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1561 | { |
ths | c899401 | 2007-08-19 21:56:03 +0000 | [diff] [blame] | 1562 | alarm_timer->stop(alarm_timer); |
| 1563 | alarm_timer = NULL; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1566 | /***********************************************************/ |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 1567 | /* host time/date access */ |
| 1568 | void qemu_get_timedate(struct tm *tm, int offset) |
| 1569 | { |
| 1570 | time_t ti; |
| 1571 | struct tm *ret; |
| 1572 | |
| 1573 | time(&ti); |
| 1574 | ti += offset; |
| 1575 | if (rtc_date_offset == -1) { |
| 1576 | if (rtc_utc) |
| 1577 | ret = gmtime(&ti); |
| 1578 | else |
| 1579 | ret = localtime(&ti); |
| 1580 | } else { |
| 1581 | ti -= rtc_date_offset; |
| 1582 | ret = gmtime(&ti); |
| 1583 | } |
| 1584 | |
| 1585 | memcpy(tm, ret, sizeof(struct tm)); |
| 1586 | } |
| 1587 | |
| 1588 | int qemu_timedate_diff(struct tm *tm) |
| 1589 | { |
| 1590 | time_t seconds; |
| 1591 | |
| 1592 | if (rtc_date_offset == -1) |
| 1593 | if (rtc_utc) |
| 1594 | seconds = mktimegm(tm); |
| 1595 | else |
| 1596 | seconds = mktime(tm); |
| 1597 | else |
| 1598 | seconds = mktimegm(tm) + rtc_date_offset; |
| 1599 | |
| 1600 | return seconds - time(NULL); |
| 1601 | } |
| 1602 | |
| 1603 | /***********************************************************/ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1604 | /* character device */ |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1605 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1606 | static void qemu_chr_event(CharDriverState *s, int event) |
| 1607 | { |
| 1608 | if (!s->chr_event) |
| 1609 | return; |
| 1610 | s->chr_event(s->handler_opaque, event); |
| 1611 | } |
| 1612 | |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1613 | static void qemu_chr_reset_bh(void *opaque) |
| 1614 | { |
| 1615 | CharDriverState *s = opaque; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1616 | qemu_chr_event(s, CHR_EVENT_RESET); |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1617 | qemu_bh_delete(s->bh); |
| 1618 | s->bh = NULL; |
| 1619 | } |
| 1620 | |
| 1621 | void qemu_chr_reset(CharDriverState *s) |
| 1622 | { |
| 1623 | if (s->bh == NULL) { |
| 1624 | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
| 1625 | qemu_bh_schedule(s->bh); |
| 1626 | } |
| 1627 | } |
| 1628 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1629 | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1630 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1631 | return s->chr_write(s, buf, len); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1634 | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1635 | { |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1636 | if (!s->chr_ioctl) |
| 1637 | return -ENOTSUP; |
| 1638 | return s->chr_ioctl(s, cmd, arg); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1641 | int qemu_chr_can_read(CharDriverState *s) |
| 1642 | { |
| 1643 | if (!s->chr_can_read) |
| 1644 | return 0; |
| 1645 | return s->chr_can_read(s->handler_opaque); |
| 1646 | } |
| 1647 | |
| 1648 | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
| 1649 | { |
| 1650 | s->chr_read(s->handler_opaque, buf, len); |
| 1651 | } |
| 1652 | |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1653 | void qemu_chr_accept_input(CharDriverState *s) |
| 1654 | { |
| 1655 | if (s->chr_accept_input) |
| 1656 | s->chr_accept_input(s); |
| 1657 | } |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1658 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1659 | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1660 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1661 | char buf[4096]; |
| 1662 | va_list ap; |
| 1663 | va_start(ap, fmt); |
| 1664 | vsnprintf(buf, sizeof(buf), fmt, ap); |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1665 | qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1666 | va_end(ap); |
| 1667 | } |
| 1668 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 1669 | void qemu_chr_send_event(CharDriverState *s, int event) |
| 1670 | { |
| 1671 | if (s->chr_send_event) |
| 1672 | s->chr_send_event(s, event); |
| 1673 | } |
| 1674 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1675 | void qemu_chr_add_handlers(CharDriverState *s, |
| 1676 | IOCanRWHandler *fd_can_read, |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1677 | IOReadHandler *fd_read, |
| 1678 | IOEventHandler *fd_event, |
| 1679 | void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1680 | { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1681 | s->chr_can_read = fd_can_read; |
| 1682 | s->chr_read = fd_read; |
| 1683 | s->chr_event = fd_event; |
| 1684 | s->handler_opaque = opaque; |
| 1685 | if (s->chr_update_read_handler) |
| 1686 | s->chr_update_read_handler(s); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1687 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1688 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1689 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1690 | { |
| 1691 | return len; |
| 1692 | } |
| 1693 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1694 | static CharDriverState *qemu_chr_open_null(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1695 | { |
| 1696 | CharDriverState *chr; |
| 1697 | |
| 1698 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1699 | if (!chr) |
| 1700 | return NULL; |
| 1701 | chr->chr_write = null_chr_write; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1702 | return chr; |
| 1703 | } |
| 1704 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1705 | /* MUX driver for serial I/O splitting */ |
| 1706 | static int term_timestamps; |
| 1707 | static int64_t term_timestamps_start; |
ths | 9c1de61 | 2007-02-21 17:25:30 +0000 | [diff] [blame] | 1708 | #define MAX_MUX 4 |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1709 | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
| 1710 | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1711 | typedef struct { |
| 1712 | IOCanRWHandler *chr_can_read[MAX_MUX]; |
| 1713 | IOReadHandler *chr_read[MAX_MUX]; |
| 1714 | IOEventHandler *chr_event[MAX_MUX]; |
| 1715 | void *ext_opaque[MAX_MUX]; |
| 1716 | CharDriverState *drv; |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1717 | unsigned char buffer[MUX_BUFFER_SIZE]; |
| 1718 | int prod; |
| 1719 | int cons; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1720 | int mux_cnt; |
| 1721 | int term_got_escape; |
| 1722 | int max_size; |
| 1723 | } MuxDriver; |
| 1724 | |
| 1725 | |
| 1726 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1727 | { |
| 1728 | MuxDriver *d = chr->opaque; |
| 1729 | int ret; |
| 1730 | if (!term_timestamps) { |
| 1731 | ret = d->drv->chr_write(d->drv, buf, len); |
| 1732 | } else { |
| 1733 | int i; |
| 1734 | |
| 1735 | ret = 0; |
| 1736 | for(i = 0; i < len; i++) { |
| 1737 | ret += d->drv->chr_write(d->drv, buf+i, 1); |
| 1738 | if (buf[i] == '\n') { |
| 1739 | char buf1[64]; |
| 1740 | int64_t ti; |
| 1741 | int secs; |
| 1742 | |
| 1743 | ti = get_clock(); |
| 1744 | if (term_timestamps_start == -1) |
| 1745 | term_timestamps_start = ti; |
| 1746 | ti -= term_timestamps_start; |
| 1747 | secs = ti / 1000000000; |
| 1748 | snprintf(buf1, sizeof(buf1), |
| 1749 | "[%02d:%02d:%02d.%03d] ", |
| 1750 | secs / 3600, |
| 1751 | (secs / 60) % 60, |
| 1752 | secs % 60, |
| 1753 | (int)((ti / 1000000) % 1000)); |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1754 | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | return ret; |
| 1759 | } |
| 1760 | |
| 1761 | static char *mux_help[] = { |
| 1762 | "% h print this help\n\r", |
| 1763 | "% x exit emulator\n\r", |
| 1764 | "% s save disk data back to file (if -snapshot)\n\r", |
| 1765 | "% t toggle console timestamps\n\r" |
| 1766 | "% b send break (magic sysrq)\n\r", |
| 1767 | "% c switch between console and monitor\n\r", |
| 1768 | "% % sends %\n\r", |
| 1769 | NULL |
| 1770 | }; |
| 1771 | |
| 1772 | static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
| 1773 | static void mux_print_help(CharDriverState *chr) |
| 1774 | { |
| 1775 | int i, j; |
| 1776 | char ebuf[15] = "Escape-Char"; |
| 1777 | char cbuf[50] = "\n\r"; |
| 1778 | |
| 1779 | if (term_escape_char > 0 && term_escape_char < 26) { |
| 1780 | sprintf(cbuf,"\n\r"); |
| 1781 | sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a'); |
| 1782 | } else { |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1783 | sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", |
| 1784 | term_escape_char); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1785 | } |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1786 | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1787 | for (i = 0; mux_help[i] != NULL; i++) { |
| 1788 | for (j=0; mux_help[i][j] != '\0'; j++) { |
| 1789 | if (mux_help[i][j] == '%') |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1790 | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1791 | else |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1792 | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
| 1798 | { |
| 1799 | if (d->term_got_escape) { |
| 1800 | d->term_got_escape = 0; |
| 1801 | if (ch == term_escape_char) |
| 1802 | goto send_char; |
| 1803 | switch(ch) { |
| 1804 | case '?': |
| 1805 | case 'h': |
| 1806 | mux_print_help(chr); |
| 1807 | break; |
| 1808 | case 'x': |
| 1809 | { |
| 1810 | char *term = "QEMU: Terminated\n\r"; |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 1811 | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1812 | exit(0); |
| 1813 | break; |
| 1814 | } |
| 1815 | case 's': |
| 1816 | { |
| 1817 | int i; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 1818 | for (i = 0; i < nb_drives; i++) { |
| 1819 | bdrv_commit(drives_table[i].bdrv); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | break; |
| 1823 | case 'b': |
balrog | 36ddb83 | 2007-05-18 17:46:59 +0000 | [diff] [blame] | 1824 | qemu_chr_event(chr, CHR_EVENT_BREAK); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1825 | break; |
| 1826 | case 'c': |
| 1827 | /* Switch to the next registered device */ |
| 1828 | chr->focus++; |
| 1829 | if (chr->focus >= d->mux_cnt) |
| 1830 | chr->focus = 0; |
| 1831 | break; |
| 1832 | case 't': |
| 1833 | term_timestamps = !term_timestamps; |
| 1834 | term_timestamps_start = -1; |
| 1835 | break; |
| 1836 | } |
| 1837 | } else if (ch == term_escape_char) { |
| 1838 | d->term_got_escape = 1; |
| 1839 | } else { |
| 1840 | send_char: |
| 1841 | return 1; |
| 1842 | } |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1846 | static void mux_chr_accept_input(CharDriverState *chr) |
| 1847 | { |
| 1848 | int m = chr->focus; |
| 1849 | MuxDriver *d = chr->opaque; |
| 1850 | |
| 1851 | while (d->prod != d->cons && |
| 1852 | d->chr_can_read[m] && |
| 1853 | d->chr_can_read[m](d->ext_opaque[m])) { |
| 1854 | d->chr_read[m](d->ext_opaque[m], |
| 1855 | &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1); |
| 1856 | } |
| 1857 | } |
| 1858 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1859 | static int mux_chr_can_read(void *opaque) |
| 1860 | { |
| 1861 | CharDriverState *chr = opaque; |
| 1862 | MuxDriver *d = chr->opaque; |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1863 | |
| 1864 | if ((d->prod - d->cons) < MUX_BUFFER_SIZE) |
| 1865 | return 1; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1866 | if (d->chr_can_read[chr->focus]) |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1867 | return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1868 | return 0; |
| 1869 | } |
| 1870 | |
| 1871 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
| 1872 | { |
| 1873 | CharDriverState *chr = opaque; |
| 1874 | MuxDriver *d = chr->opaque; |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1875 | int m = chr->focus; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1876 | int i; |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1877 | |
| 1878 | mux_chr_accept_input (opaque); |
| 1879 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1880 | for(i = 0; i < size; i++) |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1881 | if (mux_proc_byte(chr, d, buf[i])) { |
| 1882 | if (d->prod == d->cons && |
| 1883 | d->chr_can_read[m] && |
| 1884 | d->chr_can_read[m](d->ext_opaque[m])) |
| 1885 | d->chr_read[m](d->ext_opaque[m], &buf[i], 1); |
| 1886 | else |
| 1887 | d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i]; |
| 1888 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | static void mux_chr_event(void *opaque, int event) |
| 1892 | { |
| 1893 | CharDriverState *chr = opaque; |
| 1894 | MuxDriver *d = chr->opaque; |
| 1895 | int i; |
| 1896 | |
| 1897 | /* Send the event to all registered listeners */ |
| 1898 | for (i = 0; i < d->mux_cnt; i++) |
| 1899 | if (d->chr_event[i]) |
| 1900 | d->chr_event[i](d->ext_opaque[i], event); |
| 1901 | } |
| 1902 | |
| 1903 | static void mux_chr_update_read_handler(CharDriverState *chr) |
| 1904 | { |
| 1905 | MuxDriver *d = chr->opaque; |
| 1906 | |
| 1907 | if (d->mux_cnt >= MAX_MUX) { |
| 1908 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); |
| 1909 | return; |
| 1910 | } |
| 1911 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
| 1912 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
| 1913 | d->chr_read[d->mux_cnt] = chr->chr_read; |
| 1914 | d->chr_event[d->mux_cnt] = chr->chr_event; |
| 1915 | /* Fix up the real driver with mux routines */ |
| 1916 | if (d->mux_cnt == 0) { |
| 1917 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
| 1918 | mux_chr_event, chr); |
| 1919 | } |
| 1920 | chr->focus = d->mux_cnt; |
| 1921 | d->mux_cnt++; |
| 1922 | } |
| 1923 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 1924 | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv) |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1925 | { |
| 1926 | CharDriverState *chr; |
| 1927 | MuxDriver *d; |
| 1928 | |
| 1929 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1930 | if (!chr) |
| 1931 | return NULL; |
| 1932 | d = qemu_mallocz(sizeof(MuxDriver)); |
| 1933 | if (!d) { |
| 1934 | free(chr); |
| 1935 | return NULL; |
| 1936 | } |
| 1937 | |
| 1938 | chr->opaque = d; |
| 1939 | d->drv = drv; |
| 1940 | chr->focus = -1; |
| 1941 | chr->chr_write = mux_chr_write; |
| 1942 | chr->chr_update_read_handler = mux_chr_update_read_handler; |
balrog | bd9bdce | 2007-11-25 00:55:06 +0000 | [diff] [blame] | 1943 | chr->chr_accept_input = mux_chr_accept_input; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 1944 | return chr; |
| 1945 | } |
| 1946 | |
| 1947 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1948 | #ifdef _WIN32 |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1949 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1950 | static void socket_cleanup(void) |
| 1951 | { |
| 1952 | WSACleanup(); |
| 1953 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1954 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1955 | static int socket_init(void) |
| 1956 | { |
| 1957 | WSADATA Data; |
| 1958 | int ret, err; |
| 1959 | |
| 1960 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1961 | if (ret != 0) { |
| 1962 | err = WSAGetLastError(); |
| 1963 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1964 | return -1; |
| 1965 | } |
| 1966 | atexit(socket_cleanup); |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
| 1970 | static int send_all(int fd, const uint8_t *buf, int len1) |
| 1971 | { |
| 1972 | int ret, len; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1973 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1974 | len = len1; |
| 1975 | while (len > 0) { |
| 1976 | ret = send(fd, buf, len, 0); |
| 1977 | if (ret < 0) { |
| 1978 | int errno; |
| 1979 | errno = WSAGetLastError(); |
| 1980 | if (errno != WSAEWOULDBLOCK) { |
| 1981 | return -1; |
| 1982 | } |
| 1983 | } else if (ret == 0) { |
| 1984 | break; |
| 1985 | } else { |
| 1986 | buf += ret; |
| 1987 | len -= ret; |
| 1988 | } |
| 1989 | } |
| 1990 | return len1 - len; |
| 1991 | } |
| 1992 | |
| 1993 | void socket_set_nonblock(int fd) |
| 1994 | { |
| 1995 | unsigned long opt = 1; |
| 1996 | ioctlsocket(fd, FIONBIO, &opt); |
| 1997 | } |
| 1998 | |
| 1999 | #else |
| 2000 | |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 2001 | static int unix_write(int fd, const uint8_t *buf, int len1) |
| 2002 | { |
| 2003 | int ret, len; |
| 2004 | |
| 2005 | len = len1; |
| 2006 | while (len > 0) { |
| 2007 | ret = write(fd, buf, len); |
| 2008 | if (ret < 0) { |
| 2009 | if (errno != EINTR && errno != EAGAIN) |
| 2010 | return -1; |
| 2011 | } else if (ret == 0) { |
| 2012 | break; |
| 2013 | } else { |
| 2014 | buf += ret; |
| 2015 | len -= ret; |
| 2016 | } |
| 2017 | } |
| 2018 | return len1 - len; |
| 2019 | } |
| 2020 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 2021 | static inline int send_all(int fd, const uint8_t *buf, int len1) |
| 2022 | { |
| 2023 | return unix_write(fd, buf, len1); |
| 2024 | } |
| 2025 | |
| 2026 | void socket_set_nonblock(int fd) |
| 2027 | { |
| 2028 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 2029 | } |
| 2030 | #endif /* !_WIN32 */ |
| 2031 | |
| 2032 | #ifndef _WIN32 |
| 2033 | |
| 2034 | typedef struct { |
| 2035 | int fd_in, fd_out; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 2036 | int max_size; |
| 2037 | } FDCharDriver; |
| 2038 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2039 | #define STDIO_MAX_CLIENTS 1 |
| 2040 | static int stdio_nb_clients = 0; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 2041 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2042 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2043 | { |
| 2044 | FDCharDriver *s = chr->opaque; |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 2045 | return unix_write(s->fd_out, buf, len); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2048 | static int fd_chr_read_poll(void *opaque) |
| 2049 | { |
| 2050 | CharDriverState *chr = opaque; |
| 2051 | FDCharDriver *s = chr->opaque; |
| 2052 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2053 | s->max_size = qemu_chr_can_read(chr); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2054 | return s->max_size; |
| 2055 | } |
| 2056 | |
| 2057 | static void fd_chr_read(void *opaque) |
| 2058 | { |
| 2059 | CharDriverState *chr = opaque; |
| 2060 | FDCharDriver *s = chr->opaque; |
| 2061 | int size, len; |
| 2062 | uint8_t buf[1024]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2063 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2064 | len = sizeof(buf); |
| 2065 | if (len > s->max_size) |
| 2066 | len = s->max_size; |
| 2067 | if (len == 0) |
| 2068 | return; |
| 2069 | size = read(s->fd_in, buf, len); |
pbrook | 188157f | 2006-11-01 01:44:16 +0000 | [diff] [blame] | 2070 | if (size == 0) { |
| 2071 | /* FD has been closed. Remove it from the active list. */ |
| 2072 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
| 2073 | return; |
| 2074 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2075 | if (size > 0) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2076 | qemu_chr_read(chr, buf, size); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2077 | } |
| 2078 | } |
| 2079 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2080 | static void fd_chr_update_read_handler(CharDriverState *chr) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2081 | { |
| 2082 | FDCharDriver *s = chr->opaque; |
| 2083 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2084 | if (s->fd_in >= 0) { |
| 2085 | if (nographic && s->fd_in == 0) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2086 | } else { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2087 | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2088 | fd_chr_read, NULL, chr); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2089 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | |
balrog | a11d070 | 2008-01-19 13:00:43 +0000 | [diff] [blame] | 2093 | static void fd_chr_close(struct CharDriverState *chr) |
| 2094 | { |
| 2095 | FDCharDriver *s = chr->opaque; |
| 2096 | |
| 2097 | if (s->fd_in >= 0) { |
| 2098 | if (nographic && s->fd_in == 0) { |
| 2099 | } else { |
| 2100 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
| 2101 | } |
| 2102 | } |
| 2103 | |
| 2104 | qemu_free(s); |
| 2105 | } |
| 2106 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2107 | /* open a character device to a unix fd */ |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2108 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2109 | { |
| 2110 | CharDriverState *chr; |
| 2111 | FDCharDriver *s; |
| 2112 | |
| 2113 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2114 | if (!chr) |
| 2115 | return NULL; |
| 2116 | s = qemu_mallocz(sizeof(FDCharDriver)); |
| 2117 | if (!s) { |
| 2118 | free(chr); |
| 2119 | return NULL; |
| 2120 | } |
| 2121 | s->fd_in = fd_in; |
| 2122 | s->fd_out = fd_out; |
| 2123 | chr->opaque = s; |
| 2124 | chr->chr_write = fd_chr_write; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2125 | chr->chr_update_read_handler = fd_chr_update_read_handler; |
balrog | a11d070 | 2008-01-19 13:00:43 +0000 | [diff] [blame] | 2126 | chr->chr_close = fd_chr_close; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2127 | |
| 2128 | qemu_chr_reset(chr); |
| 2129 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2130 | return chr; |
| 2131 | } |
| 2132 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2133 | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2134 | { |
| 2135 | int fd_out; |
| 2136 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 2137 | TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666)); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2138 | if (fd_out < 0) |
| 2139 | return NULL; |
| 2140 | return qemu_chr_open_fd(-1, fd_out); |
| 2141 | } |
| 2142 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2143 | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2144 | { |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 2145 | int fd_in, fd_out; |
| 2146 | char filename_in[256], filename_out[256]; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2147 | |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 2148 | snprintf(filename_in, 256, "%s.in", filename); |
| 2149 | snprintf(filename_out, 256, "%s.out", filename); |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 2150 | TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
| 2151 | TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 2152 | if (fd_in < 0 || fd_out < 0) { |
| 2153 | if (fd_in >= 0) |
| 2154 | close(fd_in); |
| 2155 | if (fd_out >= 0) |
| 2156 | close(fd_out); |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 2157 | TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 2158 | if (fd_in < 0) |
| 2159 | return NULL; |
| 2160 | } |
| 2161 | return qemu_chr_open_fd(fd_in, fd_out); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2165 | /* for STDIO, we handle the case where several clients use it |
| 2166 | (nographic mode) */ |
| 2167 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 2168 | #define TERM_FIFO_MAX_SIZE 1 |
| 2169 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 2170 | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE]; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 2171 | static int term_fifo_size; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2172 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2173 | static int stdio_read_poll(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2174 | { |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2175 | CharDriverState *chr = opaque; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 2176 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2177 | /* try to flush the queue if needed */ |
| 2178 | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
| 2179 | qemu_chr_read(chr, term_fifo, 1); |
| 2180 | term_fifo_size = 0; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 2181 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2182 | /* see if we can absorb more chars */ |
| 2183 | if (term_fifo_size == 0) |
| 2184 | return 1; |
| 2185 | else |
| 2186 | return 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2189 | static void stdio_read(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2190 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2191 | int size; |
| 2192 | uint8_t buf[1]; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2193 | CharDriverState *chr = opaque; |
| 2194 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2195 | size = read(0, buf, 1); |
pbrook | 519945d | 2006-09-10 14:39:54 +0000 | [diff] [blame] | 2196 | if (size == 0) { |
| 2197 | /* stdin has been closed. Remove it from the active list. */ |
| 2198 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
| 2199 | return; |
| 2200 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2201 | if (size > 0) { |
| 2202 | if (qemu_chr_can_read(chr) > 0) { |
| 2203 | qemu_chr_read(chr, buf, 1); |
| 2204 | } else if (term_fifo_size == 0) { |
| 2205 | term_fifo[term_fifo_size++] = buf[0]; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 2206 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 2207 | } |
| 2208 | } |
| 2209 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 2210 | /* init terminal so that we can grab keys */ |
| 2211 | static struct termios oldtty; |
| 2212 | static int old_fd0_flags; |
balrog | a11d070 | 2008-01-19 13:00:43 +0000 | [diff] [blame] | 2213 | static int term_atexit_done; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 2214 | |
| 2215 | static void term_exit(void) |
| 2216 | { |
| 2217 | tcsetattr (0, TCSANOW, &oldtty); |
| 2218 | fcntl(0, F_SETFL, old_fd0_flags); |
| 2219 | } |
| 2220 | |
| 2221 | static void term_init(void) |
| 2222 | { |
| 2223 | struct termios tty; |
| 2224 | |
| 2225 | tcgetattr (0, &tty); |
| 2226 | oldtty = tty; |
| 2227 | old_fd0_flags = fcntl(0, F_GETFL); |
| 2228 | |
| 2229 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 2230 | |INLCR|IGNCR|ICRNL|IXON); |
| 2231 | tty.c_oflag |= OPOST; |
| 2232 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 2233 | /* if graphical mode, we allow Ctrl-C handling */ |
| 2234 | if (nographic) |
| 2235 | tty.c_lflag &= ~ISIG; |
| 2236 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 2237 | tty.c_cflag |= CS8; |
| 2238 | tty.c_cc[VMIN] = 1; |
| 2239 | tty.c_cc[VTIME] = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2240 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 2241 | tcsetattr (0, TCSANOW, &tty); |
| 2242 | |
balrog | a11d070 | 2008-01-19 13:00:43 +0000 | [diff] [blame] | 2243 | if (!term_atexit_done++) |
| 2244 | atexit(term_exit); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 2245 | |
| 2246 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 2247 | } |
| 2248 | |
balrog | a11d070 | 2008-01-19 13:00:43 +0000 | [diff] [blame] | 2249 | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
| 2250 | { |
| 2251 | term_exit(); |
| 2252 | stdio_nb_clients--; |
| 2253 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
| 2254 | fd_chr_close(chr); |
| 2255 | } |
| 2256 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2257 | static CharDriverState *qemu_chr_open_stdio(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2258 | { |
| 2259 | CharDriverState *chr; |
| 2260 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2261 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS) |
| 2262 | return NULL; |
| 2263 | chr = qemu_chr_open_fd(0, 1); |
balrog | a11d070 | 2008-01-19 13:00:43 +0000 | [diff] [blame] | 2264 | chr->chr_close = qemu_chr_close_stdio; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 2265 | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
| 2266 | stdio_nb_clients++; |
| 2267 | term_init(); |
| 2268 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2269 | return chr; |
| 2270 | } |
| 2271 | |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 2272 | #ifdef __sun__ |
| 2273 | /* Once Solaris has openpty(), this is going to be removed. */ |
| 2274 | int openpty(int *amaster, int *aslave, char *name, |
| 2275 | struct termios *termp, struct winsize *winp) |
| 2276 | { |
| 2277 | const char *slave; |
| 2278 | int mfd = -1, sfd = -1; |
| 2279 | |
| 2280 | *amaster = *aslave = -1; |
| 2281 | |
| 2282 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY); |
| 2283 | if (mfd < 0) |
| 2284 | goto err; |
| 2285 | |
| 2286 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
| 2287 | goto err; |
| 2288 | |
| 2289 | if ((slave = ptsname(mfd)) == NULL) |
| 2290 | goto err; |
| 2291 | |
| 2292 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
| 2293 | goto err; |
| 2294 | |
| 2295 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
| 2296 | (termp != NULL && tcgetattr(sfd, termp) < 0)) |
| 2297 | goto err; |
| 2298 | |
| 2299 | if (amaster) |
| 2300 | *amaster = mfd; |
| 2301 | if (aslave) |
| 2302 | *aslave = sfd; |
| 2303 | if (winp) |
| 2304 | ioctl(sfd, TIOCSWINSZ, winp); |
| 2305 | |
| 2306 | return 0; |
| 2307 | |
| 2308 | err: |
| 2309 | if (sfd != -1) |
| 2310 | close(sfd); |
| 2311 | close(mfd); |
| 2312 | return -1; |
| 2313 | } |
| 2314 | |
| 2315 | void cfmakeraw (struct termios *termios_p) |
| 2316 | { |
| 2317 | termios_p->c_iflag &= |
| 2318 | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
| 2319 | termios_p->c_oflag &= ~OPOST; |
| 2320 | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
| 2321 | termios_p->c_cflag &= ~(CSIZE|PARENB); |
| 2322 | termios_p->c_cflag |= CS8; |
| 2323 | |
| 2324 | termios_p->c_cc[VMIN] = 0; |
| 2325 | termios_p->c_cc[VTIME] = 0; |
| 2326 | } |
| 2327 | #endif |
| 2328 | |
ths | aec6250 | 2007-06-25 11:48:07 +0000 | [diff] [blame] | 2329 | #if defined(__linux__) || defined(__sun__) |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2330 | static CharDriverState *qemu_chr_open_pty(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2331 | { |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 2332 | struct termios tty; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2333 | int master_fd, slave_fd; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2334 | |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 2335 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) < 0) { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2336 | return NULL; |
| 2337 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2338 | |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 2339 | /* Set raw attributes on the pty. */ |
| 2340 | cfmakeraw(&tty); |
| 2341 | tcsetattr(slave_fd, TCSAFLUSH, &tty); |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 2342 | |
aurel32 | 64b7b73 | 2008-05-05 10:05:31 +0000 | [diff] [blame] | 2343 | fprintf(stderr, "char device redirected to %s\n", ptsname(master_fd)); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2344 | return qemu_chr_open_fd(master_fd, master_fd); |
| 2345 | } |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2346 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2347 | static void tty_serial_init(int fd, int speed, |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2348 | int parity, int data_bits, int stop_bits) |
| 2349 | { |
| 2350 | struct termios tty; |
| 2351 | speed_t spd; |
| 2352 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2353 | #if 0 |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2354 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2355 | speed, parity, data_bits, stop_bits); |
| 2356 | #endif |
| 2357 | tcgetattr (fd, &tty); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2358 | |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2359 | #define MARGIN 1.1 |
| 2360 | if (speed <= 50 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2361 | spd = B50; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2362 | else if (speed <= 75 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2363 | spd = B75; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2364 | else if (speed <= 300 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2365 | spd = B300; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2366 | else if (speed <= 600 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2367 | spd = B600; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2368 | else if (speed <= 1200 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2369 | spd = B1200; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2370 | else if (speed <= 2400 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2371 | spd = B2400; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2372 | else if (speed <= 4800 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2373 | spd = B4800; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2374 | else if (speed <= 9600 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2375 | spd = B9600; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2376 | else if (speed <= 19200 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2377 | spd = B19200; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2378 | else if (speed <= 38400 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2379 | spd = B38400; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2380 | else if (speed <= 57600 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2381 | spd = B57600; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2382 | else if (speed <= 115200 * MARGIN) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2383 | spd = B115200; |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 2384 | else |
| 2385 | spd = B115200; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2386 | |
| 2387 | cfsetispeed(&tty, spd); |
| 2388 | cfsetospeed(&tty, spd); |
| 2389 | |
| 2390 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 2391 | |INLCR|IGNCR|ICRNL|IXON); |
| 2392 | tty.c_oflag |= OPOST; |
| 2393 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 2394 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2395 | switch(data_bits) { |
| 2396 | default: |
| 2397 | case 8: |
| 2398 | tty.c_cflag |= CS8; |
| 2399 | break; |
| 2400 | case 7: |
| 2401 | tty.c_cflag |= CS7; |
| 2402 | break; |
| 2403 | case 6: |
| 2404 | tty.c_cflag |= CS6; |
| 2405 | break; |
| 2406 | case 5: |
| 2407 | tty.c_cflag |= CS5; |
| 2408 | break; |
| 2409 | } |
| 2410 | switch(parity) { |
| 2411 | default: |
| 2412 | case 'N': |
| 2413 | break; |
| 2414 | case 'E': |
| 2415 | tty.c_cflag |= PARENB; |
| 2416 | break; |
| 2417 | case 'O': |
| 2418 | tty.c_cflag |= PARENB | PARODD; |
| 2419 | break; |
| 2420 | } |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 2421 | if (stop_bits == 2) |
| 2422 | tty.c_cflag |= CSTOPB; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2423 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2424 | tcsetattr (fd, TCSANOW, &tty); |
| 2425 | } |
| 2426 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2427 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2428 | { |
| 2429 | FDCharDriver *s = chr->opaque; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2430 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2431 | switch(cmd) { |
| 2432 | case CHR_IOCTL_SERIAL_SET_PARAMS: |
| 2433 | { |
| 2434 | QEMUSerialSetParams *ssp = arg; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2435 | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2436 | ssp->data_bits, ssp->stop_bits); |
| 2437 | } |
| 2438 | break; |
| 2439 | case CHR_IOCTL_SERIAL_SET_BREAK: |
| 2440 | { |
| 2441 | int enable = *(int *)arg; |
| 2442 | if (enable) |
| 2443 | tcsendbreak(s->fd_in, 1); |
| 2444 | } |
| 2445 | break; |
| 2446 | default: |
| 2447 | return -ENOTSUP; |
| 2448 | } |
| 2449 | return 0; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2452 | static CharDriverState *qemu_chr_open_tty(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2453 | { |
| 2454 | CharDriverState *chr; |
| 2455 | int fd; |
| 2456 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 2457 | TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2458 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 2459 | tty_serial_init(fd, 115200, 'N', 8, 1); |
| 2460 | chr = qemu_chr_open_fd(fd, fd); |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 2461 | if (!chr) { |
| 2462 | close(fd); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2463 | return NULL; |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 2464 | } |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2465 | chr->chr_ioctl = tty_serial_ioctl; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2466 | qemu_chr_reset(chr); |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2467 | return chr; |
| 2468 | } |
ths | aec6250 | 2007-06-25 11:48:07 +0000 | [diff] [blame] | 2469 | #else /* ! __linux__ && ! __sun__ */ |
| 2470 | static CharDriverState *qemu_chr_open_pty(void) |
| 2471 | { |
| 2472 | return NULL; |
| 2473 | } |
| 2474 | #endif /* __linux__ || __sun__ */ |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2475 | |
ths | aec6250 | 2007-06-25 11:48:07 +0000 | [diff] [blame] | 2476 | #if defined(__linux__) |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2477 | typedef struct { |
| 2478 | int fd; |
| 2479 | int mode; |
| 2480 | } ParallelCharDriver; |
| 2481 | |
| 2482 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
| 2483 | { |
| 2484 | if (s->mode != mode) { |
| 2485 | int m = mode; |
| 2486 | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
| 2487 | return 0; |
| 2488 | s->mode = mode; |
| 2489 | } |
| 2490 | return 1; |
| 2491 | } |
| 2492 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2493 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 2494 | { |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2495 | ParallelCharDriver *drv = chr->opaque; |
| 2496 | int fd = drv->fd; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2497 | uint8_t b; |
| 2498 | |
| 2499 | switch(cmd) { |
| 2500 | case CHR_IOCTL_PP_READ_DATA: |
| 2501 | if (ioctl(fd, PPRDATA, &b) < 0) |
| 2502 | return -ENOTSUP; |
| 2503 | *(uint8_t *)arg = b; |
| 2504 | break; |
| 2505 | case CHR_IOCTL_PP_WRITE_DATA: |
| 2506 | b = *(uint8_t *)arg; |
| 2507 | if (ioctl(fd, PPWDATA, &b) < 0) |
| 2508 | return -ENOTSUP; |
| 2509 | break; |
| 2510 | case CHR_IOCTL_PP_READ_CONTROL: |
| 2511 | if (ioctl(fd, PPRCONTROL, &b) < 0) |
| 2512 | return -ENOTSUP; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2513 | /* Linux gives only the lowest bits, and no way to know data |
| 2514 | direction! For better compatibility set the fixed upper |
| 2515 | bits. */ |
| 2516 | *(uint8_t *)arg = b | 0xc0; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2517 | break; |
| 2518 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 2519 | b = *(uint8_t *)arg; |
| 2520 | if (ioctl(fd, PPWCONTROL, &b) < 0) |
| 2521 | return -ENOTSUP; |
| 2522 | break; |
| 2523 | case CHR_IOCTL_PP_READ_STATUS: |
| 2524 | if (ioctl(fd, PPRSTATUS, &b) < 0) |
| 2525 | return -ENOTSUP; |
| 2526 | *(uint8_t *)arg = b; |
| 2527 | break; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2528 | case CHR_IOCTL_PP_EPP_READ_ADDR: |
| 2529 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 2530 | struct ParallelIOArg *parg = arg; |
| 2531 | int n = read(fd, parg->buffer, parg->count); |
| 2532 | if (n != parg->count) { |
| 2533 | return -EIO; |
| 2534 | } |
| 2535 | } |
| 2536 | break; |
| 2537 | case CHR_IOCTL_PP_EPP_READ: |
| 2538 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 2539 | struct ParallelIOArg *parg = arg; |
| 2540 | int n = read(fd, parg->buffer, parg->count); |
| 2541 | if (n != parg->count) { |
| 2542 | return -EIO; |
| 2543 | } |
| 2544 | } |
| 2545 | break; |
| 2546 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: |
| 2547 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 2548 | struct ParallelIOArg *parg = arg; |
| 2549 | int n = write(fd, parg->buffer, parg->count); |
| 2550 | if (n != parg->count) { |
| 2551 | return -EIO; |
| 2552 | } |
| 2553 | } |
| 2554 | break; |
| 2555 | case CHR_IOCTL_PP_EPP_WRITE: |
| 2556 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 2557 | struct ParallelIOArg *parg = arg; |
| 2558 | int n = write(fd, parg->buffer, parg->count); |
| 2559 | if (n != parg->count) { |
| 2560 | return -EIO; |
| 2561 | } |
| 2562 | } |
| 2563 | break; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2564 | default: |
| 2565 | return -ENOTSUP; |
| 2566 | } |
| 2567 | return 0; |
| 2568 | } |
| 2569 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2570 | static void pp_close(CharDriverState *chr) |
| 2571 | { |
| 2572 | ParallelCharDriver *drv = chr->opaque; |
| 2573 | int fd = drv->fd; |
| 2574 | |
| 2575 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
| 2576 | ioctl(fd, PPRELEASE); |
| 2577 | close(fd); |
| 2578 | qemu_free(drv); |
| 2579 | } |
| 2580 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2581 | static CharDriverState *qemu_chr_open_pp(const char *filename) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2582 | { |
| 2583 | CharDriverState *chr; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2584 | ParallelCharDriver *drv; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2585 | int fd; |
| 2586 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 2587 | TFR(fd = open(filename, O_RDWR)); |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2588 | if (fd < 0) |
| 2589 | return NULL; |
| 2590 | |
| 2591 | if (ioctl(fd, PPCLAIM) < 0) { |
| 2592 | close(fd); |
| 2593 | return NULL; |
| 2594 | } |
| 2595 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2596 | drv = qemu_mallocz(sizeof(ParallelCharDriver)); |
| 2597 | if (!drv) { |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2598 | close(fd); |
| 2599 | return NULL; |
| 2600 | } |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2601 | drv->fd = fd; |
| 2602 | drv->mode = IEEE1284_MODE_COMPAT; |
| 2603 | |
| 2604 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2605 | if (!chr) { |
| 2606 | qemu_free(drv); |
| 2607 | close(fd); |
| 2608 | return NULL; |
| 2609 | } |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2610 | chr->chr_write = null_chr_write; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2611 | chr->chr_ioctl = pp_ioctl; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2612 | chr->chr_close = pp_close; |
| 2613 | chr->opaque = drv; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2614 | |
| 2615 | qemu_chr_reset(chr); |
| 2616 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2617 | return chr; |
| 2618 | } |
ths | aec6250 | 2007-06-25 11:48:07 +0000 | [diff] [blame] | 2619 | #endif /* __linux__ */ |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2620 | |
ths | aec6250 | 2007-06-25 11:48:07 +0000 | [diff] [blame] | 2621 | #else /* _WIN32 */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2622 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2623 | typedef struct { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2624 | int max_size; |
| 2625 | HANDLE hcom, hrecv, hsend; |
| 2626 | OVERLAPPED orecv, osend; |
| 2627 | BOOL fpipe; |
| 2628 | DWORD len; |
| 2629 | } WinCharState; |
| 2630 | |
| 2631 | #define NSENDBUF 2048 |
| 2632 | #define NRECVBUF 2048 |
| 2633 | #define MAXCONNECT 1 |
| 2634 | #define NTIMEOUT 5000 |
| 2635 | |
| 2636 | static int win_chr_poll(void *opaque); |
| 2637 | static int win_chr_pipe_poll(void *opaque); |
| 2638 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2639 | static void win_chr_close(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2640 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2641 | WinCharState *s = chr->opaque; |
| 2642 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2643 | if (s->hsend) { |
| 2644 | CloseHandle(s->hsend); |
| 2645 | s->hsend = NULL; |
| 2646 | } |
| 2647 | if (s->hrecv) { |
| 2648 | CloseHandle(s->hrecv); |
| 2649 | s->hrecv = NULL; |
| 2650 | } |
| 2651 | if (s->hcom) { |
| 2652 | CloseHandle(s->hcom); |
| 2653 | s->hcom = NULL; |
| 2654 | } |
| 2655 | if (s->fpipe) |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2656 | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2657 | else |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2658 | qemu_del_polling_cb(win_chr_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2661 | static int win_chr_init(CharDriverState *chr, const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2662 | { |
| 2663 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2664 | COMMCONFIG comcfg; |
| 2665 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
| 2666 | COMSTAT comstat; |
| 2667 | DWORD size; |
| 2668 | DWORD err; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2669 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2670 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2671 | if (!s->hsend) { |
| 2672 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2673 | goto fail; |
| 2674 | } |
| 2675 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2676 | if (!s->hrecv) { |
| 2677 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2678 | goto fail; |
| 2679 | } |
| 2680 | |
| 2681 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
| 2682 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
| 2683 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 2684 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); |
| 2685 | s->hcom = NULL; |
| 2686 | goto fail; |
| 2687 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2688 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2689 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { |
| 2690 | fprintf(stderr, "Failed SetupComm\n"); |
| 2691 | goto fail; |
| 2692 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2693 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2694 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); |
| 2695 | size = sizeof(COMMCONFIG); |
| 2696 | GetDefaultCommConfig(filename, &comcfg, &size); |
| 2697 | comcfg.dcb.DCBlength = sizeof(DCB); |
| 2698 | CommConfigDialog(filename, NULL, &comcfg); |
| 2699 | |
| 2700 | if (!SetCommState(s->hcom, &comcfg.dcb)) { |
| 2701 | fprintf(stderr, "Failed SetCommState\n"); |
| 2702 | goto fail; |
| 2703 | } |
| 2704 | |
| 2705 | if (!SetCommMask(s->hcom, EV_ERR)) { |
| 2706 | fprintf(stderr, "Failed SetCommMask\n"); |
| 2707 | goto fail; |
| 2708 | } |
| 2709 | |
| 2710 | cto.ReadIntervalTimeout = MAXDWORD; |
| 2711 | if (!SetCommTimeouts(s->hcom, &cto)) { |
| 2712 | fprintf(stderr, "Failed SetCommTimeouts\n"); |
| 2713 | goto fail; |
| 2714 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2715 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2716 | if (!ClearCommError(s->hcom, &err, &comstat)) { |
| 2717 | fprintf(stderr, "Failed ClearCommError\n"); |
| 2718 | goto fail; |
| 2719 | } |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2720 | qemu_add_polling_cb(win_chr_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2721 | return 0; |
| 2722 | |
| 2723 | fail: |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2724 | win_chr_close(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2725 | return -1; |
| 2726 | } |
| 2727 | |
| 2728 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
| 2729 | { |
| 2730 | WinCharState *s = chr->opaque; |
| 2731 | DWORD len, ret, size, err; |
| 2732 | |
| 2733 | len = len1; |
| 2734 | ZeroMemory(&s->osend, sizeof(s->osend)); |
| 2735 | s->osend.hEvent = s->hsend; |
| 2736 | while (len > 0) { |
| 2737 | if (s->hsend) |
| 2738 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
| 2739 | else |
| 2740 | ret = WriteFile(s->hcom, buf, len, &size, NULL); |
| 2741 | if (!ret) { |
| 2742 | err = GetLastError(); |
| 2743 | if (err == ERROR_IO_PENDING) { |
| 2744 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
| 2745 | if (ret) { |
| 2746 | buf += size; |
| 2747 | len -= size; |
| 2748 | } else { |
| 2749 | break; |
| 2750 | } |
| 2751 | } else { |
| 2752 | break; |
| 2753 | } |
| 2754 | } else { |
| 2755 | buf += size; |
| 2756 | len -= size; |
| 2757 | } |
| 2758 | } |
| 2759 | return len1 - len; |
| 2760 | } |
| 2761 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2762 | static int win_chr_read_poll(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2763 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2764 | WinCharState *s = chr->opaque; |
| 2765 | |
| 2766 | s->max_size = qemu_chr_can_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2767 | return s->max_size; |
| 2768 | } |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2769 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2770 | static void win_chr_readfile(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2771 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2772 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2773 | int ret, err; |
| 2774 | uint8_t buf[1024]; |
| 2775 | DWORD size; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2776 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2777 | ZeroMemory(&s->orecv, sizeof(s->orecv)); |
| 2778 | s->orecv.hEvent = s->hrecv; |
| 2779 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
| 2780 | if (!ret) { |
| 2781 | err = GetLastError(); |
| 2782 | if (err == ERROR_IO_PENDING) { |
| 2783 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | if (size > 0) { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2788 | qemu_chr_read(chr, buf, size); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2789 | } |
| 2790 | } |
| 2791 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2792 | static void win_chr_read(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2793 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2794 | WinCharState *s = chr->opaque; |
| 2795 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2796 | if (s->len > s->max_size) |
| 2797 | s->len = s->max_size; |
| 2798 | if (s->len == 0) |
| 2799 | return; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2800 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2801 | win_chr_readfile(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | static int win_chr_poll(void *opaque) |
| 2805 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2806 | CharDriverState *chr = opaque; |
| 2807 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2808 | COMSTAT status; |
| 2809 | DWORD comerr; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2810 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2811 | ClearCommError(s->hcom, &comerr, &status); |
| 2812 | if (status.cbInQue > 0) { |
| 2813 | s->len = status.cbInQue; |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2814 | win_chr_read_poll(chr); |
| 2815 | win_chr_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2816 | return 1; |
| 2817 | } |
| 2818 | return 0; |
| 2819 | } |
| 2820 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2821 | static CharDriverState *qemu_chr_open_win(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2822 | { |
| 2823 | CharDriverState *chr; |
| 2824 | WinCharState *s; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2825 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2826 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2827 | if (!chr) |
| 2828 | return NULL; |
| 2829 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2830 | if (!s) { |
| 2831 | free(chr); |
| 2832 | return NULL; |
| 2833 | } |
| 2834 | chr->opaque = s; |
| 2835 | chr->chr_write = win_chr_write; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2836 | chr->chr_close = win_chr_close; |
| 2837 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2838 | if (win_chr_init(chr, filename) < 0) { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2839 | free(s); |
| 2840 | free(chr); |
| 2841 | return NULL; |
| 2842 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2843 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2844 | return chr; |
| 2845 | } |
| 2846 | |
| 2847 | static int win_chr_pipe_poll(void *opaque) |
| 2848 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2849 | CharDriverState *chr = opaque; |
| 2850 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2851 | DWORD size; |
| 2852 | |
| 2853 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
| 2854 | if (size > 0) { |
| 2855 | s->len = size; |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2856 | win_chr_read_poll(chr); |
| 2857 | win_chr_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2858 | return 1; |
| 2859 | } |
| 2860 | return 0; |
| 2861 | } |
| 2862 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2863 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2864 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2865 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2866 | OVERLAPPED ov; |
| 2867 | int ret; |
| 2868 | DWORD size; |
| 2869 | char openname[256]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2870 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2871 | s->fpipe = TRUE; |
| 2872 | |
| 2873 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2874 | if (!s->hsend) { |
| 2875 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2876 | goto fail; |
| 2877 | } |
| 2878 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2879 | if (!s->hrecv) { |
| 2880 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2881 | goto fail; |
| 2882 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2883 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2884 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
| 2885 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 2886 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
| 2887 | PIPE_WAIT, |
| 2888 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); |
| 2889 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 2890 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); |
| 2891 | s->hcom = NULL; |
| 2892 | goto fail; |
| 2893 | } |
| 2894 | |
| 2895 | ZeroMemory(&ov, sizeof(ov)); |
| 2896 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2897 | ret = ConnectNamedPipe(s->hcom, &ov); |
| 2898 | if (ret) { |
| 2899 | fprintf(stderr, "Failed ConnectNamedPipe\n"); |
| 2900 | goto fail; |
| 2901 | } |
| 2902 | |
| 2903 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
| 2904 | if (!ret) { |
| 2905 | fprintf(stderr, "Failed GetOverlappedResult\n"); |
| 2906 | if (ov.hEvent) { |
| 2907 | CloseHandle(ov.hEvent); |
| 2908 | ov.hEvent = NULL; |
| 2909 | } |
| 2910 | goto fail; |
| 2911 | } |
| 2912 | |
| 2913 | if (ov.hEvent) { |
| 2914 | CloseHandle(ov.hEvent); |
| 2915 | ov.hEvent = NULL; |
| 2916 | } |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2917 | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2918 | return 0; |
| 2919 | |
| 2920 | fail: |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2921 | win_chr_close(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2922 | return -1; |
| 2923 | } |
| 2924 | |
| 2925 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2926 | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2927 | { |
| 2928 | CharDriverState *chr; |
| 2929 | WinCharState *s; |
| 2930 | |
| 2931 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2932 | if (!chr) |
| 2933 | return NULL; |
| 2934 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2935 | if (!s) { |
| 2936 | free(chr); |
| 2937 | return NULL; |
| 2938 | } |
| 2939 | chr->opaque = s; |
| 2940 | chr->chr_write = win_chr_write; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2941 | chr->chr_close = win_chr_close; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2942 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2943 | if (win_chr_pipe_init(chr, filename) < 0) { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2944 | free(s); |
| 2945 | free(chr); |
| 2946 | return NULL; |
| 2947 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2948 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2949 | return chr; |
| 2950 | } |
| 2951 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2952 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2953 | { |
| 2954 | CharDriverState *chr; |
| 2955 | WinCharState *s; |
| 2956 | |
| 2957 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2958 | if (!chr) |
| 2959 | return NULL; |
| 2960 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2961 | if (!s) { |
| 2962 | free(chr); |
| 2963 | return NULL; |
| 2964 | } |
| 2965 | s->hcom = fd_out; |
| 2966 | chr->opaque = s; |
| 2967 | chr->chr_write = win_chr_write; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2968 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2969 | return chr; |
| 2970 | } |
ths | 72d4647 | 2007-05-13 14:54:54 +0000 | [diff] [blame] | 2971 | |
| 2972 | static CharDriverState *qemu_chr_open_win_con(const char *filename) |
| 2973 | { |
| 2974 | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE)); |
| 2975 | } |
| 2976 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2977 | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2978 | { |
| 2979 | HANDLE fd_out; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2980 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2981 | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 2982 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 2983 | if (fd_out == INVALID_HANDLE_VALUE) |
| 2984 | return NULL; |
| 2985 | |
| 2986 | return qemu_chr_open_win_file(fd_out); |
| 2987 | } |
ths | aec6250 | 2007-06-25 11:48:07 +0000 | [diff] [blame] | 2988 | #endif /* !_WIN32 */ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2989 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2990 | /***********************************************************/ |
| 2991 | /* UDP Net console */ |
| 2992 | |
| 2993 | typedef struct { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2994 | int fd; |
| 2995 | struct sockaddr_in daddr; |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 2996 | uint8_t buf[1024]; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2997 | int bufcnt; |
| 2998 | int bufptr; |
| 2999 | int max_size; |
| 3000 | } NetCharDriver; |
| 3001 | |
| 3002 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 3003 | { |
| 3004 | NetCharDriver *s = chr->opaque; |
| 3005 | |
| 3006 | return sendto(s->fd, buf, len, 0, |
| 3007 | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
| 3008 | } |
| 3009 | |
| 3010 | static int udp_chr_read_poll(void *opaque) |
| 3011 | { |
| 3012 | CharDriverState *chr = opaque; |
| 3013 | NetCharDriver *s = chr->opaque; |
| 3014 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3015 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3016 | |
| 3017 | /* If there were any stray characters in the queue process them |
| 3018 | * first |
| 3019 | */ |
| 3020 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3021 | qemu_chr_read(chr, &s->buf[s->bufptr], 1); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3022 | s->bufptr++; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3023 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3024 | } |
| 3025 | return s->max_size; |
| 3026 | } |
| 3027 | |
| 3028 | static void udp_chr_read(void *opaque) |
| 3029 | { |
| 3030 | CharDriverState *chr = opaque; |
| 3031 | NetCharDriver *s = chr->opaque; |
| 3032 | |
| 3033 | if (s->max_size == 0) |
| 3034 | return; |
| 3035 | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
| 3036 | s->bufptr = s->bufcnt; |
| 3037 | if (s->bufcnt <= 0) |
| 3038 | return; |
| 3039 | |
| 3040 | s->bufptr = 0; |
| 3041 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3042 | qemu_chr_read(chr, &s->buf[s->bufptr], 1); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3043 | s->bufptr++; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3044 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3045 | } |
| 3046 | } |
| 3047 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3048 | static void udp_chr_update_read_handler(CharDriverState *chr) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3049 | { |
| 3050 | NetCharDriver *s = chr->opaque; |
| 3051 | |
| 3052 | if (s->fd >= 0) { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3053 | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
| 3054 | udp_chr_read, NULL, chr); |
| 3055 | } |
| 3056 | } |
| 3057 | |
| 3058 | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3059 | #ifndef _WIN32 |
| 3060 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
| 3061 | #endif |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3062 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 3063 | struct sockaddr_in *saddr, |
| 3064 | const char *str); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3065 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3066 | static CharDriverState *qemu_chr_open_udp(const char *def) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3067 | { |
| 3068 | CharDriverState *chr = NULL; |
| 3069 | NetCharDriver *s = NULL; |
| 3070 | int fd = -1; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3071 | struct sockaddr_in saddr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3072 | |
| 3073 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 3074 | if (!chr) |
| 3075 | goto return_err; |
| 3076 | s = qemu_mallocz(sizeof(NetCharDriver)); |
| 3077 | if (!s) |
| 3078 | goto return_err; |
| 3079 | |
| 3080 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 3081 | if (fd < 0) { |
| 3082 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 3083 | goto return_err; |
| 3084 | } |
| 3085 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3086 | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
| 3087 | printf("Could not parse: %s\n", def); |
| 3088 | goto return_err; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3091 | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3092 | { |
| 3093 | perror("bind"); |
| 3094 | goto return_err; |
| 3095 | } |
| 3096 | |
| 3097 | s->fd = fd; |
| 3098 | s->bufcnt = 0; |
| 3099 | s->bufptr = 0; |
| 3100 | chr->opaque = s; |
| 3101 | chr->chr_write = udp_chr_write; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3102 | chr->chr_update_read_handler = udp_chr_update_read_handler; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3103 | return chr; |
| 3104 | |
| 3105 | return_err: |
| 3106 | if (chr) |
| 3107 | free(chr); |
| 3108 | if (s) |
| 3109 | free(s); |
| 3110 | if (fd >= 0) |
| 3111 | closesocket(fd); |
| 3112 | return NULL; |
| 3113 | } |
| 3114 | |
| 3115 | /***********************************************************/ |
| 3116 | /* TCP Net console */ |
| 3117 | |
| 3118 | typedef struct { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3119 | int fd, listen_fd; |
| 3120 | int connected; |
| 3121 | int max_size; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3122 | int do_telnetopt; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3123 | int do_nodelay; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3124 | int is_unix; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3125 | } TCPCharDriver; |
| 3126 | |
| 3127 | static void tcp_chr_accept(void *opaque); |
| 3128 | |
| 3129 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 3130 | { |
| 3131 | TCPCharDriver *s = chr->opaque; |
| 3132 | if (s->connected) { |
| 3133 | return send_all(s->fd, buf, len); |
| 3134 | } else { |
| 3135 | /* XXX: indicate an error ? */ |
| 3136 | return len; |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | static int tcp_chr_read_poll(void *opaque) |
| 3141 | { |
| 3142 | CharDriverState *chr = opaque; |
| 3143 | TCPCharDriver *s = chr->opaque; |
| 3144 | if (!s->connected) |
| 3145 | return 0; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3146 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3147 | return s->max_size; |
| 3148 | } |
| 3149 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3150 | #define IAC 255 |
| 3151 | #define IAC_BREAK 243 |
| 3152 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
| 3153 | TCPCharDriver *s, |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 3154 | uint8_t *buf, int *size) |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3155 | { |
| 3156 | /* Handle any telnet client's basic IAC options to satisfy char by |
| 3157 | * char mode with no echo. All IAC options will be removed from |
| 3158 | * the buf and the do_telnetopt variable will be used to track the |
| 3159 | * state of the width of the IAC information. |
| 3160 | * |
| 3161 | * IAC commands come in sets of 3 bytes with the exception of the |
| 3162 | * "IAC BREAK" command and the double IAC. |
| 3163 | */ |
| 3164 | |
| 3165 | int i; |
| 3166 | int j = 0; |
| 3167 | |
| 3168 | for (i = 0; i < *size; i++) { |
| 3169 | if (s->do_telnetopt > 1) { |
| 3170 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
| 3171 | /* Double IAC means send an IAC */ |
| 3172 | if (j != i) |
| 3173 | buf[j] = buf[i]; |
| 3174 | j++; |
| 3175 | s->do_telnetopt = 1; |
| 3176 | } else { |
| 3177 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
| 3178 | /* Handle IAC break commands by sending a serial break */ |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3179 | qemu_chr_event(chr, CHR_EVENT_BREAK); |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3180 | s->do_telnetopt++; |
| 3181 | } |
| 3182 | s->do_telnetopt++; |
| 3183 | } |
| 3184 | if (s->do_telnetopt >= 4) { |
| 3185 | s->do_telnetopt = 1; |
| 3186 | } |
| 3187 | } else { |
| 3188 | if ((unsigned char)buf[i] == IAC) { |
| 3189 | s->do_telnetopt = 2; |
| 3190 | } else { |
| 3191 | if (j != i) |
| 3192 | buf[j] = buf[i]; |
| 3193 | j++; |
| 3194 | } |
| 3195 | } |
| 3196 | } |
| 3197 | *size = j; |
| 3198 | } |
| 3199 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3200 | static void tcp_chr_read(void *opaque) |
| 3201 | { |
| 3202 | CharDriverState *chr = opaque; |
| 3203 | TCPCharDriver *s = chr->opaque; |
| 3204 | uint8_t buf[1024]; |
| 3205 | int len, size; |
| 3206 | |
| 3207 | if (!s->connected || s->max_size <= 0) |
| 3208 | return; |
| 3209 | len = sizeof(buf); |
| 3210 | if (len > s->max_size) |
| 3211 | len = s->max_size; |
| 3212 | size = recv(s->fd, buf, len, 0); |
| 3213 | if (size == 0) { |
| 3214 | /* connection closed */ |
| 3215 | s->connected = 0; |
| 3216 | if (s->listen_fd >= 0) { |
| 3217 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
| 3218 | } |
| 3219 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 3220 | closesocket(s->fd); |
| 3221 | s->fd = -1; |
| 3222 | } else if (size > 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3223 | if (s->do_telnetopt) |
| 3224 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
| 3225 | if (size > 0) |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 3226 | qemu_chr_read(chr, buf, size); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3227 | } |
| 3228 | } |
| 3229 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3230 | static void tcp_chr_connect(void *opaque) |
| 3231 | { |
| 3232 | CharDriverState *chr = opaque; |
| 3233 | TCPCharDriver *s = chr->opaque; |
| 3234 | |
| 3235 | s->connected = 1; |
| 3236 | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
| 3237 | tcp_chr_read, NULL, chr); |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 3238 | qemu_chr_reset(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3241 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
| 3242 | static void tcp_chr_telnet_init(int fd) |
| 3243 | { |
| 3244 | char buf[3]; |
| 3245 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ |
| 3246 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
| 3247 | send(fd, (char *)buf, 3, 0); |
| 3248 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
| 3249 | send(fd, (char *)buf, 3, 0); |
| 3250 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
| 3251 | send(fd, (char *)buf, 3, 0); |
| 3252 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
| 3253 | send(fd, (char *)buf, 3, 0); |
| 3254 | } |
| 3255 | |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 3256 | static void socket_set_nodelay(int fd) |
| 3257 | { |
| 3258 | int val = 1; |
| 3259 | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
| 3260 | } |
| 3261 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3262 | static void tcp_chr_accept(void *opaque) |
| 3263 | { |
| 3264 | CharDriverState *chr = opaque; |
| 3265 | TCPCharDriver *s = chr->opaque; |
| 3266 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3267 | #ifndef _WIN32 |
| 3268 | struct sockaddr_un uaddr; |
| 3269 | #endif |
| 3270 | struct sockaddr *addr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3271 | socklen_t len; |
| 3272 | int fd; |
| 3273 | |
| 3274 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3275 | #ifndef _WIN32 |
| 3276 | if (s->is_unix) { |
| 3277 | len = sizeof(uaddr); |
| 3278 | addr = (struct sockaddr *)&uaddr; |
| 3279 | } else |
| 3280 | #endif |
| 3281 | { |
| 3282 | len = sizeof(saddr); |
| 3283 | addr = (struct sockaddr *)&saddr; |
| 3284 | } |
| 3285 | fd = accept(s->listen_fd, addr, &len); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3286 | if (fd < 0 && errno != EINTR) { |
| 3287 | return; |
| 3288 | } else if (fd >= 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3289 | if (s->do_telnetopt) |
| 3290 | tcp_chr_telnet_init(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3291 | break; |
| 3292 | } |
| 3293 | } |
| 3294 | socket_set_nonblock(fd); |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 3295 | if (s->do_nodelay) |
| 3296 | socket_set_nodelay(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3297 | s->fd = fd; |
| 3298 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
| 3299 | tcp_chr_connect(chr); |
| 3300 | } |
| 3301 | |
| 3302 | static void tcp_chr_close(CharDriverState *chr) |
| 3303 | { |
| 3304 | TCPCharDriver *s = chr->opaque; |
| 3305 | if (s->fd >= 0) |
| 3306 | closesocket(s->fd); |
| 3307 | if (s->listen_fd >= 0) |
| 3308 | closesocket(s->listen_fd); |
| 3309 | qemu_free(s); |
| 3310 | } |
| 3311 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3312 | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3313 | int is_telnet, |
| 3314 | int is_unix) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3315 | { |
| 3316 | CharDriverState *chr = NULL; |
| 3317 | TCPCharDriver *s = NULL; |
| 3318 | int fd = -1, ret, err, val; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3319 | int is_listen = 0; |
| 3320 | int is_waitconnect = 1; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 3321 | int do_nodelay = 0; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3322 | const char *ptr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3323 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3324 | #ifndef _WIN32 |
| 3325 | struct sockaddr_un uaddr; |
| 3326 | #endif |
| 3327 | struct sockaddr *addr; |
| 3328 | socklen_t addrlen; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3329 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3330 | #ifndef _WIN32 |
| 3331 | if (is_unix) { |
| 3332 | addr = (struct sockaddr *)&uaddr; |
| 3333 | addrlen = sizeof(uaddr); |
| 3334 | if (parse_unix_path(&uaddr, host_str) < 0) |
| 3335 | goto fail; |
| 3336 | } else |
| 3337 | #endif |
| 3338 | { |
| 3339 | addr = (struct sockaddr *)&saddr; |
| 3340 | addrlen = sizeof(saddr); |
| 3341 | if (parse_host_port(&saddr, host_str) < 0) |
| 3342 | goto fail; |
| 3343 | } |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3344 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3345 | ptr = host_str; |
| 3346 | while((ptr = strchr(ptr,','))) { |
| 3347 | ptr++; |
| 3348 | if (!strncmp(ptr,"server",6)) { |
| 3349 | is_listen = 1; |
| 3350 | } else if (!strncmp(ptr,"nowait",6)) { |
| 3351 | is_waitconnect = 0; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 3352 | } else if (!strncmp(ptr,"nodelay",6)) { |
| 3353 | do_nodelay = 1; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3354 | } else { |
| 3355 | printf("Unknown option: %s\n", ptr); |
| 3356 | goto fail; |
| 3357 | } |
| 3358 | } |
| 3359 | if (!is_listen) |
| 3360 | is_waitconnect = 0; |
| 3361 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3362 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 3363 | if (!chr) |
| 3364 | goto fail; |
| 3365 | s = qemu_mallocz(sizeof(TCPCharDriver)); |
| 3366 | if (!s) |
| 3367 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3368 | |
| 3369 | #ifndef _WIN32 |
| 3370 | if (is_unix) |
| 3371 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 3372 | else |
| 3373 | #endif |
| 3374 | fd = socket(PF_INET, SOCK_STREAM, 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3375 | |
| 3376 | if (fd < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3377 | goto fail; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3378 | |
| 3379 | if (!is_waitconnect) |
| 3380 | socket_set_nonblock(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3381 | |
| 3382 | s->connected = 0; |
| 3383 | s->fd = -1; |
| 3384 | s->listen_fd = -1; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3385 | s->is_unix = is_unix; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 3386 | s->do_nodelay = do_nodelay && !is_unix; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3387 | |
| 3388 | chr->opaque = s; |
| 3389 | chr->chr_write = tcp_chr_write; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3390 | chr->chr_close = tcp_chr_close; |
| 3391 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3392 | if (is_listen) { |
| 3393 | /* allow fast reuse */ |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3394 | #ifndef _WIN32 |
| 3395 | if (is_unix) { |
| 3396 | char path[109]; |
| 3397 | strncpy(path, uaddr.sun_path, 108); |
| 3398 | path[108] = 0; |
| 3399 | unlink(path); |
| 3400 | } else |
| 3401 | #endif |
| 3402 | { |
| 3403 | val = 1; |
| 3404 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
| 3405 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3406 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3407 | ret = bind(fd, addr, addrlen); |
| 3408 | if (ret < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3409 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3410 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3411 | ret = listen(fd, 0); |
| 3412 | if (ret < 0) |
| 3413 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3414 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3415 | s->listen_fd = fd; |
| 3416 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3417 | if (is_telnet) |
| 3418 | s->do_telnetopt = 1; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3419 | } else { |
| 3420 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3421 | ret = connect(fd, addr, addrlen); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3422 | if (ret < 0) { |
| 3423 | err = socket_error(); |
| 3424 | if (err == EINTR || err == EWOULDBLOCK) { |
| 3425 | } else if (err == EINPROGRESS) { |
| 3426 | break; |
ths | f5b1226 | 2007-03-25 15:58:03 +0000 | [diff] [blame] | 3427 | #ifdef _WIN32 |
| 3428 | } else if (err == WSAEALREADY) { |
| 3429 | break; |
| 3430 | #endif |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3431 | } else { |
| 3432 | goto fail; |
| 3433 | } |
| 3434 | } else { |
| 3435 | s->connected = 1; |
| 3436 | break; |
| 3437 | } |
| 3438 | } |
| 3439 | s->fd = fd; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 3440 | socket_set_nodelay(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3441 | if (s->connected) |
| 3442 | tcp_chr_connect(chr); |
| 3443 | else |
| 3444 | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr); |
| 3445 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3446 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3447 | if (is_listen && is_waitconnect) { |
| 3448 | printf("QEMU waiting for connection on: %s\n", host_str); |
| 3449 | tcp_chr_accept(chr); |
| 3450 | socket_set_nonblock(s->listen_fd); |
| 3451 | } |
| 3452 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3453 | return chr; |
| 3454 | fail: |
| 3455 | if (fd >= 0) |
| 3456 | closesocket(fd); |
| 3457 | qemu_free(s); |
| 3458 | qemu_free(chr); |
| 3459 | return NULL; |
| 3460 | } |
| 3461 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3462 | CharDriverState *qemu_chr_open(const char *filename) |
| 3463 | { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 3464 | const char *p; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3465 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3466 | if (!strcmp(filename, "vc")) { |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 3467 | return text_console_init(&display_state, 0); |
| 3468 | } else if (strstart(filename, "vc:", &p)) { |
| 3469 | return text_console_init(&display_state, p); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3470 | } else if (!strcmp(filename, "null")) { |
| 3471 | return qemu_chr_open_null(); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3472 | } else |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3473 | if (strstart(filename, "tcp:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3474 | return qemu_chr_open_tcp(p, 0, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3475 | } else |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3476 | if (strstart(filename, "telnet:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3477 | return qemu_chr_open_tcp(p, 1, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3478 | } else |
| 3479 | if (strstart(filename, "udp:", &p)) { |
| 3480 | return qemu_chr_open_udp(p); |
| 3481 | } else |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 3482 | if (strstart(filename, "mon:", &p)) { |
| 3483 | CharDriverState *drv = qemu_chr_open(p); |
| 3484 | if (drv) { |
| 3485 | drv = qemu_chr_open_mux(drv); |
| 3486 | monitor_init(drv, !nographic); |
| 3487 | return drv; |
| 3488 | } |
| 3489 | printf("Unable to open driver: %s\n", p); |
| 3490 | return 0; |
| 3491 | } else |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 3492 | #ifndef _WIN32 |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3493 | if (strstart(filename, "unix:", &p)) { |
| 3494 | return qemu_chr_open_tcp(p, 0, 1); |
| 3495 | } else if (strstart(filename, "file:", &p)) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 3496 | return qemu_chr_open_file_out(p); |
| 3497 | } else if (strstart(filename, "pipe:", &p)) { |
| 3498 | return qemu_chr_open_pipe(p); |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 3499 | } else if (!strcmp(filename, "pty")) { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3500 | return qemu_chr_open_pty(); |
| 3501 | } else if (!strcmp(filename, "stdio")) { |
| 3502 | return qemu_chr_open_stdio(); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3503 | } else |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 3504 | #if defined(__linux__) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 3505 | if (strstart(filename, "/dev/parport", NULL)) { |
| 3506 | return qemu_chr_open_pp(filename); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3507 | } else |
ths | aec6250 | 2007-06-25 11:48:07 +0000 | [diff] [blame] | 3508 | #endif |
ths | 3fda388 | 2007-06-28 15:14:49 +0000 | [diff] [blame] | 3509 | #if defined(__linux__) || defined(__sun__) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 3510 | if (strstart(filename, "/dev/", NULL)) { |
| 3511 | return qemu_chr_open_tty(filename); |
ths | 3fda388 | 2007-06-28 15:14:49 +0000 | [diff] [blame] | 3512 | } else |
| 3513 | #endif |
ths | aec6250 | 2007-06-25 11:48:07 +0000 | [diff] [blame] | 3514 | #else /* !_WIN32 */ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3515 | if (strstart(filename, "COM", NULL)) { |
| 3516 | return qemu_chr_open_win(filename); |
| 3517 | } else |
| 3518 | if (strstart(filename, "pipe:", &p)) { |
| 3519 | return qemu_chr_open_win_pipe(p); |
| 3520 | } else |
ths | 72d4647 | 2007-05-13 14:54:54 +0000 | [diff] [blame] | 3521 | if (strstart(filename, "con:", NULL)) { |
| 3522 | return qemu_chr_open_win_con(filename); |
| 3523 | } else |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3524 | if (strstart(filename, "file:", &p)) { |
| 3525 | return qemu_chr_open_win_file_out(p); |
aurel32 | 2e4d9fb | 2008-04-08 06:01:02 +0000 | [diff] [blame] | 3526 | } else |
| 3527 | #endif |
| 3528 | #ifdef CONFIG_BRLAPI |
| 3529 | if (!strcmp(filename, "braille")) { |
| 3530 | return chr_baum_init(); |
| 3531 | } else |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3532 | #endif |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 3533 | { |
| 3534 | return NULL; |
| 3535 | } |
| 3536 | } |
| 3537 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3538 | void qemu_chr_close(CharDriverState *chr) |
| 3539 | { |
| 3540 | if (chr->chr_close) |
| 3541 | chr->chr_close(chr); |
balrog | a11d070 | 2008-01-19 13:00:43 +0000 | [diff] [blame] | 3542 | qemu_free(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3545 | /***********************************************************/ |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3546 | /* network device redirectors */ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3547 | |
j_mayer | 3f4afa1 | 2007-11-19 01:05:22 +0000 | [diff] [blame] | 3548 | __attribute__ (( unused )) |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3549 | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3550 | { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3551 | int len, i, j, c; |
| 3552 | |
| 3553 | for(i=0;i<size;i+=16) { |
| 3554 | len = size - i; |
| 3555 | if (len > 16) |
| 3556 | len = 16; |
| 3557 | fprintf(f, "%08x ", i); |
| 3558 | for(j=0;j<16;j++) { |
| 3559 | if (j < len) |
| 3560 | fprintf(f, " %02x", buf[i+j]); |
| 3561 | else |
| 3562 | fprintf(f, " "); |
| 3563 | } |
| 3564 | fprintf(f, " "); |
| 3565 | for(j=0;j<len;j++) { |
| 3566 | c = buf[i+j]; |
| 3567 | if (c < ' ' || c > '~') |
| 3568 | c = '.'; |
| 3569 | fprintf(f, "%c", c); |
| 3570 | } |
| 3571 | fprintf(f, "\n"); |
| 3572 | } |
| 3573 | } |
| 3574 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3575 | static int parse_macaddr(uint8_t *macaddr, const char *p) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3576 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3577 | int i; |
balrog | 76ea08f | 2007-12-16 11:48:54 +0000 | [diff] [blame] | 3578 | char *last_char; |
| 3579 | long int offset; |
| 3580 | |
| 3581 | errno = 0; |
| 3582 | offset = strtol(p, &last_char, 0); |
| 3583 | if (0 == errno && '\0' == *last_char && |
| 3584 | offset >= 0 && offset <= 0xFFFFFF) { |
| 3585 | macaddr[3] = (offset & 0xFF0000) >> 16; |
| 3586 | macaddr[4] = (offset & 0xFF00) >> 8; |
| 3587 | macaddr[5] = offset & 0xFF; |
| 3588 | return 0; |
| 3589 | } else { |
| 3590 | for(i = 0; i < 6; i++) { |
| 3591 | macaddr[i] = strtol(p, (char **)&p, 16); |
| 3592 | if (i == 5) { |
| 3593 | if (*p != '\0') |
| 3594 | return -1; |
| 3595 | } else { |
| 3596 | if (*p != ':' && *p != '-') |
| 3597 | return -1; |
| 3598 | p++; |
| 3599 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3600 | } |
balrog | 76ea08f | 2007-12-16 11:48:54 +0000 | [diff] [blame] | 3601 | return 0; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3602 | } |
balrog | 76ea08f | 2007-12-16 11:48:54 +0000 | [diff] [blame] | 3603 | |
| 3604 | return -1; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3607 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 3608 | { |
| 3609 | const char *p, *p1; |
| 3610 | int len; |
| 3611 | p = *pp; |
| 3612 | p1 = strchr(p, sep); |
| 3613 | if (!p1) |
| 3614 | return -1; |
| 3615 | len = p1 - p; |
| 3616 | p1++; |
| 3617 | if (buf_size > 0) { |
| 3618 | if (len > buf_size - 1) |
| 3619 | len = buf_size - 1; |
| 3620 | memcpy(buf, p, len); |
| 3621 | buf[len] = '\0'; |
| 3622 | } |
| 3623 | *pp = p1; |
| 3624 | return 0; |
| 3625 | } |
| 3626 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3627 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 3628 | struct sockaddr_in *saddr, |
| 3629 | const char *input_str) |
| 3630 | { |
| 3631 | char *str = strdup(input_str); |
| 3632 | char *host_str = str; |
| 3633 | char *src_str; |
| 3634 | char *ptr; |
| 3635 | |
| 3636 | /* |
| 3637 | * Chop off any extra arguments at the end of the string which |
| 3638 | * would start with a comma, then fill in the src port information |
| 3639 | * if it was provided else use the "any address" and "any port". |
| 3640 | */ |
| 3641 | if ((ptr = strchr(str,','))) |
| 3642 | *ptr = '\0'; |
| 3643 | |
| 3644 | if ((src_str = strchr(input_str,'@'))) { |
| 3645 | *src_str = '\0'; |
| 3646 | src_str++; |
| 3647 | } |
| 3648 | |
| 3649 | if (parse_host_port(haddr, host_str) < 0) |
| 3650 | goto fail; |
| 3651 | |
| 3652 | if (!src_str || *src_str == '\0') |
| 3653 | src_str = ":0"; |
| 3654 | |
| 3655 | if (parse_host_port(saddr, src_str) < 0) |
| 3656 | goto fail; |
| 3657 | |
| 3658 | free(str); |
| 3659 | return(0); |
| 3660 | |
| 3661 | fail: |
| 3662 | free(str); |
| 3663 | return -1; |
| 3664 | } |
| 3665 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3666 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
| 3667 | { |
| 3668 | char buf[512]; |
| 3669 | struct hostent *he; |
| 3670 | const char *p, *r; |
| 3671 | int port; |
| 3672 | |
| 3673 | p = str; |
| 3674 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3675 | return -1; |
| 3676 | saddr->sin_family = AF_INET; |
| 3677 | if (buf[0] == '\0') { |
| 3678 | saddr->sin_addr.s_addr = 0; |
| 3679 | } else { |
| 3680 | if (isdigit(buf[0])) { |
| 3681 | if (!inet_aton(buf, &saddr->sin_addr)) |
| 3682 | return -1; |
| 3683 | } else { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3684 | if ((he = gethostbyname(buf)) == NULL) |
| 3685 | return - 1; |
| 3686 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3687 | } |
| 3688 | } |
| 3689 | port = strtol(p, (char **)&r, 0); |
| 3690 | if (r == p) |
| 3691 | return -1; |
| 3692 | saddr->sin_port = htons(port); |
| 3693 | return 0; |
| 3694 | } |
| 3695 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3696 | #ifndef _WIN32 |
| 3697 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3698 | { |
| 3699 | const char *p; |
| 3700 | int len; |
| 3701 | |
| 3702 | len = MIN(108, strlen(str)); |
| 3703 | p = strchr(str, ','); |
| 3704 | if (p) |
| 3705 | len = MIN(len, p - str); |
| 3706 | |
| 3707 | memset(uaddr, 0, sizeof(*uaddr)); |
| 3708 | |
| 3709 | uaddr->sun_family = AF_UNIX; |
| 3710 | memcpy(uaddr->sun_path, str, len); |
| 3711 | |
| 3712 | return 0; |
| 3713 | } |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3714 | #endif |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3715 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3716 | /* find or alloc a new VLAN */ |
| 3717 | VLANState *qemu_find_vlan(int id) |
| 3718 | { |
| 3719 | VLANState **pvlan, *vlan; |
| 3720 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 3721 | if (vlan->id == id) |
| 3722 | return vlan; |
| 3723 | } |
| 3724 | vlan = qemu_mallocz(sizeof(VLANState)); |
| 3725 | if (!vlan) |
| 3726 | return NULL; |
| 3727 | vlan->id = id; |
| 3728 | vlan->next = NULL; |
| 3729 | pvlan = &first_vlan; |
| 3730 | while (*pvlan != NULL) |
| 3731 | pvlan = &(*pvlan)->next; |
| 3732 | *pvlan = vlan; |
| 3733 | return vlan; |
| 3734 | } |
| 3735 | |
| 3736 | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3737 | IOReadHandler *fd_read, |
| 3738 | IOCanRWHandler *fd_can_read, |
| 3739 | void *opaque) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3740 | { |
| 3741 | VLANClientState *vc, **pvc; |
| 3742 | vc = qemu_mallocz(sizeof(VLANClientState)); |
| 3743 | if (!vc) |
| 3744 | return NULL; |
| 3745 | vc->fd_read = fd_read; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3746 | vc->fd_can_read = fd_can_read; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3747 | vc->opaque = opaque; |
| 3748 | vc->vlan = vlan; |
| 3749 | |
| 3750 | vc->next = NULL; |
| 3751 | pvc = &vlan->first_client; |
| 3752 | while (*pvc != NULL) |
| 3753 | pvc = &(*pvc)->next; |
| 3754 | *pvc = vc; |
| 3755 | return vc; |
| 3756 | } |
| 3757 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3758 | int qemu_can_send_packet(VLANClientState *vc1) |
| 3759 | { |
| 3760 | VLANState *vlan = vc1->vlan; |
| 3761 | VLANClientState *vc; |
| 3762 | |
| 3763 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3764 | if (vc != vc1) { |
balrog | fbd1711 | 2007-07-02 13:31:53 +0000 | [diff] [blame] | 3765 | if (vc->fd_can_read && vc->fd_can_read(vc->opaque)) |
| 3766 | return 1; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3767 | } |
| 3768 | } |
balrog | fbd1711 | 2007-07-02 13:31:53 +0000 | [diff] [blame] | 3769 | return 0; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3772 | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
| 3773 | { |
| 3774 | VLANState *vlan = vc1->vlan; |
| 3775 | VLANClientState *vc; |
| 3776 | |
| 3777 | #if 0 |
| 3778 | printf("vlan %d send:\n", vlan->id); |
| 3779 | hex_dump(stdout, buf, size); |
| 3780 | #endif |
| 3781 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3782 | if (vc != vc1) { |
| 3783 | vc->fd_read(vc->opaque, buf, size); |
| 3784 | } |
| 3785 | } |
| 3786 | } |
| 3787 | |
| 3788 | #if defined(CONFIG_SLIRP) |
| 3789 | |
| 3790 | /* slirp network adapter */ |
| 3791 | |
| 3792 | static int slirp_inited; |
| 3793 | static VLANClientState *slirp_vc; |
| 3794 | |
| 3795 | int slirp_can_output(void) |
| 3796 | { |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3797 | return !slirp_vc || qemu_can_send_packet(slirp_vc); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | void slirp_output(const uint8_t *pkt, int pkt_len) |
| 3801 | { |
| 3802 | #if 0 |
| 3803 | printf("slirp output:\n"); |
| 3804 | hex_dump(stdout, pkt, pkt_len); |
| 3805 | #endif |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3806 | if (!slirp_vc) |
| 3807 | return; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3808 | qemu_send_packet(slirp_vc, pkt, pkt_len); |
| 3809 | } |
| 3810 | |
| 3811 | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
| 3812 | { |
| 3813 | #if 0 |
| 3814 | printf("slirp input:\n"); |
| 3815 | hex_dump(stdout, buf, size); |
| 3816 | #endif |
| 3817 | slirp_input(buf, size); |
| 3818 | } |
| 3819 | |
| 3820 | static int net_slirp_init(VLANState *vlan) |
| 3821 | { |
| 3822 | if (!slirp_inited) { |
| 3823 | slirp_inited = 1; |
| 3824 | slirp_init(); |
| 3825 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3826 | slirp_vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3827 | slirp_receive, NULL, NULL); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3828 | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
| 3829 | return 0; |
| 3830 | } |
| 3831 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3832 | static void net_slirp_redir(const char *redir_str) |
| 3833 | { |
| 3834 | int is_udp; |
| 3835 | char buf[256], *r; |
| 3836 | const char *p; |
| 3837 | struct in_addr guest_addr; |
| 3838 | int host_port, guest_port; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3839 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3840 | if (!slirp_inited) { |
| 3841 | slirp_inited = 1; |
| 3842 | slirp_init(); |
| 3843 | } |
| 3844 | |
| 3845 | p = redir_str; |
| 3846 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3847 | goto fail; |
| 3848 | if (!strcmp(buf, "tcp")) { |
| 3849 | is_udp = 0; |
| 3850 | } else if (!strcmp(buf, "udp")) { |
| 3851 | is_udp = 1; |
| 3852 | } else { |
| 3853 | goto fail; |
| 3854 | } |
| 3855 | |
| 3856 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3857 | goto fail; |
| 3858 | host_port = strtol(buf, &r, 0); |
| 3859 | if (r == buf) |
| 3860 | goto fail; |
| 3861 | |
| 3862 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3863 | goto fail; |
| 3864 | if (buf[0] == '\0') { |
| 3865 | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
| 3866 | } |
| 3867 | if (!inet_aton(buf, &guest_addr)) |
| 3868 | goto fail; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3869 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3870 | guest_port = strtol(p, &r, 0); |
| 3871 | if (r == p) |
| 3872 | goto fail; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3873 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3874 | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
| 3875 | fprintf(stderr, "qemu: could not set up redirection\n"); |
| 3876 | exit(1); |
| 3877 | } |
| 3878 | return; |
| 3879 | fail: |
| 3880 | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n"); |
| 3881 | exit(1); |
| 3882 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3883 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3884 | #ifndef _WIN32 |
| 3885 | |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3886 | char smb_dir[1024]; |
| 3887 | |
balrog | 044fae8 | 2008-01-14 03:11:16 +0000 | [diff] [blame] | 3888 | static void erase_dir(char *dir_name) |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3889 | { |
| 3890 | DIR *d; |
| 3891 | struct dirent *de; |
| 3892 | char filename[1024]; |
| 3893 | |
| 3894 | /* erase all the files in the directory */ |
balrog | 044fae8 | 2008-01-14 03:11:16 +0000 | [diff] [blame] | 3895 | if ((d = opendir(dir_name)) != 0) { |
| 3896 | for(;;) { |
| 3897 | de = readdir(d); |
| 3898 | if (!de) |
| 3899 | break; |
| 3900 | if (strcmp(de->d_name, ".") != 0 && |
| 3901 | strcmp(de->d_name, "..") != 0) { |
| 3902 | snprintf(filename, sizeof(filename), "%s/%s", |
| 3903 | smb_dir, de->d_name); |
| 3904 | if (unlink(filename) != 0) /* is it a directory? */ |
| 3905 | erase_dir(filename); |
| 3906 | } |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3907 | } |
balrog | 044fae8 | 2008-01-14 03:11:16 +0000 | [diff] [blame] | 3908 | closedir(d); |
| 3909 | rmdir(dir_name); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3910 | } |
balrog | 044fae8 | 2008-01-14 03:11:16 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | /* automatic user mode samba server configuration */ |
| 3914 | static void smb_exit(void) |
| 3915 | { |
| 3916 | erase_dir(smb_dir); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | /* automatic user mode samba server configuration */ |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 3920 | static void net_slirp_smb(const char *exported_dir) |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3921 | { |
| 3922 | char smb_conf[1024]; |
| 3923 | char smb_cmdline[1024]; |
| 3924 | FILE *f; |
| 3925 | |
| 3926 | if (!slirp_inited) { |
| 3927 | slirp_inited = 1; |
| 3928 | slirp_init(); |
| 3929 | } |
| 3930 | |
| 3931 | /* XXX: better tmp dir construction */ |
| 3932 | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
| 3933 | if (mkdir(smb_dir, 0700) < 0) { |
| 3934 | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); |
| 3935 | exit(1); |
| 3936 | } |
| 3937 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3938 | |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3939 | f = fopen(smb_conf, "w"); |
| 3940 | if (!f) { |
| 3941 | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf); |
| 3942 | exit(1); |
| 3943 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3944 | fprintf(f, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3945 | "[global]\n" |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3946 | "private dir=%s\n" |
| 3947 | "smb ports=0\n" |
| 3948 | "socket address=127.0.0.1\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3949 | "pid directory=%s\n" |
| 3950 | "lock directory=%s\n" |
| 3951 | "log file=%s/log.smbd\n" |
| 3952 | "smb passwd file=%s/smbpasswd\n" |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 3953 | "security = share\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3954 | "[qemu]\n" |
| 3955 | "path=%s\n" |
| 3956 | "read only=no\n" |
| 3957 | "guest ok=yes\n", |
| 3958 | smb_dir, |
| 3959 | smb_dir, |
| 3960 | smb_dir, |
| 3961 | smb_dir, |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3962 | smb_dir, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3963 | exported_dir |
| 3964 | ); |
| 3965 | fclose(f); |
| 3966 | atexit(smb_exit); |
| 3967 | |
pbrook | a14d6c8 | 2006-12-23 15:37:33 +0000 | [diff] [blame] | 3968 | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
| 3969 | SMBD_COMMAND, smb_conf); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 3970 | |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3971 | slirp_add_exec(0, smb_cmdline, 4, 139); |
| 3972 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3973 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3974 | #endif /* !defined(_WIN32) */ |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 3975 | void do_info_slirp(void) |
| 3976 | { |
| 3977 | slirp_stats(); |
| 3978 | } |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3979 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3980 | #endif /* CONFIG_SLIRP */ |
| 3981 | |
| 3982 | #if !defined(_WIN32) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3983 | |
| 3984 | typedef struct TAPState { |
| 3985 | VLANClientState *vc; |
| 3986 | int fd; |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 3987 | char down_script[1024]; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3988 | } TAPState; |
| 3989 | |
| 3990 | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
| 3991 | { |
| 3992 | TAPState *s = opaque; |
| 3993 | int ret; |
| 3994 | for(;;) { |
| 3995 | ret = write(s->fd, buf, size); |
| 3996 | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 3997 | } else { |
| 3998 | break; |
| 3999 | } |
| 4000 | } |
| 4001 | } |
| 4002 | |
| 4003 | static void tap_send(void *opaque) |
| 4004 | { |
| 4005 | TAPState *s = opaque; |
| 4006 | uint8_t buf[4096]; |
| 4007 | int size; |
| 4008 | |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4009 | #ifdef __sun__ |
| 4010 | struct strbuf sbuf; |
| 4011 | int f = 0; |
| 4012 | sbuf.maxlen = sizeof(buf); |
| 4013 | sbuf.buf = buf; |
| 4014 | size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
| 4015 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4016 | size = read(s->fd, buf, sizeof(buf)); |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4017 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4018 | if (size > 0) { |
| 4019 | qemu_send_packet(s->vc, buf, size); |
| 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | /* fd support */ |
| 4024 | |
| 4025 | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
| 4026 | { |
| 4027 | TAPState *s; |
| 4028 | |
| 4029 | s = qemu_mallocz(sizeof(TAPState)); |
| 4030 | if (!s) |
| 4031 | return NULL; |
| 4032 | s->fd = fd; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 4033 | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4034 | qemu_set_fd_handler(s->fd, tap_send, NULL, s); |
| 4035 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
| 4036 | return s; |
| 4037 | } |
| 4038 | |
ths | 5c40d2b | 2007-06-23 16:03:36 +0000 | [diff] [blame] | 4039 | #if defined (_BSD) || defined (__FreeBSD_kernel__) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4040 | static int tap_open(char *ifname, int ifname_size) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 4041 | { |
| 4042 | int fd; |
| 4043 | char *dev; |
| 4044 | struct stat s; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 4045 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 4046 | TFR(fd = open("/dev/tap", O_RDWR)); |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 4047 | if (fd < 0) { |
| 4048 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); |
| 4049 | return -1; |
| 4050 | } |
| 4051 | |
| 4052 | fstat(fd, &s); |
| 4053 | dev = devname(s.st_rdev, S_IFCHR); |
| 4054 | pstrcpy(ifname, ifname_size, dev); |
| 4055 | |
| 4056 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 4057 | return fd; |
| 4058 | } |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 4059 | #elif defined(__sun__) |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4060 | #define TUNNEWPPA (('T'<<16) | 0x0001) |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4061 | /* |
| 4062 | * Allocate TAP device, returns opened fd. |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4063 | * Stores dev name in the first arg(must be large enough). |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4064 | */ |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4065 | int tap_alloc(char *dev) |
| 4066 | { |
| 4067 | int tap_fd, if_fd, ppa = -1; |
| 4068 | static int ip_fd = 0; |
| 4069 | char *ptr; |
| 4070 | |
| 4071 | static int arp_fd = 0; |
| 4072 | int ip_muxid, arp_muxid; |
| 4073 | struct strioctl strioc_if, strioc_ppa; |
| 4074 | int link_type = I_PLINK;; |
| 4075 | struct lifreq ifr; |
| 4076 | char actual_name[32] = ""; |
| 4077 | |
| 4078 | memset(&ifr, 0x0, sizeof(ifr)); |
| 4079 | |
| 4080 | if( *dev ){ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4081 | ptr = dev; |
| 4082 | while( *ptr && !isdigit((int)*ptr) ) ptr++; |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4083 | ppa = atoi(ptr); |
| 4084 | } |
| 4085 | |
| 4086 | /* Check if IP device was opened */ |
| 4087 | if( ip_fd ) |
| 4088 | close(ip_fd); |
| 4089 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 4090 | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
| 4091 | if (ip_fd < 0) { |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4092 | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)"); |
| 4093 | return -1; |
| 4094 | } |
| 4095 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 4096 | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
| 4097 | if (tap_fd < 0) { |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4098 | syslog(LOG_ERR, "Can't open /dev/tap"); |
| 4099 | return -1; |
| 4100 | } |
| 4101 | |
| 4102 | /* Assign a new PPA and get its unit number. */ |
| 4103 | strioc_ppa.ic_cmd = TUNNEWPPA; |
| 4104 | strioc_ppa.ic_timout = 0; |
| 4105 | strioc_ppa.ic_len = sizeof(ppa); |
| 4106 | strioc_ppa.ic_dp = (char *)&ppa; |
| 4107 | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
| 4108 | syslog (LOG_ERR, "Can't assign new interface"); |
| 4109 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 4110 | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
| 4111 | if (if_fd < 0) { |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4112 | syslog(LOG_ERR, "Can't open /dev/tap (2)"); |
| 4113 | return -1; |
| 4114 | } |
| 4115 | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
| 4116 | syslog(LOG_ERR, "Can't push IP module"); |
| 4117 | return -1; |
| 4118 | } |
| 4119 | |
| 4120 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
| 4121 | syslog(LOG_ERR, "Can't get flags\n"); |
| 4122 | |
| 4123 | snprintf (actual_name, 32, "tap%d", ppa); |
| 4124 | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name)); |
| 4125 | |
| 4126 | ifr.lifr_ppa = ppa; |
| 4127 | /* Assign ppa according to the unit number returned by tun device */ |
| 4128 | |
| 4129 | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
| 4130 | syslog (LOG_ERR, "Can't set PPA %d", ppa); |
| 4131 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
| 4132 | syslog (LOG_ERR, "Can't get flags\n"); |
| 4133 | /* Push arp module to if_fd */ |
| 4134 | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
| 4135 | syslog (LOG_ERR, "Can't push ARP module (2)"); |
| 4136 | |
| 4137 | /* Push arp module to ip_fd */ |
| 4138 | if (ioctl (ip_fd, I_POP, NULL) < 0) |
| 4139 | syslog (LOG_ERR, "I_POP failed\n"); |
| 4140 | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
| 4141 | syslog (LOG_ERR, "Can't push ARP module (3)\n"); |
| 4142 | /* Open arp_fd */ |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 4143 | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
| 4144 | if (arp_fd < 0) |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4145 | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
| 4146 | |
| 4147 | /* Set ifname to arp */ |
| 4148 | strioc_if.ic_cmd = SIOCSLIFNAME; |
| 4149 | strioc_if.ic_timout = 0; |
| 4150 | strioc_if.ic_len = sizeof(ifr); |
| 4151 | strioc_if.ic_dp = (char *)𝔦 |
| 4152 | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
| 4153 | syslog (LOG_ERR, "Can't set ifname to arp\n"); |
| 4154 | } |
| 4155 | |
| 4156 | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
| 4157 | syslog(LOG_ERR, "Can't link TAP device to IP"); |
| 4158 | return -1; |
| 4159 | } |
| 4160 | |
| 4161 | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
| 4162 | syslog (LOG_ERR, "Can't link TAP device to ARP"); |
| 4163 | |
| 4164 | close (if_fd); |
| 4165 | |
| 4166 | memset(&ifr, 0x0, sizeof(ifr)); |
| 4167 | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name)); |
| 4168 | ifr.lifr_ip_muxid = ip_muxid; |
| 4169 | ifr.lifr_arp_muxid = arp_muxid; |
| 4170 | |
| 4171 | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
| 4172 | { |
| 4173 | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
| 4174 | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
| 4175 | syslog (LOG_ERR, "Can't set multiplexor id"); |
| 4176 | } |
| 4177 | |
| 4178 | sprintf(dev, "tap%d", ppa); |
| 4179 | return tap_fd; |
| 4180 | } |
| 4181 | |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 4182 | static int tap_open(char *ifname, int ifname_size) |
| 4183 | { |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 4184 | char dev[10]=""; |
| 4185 | int fd; |
| 4186 | if( (fd = tap_alloc(dev)) < 0 ){ |
| 4187 | fprintf(stderr, "Cannot allocate TAP device\n"); |
| 4188 | return -1; |
| 4189 | } |
| 4190 | pstrcpy(ifname, ifname_size, dev); |
| 4191 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 4192 | return fd; |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 4193 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 4194 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4195 | static int tap_open(char *ifname, int ifname_size) |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 4196 | { |
| 4197 | struct ifreq ifr; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4198 | int fd, ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4199 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 4200 | TFR(fd = open("/dev/net/tun", O_RDWR)); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 4201 | if (fd < 0) { |
| 4202 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); |
| 4203 | return -1; |
| 4204 | } |
| 4205 | memset(&ifr, 0, sizeof(ifr)); |
| 4206 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4207 | if (ifname[0] != '\0') |
| 4208 | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
| 4209 | else |
| 4210 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 4211 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); |
| 4212 | if (ret != 0) { |
| 4213 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); |
| 4214 | close(fd); |
| 4215 | return -1; |
| 4216 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4217 | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 4218 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4219 | return fd; |
| 4220 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 4221 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 4222 | |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4223 | static int launch_script(const char *setup_script, const char *ifname, int fd) |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4224 | { |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4225 | int pid, status; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4226 | char *args[3]; |
| 4227 | char **parg; |
| 4228 | |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4229 | /* try to launch network script */ |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4230 | pid = fork(); |
| 4231 | if (pid >= 0) { |
| 4232 | if (pid == 0) { |
ths | 50d3eea | 2007-03-19 16:36:43 +0000 | [diff] [blame] | 4233 | int open_max = sysconf (_SC_OPEN_MAX), i; |
| 4234 | for (i = 0; i < open_max; i++) |
| 4235 | if (i != STDIN_FILENO && |
| 4236 | i != STDOUT_FILENO && |
| 4237 | i != STDERR_FILENO && |
| 4238 | i != fd) |
| 4239 | close(i); |
| 4240 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4241 | parg = args; |
| 4242 | *parg++ = (char *)setup_script; |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4243 | *parg++ = (char *)ifname; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4244 | *parg++ = NULL; |
| 4245 | execv(setup_script, args); |
bellard | 4a38940 | 2005-11-26 20:10:07 +0000 | [diff] [blame] | 4246 | _exit(1); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4247 | } |
| 4248 | while (waitpid(pid, &status, 0) != pid); |
| 4249 | if (!WIFEXITED(status) || |
| 4250 | WEXITSTATUS(status) != 0) { |
| 4251 | fprintf(stderr, "%s: could not launch network script\n", |
| 4252 | setup_script); |
| 4253 | return -1; |
| 4254 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4255 | } |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4256 | return 0; |
| 4257 | } |
| 4258 | |
| 4259 | static int net_tap_init(VLANState *vlan, const char *ifname1, |
| 4260 | const char *setup_script, const char *down_script) |
| 4261 | { |
| 4262 | TAPState *s; |
| 4263 | int fd; |
| 4264 | char ifname[128]; |
| 4265 | |
| 4266 | if (ifname1 != NULL) |
| 4267 | pstrcpy(ifname, sizeof(ifname), ifname1); |
| 4268 | else |
| 4269 | ifname[0] = '\0'; |
| 4270 | TFR(fd = tap_open(ifname, sizeof(ifname))); |
| 4271 | if (fd < 0) |
| 4272 | return -1; |
| 4273 | |
| 4274 | if (!setup_script || !strcmp(setup_script, "no")) |
| 4275 | setup_script = ""; |
| 4276 | if (setup_script[0] != '\0') { |
| 4277 | if (launch_script(setup_script, ifname, fd)) |
| 4278 | return -1; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4279 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4280 | s = net_tap_fd_init(vlan, fd); |
| 4281 | if (!s) |
| 4282 | return -1; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4283 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4284 | "tap: ifname=%s setup_script=%s", ifname, setup_script); |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4285 | if (down_script && strcmp(down_script, "no")) |
| 4286 | snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4287 | return 0; |
| 4288 | } |
| 4289 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4290 | #endif /* !_WIN32 */ |
| 4291 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4292 | /* network connection */ |
| 4293 | typedef struct NetSocketState { |
| 4294 | VLANClientState *vc; |
| 4295 | int fd; |
| 4296 | int state; /* 0 = getting length, 1 = getting data */ |
| 4297 | int index; |
| 4298 | int packet_len; |
| 4299 | uint8_t buf[4096]; |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4300 | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4301 | } NetSocketState; |
| 4302 | |
| 4303 | typedef struct NetSocketListenState { |
| 4304 | VLANState *vlan; |
| 4305 | int fd; |
| 4306 | } NetSocketListenState; |
| 4307 | |
| 4308 | /* XXX: we consider we can send the whole packet without blocking */ |
| 4309 | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4310 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4311 | NetSocketState *s = opaque; |
| 4312 | uint32_t len; |
| 4313 | len = htonl(size); |
| 4314 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4315 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
| 4316 | send_all(s->fd, buf, size); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4319 | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
| 4320 | { |
| 4321 | NetSocketState *s = opaque; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4322 | sendto(s->fd, buf, size, 0, |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4323 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
| 4324 | } |
| 4325 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4326 | static void net_socket_send(void *opaque) |
| 4327 | { |
| 4328 | NetSocketState *s = opaque; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4329 | int l, size, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4330 | uint8_t buf1[4096]; |
| 4331 | const uint8_t *buf; |
| 4332 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4333 | size = recv(s->fd, buf1, sizeof(buf1), 0); |
| 4334 | if (size < 0) { |
| 4335 | err = socket_error(); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4336 | if (err != EWOULDBLOCK) |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4337 | goto eoc; |
| 4338 | } else if (size == 0) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4339 | /* end of connection */ |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4340 | eoc: |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4341 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4342 | closesocket(s->fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4343 | return; |
| 4344 | } |
| 4345 | buf = buf1; |
| 4346 | while (size > 0) { |
| 4347 | /* reassemble a packet from the network */ |
| 4348 | switch(s->state) { |
| 4349 | case 0: |
| 4350 | l = 4 - s->index; |
| 4351 | if (l > size) |
| 4352 | l = size; |
| 4353 | memcpy(s->buf + s->index, buf, l); |
| 4354 | buf += l; |
| 4355 | size -= l; |
| 4356 | s->index += l; |
| 4357 | if (s->index == 4) { |
| 4358 | /* got length */ |
| 4359 | s->packet_len = ntohl(*(uint32_t *)s->buf); |
| 4360 | s->index = 0; |
| 4361 | s->state = 1; |
| 4362 | } |
| 4363 | break; |
| 4364 | case 1: |
| 4365 | l = s->packet_len - s->index; |
| 4366 | if (l > size) |
| 4367 | l = size; |
| 4368 | memcpy(s->buf + s->index, buf, l); |
| 4369 | s->index += l; |
| 4370 | buf += l; |
| 4371 | size -= l; |
| 4372 | if (s->index >= s->packet_len) { |
| 4373 | qemu_send_packet(s->vc, s->buf, s->packet_len); |
| 4374 | s->index = 0; |
| 4375 | s->state = 0; |
| 4376 | } |
| 4377 | break; |
| 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4382 | static void net_socket_send_dgram(void *opaque) |
| 4383 | { |
| 4384 | NetSocketState *s = opaque; |
| 4385 | int size; |
| 4386 | |
| 4387 | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4388 | if (size < 0) |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4389 | return; |
| 4390 | if (size == 0) { |
| 4391 | /* end of connection */ |
| 4392 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 4393 | return; |
| 4394 | } |
| 4395 | qemu_send_packet(s->vc, s->buf, size); |
| 4396 | } |
| 4397 | |
| 4398 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
| 4399 | { |
| 4400 | struct ip_mreq imr; |
| 4401 | int fd; |
| 4402 | int val, ret; |
| 4403 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
| 4404 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4405 | inet_ntoa(mcastaddr->sin_addr), |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4406 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4407 | return -1; |
| 4408 | |
| 4409 | } |
| 4410 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 4411 | if (fd < 0) { |
| 4412 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 4413 | return -1; |
| 4414 | } |
| 4415 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4416 | val = 1; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4417 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4418 | (const char *)&val, sizeof(val)); |
| 4419 | if (ret < 0) { |
| 4420 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
| 4421 | goto fail; |
| 4422 | } |
| 4423 | |
| 4424 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
| 4425 | if (ret < 0) { |
| 4426 | perror("bind"); |
| 4427 | goto fail; |
| 4428 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4429 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4430 | /* Add host to multicast group */ |
| 4431 | imr.imr_multiaddr = mcastaddr->sin_addr; |
| 4432 | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 4433 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4434 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4435 | (const char *)&imr, sizeof(struct ip_mreq)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4436 | if (ret < 0) { |
| 4437 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
| 4438 | goto fail; |
| 4439 | } |
| 4440 | |
| 4441 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
| 4442 | val = 1; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4443 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4444 | (const char *)&val, sizeof(val)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4445 | if (ret < 0) { |
| 4446 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
| 4447 | goto fail; |
| 4448 | } |
| 4449 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4450 | socket_set_nonblock(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4451 | return fd; |
| 4452 | fail: |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4453 | if (fd >= 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 4454 | closesocket(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4455 | return -1; |
| 4456 | } |
| 4457 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4458 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4459 | int is_connected) |
| 4460 | { |
| 4461 | struct sockaddr_in saddr; |
| 4462 | int newfd; |
| 4463 | socklen_t saddr_len; |
| 4464 | NetSocketState *s; |
| 4465 | |
| 4466 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4467 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4468 | * by ONLY ONE process: we must "clone" this dgram socket --jjo |
| 4469 | */ |
| 4470 | |
| 4471 | if (is_connected) { |
| 4472 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
| 4473 | /* must be bound */ |
| 4474 | if (saddr.sin_addr.s_addr==0) { |
| 4475 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", |
| 4476 | fd); |
| 4477 | return NULL; |
| 4478 | } |
| 4479 | /* clone dgram socket */ |
| 4480 | newfd = net_socket_mcast_create(&saddr); |
| 4481 | if (newfd < 0) { |
| 4482 | /* error already reported by net_socket_mcast_create() */ |
| 4483 | close(fd); |
| 4484 | return NULL; |
| 4485 | } |
| 4486 | /* clone newfd to fd, close newfd */ |
| 4487 | dup2(newfd, fd); |
| 4488 | close(newfd); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4489 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4490 | } else { |
| 4491 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", |
| 4492 | fd, strerror(errno)); |
| 4493 | return NULL; |
| 4494 | } |
| 4495 | } |
| 4496 | |
| 4497 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 4498 | if (!s) |
| 4499 | return NULL; |
| 4500 | s->fd = fd; |
| 4501 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 4502 | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4503 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
| 4504 | |
| 4505 | /* mcast: save bound address as dst */ |
| 4506 | if (is_connected) s->dgram_dst=saddr; |
| 4507 | |
| 4508 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4509 | "socket: fd=%d (%s mcast=%s:%d)", |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4510 | fd, is_connected? "cloned" : "", |
| 4511 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 4512 | return s; |
| 4513 | } |
| 4514 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4515 | static void net_socket_connect(void *opaque) |
| 4516 | { |
| 4517 | NetSocketState *s = opaque; |
| 4518 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); |
| 4519 | } |
| 4520 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4521 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4522 | int is_connected) |
| 4523 | { |
| 4524 | NetSocketState *s; |
| 4525 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 4526 | if (!s) |
| 4527 | return NULL; |
| 4528 | s->fd = fd; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4529 | s->vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 4530 | net_socket_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4531 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 4532 | "socket: fd=%d", fd); |
| 4533 | if (is_connected) { |
| 4534 | net_socket_connect(s); |
| 4535 | } else { |
| 4536 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
| 4537 | } |
| 4538 | return s; |
| 4539 | } |
| 4540 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4541 | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4542 | int is_connected) |
| 4543 | { |
| 4544 | int so_type=-1, optlen=sizeof(so_type); |
| 4545 | |
ths | 69b3497 | 2007-12-17 03:15:52 +0000 | [diff] [blame] | 4546 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
| 4547 | (socklen_t *)&optlen)< 0) { |
ths | 931f03e | 2007-04-28 20:49:36 +0000 | [diff] [blame] | 4548 | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4549 | return NULL; |
| 4550 | } |
| 4551 | switch(so_type) { |
| 4552 | case SOCK_DGRAM: |
| 4553 | return net_socket_fd_init_dgram(vlan, fd, is_connected); |
| 4554 | case SOCK_STREAM: |
| 4555 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 4556 | default: |
| 4557 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ |
| 4558 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); |
| 4559 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 4560 | } |
| 4561 | return NULL; |
| 4562 | } |
| 4563 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4564 | static void net_socket_accept(void *opaque) |
| 4565 | { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4566 | NetSocketListenState *s = opaque; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4567 | NetSocketState *s1; |
| 4568 | struct sockaddr_in saddr; |
| 4569 | socklen_t len; |
| 4570 | int fd; |
| 4571 | |
| 4572 | for(;;) { |
| 4573 | len = sizeof(saddr); |
| 4574 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); |
| 4575 | if (fd < 0 && errno != EINTR) { |
| 4576 | return; |
| 4577 | } else if (fd >= 0) { |
| 4578 | break; |
| 4579 | } |
| 4580 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4581 | s1 = net_socket_fd_init(s->vlan, fd, 1); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4582 | if (!s1) { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 4583 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4584 | } else { |
| 4585 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4586 | "socket: connection from %s:%d", |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4587 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 4588 | } |
| 4589 | } |
| 4590 | |
| 4591 | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
| 4592 | { |
| 4593 | NetSocketListenState *s; |
| 4594 | int fd, val, ret; |
| 4595 | struct sockaddr_in saddr; |
| 4596 | |
| 4597 | if (parse_host_port(&saddr, host_str) < 0) |
| 4598 | return -1; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4599 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4600 | s = qemu_mallocz(sizeof(NetSocketListenState)); |
| 4601 | if (!s) |
| 4602 | return -1; |
| 4603 | |
| 4604 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 4605 | if (fd < 0) { |
| 4606 | perror("socket"); |
| 4607 | return -1; |
| 4608 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4609 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4610 | |
| 4611 | /* allow fast reuse */ |
| 4612 | val = 1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4613 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4614 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4615 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 4616 | if (ret < 0) { |
| 4617 | perror("bind"); |
| 4618 | return -1; |
| 4619 | } |
| 4620 | ret = listen(fd, 0); |
| 4621 | if (ret < 0) { |
| 4622 | perror("listen"); |
| 4623 | return -1; |
| 4624 | } |
| 4625 | s->vlan = vlan; |
| 4626 | s->fd = fd; |
| 4627 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); |
| 4628 | return 0; |
| 4629 | } |
| 4630 | |
| 4631 | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
| 4632 | { |
| 4633 | NetSocketState *s; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4634 | int fd, connected, ret, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4635 | struct sockaddr_in saddr; |
| 4636 | |
| 4637 | if (parse_host_port(&saddr, host_str) < 0) |
| 4638 | return -1; |
| 4639 | |
| 4640 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 4641 | if (fd < 0) { |
| 4642 | perror("socket"); |
| 4643 | return -1; |
| 4644 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4645 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4646 | |
| 4647 | connected = 0; |
| 4648 | for(;;) { |
| 4649 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 4650 | if (ret < 0) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4651 | err = socket_error(); |
| 4652 | if (err == EINTR || err == EWOULDBLOCK) { |
| 4653 | } else if (err == EINPROGRESS) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4654 | break; |
ths | f5b1226 | 2007-03-25 15:58:03 +0000 | [diff] [blame] | 4655 | #ifdef _WIN32 |
| 4656 | } else if (err == WSAEALREADY) { |
| 4657 | break; |
| 4658 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4659 | } else { |
| 4660 | perror("connect"); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4661 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4662 | return -1; |
| 4663 | } |
| 4664 | } else { |
| 4665 | connected = 1; |
| 4666 | break; |
| 4667 | } |
| 4668 | } |
| 4669 | s = net_socket_fd_init(vlan, fd, connected); |
| 4670 | if (!s) |
| 4671 | return -1; |
| 4672 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4673 | "socket: connect to %s:%d", |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4674 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4675 | return 0; |
| 4676 | } |
| 4677 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4678 | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
| 4679 | { |
| 4680 | NetSocketState *s; |
| 4681 | int fd; |
| 4682 | struct sockaddr_in saddr; |
| 4683 | |
| 4684 | if (parse_host_port(&saddr, host_str) < 0) |
| 4685 | return -1; |
| 4686 | |
| 4687 | |
| 4688 | fd = net_socket_mcast_create(&saddr); |
| 4689 | if (fd < 0) |
| 4690 | return -1; |
| 4691 | |
| 4692 | s = net_socket_fd_init(vlan, fd, 0); |
| 4693 | if (!s) |
| 4694 | return -1; |
| 4695 | |
| 4696 | s->dgram_dst = saddr; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4697 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4698 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 4699 | "socket: mcast=%s:%d", |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4700 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 4701 | return 0; |
| 4702 | |
| 4703 | } |
| 4704 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4705 | static const char *get_opt_name(char *buf, int buf_size, const char *p) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4706 | { |
| 4707 | char *q; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4708 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4709 | q = buf; |
| 4710 | while (*p != '\0' && *p != '=') { |
| 4711 | if (q && (q - buf) < buf_size - 1) |
| 4712 | *q++ = *p; |
| 4713 | p++; |
| 4714 | } |
| 4715 | if (q) |
| 4716 | *q = '\0'; |
| 4717 | |
| 4718 | return p; |
| 4719 | } |
| 4720 | |
| 4721 | static const char *get_opt_value(char *buf, int buf_size, const char *p) |
| 4722 | { |
| 4723 | char *q; |
| 4724 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4725 | q = buf; |
| 4726 | while (*p != '\0') { |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4727 | if (*p == ',') { |
| 4728 | if (*(p + 1) != ',') |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4729 | break; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4730 | p++; |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4731 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4732 | if (q && (q - buf) < buf_size - 1) |
| 4733 | *q++ = *p; |
| 4734 | p++; |
| 4735 | } |
| 4736 | if (q) |
| 4737 | *q = '\0'; |
| 4738 | |
| 4739 | return p; |
| 4740 | } |
| 4741 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4742 | static int get_param_value(char *buf, int buf_size, |
| 4743 | const char *tag, const char *str) |
| 4744 | { |
| 4745 | const char *p; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4746 | char option[128]; |
| 4747 | |
| 4748 | p = str; |
| 4749 | for(;;) { |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4750 | p = get_opt_name(option, sizeof(option), p); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4751 | if (*p != '=') |
| 4752 | break; |
| 4753 | p++; |
| 4754 | if (!strcmp(tag, option)) { |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4755 | (void)get_opt_value(buf, buf_size, p); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4756 | return strlen(buf); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4757 | } else { |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4758 | p = get_opt_value(NULL, 0, p); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4759 | } |
| 4760 | if (*p != ',') |
| 4761 | break; |
| 4762 | p++; |
| 4763 | } |
| 4764 | return 0; |
| 4765 | } |
| 4766 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4767 | static int check_params(char *buf, int buf_size, |
| 4768 | char **params, const char *str) |
| 4769 | { |
| 4770 | const char *p; |
| 4771 | int i; |
| 4772 | |
| 4773 | p = str; |
| 4774 | for(;;) { |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4775 | p = get_opt_name(buf, buf_size, p); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4776 | if (*p != '=') |
| 4777 | return -1; |
| 4778 | p++; |
| 4779 | for(i = 0; params[i] != NULL; i++) |
| 4780 | if (!strcmp(params[i], buf)) |
| 4781 | break; |
| 4782 | if (params[i] == NULL) |
| 4783 | return -1; |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4784 | p = get_opt_value(NULL, 0, p); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4785 | if (*p != ',') |
| 4786 | break; |
| 4787 | p++; |
| 4788 | } |
| 4789 | return 0; |
| 4790 | } |
| 4791 | |
| 4792 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 4793 | static int net_client_init(const char *str) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4794 | { |
| 4795 | const char *p; |
| 4796 | char *q; |
| 4797 | char device[64]; |
| 4798 | char buf[1024]; |
| 4799 | int vlan_id, ret; |
| 4800 | VLANState *vlan; |
| 4801 | |
| 4802 | p = str; |
| 4803 | q = device; |
| 4804 | while (*p != '\0' && *p != ',') { |
| 4805 | if ((q - device) < sizeof(device) - 1) |
| 4806 | *q++ = *p; |
| 4807 | p++; |
| 4808 | } |
| 4809 | *q = '\0'; |
| 4810 | if (*p == ',') |
| 4811 | p++; |
| 4812 | vlan_id = 0; |
| 4813 | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
| 4814 | vlan_id = strtol(buf, NULL, 0); |
| 4815 | } |
| 4816 | vlan = qemu_find_vlan(vlan_id); |
| 4817 | if (!vlan) { |
| 4818 | fprintf(stderr, "Could not create vlan %d\n", vlan_id); |
| 4819 | return -1; |
| 4820 | } |
| 4821 | if (!strcmp(device, "nic")) { |
| 4822 | NICInfo *nd; |
| 4823 | uint8_t *macaddr; |
| 4824 | |
| 4825 | if (nb_nics >= MAX_NICS) { |
| 4826 | fprintf(stderr, "Too Many NICs\n"); |
| 4827 | return -1; |
| 4828 | } |
| 4829 | nd = &nd_table[nb_nics]; |
| 4830 | macaddr = nd->macaddr; |
| 4831 | macaddr[0] = 0x52; |
| 4832 | macaddr[1] = 0x54; |
| 4833 | macaddr[2] = 0x00; |
| 4834 | macaddr[3] = 0x12; |
| 4835 | macaddr[4] = 0x34; |
| 4836 | macaddr[5] = 0x56 + nb_nics; |
| 4837 | |
| 4838 | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
| 4839 | if (parse_macaddr(macaddr, buf) < 0) { |
| 4840 | fprintf(stderr, "invalid syntax for ethernet address\n"); |
| 4841 | return -1; |
| 4842 | } |
| 4843 | } |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 4844 | if (get_param_value(buf, sizeof(buf), "model", p)) { |
| 4845 | nd->model = strdup(buf); |
| 4846 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4847 | nd->vlan = vlan; |
| 4848 | nb_nics++; |
blueswir1 | 833c717 | 2007-05-27 19:36:43 +0000 | [diff] [blame] | 4849 | vlan->nb_guest_devs++; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4850 | ret = 0; |
| 4851 | } else |
| 4852 | if (!strcmp(device, "none")) { |
| 4853 | /* does nothing. It is needed to signal that no network cards |
| 4854 | are wanted */ |
| 4855 | ret = 0; |
| 4856 | } else |
| 4857 | #ifdef CONFIG_SLIRP |
| 4858 | if (!strcmp(device, "user")) { |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 4859 | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
bellard | 3f423c9 | 2006-04-30 21:34:15 +0000 | [diff] [blame] | 4860 | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf); |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 4861 | } |
blueswir1 | 833c717 | 2007-05-27 19:36:43 +0000 | [diff] [blame] | 4862 | vlan->nb_host_devs++; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4863 | ret = net_slirp_init(vlan); |
| 4864 | } else |
| 4865 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 4866 | #ifdef _WIN32 |
| 4867 | if (!strcmp(device, "tap")) { |
| 4868 | char ifname[64]; |
| 4869 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
| 4870 | fprintf(stderr, "tap: no interface name\n"); |
| 4871 | return -1; |
| 4872 | } |
blueswir1 | 833c717 | 2007-05-27 19:36:43 +0000 | [diff] [blame] | 4873 | vlan->nb_host_devs++; |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 4874 | ret = tap_win32_init(vlan, ifname); |
| 4875 | } else |
| 4876 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4877 | if (!strcmp(device, "tap")) { |
| 4878 | char ifname[64]; |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4879 | char setup_script[1024], down_script[1024]; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4880 | int fd; |
pbrook | 4f01035 | 2007-05-28 02:29:59 +0000 | [diff] [blame] | 4881 | vlan->nb_host_devs++; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4882 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 4883 | fd = strtol(buf, NULL, 0); |
pbrook | 64538cd | 2008-03-24 02:31:33 +0000 | [diff] [blame] | 4884 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4885 | ret = -1; |
| 4886 | if (net_tap_fd_init(vlan, fd)) |
| 4887 | ret = 0; |
| 4888 | } else { |
bellard | bf8c534 | 2007-01-09 19:44:41 +0000 | [diff] [blame] | 4889 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
| 4890 | ifname[0] = '\0'; |
| 4891 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4892 | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
| 4893 | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT); |
| 4894 | } |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 4895 | if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { |
| 4896 | pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT); |
| 4897 | } |
| 4898 | ret = net_tap_init(vlan, ifname, setup_script, down_script); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4899 | } |
| 4900 | } else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4901 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4902 | if (!strcmp(device, "socket")) { |
| 4903 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 4904 | int fd; |
| 4905 | fd = strtol(buf, NULL, 0); |
| 4906 | ret = -1; |
| 4907 | if (net_socket_fd_init(vlan, fd, 1)) |
| 4908 | ret = 0; |
| 4909 | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
| 4910 | ret = net_socket_listen_init(vlan, buf); |
| 4911 | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
| 4912 | ret = net_socket_connect_init(vlan, buf); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4913 | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
| 4914 | ret = net_socket_mcast_init(vlan, buf); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4915 | } else { |
| 4916 | fprintf(stderr, "Unknown socket options: %s\n", p); |
| 4917 | return -1; |
| 4918 | } |
blueswir1 | 833c717 | 2007-05-27 19:36:43 +0000 | [diff] [blame] | 4919 | vlan->nb_host_devs++; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4920 | } else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4921 | { |
| 4922 | fprintf(stderr, "Unknown network device: %s\n", device); |
| 4923 | return -1; |
| 4924 | } |
| 4925 | if (ret < 0) { |
| 4926 | fprintf(stderr, "Could not initialize device '%s'\n", device); |
| 4927 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 4928 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4929 | return ret; |
| 4930 | } |
| 4931 | |
| 4932 | void do_info_network(void) |
| 4933 | { |
| 4934 | VLANState *vlan; |
| 4935 | VLANClientState *vc; |
| 4936 | |
| 4937 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 4938 | term_printf("VLAN %d devices:\n", vlan->id); |
| 4939 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
| 4940 | term_printf(" %s\n", vc->info_str); |
| 4941 | } |
| 4942 | } |
ths | 42550fd | 2006-12-22 16:34:12 +0000 | [diff] [blame] | 4943 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4944 | #define HD_ALIAS "index=%d,media=disk" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4945 | #ifdef TARGET_PPC |
| 4946 | #define CDROM_ALIAS "index=1,media=cdrom" |
| 4947 | #else |
| 4948 | #define CDROM_ALIAS "index=2,media=cdrom" |
| 4949 | #endif |
| 4950 | #define FD_ALIAS "index=%d,if=floppy" |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4951 | #define PFLASH_ALIAS "if=pflash" |
| 4952 | #define MTD_ALIAS "if=mtd" |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 4953 | #define SD_ALIAS "index=0,if=sd" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4954 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4955 | static int drive_add(const char *file, const char *fmt, ...) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4956 | { |
| 4957 | va_list ap; |
| 4958 | |
| 4959 | if (nb_drives_opt >= MAX_DRIVES) { |
| 4960 | fprintf(stderr, "qemu: too many drives\n"); |
| 4961 | exit(1); |
| 4962 | } |
| 4963 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4964 | drives_opt[nb_drives_opt].file = file; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4965 | va_start(ap, fmt); |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 4966 | vsnprintf(drives_opt[nb_drives_opt].opt, |
| 4967 | sizeof(drives_opt[0].opt), fmt, ap); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4968 | va_end(ap); |
| 4969 | |
| 4970 | return nb_drives_opt++; |
| 4971 | } |
| 4972 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 4973 | int drive_get_index(BlockInterfaceType type, int bus, int unit) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4974 | { |
| 4975 | int index; |
| 4976 | |
| 4977 | /* seek interface, bus and unit */ |
| 4978 | |
| 4979 | for (index = 0; index < nb_drives; index++) |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 4980 | if (drives_table[index].type == type && |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4981 | drives_table[index].bus == bus && |
| 4982 | drives_table[index].unit == unit) |
| 4983 | return index; |
| 4984 | |
| 4985 | return -1; |
| 4986 | } |
| 4987 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 4988 | int drive_get_max_bus(BlockInterfaceType type) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4989 | { |
| 4990 | int max_bus; |
| 4991 | int index; |
| 4992 | |
| 4993 | max_bus = -1; |
| 4994 | for (index = 0; index < nb_drives; index++) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 4995 | if(drives_table[index].type == type && |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 4996 | drives_table[index].bus > max_bus) |
| 4997 | max_bus = drives_table[index].bus; |
| 4998 | } |
| 4999 | return max_bus; |
| 5000 | } |
| 5001 | |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 5002 | static void bdrv_format_print(void *opaque, const char *name) |
| 5003 | { |
| 5004 | fprintf(stderr, " %s", name); |
| 5005 | } |
| 5006 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5007 | static int drive_init(struct drive_opt *arg, int snapshot, |
| 5008 | QEMUMachine *machine) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5009 | { |
| 5010 | char buf[128]; |
| 5011 | char file[1024]; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 5012 | char devname[128]; |
| 5013 | const char *mediastr = ""; |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5014 | BlockInterfaceType type; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5015 | enum { MEDIA_DISK, MEDIA_CDROM } media; |
| 5016 | int bus_id, unit_id; |
| 5017 | int cyls, heads, secs, translation; |
| 5018 | BlockDriverState *bdrv; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 5019 | BlockDriver *drv = NULL; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5020 | int max_devs; |
| 5021 | int index; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 5022 | int cache; |
| 5023 | int bdrv_flags; |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5024 | char *str = arg->opt; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5025 | char *params[] = { "bus", "unit", "if", "index", "cyls", "heads", |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 5026 | "secs", "trans", "media", "snapshot", "file", |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 5027 | "cache", "format", NULL }; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5028 | |
| 5029 | if (check_params(buf, sizeof(buf), params, str) < 0) { |
balrog | ff99363 | 2008-02-10 13:21:25 +0000 | [diff] [blame] | 5030 | fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n", |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5031 | buf, str); |
| 5032 | return -1; |
| 5033 | } |
| 5034 | |
| 5035 | file[0] = 0; |
| 5036 | cyls = heads = secs = 0; |
| 5037 | bus_id = 0; |
| 5038 | unit_id = -1; |
| 5039 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 5040 | index = -1; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 5041 | cache = 1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5042 | |
| 5043 | if (!strcmp(machine->name, "realview") || |
| 5044 | !strcmp(machine->name, "SS-5") || |
| 5045 | !strcmp(machine->name, "SS-10") || |
| 5046 | !strcmp(machine->name, "SS-600MP") || |
| 5047 | !strcmp(machine->name, "versatilepb") || |
| 5048 | !strcmp(machine->name, "versatileab")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5049 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5050 | max_devs = MAX_SCSI_DEVS; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 5051 | strcpy(devname, "scsi"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5052 | } else { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5053 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5054 | max_devs = MAX_IDE_DEVS; |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 5055 | strcpy(devname, "ide"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5056 | } |
| 5057 | media = MEDIA_DISK; |
| 5058 | |
| 5059 | /* extract parameters */ |
| 5060 | |
| 5061 | if (get_param_value(buf, sizeof(buf), "bus", str)) { |
| 5062 | bus_id = strtol(buf, NULL, 0); |
| 5063 | if (bus_id < 0) { |
| 5064 | fprintf(stderr, "qemu: '%s' invalid bus id\n", str); |
| 5065 | return -1; |
| 5066 | } |
| 5067 | } |
| 5068 | |
| 5069 | if (get_param_value(buf, sizeof(buf), "unit", str)) { |
| 5070 | unit_id = strtol(buf, NULL, 0); |
| 5071 | if (unit_id < 0) { |
| 5072 | fprintf(stderr, "qemu: '%s' invalid unit id\n", str); |
| 5073 | return -1; |
| 5074 | } |
| 5075 | } |
| 5076 | |
| 5077 | if (get_param_value(buf, sizeof(buf), "if", str)) { |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 5078 | strncpy(devname, buf, sizeof(devname)); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5079 | if (!strcmp(buf, "ide")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5080 | type = IF_IDE; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5081 | max_devs = MAX_IDE_DEVS; |
| 5082 | } else if (!strcmp(buf, "scsi")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5083 | type = IF_SCSI; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5084 | max_devs = MAX_SCSI_DEVS; |
| 5085 | } else if (!strcmp(buf, "floppy")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5086 | type = IF_FLOPPY; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5087 | max_devs = 0; |
| 5088 | } else if (!strcmp(buf, "pflash")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5089 | type = IF_PFLASH; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5090 | max_devs = 0; |
| 5091 | } else if (!strcmp(buf, "mtd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5092 | type = IF_MTD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5093 | max_devs = 0; |
| 5094 | } else if (!strcmp(buf, "sd")) { |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5095 | type = IF_SD; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5096 | max_devs = 0; |
| 5097 | } else { |
| 5098 | fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf); |
| 5099 | return -1; |
| 5100 | } |
| 5101 | } |
| 5102 | |
| 5103 | if (get_param_value(buf, sizeof(buf), "index", str)) { |
| 5104 | index = strtol(buf, NULL, 0); |
| 5105 | if (index < 0) { |
| 5106 | fprintf(stderr, "qemu: '%s' invalid index\n", str); |
| 5107 | return -1; |
| 5108 | } |
| 5109 | } |
| 5110 | |
| 5111 | if (get_param_value(buf, sizeof(buf), "cyls", str)) { |
| 5112 | cyls = strtol(buf, NULL, 0); |
| 5113 | } |
| 5114 | |
| 5115 | if (get_param_value(buf, sizeof(buf), "heads", str)) { |
| 5116 | heads = strtol(buf, NULL, 0); |
| 5117 | } |
| 5118 | |
| 5119 | if (get_param_value(buf, sizeof(buf), "secs", str)) { |
| 5120 | secs = strtol(buf, NULL, 0); |
| 5121 | } |
| 5122 | |
| 5123 | if (cyls || heads || secs) { |
| 5124 | if (cyls < 1 || cyls > 16383) { |
| 5125 | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str); |
| 5126 | return -1; |
| 5127 | } |
| 5128 | if (heads < 1 || heads > 16) { |
| 5129 | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str); |
| 5130 | return -1; |
| 5131 | } |
| 5132 | if (secs < 1 || secs > 63) { |
| 5133 | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str); |
| 5134 | return -1; |
| 5135 | } |
| 5136 | } |
| 5137 | |
| 5138 | if (get_param_value(buf, sizeof(buf), "trans", str)) { |
| 5139 | if (!cyls) { |
| 5140 | fprintf(stderr, |
| 5141 | "qemu: '%s' trans must be used with cyls,heads and secs\n", |
| 5142 | str); |
| 5143 | return -1; |
| 5144 | } |
| 5145 | if (!strcmp(buf, "none")) |
| 5146 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 5147 | else if (!strcmp(buf, "lba")) |
| 5148 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 5149 | else if (!strcmp(buf, "auto")) |
| 5150 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 5151 | else { |
| 5152 | fprintf(stderr, "qemu: '%s' invalid translation type\n", str); |
| 5153 | return -1; |
| 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | if (get_param_value(buf, sizeof(buf), "media", str)) { |
| 5158 | if (!strcmp(buf, "disk")) { |
| 5159 | media = MEDIA_DISK; |
| 5160 | } else if (!strcmp(buf, "cdrom")) { |
| 5161 | if (cyls || secs || heads) { |
| 5162 | fprintf(stderr, |
| 5163 | "qemu: '%s' invalid physical CHS format\n", str); |
| 5164 | return -1; |
| 5165 | } |
| 5166 | media = MEDIA_CDROM; |
| 5167 | } else { |
| 5168 | fprintf(stderr, "qemu: '%s' invalid media\n", str); |
| 5169 | return -1; |
| 5170 | } |
| 5171 | } |
| 5172 | |
| 5173 | if (get_param_value(buf, sizeof(buf), "snapshot", str)) { |
| 5174 | if (!strcmp(buf, "on")) |
| 5175 | snapshot = 1; |
| 5176 | else if (!strcmp(buf, "off")) |
| 5177 | snapshot = 0; |
| 5178 | else { |
| 5179 | fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str); |
| 5180 | return -1; |
| 5181 | } |
| 5182 | } |
| 5183 | |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 5184 | if (get_param_value(buf, sizeof(buf), "cache", str)) { |
| 5185 | if (!strcmp(buf, "off")) |
| 5186 | cache = 0; |
| 5187 | else if (!strcmp(buf, "on")) |
| 5188 | cache = 1; |
| 5189 | else { |
| 5190 | fprintf(stderr, "qemu: invalid cache option\n"); |
| 5191 | return -1; |
| 5192 | } |
| 5193 | } |
| 5194 | |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 5195 | if (get_param_value(buf, sizeof(buf), "format", str)) { |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 5196 | if (strcmp(buf, "?") == 0) { |
| 5197 | fprintf(stderr, "qemu: Supported formats:"); |
| 5198 | bdrv_iterate_format(bdrv_format_print, NULL); |
| 5199 | fprintf(stderr, "\n"); |
| 5200 | return -1; |
| 5201 | } |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 5202 | drv = bdrv_find_format(buf); |
| 5203 | if (!drv) { |
| 5204 | fprintf(stderr, "qemu: '%s' invalid format\n", buf); |
| 5205 | return -1; |
| 5206 | } |
| 5207 | } |
| 5208 | |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 5209 | if (arg->file == NULL) |
| 5210 | get_param_value(file, sizeof(file), "file", str); |
| 5211 | else |
| 5212 | pstrcpy(file, sizeof(file), arg->file); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5213 | |
| 5214 | /* compute bus and unit according index */ |
| 5215 | |
| 5216 | if (index != -1) { |
| 5217 | if (bus_id != 0 || unit_id != -1) { |
| 5218 | fprintf(stderr, |
| 5219 | "qemu: '%s' index cannot be used with bus and unit\n", str); |
| 5220 | return -1; |
| 5221 | } |
| 5222 | if (max_devs == 0) |
| 5223 | { |
| 5224 | unit_id = index; |
| 5225 | bus_id = 0; |
| 5226 | } else { |
| 5227 | unit_id = index % max_devs; |
| 5228 | bus_id = index / max_devs; |
| 5229 | } |
| 5230 | } |
| 5231 | |
| 5232 | /* if user doesn't specify a unit_id, |
| 5233 | * try to find the first free |
| 5234 | */ |
| 5235 | |
| 5236 | if (unit_id == -1) { |
| 5237 | unit_id = 0; |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5238 | while (drive_get_index(type, bus_id, unit_id) != -1) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5239 | unit_id++; |
| 5240 | if (max_devs && unit_id >= max_devs) { |
| 5241 | unit_id -= max_devs; |
| 5242 | bus_id++; |
| 5243 | } |
| 5244 | } |
| 5245 | } |
| 5246 | |
| 5247 | /* check unit id */ |
| 5248 | |
| 5249 | if (max_devs && unit_id >= max_devs) { |
| 5250 | fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n", |
| 5251 | str, unit_id, max_devs - 1); |
| 5252 | return -1; |
| 5253 | } |
| 5254 | |
| 5255 | /* |
| 5256 | * ignore multiple definitions |
| 5257 | */ |
| 5258 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5259 | if (drive_get_index(type, bus_id, unit_id) != -1) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5260 | return 0; |
| 5261 | |
| 5262 | /* init */ |
| 5263 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5264 | if (type == IF_IDE || type == IF_SCSI) |
balrog | c8522bd | 2007-12-06 22:11:20 +0000 | [diff] [blame] | 5265 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
balrog | e6198a7 | 2007-12-24 13:58:47 +0000 | [diff] [blame] | 5266 | if (max_devs) |
| 5267 | snprintf(buf, sizeof(buf), "%s%i%s%i", |
| 5268 | devname, bus_id, mediastr, unit_id); |
| 5269 | else |
| 5270 | snprintf(buf, sizeof(buf), "%s%s%i", |
| 5271 | devname, mediastr, unit_id); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5272 | bdrv = bdrv_new(buf); |
| 5273 | drives_table[nb_drives].bdrv = bdrv; |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5274 | drives_table[nb_drives].type = type; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5275 | drives_table[nb_drives].bus = bus_id; |
| 5276 | drives_table[nb_drives].unit = unit_id; |
| 5277 | nb_drives++; |
| 5278 | |
ths | f60d39b | 2007-12-17 03:55:57 +0000 | [diff] [blame] | 5279 | switch(type) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5280 | case IF_IDE: |
| 5281 | case IF_SCSI: |
| 5282 | switch(media) { |
| 5283 | case MEDIA_DISK: |
| 5284 | if (cyls != 0) { |
| 5285 | bdrv_set_geometry_hint(bdrv, cyls, heads, secs); |
| 5286 | bdrv_set_translation_hint(bdrv, translation); |
| 5287 | } |
| 5288 | break; |
| 5289 | case MEDIA_CDROM: |
| 5290 | bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); |
| 5291 | break; |
| 5292 | } |
| 5293 | break; |
| 5294 | case IF_SD: |
| 5295 | /* FIXME: This isn't really a floppy, but it's a reasonable |
| 5296 | approximation. */ |
| 5297 | case IF_FLOPPY: |
| 5298 | bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); |
| 5299 | break; |
| 5300 | case IF_PFLASH: |
| 5301 | case IF_MTD: |
| 5302 | break; |
| 5303 | } |
| 5304 | if (!file[0]) |
| 5305 | return 0; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 5306 | bdrv_flags = 0; |
| 5307 | if (snapshot) |
| 5308 | bdrv_flags |= BDRV_O_SNAPSHOT; |
| 5309 | if (!cache) |
| 5310 | bdrv_flags |= BDRV_O_DIRECT; |
aurel32 | 1e72d3b | 2008-04-28 20:26:45 +0000 | [diff] [blame] | 5311 | if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 5312 | fprintf(stderr, "qemu: could not open disk image %s\n", |
| 5313 | file); |
| 5314 | return -1; |
| 5315 | } |
| 5316 | return 0; |
| 5317 | } |
| 5318 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 5319 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5320 | /* USB devices */ |
| 5321 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5322 | static USBPort *used_usb_ports; |
| 5323 | static USBPort *free_usb_ports; |
| 5324 | |
| 5325 | /* ??? Maybe change this to register a hub to keep track of the topology. */ |
| 5326 | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
| 5327 | usb_attachfn attach) |
| 5328 | { |
| 5329 | port->opaque = opaque; |
| 5330 | port->index = index; |
| 5331 | port->attach = attach; |
| 5332 | port->next = free_usb_ports; |
| 5333 | free_usb_ports = port; |
| 5334 | } |
| 5335 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5336 | static int usb_device_add(const char *devname) |
| 5337 | { |
| 5338 | const char *p; |
| 5339 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5340 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5341 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5342 | if (!free_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5343 | return -1; |
| 5344 | |
| 5345 | if (strstart(devname, "host:", &p)) { |
| 5346 | dev = usb_host_device_open(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5347 | } else if (!strcmp(devname, "mouse")) { |
| 5348 | dev = usb_mouse_init(); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 5349 | } else if (!strcmp(devname, "tablet")) { |
balrog | 47b2d33 | 2007-06-22 08:16:00 +0000 | [diff] [blame] | 5350 | dev = usb_tablet_init(); |
| 5351 | } else if (!strcmp(devname, "keyboard")) { |
| 5352 | dev = usb_keyboard_init(); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 5353 | } else if (strstart(devname, "disk:", &p)) { |
| 5354 | dev = usb_msd_init(p); |
balrog | f6d2a31 | 2007-06-10 19:21:04 +0000 | [diff] [blame] | 5355 | } else if (!strcmp(devname, "wacom-tablet")) { |
| 5356 | dev = usb_wacom_init(); |
balrog | a795421 | 2008-01-14 03:41:02 +0000 | [diff] [blame] | 5357 | } else if (strstart(devname, "serial:", &p)) { |
| 5358 | dev = usb_serial_init(p); |
aurel32 | 2e4d9fb | 2008-04-08 06:01:02 +0000 | [diff] [blame] | 5359 | #ifdef CONFIG_BRLAPI |
| 5360 | } else if (!strcmp(devname, "braille")) { |
| 5361 | dev = usb_baum_init(); |
| 5362 | #endif |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5363 | } else { |
| 5364 | return -1; |
| 5365 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5366 | if (!dev) |
| 5367 | return -1; |
| 5368 | |
| 5369 | /* Find a USB port to add the device to. */ |
| 5370 | port = free_usb_ports; |
| 5371 | if (!port->next) { |
| 5372 | USBDevice *hub; |
| 5373 | |
| 5374 | /* Create a new hub and chain it on. */ |
| 5375 | free_usb_ports = NULL; |
| 5376 | port->next = used_usb_ports; |
| 5377 | used_usb_ports = port; |
| 5378 | |
| 5379 | hub = usb_hub_init(VM_USB_HUB_SIZE); |
| 5380 | usb_attach(port, hub); |
| 5381 | port = free_usb_ports; |
| 5382 | } |
| 5383 | |
| 5384 | free_usb_ports = port->next; |
| 5385 | port->next = used_usb_ports; |
| 5386 | used_usb_ports = port; |
| 5387 | usb_attach(port, dev); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5388 | return 0; |
| 5389 | } |
| 5390 | |
| 5391 | static int usb_device_del(const char *devname) |
| 5392 | { |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5393 | USBPort *port; |
| 5394 | USBPort **lastp; |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 5395 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5396 | int bus_num, addr; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5397 | const char *p; |
| 5398 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5399 | if (!used_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5400 | return -1; |
| 5401 | |
| 5402 | p = strchr(devname, '.'); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5403 | if (!p) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5404 | return -1; |
| 5405 | bus_num = strtoul(devname, NULL, 0); |
| 5406 | addr = strtoul(p + 1, NULL, 0); |
| 5407 | if (bus_num != 0) |
| 5408 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5409 | |
| 5410 | lastp = &used_usb_ports; |
| 5411 | port = used_usb_ports; |
| 5412 | while (port && port->dev->addr != addr) { |
| 5413 | lastp = &port->next; |
| 5414 | port = port->next; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5415 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5416 | |
| 5417 | if (!port) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5418 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5419 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 5420 | dev = port->dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5421 | *lastp = port->next; |
| 5422 | usb_attach(port, NULL); |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 5423 | dev->handle_destroy(dev); |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5424 | port->next = free_usb_ports; |
| 5425 | free_usb_ports = port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5426 | return 0; |
| 5427 | } |
| 5428 | |
| 5429 | void do_usb_add(const char *devname) |
| 5430 | { |
| 5431 | int ret; |
| 5432 | ret = usb_device_add(devname); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5433 | if (ret < 0) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5434 | term_printf("Could not add USB device '%s'\n", devname); |
| 5435 | } |
| 5436 | |
| 5437 | void do_usb_del(const char *devname) |
| 5438 | { |
| 5439 | int ret; |
| 5440 | ret = usb_device_del(devname); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5441 | if (ret < 0) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5442 | term_printf("Could not remove USB device '%s'\n", devname); |
| 5443 | } |
| 5444 | |
| 5445 | void usb_info(void) |
| 5446 | { |
| 5447 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5448 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5449 | const char *speed_str; |
| 5450 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5451 | if (!usb_enabled) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5452 | term_printf("USB support not enabled\n"); |
| 5453 | return; |
| 5454 | } |
| 5455 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5456 | for (port = used_usb_ports; port; port = port->next) { |
| 5457 | dev = port->dev; |
| 5458 | if (!dev) |
| 5459 | continue; |
| 5460 | switch(dev->speed) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5461 | case USB_SPEED_LOW: |
| 5462 | speed_str = "1.5"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5463 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5464 | case USB_SPEED_FULL: |
| 5465 | speed_str = "12"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5466 | break; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5467 | case USB_SPEED_HIGH: |
| 5468 | speed_str = "480"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5469 | break; |
| 5470 | default: |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5471 | speed_str = "?"; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 5472 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5473 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5474 | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n", |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 5475 | 0, dev->addr, speed_str, dev->devname); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 5476 | } |
| 5477 | } |
| 5478 | |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 5479 | /***********************************************************/ |
balrog | 201a51f | 2007-04-30 00:51:09 +0000 | [diff] [blame] | 5480 | /* PCMCIA/Cardbus */ |
| 5481 | |
| 5482 | static struct pcmcia_socket_entry_s { |
| 5483 | struct pcmcia_socket_s *socket; |
| 5484 | struct pcmcia_socket_entry_s *next; |
| 5485 | } *pcmcia_sockets = 0; |
| 5486 | |
| 5487 | void pcmcia_socket_register(struct pcmcia_socket_s *socket) |
| 5488 | { |
| 5489 | struct pcmcia_socket_entry_s *entry; |
| 5490 | |
| 5491 | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
| 5492 | entry->socket = socket; |
| 5493 | entry->next = pcmcia_sockets; |
| 5494 | pcmcia_sockets = entry; |
| 5495 | } |
| 5496 | |
| 5497 | void pcmcia_socket_unregister(struct pcmcia_socket_s *socket) |
| 5498 | { |
| 5499 | struct pcmcia_socket_entry_s *entry, **ptr; |
| 5500 | |
| 5501 | ptr = &pcmcia_sockets; |
| 5502 | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr) |
| 5503 | if (entry->socket == socket) { |
| 5504 | *ptr = entry->next; |
| 5505 | qemu_free(entry); |
| 5506 | } |
| 5507 | } |
| 5508 | |
| 5509 | void pcmcia_info(void) |
| 5510 | { |
| 5511 | struct pcmcia_socket_entry_s *iter; |
| 5512 | if (!pcmcia_sockets) |
| 5513 | term_printf("No PCMCIA sockets\n"); |
| 5514 | |
| 5515 | for (iter = pcmcia_sockets; iter; iter = iter->next) |
| 5516 | term_printf("%s: %s\n", iter->socket->slot_string, |
| 5517 | iter->socket->attached ? iter->socket->card_string : |
| 5518 | "Empty"); |
| 5519 | } |
| 5520 | |
| 5521 | /***********************************************************/ |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 5522 | /* dumb display */ |
| 5523 | |
| 5524 | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
| 5525 | { |
| 5526 | } |
| 5527 | |
| 5528 | static void dumb_resize(DisplayState *ds, int w, int h) |
| 5529 | { |
| 5530 | } |
| 5531 | |
| 5532 | static void dumb_refresh(DisplayState *ds) |
| 5533 | { |
| 5534 | #if defined(CONFIG_SDL) |
| 5535 | vga_hw_update(); |
| 5536 | #endif |
| 5537 | } |
| 5538 | |
| 5539 | static void dumb_display_init(DisplayState *ds) |
| 5540 | { |
| 5541 | ds->data = NULL; |
| 5542 | ds->linesize = 0; |
| 5543 | ds->depth = 0; |
| 5544 | ds->dpy_update = dumb_update; |
| 5545 | ds->dpy_resize = dumb_resize; |
| 5546 | ds->dpy_refresh = dumb_refresh; |
| 5547 | } |
| 5548 | |
| 5549 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5550 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5551 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5552 | #define MAX_IO_HANDLERS 64 |
| 5553 | |
| 5554 | typedef struct IOHandlerRecord { |
| 5555 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5556 | IOCanRWHandler *fd_read_poll; |
| 5557 | IOHandler *fd_read; |
| 5558 | IOHandler *fd_write; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 5559 | int deleted; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5560 | void *opaque; |
| 5561 | /* temporary data */ |
| 5562 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5563 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5564 | } IOHandlerRecord; |
| 5565 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5566 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5567 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5568 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 5569 | necessary in the character devices to suppress fd_can_read(). */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5570 | int qemu_set_fd_handler2(int fd, |
| 5571 | IOCanRWHandler *fd_read_poll, |
| 5572 | IOHandler *fd_read, |
| 5573 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5574 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 5575 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5576 | IOHandlerRecord **pioh, *ioh; |
| 5577 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5578 | if (!fd_read && !fd_write) { |
| 5579 | pioh = &first_io_handler; |
| 5580 | for(;;) { |
| 5581 | ioh = *pioh; |
| 5582 | if (ioh == NULL) |
| 5583 | break; |
| 5584 | if (ioh->fd == fd) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 5585 | ioh->deleted = 1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5586 | break; |
| 5587 | } |
| 5588 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5589 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5590 | } else { |
| 5591 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 5592 | if (ioh->fd == fd) |
| 5593 | goto found; |
| 5594 | } |
| 5595 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
| 5596 | if (!ioh) |
| 5597 | return -1; |
| 5598 | ioh->next = first_io_handler; |
| 5599 | first_io_handler = ioh; |
| 5600 | found: |
| 5601 | ioh->fd = fd; |
| 5602 | ioh->fd_read_poll = fd_read_poll; |
| 5603 | ioh->fd_read = fd_read; |
| 5604 | ioh->fd_write = fd_write; |
| 5605 | ioh->opaque = opaque; |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 5606 | ioh->deleted = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5607 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5608 | return 0; |
| 5609 | } |
| 5610 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5611 | int qemu_set_fd_handler(int fd, |
| 5612 | IOHandler *fd_read, |
| 5613 | IOHandler *fd_write, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 5614 | void *opaque) |
| 5615 | { |
| 5616 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 5617 | } |
| 5618 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5619 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 5620 | /* Polling handling */ |
| 5621 | |
| 5622 | typedef struct PollingEntry { |
| 5623 | PollingFunc *func; |
| 5624 | void *opaque; |
| 5625 | struct PollingEntry *next; |
| 5626 | } PollingEntry; |
| 5627 | |
| 5628 | static PollingEntry *first_polling_entry; |
| 5629 | |
| 5630 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 5631 | { |
| 5632 | PollingEntry **ppe, *pe; |
| 5633 | pe = qemu_mallocz(sizeof(PollingEntry)); |
| 5634 | if (!pe) |
| 5635 | return -1; |
| 5636 | pe->func = func; |
| 5637 | pe->opaque = opaque; |
| 5638 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 5639 | *ppe = pe; |
| 5640 | return 0; |
| 5641 | } |
| 5642 | |
| 5643 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 5644 | { |
| 5645 | PollingEntry **ppe, *pe; |
| 5646 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 5647 | pe = *ppe; |
| 5648 | if (pe->func == func && pe->opaque == opaque) { |
| 5649 | *ppe = pe->next; |
| 5650 | qemu_free(pe); |
| 5651 | break; |
| 5652 | } |
| 5653 | } |
| 5654 | } |
| 5655 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 5656 | #ifdef _WIN32 |
| 5657 | /***********************************************************/ |
| 5658 | /* Wait objects support */ |
| 5659 | typedef struct WaitObjects { |
| 5660 | int num; |
| 5661 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 5662 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 5663 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 5664 | } WaitObjects; |
| 5665 | |
| 5666 | static WaitObjects wait_objects = {0}; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5667 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 5668 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 5669 | { |
| 5670 | WaitObjects *w = &wait_objects; |
| 5671 | |
| 5672 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 5673 | return -1; |
| 5674 | w->events[w->num] = handle; |
| 5675 | w->func[w->num] = func; |
| 5676 | w->opaque[w->num] = opaque; |
| 5677 | w->num++; |
| 5678 | return 0; |
| 5679 | } |
| 5680 | |
| 5681 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 5682 | { |
| 5683 | int i, found; |
| 5684 | WaitObjects *w = &wait_objects; |
| 5685 | |
| 5686 | found = 0; |
| 5687 | for (i = 0; i < w->num; i++) { |
| 5688 | if (w->events[i] == handle) |
| 5689 | found = 1; |
| 5690 | if (found) { |
| 5691 | w->events[i] = w->events[i + 1]; |
| 5692 | w->func[i] = w->func[i + 1]; |
| 5693 | w->opaque[i] = w->opaque[i + 1]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 5694 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 5695 | } |
| 5696 | if (found) |
| 5697 | w->num--; |
| 5698 | } |
| 5699 | #endif |
| 5700 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 5701 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5702 | /* savevm/loadvm support */ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 5703 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5704 | #define IO_BUF_SIZE 32768 |
| 5705 | |
| 5706 | struct QEMUFile { |
| 5707 | FILE *outfile; |
| 5708 | BlockDriverState *bs; |
| 5709 | int is_file; |
| 5710 | int is_writable; |
| 5711 | int64_t base_offset; |
| 5712 | int64_t buf_offset; /* start of buffer when writing, end of buffer |
| 5713 | when reading */ |
| 5714 | int buf_index; |
| 5715 | int buf_size; /* 0 when writing */ |
| 5716 | uint8_t buf[IO_BUF_SIZE]; |
| 5717 | }; |
| 5718 | |
| 5719 | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
| 5720 | { |
| 5721 | QEMUFile *f; |
| 5722 | |
| 5723 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 5724 | if (!f) |
| 5725 | return NULL; |
| 5726 | if (!strcmp(mode, "wb")) { |
| 5727 | f->is_writable = 1; |
| 5728 | } else if (!strcmp(mode, "rb")) { |
| 5729 | f->is_writable = 0; |
| 5730 | } else { |
| 5731 | goto fail; |
| 5732 | } |
| 5733 | f->outfile = fopen(filename, mode); |
| 5734 | if (!f->outfile) |
| 5735 | goto fail; |
| 5736 | f->is_file = 1; |
| 5737 | return f; |
| 5738 | fail: |
| 5739 | if (f->outfile) |
| 5740 | fclose(f->outfile); |
| 5741 | qemu_free(f); |
| 5742 | return NULL; |
| 5743 | } |
| 5744 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 5745 | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5746 | { |
| 5747 | QEMUFile *f; |
| 5748 | |
| 5749 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 5750 | if (!f) |
| 5751 | return NULL; |
| 5752 | f->is_file = 0; |
| 5753 | f->bs = bs; |
| 5754 | f->is_writable = is_writable; |
| 5755 | f->base_offset = offset; |
| 5756 | return f; |
| 5757 | } |
| 5758 | |
| 5759 | void qemu_fflush(QEMUFile *f) |
| 5760 | { |
| 5761 | if (!f->is_writable) |
| 5762 | return; |
| 5763 | if (f->buf_index > 0) { |
| 5764 | if (f->is_file) { |
| 5765 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 5766 | fwrite(f->buf, 1, f->buf_index, f->outfile); |
| 5767 | } else { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5768 | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5769 | f->buf, f->buf_index); |
| 5770 | } |
| 5771 | f->buf_offset += f->buf_index; |
| 5772 | f->buf_index = 0; |
| 5773 | } |
| 5774 | } |
| 5775 | |
| 5776 | static void qemu_fill_buffer(QEMUFile *f) |
| 5777 | { |
| 5778 | int len; |
| 5779 | |
| 5780 | if (f->is_writable) |
| 5781 | return; |
| 5782 | if (f->is_file) { |
| 5783 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 5784 | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile); |
| 5785 | if (len < 0) |
| 5786 | len = 0; |
| 5787 | } else { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5788 | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5789 | f->buf, IO_BUF_SIZE); |
| 5790 | if (len < 0) |
| 5791 | len = 0; |
| 5792 | } |
| 5793 | f->buf_index = 0; |
| 5794 | f->buf_size = len; |
| 5795 | f->buf_offset += len; |
| 5796 | } |
| 5797 | |
| 5798 | void qemu_fclose(QEMUFile *f) |
| 5799 | { |
| 5800 | if (f->is_writable) |
| 5801 | qemu_fflush(f); |
| 5802 | if (f->is_file) { |
| 5803 | fclose(f->outfile); |
| 5804 | } |
| 5805 | qemu_free(f); |
| 5806 | } |
| 5807 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5808 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
| 5809 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5810 | int l; |
| 5811 | while (size > 0) { |
| 5812 | l = IO_BUF_SIZE - f->buf_index; |
| 5813 | if (l > size) |
| 5814 | l = size; |
| 5815 | memcpy(f->buf + f->buf_index, buf, l); |
| 5816 | f->buf_index += l; |
| 5817 | buf += l; |
| 5818 | size -= l; |
| 5819 | if (f->buf_index >= IO_BUF_SIZE) |
| 5820 | qemu_fflush(f); |
| 5821 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5822 | } |
| 5823 | |
| 5824 | void qemu_put_byte(QEMUFile *f, int v) |
| 5825 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5826 | f->buf[f->buf_index++] = v; |
| 5827 | if (f->buf_index >= IO_BUF_SIZE) |
| 5828 | qemu_fflush(f); |
| 5829 | } |
| 5830 | |
| 5831 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
| 5832 | { |
| 5833 | int size, l; |
| 5834 | |
| 5835 | size = size1; |
| 5836 | while (size > 0) { |
| 5837 | l = f->buf_size - f->buf_index; |
| 5838 | if (l == 0) { |
| 5839 | qemu_fill_buffer(f); |
| 5840 | l = f->buf_size - f->buf_index; |
| 5841 | if (l == 0) |
| 5842 | break; |
| 5843 | } |
| 5844 | if (l > size) |
| 5845 | l = size; |
| 5846 | memcpy(buf, f->buf + f->buf_index, l); |
| 5847 | f->buf_index += l; |
| 5848 | buf += l; |
| 5849 | size -= l; |
| 5850 | } |
| 5851 | return size1 - size; |
| 5852 | } |
| 5853 | |
| 5854 | int qemu_get_byte(QEMUFile *f) |
| 5855 | { |
| 5856 | if (f->buf_index >= f->buf_size) { |
| 5857 | qemu_fill_buffer(f); |
| 5858 | if (f->buf_index >= f->buf_size) |
| 5859 | return 0; |
| 5860 | } |
| 5861 | return f->buf[f->buf_index++]; |
| 5862 | } |
| 5863 | |
| 5864 | int64_t qemu_ftell(QEMUFile *f) |
| 5865 | { |
| 5866 | return f->buf_offset - f->buf_size + f->buf_index; |
| 5867 | } |
| 5868 | |
| 5869 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) |
| 5870 | { |
| 5871 | if (whence == SEEK_SET) { |
| 5872 | /* nothing to do */ |
| 5873 | } else if (whence == SEEK_CUR) { |
| 5874 | pos += qemu_ftell(f); |
| 5875 | } else { |
| 5876 | /* SEEK_END not supported */ |
| 5877 | return -1; |
| 5878 | } |
| 5879 | if (f->is_writable) { |
| 5880 | qemu_fflush(f); |
| 5881 | f->buf_offset = pos; |
| 5882 | } else { |
| 5883 | f->buf_offset = pos; |
| 5884 | f->buf_index = 0; |
| 5885 | f->buf_size = 0; |
| 5886 | } |
| 5887 | return pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
| 5890 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
| 5891 | { |
| 5892 | qemu_put_byte(f, v >> 8); |
| 5893 | qemu_put_byte(f, v); |
| 5894 | } |
| 5895 | |
| 5896 | void qemu_put_be32(QEMUFile *f, unsigned int v) |
| 5897 | { |
| 5898 | qemu_put_byte(f, v >> 24); |
| 5899 | qemu_put_byte(f, v >> 16); |
| 5900 | qemu_put_byte(f, v >> 8); |
| 5901 | qemu_put_byte(f, v); |
| 5902 | } |
| 5903 | |
| 5904 | void qemu_put_be64(QEMUFile *f, uint64_t v) |
| 5905 | { |
| 5906 | qemu_put_be32(f, v >> 32); |
| 5907 | qemu_put_be32(f, v); |
| 5908 | } |
| 5909 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5910 | unsigned int qemu_get_be16(QEMUFile *f) |
| 5911 | { |
| 5912 | unsigned int v; |
| 5913 | v = qemu_get_byte(f) << 8; |
| 5914 | v |= qemu_get_byte(f); |
| 5915 | return v; |
| 5916 | } |
| 5917 | |
| 5918 | unsigned int qemu_get_be32(QEMUFile *f) |
| 5919 | { |
| 5920 | unsigned int v; |
| 5921 | v = qemu_get_byte(f) << 24; |
| 5922 | v |= qemu_get_byte(f) << 16; |
| 5923 | v |= qemu_get_byte(f) << 8; |
| 5924 | v |= qemu_get_byte(f); |
| 5925 | return v; |
| 5926 | } |
| 5927 | |
| 5928 | uint64_t qemu_get_be64(QEMUFile *f) |
| 5929 | { |
| 5930 | uint64_t v; |
| 5931 | v = (uint64_t)qemu_get_be32(f) << 32; |
| 5932 | v |= qemu_get_be32(f); |
| 5933 | return v; |
| 5934 | } |
| 5935 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5936 | typedef struct SaveStateEntry { |
| 5937 | char idstr[256]; |
| 5938 | int instance_id; |
| 5939 | int version_id; |
| 5940 | SaveStateHandler *save_state; |
| 5941 | LoadStateHandler *load_state; |
| 5942 | void *opaque; |
| 5943 | struct SaveStateEntry *next; |
| 5944 | } SaveStateEntry; |
| 5945 | |
| 5946 | static SaveStateEntry *first_se; |
| 5947 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5948 | int register_savevm(const char *idstr, |
| 5949 | int instance_id, |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5950 | int version_id, |
| 5951 | SaveStateHandler *save_state, |
| 5952 | LoadStateHandler *load_state, |
| 5953 | void *opaque) |
| 5954 | { |
| 5955 | SaveStateEntry *se, **pse; |
| 5956 | |
| 5957 | se = qemu_malloc(sizeof(SaveStateEntry)); |
| 5958 | if (!se) |
| 5959 | return -1; |
| 5960 | pstrcpy(se->idstr, sizeof(se->idstr), idstr); |
| 5961 | se->instance_id = instance_id; |
| 5962 | se->version_id = version_id; |
| 5963 | se->save_state = save_state; |
| 5964 | se->load_state = load_state; |
| 5965 | se->opaque = opaque; |
| 5966 | se->next = NULL; |
| 5967 | |
| 5968 | /* add at the end of list */ |
| 5969 | pse = &first_se; |
| 5970 | while (*pse != NULL) |
| 5971 | pse = &(*pse)->next; |
| 5972 | *pse = se; |
| 5973 | return 0; |
| 5974 | } |
| 5975 | |
| 5976 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5977 | #define QEMU_VM_FILE_VERSION 0x00000002 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5978 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 5979 | static int qemu_savevm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5980 | { |
| 5981 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5982 | int len, ret; |
| 5983 | int64_t cur_pos, len_pos, total_len_pos; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 5984 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5985 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 5986 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5987 | total_len_pos = qemu_ftell(f); |
| 5988 | qemu_put_be64(f, 0); /* total size */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5989 | |
| 5990 | for(se = first_se; se != NULL; se = se->next) { |
| 5991 | /* ID string */ |
| 5992 | len = strlen(se->idstr); |
| 5993 | qemu_put_byte(f, len); |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 5994 | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5995 | |
| 5996 | qemu_put_be32(f, se->instance_id); |
| 5997 | qemu_put_be32(f, se->version_id); |
| 5998 | |
| 5999 | /* record size: filled later */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6000 | len_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6001 | qemu_put_be32(f, 0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6002 | se->save_state(f, se->opaque); |
| 6003 | |
| 6004 | /* fill record size */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6005 | cur_pos = qemu_ftell(f); |
| 6006 | len = cur_pos - len_pos - 4; |
| 6007 | qemu_fseek(f, len_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6008 | qemu_put_be32(f, len); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6009 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6010 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6011 | cur_pos = qemu_ftell(f); |
| 6012 | qemu_fseek(f, total_len_pos, SEEK_SET); |
| 6013 | qemu_put_be64(f, cur_pos - total_len_pos - 8); |
| 6014 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6015 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6016 | ret = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6017 | return ret; |
| 6018 | } |
| 6019 | |
| 6020 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
| 6021 | { |
| 6022 | SaveStateEntry *se; |
| 6023 | |
| 6024 | for(se = first_se; se != NULL; se = se->next) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6025 | if (!strcmp(se->idstr, idstr) && |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6026 | instance_id == se->instance_id) |
| 6027 | return se; |
| 6028 | } |
| 6029 | return NULL; |
| 6030 | } |
| 6031 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 6032 | static int qemu_loadvm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6033 | { |
| 6034 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6035 | int len, ret, instance_id, record_len, version_id; |
| 6036 | int64_t total_len, end_pos, cur_pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6037 | unsigned int v; |
| 6038 | char idstr[256]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6039 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6040 | v = qemu_get_be32(f); |
| 6041 | if (v != QEMU_VM_FILE_MAGIC) |
| 6042 | goto fail; |
| 6043 | v = qemu_get_be32(f); |
| 6044 | if (v != QEMU_VM_FILE_VERSION) { |
| 6045 | fail: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6046 | ret = -1; |
| 6047 | goto the_end; |
| 6048 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6049 | total_len = qemu_get_be64(f); |
| 6050 | end_pos = total_len + qemu_ftell(f); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6051 | for(;;) { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6052 | if (qemu_ftell(f) >= end_pos) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6053 | break; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6054 | len = qemu_get_byte(f); |
ths | ffe8ab8 | 2007-12-16 03:16:05 +0000 | [diff] [blame] | 6055 | qemu_get_buffer(f, (uint8_t *)idstr, len); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6056 | idstr[len] = '\0'; |
| 6057 | instance_id = qemu_get_be32(f); |
| 6058 | version_id = qemu_get_be32(f); |
| 6059 | record_len = qemu_get_be32(f); |
| 6060 | #if 0 |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6061 | printf("idstr=%s instance=0x%x version=%d len=%d\n", |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6062 | idstr, instance_id, version_id, record_len); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6063 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6064 | cur_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6065 | se = find_se(idstr, instance_id); |
| 6066 | if (!se) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6067 | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n", |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6068 | instance_id, idstr); |
| 6069 | } else { |
| 6070 | ret = se->load_state(f, se->opaque, version_id); |
| 6071 | if (ret < 0) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6072 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6073 | instance_id, idstr); |
| 6074 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 6075 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6076 | /* always seek to exact end of record */ |
| 6077 | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
| 6078 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6079 | ret = 0; |
| 6080 | the_end: |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6081 | return ret; |
| 6082 | } |
| 6083 | |
| 6084 | /* device can contain snapshots */ |
| 6085 | static int bdrv_can_snapshot(BlockDriverState *bs) |
| 6086 | { |
| 6087 | return (bs && |
| 6088 | !bdrv_is_removable(bs) && |
| 6089 | !bdrv_is_read_only(bs)); |
| 6090 | } |
| 6091 | |
| 6092 | /* device must be snapshots in order to have a reliable snapshot */ |
| 6093 | static int bdrv_has_snapshot(BlockDriverState *bs) |
| 6094 | { |
| 6095 | return (bs && |
| 6096 | !bdrv_is_removable(bs) && |
| 6097 | !bdrv_is_read_only(bs)); |
| 6098 | } |
| 6099 | |
| 6100 | static BlockDriverState *get_bs_snapshots(void) |
| 6101 | { |
| 6102 | BlockDriverState *bs; |
| 6103 | int i; |
| 6104 | |
| 6105 | if (bs_snapshots) |
| 6106 | return bs_snapshots; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 6107 | for(i = 0; i <= nb_drives; i++) { |
| 6108 | bs = drives_table[i].bdrv; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6109 | if (bdrv_can_snapshot(bs)) |
| 6110 | goto ok; |
| 6111 | } |
| 6112 | return NULL; |
| 6113 | ok: |
| 6114 | bs_snapshots = bs; |
| 6115 | return bs; |
| 6116 | } |
| 6117 | |
| 6118 | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
| 6119 | const char *name) |
| 6120 | { |
| 6121 | QEMUSnapshotInfo *sn_tab, *sn; |
| 6122 | int nb_sns, i, ret; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6123 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6124 | ret = -ENOENT; |
| 6125 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 6126 | if (nb_sns < 0) |
| 6127 | return ret; |
| 6128 | for(i = 0; i < nb_sns; i++) { |
| 6129 | sn = &sn_tab[i]; |
| 6130 | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { |
| 6131 | *sn_info = *sn; |
| 6132 | ret = 0; |
| 6133 | break; |
| 6134 | } |
| 6135 | } |
| 6136 | qemu_free(sn_tab); |
| 6137 | return ret; |
| 6138 | } |
| 6139 | |
| 6140 | void do_savevm(const char *name) |
| 6141 | { |
| 6142 | BlockDriverState *bs, *bs1; |
| 6143 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
| 6144 | int must_delete, ret, i; |
| 6145 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 6146 | QEMUFile *f; |
| 6147 | int saved_vm_running; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 6148 | #ifdef _WIN32 |
| 6149 | struct _timeb tb; |
| 6150 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6151 | struct timeval tv; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 6152 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6153 | |
| 6154 | bs = get_bs_snapshots(); |
| 6155 | if (!bs) { |
| 6156 | term_printf("No block device can accept snapshots\n"); |
| 6157 | return; |
| 6158 | } |
| 6159 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 6160 | /* ??? Should this occur after vm_stop? */ |
| 6161 | qemu_aio_flush(); |
| 6162 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6163 | saved_vm_running = vm_running; |
| 6164 | vm_stop(0); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6165 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6166 | must_delete = 0; |
| 6167 | if (name) { |
| 6168 | ret = bdrv_snapshot_find(bs, old_sn, name); |
| 6169 | if (ret >= 0) { |
| 6170 | must_delete = 1; |
| 6171 | } |
| 6172 | } |
| 6173 | memset(sn, 0, sizeof(*sn)); |
| 6174 | if (must_delete) { |
| 6175 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); |
| 6176 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); |
| 6177 | } else { |
| 6178 | if (name) |
| 6179 | pstrcpy(sn->name, sizeof(sn->name), name); |
| 6180 | } |
| 6181 | |
| 6182 | /* fill auxiliary fields */ |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 6183 | #ifdef _WIN32 |
| 6184 | _ftime(&tb); |
| 6185 | sn->date_sec = tb.time; |
| 6186 | sn->date_nsec = tb.millitm * 1000000; |
| 6187 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6188 | gettimeofday(&tv, NULL); |
| 6189 | sn->date_sec = tv.tv_sec; |
| 6190 | sn->date_nsec = tv.tv_usec * 1000; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 6191 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6192 | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6193 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6194 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 6195 | term_printf("Device %s does not support VM state snapshots\n", |
| 6196 | bdrv_get_device_name(bs)); |
| 6197 | goto the_end; |
| 6198 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6199 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6200 | /* save the VM state */ |
| 6201 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1); |
| 6202 | if (!f) { |
| 6203 | term_printf("Could not open VM state file\n"); |
| 6204 | goto the_end; |
| 6205 | } |
| 6206 | ret = qemu_savevm_state(f); |
| 6207 | sn->vm_state_size = qemu_ftell(f); |
| 6208 | qemu_fclose(f); |
| 6209 | if (ret < 0) { |
| 6210 | term_printf("Error %d while writing VM\n", ret); |
| 6211 | goto the_end; |
| 6212 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6213 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6214 | /* create the snapshots */ |
| 6215 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 6216 | for(i = 0; i < nb_drives; i++) { |
| 6217 | bs1 = drives_table[i].bdrv; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6218 | if (bdrv_has_snapshot(bs1)) { |
| 6219 | if (must_delete) { |
| 6220 | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
| 6221 | if (ret < 0) { |
| 6222 | term_printf("Error while deleting snapshot on '%s'\n", |
| 6223 | bdrv_get_device_name(bs1)); |
| 6224 | } |
| 6225 | } |
| 6226 | ret = bdrv_snapshot_create(bs1, sn); |
| 6227 | if (ret < 0) { |
| 6228 | term_printf("Error while creating snapshot on '%s'\n", |
| 6229 | bdrv_get_device_name(bs1)); |
| 6230 | } |
| 6231 | } |
| 6232 | } |
| 6233 | |
| 6234 | the_end: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6235 | if (saved_vm_running) |
| 6236 | vm_start(); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6237 | } |
| 6238 | |
| 6239 | void do_loadvm(const char *name) |
| 6240 | { |
| 6241 | BlockDriverState *bs, *bs1; |
| 6242 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 6243 | QEMUFile *f; |
| 6244 | int i, ret; |
| 6245 | int saved_vm_running; |
| 6246 | |
| 6247 | bs = get_bs_snapshots(); |
| 6248 | if (!bs) { |
| 6249 | term_printf("No block device supports snapshots\n"); |
| 6250 | return; |
| 6251 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6252 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 6253 | /* Flush all IO requests so they don't interfere with the new state. */ |
| 6254 | qemu_aio_flush(); |
| 6255 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6256 | saved_vm_running = vm_running; |
| 6257 | vm_stop(0); |
| 6258 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 6259 | for(i = 0; i <= nb_drives; i++) { |
| 6260 | bs1 = drives_table[i].bdrv; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6261 | if (bdrv_has_snapshot(bs1)) { |
| 6262 | ret = bdrv_snapshot_goto(bs1, name); |
| 6263 | if (ret < 0) { |
| 6264 | if (bs != bs1) |
| 6265 | term_printf("Warning: "); |
| 6266 | switch(ret) { |
| 6267 | case -ENOTSUP: |
| 6268 | term_printf("Snapshots not supported on device '%s'\n", |
| 6269 | bdrv_get_device_name(bs1)); |
| 6270 | break; |
| 6271 | case -ENOENT: |
| 6272 | term_printf("Could not find snapshot '%s' on device '%s'\n", |
| 6273 | name, bdrv_get_device_name(bs1)); |
| 6274 | break; |
| 6275 | default: |
| 6276 | term_printf("Error %d while activating snapshot on '%s'\n", |
| 6277 | ret, bdrv_get_device_name(bs1)); |
| 6278 | break; |
| 6279 | } |
| 6280 | /* fatal on snapshot block device */ |
| 6281 | if (bs == bs1) |
| 6282 | goto the_end; |
| 6283 | } |
| 6284 | } |
| 6285 | } |
| 6286 | |
| 6287 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 6288 | term_printf("Device %s does not support VM state snapshots\n", |
| 6289 | bdrv_get_device_name(bs)); |
| 6290 | return; |
| 6291 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6292 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6293 | /* restore the VM state */ |
| 6294 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0); |
| 6295 | if (!f) { |
| 6296 | term_printf("Could not open VM state file\n"); |
| 6297 | goto the_end; |
| 6298 | } |
| 6299 | ret = qemu_loadvm_state(f); |
| 6300 | qemu_fclose(f); |
| 6301 | if (ret < 0) { |
| 6302 | term_printf("Error %d while loading VM state\n", ret); |
| 6303 | } |
| 6304 | the_end: |
| 6305 | if (saved_vm_running) |
| 6306 | vm_start(); |
| 6307 | } |
| 6308 | |
| 6309 | void do_delvm(const char *name) |
| 6310 | { |
| 6311 | BlockDriverState *bs, *bs1; |
| 6312 | int i, ret; |
| 6313 | |
| 6314 | bs = get_bs_snapshots(); |
| 6315 | if (!bs) { |
| 6316 | term_printf("No block device supports snapshots\n"); |
| 6317 | return; |
| 6318 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6319 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 6320 | for(i = 0; i <= nb_drives; i++) { |
| 6321 | bs1 = drives_table[i].bdrv; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6322 | if (bdrv_has_snapshot(bs1)) { |
| 6323 | ret = bdrv_snapshot_delete(bs1, name); |
| 6324 | if (ret < 0) { |
| 6325 | if (ret == -ENOTSUP) |
| 6326 | term_printf("Snapshots not supported on device '%s'\n", |
| 6327 | bdrv_get_device_name(bs1)); |
| 6328 | else |
| 6329 | term_printf("Error %d while deleting snapshot on '%s'\n", |
| 6330 | ret, bdrv_get_device_name(bs1)); |
| 6331 | } |
| 6332 | } |
| 6333 | } |
| 6334 | } |
| 6335 | |
| 6336 | void do_info_snapshots(void) |
| 6337 | { |
| 6338 | BlockDriverState *bs, *bs1; |
| 6339 | QEMUSnapshotInfo *sn_tab, *sn; |
| 6340 | int nb_sns, i; |
| 6341 | char buf[256]; |
| 6342 | |
| 6343 | bs = get_bs_snapshots(); |
| 6344 | if (!bs) { |
| 6345 | term_printf("No available block device supports snapshots\n"); |
| 6346 | return; |
| 6347 | } |
| 6348 | term_printf("Snapshot devices:"); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 6349 | for(i = 0; i <= nb_drives; i++) { |
| 6350 | bs1 = drives_table[i].bdrv; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 6351 | if (bdrv_has_snapshot(bs1)) { |
| 6352 | if (bs == bs1) |
| 6353 | term_printf(" %s", bdrv_get_device_name(bs1)); |
| 6354 | } |
| 6355 | } |
| 6356 | term_printf("\n"); |
| 6357 | |
| 6358 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 6359 | if (nb_sns < 0) { |
| 6360 | term_printf("bdrv_snapshot_list: error %d\n", nb_sns); |
| 6361 | return; |
| 6362 | } |
| 6363 | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs)); |
| 6364 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 6365 | for(i = 0; i < nb_sns; i++) { |
| 6366 | sn = &sn_tab[i]; |
| 6367 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
| 6368 | } |
| 6369 | qemu_free(sn_tab); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6370 | } |
| 6371 | |
| 6372 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6373 | /* ram save/restore */ |
| 6374 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6375 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
| 6376 | { |
| 6377 | int v; |
| 6378 | |
| 6379 | v = qemu_get_byte(f); |
| 6380 | switch(v) { |
| 6381 | case 0: |
| 6382 | if (qemu_get_buffer(f, buf, len) != len) |
| 6383 | return -EIO; |
| 6384 | break; |
| 6385 | case 1: |
| 6386 | v = qemu_get_byte(f); |
| 6387 | memset(buf, v, len); |
| 6388 | break; |
| 6389 | default: |
| 6390 | return -EINVAL; |
| 6391 | } |
| 6392 | return 0; |
| 6393 | } |
| 6394 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6395 | static int ram_load_v1(QEMUFile *f, void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6396 | { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 6397 | int ret; |
| 6398 | ram_addr_t i; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6399 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6400 | if (qemu_get_be32(f) != phys_ram_size) |
| 6401 | return -EINVAL; |
| 6402 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
| 6403 | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
| 6404 | if (ret) |
| 6405 | return ret; |
| 6406 | } |
| 6407 | return 0; |
| 6408 | } |
| 6409 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6410 | #define BDRV_HASH_BLOCK_SIZE 1024 |
| 6411 | #define IOBUF_SIZE 4096 |
| 6412 | #define RAM_CBLOCK_MAGIC 0xfabe |
| 6413 | |
| 6414 | typedef struct RamCompressState { |
| 6415 | z_stream zstream; |
| 6416 | QEMUFile *f; |
| 6417 | uint8_t buf[IOBUF_SIZE]; |
| 6418 | } RamCompressState; |
| 6419 | |
| 6420 | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
| 6421 | { |
| 6422 | int ret; |
| 6423 | memset(s, 0, sizeof(*s)); |
| 6424 | s->f = f; |
| 6425 | ret = deflateInit2(&s->zstream, 1, |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6426 | Z_DEFLATED, 15, |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6427 | 9, Z_DEFAULT_STRATEGY); |
| 6428 | if (ret != Z_OK) |
| 6429 | return -1; |
| 6430 | s->zstream.avail_out = IOBUF_SIZE; |
| 6431 | s->zstream.next_out = s->buf; |
| 6432 | return 0; |
| 6433 | } |
| 6434 | |
| 6435 | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
| 6436 | { |
| 6437 | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
| 6438 | qemu_put_be16(s->f, len); |
| 6439 | qemu_put_buffer(s->f, buf, len); |
| 6440 | } |
| 6441 | |
| 6442 | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
| 6443 | { |
| 6444 | int ret; |
| 6445 | |
| 6446 | s->zstream.avail_in = len; |
| 6447 | s->zstream.next_in = (uint8_t *)buf; |
| 6448 | while (s->zstream.avail_in > 0) { |
| 6449 | ret = deflate(&s->zstream, Z_NO_FLUSH); |
| 6450 | if (ret != Z_OK) |
| 6451 | return -1; |
| 6452 | if (s->zstream.avail_out == 0) { |
| 6453 | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
| 6454 | s->zstream.avail_out = IOBUF_SIZE; |
| 6455 | s->zstream.next_out = s->buf; |
| 6456 | } |
| 6457 | } |
| 6458 | return 0; |
| 6459 | } |
| 6460 | |
| 6461 | static void ram_compress_close(RamCompressState *s) |
| 6462 | { |
| 6463 | int len, ret; |
| 6464 | |
| 6465 | /* compress last bytes */ |
| 6466 | for(;;) { |
| 6467 | ret = deflate(&s->zstream, Z_FINISH); |
| 6468 | if (ret == Z_OK || ret == Z_STREAM_END) { |
| 6469 | len = IOBUF_SIZE - s->zstream.avail_out; |
| 6470 | if (len > 0) { |
| 6471 | ram_put_cblock(s, s->buf, len); |
| 6472 | } |
| 6473 | s->zstream.avail_out = IOBUF_SIZE; |
| 6474 | s->zstream.next_out = s->buf; |
| 6475 | if (ret == Z_STREAM_END) |
| 6476 | break; |
| 6477 | } else { |
| 6478 | goto fail; |
| 6479 | } |
| 6480 | } |
| 6481 | fail: |
| 6482 | deflateEnd(&s->zstream); |
| 6483 | } |
| 6484 | |
| 6485 | typedef struct RamDecompressState { |
| 6486 | z_stream zstream; |
| 6487 | QEMUFile *f; |
| 6488 | uint8_t buf[IOBUF_SIZE]; |
| 6489 | } RamDecompressState; |
| 6490 | |
| 6491 | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
| 6492 | { |
| 6493 | int ret; |
| 6494 | memset(s, 0, sizeof(*s)); |
| 6495 | s->f = f; |
| 6496 | ret = inflateInit(&s->zstream); |
| 6497 | if (ret != Z_OK) |
| 6498 | return -1; |
| 6499 | return 0; |
| 6500 | } |
| 6501 | |
| 6502 | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
| 6503 | { |
| 6504 | int ret, clen; |
| 6505 | |
| 6506 | s->zstream.avail_out = len; |
| 6507 | s->zstream.next_out = buf; |
| 6508 | while (s->zstream.avail_out > 0) { |
| 6509 | if (s->zstream.avail_in == 0) { |
| 6510 | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC) |
| 6511 | return -1; |
| 6512 | clen = qemu_get_be16(s->f); |
| 6513 | if (clen > IOBUF_SIZE) |
| 6514 | return -1; |
| 6515 | qemu_get_buffer(s->f, s->buf, clen); |
| 6516 | s->zstream.avail_in = clen; |
| 6517 | s->zstream.next_in = s->buf; |
| 6518 | } |
| 6519 | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
| 6520 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 6521 | return -1; |
| 6522 | } |
| 6523 | } |
| 6524 | return 0; |
| 6525 | } |
| 6526 | |
| 6527 | static void ram_decompress_close(RamDecompressState *s) |
| 6528 | { |
| 6529 | inflateEnd(&s->zstream); |
| 6530 | } |
| 6531 | |
| 6532 | static void ram_save(QEMUFile *f, void *opaque) |
| 6533 | { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 6534 | ram_addr_t i; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6535 | RamCompressState s1, *s = &s1; |
| 6536 | uint8_t buf[10]; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6537 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6538 | qemu_put_be32(f, phys_ram_size); |
| 6539 | if (ram_compress_open(s, f) < 0) |
| 6540 | return; |
| 6541 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 6542 | #if 0 |
| 6543 | if (tight_savevm_enabled) { |
| 6544 | int64_t sector_num; |
| 6545 | int j; |
| 6546 | |
| 6547 | /* find if the memory block is available on a virtual |
| 6548 | block device */ |
| 6549 | sector_num = -1; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 6550 | for(j = 0; j < nb_drives; j++) { |
| 6551 | sector_num = bdrv_hash_find(drives_table[j].bdrv, |
| 6552 | phys_ram_base + i, |
| 6553 | BDRV_HASH_BLOCK_SIZE); |
| 6554 | if (sector_num >= 0) |
| 6555 | break; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6556 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 6557 | if (j == nb_drives) |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6558 | goto normal_compress; |
| 6559 | buf[0] = 1; |
| 6560 | buf[1] = j; |
| 6561 | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num); |
| 6562 | ram_compress_buf(s, buf, 10); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6563 | } else |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6564 | #endif |
| 6565 | { |
| 6566 | // normal_compress: |
| 6567 | buf[0] = 0; |
| 6568 | ram_compress_buf(s, buf, 1); |
| 6569 | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
| 6570 | } |
| 6571 | } |
| 6572 | ram_compress_close(s); |
| 6573 | } |
| 6574 | |
| 6575 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 6576 | { |
| 6577 | RamDecompressState s1, *s = &s1; |
| 6578 | uint8_t buf[10]; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 6579 | ram_addr_t i; |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6580 | |
| 6581 | if (version_id == 1) |
| 6582 | return ram_load_v1(f, opaque); |
| 6583 | if (version_id != 2) |
| 6584 | return -EINVAL; |
| 6585 | if (qemu_get_be32(f) != phys_ram_size) |
| 6586 | return -EINVAL; |
| 6587 | if (ram_decompress_open(s, f) < 0) |
| 6588 | return -EINVAL; |
| 6589 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 6590 | if (ram_decompress_buf(s, buf, 1) < 0) { |
| 6591 | fprintf(stderr, "Error while reading ram block header\n"); |
| 6592 | goto error; |
| 6593 | } |
| 6594 | if (buf[0] == 0) { |
| 6595 | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 6596 | fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6597 | goto error; |
| 6598 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6599 | } else |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6600 | #if 0 |
| 6601 | if (buf[0] == 1) { |
| 6602 | int bs_index; |
| 6603 | int64_t sector_num; |
| 6604 | |
| 6605 | ram_decompress_buf(s, buf + 1, 9); |
| 6606 | bs_index = buf[1]; |
| 6607 | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2)); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 6608 | if (bs_index >= nb_drives) { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6609 | fprintf(stderr, "Invalid block device index %d\n", bs_index); |
| 6610 | goto error; |
| 6611 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 6612 | if (bdrv_read(drives_table[bs_index].bdrv, sector_num, |
| 6613 | phys_ram_base + i, |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6614 | BDRV_HASH_BLOCK_SIZE / 512) < 0) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6615 | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n", |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6616 | bs_index, sector_num); |
| 6617 | goto error; |
| 6618 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6619 | } else |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 6620 | #endif |
| 6621 | { |
| 6622 | error: |
| 6623 | printf("Error block header\n"); |
| 6624 | return -EINVAL; |
| 6625 | } |
| 6626 | } |
| 6627 | ram_decompress_close(s); |
| 6628 | return 0; |
| 6629 | } |
| 6630 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6631 | /***********************************************************/ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6632 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 6633 | |
| 6634 | struct QEMUBH { |
| 6635 | QEMUBHFunc *cb; |
| 6636 | void *opaque; |
| 6637 | int scheduled; |
| 6638 | QEMUBH *next; |
| 6639 | }; |
| 6640 | |
| 6641 | static QEMUBH *first_bh = NULL; |
| 6642 | |
| 6643 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) |
| 6644 | { |
| 6645 | QEMUBH *bh; |
| 6646 | bh = qemu_mallocz(sizeof(QEMUBH)); |
| 6647 | if (!bh) |
| 6648 | return NULL; |
| 6649 | bh->cb = cb; |
| 6650 | bh->opaque = opaque; |
| 6651 | return bh; |
| 6652 | } |
| 6653 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 6654 | int qemu_bh_poll(void) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6655 | { |
| 6656 | QEMUBH *bh, **pbh; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 6657 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6658 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 6659 | ret = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6660 | for(;;) { |
| 6661 | pbh = &first_bh; |
| 6662 | bh = *pbh; |
| 6663 | if (!bh) |
| 6664 | break; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 6665 | ret = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6666 | *pbh = bh->next; |
| 6667 | bh->scheduled = 0; |
| 6668 | bh->cb(bh->opaque); |
| 6669 | } |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 6670 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6671 | } |
| 6672 | |
| 6673 | void qemu_bh_schedule(QEMUBH *bh) |
| 6674 | { |
| 6675 | CPUState *env = cpu_single_env; |
| 6676 | if (bh->scheduled) |
| 6677 | return; |
| 6678 | bh->scheduled = 1; |
| 6679 | bh->next = first_bh; |
| 6680 | first_bh = bh; |
| 6681 | |
| 6682 | /* stop the currently executing CPU to execute the BH ASAP */ |
| 6683 | if (env) { |
| 6684 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
| 6685 | } |
| 6686 | } |
| 6687 | |
| 6688 | void qemu_bh_cancel(QEMUBH *bh) |
| 6689 | { |
| 6690 | QEMUBH **pbh; |
| 6691 | if (bh->scheduled) { |
| 6692 | pbh = &first_bh; |
| 6693 | while (*pbh != bh) |
| 6694 | pbh = &(*pbh)->next; |
| 6695 | *pbh = bh->next; |
| 6696 | bh->scheduled = 0; |
| 6697 | } |
| 6698 | } |
| 6699 | |
| 6700 | void qemu_bh_delete(QEMUBH *bh) |
| 6701 | { |
| 6702 | qemu_bh_cancel(bh); |
| 6703 | qemu_free(bh); |
| 6704 | } |
| 6705 | |
| 6706 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6707 | /* machine registration */ |
| 6708 | |
| 6709 | QEMUMachine *first_machine = NULL; |
| 6710 | |
| 6711 | int qemu_register_machine(QEMUMachine *m) |
| 6712 | { |
| 6713 | QEMUMachine **pm; |
| 6714 | pm = &first_machine; |
| 6715 | while (*pm != NULL) |
| 6716 | pm = &(*pm)->next; |
| 6717 | m->next = NULL; |
| 6718 | *pm = m; |
| 6719 | return 0; |
| 6720 | } |
| 6721 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 6722 | static QEMUMachine *find_machine(const char *name) |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6723 | { |
| 6724 | QEMUMachine *m; |
| 6725 | |
| 6726 | for(m = first_machine; m != NULL; m = m->next) { |
| 6727 | if (!strcmp(m->name, name)) |
| 6728 | return m; |
| 6729 | } |
| 6730 | return NULL; |
| 6731 | } |
| 6732 | |
| 6733 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6734 | /* main execution loop */ |
| 6735 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 6736 | static void gui_update(void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6737 | { |
ths | 740733b | 2007-06-08 01:57:56 +0000 | [diff] [blame] | 6738 | DisplayState *ds = opaque; |
| 6739 | ds->dpy_refresh(ds); |
aurel32 | f442e08 | 2008-03-13 19:20:33 +0000 | [diff] [blame] | 6740 | qemu_mod_timer(ds->gui_timer, |
| 6741 | (ds->gui_timer_interval ? |
| 6742 | ds->gui_timer_interval : |
| 6743 | GUI_REFRESH_INTERVAL) |
| 6744 | + qemu_get_clock(rt_clock)); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6745 | } |
| 6746 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6747 | struct vm_change_state_entry { |
| 6748 | VMChangeStateHandler *cb; |
| 6749 | void *opaque; |
| 6750 | LIST_ENTRY (vm_change_state_entry) entries; |
| 6751 | }; |
| 6752 | |
| 6753 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
| 6754 | |
| 6755 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 6756 | void *opaque) |
| 6757 | { |
| 6758 | VMChangeStateEntry *e; |
| 6759 | |
| 6760 | e = qemu_mallocz(sizeof (*e)); |
| 6761 | if (!e) |
| 6762 | return NULL; |
| 6763 | |
| 6764 | e->cb = cb; |
| 6765 | e->opaque = opaque; |
| 6766 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
| 6767 | return e; |
| 6768 | } |
| 6769 | |
| 6770 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 6771 | { |
| 6772 | LIST_REMOVE (e, entries); |
| 6773 | qemu_free (e); |
| 6774 | } |
| 6775 | |
| 6776 | static void vm_state_notify(int running) |
| 6777 | { |
| 6778 | VMChangeStateEntry *e; |
| 6779 | |
| 6780 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { |
| 6781 | e->cb(e->opaque, running); |
| 6782 | } |
| 6783 | } |
| 6784 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6785 | /* XXX: support several handlers */ |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6786 | static VMStopHandler *vm_stop_cb; |
| 6787 | static void *vm_stop_opaque; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6788 | |
| 6789 | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 6790 | { |
| 6791 | vm_stop_cb = cb; |
| 6792 | vm_stop_opaque = opaque; |
| 6793 | return 0; |
| 6794 | } |
| 6795 | |
| 6796 | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 6797 | { |
| 6798 | vm_stop_cb = NULL; |
| 6799 | } |
| 6800 | |
| 6801 | void vm_start(void) |
| 6802 | { |
| 6803 | if (!vm_running) { |
| 6804 | cpu_enable_ticks(); |
| 6805 | vm_running = 1; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6806 | vm_state_notify(1); |
ths | efe7541 | 2007-08-24 01:36:32 +0000 | [diff] [blame] | 6807 | qemu_rearm_alarm_timer(alarm_timer); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6808 | } |
| 6809 | } |
| 6810 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6811 | void vm_stop(int reason) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6812 | { |
| 6813 | if (vm_running) { |
| 6814 | cpu_disable_ticks(); |
| 6815 | vm_running = 0; |
| 6816 | if (reason != 0) { |
| 6817 | if (vm_stop_cb) { |
| 6818 | vm_stop_cb(vm_stop_opaque, reason); |
| 6819 | } |
| 6820 | } |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6821 | vm_state_notify(0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6822 | } |
| 6823 | } |
| 6824 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6825 | /* reset/shutdown handler */ |
| 6826 | |
| 6827 | typedef struct QEMUResetEntry { |
| 6828 | QEMUResetHandler *func; |
| 6829 | void *opaque; |
| 6830 | struct QEMUResetEntry *next; |
| 6831 | } QEMUResetEntry; |
| 6832 | |
| 6833 | static QEMUResetEntry *first_reset_entry; |
| 6834 | static int reset_requested; |
| 6835 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6836 | static int powerdown_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6837 | |
aurel32 | cf7a2fe | 2008-03-18 06:53:05 +0000 | [diff] [blame] | 6838 | int qemu_shutdown_requested(void) |
| 6839 | { |
| 6840 | int r = shutdown_requested; |
| 6841 | shutdown_requested = 0; |
| 6842 | return r; |
| 6843 | } |
| 6844 | |
| 6845 | int qemu_reset_requested(void) |
| 6846 | { |
| 6847 | int r = reset_requested; |
| 6848 | reset_requested = 0; |
| 6849 | return r; |
| 6850 | } |
| 6851 | |
| 6852 | int qemu_powerdown_requested(void) |
| 6853 | { |
| 6854 | int r = powerdown_requested; |
| 6855 | powerdown_requested = 0; |
| 6856 | return r; |
| 6857 | } |
| 6858 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6859 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
| 6860 | { |
| 6861 | QEMUResetEntry **pre, *re; |
| 6862 | |
| 6863 | pre = &first_reset_entry; |
| 6864 | while (*pre != NULL) |
| 6865 | pre = &(*pre)->next; |
| 6866 | re = qemu_mallocz(sizeof(QEMUResetEntry)); |
| 6867 | re->func = func; |
| 6868 | re->opaque = opaque; |
| 6869 | re->next = NULL; |
| 6870 | *pre = re; |
| 6871 | } |
| 6872 | |
aurel32 | cf7a2fe | 2008-03-18 06:53:05 +0000 | [diff] [blame] | 6873 | void qemu_system_reset(void) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6874 | { |
| 6875 | QEMUResetEntry *re; |
| 6876 | |
| 6877 | /* reset all devices */ |
| 6878 | for(re = first_reset_entry; re != NULL; re = re->next) { |
| 6879 | re->func(re->opaque); |
| 6880 | } |
| 6881 | } |
| 6882 | |
| 6883 | void qemu_system_reset_request(void) |
| 6884 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6885 | if (no_reboot) { |
| 6886 | shutdown_requested = 1; |
| 6887 | } else { |
| 6888 | reset_requested = 1; |
| 6889 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6890 | if (cpu_single_env) |
| 6891 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6892 | } |
| 6893 | |
| 6894 | void qemu_system_shutdown_request(void) |
| 6895 | { |
| 6896 | shutdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6897 | if (cpu_single_env) |
| 6898 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6899 | } |
| 6900 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6901 | void qemu_system_powerdown_request(void) |
| 6902 | { |
| 6903 | powerdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6904 | if (cpu_single_env) |
| 6905 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6906 | } |
| 6907 | |
aurel32 | 0ecdffb | 2008-05-04 20:11:34 +0000 | [diff] [blame] | 6908 | /* boot_set handler */ |
| 6909 | QEMUBootSetHandler *qemu_boot_set_handler = NULL; |
| 6910 | |
| 6911 | void qemu_register_boot_set(QEMUBootSetHandler *func) |
| 6912 | { |
| 6913 | qemu_boot_set_handler = func; |
| 6914 | } |
| 6915 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6916 | void main_loop_wait(int timeout) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6917 | { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 6918 | IOHandlerRecord *ioh; |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6919 | fd_set rfds, wfds, xfds; |
ths | 877cf88 | 2007-04-18 18:11:47 +0000 | [diff] [blame] | 6920 | int ret, nfds; |
| 6921 | #ifdef _WIN32 |
| 6922 | int ret2, i; |
| 6923 | #endif |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6924 | struct timeval tv; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6925 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6926 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6927 | |
| 6928 | /* XXX: need to suppress polling by better using win32 events */ |
| 6929 | ret = 0; |
| 6930 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 6931 | ret |= pe->func(pe->opaque); |
| 6932 | } |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 6933 | #ifdef _WIN32 |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 6934 | if (ret == 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 6935 | int err; |
| 6936 | WaitObjects *w = &wait_objects; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6937 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 6938 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
| 6939 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 6940 | if (w->func[ret - WAIT_OBJECT_0]) |
| 6941 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6942 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 6943 | /* Check for additional signaled events */ |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 6944 | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6945 | |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 6946 | /* Check if event is signaled */ |
| 6947 | ret2 = WaitForSingleObject(w->events[i], 0); |
| 6948 | if(ret2 == WAIT_OBJECT_0) { |
| 6949 | if (w->func[i]) |
| 6950 | w->func[i](w->opaque[i]); |
| 6951 | } else if (ret2 == WAIT_TIMEOUT) { |
| 6952 | } else { |
| 6953 | err = GetLastError(); |
| 6954 | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6955 | } |
| 6956 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 6957 | } else if (ret == WAIT_TIMEOUT) { |
| 6958 | } else { |
| 6959 | err = GetLastError(); |
ths | e6b1e55 | 2007-04-18 17:56:02 +0000 | [diff] [blame] | 6960 | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err); |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 6961 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6962 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6963 | #endif |
| 6964 | /* poll any events */ |
| 6965 | /* XXX: separate device handlers from system ones */ |
| 6966 | nfds = -1; |
| 6967 | FD_ZERO(&rfds); |
| 6968 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6969 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6970 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 6971 | if (ioh->deleted) |
| 6972 | continue; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6973 | if (ioh->fd_read && |
| 6974 | (!ioh->fd_read_poll || |
| 6975 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 6976 | FD_SET(ioh->fd, &rfds); |
| 6977 | if (ioh->fd > nfds) |
| 6978 | nfds = ioh->fd; |
| 6979 | } |
| 6980 | if (ioh->fd_write) { |
| 6981 | FD_SET(ioh->fd, &wfds); |
| 6982 | if (ioh->fd > nfds) |
| 6983 | nfds = ioh->fd; |
| 6984 | } |
| 6985 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 6986 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6987 | tv.tv_sec = 0; |
| 6988 | #ifdef _WIN32 |
| 6989 | tv.tv_usec = 0; |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 6990 | #else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6991 | tv.tv_usec = timeout * 1000; |
| 6992 | #endif |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6993 | #if defined(CONFIG_SLIRP) |
| 6994 | if (slirp_inited) { |
| 6995 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 6996 | } |
| 6997 | #endif |
| 6998 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6999 | if (ret > 0) { |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 7000 | IOHandlerRecord **pioh; |
| 7001 | |
| 7002 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 7003 | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7004 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7005 | } |
ths | 6ab43fd | 2007-08-25 01:34:19 +0000 | [diff] [blame] | 7006 | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7007 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 7008 | } |
| 7009 | } |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 7010 | |
| 7011 | /* remove deleted IO handlers */ |
| 7012 | pioh = &first_io_handler; |
| 7013 | while (*pioh) { |
| 7014 | ioh = *pioh; |
| 7015 | if (ioh->deleted) { |
| 7016 | *pioh = ioh->next; |
| 7017 | qemu_free(ioh); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 7018 | } else |
ths | cafffd4 | 2007-02-28 21:59:44 +0000 | [diff] [blame] | 7019 | pioh = &ioh->next; |
| 7020 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7021 | } |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 7022 | #if defined(CONFIG_SLIRP) |
| 7023 | if (slirp_inited) { |
| 7024 | if (ret < 0) { |
| 7025 | FD_ZERO(&rfds); |
| 7026 | FD_ZERO(&wfds); |
| 7027 | FD_ZERO(&xfds); |
| 7028 | } |
| 7029 | slirp_select_poll(&rfds, &wfds, &xfds); |
| 7030 | } |
| 7031 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 7032 | qemu_aio_poll(); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7033 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7034 | if (vm_running) { |
edgar_igl | 21b2081 | 2008-05-15 19:54:00 +0000 | [diff] [blame] | 7035 | if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER))) |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 7036 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7037 | qemu_get_clock(vm_clock)); |
| 7038 | /* run dma transfers, if any */ |
| 7039 | DMA_run(); |
| 7040 | } |
pbrook | 423f074 | 2007-05-23 00:06:54 +0000 | [diff] [blame] | 7041 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7042 | /* real time timers */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 7043 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7044 | qemu_get_clock(rt_clock)); |
pbrook | 423f074 | 2007-05-23 00:06:54 +0000 | [diff] [blame] | 7045 | |
balrog | d5d0833 | 2008-01-05 19:41:47 +0000 | [diff] [blame] | 7046 | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) { |
| 7047 | alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED); |
| 7048 | qemu_rearm_alarm_timer(alarm_timer); |
| 7049 | } |
balrog | b99dc0d | 2007-12-16 13:17:12 +0000 | [diff] [blame] | 7050 | |
pbrook | 423f074 | 2007-05-23 00:06:54 +0000 | [diff] [blame] | 7051 | /* Check bottom-halves last in case any of the earlier events triggered |
| 7052 | them. */ |
| 7053 | qemu_bh_poll(); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7054 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7055 | } |
| 7056 | |
pbrook | 9596ebb | 2007-11-18 01:44:38 +0000 | [diff] [blame] | 7057 | static int main_loop(void) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7058 | { |
| 7059 | int ret, timeout; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7060 | #ifdef CONFIG_PROFILER |
| 7061 | int64_t ti; |
| 7062 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7063 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7064 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7065 | cur_cpu = first_cpu; |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 7066 | next_cpu = cur_cpu->next_cpu ?: first_cpu; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7067 | for(;;) { |
| 7068 | if (vm_running) { |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 7069 | |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 7070 | for(;;) { |
| 7071 | /* get next cpu */ |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 7072 | env = next_cpu; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7073 | #ifdef CONFIG_PROFILER |
| 7074 | ti = profile_getclock(); |
| 7075 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7076 | ret = cpu_exec(env); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7077 | #ifdef CONFIG_PROFILER |
| 7078 | qemu_time += profile_getclock() - ti; |
| 7079 | #endif |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 7080 | next_cpu = env->next_cpu ?: first_cpu; |
aurel32 | 95b0100 | 2008-04-04 17:16:35 +0000 | [diff] [blame] | 7081 | if (event_pending && likely(ret != EXCP_DEBUG)) { |
balrog | ee5605e | 2007-12-03 03:01:40 +0000 | [diff] [blame] | 7082 | ret = EXCP_INTERRUPT; |
| 7083 | event_pending = 0; |
| 7084 | break; |
| 7085 | } |
pbrook | bd967e0 | 2007-03-11 18:54:57 +0000 | [diff] [blame] | 7086 | if (ret == EXCP_HLT) { |
| 7087 | /* Give the next CPU a chance to run. */ |
| 7088 | cur_cpu = env; |
| 7089 | continue; |
| 7090 | } |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 7091 | if (ret != EXCP_HALTED) |
| 7092 | break; |
| 7093 | /* all CPUs are halted ? */ |
pbrook | bd967e0 | 2007-03-11 18:54:57 +0000 | [diff] [blame] | 7094 | if (env == cur_cpu) |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 7095 | break; |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 7096 | } |
| 7097 | cur_cpu = env; |
| 7098 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7099 | if (shutdown_requested) { |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 7100 | ret = EXCP_INTERRUPT; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 7101 | if (no_shutdown) { |
| 7102 | vm_stop(0); |
| 7103 | no_shutdown = 0; |
| 7104 | } |
| 7105 | else |
| 7106 | break; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7107 | } |
| 7108 | if (reset_requested) { |
| 7109 | reset_requested = 0; |
| 7110 | qemu_system_reset(); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 7111 | ret = EXCP_INTERRUPT; |
| 7112 | } |
| 7113 | if (powerdown_requested) { |
| 7114 | powerdown_requested = 0; |
| 7115 | qemu_system_powerdown(); |
| 7116 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7117 | } |
aurel32 | 95b0100 | 2008-04-04 17:16:35 +0000 | [diff] [blame] | 7118 | if (unlikely(ret == EXCP_DEBUG)) { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7119 | vm_stop(EXCP_DEBUG); |
| 7120 | } |
pbrook | bd967e0 | 2007-03-11 18:54:57 +0000 | [diff] [blame] | 7121 | /* If all cpus are halted then wait until the next IRQ */ |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7122 | /* XXX: use timeout computed from timers */ |
pbrook | bd967e0 | 2007-03-11 18:54:57 +0000 | [diff] [blame] | 7123 | if (ret == EXCP_HALTED) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7124 | timeout = 10; |
| 7125 | else |
| 7126 | timeout = 0; |
| 7127 | } else { |
| 7128 | timeout = 10; |
| 7129 | } |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7130 | #ifdef CONFIG_PROFILER |
| 7131 | ti = profile_getclock(); |
| 7132 | #endif |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7133 | main_loop_wait(timeout); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7134 | #ifdef CONFIG_PROFILER |
| 7135 | dev_time += profile_getclock() - ti; |
| 7136 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 7137 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 7138 | cpu_disable_ticks(); |
| 7139 | return ret; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 7140 | } |
| 7141 | |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 7142 | static void help(int exitcode) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7143 | { |
bellard | 68d0f70 | 2008-01-06 17:21:48 +0000 | [diff] [blame] | 7144 | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 7145 | "usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7146 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7147 | "'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] | 7148 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7149 | "Standard options:\n" |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7150 | "-M machine select emulated machine (-M ? for list)\n" |
pbrook | 5adb483 | 2007-03-08 03:15:18 +0000 | [diff] [blame] | 7151 | "-cpu cpu select CPU (-cpu ? for list)\n" |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7152 | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n" |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7153 | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" |
| 7154 | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7155 | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" |
aurel32 | a1620fa | 2008-04-29 05:58:01 +0000 | [diff] [blame] | 7156 | "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n" |
| 7157 | " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n" |
| 7158 | " [,cache=on|off][,format=f]\n" |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7159 | " use 'file' as a drive image\n" |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 7160 | "-mtdblock file use 'file' as on-board Flash memory image\n" |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 7161 | "-sd file use 'file' as SecureDigital card image\n" |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 7162 | "-pflash file use 'file' as a parallel flash image\n" |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 7163 | "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n" |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7164 | "-snapshot write to temporary files instead of disk image files\n" |
| 7165 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 7166 | "-no-frame open SDL window without a frame and window decorations\n" |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 7167 | "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n" |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7168 | "-no-quit disable SDL window close capability\n" |
| 7169 | #endif |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 7170 | #ifdef TARGET_I386 |
| 7171 | "-no-fd-bootchk disable boot signature checking for floppy disks\n" |
| 7172 | #endif |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 7173 | "-m megs set virtual RAM size to megs MB [default=%d]\n" |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 7174 | "-smp n set the number of CPUs to 'n' [default=1]\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7175 | "-nographic disable graphical output and redirect serial I/Os to console\n" |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 7176 | "-portrait rotate graphical output 90 deg left (only PXA LCD)\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 7177 | #ifndef _WIN32 |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7178 | "-k language use keyboard layout (for example \"fr\" for French)\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 7179 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7180 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7181 | "-audio-help print list of audio drivers and their options\n" |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 7182 | "-soundhw c1,... enable audio support\n" |
| 7183 | " and only specified sound cards (comma separated list)\n" |
| 7184 | " use -soundhw ? to get the list of supported cards\n" |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7185 | " use -soundhw all to enable all of them\n" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7186 | #endif |
bellard | 8998028 | 2004-06-03 14:04:03 +0000 | [diff] [blame] | 7187 | "-localtime set the real time clock to local time [default=utc]\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7188 | "-full-screen start in full screen\n" |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 7189 | #ifdef TARGET_I386 |
| 7190 | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n" |
| 7191 | #endif |
bellard | b389dbf | 2005-11-06 16:49:55 +0000 | [diff] [blame] | 7192 | "-usb enable the USB driver (will be the default soon)\n" |
| 7193 | "-usbdevice name add the host or guest USB device 'name'\n" |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 7194 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
| 7195 | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 7196 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 7197 | "-name string set the name of the guest\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7198 | "\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7199 | "Network options:\n" |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 7200 | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7201 | " create a new Network Interface Card and connect it to VLAN 'n'\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7202 | #ifdef CONFIG_SLIRP |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 7203 | "-net user[,vlan=n][,hostname=host]\n" |
| 7204 | " connect the user mode network stack to VLAN 'n' and send\n" |
| 7205 | " hostname 'host' to DHCP clients\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7206 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 7207 | #ifdef _WIN32 |
| 7208 | "-net tap[,vlan=n],ifname=name\n" |
| 7209 | " connect the host TAP network interface to VLAN 'n'\n" |
| 7210 | #else |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 7211 | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n" |
| 7212 | " connect the host TAP network interface to VLAN 'n' and use the\n" |
| 7213 | " network scripts 'file' (default=%s)\n" |
| 7214 | " and 'dfile' (default=%s);\n" |
| 7215 | " use '[down]script=no' to disable script execution;\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7216 | " use 'fd=h' to connect to an already opened TAP interface\n" |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 7217 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7218 | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7219 | " connect the vlan 'n' to another VLAN using a socket connection\n" |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 7220 | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n" |
| 7221 | " connect the vlan 'n' to multicast maddr and port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7222 | "-net none use it alone to have zero network devices; if no -net option\n" |
| 7223 | " is provided, the default is '-net nic -net user'\n" |
| 7224 | "\n" |
| 7225 | #ifdef CONFIG_SLIRP |
ths | 0db1137 | 2007-02-20 00:12:07 +0000 | [diff] [blame] | 7226 | "-tftp dir allow tftp access to files in dir [-net user]\n" |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 7227 | "-bootp file advertise file in BOOTP replies\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7228 | #ifndef _WIN32 |
| 7229 | "-smb dir allow SMB access to files in 'dir' [-net user]\n" |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7230 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7231 | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7232 | " redirect TCP or UDP connections from host to guest [-net user]\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7233 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7234 | "\n" |
| 7235 | "Linux boot specific:\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7236 | "-kernel bzImage use 'bzImage' as kernel image\n" |
| 7237 | "-append cmdline use 'cmdline' as kernel command line\n" |
| 7238 | "-initrd file use 'file' as initial ram disk\n" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 7239 | "\n" |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7240 | "Debug/Expert options:\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7241 | "-monitor dev redirect the monitor to char device 'dev'\n" |
| 7242 | "-serial dev redirect the serial port to char device 'dev'\n" |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7243 | "-parallel dev redirect the parallel port to char device 'dev'\n" |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 7244 | "-pidfile file Write PID to 'file'\n" |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7245 | "-S freeze CPU at startup (use 'c' to start execution)\n" |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 7246 | "-s wait gdb connection to port\n" |
| 7247 | "-p port set gdb connection port [default=%s]\n" |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 7248 | "-d item1,... output log to %s (use -d ? for a list of log items)\n" |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7249 | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n" |
| 7250 | " translation (t=none or lba) (usually qemu can guess them)\n" |
bellard | 87b4735 | 2006-08-17 17:22:54 +0000 | [diff] [blame] | 7251 | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7252 | #ifdef USE_KQEMU |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 7253 | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7254 | "-no-kqemu disable KQEMU kernel module usage\n" |
| 7255 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 7256 | #ifdef TARGET_I386 |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 7257 | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n" |
| 7258 | " (default is CL-GD5446 PCI VGA)\n" |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 7259 | "-no-acpi disable ACPI\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 7260 | #endif |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 7261 | #ifdef CONFIG_CURSES |
| 7262 | "-curses use a curses/ncurses interface instead of SDL\n" |
| 7263 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 7264 | "-no-reboot exit instead of rebooting\n" |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 7265 | "-no-shutdown stop before shutdown\n" |
aurel32 | a808000 | 2008-05-10 23:28:26 +0000 | [diff] [blame] | 7266 | "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n" |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7267 | "-vnc display start a VNC server on display\n" |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7268 | #ifndef _WIN32 |
| 7269 | "-daemonize daemonize QEMU after initializing\n" |
| 7270 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7271 | "-option-rom rom load a file, rom, into the option ROM space\n" |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 7272 | #ifdef TARGET_SPARC |
| 7273 | "-prom-env variable=value set OpenBIOS nvram variables\n" |
| 7274 | #endif |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 7275 | "-clock force the use of the given methods for timer alarm.\n" |
aurel32 | 3adda04 | 2008-03-09 23:43:49 +0000 | [diff] [blame] | 7276 | " To see what timers are available use -clock ?\n" |
bellard | bce6184 | 2008-02-01 22:18:51 +0000 | [diff] [blame] | 7277 | "-startdate select initial date of the clock\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7278 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7279 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 7280 | "ctrl-alt-f toggle full screen\n" |
| 7281 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 7282 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7283 | "\n" |
| 7284 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 7285 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 7286 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 7287 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7288 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 7289 | DEFAULT_NETWORK_SCRIPT, |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 7290 | DEFAULT_NETWORK_DOWN_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7291 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 7292 | DEFAULT_GDBSTUB_PORT, |
bellard | bce6184 | 2008-02-01 22:18:51 +0000 | [diff] [blame] | 7293 | "/tmp/qemu.log"); |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 7294 | exit(exitcode); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7295 | } |
| 7296 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7297 | #define HAS_ARG 0x0001 |
| 7298 | |
| 7299 | enum { |
| 7300 | QEMU_OPTION_h, |
| 7301 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7302 | QEMU_OPTION_M, |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 7303 | QEMU_OPTION_cpu, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7304 | QEMU_OPTION_fda, |
| 7305 | QEMU_OPTION_fdb, |
| 7306 | QEMU_OPTION_hda, |
| 7307 | QEMU_OPTION_hdb, |
| 7308 | QEMU_OPTION_hdc, |
| 7309 | QEMU_OPTION_hdd, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7310 | QEMU_OPTION_drive, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7311 | QEMU_OPTION_cdrom, |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 7312 | QEMU_OPTION_mtdblock, |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 7313 | QEMU_OPTION_sd, |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 7314 | QEMU_OPTION_pflash, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7315 | QEMU_OPTION_boot, |
| 7316 | QEMU_OPTION_snapshot, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 7317 | #ifdef TARGET_I386 |
| 7318 | QEMU_OPTION_no_fd_bootchk, |
| 7319 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7320 | QEMU_OPTION_m, |
| 7321 | QEMU_OPTION_nographic, |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 7322 | QEMU_OPTION_portrait, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7323 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7324 | QEMU_OPTION_audio_help, |
| 7325 | QEMU_OPTION_soundhw, |
| 7326 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7327 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7328 | QEMU_OPTION_net, |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7329 | QEMU_OPTION_tftp, |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 7330 | QEMU_OPTION_bootp, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 7331 | QEMU_OPTION_smb, |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7332 | QEMU_OPTION_redir, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7333 | |
| 7334 | QEMU_OPTION_kernel, |
| 7335 | QEMU_OPTION_append, |
| 7336 | QEMU_OPTION_initrd, |
| 7337 | |
| 7338 | QEMU_OPTION_S, |
| 7339 | QEMU_OPTION_s, |
| 7340 | QEMU_OPTION_p, |
| 7341 | QEMU_OPTION_d, |
| 7342 | QEMU_OPTION_hdachs, |
| 7343 | QEMU_OPTION_L, |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 7344 | QEMU_OPTION_bios, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 7345 | QEMU_OPTION_k, |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 7346 | QEMU_OPTION_localtime, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 7347 | QEMU_OPTION_cirrusvga, |
ths | d34cab9 | 2007-04-02 01:10:46 +0000 | [diff] [blame] | 7348 | QEMU_OPTION_vmsvga, |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 7349 | QEMU_OPTION_g, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 7350 | QEMU_OPTION_std_vga, |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 7351 | QEMU_OPTION_echr, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7352 | QEMU_OPTION_monitor, |
| 7353 | QEMU_OPTION_serial, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7354 | QEMU_OPTION_parallel, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7355 | QEMU_OPTION_loadvm, |
| 7356 | QEMU_OPTION_full_screen, |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 7357 | QEMU_OPTION_no_frame, |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 7358 | QEMU_OPTION_alt_grab, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7359 | QEMU_OPTION_no_quit, |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 7360 | QEMU_OPTION_pidfile, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7361 | QEMU_OPTION_no_kqemu, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7362 | QEMU_OPTION_kernel_kqemu, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 7363 | QEMU_OPTION_win2k_hack, |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 7364 | QEMU_OPTION_usb, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7365 | QEMU_OPTION_usbdevice, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7366 | QEMU_OPTION_smp, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7367 | QEMU_OPTION_vnc, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 7368 | QEMU_OPTION_no_acpi, |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 7369 | QEMU_OPTION_curses, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 7370 | QEMU_OPTION_no_reboot, |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 7371 | QEMU_OPTION_no_shutdown, |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 7372 | QEMU_OPTION_show_cursor, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7373 | QEMU_OPTION_daemonize, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7374 | QEMU_OPTION_option_rom, |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 7375 | QEMU_OPTION_semihosting, |
| 7376 | QEMU_OPTION_name, |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 7377 | QEMU_OPTION_prom_env, |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 7378 | QEMU_OPTION_old_param, |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 7379 | QEMU_OPTION_clock, |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 7380 | QEMU_OPTION_startdate, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7381 | }; |
| 7382 | |
| 7383 | typedef struct QEMUOption { |
| 7384 | const char *name; |
| 7385 | int flags; |
| 7386 | int index; |
| 7387 | } QEMUOption; |
| 7388 | |
| 7389 | const QEMUOption qemu_options[] = { |
| 7390 | { "h", 0, QEMU_OPTION_h }, |
pbrook | 64423fb | 2007-01-27 17:11:41 +0000 | [diff] [blame] | 7391 | { "help", 0, QEMU_OPTION_h }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7392 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7393 | { "M", HAS_ARG, QEMU_OPTION_M }, |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 7394 | { "cpu", HAS_ARG, QEMU_OPTION_cpu }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7395 | { "fda", HAS_ARG, QEMU_OPTION_fda }, |
| 7396 | { "fdb", HAS_ARG, QEMU_OPTION_fdb }, |
| 7397 | { "hda", HAS_ARG, QEMU_OPTION_hda }, |
| 7398 | { "hdb", HAS_ARG, QEMU_OPTION_hdb }, |
| 7399 | { "hdc", HAS_ARG, QEMU_OPTION_hdc }, |
| 7400 | { "hdd", HAS_ARG, QEMU_OPTION_hdd }, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7401 | { "drive", HAS_ARG, QEMU_OPTION_drive }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7402 | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom }, |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 7403 | { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock }, |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 7404 | { "sd", HAS_ARG, QEMU_OPTION_sd }, |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 7405 | { "pflash", HAS_ARG, QEMU_OPTION_pflash }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7406 | { "boot", HAS_ARG, QEMU_OPTION_boot }, |
| 7407 | { "snapshot", 0, QEMU_OPTION_snapshot }, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 7408 | #ifdef TARGET_I386 |
| 7409 | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
| 7410 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7411 | { "m", HAS_ARG, QEMU_OPTION_m }, |
| 7412 | { "nographic", 0, QEMU_OPTION_nographic }, |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 7413 | { "portrait", 0, QEMU_OPTION_portrait }, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 7414 | { "k", HAS_ARG, QEMU_OPTION_k }, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7415 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7416 | { "audio-help", 0, QEMU_OPTION_audio_help }, |
| 7417 | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw }, |
| 7418 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7419 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7420 | { "net", HAS_ARG, QEMU_OPTION_net}, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 7421 | #ifdef CONFIG_SLIRP |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7422 | { "tftp", HAS_ARG, QEMU_OPTION_tftp }, |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 7423 | { "bootp", HAS_ARG, QEMU_OPTION_bootp }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7424 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 7425 | { "smb", HAS_ARG, QEMU_OPTION_smb }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7426 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7427 | { "redir", HAS_ARG, QEMU_OPTION_redir }, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 7428 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7429 | |
| 7430 | { "kernel", HAS_ARG, QEMU_OPTION_kernel }, |
| 7431 | { "append", HAS_ARG, QEMU_OPTION_append }, |
| 7432 | { "initrd", HAS_ARG, QEMU_OPTION_initrd }, |
| 7433 | |
| 7434 | { "S", 0, QEMU_OPTION_S }, |
| 7435 | { "s", 0, QEMU_OPTION_s }, |
| 7436 | { "p", HAS_ARG, QEMU_OPTION_p }, |
| 7437 | { "d", HAS_ARG, QEMU_OPTION_d }, |
| 7438 | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs }, |
| 7439 | { "L", HAS_ARG, QEMU_OPTION_L }, |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 7440 | { "bios", HAS_ARG, QEMU_OPTION_bios }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7441 | #ifdef USE_KQEMU |
| 7442 | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7443 | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7444 | #endif |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 7445 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 7446 | { "g", 1, QEMU_OPTION_g }, |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 7447 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 7448 | { "localtime", 0, QEMU_OPTION_localtime }, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 7449 | { "std-vga", 0, QEMU_OPTION_std_vga }, |
balrog | 8b6e072 | 2007-06-22 08:23:44 +0000 | [diff] [blame] | 7450 | { "echr", HAS_ARG, QEMU_OPTION_echr }, |
| 7451 | { "monitor", HAS_ARG, QEMU_OPTION_monitor }, |
| 7452 | { "serial", HAS_ARG, QEMU_OPTION_serial }, |
| 7453 | { "parallel", HAS_ARG, QEMU_OPTION_parallel }, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7454 | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm }, |
| 7455 | { "full-screen", 0, QEMU_OPTION_full_screen }, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7456 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 7457 | { "no-frame", 0, QEMU_OPTION_no_frame }, |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 7458 | { "alt-grab", 0, QEMU_OPTION_alt_grab }, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7459 | { "no-quit", 0, QEMU_OPTION_no_quit }, |
| 7460 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 7461 | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile }, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 7462 | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7463 | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice }, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7464 | { "smp", HAS_ARG, QEMU_OPTION_smp }, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7465 | { "vnc", HAS_ARG, QEMU_OPTION_vnc }, |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 7466 | #ifdef CONFIG_CURSES |
| 7467 | { "curses", 0, QEMU_OPTION_curses }, |
| 7468 | #endif |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7469 | |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 7470 | /* temporary options */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7471 | { "usb", 0, QEMU_OPTION_usb }, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 7472 | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
ths | d34cab9 | 2007-04-02 01:10:46 +0000 | [diff] [blame] | 7473 | { "vmwarevga", 0, QEMU_OPTION_vmsvga }, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 7474 | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 7475 | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 7476 | { "no-shutdown", 0, QEMU_OPTION_no_shutdown }, |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 7477 | { "show-cursor", 0, QEMU_OPTION_show_cursor }, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7478 | { "daemonize", 0, QEMU_OPTION_daemonize }, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7479 | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom }, |
pbrook | a87295e | 2007-05-26 15:09:38 +0000 | [diff] [blame] | 7480 | #if defined(TARGET_ARM) || defined(TARGET_M68K) |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 7481 | { "semihosting", 0, QEMU_OPTION_semihosting }, |
| 7482 | #endif |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 7483 | { "name", HAS_ARG, QEMU_OPTION_name }, |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 7484 | #if defined(TARGET_SPARC) |
| 7485 | { "prom-env", HAS_ARG, QEMU_OPTION_prom_env }, |
| 7486 | #endif |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 7487 | #if defined(TARGET_ARM) |
| 7488 | { "old-param", 0, QEMU_OPTION_old_param }, |
| 7489 | #endif |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 7490 | { "clock", HAS_ARG, QEMU_OPTION_clock }, |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 7491 | { "startdate", HAS_ARG, QEMU_OPTION_startdate }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7492 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 7493 | }; |
| 7494 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7495 | /* password input */ |
| 7496 | |
balrog | 2bac601 | 2007-04-30 01:34:31 +0000 | [diff] [blame] | 7497 | int qemu_key_check(BlockDriverState *bs, const char *name) |
| 7498 | { |
| 7499 | char password[256]; |
| 7500 | int i; |
| 7501 | |
| 7502 | if (!bdrv_is_encrypted(bs)) |
| 7503 | return 0; |
| 7504 | |
| 7505 | term_printf("%s is encrypted.\n", name); |
| 7506 | for(i = 0; i < 3; i++) { |
| 7507 | monitor_readline("Password: ", 1, password, sizeof(password)); |
| 7508 | if (bdrv_set_key(bs, password) == 0) |
| 7509 | return 0; |
| 7510 | term_printf("invalid password\n"); |
| 7511 | } |
| 7512 | return -EPERM; |
| 7513 | } |
| 7514 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7515 | static BlockDriverState *get_bdrv(int index) |
| 7516 | { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7517 | if (index > nb_drives) |
| 7518 | return NULL; |
| 7519 | return drives_table[index].bdrv; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7520 | } |
| 7521 | |
| 7522 | static void read_passwords(void) |
| 7523 | { |
| 7524 | BlockDriverState *bs; |
balrog | 2bac601 | 2007-04-30 01:34:31 +0000 | [diff] [blame] | 7525 | int i; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7526 | |
| 7527 | for(i = 0; i < 6; i++) { |
| 7528 | bs = get_bdrv(i); |
balrog | 2bac601 | 2007-04-30 01:34:31 +0000 | [diff] [blame] | 7529 | if (bs) |
| 7530 | qemu_key_check(bs, bdrv_get_device_name(bs)); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7531 | } |
| 7532 | } |
| 7533 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7534 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7535 | struct soundhw soundhw[] = { |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 7536 | #ifdef HAS_AUDIO_CHOICE |
aurel32 | 4ce7ff6 | 2008-04-07 19:47:14 +0000 | [diff] [blame] | 7537 | #if defined(TARGET_I386) || defined(TARGET_MIPS) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 7538 | { |
| 7539 | "pcspk", |
| 7540 | "PC speaker", |
| 7541 | 0, |
| 7542 | 1, |
| 7543 | { .init_isa = pcspk_audio_init } |
| 7544 | }, |
| 7545 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7546 | { |
| 7547 | "sb16", |
| 7548 | "Creative Sound Blaster 16", |
| 7549 | 0, |
| 7550 | 1, |
| 7551 | { .init_isa = SB16_init } |
| 7552 | }, |
| 7553 | |
| 7554 | #ifdef CONFIG_ADLIB |
| 7555 | { |
| 7556 | "adlib", |
| 7557 | #ifdef HAS_YMF262 |
| 7558 | "Yamaha YMF262 (OPL3)", |
| 7559 | #else |
| 7560 | "Yamaha YM3812 (OPL2)", |
| 7561 | #endif |
| 7562 | 0, |
| 7563 | 1, |
| 7564 | { .init_isa = Adlib_init } |
| 7565 | }, |
| 7566 | #endif |
| 7567 | |
| 7568 | #ifdef CONFIG_GUS |
| 7569 | { |
| 7570 | "gus", |
| 7571 | "Gravis Ultrasound GF1", |
| 7572 | 0, |
| 7573 | 1, |
| 7574 | { .init_isa = GUS_init } |
| 7575 | }, |
| 7576 | #endif |
| 7577 | |
balrog | e5c9a13 | 2008-01-14 04:27:55 +0000 | [diff] [blame] | 7578 | #ifdef CONFIG_AC97 |
| 7579 | { |
| 7580 | "ac97", |
| 7581 | "Intel 82801AA AC97 Audio", |
| 7582 | 0, |
| 7583 | 0, |
| 7584 | { .init_pci = ac97_init } |
| 7585 | }, |
| 7586 | #endif |
| 7587 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7588 | { |
| 7589 | "es1370", |
| 7590 | "ENSONIQ AudioPCI ES1370", |
| 7591 | 0, |
| 7592 | 0, |
| 7593 | { .init_pci = es1370_init } |
| 7594 | }, |
balrog | b00052e | 2007-04-30 02:22:06 +0000 | [diff] [blame] | 7595 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7596 | |
| 7597 | { NULL, NULL, 0, 0, { NULL } } |
| 7598 | }; |
| 7599 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7600 | static void select_soundhw (const char *optarg) |
| 7601 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7602 | struct soundhw *c; |
| 7603 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7604 | if (*optarg == '?') { |
| 7605 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7606 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7607 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7608 | for (c = soundhw; c->name; ++c) { |
| 7609 | printf ("%-11s %s\n", c->name, c->descr); |
| 7610 | } |
| 7611 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7612 | exit (*optarg != '?'); |
| 7613 | } |
| 7614 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7615 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7616 | const char *p; |
| 7617 | char *e; |
| 7618 | int bad_card = 0; |
| 7619 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7620 | if (!strcmp (optarg, "all")) { |
| 7621 | for (c = soundhw; c->name; ++c) { |
| 7622 | c->enabled = 1; |
| 7623 | } |
| 7624 | return; |
| 7625 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7626 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7627 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7628 | while (*p) { |
| 7629 | e = strchr (p, ','); |
| 7630 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7631 | |
| 7632 | for (c = soundhw; c->name; ++c) { |
| 7633 | if (!strncmp (c->name, p, l)) { |
| 7634 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7635 | break; |
| 7636 | } |
| 7637 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 7638 | |
| 7639 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7640 | if (l > 80) { |
| 7641 | fprintf (stderr, |
| 7642 | "Unknown sound card name (too big to show)\n"); |
| 7643 | } |
| 7644 | else { |
| 7645 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 7646 | (int) l, p); |
| 7647 | } |
| 7648 | bad_card = 1; |
| 7649 | } |
| 7650 | p += l + (e != NULL); |
| 7651 | } |
| 7652 | |
| 7653 | if (bad_card) |
| 7654 | goto show_valid_cards; |
| 7655 | } |
| 7656 | } |
| 7657 | #endif |
| 7658 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 7659 | #ifdef _WIN32 |
| 7660 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 7661 | { |
| 7662 | exit(STATUS_CONTROL_C_EXIT); |
| 7663 | return TRUE; |
| 7664 | } |
| 7665 | #endif |
| 7666 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7667 | #define MAX_NET_CLIENTS 32 |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7668 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7669 | int main(int argc, char **argv) |
| 7670 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7671 | #ifdef CONFIG_GDBSTUB |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 7672 | int use_gdbstub; |
| 7673 | const char *gdbstub_port; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7674 | #endif |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 7675 | uint32_t boot_devices_bitmap = 0; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7676 | int i; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 7677 | int snapshot, linux_boot, net_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7678 | const char *initrd_filename; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7679 | const char *kernel_filename, *kernel_cmdline; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 7680 | const char *boot_devices = ""; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7681 | DisplayState *ds = &display_state; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7682 | int cyls, heads, secs, translation; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 7683 | const char *net_clients[MAX_NET_CLIENTS]; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7684 | int nb_net_clients; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7685 | int hda_index; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7686 | int optind; |
| 7687 | const char *r, *optarg; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7688 | CharDriverState *monitor_hd; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 7689 | const char *monitor_device; |
| 7690 | const char *serial_devices[MAX_SERIAL_PORTS]; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7691 | int serial_device_index; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 7692 | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7693 | int parallel_device_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7694 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7695 | QEMUMachine *machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 7696 | const char *cpu_model; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 7697 | const char *usb_devices[MAX_USB_CMDLINE]; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7698 | int usb_devices_index; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7699 | int fds[2]; |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 7700 | const char *pid_file = NULL; |
blueswir1 | 833c717 | 2007-05-27 19:36:43 +0000 | [diff] [blame] | 7701 | VLANState *vlan; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 7702 | |
| 7703 | LIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 7704 | #ifndef _WIN32 |
| 7705 | { |
| 7706 | struct sigaction act; |
| 7707 | sigfillset(&act.sa_mask); |
| 7708 | act.sa_flags = 0; |
| 7709 | act.sa_handler = SIG_IGN; |
| 7710 | sigaction(SIGPIPE, &act, NULL); |
| 7711 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 7712 | #else |
| 7713 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 7714 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 7715 | QEMU to run on a single CPU */ |
| 7716 | { |
| 7717 | HANDLE h; |
| 7718 | DWORD mask, smask; |
| 7719 | int i; |
| 7720 | h = GetCurrentProcess(); |
| 7721 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 7722 | for(i = 0; i < 32; i++) { |
| 7723 | if (mask & (1 << i)) |
| 7724 | break; |
| 7725 | } |
| 7726 | if (i != 32) { |
| 7727 | mask = 1 << i; |
| 7728 | SetProcessAffinityMask(h, mask); |
| 7729 | } |
| 7730 | } |
| 7731 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7732 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 7733 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7734 | register_machines(); |
| 7735 | machine = first_machine; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 7736 | cpu_model = NULL; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 7737 | initrd_filename = NULL; |
aurel32 | 4fc5d07 | 2008-04-27 21:39:40 +0000 | [diff] [blame] | 7738 | ram_size = 0; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7739 | vga_ram_size = VGA_RAM_SIZE; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7740 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 7741 | use_gdbstub = 0; |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 7742 | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7743 | #endif |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 7744 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7745 | nographic = 0; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 7746 | curses = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7747 | kernel_filename = NULL; |
| 7748 | kernel_cmdline = ""; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7749 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7750 | translation = BIOS_ATA_TRANSLATION_AUTO; |
aurel32 | c75a823 | 2008-05-04 00:50:34 +0000 | [diff] [blame] | 7751 | monitor_device = "vc:800x600"; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7752 | |
aurel32 | c75a823 | 2008-05-04 00:50:34 +0000 | [diff] [blame] | 7753 | serial_devices[0] = "vc:80Cx24C"; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7754 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 7755 | serial_devices[i] = NULL; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7756 | serial_device_index = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7757 | |
aurel32 | c75a823 | 2008-05-04 00:50:34 +0000 | [diff] [blame] | 7758 | parallel_devices[0] = "vc:640x480"; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7759 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 7760 | parallel_devices[i] = NULL; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7761 | parallel_device_index = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7762 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7763 | usb_devices_index = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7764 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7765 | nb_net_clients = 0; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7766 | nb_drives = 0; |
| 7767 | nb_drives_opt = 0; |
| 7768 | hda_index = -1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7769 | |
| 7770 | nb_nics = 0; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 7771 | /* default mac address of the first network interface */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7772 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7773 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7774 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7775 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7776 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7777 | r = argv[optind]; |
| 7778 | if (r[0] != '-') { |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7779 | hda_index = drive_add(argv[optind++], HD_ALIAS, 0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7780 | } else { |
| 7781 | const QEMUOption *popt; |
| 7782 | |
| 7783 | optind++; |
pbrook | dff5efc | 2007-01-27 17:19:39 +0000 | [diff] [blame] | 7784 | /* Treat --foo the same as -foo. */ |
| 7785 | if (r[1] == '-') |
| 7786 | r++; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7787 | popt = qemu_options; |
| 7788 | for(;;) { |
| 7789 | if (!popt->name) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 7790 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7791 | argv[0], r); |
| 7792 | exit(1); |
| 7793 | } |
| 7794 | if (!strcmp(popt->name, r + 1)) |
| 7795 | break; |
| 7796 | popt++; |
| 7797 | } |
| 7798 | if (popt->flags & HAS_ARG) { |
| 7799 | if (optind >= argc) { |
| 7800 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 7801 | argv[0], r); |
| 7802 | exit(1); |
| 7803 | } |
| 7804 | optarg = argv[optind++]; |
| 7805 | } else { |
| 7806 | optarg = NULL; |
| 7807 | } |
| 7808 | |
| 7809 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7810 | case QEMU_OPTION_M: |
| 7811 | machine = find_machine(optarg); |
| 7812 | if (!machine) { |
| 7813 | QEMUMachine *m; |
| 7814 | printf("Supported machines are:\n"); |
| 7815 | for(m = first_machine; m != NULL; m = m->next) { |
| 7816 | printf("%-10s %s%s\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 7817 | m->name, m->desc, |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7818 | m == first_machine ? " (default)" : ""); |
| 7819 | } |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 7820 | exit(*optarg != '?'); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7821 | } |
| 7822 | break; |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 7823 | case QEMU_OPTION_cpu: |
| 7824 | /* hw initialization will check this */ |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 7825 | if (*optarg == '?') { |
j_mayer | c732abe | 2007-10-12 06:47:46 +0000 | [diff] [blame] | 7826 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
| 7827 | #if defined(cpu_list) |
| 7828 | cpu_list(stdout, &fprintf); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 7829 | #endif |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 7830 | exit(0); |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 7831 | } else { |
| 7832 | cpu_model = optarg; |
| 7833 | } |
| 7834 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7835 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 7836 | initrd_filename = optarg; |
| 7837 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7838 | case QEMU_OPTION_hda: |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7839 | if (cyls == 0) |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7840 | hda_index = drive_add(optarg, HD_ALIAS, 0); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7841 | else |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7842 | hda_index = drive_add(optarg, HD_ALIAS |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7843 | ",cyls=%d,heads=%d,secs=%d%s", |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7844 | 0, cyls, heads, secs, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7845 | translation == BIOS_ATA_TRANSLATION_LBA ? |
| 7846 | ",trans=lba" : |
| 7847 | translation == BIOS_ATA_TRANSLATION_NONE ? |
| 7848 | ",trans=none" : ""); |
| 7849 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7850 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7851 | case QEMU_OPTION_hdc: |
| 7852 | case QEMU_OPTION_hdd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7853 | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 7854 | break; |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7855 | case QEMU_OPTION_drive: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7856 | drive_add(NULL, "%s", optarg); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7857 | break; |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 7858 | case QEMU_OPTION_mtdblock: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7859 | drive_add(optarg, MTD_ALIAS); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 7860 | break; |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 7861 | case QEMU_OPTION_sd: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7862 | drive_add(optarg, SD_ALIAS); |
pbrook | a1bb27b | 2007-04-06 16:49:48 +0000 | [diff] [blame] | 7863 | break; |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 7864 | case QEMU_OPTION_pflash: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7865 | drive_add(optarg, PFLASH_ALIAS); |
j_mayer | 86f5566 | 2007-04-24 06:52:59 +0000 | [diff] [blame] | 7866 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7867 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 7868 | snapshot = 1; |
| 7869 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7870 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7871 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7872 | const char *p; |
| 7873 | p = optarg; |
| 7874 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7875 | if (cyls < 1 || cyls > 16383) |
| 7876 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7877 | if (*p != ',') |
| 7878 | goto chs_fail; |
| 7879 | p++; |
| 7880 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7881 | if (heads < 1 || heads > 16) |
| 7882 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7883 | if (*p != ',') |
| 7884 | goto chs_fail; |
| 7885 | p++; |
| 7886 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7887 | if (secs < 1 || secs > 63) |
| 7888 | goto chs_fail; |
| 7889 | if (*p == ',') { |
| 7890 | p++; |
| 7891 | if (!strcmp(p, "none")) |
| 7892 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 7893 | else if (!strcmp(p, "lba")) |
| 7894 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 7895 | else if (!strcmp(p, "auto")) |
| 7896 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 7897 | else |
| 7898 | goto chs_fail; |
| 7899 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7900 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7901 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 7902 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7903 | } |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7904 | if (hda_index != -1) |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7905 | snprintf(drives_opt[hda_index].opt, |
| 7906 | sizeof(drives_opt[hda_index].opt), |
| 7907 | HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s", |
| 7908 | 0, cyls, heads, secs, |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 7909 | translation == BIOS_ATA_TRANSLATION_LBA ? |
| 7910 | ",trans=lba" : |
| 7911 | translation == BIOS_ATA_TRANSLATION_NONE ? |
| 7912 | ",trans=none" : ""); |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7913 | } |
| 7914 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7915 | case QEMU_OPTION_nographic: |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 7916 | serial_devices[0] = "stdio"; |
| 7917 | parallel_devices[0] = "null"; |
| 7918 | monitor_device = "stdio"; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7919 | nographic = 1; |
| 7920 | break; |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 7921 | #ifdef CONFIG_CURSES |
| 7922 | case QEMU_OPTION_curses: |
| 7923 | curses = 1; |
| 7924 | break; |
| 7925 | #endif |
balrog | a171fe3 | 2007-04-30 01:48:07 +0000 | [diff] [blame] | 7926 | case QEMU_OPTION_portrait: |
| 7927 | graphic_rotate = 1; |
| 7928 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7929 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7930 | kernel_filename = optarg; |
| 7931 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7932 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7933 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7934 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7935 | case QEMU_OPTION_cdrom: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7936 | drive_add(optarg, CDROM_ALIAS); |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7937 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7938 | case QEMU_OPTION_boot: |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 7939 | boot_devices = optarg; |
| 7940 | /* We just do some generic consistency checks */ |
| 7941 | { |
| 7942 | /* Could easily be extended to 64 devices if needed */ |
ths | 60fe76f | 2007-12-16 03:02:09 +0000 | [diff] [blame] | 7943 | const char *p; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 7944 | |
| 7945 | boot_devices_bitmap = 0; |
| 7946 | for (p = boot_devices; *p != '\0'; p++) { |
| 7947 | /* Allowed boot devices are: |
| 7948 | * a b : floppy disk drives |
| 7949 | * c ... f : IDE disk drives |
| 7950 | * g ... m : machine implementation dependant drives |
| 7951 | * n ... p : network devices |
| 7952 | * It's up to each machine implementation to check |
| 7953 | * if the given boot devices match the actual hardware |
| 7954 | * implementation and firmware features. |
| 7955 | */ |
| 7956 | if (*p < 'a' || *p > 'q') { |
| 7957 | fprintf(stderr, "Invalid boot device '%c'\n", *p); |
| 7958 | exit(1); |
| 7959 | } |
| 7960 | if (boot_devices_bitmap & (1 << (*p - 'a'))) { |
| 7961 | fprintf(stderr, |
| 7962 | "Boot device '%c' was given twice\n",*p); |
| 7963 | exit(1); |
| 7964 | } |
| 7965 | boot_devices_bitmap |= 1 << (*p - 'a'); |
| 7966 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7967 | } |
| 7968 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7969 | case QEMU_OPTION_fda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7970 | case QEMU_OPTION_fdb: |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 7971 | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7972 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 7973 | #ifdef TARGET_I386 |
| 7974 | case QEMU_OPTION_no_fd_bootchk: |
| 7975 | fd_bootchk = 0; |
| 7976 | break; |
| 7977 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7978 | case QEMU_OPTION_net: |
| 7979 | if (nb_net_clients >= MAX_NET_CLIENTS) { |
| 7980 | fprintf(stderr, "qemu: too many network clients\n"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7981 | exit(1); |
| 7982 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 7983 | net_clients[nb_net_clients] = optarg; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7984 | nb_net_clients++; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 7985 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7986 | #ifdef CONFIG_SLIRP |
| 7987 | case QEMU_OPTION_tftp: |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7988 | tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7989 | break; |
ths | 47d5d01 | 2007-02-20 00:05:08 +0000 | [diff] [blame] | 7990 | case QEMU_OPTION_bootp: |
| 7991 | bootp_filename = optarg; |
| 7992 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7993 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 7994 | case QEMU_OPTION_smb: |
| 7995 | net_slirp_smb(optarg); |
| 7996 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7997 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7998 | case QEMU_OPTION_redir: |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 7999 | net_slirp_redir(optarg); |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 8000 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 8001 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 8002 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 8003 | case QEMU_OPTION_audio_help: |
| 8004 | AUD_help (); |
| 8005 | exit (0); |
| 8006 | break; |
| 8007 | case QEMU_OPTION_soundhw: |
| 8008 | select_soundhw (optarg); |
| 8009 | break; |
| 8010 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8011 | case QEMU_OPTION_h: |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 8012 | help(0); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8013 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 8014 | case QEMU_OPTION_m: { |
| 8015 | uint64_t value; |
| 8016 | char *ptr; |
| 8017 | |
| 8018 | value = strtoul(optarg, &ptr, 10); |
| 8019 | switch (*ptr) { |
| 8020 | case 0: case 'M': case 'm': |
| 8021 | value <<= 20; |
| 8022 | break; |
| 8023 | case 'G': case 'g': |
| 8024 | value <<= 30; |
| 8025 | break; |
| 8026 | default: |
| 8027 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8028 | exit(1); |
| 8029 | } |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 8030 | |
| 8031 | /* On 32-bit hosts, QEMU is limited by virtual address space */ |
| 8032 | if (value > (2047 << 20) |
| 8033 | #ifndef USE_KQEMU |
| 8034 | && HOST_LONG_BITS == 32 |
| 8035 | #endif |
| 8036 | ) { |
| 8037 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n"); |
| 8038 | exit(1); |
| 8039 | } |
| 8040 | if (value != (uint64_t)(ram_addr_t)value) { |
| 8041 | fprintf(stderr, "qemu: ram size too large\n"); |
| 8042 | exit(1); |
| 8043 | } |
| 8044 | ram_size = value; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8045 | break; |
aurel32 | 00f82b8 | 2008-04-27 21:12:55 +0000 | [diff] [blame] | 8046 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8047 | case QEMU_OPTION_d: |
| 8048 | { |
| 8049 | int mask; |
| 8050 | CPULogItem *item; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8051 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8052 | mask = cpu_str_to_log_mask(optarg); |
| 8053 | if (!mask) { |
| 8054 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 8055 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 8056 | printf("%-10s %s\n", item->name, item->help); |
| 8057 | } |
| 8058 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8059 | } |
| 8060 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 8061 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8062 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 8063 | #ifdef CONFIG_GDBSTUB |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8064 | case QEMU_OPTION_s: |
| 8065 | use_gdbstub = 1; |
| 8066 | break; |
| 8067 | case QEMU_OPTION_p: |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 8068 | gdbstub_port = optarg; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8069 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 8070 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8071 | case QEMU_OPTION_L: |
| 8072 | bios_dir = optarg; |
| 8073 | break; |
j_mayer | 1192dad | 2007-10-05 13:08:35 +0000 | [diff] [blame] | 8074 | case QEMU_OPTION_bios: |
| 8075 | bios_name = optarg; |
| 8076 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8077 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 8078 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8079 | break; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 8080 | case QEMU_OPTION_k: |
| 8081 | keyboard_layout = optarg; |
| 8082 | break; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 8083 | case QEMU_OPTION_localtime: |
| 8084 | rtc_utc = 0; |
| 8085 | break; |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 8086 | case QEMU_OPTION_cirrusvga: |
| 8087 | cirrus_vga_enabled = 1; |
ths | d34cab9 | 2007-04-02 01:10:46 +0000 | [diff] [blame] | 8088 | vmsvga_enabled = 0; |
| 8089 | break; |
| 8090 | case QEMU_OPTION_vmsvga: |
| 8091 | cirrus_vga_enabled = 0; |
| 8092 | vmsvga_enabled = 1; |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 8093 | break; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 8094 | case QEMU_OPTION_std_vga: |
| 8095 | cirrus_vga_enabled = 0; |
ths | d34cab9 | 2007-04-02 01:10:46 +0000 | [diff] [blame] | 8096 | vmsvga_enabled = 0; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 8097 | break; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 8098 | case QEMU_OPTION_g: |
| 8099 | { |
| 8100 | const char *p; |
| 8101 | int w, h, depth; |
| 8102 | p = optarg; |
| 8103 | w = strtol(p, (char **)&p, 10); |
| 8104 | if (w <= 0) { |
| 8105 | graphic_error: |
| 8106 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 8107 | exit(1); |
| 8108 | } |
| 8109 | if (*p != 'x') |
| 8110 | goto graphic_error; |
| 8111 | p++; |
| 8112 | h = strtol(p, (char **)&p, 10); |
| 8113 | if (h <= 0) |
| 8114 | goto graphic_error; |
| 8115 | if (*p == 'x') { |
| 8116 | p++; |
| 8117 | depth = strtol(p, (char **)&p, 10); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 8118 | if (depth != 8 && depth != 15 && depth != 16 && |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 8119 | depth != 24 && depth != 32) |
| 8120 | goto graphic_error; |
| 8121 | } else if (*p == '\0') { |
| 8122 | depth = graphic_depth; |
| 8123 | } else { |
| 8124 | goto graphic_error; |
| 8125 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8126 | |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 8127 | graphic_width = w; |
| 8128 | graphic_height = h; |
| 8129 | graphic_depth = depth; |
| 8130 | } |
| 8131 | break; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 8132 | case QEMU_OPTION_echr: |
| 8133 | { |
| 8134 | char *r; |
| 8135 | term_escape_char = strtol(optarg, &r, 0); |
| 8136 | if (r == optarg) |
| 8137 | printf("Bad argument to echr\n"); |
| 8138 | break; |
| 8139 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 8140 | case QEMU_OPTION_monitor: |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8141 | monitor_device = optarg; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 8142 | break; |
| 8143 | case QEMU_OPTION_serial: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 8144 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
| 8145 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 8146 | exit(1); |
| 8147 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8148 | serial_devices[serial_device_index] = optarg; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 8149 | serial_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 8150 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 8151 | case QEMU_OPTION_parallel: |
| 8152 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { |
| 8153 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 8154 | exit(1); |
| 8155 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8156 | parallel_devices[parallel_device_index] = optarg; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 8157 | parallel_device_index++; |
| 8158 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 8159 | case QEMU_OPTION_loadvm: |
| 8160 | loadvm = optarg; |
| 8161 | break; |
| 8162 | case QEMU_OPTION_full_screen: |
| 8163 | full_screen = 1; |
| 8164 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 8165 | #ifdef CONFIG_SDL |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 8166 | case QEMU_OPTION_no_frame: |
| 8167 | no_frame = 1; |
| 8168 | break; |
ths | 3780e19 | 2007-06-21 21:08:02 +0000 | [diff] [blame] | 8169 | case QEMU_OPTION_alt_grab: |
| 8170 | alt_grab = 1; |
| 8171 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 8172 | case QEMU_OPTION_no_quit: |
| 8173 | no_quit = 1; |
| 8174 | break; |
| 8175 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 8176 | case QEMU_OPTION_pidfile: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 8177 | pid_file = optarg; |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 8178 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 8179 | #ifdef TARGET_I386 |
| 8180 | case QEMU_OPTION_win2k_hack: |
| 8181 | win2k_install_hack = 1; |
| 8182 | break; |
| 8183 | #endif |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 8184 | #ifdef USE_KQEMU |
| 8185 | case QEMU_OPTION_no_kqemu: |
| 8186 | kqemu_allowed = 0; |
| 8187 | break; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 8188 | case QEMU_OPTION_kernel_kqemu: |
| 8189 | kqemu_allowed = 2; |
| 8190 | break; |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 8191 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 8192 | case QEMU_OPTION_usb: |
| 8193 | usb_enabled = 1; |
| 8194 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 8195 | case QEMU_OPTION_usbdevice: |
| 8196 | usb_enabled = 1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 8197 | if (usb_devices_index >= MAX_USB_CMDLINE) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 8198 | fprintf(stderr, "Too many USB devices\n"); |
| 8199 | exit(1); |
| 8200 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8201 | usb_devices[usb_devices_index] = optarg; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 8202 | usb_devices_index++; |
| 8203 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 8204 | case QEMU_OPTION_smp: |
| 8205 | smp_cpus = atoi(optarg); |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 8206 | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 8207 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 8208 | exit(1); |
| 8209 | } |
| 8210 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 8211 | case QEMU_OPTION_vnc: |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 8212 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 8213 | break; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 8214 | case QEMU_OPTION_no_acpi: |
| 8215 | acpi_enabled = 0; |
| 8216 | break; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 8217 | case QEMU_OPTION_no_reboot: |
| 8218 | no_reboot = 1; |
| 8219 | break; |
aurel32 | b2f7616 | 2008-04-11 21:35:52 +0000 | [diff] [blame] | 8220 | case QEMU_OPTION_no_shutdown: |
| 8221 | no_shutdown = 1; |
| 8222 | break; |
balrog | 9467cd4 | 2007-05-01 01:34:14 +0000 | [diff] [blame] | 8223 | case QEMU_OPTION_show_cursor: |
| 8224 | cursor_hide = 0; |
| 8225 | break; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 8226 | case QEMU_OPTION_daemonize: |
| 8227 | daemonize = 1; |
| 8228 | break; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 8229 | case QEMU_OPTION_option_rom: |
| 8230 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 8231 | fprintf(stderr, "Too many option ROMs\n"); |
| 8232 | exit(1); |
| 8233 | } |
| 8234 | option_rom[nb_option_roms] = optarg; |
| 8235 | nb_option_roms++; |
| 8236 | break; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 8237 | case QEMU_OPTION_semihosting: |
| 8238 | semihosting_enabled = 1; |
| 8239 | break; |
ths | c35734b | 2007-03-19 15:17:08 +0000 | [diff] [blame] | 8240 | case QEMU_OPTION_name: |
| 8241 | qemu_name = optarg; |
| 8242 | break; |
blueswir1 | 6650860 | 2007-05-01 14:16:52 +0000 | [diff] [blame] | 8243 | #ifdef TARGET_SPARC |
| 8244 | case QEMU_OPTION_prom_env: |
| 8245 | if (nb_prom_envs >= MAX_PROM_ENVS) { |
| 8246 | fprintf(stderr, "Too many prom variables\n"); |
| 8247 | exit(1); |
| 8248 | } |
| 8249 | prom_envs[nb_prom_envs] = optarg; |
| 8250 | nb_prom_envs++; |
| 8251 | break; |
| 8252 | #endif |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 8253 | #ifdef TARGET_ARM |
| 8254 | case QEMU_OPTION_old_param: |
| 8255 | old_param = 1; |
ths | 05ebd53 | 2008-01-08 19:32:16 +0000 | [diff] [blame] | 8256 | break; |
balrog | 2b8f2d4 | 2007-07-27 22:08:46 +0000 | [diff] [blame] | 8257 | #endif |
ths | f3dcfad | 2007-08-24 01:26:02 +0000 | [diff] [blame] | 8258 | case QEMU_OPTION_clock: |
| 8259 | configure_alarms(optarg); |
| 8260 | break; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 8261 | case QEMU_OPTION_startdate: |
| 8262 | { |
| 8263 | struct tm tm; |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 8264 | time_t rtc_start_date; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 8265 | if (!strcmp(optarg, "now")) { |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 8266 | rtc_date_offset = -1; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 8267 | } else { |
| 8268 | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
| 8269 | &tm.tm_year, |
| 8270 | &tm.tm_mon, |
| 8271 | &tm.tm_mday, |
| 8272 | &tm.tm_hour, |
| 8273 | &tm.tm_min, |
| 8274 | &tm.tm_sec) == 6) { |
| 8275 | /* OK */ |
| 8276 | } else if (sscanf(optarg, "%d-%d-%d", |
| 8277 | &tm.tm_year, |
| 8278 | &tm.tm_mon, |
| 8279 | &tm.tm_mday) == 3) { |
| 8280 | tm.tm_hour = 0; |
| 8281 | tm.tm_min = 0; |
| 8282 | tm.tm_sec = 0; |
| 8283 | } else { |
| 8284 | goto date_fail; |
| 8285 | } |
| 8286 | tm.tm_year -= 1900; |
| 8287 | tm.tm_mon--; |
bellard | 3c6b208 | 2007-11-10 19:36:39 +0000 | [diff] [blame] | 8288 | rtc_start_date = mktimegm(&tm); |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 8289 | if (rtc_start_date == -1) { |
| 8290 | date_fail: |
| 8291 | fprintf(stderr, "Invalid date format. Valid format are:\n" |
| 8292 | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n"); |
| 8293 | exit(1); |
| 8294 | } |
balrog | f650305 | 2008-02-17 11:42:19 +0000 | [diff] [blame] | 8295 | rtc_date_offset = time(NULL) - rtc_start_date; |
bellard | 7e0af5d0 | 2007-11-07 16:24:33 +0000 | [diff] [blame] | 8296 | } |
| 8297 | } |
| 8298 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 8299 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 8300 | } |
| 8301 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 8302 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 8303 | #ifndef _WIN32 |
| 8304 | if (daemonize && !nographic && vnc_display == NULL) { |
| 8305 | fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n"); |
| 8306 | daemonize = 0; |
| 8307 | } |
| 8308 | |
| 8309 | if (daemonize) { |
| 8310 | pid_t pid; |
| 8311 | |
| 8312 | if (pipe(fds) == -1) |
| 8313 | exit(1); |
| 8314 | |
| 8315 | pid = fork(); |
| 8316 | if (pid > 0) { |
| 8317 | uint8_t status; |
| 8318 | ssize_t len; |
| 8319 | |
| 8320 | close(fds[1]); |
| 8321 | |
| 8322 | again: |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 8323 | len = read(fds[0], &status, 1); |
| 8324 | if (len == -1 && (errno == EINTR)) |
| 8325 | goto again; |
| 8326 | |
| 8327 | if (len != 1) |
| 8328 | exit(1); |
| 8329 | else if (status == 1) { |
| 8330 | fprintf(stderr, "Could not acquire pidfile\n"); |
| 8331 | exit(1); |
| 8332 | } else |
| 8333 | exit(0); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 8334 | } else if (pid < 0) |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 8335 | exit(1); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 8336 | |
| 8337 | setsid(); |
| 8338 | |
| 8339 | pid = fork(); |
| 8340 | if (pid > 0) |
| 8341 | exit(0); |
| 8342 | else if (pid < 0) |
| 8343 | exit(1); |
| 8344 | |
| 8345 | umask(027); |
| 8346 | chdir("/"); |
| 8347 | |
| 8348 | signal(SIGTSTP, SIG_IGN); |
| 8349 | signal(SIGTTOU, SIG_IGN); |
| 8350 | signal(SIGTTIN, SIG_IGN); |
| 8351 | } |
| 8352 | #endif |
| 8353 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 8354 | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
ths | 93815bc | 2007-03-19 15:58:31 +0000 | [diff] [blame] | 8355 | if (daemonize) { |
| 8356 | uint8_t status = 1; |
| 8357 | write(fds[1], &status, 1); |
| 8358 | } else |
| 8359 | fprintf(stderr, "Could not acquire pid file\n"); |
| 8360 | exit(1); |
| 8361 | } |
| 8362 | |
bellard | ff3fbb3 | 2006-01-08 10:53:14 +0000 | [diff] [blame] | 8363 | #ifdef USE_KQEMU |
| 8364 | if (smp_cpus > 1) |
| 8365 | kqemu_allowed = 0; |
| 8366 | #endif |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 8367 | linux_boot = (kernel_filename != NULL); |
balrog | 7317b8c | 2007-11-18 02:09:36 +0000 | [diff] [blame] | 8368 | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
balrog | 6c41b27 | 2007-11-17 12:12:29 +0000 | [diff] [blame] | 8369 | |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 8370 | /* XXX: this should not be: some embedded targets just have flash */ |
| 8371 | if (!linux_boot && net_boot == 0 && |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 8372 | nb_drives_opt == 0) |
ths | 15f8220 | 2007-06-29 23:26:08 +0000 | [diff] [blame] | 8373 | help(1); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 8374 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 8375 | /* boot to floppy or the default cd if no hard disk defined yet */ |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 8376 | if (!boot_devices[0]) { |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 8377 | boot_devices = "cad"; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 8378 | } |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 8379 | setvbuf(stdout, NULL, _IOLBF, 0); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 8380 | |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 8381 | init_timers(); |
| 8382 | init_timer_alarm(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 8383 | qemu_aio_init(); |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 8384 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 8385 | #ifdef _WIN32 |
| 8386 | socket_init(); |
| 8387 | #endif |
| 8388 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 8389 | /* init network clients */ |
| 8390 | if (nb_net_clients == 0) { |
| 8391 | /* if no clients, we use a default config */ |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8392 | net_clients[0] = "nic"; |
| 8393 | net_clients[1] = "user"; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 8394 | nb_net_clients = 2; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 8395 | } |
| 8396 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 8397 | for(i = 0;i < nb_net_clients; i++) { |
| 8398 | if (net_client_init(net_clients[i]) < 0) |
| 8399 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 8400 | } |
blueswir1 | 833c717 | 2007-05-27 19:36:43 +0000 | [diff] [blame] | 8401 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 8402 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
| 8403 | continue; |
| 8404 | if (vlan->nb_guest_devs == 0) { |
| 8405 | fprintf(stderr, "Invalid vlan (%d) with no nics\n", vlan->id); |
| 8406 | exit(1); |
| 8407 | } |
| 8408 | if (vlan->nb_host_devs == 0) |
| 8409 | fprintf(stderr, |
| 8410 | "Warning: vlan %d is not connected to host network\n", |
| 8411 | vlan->id); |
| 8412 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 8413 | |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 8414 | #ifdef TARGET_I386 |
balrog | ed494d8 | 2007-12-11 23:23:52 +0000 | [diff] [blame] | 8415 | /* XXX: this should be moved in the PC machine instantiation code */ |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 8416 | if (net_boot != 0) { |
| 8417 | int netroms = 0; |
| 8418 | for (i = 0; i < nb_nics && i < 4; i++) { |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 8419 | const char *model = nd_table[i].model; |
| 8420 | char buf[1024]; |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 8421 | if (net_boot & (1 << i)) { |
| 8422 | if (model == NULL) |
| 8423 | model = "ne2k_pci"; |
| 8424 | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
| 8425 | if (get_image_size(buf) > 0) { |
| 8426 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 8427 | fprintf(stderr, "Too many option ROMs\n"); |
| 8428 | exit(1); |
| 8429 | } |
| 8430 | option_rom[nb_option_roms] = strdup(buf); |
| 8431 | nb_option_roms++; |
| 8432 | netroms++; |
| 8433 | } |
| 8434 | } |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 8435 | } |
j_mayer | 28c5af5 | 2007-11-11 01:50:45 +0000 | [diff] [blame] | 8436 | if (netroms == 0) { |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 8437 | fprintf(stderr, "No valid PXE rom found for network device\n"); |
| 8438 | exit(1); |
| 8439 | } |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 8440 | } |
| 8441 | #endif |
| 8442 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 8443 | /* init the memory */ |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 8444 | phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED; |
| 8445 | |
| 8446 | if (machine->ram_require & RAMSIZE_FIXED) { |
| 8447 | if (ram_size > 0) { |
| 8448 | if (ram_size < phys_ram_size) { |
aurel32 | cd94006 | 2008-04-28 20:26:54 +0000 | [diff] [blame] | 8449 | fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n", |
| 8450 | machine->name, (unsigned long long) phys_ram_size); |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 8451 | exit(-1); |
| 8452 | } |
| 8453 | |
| 8454 | phys_ram_size = ram_size; |
| 8455 | } else |
| 8456 | ram_size = phys_ram_size; |
| 8457 | } else { |
aurel32 | 4fc5d07 | 2008-04-27 21:39:40 +0000 | [diff] [blame] | 8458 | if (ram_size == 0) |
balrog | 7fb4fdc | 2008-04-24 17:59:27 +0000 | [diff] [blame] | 8459 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
| 8460 | |
| 8461 | phys_ram_size += ram_size; |
| 8462 | } |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 8463 | |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 8464 | phys_ram_base = qemu_vmalloc(phys_ram_size); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 8465 | if (!phys_ram_base) { |
| 8466 | fprintf(stderr, "Could not allocate physical memory\n"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 8467 | exit(1); |
| 8468 | } |
| 8469 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 8470 | bdrv_init(); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 8471 | |
| 8472 | /* we always create the cdrom drive, even if no disk is there */ |
| 8473 | |
| 8474 | if (nb_drives_opt < MAX_DRIVES) |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 8475 | drive_add(NULL, CDROM_ALIAS); |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 8476 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 8477 | /* we always create at least one floppy */ |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 8478 | |
| 8479 | if (nb_drives_opt < MAX_DRIVES) |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 8480 | drive_add(NULL, FD_ALIAS, 0); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 8481 | |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 8482 | /* we always create one sd slot, even if no card is in it */ |
| 8483 | |
| 8484 | if (nb_drives_opt < MAX_DRIVES) |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 8485 | drive_add(NULL, SD_ALIAS); |
balrog | 9d413d1 | 2007-12-04 00:10:34 +0000 | [diff] [blame] | 8486 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 8487 | /* open the virtual block devices */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 8488 | |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 8489 | for(i = 0; i < nb_drives_opt; i++) |
balrog | 609497a | 2008-01-14 02:56:53 +0000 | [diff] [blame] | 8490 | if (drive_init(&drives_opt[i], snapshot, machine) == -1) |
ths | e4bcb14 | 2007-12-02 04:51:10 +0000 | [diff] [blame] | 8491 | exit(1); |
balrog | 3e3d581 | 2007-04-30 02:09:25 +0000 | [diff] [blame] | 8492 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 8493 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
| 8494 | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 8495 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 8496 | init_ioports(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 8497 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 8498 | /* terminal init */ |
ths | 740733b | 2007-06-08 01:57:56 +0000 | [diff] [blame] | 8499 | memset(&display_state, 0, sizeof(display_state)); |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 8500 | if (nographic) { |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 8501 | if (curses) { |
| 8502 | fprintf(stderr, "fatal: -nographic can't be used with -curses\n"); |
| 8503 | exit(1); |
| 8504 | } |
ths | 2ff8979 | 2007-06-21 23:34:19 +0000 | [diff] [blame] | 8505 | /* nearly nothing to do */ |
| 8506 | dumb_display_init(ds); |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 8507 | } else if (vnc_display != NULL) { |
ths | 71cab5c | 2007-08-25 01:35:38 +0000 | [diff] [blame] | 8508 | vnc_display_init(ds); |
| 8509 | if (vnc_display_open(ds, vnc_display) < 0) |
| 8510 | exit(1); |
balrog | 4d3b6f6 | 2008-02-10 16:33:14 +0000 | [diff] [blame] | 8511 | } else |
| 8512 | #if defined(CONFIG_CURSES) |
| 8513 | if (curses) { |
| 8514 | curses_display_init(ds, full_screen); |
| 8515 | } else |
| 8516 | #endif |
| 8517 | { |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 8518 | #if defined(CONFIG_SDL) |
ths | 43523e9 | 2007-02-18 18:19:32 +0000 | [diff] [blame] | 8519 | sdl_display_init(ds, full_screen, no_frame); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 8520 | #elif defined(CONFIG_COCOA) |
| 8521 | cocoa_display_init(ds, full_screen); |
pbrook | 67276f5 | 2007-11-15 19:04:08 +0000 | [diff] [blame] | 8522 | #else |
| 8523 | dumb_display_init(ds); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 8524 | #endif |
| 8525 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 8526 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 8527 | /* Maintain compatibility with multiple stdio monitors */ |
| 8528 | if (!strcmp(monitor_device,"stdio")) { |
| 8529 | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8530 | const char *devname = serial_devices[i]; |
| 8531 | if (devname && !strcmp(devname,"mon:stdio")) { |
| 8532 | monitor_device = NULL; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 8533 | break; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8534 | } else if (devname && !strcmp(devname,"stdio")) { |
| 8535 | monitor_device = NULL; |
| 8536 | serial_devices[i] = "mon:stdio"; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 8537 | break; |
| 8538 | } |
| 8539 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 8540 | } |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8541 | if (monitor_device) { |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame] | 8542 | monitor_hd = qemu_chr_open(monitor_device); |
| 8543 | if (!monitor_hd) { |
| 8544 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); |
| 8545 | exit(1); |
| 8546 | } |
| 8547 | monitor_init(monitor_hd, !nographic); |
| 8548 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 8549 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 8550 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 8551 | const char *devname = serial_devices[i]; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8552 | if (devname && strcmp(devname, "none")) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 8553 | serial_hds[i] = qemu_chr_open(devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 8554 | if (!serial_hds[i]) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 8555 | fprintf(stderr, "qemu: could not open serial device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 8556 | devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 8557 | exit(1); |
| 8558 | } |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 8559 | if (strstart(devname, "vc", 0)) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 8560 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 8561 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 8562 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 8563 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 8564 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 8565 | const char *devname = parallel_devices[i]; |
pbrook | fd5f393 | 2008-03-26 20:55:43 +0000 | [diff] [blame] | 8566 | if (devname && strcmp(devname, "none")) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 8567 | parallel_hds[i] = qemu_chr_open(devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 8568 | if (!parallel_hds[i]) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 8569 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 8570 | devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 8571 | exit(1); |
| 8572 | } |
ths | af3a903 | 2007-07-11 23:14:59 +0000 | [diff] [blame] | 8573 | if (strstart(devname, "vc", 0)) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 8574 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 8575 | } |
| 8576 | } |
| 8577 | |
blueswir1 | b881c2c | 2007-11-18 08:46:58 +0000 | [diff] [blame] | 8578 | machine->init(ram_size, vga_ram_size, boot_devices, ds, |
j_mayer | 94fc95c | 2007-03-05 19:44:02 +0000 | [diff] [blame] | 8579 | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 8580 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 8581 | /* init USB devices */ |
| 8582 | if (usb_enabled) { |
| 8583 | for(i = 0; i < usb_devices_index; i++) { |
| 8584 | if (usb_device_add(usb_devices[i]) < 0) { |
| 8585 | fprintf(stderr, "Warning: could not add USB device %s\n", |
| 8586 | usb_devices[i]); |
| 8587 | } |
| 8588 | } |
| 8589 | } |
| 8590 | |
ths | 740733b | 2007-06-08 01:57:56 +0000 | [diff] [blame] | 8591 | if (display_state.dpy_refresh) { |
| 8592 | display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state); |
| 8593 | qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock)); |
| 8594 | } |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 8595 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 8596 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 8597 | if (use_gdbstub) { |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 8598 | /* XXX: use standard host:port notation and modify options |
| 8599 | accordingly. */ |
pbrook | cfc3475 | 2007-02-22 01:48:01 +0000 | [diff] [blame] | 8600 | if (gdbserver_start(gdbstub_port) < 0) { |
| 8601 | fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n", |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 8602 | gdbstub_port); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 8603 | exit(1); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 8604 | } |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 8605 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 8606 | #endif |
balrog | 45669e0 | 2007-07-02 13:20:17 +0000 | [diff] [blame] | 8607 | |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 8608 | if (loadvm) |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 8609 | do_loadvm(loadvm); |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 8610 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 8611 | { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 8612 | /* XXX: simplify init */ |
| 8613 | read_passwords(); |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 8614 | if (autostart) { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 8615 | vm_start(); |
| 8616 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 8617 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 8618 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 8619 | if (daemonize) { |
| 8620 | uint8_t status = 0; |
| 8621 | ssize_t len; |
| 8622 | int fd; |
| 8623 | |
| 8624 | again1: |
| 8625 | len = write(fds[1], &status, 1); |
| 8626 | if (len == -1 && (errno == EINTR)) |
| 8627 | goto again1; |
| 8628 | |
| 8629 | if (len != 1) |
| 8630 | exit(1); |
| 8631 | |
balrog | aeb30be | 2007-07-02 15:03:13 +0000 | [diff] [blame] | 8632 | TFR(fd = open("/dev/null", O_RDWR)); |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 8633 | if (fd == -1) |
| 8634 | exit(1); |
| 8635 | |
| 8636 | dup2(fd, 0); |
| 8637 | dup2(fd, 1); |
| 8638 | dup2(fd, 2); |
| 8639 | |
| 8640 | close(fd); |
| 8641 | } |
| 8642 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 8643 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 8644 | quit_timers(); |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 8645 | |
ths | 7d294b6 | 2007-10-26 17:21:58 +0000 | [diff] [blame] | 8646 | #if !defined(_WIN32) |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 8647 | /* close network clients */ |
| 8648 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 8649 | VLANClientState *vc; |
| 8650 | |
ths | 7d294b6 | 2007-10-26 17:21:58 +0000 | [diff] [blame] | 8651 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
ths | b46a890 | 2007-10-21 23:20:45 +0000 | [diff] [blame] | 8652 | if (vc->fd_read == tap_receive) { |
| 8653 | char ifname[64]; |
| 8654 | TAPState *s = vc->opaque; |
| 8655 | |
| 8656 | if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && |
| 8657 | s->down_script[0]) |
| 8658 | launch_script(s->down_script, ifname, s->fd); |
| 8659 | } |
ths | 4fddf62 | 2007-12-17 04:42:29 +0000 | [diff] [blame] | 8660 | } |
ths | 7d294b6 | 2007-10-26 17:21:58 +0000 | [diff] [blame] | 8661 | } |
| 8662 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 8663 | return 0; |
| 8664 | } |